Ejemplo n.º 1
0
def test_apply_function_to_structured_array():
    f = lambda m: 2 * m + 3
    input = np.arange(12).reshape((4, 3))
    m0 = LazyArray(input, shape=(4, 3))
    m1 = f(m0)
    assert isinstance(m1, larray)
    assert_array_equal(m1.evaluate(simplify=True), input * 2 + 3)
Ejemplo n.º 2
0
def test_columnwise_iteration_with_function():
    input = lambda i, j: 2 * i + j
    m = LazyArray(input, shape=(4, 3))
    cols = [col for col in m.by_column()]
    assert_array_equal(cols[0], np.array([0, 2, 4, 6]))
    assert_array_equal(cols[1], np.array([1, 3, 5, 7]))
    assert_array_equal(cols[2], np.array([2, 4, 6, 8]))
Ejemplo n.º 3
0
 def _get_connection_map_no_self_connections(self, projection):
     if (isinstance(projection.pre, Population)
             and isinstance(projection.post, Population)
             and projection.pre == projection.post):
         # special case, expected to be faster than the default, below
         connection_map = LazyArray(lambda i, j: i != j,
                                    shape=projection.shape)
     else:
         # this could be optimized by checking parent or component populations
         # but should handle both views and assemblies
         a = numpy.broadcast_to(
             projection.pre.all_cells,
             (projection.post.size, projection.pre.size)).T
         b = projection.post.all_cells
         connection_map = LazyArray(a != b, shape=projection.shape)
     return connection_map
Ejemplo n.º 4
0
def test_columnwise_iteration_with_structured_array_and_mask():
    input = np.arange(12).reshape((4, 3))
    m = LazyArray(input, shape=(4, 3))  # 4 rows, 3 columns
    mask = np.array([False, True, True])
    cols = [col for col in m.by_column(mask=mask)]
    assert_array_equal(cols[0], input[:, 1])
    assert_array_equal(cols[1], input[:, 2])
Ejemplo n.º 5
0
 def connect(self, projection):
     assert self.rng.parallel_safe
     shuffle = self.rng.permutation(numpy.arange(projection.pre.size))
     n = self.n
     if hasattr(self, "rand_distr"):
         n = self.rand_distr.next(projection.pre.size)
     f_ij = lambda i, j: shuffle[i] < n
     connection_map = LazyArray(f_ij, projection.shape)
     if projection.pre == projection.post:
         if not self.allow_self_connections:
             connection_map *= LazyArray(lambda i, j: i != j,
                                         shape=projection.shape)
         elif self.allow_self_connections == 'NoMutual':
             connection_map *= LazyArray(lambda i, j: i > j,
                                         shape=projection.shape)
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 6
0
def test_evaluate_with_functional_array():
    def input(i, j):
        return 2 * i + j

    m = LazyArray(input, shape=(4, 3))
    assert_array_equal(m.evaluate(),
                       np.array([[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]]))
Ejemplo n.º 7
0
    def _get_parameters(self, *names):
        """
        return a ParameterSpace containing native parameters
        """
        ids = self.local_cells.tolist()
        if hasattr(self.celltype, "uses_parrot") and self.celltype.uses_parrot:
            ids = [id.source for id in ids]

        if "spike_times" in names:
            parameter_dict = {
                "spike_times":
                [Sequence(value) for value in nest.GetStatus(ids, names)]
            }
        else:
            parameter_dict = {}
            for name in names:  # one name at a time, since some parameter values may be tuples
                val = np.array(nest.GetStatus(ids, name))
                if isinstance(val[0], tuple) or len(val.shape) == 2:
                    val = np.array([ArrayParameter(v) for v in val])
                    val = LazyArray(simplify(val),
                                    shape=(self.local_size, ),
                                    dtype=ArrayParameter)
                    parameter_dict[name] = val
                else:
                    parameter_dict[name] = simplify(val)
        ps = ParameterSpace(parameter_dict, shape=(self.local_size, ))
        return ps
Ejemplo n.º 8
0
    def initialize(self, **initial_values):
        """
        Set initial values of state variables of synaptic plasticity models.

        Values passed to initialize() may be:
            (1) single numeric values (all neurons set to the same value)
            (2) RandomDistribution objects
            (3) a 2D array with the same dimensions as the connectivity matrix
                (as returned by `get(format='array')`
            (4) a mapping function, which accepts a single float argument (the
                distance between pre- and post-synaptic cells) and returns a single value.

        Values should be expressed in the standard PyNN units (i.e. millivolts,
        nanoamps, milliseconds, microsiemens, nanofarads, event per second).

        Example::

            prj.initialize(u=-70.0)
        """
        for variable, value in initial_values.items():
            logger.debug("In Projection '%s', initialising %s to %s" %
                         (self.label, variable, value))
            initial_value = LazyArray(value, shape=(self.size,), dtype=float)
            self._set_initial_value_array(variable, initial_value)
            self.initial_values[variable] = initial_value
Ejemplo n.º 9
0
 def _get_connection_map_no_mutual_connections(self, projection):
     if (isinstance(projection.pre, Population)
         and isinstance(projection.post, Population)
             and projection.pre == projection.post):
         connection_map = LazyArray(lambda i, j: i > j, shape=projection.shape)
     else:
         raise NotImplementedError("todo")
     return connection_map
Ejemplo n.º 10
0
def test_apply_function_to_functional_array():
    input = lambda i, j: 2 * i + j
    m0 = LazyArray(input, shape=(4, 3))
    f = lambda m: 2 * m + 3
    m1 = f(m0)
    assert_array_equal(
        m1.evaluate(),
        np.array([[3, 5, 7], [7, 9, 11], [11, 13, 15], [15, 17, 19]]))
Ejemplo n.º 11
0
 def connect(self, projection):
     if not self.allow_self_connections:
         connection_map = self._get_connection_map_no_self_connections(projection)
     elif self.allow_self_connections == 'NoMutual':
         connection_map = self._get_connection_map_no_mutual_connections(projection)
     else:
         connection_map = LazyArray(True, shape=projection.shape)
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 12
0
def test_multiple_operations_with_structured_array():
    input = np.arange(12).reshape((4, 3))
    m0 = LazyArray(input, shape=(4, 3))
    m1 = (m0 + 2) < 5
    m2 = (m0 < 5) + 2
    assert_array_equal(m1.evaluate(simplify=True), (input + 2) < 5)
    assert_array_equal(m2.evaluate(simplify=True), (input < 5) + 2)
    assert_array_equal(m0.evaluate(simplify=True), input)
Ejemplo n.º 13
0
 def connect(self, projection):
     # The index function is copied so as to avoid the connector being altered by the "connect"
     # function, which is probably unexpected behaviour.
     index_expression = copy(self.index_expression)
     index_expression.projection = projection
     probability_map = LazyArray(index_expression, projection.shape)
     random_map = LazyArray(
         RandomDistribution('uniform', (0, 1), rng=self.rng),
         projection.shape)
     connection_map = random_map < probability_map
     if not self.allow_self_connections:
         mask = self._get_connection_map_no_self_connections(projection)
         connection_map *= mask
     elif self.allow_self_connections == 'NoMutual':
         mask = self._get_connection_map_no_mutual_connections(projection)
         connection_map *= mask
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 14
0
 def connect(self, projection):
     conn_list = numpy.array([(self.orig_proj.pre.id_to_index(c.source),
                               self.orig_proj.post.id_to_index(c.target))
                              for c in self.orig_proj.connections])
     conn_matrix = numpy.zeros((projection.pre.size, projection.post.size))
     conn_matrix[conn_list[:, 0], conn_list[:, 1]] = True
     connection_map = LazyArray(conn_matrix)
     self._connect_with_map(connection_map)
Ejemplo n.º 15
0
 def connect(self, projection):
     assert self.rng.parallel_safe
     # this is probably very inefficient, would be better to use
     # divergent connect
     shuffle = numpy.array([
         self.rng.permutation(numpy.arange(projection.post.size))
         for i in range(projection.pre.size)
     ])
     n = self.n
     if hasattr(self, "rand_distr"):
         n = self.rand_distr.next(projection.pre.size)
     f_ij = lambda i, j: shuffle[:, j] < n
     connection_map = LazyArray(f_ij, projection.shape)
     if not self.allow_self_connections and projection.pre == projection.post:
         connection_map *= LazyArray(lambda i, j: i != j,
                                     shape=projection.shape)
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 16
0
def test_apply_function_to_constant_array():
    f = lambda m: 2 * m + 3
    m0 = LazyArray(5, shape=(4, 3))
    m1 = f(m0)
    assert isinstance(m1, larray)
    assert_equal(m1.evaluate(simplify=True), 13)
    # the following tests the internals, not the behaviour
    # it is just to check I understand what's going on
    assert_equal(m1.operations, [(operator.mul, 2), (operator.add, 3)])
Ejemplo n.º 17
0
def test_columnwise_iteration_with_random_array_parallel_safe_no_mask():
    random.mpi_rank = 0
    random.num_processes = 2
    input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    copy_input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    cols = [col for col in m.by_column()]
    assert_array_equal(cols[0], copy_input.next(4, mask_local=False))
    assert_array_equal(cols[1], copy_input.next(4, mask_local=False))
    assert_array_equal(cols[2], copy_input.next(4, mask_local=False))
Ejemplo n.º 18
0
def test_columnwise_iteration_with_random_array_parallel_safe_with_mask():
    random.mpi_rank = 0
    random.num_processes = 2
    input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    copy_input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    mask = np.array([False, False, True])
    cols = [col for col in m.by_column(mask=mask)]
    assert_equal(len(cols), 1)
    assert_array_almost_equal(cols[0],
                              copy_input.next(12, mask_local=False)[8:], 15)
Ejemplo n.º 19
0
def test_columnwise_iteration_with_random_array_parallel_safe_no_mask():
    orig_get_mpi_config = random.get_mpi_config
    random.get_mpi_config = lambda: (0, 2)
    input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    copy_input = random.RandomDistribution(rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    cols = [col for col in m.by_column()]
    assert_array_equal(cols[0], copy_input.next(4, mask_local=False))
    assert_array_equal(cols[1], copy_input.next(4, mask_local=False))
    assert_array_equal(cols[2], copy_input.next(4, mask_local=False))
    random.get_mpi_config = orig_get_mpi_config
def test_columnwise_iteration_with_random_array_parallel_safe_with_mask():
    orig_get_mpi_config = random.get_mpi_config
    random.get_mpi_config = lambda: (0, 2)
    input = random.RandomDistribution('uniform', (0, 1), rng=MockRNG(parallel_safe=True))
    copy_input = random.RandomDistribution('gamma', (2, 3), rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    mask = np.array([False, False, True])
    cols = [col for col in m.by_column(mask=mask)]
    assert_equal(len(cols), 1)
    assert_array_almost_equal(cols[0], copy_input.next(12, mask_local=False)[8:], 15)
    random.get_mpi_config = orig_get_mpi_config
Ejemplo n.º 21
0
 def connect(self, projection):
     random_map = LazyArray(RandomDistribution('uniform', (0, 1), rng=self.rng),
                            projection.shape)
     connection_map = random_map < self.p_connect
     if not self.allow_self_connections:
         mask = self._get_connection_map_no_self_connections(projection)
         connection_map *= mask
     elif self.allow_self_connections == 'NoMutual':
         mask = self._get_connection_map_no_mutual_connections(projection)
         connection_map *= mask
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 22
0
def test_columnwise_iteration_with_random_array_parallel_safe_no_mask():
    orig_get_mpi_config = random.get_mpi_config

    # first, with a single MPI node
    random.get_mpi_config = lambda: (0, 2)
    input = random.RandomDistribution('uniform', (0, 1),
                                      rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    cols_np1 = [col for col in m.by_column()]

    # now, on one node of two
    random.get_mpi_config = lambda: (1, 2)
    input = random.RandomDistribution('uniform', (0, 1),
                                      rng=MockRNG(parallel_safe=True))
    m = LazyArray(input, shape=(4, 3))
    cols_np2_1 = [col for col in m.by_column()]

    for i in range(3):
        assert_array_equal(cols_np1[i], cols_np2_1[i])

    random.get_mpi_config = orig_get_mpi_config
Ejemplo n.º 23
0
 def connect(self, projection):
     if (projection.pre != self.reference_projection.pre or
             projection.post != self.reference_projection.post):
         raise errors.ConnectionError("Pre and post populations must match between reference ({0}"
                                      "  and {1}) and clone projections ({2} and {3}) for "
                                      "CloneConnector"
                                      .format(self.reference_projection.pre,
                                              self.reference_projection.post,
                                              projection.pre, projection.post))
     connection_map = LazyArray(~np.isnan(self.reference_projection.get(['weight'], 'array',
                                                                           gather='all')[0]))
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 24
0
def test_apply_function_to_functional_array():
    def input(i, j):
        return 2 * i + j

    m0 = LazyArray(input, shape=(4, 3))

    def f(m):
        return 2 * m + 3

    m1 = f(m0)
    assert_array_equal(
        m1.evaluate(),
        np.array([[3, 5, 7], [7, 9, 11], [11, 13, 15], [15, 17, 19]]))
Ejemplo n.º 25
0
 def connect(self, projection):
     distance_map = self._generate_distance_map(projection)
     probability_map = self.distance_function(distance_map)
     random_map = LazyArray(RandomDistribution('uniform', (0, 1), rng=self.rng),
                            projection.shape)
     connection_map = random_map < probability_map
     if not self.allow_self_connections:
         mask = self._get_connection_map_no_self_connections(projection)
         connection_map *= mask
     elif self.allow_self_connections == 'NoMutual':
         mask = self._get_connection_map_no_mutual_connections(projection)
         connection_map *= mask
     self._connect_with_map(projection, connection_map, distance_map)
Ejemplo n.º 26
0
 def connect(self, projection):
     raise NotImplementedError()  # the code below is not correct
     assert self.rng.parallel_safe
     # this is probably very inefficient, would be better to use
     # divergent connect
     shuffle = numpy.array([
         self.rng.permutation(numpy.arange(projection.post.size))
         for i in range(projection.pre.size)
     ])
     n = self.n
     if hasattr(self, "rand_distr"):
         n = self.rand_distr.next(projection.pre.size)
     f_ij = lambda i, j: shuffle[:, j] < n
     connection_map = LazyArray(f_ij, projection.shape)
     if projection.pre == projection.post:
         if not self.allow_self_connections:
             connection_map *= LazyArray(lambda i, j: i != j,
                                         shape=projection.shape)
         elif self.allow_self_connections == 'NoMutual':
             connection_map *= LazyArray(lambda i, j: i > j,
                                         shape=projection.shape)
     self._connect_with_map(projection, connection_map)
Ejemplo n.º 27
0
 def _handle_distance_expressions(self, parameter_space):
     # also index-based expressions
     for name, map in parameter_space.items():
         if callable(map.base_value):
             if isinstance(map.base_value, core.IndexBasedExpression):
                 map.base_value.projection = self
                 parameter_space[name] = map
             else:
                 # Assumes map is a function of distance
                 position_generators = (self.pre.position_generator,
                                        self.post.position_generator)
                 distance_map = LazyArray(self.space.distance_generator(*position_generators),
                                          shape=self.shape)
                 parameter_space[name] = map(distance_map)
     return parameter_space
Ejemplo n.º 28
0
    def connect(self, projection):
        """Connect-up a Projection."""
        # Cut out finite part
        c = csa.cross((0, projection.pre.size - 1), (0, projection.post.size - 1)) * self.cset  # can't we cut out just the columns we want?

        if csa.arity(self.cset) == 2:
            # Connection-set with arity 2
            for (i, j, weight, delay) in c:
                projection._convergent_connect([projection.pre[i]], projection.post[j], weight, delay)
        elif csa.arity(self.cset) == 0:
            # inefficient implementation as a starting point
            connection_map = numpy.zeros((projection.pre.size, projection.post.size), dtype=bool)
            for addr in c:
                connection_map[addr] = True
            self._connect_with_map(projection, LazyArray(connection_map))
        else:
            raise NotImplementedError
Ejemplo n.º 29
0
 def _get_parameters(self, *names):
     """
     return a ParameterSpace containing native parameters
     """
     parameter_dict = {}
     for name in names:
         if name == 'spike_times':  # hack
             parameter_dict[name] = [Sequence(getattr(id._cell, name)) for id in self]
         else:
             val = numpy.array([getattr(id._cell, name) for id in self])
             if isinstance(val[0], tuple) or len(val.shape) == 2:
                 val = numpy.array([ArrayParameter(v) for v in val])
                 val = LazyArray(simplify(val), shape=(self.local_size,), dtype=ArrayParameter)
                 parameter_dict[name] = val
             else:
                 parameter_dict[name] = simplify(val)
             parameter_dict[name] = simplify(val)
     return ParameterSpace(parameter_dict, shape=(self.local_size,))
Ejemplo n.º 30
0
def test_columnwise_iteration_with_structured_array():
    input = np.arange(12).reshape((4, 3))
    m = LazyArray(input, shape=(4, 3))  # 4 rows, 3 columns
    cols = [col for col in m.by_column()]
    assert_array_equal(cols[0], input[:, 0])
    assert_array_equal(cols[2], input[:, 2])