Example #1
0
    def test_base_regression(self):
        """NDR base regression test on sample data.

        Execute NDR with sample data and checks that the output files are
        generated and that the aggregate shapefile fields are the same as the
        regression case.
        """
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)

        # copy a junk AOI on top of where the output shapefile should reside
        # to ensure the model overwrites it
        os.makedirs(os.path.join(self.workspace_dir, 'output'))
        shutil.copy(
            args['watersheds_path'],
            os.path.join(self.workspace_dir, 'watershed_results_ndr.shp'))

        # make args explicit that this is a base run of SWY
        ndr.execute(args)

        NDRTests._assert_regression_results_equal(
            args['workspace_dir'],
            os.path.join(REGRESSION_DATA, 'file_list_base.txt'),
            os.path.join(args['workspace_dir'], 'watershed_results_ndr.shp'),
            os.path.join(REGRESSION_DATA, 'agg_results_base.csv'))
Example #2
0
    def test_crit_len_0(self):
        """NDR test case where crit len is 0 in biophysical table."""
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        new_table_path = os.path.join(self.workspace_dir, 'table_c_len_0.csv')
        with open(new_table_path, 'w') as target_file:
            with open(args['biophysical_table_path'], 'r') as table_file:
                target_file.write(table_file.readline())
                while True:
                    line = table_file.readline()
                    if not line:
                        break
                    line_list = line.split(',')
                    # replace the crit_len_p with 0 in this column
                    line = (
                        ','.join(line_list[0:12] + ['0.0'] + line_list[13::]))
                    target_file.write(line)

        args['biophysical_table_path'] = new_table_path
        ndr.execute(args)

        result_vector = ogr.Open(
            os.path.join(args['workspace_dir'], 'watershed_results_ndr.shp'))
        result_layer = result_vector.GetLayer()
        error_results = {}

        surf_p_ld = 41.921
        sub_p_ld = 0
        p_exp_tot = 7.666
        surf_n_ld = 2978.520
        sub_n_ld = 28.614
        n_exp_tot = 339.839
        feature = result_layer.GetFeature(0)
        if not feature:
            raise AssertionError("No features were output.")
        for field, value in [
                ('surf_p_ld', surf_p_ld),
                ('sub_p_ld', sub_p_ld),
                ('p_exp_tot', p_exp_tot),
                ('surf_n_ld', surf_n_ld),
                ('sub_n_ld', sub_n_ld),
                ('n_exp_tot', n_exp_tot)]:
            if not numpy.isclose(feature.GetField(field), value, atol=1e-2):
                error_results[field] = (
                    'field', feature.GetField(field), value)
        ogr.Feature.__swig_destroy__(feature)
        feature = None
        result_layer = None
        ogr.DataSource.__swig_destroy__(result_vector)
        result_vector = None

        if error_results:
            raise AssertionError(
                "The following values are not equal: %s" % error_results)
Example #3
0
    def test_no_nutrient_selected(self):
        """NDR no nutrient selected should raise a ValueError."""
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        # make args explicit that this is a base run of SWY
        args['calc_n'] = False
        args['calc_p'] = False
        with self.assertRaises(ValueError):
            ndr.execute(args)
Example #4
0
    def test_missing_headers(self):
        """NDR biphysical headers missing should raise a ValueError."""
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        # make args explicit that this is a base run of SWY
        args['biophysical_table_path'] = os.path.join(
            REGRESSION_DATA, 'input', 'biophysical_table_missing_headers.csv')
        with self.assertRaises(ValueError):
            ndr.execute(args)
Example #5
0
    def test_base_regression(self):
        """NDR base regression test on sample data.

        Execute NDR with sample data and checks that the output files are
        generated and that the aggregate shapefile fields are the same as the
        regression case.
        """
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        # make an empty output shapefile on top of where the new output
        # shapefile should reside to ensure the model overwrites it
        with open(
                os.path.join(self.workspace_dir, 'watershed_results_ndr.gpkg'),
                'wb') as f:
            f.write(b'')

        # make args explicit that this is a base run of SWY
        ndr.execute(args)

        result_vector = ogr.Open(
            os.path.join(args['workspace_dir'], 'watershed_results_ndr.gpkg'))
        result_layer = result_vector.GetLayer()
        result_feature = result_layer.GetFeature(1)
        result_layer = None
        result_vector = None
        mismatch_list = []
        # these values were generated by manual inspection of regression
        # results
        for field, expected_value in [('p_surface_load', 41.921860),
                                      ('p_surface_export', 5.899117),
                                      ('n_surface_load', 2978.519775),
                                      ('n_surface_export', 289.0498),
                                      ('n_subsurface_load', 28.614094),
                                      ('n_subsurface_export', 15.61077),
                                      ('n_total_export', 304.660614)]:
            val = result_feature.GetField(field)
            if not numpy.isclose(val, expected_value):
                mismatch_list.append((field, 'expected: %f' % expected_value,
                                      'actual: %f' % val))
        result_feature = None
        if mismatch_list:
            raise RuntimeError("results not expected: %s" % mismatch_list)

        # We only need to test that the drainage mask exists.  Functionality
        # for that raster is tested in SDR.
        self.assertTrue(
            os.path.exists(
                os.path.join(args['workspace_dir'], 'intermediate_outputs',
                             'what_drains_to_stream.tif')))
Example #6
0
    def test_missing_lucode(self):
        """NDR missing lucode in biophysical table should raise a KeyError."""
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        # make args explicit that this is a base run of SWY
        args['biophysical_table_path'] = os.path.join(
            REGRESSION_DATA, 'input', 'biophysical_table_missing_lucode.csv')
        with self.assertRaises(KeyError) as cm:
            ndr.execute(args)
        actual_message = str(cm.exception)
        self.assertTrue(
            'present in the landuse raster but missing from the biophysical' in
            actual_message)
Example #7
0
    def test_base_regression(self):
        """NDR base regression test on sample data.

        Execute NDR with sample data and checks that the output files are
        generated and that the aggregate shapefile fields are the same as the
        regression case.
        """
        from natcap.invest.ndr import ndr

        # use predefined directory so test can clean up files during teardown
        args = NDRTests.generate_base_args(self.workspace_dir)
        # make an empty output shapefile on top of where the new output
        # shapefile should reside to ensure the model overwrites it
        with open(
                os.path.join(self.workspace_dir, 'watershed_results_ndr.shp'),
                'wb') as f:
            f.write(b'')

        # make args explicit that this is a base run of SWY
        ndr.execute(args)

        result_vector = ogr.Open(os.path.join(
            args['workspace_dir'], 'watershed_results_ndr.shp'))
        result_layer = result_vector.GetLayer()
        result_feature = result_layer.GetFeature(0)
        result_layer = None
        result_vector = None
        mismatch_list = []
        # these values were generated by manual inspection of regression
        # results
        for field, expected_value in [
                ('surf_p_ld', 41.921860),
                ('p_exp_tot', 8.598053),
                ('surf_n_ld', 2978.519775),
                ('sub_n_ld', 28.614094),
                ('n_exp_tot', 339.839386)]:
            val = result_feature.GetField(field)
            if not numpy.isclose(val, expected_value):
                mismatch_list.append(
                    (field, 'expected: %f' % expected_value,
                     'actual: %f' % val))
        result_feature = None
        if mismatch_list:
            raise RuntimeError("results not expected: %s" % mismatch_list)
args['calc_sequestration'] = False
args['carbon_pools_path'] = BioTable
args['do_redd'] = False
args['do_valuation'] = False
args['lulc_cur_path'] = LULC

# Seasonal Water Yield
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '02-Seasonal-Water-Yield')
swy.execute(args)

# Anual Water Yield
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '01-Anual-Water-Yield')
awy.execute(args)

# Sediment Delivery Ratio
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '03-Sediment-Delivery-Ratio')
sdr.execute(args)

# Nutrient Delivery Ratio
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '04-Nutrient-Delivery-Ratio')
ndr.execute(args)

# Carbons
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '05-Carbons')
carbon.execute(args)