cityiq package

Submodules

cityiq.api module

class cityiq.api.CacheFile(cache_path, access_object, fetch_func=None, event_type=None, dt=None, end_time=None, group=None, format='json')[source]

Bases: object

Represents a cached file of records for one location or asset, one type of event, and one day. Or, if the date and event type are omitted, just the information about an asset or location

delete()[source]
exists()[source]
classmethod object_prefix(obj, event_type)[source]
property path

The filesystem path to the cache file

read()[source]
run()[source]
write(result)[source]

Write Json data, or a CSV if the result valule is a dataframe

class cityiq.api.CityIq(config=None, cache_metadata=True, **kwargs)[source]

Bases: object

asset_url_suffix = '/api/v2/metadata/assets/{uid}'
property assets

Return all system assets

assets_by_event(event_types)[source]
assets_search_suffix = '/api/v2/metadata/assets/search'
cache_events(obj, event_type, start_time, end_time, bbox=None)[source]
property cameras

Return camera assets

clear_meta_cache()[source]
convert_time(t)[source]

Convert a variety of time formats into the millisecond format used by the CityIQ interface. Converts naieve times to the configured timezone

property em_sensors

Return some other kind of sensor. Electro-magnetic?

property env_sensors

Return environmental sensors

events_url_suffix = '/api/v2/event/locations/events'
get_asset(asset_uid, use_cache=True)[source]
get_assets(device_type=None, zone=None, bbox=None, use_cache=None)[source]
get_cache_files(objects, event_types, start_time, end_time)[source]
get_cached_events(obj, event_type, start_time, end_time, bbox=None)[source]
get_location(location_uid, use_cache=True)[source]

Get a single location by its uid

get_locations(location_type=None, zone=None, bbox=None)[source]

Get all locations, options for a zone or bounding box

get_meta_cache(key)[source]
get_meta_pages(url, params=None, query=None, zone=None, bbox=None)[source]
http_get(url, zone=None, params=None, *args, **kwargs)[source]

Get the events of one type :param start_time: :param span: time span in seconds :param event_type: :param tz_name: :return:

load_locations(path)[source]
location_url_suffix = '/api/v2/metadata/locations/{uid}'
property locations

Return all locations

locations_by_event(event_types)[source]
property locations_dataframe
locations_search_suffix = '/api/v2/metadata/locations/search'
make_tasks(objects, events, start_time, end_time, task_class=None)[source]

Fetch, and cache, events requests for a set of assets or locations

property mics

Return microphone assets

property nodes

Return all nodes

object_sub_dir = 'object'
property parking_zones
process_url(url, params)[source]
request_headers(zone=None)[source]
run_async(tasks, workers=4)[source]

Run a set of tasks, created with make_tasks, with multiple workers

run_sync(tasks)[source]

Run all of the tasks, one at a time, and return the combined results

set_meta_cache(key, value)[source]
property token
property total_bounds

Return a bounding box for the system from all of the assets. This will be affected by the bbox set in the config, so it should usually be smaller than the one in the config

Order is: lat_max, lon_min, lat_min, lon_max

property total_bounds_str

Total bounds bounding box, in the form of the city_iq config

property traffic_lanes
property walkways
class cityiq.api.CityIqObject(client, data, use_cache=True)[source]

Bases: object

as_dict(wkt=False)[source]
cache_file(fetch_func=None, event_type=None, dt=None, group=None, format='csv')[source]
property events_url

Return the URL for fetching events, called from get_events() in the base class.

property geometry

Return a Shapely polygon for the coordinates

get_events(event_type, start_time, end_time=None)[source]
update()[source]

Make an uncached call to the API to replace the data in this object

write()[source]

Write data to the cache

class cityiq.api.Event(client, data, use_cache=True)[source]

Bases: cityiq.api.CityIqObject

types = ['PKIN', 'PKOUT', 'PEDEVT', 'TFEVT', 'TEMPERATURE', 'PRESSURE', 'ORIENTATION', 'METROLOGY', 'HUMIDITY', 'ENERGY_TIMESERIES', 'ENERGY_ALERT']
cityiq.api.to_date(t)[source]

cityiq.asset module

class cityiq.asset.Asset(client, data, use_cache=True)[source]

Bases: cityiq.api.CityIqObject

property children

Sub assets of this asset

children_url_suffix = '/api/v2/metadata/assets/{}/subAssets'
dclass_map = {'CAMERA': <class 'cityiq.asset.CameraAsset'>, 'EM_SENSOR': <class 'cityiq.asset.EmSensorAsset'>, 'ENV_SENSOR': <class 'cityiq.asset.EnvSensorAsset'>, 'MIC': <class 'cityiq.asset.MicSensorAsset'>, 'NODE': <class 'cityiq.asset.NodeAsset'>}
property detail

Asset details, which appears to be just re-fetching the object data. On some systems it may return additional data.

Most importantly, the read is not cached, so it can be used to return the online/offline state of an asset without a time delay.

detail_url_suffix = '/api/v2/metadata/assets/{}'
event_type(type)[source]

Return a specific event type record

property event_types

Return event types records

events_url_suffix = '/api/v2/event/assets/{uid}/events'
generate_events(event_type, start_time, end_time=None)[source]
get_events(event_type, start_time, end_time=None)[source]
has_events(events)[source]
property lat
property locations

Locations at this asset

locations_url_suffix = '/api/v2/metadata/assets/{}/locations'
property lon
object_sub_dir = 'asset'
property parent
row_header = ['assetUid', 'assetType', 'parentAssetUid', 'mediaType', 'events', 'geometry']
types = ['NODE', 'EM_SENSOR', 'MIC', 'ENV_SENSOR', 'CAMERA']
property uid
uid_key = 'assetUid'
class cityiq.asset.CameraAsset(client, data, use_cache=True)[source]

Bases: cityiq.asset.Asset

class cityiq.asset.EmSensorAsset(client, data, use_cache=True)[source]

Bases: cityiq.asset.Asset

class cityiq.asset.EnvSensorAsset(client, data, use_cache=True)[source]

Bases: cityiq.asset.Asset

class cityiq.asset.MicSensorAsset(client, data, use_cache=True)[source]

Bases: cityiq.asset.Asset

class cityiq.asset.NodeAsset(client, data, use_cache=True)[source]

Bases: cityiq.asset.Asset

cityiq.clean_events module

Clean events, removing duplicates

cityiq.clean_events.augment_df(df)[source]
cityiq.clean_events.clean_events(s, use_tqdm=True, locations=None)[source]

Given an event scraper that has has

cityiq.clean_events.dedup_1s(df)[source]

Deduplicate by dropping events of sametype that occur in 1s window.

cityiq.clean_events.dedup_rolling(df)[source]

Entry point for rolling count debouncing

cityiq.clean_events.plot_loc_norming(t)[source]
cityiq.clean_events.re_norm_location(df)[source]

cityiq.config module

class cityiq.config.Config(path=None, **kwargs)[source]

Bases: object

property dict
property which

cityiq.exceptions module

exception cityiq.exceptions.AuthenticationError[source]

Bases: cityiq.exceptions.CityIqError

exception cityiq.exceptions.CityIqError[source]

Bases: Exception

exception cityiq.exceptions.ConfigurationError[source]

Bases: cityiq.exceptions.CityIqError

exception cityiq.exceptions.TimeError[source]

Bases: cityiq.exceptions.CityIqError

cityiq.location module

class cityiq.location.Location(client, data, use_cache=True)[source]

Bases: cityiq.api.CityIqObject

property assets

Assets at this location

assets_url_suffix = '/api/v2/metadata/locations/{}/assets'
dclass_map = {'PARKING_ZONE': <class 'cityiq.location.ParkingZoneLocation'>, 'TRAFFIC_LANE': <class 'cityiq.location.TrafficLaneLocation'>, 'WALKWAY': <class 'cityiq.location.WalkwayLocation'>}
property detail

Return a new Location after making a non-cached call to the API. The response value is cached, so this will update the cache.

detail_url_suffix = '/api/v2/metadata/locations/{}'
events_url_suffix = '/api/v2/event/locations/{uid}/events'
object_sub_dir = 'location'
property row

Return most important fields in a row format

row_header = ['locationUid', 'locationType', 'parentLocationUid', 'geometry']
types = ['WALKWAY', 'TRAFFIC_LANE', 'PARKING_ZONE']
property uid
uid_key = 'locationUid'
class cityiq.location.ParkingZoneLocation(client, data, use_cache=True)[source]

Bases: cityiq.location.Location

class cityiq.location.TrafficLaneLocation(client, data, use_cache=True)[source]

Bases: cityiq.location.Location

class cityiq.location.WalkwayLocation(client, data, use_cache=True)[source]

Bases: cityiq.location.Location

cityiq.task module

class cityiq.task.DownloadTask(access_object: cityiq.api.CityIqObject, event_type, start_date: datetime.date, end_date: datetime.date)[source]

Bases: cityiq.task.EventTask

classmethod make_tasks(objects: Sequence[cityiq.api.CityIqObject], event_types: Sequence[str], start_date: datetime.date, end_date: datetime.date)[source]
run()[source]
class cityiq.task.EventTask(access_object: cityiq.api.CityIqObject, event_type, start_date: datetime.date, end_date: datetime.date)[source]

Bases: object

Base class for operations on a single object, event type and day. These tasks can be run in parallel and be subclassed to provide specific operations.

classmethod make_tasks(objects: Sequence[cityiq.api.CityIqObject], event_types: Sequence[str], start_date: datetime.date, end_date: datetime.date)[source]
run()[source]
class cityiq.task.EventWorker(client, events, queue)[source]

Bases: threading.Thread

Thread worker for websocket events

static events_async(client, events=['PKIN', 'PKOUT'])[source]

Use the websocket to get events. The websocket is run in a thread, and this function is a generator that returns results.

run()None[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

cityiq.task.ensure_date(v)[source]
cityiq.task.generate_days(start_time, end_time, include_end=False)[source]

Generate day ranges for the request

Parameters
  • start_time

  • end_time

  • include_end – If True, range will include end date, if False, it will stop one day before

Returns

cityiq.task.generate_months(start_time, end_time, include_end=False)[source]

Generate month ranges from the start time to the end time

Parameters
  • start_time

  • end_time

  • include_end – If True, range will include end date, if False, it will stop one day before

Returns

cityiq.task.grouper(3, 'abcdefg', 'x') --> ('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')[source]
cityiq.task.request_ranges(start_date, end_date, extant)[source]

cityiq.token module

cityiq.token.get_cached_token(cache_path, uaa, client, secret)[source]

Return a cached access token from the CityIQ service. Returns just the token. Use _get_token() to get the full response

Parameters
  • cache_path – Directory where cached token will be stored, if a directory or token name if a file

  • uaa – Url to the user authentication service

  • client

  • secret

Returns

A token string

If a directory is specified in cache_dir, the file name will be ‘cityiq-token-<crc>.json’, with the CRC32 of the source url. The token will be expired after 8 hours.

cityiq.token.get_token(uaa, client, secret)[source]

Get an access token from the CityIQ service. Returns just the token. Use _get_token() to get the full response

Parameters
  • uaa – Url to the user authentication service

  • client

  • secret

Returns

cityiq.util module

cityiq.util.current_time()[source]

Return the epoch time in miliseconds

cityiq.util.event_to_location_type(event_type)[source]
cityiq.util.event_to_zone(config, event_type)[source]
cityiq.util.event_type_to_location_type(event_type)[source]
cityiq.util.grouper(n, iterable)[source]
cityiq.util.json_serial(obj)[source]

JSON serializer for objects not serializable by default json code

cityiq.util.local_to_timestamp(dt, tz)[source]

Convert a local time, assumed in timezone tz, into a milisecond UTC timestamp

cityiq.util.log_message(r)[source]

Debugging log message for requests

cityiq.util.make_csv_file_name(cache, locationUid, event_type)[source]
cityiq.util.run_async(items, workers=4)[source]

Run a function in multiple threads

cityiq.util.timestamp_to_local(ts, tz)[source]

Convert a UTC timestamp in milliseconds to a local time, in the timezone tz, with no timezone

Module contents