def test_calculate_average_semivariance_id_as_str(self): # Data prepration my_dir = os.path.dirname(__file__) areal_dataset = os.path.join( my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') a_id = 'idx' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 4 areal_data_prepared = prepare_areal_shapefile(areal_dataset, a_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=a_id, points_val_col_name=points_val) # Set areal semivariance class areal_semivariance = ArealSemivariance(areal_data_prepared, step_size, max_range, points_in_area) areal_semivariance.regularize_semivariogram() # Change ints to str in inblock semivariance for calculations inblock = [[str(x[0]), x[1]] for x in areal_semivariance.inblock_semivariance] inblock = np.asarray(inblock, dtype=object) ar_dists = [ areal_semivariance.distances_between_blocks[0], [str(x) for x in areal_semivariance.distances_between_blocks[1]] ] avg_semi = calculate_average_semivariance( ar_dists, inblock, areal_semivariance.areal_lags, areal_semivariance.areal_ss) data_test_vals = avg_semi.astype(np.int) desired_output = np.array([[0, 0], [1, 0], [1, 24]]) test_vals = np.equal(data_test_vals, desired_output).all() self.assertTrue( test_vals.all(), f"Output is {data_test_vals} ut should be {desired_output}")
def test_calculate_semivariance_within_blocks(self): # Data prepration my_dir = os.path.dirname(__file__) areal_dataset = os.path.join( my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') a_id = 'id' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 4 areal_data_prepared = prepare_areal_shapefile(areal_dataset, a_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=a_id, points_val_col_name=points_val) # Get areal centroids with data values areal_centroids = areal_data_prepared[:, 2:] areal_centroids = np.array([[x[0], x[1], x[2]] for x in areal_centroids]) gamma = calculate_semivariance(areal_centroids, step_size, max_range) # Get theoretical semivariogram model ts = TheoreticalSemivariogram(areal_centroids, gamma) ts.find_optimal_model(number_of_ranges=8) # Get centroids to calculate experimental semivariance inblock_semivariance = calculate_semivariance_within_blocks( points_in_area, ts) inblock_semivariance = np.array(inblock_semivariance) data_point = inblock_semivariance[inblock_semivariance[:, 0] == 1][0] EXPECTED_OUTPUT = 24 output = int(data_point[1]) self.assertEqual( output, EXPECTED_OUTPUT, f"First data point's integer part should be equal to " f"{EXPECTED_OUTPUT} but it is {output}")
def test_ata_pk(self): my_dir = os.path.dirname(__file__) areal_dataset = os.path.join(my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') a_id = 'id' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 10 areal_data_prepared = prepare_areal_shapefile(areal_dataset, a_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=a_id, points_val_col_name=points_val) # Get one area as unknown unknown_area_id = [1] u_points = points_in_area[points_in_area[:, 0] == unknown_area_id][0] k_areas = areal_data_prepared[areal_data_prepared[:, 0] != unknown_area_id] k_points = points_in_area[points_in_area[:, 0] != unknown_area_id] # Semivariance deconvolution semivar_modeling_data = set_areal_weights(k_areas, k_points) smv_model = calculate_weighted_semivariance(semivar_modeling_data, step_size, max_range) semivariogram = TheoreticalSemivariogram(k_areas[:, 2:], smv_model) semivariogram.find_optimal_model() # Poisson Kriging search_radius = max_range / 2 number_of_observations = 3 pkc = AtAPoissonKriging(regularized_model=semivariogram, known_areas=k_areas, known_areas_points=k_points) d = pkc.predict(u_points, number_of_observations, search_radius) self.assertEqual(int(d[0]), 126, "Int of first value should be equal to 126")
def test_regularize_semivariogram_id_as_str(self): reg_mod = RegularizedSemivariogram() # Data prepration my_dir = os.path.dirname(__file__) areal_dataset = os.path.join( my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') areal_id = 'idx' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 4 areal_data_prepared = prepare_areal_shapefile(areal_dataset, areal_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=areal_id, points_val_col_name=points_val) # Fit reg_mod.fit(areal_data_prepared, step_size, max_range, points_in_area) # Transform reg_mod.transform() regularized_smv = np.array([0, 75, 39]) test_output = reg_mod.final_optimal_model.astype(int) check = np.equal(test_output, regularized_smv).all() self.assertTrue( check, "Expected output is {} and an estimated output is {}".format( regularized_smv, test_output))
def test_calculate_block_to_block_semivariance(self): # Data prepration my_dir = os.path.dirname(__file__) areal_dataset = os.path.join( my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') a_id = 'id' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 4 areal_data_prepared = prepare_areal_shapefile(areal_dataset, a_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=a_id, points_val_col_name=points_val) # Set areal semivariance class areal_semivariance = ArealSemivariance(areal_data_prepared, step_size, max_range, points_in_area) areal_semivariance.regularize_semivariogram() blocks = calculate_block_to_block_semivariance( areal_semivariance.within_area_points, areal_semivariance.distances_between_blocks, areal_semivariance.theoretical_semivariance_model) test_block = np.array(blocks[0][0]) mean_semivariance = int(np.mean(test_block[:, 1])) EXPECTED_OUTPUT = 60 self.assertEqual( mean_semivariance, EXPECTED_OUTPUT, f"Average semivariance should be {EXPECTED_OUTPUT} " f"but it is {mean_semivariance}")
def test_areal_semivariance(self): # Data prepration my_dir = os.path.dirname(__file__) areal_dataset = os.path.join( my_dir, '../sample_data/test_areas_pyinterpolate.shp') subset = os.path.join(my_dir, '../sample_data/test_points_pyinterpolate.shp') a_id = 'id' areal_val = 'value' points_val = 'value' # Get maximum range and set step size gdf = gpd.read_file(areal_dataset) total_bounds = gdf.geometry.total_bounds total_bounds_x = np.abs(total_bounds[2] - total_bounds[0]) total_bounds_y = np.abs(total_bounds[3] - total_bounds[1]) max_range = min(total_bounds_x, total_bounds_y) step_size = max_range / 4 areal_data_prepared = prepare_areal_shapefile(areal_dataset, a_id, areal_val) points_in_area = get_points_within_area(areal_dataset, subset, areal_id_col_name=a_id, points_val_col_name=points_val) # Set areal semivariance class areal_semivariance = ArealSemivariance(areal_data_prepared, step_size, max_range, points_in_area) output = areal_semivariance.regularize_semivariogram() test_d = output[:, 1].astype(np.int) correct_output = np.array([0, 63, 46]) tescase = np.equal(test_d, correct_output).all() self.assertTrue( tescase, f"Correct output is {correct_output} but calculated output is {test_d}" )