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 2eea3adcd1e2e6b3008a8fef53948f8e33c953cf Author: Alexandre Gramfort <[email protected]> Date: Wed Dec 29 22:26:21 2010 -0500 starting readind code with cov (not working yet!) --- examples/read_cov.py | 10 +- examples/read_forward.py | 2 +- examples/read_raw.py | 2 - fiff/__init__.py | 2 +- fiff/cov.py | 88 +++++++++++++++++ fiff/open.py | 3 +- fiff/proj.py | 37 ++++++++ fiff/write.py | 243 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 381 insertions(+), 6 deletions(-) diff --git a/examples/read_cov.py b/examples/read_cov.py index 30f2406..4b02e3d 100644 --- a/examples/read_cov.py +++ b/examples/read_cov.py @@ -7,9 +7,17 @@ import fiff fname = 'MNE-sample-data/MEG/sample/sample_audvis-cov.fif' fid, tree, _ = fiff.fiff_open(fname) - cov_type = 1 cov = fiff.read_cov(fid, tree, cov_type) +fid.close() + +fiff.write_cov_file('cov.fif', cov) + +fid, tree, _ = fiff.fiff_open('cov.fif') +cov2 = fiff.read_cov(fid, tree, cov_type) +fid.close() + +print np.linalg.norm(cov['data'] - cov2['data']) print "covariance matrix size: %s x %s" % cov['data'].shape diff --git a/examples/read_forward.py b/examples/read_forward.py index 28ad715..5312a93 100644 --- a/examples/read_forward.py +++ b/examples/read_forward.py @@ -4,7 +4,7 @@ print __doc__ import fiff -fname = 'MNE-sample-data/subjects/sample/bem/sample-5120-bem-sol.fif' +# fname = 'MNE-sample-data/MEG/sample/sample_audvis-ave-7-fwd.fif' fname = 'sm01a5-ave-oct-6-fwd.fif' data = fiff.read_forward_solution(fname) diff --git a/examples/read_raw.py b/examples/read_raw.py index 80666c9..79207d1 100644 --- a/examples/read_raw.py +++ b/examples/read_raw.py @@ -23,5 +23,3 @@ pl.plot(times, data.T) pl.xlabel('time (ms)') pl.ylabel('MEG data (T)') pl.show() - - diff --git a/fiff/__init__.py b/fiff/__init__.py index 10b382e..75a998c 100644 --- a/fiff/__init__.py +++ b/fiff/__init__.py @@ -3,7 +3,7 @@ __version__ = '0.1.git' from .constants import FIFF from .open import fiff_open from .evoked import read_evoked -from .cov import read_cov +from .cov import read_cov, write_cov, write_cov_file from .raw import setup_read_raw, read_raw_segment, read_raw_segment_times from .event import read_events from .forward import read_forward_solution diff --git a/fiff/cov.py b/fiff/cov.py index 3b6dabf..91534a9 100644 --- a/fiff/cov.py +++ b/fiff/cov.py @@ -1,3 +1,4 @@ +import os import numpy as np from .constants import FIFF @@ -109,3 +110,90 @@ def read_cov(fid, node, cov_kind): raise ValueError, 'Did not find the desired covariance matrix' return None + +############################################################################### +# Writing + +from .write import start_block, end_block, write_int, write_name_list, \ + write_double, write_float_matrix, start_file, end_file +from .proj import write_proj + +def write_cov(fid, cov): + """ + % + % + % mne_write_cov(fid,cov) + % + % Write a covariance matrix to an open file + % + % fid - an open file id + % cov - the covariance matrix to write + % + """ + + start_block(fid, FIFF.FIFFB_MNE_COV) + + # Dimensions etc. + write_int(fid, FIFF.FIFF_MNE_COV_KIND, cov['kind']) + write_int(fid, FIFF.FIFF_MNE_COV_DIM, cov['dim']) + if cov['nfree'] > 0: + write_int(fid, FIFF.FIFF_MNE_COV_NFREE, cov['nfree']); + + # Channel names + if cov['names'] is not None: + write_name_list(fid, FIFF.FIFF_MNE_ROW_NAMES, cov['names']) + + # Data + if cov['diag']: + write_double(fid, FIFF.FIFF_MNE_COV_DIAG, cov['data']); + else: + dim = cov['dim'] + vals = np.empty(dim*(dim + 1)/2) + # XXX : should be improved later + q = 0 + for j in range(dim): + for k in range(j): + vals[q] = cov['data'][j,k] + q = q + 1 + + write_double(fid, FIFF.FIFF_MNE_COV, vals) + + # Eigenvalues and vectors if present + if cov['eig'] is not None and cov['eigvec'] is not None: + write_float_matrix(fid, FIFF.FIFF_MNE_COV_EIGENVECTORS, cov['eigvec']) + write_double(fid, FIFF.FIFF_MNE_COV_EIGENVALUES, cov['eig']) + + # Projection operator + write_proj(fid, cov['projs']) + + # Bad channels + if cov['bads'] is not None: + start_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS) + write_name_list(fid, FIFF.FIFF_MNE_CH_NAME_LIST, cov['bads']) + end_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS) + + # Done! + end_block(fid, FIFF.FIFFB_MNE_COV) + + +def write_cov_file(fname, cov): + """ + % + % function mne_write_cov_file(name,cov) + % + % Write a complete fif file containing a covariance matrix + % + % fname filename + % cov the covariance matrix to write + % + % + """ + fid = start_file(fname) + + try: + write_cov(fid,cov); + except Exception as inst: + os.remove(fname) + raise '%s', inst + + end_file(fid) diff --git a/fiff/open.py b/fiff/open.py index a704419..0a5edc6 100644 --- a/fiff/open.py +++ b/fiff/open.py @@ -2,6 +2,7 @@ from .tag import read_tag_info, read_tag from .tree import make_dir_tree from .constants import FIFF + def fiff_open(fname, verbose=False): fid = open(fname, "rb") # Open in binary mode @@ -47,7 +48,7 @@ def fiff_open(fname, verbose=False): tree, _ = make_dir_tree(fid, directory) if verbose: - print '[done]\n' + print '[done]\n' # # Back to the beginning diff --git a/fiff/proj.py b/fiff/proj.py index 1cdfcb2..a531592 100644 --- a/fiff/proj.py +++ b/fiff/proj.py @@ -103,3 +103,40 @@ def read_proj(fid, node): misc) return projdata + +############################################################################### +# Write + +from .write import write_int, write_float, write_string, write_name_list, \ + write_float_matrix, end_block, start_block + +def write_proj(fid, projs): + """ + % + % fiff_write_proj(fid,projs) + % + % Writes the projection data into a fif file + % + % fid An open fif file descriptor + % projs The compensation data to write + % + """ + start_block(fid, FIFF.FIFFB_PROJ) + + for proj in projs: + start_block(fid, FIFF.FIFFB_PROJ_ITEM) + write_string(fid, FIFF.FIFF_NAME, proj['desc']) + write_int(fid, FIFF.FIFF_PROJ_ITEM_KIND, proj['kind']) + if proj['kind'] == FIFF.FIFFV_PROJ_ITEM_FIELD: + write_float(fid, FIFF.FIFF_PROJ_ITEM_TIME, 0.0) + + write_int(fid, FIFF.FIFF_NCHAN, proj['data']['ncol']) + write_int(fid, FIFF.FIFF_PROJ_ITEM_NVEC, proj['data']['nrow']) + write_int(fid, FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE, proj['active']) + write_name_list(fid, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST, + proj['data']['col_names']) + write_float_matrix(fid, FIFF.FIFF_PROJ_ITEM_VECTORS, + proj['data']['data']) + end_block(fid,FIFF.FIFFB_PROJ_ITEM) + + end_block(fid, FIFF.FIFFB_PROJ) diff --git a/fiff/write.py b/fiff/write.py new file mode 100644 index 0000000..e7e55c9 --- /dev/null +++ b/fiff/write.py @@ -0,0 +1,243 @@ +import time +import numpy as np + +from .constants import FIFF + + +def _write(fid, data, kind, data_size, FIFFT_TYPE, dtype): + FIFFV_NEXT_SEQ = 0 + if isinstance(data, np.ndarray): + data_size *= data.size + fid.write(np.array(kind, dtype='>i4').tostring()) + fid.write(np.array(FIFFT_TYPE, dtype='>i4').tostring()) + fid.write(np.array(data_size, dtype='>i4').tostring()) + fid.write(np.array(FIFFV_NEXT_SEQ, dtype='>i4').tostring()) + fid.write(np.array(data, dtype=dtype).tostring()) + + +def write_int(fid, kind, data): + """ + % + % fiff_write_int(fid,kind,data) + % + % Writes a 32-bit integer tag to a fif file + % + % fid An open fif file descriptor + % kind Tag kind + % data The integers to use as data + % + """ + FIFFT_INT = 3 + data_size = 4 + _write(fid, data, kind, data_size, FIFFT_INT, '>i4') + + +def write_double(fid, kind, data): + """ + % + % fiff_write_int(fid,kind,data) + % + % Writes a double-precision floating point tag to a fif file + % + % fid An open fif file descriptor + % kind Tag kind + % data The data + % + """ + FIFFT_DOUBLE = 5 + data_size = 8 + _write(fid, data, kind, data_size, FIFFT_DOUBLE, '>f8') + + +def write_float(fid, kind, data): + """ + % + % fiff_write_float(fid,kind,data) + % + % Writes a single-precision floating point tag to a fif file + % + % fid An open fif file descriptor + % kind Tag kind + % data The data + % + """ + FIFFT_FLOAT = 4 + data_size = 4 + _write(fid, data, kind, data_size, FIFFT_FLOAT, '>f4') + + +def write_string(fid, kind, data): + """ + % + % fiff_write_string(fid,kind,data) + % + % Writes a string tag + % + % fid An open fif file descriptor + % kind The tag kind + % data The string data to write + % + """ + FIFFT_STRING = 10 + data_size = len(data) + _write(fid, data, kind, data_size, FIFFT_STRING, '>c') + + +def write_name_list(fid, kind, data): + """ + % + % fiff_write_name_list(fid,kind,mat) + % + % Writes a colon-separated list of names + % + % fid An open fif file descriptor + % kind The tag kind + % data An array of names to create the list from + % + """ + write_string(fid, kind, ':'.join(data)) + + +def write_float_matrix(fid, kind, data): + """ + % + % fiff_write_float_matrix(fid,kind,mat) + % + % Writes a single-precision floating-point matrix tag + % + % fid An open fif file descriptor + % kind The tag kind + % data The data matrix + % + """ + + FIFFT_FLOAT = 4 + FIFFT_MATRIX = 1 << 30 + FIFFT_MATRIX_FLOAT = FIFFT_FLOAT | FIFFT_MATRIX + data_size = 4*data.size + 4*3 + + _write(fid, data, kind, data_size, FIFFT_MATRIX_FLOAT, '>f4') + + dims = np.empty(3, dtype=np.int) + dims[0] = data.shape[1] + dims[1] = data.shape[0] + dims[3] = 2 + fid.write(np.array(dims, dtype='>i4').tostring()) + + +def write_id(fid, kind, id_=None): + """ + % + % fiff_write_id(fid, kind, id) + % + % Writes fiff id + % + % fid An open fif file descriptor + % kind The tag kind + % id The id to write + % + % If the id argument is missing it will be generated here + % + """ + + if id_ is None: + id_ = dict() + id_['version'] = (1 << 16) | 2 # Version (1 << 16) | 2 + id_['machid'] = 65536 * np.random.rand(2) # Machine id (andom for now) + id_['secs'] = time.time() + id_['usecs'] = 0 # Do not know how we could get this XXX + + FIFFT_ID_STRUCT = 31 + FIFFV_NEXT_SEQ = 0 + + data_size = 5*4 # The id comprises five integers + fid.write(np.array(kind, dtype='>i4').tostring()) + fid.write(np.array(FIFFT_ID_STRUCT, dtype='>i4').tostring()) + fid.write(np.array(data_size, dtype='>i4').tostring()) + fid.write(np.array(FIFFV_NEXT_SEQ, dtype='>i4').tostring()) + + # Collect the bits together for one write + data = np.empty(5, dtype=np.int32) + data[0] = id_['version'] + data[1] = id_['machid'][0] + data[2] = id_['machid'][1] + data[3] = id_['secs'] + data[4] = id_['usecs'] + fid.write(np.array(data, dtype='>i4').tostring()) + + +def start_block(fid, kind): + """ + % + % fiff_start_block(fid,kind) + % + % Writes a FIFF_BLOCK_START tag + % + % fid An open fif file descriptor + % kind The block kind to start + % + """ + + FIFF_BLOCK_START = 104 + write_int(fid, FIFF_BLOCK_START, kind) + + +def end_block(fid, kind): + """ + % + % fiff_end_block(fid,kind) + % + % Writes a FIFF_BLOCK_END tag + % + % fid An open fif file descriptor + % kind The block kind to end + % + """ + + FIFF_BLOCK_END = 105 + write_int(fid, FIFF_BLOCK_END, kind) + + +def start_file(name): + """ + % + % [fid] = fiff_start_file(name) + % + % Opens a fif file for writing and writes the compulsory header tags + % + % name The name of the file to open. It is recommended + % that the name ends with .fif + % + """ + fid = open(name, 'wb') + + # Write the compulsory items + FIFF_FILE_ID = 100 + FIFF_DIR_POINTER = 101 + FIFF_FREE_LIST = 106 + + write_id(fid, FIFF_FILE_ID) + write_int(fid, FIFF_DIR_POINTER, -1) + write_int(fid, FIFF_FREE_LIST, -1) + + return fid + + +def end_file(fid): + """ + % + % fiff_end_file(fid) + % + % Writes the closing tags to a fif file and closes the file + % + % fid An open fif file descriptor + % + """ + + data_size = 0 + + fid.write(np.array(FIFF.FIFF_NOP, dtype='>i4').tostring()) + fid.write(np.array(FIFF.FIFFT_VOID, dtype='>i4').tostring()) + fid.write(np.array(data_size, dtype='>i4').tostring()) + fid.write(np.array(FIFF.FIFFV_NEXT_NONE, dtype='>i4').tostring()) + fid.close() -- 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
