Skip to content

Commit 6cfacf3

Browse files
committed
Simplify slice processing (#396)
1 parent 0f9279e commit 6cfacf3

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

src/openlifu/db/database.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,8 @@ def convert_dicom_to_nifti(input_path: PathLike, output_filepath: PathLike) -> N
104104
# so we reconstruct the 3D volume in the right order
105105
slices.sort(key=lambda x: x[0])
106106

107-
# stack into 3D volume
108-
if len(slices) == 1:
109-
# single slice needs extra dimension
110-
volume = slices[0][1][np.newaxis, :, :] if slices[0][1].ndim == 2 else slices[0][1]
111-
else:
112-
# multiple slices stacked along last axis
113-
volume = np.stack([s[1] for s in slices], axis=-1)
107+
# stack into 3D volume (handles both single and multiple slices)
108+
volume = np.stack([s[1] for s in slices], axis=-1)
114109

115110
# identity affine for now - could extract from dicom headers in the future
116111
affine = np.eye(4)
@@ -481,12 +476,12 @@ def write_volume(self, subject_id, volume_id, volume_name, volume_data_filepath,
481476
raise ValueError(f'Volume data filepath is a directory without DICOM files: {volume_data_filepath}')
482477

483478
# convert dicom to nifti if needed
484-
temp_nifti_file = None
479+
temp_nifti_path = None
485480
if is_dicom_file_or_directory(volume_data_filepath):
486481
self.logger.info(f"Detected DICOM input for volume {volume_id}, converting to NIfTI format")
487-
temp_nifti_file = tempfile.NamedTemporaryFile(suffix='.nii.gz', delete=False)
488-
temp_nifti_path = Path(temp_nifti_file.name)
489-
temp_nifti_file.close()
482+
temp_file = tempfile.NamedTemporaryFile(suffix='.nii.gz', delete=False)
483+
temp_nifti_path = Path(temp_file.name)
484+
temp_file.close()
490485

491486
try:
492487
convert_dicom_to_nifti(volume_data_filepath, temp_nifti_path)
@@ -526,10 +521,8 @@ def write_volume(self, subject_id, volume_id, volume_name, volume_data_filepath,
526521
self.logger.info(f"Added volume with ID {volume_id} for subject {subject_id} to the database.")
527522
finally:
528523
# cleanup temp nifti file
529-
if temp_nifti_file is not None:
530-
temp_path = Path(temp_nifti_file.name)
531-
if temp_path.exists():
532-
temp_path.unlink()
524+
if temp_nifti_path is not None and temp_nifti_path.exists():
525+
temp_nifti_path.unlink()
533526

534527
def write_photocollection(self, subject_id, session_id, reference_number: str, photo_paths: List[PathLike], on_conflict=OnConflictOpts.ERROR):
535528
""" Writes a photocollection to database and copies the associated

0 commit comments

Comments
 (0)