Skip to content

Commit

Permalink
debug resample
Browse files Browse the repository at this point in the history
  • Loading branch information
bmandracchia committed Oct 15, 2024
1 parent c310e22 commit e4d7a9e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 91 deletions.
7 changes: 5 additions & 2 deletions bioMONAI/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@
'bioMONAI.transforms.RandRot90.encodes': ( 'transforms.html#randrot90.encodes',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample': ('transforms.html#resample', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample.__call__': ( 'transforms.html#resample.__call__',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample.__init__': ( 'transforms.html#resample.__init__',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample.encodes': ('transforms.html#resample.encodes', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensity': ('transforms.html#scaleintensity', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensity.__init__': ( 'transforms.html#scaleintensity.__init__',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensity.encodes': ( 'transforms.html#scaleintensity.encodes',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensityPercentiles': ( 'transforms.html#scaleintensitypercentiles',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensityPercentiles.__init__': ( 'transforms.html#scaleintensitypercentiles.__init__',
Expand Down
64 changes: 39 additions & 25 deletions bioMONAI/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_transforms.ipynb.

# %% auto 0
__all__ = ['Resample', 'RandCameraNoise', 'ScaleIntensityPercentiles', 'ScaleIntensityVariance', 'RandCrop2D', 'RandCropND',
'RandFlip', 'RandRot90']
__all__ = ['Resample', 'RandCameraNoise', 'ScaleIntensity', 'ScaleIntensityPercentiles', 'ScaleIntensityVariance', 'RandCrop2D',
'RandCropND', 'RandFlip', 'RandRot90']

# %% ../nbs/05_transforms.ipynb 3
import numpy as np
from fastai.vision.all import *
from fastai.data.all import *
from monai.transforms import SpatialCrop, Flip, Rotate90, Spacing, ScaleIntensity
from monai.transforms import SpatialCrop, Flip, Rotate90, Spacing
from numpy import percentile, isscalar, float32 as np_float32

from .data import BioImageBase, BioImageStack

# %% ../nbs/05_transforms.ipynb 5
class Resample(Spacing):
class Resample(Transform):
"""
A subclass of Spacing that handles image resampling based on specified sampling factors or voxel dimensions.
Expand Down Expand Up @@ -45,15 +45,13 @@ def __init__(self, sampling, **kwargs):
The Spacing class from which Resample inherits is initialized with either the provided pixdim or calculated based on the sampling factor and original image properties.
"""
if 'pixdim' in kwargs:
super().__init__(**kwargs)
self.spacing = Spacing(**kwargs)
else:
super().__init__(sampling, **kwargs)

def __call__(self, img: BioImageBase):
return super().__call__(img.data)

def encodes(self, img: BioImageBase):
return self.__call__(img.data)
self.spacing = Spacing(sampling, **kwargs)

def encodes(self, img:BioImageBase):
bioimagetype = type(img)
return bioimagetype(self.spacing(img))


# %% ../nbs/05_transforms.ipynb 9
Expand Down Expand Up @@ -127,7 +125,7 @@ def encodes(self,
return bioimagetype(adu)


# %% ../nbs/05_transforms.ipynb 15
# %% ../nbs/05_transforms.ipynb 14
def _scale_intensity_range(x, mi, ma, eps=1e-20, dtype=np_float32):
if dtype is not None:
x = x.astype(dtype, copy=False)
Expand All @@ -137,16 +135,27 @@ def _scale_intensity_range(x, mi, ma, eps=1e-20, dtype=np_float32):
x = (x - mi) / (ma - mi + eps)
return x

# %% ../nbs/05_transforms.ipynb 15
class ScaleIntensity(Transform):
"""Percentile-based image normalization."""
def __init__(x, min=0.0, max=1.0, axis=None, eps=1e-20, dtype=np_float32):
store_attr()

def encodes(self, x: BioImageBase):
bioimagetype = type(x)
return bioimagetype(_scale_intensity_range(x, self.min, self.max, eps=self.eps, dtype=self.dtype))

# %% ../nbs/05_transforms.ipynb 16
class ScaleIntensityPercentiles(Transform):
"""Percentile-based image normalization."""
def __init__(x, pmin=3, pmax=99.8, axis=None, eps=1e-20, dtype=np_float32):
store_attr()

def encodes(self, x: BioImageBase):
bioimagetype = type(x)
mi = percentile(x, self.pmin, axis=self.axis, keepdims=True)
ma = percentile(x, self.pmax, axis=self.axis, keepdims=True)
return _scale_intensity_range(x, mi, ma, eps=self.eps, dtype=self.dtype)
return bioimagetype(_scale_intensity_range(x, mi, ma, eps=self.eps, dtype=self.dtype))


# %% ../nbs/05_transforms.ipynb 17
Expand All @@ -162,6 +171,7 @@ def __init__(self, ndim=2):
store_attr()

def encodes(self, x: BioImageBase):
bioimagetype = type(x)
# Calculate the current variance of the image intensities
mean, variance = torch.mean(x), torch.var(x)

Expand All @@ -175,7 +185,7 @@ def encodes(self, x: BioImageBase):
else:
x = (x - mean) * scale_factor

return x
return bioimagetype(x)


# %% ../nbs/05_transforms.ipynb 20
Expand Down Expand Up @@ -216,8 +226,9 @@ def before_call(self,
h_rand = (hd, -1) if hd < 0 else (0, hd)
self.ctr = fastuple(random.randint(*w_rand)+self.size[0]//2, random.randint(*h_rand)+self.size[1]//2)

def encodes(self, x):
return SpatialCrop(roi_center=self.ctr, roi_size=self.size, lazy=self.lazy)(x)
def encodes(self, x: BioImageBase):
bioimagetype = type(x)
return bioimagetype(SpatialCrop(roi_center=self.ctr, roi_size=self.size, lazy=self.lazy)(x))

# %% ../nbs/05_transforms.ipynb 23
class RandCropND(RandTransform):
Expand Down Expand Up @@ -267,9 +278,10 @@ def before_call(self, b, split_idx: int):
self.tl = fastuple(*tl)
self.br = fastuple(*br)

def encodes(self, x):
def encodes(self, x:BioImageBase):
"Apply spatial crop transformation to the input image."
return SpatialCrop(roi_start=self.tl, roi_end=self.br, lazy=self.lazy)(x)
bioimagetype = type(x)
return bioimagetype(SpatialCrop(roi_start=self.tl, roi_end=self.br, lazy=self.lazy)(x))


# %% ../nbs/05_transforms.ipynb 25
Expand Down Expand Up @@ -299,11 +311,12 @@ def before_call(self, b, split_idx: int):
if self.spatial_axis is None:
self.spatial_axis = np.random.choice(np.arange(self.ndim), size=np.random.randint(1, self.ndim+1), replace=False, p=None)

def encodes(self, x):
def encodes(self, x:BioImageBase):
bioimagetype = type(x)
if self.flip:
return Flip(spatial_axis=self.spatial_axis, lazy=self.lazy)(x)
return bioimagetype(Flip(spatial_axis=self.spatial_axis, lazy=self.lazy)(x))
else:
return x
return bioimagetype(x)

# %% ../nbs/05_transforms.ipynb 27
class RandRot90(RandTransform):
Expand Down Expand Up @@ -333,8 +346,9 @@ def before_call(self, b, split_idx: int):
# if self.spatial_axes is None:
# self.spatial_axes = np.random.choice(np.arange(self.ndim), size=np.random.randint(1, self.ndim+1), replace=False, p=None)

def encodes(self, x):
def encodes(self, x:BioImageBase):
bioimagetype = type(x)
if self.rot90:
return Rotate90(k=self.k, spatial_axes=self.spatial_axes, lazy=self.lazy)(x)
return bioimagetype(Rotate90(k=self.k, spatial_axes=self.spatial_axes, lazy=self.lazy)(x))
else:
return x
return bioimagetype(x)
150 changes: 109 additions & 41 deletions nbs/05_transforms.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit e4d7a9e

Please sign in to comment.