Pass aggregate_results_table_path to aggregate_regression_results_to_polygons instead of redefining it in the function

This commit is contained in:
Emily Davis 2025-02-12 10:02:27 -07:00
parent 3a13f1012b
commit 76494c6e8e
2 changed files with 17 additions and 17 deletions

View File

@ -831,6 +831,7 @@ def execute(args):
func=aggregate_regression_results_to_polygons,
args=(args['aggregate_polygon_path'],
target_aggregate_vector_path,
aggregate_results_table_path,
landcover_raster_info['projection_wkt'],
crop_names, nutrient_df, pixel_area_ha,
output_dir, file_suffix),
@ -1033,8 +1034,8 @@ def tabulate_regression_results(
def aggregate_regression_results_to_polygons(
base_aggregate_vector_path, target_aggregate_vector_path,
landcover_raster_projection, crop_names,
nutrient_df, pixel_area_ha, output_dir, file_suffix):
aggregate_results_table_path, landcover_raster_projection,
crop_names, nutrient_df, pixel_area_ha, output_dir, file_suffix):
"""Write table with aggregate results of yield and nutrient values.
Use zonal statistics to summarize total observed and interpolated
@ -1043,8 +1044,10 @@ def aggregate_regression_results_to_polygons(
Args:
base_aggregate_vector_path (string): path to polygon vector
target_aggregate_vector_path (string):
path to re-projected copy of polygon vector
target_aggregate_vector_path (string): path to re-projected copy of
polygon vector
aggregate_results_table_path (string): path to CSV file where aggregate
results will be reported.
landcover_raster_projection (string): a WKT projection string
crop_names (list): list of crop names
nutrient_df (pandas.DataFrame): a table of nutrient values by crop
@ -1115,9 +1118,7 @@ def aggregate_regression_results_to_polygons(
* nutrient_df[nutrient_id][crop_name]) # nutrient unit per 100g crop
# report everything to a table
aggregate_table_path = os.path.join(
output_dir, _AGGREGATE_TABLE_FILE_PATTERN % file_suffix)
with open(aggregate_table_path, 'w') as aggregate_table:
with open(aggregate_results_table_path, 'w') as aggregate_table:
# write header
aggregate_table.write('FID,')
aggregate_table.write(','.join(sorted(total_yield_lookup)) + ',')

View File

@ -834,24 +834,23 @@ class CropProductionTests(unittest.TestCase):
os.makedirs(output_dir, exist_ok=True)
file_suffix = 'test'
pixel_area_ha = 10
_create_crop_rasters(output_dir, crop_names, file_suffix)
aggregate_regression_results_to_polygons(
base_aggregate_vector_path, target_aggregate_vector_path,
landcover_raster_projection, crop_names,
nutrient_df, pixel_area_ha, output_dir, file_suffix)
_AGGREGATE_TABLE_FILE_PATTERN = os.path.join(
'.', 'aggregate_results%s.csv')
aggregate_table_path = os.path.join(
output_dir, _AGGREGATE_TABLE_FILE_PATTERN % file_suffix)
pixel_area_ha = 10
_create_crop_rasters(output_dir, crop_names, file_suffix)
aggregate_regression_results_to_polygons(
base_aggregate_vector_path, target_aggregate_vector_path,
aggregate_table_path, landcover_raster_projection, crop_names,
nutrient_df, pixel_area_ha, output_dir, file_suffix)
actual_aggregate_table = pandas.read_csv(aggregate_table_path,
dtype=float)
print(actual_aggregate_table)
expected_aggregate_table = _create_expected_agg_table()