def main(argv): """To do... Usage ----- Parameters --------- Example -------------------------- """ # Get the from and to number of files to process: sino_idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Essential reconstruction parameters: angles = float(argv[3]) offset = float(argv[4]) recpar = 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[19]) norm_dx = int(argv[20]) 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]) ext_fov_normalize = True if argv[17] == "True" else False ext_fov_average = True if argv[18] == "True" else False skip_ringrem = True if argv[21] == "True" else False ringrem = argv[22] # Extra reconstruction parameters: zerone_mode = True if argv[23] == "True" else False corr_offset = float(argv[24]) reconmethod = argv[25] # Force overpadding in case of GRIDREC for unknown reasons: if reconmethod == "GRIDREC": overpad = True decim_factor = int(argv[26]) downsc_factor = int(argv[27]) # Parameters for postprocessing: postprocess_required = True if argv[28] == "True" else False polarfilt_opt = argv[29] convert_opt = argv[30] crop_opt = argv[31] # Parameters for on-the-fly phase retrieval: phaseretrieval_required = True if argv[32] == "True" else False phrtmethod = int(argv[33]) phrt_param1 = double(argv[34]) # param1( e.g. regParam, or beta) phrt_param2 = double(argv[35]) # param2( e.g. thresh or delta) energy = double(argv[36]) distance = double(argv[37]) pixsize = double(argv[38]) / 1000.0 # pixsixe from micron to mm: phrtpad = True if argv[39] == "True" else False approx_win = int(argv[40]) angles_projfrom = int(argv[41]) angles_projto = int(argv[42]) rolling = True if argv[43] == "True" else False roll_shift = int(int(argv[44]) / decim_factor) preprocessingplan_fromcache = True if argv[45] == "True" else False dynamic_ff = True if argv[46] == "True" else False nr_threads = int(argv[47]) tmppath = argv[48] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[49] # 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 / downsc_factor): sino_idx = num_sinos / downsc_factor - 1 # Get correction plan and phase retrieval plan (if required): skipflat = False corrplan = 0 im_dark = 0 EFF = 0 filtEFF = 0 if (preprocessing_required): if not dynamic_ff: # 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) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: plan2cache(corrplan, infile, tmppath) else: corrplan = extract_flatdark(f_in, flat_end, logfilename) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: 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] else: # Dynamic flat fielding: if "/tomo" in f_in: if "/flat" in f_in: flat_dset = f_in['flat'] if "/dark" in f_in: im_dark = _medianize(f_in['dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case else: if "/exchange/data_white" in f_in: flat_dset = f_in['/exchange/data_white'] if "/exchange/data_dark" in f_in: im_dark = _medianize(f_in['/exchange/data_dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case # Prepare plan for dynamic flat fielding with 16 repetitions: if not skipflat: EFF, filtEFF = dff_prepare_plan(flat_dset, 16, im_dark) # Downscale images if necessary: im_dark = im_dark[::downsc_factor,::downsc_factor] EFF = EFF[::downsc_factor,::downsc_factor,:] filtEFF = filtEFF[::downsc_factor,::downsc_factor,:] f_in.close() # Run computation: process(sino_idx, num_sinos, infile, outfile, preprocessing_required, corrplan, skipflat, norm_sx, norm_dx, flat_end, half_half, half_half_line, ext_fov, ext_fov_rot_right, ext_fov_overlap, ext_fov_normalize, ext_fov_average, ringrem, phaseretrieval_required, phrtmethod, phrt_param1, phrt_param2, energy, distance, pixsize, phrtpad, approx_win, angles, angles_projfrom, angles_projto, offset, logtrsf, recpar, circle, scale, overpad, reconmethod, rolling, roll_shift, zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset, postprocess_required, polarfilt_opt, convert_opt, crop_opt, dynamic_ff, EFF, filtEFF, im_dark, nr_threads, logfilename, tmppath)
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]) off_step = 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[19]) norm_dx = int(argv[20]) 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]) ext_fov_normalize = True if argv[17] == "True" else False ext_fov_average = True if argv[18] == "True" else False skip_ringrem = True if argv[21] == "True" else False ringrem = argv[22] # Extra reconstruction parameters: zerone_mode = True if argv[23] == "True" else False corr_offset = float(argv[24]) reconmethod = argv[25] decim_factor = int(argv[26]) downsc_factor = int(argv[27]) # Parameters for postprocessing: postprocess_required = True if argv[28] == "True" else False convert_opt = argv[29] crop_opt = argv[30] # Parameters for on-the-fly phase retrieval: phaseretrieval_required = True if argv[31] == "True" else False phrtmethod = int(argv[32]) phrt_param1 = double(argv[33]) # param1( e.g. regParam, or beta) phrt_param2 = double(argv[34]) # param2( e.g. thresh or delta) energy = double(argv[35]) distance = double(argv[36]) pixsize = double(argv[37]) / 1000.0 # pixsixe from micron to mm: phrtpad = True if argv[38] == "True" else False approx_win = int(argv[39]) angles_projfrom = int(argv[40]) angles_projto = int(argv[41]) preprocessingplan_fromcache = True if argv[42] == "True" else False tmppath = argv[43] if not tmppath.endswith(sep): tmppath += sep nr_threads = int(argv[44]) off_from = float(argv[45]) off_to = float(argv[46]) slice_prefix = argv[47] logfilename = argv[48] 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 / downsc_factor): sino_idx = num_sinos / downsc_factor - 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, ext_fov_normalize, ext_fov_average, ringrem, phaseretrieval_required, phrtmethod, phrt_param1, phrt_param2, energy, distance, pixsize, phrtpad, approx_win, angles, angles_projfrom, angles_projto, off_step, 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, off_from, off_to, logfilename, lock, slice_prefix)
def main(argv): """Try to guess the center of rotation of the input CT dataset. Parameters ---------- infile : array_like HDF5 input dataset outfile : string Full path where the identified center of rotation will be written as output scale : int If sub-pixel precision is interesting, use e.g. 2.0 to get a center of rotation of .5 value. Use 1.0 if sub-pixel precision is not required angles : int Total number of angles of the input dataset method : string One of the following options: "registration" tmppath : string Temporary path where look for cached flat/dark files """ # Get path: infile = argv[0] # The HDF5 file on the outfile = argv[1] # The txt file with the proposed center scale = float(argv[2]) angles = float(argv[3]) method = argv[4] tmppath = argv[5] if not tmppath.endswith(sep): tmppath += sep pyfftw_cache_disable() pyfftw_cache_enable() pyfftw_set_keepalive_time(1800) # Create a silly temporary log: tmplog = tmppath + basename(infile) + str(time.time()) # Open the HDF5 file (take into account also older TDF versions): f_in = getHDF5( infile, 'r' ) if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Get flats and darks from cache or from file: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, tmplog) remove(tmplog) plan2cache(corrplan, infile, tmppath) # Get first and the 180 deg projections: im1 = tdf.read_tomo(dset,0).astype(float32) idx = int(round(num_proj/angles * pi)) - 1 im2 = tdf.read_tomo(dset,idx).astype(float32) # Apply simple flat fielding (if applicable): if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)) : im1 = ((abs(im1 - corrplan['im_dark'])) / (abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps)).astype(float32) im2 = ((abs(im2 - corrplan['im_dark_after'])) / (abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps)).astype(float32) # Scale projections (if required) to get subpixel estimation: if ( abs(scale - 1.0) > finfo(float32).eps ): im1 = imresize(im1, (int(round(scale*im1.shape[0])), int(round(scale*im1.shape[1]))), interp='bicubic', mode='F'); im2 = imresize(im2, (int(round(scale*im2.shape[0])), int(round(scale*im2.shape[1]))), interp='bicubic', mode='F'); # Find the center (flipping left-right im2): cen = findcenter.usecorrelation(im1, im2[ :,::-1]) cen = cen / scale # Print center to output file: text_file = open(outfile, "w") text_file.write(str(int(cen))) text_file.close() # Close input HDF5: f_in.close()
def main(argv): """To do... """ lock = Lock() skip_flat = True first_done = False pyfftw_cache_disable() pyfftw_cache_enable() pyfftw_set_keepalive_time(1800) # Get the from and to number of files to process: idx = int(argv[0]) # Get full paths of input TDF and output TDF: infile = argv[1] outfile = argv[2] # Get the phase retrieval parameters: beta = double(argv[3]) # param1( e.g. regParam, or beta) delta = double(argv[4]) # param2( e.g. thresh or delta) energy = double(argv[5]) distance = double(argv[6]) pixsize = double(argv[7]) / 1000.0 # pixsixe from micron to mm: pad = True if argv[8] == "True" else False # Tmp path and log file: tmppath = argv[9] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[10] # Open the HDF5 file: f_in = getHDF5(infile, 'r') if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_proj == 0): log = open(logfilename,"a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flats and darks from cache or from file: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, logfilename) remove(logfilename) plan2cache(corrplan, infile, tmppath) # Read projection: im = tdf.read_tomo(dset,idx).astype(float32) f_in.close() # Apply simple flat fielding (if applicable): if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)) : if (idx < num_proj/2): im = (im - corrplan['im_dark']) / (abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps) else: im = (im - corrplan['im_dark_after']) / (abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps) # Prepare plan: im = im.astype(float32) plan = prepare_plan (im, beta, delta, energy, distance, pixsize, padding=pad) # Perform phase retrieval (first time also PyFFTW prepares a plan): im = phase_retrieval(im, plan) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile = outfile + '_' + str(im.shape[1]) + 'x' + str(im.shape[0]) + '_' + str( nanmin(im)) + '$' + str( nanmax(im) ) im.tofile(outfile)
def main(argv): """Try to guess the amount of overlap in the case of extended FOV CT. Parameters ---------- infile : array_like HDF5 input dataset outfile : string Full path where the identified overlap will be written as output scale : int If sub-pixel precision is interesting, use e.g. 2.0 to get an overlap of .5 value. Use 1.0 if sub-pixel precision is not required tmppath : int Temporary path where look for cached flat/dark files """ # Get path: infile = argv[0] # The HDF5 file on the SSD outfile = argv[1] # The txt file with the proposed center scale = float(argv[2]) tmppath = argv[3] if not tmppath.endswith(sep): tmppath += sep # Create a silly temporary log: tmplog = tmppath + basename(infile) + str(time.time()) # Open the HDF5 file: f_in = getHDF5( infile, 'r' ) if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] num_proj = tdf.get_nr_projs(dset) # Get first and 180 deg projections: im1 = tdf.read_tomo(dset,0).astype(float32) im2 = tdf.read_tomo(dset,num_proj/2).astype(float32) # Get flats and darks from cache or from file: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, tmplog) remove(tmplog) plan2cache(corrplan, infile, tmppath) # Apply simple flat fielding (if applicable): if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)) : im1 = ((abs(im1 - corrplan['im_dark'])) / (abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps)).astype(float32) im2 = ((abs(im2 - corrplan['im_dark_after'])) / (abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps)).astype(float32) # Scale projections (if required) to get subpixel estimation: if ( abs(scale - 1.0) > finfo(float32).eps ): im1 = imresize(im1, (int(round(scale*im1.shape[0])), int(round(scale*im1.shape[1]))), interp='bicubic', mode='F'); im2 = imresize(im2, (int(round(scale*im2.shape[0])), int(round(scale*im2.shape[1]))), interp='bicubic', mode='F'); # Find the center (flipping left-right im2): DISTINGUISH BETWEEN AIR ON THE RIGHT AND ON THE LEFT?????? cen = findcenter.usecorrelation(im1, im2[ :,::-1]) cen = (cen / scale)*2.0 # Print center to output file: text_file = open(outfile, "w") text_file.write(str(int(abs(cen)))) text_file.close() # Close input HDF5: f_in.close()
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[19]) norm_dx = int(argv[20]) 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]) ext_fov_normalize = True if argv[17] == "True" else False ext_fov_average = True if argv[18] == "True" else False skip_ringrem = True if argv[21] == "True" else False ringrem = argv[22] # Extra reconstruction parameters: zerone_mode = True if argv[23] == "True" else False corr_offset = float(argv[24]) reconmethod = argv[25] decim_factor = int(argv[26]) downsc_factor = int(argv[27]) # Parameters for postprocessing: postprocess_required = True if argv[28] == "True" else False convert_opt = argv[29] crop_opt = argv[30] # Parameters for on-the-fly phase retrieval: phaseretrieval_required = True if argv[31] == "True" else False phrtmethod = int(argv[32]) phrt_param1 = double(argv[33]) # param1( e.g. regParam, or beta) phrt_param2 = double(argv[34]) # param2( e.g. thresh or delta) energy = double(argv[35]) distance = double(argv[36]) pixsize = double(argv[37]) / 1000.0 # pixsixe from micron to mm: phrtpad = True if argv[38] == "True" else False approx_win = int(argv[39]) preprocessingplan_fromcache = True if argv[40] == "True" else False tmppath = argv[41] if not tmppath.endswith(sep): tmppath += sep nr_threads = int(argv[42]) angles_from = float(argv[43]) angles_to = float(argv[44]) slice_prefix = argv[45] logfilename = argv[46] 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 / downsc_factor): sino_idx = num_sinos / downsc_factor - 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, ext_fov_normalize, ext_fov_average, 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 )
def main(argv): """Try to guess the amount of overlap in the case of extended FOV CT. Parameters ---------- infile : array_like HDF5 input dataset outfile : string Full path where the identified overlap will be written as output scale : int If sub-pixel precision is interesting, use e.g. 2.0 to get an overlap of .5 value. Use 1.0 if sub-pixel precision is not required tmppath : int Temporary path where look for cached flat/dark files """ # Get path: infile = argv[0] # The HDF5 file on the SSD outfile = argv[1] # The txt file with the proposed center scale = float(argv[2]) tmppath = argv[3] if not tmppath.endswith(sep): tmppath += sep # Create a silly temporary log: tmplog = tmppath + basename(infile) + str(time.time()) # Open the HDF5 file: f_in = getHDF5(infile, 'r') if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] num_proj = tdf.get_nr_projs(dset) # Get first and 180 deg projections: im1 = tdf.read_tomo(dset, 0).astype(float32) im2 = tdf.read_tomo(dset, num_proj / 2).astype(float32) # Get flats and darks from cache or from file: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, tmplog) remove(tmplog) plan2cache(corrplan, infile, tmppath) # Apply simple flat fielding (if applicable): if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)): im1 = ((abs(im1 - corrplan['im_dark'])) / (abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps)).astype(float32) im2 = ((abs(im2 - corrplan['im_dark_after'])) / (abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps)).astype(float32) # Scale projections (if required) to get subpixel estimation: if (abs(scale - 1.0) > finfo(float32).eps): im1 = imresize(im1, (int(round( scale * im1.shape[0])), int(round(scale * im1.shape[1]))), interp='bicubic', mode='F') im2 = imresize(im2, (int(round( scale * im2.shape[0])), int(round(scale * im2.shape[1]))), interp='bicubic', mode='F') # Find the center (flipping left-right im2): DISTINGUISH BETWEEN AIR ON THE RIGHT AND ON THE LEFT?????? cen = findcenter.usecorrelation(im1, im2[:, ::-1]) cen = (cen / scale) * 2.0 # Print center to output file: text_file = open(outfile, "w") text_file.write(str(int(abs(cen)))) text_file.close() # Close input HDF5: f_in.close()
def main(argv): """To do... """ lock = Lock() skip_flat = True first_done = False pyfftw_cache_disable() pyfftw_cache_enable() pyfftw_set_keepalive_time(1800) # Get the from and to number of files to process: idx = int(argv[0]) # Get full paths of input TDF and output TDF: infile = argv[1] outfile = argv[2] # Get the phase retrieval parameters: method = int(argv[3]) param1 = double(argv[4]) # param1( e.g. regParam, or beta) param2 = double(argv[5]) # param2( e.g. thresh or delta) energy = double(argv[6]) distance = double(argv[7]) pixsize = double(argv[8]) / 1000.0 # pixsixe from micron to mm: pad = True if argv[9] == "True" else False # Tmp path and log file: tmppath = argv[10] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[11] # Open the HDF5 file and check it contains flat files: skipflat = False f_in = getHDF5(infile, 'r') if "/tomo" in f_in: dset = f_in['tomo'] if not "/flat" in f_in: skipflat = True else: dset = f_in['exchange/data'] if not "/exchange/data_white" in f_in: skipflat = True num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_proj == 0): log = open(logfilename, "a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flats and darks from cache or from file: if not skipflat: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, logfilename) remove(logfilename) plan2cache(corrplan, infile, tmppath) # Read projection: im = tdf.read_tomo(dset, idx).astype(float32) f_in.close() # Apply simple flat fielding (if applicable): if not skipflat: if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)): if (idx < num_proj / 2): im = (im - corrplan['im_dark']) / ( abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps) else: im = (im - corrplan['im_dark_after']) / ( abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps) # Prepare plan: im = im.astype(float32) if (method == 0): # Paganin 2002: plan = tiehom_plan(im, param1, param2, energy, distance, pixsize, pad) im = tiehom(im, plan).astype(float32) elif (method == 1): # Paganin 2020: plan = tiehom_plan2020(im, param1, param2, energy, distance, pixsize, pad) im = tiehom2020(im, plan).astype(float32) else: plan = phrt_plan(im, energy, distance, pixsize, param2, param1, method, pad) im = phrt(im, plan, method).astype(float32) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile = outfile + '_' + str(im.shape[1]) + 'x' + str( im.shape[0]) + '_' + str(nanmin(im)) + '$' + str(nanmax(im)) im.tofile(outfile)
def main(argv): """To do... Usage ----- Parameters --------- Example -------------------------- The following line processes the first ten TIFF files of input path "/home/in" and saves the processed files to "/home/out" with the application of the Boin and Haibel filter with smoothing via a Butterworth filter of order 4 and cutoff frequency 0.01: reconstruct 0 4 C:\Temp\Dullin_Aug_2012\sino_noflat C:\Temp\Dullin_Aug_2012\sino_noflat\output 9.0 10.0 0.0 0.0 0.0 true sino slice C:\Temp\Dullin_Aug_2012\sino_noflat\tomo_conv flat dark """ 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] outfile = 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 beta = double(argv[30]) # param1( e.g. regParam, or beta) delta = double(argv[31]) # param2( e.g. thresh or delta) energy = double(argv[32]) distance = double(argv[33]) pixsize = double(argv[34]) / 1000.0 # pixsixe from micron to mm: phrtpad = True if argv[35] == "True" else False approx_win = int(argv[36]) preprocessingplan_fromcache = True if argv[37] == "True" else False nr_threads = int(argv[38]) tmppath = argv[39] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[40] # 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() # Run computation: process( sino_idx, num_sinos, infile, outfile, 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, beta, delta, 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, logfilename )
def main(argv): """To do... """ # Get the zero-order index of the sinogram to pre-process: idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Normalization parameters: norm_sx = int(argv[3]) norm_dx = int(argv[4]) # Params for flat fielding with post flats/darks: flat_end = True if argv[5] == "True" else False half_half = True if argv[6] == "True" else False half_half_line = int(argv[7]) # Params for extended FOV: ext_fov = True if argv[8] == "True" else False ext_fov_rot_right = argv[9] 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[10]) ext_fov_normalize = True if argv[11] == "True" else False ext_fov_average = True if argv[12] == "True" else False # Method and parameters coded into a string: ringrem = argv[13] # Flat fielding method (conventional or dynamic): dynamic_ff = True if argv[14] == "True" else False # Tmp path and log file: tmppath = argv[15] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[16] # Open the HDF5 file: f_in = getHDF5(infile, 'r') try: if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] except: log = open(logfilename,"a") log.write(linesep + "\tError reading input dataset. Process will end.") log.close() exit() num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_sinos == 0): log = open(logfilename,"a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flat and darks from cache or from file: skipflat = False skipdark = False if not dynamic_ff: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, flat_end, logfilename) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after']) ): skipflat = True else: plan2cache(corrplan, infile, tmppath) else: # Dynamic flat fielding: if "/tomo" in f_in: if "/flat" in f_in: flat_dset = f_in['flat'] if "/dark" in f_in: im_dark = _medianize(f_in['dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case else: if "/exchange/data_white" in f_in: flat_dset = f_in['/exchange/data_white'] if "/exchange/data_dark" in f_in: im_dark = _medianize(f_in['/exchange/data_dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case # Prepare plan for dynamic flat fielding with 16 repetitions: if not skipflat: EFF, filtEFF = dff_prepare_plan(flat_dset, 16, im_dark) # Read input image: im = tdf.read_sino(dset,idx).astype(float32) f_in.close() # Perform pre-processing (flat fielding, extended FOV, ring removal): if not skipflat: if dynamic_ff: # Dynamic flat fielding with downsampling = 2: im = dynamic_flat_fielding(im, idx, EFF, filtEFF, 2, im_dark, norm_sx, norm_dx) else: im = flat_fielding(im, idx, corrplan, flat_end, half_half, half_half_line, norm_sx, norm_dx) if ext_fov: im = extfov_correction(im, ext_fov_rot_right, ext_fov_overlap, ext_fov_normalize, ext_fov_average) if not skipflat and not dynamic_ff: im = ring_correction (im, ringrem, flat_end, corrplan['skip_flat_after'], half_half, half_half_line, ext_fov) else: im = ring_correction (im, ringrem, False, False, half_half, half_half_line, ext_fov) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile = outfile + '_' + str(im.shape[1]) + 'x' + str(im.shape[0]) + '_' + str( nanmin(im)) + '$' + str( nanmax(im) ) im.tofile(outfile)
def main(argv): """To do... """ # Get the zero-order index of the sinogram to pre-process: idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Normalization parameters: norm_sx = int(argv[3]) norm_dx = int(argv[4]) # Params for flat fielding with post flats/darks: flat_end = True if argv[5] == "True" else False half_half = True if argv[6] == "True" else False half_half_line = int(argv[7]) # Flat fielding method (conventional or dynamic): dynamic_ff = True if argv[8] == "True" else False # Parameters for rotation: rotation = float(argv[9]) interp = argv[10] border = argv[11] # Tmp path and log file: tmppath = argv[12] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[13] # Open the HDF5 file: f_in = getHDF5(infile, 'r') try: if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] except: log = open(logfilename, "a") log.write(linesep + "\tError reading input dataset. Process will end.") log.close() exit() num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_sinos == 0): log = open(logfilename, "a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flat and darks from cache or from file: skipflat = False skipdark = False if not dynamic_ff: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, flat_end, logfilename) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: plan2cache(corrplan, infile, tmppath) else: # Dynamic flat fielding: if "/tomo" in f_in: if "/flat" in f_in: flat_dset = f_in['flat'] if "/dark" in f_in: im_dark = _medianize(f_in['dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case else: if "/exchange/data_white" in f_in: flat_dset = f_in['/exchange/data_white'] if "/exchange/data_dark" in f_in: im_dark = _medianize(f_in['/exchange/data_dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case # Prepare plan for dynamic flat fielding with 16 repetitions: if not skipflat: EFF, filtEFF = dff_prepare_plan(flat_dset, 16, im_dark) # Read input image: im = tdf.read_tomo(dset, idx).astype(float32) f_in.close() # Perform pre-processing (flat fielding, extended FOV, ring removal): if not skipflat: if dynamic_ff: # Dynamic flat fielding with downsampling = 2: im = dynamic_flat_fielding(im, EFF, filtEFF, 2, im_dark) else: im = flat_fielding(im, idx, corrplan, flat_end, half_half, half_half_line, norm_sx, norm_dx) # Rotate: rows, cols = im.shape M = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotation, 1) if interp == 'nearest': interpflag = cv2.INTER_NEAREST elif interp == 'cubic': interpflag = cv2.INTER_CUBIC elif interp == 'lanczos': interpflag = cv2.INTER_LANCZOS4 else: interpflag = cv2.INTER_LINEAR if border == 'constant': borderflag = cv2.BORDER_CONSTANT else: borderflag = cv2.BORDER_REPLICATE im = cv2.warpAffine(im, M, (cols, rows), flags=interpflag, borderMode=borderflag) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile2 = outfile + '_' + str(im.shape[1]) + 'x' + str( im.shape[0]) + '_' + str(nanmin(im)) + '$' + str( nanmax(im)) + '_after.raw' im.tofile(outfile2)
def main(argv): """To do... """ # Get the zero-order index of the sinogram to pre-process: idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Normalization parameters: norm_sx = int(argv[3]) norm_dx = int(argv[4]) # Params for flat fielding with post flats/darks: flat_end = True if argv[5] == "True" else False half_half = True if argv[6] == "True" else False half_half_line = int(argv[7]) # Params for extended FOV: ext_fov = True if argv[8] == "True" else False ext_fov_rot_right = argv[9] 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[10]) ext_fov_normalize = True if argv[11] == "True" else False ext_fov_average = True if argv[12] == "True" else False # Method and parameters coded into a string: ringrem = argv[13] # Flat fielding method (conventional or dynamic): dynamic_ff = True if argv[14] == "True" else False # Tmp path and log file: tmppath = argv[15] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[16] # Open the HDF5 file: f_in = getHDF5(infile, 'r') try: if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] except: log = open(logfilename, "a") log.write(linesep + "\tError reading input dataset. Process will end.") log.close() exit() num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_sinos == 0): log = open(logfilename, "a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flat and darks from cache or from file: skipflat = False skipdark = False if not dynamic_ff: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, flat_end, logfilename) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: plan2cache(corrplan, infile, tmppath) else: # Dynamic flat fielding: if "/tomo" in f_in: if "/flat" in f_in: flat_dset = f_in['flat'] if "/dark" in f_in: im_dark = _medianize(f_in['dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case else: if "/exchange/data_white" in f_in: flat_dset = f_in['/exchange/data_white'] if "/exchange/data_dark" in f_in: im_dark = _medianize(f_in['/exchange/data_dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case # Prepare plan for dynamic flat fielding with 16 repetitions: if not skipflat: EFF, filtEFF = dff_prepare_plan(flat_dset, 16, im_dark) # Read input image: im = tdf.read_sino(dset, idx).astype(float32) f_in.close() # Perform pre-processing (flat fielding, extended FOV, ring removal): if not skipflat: if dynamic_ff: # Dynamic flat fielding with downsampling = 2: im = dynamic_flat_fielding(im, idx, EFF, filtEFF, 2, im_dark, norm_sx, norm_dx) else: im = flat_fielding(im, idx, corrplan, flat_end, half_half, half_half_line, norm_sx, norm_dx) if ext_fov: im = extfov_correction(im, ext_fov_rot_right, ext_fov_overlap, ext_fov_normalize, ext_fov_average) if not skipflat and not dynamic_ff: im = ring_correction(im, ringrem, flat_end, corrplan['skip_flat_after'], half_half, half_half_line, ext_fov) else: im = ring_correction(im, ringrem, False, False, half_half, half_half_line, ext_fov) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile = outfile + '_' + str(im.shape[1]) + 'x' + str( im.shape[0]) + '_' + str(nanmin(im)) + '$' + str(nanmax(im)) im.tofile(outfile)
def main(argv): """To do... Usage ----- Parameters --------- Example -------------------------- """ # Get the from and to number of files to process: sino_idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Essential reconstruction parameters: angles = float(argv[3]) offset = float(argv[4]) recpar = 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[19]) norm_dx = int(argv[20]) 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]) ext_fov_normalize = True if argv[17] == "True" else False ext_fov_average = True if argv[18] == "True" else False skip_ringrem = True if argv[21] == "True" else False ringrem = argv[22] # Extra reconstruction parameters: zerone_mode = True if argv[23] == "True" else False corr_offset = float(argv[24]) reconmethod = argv[25] # Force overpadding in case of GRIDREC for unknown reasons: if reconmethod == "GRIDREC": overpad = True decim_factor = int(argv[26]) downsc_factor = int(argv[27]) # Parameters for postprocessing: postprocess_required = True if argv[28] == "True" else False polarfilt_opt = argv[29] convert_opt = argv[30] crop_opt = argv[31] # Parameters for on-the-fly phase retrieval: phaseretrieval_required = True if argv[32] == "True" else False phrtmethod = int(argv[33]) phrt_param1 = double(argv[34]) # param1( e.g. regParam, or beta) phrt_param2 = double(argv[35]) # param2( e.g. thresh or delta) energy = double(argv[36]) distance = double(argv[37]) pixsize = double(argv[38]) / 1000.0 # pixsixe from micron to mm: phrtpad = True if argv[39] == "True" else False approx_win = int(argv[40]) angles_projfrom = int(argv[41]) angles_projto = int(argv[42]) rolling = True if argv[43] == "True" else False roll_shift = int(int(argv[44]) / decim_factor) preprocessingplan_fromcache = True if argv[45] == "True" else False dynamic_ff = True if argv[46] == "True" else False nr_threads = int(argv[47]) tmppath = argv[48] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[49] # 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 / downsc_factor): sino_idx = num_sinos / downsc_factor - 1 # Get correction plan and phase retrieval plan (if required): skipflat = False corrplan = 0 im_dark = 0 EFF = 0 filtEFF = 0 if (preprocessing_required): if not dynamic_ff: # 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) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: plan2cache(corrplan, infile, tmppath) else: corrplan = extract_flatdark(f_in, flat_end, logfilename) if (isscalar(corrplan['im_flat']) and isscalar(corrplan['im_flat_after'])): skipflat = True else: 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] else: # Dynamic flat fielding: if "/tomo" in f_in: if "/flat" in f_in: flat_dset = f_in['flat'] if "/dark" in f_in: im_dark = _medianize(f_in['dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case else: if "/exchange/data_white" in f_in: flat_dset = f_in['/exchange/data_white'] if "/exchange/data_dark" in f_in: im_dark = _medianize(f_in['/exchange/data_dark']) else: skipdark = True else: skipflat = True # Nothing to do in this case # Prepare plan for dynamic flat fielding with 16 repetitions: if not skipflat: EFF, filtEFF = dff_prepare_plan(flat_dset, 16, im_dark) # Downscale images if necessary: im_dark = im_dark[::downsc_factor, ::downsc_factor] EFF = EFF[::downsc_factor, ::downsc_factor, :] filtEFF = filtEFF[::downsc_factor, ::downsc_factor, :] f_in.close() # Run computation: process(sino_idx, num_sinos, infile, outfile, preprocessing_required, corrplan, skipflat, norm_sx, norm_dx, flat_end, half_half, half_half_line, ext_fov, ext_fov_rot_right, ext_fov_overlap, ext_fov_normalize, ext_fov_average, ringrem, phaseretrieval_required, phrtmethod, phrt_param1, phrt_param2, energy, distance, pixsize, phrtpad, approx_win, angles, angles_projfrom, angles_projto, offset, logtrsf, recpar, circle, scale, overpad, reconmethod, rolling, roll_shift, zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset, postprocess_required, polarfilt_opt, convert_opt, crop_opt, dynamic_ff, EFF, filtEFF, im_dark, nr_threads, logfilename, tmppath)
def main(argv): """To do... Usage ----- Parameters --------- Example -------------------------- The following line processes the first ten TIFF files of input path "/home/in" and saves the processed files to "/home/out" with the application of the Boin and Haibel filter with smoothing via a Butterworth filter of order 4 and cutoff frequency 0.01: destripe /home/in /home/out 1 10 1 0.01 4 """ lock = Lock() skip_ringrem = False skip_flat = False skip_flat_after = True first_done = False # Get the number of sino to pre-process: idx = int(argv[0]) # Get paths: infile = argv[1] outfile = argv[2] # Normalization parameters: norm_sx = int(argv[3]) norm_dx = int(argv[4]) # Params for flat fielding with post flats/darks: flat_end = True if argv[5] == "True" else False half_half = True if argv[6] == "True" else False half_half_line = int(argv[7]) # Params for extended FOV: ext_fov = True if argv[8] == "True" else False ext_fov_rot_right = argv[9] 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[10]) # Method and parameters coded into a string: ringrem = argv[11] # Tmp path and log file: tmppath = argv[12] if not tmppath.endswith(sep): tmppath += sep logfilename = argv[13] # Open the HDF5 file: f_in = getHDF5(infile, 'r') if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] prov_dset = f_in['provenance/detector_output'] num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Check if the HDF5 makes sense: if (num_sinos == 0): log = open(logfilename,"a") log.write(linesep + "\tNo projections found. Process will end.") log.close() exit() # Get flat and darks from cache or from file: 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) # Read input image: im = tdf.read_sino(dset,idx).astype(float32) f_in.close() # Perform pre-processing (flat fielding, extended FOV, ring removal): im = flat_fielding(im, idx, corrplan, flat_end, half_half, half_half_line, norm_sx, norm_dx) im = extfov_correction(im, ext_fov, ext_fov_rot_right, ext_fov_overlap) im = ring_correction (im, ringrem, flat_end, corrplan['skip_flat_after'], half_half, half_half_line, ext_fov) # Write down reconstructed preview file (file name modified with metadata): im = im.astype(float32) outfile = outfile + '_' + str(im.shape[1]) + 'x' + str(im.shape[0]) + '_' + str( nanmin(im)) + '$' + str( nanmax(im) ) im.tofile(outfile)
def main(argv): """Try to guess the center of rotation of the input CT dataset. Parameters ---------- infile : array_like HDF5 input dataset outfile : string Full path where the identified center of rotation will be written as output scale : int If sub-pixel precision is interesting, use e.g. 2.0 to get a center of rotation of .5 value. Use 1.0 if sub-pixel precision is not required angles : int Total number of angles of the input dataset proj_from : int Initial projections to consider for the assumed angles proj_to : int Final projections to consider for the assumed angles method : string (not implemented yet) tmppath : string Temporary path where look for cached flat/dark files """ # Get path: infile = argv[0] # The HDF5 file on the outfile = argv[1] # The txt file with the proposed center scale = float(argv[2]) angles = float(argv[3]) proj_from = int(argv[4]) proj_to = int(argv[5]) method = argv[6] tmppath = argv[7] if not tmppath.endswith(sep): tmppath += sep pyfftw_cache_disable() pyfftw_cache_enable() pyfftw_set_keepalive_time(1800) # Create a silly temporary log: tmplog = tmppath + basename(infile) + str(time.time()) # Open the HDF5 file (take into account also older TDF versions): f_in = getHDF5(infile, 'r') if "/tomo" in f_in: dset = f_in['tomo'] else: dset = f_in['exchange/data'] num_proj = tdf.get_nr_projs(dset) num_sinos = tdf.get_nr_sinos(dset) # Get flats and darks from cache or from file: try: corrplan = cache2plan(infile, tmppath) except Exception as e: #print "Error(s) when reading from cache" corrplan = extract_flatdark(f_in, True, tmplog) remove(tmplog) plan2cache(corrplan, infile, tmppath) # Get first and the 180 deg projections: im1 = tdf.read_tomo(dset, proj_from).astype(float32) idx = int(round((proj_to - proj_from) / angles * pi)) + proj_from im2 = tdf.read_tomo(dset, idx).astype(float32) # Apply simple flat fielding (if applicable): if (isinstance(corrplan['im_flat_after'], ndarray) and isinstance(corrplan['im_flat'], ndarray) and isinstance(corrplan['im_dark'], ndarray) and isinstance(corrplan['im_dark_after'], ndarray)): im1 = ((abs(im1 - corrplan['im_dark'])) / (abs(corrplan['im_flat'] - corrplan['im_dark']) + finfo(float32).eps)).astype(float32) im2 = ((abs(im2 - corrplan['im_dark_after'])) / (abs(corrplan['im_flat_after'] - corrplan['im_dark_after']) + finfo(float32).eps)).astype(float32) # Scale projections (if required) to get subpixel estimation: if (abs(scale - 1.0) > finfo(float32).eps): im1 = imresize(im1, (int(round( scale * im1.shape[0])), int(round(scale * im1.shape[1]))), interp='bicubic', mode='F') im2 = imresize(im2, (int(round( scale * im2.shape[0])), int(round(scale * im2.shape[1]))), interp='bicubic', mode='F') # Find the center (flipping left-right im2): cen = findcenter.usecorrelation(im1, im2[:, ::-1]) cen = cen / scale # Print center to output file: text_file = open(outfile, "w") text_file.write(str(int(cen))) text_file.close() # Close input HDF5: f_in.close()