natcap.invest.file_registry

class natcap.invest.file_registry.FileRegistry(outputs, workspace_dir, file_suffix=None)

Bases: object

The FileRegistry creates and tracks absolute paths that correspond to model outputs defined in a ModelSpec. Instantiate a FileRegistry from a list of spec.Outputs, a target directory, and a file suffix:

file_registry = FileRegistry(MODEL_SPEC.outputs, workspace_dir, file_suffix)

The file registry is responsible for creating the output filepaths defined in the ModelSpec, in their real locations within the workspace directory, and adding the file suffix if one is provided.

The primary way of interacting with a FileRegistry is by indexing. You can index the FileRegistry by output IDs. For output IDs that represent a pattern (that use square brackets to represent a section of the path that is dynamically substituted), pass in the value(s) to substitute to the index operator. For example,

file_registry['aligned_dem'] returns the equivalent of os.path.join(workspace_dir, 'aligned_dem_{file_suffix}.tif')

file_registry['[CROP]_[PERCENTILE]_coarse_yield', 'corn', '25th'] returns the equivalent of os.path.join(workspace_dir, f'intermediate_outputs/corn_25th_coarse_yield_{file_suffix}.tif')

The FileRegistry tracks which paths have been accessed in its registry attribute, which is a nested dictionary mapping keys to absolute paths. For example, after performing the indexing examples above, file_registry.registry would be:

{
    'aligned_dem': '/.../workspace_dir/aligned_dem_suffix.tif',
    '[CROP]_[PERCENTILE]_coarse_yield': {
        'corn': {
            'yield_25th': '/.../workspace_dir/intermediate_outputs/corn_yield_25th_coarse_yield_suffix.tif''
        }
    }
}