Skip to content

WIP mne_kit2fiff - #366

Closed
teonbrooks wants to merge 104 commits into
mne-tools:masterfrom
teonbrooks:tb-mne_kit2fiff
Closed

teonbrooks wants to merge 104 commits into
mne-tools:masterfrom
teonbrooks:tb-mne_kit2fiff

Conversation

@teonbrooks

Copy link
Copy Markdown
Member

Very much still WIP.

Hi everyone,

I have been working on a python implementation of the mne_kit2fiff for the KIT MEG system. I have completed a large portion of it but I may need some help with a few tweaks and final steps:
-channel coil transformation
-channel unit vectors of coord_frame.
-fix dev_head_t
-creating a synthetic trigger channel

Feedback is welcome.

@dengemann

Copy link
Copy Markdown
Member

Hi @t3on great you work on this. I will have a deeper look soon. Just two tiny remarks. If it's meant to be a script, you should add option parsers and line calling the classes. Second as it looks, the stuff for reading kit files is almost there here. We should think about a module then like mne/fiff/kit.py. Finally, (ok, a third point), think about minimizing adding new classes. E.g. the head shape certainly don't needs a class. As you proceed, don't forget to add tests, if you can add a test-dataset.

@dengemann

Copy link
Copy Markdown
Member

.... finally, finally. doc strings are missing in some functions. even if its a private function, tell the visitors of the code by adding """Aux function""" or so.

@teonbrooks

Copy link
Copy Markdown
Member Author

Thanks @dengemann for the comments. I was wondering if you or anyone else knew how the channel coil transformation is calculated. Tangentially, do you know about the three unit vectors that follow the coordinates for the sensors? Are they actually used? If so, how are they calculated and where are they applied?

Comment thread mne/bin/mne_kit2fiff.py Outdated

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.

docstring missing ;-)

@dengemann

Copy link
Copy Markdown
Member

Hi @t3on, it may be a bit convolved, but the bti example demonstrates how this can achieved, at least when coming from bti data. Now I don't know, which coord system the KIT uses. Much depends on that.

@dengemann dengemann closed this Jan 11, 2013
@dengemann dengemann reopened this Jan 11, 2013
@dengemann

Copy link
Copy Markdown
Member

sorry, wrong button, reopened.

@dengemann

Copy link
Copy Markdown
Member

Basically you have to know how kit coordinates map on neuromag coordinates and you have to know whether the coordinates are in head or in sensor space. Depending on the ensuing 'case matrix' you have to do less or more work ;-)

Comment thread mne/bin/mne_kit2fiff.py Outdated

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.

too many blank lines

@larsoner

Copy link
Copy Markdown
Member

Some general comments, before we get too in-depth on the review:

  1. Thanks for looking into this -- it will be nice to expand capabilities to this system.
  2. Consider using a pep8 (and pyflakes) capable IDE. You can also run pep8 manually (pep8 whatever.py) from the command line. We adhere to PEP8 98% of the time.
  3. Look at examples of docstrings used in MNE. Good go-to examples can be found in Epochs.py. Notice how newlines are done, functions have one-line descriptions followed a couple lines later by a longer one (if necessary) and such. This all seems pedantic, but actually will helps us maintain the code in the long run, as well as make appropriate representations on the website and in capable IDEs. We always nitpick each other's code, so don't feel bad if we do it to you :)
  4. New functionality requires new tests. Especially since this is a system some are not familiar with (including me), you might want to upload a test file somewhere and link to it for us all to test it out with. Eventually we will need to work an abbreviated version in into the mne testing suite.
  5. I'll have to defer to @agramfort on the coil transformations, but I don't think creating a synthetic trigger channel will be too difficult. I would name it STI101, since I think that's the new Neuromag standard (right @agramfort?), and it could be all zeros except for one sample whenever a trigger was supposed to occur, which would be set to the trigger value. This should work with our event finding code, which basically seeks diffs > 0. (But again, see point 4, this would need to be covered by a test for validation.)

Incidentally, when I go about adding new functionality, I sometimes actually start with step (4), above. That is, I write or modify test_whatever.py to include the functionality that I'm looking for, then I start coding it. It can help validate that things actually work. And during the review phase, it can help us suggest different functionality. But to each their own...

@larsoner

Copy link
Copy Markdown
Member

Incidentally @agramfort @dengemann, how do you think on starting a new, more generic mne/import.py or mne.fiff.import.py? I think it would be appropriate, given this PR and @dengemann's #248 on importing 4D MagnesWH3600 files.

@dengemann

Copy link
Copy Markdown
Member

I think we already discussed it somewhere that this is the way to go. At least something like fiff/kit.py fiff/bti.py
But it's an excellent time to discuss this. Especially as additional support for systems brings up some style questions. E.g. try to use tag , open, reas and write modules, add new functions to those, add new Bunches to constants, etc. all to make it look consistently and not just a collection of code that works somehow.

Maybe 'import' might not be the best choice though for a name, as import already is a keyword...

On 11.01.2013, at 10:47, Eric89GXL [email protected] wrote:

Incidentally @agramfort @dengemann, how do you think on starting a new, more generic mne/import.py or mne.fiff.import.py? I think it would be appropriate, given this PR and @dengemann's #248 on importing 4D MagnesWH3600 files.

—
Reply to this email directly or view it on GitHub.

@dengemann

Copy link
Copy Markdown
Member

so to be a bit more clear my proposal would be to integrate support for other systems with our fiff eco system as densely as possible.
this implies additiona functions and constants in the modules we currently use for managing fiff -- makes sense?

On 11.01.2013, at 10:47, Eric89GXL [email protected] wrote:

Incidentally @agramfort @dengemann, how do you think on starting a new, more generic mne/import.py or mne.fiff.import.py? I think it would be appropriate, given this PR and @dengemann's #248 on importing 4D MagnesWH3600 files.

—
Reply to this email directly or view it on GitHub.

@dengemann

Copy link
Copy Markdown
Member

on the other hand side a submodule in fiff to mark this functionality / put it in an extra name space is probably a good idea. both could work together.

as an alternative we could call

  • mne/fiff/foreign
  • mne/fiff/devices
  • mne/fiff/support

but maybe mne/fiff/import would be fine

import. would then harbor submodules for each vendor / system type

but we would try as much as possible to use a similar aproach as in constants, write, tag, open, read, etc.

either by reproducing similar modules in the import name space or by adding to the existing ones,
in this case we would prepend the system names for disambiguation.
e.g. bti_read_double_matrix, kit_read_doublematrix, etc.

On 11.01.2013, at 10:47, Eric89GXL [email protected] wrote:

Incidentally @agramfort @dengemann, how do you think on starting a new, more generic mne/import.py or mne.fiff.import.py? I think it would be appropriate, given this PR and @dengemann's #248 on importing 4D MagnesWH3600 files.

—
Reply to this email directly or view it on GitHub.

@christianbrodbeck

Copy link
Copy Markdown
Member

@dengemann

so to be a bit more clear my proposal would be to integrate support for other systems with our fiff eco system as densely as possible.
this implies additiona functions and constants in the modules we currently use for managing fiff -- makes sense?

What would be the rationale for this? Wouldn't it be cleaner to have separate modules for separate file types?

About the location, what about mne.io.kit, or if it is actually mainly kit2fiff conversion, mne.convert.kit?

@dengemann

Copy link
Copy Markdown
Member

More or less. But what we will support is creating fiffs from other foreing
files, not writing or editing them. So it would make sense it have it
somewhere under /mne/fiff . submodules are fine, my idea was just to
organise the code consistently, if possible, e.g. creat Bunch objects for
storing constants, etc.

On Fri, Jan 11, 2013 at 3:01 PM, Christian Brodbeck <
[email protected]> wrote:

@dengemann https://github.com/dengemann

so to be a bit more clear my proposal would be to integrate support for
other systems with our fiff eco system as densely as possible.
this implies additiona functions and constants in the modules we currently
use for managing fiff -- makes sense?

What would be the rationale for this? Wouldn't it be cleaner to have
separate modules for separate file types?

About the location, what about mne.io.kit, or if it is actually mainly
kit2fiff conversion, mne.convert.kit?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/366#issuecomment-12145166.

@christianbrodbeck

Copy link
Copy Markdown
Member

my idea was just to
organise the code consistently, if possible, e.g. creat Bunch objects for
storing constants

Yes that makes sense as long as file formats are sufficiently similar

what we will support is creating fiffs from other foreing
files, not writing or editing them.

It just seems somewhat strange to have code for reading other file types under the fiff module, or is that just me? :)

@dengemann

Copy link
Copy Markdown
Member

I mean, we already have ctf.py, this is why I started having this idea. But
not too important. Another option would be to rename fiff to something more
general like io.

On Fri, Jan 11, 2013 at 3:35 PM, Christian Brodbeck <
[email protected]> wrote:

my idea was just to
organise the code consistently, if possible, e.g. creat Bunch objects for
storing constants

Yes that makes sense as long as file formats are sufficiently similar

what we will support is creating fiffs from other foreing
files, not writing or editing them.

It just seems somewhat strange to have code for reading other file types
under the fiff module, or is that just me? :)

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/366#issuecomment-12146419.

@larsoner

Copy link
Copy Markdown
Member

I think eventually we will rename fiff to something like io. And I didn't
realize we had ctf.py. I'd now like the idea of staying consistent and
doing fiff/whatever.py, like originally proposed by @dengemann, the logic
being the other formats are only dealt with as a conversion to fiff. And
you're right @dengemann, fiff/import.py is a terrible idea, must have been
tired when I thought of that one :)

If we do want a unified "fiff/convert.py" or whatever we could still do it,
and deprecate the use of ctf.py. I think this is my favorite option, since
it would mean fewer submodules, most of which would only contain one or two
functions.

@dengemann

Copy link
Copy Markdown
Member

@Eric89GXL yeayeah, glad I could convey the point.

On Fri, Jan 11, 2013 at 6:19 PM, Eric89GXL [email protected] wrote:

I think eventually we will rename fiff to something like io. And I didn't
realize we had ctf.py. I'd now like the idea of staying consistent and
doing fiff/whatever.py, like originally proposed by @dengemann, the logic
being the other formats are only dealt with as a conversion to fiff. And
you're right @dengemann, fiff/import.py is a terrible idea, must have been
tired when I thought of that one :)

If we do want a unified "fiff/convert.py" or whatever we could still do it,
and deprecate the use of ctf.py. I think this is my favorite option, since
it would mean fewer submodules, most of which would only contain one or two
functions.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/366#issuecomment-12153941.

@dengemann

Copy link
Copy Markdown
Member

Although the ctf.py --admittedly-- does not doe conversion, as I understood it, it's for offline compensation. Bu this shouldn't change too much, the point remains valid, I think. So let's go with fiff/whatever_system.py and try do distribute common tasks like reading, constants, etc. across the related fiff submodules and only keep the most idiosyncratic stuff in the related whatever.py modules. Any bin we might provide should import the functionality from there. Believe me, it will look more beautiful and less hakish.
Don't forget: http://www.kennethlange.com/steve_jobs_his_rule_for_spotting_true_craftsmanship.html

Comment thread mne/fiff/kit/kit.py Outdated

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.

These statements seem somewhat redundant :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Lol, yeah...true. You make a very strong argument.

Teon
Sent from an iPhone

On Feb 16, 2013, at 10:36, Christian Brodbeck [email protected]
wrote:

In mne/fiff/kit/kit.py:

  •    If not None, override default verbose level (see mne.verbose).
    
  • See Also

  • mne.fiff.Raw : Documentation of attribute and methods.
  • """
  • @verbose
  • def init(self, input_fname, mrk_fname, elp_fname, hsp_fname, sns_fname,
  •             stim, data=None, stimthresh=3.5, verbose=None):
    
  •    logger.info('Extracting SQD Parameters from %s...' % input_fname)
    
  •    sqd = _get_sqd_params(input_fname)
    
  •    mrk_fname = mrk_fname
    
  •    elp_fname = elp_fname
    
  •    hsp_fname = hsp_fname
    
  •    sns_fname = sns_fname
    

These statements seem somewhat redundant :)

—
Reply to this email directly or view it on
GitHubhttps://github.com//pull/366/files#r3039523.

@dengemann

Copy link
Copy Markdown
Member

I think that I will remove the data option. It isn't really useful. If one
would want to insert data into a fif object, they can make an instance of
the raw and do it there. This should be kept as just a converter.

That would make it more consistent with the other reader (BTi) and presumably less confusing. Data should come from an sqd file if it is a KIT, right?

ENH fit_matched_pts: add tolerance parameter
@dengemann

Copy link
Copy Markdown
Member

Since vstack creates a new array, it might be more memory efficient for _get_sqd_data() to create an array with an empty last row, so that you could do here self._data[-1,:] = stim_ch.

Would'nt that be a transient issue since the previous original self._data is left to the GC? Initializing with extra row makes sense if stim channel is always added.

@teonbrooks

Copy link
Copy Markdown
Member Author

I think that the stim channel is always created.

On Sat, Feb 16, 2013 at 11:32 AM, Denis A. Engemann <
[email protected]> wrote:

Since vstack creates a new array, it might be more memory efficient for
_get_sqd_data() to create an array with an empty last row, so that you
could do here self._data[-1,:] = stim_ch.

Would'nt that be a transient issue since the previous original self._data
is left to the GC? Initializing with extra row makes sense if stim channel
is always added.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/366#issuecomment-13656612.

@christianbrodbeck

Copy link
Copy Markdown
Member

Would'nt that be a transient issue since the previous original self._data is left to the GC?

Yes, the problem is that for large sqd files, python became slow and consumed large quantities of memory, so that might also be improved by having fewer large arrays at a time(?)

@christianbrodbeck

Copy link
Copy Markdown
Member

Any suggestions of preallocating space and reshape the extracted data with having multiple representations in memory?

If you know beforehand how many samples are going to be in the data, you can preallocate with data = np.emtpy((n_channels, n_samples), dtype=...)

Comment thread mne/fiff/kit/kit.py Outdated

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.

Also here you could use data *= conv_factor to avoid duplicating the array

@teonbrooks

Copy link
Copy Markdown
Member Author

If you know beforehand how many samples are going to be in the data, you
can preallocate with data = np.emtpy((n_channels, n_samples), dtype=...)

Would then say, data[:192,:] = np.fromfile(...), reshape to the allotted
dimensions?

@christianbrodbeck

Copy link
Copy Markdown
Member

Would then say, data[:192,:] = np.fromfile(...), reshape to the allotted
dimensions?

np.fromfile() still returns a separate array, so if you still experience performance issues you could try reading the sqd in chunks and filling the data array in a for loop.

@dengemann

Copy link
Copy Markdown
Member

On Sat, Feb 16, 2013 at 6:57 PM, Christian Brodbeck <
[email protected]> wrote:

Would then say, data[:192,:] = np.fromfile(...), reshape to the allotted
dimensions?

But this should really be fine. I used the np.fromfile().reshape() idiom fo
BTi and it works fine, even on my tiny Macbook Air.

np.fromfile() still returns a separate array, so if you still experience
performance issues you could try reading the sqd in chunks and filling the
data array in a for loop.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/366#issuecomment-13662570.

@christianbrodbeck

Copy link
Copy Markdown
Member

@dengemann I am not saying these are necessary changes, but @t3on mentioned performance issues with his files (which are quite big) so I am making suggestions on things that might help

@christianbrodbeck

Copy link
Copy Markdown
Member

My files are half his size so I don't personally care :P

@dengemann

Copy link
Copy Markdown
Member

My files are half his size so I don't personally care 📦

Btw. how big are the files creating problems, just out of curiosity. The files I use are often around 1GB.

@christianbrodbeck

Copy link
Copy Markdown
Member

Around 1 hour at 1000 Hz

@dengemann

Copy link
Copy Markdown
Member

That makes really big files, I suppose ;-)

@teonbrooks

Copy link
Copy Markdown
Member Author

Thanks @christianmbrodbeck , @dengemann . Those issues have now been addressed.

Comment thread mne/fiff/kit/coreg.py Outdated

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are you talking about the dumping of the values? I could say fid.seek(mrk_offset + KIT.INT + (2*KIT.DOUBLE*4**2)) to seek over those values.

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.

Yes, or with os.SEEK_CUR you can move by a relative offset. I am not sure, but I would assume it's more efficient than reading data you don't need.

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.

if you guys are happy with this PR I'll squash a few commits to remove the
large files from the history and merge.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

a-ok with me.

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.

Tests pass here, +1 for squash/merge

@agramfort

Copy link
Copy Markdown
Member

just spent 30mins trying to merge this branch by rebase but due to the previous merge with master it's a nightmare. @t3on would you mind reopening a new clean PR ? the idea is to have no diff between a new branch kit_clean so git diff tb-mne_kit2fiff..kit_clean empty but with a cleaner history (no large files and a linear history that makes things like git bisect doable)/. Thanks

@teonbrooks teonbrooks mentioned this pull request Feb 18, 2013
@teonbrooks

Copy link
Copy Markdown
Member Author

See #475.

@teonbrooks teonbrooks closed this Feb 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants