Example #1
0
    popt, pcov = curve_fit(lambda x, a, b, c, d: fourier(x, a, b, c, d),
                           X_trim, Y_trim, [30, 5, 0, 20])

    #create resid
    resid = Y_trim - fourier(X_trim, *popt)
    #plt.plot(X_trim,Y_trim)
    plt.plot(X_trim, fourier(X_trim, *popt))

    #resid = [1,1,1,1]
    sd = statistics.stdev(resid) * 3

    #plt.fill_between(X,Y+sd,Y-sd,facecolor='red', alpha=0.5)
    plt.plot(X, Y, color="blue")

    if draw_peaks:
        max_p = np.max(peakBBAGrid.data_at_loc(loc)[:, 2])
        for x in peakBBAGrid.data_at_loc(loc)[:, 1]:
            i = np.argmin(np.abs(X - x))
            if Y[i] / max_p > percent_trim:
                plt.plot(X[i], Y[i], marker="o", color="red")
            else:
                plt.plot(X[i], Y[i], marker="o", color="black")
    if draw_curves:
        max_p = 0
        for x in curveBBAGrid.data_at_loc(loc)[:, 2]:
            i = np.argmin(np.abs(X - x))
            max_p = max(max_p, Y[i])
        for x in curveBBAGrid.data_at_loc(loc):
            i = np.argmin(np.abs(X - x[2]))
            if x[4] > sd:
                pass
        X = dataGrid.data_at_loc(loc)[:, 0]
        peakGrid.data[loc] = np.array([X[peaks], X[peaks]])
"""
Create a list of peaks in the form [x,y,p]
"""
SCALE = 100


def to_point(x, y, p):
    return [(x - 1) / 15., (y - 1) / 15., SCALE * float(p) / 5]


peaks = []
for k in peakGrid.data.keys():
    x, y = peakGrid.coord(k)
    [peaks.append(to_point(x, y, p)) for p in peakGrid.data_at_loc(k)[:, 1]]
"""
DBSCAN PEAK CLUSTERING
"""
X = np.array(peaks)

clustering = DBSCAN(eps=0.25, min_samples=5).fit(X)

C = len(set(clustering.labels_).difference(set([-1])))
"""
REDUCE DIMENSIONS BASED ON PEAK CLUSTERING
"""
M = np.zeros(shape=(peakGrid.size, C))

for k in peakGrid.data.keys():
    x, y = peakGrid.coord(k)
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import
import matplotlib.pyplot as plt

import numpy as np

from data_loading.data_grid_TiNiSn import DataGrid, DataGrid_TiNiSn_500C, DataGrid_TiNiSn_600C

data_dir = "/home/sasha/Desktop/iterative_curve_fitting_save_test/"
regex = """params_(?P<num>.*?).csv"""
peakGrid = DataGrid(data_dir, regex)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for loc in peakGrid.data.keys():
    x, y = peakGrid.coord(loc)
    for i, peak in enumerate(peakGrid.data_at_loc(loc)[:, 1]):
        if peakGrid.data_at_loc(loc)[i, 0] > 10:
            ax.scatter([x], [y], [peak], marker='o', color='red')
    #xs = [x for p in peaks]
    #ys = [y for p in peaks]

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Q')
#ax.set_xlim3d(0, 150)
#ax.set_ylim3d(0, 150)
#ax.set_zlim3d(0, 1000)

plt.show()
save_path = "/home/sasha/Desktop/python/peak_error/"

mistakes = csv_to_dict(save_path,"peak_errors")

w = 5
h = 5
fig, ax = plt.subplots(w, h)

skip = (w*h)*1
i = 0
for key in mistakes.keys():
    loc = int(key)
    m_list = eval(mistakes[key])
    X = dataGrid.data_at_loc(loc)[:,0]
    Y = dataGrid.data_at_loc(loc)[:,1]
    peaks = peakGrid.data_at_loc(loc)[:,2]
    dips,_ = find_peaks(max(Y) - Y)
    for m in m_list:
        if skip > 0:
            skip -= 1
            continue
        # Nearest Peak Found
        peak_index = np.argmin(np.abs(peaks-X[m]))
        P = np.argmin(np.abs(X - peaks[peak_index]))

        #Find Local Minima
        if len(np.where(dips-m > 0)[0]) == 0:
            D = len(peaks)-1
        else:
            D = np.where(dips-m > 0)[0][0]
mistakes = {}
grid_location = 1
cur = 0
next = False


def nearest_index(x):
    return (np.abs(X - x)).argmin()


for grid_location in range(1, dataGrid.size + 1):
    mistakes[grid_location] = []
    X = dataGrid.data_at_loc(grid_location)[:, 0]
    Y = dataGrid.data_at_loc(grid_location)[:, 1]
    next = False
    while not next:
        plt.cla()
        plt.title(str(grid_location))
        plt.plot(X, Y, color='blue')
        for peak_x in peakGrid.data_at_loc(grid_location)[:, 1]:
            i = (np.abs(X - peak_x)).argmin()
            plt.plot([X[i]], [Y[i]], "x", color='black')
        for loc in mistakes[grid_location]:
            plt.plot([X[loc]], [Y[loc]], 'x', color='red')
        plt.plot([X[cur]], [Y[cur]], "x", color='red')
        plt.draw()
        plt.pause(.1)
    #print(X[mistakes[grid_location]])
dict_to_csv(mistakes, save_path)
"""
Load and Cluster peaks
"""

SCALE = 1000


def to_point(x, y, p):
    return [(x - 1) / 15., (y - 1) / 15., SCALE * float(p) / 5]


peaks = []
for k in peakGrid.data.keys():
    x, y = peakGrid.coord(k)
    X = dataGrid.data_at_loc(k)[:, 0]
    [peaks.append(to_point(x, y, X[p])) for p in peakGrid.data_at_loc(k)]

X = np.array(peaks)
#clustering = SpectralClustering(n_clusters=C,assign_labels="discretize",random_state=0).fit(X)

#clustering = AgglomerativeClustering(n_clusters=C,linkage='average').fit(X)

clustering = DBSCAN(eps=0.25, min_samples=5).fit(X)

#clustering = OPTICS().fit(X)
#clustering = Birch(branching_factor=5, n_clusters=None, threshold=0.5).fit(X)
"""
Plotting
"""
for i, k in enumerate(locations):
    x, y = dataGrid.coord(k)
Example #7
0
from data_loading.data_grid_TiNiSn import DataGrid, DataGrid_TiNiSn_500C, DataGrid_TiNiSn_600C
"""
Load Data and Peak Data
"""
dataGrid = DataGrid_TiNiSn_500C()

data_dir = "/home/sasha/Desktop/iterative_curve_fitting_save_test/"
regex = """params_(?P<num>.*?).csv"""
peakGrid = DataGrid(data_dir, regex)

used_points = set()  #dictionary of used points

total_peaks = 0
for loc in peakGrid.data.keys():
    total_peaks += len(peakGrid.data_at_loc(loc))
"""
Get the adjacent peaks that "connect" to a given peak
"""


def get_adjacent_points(x, y, Q_i, is_vert):
    def dir(dx, dy):
        if not peakGrid.in_grid(x + dx, y + dy):
            return None
        Q = peakGrid.data_at(x, y)[Q_i, 1]
        FWHM = peakGrid.data_at(x, y)[Q_i, 5]

        #all peaks except peak Q_i
        peakQ_in_pattern = np.append(
            peakGrid.data_at(x, y)[:Q_i - 1, 1],
    skip += 1
    if skip < 7:
        continue
    lst = []
    for L in locations:
            lst.append(dataGrid.data[L][:,1])
    im = np.array(lst)
    im_log = np.log(im + 1)
    im_sqrt = np.sqrt(im)

    SCALE = 20
    plt.imshow(np.repeat(im_log,SCALE,axis=0))
    if ShowBBA:
        for i,L in enumerate(locations):
            X = dataGrid.data_at_loc(L)[:,0]
            for peak in peakGrid.data_at_loc(L)[:,1]:
                p =np.argmin(np.abs(X - peak))
                plt.scatter(p,i*SCALE + SCALE//2,marker='x',color="red",s=5)
    else:
        for i,L in enumerate(locations):
            for p in peakGrid.data_at_loc(L):
                plt.scatter(p,i*SCALE + SCALE//2,marker='x',color="red",s=5)
    plt.gca().invert_yaxis()
    #plt.xlim((0,400))
    plt.show()
    #plt.draw()
    #plt.pause(.3)
locations = range(82,96+1)


lst = []
peakGrid = DataGrid(data_dir, regex)

# grid locations to plot
locations = [152, 151, 150, 149, 148, 147, 137, 136, 123]

#how much to shift each grid location vertically
#(makes it easier to see peaks)
#shifts = [0,100,200,300,400]
shifts = [100 * i for i in range(len(locations))]

colors = cm.get_cmap("viridis")

for i, k in enumerate(locations):
    y = dataGrid.data[k][:, 1]
    if len(shifts) == len(locations):
        y = y + shifts[i]
    x = dataGrid.data[k][:, 0]
    plt.plot(x, y, label=str(k), color=colors(i / len(locations)))
    for peak in peakGrid.data_at_loc(k):
        if len(shifts) == len(locations):
            plt.plot(peak[1],
                     shifts[i],
                     color=colors(i / len(locations)),
                     marker="o")
        else:
            plt.plot(peak[1],
                     shifts[i],
                     color=colors(i / len(locations)),
                     marker="o")
plt.show()
from data_loading.data_grid_TiNiSn import DataGrid
"""
Load Data and Peak Data
"""

data_dir = "/home/sasha/Desktop/iterative_curve_fitting_save_test/"
data_dir = "/home/sasha/Desktop/TiNiSn_500C-20190604T152446Z-001/TiNiSn_500C/params/"
regex = """TiNiSn_500C_Y20190218_14x14_t60_(?P<num>.*?)_bkgdSub_1D_params.csv"""
peakGrid = DataGrid(data_dir, regex)

used_points = set()  #dictionary of used points

total_peaks = 0
for loc in peakGrid.data.keys():
    total_peaks += len(peakGrid.data_at_loc(loc))
"""
Get the adjacent peaks that "connect" to a given peak
"""


def get_adjacent_points(x, y, Q_i):
    def dir(dx, dy):
        if not peakGrid.in_grid(x + dx, y + dy):
            return None
        Q = peakGrid.data_at(x, y)[Q_i, 1]
        FWHM = peakGrid.data_at(x, y)[Q_i, 5]

        #all peaks except peak Q_i
        peakQ_in_pattern = np.append(
            peakGrid.data_at(x, y)[:Q_i - 1, 1],
    smooth = []
    for i in range(len(list)):
        a = max(i - int(k / 2), 0)
        b = min(i + int(k / 2), len(list) - 1)
        smooth.append(sum(list[a:b + 1]) / (b - a))
    return smooth


smooth_stack = lambda l, k, n: smooth(l, k) if n == 1 else smooth(
    smooth_stack(l, k, n - 1), k)

for grid_location in range(53, dataGrid.size + 1):
    fig = plt.figure(figsize=(17, 9))
    X = dataGrid.data_at_loc(grid_location)[:, 0]
    Y = dataGrid.data_at_loc(grid_location)[:, 1]
    Slope = [(Y[i] - Y[i + 1]) / (X[i] - X[i + 1]) / 100
             for i in range(len(X) - 1)]

    #for peak_x in peakGrid.data_at_loc(grid_location)[:,1]:
    #    i = (np.abs(X - peak_x)).argmin()
    #    plt.plot([X[i]],[Y[i]],"x",color='black')

    for peak_x in curveGrid.data_at_loc(grid_location)[:, 2]:
        i = (np.abs(X - peak_x)).argmin()
        plt.plot([X[i]], [Y[i]], "x", color='red')
    plt.plot(X, Y, color='blue')
    #plt.plot(X[:-1],Slope,color='green')
    #plt.plot(X,[0 for i in X],color='black')
    plt.title(grid_location)
    plt.show()
    return amp * np.real(wofz(
        ((x - cen) + 1j * gamma) / sigma / np.sqrt(2))) / sigma / np.sqrt(
            2 * np.pi) + slope * x + shift


def plot_curve(curve, params, range):
    plt.plot(range, [curve(x, *params) for x in range], color="green")


for loc in range(1, dataGrid.size):
    X = dataGrid.data_at_loc(loc)[:, 0]
    Y = dataGrid.data_at_loc(loc)[:, 1]

    curve_params = fit_curves_to_data(X, Y)
    peaks = get_peak_indices(X, Y)[:, 0]
    BBA_peaks = peakGrid.data_at_loc(loc)[:, 2]

    plt.plot(X, Y, color="blue")
    """
    for peak in BBA_peaks:
        p = np.argmin(np.abs(X-peak))
        plt.plot(X[p],Y[p],'o',color="black")
    for p in peaks:
        plt.plot(X[p],Y[p],'x',color="red")
    """
    """
    for params in curve_params:
        cen = params[1]
        sig = .1
        range = np.linspace(cen-sig,cen+sig,60)
        plt.plot(range,[voigt_shift(x,*params) for x in range])