You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH/API: new contracts and conditions for mne.io.meas_info.Info #2860
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."
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 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.
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).
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?
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.
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?
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)
.
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.
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.
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 ;)
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.
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?
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".
@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.
Activity
larsoner commentedon Feb 8, 2016
@wmvanvliet pointed out in the other thread that we need these to work:
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
badsattribute. One advantage to making thebadssetting 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.dengemann commentedon Feb 8, 2016
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).wmvanvliet commentedon Feb 9, 2016
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?
dengemann commentedon Feb 9, 2016
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.
wmvanvliet commentedon Feb 9, 2016
ok, that makes sense. thanks.
wmvanvliet commentedon Feb 9, 2016
Here are MNE's dict structures:
ConductorModelCovariance_DecodingTime)ForwardInfoInverseOperatorProjectionMost if not all of them have some consistency requirements. For example, the length of the
ch_namesfield should be the number of rows in thedatafield. 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 subclasscollections.Mapping. Or alternatively subclass aFrozenDictwhich 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 becomeFrozenDict's and any lists will become tuples. (If we would not do this, things likeinfo['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?
dengemann commentedon Feb 9, 2016
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:
agramfort commentedon Feb 9, 2016
in the past dict were converted to object eg to add a nice repr or to
add read/write methods
jaeilepp commentedon Feb 9, 2016
With this new
ChannelNameListthe output ofraw.info['ch_names']is quite uninformative + it throws a bunch of warnings for me:This should be fixed, unless we decide to roll back.
wmvanvliet commentedon Feb 9, 2016
@jaeilepp how would you like the output to look?
wmvanvliet commentedon Feb 9, 2016
These two statements sound contradictory to me. You want all the fields to be write protected, but you only want to do this for
Infodicts and not other dicts where it would make just as much sense.agramfort commentedon Feb 9, 2016
like before :)
dengemann commentedon Feb 9, 2016
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.
wmvanvliet commentedon Feb 9, 2016
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
updatemethod and a private_check_consistency. We make it clear in the docs that theupdatemethod 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 theupdatefunction, it's his/her own fault if things get buggy ;)dengemann commentedon Feb 9, 2016
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.20 remaining items
wmvanvliet commentedon Feb 12, 2016
yes!
larsoner commentedon Feb 15, 2016
sounds reasonable to me
larsoner commentedon Apr 14, 2016
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?
wmvanvliet commentedon Apr 15, 2016
I'll see what I can do. Any help is appreciated! :)
larsoner commentedon Apr 15, 2016
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".larsoner commentedon Apr 20, 2016
@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.
wmvanvliet commentedon Apr 20, 2016
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.
larsoner commentedon Sep 8, 2016
Can we close this one for now unless someone is really interested in enhancing the object?
agramfort commentedon Sep 8, 2016
larsoner commentedon Sep 8, 2016
We can reopen if someone gets motivated again