Skip to content

Commit

Permalink
moved tiff2torch
Browse files Browse the repository at this point in the history
  • Loading branch information
bmandracchia committed Aug 28, 2024
1 parent 5abe59e commit edada14
Show file tree
Hide file tree
Showing 6 changed files with 3,795 additions and 70 deletions.
2 changes: 1 addition & 1 deletion bioMONAI/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'bioMONAI.io.h5_reader': ('io.html#h5_reader', 'bioMONAI/io.py'),
'bioMONAI.io.img_reader': ('io.html#img_reader', 'bioMONAI/io.py'),
'bioMONAI.io.lif_reader': ('io.html#lif_reader', 'bioMONAI/io.py'),
'bioMONAI.io.tiff2torch': ('io.html#tiff2torch', 'bioMONAI/io.py'),
'bioMONAI.io.tiff_reader': ('io.html#tiff_reader', 'bioMONAI/io.py')},
'bioMONAI.losses': { 'bioMONAI.losses.CombinedLoss': ('losses.html#combinedloss', 'bioMONAI/losses.py'),
'bioMONAI.losses.CombinedLoss.__call__': ('losses.html#combinedloss.__call__', 'bioMONAI/losses.py'),
Expand Down Expand Up @@ -113,5 +114,4 @@
'bioMONAI.visualize.mosaic_image_3d': ('visualize.html#mosaic_image_3d', 'bioMONAI/visualize.py'),
'bioMONAI.visualize.show_images_grid': ('visualize.html#show_images_grid', 'bioMONAI/visualize.py'),
'bioMONAI.visualize.show_plane': ('visualize.html#show_plane', 'bioMONAI/visualize.py'),
'bioMONAI.visualize.tiff2torch': ('visualize.html#tiff2torch', 'bioMONAI/visualize.py'),
'bioMONAI.visualize.visualize_slices': ('visualize.html#visualize_slices', 'bioMONAI/visualize.py')}}}
30 changes: 20 additions & 10 deletions bioMONAI/io.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_io.ipynb.

# %% auto 0
__all__ = ['tiff_reader', 'lif_reader', 'czi_reader', 'h5_reader', 'img_reader']
__all__ = ['tiff2torch', 'tiff_reader', 'lif_reader', 'czi_reader', 'h5_reader', 'img_reader']

# %% ../nbs/02_io.ipynb 3
from fastai.vision.all import *
from fastai.data.all import *
from torchio import ScalarImage, ToCanonical, Resample
import multipagetiff as mtif

from .core import torchTensor
from .core import torchTensor, torch_from_numpy

# %% ../nbs/02_io.ipynb 6
def tiff2torch(file_path: str):
'''
Load tiff into pytorch tensor
'''
import tifffile as tiff

img = np.array(tiff.imread(file_path))
return torch_from_numpy(img)

# %% ../nbs/02_io.ipynb 7
def tiff_reader(path, # The path to the TIFF file to be read
units='um', # The units for the image data.
):
Expand Down Expand Up @@ -44,7 +54,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/02_io.ipynb 9
# %% ../nbs/02_io.ipynb 10
from aicsimageio import AICSImage

def lif_reader(path, # The path to the LIF file to be read
Expand Down Expand Up @@ -82,7 +92,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/02_io.ipynb 12
# %% ../nbs/02_io.ipynb 13
from aicsimageio.readers import CziReader

def czi_reader(path, # The path to the CZI file to be read
Expand Down Expand Up @@ -112,7 +122,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/02_io.ipynb 15
# %% ../nbs/02_io.ipynb 16
def _image_reader(path, # The file path to the image
):

Expand Down Expand Up @@ -165,7 +175,7 @@ def _image_reader(path, # The file path to the image
return data, affine


# %% ../nbs/02_io.ipynb 18
# %% ../nbs/02_io.ipynb 19
import h5py

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


# %% ../nbs/02_io.ipynb 21
# %% ../nbs/02_io.ipynb 22
def _preprocess(obj, # The object to preprocess
reorder, # Whether to reorder the object
resample # Whether to resample the object
Expand Down Expand Up @@ -213,7 +223,7 @@ def _preprocess(obj, # The object to preprocess
return obj, original_size


# %% ../nbs/02_io.ipynb 23
# %% ../nbs/02_io.ipynb 24
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 @@ -237,7 +247,7 @@ def _load_and_preprocess(file_path, # Image file path
return org_img, input_img, org_size


# %% ../nbs/02_io.ipynb 26
# %% ../nbs/02_io.ipynb 27
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 @@ -273,7 +283,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/02_io.ipynb 29
# %% ../nbs/02_io.ipynb 30
def img_reader(file_path: (str, Path, L, list), # Path to the image
dtype=torchTensor, # Datatype for the return value. Defaults to torchTensor
reorder: bool = False, # Whether to reorder to canonical orientation
Expand Down
18 changes: 4 additions & 14 deletions bioMONAI/visualize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/09_visualize.ipynb.

# %% auto 0
__all__ = ['mosaic_image_3d', 'tiff2torch', 'show_images_grid', 'show_plane', 'visualize_slices']
__all__ = ['mosaic_image_3d', 'show_images_grid', 'show_plane', 'visualize_slices']

# %% ../nbs/09_visualize.ipynb 3
import numpy as np
Expand All @@ -16,14 +16,15 @@
from fastai.vision.all import plt, ifnone

from .core import torchTensor, tensor, delegates, torch_from_numpy, hasattrs
from .io import tiff2torch

# %% ../nbs/09_visualize.ipynb 5
# %% ../nbs/09_visualize.ipynb 6
# Utility function for determining figure bounds
def _fig_bounds(x):
r = x//8
return min(15, max(1,r))

# %% ../nbs/09_visualize.ipynb 6
# %% ../nbs/09_visualize.ipynb 7
def mosaic_image_3d(t: (np.ndarray, torchTensor),
axis: int = 0,
figsize: tuple = (15,15),
Expand Down Expand Up @@ -77,17 +78,6 @@ def mosaic_image_3d(t: (np.ndarray, torchTensor),
plt.imshow(grid[0,:,:], cmap=cmap, alpha=alpha)
plt.axis('off')

# %% ../nbs/09_visualize.ipynb 7
def tiff2torch(file_path: str):
'''
Load tiff into pytorch tensor
'''
import tifffile as tiff

img = np.array(tiff.imread(file_path))
return torch_from_numpy(img)


# %% ../nbs/09_visualize.ipynb 8
@delegates(plt.Axes.imshow, keep=True, but=['shape', 'imlim'])
def show_images_grid(images, ax=None, ncols=10, figsize=None, title=None, spacing=0.02, max_slices=3, ctx=None, **kwargs):
Expand Down
Loading

0 comments on commit edada14

Please sign in to comment.