Ejemplo n.º 1
0
def test_partial_shape():
    a = np.arange(12).reshape((4, 3))
    test_cases = [
        (slice(None), (4, 3)),
        ((slice(None), slice(None)), (4, 3)),
        (slice(1, None, 2), (2, 3)),
        (1, (3, )),
        ((1, slice(None)), (3, )),
        ([0, 2, 3], (3, 3)),
        (np.array([0, 2, 3]), (3, 3)),
        ((np.array([0, 2, 3]), slice(None)), (3, 3)),
        (np.array([True, False, True, True]), (3, 3)),
        #(np.array([True, False]), (1, 3)),  # not valid with NumPy 1.13
        (np.array([[True, False, False], [False, False, False],
                   [True, True, False], [False, True, False]]), (4, )),
        #(np.array([[True, False, False], [False, False, False], [True, True, False]]), (3,)),  # not valid with NumPy 1.13
        ((3, 1), tuple()),
        ((slice(None), 1), (4, )),
        ((slice(None), slice(1, None, 3)), (4, 1)),
        ((np.array([0, 3]), 2), (2, )),
        ((np.array([0, 3]), np.array([1, 2])), (2, )),
        ((slice(None), np.array([2])), (4, 1)),
        (((1, 3), (0, 2)), (2, )),
        (np.array([], bool), (0, 3)),
    ]
    for mask, expected_shape in test_cases:
        assert_equal(partial_shape(mask, a.shape), a[mask].shape)
        assert_equal(partial_shape(mask, a.shape), expected_shape)
    b = np.arange(5)
    test_cases = [(np.arange(5), (5, ))]
    for mask, expected_shape in test_cases:
        assert_equal(partial_shape(mask, b.shape), b[mask].shape)
        assert_equal(partial_shape(mask, b.shape), expected_shape)
Ejemplo n.º 2
0
def test__issue4():
    # In order to avoid the errors associated with version changes of numpy, mask1 and mask2 no longer contain boolean values ​​but integer values
    a = np.arange(12).reshape((4, 3))
    b = larray(np.arange(12).reshape((4, 3)))
    mask1 = (slice(None), int(True))
    mask2 = (slice(None), np.array([int(True)]))
    assert_equal(b[mask1].shape, partial_shape(mask1, b.shape), a[mask1].shape)
    assert_equal(b[mask2].shape, partial_shape(mask2, b.shape), a[mask2].shape)
Ejemplo n.º 3
0
Archivo: random.py Proyecto: wau/PyNN
 def lazily_evaluate(self, mask=None, shape=None):
     """
     Generate an array of random numbers of the requested shape.
     
     If a mask is given, produce only enough numbers to fill the
     region defined by the mask (hence 'lazily').
     
     This method is called by the lazyarray `evaluate()` and
     `_partially_evaluate()` methods.
     """
     if mask is None:
         # produce an array of random numbers with the requested shape
         n = reduce(operator.mul, shape)
         res = self.next(n)
         if res.shape != shape:
             res = res.reshape(shape)
         if n == 1:
             res = res[0]
     else:
         # produce an array of random numbers whose shape is
         # that of a sub-array produced by applying the mask
         # to an array of the requested global shape
         p_shape = partial_shape(mask, shape)
         if p_shape:
             n = reduce(operator.mul, p_shape)
         else:
             n = 1
         res = self.next(n).reshape(p_shape)
     return res
Ejemplo n.º 4
0
 def lazily_evaluate(self, mask=None, shape=None):
     """
     Generate an array of random numbers of the requested shape.
     
     If a mask is given, produce only enough numbers to fill the
     region defined by the mask (hence 'lazily').
     
     This method is called by the lazyarray `evaluate()` and
     `_partially_evaluate()` methods.
     """
     if mask is None:
         # produce an array of random numbers with the requested shape
         n = reduce(operator.mul, shape)
         res = self.next(n)
         if res.shape != shape:
             res = res.reshape(shape)
         if n == 1:
             res = res[0]
     else:
         # produce an array of random numbers whose shape is
         # that of a sub-array produced by applying the mask
         # to an array of the requested global shape
         p_shape = partial_shape(mask, shape)
         if p_shape:
             n = reduce(operator.mul, p_shape)
         else:
             n = 1
         res = self.next(n).reshape(p_shape)
     return res
Ejemplo n.º 5
0
 def evaluate(self, mask=None, simplify=False):
     """
     Evaluate all lazy arrays contained in the parameter space, using the
     given mask.
     """
     if self._shape is None:
         raise Exception(
             "Must set shape of parameter space before evaluating")
     if mask is None:
         for name, value in self._parameters.items():
             self._parameters[name] = value.evaluate(simplify=simplify)
         self._evaluated_shape = self._shape
     else:
         for name, value in self._parameters.items():
             try:
                 if isinstance(value.base_value, RandomDistribution
                               ) and value.base_value.rng.parallel_safe:
                     value = value.evaluate(
                     )  # can't partially evaluate if using parallel safe
                 self._parameters[name] = value[mask]
             except ValueError:
                 raise errors.InvalidParameterValueError(
                     f"{name} should not be of type {type(value)}")
         self._evaluated_shape = partial_shape(mask, self._shape)
     self._evaluated = True
Ejemplo n.º 6
0
 def evaluate(self, mask=None, simplify=False):
     """
     Evaluate all lazy arrays contained in the parameter space, using the
     given mask.
     """
     if self._shape is None:
         raise Exception("Must set shape of parameter space before evaluating")
     if mask is None:
         for name, value in self._parameters.items():
             self._parameters[name] = value.evaluate(simplify=simplify)
         self._evaluated_shape = self._shape
     else:
         for name, value in self._parameters.items():
             self._parameters[name] = value[mask]
         self._evaluated_shape = partial_shape(mask, self._shape)
     self._evaluated = True
Ejemplo n.º 7
0
 def evaluate(self, mask=None, simplify=False):
     """
     Evaluate all lazy arrays contained in the parameter space, using the
     given mask.
     """
     if self._shape is None:
         raise Exception(
             "Must set shape of parameter space before evaluating")
     if mask is None:
         for name, value in self._parameters.items():
             self._parameters[name] = value.evaluate(simplify=simplify)
         self._evaluated_shape = self._shape
     else:
         for name, value in self._parameters.items():
             self._parameters[name] = value[mask]
         self._evaluated_shape = partial_shape(mask, self._shape)
     self._evaluated = True
Ejemplo n.º 8
0
 def evaluate(self, mask=None, simplify=False):
     """
     Evaluate all lazy arrays contained in the parameter space, using the
     given mask.
     """
     if self._shape is None:
         raise Exception("Must set shape of parameter space before evaluating")
     if mask is None:
         for name, value in self._parameters.items():
             self._parameters[name] = value.evaluate(simplify=simplify)
         self._evaluated_shape = self._shape
     else:
         for name, value in self._parameters.items():
             if isinstance(value.base_value, RandomDistribution) and value.base_value.rng.parallel_safe:
                 value = value.evaluate()  # can't partially evaluate if using parallel safe
             self._parameters[name] = value[mask]
         self._evaluated_shape = partial_shape(mask, self._shape)
     self._evaluated = True