fixes #BITBUCKET-3833 guarding against ratio == 1, adding test, and updating history

This commit is contained in:
Rich Sharp 2019-01-14 11:11:57 -08:00
parent 38b7120482
commit 651ceb2b93
3 changed files with 46 additions and 8 deletions

View File

@ -57,6 +57,8 @@ Unreleased Changes
A DQ score of 1 should represent better data quality whereas the score of 3 is
worse data quality. A weight score of 1 is more important, whereas that of 3
is less important.
* Fixing a case where a zero discount rate and rate of change in the carbon
model would cause a divide by zero error.
3.5.0 (2018-08-14)
------------------

View File

@ -169,9 +169,9 @@ def execute(args):
storage_path_list = []
for pool_type in ['c_above', 'c_below', 'c_soil', 'c_dead']:
carbon_pool_by_type = dict([
(lucode, float(carbon_pool_table[lucode][pool_type]))
(lucode, float(carbon_pool_table[lucode][pool_type]))
for lucode in carbon_pool_table])
lulc_key = 'lulc_%s_path' % scenario_type
storage_key = '%s_%s' % (pool_type, scenario_type)
LOGGER.info(
@ -214,7 +214,7 @@ def execute(args):
_diff_rasters,
args=(storage_path_list, file_registry[output_key]),
target_path_list=[file_registry[output_key]],
dependent_task_list=[sum_rasters_task_lookup['cur'],
dependent_task_list=[sum_rasters_task_lookup['cur'],
sum_rasters_task_lookup[scenario_type]],
task_name='diff_rasters_for_%s' % output_key)
diff_rasters_task_lookup[scenario_type] = diff_rasters_task
@ -244,10 +244,10 @@ def execute(args):
task_name='calculate_%s' % output_key)
calculate_npv_tasks.append(calculate_npv_task)
tifs_to_summarize.add(file_registry[output_key])
# Report aggregate results
tasks_to_report = (sum_rasters_task_lookup.values()
+ diff_rasters_task_lookup.values()
tasks_to_report = (sum_rasters_task_lookup.values()
+ diff_rasters_task_lookup.values()
+ calculate_npv_tasks)
generate_report_task = graph.add_task(
_generate_report,
@ -363,8 +363,9 @@ def _calculate_valuation_constant(
(1 + float(rate_change) / 100.0)))
valuation_constant = (
float(price_per_metric_ton_of_c) /
(float(lulc_fut_year) - float(lulc_cur_year)) *
(1.0 - ratio ** (n_years + 1)) / (1.0 - ratio))
(float(lulc_fut_year) - float(lulc_cur_year)))
if ratio != 1.0:
valuation_constant *= (1.0 - ratio ** (n_years + 1)) / (1.0 - ratio)
return valuation_constant

View File

@ -121,6 +121,41 @@ class CarbonTests(unittest.TestCase):
assert_raster_equal_value(
os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.4602106)
def test_carbon_zero_rates(self):
"""Carbon: test with 0 discount and rate change."""
from natcap.invest import carbon
args = {
u'workspace_dir': self.workspace_dir,
u'do_valuation': True,
u'price_per_metric_ton_of_c': 43.0,
u'rate_change': 0.0,
u'lulc_cur_year': 2016,
u'lulc_fut_year': 2030,
u'discount_rate': 0.0,
u'n_workers': -1,
}
# Create LULC rasters and pools csv in workspace and add them to args.
lulc_names = ['lulc_cur_path', 'lulc_fut_path', 'lulc_redd_path']
for fill_val, lulc_name in enumerate(lulc_names, 1):
args[lulc_name] = os.path.join(args['workspace_dir'],
lulc_name + '.tif')
make_simple_raster(args[lulc_name], fill_val)
args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
'pools.csv')
make_pools_csv(args['carbon_pools_path'])
carbon.execute(args)
# Add assertions for npv for future and REDD scenarios.
# The npv was calculated based on _calculate_npv in carbon.py.
assert_raster_equal_value(
os.path.join(args['workspace_dir'], 'npv_fut.tif'), -0.0178143)
assert_raster_equal_value(
os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.0239571)
def test_carbon_future(self):
"""Carbon: regression testing future scenario."""
from natcap.invest import carbon