mobilkit.tools module

mobilkit.tools.checkScore(results_clusters, score='scores')

Function to plot the score of clustering.

Parameters:
  • results_clusters (dict) – As returned by mobilkit.tools.computeClusters.

  • score (str, optional) – One of "scores", "inconsistents". If scores, the best split is at a local maximum of the score.

Returns:

ax – The ax of the figure.

Return type:

axis

mobilkit.tools.computeClusters(results, signal, metric='cosine', nClusters=[2, 3, 4, 5, 6, 7, 8, 9])

Function to compute the clusters.

Parameters:
  • results (dict) – As returned from mobilkit.spatial.computeResiduals.

  • signal (str) – One of 'mean', 'zscore', 'residual', the indicator to use to cluster the areas.

  • metric (str, optional) – One of 'cosine', 'euclidean', the metric used to compute the linkage matrix. Default to cosine.

  • nClusters (list, optinal) – The list or set of number of clusters to try.

Returns:

results_clusters – A dict with the results to be used for plotting and inspection.

Return type:

dict

mobilkit.tools.computeGDFbounds(gdf: GeoDataFrame) dict

Computes the bounds of a geopandas.GeoDataFrame gdf and returns a dictionary with the min(x,y) and max(x,y) keys and their value as values (minx is the minimum longitude).

Parameters:

gdf (geopandas.GeoDataFrame) – The GeoDataFrame with at least the geometry column.

Returns:

bounds – The mapping between the min/max x and y and their values.

Return type:

dict

mobilkit.tools.osrm_time_trip(latlon_start, latlon_end, osrm_url, what='duration', max_trip_duration=10800, max_trip_distance=100.0)

Queries an OSRM backend server on osrm_url for the travel time or distance (depending on what) between latlon_start and latlon_end.

Parameters:
  • latlon_start, latlon_end (list or np.array) – The latitude and longitude of the initial and final trip point (in WGS84, EPSG:4326 crs).

  • osrm_url (str) – A valid table endpoint url of a OSRM-bakend service (like http://localhost:5000/table/v1/car/).

  • what (str, optional) – Whether you want the duration or distance or duration,distance of the trip.

  • max_trip_duration (float, optional) – The maximum trip duration in seconds that will be put in place of NaNs or invalid trips.

  • max_trip_distance (float, optional) – The maximum trip distance in km that will be put in place of NaNs or invalid trips.

Returns:

duration – The duration (length) of the trip in seconds (meters) or the two (duration,distance) if what==duration,distance.

Return type:

float or tuple

mobilkit.tools.plotClustersMap(gdf, results_clusters, mappings, nClusts=5)

Function to plot the similarity matrix between areas.

Parameters:
  • gdf (geopandas.GeoDataFrame) – The GeoDataFrame with at least the tile_ID and geometry columns.

  • results_clusters (dict) – As returned by mobilkit.tools.computeClusters.

  • mappings (dict) – The mappings as returned by mobilkit.spatial.computeResiduals.

  • nClusts (int, optional) – The number of clusters to use.

Returns:

  • gdf (geopandas.GeoDataFrame) – The original GeoDataFrame with an additional column 'cluster' containing the cluster assigned to that area.

  • ax (axis) – The ax of the figure.

mobilkit.tools.plotCommunities(results_clusters, nClusts)

Function to plot the similarity matrix between areas.

Parameters:
Returns:

ax – The ax of the figure.

Return type:

axis

mobilkit.tools.userHomeWorkTravelTimeOSRM(r, osrm_url, direction='hw', what='duration', max_trip_duration_h=4, max_trip_distance_km=150)

Queries an OSRM backend server on osrm_url for the travel time or distance (depending on what) between the home and work location for the user in the row r.

Parameters:
  • r (dict or row of a pd.Dataframe) – The row with the home and work latitude and longitude (in WGS84, EPSG:4326 crs) in the lat_home format, as returned by mobilkit.stats.userHomeWorkLocation.

  • osrm_url (str) – A valid table endpoint url of a OSRM-bakend service (like http://localhost:5000/table/v1/car/).

  • direction (str, optional) – Whether you want the home->work (“hw”) or work->home (“wh”) time of the trip.

  • what (str, optional) – Whether you want the duration, distance or duration,distance of the trip.

  • max_trip_duration_h (float, optional) – The maximum trip duration in hours (or km if asking for distance) that will be put in place of NaNs or invalid trips.

Returns:

duration – The duration (length) of the trip in seconds (meters) or the two (duration,distance) if what==duration,distance.

Return type:

float

mobilkit.tools.visualizeClustersProfiles(results_clusters, nClusts=5, showMean=False, showMedian=True, showCurves=True, together=False)

Function to plot the temporal profiles of clustering.

Parameters:
  • results_clusters (dict) – As returned by mobilkit.tools.computeClusters.

  • nClusts (int, optional) – The number of clusters to use.

  • showMean (bool, optional) – Whether or not to show the mean curve of the cluster.

  • showMedian (bool, optional) – Whether or not to show the median curve of the cluster. If both median and mean are to be plotted, only the median will be shown.

  • showCurves (bool, optional) – Whether or not to show the curves of the cluster in transparency.

  • together (bool, optional) – Whether to plot all the cluster in one plot. Default False.

Returns:

ax – The ax of the figure.

Return type:

axis