Skip to content

Commit

Permalink
Merge pull request #260 from Dana-Farber-AIOS/dev-h5pathcounts
Browse files Browse the repository at this point in the history
Improve h5path read/write
  • Loading branch information
jacob-rosenthal committed Dec 25, 2021
2 parents dfb3d11 + 99e6cdd commit 2721cb7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pathml/_version.py
Expand Up @@ -3,4 +3,4 @@
License: GNU GPL 2.0
"""

__version__ = "2.0.0"
__version__ = "2.0.1"
5 changes: 2 additions & 3 deletions pathml/core/h5managers.py
Expand Up @@ -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
Expand All @@ -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"
)
Expand Down
10 changes: 5 additions & 5 deletions pathml/core/utils.py
Expand Up @@ -110,8 +110,8 @@ 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)
return anndata.read_h5ad(path.name)
22 changes: 6 additions & 16 deletions pathml/preprocessing/transforms.py
Expand Up @@ -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


Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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]):
Expand All @@ -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):
Expand Down

0 comments on commit 2721cb7

Please sign in to comment.