def setupGrid( self, matrix ): if self.__test: self.__rows = 101 self.__cols = 101 self.__gridWidget = GridView( self, self.__rows, self.__cols, False ) self.__layout.addWidget( self.__gridWidget, 0, 0 ) b_overwriteSpectralValue = True maxEnergy = 255 / 3 automaton = C_spectrum_CAM_RGB( maxQuanta=maxEnergy ) automaton.component_add( 'R', maxEnergy / 3, b_overwriteSpectralValue ) automaton.component_add( 'G', maxEnergy / 3, b_overwriteSpectralValue ) automaton.component_add( 'B', maxEnergy / 3, b_overwriteSpectralValue ) automaton.updateRule_changeAmount(self.__updateAmount) world = C_CAE( np.array( ( self.__rows, self.__cols ) ), automaton ) world.verbosity_set( 1 ) arr_world = np.zeros( ( self.__rows, self.__cols ) ) arr_world[0, 0] = 1 arr_world[50, 50] = maxEnergy / 3 + 1 arr_world[100, 100] = maxEnergy / 3 * 2 + 1 elif matrix: maxEnergy = 255 arr_worldRaw = np.loadtxt( matrix, float, '#', '\t' ) arr_world = misc.arr_normalize( arr_worldRaw, scale=maxEnergy ) self.__rows, self.__cols = arr_world.shape self.__gridWidget = GridView( self, self.__rows, self.__cols, False ) self.__layout.addWidget( self.__gridWidget, 0, 0 ) b_overwriteSpectralValue = True automaton = C_spectrum_CAM_RGB( maxQuanta=maxEnergy ) automaton.component_add( 'R', maxEnergy / 3, b_overwriteSpectralValue ) automaton.component_add( 'G', maxEnergy / 3, b_overwriteSpectralValue ) automaton.component_add( 'B', maxEnergy / 3, b_overwriteSpectralValue ) print "Update amount = %d" % self.__updateAmount automaton.updateRule_changeAmount(self.__updateAmount) world = C_CAE( np.array( ( self.__rows, self.__cols ) ), automaton ) world.verbosity_set( 1 ) else: c.error( 'No test mode and no matrix..' ) sys.exit() print arr_world world.initialize( arr_world ) self.__world = world
def initialize( self, *args, **kwargs ): """ ARGS *args[0] nparray initialize each CAM with corresponding element of nparray. Assumes that size(nparray) == size(grid). Passes array value at [row, col] to CAM at [row, col]. DESC Generates an initial distribution across the world grid. KWARGS The pattern of initialization is specified by the kwargs: pattern = "random" | "corners" | "diagonal" Choose elements either randomly on the grid, or only on the corners, or only along the diagonal. elements = "canonical" | <N> | "all" "canonical" The number of grid elements to initialize. If "canonical", initialize only as many elements as there are spectral components in the CAM. Each successive element initializes a single successive spectral component. <N> Initialize <N> elements. "all" Initialize all elements. PRECONIDTIONS: o Internal grids must exist and contain valid objects. POSTCONDITIONS: o The "current" and "next" are initialized based on pattern of args. o If an array is passed as first unnamed argument, values in the array are used to initialize the grid (provided that the array is the same size as grid). In such a case, all kwargs are ignored. If array size is not same as grid, no initialization is performed. """ l_components = self.mgg_current.spectrum_get( 0, 0 ).spectrumKeys_get() numComponents = len( l_components ) maxEnergy = 255 maxQuanta = maxEnergy / numComponents if len( args ): a_init = args[0] if type( a_init ).__name__ == 'ndarray': rows, cols = a_init.shape if rows == self.m_rows and cols == self.m_cols: # In this case, an initialization matrix has been supplied # by the caller. The value at each cell index is used # to initialize the corresponding spectrum object. In order # for the initialization to be "uniform" across the RGB # space, we need to re-scale the observed values such that # a uniform partitioning of the domain results in a uniform # partitioning of the values, too. a_norm = misc.arr_normalize( a_init, scale=maxEnergy ) a_round = a_norm.round() # We bin the cdf with a '+1' since the 'zeros' in the # input matrix are a special case. This also simplifies # the value lookup in the cdf partitions. a_cdf = misc.cdf( a_round, bins=a_round.max() + 1 ) l_v = misc.cdf_distribution( a_cdf, numComponents ) for row in np.arange( 0, rows ): for col in np.arange( 0, cols ): value = a_round[row, col] self.mgg_current.spectrum_get( row, col ).\ spectrum_init( value, l_v ) # self.mgg_next = copy.deepcopy( self.mgg_current ) # use cPickle instead of deepcopy self.mgg_next = cPickle.loads( cPickle.dumps( self.mgg_current, -1 ) )