Skip to content

[MRG] Annotation fix to allow Semicolons in Description - #7288

Merged
larsoner merged 16 commits into
mne-tools:masterfrom
adam2392:annotation
Feb 11, 2020
Merged

larsoner merged 16 commits into
mne-tools:masterfrom
adam2392:annotation

Conversation

@adam2392

@adam2392 adam2392 commented Feb 5, 2020

Copy link
Copy Markdown
Member

Thanks for contributing a pull request! Please make sure you have read the
contribution guidelines
before submitting.

Reference issue

Fixes: #7287

What does this implement/fix?

WIP, attempts to check if removing the ";" check breaks anything.

Additional information

See Issue for sample data.

@adam2392 adam2392 changed the title Annotation [WIP] Annotation fix to allow Semicolons in Description Feb 5, 2020
@codecov

codecov Bot commented Feb 5, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #7288 into master will increase coverage by 0.11%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #7288      +/-   ##
==========================================
+ Coverage   89.69%   89.81%   +0.11%     
==========================================
  Files         447      447              
  Lines       80608    80716     +108     
  Branches    12882    12876       -6     
==========================================
+ Hits        72301    72492     +191     
+ Misses       5447     5394      -53     
+ Partials     2860     2830      -30     

@agramfort

Copy link
Copy Markdown
Member

I suspect the issue comes from saving the file to fif. However I agree that we should not prevent ";" unless you want to save it to a .fif file.

@larsoner

larsoner commented Feb 5, 2020

Copy link
Copy Markdown
Member

And even in that case we can probably sanitize with a substitution like ; turns into {SEMICOLON}, and on read {SEMICOLON} gets turned into ;. The only problem with this is the tiny possibility someone has {SEMICOLON} in their descriptions currently, but this probability is quite small and even if they has this, they can probably easily work around our substitution with a my_str.replace(';', '{SEMICOLON}') somewhere in their pipelines.

@agramfort

agramfort commented Feb 5, 2020 via email

Copy link
Copy Markdown
Member

@adam2392

adam2392 commented Feb 5, 2020

Copy link
Copy Markdown
Member Author

In annotations.py:806,

            elif kind == FIFF.FIFF_COMMENT:
                description = tag.data.split(':')
                description = [d.replace(';', ':') for d in
                               description]

Is this already doing that?

@larsoner

larsoner commented Feb 5, 2020

Copy link
Copy Markdown
Member

Maybe? I would need to look at the code more closely to know, but really what I would do first (before changing any MNE code) is write a little test (following test-driven development):

def test_annot_sanitizing(tmpdir):
    """Test description sanitizing."""
    annot = Annotations([0], [1], ['a;:b'])
    fname = str(tmpdir.join('custom-annot.fif'))
    annot.save(fname)
    annot_read = read_annotations(fname)
    _assert_annotations_equal(annot, annot_read)

On master this reproduces your failure, and it's what we want to fix, which is great.

Then with your commenting out of lines this happens:

larsoner@bunk:~/python/mne-python$ pytest mne/tests/test_annotations.py -k sanit
...
        annot_read = read_annotations(fname)
>       _assert_annotations_equal(annot, annot_read)
E       AssertionError: 
E       Arrays are not equal
E       
E       Mismatched elements: 1 / 1 (100%)
E        x: array(['a;:b'], dtype='<U4')
E        y: array(['a::b'], dtype='<U4')

So it replaces ; with : but does not restore it on read.

My suggestion above should get this to pass.

@adam2392

adam2392 commented Feb 6, 2020

Copy link
Copy Markdown
Member Author

So it turns out that when you save a .fif annotations, the ":" is the issue, and they are replaced by ";" because of this function in write.py. I assume that ValueError was added originally to check downstream when annotations are split into onset, value and description.

def write_name_list(fid, kind, data):
    """Write a colon-separated list of names.

    Parameters
    ----------
    data : list of strings
    """
    write_string(fid, kind, ':'.join(data))

The tests pass, but I'm wondering is it necessary to add some documentation to the docstrings regarding this behavior?

@agramfort

Copy link
Copy Markdown
Member

I fear this will break reading files made with an older version of MNE

@larsoner

larsoner commented Feb 6, 2020

Copy link
Copy Markdown
Member

I fear this will break reading files made with an older version of MNE

I'm not sure what you mean -- the separator in the FIF file on disk will still be : -- it's just that other readers that didn't implement un-sanitizing would see {COLON} instead of : in their strings after reading from disk.

In any case, the changes in this PR are just to sanitize (and un-sanitize) descriptions annotations specifically, not globally in write_name_list. So the only ones being sanitized with {COLON} (and back) are annotations descriptions so the scope is much more localized than a global write_name_list change would be.

Basically before the : character could be used but ; could not. This PR essentially would change it so that instead of ; being unusable, the sequence of characters {COLON} cannot be used (or can, but will be changed to : on I/O round-trip).

@agramfort

Copy link
Copy Markdown
Member

@larsoner if you think it can be merged fair enough.

Comment thread mne/annotations.py Resolved
@adam2392 adam2392 changed the title [WIP] Annotation fix to allow Semicolons in Description [MRG] Annotation fix to allow Semicolons in Description Feb 6, 2020
@adam2392
adam2392 requested a review from larsoner February 9, 2020 21:46

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Other than a minor wording suggestion (it's no longer a character but a substring), +1 for merge from me

Comment thread mne/annotations.py Resolved
Co-Authored-By: Eric Larson <[email protected]>
@adam2392

Copy link
Copy Markdown
Member Author

@larsoner cool sounds good! fixed it up. Thanks.

@larsoner
larsoner merged commit 2434a41 into mne-tools:master Feb 11, 2020
@larsoner

Copy link
Copy Markdown
Member

Thanks @adam2392 !

@larsoner

Copy link
Copy Markdown
Member

Ahh, forgot changes/latest.inc update, can you open a quick PR to add a line there in the BUG section about this fix?

@adam2392 adam2392 mentioned this pull request Feb 11, 2020
@nbara

nbara commented Mar 30, 2020 •

Copy link
Copy Markdown
Contributor

I fear this will break reading files made with an older version of MNE

This is essentially what happened here though ? I had .fif files with :, which got converted to ; before this PR.

Now loading these .fif files results in annotations with ; instead of :

Am I missing something @larsoner ?

@larsoner

Copy link
Copy Markdown
Member

Yes that might still be the case :(

Would a workaround like this work for you? It should be backward compatible / work on 0.19 and 0.20:

for di, desc in enumerate(annotations.description):
    annotations.description[di] = desc.replace(';', ':')

In principle we could add a replace_semicolon=None kwarg which defaults to True in 0.20, False in 0.21, and disappears in 0.22, and only warns if there are actually semicolons in the descriptions. I guess this would need to be added to read_raw_fif and read_annotations. WDYT @nbara ?

@nbara

nbara commented Mar 31, 2020

Copy link
Copy Markdown
Contributor

Yeah I guess I'll be using this workaround, until I convert my .fif files to be 0.20 compatible.

The replace_semicolon kwarg seems a bit overkill, especially if it's to be removed in future versions.

AdoNunes pushed a commit to AdoNunes/mne-python that referenced this pull request Apr 6, 2020
* Adding use_scalebars as a parameter.

* Fixed to adhere to mne-coding standards.

* Fixing formatting issues.

* Fixing show_scalebars in viz ica from the original naming of  use_scalebars.

* Update mne/viz/ica.py

Co-Authored-By: Daniel McCloy <[email protected]>

* Update mne/viz/raw.py

Co-Authored-By: Daniel McCloy <[email protected]>

* DOC: Correct version [ci skip]

* Remove semicolon.

* Delete test.py

* Fixing annotations bug.

* Adding unit test annotations. for expected error.

* Update mne/annotations.py

Co-Authored-By: Eric Larson <[email protected]>

Co-authored-by: Daniel McCloy <[email protected]>
Co-authored-by: Eric Larson <[email protected]>
AdoNunes pushed a commit to AdoNunes/mne-python that referenced this pull request Apr 6, 2020
* Adding use_scalebars as a parameter.

* Fixed to adhere to mne-coding standards.

* Fixing formatting issues.

* Fixing show_scalebars in viz ica from the original naming of  use_scalebars.

* Update mne/viz/ica.py

Co-Authored-By: Daniel McCloy <[email protected]>

* Update mne/viz/raw.py

Co-Authored-By: Daniel McCloy <[email protected]>

* DOC: Correct version [ci skip]

* Remove semicolon.

* Delete test.py

* Fixing annotations bug.

* Adding unit test annotations. for expected error.

* Update mne/annotations.py

Co-Authored-By: Eric Larson <[email protected]>

Co-authored-by: Daniel McCloy <[email protected]>
Co-authored-by: Eric Larson <[email protected]>
@adam2392
adam2392 deleted the annotation branch October 30, 2020 16:06
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.

";" are not supported in EDF annotation descriptions

4 participants