def run(fname): try: # Form auto-complete for recon out = subprocess.Popen(['tomopy', 'recon', '-h'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = out.communicate() stdout = str(stdout) cmdscan = [] parscan = [] st = stdout.find("optional") while (1): st = stdout.find('--', st) end = min(stdout.find(' ', st), stdout.find('\\n', st)) if (st < 0): break cmdscan.append(stdout[st:end]) st = stdout.find('(default:', end) st1 = stdout.find('--', end) if (st1 > st or st1 < 0): end = stdout.find(')', st) parscan.append(stdout[st + 9:end].replace(" ", "").replace( "\\n", "")) else: parscan.append("") st = end # Create bash file fid = open(fname, 'w') fid.write( '#/usr/bin/env bash\n _tomopy()\n{\n\tlocal cur prev opts\n\tCOMPREPLY=()\n\tcur="${COMP_WORDS[COMP_CWORD]}"\n\tprev="${COMP_WORDS[COMP_CWORD-1]}"\n' ) # check all tomopy recon fid.write('\tif [[ ${COMP_WORDS[1]} == "recon" ]] ; then\n') fid.write('\t\topts="') fid.write(' '.join(cmdscan)) fid.write('"\n') fid.write( '\t\tCOMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )\n\tfi\n') for k in range(len(cmdscan)): fid.write('\tif [[ ${prev} == "') fid.write(cmdscan[k]) fid.write('" ]] ; then\n') fid.write('\t\topts="') fid.write(parscan[k]) fid.write('"\n') fid.write( '\t\tCOMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )\n\t\treturn 0\n\tfi\n' ) fid.write('}\n') fid.write('complete -F _tomopy -A directory tomopy') fid.close() except: log.error('Autocomplete file was not created')
def _read_theta_size(params): if (str(params.file_type) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}): theta_size = dxreader.read_dx_dims(params.file_name, 'data')[0] # elif: # # add here other reader of theta size for other formats # log.info(" *** %s is a valid xxx file format" % params.file_name) else: log.error(" *** %s is not a supported file format" % params.file_format) exit() return theta_size
def _read_tomo(params, sino): if (str(params.file_format) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}): proj, flat, dark, theta = dxchange.read_aps_32id(params.file_name, sino=sino) log.info(" *** %s is a valid dx file format" % params.file_name) # elif: # # add here other dxchange loader # log.info(" *** %s is a valid xxx file format" % params.file_name) else: log.error(" *** %s is not a supported file format" % params.file_format) exit() return proj, flat, dark, theta
def read_tomo(sino, params): if params.hdf_file_type == 'standard': # Read APS 32-BM raw data. log.info(" *** loading a stardard data set: %s" % params.hdf_file) proj, flat, dark, theta = dxchange.read_aps_32id(params.hdf_file, sino=sino) elif params.hdf_file_type == 'flip_and_stich': log.info(" *** loading a 360 deg flipped data set: %s" % params.hdf_file) proj360, flat360, dark360, theta360 = dxchange.read_aps_32id( params.hdf_file, sino=sino) proj, flat, dark = flip_and_stitch(variableDict, proj360, flat360, dark360) theta = theta360[:len(theta360) // 2] # take first half else: # params.hdf_file_type == 'mosaic': log.error(" *** loading a mosaic data set is not supported yet") exit() if params.reverse: log.info(" *** correcting for 180-0 data collection") step_size = (theta[1] - theta[0]) theta_size = dxreader.read_dx_dims(params.hdf_file, 'data')[0] theta = np.linspace(np.pi, (0 + step_size), theta_size) if params.blocked_views: log.info(" *** correcting for blocked view data collection") miss_angles = [params.missing_angles_start, params.missing_angle_end] # Manage the missing angles: proj = np.concatenate( (proj[0:miss_angles[0], :, :], proj[miss_angles[1] + 1:-1, :, :]), axis=0) theta = np.concatenate( (theta[0:miss_angles[0]], theta[miss_angles[1] + 1:-1])) # new missing projection handling # if params.blocked_views: # log.warning(" *** new missing angle handling") # miss_angles = [params.missing_angles_start, params.missing_angle_end] # data = patch_projection(data, miss_angles) proj, flat, dark = binning(proj, flat, dark, params) rotation_axis = params.rotation_axis / np.power(2, float(params.binning)) log.info(" *** rotation center: %f" % rotation_axis) return proj, flat, dark, theta, rotation_axis
def read_tomo(sino, params): """ Read in the tomography data. Inputs: sino: tuple of (start_row, end_row) to be read in params: parameters for reconstruction Output: projection data flat field (bright) data dark field data theta: Numpy array of angle for each projection rotation_axis: location of the rotation axis """ if params.file_type == 'standard': # Read APS 32-BM raw data. log.info(" *** loading a stardard data set: %s" % params.file_name) proj, flat, dark, theta = _read_tomo(params, sino=sino) elif params.file_type == 'flip_and_stich': log.info(" *** loading a 360 deg flipped data set: %s" % params.file_name) proj360, flat360, dark360, theta360 = _read_tomo(params, sino=sino) proj, flat, dark = flip_and_stitch(variableDict, proj360, flat360, dark360) theta = theta360[:len(theta360) // 2] # take first half else: # params.file_type == 'mosaic': log.error(" *** loading a mosaic data set is not supported yet") exit() if params.reverse: log.info(" *** correcting for 180-0 data collection") step_size = (theta[1] - theta[0]) theta_size = _read_theta_size(params) theta = np.linspace(np.pi, (0 + step_size), theta_size) proj, theta = blocked_view(proj, theta, params) # new missing projection handling # if params.blocked_views: # log.warning(" *** new missing angle handling") # miss_angles = [params.missing_angles_start, params.missing_angle_end] # data = patch_projection(data, miss_angles) proj, flat, dark = binning(proj, flat, dark, params) rotation_axis = params.rotation_axis / np.power(2, float(params.binning)) log.info(" *** rotation center: %f" % rotation_axis) return proj, flat, dark, theta, rotation_axis
def mask(data, params): log.info(" *** mask") if (params.reconstruction_mask): log.info(' *** *** ON') if 0 < params.reconstruction_mask_ratio <= 1: log.warning(" *** mask ratio: %f " % params.reconstruction_mask_ratio) data = tomopy.circ_mask(data, axis=0, ratio=params.reconstruction_mask_ratio) else: log.error(" *** mask ratio must be between 0-1: %f is ignored" % params.reconstruction_mask_ratio) else: log.warning(' *** *** OFF') return data
def read_rot_centers(params): # Add a trailing slash if missing top = os.path.join(params.hdf_file, '') # Load the the rotation axis positions. jfname = top + "rotation_axis.json" try: with open(jfname) as json_file: json_string = json_file.read() dictionary = json.loads(json_string) return collections.OrderedDict(sorted(dictionary.items())) except Exception as error: log.error( "ERROR: the json %s file containing the rotation axis locations is missing" % jfname) log.error("ERROR: to create one run:") log.error("ERROR: $ tomopy find_center --hdf-file %s" % top) exit()
def segment(params): # slice/full reconstruction file location tail = os.sep + os.path.splitext(os.path.basename( params.hdf_file))[0] + '_rec' + os.sep top = os.path.dirname(params.hdf_file) + '_rec' + tail # log.info(os.listdir(top)) if os.path.isdir(top): rec_file_list = list( filter(lambda x: x.endswith(('.tiff', '.tif')), os.listdir(top))) rec_file_list.sort() log.info('found in %s' % top) log.info('files %s' % rec_file_list) log.info('applying segmentation') log.warning('not implemented') else: log.error("ERROR: the directory %s does not exist" % top) log.error("ERROR: to create one run a full reconstruction first:") log.error( "ERROR: $ tomopy recon --reconstruction-type full --hdf-file %s" % params.hdf_file)