Exemplo n.º 1
0
    def _distrib(self, shape, p):
        max_density = min(p.xdensity,p.ydensity)
        if (p.grid_density > max_density and not hasattr(self,"warned_about_density")):
            self.warning("Requested grid_density %s larger than xdensity %s or ydensity %s; capped at %s" %
                         (p.grid_density,p.xdensity,p.ydensity,max_density))
            p.grid_density = max_density
            self.warned_about_density=True

        Nx = shape[1]
        Ny = shape[0] # Size of the pixel matrix

        assert (Nx>0 and Ny>0), 'Pixel matrix cannot be zero'

        SC = SheetCoordinateSystem(p.bounds, p.xdensity, p.ydensity)
        unitary_distance_x = SC._SheetCoordinateSystem__xstep
        unitary_distance_y = SC._SheetCoordinateSystem__ystep

        sheet_x_size = unitary_distance_x * Nx
        sheet_y_size = unitary_distance_y * Ny

        # Sizes of the structure matrix
        nx = int(round(sheet_x_size * p.grid_density))  # Number of points in the x's
        ny = int(round(sheet_y_size * p.grid_density))  # Number of points in the y's

        assert ( nx > 0 ), 'Grid density or bound box in the x dimension too small'
        assert ( ny > 0 ), 'Grid density or bound box in the y dimension too smal'

        ps_x = int(round(Nx / nx)) #Closest integer
        ps_y = int(round(Ny / ny))

        # This is the actual matrix of the pixels
        A = np.ones(shape) * 0.5

        if p.grid == False:  #The centers of the spots are randomly distributed in space

            x = p.random_generator.randint(0, Nx - ps_x + 1)
            y = p.random_generator.randint(0, Ny - ps_y + 1)
            z = p.random_generator.randint(0,2)

            # Noise matrix is mapped to the pixel matrix
            A[x: (x + ps_y), y: (y + ps_x)] =  z

            return A * p.scale + p.offset

        else: #In case you want the grid

            if  ( Nx % nx == 0) and (Ny % ny == 0): #When the noise grid falls neatly into the the pixel grid
                x = p.random_generator.randint(0, nx)
                y = p.random_generator.randint(0, ny)
                z = p.random_generator.randint(0,2)

               # Noise matrix is mapped to the pixel matrix (faster method)
                A[x*ps_y: (x*ps_y + ps_y), y*ps_x: (y*ps_x + ps_x)] = z

                return A * p.scale + p.offset

            else: # If noise grid does not fit neatly in the pixel grid (slow method)

                x_points,y_points = SC.sheetcoordinates_of_matrixidx()

                # Obtain length of the side and length of the
                # division line between the grid

                size_of_block_x = Nx * 1.0 / nx
                size_of_block_y = Ny * 1.0 / ny

                # Construct the noise matrix
                Z = np.ones((nx,ny)) * 0.5
                x = p.random_generator.randint(0, nx)
                y = p.random_generator.randint(0, ny)
                z = p.random_generator.randint(0,2)
                Z[x,y] = z

                            # Noise matrix is mapped to the pixel matrix
                for i in range(Nx):
                    for j in range(Ny):
                        # Map along the x coordinates
                        x_entry = int( i / size_of_block_x)
                        y_entry = int( j / size_of_block_y)
                        A[j][i] = Z[x_entry][y_entry]

                return A * p.scale + p.offset
Exemplo n.º 2
0
    def _distrib(self, shape, p):
        max_density = min(p.xdensity,p.ydensity)
        if (p.grid_density > max_density and not hasattr(self,"warned_about_density")):
            self.warning("Requested grid_density %s larger than xdensity %s or ydensity %s; capped at %s" %
                         (p.grid_density,p.xdensity,p.ydensity,max_density))
            p.grid_density = max_density
            self.warned_about_density=True

        Nx = shape[1]
        Ny = shape[0] # Size of the pixel matrix

        assert (Nx>0 and Ny>0), 'Pixel matrix cannot be zero'

        SC = SheetCoordinateSystem(p.bounds, p.xdensity, p.ydensity)
        unitary_distance_x = SC._SheetCoordinateSystem__xstep
        unitary_distance_y = SC._SheetCoordinateSystem__ystep


        sheet_x_size = unitary_distance_x * Nx
        sheet_y_size = unitary_distance_y * Ny

        # Sizes of the structure matrix
        nx = int(round(sheet_x_size * p.grid_density))  # Number of points in the x's
        ny = int(round(sheet_y_size * p.grid_density))  # Number of points in the y's

        assert ( nx > 0 ), 'Grid density or bound box in the x dimension too small'
        assert ( ny > 0 ), 'Grid density or bound box in the y dimension too small'

        # If the noise grid is proportional to the pixel grid and fits
        # neatly into it then this method is ~100 times faster
        if ( Nx % nx == 0) and (Ny % ny == 0):

            if (Nx == nx) and (Ny == ny):  #This is faster to call the whole procedure
                result = 0.5 * (p.random_generator.randint(-1, 2, shape) + 1)
                return  result * p.scale + p.offset

            else:
                # This is the actual matrix of the pixels
                A = np.zeros(shape)
                # Noise matrix that contains the structure of 0, 0.5, and 1's
                Z = 0.5 * (p.random_generator.randint(-1, 2, (nx, ny)) + 1 )

                ps_x = int(round(Nx * 1.0/ nx))  #Closest integer
                ps_y = int(round(Ny * 1.0/ ny))

                # Noise matrix is mapped to the pixel matrix
                for i in range(nx):
                    for j in range(ny):
                        A[i * ps_y: (i + 1) * ps_y, j * ps_x: (j + 1) * ps_x] = Z[i,j]

                return A * p.scale + p.offset

        # General method in case the noise grid does not
        # fall neatly in the pixel grid
        else:

            # Obtain length of the side and length of the
            # division line between the grid
            x_points,y_points = SC.sheetcoordinates_of_matrixidx()

            # This is the actual matrix of the pixels
            A = np.zeros(shape)
            # Noise matrix that contains the structure of 0, 0.5, and 1's
            Z = 0.5 * (p.random_generator.randint(-1, 2, (nx, ny)) + 1 )

            size_of_block_x = Nx * 1.0 / nx
            size_of_block_y = Ny * 1.0 / ny

            # Noise matrix is mapped to the pixel matrix
            for i in range(Nx):
                for j in range(Ny):
                    # Map along the x coordinates
                    x_entry = int( i / size_of_block_x)
                    y_entry = int( j / size_of_block_y)
                    A[j][i] = Z[x_entry][y_entry]

            return A * p.scale + p.offset