mnetonix.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. DATA_BLOCK_NAME = "EEG Data Block"
  36. DATA_BLOCK_TYPE = "Recording"
  37. RAW_DATA_GROUP_NAME = "Raw Data Group"
  38. RAW_DATA_GROUP_TYPE = "EEG Channels"
  39. RAW_DATA_TYPE = "Raw Data"
  40. def plot_channel(data_array, index):
  41. signal = data_array[index]
  42. tdim = data_array.dimensions[1]
  43. datadim = data_array.dimensions[0]
  44. plt.plot(tdim.ticks, signal, label=datadim.labels[index])
  45. xlabel = f"({tdim.unit})"
  46. plt.xlabel(xlabel)
  47. ylabel = f"{datadim.labels[index]} ({data_array.unit})"
  48. plt.ylabel(ylabel)
  49. plt.legend()
  50. plt.show()
  51. def create_md_tree(section, values, block):
  52. if values is None:
  53. return
  54. for k, v in values.items():
  55. if v is None:
  56. continue
  57. if isinstance(v, Iterable):
  58. if not len(v):
  59. continue
  60. ndim = np.ndim(v)
  61. if ndim > 1:
  62. da = block.create_data_array(k, "Multidimensional Metadata",
  63. data=v)
  64. section.create_property(k, da.id)
  65. da.metadata = section
  66. continue
  67. # check element type
  68. if isinstance(v, Mapping):
  69. # Create a new Section to hold the metadata found in the
  70. # dictionary
  71. subsec = section.create_section(k, str(v.__class__))
  72. create_md_tree(subsec, v, block)
  73. continue
  74. elif isinstance(v[0], Mapping):
  75. # Create multiple new Sections to hold the metadata found in
  76. # each nested dictionary
  77. for idx, subd in enumerate(v):
  78. secname = f"{k}-{idx}"
  79. subsec = section.create_section(secname, str(v.__class__))
  80. create_md_tree(subsec, subd, block)
  81. continue
  82. try:
  83. section.create_property(k, v)
  84. except TypeError:
  85. # inconsistent iterable types: upgrade to floats
  86. section.create_property(k, [float(vi) for vi in v])
  87. def write_single_da(mneraw, block):
  88. # data and times
  89. data = mneraw.get_data()
  90. time = mneraw.times
  91. nchan = mneraw.info["nchan"]
  92. print(f"Found {nchan} channels with {mneraw.n_times} samples per channel")
  93. da = block.create_data_array("EEG Data", RAW_DATA_TYPE, data=data)
  94. block.groups[RAW_DATA_GROUP_NAME].data_arrays.append(da)
  95. da.unit = "V"
  96. for dimlen in data.shape:
  97. if dimlen == nchan:
  98. # channel labels: SetDimension
  99. da.append_set_dimension(labels=mneraw.ch_names)
  100. elif dimlen == mneraw.n_times:
  101. # times: RangeDimension
  102. # NOTE: EDF always uses seconds
  103. da.append_range_dimension(ticks=time, label="time", unit="s")
  104. def write_multi_da(mneraw, block):
  105. data = mneraw.get_data()
  106. time = mneraw.times
  107. nchan = mneraw.info["nchan"]
  108. channames = mneraw.ch_names
  109. print(f"Found {nchan} channels with {mneraw.n_times} samples per channel")
  110. # find the channel dimension to iterate over it
  111. for idx, dimlen in enumerate(data.shape):
  112. if dimlen == nchan:
  113. chanidx = idx
  114. break
  115. else:
  116. raise RuntimeError("Could not find data dimension that matches number "
  117. "of channels")
  118. for idx, chandata in enumerate(np.rollaxis(data, chanidx)):
  119. chname = channames[idx]
  120. da = block.create_data_array(chname, RAW_DATA_TYPE, data=chandata)
  121. block.groups[RAW_DATA_GROUP_NAME].data_arrays.append(da)
  122. da.unit = "V"
  123. # times: RangeDimension
  124. # NOTE: EDF always uses seconds
  125. da.append_range_dimension(ticks=time, label="time", unit="s")
  126. def write_stim_tags(mneraw, block):
  127. stimuli = mneraw.annotations
  128. positions = [(p,) for p in stimuli.onset]
  129. extents = [(e,) for e in stimuli.duration]
  130. labels = stimuli.description
  131. posda = block.create_data_array("Stimuli onset", "Stimuli Positions",
  132. data=positions)
  133. posda.append_set_dimension(labels=labels.tolist())
  134. extda = block.create_data_array("Stimuli Durations", "Stimuli Extents",
  135. data=extents)
  136. extda.append_set_dimension(labels=labels.tolist())
  137. stimmtag = block.create_multi_tag("Stimuli", "EEG Stimuli",
  138. positions=posda)
  139. stimmtag.extents = extda
  140. block.groups[RAW_DATA_GROUP_NAME].multi_tags.append(stimmtag)
  141. for da in block.data_arrays:
  142. if da.type == RAW_DATA_TYPE:
  143. stimmtag.references.append(da)
  144. def write_raw_mne(nfname, mneraw, split_data_channels=True):
  145. mneinfo = mneraw.info
  146. extrainfo = mneraw._raw_extras
  147. # Create NIX file
  148. nf = nix.File(nfname, nix.FileMode.Overwrite)
  149. # Write Data to NIX
  150. block = nf.create_block(DATA_BLOCK_NAME, DATA_BLOCK_TYPE,
  151. compression=nix.Compression.DeflateNormal)
  152. block.create_group(RAW_DATA_GROUP_NAME, RAW_DATA_GROUP_TYPE)
  153. if split_data_channels:
  154. write_multi_da(mneraw, block)
  155. else:
  156. write_single_da(mneraw, block)
  157. write_stim_tags(mneraw, block)
  158. # Write metadata to NIX
  159. # info dictionary
  160. infomd = nf.create_section("Info", "File metadata")
  161. create_md_tree(infomd, mneinfo, block)
  162. # extras
  163. if len(extrainfo) > 1:
  164. for idx, emd_i in enumerate(extrainfo):
  165. extrasmd = nf.create_section(f"Extras-{idx}",
  166. "Raw Extras metadata")
  167. create_md_tree(extrasmd, emd_i, block)
  168. elif extrainfo:
  169. extrasmd = nf.create_section("Extras", "Raw Extras metadata")
  170. create_md_tree(extrasmd, extrainfo[0], block)
  171. # all done
  172. nf.close()
  173. print(f"Created NIX file at '{nfname}'")
  174. print("Done")
  175. def main():
  176. if len(sys.argv) < 2:
  177. print("Please provide either a BrainVision vhdr or "
  178. "an EDF filename as the first argument")
  179. sys.exit(1)
  180. datafilename = sys.argv[1]
  181. montage = None
  182. if len(sys.argv) > 2:
  183. montage = sys.argv[2]
  184. montage = os.path.abspath(montage)
  185. root, ext = os.path.splitext(datafilename)
  186. nfname = root + os.path.extsep + "nix"
  187. if ext.casefold() == ".edf".casefold():
  188. mneraw = mne.io.read_raw_edf(datafilename, montage=montage,
  189. preload=True, stim_channel=False)
  190. elif ext.casefold() == ".vhdr".casefold():
  191. mneraw = mne.io.read_raw_brainvision(datafilename, montage=montage,
  192. preload=True, stim_channel=False)
  193. else:
  194. raise RuntimeError(f"Unknown extension '{ext}'")
  195. print(f"Converting '{datafilename}' to NIX")
  196. write_raw_mne(nfname, mneraw)
  197. mneraw.close()
  198. if __name__ == "__main__":
  199. main()