alkzar90 commited on
Commit
62ec5aa
1 Parent(s): 8fdbc29

Format and improve readability

Browse files
Files changed (1) hide show
  1. NIH-Chest-X-ray-dataset.py +65 -56
NIH-Chest-X-ray-dataset.py CHANGED
@@ -1,77 +1,89 @@
1
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
 
3
  import datasets
4
  from datasets.tasks import ImageClassification
5
-
6
  from requests import get
7
  from pandas import read_csv
8
 
9
  logger = datasets.logging.get_logger(__name__)
10
 
11
-
12
- _HOMEPAGE = "https://nihcc.app.box.com/v/ChestXray-NIHCC"
13
-
14
  _CITATION = """\
15
- @ONLINE {beansdata,
16
- author="Xiaosong Wang, Yifan Peng, Le Lu, Zhiyong Lu, Mohammadhadi Bagheri, Ronald Summer",
17
- title="ChestX-ray8: Hospital-scale Chest X-ray Database and Benchmarks on Weakly-Supervised Classification and Localization of Common Thorax Diseases",
18
- month="January",
19
- year="2017",
20
- url="https://nihcc.app.box.com/v/ChestXray-NIHCC"
 
 
 
 
21
  }
22
  """
23
 
 
24
  _DESCRIPTION = """\
25
  The NIH Chest X-ray dataset consists of 100,000 de-identified images of chest x-rays. The images are in PNG format.
26
 
27
  The data is provided by the NIH Clinical Center and is available through the NIH download site: https://nihcc.app.box.com/v/ChestXray-NIHCC
28
  """
29
 
30
- _IMAGE_URLS2 = [
31
- 'https://nihcc.box.com/shared/static/vfk49d74nhbxq3nqjg0900w5nvkorp5c.gz',
32
- 'https://nihcc.box.com/shared/static/i28rlmbvmfjbl8p2n3ril0pptcmcu9d1.gz',
33
- 'https://nihcc.box.com/shared/static/f1t00wrtdk94satdfb9olcolqx20z2jp.gz',
34
- 'https://nihcc.box.com/shared/static/0aowwzs5lhjrceb3qp67ahp0rd1l1etg.gz',
35
- 'https://nihcc.box.com/shared/static/v5e3goj22zr6h8tzualxfsqlqaygfbsn.gz',
36
- 'https://nihcc.box.com/shared/static/asi7ikud9jwnkrnkj99jnpfkjdes7l6l.gz',
37
- 'https://nihcc.box.com/shared/static/jn1b4mw4n6lnh74ovmcjb8y48h8xj07n.gz',
38
- 'https://nihcc.box.com/shared/static/tvpxmn7qyrgl0w8wfh9kqfjskv6nmm1j.gz',
39
- 'https://nihcc.box.com/shared/static/upyy3ml7qdumlgk2rfcvlb9k6gvqq2pj.gz',
40
- 'https://nihcc.box.com/shared/static/l6nilvfa9cg3s28tqv1qc1olm3gnz54p.gz',
41
- 'https://nihcc.box.com/shared/static/hhq8fkdgvcari67vfhs7ppg2w6ni4jze.gz',
42
- 'https://nihcc.box.com/shared/static/ioqwiy20ihqwyr8pf4c24eazhh281pbu.gz'
43
- ]
44
 
45
  _IMAGE_URLS = [
46
- "https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/data/images/images_001.zip",
47
- "https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/data/images/images_003.zip",
48
- "https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/data/images/images_004.zip"
 
49
  #'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/dummy/0.0.0/images_001.tar.gz',
50
  #'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/dummy/0.0.0/images_002.tar.gz'
51
  ]
52
 
 
53
  _URLS = {
54
- 'train_val_list': 'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/raw/main/data/train_val_list.txt',
55
- 'test_list': 'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/raw/main/data/test_list.txt',
56
- 'labels': 'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/raw/main/data/Data_Entry_2017_v2020.csv',
57
- 'image_urls': _IMAGE_URLS
58
  }
59
 
60
- _LABEL2IDX = {'No Finding': 0,
61
- 'Atelectasis': 1,
62
- 'Cardiomegaly': 2,
63
- 'Effusion': 3,
64
- 'Infiltration': 4,
65
- 'Mass': 5,
66
- 'Nodule': 6,
67
- 'Pneumonia': 7,
68
- 'Pneumothorax': 8,
69
- 'Consolidation': 9,
70
- 'Edema': 10,
71
- 'Emphysema': 11,
72
- 'Fibrosis': 12,
73
- 'Pleural_Thickening': 13,
74
- 'Hernia': 14}
 
 
75
 
76
  _NAMES = list(_LABEL2IDX.keys())
77
 
@@ -79,7 +91,7 @@ _NAMES = list(_LABEL2IDX.keys())
79
  class XChest(datasets.GeneratorBasedBuilder):
80
  """NIH Image Chest X-ray dataset."""
81
 
82
- VERSION = datasets.Version("0.0.0")
83
 
84
  def _info(self):
85
  return datasets.DatasetInfo(
@@ -88,7 +100,6 @@ class XChest(datasets.GeneratorBasedBuilder):
88
  {
89
  "image_file_path": datasets.Value("string"),
90
  "image": datasets.Image(),
91
- #"labels": datasets.features.ClassLabel(names=_NAMES),
92
  "labels": datasets.features.Sequence(
93
  datasets.features.ClassLabel(num_classes=len(_NAMES),
94
  names=_NAMES)
@@ -98,8 +109,6 @@ class XChest(datasets.GeneratorBasedBuilder):
98
  supervised_keys=("image", "labels"),
99
  homepage=_HOMEPAGE,
100
  citation=_CITATION,
101
- #task_templates=[ImageClassification(image_column="image",
102
- # label_column="labels")],
103
  )
104
 
105
 
@@ -115,7 +124,7 @@ class XChest(datasets.GeneratorBasedBuilder):
115
  test_files = []
116
 
117
  # Download batches
118
- data_files = dl_manager.download_and_extract(_URLS['image_urls'])
119
 
120
  # Iterate trought image folder and check if they belong to
121
  # the trainset or testset
@@ -133,26 +142,26 @@ class XChest(datasets.GeneratorBasedBuilder):
133
  datasets.SplitGenerator(
134
  name=datasets.Split.TRAIN,
135
  gen_kwargs={
136
- 'files': iter(train_files)
137
  }
138
 
139
  ),
140
  datasets.SplitGenerator(
141
  name=datasets.Split.TEST,
142
  gen_kwargs={
143
- 'files': iter(test_files)
144
  }
145
  )
146
  ]
147
 
148
  def _generate_examples(self, files):
149
  # Read csv with image labels
150
- label_csv = read_csv(_URLS['labels'])
151
  for i, path in enumerate(files):
152
  file_name = os.path.basename(path)
153
  # Get image id to filter the respective row of the csv
154
  image_id = file_name.split('/')[-1]
155
- image_labels = label_csv[label_csv['Image Index'] == image_id]['Finding Labels'].values[0].split('|')
156
  if file_name.endswith(".png"):
157
  yield i, {
158
  "image_file_path": path,
 
1
+ # Copyright 2022 Cristóbal Alcázar
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """NIH Chest X-ray Dataset"""
15
+
16
 
17
+ import os
18
  import datasets
19
  from datasets.tasks import ImageClassification
 
20
  from requests import get
21
  from pandas import read_csv
22
 
23
  logger = datasets.logging.get_logger(__name__)
24
 
 
 
 
25
  _CITATION = """\
26
+ @inproceedings{Wang_2017,
27
+ doi = {10.1109/cvpr.2017.369},
28
+ url = {https://doi.org/10.1109%2Fcvpr.2017.369},
29
+ year = 2017,
30
+ month = {jul},
31
+ publisher = {{IEEE}
32
+ },
33
+ author = {Xiaosong Wang and Yifan Peng and Le Lu and Zhiyong Lu and Mohammadhadi Bagheri and Ronald M. Summers},
34
+ title = {{ChestX}-Ray8: Hospital-Scale Chest X-Ray Database and Benchmarks on Weakly-Supervised Classification and Localization of Common Thorax Diseases},
35
+ booktitle = {2017 {IEEE} Conference on Computer Vision and Pattern Recognition ({CVPR})}
36
  }
37
  """
38
 
39
+
40
  _DESCRIPTION = """\
41
  The NIH Chest X-ray dataset consists of 100,000 de-identified images of chest x-rays. The images are in PNG format.
42
 
43
  The data is provided by the NIH Clinical Center and is available through the NIH download site: https://nihcc.app.box.com/v/ChestXray-NIHCC
44
  """
45
 
46
+
47
+ _HOMEPAGE = "https://nihcc.app.box.com/v/chestxray-nihcc"
48
+
49
+
50
+ _REPO = "https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/data"
51
+
 
 
 
 
 
 
 
 
52
 
53
  _IMAGE_URLS = [
54
+ f"{_REPO}/images/images_001.zip",
55
+ f"{_repo}/images/images_003.zip",
56
+ f"{_REPO}/images/images_004.zip",
57
+ f"{_REPO}/images/images_005.zip"
58
  #'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/dummy/0.0.0/images_001.tar.gz',
59
  #'https://huggingface.co/datasets/alkzar90/NIH-Chest-X-ray-dataset/resolve/main/dummy/0.0.0/images_002.tar.gz'
60
  ]
61
 
62
+
63
  _URLS = {
64
+ "train_val_list": f"{_REPO}/train_val_list.txt",
65
+ "test_list": f"{_REPO}/test_list.txt",
66
+ "labels": f"{_REPO}/Data_Entry_2017_v2020.csv",
67
+ "image_urls": _IMAGE_URLS
68
  }
69
 
70
+
71
+ _LABEL2IDX = {"No Finding": 0,
72
+ "Atelectasis": 1,
73
+ "Cardiomegaly": 2,
74
+ "Effusion": 3,
75
+ "Infiltration": 4,
76
+ "Mass": 5,
77
+ "Nodule": 6,
78
+ "Pneumonia": 7,
79
+ "Pneumothorax": 8,
80
+ "Consolidation": 9,
81
+ "Edema": 10,
82
+ "Emphysema": 11,
83
+ "Fibrosis": 12,
84
+ "Pleural_Thickening": 13,
85
+ "Hernia": 14}
86
+
87
 
88
  _NAMES = list(_LABEL2IDX.keys())
89
 
 
91
  class XChest(datasets.GeneratorBasedBuilder):
92
  """NIH Image Chest X-ray dataset."""
93
 
94
+ VERSION = datasets.Version("1.0.0")
95
 
96
  def _info(self):
97
  return datasets.DatasetInfo(
 
100
  {
101
  "image_file_path": datasets.Value("string"),
102
  "image": datasets.Image(),
 
103
  "labels": datasets.features.Sequence(
104
  datasets.features.ClassLabel(num_classes=len(_NAMES),
105
  names=_NAMES)
 
109
  supervised_keys=("image", "labels"),
110
  homepage=_HOMEPAGE,
111
  citation=_CITATION,
 
 
112
  )
113
 
114
 
 
124
  test_files = []
125
 
126
  # Download batches
127
+ data_files = dl_manager.download_and_extract(_URLS["image_urls"])
128
 
129
  # Iterate trought image folder and check if they belong to
130
  # the trainset or testset
 
142
  datasets.SplitGenerator(
143
  name=datasets.Split.TRAIN,
144
  gen_kwargs={
145
+ "files": iter(train_files)
146
  }
147
 
148
  ),
149
  datasets.SplitGenerator(
150
  name=datasets.Split.TEST,
151
  gen_kwargs={
152
+ "files": iter(test_files)
153
  }
154
  )
155
  ]
156
 
157
  def _generate_examples(self, files):
158
  # Read csv with image labels
159
+ label_csv = read_csv(_URLS["labels"])
160
  for i, path in enumerate(files):
161
  file_name = os.path.basename(path)
162
  # Get image id to filter the respective row of the csv
163
  image_id = file_name.split('/')[-1]
164
+ image_labels = label_csv[label_csv["Image Index"] == image_id]["Finding Labels"].values[0].split("|")
165
  if file_name.endswith(".png"):
166
  yield i, {
167
  "image_file_path": path,