crime_hotspots_uk package

Submodules

crime_hotspots_uk.constants module

crime_hotspots_uk.crimes module

class crime_hotspots_uk.crimes.Data(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>)[source]

Bases: crime_hotspots_uk.data.Root

This class is used to hold a dataframe of constituincy boundaries and any relevant data. Any data pertaining to a perticular constituincy including demographics or political representation should be implemented here.

__init__(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>)[source]

This function initiates the class

It does this by downloading the location boundaries and crime type options from the UK Police API.

Parameters

usage (string, optional) – Wether to get crime data or stop and search data from the police API. If ‘crime’ is passed the class will use the Street level crimes method, if search is passed it will use the Stop and searches by area method.

Raises

AssertionError – This error is raised if the string passed to usage is not ‘crime’ or ‘search’.

crime_hotspots_uk.data module

This module is used to download and analyse data from the data.police.uk API.

class crime_hotspots_uk.data.Root(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>, usage='crime')[source]

Bases: object

This class handles all downloading and processing of the data.

__init__(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>, usage='crime')[source]

This function initiates the class

It does this by downloading the location boundaries and crime type options from the UK Police API.

Parameters

usage (string, optional) –

Wether to get crime data or stop and search data from the police API. If ‘crime’ is passed the class will use the Street level crimes method, if search is passed it will use the Stop and searches by area method.

Raises

AssertionError – This error is raised if the string passed to usage is not ‘crime’ or ‘search’.

cache_data()[source]
create_mappings()[source]
export(name, file_type)[source]
fishnet(geometry, threshold)[source]

Divide a shapely geometry into small sections

Note

This function is not currently used and is not doccumented

fix_locations()[source]

Fix locations in the self.all_crimes dataframe

This is needed because some of the location names used by the police are used for multiple locations. For instance On or near bus stop doesn’t tell us which bus stop it was near. This function takes the provided latitude and longitude coordinates and identifies which locale with a definitive name in the local area is closest.

Raises

AssertionError – This error is raised if a location name can’t be correctly mapped to a street because there was no points close enough.

fix_polygons(polygon)[source]
get_crimes(coords, name)[source]

Download all crimes of a specific type within a boundary

Parameters
  • coords (string) – A two deep list containing latitude and longitude coordinate pairs

  • name – The name of the area the data is for, this name will be appended as a column to the output dataframe to ensure that each area can be selected individualy

Returns

Returns either a pandas dataframe if the data retireval was successfull or NONE if it wasn’t

Return type

pandas.dataframe

get_data(crime_type)[source]

Download data for a specified crime type.

This is also used to download stop and search data. To do so make sure self.usage has been set previously.

Parameters

crime_type (string, required) – The crime type to download the data for. It must be one of the types listed in self.crime_types, it should be the readable name (without any -/_) The full explanation of what each category is can be infered from the Police website <https://www.police.uk/pu/contact-the-police/what-and-how-to-report/what-report/>_ # noqa e501

Returns

Will return true if it managed to successfully download and validate the data. If it fails to it will return false.

Return type

bool

hotspots_graph(top, location, location_type=['All'])[source]

Draw a bargraph of the rates of assult at the top hotspots

Parameters
  • top (int) – how many hotspots to plot, for instance 10 would show the top 10 hotspots. IF this is set to none all hotspots will be graphed.

  • location (string) – Where the title of the graph should say the data is from

  • location_type (list (optional)) – Type of location to make the graph for, must be a list of location types, each entry must be either Street or value in the ignore list in constants.py. You can also pass All to select all crimes. The default value is All

import_cache(location_type, area, month, category=None)[source]
url_gen(location, date)[source]

Generate the url for API requests

Parameters
  • location (String) – String of Lat/Lon coordinates marking out a boundary

  • date – The month to get the data for in format yyyy-mm

Type

date String

exception crime_hotspots_uk.data.http_error_code(code, url)[source]

Bases: Exception

Exception raised when a function that should only be run after the crime data location data has been fixed to ensure readable place names are used instead of generic identifiers.

__init__(code, url)[source]

Initialize self. See help(type(self)) for accurate signature.

exception crime_hotspots_uk.data.locations_not_fixed_yet(message='Locations have not been fixed yet')[source]

Bases: Exception

Exception raised when a function that should only be run after the crime data location data has been fixed to ensure readable place names are used instead of generic identifiers.

__init__(message='Locations have not been fixed yet')[source]

Initialize self. See help(type(self)) for accurate signature.

crime_hotspots_uk.searches module

class crime_hotspots_uk.searches.Data(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>)[source]

Bases: crime_hotspots_uk.data.Root

This class is used to hold a dataframe of constituincy boundaries and any relevant data. Any data pertaining to a perticular constituincy including demographics or political representation should be implemented here.

__init__(name, location_names, location_type=<class 'crime_hotspots_uk.locations.constituincy.Constituincy'>)[source]

This function initiates the class

It does this by downloading the location boundaries and crime type options from the UK Police API.

Parameters

usage (string, optional) –

Wether to get crime data or stop and search data from the police API. If ‘crime’ is passed the class will use the Street level crimes method, if search is passed it will use the Stop and searches by area method.

Raises

AssertionError – This error is raised if the string passed to usage is not ‘crime’ or ‘search’.

cache_data()[source]
get_data()[source]

Download data for a specified crime type.

This is also used to download stop and search data. To do so make sure self.usage has been set previously.

Parameters

crime_type (string, required) – The crime type to download the data for. It must be one of the types listed in self.crime_types, it should be the readable name (without any -/_) The full explanation of what each category is can be infered from the Police website <https://www.police.uk/pu/contact-the-police/what-and-how-to-report/what-report/>_ # noqa e501

Returns

Will return true if it managed to successfully download and validate the data. If it fails to it will return false.

Return type

bool

import_cache(location_type, area, month, category=None)[source]

Module contents