|
13 | 13 | import cv2 |
14 | 14 | import dask.array as da |
15 | 15 | import joblib |
16 | | -import numcodecs |
17 | 16 | import numpy as np |
18 | 17 | import pandas as pd |
19 | 18 | import requests |
@@ -1628,187 +1627,6 @@ def write_probability_heatmap_as_ome_tiff( |
1628 | 1627 | logger.info(msg) |
1629 | 1628 |
|
1630 | 1629 |
|
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 | | - |
1812 | 1630 | def get_tqdm() -> type[tqdm_notebook | tqdm]: |
1813 | 1631 | """Returns appropriate tqdm tqdm object.""" |
1814 | 1632 | if is_notebook(): # pragma: no cover |
|
0 commit comments