NYC_Motor_Vehicle_Collisions_and_Weather_Dataset
/
NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.py
import json | |
import os | |
import tempfile | |
import datasets | |
import boto3 | |
from botocore.config import Config | |
from botocore import UNSIGNED | |
_CITATION = """\ | |
@misc{ny_motor_vehicle_collisions_weather_dataset, | |
title={New York City Motor Vehicle Collisions and Weather Dataset}, | |
author={XX103}, | |
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 = "Apache License 2.0" | |
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)) | |
bucket_name = 'sta663data1' | |
file_key = 'NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.json' | |
local_file_name = 'NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.json' | |
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(", "), | |
"vehicle_types": row.get("VEHICLE TYPES", "").split(", "), | |
"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), | |
} | |