Datasets:

Modalities:
Text
Formats:
arrow
Languages:
English
ArXiv:
Libraries:
Datasets
License:
AnnaWegmann commited on
Commit
635cc57
·
verified ·
1 Parent(s): 97650d9

Create dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +50 -0
dataset.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, BuilderConfig
2
+ import datasets
3
+ import csv
4
+ import pyarrow as pa
5
+
6
+ class CustomConfig(BuilderConfig):
7
+ def __init__(self, **kwargs):
8
+ super().__init__(**kwargs)
9
+
10
+ class MyDataset(GeneratorBasedBuilder):
11
+ BUILDER_CONFIGS = [
12
+ CustomConfig(name="Contrastive Learning", version=datasets.Version("1.0.0"),
13
+ description="Loads Arrow files for contrastive learning"),
14
+ CustomConfig(name="Thresholding", version=datasets.Version("1.0.0"),
15
+ description="Loads CSV files for thresholding"),
16
+ ]
17
+
18
+ def _info(self):
19
+ # No features defined — schema will be inferred
20
+ return DatasetInfo()
21
+
22
+ def _split_generators(self, dl_manager):
23
+ data_files = self.config.data_files
24
+
25
+ def get_path(split_name, fallback=None):
26
+ for entry in data_files:
27
+ if entry["split"] == split_name:
28
+ return entry["path"]
29
+ return fallback
30
+
31
+ return [
32
+ SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": get_path("train")}),
33
+ SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": get_path("dev") or get_path("val")}),
34
+ SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": get_path("test")}),
35
+ ]
36
+
37
+ def _generate_examples(self, filepath):
38
+ if filepath.endswith(".arrow"):
39
+ table = pa.ipc.RecordBatchFileReader(filepath).read_all()
40
+ records = table.to_pydict()
41
+ keys = list(records.keys())
42
+ for i in range(len(records[keys[0]])):
43
+ yield i, {k: records[k][i] for k in keys}
44
+ elif filepath.endswith(".csv"):
45
+ with open(filepath, encoding="utf-8") as f:
46
+ reader = csv.DictReader(f)
47
+ for i, row in enumerate(reader):
48
+ yield i, row
49
+ else:
50
+ raise ValueError(f"Unsupported file format for file: {filepath}")