Ver Fonte

gin commit from Ivory

New files: 1
Modified files: 1
Achilleas Koutsou há 5 anos atrás
pai
commit
7ff808d81a
2 ficheiros alterados com 56 adições e 8 exclusões
  1. 7 8
      mnetonix.py
  2. 49 0
      readrawnix.py

+ 7 - 8
mnetonix.py

@@ -89,12 +89,14 @@ def create_md_tree(section, values, block):
                 subsec = section.create_section(k, str(v.__class__))
                 create_md_tree(subsec, v, block)
                 continue
-            elif isinstance(v[0], Mapping):
-                # Create multiple new Sections to hold the metadata found in
-                # each nested dictionary
+            if isinstance(v[0], Mapping):
+                # Create a new subsection to hold each nested dictionary as
+                # sub-subsections
+                print(k)
+                print(v)
+                subsec = section.create_section(k, str(v.__class__))
                 for idx, subd in enumerate(v):
-                    secname = f"{k}-{idx}"
-                    subsec = section.create_section(secname, str(v.__class__))
+                    print(subd)
                     create_md_tree(subsec, subd, block)
                 continue
 
@@ -257,9 +259,6 @@ def main():
         raise RuntimeError(f"Unknown extension '{ext}'")
     print(f"Converting '{datafilename}' to NIX")
 
-    write_raw_mne(nfname, mneraw, True)
-
-    nfname = root + "-oneda" + os.path.extsep + "nix"
     write_raw_mne(nfname, mneraw, False)
 
     mneraw.close()

+ 49 - 0
readrawnix.py

@@ -0,0 +1,49 @@
+import os
+import sys
+import nixio as nix
+import mne
+
+
+def md_to_dict(section):
+    sdict = dict()
+    for prop in section.props:
+        values = list(prop.values)
+        if len(values) == 1:
+            values = values[0]
+        sdict[prop.name] = values
+
+    for sec in section.sections:
+        sdict[sec.name] = md_to_dict(sec)
+
+    return sdict
+
+
+def main():
+    if len(sys.argv) < 2:
+        print("Please provide either a NIX filename as the first argument")
+        sys.exit(1)
+
+    nixfilename = sys.argv[1]
+    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)
+    print(info)
+
+    info.update(md_to_dict(infosec))
+
+    print(info)
+    print(bvfile.info)
+
+    nixfile.close()
+
+
+if __name__ == "__main__":
+    main()