Skip to content

Commit

Permalink
FIX: Fixes for TRIUX and MEG ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed May 13, 2016
1 parent 8f7d9e5 commit e9cf773
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 125 deletions.
3 changes: 3 additions & 0 deletions mne/chpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ def read_head_pos(fname):
_check_fname(fname, must_exist=True, overwrite=True)
data = np.loadtxt(fname, skiprows=1) # first line is header, skip it
data.shape = (-1, 10) # ensure it's the right size even if empty
if np.isnan(data).any(): # make sure we didn't do something dumb
raise RuntimeError('positions could not be read properly from %s'
% fname)
return data


Expand Down
4 changes: 2 additions & 2 deletions mne/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _data_path(path=None, force_update=False, update_path=True, download=True,
path = _get_path(path, key, name)
# To update the testing or misc dataset, push commits, then make a new
# release on GitHub. Then update the "releases" variable:
releases = dict(testing='0.19', misc='0.1')
releases = dict(testing='0.20', misc='0.1')
# And also update the "hashes['testing']" variable below.

# To update any other dataset, update the data archive itself (upload
Expand Down Expand Up @@ -211,7 +211,7 @@ def _data_path(path=None, force_update=False, update_path=True, download=True,
sample='1d5da3a809fded1ef5734444ab5bf857',
somato='f3e3a8441477bb5bacae1d0c6e0964fb',
spm='f61041e3f3f2ba0def8a2ca71592cc41',
testing='77b2a435d80adb23cbe7e19144e7bc47'
testing='625cb9c1c62de03bd2bc6a3ca9a6f2d8'
)
folder_origs = dict( # not listed means None
misc='mne-misc-data-%s' % releases['misc'],
Expand Down
84 changes: 84 additions & 0 deletions mne/preprocessing/_fine_cal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
# Authors: Eric Larson <larson.eric.d@gmail.com>

# License: BSD (3-clause)

import numpy as np

from ..utils import check_fname, _check_fname


def read_fine_calibration(fname):
"""Read fine calibration information from a .dat file
The fine calibration typically includes improved sensor locations,
calibration coefficients, and gradiometer imbalance information.
Parameters
----------
fname : str
The filename.
Returns
-------
calibration : dict
Fine calibration information.
"""
# Read new sensor locations
_check_fname(fname, overwrite=True, must_exist=True)
check_fname(fname, 'cal', ('.dat',))
ch_names = list()
locs = list()
imb_cals = list()
with open(fname, 'r') as fid:
for line in fid:
if line[0] in '#\n':
continue
vals = line.strip().split()
if len(vals) not in [14, 16]:
raise RuntimeError('Error parsing fine calibration file, '
'should have 14 or 16 entries per line '
'but found %s on line:\n%s'
% (len(vals), line))
# `vals` contains channel number
ch_name = vals[0]
if len(ch_name) in (3, 4): # heuristic for Neuromag fix
try:
ch_name = int(ch_name)
except ValueError: # something other than e.g. 113 or 2642
pass
else:
ch_name = 'MEG' + '%04d' % ch_name
ch_names.append(ch_name)
# (x, y, z), x-norm 3-vec, y-norm 3-vec, z-norm 3-vec
locs.append(np.array([float(x) for x in vals[1:13]]))
# and 1 or 3 imbalance terms
imb_cals.append([float(x) for x in vals[13:]])
locs = np.array(locs)
return dict(ch_names=ch_names, locs=locs, imb_cals=imb_cals)


def write_fine_calibration(fname, calibration):
"""Write fine calibration information to a .dat file
Parameters
----------
fname : str
The filename to write out.
calibration : dict
Fine calibration information.
"""
_check_fname(fname, overwrite=True)
check_fname(fname, 'cal', ('.dat',))

with open(fname, 'wb') as cal_file:
for ci, chan in enumerate(calibration['ch_names']):
# Write string containing 1) channel, 2) loc info, 3) calib info
# with field widths (e.g., %.6f) chosen to match how Elekta writes
# them out
cal_line = np.concatenate([calibration['locs'][ci],
calibration['imb_cals'][ci]]).round(6)
cal_str = str(chan) + ' ' + ' '.join(map(lambda x: "%.6f" % x,
cal_line))

cal_file.write((cal_str + '\n').encode('ASCII'))
Loading

0 comments on commit e9cf773

Please sign in to comment.