예제 #1
0
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()
예제 #2
0
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()
예제 #3
0
    def test_compute_table(self):

        # print self.crosstab.T
        # CrossTab:
        #  [[ 3.  1.  0.]           
        #   [ 0.  1.  0.]           
        #   [ 1.  0.  2.]]          
        # prediction = self.model.getPrediction()
        # prediction = [[2.0 2.0 1.0]
                     #  [1.0 3.0 1.0]
                     #  [-- 1.0 2.0]]
        # confidence = self.model.getConfidence()
        # confidence =     [[1.0 0.5  0.33]
                         #  [0.5 0.33 0.25]
                         #  [--  0.25 0.2]]
        
        
        result = np.array([
            [2.0, 1.0, 3.0],
            [1.0, 2.0, 1.0],
            [0,   3.0, 1.0]
        ])
        result = np.ma.array(result, mask = (result==0))
        
        simulator = Simulator(self.raster1, None, self.model, self.crosstab)    # The model does't use factors
        simulator.sim()
        state = simulator.getState().getBand(1)
        assert_array_equal(result, state)
        
        result = np.array([
            [2.0, 2.0, 1.0],
            [1.0, 2.0, 1.0],
            [0,   3.0, 1.0]
        ])
        result = np.ma.array(result, mask = (result==0))
        
        simulator = Simulator(self.raster1, None, self.model, self.crosstab)
        simulator.simN(2)
        state = simulator.getState().getBand(1)
        assert_array_equal(result, state)
예제 #4
0
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 ANN Model
    #~ model = MlpManager(ns=0)
    #~ model.createMlp(initRaster, factors, finalRaster, [10])
    #~ print 'Start Setting MLP Trainig Data...', clock()
    #~ model.setTrainingData(initRaster, factors, finalRaster, mode='Balanced', samples=1000)
    #~ print 'Finish Setting Trainig Data', clock(), '\n'
    #~ print 'Start MLP Training...', clock()
    #~ model.train(1000, valPercent=20)
    #~ print 'Finish Trainig', clock(), '\n'
    #~
    #~ print 'Start ANN Prediction...', clock()
    #~ predict = model.getPrediction(initRaster, factors)
    #~ filename = 'ann_predict.tiff'
    #~ try:
        #~ predict.save(filename)
    #~ finally:
        #~ #os.remove(filename)
        #~ pass
    #~ print 'Finish ANN Prediction...', clock(), '\n'

    #~ # Create and Train LR Model
    #~ model = LR(ns=1)
    #~ print 'Start Setting LR Trainig Data...', clock()
    #~ model.setTrainingData(initRaster, factors, finalRaster, mode='Balanced', 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)
    #~ pass
    #~ print 'Finish LR Prediction...', clock(), '\n'

    #~ # Create and Train WoE Model
    #~ print 'Start creating AreaAnalyst...', clock()
    #~ analyst = AreaAnalyst(initRaster, finalRaster)
    #~ print 'Finish creating AreaAnalyst ...', clock(), '\n'
    #~ print 'Start creating WoE model...', clock()
    #~ bins = {0: [[1000, 2000, 3000]], 1: [[200, 500, 1000, 1500]]}
    #~ model = WoeManager(factors, analyst, bins= bins)
    #~ print 'Finish creating WoE model...', clock(), '\n'

    # Create and Train MCE Model
    print 'Start creating MCE model...', clock()
    matrix = [
        [1,     6],
        [1.0/6,   1]
    ]
    model = MCE(factors, matrix, 2, 3)
    print 'Finish creating MCE model...', clock(), '\n'

    # simulation
    print 'Start Simulation...', clock()
    simulator = Simulator(initRaster, factors, model, crosstab)
    # Make 1 cycle of simulation:
    simulator.sim()
    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()
예제 #5
0
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 crosstab.getTransitionStat()
    print "Finish Making CrossTable", clock(), '\n'

    # Create and Train Analyst
    print 'Start creating AreaAnalyst...', clock()
    analyst = AreaAnalyst(initRaster, finalRaster)
    print 'Finish creating AreaAnalyst ...', clock(), '\n'

    print 'Start Making Change Map...', clock()
    analyst = AreaAnalyst(initRaster, finalRaster)
    changeMap = analyst.getChangeMap()
    print 'Finish Making Change Map', clock(), '\n'

    #~ # Create and Train ANN Model
    model = MlpManager(ns=1)
    model.createMlp(initRaster, factors, changeMap, [10])
    print 'Start Setting MLP Trainig Data...', clock()
    model.setTrainingData(initRaster,
                          factors,
                          changeMap,
                          mode='Stratified',
                          samples=1000)
    print 'Finish Setting Trainig Data', clock(), '\n'
    print 'Start MLP Training...', clock()
    model.train(20, valPercent=20)
    print 'Finish Trainig', clock(), '\n'

    # print 'Start ANN Prediction...', clock()
    # predict = model.getPrediction(initRaster, factors, calcTransitions=True)
    # confidence = model.getConfidence()
    # potentials = model.getTransitionPotentials()

    #~ # Create and Train LR Model
    #~ model = LR(ns=0)
    #~ print 'Start Setting LR Trainig Data...', clock()
    #~ model.setState(initRaster)
    #~ model.setFactors(factors)
    #~ model.setOutput(changeMap)
    #~ model.setMode('Stratified')
    #~ model.setSamples(100)
    #~ model.setTrainingData()
    #~ 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, calcTransitions=True)
    #~ print 'Finish LR Prediction...', clock(), '\n'

    # Create and Train WoE Model
    # print 'Start creating AreaAnalyst...', clock()
    # analyst = AreaAnalyst(initRaster, finalRaster)
    # print 'Finish creating AreaAnalyst ...', clock(), '\n'
    # print 'Start creating WoE model...', clock()
    # bins = {0: [[1000, 3000]], 1: [[200, 500, 1500]]}
    # model = WoeManager(factors, analyst, bins= bins)
    # model.train()
    # print 'Finish creating WoE model...', clock(), '\n'

    #~ # Create and Train MCE Model
    #~ print 'Start creating MCE model...', clock()
    #~ matrix = [
    #~ [1,     6],
    #~ [1.0/6,   1]
    #~ ]
    #~ model = MCE(factors, matrix, 2, 3, analyst)
    #~ print 'Finish creating MCE model...', clock(), '\n'

    # predict = model.getPrediction(initRaster, factors, calcTransitions=True)
    # confidence = model.getConfidence()
    # potentials = model.getTransitionPotentials()
    # filename = 'predict.tif'
    # confname = 'confidence.tif'
    # trans_prefix='trans_'
    # try:
    #     predict.save(filename)
    #     confidence.save(confname)
    #     if potentials != None:
    #         for k,v in potentials.iteritems():
    #             map = v.save(trans_prefix+str(k) + '.tif')
    # finally:
    #     os.remove(filename)
    #     #pass
    # print 'Finish Saving...', clock(), '\n'

    # simulation
    print 'Start Simulation...', clock()
    simulator = Simulator(initRaster, factors, model, crosstab)
    # Make 1 cycle of simulation:
    simulator.setIterationCount(1)
    simulator.simN()
    monteCarloSim = simulator.getState()  # Result of MonteCarlo simulation
    errors = simulator.errorMap(finalRaster)  # Risk class validation
    riskFunct = simulator.getConfidence()  # Risk function

    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()
예제 #6
0
파일: workflow.py 프로젝트: nextgis/molusce
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 crosstab.getTransitionStat()
    print "Finish Making CrossTable", clock(), '\n'

    # Create and Train Analyst
    print 'Start creating AreaAnalyst...', clock()
    analyst = AreaAnalyst(initRaster, finalRaster)
    print 'Finish creating AreaAnalyst ...', clock(), '\n'

    print 'Start Making Change Map...', clock()
    analyst = AreaAnalyst(initRaster,finalRaster)
    changeMap = analyst.getChangeMap()
    print 'Finish Making Change Map', clock(), '\n'


    #~ # Create and Train ANN Model
    model = MlpManager(ns=1)
    model.createMlp(initRaster, factors, changeMap, [10])
    print 'Start Setting MLP Trainig Data...', clock()
    model.setTrainingData(initRaster, factors, changeMap, mode='Stratified', samples=1000)
    print 'Finish Setting Trainig Data', clock(), '\n'
    print 'Start MLP Training...', clock()
    model.train(20, valPercent=20)
    print 'Finish Trainig', clock(), '\n'
    
    # print 'Start ANN Prediction...', clock()
    # predict = model.getPrediction(initRaster, factors, calcTransitions=True)
    # confidence = model.getConfidence()
    # potentials = model.getTransitionPotentials()

    #~ # Create and Train LR Model
    #~ model = LR(ns=0)
    #~ print 'Start Setting LR Trainig Data...', clock()
    #~ model.setState(initRaster)
    #~ model.setFactors(factors)
    #~ model.setOutput(changeMap)
    #~ model.setMode('Stratified')
    #~ model.setSamples(100)
    #~ model.setTrainingData()
    #~ 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, calcTransitions=True)
    #~ print 'Finish LR Prediction...', clock(), '\n'

    # Create and Train WoE Model
    # print 'Start creating AreaAnalyst...', clock()
    # analyst = AreaAnalyst(initRaster, finalRaster)
    # print 'Finish creating AreaAnalyst ...', clock(), '\n'
    # print 'Start creating WoE model...', clock()
    # bins = {0: [[1000, 3000]], 1: [[200, 500, 1500]]}
    # model = WoeManager(factors, analyst, bins= bins)
    # model.train()
    # print 'Finish creating WoE model...', clock(), '\n'

    #~ # Create and Train MCE Model
    #~ print 'Start creating MCE model...', clock()
    #~ matrix = [
        #~ [1,     6],
        #~ [1.0/6,   1]
    #~ ]
    #~ model = MCE(factors, matrix, 2, 3, analyst)
    #~ print 'Finish creating MCE model...', clock(), '\n'

    # predict = model.getPrediction(initRaster, factors, calcTransitions=True)
    # confidence = model.getConfidence()
    # potentials = model.getTransitionPotentials()
    # filename = 'predict.tif'
    # confname = 'confidence.tif'
    # trans_prefix='trans_'
    # try:
    #     predict.save(filename)
    #     confidence.save(confname)
    #     if potentials != None:
    #         for k,v in potentials.iteritems():
    #             map = v.save(trans_prefix+str(k) + '.tif')
    # finally:
    #     os.remove(filename)
    #     #pass
    # print 'Finish Saving...', clock(), '\n'

    # simulation
    print 'Start Simulation...', clock()
    simulator = Simulator(initRaster, factors, model, crosstab)
    # Make 1 cycle of simulation:
    simulator.setIterationCount(1)
    simulator.simN()
    monteCarloSim   = simulator.getState()              # Result of MonteCarlo simulation
    errors          = simulator.errorMap(finalRaster)   # Risk class validation
    riskFunct       = simulator.getConfidence()         # Risk function

    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()
예제 #7
0
    def test_compute_table(self):

        # print self.crosstab.getCrosstable().getCrosstable()
        # CrossTab:
        #  [[ 3.  1.  0.]
        #   [ 0.  1.  0.]
        #   [ 1.  0.  2.]]
        prediction = self.model.getPrediction(self.raster1)
        # print prediction.getBand(1)
        # prediction = [[1.0 1.0 6.0]
                     #  [6.0 5.0 1.0]
                     #  [-- 6.0 1.0]]
        # confidence = self.model.getConfidence()
        # print confidence.getBand(1)
        # confidence =     [[1.0 0.5  0.33]
                         #  [0.5 0.33 0.25]
                         #  [--  0.25 0.2]]
        result = np.array([
            [2.0, 1.0, 3.0],
            [1.0, 2.0, 1.0],
            [0,   3.0, 1.0]
        ])
        result = np.ma.array(result, mask = (result==0))

        simulator = Simulator(state=self.raster1, factors=None, model=self.model, crosstable=self.crosstab)    # The model does't use factors
        simulator.setIterationCount(1)
        simulator.simN()
        state = simulator.getState().getBand(1)
        assert_array_equal(result, state)

        result = np.array([
            [2.0, 1.0, 1.0],
            [2.0, 2.0, 1.0],
            [0,   3.0, 1.0]
        ])
        result = np.ma.array(result, mask = (result==0))

        simulator = Simulator(self.raster1, None, self.model, self.crosstab)
        simulator.setIterationCount(2)
        simulator.simN()
        state = simulator.getState().getBand(1)
        assert_array_equal(result, state)