-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfir_utils.py
More file actions
65 lines (49 loc) · 1.89 KB
/
Copy pathfir_utils.py
File metadata and controls
65 lines (49 loc) · 1.89 KB
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
def maximum_idx(array):
"""finds the index of the center-of-mass"""
import numpy as np
import sys
if not isinstance(array,np.ndarray):
array = np.array(array)
return np.unravel_index( array.argmax(axis=None), array.shape)
def functional_binarize(array,bool_func):
"""binarizes the array by settting values that bool_func
returns true to 1. Bool func must accept array as its first
input"""
import numpy as np
binarized_array = np.zeros(array.shape)
binarized_array[bool_func(array)] = 1
return binarized_array
def is_binary(array):
import numpy as np
return (np.unique(array) == np.array([0,1])).all()
def binarize_peak(array):
""" finds the peak value and binarizes it"""
import numpy as np
binarized_peak = np.zeros(array.shape)
binarized_peak[maximum_idx(array)] = 1
return binarized_peak
#return functional_binarize(np.array(array),lambda a: a == a[maximum_idx(a)])
def dilate_mask(mask, iterations=1):
"""dilates the mask iterations number of times"""
from scipy.ndimage.morphology import binary_dilation
import sys
if not is_binary(mask):
sys.exit("MASK TO BE DILATED WAS NOT A BINARY MASK")
mask[binary_dilation(mask,iterations=iterations)] = 1
return mask
def convert_affine(unwarped_brain, mean_func, out_fsl_file):
"""Converts fsl-style Affine registration into ANTS compatible itk format
Parameters
----------
unwarped_brain : structural reference image
mean_func : image that was coregistered
out_fsl_file : fsl-style coregistration matrix
Returns
-------
file : returns the filename corresponding to the converted registration
"""
import os
cmd = "c3d_affine_tool -ref %s -src %s %s -fsl2ras \
-oitk fsl2antsAffine.txt" % (unwarped_brain, mean_func, out_fsl_file)
os.system(cmd)
return os.path.abspath('fsl2antsAffine.txt')