Example #1
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("top", help="top directory where the tiff images are located: /data/")
    parser.add_argument("start", nargs='?', const=0, type=int, default=0, help="index of the first image: 10001 (default 0)")

    args = parser.parse_args()

    top = args.top

    # Select the sinogram range to reconstruct.
    start = 290
    end = 294

    print(top)
    # Read the Australian Synchrotron Facility data
    proj, flat, dark = dxchange.read_aps_5bm(top)
#    proj, flat, dark = dxchange.read_aps_5bm(fname, sino=(start, end))

    slider(proj)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0])

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

    slider(proj)
Example #2
0
def MatrixGeneration(filePath):
    filenames = [img for img in glob.glob(filePath)]
    print("\n\nHello\n\n")
    filenames.sort()

    temp = pylab.imread(filenames[0])
    w, d = temp.shape
    h = len(filenames)
    matrix = np.zeros((w, d, h), dtype=np.uint16)
    k = 0
    for img in filenames:  #On suppose que tous les fichiers sont des tif
        im = pylab.imread(img)
        matrix[:, :, k] = im
        k += 1
    print("Je suis là")
    #matrix=np.rot90(matrix, axes=(0,1))
    print(matrix.shape)
    matrix = tomopy.minus_log(matrix)
    print(matrix.shape)
    rec = tomopy.recon(matrix,
                       tomopy.angles(103, 0.471, 357.829),
                       algorithm='fbp')
    compteur = 0
    print(rec.shape)
    for i in range(len(rec)):
        #img = Image.fromarray(rec[i], mode='F')
        #img = img.convert("L")
        #img.save(str(i) + ".png", "PNG")
        compteur = compteur + 1
    print(compteur)
    pylab.imshow(rec[65], cmap='gray')
    #pylab.imshow(rec[250], cmap='gray')
    pylab.show()
    return 0
def rec_test(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1):

    print '\n#### Processing '+ file_name
    sino_start = sino_start + 200
    sino_end = sino_start + 2
    print "Test reconstruction of slice [%d]" % sino_start
    # Read HDF5 file.
    prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_end))

    # Manage the missing angles:
    theta  = tomopy.angles(prj.shape[0])
    prj = np.concatenate((prj[0:miss_angles[0],:,:], prj[miss_angles[1]+1:-1,:,:]), axis=0)
    theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1]))

    # normalize the prj
    prj = tomopy.normalize(prj, flat, dark)
    
    # remove ring artefacts
    prjn = tomopy.remove_stripe_fw(prj)

    # reconstruct 
    rec = tomopy.recon(prj[:,::reduce_amount,::reduce_amount], theta, center=float(best_center)/reduce_amount, algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False)
        
    # Write data as stack of TIFs.
    tomopy.io.writer.write_tiff_stack(rec, fname=output_name)

    print "Slice saved as [%s_00000.tiff]" % output_name
Example #4
0
def rec_test(file_name, sino_start, sino_end):

    print "\n#### Processing " + file_name
    sino_start = sino_start + 200
    sino_end = sino_start + 2
    print "Test reconstruction of slice [%d]" % sino_start
    # Read HDF5 file.
    prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_end))

    # Manage the missing angles:
    theta = tomopy.angles(prj.shape[0])
    prj = np.concatenate((prj[0 : miss_angles[0], :, :], prj[miss_angles[1] + 1 : -1, :, :]), axis=0)
    theta = np.concatenate((theta[0 : miss_angles[0]], theta[miss_angles[1] + 1 : -1]))

    # normalize the prj
    prj = tomopy.normalize(prj, flat, dark)

    # reconstruct
    rec = tomopy.recon(prj, theta, center=best_center, algorithm="gridrec", emission=False)

    # Write data as stack of TIFs.
    tomopy.io.writer.write_tiff_stack(rec, fname=output_name)

    print "Slice saved as [%s_00000.tiff]" % output_name
    # show the reconstructed slice
    pl.gray()
    pl.axis("off")
    pl.imshow(rec[0])
Example #5
0
def main(argv):
    try:
        opts, args = getopt.getopt(argv,"hc:s:",["core=","sino="])
    except getopt.GetoptError:
        print 'test.py -c <ncore> -s <nsino>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test.py -c <ncore> -s <nsino>'
            sys.exit()
        elif opt in ("-c", "--core"):
            ncore = int(arg)
        elif opt in ("-s", "--sino"):
            nsino = int(arg)
    file_name = '/local/decarlo/data/proj_10.hdf'
    output_name = './recon/proj10_rec'
    sino_start = 200


    # Read HDF5 file.
    prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_start+nsino))

    # Fix flats because sample did not move
    flat = np.full((flat.shape[0], flat.shape[1], flat.shape[2]), 1000)

    # Set angles
    theta  = tomopy.angles(prj.shape[0])
Example #6
0
def generate(phantom, args):
    """Return the simulated data for the given phantom."""
    with timemory.util.auto_timer("[tomopy.misc.phantom.{}]".format(phantom)):
        obj = getattr(tomopy.misc.phantom, phantom)(size=args.size)
        obj = tomopy.misc.morph.pad(obj, axis=1, mode='constant')
        obj = tomopy.misc.morph.pad(obj, axis=2, mode='constant')

        if args.partial:
            data_size = obj.shape[0]
            subset = list(args.subset)
            subset.sort()
            nbeg, nend = subset[0], subset[1]
            if nbeg == nend:
                nend += 1
            if not args.no_center:
                ndiv = (nend - nbeg) // 2
                offset = data_size // 2
                nbeg = (offset - ndiv)
                nend = (offset + ndiv)
            print("[partial]> slices = {} ({}, {}) of {}".format(
                nend - nbeg, nbeg, nend, data_size))
            obj = obj[nbeg:nend,:,:]

    with timemory.util.auto_timer("[tomopy.angles]"):
        ang = tomopy.angles(args.angles)

    with timemory.util.auto_timer("[tomopy.project]"):
        prj = tomopy.project(obj, ang)

    print("[dims]> projection = {}, angles = {}, object = {}".format(
        prj.shape, ang.shape, obj.shape))
    return [prj, ang, obj]
Example #7
0
def find_center_single(sino_name,
                       search_range,
                       search_step=1,
                       preprocess_single=False,
                       method='entropy',
                       output_fname='center_pos.txt'):

    log = open(output_fname, 'a')
    center_st, center_end = search_range
    sino = dxchange.read_tiff(sino_name)
    if sino.ndim == 2:
        sino = sino.reshape([sino.shape[0], 1, sino.shape[1]])
    if preprocess_single:
        sino = preprocess(np.copy(sino))
    if method == 'manual':
        write_center(sino,
                     tomopy.angles(sino.shape[0]),
                     dpath='center',
                     cen_range=(center_st, center_end, search_step))
    elif method == 'vo':
        mid = sino.shape[2] / 2
        smin = (center_st - mid) * 2
        smax = (center_end - mid) * 2
        center = find_center_vo(sino, smin=smin, smax=smax, step=search_step)
        internal_print('Center is {}.'.format(center))
        log.write('{}\n'.format(center))
        log.close()
def srxfftomo_findcenter(corrected_proj_tiff=None,
                         proj=None,
                         save_find_center=True,
                         autocheck=False,
                         check_cen_range_step=[390, 410, 1],
                         auto_selecslice=True,
                         cen_slice=400,
                         outpath=None,
                         samplename=None,
                         starting_angle=0,
                         last_angle=180):
    '''
    input needs to be either 
    1) corrected_proj_tiff, provided as a file path and name, 
    e.g. '/home/xf05id1/localdata/TomoCommissioning/testsample_01/testsample_01_corrected.tiff'
    it is a stack of tiff generated by srxfftomo_correction, where background and stage round out have been corrected
    2) projection numpy array, returned from srxfftomo_correction
    
    The 'proj' array assignment has priority. If it is None, proj was be read from corrected_proj_tiff 
    
    input example:
        check_cen_range_step = [390, 410, 1]
        cen_slice = 400    
    
    return: 
    proj (numpy array, float), with negative natural log has been taken
    
    '''
    if proj is None:
        print('proj is not asigned, load corrected projection from:',
              corrected_proj_tiff)
        proj = tifffile.imread(corrected_proj_tiff)
    else:
        print('proj array is not None, use it as is')

    if save_find_center is True:
        center_check_path = outpath + '/' + samplename + '/center_check/'
        print('saving find center value into ' + center_check_path)

        theta = tomopy.angles(proj.shape[0],
                              ang1=starting_angle,
                              ang2=last_angle)

        if autocheck is True:
            check_cen_range_step = [
                int(proj.shape[2] / 2) - 50,
                int(proj.shape[2]) / 2 + 50, 5
            ]

        if auto_selecslice is True:
            cen_slice = int(proj.shape[1] / 2)

        tomopy.write_center(proj,
                            theta,
                            center_check_path,
                            cen_range=check_cen_range_step,
                            ind=cen_slice,
                            mask=True)

    return proj
def main():
    #****************************************************************************
    file_name = '/local/dataraid/databank/dataExchange/tmp/Australian_rank3.h5'
    output_name = '/local/dataraid/databank/dataExchange/tmp/rec/Australian_rank3'    
    sino_start = 290    
    sino_end = 294    

    # Read HDF5 file.
    exchange_rank = 3;
    prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, exchange_rank, sino=(sino_start, sino_end))
    theta  = tomopy.angles(prj.shape[0])

    # normalize the data
    prj = tomopy.normalize(prj, flat, dark)

    best_center=1184
    print "Best Center: ", best_center
    calc_center = best_center
    #calc_center = tomopy.find_center(prj, theta, emission=False, ind=0, init=best_center, tol=0.8)
    print "Calculated Center:", calc_center
    
    # reconstruct 
    rec = tomopy.recon(prj, theta, center=calc_center, algorithm='gridrec', emission=False)
    #rec = tomopy.circ_mask(rec, axis=0)
    
    # Write data as stack of TIFs.
    tomopy.io.writer.write_tiff_stack(rec, fname=output_name)
    plt.gray()
    plt.axis('off')
    plt.imshow(rec[0])
Example #10
0
def generate(phantom, args):
    """Return the simulated data for the given phantom."""
    with timemory.util.auto_timer("[tomopy.misc.phantom.{}]".format(phantom)):
        obj = getattr(tomopy.misc.phantom, phantom)(size=args.size)
        obj = tomopy.misc.morph.pad(obj, axis=1, mode='constant')
        obj = tomopy.misc.morph.pad(obj, axis=2, mode='constant')

        if args.partial:
            data_size = obj.shape[0]
            subset = list(args.subset)
            subset.sort()
            nbeg, nend = subset[0], subset[1]
            if nbeg == nend:
                nend += 1
            if not args.no_center:
                ndiv = (nend - nbeg) // 2
                offset = data_size // 2
                nbeg = (offset - ndiv)
                nend = (offset + ndiv)
            print("[partial]> slices = {} ({}, {}) of {}".format(
                nend - nbeg, nbeg, nend, data_size))
            obj = obj[nbeg:nend, :, :]

    with timemory.util.auto_timer("[tomopy.angles]"):
        ang = tomopy.angles(args.angles)

    with timemory.util.auto_timer("[tomopy.project]"):
        prj = tomopy.project(obj, ang)

    print("[dims]> projection = {}, angles = {}, object = {}".format(
        prj.shape, ang.shape, obj.shape))
    return [prj, ang, obj]
Example #11
0
def main(arg):

    fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5'
    
    # Read the hdf raw data.
    sino, sflat, sdark, th = dxchange.read_aps_32id(fname)

    slider(sino)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(sino.shape[1], ang1=0.0, ang2=180.0)

    print(sino.shape, sdark.shape, sflat.shape, theta.shape)

    # Quick normalization just to see something ....
    ndata = sino / float(np.amax(sino))
    slider(ndata)

    # Find rotation center.
    rot_center = 962

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

    ndata = tomopy.minus_log(ndata)
    
    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(ndata, theta, center=rot_center, sinogram_order=True, 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')
Example #12
0
def reconstruct_tomcat(proj, angle_offset, rot_center):
    theta = tomopy.angles(proj.shape[0]) + angle_offset
    recon = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec')
    # rot_center = tomopy.find_center(proj, theta, init=882, ind=0, tol=0.5)
    print(f"Reconstruction shape: {recon.shape}")

    recon = tomopy.circ_mask(recon, axis=0, ratio=0.90)
    return recon
Example #13
0
    def estimate_dose(self,
                      energy,
                      sample,
                      flux_rate,
                      exposure,
                      mode='tomosaic'):
        """
        Estimate radiation dose.
        :param flux_rate: photon flux rate (ph/s/mm)
        :param exposure: exposure time (ms)
        :param mode: "tomosaic" or "local"
        :return: radiation energy deposition (J/m^2)
        """
        print('Calculating dose.')
        assert mode in ('tomosaic', 'local') and isinstance(sample, Sample)
        n_proj = self.raw_sino.shape[0]
        n_fov = len(self.inst.stage_positions) if mode == 'tomosaic' else len(
            self.inst.center_positions)
        fov = self.inst.fov
        fov2 = int(fov / 2)
        w = self.raw_sino.shape[1]
        w2 = w / 2.

        cot = lambda x: 1. / np.tan(x)
        csc = lambda x: 1. / np.sin(x)

        # assume disk sample
        # use intersection length of the central ray as thickness
        e_abs = 0
        n0 = flux_rate * exposure * fov * self.pixel_size * 1e-6
        if mode == 'tomosaic':
            for x0 in self.inst.stage_positions:
                t = 2 * np.sqrt(w2**2 - (w2 - x0)**2) * self.pixel_size
                f_abs = 1 - np.exp(-sample.get_attenuation_coeff(energy) * t)
                e_abs += (f_abs * n0 * n_proj) * energy
                # x1 = x0 - fov2 if x0 - fov2 >= 0 else 0
                # x2 = x1 + fov
                # t = 2 * np.sqrt(w2 ** 2 - (w2 - np.arange(x1, x2, dtype='float')) ** 2)
                # t = t * self.pixel_size
                # n_abs = 1 - np.exp(-sample.get_attenuation_coeff(energy) * t)
                # n_abs = np.sum(n_abs) / fov
        else:
            theta_ls = tomopy.angles(n_proj, ang1=0, ang2=180)
            for (y0, x0) in self.inst.center_positions:
                for theta in theta_ls:
                    if theta == 0 or np.abs(theta - np.pi / 2) < 1e-6:
                        a = np.abs(w2 - x0)
                    else:
                        a = np.abs((cot(theta) - 1) * w2 + y0 -
                                   cot(theta) * x0) / np.abs(csc(theta))
                    if a < w2:
                        t = 2 * np.sqrt(w2**2 - a**2) * self.pixel_size
                        f_abs = 1 - np.exp(
                            -sample.get_attenuation_coeff(energy) * t)
                        e_abs += (f_abs * n0) * energy
        e_abs = e_abs * ElectronCharge * 1e3
        return e_abs / (np.pi * (w2 * self.pixel_size * 1e-6)**2)
Example #14
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("top", help="top directory where the tiff images are located: /data/")
    parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)")

    args = parser.parse_args()

    top = args.top
    index_start = int(args.start)

    template = os.listdir(top)[0]

    nfile = len(fnmatch.filter(os.listdir(top), '*.tif'))
    index_end = index_start + nfile
    ind_tomo = range(index_start, index_end)
    
    fname = top + template

    print (nfile, index_start, index_end, fname)


    # Select the sinogram range to reconstruct.
    start = 0
    end = 512
    sino=(start, end)

    # Read the tiff raw data.
    ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None))
 
    # Normalize to 1 using the air counts
    ndata = tomopy.normalize_bg(ndata, air=5)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(ndata.shape[0])

    ndata = tomopy.minus_log(ndata)

    # Set binning and number of iterations
    binning = 8
    iters = 21

    print("Original", ndata.shape)
    ndata = tomopy.downsample(ndata, level=binning, axis=1)
#    ndata = tomopy.downsample(ndata, level=binning, axis=2)
    print("Processing:", ndata.shape)

    fdir = 'aligned' + '/noblur_iter_' + str(iters) + '_bin_' + str(binning) 

    print(fdir)
    cprj, sx, sy, conv = alignment.align_seq(ndata, theta, fdir=fdir, iters=iters, pad=(10, 10), blur=False, save=True, debug=True)

    np.save(fdir + '/shift_x', sx)
    np.save(fdir + '/shift_y', sy)

    # Write aligned projections as stack of TIFs.
    dxchange.write_tiff_stack(cprj, fname=fdir + '/radios/image')
Example #15
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument("top", help="top directory where the tiff images are located: /data/")
    parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 1000 (default 1)")

    args = parser.parse_args()

    top = args.top
    index_start = int(args.start)

    template = os.listdir(top)[0]

    nfile = len(fnmatch.filter(os.listdir(top), '*.tif'))
    index_end = index_start + nfile
    ind_tomo = range(index_start, index_end)
    
    fname = top + template

    print (nfile, index_start, index_end, fname)


    # Select the sinogram range to reconstruct.
    start = 0
    end = 512
    sino=(start, end)

    # Read the tiff raw data.
    ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None))

    print(ndata.shape)
    binning = 8
    ndata = tomopy.downsample(ndata, level=binning, axis=1)
    print(ndata.shape)
    
    # Normalize to 1 using the air counts
    ndata = tomopy.normalize_bg(ndata, air=5)

    ## slider(ndata)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(ndata.shape[0])
   
    rot_center = 960
    print("Center of rotation: ", rot_center)

    ndata = tomopy.minus_log(ndata)

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(ndata, 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='/local/dataraid/mark/rec/recon')
Example #16
0
def reconstruct(sname, rot_center, ovlpfind, s_start, s_end):
    fname = dfolder + sname + '.h5'
    print(fname)
    start = s_start
    end = s_end
    chunks = 24
    num_sino = (end - start) // chunks
    for m in range(chunks):
        sino_start = start + num_sino * m
        sino_end = start + num_sino * (m + 1)
        start_read_time = time.time()
        proj, flat, dark, thetat = dxchange.read_aps_2bm(fname,
                                                         sino=(sino_start,
                                                               sino_end))
        print('   done read in %0.1f min' %
              ((time.time() - start_read_time) / 60))
        dark = proj[9001:9002]
        flat = proj[0:1]
        proj = proj[1:9000]
        theta = tomopy.angles(proj.shape[0], 0., 360.)
        proj = tomopy.sino_360_to_180(proj, overlap=ovlpfind, rotation='right')
        proj = tomopy.remove_outlier(proj, dif=0.4)
        proj = tomopy.normalize_bg(proj, air=10)
        proj = tomopy.minus_log(proj)
        center = rot_center
        start_ring_time = time.time()
        proj = tomopy.remove_stripe_fw(proj, wname='sym5', sigma=4, pad=False)
        proj = tomopy.remove_stripe_sf(proj, size=3)
        print('   done pre-process in %0.1f min' %
              ((time.time() - start_ring_time) / 60))
        start_phase_time = time.time()
        proj = tomopy.retrieve_phase(proj,
                                     pixel_size=detector_pixel_size_x,
                                     dist=sample_detector_distance,
                                     energy=energy,
                                     alpha=alpha,
                                     pad=True,
                                     ncore=None,
                                     nchunk=None)
        print('   done phase retrieval in %0.1f min' %
              ((time.time() - start_phase_time) / 60))
        start_recon_time = time.time()
        rec = tomopy.recon(proj,
                           theta,
                           center=center,
                           algorithm='gridrec',
                           filter_name='ramalk')
        tomopy.circ_mask(rec, axis=0, ratio=0.95)
        print("Reconstructed", rec.shape)
        dxchange.write_tiff_stack(rec,
                                  fname=dfolder + '/' + sname + '/' + sname,
                                  overwrite=True,
                                  start=sino_start)
        print('   Chunk reconstruction done in %0.1f min' %
              ((time.time() - start_recon_time) / 60))
    print("Done!")
def generate(phantom="shepp3d", nsize=512, nangles=360):

    with timemory.util.auto_timer("[tomopy.misc.phantom.{}]".format(phantom)):
        obj = getattr(tomopy.misc.phantom, phantom)(size=nsize)
    with timemory.util.auto_timer("[tomopy.angles]"):
        ang = tomopy.angles(nangles)
    with timemory.util.auto_timer("[tomopy.project]"):
        prj = tomopy.project(obj, ang)

    return [prj, ang, obj]
Example #18
0
def generate(phantom="shepp3d", nsize=512, nangles=360):

    with timemory.util.auto_timer("[tomopy.misc.phantom.{}]".format(phantom)):
        obj = getattr(tomopy.misc.phantom, phantom)(size=nsize)
    with timemory.util.auto_timer("[tomopy.angles]"):
        ang = tomopy.angles(nangles)
    with timemory.util.auto_timer("[tomopy.project]"):
        prj = tomopy.project(obj, ang)

    return [prj, ang, obj]
Example #19
0
def main():

        oversamp_factor = 1.25 #For NUFFT
        parser = argparse.ArgumentParser()
        inputs = bl832inputs_parser(parser)

        num_slice = inputs['z_numElts']
        num_angles= inputs['num_views']/inputs['view_subsmpl_fact']
        
        pad_size = np.int16(inputs['x_width']*oversamp_factor)
        num_iter = inputs['num_iter']
        nufft_scaling = (np.pi/pad_size)**2
        
        
        tomo, flats, darks, floc = tomopy.read_als_832h5(inputs['input_hdf5'],ind_tomo=range(1,inputs['num_views']+1,inputs['view_subsmpl_fact']),sino=(inputs['z_start'], inputs['z_start']+inputs['z_numElts'], 1))
        print('Data read complete')


        print('Generating angles')
        theta = tomopy.angles(num_angles)

        #Need to modify to return the raw counts for noise estimation 
        print('Normalization')
        tomo,weight = normalize_bo(tomo, flats, darks,inputs['num_dark'])

        print('Ring removal')
        tomo = tomopy.remove_stripe_fw(tomo)
        
        ################## GPU MBIR ######################
        input_params={}
        input_params['gpu_device']=inputs['gpu_device']
        input_params['oversamp_factor']=oversamp_factor
        input_params['num_iter']=num_iter
        input_params['p']=inputs['p']
        input_params['smoothness']=inputs['smoothness']
        t=time.time()
        rec_mbir_final = gpuMBIR(tomo,theta,inputs['rot_center'],input_params)
        elapsed_time = (time.time()-t)
        print('Time for reconstucting using GPU-MBIR of %d slices with %d iter : %f' % (num_slice,num_iter,elapsed_time))

        pg.image(rec_mbir_final);pg.QtGui.QApplication.exec_()
#        np.save('/home/svvenkatakrishnan/results/mbir_notch1080_70slice',rec_mbir_final)
        
#        fig = plt.figure()
#        sirt_camTomo = rec_sirt_final[124]
#        plt.imshow(sirt_camTomo,cmap=plt.cm.Greys_r,vmin=0,vmax=0.00075)
#        plt.colorbar()
#        fig.suptitle('Tomopy GPU-SIRT Reconstruction')
#        plt.draw()
#        plt.show()

        print 'main: Done!'
Example #20
0
def recon_slice(row_sino, center_pos, sinogram_order=False, algorithm=None,
        init_recon=None, ncore=None, nchunk=None, **kwargs):
    t = time.time()
    ang = tomopy.angles(row_sino.shape[0])
    print(row_sino.shape)
    row_sino = row_sino.astype('float32')
    # row_sino = tomopy.normalize_bg(row_sino) # WARNING: normalize_bg can unpredicatably give bad results for some slices
    row_sino = tomopy.remove_stripe_ti(row_sino, alpha=4)
    rec = tomopy.recon(row_sino, ang, center=center_pos, sinogram_order=sinogram_order, algorithm=algorithm,
        init_recon=init_recon, ncore=ncore, nchunk=nchunk, **kwargs)

    print('recon:           ' + str(time.time() - t))
    return rec
Example #21
0
def main(arg):

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "top", help="top directory where the tiff images are located: /data/")
    parser.add_argument("start",
                        nargs='?',
                        const=1,
                        type=int,
                        default=1,
                        help="index of the first image: 1000 (default 1)")

    args = parser.parse_args()

    top = args.top
    index_start = int(args.start)

    template = os.listdir(top)[0]

    nfile = len(fnmatch.filter(os.listdir(top), '*.tiff'))
    index_end = index_start + nfile
    ind_tomo = range(index_start, index_end)

    fname = top + template

    print(nfile, index_start, index_end, fname)

    # Select the sinogram range to reconstruct.
    start = 70
    end = 72
    sino = (start, end)

    # Read the tiff raw data.
    ndata = dxchange.read_tiff_stack(fname, ind=ind_tomo, slc=(sino, None))

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(ndata.shape[0])

    rot_center = 251
    print("Center of rotation: ", rot_center)

    #ndata = tomopy.minus_log(ndata)

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(ndata, 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='/local/dataraid/mark/rec/recon')
def main(argv):
    try:
        opts, args = getopt.getopt(argv,"hc:s:",["core=","sino="])
    except getopt.GetoptError:
        print 'test.py -c <ncore> -s <nsino>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test.py -c <ncore> -s <nsino>'
            sys.exit()
        elif opt in ("-c", "--core"):
            ncore = int(arg)
        elif opt in ("-s", "--sino"):
            nsino = int(arg)

    # **********************************************
    #file_name = '/local/decarlo/data/proj_10.hdf'
    #output_name = './recon/proj10_rec'
    #sino_start = 0
    #sino_end = 2048
    # **********************************************
    file_name = '/local/decarlo/data/Hornby_APS_2011.h5'
    output_name = './recon/Hornby_APS_2011_'
    best_center=1024
    sino_start = 0
    sino_end = 1792
    # **********************************************

    step_00 = time.time()
    step_02_delta_total = 0
    
    count = 0
    while (sino_start <= (sino_end - nsino)):
        # Read HDF5 file.
        prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_start, sino_start+nsino))

        # Fix flats because sample did not move
        flat = np.full((flat.shape[0], flat.shape[1], flat.shape[2]), 1000)

        # Set angles
        theta  = tomopy.angles(prj.shape[0])

        # normalize the prj
        prj = tomopy.normalize(prj, flat, dark)

        best_center = 1298
        step_01 = time.time()

        # reconstruct 
        rec = tomopy.recon(prj, theta, center=best_center, algorithm='gridrec', emission=False, ncore = ncore)
Example #23
0
    def execute(self, input_dataobj, output_dataobj):
        import tomopy
        projs = self.data[input_dataobj]
        if (projs is None):
            self.LogError('Please specific input Projections')
            return False

        if (len(projs.shape) != 3):
            self.LogError('The shape of Projections should been 3! here is ' +
                          str(projs.shape))
            return False

        dataobj = tomopy.angles(projs.shape[0])
        self.data[output_dataobj] = dataobj
        return True
Example #24
0
def recon(path='NUS 3D/sample2', save_path='./model/'):
    save_path = os.path.join(save_path, path.split('/')[-1] + '_recon')
    os.makedirs(save_path, exist_ok=True)
    paths = os.listdir(path)
    paths = sorted(paths)
    for file_name in paths[:5]:
        ph = os.path.join(path, file_name)
        sh = os.path.join(save_path, file_name)
        obj = dxchange.reader.read_tiff(fname=ph,
                                        slc=None)  # Generate an object
        ang = tomopy.angles(180)  # Generate uniformly spaced tilt angles
        obj = obj.astype(np.float32)
        sim = tomopy.project(obj, ang)  # Calculate projections
        rec = tomopy.recon(sim, ang, algorithm='art')  # Reconstruct object
        tifffile.imwrite(sh, rec)
def srxfftomo_recon(corrected_proj_tiff=None,
                    proj=None,
                    rot_center=None,
                    recon_algorithm='art',
                    recon_section=None,
                    outpath=None,
                    recon_outpath='recon',
                    samplename=None,
                    starting_angle=0,
                    last_angle=180):
    '''
    input needs to be either 
    1) corrected_proj_tiff, provided as a file path and name, 
    e.g. '/home/xf05id1/localdata/TomoCommissioning/testsample_01/testsample_01_corrected.tiff'
    it is a stack of tiff generated by srxfftomo_correction, where background and stage round out have been corrected
    2) projection numpy array, returned from srxfftomo_correction
    
    The 'proj' array assignment has priority. If it is None, proj was be read from corrected_proj_tiff 
    
    recon_section is a list of upper and lower bounds for y range, e.g. [380, 420]
    
    '''

    if proj is None:
        print('proj is not asigned, load corrected projection from:',
              corrected_proj_tiff)
        proj = tifffile.imread(corrected_proj_tiff)
    else:
        print('proj array is not None, use it as is')

    print('running reconstruction')

    if recon_section is not None:
        proj = proj[:, recon_section[0]:recon_section[1], :]

    theta = tomopy.angles(proj.shape[0], ang1=starting_angle, ang2=last_angle)
    print(proj.shape, theta.shape)
    proj = tomopy.remove_stripe_ti(proj)
    rec = tomopy.recon(proj,
                       theta,
                       center=rot_center,
                       algorithm=recon_algorithm)

    print('saving reconstruction into')
    recon_outfile = outpath + '/' + samplename + '/' + recon_outpath + '/' + samplename + '_recon'
    tomopy.write_tiff_stack(rec, fname=recon_outfile)

    return rec
Example #26
0
def choix_fichier(self):
    # L'utilisateur choisi le dossier dans lequel se trouve les fichiers
    path = QtWidgets.QFileDialog.getExistingDirectory(
        self,
        "Open a folder",
        os.sep.join(
            (os.path.expanduser('~'), 'Desktop'
             )),  #Permet d'afficher le bureau à l'ouverture de la fenêtre
        options=QtWidgets.QFileDialog.ShowDirsOnly)
    #Gestion de l'exception dans le cas où l'utilisateur choisit un mauvais dossier
    #Si aucun dossier n'est sélectionné path = 0 et il ne se passe rien
    if path:
        try:
            MatrixGeneration(path + '/*.tif')
        except IndexError:
            return tomopy.project(tomopy.shepp3d(), tomopy.angles(180))
Example #27
0
def choix_fichier(self):
    factor = float(str(self.selectFactor.currentText()))
    # L'utilisateur choisi le dossier dans lequel se trouve les fichiers
    path = QtWidgets.QFileDialog.getExistingDirectory(
            self,
            "Open a folder",
            os.sep.join((os.path.expanduser('~'), 'Desktop')), #Permet d'afficher le bureau à l'ouverture de la fenêtre
            options = QtWidgets.QFileDialog.ShowDirsOnly
            )
    #Gestion de l'exception dans le cas où l'utilisateur choisit un mauvais dossier
    #Dans ce cas on envoie des projections générées virtuellement par tomopy
    #Si aucun dossier n'est sélectionné path = 0 et il ne se passe rien
    if path:
        try:
            MatrixGeneration(self, path, factor)
        except IndexError:
            return tomopy.project(tomopy.shepp3d(), tomopy.angles(180))
Example #28
0
def recon_hdf5_mpi(src_fanme, dest_folder, sino_range, sino_step, center_vec, shift_grid, dtype='float32',
               algorithm='gridrec', tolerance=1, save_sino=False, sino_blur=None, **kwargs):
    """
    Reconstruct a single tile, or fused HDF5 created using util/total_fusion. MPI supported.
    """

    raise DeprecationWarning

    if rank == 0:
        if not os.path.exists(dest_folder):
            os.mkdir(dest_folder)
    sino_ini = int(sino_range[0])
    sino_end = int(sino_range[1])
    f = h5py.File(src_fanme)
    dset = f['exchange/data']
    full_shape = dset.shape
    theta = tomopy.angles(full_shape[0])
    center_vec = np.asarray(center_vec)
    sino_ls = np.arange(sino_ini, sino_end, sino_step, dtype='int')
    grid_bins = np.ceil(shift_grid[:, 0, 0])

    t0 = time.time()
    alloc_set = allocate_mpi_subsets(sino_ls.size, size, task_list=sino_ls)
    for slice in alloc_set[rank]:
        print('    Rank {:d}: reconstructing {:d}'.format(rank, slice))
        grid_line = np.digitize(slice, grid_bins)
        grid_line = grid_line - 1
        center = center_vec[grid_line]
        data = dset[:, slice, :]
        if sino_blur is not None:
            data = gaussian_filter(data, sino_blur)
        data = data.reshape([full_shape[0], 1, full_shape[2]])
        data[np.isnan(data)] = 0
        data = data.astype('float32')
        if save_sino:
            dxchange.write_tiff(data[:, slice, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, center))
        # data = tomopy.remove_stripe_ti(data)
        rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs)
        # rec = tomopy.remove_ring(rec)
        rec = tomopy.remove_outlier(rec, tolerance)
        rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
        dxchange.write_tiff(rec, fname='{:s}/recon/recon_{:05d}_{:d}'.format(dest_folder, slice, center), dtype=dtype)

    print('Rank {:d} finished in {:.2f} s.'.format(rank, time.time()-t0))
    return
Example #29
0
def rec_full(file_name, sino_start, sino_end):

    print "\n#### Processing " + file_name

    chunks = 10  # number of data chunks for the reconstruction

    nSino_per_chunk = (sino_end - sino_start) / chunks
    print "Reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % (
        (sino_end - sino_start),
        sino_start,
        sino_end,
        chunks,
        nSino_per_chunk,
    )

    for iChunk in range(0, chunks):
        print "\n  -- chunk # %i" % (iChunk + 1)
        sino_chunk_start = sino_start + nSino_per_chunk * iChunk
        sino_chunk_end = sino_start + nSino_per_chunk * (iChunk + 1)
        print "\n  --------> [%i, %i]" % (sino_chunk_start, sino_chunk_end)

        if sino_chunk_end > sino_end:
            break

        # Read HDF5 file.
        prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_chunk_start, sino_chunk_end))

        # Manage the missing angles:
        theta = tomopy.angles(prj.shape[0])
        prj = np.concatenate((prj[0 : miss_angles[0], :, :], prj[miss_angles[1] + 1 : -1, :, :]), axis=0)
        theta = np.concatenate((theta[0 : miss_angles[0]], theta[miss_angles[1] + 1 : -1]))

        # normalize the prj
        prj = tomopy.normalize(prj, flat, dark)

        # reconstruct
        rec = tomopy.recon(prj, theta, center=best_center, algorithm="gridrec", emission=False)

        print output_name

        # Write data as stack of TIFs.
        tomopy.io.writer.write_tiff_stack(rec, fname=output_name, start=sino_chunk_start)
Example #30
0
def read_ALS(path: str, sinoindex: int = 5, chunksize: int = 5) -> (np.ndarray, np.ndarray, np.ndarray, np.ndarray):
    import h5py
    import numpy
    from math import pi
    import tomopy

    h5file = h5py.File(path, "r")
    group = list(h5file.keys())[0]
    dgroup = h5file[group]

    nangles = int(dgroup.attrs['nangles'])
    h5file.close()

    angles = tomopy.angles(nangles)

    tomo, flats, darks, _ = dxchange.read_als_832h5(path, sino=(sinoindex, sinoindex+chunksize, 1))

    print("PROCESSING SINOINDEX", tomo.shape)
    # print("HERE", tomo, flats, darks, angles)
    return tomo, flats, darks, angles
Example #31
0
def main():
    obj = dxchange.reader.read_tiff(
        fname=
        './NUS 3D/sample1/sample1 zscan 1xzoom 50um fiber 70um 1um step.tif',
        slc=None)  # Generate an object

    ang = tomopy.angles(180)  # Generate uniformly spaced tilt angles
    obj = obj.astype(np.float32)
    sim = tomopy.project(obj, ang)  # Calculate projections
    rec = tomopy.recon(sim, ang, algorithm='art')  # Reconstruct object
    print(rec.shape)

    # show 64th slice of the reconstructed object.
    # mlab.imshow(rec[64], colormap='gray')
    # mlab.show()

    mlab.contour3d(rec, contours=1, colormap='gist_gray',
                   transparent=True)  # transparent: 该对象可以透明表示,可以查看内部
    mlab.show()
    return
def rec_full(file_name, sino_start, sino_end, astra_method, extra_options, num_iter=1):

    print '\n#### Processing '+ file_name

    chunks = 10 # number of data chunks for the reconstruction

    nSino_per_chunk = (sino_end - sino_start)/chunks
    print "Reconstructing [%d] slices from slice [%d] to [%d] in [%d] chunks of [%d] slices each" % ((sino_end - sino_start), sino_start, sino_end, chunks, nSino_per_chunk)
    strt = 0
    for iChunk in range(0,chunks):
        print '\n  -- chunk # %i' % (iChunk+1)
        sino_chunk_start = sino_start + nSino_per_chunk*iChunk 
        sino_chunk_end = sino_start + nSino_per_chunk*(iChunk+1)
        print '\n  --------> [%i, %i]' % (sino_chunk_start, sino_chunk_end)
        
        if sino_chunk_end > sino_end: 
            break
                
        # Read HDF5 file.
        prj, flat, dark = tomopy.io.exchange.read_aps_32id(file_name, sino=(sino_chunk_start, sino_chunk_end))

        # Manage the missing angles:
        theta  = tomopy.angles(prj.shape[0])
        prj = np.concatenate((prj[0:miss_angles[0],:,:], prj[miss_angles[1]+1:-1,:,:]), axis=0)
        theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]+1:-1]))

        # normalize the prj
        prj = tomopy.normalize(prj, flat, dark)
        
        # remove ring artefacts
        prj = tomopy.remove_stripe_fw(prj)

        # reconstruct 
        rec = tomopy.recon(prj[:,::reduce_amount,::reduce_amount], theta, center=float(best_center)/reduce_amount, algorithm=tomopy.astra, options={'proj_type':proj_type,'method':astra_method,'extra_options':extra_options,'num_iter':num_iter}, emission=False)
        
        print output_name

        # Write data as stack of TIFs.
        tomopy.io.writer.write_tiff_stack(rec, fname=output_name, start=strt)
        strt += prj[:,::reduce_amount,:].shape[1]
Example #33
0
    def reconstruct(self,
                    center=None,
                    mask_ratio=1,
                    poisson_maxcount=None,
                    remove_ring=True):

        if center is None:
            center = self.center
        if self.padded:
            ind = int((self.sinogram.shape[1] - self.shape[1]) / 2)
            center += ind
        nang = self.sinogram.shape[0]
        theta = tomopy.angles(nang, ang1=0, ang2=self.fin_angle)
        data = self.sinogram[:, np.newaxis, :]
        data = tomopy.remove_stripe_ti(data)
        # dxchange.write_tiff(np.squeeze(data), '/raid/home/mingdu/data/shirley/local_tomo/temp/raw', dtype='float32')
        if poisson_maxcount is not None:
            if self.is_mlogged:
                data = np.exp(-data)
            data = self.add_poisson_noise(data, max_count=poisson_maxcount)
            if self.is_mlogged:
                data = -np.log(data)
            m_value = np.mean(data[np.isfinite(data)])
            data[np.isinf(data)] = m_value
            # dxchange.write_tiff(np.squeeze(data), '/raid/home/mingdu/data/shirley/local_tomo/temp/noise_sino', dtype='float32')
        rec = tomopy.recon(data,
                           theta,
                           center=center,
                           algorithm='gridrec',
                           filter_name='parzen')
        if self.padded:
            rec = rec[:, ind:ind + self.shape[1], ind:ind + self.shape[1]]
        if remove_ring:
            rec = tomopy.remove_ring(rec)
        rec = np.squeeze(rec)
        # if self.normalized_bg:
        # rec = rec * self.scaler
        self.recon_mask = tomopy.misc.corr._get_mask(rec.shape[0],
                                                     rec.shape[1], mask_ratio)
        self.recon = rec
Example #34
0
def main(arg):

    fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5'
    
    # Read the hdf raw data.
    sino, sflat, sdark, th = dxchange.read_aps_32id(fname)

    slider(sino)
    proj = np.swapaxes(sino,0,1)
    flat = np.swapaxes(sflat,0,1)
    dark = np.swapaxes(sdark,0,1)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0], ang1=0.0, ang2=180.0)

    print(proj.shape, dark.shape, flat.shape, theta.shape)

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

    # Find rotation center.
    rot_center = 962

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

    ndata = tomopy.minus_log(ndata)
    
    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(ndata, 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')
Example #35
0
def main(arg):

    fname = '/local/dataraid/elettra/Oak_16bit_slice343_all_repack.h5'

    # Read the hdf raw data.
    sino, sflat, sdark, th = dxchange.read_aps_32id(fname)

    slider(sino)
    proj = np.swapaxes(sino, 0, 1)
    flat = np.swapaxes(sflat, 0, 1)
    dark = np.swapaxes(sdark, 0, 1)

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0], ang1=0.0, ang2=180.0)

    print(proj.shape, dark.shape, flat.shape, theta.shape)

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

    # Find rotation center.
    rot_center = 962

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

    ndata = tomopy.minus_log(ndata)

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(ndata, 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')
Example #36
0
def reconstruct(sname, rot_center, ovlpfind, s_start, s_end):
    fname = dfolder + sname + '.h5'
    print (fname)
    start = s_start  
    end =   s_end
    chunks = 24 
    num_sino = (end - start) // chunks
    for m in range(chunks):
        sino_start = start + num_sino * m
        sino_end = start + num_sino * (m + 1)
        start_read_time = time.time()
        proj, flat, dark, thetat = dxchange.read_aps_2bm(fname, sino=(sino_start, sino_end))
        print('   done read in %0.1f min' % ((time.time() - start_read_time)/60))
        dark = proj[9001:9002]
        flat = proj[0:1]
        proj = proj[1:9000]
        theta = tomopy.angles(proj.shape[0], 0., 360.)
        proj = tomopy.sino_360_to_180(proj, overlap=ovlpfind, rotation='right')
        proj = tomopy.remove_outlier(proj, dif=0.4)
        proj = tomopy.normalize_bg(proj, air=10)
        proj = tomopy.minus_log(proj)
        center = rot_center
        start_ring_time = time.time()
        proj = tomopy.remove_stripe_fw(proj, wname='sym5', sigma=4, pad=False)
        proj = tomopy.remove_stripe_sf(proj, size=3)
        print('   done pre-process in %0.1f min' % ((time.time() - start_ring_time)/60))
        start_phase_time = time.time()
        proj = tomopy.retrieve_phase(proj, pixel_size=detector_pixel_size_x, dist=sample_detector_distance, energy=energy, alpha=alpha, pad=True, ncore=None, nchunk=None)
        print('   done phase retrieval in %0.1f min' % ((time.time() - start_phase_time)/60))
        start_recon_time = time.time()
        rec = tomopy.recon(proj, theta, center=center, algorithm='gridrec', filter_name='ramalk')
        tomopy.circ_mask(rec, axis=0, ratio=0.95)
        print ("Reconstructed", rec.shape)
        dxchange.write_tiff_stack(rec, fname = dfolder + '/' + sname + '/' + sname, overwrite=True, start=sino_start)
        print('   Chunk reconstruction done in %0.1f min' % ((time.time() - start_recon_time)/60))
    print ("Done!")
Example #37
0
# data I/O: http://tomopy.readthedocs.org/en/latest/source/api/tomopy.io.writer.html
# data I/O: http://tomopy.readthedocs.org/en/latest/source/api/tomopy.io.reader.html

fname = "/home/oxygen/DGURSOY/Data/Hornby_SLS_2011.h5"


# Load real data from APS ------------

# load DataExchange data
t = time.time()
prj, flt, drk = tomopy.read_aps_2bm(fname, sino=(1000, 1032))
print prj.shape, flt.shape, drk.shape
print time.time() - t

# define projection angles
ang = tomopy.angles(prj.shape[0])

# data normalization
t = time.time()
prj = tomopy.normalize(prj, flt, drk)
print time.time() - t

# --------------------------------



# Generate a random matrix with dimensions [dim1, dim2, dim3] -----------

dim1 = between 1000 and 10000
dim2 = between 1000 and 10000
dim3 = between 1000 and 10000
Example #38
0
from project import *
from simulator import *
from sinogram import *
from instrument import *
from sample import *

if __name__ == '__main__':

    # create reference recon
    if os.path.exists(os.path.join('data', 'ref_recon.tiff')):
        ref_recon = dxchange.read_tiff(os.path.join('data', 'ref_recon.tiff'))
    else:
        sino = dxchange.read_tiff('data/shepp_sino_pad.tiff')
        sino = -np.log(sino)
        sino = sino[:, np.newaxis, :]
        theta = tomopy.angles(sino.shape[0])
        ref_recon = tomopy.recon(sino,
                                 theta,
                                 center=2048 + 2048,
                                 algorithm='gridrec')
        dxchange.write_tiff(ref_recon, 'data/ref_recon', overwrite=True)
    ref_recon = np.squeeze(ref_recon)

    stage_list = range(256 + 2048, 4096 + 2048, 306)
    inst = Instrument(512)
    inst.add_stage_positions(stage_list)

    stage_list = range(256 + 2048, 4096 + 2048, 306)
    center_list = [(y, x) for y in stage_list for x in stage_list]
    inst.add_center_positions(center_list)
Example #39
0
    step = 100;

    index = dataProj.shape[1] / step+1

    for i in range(0, index):
        sliceStart = i*step;
        sliceEnd = (i+1)*step;
        if(sliceStart >= dataProj.shape[1]):
            sys.exit(0);
        if(sliceEnd > dataProj.shape[1]):
            sliceEnd = dataProj.shape[1];

        proj = dataProj[:,sliceStart:sliceEnd,:]

        start1 = timeit.default_timer()
        theta = tomopy.angles(proj.shape[0], 11, 168)

        stop1 = timeit.default_timer()
        print("end angles", (stop1 - start1))

        start2 = timeit.default_timer()
        # # Flat-field correction of raw data.
        proj = tomopy.normalize(proj, flat, dark)
        stop2 = timeit.default_timer()
        print("end normalize", (stop2 - start2))

        start3 = timeit.default_timer()
        # # Find rotation center.
        rot_center = tomopy.find_center(proj, theta, emission=False, ind=0, init=1024, tol=0.5)
        stop3 = timeit.default_timer()
        print("end find_center", (stop3 - start3))
Example #40
0
def fast_tomo_recon(argv):
    """
    Reconstruct subset slices (sinograms) equally spaced within tomographic
    dataset
    """

    logger = logging.getLogger("fast_tomopy.fast_tomo_recon")

    # Parse arguments passed to function
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input", type=str, help="path to input raw " "dataset", required=True)
    parser.add_argument(
        "-o",
        "--output-file",
        type=str,
        help="full path to h5 output " "file",
        default=os.path.join(os.getcwd(), "fast-tomopy.h5"),
    )
    parser.add_argument("-sn", "--sino-num", type=int, help="Number of slices " "to reconstruct", default=5)
    parser.add_argument(
        "-a",
        "--algorithm",
        type=str,
        help="Reconstruction" " algorithm",
        default="gridrec",
        choices=[
            "art",
            "bart",
            "fbp",
            "gridrec",
            "mlem",
            "ospml_hybrid",
            "ospml_quad",
            "pml_hybrid",
            "pml_quad",
            "sirt",
        ],
    )
    parser.add_argument("-c", "--center", type=float, help="Center of rotation", default=None)
    parser.add_argument(
        "-fn",
        "--filter-name",
        type=str,
        help="Name of filter" " used for reconstruction",
        choices=["none", "shepp", "cosine", "hann", "hamming", "ramlak", "parzen", "butterworth"],
        default="butterworth",
    )
    parser.add_argument(
        "-rr",
        "--ring-remove",
        type=str,
        help="Ring removal " "method",
        choices=["Octopus", "Tomopy-FW", "Tomopy-T"],
        default="Tomopy-T",
    )
    parser.add_argument("-lf", "--log-file", type=str, help="log file name", default="fast-tomopy.log")

    args = parser.parse_args()

    fh = logging.FileHandler(args.log_file)
    fh.setLevel(logging.INFO)
    fh.setFormatter(formatter)
    logger.addHandler(fh)

    if os.path.isdir(os.path.dirname(args.output_file)) is False:
        raise IOError(2, "Directory of output file does not exist", args.output_file)

    # Read file metadata
    logger.info("Reading input file metadata")
    fdata, gdata = read_als_832h5_metadata(args.input)
    proj_total = int(gdata["nangles"])
    last = proj_total - 1
    sino_total = int(gdata["nslices"])
    ray_total = int(gdata["nrays"])
    px_size = float(gdata["pxsize"]) / 10  # cm

    # Set parameters for sinograms to read
    step = sino_total // (args.sino_num + 2)
    start = step
    end = step * (args.sino_num + 1)
    sino = (start, end, step)

    # Read full first and last projection to determine center of rotation
    if args.center is None:
        logger.info("Reading full first and last projection for COR")
        first_last, flats, darks, floc = tomopy.read_als_832h5(args.input, ind_tomo=(0, last))
        first_last = tomopy.normalize(first_last, flats, darks)
        args.center = tomopy.find_center_pc(first_last[0, :, :], first_last[1, :, :], tol=0.1)
        logger.info("Detected center: %f", args.center)

    # Read and normalize raw sinograms
    logger.info("Reading raw data")
    tomo, flats, darks, floc = tomopy.read_als_832h5(args.input, sino=sino)
    logger.info("Normalizing raw data")
    tomo = tomopy.normalize_nf(tomo, flats, darks, floc)

    # Remove stripes from sinograms (remove rings)
    logger.info("Preprocessing normalized data")
    if args.ring_remove == "Tomopy-FW":
        logger.info("Removing stripes from sinograms with %s", args.ring_remove)
        tomo = tomopy.remove_stripe_fw(tomo)
    elif args.ring_remove == "Tomopy-T":
        logger.info("Removing stripes from sinograms with %s", args.ring_remove)
        tomo = tomopy.remove_stripe_ti(tomo)

    # Pad sinograms with edge values
    npad = int(np.ceil(ray_total * np.sqrt(2)) - ray_total) // 2
    tomo = tomopy.pad(tomo, 2, npad=npad, mode="edge")
    args.center += npad  # account for padding

    filter_name = np.array(args.filter_name, dtype=(str, 16))
    theta = tomopy.angles(proj_total, 270, 90)

    logger.info("Reconstructing normalized data")
    # Reconstruct sinograms
    # rec = tomopy.minus_log(tomo, out=tomo)
    rec = tomopy.recon(
        tomo, theta, center=args.center, emission=False, algorithm=args.algorithm, filter_name=filter_name
    )
    rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0)
    rec = rec / px_size

    # Remove rings from reconstruction
    if args.ring_remove == "Octopus":
        logger.info("Removing rings from reconstructions with %s", args.ring_remove)
        thresh = float(gdata["ring_threshold"])
        thresh_max = float(gdata["upp_ring_value"])
        thresh_min = float(gdata["low_ring_value"])
        theta_min = int(gdata["max_arc_length"])
        rwidth = int(gdata["max_ring_size"])
        rec = tomopy.remove_rings(
            rec,
            center_x=args.center,
            thresh=thresh,
            thresh_max=thresh_max,
            thresh_min=thresh_min,
            theta_min=theta_min,
            rwidth=rwidth,
        )

    # Write reconstruction data to new hdf5 file
    fdata["stage"] = "fast-tomopy"
    fdata["stage_flow"] = "/raw/" + fdata["stage"]
    fdata["stage_version"] = "fast-tomopy-0.1"
    # Generate a new uuid based on host ID and current time
    fdata["uuid"] = str(uuid.uuid1())

    gdata["Reconstruction_Type"] = "tomopy-gridrec"
    gdata["ring_removal_method"] = args.ring_remove
    gdata["rfilter"] = args.filter_name

    logger.info("Writing reconstructed data to h5 file")
    write_als_832h5(rec, args.input, fdata, gdata, args.output_file, step)

    return
Example #41
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
TomoPy example script to reconstruct the Anka topo-tomo tomography data as original tiff.
"""

from __future__ import print_function
import tomopy

if __name__ == '__main__':

    obj = tomopy.baboon()
    theta = tomopy.angles(180)
    proj = tomopy.project(obj, theta)
    import pdb; pdb.set_trace()

    # Find rotation center.
    rot_center = tomopy.find_center(proj, theta, emission=False, init=1024, ind=0, tol=0.5)
    print("Center of rotation: ", rot_center)

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

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

    # Write data as stack of TIFs.
    tomopy.write_tiff_stack(rec, fname='recon_dir/recon')
def reconstruct(filename,inputPath="", outputPath="", COR=COR, doOutliers=doOutliers, outlier_diff=outlier_diff, outlier_size=outlier_size, doFWringremoval=doFWringremoval, ringSigma=ringSigma,ringLevel=ringLevel, ringWavelet=ringWavelet,pad_sino=pad_sino,  doPhaseRetrieval=doPhaseRetrieval, propagation_dist=propagation_dist, kev=kev,alphaReg=alphaReg, butterworthpars=butterworthpars, doPolarRing=doPolarRing,Rarc=Rarc, Rmaxwidth=Rmaxwidth, Rtmax=Rtmax, Rthr=Rthr, Rtmin=Rtmin, useAutoCOR=useAutoCOR, use360to180=use360to180, num_substacks=num_substacks,recon_slice=recon_slice):

	# Convert filename to list type if only one file name is given
	if type(filename) != list:
		filename=[filename]

	# If useAutoCor == true, a list of COR will be automatically calculated for all files
	# If a list of COR is given, only entries with boolean False will use automatic COR calculation
	if useAutoCOR==True or (len(COR) != len(filename)):
		logging.info('using auto COR for all input files')
		COR = [False]*len(filename)

	for x in range(len(filename)):
		logging.info('opening data set, checking metadata')

		fdata, gdata = read_als_832h5_metadata(inputPath[x]+filename[x]+'.h5')
		pxsize = float(gdata['pxsize'])/10.0 # convert from metadata (mm) to this script (cm)
		numslices = int(gdata['nslices'])

		# recon_slice == True, only center slice will be reconstructed
		# if integer is given, a specific 		
		if recon_slice != False:
			if (type(recon_slice) == int) and (recon_slice <= numslices):
				sinorange [recon_slice-1, recon_slice]
			else:
				sinorange = [numslices//2-1, numslices//2]
		else:
			sinorange = [0, numslices]

		# Calculate number of substacks (chunks)
		substacks = num_substacks #(sinorange[1]-sinorange[0]-1)//num_sino_per_substack+1

		if (sinorange[1]-sinorange[0]) >= substacks:
			num_sino_per_substack = (sinorange[1]-sinorange[0])//num_substacks
		else:
			num_sino_per_substack = 1

	
		firstcor, lastcor = 0, int(gdata['nangles'])-1
		projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', ind_tomo=(firstcor, lastcor))
		projs = tomopy.normalize_nf(projs, flat, dark, floc)
		autocor = tomopy.find_center_pc(projs[0], projs[1], tol=0.25)


		if (type(COR[x]) == bool) or (COR[x]<0) or (COR[x]=='auto'):
			firstcor, lastcor = 0, int(gdata['nangles'])-1
			projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', ind_tomo=(firstcor, lastcor))
			projs = tomopy.normalize_nf(projs, flat, dark, floc)
			cor = tomopy.find_center_pc(projs[0], projs[1], tol=0.25)
		else:
			cor = COR[x]

		logging.info('Dataset %s, has %d total slices, reconstructing slices %d through %d in %d substack(s), using COR: %f',filename[x], int(gdata['nslices']), sinorange[0], sinorange[1]-1, substacks, cor)
		
		for y in range(0, substacks):
			logging.info('Starting dataset %s (%d of %d), substack %d of %d',filename[x], x+1, len(filename), y+1, substacks)

			logging.info('Reading sinograms...')
			projs, flat, dark, floc = dxchange.read_als_832h5(inputPath[x]+filename[x]+'.h5', sino=(sinorange[0]+y*num_sino_per_substack, sinorange[0]+(y+1)*num_sino_per_substack, 1)) 

			logging.info('Doing remove outliers, norm (nearest flats), and -log...')
			if doOutliers:
				projs = tomopy.remove_outlier(projs, outlier_diff, size=outlier_size, axis=0)
				flat = tomopy.remove_outlier(flat, outlier_diff, size=outlier_size, axis=0)
			tomo = tomopy.normalize_nf(projs, flat, dark, floc)
			tomo = tomopy.minus_log(tomo, out=tomo) # in place logarithm 
		
			# Use padding to remove halo in reconstruction if present
			if pad_sino:
				npad = int(np.ceil(tomo.shape[2] * np.sqrt(2)) - tomo.shape[2])//2
				tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge')
				cor_rec = cor + npad # account for padding
			else:
				cor_rec = cor
		
			if doFWringremoval:
				logging.info('Doing ring (Fourier-wavelet) function...')
				tomo = tomopy.remove_stripe_fw(tomo, sigma=ringSigma, level=ringLevel, pad=True, wname=ringWavelet)		

			if doPhaseRetrieval:
				logging.info('Doing Phase retrieval...')
				#tomo = tomopy.retrieve_phase(tomo, pixel_size=pxsize, dist=propagation_dist, energy=kev, alpha=alphaReg, pad=True)	
				tomo = tomopy.retrieve_phase(tomo, pixel_size=pxsize, dist=propagation_dist, energy=kev, alpha=alphaReg, pad=True)		

			logging.info('Doing recon (gridrec) function and scaling/masking, with cor %f...',cor_rec)
			rec = tomopy.recon(tomo, tomopy.angles(tomo.shape[0], 270, 90), center=cor_rec, algorithm='gridrec', filter_name='butterworth', filter_par=butterworthpars)
			#rec = tomopy.recon(tomo, tomopy.angles(tomo.shape[0], 180+angularrange/2, 180-angularrange/2), center=cor_rec, algorithm='gridrec', filter_name='butterworth', filter_par=butterworthpars)		
			rec /= pxsize  # intensity values in cm^-1
			if pad_sino:
				rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0)
			else:
				rec = tomopy.circ_mask(rec, 0, ratio=1.0, val=0.0)
			
			if doPolarRing:
				logging.info('Doing ring (polar mean filter) function...')
				rec = tomopy.remove_ring(rec, theta_min=Rarc, rwidth=Rmaxwidth, thresh_max=Rtmax, thresh=Rthr, thresh_min=Rtmin)

			logging.info('Writing reconstruction slices to %s', filename[x])
			#dxchange.write_tiff_stack(rec, fname=outputPath+'alpha'+str(alphaReg)+'/rec'+filename[x]+'/rec'+filename[x], start=sinorange[0]+y*num_sino_per_substack)
			dxchange.write_tiff_stack(rec, fname=outputPath + 'recon_'+filename[x]+'/recon_'+filename[x], start=sinorange[0]+y*num_sino_per_substack)
		logging.info('Reconstruction Complete: '+ filename[x])
Example #43
0
    sample_detector_distance = 10      # Propagation distance of the wavefront in cm
    detector_pixel_size_x = 1.2e-4     # Detector pixel size in cm
    monochromator_energy = 61.332    # Energy of incident wave in keV

    # Select the sinogram range to reconstruct.
    start = 600
    end = 900

    # Read the APS 1-ID raw data.
    proj, flat, dark = dxchange.read_aps_1id(fname, sino=(start, end))

    print(proj.shape, flat.shape, dark.shape)
    
    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0], ang1=0.0, ang2=360.0)

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

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

    # Find rotation center.
    #rot_center = tomopy.find_center(ndata, theta, init=1024, ind=0, tol=0.5)
    rot_center = 576

    binning = 0
Example #44
0
#!/usr/bin/env python

import tomopy
import numpy
import time

print "Hey!"

obj = numpy.ones((512, 512, 512), dtype='float32')
ang = tomopy.angles(512)

t = time.time()
prj = tomopy.project(obj, ang)
print time.time() - t

t = time.time()
rec = tomopy.recon(prj, ang, algorithm='gridrec', num_gridx=1024, num_gridy=1024)
print time.time() - t

tomopy.write_tiff_stack(rec, overwrite=True)
Example #45
0
dark_start = 0
dark_end = 100

ind_tomo = range(proj_start, proj_end)
ind_flat = range(flat_start, flat_end)
ind_dark = range(dark_start, dark_end)

# Select the sinogram range to reconstruct.
start = 800
end = 804

# Read the APS 1-ID raw data.
proj, flat, dark = tomopy.io.exchange.read_anka_topotomo(fname, ind_tomo, ind_flat, ind_dark, sino=(start, end))

# Set data collection angles as equally spaced between 0-180 degrees.
theta  = tomopy.angles(proj.shape[0], 0, 180)
print proj.shape
print flat.shape
print dark.shape

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

# Set rotation axis location manually.
best_center = 993.825; 
rot_center = best_center

# Find rotation center.
#rot_center = tomopy.find_center(proj, theta, emission=False, init=best_center, ind=0, tol=0.3)
print "Center of rotation:", rot_center
Example #46
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
TomoPy example script to reconstruct the Anka topo-tomo tomography data as original tiff.
"""

from __future__ import print_function
import tomopy

if __name__ == '__main__':

    obj = tomopy.baboon()
    theta = tomopy.angles(180)
    proj = tomopy.project(obj, theta)
    import pdb
    pdb.set_trace()

    # Find rotation center.
    rot_center = tomopy.find_center(proj,
                                    theta,
                                    emission=False,
                                    init=1024,
                                    ind=0,
                                    tol=0.5)
    print("Center of rotation: ", rot_center)

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(proj,
                       theta,
                       center=rot_center,
                       algorithm='gridrec',
Example #47
0
import tomopy

if __name__ == '__main__':

    # Set path to the micro-CT data to reconstruct.
    fname = 'data_dir/sample_name_prefix'

    # Select the sinogram range to reconstruct.
    start = 0
    end = 16

    # Read the APS 1-ID raw data.
    proj, flat, dark = tomopy.read_sls_tomcat(fname, sino=(start, end))

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0], 0, 180)

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

    # Find rotation center.
    rot_center = tomopy.find_center(proj,
                                    theta,
                                    emission=False,
                                    init=1024,
                                    ind=0,
                                    tol=0.5)
    print("Center of rotation:", rot_center)

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(proj,
Example #48
0
#### for 1 slice reconstruction:
#-------------------------------
if 0:
    slice_first = 1500
    # Read HDF5 file.
    #data, white, dark, theta = tomopy.xtomo_reader(file_name,
    #                                               exchange_rank = ExchangeRank,
    #                                               slices_start=slice_first,
    #                                               slices_end=slice_first+1)
    exchange_rank = ExchangeRank
    data, white, dark = tomopy.io.exchange.read_aps_32id(file_name, 
                                                        exchange_rank, 
                                                        sino=(slice_first, slice_first+4))

    theta  = tomopy.angles(data.shape[0])
    
    # Xtomo object creation and pipeline of methods.
    ##d = tomopy.xtomo_dataset(log='debug')
    ##d.dataset(data, white, dark, theta)

    #if perform_norm: d.normalize() # flat & dark field correction
    if perform_norm: data = tomopy.normalize(data, white, dark)

    ##if drift_correct: d.correct_drift()
    if drift_correct: data = tomopy.normalize_bg(data)

    #d.median_filter(size=medfilt_size, axis=0) # Apply a median filter in the projection plane
    data = tomopy.median_filter(data, size=medfilt_size, axis=0)

    #if remove_stripe1: d.stripe_removal(level=stripe_lvl, sigma=sig, wname=Wname)
Example #49
0
import tomopy
from PIL import Image
import numpy as np


def load_image(infilename):
    pil_imgray = Image.open(infilename).convert('LA')
    img = np.array(list(pil_imgray.getdata(band=0)), float)
    img.shape = (pil_imgray.size[1], pil_imgray.size[0])
    return img


obj = tomopy.misc.phantom.baboon(size=256, dtype=u'float32')
ang = tomopy.angles(25)

prj = tomopy.project(obj, ang)
rec = tomopy.recon(prj,
                   ang,
                   algorithm='gridrec',
                   num_gridx=1024,
                   num_gridy=1024)
Example #50
0
def fast_tomo_recon(argv):
    """
    Reconstruct subset slices (sinograms) equally spaced within tomographic
    dataset
    """

    logger = logging.getLogger('fast_tomopy.fast_tomo_recon')

    # Parse arguments passed to function
    parser = argparse.ArgumentParser()
    parser.add_argument('-i',
                        '--input',
                        type=str,
                        help='path to input raw '
                        'dataset',
                        required=True)
    parser.add_argument('-o',
                        '--output-file',
                        type=str,
                        help='full path to h5 output '
                        'file',
                        default=os.path.join(os.getcwd(), "fast-tomopy.h5"))
    parser.add_argument('-sn',
                        '--sino-num',
                        type=int,
                        help='Number of slices '
                        'to reconstruct',
                        default=5)
    parser.add_argument('-a',
                        '--algorithm',
                        type=str,
                        help='Reconstruction'
                        ' algorithm',
                        default='gridrec',
                        choices=[
                            'art', 'bart', 'fbp', 'gridrec', 'mlem',
                            'ospml_hybrid', 'ospml_quad', 'pml_hybrid',
                            'pml_quad', 'sirt'
                        ])
    parser.add_argument('-c',
                        '--center',
                        type=float,
                        help='Center of rotation',
                        default=None)
    parser.add_argument('-fn',
                        '--filter-name',
                        type=str,
                        help='Name of filter'
                        ' used for reconstruction',
                        choices=[
                            'none', 'shepp', 'cosine', 'hann', 'hamming',
                            'ramlak', 'parzen', 'butterworth'
                        ],
                        default='butterworth')
    parser.add_argument('-rr',
                        '--ring-remove',
                        type=str,
                        help='Ring removal '
                        'method',
                        choices=['Octopus', 'Tomopy-FW', 'Tomopy-T'],
                        default='Tomopy-T')
    parser.add_argument('-lf',
                        '--log-file',
                        type=str,
                        help='log file name',
                        default='fast-tomopy.log')

    args = parser.parse_args()

    fh = logging.FileHandler(args.log_file)
    fh.setLevel(logging.INFO)
    fh.setFormatter(formatter)
    logger.addHandler(fh)

    if os.path.isdir(os.path.dirname(args.output_file)) is False:
        raise IOError(2, 'Directory of output file does not exist',
                      args.output_file)

    # Read file metadata
    logger.info('Reading input file metadata')
    fdata, gdata = read_als_832h5_metadata(args.input)
    proj_total = int(gdata['nangles'])
    last = proj_total - 1
    sino_total = int(gdata['nslices'])
    ray_total = int(gdata['nrays'])
    px_size = float(gdata['pxsize']) / 10  # cm

    # Set parameters for sinograms to read
    step = sino_total // (args.sino_num + 2)
    start = step
    end = step * (args.sino_num + 1)
    sino = (start, end, step)

    # Read full first and last projection to determine center of rotation
    if args.center is None:
        logger.info('Reading full first and last projection for COR')
        first_last, flats, darks, foobar = dx.read_als_832h5(args.input,
                                                             ind_tomo=(0,
                                                                       last))
        first_last = tomopy.normalize(first_last, flats, darks)
        args.center = tomopy.find_center_pc(first_last[0, :, :],
                                            first_last[1, :, :],
                                            tol=0.1)
        logger.info('Detected center: %f', args.center)

    # Read and normalize raw sinograms
    logger.info('Reading raw data')
    tomo, flats, darks, foobar = dx.read_als_832h5(args.input, sino=sino)
    logger.info('Normalizing raw data')
    tomo = tomopy.normalize(tomo, flats, darks)
    tomo = tomopy.minus_log(tomo)

    # Remove stripes from sinograms (remove rings)
    logger.info('Preprocessing normalized data')
    if args.ring_remove == 'Tomopy-FW':
        logger.info('Removing stripes from sinograms with %s',
                    args.ring_remove)
        tomo = tomopy.remove_stripe_fw(tomo)
    elif args.ring_remove == 'Tomopy-T':
        logger.info('Removing stripes from sinograms with %s',
                    args.ring_remove)
        tomo = tomopy.remove_stripe_ti(tomo)

    # Pad sinograms with edge values
    npad = int(np.ceil(ray_total * np.sqrt(2)) - ray_total) // 2
    tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge')
    args.center += npad  # account for padding

    filter_name = np.array(args.filter_name, dtype=(str, 16))
    theta = tomopy.angles(proj_total, 270, 90)

    logger.info('Reconstructing normalized data')
    # Reconstruct sinograms
    # rec = tomopy.minus_log(tomo, out=tomo)
    rec = tomopy.recon(tomo,
                       theta,
                       center=args.center,
                       algorithm=args.algorithm,
                       filter_name=filter_name)
    rec = tomopy.circ_mask(rec[:, npad:-npad, npad:-npad], 0)
    rec = rec / px_size

    # Remove rings from reconstruction
    if args.ring_remove == 'Octopus':
        logger.info('Removing rings from reconstructions with %s',
                    args.ring_remove)
        thresh = float(gdata['ring_threshold'])
        thresh_max = float(gdata['upp_ring_value'])
        thresh_min = float(gdata['low_ring_value'])
        theta_min = int(gdata['max_arc_length'])
        rwidth = int(gdata['max_ring_size'])
        rec = tomopy.remove_rings(rec,
                                  center_x=args.center,
                                  thresh=thresh,
                                  thresh_max=thresh_max,
                                  thresh_min=thresh_min,
                                  theta_min=theta_min,
                                  rwidth=rwidth)

    # Write reconstruction data to new hdf5 file
    fdata['stage'] = 'fast-tomopy'
    fdata['stage_flow'] = '/raw/' + fdata['stage']
    fdata['stage_version'] = 'fast-tomopy-0.1'
    # Generate a new uuid based on host ID and current time
    fdata['uuid'] = str(uuid.uuid1())

    gdata['Reconstruction_Type'] = 'tomopy-gridrec'
    gdata['ring_removal_method'] = args.ring_remove
    gdata['rfilter'] = args.filter_name

    logger.info('Writing reconstructed data to h5 file')
    write_als_832h5(rec, args.input, fdata, gdata, args.output_file, step)

    return
Example #51
0
def recon(
        filename,
        inputPath='./',
        outputPath=None,
        outputFilename=None,
        doOutliers1D=False,  # outlier removal in 1d (along sinogram columns)
        outlier_diff1D=750,  # difference between good data and outlier data (outlier removal)
        outlier_size1D=3,  # radius around each pixel to look for outliers (outlier removal)
        doOutliers2D=False,  # outlier removal, standard 2d on each projection
        outlier_diff2D=750,  # difference between good data and outlier data (outlier removal)
        outlier_size2D=3,  # radius around each pixel to look for outliers (outlier removal)
        doFWringremoval=True,  # Fourier-wavelet ring removal
        doTIringremoval=False,  # Titarenko ring removal
        doSFringremoval=False,  # Smoothing filter ring removal
        ringSigma=3,  # damping parameter in Fourier space (Fourier-wavelet ring removal)
        ringLevel=8,  # number of wavelet transform levels (Fourier-wavelet ring removal)
        ringWavelet='db5',  # type of wavelet filter (Fourier-wavelet ring removal)
        ringNBlock=0,  # used in Titarenko ring removal (doTIringremoval)
        ringAlpha=1.5,  # used in Titarenko ring removal (doTIringremoval)
        ringSize=5,  # used in smoothing filter ring removal (doSFringremoval)
        doPhaseRetrieval=False,  # phase retrieval
        alphaReg=0.0002,  # smaller = smoother (used for phase retrieval)
        propagation_dist=75,  # sample-to-scintillator distance (phase retrieval)
        kev=24,  # energy level (phase retrieval)
        butterworth_cutoff=0.25,  #0.1 would be very smooth, 0.4 would be very grainy (reconstruction)
        butterworth_order=2,  # for reconstruction
        doPolarRing=False,  # ring removal
        Rarc=30,  # min angle needed to be considered ring artifact (ring removal)
        Rmaxwidth=100,  # max width of rings to be filtered (ring removal)
        Rtmax=3000.0,  # max portion of image to filter (ring removal)
        Rthr=3000.0,  # max value of offset due to ring artifact (ring removal)
        Rtmin=-3000.0,  # min value of image to filter (ring removal)
        cor=None,  # center of rotation (float). If not used then cor will be detected automatically
        corFunction='pc',  # center of rotation function to use - can be 'pc', 'vo', or 'nm'
        voInd=None,  # index of slice to use for cor search (vo)
        voSMin=-40,  # min radius for searching in sinogram (vo)
        voSMax=40,  # max radius for searching in sinogram (vo)
        voSRad=10,  # search radius (vo)
        voStep=0.5,  # search step (vo)
        voRatio=2.0,  # ratio of field-of-view and object size (vo)
        voDrop=20,  # drop lines around vertical center of mask (vo)
        nmInd=None,  # index of slice to use for cor search (nm)
        nmInit=None,  # initial guess for center (nm)
        nmTol=0.5,  # desired sub-pixel accuracy (nm)
        nmMask=True,  # if True, limits analysis to circular region (nm)
        nmRatio=1.0,  # ratio of radius of circular mask to edge of reconstructed image (nm)
        nmSinoOrder=False,  # if True, analyzes in sinogram space. If False, analyzes in radiograph space
        use360to180=False,  # use 360 to 180 conversion
        doBilateralFilter=False,  # if True, uses bilateral filter on image just before write step # NOTE: image will be converted to 8bit if it is not already
        bilateral_srad=3,  # spatial radius for bilateral filter (image will be converted to 8bit if not already)
        bilateral_rrad=30,  # range radius for bilateral filter (image will be converted to 8bit if not already)
        castTo8bit=False,  # convert data to 8bit before writing
        cast8bit_min=-10,  # min value if converting to 8bit
        cast8bit_max=30,  # max value if converting to 8bit
        useNormalize_nf=False,  # normalize based on background intensity (nf)
        chunk_proj=100,  # chunk size in projection direction
        chunk_sino=100,  # chunk size in sinogram direction
        npad=None,  # amount to pad data before reconstruction
        projused=None,  #should be slicing in projection dimension (start,end,step)
        sinoused=None,  #should be sliceing in sinogram dimension (start,end,step). If first value is negative, it takes the number of slices from the second value in the middle of the stack.
        correcttilt=0,  #tilt dataset
        tiltcenter_slice=None,  # tilt center (x direction)
        tiltcenter_det=None,  # tilt center (y direction)
        angle_offset=0,  #this is the angle offset from our default (270) so that tomopy yields output in the same orientation as previous software (Octopus)
        anglelist=None,  #if not set, will assume evenly spaced angles which will be calculated by the angular range and number of angles found in the file. if set to -1, will read individual angles from each image. alternatively, a list of angles can be passed.
        doBeamHardening=False,  #turn on beam hardening correction, based on "Correction for beam hardening in computed tomography", Gabor Herman, 1979 Phys. Med. Biol. 24 81
        BeamHardeningCoefficients=None,  #6 values, tomo = a0 + a1*tomo + a2*tomo^2 + a3*tomo^3 + a4*tomo^4 + a5*tomo^5
        projIgnoreList=None,  #projections to be ignored in the reconstruction (for simplicity in the code, they will not be removed and will be processed as all other projections but will be set to zero absorption right before reconstruction.
        *args,
        **kwargs):

    start_time = time.time()
    print("Start {} at:".format(filename) +
          time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()))

    outputPath = inputPath if outputPath is None else outputPath

    outputFilename = filename if outputFilename is None else outputFilename
    tempfilenames = [outputPath + 'tmp0.h5', outputPath + 'tmp1.h5']
    filenametowrite = outputPath + '/rec' + filename.strip(
        ".h5") + '/' + outputFilename
    #filenametowrite = outputPath+'/rec'+filename+'/'+outputFilename

    print("cleaning up previous temp files", end="")
    for tmpfile in tempfilenames:
        try:
            os.remove(tmpfile)
        except OSError:
            pass

    print(", reading metadata")

    datafile = h5py.File(inputPath + filename, 'r')
    gdata = dict(dxchange.reader._find_dataset_group(datafile).attrs)
    pxsize = float(gdata['pxsize']) / 10  # /10 to convert unites from mm to cm
    numslices = int(gdata['nslices'])
    numangles = int(gdata['nangles'])
    angularrange = float(gdata['arange'])
    numrays = int(gdata['nrays'])
    npad = int(np.ceil(numrays * np.sqrt(2)) -
               numrays) // 2 if npad is None else npad
    projused = (0, numangles - 1, 1) if projused is None else projused

    #	ndark = int(gdata['num_dark_fields'])
    #	ind_dark = list(range(0, ndark))
    #	group_dark = [numangles - 1]
    inter_bright = int(gdata['i0cycle'])
    nflat = int(gdata['num_bright_field'])
    ind_flat = list(range(0, nflat))
    if inter_bright > 0:
        group_flat = list(range(0, numangles, inter_bright))
        if group_flat[-1] != numangles - 1:
            group_flat.append(numangles - 1)
    elif inter_bright == 0:
        group_flat = [0, numangles - 1]
    else:
        group_flat = None
    ind_tomo = list(range(0, numangles))
    floc_independent = dxchange.reader._map_loc(ind_tomo, group_flat)

    #figure out the angle list (a list of angles, one per projection image)
    dtemp = datafile[list(datafile.keys())[0]]
    fltemp = list(dtemp.keys())
    firstangle = float(dtemp[fltemp[0]].attrs.get('rot_angle', 0))
    if anglelist is None:
        #the offset angle should offset from the angle of the first image, which is usually 0, but in the case of timbir data may not be.
        #we add the 270 to be inte same orientation as previous software used at bl832
        angle_offset = 270 + angle_offset - firstangle
        anglelist = tomopy.angles(numangles, angle_offset,
                                  angle_offset - angularrange)
    elif anglelist == -1:
        anglelist = np.zeros(shape=numangles)
        for icount in range(0, numangles):
            anglelist[icount] = np.pi / 180 * (270 + angle_offset - float(
                dtemp[fltemp[icount]].attrs['rot_angle']))

    #if projused is different than default, need to chnage numangles and angularrange

    #can't do useNormalize_nf and doOutliers2D at the same time, or doOutliers2D and doOutliers1D at the same time, b/c of the way we chunk, for now just disable that
    if useNormalize_nf == True and doOutliers2D == True:
        useNormalize_nf = False
        print(
            "we cannot currently do useNormalize_nf and doOutliers2D at the same time, turning off useNormalize_nf"
        )
    if doOutliers2D == True and doOutliers1D == True:
        doOutliers1D = False
        print(
            "we cannot currently do doOutliers1D and doOutliers2D at the same time, turning off doOutliers1D"
        )

    #figure out how user can pass to do central x number of slices, or set of slices dispersed throughout (without knowing a priori the value of numslices)
    if sinoused is None:
        sinoused = (0, numslices, 1)
    elif sinoused[0] < 0:
        sinoused = (
            int(np.floor(numslices / 2.0) - np.ceil(sinoused[1] / 2.0)),
            int(np.floor(numslices / 2.0) + np.floor(sinoused[1] / 2.0)), 1)

    num_proj_per_chunk = np.minimum(chunk_proj, projused[1] - projused[0])
    numprojchunks = (projused[1] - projused[0] - 1) // num_proj_per_chunk + 1
    num_sino_per_chunk = np.minimum(chunk_sino, sinoused[1] - sinoused[0])
    numsinochunks = (sinoused[1] - sinoused[0] - 1) // num_sino_per_chunk + 1
    numprojused = (projused[1] - projused[0]) // projused[2]
    numsinoused = (sinoused[1] - sinoused[0]) // sinoused[2]

    BeamHardeningCoefficients = (
        0, 1, 0, 0, 0,
        .1) if BeamHardeningCoefficients is None else BeamHardeningCoefficients

    if cor is None:
        print("Detecting center of rotation", end="")
        if angularrange > 300:
            lastcor = int(np.floor(numangles / 2) - 1)
        else:
            lastcor = numangles - 1
        #I don't want to see the warnings about the reader using a deprecated variable in dxchange
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            tomo, flat, dark, floc = dxchange.read_als_832h5(
                inputPath + filename, ind_tomo=(0, lastcor))
        tomo = tomo.astype(np.float32)
        if useNormalize_nf:
            tomopy.normalize_nf(tomo, flat, dark, floc, out=tomo)
        else:
            tomopy.normalize(tomo, flat, dark, out=tomo)

        if corFunction == 'vo':
            # same reason for catching warnings as above
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                cor = tomopy.find_center_vo(tomo,
                                            ind=voInd,
                                            smin=voSMin,
                                            smax=voSMax,
                                            srad=voSRad,
                                            step=voStep,
                                            ratio=voRatio,
                                            drop=voDrop)
        elif corFunction == 'nm':
            cor = tomopy.find_center(
                tomo,
                tomopy.angles(numangles, angle_offset,
                              angle_offset - angularrange),
                ind=nmInd,
                init=nmInit,
                tol=nmTol,
                mask=nmMask,
                ratio=nmRatio,
                sinogram_order=nmSinoOrder)
        elif corFunction == 'pc':
            cor = tomopy.find_center_pc(tomo[0], tomo[1], tol=0.25)
        else:
            raise ValueError("\'corFunction\' must be one of: [ pc, vo, nm ].")
        print(", {}".format(cor))
    else:
        print("using user input center of {}".format(cor))

    function_list = []

    if doOutliers1D:
        function_list.append('remove_outlier1d')
    if doOutliers2D:
        function_list.append('remove_outlier2d')
    if useNormalize_nf:
        function_list.append('normalize_nf')
    else:
        function_list.append('normalize')
    function_list.append('minus_log')
    if doBeamHardening:
        function_list.append('beam_hardening')
    if doFWringremoval:
        function_list.append('remove_stripe_fw')
    if doTIringremoval:
        function_list.append('remove_stripe_ti')
    if doSFringremoval:
        function_list.append('remove_stripe_sf')
    if correcttilt:
        function_list.append('correcttilt')
    if use360to180:
        function_list.append('do_360_to_180')
    if doPhaseRetrieval:
        function_list.append('phase_retrieval')
    function_list.append('recon_mask')
    if doPolarRing:
        function_list.append('polar_ring')
    if castTo8bit:
        function_list.append('castTo8bit')
    if doBilateralFilter:
        function_list.append('bilateral_filter')
    function_list.append('write_output')

    # Figure out first direction to slice
    for func in function_list:
        if slice_dir[func] != 'both':
            axis = slice_dir[func]
            break

    done = False
    curfunc = 0
    curtemp = 0
    while True:  # Loop over reading data in certain chunking direction
        if axis == 'proj':
            niter = numprojchunks
        else:
            niter = numsinochunks
        for y in range(niter):  # Loop over chunks
            print("{} chunk {} of {}".format(axis, y + 1, niter))
            if curfunc == 0:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    if axis == 'proj':
                        tomo, flat, dark, floc = dxchange.read_als_832h5(
                            inputPath + filename,
                            ind_tomo=range(
                                y * num_proj_per_chunk + projused[0],
                                np.minimum(
                                    (y + 1) * num_proj_per_chunk + projused[0],
                                    numangles)),
                            sino=(sinoused[0], sinoused[1], sinoused[2]))
                    else:
                        tomo, flat, dark, floc = dxchange.read_als_832h5(
                            inputPath + filename,
                            ind_tomo=range(projused[0], projused[1],
                                           projused[2]),
                            sino=(y * num_sino_per_chunk + sinoused[0],
                                  np.minimum((y + 1) * num_sino_per_chunk +
                                             sinoused[0], numslices), 1))
            else:
                if axis == 'proj':
                    start, end = y * num_proj_per_chunk, np.minimum(
                        (y + 1) * num_proj_per_chunk, numprojused)
                    tomo = dxchange.reader.read_hdf5(
                        tempfilenames[curtemp],
                        '/tmp/tmp',
                        slc=((start, end, 1), (0, numslices, 1),
                             (0, numrays, 1)))  #read in intermediate file
                else:
                    start, end = y * num_sino_per_chunk, np.minimum(
                        (y + 1) * num_sino_per_chunk, numsinoused)
                    tomo = dxchange.reader.read_hdf5(tempfilenames[curtemp],
                                                     '/tmp/tmp',
                                                     slc=((0, numangles,
                                                           1), (start, end, 1),
                                                          (0, numrays, 1)))
            dofunc = curfunc
            keepvalues = None
            while True:  # Loop over operations to do in current chunking direction
                func_name = function_list[dofunc]
                newaxis = slice_dir[func_name]
                if newaxis != 'both' and newaxis != axis:
                    # We have to switch axis, so flush to disk
                    if y == 0:
                        try:
                            os.remove(tempfilenames[1 - curtemp])
                        except OSError:
                            pass
                    appendaxis = 1 if axis == 'sino' else 0
                    dxchange.writer.write_hdf5(
                        tomo,
                        fname=tempfilenames[1 - curtemp],
                        gname='tmp',
                        dname='tmp',
                        overwrite=False,
                        appendaxis=appendaxis)  #writing intermediate file...
                    break
                print(func_name, end=" ")
                curtime = time.time()
                if func_name == 'remove_outlier1d':
                    tomo = tomo.astype(np.float32, copy=False)
                    remove_outlier1d(tomo,
                                     outlier_diff1D,
                                     size=outlier_size1D,
                                     out=tomo)
                if func_name == 'remove_outlier2d':
                    tomo = tomo.astype(np.float32, copy=False)
                    tomopy.remove_outlier(tomo,
                                          outlier_diff2D,
                                          size=outlier_size2D,
                                          axis=0,
                                          out=tomo)
                elif func_name == 'normalize_nf':
                    tomo = tomo.astype(np.float32, copy=False)
                    tomopy.normalize_nf(
                        tomo, flat, dark, floc_independent, out=tomo
                    )  #use floc_independent b/c when you read file in proj chunks, you don't get the correct floc returned right now to use here.
                elif func_name == 'normalize':
                    tomo = tomo.astype(np.float32, copy=False)
                    tomopy.normalize(tomo, flat, dark, out=tomo)
                elif func_name == 'minus_log':
                    mx = np.float32(0.00000000000000000001)
                    ne.evaluate('where(tomo>mx, tomo, mx)', out=tomo)
                    tomopy.minus_log(tomo, out=tomo)
                elif func_name == 'beam_hardening':
                    loc_dict = {
                        'a{}'.format(i): np.float32(val)
                        for i, val in enumerate(BeamHardeningCoefficients)
                    }
                    tomo = ne.evaluate(
                        'a0 + a1*tomo + a2*tomo**2 + a3*tomo**3 + a4*tomo**4 + a5*tomo**5',
                        local_dict=loc_dict,
                        out=tomo)
                elif func_name == 'remove_stripe_fw':
                    tomo = tomopy.remove_stripe_fw(tomo,
                                                   sigma=ringSigma,
                                                   level=ringLevel,
                                                   pad=True,
                                                   wname=ringWavelet)
                elif func_name == 'remove_stripe_ti':
                    tomo = tomopy.remove_stripe_ti(tomo,
                                                   nblock=ringNBlock,
                                                   alpha=ringAlpha)
                elif func_name == 'remove_stripe_sf':
                    tomo = tomopy.remove_stripe_sf(tomo, size=ringSize)
                elif func_name == 'correcttilt':
                    if tiltcenter_slice is None:
                        tiltcenter_slice = numslices / 2.
                    if tiltcenter_det is None:
                        tiltcenter_det = tomo.shape[2] / 2
                    new_center = tiltcenter_slice - 0.5 - sinoused[0]
                    center_det = tiltcenter_det - 0.5

                    #add padding of 10 pixels, to be unpadded right after tilt correction. This makes the tilted image not have zeros at certain edges, which matters in cases where sample is bigger than the field of view. For the small amounts we are generally tilting the images, 10 pixels is sufficient.
                    #					tomo = tomopy.pad(tomo, 2, npad=10, mode='edge')
                    #					center_det = center_det + 10

                    cntr = (center_det, new_center)
                    for b in range(tomo.shape[0]):
                        tomo[b] = st.rotate(
                            tomo[b],
                            correcttilt,
                            center=cntr,
                            preserve_range=True,
                            order=1,
                            mode='edge',
                            clip=True
                        )  #center=None means image is rotated around its center; order=1 is default, order of spline interpolation


#					tomo = tomo[:, :, 10:-10]

                elif func_name == 'do_360_to_180':

                    # Keep values around for processing the next chunk in the list
                    keepvalues = [
                        angularrange, numangles, projused, num_proj_per_chunk,
                        numprojchunks, numprojused, numrays, anglelist
                    ]

                    #why -.5 on one and not on the other?
                    if tomo.shape[0] % 2 > 0:
                        tomo = sino_360_to_180(
                            tomo[0:-1, :, :],
                            overlap=int(
                                np.round((tomo.shape[2] - cor - .5)) * 2),
                            rotation='right')
                        angularrange = angularrange / 2 - angularrange / (
                            tomo.shape[0] - 1)
                    else:
                        tomo = sino_360_to_180(
                            tomo[:, :, :],
                            overlap=int(np.round((tomo.shape[2] - cor)) * 2),
                            rotation='right')
                        angularrange = angularrange / 2
                    numangles = int(numangles / 2)
                    projused = (0, numangles - 1, 1)
                    num_proj_per_chunk = np.minimum(chunk_proj,
                                                    projused[1] - projused[0])
                    numprojchunks = (projused[1] - projused[0] -
                                     1) // num_proj_per_chunk + 1
                    numprojused = (projused[1] - projused[0]) // projused[2]
                    numrays = tomo.shape[2]

                    anglelist = anglelist[:numangles]

                elif func_name == 'phase_retrieval':
                    tomo = tomopy.retrieve_phase(tomo,
                                                 pixel_size=pxsize,
                                                 dist=propagation_dist,
                                                 energy=kev,
                                                 alpha=alphaReg,
                                                 pad=True)
                elif func_name == 'recon_mask':
                    tomo = tomopy.pad(tomo, 2, npad=npad, mode='edge')

                    if projIgnoreList is not None:
                        for badproj in projIgnoreList:
                            tomo[badproj] = 0

                    rec = tomopy.recon(
                        tomo,
                        anglelist,
                        center=cor + npad,
                        algorithm='gridrec',
                        filter_name='butterworth',
                        filter_par=[butterworth_cutoff, butterworth_order])
                    rec = rec[:, npad:-npad, npad:-npad]
                    rec /= pxsize  # convert reconstructed voxel values from 1/pixel to 1/cm
                    rec = tomopy.circ_mask(rec, 0)
                elif func_name == 'polar_ring':
                    rec = np.ascontiguousarray(rec, dtype=np.float32)
                    rec = tomopy.remove_ring(rec,
                                             theta_min=Rarc,
                                             rwidth=Rmaxwidth,
                                             thresh_max=Rtmax,
                                             thresh=Rthr,
                                             thresh_min=Rtmin,
                                             out=rec)
                elif func_name == 'castTo8bit':
                    rec = convert8bit(rec, cast8bit_min, cast8bit_max)
                elif func_name == 'bilateral_filter':
                    rec = pyF3D.run_BilateralFilter(
                        rec,
                        spatialRadius=bilateral_srad,
                        rangeRadius=bilateral_rrad)
                elif func_name == 'write_output':
                    dxchange.write_tiff_stack(rec,
                                              fname=filenametowrite,
                                              start=y * num_sino_per_chunk +
                                              sinoused[0])
                print('(took {:.2f} seconds)'.format(time.time() - curtime))
                dofunc += 1
                if dofunc == len(function_list):
                    break
            if y < niter - 1 and keepvalues:  # Reset original values for next chunk
                angularrange, numangles, projused, num_proj_per_chunk, numprojchunks, numprojused, numrays, anglelist = keepvalues

        curtemp = 1 - curtemp
        curfunc = dofunc
        if curfunc == len(function_list):
            break
        axis = slice_dir[function_list[curfunc]]
    print("cleaning up temp files")
    for tmpfile in tempfilenames:
        try:
            os.remove(tmpfile)
        except OSError:
            pass
    print("End Time: " +
          time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()))
    print('It took {:.3f} s to process {}'.format(time.time() - start_time,
                                                  inputPath + filename))
Example #52
0
def recon_hdf5(src_fanme, dest_folder, sino_range, sino_step, shift_grid, center_vec=None, center_eq=None, dtype='float32',
               algorithm='gridrec', tolerance=1, chunk_size=20, save_sino=False, sino_blur=None, flattened_radius=120,
               mode='180', test_mode=False, phase_retrieval=None, ring_removal=True, **kwargs):
    """
    center_eq: a and b parameters in fitted center position equation center = a*slice + b.
    """

    if not os.path.exists(dest_folder):
        try:
            os.mkdir(dest_folder)
        except:
            pass
    sino_ini = int(sino_range[0])
    sino_end = int(sino_range[1])
    sino_ls_all = np.arange(sino_ini, sino_end, sino_step, dtype='int')
    alloc_set = allocate_mpi_subsets(sino_ls_all.size, size, task_list=sino_ls_all)
    sino_ls = alloc_set[rank]

    # prepare metadata
    f = h5py.File(src_fanme)
    dset = f['exchange/data']
    full_shape = dset.shape
    theta = tomopy.angles(full_shape[0])
    if center_eq is not None:
        a, b = center_eq
        center_ls = sino_ls * a + b
        center_ls = np.round(center_ls)
        for iblock in range(int(sino_ls.size/chunk_size)+1):
            print('Beginning block {:d}.'.format(iblock))
            t0 = time.time()
            istart = iblock*chunk_size
            iend = np.min([(iblock+1)*chunk_size, sino_ls.size])
            fstart = sino_ls[istart]
            fend = sino_ls[iend]
            center = center_ls[istart:iend]
            data = dset[:, fstart:fend:sino_step, :]
            data[np.isnan(data)] = 0
            data = data.astype('float32')
            data = tomopy.remove_stripe_ti(data, alpha=4)
            if sino_blur is not None:
                for i in range(data.shape[1]):
                    data[:, i, :] = gaussian_filter(data[:, i, :], sino_blur)
            rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs)
            rec = tomopy.remove_ring(rec)
            rec = tomopy.remove_outlier(rec, tolerance)
            rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)
            for i in range(rec.shape[0]):
                slice = fstart + i*sino_step
                dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}_{:05d}.tiff').format(slice, sino_ini))
                if save_sino:
                    dxchange.write_tiff(data[:, i, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, int(center[i])))
            iblock += 1
            print('Block {:d} finished in {:.2f} s.'.format(iblock, time.time()-t0))
    else:
        # divide chunks
        grid_bins = np.append(np.ceil(shift_grid[:, 0, 0]), full_shape[1])
        chunks = []
        center_ls = []
        istart = 0
        counter = 0
        # irow should be 0 for slice 0
        irow = np.searchsorted(grid_bins, sino_ls[0], side='right')-1

        for i in range(sino_ls.size):
            counter += 1
            sino_next = i+1 if i != sino_ls.size-1 else i
            if counter >= chunk_size or sino_ls[sino_next] >= grid_bins[irow+1] or sino_next == i:
                iend = i+1
                chunks.append((istart, iend))
                istart = iend
                center_ls.append(center_vec[irow])
                if sino_ls[sino_next] >= grid_bins[irow+1]:
                    irow += 1
                counter = 0

        # reconstruct chunks
        iblock = 1
        for (istart, iend), center in izip(chunks, center_ls):
            print('Beginning block {:d}.'.format(iblock))
            t0 = time.time()
            fstart = sino_ls[istart]
            fend = sino_ls[iend-1]
            print('Reading data...')
            data = dset[:, fstart:fend+1:sino_step, :]
            if mode == '360':
                overlap = 2 * (dset.shape[2] - center)
                data = tomosaic.morph.sino_360_to_180(data, overlap=overlap, rotation='right')
                theta = tomopy.angles(data.shape[0])
            data[np.isnan(data)] = 0
            data = data.astype('float32')
            if sino_blur is not None:
                for i in range(data.shape[1]):
                    data[:, i, :] = gaussian_filter(data[:, i, :], sino_blur)
            if ring_removal:
                data = tomopy.remove_stripe_ti(data, alpha=4)
                if phase_retrieval:
                    data = tomopy.retrieve_phase(data, kwargs['pixel_size'], kwargs['dist'], kwargs['energy'],
                                                 kwargs['alpha'])
                rec0 = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs)
                rec = tomopy.remove_ring(np.copy(rec0))
                cent = int((rec.shape[1]-1) / 2)
                xx, yy = np.meshgrid(np.arange(rec.shape[2]), np.arange(rec.shape[1]))
                mask0 = ((xx-cent)**2+(yy-cent)**2 <= flattened_radius**2)
                mask = np.zeros(rec.shape, dtype='bool')
                for i in range(mask.shape[0]):
                    mask[i, :, :] = mask0
                rec[mask] = (rec[mask] + rec0[mask])/2
            else:
                rec = tomopy.recon(data, theta, center=center, algorithm=algorithm, **kwargs)
            rec = tomopy.remove_outlier(rec, tolerance)
            rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

            for i in range(rec.shape[0]):
                slice = fstart + i*sino_step
                if test_mode:
                    dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}_{:d}.tiff').format(slice, center), dtype=dtype)
                else:
                    dxchange.write_tiff(rec[i, :, :], fname=os.path.join(dest_folder, 'recon/recon_{:05d}.tiff').format(slice), dtype=dtype)
                if save_sino:
                    dxchange.write_tiff(data[:, i, :], fname=os.path.join(dest_folder, 'sino/recon_{:05d}_{:d}.tiff').format(slice, center), dtype=dtype)
            print('Block {:d} finished in {:.2f} s.'.format(iblock, time.time()-t0))
            iblock += 1
    return
Example #53
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
Example #54
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
TomoPy example script to reconstruct the tomography data as
with gridrec.
"""
from __future__ import print_function
import tomopy
import dxchange

if __name__ == '__main__':

    # Set path to the micro-CT data to reconstruct.
    fname = '../../../tomopy/data/tooth.h5'

    # Select the sinogram range to reconstruct.
    start = 0
    end = 2

    # Read the APS 2-BM 0r 32-ID raw data.
    proj, flat, dark = dxchange.read_aps_32id(fname, sino=(start, end))

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta = tomopy.angles(proj.shape[0])

    # Set data collection angles as equally spaced between 0-180 degrees.
    proj = tomopy.normalize(proj, flat, dark)

    # Set data collection angles as equally spaced between 0-180 degrees.
    rot_center = tomopy.find_center(proj, theta, init=290, ind=0, tol=0.5)
Example #55
0
import tomopy
import pylab

obj = tomopy.shepp3d()  # Generate an object.
ang = tomopy.angles(180)  # Generate uniformly spaced tilt angles.
sim = tomopy.project(obj, ang)  # Calculate projections.
# rec = tomopy.recon_accelerated(sim, ang, algorithm='ospml_quad', hardware='Xeon_Phi', implementation='tomoperi') # Reconstruct object.

rec = tomopy.recon_accelerated(sim, ang, algorithm="ospml_hybrid")

# Show 64th slice of the reconstructed object.
pylab.imshow(rec[64], cmap="gray")
pylab.show()
Example #56
0
import tomopy

if __name__ == '__main__':

    # Set path to the micro-CT data to reconstruct.
    fname = 'data_dir/sample_name_prefix'

    # Select the sinogram range to reconstruct.
    start = 0
    end = 16

    # Read the APS 1-ID raw data.
    proj, flat, dark = tomopy.read_aps_1id(fname, sino=(start, end))

    # Set data collection angles as equally spaced between 0-180 degrees.
    theta  = tomopy.angles(proj.shape[0])

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

    # Find rotation center.
    rot_center = tomopy.find_center(proj, theta, emission=False, init=1024, ind=0, tol=0.5)
    print "Center of rotation: ", rot_center

    # Reconstruct object using Gridrec algorithm.
    rec = tomopy.recon(proj, theta, center=rot_center, algorithm='gridrec', emission=False)
        
    # Mask each reconstructed slice with a circle.
    rec = tomopy.circ_mask(rec, axis=0, ratio=0.95)

    # Write data as stack of TIFs.
Example #57
0
from normalize import normalize_bo

dataset = '/home/svvenkatakrishnan/data/20131106_074854_S45L3_notch_OP_10x.h5'
#20130807_234356_OIM121R_SAXS_5x.h5'
algo = 'fbp'
num_sets = 2

tomo, flats, darks, floc = tomopy.read_als_832h5(dataset, sino=(1000, 1010, 1))
print('Data read complete')

print('Displaying  sinogram')
imgplot = plt.imshow(tomo[:, 0, :])
plt.show()

print('Generating angles')
theta = tomopy.angles(tomo.shape[0])

print('Normalization')
tomo, weight = normalize_bo(tomo, flats, darks, num_sets)

#print('Ring removal')
#tomo = tomopy.remove_stripe_fw(tomo)

print('Recon')
rec = tomopy.recon(tomo, theta, center=1328, algorithm=algo)  #1294

print('Masking')
rec = tomopy.circ_mask(rec, 0)

imgplot = plt.imshow(rec[:, 0, :])
plt.show()
Example #58
0
    end = 2048

    # Set number of data chunks for the reconstruction.
    chunks = 64
    num_sino = (end - start) // chunks

    for m in range(chunks):
        sino_start = start + num_sino * m
        sino_end = start + num_sino * (m + 1)

        # Read APS 32-ID raw data.
        proj, flat, dark = tomopy.read_aps_32id(fname,
                                                sino=(sino_start, sino_end))

        # Set data collection angles as equally spaced between 0-180 degrees.
        theta = tomopy.angles(proj.shape[0])

        # Remove the missing angles from data.
        proj = np.concatenate(
            (proj[0:miss_projs[0], :, :], proj[miss_projs[1] + 1:-1, :, :]),
            axis=0)
        theta = np.concatenate(
            (theta[0:miss_projs[0]], theta[miss_projs[1] + 1:-1]))

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

        # Reconstruct object using Gridrec algorithm.
        rec = tomopy.recon(proj,
                           theta,
                           center=1024,
Example #59
0
# -*- coding: utf-8 -*-
"""
Created on Fri Nov  6 14:41:29 2015

@author: lbluque
"""

import tomopy
import numpy as np

proj_num = 45
input_path = '/home/lbluque/TestDataSets/PhantomSets/projections832small' + str(proj_num)
basename = 'projections832small' + str(proj_num)
filename = basename + '_0000.tif'
input_name = input_path + '/' + filename
ind = range(proj_num)
digits = 4

tomo = tomopy.read_tiff_stack(input_name, ind, digits)
tomo_padded = np.pad(tomo, ((0, 0), (0,0), (20,20)), 'constant', constant_values=0)
center = (tomo.shape[2] - 1)/2.0
theta = tomopy.angles(tomo.shape[0], 90, 270 - 180/proj_num)

rec = tomopy.recon(tomo_padded, theta, center=center, algorithm='gridrec', emission=False)

output_path = '/home/lbluque/TestRecons/PhantomRecons/' + basename + '_reverse/'
outname = output_path + basename
tomopy.write_tiff_stack(rec, fname=outname, digit=digits)