Skip to content

API that calculates coverage of a network of air quality sensors

License

Notifications You must be signed in to change notification settings

NewcastleRSE/spatial-inequality-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spatial-inequality

Repository for the Spatial Inequality in the Smart City project.

Papers and Publications

If you're looking for the scripts used to generate the figures in the paper "Optimising for equity: Sensor coverage, networks and the responsive city", please go to the OptimisingForEquity directory.

Pre-requisites

  1. Git
  2. Conda

Setup

  1. Clone the repo:

    git clone https://github.com/alan-turing-institute/spatial-inequality.git
  2. Change to the spatial-inequality directory:

    cd spatial-inequality
  3. Create the conda environment:

    conda env create
    
  4. Activate the environment:

    conda activate spatial-inequality
    

API

Start the API

docker-compose up

Should then be available on 0.0.0.0:5000

Command to force a rebuild if something hasn't udpated correctly:

docker-compose up --build --force-recreate

Submitting Optimisation Jobs with SocketIO

Generate a pseudo-optimised network of sensors with a greedy algorithm. See scripts/client.py for an example.

  • Submit an optimisation job:

    • Client emits event submitJob with data {"n_sensors": 10, "theta": 500}
    • Server emits event job with job data.
    • On job completion, server emits jobFinished with job_id and result.
    • Optionally can give the following parameters with submitJob:
      • min_age: Minimum age to include for population coverage (default: 0)
      • max_age: Maximum age to include for population coverage (default: 90)
      • population_weight: Weight for population coverage (default: 1)
      • workplace_weight: Weight for place of work coverage (default: 0)
  • Retrieve the result/status of a job:

    • Client emits event getJob with data <the_job_id>
    • Server emits event job with job data.
  • Get a list of available job IDs:

    • Client emits event getQueue
    • Server emits event queue with queue data.
  • Remove a job from the queue:

    • Client emits event deleteJob with data <the_job_id>
    • Server emits event message with deletion result.
  • Remove all jobs from the queue:

    • Client emits event deleteQueue
    • Server emits event message with deletion result.

Coverage Endpoint

The /coverage endpoint computes coverage for a user-defined network (set of sensors placed at output area centroids), and takes a JSON with the following format:

{
   "sensors": ["E00042671","E00042803"],
   "theta": 500,
   "lad20cd": "E08000021"
}

where theta and lad20cd are optional and take the values above by default, but sensors must be defined and takes a list of oa11cd codes that have sensors in them.

It should return a JSON with this structure:

{
   "oa_coverage":[
      {"coverage":0.6431659719289781,"oa11cd":"E00042665"}, 
      ...,
    ],
   "total_coverage": {
      "pop_children":0.0396946631327479,
      "pop_elderly":0.024591629984248815,
      "pop_total":0.059299090984356984,
      "workplace":0.0947448314996531
   }
}

Dependencies

The dockerised version of the code used for the API uses only pip and the packages in requirements.txt. This doesn't include some plotting and optimisation libraries included in the conda environment.

Deployment

Create a web app on Azure, selecting the 'container' option. In the deplyoment center, add this dompose file:

version: '3'

services:

  web:
    image: katecourt123/sisc:latest
    container_name: spineq-api
    ports:
      - "5000:5000"
    depends_on:
      - redis
      - worker
    command: "gunicorn --bind 0.0.0.0:5000 --worker-class eventlet -w 1 app:app"

  worker:
    image: katecourt123/sisc:latest
    container_name: spineq-worker
    depends_on:
      - redis
    command: python worker.py

  redis:
    image: redis
    container_name: redis
    ```

Run `arch`. If using arm64, build image with platform tag. 

1. Build the image locally:
`docker  build . -t katecourt123/sisc:latest` or `docker  build --platform linux/amd64 . -t katecourt123/sisc:latest`

2. Push to Docker hub:
`docker push katecourt123/sisc:latest`

3. Restart [web service](https://portal.azure.com/#@newcastle.onmicrosoft.com/resource/subscriptions/e7cbfebb-f482-46c4-a90b-126855b03325/resourceGroups/sisc/providers/Microsoft.Web/sites/sisc/appServices) on Azure

About

API that calculates coverage of a network of air quality sensors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 99.4%
  • Other 0.6%