Ziyuan111 commited on
Commit
d7f18e1
1 Parent(s): cdfebee

Upload durhamtrees.py

Browse files
Files changed (1) hide show
  1. durhamtrees.py +299 -0
durhamtrees.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/1C4L9gZ_hkydWda4rUgNuU-GNJa9fBV-b
8
+ """
9
+
10
+ Hugging Face's logo
11
+ Hugging Face
12
+
13
+ Models
14
+ Datasets
15
+ Spaces
16
+ Posts
17
+ Docs
18
+ Pricing
19
+
20
+
21
+
22
+
23
+ Datasets:
24
+
25
+ Ziyuan111
26
+ /
27
+ DurhamTrees
28
+
29
+ like
30
+ 0
31
+
32
+ Tasks:
33
+
34
+ Token Classification
35
+
36
+ Table Question Answering
37
+ Languages:
38
+
39
+ English
40
+ Size Categories:
41
+ 1M<n<10M
42
+ Tags:
43
+
44
+ Croissant
45
+ License:
46
+
47
+ apache-2.0
48
+ Dataset card
49
+ Viewer
50
+
51
+ Files and versions
52
+ Community
53
+ 1
54
+ Settings
55
+ DurhamTrees
56
+ /
57
+ durhamtrees.py
58
+ Ziyuan111's picture
59
+ Ziyuan111
60
+ Upload durhamtrees.py
61
+ 9acf5e8
62
+ VERIFIED
63
+ 10 minutes ago
64
+ raw
65
+ history
66
+ blame
67
+ edit
68
+ delete
69
+ No virus
70
+ 10.9 kB
71
+ # -*- coding: utf-8 -*-
72
+ """DurhamTrees
73
+ Automatically generated by Colaboratory.
74
+ Original file is located at
75
+ https://colab.research.google.com/drive/1C4L9gZ_hkydWda4rUgNuU-GNJa9fBV-b
76
+ """
77
+
78
+ # -*- coding: utf-8 -*-
79
+ """DurhamTrees
80
+ Automatically generated by Colaboratory.
81
+ Original file is located at
82
+ https://colab.research.google.com/drive/1czig7JIbqTKp9wNUIRcdMEDF3pFgtxKv
83
+ """
84
+
85
+ import pandas as pd
86
+ import geopandas as gpd
87
+ from datasets import (
88
+ GeneratorBasedBuilder, Version, DownloadManager, SplitGenerator, Split,
89
+ Features, Value, BuilderConfig, DatasetInfo
90
+ )
91
+ import matplotlib.pyplot as plt
92
+ import seaborn as sns
93
+ import csv
94
+ import json
95
+ from shapely.geometry import Point
96
+
97
+ # URL definitions
98
+ _URLS = {
99
+ "first_domain1": {
100
+ "csv_file": "https://drive.google.com/uc?export=download&id=1P61XDtW9fkRYKj6ULhxJyOHG7PqFhZ3s",
101
+ "geojson_file": "https://drive.google.com/uc?export=download&id=1St986GN9m8r1_xwyWWTJBmZG7iadYHgW",
102
+ },
103
+ "first_domain2": {
104
+ "csv_file2": "https://drive.google.com/uc?export=download&id=1QyTJZltvqxiZBDm1V6XcSeykBreY43tj",
105
+ },
106
+ }
107
+
108
+ class DurhamTrees(GeneratorBasedBuilder):
109
+ VERSION = Version("1.0.0")
110
+ def _info(self):
111
+ return DatasetInfo(
112
+ description="This dataset combines information from both classes, with additional processing for csv_file2.",
113
+ features=Features({
114
+ "feature1_from_class1": Value("string"),
115
+ "geometry":Value("string"),
116
+ "OBJECTID": Value("int64"),
117
+ "X": Value("float64"),
118
+ "Y": Value("float64"),
119
+ "feature1_from_class2": Value("string"),
120
+ "streetaddress": Value("string"),
121
+ "city": Value("string"),
122
+ "facilityid": Value("int64"),
123
+ "present": Value("string"),
124
+ "genus": Value("string"),
125
+ "species": Value("string"),
126
+ "commonname": Value("string"),
127
+ "diameterin": Value("float64"),
128
+ "condition": Value("string"),
129
+ "neighborhood": Value("string"),
130
+ "program": Value("string"),
131
+ "plantingw": Value("string"),
132
+ "plantingcond": Value("string"),
133
+ "underpwerlins": Value("string"),
134
+ "GlobalID": Value("string"),
135
+ "created_user": Value("string"),
136
+ "last_edited_user": Value("string"),
137
+ "isoprene": Value("float64"),
138
+ "monoterpene": Value("float64"),
139
+ "monoterpene_class2": Value("float64"),
140
+ "vocs": Value("float64"),
141
+ "coremoved_ozperyr": Value("float64"),
142
+ "coremoved_dolperyr": Value("float64"),
143
+ "o3removed_ozperyr": Value("float64"),
144
+ "o3removed_dolperyr": Value("float64"),
145
+ "no2removed_ozperyr": Value("float64"),
146
+ "no2removed_dolperyr": Value("float64"),
147
+ "so2removed_ozperyr": Value("float64"),
148
+ "so2removed_dolperyr": Value("float64"),
149
+ "pm10removed_ozperyr": Value("float64"),
150
+ "pm10removed_dolperyr": Value("float64"),
151
+ "pm25removed_ozperyr": Value("float64"),
152
+ "o2production_lbperyr": Value("float64"),
153
+ "replacevalue_dol": Value("float64"),
154
+ "carbonstorage_lb": Value("float64"),
155
+ "carbonstorage_dol": Value("float64"),
156
+ "grosscarseq_lbperyr": Value("float64"),
157
+ "grosscarseq_dolperyr": Value("float64"),
158
+ "avoidrunoff_ft2peryr": Value("float64"),
159
+ "avoidrunoff_dol2peryr": Value("float64"),
160
+ "polremoved_ozperyr": Value("float64"),
161
+ "polremoved_dolperyr": Value("float64"),
162
+ "totannbenefits_dolperyr": Value("float64"),
163
+ "leafarea_sqft": Value("float64"),
164
+ "potevapotran_cuftperyr": Value("float64"),
165
+ "evaporation_cuftperyr": Value("float64"),
166
+ "transpiration_cuftperyr": Value("float64"),
167
+ "h2ointercept_cuftperyr": Value("float64"),
168
+ "carbonavoid_lbperyr": Value("float64"),
169
+ "carbonavoid_dolperyr": Value("float64"),
170
+ "heating_mbtuperyr": Value("float64"),
171
+ "heating_dolperyrmbtu": Value("float64"),
172
+ "heating_kwhperyr": Value("float64"),
173
+ "heating_dolperyrmwh": Value("float64"),
174
+ "cooling_kwhperyr": Value("float64"),
175
+ "cooling_dolperyr": Value("float64"),
176
+ "totalenerg_dolperyr": Value("float64"),
177
+ }),
178
+ supervised_keys=None,
179
+ homepage="https://github.com/AuraMa111?tab=repositories",
180
+ citation="Citation for the combined dataset",
181
+ )
182
+
183
+
184
+ def _split_generators(self, dl_manager):
185
+ downloaded_files = dl_manager.download_and_extract(_URLS)
186
+
187
+ return [
188
+ SplitGenerator(
189
+ name=Split.TRAIN,
190
+ gen_kwargs={
191
+ "class1_data_file": downloaded_files["first_domain1"]["csv_file"],
192
+ "class1_geojson_file": downloaded_files["first_domain1"]["geojson_file"],
193
+ "class2_data_file": downloaded_files["first_domain2"]["csv_file2"],
194
+ "split": Split.TRAIN,
195
+ },
196
+ ),
197
+ SplitGenerator(
198
+ name=Split.VALIDATION,
199
+ gen_kwargs={
200
+ "class1_data_file": downloaded_files["first_domain1"]["csv_file"],
201
+ "class1_geojson_file": downloaded_files["first_domain1"]["geojson_file"],
202
+ "class2_data_file": downloaded_files["first_domain2"]["csv_file2"],
203
+ "split": Split.VALIDATION,
204
+ },
205
+ ),
206
+ SplitGenerator(
207
+ name=Split.TEST,
208
+ gen_kwargs={
209
+ "class1_data_file": downloaded_files["first_domain1"]["csv_file"],
210
+ "class1_geojson_file": downloaded_files["first_domain1"]["geojson_file"],
211
+ "class2_data_file": downloaded_files["first_domain2"]["csv_file2"],
212
+ "split": Split.TEST,
213
+ },
214
+ ),
215
+ ]
216
+
217
+ def _generate_examples(self, class1_data_file, class1_geojson_file, class2_data_file, split):
218
+ if split == Split.TRAIN:
219
+ class1_examples = list(self._generate_examples_from_class1(class1_data_file, class1_geojson_file))
220
+ class2_examples = list(self._generate_examples_from_class2(class2_data_file))
221
+ examples = class1_examples + class2_examples
222
+ elif split == Split.VALIDATION:
223
+ class1_examples = list(self._generate_examples_from_class1(class1_data_file, class1_geojson_file))
224
+ examples = class1_examples
225
+ elif split == Split.TEST:
226
+ class2_examples = list(self._generate_examples_from_class2(class2_data_file))
227
+ examples = class2_examples
228
+
229
+ df = pd.DataFrame(examples)
230
+
231
+ for id_, example in enumerate(examples):
232
+ if not isinstance(example, dict):
233
+ example = {"example": example}
234
+ yield id_, example
235
+
236
+ def _generate_examples_from_class1(self, csv_filepath, geojson_filepath):
237
+ columns_to_extract = ["OBJECTID", "X", "Y"] # Remove "geometry" from columns_to_extract
238
+ csv_data = pd.read_csv(csv_filepath)
239
+
240
+ with open(geojson_filepath, 'r') as file:
241
+ geojson_dict = json.load(file)
242
+ gdf = gpd.GeoDataFrame.from_features(geojson_dict['features'], crs="EPSG:4326") # Specify the CRS if known
243
+ merged_data = gdf.merge(csv_data, on='OBJECTID')
244
+ final_data = merged_data[columns_to_extract + ['geometry']] # Include 'geometry' in the final_data
245
+ for id_, row in final_data.iterrows():
246
+ example = row.to_dict()
247
+ yield id_, example
248
+
249
+ def _generate_examples_from_class2(self, csv_filepath2):
250
+ csv_data2 = pd.read_csv(csv_filepath2)
251
+
252
+ columns_to_extract = [
253
+ "streetaddress", "city", "facilityid", "present", "genus", "species",
254
+ "commonname", "diameterin", "condition", "neighborhood", "program", "plantingw",
255
+ "plantingcond", "underpwerlins", "GlobalID", "created_user", "last_edited_user", "isoprene", "monoterpene",
256
+ "monoterpene", "vocs", "coremoved_ozperyr", "coremoved_dolperyr",
257
+ "o3removed_ozperyr", "o3removed_dolperyr", "no2removed_ozperyr", "no2removed_dolperyr",
258
+ "so2removed_ozperyr", "so2removed_dolperyr", "pm10removed_ozperyr", "pm10removed_dolperyr",
259
+ "pm25removed_ozperyr", "o2production_lbperyr", "replacevalue_dol", "carbonstorage_lb",
260
+ "carbonstorage_dol", "grosscarseq_lbperyr", "grosscarseq_dolperyr", "polremoved_ozperyr", "polremoved_dolperyr",
261
+ "totannbenefits_dolperyr", "leafarea_sqft", "potevapotran_cuftperyr", "evaporation_cuftperyr",
262
+ "transpiration_cuftperyr", "h2ointercept_cuftperyr",
263
+ "carbonavoid_lbperyr", "carbonavoid_dolperyr", "heating_mbtuperyr",
264
+ "heating_dolperyrmbtu", "heating_kwhperyr", "heating_dolperyrmwh", "cooling_kwhperyr",
265
+ "cooling_dolperyr", "totalenerg_dolperyr",
266
+ ]
267
+
268
+ final_data = csv_data2[columns_to_extract]
269
+ for id_, row in final_data.iterrows():
270
+ example = row.to_dict()
271
+ non_empty_example = {key: value for key, value in example.items() if pd.notna(value)}
272
+ yield id_, non_empty_example
273
+
274
+ def _correlation_analysis(self, df):
275
+ correlation_matrix = df.corr()
276
+ sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=.5)
277
+ plt.title("Correlation Analysis")
278
+ plt.show()
279
+
280
+ # Create an instance of the DurhamTrees class for training
281
+ durham_trees_dataset_train = DurhamTrees(split=Split.TRAIN)
282
+
283
+ # Build the training dataset
284
+ durham_trees_dataset_train.download_and_prepare()
285
+ dataset_train = durham_trees_dataset_train.as_dataset()
286
+
287
+ # Create an instance of the DurhamTrees class for validation
288
+ durham_trees_dataset_val = DurhamTrees(split=Split.VALIDATION)
289
+
290
+ # Build the validation dataset
291
+ durham_trees_dataset_val.download_and_prepare()
292
+ dataset_val = durham_trees_dataset_val.as_dataset()
293
+
294
+ # Create an instance of the DurhamTrees class for testing
295
+ durham_trees_dataset_test = DurhamTrees(split=Split.TEST)
296
+
297
+ # Build the test dataset
298
+ durham_trees_dataset_test.download_and_prepare()
299
+ dataset_test = durham_trees_dataset_test.as_dataset()