Last active
December 13, 2015 20:18
-
-
Save dengemann/4968794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Denis A. Engemann <d.engemann@fz-juelich.de> | |
""" Export Magnes3600WH weight tables for your study | |
If you want to include reference channels (might be a good idea), | |
currently, the way to go is to use the following mne command | |
to create the compensation data: | |
$ mne_create_comp_data | |
usage: mne_create_comp_data [options] | |
This program takes a text format compensation data matrix and converts it into fif. | |
--in name The input file. | |
--kind value Then compensation kind to assign (default = 101) | |
--out name The output file. | |
--help print this info. | |
--version print version info. | |
mne_create_comp_data -in my_weigths.txt -out my_weigths.fif | |
And this mne command to add it to the exported fiff: | |
$ mne_add_to_meas_info | |
usage: mne_add_to_meas_info [options] | |
Add new data to meas info. | |
--add name The file to add. | |
--dest name The destination file. | |
--help print this info. | |
--version print version info. | |
This example demonstrates how to get the weight tables out of the Solaris DAW. | |
You can edit + save this example and run it using python which is installed | |
on megsun02. Alternatively you can past it into the python interpreter, | |
block by block. This will allow you to play with this stuff. | |
CAVE: | |
This script assumes you've already exported you data to the path bound to the | |
variable destination and that you organized it in a 'path/subject_name' style. | |
""" | |
import os.path as op | |
from commands import getstatusoutput | |
study_name = 'SOMO' # adjust this accordingly | |
find = 'find . | grep %s' % study_name | |
destination = '/export/raid_meg1/exp' # set your path here | |
res, hits = getstatusoutput(find) # system command find | |
if res > 0: # break if sth goes wrong. | |
raise RuntimeError(hits) | |
# get the top level study folders for each subject | |
targets = [h for h in hits.split() if 'config' in h.split('/')[-1]] | |
# prepare Eberhard's command and insert open string formatting slots | |
print_tables = 'print_table -P %s -S %s -s %r -r %s -WP DC:supine:both' | |
exclude = '203511', '203373', '204652' # my subjects to be excluded | |
""" | |
The following renaming operation addresses 2 issues. | |
The current mne-python exporter renames the ref channels because I wrote it so. | |
Integrating the weights however requires all ref channels can be linked to | |
MEG channels. For this reason, on printing the tables I've got to | |
rename them such that they match the names in the exported fiff. | |
""" | |
REF_CH = ['GxxA', 'GyxA', 'GyyA', 'GzxA', 'GzyA', 'MCxA', 'MCxaA', 'MCyA', | |
'MCyaA', 'MCzA', 'MCzaA', 'MLxA', 'MLxaA', 'MLyA', 'MLyaA', 'MLzA', | |
'MLzaA', 'MRxA', 'MRxaA', 'MRyA', 'MRyaA', 'MRzA', 'MRzaA'] | |
NEW_CH = ['RFG%03d' % ii for ii, ch in enumerate(REF_CH[:5], 1)] | |
NEW_CH += ['RFM%03d' % ii for ii, ch in enumerate(REF_CH[5:], 1)] | |
CH_SEP = '\t ' # channel names are separated like this. | |
ch_map = dict(zip(REF_CH, NEW_CH)) # create a mapping between old : new | |
def rename(line): | |
""" This will be fed with one line from the weight tables | |
""" | |
# this is mandatory, the C routine expects integers. | |
if any(k in line for k in ('NumSignalChans', 'NumReferenceChans')): | |
line = ''.join(ch for ch in line if ch.isdigit()) | |
# this is a fix, related to the renaming. For future vesions | |
# of the exporter this block might be dropped. | |
elif any(k in line for k in REF_CH): | |
line = CH_SEP.join(ch_map.get(k, '') for k in line.split(CH_SEP)) | |
# also this is mandatory | |
elif line.startswith('A'): | |
line = line.split('\t') | |
line.insert(0, 'MEG%03d' % int(line[0].strip('A'))) | |
line.pop(1) | |
line = '\t'.join(line) | |
return line | |
def match(line): | |
""" This is used to drop lines that aren't expected by the C routine""" | |
matches = [] | |
if line.startswith('A'): | |
matches += [1] | |
elif any(k in line for k in REF_CH): | |
matches += [1] | |
elif any(k in line for k in ('NumSignalChans', 'NumReferenceChans')): | |
matches += [1] | |
return any(matches) | |
def rename_weights(weights): | |
"""This is the renaming function, this will stay for a while""" | |
return '\n'.join(rename(li) for li in weights.split('\n') if match(li)) | |
# let's get our hands dirty and do what we need to do.. | |
for conf in targets: | |
P, S, scan, run = conf.split('/')[1:-1] # get -P -S -s -r | |
scan = scan.replace('@', ' ') # required to make Eberhard's program happy. | |
if P not in exclude: # only process subjects to be included | |
fname = 'weigths_%s_%s_run%s.txt' % (P, scan, run) # compile file name | |
cmd = print_tables % (P, S, scan, run) # compile command | |
res, output = getstatusoutput(cmd) # run command | |
if res == 0: | |
weights = rename_weights(output) | |
with open(op.join(destination, S, P, fname), 'w') as fi: | |
fi.write(weights) | |
else: | |
print res | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment