-
Notifications
You must be signed in to change notification settings - Fork 25
/
xfmjoin
executable file
·143 lines (119 loc) · 4.43 KB
/
xfmjoin
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
#
# Joins together several xfm files, leaving the original displacement volumes
# where they are.
#
# Note: this operation means that resulting xfm file uses absolute paths, and
# so will become invalid if any of the displacement volumes are moved.
#
# Assuming the following structure for and XFM file:
#
# MNI Transform File
# %Thu Jan 10 17:15:06 2013>>> minctracc ....
#
# Transform_Type = Grid_Transform;
# Displacement_Volume = 0021_0295_0413_grid_0.mnc;
# Transform_Type = Linear;
# Linear_Transform =
# 0.9772509301 -0.09474506176 -0.03084412052 -19.081777
# 0.07106130268 0.9716283851 -0.18306245 -24.92575407
# 0.04604540017 0.2288221291 0.9933863879 -4.204577453;
# Transform_Type = Grid_Transform;
# Displacement_Volume = 0021_0295_0413_grid_1.mnc;
# Transform_Type = Linear;
# Linear_Transform =
# 0.9938300533 0.007599120733 -0.03030895914 12.17102408
# -0.006040286116 0.9816438948 0.1181265793 2.516647066
# 0.02683032485 -0.118368505 0.9499845023 4.303799096;
#
#
# Jon Pipitone, [email protected], 2013
#
from __future__ import print_function
from builtins import str
from builtins import map
from builtins import object
import sys
import copy
import datetime
from os import getcwd
from os.path import dirname, normpath, join
def _pop(l):
while l and l[0].strip() == "":
l.pop(0)
return l and l.pop(0).strip() or None
class Transform(object):
def __repr__(self):
return "\n".join([self.type] + self.transform)
class GridTransform(Transform):
def update_volume_path(self, relative_to='source'):
if relative_to == 'source':
relative_to_path = dirname(self.fromfile)
else:
relative_to_path = relative_to
key, grid = [x.strip().strip(';') for x in self.transform[0].split("=")]
grid = normpath(join(relative_to_path, grid))
self.transform = ["%s = %s;" % (key, grid)]
class XFM(object):
def __init__(self, xfmfile_path=None):
self.header = None
self.history = []
self.transforms = []
self.xfmfile_path = xfmfile_path
if xfmfile_path:
self.populate(xfmfile_path)
def populate(self, xfmfile_path=None):
lines = open(xfmfile_path).readlines()
while lines:
line = _pop(lines)
if line.startswith('%'):
self.history.append(line)
continue
if line.startswith("MNI"):
self.header = line
continue
if line.startswith("Transform_Type = Grid_Transform;"):
transform = GridTransform()
transform.fromfile = xfmfile_path
transform.type = line
transform.transform = [_pop(lines)]
self.transforms.append(transform)
continue
if line.startswith("Transform_Type = Linear;"):
transform = Transform()
transform.fromfile = xfmfile_path
transform.type = line
transform.transform = [
_pop(lines),
_pop(lines),
_pop(lines),
_pop(lines)]
self.transforms.append(transform)
continue
print(("Ignoring line:", line))
def __repr__(self):
return "\n".join([self.header] + self.history +
list(map(str, self.transforms)))
def merge(self, xfms, paths_relative_to='source'):
self.history.append(
"%% %s >> Merging %s" %
(datetime.datetime.now().isoformat(),
", ".join([x.xfmfile_path for x in xfms])))
for xfm in xfms:
self.header = xfm.header
self.history.extend(xfm.history)
for transform in map(copy.deepcopy, xfm.transforms):
if isinstance(transform, GridTransform):
transform.update_volume_path(relative_to=paths_relative_to)
self.transforms.append(transform)
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("Joins together several xfm files, leaving the original displacement volumes")
print("in place.")
print("")
print("Usage: %s xfm [xfm ...] merged.xfm" % sys.argv[0])
print("")
sys.exit(-1)
merged_xfm = XFM()
merged_xfm.merge(list(map(XFM, sys.argv[1:-1])))
open(sys.argv[-1], 'w').write(str(merged_xfm))