Skip to content

Commit

Permalink
[MRG] Be more precise about warnings (round 2) (#5428)
Browse files Browse the repository at this point in the history
* more descriptive 'match=xxxxxxxxx'

* Unify do not warn

* pep8
  • Loading branch information
massich authored and agramfort committed Aug 10, 2018
1 parent 8e44a98 commit ff8ebca
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions mne/beamformer/tests/test_dics.py
Expand Up @@ -335,9 +335,9 @@ def test_apply_dics_timeseries():
# Sanity checks on the resulting STC after applying DICS on epochs.
# Also test here that no warnings are thrown - implemented to check whether
# src should not be None warning occurs
with pytest.warns(None) as wrn:
with pytest.warns(None) as w:
stcs = apply_dics_epochs(epochs, filters)
assert len(wrn) == 0
assert len(w) == 0

assert isinstance(stcs, list)
assert len(stcs) == 1
Expand Down
10 changes: 6 additions & 4 deletions mne/beamformer/tests/test_lcmv.py
Expand Up @@ -341,9 +341,9 @@ def test_lcmv():
# this channel from the data
# also test here that no warnings are thrown - implemented to check whether
# src should not be None warning occurs
with pytest.warns(None) as wrn:
with pytest.warns(None) as w:
stc = apply_lcmv(evoked, filters, max_ori_out='signed')
assert len(wrn) == 0
assert len(w) == 0
# the result should be equal to applying this filter to a dataset without
# this channel:
stc_ch = apply_lcmv(evoked_ch, filters, max_ori_out='signed')
Expand Down Expand Up @@ -593,7 +593,8 @@ def test_tf_lcmv():

# Pass only one epoch to test if subtracting evoked
# responses yields zeros
with pytest.warns(RuntimeWarning, match='samples'):
with pytest.warns(RuntimeWarning,
match='Too few samples .* estimate may be unreliable'):
stcs = tf_lcmv(epochs[0], forward, noise_covs, tmin, tmax, tstep,
win_lengths, freq_bins, subtract_evoked=True, reg=reg,
label=label, raw=raw)
Expand Down Expand Up @@ -635,7 +636,8 @@ def test_lcmv_ctf_comp():
epochs = mne.Epochs(raw, events, tmin=0., tmax=0.2)
evoked = epochs.average()

with pytest.warns(RuntimeWarning, match='samples'):
with pytest.warns(RuntimeWarning,
match='Too few samples .* estimate may be unreliable'):
data_cov = mne.compute_covariance(epochs)
fwd = mne.make_forward_solution(evoked.info, None,
mne.setup_volume_source_space(pos=15.0),
Expand Down
2 changes: 1 addition & 1 deletion mne/channels/tests/test_interpolation.py
Expand Up @@ -115,7 +115,7 @@ def test_interpolation():
# check that interpolation works when non M/EEG channels are present
# before MEG channels
raw.rename_channels({'MEG 0113': 'TRIGGER'})
with pytest.warns(RuntimeWarning, match='unit'): # change of units
with pytest.warns(RuntimeWarning, match='unit .* changed from .* to .*'):
raw.set_channel_types({'TRIGGER': 'stim'})
raw.info['bads'] = [raw.info['ch_names'][1]]
raw.load_data()
Expand Down
2 changes: 1 addition & 1 deletion mne/channels/tests/test_montage.py
Expand Up @@ -396,7 +396,7 @@ def test_read_dig_montage():
else:
# extra column
fout.write(line.rstrip() + b' 0.0 0.0 0.0\n')
with pytest.warns(RuntimeWarning, match='columns'):
with pytest.warns(RuntimeWarning, match='Found .* columns instead of 3'):
montage_extra = read_dig_montage(extra_hsp, hpi, elp, names)
assert_allclose(montage_extra.hsp, montage.hsp)
assert_allclose(montage_extra.elp, montage.elp)
Expand Down
2 changes: 1 addition & 1 deletion mne/decoding/tests/test_transformer.py
Expand Up @@ -112,7 +112,7 @@ def test_filterestimator():

filt = FilterEstimator(epochs.info, l_freq=1, h_freq=1)
y = epochs.events[:, -1]
with pytest.warns(RuntimeWarning, match='longer than'):
with pytest.warns(RuntimeWarning, match='longer than the signal'):
pytest.raises(ValueError, filt.fit_transform, epochs_data, y)

filt = FilterEstimator(epochs.info, l_freq=40, h_freq=None,
Expand Down
4 changes: 2 additions & 2 deletions mne/io/ctf/tests/test_ctf.py
Expand Up @@ -57,7 +57,7 @@ def test_read_ctf():
os.mkdir(op.join(temp_dir, 'randpos'))
ctf_eeg_fname = op.join(temp_dir, 'randpos', ctf_fname_catch)
shutil.copytree(op.join(ctf_dir, ctf_fname_catch), ctf_eeg_fname)
with pytest.warns(RuntimeWarning, match='MISC channel'):
with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'):
raw = _test_raw_reader(read_raw_ctf, directory=ctf_eeg_fname)
picks = pick_types(raw.info, meg=False, eeg=True)
pos = np.random.RandomState(42).randn(len(picks), 3)
Expand All @@ -73,7 +73,7 @@ def test_read_ctf():
'%0.5f' % x for x in 100 * pos[ii]) # convert to cm
fid.write(('\t'.join(args) + '\n').encode('ascii'))
pos_read_old = np.array([raw.info['chs'][p]['loc'][:3] for p in picks])
with pytest.warns(RuntimeWarning, match='MISC channel'):
with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'):
raw = read_raw_ctf(ctf_eeg_fname) # read modified data
pos_read = np.array([raw.info['chs'][p]['loc'][:3] for p in picks])
assert_allclose(apply_trans(raw.info['ctf_head_t'], pos), pos_read,
Expand Down
12 changes: 7 additions & 5 deletions mne/io/edf/tests/test_edf.py
Expand Up @@ -100,7 +100,7 @@ def test_bdf_stim_channel():
@testing.requires_testing_data
def test_edf_overlapping_annotations():
"""Test EDF with overlapping annotations."""
with pytest.warns(RuntimeWarning, match='overlapping'):
with pytest.warns(RuntimeWarning, match='overlapping.* not fully support'):
read_raw_edf(edf_overlap_annot_path, preload=True, stim_channel='auto',
verbose=True)

Expand Down Expand Up @@ -156,7 +156,8 @@ def test_edf_data():
fid_out.write(rbytes[:236])
fid_out.write(bytes('-1 '.encode()))
fid_out.write(rbytes[244:])
with pytest.warns(RuntimeWarning, match='record'):
with pytest.warns(RuntimeWarning,
match='records .* not match the file size'):
raw = read_raw_edf(broken_fname, preload=True, stim_channel='auto')
read_raw_edf(broken_fname, exclude=raw.ch_names[:132], preload=True,
stim_channel='auto')
Expand Down Expand Up @@ -200,11 +201,12 @@ def test_stim_channel():
pytest.raises(RuntimeError, read_raw_edf, edf_path, preload=False,
stim_channel=-1)

with pytest.warns(RuntimeWarning, match='jitter'):
with pytest.warns(RuntimeWarning,
match='Interpolating stim .* Events may jitter'):
raw = read_raw_edf(edf_stim_resamp_path, verbose=True, stim_channel=-1)
with pytest.warns(None) as record:
with pytest.warns(None) as w:
raw[:]
assert len(record) == 0
assert len(w) == 0

events = raw_py.find_edf_events()
assert (len(events) == 0)
Expand Down
4 changes: 2 additions & 2 deletions mne/io/fiff/tests/test_raw_fiff.py
Expand Up @@ -642,7 +642,7 @@ def test_io_complex():
raw_cp = raw.copy()
raw_cp._data = np.array(raw_cp._data, dtype)
raw_cp._data[picks, start:stop] += imag_rand
with pytest.warns(RuntimeWarning, match='complex'):
with pytest.warns(RuntimeWarning, match='Saving .* complex data.'):
raw_cp.save(op.join(tempdir, 'raw.fif'), picks, tmin=0, tmax=5,
overwrite=True)

Expand Down Expand Up @@ -1254,7 +1254,7 @@ def test_annotation_crop():
sfreq = raw.info['sfreq']
annot = Annotations([0., raw.times[-1]], [2., 2.], 'test',
raw.info['meas_date'] + raw.first_samp / sfreq - 1.)
with pytest.warns(RuntimeWarning, match='data range'):
with pytest.warns(RuntimeWarning, match='Limited .* expanding outside'):
raw.set_annotations(annot)
assert_allclose(raw.annotations.duration,
[1., 1. + 1. / raw.info['sfreq']], atol=1e-3)
Expand Down
3 changes: 2 additions & 1 deletion mne/io/kit/tests/test_kit.py
Expand Up @@ -236,7 +236,8 @@ def test_decimate():
np.savetxt(sphere_hsp_path, hsp_mm)

# read in raw data using spherical hsp, and extract new hsp
with pytest.warns(RuntimeWarning, match='more than'):
with pytest.warns(RuntimeWarning,
match='was automatically downsampled .* FastScan'):
raw = read_raw_kit(sqd_path, mrk_path, elp_txt_path, sphere_hsp_path)
# collect headshape from raw (should now be in m)
hsp_dec = np.array([dig['r'] for dig in raw.info['dig']])[8:]
Expand Down
4 changes: 2 additions & 2 deletions mne/io/tests/test_raw.py
Expand Up @@ -148,8 +148,8 @@ def test_time_index():

def test_annotation_property_deprecation_warning():
"""Test that assigning annotations warns and nowhere else."""
with pytest.warns(None) as record:
with pytest.warns(None) as w:
raw = RawArray(np.random.rand(1, 1), create_info(1, 1))
assert len(record) is 0
assert len(w) is 0
with pytest.warns(DeprecationWarning, match='by assignment is deprecated'):
raw.annotations = None
4 changes: 2 additions & 2 deletions mne/io/tests/test_reference.py
Expand Up @@ -415,7 +415,7 @@ def test_add_reference():
# create epochs in delayed mode, allowing removal of CAR when re-reffing
epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5,
picks=picks_eeg, preload=True, proj='delayed')
with pytest.warns(RuntimeWarning, match='ignored'):
with pytest.warns(RuntimeWarning, match='ignored .set to zero.'):
epochs_ref = add_reference_channels(epochs, ['M1', 'M2'], copy=True)
assert_equal(epochs_ref._data.shape[1], epochs._data.shape[1] + 2)
_check_channel_names(epochs_ref, ['M1', 'M2'])
Expand Down Expand Up @@ -455,7 +455,7 @@ def test_add_reference():
epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5,
picks=picks_eeg, preload=True, proj='delayed')
evoked = epochs.average()
with pytest.warns(RuntimeWarning, match='ignored'):
with pytest.warns(RuntimeWarning, match='ignored .set to zero.'):
evoked_ref = add_reference_channels(evoked, ['M1', 'M2'], copy=True)
assert_equal(evoked_ref.data.shape[0], evoked.data.shape[0] + 2)
_check_channel_names(evoked_ref, ['M1', 'M2'])
Expand Down
2 changes: 1 addition & 1 deletion mne/preprocessing/tests/test_ica.py
Expand Up @@ -428,7 +428,7 @@ def test_ica_additional(method):
for cov in (None, test_cov):
ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4,
n_pca_components=4, method=method, max_iter=1)
with pytest.warns(None):
with pytest.warns(None): # ICA does not converge
ica.fit(raw, picks=picks[:10], start=start, stop=stop2)
sources = ica.get_sources(epochs).get_data()
assert (ica.mixing_matrix_.shape == (2, 2))
Expand Down

0 comments on commit ff8ebca

Please sign in to comment.