def test_LR(self): data = [ [3.0, 1.0, 3.0], [3.0, 1.0, 3.0], [0, 3.0, 1.0] ] result = np.ma.array(data = data, mask = (data==0)) lr = LR(ns=0) # 3-class problem lr.setTrainingData(self.state, self.factors, self.output) lr.train() predict = lr.getPrediction(self.state, self.factors) predict = predict.getBand(1) assert_array_equal(predict, result) lr = LR(ns=1) # Two-class problem (it's because of boundary effect) lr.setTrainingData(self.state1, self.factors1, self.output1) lr.train() predict = lr.getPrediction(self.state1, self.factors1) predict = predict.getBand(1) data = [ [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 2.0, 2.0, 0.0], [0.0, 0.0, 0.0, 0.0], ] result = np.ma.array(data = data, mask = (data==0)) assert_array_equal(predict, result)
def test_LR(self): #~ data = [ #~ [3.0, 1.0, 3.0], #~ [3.0, 1.0, 3.0], #~ [0, 3.0, 1.0] #~ ] #~ result = np.ma.array(data = data, mask = (data==0)) lr = LR(ns=0) # 3-class problem lr.setState(self.state) lr.setFactors(self.factors) lr.setOutput(self.output) lr.setTrainingData() lr.train() predict = lr.getPrediction(self.state, self.factors) predict = predict.getBand(1) assert_array_equal(predict, self.output.getBand(1)) lr = LR(ns=1) # Two-class problem (it's because of boundary effect) lr.setState(self.state1) lr.setFactors(self.factors1) lr.setOutput(self.output1) lr.setTrainingData() lr.train() predict = lr.getPrediction(self.state1, self.factors1, calcTransitions=True) predict = predict.getBand(1) self.assertEquals(predict.dtype, np.uint8) data = [ [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 2.0, 2.0, 0.0], [0.0, 0.0, 0.0, 0.0], ] result = np.ma.array(data=data, mask=(data == 0)) assert_array_equal(predict, result) # Confidence is zero confid = lr.getConfidence() self.assertEquals(confid.getBand(1).dtype, np.uint8) # Transition Potentials potentials = lr.getTransitionPotentials() cats = self.output.getBandGradation(1) for cat in [1.0, 2.0]: map = potentials[cat] self.assertEquals(map.getBand(1).dtype, np.uint8)
def test_LR(self): #~ data = [ #~ [3.0, 1.0, 3.0], #~ [3.0, 1.0, 3.0], #~ [0, 3.0, 1.0] #~ ] #~ result = np.ma.array(data = data, mask = (data==0)) lr = LR(ns=0) # 3-class problem lr.setState(self.state) lr.setFactors(self.factors) lr.setOutput(self.output) lr.setTrainingData() lr.train() predict = lr.getPrediction(self.state, self.factors) predict = predict.getBand(1) assert_array_equal(predict, self.output.getBand(1)) lr = LR(ns=1) # Two-class problem (it's because of boundary effect) lr.setState(self.state1) lr.setFactors(self.factors1) lr.setOutput(self.output1) lr.setTrainingData() lr.train() predict = lr.getPrediction(self.state1, self.factors1, calcTransitions=True) predict = predict.getBand(1) self.assertEquals(predict.dtype, np.uint8) data = [ [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 2.0, 2.0, 0.0], [0.0, 0.0, 0.0, 0.0], ] result = np.ma.array(data = data, mask = (data==0)) assert_array_equal(predict, result) # Confidence is zero confid = lr.getConfidence() self.assertEquals(confid.getBand(1).dtype, np.uint8) # Transition Potentials potentials = lr.getTransitionPotentials() cats = self.output.getBandGradation(1) for cat in [1.0, 2.0]: map = potentials[cat] self.assertEquals(map.getBand(1).dtype, np.uint8)
def main(initRaster, finalRaster, factors): print 'Start Reading Init Data...', clock() initRaster = Raster(initRaster) finalRaster = Raster(finalRaster) factors = [Raster(rasterName) for rasterName in factors] print 'Finish Reading Init Data', clock(), '\n' print "Start Making CrossTable...", clock() crosstab = CrossTableManager(initRaster, finalRaster) print "Finish Making CrossTable", clock(), '\n' # Create and Train LR Model model = LR(ns=1) print 'Start Setting LR Trainig Data...', clock() model.setTrainingData(initRaster, factors, finalRaster, mode='Stratified', samples=1000) print 'Finish Setting Trainig Data', clock(), '\n' print 'Start LR Training...', clock() model.train() print 'Finish Trainig', clock(), '\n' print 'Start LR Prediction...', clock() predict = model.getPrediction(initRaster, factors) filename = 'lr_predict.tiff' try: predict.save(filename) finally: os.remove(filename) print 'Finish LR Prediction...', clock(), '\n' # simulation print 'Start Simulation...', clock() simulator = Simulator(initRaster, factors, model, crosstab) # Make 1 cycle of simulation: simulator.simN(1) monteCarloSim = simulator.getState() # Result of MonteCarlo simulation errors = simulator.errorMap(finalRaster) # Risk class validation riskFunct = simulator.getConfidence() # Risk function # Make K cycles of simulation: # simulator.simN(K) try: monteCarloSim.save('simulation_result.tiff') errors.save('risk_validation.tiff') riskFunct.save('risk_func.tiff') finally: pass os.remove('simulation_result.tiff') os.remove('risk_validation.tiff') os.remove('risk_func.tiff') print 'Finish Simulation', clock(), '\n' print 'Done', clock()