Skip to content

Commit 9f9756f

Browse files
committed
new tutorial on mni preferences
1 parent e141122 commit 9f9756f

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

docs/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ NLTools
1010
.. image:: https://codecov.io/gh/cosanlab/nltools/branch/master/graph/badge.svg
1111
:target: https://codecov.io/gh/cosanlab/nltools
1212

13-
.. image:: https://api.codacy.com/project/badge/Grade/625677967a0749299f38c2bf8ee269c3
14-
:target: https://www.codacy.com/app/ljchang/nltools?utm_source=github.com&utm_medium=referral&utm_content=ljchang/nltools&utm_campaign=Badge_Grade
13+
.. image:: https://app.codacy.com/project/badge/Grade/f118dc39e5df46d28e80d0d326721cbb
14+
:target: https://app.codacy.com/gh/cosanlab/nltools/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade
1515

1616
.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.2229813.svg
1717
:target: https://doi.org/10.5281/zenodo.2229813
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Brain resolution and MNI Template Preferences
3+
=============================================
4+
5+
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:
6+
"""
7+
8+
#########################################################################
9+
# Setting GLOBAL MNI template preferences
10+
# ---------------------
11+
#
12+
from nltools.prefs import MNI_Template, resolve_mni_path
13+
from nltools.data import Brain_Data
14+
from nltools.simulator import Simulator # just for dummy data
15+
16+
#########################################################################
17+
# 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:
18+
dummy_brain = Simulator().create_data([0, 1], 1, reps=3)
19+
dummy_brain.write("dummy_2mm_brain.nii.gz") # save it for later
20+
dummy_brain # default 2mm resolution
21+
22+
#########################################################################
23+
# You can also get the exact file locations of the currently loaded default template and masks:
24+
resolve_mni_path(MNI_Template)
25+
26+
#########################################################################
27+
# 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
28+
MNI_Template.resolution = 3 # passing the string '3mm' also works
29+
dummy_brain_3mm = Simulator().create_data([0, 1], 1, reps=3)
30+
dummy_brain_3mm # should be 3mm
31+
32+
#########################################################################
33+
# The voxel count is now ~70k and you can see the file paths of the global template:
34+
35+
resolve_mni_path(MNI_Template)
36+
37+
#########################################################################
38+
# Notice that when we load we load the previous 2mm brain, it's **automatically** resampled to the currently set default MNI template (3mm):
39+
loaded_brain = Brain_Data("dummy_2mm_brain.nii.gz")
40+
loaded_brain # now in 3mm space!
41+
42+
#########################################################################
43+
# Setting local resolution preferences
44+
# ------------------------------------
45+
#
46+
# 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:
47+
48+
# Here we save the 3mm path as a variable, but in your own data you can provide
49+
# the location of any nifti file
50+
mask_file_3mm = resolve_mni_path(MNI_Template)["mask"]
51+
52+
MNI_Template.resolution = 2 # reset the global MNI template to 2mm
53+
load_using_default = Brain_Data("dummy_2mm_brain.nii.gz")
54+
load_using_default # 2mm space
55+
56+
#########################################################################
57+
load_using_3mm_mask = Brain_Data("dummy_2mm_brain.nii.gz", mask=mask_file_3mm)
58+
load_using_3mm_mask # resampled to 3mm space because a mask was provided
59+
60+
#########################################################################
61+
# Notice that the global setting is still 2mm, but by providing a `mask` we were able to override it
62+
63+
resolve_mni_path(MNI_Template)

0 commit comments

Comments
 (0)