Dig Points
Hi everyone,
I had a question about raw.info['dig']. Is it a reason to not store the headshape digitizer points as a single array in a dictionary instead of a separate dictionary for each point? It seems it would be more efficient to have them stored together. For our laser digitization, we have 300k points and I feel that the current way might not be the best solution.
Hi Teon,
good question. I think it should be possible to have a npoints x 5 array with the frame info first and the positions following for each row. However the question is whether it also can be stored like that in an MNE C command compatible fashion. We should look that up and see how the related block is written to a fiff file.
On 04.02.2013, at 06:57, Teon [email protected] wrote:
Hi everyone,
I had a question about raw.info['dig']. Is it a reason to not store the headshape digitizer points as a single array in a dictionary instead of a separate dictionary for each point? It seems it would be more efficient to have them stored together. For our laser digitization, we have 300k points and I feel that the current way might not be the best solution.
— Reply to this email directly or view it on GitHub.
You just need to write each point separately into the fif file. I do not think it matters how you represent the points in MNE-Python.
- Matti
On Feb 4, 2013, at 3:10 AM, Denis A. Engemann wrote:
Hi Teon,
good question. I think it should be possible to have a npoints x 5 array with the frame info first and the positions following for each row. However the question is whether it also can be stored like that in an MNE C command compatible fashion. We should look that up and see how the related block is written to a fiff file.
On 04.02.2013, at 06:57, Teon [email protected] wrote:
Hi everyone,
I had a question about raw.info['dig']. Is it a reason to not store the headshape digitizer points as a single array in a dictionary instead of a separate dictionary for each point? It seems it would be more efficient to have them stored together. For our laser digitization, we have 300k points and I feel that the current way might not be the best solution.
— Reply to this email directly or view it on GitHub.
— Reply to this email directly or view it on GitHub.
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Like @mshamalainen, each point is separate in the fif file, and this is the reason why it was also implemented that way in mne-python (we just read each point fif-tag and add it to info['dig']).
If this is a problem with many points we could change it. For the file with 300k points, did you have any problems other than efficiency? I assume storing the points separately needs a few MB additional memory.
it it's a really computational bottle neck we could store the dig in python as structure of arrays rather than an array of structures
see e.g.
http://stackoverflow.com/questions/8377667/layout-in-memory-of-a-struct-struct-of-arrays-and-array-of-structs-in-c-c
but first I'd like to make sure it's a really bottleneck.
can you give us some timings?
This code:
import resource
import numpy as np
import time
m = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
info = dict(dig=[])
t = time.time()
for ii in xrange(300000):
info['dig'].append(dict(kind=1, ident=1,
r=np.empty(3, dtype='float32'),
coord_frame=4))
t = time.time() - t
print (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - m) / 1024
t2 = time.time()
coords = np.array([ii['r'] for ii in info['dig']])
coord_frames = np.array([ii['coord_frame'] for ii in info['dig']])
idents = np.array([ii['ident'] for ii in info['dig']])
kinds = np.array([ii['kind'] for ii in info['dig']])
t2 = time.time() - t2
print (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - m) / 1024
print t
print t2
Yielded:
66
78
0.591195821762
0.276946783066
In other words, storing them in a dict took about 66 MB, and creating those dicts took about 0.6 seconds. Taking the values out of the dicts and storing them in numpy arrays took about 12 MB, and extracting those took about 0.27 seconds. So our current method is not as memory efficient as it could be, but I wouldn't expect too large performance deficits. On the other hand, it shouldn't be too bad to store the data in arrays, but allow for dictionary-like access via a generator, I don't think. (I've never made a generator, but I think this is the sort of thing it would be useful for.) I can give it a shot if you think it would be worthwhile @agramfort...
I gain less than a second of computation with 1 million dig points. Is it really worth it?
I'd like to have @t3on feedback on usability issues.
@agramfort I may have used the word efficient too liberally. Although there are differences in the storage and the calculation of the dig points where there is just a decrease with storage as an array than separate dicts, my concern relates mostly with the readability. It would make interacting with and extracting the points easier with they were stored together. This would really only be done for plotting the headshape. I wasn't sure of the history their fif representation.
If it is cumbersome to change its representation or if there is no need otherwise, then I can see that there is no real need for it.
@t3on could you clarify what you'd like to be able to do more easily? get the location of all the dig points for plotting? apply a transformation? if so maybe a function/method could do the job.
I was thinking the same.
raw.get_digpoints()
returns : ndarray
On 04.02.2013, at 21:35, Alexandre Gramfort [email protected] wrote:
@t3on could you clarify what you'd like to be able to do more easily? get the location of all the dig points for plotting? apply a transformation? if so maybe a function/method could do the job. — Reply to this email directly or view it on GitHub.
Yes, @t3on it would be fairly trivial for us to add a function to return the N x 3 matrix of positions. We could do the following function (and then wrap a call to raw.get_digitization() for convenience):
def get_digitization(info):
"""Get digitization information from info
Parameters
----------
info : dict
The measurement info.
Returns
--------
coordinates : array
The Nx3 array of digitization positions.
kinds : array
The length-N array of kinds. Typically from mne.fiff.FIFFV_COORD_*.
idents : array
The length-N array of digitization identifiers.
coord_frames : array
The coordinate frame references.
```
coordinates = np.array([ii['r'] for ii in info['dig']])
kinds = np.array([ii['kind'] for ii in info['dig']])
idents = np.array([ii['ident'] for ii in info['dig']])
coord_frames = np.array([ii['coord_frame'] for ii in info['dig']])
return coordinates, kinds, idents, coord_frames
Would this solve your use case?
Sorry, I should clarify. I was initially interested in the way the digitizer points were represented in the raw.info['dig']. For my conversion tool, I didn't know the reason behind not being able to store all the digitizer points in an array in one dictionary instead of as separate entries. @mshamalainen informed me that it is because each point needs to be written to the fif separately and @mluessi says that because of that, mne-python implemented the same way.
I don't think that the method is necessary. It can definitely be handled outside of mne-python.
Thanks everybody so much for the help and discussion.