-
Notifications
You must be signed in to change notification settings - Fork 0
/
fw_sdk_functions.py
70 lines (56 loc) · 3.23 KB
/
fw_sdk_functions.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
import os
import sys
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
from pprint import pprint as pp
#### Define functions
def get_diffusion_acquisitions(acquisitions, diffusion_acquisition_label):
"""
TODO: Use parameters during autodetect to find the best scans to use.
"""
if diffusion_acquisition_label == 'autodetect':
print('Attempting to autodetect diffusion acquisitions')
# Find diffusion acquisitions by finding acquisitions with a diffusion type and a bvec file
diffusion_acquisitions = [ x for x in acquisitions
if [y for y in x['files']
if y.classification.has_key('Measurement') and 'Diffusion' in y.classification['Measurement']]
and [z for z in x['files']
if z['type'] == 'bvec' or z['type'] == 'bval'] ]
# Determine acquisitions to use based on label fuzzy matching
if len(diffusion_acquisitions) == 0:
print('No Diffusion acquisitions found! Either classification was not set or required files (bvec, nifti, bval) do not exist in any aquisitions.')
return diffusion_acquisitions
if len(diffusion_acquisitions) > 2:
print(' Found %s diffusion acquisitions - trying to determine if we have a usable set based on label.'
% (len(diffusion_acquisitions)))
labels = [x['label'] for x in diffusion_acquisitions]
d_labels = labels
for l in labels:
this_label = d_labels.pop(d_labels.index(l))
for dl in d_labels:
ratio = fuzz.ratio(this_label, dl)
print(' Analyzing %s & %s... ratio = %s' % (this_label, dl, str(ratio)))
if ratio > 90:
label_1, label_2 = this_label, dl
print(' Matched %s and %s!' % (label_1, label_2))
if label_1 and label_2:
diffusion_acquisitions = [ x for x in acquisitions if x['label'] == label_1 or x['label'] == label_2 ]
else:
diffusion_acquisitions = [x for x in acquisitions if x['label'].find(diffusion_acquisition_label) != -1]
print('Returning %s diffusion acquisitions' % (str(len(diffusion_acquisitions))))
return diffusion_acquisitions
def get_anatomical_acquisitions(acquisitions, anatomical_acquisition_label):
"""
TODO: Use parameters during autodetect to find the best scan to use.
"""
if anatomical_acquisition_label == 'autodetect':
print('Attempting to autodetect anatomical acquisitions')
# Find diffusion acquisitions by finding acquisitions with a diffusion type and a bvec file
anatomical_acquisitions = [ x for x in acquisitions
if [y for y in x['files']
if y.classification.has_key('Measurement') and 'T1' in y.classification['Measurement']]
and [z for z in x['files']
if z['type'] == 'nifti' ] ]
else:
anatomical_acquisitions = [x for x in acquisitions if x['label'].find(anatomical_acquisition_label) != -1]
return anatomical_acquisitions