Skip to content

Commit

Permalink
Merge pull request SyneRBI#101 from ashgillman/working_dir
Browse files Browse the repository at this point in the history
Add `cd_to_working_dir` function and use it.

Fixes SyneRBI#84
  • Loading branch information
KrisThielemans committed Jun 17, 2021
2 parents 24d94f1 + 5926ddf commit 033a03e
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 215 deletions.
22 changes: 20 additions & 2 deletions lib/sirf_exercises/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os


def exercises_data_path(*data_type):
'''
Returns the path to data used by SIRF-exercises.
Expand All @@ -32,6 +33,23 @@ def exercises_data_path(*data_type):
if data_path is None or not os.path.exists(data_path):
raise RuntimeError(
"Exercises data weren't found. Please run download_data.sh in the "
"scripts directory")
"scripts directory (use its -h option to get help)")

return os.path.join(data_path, *data_type)

return os.path.join(data_path, *data_type)

def cd_to_working_dir(*subfolders):
'''
Creates and changes the current directory to a working directory for the
current exercise, based on the argument(s). If multiple
strings are given, they will be treated as subdirectories.
Implementation detail: this is defined as
{exercises_data_path()}/working_folder/{subfolders[0]}/{subfolders[1]}/...
subfolders: the path will include this.
Multiple arguments can be given for nested subdirectories.
'''
working_dir = exercises_data_path('working_folder', *subfolders)
os.makedirs(working_dir, exist_ok=True)
os.chdir(working_dir)
5 changes: 4 additions & 1 deletion notebooks/Introductory/acquisition_model_mr_pet_ct.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
"outputs": [],
"source": [
"# Make sure figures appears inline and animations works\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('Introductory', 'acquisition_model_mr_pet_ct')"
]
},
{
Expand Down
25 changes: 16 additions & 9 deletions notebooks/Introductory/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
"metadata": {},
"outputs": [],
"source": [
"# We have placed a file in this directory, notebook_setup.py, which will allow us to import the sirf_exercises library\n",
"import notebook_setup\n",
"\n",
"# The sirf_exercises defines some handy tools for these notebooks\n",
"from sirf_exercises import cd_to_working_dir\n",
"\n",
"# Initial imports etc\n",
"import numpy\n",
"from numpy.linalg import norm\n",
Expand All @@ -77,6 +83,16 @@
"from sirf.Utilities import examples_data_path"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Move to a working directory for this notebook\n",
"cd_to_working_dir('Introductory', 'introduction')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -716,15 +732,6 @@
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython"
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python"
}
},
"nbformat": 4,
Expand Down
19 changes: 10 additions & 9 deletions notebooks/MR/a_fully_sampled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@
"outputs": [],
"source": [
"#%% make sure figures appears inline and animations works\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"\n",
"# Setup the working directory for the notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('MR', 'a_fully_sampled')"
]
},
{
Expand All @@ -166,7 +171,6 @@
"outputs": [],
"source": [
"__version__ = '0.1.0'\n",
"import notebook_setup # set exercises path\n",
"\n",
"# import engine module\n",
"import sirf.Gadgetron as pMR\n",
Expand All @@ -175,11 +179,7 @@
"# import further modules\n",
"import os\n",
"import matplotlib.pyplot as plt\n",
"from sirf_exercises import exercises_data_path\n",
"\n",
"#%% GO TO MR FOLDER\n",
"acrphantom_data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"os.chdir(acrphantom_data_path)"
"from sirf_exercises import exercises_data_path"
]
},
{
Expand All @@ -198,7 +198,8 @@
"outputs": [],
"source": [
"#%% LOAD RAW MR DATA\n",
"filename = 'ptb_resolutionphantom_fully_ismrmrd.h5' \n",
"data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"filename = os.path.join(data_path, 'ptb_resolutionphantom_fully_ismrmrd.h5')\n",
"acq_data = pMR.AcquisitionData(filename)\n",
"\n",
"print(type(acq_data))"
Expand Down Expand Up @@ -461,5 +462,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
19 changes: 10 additions & 9 deletions notebooks/MR/b_kspace_filter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
"outputs": [],
"source": [
"#%% make sure figures appears inline and animations works\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"\n",
"# Setup the working directory for the notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('MR', 'b_kspace_filter')"
]
},
{
Expand All @@ -60,8 +65,6 @@
"source": [
"__version__ = '0.1.0'\n",
"\n",
"import notebook_setup\n",
"\n",
"# import engine module\n",
"import sirf.Gadgetron as pMR\n",
"from sirf.Utilities import examples_data_path\n",
Expand All @@ -70,11 +73,7 @@
"# import further modules\n",
"import os, numpy\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"#%% GO TO MR FOLDER\n",
"acrphantom_data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"os.chdir(acrphantom_data_path)"
"import matplotlib.pyplot as plt"
]
},
{
Expand Down Expand Up @@ -166,7 +165,9 @@
"\n",
"\n",
"\n",
"acq_data = pMR.AcquisitionData('ptb_resolutionphantom_fully_ismrmrd.h5')\n",
"data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"filename = os.path.join(data_path, 'ptb_resolutionphantom_fully_ismrmrd.h5')\n",
"acq_data = pMR.AcquisitionData(filename)\n",
"\n",
"kdim = acq_data.dimensions()\n",
"print('Size of k-space %dx%dx%d' % kdim)"
Expand Down
18 changes: 9 additions & 9 deletions notebooks/MR/c_coil_combination.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
"## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"## See the License for the specific language governing permissions and\n",
"## limitations under the License.\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"\n",
"# Setup the working directory for the notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('MR', 'c_coil_combination')"
]
},
{
Expand All @@ -59,20 +64,14 @@
"source": [
"__version__ = '0.1.0'\n",
"\n",
"import notebook_setup\n",
"\n",
"# import engine module\n",
"import sirf.Gadgetron as pMR\n",
"from sirf.Utilities import examples_data_path\n",
"from sirf_exercises import exercises_data_path\n",
"\n",
"# import further modules\n",
"import os, numpy\n",
"import matplotlib.pyplot as plt\n",
"\n",
"#%% GO TO MR FOLDER\n",
"acrphantom_data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"os.chdir(acrphantom_data_path)"
"import matplotlib.pyplot as plt"
]
},
{
Expand Down Expand Up @@ -112,7 +111,8 @@
"outputs": [],
"source": [
"#%% LOAD AND PREPROCESS RAW MR DATA\n",
"filename = 'ptb_resolutionphantom_fully_ismrmrd.h5'\n",
"data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"filename = os.path.join(data_path, 'ptb_resolutionphantom_fully_ismrmrd.h5')\n",
"acq_data = pMR.AcquisitionData(filename)\n",
"preprocessed_data = pMR.preprocess_acquisition_data(acq_data)\n",
"preprocessed_data.sort()\n",
Expand Down
19 changes: 10 additions & 9 deletions notebooks/MR/d_undersampled_reconstructions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
"outputs": [],
"source": [
"#%% make sure figures appears inline and animations works\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"\n",
"# Setup the working directory for the notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('MR', 'd_undersampled_reconstructions')"
]
},
{
Expand All @@ -59,7 +64,6 @@
"outputs": [],
"source": [
"__version__ = '0.1.0'\n",
"import notebook_setup\n",
"\n",
"# import engine module\n",
"import sirf.Gadgetron as pMR\n",
Expand All @@ -72,11 +76,7 @@
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation\n",
"\n",
"from tqdm.auto import trange\n",
"\n",
"#%% GO TO MR FOLDER\n",
"acrphantom_data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"os.chdir(acrphantom_data_path)"
"from tqdm.auto import trange"
]
},
{
Expand Down Expand Up @@ -206,7 +206,8 @@
"\n",
"\n",
"\n",
"filename_full_file = 'ptb_resolutionphantom_fully_ismrmrd.h5'\n",
"data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')\n",
"filename_full_file = os.path.join(data_path, 'ptb_resolutionphantom_fully_ismrmrd.h5')\n",
"\n",
"full_acq_data = pMR.AcquisitionData(filename_full_file)\n",
"prep_full_data = pMR.preprocess_acquisition_data(full_acq_data)\n",
Expand Down Expand Up @@ -242,7 +243,7 @@
"outputs": [],
"source": [
"# LOADING AND PREPROCESSING DATA FOR THIS SET\n",
"filename_grappa_file = 'ptb_resolutionphantom_GRAPPA4_ismrmrd.h5'\n",
"filename_grappa_file = os.path.join(data_path, 'ptb_resolutionphantom_GRAPPA4_ismrmrd.h5')\n",
"acq_data = pMR.AcquisitionData(filename_grappa_file)\n",
"preprocessed_data = pMR.preprocess_acquisition_data(acq_data)\n",
"preprocessed_data.sort()\n"
Expand Down
14 changes: 8 additions & 6 deletions notebooks/MR/e_advanced_recon.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
"outputs": [],
"source": [
"#%% make sure figures appears inline and animations works\n",
"%matplotlib notebook"
"%matplotlib notebook\n",
"\n",
"# Setup the working directory for the notebook\n",
"import notebook_setup\n",
"from sirf_exercises import cd_to_working_dir\n",
"cd_to_working_dir('MR', 'e_advanced_recon')"
]
},
{
Expand All @@ -64,8 +69,6 @@
"source": [
"__version__ = '0.1.1'\n",
"\n",
"import notebook_setup\n",
"\n",
"# import engine module\n",
"import sirf.Gadgetron as pMR\n",
"\n",
Expand All @@ -85,8 +88,7 @@
"outputs": [],
"source": [
"from sirf_exercises import exercises_data_path\n",
"#%% GO TO MR FOLDER\n",
"os.chdir(exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA'))"
"data_path = exercises_data_path('MR', 'PTB_ACRPhantom_GRAPPA')"
]
},
{
Expand Down Expand Up @@ -121,7 +123,7 @@
"outputs": [],
"source": [
"# LOADING AND PREPROCESSING DATA FOR THIS SET\n",
"filename_grappa_file = 'ptb_resolutionphantom_GRAPPA4_ismrmrd.h5'\n",
"filename_grappa_file = os.path.join(data_path, 'ptb_resolutionphantom_GRAPPA4_ismrmrd.h5')\n",
"acq_data = pMR.AcquisitionData(filename_grappa_file)\n",
"preprocessed_data = pMR.preprocess_acquisition_data(acq_data)\n",
"preprocessed_data.sort()\n",
Expand Down

0 comments on commit 033a03e

Please sign in to comment.