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