From 6c9d60f49a768a8b4677a7e36734d5860af21bd0 Mon Sep 17 00:00:00 2001 From: ryanccarelli Date: Fri, 24 Dec 2021 14:14:36 -0500 Subject: [PATCH 1/3] fix read/write bug in h5path for anndata counts matrix --- pathml/core/h5managers.py | 5 ++--- pathml/core/utils.py | 12 +++++++----- pathml/preprocessing/transforms.py | 22 ++++++---------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/pathml/core/h5managers.py b/pathml/core/h5managers.py index 676be184..cc35932b 100755 --- a/pathml/core/h5managers.py +++ b/pathml/core/h5managers.py @@ -155,9 +155,7 @@ def add_tile(self, tile): # add tile-level masks for key, mask in tile.masks.items(): self.h5["tiles"][str(tile.coords)]["masks"].create_dataset( - str(key), - data=mask, - dtype="float16", + str(key), data=mask, dtype="float16", ) # add coords @@ -177,6 +175,7 @@ def add_tile(self, tile): if self.counts: self.counts = self.counts.to_memory() self.counts = self.counts.concatenate(tile.counts, join="outer") + del self.counts.obs["batch"] self.counts.filename = os.path.join( self.countspath.name + "/tmpfile.h5ad" ) diff --git a/pathml/core/utils.py b/pathml/core/utils.py index d2b33922..cba687d9 100644 --- a/pathml/core/utils.py +++ b/pathml/core/utils.py @@ -110,8 +110,10 @@ def readcounts(h5): # create and save temp h5py file # read using anndata from temp file # anndata does not support reading directly from h5 - path = tempfile.NamedTemporaryFile() - f = h5py.File(path, "w") - for ds in h5.keys(): - h5.copy(ds, f) - return anndata.read_h5ad(path.name) + with tempfile.NamedTemporaryFile() as path: + with h5py.File(path, "w") as f: + for ds in h5.keys(): + h5.copy(ds, f) + print(path) + print(path.name) + return anndata.read_h5ad(path.name) diff --git a/pathml/preprocessing/transforms.py b/pathml/preprocessing/transforms.py index c52557ef..35b7de27 100644 --- a/pathml/preprocessing/transforms.py +++ b/pathml/preprocessing/transforms.py @@ -13,15 +13,11 @@ import pathml.core import pathml.core.slide_data import spams -from pathml.utils import ( - RGB_to_GREY, - RGB_to_HSI, - RGB_to_HSV, - RGB_to_OD, - normalize_matrix_cols, -) +from pathml.utils import (RGB_to_GREY, RGB_to_HSI, RGB_to_HSV, RGB_to_OD, + normalize_matrix_cols) from skimage import restoration -from skimage.exposure import equalize_adapthist, equalize_hist, rescale_intensity +from skimage.exposure import (equalize_adapthist, equalize_hist, + rescale_intensity) from skimage.measure import regionprops_table @@ -275,10 +271,7 @@ def F(self, image): image.ndim == 2 ), f"input image has shape {image.shape}. Must convert to 1-channel image (H, W)." _, out = cv2.threshold( - src=image, - thresh=self.threshold, - maxval=self.max_value, - type=self.type, + src=image, thresh=self.threshold, maxval=self.max_value, type=self.type, ) return out.astype(np.uint8) @@ -1431,9 +1424,7 @@ def F(self, tile): ], ) counts.obs = counts.obs.rename(columns={0: "x", 1: "y"}) - counts.obs["coords"] = str(countsdataframe["coords"]) counts.obs["filled_area"] = countsdataframe["filled_area"] - counts.obs["slice"] = str(countsdataframe["slice"]) counts.obs["euler_number"] = countsdataframe["euler_number"] min_intensities = pd.DataFrame() for i in range(img.shape[-1]): @@ -1446,8 +1437,7 @@ def F(self, tile): try: counts.obsm["spatial"] = np.array(counts.obs[["x", "y"]]) except: - pass - counts.obs["tile"] = str(tile.coords) + print("warning: did not log coordinates in obsm") return counts def apply(self, tile): From 9fe331b766511c2fc686ab9b330414d62ef8609d Mon Sep 17 00:00:00 2001 From: ryanccarelli Date: Fri, 24 Dec 2021 15:30:58 -0500 Subject: [PATCH 2/3] remove print statements --- pathml/core/utils.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pathml/core/utils.py b/pathml/core/utils.py index cba687d9..926fe636 100644 --- a/pathml/core/utils.py +++ b/pathml/core/utils.py @@ -114,6 +114,4 @@ def readcounts(h5): with h5py.File(path, "w") as f: for ds in h5.keys(): h5.copy(ds, f) - print(path) - print(path.name) return anndata.read_h5ad(path.name) From 99e6cddd45f008fe28b85da531cfa1f8dd9022f1 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 25 Dec 2021 17:11:57 -0500 Subject: [PATCH 3/3] bump version number --- pathml/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pathml/_version.py b/pathml/_version.py index 8b253f81..26609d7c 100644 --- a/pathml/_version.py +++ b/pathml/_version.py @@ -3,4 +3,4 @@ License: GNU GPL 2.0 """ -__version__ = "2.0.0" +__version__ = "2.0.1"