File size: 4,586 Bytes
ad2675f
 
afab938
ad2675f
 
 
07013f8
2e2db6d
7ee89dc
07013f8
ad2675f
 
 
 
b754747
ad2675f
 
 
 
 
 
 
 
 
 
 
 
 
3ca93aa
ad2675f
b754747
 
 
 
 
 
ad2675f
 
 
 
 
 
afab938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2675f
 
 
 
 
 
07013f8
b754747
ad2675f
 
 
5b87f31
ad2675f
 
afab938
10e41ee
 
421b86f
283bd7d
1a150d2
 
283bd7d
1a150d2
 
 
 
 
 
 
179232d
 
1a150d2
 
 
 
 
 
 
 
 
283bd7d
421b86f
179232d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import os
import tempfile
import datasets
import boto3
from botocore.config import Config
from botocore import UNSIGNED
import s3fs
from datasets import load_dataset


_CITATION = """\
@misc{ny_motor_vehicle_collisions_weather_dataset,
  title={New York City Motor Vehicle Collisions and Weather Dataset},
  author={Xingzhi Xie},
  year={2024},
  url={https://huggingface.co/datasets/xx103/NYC_Motor_Vehicle_Collisions_and_Weather_Dataset},
}
"""

_DESCRIPTION = """\
This dataset contains information about motor vehicle collisions in New York City, along with associated weather conditions. 
Each record includes details about the crash date, location, collision ID, crash time period, contributing factors, vehicle types, 
the number of injuries and deaths, street name, weather description, and temperature metrics.
"""

_HOMEPAGE = "https://huggingface.co/datasets/xx103/NYC_Motor_Vehicle_Collisions_and_Weather_Dataset"

_LICENSE = ""

# The dataset is stored in AWS S3 bucket
_BUCKET_NAME = 'sta663data1'
_FILE_KEY = 'NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.json'
_LOCAL_FILE_NAME = 'NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.json'


class NYCMotorVehicleCollisionsWeatherDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "crash_date": datasets.Value("string"),
                "borough": datasets.Value("string"),
                "zip_code": datasets.Value("string"),
                "latitude": datasets.Value("float"),
                "longitude": datasets.Value("float"),
                "collision_id": datasets.Value("int32"),
                "crash_time_period": datasets.Value("string"),
                "contributing_factor_vehicles": datasets.Sequence(datasets.Value("string")),
                "vehicle_types": datasets.Sequence(datasets.Value("string")),
                "number_of_injuries": datasets.Value("int32"),
                "number_of_deaths": datasets.Value("int32"),
                "street_name": datasets.Value("string"),
                "street_type": datasets.Value("string"),
                "weather_description": datasets.Value("string"),
                "precipitation": datasets.Value("float"),
                "precipitation_type": datasets.Value("string"),
                "temp_max": datasets.Value("float"),
                "temp_min": datasets.Value("float"),
            }),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
        s3.download_file(_BUCKET_NAME, _FILE_KEY, _LOCAL_FILE_NAME)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": _LOCAL_FILE_NAME},
            ),
        ]

              
   
    def _generate_examples(self, filepath):
        with open(filepath, encoding="utf-8") as f:
            for key, line in enumerate(f):
                row = json.loads(line)
                yield key, {
                    "crash_date": row.get("CRASH DATE", ""),
                    "borough": row.get("BOROUGH", ""),
                    "zip_code": row.get("ZIP CODE", ""),
                    "latitude": row.get("LATITUDE", None),
                    "longitude": row.get("LONGITUDE", None),
                    "collision_id": row.get("COLLISION_ID", None),
                    "crash_time_period": row.get("CRASH TIME PERIOD", ""),
                    "contributing_factor_vehicles": row.get("CONTRIBUTING FACTOR VEHICLES", "").split(", ") if row.get("CONTRIBUTING FACTOR VEHICLES") else [],
                    "vehicle_types": row.get("VEHICLE TYPES", "").split(", ") if row.get("VEHICLE TYPES") else [],
                    "number_of_injuries": row.get("NUMBER OF INJURIES", None),
                    "number_of_deaths": row.get("NUMBER OF DEATHS", None),
                    "street_name": row.get("STREET NAME", ""),
                    "street_type": row.get("STREET TYPE", ""),
                    "weather_description": row.get("WEATHER DESCRIPTION", ""),
                    "precipitation": row.get("PRECIPITATION", None),
                    "precipitation_type": row.get("PRECIPITATION TYPE", ""),
                    "temp_max": row.get("TEMPMAX", None),
                    "temp_min": row.get("TEMPMIN", None),
                }