Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

nsdfio.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. # -*- coding: utf-8 -*-
  2. """
  3. Module for reading and writing NSDF files
  4. Author: Mieszko Grodzicki
  5. This module support both reading and writing NDSF files.
  6. Note: Read file must be written using this IO
  7. """
  8. from __future__ import absolute_import
  9. import numpy as np
  10. import quantities as pq
  11. from uuid import uuid1
  12. import pickle
  13. from datetime import datetime
  14. import os
  15. try:
  16. import nsdf
  17. except ImportError as err:
  18. HAVE_NSDF = False
  19. NSDF_ERR = err
  20. else:
  21. HAVE_NSDF = True
  22. NSDF_ERR = None
  23. from neo.io.baseio import BaseIO
  24. from neo.core import Block, Segment, AnalogSignal, ChannelIndex
  25. class NSDFIO(BaseIO):
  26. """
  27. Class for reading and writing files in NSDF Format.
  28. It supports reading and writing: Block, Segment, AnalogSignal, ChannelIndex, with all relationships and metadata.
  29. """
  30. is_readable = True
  31. is_writable = True
  32. supported_objects = [Block, Segment, AnalogSignal, ChannelIndex]
  33. readable_objects = [Block, Segment]
  34. writeable_objects = [Block, Segment]
  35. has_header = False
  36. is_streameable = False
  37. name = 'NSDF'
  38. extensions = ['h5']
  39. mode = 'file'
  40. def __init__(self, filename=None):
  41. """
  42. Initialise NSDFIO instance
  43. :param filename: Path to the file
  44. """
  45. if not HAVE_NSDF:
  46. raise Exception("Failed to import NSDF.")
  47. if filename is None:
  48. raise ValueError("Must provide an input file.")
  49. BaseIO.__init__(self)
  50. self.filename = filename
  51. self.dt_format = '%d/%m/%Y %H:%M:%S'
  52. self.modeltree_path = '/model/modeltree/neo/'
  53. def write_all_blocks(self, blocks):
  54. """
  55. Write list of blocks to the file
  56. :param blocks: List of blocks to be written
  57. """
  58. writer = self._init_writing()
  59. neo_model, blocks_model, segments_model = self._prepare_model_tree(writer)
  60. name_pattern = self._name_pattern(len(blocks))
  61. for i, block in enumerate(blocks):
  62. self.write_block(block, name_pattern.format(i), writer, blocks_model)
  63. def write_block(self, block=None, name='0', writer=None, parent=None):
  64. """
  65. Write a Block to the file
  66. :param block: Block to be written
  67. :param name: Name for block representation in NSDF model tree (optional)
  68. :param writer: NSDFWriter instance (optional)
  69. :param parent: NSDF ModelComponent which will be the parent of block NSDF representation (optional)
  70. """
  71. if not isinstance(block, Block):
  72. raise ValueError("Must provide a Block to write.")
  73. if writer is None:
  74. writer = self._init_writing()
  75. if parent is None:
  76. neo_model, parent, segments_model = self._prepare_model_tree(writer)
  77. block_model = nsdf.ModelComponent(name, uid=uuid1().hex, parent=parent)
  78. self._write_container_metadata(block, block_model)
  79. self._write_model_component(block_model, writer)
  80. self._write_block_children(block, block_model, writer)
  81. self._clean_nsdfio_annotations(block)
  82. def _write_block_children(self, block, block_model, writer):
  83. segments_model = nsdf.ModelComponent(name='segments', uid=uuid1().hex, parent=block_model)
  84. self._write_model_component(segments_model, writer)
  85. name_pattern = self._name_pattern(len(block.segments))
  86. for i, segment in enumerate(block.segments):
  87. self.write_segment(segment=segment, name=name_pattern.format(i),
  88. writer=writer, parent=segments_model)
  89. channel_indexes_model = nsdf.ModelComponent(
  90. name='channel_indexes', uid=uuid1().hex, parent=block_model)
  91. self._write_model_component(channel_indexes_model, writer)
  92. name_pattern = self._name_pattern(len(block.channel_indexes))
  93. for i, channelindex in enumerate(block.channel_indexes):
  94. self.write_channelindex(channelindex=channelindex, name=name_pattern.format(i),
  95. writer=writer, parent=channel_indexes_model)
  96. def write_segment(self, segment=None, name='0', writer=None, parent=None):
  97. """
  98. Write a Segment to the file
  99. :param segment: Segment to be written
  100. :param name: Name for segment representation in NSDF model tree (optional)
  101. :param writer: NSDFWriter instance (optional)
  102. :param parent: NSDF ModelComponent which will be the parent of segment NSDF representation (optional)
  103. """
  104. if not isinstance(segment, Segment):
  105. raise ValueError("Must provide a Segment to write.")
  106. if writer is None:
  107. writer = self._init_writing()
  108. single_segment = False
  109. if parent is None:
  110. neo_model, blocks_model, parent = self._prepare_model_tree(writer)
  111. single_segment = True
  112. model = nsdf.ModelComponent(name, uid=uuid1().hex, parent=parent)
  113. self._write_container_metadata(segment, model)
  114. self._write_model_component(model, writer)
  115. self._write_segment_children(model, segment, writer)
  116. if single_segment:
  117. self._clean_nsdfio_annotations(segment)
  118. def _write_segment_children(self, model, segment, writer):
  119. analogsignals_model = nsdf.ModelComponent(
  120. name='analogsignals', uid=uuid1().hex, parent=model)
  121. self._write_model_component(analogsignals_model, writer)
  122. name_pattern = self._name_pattern(len(segment.analogsignals))
  123. for i, signal in enumerate(segment.analogsignals):
  124. self.write_analogsignal(signal=signal, name=name_pattern.format(i),
  125. parent=analogsignals_model, writer=writer)
  126. def write_analogsignal(self, signal, name, writer, parent):
  127. """
  128. Write an AnalogSignal to the file
  129. :param signal: AnalogSignal to be written
  130. :param name: Name for signal representation in NSDF model tree
  131. :param writer: NSDFWriter instance
  132. :param parent: NSDF ModelComponent which will be the parent of signal NSDF representation
  133. """
  134. uid = uuid1().hex
  135. model = nsdf.ModelComponent(name, uid=uid, parent=parent)
  136. if signal.annotations.get('nsdfio_uid') is not None:
  137. model.attrs['reference_to'] = signal.annotations['nsdfio_uid']
  138. self._write_model_component(model, writer)
  139. return
  140. self._write_basic_metadata(model, signal)
  141. signal.annotations['nsdfio_uid'] = uid
  142. r_signal = np.swapaxes(signal, 0, 1)
  143. channels_model, channels, source_ds = self._create_signal_data_sources(
  144. model, r_signal, uid, writer)
  145. self._write_signal_data(model, channels, r_signal, signal, source_ds, writer)
  146. self._write_model_component(model, writer)
  147. self._write_model_component(channels_model, writer)
  148. for channel_model in channels:
  149. self._write_model_component(channel_model, writer)
  150. def write_channelindex(self, channelindex, name, writer, parent):
  151. """
  152. Write a ChannelIndex to the file
  153. :param channelindex: ChannelIndex to be written
  154. :param name: Name for channelindex representation in NSDF model tree
  155. :param writer: NSDFWriter instance
  156. :param parent: NSDF ModelComponent which will be the parent of channelindex NSDF representation
  157. """
  158. uid = uuid1().hex
  159. model = nsdf.ModelComponent(name, uid=uid, parent=parent)
  160. self._write_basic_metadata(model, channelindex)
  161. self._write_model_component(model, writer)
  162. self._write_channelindex_arrays(model, channelindex, writer)
  163. self._write_channelindex_children(channelindex, model, writer)
  164. def _write_channelindex_children(self, channelindex, model, writer):
  165. analogsignals_model = nsdf.ModelComponent(
  166. name='analogsignals', uid=uuid1().hex, parent=model)
  167. self._write_model_component(analogsignals_model, writer)
  168. name_pattern = self._name_pattern(len(channelindex.analogsignals))
  169. for i, signal in enumerate(channelindex.analogsignals):
  170. self.write_analogsignal(signal=signal, name=name_pattern.format(i),
  171. parent=analogsignals_model, writer=writer)
  172. def _init_writing(self):
  173. return nsdf.NSDFWriter(self.filename, mode='w')
  174. def _prepare_model_tree(self, writer):
  175. neo_model = nsdf.ModelComponent('neo', uid=uuid1().hex)
  176. self._write_model_component(neo_model, writer)
  177. blocks_model = nsdf.ModelComponent('blocks', uid=uuid1().hex, parent=neo_model)
  178. self._write_model_component(blocks_model, writer)
  179. segments_model = nsdf.ModelComponent('segments', uid=uuid1().hex, parent=neo_model)
  180. self._write_model_component(segments_model, writer)
  181. return neo_model, blocks_model, segments_model
  182. def _number_of_digits(self, n):
  183. return len(str(n))
  184. def _name_pattern(self, how_many_items):
  185. return '{{:0{}d}}'.format(self._number_of_digits(max(how_many_items - 1, 0)))
  186. def _clean_nsdfio_annotations(self, object):
  187. nsdfio_annotations = ('nsdfio_uid',)
  188. for key in nsdfio_annotations:
  189. object.annotations.pop(key, None)
  190. if hasattr(object, 'children'):
  191. for child in object.children:
  192. self._clean_nsdfio_annotations(child)
  193. def _write_model_component(self, model, writer):
  194. if model.parent is None:
  195. nsdf.add_model_component(model, writer.model['modeltree/'])
  196. else:
  197. nsdf.add_model_component(model, model.parent.hdfgroup)
  198. def _write_container_metadata(self, container, container_model):
  199. self._write_basic_metadata(container_model, container)
  200. self._write_datetime_attributes(container_model, container)
  201. self._write_index_attribute(container_model, container)
  202. def _write_basic_metadata(self, model, object):
  203. self._write_basic_attributes(model, object)
  204. self._write_annotations(model, object)
  205. def _write_basic_attributes(self, model, object):
  206. if object.name is not None:
  207. model.attrs['name'] = object.name
  208. if object.description is not None:
  209. model.attrs['description'] = object.description
  210. def _write_datetime_attributes(self, model, object):
  211. if object.rec_datetime is not None:
  212. model.attrs['rec_datetime'] = object.rec_datetime.strftime(self.dt_format)
  213. def _write_index_attribute(self, model, object):
  214. if object.index is not None:
  215. model.attrs['index'] = object.index
  216. def _write_annotations(self, model, object):
  217. if object.annotations is not None:
  218. model.attrs['annotations'] = pickle.dumps(object.annotations)
  219. def _write_signal_data(self, model, channels, r_signal, signal, source_ds, writer):
  220. dataobj = nsdf.UniformData('signal', unit=str(signal.units.dimensionality))
  221. dataobj.dtype = signal.dtype
  222. for i in range(len(channels)):
  223. dataobj.put_data(channels[i].uid, r_signal[i])
  224. dataobj.set_dt(float(signal.sampling_period.magnitude),
  225. str(signal.sampling_period.dimensionality))
  226. rescaled_tstart = signal.t_start.rescale(signal.sampling_period.dimensionality)
  227. writer.add_uniform_data(source_ds, dataobj,
  228. tstart=float(rescaled_tstart.magnitude))
  229. model.attrs['t_start_unit'] = str(signal.t_start.dimensionality)
  230. def _create_signal_data_sources(self, model, r_signal, uid, writer):
  231. channels = []
  232. channels_model = nsdf.ModelComponent(name='channels', uid=uuid1().hex, parent=model)
  233. name_pattern = '{{:0{}d}}'.format(self._number_of_digits(max(len(r_signal) - 1, 0)))
  234. for i in range(len(r_signal)):
  235. channels.append(nsdf.ModelComponent(name_pattern.format(i),
  236. uid=uuid1().hex,
  237. parent=channels_model))
  238. source_ds = writer.add_uniform_ds(uid, [channel.uid for channel in channels])
  239. return channels_model, channels, source_ds
  240. def _write_channelindex_arrays(self, model, channelindex, writer):
  241. group = model.hdfgroup
  242. self._write_array(group, 'index', channelindex.index)
  243. if channelindex.channel_names is not None:
  244. self._write_array(group, 'channel_names', channelindex.channel_names)
  245. if channelindex.channel_ids is not None:
  246. self._write_array(group, 'channel_ids', channelindex.channel_ids)
  247. if channelindex.coordinates is not None:
  248. self._write_array(group, 'coordinates', channelindex.coordinates)
  249. def _write_array(self, group, name, array):
  250. if isinstance(array, pq.Quantity):
  251. group.create_dataset(name, data=array.magnitude)
  252. group[name].attrs['dimensionality'] = str(array.dimensionality)
  253. else:
  254. group.create_dataset(name, data=array)
  255. def read_all_blocks(self, lazy=False):
  256. """
  257. Read all blocks from the file
  258. :param lazy: Enables lazy reading
  259. :return: List of read blocks
  260. """
  261. assert not lazy, 'Do not support lazy'
  262. reader = self._init_reading()
  263. blocks = []
  264. blocks_path = self.modeltree_path + 'blocks/'
  265. for block in reader.model[blocks_path].values():
  266. blocks.append(self.read_block(group=block, reader=reader))
  267. return blocks
  268. def read_block(self, lazy=False, group=None, reader=None):
  269. """
  270. Read a Block from the file
  271. :param lazy: Enables lazy reading
  272. :param group: HDF5 Group representing the block in NSDF model tree (optional)
  273. :param reader: NSDFReader instance (optional)
  274. :return: Read block
  275. """
  276. assert not lazy, 'Do not support lazy'
  277. block = Block()
  278. group, reader = self._select_first_container(group, reader, 'block')
  279. if group is None:
  280. return None
  281. attrs = group.attrs
  282. self._read_block_children(block, group, reader)
  283. block.create_many_to_one_relationship()
  284. self._read_container_metadata(attrs, block)
  285. return block
  286. def _read_block_children(self, block, group, reader):
  287. for child in group['segments/'].values():
  288. block.segments.append(self.read_segment(group=child, reader=reader))
  289. for child in group['channel_indexes/'].values():
  290. block.channel_indexes.append(self.read_channelindex(group=child, reader=reader))
  291. def read_segment(self, lazy=False, group=None, reader=None):
  292. """
  293. Read a Segment from the file
  294. :param lazy: Enables lazy reading
  295. :param group: HDF5 Group representing the segment in NSDF model tree (optional)
  296. :param reader: NSDFReader instance (optional)
  297. :return: Read segment
  298. """
  299. assert not lazy, 'Do not support lazy'
  300. segment = Segment()
  301. group, reader = self._select_first_container(group, reader, 'segment')
  302. if group is None:
  303. return None
  304. attrs = group.attrs
  305. self._read_segment_children(group, reader, segment)
  306. self._read_container_metadata(attrs, segment)
  307. return segment
  308. def _read_segment_children(self, group, reader, segment):
  309. for child in group['analogsignals/'].values():
  310. segment.analogsignals.append(self.read_analogsignal(group=child, reader=reader))
  311. def read_analogsignal(self, lazy=False, group=None, reader=None):
  312. """
  313. Read an AnalogSignal from the file (must be child of a Segment)
  314. :param lazy: Enables lazy reading
  315. :param group: HDF5 Group representing the analogsignal in NSDF model tree
  316. :param reader: NSDFReader instance
  317. :return: Read AnalogSignal
  318. """
  319. assert not lazy, 'Do not support lazy'
  320. attrs = group.attrs
  321. if attrs.get('reference_to') is not None:
  322. return self.objects_dict[attrs['reference_to']]
  323. uid = attrs['uid']
  324. data_group = reader.data['uniform/{}/signal'.format(uid)]
  325. t_start = self._read_analogsignal_t_start(attrs, data_group)
  326. signal = self._create_analogsignal(data_group, group, t_start, uid, reader)
  327. self._read_basic_metadata(attrs, signal)
  328. self.objects_dict[uid] = signal
  329. return signal
  330. def read_channelindex(self, lazy=False, group=None, reader=None):
  331. """
  332. Read a ChannelIndex from the file (must be child of a Block)
  333. :param lazy: Enables lazy reading
  334. :param group: HDF5 Group representing the channelindex in NSDF model tree
  335. :param reader: NSDFReader instance
  336. :return: Read ChannelIndex
  337. """
  338. assert not lazy, 'Do not support lazy'
  339. attrs = group.attrs
  340. channelindex = self._create_channelindex(group)
  341. self._read_channelindex_children(group, reader, channelindex)
  342. self._read_basic_metadata(attrs, channelindex)
  343. return channelindex
  344. def _read_channelindex_children(self, group, reader, channelindex):
  345. for child in group['analogsignals/'].values():
  346. channelindex.analogsignals.append(self.read_analogsignal(group=child, reader=reader))
  347. def _init_reading(self):
  348. reader = nsdf.NSDFReader(self.filename)
  349. self.file_datetime = datetime.fromtimestamp(os.stat(self.filename).st_mtime)
  350. self.objects_dict = {}
  351. return reader
  352. def _select_first_container(self, group, reader, name):
  353. if reader is None:
  354. reader = self._init_reading()
  355. if group is None:
  356. path = self.modeltree_path + name + 's/'
  357. if len(reader.model[path].values()) > 0:
  358. group = reader.model[path].values()[0]
  359. return group, reader
  360. def _read_container_metadata(self, attrs, container):
  361. self._read_basic_metadata(attrs, container)
  362. self._read_datetime_attributes(attrs, container)
  363. self._read_index_attribute(attrs, container)
  364. def _read_basic_metadata(self, attrs, signal):
  365. self._read_basic_attributes(attrs, signal)
  366. self._read_annotations(attrs, signal)
  367. def _read_basic_attributes(self, attrs, object):
  368. if attrs.get('name') is not None:
  369. object.name = attrs['name']
  370. if attrs.get('description') is not None:
  371. object.description = attrs['description']
  372. object.file_origin = self.filename
  373. def _read_datetime_attributes(self, attrs, object):
  374. object.file_datetime = self.file_datetime
  375. if attrs.get('rec_datetime') is not None:
  376. object.rec_datetime = datetime.strptime(attrs['rec_datetime'], self.dt_format)
  377. def _read_annotations(self, attrs, object):
  378. if attrs.get('annotations') is not None:
  379. object.annotations = pickle.loads(attrs['annotations'])
  380. def _read_index_attribute(self, attrs, object):
  381. if attrs.get('index') is not None:
  382. object.index = attrs['index']
  383. def _create_analogsignal(self, data_group, group, t_start, uid, reader):
  384. # for lazy
  385. # data_shape = data_group.shape
  386. # data_shape = (data_shape[1], data_shape[0])
  387. dataobj = reader.get_uniform_data(uid, 'signal')
  388. data = self._read_signal_data(dataobj, group)
  389. signal = self._create_normal_analogsignal(data, dataobj, uid, t_start)
  390. return signal
  391. def _read_analogsignal_t_start(self, attrs, data_group):
  392. t_start = float(data_group.attrs['tstart']) * pq.Quantity(1, data_group.attrs['tunit'])
  393. t_start = t_start.rescale(attrs['t_start_unit'])
  394. return t_start
  395. def _read_signal_data(self, dataobj, group):
  396. data = []
  397. for channel in group['channels/'].values():
  398. channel_uid = channel.attrs['uid']
  399. data += [dataobj.get_data(channel_uid)]
  400. return data
  401. def _create_normal_analogsignal(self, data, dataobj, uid, t_start):
  402. return AnalogSignal(np.swapaxes(data, 0, 1), dtype=dataobj.dtype, units=dataobj.unit,
  403. t_start=t_start, sampling_period=pq.Quantity(dataobj.dt, dataobj.tunit))
  404. def _create_channelindex(self, group):
  405. return ChannelIndex(index=self._read_array(group, 'index'),
  406. channel_names=self._read_array(group, 'channel_names'),
  407. channel_ids=self._read_array(group, 'channel_ids'),
  408. coordinates=self._read_array(group, 'coordinates'))
  409. def _read_array(self, group, name):
  410. if group.__contains__(name) == False:
  411. return None
  412. array = group[name][:]
  413. if group[name].attrs.get('dimensionality') is not None:
  414. return pq.Quantity(array, group[name].attrs['dimensionality'])
  415. return array