Пример #1
0
def trajectory_plot(time, data, averaging_time=1.):
    """
    TODO

    """
    data = np.array(data)
    x, x_average, _ = cal.subtract_moving_average(time, data[:, 0],
                                                  averaging_time)
    y, y_average, time = cal.subtract_moving_average(time, data[:, 1],
                                                     averaging_time)

    trajectory = np.stack((x, y), axis=1)
    trajectory_averaged = np.stack((x_average, y_average), axis=1)

    fig = plt.figure()
    titles = ['x', 'y']
    for i in range(2):
        ax = fig.add_subplot(1, 2, i + 1)
        ax.set_title(
            'Trajectory of a trapped particle \n in {} direction'.format(
                titles[i]))
        ax.grid(True)
        ax.set_xlabel('Time [s]')
        ax.set_ylabel('Direction {} '.format(titles[i]) + r'[$\mu m$]')
        ax.scatter(time,
                   trajectory[:, i] + trajectory_averaged[:, i],
                   s=4,
                   label=r'original')
        ax.scatter(time, trajectory_averaged[:, i], s=4, label=r'averaged')
        ax.legend(loc='best')
    fig.tight_layout()
    plt.show()
    return None
Пример #2
0
def calibration_plots(time, data, averaging_time=1., temp=293.):
    """
    TODO (Taken from the calibration.py)

    """
    data = np.array(data)
    x = cal.subtract_moving_average(time, data[:, 0], averaging_time)[0]
    y = cal.subtract_moving_average(time, data[:, 1], averaging_time)[0]
    trajectory, phi, var = cal.center_and_rotate(x, y)
    k = cal.KB * temp / var * 1e12

    def scatter_plot(data, trajectory, phi):
        fig, (ax1, ax2) = plt.subplots(1, 2)
        ax1.set_title('Original data')
        ax1.grid(True)
        ax1.set_xlabel('Direction x ' + r'[$\mu m$]')
        ax1.set_ylabel('Direction y ' + r'[$\mu m$]')
        ax1.scatter(data[:, 0], data[:, 1], s=4)
        ax1.set_aspect('equal')
        ax2.set_title('Centered data, phi = {:.2f} rad'.format(phi, ))
        ax2.grid(True)
        ax2.set_xlabel('Direction x ' + r'[$\mu m$]')
        ax2.set_ylabel('Direction y ' + r'[$\mu m$]')
        ax2.scatter(trajectory[:, 0], trajectory[:, 1], s=4)
        ax2.set_aspect('equal')
        fig.tight_layout()
        plt.show()
        return None

    def histogram_plot(trajectory, var):
        fig = plt.figure()
        titles = ['x', 'y']
        for i in range(2):
            ax = fig.add_subplot(1, 2, i + 1)
            ax.set_xlabel(('Direction {} ' + r'[$\mu m$]').format(titles[i]))
            ax.set_ylabel('Bin height')
            hist, bin_edges = np.histogram(trajectory[:, i],
                                           bins=int(
                                               np.sqrt(len(trajectory[:, i]))),
                                           density=True)
            ax.set_title('k_{} = {:.2e}J/m^2'.format(titles[i], k[i]))
            bin_centres = (bin_edges[:-1] + bin_edges[1:]) / 2.
            ax.scatter(bin_centres, hist, s=4)
            x_model = np.linspace(min(bin_centres), max(bin_centres), 100)
            prefactor = 1. / np.sqrt(2. * np.pi * var[i])
            ax.plot(x_model,
                    prefactor * np.exp(-x_model**2. / (2. * var[i])),
                    label=r'fit')
            ax.legend(loc='best')
        fig.tight_layout()
        plt.show()
        return None

    scatter_plot(data, trajectory, phi)
    histogram_plot(trajectory, var)
    return None
Пример #3
0
def trajectory_plot(time, data, averaging_time=1.):
    """
    Creates a pair of plots showing the particle's trajectory components (x(t),y(t)). Time-averaged data is overlaid onto the raw data points.
    
    Parameters
    ----------
    time : list of floats
        times of recorded points
    data : ndarray of floats
        x- and y-positions of particle at each point in time
    averaging_time : float
        time interval over which data is averaged
    """
    data = np.array(data)
    x, x_average, _ = cal.subtract_moving_average(
        time, data[:, 0], averaging_time)
    y, y_average, time = cal.subtract_moving_average(
        time, data[:, 1], averaging_time)

    trajectory = np.stack((x, y), axis=1)
    trajectory_averaged = np.stack((x_average, y_average), axis=1)

    fig = plt.figure()
    titles = ['x', 'y']
    for i in range(2):
        ax = fig.add_subplot(1, 2, i+1)
        ax.set_title('Trajectory of a trapped particle \n in {} direction'.format(titles[i]))
        ax.grid(True)
        ax.set_xlabel('Time [s]')
        ax.set_ylabel('Direction {} '.format(titles[i]) + r'[$\mu m$]')
        ax.scatter(
            time, trajectory[:, i] + trajectory_averaged[:, i],
            s=4, label=r'original'
            )
        ax.scatter(time, trajectory_averaged[:, i], s=4, label=r'averaged')
        ax.legend(loc='best')
    fig.tight_layout()
    plt.show()
    return None
Пример #4
0
def calibration_plots(time, data, averaging_time=1., temp=293.):
    """
    Creates sets of scatter (y(x)) and histogram (#points(r)) plots for the particle's position. The first plot of each set shows raw data; in the second one, trap drift is averaged out and the trapping potential's axes aligned with the coordinate system.
    
    Parameters
    ----------
    time : list of floats
        times of recorded points
    data : ndarray of floats
        x- and y-positions of particle at each point in time
    averaging_time : float
        time interval over which data is averaged
    temp : float
        temperature of system in Kelvin
    """
    data = np.array(data)
    x = cal.subtract_moving_average(time, data[:, 0], averaging_time)[0]
    y = cal.subtract_moving_average(time, data[:, 1], averaging_time)[0]
    trajectory, phi, var = cal.center_and_rotate(x, y)
    k = cal.KB*temp/var*1e12

    def scatter_plot(data, trajectory, phi):
        fig, (ax1, ax2) = plt.subplots(1, 2)
        ax1.set_title('Original data')
        ax1.grid(True)
        ax1.set_xlabel('Direction x ' + r'[$\mu m$]')
        ax1.set_ylabel('Direction y ' + r'[$\mu m$]')
        ax1.scatter(data[:, 0], data[:, 1], s=4)
        ax1.set_aspect('equal')
        ax2.set_title('Centered data, phi = {:.2f} rad'.format(phi,))
        ax2.grid(True)
        ax2.set_xlabel('Direction x ' + r'[$\mu m$]')
        ax2.set_ylabel('Direction y ' + r'[$\mu m$]')
        ax2.scatter(trajectory[:, 0], trajectory[:, 1], s=4)
        ax2.set_aspect('equal')
        fig.tight_layout()
        plt.show()
        return None

    def histogram_plot(trajectory, var):
        fig = plt.figure()
        titles = ['x', 'y']
        for i in range(2):
            ax = fig.add_subplot(1, 2, i+1)
            ax.set_xlabel(('Direction {} ' + r'[$\mu m$]').format(titles[i]))
            ax.set_ylabel('Bin height')
            hist, bin_edges = np.histogram(trajectory[:, i], bins=int(
                np.sqrt(len(trajectory[:, i]))), density=True)
            ax.set_title('k_{} = {:.2e}J/m^2'.format(titles[i], k[i]))
            bin_centres = (bin_edges[:-1] + bin_edges[1:])/2.
            ax.scatter(bin_centres, hist, s=4)
            x_model = np.linspace(min(bin_centres), max(bin_centres), 100)
            prefactor = 1./np.sqrt(2.*np.pi*var[i])
            ax.plot(x_model, prefactor*np.exp(-x_model **
                                              2./(2.*var[i])), label=r'fit')
            ax.legend(loc='best')
        fig.tight_layout()
        plt.show()
        return None

    scatter_plot(data, trajectory, phi)
    histogram_plot(trajectory, var)
    return None
Пример #5
0
 def test_moving_average(self):
     trajectory = gen.generate([self.kx, self.ky], phi = self.phi)
     t = gen.generate_time()
     x, _, _ = cal.subtract_moving_average(t, trajectory[:, 0], 1)
     y, _, _ = cal.subtract_moving_average(t, trajectory[:, 1], 1)
     self.assertTrue(np.allclose(np.mean(x), 0, atol = 1e-4) and np.allclose(np.mean(y), 0, atol = 1e-4))