pystats19


Namepystats19 JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryPython package for downloading and formatting the UK's Road Safety Data.
upload_time2024-08-02 23:10:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT License Copyright (c) 2024 Jinshuai Ma Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🚸 py-stats19

**Authors:** 

Xiaowei Gao [📩 Email: ucesxwg@ucl.ac.uk] (SpacetimeLab, UCL, UK)

Jinshuai Ma [📩 Email: j.ma23@lse.ac.uk] (LSE Data Science Institute, UK)

**Supervisors:** 

[Dr. James Haworth](https://profiles.ucl.ac.uk/24884), Associate Professor in Spatio-temporal Analytics, SpaceTimeLab, Department of Civil, Environmental and Geomatic Engineering, UCL

[Prof. Tao Cheng](https://profiles.ucl.ac.uk/10774), Professor in GeoInformatics, SpaceTimeLab, Department of Civil, Environmental and Geomatic Engineering, UCL 




🚸 **py-stats19**  is a Python package developed to support digital twin applications for spatio-temporal urban crash analysis. Inspired by the [**R stats19 package**](https://github.com/ropensci/stats19) package, this package provides a Python version tool to download and format the Road Safety Data from the official [**Road Safety Database**](https://www.data.gov.uk/dataset/cb7ae6f0-4be6-4935-9277-47e5ce24a11f/road-safety-data) published by the Department for Transport, UK, since 1979. Additionally, **py-stats19** enhances the data by incorporating extra `temporal information` and `geometric details`.


The whole data set contains three tables: `casualty`, `collision`, and `vehicle`. The data set is updated annually and contains detailed information about road traffic accidents in Great Britain.



🧰 The current py-stats19 package is under development and testing stages. It is available as a beta version for early access.    

## Installation

Install using pip:

```bash
$ pip install pystats19
```

Alternatively, download and install the latest release from Github, e.g. [pystats19-0.1.0-py3-none-any.whl](https://github.com/Mayazure/py-stats19/releases/download/v0.1.0/pystats19-0.1.0-py3-none-any.whl).

```bash
$ pip install pystats19-0.1.0-py3-none-any.whl
```

## list_files()

`list_files()` can list all available stats19 dataset files, which can be simply filtered by passing `year` and `table` arguments. 

Here, you could specify the table name as `casualty`, `collision`, or `vehicle`. Those files could be merged by the `accident_index` key.

```python
from pystats19.read import list_files

# List all files contain year 2021 data
list_files(year=2021) 
# ['dft-road-casualty-statistics-casualty-1979-latest-published-year.csv',
#  'dft-road-casualty-statistics-casualty-2021.csv',
#  'dft-road-casualty-statistics-casualty-last-5-years.csv',
#  'dft-road-casualty-statistics-collision-1979-latest-published-year.csv',
#  'dft-road-casualty-statistics-collision-2021.csv',
#  'dft-road-casualty-statistics-collision-last-5-years.csv',
#  'dft-road-casualty-statistics-vehicle-1979-latest-published-year.csv',
#  'dft-road-casualty-statistics-vehicle-2021.csv',
#  'dft-road-casualty-statistics-vehicle-e-scooter-2020-Latest-Published-Year.csv',
#  'dft-road-casualty-statistics-vehicle-last-5-years.csv']

# List all files contain year 2021 and table vehicle data
list_files(year=2021, table="vehicle")
# ['dft-road-casualty-statistics-vehicle-1979-latest-published-year.csv',
#  'dft-road-casualty-statistics-vehicle-2021.csv',
#  'dft-road-casualty-statistics-vehicle-last-5-years.csv']
```

## pull_file()

`pull()` requires `filename` parameter, downloading the data file. `filename` should be obtained using `list_files()`.  

Optionally, `data_dir` can specify the location where the file will be stored.

```python
from pystats19.source import pull_file

pull_file('dft-road-casualty-statistics-vehicle-2019.csv', data_dir="./data")
```
### Data directory

Data directory can also be configured globally by setting an environment variable *PYSTATS19_DOWNLOAD_DIRECTORY*

```bash
$ export PYSTATS19_DOWNLOAD_DIRECTORY=~/my_pystats19_data
```

## load()

`load()` loads the data file as a `pandas.DataFrame` or `geopandas.GeoDataFrame`. Set `auto_download=True` to automatically download the file if not exists. 

Optionally, 

set `convert_code_to_label=True` to convert categorical data codes to text labels.  

set `add_temporal_info=True` to format `datetime` and `time` and add additional time information.

set `add_geo_info=True` to add geo information. This will return a `geopandas.GeoDataFrame`.

```python
from pystats19.read import load

load(
    'dft-road-casualty-statistics-collision-2021.csv',
    auto_download=True,
    convert_code_to_label=True,
    add_temporal_info=True,
    add_geo_info=True
)

# Removed 17 records due to missing Latitude or Longitude.
# 
#        accident_index  ...                       geometry
# 0       2021010287148  ...   POINT (521509.659 193079.41)
# 1       2021010287149  ...  POINT (535380.824 180783.228)
# 2       2021010287151  ...  POINT (529702.828 170398.085)
# 3       2021010287155  ...  POINT (525313.658 178385.183)
# 4       2021010287157  ...  POINT (512145.497 171526.072)
# ...               ...  ...                            ...
# 101082  2021991196247  ...  POINT (325545.894 674547.399)
# 101083  2021991196607  ...  POINT (271195.339 558271.954)
# 101084  2021991197944  ...   POINT (357296.909 860766.24)
# 101085  2021991200639  ...  POINT (326935.908 675924.391)
# 101086  2021991201030  ...  POINT (270574.351 556367.939)
# [101070 rows x 39 columns]
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pystats19",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "Jinshuai Ma <j.ma23@lse.ac.uk>, Xiaowei Gao <ucesxwg@ucl.ac.uk>",
    "keywords": null,
    "author": null,
    "author_email": "Jinshuai Ma <j.ma23@lse.ac.uk>, Xiaowei Gao <ucesxwg@ucl.ac.uk>",
    "download_url": "https://files.pythonhosted.org/packages/b2/bf/1baf1806195911d47e3a42bcbd071abf82a8d805d79a2dbaa0809710067e/pystats19-0.1.2.tar.gz",
    "platform": null,
    "description": "# \ud83d\udeb8 py-stats19\r\n\r\n**Authors:** \r\n\r\nXiaowei Gao [\ud83d\udce9 Email: ucesxwg@ucl.ac.uk] (SpacetimeLab, UCL, UK)\r\n\r\nJinshuai Ma [\ud83d\udce9 Email: j.ma23@lse.ac.uk] (LSE Data Science Institute, UK)\r\n\r\n**Supervisors:** \r\n\r\n[Dr. James Haworth](https://profiles.ucl.ac.uk/24884), Associate Professor in Spatio-temporal Analytics, SpaceTimeLab, Department of Civil, Environmental and Geomatic Engineering, UCL\r\n\r\n[Prof. Tao Cheng](https://profiles.ucl.ac.uk/10774), Professor in GeoInformatics, SpaceTimeLab, Department of Civil, Environmental and Geomatic Engineering, UCL \r\n\r\n\r\n\r\n\r\n\ud83d\udeb8 **py-stats19**  is a Python package developed to support digital twin applications for spatio-temporal urban crash analysis. Inspired by the [**R stats19 package**](https://github.com/ropensci/stats19) package, this package provides a Python version tool to download and format the Road Safety Data from the official [**Road Safety Database**](https://www.data.gov.uk/dataset/cb7ae6f0-4be6-4935-9277-47e5ce24a11f/road-safety-data) published by the Department for Transport, UK, since 1979. Additionally, **py-stats19** enhances the data by incorporating extra `temporal information` and `geometric details`.\r\n\r\n\r\nThe whole data set contains three tables: `casualty`, `collision`, and `vehicle`. The data set is updated annually and contains detailed information about road traffic accidents in Great Britain.\r\n\r\n\r\n\r\n\ud83e\uddf0 The current py-stats19 package is under development and testing stages. It is available as a beta version for early access.    \r\n\r\n## Installation\r\n\r\nInstall using pip:\r\n\r\n```bash\r\n$ pip install pystats19\r\n```\r\n\r\nAlternatively, download and install the latest release from Github, e.g. [pystats19-0.1.0-py3-none-any.whl](https://github.com/Mayazure/py-stats19/releases/download/v0.1.0/pystats19-0.1.0-py3-none-any.whl).\r\n\r\n```bash\r\n$ pip install pystats19-0.1.0-py3-none-any.whl\r\n```\r\n\r\n## list_files()\r\n\r\n`list_files()` can list all available stats19 dataset files, which can be simply filtered by passing `year` and `table` arguments. \r\n\r\nHere, you could specify the table name as `casualty`, `collision`, or `vehicle`. Those files could be merged by the `accident_index` key.\r\n\r\n```python\r\nfrom pystats19.read import list_files\r\n\r\n# List all files contain year 2021 data\r\nlist_files(year=2021) \r\n# ['dft-road-casualty-statistics-casualty-1979-latest-published-year.csv',\r\n#  'dft-road-casualty-statistics-casualty-2021.csv',\r\n#  'dft-road-casualty-statistics-casualty-last-5-years.csv',\r\n#  'dft-road-casualty-statistics-collision-1979-latest-published-year.csv',\r\n#  'dft-road-casualty-statistics-collision-2021.csv',\r\n#  'dft-road-casualty-statistics-collision-last-5-years.csv',\r\n#  'dft-road-casualty-statistics-vehicle-1979-latest-published-year.csv',\r\n#  'dft-road-casualty-statistics-vehicle-2021.csv',\r\n#  'dft-road-casualty-statistics-vehicle-e-scooter-2020-Latest-Published-Year.csv',\r\n#  'dft-road-casualty-statistics-vehicle-last-5-years.csv']\r\n\r\n# List all files contain year 2021 and table vehicle data\r\nlist_files(year=2021, table=\"vehicle\")\r\n# ['dft-road-casualty-statistics-vehicle-1979-latest-published-year.csv',\r\n#  'dft-road-casualty-statistics-vehicle-2021.csv',\r\n#  'dft-road-casualty-statistics-vehicle-last-5-years.csv']\r\n```\r\n\r\n## pull_file()\r\n\r\n`pull()` requires `filename` parameter, downloading the data file. `filename` should be obtained using `list_files()`.  \r\n\r\nOptionally, `data_dir` can specify the location where the file will be stored.\r\n\r\n```python\r\nfrom pystats19.source import pull_file\r\n\r\npull_file('dft-road-casualty-statistics-vehicle-2019.csv', data_dir=\"./data\")\r\n```\r\n### Data directory\r\n\r\nData directory can also be configured globally by setting an environment variable *PYSTATS19_DOWNLOAD_DIRECTORY*\r\n\r\n```bash\r\n$ export PYSTATS19_DOWNLOAD_DIRECTORY=~/my_pystats19_data\r\n```\r\n\r\n## load()\r\n\r\n`load()` loads the data file as a `pandas.DataFrame` or `geopandas.GeoDataFrame`. Set `auto_download=True` to automatically download the file if not exists. \r\n\r\nOptionally, \r\n\r\nset `convert_code_to_label=True` to convert categorical data codes to text labels.  \r\n\r\nset `add_temporal_info=True` to format `datetime` and `time` and add additional time information.\r\n\r\nset `add_geo_info=True` to add geo information. This will return a `geopandas.GeoDataFrame`.\r\n\r\n```python\r\nfrom pystats19.read import load\r\n\r\nload(\r\n    'dft-road-casualty-statistics-collision-2021.csv',\r\n    auto_download=True,\r\n    convert_code_to_label=True,\r\n    add_temporal_info=True,\r\n    add_geo_info=True\r\n)\r\n\r\n# Removed 17 records due to missing Latitude or Longitude.\r\n# \r\n#        accident_index  ...                       geometry\r\n# 0       2021010287148  ...   POINT (521509.659 193079.41)\r\n# 1       2021010287149  ...  POINT (535380.824 180783.228)\r\n# 2       2021010287151  ...  POINT (529702.828 170398.085)\r\n# 3       2021010287155  ...  POINT (525313.658 178385.183)\r\n# 4       2021010287157  ...  POINT (512145.497 171526.072)\r\n# ...               ...  ...                            ...\r\n# 101082  2021991196247  ...  POINT (325545.894 674547.399)\r\n# 101083  2021991196607  ...  POINT (271195.339 558271.954)\r\n# 101084  2021991197944  ...   POINT (357296.909 860766.24)\r\n# 101085  2021991200639  ...  POINT (326935.908 675924.391)\r\n# 101086  2021991201030  ...  POINT (270574.351 556367.939)\r\n# [101070 rows x 39 columns]\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Jinshuai Ma  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Python package for downloading and formatting the UK's Road Safety Data.",
    "version": "0.1.2",
    "project_urls": {
        "Repository": "https://github.com/Mayazure/py-stats19"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6a7202b7f456b9dca554b51d551df9dfad9456be55456d513946ded94e9807ac",
                "md5": "3db4b0d370826b46d8d7e1b732868531",
                "sha256": "29a9ce3300a63c723bf864962b75a3ca73fa7c7de273e0cf688630aad261c176"
            },
            "downloads": -1,
            "filename": "pystats19-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3db4b0d370826b46d8d7e1b732868531",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 33127,
            "upload_time": "2024-08-02T23:10:49",
            "upload_time_iso_8601": "2024-08-02T23:10:49.006500Z",
            "url": "https://files.pythonhosted.org/packages/6a/72/02b7f456b9dca554b51d551df9dfad9456be55456d513946ded94e9807ac/pystats19-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b2bf1baf1806195911d47e3a42bcbd071abf82a8d805d79a2dbaa0809710067e",
                "md5": "c421ae7b0f73879e688876dec214beaa",
                "sha256": "75a5d290de3a44f575e6881e59b0da77e1b8ef066a74987cc751c448c86a5c2f"
            },
            "downloads": -1,
            "filename": "pystats19-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "c421ae7b0f73879e688876dec214beaa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 34697,
            "upload_time": "2024-08-02T23:10:50",
            "upload_time_iso_8601": "2024-08-02T23:10:50.144858Z",
            "url": "https://files.pythonhosted.org/packages/b2/bf/1baf1806195911d47e3a42bcbd071abf82a8d805d79a2dbaa0809710067e/pystats19-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-02 23:10:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Mayazure",
    "github_project": "py-stats19",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pystats19"
}
        
Elapsed time: 0.39742s