jesse-radiata commited on
Commit
63e941c
·
verified ·
1 Parent(s): c10e3f5

Add files using upload-large-folder tool

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
OASIS-2/.DS_Store CHANGED
Binary files a/OASIS-2/.DS_Store and b/OASIS-2/.DS_Store differ
 
OASIS-2/sub-OASIS20169/.DS_Store ADDED
Binary file (6.15 kB). View file
 
OASIS-2/sub-OASIS20169/ses-02/.DS_Store ADDED
Binary file (6.15 kB). View file
 
README.md CHANGED
@@ -84,75 +84,92 @@ brain-structure/
84
 
85
  # Example usage
86
  ```
87
- # install Hugging Face Datasets library
88
  pip install datasets
 
 
 
 
89
  ```
90
 
91
  ```
92
  # load datasets
93
  from datasets import load_dataset
94
 
95
- ds_train = load_dataset("radiata-ai/brain-structure", name="all", split="train", trust_remote_code=True)
96
- ds_val = load_dataset("radiata-ai/brain-structure", name="all", split="validation", trust_remote_code=True)
97
- ds_test = load_dataset("radiata-ai/brain-structure", name="all", split="test", trust_remote_code=True)
98
  ```
99
 
100
  ```
101
  # example PyTorch processing of images
 
102
  import torch
103
  import torch.nn.functional as F
104
  from torch.utils.data import Dataset
105
- import nibabel as nib
106
 
107
- class NiiDataset(Dataset):
108
  """
109
- A PyTorch Dataset that wraps a Hugging Face Dataset containing
110
- MRI .nii.gz file paths, and loads/normalizes/resamples each volume.
111
  """
112
- def __init__(self, hf_dataset):
113
- """
114
- hf_dataset: a Hugging Face Dataset object, e.g. ds_train
115
- (each example should have a 'nii_filepath')
116
- """
117
- self.hf_dataset = hf_dataset
118
-
119
- def __len__(self):
120
- return len(self.hf_dataset)
121
-
122
- def __getitem__(self, idx):
123
- # Load the dataset example
124
- example = self.hf_dataset[idx]
125
- nii_path = example["nii_filepath"] # e.g., "IXI/sub-017/ses-01/anat/sub-017_ses-01_T1w.nii.gz"
126
-
127
- # Load the .nii.gz file with nibabel
128
- cur_t1_file = nib.load(nii_path)
129
- t1_data = cur_t1_file.get_fdata()
130
-
131
- # Preprocess: example sub-volume
132
- # (7:105, 8:132, :108) => shape: (98, 124, 108)
133
- t1_data = t1_data[7:105, 8:132, :108]
134
-
135
- # Normalize intensities
136
- t1_data = t1_data / t1_data.max()
137
-
138
- # Convert to PyTorch tensor, add two channel dims for volumetric interpolation
139
- # shape => (1, 1, 98, 124, 108)
140
- t1_tensor = torch.from_numpy(t1_data).float().unsqueeze(0).unsqueeze(0)
141
-
142
- # Downsample/resample to e.g. (96, 96, 96)
143
- img_downsample = F.interpolate(
144
- t1_tensor,
145
- size=(96, 96, 96),
146
- mode="trilinear",
147
- align_corners=False
148
- )
149
- # shape => (1, 96, 96, 96)
150
-
151
- # Squeeze out the channel dim if needed: shape => (96, 96, 96)
152
- img_downsample = img_downsample.squeeze(0)
153
-
154
- sample = {"img": img_downsample}
155
- return sample
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  ```
157
 
158
 
@@ -188,7 +205,7 @@ Scans were partitioned into train/validation/test datasets with a 80%/10%/10% sp
188
  ```
189
  @dataset{Radiata-Brain-Structure,
190
  author = {Jesse Brown and Clayton Young},
191
- title = {Brain-Structure: A Collection of Processed Structural MRI Scans},
192
  year = {2025},
193
  url = {https://huggingface.co/datasets/radiata-ai/brain-structure},
194
  note = {Version 1.0},
 
84
 
85
  # Example usage
86
  ```
87
+ # install Hugging Face Datasets
88
  pip install datasets
89
+
90
+ # optional installs: NiBabel and PyTorch
91
+ pip install nibabel
92
+ pip install torch torchvision
93
  ```
94
 
95
  ```
96
  # load datasets
97
  from datasets import load_dataset
98
 
99
+ ds_train = load_dataset("radiata-ai/brain-structure", split="train", trust_remote_code=True)
100
+ ds_val = load_dataset("radiata-ai/brain-structure", split="validation", trust_remote_code=True)
101
+ ds_test = load_dataset("radiata-ai/brain-structure", split="test", trust_remote_code=True)
102
  ```
103
 
104
  ```
105
  # example PyTorch processing of images
106
+ import nibabel as nib
107
  import torch
108
  import torch.nn.functional as F
109
  from torch.utils.data import Dataset
 
110
 
111
+ def preprocess_nifti(example):
112
  """
113
+ Loads a .nii.gz file, crops, normalizes, and resamples to 96^3.
114
+ Returns a numpy array (or tensor) in example["img"].
115
  """
116
+ nii_path = example["nii_filepath"]
117
+ # Load volume data
118
+ vol = nib.load(nii_path).get_fdata()
119
+
120
+ # Crop sub-volume
121
+ vol = vol[7:105, 8:132, :108] # shape: (98, 124, 108)
122
+
123
+ # Shift intensities to be non-negative
124
+ vol = vol + abs(vol.min())
125
+ # Normalize to [0,1]
126
+ vol = vol / vol.max()
127
+
128
+ # Convert to torch.Tensor: (1,1,D,H,W)
129
+ t_tensor = torch.from_numpy(vol).float().unsqueeze(0).unsqueeze(0)
130
+
131
+ # Scale factor based on (124 -> 96) for the y-dimension
132
+ scale_factor = 96 / 124
133
+ downsampled = F.interpolate(
134
+ t_tensor,
135
+ scale_factor=(scale_factor, scale_factor, scale_factor),
136
+ mode="trilinear",
137
+ align_corners=False
138
+ )
139
+
140
+ # Now pad each dimension to exactly 96 (symmetric padding)
141
+ _, _, d, h, w = downsampled.shape
142
+ pad_d = 96 - d
143
+ pad_h = 96 - h
144
+ pad_w = 96 - w
145
+ padding = (
146
+ pad_w // 2, pad_w - pad_w // 2,
147
+ pad_h // 2, pad_h - pad_h // 2,
148
+ pad_d // 2, pad_d - pad_d // 2
149
+ )
150
+ final_img = F.pad(downsampled, padding) # shape => (1, 1, 96, 96, 96)
151
+ final_img = final_img.squeeze(0)
152
+
153
+ # Store as numpy or keep as torch.Tensor
154
+ example["img"] = final_img.numpy()
155
+ return example
156
+ ```
157
+
158
+ ```
159
+ # Apply the preprocessing to each split
160
+ ds_train = ds_train.map(preprocess_nifti)
161
+ ds_val = ds_val.map(preprocess_nifti)
162
+ ds_test = ds_test.map(preprocess_nifti)
163
+
164
+ # Set the dataset format to return PyTorch tensors for the 'img' column
165
+ ds_train.set_format(type='torch', columns=['img'])
166
+ ds_val.set_format(type='torch', columns=['img'])
167
+ ds_test.set_format(type='torch', columns=['img'])
168
+
169
+ # Set up data loaders for model training
170
+ train_loader = DataLoader(ds_train, batch_size=16, shuffle=True)
171
+ val_loader = DataLoader(ds_val, batch_size=16, shuffle=False)
172
+ test_loader = DataLoader(ds_test, batch_size=16, shuffle=False)
173
  ```
174
 
175
 
 
205
  ```
206
  @dataset{Radiata-Brain-Structure,
207
  author = {Jesse Brown and Clayton Young},
208
+ title = {Brain-Structure: Processed Structural MRI Brain Scans Across the Lifespan},
209
  year = {2025},
210
  url = {https://huggingface.co/datasets/radiata-ai/brain-structure},
211
  note = {Version 1.0},
brain-structure.py CHANGED
@@ -102,6 +102,7 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
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 [
 
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
+ print(os.path.abspath(data_dir))
106
  logger.info(f"BrainStructure: scanning data in {os.path.abspath(data_dir)}")
107
 
108
  return [