-
Notifications
You must be signed in to change notification settings - Fork 0
/
datman_scanid.py
executable file
·121 lines (97 loc) · 3.61 KB
/
datman_scanid.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
114
115
116
117
118
119
120
121
"""
Represents scan identifiers that conform to the TIGRLab naming scheme
Taken from TIGRLab/datman
"""
import os.path
import re
SCANID_RE = '(?P<study>[^_]+)_' \
'(?P<site>[^_]+)_' \
'(?P<subject>[^_]+)_' \
'(?P<timepoint>[^_]+)_' \
'(?P<session>[^_]+)'
SCANID_PHA_RE = '(?P<study>[^_]+)_' \
'(?P<site>[^_]+)_' \
'(?P<subject>PHA_[^_]+)' \
'(?P<timepoint>)(?P<session>)' # empty
FILENAME_RE = SCANID_RE + '_' + \
r'(?P<tag>[^_]+)_' + \
r'(?P<series>\d+)_' + \
r'(?P<description>[^\.]*)' + \
r'(?P<ext>\..*)?'
FILENAME_PHA_RE = SCANID_PHA_RE + '_' + \
r'(?P<tag>[^_]+)_' + \
r'(?P<series>\d+)_' + \
r'(?P<description>[^\.]*)' + \
r'(?P<ext>\..*)?'
SCANID_PATTERN = re.compile('^'+SCANID_RE+'$')
SCANID_PHA_PATTERN = re.compile('^'+SCANID_PHA_RE+'$')
FILENAME_PATTERN = re.compile('^'+FILENAME_RE+'$')
FILENAME_PHA_PATTERN = re.compile('^'+FILENAME_PHA_RE+'$')
class ParseException(Exception):
pass
class Identifier:
def __init__(self, study, site, subject, timepoint, session):
self.study = study
self.site = site
self.subject = subject
self.timepoint = timepoint
self.session = session
def get_full_subjectid(self):
return "_".join([self.study, self.site, self.subject])
def get_full_subjectid_with_timepoint(self):
ident = self.get_full_subjectid()
if self.timepoint:
ident += "_"+self.timepoint
return ident
def __str__(self):
if self.timepoint:
return "_".join([self.study,
self.site,
self.subject,
self.timepoint,
self.session])
else: # it's a phantom, so no timepoints
return self.get_full_subjectid()
def parse(identifier):
if type(identifier) is not str: raise ParseException()
match = SCANID_PATTERN.match(identifier)
if not match: match = SCANID_PHA_PATTERN.match(identifier)
if not match: raise ParseException()
ident = Identifier(study = match.group("study"),
site = match.group("site"),
subject = match.group("subject"),
timepoint= match.group("timepoint"),
session = match.group("session"))
return ident
def parse_filename(path):
fname = os.path.basename(path)
match = FILENAME_PHA_PATTERN.match(fname) # check PHA first
if not match: match = FILENAME_PATTERN.match(fname)
if not match: raise ParseException()
ident = Identifier(study = match.group("study"),
site = match.group("site"),
subject = match.group("subject"),
timepoint= match.group("timepoint"),
session = match.group("session"))
tag = match.group("tag")
series = match.group("series")
description = match.group("description")
return ident, tag, series, description
def make_filename(ident, tag, series, description, ext = None):
filename = "_".join([str(ident), tag, series, description])
if ext:
filename += ext
return filename
def is_scanid(identifier):
try:
parse(identifier)
return True
except ParseException:
return False
def is_phantom(identifier):
try:
x = parse(identifier)
return x.subject[0:3] == 'PHA'
except ParseException:
return False
# vim: ts=4 sw=4: