| 
 | 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