Skip to content

Commit

Permalink
Merge pull request #150 from qurit/development
Browse files Browse the repository at this point in the history
dicom save bug
  • Loading branch information
lukepolson committed Apr 9, 2024
2 parents 5bd256e + ae2ca40 commit f14958d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "pytomography"
version = "2.0.4"
version = "2.0.5"

authors = [
{ name="Luke Polson", email="lukepolson@outlook.com" },
Expand Down
23 changes: 12 additions & 11 deletions src/pytomography/io/SPECT/dicom.py
@@ -1,5 +1,6 @@
from __future__ import annotations
import warnings
import copy
import os
import collections.abc
from collections.abc import Sequence
Expand Down Expand Up @@ -574,12 +575,13 @@ def save_dcm(
recon_name (str): Type of reconstruction performed. Obtained from the `recon_method_str` attribute of a reconstruction algorithm class.
return_ds (bool): If true, returns the DICOM dataset objects instead of saving to file. Defaults to False.
"""
try:
Path(save_path).resolve().mkdir(parents=True, exist_ok=False)
except:
raise Exception(
f"Folder {save_path} already exists; new folder name is required."
)
if not return_ds:
try:
Path(save_path).resolve().mkdir(parents=True, exist_ok=False)
except:
raise Exception(
f"Folder {save_path} already exists; new folder name is required."
)
# Convert tensor image to numpy array
pixel_data = torch.permute(object.squeeze(),(2,1,0)).cpu().numpy()
scale_factor = (2**16 - 1) / pixel_data.max()
Expand Down Expand Up @@ -620,19 +622,18 @@ def save_dcm(
dss = []
for i in range(pixel_data.shape[0]):
# Load existing DICOM file
ds_i = ds.copy()
ds_i = copy.deepcopy(ds)
ds_i.InstanceNumber = i + 1
ds_i.ImagePositionPatient = [Sx, Sy, Sz + i * dz]
# Create SOP Instance UID unique to slice
SOP_instance_UID_slice = f"{ds_i.SOPInstanceUID[:-3]}{i+1:03d}"
ds_i.SOPInstanceUID = SOP_instance_UID_slice
ds_i.file_meta.MediaStorageSOPInstanceUID = SOP_instance_UID_slice
ds_i.SOPInstanceUID = f"{ds.SOPInstanceUID[:-3]}{i+1:03d}"
ds_i.file_meta.MediaStorageSOPInstanceUID = ds_i.SOPInstanceUID
# Set the pixel data
ds_i.PixelData = pixel_data[i].tobytes()
dss.append(ds_i)
if return_ds:
return dss
else:
for ds_i in dss:
ds_i.save_as(os.path.join(save_path, f'{ds.SOPInstanceUID}.dcm'))
ds_i.save_as(os.path.join(save_path, f'{ds_i.SOPInstanceUID}.dcm'))

0 comments on commit f14958d

Please sign in to comment.