Cyrile commited on
Commit
08853d8
1 Parent(s): d41cb84

Upload aftdb.py

Browse files
Files changed (1) hide show
  1. aftdb.py +217 -0
aftdb.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import datasets
5
+ from PIL import Image
6
+
7
+
8
+ _DESCRIPTION = """
9
+ The Arxiv Figure Table Database (AFTdb) facilitates the linking of documentary
10
+ objects, such as figures and tables, with their captions. This enables a
11
+ comprehensive description of document-oriented images (excluding images from
12
+ cameras). For the table component, the character structure is preserved in
13
+ addition to the image of the table and its caption. This database is ideal
14
+ for multimodal processing of documentary images.
15
+ """
16
+ _LICENSE = "apache-2.0"
17
+ _CITATION = """
18
+ @online{AFTdb,
19
+ AUTHOR = {Cyrile Delestre},
20
+ URL = {},
21
+ YEAR = {2024},
22
+ KEYWORDS = {NLP ; Multimodal}
23
+ }
24
+ """
25
+ _NB_TAR_FIGURE = [158, 4] # train, test
26
+ _NB_TAR_TABLE = [17, 1] # train, test
27
+
28
+
29
+ def extract_files_tar(all_path, data_dir, nb_files):
30
+ paths = [
31
+ os.path.join(data_dir, f"train-{ii:03d}.tar")
32
+ for ii in range(nb_files[0])
33
+ ]
34
+ all_path['train'] += paths
35
+ paths = [
36
+ os.path.join(data_dir, f"test-{ii:03d}.tar")
37
+ for ii in range(nb_files[1])
38
+ ]
39
+ all_path['test'] += paths
40
+
41
+
42
+ class AFTConfig(datasets.BuilderConfig):
43
+ """Builder Config for AFT"""
44
+
45
+ def __init__(self, nb_files_figure, nb_files_table, **kwargs):
46
+ """BuilderConfig for Food-101.
47
+ Args:
48
+ data_url: `string`, url to download the zip file from.
49
+ """
50
+ super().__init__(version=datasets.__version__, **kwargs)
51
+ self.nb_files_figure = nb_files_figure
52
+ self.nb_files_table = nb_files_table
53
+
54
+
55
+ class AFT_Dataset(datasets.GeneratorBasedBuilder):
56
+ """Arxiv Figure Table (AFT) dataset"""
57
+
58
+ BUILDER_CONFIGS = [
59
+ AFTConfig(
60
+ name="figure",
61
+ description=(
62
+ "Dataset containing scientific article figures associated "
63
+ "with their caption, summary, and article title."
64
+ ),
65
+ data_dir="./data/arxiv_dataset/{type}",
66
+ nb_files_figure=_NB_TAR_FIGURE,
67
+ nb_files_table=None
68
+ ),
69
+ AFTConfig(
70
+ name="table",
71
+ description=(
72
+ "Dataset containing tables in JPG image format from "
73
+ "scientific articles, along with the corresponding textual "
74
+ "representation of the table, including its caption, summary, "
75
+ "and article title."
76
+ ),
77
+ data_dir="./data/arxiv_dataset/{type}",
78
+ nb_files_figure=None,
79
+ nb_files_table=_NB_TAR_TABLE
80
+ ),
81
+ AFTConfig(
82
+ name="figure+table",
83
+ description=(
84
+ "Dataset containing figure and tables in JPG image format "
85
+ "from scientific articles, along with the corresponding "
86
+ "textual representation of the table, including its caption, "
87
+ "summary, and article title."
88
+ ),
89
+ data_dir="./data/arxiv_dataset/{type}",
90
+ nb_files_figure=_NB_TAR_FIGURE,
91
+ nb_files_table=_NB_TAR_TABLE
92
+ )
93
+ ]
94
+
95
+ DEFAULT_CONFIG_NAME = "figure+table"
96
+
97
+ def _info(self):
98
+ return datasets.DatasetInfo(
99
+ description=_DESCRIPTION,
100
+ features=datasets.Features(
101
+ {
102
+ 'id': datasets.Value('string'),
103
+ 'paper_id': datasets.Value('string'),
104
+ 'type': datasets.Value('string'),
105
+ 'authors': datasets.Value('string'),
106
+ 'categories': datasets.Value('string'),
107
+ 'title': {
108
+ 'english': datasets.Value('string'),
109
+ 'french': datasets.Value('string')
110
+ },
111
+ 'summary': {
112
+ 'english': datasets.Value('string'),
113
+ 'french': datasets.Value('string')
114
+ },
115
+ 'caption': {
116
+ 'english': datasets.Value('string'),
117
+ 'french': datasets.Value('string')
118
+ },
119
+ 'image': datasets.Image(),
120
+ 'data': datasets.Value('string'),
121
+ 'newcommands': datasets.Sequence(datasets.Value('string'))
122
+ }
123
+ ),
124
+ citation=_CITATION,
125
+ license=_LICENSE
126
+ )
127
+
128
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
129
+ all_path = dict(train=[], test=[])
130
+ if self.config.nb_files_figure:
131
+ extract_files_tar(
132
+ all_path=all_path,
133
+ data_dir=self.config.data_dir.format(type='figure'),
134
+ nb_files=self.config.nb_files_figure
135
+ )
136
+ if self.config.nb_files_table:
137
+ extract_files_tar(
138
+ all_path=all_path,
139
+ data_dir=self.config.data_dir.format(type='table'),
140
+ nb_files=self.config.nb_files_table
141
+ )
142
+ if dl_manager.is_streaming:
143
+ downloaded_files = dl_manager.download(all_path)
144
+ else:
145
+ downloaded_files = dl_manager.download_and_extract(all_path)
146
+ return [
147
+ datasets.SplitGenerator(
148
+ name=datasets.Split.TRAIN,
149
+ gen_kwargs={
150
+ 'filepaths': downloaded_files['train'],
151
+ 'is_streaming': dl_manager.is_streaming
152
+ }
153
+ ),
154
+ datasets.SplitGenerator(
155
+ name=datasets.Split.TEST,
156
+ gen_kwargs={
157
+ "filepaths": downloaded_files['test'],
158
+ 'is_streaming': dl_manager.is_streaming
159
+ }
160
+ )
161
+ ]
162
+
163
+ def _generate_examples(self, filepaths, is_streaming):
164
+ if is_streaming:
165
+ _json, _jpg = False, False
166
+ dl_manager = datasets.DownloadManager()
167
+ for path_tar in filepaths:
168
+ iter_tar = dl_manager.iter_archive(path_tar)
169
+ for path, file_obj in iter_tar:
170
+ if path.endswith('.json'):
171
+ metadata = json.load(file_obj)
172
+ _json = True
173
+ if path.endswith('.jpg'):
174
+ img = Image.open(file_obj)
175
+ _jpg = True
176
+ if _json and _jpg:
177
+ _json, _jpg = False, False
178
+ yield metadata['id'], {
179
+ 'id': metadata['id'],
180
+ 'paper_id': metadata['paper_id'],
181
+ 'type': metadata['type'],
182
+ 'authors': metadata['authors'],
183
+ 'categories': metadata['categories'],
184
+ 'title': metadata['title'],
185
+ 'summary': metadata['summary'],
186
+ 'caption': metadata['caption'],
187
+ 'image': img,
188
+ 'data': metadata['data'],
189
+ 'newcommands': metadata['newcommands']
190
+ }
191
+ else:
192
+ for path in filepaths:
193
+ all_file = os.listdir(path)
194
+ all_id_obs = sorted(
195
+ set(map(lambda x: x.split('.')[0], all_file))
196
+ )
197
+ for id_obs in all_id_obs:
198
+ path_metadata = os.path.join(
199
+ path,
200
+ f"{id_obs}.metadata.json"
201
+ )
202
+ path_image = os.path.join(path, f"{id_obs}.image.jpg")
203
+ metadata = json.load(open(path_metadata, 'r'))
204
+ img = Image.open(path_image)
205
+ yield id_obs, {
206
+ 'id': metadata['id'],
207
+ 'paper_id': metadata['paper_id'],
208
+ 'type': metadata['type'],
209
+ 'authors': metadata['authors'],
210
+ 'categories': metadata['categories'],
211
+ 'title': metadata['title'],
212
+ 'summary': metadata['summary'],
213
+ 'caption': metadata['caption'],
214
+ 'image': img,
215
+ 'data': metadata['data'],
216
+ 'newcommands': metadata['newcommands']
217
+ }