Skip to content

Commit

Permalink
Add ability to resample subject while reading (#363)
Browse files Browse the repository at this point in the history
* Enable diffdrr.data.Subject to be resampled

* Fix docs
  • Loading branch information
eigenvivek authored Feb 7, 2025
1 parent f4d3dcc commit b634b50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions diffdrr/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def read(
fiducials: torch.Tensor = None, # 3D fiducials in world coordinates
transform: RigidTransform = None, # RigidTransform to apply to the volume's affine
center_volume: bool = True, # Move the volume's isocenter to the world origin
resample_target=None, # Resampling resolution argument passed to torchio.transforms.Resample
**kwargs, # Any additional information to be stored in the torchio.Subject
) -> Subject:
"""
Expand Down Expand Up @@ -172,6 +173,11 @@ def read(
except:
subject.density.data = subject.density.data * mask

# Apply resample
if resample_target is not None:
resample = Resample(resample_target)
subject = resample(subject)

return subject

# %% ../notebooks/api/03_data.ipynb 7
Expand Down
7 changes: 7 additions & 0 deletions notebooks/api/03_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"from a variety of formats and orientations. We canonicalize all volumes to \n",
"the RAS+ coordinate space. In addition to reading an input volume, you can also pass\n",
"the following to `diffdrr.data.read` when loading a subject:\n",
"\n",
"- `labelmap` : a 3D segmentation of the input volume\n",
"- `labels` : a subset of structures from the labelmap that you want to render\n",
"- `orientation` : a frame-of-reference change for the C-arm (currently, \"AP\" and \"PA\" are supported)\n",
Expand Down Expand Up @@ -121,6 +122,7 @@
" fiducials: torch.Tensor = None, # 3D fiducials in world coordinates\n",
" transform: RigidTransform = None, # RigidTransform to apply to the volume's affine\n",
" center_volume: bool = True, # Move the volume's isocenter to the world origin\n",
" resample_target=None, # Resampling resolution argument passed to torchio.transforms.Resample\n",
" **kwargs, # Any additional information to be stored in the torchio.Subject\n",
") -> Subject:\n",
" \"\"\"\n",
Expand Down Expand Up @@ -243,6 +245,11 @@
" except:\n",
" subject.density.data = subject.density.data * mask\n",
"\n",
" # Apply resample\n",
" if resample_target is not None:\n",
" resample = Resample(resample_target)\n",
" subject = resample(subject)\n",
"\n",
" return subject"
]
},
Expand Down
3 changes: 2 additions & 1 deletion notebooks/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"\n",
"```zsh\n",
"git clone https://github.com/eigenvivek/DiffDRR.git --depth 1\n",
"pip install -e 'DiffDRR/[dev]'"
"pip install -e 'DiffDRR/[dev]'\n",
"```"
]
},
{
Expand Down

0 comments on commit b634b50

Please # to comment.