This is an automated email from the git hooks/post-receive script. yoh pushed a commit to annotated tag v0.1 in repository python-mne.
commit f898d94770b02ae5afd0e74ba8546478c50243e4 Author: Alexandre Gramfort <[email protected]> Date: Thu Jan 27 10:56:57 2011 -0500 a bit of reorganization + pep8 --- mne/compensator.py | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++ mne/fiff/ctf.py | 6 +- mne/fiff/meas_info.py | 16 ----- mne/fiff/proj.py | 1 + mne/fiff/raw.py | 115 -------------------------------- 5 files changed, 182 insertions(+), 134 deletions(-) diff --git a/mne/compensator.py b/mne/compensator.py new file mode 100644 index 0000000..8290f73 --- /dev/null +++ b/mne/compensator.py @@ -0,0 +1,178 @@ +import numpy as np + +from .constants import FIFF + + +def get_current_comp(info): + """Get the current compensation in effect in the data + """ + comp = 0 + first_comp = -1 + for k, chan in enumerate(info['chs']): + if chan.kind == FIFF.FIFFV_MEG_CH: + comp = int(chan['coil_type']) >> 16 + if first_comp < 0: + first_comp = comp + elif comp != first_comp: + raise ValueError, ('Compensation is not set equally on ' + 'all MEG channels') + + return comp + + +def findall(L, value, start=0): + """Returns indices of all occurence of value in list L starting from start + """ + c = L.count(value) + if c == 0: + return list() + else: + ind = list() + i = start-1 + for _ in range(c): + i = L.index(value, i+1) + ind.append(i) + return ind + + +def _make_compensator(info, kind): + """Auxiliary function for make_compensator + """ + for k in range(len(info['comps'])): + if info['comps'][k]['kind'] == kind: + this_data = info['comps'][k]['data'] + + # Create the preselector + presel = np.zeros((this_data['ncol'], info['nchan'])) + for col, col_name in enumerate(this_data['col_names']): + ind = findall(info['ch_names'], col_name) + if len(ind) == 0: + raise ValueError, 'Channel %s is not available in data' % \ + col_name + elif len(ind) > 1: + raise ValueError, 'Ambiguous channel %s' % col_name + presel[col, ind] = 1.0 + + # Create the postselector + postsel = np.zeros((info['nchan'], this_data['nrow'])) + for c, ch_name in enumerate(info['ch_names']): + ind = findall(this_data['row_names'], ch_name) + if len(ind) > 1: + raise ValueError, 'Ambiguous channel %s' % ch_name + elif len(ind) == 1: + postsel[c, ind] = 1.0 + + this_comp = np.dot(postsel, np.dot(this_data['data'], presel)) + return this_comp + + return [] + + +def make_compensator(info, from_, to, exclude_comp_chs=False): + """ XXX : bug !!! 2 make_compensator functions + % + % [comp] = mne_make_compensator(info,from,to,exclude_comp_chs) + % + % info - measurement info as returned by the fif reading routines + % from - compensation in the input data + % to - desired compensation in the output + % exclude_comp_chs - exclude compensation channels from the output (optional) + % + + % + % Create a compensation matrix to bring the data from one compensation + % state to another + % + """ + + if from_ == to: + comp = np.zeros((info['nchan'], info['nchan'])) + return comp + + if from_ == 0: + C1 = np.zeros((info['nchan'], info['nchan'])) + else: + try: + C1 = _make_compensator(info, from_) + except Exception as inst: + raise ValueError, 'Cannot create compensator C1 (%s)' % inst + + if len(C1) == 0: + raise ValueError, ('Desired compensation matrix (kind = %d) not' + ' found' % from_) + + + if to == 0: + C2 = np.zeros((info['nchan'], info['nchan'])) + else: + try: + C2 = _make_compensator(info, to) + except Exception as inst: + raise ValueError, 'Cannot create compensator C2 (%s)' % inst + + if len(C2) == 0: + raise ValueError, ('Desired compensation matrix (kind = %d) not ' + 'found' % to) + + + # s_orig = s_from + C1*s_from = (I + C1)*s_from + # s_to = s_orig - C2*s_orig = (I - C2)*s_orig + # s_to = (I - C2)*(I + C1)*s_from = (I + C1 - C2 - C2*C1)*s_from + comp = np.eye(info['nchan']) + C1 - C2 - C2*C1 + + if exclude_comp_chs: + pick = np.zeros((info['nchan'], info['nchan'])) + npick = 0 + for k, chan in info['chs']: + if chan['kind'] != FIFF.FIFFV_REF_MEG_CH: + npick += 1 + pick[npick] = k + + if npick == 0: + raise ValueError, ('Nothing remains after excluding the ' + 'compensation channels') + + comp = comp[pick[1:npick], :] + + return comp + + +def compensate_to(data, to): + """ + % + % [newdata] = mne_compensate_to(data,to) + % + % Apply compensation to the data as desired + % + """ + + newdata = data.copy() + now = get_current_comp(newdata['info']) + + # Are we there already? + if now == to: + print 'Data are already compensated as desired' + + # Make the compensator and apply it to all data sets + comp = make_compensator(newdata['info'], now, to) + for k in range(len(newdata['evoked'])): + newdata['evoked'][k]['epochs'] = np.dot(comp, + newdata['evoked'][k]['epochs']) + + # Update the compensation info in the channel descriptors + newdata['info']['chs'] = set_current_comp(newdata['info']['chs'], to) + return newdata + + +def set_current_comp(chs, value): + """Set the current compensation value in the channel info structures + """ + new_chs = chs + + lower_half = int('FFFF', 16) # hex2dec('FFFF') + for k in range(len(chs)): + if chs[k].kind == FIFF.FIFFV_MEG_CH: + coil_type = float(chs[k]['coil_type']) & lower_half + new_chs[k]['coil_type'] = int(coil_type | (value << 16)) + + return new_chs diff --git a/mne/fiff/ctf.py b/mne/fiff/ctf.py index 7111a90..dcf358c 100644 --- a/mne/fiff/ctf.py +++ b/mne/fiff/ctf.py @@ -125,11 +125,11 @@ def read_ctf_comp(fid, node, chs): one = dict(ctfkind=tag.data, kind=-1) del tag - if one.ctfkind == hex2dec('47314252'): + if one.ctfkind == int('47314252', 16): # hex2dec('47314252'): one.kind = 1 - elif one.ctfkind == hex2dec('47324252'): + elif one.ctfkind == int('47324252', 16): # hex2dec('47324252'): one.kind = 2 - elif one.ctfkind == hex2dec('47334252'): + elif one.ctfkind == int('47334252', 16): # hex2dec('47334252'): one.kind = 3 else: one.kind = one.ctfkind diff --git a/mne/fiff/meas_info.py b/mne/fiff/meas_info.py index 2501125..8e1f936 100644 --- a/mne/fiff/meas_info.py +++ b/mne/fiff/meas_info.py @@ -257,19 +257,3 @@ def read_meas_info(source, tree=None): return info, meas - -def get_current_comp(info): - """Get the current compensation in effect in the data - """ - comp = 0; - first_comp = -1; - for k, chan in enumerate(info['chs']): - if chan.kind == FIFF.FIFFV_MEG_CH: - comp = int(chan['coil_type']) >> 16 - if first_comp < 0: - first_comp = comp; - elif comp != first_comp: - raise ValueError, ('Compensation is not set equally on ' - 'all MEG channels') - - return comp diff --git a/mne/fiff/proj.py b/mne/fiff/proj.py index 8a5a6c2..b3b7685 100644 --- a/mne/fiff/proj.py +++ b/mne/fiff/proj.py @@ -263,3 +263,4 @@ def make_projector_info(info): """ proj, nproj, _ = make_projector(info['projs'], info['ch_names'], info['bads']) return proj, nproj + diff --git a/mne/fiff/raw.py b/mne/fiff/raw.py index 6d8a0f9..f90cfd2 100644 --- a/mne/fiff/raw.py +++ b/mne/fiff/raw.py @@ -566,118 +566,3 @@ def finish_writing_raw(fid): end_block(fid, FIFF.FIFFB_MEAS) end_file(fid) -############################################################################### -# misc - -def findall(L, value, start=0): - """Returns indices of all occurence of value in list L starting from start - """ - c = L.count(value) - if c == 0: - return list() - else: - ind = list() - i = start-1 - for _ in range(c): - i = L.index(value, i+1) - ind.append(i) - return ind - - -def _make_compensator(info, kind): - """Auxiliary function for make_compensator - """ - for k in range(len(info['comps'])): - if info['comps'][k]['kind'] == kind: - this_data = info['comps'][k]['data']; - - # Create the preselector - presel = np.zeros((this_data['ncol'], info['nchan'])) - for col, col_name in enumerate(this_data['col_names']): - ind = findall(info['ch_names'], col_name) - if len(ind) == 0: - raise ValueError, 'Channel %s is not available in data' % \ - col_name - elif len(ind) > 1: - raise ValueError, 'Ambiguous channel %s' % col_name - presel[col, ind] = 1.0 - - # Create the postselector - postsel = np.zeros((info['nchan'], this_data['nrow'])) - for c, ch_name in enumerate(info['ch_names']): - ind = findall(this_data['row_names'], ch_name) - if len(ind) > 1: - raise ValueError, 'Ambiguous channel %s' % ch_name - elif len(ind) == 1: - postsel[c, ind] = 1.0 - - this_comp = postsel*this_data['data']*presel; - return this_comp - - return [] - - -def make_compensator(info, from_, to, exclude_comp_chs=False): - """ XXX : bug !!! 2 make_compensator functions - % - % [comp] = mne_make_compensator(info,from,to,exclude_comp_chs) - % - % info - measurement info as returned by the fif reading routines - % from - compensation in the input data - % to - desired compensation in the output - % exclude_comp_chs - exclude compensation channels from the output (optional) - % - - % - % Create a compensation matrix to bring the data from one compensation - % state to another - % - """ - - if from_ == to: - comp = np.zeros((info['nchan'], info['nchan'])) - return comp - - if from_ == 0: - C1 = np.zeros((info['nchan'], info['nchan'])) - else: - try: - C1 = _make_compensator(info, from_) - except Exception as inst: - raise ValueError, 'Cannot create compensator C1 (%s)' % inst - - if len(C1) == 0: - raise ValueError, 'Desired compensation matrix (kind = %d) not found' % from_ - - - if to == 0: - C2 = np.zeros((info['nchan'], info['nchan'])) - else: - try: - C2 = _make_compensator(info, to) - except Exception as inst: - raise ValueError, 'Cannot create compensator C2 (%s)' % inst - - if len(C2) == 0: - raise ValueError, 'Desired compensation matrix (kind = %d) not found' % to - - - # s_orig = s_from + C1*s_from = (I + C1)*s_from - # s_to = s_orig - C2*s_orig = (I - C2)*s_orig - # s_to = (I - C2)*(I + C1)*s_from = (I + C1 - C2 - C2*C1)*s_from - comp = np.eye(info['nchan']) + C1 - C2 - C2*C1; - - if exclude_comp_chs: - pick = np.zeros((info['nchan'], info['nchan'])) - npick = 0 - for k, chan in info['chs']: - if chan['kind'] != FIFF.FIFFV_REF_MEG_CH: - npick += 1 - pick[npick] = k - - if npick == 0: - raise ValueError, 'Nothing remains after excluding the compensation channels' - - comp = comp[pick[1:npick], :] - - return comp -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
