forked from chrisadamsonmcri/CCSegThickness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCSegLong.py
executable file
·63 lines (52 loc) · 2.36 KB
/
CCSegLong.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
#!/usr/bin/python
import os
import shutil
# loadLongSubjectFile(fileName)
# fileName is a list of subjects
# each line contains a tab-delimited list of image names
# the first file in the list is the first timepoint, the others are other timepoints
# returns a dictionary with the first timepoint file as the key and for that key the value is a list of the remaining files in that line, i.e. the other timepoints for that subject
def loadLongSubjectFile(fileName):
if not os.path.isfile(fileName):
return None
else:
fp = open(fileName)
mainAndTimePoints = dict()
timePointsToReference = dict()
for line in fp:
splitLine = line.strip().split()
referenceImage = splitLine[0]
otherTimePoints = splitLine[1:]
if referenceImage in mainAndTimePoints:
print "Warning: reference image appears twice in file - " + referenceImage
else:
if len(otherTimePoints) > 0:
mainAndTimePoints[referenceImage] = otherTimePoints[:]
for otherImage in otherTimePoints:
if otherImage in timePointsToReference or otherImage in mainAndTimePoints:
print "Warning: image appears twice - " + otherImage
else:
timePointsToReference[otherImage] = referenceImage
else:
mainAndTimePoints[referenceImage] = None
timePointsToReference[referenceImage] = None
fp.close()
return (mainAndTimePoints, timePointsToReference)
def longOptionText():
return "\t\t--long-subject-timepoint-file=<file>: the file with the subject ids and timepoints, see LONGITUDINAL FILE FORMAT for details"
def longOptionFileFormat():
return "\tLONGITUDINAL FILE FORMAT\n\t\tFile is a newline-delimited text file with each row being: <tp1> <tp2> <tp3> ... each tpX is the name of a NIFTI file in the input directory"
if __name__ == "__main__":
mainAndTimePoints, timePointsToReference = loadLongSubjectFile('long_file.txt')
#print mainAndTimePoints
#print timePointsToReference
inDir = 'T1NeckCroppedRegToFirstTimepoint'
for curFile in timePointsToReference.iterkeys():
if timePointsToReference[curFile] != None:
extensions = ['.mat', '.nii.gz']
for curExtension in extensions:
curInFile = os.path.join(inDir, curFile + curExtension)
curOutFile = os.path.join(inDir, curFile + "_to_" + timePointsToReference[curFile] + curExtension)
if os.path.isfile(curInFile):
print curInFile + " -> " + curOutFile
shutil.move(curInFile, curOutFile)