Skip to content

Commit 4e9666a

Browse files
adding a Python demo for EM step with gradient (#1620)
* adding a demo for EM step with gradient * adding reconddemo to Actions workflow * disable appveyor test as it seems to hang (maybe because of plotting) --------- Co-authored-by: Kris Thielemans <[email protected]>
1 parent 19ef64e commit 4e9666a

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

.appveyor.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ test_script:
6868
- cd ..\src
6969
- "set PYTHONPATH=C:\\projects\\stir\\install\\python"
7070
- python -m pytest .
71+
- echo "Running Python examples"
72+
- cd ..\examples\python
73+
- rem Disabled as fails, probably because of plotting
74+
- rem python recon_demo_gradient.py
7175
- conda deactivate
7276

7377
on_finish:

.github/workflows/build-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,5 +539,7 @@ jobs:
539539
;;
540540
(*)
541541
python -m pytest .
542+
cd ../examples/python
543+
python recon_demo_gradient.py
542544
;;
543545
esac
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# %%
2+
import stir
3+
import stirextra
4+
import matplotlib.pyplot as plt
5+
import os
6+
7+
# go to directory with input files
8+
os.chdir('../recon_demo')
9+
10+
# %%
11+
# initialise reconstruction object
12+
# we will do this here via a .par file
13+
recon = stir.OSMAPOSLReconstruction3DFloat('recon_demo_OSEM.par')
14+
# now modify a few settings from in Python for illustration
15+
recon.set_num_subsets(2)
16+
num_subiterations = 4
17+
# set filenames to save subset sensitivities (for illustration purposes)
18+
poissonobj = recon.get_objective_function()
19+
poissonobj.set_subsensitivity_filenames('sens_subset{}.hv')
20+
poissonobj.set_recompute_sensitivity(True)
21+
22+
23+
# %%
24+
# construct image related to the data to reconstruct
25+
projdata=stir.ProjData.read_from_file('smalllong.hs');
26+
# use smaller voxels than the default
27+
zoom=2.216842;
28+
target=stir.FloatVoxelsOnCartesianGrid(projdata.get_proj_data_info(), zoom);
29+
# get initial image
30+
help(target)
31+
# target = stir.FloatVoxelsOnCartesianGrid.read_from_file('init.hv')
32+
# we will just fill the whole array with 1 here
33+
target.fill(1)
34+
s = recon.set_up(target)
35+
36+
# %%
37+
38+
# compute gradient of objective function
39+
# create a copy to store the gradient
40+
gradient=target.get_empty_copy();
41+
# compute gradient
42+
subset_num=1;
43+
poissonobj.compute_sub_gradient(gradient,target,subset_num)
44+
45+
# extract to python for plotting
46+
npimage = stirextra.to_numpy(gradient)
47+
plt.plot(npimage[10, 30, :])
48+
plt.show()
49+
50+
# this is useful to find the EM update (i.e. multiply with image)
51+
poissonobj.compute_sub_gradient_without_penalty_plus_sensitivity(gradient,target,subset_num)
52+
# extract to python for plotting
53+
npimage = stirextra.to_numpy(gradient)
54+
plt.plot(npimage[10, 30, :])
55+
plt.show()
56+
57+
# The followin is just the first iteration you need to create a for
58+
# loop to run all the iterations and subsets
59+
EMupdate = target*gradient
60+
# extract to python for plotting
61+
npimage = stirextra.to_numpy(EMupdate)
62+
plt.plot(npimage[10, 30, :])
63+
plt.show()
64+
65+
66+

0 commit comments

Comments
 (0)