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: Fix amending a dataset when new columns are being added to participants.tsv #1113

Merged
merged 5 commits into from
Dec 27, 2022

Conversation

hoechenberger
Copy link
Member

@hoechenberger hoechenberger commented Dec 24, 2022

Closes #1104

MWE:

from pathlib import Path
import pandas as pd
import mne
from mne_bids import write_raw_bids, BIDSPath

BIDS_ROOT = Path('/tmp/bids-test')

sample_data_path = mne.datasets.sample.data_path(download=False)
raw_path = sample_data_path / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(raw_path)

bids_path = BIDSPath(
    task='test',
    suffix='meg',
    extension='.fif',
    datatype='meg',
    root=BIDS_ROOT,
)

# Need to write >1 participants first
bids_path.update(subject='01')
write_raw_bids(raw, bids_path, overwrite=True)

bids_path.update(subject='02')
write_raw_bids(raw, bids_path, overwrite=True)

# Drop two columns
participants_csv = pd.read_csv(BIDS_ROOT / "participants.tsv", sep='\t')
participants_csv = participants_csv.drop(columns=['weight', 'height'])
participants_csv.to_csv(BIDS_ROOT / "participants.tsv", index=False, na_rep="n/a", sep="\t")

# Write additional participant
bids_path.update(subject='03')
write_raw_bids(raw, bids_path, overwrite=True)

On main:

Traceback (most recent call last):
[...]
  File "/Users/hoechenberger/Development/mne-bids/mne_bids/write.py", line 1804, in write_raw_bids
    _participants_tsv(
  File "/Users/hoechenberger/Development/mne-bids/mne_bids/write.py", line 447, in _participants_tsv
    data = _combine_rows(orig_data, data, 'participant_id')
  File "/Users/hoechenberger/Development/mne-bids/mne_bids/tsv_handler.py", line 44, in _combine_rows
    data[key] = [data[key][n_rows - 1 - idx] for idx in idxs]
  File "/Users/hoechenberger/Development/mne-bids/mne_bids/tsv_handler.py", line 44, in <listcomp>
    data[key] = [data[key][n_rows - 1 - idx] for idx in idxs]
IndexError: list index out of range

Passes with this PR branch.

  • Add tests
  • Add changelog entry

PR Description

Merge checklist

Maintainer, please confirm the following before merging.
If applicable:

  • All comments are resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • New contributors have been added to CITATION.cff
  • PR description includes phrase "closes <#issue-number>"

Comment on lines +438 to +440
p_id = data['participant_id'][0]
if p_id in orig_data['participant_id']:
row_idx = orig_data['participant_id'].index(p_id)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just aesthetics.

for key in data.keys():
if key in orig_data:
continue

# add 'n/a' if any missing columns
orig_data[key] = ['n/a'] * len(next(iter(data.values())))
orig_data[key] = ['n/a'] * len(orig_data['participant_id'])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

participant_id column must always be present so just use this one.

@codecov
Copy link

Codecov bot commented Dec 24, 2022

Codecov Report

Merging #1113 (af951ce) into main (8b20f3a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1113   +/-   ##
=======================================
  Coverage   95.03%   95.04%           
=======================================
  Files          40       40           
  Lines        8668     8670    +2     
=======================================
+ Hits         8238     8240    +2     
  Misses        430      430           
Impacted Files Coverage Δ
mne_bids/tests/test_write.py 89.31% <100.00%> (+0.15%) ⬆️
mne_bids/write.py 97.28% <100.00%> (-0.01%) ⬇️
mne_bids/commands/tests/test_cli.py 96.61% <0.00%> (-1.70%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@hoechenberger hoechenberger changed the title Fix amending a dataset when new columns are being added to the TSV sidecars Fix amending a dataset when new columns are being added to participants.tsv Dec 25, 2022
@hoechenberger hoechenberger marked this pull request as ready for review December 25, 2022 16:44
@hoechenberger hoechenberger changed the title Fix amending a dataset when new columns are being added to participants.tsv MRG: Fix amending a dataset when new columns are being added to participants.tsv Dec 25, 2022
@hoechenberger

This comment was marked as resolved.

@hoechenberger

This comment was marked as resolved.

@hoechenberger hoechenberger merged commit 0c83b75 into mne-tools:main Dec 27, 2022
@hoechenberger hoechenberger deleted the hoechenberger/issue1104 branch December 27, 2022 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

write_raw_bids() throws an IndexError when overwriting participants.tsv with missing "default" columns
2 participants