def test_missing_aoi(self):
     """Forest carbon edge: ensure missing AOI causes exception."""
     from natcap.invest import forest_carbon_edge_effect
     args = {
         'biomass_to_carbon_conversion_factor':
         '0.47',
         'biophysical_table_path':
         os.path.join(REGRESSION_DATA, 'input',
                      'no_forest_edge_carbon_lu_table_bad_pool_value.csv'),
         'compute_forest_edge_effects':
         False,
         'lulc_raster_path':
         os.path.join(REGRESSION_DATA, 'input', 'small_lulc.tif'),
         'n_nearest_model_points':
         1,
         'pools_to_calculate':
         'all',
         'results_suffix':
         'small_no_edge_effect',
         'tropical_forest_edge_carbon_model_vector_path':
         os.path.join(REGRESSION_DATA, 'input', 'core_data',
                      'forest_carbon_edge_regression_model_parameters.shp'),
         'workspace_dir':
         self.workspace_dir,
         'n_workers':
         -1
     }
     args['aoi_vector_path'] = os.path.join('path', 'to', 'nonexistant',
                                            'aoi.shp')
     with self.assertRaises(ValueError) as cm:
         forest_carbon_edge_effect.execute(args)
     expected_message = 'Unable to open aoi at:'
     actual_message = str(cm.exception)
     self.assertTrue(expected_message in actual_message, actual_message)
    def test_carbon_nodata_lulc(self):
        """Forest Carbon Edge: ensure nodata lulc raster cause exception"""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'aoi_vector_path':
            os.path.join(REGRESSION_DATA, 'input', 'small_aoi.shp'),
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_path':
            os.path.join(REGRESSION_DATA, 'input',
                         'forest_edge_carbon_lu_table.csv'),
            'compute_forest_edge_effects':
            True,
            'lulc_raster_path':
            os.path.join(REGRESSION_DATA, 'input', 'nodata_lulc.tif'),
            'n_nearest_model_points':
            10,
            'pools_to_calculate':
            'all',
            'tropical_forest_edge_carbon_model_vector_path':
            os.path.join(REGRESSION_DATA, 'input', 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
            'n_workers':
            -1
        }
        with self.assertRaises(ValueError) as cm:
            forest_carbon_edge_effect.execute(args)
        expected_message = 'The landcover raster '
        actual_message = str(cm.exception)
        self.assertTrue(expected_message in actual_message, actual_message)
    def test_carbon_bad_pool_value(self):
        """Forest Carbon Edge: test with bad carbon pool value."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_path':
            os.path.join(REGRESSION_DATA, 'input',
                         'no_forest_edge_carbon_lu_table_bad_pool_value.csv'),
            'compute_forest_edge_effects':
            False,
            'lulc_raster_path':
            os.path.join(REGRESSION_DATA, 'input', 'small_lulc.tif'),
            'n_nearest_model_points':
            1,
            'pools_to_calculate':
            'all',
            'results_suffix':
            'small_no_edge_effect',
            'tropical_forest_edge_carbon_model_vector_path':
            os.path.join(REGRESSION_DATA, 'input', 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
            'n_workers':
            -1
        }

        with self.assertRaises(ValueError) as cm:
            forest_carbon_edge_effect.execute(args)
        expected_message = 'Could not interpret carbon pool value'
        actual_message = str(cm.exception)
        self.assertTrue(expected_message in actual_message, actual_message)
    def test_carbon_small_dup_output(self):
        """Forest Carbon Edge: small test for existing output overlap."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'aoi_uri':
            os.path.join(REGRESSION_DATA, 'small_aoi.shp'),
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_uri':
            os.path.join(SAMPLE_DATA, 'forest_edge_carbon_lu_table.csv'),
            'compute_forest_edge_effects':
            True,
            'lulc_uri':
            os.path.join(REGRESSION_DATA, 'small_lulc.tif'),
            'n_nearest_model_points':
            '1',
            'pools_to_calculate':
            'above_ground',
            'results_suffix':
            'small',
            'tropical_forest_edge_carbon_model_shape_uri':
            os.path.join(SAMPLE_DATA, 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
        }

        # explictily testing that invoking twice doesn't cause the model to
        # crash because of exising outputs
        forest_carbon_edge_effect.execute(args)
        forest_carbon_edge_effect.execute(args)
        self.assertTrue(True)  # explict pass of the model
    def test_carbon_small_bad_pool_value(self):
        """Forest Carbon Edge: small test with bad carbon pool value."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_uri':
            os.path.join(REGRESSION_DATA,
                         'no_forest_edge_carbon_lu_table_bad_pool_value.csv'),
            'compute_forest_edge_effects':
            False,
            'lulc_uri':
            os.path.join(REGRESSION_DATA, 'small_lulc.tif'),
            'n_nearest_model_points':
            '1',
            'pools_to_calculate':
            'all',
            'results_suffix':
            'small_no_edge_effect',
            'tropical_forest_edge_carbon_model_shape_uri':
            os.path.join(SAMPLE_DATA, 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
        }

        with self.assertRaises(ValueError):
            forest_carbon_edge_effect.execute(args)
    def test_carbon_small_no_forest_edge(self):
        """Forest Carbon Edge: small test for no forest edge effects."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'aoi_uri':
            os.path.join(REGRESSION_DATA, 'small_aoi.shp'),
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_uri':
            os.path.join(REGRESSION_DATA,
                         'no_forest_edge_carbon_lu_table.csv'),
            'compute_forest_edge_effects':
            False,
            'lulc_uri':
            os.path.join(REGRESSION_DATA, 'small_lulc.tif'),
            'n_nearest_model_points':
            '1',
            'pools_to_calculate':
            'above_ground',
            'results_suffix':
            'small_no_edge_effect',
            'tropical_forest_edge_carbon_model_shape_uri':
            os.path.join(SAMPLE_DATA, 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
        }

        forest_carbon_edge_effect.execute(args)

        ForestCarbonEdgeTests._test_same_files(
            os.path.join(REGRESSION_DATA,
                         'file_list_small_no_edge_effect.txt'),
            args['workspace_dir'])
        ForestCarbonEdgeTests._assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(args['workspace_dir'],
                         'aggregated_carbon_stocks_small_no_edge_effect.shp'),
            os.path.join(REGRESSION_DATA,
                         'agg_results_small_no_edge_effect.csv'))
    def test_carbon_full(self):
        """Forest Carbon Edge: regression testing all functionality."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'aoi_vector_path':
            os.path.join(REGRESSION_DATA, 'input', 'small_aoi.shp'),
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_path':
            os.path.join(REGRESSION_DATA, 'input',
                         'forest_edge_carbon_lu_table.csv'),
            'compute_forest_edge_effects':
            True,
            'lulc_raster_path':
            os.path.join(REGRESSION_DATA, 'input', 'small_lulc.tif'),
            'n_nearest_model_points':
            10,
            'pools_to_calculate':
            'all',
            'tropical_forest_edge_carbon_model_vector_path':
            os.path.join(REGRESSION_DATA, 'input', 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
            'n_workers':
            -1
        }
        forest_carbon_edge_effect.execute(args)
        ForestCarbonEdgeTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'file_list.txt'),
            args['workspace_dir'])

        self._assert_vector_results_close(
            args['workspace_dir'], 'id', ['c_sum', 'c_ha_mean'],
            os.path.join(args['workspace_dir'],
                         'aggregated_carbon_stocks.shp'),
            os.path.join(REGRESSION_DATA, 'agg_results_base.shp'))
    def test_carbon_full(self):
        """Forest Carbon Edge: regression testing all functionality."""
        from natcap.invest import forest_carbon_edge_effect

        args = {
            'aoi_uri':
            os.path.join(SAMPLE_DATA, 'forest_carbon_edge_demo_aoi.shp'),
            'biomass_to_carbon_conversion_factor':
            '0.47',
            'biophysical_table_uri':
            os.path.join(SAMPLE_DATA, 'forest_edge_carbon_lu_table.csv'),
            'compute_forest_edge_effects':
            True,
            'lulc_uri':
            os.path.join(SAMPLE_DATA, 'forest_carbon_edge_lulc_demo.tif'),
            'n_nearest_model_points':
            10,
            'pools_to_calculate':
            'all',
            'tropical_forest_edge_carbon_model_shape_uri':
            os.path.join(SAMPLE_DATA, 'core_data',
                         'forest_carbon_edge_regression_model_parameters.shp'),
            'workspace_dir':
            self.workspace_dir,
        }

        forest_carbon_edge_effect.execute(args)
        ForestCarbonEdgeTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'file_list.txt'),
            args['workspace_dir'])

        ForestCarbonEdgeTests._assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(args['workspace_dir'],
                         'aggregated_carbon_stocks.shp'),
            os.path.join(REGRESSION_DATA, 'agg_results_base.csv'))