def test_some(self): a = 1 b = np.array([3.14159265, 3.1415]) c = [1, '1', 2.3, '5 8'] d = set([1, '2', 'spam']) objs = [a, b, b, b, c, b, a, d, b, b, a, d, d, d, c, d, b, d, c, a] targ = [a, b, c, d] res = unique_objs(objs) self.assertEqual(targ, res)
def data_children_recur(self): ''' All data child objects stored in the current object, obtained recursively. ''' # subclassing this to remove duplicate objects such as SpikeTrain # objects in both Segment and Unit # Only Block can have duplicate items right now, so implement # this here for performance reasons. return tuple(unique_objs(super().data_children_recur))
def data_children_recur(self): ''' All data child objects stored in the current object, obtained recursively. ''' # subclassing this to remove duplicate objects such as SpikeTrain # objects in both Segment and Unit # Only Block can have duplicate items right now, so implement # this here for performance reasons. return tuple(unique_objs(super(Block, self).data_children_recur))
def list_children_by_class(self, cls): ''' List all children of a particular class recursively. You can either provide a class object, a class name, or the name of the container storing the class. ''' # subclassing this to remove duplicate objects such as SpikeTrain # objects in both Segment and Unit # Only Block can have duplicate items right now, so implement # this here for performance reasons. return unique_objs(super().list_children_by_class(cls))
def list_children_by_class(self, cls): ''' List all children of a particular class recursively. You can either provide a class object, a class name, or the name of the container storing the class. ''' # subclassing this to remove duplicate objects such as SpikeTrain # objects in both Segment and Unit # Only Block can have duplicate items right now, so implement # this here for performance reasons. return unique_objs(super(Block, self).list_children_by_class(cls))
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)
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__'): 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)