Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Aug 28, 2024
1 parent d9d249f commit e114f21
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 55 deletions.
88 changes: 46 additions & 42 deletions SIRF_data_preparation/Siemens_mMR_ACR/VOI_prep.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,95 @@
#%% file to prepare VOIs from the original ACR NEMA VOIs supplied by Pawel Markiewicz
#%%
import numpy as np
import matplotlib.pyplot as plt
# %% file to prepare VOIs from the original ACR NEMA VOIs supplied by Pawel Markiewicz
# %%
import os
import sys

import matplotlib.pyplot as plt
import numpy as np

import sirf.STIR as STIR
from sirf.Utilities import examples_data_path
from scipy.ndimage import binary_erosion
from pathlib import Path
import SIRF_data_preparation.data_QC as data_QC
from SIRF_data_preparation.data_utilities import the_data_path, the_orgdata_path
from SIRF_data_preparation.dataset_settings import get_settings
import SIRF_data_preparation.data_QC as data_QC
#%%

# %%
scanID = 'Siemens_mMR_ACR'
org_VOI_path = the_orgdata_path(scanID, 'output', 'sampling_masks')
data_path = the_data_path(scanID)
output_path = the_data_path(scanID, 'PETRIC')
os.makedirs(output_path, exist_ok=True)
settings = get_settings(scanID)
slices = settings.slices
#%%
# %%
OSEM_image = STIR.ImageData(os.path.join(data_path, 'OSEM_image.hv'))
cmax = OSEM_image.max()
#%% read in original VOIs
# %% read in original VOIs
orgVOIs = STIR.ImageData(os.path.join(org_VOI_path, 'acr-all-sampling-0-2mm_dipy.nii'))
data_QC.plot_image(orgVOIs, **slices)
#%%
# %%
orgVOIs_arr = orgVOIs.as_array()
print(np.unique(orgVOIs_arr))
#%% output


# %% output
# [ 0, 10-29, 40-81, 90- 101, 300- 317]
#%%
# %%
def plotMask(mask):
plt.figure()
plt.subplot(141)
plt.imshow(mask[:,109,:])
plt.imshow(mask[:, 109, :])
plt.subplot(142)
plt.imshow(mask[85,:,:])
plt.imshow(mask[85, :, :])
plt.subplot(143)
plt.imshow(mask[99,:,:])
plt.imshow(mask[99, :, :])
plt.subplot(144)
plt.imshow(mask[40,:,:])
#%% background mask: slices in uniform part (idx 300-317), taking slightly eroded mask
mask = (orgVOIs_arr>=300) * (orgVOIs_arr<316)
plt.imshow(mask[40, :, :])


# %% background mask: slices in uniform part (idx 300-317), taking slightly eroded mask
mask = (orgVOIs_arr >= 300) * (orgVOIs_arr < 316)
plotMask(mask)
#%%
# %%
background_mask = OSEM_image.clone()
background_mask.fill(mask)
#%% 6 cylinder masks
mask = (orgVOIs_arr>=10) * (orgVOIs_arr<101)
# %% 6 cylinder masks
mask = (orgVOIs_arr >= 10) * (orgVOIs_arr < 101)
plotMask(mask)
#%% cold cylinder mask
mask = (orgVOIs_arr>=90) * (orgVOIs_arr<100)
# %% cold cylinder mask
mask = (orgVOIs_arr >= 90) * (orgVOIs_arr < 100)
plotMask(mask)
cold_cyl_mask = OSEM_image.clone()
cold_cyl_mask.fill(mask)
#%% faint hot cylinder mask
mask = (orgVOIs_arr>=20) * (orgVOIs_arr<29)
# %% faint hot cylinder mask
mask = (orgVOIs_arr >= 20) * (orgVOIs_arr < 29)
plotMask(mask)
hot_cyl_mask = OSEM_image.clone()
hot_cyl_mask.fill(mask)
#%% Jasczcak part, not available so derive from "background mask"
mask = (orgVOIs_arr>=300) * (orgVOIs_arr<316)
# %% Jasczcak part, not available so derive from "background mask"
mask = (orgVOIs_arr >= 300) * (orgVOIs_arr < 316)
# get single cylinder from a slice in the background
oneslice = mask[85,:,:].copy()
mask[35:45,:,:] = oneslice[:,:]
mask[46:127,:,:] = False
oneslice = mask[85, :, :].copy()
mask[35:45, :, :] = oneslice[:, :]
mask[46:127, :, :] = False
plotMask(mask)
rods_mask = OSEM_image.clone()
rods_mask.fill(mask)
#%% whole phantom
mask = OSEM_image.as_array() > cmax/20
# %% whole phantom
mask = OSEM_image.as_array() > cmax / 20
plotMask(mask)
whole_object_mask = OSEM_image.clone()
whole_object_mask.fill(mask)
#%%
# %%
plotMask(OSEM_image.as_array())
#%% write PETRIC VOIs
# %% write PETRIC VOIs
whole_object_mask.write(os.path.join(output_path, 'VOI_whole_object.hv'))
background_mask.write(os.path.join(output_path, 'VOI_background.hv'))
cold_cyl_mask.write(os.path.join(output_path, 'VOI_cold_cylinder.hv'))
hot_cyl_mask.write(os.path.join(output_path, 'VOI_hot_cylinder.hv'))
rods_mask.write(os.path.join(output_path, 'VOI_rods.hv'))

#%%
# %%
VOIs = (whole_object_mask, background_mask, cold_cyl_mask, hot_cyl_mask, rods_mask)
#%%
[ data_QC.plot_image(VOI, **slices) for VOI in VOIs]
#%%
data_QC.VOI_checks(['VOI_whole_object', 'VOI_background', 'VOI_cold_cylinder', 'VOI_hot_cylinder', 'VOI_rods'], OSEM_image, srcdir=output_path, **slices)
# %%
[data_QC.plot_image(VOI, **slices) for VOI in VOIs]
# %%
data_QC.VOI_checks(['VOI_whole_object', 'VOI_background', 'VOI_cold_cylinder', 'VOI_hot_cylinder', 'VOI_rods'],
OSEM_image, srcdir=output_path, **slices)
3 changes: 2 additions & 1 deletion SIRF_data_preparation/Siemens_mMR_ACR/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

from zenodo_get import zenodo_get

from sirf.Utilities import examples_data_path

# %%
from SIRF_data_preparation.data_utilities import the_data_path
from sirf.Utilities import examples_data_path

sirf_data_path = os.path.join(examples_data_path('PET'), 'mMR')

Expand Down
3 changes: 2 additions & 1 deletion SIRF_data_preparation/Siemens_mMR_ACR/register_mumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import sirf.Reg as Reg
import sirf.STIR as STIR
from SIRF_data_preparation.data_utilities import the_data_path, the_orgdata_path
from SIRF_data_preparation.data_utilities import the_orgdata_path

# %% set paths filenames
scanID = 'Siemens_mMR_ACR'
intermediate_data_path = the_orgdata_path(scanID, 'processing')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from pathlib import Path

import matplotlib.pyplot as plt
import numpy
from data_QC import VOI_mean, plot_image
from scipy import ndimage
from data_QC import VOI_mean
from scipy.ndimage import binary_erosion

import sirf.STIR as STIR
Expand Down Expand Up @@ -78,9 +76,9 @@
plt.figure()
data_QC.plot_image(reference_image, **slices, alpha=allVOIs)

#%%
VOI_checks(['VOI_whole_object', 'VOI_sphere5'], OSEM_image, srcdir=os.path.join(datadir, 'PETRIC'), **slices)
#%%
[ VOI_mean(OSEM_image, VOI) for VOI in VOIs]
#%%
[ VOI_mean(reference_image, VOI) for VOI in VOIs]
# %%
data_QC.VOI_checks(['VOI_whole_object', 'VOI_sphere5'], OSEM_image, srcdir=os.path.join(datadir, 'PETRIC'), **slices)
# %%
[VOI_mean(OSEM_image, VOI) for VOI in VOIs]
# %%
[VOI_mean(reference_image, VOI) for VOI in VOIs]
3 changes: 2 additions & 1 deletion SIRF_data_preparation/create_initial_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<data_path> path to data files
Options:
-t <template_image>, --template_image=<template_image> filename (relative to <data_path>) of image to use for data sizes [default: PETRIC/VOI_whole_phantom.hv]
-t <template_image>, --template_image=<template_image> filename (relative to <data_path>) of image to use
for data sizes [default: PETRIC/VOI_whole_phantom.hv]
-s <xy-size>, --xy-size=<xy-size> force xy-size (do not use when using VOIs as init) [default: -1]
-S <subsets>, --subsets=<subsets> number of subsets [default: 2]
-i <subiterations>, --subiterations=<subiterations> number of sub-iterations [default: 14]
Expand Down
2 changes: 2 additions & 0 deletions SIRF_data_preparation/data_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def the_data_path(*folders):
'''
return os.path.join(DATA_PATH, *folders)


def the_orgdata_path(*folders):
'''
Returns the path to original data (for downloads/processing)
Expand All @@ -37,6 +38,7 @@ def the_orgdata_path(*folders):
'''
return os.path.join(ORG_DATA_PATH, *folders)


def fix_siemens_norm_EOL(in_filename, out_filename):
with open(in_filename, mode="rb") as f:
data = bytearray(f.read())
Expand Down
2 changes: 1 addition & 1 deletion SIRF_data_preparation/dataset_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_settings(scanID: str):
slices = {'transverse_slice': 72, 'coronal_slice': 109} # , 'sagittal_slice': 89}
num_subsets = 7
elif scanID == 'Siemens_mMR_ACR':
slices = { 'transverse_slice': 99}
slices = {'transverse_slice': 99}
num_subsets = 7
elif scanID == 'NeuroLF_Hoffman_Dataset':
slices = {'transverse_slice': 72}
Expand Down

0 comments on commit e114f21

Please sign in to comment.