-
Notifications
You must be signed in to change notification settings - Fork 0
/
prof_apply_mask.py
113 lines (89 loc) · 3.45 KB
/
prof_apply_mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Benchmark for nilearn.apply_mask()
This script makes use of the adhd dataset. If it is not already on you
disk, it will be downloaded the first time this script is run. It can
take a long time.
Usage:
- first run once as 'python apply_mask.py init'
This computes two files: one containing a mask, another one the data,
uncompressed.
- then run 'python apply_mask.py gz' or 'python apply_mask.py nogz'
To run the corresponding benchmark. The first one uses an compressed
nifti file as input, the second an uncompressed nifti file. Except for
compression, the input data are identical. No output is generated: only
execution time is used.
The script is already instrumented: the apply_mask() function is wrapped
into a profile() function that is defined by profiling tools, like the
memory_profiler on the line_profiler.
Basic timing can be performed with: 'time python apply_mask.py gz'
More sophisticated timing is obtained with the line profiler:
'time kernprof.py -lv apply_mask.py gz'
To remove any cache effect, the system buffer can be deleted on linux
with this command (as root):
sync ; echo 3 > /proc/sys/vm/drop_caches
This can have a very strong influence, in particular for uncompressed
input file.
"""
import sys
import os.path as osp
import numpy as np
import nibabel
import nilearn.masking
import nilearn.datasets
import utils
def get_filenames(kind="gz"):
if kind == "gz":
adhd = nilearn.datasets.fetch_adhd(n_subjects=1)
filename = adhd["func"][0]
elif kind == "nogz":
filename = "0010042_rest_tshift_RPI_voreg_mni.nii"
else:
raise ValueError("A kind must be provided")
# mask file is generated by write_mask.py
return filename, "adhd_0_mask.nii"
def create_mask():
import gzip
print ("Computing and writing mask file...")
filename, mask_filename = get_filenames(kind="gz")
print("Loading data...")
fmri = nibabel.load(filename)
data = fmri.get_data()
print("Computing mask on all images...")
mask = nilearn.masking.compute_epi_mask(data, opening=False).astype(np.int8)
nibabel.save(nibabel.nifti1.Nifti1Image(mask, fmri.get_affine()),
mask_filename)
print("Mask saved.")
del mask_filename
print("Computing uncompressed dataset...")
filename_nogz, _ = get_filenames(kind="nogz")
if not osp.exists(filename_nogz):
infile = gzip.open(filename) # no support for "with" in 2.6
data = infile.read()
infile.close()
with open(filename_nogz, 'wb') as outfile:
outfile.write(data)
def benchmark(kind="gz"):
data_filename, mask_filename = get_filenames(kind=kind)
smooth = 2
if utils.cache_tools_available:
print("Invalidating cache of input file...")
utils.dontneed(data_filename)
utils.dontneed(mask_filename)
print("Masking data...")
masked = utils.timeit(profile(nilearn.masking.apply_mask)
)(data_filename, mask_filename,
smooth=smooth)
del masked
print("Masking data...")
masked = utils.timeit(profile(nilearn.masking.apply_mask)
)(data_filename, mask_filename,
smooth=smooth)
del masked
if __name__ == "__main__":
# Use "gz" or "nogz" as first argument. Default is "gz"
kind = "nogz"
if len(sys.argv) == 2:
kind = sys.argv[1]
if kind == "init":
create_mask()
else:
benchmark(kind=kind)