def test_correlation_fraction_accepted_025(self): c1 = correlate_maps_cython(self.map1, self.map2, fraction_accepted=0.25) c2 = correlate_maps_simple(self.map1, self.map2, fraction_accepted=0.25) self.assertTrue(np.allclose(c1, c2, equal_nan=True))
def test_reduce(self): # Test if the right shape comes out self.assertTrue( correlate_maps_cython( self.map1, self.map2, window_size=4, reduce=True).shape == (5, 5)) # Test if the right value comes out c1 = self.map1[:4, :4].flatten() c2 = self.map2[:4, :4].flatten() # print(correlate_maps_cython(self.map1, self.map2, window_size=4, reduce=True)) # print(pearsonr(c1, c2)) self.assertTrue( np.allclose( correlate_maps_cython(self.map1, self.map2, window_size=4, reduce=True)[0, 0], pearsonr(c1, c2)[0]))
def test_correlation_value(self): # test the value of the correlation against scipys implementation np.random.seed(0) c1 = np.random.rand(5, 5) c2 = np.random.rand(5, 5) self.assertTrue( np.allclose( pearsonr(c1.flatten(), c2.flatten())[0], correlate_maps_cython(c1, c2, window_size=5)[2, 2]))
def test_tile_correlation(self): loc = "test.tif" self.map1.window_size = 5 self.map2.window_size = 5 self.map1.correlate(self.map2, output_file=loc, window_size=5, ind=32, overwrite=True) t = Raster(loc) c1 = t[0] t.close(verbose=False) c2 = correlate_maps_cython(self.map1[32], self.map2[32], window_size=5) self.assertTrue(np.allclose(c1, c2, equal_nan=True))
def test_correlation(self): loc = "test.tif" self.map1.correlate(self.map2, output_file=loc, window_size=5, overwrite=True) t = Raster(loc) c1 = t[0] t.close(verbose=False) c2 = correlate_maps_cython(self.map1.get_file_data(), self.map2.get_file_data(), window_size=5) self.assertTrue( np.allclose(c1[self.map1.ind_inner], c2[self.map1.ind_inner], equal_nan=True))
def test_correlation_window_size_15(self): c1 = correlate_maps_cython(self.map1, self.map2, window_size=15) c2 = correlate_maps_simple(self.map1, self.map2, window_size=15) self.assertTrue(np.allclose(c1, c2, equal_nan=True))
def test_correlation(self): # fraction_accepted 0.7 and window_size 5 c1 = correlate_maps_cython(self.map1, self.map2) c2 = correlate_maps_simple(self.map1, self.map2) self.assertTrue(np.allclose(c1, c2, equal_nan=True))
def test_assumptions(self): with self.assertRaises((ValueError, IndexError)): # Only 2D is supported correlate_maps_cython(np.random.rand(10, 10, 10), np.random.rand(10, 10, 10)) with self.assertRaises((ValueError, IndexError)): # Only 2D is supported correlate_maps_cython(np.random.rand(10), np.random.rand(10)) with self.assertRaises(ValueError): # fraction accepted needs to be in range 0-1 correlate_maps_cython(self.map1, self.map2, fraction_accepted=-0.1) with self.assertRaises(ValueError): # fraction accepted needs to be in range 0-1 correlate_maps_cython(self.map1, self.map2, fraction_accepted=1.1) with self.assertRaises(ValueError): # window_size should be bigger than 1 correlate_maps_cython(self.map1, self.map2, window_size=1) with self.assertRaises(ValueError): # window_size can't be even correlate_maps_cython(self.map1, self.map2, window_size=4) # window_size can't be even except when reduce=True correlate_maps_cython(self.map1, self.map2, window_size=4, reduce=True)