|
|
|
"""DurhamTrees |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1czig7JIbqTKp9wNUIRcdMEDF3pFgtxKv |
|
""" |
|
|
|
|
|
"""DurhamTrees |
|
Automatically generated by Colaboratory. |
|
Original file is located at |
|
https://colab.research.google.com/drive/1czig7JIbqTKp9wNUIRcdMEDF3pFgtxKv |
|
""" |
|
import pyarrow.parquet as pq |
|
import pandas as pd |
|
import geopandas as gpd |
|
from datasets import ( |
|
GeneratorBasedBuilder, Version, DownloadManager, SplitGenerator, Split, |
|
Features, Value, BuilderConfig, DatasetInfo |
|
) |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
import csv |
|
import json |
|
from shapely.geometry import Point |
|
import base64 |
|
import matplotlib.pyplot as plt |
|
import matplotlib.image as mpimg |
|
import io |
|
|
|
_URLS = { |
|
"first_domain1": { |
|
"csv_file": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy", |
|
"geojson_file": "https://drive.google.com/uc?export=download&id=1cbn7EY7RofXN7c6Ph0eIGFIZowPZ5lKE", |
|
"parquet_file": "https://drive.google.com/uc?export=download&id=1RNDLJLoSSV9RJptVyfWFhPra0nh-i_CN", |
|
}, |
|
"first_domain2": { |
|
"csv_file2": "https://drive.google.com/uc?export=download&id=1RVdaI5dSTPStjhOHO40ypDv2cAQZpi_Y", |
|
}, |
|
} |
|
|
|
|
|
class DurhamTrees(GeneratorBasedBuilder): |
|
VERSION = Version("1.0.0") |
|
|
|
class MyConfig(BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super().__init__(**kwargs) |
|
|
|
BUILDER_CONFIGS = [ |
|
MyConfig(name="class1_domain1", description="this is combined of csv and geojson"), |
|
MyConfig(name="class2_domain1", description="this is csv file"), |
|
] |
|
|
|
def _info(self): |
|
return DatasetInfo( |
|
description="This dataset combines information from both classes, with additional processing for csv_file2.", |
|
features=Features({ |
|
"feature1_from_class1": Value("string"), |
|
"geometry":Value("string"), |
|
"OBJECTID": Value("int64"), |
|
"X": Value("float64"), |
|
"Y": Value("float64"), |
|
"image": Value("binary"), |
|
"label": Value("int64"), |
|
"feature1_from_class2": Value("string"), |
|
"streetaddress": Value("string"), |
|
"city": Value("string"), |
|
"facilityid": Value("int64"), |
|
"present": Value("string"), |
|
"genus": Value("string"), |
|
"species": Value("string"), |
|
"commonname": Value("string"), |
|
"diameterin": Value("float64"), |
|
"condition": Value("string"), |
|
"neighborhood": Value("string"), |
|
"program": Value("string"), |
|
"plantingw": Value("string"), |
|
"plantingcond": Value("string"), |
|
"underpwerlins": Value("string"), |
|
"GlobalID": Value("string"), |
|
"created_user": Value("string"), |
|
"last_edited_user": Value("string"), |
|
"isoprene": Value("float64"), |
|
"monoterpene": Value("float64"), |
|
"monoterpene_class2": Value("float64"), |
|
"vocs": Value("float64"), |
|
"coremoved_ozperyr": Value("float64"), |
|
"coremoved_dolperyr": Value("float64"), |
|
"o3removed_ozperyr": Value("float64"), |
|
"o3removed_dolperyr": Value("float64"), |
|
"no2removed_ozperyr": Value("float64"), |
|
"no2removed_dolperyr": Value("float64"), |
|
"so2removed_ozperyr": Value("float64"), |
|
"so2removed_dolperyr": Value("float64"), |
|
"pm10removed_ozperyr": Value("float64"), |
|
"pm10removed_dolperyr": Value("float64"), |
|
"pm25removed_ozperyr": Value("float64"), |
|
"o2production_lbperyr": Value("float64"), |
|
"replacevalue_dol": Value("float64"), |
|
"carbonstorage_lb": Value("float64"), |
|
"carbonstorage_dol": Value("float64"), |
|
"grosscarseq_lbperyr": Value("float64"), |
|
"grosscarseq_dolperyr": Value("float64"), |
|
"avoidrunoff_ft2peryr": Value("float64"), |
|
"avoidrunoff_dol2peryr": Value("float64"), |
|
"polremoved_ozperyr": Value("float64"), |
|
"polremoved_dolperyr": Value("float64"), |
|
"totannbenefits_dolperyr": Value("float64"), |
|
"leafarea_sqft": Value("float64"), |
|
"potevapotran_cuftperyr": Value("float64"), |
|
"evaporation_cuftperyr": Value("float64"), |
|
"transpiration_cuftperyr": Value("float64"), |
|
"h2ointercept_cuftperyr": Value("float64"), |
|
"carbonavoid_lbperyr": Value("float64"), |
|
"carbonavoid_dolperyr": Value("float64"), |
|
"heating_mbtuperyr": Value("float64"), |
|
"heating_dolperyrmbtu": Value("float64"), |
|
"heating_kwhperyr": Value("float64"), |
|
"heating_dolperyrmwh": Value("float64"), |
|
"cooling_kwhperyr": Value("float64"), |
|
"cooling_dolperyr": Value("float64"), |
|
"totalenerg_dolperyr": Value("float64"), |
|
}), |
|
supervised_keys=("image", "label"), |
|
homepage="https://github.com/AuraMa111?tab=repositories", |
|
citation="Citation for the combined dataset", |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
downloaded_files = dl_manager.download_and_extract(_URLS) |
|
|
|
return [ |
|
SplitGenerator( |
|
name=Split.TRAIN, |
|
gen_kwargs={ |
|
"class1_data_file": downloaded_files["first_domain1"]["csv_file"], |
|
"class1_geojson_file": downloaded_files["first_domain1"]["geojson_file"], |
|
"class2_data_file": downloaded_files["first_domain2"]["csv_file2"], |
|
"parquet_file": downloaded_files["first_domain1"]["parquet_file"], |
|
"split": Split.TRAIN, |
|
}, |
|
), |
|
] |
|
|
|
|
|
|
|
|
|
def _generate_examples(self, class1_data_file, class1_geojson_file, class2_data_file, parquet_file, split): |
|
class1_examples = list(self._generate_examples_from_class1(class1_data_file, class1_geojson_file)) |
|
class2_examples = list(self._generate_examples_from_class2(class2_data_file)) |
|
|
|
|
|
parquet_data = pq.read_table(parquet_file).to_pandas() |
|
class1_examples += list(self._generate_examples_from_parquet(parquet_data)) |
|
|
|
examples = class1_examples + class2_examples |
|
df = pd.DataFrame(examples) |
|
|
|
for id_, example in enumerate(examples): |
|
if not isinstance(example, dict): |
|
|
|
example = {"example": example} |
|
yield id_, example |
|
|
|
def _generate_examples_from_class1(self, csv_filepath, geojson_filepath): |
|
columns_to_extract = ["OBJECTID", "X", "Y"] |
|
csv_data = pd.read_csv(csv_filepath) |
|
|
|
with open(geojson_filepath, 'r') as file: |
|
geojson_dict = json.load(file) |
|
gdf = gpd.GeoDataFrame.from_features(geojson_dict['features'], crs="EPSG:4326") |
|
merged_data = gdf.merge(csv_data, on='OBJECTID') |
|
final_data = merged_data[columns_to_extract + ['geometry']] |
|
for id_, row in final_data.iterrows(): |
|
example = row.to_dict() |
|
yield id_, example |
|
|
|
|
|
|
|
|
|
|
|
def _generate_examples_from_class2(self, csv_filepath2): |
|
csv_data2 = pd.read_csv(csv_filepath2) |
|
|
|
|
|
columns_to_extract = [ |
|
"streetaddress", "city", "facilityid", "present", "genus", "species", |
|
"commonname", "diameterin", "condition", "neighborhood", "program", "plantingw", |
|
"plantingcond", "underpwerlins", "GlobalID", "created_user", "last_edited_user", "isoprene", "monoterpene", |
|
"monoterpene", "vocs", "coremoved_ozperyr", "coremoved_dolperyr", |
|
"o3removed_ozperyr", "o3removed_dolperyr", "no2removed_ozperyr", "no2removed_dolperyr", |
|
"so2removed_ozperyr", "so2removed_dolperyr", "pm10removed_ozperyr", "pm10removed_dolperyr", |
|
"pm25removed_ozperyr", "o2production_lbperyr", "replacevalue_dol", "carbonstorage_lb", |
|
"carbonstorage_dol", "grosscarseq_lbperyr", "grosscarseq_dolperyr", "polremoved_ozperyr", "polremoved_dolperyr", |
|
"totannbenefits_dolperyr", "leafarea_sqft", "potevapotran_cuftperyr", "evaporation_cuftperyr", |
|
"transpiration_cuftperyr", "h2ointercept_cuftperyr", |
|
"carbonavoid_lbperyr", "carbonavoid_dolperyr", "heating_mbtuperyr", |
|
"heating_dolperyrmbtu", "heating_kwhperyr", "heating_dolperyrmwh", "cooling_kwhperyr", |
|
"cooling_dolperyr", "totalenerg_dolperyr", |
|
] |
|
|
|
final_data = csv_data2[columns_to_extract] |
|
for id_, row in final_data.iterrows(): |
|
example = row.to_dict() |
|
non_empty_example = {key: value for key, value in example.items() if pd.notna(value)} |
|
yield id_, example |
|
|
|
|
|
def _generate_examples_from_parquet(self, parquet_data): |
|
for id_, row in parquet_data.iterrows(): |
|
|
|
if "image" in row and "bytes" in row["image"]: |
|
|
|
image_data = base64.b64decode(row["image"]["bytes"]) |
|
example = {"image": image_data, "label": row["label"]} |
|
|
|
|
|
image_bytes = example.get('image', None) |
|
if image_bytes: |
|
img = mpimg.imread(io.BytesIO(image_bytes), format='PNG') |
|
plt.imshow(img) |
|
plt.show() |
|
|
|
yield id_, example |
|
else: |
|
print(f"Skipping example {id_} as it has missing or invalid image data") |
|
|
|
|
|
|
|
def _correlation_analysis(self, df): |
|
correlation_matrix = df.corr() |
|
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=.5) |
|
plt.title("Correlation Analysis") |
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
durham_trees_dataset = DurhamTrees(name='class1_domain1') |
|
|
|
|
|
durham_trees_dataset.download_and_prepare() |
|
|
|
|
|
dataset = durham_trees_dataset.as_dataset() |
|
|
|
|
|
for example in dataset['train']: |
|
if "image" in example and example["image"] is not None and "bytes" in example["image"]: |
|
|
|
image_data = base64.b64decode(example["image"]["bytes"]) |
|
img = mpimg.imread(io.BytesIO(image_data), format='PNG') |
|
plt.imshow(img) |
|
plt.show() |
|
|
|
|
|
durham_trees_dataset_another = DurhamTrees(name='class2_domain1') |
|
|
|
|
|
durham_trees_dataset_another.download_and_prepare() |
|
|
|
|
|
dataset_another = durham_trees_dataset_another.as_dataset() |
|
|
|
|
|
for example in dataset_another['train']: |
|
if "image" in example and example["image"] is not None and "bytes" in example["image"]: |
|
|
|
image_data = base64.b64decode(example["image"]["bytes"]) |
|
img = mpimg.imread(io.BytesIO(image_data), format='PNG') |
|
plt.imshow(img) |
|
plt.show() |