Create NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.py
Browse files
NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from typing import List
|
4 |
+
import pandas as pd
|
5 |
+
import datasets
|
6 |
+
import boto3
|
7 |
+
from botocore import UNSIGNED
|
8 |
+
from botocore.config import Config
|
9 |
+
|
10 |
+
|
11 |
+
_CITATION = """\
|
12 |
+
@misc{ny_motor_vehicle_collisions_weather_dataset,
|
13 |
+
title={New York City Motor Vehicle Collisions and Weather Dataset},
|
14 |
+
author={XX103},
|
15 |
+
year={2024},
|
16 |
+
url={https://huggingface.co/datasets/xx103/NYC_Motor_Vehicle_Collisions_and_Weather_Dataset},
|
17 |
+
}
|
18 |
+
"""
|
19 |
+
|
20 |
+
_DESCRIPTION = """\
|
21 |
+
This dataset contains information about motor vehicle collisions in New York City, along with associated weather conditions.
|
22 |
+
Each record includes details about the crash date, location, collision ID, crash time period, contributing factors, vehicle types,
|
23 |
+
the number of injuries and deaths, street name, weather description, and temperature metrics.
|
24 |
+
"""
|
25 |
+
|
26 |
+
_HOMEPAGE = "https://huggingface.co/datasets/xx103/NYC_Motor_Vehicle_Collisions_and_Weather_Dataset"
|
27 |
+
|
28 |
+
_LICENSE = "Apache License 2.0"
|
29 |
+
|
30 |
+
class NYCMotorVehicleCollisionsWeatherDataset(datasets.GeneratorBasedBuilder):
|
31 |
+
VERSION = datasets.Version("1.0.0")
|
32 |
+
|
33 |
+
def _info(self):
|
34 |
+
return datasets.DatasetInfo(
|
35 |
+
description=_DESCRIPTION,
|
36 |
+
features=datasets.Features(
|
37 |
+
{
|
38 |
+
"crash_date": datasets.Value("string"),
|
39 |
+
"borough": datasets.Value("string"),
|
40 |
+
"zip_code": datasets.Value("string"),
|
41 |
+
"latitude": datasets.Value("float"),
|
42 |
+
"longitude": datasets.Value("float"),
|
43 |
+
"collision_id": datasets.Value("int32"),
|
44 |
+
"crash_time_period": datasets.Value("string"),
|
45 |
+
"contributing_factor_vehicles": datasets.Sequence(datasets.Value("string")),
|
46 |
+
"vehicle_types": datasets.Sequence(datasets.Value("string")),
|
47 |
+
"number_of_injuries": datasets.Value("int32"),
|
48 |
+
"number_of_deaths": datasets.Value("int32"),
|
49 |
+
"street_name": datasets.Value("string"),
|
50 |
+
"street_type": datasets.Value("string"),
|
51 |
+
"weather_description": datasets.Value("string"),
|
52 |
+
"precipitation": datasets.Value("float"),
|
53 |
+
"precipitation_type": datasets.Value("string"),
|
54 |
+
"temp_max": datasets.Value("float"),
|
55 |
+
"temp_min": datasets.Value("float"),
|
56 |
+
}
|
57 |
+
),
|
58 |
+
homepage=_HOMEPAGE,
|
59 |
+
license=_LICENSE,
|
60 |
+
citation=_CITATION,
|
61 |
+
)
|
62 |
+
|
63 |
+
def _split_generators(self, dl_manager):
|
64 |
+
# Initialize the S3 client for anonymous access
|
65 |
+
s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
|
66 |
+
bucket_name = 'sta663data1'
|
67 |
+
file_key = 'NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.json'
|
68 |
+
local_file_name = dl_manager.download(f"s3://{bucket_name}/{file_key}")
|
69 |
+
|
70 |
+
return [
|
71 |
+
datasets.SplitGenerator(
|
72 |
+
name=datasets.Split.TRAIN,
|
73 |
+
gen_kwargs={
|
74 |
+
"filepath": local_file_name,
|
75 |
+
"split": "train",
|
76 |
+
},
|
77 |
+
),
|
78 |
+
]
|
79 |
+
|
80 |
+
def _generate_examples(self, filepath, split):
|
81 |
+
with open(filepath, encoding="utf-8") as f:
|
82 |
+
data = json.load(f)
|
83 |
+
for key, row in enumerate(data):
|
84 |
+
yield key, {
|
85 |
+
"crash_date": row["crash_date"],
|
86 |
+
"borough": row["borough"],
|
87 |
+
"zip_code": row["zip_code"],
|
88 |
+
"latitude": row["latitude"],
|
89 |
+
"longitude": row["longitude"],
|
90 |
+
"collision_id": row["collision_id"],
|
91 |
+
"crash_time_period": row["crash_time_period"],
|
92 |
+
"contributing_factor_vehicles": row["contributing_factor_vehicles"],
|
93 |
+
"vehicle_types": row["vehicle_types"],
|
94 |
+
"number_of_injuries": row["number_of_injuries"],
|
95 |
+
"number_of_deaths": row["number_of_deaths"],
|
96 |
+
"street_name": row["street_name"],
|
97 |
+
"street_type": row["street_type"],
|
98 |
+
"weather_description": row["weather_description"],
|
99 |
+
"precipitation": row["precipitation"],
|
100 |
+
"precipitation_type": row["precipitation_type"],
|
101 |
+
"temp_max": row["temp_max"],
|
102 |
+
"temp_min": row["temp_min"],
|
103 |
+
}
|