Beispiel #1
0
    def test_Mask(self):
        reference = Raster('../../examples/data.tif')
        simulated = Raster('../../examples/data1.tif')
        reference.resetMask([2])
        simulated.resetMask([2])

        eb = EBudget(reference, simulated)
        W = eb.W

        S1 = weightedSum(eb.Sj[1], W)
        S3 = weightedSum(eb.Sj[3], W)
        np.testing.assert_almost_equal(S1, 5.0 / 8)
        np.testing.assert_almost_equal(S3, 3.0 / 8)

        noNo = eb.NoNo()
        np.testing.assert_almost_equal(noNo, 0.5)

        noM = eb.NoMed()
        np.testing.assert_almost_equal(noM, (5.0 * 5 / 8 + 3.0 * 3 / 8) / 8)

        medM = eb.MedMed()
        np.testing.assert_almost_equal(medM, 4.0 / 8)

        medP = eb.MedPer()
        np.testing.assert_almost_equal(medP, 1.0)
Beispiel #2
0
    def setUp(self):
        self.r1 = Raster('../../examples/multifact.tif')
        # r1 -> r1 transition
        self.r1r1 = [[
            5,
            5,
            15,
        ], [
            15,
            10,
            5,
        ], [
            0,
            15,
            5,
        ]]

        self.r2 = Raster('../../examples/multifact.tif')
        self.r2.resetMask([0])
        self.r2r2 = [[
            0,
            0,
            8,
        ], [
            8,
            4,
            0,
        ], [
            100,
            8,
            0,
        ]]

        self.r3 = Raster('../../examples/multifact.tif')
        self.r3.resetMask([2])
Beispiel #3
0
    def test_normalize(self):
        multifact = [
            [1, 1, 3],
            [3, 2, 1],
            [0, 3, 1],
        ]

        # Normalize using std and mean
        r1 = Raster('examples/multifact.tif')
        r1.normalize()
        r1.denormalize()
        assert_array_equal(r1.getBand(1), multifact)

        # Normalize using min and max
        r1 = Raster('examples/multifact.tif')
        r1.normalize(mode='maxmin')
        r1.denormalize()
        assert_array_equal(r1.getBand(1), multifact)

        # Two normalization procedures
        r1 = Raster('examples/multifact.tif')
        r1.normalize()
        r1.normalize(mode='maxmin')
        r1.denormalize()
        assert_array_equal(r1.getBand(1), multifact)
        r1 = Raster('examples/multifact.tif')
        r1.normalize(mode='maxmin')
        r1.normalize()
        r1.denormalize()
        assert_array_equal(r1.getBand(1), multifact)
Beispiel #4
0
    def test_getStat(self):
        reference = Raster('../../examples/data.tif')
        simulated = Raster('../../examples/data1.tif')
        reference.resetMask([2])
        simulated.resetMask([2])

        eb = EBudget(reference, simulated)
        stat = eb.getStat(nIter=3)
        ans0 = {
            'NoNo': 0.5,
            'NoMed': (5.0 * 5 / 8 + 3.0 * 3 / 8) / 8,
            'MedMed': 4.0 / 8,
            'MedPer': 1.0,
            'PerPer': 1.0
        }
        for k in stat[0].keys():
            np.testing.assert_almost_equal(stat[0][k], ans0[k])
        ans1 = {
            'NoNo': 0.5,
            'NoMed': (5.0 / 8 + 5.0 / 32 + 3.0 / 16 + 3.0 / 32) / 2,
            'MedMed': 4.0 / 8,
            'MedPer': 1.0,
            'PerPer': 1.0
        }
        for k in stat[1].keys():
            np.testing.assert_almost_equal(stat[0][k], ans1[k])
Beispiel #5
0
    def _predict(self, state, calcTransitions=False):
        '''
        Predict the changes.
        '''
        try:
            geodata = state.getGeodata()
            rows, cols = geodata['ySize'], geodata['xSize']

            self.transitionPotentials = None    # Reset tr.potentials if they exist

            # Get locations where self.initStateNum is occurs
            band = state.getBand(1)
            initStateMask = binaryzation(band, [self.initStateNum])
            mask = band.mask

            # Calculate summary map of factors weights
            # Transition potentials:
            #   current implementation: potential and confidence are equal (two-class implementation)
            # Confidence:
            #   confidence is summary map of factors scaled to 0-100, if current state = self.initState
            #   confidence is 0, if current state != self.initState
            # Prediction:
            #   predicted value is a constant = areaAnalyst.encode(initStateNum, finalStateNum), if current state = self.initState
            #   predicted value is the transition code current_state -> current_state, if current state != self.initState
            confidence = np.zeros((rows,cols), dtype=np.uint8)
            weights = self.getWeights()
            weightNum = 0               # Number of processed weights
            for f in self.factors:
                if not f.geoDataMatch(state):
                    raise MCEError('Geometries of the state and factor rasters are different!')
                f.normalize(mode = 'maxmin')
                for i in xrange(f.getBandsCount()):
                    band = f.getBand(i+1)
                    confidence = confidence + (band*weights[weightNum]*100).astype(np.uint8)
                    mask = np.ma.mask_or(mask, band.mask)
                    weightNum = weightNum + 1
            confidence = confidence*initStateMask
            prediction = np.copy(state.getBand(1))
            for code in self.areaAnalyst.categories:
                if code != self.initStateNum:
                    prediction[prediction==code] = self.areaAnalyst.encode(code, code)
                else:
                    prediction[prediction==code] = self.areaAnalyst.encode(self.initStateNum, self.finalStateNum)

            predicted_band = np.ma.array(data=prediction, mask=mask, dtype=np.uint8)
            self.prediction = Raster()
            self.prediction.create([predicted_band], geodata)
            confidence_band = np.ma.array(data=confidence, mask=mask, dtype=np.uint8)
            self.confidence = Raster()
            self.confidence.create([confidence_band], geodata)

            code = self.areaAnalyst.encode(self.initStateNum, self.finalStateNum)
            self.transitionPotentials = {code: self.confidence}
        except MemoryError:
            self.errorReport.emit(self.tr("The system out of memory during MCE prediction"))
            raise
        except:
            self.errorReport.emit(self.tr("An unknown error occurs during MCE prediction"))
            raise
Beispiel #6
0
    def setUp(self):
        self.reference = Raster('../../examples/data.tif')
        #~ [1,1,1,1],
        #~ [1,1,2,2],
        #~ [2,2,2,2],
        #~ [3,3,3,3]

        self.simulated = Raster('../../examples/data1.tif')
Beispiel #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 "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()
Beispiel #8
0
    def _predict(self, state, calcTransitions=False):
        '''
        Predict the changes.
        '''
        try:
            self.rangeChanged.emit(self.tr("Initialize model %p%"), 1)

            rows, cols = self.geodata['ySize'], self.geodata['xSize']
            if not self.changeMap.geoDataMatch(state):
                raise WoeManagerError('Geometries of the state and changeMap rasters are different!')

            prediction = np.zeros((rows,cols), dtype=np.uint8)
            confidence = np.zeros((rows,cols), dtype=np.uint8)
            mask = np.zeros((rows,cols), dtype=np.byte)

            stateBand = state.getBand(1)

            self.updateProgress.emit()
            self.rangeChanged.emit(self.tr("Prediction %p%"), rows)

            for r in xrange(rows):
                for c in xrange(cols):
                    oldMax, currMax = -1000, -1000  # Small numbers
                    indexMax = -1                   # Index of Max weight
                    initCat = stateBand[r,c]        # Init category (state before transition)
                    try:
                        codes = self.analyst.codes(initCat)   # Possible final states
                        for code in codes:
                            try: # If not all possible transitions are presented in the changeMap
                                map = self.woe[code]     # Get WoE map of transition 'code'
                            except KeyError:
                                continue
                            w = map[r,c]        # The weight in the (r,c)-pixel
                            if w > currMax:
                                indexMax, oldMax, currMax = code, currMax, w
                        prediction[r,c] = indexMax
                        confidence[r,c] = int(100*(sigmoid(currMax) - sigmoid(oldMax)))
                    except ValueError:
                        mask[r,c] = 1
                self.updateProgress.emit()

            predicted_band = np.ma.array(data=prediction, mask=mask, dtype=np.uint8)
            self.prediction = Raster()
            self.prediction.create([predicted_band], self.geodata)
            confidence_band = np.ma.array(data=confidence, mask=mask, dtype=np.uint8)
            self.confidence = Raster()
            self.confidence.create([confidence_band], self.geodata)
        except MemoryError:
            self.errorReport.emit(self.tr("The system out of memory during WOE prediction"))
            raise
        except:
            self.errorReport.emit(self.tr("An unknown error occurs during WoE prediction"))
            raise
        finally:
            self.processFinished.emit()
Beispiel #9
0
    def setUp(self):
        self.factor = Raster('../../examples/multifact.tif')
        #~ [1,1,3]
        #~ [3,2,1]
        #~ [0,3,1]

        self.sites = Raster('../../examples/sites.tif')
        #~ [1,2,1],
        #~ [1,2,1],
        #~ [0,1,2]
        self.sites.resetMask(maskVals=[0])

        self.mask = [[
            False,
            False,
            False,
        ], [
            False,
            False,
            False,
        ], [
            True,
            False,
            False,
        ]]
        fact = [[
            1,
            1,
            3,
        ], [
            3,
            2,
            1,
        ], [
            0,
            3,
            1,
        ]]
        site = [[
            False,
            True,
            False,
        ], [
            False,
            True,
            False,
        ], [
            False,
            False,
            True,
        ]]
        self.factraster = ma.array(data=fact, mask=self.mask, dtype=np.int)
        self.sitesraster = ma.array(data=site, mask=self.mask, dtype=np.bool)
Beispiel #10
0
    def setUp(self):
        self.factor = Raster('../../examples/multifact.tif')
        #~ [1,1,3]
        #~ [3,2,1]
        #~ [0,3,1]

        self.state = Raster('../../examples/sites.tif')
        self.state.resetMask(maskVals=[0])
        #~ [1,2,1],
        #~ [1,2,1],
        #~ [0,1,2]
        self.areaAnalyst = AreaAnalyst(self.state, second=None)
Beispiel #11
0
    def setUp(self):
        self.r1 = Raster('examples/multifact.tif')
        self.r2 = Raster('examples/sites.tif')
        self.r3 = Raster('examples/two_band.tif')

        # r1
        data1 = np.array([[1, 1, 3], [3, 2, 1], [0, 3, 1]])
        # r2
        data2 = np.array([[1, 2, 1], [1, 2, 1], [0, 1, 2]])
        mask = [[False, False, False], [False, False, False],
                [False, False, False]]
        self.data1 = ma.array(data=data1, mask=mask)
        self.data2 = ma.array(data=data2, mask=mask)
Beispiel #12
0
    def test_get_state(self):
        smp = Sampler(self.state, self.factors, self.output, ns=0)
        assert_array_equal(smp.get_state(self.state, 1, 1), [0, 0])
        assert_array_equal(smp.get_state(self.state, 0, 0), [0, 1])

        smp = Sampler(self.state, self.factors, self.output, ns=1)
        res = [
            0,
            1,
            0,
            0,
            0,
            1,
            0,
            1,
            0,
            0,
            0,
            1,
            1,
            0,
            0,
            1,
            0,
            0,
        ]
        assert_array_equal(smp.get_state(self.state, 1, 1), res)

        inputRast = Raster('../../examples/sites.tif')
        inputRast.resetMask([0])
        smp = Sampler(inputRast, self.factors, self.output, ns=0)
        assert_array_equal(smp.get_state(self.state, 1, 1), [0])
        assert_array_equal(smp.get_state(self.state, 0, 0), [1])
Beispiel #13
0
    def test_roundBands(self):
        rast = Raster('examples/multifact.tif')
        rast.bands = rast.bands * 0.1
        rast.roundBands()
        answer = [[[
            0,
            0,
            0,
        ], [0, 0, 0], [0, 0, 0]]]
        assert_array_equal(answer, rast.bands)

        rast = Raster('examples/multifact.tif')
        rast.bands = rast.bands * 1.1
        rast.roundBands(decimals=1)
        answer = np.array([[[1.1, 1.1, 3.3], [3.3, 2.2, 1.1], [0.0, 3.3,
                                                               1.1]]])
        assert_array_equal(answer, rast.bands)
Beispiel #14
0
    def setUp(self):
        self.output = Raster('../../examples/multifact.tif')
        #~ [1,1,3]
        #~ [3,2,1]
        #~ [0,3,1]
        self.output.resetMask([0])
        self.state = self.output
        self.factors = [
            Raster('../../examples/sites.tif'),
            Raster('../../examples/sites.tif')
        ]
        #~ [1,2,1],
        #~ [1,2,1],
        #~ [0,1,2]

        self.output1 = Raster('../../examples/data.tif')
        self.state1 = self.output1
        self.factors1 = [Raster('../../examples/fact16.tif')]
Beispiel #15
0
    def setUp(self):
        self.factors = [Raster('../../examples/multifact.tif')]
        self.output = Raster('../../examples/sites.tif')
        #~ sites.tif is 1-band 3x3 raster:
            #~ [1,2,1],
            #~ [1,2,1],
            #~ [0,1,2]

        self.factors2 = [Raster('../../examples/multifact.tif'), Raster('../../examples/multifact.tif')]
        self.factors3 = [Raster('../../examples/two_band.tif')]
        self.factors4 = [Raster('../../examples/two_band.tif'), Raster('../../examples/multifact.tif')]

        self.output1  = Raster('../../examples/data.tif')
        self.state1   = self.output1
        self.factors1 = [Raster('../../examples/fact16.tif')]
Beispiel #16
0
 def test_save(self):
     try:
         filename = 'temp.tiff'
         self.r1.save(filename)
         r2 = Raster(filename)
         self.assertEqual(r2.get_dtype(), self.r1.get_dtype())
         self.assertEqual(r2.getBandsCount(), self.r1.getBandsCount())
         for i in range(r2.getBandsCount()):
             assert_array_equal(r2.getBand(i + 1), self.r1.getBand(i + 1))
     finally:
         os.remove(filename)
Beispiel #17
0
    def test_cat2vect(self):
        smp = Sampler(self.state, self.factors, self.output, ns=0)
        assert_array_equal(smp.cat2vect(0), [1, 0])
        assert_array_equal(smp.cat2vect(1), [0, 1])
        assert_array_equal(smp.cat2vect(2), [0, 0])

        inputRast = Raster('../../examples/sites.tif')
        inputRast.resetMask([0])
        smp = Sampler(inputRast, self.factors, self.output, ns=0)
        assert_array_equal(smp.cat2vect(1), [1])
        assert_array_equal(smp.cat2vect(2), [0])
Beispiel #18
0
 def errorMap(self, answer):
     '''
     Create map of correct and incorrect prediction.
     This function compares the known answer and the result of predicting procedure,
     correct pixel is marked as 0.
     '''
     state = self.getState()
     b = state.getBand(1)
     a = answer.getBand(1)
     diff = (a - b).astype(np.int16)
     result = Raster()
     result.create([diff], state.getGeodata())
     return result
Beispiel #19
0
    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()
Beispiel #20
0
    def _predict(self, state, factors = None, calcTransitions=False):
        geodata = state.getGeodata()
        band = state.getBand(1)
        rows, cols = geodata['ySize'], geodata['xSize']
        # Let the new state is: 1 -> 2, 2- >3, 3 -> 1, then
        # the prediction is 1->1, 2->5, 3->6

        predicted_band  = np.copy(band)
        predicted_band[band == 1] = 1.0
        predicted_band[band == 2] = 5.0
        predicted_band[band == 3] = 6.0

        # Let the confidence is 1/(1+row+col), where row is row number of the cell, col is column number of the cell.
        confidence_band = np.zeros([rows, cols])
        for i in xrange(cols):
            for j in xrange(rows):
                confidence_band[i,j] = 1.0/(1+i+j)

        predicted_bands  = [np.ma.array(data = predicted_band, mask = band.mask)]
        confidence_bands = [np.ma.array(data = confidence_band, mask = band.mask)]
        self.prediction = Raster()
        self.prediction.create(predicted_bands, state.geodata)
        self.confidence = Raster()
        self.confidence.create(confidence_bands, state.geodata)
Beispiel #21
0
    def setUp(self):

        # Raster1:
            #~ [1, 1, 3,],
            #~ [3, 2, 1,],
            #~ [0, 3, 1,]
        self.raster1 = Raster('../../examples/multifact.tif')
        self.raster1.resetMask([0])

        self.X = np.array([
            [1, 2, 3],
            [3, 2, 1],
            [0, 1, 1]
        ])
        self.X = np.ma.array(self.X, mask=(self.X == 0))
        self.raster2 = Raster()
        self.raster2.create([self.X], self.raster1.getGeodata())

        self.aa = AreaAnalyst(self.raster1, self.raster2)

        self.crosstab = CrossTableManager(self.raster1, self.raster2)

        # Simple model
        self.model = Model(state=self.raster1)
Beispiel #22
0
    def test_coarse(self):
        reference = Raster('../../examples/data.tif')
        simulated = Raster('../../examples/data1.tif')
        reference.resetMask([2])
        simulated.resetMask([2])

        eb = EBudget(reference, simulated)
        eb.coarse(2)
        # W
        answer = np.array([[1.0, 0.25], [0.5, 0.25]])
        np.testing.assert_array_equal(eb.W, answer)
        # Rj
        answer1 = np.array([[1.0, 1.0], [0, 0]])
        answer3 = np.array([[0, 0], [1.0, 1.0]])
        ans = {1: answer1, 3: answer3}
        np.testing.assert_equal(eb.Rj, ans)
        # Sj
        answer1 = np.array([[3.0 / 4, 0.0], [1.0, 0]])
        answer3 = np.array([[1.0 / 4, 1.0], [0, 1.0]])
        ans = {1: answer1, 3: answer3}
        np.testing.assert_equal(eb.Sj, ans)

        eb.coarse(2)
        # W
        answer = np.array([[0.5]])
        np.testing.assert_array_equal(eb.W, answer)
        # Rj
        answer1 = np.array([[(1 + 1.0 / 4) / 2]])
        answer3 = np.array([[(1.0 / 2 + 1.0 / 4) / 2]])
        ans = {1: answer1, 3: answer3}
        np.testing.assert_equal(eb.Rj, ans)
        # Sj
        answer1 = np.array([[(3.0 / 4 + 0.5) / 2]])
        answer3 = np.array([[(1.0 / 4 + 1.0 / 4 + 1.0 / 4) / 2]])
        ans = {1: answer1, 3: answer3}
        np.testing.assert_equal(eb.Sj, ans)
Beispiel #23
0
    def test_MCE(self):

        data = [[1.0, 4.0, 6.0, 7.0], [1.0 / 4, 1.0, 3.0, 4.0],
                [1.0 / 6, 1.0 / 3, 1.0, 2.0], [1.0 / 7, 1.0 / 4, 1.0 / 2, 1]]
        # Multiband
        factor = Raster('../../examples/two_band.tif')

        mce = MCE([self.factor, factor, self.factor], data, 1, 2,
                  self.areaAnalyst)
        w = mce.getWeights()
        answer = [0.61682294, 0.22382863, 0.09723423, 0.06211421]
        assert_almost_equal(w, answer)

        # One-band
        mce = MCE([self.factor, self.factor, self.factor, self.factor], data,
                  1, 2, self.areaAnalyst)
        w = mce.getWeights()
        answer = [0.61682294, 0.22382863, 0.09723423, 0.06211421]
        assert_almost_equal(w, answer)

        mask = [[False, False, False], [False, False, False],
                [False, False, True]]
        p = mce.getPrediction(self.state).getBand(1)
        self.assertEquals(p.dtype, np.uint8)
        answer = [  # The locations where the big numbers are stored must be masked (see mask and self.state)
            [1, 3, 1], [1, 3, 1], [100, 1, 100]
        ]
        answer = np.ma.array(data=answer, mask=mask)
        assert_almost_equal(p, answer)

        c = mce.getConfidence().getBand(1)
        self.assertEquals(c.dtype, np.uint8)
        answer = [  # The locations where the big numbers are stored must be masked (see mask and self.state)
            [sum((w * 100).astype(int)) / 3, 0,
             sum((w * 100).astype(int))],
            [sum((w * 100).astype(int)), 0,
             sum((w * 100).astype(int)) / 3],
            [10000, sum((w * 100).astype(int)), 10000]
        ]
        answer = np.ma.array(data=answer, mask=mask)
        assert_almost_equal(c, answer)
Beispiel #24
0
    def makeChangeMap(self):
        rows, cols = self.geodata['ySize'], self.geodata['xSize']
        band = np.zeros([rows, cols], dtype=np.int16)

        f, s = self.first, self.second
        if self.initRaster == None:
            checkPersistent = False
        else:
            checkPersistent = True
            t = self.initRaster.getBand(1)
        raster = None
        try:
            self.rangeChanged.emit(self.tr("Creating change map %p%"), rows)
            for i in xrange(rows):
                for j in xrange(cols):
                    if (f.mask.shape == ()) or (not f.mask[i,j]):
                        r = f[i,j]
                        c = s[i,j]
                        # Percistent category is the category that is constant for all three rasters
                        if checkPersistent and (r==c) and (r==t[i,j]):
                            band[i, j] = self.persistentCategoryCode
                        else:
                            band[i, j] = self.encode(r, c)
                self.updateProgress.emit()
            bands = [np.ma.array(data = band, mask = f.mask, dtype=np.int16)]
            raster = Raster()
            raster.create(bands, self.geodata)
            self.changeMap = raster
        except MemoryError:
            self.errorReport.emit(self.tr("The system out of memory during change map creating"))
            raise
        except:
            self.errorReport.emit(self.tr("An unknown error occurs during change map creating"))
            raise
        finally:
            self.processFinished.emit(raster)
Beispiel #25
0
 def setUp(self):
     self.init = Raster('../../examples/init.tif', maskVals={1: [255]})
     self.final = Raster('../../examples/final.tif', maskVals={1: [255]})
Beispiel #26
0
    def _predict(self, state, factors, calcTransitions=False):
        '''
        Calculate output and confidence rasters using LR model and input rasters
        @param state            Raster of the current state (categories) values.
        @param factors          List of the factor rasters (predicting variables).
        '''
        try:
            self.rangeChanged.emit(self.tr("Initialize model %p%"), 1)
            geodata = state.getGeodata()
            rows, cols = geodata['ySize'], geodata['xSize']
            for r in factors:
                if not state.geoDataMatch(r):
                    raise LRError(
                        'Geometries of the input rasters are different!')

            self.transitionPotentials = None  # Reset tr.potentials if they exist

            # Normalize factors before prediction:
            for f in factors:
                f.normalize(mode='mean')

            predicted_band = np.zeros([rows, cols], dtype=np.uint8)
            confidence_band = np.zeros([rows, cols], dtype=np.uint8)
            if calcTransitions:
                self.transitionPotentials = {}
                for cat in self.catlist:
                    self.transitionPotentials[cat] = np.zeros([rows, cols],
                                                              dtype=np.uint8)

            self.sampler = Sampler(state, factors, ns=self.ns)
            mask = state.getBand(1).mask.copy()
            if mask.shape == ():
                mask = np.zeros([rows, cols], dtype=np.bool)
            self.updateProgress.emit()
            self.rangeChanged.emit(self.tr("Prediction %p%"), rows)
            for i in xrange(rows):
                for j in xrange(cols):
                    if not mask[i, j]:
                        input = self.sampler.get_inputs(state, i, j)
                        if input != None:
                            input = np.array([input])
                            out = self.logreg.predict(input)
                            predicted_band[i, j] = out
                            confidence = self._outputConfidence(input)
                            confidence_band[i, j] = confidence

                            if calcTransitions:
                                potentials = self.outputTransitions(input)
                                for cat in self.catlist:
                                    map = self.transitionPotentials[cat]
                                    map[i, j] = potentials[cat]
                        else:  # Input sample is incomplete => mask this pixel
                            mask[i, j] = True
                self.updateProgress.emit()
            predicted_bands = [
                np.ma.array(data=predicted_band, mask=mask, dtype=np.uint8)
            ]
            confidence_bands = [
                np.ma.array(data=confidence_band, mask=mask, dtype=np.uint8)
            ]

            self.prediction = Raster()
            self.prediction.create(predicted_bands, geodata)
            self.confidence = Raster()
            self.confidence.create(confidence_bands, geodata)

            if calcTransitions:
                for cat in self.catlist:
                    band = [
                        np.ma.array(data=self.transitionPotentials[cat],
                                    mask=mask,
                                    dtype=np.uint8)
                    ]
                    self.transitionPotentials[cat] = Raster()
                    self.transitionPotentials[cat].create(band, geodata)
        except MemoryError:
            self.errorReport.emit(
                self.tr("The system out of memory during LR prediction"))
            raise
        except:
            self.errorReport.emit(
                self.tr("An unknown error occurs during LR prediction"))
            raise
        finally:
            self.processFinished.emit()
Beispiel #27
0
    def test_WoeManager(self):
        aa = AreaAnalyst(self.sites, self.sites)
        w1 = WoeManager([self.factor], aa)
        w1.train()
        p = w1.getPrediction(self.sites).getBand(1)
        answer = [[0, 3, 0], [0, 3, 0], [9, 0, 3]]
        answer = ma.array(data=answer, mask=self.mask)
        assert_array_equal(p, answer)

        initState = Raster('../../examples/data.tif')
        #~ [1,1,1,1],
        #~ [1,1,2,2],
        #~ [2,2,2,2],
        #~ [3,3,3,3]
        finalState = Raster('../../examples/data1.tif')
        #~ [1,1,2,3],
        #~ [3,1,2,3],
        #~ [3,3,3,3],
        #~ [1,1,3,2]
        aa = AreaAnalyst(initState, finalState)
        w = WoeManager([initState], aa)
        w.train()
        #print w.woe
        p = w.getPrediction(initState).getBand(1)
        self.assertEquals(p.dtype, np.uint8)

        # Calculate by hands:
        #1->1 transition raster:
        r11 = [[1, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
        #1->2 raster:
        r12 = [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
        #1->3 raster:
        r13 = [[0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
        # 2->1
        r21 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
        # 2->2
        r22 = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
        # 2->3
        r23 = [[0, 0, 0, 0], [0, 0, 0, 1], [1, 1, 1, 1], [0, 0, 0, 0]]
        # 3->1
        r31 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0]]
        # 3->2
        r32 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
        # 3->3
        r33 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0]]
        geodata = initState.getGeodata()
        sites = {
            '11': r11,
            '12': r12,
            '13': r13,
            '21': r21,
            '22': r22,
            '23': r23,
            '31': r31,
            '32': r32,
            '33': r33
        }
        woeDict = {}  # WoE of transitions
        for k in sites.keys():  #
            if k != '21':  # !!! r21 is zero
                x = Raster()
                x.create([np.ma.array(data=sites[k])], geodata)
                sites[k] = x
                woeDict[k] = woe(initState.getBand(1), x.getBand(1))
        #w1max = np.maximum(woeDict['11'], woeDict['12'], woeDict['13'])
        #w2max = np.maximum(woeDict['22'], woeDict['23'])
        #w3max = np.maximum(woeDict['31'], woeDict['32'], woeDict['33'])
        # Answer is a transition code with max weight
        answer = [[0, 0, 0, 0], [0, 0, 5, 5], [5, 5, 5, 5], [6, 6, 6, 6]]
        assert_array_equal(p, answer)

        w = WoeManager([initState], aa, bins={
            0: [
                [2],
            ],
        })
        w.train()
        p = w.getPrediction(initState).getBand(1)
        self.assertEquals(p.dtype, np.uint8)
        c = w.getConfidence().getBand(1)
        self.assertEquals(c.dtype, np.uint8)
Beispiel #28
0
 def test_create(self):
     raster = Raster()
     raster.create([self.data1], geodata=self.r1.getGeodata())
     self.assertTrue(raster.geoDataMatch(self.r1))
     self.assertEqual(raster.getBandsCount(), 1)
     self.assertEqual(set(raster.getBandGradation(1)), set([0, 1, 2, 3]))
Beispiel #29
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()
Beispiel #30
0
 def test_isContinues(self):
     rast = Raster('examples/multifact.tif')
     self.assertFalse(rast.isCountinues(bandNo=1))
     rast = Raster('examples/dist_roads.tif')
     self.assertTrue(rast.isCountinues(bandNo=1))