Package: region-estimators
region-estimators packageregion-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 StructureRequirementsUsageUnit TestingContributingCopyright and licensingRepository StructureTheregion_estimatorsdirectory contains the python modules used by the tools.
Thesample_input_filesdirectory contains examples of the input files required to test installation.
Operational scripts are stored within thescriptsdirectory.
A set of python unittest test files can be found in thetestdirectory..
├── region_estimators
├── sample_input_files
├── scripts
│   └── outputs
├── testRequirementsUse the package managerpipto install region-estimators.pipinstallshapely
pipinstallpandas
pipinstallgeopandas
pipinstallregion-estimatorsUsageAn example python script that uses the region-estimators package can be found in thescriptsdirectory.
The required parts are highlighted in the following shortened excerpt:fromshapelyimportwktimportpandasaspdfromregion_estimatorsimportRegionEstimatorFactory,EstimationDataif__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 wktdf_regions['geometry']=df_regions.apply(lambdarow: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 estimationsifmethod=='concentric-regions':estimator.max_ring_count=max_ringsdf_estimates=estimator.get_estimations(measurement,region_id,timestamp)print(df_estimates)# Convert dataframe result to (for example) a csv file:ifargs.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 classDetails 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 classDetails 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 testingA set of python unittest test files can be found in thetestdirectory, and can be run from the shell
(once the necessary requirements are installed) with the command:python-munittestContributingImprovements to codePull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.Extending the RegionEstimator classwith 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 & LicensingThis software has been developed by theResearch ITgroup at theUniversity of Manchesterfor anAlan Turing Instituteproject.(c) 2019-2021 University of Manchester.
Licensed under the MIT license (https://opensource.org/licenses/MIT)