mnetonix.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. """
  2. mnetonix.py
  3. Usage:
  4. python mnetonix.py <datafile> <montage>
  5. datafile: Either an EDF file or a BrainVision header file (vhdr).
  6. montage: Any format montage file supported by MNE.
  7. (Requires Python 3)
  8. Command line script for reading EDF and BrainVision files using MNE
  9. (mne-python) and storing the data and metadata into a NIX file. Supports
  10. reading montage files for recording channel locations.
  11. NIX Format layout:
  12. Data:
  13. Raw Data are stored in either a single 2-dimensional DataArray or a collection
  14. of DataArrays (one per recording channel). The latter makes tagging easier
  15. since MultiTag positions and extents don't need to specify every channel they
  16. reference. However, creating multiple DataArrays makes file sizes much
  17. bigger.
  18. Stimuli:
  19. MNE provides stimulus information through the Raw.annotations dictionary.
  20. Onsets correspond to the 'positions' array and durations correspond to the
  21. 'extents' array of the "Stimuli" MultiTag.
  22. Metadata:
  23. MNE collects metadata into a (nested) dictionary (Raw.info). All non-empty
  24. keys are converted into Properties in NIX. The nested structure of the
  25. dictionary is replicated in NIX by creating child Sections, starting with one
  26. root section with name "Info".
  27. """
  28. import sys
  29. import os
  30. from collections.abc import Iterable, Mapping
  31. import mne
  32. import matplotlib.pyplot as plt
  33. import numpy as np
  34. import nixio as nix
  35. RAW_DATA_TYPE = "Raw Data"
  36. def plot_channel(data_array, index):
  37. signal = data_array[index]
  38. tdim = data_array.dimensions[1]
  39. datadim = data_array.dimensions[0]
  40. plt.plot(tdim.ticks, signal, label=datadim.labels[index])
  41. xlabel = f"({tdim.unit})"
  42. plt.xlabel(xlabel)
  43. ylabel = f"{datadim.labels[index]} ({data_array.unit})"
  44. plt.ylabel(ylabel)
  45. plt.legend()
  46. plt.show()
  47. def create_md_tree(section, values, block):
  48. if values is None:
  49. return
  50. for k, v in values.items():
  51. if v is None:
  52. continue
  53. if isinstance(v, Iterable):
  54. if not len(v):
  55. continue
  56. ndim = np.ndim(v)
  57. if ndim > 1:
  58. da = block.create_data_array(k, "Multidimensional Metadata",
  59. data=v)
  60. section.create_property(k, da.id)
  61. da.metadata = section
  62. continue
  63. # check element type
  64. if isinstance(v, Mapping):
  65. # Create a new Section to hold the metadata found in the
  66. # dictionary
  67. subsec = section.create_section(k, str(v.__class__))
  68. create_md_tree(subsec, v, block)
  69. continue
  70. elif isinstance(v[0], Mapping):
  71. # Create multiple new Sections to hold the metadata found in
  72. # each nested dictionary
  73. for idx, subd in enumerate(v):
  74. secname = f"{k}-{idx}"
  75. subsec = section.create_section(secname, str(v.__class__))
  76. create_md_tree(subsec, subd, block)
  77. continue
  78. try:
  79. section.create_property(k, v)
  80. except TypeError:
  81. # inconsistent iterable types: upgrade to floats
  82. section.create_property(k, [float(vi) for vi in v])
  83. def write_single_da(mneraw, block):
  84. # data and times
  85. data = mneraw.get_data()
  86. time = mneraw.times
  87. nchan = mneraw.info["nchan"]
  88. print(f"Found {nchan} channels with {mneraw.n_times} samples per channel")
  89. da = block.create_data_array("EEG Data", RAW_DATA_TYPE, data=data)
  90. da.unit = "V"
  91. for dimlen in data.shape:
  92. if dimlen == nchan:
  93. # channel labels: SetDimension
  94. da.append_set_dimension(labels=mneraw.ch_names)
  95. elif dimlen == mneraw.n_times:
  96. # times: RangeDimension
  97. # NOTE: EDF always uses seconds
  98. da.append_range_dimension(ticks=time, label="time", unit="s")
  99. def write_multi_da(mneraw, block):
  100. data = mneraw.get_data()
  101. time = mneraw.times
  102. nchan = mneraw.info["nchan"]
  103. channames = mneraw.ch_names
  104. print(f"Found {nchan} channels with {mneraw.n_times} samples per channel")
  105. # find the channel dimension to iterate over it
  106. for idx, dimlen in enumerate(data.shape):
  107. if dimlen == nchan:
  108. chanidx = idx
  109. break
  110. else:
  111. raise RuntimeError("Could not find data dimension that matches number "
  112. "of channels")
  113. for idx, chandata in enumerate(np.rollaxis(data, chanidx)):
  114. chname = channames[idx]
  115. da = block.create_data_array(chname, RAW_DATA_TYPE, data=chandata)
  116. da.unit = "V"
  117. # times: RangeDimension
  118. # NOTE: EDF always uses seconds
  119. da.append_range_dimension(ticks=time, label="time", unit="s")
  120. def write_stim_tags(mneraw, block):
  121. stimuli = mneraw.annotations
  122. positions = [(p,) for p in stimuli.onset]
  123. extents = [(e,) for e in stimuli.duration]
  124. labels = stimuli.description
  125. posda = block.create_data_array("Stimuli onset", "Stimuli Positions",
  126. data=positions)
  127. posda.append_set_dimension(labels=labels.tolist())
  128. extda = block.create_data_array("Stimuli Durations", "Stimuli Extents",
  129. data=extents)
  130. extda.append_set_dimension(labels=labels.tolist())
  131. stimmtag = block.create_multi_tag("Stimuli", "EEG Stimuli",
  132. positions=posda)
  133. stimmtag.extents = extda
  134. for da in block.data_arrays:
  135. if da.type == RAW_DATA_TYPE:
  136. stimmtag.references.append(da)
  137. def write_raw_mne(nfname, mneraw, split_data_channels=True):
  138. mneinfo = mneraw.info
  139. extrainfo = mneraw._raw_extras
  140. # Create NIX file
  141. nf = nix.File(nfname, nix.FileMode.Overwrite)
  142. # Write Data to NIX
  143. block = nf.create_block("EEG Data Block", "Recording",
  144. compression=nix.Compression.DeflateNormal)
  145. if split_data_channels:
  146. write_multi_da(mneraw, block)
  147. else:
  148. write_single_da(mneraw, block)
  149. write_stim_tags(mneraw, block)
  150. # Write metadata to NIX
  151. # info dictionary
  152. infomd = nf.create_section("Info", "File metadata")
  153. create_md_tree(infomd, mneinfo, block)
  154. # extras
  155. if len(extrainfo) > 1:
  156. for idx, emd_i in enumerate(extrainfo):
  157. extrasmd = nf.create_section(f"Extras-{idx}",
  158. "Raw Extras metadata")
  159. create_md_tree(extrasmd, emd_i, block)
  160. elif extrainfo:
  161. extrasmd = nf.create_section("Extras", "Raw Extras metadata")
  162. create_md_tree(extrasmd, extrainfo[0], block)
  163. # all done
  164. nf.close()
  165. print(f"Created NIX file at '{nfname}'")
  166. print("Done")
  167. def main():
  168. if len(sys.argv) < 2:
  169. print("Please provide either a BrainVision vhdr or "
  170. "an EDF filename as the first argument")
  171. sys.exit(1)
  172. datafilename = sys.argv[1]
  173. montage = None
  174. if len(sys.argv) > 2:
  175. montage = sys.argv[2]
  176. montage = os.path.abspath(montage)
  177. root, ext = os.path.splitext(datafilename)
  178. nfname = root + os.path.extsep + "nix"
  179. if ext.casefold() == ".edf".casefold():
  180. mneraw = mne.io.read_raw_edf(datafilename, montage=montage,
  181. preload=True, stim_channel=False)
  182. elif ext.casefold() == ".vhdr".casefold():
  183. mneraw = mne.io.read_raw_brainvision(datafilename, montage=montage,
  184. preload=True, stim_channel=False)
  185. else:
  186. raise RuntimeError(f"Unknown extension '{ext}'")
  187. print(f"Converting '{datafilename}' to NIX")
  188. write_raw_mne(nfname, mneraw)
  189. mneraw.close()
  190. if __name__ == "__main__":
  191. main()