def test_vectorize_lambda_xfail(self):
     functions = [lambda a, b: a + b, lambda a, b: a * b]
     f = cupy.vectorize(functions[0])
     x1 = testing.shaped_random((20, 30), cupy, numpy.int64, seed=1)
     x2 = testing.shaped_random((20, 30), cupy, numpy.int64, seed=2)
     with pytest.raises(ValueError, match='Multiple callables are found'):
         return f(x1, x2)
示例#2
0
def get_perlin_init(shape=(1000, 1000),
                    n=100000,
                    cutoff=None,
                    repetition=(1000, 1000),
                    scale=100,
                    octaves=20.0,
                    persistence=0.1,
                    lacunarity=2.0):
    """Returns a tuple of x,y-coordinates sampled from Perlin noise.
    This can be used to initialize the starting positions of a physarum-
    population, as well as to generate a cloudy feeding-pattern that will
    have a natural feel to it. This function wraps the one from the noise-
    library from Casey Duncan, and is in parts borrowed from here (see also this for a good explanation of the noise-parameters):
    https://medium.com/@yvanscher/playing-with-perlin-noise-generating-realistic-archipelagos-b59f004d8401
    The most relevant paramaters for our purposes are:

    :param shape: The shape of the area in which the noise is to be generated. Defaults to (1000,1000)
    :type shape: Tuple of integers with the form (width, height).
    :param n: Number of particles to sample. When used as a feeeding trace,
    this translates to the relative strength of the pattern. defaults to 100000.
    :param cutoff: value below which noise should be set to zero. Default is None. Will lead to probabilities 'contains NaN-error, if to high'
    :param scale: (python-noise parameter) The scale of the noise -- larger or smaller patterns, defaults to 100.
    :param repetition: (python-noise parameter) Tuple that denotes the size of the area in which the noise should repeat itself. Defaults to (1000,1000)

    """
    import numpy as np
    import cupy as cp  # vectorized not present in cupy, so for now to conversion at the end

    shape = [i - 1 for i in shape]

    # make coordinate grid on [0,1]^2
    x_idx = np.linspace(0, shape[0], shape[0])
    y_idx = np.linspace(0, shape[1], shape[1])
    world_x, world_y = np.meshgrid(x_idx, y_idx)

    # apply perlin noise, instead of np.vectorize, consider using itertools.starmap()
    world = np.vectorize(noise.pnoise2)(
        world_x / scale,
        world_y / scale,
        octaves=int(octaves),
        persistence=persistence,
        lacunarity=lacunarity,
        repeatx=repetition[0],
        repeaty=repetition[1],
        base=np.random.randint(0, 100),
    )
    # world = world * 3
    # 	 Sample particle init from map:
    world[world <= 0.0] = 0.0  # filter negative values
    if cutoff is not None:
        world[world <= cutoff] = 0.0
    linear_idx = np.random.choice(world.size,
                                  size=n,
                                  p=world.ravel() / float(world.sum()))
    x, y = np.unravel_index(linear_idx, shape)
    x = x.reshape(-1, 1)
    y = y.reshape(-1, 1)

    return cp.asarray(np.hstack([x, y]))
示例#3
0
    def test_vectorize_const_typeerror(self):
        def my_invalid_type(x):
            x = numpy.dtype('f').type
            return x

        f = cupy.vectorize(my_invalid_type)
        x = testing.shaped_random((20, 30), cupy, numpy.int32, seed=1)
        with pytest.raises(TypeError):
            f(x)
示例#4
0
    def test_vectorize_const_non_toplevel(self):
        def my_invalid_type(x):
            if x == 3:
                typecast = numpy.dtype('f').type  # NOQA
            return x

        f = cupy.vectorize(my_invalid_type)
        x = cupy.array([1, 2, 3, 4, 5])
        with pytest.raises(TypeError):
            f(x)
示例#5
0
    def test_tuple_pattern_match_type_error(self):
        def func_pattern_match(x, y):
            x, y = y, x
            z = x, y
            (a, b), z = z, x
            return a * a + b

        f = cupy.vectorize(func_pattern_match)
        x = cupy.array([0, 1, 2, 3, 4])
        y = cupy.array([5, 6, 7, 8, 9])
        with pytest.raises(TypeError, match='Data type mismatch of variable:'):
            return f(x, y)
示例#6
0
    def collapse_last_dim(arr, keep_dim=True):
        """
        Collapse the last dimension
        :param arr: data array
        :param keep_dim: keep the last dim as one or not
        :return:
        """
        def _collapse(x):
            return x

        fn = cupy.vectorize(_collapse, otypes=[object], signature='(m)->()')
        t = fn(arr)
        if keep_dim:
            shape = arr.shape[:-1] + (1, )
            t = t.reshape(shape)
        return t
示例#7
0
 def test_relu_type_error(self):
     f = cupy.vectorize(lambda x: x if x > 0.0 else cupy.float64(0.0))
     a = cupy.array([0.4, -0.2, 1.8, -1.2], dtype=cupy.float32)
     with pytest.raises(TypeError):
         return f(a)