Skip to content

Commit 6e809f9

Browse files
committed
🔥 Remove unnecessary code.
1 parent eca8fc8 commit 6e809f9

File tree

2 files changed

+0
-219
lines changed

2 files changed

+0
-219
lines changed

tests/test_utils.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,43 +1742,6 @@ def test_patch_pred_store_sf() -> None:
17421742
assert annotation.geometry.area == 4
17431743

17441744

1745-
def test_patch_pred_store_zarr(tmp_path: pytest.TempPathFactory) -> None:
1746-
"""Test patch_pred_store_zarr."""
1747-
# Define a mock patch_output
1748-
patch_output = {
1749-
"predictions": [1, 0, 1],
1750-
"coordinates": [(0, 0, 1, 1), (1, 1, 2, 2), (2, 2, 3, 3)],
1751-
"probabilities": [[0.1, 0.9], [0.9, 0.1], [0.4, 0.6]],
1752-
"labels": [1, 0, 1],
1753-
}
1754-
1755-
save_path = tmp_path / "patch_output" / "output.zarr"
1756-
1757-
store_path = misc.dict_to_zarr(patch_output, save_path=save_path)
1758-
1759-
print("Zarr path: ", store_path)
1760-
assert Path.exists(store_path), "Zarr output file does not exist"
1761-
1762-
1763-
def test_patch_pred_store_zarr_ext(tmp_path: pytest.TempPathFactory) -> None:
1764-
"""Test patch_pred_store_zarr and ensures the output file extension is `.zarr`."""
1765-
# Define a mock patch_output
1766-
patch_output = {
1767-
"predictions": [1, 0, 1],
1768-
"coordinates": [(0, 0, 1, 1), (1, 1, 2, 2), (2, 2, 3, 3)],
1769-
"probabilities": [[0.1, 0.9], [0.9, 0.1], [0.4, 0.6]],
1770-
"labels": [1, 0, 1],
1771-
}
1772-
1773-
# sends the path of a jpeg source image, expects .zarr file in the same directory
1774-
save_path = tmp_path / "patch_output" / "patch.jpeg"
1775-
1776-
store_path = misc.dict_to_zarr(patch_output, save_path=save_path)
1777-
1778-
print("Zarr path: ", store_path)
1779-
assert Path.exists(store_path), "Zarr output file does not exist"
1780-
1781-
17821745
def test_patch_pred_store_persist(tmp_path: pytest.TempPathFactory) -> None:
17831746
"""Test patch_pred_store. and persists store output to a .db file."""
17841747
# Define a mock patch_output

tiatoolbox/utils/misc.py

Lines changed: 0 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import cv2
1414
import dask.array as da
1515
import joblib
16-
import numcodecs
1716
import numpy as np
1817
import pandas as pd
1918
import requests
@@ -1628,187 +1627,6 @@ def write_probability_heatmap_as_ome_tiff(
16281627
logger.info(msg)
16291628

16301629

1631-
def dict_to_zarr(
1632-
raw_predictions: dict,
1633-
save_path: Path,
1634-
**kwargs: dict,
1635-
) -> Path:
1636-
"""Saves the output of TIAToolbox engines to a zarr file.
1637-
1638-
Args:
1639-
raw_predictions (dict):
1640-
A dictionary in the TIAToolbox Engines output format.
1641-
save_path (str or Path):
1642-
Path to save the zarr file.
1643-
**kwargs (dict):
1644-
Keyword Args to update patch_pred_store_zarr attributes.
1645-
1646-
1647-
Returns:
1648-
Path to zarr file storing the patch predictor output
1649-
1650-
"""
1651-
# Default values for Compressor and Chunks set if not received from kwargs.
1652-
compressor = kwargs.get("compressor", numcodecs.Zstd(level=1))
1653-
1654-
# ensure proper zarr extension
1655-
save_path = save_path.parent.absolute() / (save_path.stem + ".zarr")
1656-
1657-
z = zarr.open(str(save_path), mode="w")
1658-
1659-
for key, value in raw_predictions.items():
1660-
# save to zarr
1661-
array = np.array(value)
1662-
z.create_dataset(
1663-
name=key,
1664-
data=value,
1665-
compression=compressor,
1666-
shape=array.shape,
1667-
chunks=array.shape,
1668-
)
1669-
1670-
return save_path
1671-
1672-
1673-
def wsi_batch_output_to_zarr_group(
1674-
wsi_batch_zarr_group: zarr.group | None,
1675-
batch_output_probabilities: np.ndarray,
1676-
batch_output_predictions: np.ndarray,
1677-
batch_output_coordinates: np.ndarray | None,
1678-
batch_output_label: np.ndarray | None,
1679-
save_path: Path,
1680-
**kwargs: dict,
1681-
) -> zarr.group | Path:
1682-
"""Saves the intermediate batch outputs of TIAToolbox engines to a zarr file.
1683-
1684-
Args:
1685-
wsi_batch_zarr_group (zarr.group):
1686-
Optional zarr group name consisting of zarrs to save the batch output
1687-
values.
1688-
batch_output_probabilities (np.ndarray):
1689-
Probability batch output from infer wsi.
1690-
batch_output_predictions (np.ndarray):
1691-
Predictions batch output from infer wsi.
1692-
batch_output_coordinates (np.ndarray):
1693-
Coordinates batch output from infer wsi.
1694-
batch_output_label (np.ndarray):
1695-
Labels batch output from infer wsi.
1696-
save_path (str or Path):
1697-
Path to save the zarr file.
1698-
**kwargs (dict):
1699-
Keyword Args to update wsi_batch_output_to_zarr_group attributes.
1700-
1701-
Returns:
1702-
Path to the zarr file storing the :class:`EngineABC` output.
1703-
1704-
"""
1705-
# Default values for Compressor and Chunks set if not received from kwargs.
1706-
compressor = kwargs.get("compressor", numcodecs.Zstd(level=1))
1707-
1708-
# case 1 - new zarr group
1709-
if not wsi_batch_zarr_group:
1710-
# ensure proper zarr extension and create persistant zarr group
1711-
save_path = save_path.parent.absolute() / (save_path.stem + ".zarr")
1712-
wsi_batch_zarr_group = zarr.open(save_path, mode="w")
1713-
1714-
# populate the zarr group for the first time
1715-
probabilities_zarr = wsi_batch_zarr_group.create_dataset(
1716-
name="probabilities",
1717-
shape=batch_output_probabilities.shape,
1718-
chunks=batch_output_probabilities.shape,
1719-
compressor=compressor,
1720-
)
1721-
probabilities_zarr[:] = batch_output_probabilities
1722-
1723-
predictions_zarr = wsi_batch_zarr_group.create_dataset(
1724-
name="predictions",
1725-
shape=batch_output_predictions.shape,
1726-
chunks=batch_output_predictions.shape,
1727-
compressor=compressor,
1728-
)
1729-
predictions_zarr[:] = batch_output_predictions
1730-
1731-
if batch_output_coordinates is not None:
1732-
coordinates_zarr = wsi_batch_zarr_group.create_dataset(
1733-
name="coordinates",
1734-
shape=batch_output_coordinates.shape,
1735-
chunks=batch_output_coordinates.shape,
1736-
compressor=compressor,
1737-
)
1738-
coordinates_zarr[:] = batch_output_coordinates
1739-
1740-
if batch_output_label is not None:
1741-
labels_zarr = wsi_batch_zarr_group.create_dataset(
1742-
name="labels",
1743-
shape=batch_output_label.shape,
1744-
chunks=batch_output_label.shape,
1745-
compressor=compressor,
1746-
)
1747-
labels_zarr[:] = batch_output_label
1748-
1749-
# case 2 - append to existing zarr group
1750-
probabilities_zarr = wsi_batch_zarr_group["probabilities"]
1751-
probabilities_zarr.append(batch_output_probabilities)
1752-
1753-
predictions_zarr = wsi_batch_zarr_group["predictions"]
1754-
predictions_zarr.append(batch_output_predictions)
1755-
1756-
if batch_output_coordinates is not None:
1757-
coordinates_zarr = wsi_batch_zarr_group["coordinates"]
1758-
coordinates_zarr.append(batch_output_coordinates)
1759-
1760-
if batch_output_label is not None:
1761-
labels_zarr = wsi_batch_zarr_group["labels"]
1762-
labels_zarr.append(batch_output_label)
1763-
1764-
return wsi_batch_zarr_group
1765-
1766-
1767-
def write_to_zarr_in_cache_mode(
1768-
zarr_group: zarr.group,
1769-
output_data_to_save: dict,
1770-
**kwargs: dict,
1771-
) -> zarr.group | Path:
1772-
"""Saves the intermediate batch outputs of TIAToolbox engines to a zarr file.
1773-
1774-
Args:
1775-
zarr_group (zarr.group):
1776-
Zarr group name consisting of zarr(s) to save the batch output
1777-
values.
1778-
output_data_to_save (dict):
1779-
Output data from the Engine to save to Zarr. Expects the data saved in
1780-
dictionary to be a numpy array.
1781-
**kwargs (dict):
1782-
Keyword Args to update zarr_group attributes.
1783-
1784-
Returns:
1785-
Path to the zarr file storing the :class:`EngineABC` output.
1786-
1787-
"""
1788-
# Default values for Compressor and Chunks set if not received from kwargs.
1789-
compressor = kwargs.get("compressor", numcodecs.Zstd(level=1))
1790-
1791-
# case 1 - new zarr group
1792-
if not zarr_group:
1793-
for key, value in output_data_to_save.items():
1794-
# populate the zarr group for the first time
1795-
zarr_dataset = zarr_group.create_dataset(
1796-
name=key,
1797-
shape=value.shape,
1798-
compressor=compressor,
1799-
chunks=value.shape,
1800-
)
1801-
zarr_dataset[:] = value
1802-
1803-
return zarr_group
1804-
1805-
# case 2 - append to existing zarr group
1806-
for key, value in output_data_to_save.items():
1807-
zarr_group[key].append(value)
1808-
1809-
return zarr_group
1810-
1811-
18121630
def get_tqdm() -> type[tqdm_notebook | tqdm]:
18131631
"""Returns appropriate tqdm tqdm object."""
18141632
if is_notebook(): # pragma: no cover

0 commit comments

Comments
 (0)