region-estimators package
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:
- 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.
- 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)
Metadata
Release files for region-estimators 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| region_estimators-1.2.0.tar.gz | 18.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| region_estimators-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.0 kB
Release files / region_estimators-1.2.0.tar.gz
| Download URL | region_estimators-1.2.0.tar.gz |
|---|---|
| Size | 18.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5c8202bd029157097ba77c6c8844197e29e250e70ab42fd38b3a1c65e8e52659
|
|
BLAKE2b-256 checksum How to use checksums |
0eb055dd9acec3b095f51fc4013dbe032979b270e86763ccfe9439c3b1d1e7c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.3
|
Release files / region_estimators-1.2.0-py3-none-any.whl
| Download URL | region_estimators-1.2.0-py3-none-any.whl |
|---|---|
| Size | 24.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
956758b3770bda7f35ad3ac4c574ea6d0e62e3433b560fa14e45ac5cf9795060
|
|
BLAKE2b-256 checksum How to use checksums |
97255c1701c28d421b2c55051d5a7302de4bab1f9cf17f71304f108ece1d776d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.3
|