コード例 #1
0
ファイル: IOMethods.py プロジェクト: robbertbloem/croc
def import_labview_data(path, base_filename):
    
    try:
        # data
        path_and_filename = path + base_filename + ".csv"
        data = numpy.loadtxt(path_and_filename, dtype = "float", delimiter = ",")
        data = data.T
        
        # time axis in fringes
        path_and_filename = path + base_filename + "_t1.csv"
        t1_axis = numpy.loadtxt(path_and_filename, dtype = "float", delimiter = ",") 
        
        # frequency axis
        path_and_filename = path + base_filename + "_w3.csv"
        w3_axis = numpy.loadtxt(path_and_filename, dtype = "float", delimiter = ",") 
        
        # phase
        path_and_filename = path + base_filename + "_phase.txt"
        f = open(path_and_filename)
        for line in f:
            temp = line
        f.close()
        if temp == "NaN":
            D.printWarning("Phase is Not-a-Number, will be set to 0 ", inspect.stack())
            phase = 0
        else:
            phase = float(temp)
        
        # last pump
        path_and_filename = path + base_filename + "_lastpump.txt"
        f = open(path_and_filename)
        for line in f:
            lastpump = line
        f.close()
                
        # determine number of fringes
        n_fringes = int((len(t1_axis)+1)/2)
        n_pixels = len(w3_axis)
        
        # convert NaN to zeros
        data = numpy.nan_to_num(data)
        
        # labview measures 4000-N to 4000+N, we want the data split into 4000-N to 4000 (non-rephasing) and 4000 to 4000+N (rephasing)
        R = data[n_fringes-1:, :]
        NR = numpy.flipud(data[:n_fringes, :])
        
        # fringe 4000 is included twice. 
        
        # for the FFT, we don't want 4000 to be zero. The axes run from 0 to N
        # also: calculate the axis in fs        
        t1fr_axis = numpy.arange(n_fringes)
        t1fs_axis = numpy.arange(n_fringes) * C.hene_fringe_fs
        
        # return everything
        return R, NR, t1fs_axis, t1fr_axis, w3_axis, phase, lastpump, n_fringes, n_pixels
    
    except IOError:
        D.printError("Unable to LabView data from file " + path + "/" + base_filename, inspect.stack())
        raise
        return 0, 0, 0    
コード例 #2
0
ファイル: IOMethods.py プロジェクト: robbertbloem/croc
def import_data_FS(path_and_filename, n_shots = 30000, n_channels = 37, flag_counter = False):
    """
    This method is a derivative of croc.Pe.PeFS.import_raw_data, but without the reliance on the class structure.
    
    INPUT:
    - path_and_filename (string): where the file can be found
    - n_shots (int): number of shots
    - n_channels (int): number of channels
    
    OUTPUT:
    - m (2d ndarray): array with (channels x shots)
    - fringes (array with two elements): the begin and end fringe, as appended to the data.
    
    CHANGELOG:
    - 201110xx RB: wrote function
    - 201202xx RB: rewrote the function for wider purpose, moved it to IOMethods
    
    """    
    try:
        data = numpy.fromfile(path_and_filename)
        
        # remove the two fringes at the end
        fringes = [data[-2], data[-1]]
        data = data[:-2]
        
        # construct m
        m = numpy.zeros((n_channels, n_shots), dtype = "cfloat")
        
        if flag_counter:
            c = data[-n_shots:]
            if c[-1] == 0:
                # this is to repair a (now fixed) bug from VB6. 
                c = data[-n_shots-1:-1]
            
            data = data[:-n_shots]
        
        # order the data in a 2d array
        i_range = range(n_shots)
        j_range = range(n_channels)
        for i in i_range:
            for j in j_range:
                m[j, i] = data[j + i * n_channels] 
                
        if flag_counter:
            return m, c, fringes
        else:
            return m, fringes
    except IOError:
#        print("ERROR (croc.Resources.IOMethods.import_data_FS): Unable to import data from file " + path_and_filename)
        D.printError("Unable to import raw data from file " + path_and_filename, inspect.stack())
        
        raise
        return 0, 0
コード例 #3
0
ファイル: Plotting.py プロジェクト: robbertbloem/croc
def linear(data, axis, x_range = [0, 0], y_range = [0, 0], x_label = "", y_label = "", title = "", legend = "", new_figure = True, plot_real = True):
    
    if plot_real:
        data = numpy.real(data)
    
    # make the x-axis
    if x_range == [0, 0]:
        x_min = axis[0]
        x_max = axis[-1]
    else:
        x_min = x_range[0]
        x_max = x_range[1]
        
    # select the appropriate data range
    x_min_i = numpy.where(axis > x_min)[0][0]
    x_max_i = numpy.where(axis < x_max)[0][-1] 
    
    axis = axis[x_min_i:x_max_i]
    data = data[x_min_i:x_max_i]     

    try:
        if new_figure:  
            plt.figure()
    
        # the actual plot
        plt.plot(axis, data, label = legend)
        
        plt.xlim(x_min, x_max)
        # plt.ylim(y_min, y_max)
        
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        
        plt.title(title)
        
        if legend != "":
            plt.legend()
        
        if new_figure: 
        
            plt.show()

    except:
        if D.FlagRunningOn == "server":
            D.printError("Is the server correctly configured to show plots?", inspect.stack())
        else:
            raise
コード例 #4
0
ファイル: IOMethods.py プロジェクト: robbertbloem/croc
def import_binned_data(path_and_filename, n_pixels, diagram):

    try:
        data = numpy.fromfile(path_and_filename)

        fringes = [data[-2], data[-1]]
        b_axis = numpy.arange(fringes[0], fringes[1] + 1)
        n_fringes = len(b_axis)
        
        # without the two fringes at the end, the number of elements should be equal to (2*n_pixels + 1), for each chopper state the pixels and the count
        
        if ((len(data)-2)/n_fringes) == (2 * n_pixels + 2):
            
            b_count = [0]*2
            b_count[0] = data[-2 - 2*n_fringes:-2 - n_fringes]
            b_count[1] = data[-2 - n_fringes:-2]
            
            data = data[:2*n_pixels*n_fringes]         
    
            # rearrange the data
            b = numpy.zeros((4, n_fringes, n_pixels), dtype = "cfloat")
            
            for i in range(2): # the two chopper states
                for j in range(n_fringes):
                    for p in range(n_pixels):
                        b[i+diagram*2,j,p] = data[i * n_fringes * n_pixels + j * n_pixels + p]
    
            return b, b_count, b_axis
        
        else:
            D.printWarning("The file does not contain binned data: " + path_and_filename, inspect.stack())
            return 1, 1, 1
        
    except IOError:
        D.printError("Unable to import binned data from file " + path_and_filename, inspect.stack())
        raise
        return 0, 0, 0    
コード例 #5
0
ファイル: Plotting.py プロジェクト: robbertbloem/croc
def contourplot(data, x_axis, y_axis, x_range = [0, 0], y_range = [0, -1], zlimit = -1, contours = 12, filled = True, black_contour = True, x_label = "", y_label = "", title = "", diagonal_line = True, new_figure = True, invert_colors = False, flag_aspect_ratio = True, linewidth = 1):

    """
    croc.Plotting.contourplot
    
    INPUT:
    - data (2d ndarray): the data, in the form of (y, x)
    - x_axis, y_axis (ndarray): the axes. Should have the same length as the corresponding axes in data
    - x_range, y_range (array with 2 elements): the range to be plotted. 
        Possible cases:
        - [min, max]: plot range min to max
        - [0, 0]: plot the whole range
        - [0, -1]: use the range from the other axis. If both have this, it will plot both axes complete. (ie. it is identical to both having [0,0])
    - zlimit (number): the z-range that will be used
        Possible cases:
        zlimit = 0, show all, not don't care about centering around zero
        zlimit = -1, show all, centered around zero
        zlimit = all else, use that, centered around zero
        zlimit = [a,b], plot from a to b
    - contours (number): number of contours to be used
    - filled (BOOL): use colors
    - black_contour (BOOL): use the black contour lines
    - x_label, y_label (string): the labels for the x and y axes
    - title (string): the title of the plot
    - diagonal_line (BOOL): plot a diagonal line
    - new_figure (BOOL): will make a new figure. To make subplots etc, set it to False
    - invert_colors (BOOL, False): will make the red blue and the other way around
    - flag_aspect_ratio (BOOL, True): the aspect ratio will be corresponding to the range plotted
    
    """
            
    # CHECKS
    if invert_colors:
        data = -data

    # check if the lengths of the axes correspond to the shape of the data
    y, x = numpy.shape(data)
    try:
        if len(x_axis) != x or len(y_axis) != y:
            # oops, the shape of the data does not correspond with the axes.
            
            # see if they are switched
            if len(x_axis) == y and len(y_axis) == x:
                D.printWarning("The shape of the data seems to be flipped. The data will be transposed.", inspect.stack())
                data = data.T
            else:
                D.printError("The shape of the data does not correspond with the axes.", inspect.stack())
                return 0
    except TypeError:
        D.printError("The x or y axis seems to have no length. It should be an array, not an integer.", inspect.stack())
        print("x-axis:", x_axis)
        print("y-axis:", y_axis)
        return 0

    # determine the range to be plotted
    x_min, x_max, y_min, y_max = find_axes(x_axis, y_axis, x_range, y_range)
    
    # make the contours
    # first find the area to be plotted
    # not the most elegant way I guess
    try:
        y_min_i = numpy.where(y_axis < y_min)[0][-1]
    except: 
        y_min_i = 0
    
    try:
        y_max_i = numpy.where(y_axis > y_max)[0][0] + 1
    except: 
        y_max_i = len(y_axis)

    try:
        x_min_i = numpy.where(x_axis < x_min)[0][-1]
    except: 
        x_min_i = 0
    
    try:
        x_max_i = numpy.where(x_axis > x_max)[0][0] + 1
    except: 
        x_max_i = len(x_axis)
    
#    print(x_axis[x_max_i], x_max)
    
    # truncate the data, this speeds up the plotting
    data = data[y_min_i:y_max_i,x_min_i:x_max_i]
    x_axis = x_axis[x_min_i:x_max_i]
    y_axis = y_axis[y_min_i:y_max_i]
    
    # now make the actual contours   
    #V = make_contours_2d(data[y_min_i:y_max_i, x_min_i:x_max_i], zlimit, contours)
    V = make_contours_2d(data, zlimit, contours)
    
    # print some extra stuff if the debug flag is set
    if debug_flag:
        print("Range to be plotted (x_min, x_max, y_min, y_max):")
        print(x_min, x_max, y_min, y_max)
    
        print("Indices of plotted range (x_min_i, x_max_i, y_min_i, y_max_i):")
        print(x_min_i, x_max_i, y_min_i, y_max_i)
        
    if zlimit == -1:
        print("zlimit: " + str(V[-1]))
    
    try:
        # make the actual figure
        if new_figure:
            plt.figure()
    
        if filled:
                    
            plt.contourf(x_axis, y_axis, data, V, cmap = my_cmap)
            if debug_flag:
                plt.colorbar()
        if black_contour:
            if filled:
                plt.contour(x_axis, y_axis, data, V, linewidths = linewidth, linestyles = "solid", colors = "k")
            else:
                # this will make dashed lines for negative values
                plt.contour(x_axis, y_axis, data, V, colors = "k", linewidths = linewidth)            
    
        if flag_aspect_ratio and new_figure:
            # setting the aspect ration will break the subplots
            plt.axes().set_aspect("equal")

        # the diagonal line
        if diagonal_line:
            # plt.plot([0, 10000], [0, 10000], "k", linewidth = linewidth)
            plt.plot([x_axis[0]-100,x_axis[0]+100], [x_axis[0]-100,x_axis[0]+100], "k", linewidth = linewidth)
        # we only want to see a certain part of the spectrum   
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)
        
        # add some text
        if x_label != "" and x_label != "no_label":
            plt.xlabel(x_label)
    
        if y_label != "" and y_label != "no_label":
            plt.ylabel(y_label)
        
        if title != "":
            plt.title(title)    
        
        # show it!
        if new_figure:
            plt.show()     
    except:
        if D.FlagRunningOn == "server":
            D.printError("Is the server correctly configured to show plots?", inspect.stack())
        else:
            raise