예제 #1
0
def print_nix(nixfname):
    nf = nix.File(nixfname, mode=nix.FileMode.ReadOnly)
    print("::   DATA   ::")
    print_nix_data(nf)
    print(":: METADATA ::")
    print_md(nf)
    nf.close()
예제 #2
0
def write_raw_mne(nfname,
                  mneraw,
                  split_data_channels=False,
                  split_stimuli=False):
    """
    Writes the provided Raw MNE structure to a NIX file with the given name.

    :param nfname: Name for the NIX file to write to. Existing file will be
    overwritten.
    :param mneraw: An MNE Raw structure (any mne.io.BaseRaw subclass).
    :param split_data_channels: If True, each raw data channel will be stored
    in a separate DataArray.
    :param split_stimuli: If True, stimuli will be split into separate
    MultiTags based on the stimulus type (label).
    :rtype: None
    """
    mneinfo = mneraw.info
    extrainfo = mneraw._raw_extras

    # Create NIX file
    print(nfname)
    print(nix.FileMode.Overwrite)
    nf = nix.File(nfname, nix.FileMode.Overwrite)

    # Write Data to NIX
    block = nf.create_block(DATA_BLOCK_NAME,
                            DATA_BLOCK_TYPE,
                            compression=nix.Compression.DeflateNormal)
    block.create_group(RAW_DATA_GROUP_NAME, RAW_DATA_GROUP_TYPE)

    if split_data_channels:
        write_multi_da(mneraw, block)
    else:
        write_single_da(mneraw, block)

    if mneraw.annotations:
        write_stim_tags(mneraw, block, split_stimuli)

    # Write metadata to NIX
    # info dictionary
    infomd = nf.create_section("Info", "File metadata")
    create_md_tree(infomd, mneinfo, block)
    # extras
    if len(extrainfo) > 1:
        for idx, emd_i in enumerate(extrainfo):
            extrasmd = nf.create_section(f"Extras-{idx}",
                                         "Raw Extras metadata")
            create_md_tree(extrasmd, emd_i, block)
    elif extrainfo:
        extrasmd = nf.create_section("Extras", "Raw Extras metadata")
        create_md_tree(extrasmd, extrainfo[0], block)

    # all done
    nf.close()
    print(f"Created NIX file at '{nfname}'")
    print("Done")
    def test_double_conversion(self):
        convert.nixwrite(self.odml_doc, 'tmp.nix')
        nix_file = nix.File('tmp.nix')
        convert.odmlwrite(nix_file, 'tmp.odml')
        odml_doc = odml.fileio.load('tmp.odml')

        for attr in document_attributes:
            self.assertEqual(getattr(self.odml_doc, attr),
                             getattr(odml_doc, attr))

        for sec in self.odml_doc.sections:
            sec2 = odml_doc.sections[sec.name]
            for attr in section_attributes:
                self.assertEqual(getattr(sec, attr), getattr(sec2, attr))

            for prop in sec.properties:
                prop2 = sec2.properties[prop.name]
                for attr in property_attributes:
                    self.assertEqual(getattr(prop, attr), getattr(prop2, attr))
예제 #4
0
def import_nix(nixfilename):
    """
    Import a NIX file (generated with mne2nix.py) into an MNE Raw structure.

    :param nixfilename: Path to the NIX file to be loaded.
    :rtype: mne.io.RawArray
    """
    nixfile = nix.File(nixfilename, mode=nix.FileMode.ReadOnly)

    # root, ext = os.path.splitext(nixfilename)
    # bvfilename = root + os.extsep + "vhdr"
    # bvfile = mne.io.read_raw_brainvision(bvfilename, stim_channel=False)

    # Create MNE Info object
    infosec = nixfile.sections["Info"]
    nchan = infosec["nchan"]
    sfreq = infosec["sfreq"]
    info = mne.create_info(nchan, sfreq)

    nixinfodict = md_to_dict(infosec)
    info.update(nixinfodict)

    # Read raw data into MNE objects
    datagroup = nixfile.blocks[DATA_BLOCK_NAME].groups[RAW_DATA_GROUP_NAME]
    if len(datagroup.data_arrays) > 1:
        # Data split: One DataArray per channel.  Merging
        nixrawdata = merge_data_arrays(datagroup.data_arrays)
    else:
        nixrawdata = datagroup.data_arrays[0][:]

    # Create MNE RawArray
    mnerawdata = mne.io.RawArray(nixrawdata, info)

    # Add annotations: Stimuli from MultiTags
    mtags = datagroup.multi_tags
    annotations = create_mne_annotations(mtags)

    mnerawdata.set_annotations(annotations)

    nixfile.close()

    return mnerawdata
예제 #5
0
    def test_double_conversion(self):
        nf = os.path.join(self.test_dir, 'tmp.nix')
        of = os.path.join(self.test_dir, 'tmp.odml')
        convert.nixwrite(self.odml_doc, nf, 'overwrite')
        nix_file = nix.File(nf)
        convert.odmlwrite(nix_file, of)
        odml_doc = odml.load(of)

        for attr in document_attributes:
            self.assertEqual(getattr(self.odml_doc, attr), getattr(odml_doc, attr))

        for sec in self.odml_doc.sections:
            sec2 = odml_doc.sections[sec.name]
            for attr in section_attributes:
                self.assertEqual(getattr(sec, attr), getattr(sec2, attr))

            for prop in sec.properties:
                prop2 = sec2.properties[prop.name]
                for attr in property_attributes:
                    self.assertEqual(getattr(prop, attr), getattr(prop2, attr))

        nix_file.close()
예제 #6
0
def validate(filename):
    nf = nix.File(filename, mode=nix.FileMode.ReadOnly)
    results = nf.validate()
    print("Results for '{}'".format(filename))
    errors = results["errors"]
    if errors:
        plural = "s" if len(errors) > 1 else ""
        print_header("{} object{} with errors".format(len(errors), plural))
        for idx, (obj, messages) in enumerate(errors.items()):
            print(" [{}] {}".format(idx+1, format_obj(obj)))
            for msg in messages:
                print("    {}".format(msg))

    warnings = results["warnings"]
    if warnings:
        plural = "s" if len(warnings) > 1 else ""
        print_header("{} object{} with warnings".format(len(warnings), plural))
        for idx, (obj, messages) in enumerate(warnings.items()):
            print(" [{}] {}".format(idx+1, format_obj(obj)))
            for msg in messages:
                print("    {}".format(msg))

    print()
예제 #7
0
def do_merge(nixfname, odmlfname):
    nf = nix.File(nixfname, mode=nix.FileMode.ReadWrite)
    md = odml.load(odmlfname)
    odmlnixsec = nf.create_section(odmlfname, "odML import")
    write_recurse(md.sections, odmlnixsec)
    nf.close()
예제 #8
0
import sys
import matplotlib.pyplot as plt
import nixio as nix

fname = sys.argv[1]
nixfile = nix.File(fname, mode=nix.FileMode.ReadOnly)
block = nixfile.blocks[0]

# plot all signals
data = block.data_arrays["EEG Data"]
time = data.dimensions[1].ticks
for idx, row in enumerate(data):
    plt.plot(time, row + idx * 0.001)

stim = block.multi_tags["Stimuli"]
for idx, p in enumerate(stim.positions):
    if stim.positions.dimensions[0].labels[idx] == "Stimulus/S  2":
        plt.plot([p, p], [0, len(data) * 0.001], "k")

plt.show()

nixfile.close()