# -*- coding: utf-8 -*-
"""
Tools to manipulate Neo objects.
.. autosummary::
:toctree: _toctree/neo_tools
extract_neo_attributes
get_all_spiketrains
get_all_events
get_all_epochs
:copyright: Copyright 2014-2024 by the Elephant team, see `doc/authors.rst`.
:license: Modified BSD, see LICENSE.txt for details.
"""
from __future__ import division, print_function, unicode_literals
import warnings
from itertools import chain
from neo.core.spiketrainlist import SpikeTrainList
from neo.core.container import unique_objs
__all__ = [
"extract_neo_attributes",
"get_all_spiketrains",
"get_all_events",
"get_all_epochs"
]
def extract_neo_attrs(*args, **kwargs):
warnings.warn("'extract_neo_attrs' function is deprecated; "
"use 'extract_neo_attributes'", DeprecationWarning)
return extract_neo_attributes(*args, **kwargs)
def _get_all_objs(container, class_name):
"""
Get all Neo objects of a given type from a container.
The objects can be any list, dict, or other iterable or mapping containing
Neo objects of a particular class, as well as any Neo object that can hold
the object.
Objects are searched recursively, so the objects can be nested (such as a
list of blocks).
Parameters
----------
container : list, tuple, iterable, dict, neo.Container
The container for the Neo objects.
class_name : str
The name of the class, with proper capitalization
(i.e., 'SpikeTrain', not 'Spiketrain' or 'spiketrain').
Returns
-------
list
A list of unique Neo objects.
Raises
------
ValueError
If can not handle containers of the type passed in `container`.
"""
if container.__class__.__name__ == class_name:
return [container]
classholder = class_name.lower() + 's'
if hasattr(container, classholder):
vals = getattr(container, classholder)
elif hasattr(container, 'list_children_by_class'):
vals = container.list_children_by_class(class_name)
elif hasattr(container, 'values') and not hasattr(container, 'ndim'):
vals = container.values()
elif hasattr(container, '__iter__') and not hasattr(container, 'ndim'):
vals = container
else:
raise ValueError('Cannot handle object of type %s' % type(container))
res = list(chain.from_iterable(_get_all_objs(obj, class_name)
for obj in vals))
return unique_objs(res)
[docs]
def get_all_spiketrains(container):
"""
Get all `neo.Spiketrain` objects from a container.
The objects can be any list, dict, or other iterable or mapping containing
spiketrains, as well as any Neo object that can hold spiketrains:
`neo.Block`, `neo.ChannelIndex`, `neo.Unit`, and `neo.Segment`.
Containers are searched recursively, so the objects can be nested
(such as a list of blocks).
Parameters
----------
container : list, tuple, iterable, dict, neo.Block, neo.Segment, neo.Unit,
neo.ChannelIndex
The container for the spiketrains.
Returns
-------
list
A `neo.SpikeTrainList` object of the unique `neo.SpikeTrain` objects
in `container`.
"""
return SpikeTrainList(_get_all_objs(container, 'SpikeTrain'))
[docs]
def get_all_events(container):
"""
Get all `neo.Event` objects from a container.
The objects can be any list, dict, or other iterable or mapping containing
events, as well as any neo object that can hold events:
`neo.Block` and `neo.Segment`.
Containers are searched recursively, so the objects can be nested
(such as a list of blocks).
Parameters
----------
container : list, tuple, iterable, dict, neo.Block, neo.Segment
The container for the events.
Returns
-------
list
A list of the unique `neo.Event` objects in `container`.
"""
return _get_all_objs(container, 'Event')
[docs]
def get_all_epochs(container):
"""
Get all `neo.Epoch` objects from a container.
The objects can be any list, dict, or other iterable or mapping containing
epochs, as well as any neo object that can hold epochs:
`neo.Block` and `neo.Segment`.
Containers are searched recursively, so the objects can be nested
(such as a list of blocks).
Parameters
----------
container : list, tuple, iterable, dict, neo.Block, neo.Segment
The container for the epochs.
Returns
-------
list
A list of the unique `neo.Epoch` objects in `container`.
"""
return _get_all_objs(container, 'Epoch')