tools_multiangleΒΆ
This section contains the tools_multiangle script.
Download file: tools_multiangle.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | ###########################################################################
# (C) 2016 Elettra - Sincrotrone Trieste S.C.p.A.. All rights reserved. #
# #
# #
# This file is part of STP-Core, the Python core of SYRMEP Tomo Project, #
# a software tool for the reconstruction of experimental CT datasets. #
# #
# STP-Core is free software: you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the #
# Free Software Foundation, either version 3 of the License, or (at your #
# option) any later version. #
# #
# STP-Core is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with STP-Core. If not, see <http://www.gnu.org/licenses/>. #
# #
###########################################################################
#
# Author: Francesco Brun
# Last modified: Sept, 28th 2016
#
# python:
from sys import argv, exit
from os import remove, sep, linesep, listdir, makedirs
from os.path import exists, dirname, basename, splitext
from numpy import array, finfo, copy, float32, double, amin, amax, tile, concatenate, asarray
from numpy import empty, reshape, log as nplog, arange, squeeze, fromfile, ndarray, where, meshgrid
from time import time
from multiprocessing import Process, Lock
# pystp-specific:
from stp_core.preprocess.extfov_correction import extfov_correction
from stp_core.preprocess.flat_fielding import flat_fielding
from stp_core.preprocess.ring_correction import ring_correction
from stp_core.preprocess.extract_flatdark import extract_flatdark
from stp_core.phaseretrieval.tiehom import tiehom, tiehom_plan
from stp_core.phaseretrieval.phrt import phrt, phrt_plan
from stp_core.reconstruct.rec_astra import recon_astra_fbp, recon_astra_iterative
from stp_core.reconstruct.rec_fista_tv import recon_fista_tv
from stp_core.reconstruct.rec_mr_fbp import recon_mr_fbp
from stp_core.reconstruct.rec_gridrec import recon_gridrec
from stp_core.postprocess.postprocess import postprocess
from stp_core.utils.padding import upperPowerOfTwo, padImage, padSmoothWidth
from stp_core.utils.caching import cache2plan, plan2cache
from tifffile import imread, imsave
from h5py import File as getHDF5
import stp_core.io.tdf as tdf
def write_log(lock, fname, logfilename):
"""To do...
"""
lock.acquire()
try:
# Print out execution time:
log = open(logfilename,"a")
log.write(linesep + "\t%s reconstructed." % basename(fname))
log.close()
finally:
lock.release()
def reconstruct(im, angles, offset, logtransform, param1, circle, scale, pad, method,
zerone_mode, dset_min, dset_max, corr_offset, postprocess_required, convert_opt,
crop_opt, start, end, outpath, sino_idx, downsc_factor, decim_factor, logfilename, lock, slice_prefix):
"""Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox).
Parameters
----------
im1 : array_like
Sinogram image data as numpy array.
center : float
Offset of the center of rotation to use for the tomographic
reconstruction with respect to the half of sinogram width
(default=0, i.e. half width).
logtransform : boolean
Apply logarithmic transformation before reconstruction (default=True).
filter : string
Filter to apply before the application of the reconstruction algorithm. Filter
types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular,
gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall,
flat-top, kaiser, parzen.
circle : boolean
Create a circle in the reconstructed image and set to zero pixels outside the
circle (default=False).
"""
# Copy required due to multithreading:
im_f = im
# Decimate projections if required:
#if decim_factor > 1:
# im = im[::decim_factor,:]
# Upscale projections (if required):
if (abs(scale - 1.0) > finfo(float32).eps):
siz_orig1 = im_f.shape[1]
im_f = imresize(im_f, (im_f.shape[0], int(round(scale * im_f.shape[1]))), interp='bicubic', mode='F')
offset = int(offset * scale)
offset = int(round(offset))
# Apply transformation for changes in the center of rotation:
if (offset != 0):
if (offset >= 0):
im_f = im_f[:,:-offset]
tmp = im_f[:,0] # Get first column
tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times
im_f = concatenate((tmp.T,im_f), axis=1) # Concatenate tmp before the image
else:
im_f = im_f[:,abs(offset):]
tmp = im_f[:,im_f.shape[1] - 1] # Get last column
tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times
im_f = concatenate((im_f,tmp.T), axis=1) # Concatenate tmp after the image
# Downscale projections (without pixel averaging):
#if downsc_factor > 1:
# im = im[:,::downsc_factor]
# Scale image to [0,1] range (if required):
if (zerone_mode):
#print dset_min
#print dset_max
#print numpy.amin(im_f[:])
#print numpy.amax(im_f[:])
#im_f = (im_f - dset_min) / (dset_max - dset_min)
# Cheating the whole process:
im_f = (im_f - numpy.amin(im_f[:])) / (numpy.amax(im_f[:]) - numpy.amin(im_f[:]))
# Apply log transform:
if (logtransform == True):
im_f[im_f <= finfo(float32).eps] = finfo(float32).eps
im_f = -nplog(im_f + corr_offset)
# Replicate pad image to double the width:
if (pad):
dim_o = im_f.shape[1]
n_pad = im_f.shape[1] + im_f.shape[1] / 2
marg = (n_pad - dim_o) / 2
# Pad image:
im_f = padSmoothWidth(im_f, n_pad)
# Loop for all the required angles:
for i in range(int(round(start)), int(round(end)) + 1):
# Save image for next step:
im = im_f
# Apply projection removal (if required):
im_f = im_f[0:int(round(i/decim_factor)), :]
# Perform the actual reconstruction:
if (method.startswith('FBP')):
im_f = recon_astra_fbp(im_f, angles, method, param1)
elif (method == 'MR-FBP_CUDA'):
im_f = recon_mr_fbp(im_f, angles)
elif (method == 'FISTA-TV_CUDA'):
im_f = recon_fista_tv(im_f, angles, param1, param1)
elif (method == 'MLEM'):
im_f = recon_tomopy_iterative(im_f, angles, method, param1)
elif (method == 'GRIDREC'):
[im_f, im_f] = recon_gridrec(im_f, im_f, angles, param1)
else:
im_f = recon_astra_iterative(im_f, angles, method, param1, zerone_mode)
# Crop:
if (pad):
im_f = im_f[marg:dim_o + marg, marg:dim_o + marg]
# Resize (if necessary):
if (abs(scale - 1.0) > finfo(float32).eps):
im_f = imresize(im_f, (siz_orig1, siz_orig1), interp='nearest', mode='F')
# Apply post-processing (if required):
if postprocess_required:
im_f = postprocess(im_f, convert_opt, crop_opt)
else:
# Create the circle mask for fancy output:
if (circle == True):
siz = im_f.shape[1]
if siz % 2:
rang = arange(-siz / 2 + 1, siz / 2 + 1)
else:
rang = arange(-siz / 2,siz / 2)
x,y = meshgrid(rang,rang)
z = x ** 2 + y ** 2
a = (z < (siz / 2 - int(round(abs(offset)/downsc_factor)) ) ** 2)
im_f = im_f * a
# Write down reconstructed image (file name modified with metadata):
fname = outpath + slice_prefix + '_' + str(sino_idx).zfill(4) + '_off=' + str(offset*downsc_factor).zfill(4) + '_proj=' + str(i).zfill(4) + '.tif'
imsave(fname, im_f)
# Restore original image for next step:
im_f = im
# Write log (atomic procedure - lock used):
write_log(lock, fname, logfilename )
def process(sino_idx, num_sinos, infile, outpath, preprocessing_required, corr_plan, norm_sx, norm_dx, flat_end, half_half,
half_half_line, ext_fov, ext_fov_rot_right, ext_fov_overlap, ringrem, phaseretrieval_required, phrtmethod,
phrt_param1, phrt_param2,
energy, distance, pixsize, phrtpad, approx_win, angles, offset, logtransform, param1, circle, scale, pad, method,
zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset, postprocess_required, convert_opt,
crop_opt, nr_threads, angles_from, angles_to, logfilename, lock, slice_prefix):
"""To do...
"""
slice_nr = sino_idx
# Perform reconstruction (on-the-fly preprocessing and phase retrieval, if required):
if (phaseretrieval_required):
# In this case a bunch of sinograms is loaded into memory:
#
# Load the temporary data structure reading the input TDF file.
# To know the right dimension the first sinogram is pre-processed.
#
# Open the TDF file and get the dataset:
f_in = getHDF5(infile, 'r')
if "/tomo" in f_in:
dset = f_in['tomo']
else:
dset = f_in['exchange/data']
# Downscaling and decimation factors considered when determining the approximation window:
zrange = arange(sino_idx - approx_win*downsc_factor/2, sino_idx + approx_win*downsc_factor/2, downsc_factor)
zrange = zrange[ (zrange >= 0) ]
zrange = zrange[ (zrange < num_sinos) ]
approx_win = zrange.shape[0]
# Approximation window cannot be odd:
if (approx_win % 2 == 1):
approx_win = approx_win-1
zrange = zrange[0:approx_win]
# Read one sinogram to get the proper dimensions:
test_im = tdf.read_sino(dset, zrange[0]).astype(float32)
test_im = test_im[::decim_factor, ::downsc_factor]
# Perform the pre-processing of the first sinogram to get the right dimension:
if (preprocessing_required):
test_im = flat_fielding (test_im, zrange[0]/downsc_factor, corr_plan, flat_end, half_half,
half_half_line/decim_factor, norm_sx, norm_dx).astype(float32)
test_im = extfov_correction (test_im, ext_fov, ext_fov_rot_right, ext_fov_overlap/downsc_factor).astype(float32)
test_im = ring_correction (test_im, ringrem, flat_end, corr_plan['skip_flat_after'], half_half,
half_half_line/decim_factor, ext_fov).astype(float32)
# Now we can allocate memory for the bunch of slices:
tmp_im = empty((approx_win, test_im.shape[0], test_im.shape[1]), dtype=float32)
tmp_im[0,:,:] = test_im
# Reading all the the sinos from TDF file and close:
for ct in range(1, approx_win):
test_im = tdf.read_sino(dset, zrange[ct]).astype(float32)
test_im = test_im[::decim_factor, ::downsc_factor]
# Perform the pre-processing for each sinogram of the bunch:
if (preprocessing_required):
test_im = flat_fielding (test_im, zrange[ct]/downsc_factor, corr_plan, flat_end, half_half,
half_half_line/decim_factor, norm_sx, norm_dx).astype(float32)
test_im = extfov_correction (test_im, ext_fov, ext_fov_rot_right, ext_fov_overlap/downsc_factor).astype(float32)
test_im = ring_correction (test_im, ringrem, flat_end, corr_plan['skip_flat_after'], half_half,
half_half_line/decim_factor, ext_fov).astype(float32)
tmp_im[ct,:,:] = test_im
f_in.close()
# Now everything has to refer to a downscaled dataset:
sino_idx = ((zrange == sino_idx).nonzero())
#
# Perform phase retrieval:
#
# Prepare the plan:
if (phrtmethod == 0):
# Paganin's:
phrtplan = tiehom_plan (tmp_im[:,0,:], phrt_param1, phrt_param2, energy, distance, pixsize*downsc_factor, padding=phrtpad)
else:
phrtplan = phrt_plan (tmp_im[:,0,:], energy, distance, pixsize*downsc_factor, phrt_param2, phrt_param1, phrtmethod, padding=phrtpad)
# Process each projection (whose height depends on the size of the bunch):
for ct in range(0, tmp_im.shape[1]):
if (phrtmethod == 0):
tmp_im[:,ct,:] = tiehom(tmp_im[:,ct,:], phrtplan).astype(float32)
else:
tmp_im[:,ct,:] = phrt(tmp_im[:,ct,:], phrtplan, phrtmethod).astype(float32)
# Extract the requested sinogram:
im = tmp_im[sino_idx[0],:,:].squeeze()
else:
# Read only one sinogram:
f_in = getHDF5(infile, 'r')
if "/tomo" in f_in:
dset = f_in['tomo']
else:
dset = f_in['exchange/data']
im = tdf.read_sino(dset,sino_idx).astype(float32)
f_in.close()
# Downscale and decimate the sinogram:
im = im[::decim_factor,::downsc_factor]
sino_idx = sino_idx/downsc_factor
# Perform the preprocessing of the sinogram (if required):
if (preprocessing_required):
im = flat_fielding (im, sino_idx, corr_plan, flat_end, half_half, half_half_line/decim_factor,
norm_sx, norm_dx).astype(float32)
im = extfov_correction (im, ext_fov, ext_fov_rot_right, ext_fov_overlap)
im = ring_correction (im, ringrem, flat_end, corr_plan['skip_flat_after'], half_half,
half_half_line/decim_factor, ext_fov)
# Log infos:
log = open(logfilename,"a")
log.write(linesep + "\tPerforming reconstruction with multiple centers of rotation...")
log.write(linesep + "\t--------------")
log.close()
# Split the computation into multiple processes:
for num in range(nr_threads):
start = ( (angles_to - angles_from + 1) / nr_threads)*num + angles_from
if (num == nr_threads - 1):
end = angles_to
else:
end = ( (angles_to - angles_from + 1) / nr_threads)*(num + 1) + angles_from - 1
#Process(target=reconstruct, args=(im, angles, offset/downsc_factor, logtransform, param1, circle, scale, pad, method,
# zerone_mode, dset_min, dset_max, corr_offset, postprocess_required, convert_opt, crop_opt, start, end,
# outpath, slice_nr, downsc_factor, decim_factor, logfilename, lock, slice_prefix)).start()
# Actual reconstruction:
reconstruct(im, angles, offset/downsc_factor, logtransform, param1, circle, scale, pad, method,
zerone_mode, dset_min, dset_max, corr_offset, postprocess_required, convert_opt, crop_opt,
start, end, outpath, slice_nr, downsc_factor, decim_factor, logfilename, lock, slice_prefix)
def main(argv):
"""To do...
"""
lock = Lock()
skip_flat = False
skip_flat_after = True
# Get the from and to number of files to process:
sino_idx = int(argv[0])
# Get paths:
infile = argv[1]
outpath = argv[2]
# Essential reconstruction parameters::
angles = float(argv[3])
offset = float(argv[4])
param1 = argv[5]
scale = int(float(argv[6]))
overpad = True if argv[7] == "True" else False
logtrsf = True if argv[8] == "True" else False
circle = True if argv[9] == "True" else False
# Parameters for on-the-fly pre-processing:
preprocessing_required = True if argv[10] == "True" else False
flat_end = True if argv[11] == "True" else False
half_half = True if argv[12] == "True" else False
half_half_line = int(argv[13])
ext_fov = True if argv[14] == "True" else False
norm_sx = int(argv[17])
norm_dx = int(argv[18])
ext_fov_rot_right = argv[15]
if ext_fov_rot_right == "True":
ext_fov_rot_right = True
if (ext_fov):
norm_sx = 0
else:
ext_fov_rot_right = False
if (ext_fov):
norm_dx = 0
ext_fov_overlap = int(argv[16])
skip_ringrem = True if argv[19] == "True" else False
ringrem = argv[20]
# Extra reconstruction parameters:
zerone_mode = True if argv[21] == "True" else False
corr_offset = float(argv[22])
reconmethod = argv[23]
decim_factor = int(argv[24])
downsc_factor = int(argv[25])
# Parameters for postprocessing:
postprocess_required = True if argv[26] == "True" else False
convert_opt = argv[27]
crop_opt = argv[28]
# Parameters for on-the-fly phase retrieval:
phaseretrieval_required = True if argv[29] == "True" else False
phrtmethod = int(argv[30])
phrt_param1 = double(argv[31]) # param1( e.g. regParam, or beta)
phrt_param2 = double(argv[32]) # param2( e.g. thresh or delta)
energy = double(argv[33])
distance = double(argv[34])
pixsize = double(argv[35]) / 1000.0 # pixsixe from micron to mm:
phrtpad = True if argv[36] == "True" else False
approx_win = int(argv[37])
preprocessingplan_fromcache = True if argv[38] == "True" else False
tmppath = argv[39]
if not tmppath.endswith(sep): tmppath += sep
nr_threads = int(argv[40])
angles_from = float(argv[41])
angles_to = float(argv[42])
slice_prefix = argv[43]
logfilename = argv[44]
if not exists(outpath):
makedirs(outpath)
if not outpath.endswith(sep): outpath += sep
# Log info:
log = open(logfilename,"w")
log.write(linesep + "\tInput dataset: %s" % (infile))
log.write(linesep + "\tOutput path: %s" % (outpath))
log.write(linesep + "\t--------------")
log.write(linesep + "\tLoading flat and dark images...")
log.close()
# Open the HDF5 file:
f_in = getHDF5(infile, 'r')
if "/tomo" in f_in:
dset = f_in['tomo']
else:
dset = f_in['exchange/data']
if "/provenance/detector_output" in f_in:
prov_dset = f_in['provenance/detector_output']
dset_min = -1
dset_max = -1
if (zerone_mode):
if ('min' in dset.attrs):
dset_min = float(dset.attrs['min'])
else:
zerone_mode = False
if ('max' in dset.attrs):
dset_max = float(dset.attrs['max'])
else:
zerone_mode = False
num_sinos = tdf.get_nr_sinos(dset) # Pay attention to the downscale factor
if (num_sinos == 0):
exit()
# Check extrema:
if (sino_idx >= num_sinos):
sino_idx = num_sinos - 1
# Get correction plan and phase retrieval plan (if required):
corrplan = 0
if (preprocessing_required):
# Load flat fielding plan either from cache (if required) or from TDF file and cache it for faster re-use:
if (preprocessingplan_fromcache):
try:
corrplan = cache2plan(infile, tmppath)
except Exception as e:
#print "Error(s) when reading from cache"
corrplan = extract_flatdark(f_in, flat_end, logfilename)
plan2cache(corrplan, infile, tmppath)
else:
corrplan = extract_flatdark(f_in, flat_end, logfilename)
plan2cache(corrplan, infile, tmppath)
# Dowscale flat and dark images if necessary:
if isinstance(corrplan['im_flat'], ndarray):
corrplan['im_flat'] = corrplan['im_flat'][::downsc_factor,::downsc_factor]
if isinstance(corrplan['im_dark'], ndarray):
corrplan['im_dark'] = corrplan['im_dark'][::downsc_factor,::downsc_factor]
if isinstance(corrplan['im_flat_after'], ndarray):
corrplan['im_flat_after'] = corrplan['im_flat_after'][::downsc_factor,::downsc_factor]
if isinstance(corrplan['im_dark_after'], ndarray):
corrplan['im_dark_after'] = corrplan['im_dark_after'][::downsc_factor,::downsc_factor]
f_in.close()
# Log infos:
log = open(logfilename,"a")
log.write(linesep + "\tPerforming preprocessing...")
log.close()
# Run computation:
process( sino_idx, num_sinos, infile, outpath, preprocessing_required, corrplan, norm_sx,
norm_dx, flat_end, half_half, half_half_line, ext_fov, ext_fov_rot_right, ext_fov_overlap, ringrem,
phaseretrieval_required, phrtmethod, phrt_param1, phrt_param2, energy, distance, pixsize, phrtpad,
approx_win, angles, offset, logtrsf, param1, circle, scale, overpad, reconmethod, zerone_mode,
dset_min, dset_max, decim_factor, downsc_factor, corr_offset, postprocess_required, convert_opt,
crop_opt, nr_threads, angles_from, angles_to, logfilename, lock, slice_prefix )
# Sample:
# 26 C:\Temp\dataset42.tdf C:\Temp\test_angles 3.1416 -72.0 shepp-logan 1.0 False True True True True False 5 False False 100 0 0 False rivers:11;0 False 0.0 FBP_CUDA 1 4 False - - False 0 1.0 1000.0 22 150 2.2 True 16 False C:\Temp 1 1148 1248 slice C:\Temp\log_angles_00.txt
if __name__ == "__main__":
main(argv[1:])
|