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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""DurhamTrees
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1czig7JIbqTKp9wNUIRcdMEDF3pFgtxKv
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import geopandas as gpd
|
| 12 |
+
from datasets import (
|
| 13 |
+
GeneratorBasedBuilder, Version, DownloadManager, SplitGenerator, Split,
|
| 14 |
+
Features, Value, BuilderConfig, DatasetInfo
|
| 15 |
+
)
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
import seaborn as sns
|
| 18 |
+
import csv
|
| 19 |
+
import json
|
| 20 |
+
from shapely.geometry import Point
|
| 21 |
+
# URL definitions
|
| 22 |
+
_URLS = {
|
| 23 |
+
"first_domain1": {
|
| 24 |
+
"csv_file": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy",
|
| 25 |
+
"geojson_file": "https://drive.google.com/uc?export=download&id=1cbn7EY7RofXN7c6Ph0eIGFIZowPZ5lKE",
|
| 26 |
+
},
|
| 27 |
+
"first_domain2": {
|
| 28 |
+
"csv_file2": "https://drive.google.com/uc?export=download&id=1RVdaI5dSTPStjhOHO40ypDv2cAQZpi_Y",
|
| 29 |
+
},
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Combined Dataset Class
|
| 33 |
+
class DurhamTrees(GeneratorBasedBuilder):
|
| 34 |
+
VERSION = Version("1.0.0")
|
| 35 |
+
|
| 36 |
+
class MyConfig(BuilderConfig):
|
| 37 |
+
def __init__(self, **kwargs):
|
| 38 |
+
super().__init__(**kwargs)
|
| 39 |
+
|
| 40 |
+
BUILDER_CONFIGS = [
|
| 41 |
+
MyConfig(name="class1_domain1", description="this is combined of csv and geojson"),
|
| 42 |
+
MyConfig(name="class2_domain1", description="this is csv file"),
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
def _info(self):
|
| 46 |
+
return DatasetInfo(
|
| 47 |
+
description="This dataset combines information from both classes, with additional processing for csv_file2.",
|
| 48 |
+
features=Features({
|
| 49 |
+
"feature1_from_class1": Value("string"),
|
| 50 |
+
"geometry":Value("string"),
|
| 51 |
+
"OBJECTID": Value("int64"),
|
| 52 |
+
"X": Value("float64"),
|
| 53 |
+
"Y": Value("float64"),
|
| 54 |
+
"feature1_from_class2": Value("string"),
|
| 55 |
+
"streetaddress": Value("string"),
|
| 56 |
+
"city": Value("string"),
|
| 57 |
+
"facilityid": Value("int64"),
|
| 58 |
+
"present": Value("string"),
|
| 59 |
+
"genus": Value("string"),
|
| 60 |
+
"species": Value("string"),
|
| 61 |
+
"commonname": Value("string"),
|
| 62 |
+
"diameterin": Value("float64"),
|
| 63 |
+
"condition": Value("string"),
|
| 64 |
+
"neighborhood": Value("string"),
|
| 65 |
+
"program": Value("string"),
|
| 66 |
+
"plantingw": Value("string"),
|
| 67 |
+
"plantingcond": Value("string"),
|
| 68 |
+
"underpwerlins": Value("string"),
|
| 69 |
+
"GlobalID": Value("string"),
|
| 70 |
+
"created_user": Value("string"),
|
| 71 |
+
"last_edited_user": Value("string"),
|
| 72 |
+
"isoprene": Value("float64"),
|
| 73 |
+
"monoterpene": Value("float64"),
|
| 74 |
+
"monoterpene_class2": Value("float64"),
|
| 75 |
+
"vocs": Value("float64"),
|
| 76 |
+
"coremoved_ozperyr": Value("float64"),
|
| 77 |
+
"coremoved_dolperyr": Value("float64"),
|
| 78 |
+
"o3removed_ozperyr": Value("float64"),
|
| 79 |
+
"o3removed_dolperyr": Value("float64"),
|
| 80 |
+
"no2removed_ozperyr": Value("float64"),
|
| 81 |
+
"no2removed_dolperyr": Value("float64"),
|
| 82 |
+
"so2removed_ozperyr": Value("float64"),
|
| 83 |
+
"so2removed_dolperyr": Value("float64"),
|
| 84 |
+
"pm10removed_ozperyr": Value("float64"),
|
| 85 |
+
"pm10removed_dolperyr": Value("float64"),
|
| 86 |
+
"pm25removed_ozperyr": Value("float64"),
|
| 87 |
+
"o2production_lbperyr": Value("float64"),
|
| 88 |
+
"replacevalue_dol": Value("float64"),
|
| 89 |
+
"carbonstorage_lb": Value("float64"),
|
| 90 |
+
"carbonstorage_dol": Value("float64"),
|
| 91 |
+
"grosscarseq_lbperyr": Value("float64"),
|
| 92 |
+
"grosscarseq_dolperyr": Value("float64"),
|
| 93 |
+
"avoidrunoff_ft2peryr": Value("float64"),
|
| 94 |
+
"avoidrunoff_dol2peryr": Value("float64"),
|
| 95 |
+
"polremoved_ozperyr": Value("float64"),
|
| 96 |
+
"polremoved_dolperyr": Value("float64"),
|
| 97 |
+
"totannbenefits_dolperyr": Value("float64"),
|
| 98 |
+
"leafarea_sqft": Value("float64"),
|
| 99 |
+
"potevapotran_cuftperyr": Value("float64"),
|
| 100 |
+
"evaporation_cuftperyr": Value("float64"),
|
| 101 |
+
"transpiration_cuftperyr": Value("float64"),
|
| 102 |
+
"h2ointercept_cuftperyr": Value("float64"),
|
| 103 |
+
"carbonavoid_lbperyr": Value("float64"),
|
| 104 |
+
"carbonavoid_dolperyr": Value("float64"),
|
| 105 |
+
"heating_mbtuperyr": Value("float64"),
|
| 106 |
+
"heating_dolperyrmbtu": Value("float64"),
|
| 107 |
+
"heating_kwhperyr": Value("float64"),
|
| 108 |
+
"heating_dolperyrmwh": Value("float64"),
|
| 109 |
+
"cooling_kwhperyr": Value("float64"),
|
| 110 |
+
"cooling_dolperyr": Value("float64"),
|
| 111 |
+
"totalenerg_dolperyr": Value("float64"),
|
| 112 |
+
}),
|
| 113 |
+
supervised_keys=None,
|
| 114 |
+
homepage="https://github.com/AuraMa111?tab=repositories",
|
| 115 |
+
citation="Citation for the combined dataset",
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
def _split_generators(self, dl_manager):
|
| 119 |
+
downloaded_files = dl_manager.download_and_extract(_URLS)
|
| 120 |
+
|
| 121 |
+
return [
|
| 122 |
+
SplitGenerator(
|
| 123 |
+
name=Split.TRAIN,
|
| 124 |
+
gen_kwargs={
|
| 125 |
+
"class1_data_file": downloaded_files["first_domain1"]["csv_file"],
|
| 126 |
+
"class1_geojson_file": downloaded_files["first_domain1"]["geojson_file"],
|
| 127 |
+
"class2_data_file": downloaded_files["first_domain2"]["csv_file2"],
|
| 128 |
+
"split": Split.TRAIN,
|
| 129 |
+
},
|
| 130 |
+
),
|
| 131 |
+
]
|
| 132 |
+
def _generate_examples(self, class1_data_file, class1_geojson_file, class2_data_file, split):
|
| 133 |
+
class1_examples = list(self._generate_examples_from_class1(class1_data_file, class1_geojson_file))
|
| 134 |
+
class2_examples = list(self._generate_examples_from_class2(class2_data_file))
|
| 135 |
+
examples = class1_examples + class2_examples
|
| 136 |
+
df = pd.DataFrame(examples)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
for id_, example in enumerate(examples):
|
| 141 |
+
yield id_, example
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _generate_examples(self, class1_data_file, class1_geojson_file, class2_data_file, split):
|
| 148 |
+
class1_examples = list(self._generate_examples_from_class1(class1_data_file, class1_geojson_file))
|
| 149 |
+
class2_examples = list(self._generate_examples_from_class2(class2_data_file))
|
| 150 |
+
examples = class1_examples + class2_examples
|
| 151 |
+
df = pd.DataFrame(examples)
|
| 152 |
+
|
| 153 |
+
for id_, example in enumerate(examples):
|
| 154 |
+
if not isinstance(example, dict):
|
| 155 |
+
# Convert the example to a dictionary if it's not
|
| 156 |
+
example = {"example": example}
|
| 157 |
+
yield id_, example
|
| 158 |
+
|
| 159 |
+
def _generate_examples_from_class1(self, csv_filepath, geojson_filepath):
|
| 160 |
+
columns_to_extract = ["OBJECTID", "X", "Y"] # Remove "geometry" from columns_to_extract
|
| 161 |
+
csv_data = pd.read_csv(csv_filepath)
|
| 162 |
+
|
| 163 |
+
with open(geojson_filepath, 'r') as file:
|
| 164 |
+
geojson_dict = json.load(file)
|
| 165 |
+
gdf = gpd.GeoDataFrame.from_features(geojson_dict['features'], crs="EPSG:4326") # Specify the CRS if known
|
| 166 |
+
merged_data = gdf.merge(csv_data, on='OBJECTID')
|
| 167 |
+
final_data = merged_data[columns_to_extract + ['geometry']] # Include 'geometry' in the final_data
|
| 168 |
+
for id_, row in final_data.iterrows():
|
| 169 |
+
example = row.to_dict()
|
| 170 |
+
yield id_, example
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def _generate_examples_from_class2(self, csv_filepath2):
|
| 177 |
+
csv_data2 = pd.read_csv(csv_filepath2)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
columns_to_extract = [
|
| 181 |
+
"streetaddress", "city", "facilityid", "present", "genus", "species",
|
| 182 |
+
"commonname", "diameterin", "condition", "neighborhood", "program", "plantingw",
|
| 183 |
+
"plantingcond", "underpwerlins", "GlobalID", "created_user", "last_edited_user", "isoprene", "monoterpene",
|
| 184 |
+
"monoterpene", "vocs", "coremoved_ozperyr", "coremoved_dolperyr",
|
| 185 |
+
"o3removed_ozperyr", "o3removed_dolperyr", "no2removed_ozperyr", "no2removed_dolperyr",
|
| 186 |
+
"so2removed_ozperyr", "so2removed_dolperyr", "pm10removed_ozperyr", "pm10removed_dolperyr",
|
| 187 |
+
"pm25removed_ozperyr", "o2production_lbperyr", "replacevalue_dol", "carbonstorage_lb",
|
| 188 |
+
"carbonstorage_dol", "grosscarseq_lbperyr", "grosscarseq_dolperyr", "polremoved_ozperyr", "polremoved_dolperyr",
|
| 189 |
+
"totannbenefits_dolperyr", "leafarea_sqft", "potevapotran_cuftperyr", "evaporation_cuftperyr",
|
| 190 |
+
"transpiration_cuftperyr", "h2ointercept_cuftperyr",
|
| 191 |
+
"carbonavoid_lbperyr", "carbonavoid_dolperyr", "heating_mbtuperyr",
|
| 192 |
+
"heating_dolperyrmbtu", "heating_kwhperyr", "heating_dolperyrmwh", "cooling_kwhperyr",
|
| 193 |
+
"cooling_dolperyr", "totalenerg_dolperyr",
|
| 194 |
+
]
|
| 195 |
+
|
| 196 |
+
final_data = csv_data2[columns_to_extract]
|
| 197 |
+
for id_, row in final_data.iterrows():
|
| 198 |
+
example = row.to_dict()
|
| 199 |
+
non_empty_example = {key: value for key, value in example.items() if pd.notna(value)}
|
| 200 |
+
yield id_, example
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def _correlation_analysis(self, df):
|
| 205 |
+
correlation_matrix = df.corr()
|
| 206 |
+
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=.5)
|
| 207 |
+
plt.title("Correlation Analysis")
|
| 208 |
+
plt.show()
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
# Create an instance of the DurhamTrees class
|
| 214 |
+
durham_trees_dataset = DurhamTrees(name='class1_domain1')
|
| 215 |
+
|
| 216 |
+
# Build the dataset
|
| 217 |
+
durham_trees_dataset.download_and_prepare()
|
| 218 |
+
|
| 219 |
+
# Access the dataset
|
| 220 |
+
dataset = durham_trees_dataset.as_dataset()
|
| 221 |
+
|
| 222 |
+
# Create an instance of the DurhamTrees class for another configuration
|
| 223 |
+
durham_trees_dataset_another = DurhamTrees(name='class2_domain1')
|
| 224 |
+
|
| 225 |
+
# Build the dataset for the new instance
|
| 226 |
+
durham_trees_dataset_another.download_and_prepare()
|
| 227 |
+
|
| 228 |
+
# Access the dataset for the new instance
|
| 229 |
+
dataset_another = durham_trees_dataset_another.as_dataset()
|