Graptoloidea-Specimens-Imaging / Graptolodiea-Speciemens-Imaging.py
LeoZhangzaolin's picture
Update Graptolodiea-Speciemens-Imaging.py
d635639 verified
raw
history blame
4.4 kB
"""Graptoloidea Specimens dataset."""
import os
import random
from typing import List
import datasets
import pandas as pd
import numpy as np
import csv
import logging
from PIL import Image
import ast
_CITATION = """\
111
"""
_DESCRIPTION = """\
[Your dataset description here...]
"""
_HOMEPAGE = "https://zenodo.org/records/6194943"
_license = "111"
class GraptoloideaSpecimensDataset(datasets.GeneratorBasedBuilder):
_URL = "https://raw.githubusercontent.com/LeoZhangzaolin/photos/main/Final_GS_with_Images.csv"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"Suborder": datasets.Value("string"),
"Infraorder": datasets.Value("string"),
"Family (Subfamily)": datasets.Value("string"),
"Genus": datasets.Value("string"),
"Tagged Species Name": datasets.Value("string"),
"Image": datasets.Value("string"),
"Stage": datasets.Value("string"),
"Mean Age Value": datasets.Value("float64"),
"Locality (Longitude, Latitude, Horizon)": datasets.Value("string"),
"Reference (Specimens Firstly Published)": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
downloaded_file = dl_manager.download_and_extract(self._URL)
# Read the CSV file
df = pd.read_csv(downloaded_file)
df = df.sample(frac=1).reset_index(drop=True) # Shuffle the dataset
# Splitting the dataset
train_size = int(0.7 * len(df))
test_size = int(0.15 * len(df))
train_df = df[:train_size]
test_df = df[train_size:train_size + test_size]
validation_df = df[train_size + test_size:]
# Save split dataframes to temporary CSV files
train_file = '/tmp/train_split.csv'
test_file = '/tmp/test_split.csv'
validation_file = '/tmp/validation_split.csv'
train_df.to_csv(train_file, index=False)
test_df.to_csv(test_file, index=False)
validation_df.to_csv(validation_file, index=False)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_file}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_file}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": validation_file}),
]
def _generate_examples(self, filepath):
"""This function returns the examples from the CSV file."""
logging.info("generating examples from = %s", filepath)
with open(filepath, encoding='utf-8') as f:
reader = csv.DictReader(f)
key = 0
for row in reader:
key += 1
# Extracting data from each column
suborder = row['Suborder'].strip()
infraorder = row['Infraorder'].strip()
family_subfamily = row['Family (Subfamily)'].strip()
genus = row['Genus'].strip()
species_name = row['tagged species name'].strip()
image = row['image'].strip()
stage = row['Stage'].strip()
mean_age = row['mean age value']
locality = row['Locality (Longitude, Latitude, Horizon)'].strip()
reference = row['Reference (specimens firstly published)'].strip()
# Constructing the example
yield key, {
"Suborder": suborder,
"Infraorder": infraorder,
"Family (Subfamily)": family_subfamily,
"Genus": genus,
"Tagged Species Name": species_name,
"Image": image,
"Stage": stage,
"Mean Age Value": mean_age,
"Locality (Longitude, Latitude, Horizon)": locality,
"Reference (Specimens Firstly Published)": reference,
}