Ziyuan111 commited on
Commit
9acf5e8
·
verified ·
1 Parent(s): 85c1848

Upload durhamtrees.py

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