natcap.invest.pygeoprocessing_0_3_3.testing package

Submodules

natcap.invest.pygeoprocessing_0_3_3.testing.assertions module

Assertions for geospatial testing.

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_checksums_equal(checksum_file, base_folder=None)

Assert all files in the checksum_file have the same checksum.

Checksum files could be created by pygeoprocessing.testing.utils.checksum_folder(), but this function may also assert the output of GNU md5sum or BSD md5. Either format (GNU or BSD) may be provided as input to this assertion function. Any files not included in checksum_file are ignored.

Parameters:
  • checksum_file (string) – the path to the snapshot file to use.
  • base_folder=None (string or None) – the folder to refer to as the base path for files in the checksum_file. If None, the current working directory will be used.
Raises:

AssertionError – when a nonmatching md5sum is found.

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_close(value_a, value_b, rel_tol=1e-09, abs_tol=0.0, msg=None)

Assert equality to an absolute tolerance.

Parameters:
  • value_a (int or float) – The first value to test.
  • value_b (int or float) – The second value to test.
  • rel_tol (int or float) – The relative numerical tolerance. If the relative difference of these values are less than this value, assertion will pass.
  • abs_tol (int or float) – The absolute numerical tolerance. If the difference of the values being tested are less than this value, the assertion will pass.
  • msg=None (string or None) – The assertion message to use if value_a and value_b are not found to be equal out to the target tolerance.
Returns:

None.

Raises:
  • AssertionError – Raised when the values are not equal out to the
  • desired tolerance.
natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_csv_equal(a_uri, b_uri, rel_tol=1e-09, abs_tol=0.0)

Assert the equality of CSV files at a_uri and b_uri.

Tests if csv files a and b are ‘almost equal’ to each other on a per cell basis. Numeric cells are asserted to be equal within the given tolerance. Other cell types are asserted to be equal.

Parameters:
  • a_uri (string) – a URI to a csv file
  • b_uri (string) – a URI to a csv file
  • rel_tol (int or float) – The relative numerical tolerance allowed, if relative difference of values are less than this, assertion passes.
  • abs_tol (int or float) – The absolute numerical tolerance allowed, if difference of values are less than this, assertion passes.
Raises:
  • AssertionError – Raised when the two CSV files are found to be
  • different.
Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_json_equal(json_1_uri, json_2_uri)

Assert two JSON files against each other.

The two JSON files provided will be opened, read, and their contents will be asserted to be equal. If the two are found to be different, the diff of the two files will be printed.

Parameters:
  • json_1_uri (string) – a uri to a JSON file.
  • json_2_uri (string) – a uri to a JSON file.
Raises:

AssertionError – Raised when the two JSON objects differ.

Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_md5_equal(uri, regression_hash)

Assert the MD5sum of a file against a regression MD5sum.

This method is a convenience method that uses natcap.invest.testing.digest_file() to determine the MD5sum of the file located at uri. It is functionally equivalent to calling:

if digest_file(uri) != '<some md5sum>':
    raise AssertionError

Regression MD5sums can be calculated for you by using natcap.invest.testing.digest_file() or a system-level md5sum program.

Parameters:
  • uri (string) – a string URI to the file to be tested.
  • regression_hash (string) – a string md5sum to be tested against.
Raises:
  • AssertionError – Raised when the MD5sum of the file at uri
  • differs from the provided regression md5sum hash.
Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_rasters_equal(a_uri, b_uri, rel_tol=1e-09, abs_tol=0.0)

Assert te equality of rasters a and b out to the given tolerance.

This assertion method asserts the equality of these raster characteristics:

  • Raster height and width
  • The number of layers in the raster
  • Each pixel value, such that the absolute difference between the
    pixels is less than tolerance.
  • Projection
Parameters:
  • a_uri (string) – a URI to a GDAL dataset
  • b_uri (string) – a URI to a GDAL dataset
  • rel_tol (int or float) – the relative tolerance to which values should be asserted. This is a numerical tolerance, not the number of places to which a value should be rounded. If the relative value is below this value then the assert passes.
  • abs_tol (int or float) – absolute tolerance to which values should be asserted. If absolute difference in values is below this value then the assert passes.
Returns:

None

Raises:
  • IOError – Raised when one of the input files is not found on disk.
  • AssertionError – Raised when the two rasters are found to be not
  • equal to each other.
natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_text_equal(text_1_uri, text_2_uri)

Assert that two text files are equal.

This comparison is done line-by-line.

Parameters:
  • text_1_uri (string) – a python string uri to a text file. Considered the file to be tested.
  • text_2_uri (string) – a python string uri to a text file. Considered the regression file.
Raises:

AssertionError – Raised when a line differs in the two files.

Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assertions.assert_vectors_equal(a_uri, b_uri, field_tolerance)

Assert that the vectors at a_uri and b_uri are equal to each other.

This assertion method asserts the equality of these vector characteristics:

  • Number of layers in the vector
  • Number of features in each layer
  • Geometry type of the layer
  • Feature geometry type
  • Number of fields in each feature
  • Name of each field
  • Field values for each feature
  • Projection
Parameters:
  • a_uri (string) – a URI to an OGR vector
  • b_uri (string) – a URI to an OGR vector
  • field_tolerance (int or float) – The relative numerical tolerance to which field values should be asserted.
Raises:
  • IOError – Raised if one of the input files is not found on disk.
  • AssertionError – Raised if the vectors are not found to be equal to
  • one another.
Returns
None
natcap.invest.pygeoprocessing_0_3_3.testing.assertions.isclose(val_a, val_b, rel_tol=1e-09, abs_tol=0.0)

Assert that values are equal to the given tolerance.

Adapted from the python 3.5 standard library based on the specification found in PEP485.

Parameters:
  • val_a (int or float) – The first value to test
  • val_b (int or float) – The second value to test
  • rel_tol (int or float) – is the relative tolerance - it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.
  • abs_tol (float) – is the minimum absolute tolerance - useful for comparisons near zero. abs_tol must be at least zero.
Returns:

A boolean.

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata module

The sampledata module provides functions for creating raster and vector data, constants for assisting with the creation of data, and some sample spatial reference data.

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.SRS_COLOMBIA

An instance of the ReferenceData namedtuple for the Colombia projection.

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.SRS_WILLAMETTE

An instance of the ReferenceData namedtuple for the Willamette projection (UTM zone 10N)

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.VECTOR_FIELD_TYPES

A dictionary mapping string field names to OGR field types.

class natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.ReferenceData

A namedtuple with two attributes and a function:

  • projection: The well-known-text projection string.
  • origin: A two-tuple of ints or floats representing the origin
    of the projection in the cartesian plane.
  • pixel_size: A function accepting one parameter (an int or float).
    Returns a two-tuple of the height and width of pixels in this projection.
class natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.ReferenceData(projection, origin, pixel_size)

Bases: tuple

origin

Alias for field number 1

pixel_size

Alias for field number 2

projection

Alias for field number 0

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.cleanup(uri)

Remove the uri. If it’s a folder, recursively remove its contents.

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.create_raster_on_disk(band_matrices, origin, projection_wkt, nodata, pixel_size, datatype='auto', format='GTiff', dataset_opts=None, filename=None)

Create a GDAL raster on disk.

Parameters:
  • band_matrices (list of numpy.ndarray) – a list of 2D numpy matrices representing pixel values, one array per band to be created in the output raster. The index of the matrix will be the index of the corresponding band that is created.
  • origin (tuple of numbers) – A 2-element tuple representing the origin of the pixel values in the raster. This must be a tuple of numbers.
  • projection_wkt (string) – A string WKT represntation of the projection to use in the output raster.
  • nodata (int or float) – The nodata value for the raster. If nodata is None, a nodata value will not be set.
  • pixel_size (tuple of numbers) – A 2-element tuple representing the size of all pixels in the raster arranged in the order (x, y). Either or both of these values could be negative.
  • datatype (int or 'auto') – A GDAL datatype. If ‘auto’, a reasonable datatype will be chosen based on the datatype of the numpy matrix.
  • format='GTiff' (string) – The string driver name to use. Determines the output format of the raster. Defaults to GeoTiff. See http://www.gdal.org/formats_list.html for a list of available formats.
  • dataset_opts=None (list of strings) – A list of strings to pass to the underlying GDAL driver for creating this raster. Possible options are usually format dependent. If None, no options will be passed to the driver. For a GTiff, the most common set of options will usually be [‘TILED=YES’]. GTiff block sizes default to 256 along an edge if not otherwise specified.
  • filename=None (string) – If provided, the new raster should be created at this filepath. If None, a new temporary file will be created within your tempfile directory (within tempfile.gettempdir()) and this path will be returned from the function.

Notes

  • Writes a raster created with the given options.
  • File management is up to the user.
Returns:The string path to the new raster created on disk.
natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.create_vector_on_disk(geometries, projection, fields=None, attributes=None, vector_format='GeoJSON', filename=None)

Create an OGR-compatible vector on disk in the target format.

Parameters:
  • geometries (list) – a list of Shapely objects.
  • projection (string) – a WKT representation of the vector’s projection.
  • fields (dict or None) – a python dictionary mapping string fieldname to a string datatype representation of the target ogr fieldtype. Example: {‘ws_id’: ‘int’}. See pygeoprocessing.testing.sampledata.VECTOR_FIELD_TYPES.keys() for the complete list of all allowed types. If None, the datatype will be determined automatically based on the types of the attribute values.
  • attributes (list of dicts) – a list of python dictionary mapping fieldname to field value. The field value’s type must match the type defined in the fields input. It is an error if it doesn’t.
  • vector_format (string) – a python string indicating the OGR format to write. GeoJSON is pretty good for most things, but doesn’t handle multipolygons very well. ‘ESRI Shapefile’ is usually a good bet.
  • filename=None (None or string) – None or a python string where the file should be saved. If None, the vector will be saved to a temporary folder.

Notes

  • Writes a vector created with the given options.
  • File management is up to the user.
Returns:A string filepath to the location of the vector on disk.
natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.dtype_index(dtype)

Return the relative data complexity index of the datatype provided.

Parameters:dtype (numpy.dtype or int GDAL datatype) – The dtype to check.
Returns:The data complexity index relative to the other numpy/gdal type pairs.
natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.make_geotransform(x_len, y_len, origin)

Build an array of affine transformation coefficients.

Parameters:
  • x_len (int or float) – The length of a pixel along the x axis. Generally, this will be a positive value.
  • y_len (int of float) – The length of a pixel along the y axis. Generally (in North-up rasters), this will be a negative value.
  • origin (tuple of ints or floats) – The origin of the raster, a 2-element tuple.
Returns:

[
    Origin along x-axis,
    Length of a pixel along the x axis,
    0.0,  (this is the standard in north-up projections)
    Origin along y-axis,
    Length of a pixel along the y axis,
    0.0   (this is the standard in north-up projections)
]

Return type:

A 6-element list with this structure

natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.open_files_in_gis_browser(file_list)

Open the specified geospatial files with the provided application or visualization method (qgis is the only supported method at the moment).

NOTE: This functionality does not appear to work on Darwin systems with QGIS installed via homebrew. In this case, QGIS will open, but will not show the files requested.

Parameters:file_list (list) – a list of string filepaths to geospatial files.
Returns:Nothing.
natcap.invest.pygeoprocessing_0_3_3.testing.sampledata.projection_wkt(epsg_id)

Import a projection from an EPSG code.

Parameters:proj_id (int) – If an int, it’s an EPSG code
Returns:A WKT projection string.

natcap.invest.pygeoprocessing_0_3_3.testing.scm module

natcap.invest.pygeoprocessing_0_3_3.testing.scm.checkout_svn(local_path, remote_path, rev=None)

Check out (or update) an SVN repository to the target revision.

Parameters:
  • local_path (string) – The path to an SVN repository on disk.
  • remote_path (string) – The path to the SVN repository to check out.
  • rev=None (string or None) – The revision to check out. If None, the latest revision will be checked out.
natcap.invest.pygeoprocessing_0_3_3.testing.scm.load_config(config_file)

Load the SCM configuration from a JSON configuration file.

Expects that the config file has three values: {

“local”: <local path to repo>, “remote”: <path to remote repo>, “rev”: <target revision>

}

natcap.invest.pygeoprocessing_0_3_3.testing.scm.skip_if_data_missing(repo_path)

Decorator for unittest.TestCase test functions. Raises SkipTest if the pygeoprocessing data repository has not been cloned.

Parameters:repo_path (string) – The path to an SVN data repo on disk.
Returns:A reference to a decorated unittest.TestCase test method that will raise SkipTest when executed.

natcap.invest.pygeoprocessing_0_3_3.testing.utils module

natcap.invest.pygeoprocessing_0_3_3.testing.utils.checksum_folder(workspace_uri, logfile_uri, style='GNU', ignore_exts=None)

Recurse through the workspace_uri and for every file in the workspace, record the filepath and md5sum to the logfile. Additional environment metadata will also be recorded to help debug down the road.

This output logfile will have two sections separated by a blank line. The first section will have relevant system information, with keys and values separated by ‘=’ and some whitespace.

This second section will identify the files we’re snapshotting and the md5sums of these files, separated by ‘::’ and some whitspace on each line. MD5sums are determined by calling natcap.testing.utils.digest_file().

Parameters:
  • workspace_uri (string) – A URI to the workspace to analyze
  • logfile_uri (string) – A URI to the logfile to which md5sums and paths will be recorded.
  • style='GNU' (string) – Either ‘GNU’ or ‘BSD’. Corresponds to the style of the output file.
  • ignore_exts=None (list/set of strings or None) – Extensions of files to ignore when checksumming. If None, all files will be included. If a list or set of extension strings, any file with that extension will be skipped.
Returns:

Nothing.

natcap.invest.pygeoprocessing_0_3_3.testing.utils.digest_file(filepath)

Get the MD5sum for a single file on disk. Files are read in a memory-efficient fashion.

Parameters:filepath (string) – a string path to the file or folder to be tested or a list of files to be analyzed.
Returns:An md5sum of the input file
natcap.invest.pygeoprocessing_0_3_3.testing.utils.digest_file_list(filepath_list, ifdir='skip')

Create a single MD5sum from all the files in filepath_list.

This is done by creating an MD5sum from the string MD5sums calculated from each individual file in the list. The filepath_list will be sorted before generating an MD5sum. If a given file is in this list multiple times, it will be double-counted.

Note

When passed a list with a single file in it, this function will produce a different MD5sum than if you were to simply take the md5sum of that single file. This is because this function produces an MD5sum of MD5sums.

Parameters:
  • filepath_list (list of strings) – A list of files to analyze.
  • ifdir (string) – Either ‘skip’ or ‘raise’. Indicates what to do if a directory is encountered in this list. If ‘skip’, the directory skipped will be logged. If ‘raise’, IOError will be raised with the directory name.
Returns:

A string MD5sum generated for all of the files in the list.

Raises:

IOError – When a file in filepath_list is a directory and ifdir == skip or a file could not be found.

natcap.invest.pygeoprocessing_0_3_3.testing.utils.digest_folder(folder)

Create a single MD5sum from all of the files in a folder. This recurses through folder and will take the MD5sum of all files found within.

Parameters:folder (string) – A string path to a folder on disk.

Note

When there is a single file within this folder, the return value of this function will be different than if you were to take the MD5sum of just that one file. This is because we are taking an MD5sum of MD5sums.

Returns:
A string MD5sum generated from all of the files contained in
this folder.

Module contents

The testing subpackage provides reasonable testing functionality for building programmatic tests with and of geospatial data. It provides functions to generate input data of both raster and vector formats, and offers assertions to verify the correctness of geospatial outputs.

Most useful features of this package have been exposed at the pygeoprocessing.testing level.

Select locations have been chosen for their spatial references. These references are available in the sampledata module and al have the prefix SRS_:

from pygeoprocessing.testing import sampledata

sampledata.SRS_WILLAMETTE
sampledata.SRS_COLOMBIA

For writing tests, write them as you normally do! Assertions are generally file-based and raise AssertionError when a failure is encountered.

This example is relatively simplistic, since there will often be many more assertions you may need to make to be able to test your model effectively:

import unittest
import pygeoprocessing.testing
import natcap.invest.example_model

class ExampleTest(unittest.TestCase):
    def test_some_model(self):
        example_args = {
            'workspace_dir': './workspace',
            'arg_1': 'foo',
            'arg_2': 'bar',
        }
        natcap.invest.example_model.execute(example_args)

        # example assertion
        pygeoprocessing.testing.assert_rasters_equal('workspace/raster_1.tif',
            'regression_data/raster_1.tif')
natcap.invest.pygeoprocessing_0_3_3.testing.create_raster_on_disk(band_matrices, origin, projection_wkt, nodata, pixel_size, datatype='auto', format='GTiff', dataset_opts=None, filename=None)

Create a GDAL raster on disk.

Parameters:
  • band_matrices (list of numpy.ndarray) – a list of 2D numpy matrices representing pixel values, one array per band to be created in the output raster. The index of the matrix will be the index of the corresponding band that is created.
  • origin (tuple of numbers) – A 2-element tuple representing the origin of the pixel values in the raster. This must be a tuple of numbers.
  • projection_wkt (string) – A string WKT represntation of the projection to use in the output raster.
  • nodata (int or float) – The nodata value for the raster. If nodata is None, a nodata value will not be set.
  • pixel_size (tuple of numbers) – A 2-element tuple representing the size of all pixels in the raster arranged in the order (x, y). Either or both of these values could be negative.
  • datatype (int or 'auto') – A GDAL datatype. If ‘auto’, a reasonable datatype will be chosen based on the datatype of the numpy matrix.
  • format='GTiff' (string) – The string driver name to use. Determines the output format of the raster. Defaults to GeoTiff. See http://www.gdal.org/formats_list.html for a list of available formats.
  • dataset_opts=None (list of strings) – A list of strings to pass to the underlying GDAL driver for creating this raster. Possible options are usually format dependent. If None, no options will be passed to the driver. For a GTiff, the most common set of options will usually be [‘TILED=YES’]. GTiff block sizes default to 256 along an edge if not otherwise specified.
  • filename=None (string) – If provided, the new raster should be created at this filepath. If None, a new temporary file will be created within your tempfile directory (within tempfile.gettempdir()) and this path will be returned from the function.

Notes

  • Writes a raster created with the given options.
  • File management is up to the user.
Returns:The string path to the new raster created on disk.
natcap.invest.pygeoprocessing_0_3_3.testing.create_vector_on_disk(geometries, projection, fields=None, attributes=None, vector_format='GeoJSON', filename=None)

Create an OGR-compatible vector on disk in the target format.

Parameters:
  • geometries (list) – a list of Shapely objects.
  • projection (string) – a WKT representation of the vector’s projection.
  • fields (dict or None) – a python dictionary mapping string fieldname to a string datatype representation of the target ogr fieldtype. Example: {‘ws_id’: ‘int’}. See pygeoprocessing.testing.sampledata.VECTOR_FIELD_TYPES.keys() for the complete list of all allowed types. If None, the datatype will be determined automatically based on the types of the attribute values.
  • attributes (list of dicts) – a list of python dictionary mapping fieldname to field value. The field value’s type must match the type defined in the fields input. It is an error if it doesn’t.
  • vector_format (string) – a python string indicating the OGR format to write. GeoJSON is pretty good for most things, but doesn’t handle multipolygons very well. ‘ESRI Shapefile’ is usually a good bet.
  • filename=None (None or string) – None or a python string where the file should be saved. If None, the vector will be saved to a temporary folder.

Notes

  • Writes a vector created with the given options.
  • File management is up to the user.
Returns:A string filepath to the location of the vector on disk.
natcap.invest.pygeoprocessing_0_3_3.testing.digest_file(filepath)

Get the MD5sum for a single file on disk. Files are read in a memory-efficient fashion.

Parameters:filepath (string) – a string path to the file or folder to be tested or a list of files to be analyzed.
Returns:An md5sum of the input file
natcap.invest.pygeoprocessing_0_3_3.testing.digest_file_list(filepath_list, ifdir='skip')

Create a single MD5sum from all the files in filepath_list.

This is done by creating an MD5sum from the string MD5sums calculated from each individual file in the list. The filepath_list will be sorted before generating an MD5sum. If a given file is in this list multiple times, it will be double-counted.

Note

When passed a list with a single file in it, this function will produce a different MD5sum than if you were to simply take the md5sum of that single file. This is because this function produces an MD5sum of MD5sums.

Parameters:
  • filepath_list (list of strings) – A list of files to analyze.
  • ifdir (string) – Either ‘skip’ or ‘raise’. Indicates what to do if a directory is encountered in this list. If ‘skip’, the directory skipped will be logged. If ‘raise’, IOError will be raised with the directory name.
Returns:

A string MD5sum generated for all of the files in the list.

Raises:

IOError – When a file in filepath_list is a directory and ifdir == skip or a file could not be found.

natcap.invest.pygeoprocessing_0_3_3.testing.digest_folder(folder)

Create a single MD5sum from all of the files in a folder. This recurses through folder and will take the MD5sum of all files found within.

Parameters:folder (string) – A string path to a folder on disk.

Note

When there is a single file within this folder, the return value of this function will be different than if you were to take the MD5sum of just that one file. This is because we are taking an MD5sum of MD5sums.

Returns:
A string MD5sum generated from all of the files contained in
this folder.
natcap.invest.pygeoprocessing_0_3_3.testing.checksum_folder(workspace_uri, logfile_uri, style='GNU', ignore_exts=None)

Recurse through the workspace_uri and for every file in the workspace, record the filepath and md5sum to the logfile. Additional environment metadata will also be recorded to help debug down the road.

This output logfile will have two sections separated by a blank line. The first section will have relevant system information, with keys and values separated by ‘=’ and some whitespace.

This second section will identify the files we’re snapshotting and the md5sums of these files, separated by ‘::’ and some whitspace on each line. MD5sums are determined by calling natcap.testing.utils.digest_file().

Parameters:
  • workspace_uri (string) – A URI to the workspace to analyze
  • logfile_uri (string) – A URI to the logfile to which md5sums and paths will be recorded.
  • style='GNU' (string) – Either ‘GNU’ or ‘BSD’. Corresponds to the style of the output file.
  • ignore_exts=None (list/set of strings or None) – Extensions of files to ignore when checksumming. If None, all files will be included. If a list or set of extension strings, any file with that extension will be skipped.
Returns:

Nothing.

natcap.invest.pygeoprocessing_0_3_3.testing.assert_close(value_a, value_b, rel_tol=1e-09, abs_tol=0.0, msg=None)

Assert equality to an absolute tolerance.

Parameters:
  • value_a (int or float) – The first value to test.
  • value_b (int or float) – The second value to test.
  • rel_tol (int or float) – The relative numerical tolerance. If the relative difference of these values are less than this value, assertion will pass.
  • abs_tol (int or float) – The absolute numerical tolerance. If the difference of the values being tested are less than this value, the assertion will pass.
  • msg=None (string or None) – The assertion message to use if value_a and value_b are not found to be equal out to the target tolerance.
Returns:

None.

Raises:
  • AssertionError – Raised when the values are not equal out to the
  • desired tolerance.
natcap.invest.pygeoprocessing_0_3_3.testing.isclose(val_a, val_b, rel_tol=1e-09, abs_tol=0.0)

Assert that values are equal to the given tolerance.

Adapted from the python 3.5 standard library based on the specification found in PEP485.

Parameters:
  • val_a (int or float) – The first value to test
  • val_b (int or float) – The second value to test
  • rel_tol (int or float) – is the relative tolerance - it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero.
  • abs_tol (float) – is the minimum absolute tolerance - useful for comparisons near zero. abs_tol must be at least zero.
Returns:

A boolean.

natcap.invest.pygeoprocessing_0_3_3.testing.assert_rasters_equal(a_uri, b_uri, rel_tol=1e-09, abs_tol=0.0)

Assert te equality of rasters a and b out to the given tolerance.

This assertion method asserts the equality of these raster characteristics:

  • Raster height and width
  • The number of layers in the raster
  • Each pixel value, such that the absolute difference between the
    pixels is less than tolerance.
  • Projection
Parameters:
  • a_uri (string) – a URI to a GDAL dataset
  • b_uri (string) – a URI to a GDAL dataset
  • rel_tol (int or float) – the relative tolerance to which values should be asserted. This is a numerical tolerance, not the number of places to which a value should be rounded. If the relative value is below this value then the assert passes.
  • abs_tol (int or float) – absolute tolerance to which values should be asserted. If absolute difference in values is below this value then the assert passes.
Returns:

None

Raises:
  • IOError – Raised when one of the input files is not found on disk.
  • AssertionError – Raised when the two rasters are found to be not
  • equal to each other.
natcap.invest.pygeoprocessing_0_3_3.testing.assert_vectors_equal(a_uri, b_uri, field_tolerance)

Assert that the vectors at a_uri and b_uri are equal to each other.

This assertion method asserts the equality of these vector characteristics:

  • Number of layers in the vector
  • Number of features in each layer
  • Geometry type of the layer
  • Feature geometry type
  • Number of fields in each feature
  • Name of each field
  • Field values for each feature
  • Projection
Parameters:
  • a_uri (string) – a URI to an OGR vector
  • b_uri (string) – a URI to an OGR vector
  • field_tolerance (int or float) – The relative numerical tolerance to which field values should be asserted.
Raises:
  • IOError – Raised if one of the input files is not found on disk.
  • AssertionError – Raised if the vectors are not found to be equal to
  • one another.
Returns
None
natcap.invest.pygeoprocessing_0_3_3.testing.assert_csv_equal(a_uri, b_uri, rel_tol=1e-09, abs_tol=0.0)

Assert the equality of CSV files at a_uri and b_uri.

Tests if csv files a and b are ‘almost equal’ to each other on a per cell basis. Numeric cells are asserted to be equal within the given tolerance. Other cell types are asserted to be equal.

Parameters:
  • a_uri (string) – a URI to a csv file
  • b_uri (string) – a URI to a csv file
  • rel_tol (int or float) – The relative numerical tolerance allowed, if relative difference of values are less than this, assertion passes.
  • abs_tol (int or float) – The absolute numerical tolerance allowed, if difference of values are less than this, assertion passes.
Raises:
  • AssertionError – Raised when the two CSV files are found to be
  • different.
Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assert_md5_equal(uri, regression_hash)

Assert the MD5sum of a file against a regression MD5sum.

This method is a convenience method that uses natcap.invest.testing.digest_file() to determine the MD5sum of the file located at uri. It is functionally equivalent to calling:

if digest_file(uri) != '<some md5sum>':
    raise AssertionError

Regression MD5sums can be calculated for you by using natcap.invest.testing.digest_file() or a system-level md5sum program.

Parameters:
  • uri (string) – a string URI to the file to be tested.
  • regression_hash (string) – a string md5sum to be tested against.
Raises:
  • AssertionError – Raised when the MD5sum of the file at uri
  • differs from the provided regression md5sum hash.
Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assert_json_equal(json_1_uri, json_2_uri)

Assert two JSON files against each other.

The two JSON files provided will be opened, read, and their contents will be asserted to be equal. If the two are found to be different, the diff of the two files will be printed.

Parameters:
  • json_1_uri (string) – a uri to a JSON file.
  • json_2_uri (string) – a uri to a JSON file.
Raises:

AssertionError – Raised when the two JSON objects differ.

Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assert_text_equal(text_1_uri, text_2_uri)

Assert that two text files are equal.

This comparison is done line-by-line.

Parameters:
  • text_1_uri (string) – a python string uri to a text file. Considered the file to be tested.
  • text_2_uri (string) – a python string uri to a text file. Considered the regression file.
Raises:

AssertionError – Raised when a line differs in the two files.

Returns:

None

natcap.invest.pygeoprocessing_0_3_3.testing.assert_checksums_equal(checksum_file, base_folder=None)

Assert all files in the checksum_file have the same checksum.

Checksum files could be created by pygeoprocessing.testing.utils.checksum_folder(), but this function may also assert the output of GNU md5sum or BSD md5. Either format (GNU or BSD) may be provided as input to this assertion function. Any files not included in checksum_file are ignored.

Parameters:
  • checksum_file (string) – the path to the snapshot file to use.
  • base_folder=None (string or None) – the folder to refer to as the base path for files in the checksum_file. If None, the current working directory will be used.
Raises:

AssertionError – when a nonmatching md5sum is found.