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)
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)
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)
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)
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)
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_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') 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) def test_getNeighbours(self): neighbours = self.r2.getNeighbours(row=1,col=0, size=0) self.assertEqual(neighbours, [[1]]) neighbours = self.r2.getNeighbours(row=1,col=1, size=1) assert_array_equal(neighbours, [self.data2]) neighbours = self.r3.getNeighbours(row=1,col=1, size=1) assert_array_equal(neighbours, [self.data2, self.data1]) # Check pixel on the raster bound and nonzero neighbour size self.assertRaises(ProviderError, self.r2.getNeighbours, col=1, row=0, size=1) self.assertRaises(ProviderError, self.r2.getNeighbours, col=1, row=1, size=2) def test_geodata(self): geodata = self.r1.getGeodata() self.r1.setGeoData(geodata) geodata['xSize'] = geodata['xSize'] + 10 self.assertRaises(ProviderError, self.r1.setGeoData, geodata=geodata) 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)
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)
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)
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') 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) def test_getNeighbours(self): neighbours = self.r2.getNeighbours(row=1, col=0, size=0) self.assertEqual(neighbours, [[1]]) neighbours = self.r2.getNeighbours(row=1, col=1, size=1) assert_array_equal(neighbours, [self.data2]) neighbours = self.r3.getNeighbours(row=1, col=1, size=1) assert_array_equal(neighbours, [self.data2, self.data1]) # Check pixel on the raster bound and nonzero neighbour size self.assertRaises(ProviderError, self.r2.getNeighbours, col=1, row=0, size=1) self.assertRaises(ProviderError, self.r2.getNeighbours, col=1, row=1, size=2) def test_geodata(self): geodata = self.r1.getGeodata() self.r1.setGeoData(geodata) geodata['xSize'] = geodata['xSize'] + 10 self.assertRaises(ProviderError, self.r1.setGeoData, geodata=geodata) self.assertTrue(self.r1.geoDataMatch(self.r1)) self.assertTrue( self.r1.geoDataMatch(raster=None, geodata=self.r1.getGeodata())) self.assertTrue(self.r1.geoTransformMatch(self.r1)) self.assertTrue( self.r1.geoTransformMatch(raster=None, geodata=self.r1.getGeodata())) 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) def test_getBandGradation(self): self.assertEqual(set(self.r1.getBandGradation(1)), set([0, 1, 2, 3]))
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)
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)