Skip to main content

Make estimations for geographic regions, based on actual data (e.g. from sensors)

Project description

region-estimators package

Build Status

region-estimators is a Python library to calculate regional estimations of scalar quantities, based on some known scalar quantities at specific locations. For example, estimating the NO2 (pollution) level of a postcode/zip region, based on site data nearby. This first version of the package is initialised with 2 estimation methods:

  1. ConcentricRegions: look for actual data points in gradually wider rings, starting with sites within the region, and then working in rings outwards, until sites are found. If more than one site is found at the final stage, it takes the mean.
  2. Simple Distance measure: This is a very basic implementation... Find the nearest site to the region and use that value. If sites exist within the region, take the mean.

The sections below are:

Repository Structure

The region_estimators directory contains the python modules used by the tools. The sample_input_files directory contains examples of the input files required to test installation. Operational scripts are stored within the scripts directory. A set of python unittest test files can be found in the test directory.

.
├── region_estimators
├── sample_input_files
├── scripts
│   └── outputs
├── test

Requirements

Use the package manager pip to install region-estimators.

pip install shapely
pip install pandas
pip install geopandas
pip install region-estimators

Usage

An example python script that uses the region-estimators package can be found in the scripts directory. The required parts are highlighted in the following shortened excerpt:

from shapely import wkt
import pandas as pd
from region_estimators import RegionEstimatorFactory, EstimationData


if __name__ == '__main__':
    # obtain inputs arguments 
    #... [See scripts for obtaining via commandline arguments (argparse)]    

    # Prepare input files  (For sample input files, see the 'sample_input_files' folder)
    df_regions = pd.read_csv(regions_filespec, index_col='region_id')
    df_sites = pd.read_csv(sites_filespec, index_col='site_id')
    df_actuals = pd.read_csv(actuals_filespec)

    # Convert the regions geometry column from string to wkt format using wkt
    df_regions['geometry'] = df_regions.apply(lambda row: wkt.loads(row.geometry), axis=1)

    # Create estimator, the first parameter is the estimation method.

    estimation_data = EstimationData(df_sites, df_regions, df_actuals)

    estimator = RegionEstimatorFactory.region_estimator(method, estimation_data, verbose, max_processors)

    # Make estimations
    if method == 'concentric-regions':
        estimator.max_ring_count = max_rings
    df_estimates = estimator.get_estimations(measurement, region_id, timestamp)

    print(df_estimates)

    # Convert dataframe result to (for example) a csv file:
    if args.save_to_csv:
        df_estimates.to_csv(os.path.join(outdir_name, 'estimates_{}.csv'.format(outfile_suffix)))
Details of estimation_data class creation / parameters:
├── Constructor
│   ├── 3 pandas.Dataframe objects:  
│   │   └── regions (metadata)
│   │   │    └── required columns
│   │   │   │    └── 'region_id' (INDEX): identifier for region (must be unique to each region)
│   │   │   │    └── 'geometry' (shapely.wkt/geom.wkt):  Multi-polygon representing regions location and shape.

│   │   └── sites (metadata)
│   │   │    └── required columns
│   │   │   │    └── 'site_id' (INDEX): identifier for site (must be unique to each site)
│   │   │   │    └── 'latitude' (numeric): latitude of site location
│   │   │   │    └── 'longitude' (numeric): longitude of site location
│   │   │    └── optional columns
│   │   │   │    └── 'name' (string): Human readable name of site

│   │   └── actuals (data)
│   │   │    └── required columns
│   │   │   │    └── 'timestamp' (string): timestamp of actual reading
│   │   │   │    └── 'site_id': (string) ID of site which took actua in sites (in value and type))
│   │   │   │    └── [one or more value columns] (float):    value of actual measurement readings.

├── Returns
│   ├── Initialised instance of EstimationData class
Details of region_estimators factory class parameters:
├── Required inputs
│   ├── method_name (string): The estimation method to be uesed
                              In the first version the options are 'concentric-regions' or 'distance-simple'
│   ├── estimation_data (EstimationData instance - see above): data required to make estimations

├── Optional inputs
│   ├── verbose: (int) Verbosity of output level. zero or less => No debug output. Default=0
│   ├── max_processors (int) Maximum number of processors to use. Default=1
│                     (Maximum: Number of processor available)

├── Returns
│   ├── Initialised instance of subclass of RegionEstimator class
Details of RegionEstimator (subclass) get_estimations method parameters
├── Required inputs
│   ├── measurement: which measurement to be estimated (e.g. 'urtica')

├── Optional inputs
│   ├── region_id: region identifier (string (or None to get all regions))
│   ├── timestamp: timestamp identifier (string (or None to get all timestamps))
│   ├── ignore_site_ids: (list of str) Site IDs to be ignored. Default=[]

├── Returns
│   ├── pandas dataframe, with columns:
│   │   └── measurement
│   │   └── region_id
│   │   └── timestamp
│   │   └── value: (float or empty) The estimated value
│   │   └── extra_data: (dict string) Extra info about the estimation calculation

WARNING! - estimator.get_estimates('urtica', None, None) will calculate every region at every timestamp.

Unit testing

A set of python unittest test files can be found in the test directory, and can be run from the shell (once the necessary requirements are installed) with the command:

python -m unittest

Contributing

Improvements to code

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Extending the RegionEstimator class

with further method classes:

  • Add the new class name (str) to the available_methods list in region_estimator_factory.py:
    available_methods = {'concentric-regions': ConcentricRegionsEstimator,
                          'distance-simple': DistanceSimpleEstimator}
    
  • Create the new RegionEstimator subclass, following the layout in other current subclasses. E.g. ConcentricRegionsEstimator (concentric_regions_estimator.py) and DistanceSimpleEstimator (distance_simple_estimator.py)

Copyright & Licensing

This software has been developed by the Research IT group at the University of Manchester for an Alan Turing Institute project.

(c) 2019-2021 University of Manchester. Licensed under the MIT license (https://opensource.org/licenses/MIT)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

region_estimators-1.2.0.tar.gz (18.7 kB view hashes)

Uploaded Source

Built Distribution

region_estimators-1.2.0-py3-none-any.whl (24.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page