Exemple #1
0
 def test_raster_values_not_in_lookup_table(self):
     """Coastal Blue Carbon: Test raster values not in lookup table."""
     from natcap.invest.coastal_blue_carbon import preprocessor
     args = _get_preprocessor_args(1, self.workspace_dir)
     _create_table(args['lulc_lookup_uri'], lulc_lookup_list_no_ones)
     with self.assertRaises(ValueError):
         preprocessor.execute(args)
Exemple #2
0
    def test_mark_transition_type(self):
        """Coastal Blue Carbon: Test mark_transition_type."""
        from natcap.invest.coastal_blue_carbon import preprocessor
        args = _get_preprocessor_args(1, self.workspace_dir)

        band_matrices_zero = [numpy.zeros((2, 2))]
        srs = pygeotest.sampledata.SRS_WILLAMETTE
        raster_zeros = pygeotest.create_raster_on_disk(
            band_matrices_zero,
            srs.origin,
            srs.projection,
            NODATA_INT,
            srs.pixel_size(100),
            datatype=gdal.GDT_Int32,
            filename=os.path.join(
                self.workspace_dir, 'raster_1.tif'))
        args['lulc_snapshot_list'][0] = raster_zeros

        preprocessor.execute(args)
        trans_csv = os.path.join(
            self.workspace_dir,
            'workspace',
            'outputs_preprocessor',
            'transitions_test.csv')
        with open(trans_csv, 'r') as f:
            lines = f.readlines()
        self.assertTrue(lines[1][:].startswith('n,NCC,accum'))
Exemple #3
0
 def test_lookup_parsing_exception(self):
     """Coastal Blue Carbon: Test lookup table parsing exception."""
     from natcap.invest.coastal_blue_carbon import preprocessor
     args = _get_preprocessor_args(1, self.workspace_dir)
     _create_table(args['lulc_lookup_uri'], lulc_lookup_list_unreadable)
     with self.assertRaises(ValueError):
         preprocessor.execute(args)
    def test_preprocessor_nodata(self):
        """Coastal Blue Carbon: Test run of preprocessor (various values).

        First raster contains ones, second nodata, third zeros.
        """
        from natcap.invest.coastal_blue_carbon import preprocessor
        args = _get_preprocessor_args(3, self.workspace_dir)
        preprocessor.execute(args)
        trans_csv = os.path.join(args['workspace_dir'], 'outputs_preprocessor',
                                 'transitions_test.csv')
        with open(trans_csv, 'r') as f:
            lines = f.readlines()
        # just a regression test.  this tests that an output file was
        # successfully created, and that two particular land class transitions
        # occur and are set in the right directions.
        self.assertTrue(lines[2][:].startswith('x,,'))
    def test_preprocessor_ones(self):
        """Coastal Blue Carbon: Test entire run of preprocessor (ones).

        All rasters contain ones.
        """
        from natcap.invest.coastal_blue_carbon import preprocessor
        args = _get_preprocessor_args(1, self.workspace_dir)
        preprocessor.execute(args)
        trans_csv = os.path.join(args['workspace_dir'], 'outputs_preprocessor',
                                 'transitions_test.csv')
        with open(trans_csv, 'r') as f:
            lines = f.readlines()
        # just a regression test.  this tests that an output file was
        # successfully created, and demonstrates that one land class transition
        # does not occur and the other is set in the right direction.
        self.assertTrue(lines[2].startswith('x,,accum'))
Exemple #6
0
    def test_sample_data(self):
        """CBC Preprocessor: Test on sample data."""
        from natcap.invest.coastal_blue_carbon import preprocessor

        snapshot_csv_path = os.path.join(REGRESSION_DATA, 'inputs',
                                         'snapshots.csv')

        args = {
            'workspace_dir':
            os.path.join(self.workspace_dir, 'workspace'),
            'results_suffix':
            '150225',
            'lulc_lookup_table_path':
            os.path.join(REGRESSION_DATA, 'inputs', 'lulc_lookup.csv'),
            'landcover_snapshot_csv':
            snapshot_csv_path,
        }
        preprocessor.execute(args)

        # walk through all files in the workspace and assert that outputs have
        # the file suffix.
        non_suffixed_files = []
        outputs_dir = os.path.join(args['workspace_dir'],
                                   'outputs_preprocessor')
        for root_dir, dirnames, filenames in os.walk(outputs_dir):
            for filename in filenames:
                if not filename.lower().endswith('.txt'):  # ignore logfile
                    basename, extension = os.path.splitext(filename)
                    if not basename.endswith('_150225'):
                        path_rel_to_workspace = os.path.relpath(
                            os.path.join(root_dir, filename),
                            args['workspace_dir'])
                        non_suffixed_files.append(path_rel_to_workspace)

        if non_suffixed_files:
            self.fail(
                '%s files are missing suffixes: %s' %
                (len(non_suffixed_files), pprint.pformat(non_suffixed_files)))

        expected_landcover_codes = set(range(0, 24))
        found_landcover_codes = set(
            utils.build_lookup_from_csv(
                os.path.join(outputs_dir,
                             'carbon_biophysical_table_template_150225.csv'),
                'code').keys())
        self.assertEqual(expected_landcover_codes, found_landcover_codes)
    def test_preprocessor_zeros(self):
        """Coastal Blue Carbon: Test entire run of preprocessor (zeroes).

        First two rasters contain ones, last contains zeros.
        """
        from natcap.invest.coastal_blue_carbon import preprocessor
        args2 = _get_preprocessor_args(2, self.workspace_dir)
        preprocessor.execute(args2)
        trans_csv = os.path.join(args2['workspace_dir'],
                                 'outputs_preprocessor',
                                 'transitions_test.csv')
        with open(trans_csv, 'r') as f:
            lines = f.readlines()

        # just a regression test.  this tests that an output file was
        # successfully created, and that two particular land class transitions
        # occur and are set in the right directions.
        self.assertTrue(lines[2][:].startswith('x,disturb,accum'))
Exemple #8
0
    def test_mark_transition_type_nodata_check(self):
        """Coastal Blue Carbon: Test mark_transition_type with nodata check."""
        from natcap.invest.coastal_blue_carbon import preprocessor
        args = _get_preprocessor_args(1, self.workspace_dir)

        band_matrices_zero = [numpy.zeros((2, 2))]
        srs = pygeotest.sampledata.SRS_WILLAMETTE
        raster_zeros = pygeotest.create_raster_on_disk(
            band_matrices_zero,
            srs.origin,
            srs.projection,
            NODATA_INT,
            srs.pixel_size(100),
            datatype=gdal.GDT_Int32,
            filename=os.path.join(
                self.workspace_dir, 'raster_1.tif'))
        args['lulc_snapshot_list'][0] = raster_zeros

        preprocessor.execute(args)
Exemple #9
0
 def test_raster_validation(self):
     """Coastal Blue Carbon: Test raster validation."""
     from natcap.invest.coastal_blue_carbon import preprocessor
     args = _get_preprocessor_args(1, self.workspace_dir)
     OTHER_NODATA = -1
     srs = pygeotest.sampledata.SRS_WILLAMETTE
     band_matrices_with_nodata = [numpy.ones((2, 2)) * OTHER_NODATA]
     raster_wrong_nodata = pygeotest.create_raster_on_disk(
         band_matrices_with_nodata,
         srs.origin,
         srs.projection,
         OTHER_NODATA,
         srs.pixel_size(100),
         datatype=gdal.GDT_Int32,
         filename=os.path.join(
             self.workspace_dir, 'raster_wrong_nodata.tif'))
     args['lulc_snapshot_list'][0] = raster_wrong_nodata
     with self.assertRaises(ValueError):
         preprocessor.execute(args)
    def test_binary(self):
        """Coastal Blue Carbon: Test preprocessor run against InVEST-Data."""
        from natcap.invest.coastal_blue_carbon import preprocessor

        raster_0_uri = os.path.join(REGRESSION_DATA,
                                    'inputs/GBJC_2010_mean_Resample.tif')
        raster_1_uri = os.path.join(REGRESSION_DATA,
                                    'inputs/GBJC_2030_mean_Resample.tif')
        raster_2_uri = os.path.join(REGRESSION_DATA,
                                    'inputs/GBJC_2050_mean_Resample.tif')
        args = {
            'workspace_dir':
            _create_workspace(),
            'results_suffix':
            '150225',
            'lulc_lookup_uri':
            os.path.join(REGRESSION_DATA, 'inputs', 'lulc_lookup.csv'),
            'lulc_snapshot_list': [raster_0_uri, raster_1_uri, raster_2_uri]
        }
        preprocessor.execute(args)

        # walk through all files in the workspace and assert that outputs have
        # the file suffix.
        non_suffixed_files = []
        for root_dir, dirnames, filenames in os.walk(args['workspace_dir']):
            for filename in filenames:
                if not filename.lower().endswith('.txt'):  # ignore logfile
                    basename, extension = os.path.splitext(filename)
                    if not basename.endswith('_150225'):
                        path_rel_to_workspace = os.path.relpath(
                            os.path.join(root_dir, filename),
                            self.args['workspace_dir'])
                        non_suffixed_files.append(path_rel_to_workspace)

        if non_suffixed_files:
            self.fail(
                '%s files are missing suffixes: %s' %
                (len(non_suffixed_files), pprint.pformat(non_suffixed_files)))