GitHub PullRequestReviewCommentEvent at 2014-03-13 12:26:03
Repository: imagen
Actor: h-mayorquin
Payload: {"comment":{"url":"https://api.github.com/repos/ioam/imagen/pulls/comments/10561895","id":10561895,"diff_hunk":"@@ -64,14 +64,260 @@ def __call__(self,**params_to_override):\n             of(result)\n \n         return result\n+    \n \n-\n-\n+class DenseNoise(RandomGenerator):\n+    \"\"\"\n+    2D dense noise pattern generator with variable and free grid size\n+    \n+    By default this produces a matrix with random values 0,-1 and 1\n+    When a scale and an offset are provided the transformation maps them to:\n+    -1 -> offset - scale\n+     0 -> offset\n+     1 -> offset + scale \n+     \n+    if grid_size > 1 then instead of entries spots or boxes of size grid_size \n+    with -1, 0 or 1 will be mapped to the full matrix \n+ \n+    ---\n+    Examples \n+    ---\n+    SparseNoise(grid_size = 2, grid = True, bounds = BoundingBox(radius = 1),\n+    xdensity = 4, ydensity = 4) will produce something like this:\n+     \n+    [[ 1.  1.  1.  1.  0.  0.  0.  0.]\n+    [ 1.  1.  1.  1.  0.  0.  0.  0.]\n+    [ 1.  1.  1.  1.  0.  0.  0.  0.] <-- grid_size = 2 of this spots will\n+    [-1. -1. -1. -1. -1. -1. -1. -1.]     fill the matrix \n+    [-1. -1. -1. -1. -1. -1. -1. -1.]\n+    [-1. -1. -1. -1. -1. -1. -1. -1.]\n+    [-1. -1. -1. -1. -1. -1. -1. -1.]]\n+    \n+    SparseNoise(grid_size = 4, grid = True, bounds = BoundingBox(radius = 1),\n+    xdensity = 4, ydensity = 4) on the other hand will produce:\n+    \n+    [[ 1.  1.  0.  0. -1. -1. -1. -1.]\n+    [ 1.  1.  0.  0. -1. -1. -1. -1.]\n+    [ 1.  1. -1. -1.  0.  0.  1.  1.]\n+    [ 1.  1. -1. -1.  0.  0.  1.  1.] < -- grid_size = 4 of this spots will \n+    [ 0.  0.  0.  0.  1.  1.  1.  1.]      fill the matrix \n+    [ 0.  0.  0.  0.  1.  1.  1.  1.]\n+    [-1. -1.  0.  0.  0.  0.  0.  0.]\n+    [-1. -1.  0.  0.  0.  0.  0.  0.]]\n+    \n+    ---\n+    Notes \n+    ---\n+    1 ) This method works much faster when the size of the matrix of pixels \n+    is proportional to the grid_size  shape[0] % grid_size == 0 ( ~ 100 times faster)\n+    \n+    2 ) This method only works for square pixels matrix shape[0] == shape[1]\n+    \n+    3 ) In case that the a pixel has an intersection with two or more squares from the noise\n+    grid, the allocating of the value is done by taking into account where the center\n+    of the pixels lies\n+    \"\"\"\n+    \n+    grid_size = param.Integer(default=10, bounds=(1,None), doc=\"\"\"\n+    In a 10 x 10 grid this will be 10.\"\"\")\n+\n+    def _distrib(self, shape, p):\n+        \n+        assert (shape[0] == shape[1]),\" This method only works for square matrices\"\n+        assert (p.grid_size <= shape[0]),\" Size of the grid  must be smaller than the number of pixels\"\n+        \n+        N = shape[0] # Size of the pixel matrix \n+        n = p.grid_size #Size of the grid spots \n+        ps = int(round(N / n)) #Closest integer \n+        \n+        # If the noise grid is proportional to the pixel grid \n+        # and fits neatly into it then this method is faster (~100 times faster)\n+        if ( N % n == 0):\n+              \n+            if ps == 1:  #This is faster to call the whole procedure \n+                return p.random_generator.randint(-1, 2, shape) * p.scale + p.offset\n+            \n+            else: \n+                # This is the actual matrix of the pixels \n+                A = np.zeros(shape)    \n+                # Noise matrix that contains the structure of -1,0 and 1's \n+                Z = p.random_generator.randint(-1, 2, (n, n)) \n+                \n+                # Noise matrix is mapped to the pixel matrix   \n+                for i in range(n):\n+                    for j in range(n): \n+                        A[i * ps: (i + 1) * ps, j * ps: (j + 1) * ps] = Z[i,j]\n+                \n+                return A * p.scale + p.offset\n+            \n+        # General method in case the noise grid does not \n+        # fall neatly in the pixels grid      \n+        else:\n+        \n+            SC = SheetCoordinateSystem(p.bounds, p.xdensity, p.ydensity)\n+            x_points,y_points = SC.sheetcoordinates_of_matrixidx()\n+            \n+            # Obtain length of the side and length of the\n+            # division line between the grid \n+            unitary_distance = x_points[1] - x_points[0]\n+            side_length = round(unitary_distance * N)\n+            division = side_length / n\n+            \n+            # This is the actual matrix of the pixels \n+            A = np.zeros(shape)\n+            # Noise matrix that contains the structure of -1,0 and 1's \n+            Z = p.random_generator.randint(-1, 2, (n,n))\n+    \n+            # Noise matrix is mapped to the pixel matrix   \n+            for i in range(N):\n+                for j in range(N):\n+                    # Map along the x coordinates \n+                    aux1 = x_points[i] + (side_length / 2)\n+                    outcome1 = int(aux1 / division)\n+                    # Map along the y coordinates\n+                    aux2 = y_points[-(j+1)] + (side_length / 2)\n+                    outcome2 = int(aux2 / division)\n+                    # Assign \n+                    A[i][j] = Z[outcome1][outcome2]\n+            \n+            return A * p.scale + p.offset\n+\n+\n+class SparseNoise(RandomGenerator):\n+    '''\n+    2D sparse noise pattern generator with variable and free grid size\n+    \n+    In the default this produces a matrix with zeros everywhere except in one \n+    random entry. This value is randomly assigned to either  -1 or 1 and then\n+    is scaled with the parameters scale and offset in the following way\n+    \n+    -1 -> offset - scale\n+     1 -> offset + scale \n+     \n+     if grid_size > 1 then instead of entries spots or boxes of size grid_size \n+     with -1 or 1 will be mapped to the full matrix \n+          \n+    ---\n+    Example \n+    ---\n+    \n+    SparseNoise(grid_size = 2, grid = True, bounds = BoundingBox(radius = 1),\n+    xdensity = 4, ydensity = 4) will produce something like this\n+   \n+    [[ 0.  0.  0.  0. -1. -1. -1. -1.]\n+     [ 0.  0.  0.  0. -1. -1. -1. -1.] < -- grid_size = 2 of those spots \n+     [ 0.  0.  0.  0. -1. -1. -1. -1.]      will fill the matrix \n+     [ 0.  0.  0.  0. -1. -1. -1. -1.]\n+     [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+     [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+     [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+     [ 0.  0.  0.  0.  0.  0.  0.  0.]]    \n+     \n+    SparseNoise(grid_size = 4, grid = True, bounds = BoundingBox(radius = 1) ,\n+    xdensity = 4, ydensity = 4) on the other hand will produce:\n+     \n+    [[ 0.  0.  0.  0.  0.  0.  0.  0.]\n+    [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+    [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+    [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+    [ 0.  0.  0.  0.  0.  0. -1. -1.] < --- grid_size = 4 of those spots  \n+    [ 0.  0.  0.  0.  0.  0. -1. -1.]       will fill the matrix \n+    [ 0.  0.  0.  0.  0.  0.  0.  0.]\n+    [ 0.  0.  0.  0.  0.  0.  0.  0.]]\n+\n+    ---\n+    Notes \n+    ---\n+    1 ) This method works much faster when the size of the matrix of pixels \n+    is proportional to the grid_size  shape[0] % grid_size == 0 (~ 100 times faster)\n+    \n+    2 ) This method only works for square pixels matrix shape[0] = shape[1]\n+    \n+    3 ) In case that the a pixel has an intersection with two or more squares from the noise\n+    grid, the allocating of the value is done by taking into account where the center\n+    of the pixels lies\n+    '''\n+    \n+    grid_size = param.Integer(default=10, bounds=(1,None), doc=\"\"\"\n+    In a 10 x 10 grid this will be 10.\"\"\")\n+    grid = param.Boolean(default = True, doc=\"\"\" \n+    True - Forces the spots to appear in a grid\n+    False - The patterns can appear randomly anywhere d\"\"\")\n+\n+    \n+    def _distrib(self, shape, p):\n+        \n+        assert (shape[0] == shape[1]),\" This method only works for square matrices\"\n+        assert (p.grid_size <= shape[0]),\" Size of the grid  must be smaller than the number of pixels\"               \n+        \n+        N = shape[0] #Size of the pixel matrix\n+        n = p.grid_size #Size of the grid spots  \n+        ps = int(round( N / n )) #Closest integer \n+      \n+        # This is the actual matrix of the pixels \n+        A = np.zeros(shape)   \n+           \n+        if p.grid == False:  #The centers of the spots are randomly distributed in space \n+                        \n+            x = p.random_generator.randint(0, N - ps + 1)\n+            y = p.random_generator.randint(0, N - ps + 1)\n+            z = p.random_generator.choice([-1,1]) \n+            \n+            # Noise matrix is mapped to the pixel matrix   \n+            A[x: (x + ps), y: (y + ps)] = A[x: (x + ps), y: (y + ps)] + z   \n+           \n+            return A * p.scale + p.offset\n+        \n+        else: #In case you want the grid\n+            \n+            if ( N % n == 0): #When the noise grid falls neatly into the the pixel grid \n+                x = p.random_generator.randint(0, n)\n+                y = p.random_generator.randint(0, n)\n+                z = p.random_generator.choice([-1,1])\n+                ","path":"imagen/random.py","position":226,"original_position":216,"commit_id":"237223ccad431f98441025e5ae1898101d860d72","original_commit_id":"c030990a2db7e9309604e1211c7212725a1a6a29","user":{"login":"h-mayorquin","id":6429509,"avatar_url":"https://gravatar.com/avatar/74ecf682da64d7fda06ff6b2c143303d?d=https%3A%2F%2Fidenticons.github.com%2F1796de014f45051648b36a35352abd29.png&r=x","gravatar_id":"74ecf682da64d7fda06ff6b2c143303d","url":"https://api.github.com/users/h-mayorquin","html_url":"https://github.com/h-mayorquin","followers_url":"https://api.github.com/users/h-mayorquin/followers","following_url":"https://api.github.com/users/h-mayorquin/following{/other_user}","gists_url":"https://api.github.com/users/h-mayorquin/gists{/gist_id}","starred_url":"https://api.github.com/users/h-mayorquin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h-mayorquin/subscriptions","organizations_url":"https://api.github.com/users/h-mayorquin/orgs","repos_url":"https://api.github.com/users/h-mayorquin/repos","events_url":"https://api.github.com/users/h-mayorquin/events{/privacy}","received_events_url":"https://api.github.com/users/h-mayorquin/received_events","type":"User","site_admin":false},"body":"It is a matter of the version of numpy I guess. Choice is a recent function I think.","created_at":"2014-03-13T12:26:03Z","updated_at":"2014-03-13T12:26:03Z","html_url":"https://github.com/ioam/imagen/pull/19#discussion_r10561895","pull_request_url":"https://api.github.com/repos/ioam/imagen/pulls/19","_links":{"self":{"href":"https://api.github.com/repos/ioam/imagen/pulls/comments/10561895"},"html":{"href":"https://github.com/ioam/imagen/pull/19#discussion_r10561895"},"pull_request":{"href":"https://api.github.com/repos/ioam/imagen/pulls/19"}}}}