Exemplo n.º 1
0
def process(targ):
    if P(targ).is_file():
        targ = P(targ).parent
    regex = re.compile(r"_Scan(\d+).dat")
    experiments = list(set([P("_".join(str(ii).split("_")[:-1]))
                            for ii in P(targ).iterdir() if regex.search(ii.name)]))

    for ind, exp in enumerate(experiments):
        files = [ii for ii in P(targ).iterdir() if ii.name.startswith(
            exp.name) and regex.search(ii.name)]

        for idx, f in enumerate(files):
            header, data = read(f, flipX=False)

            if idx == 0:
                outdata = np.zeros_like(data)
                for row in header.split("\n"):
                    if 'Wavelength:' in row:
                        wv = int(
                            float(("".join([ii for ii in row if ii.isdigit() or ii == "."]))))
            outdata += data
        outdata /= len(files)
        try:
            os.mkdir(str(exp.parent) + '/processed_data/')
        except FileExistsError:
            pass
        fileout = exp.parent.joinpath(
            "processed_data/" + exp.name + f"_{wv}.csv")
        outstr = ""

        for row in outdata:
            if row[0] > 0:
                outstr += f"{row[0]}, {row[1]}\n"
        fileout.write_text(outstr)
        statusBar((ind + 1) / len(experiments) * 100)
Exemplo n.º 2
0
def process(filename):
    header, data = read(filename)

    data_array = np.array(data)
    data_dict = {
        'param': 1E6 * data_array[:, 0],
        'echo': data_array[:, 3],
    }

    popt, pcov = cf(func, data_dict['param'], data_dict['echo'])
    min_idx = np.argmin(func(data_dict['param'], *popt))
    min_time = data_dict['param'][min_idx]
    rabi_freq = 1 / (2 * min_time *
                     1E-6) / 1E6  # convert the us to s then get freq
    # print(popt)
    fig, ax = plt.subplots()
    ax.plot(data_dict['param'], data_dict['echo'], label='Raw data', c='black')
    ax.plot(data_dict['param'],
            func(data_dict['param'], *popt),
            label=f"Min time: {min_time:.2f} $\mu$s\nFreq: {popt[0]:.2f} MHz",
            c='red')
    ax.legend()
    ax.set_ylabel('Echo intensity (arb. u)')
    ax.set_yticks([])
    ax.set_xlabel('$P_1$ length ($\mu$s)')
    ax.set_title('Rabi echo intensity vs. inversion pulse length')
    for s in ['top', 'right']:
        ax.spines[s].set_visible(False)
    plt.savefig(P(filename).parent.joinpath('fixedSourceRabi.png'), dpi=300)
    plt.show()
Exemplo n.º 3
0
def fit(filename, fitax, difax):
    folder = P(filename).parent
    _, data = read(filename)
    field = str(P(filename).stem).split("data_")[-1]
    if TESTING:
        popt, pcov = cf(double_debye,
                        data[:, 0],
                        data[:, 1],
                        method='trf',
                        verbose=0)
        print(field)
    else:
        popt, pcov = cf(double_debye,
                        data[:, 0],
                        data[:, 1],
                        method='trf',
                        verbose=2,
                        p0=[50, 10, 200, 10])
    leg_popt = [f"{ii:.1f}" for ii in popt]
    fitax.plot(data[:, 0],
               double_debye(data[:, 0], *popt),
               label=f'fit: {field}')
    fitax.plot(data[:, 0], data[:, 1], label=f'raw: {field}')
    fitax.legend()

    difax.plot(data[:, 0],
               data[:, 1] - double_debye(data[:, 0], *popt),
               label=f'{field}')
    difax.legend()

    return popt
Exemplo n.º 4
0
def fit(filename, debye_T, approx=False, dims=False):
    header, data = read(filename)
    max_T = 15
    data = data[data[:, 0] < max_T]
    if not approx:
        if dims:
            popt, pcov = cf(debye_ndim, data[:,0], data[:, 1], bounds=(([55, 0, 1/2],[65,np.inf,4])))
        else:
            popt, pcov = cf(debye, data[:,0], data[:, 1], bounds=(([55, 0],[65,np.inf])))
    else:
        popt, pcov = cf(low_T_approx, data[:,0], data[:, 1], p0=[150, 10E-3])
    fig, ax = plt.subplots()
    if not approx:
        if dims:
            ax.plot(data[:,0], debye_ndim(data[:, 0], *popt),label=r"$T_D$" + f"={popt[0]:.1f} K\nA={popt[1]:.1e}\nn={popt[2]:.1f}")
        else:
            ax.plot(data[:,0], debye(data[:, 0], *popt),label=r"$T_D$" + f"={popt[0]:.1f} K\nA={popt[1]:.1e}")
    else:
        ax.plot(data[:,0], low_T_approx(data[:, 0], *popt),label=f"A={popt[0]:.3e}; B={popt[1]:.3e}")
    ax.plot(data[:,0], data[:,1], label="raw")
    ax.legend()
    plt.savefig(P(filename).parent.joinpath('fit_to_0T.png'),dpi=300) 
    # ax.set_yscale('log')
    figg, axx = plt.subplots()
    for field in [0, 1, 3, 6, 9]:
        _, data = read(f"/Users/Brad/Library/Containers/com.eltima.cloudmounter.mas/Data/.CMVolumes/Brad Price/Research/Data/2021/04/1/data_{field}T.dat")
        data = data[data[:, 0] < max_T]
        if not approx:
            if dims:
                plt.plot(data[:, 0], data[:, 1] - debye_ndim(data[:, 0], *popt), label=f"{field} T")
            else:
                plt.plot(data[:, 0], data[:, 1] - debye(data[:, 0], *popt), label=f"{field} T")
        else:
            plt.plot(data[:, 0], data[:, 1] - low_T_approx(data[:, 0], *popt), label=f"{field} T")
    axx.legend()
    plt.savefig(P(filename).parent.joinpath('all_fields_subtract_0T_fit.png'),dpi=300) 
    plt.show()
Exemplo n.º 5
0
def compare(targ='./'):
    if not targ.endswith('/'):
        targ += '/'

    filelist = [
        targ + ii for ii in os.listdir(targ)
        if (ii.startswith('dispersion') or ii.startswith('absorption'))
        and ii.endswith('_exp.txt')
    ]

    disp_add = False
    abs_add = False

    for file in filelist:
        legend = ' '.join([
            ii.title() for ii in P(file).stem.split('_') if
            ('absorption' in ii or 'dispersion' in ii or 'light' in ii.lower())
        ]).replace('light', '').replace('Absorption',
                                        'Abs').replace('Dispersion', 'Disp')
        header, data = read(file)
        plt.figure('Comparison')

        # data[:, 1] = subtract(data[:, 1])

        if not disp_add:
            if 'Disp' in legend:
                disp_add = np.max(data[:, 1])
        if not abs_add:
            if 'Abs' in legend:
                abs_add = np.min(data[:, 1])

        if 'Disp' in legend:
            data[:, 1] += disp_add
        elif 'Abs' in legend:
            data[:, 1] += abs_add

        plt.plot(data[:, 0], data[:, 1], label=legend)

    plt.legend()
    plt.title('Light on/off comparison')
    plt.xlabel('Field (T)')
    plt.ylabel('Signal (arb. u)')
    plt.yticks([])
    plt.ticklabel_format(axis='y', style='sci', scilimits=(-2, 2))
    plt.savefig(targ + 'compared.png', dpi=200)
    plt.show()
Exemplo n.º 6
0
def importData(filename):
    """importData.

    :param filename: file
    """
    file = open(filename, 'r').read()

    if file[0] == '{':
        data = ast.literal_eval(file)
    else:
        data = {}
        header, npdata = read(filename)

        for item in header.strip().split('\n')[-1].split(','):
            itemdata = list(
                npdata[:,
                       header.strip().split('\n')[-1].split(',').index(item)])
            data[item.strip()] = itemdata

    return data
Exemplo n.º 7
0
def create(targ):
    files = [
        ii for ii in P(targ).iterdir()
        if ii.name.endswith('_exp.txt') and ii.name.startswith('dispersion')
    ]

    for f in files:
        outstr = ""
        header, data = read(f)
        data[:, 1] = -1 * np.imag(signal.hilbert(data[:, 1]))

        frac = 100
        deltaB = (data[-1, 0] - data[0, 0]) / len(data[:, 0])
        leftB = np.linspace(data[0, 0] - (data[-1, 0] - data[0, 0]) / frac,
                            data[0, 0],
                            int(((data[-1, 0] - data[0, 0]) / frac) // deltaB))
        rightB = np.linspace(
            data[-1, 0] + (data[-1, 0] - data[0, 0]) / frac, data[-1, 0],
            int(((data[-1, 0] - data[0, 0]) / frac) // deltaB))

        left_zeros = np.zeros_like(leftB)
        right_zeros = np.zeros_like(rightB)

        B = np.zeros(len(data[:, 0]) + len(leftB) + len(rightB))
        Data = np.zeros_like(B)

        B[:len(leftB)] = leftB
        B[len(leftB):len(leftB) + len(data[:, 0])] = data[:, 0]
        B[len(leftB) + len(data[:, 0]):len(leftB) + len(data[:, 0]) +
          len(rightB)] = rightB
        Data[:len(leftB)] = left_zeros
        Data[len(leftB):len(leftB) + len(data[:, 1])] = data[:, 1]
        Data[len(leftB) + len(data[:, 1]):len(leftB) + len(data[:, 1]) +
             len(rightB)] = right_zeros

        outdat = np.transpose(np.array([B, Data]))
        for index, row in enumerate(outdat):
            outstr += f"{row[0]}, {row[1]}\n"
        P(targ).joinpath(f.name.replace('dispersion',
                                        'hilbert_abs')).write_text(outstr)
Exemplo n.º 8
0
def process(filename, on, off, window_frac=10, order=2):
    """process.

    :param filename: input filename
    :param on: on-time for laser
    :param off: off-time for laser
    :window_frac: reciprocal of data length for window choice
    :order: savgol_filter polyfit order
    """
    header, data = read(filename)
    t = data[:, 0]
    r = data[:, 2] + 1j * data[:, 3]
    i = data[:, 4] + 1j * data[:, 5]
    sig = np.abs(r) + 1j * np.abs(i)

    index = 0
    avgsig = []
    avgr = []
    avgi = []

    prevlen = 0

    while index < len(sig):
        lo = np.where(t >= index * (on + off))[0][0]
        hi = np.where(t < (index + 1) * (on + off))[0][-1]

        if np.abs(hi - lo) < prevlen * 0.9:
            break

        if window_frac == 0:  # use 0 to not do savgol filtering
            avgsig.append(np.abs(sig[lo:hi]))
            avgr.append(np.real(r[lo:hi]) + 1j * np.imag(r[lo:hi]))
            avgi.append(np.real(i[lo:hi]) + 1j * np.imag(i[lo:hi]))
        else:
            avgsig.append(
                savgol_filter(np.abs(sig[lo:hi]),
                              (2 * (np.abs(hi - lo) // window_frac) + 1),
                              order))
            avgr.append(
                savgol_filter(
                    np.real(r)[lo:hi], (2 * (np.abs(hi - lo) // window_frac) +
                                        1), order) +
                1j * savgol_filter(
                    np.imag(r)[lo:hi],
                    (2 * (np.abs(hi - lo) // window_frac) + 1), order))
            avgi.append(
                savgol_filter(
                    np.real(i)[lo:hi], (2 * (np.abs(hi - lo) // window_frac) +
                                        1), order) +
                1j * savgol_filter(
                    np.imag(i)[lo:hi],
                    (2 * (np.abs(hi - lo) // window_frac) + 1), order))
        prevlen = np.abs(hi - lo)
        index += 1

    smallen = np.inf

    for row in avgsig:
        if len(row) < smallen:
            smallen = len(row)

    full = np.zeros(smallen)

    plots = {
        "Average mag": avgsig,
        "Ch1 real": avgr,
        "Ch1 imag": avgr,
        "Ch2 real": avgi,
        "Ch2 imag": avgi
    }
    # avgr, "Ch2 real": avgi, "Ch2 imag": avgi}

    for plot, dat in plots.items():
        plt.figure(plot)
        for row in dat:
            if "real" in plot:
                row = np.real(row)
            elif "imag" in plot:
                row = np.imag(row)
            if np.min(row) < 0:
                row -= np.min(row)
            plt.plot(t[:len(row)],
                     row / np.max(row),
                     alpha=0.3,
                     color='lightgray')
            full += row[:smallen] / np.max(row[:smallen])
        full = full / np.max(full)
        popt, pcov = curve_fit(biexponential,
                               t[:smallen],
                               full,
                               maxfev=1000000)
        abovelas = np.where(t > on)[0][0]
        plt.plot(t[:smallen], full, label="Raw data")
        plt.plot(np.linspace(t[abovelas], t[smallen]),
                 biexponential(np.linspace(t[abovelas], t[smallen]), *popt),
                 label=r"Fit: $\tau_1$=" + f"{popt[2]:.2f}" +
                 r" $s^{-1}$;$\tau_2$=" + f"{popt[4]:.2f}" + r" $s^{-1}$")
        plt.legend()
        rang = np.max(full) - np.min(full)
        plt.annotate('Laser\npulse', (on / 2, np.max(full)),
                     color='gray',
                     horizontalalignment='center')
        plt.ylim(np.min(full) - rang / 4, np.max(full) + rang / 4)
        plt.axvspan(0, on, facecolor='palegreen')
        plt.title(plot)
        if plot == "Ch2 imag":
            plt.title("Time-resolved EPR absoption vs. time")
        plt.ylabel('Normalized signal')
        plt.xlabel('Time (s)')
        full = np.zeros(smallen)
        plt.savefig(P(filename).parent.joinpath(f"{plot}.png"))

    plt.show()
Exemplo n.º 9
0
def shrink(filename, start=0, end=-1, directory='./'):
    header, data = read(directory + filename)
    data = data[start:end, :]
    savename = directory + '/short/' + filename
    np.savetxt(savename, data, header=header, delimiter=', ')
Exemplo n.º 10
0
def subtract(targ='./', background=0, experiment=1, downsample=10):
    if not targ.endswith('/'):
        targ += '/'

    filelist = [
        targ + ii for ii in os.listdir(targ)
        if ii.endswith('.dat') and ii.startswith('M')
    ]

    background_file = ''
    data_file = ''

    for file in filelist:
        m = re.search('[M][0-9][0-9]', file)
        num = int(m.group(0).lstrip('M'))

        if num == background and 'AverageLog' in file:
            background_file = file

        if num == experiment and 'AverageLog' in file:
            data_file = file

    if not background_file:
        print('No background average found. Attempting to calculate it.')
        background_list = [
            ii for ii in filelist
            if int(re.search('[M][0-9][0-9]', ii).group(0).lstrip('M')) ==
            background
        ]
        first_head, first_data = read(background_list[0])
        background_sum = np.copy(first_data)
        count = 1

        for file in background_list:
            h, d = read(file)
            background_sum += d
            count += 1
        b_data = background_sum / count
    else:
        b_header, b_data = read(background_file)

    if not data_file:
        print('No data average found. Attempting to calculate it.')
        data_list = [
            ii for ii in filelist
            if int(re.search('[M][0-9][0-9]', ii).group(0).lstrip('M')) == data
        ]
        first_head, first_data = read(data_list[0])
        data_sum = np.copy(first_data)
        count = 1

        for file in data_list:
            h, d = read(file)
            data_sum += d
            count += 1
        data = data_sum / count
    else:
        header, data = read(data_file)

    datatypes = header.strip().split(', ')

    title = ' '.join(data_file.split('/')[-1].split('_')[:-1]).title() + \
        ' spectrum w/o background'

    true_data = data[2:, 1] - b_data[2:, 1]
    plt.figure('True data')
    plt.plot(data[2:, 0], true_data, label=f"{targ.split('/')[-2]}nm")
    plt.plot(data[2::downsample, 0],
             decimate(true_data, downsample),
             label=f"{targ.split('/')[-2]}nm {downsample}x downsample")
    yy = savgol_filter(true_data, len(true_data) // downsample, 2)
    plt.plot(data[2:, 0], yy, label='savgol_filter')
    plt.ticklabel_format(axis='y', style='sci', scilimits=(-2, 2))
    plt.ylabel(datatypes[1])
    plt.yticks([])
    plt.xlabel(datatypes[0])
    plt.title(title)
    plt.legend()
    plt.savefig(targ + 'subtracted.png')
Exemplo n.º 11
0
if __name__ == "__main__":
    _, ax = plt.subplots()
    if FITALL:
        fields = [0, 3, 6, 9]
        _, dax = plt.subplots()
        results = {}
        for field in fields:
            filename = f"/Users/Brad/Library/Containers/com.eltima.cloudmounter.mas/Data/.CMVolumes/Brad Price/Research/Data/2021/04/13/BDPA_{field}T.csv"
            results[f"{field}T"] = list(fit(filename, ax, dax))
        P(filename).parent.joinpath('fit_params.txt').write_text(repr(results))
    else:
        fields = [3, 6, 9]
        filename = f"/Users/Brad/Library/Containers/com.eltima.cloudmounter.mas/Data/.CMVolumes/Brad Price/Research/Data/2021/04/13/BDPA_0T.csv"
        params = P(filename).parent.joinpath("fit_params.txt").read_text()
        popt = ast.literal_eval(params)["0T"]
        _, dat = read(filename)
        f = interp1d(dat[:, 0], dat[:, 1])
        for field in fields:
            # popt = ast.literal_eval(params)[f"{field}T"]
            filename = f"/Users/Brad/Library/Containers/com.eltima.cloudmounter.mas/Data/.CMVolumes/Brad Price/Research/Data/2021/04/13/BDPA_{field}T.csv"
            _, data = read(filename)
            data = data[(data[:, 0] <= dat[-1, 0]) & (data[:, 0] >= dat[0, 0])]
            bdata = f(data[:, 0])
            # ax.plot(data[:, 0], (data[:, 1]-double_debye(data[:, 0], *popt))/(double_debye(data[:,0], *popt))*100, label=f"{field}T")
            # ax.scatter(data[:, 0], (data[:, 1]-bdata)/bdata*100, label=f"{field}T", s=10)
            ax.plot(data[:, 0], (data[:, 1] - bdata) / bdata * 100,
                    label=f"{field}T")
        # ax.plot(data[:, 0], double_debye(data[:, 0], *popt), label="1T fit")
        # ax.set_title(r"$T_{D_1}$=" + f"{popt[0]:.1f}, " + f"A={popt[1]:.1f}, " + r"$T_{D_2}$=" + f"{popt[2]:.1f}, B={popt[3]:.1f}")
        ax.set_title("Field dependent magnetic heat capacity")
        # plot =