-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_params.py
executable file
·216 lines (181 loc) · 8.18 KB
/
plot_params.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
# Standard libs
import re
from warnings import warn, simplefilter
# Third party libs
import argparse
import numpy as np
from enthought.enable.api import ComponentEditor
import enthought.traits.api as traits
import enthought.traits.ui.api as ui
import enthought.chaco.api as chaco
import enthought.chaco.tools.api as tools
PARAM_DTYPE = [('trans', [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]),
('rot', [('pitch', 'f4'), ('roll', 'f4'), ('yaw', 'f4')])]
SPM_COLORS = ['blue', 'green', 'red']
# cmap = chaco.Spectral(chaco.DataRange1D(low=0, high=5))
# COLOR_PALETTE = cmap.color_bands
class MovementParamPlot(traits.HasTraits):
# "Internal" attributes
data = traits.DictStrAny(value_trait=chaco.ArrayPlotData)
zoom = traits.Instance(tools.ZoomTool)
pan = traits.Instance(tools.PanTool)
# Would be nice to actually make this a list of files - but maybe not here,
# as we may want to load the file-names from a canned report datasource
# (i.e. the original files may not be present)
file_list = traits.List(trait=traits.Str)
# Attributes in the view
rot_plot = traits.Instance(chaco.Plot)
trans_plot = traits.Instance(chaco.Plot)
# It'd be nice to split out the notion of which subject is selected, so that
# this plot can simply take a given set of data
max_params = traits.Int(0)
params_num = traits.Range(0, 'max_params')
file_name = traits.Str('file_name not set')
traits_view = ui.View(
ui.Item('params_num', label="Scan Number",
editor=ui.RangeEditor(mode='spinner',
high_name='max_params') ),
ui.Item('file_name', label='File', style='readonly'),
ui.Item('trans_plot', label='Translation',
editor=ComponentEditor(height=300)),
ui.Item('rot_plot', label='Rotation',
editor=ComponentEditor(height=300)),
# width=550,
# height=750,
resizable=True,
title="Movement parameters"
)
def __init__(self, fnames=None, params=None):
'''params: a list of numpy arrays'''
super(MovementParamPlot, self).__init__()
if fnames is None:
# XXX Hardcode hack to get envisage working
fnames = ['data/movement_params.txt']
params = np.recfromtxt(fnames[0], dtype=PARAM_DTYPE)
self.file_list = fnames
self.file_name = fnames[0]
self.params_num = 0
self.max_params = len(params) - 1
self.params = params
# XXX - the following info should go (soon) into
# nipype.interfaces.fsl.McFLIRT
self.trans_plot = self.create_line_plot('trans', 'mm')
self.rot_plot = self.create_line_plot('rot', 'radians (clockwise)',
self.trans_plot.index_range)
# Note - it doesn't matter which plot you use to init the tools
self.zoom = tools.ZoomTool(self.trans_plot, tool_mode='range',
axis='index')
self.pan = tools.PanTool(self.rot_plot, constrain=True,
constrain_direction='x')
self.trans_plot.tools.extend((self.zoom, self.pan))
self.rot_plot.tools.extend((self.pan, self.zoom))
def create_line_plot(self, name, units='Arbitrary Units',
index_range=None):
'''Make a line plot overlaying x, y and z values'''
data = chaco.ArrayPlotData()
plot = chaco.Plot(data)
if index_range is not None:
plot.index_range = index_range
xyz = self.params[0][name]
axes = xyz.dtype.names
for i, axis in enumerate(axes):
data.set_data(axis, xyz[axis])
plot.plot(axis, color = SPM_COLORS[i])
plot.index_axis.title = 'Slice Number'
plot.value_axis.title = units
# Our left axis labels are a bit large
plot.padding = [70, 50, 20, 50]
# might be clearer to write:
# plot.padding_top = 20
# plot.padding_left = 70
plot.legend.tools.append(
tools.LegendTool(plot.legend, drag_button="right") )
plot.legend.labels = list(axes)
plot.legend.visible = True
self.data[name] = data
return plot
# TODO: need to re-set range on plots
@traits.on_trait_change('params_num')
def set_plot(self, n=None):
if n is None:
n = self.params_num
else:
self.params_num = n
self.file_name = self.file_list[n]
params = self.params[n]
for name in params.dtype.names:
xyz = params[name]
for axis in xyz.dtype.names:
self.data[name].set_data(axis, xyz[axis])
# XXX Not sure if this is the best way to split the view out but doing
# this to try and get the envisage framework working. Modelling the
# code after the EnvisagePlugins Lorenz example.
class MovementParamsView(traits.HasTraits):
movement_params = traits.Instance(MovementParamPlot)
params_num = traits.DelegatesTo('movement_params')
max_params = traits.DelegatesTo('movement_params')
file_name = traits.DelegatesTo('movement_params')
rot_plot = traits.DelegatesTo('movement_params')
trans_plot = traits.DelegatesTo('movement_params')
traits_view = ui.View(
ui.Item('params_num', label="Scan Number",
editor=ui.RangeEditor(mode='spinner',
high_name='max_params') ),
ui.Item('file_name', label='File', style='readonly'),
ui.Item('rot_plot', label='Rotation',
editor=ComponentEditor(height=300)),
ui.Item('trans_plot', label='Translation',
editor=ComponentEditor(height=300)),
# width=550,
# height=750,
resizable=True,
title="Movement parameters"
)
class TimediffPlot(traits.HasTraits):
traits_view = ui.View(
ui.Item('params_num', label="Scan Number",
editor=ui.RangeEditor(mode='spinner',
high_name='max_params') ),
ui.Item('rot_plot', label='Rotation',
editor=ComponentEditor()),
ui.Item('trans_plot', label='Translation',
editor=ComponentEditor()),
# width=550, height=550,
resizable=True,
title="Movement parameters"
)
class PhysioPlot(traits.HasTraits):
traits_view = ui.View(
ui.Item('params_num', label="Scan Number",
editor=ui.RangeEditor(mode='spinner',
high_name='max_params') ),
ui.Item('rot_plot', label='Rotation',
editor=ComponentEditor()),
ui.Item('trans_plot', label='Translation',
editor=ComponentEditor()),
# width=550, height=550,
resizable=True,
title="Movement parameters"
)
def main(fnames, verbose=False, plot=True):
'''main(fnames): simply reads each file in fnames'''
if verbose:
simplefilter('always', UserWarning)
else:
simplefilter('ignore', UserWarning)
# Allow passing in a single string
if isinstance(fnames, str):
fnames = [fnames]
params = []
for fname in fnames:
params.append(np.recfromtxt(fname, dtype=PARAM_DTYPE))
plot_obj = MovementParamPlot(fnames, params)
plot_obj.configure_traits()
return plot_obj
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('fnames', nargs='+')
args = parser.parse_args()
main(args.fnames, verbose=args.verbose, plot=False)