Datasets:
Update dataset.py
Browse files- dataset.py +25 -29
dataset.py
CHANGED
|
@@ -3,19 +3,7 @@ from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split,
|
|
| 3 |
import datasets
|
| 4 |
import csv
|
| 5 |
import pyarrow as pa
|
| 6 |
-
|
| 7 |
-
DEFAULT_DATA_FILES = {
|
| 8 |
-
"Thresholding": {
|
| 9 |
-
"train": "train/train.csv",
|
| 10 |
-
"val": "validation/validation.csv",
|
| 11 |
-
"test": "test/test.csv",
|
| 12 |
-
},
|
| 13 |
-
"Contrastive Learning": {
|
| 14 |
-
"train": "train/data-00000-of-00001.arrow",
|
| 15 |
-
"dev": "validation/data-00000-of-00001.arrow",
|
| 16 |
-
"test": "test/data-00000-of-00001.arrow",
|
| 17 |
-
}
|
| 18 |
-
}
|
| 19 |
|
| 20 |
class CustomConfig(BuilderConfig):
|
| 21 |
def __init__(self, **kwargs):
|
|
@@ -26,44 +14,52 @@ class MyDataset(GeneratorBasedBuilder):
|
|
| 26 |
CustomConfig(
|
| 27 |
name="Contrastive Learning",
|
| 28 |
version=datasets.Version("1.0.0"),
|
| 29 |
-
description="
|
| 30 |
),
|
| 31 |
CustomConfig(
|
| 32 |
name="Thresholding",
|
| 33 |
version=datasets.Version("1.0.0"),
|
| 34 |
-
description="
|
| 35 |
),
|
| 36 |
]
|
| 37 |
|
| 38 |
def _info(self):
|
| 39 |
-
return DatasetInfo()
|
| 40 |
|
| 41 |
def _split_generators(self, dl_manager):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": file_dict.get("val") or file_dict.get("dev")}),
|
| 50 |
-
SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": file_dict["test"]}),
|
| 51 |
-
]
|
| 52 |
|
| 53 |
def _generate_examples(self, filepath):
|
| 54 |
if filepath.endswith(".arrow"):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
table = pa.ipc.RecordBatchFileReader(f).read_all()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
data = table.to_pydict()
|
| 61 |
for i in range(len(next(iter(data.values())))):
|
| 62 |
yield i, {k: data[k][i] for k in data}
|
|
|
|
| 63 |
elif filepath.endswith(".csv"):
|
| 64 |
with open(filepath, encoding="utf-8") as f:
|
| 65 |
reader = csv.DictReader(f)
|
| 66 |
for i, row in enumerate(reader):
|
| 67 |
yield i, row
|
|
|
|
| 68 |
else:
|
| 69 |
raise ValueError(f"Unsupported file format: {filepath}")
|
|
|
|
| 3 |
import datasets
|
| 4 |
import csv
|
| 5 |
import pyarrow as pa
|
| 6 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
class CustomConfig(BuilderConfig):
|
| 9 |
def __init__(self, **kwargs):
|
|
|
|
| 14 |
CustomConfig(
|
| 15 |
name="Contrastive Learning",
|
| 16 |
version=datasets.Version("1.0.0"),
|
| 17 |
+
description="Load from Arrow files"
|
| 18 |
),
|
| 19 |
CustomConfig(
|
| 20 |
name="Thresholding",
|
| 21 |
version=datasets.Version("1.0.0"),
|
| 22 |
+
description="Load from CSV files"
|
| 23 |
),
|
| 24 |
]
|
| 25 |
|
| 26 |
def _info(self):
|
| 27 |
+
return DatasetInfo()
|
| 28 |
|
| 29 |
def _split_generators(self, dl_manager):
|
| 30 |
+
if self.config.name == "Contrastive Learning":
|
| 31 |
+
return [
|
| 32 |
+
SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": "train/data-00000-of-00001.arrow"}),
|
| 33 |
+
SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": "validation/data-00000-of-00001.arrow"}),
|
| 34 |
+
SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": "test/data-00000-of-00001.arrow"}),
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
elif self.config.name == "Thresholding":
|
| 38 |
+
return [
|
| 39 |
+
SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": "train/train.csv"}),
|
| 40 |
+
SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": "validation/validation.csv"}),
|
| 41 |
+
SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": "test/test.csv"}),
|
| 42 |
+
]
|
| 43 |
|
| 44 |
+
else:
|
| 45 |
+
raise ValueError(f"Unsupported config: {self.config.name}")
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def _generate_examples(self, filepath):
|
| 48 |
if filepath.endswith(".arrow"):
|
| 49 |
+
with open(filepath, "rb") as f:
|
| 50 |
+
try:
|
| 51 |
table = pa.ipc.RecordBatchFileReader(f).read_all()
|
| 52 |
+
except Exception as e:
|
| 53 |
+
raise ValueError(f"Could not read Arrow file: {filepath}") from e
|
| 54 |
data = table.to_pydict()
|
| 55 |
for i in range(len(next(iter(data.values())))):
|
| 56 |
yield i, {k: data[k][i] for k in data}
|
| 57 |
+
|
| 58 |
elif filepath.endswith(".csv"):
|
| 59 |
with open(filepath, encoding="utf-8") as f:
|
| 60 |
reader = csv.DictReader(f)
|
| 61 |
for i, row in enumerate(reader):
|
| 62 |
yield i, row
|
| 63 |
+
|
| 64 |
else:
|
| 65 |
raise ValueError(f"Unsupported file format: {filepath}")
|