|
|
import pandas as pd |
|
|
import datasets |
|
|
import numpy as np |
|
|
import ast |
|
|
from PIL import Image |
|
|
|
|
|
class NeuripsConfig(datasets.BuilderConfig): |
|
|
|
|
|
def __init__(self, **kwargs): |
|
|
super(NeuripsConfig, self).__init__(**kwargs) |
|
|
|
|
|
class NeuripsDataset(datasets.GeneratorBasedBuilder): |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
|
NeuripsConfig( |
|
|
name="Kenya", |
|
|
version=datasets.Version("1.0.0"), |
|
|
description="Full dataset, combining both systematically and opportunistically sampled leaves" |
|
|
), |
|
|
NeuripsConfig( |
|
|
name="South_Africa", |
|
|
version=datasets.Version("1.0.0"), |
|
|
description="Subset containing only systematically sampled leaves" |
|
|
), |
|
|
NeuripsConfig( |
|
|
name="USA_Summer", |
|
|
version=datasets.Version("1.0.0"), |
|
|
description="Subset containing only opportunistically sampled leaves" |
|
|
), |
|
|
NeuripsConfig( |
|
|
name="USA_Winter", |
|
|
version=datasets.Version("1.0.0"), |
|
|
description="Subset containing only opportunistically sampled leaves" |
|
|
), |
|
|
NeuripsConfig( |
|
|
name="Species_ID", |
|
|
version=datasets.Version("1.0.0"), |
|
|
description="Subset containing the DataFrames allowing to link the target encounter rates to a list of species for each country" |
|
|
) |
|
|
] |
|
|
|
|
|
DEFAULT_CONFIG_NAME = "Kenya" |
|
|
|
|
|
def _info(self): |
|
|
|
|
|
|
|
|
|
|
|
if self.config.name == "Kenya": |
|
|
features = datasets.Features({ |
|
|
"hotspot_id": datasets.Value("string"), |
|
|
"hotspot_name": datasets.Value("string"), |
|
|
"lon": datasets.Value("float32"), |
|
|
"lat": datasets.Value("float32"), |
|
|
"county": datasets.Value("string"), |
|
|
"county_code": datasets.Value("string"), |
|
|
"state": datasets.Value("string"), |
|
|
"state_code": datasets.Value("string"), |
|
|
'sat_imagery_path': datasets.Value("string"), |
|
|
'environmental_path': datasets.Value("string"), |
|
|
"split": datasets.Value("string"), |
|
|
"num_complete_checklists" : datasets.Value("int32"), |
|
|
"target": datasets.Sequence(feature=datasets.Value("float32"), length=1054), |
|
|
"geometry": datasets.Value("string") |
|
|
|
|
|
}) |
|
|
elif self.config.name == "South_Africa": |
|
|
features = datasets.Features({ |
|
|
"hotspot_id": datasets.Value("string"), |
|
|
"hotspot_name": datasets.Value("string"), |
|
|
"lon": datasets.Value("float32"), |
|
|
"lat": datasets.Value("float32"), |
|
|
"county": datasets.Value("string"), |
|
|
"county_code": datasets.Value("string"), |
|
|
"state": datasets.Value("string"), |
|
|
"state_code": datasets.Value("string"), |
|
|
'sat_imagery_path': datasets.Value("string"), |
|
|
'environmental_path': datasets.Value("string"), |
|
|
"split": datasets.Value("string"), |
|
|
"num_complete_checklists" : datasets.Value("int32"), |
|
|
"target": datasets.Sequence(feature=datasets.Value("float32"), length=1054), |
|
|
"geometry": datasets.Value("string") |
|
|
}) |
|
|
elif self.config.name == "USA_Summer": |
|
|
features = datasets.Features({ |
|
|
"hotspot_id": datasets.Value("string"), |
|
|
"hotspot_name": datasets.Value("string"), |
|
|
"lon": datasets.Value("float32"), |
|
|
"lat": datasets.Value("float32"), |
|
|
"county": datasets.Value("string"), |
|
|
"county_code": datasets.Value("string"), |
|
|
"state": datasets.Value("string"), |
|
|
"state_code": datasets.Value("string"), |
|
|
'sat_imagery_path': datasets.Value("string"), |
|
|
'environmental_path': datasets.Value("string"), |
|
|
"split": datasets.Value("string"), |
|
|
"num_complete_checklists" : datasets.Value("int32"), |
|
|
"target": datasets.Sequence(feature=datasets.Value("float32"), length=1054), |
|
|
"geometry": datasets.Value("string") |
|
|
}) |
|
|
elif self.config.name == "USA_Winter": |
|
|
features = datasets.Features({ |
|
|
"hotspot_id": datasets.Value("string"), |
|
|
"hotspot_name": datasets.Value("string"), |
|
|
"lon": datasets.Value("float32"), |
|
|
"lat": datasets.Value("float32"), |
|
|
"county": datasets.Value("string"), |
|
|
"county_code": datasets.Value("string"), |
|
|
"state": datasets.Value("string"), |
|
|
"state_code": datasets.Value("string"), |
|
|
'sat_imagery_path': datasets.Value("string"), |
|
|
'environmental_path': datasets.Value("string"), |
|
|
"split": datasets.Value("string"), |
|
|
"num_complete_checklists" : datasets.Value("int32"), |
|
|
"target": datasets.Sequence(feature=datasets.Value("float32"), length=1054), |
|
|
"geometry": datasets.Value("string") |
|
|
}) |
|
|
elif self.config.name == "Species_ID": |
|
|
features = datasets.Features({ |
|
|
"scientific_name": datasets.Value("string"), |
|
|
"ebird_code": datasets.Value("string"), |
|
|
"inat_preview": datasets.Image(), |
|
|
"target_value_index" : datasets.Value("int32"), |
|
|
}) |
|
|
else: |
|
|
raise ValueError(f"Unsupported config: {self.config.name}") |
|
|
|
|
|
return datasets.DatasetInfo( |
|
|
description="The SITTELLE Benchmark Dataset", |
|
|
features=features, |
|
|
supervised_keys=None, |
|
|
homepage="https://huggingface.co/datasets/imageomics/invasive_plants_hawaii", |
|
|
license="MIT", |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
if self.config.name == "Kenya": |
|
|
train_csv = "Kenya/train_split.csv" |
|
|
test_csv = "Kenya/test_split.csv" |
|
|
val_csv = "Kenya/valid_split.csv" |
|
|
return [ |
|
|
datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}), |
|
|
datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}), |
|
|
datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}), |
|
|
] |
|
|
elif self.config.name == "South_Africa": |
|
|
train_csv = "Kenya/train_split.csv" |
|
|
test_csv = "Kenya/test_split.csv" |
|
|
val_csv = "Kenya/valid_split.csv" |
|
|
return [ |
|
|
datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}), |
|
|
datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}), |
|
|
datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}), |
|
|
] |
|
|
elif self.config.name == "USA_Summer": |
|
|
train_csv = "Kenya/train_split.csv" |
|
|
test_csv = "Kenya/test_split.csv" |
|
|
val_csv = "Kenya/valid_split.csv" |
|
|
return [ |
|
|
datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}), |
|
|
datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}), |
|
|
datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}), |
|
|
] |
|
|
elif self.config.name == "USA_Winter": |
|
|
train_csv = "Kenya/train_split.csv" |
|
|
test_csv = "Kenya/test_split.csv" |
|
|
val_csv = "Kenya/valid_split.csv" |
|
|
return [ |
|
|
datasets.SplitGenerator(name="train", gen_kwargs={"filepath": train_csv}), |
|
|
datasets.SplitGenerator(name="val", gen_kwargs={"filepath": test_csv}), |
|
|
datasets.SplitGenerator(name="test", gen_kwargs={"filepath": val_csv}), |
|
|
] |
|
|
elif self.config.name == "Species_ID": |
|
|
kenya_csv = "Kenya/species_id_kenya.csv" |
|
|
southafrica_csv = "Kenya/species_id_kenya.csv" |
|
|
usasummer_csv = "Kenya/species_id_kenya.csv" |
|
|
usawinter_csv = "Kenya/species_id_kenya.csv" |
|
|
return [ |
|
|
datasets.SplitGenerator(name="Kenya", gen_kwargs={"filepath": kenya_csv}), |
|
|
datasets.SplitGenerator(name="South_Africa", gen_kwargs={"filepath": southafrica_csv}), |
|
|
datasets.SplitGenerator(name="USA_Summer", gen_kwargs={"filepath": usasummer_csv}), |
|
|
datasets.SplitGenerator(name="USA_Winter", gen_kwargs={"filepath": usawinter_csv}), |
|
|
] |
|
|
else: |
|
|
raise ValueError(f"Unknown config: {self.config.name}") |
|
|
|
|
|
def _generate_examples(self, filepath): |
|
|
if self.config.name in ["Kenya", "South_Africa", "USA_Summer", "USA_Winter"]: |
|
|
df_metadata = pd.read_csv(filepath) |
|
|
df_metadata["target"] = df_metadata["target"].apply(ast.literal_eval).apply(lambda x: list(map(float, x))) |
|
|
for idx in range(len(df_metadata)): |
|
|
row = df_metadata.iloc[idx] |
|
|
yield idx, { |
|
|
"hotspot_id": row['hotspot_id'], |
|
|
"hotspot_name": row['hotspot_name'], |
|
|
"lon": row['lon'], |
|
|
"lat": row['lat'], |
|
|
"county": row['county'], |
|
|
"county_code": row['county_code'], |
|
|
"state": row['state'], |
|
|
"state_code": row['state_code'], |
|
|
'sat_imagery_path': row['sat_imagery_path'], |
|
|
'environmental_path': row['environmental_path'], |
|
|
"split": row['split'], |
|
|
"num_complete_checklists" : row['num_complete_checklists'], |
|
|
"target": row['target'], |
|
|
"geometry": row['geometry'] |
|
|
} |
|
|
if self.config.name == "Species_ID": |
|
|
df_metadata = pd.read_csv(filepath) |
|
|
for idx in range(len(df_metadata)): |
|
|
row = df_metadata.iloc[idx] |
|
|
yield idx, { |
|
|
"scientific_name": row['scientific_name'], |
|
|
"ebird_code": row['ebird_code'], |
|
|
"inat_preview": row['inat_preview'], |
|
|
"target_value_index" : row['target_value_index'], |
|
|
} |