Skip to content

Unique Subscriber Counts

Counting the number of subscribers per region

In this worked example we use FlowClient to count the number of subscribers per region. This is designed as a simple example to demonstrate that a FlowKit system has been successfully deployed.

The Jupyter notebook for this worked example can be downloaded here, or can be run using the quick start setup.

Load FlowClient and connect to FlowAPI

We start by importing FlowClient. We also import geopandas and mapboxgl, which we will use later to to visualise the data.

import flowclient
import os
import numpy as np
import geopandas as gpd
import mapboxgl
from mapboxgl.utils import create_color_stops

We must next generate a FlowAPI access token using FlowAuth. If you are running this notebook using the quick start setup, generating a token requires the following steps:

  1. Visit the FlowAuth login page at http://localhost:9091.
  2. Log in with username TEST_USER and password DUMMY_PASSWORD.
  3. Under "My Servers", select TEST_SERVER.
  4. Click the + button to create a new token.
  5. Give the new token a name, and click SAVE.
  6. Copy the token string using the COPY button.
  7. Paste the token in this notebook as TOKEN.

The steps are the same in a production setup, but the FlowAuth URL, login details and server name will differ.

Once we have a token, we can start a connection to the FlowAPI system using flowclient.connect(). If you are connecting to FlowAPI over https (recommended) and the system administrator has provided you with an SSL certificate file, you should provide the path to this file as the ssl_certificate argument toflowclient.connect() (in this example, you can set the path in the environment variable SSL_CERTIFICATE_FILE). If you are connecting over http, this argument is not required.

conn = flowclient.connect(
    url=os.getenv("FLOWAPI_URL", "http://localhost:9090"),
    token=TOKEN,
    ssl_certificate=os.getenv("SSL_CERTIFICATE_FILE"),
)

Get tower counts

We can get subscriber counts using a unique_subscriber_counts query. We start by creating a specification for a unique_subscriber_counts query to count the number of subscribers per level 3 administrative unit during the first week of 2016.

query_spec = flowclient.aggregates.unique_subscriber_counts_spec(
    start_date="2016-01-01", end_date="2016-01-08", aggregation_unit="admin3"
)
query_spec
{'aggregation_unit': 'admin3',
 'end_date': '2016-01-08',
 'event_types': None,
 'geom_table': None,
 'geom_table_join_column': None,
 'hours': None,
 'mapping_table': None,
 'query_kind': 'unique_subscriber_counts',
 'start_date': '2016-01-01',
 'subscriber_subset': None}

We run this query using get_result, which returns the result as a pandas DataFrame.

subscribers_per_admin3 = flowclient.get_result(connection=conn, query_spec=query_spec)
subscribers_per_admin3.head()
pcod value
0 NPL.2.3.3_1 1567
1 NPL.3.2.5_1 3525
2 NPL.1.3.3_1 2409
3 NPL.4.1.1_1 1825
4 NPL.1.1.3_1 11936

Visualise tower counts on a choropleth map

We use the get_geography function to download the geography for the level 3 administrative regions as GeoJSON.

# Download geography data as GeoJSON.
regions = flowclient.get_geography(connection=conn, aggregation_unit="admin3")

# Create a geopandas GeoDataFrame from the GeoJSON
regions_geodataframe = gpd.GeoDataFrame.from_features(regions)

We can now combine the result of the unique_subscriber_counts query with the geography data, and use the Mapbox GL library to create a choropleth showing the distribution of subscribers.

Note: Mapbox requires an access token, which should be set as the environment variable MAPBOX_ACCESS_TOKEN. Note that this is only required for producing the Mapbox visualisations, which is completely separate from FlowKit.

subscribers_per_admin3_geodataframe = (
    regions_geodataframe.join(
        subscribers_per_admin3.set_index("pcod"), on="pcod", how="left"
    )
    .fillna(value={"value": 0})
    .drop(columns=["centroid"])
    .rename(
        columns={
            "pcod": "P-code",
            "value": "Number of subscribers",
        }
    )
)
mapbox_token = os.environ["MAPBOX_ACCESS_TOKEN"]

# Colour scale for legend
color_stops = create_color_stops(
    np.linspace(
        1, subscribers_per_admin3_geodataframe["Number of subscribers"].max(), 9
    ),
    colors="YlGn",
)

modal_locations_viz = mapboxgl.ChoroplethViz(
    subscribers_per_admin3_geodataframe.__geo_interface__,
    access_token=mapbox_token,
    color_property="Number of subscribers",
    color_stops=color_stops,
    opacity=0.8,
    line_color="black",
    line_width=0.5,
    legend_gradient=True,
    legend_layout="horizontal",
    legend_text_numeric_precision=0,
    below_layer="waterway-label",
    center=(84.1, 28.4),
    zoom=5.5,
)

modal_locations_viz.show()