# -*- coding: utf-8 -*-
"""
Tools to manipulate Neo objects.
:copyright: Copyright 2014-2016 by the Elephant team, see AUTHORS.txt.
:license: Modified BSD, see LICENSE.txt for details.
"""
from __future__ import division, print_function
from itertools import chain
from neo.core.container import unique_objs
def _get_all_objs(container, classname):
"""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.
classname : str
The name of the class, with proper capitalization
(so `SpikeTrain`, not `Spiketrain` or `spiketrain`)
Returns
-------
list
A list of unique `neo` objects
"""
if container.__class__.__name__ == classname:
return [container]
classholder = classname.lower() + 's'
if hasattr(container, classholder):
vals = getattr(container, classholder)
elif hasattr(container, 'list_children_by_class'):
vals = container.list_children_by_class(classname)
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, classname)
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 list of the unique `neo.SpikeTrain` objects in `container`.
"""
return _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')