NYC_Motor_Vehicle_Collisions_and_Weather_Dataset / NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.py
xx103's picture
Update NYC_Motor_Vehicle_Collisions_and_Weather_Dataset.py
2e2db6d verified
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),
}