Esempio n. 1
0
def plot_3D(coords, fig_num, filepath):

    logfile = file_io.read_logfile(filepath)

    ypix = np.uint(str.split(logfile[8], '=')[1])
    xpix = np.uint(str.split(logfile[9], '=')[1])
    zsteps = logfile[18]
    zsteps = str.split(zsteps, "=")[1]
    zsteps = np.uint(str.split(zsteps, ",")[0])

    num_stacks = len(coords)
    for stack_nr in range(0, num_stacks):

        x_loc = coords[stack_nr][:, 0]
        y_loc = coords[stack_nr][:, 1]
        z_loc = coords[stack_nr][:, 2]

        fig = plt.figure(fig_num)
        ax = fig.gca(projection='3d')
        ax.scatter(x_loc, y_loc, z_loc, zdir='z')

    plt.axis([0, xpix, 0, ypix, 0, zsteps])
    plt.xlabel('X-Coordinate [pix]')
    plt.ylabel('Y-Coorindate [pix]')
    return logfile
Esempio n. 2
0
def get_global_coords(filepath, threshold_factor, mask_size,max_num_peaks):
    # Get file parameters 
    logfile = file_io.read_logfile(filepath)
    
    ypix   = np.uint(str.split(logfile[8],'=')[1])
    xpix   = np.uint(str.split(logfile[9],'=')[1])
    zsteps = logfile[18]
    zsteps = str.split(zsteps,"=")[1]
    zsteps = np.uint(str.split(zsteps,",")[0])
    nframes= np.uint(str.split(logfile[7],'=')[1])
    nstacks= int(nframes/zsteps)
    
    # Get Peak coordinates from all stacks 
    stack=np.zeros([xpix,ypix,zsteps], dtype = np.uint16)
    num_traces_pstack=np.empty(nstacks, dtype = np.uint16)
    peak_coordinates = list(range(0,nstacks))
    
    threshold_factor=1.8
    mask_size = [11,11,17]
    for stack_nr in range(0,nstacks):
        # Read stack from file     
        for slice_nr in range(stack_nr*zsteps,stack_nr*zsteps + zsteps):
            stack[:,:,slice_nr-stack_nr*zsteps]=file_io.read_bin(filepath,slice_nr)
            
        # Get Peak coordinates from stack
        [peak_coordinates_stack, num_trace] = Get_Traces_3D(filepath,stack,threshold_factor,mask_size,max_num_peaks)
        peak_coordinates[stack_nr] = peak_coordinates_stack
        num_traces_pstack[stack_nr]=num_trace
    
    return peak_coordinates, num_traces_pstack
Esempio n. 3
0
def get_ROI_from_stack(filepath,stack,peak_coords,mask):
    
    # Get file parameters     
    logfile = file_io.read_logfile(filepath)

    ypix = np.uint(str.split(logfile[8],'=')[1])
    xpix = np.uint(str.split(logfile[9],'=')[1])
    zsteps = logfile[18]
    zsteps = str.split(zsteps,"=")[1]
    zsteps = np.uint(str.split(zsteps,",")[0])   
    size_x,size_y,size_z=mask.shape
    
    x_range = np.int16([peak_coords[0]-np.floor(size_x/2),peak_coords[0]+np.floor(size_x/2)])
    y_range = np.int16([peak_coords[1]-np.floor(size_y/2),peak_coords[1]+np.floor(size_y/2)])
    z_range = np.int16([peak_coords[2]-np.floor(size_z/2),peak_coords[2]+np.floor(size_z/2)])
    
    mask_bounds = np.int16([np.floor(size_x/2),np.floor(size_y/2), np.floor(size_z/2)])    
    mask_temp = mask
    
    # X-coordinates too small:
    if  peak_coords[0] < mask_bounds[0]:
        x_range = np.int16([0,peak_coords[0]+np.floor(size_x/2)])
        mask_temp = mask_temp[mask_bounds[0]-peak_coords[0]::,:,:]
     
    # X-coordinates too large:
    if peak_coords[0] + mask_bounds[0] >= xpix:
        x_range = np.int16([peak_coords[0]-np.floor(size_x/2),xpix])
        ind_cut = ((x_range[0])+size_x)-xpix
        mask_temp = mask_temp[0:(size_x-ind_cut),:,:]
    
    # Y-coordinates too small:
    if peak_coords[1] < mask_bounds[1]:
        y_range = np.int16([0,peak_coords[1]+np.floor(size_y/2)])
        mask_temp = mask_temp[:,mask_bounds[1]-peak_coords[1]::,:]
        
    # Y-coordinates too large:             
    if peak_coords[1] + mask_bounds[1] >= ypix:
        y_range = np.int16([peak_coords[1]-np.floor(size_y/2),ypix])
        ind_cut = ((y_range[0])+size_y)-ypix
        mask_temp = mask_temp[:,0:(size_y-ind_cut),:]
        
    # Z-coordinates too small:          
    if peak_coords[2] < mask_bounds[2]:
        z_range = np.int16([0,peak_coords[2]+np.floor(size_z/2)])
        mask_temp = mask_temp[:,:,mask_bounds[2]-peak_coords[2]::]
        
    # Z-coordinates too large:
    if peak_coords[2] + mask_bounds[2] >= zsteps:
        z_range = np.int16([peak_coords[2]-np.floor(size_z/2),zsteps])
        ind_cut = ((z_range[0])+size_z)-zsteps
        mask_temp = mask_temp[:,:,0:(size_z-ind_cut)]
        
    ROI_stack = stack[x_range[0]:x_range[1]+1,y_range[0]:y_range[1]+1,z_range[0]:z_range[1]+1]
    mask_size = np.shape(mask_temp)
    
    return ROI_stack, mask_size
Esempio n. 4
0
def get_zprofile(file_path, trace_coordinate, stack, mask_size):

    #Create mask
    size_x, size_y = mask_size[0:2]
    mask = create_2D_mask(size_x, size_y, 10, 10)

    # Get log-file parameters
    logfile = file_io.read_logfile(file_path)

    ypix = np.uint(str.split(logfile[8], '=')[1])
    xpix = np.uint(str.split(logfile[9], '=')[1])
    size_x, size_y = mask.shape

    ### Get z-profile for fitting Lorentzian squared
    x_range = np.int16([
        trace_coordinate[0] - np.floor(size_x / 2),
        trace_coordinate[0] + np.floor(size_x / 2)
    ])
    y_range = np.int16([
        trace_coordinate[1] - np.floor(size_y / 2),
        trace_coordinate[1] + np.floor(size_y / 2)
    ])

    mask_bounds = np.int16([np.floor(size_x / 2), np.floor(size_y / 2)])
    mask_temp = mask

    # X-coordinates too small:
    if trace_coordinate[0] < mask_bounds[0]:
        x_range = np.int16([0, trace_coordinate[0] + np.floor(size_x / 2)])
        mask_temp = mask_temp[mask_bounds[0] - trace_coordinate[0]::, :, :]

    # X-coordinates too large:
    if trace_coordinate[0] + mask_bounds[0] >= xpix:
        x_range = np.int16([trace_coordinate[0] - np.floor(size_x / 2), xpix])
        ind_cut = ((x_range[0]) + size_x) - xpix
        mask_temp = mask_temp[0:(size_x - ind_cut), :, :]

    # Y-coordinates too small:
    if trace_coordinate[1] < mask_bounds[1]:
        y_range = np.int16([0, trace_coordinate[1] + np.floor(size_y / 2)])
        mask_temp = mask_temp[:, mask_bounds[1] - trace_coordinate[1]::, :]

    # Y-coordinates too large:
    if trace_coordinate[1] + mask_bounds[1] >= ypix:
        y_range = np.int16([trace_coordinate[1] - np.floor(size_y / 2), ypix])
        ind_cut = ((y_range[0]) + size_y) - ypix
        mask_temp = mask_temp[:, 0:(size_y - ind_cut), :]

    ROI_stack = stack[x_range[0]:x_range[1] + 1, y_range[0]:y_range[1] + 1, :]

    # Get z-profile
    data_z = np.mean(ROI_stack, (0, 1)) - np.mean(stack, (0, 1))

    return data_z
Esempio n. 5
0
def get_global_coords(filepath, threshold_factor, mask_size, max_num_peaks):
    # Get file parameters
    logfile = file_io.read_logfile(filepath)

    ypix = np.uint(str.split(logfile[8], '=')[1])
    xpix = np.uint(str.split(logfile[9], '=')[1])
    zsteps = logfile[18]
    zsteps = str.split(zsteps, "=")[1]
    zsteps = np.uint(str.split(zsteps, ",")[0])
    nframes = np.uint(str.split(logfile[7], '=')[1])
    nstacks = int(nframes / zsteps)

    if zsteps == 1:
        nstacks = 1

    else:
        nframes = np.uint(str.split(logfile[7], '=')[1])
        nstacks = int(nframes / zsteps)

    # Get Peak coordinates from all stacks
    stack = np.zeros([xpix, ypix, zsteps], dtype=np.uint16)
    num_traces_pstack = np.empty(nstacks, dtype=np.uint16)
    peak_coordinates = list(range(0, nstacks))

    for stack_nr in range(0, nstacks):
        # Read stack from file
        for slice_nr in range(stack_nr * zsteps, stack_nr * zsteps + zsteps):
            stack[:, :, slice_nr - stack_nr * zsteps] = file_io.read_bin(
                filepath, slice_nr)

        # Get Peak coordinates from stack
        [peak_coordinates_stack,
         num_trace] = Get_Traces_3D(filepath, stack, threshold_factor,
                                    mask_size, max_num_peaks)
        peak_coordinates[stack_nr] = peak_coordinates_stack
        num_traces_pstack[stack_nr] = num_trace

    ## Remove traces with wrong coordinates
    if len(peak_coordinates) == 1:
        peak_coordinates = peak_coordinates[0]

    global_coords_corrected = np.empty([0, 3])
    trace_nr = 0
    for trace_coords in peak_coordinates:
        if trace_coords[0] < 5 or trace_coords[1] < 5:
            trace_nr = trace_nr + 1
        else:
            trace_coords = np.expand_dims(trace_coords, 1)
            trace_coords = np.swapaxes(trace_coords, 0, 1)
            global_coords_corrected = np.append(global_coords_corrected,
                                                trace_coords, 0)
            trace_nr = trace_nr + 1

    return global_coords_corrected, num_traces_pstack
Esempio n. 6
0
def plot_hist_errs(errors, fig_num, filepath):

    logfile = file_io.read_logfile(filepath)

    zsteps = str.split(logfile[19], '=')[1]
    zsteps = zsteps.replace(',', '.', 1)
    zsteps = np.double(zsteps)

    pix_size = str.split(logfile[10], '=')[1]
    pix_size = pix_size.replace(',', '.', 1)
    pix_size = np.double(pix_size)
    pix_size = (pix_size * 260) / pix_size

    fit_errors = errors

    plt.figure(fig_num)
    plt.title('Fit Errors')

    fit_err_x = np.ndarray.flatten(fit_errors[0][:, 2]) * pix_size
    fit_err_y = np.ndarray.flatten(fit_errors[0][:, 3]) * pix_size
    fit_err_z = np.ndarray.flatten(fit_errors[0][:, 4]) * zsteps

    plt.subplot(3, 1, 1)
    plt.title('X-Coordinate', fontsize=16)
    plt.hist(fit_err_x, 50, [0, 10])
    plt.tick_params(labelsize=16)

    plt.subplot(3, 1, 2)
    plt.title('Y-Coordinate', fontsize=16)
    plt.hist(fit_err_y, 50, [0, 10])
    plt.tick_params(labelsize=16)

    plt.subplot(3, 1, 3)
    plt.title('Z-Coordinate', fontsize=16)
    plt.hist(fit_err_z, 50, [0, 10])
    plt.tight_layout()

    plt.xlabel('Fit Error [nm]', fontsize=16)
    plt.tick_params(labelsize=16)
Esempio n. 7
0
def read_bin(path_binfile, slice_nr):
    path_binfile = func.ChangeExtension(path_binfile, '.bin')

    path_logfile = func.ChangeExtension(path_binfile, '.log')
    data_log = file_io.read_logfile(path_logfile)

    # Get 2D dimensions image from log file
    ypix = np.uint(str.split(data_log[8], ' ')[3])
    xpix = np.uint(str.split(data_log[9], ' ')[3])
    pix_slice = ypix * xpix

    # Open file and read data of 1 slice
    f = open(path_binfile, mode='rb')

    if slice_nr != 0:
        f.seek(slice_nr * pix_slice * 2, 0)
        im_slice = np.fromfile(f, dtype=np.uint16, count=pix_slice)
    else:
        im_slice = np.fromfile(f, dtype=np.uint16, count=pix_slice)

    f.close()
    im_slice = np.reshape(im_slice, [ypix, xpix], 'F')

    return im_slice
Esempio n. 8
0
        im_slice = np.fromfile(f, dtype=np.uint16, count=pix_slice)

    f.close()
    im_slice = np.reshape(im_slice, [ypix, xpix], 'F')

    return im_slice


aap2 = read_bin(filepath, 0)

#%%

path_binfile = func.ChangeExtension(filepath, '.bin')

path_logfile = func.ChangeExtension(filepath, '.log')
data_log = file_io.read_logfile(path_logfile)

# Get 2D dimensions image from log file
ypix = np.uint(str.split(data_log[8], ' ')[3])
xpix = np.uint(str.split(data_log[9], ' ')[3])
pix_slice = ypix * xpix

# Open file and read data of 1 slice
f = open(path_binfile, 'rb')
slice_nr = 1
if slice_nr != 0:
    f.seek(slice_nr * pix_slice * 2, 0)
    im_slice = np.fromfile(f, count=pix_slice, dtype='>i2')
else:
    im_slice = np.fromfile(f, count=pix_slice, dtype=np.uint16)
Esempio n. 9
0
def get_local_coords(filepath,global_coords,mask_size):

    # Get Measurement Parameters 
    logfile = file_io.read_logfile(filepath)
    zsteps = logfile[18]
    zsteps = str.split(zsteps,"=")[1]
    zsteps = np.uint(str.split(zsteps,",")[0])
    
### Get ROI from the stack using Global Coordinates and Fit 3D Gaussian
    
    # Make 3D Mask
    size_x,size_y,size_z=mask_size
    mask = create_3D_mask(size_x,size_y,size_z,10,10)
    
    # Define Bounds
    max_bounds=(65536,65536,size_x,size_y,size_z,size_x-1,size_y-1,size_z-1)
    min_bounds=(0,0,0,0,0,0,0,0)
    bounds = (min_bounds,max_bounds)

### Fit ROI to a 3D Gauss 
      
    # Allocate memory for all fit parameters and errors 
    num_stacks= len(global_coords)    
    fit_params_all, mask_size_all, fit_errors_all = [list(range(0,num_stacks)),list(range(0,num_stacks)),list(range(0,num_stacks))]
    
    for stack_nr in range(0,num_stacks):
        
        # Read one stack from .bin file 
        stack = file_io.get_stack(filepath,stack_nr)
        global_coords_stack = global_coords[stack_nr]
        
        # Allocate memory for fit parameters for 1 stack 
        coords_stack = global_coords[stack_nr]
        fit_params_stack = np.empty([len(coords_stack[:,0]),len(max_bounds)])
        fit_errors_stack = np.empty([len(coords_stack[:,0]),len(max_bounds)])
        mask_size_stack  = np.empty([len(coords_stack[:,0]),3])
           
        for trace_nr in range(0,len(global_coords_stack)):
            
            # Get ROI
            peak_coords = global_coords_stack[trace_nr,:]
            ROI_stack,mask_size = get_ROI_from_stack(filepath,stack,peak_coords,mask)
            
            ##### Fit Data 
            # Get ROI-data from the stack
            data = np.ndarray.flatten(ROI_stack)
            size_x,size_y,size_z = mask_size    
            x = np.arange(0,size_x)
            y = np.arange(0,size_y)
            z = np.arange(0,size_z)
            
            # Create xyz-coordinates 
            xx, yy, zz = np.meshgrid(x, y, z, sparse=False)
            xyz = [np.ndarray.flatten(xx),np.ndarray.flatten(yy), np.ndarray.flatten(zz)]
           
            # Initial Fit Parameters
            in_A  = float(max(data))
            in_C  = float(min(data))
            in_x0 = max(x)/2
            in_y0 = max(y)/2
            in_z0 = max(z)/2
            in_wx = 1.5
            in_wy = 1.5
            in_wz = 3
            
            # p0 is the initial guess for the fit coefficients
            p0 = [in_A, in_C, in_x0, in_y0, in_z0, in_wx, in_wy, in_wz]
            
            try:
                coeff, var_matrix = curve_fit(gauss_3D, xyz, data, p0=p0,bounds=bounds, absolute_sigma=True)
                perr = np.sqrt(np.diag(var_matrix))
                
            except RuntimeError:
                print('Error - curve_fit failed')
                coeff = np.empty(len(p0))                
                perr  = np.empty(len(p0))
                
            fit_params_stack[trace_nr,:]= coeff
            fit_errors_stack[trace_nr,:]= perr
            mask_size_stack[trace_nr,:] = mask_size

            
        fit_params_all[stack_nr]= fit_params_stack
        fit_errors_all[stack_nr]= fit_errors_stack
        mask_size_all[stack_nr] = mask_size_stack
        
### Transform Global to Local (sub-pixel) Coordinates
    local_coords = list(range(0,num_stacks))
    for stack_nr in range(0,num_stacks):
        
        fit_x,fit_y,fit_z            = fit_params_all[stack_nr][:,2],fit_params_all[stack_nr][:,3],fit_params_all[stack_nr][:,4]
        global_x,global_y,global_z   = global_coords[stack_nr][:,0],global_coords[stack_nr][:,1],global_coords[stack_nr][:,2]
        mask_size_x, mask_size_y, mask_size_z = mask_size_all[stack_nr][:,0],mask_size_all[stack_nr][:,1],mask_size_all[stack_nr][:,2]
        
        centered_x = fit_x-mask_size_x/2
        centered_y = fit_y-mask_size_y/2
        centered_z = fit_z-mask_size_z/2
        
        local_x,local_y,local_z = global_x+centered_x, global_y+centered_y, global_z+centered_z
        local_coords_temp  = np.swapaxes(np.array([local_x,local_y,local_z],order='C'),0,1)
        local_coords[stack_nr] = local_coords_temp
        
    return local_coords, fit_params_all, fit_errors_all
Esempio n. 10
0
def Get_Traces_3D(filepath,stack,threshold_factor,mask_size,max_num_peaks):
        
    # Get file parameters     
    logfile = file_io.read_logfile(filepath)

    ypix = np.uint(str.split(logfile[8],'=')[1])
    xpix = np.uint(str.split(logfile[9],'=')[1])
    zsteps = logfile[18]
    zsteps = str.split(zsteps,"=")[1]
    zsteps = np.uint(str.split(zsteps,",")[0])    
        
    # Make 3D Mask
    size_x, size_y, size_z = mask_size 
    mask_bounds = np.int16([np.floor(size_x/2),np.floor(size_y/2), np.floor(size_z/2)])
    mask = create_3D_mask(size_x, size_y, size_z,10,10)
    
    # Find maximum intensity indeces
    
    threshold = np.median(stack)*threshold_factor
    max_intensity=np.max(stack)
    masked_stack = stack
    peak_coordinates = np.array([], dtype=np.uint16)
    num_peaks = 0
    
    if not max_num_peaks or max_num_peaks is 0:
        max_num_peaks = np.inf
    
    while max_intensity > threshold and num_peaks <= max_num_peaks:
    
        peak_coords = np.unravel_index(np.argmax(masked_stack), np.shape(masked_stack))
        max_intensity = masked_stack[peak_coords]
        peak_coords = np.int16(peak_coords)    
        
        x_range = np.int16([peak_coords[0]-np.floor(size_x/2),peak_coords[0]+np.floor(size_x/2)])
        y_range = np.int16([peak_coords[1]-np.floor(size_y/2),peak_coords[1]+np.floor(size_y/2)])
        z_range = np.int16([peak_coords[2]-np.floor(size_z/2),peak_coords[2]+np.floor(size_z/2)])
        
        mask_temp = mask
    
        # X-coordinates too small:
        if  peak_coords[0] < mask_bounds[0]:
            x_range = np.int16([0,peak_coords[0]+np.floor(size_x/2)])
            mask_temp = mask_temp[mask_bounds[0]-peak_coords[0]::,:,:]
         
        # X-coordinates too large:
        if peak_coords[0] + mask_bounds[0] >= xpix:
            x_range = np.int16([peak_coords[0]-np.floor(size_x/2),xpix])
            ind_cut = ((x_range[0])+size_x)-xpix
            mask_temp = mask_temp[0:(size_x-ind_cut),:,:]
    
        # Y-coordinates too small:
        if peak_coords[1] < mask_bounds[1]:
            y_range = np.int16([0,peak_coords[1]+np.floor(size_y/2)])
            mask_temp = mask_temp[:,mask_bounds[1]-peak_coords[1]::,:]
            
        # Y-coordinates too large:             
        if peak_coords[1] + mask_bounds[1] >= ypix:
            y_range = np.int16([peak_coords[1]-np.floor(size_y/2),ypix])
            ind_cut = ((y_range[0])+size_y)-ypix
            mask_temp = mask_temp[:,0:(size_y-ind_cut),:]
            
        # Z-coordinates too small:          
        if peak_coords[2] < mask_bounds[2]:
            z_range = np.int16([0,peak_coords[2]+np.floor(size_z/2)])
            mask_temp = mask_temp[:,:,mask_bounds[2]-peak_coords[2]::]
            
        # Z-coordinates too large:
        if peak_coords[2] + mask_bounds[2] >= zsteps:
            z_range = np.int16([peak_coords[2]-np.floor(size_z/2),zsteps])
            ind_cut = ((z_range[0])+size_z)-zsteps
            mask_temp = mask_temp[:,:,0:(size_z-ind_cut)]
               
        #z_range=np.int16(z_range), x_range=np.int16(x_range), y_range=np.int16(y_range)
        ROI_stack = stack[x_range[0]:x_range[1]+1,y_range[0]:y_range[1]+1,z_range[0]:z_range[1]+1]
        ROI_stack_masked = ROI_stack*mask_temp
        masked_stack[x_range[0]:x_range[1]+1,y_range[0]:y_range[1]+1,z_range[0]:z_range[1]+1]=ROI_stack_masked
        peak_coordinates = np.append(peak_coordinates,peak_coords)
        
        num_peaks = num_peaks+1
    
    peak_coordinates = np.reshape(peak_coordinates,[3,int(len(peak_coordinates)/3)],1)
    peak_coordinates = np.transpose(peak_coordinates)
    
    return peak_coordinates, num_peaks
Esempio n. 11
0
##         to acquire the spectral parameters
## Step 4: Concatonate all global coordinates and fit parameters in a DataFrame
##         and write to a .xlsx file.

########################

#%% Load Necessary Libraries
import numpy as np
import file_io as file_io
import functions_2Pspectra as func
import pandas as pd
import matplotlib.pyplot as plt

#%% Get filename & File parameters
file_path = file_io.get_path()
file_log = file_io.read_logfile(file_path)

ypix = np.uint(str.split(file_log[8], '=')[1])
xpix = np.uint(str.split(file_log[9], '=')[1])
zsteps = file_log[18]
zsteps = str.split(zsteps, "=")[1]
zsteps = np.uint(str.split(zsteps, ",")[0])

if zsteps == 1:
    nstacks = 1
else:
    nframes = np.uint(str.split(file_log[7], '=')[1])
    nstacks = int(nframes / zsteps)

#%% Get Global coordinates from stack
threshold_factor = 1.3
Esempio n. 12
0
def fit_zprofile(file_path, global_coords, stack_nr, mask_size):
    # Get Measurement Parameters
    file_log = file_io.read_logfile(file_path)
    zsteps = file_log[18]
    zsteps = str.split(zsteps, "=")[1]
    zsteps = np.uint(str.split(zsteps, ",")[0])

    ### Get ROI from the stack using Global Coordinates and Fit 3D Gaussian

    # Make 3D Mask
    size_x, size_y, size_z = mask_size
    mask = create_3D_mask(size_x, size_y, size_z, 10, 10)

    # Define Bounds
    max_bounds = (65536, 65536, size_x, size_y, size_z, size_x - 1, size_y - 1,
                  size_z - 1)
    #min_bounds=(0,0,0,0,0,0,0,0)
    #bounds = (min_bounds,max_bounds)

    ### Fit ROI to a 3D Gauss

    # Read one stack from .bin file
    if zsteps == 1:
        nstacks = 1
        stack = file_io.read_bin_all(file_path)
    else:
        nframes = np.uint(str.split(file_log[7], '=')[1])
        nstacks = int(nframes / zsteps)
        stack = file_io.read_stack(file_path, stack_nr)

    # Allocate memory for fit parameters & errors for 1 stack
    fit_params = np.empty([len(global_coords[:, 0]), len(max_bounds)])
    fit_errors = np.empty([len(global_coords[:, 0]), len(max_bounds)])
    mask_size_traces = np.empty([len(global_coords[:, 0]), len(mask_size)])

    for trace_nr in range(0, len(global_coords)):

        # Get ROI
        peak_coords = global_coords[trace_nr, :]
        ROI_stack, mask_size_trace = func.get_ROI_from_stack(
            file_path, stack, peak_coords, mask)

        ##### Fit Data #####
        # Do not fit Z-coordinates when measurement is in 2D
        if zsteps == 1:
            coeff, perr = fit_2D_gauss(ROI_stack, mask_size_trace)
        else:
            coeff, perr = fit_3D_gauss(ROI_stack, mask_size_trace)

        fit_params[trace_nr, :] = coeff
        fit_errors[trace_nr, :] = perr
        mask_size_traces[trace_nr, :] = mask_size_trace

    ### Transform Global to Local (sub-pixel) Coordinates
    local_coords = list(range(0, nstacks))

    fit_x, fit_y, fit_z = fit_params[:, 2], fit_params[:, 3], fit_params[:, 4]
    global_x, global_y, global_z = global_coords[:,
                                                 0], global_coords[:,
                                                                   1], global_coords[:,
                                                                                     2]
    mask_size_x, mask_size_y, mask_size_z = mask_size[0], mask_size[
        1], mask_size[2]

    centered_x = fit_x - mask_size_x / 2
    centered_y = fit_y - mask_size_y / 2

    if zsteps == 1:
        centered_z = 0
    else:
        centered_z = fit_z - mask_size_z / 2

    local_x, local_y, local_z = global_x + centered_x, global_y + centered_y, global_z + centered_z
    local_coords_temp = np.swapaxes(
        np.array([local_x, local_y, local_z], order='C'), 0, 1)
    local_coords = local_coords_temp

    return local_coords, fit_params, fit_errors
Esempio n. 13
0
def get_local_and_spectrum(file_path, global_coords_corrected, mask_size,
                           stack_nr):
    i = 0
    file_path_dat = file_io.change_extension(file_path, '.dat')
    data_dat = pd.read_csv(file_path_dat, delimiter='\t', decimal=',')
    wavelength = np.array(data_dat['Spectrum peak (nm)'])

    # Get Measurement Parameters
    file_log = file_io.read_logfile(file_path)
    zsteps = file_log[18]
    zsteps = str.split(zsteps, "=")[1]
    zsteps = np.uint(str.split(zsteps, ",")[0])

    ### Get ROI from the stack using Global Coordinates and Fit 3D Gaussian

    # Make 3D Mask
    size_x, size_y, size_z = mask_size

    # Define Bounds
    max_bounds = (65536, 65536, size_x, size_y, size_x - 1, size_y - 1, 1200,
                  300)
    #min_bounds=(0,0,0,0,0,0,0,0)
    #bounds = (min_bounds,max_bounds)

    ### Fit ROI to a 3D Gauss

    # Read one stack from .bin file
    if zsteps == 1:
        nstacks = 1
        stack = file_io.read_bin_all(file_path)
        mask = tools.create_3D_mask(size_x, size_y, len(wavelength), 10, 10)

    else:
        nframes = np.uint(str.split(file_log[7], '=')[1])
        nstacks = int(nframes / zsteps)
        stack = file_io.read_stack(file_path, stack_nr)
        mask = tools.create_3D_mask(size_x, size_y, size_z, 10, 10)

    # Allocate memory for fit parameters & errors for 1 stack
    fit_params = np.empty(
        [len(global_coords_corrected[:, 0]),
         len(max_bounds)])
    fit_errors = np.empty(
        [len(global_coords_corrected[:, 0]),
         len(max_bounds)])
    mask_size_traces = np.empty(
        [len(global_coords_corrected[:, 0]),
         len(mask_size)])

    for trace_nr in range(0, len(global_coords_corrected)):

        # Get ROI
        peak_coords = global_coords_corrected[trace_nr, :]
        ROI_stack, mask_size_trace = get_ROI_from_stack(
            file_path, stack, peak_coords, mask)

        ##### Fit Data #####
        # Do not fit Z-coordinates when measurement is in 2D

        if zsteps == 1:
            coeff, perr = fit_2Dgauss_lorsq(wavelength, ROI_stack,
                                            mask_size_trace)
            i = i + 1
            print(i)

        else:
            coeff, perr = tools.fit_3D_gauss(ROI_stack, mask_size_trace)

        fit_params[trace_nr, :] = coeff
        fit_errors[trace_nr, :] = perr
        mask_size_traces[trace_nr, :] = mask_size_trace

    traces_parameters = np.array(
        np.concatenate((global_coords_corrected, fit_params, fit_errors), 1))

    column_headers = [
        'x_global (pix)', 'y_global (pix)', 'z_global (pix)',
        'amplitude (a.u.)', 'offset (a.u.)', 'x_local (pix)', 'y_local (pix)',
        'width_x (pix)', 'width_y (pix)', 'spectrum peak (nm)',
        'spectrum width (nm)', 'err_amplitude (a.u.)', 'err_offset (a.u.)',
        'err x_local (pix)', 'err_y_local (pix)', 'err_width_x (pix)',
        'err_width_y (pix)', 'err_spectrum peak (nm)', 'err_peak width (nm)'
    ]

    ## Convert Fit Parameters to DataFrame
    pd_fitparams = pd.DataFrame(data=traces_parameters, columns=column_headers)

    ### Transform Global to Local (sub-pixel) Coordinates
    local_coords = list(range(0, nstacks))

    fit_x, fit_y, fit_z = fit_params[:, 2], fit_params[:, 3], fit_params[:, 4]
    global_x, global_y, global_z = global_coords_corrected[:,
                                                           0], global_coords_corrected[:,
                                                                                       1], global_coords_corrected[:,
                                                                                                                   2]
    mask_size_x, mask_size_y, mask_size_z = mask_size[0], mask_size[
        1], mask_size[2]

    centered_x = fit_x - mask_size_x / 2
    centered_y = fit_y - mask_size_y / 2

    if zsteps == 1:
        centered_z = 0
    else:
        centered_z = fit_z - mask_size_z / 2

    local_x, local_y, local_z = global_x + centered_x, global_y + centered_y, global_z + centered_z
    local_coords_temp = np.swapaxes(
        np.array([local_x, local_y, local_z], order='C'), 0, 1)
    local_coords = local_coords_temp

    pd_fitparams['x_local (pix)'] = local_coords[:, 0]
    pd_fitparams['y_local (pix)'] = local_coords[:, 1]

    return pd_fitparams