当前位置: 首页>>代码示例>>Python>>正文


Python Raster.create方法代码示例

本文整理汇总了Python中molusce.algorithms.dataprovider.Raster.create方法的典型用法代码示例。如果您正苦于以下问题:Python Raster.create方法的具体用法?Python Raster.create怎么用?Python Raster.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在molusce.algorithms.dataprovider.Raster的用法示例。


在下文中一共展示了Raster.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: sim

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
    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()
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:62,代码来源:sim.py

示例2: TestSimulator

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
class TestSimulator(unittest.TestCase):
    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)

    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)
开发者ID:nextgis,项目名称:molusce,代码行数:60,代码来源:test_sim.py

示例3: train

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
    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()
开发者ID:nextgis,项目名称:molusce,代码行数:56,代码来源:manager.py

示例4: errorMap

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
 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
开发者ID:nextgis,项目名称:molusce,代码行数:15,代码来源:sim.py

示例5: test_WoeManager

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
    def test_WoeManager(self):
        aa = AreaAnalyst(self.sites, self.sites)
        w1 = WoeManager([self.factor], aa)
        p = w1.getPrediction(self.sites).getBand(1)
        assert_array_equal(p, self.sites.getBand(1))

        initState = Raster("../../examples/data.tif")
        finalState = Raster("../../examples/data1.tif")
        aa = AreaAnalyst(initState, finalState)
        w = WoeManager([initState], aa)
        p = w.getPrediction(initState).getBand(1)

        # 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 index of finalClass that maximizes weights of transiotion initClass -> finalClass
        answer = [[1, 1, 1, 1], [1, 1, 3, 3], [3, 3, 3, 3], [1, 1, 1, 1]]
        assert_array_equal(p, answer)

        w = WoeManager([initState], aa, bins={0: [[2]]})
        p = w.getPrediction(initState).getBand(1)
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:51,代码来源:test_manager.py

示例6: makeChangeMap

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
 def makeChangeMap(self):
     f, s = self.first, self.second
     rows, cols = self.geodata['ySize'], self.geodata['xSize']
     band = np.zeros([rows, cols])
     self.rangeChanged.emit(self.tr("Creating change map %p%"), rows)
     for i in xrange(rows):
         for j in xrange(cols):
             if not f.mask[i,j]:
                 r = f[i,j]
                 c = s[i,j]
                 band[i, j] = self.encode(r, c)
         self.updateProgress.emit()
     bands = [np.ma.array(data = band, mask = f.mask)]
     raster = Raster()
     raster.create(bands, self.geodata)
     self.processFinished.emit(raster)
     self.changeMap = raster
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:19,代码来源:manager.py

示例7: Model

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
class Model(object):
    """
    Simple predicting model for Simulator tests
    """

    def __init__(self, state):
        self._predict(state)

    def getConfidence(self):
        return self.confidence

    def getPrediction(self, state, factors=None, calcTransitions=False):
        self._predict(state, factors)
        return self.prediction

    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)
开发者ID:nextgis,项目名称:molusce,代码行数:41,代码来源:test_sim.py

示例8: Model

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
class Model(object):
    '''
    Simple predicting model for Simulator tests
    '''
    def __init__(self, state):
        self.state = state
        self._predict(state)
    
    def getConfidence(self):
        return self.confidence
        
    def getPrediction(self, state, factors=None):
        self._predict(state, factors)
        return self.prediction
        
    def _predict(self, state, factors = None):
        geodata = self.state.getGeodata()
        band = state.getBand(1)
        rows, cols = geodata['ySize'], geodata['xSize']
        # Let the prediction is: 1 -> 2, 2- >3, 3 -> 1
        
        predicted_band  = np.copy(band)
        predicted_band[band == 1] = 2
        predicted_band[band == 2] = 3
        predicted_band[band == 3] = 1
        
        # 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)
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:40,代码来源:test_sim.py

示例9: makeChangeMap

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
    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)
开发者ID:nextgis,项目名称:molusce,代码行数:38,代码来源:manager.py

示例10: WoeManager

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]

#.........这里部分代码省略.........

            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()

    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]
开发者ID:nextgis,项目名称:molusce,代码行数:70,代码来源:manager.py

示例11: WoeManager

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]

#.........这里部分代码省略.........
        self.analyst = areaAnalyst
        self.changeMap   = areaAnalyst.getChangeMap()

        self.prediction = None
        self.confidence = None

        if (bins != None) and (len(factors) != len(bins.keys())):
            raise WoeManagerError('Lengths of bins and factors are different!')

        for r in self.factors:
            if not self.changeMap.geoDataMatch(r):
                raise WoeManagerError('Geometries of the input rasters are different!')

        if self.changeMap.getBandsCount() != 1:
            raise WoeManagerError('Change map must have one band!')

        # Get list of codes from the changeMap raster
        classes = self.changeMap.getBandStat(1)['gradation']
        cMap = self.changeMap.getBand(1)

        self.codes = [int(c) for c in classes]    # Codes of transitions initState->finalState (see AreaAnalyst.encode)

        self.woe = {}
        for code in self.codes:
            sites = binaryzation(cMap, [code])
            # TODO: reclass factors (continuous factor -> ordinal factor)
            wMap = np.ma.zeros(cMap.shape)
            for k in xrange(len(factors)):
                fact = factors[k]
                if bins: # Get bins of the factor
                    bin = 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:
                        band = reclass(band, bin[i-1])
                    band, sites = masks_identity(band, sites)   # Combine masks of the rasters
                    weights = woe(band, sites, unit_cell)       # WoE for the 'code' (initState->finalState) transition and current 'factor'.
                    wMap = wMap + weights
            self.woe[code]=wMap             # WoE for all factors and the transition.


    def getConfidence(self):
        return self.confidence

    def getPrediction(self, state, factors=None):
        '''
        Most of the models use factors for prediction, but WoE takes list of factors only once (during the initialization).
        '''
        self._predict(state)
        return self.prediction

    def getWoe(self):
        return self.woe

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

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

        woe = self.getWoe()
        stateBand = state.getBand(1)

        for r in xrange(rows):
            for c in xrange(cols):
                oldMax, currMax = -1000, -1000  # Small numbers
                indexMax = -1                   # Index of Max weight
                initClass = stateBand[r,c]      # Init class (state before transition)
                try:
                    codes = self.analyst.codes(initClass)   # Possible final states
                    for code in codes:
                        try: # If not all possible transitions are presented in the changeMap
                            map = 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
                    decode = self.analyst.decode(indexMax)    # Get init & final classes (initState, finalState)
                    prediction[r,c] = decode[1]               # final class
                    confidence[r,c] = sigmoid(currMax) - sigmoid(oldMax)
                except ValueError:
                    mask[r,c] = 1

        predicted_band = np.ma.array(data=prediction, mask=mask)
        self.prediction = Raster()
        self.prediction.create([predicted_band], geodata)
        confidence_band = np.ma.array(data=confidence, mask=mask)
        self.confidence = Raster()
        self.confidence.create([confidence_band], geodata)
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:104,代码来源:manager.py

示例12: LR

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]

#.........这里部分代码省略.........
                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()

    def __propagateSamplerSignals(self):
        self.sampler.rangeChanged.connect(self.__samplerProgressRangeChanged)
        self.sampler.updateProgress.connect(self.__samplerProgressChanged)
        self.sampler.samplingFinished.connect(self.__samplerFinished)

    def __samplerFinished(self):
        self.sampler.rangeChanged.disconnect(self.__samplerProgressRangeChanged)
        self.sampler.updateProgress.disconnect(self.__samplerProgressChanged)
        self.sampler.samplingFinished.disconnect(self.__samplerFinished)
        self.samplingFinished.emit()

    def __samplerProgressRangeChanged(self, message, maxValue):
        self.rangeChanged.emit(message, maxValue)

    def __samplerProgressChanged(self):
开发者ID:nextgis,项目名称:molusce,代码行数:70,代码来源:lr.py

示例13: MCE

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]

#.........这里部分代码省略.........

        self.wMatr = np.array(wMatr)

        self.weights = None     # Weights of the factors, calculated using wMatr
                                # It's a list, the length is self.dim
                                # first element is the weight of first band of the first factor and so on:
                                # [W_f1, ... weights of 1-st factors ... , W_f2, ... weights of 1-st factors..., W_fn, ...]

        self.consistency =None  # Consistency ratio of the comparison matrix.

        self.prediction = None
        self.confidence = None


    def getConsistency(self):
        if self.consistency == None:
            self.setWeights()
        return self.consistency

    def getConfidence(self):
        return self.confidence

    def getPrediction(self, state, factors=None):
        '''
        Most of the models use factors for prediction, but WoE takes list of factors only once (during the initialization).
        '''
        self._predict(state)
        return self.prediction

    def getWeights(self):
        if self.weights == None:
            self.setWeights()
        return self.weights

    def _predict(self, state):
        '''
        Predict the changes.
        '''
        geodata = state.getGeodata()
        rows, cols = geodata['ySize'], geodata['xSize']

        # 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
        # Confidence:
        #   confidence is summary map of factors, if current state = self.initState
        #   confidence is 0, if current state != self.initState
        # Prediction:
        #   predicted value is a constant = self.finalStateNum, if current state = self.initState
        #   predicted value is current state, if current state != self.initState
        confidence = np.zeros((rows,cols))
        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]
                mask = np.ma.mask_or(mask, band.mask)
                weightNum = weightNum + 1
        confidence = confidence*initStateMask
        prediction = np.copy(state.getBand(1))
        prediction = np.logical_not(initStateMask) * prediction
        prediction = prediction + initStateMask*self.finalStateNum

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

    def setWeights(self):
        '''
        Calculate the weigths and consistency ratio.
        '''
        # Weights
        w, v = np.linalg.eig(self.wMatr)
        maxW = np.max(w)
        maxInd = list(w).index(maxW)    # Index of the biggest eigenvalue
        maxW = maxW.real
        v = v[:,maxInd]       # The eigen vector
        self.weights = [x.real for x in v]  # Maxtix v can be complex
        self.weights = self.weights/sum(self.weights)

        # Consistency ratio
        if self.dim > 2:
            ci = (maxW - self.dim)/(self.dim - 1)
            try:
                ri = self.randomConsistencyIndex[self.dim]
                self.consistency = ci/ri
            except KeyError:
                self.consistency = -1
        else:
            self.consistency = 0
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:104,代码来源:mce.py

示例14: MlpManager

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
class MlpManager(QObject):
    '''This class gets the data extracted from the UI and
    pass it to multi-layer perceptron, then gets and stores the result.
    '''

    updateGraph = pyqtSignal(float, float)      # Train error, val. error
    updateMinValErr = pyqtSignal(float)         # Min validation error
    updateDeltaRMS  = pyqtSignal(float)         # Delta of RMS: min(valError) - currentValError
    processFinished = pyqtSignal()
    logMessage = pyqtSignal(str)


    def __init__(self, ns=0, MLP=None):

        QObject.__init__(self)

        self.MLP = MLP

        self.layers = None
        if self.MLP:
            self.layers = self.getMlpTopology()

        self.ns = ns            # Neighbourhood size of training rasters.
        self.data = None        # Training data
        self.classlist   = None # List of unique output values of the output raster
        self.train_error = None # Error on training set
        self.val_error   = None # Error on validation set
        self.minValError = None # The minimum error that is achieved on the validation set

        # Results of the MLP prediction
        self.prediction = None  # Raster of the MLP prediction results
        self.confidence = None  # Raster of the MLP results confidence

        # Outputs of the activation function for small and big numbers
        self.sigmax, self.sigmin = sigmoid(100), sigmoid(-100)  # Max and Min of the sigmoid function
        self.sigrange = self.sigmax - self.sigmin               # Range of the sigmoid

    def computeMlpError(self, sample):
        '''Get MLP error on the sample'''
        input = np.hstack( (sample['state'], sample['factors']) )
        out = self.getOutput( input )
        err = ((sample['output'] - out)**2).sum()/len(out)
        return err

    def computePerformance(self, train_indexes, val_ind):
        '''Check errors of training and validation sets
        @param train_indexes     Tuple that contains indexes of the first and last elements of the training set.
        @param val_ind           Tuple that contains indexes of the first and last elements of the validation set.
        '''
        train_error = 0
        train_sampl = train_indexes[1] - train_indexes[0]       # Count of training samples
        for i in range(train_indexes[0], train_indexes[1]):
            train_error = train_error + self.computeMlpError(sample = self.data[i])
        self.setTrainError(train_error/train_sampl)

        if val_ind:
            val_error = 0
            val_sampl = val_ind[1] - val_ind[0]
            for i in xrange(val_ind[0], val_ind[1]):
                val_error = val_error + self.computeMlpError(sample = self.data[i])
            self.setValError(val_error/val_sampl)

    def copyWeights(self):
        '''Deep copy of the MLP weights'''
        return copy.deepcopy(self.MLP.weights)

    def createMlp(self, state, factors, output, hidden_layers):
        '''
        @param state            Raster of the current state (classes) values.
        @param factors          List of the factor rasters (predicting variables).
        @param hidden_layers    List of neuron counts in hidden layers.
        @param ns               Neighbourhood size.
        '''

        if output.getBandsCount() != 1:
            raise MplManagerError('Output layer must have one band!')

        input_neurons = 0
        for raster in [state] + factors:
            input_neurons = input_neurons+ raster.getNeighbourhoodSize(self.ns)


        # Output class (neuron) count
        band = output.getBand(1)
        self.classlist = np.unique(band.compressed())
        classes = len(self.classlist)

        # set neuron counts in the MLP layers
        self.layers = hidden_layers
        self.layers.insert(0, input_neurons)
        self.layers.append(classes)

        self.MLP = MLP(*self.layers)

    def getConfidence(self):
        return self.confidence

    def getInputVectLen(self):
        '''Length of input data vector of the MLP'''
        shape = self.getMlpTopology()
#.........这里部分代码省略.........
开发者ID:asiaairsurvey,项目名称:molusce,代码行数:103,代码来源:manager.py

示例15: __sim

# 需要导入模块: from molusce.algorithms.dataprovider import Raster [as 别名]
# 或者: from molusce.algorithms.dataprovider.Raster import create [as 别名]
    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
开发者ID:nextgis,项目名称:molusce,代码行数:79,代码来源:sim.py


注:本文中的molusce.algorithms.dataprovider.Raster.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。