|
157 | 157 | " return type(o)(self.pad_or_crop(o))" |
158 | 158 | ] |
159 | 159 | }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "id": "534509q2nn", |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": "#| export\ndef _foreground_masking(tensor):\n \"\"\"Mask for non-zero voxels (nnU-Net-style foreground normalization).\"\"\"\n return tensor > 0" |
| 167 | + }, |
160 | 168 | { |
161 | 169 | "cell_type": "code", |
162 | 170 | "execution_count": null, |
163 | 171 | "id": "ca95a690", |
164 | 172 | "metadata": {}, |
165 | 173 | "outputs": [], |
166 | | - "source": [ |
167 | | - "# | export\n", |
168 | | - "class ZNormalization(DisplayedTransform):\n", |
169 | | - " \"\"\"Apply TorchIO `ZNormalization`.\"\"\"\n", |
170 | | - "\n", |
171 | | - " order = 0\n", |
172 | | - "\n", |
173 | | - " def __init__(self, masking_method=None, channel_wise=True):\n", |
174 | | - " self.z_normalization = tio.ZNormalization(masking_method=masking_method)\n", |
175 | | - " self.channel_wise = channel_wise\n", |
176 | | - "\n", |
177 | | - " @property\n", |
178 | | - " def tio_transform(self):\n", |
179 | | - " \"\"\"Return the underlying TorchIO transform.\"\"\"\n", |
180 | | - " return self.z_normalization\n", |
181 | | - "\n", |
182 | | - " def encodes(self, o: MedImage):\n", |
183 | | - " try:\n", |
184 | | - " if self.channel_wise:\n", |
185 | | - " o = torch.stack([self.z_normalization(c[None])[0] for c in o])\n", |
186 | | - " else: \n", |
187 | | - " o = self.z_normalization(o)\n", |
188 | | - " except RuntimeError as e:\n", |
189 | | - " if \"Standard deviation is 0\" in str(e):\n", |
190 | | - " # Calculate mean for debugging information\n", |
191 | | - " mean = float(o.mean())\n", |
192 | | - " \n", |
193 | | - " error_msg = (\n", |
194 | | - " f\"Standard deviation is 0 for image (mean={mean:.3f}).\\n\"\n", |
195 | | - " f\"This indicates uniform pixel values.\\n\\n\"\n", |
196 | | - " f\"Possible causes:\\n\"\n", |
197 | | - " f\"• Corrupted or blank image\\n\"\n", |
198 | | - " f\"• Oversaturated regions\\n\" \n", |
199 | | - " f\"• Background-only regions\\n\"\n", |
200 | | - " f\"• All-zero mask being processed as image\\n\\n\"\n", |
201 | | - " f\"Suggested solutions:\\n\"\n", |
202 | | - " f\"• Check image quality and acquisition\\n\"\n", |
203 | | - " f\"• Verify image vs mask data loading\"\n", |
204 | | - " )\n", |
205 | | - " raise RuntimeError(error_msg) from e\n", |
206 | | - "\n", |
207 | | - " return MedImage.create(o)\n", |
208 | | - "\n", |
209 | | - " def encodes(self, o: MedMask):\n", |
210 | | - " return o" |
211 | | - ] |
| 174 | + "source": "# | export\nclass ZNormalization(DisplayedTransform):\n \"\"\"Apply TorchIO `ZNormalization`.\"\"\"\n\n order = 0\n\n def __init__(self, masking_method=None, channel_wise=True):\n if masking_method == 'foreground':\n masking_method = _foreground_masking\n self.z_normalization = tio.ZNormalization(masking_method=masking_method)\n self.channel_wise = channel_wise\n\n @property\n def tio_transform(self):\n \"\"\"Return the underlying TorchIO transform.\"\"\"\n return self.z_normalization\n\n def encodes(self, o: MedImage):\n try:\n if self.channel_wise:\n o = torch.stack([self.z_normalization(c[None])[0] for c in o])\n else: \n o = self.z_normalization(o)\n except RuntimeError as e:\n if \"Standard deviation is 0\" in str(e):\n # Calculate mean for debugging information\n mean = float(o.mean())\n \n error_msg = (\n f\"Standard deviation is 0 for image (mean={mean:.3f}).\\n\"\n f\"This indicates uniform pixel values.\\n\\n\"\n f\"Possible causes:\\n\"\n f\"• Corrupted or blank image\\n\"\n f\"• Oversaturated regions\\n\" \n f\"• Background-only regions\\n\"\n f\"• All-zero mask being processed as image\\n\\n\"\n f\"Suggested solutions:\\n\"\n f\"• Check image quality and acquisition\\n\"\n f\"• Verify image vs mask data loading\"\n )\n raise RuntimeError(error_msg) from e\n\n return MedImage.create(o)\n\n def encodes(self, o: MedMask):\n return o" |
212 | 175 | }, |
213 | 176 | { |
214 | 177 | "cell_type": "code", |
|
0 commit comments