Commit 08c07c9a authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Refactored test_retrieve_unique_points() to be more compact

parent 3bc952c0
......@@ -160,6 +160,15 @@ def test_retrieve_unique_points():
when running the test. The key is to have a DataFrame with points (lon,lat) that repeat
themselves in several entries (=rows) of the DataFrame, hence the strategy of defining a
limited number of coordinates first and combining them to generate the DataFrame.
The test uses two alternative values of the precision parameter (precision_vals). The checks
are the same for both cases. The resulting length of the output DataFrame is verified
against values determined manually (should_be_out_length).
The comparison of the final values of longitude and latitude assigned to the unique points
is carried out going by element of the original input DataFrame, which ends up linked to the
output DataFrame through function_ids_of_unique, which is an array of IDs of unique points
(those of the output DataFrame) associated with each row of the input DataFrame.
"""
# Build a Pandas DataFrame for testing:
......@@ -180,45 +189,30 @@ def test_retrieve_unique_points():
in_crs = "EPSG:4326"
id_str = "IND"
# Case 1: precision = 4
precision_1 = 4
res = gdet_cr_ind.retrieve_unique_points(dummy_df, col_lon, col_lat, id_str,
precision=precision_1, in_crs=in_crs)
function_out_gpd, function_ids_of_unique = res
assert len(function_ids_of_unique) == dummy_df.shape[0]
assert function_out_gpd.shape[0] == 5 # num. of unique points in dummy_df (with precision)
# Compare the original values of lon,lat against those they get assigned according to the
# output, considering the precision used:
for i, cell_id in enumerate(function_ids_of_unique):
orig_lon = dummy_df[col_lon].values[i]
orig_lat = dummy_df[col_lat].values[i]
which_row = np.where(function_out_gpd["id"]==cell_id)[0][0]
function_lon = function_out_gpd[col_lon].values[which_row]
function_lat = function_out_gpd[col_lat].values[which_row]
assert round(orig_lon, precision_1) == round(function_lon, precision_1)
assert round(orig_lat, precision_1) == round(function_lat, precision_1)
# Case 2: precision = 3
precision_2 = 3
res = gdet_cr_ind.retrieve_unique_points(dummy_df, col_lon, col_lat, id_str,
precision=precision_2, in_crs=in_crs)
function_out_gpd, function_ids_of_unique = res
assert len(function_ids_of_unique) == dummy_df.shape[0]
assert function_out_gpd.shape[0] == 3 # num. of unique points in dummy_df (with precision)
# Compare the original values of lon,lat against those they get assigned according to the
# output, considering the precision used:
for i, cell_id in enumerate(function_ids_of_unique):
orig_lon = dummy_df[col_lon].values[i]
orig_lat = dummy_df[col_lat].values[i]
which_row = np.where(function_out_gpd["id"]==cell_id)[0][0]
function_lon = function_out_gpd[col_lon].values[which_row]
function_lat = function_out_gpd[col_lat].values[which_row]
assert round(orig_lon, precision_2) == round(function_lon, precision_2)
assert round(orig_lat, precision_2) == round(function_lat, precision_2)
# Two cases of precision will be tested:
precision_vals = [4, 3]
should_be_out_length = [5, 3] # results in correspondance with precision_vals
# Run the checks:
for k in range(len(precision_vals)):
res = gdet_cr_ind.retrieve_unique_points(dummy_df, col_lon, col_lat, id_str,
precision=precision_vals[k], in_crs=in_crs)
function_out_gpd, function_ids_of_unique = res
assert len(function_ids_of_unique) == dummy_df.shape[0]
# Number of unique points in dummy_df (as a function of the precision):
assert function_out_gpd.shape[0] == should_be_out_length[k]
# Compare the original values of lon,lat against those they get assigned according to the
# output, considering the precision used:
for i, cell_id in enumerate(function_ids_of_unique):
orig_lon = dummy_df[col_lon].values[i]
orig_lat = dummy_df[col_lat].values[i]
which_row = np.where(function_out_gpd["id"]==cell_id)[0][0]
function_lon = function_out_gpd[col_lon].values[which_row]
function_lat = function_out_gpd[col_lat].values[which_row]
assert round(orig_lon, precision_vals[k]) == round(function_lon, precision_vals[k])
assert round(orig_lat, precision_vals[k]) == round(function_lat, precision_vals[k])
def test_define_col_names():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment