def __init__(self, first, second): ''' @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 not first.geoDataMatch(second): raise AreaAnalizerError('Geometries of the rasters are different!') if first.getBandsCount() + second.getBandsCount() > 2: raise AreaAnalizerError('Rasters mast have 1 band!') self.geodata = first.getGeodata() statFirst = first.getBandStat(1) statSecong = second.getBandStat(1) self.classes = statFirst['gradation'] self.classesSecond = statSecong['gradation'] first, second = masks_identity(first.getBand(1), second.getBand(1)) self.first = first self.second = second for cl in self.classesSecond: if cl not in self.classes: raise AreaAnalizerError("List of classes of the first raster doesn't contains a class of the second raster!") self.changeMap = None
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 __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 __init__(self, factors, areaAnalyst, unit_cell=1, bins = None): ''' @param factors List of the pattern rasters used for prediction of point objects (sites). @param areaAnalyst AreaAnalyst that contains map of the changes, encodes and decodes class numbers. @param unit_cell Method parameter, pixelsize of resampled rasters. @param bins Dictionary of bins. Bins are binning boundaries that used for reduce count of classes. For example if factors = [f0, f1], then bins could be (for example) {0:[bins for f0], 1:[bins for f1]} = {0:[[10, 100, 250]],1:[[0.2, 1, 1.5, 4]]}. List of list used because a factor can be a multiband raster, we need get a list of bins for every band. For example: factors = [f0, 2-band-factor], bins= {0: [[10, 100, 250]], 1:[[0.2, 1, 1.5, 4], [3, 4, 7]] } ''' self.factors = factors self.analyst = areaAnalyst self.changeMap = areaAnalyst.getChangeMap() self.prediction = None self.confidence = None if (bins != None) and (len(factors) != len(bins.keys())): raise WoeManagerError('Lengths of bins and factors are different!') for r in self.factors: if not self.changeMap.geoDataMatch(r): raise WoeManagerError('Geometries of the input rasters are different!') if self.changeMap.getBandsCount() != 1: raise WoeManagerError('Change map must have one band!') # Get list of codes from the changeMap raster classes = self.changeMap.getBandStat(1)['gradation'] cMap = self.changeMap.getBand(1) self.codes = [int(c) for c in classes] # Codes of transitions initState->finalState (see AreaAnalyst.encode) self.woe = {} for code in self.codes: sites = binaryzation(cMap, [code]) # TODO: reclass factors (continuous factor -> ordinal factor) wMap = np.ma.zeros(cMap.shape) for k in xrange(len(factors)): fact = factors[k] if bins: # Get bins of the factor bin = 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: band = reclass(band, bin[i-1]) band, sites = masks_identity(band, sites) # Combine masks of the rasters weights = woe(band, sites, unit_cell) # WoE for the 'code' (initState->finalState) transition and current 'factor'. wMap = wMap + weights self.woe[code]=wMap # WoE for all factors and the transition.
def correlation(X, Y): ''' Define correlation coefficient of the rasters. @param X First raster's array @param Y Second raster's array ''' X, Y = masks_identity(X, Y) R = np.corrcoef(np.ma.compressed(X),np.ma.compressed(Y)) # function np.corrcoef return 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 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 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])
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])
def __init__(self, band1, band2): if not sizes_equal(band1, band2): raise CrossTabError('Sizes of rasters are not equal!') band1, band2 = masks_identity(band1, band2) X = np.ma.compressed(band1) Y = np.ma.compressed(band2) # Compute gradations of the bands self.graduation_x = get_gradations(X) self.graduation_y = get_gradations(Y) rows, cols = len(self.graduation_x), len(self.graduation_y) self.shape = (rows, cols) # Compute crosstable self.T = np.zeros([rows, cols], dtype=int) self.n = len(X) # Count of unmasked elements (= sum of all elements of the table) for i in range(self.n): class_num_x = self.graduation_x.index(X[i]) class_num_y = self.graduation_y.index(Y[i]) self.T[class_num_x][class_num_y] +=1
def test_masks_identity(self): self.X, self.Y = masks_identity(self.X, self.Y) mask_x = np.matrix.flatten(self.X.mask) self.combo_mask = np.matrix.flatten(self.combo_mask) k = all(np.equal(mask_x, self.combo_mask)) self.assertEqual(k, True, 'masks_identify failed')