Fixing values in tests after merging 3.15 (mostly related to ha change) #1603

This commit is contained in:
Claire Simpson 2025-04-07 17:28:24 -06:00
parent 0d152f9409
commit eca9a6f371
2 changed files with 8 additions and 32 deletions

View File

@ -989,6 +989,7 @@ class CropProductionTests(unittest.TestCase):
crop_names = ['corn', 'soybean']
nutrient_df = create_nutrient_df()
yield_percentile_headers = ['25', '50', '75']
pixel_area_ha = 1
output_dir = os.path.join(workspace, "OUTPUT")
os.makedirs(output_dir, exist_ok=True)
file_suffix = 'v1'
@ -1000,9 +1001,9 @@ class CropProductionTests(unittest.TestCase):
aggregate_to_polygons(
base_aggregate_vector_path, target_aggregate_vector_path,
landcover_raster_projection, crop_names,
nutrient_df, yield_percentile_headers, output_dir, file_suffix,
target_aggregate_table_path)
landcover_raster_projection, crop_names, nutrient_df,
yield_percentile_headers, pixel_area_ha, output_dir,
file_suffix, target_aggregate_table_path)
actual_aggregate_pctl_table = pandas.read_csv(
target_aggregate_table_path, dtype=float)
@ -1018,7 +1019,7 @@ class CropProductionTests(unittest.TestCase):
def _create_expected_result_table():
return pandas.DataFrame({
"crop": ["corn", "soybean"], "area (ha)": [20, 40],
"crop": ["corn", "soybean"], "area (ha)": [2, 4],
"production_observed": [4, 7], "production_25": [1.25, 2.25],
"production_50": [2.5, 4.5], "production_75": [3.75, 6.75],
"protein_25": [488250, 675675], "protein_50": [976500, 1351350],
@ -1099,7 +1100,7 @@ class CropProductionTests(unittest.TestCase):
os.makedirs(output_dir, exist_ok=True)
yield_percentile_headers = ['yield_25', 'yield_50', 'yield_75']
crop_names = ['corn', 'soybean']
pixel_area_ha = 10
pixel_area_ha = 1
landcover_raster_path = os.path.join(self.workspace_dir,
"landcover.tif")
landcover_nodata = -1

View File

@ -2343,30 +2343,6 @@ class HabitatQualityTests(unittest.TestCase):
with self.assertRaises(TypeError):
habitat_quality.execute(args)
def test_calculate_total_degradation(self):
"""Test `_calculate_total_degradation`"""
from natcap.invest.habitat_quality import _calculate_total_degradation
deg_raster_list = [os.path.join(self.workspace_dir, f"threat_{i}.tif")
for i in range(6)]
deg_sum_raster_path = os.path.join(self.workspace_dir, "deg_sum.tif")
for i, raster in enumerate(deg_raster_list, start=1):
# make arbitrary arrays
array = numpy.array([[i/10, i/20], [i/8, i/6]])
make_raster_from_array(array, raster)
weight_list = [0.9, 0.2, 0.5]
_calculate_total_degradation(
deg_raster_list, deg_sum_raster_path, weight_list)
actual_result = pygeoprocessing.raster_to_numpy_array(
deg_sum_raster_path)
expected_result = numpy.array([[0.1152, 0.0144], [0.225, 0.533333333]])
numpy.testing.assert_allclose(actual_result, expected_result)
def test_compute_rarity_operation(self):
"""Test `_compute_rarity_operation`"""
from natcap.invest.habitat_quality import _compute_rarity_operation
@ -2413,17 +2389,16 @@ class HabitatQualityTests(unittest.TestCase):
from natcap.invest.habitat_quality import _decay_distance
dist_raster_path = os.path.join(self.workspace_dir, "dist.tif")
max_dist = 2
max_dist = 20
decay_type = 'linear'
target_path = os.path.join(self.workspace_dir, "output.tif")
dist_array = numpy.array([[0, 21, 1], [2, 50, 600]], dtype=float)
make_raster_from_array(dist_array, dist_raster_path, pixel_size=100)
make_raster_from_array(dist_array, dist_raster_path)
_decay_distance(dist_raster_path, max_dist, decay_type, target_path)
actual_output = pygeoprocessing.raster_to_numpy_array(target_path)
expected_output = numpy.array([[1.0, 0.0, 0.95],
[0.9, 0.0, 0.0]])
numpy.testing.assert_allclose(actual_output, expected_output)