Beispiel #1
0
def clean_sino(h5fname, chunks=None):
    import tomopy
    import dxchange

    
    # Read APS 32-BM raw data.
    #print('reading data')
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=chunks)
    #proj, flat, dark, theta = dxchange.read_aps_32id(h5fname)
       
#    # Manage the missing angles:
#    if blocked_views is not None:
#        print("Blocked Views: ", blocked_views)
#        proj = np.concatenate((proj[0:blocked_views[0],:,:], proj[blocked_views[1]+1:-1,:,:]), axis=0)
#        theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1:-1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)
    #data = tomopy.normalize(proj, flat, dark, cutoff=(1,2))

    # remove stripes
    #print('removing stripes, may take a while',flush=True)
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #print("Raw data: ", h5fname)
    #print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    data=np.swapaxes(data,0,1)
    return  data, theta
Beispiel #2
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 3  # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.17e-4  # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 22.7  # Energy of incident wave in keV
    alpha = 1e-02  # Phase retrieval coeff.
    zinger_level = 800  # Zinger level for projections
    zinger_level_w = 1000  # Zinger level for white

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat,
                                           zinger_level_w,
                                           size=15,
                                           axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    ## data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    ##data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    ##data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center / np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning)
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data,
                           theta,
                           center=rot_center,
                           algorithm=algorithm,
                           filter_name='parzen')

    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    return rec
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 30       # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.17e-4     # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 25.74        # Energy of incident wave in keV
    alpha = 1e-02                       # Phase retrieval coeff.
    zinger_level = 1000                 # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    miss_angles = [141,226]

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        

    print (theta)
    # Manage the missing angles:
    #proj_size = np.shape(proj)
    #theta = np.linspace(0,180,proj_size[0])
    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]))

    # zinger_removal
    #proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    #flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=0.8)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    # phase retrieval
    # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    ##rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #4
0
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning):
    
    data_shape = get_dx_dims(h5fname, 'data')
    print(data_shape)
    ssino = int(data_shape[1] * nsino)

    center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5)
    #print(sino,ssino, center_range)
    #print(center_range[0], center_range[1], center_range[2])

    # Select sinogram range to reconstruct
    sino = None
        
    start = ssino
    end = start + 1
    sino = (start, end)

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    # data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)


    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2]))

    index = 0
    for axis in np.arange(*center_range):
        stack[index] = data[:, 0, :]
        index = index + 1

    # Reconstruct the same slice with a range of centers.
    rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    index = 0
    # Save images to a temporary folder.
    fname = os.path.dirname(h5fname) + os.sep + 'try_rec/' + path_base_name(h5fname) + os.sep + 'recon_' + os.path.splitext(os.path.basename(h5fname))[0]    
    for axis in np.arange(*center_range):
        rfname = fname + '_' + str('{0:.2f}'.format(axis) + '.tiff')
        dxchange.write_tiff(rec[index], fname=rfname, overwrite=True)
        index = index + 1

    print("Reconstructions: ", fname)
Beispiel #5
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 8        # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.247e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 24.9         # Energy of incident wave in keV
    alpha = 1e-02                       # Phase retrieval coeff.
    zinger_level = 800                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # zinger_removal
    # proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    # data = tomopy.remove_stripe_ti(data, alpha=1.5)
    # data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    elif algorithm == 'astrasirt':
        extra_options ={'MinConstraint':0}
        options = {'proj_type':'cuda', 'method':'SIRT_CUDA', 'num_iter':200, 'extra_options':extra_options}
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options)
    else:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #6
0
def rec_try(h5fname, nsino, rot_center, center_search_width, algorithm, binning):
    
    data_shape = get_dx_dims(h5fname, 'data')
    print(data_shape)
    ssino = int(data_shape[1] * nsino)

    center_range = (rot_center-center_search_width, rot_center+center_search_width, 0.5)
    #print(sino,ssino, center_range)
    #print(center_range[0], center_range[1], center_range[2])

    # Select sinogram range to reconstruct
    sino = None
        
    start = ssino
    end = start + 1
    sino = (start, end)

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    # data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)


    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    stack = np.empty((len(np.arange(*center_range)), data_shape[0], data_shape[2]))

    index = 0
    for axis in np.arange(*center_range):
        stack[index] = data[:, 0, :]
        index = index + 1

    # Reconstruct the same slice with a range of centers.
    rec = tomopy.recon(stack, theta, center=np.arange(*center_range), sinogram_order=True, algorithm='gridrec', filter_name='parzen', nchunk=1)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    index = 0
    # Save images to a temporary folder.
    fname = os.path.dirname(h5fname) + os.sep + 'try_rec/' + path_base_name(h5fname) + os.sep + 'recon_' ##+ os.path.splitext(os.path.basename(h5fname))[0]    
    for axis in np.arange(*center_range):
        rfname = fname + str('{0:.2f}'.format(axis) + '.tiff')
        dxchange.write_tiff(rec[index], fname=rfname, overwrite=True)
        index = index + 1

    print("Reconstructions: ", fname)
Beispiel #7
0
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0], :, :],
                               proj[blocked_views[1] + 1:-1, :, :]),
                              axis=0)
        theta = np.concatenate(
            (theta[0:blocked_views[0]], theta[blocked_views[1] + 1:-1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    algorithm = args.algorithm
    ncores = args.ncores
    nitr = args.num_iter

    # always add algorithm
    _kwargs = {"algorithm": algorithm}

    # assign number of cores
    _kwargs["ncore"] = ncores

    # don't assign "num_iter" if gridrec or fbp
    if algorithm not in ["fbp", "gridrec"]:
        _kwargs["num_iter"] = nitr

    # Reconstruct object.
    with timemory.util.auto_timer(
            "[tomopy.recon(algorithm='{}')]".format(algorithm)):
        rec = tomopy.recon(proj, theta, **_kwargs)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    return rec
Beispiel #8
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 8        # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.247e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 24.9         # Energy of incident wave in keV
    alpha = 1e-02                       # Phase retrieval coeff.
    zinger_level = 800                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # h5fname_norm = '/local/data/2019-02/Burke/C47M_0015.h5'
    h5fname_norm = '/local/data/2019-02/Burke/kc78_Menardii_0003.h5'
    proj1, flat, dark, theta1 = dxchange.read_aps_32id(h5fname_norm, sino=sino)
    proj, dummy, dummy1, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=20)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #9
0
def remove_nan_neg_inf(data, params):

    log.info('  *** remove nan, neg and inf')
    if(params.fix_nan_and_inf == True):
        log.info('  *** *** ON')
        log.info('  *** *** replacement value %f ' % params.fix_nan_and_inf_value)
        data = tomopy.remove_nan(data, val=params.fix_nan_and_inf_value)
        data = tomopy.remove_neg(data, val= 0.0)
        data[np.isinf(data)] = params.fix_nan_and_inf_value
    else:
        log.warning('  *** *** OFF')
    return data
Beispiel #10
0
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0], :, :],
                               proj[blocked_views[1]+1:-1, :, :]), axis=0)
        theta = np.concatenate((theta[0:blocked_views[0]],
                                theta[blocked_views[1]+1: -1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data, level=7, wname='sym16', sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    algorithm = args.algorithm
    ncores = args.ncores
    nitr = args.num_iter

    # always add algorithm
    _kwargs = {"algorithm": algorithm}

    # assign number of cores
    _kwargs["ncore"] = ncores

    # don't assign "num_iter" if gridrec or fbp
    if algorithm not in ["fbp", "gridrec"]:
        _kwargs["num_iter"] = nitr

    # Reconstruct object.
    with timemory.util.auto_timer(
        "[tomopy.recon(algorithm='{}')]".format(algorithm)):
        rec = tomopy.recon(proj, theta, **_kwargs)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    return rec
    def reconstruct(self, data, element, box_checked, center, method, beta,
                    delta, iters, thetas):
        '''
		load data for reconstruction and load variables for reconstruction
		make it sure that data doesn't have infinity or nan as one of
		entries
		'''
        recData = data[element, :, :, :]
        recData[recData == inf] = True
        recData[np.isnan(recData)] = True
        recCenter = np.array(center, dtype=float32)

        if box_checked:
            recCenter = None
        print("working fine")

        if method == 0:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='mlem',
                                      center=recCenter,
                                      num_iter=iters)
        elif method == 1:
            self.recon = tomopy.recon(recData,
                                      thetas,
                                      algorithm='gridrec',
                                      emission=True)
        elif method == 2:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='art',
                                      num_iter=iters)
        elif method == 3:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_hybrid',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)
        elif method == 4:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_quad',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)

        self.recon = tomopy.remove_nan(self.recon)
        return self.recon
Beispiel #12
0
def prj_pro(prj):
    prj = prj + np.abs(prj.min())
    prj = prj + prj.mean()
    print prj.max(), prj.min()
    prj = prj / prj.max()
    print prj.max(), prj.min()
    prj = tomopy.minus_log(prj)
    prj = tomopy.remove_nan(prj)
    print prj.max(), prj.min()
    prj = tomopy.remove_neg(prj)
    print prj.max(), prj.min()

    prj = prj * 500
    print prj.max(), prj.min()
    return prj
Beispiel #13
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("dname", help="directory containing multiple datasets: /data/")
    parser.add_argument("--iters", nargs='?', type=int, default=10, help="number of iteration for alignment (default 7)")

    args = parser.parse_args()
    dname = args.dname
    iters = args.iters

    if os.path.isdir(dname):
        # Add a trailing slash if missing
        top = os.path.join(dname, '')
        print("DNAME:", dname)

    h5_file_list = list(filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top)))    

    prj = np.zeros((61, 101, 151), dtype='float32')

    for m in range(len(h5_file_list)):
        print(h5_file_list[m])
        # Read the XRF raw data.
        # prj += dx.read_hdf5(os.path.join(top, h5_file_list[m]), dataset='/exchange/data').astype('float32').copy()
        # ang = dx.read_hdf5(os.path.join(top, h5_file_list[4]), dataset='/exchange/theta').astype('float32').copy()
        # ang *= np.pi / 180.
        proj, flat, dark, theta = dx.read_aps_32id(top+h5_file_list[m])
        prj += proj

    proj, flat, dark, theta = dx.read_aps_32id(top+h5_file_list[0])
    ang = theta

    # Clean folder.
    try:
        shutil.rmtree('tmp/iters')
    except:
        pass

    prj = tomopy.remove_nan(prj, val=0.0)
    prj = tomopy.remove_neg(prj, val=0.0)
    prj[np.where(prj == np.inf)] = 0.0
    
    print (prj.min(), prj.max())

    prj, sx, sy, conv = tomopy.align_joint(prj, ang, iters=100, pad=(0, 0),
                        blur=True, rin=0.8, rout=0.95, center=None,
                        algorithm='pml_hybrid',
                        upsample_factor=100,
                        save=True, debug=True)
Beispiel #14
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("fname",
                        help="file name of xrf dxchange file: ./data.h5")
    parser.add_argument("--iters",
                        nargs='?',
                        type=int,
                        default=10,
                        help="number of iteration for alignment (default 10)")

    args = parser.parse_args()
    fname = args.fname
    iters = args.iters

    if os.path.isfile(fname):
        # Add a trailing slash if missing
        top = os.path.join(fname, '')

        data, theta, elements = dxr.read_dx_xrf(fname)
        print("Elements: ", elements)

        prj = np.sum(data, axis=0)

        # Clean folder.
        try:
            shutil.rmtree('tmp/iters')
        except:
            pass

        prj = tomopy.remove_nan(prj, val=0.0)
        prj = tomopy.remove_neg(prj, val=0.0)
        prj[np.where(prj == np.inf)] = 0.0

        prj, sx, sy, conv = tomopy.align_joint(prj,
                                               theta,
                                               iters=iters,
                                               pad=(0, 0),
                                               blur=True,
                                               rin=0.8,
                                               rout=0.95,
                                               center=None,
                                               algorithm='pml_hybrid',
                                               upsample_factor=100,
                                               save=True,
                                               debug=True)
Beispiel #15
0
def reconstruct(h5fname, sino, rot_center, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0],:,:], proj[blocked_views[1]+1:-1,:,:]), axis=0)
        theta = np.concatenate((theta[0:blocked_views[0]], theta[blocked_views[1]+1:-1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    # # phase retrieval
    # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-3,pad=True)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    # Reconstruct object.
    rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec')
        
    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #16
0
    def iterative_align(self, element, data, thetas, pad, blur_bool, rin, rout, center, algorithm, upsample_factor, save_bool, debug_bool, iters=5):
        '''
        iterative alignment method from TomoPy
        Variables
        -----------
        element: int
            element index
        data: ndarray
            4D xrf dataset ndarray [elements, theta, y,x]
        thetas: ndarray
            sorted projection angle list
        iters: int
            number of iterations
        '''
        num_projections = data.shape[1]
        prj = data[element]
        # prj = np.sum(data, axis=0)
        prj = tomopy.remove_nan(prj, val=0.0)
        prj[np.where(prj == np.inf)] = 0.0
        self.thetas = thetas


        # self.get_iter_paraeters()


        prj, sx, sy, conv = tomopy.align_joint(prj, thetas, iters=iters, pad=pad,
                            blur=blur_bool, rin=rin, rout=rout, center=center, algorithm=algorithm, 
                            upsample_factor=upsample_factor, save=save_bool, debug=debug_bool)
        self.x_shifts = np.round(sx).astype(int)
        self.y_shifts = np.round(sy).astype(int)

        for i in range(num_projections):
            data[:,i,:,:] = np.roll(data[:,i,:,:], int(np.round(self.y_shifts[i])), axis=1)
            data[:,i,:,:] = np.roll(data[:,i,:,:], int(np.round(self.x_shifts[i])), axis=2)
        
        return self.x_shifts, self.y_shifts, data
Beispiel #17
0
h5fname = "/home/beams/VNIKITIN/tomobank_rec/dk_MCFG_1_p_s1_.h5"
sino = (1300, 1316) # slices for reconstructions
nframes = 8 # time frames for reconstruction
frame = 94 # middle time frame for reconstruction
nproj = 300 # number of angles for 180 degrees interval
binning = 2 

proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
proj = proj[(frame-nframes//2)*nproj:(frame+nframes//2)*nproj, :, :]

# Flat-field correction of raw data.
data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

# remove stripes
data = tomopy.remove_stripe_fw(
    data, level=7, wname='sym16', sigma=1, pad=True)

# log fitler
data = tomopy.minus_log(data)
data = tomopy.remove_nan(data, val=0.0)
data = tomopy.remove_neg(data, val=0.00)
data[np.where(data == np.inf)] = 0.00

# Binning
data = tomopy.downsample(data, level=binning, axis=2)
if data.shape[1] > 1:
    data = tomopy.downsample(data, level=binning, axis=1)

# reshape for 4d
data = np.reshape(data,[nframes,nproj,data.shape[1],data.shape[2]])
np.save('data.npy',data)
Beispiel #18
0
def transform_scalars(dataset, rot_center=0, tune_rot_center=True):
    """Reconstruct sinograms using the tomopy gridrec algorithm

    Typically, a data exchange file would be loaded for this
    reconstruction. This operation will attempt to perform
    flat-field correction of the raw data using the dark and
    white background data found in the data exchange file.

    This operator also requires either the tomviz/tomopy-pipeline
    docker image, or a python environment with tomopy installed.
    """

    from tomviz import utils
    import numpy as np
    import tomopy

    # Get the current volume as a numpy array.
    array = utils.get_array(dataset)

    dark = dataset.dark
    white = dataset.white
    angles = utils.get_tilt_angles(dataset)
    tilt_axis = dataset.tilt_axis

    # TomoPy wants the tilt axis to be zero, so ensure that is true
    if tilt_axis == 2:
        order = [2, 1, 0]
        array = np.transpose(array, order)
        if dark is not None and white is not None:
            dark = np.transpose(dark, order)
            white = np.transpose(white, order)

    if angles is not None:
        # tomopy wants radians
        theta = np.radians(angles)
    else:
        # Assume it is equally spaced between 0 and 180 degrees
        theta = tomopy.angles(array.shape[0])

    # Perform flat-field correction of raw data
    if white is not None and dark is not None:
        array = tomopy.normalize(array, white, dark, cutoff=1.4)

    if rot_center == 0:
        # Try to find it automatically
        init = array.shape[2] / 2.0
        rot_center = tomopy.find_center(array,
                                        theta,
                                        init=init,
                                        ind=0,
                                        tol=0.5)
    elif tune_rot_center:
        # Tune the center
        rot_center = tomopy.find_center(array,
                                        theta,
                                        init=rot_center,
                                        ind=0,
                                        tol=0.5)

    # Calculate -log(array)
    array = tomopy.minus_log(array)

    # Remove nan, neg, and inf values
    array = tomopy.remove_nan(array, val=0.0)
    array = tomopy.remove_neg(array, val=0.00)
    array[np.where(array == np.inf)] = 0.00

    # Perform the reconstruction
    array = tomopy.recon(array, theta, center=rot_center, algorithm='gridrec')

    # Mask each reconstructed slice with a circle.
    array = tomopy.circ_mask(array, axis=0, ratio=0.95)

    # Set the transformed array
    child = utils.make_child_dataset(dataset)
    utils.mark_as_volume(child)
    utils.set_array(child, array)

    return_values = {}
    return_values['reconstruction'] = child
    return return_values
 def evaluate(self):
     self.tomo.value = tomopy.remove_nan(self.tomo.value,
                                         val=self.val.value,
                                         ncore=self.ncore.value)
Beispiel #20
0
prj = np.zeros((61, 101, 151), dtype='float32')

for m in range(len(fname)):
    print(fname[m])
    prj += dx.read_hdf5(os.path.join(mydir, fname[m]), dataset='/exchange/data').astype('float32').copy()
    ang = dx.read_hdf5(os.path.join(mydir, f), dataset='/exchange/theta').astype('float32').copy()
    ang *= np.pi / 180.


# Clean folder.
try:
    shutil.rmtree('tmp/iters')
except:
    pass


prj = tomopy.remove_nan(prj, val=0.0)
prj = tomopy.remove_neg(prj, val=0.0)
prj[np.where(prj == np.inf)] = 0.0
# prj = tomopy.median_filter(prj, size=3)

print (prj.min(), prj.max())

prj, sx, sy, conv = tomopy.align_joint(prj, ang, iters=100, pad=(0, 0),
                    blur=True, rin=0.8, rout=0.95, center=None,
                    algorithm='pml_hybrid',
                    upsample_factor=100,
                    save=True, debug=True)
# np.save('tmp/sx.npy', sx)
# np.save('tmp/sy.npy', sy)
Beispiel #21
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("dname",
                        help="directory containing multiple datasets: /data/")
    parser.add_argument("--iters",
                        nargs='?',
                        type=int,
                        default=10,
                        help="number of iteration for alignment (default 7)")

    args = parser.parse_args()
    dname = args.dname
    iters = args.iters

    if os.path.isdir(dname):
        # Add a trailing slash if missing
        top = os.path.join(dname, '')
        print("DNAME:", dname)

    h5_file_list = list(
        filter(lambda x: x.endswith(('.h5', '.hdf')), os.listdir(top)))

    prj = np.zeros((61, 101, 151), dtype='float32')

    for m in range(len(h5_file_list)):
        print(h5_file_list[m])
        # Read the XRF raw data.
        # prj += dx.read_hdf5(os.path.join(top, h5_file_list[m]), dataset='/exchange/data').astype('float32').copy()
        # ang = dx.read_hdf5(os.path.join(top, h5_file_list[4]), dataset='/exchange/theta').astype('float32').copy()
        # ang *= np.pi / 180.
        proj, flat, dark, theta = dx.read_aps_32id(top + h5_file_list[m])
        prj += proj

    proj, flat, dark, theta = dx.read_aps_32id(top + h5_file_list[0])
    ang = theta

    # Clean folder.
    try:
        shutil.rmtree('tmp/iters')
    except:
        pass

    prj = tomopy.remove_nan(prj, val=0.0)
    prj = tomopy.remove_neg(prj, val=0.0)
    prj[np.where(prj == np.inf)] = 0.0

    print(prj.min(), prj.max())

    prj, sx, sy, conv = tomopy.align_joint(prj,
                                           ang,
                                           iters=100,
                                           pad=(0, 0),
                                           blur=True,
                                           rin=0.8,
                                           rout=0.95,
                                           center=None,
                                           algorithm='pml_hybrid',
                                           upsample_factor=100,
                                           save=True,
                                           debug=True)
Beispiel #22
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 8        # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.247e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 24.9         # Energy of incident wave in keV
    alpha = 1e-02                       # Phase retrieval coeff.
    zinger_level = 800                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)
        
    # zinger_removal
    # proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    #data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)
    # padding 
    N = data.shape[2]
    data_pad = np.zeros([data.shape[0],data.shape[1],3*N//2],dtype = "float32")
    data_pad[:,:,N//4:5*N//4] = data
    data_pad[:,:,0:N//4] = np.tile(np.reshape(data[:,:,0],[data.shape[0],data.shape[1],1]),(1,1,N//4))
    data_pad[:,:,5*N//4:] = np.tile(np.reshape(data[:,:,-1],[data.shape[0],data.shape[1],1]),(1,1,N//4))

    data = data_pad
    rot_center = rot_center+N//4
 
    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
    rec = rec[:,N//4:5*N//4,N//4:5*N//4]
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
#   rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #23
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 25       # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.143e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    #monochromator_energy = 24.9        # Energy of incident wave in keV
    # used pink beam

    alpha = 1e-02                       # Phase retrieval coeff.
    zinger_level = 800                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # Read APS 2-BM raw data.
    # DIMAX saves 3 files: proj, flat, dark
    # when loading the data set select the proj file (larger size)

    fname = os.path.splitext(h5fname)[0]    
 
    fbase = fname.rsplit('_', 1)[0]
    fnum = fname.rsplit('_', 1)[1]
    fext = os.path.splitext(h5fname)[1]  

    fnum_flat = str("%4.4d" % (int(fnum)+1))
    fnum_dark = str("%4.4d" % (int(fnum)+2))

    fnproj = fbase + '_' + fnum + fext
    fnflat = fbase + '_' + fnum_flat + fext
    fndark = fbase + '_' + fnum_dark + fext

    fnflat = '/local/data/2018-11/Chawla/1G_A/1G_A_0002.hdf'
    fndark = '/local/data/2018-11/Chawla/1G_A/1G_A_0003.hdf'

    print('proj', fnproj)
    print('flat', fnflat)
    print('dark', fndark)
    # Read APS 2-BM DIMAX raw data.
    proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino)
    dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)      
    dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)        
    
    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    #rec = np.swapaxes(rec,0,2)
    
    return rec
Beispiel #24
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("fname", help="Full file name: /data/fname.raw")
    parser.add_argument("--start", nargs='?', type=int, default=0, help="First image to read")
    parser.add_argument("--nimg", nargs='?', type=int, default=1, help="Number of images to read")
    parser.add_argument("--ndark", nargs='?', type=int, default=10, help="Number of dark images")
    parser.add_argument("--nflat", nargs='?', type=int, default=10, help="Number of white images")

    args = parser.parse_args()

    fname = args.fname
    start = args.start
    end = args.start + args.nimg

    nflat, ndark, nimg, height, width = read_adimec_header(fname)
    print("Image Size:", width, height)
    print("Dataset metadata (nflat, ndark, nimg:", nflat, ndark, nimg)

    # override nflat and ndark from header with the passed parameter
    # comment the two lines below if the meta data in the binary 
    # file for nflat and ndark is correct
    nflat = args.nflat
    ndark = args.ndark

    proj = read_adimec_stack(fname, img=(start, end))
    print("Projection:", proj.shape)
    # slider(proj)

    flat = read_adimec_stack(fname, img=(nimg-ndark-nflat, nimg-ndark))
    print("Flat:", flat.shape)
    # slider(flat)

    dark = read_adimec_stack(fname, img=(nimg-ndark, nimg))
    print("Dark:", dark.shape)
    # slider(dark)

    nproj = tomopy.normalize(proj, flat, dark)
    print("Normalized projection:", nproj.shape)
    # slider(proj)

    
    proj = nproj[:,100:110, :]
    print("Sino chunk:", proj.shape)
    slider(proj)
    
    theta = tomopy.angles(proj.shape[0])
    print(theta.shape)

    proj = tomopy.minus_log(proj)

    proj = tomopy.remove_nan(proj, val=0.0)
    proj = tomopy.remove_neg(proj, val=0.00)
    proj[np.where(proj == np.inf)] = 0.00

    rot_center = 1280
    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec')

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    # Write data as stack of TIFs.
    dxchange.write_tiff_stack(rec, fname='recon_dir/recon')
Beispiel #25
0
for m in range(len(fname)):
    print(fname[m])
    prj += dx.read_hdf5(os.path.join(mydir, fname[m]),
                        dataset='/exchange/data').astype('float32').copy()
    ang = dx.read_hdf5(os.path.join(mydir, f),
                       dataset='/exchange/theta').astype('float32').copy()
    ang *= np.pi / 180.

# Clean folder.
try:
    shutil.rmtree('tmp/iters')
except:
    pass

prj = tomopy.remove_nan(prj, val=0.0)
prj = tomopy.remove_neg(prj, val=0.0)
prj[np.where(prj == np.inf)] = 0.0
# prj = tomopy.median_filter(prj, size=3)

print(prj.min(), prj.max())

prj, sx, sy, conv = tomopy.align_joint(prj,
                                       ang,
                                       iters=100,
                                       pad=(0, 0),
                                       blur=True,
                                       rin=0.8,
                                       rout=0.95,
                                       center=None,
                                       algorithm='pml_hybrid',
Beispiel #26
0
def reconstruct(h5fname, sino, rot_center, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0], :, :],
                               proj[blocked_views[1] + 1:-1, :, :]),
                              axis=0)
        theta = np.concatenate(
            (theta[0:blocked_views[0]], theta[blocked_views[1] + 1:-1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    # Phase retrieval for tomobank id from 00032 to 00056
    # sample_detector_distance = 6.0
    # detector_pixel_size_x = 0.65e-4
    # monochromator_energy = 27.4

    # Phase retrieval for tomobank id 00058 and tomobank id 00059
    # sample_detector_distance = 6.0
    # detector_pixel_size_x = 0.65e-4
    # monochromator_energy = 27.4

    # Phase retrieval for tomobank id 00060 and tomobank id 00063
    # sample_detector_distance = 2.5
    # detector_pixel_size_x = 0.65e-4
    # monochromator_energy = 27.4

    # Phase retrieval for tomobank id 00064
    # sample_detector_distance = 0.8
    # detector_pixel_size_x = 1.4e-4
    # monochromator_energy = 55.0

    # Phase retrieval for tomobank id 00065
    # sample_detector_distance = 5.8
    # detector_pixel_size_x = 1.4e-4
    # monochromator_energy = 55.0

    # Phase retrieval for tomobank id 00066
    # sample_detector_distance = 15.8
    # detector_pixel_size_x = 1.4e-4
    # monochromator_energy = 55.0

    # Phase retrieval for tomobank id 00067
    # sample_detector_distance = 30.8
    # detector_pixel_size_x = 1.4e-4
    # monochromator_energy = 55.0

    # Phase retrieval for tomobank id 00068
    # sample_detector_distance = 15.0
    # detector_pixel_size_x = 4.1e-4
    # monochromator_energy = 14.0

    # Phase retrieval for tomobank id 00069
    # sample_detector_distance = 0.4
    # detector_pixel_size_x = 3.7e-4
    # monochromator_energy = 36.085

    # Phase retrieval for tomobank id 00070
    # sample_detector_distance = 5.0
    # detector_pixel_size_x = 0.65e-4
    # monochromator_energy = 24.999

    # Phase retrieval for tomobank id 00071
    # sample_detector_distance = 1.5
    # detector_pixel_size_x = 0.65e-4
    # monochromator_energy = 24.999

    # Phase retrieval for tomobank id 00072
    # sample_detector_distance = 1.5
    # detector_pixel_size_x = 1.43e-4
    # monochromator_energy = 20.0

    # Phase retrieval for tomobank id 00073
    # sample_detector_distance = 1.0
    # detector_pixel_size_x = 0.74e-4
    # monochromator_energy = 25.0

    # Phase retrieval for tomobank id 00074
    # sample_detector_distance = 1.0
    # detector_pixel_size_x = 0.74e-4
    # monochromator_energy = 25.0

    # Phase retrieval for tomobank id 00075
    # sample_detector_distance = 11.0
    # detector_pixel_size_x = 1.43e-4
    # monochromator_energy = 60

    # Phase retrieval for tomobank id 00076
    # sample_detector_distance = 9.0
    # detector_pixel_size_x = 2.2e-4
    # monochromator_energy = 65

    # # phase retrieval
    # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-3,pad=True)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    # Reconstruct object.
    rec = tomopy.recon(data, theta, center=rot_center, algorithm='gridrec')

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    return rec
Beispiel #27
0
    ## Additional flat-field correction of raw data to negate need to mask.
    proj = tp.normalize_bg(proj, air=10)

    ## Set rotation center.
    rot_center = tp.find_center_vo(proj)
    print('Center of rotation: ', rot_center)

    tp.minus_log(proj, out=proj)

    # Reconstruct object using Gridrec algorith.
    rec = tp.recon(proj,
                   theta,
                   center=rot_center,
                   sinogram_order=False,
                   algorithm='gridrec',
                   filter_name='hann')
    rec = tp.remove_nan(rec)

    ## Writing data in netCDF3 .volume.
    ncfile = Dataset('filename.volume',
                     'w',
                     format='NETCDF3_64BIT',
                     clobber=True)
    NX = ncfile.createDimension('NX', rec.shape[2])
    NY = ncfile.createDimension('NY', rec.shape[1])
    NZ = ncfile.createDimension('NZ', rec.shape[0])
    volume = ncfile.createVariable('VOLUME', 'f4', ('NZ', 'NY', 'NX'))
    volume[:] = rec
    ncfile.close()
Beispiel #28
0
def reconstruct(h5fname, sino, nframes, frame, nproj, binning, tv):

    # Read APS 32-BM raw data.
    print("Read data")
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    print("Processing")
    proj = proj[(frame - nframes / 2) * nproj:(frame + nframes / 2) *
                nproj, :, :]

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Frames for reconstruction:", (frame - nframes / 2), "..",
          (frame + nframes / 2))
    # Phase retrieval for tomobank id 00080
    # sample_detector_distance = 25
    # detector_pixel_size_x = 3.0e-4
    # monochromator_energy = 16

    # phase retrieval
    # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=8e-03,pad=True)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    # Binning
    data = tomopy.downsample(data, level=binning, axis=2)
    if data.shape[1] > 1:
        data = tomopy.downsample(data, level=binning, axis=1)

    rot_center = data.shape[2] / 2
    theta = np.linspace(0, np.pi * nframes, nproj * nframes, endpoint=False)

    if tv:
        import rectv
        # Reconstruct. Iterative TV.
        [Ntheta, Nz, N] = data.shape
        Nzp = 4  # number of slices to process simultaniously by gpus
        M = nframes  # number of basis functions, must be a multiple of nframes
        lambda0 = pow(2, -9)  # regularization parameter 1
        lambda1 = pow(2, 2)  # regularization parameter 2
        niters = 1024  # number of iterations
        ngpus = 1  # number of gpus

        # reorder input data for compatibility
        data = np.ndarray.flatten(data.swapaxes(0, 1))
        rec = np.zeros([N * N * Nz * M], dtype='float32')  # memory for result

        # Make a class for tv
        cl = rectv.rectv(N, Ntheta, M, nframes, Nz, Nzp, ngpus, lambda0,
                         lambda1)
        # Run iterations
        cl.itertvR_wrap(rec, data, niters)

        rec = np.rot90(
            np.reshape(rec, [Nz, M, N, N]).swapaxes(0, 1), axes=(2, 3)
        ) / Ntheta * nframes * 2  # reorder result for compatibility
        rec = rec[::M / nframes]
    else:
        # Reconstruct object. FBP.
        rec = np.zeros((nframes, data.shape[1], data.shape[2], data.shape[2]),
                       dtype='float32')
        for time_frame in range(0, nframes):
            rec0 = tomopy.recon(
                data[time_frame * nproj:(time_frame + 1) * nproj],
                theta[time_frame * nproj:(time_frame + 1) * nproj],
                center=rot_center - np.mod(time_frame, 2),
                algorithm='gridrec')
            # Mask each reconstructed slice with a circle.
            rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95)
    return rec
Beispiel #29
0
def main():
    args = parse_arguments()

    context = zmq.Context()

    # TQM setup
    if args.my_distributor_addr is not None:
        addr_split = re.split("://|:", args.my_distributor_addr)
        tmq.init_tmq()
        # Handshake w. remote processes
        print(addr_split)
        tmq.handshake(addr_split[1], int(addr_split[2]), args.num_sinograms,
                      args.num_columns)
    else:
        print("No distributor..")

    # Subscriber setup
    print("Subscribing to: {}".format(args.data_source_addr))
    subscriber_socket = context.socket(zmq.SUB)
    subscriber_socket.set_hwm(args.data_source_hwm)
    subscriber_socket.connect(args.data_source_addr)
    subscriber_socket.setsockopt(zmq.SUBSCRIBE, b'')

    # Local publisher socket
    if args.my_publisher_addr is not None:
        publisher_socket = context.socket(zmq.PUB)
        publisher_socket.set_hwm(200)  # XXX
        publisher_socket.bind(args.my_publisher_addr)

    if args.data_source_synch_addr is not None:
        synchronize_subs(context, args.data_source_synch_addr)

    # Setup flatbuffer builder and serializer
    builder = flatbuffers.Builder(0)
    serializer = TraceSerializer.ImageSerializer(builder)

    # White/dark fields
    white_imgs = []
    tot_white_imgs = 0
    dark_imgs = []
    tot_dark_imgs = 0

    # Receive images
    total_received = 0
    total_size = 0
    seq = 0
    time0 = time.time()
    while True:
        msg = subscriber_socket.recv()
        total_received += 1
        total_size += len(msg)
        if msg == b"end_data": break  # End of data acquisition

        # This is mostly for data rate tests
        if args.skip_serialize:
            print("Skipping rest. Received msg: {}".format(total_received))
            continue

        # Deserialize msg to image
        read_image = serializer.deserialize(serialized_image=msg)
        serializer.info(read_image)  # print image information

        # Local checks
        if args.check_seq:
            if seq != read_image.Seq():
                print("Wrong sequence number: {} != {}".format(
                    seq, read_image.Seq()))
            seq += 1

        # Push image to workers (REQ/REP)
        my_image_np = read_image.TdataAsNumpy()
        if args.uint8_to_float32:
            my_image_np.dtype = np.uint8
            sub = np.array(my_image_np, dtype="float32")
        elif args.uint16_to_float32:
            my_image_np.dtype = np.uint16
            sub = np.array(my_image_np, dtype="float32")
        else:
            sub = my_image_np
        sub = sub.reshape((1, read_image.Dims().Y(), read_image.Dims().X()))

        # If incoming data is projection
        if read_image.Itype() is serializer.ITypes.Projection:
            rotation = read_image.Rotation()
            if args.degree_to_radian: rotation = rotation * math.pi / 180.

            # Tomopy operations expect 3D data, reshape incoming projections.
            if args.normalize:
                # flat/dark fields' corresponding rows
                if tot_white_imgs > 0 and tot_dark_imgs > 0:
                    print(
                        "normalizing: white_imgs.shape={}; dark_imgs.shape={}".
                        format(
                            np.array(white_imgs).shape,
                            np.array(dark_imgs).shape))
                    sub = tp.normalize(sub, flat=white_imgs, dark=dark_imgs)
            if args.remove_stripes:
                print("removing stripes")
                sub = tp.remove_stripe_fw(sub,
                                          level=7,
                                          wname='sym16',
                                          sigma=1,
                                          pad=True)
            if args.mlog:
                print("applying -log")
                sub = -np.log(sub)
            if args.remove_invalids:
                print("removing invalids")
                sub = tp.remove_nan(sub, val=0.0)
                sub = tp.remove_neg(sub, val=0.00)
                sub[np.where(sub == np.inf)] = 0.00

            # Publish to the world
            if (args.my_publisher_addr
                    is not None) and (total_received % args.my_publisher_freq
                                      == 0):
                builder.Reset()
                serializer = TraceSerializer.ImageSerializer(builder)
                mub = np.reshape(
                    sub, (read_image.Dims().Y(), read_image.Dims().X()))
                serialized_data = serializer.serialize(
                    image=mub,
                    uniqueId=0,
                    rotation=0,
                    itype=serializer.ITypes.Projection)
                print("Publishing:{}".format(read_image.UniqueId()))
                publisher_socket.send(serialized_data)

            # Send to workers
            if args.num_sinograms is not None:
                sub = sub[:, args.beg_sinogram:args.beg_sinogram +
                          args.num_sinograms, :]

            ncols = sub.shape[2]
            sub = sub.reshape(sub.shape[0] * sub.shape[1] * sub.shape[2])

            if args.my_distributor_addr is not None:
                tmq.push_image(sub, args.num_sinograms, ncols, rotation,
                               read_image.UniqueId(), read_image.Center())

        # If incoming data is white field
        if read_image.Itype() is serializer.ITypes.White:
            #print("White field data is received: {}".format(read_image.UniqueId()))
            white_imgs.extend(sub)
            tot_white_imgs += 1

        # If incoming data is white-reset
        if read_image.Itype() is serializer.ITypes.WhiteReset:
            #print("White-reset data is received: {}".format(read_image.UniqueId()))
            white_imgs = []
            white_imgs.extend(sub)
            tot_white_imgs += 1

        # If incoming data is dark field
        if read_image.Itype() is serializer.ITypes.Dark:
            #print("Dark data is received: {}".format(read_image.UniqueId()))
            dark_imgs.extend(sub)
            tot_dark_imgs += 1

        # If incoming data is dark-reset
        if read_image.Itype() is serializer.ITypes.DarkReset:
            #print("Dark-reset data is received: {}".format(read_image.UniqueId()))
            dark_imgs = []
            dark_imgs.extend(sub)
            tot_dark_imgs += 1

    time1 = time.time()

    # Profile information
    elapsed_time = time1 - time0
    tot_MiBs = (total_size * 1.) / 2**20
    print(
        "Received number of projections: {}; Total size (MiB): {:.2f}; Elapsed time (s): {:.2f}"
        .format(total_received, tot_MiBs, elapsed_time))
    print("Rate (MiB/s): {:.2f}; (msg/s): {:.2f}".format(
        tot_MiBs / elapsed_time, total_received / elapsed_time))

    # Finalize TMQ
    if not args.my_distributor_addr is not None:
        tmq.done_image()
        tmq.finalize_tmq()
Beispiel #30
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec', options=None, num_iter=100, dark_file=None):

    sample_detector_distance = 10       # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.247e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 35           # Energy of incident wave in keV
    alpha = 1e-01                       # Phase retrieval coeff.
    zinger_level = 500                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    if dark_file is not None:
        proj_, flat, dark, theta_ = dxchange.read_aps_32id(dark_file, sino=sino)
        del proj_, theta_
        
    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    # flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    print(algorithm)
    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    elif algorithm == "astra_fbp":
        if options == 'linear':
            options = {'proj_type':'linear', 'method':'FBP'}
        else:
            options = {'proj_type':'cuda', 'method':'FBP_CUDA'}
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options, ncore=1)
    elif algorithm == "astra_sirt":
        extra_options = {'MinConstraint':0}
        options = {'proj_type':'cuda', 'method':'SIRT_CUDA', 'num_iter':num_iter, 'extra_options':extra_options}
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options)
    elif algorithm == tomopy.astra:
        rec = tomopy.recon(data, theta, center=rot_center, algorithm=tomopy.astra, options=options)
    else:
        try:
            rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm, filter_name='parzen')
        except:
            rec = tomopy.recon(data, theta, center=rot_center, algorithm=algorithm)
        
    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    
    return rec
Beispiel #31
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 30  # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.17e-4  # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 25.74  # Energy of incident wave in keV
    alpha = 1e-02  # Phase retrieval coeff.
    zinger_level = 1000  # Zinger level for projections
    zinger_level_w = 1000  # Zinger level for white

    miss_angles = [500, 1050]

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    print(theta)
    # Manage the missing angles:
    #proj_size = np.shape(proj)
    #theta = np.linspace(0,180,proj_size[0])
    print(proj.shape, theta.shape)

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

    print(proj.shape, theta.shape)
    # zinger_removal
    #proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    #flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=0.8)

    # remove stripes
    #    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)
    data = tomopy.remove_stripe_ti(data, 2)

    # phase retrieval
    # data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center / np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning)
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data,
                           theta,
                           center=rot_center,
                           algorithm=algorithm,
                           filter_name='parzen')

    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    ##rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    return rec
Beispiel #32
0
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0], :, :],
                               proj[blocked_views[1]+1:-1, :, :]), axis=0)
        theta = np.concatenate((theta[0:blocked_views[0]],
                                theta[blocked_views[1]+1: -1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data, level=7, wname='sym16', sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    algorithm = args.algorithm
    ncores = args.ncores
    nitr = args.num_iter

    # always add algorithm
    _kwargs = {"algorithm": algorithm}

    # assign number of cores
    _kwargs["ncore"] = ncores

    # use the accelerated version
    if algorithm in ["mlem", "sirt"]:
        _kwargs["accelerated"] = True

    # don't assign "num_iter" if gridrec or fbp
    if algorithm not in ["fbp", "gridrec"]:
        _kwargs["num_iter"] = nitr

    sname = os.path.join(args.output_dir, 'proj_{}'.format(args.algorithm))
    print(proj.shape)
    tmp = np.zeros((proj.shape[0], proj.shape[2]))
    tmp[:,:] = proj[:,0,:]
    output_image(tmp, sname + "." + args.format)

    # Reconstruct object.
    with timemory.util.auto_timer(
        "[tomopy.recon(algorithm='{}')]".format(algorithm)):
        print("Starting reconstruction with kwargs={}...".format(_kwargs))
        rec = tomopy.recon(data, theta, **_kwargs)
    print("Completed reconstruction...")

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    obj = np.zeros(rec.shape, dtype=rec.dtype)
    label = "{} @ {}".format(algorithm.upper(), h5fname)
    quantify_difference(label, obj, rec)

    return rec
Beispiel #33
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 25  # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 2.143e-4  # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    #monochromator_energy = 24.9        # Energy of incident wave in keV
    # used pink beam

    alpha = 1e-02  # Phase retrieval coeff.
    zinger_level = 800  # Zinger level for projections
    zinger_level_w = 1000  # Zinger level for white

    # Read APS 2-BM raw data.
    # DIMAX saves 3 files: proj, flat, dark
    # when loading the data set select the proj file (larger size)

    fname = os.path.splitext(h5fname)[0]

    fbase = fname.rsplit('_', 1)[0]
    fnum = fname.rsplit('_', 1)[1]
    fext = os.path.splitext(h5fname)[1]

    fnum_flat = str("%4.4d" % (int(fnum) + 1))
    fnum_dark = str("%4.4d" % (int(fnum) + 2))

    fnproj = fbase + '_' + fnum + fext
    fnflat = fbase + '_' + fnum_flat + fext
    fndark = fbase + '_' + fnum_dark + fext

    fnflat = '/local/data/2018-11/Chawla/1G_A/1G_A_0002.hdf'
    fndark = '/local/data/2018-11/Chawla/1G_A/1G_A_0003.hdf'

    print('proj', fnproj)
    print('flat', fnflat)
    print('dark', fndark)
    # Read APS 2-BM DIMAX raw data.
    proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino)
    dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)
    dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat,
                                           zinger_level_w,
                                           size=15,
                                           axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center / np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning)
    data = tomopy.downsample(data, level=binning, axis=1)

    # Reconstruct object.
    if algorithm == 'sirtfbp':
        rec = rec_sirtfbp(data, theta, rot_center)
    else:
        rec = tomopy.recon(data,
                           theta,
                           center=rot_center,
                           algorithm=algorithm,
                           filter_name='parzen')

    print("Algorithm: ", algorithm)

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
    #rec = np.swapaxes(rec,0,2)

    return rec
Beispiel #34
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 31  # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.17e-4  # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 65  # Energy of incident wave in keV
    # used pink beam

    alpha = 1e-5 * 2**4  # Phase retrieval coeff.
    zinger_level = 800  # Zinger level for projections
    zinger_level_w = 1000  # Zinger level for white

    # Read APS 2-BM raw data.
    # DIMAX saves 3 files: proj, flat, dark
    # when loading the data set select the proj file (larger size)

    fname = os.path.splitext(h5fname)[0]

    fbase = fname.rsplit('_', 1)[0]
    fnum = fname.rsplit('_', 1)[1]
    fext = os.path.splitext(h5fname)[1]

    fnum_flat = str("%4.4d" % (int(fnum) + 1))
    fnum_dark = str("%4.4d" % (int(fnum) + 2))

    fnproj = fbase + '_' + fnum + fext
    fnflat = fbase + '_' + fnum_flat + fext
    fndark = fbase + '_' + fnum_dark + fext

    print('proj', fnproj)
    print('flat', fnflat)
    print('dark', fndark)
    # Read APS 2-BM DIMAX raw data.
    proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino)
    dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)
    #flat, dum3, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)
    dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat,
                                           zinger_level_w,
                                           size=15,
                                           axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    data = tomopy.prep.phase.retrieve_phase(data,
                                            pixel_size=detector_pixel_size_x,
                                            dist=sample_detector_distance,
                                            energy=monochromator_energy,
                                            alpha=alpha,
                                            pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center / np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning)
    data = tomopy.downsample(data, level=binning, axis=1)

    # padding
    N = data.shape[2]
    data_pad = np.zeros([data.shape[0], data.shape[1], 3 * N // 2],
                        dtype="float32")
    data_pad[:, :, N // 4:5 * N // 4] = data
    data_pad[:, :, 0:N // 4] = np.tile(
        np.reshape(data[:, :, 0], [data.shape[0], data.shape[1], 1]),
        (1, 1, N // 4))
    data_pad[:, :, 5 * N // 4:] = np.tile(
        np.reshape(data[:, :, -1], [data.shape[0], data.shape[1], 1]),
        (1, 1, N // 4))

    data = data_pad
    rot_center = rot_center + N // 4

    nframes = 1
    nproj = 1500
    theta = np.linspace(0, np.pi * nframes, nproj * nframes, endpoint=False)
    rec = np.zeros((nframes, data.shape[1], data.shape[2], data.shape[2]),
                   dtype='float32')
    for time_frame in range(0, nframes):
        rec0 = tomopy.recon(data[time_frame * nproj:(time_frame + 1) * nproj],
                            theta[time_frame * nproj:(time_frame + 1) * nproj],
                            center=rot_center,
                            algorithm='gridrec')
        # Mask each reconstructed slice with a circle.
        rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95)
    rec = rec[:, :, N // 4:5 * N // 4, N // 4:5 * N // 4]

    print("Algorithm: ", algorithm)

    return rec
    def reconstruct(self,
                    data,
                    element,
                    center,
                    method,
                    beta,
                    delta,
                    iters,
                    thetas,
                    mid_indx,
                    show_stats=False):
        '''
		load data for reconstruction and load variables for reconstruction
		make it sure that data doesn't have infinity or nan as one of
		entries
		'''
        recData = data[element, :, :, :]
        recData[recData == np.inf] = True
        recData[np.isnan(recData)] = True
        recCenter = np.array(center, dtype=np.float32)

        print("working fine")

        if method == 0:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='mlem',
                                      center=recCenter,
                                      num_iter=iters,
                                      accelerated=True,
                                      device='cpu')
            self.recon /= 0.0070430035033585735
        elif method == 1:
            # TODO: gridrec fails and cannot recover, all of python shuts down. consider removing.
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='gridrec')
            self.recon /= 0.9080359350734858
        elif method == 2:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='art',
                                      num_iter=iters)
            self.recon /= 0.9998318282790742
        elif method == 3:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_hybrid',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)
            self.recon /= 0.9998791750170672
        elif method == 4:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_quad',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)
            self.recon /= 0.9997819558282253
        elif method == 5:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='fbp')
        elif method == 6:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='sirt',
                                      num_iter=iters)
        elif method == 7:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='tv',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)

        self.recon[self.recon < 0] = 0
        #tomopy.remove_nan() does not remove inf values
        self.recon = tomopy.remove_nan(self.recon)

        if np.isinf(self.recon).max():
            print(
                "WARNING: inf values found in reconstruction, consider reconstructing with less iterations"
            )
            print("inf values replaced with 0.001")
            self.recon[self.recon == np.inf] = 0.001

        if show_stats:
            err, mse = self.assessRecon(self.recon, recData, thetas, mid_indx,
                                        show_stats)
            print(mse)
        return self.recon
Beispiel #36
0
    ## Set path (without file suffix) to the micro-CT data to reconstruct.
    fname = 'data_dir/sample'

    ## Import Data.
    proj, flat, dark, theta = dx.exchange.read_aps_13bm(fname, format = 'netcdf4')

    ## Flat-field correction of raw data.
    proj = tp.normalize(proj, flat = flat, dark = dark)

    ## Additional flat-field correction of raw data to negate need to mask.
    proj = tp.normalize_bg(proj, air = 10)

    ## Set rotation center.
    rot_center = tp.find_center_vo(proj)
    print('Center of rotation: ', rot_center)

    tp.minus_log(proj, out = proj)

    # Reconstruct object using Gridrec algorith.
    rec = tp.recon(proj, theta, center = rot_center, sinogram_order = False, algorithm = 'gridrec', filter_name = 'hann')
    rec = tp.remove_nan(rec)

    ## Writing data in netCDF3 .volume.
    ncfile = Dataset('filename.volume', 'w', format = 'NETCDF3_64BIT', clobber = True)
    NX = ncfile.createDimension('NX', rec.shape[2])
    NY = ncfile.createDimension('NY', rec.shape[1])
    NZ = ncfile.createDimension('NZ', rec.shape[0])
    volume = ncfile.createVariable('VOLUME', 'f4', ('NZ','NY','NX'))
    volume[:] = rec
    ncfile.close()
Beispiel #37
0
    def reconstruct(self,
                    data,
                    element,
                    center,
                    method,
                    beta,
                    delta,
                    iters,
                    thetas,
                    guess=None):
        '''
		load data for reconstruction and load variables for reconstruction
		make it sure that data doesn't have infinity or nan as one of
		entries
		'''
        recData = data[element, :, :, :]
        recData[recData == np.inf] = True
        recData[np.isnan(recData)] = True
        recCenter = np.array(center, dtype=np.float32)

        if method == 0:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='mlem',
                                 center=recCenter,
                                 num_iter=1,
                                 init_recon=guess,
                                 accelerated=False,
                                 device='cpu')
        elif method == 1:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='gridrec',
                                 center=recCenter,
                                 init_recon=guess)
            recon = recon / 1.49
        elif method == 2:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='art',
                                 num_iter=iters)
            recon = recon / 1.49
        elif method == 3:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='pml_hybrid',
                                 center=recCenter,
                                 reg_par=np.array([beta, delta],
                                                  dtype=np.float32),
                                 num_iter=iters)
        elif method == 4:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='pml_quad',
                                 center=recCenter,
                                 reg_par=np.array([beta, delta],
                                                  dtype=np.float32),
                                 num_iter=iters)
        elif method == 5:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='fbp')
        elif method == 6:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='sirt',
                                 num_iter=iters)
        elif method == 7:
            recon = tomopy.recon(recData,
                                 thetas * np.pi / 180,
                                 algorithm='tv',
                                 center=recCenter,
                                 reg_par=np.array([beta, delta],
                                                  dtype=np.float32),
                                 num_iter=iters)
        recon[recon < 0] = 0
        #tomopy.remove_nan() does not remove inf values
        recon = tomopy.remove_nan(recon)

        if np.isinf(recon).max():
            print(
                "WARNING: inf values found in reconstruction, consider reconstructing with less iterations"
            )
            print("inf values replaced with 0.001")
            recon[recon == np.inf] = 0.001

        return recon
Beispiel #38
0
def reconstruct(h5fname, sino, rot_center, args, blocked_views=None):

    # Read APS 32-BM raw data.
    proj, flat, dark, theta = dxchange.read_aps_32id(h5fname, sino=sino)

    # Manage the missing angles:
    if blocked_views is not None:
        print("Blocked Views: ", blocked_views)
        proj = np.concatenate((proj[0:blocked_views[0], :, :],
                               proj[blocked_views[1] + 1:-1, :, :]),
                              axis=0)
        theta = np.concatenate(
            (theta[0:blocked_views[0]], theta[blocked_views[1] + 1:-1]))

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,
                                   level=7,
                                   wname='sym16',
                                   sigma=1,
                                   pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    algorithm = args.algorithm
    ncores = args.ncores
    nitr = args.num_iter

    # always add algorithm
    _kwargs = {"algorithm": algorithm}

    # assign number of cores
    _kwargs["ncore"] = ncores

    # use the accelerated version
    if algorithm in ["mlem", "sirt"]:
        _kwargs["accelerated"] = True

    # don't assign "num_iter" if gridrec or fbp
    if algorithm not in ["fbp", "gridrec"]:
        _kwargs["num_iter"] = nitr

    sname = os.path.join(args.output_dir, 'proj_{}'.format(args.algorithm))
    print(proj.shape)
    tmp = np.zeros((proj.shape[0], proj.shape[2]))
    tmp[:, :] = proj[:, 0, :]
    output_image(tmp, sname + "." + args.format)

    # Reconstruct object.
    with timemory.util.auto_timer(
            "[tomopy.recon(algorithm='{}')]".format(algorithm)):
        print("Starting reconstruction with kwargs={}...".format(_kwargs))
        rec = tomopy.recon(data, theta, **_kwargs)
    print("Completed reconstruction...")

    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    obj = np.zeros(rec.shape, dtype=rec.dtype)
    label = "{} @ {}".format(algorithm.upper(), h5fname)
    quantify_difference(label, obj, rec)

    return rec
    def reconstruct(self,
                    data,
                    element,
                    center,
                    method,
                    beta,
                    delta,
                    iters,
                    thetas,
                    show_stats=False):
        '''
		load data for reconstruction and load variables for reconstruction
		make it sure that data doesn't have infinity or nan as one of
		entries
		'''
        recData = data[element, :, :, :]
        recData[recData == np.inf] = True
        recData[np.isnan(recData)] = True
        recCenter = np.array(center, dtype=np.float32)

        print("working fine")

        if method == 0:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='mlem',
                                      center=recCenter,
                                      num_iter=iters,
                                      accelerated=True,
                                      device='cpu')
        elif method == 1:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='gridrec')
        elif method == 2:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='art',
                                      num_iter=iters)
        elif method == 3:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_hybrid',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)
        elif method == 4:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='pml_quad',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)
        elif method == 5:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='fbp')
        elif method == 6:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='sirt',
                                      num_iter=iters)
        elif method == 7:
            self.recon = tomopy.recon(recData,
                                      thetas * np.pi / 180,
                                      algorithm='tv',
                                      center=recCenter,
                                      reg_par=np.array([beta, delta],
                                                       dtype=np.float32),
                                      num_iter=iters)

        #tomopy.remove_nan() does not remove inf values
        self.recon = tomopy.remove_nan(self.recon)

        if np.isinf(self.recon).max():
            print(
                "WARNING: inf values found in reconstruction, consider reconstructing with less iterations"
            )
            print("inf values replaced with 0.001")
            self.recon[self.recon == np.inf] = 0.001

        err, mse = self.assessRecon(self.recon, data, thetas, show_stats)
        print(mse)
        return self.recon
Beispiel #40
0
def reconstruct(h5fname, sino, rot_center, binning, algorithm='gridrec'):

    sample_detector_distance = 31      # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.17e-4    # Detector pixel size in cm (5x: 1.17e-4, 2X: 2.93e-4)
    monochromator_energy = 65    # Energy of incident wave in keV
    # used pink beam

    alpha = 4*1e-4                       # Phase retrieval coeff.
    zinger_level = 800                  # Zinger level for projections
    zinger_level_w = 1000               # Zinger level for white

    # Read APS 2-BM raw data.
    # DIMAX saves 3 files: proj, flat, dark
    # when loading the data set select the proj file (larger size)

    fname = os.path.splitext(h5fname)[0]    
 
    fbase = fname.rsplit('_', 1)[0]
    fnum = fname.rsplit('_', 1)[1]
    fext = os.path.splitext(h5fname)[1]  

    fnum_flat = str("%4.4d" % (int(fnum)+1))
    fnum_dark = str("%4.4d" % (int(fnum)+2))

    fnproj = fbase + '_' + fnum + fext
    fnflat = fbase + '_' + fnum_flat + fext
    fndark = fbase + '_' + fnum_dark + fext
    
    print('proj', fnproj)
    print('flat', fnflat)
    print('dark', fndark)
    # Read APS 2-BM DIMAX raw data.
    proj, dum, dum2, theta = dxchange.read_aps_32id(fnproj, sino=sino)
    dum3, flat, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)
    #flat, dum3, dum4, dum5 = dxchange.read_aps_32id(fnflat, sino=sino)          
    dum6, dum7, dark, dum8 = dxchange.read_aps_32id(fndark, sino=sino)

    # Flat-field correction of raw data.
    data = tomopy.normalize(proj, flat, dark, cutoff=1.4)

    # remove stripes
    data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)        
    
    # zinger_removal
    proj = tomopy.misc.corr.remove_outlier(proj, zinger_level, size=15, axis=0)
    flat = tomopy.misc.corr.remove_outlier(flat, zinger_level_w, size=15, axis=0)

    # Flat-field correction of raw data.
    ##data = tomopy.normalize(proj, flat, dark, cutoff=0.8)
    data = tomopy.normalize(proj, flat, dark)

    # remove stripes
    #data = tomopy.remove_stripe_fw(data,level=7,wname='sym16',sigma=1,pad=True)

    #data = tomopy.remove_stripe_ti(data, alpha=1.5)
    data = tomopy.remove_stripe_sf(data, size=150)

    # phase retrieval
    #data = tomopy.prep.phase.retrieve_phase(data,pixel_size=detector_pixel_size_x,dist=sample_detector_distance,energy=monochromator_energy,alpha=alpha,pad=True)

    print("Raw data: ", h5fname)
    print("Center: ", rot_center)

    data = tomopy.minus_log(data)

    data = tomopy.remove_nan(data, val=0.0)
    data = tomopy.remove_neg(data, val=0.00)
    data[np.where(data == np.inf)] = 0.00

    rot_center = rot_center/np.power(2, float(binning))
    data = tomopy.downsample(data, level=binning) 
    data = tomopy.downsample(data, level=binning, axis=1)

    # padding 
    N = data.shape[2]
    data_pad = np.zeros([data.shape[0],data.shape[1],3*N//2],dtype = "float32")
    data_pad[:,:,N//4:5*N//4] = data
    data_pad[:,:,0:N//4] = np.tile(np.reshape(data[:,:,0],[data.shape[0],data.shape[1],1]),(1,1,N//4))
    data_pad[:,:,5*N//4:] = np.tile(np.reshape(data[:,:,-1],[data.shape[0],data.shape[1],1]),(1,1,N//4))

    data = data_pad
    rot_center = rot_center+N//4

    nframes = 8 
    nproj = 1500
    theta = np.linspace(0, np.pi*nframes, nproj*nframes, endpoint=False)
    rec = np.zeros(
            (nframes, data.shape[1], data.shape[2], data.shape[2]), dtype='float32')
    for time_frame in range(0, nframes):
        rec0 = tomopy.recon(data[time_frame*nproj:(time_frame+1)*nproj], theta[time_frame*nproj:(
               time_frame+1)*nproj], center=rot_center, algorithm='gridrec')
        # Mask each reconstructed slice with a circle.
        rec[time_frame] = tomopy.circ_mask(rec0, axis=0, ratio=0.95)
    rec = rec[:,:,N//4:5*N//4,N//4:5*N//4]

        
    print("Algorithm: ", algorithm)
    
    return rec