Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into Siemens_mMR_ACR2
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisThielemans committed Aug 22, 2024
2 parents 92d40a7 + 513473a commit 6fdda40
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from petric import MetricsWithTimeout, get_data, SRCDIR
from petric import SRCDIR, MetricsWithTimeout, get_data
from sirf.contrib.BSREM.BSREM import BSREM1
from sirf.contrib.partitioner import partitioner
from SIRF_data_preparation.dataset_settings import get_settings

scanID = "NeuroLF_Hoffman_Dataset"

from SIRF_data_preparation.dataset_settings import get_settings
settings = get_settings(scanID)
slices = settings.slices
num_subsets = settings.num_subsets
outdir=f"./output/{scanID}/BSREM"
outdir1=f"./output/{scanID}/BSREM_cont"
outdir = f"./output/{scanID}/BSREM"
outdir1 = f"./output/{scanID}/BSREM_cont"

data = get_data(srcdir=SRCDIR / scanID, outdir=outdir)
data_sub, acq_models, obj_funs = partitioner.data_partition(data.acquired_data, data.additive_term, data.mult_factors,
Expand All @@ -22,5 +21,5 @@

algo = BSREM1(data_sub, obj_funs, initial=data.OSEM_image, initial_step_size=.3, relaxation_eta=.01,
update_objective_interval=80)
#%%
algo.run(15000, callbacks=[MetricsWithTimeout(**slices, outdir=outdir, seconds=3600*100)])
# %%
algo.run(15000, callbacks=[MetricsWithTimeout(**slices, outdir=outdir, seconds=3600 * 100)])
100 changes: 54 additions & 46 deletions SIRF_data_preparation/NeuroLF_Hoffman_Dataset/NeuroLF_VOIs.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
#%%
import numpy
import matplotlib.pyplot as plt
# %%
import os
import sys
import sirf.STIR as STIR
from sirf.Utilities import examples_data_path
from scipy.ndimage import binary_erosion
from pathlib import Path
#%%
#%matplotlib ipympl
#%%

import matplotlib.pyplot as plt
from scipy.ndimage import binary_erosion

import sirf.STIR as STIR
from petric import SRCDIR
from SIRF_data_preparation import data_QC

# %%
# %matplotlib ipympl
# %%
STIR.AcquisitionData.set_storage_scheme('memory')
# set-up redirection of STIR messages to files
#_ = STIR.MessageRedirector('BSREM_info.txt', 'BSREM_warnings.txt', 'BSREM_errors.txt')
# _ = STIR.MessageRedirector('BSREM_info.txt', 'BSREM_warnings.txt', 'BSREM_errors.txt')
# fewer messages from STIR and SIRF (set to higher value to diagnose have problems)
STIR.set_verbosity(0)
#%%


# %%
def plot_image(image, vmin=0, vmax=None):
if vmax is None:
vmax=image.max()
vmax = image.max()
plt.figure()
plt.subplot(131)
plt.imshow(image.as_array()[im_slice,:,:], vmin=vmin, vmax=vmax)
plt.imshow(image.as_array()[im_slice, :, :], vmin=vmin, vmax=vmax)
plt.subplot(132)
plt.imshow(image.as_array()[:,cor_slice,:], vmin=vmin, vmax=vmax)
plt.imshow(image.as_array()[:, cor_slice, :], vmin=vmin, vmax=vmax)
plt.subplot(133)
plt.imshow(image.as_array()[:,:, sag_slice],vmin=vmin, vmax=vmax)
plt.imshow(image.as_array()[:, :, sag_slice], vmin=vmin, vmax=vmax)
plt.show()

#%% read

# %% read
if not SRCDIR.is_dir():
PETRICDIR = Path('~/devel/PETRIC').expanduser()
SRCDIR = PETRICDIR / 'data'
datadir = str(SRCDIR / 'NeuroLF_Hoffman_Dataset') + '/'

#%%
OSEM_image = STIR.ImageData(datadir+'OSEM_image.hv')
# %%
OSEM_image = STIR.ImageData(datadir + 'OSEM_image.hv')
im_slice = 72
cor_slice = OSEM_image.dimensions()[1]//2
sag_slice = OSEM_image.dimensions()[2]//2
cor_slice = OSEM_image.dimensions()[1] // 2
sag_slice = OSEM_image.dimensions()[2] // 2
cmax = OSEM_image.max()

#%%
reference_image = STIR.ImageData(datadir+'reference_image.hv')
#%% read in original VOIs
whole_object_mask = STIR.ImageData(datadir+'whole_phantom.hv')
#whole_object_mask = STIR.ImageData(datadir+'VOI_whole_object.hv')
# %%
reference_image = STIR.ImageData(datadir + 'reference_image.hv')
# %% read in original VOIs
whole_object_mask = STIR.ImageData(datadir + 'whole_phantom.hv')
# whole_object_mask = STIR.ImageData(datadir+'VOI_whole_object.hv')
# from README.md
# 1: ventricles, 2: white matter, 3: grey matter
allVOIs=STIR.ImageData(datadir+'vois_ventricles_white_grey.hv')
#%% create PETRIC VOIs
allVOIs = STIR.ImageData(datadir + 'vois_ventricles_white_grey.hv')
# %% create PETRIC VOIs
background_mask = whole_object_mask.clone()
background_mask.fill(binary_erosion(allVOIs.as_array() == 2, iterations=2))
VOI1_mask = whole_object_mask.clone()
Expand All @@ -58,30 +63,33 @@ def plot_image(image, vmin=0, vmax=None):
VOI2_mask.fill(binary_erosion(allVOIs.as_array() == 2, iterations=1))
VOI3_mask = whole_object_mask.clone()
VOI3_mask.fill(allVOIs.as_array() == 3)
#%% write PETRIC VOIs
whole_object_mask.write(datadir+'PETRIC/VOI_whole_object.hv')
background_mask.write(datadir+'PETRIC/VOI_background.hv')
VOI1_mask.write(datadir+'PETRIC/VOI_ventricles.hv')
VOI2_mask.write(datadir+'PETRIC/VOI_WM.hv')
VOI3_mask.write(datadir+'PETRIC/VOI_GM.hv')
#%%
# %% write PETRIC VOIs
whole_object_mask.write(datadir + 'PETRIC/VOI_whole_object.hv')
background_mask.write(datadir + 'PETRIC/VOI_background.hv')
VOI1_mask.write(datadir + 'PETRIC/VOI_ventricles.hv')
VOI2_mask.write(datadir + 'PETRIC/VOI_WM.hv')
VOI3_mask.write(datadir + 'PETRIC/VOI_GM.hv')
# %%
VOIs = (whole_object_mask, background_mask, VOI1_mask, VOI2_mask, VOI3_mask)
#%%
[ plot_image(VOI) for VOI in VOIs]
#%%
[ plot_image(VOI) for VOI in VOIs]
#%%
# %%
[plot_image(VOI) for VOI in VOIs]
# %%
[plot_image(VOI) for VOI in VOIs]


# %%
def VOI_mean(image, VOI):
return float((image*VOI).sum() / VOI.sum())
return float((image * VOI).sum() / VOI.sum())

#%%
[ VOI_mean(OSEM_image, VOI) for VOI in VOIs]
#%%
[ VOI_mean(reference_image, VOI) for VOI in VOIs]

# %%
[VOI_mean(OSEM_image, VOI) for VOI in VOIs]
# %%
[VOI_mean(reference_image, VOI) for VOI in VOIs]

# %%
os.chdir(datadir)
#%%
# %%
data_QC.main()
# %%
voidir = Path(datadir) / 'PETRIC'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from petric import MetricsWithTimeout, get_data, SRCDIR
import os

import sirf.STIR as STIR
from petric import SRCDIR, MetricsWithTimeout, get_data
from sirf.contrib.BSREM.BSREM import BSREM1
from sirf.contrib.partitioner import partitioner
import sirf.STIR as STIR
import os
from SIRF_data_preparation.dataset_settings import get_settings

scanID = 'Siemens_Vision600_thorax'

from SIRF_data_preparation.dataset_settings import get_settings
settings = get_settings(scanID)
slices = settings.slices
num_subsets = settings.num_subsets
outdir=f"./output/{scanID}/BSREM"
outdir1=f"./output/{scanID}/BSREM_cont"
outdir = f"./output/{scanID}/BSREM"
outdir1 = f"./output/{scanID}/BSREM_cont"

data = get_data(srcdir=SRCDIR / scanID, outdir=outdir)
data_sub, acq_models, obj_funs = partitioner.data_partition(data.acquired_data, data.additive_term, data.mult_factors,
Expand All @@ -27,13 +27,13 @@
del data
try:
image1000 = STIR.ImageData(os.path.join(outdir, 'iter_1000.hv'))
except:
except Exception:
algo = BSREM1(data_sub, obj_funs, initial=OSEM_image, initial_step_size=.3, relaxation_eta=.01,
update_objective_interval=5)
algo.run(1005, callbacks=[MetricsWithTimeout(seconds=3600*34, outdir=outdir)])
algo.run(1005, callbacks=[MetricsWithTimeout(seconds=3600 * 34, outdir=outdir)])

# restart

algo = BSREM1(data_sub, obj_funs, initial=image1000, initial_step_size=.3, relaxation_eta=.01,
update_objective_interval=5)
algo.run(9000, callbacks=[MetricsWithTimeout(seconds=3600*34, outdir=outdir1)])
algo.run(9000, callbacks=[MetricsWithTimeout(seconds=3600 * 34, outdir=outdir1)])
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ stir_math ../PETRIC/VOI_whole_object.hv phantom.hv
stir_math ../PETRIC/VOI_background.hv background.hv
for r in 1 4 7; do
stir_math ../PETRIC/VOI_${r}.hv VOI${r}_ds.hv
done
done
Empty file.
Empty file.
11 changes: 5 additions & 6 deletions SIRF_data_preparation/Siemens_mMR_NEMA_IQ/BSREM_mMR_NEMA_IQ.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from petric import MetricsWithTimeout, get_data, SRCDIR
from petric import SRCDIR, MetricsWithTimeout, get_data
from sirf.contrib.BSREM.BSREM import BSREM1
from sirf.contrib.partitioner import partitioner
from SIRF_data_preparation.dataset_settings import get_settings

scanID = "Siemens_mMR_NEMA_IQ"

from SIRF_data_preparation.dataset_settings import get_settings
settings = get_settings(scanID)
slices = settings.slices
num_subsets = settings.num_subsets
outdir=f"./output/{scanID}/BSREM"
outdir = f"./output/{scanID}/BSREM"

data = get_data(srcdir=SRCDIR / scanID, outdir=outdir)
data_sub, acq_models, obj_funs = partitioner.data_partition(data.acquired_data, data.additive_term, data.mult_factors,
Expand All @@ -22,5 +21,5 @@

algo = BSREM1(data_sub, obj_funs, initial=data.OSEM_image, initial_step_size=.3, relaxation_eta=.005,
update_objective_interval=80)
#%%
algo.run(15000, callbacks=[MetricsWithTimeout(**slices, outdir=outdir, seconds=3600*100)])
# %%
algo.run(15000, callbacks=[MetricsWithTimeout(**slices, outdir=outdir, seconds=3600 * 100)])
111 changes: 51 additions & 60 deletions SIRF_data_preparation/Siemens_mMR_NEMA_IQ/Siemens_mMR_NEMA_VOIs.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,86 @@
#%%
%load_ext autoreload
#%%
2
# #%%
import numpy
import matplotlib.pyplot as plt
# %%
# %load_ext autoreload
# %%
import os
import sys
import sirf.STIR as STIR
from sirf.Utilities import examples_data_path
from scipy.ndimage import binary_erosion
from pathlib import Path
#%%
%autoreload 2

#%%
from petric import get_data, SRCDIR
import data_QC
#%%
#%matplotlib ipympl
#%%
import matplotlib.pyplot as plt
import numpy
from data_QC import VOI_mean, plot_image
from scipy import ndimage
from scipy.ndimage import binary_erosion

import sirf.STIR as STIR
from petric import SRCDIR
from SIRF_data_preparation import data_QC

# %%
# %autoreload 2

# %%
# %matplotlib ipympl
# %%
STIR.AcquisitionData.set_storage_scheme('memory')
# set-up redirection of STIR messages to files
#_ = STIR.MessageRedirector('BSREM_info.txt', 'BSREM_warnings.txt', 'BSREM_errors.txt')
# _ = STIR.MessageRedirector('BSREM_info.txt', 'BSREM_warnings.txt', 'BSREM_errors.txt')
# fewer messages from STIR and SIRF (set to higher value to diagnose have problems)
STIR.set_verbosity(0)

#%% read
# %% read
if not SRCDIR.is_dir():
PETRICDIR = Path('~/devel/PETRIC').expanduser()
SRCDIR = PETRICDIR / 'data'
datadir = str(SRCDIR /'Siemens_mMR_NEMA_IQ/')
#%%
datadir = str(SRCDIR / 'Siemens_mMR_NEMA_IQ/')
# %%
OSEM_image = STIR.ImageData(datadir + 'OSEM_image.hv')
im_slice = 72
cor_slice = 109
sag_slice = OSEM_image.dimensions()[2]//2
sag_slice = OSEM_image.dimensions()[2] // 2
cmax = OSEM_image.max()
#%%
slices = {'transverse_slice':72, 'coronal_slice':109}
#%% read in original VOIs
orgVOIs=[]
for i in range(1,8):
orgVOIs.append(STIR.ImageData(datadir+f"S{i}.hv"))
data_QC.plot_image(orgVOIs[i-1], **slices)
#%%
reference_image = STIR.ImageData(datadir+'PETRIC/reference_image.hv')
# %%
slices = {'transverse_slice': 72, 'coronal_slice': 109}
# %% read in original VOIs
orgVOIs = []
for i in range(1, 8):
orgVOIs.append(STIR.ImageData(datadir + f"S{i}.hv"))
data_QC.plot_image(orgVOIs[i - 1], **slices)
# %%
reference_image = STIR.ImageData(datadir + 'PETRIC/reference_image.hv')
data_QC.plot_image(reference_image, **slices)
#%% create PETRIC VOIs
# %% create PETRIC VOIs
whole_object_mask = reference_image.clone()
whole_object_mask.fill(binary_erosion(reference_image.as_array()> reference_image.max()*.05, iterations=2))
whole_object_mask.fill(binary_erosion(reference_image.as_array() > reference_image.max() * .05, iterations=2))
data_QC.plot_image(whole_object_mask, **slices)
#%%
# %%
background_mask = orgVOIs[6]
VOI1_mask = orgVOIs[4]
VOI2_mask = orgVOIs[2]
VOI3_mask = orgVOIs[0]
#%% write PETRIC VOIs
whole_object_mask.write(datadir+'PETRIC/VOI_whole_object.hv')
background_mask.write(datadir+'PETRIC/VOI_background.hv')
VOI1_mask.write(datadir+'PETRIC/VOI_sphere5.hv')
VOI2_mask.write(datadir+'PETRIC/VOI_sphere3.hv')
VOI3_mask.write(datadir+'PETRIC/VOI_sphere1.hv')
#%%
# %% write PETRIC VOIs
whole_object_mask.write(datadir + 'PETRIC/VOI_whole_object.hv')
background_mask.write(datadir + 'PETRIC/VOI_background.hv')
VOI1_mask.write(datadir + 'PETRIC/VOI_sphere5.hv')
VOI2_mask.write(datadir + 'PETRIC/VOI_sphere3.hv')
VOI3_mask.write(datadir + 'PETRIC/VOI_sphere1.hv')
# %%
VOIs = (whole_object_mask, background_mask, VOI1_mask, VOI2_mask, VOI3_mask)
#%%
[ data_QC.plot_image(VOI, **slices) for VOI in VOIs]
#%%
allVOIs = whole_object_mask.clone()*.2
# %%
[data_QC.plot_image(VOI, **slices) for VOI in VOIs]
# %%
allVOIs = whole_object_mask.clone() * .2
allVOIs += background_mask
for VOI in VOIs:
allVOIs+= VOI
allVOIs += VOI
allVOIs /= allVOIs.max()
#%%
# %%
plt.figure()
data_QC.plot_image(reference_image, **slices)
plt.figure()
data_QC.plot_image(reference_image, **slices, alpha=allVOIs)
#%%
from data_QC import plot_image_if_exists, VOI_checks
from data_QC import plot_image
from data_QC import VOI_mean

from scipy import ndimage

#%%
VOI_checks(['VOI_whole_object', 'VOI_sphere5'], OSEM_image, srcdir=os.path.join(datadir, 'PETRIC'), **slices)
#%%
[ data_QC.VOI_mean(OSEM_image, VOI) for VOI in VOIs]
[ VOI_mean(OSEM_image, VOI) for VOI in VOIs]
#%%
[ data_QC.VOI_mean(reference_image, VOI) for VOI in VOIs]
# %%
data_QC.main(['--srcdir', datadir])
# %%
[ VOI_mean(reference_image, VOI) for VOI in VOIs]
Empty file.
Loading

0 comments on commit 6fdda40

Please sign in to comment.