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


Python Raster.getBandGradation方法代码示例

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


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

示例1: TestMlpManager

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

#.........这里部分代码省略.........
        assert_array_equal(data[0]['output'], mng.data[0]['output'])
        assert_array_equal(data[0]['state'], mng.data[0]['state'])

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

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

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

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

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

    def test_train(self):
        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        mng.setTrainingData(self.output, self.factors, self.output)

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

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

        self.assertTrue(not all(mask.flatten()))

    def test_predict(self):
        mng = MlpManager()
        mng.createMlp(self.output, self.factors, self.output, [10])
        weights = mng.copyWeights()

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


        # Prediction ( the output must be sigmoid(0) )
        raster = mng.getPrediction(self.output, self.factors, calcTransitions=True)
        assert_array_equal(raster.getBand(1), sigmoid(0)*np.ones((3,3)))
        # Confidence is zero
        confid = mng.getConfidence()
        self.assertEquals(confid.getBand(1).dtype, np.uint8)
        assert_array_equal(confid.getBand(1), np.zeros((3,3)))
        # Transition Potentials (is (sigmoid(0) - sigmin)/sigrange )
        potentials = mng.getTransitionPotentials()
        cats = self.output.getBandGradation(1)
        for cat in cats:
            map = potentials[cat]
            self.assertEquals(map.getBand(1).dtype, np.uint8)
            assert_array_equal(map.getBand(1), 50*np.ones((3,3)))
开发者ID:nextgis,项目名称:molusce,代码行数:104,代码来源:test_manager.py

示例2: test_create

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

示例3: TestLRManager

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

    def test_LR(self):
        #~ data = [
            #~ [3.0, 1.0, 3.0],
            #~ [3.0, 1.0, 3.0],
            #~ [0,   3.0, 1.0]
        #~ ]
        #~ result = np.ma.array(data = data, mask = (data==0))

        lr = LR(ns=0)   # 3-class problem
        lr.setState(self.state)
        lr.setFactors(self.factors)
        lr.setOutput(self.output)
        lr.setTrainingData()
        lr.train()
        predict = lr.getPrediction(self.state, self.factors)
        predict = predict.getBand(1)
        assert_array_equal(predict, self.output.getBand(1))

        lr = LR(ns=1) # Two-class problem (it's because of boundary effect)
        lr.setState(self.state1)
        lr.setFactors(self.factors1)
        lr.setOutput(self.output1)
        lr.setTrainingData()
        lr.train()
        predict = lr.getPrediction(self.state1, self.factors1, calcTransitions=True)
        predict = predict.getBand(1)
        self.assertEquals(predict.dtype, np.uint8)
        data = [
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 2.0, 0.0],
            [0.0, 2.0, 2.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
        ]
        result = np.ma.array(data = data, mask = (data==0))
        assert_array_equal(predict, result)

        # Confidence is zero
        confid = lr.getConfidence()
        self.assertEquals(confid.getBand(1).dtype, np.uint8)

        # Transition Potentials
        potentials = lr.getTransitionPotentials()
        cats = self.output.getBandGradation(1)
        for cat in [1.0, 2.0]:
            map = potentials[cat]
            self.assertEquals(map.getBand(1).dtype, np.uint8)
开发者ID:nextgis,项目名称:molusce,代码行数:66,代码来源:test_lr.py

示例4: TestRaster

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

    def test_RasterInit(self):
        self.assertEqual(self.r1.getBandsCount(), 1)
        band = self.r1.getBand(1)
        shape = band.shape
        x = self.r1.getXSize()
        y = self.r1.getYSize()
        self.assertEqual(shape, (x,y))

        self.assertEqual(self.r2.getBandsCount(), 1)
        band = self.r2.getBand(1)
        assert_array_equal(band, self.data2)

        self.assertTrue(self.r1.geoDataMatch(self.r2))

        self.assertTrue(self.r1.isMetricProj())

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


    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)

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


    def test_getBandStat(self):
        stat = self.r1.getBandStat(1)
        self.assertAlmostEqual(stat['mean'], 15.0/9)
        self.assertAlmostEqual(stat['std'], np.sqrt(10.0/9))

    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')
#.........这里部分代码省略.........
开发者ID:nextgis,项目名称:molusce,代码行数:103,代码来源:test_dataprovider.py


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