group.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. This module implements :class:`Group`, which represents a subset of the
  3. channels in an :class:`AnalogSignal` or :class:`IrregularlySampledSignal`.
  4. It replaces and extends the grouping function of the former :class:`ChannelIndex`
  5. and :class:`Unit`.
  6. """
  7. from os import close
  8. from neo.core.container import Container
  9. class Group(Container):
  10. """
  11. Can contain any of the data objects, views, or other groups,
  12. outside the hierarchy of the segment and block containers.
  13. A common use is to link the :class:`SpikeTrain` objects within a :class:`Block`,
  14. possibly across multiple Segments, that were emitted by the same neuron.
  15. *Required attributes/properties*:
  16. None
  17. *Recommended attributes/properties*:
  18. :objects: (Neo object) Objects with which to pre-populate the :class:`Group`
  19. :name: (str) A label for the group.
  20. :description: (str) Text description.
  21. :file_origin: (str) Filesystem path or URL of the original data file.
  22. *Optional arguments*:
  23. :allowed_types: (list or tuple) Types of Neo object that are allowed to be
  24. added to the Group. If not specified, any Neo object can be added.
  25. Note: Any other additional arguments are assumed to be user-specific
  26. metadata and stored in :attr:`annotations`.
  27. *Container of*:
  28. :class:`AnalogSignal`, :class:`IrregularlySampledSignal`, :class:`SpikeTrain`,
  29. :class:`Event`, :class:`Epoch`, :class:`ChannelView`, :class:`Group`
  30. """
  31. _data_child_objects = (
  32. 'AnalogSignal', 'IrregularlySampledSignal', 'SpikeTrain',
  33. 'Event', 'Epoch', 'ChannelView', 'ImageSequence'
  34. )
  35. _container_child_objects = ('Segment', 'Group')
  36. _single_parent_objects = ('Block',)
  37. def __init__(self, objects=None, name=None, description=None, file_origin=None,
  38. allowed_types=None, **annotations):
  39. super().__init__(name=name, description=description,
  40. file_origin=file_origin, **annotations)
  41. if allowed_types is None:
  42. self.allowed_types = None
  43. else:
  44. self.allowed_types = tuple(allowed_types)
  45. if objects:
  46. self.add(*objects)
  47. @property
  48. def _container_lookup(self):
  49. return {
  50. cls_name: getattr(self, container_name)
  51. for cls_name, container_name in zip(self._child_objects, self._child_containers)
  52. }
  53. def _get_container(self, cls):
  54. if hasattr(cls, "proxy_for"):
  55. cls = cls.proxy_for
  56. return self._container_lookup[cls.__name__]
  57. def add(self, *objects):
  58. """Add a new Neo object to the Group"""
  59. for obj in objects:
  60. if self.allowed_types and not isinstance(obj, self.allowed_types):
  61. raise TypeError("This Group can only contain {}, but not {}"
  62. "".format(self.allowed_types, type(obj)))
  63. container = self._get_container(obj.__class__)
  64. container.append(obj)