Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG: Better split file handling in Report.parse_folder() #8486

Merged
merged 7 commits into from Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Expand Up @@ -103,6 +103,8 @@ Bugs

- Attempting to remove baseline correction from preloaded `~mne.Epochs` will now raise an exception by `Richard Höchenberger`_ (:gh:`8435`)

- :meth:`mne.Report.parse_folder` will now correctly handle split FIFF files by `Richard Höchenberger`_ (:gh:`8486`)

API changes
~~~~~~~~~~~

Expand Down
24 changes: 24 additions & 0 deletions mne/report.py
Expand Up @@ -1456,6 +1456,30 @@ class construction.
for p in pattern:
fnames.extend(sorted(_recursive_search(self.data_path, p)))

if not fnames and not render_bem:
raise RuntimeError(f'No matching files found in {self.data_path}')

# For split files, only keep the first one.
fnames_to_remove = []
for fname in fnames:
if _endswith(fname, ('raw', 'sss', 'meg')):
inst = read_raw_fif(fname, allow_maxshield=True, preload=False)
elif _endswith(fname, ('epo',)):
inst = read_epochs(fname, preload=False)
elif _endswith(fname, ('ave',)):
inst = read_evokeds(fname, condition=0, allow_maxshield=True)
else:
continue

if len(inst.filenames) > 1:
fnames_to_remove.extend(inst.filenames[1:])

fnames_to_remove = list(set(fnames_to_remove)) # Drop duplicates
for fname in fnames_to_remove:
if fname in fnames:
del fnames[fnames.index(fname)]
del fnames_to_remove

if self.info_fname is not None:
info = read_info(self.info_fname, verbose=False)
sfreq = info['sfreq']
Expand Down
17 changes: 16 additions & 1 deletion mne/tests/test_report.py
Expand Up @@ -296,7 +296,8 @@ def test_render_mri_without_bem(tmpdir):
shutil.copyfile(mri_fname, op.join(tempdir, 'sample', 'mri', 'T1.mgz'))
report = Report(info_fname=raw_fname,
subject='sample', subjects_dir=tempdir)
report.parse_folder(tempdir, render_bem=False)
with pytest.raises(RuntimeError, match='No matching files found'):
report.parse_folder(tempdir, render_bem=False)
with pytest.warns(RuntimeWarning, match='No BEM surfaces found'):
report.parse_folder(tempdir, render_bem=True, mri_decim=20)
assert 'bem' in report.fnames
Expand Down Expand Up @@ -495,4 +496,18 @@ def test_scraper(tmpdir):
assert op.isfile(img_fname.replace('png', 'svg'))


@pytest.mark.parametrize('split_naming', ('neuromag', 'bids',))
def test_split_files(tmpdir, split_naming):
"""Test that in the case of split files, we only parse the first."""
raw = read_raw_fif(raw_fname)
split_size = '7MB' # Should produce 3 files
buffer_size_sec = 1 # Tiny buffer so it's smaller than the split size
raw.save(op.join(tmpdir, 'raw_meg.fif'), split_size=split_size,
split_naming=split_naming, buffer_size_sec=buffer_size_sec)

report = Report()
report.parse_folder(tmpdir, render_bem=False)
assert len(report.fnames) == 1


run_tests_if_main()