datagenerator.simplexNoise

  1import os
  2
  3import numpy as np
  4
  5import noise
  6
  7
  8def noise_3d(cube_shape, verbose=False):
  9    if verbose:
 10        print("   ... inside noise3D")
 11    noise3d = np.random.exponential(
 12        1.0 / 100.0, size=cube_shape[0] * cube_shape[1] * cube_shape[2]
 13    )
 14    sign = np.random.binomial(
 15        1, 0.5, size=cube_shape[0] * cube_shape[1] * cube_shape[2]
 16    )
 17    sign[sign == 0] = -1
 18    noise3d *= sign
 19    noise3d = noise3d.reshape((cube_shape[0], cube_shape[1], cube_shape[2]))
 20    return noise3d
 21
 22
 23def perlin(xsize, ysize, base=None, octave=1, lac=1.9, do_rotate=True):
 24    # print "   ...inside perlin"
 25    if base is None:
 26        base = np.random.randint(255)
 27    temp = np.array(
 28        [
 29            [
 30                noise.pnoise2(
 31                    float(i) / xsize,
 32                    float(j) / ysize,
 33                    lacunarity=lac,
 34                    octaves=octave,
 35                    base=base,
 36                )
 37                for j in range(ysize)
 38            ]
 39            for i in range(xsize)
 40        ]
 41    )
 42    # randomly rotate image
 43    if do_rotate:
 44        if xsize == ysize and np.random.binomial(1, 0.5) == 1:
 45            number_90_deg_rotations = int(np.random.uniform(1, 4))
 46            temp = np.rot90(temp, number_90_deg_rotations)
 47        # randomly flip left and right, top and bottom
 48        if np.random.binomial(1, 0.5) == 1:
 49            temp = np.fliplr(temp)
 50        if np.random.binomial(1, 0.5) == 1:
 51            temp = np.flipud(temp)
 52    return temp
 53
 54
 55def noise_2d(xsize, ysize, threshold, octaves=9):
 56    for i in range(25):
 57        im = perlin(xsize, ysize, octave=octaves, lac=1.9)
 58
 59        im_x = np.mean(im, axis=0)
 60        im_x1 = np.mean(im[: -im.shape[0] / 2, :], axis=0)
 61        im_x2 = np.mean(im[im.shape[0] / 2 :, :], axis=0)
 62        nxcor1 = np.mean((im_x - im_x.mean()) * (im_x1 - im_x1.mean())) / (
 63            im_x.std() * im_x1.std()
 64        )
 65        nxcor2 = np.mean((im_x - im_x.mean()) * (im_x2 - im_x2.mean())) / (
 66            im_x.std() * im_x2.std()
 67        )
 68        nxcor3 = np.mean((im_x1 - im_x1.mean()) * (im_x2 - im_x2.mean())) / (
 69            im_x1.std() * im_x2.std()
 70        )
 71        test_x = np.mean((nxcor1, nxcor2, nxcor3))
 72        if np.isnan(test_x):
 73            test_x = 1.0
 74
 75        im_y = np.mean(im, axis=1)
 76        im_y1 = np.mean(im[:, : -im.shape[0] / 2], axis=1)
 77        im_y2 = np.mean(im[:, im.shape[0] / 2 :], axis=1)
 78        nycor1 = np.mean((im_y - im_y.mean()) * (im_y1 - im_y1.mean())) / (
 79            im_y.std() * im_y1.std()
 80        )
 81        nycor2 = np.mean((im_y - im_y.mean()) * (im_y2 - im_y2.mean())) / (
 82            im_y.std() * im_y2.std()
 83        )
 84        nycor3 = np.mean((im_y1 - im_y1.mean()) * (im_y2 - im_y2.mean())) / (
 85            im_y1.std() * im_y2.std()
 86        )
 87        test_y = np.mean((nycor1, nycor2, nycor3))
 88        if np.isnan(test_y):
 89            test_y = 1.0
 90
 91        print(
 92            i,
 93            (test_x, test_y),
 94            "    thresholds = ",
 95            (threshold, 1.0 / threshold),
 96            threshold < test_x < 1.0 / threshold
 97            or threshold < test_y < 1.0 / threshold,
 98        )
 99
100        if test_x > threshold or test_y > threshold:
101            continue
102        else:
103            return im, test_x, test_y
104
105    return im, test_x, test_y
def noise_3d(cube_shape, verbose=False):
 9def noise_3d(cube_shape, verbose=False):
10    if verbose:
11        print("   ... inside noise3D")
12    noise3d = np.random.exponential(
13        1.0 / 100.0, size=cube_shape[0] * cube_shape[1] * cube_shape[2]
14    )
15    sign = np.random.binomial(
16        1, 0.5, size=cube_shape[0] * cube_shape[1] * cube_shape[2]
17    )
18    sign[sign == 0] = -1
19    noise3d *= sign
20    noise3d = noise3d.reshape((cube_shape[0], cube_shape[1], cube_shape[2]))
21    return noise3d
def perlin(xsize, ysize, base=None, octave=1, lac=1.9, do_rotate=True):
24def perlin(xsize, ysize, base=None, octave=1, lac=1.9, do_rotate=True):
25    # print "   ...inside perlin"
26    if base is None:
27        base = np.random.randint(255)
28    temp = np.array(
29        [
30            [
31                noise.pnoise2(
32                    float(i) / xsize,
33                    float(j) / ysize,
34                    lacunarity=lac,
35                    octaves=octave,
36                    base=base,
37                )
38                for j in range(ysize)
39            ]
40            for i in range(xsize)
41        ]
42    )
43    # randomly rotate image
44    if do_rotate:
45        if xsize == ysize and np.random.binomial(1, 0.5) == 1:
46            number_90_deg_rotations = int(np.random.uniform(1, 4))
47            temp = np.rot90(temp, number_90_deg_rotations)
48        # randomly flip left and right, top and bottom
49        if np.random.binomial(1, 0.5) == 1:
50            temp = np.fliplr(temp)
51        if np.random.binomial(1, 0.5) == 1:
52            temp = np.flipud(temp)
53    return temp
def noise_2d(xsize, ysize, threshold, octaves=9):
 56def noise_2d(xsize, ysize, threshold, octaves=9):
 57    for i in range(25):
 58        im = perlin(xsize, ysize, octave=octaves, lac=1.9)
 59
 60        im_x = np.mean(im, axis=0)
 61        im_x1 = np.mean(im[: -im.shape[0] / 2, :], axis=0)
 62        im_x2 = np.mean(im[im.shape[0] / 2 :, :], axis=0)
 63        nxcor1 = np.mean((im_x - im_x.mean()) * (im_x1 - im_x1.mean())) / (
 64            im_x.std() * im_x1.std()
 65        )
 66        nxcor2 = np.mean((im_x - im_x.mean()) * (im_x2 - im_x2.mean())) / (
 67            im_x.std() * im_x2.std()
 68        )
 69        nxcor3 = np.mean((im_x1 - im_x1.mean()) * (im_x2 - im_x2.mean())) / (
 70            im_x1.std() * im_x2.std()
 71        )
 72        test_x = np.mean((nxcor1, nxcor2, nxcor3))
 73        if np.isnan(test_x):
 74            test_x = 1.0
 75
 76        im_y = np.mean(im, axis=1)
 77        im_y1 = np.mean(im[:, : -im.shape[0] / 2], axis=1)
 78        im_y2 = np.mean(im[:, im.shape[0] / 2 :], axis=1)
 79        nycor1 = np.mean((im_y - im_y.mean()) * (im_y1 - im_y1.mean())) / (
 80            im_y.std() * im_y1.std()
 81        )
 82        nycor2 = np.mean((im_y - im_y.mean()) * (im_y2 - im_y2.mean())) / (
 83            im_y.std() * im_y2.std()
 84        )
 85        nycor3 = np.mean((im_y1 - im_y1.mean()) * (im_y2 - im_y2.mean())) / (
 86            im_y1.std() * im_y2.std()
 87        )
 88        test_y = np.mean((nycor1, nycor2, nycor3))
 89        if np.isnan(test_y):
 90            test_y = 1.0
 91
 92        print(
 93            i,
 94            (test_x, test_y),
 95            "    thresholds = ",
 96            (threshold, 1.0 / threshold),
 97            threshold < test_x < 1.0 / threshold
 98            or threshold < test_y < 1.0 / threshold,
 99        )
100
101        if test_x > threshold or test_y > threshold:
102            continue
103        else:
104            return im, test_x, test_y
105
106    return im, test_x, test_y