Пример #1
0
    def sim(self):
        """
        Make 1 iteracion of simulation.
        """
        # TODO: eleminate AreaAnalyst.getChangeMap() from the process

        transition = self.crosstable.getCrosstable()

        prediction = self.getPrediction()
        state = self.getState()
        new_state = state.getBand(1).copy()  # New states (the result of simulation) will be stored there.
        analyst = AreaAnalyst(state, prediction)
        classes = analyst.classes
        changes = analyst.getChangeMap().getBand(1)

        # Make transition between classes according to
        # number of moved pixel in crosstable
        self.rangeChanged.emit(self.tr("Simulation process %p%"), len(classes) ** 2 - len(classes))
        for initClass in classes:
            for finalClass in classes:
                if initClass == finalClass:
                    continue

                # TODO: Calculate number of pixels to be moved via TransitoionMatrix and state raster
                n = transition.getTransition(
                    initClass, finalClass
                )  # Number of pixels to be moved (constant count now).
                # Find n appropriate places for transition initClass -> finalClass
                class_code = analyst.encode(initClass, finalClass)
                places = changes == class_code  # Array of places where transitions initClass -> finalClass are occured
                placesCount = np.sum(places)
                if placesCount < n:
                    self.logMessage.emit(
                        self.tr("There are more transitions in the transition matrix, then the model have found")
                    )
                    n = placesCount

                confidence = self.getConfidence().getBand(1)
                confidence = (
                    confidence * places
                )  # The higher is number in cell, the higer is probability of transition in the cell
                indices = []
                for i in range(n):
                    index = np.unravel_index(
                        confidence.argmax(), confidence.shape
                    )  # Select the cell with biggest probability
                    indices.append(index)
                    confidence[index] = -1  # Mark the cell to prevent second selection

                # Now "indices" contains indices of the appropriate places,
                # make transition initClass -> finalClass
                for index in indices:
                    new_state[index] = finalClass
                self.updateProgress.emit()

        result = Raster()
        result.create([new_state], state.getGeodata())
        self.state = result
        self.updatePrediction(result)
        self.processFinished.emit()
Пример #2
0
 def test_encode(self):
     aa = AreaAnalyst(self.r1, self.r1)
     self.assertEqual(aa.classes, [0,1,2,3])
     self.assertEqual(aa.encode(1,2), 6)
     for initClass in range(4):
         for finalClass in range(4):
             k = aa.encode(initClass, finalClass)
             self.assertEqual(aa.decode(k), (initClass, finalClass))
     self.assertEqual(aa.finalCodes(0), [0,1,2,3])
     self.assertEqual(aa.finalCodes(1), [4,5,6,7])
Пример #3
0
 def test_AreaAnalyst(self):
     aa = AreaAnalyst(self.r1, self.r1)
     raster = aa.getChangeMap()
     band = raster.getBand(1)
     assert_array_equal(band, self.r1r1)
     
     # Masked raster
     aa = AreaAnalyst(self.r2, self.r2)
     raster = aa.getChangeMap()  
     band = raster.getBand(1)
     assert_array_equal(band, self.r2r2)
Пример #4
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)
Пример #5
0
 def test_CheckBins(self):
     aa = AreaAnalyst(self.sites, self.sites)
     w1 = WoeManager([self.factor], aa, bins=None)
     self.assertTrue(w1.checkBins())
     w1 = WoeManager([self.factor], aa, bins={0: [None]})
     self.assertTrue(w1.checkBins())
     w1 = WoeManager([self.factor], aa, bins={0: [[1, 2, 3]]})
     self.assertTrue(w1.checkBins())
     w1 = WoeManager([self.factor], aa, bins={0: [[1, 4]]})
     self.assertFalse(w1.checkBins())
     w1 = WoeManager([self.factor], aa, bins={0: [[-1, 1]]})
     self.assertFalse(w1.checkBins())
     w1 = WoeManager([self.factor], aa, bins={0: [[2, 3, 1]]})
     self.assertFalse(w1.checkBins())
Пример #6
0
 def test_encode(self):
     aa = AreaAnalyst(self.r1, self.r1)
     self.assertEqual(aa.categories, [0, 1, 2, 3])
     self.assertEqual(aa.encode(1, 2), 6)
     for initClass in range(4):
         for finalClass in range(4):
             k = aa.encode(initClass, finalClass)
             self.assertEqual(aa.decode(k), (initClass, finalClass))
     self.assertEqual(aa.finalCodes(0), [0, 1, 2, 3])
     self.assertEqual(aa.finalCodes(1), [4, 5, 6, 7])
Пример #7
0
    def test_AreaAnalyst(self):
        aa = AreaAnalyst(self.r1, self.r1)
        raster = aa.getChangeMap()
        band = raster.getBand(1)
        assert_array_equal(band, self.r1r1)

        # Masked raster
        aa = AreaAnalyst(self.r2, self.r2)
        raster = aa.getChangeMap()
        band = raster.getBand(1)
        assert_array_equal(band, self.r2r2)
Пример #8
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)
Пример #9
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()
Пример #10
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()
Пример #11
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)
Пример #12
0
    def __sim(self):
        '''
        1 iteracion of simulation.
        '''
        transition = self.crosstable.getCrosstable()

        self.updatePrediction(self.state)
        changes = self.getPrediction().getBand(1)   # Predicted change map
        changes = changes + 1                       # Filling nodata as 0 can be ambiguous:
        changes = np.ma.filled(changes, 0)          #   (cat_code can be 0, to do not mix it with no-data, add 1)
        state = self.getState()
        new_state = state.getBand(1).copy().astype(np.uint8)    # New states (the result of simulation) will be stored there.

        self.rangeChanged.emit(self.tr("Area Change Analysis %p%"), 2)
        self.updateProgress.emit()
        QCoreApplication.processEvents()
        analyst = AreaAnalyst(state, second = None)
        self.updateProgress.emit()
        QCoreApplication.processEvents()

        categories = state.getBandGradation(1)

        # Make transition between categories according to
        # number of moved pixel in crosstable
        self.rangeChanged.emit(self.tr("Simulation process %p%"), len(categories)**2 - len(categories))
        QCoreApplication.processEvents()
        for initClass in categories:
            for finalClass in categories:
                if initClass == finalClass: continue

                # TODO: Calculate number of pixels to be moved via TransitionMatrix and state raster
                n = transition.getTransition(initClass, finalClass)   # Number of pixels that have to be
                                                                      # changed the categories
                                                                      # (use TransitoionMatrix only).
                if n==0:
                    continue
                # Find n appropriate places for transition initClass -> finalClass
                cat_code = analyst.encode(initClass, finalClass)
                # Array of places where transitions initClass -> finalClass are occured
                places = (changes==cat_code+1)  # cat_code can be 0, do not mix it with no-data in 'changes' variable
                placesCount = np.sum(places)
                # print "cat_code, placesCount, n", cat_code, placesCount

                if placesCount < n:
                    self.logMessage.emit(self.tr("There are more transitions in the transition matrix, then the model have found"))
                    # print "There are more transitions in the transition matrix, then the model have found"
                    # print "cat_code, placesCount, n", cat_code, placesCount, n
                    QCoreApplication.processEvents()
                    n = placesCount
                if n >0:
                    confidence = self.getConfidence().getBand(1)
                    # Add some random value
                    rnd = np.random.sample(size=confidence.shape)/1000 # A small random
                    confidence = np.ma.filled(confidence, 0) + rnd
                    confidence = confidence * places # The higher is number in cell, the higer is probability of transition in the cell.

                    # Ensure, n is bigger then nonzero confidence
                    placesCount = np.sum(confidence>0)
                    if placesCount < n: # Some confidence where transitions has to be appear is zero. The transition count will be cropped.
                        # print "Some confidence is zero. cat_code, nonzeroConf, wantedPixels", cat_code, placesCount, n
                        n = placesCount

                    ind = confidence.argsort(axis=None)[-n:]
                    indices = [np.unravel_index(i, confidence.shape) for i in ind]

                    # Now "indices" contains indices of the appropriate places,
                    # make transition initClass -> finalClass
                    r1 = np.zeros(confidence.shape)
                    for index in indices:
                        new_state[index] = finalClass

                self.updateProgress.emit()
                QCoreApplication.processEvents()

        result = Raster()
        result.create([new_state], state.getGeodata())
        self.state = result
Пример #13
0
    def __sim(self):
        '''
        1 iteracion of simulation.
        '''
        transition = self.crosstable.getCrosstable()

        self.updatePrediction(self.state)
        changes = self.getPrediction().getBand(1)  # Predicted change map
        changes = changes + 1  # Filling nodata as 0 can be ambiguous:
        changes = np.ma.filled(
            changes,
            0)  #   (cat_code can be 0, to do not mix it with no-data, add 1)
        state = self.getState()
        new_state = state.getBand(1).copy().astype(
            np.uint8
        )  # New states (the result of simulation) will be stored there.

        self.rangeChanged.emit(self.tr("Area Change Analysis %p%"), 2)
        self.updateProgress.emit()
        QCoreApplication.processEvents()
        analyst = AreaAnalyst(state, second=None)
        self.updateProgress.emit()
        QCoreApplication.processEvents()

        categories = state.getBandGradation(1)

        # Make transition between categories according to
        # number of moved pixel in crosstable
        self.rangeChanged.emit(self.tr("Simulation process %p%"),
                               len(categories)**2 - len(categories))
        QCoreApplication.processEvents()
        for initClass in categories:
            for finalClass in categories:
                if initClass == finalClass: continue

                # TODO: Calculate number of pixels to be moved via TransitionMatrix and state raster
                n = transition.getTransition(
                    initClass, finalClass)  # Number of pixels that have to be
                # changed the categories
                # (use TransitoionMatrix only).
                if n == 0:
                    continue
                # Find n appropriate places for transition initClass -> finalClass
                cat_code = analyst.encode(initClass, finalClass)
                # Array of places where transitions initClass -> finalClass are occured
                places = (
                    changes == cat_code + 1
                )  # cat_code can be 0, do not mix it with no-data in 'changes' variable
                placesCount = np.sum(places)
                # print "cat_code, placesCount, n", cat_code, placesCount

                if placesCount < n:
                    self.logMessage.emit(
                        self.
                        tr("There are more transitions in the transition matrix, then the model have found"
                           ))
                    # print "There are more transitions in the transition matrix, then the model have found"
                    # print "cat_code, placesCount, n", cat_code, placesCount, n
                    QCoreApplication.processEvents()
                    n = placesCount
                if n > 0:
                    confidence = self.getConfidence().getBand(1)
                    # Add some random value
                    rnd = np.random.sample(
                        size=confidence.shape) / 1000  # A small random
                    confidence = np.ma.filled(confidence, 0) + rnd
                    confidence = confidence * places  # The higher is number in cell, the higer is probability of transition in the cell.

                    # Ensure, n is bigger then nonzero confidence
                    placesCount = np.sum(confidence > 0)
                    if placesCount < n:  # Some confidence where transitions has to be appear is zero. The transition count will be cropped.
                        # print "Some confidence is zero. cat_code, nonzeroConf, wantedPixels", cat_code, placesCount, n
                        n = placesCount

                    ind = confidence.argsort(axis=None)[-n:]
                    indices = [
                        np.unravel_index(i, confidence.shape) for i in ind
                    ]

                    # Now "indices" contains indices of the appropriate places,
                    # make transition initClass -> finalClass
                    r1 = np.zeros(confidence.shape)
                    for index in indices:
                        new_state[index] = finalClass

                self.updateProgress.emit()
                QCoreApplication.processEvents()

        result = Raster()
        result.create([new_state], state.getGeodata())
        self.state = result