Пример #1
0
    def test_globio_predefined_lulc(self):
        """GLOBIO: regression testing predefined LULC (mode b)."""
        from natcap.invest import globio

        args = {
            'aoi_path': '',
            'globio_lulc_path': os.path.join(
                SAMPLE_DATA, 'globio_lulc_small.tif'),
            'infrastructure_dir':  os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'msa_parameters_path': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'predefined_globio': True,
            'workspace_dir': self.workspace_dir,
            'n_workers': '-1',
        }
        globio.execute(args)
        GLOBIOTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'expected_file_list_lulc.txt'),
            args['workspace_dir'])

        model_array = pygeoprocessing.raster_to_numpy_array(
            os.path.join(args['workspace_dir'], 'msa.tif'))
        reg_array = pygeoprocessing.raster_to_numpy_array(
            os.path.join(REGRESSION_DATA, 'msa_lulc_regression.tif'))
        numpy.testing.assert_allclose(model_array, reg_array)
Пример #2
0
    def test_globio_shape_infra(self):
        """GLOBIO: regression testing with shapefile infrastructure."""
        from natcap.invest import globio

        args = {
            'aoi_path': '',
            'globio_lulc_path': os.path.join(SAMPLE_DATA,
                                             'globio_lulc_small.tif'),
            'infrastructure_dir': os.path.join(SAMPLE_DATA,
                                               'shape_infrastructure'),
            'intensification_fraction': '0.46',
            'msa_parameters_path': os.path.join(SAMPLE_DATA,
                                                'msa_parameters.csv'),
            'predefined_globio': True,
            'workspace_dir': self.workspace_dir,
            'n_workers': '-1',
        }
        globio.execute(args)
        GLOBIOTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'expected_file_list_lulc.txt'),
            args['workspace_dir'])

        pygeoprocessing.testing.assert_rasters_equal(
            os.path.join(args['workspace_dir'], 'msa.tif'),
            os.path.join(REGRESSION_DATA, 'msa_shape_infra_regression.tif'),
            1e-6)
Пример #3
0
    def test_globio_full(self):
        """GLOBIO: regression testing all functionality."""
        from natcap.invest import globio

        args = {
            'aoi_uri': os.path.join(SAMPLE_DATA, 'sub_aoi.shp'),
            'globio_lulc_uri': '',
            'infrastructure_dir': os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'lulc_to_globio_table_uri': os.path.join(
                SAMPLE_DATA, 'lulc_conversion_table.csv'),
            'lulc_uri': os.path.join(SAMPLE_DATA, 'lulc_2008.tif'),
            'msa_parameters_uri': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'pasture_threshold': '0.5',
            'pasture_uri': os.path.join(SAMPLE_DATA, 'pasture.tif'),
            'potential_vegetation_uri': os.path.join(
                SAMPLE_DATA, 'potential_vegetation.tif'),
            'predefined_globio': False,
            'primary_threshold': 0.66,
            'workspace_dir': self.workspace_dir,
        }

        globio.execute(args)
        GLOBIOTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'expected_file_list.txt'),
            args['workspace_dir'])

        GLOBIOTests._assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(
                args['workspace_dir'], 'aoi_summary.shp'),
            os.path.join(REGRESSION_DATA, 'globio_agg_results.csv'))
Пример #4
0
    def test_globio_full(self):
        """GLOBIO: regression testing all functionality (mode a)."""
        from natcap.invest import globio

        args = {
            'aoi_path':
            os.path.join(SAMPLE_DATA, 'sub_aoi.shp'),
            'globio_lulc_path':
            '',
            'infrastructure_dir':
            os.path.join(SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction':
            '0.46',
            'lulc_to_globio_table_path':
            os.path.join(SAMPLE_DATA, 'lulc_conversion_table.csv'),
            'lulc_path':
            os.path.join(SAMPLE_DATA, 'lulc_2008.tif'),
            'msa_parameters_path':
            os.path.join(SAMPLE_DATA, 'msa_parameters.csv'),
            'pasture_threshold':
            '0.5',
            'pasture_path':
            os.path.join(SAMPLE_DATA, 'pasture.tif'),
            'potential_vegetation_path':
            os.path.join(SAMPLE_DATA, 'potential_vegetation.tif'),
            'predefined_globio':
            False,
            'primary_threshold':
            0.66,
            'workspace_dir':
            self.workspace_dir,
            'n_workers':
            '-1',
        }

        # Test that overwriting output does not crash.
        _make_dummy_file(args['workspace_dir'], 'aoi_summary.shp')
        globio.execute(args)

        GLOBIOTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'expected_file_list.txt'),
            args['workspace_dir'])

        GLOBIOTests._assert_regression_results_eq(
            os.path.join(args['workspace_dir'], 'aoi_summary.shp'),
            os.path.join(REGRESSION_DATA, 'agg_results.csv'))

        # Infer an explicit 'pass'
        self.assertTrue(True)
Пример #5
0
    def test_globio_missing_globio_lulc_value(self):
        """GLOBIO: test error raised when missing GLOBIO LULC value from table.

        This test is when a GLOBIO LULC value is not represented in the
        MSA parameter table under the msa_lu rows.
        """
        from natcap.invest import globio
        import pandas

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'sub_aoi.shp'),
            'globio_lulc_path': os.path.join(
                SAMPLE_DATA, 'globio_lulc_small.tif'),
            'infrastructure_dir': os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'lulc_to_globio_table_path': os.path.join(
                SAMPLE_DATA, 'lulc_conversion_table.csv'),
            'msa_parameters_path': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'pasture_threshold': '0.5',
            'pasture_path': os.path.join(SAMPLE_DATA, 'pasture.tif'),
            'potential_vegetation_path': os.path.join(
                SAMPLE_DATA, 'potential_vegetation.tif'),
            'predefined_globio': True,
            'primary_threshold': 0.66,
            'workspace_dir': self.workspace_dir,
            'n_workers': '-1',
        }

        # remove a row from the msa table so that a msa_lu value is missing
        bad_msa_param_path = os.path.join(
            self.workspace_dir, 'bad_msa_param_table_path.csv')

        table_df = pandas.read_csv(args['msa_parameters_path'])
        # Using '3' here because Value column is of mix type and will be string
        table_df = table_df.loc[table_df['Value'] != '3']
        table_df.to_csv(bad_msa_param_path)
        table_df = None

        args['msa_parameters_path'] = bad_msa_param_path
        with self.assertRaises(ValueError) as context:
            globio.execute(args)
        self.assertTrue(
            "The missing values found in the GLOBIO LULC raster but not the"
            " table are: [3]" in str(context.exception))
Пример #6
0
    def test_globio_missing_lulc_value(self):
        """GLOBIO: test error is raised when missing LULC value from table.

        This test is when an LULC value is not represented in the Land Cover
        to GLOBIO Land Cover table.
        """
        from natcap.invest import globio
        import pandas

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'sub_aoi.shp'),
            'globio_lulc_path': '',
            'infrastructure_dir': os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'lulc_to_globio_table_path': os.path.join(
                SAMPLE_DATA, 'lulc_conversion_table.csv'),
            'lulc_path': os.path.join(SAMPLE_DATA, 'lulc_2008.tif'),
            'msa_parameters_path': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'pasture_threshold': '0.5',
            'pasture_path': os.path.join(SAMPLE_DATA, 'pasture.tif'),
            'potential_vegetation_path': os.path.join(
                SAMPLE_DATA, 'potential_vegetation.tif'),
            'predefined_globio': False,
            'primary_threshold': 0.66,
            'workspace_dir': self.workspace_dir,
            'n_workers': '-1',
        }

        # remove a row from the lulc table so that lulc value is missing
        bad_lulc_to_globio_path = os.path.join(
            self.workspace_dir, 'bad_lulc_to_globio_table_path.csv')

        table_df = pandas.read_csv(args['lulc_to_globio_table_path'])
        table_df = table_df.loc[table_df['lucode'] != 2]
        table_df.to_csv(bad_lulc_to_globio_path)
        table_df = None

        args['lulc_to_globio_table_path'] = bad_lulc_to_globio_path
        with self.assertRaises(ValueError) as context:
            globio.execute(args)
        # Note the '2.' here, since the lulc_2008.tif is a Float32
        self.assertTrue(
            "The missing values found in the LULC raster but not the table"
            " are: [2.]" in str(context.exception))
Пример #7
0
    def test_globio_empty_infra(self):
        """GLOBIO: testing that empty infra directory raises exception."""
        from natcap.invest import globio

        args = {
            'aoi_path': '',
            'globio_lulc_path': os.path.join(SAMPLE_DATA,
                                             'globio_lulc_small.tif'),
            'infrastructure_dir': os.path.join(SAMPLE_DATA, 'empty_dir'),
            'intensification_fraction': '0.46',
            'msa_parameters_path': os.path.join(SAMPLE_DATA,
                                                'msa_parameters.csv'),
            'predefined_globio': True,
            'workspace_dir': self.workspace_dir,
            'n_workers': '-1',
        }

        with self.assertRaises(ValueError):
            globio.execute(args)
Пример #8
0
    def test_globio_duplicate_output(self):
        """GLOBIO: testing that overwriting output does not crash."""
        from natcap.invest import globio

        args = {
            'aoi_uri': os.path.join(SAMPLE_DATA, 'sub_aoi.shp'),
            'globio_lulc_uri': os.path.join(
                REGRESSION_DATA, 'globio_lulc_small.tif'),
            'infrastructure_dir':  os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'msa_parameters_uri': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'predefined_globio': True,
            'workspace_dir': self.workspace_dir,
        }

        # invoke twice to ensure no error is raised
        globio.execute(args)
        globio.execute(args)

        # inferring an explicit 'pass'
        self.assertTrue(True)
Пример #9
0
    def test_globio_predefined_lulc(self):
        """GLOBIO: regression testing predefined LULC."""
        from natcap.invest import globio

        args = {
            'aoi_uri': '',
            'globio_lulc_uri': os.path.join(
                REGRESSION_DATA, 'globio_lulc.tif'),
            'infrastructure_dir':  os.path.join(
                SAMPLE_DATA, 'infrastructure_dir'),
            'intensification_fraction': '0.46',
            'msa_parameters_uri': os.path.join(
                SAMPLE_DATA, 'msa_parameters.csv'),
            'predefined_globio': True,
            'workspace_dir': self.workspace_dir,
        }
        globio.execute(args)
        GLOBIOTests._test_same_files(
            os.path.join(REGRESSION_DATA, 'expected_file_list_lulc.txt'),
            args['workspace_dir'])

        natcap.invest.pygeoprocessing_0_3_3.testing.assert_rasters_equal(
            os.path.join(self.workspace_dir, 'msa.tif'),
            os.path.join(REGRESSION_DATA, 'msa_lulc_regression.tif'), 1e-6)