Skip to content

Commit

Permalink
updating tests to reproduce a previous problem
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf committed May 3, 2016
1 parent 07d42ca commit 45bf139
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
12 changes: 7 additions & 5 deletions mne/channels/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,24 @@ def inst_has(attr):
elif isinstance(self, Evoked):
self.data = self.data.take(idx, axis=0)

def add_channels(self, add_list, force_update_info=False, copy=None):
def add_channels(self, add_list, copy=None, force_update_info=False):
"""Append new channels to the instance.
Parameters
----------
add_list : list
A list of objects to append to self. Must contain all the same
type as the current object
force_update_info : bool
If True, force the info for objects to be appended to match the
values in `self`. This should generally only be used when adding
stim channels for which important metadata won't be overwritten.
copy : bool
This parameter has been deprecated and will be removed in 0.13.
Use inst.copy() instead.
Whether to return a new instance or modify in place.
force_update_info : bool
If True, force the info for objects to be appended to match the
values in `self`. This should generally only be used when adding
stim channels for which important metadata won't be overwritten.
.. versionadded:: 0.12
Returns
-------
Expand Down
20 changes: 11 additions & 9 deletions mne/io/fiff/tests/test_raw_fiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,20 +1051,22 @@ def test_add_channels():
)
raw_new = raw_meg.copy().add_channels([raw_eeg])

# Testing force updates
raw_eeg_conf = raw_eeg.copy()
raw_eeg_conf.info['sfreq'] = 1.
assert_raises(RuntimeError, raw_meg.copy().add_channels, [raw_eeg_conf])
raw_new = raw_meg.copy().add_channels([raw_eeg_conf],
force_update_info=True)
assert_true(raw_eeg_conf.info['sfreq'] == 1.)
assert_true(raw_new.info['sfreq'] == raw_meg.info['sfreq'])

assert_true(ch in raw_new.ch_names for ch in raw.ch_names)
assert_array_equal(raw_new[:, :][0], raw_eeg_meg[:, :][0])
assert_array_equal(raw_new[:, :][1], raw[:, :][1])
assert_true(all(ch not in raw_new.ch_names for ch in raw_stim.ch_names))

# Testing force updates
raw_arr_info = create_info(['1', '2'], raw_meg.info['sfreq'], 'eeg')
orig_head_t = raw_arr_info['dev_head_t']
raw_arr = np.random.randn(2, raw_eeg.n_times)
raw_arr = RawArray(raw_arr, raw_arr_info)
# This should error because of conflicts in Info
assert_raises(ValueError, raw_meg.copy().add_channels, [raw_arr])
raw_meg.copy().add_channels([raw_arr], force_update_info=True)
# Make sure that values didn't get overwritten
assert_true(raw_arr.info['dev_head_t'] is orig_head_t)

# Now test errors
raw_badsf = raw_eeg.copy()
raw_badsf.info['sfreq'] = 3.1415927
Expand Down
7 changes: 4 additions & 3 deletions mne/io/meas_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,9 +1504,10 @@ def _force_update_info(info_base, info_target):
exclude_keys = ['chs', 'ch_names', 'nchan']
info_target = np.atleast_1d(info_target).ravel()
all_infos = np.hstack([info_base, info_target])
msg = 'Inputs must be of type Info. Found type {0}'
if not all([isinstance(ii, Info) for ii in all_infos]):
raise ValueError(msg.format(type(ii)))
for ii in all_infos:
if not isinstance(ii, Info):
raise ValueError('Inputs must be of type Info. '
'Found type %s' % type(ii))
for key, val in info_base.items():
if key in exclude_keys:
continue
Expand Down
1 change: 1 addition & 0 deletions mne/io/tests/test_meas_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def test_merge_info():

# Testing for force updates before merging
info_c = create_info(ch_names=['g', 'h', 'i'], sfreq=500., ch_types=None)
# This will break because sfreq is not equal
assert_raises(RuntimeError, _merge_info, [info_a, info_c])
_force_update_info(info_a, info_c)
assert_true(info_c['sfreq'] == info_a['sfreq'])
Expand Down

0 comments on commit 45bf139

Please sign in to comment.