Exemplo n.º 1
0
 def _container_child_containers(self):
     """
     Containers for child objects that are a container and
     have a single parent.
     """
     return tuple([_container_name(child) for child in
                   self._container_child_objects])
Exemplo n.º 2
0
 def _container_child_containers(self):
     """
     Containers for child objects that are a container and
     have a single parent.
     """
     return tuple([_container_name(child) for child in
                   self._container_child_objects])
Exemplo n.º 3
0
    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.
        """
        if not hasattr(cls, 'lower'):
            cls = cls.__name__
        container_name = _container_name(cls)
        objs = list(getattr(self, container_name, []))
        for child in self.container_children_recur:
            objs.extend(getattr(child, container_name, []))
        return objs
Exemplo n.º 4
0
    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.
        """
        if not hasattr(cls, 'lower'):
            cls = cls.__name__
        container_name = _container_name(cls)
        objs = list(getattr(self, container_name, []))
        for child in self.container_children_recur:
            objs.extend(getattr(child, container_name, []))
        return objs
Exemplo n.º 5
0
    def create_many_to_many_relationship(self, append=True, recursive=True):
        """
        For children of the current object that can have more than one parent
        of this type, put the current object in the parent list.

        If append is True add it to the list, otherwise overwrite the list.
        If recursive is True desecend into child objects and create
        relationships there
        """
        parent_name = _container_name(self.__class__.__name__)
        for child in self._multi_children:
            if not hasattr(child, parent_name):
                continue
            if append:
                target = getattr(child, parent_name)
                if not self in target:
                    target.append(self)
                continue
            setattr(child, parent_name, [self])

        if recursive:
            for child in self.container_children:
                child.create_many_to_many_relationship(append=append,
                                                       recursive=True)
Exemplo n.º 6
0
    def create_many_to_many_relationship(self, append=True, recursive=True):
        """
        For children of the current object that can have more than one parent
        of this type, put the current object in the parent list.

        If append is True add it to the list, otherwise overwrite the list.
        If recursive is True desecend into child objects and create
        relationships there
        """
        parent_name = _container_name(self.__class__.__name__)
        for child in self._multi_children:
            if not hasattr(child, parent_name):
                continue
            if append:
                target = getattr(child, parent_name)
                if not self in target:
                    target.append(self)
                continue
            setattr(child, parent_name, [self])

        if recursive:
            for child in self.container_children:
                child.create_many_to_many_relationship(append=append,
                                                       recursive=True)
Exemplo n.º 7
0
 def _multi_child_containers(self):
     """
     Containers for child objects that can have multiple parents.
     """
     return tuple(
         [_container_name(child) for child in self._multi_child_objects])
Exemplo n.º 8
0
 def _single_child_containers(self):
     """
     Containers for child objects with a single parent.
     """
     return tuple(
         [_container_name(child) for child in self._single_child_objects])
Exemplo n.º 9
0
def fake_neo(obj_type="Block", cascade=True, seed=None, n=1):
    '''
    Create a fake NEO object of a given type. Follows one-to-many
    and many-to-many relationships if cascade.

    n (default=1) is the number of child objects of each type will be created.
    In cases like segment.spiketrains, there will be more than this number
    because there will be n for each unit, of which there will be n for
    each channelindex, of which there will be n.
    '''

    if hasattr(obj_type, 'lower'):
        cls = class_by_name[obj_type]
    else:
        cls = obj_type
        obj_type = obj_type.__name__

    if cls is Epoch:
        obj = fake_epoch(seed=seed, n=n)
    else:
        kwargs = get_fake_values(obj_type, annotate=True, seed=seed, n=n)
        obj = cls(**kwargs)

    # if not cascading, we don't need to do any of the stuff after this
    if not cascade:
        return obj

    # this is used to signal other containers that they shouldn't duplicate
    # data
    if obj_type == 'Block':
        cascade = 'block'
    for i, childname in enumerate(getattr(obj, '_child_objects', [])):
        # we create a few of each class
        for j in range(n):
            if seed is not None:
                iseed = 10 * seed + 100 * i + 1000 * j
            else:
                iseed = None
            child = fake_neo(obj_type=childname, cascade=cascade, seed=iseed, n=n)
            child.annotate(i=i, j=j)

            # if we are creating a block and this is the object's primary
            # parent, don't create the object, we will import it from secondary
            # containers later
            if (cascade == 'block' and len(child._parent_objects) > 0
                    and obj_type != child._parent_objects[-1]):
                continue
            getattr(obj, _container_name(childname)).append(child)

    # need to manually create 'implicit' connections
    if obj_type == 'Block':
        # connect data objects to segment
        for i, chx in enumerate(obj.channel_indexes):
            for k, sigarr in enumerate(chx.analogsignals):
                obj.segments[k].analogsignals.append(sigarr)
            for k, sigarr in enumerate(chx.irregularlysampledsignals):
                obj.segments[k].irregularlysampledsignals.append(sigarr)
            for j, unit in enumerate(chx.units):
                for k, train in enumerate(unit.spiketrains):
                    obj.segments[k].spiketrains.append(train)
    # elif obj_type == 'ChannelIndex':
    #    inds = []
    #    names = []
    #    chinds = np.array([unit.channel_indexes[0] for unit in obj.units])
    #    obj.indexes = np.array(inds, dtype='i')
    #    obj.channel_names = np.array(names).astype('S')

    if hasattr(obj, 'create_many_to_one_relationship'):
        obj.create_many_to_one_relationship()

    return obj
Exemplo n.º 10
0
 def _multi_child_containers(self):
     """
     Containers for child objects that can have multiple parents.
     """
     return tuple([_container_name(child) for child in
                   self._multi_child_objects])
Exemplo n.º 11
0
 def _single_child_containers(self):
     """
     Containers for child objects with a single parent.
     """
     return tuple([_container_name(child) for child in
                   self._single_child_objects])
def fake_neo(obj_type="Block", cascade=True, seed=None, n=1):
    '''
    Create a fake NEO object of a given type. Follows one-to-many
    and many-to-many relationships if cascade.

    n (default=1) is the number of child objects of each type will be created.
    In cases like segment.spiketrains, there will be more than this number
    because there will be n for each unit, of which there will be n for
    each channelindex, of which there will be n.
    '''

    if hasattr(obj_type, 'lower'):
        cls = class_by_name[obj_type]
    else:
        cls = obj_type
        obj_type = obj_type.__name__

    kwargs = get_fake_values(obj_type, annotate=True, seed=seed, n=n)
    obj = cls(**kwargs)

    # if not cascading, we don't need to do any of the stuff after this
    if not cascade:
        return obj

    # this is used to signal other containers that they shouldn't duplicate
    # data
    if obj_type == 'Block':
        cascade = 'block'
    for i, childname in enumerate(getattr(obj, '_child_objects', [])):
        # we create a few of each class
        for j in range(n):
            if seed is not None:
                iseed = 10*seed+100*i+1000*j
            else:
                iseed = None
            child = fake_neo(obj_type=childname, cascade=cascade,
                             seed=iseed, n=n)
            child.annotate(i=i, j=j)

            # if we are creating a block and this is the object's primary
            # parent, don't create the object, we will import it from secondary
            # containers later
            if (cascade == 'block' and len(child._parent_objects) > 0 and
                    obj_type != child._parent_objects[-1]):
                continue
            getattr(obj, _container_name(childname)).append(child)

    # need to manually create 'implicit' connections
    if obj_type == 'Block':
        # connect data objects to segment
        for i, chx in enumerate(obj.channel_indexes):
            for k, sigarr in enumerate(chx.analogsignals):
                obj.segments[k].analogsignals.append(sigarr)
            for k, sigarr in enumerate(chx.irregularlysampledsignals):
                obj.segments[k].irregularlysampledsignals.append(sigarr)
            for j, unit in enumerate(chx.units):
                for k, train in enumerate(unit.spiketrains):
                    obj.segments[k].spiketrains.append(train)
    #elif obj_type == 'ChannelIndex':
    #    inds = []
    #    names = []
    #    chinds = np.array([unit.channel_indexes[0] for unit in obj.units])
    #    obj.indexes = np.array(inds, dtype='i')
    #    obj.channel_names = np.array(names).astype('S')

    if hasattr(obj, 'create_many_to_one_relationship'):
        obj.create_many_to_one_relationship()

    return obj