Skip to content

Commit

Permalink
Simplify the notebook (WIP again)
Browse files Browse the repository at this point in the history
  • Loading branch information
woctezuma committed Nov 10, 2023
1 parent ab7c050 commit bb236dc
Showing 1 changed file with 81 additions and 171 deletions.
252 changes: 81 additions & 171 deletions playing_with_pytorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"id": "doj9EzIVIMFv"
},
"source": [
"# PyTorch"
"# Playing with SimpleITK and nnU-Net"
]
},
{
Expand All @@ -31,28 +31,38 @@
"## Installation"
]
},
{
"cell_type": "code",
"metadata": {
"id": "oWi4McPs_NuY"
},
"source": [
"!pip install -qq SimpleITK nnunetv2"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content\n",
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git"
"!git clone https://github.com/woctezuma/playing-with-simpleitk.git\n",
"\n",
"%pip install -qq SimpleITK nnunetv2"
],
"metadata": {
"id": "vL5WJI3NVxIe"
},
"execution_count": null,
"outputs": []
"id": "vL5WJI3NVxIe",
"outputId": "cedbef25-33a3-45f1-e036-913a318ba177",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n",
"Cloning into 'playing-with-simpleitk'...\n",
"remote: Enumerating objects: 295, done.\u001b[K\n",
"remote: Counting objects: 100% (42/42), done.\u001b[K\n",
"remote: Compressing objects: 100% (35/35), done.\u001b[K\n",
"remote: Total 295 (delta 22), reused 18 (delta 7), pack-reused 253\u001b[K\n",
"Receiving objects: 100% (295/295), 30.21 MiB | 23.78 MiB/s, done.\n",
"Resolving deltas: 100% (42/42), done.\n"
]
}
]
},
{
"cell_type": "markdown",
Expand All @@ -63,94 +73,27 @@
"## Data nomenclature"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5oFoBRkhIVVX"
},
"source": [
"Patients"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8AJwRH-8FmaD"
},
"source": [
"def get_patient_indices():\n",
" patient_indices = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n",
"\n",
" return patient_indices"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "P65kljpNIRK4"
},
"source": [
"Folders"
]
},
{
"cell_type": "code",
"metadata": {
"id": "uiLgnz7zFY13"
},
"source": [
"def get_data_folder():\n",
" data_folder = 'data/'\n",
"\n",
" return data_folder\n",
"\n",
"def get_image_folder():\n",
" image_folder = get_data_folder() + 'imagesTr/'\n",
"\n",
" return image_folder\n",
"\n",
"def get_ground_truth_folder():\n",
" ground_truth_folder = get_data_folder() + 'labelsTr/'\n",
"\n",
" return ground_truth_folder"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "NfQXankUIS88"
},
"source": [
"Files"
]
},
{
"cell_type": "code",
"metadata": {
"id": "jbGlq7B8biXg"
},
"source": [
"def get_file_extension():\n",
" file_extension = '.nii.gz'\n",
"ROOT_FOLDER = '/content/'\n",
"DATA_FOLDER = 'data/'\n",
"IMAGE_FOLDER = f'{DATA_FOLDER}imagesTr/'\n",
"GROUND_TRUTH_FOLDER = f'{DATA_FOLDER}labelsTr/'\n",
"\n",
" return file_extension\n",
"PATIENT_INDICES = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n",
"IMAGE_TYPE = '.nii.gz'\n",
"\n",
"def get_image_file_name(patient_no, modality_no=0):\n",
" image_modality_convention = '_{:04.0f}'.format(modality_no)\n",
" image_file_name = get_image_folder() + 'patientID' + str(patient_no) + image_modality_convention + get_file_extension()\n",
"\n",
" return image_file_name\n",
" return f'{IMAGE_FOLDER}patientID{patient_no}_{modality_no:04}{IMAGE_TYPE}'\n",
"\n",
"def get_ground_truth_file_name(patient_no):\n",
" ground_truth_file_name = get_ground_truth_folder() +'patientID' + str(patient_no) + get_file_extension()\n",
"\n",
" return ground_truth_file_name"
" return f'{GROUND_TRUTH_FOLDER}patientID{patient_no}{IMAGE_TYPE}'"
],
"execution_count": null,
"execution_count": 3,
"outputs": []
},
{
Expand All @@ -170,46 +113,51 @@
"!unzip -qq CHAOS_Train_Sets.zip"
],
"metadata": {
"id": "bSD1Q_6Quslt"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%mv /content/Train_Sets /content/playing-with-simpleitk/data/"
],
"metadata": {
"id": "TQ8-7kLU7bs5"
},
"execution_count": null,
"outputs": []
"id": "bSD1Q_6Quslt",
"outputId": "3341ee8a-eaf7-4dda-b5b8-d12c670f2a04",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n",
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
"100 150 100 150 0 0 182 0 --:--:-- --:--:-- --:--:-- 182\n",
"[CHAOS_Train_Sets.zip]\n",
" End-of-central-directory signature not found. Either this file is not\n",
" a zipfile, or it constitutes one disk of a multi-part archive. In the\n",
" latter case the central directory and zipfile comment will be found on\n",
" the last disk(s) of this archive.\n",
"unzip: cannot find zipfile directory in one of CHAOS_Train_Sets.zip or\n",
" CHAOS_Train_Sets.zip.zip, and cannot find CHAOS_Train_Sets.zip.ZIP, period.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"DATASET_FNAME = \"Dataset000_MY_DATASET\"\n",
"NNUNET_FOLDER_NAME = f\"{ROOT_FOLDER}nnUNet_base/{DATASET_FNAME}/\"\n",
"\n",
"%cd /content/playing-with-simpleitk\n",
"!python convert_to_nii.py"
"%mv /content/Train_Sets data/\n",
"!python convert_to_nii.py\n",
"%mkdir -p {NNUNET_FOLDER_NAME}\n",
"%mv data/output/* {NNUNET_FOLDER_NAME}"
],
"metadata": {
"id": "qugHxbsFvCtM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "HbPo9F8kB31W"
},
"source": [
"%mkdir -p /content/nnUNet_base/Dataset000_MY_DATASET\n",
"%mv /content/playing-with-simpleitk/data/output/* /content/nnUNet_base/Dataset000_MY_DATASET/"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -219,29 +167,6 @@
"## Tests"
]
},
{
"cell_type": "code",
"metadata": {
"id": "7sGD_c2Iq5Da"
},
"source": [
"root_folder = '/content/'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QgbS9WFvrJQb"
},
"source": [
"patient_indices = get_patient_indices()\n",
"patient_no = patient_indices[0]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
Expand All @@ -251,35 +176,18 @@
"import numpy as np\n",
"import SimpleITK as sitk\n",
"\n",
"image_name = root_folder + get_image_file_name(patient_no)\n",
"\n",
"image = sitk.ReadImage(image_name)\n",
"print(image.GetSize())\n",
"\n",
"v = sitk.GetArrayViewFromImage(image)\n",
"\n",
"np.unique(v)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "5_X6uel5LMmS"
},
"source": [
"import numpy as np\n",
"import SimpleITK as sitk\n",
"\n",
"image_name = root_folder + get_ground_truth_file_name(patient_no)\n",
"patient_no = PATIENT_INDICES[0]\n",
"\n",
"image = sitk.ReadImage(image_name)\n",
"print(image.GetSize())\n",
"for image_name in [\n",
" get_image_file_name(patient_no),\n",
" get_ground_truth_file_name(patient_no),\n",
" ]:\n",
"\n",
"v = sitk.GetArrayViewFromImage(image)\n",
" image = sitk.ReadImage(ROOT_FOLDER + image_name)\n",
" print(image.GetSize())\n",
"\n",
"np.unique(v)"
" v = sitk.GetArrayViewFromImage(image)\n",
" print(np.unique(v))"
],
"execution_count": null,
"outputs": []
Expand All @@ -292,7 +200,9 @@
"source": [
"## Down-sample images\n",
"\n",
"Reference: https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py"
"References:\n",
"- https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py\n",
"- https://simpleitk.org/SPIE2019_COURSE/02_images_and_resampling.html"
]
},
{
Expand Down

0 comments on commit bb236dc

Please sign in to comment.