Skip to content

Commit

Permalink
Export to single nifti
Browse files Browse the repository at this point in the history
  • Loading branch information
uw0s committed Apr 1, 2024
1 parent f958958 commit 0c67944
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 15 additions & 3 deletions python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class SegData(BaseModel):
filepath: str
classes: list[str]
export2nifti: bool
n_dicom: int
dcm_idx: int


class BoxData(BaseModel):
Expand Down Expand Up @@ -434,10 +436,20 @@ async def export_masks(data: SegData) -> ModifyResponse:
modified_dcm.SegmentSequence[0].SegmentDescription = ";".join(data.classes)
modified_dcm.save_as(fp)
h, w = modified_dcm.SegmentSequence[0].Rows, modified_dcm.SegmentSequence[0].Columns
arr = np.frombuffer(pixel_data, dtype=np.uint8).reshape((h, w))
nii_file = Path(fp).parent / "masks.nii.gz"
if data.export2nifti:
niifp = ".".join(fp.split(".")[:-1]) + ".nii"
arr = np.frombuffer(pixel_data, dtype=np.uint8).reshape((h, w))
nib.save(nib.Nifti1Image(arr, np.eye(4)), niifp) # type: ignore[attr-defined, no-untyped-call]
if nii_file.exists():
nii_img = nib.load(nii_file) # type: ignore[attr-defined]
else:
nii_img_data = np.zeros((h, w, data.n_dicom), dtype=np.uint8)
nii_img = nib.Nifti1Image(nii_img_data, np.eye(4)) # type: ignore[attr-defined,no-untyped-call]
if data.dcm_idx < data.n_dicom:
nii_img_data = nii_img.get_fdata() # type: ignore[attr-defined]
nii_img_data[:, :, data.dcm_idx] = arr
nii_img = nib.Nifti1Image(nii_img_data, np.eye(4)) # type: ignore[attr-defined,no-untyped-call]

nib.save(nii_img, nii_file) # type: ignore[attr-defined]
return ModifyResponse(success=True)


Expand Down
4 changes: 3 additions & 1 deletion python/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,9 @@ async function export_masks() {
pixel_data: canvastobase64(),
filepath: dicom_data_fps[dcm_idx_][1],
classes: classesMap,
export2nifti: ExportAnnot2Nifti.checked
export2nifti: ExportAnnot2Nifti.checked,
n_dicom: n_uploaded_files,
dcm_idx: dcm_idx_
};
const modify_response = await fetch(
'/export_masks/',
Expand Down

0 comments on commit 0c67944

Please sign in to comment.