Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from PhoenixDL/sptial_transforms_update
Browse files Browse the repository at this point in the history
Affine Transform Refactoring
  • Loading branch information
mibaumgartner authored Feb 24, 2020
2 parents a2025be + 42cfe91 commit a11abc7
Show file tree
Hide file tree
Showing 10 changed files with 1,340 additions and 985 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/notebook_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ jobs:
max-parallel: 4
matrix:
python-version: [3.7]

env:
TEST_ENV: TRUE
steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
Expand Down
279 changes: 279 additions & 0 deletions notebooks/transformations.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-15T09:57:50.791332Z",
"start_time": "2020-02-15T09:57:46.068701Z"
},
"scrolled": true
},
"outputs": [],
"source": [
"!pip install napari\n",
"!pip install SimpleITK"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:54:54.920459Z",
"start_time": "2020-02-16T16:54:54.669509Z"
}
},
"outputs": [],
"source": [
"%reload_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline\n",
"%gui qt\n",
"import os\n",
"if 'TEST_ENV' in os.environ:\n",
" TEST_ENV = os.environ['TEST_ENV'].lower() == \"true\"\n",
"else:\n",
" TEST_ENV = 0\n",
"print(f\"Running test environment: {bool(TEST_ENV)}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-15T11:55:06.130717Z",
"start_time": "2020-02-15T11:54:22.119553Z"
}
},
"outputs": [],
"source": [
"from io import BytesIO\n",
"from zipfile import ZipFile\n",
"from urllib.request import urlopen\n",
"\n",
"resp = urlopen(\"http://www.fmrib.ox.ac.uk/primers/intro_primer/ExBox3/ExBox3.zip\")\n",
"zipfile = ZipFile(BytesIO(resp.read()))\n",
"\n",
"img_file = zipfile.extract(\"ExBox3/T1_brain.nii.gz\")\n",
"mask_file = zipfile.extract(\"ExBox3/T1_brain_seg.nii.gz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:55:01.394975Z",
"start_time": "2020-02-16T16:55:00.893340Z"
}
},
"outputs": [],
"source": [
"import SimpleITK as sitk\n",
"import numpy as np\n",
"\n",
"# load image and mask\n",
"img_file = \"./ExBox3/T1_brain.nii.gz\"\n",
"mask_file = \"./ExBox3/T1_brain_seg.nii.gz\"\n",
"img = sitk.GetArrayFromImage(sitk.ReadImage(img_file))\n",
"img = img.astype(np.float32)\n",
"mask = mask = sitk.GetArrayFromImage(sitk.ReadImage(mask_file))\n",
"mask = mask.astype(np.float32)\n",
"\n",
"assert mask.shape == img.shape\n",
"print(f\"Image shape {img.shape}\")\n",
"print(f\"Image shape {mask.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:55:04.255613Z",
"start_time": "2020-02-16T16:55:03.213336Z"
}
},
"outputs": [],
"source": [
"if TEST_ENV:\n",
" def view_batch(batch):\n",
" pass\n",
"else:\n",
" %gui qt\n",
" import napari\n",
" def view_batch(batch):\n",
" viewer = napari.view_image(batch[\"data\"].cpu().numpy(), name=\"data\")\n",
" viewer.add_image(batch[\"mask\"].cpu().numpy(), name=\"mask\", opacity=0.2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:55:54.082493Z",
"start_time": "2020-02-16T16:55:54.019599Z"
}
},
"outputs": [],
"source": [
"import torch\n",
"from rising.transforms import *\n",
"\n",
"batch = {\n",
" \"data\": torch.from_numpy(img).float()[None, None],\n",
" \"mask\": torch.from_numpy(mask).long()[None, None],\n",
"}\n",
"\n",
"def apply_transform(trafo, batch):\n",
" transformed = trafo(**batch)\n",
" print(f\"Transformed data shape: {transformed['data'].shape}\")\n",
" print(f\"Transformed mask shape: {transformed['mask'].shape}\")\n",
" print(f\"Transformed data min: {transformed['data'].min()}\")\n",
" print(f\"Transformed data max: {transformed['data'].max()}\")\n",
" print(f\"Transformed data mean: {transformed['data'].mean()}\")\n",
" return transformed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:55:06.109008Z",
"start_time": "2020-02-16T16:55:06.069336Z"
}
},
"outputs": [],
"source": [
"print(f\"Transformed data shape: {batch['data'].shape}\")\n",
"print(f\"Transformed mask shape: {batch['mask'].shape}\")\n",
"print(f\"Transformed data min: {batch['data'].min()}\")\n",
"print(f\"Transformed data max: {batch['data'].max()}\")\n",
"print(f\"Transformed data mean: {batch['data'].mean()}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:55:57.391117Z",
"start_time": "2020-02-16T16:55:55.675294Z"
}
},
"outputs": [],
"source": [
"trafo = Scale(1.5, adjust_size=False)\n",
"transformed = apply_transform(trafo, batch)\n",
"view_batch(transformed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T17:03:58.535489Z",
"start_time": "2020-02-16T17:03:57.964843Z"
}
},
"outputs": [],
"source": [
"trafo = Rotate([0, 0, 45], degree=True, adjust_size=False)\n",
"transformed = apply_transform(trafo, batch)\n",
"view_batch(transformed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-02-16T16:00:26.032367Z",
"start_time": "2020-02-16T16:00:25.466391Z"
}
},
"outputs": [],
"source": [
"trafo = Translate([0.1, 0, 0], adjust_size=False)\n",
"transformed = apply_transform(trafo, batch)\n",
"view_batch(transformed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions rising/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from rising.transforms.spatial import *
from rising.transforms.utility import *
from rising.transforms.tensor import *
from rising.transforms.affine import *
Loading

0 comments on commit a11abc7

Please sign in to comment.