axiong commited on
Commit
873a82c
1 Parent(s): 2cee5e8

Update pmc_oa.py

Browse files
Files changed (1) hide show
  1. pmc_oa.py +99 -45
pmc_oa.py CHANGED
@@ -1,16 +1,12 @@
1
  """PMC-OA Dataset"""
2
 
3
  import os
4
- raise RuntimeError( os.system('ls') )
5
-
6
  import jsonlines
7
 
8
  import datasets
9
 
10
-
11
  logger = datasets.logging.get_logger(__name__)
12
 
13
-
14
  _CITATION = """\
15
  @article{lin2023pmc,
16
  title={PMC-CLIP: Contrastive Language-Image Pre-training using Biomedical Documents},
@@ -30,56 +26,114 @@ including image-text retrieval on ROCO, MedMNIST image classification, Medical V
30
 
31
  _HOMEPAGE = "https://weixionglin.github.io/PMC-CLIP/"
32
 
33
- _URL = "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/"
34
- _URLS = {
35
- "images": _URL + "images.zip",
36
- "train": _URL + "train.jsonl",
37
- "valid": _URL + "valid.jsonl",
38
- "test": _URL + "test.jsonl"
39
  }
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  class PMC_OA(datasets.GeneratorBasedBuilder):
43
- """PMC_OA Dataset."""
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  def _info(self):
46
- return datasets.DatasetInfo(
47
- description=_DESCRIPTION,
48
- features=datasets.Features(
49
- {
50
- "image": datasets.Image(),
51
- "caption": datasets.Value("string"),
52
- }
53
- ),
54
- supervised_keys=None,
55
- homepage=_HOMEPAGE,
56
- citation=_CITATION,
57
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def _split_generators(self, dl_manager):
60
- data_dir = dl_manager.download_and_extract(_URLS)
61
-
62
- return [
63
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir['train'], "image_dir": data_dir['images']}),
64
- datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_dir['valid'], "image_dir": data_dir['images']}),
65
- datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_dir['test'], "image_dir": data_dir['images']}),
66
- ]
 
 
 
 
 
 
 
67
 
68
  def _generate_examples(self, filepath, image_dir):
69
- """This function returns the examples in the raw (text) form."""
70
  logger.info("generating examples from = %s", filepath)
71
-
72
  with jsonlines.open(filepath) as reader:
73
- _id = 0
74
- for obj in reader:
75
- relative_image_path = obj['image']
76
- image_path = os.path.join(image_dir, "caption_T060_filtered_top4_sep_v0_subfigures", relative_image_path)
77
- caption = obj['caption']
78
- yield _id, {
79
- "image": {
80
- "path": image_path,
81
- "bytes": open(image_path, "rb").read(),
82
- },
83
- "caption": caption,
84
- }
85
- _id += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """PMC-OA Dataset"""
2
 
3
  import os
 
 
4
  import jsonlines
5
 
6
  import datasets
7
 
 
8
  logger = datasets.logging.get_logger(__name__)
9
 
 
10
  _CITATION = """\
11
  @article{lin2023pmc,
12
  title={PMC-CLIP: Contrastive Language-Image Pre-training using Biomedical Documents},
 
26
 
27
  _HOMEPAGE = "https://weixionglin.github.io/PMC-CLIP/"
28
 
29
+ _URLs = {
30
+ "images": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/images.zip",
31
+ "pmc_oa_beta": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/pmc_oa_beta.jsonl",
32
+ "pmc_oa": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/pmc_oa.jsonl",
 
 
33
  }
34
 
35
 
36
+ class PMC_OA_Config(datasets.BuilderConfig):
37
+ """BuilderConfig for PMC_OA"""
38
+
39
+ def __init__(self, **kwargs):
40
+ """
41
+ Args:
42
+ **kwargs: keyword arguments forwarded to super.
43
+ """
44
+ super(PMC_OA_Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
45
+
46
+
47
  class PMC_OA(datasets.GeneratorBasedBuilder):
48
+ """PMC_OA Dataset"""
49
+
50
+ VERSION = datasets.Version("1.0.0")
51
+ BUILDER_CONFIGS = [
52
+ PMC_OA_Config(
53
+ name="pmc_oa_beta",
54
+ description="<subfigure, caption> pairs. Subfigures detected by a DETR model.",
55
+ ),
56
+ PMC_OA_Config(
57
+ name="pmc_oa",
58
+ description="<subfigure, subcaption> pairs. Subfigures detected by a DETR model. Subcaptions detected by ChatGPT and aligned with subfigures.",
59
+ ),
60
+ ]
61
 
62
  def _info(self):
63
+ if self.config.name == "pmc_oa_beta":
64
+ return datasets.DatasetInfo(
65
+ description=_DESCRIPTION,
66
+ features=datasets.Features(
67
+ {
68
+ "image": datasets.Value("string"),
69
+ "caption": datasets.Value("string"),
70
+ }
71
+ ),
72
+ supervised_keys=None,
73
+ citation=_CITATION,
74
+ homepage=_HOMEPAGE,
75
+ )
76
+ elif self.config.name == "pmc_oa":
77
+ return datasets.DatasetInfo(
78
+ description=_DESCRIPTION,
79
+ features=datasets.Features(
80
+ {
81
+ "image": datasets.Value("string"),
82
+ "caption": datasets.Value("string"),
83
+ "alignment_type": datasets.Value("string"),
84
+ "alignment_score": datasets.Value("float"),
85
+ }
86
+ ),
87
+ supervised_keys=None,
88
+ citation=_CITATION,
89
+ homepage=_HOMEPAGE,
90
+ )
91
 
92
  def _split_generators(self, dl_manager):
93
+ """Returns SplitGenerators."""
94
+ downloaded_files = dl_manager.download_and_extract(_URLs)
95
+ if self.config.name == "pmc_oa_beta":
96
+ return [
97
+ datasets.SplitGenerator(
98
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["pmc_oa_beta"], "image_dir": downloaded_files['images']}
99
+ )
100
+ ]
101
+ elif self.config.name == "pmc_oa":
102
+ return [
103
+ datasets.SplitGenerator(
104
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["pmc_oa"], "image_dir": downloaded_files['images']}
105
+ )
106
+ ]
107
 
108
  def _generate_examples(self, filepath, image_dir):
109
+ """Yields examples."""
110
  logger.info("generating examples from = %s", filepath)
111
+
112
  with jsonlines.open(filepath) as reader:
113
+ for _id, obj in enumerate(reader):
114
+ if self.config.name == "pmc_oa_beta":
115
+ relative_image_path = obj['image']
116
+ image_path = os.path.join(image_dir, "caption_T060_filtered_top4_sep_v0_subfigures", relative_image_path)
117
+ caption = obj['caption']
118
+ yield _id, {
119
+ "image": {
120
+ "path": image_path,
121
+ "bytes": open(image_path, "rb").read(),
122
+ },
123
+ "caption": caption,
124
+ }
125
+ elif self.config.name == "pmc_oa":
126
+ relative_image_path = obj['image']
127
+ image_path = os.path.join(image_dir, "caption_T060_filtered_top4_sep_v0_subfigures", relative_image_path)
128
+ caption = obj['caption']
129
+ alignment_type = obj['alignment_type']
130
+ alignment_score = obj['alignment_score']
131
+ yield _id, {
132
+ "image": {
133
+ "path": image_path,
134
+ "bytes": open(image_path, "rb").read(),
135
+ },
136
+ "caption": caption,
137
+ "alignment_type": alignment_type,
138
+ "alignment_score": alignment_score,
139
+ }