DurhamTrees / durhamtrees.py
Ziyuan111's picture
Update durhamtrees.py
0b8b952 verified
raw
history blame
No virus
9.67 kB
# -*- coding: utf-8 -*-
"""DurhamTrees
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Hvt3Y131OjTl7oGQGS55S_v7-aYu1Yj8
"""
from datasets import DatasetBuilder, DownloadManager, DatasetInfo, SplitGenerator, Split
from datasets.features import Features, Value, ClassLabel
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import csv
import json
import os
from typing import List
import datasets
class DurhamTrees(DatasetBuilder):
_URLS = {
"csv": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy",
"geojson": "https://drive.google.com/uc?export=download&id=1jpFVanNGy7L5tVO-Z_nltbBXKvrcAoDo"
}
VERSION = datasets.Version("1.0.0")
def _info(self):
# Specifies the dataset's features
return DatasetInfo(
description="This dataset contains information about tree planting sites from CSV and GeoJSON files.",
features=Features({
"geometry": Value("string"), # Geometry feature, usually spatial data (GeoJSON format)
"OBJECTID": Value("int64"), # Unique identifier for each record
"streetaddress": Value("string"), # Street address of the tree planting site
"city": Value("string"), # City where the tree planting site is located
"zipcode": Value("int64"), # Zip code of the tree planting site
"facilityid": Value("int64"), # Identifier for the facility
"present": Value("string"), # Presence status, assumed to be string
"genus": Value("string"), # Genus of the tree
"species": Value("string"), # Species of the tree
"commonname": Value("string"), # Common name of the tree
"diameterin": Value("float64"), # Diameter in inches
"heightft": Value("float64"), # Height in feet (changed to "float64")
"condition": Value("string"), # Condition of the tree
"contractwork": Value("string"), # Contract work information
"neighborhood": Value("string"), # Neighborhood where the tree is located
"program": Value("string"), # Program under which the tree was planted
"plantingw": Value("string"), # Width available for planting
"plantingcond": Value("string"), # Planting condition
"underpwerlins": Value("string"), # Whether the tree is under power lines
"GlobalID": Value("string"), # Global identifier
"created_user": Value("string"), # User who created the record
"last_edited_user": Value("string"), # User who last edited the record
"isoprene": Value("float64"), # Isoprene emission rate
"monoterpene": Value("float64"),
"coremoved_ozperyr": Value("float64"), # Carbon monoxide removed, in ounces per year
"coremoved_dolperyr": Value("float64"), # Monetary value of carbon monoxide removal per year
"o3removed_ozperyr": Value("float64"), # Ozone removed, in ounces per year
"o3removed_dolperyr": Value("float64"), # Monetary value of ozone removal per year
"no2removed_ozperyr": Value("float64"), # Nitrogen dioxide removed, in ounces per year
"no2removed_dolperyr": Value("float64"), # Monetary value of nitrogen dioxide removal per year
"so2removed_ozperyr": Value("float64"), # Sulfur dioxide removed, in ounces per year
"so2removed_dolperyr": Value("float64"), # Monetary value of sulfur dioxide removal per year
"pm10removed_ozperyr": Value("float64"), # Particulate matter (10 micrometers or less) removed, in ounces per year
"pm10removed_dolperyr": Value("float64"), # Monetary value of particulate matter removal per year
"pm25removed_ozperyr": Value("float64"), # Particulate matter (2.5 micrometers or less) removed, in ounces per year
"o2production_lbperyr": Value("float64"), # Oxygen production, in pounds per year
"replacevalue_dol": Value("float64"), # Replacement value in dollars
"carbonstorage_lb": Value("float64"), # Carbon storage, in pounds
"carbonstorage_dol": Value("float64"), # Monetary value of carbon storage
"grosscarseq_lbperyr": Value("float64"), # Gross carbon sequestration, in pounds per year
"X": Value("float64"), # X coordinate (longitude if geographic)
"Y": Value("float64"), # Y coordinate (latitude if geographic)
}),
supervised_keys=None,
homepage="https://github.com/AuraMa111?tab=repositories",
citation="Citation for the dataset",
)
def _split_generators(self, dl_manager: datasets.DownloadManager):
urls_to_download = self._URLS # This should now work as _URLS is defined
downloaded_files = dl_manager.download_and_extract(urls_to_download)
return [
datasets.SplitGenerator(name=Split.TRAIN, gen_kwargs={
"csv_path": downloaded_files["csv"],
"geojson_path": downloaded_files["geojson"]
}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"geojson_path": downloaded_files["geojson"]}),
]
def _generate_examples(self, csv_path, geojson_path):
# Log the information about the CSV file being processed
# Load the CSV data into a pandas DataFrame
csv_data = pd.read_csv(csv_path)
# Load the GeoJSON data into a GeoDataFrame
geojson_data = gpd.read_file(geojson_path)
# Merge the CSV data with the GeoJSON data on the 'OBJECTID' column
merged_data = geojson_data.merge(csv_data, on='OBJECTID')
# Drop columns with suffix '_y' that might have been created during the merge
merged_data.drop(columns=[col for col in merged_data if col.endswith('_y')], inplace=True)
# Rename columns to remove suffix '_x'
merged_data.rename(columns=lambda x: x.rstrip('_x'), inplace=True)
# Select the desired columns
columns_to_extract = [ "geometry", # Geometry feature, usually spatial data (GeoJSON format)
"OBJECTID", # Unique identifier for each record
"streetaddress", # Street address of the tree planting site
"city", # City where the tree planting site is located
"zipcode", # Zip code of the tree planting site (changed to string)
"facilityid", # Identifier for the facility
"present", # Presence status, assumed to be string
"genus", # Genus of the tree
"species", # Species of the tree
"commonname", # Common name of the tree
"diameterin", # Diameter in inches
"heightft", # Height in feet (changed to "float64")
"condition", # Condition of the tree
"contractwork", # Contract work information
"neighborhood", # Neighborhood where the tree is located
"program", # Program under which the tree was planted
"plantingw", # Width available for planting
"plantingcond", # Planting condition
"underpwerlins", # Whether the tree is under power lines
"GlobalID", # Global identifier
"created_user", # User who created the record
"last_edited_user", # User who last edited the record
"isoprene", # Isoprene emission rate
"monoterpene",
"coremoved_ozperyr", # Carbon monoxide removed, in ounces per year
"coremoved_dolperyr", # Monetary value of carbon monoxide removal per year
"o3removed_ozperyr", # Ozone removed, in ounces per year
"o3removed_dolperyr", # Monetary value of ozone removal per year
"no2removed_ozperyr", # Nitrogen dioxide removed, in ounces per year
"no2removed_dolperyr", # Monetary value of nitrogen dioxide removal per year
"so2removed_ozperyr", # Sulfur dioxide removed, in ounces per year
"so2removed_dolperyr", # Monetary value of sulfur dioxide removal per year
"pm10removed_ozperyr", # Particulate matter (10 micrometers or less) removed, in ounces per year
"pm10removed_dolperyr", # Monetary value of particulate matter removal per year
"pm25removed_ozperyr", # Particulate matter (2.5 micrometers or less) removed, in ounces per year
"o2production_lbperyr", # Oxygen production, in pounds per year
"replacevalue_dol", # Replacement value in dollars
"carbonstorage_lb", # Carbon storage, in pounds
"carbonstorage_dol", # Monetary value of carbon storage
"grosscarseq_lbperyr", # Gross carbon sequestration, in pounds per year
"X", # X coordinate (longitude if geographic)
"Y"]
# Create the final DataFrame with the selected columns
df = merged_data[columns_to_extract]
# Iterate over each row in the DataFrame and yield it
for index, row in df.iterrows():
# Convert the row to a dictionary, it's more convenient for yielding
yield index, row.to_dict()