def __init__(self, first, second=None): ''' @param first Raster of the first stage (the state before transition). @param second Raster of the second stage (the state after transition). ''' QObject.__init__(self) if second != None and (not first.geoDataMatch(second)): raise AreaAnalizerError('Geometries of the rasters are different!') if first.getBandsCount() != 1: raise AreaAnalizerError('First raster mast have 1 band!') if second !=None and second.getBandsCount() != 1: raise AreaAnalizerError('Second raster mast have 1 band!') self.geodata = first.getGeodata() self.categories = first.getBandGradation(1) if second != None: self.categoriesSecond = second.getBandGradation(1) first, second = masks_identity(first.getBand(1), second.getBand(1), dtype=np.uint8) self.first = first self.second = second if second != None: for cat in self.categoriesSecond: if cat not in self.categories: raise AreaAnalizerError("List of categories of the first raster doesn't contains a category of the second raster!") self.changeMap = None self.initRaster = None self.persistentCategoryCode = -1
def __init__(self, band1, band2, expand = False): """ @param band1 First band (numpy masked array) @param band2 Second band (numpy masked array) @param expand If the param is True, use union of categories of the bands and compute NxN crosstable """ QObject.__init__(self) if not sizes_equal(band1, band2): raise CrossTabError('Sizes of rasters are not equal!') band1, band2 = masks_identity(band1, band2, dtype=np.uint8) self.X = np.ma.compressed(band1).flatten() self.Y = np.ma.compressed(band2).flatten() # Compute gradations of the bands self.graduation_x = get_gradations(self.X) self.graduation_y = get_gradations(self.Y) if expand: self.graduation_x = list(set(self.graduation_x + self.graduation_y)) self.graduation_y = self.graduation_x rows, cols = len(self.graduation_x), len(self.graduation_y) self.shape = (rows, cols) self._T = None # Crosstable self.n = None # Count of elements in the crosstable
def train(self): ''' Train the model ''' self.transitionPotentials = {} try: iterCount = len(self.codes)*len(self.factors) self.rangeChanged.emit(self.tr("Training WoE... %p%"), iterCount) changeMap = self.changeMap.getBand(1) for code in self.codes: sites = binaryzation(changeMap, [code]) # Reclass factors (continuous factor -> ordinal factor) wMap = np.ma.zeros(changeMap.shape) # The map of summary weight of the all factors self.weights[code] = {} # Dictionary for storing wheights of every raster's band for k in xrange(len(self.factors)): fact = self.factors[k] self.weights[code][k] = {} # Weights of the factor factorW = self.weights[code][k] if self.bins: # Get bins of the factor bin = self.bins[k] if (bin != None) and fact.getBandsCount() != len(bin): raise WoeManagerError("Count of bins list for multiband factor is't equal to band count!") else: bin = None for i in range(1, fact.getBandsCount()+1): band = fact.getBand(i) if bin and bin[i-1]: # band = reclass(band, bin[i-1]) band, sites = masks_identity(band, sites, dtype=np.uint8) # Combine masks of the rasters woeRes = woe(band, sites, self.unit_cell) # WoE for the 'code' (initState->finalState) transition and current 'factor'. weights = woeRes['map'] wMap = wMap + weights factorW[i] = woeRes['weights'] self.updateProgress.emit() # Reclassification finished => set WoE coefficients self.woe[code]=wMap # WoE for all factors and the transition code. # Potentials are WoE map rescaled to 0--100 percents band = (sigmoid(wMap)*100).astype(np.uint8) p = Raster() p.create([band], self.geodata) self.transitionPotentials[code] = p gc.collect() except MemoryError: self.errorReport.emit('The system out of memory during WoE trainig') raise except: self.errorReport.emit(self.tr("An unknown error occurs during WoE trainig")) raise finally: self.processFinished.emit()
def correlation(self): ''' Define correlation coefficient of the rasters. ''' x, y = masks_identity(self.X.flatten(), self.Y.flatten()) x, y = np.ma.compressed(x), np.ma.compressed(y) R = np.corrcoef(x, y) del x del y # function np.corrcoef returns array of coefficients # R[0][0] = R[1][1] = 1.0 - correlation X--X and Y--Y # R[0][1] = R[1][0] - correlation X--Y and Y--X return R[0][1]
def __init__ (self, referenceMap, simulatedMap): """ @param referenceMap Reference raster @param simulatedMap Simulated raster """ QObject.__init__(self) if referenceMap.getBandsCount() + simulatedMap.getBandsCount() !=2: raise EBError('The reference and simulated rasters must be 1-band rasters!') if not referenceMap.geoDataMatch(simulatedMap): raise EBError('Geometries of the reference and simulated rasters are different!') self.categories = referenceMap.getBandGradation(1) for s in simulatedMap.getBandGradation(1): if not s in self.categories: raise EBError('Categories in the reference and simulated rasters are different!') R = referenceMap.getBand(1) S = simulatedMap.getBand(1) self.shape = R.shape R, S = masks_identity(R,S, dtype=np.uint8) # Array for weight self.W = np.ones(self.shape) self.W = self.W - np.ma.getmask(R) R = np.ma.filled(R, 0) S = np.ma.filled(S, 0) # Proportion of category j in pixel n at the beginning resolution of the reference map self.Rj = {} for j in self.categories: self.Rj[j] = 1.0*binaryzation(R, [j]) # Proportion of category j in pixel n at the beginning resolution of the simulated map self.Sj = {} for j in self.categories: self.Sj[j] = 1.0*binaryzation(S, [j])