axiong commited on
Commit
5ca1280
·
1 Parent(s): 554e5b7

Update pmc_oa.py

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