Skip to content

ENH/API: new contracts and conditions for mne.io.meas_info.Info #2860

Closed

Description

@dengemann

The recent discussion in #2765 has lead me to discuss with Eric about different ways in which we could improve the Info API to be more clearer. One aspect that maybe was not so clear during the discussion we had over there was the notion of semantic VS code complexity. My idea was that we can have complex implementations but clear APIs that you can quickly conceptualise without exceptions and sub-rules. As it looks now we would have to say:

"our info is like a dict but you cannot use the dict API without constraints, as it has some extra behavior, depending on the keys it will be read only and it requires a subclass of list with reduced API for ch_names"

Maybe it's time to change the API and introduce explictly a new object with simplified semantics

"our info is a read only dict with nice convenience extras, but items cannot be set directly after construction."

it would then allow for a worflow like this:

info_as_dict = dict(info)
info_as_dict['lowpass'] /= 2
info = Info(info_as_dict)

This could then be the internals of a public or private _update method that takes keyword arguments and hence allows for easier consistency and value checking. It could return a copy to be consistent. We could also use the FrozenDict pattern

It would also allow us to implement expose a save method etc.

We could then send a clear message with the release and have a nice documentation that prepares everyone for this backwards incompatible change (without deprecation)

@wmvanvliet @Eric89GXL @jasmainak @agramfort, @jona-sassenhagen ..., ...

Let me know what you think.

Activity

  1. larsoner commented on Feb 8, 2016

    @larsoner
    Member

    @wmvanvliet pointed out in the other thread that we need these to work:

    info['bads'] = [...]
    info['bads'] += [...]
    

    But I think those are the only direct-write methods that we'd need to support. So we could say it's frozen other than the bads attribute. One advantage to making the bads setting a little bit smarter is that we can do things like emit warnings when bads are added that exist in projs. So we'd get that as an advantage, too.

    Are there other usage patterns that wouldn't work making it write-only? I'm not worried about how we modify things internally in private functions (e.g., in set_channel_types), but rather in terms of what users would actually access and set. info['bads'] is all that comes immediately for mind to me.

  2. dengemann commented on Feb 8, 2016

    @dengemann
    MemberAuthor

    I think it might also be a good moment to abandon this, it is not very explicit anyways.
    Why not have a .set_bads(ch_names) method. Also we could have other setters (that would always return a copy).

  3. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    Before I can weigh in on this, I need to be educated a little.

    Can someone explain to me why some data structures are implemented as dicts (info, forward) and others as full classes (source estimate, montage)? I always thought this was just historical, but @dengemann hinted at deeper reasons of having simple, flat objects. If I were to implement a new datastructure, should it be a dict or a class?

  4. dengemann commented on Feb 9, 2016

    @dengemann
    MemberAuthor

    The idea is that dicts are nice general purpose heterogenous data containers. So if the purpose of a class is essentially to contain data we're using dicts. If the class is also there to process data (includig viz), we most of the time don't subclass dicts. I think that's the main distinction.

  5. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    ok, that makes sense. thanks.

  6. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    Here are MNE's dict structures:

    • ConductorModel
    • Covariance
    • (_DecodingTime)
    • Forward
    • Info
    • InverseOperator
    • Projection

    Most if not all of them have some consistency requirements. For example, the length of the ch_names field should be the number of rows in the data field. In cases like this, the immutable pattern makes a lot of sense. New objects can be created, but afterwards, never altered. If a value needs altering, you make a new object, optionally using the old object as a template. The main advantage of this approach is that you can always have sane consistency checking.

    Making these objects immutable would mean that instead of subclassing dict, they would subclass collections.Mapping. Or alternatively subclass a FrozenDict which we would make ourselves. Any data stored inside the structure would also have to be immutable objects. All primitive types (int, str, float, bool) are immutable, so this mainly means that any dicts will become FrozenDict's and any lists will become tuples. (If we would not do this, things like info['bads'] += 'foo' would still work and bypass the consistency checks.)

    Obviously, this would require many small refactorings across our code base.

    IMO, the main question we must ask ourselves is how much user script we're going to break with this. How common is it for a user to mess with the values in a dict structure?

  7. dengemann commented on Feb 9, 2016

    @dengemann
    MemberAuthor

    I disagree that the subtypes have to be frozen too. My point was to have
    clear API pattern instead of write protecting some fields but not others.

    On Tue, Feb 9, 2016 at 1:01 PM, Marijn van Vliet [email protected]
    wrote:

    Here are MNE's dict structures:

    • ConductorModel
    • Covariance
    • (_DecodingTime)
    • Forward
    • Info
    • InverseOperator
    • Projection

    Most if not all of them have some consistency requirements. For example,
    the length of the ch_names field should be the number of rows in the data
    field. In cases like this, the immutable pattern makes a lot of sense. New
    objects can be created, but afterwards, never altered. If a value needs
    altering, you make a new object, optionally using the old object as a
    template. The main advantage of this approach is that you can always have
    sane consistency checking.

    Making these objects immutable would mean that instead of subclassing dict,
    they would subclass collections.Mapping. Or alternatively subclass a
    FrozenDict which we would make ourselves. Any data stored inside the
    structure would also have to be immutable objects. All primitive types
    (int, str, float, bool) are immutable, so this mainly means that any dicts
    will become FrozenDict's and any lists will become tuples. (If we would
    not do this, things like info['bads'] += 'foo' would still work and
    bypass the consistency checks.)

    Obviously, this would require many small refactorings across our code base.

    IMO, the main question we must ask ourselves is how much user script we're
    going to break with this. How common is it for a user to mess with the
    values in a dict structure?

    —
    Reply to this email directly or view it on GitHub
    #2860 (comment)
    .

  8. agramfort commented on Feb 9, 2016

    @agramfort
    SponsorMember

    in the past dict were converted to object eg to add a nice repr or to
    add read/write methods

  9. jaeilepp commented on Feb 9, 2016

    @jaeilepp
    Contributor

    With this new ChannelNameList the output of raw.info['ch_names'] is quite uninformative + it throws a bunch of warnings for me:

    /home/jaakko/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:92: DeprecationWarning: DisplayFormatter._ipython_display_formatter_default is deprecated: use @default decorator instead.
      def _ipython_display_formatter_default(self):
    /home/jaakko/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:669: DeprecationWarning: PlainTextFormatter._singleton_printers_default is deprecated: use @default decorator instead.
      def _singleton_printers_default(self):
    /home/jaakko/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:672: DeprecationWarning: PlainTextFormatter._type_printers_default is deprecated: use @default decorator instead.
      def _type_printers_default(self):
    /home/jaakko/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:677: DeprecationWarning: PlainTextFormatter._deferred_printers_default is deprecated: use @default decorator instead.
      def _deferred_printers_default(self):
    Out[3]: <ChannelNameList | 320 channels | IASX+, IASX-, IASY+, IASY-, IASZ+ ... MEG2642, MEG2643, STI101, SYS101, SYS201>
    

    This should be fixed, unless we decide to roll back.

  10. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    @jaeilepp how would you like the output to look?

  11. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    I disagree that the subtypes have to be frozen too. My point was to have clear API pattern instead of write protecting some fields but not others.

    These two statements sound contradictory to me. You want all the fields to be write protected, but you only want to do this for Info dicts and not other dicts where it would make just as much sense.

  12. agramfort commented on Feb 9, 2016

    @agramfort
    SponsorMember
  13. dengemann commented on Feb 9, 2016

    @dengemann
    MemberAuthor

    You want all the fields to be write protected, but you only want to do this for Info dicts and not other dicts where it would make just as much sense.

    I did not comment on that actually. I only talked about the values of that dict that do not have to be frozen necessarily. Well we can just keep the dict API in many other cases or we could also use kind of a frozen pattern.

  14. wmvanvliet commented on Feb 9, 2016

    @wmvanvliet
    Contributor

    here is an alternative proposal:

    we revert #2765 and keep the dict structures as they are, but we add to each of them a public update method and a private _check_consistency. We make it clear in the docs that the update method is the preferred way to modify the data structure. But we don't strictly enforce this, hence, no existing code is broken at all. If in the future a user doesn't use the update function, it's his/her own fault if things get buggy ;)

  15. dengemann commented on Feb 9, 2016

    @dengemann
    MemberAuthor

    That sounds like a reasonable direction. Subclassing the update method would allow us to do all sorts of checking on the fly. And the API pattern would be simple such that we could easily alert users about it in the next release. We could still freeze the __set_item__ methods if that would make us feel safer.

  16. 20 remaining items

  17. wmvanvliet commented on Feb 12, 2016

    @wmvanvliet
    Contributor

    yes!

  18. larsoner commented on Feb 15, 2016

    @larsoner
    Member

    sounds reasonable to me

  19. added this to the 0.12 milestone on Apr 14, 2016
  20. larsoner commented on Apr 14, 2016

    @larsoner
    Member

    We should really resolve this one for 0.12 so we don't immediately need to deprecate. @wmvanvliet do you have any time you could dedicate to this in the next week or two?

  21. wmvanvliet commented on Apr 15, 2016

    @wmvanvliet
    Contributor

    I'll see what I can do. Any help is appreciated! :)

  22. larsoner commented on Apr 15, 2016

    @larsoner
    Member

    One thing we could do is more or less revert the changes -- make a single commit that changes it back to deriving from dict. Then we can deal with the more complex solution later. WDYT? If so I might be able to do the first "fix".

  23. larsoner commented on Apr 20, 2016

    @larsoner
    Member

    @wmvanvliet I assume by the silence that you will be too busy to pursue something comprehensive in the next week or two for 0.12, so I'll go ahead with the revert-like plan tomorrow or the next day unless I hear otherwise.

  24. wmvanvliet commented on Apr 20, 2016

    @wmvanvliet
    Contributor

    That would be best. I don't have the time right now to make such an invasive change properly. I will pick this up though :) Maybe for 0.13.

  25. modified the milestones: 0.13, 0.12 on Apr 20, 2016
  26. larsoner commented on Sep 8, 2016

    @larsoner
    Member

    Can we close this one for now unless someone is really interested in enhancing the object?

  27. removed this from the 0.13 milestone on Sep 8, 2016
  28. agramfort commented on Sep 8, 2016

    @agramfort
    SponsorMember
  29. larsoner commented on Sep 8, 2016

    @larsoner
    Member

    We can reopen if someone gets motivated again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions