def test_woe(self): wPlus1 = np.math.log ( (2.0/3 + EPSILON)/(2.0/5 + EPSILON) ) wMinus1 = np.math.log ( (1.0/3 + EPSILON)/(3.0/5 + EPSILON) ) wPlus2 = np.math.log ( (1.0/3 + EPSILON)/(EPSILON) ) wMinus2 = np.math.log ( (2.0/3 + EPSILON)/(1.0 + EPSILON) ) wPlus3 = np.math.log ( (EPSILON)/(3.0/5 + EPSILON) ) wMinus3 = np.math.log ( (1.0 + EPSILON)/(2.0/5 + EPSILON) ) # Binary classes ans = [ [wPlus1, wPlus1, wMinus1,], [wMinus1, wMinus1, wPlus1, ], [None, wMinus1, wPlus1, ] ] ans = ma.array(data=ans, mask=self.mask) np.testing.assert_equal(woe(self.factor, self.sites), ans) # Multiclass w1, w2, w3 = (wPlus1 + wMinus2+wMinus3), (wPlus2 + wMinus1 + wMinus3), (wPlus3 + wMinus1 + wMinus2) ans = [ [w1, w1, w3,], [w3, w2, w1,], [ 0, w3, w1,] ] ans = ma.array(data=ans, mask=self.mask) weights = woe(self.multifact, self.sites) np.testing.assert_equal(ans, weights)
def test_binary_woe(self): wPlus = np.math.log((2.0 / 3 + EPSILON) / (2.0 / 5 + EPSILON)) wMinus = np.math.log((1.0 / 3 + EPSILON) / (3.0 / 5 + EPSILON)) self.assertEqual(_binary_woe(self.factor, self.sites), (wPlus, wMinus)) wPlus = np.math.log((5.0 / 7 + EPSILON) / (0.5 / 3.5 + EPSILON)) wMinus = np.math.log((2.0 / 7 + EPSILON) / (3.0 / 3.5 + EPSILON)) self.assertEqual(_binary_woe(self.bigfactor, self.bigsite, unitcell=2), (wPlus, wMinus)) # if Sites=Factor: wPlus = np.math.log((1 + EPSILON) / EPSILON) wMinus = np.math.log(EPSILON / (1 + EPSILON)) self.assertEqual(_binary_woe(self.factor, self.factor), (wPlus, wMinus)) # Check areas size self.assertRaises(WoeError, _binary_woe, self.factor, self.zero) self.assertRaises(WoeError, _binary_woe, self.zero, self.sites) self.assertRaises(WoeError, _binary_woe, self.bigfactor, self.bigsite, 3) # Non-binary sites self.assertRaises(WoeError, woe, self.fact1, self.sites1) # Assert does not raises woe(self.multifact, self.sites2)
def test_woe(self): wPlus1 = np.math.log((2.0 / 3 + EPSILON) / (2.0 / 5 + EPSILON)) wMinus1 = np.math.log((1.0 / 3 + EPSILON) / (3.0 / 5 + EPSILON)) wPlus2 = np.math.log((1.0 / 3 + EPSILON) / (EPSILON)) wMinus2 = np.math.log((2.0 / 3 + EPSILON) / (1.0 + EPSILON)) wPlus3 = np.math.log((EPSILON) / (3.0 / 5 + EPSILON)) wMinus3 = np.math.log((1.0 + EPSILON) / (2.0 / 5 + EPSILON)) # Binary categories ans = [[ wPlus1, wPlus1, wMinus1, ], [ wMinus1, wMinus1, wPlus1, ], [ None, wMinus1, wPlus1, ]] ans = ma.array(data=ans, mask=self.mask) w = woe(self.factor, self.sites) np.testing.assert_equal(w['map'], ans) # Multiclass w1, w2, w3 = (wPlus1 + wMinus2 + wMinus3), (wPlus2 + wMinus1 + wMinus3), (wPlus3 + wMinus1 + wMinus2) ans = [[ w1, w1, w3, ], [ w3, w2, w1, ], [ 0, w3, w1, ]] ans = ma.array(data=ans, mask=self.mask) weights = woe(self.multifact, self.sites) np.testing.assert_equal(ans, weights['map'])
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_binary_woe(self): wPlus = np.math.log ( (2.0/3 + EPSILON)/(2.0/5 + EPSILON) ) wMinus = np.math.log ( (1.0/3 + EPSILON)/(3.0/5 + EPSILON) ) self.assertEqual(_binary_woe(self.factor, self.sites), (wPlus, wMinus)) wPlus = np.math.log ( (5.0/7 + EPSILON)/(0.5/3.5 + EPSILON) ) wMinus = np.math.log ( (2.0/7 + EPSILON)/(3.0/3.5 + EPSILON) ) self.assertEqual(_binary_woe(self.bigfactor, self.bigsite, unitcell=2), (wPlus, wMinus)) # if Sites=Factor: wPlus = np.math.log ( (1 + EPSILON)/EPSILON ) wMinus = np.math.log ( EPSILON/(1 + EPSILON) ) self.assertEqual(_binary_woe(self.factor, self.factor), (wPlus, wMinus)) # Check areas size self.assertRaises(WoeError, _binary_woe, self.factor, self.zero) self.assertRaises(WoeError, _binary_woe, self.zero, self.sites) self.assertRaises(WoeError, _binary_woe, self.bigfactor, self.bigsite, 3) # Non-binary sites self.assertRaises(WoeError, woe, self.fact1, self.sites1) # Assert does not raises woe(self.multifact, self.sites2)
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)
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)