.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_DataOperations/plot_mni_prefs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_DataOperations_plot_mni_prefs.py: Brain resolution and MNI Template Preferences ============================================= By default nltools uses a 2mm MNI template which means all `Brain_Data` operations will automatically be resampled to that space if they aren't already at that resolution. If you know you want to work in another space you can set that for all operations using the prefs module: .. GENERATED FROM PYTHON SOURCE LINES 9-12 Setting GLOBAL MNI template preferences --------------------- .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: default from nltools.prefs import MNI_Template, resolve_mni_path from nltools.data import Brain_Data from nltools.simulator import Simulator # just for dummy data .. GENERATED FROM PYTHON SOURCE LINES 17-18 Here we create some dummy data. Notice that it defaults to 2mm resolution. You can verify this by seeing that the voxel count is approximately 240k: .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: default dummy_brain = Simulator().create_data([0, 1], 1, reps=3) dummy_brain.write("dummy_2mm_brain.nii.gz") # save it for later dummy_brain # default 2mm resolution .. rst-class:: sphx-glr-script-out .. code-block:: none nltools.data.brain_data.Brain_Data(data=(6, 238955), Y=(6, 1), X=(0, 0), mask=MNI152_T1_2mm_brain_mask.nii.gz) .. GENERATED FROM PYTHON SOURCE LINES 23-24 You can also get the exact file locations of the currently loaded default template and masks: .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: default resolve_mni_path(MNI_Template) .. rst-class:: sphx-glr-script-out .. code-block:: none {'resolution': '2mm', 'mask_type': 'with_ventricles', 'mask': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm_brain_mask.nii.gz', 'plot': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm.nii.gz', 'brain': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm_brain.nii.gz'} .. GENERATED FROM PYTHON SOURCE LINES 27-28 To update this simply change the resolution attribute of the MNI_Template. NOTE: that this will change **all** subsequent Brain_Data operations to utilize this new space. Therefore we **highly recommend** doing this at the top of any analysis notebook or script you use to prevent unexpected results .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: default MNI_Template.resolution = 3 # passing the string '3mm' also works dummy_brain_3mm = Simulator().create_data([0, 1], 1, reps=3) dummy_brain_3mm # should be 3mm .. rst-class:: sphx-glr-script-out .. code-block:: none nltools.data.brain_data.Brain_Data(data=(6, 71020), Y=(6, 1), X=(0, 0), mask=MNI152_T1_3mm_brain_mask.nii.gz) .. GENERATED FROM PYTHON SOURCE LINES 33-34 The voxel count is now ~70k and you can see the file paths of the global template: .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default resolve_mni_path(MNI_Template) .. rst-class:: sphx-glr-script-out .. code-block:: none {'resolution': '3mm', 'mask_type': 'with_ventricles', 'mask': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_3mm_brain_mask.nii.gz', 'plot': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_3mm.nii.gz', 'brain': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_3mm_brain.nii.gz'} .. GENERATED FROM PYTHON SOURCE LINES 38-39 Notice that when we load we load the previous 2mm brain, it's **automatically** resampled to the currently set default MNI template (3mm): .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default loaded_brain = Brain_Data("dummy_2mm_brain.nii.gz") loaded_brain # now in 3mm space! .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/share/miniconda3/envs/test/lib/python3.8/site-packages/nilearn/maskers/nifti_masker.py:108: UserWarning: imgs are being resampled to the mask_img resolution. This process is memory intensive. You might want to provide a target_affine that is equal to the affine of the imgs or resample the mask beforehand to save memory and computation time. warnings.warn( nltools.data.brain_data.Brain_Data(data=(6, 71020), Y=(0, 0), X=(0, 0), mask=MNI152_T1_3mm_brain_mask.nii.gz) .. GENERATED FROM PYTHON SOURCE LINES 43-47 Setting local resolution preferences ------------------------------------ If you want to override the global setting on a case-by-case basis, simply use the `mask` argument in `Brain_Data`. This will resample data to the resolution of the `mask` ignoring whatever `MNI_Template` is set to: .. GENERATED FROM PYTHON SOURCE LINES 47-56 .. code-block:: default # Here we save the 3mm path as a variable, but in your own data you can provide # the location of any nifti file mask_file_3mm = resolve_mni_path(MNI_Template)["mask"] MNI_Template.resolution = 2 # reset the global MNI template to 2mm load_using_default = Brain_Data("dummy_2mm_brain.nii.gz") load_using_default # 2mm space .. rst-class:: sphx-glr-script-out .. code-block:: none nltools.data.brain_data.Brain_Data(data=(6, 238955), Y=(0, 0), X=(0, 0), mask=MNI152_T1_2mm_brain_mask.nii.gz) .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: default load_using_3mm_mask = Brain_Data("dummy_2mm_brain.nii.gz", mask=mask_file_3mm) load_using_3mm_mask # resampled to 3mm space because a mask was provided .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/share/miniconda3/envs/test/lib/python3.8/site-packages/nilearn/maskers/nifti_masker.py:108: UserWarning: imgs are being resampled to the mask_img resolution. This process is memory intensive. You might want to provide a target_affine that is equal to the affine of the imgs or resample the mask beforehand to save memory and computation time. warnings.warn( nltools.data.brain_data.Brain_Data(data=(6, 71020), Y=(0, 0), X=(0, 0), mask=MNI152_T1_3mm_brain_mask.nii.gz) .. GENERATED FROM PYTHON SOURCE LINES 61-62 Notice that the global setting is still 2mm, but by providing a `mask` we were able to override it .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: default resolve_mni_path(MNI_Template) .. rst-class:: sphx-glr-script-out .. code-block:: none {'resolution': '2mm', 'mask_type': 'with_ventricles', 'mask': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm_brain_mask.nii.gz', 'plot': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm.nii.gz', 'brain': '/home/runner/work/nltools/nltools/nltools/resources/MNI152_T1_2mm_brain.nii.gz'} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.992 seconds) .. _sphx_glr_download_auto_examples_01_DataOperations_plot_mni_prefs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mni_prefs.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mni_prefs.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_