Esempio n. 1
0
    def test_MlpManager(self):
        mng = MlpManager(ns=1)
        mng.createMlp(self.output, self.factors2, self.output, [10])
        assert_array_equal(mng.getMlpTopology(), [2*9 + 2*9, 10, 3])

        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        assert_array_equal(mng.getMlpTopology(), [2*1 + 1*1, 10, 3])
Esempio n. 2
0
 def test_predict(self):
     mng = MlpManager()
     mng.createMlp(self.output, self.factors, self.output, [10])
     weights = mng.copyWeights()
     
     # Set weights=0
     # then the output must be sigmoid(0)
     layers = []
     for layer in weights:
         shape = layer.shape
         layers.append(np.zeros(shape))
     mng.setMlpWeights(layers)
     mng._predict(self.output, self.factors)
     
     # Prediction
     raster = mng.getPrediction(self.output, self.factors)
     assert_array_equal(raster.getBand(1), sigmoid(0)*np.zeros((3,3)))
     # Confidence
     confid = mng.getConfidence()
     assert_array_equal(confid.getBand(1), sigmoid(0)*np.zeros((3,3)))
Esempio n. 3
0
    def test_setTrainingData(self):
        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        stat = self.factors[0].getBandStat(1) # mean & std
        m,s = stat['mean'], stat['std']
        mng.setTrainingData(self.output, self.factors, self.output, shuffle=False)

        min, max = mng.sigmin, mng.sigmax
        data = np.array(
            [
                ((0,3), [0, 1], (1.0-m)/s, [min,  max, min]),
                ((1,3), [0, 0], (1.0-m)/s, [min,  min, max]),
                ((2,3), [0, 1], (3.0-m)/s, [min,  max, min]),
                ((0,2), [0, 1], (3.0-m)/s, [min,  max, min]),
                ((1,2), [0, 0], (2.0-m)/s, [min,  min, max]),
                ((2,2), [0, 1], (1.0-m)/s, [min,  max, min]),
                ((0,1), [1, 0], (0.0-m)/s, [max,  min, min]),
                ((1,1), [0, 1], (3.0-m)/s, [min,  max, min]),
                ((2,1), [0, 0], (1.0-m)/s, [min,  min, max]),
            ],
            dtype=[('coords', float, 2),('state', float, (2,)), ('factors', float, (1,)), ('output', float, 3)]
        )
        self.assertEqual(mng.data.shape, (9,))
        for i in range(len(data)):
            assert_array_equal(data[i]['coords'], mng.data[i]['coords'])
            assert_array_equal(data[i]['factors'], mng.data[i]['factors'])
            assert_array_equal(data[i]['output'], mng.data[i]['output'])

        # two input rasters
        mng = MlpManager(ns=1)
        mng.createMlp(self.output, self.factors2, self.output, [10])
        mng.setTrainingData(self.output, self.factors2, self.output)
        data = [
            {
            'factors': (np.array([ 1.,  1.,  3.,  3.,  2.,  1.,  0.,  3.,  1.,
                                1.,  1.,  3.,  3.,  2.,  1.,  0.,  3.,  1.]) - stat['mean'])/stat['std'],
            'output': np.array([min, min,  max]),
            'state': np.array([0,1,  0,0,  0,1,   0,1,  0,0,  0,1,   1,0,  0,1,  0,0])
            }
        ]
        self.assertEqual(mng.data.shape, (1,))
        assert_array_equal(data[0]['factors'], mng.data[0]['factors'])
        assert_array_equal(data[0]['output'], mng.data[0]['output'])
        assert_array_equal(data[0]['state'], mng.data[0]['state'])

        # Multiband input
        mng = MlpManager(ns=1)
        mng.createMlp(self.output, self.factors3, self.output, [10])
        stat1 = self.factors3[0].getBandStat(1) # mean & std
        m1,s1 = stat1['mean'], stat1['std']
        stat2 = self.factors3[0].getBandStat(2) # mean & std
        m2,s2 = stat2['mean'], stat2['std']
        mng.setTrainingData(self.output, self.factors3, self.output)

        data = [
            {
            'factors': np.array([ (1.-m1)/s1,  (2.-m1)/s1,  (1.-m1)/s1,  (1.-m1)/s1,  (2.-m1)/s1,  (1.-m1)/s1,  (0.-m1)/s1,  (1.-m1)/s1,  (2.-m1)/s1,
                                (1.-m2)/s2,  (1.-m2)/s2,  (3.-m2)/s2,  (3.-m2)/s2,  (2.-m2)/s2,  (1.-m2)/s2,  (0.-m2)/s2,  (3.-m2)/s2,  (1.-m2)/s2]),
            'output': np.array([min, min,  max]),
            'state': np.array([0,1,  0,0,  0,1,   0,1,  0,0,  0,1,   1,0,  0,1,  0,0])
            }
        ]
        self.assertEqual(mng.data.shape, (1,))
        assert_array_equal(data[0]['factors'], mng.data[0]['factors'])
        assert_array_equal(data[0]['output'], mng.data[0]['output'])
        assert_array_equal(data[0]['state'], mng.data[0]['state'])

        # Complex case:
        mng = MlpManager(ns=1)
        mng.createMlp(self.output, self.factors4, self.output, [10])
        stat1 = self.factors4[0].getBandStat(1) # mean & std
        m1,s1 = stat1['mean'], stat1['std']
        stat2 = self.factors4[0].getBandStat(2) # mean & std
        m2,s2 = stat2['mean'], stat2['std']
        stat3 = self.factors4[1].getBandStat(1)
        m3,s3 = stat3['mean'], stat2['std']

        mng.setTrainingData(self.output, self.factors4, self.output)

        data = [
            {
            'factors': np.array([ (1.-m1)/s1,  (2.-m1)/s1,  (1.-m1)/s1,  (1.-m1)/s1,  (2.-m1)/s1,  (1.-m1)/s1,  (0.-m1)/s1,  (1.-m1)/s1,  (2.-m1)/s1,
                                (1.-m2)/s2,  (1.-m2)/s2,  (3.-m2)/s2,  (3.-m2)/s2,  (2.-m2)/s2,  (1.-m2)/s2,  (0.-m2)/s2,  (3.-m2)/s2,  (1.-m2)/s2,
                                (1.-m3)/s3,  (1.-m3)/s3,  (3.-m3)/s3,  (3.-m3)/s3,  (2.-m3)/s3,  (1.-m3)/s3,  (0.-m3)/s3,  (3.-m3)/s3,  (1.-m3)/s3]),
            'output': np.array([min, min,  max]),
            'state': np.array([0,1,  0,0,  0,1,   0,1,  0,0,  0,1,   1,0,  0,1,  0,0])
            }
        ]
        self.assertEqual(mng.data.shape, (1,))
        assert_array_equal(data[0]['factors'], mng.data[0]['factors'])
        assert_array_equal(data[0]['output'], mng.data[0]['output'])
        assert_array_equal(data[0]['state'], mng.data[0]['state'])
Esempio n. 4
0
    def test_predict(self):
        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        weights = mng.copyWeights()

        # Set weights=0
        # then the output must be sigmoid(0)
        layers = []
        for layer in weights:
            shape = layer.shape
            layers.append(np.zeros(shape))
        mng.setMlpWeights(layers)
        mng._predict(self.output, self.factors)


        # Prediction ( the output must be sigmoid(0) )
        raster = mng.getPrediction(self.output, self.factors, calcTransitions=True)
        assert_array_equal(raster.getBand(1), sigmoid(0)*np.ones((3,3)))
        # Confidence is zero
        confid = mng.getConfidence()
        self.assertEquals(confid.getBand(1).dtype, np.uint8)
        assert_array_equal(confid.getBand(1), np.zeros((3,3)))
        # Transition Potentials (is (sigmoid(0) - sigmin)/sigrange )
        potentials = mng.getTransitionPotentials()
        cats = self.output.getBandGradation(1)
        for cat in cats:
            map = potentials[cat]
            self.assertEquals(map.getBand(1).dtype, np.uint8)
            assert_array_equal(map.getBand(1), 50*np.ones((3,3)))
Esempio n. 5
0
    def test_train(self):
        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        mng.setTrainingData(self.output, self.factors, self.output)

        mng.train(1, valPercent=50)
        val = mng.getMinValError()
        tr  = mng.getTrainError()
        mng.train(20, valPercent=50, continue_train=True)
        self.assertGreaterEqual(val, mng.getMinValError())

        mng = MlpManager(ns=1)
        mng.createMlp(self.state1, self.factors1, self.output1, [10])
        mng.setTrainingData(self.state1, self.factors1, self.output1)
        mng.train(1, valPercent=20)
        predict = mng.getPrediction(self.state1, self.factors1)
        mask = predict.getBand(1).mask

        self.assertTrue(not all(mask.flatten()))
Esempio n. 6
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()
Esempio n. 7
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()