DEV: Add a load script
Browse filesThis load script should separate the partitions according to the "split" column
- dataset.py +53 -0
dataset.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import datasets
|
| 3 |
+
|
| 4 |
+
class DNABarcodeDataset(datasets.GeneratorBasedBuilder):
|
| 5 |
+
def _info(self):
|
| 6 |
+
return datasets.DatasetInfo(
|
| 7 |
+
description="DNA barcode dataset with hierarchical taxonomy labels and multiple splits.",
|
| 8 |
+
features=datasets.Features({
|
| 9 |
+
"processid": datasets.Value("string"),
|
| 10 |
+
"sampleid": datasets.Value("string"),
|
| 11 |
+
"dna_bin": datasets.Value("string"),
|
| 12 |
+
"phylum": datasets.Value("string"),
|
| 13 |
+
"class": datasets.Value("string"),
|
| 14 |
+
"order": datasets.Value("string"),
|
| 15 |
+
"family": datasets.Value("string"),
|
| 16 |
+
"genus": datasets.Value("string"),
|
| 17 |
+
"species": datasets.Value("string"), # label
|
| 18 |
+
"dna_barcode": datasets.Value("string"), # input data
|
| 19 |
+
"split": datasets.ClassLabel(names=["train", "val", "test", "test_unseen", "pretrain"]),
|
| 20 |
+
}),
|
| 21 |
+
supervised_keys=("dna_barcode", "species"), # For model training
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def _split_generators(self, dl_manager):
|
| 25 |
+
data_path = dl_manager.download("CanInv_metadata.csv") # Use a URL or relative path
|
| 26 |
+
return [
|
| 27 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_path, "split": "train"}),
|
| 28 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_path, "split": "val"}),
|
| 29 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_path, "split": "test"}),
|
| 30 |
+
datasets.SplitGenerator(name="test_unseen", gen_kwargs={"filepath": data_path, "split": "test_unseen"}),
|
| 31 |
+
datasets.SplitGenerator(name="pretrain", gen_kwargs={"filepath": data_path, "split": "pretrain"}),
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
def _generate_examples(self, filepath, split):
|
| 35 |
+
with open(filepath, encoding="utf-8") as f:
|
| 36 |
+
reader = csv.DictReader(f)
|
| 37 |
+
idx = 0
|
| 38 |
+
for row in reader:
|
| 39 |
+
if row["split"] == split:
|
| 40 |
+
yield idx, {
|
| 41 |
+
"processid": row["processid"],
|
| 42 |
+
"sampleid": row["sampleid"],
|
| 43 |
+
"dna_bin": row["dna_bin"],
|
| 44 |
+
"phylum": row["phylum"],
|
| 45 |
+
"class": row["class"],
|
| 46 |
+
"order": row["order"],
|
| 47 |
+
"family": row["family"],
|
| 48 |
+
"genus": row["genus"],
|
| 49 |
+
"species": row["species"],
|
| 50 |
+
"dna_barcode": row["dna_barcode"],
|
| 51 |
+
"split": row["split"],
|
| 52 |
+
}
|
| 53 |
+
idx += 1
|