示例#1
0
    def test_predictor_id_too_long(self):
        """Recreation test ID too long raises ValueError."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path':
            os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'compute_regression':
            True,
            'start_year':
            '2005',
            'end_year':
            '2014',
            'grid_aoi':
            True,
            'grid_type':
            'square',
            'cell_size':
            20000,
            'predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors_id_too_long.csv'),
            'results_suffix':
            u'',
            'workspace_dir':
            self.workspace_dir,
        }

        with self.assertRaises(ValueError):
            recmodel_client.execute(args)
示例#2
0
    def test_end_year_out_of_range(self):
        """Recreation that end_year out of range raise ValueError."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path':
            os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'cell_size':
            7000.0,
            'compute_regression':
            True,
            'start_year':
            '2005',
            'end_year':
            '2219',  # end year ridiculously out of range
            'grid_aoi':
            True,
            'grid_type':
            'hexagon',
            'predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors.csv'),
            'results_suffix':
            u'',
            'scenario_predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors_scenario.csv'),
            'workspace_dir':
            self.workspace_dir,
        }

        with self.assertRaises(ValueError):
            recmodel_client.execute(args)
示例#3
0
    def test_no_grid_regression(self):
        """Recreation base regression on ungridded AOI."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'compute_regression': False,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': False,
            'results_suffix': u'',
            'workspace_dir': self.workspace_dir,
        }

        recmodel_client.execute(args)

        output_lines = open(os.path.join(
            self.workspace_dir, 'monthly_table.csv'), 'rb').readlines()
        expected_lines = open(os.path.join(
            REGRESSION_DATA, 'expected_monthly_table_for_no_grid.csv'),
                              'rb').readlines()

        if output_lines != expected_lines:
            raise ValueError(
                "Output table not the same as input. "
                "Expected:\n%s\nGot:\n%s" % (expected_lines, output_lines))
示例#4
0
    def test_all_metrics(self):
        """Recreation test with all but trivial predictor metrics."""
        from natcap.invest.recreation import recmodel_client
        args = {
            'aoi_path': os.path.join(
                REGRESSION_DATA, 'andros_aoi_with_extra_fields.shp'),
            'compute_regression': True,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': False,
            'predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors_all.csv'),
            'scenario_predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors_all.csv'),
            'results_suffix': u'',
            'workspace_dir': self.workspace_dir,
        }
        recmodel_client.execute(args)

        out_grid_vector_path = os.path.join(
            self.workspace_dir, 'regression_coefficients.shp')
        expected_grid_vector_path = os.path.join(
            REGRESSION_DATA, 'trivial_regression_coefficients.shp')
        natcap.invest.pygeoprocessing_0_3_3.testing.assert_vectors_equal(
            out_grid_vector_path, expected_grid_vector_path)

        out_scenario_path = os.path.join(
            self.workspace_dir, 'scenario_results.shp')
        expected_scenario_path = os.path.join(
            REGRESSION_DATA, 'trivial_scenario_results.shp')
        natcap.invest.pygeoprocessing_0_3_3.testing.assert_vectors_equal(
            out_scenario_path, expected_scenario_path)
示例#5
0
    def test_base_regression(self):
        """Recreation base regression test on fast sample data.

        Executes Recreation model with default data and default arguments.
        """
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'cell_size': 40000.0,
            'compute_regression': True,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': True,
            'grid_type': 'hexagon',
            'predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors.csv'),
            'results_suffix': u'',
            'scenario_predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors_scenario.csv'),
            'workspace_dir': self.workspace_dir,
        }

        recmodel_client.execute(args)
        RecreationRegressionTests._assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(REGRESSION_DATA, 'file_list_base.txt'),
            os.path.join(args['workspace_dir'], 'scenario_results.shp'),
            os.path.join(REGRESSION_DATA, 'scenario_results_40000.csv'))
示例#6
0
    def test_regression_local_server(self):
        """Recreation base regression test on sample data on local server.

        Executes Recreation model with default data and default arguments.
        """
        from natcap.invest.recreation import recmodel_client
        from natcap.invest.recreation import recmodel_server

        natcap.invest.pygeoprocessing_0_3_3.create_directories([self.workspace_dir])
        point_data_path = os.path.join(REGRESSION_DATA, 'sample_data.csv')

        # attempt to get an open port; could result in race condition but
        # will be okay for a test. if this test ever fails because of port
        # in use, that's probably why
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()
        sock = None

        server_args = {
            'hostname': 'localhost',
            'port': port,
            'raw_csv_point_data_path': point_data_path,
            'cache_workspace': self.workspace_dir,
            'min_year': 2004,
            'max_year': 2015,
            'max_points_per_node': 50,
        }

        server_thread = threading.Thread(
            target=recmodel_server.execute, args=(server_args,))
        server_thread.daemon = True
        server_thread.start()

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'cell_size': 40000.0,
            'compute_regression': True,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': True,
            'grid_type': 'hexagon',
            'predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors.csv'),
            'results_suffix': u'',
            'scenario_predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors_scenario.csv'),
            'workspace_dir': self.workspace_dir,
        }

        recmodel_client.execute(args)

        RecreationRegressionTests._assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(REGRESSION_DATA, 'file_list_base.txt'),
            os.path.join(args['workspace_dir'], 'scenario_results.shp'),
            os.path.join(REGRESSION_DATA, 'scenario_results_40000.csv'))
示例#7
0
    def test_empty_server(self):
        """Recreation test a client call to simple server."""
        from natcap.invest.recreation import recmodel_server
        from natcap.invest.recreation import recmodel_client

        natcap.invest.pygeoprocessing_0_3_3.create_directories([self.workspace_dir])
        empty_point_data_path = os.path.join(
            self.workspace_dir, 'empty_table.csv')
        open(empty_point_data_path, 'w').close()  # touch the file

        # attempt to get an open port; could result in race condition but
        # will be okay for a test. if this test ever fails because of port
        # in use, that's probably why
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()
        sock = None

        server_args = {
            'hostname': 'localhost',
            'port': port,
            'raw_csv_point_data_path': empty_point_data_path,
            'cache_workspace': self.workspace_dir,
            'min_year': 2004,
            'max_year': 2015,
        }

        server_thread = threading.Thread(
            target=recmodel_server.execute, args=(server_args,))
        server_thread.daemon = True
        server_thread.start()

        client_args = {
            'aoi_path': os.path.join(
                REGRESSION_DATA, 'test_aoi_for_subset.shp'),
            'cell_size': 7000.0,
            'hostname': 'localhost',
            'port': port,
            'compute_regression': False,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': False,
            'results_suffix': u'',
            'workspace_dir': self.workspace_dir,
        }
        recmodel_client.execute(client_args)

        # testing for file existence seems reasonable since mostly we are
        # testing that a local server starts and a client connects to it
        _test_same_files(
            os.path.join(REGRESSION_DATA, 'file_list_empty_local_server.txt'),
            self.workspace_dir)
示例#8
0
    def test_bad_grid_type(self):
        """Recreation ensure that bad grid type raises ValueError."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'cell_size': 7000.0,
            'compute_regression': False,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': True,
            'grid_type': 'circle',  # intentionally bad gridtype
            'results_suffix': u'',
            'workspace_dir': self.workspace_dir,
        }

        with self.assertRaises(ValueError):
            recmodel_client.execute(args)
示例#9
0
    def test_year_order(self):
        """Recreation ensure that end year < start year raise ValueError."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'cell_size': 7000.0,
            'compute_regression': True,
            'start_year': '2014',  # note start_year > end_year
            'end_year': '2005',
            'grid_aoi': True,
            'grid_type': 'hexagon',
            'predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors.csv'),
            'results_suffix': u'',
            'scenario_predictor_table_path': os.path.join(
                REGRESSION_DATA, 'predictors_scenario.csv'),
            'workspace_dir': self.workspace_dir,
        }

        with self.assertRaises(ValueError):
            recmodel_client.execute(args)
示例#10
0
    def test_no_grid_regression(self):
        """Recreation base regression on ungridded AOI."""
        from natcap.invest.recreation import recmodel_client

        args = {
            'aoi_path': os.path.join(SAMPLE_DATA, 'andros_aoi.shp'),
            'compute_regression': False,
            'start_year': '2005',
            'end_year': '2014',
            'grid_aoi': False,
            'results_suffix': u'',
            'workspace_dir': self.workspace_dir,
        }

        recmodel_client.execute(args)

        expected_result_table = pandas.read_csv(
            os.path.join(REGRESSION_DATA,
                         'expected_monthly_table_for_no_grid.csv'))
        result_table = pandas.read_csv(
            os.path.join(self.workspace_dir, 'monthly_table.csv'))
        pandas.testing.assert_frame_equal(expected_result_table,
                                          result_table,
                                          check_dtype=False)
示例#11
0
    def test_all_metrics_local_server(self):
        """Recreation test with all but trivial predictor metrics.

        Executes Recreation model all the way through scenario prediction.
        With this 'extra_fields_features' AOI, we also cover two edge cases:
        1) the AOI has a pre-existing field that the model wishes to create.
        2) the AOI has features only covering nodata raster predictor values."""
        from natcap.invest.recreation import recmodel_client
        from natcap.invest.recreation import recmodel_server

        # attempt to get an open port; could result in race condition but
        # will be okay for a test. if this test ever fails because of port
        # in use, that's probably why
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()
        sock = None

        server_args = {
            'hostname': 'localhost',
            'port': port,
            'raw_csv_point_data_path': self.resampled_data_path,
            'cache_workspace': self.workspace_dir,
            'min_year': 2008,
            'max_year': 2015,
            'max_points_per_node': 200,
        }

        server_thread = threading.Thread(target=recmodel_server.execute,
                                         args=(server_args, ))
        server_thread.daemon = True
        server_thread.start()

        args = {
            'aoi_path':
            os.path.join(SAMPLE_DATA,
                         'andros_aoi_with_extra_fields_features.shp'),
            'compute_regression':
            True,
            'start_year':
            '2008',
            'end_year':
            '2014',
            'grid_aoi':
            False,
            'predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors_all.csv'),
            'scenario_predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors_all.csv'),
            'results_suffix':
            u'',
            'workspace_dir':
            self.workspace_dir,
            'hostname':
            server_args['hostname'],
            'port':
            server_args['port'],
        }
        recmodel_client.execute(args)

        out_grid_vector_path = os.path.join(args['workspace_dir'],
                                            'predictor_data.shp')
        expected_grid_vector_path = os.path.join(
            REGRESSION_DATA, 'predictor_data_all_metrics.shp')
        pygeoprocessing.testing.assert_vectors_equal(
            out_grid_vector_path, expected_grid_vector_path, 1E-6)

        out_scenario_path = os.path.join(args['workspace_dir'],
                                         'scenario_results.shp')
        expected_scenario_path = os.path.join(
            REGRESSION_DATA, 'scenario_results_all_metrics.shp')
        pygeoprocessing.testing.assert_vectors_equal(out_scenario_path,
                                                     expected_scenario_path,
                                                     1E-6)
示例#12
0
    def test_regression_local_server(self):
        """Recreation base regression test on sample data on local server.

        Executes Recreation model all the way through scenario prediction.
        With this florida AOI, raster and vector predictors do not
        intersect the AOI. This makes for a fast test and incidentally
        covers an edge case.
        """
        from natcap.invest.recreation import recmodel_client
        from natcap.invest.recreation import recmodel_server

        # attempt to get an open port; could result in race condition but
        # will be okay for a test. if this test ever fails because of port
        # in use, that's probably why
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()
        sock = None

        server_args = {
            'hostname': 'localhost',
            'port': port,
            'raw_csv_point_data_path': self.resampled_data_path,
            'cache_workspace': self.workspace_dir,
            'min_year': 2004,
            'max_year': 2015,
            'max_points_per_node': 200,
        }

        server_thread = threading.Thread(target=recmodel_server.execute,
                                         args=(server_args, ))
        server_thread.daemon = True
        server_thread.start()

        args = {
            'aoi_path':
            os.path.join(SAMPLE_DATA,
                         'local_recreation_aoi_florida_utm18n.shp'),
            'cell_size':
            40000.0,
            'compute_regression':
            True,
            'start_year':
            '2005',
            'end_year':
            '2014',
            'hostname':
            'localhost',
            'port':
            port,
            'grid_aoi':
            True,
            'grid_type':
            'hexagon',
            'predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors.csv'),
            'results_suffix':
            u'',
            'scenario_predictor_table_path':
            os.path.join(SAMPLE_DATA, 'predictors_scenario.csv'),
            'workspace_dir':
            self.workspace_dir,
        }

        recmodel_client.execute(args)

        _assert_regression_results_eq(
            args['workspace_dir'],
            os.path.join(REGRESSION_DATA, 'file_list_base_florida_aoi.txt'),
            os.path.join(args['workspace_dir'], 'scenario_results.shp'),
            os.path.join(REGRESSION_DATA, 'local_server_scenario_results.csv'))