Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- CellAware.cpython-312.pyc +0 -0
- CellAware.py +88 -0
- LoadImage.cpython-312.pyc +0 -0
- NormalizeImage.cpython-312.pyc +0 -0
- NormalizeImage.py +77 -0
- __init__.cpython-312.pyc +0 -0
- __init__.py +3 -0
- modalities.pkl +3 -0
CellAware.cpython-312.pyc
ADDED
|
Binary file (3.84 kB). View file
|
|
|
CellAware.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import copy
|
| 3 |
+
|
| 4 |
+
from monai.transforms import RandScaleIntensity, Compose
|
| 5 |
+
from monai.transforms.compose import MapTransform
|
| 6 |
+
from skimage.segmentation import find_boundaries
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
__all__ = ["BoundaryExclusion", "IntensityDiversification"]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class BoundaryExclusion(MapTransform):
|
| 13 |
+
"""Map the cell boundary pixel labels to the background class (0)."""
|
| 14 |
+
|
| 15 |
+
def __init__(self, keys=["label"], allow_missing_keys=False):
|
| 16 |
+
super(BoundaryExclusion, self).__init__(keys, allow_missing_keys)
|
| 17 |
+
|
| 18 |
+
def __call__(self, data):
|
| 19 |
+
# Find and Exclude Boundary
|
| 20 |
+
label_original = data["label"]
|
| 21 |
+
label = copy.deepcopy(label_original)
|
| 22 |
+
boundary = find_boundaries(label, connectivity=1, mode="thick")
|
| 23 |
+
label[boundary] = 0
|
| 24 |
+
|
| 25 |
+
# Do not exclude if the cell is too small (< 14x14).
|
| 26 |
+
new_label = copy.deepcopy(label_original)
|
| 27 |
+
new_label[label == 0] = 0
|
| 28 |
+
|
| 29 |
+
cell_idx, cell_counts = np.unique(label_original, return_counts=True)
|
| 30 |
+
|
| 31 |
+
for k in range(len(cell_counts)):
|
| 32 |
+
if cell_counts[k] < 196:
|
| 33 |
+
new_label[label_original == cell_idx[k]] = cell_idx[k]
|
| 34 |
+
|
| 35 |
+
# Do not exclude if the pixels are at the image boundaries.
|
| 36 |
+
_, W, H = label_original.shape
|
| 37 |
+
bd = np.zeros_like(label_original, dtype=label.dtype)
|
| 38 |
+
bd[:, 2 : W - 2, 2 : H - 2] = 1
|
| 39 |
+
new_label += label_original * bd
|
| 40 |
+
|
| 41 |
+
# Assign the transformed label
|
| 42 |
+
data["label"] = new_label
|
| 43 |
+
|
| 44 |
+
return data
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class IntensityDiversification(MapTransform):
|
| 48 |
+
"""Randomly rescale the intensity of cell pixels."""
|
| 49 |
+
|
| 50 |
+
def __init__(
|
| 51 |
+
self,
|
| 52 |
+
keys=["img"],
|
| 53 |
+
change_cell_ratio=0.4,
|
| 54 |
+
scale_factors=[0, 0.7],
|
| 55 |
+
allow_missing_keys=False,
|
| 56 |
+
):
|
| 57 |
+
super(IntensityDiversification, self).__init__(keys, allow_missing_keys)
|
| 58 |
+
|
| 59 |
+
self.change_cell_ratio = change_cell_ratio
|
| 60 |
+
self.randscale_intensity = Compose(
|
| 61 |
+
[RandScaleIntensity(prob=1.0, factors=scale_factors)]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
def __call__(self, data):
|
| 65 |
+
# Select cells to be transformed
|
| 66 |
+
cell_count = int(data["label"].max())
|
| 67 |
+
change_cell_count = int(cell_count * self.change_cell_ratio)
|
| 68 |
+
change_cell = np.random.choice(cell_count, change_cell_count, replace=False)
|
| 69 |
+
|
| 70 |
+
mask = copy.deepcopy(data["label"])
|
| 71 |
+
|
| 72 |
+
for i in range(cell_count):
|
| 73 |
+
cell_id = i + 1
|
| 74 |
+
|
| 75 |
+
if cell_id not in change_cell:
|
| 76 |
+
mask[mask == cell_id] = 0
|
| 77 |
+
|
| 78 |
+
mask[mask > 0] = 1
|
| 79 |
+
|
| 80 |
+
# Conduct intensity transformation for the selected cells
|
| 81 |
+
img_original = copy.deepcopy((1 - mask) * data["img"])
|
| 82 |
+
img_transformed = copy.deepcopy(mask * data["img"])
|
| 83 |
+
img_transformed = self.randscale_intensity(img_transformed)
|
| 84 |
+
|
| 85 |
+
# Assign the transformed image
|
| 86 |
+
data["img"] = img_original + img_transformed
|
| 87 |
+
|
| 88 |
+
return data
|
LoadImage.cpython-312.pyc
ADDED
|
Binary file (7 kB). View file
|
|
|
NormalizeImage.cpython-312.pyc
ADDED
|
Binary file (3.82 kB). View file
|
|
|
NormalizeImage.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from skimage import exposure
|
| 3 |
+
from monai.config import KeysCollection
|
| 4 |
+
|
| 5 |
+
from monai.transforms.transform import Transform
|
| 6 |
+
from monai.transforms.compose import MapTransform
|
| 7 |
+
|
| 8 |
+
from typing import Dict, Hashable, Mapping
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
__all__ = [
|
| 12 |
+
"CustomNormalizeImage",
|
| 13 |
+
"CustomNormalizeImageD",
|
| 14 |
+
"CustomNormalizeImageDict",
|
| 15 |
+
"CustomNormalizeImaged",
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class CustomNormalizeImage(Transform):
|
| 20 |
+
"""Normalize the image."""
|
| 21 |
+
|
| 22 |
+
def __init__(self, percentiles=[0, 99.5], channel_wise=False):
|
| 23 |
+
self.lower, self.upper = percentiles
|
| 24 |
+
self.channel_wise = channel_wise
|
| 25 |
+
|
| 26 |
+
def _normalize(self, img) -> np.ndarray:
|
| 27 |
+
non_zero_vals = img[np.nonzero(img)]
|
| 28 |
+
percentiles = np.percentile(non_zero_vals, [self.lower, self.upper])
|
| 29 |
+
img_norm = exposure.rescale_intensity(
|
| 30 |
+
img, in_range=(percentiles[0], percentiles[1]), out_range="uint8"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return img_norm.astype(np.uint8)
|
| 34 |
+
|
| 35 |
+
def __call__(self, img: np.ndarray) -> np.ndarray:
|
| 36 |
+
if self.channel_wise:
|
| 37 |
+
pre_img_data = np.zeros(img.shape, dtype=np.uint8)
|
| 38 |
+
for i in range(img.shape[-1]):
|
| 39 |
+
img_channel_i = img[:, :, i]
|
| 40 |
+
|
| 41 |
+
if len(img_channel_i[np.nonzero(img_channel_i)]) > 0:
|
| 42 |
+
pre_img_data[:, :, i] = self._normalize(img_channel_i)
|
| 43 |
+
|
| 44 |
+
img = pre_img_data
|
| 45 |
+
|
| 46 |
+
else:
|
| 47 |
+
img = self._normalize(img)
|
| 48 |
+
|
| 49 |
+
return img
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class CustomNormalizeImaged(MapTransform):
|
| 53 |
+
"""Dictionary-based wrapper of NormalizeImage"""
|
| 54 |
+
|
| 55 |
+
def __init__(
|
| 56 |
+
self,
|
| 57 |
+
keys: KeysCollection,
|
| 58 |
+
percentiles=[1, 99],
|
| 59 |
+
channel_wise: bool = False,
|
| 60 |
+
allow_missing_keys: bool = False,
|
| 61 |
+
):
|
| 62 |
+
super(CustomNormalizeImageD, self).__init__(keys, allow_missing_keys)
|
| 63 |
+
self.normalizer = CustomNormalizeImage(percentiles, channel_wise)
|
| 64 |
+
|
| 65 |
+
def __call__(
|
| 66 |
+
self, data: Mapping[Hashable, np.ndarray]
|
| 67 |
+
) -> Dict[Hashable, np.ndarray]:
|
| 68 |
+
|
| 69 |
+
d = dict(data)
|
| 70 |
+
|
| 71 |
+
for key in self.keys:
|
| 72 |
+
d[key] = self.normalizer(d[key])
|
| 73 |
+
|
| 74 |
+
return d
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
CustomNormalizeImageD = CustomNormalizeImageDict = CustomNormalizeImaged
|
__init__.cpython-312.pyc
ADDED
|
Binary file (252 Bytes). View file
|
|
|
__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .LoadImage import *
|
| 2 |
+
from .NormalizeImage import *
|
| 3 |
+
from .CellAware import *
|
modalities.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0b50fb364519e5eafb29d7b2861c23a3420652f0ac55f28f0737307da39177c4
|
| 3 |
+
size 3762
|