Skip to content

Commit bb236dc

Browse files
committed
Simplify the notebook (WIP again)
1 parent ab7c050 commit bb236dc

File tree

1 file changed

+81
-171
lines changed

1 file changed

+81
-171
lines changed

playing_with_pytorch.ipynb

Lines changed: 81 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"id": "doj9EzIVIMFv"
2020
},
2121
"source": [
22-
"# PyTorch"
22+
"# Playing with SimpleITK and nnU-Net"
2323
]
2424
},
2525
{
@@ -31,28 +31,38 @@
3131
"## Installation"
3232
]
3333
},
34-
{
35-
"cell_type": "code",
36-
"metadata": {
37-
"id": "oWi4McPs_NuY"
38-
},
39-
"source": [
40-
"!pip install -qq SimpleITK nnunetv2"
41-
],
42-
"execution_count": null,
43-
"outputs": []
44-
},
4534
{
4635
"cell_type": "code",
4736
"source": [
4837
"%cd /content\n",
49-
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git"
38+
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git\n",
39+
"\n",
40+
"%pip install -qq SimpleITK nnunetv2"
5041
],
5142
"metadata": {
52-
"id": "vL5WJI3NVxIe"
53-
},
54-
"execution_count": null,
55-
"outputs": []
43+
"id": "vL5WJI3NVxIe",
44+
"outputId": "cedbef25-33a3-45f1-e036-913a318ba177",
45+
"colab": {
46+
"base_uri": "https://localhost:8080/"
47+
}
48+
},
49+
"execution_count": 2,
50+
"outputs": [
51+
{
52+
"output_type": "stream",
53+
"name": "stdout",
54+
"text": [
55+
"/content\n",
56+
"Cloning into 'playing-with-simpleitk'...\n",
57+
"remote: Enumerating objects: 295, done.\u001b[K\n",
58+
"remote: Counting objects: 100% (42/42), done.\u001b[K\n",
59+
"remote: Compressing objects: 100% (35/35), done.\u001b[K\n",
60+
"remote: Total 295 (delta 22), reused 18 (delta 7), pack-reused 253\u001b[K\n",
61+
"Receiving objects: 100% (295/295), 30.21 MiB | 23.78 MiB/s, done.\n",
62+
"Resolving deltas: 100% (42/42), done.\n"
63+
]
64+
}
65+
]
5666
},
5767
{
5868
"cell_type": "markdown",
@@ -63,94 +73,27 @@
6373
"## Data nomenclature"
6474
]
6575
},
66-
{
67-
"cell_type": "markdown",
68-
"metadata": {
69-
"id": "5oFoBRkhIVVX"
70-
},
71-
"source": [
72-
"Patients"
73-
]
74-
},
7576
{
7677
"cell_type": "code",
7778
"metadata": {
7879
"id": "8AJwRH-8FmaD"
7980
},
8081
"source": [
81-
"def get_patient_indices():\n",
82-
" patient_indices = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n",
83-
"\n",
84-
" return patient_indices"
85-
],
86-
"execution_count": null,
87-
"outputs": []
88-
},
89-
{
90-
"cell_type": "markdown",
91-
"metadata": {
92-
"id": "P65kljpNIRK4"
93-
},
94-
"source": [
95-
"Folders"
96-
]
97-
},
98-
{
99-
"cell_type": "code",
100-
"metadata": {
101-
"id": "uiLgnz7zFY13"
102-
},
103-
"source": [
104-
"def get_data_folder():\n",
105-
" data_folder = 'data/'\n",
106-
"\n",
107-
" return data_folder\n",
108-
"\n",
109-
"def get_image_folder():\n",
110-
" image_folder = get_data_folder() + 'imagesTr/'\n",
111-
"\n",
112-
" return image_folder\n",
113-
"\n",
114-
"def get_ground_truth_folder():\n",
115-
" ground_truth_folder = get_data_folder() + 'labelsTr/'\n",
116-
"\n",
117-
" return ground_truth_folder"
118-
],
119-
"execution_count": null,
120-
"outputs": []
121-
},
122-
{
123-
"cell_type": "markdown",
124-
"metadata": {
125-
"id": "NfQXankUIS88"
126-
},
127-
"source": [
128-
"Files"
129-
]
130-
},
131-
{
132-
"cell_type": "code",
133-
"metadata": {
134-
"id": "jbGlq7B8biXg"
135-
},
136-
"source": [
137-
"def get_file_extension():\n",
138-
" file_extension = '.nii.gz'\n",
82+
"ROOT_FOLDER = '/content/'\n",
83+
"DATA_FOLDER = 'data/'\n",
84+
"IMAGE_FOLDER = f'{DATA_FOLDER}imagesTr/'\n",
85+
"GROUND_TRUTH_FOLDER = f'{DATA_FOLDER}labelsTr/'\n",
13986
"\n",
140-
" return file_extension\n",
87+
"PATIENT_INDICES = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n",
88+
"IMAGE_TYPE = '.nii.gz'\n",
14189
"\n",
14290
"def get_image_file_name(patient_no, modality_no=0):\n",
143-
" image_modality_convention = '_{:04.0f}'.format(modality_no)\n",
144-
" image_file_name = get_image_folder() + 'patientID' + str(patient_no) + image_modality_convention + get_file_extension()\n",
145-
"\n",
146-
" return image_file_name\n",
91+
" return f'{IMAGE_FOLDER}patientID{patient_no}_{modality_no:04}{IMAGE_TYPE}'\n",
14792
"\n",
14893
"def get_ground_truth_file_name(patient_no):\n",
149-
" ground_truth_file_name = get_ground_truth_folder() +'patientID' + str(patient_no) + get_file_extension()\n",
150-
"\n",
151-
" return ground_truth_file_name"
94+
" return f'{GROUND_TRUTH_FOLDER}patientID{patient_no}{IMAGE_TYPE}'"
15295
],
153-
"execution_count": null,
96+
"execution_count": 3,
15497
"outputs": []
15598
},
15699
{
@@ -170,46 +113,51 @@
170113
"!unzip -qq CHAOS_Train_Sets.zip"
171114
],
172115
"metadata": {
173-
"id": "bSD1Q_6Quslt"
174-
},
175-
"execution_count": null,
176-
"outputs": []
177-
},
178-
{
179-
"cell_type": "code",
180-
"source": [
181-
"%mv /content/Train_Sets /content/playing-with-simpleitk/data/"
182-
],
183-
"metadata": {
184-
"id": "TQ8-7kLU7bs5"
185-
},
186-
"execution_count": null,
187-
"outputs": []
116+
"id": "bSD1Q_6Quslt",
117+
"outputId": "3341ee8a-eaf7-4dda-b5b8-d12c670f2a04",
118+
"colab": {
119+
"base_uri": "https://localhost:8080/"
120+
}
121+
},
122+
"execution_count": 7,
123+
"outputs": [
124+
{
125+
"output_type": "stream",
126+
"name": "stdout",
127+
"text": [
128+
"/content\n",
129+
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
130+
" Dload Upload Total Spent Left Speed\n",
131+
"100 150 100 150 0 0 182 0 --:--:-- --:--:-- --:--:-- 182\n",
132+
"[CHAOS_Train_Sets.zip]\n",
133+
" End-of-central-directory signature not found. Either this file is not\n",
134+
" a zipfile, or it constitutes one disk of a multi-part archive. In the\n",
135+
" latter case the central directory and zipfile comment will be found on\n",
136+
" the last disk(s) of this archive.\n",
137+
"unzip: cannot find zipfile directory in one of CHAOS_Train_Sets.zip or\n",
138+
" CHAOS_Train_Sets.zip.zip, and cannot find CHAOS_Train_Sets.zip.ZIP, period.\n"
139+
]
140+
}
141+
]
188142
},
189143
{
190144
"cell_type": "code",
191145
"source": [
146+
"DATASET_FNAME = \"Dataset000_MY_DATASET\"\n",
147+
"NNUNET_FOLDER_NAME = f\"{ROOT_FOLDER}nnUNet_base/{DATASET_FNAME}/\"\n",
148+
"\n",
192149
"%cd /content/playing-with-simpleitk\n",
193-
"!python convert_to_nii.py"
150+
"%mv /content/Train_Sets data/\n",
151+
"!python convert_to_nii.py\n",
152+
"%mkdir -p {NNUNET_FOLDER_NAME}\n",
153+
"%mv data/output/* {NNUNET_FOLDER_NAME}"
194154
],
195155
"metadata": {
196156
"id": "qugHxbsFvCtM"
197157
},
198158
"execution_count": null,
199159
"outputs": []
200160
},
201-
{
202-
"cell_type": "code",
203-
"metadata": {
204-
"id": "HbPo9F8kB31W"
205-
},
206-
"source": [
207-
"%mkdir -p /content/nnUNet_base/Dataset000_MY_DATASET\n",
208-
"%mv /content/playing-with-simpleitk/data/output/* /content/nnUNet_base/Dataset000_MY_DATASET/"
209-
],
210-
"execution_count": null,
211-
"outputs": []
212-
},
213161
{
214162
"cell_type": "markdown",
215163
"metadata": {
@@ -219,29 +167,6 @@
219167
"## Tests"
220168
]
221169
},
222-
{
223-
"cell_type": "code",
224-
"metadata": {
225-
"id": "7sGD_c2Iq5Da"
226-
},
227-
"source": [
228-
"root_folder = '/content/'"
229-
],
230-
"execution_count": null,
231-
"outputs": []
232-
},
233-
{
234-
"cell_type": "code",
235-
"metadata": {
236-
"id": "QgbS9WFvrJQb"
237-
},
238-
"source": [
239-
"patient_indices = get_patient_indices()\n",
240-
"patient_no = patient_indices[0]"
241-
],
242-
"execution_count": null,
243-
"outputs": []
244-
},
245170
{
246171
"cell_type": "code",
247172
"metadata": {
@@ -251,35 +176,18 @@
251176
"import numpy as np\n",
252177
"import SimpleITK as sitk\n",
253178
"\n",
254-
"image_name = root_folder + get_image_file_name(patient_no)\n",
255-
"\n",
256-
"image = sitk.ReadImage(image_name)\n",
257-
"print(image.GetSize())\n",
258-
"\n",
259-
"v = sitk.GetArrayViewFromImage(image)\n",
260-
"\n",
261-
"np.unique(v)"
262-
],
263-
"execution_count": null,
264-
"outputs": []
265-
},
266-
{
267-
"cell_type": "code",
268-
"metadata": {
269-
"id": "5_X6uel5LMmS"
270-
},
271-
"source": [
272-
"import numpy as np\n",
273-
"import SimpleITK as sitk\n",
274-
"\n",
275-
"image_name = root_folder + get_ground_truth_file_name(patient_no)\n",
179+
"patient_no = PATIENT_INDICES[0]\n",
276180
"\n",
277-
"image = sitk.ReadImage(image_name)\n",
278-
"print(image.GetSize())\n",
181+
"for image_name in [\n",
182+
" get_image_file_name(patient_no),\n",
183+
" get_ground_truth_file_name(patient_no),\n",
184+
" ]:\n",
279185
"\n",
280-
"v = sitk.GetArrayViewFromImage(image)\n",
186+
" image = sitk.ReadImage(ROOT_FOLDER + image_name)\n",
187+
" print(image.GetSize())\n",
281188
"\n",
282-
"np.unique(v)"
189+
" v = sitk.GetArrayViewFromImage(image)\n",
190+
" print(np.unique(v))"
283191
],
284192
"execution_count": null,
285193
"outputs": []
@@ -292,7 +200,9 @@
292200
"source": [
293201
"## Down-sample images\n",
294202
"\n",
295-
"Reference: https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py"
203+
"References:\n",
204+
"- https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py\n",
205+
"- https://simpleitk.org/SPIE2019_COURSE/02_images_and_resampling.html"
296206
]
297207
},
298208
{

0 commit comments

Comments
 (0)