jesse-radiata commited on
Commit
6559553
·
verified ·
1 Parent(s): 3b76088

Add files using upload-large-folder tool

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. brain-structure.py +62 -69
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
brain-structure.py CHANGED
@@ -29,7 +29,6 @@ _LICENSE = "ODC-By v1.0"
29
  class BrainStructureConfig(datasets.BuilderConfig):
30
  """
31
  Configuration class for the Brain-Structure dataset.
32
- You can define multiple configurations if needed (e.g., different subsets).
33
  """
34
  def __init__(self, **kwargs):
35
  super().__init__(**kwargs)
@@ -38,19 +37,17 @@ class BrainStructureConfig(datasets.BuilderConfig):
38
  class BrainStructure(datasets.GeneratorBasedBuilder):
39
  """
40
  A dataset loader for T1 .nii.gz files plus JSON sidecars indicating splits
41
- (train, validation, test). Usage example:
42
 
43
- ds = load_dataset(
44
- "radiata-ai/brain-structure",
45
- name="all",
46
- split="train",
47
- trust_remote_code=True
48
- )
49
  """
50
 
51
  VERSION = datasets.Version("1.0.0")
52
 
53
- # If you do NOT need multiple configs, you can define just one here:
54
  BUILDER_CONFIGS = [
55
  BrainStructureConfig(
56
  name="all",
@@ -62,7 +59,7 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
62
 
63
  def _info(self):
64
  """
65
- Returns DatasetInfo, including feature types and other meta information.
66
  """
67
  return datasets.DatasetInfo(
68
  description=_DESCRIPTION,
@@ -75,8 +72,6 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
75
  "participant_id": datasets.Value("string"),
76
  "session_id": datasets.Value("string"),
77
  "study": datasets.Value("string"),
78
-
79
- # Additional fields from the JSON sidecar
80
  "age": datasets.Value("int32"),
81
  "sex": datasets.Value("string"),
82
  "clinical_diagnosis": datasets.Value("string"),
@@ -100,15 +95,14 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
100
 
101
  def _split_generators(self, dl_manager: datasets.DownloadManager):
102
  """
103
- Creates SplitGenerators for train, validation, and test.
104
- No remote download is performed here. Instead, we reference
105
- the local directory containing this script.
106
- """
107
- # Typically, we use dl_manager.download_and_extract(...) for remote data,
108
- # but here we assume the data is already in the same repo as this script.
109
 
110
- # Path to the folder containing this script (and presumably the data).
111
- data_dir = os.path.abspath(os.path.dirname(__file__))
 
 
 
112
 
113
  return [
114
  datasets.SplitGenerator(
@@ -127,56 +121,55 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
127
 
128
  def _generate_examples(self, data_dir, desired_split):
129
  """
130
- Recursively walks data_dir, locates JSON sidecar files, and yields
131
- examples that match the specified 'desired_split'.
 
 
 
132
  """
133
  id_ = 0
134
  for root, dirs, files in os.walk(data_dir):
135
  for fname in files:
136
- # Only consider sidecar files that start with "msub" and end with "_scandata.json"
137
- if not (fname.startswith("msub") and fname.endswith("_scandata.json")):
138
- continue
139
-
140
- sidecar_path = os.path.join(root, fname)
141
- with open(sidecar_path, "r") as f:
142
- sidecar = json.load(f)
143
-
144
- # if sidecar's "split" matches the desired_split...
145
- if sidecar.get("split") == desired_split:
146
- # attempt to find the .nii.gz with the same "msub" prefix
147
- possible_nii_prefix = fname.replace("_scandata.json", "_T1w")
148
- nii_filepath = None
149
- for potential_file in files:
150
- if (potential_file.startswith(possible_nii_prefix)
151
- and potential_file.endswith(".nii.gz")):
152
- nii_filepath = os.path.join(root, potential_file)
153
- break
154
-
155
- if not nii_filepath:
156
- logger.warning(f"No corresponding .nii.gz file found for {sidecar_path}")
157
- continue
158
-
159
- yield id_, {
160
- "id": str(id_),
161
- "nii_filepath": nii_filepath,
162
- "metadata": {
163
- "split": sidecar.get("split", ""),
164
- "participant_id": sidecar.get("participant_id", ""),
165
- "session_id": sidecar.get("session_id", ""),
166
- "study": sidecar.get("study", ""),
167
- "age": sidecar.get("age", 0),
168
- "sex": sidecar.get("sex", ""),
169
- "clinical_diagnosis": sidecar.get("clinical_diagnosis", ""),
170
- "scanner_manufacturer": sidecar.get("scanner_manufacturer", ""),
171
- "scanner_model": sidecar.get("scanner_model", ""),
172
- "field_strength": sidecar.get("field_strength", ""),
173
- "image_quality_rating": float(sidecar.get("image_quality_rating", 0.0)),
174
- "total_intracranial_volume": float(sidecar.get("total_intracranial_volume", 0.0)),
175
- "license": sidecar.get("license", ""),
176
- "website": sidecar.get("website", ""),
177
- "citation": sidecar.get("citation", ""),
178
- "t1_file_name": sidecar.get("t1_file_name", ""),
179
- "radiata_id": sidecar.get("radiata_id", 0),
180
- },
181
- }
182
- id_ += 1
 
29
  class BrainStructureConfig(datasets.BuilderConfig):
30
  """
31
  Configuration class for the Brain-Structure dataset.
 
32
  """
33
  def __init__(self, **kwargs):
34
  super().__init__(**kwargs)
 
37
  class BrainStructure(datasets.GeneratorBasedBuilder):
38
  """
39
  A dataset loader for T1 .nii.gz files plus JSON sidecars indicating splits
40
+ (train, validation, test).
41
 
42
+ Examples of how users typically load this dataset:
43
+
44
+ >>> from datasets import load_dataset
45
+ >>> ds_val = load_dataset("radiata-ai/brain-structure", split="validation", trust_remote_code=True)
46
+ >>> ds_train = load_dataset("./brain-structure", split="train") # local clone
 
47
  """
48
 
49
  VERSION = datasets.Version("1.0.0")
50
 
 
51
  BUILDER_CONFIGS = [
52
  BrainStructureConfig(
53
  name="all",
 
59
 
60
  def _info(self):
61
  """
62
+ Returns DatasetInfo, including the feature schema and other metadata.
63
  """
64
  return datasets.DatasetInfo(
65
  description=_DESCRIPTION,
 
72
  "participant_id": datasets.Value("string"),
73
  "session_id": datasets.Value("string"),
74
  "study": datasets.Value("string"),
 
 
75
  "age": datasets.Value("int32"),
76
  "sex": datasets.Value("string"),
77
  "clinical_diagnosis": datasets.Value("string"),
 
95
 
96
  def _split_generators(self, dl_manager: datasets.DownloadManager):
97
  """
98
+ Creates SplitGenerators for 'train', 'validation', and 'test'.
99
+ *No actual download* is done here, so data_dir is simply '.'.
 
 
 
 
100
 
101
+ Why '.'? Because whether the dataset is used locally or from the Hub,
102
+ Hugging Face will place (or reference) files in this same folder context.
103
+ """
104
+ data_dir = "." # The local folder containing subdirectories like IXI/, DLBS/, etc.
105
+ logger.info(f"BrainStructure: scanning data in {os.path.abspath(data_dir)}")
106
 
107
  return [
108
  datasets.SplitGenerator(
 
121
 
122
  def _generate_examples(self, data_dir, desired_split):
123
  """
124
+ Recursively walks the directory structure, looking for JSON sidecar files
125
+ ending in '_scandata.json'. For each matching file, yields an example
126
+ if sidecar["split"] == desired_split.
127
+
128
+ The corresponding .nii.gz is located by prefix matching.
129
  """
130
  id_ = 0
131
  for root, dirs, files in os.walk(data_dir):
132
  for fname in files:
133
+ # If you only want "msub" files, you can add: if fname.startswith("msub") and ...
134
+ if fname.endswith("_scandata.json"):
135
+ sidecar_path = os.path.join(root, fname)
136
+ with open(sidecar_path, "r") as f:
137
+ sidecar = json.load(f)
138
+
139
+ if sidecar.get("split") == desired_split:
140
+ # Find the .nii.gz prefix
141
+ nii_prefix = fname.replace("_scandata.json", "_T1w")
142
+ nii_filepath = None
143
+ for potential_file in files:
144
+ if potential_file.startswith(nii_prefix) and potential_file.endswith(".nii.gz"):
145
+ nii_filepath = os.path.join(root, potential_file)
146
+ break
147
+
148
+ if not nii_filepath:
149
+ logger.warning(f"No corresponding .nii.gz found for {sidecar_path}")
150
+ continue
151
+
152
+ yield id_, {
153
+ "id": str(id_),
154
+ "nii_filepath": nii_filepath,
155
+ "metadata": {
156
+ "split": sidecar.get("split", ""),
157
+ "participant_id": sidecar.get("participant_id", ""),
158
+ "session_id": sidecar.get("session_id", ""),
159
+ "study": sidecar.get("study", ""),
160
+ "age": sidecar.get("age", 0),
161
+ "sex": sidecar.get("sex", ""),
162
+ "clinical_diagnosis": sidecar.get("clinical_diagnosis", ""),
163
+ "scanner_manufacturer": sidecar.get("scanner_manufacturer", ""),
164
+ "scanner_model": sidecar.get("scanner_model", ""),
165
+ "field_strength": sidecar.get("field_strength", ""),
166
+ "image_quality_rating": float(sidecar.get("image_quality_rating", 0.0)),
167
+ "total_intracranial_volume": float(sidecar.get("total_intracranial_volume", 0.0)),
168
+ "license": sidecar.get("license", ""),
169
+ "website": sidecar.get("website", ""),
170
+ "citation": sidecar.get("citation", ""),
171
+ "t1_file_name": sidecar.get("t1_file_name", ""),
172
+ "radiata_id": sidecar.get("radiata_id", 0),
173
+ },
174
+ }
175
+ id_ += 1