moukaii commited on
Commit
aaf5555
·
verified ·
1 Parent(s): 4374727

Update Tuberculosis_Dataset.py

Browse files
Files changed (1) hide show
  1. Tuberculosis_Dataset.py +23 -27
Tuberculosis_Dataset.py CHANGED
@@ -1,12 +1,9 @@
1
- from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Array3D, BuilderConfig, SplitGenerator, Version
2
- from datasets.features import Features, Value, Sequence
3
  import datasets
4
  import pandas as pd
5
  import json
6
  import zipfile
7
- from PIL import Image
8
- import numpy as np
9
- import io
10
 
11
  _DESCRIPTION = """\
12
  This dataset is curated from the original “The MultiCaRe Dataset” to focus on the chest tuberculosis patients and can be used to develop algorithms of the segmentation of chest CT images and the classification of tuberculosis positive or control.
@@ -24,19 +21,19 @@ class TuberculosisDataset(GeneratorBasedBuilder):
24
 
25
  def _info(self):
26
  return DatasetInfo(
27
- description = _DESCRIPTION,
28
  features=Features({
29
  "case_id": Value("string"),
30
  "gender": Value("string"),
31
  "age": Value("int8"),
32
  "case_text": Value("string"),
33
  "keywords": Value("string"),
34
- "image_arrays": Sequence(Value(dtype="uint8")),
35
  "caption": Value("string"),
36
  }),
37
- supervised_keys = None,
38
- homepage = "https://zenodo.org/api/records/10079370/files-archive",
39
- citation = _CITATION,
40
  )
41
 
42
  def _split_generators(self, dl_manager):
@@ -47,7 +44,7 @@ class TuberculosisDataset(GeneratorBasedBuilder):
47
  "caption_json": f"{base_url}image_metadata.json",
48
  "images_zip": "https://github.com/zhankai-ye/tuberculosis_dataset/raw/main/images/PMC.zip"
49
  }
50
- downloaded_files = dl_manager.download(urls)
51
 
52
  return [
53
  SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=downloaded_files),
@@ -77,31 +74,30 @@ class TuberculosisDataset(GeneratorBasedBuilder):
77
  merged_df = merged_df.where(pd.notnull(merged_df), None)
78
  merged_df['age'] = merged_df['age'].astype('int8')
79
 
80
- # Prepare images
81
- image_arrays = self._prepare_images(images_zip)
82
 
83
  # Yield examples
84
  for idx, row in merged_df.iterrows():
 
85
  yield idx, {
86
  "case_id": row["case_id"],
87
  "gender": row["gender"],
88
  "age": int(row["age"]),
89
  "case_text": row["case_text"],
90
  "keywords": row["keywords"],
91
- "image_arrays": image_arrays.get(row["case_id"], []),
92
  "caption": row["caption"],
93
  }
94
 
95
- def _prepare_images(self, images_zip_path):
96
- image_arrays = {}
97
- with zipfile.ZipFile(images_zip_path, 'r') as zip_ref:
98
- for file_info in zip_ref.infolist():
99
- if file_info.filename.endswith('.jpg') and not file_info.is_dir():
100
- with zip_ref.open(file_info) as image_file:
101
- img = Image.open(io.BytesIO(image_file.read()))
102
- img_array = np.array(img)
103
- key = file_info.filename.split('/')[-1].split('_')[0]
104
- if key not in image_arrays:
105
- image_arrays[key] = []
106
- image_arrays[key].append(img_array)
107
- return image_arrays
 
1
+ from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Features, Value, Sequence, ClassLabel, Image, BuilderConfig, SplitGenerator, Version
 
2
  import datasets
3
  import pandas as pd
4
  import json
5
  import zipfile
6
+ import os
 
 
7
 
8
  _DESCRIPTION = """\
9
  This dataset is curated from the original “The MultiCaRe Dataset” to focus on the chest tuberculosis patients and can be used to develop algorithms of the segmentation of chest CT images and the classification of tuberculosis positive or control.
 
21
 
22
  def _info(self):
23
  return DatasetInfo(
24
+ description=_DESCRIPTION,
25
  features=Features({
26
  "case_id": Value("string"),
27
  "gender": Value("string"),
28
  "age": Value("int8"),
29
  "case_text": Value("string"),
30
  "keywords": Value("string"),
31
+ "image_files": Sequence(Image()), # Change from image_arrays to image_files
32
  "caption": Value("string"),
33
  }),
34
+ supervised_keys=None,
35
+ homepage="https://zenodo.org/api/records/10079370/files-archive",
36
+ citation=_CITATION,
37
  )
38
 
39
  def _split_generators(self, dl_manager):
 
44
  "caption_json": f"{base_url}image_metadata.json",
45
  "images_zip": "https://github.com/zhankai-ye/tuberculosis_dataset/raw/main/images/PMC.zip"
46
  }
47
+ downloaded_files = dl_manager.download_and_extract(urls)
48
 
49
  return [
50
  SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=downloaded_files),
 
74
  merged_df = merged_df.where(pd.notnull(merged_df), None)
75
  merged_df['age'] = merged_df['age'].astype('int8')
76
 
77
+ # Extract and prepare image file paths
78
+ image_file_paths = self._prepare_image_file_paths(images_zip)
79
 
80
  # Yield examples
81
  for idx, row in merged_df.iterrows():
82
+ image_files = image_file_paths.get(row["case_id"], [])
83
  yield idx, {
84
  "case_id": row["case_id"],
85
  "gender": row["gender"],
86
  "age": int(row["age"]),
87
  "case_text": row["case_text"],
88
  "keywords": row["keywords"],
89
+ "image_files": image_files,
90
  "caption": row["caption"],
91
  }
92
 
93
+ def _prepare_image_file_paths(self, images_zip_path):
94
+ # Assumes images have been extracted to a directory in dl_manager's cache directory
95
+ image_file_paths = {}
96
+ for root, _, files in os.walk(images_zip_path):
97
+ for file in files:
98
+ if file.endswith('.jpg'):
99
+ key = file.split('_')[0]
100
+ if key not in image_file_paths:
101
+ image_file_paths[key] = []
102
+ image_file_paths[key].append(os.path.join(root, file))
103
+ return image_file_paths