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 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()