Skip to content

Commit

Permalink
new - camera noise
Browse files Browse the repository at this point in the history
  • Loading branch information
bmandracchia committed Sep 3, 2024
1 parent 660becb commit c0dff39
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 130 deletions.
10 changes: 9 additions & 1 deletion bioMONAI/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
'bioMONAI.nets.get_padding': ('nets.html#get_padding', 'bioMONAI/nets.py'),
'bioMONAI.nets.interpolate': ('nets.html#interpolate', 'bioMONAI/nets.py')},
'bioMONAI.template': {'bioMONAI.template.foo': ('template.html#foo', 'bioMONAI/template.py')},
'bioMONAI.transforms': { 'bioMONAI.transforms.RandCrop2D': ('transforms.html#randcrop2d', 'bioMONAI/transforms.py'),
'bioMONAI.transforms': { 'bioMONAI.transforms.RandCameraNoise': ('transforms.html#randcameranoise', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.RandCrop2D': ('transforms.html#randcrop2d', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.RandCrop2D.__init__': ( 'transforms.html#randcrop2d.__init__',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.RandCrop2D.before_call': ( 'transforms.html#randcrop2d.before_call',
Expand Down Expand Up @@ -128,6 +129,13 @@
'bioMONAI/transforms.py'),
'bioMONAI.transforms.RandRot90.encodes': ( 'transforms.html#randrot90.encodes',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample': ('transforms.html#resample', 'bioMONAI/transforms.py'),
'bioMONAI.transforms.Resample.__init__': ( 'transforms.html#resample.__init__',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensityPercentiles': ( 'transforms.html#scaleintensitypercentiles',
'bioMONAI/transforms.py'),
'bioMONAI.transforms.ScaleIntensityRange': ( 'transforms.html#scaleintensityrange',
'bioMONAI/transforms.py'),
'bioMONAI.transforms._get_sz': ('transforms.html#_get_sz', 'bioMONAI/transforms.py'),
'bioMONAI.transforms._process_sz': ('transforms.html#_process_sz', 'bioMONAI/transforms.py')},
'bioMONAI.visualize': { 'bioMONAI.visualize._fig_bounds': ('visualize.html#_fig_bounds', 'bioMONAI/visualize.py'),
Expand Down
116 changes: 109 additions & 7 deletions bioMONAI/transforms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,117 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_transforms.ipynb.

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

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

# %% ../nbs/05_transforms.ipynb 13
# %% ../nbs/05_transforms.ipynb 5
class Resample(Spacing):
"""
A subclass of Spacing that handles image resampling based on specified sampling factors or voxel dimensions.
The `Resample` class inherits from `Spacing` and provides a flexible way to adjust the spacing (voxel size) of images by specifying either a sampling factor or explicitly providing new voxel dimensions.
Args:
sampling (int, optional): Sampling factor for isotropic resampling. Default is 1, indicating no change in resolution.
**kwargs: Additional keyword arguments that can include 'pixdim' to specify custom voxel dimensions.
Attributes:
pixdim (list or tuple): The voxel dimensions of the image after resampling. If not provided during initialization, this will be determined based on the sampling factor and original image properties.
Methods:
None
Example Usage:
# Creating a Resample object with a specified sampling factor
resampler = Resample(sampling=2)
# Creating a Resample object with custom voxel dimensions
resampler = Resample(**kwargs={'pixdim': [1.5, 1.5, 3]})
"""

def __init__(self, sampling, **kwargs):
"""
Initializes the Resample class instance.
Args:
sampling (int, optional): Sampling factor for isotropic resampling. Default is 1.
**kwargs: Additional keyword arguments that can include 'pixdim' to specify custom voxel dimensions.
If 'pixdim' is provided in kwargs, it will be used directly; otherwise, the sampling factor will determine the new voxel dimensions.
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)
else:
super().__init__(sampling, **kwargs)


# %% ../nbs/05_transforms.ipynb 8
def RandCameraNoise(input_image, qe=0.7, gain=2, exp_time=0.1,
dark_current=0.06, readout=2.5, bitdepth=16, offset=100,
seed=42, simulation = False, camera = 'cmos', gain_variance=.1, offset_variance=5):

# Random state
rs = np.random.RandomState(seed=seed)

# Add shot noise
input_photons = input_image if simulation else input_image / gain / qe
photons = rs.poisson(input_photons, size=input_photons.shape)

# Convert to electrons
electrons = qe * photons

# Add dark noise
dark_noise = rs.poisson(dark_current*exp_time, size=electrons.shape)
electrons += dark_noise

# Add readout noise
read_noise = rs.normal(scale=readout**2, size=electrons.shape)
electrons += read_noise

if camera == 'cmos':
gain = rs.normal(scale=gain_variance, size=electrons.shape) + gain + rs.normal(scale=gain_variance, size=electrons.shape[0])
fixed_pattern = rs.normal(scale=offset_variance, size=electrons.shape[0])
offset = rs.normal(scale=offset_variance, size=electrons.shape)+ fixed_pattern + offset

# Convert to ADU and add offset
max_adu = (2**bitdepth - 1)
adu = (electrons * gain)
adu += offset
adu[adu > max_adu] = max_adu # pixel saturation

# pixel_type = np.uint8 if bitdepth <= 8 else np.uint16
# adu = adu.astype(pixel_type) # Convert to discrete numbers

return adu

# %% ../nbs/05_transforms.ipynb 11
def ScaleIntensityRange(x, mi, ma, eps=1e-20, dtype=np_float32):
if dtype is not None:
x = x.astype(dtype, copy=False)
mi = dtype(mi) if isscalar(mi) else mi.astype(dtype, copy=False)
ma = dtype(ma) if isscalar(ma) else ma.astype(dtype, copy=False)
eps = dtype(eps)
x = (x - mi) / (ma - mi + eps)
return x

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

mi = percentile(x, pmin, axis=axis, keepdims=True)
ma = percentile(x, pmax, axis=axis, keepdims=True)
return ScaleIntensityRange(x, mi, ma, eps=eps, dtype=dtype)


# %% ../nbs/05_transforms.ipynb 16
def _process_sz(size, ndim=3):
if isinstance(size,int):
size=(size,)*ndim
Expand All @@ -20,7 +122,7 @@ def _get_sz(x):
if not isinstance(x, Tensor): return fastuple(x.size)
return fastuple(getattr(x, 'img_size', getattr(x, 'sz', (x.shape[1:])))) # maybe it should swap x and y axes

# %% ../nbs/05_transforms.ipynb 16
# %% ../nbs/05_transforms.ipynb 19
class RandCrop2D(RandTransform):
"Randomly crop an image to `size`"
split_idx,order = None,1
Expand Down Expand Up @@ -50,7 +152,7 @@ def before_call(self,
def encodes(self, x):
return SpatialCrop(roi_center=self.ctr, roi_size=self.size, lazy=self.lazy)(x)

# %% ../nbs/05_transforms.ipynb 17
# %% ../nbs/05_transforms.ipynb 20
class RandCropND(RandTransform):
"""
Randomly crops an ND image to a specified size.
Expand Down Expand Up @@ -103,7 +205,7 @@ def encodes(self, x):
return SpatialCrop(roi_start=self.tl, roi_end=self.br, lazy=self.lazy)(x)


# %% ../nbs/05_transforms.ipynb 19
# %% ../nbs/05_transforms.ipynb 22
class RandCropND_T(RandTransform):
"""
Randomly crops an ND image to a specified size.
Expand Down Expand Up @@ -166,7 +268,7 @@ def encodes(self, x):
return cropped_img


# %% ../nbs/05_transforms.ipynb 21
# %% ../nbs/05_transforms.ipynb 24
class RandFlip(RandTransform):
"""
Randomly flips an ND image over a specified axis.
Expand Down Expand Up @@ -199,7 +301,7 @@ def encodes(self, x):
else:
return x

# %% ../nbs/05_transforms.ipynb 23
# %% ../nbs/05_transforms.ipynb 26
class RandRot90(RandTransform):
"""
Randomly rotate an ND image by 90 degrees in the plane specified by axes.
Expand Down
30 changes: 9 additions & 21 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -56,7 +56,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -86,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -100,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -111,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -129,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -143,18 +143,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/01_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/02_io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/03_losses.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/04_nets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -954,10 +954,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
213 changes: 148 additions & 65 deletions nbs/05_transforms.ipynb

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions nbs/06_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/08_datasets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/09_visualize.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2408051,10 +2408051,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/901_demo_RI2FL_2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
4 changes: 0 additions & 4 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@
"display_name": "python3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c0dff39

Please sign in to comment.