Skip to content

Commit

Permalink
Added a basic transform as discussed in #21
Browse files Browse the repository at this point in the history
- More transforms can be experimented with later, and the format will essentially remain the same.
  • Loading branch information
MrinalJain17 committed Oct 20, 2020
1 parent d7996cc commit 5538bc1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
39 changes: 28 additions & 11 deletions capstone/notebooks/miccai_batch_exploration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,22 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def window_stats(patient, window, crop=True):\n",
" temp_patient = patient\n",
" window_width, window_level = window\n",
" if crop:\n",
" temp_patient.crop_data()\n",
" \n",
" vol = temp_patient.image.as_numpy()\n",
" \n",
" if window is None:\n",
" window_width = np.math.ceil(np.max(vol) - np.min(vol))\n",
" window_level = np.math.ceil(np.max(vol) - (window_width / 2))\n",
" else:\n",
" window_width, window_level = window\n",
" \n",
" clipped = apply_window(vol, window_width, window_level, shift=True)\n",
" N = np.prod(vol.shape)\n",
" \n",
Expand All @@ -379,16 +384,17 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:09<00:00, 2.78s/it]\n",
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:12<00:00, 2.88s/it]\n",
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:08<00:00, 2.76s/it]\n"
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:11<00:00, 2.86s/it]\n",
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:06<00:00, 2.67s/it]\n",
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:08<00:00, 2.73s/it]\n",
"100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [01:14<00:00, 2.96s/it]\n"
]
}
],
Expand All @@ -400,12 +406,15 @@
"results_tissue = patient_collection.apply_function(window_stats, window=WINDOWING_CONFIG[\"soft_tissue\"])\n",
"\n",
"# Bone windowing\n",
"results_bone = patient_collection.apply_function(window_stats, window=WINDOWING_CONFIG[\"bone\"])"
"results_bone = patient_collection.apply_function(window_stats, window=WINDOWING_CONFIG[\"bone\"])\n",
"\n",
"# No windowing\n",
"results_none = patient_collection.apply_function(window_stats, window=None)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -414,7 +423,8 @@
"text": [
"Brain windowing stats: mean=0.10677633102763491, std=0.2708013287518585\n",
"Soft tissue windowing stats: mean=0.13461463937176568, std=0.2668329203556111\n",
"Bone windowing stats: mean=0.08499418334609993, std=0.1520881280521281\n"
"Bone windowing stats: mean=0.08499418334609993, std=0.1520881280521281\n",
"No windowing stats: mean=0.07861779334888626, std=0.13267967531876107\n"
]
}
],
Expand All @@ -438,7 +448,14 @@
"sum_vars = temp[:, 1].sum()\n",
"sum_voxels = temp[:, 2].sum()\n",
"\n",
"print(f\"Bone windowing stats: mean={sum_means / sum_voxels}, std={np.sqrt(sum_vars / sum_voxels)}\")"
"print(f\"Bone windowing stats: mean={sum_means / sum_voxels}, std={np.sqrt(sum_vars / sum_voxels)}\")\n",
"\n",
"temp = np.array(list(results_none.values()))\n",
"sum_means = temp[:, 0].sum()\n",
"sum_vars = temp[:, 1].sum()\n",
"sum_voxels = temp[:, 2].sum()\n",
"\n",
"print(f\"No windowing stats: mean={sum_means / sum_voxels}, std={np.sqrt(sum_vars / sum_voxels)}\")"
]
},
{
Expand Down
14 changes: 3 additions & 11 deletions capstone/notebooks/sample_dataset_2d.ipynb

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions capstone/transforms/predefined.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
from capstone.transforms.transforms_2d import WindowedChannels

_stacked_window_stats = {"mean": (0.107, 0.135, 0.085), "std": (0.271, 0.267, 0.152)}
# _no_window_stats = {"mean": (0.077), "std": (0.133)}

_minimal_windowed_test_transform = A.Compose(
[
WindowedChannels(),
A.Resize(256, 256),
A.Normalize(
mean=_stacked_window_stats["mean"],
std=_stacked_window_stats["std"],
max_pixel_value=1.0,
),
ToTensorV2(),
]
)


def minimal_windowed_transforms(split="train"):
assert split in ["train", "valid", "test"], "Invalid data split passed"
if split == "train":
return A.Compose(
[
WindowedChannels(),
A.RandomCrop(256, 256),
A.RandomRotate90(),
A.HorizontalFlip(),
A.Normalize(
mean=_stacked_window_stats["mean"],
std=_stacked_window_stats["std"],
max_pixel_value=1.0,
),
ToTensorV2(),
]
)
else:
return _minimal_windowed_test_transform

0 comments on commit 5538bc1

Please sign in to comment.