Skip to content

Commit d6350fd

Browse files
authored
Merge pull request #158 from christianbrodbeck/add_data
[MRG] ENH: Set initial time in Brain.add_data()
2 parents 75daf55 + d12d48d commit d6350fd

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

examples/plot_meg_inverse_solution.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,30 @@ def time_label(t):
5050
time points (in seconds)
5151
"""
5252
time = np.linspace(stc['tmin'], stc['tmin'] + data.shape[1] * stc['tstep'],
53-
data.shape[1])
53+
data.shape[1], endpoint=False)
5454

5555
"""
5656
colormap to use
5757
"""
5858
colormap = 'hot'
5959

60+
"""
61+
add data and set the initial time displayed to 100 ms
62+
"""
6063
brain.add_data(data, colormap=colormap, vertices=vertices,
6164
smoothing_steps=10, time=time, time_label=time_label,
62-
hemi=hemi)
65+
hemi=hemi, initial_time=0.1)
6366

6467
"""
65-
scale colormap and set time (index) to display
68+
scale colormap
6669
"""
67-
brain.set_data_time_index(2)
6870
brain.scale_data_colormap(fmin=13, fmid=18, fmax=22, transparent=True)
6971

72+
"""
73+
To change the time displayed to 80 ms uncomment this line
74+
"""
75+
# brain.set_time(0.08)
76+
7077
"""
7178
uncomment these lines to use the interactive TimeViewer GUI
7279
"""

surfer/tests/test_viz.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,26 @@ def test_meg_inverse():
191191
stc = io.read_stc(stc_fname)
192192
data = stc['data']
193193
vertices = stc['vertices']
194-
time = 1e3 * np.linspace(stc['tmin'],
195-
stc['tmin'] + data.shape[1] * stc['tstep'],
196-
data.shape[1])
194+
time = np.linspace(stc['tmin'], stc['tmin'] + data.shape[1] * stc['tstep'],
195+
data.shape[1], endpoint=False)
197196
colormap = 'hot'
198-
time_label = 'time=%0.2f ms'
197+
198+
def time_label(t):
199+
return 'time=%0.2f ms' % (1e3 * t)
200+
199201
brain.add_data(data, colormap=colormap, vertices=vertices,
200202
smoothing_steps=10, time=time, time_label=time_label)
201-
brain.set_data_time_index(2)
202203
brain.scale_data_colormap(fmin=13, fmid=18, fmax=22, transparent=True)
204+
assert_equal(brain.data_dict['lh']['time_idx'], 0)
205+
206+
brain.set_time(.1)
207+
assert_equal(brain.data_dict['lh']['time_idx'], 2)
203208
# viewer = TimeViewer(brain)
209+
210+
brain.add_data(data, colormap=colormap, vertices=vertices,
211+
smoothing_steps=10, time=time, time_label=time_label,
212+
initial_time=.09, remove_existing=True)
213+
assert_equal(brain.data_dict['lh']['time_idx'], 1)
204214
brain.close()
205215

206216

surfer/viz.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ def add_data(self, array, min=None, max=None, thresh=None,
802802
colormap="RdBu_r", alpha=1,
803803
vertices=None, smoothing_steps=20, time=None,
804804
time_label="time index=%d", colorbar=True,
805-
hemi=None, remove_existing=False, time_label_size=14):
805+
hemi=None, remove_existing=False, time_label_size=14,
806+
initial_time=None):
806807
"""Display data from a numpy array on the surface.
807808
808809
This provides a similar interface to add_overlay, but it displays
@@ -857,6 +858,9 @@ def add_data(self, array, min=None, max=None, thresh=None,
857858
conserving memory when displaying different data in a loop.
858859
time_label_size : int
859860
Font size of the time label (default 14)
861+
initial_time : float | None
862+
Time initially shown in the plot. ``None`` to use the first time
863+
sample (default).
860864
"""
861865
hemi = self._check_hemi(hemi)
862866

@@ -907,6 +911,12 @@ def add_data(self, array, min=None, max=None, thresh=None,
907911
if not self.n_times == len(time):
908912
raise ValueError('time is not the same length as '
909913
'array.shape[1]')
914+
# initial time
915+
if initial_time is None:
916+
initial_time_index = None
917+
else:
918+
initial_time_index = self.index_for_time(initial_time)
919+
# time label
910920
if isinstance(time_label, string_types):
911921
time_label_fmt = time_label
912922

@@ -919,6 +929,7 @@ def time_label(x):
919929
else:
920930
self._times = None
921931
self.n_times = None
932+
initial_time_index = None
922933

923934
surfs = []
924935
bars = []
@@ -938,17 +949,19 @@ def time_label(x):
938949
name="time_label", row=row, col=col,
939950
font_size=time_label_size,
940951
justification='right')
941-
self._toggle_render(True, views)
942952
data['surfaces'] = surfs
943953
data['colorbars'] = bars
944954
data['orig_ctable'] = ct
945955

946956
if remove_existing and self.data_dict[hemi] is not None:
947957
for surf in self.data_dict[hemi]['surfaces']:
948958
surf.parent.parent.remove()
949-
950959
self.data_dict[hemi] = data
951960

961+
if initial_time_index is not None:
962+
self.set_data_time_index(initial_time_index)
963+
self._toggle_render(True, views)
964+
952965
def add_annotation(self, annot, borders=True, alpha=1, hemi=None,
953966
remove_existing=True):
954967
"""Add an annotation file.

0 commit comments

Comments
 (0)