lucas-morin commited on
Commit
1984170
·
verified ·
1 Parent(s): 996fbd0

Upload markushgrapher-datasets.py

Browse files
Files changed (1) hide show
  1. markushgrapher-datasets.py +57 -0
markushgrapher-datasets.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datasets import DatasetInfo, GeneratorBasedBuilder, SplitGenerator, Split, BuilderConfig, Features, Value, Array2D, Array3D, Sequence, Image, DatasetDict
3
+ import datasets
4
+
5
+ class MarkushGrapherConfig(BuilderConfig):
6
+ def __init__(self, **kwargs):
7
+ super().__init__(**kwargs)
8
+
9
+
10
+ class MarkushGrapherDataset(GeneratorBasedBuilder):
11
+ BUILDER_CONFIGS = [
12
+ MarkushGrapherConfig(name="markushgrapher-synthetic-training", description="Synthetic training set"),
13
+ MarkushGrapherConfig(name="m2s", description="Multi-modal real-world benchmark set"),
14
+ MarkushGrapherConfig(name="markushgrapher-synthetic", description="Synthetic benchmark set"),
15
+ MarkushGrapherConfig(name="uspto-markush", description="Image-only real-world benchmark set"),
16
+ ]
17
+
18
+ def _info(self):
19
+ return DatasetInfo(
20
+ description="MarkushGrapher-Datasets",
21
+ features=Features({
22
+ "id": Value("int64"),
23
+ "image_name": Value("string"),
24
+ "page_image": Image(mode=None, decode=True, id=None),
25
+ "description": Value("string"),
26
+ "annotation": Value("string"),
27
+ "mol": Value("string"),
28
+ "cxsmiles_dataset": Value("string"),
29
+ "cxsmiles": Value("string"),
30
+ "cxsmiles_opt": Value("string"),
31
+ "keypoints": Value(dtype='null', id=None),
32
+ "cells": [{'bbox': Sequence(feature=Value(dtype='float64', id=None), length=-1, id=None), 'text': Value(dtype='string', id=None)}],
33
+ }),
34
+ supervised_keys=None,
35
+ )
36
+
37
+ def _split_generators(self, dl_manager):
38
+ data_dir = dl_manager.download_and_extract(self.config.name)
39
+
40
+ splits = []
41
+ for split in ["train", "test"]:
42
+ split_path = os.path.join(data_dir, split)
43
+ if os.path.exists(split_path):
44
+ splits.append(SplitGenerator(
45
+ name=Split.TRAIN if split == "train" else Split.TEST,
46
+ gen_kwargs={"data_dir": split_path},
47
+ ))
48
+
49
+ return splits
50
+
51
+ def _generate_examples(self, data_dir):
52
+ # Automatically loads .arrow files
53
+ arrow_files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.endswith(".arrow")]
54
+ for file in arrow_files:
55
+ dataset = datasets.Dataset.from_file(file)
56
+ for i, row in enumerate(dataset):
57
+ yield f"{file}_{i}", row