示例#1
0
    def _distrib(self, shape, p):
        assert ( p.grid_density <= p.xdensity ), 'grid density bigger than pixel density x'
        assert ( p.grid_density <= p.ydensity),  'grid density bigger than pixel density y'
        assert ( shape[1] > 0 ), 'Pixel matrix can not be zero'
        assert ( shape[0] > 0 ),  'Pixel matrix can not be zero'
        
        Nx = shape[1]  
        Ny = shape[0] # Size of the pixel matrix 
        
      
        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 smal'
        assert ( ny > 0 ), 'Grid density or bound bonx 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 
                
                division_x = 1.0 / p.grid_density
                division_y = 1.0 / p.grid_density

                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
示例#2
0
    def _distrib(self, shape, p):               
        assert ( p.grid_density <= p.xdensity ), 'grid density bigger than pixel density x'
        assert ( p.grid_density <= p.ydensity),  'grid density bigger than pixel density y'
        assert ( shape[1] > 0 ), 'Pixel matrix can not be zero'
        assert ( shape[0] > 0 ),  'Pixel matrix can not be zero'
        
        Nx = shape[1]  
        Ny = shape[0] # Size of the pixel matrix 
      
        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 smal'
        assert ( ny > 0 ), 'Grid density or bound bonx in the y dimension too smal'
        
        # If the noise grid is proportional to the pixel grid 
        # and fits neatly into it then this method is faster (~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 pixels grid      
        else:
            
            # Obtain length of the side and length of the
            # division line between the grid 
            x_points,y_points = SC.sheetcoordinates_of_matrixidx()

            division_x = 1.0 / p.grid_density
            division_y = 1.0 / p.grid_density
            
            # 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