Skip to content

Instantly share code, notes, and snippets.

@canismarko
Created June 16, 2016 17:22
Show Gist options
  • Save canismarko/22084738310ab7b412ed7efff27d499c to your computer and use it in GitHub Desktop.
Save canismarko/22084738310ab7b412ed7efff27d499c to your computer and use it in GitHub Desktop.
In [1]: runfile('/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C/Scripts/rec_ASTRA_one.py', wdir='/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C')
#### Processing NCA2b_3.9V_8400eV_10X_stepscan_721proj_1s_wfilter_5_123.h5
Test reconstruction of slice [0]
/usr/lib/python2.7/site-packages/dxchange-0.1.1-py2.7.egg/dxchange/exchange.py:644: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
Traceback (most recent call last):
File "<ipython-input-1-283c6f023cda>", line 1, in <module>
runfile('/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C/Scripts/rec_ASTRA_one.py', wdir='/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C')
File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C/Scripts/rec_ASTRA_one.py", line 158, in <module>
if reconstruction_test: rec_test(file_name, sino_start, sino_end, astra_method, extra_options, num_iter)
File "/run/media/mwolf/WOLF_DATA/txm-2016-06-07-aps-32-ID-C/Scripts/rec_ASTRA_one.py", line 74, in rec_test
rec = tomopy.recon(prj, theta, center=best_center/pow(2,level), algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/recon/algorithm.py", line 284, in recon
tomo, center_arr, recon, _get_func(algorithm), args, kwargs, ncore, nchunk)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/recon/algorithm.py", line 353, in _dist_recon
nchunk=nchunk)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/util/mproc.py", line 222, in distribute_jobs
clear_queue(queue, shared_arrays, shared_out)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/util/mproc.py", line 278, in clear_queue
_arg_parser(params)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/util/mproc.py", line 261, in _arg_parser
result = func(*func_args, **kwargs)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/recon/wrappers.py", line 122, in astra
astra_run(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/tomopy-1.0.0-py2.7-linux-x86_64.egg/tomopy/recon/wrappers.py", line 206, in astra_run
opts['proj_type'], proj_geom, vol_geom)
File "/usr/lib/python2.7/site-packages/astra/creators.py", line 558, in create_projector
return projector.create(cfg)
File "/usr/lib/python2.7/site-packages/astra/projector.py", line 36, in create
return p.create(config)
File "astra/projector_c.pyx", line 63, in astra.projector_c.create (astra/projector_c.cpp:1021)
Exception: Error creating projector.
import tomopy
import dxchange
import astra
import sirtfbp
import time
proj_type='cuda'
##################################### Inputs #########################################################################
file_name = '/local/prom04/2016-06/Cabana-Jimenez/NMC1a_pristine_8400eV_10X_step_scan_721proj_2s_0_119.h5' # best center = 1190 # Remove first dark
output_name = '/local/prom04/2016-06/Cabana-Jimenez/NMC1a_pristine_8400eV_10X_step_scan_721proj_2s_0_119_sirtfbp_recon/NMC1a_pristine_8400eV_10X_step_scan_721proj_2s_0_119_sirtfbp_recon_'
file_name = '/local/prom04/2016-06/Cabana-Jimenez/NCA2b_3.9V_8400eV_10X_stepscan_721proj_1s_wfilter_5_123.h5' # best center = 1166
output_name = '/local/prom04/2016-06/Cabana-Jimenez/NCA2b_3.9V_8400eV_10X_stepscan_721proj_1s_wfilter_5_123_sirtfbp_recon/9V_8400eV_10X_stepscan_721proj_1s_wfilter_5_123_sirtfbp_recon_'
file_name = '/local/prom04/2016-06/Cabana-Jimenez/lmo_pristine_XANEStomo_21_6576eV_184.h5' # best center =1206
#output_name = '/local/prom04/2016-06/Cabana-Jimenez/lmo_pristine_XANEStomo_21_6576eV_184_fbp_parzen_recon/lmo_pristine_XANEStomo_21_6576eV_184_sirtfbp_recon_'
output_name = '/local/prom04/2016-06/Cabana-Jimenez/lmo_pristine_XANEStomo_21_6576eV_184_sirtfbp_recon/lmo_pristine_XANEStomo_21_6576eV_184_sirtfbp_recon_'
file_name = 'NCA2b_3.9V_8400eV_10X_stepscan_721proj_1s_wfilter_5_123.h5' # best center =1206
output_name = 'NCA2b_3.9V_8400eV_10X_stepscan_recon/NCA2b_3.9V_8400eV_10X_stepscan_recon'
reconstruction_test = True
best_center = 1264
sino_start = 0
sino_end = 2048
miss_angles = [0,721]
level = 3
medfilt_size = 2
######################################################################################################################
def rec_test(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1):
print '\n#### Processing '+ file_name
print "Test reconstruction of slice [%d]" % sino_start
# Read HDF5 file.
prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_start, sino_end))
# Manage the missing angles:
if theta is None:
theta = tomopy.angles(prj.shape[0])
else:
pass
# prj = np.concatenate((prj[0:miss_angles[0],:,:], prj[miss_angles[1]+1:-1,:,:]), axis=0)
# theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1]))
prj = prj[miss_angles[0]:miss_angles[1],:,:]
dark = dark[1:,:,:]
theta = theta[miss_angles[0]:miss_angles[1]]
# normalize the prj
prj = tomopy.normalize(prj, flat, dark)
#prj[prj<=0]=0.1
#prj = tomopy.minus_log(prj)
# import pdb; pdb.set_trace()
# remove ring artefacts
prj = tomopy.remove_stripe_ti(prj)
prj = tomopy.remove_stripe_sf(prj,10)
# Median filter:
if medfilt_size:
prj = tomopy.median_filter(prj,size=medfilt_size)
if level>0:
prj = tomopy.downsample(prj, level=level)
# reconstruct
rec = tomopy.recon(prj, theta, center=best_center/pow(2,level), algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False)
# rec = tomopy.recon(prj, theta, center=best_center/pow(2,level), algorithm='gridrec', filter_name='parzen')
# rec = tomopy.recon(prj*1e2, theta, center=best_center/pow(2,level), algorithm='ospml_hybrid',
# num_iter=20, reg_par=[1e3, 1e-3])
# Write data as stack of TIFs.
dxchange.write_tiff_stack(rec, fname=output_name)
print "Slice saved as [%s_00000.tiff]" % output_name
def rec_full(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1):
start_time = time.time()
print '\n#### Processing '+ file_name
chunks = 8 # number of data chunks for the reconstruction
nSino_per_chunk = (sino_end - sino_start)/chunks
print "Reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % ((sino_end - sino_start), sino_start, sino_end, chunks, nSino_per_chunk)
strt = 0
for iChunk in range(0,chunks):
print '\n -- chunk # %i' % (iChunk+1)
sino_chunk_start = sino_start + nSino_per_chunk*iChunk
sino_chunk_end = sino_start + nSino_per_chunk*(iChunk+1)
print '\n --------> [%i, %i]' % (sino_chunk_start, sino_chunk_end)
if sino_chunk_end > sino_end:
break
# Read HDF5 file.
prj, flat, dark, theta = dxchange.read_aps_32id(file_name, sino=(sino_chunk_start, sino_chunk_end))
#prj = prj[:-1,:,:]
#flat = flat[20:-1,:,:]
dark = dark[1:,:,:]
# Manage the missing angles:
if theta == None:
theta = tomopy.angles(prj.shape[0])
else:
pass
# theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1]))
# theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1]))
prj = prj[miss_angles[0]:miss_angles[1],:,:]
theta = theta[miss_angles[0]:miss_angles[1]]
# normalize the prj
#prj = -np.log(tomopy.normalize(prj, flat, dark))
#prj[prj<=0]=0.001
#prj = tomopy.minus_log(prj)
# remove ring artefacts
prj = tomopy.remove_stripe_ti(prj,2)
prj = tomopy.remove_stripe_sf(prj,10)
# Median filter:
if medfilt_size:
prj = tomopy.median_filter(prj,size=medfilt_size)
if level>0:
prj = tomopy.downsample(prj, level=level)
prj = tomopy.downsample(prj, level=level, axis=1)
# reconstruct
rec = tomopy.recon(prj, theta, center=best_center/pow(2,level), algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False)
#rec = tomopy.recon(prj, theta, center=best_center/pow(2,level), algorithm='gridrec', filter_name='parzen')
print output_name
# Write data as stack of TIFs.
dxchange.write_tiff_stack(rec, fname=output_name, start=strt)
strt += prj.shape[1]
print("%i minutes" % ((time.time() - start_time)/60))
astra_method='SIRT-FBP'
if astra_method=='SIRT-FBP':
astra.plugin.register(sirtfbp.plugin)
extra_options = {'filter_dir':'./filters'}
num_iter = 100
if reconstruction_test: rec_test(file_name, sino_start, sino_end, astra_method, extra_options, num_iter)
else: rec_full(file_name, sino_start, sino_end, astra_method, extra_options, num_iter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment