Skip to content

Commit

Permalink
data 'tif' support
Browse files Browse the repository at this point in the history
  • Loading branch information
jpitalopez committed Jun 25, 2024
1 parent 898c88c commit 1ee8623
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 100 deletions.
48 changes: 29 additions & 19 deletions bioMONAI/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def tiff_reader(path, # The path to the TIFF file to be read
# Return the image data and the affine matrix
return data, affine

# %% ../nbs/03_data.ipynb 9
# %% ../nbs/03_data.ipynb 11
from aicsimageio import AICSImage

def lif_reader(path, # The path to the LIF file to be read
Expand Down Expand Up @@ -83,7 +83,7 @@ def lif_reader(path, # The path to the LIF file to be read
# Return the image data and the affine matrix
return data, affine

# %% ../nbs/03_data.ipynb 12
# %% ../nbs/03_data.ipynb 14
from aicsimageio.readers import CziReader

def czi_reader(path, # The path to the CZI file to be read
Expand Down Expand Up @@ -113,7 +113,7 @@ def czi_reader(path, # The path to the CZI file to be read
# Return the image data and the affine matrix
return data, affine

# %% ../nbs/03_data.ipynb 15
# %% ../nbs/03_data.ipynb 17
def _image_reader(path, # The file path to the image
):

Expand Down Expand Up @@ -143,6 +143,16 @@ def _image_reader(path, # The file path to the image

return data, affine

if (path[-3:]=="tif"):

# Reorder for tiff files
data = image_aics.get_image_data("CZYX", T=0) # returns 4D CZYX numpy array

affine = np.eye(4) #to change

return data, affine


# Convert to numpy array
data = image_aics.data

Expand All @@ -156,7 +166,7 @@ def _image_reader(path, # The file path to the image
return data, affine


# %% ../nbs/03_data.ipynb 18
# %% ../nbs/03_data.ipynb 20
import h5py

def h5_reader(path, dataset):
Expand All @@ -170,7 +180,7 @@ def h5_reader(path, dataset):
return dataset1


# %% ../nbs/03_data.ipynb 21
# %% ../nbs/03_data.ipynb 23
def _preprocess(obj, # The object to preprocess
reorder, # Whether to reorder the object
resample # Whether to resample the object
Expand Down Expand Up @@ -204,7 +214,7 @@ def _preprocess(obj, # The object to preprocess
return obj, original_size


# %% ../nbs/03_data.ipynb 23
# %% ../nbs/03_data.ipynb 25
def _load_and_preprocess(file_path, # Image file path
reorder=False, # Whether to reorder data for canonical (RAS+) orientation
resample=False, # Whether to resample image to different voxel sizes and dimensions
Expand All @@ -228,7 +238,7 @@ def _load_and_preprocess(file_path, # Image file path
return org_img, input_img, org_size


# %% ../nbs/03_data.ipynb 26
# %% ../nbs/03_data.ipynb 28
def _multi_channel(image_paths: (L, list), # List of image paths (e.g., T1, T2, T1CE, DWI)
reorder: bool = False, # Whether to reorder data for canonical (RAS+) orientation
resample: list = None, # Whether to resample image to different voxel sizes and dimensions
Expand Down Expand Up @@ -264,7 +274,7 @@ def _multi_channel(image_paths: (L, list), # List of image paths (e.g., T1, T2,
input_img.set_data(tensor)
return org_img, input_img, org_size

# %% ../nbs/03_data.ipynb 29
# %% ../nbs/03_data.ipynb 31
def img_reader(file_path: (str, Path, L, list), # Path to the image
dtype=torch.Tensor, # Datatype for the return value. Defaults to torch.Tensor
reorder: bool = False, # Whether to reorder to canonical orientation
Expand Down Expand Up @@ -303,7 +313,7 @@ def img_reader(file_path: (str, Path, L, list), # Path to the image
return org_img, input_img, org_size


# %% ../nbs/03_data.ipynb 33
# %% ../nbs/03_data.ipynb 35
class MetaResolver(type(torch.Tensor), metaclass=BypassNewMeta):
"""
A class to bypass metaclass conflict:
Expand All @@ -312,12 +322,12 @@ class MetaResolver(type(torch.Tensor), metaclass=BypassNewMeta):
pass


# %% ../nbs/03_data.ipynb 35
# %% ../nbs/03_data.ipynb 37
from .core import show_images_grid, mosaic_image_3d
from monai.data import MetaTensor


# %% ../nbs/03_data.ipynb 36
# %% ../nbs/03_data.ipynb 38
class BioImageBase(MetaTensor, metaclass=MetaResolver):
"""
A class that represents an image object.
Expand Down Expand Up @@ -389,7 +399,7 @@ def __repr__(self) -> str:
"""Returns the string representation of the ImageBase instance."""
return f"BioImageBase{self.as_tensor().__repr__()[6:]}"

# %% ../nbs/03_data.ipynb 38
# %% ../nbs/03_data.ipynb 40
class BioImage(BioImageBase):
"""Subclass of BioImageBase that represents 2D and 3D image objects."""
_show_args = {'cmap':'gray'}
Expand Down Expand Up @@ -423,15 +433,15 @@ def __repr__(self) -> str:
# return f'{self.__class__.__name__} shape={"x".join([str(d) for d in self.shape])}'
return f"BioImage{self.as_tensor().__repr__()[6:]}"

# %% ../nbs/03_data.ipynb 41
# %% ../nbs/03_data.ipynb 43
class BioImageStack(BioImageBase):
"""Subclass of BioImageBase that represents a 3D image object."""

def __repr__(self) -> str:
"""Returns the string representation of the ImageBase instance."""
return f"BioImageStack{self.as_tensor().__repr__()[6:]}"

# %% ../nbs/03_data.ipynb 44
# %% ../nbs/03_data.ipynb 46
class BioImageProject(BioImageBase):
"""Subclass of BioImageBase that represents a 2D image object."""
_show_args = {'cmap':'gray'}
Expand Down Expand Up @@ -465,7 +475,7 @@ def __repr__(self) -> str:
"""Returns the string representation of the ImageBase instance."""
return f"BioImage{self.as_tensor().__repr__()[6:]}"

# %% ../nbs/03_data.ipynb 47
# %% ../nbs/03_data.ipynb 49
class BioImageMulti(BioImageBase):
"""Subclass of BioImageBase that represents a multi-channel 2D image object."""

Expand Down Expand Up @@ -494,7 +504,7 @@ def __repr__(self) -> str:
return f"BioImageMulti{self.as_tensor().__repr__()[6:]}"


# %% ../nbs/03_data.ipynb 53
# %% ../nbs/03_data.ipynb 55
class Tensor2BioImage(DisplayedTransform):
def __init__(self, cls:BioImageBase=BioImageStack):
self.cls = cls
Expand All @@ -506,20 +516,20 @@ def encodes(self, o):
if isinstance(o, torch.Tensor):
return self.cls(o)

# %% ../nbs/03_data.ipynb 56
# %% ../nbs/03_data.ipynb 58
def BioImageBlock(cls:BioImageBase=BioImageStack):
"A `TransformBlock` for images of `cls`"
return TransformBlock(type_tfms=cls.create, batch_tfms=[Tensor2BioImage(cls)]) # IntToFloatTensor

# %% ../nbs/03_data.ipynb 59
# %% ../nbs/03_data.ipynb 61
@typedispatch
def show_batch(x:BioImageBase, y:BioImageBase, samples, ctxs=None, max_n=10, nrows=None, ncols=None, figsize=None, **kwargs):
if ctxs is None: ctxs = get_grid(min(len(samples), max_n), nrows=nrows, ncols=ncols, figsize=figsize, double=True)
for i in range(2):
ctxs[i::2] = [b.show(ctx=c, **kwargs) for b,c,_ in zip(samples.itemgot(i),ctxs[i::2],range(max_n))]
return ctxs

# %% ../nbs/03_data.ipynb 61
# %% ../nbs/03_data.ipynb 63
@typedispatch
def show_results(x:BioImageBase, y:BioImageBase, samples, outs, ctxs=None, max_n=10, figsize=None, **kwargs):
if ctxs is None: ctxs = get_grid(3*min(len(samples), max_n), ncols=3, figsize=figsize, title='Input/Target/Prediction')
Expand Down
Loading

0 comments on commit 1ee8623

Please sign in to comment.