89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
import psycopg2
|
|
import matplotlib.pyplot as plt
|
|
from scipy import signal
|
|
import numpy as np
|
|
|
|
# data_spin_histogram
|
|
def histogram_slope(x_data, y_data, slope =0, bins =0, plot =True):
|
|
if len(x_data)!=len(y_data):
|
|
print('Scale of x_data and y_data are different.')
|
|
return False
|
|
elif bins == 0:
|
|
bins = round(len(x_data)/2)
|
|
arr_list = [y_data[i] - slope*x_data[i] for i in range(len(x_data))]
|
|
cons_max = max(arr_list)
|
|
cons_min = min(arr_list)
|
|
hist_range = np.linspace(cons_min, cons_max, bins+1)
|
|
hist_range_mid = [(hist_range[i]+hist_range[i+1])/2 for i in range(len(hist_range)-1)]
|
|
hist_range[-1] = hist_range[-1]+1
|
|
hist_list = [len([j for j in arr_list if j>=hist_range[i] and j<hist_range[i+1]]) for i in range(len(hist_range)-1)]
|
|
peek_temp_list,_ = signal.find_peaks(hist_list)
|
|
peek_temp_list = list(peek_temp_list)
|
|
print(peek_temp_list)
|
|
# peek_temp_list[0] = list(peek_temp_list[0])
|
|
if np.sign(np.diff(hist_list))[0] < 0:
|
|
# print( np.sign(np.diff(hist_list))[0])
|
|
peek_temp_list = [0] + peek_temp_list
|
|
if np.sign(np.diff(hist_list))[-1] > 0:
|
|
# print(np.sign(np.diff(hist_list))[-1])
|
|
peek_temp_list = peek_temp_list+[len(hist_list)-1]
|
|
print(peek_temp_list)
|
|
|
|
peek_list = [i for i in peek_temp_list if hist_list[i] >= len(x_data)/bins*3]
|
|
print(peek_list)
|
|
results_half = signal.peak_widths(hist_list,peek_list, rel_height=0.8)
|
|
peak_min,peak_max = [cons_min + (cons_max-cons_min)/bins*j for j in results_half[2]],[cons_min + (cons_max-cons_min)/bins*j for j in results_half[3]]
|
|
print(peak_min,peak_max)
|
|
peak_80=[]
|
|
for i in range(len(peak_min)):
|
|
peak_filt_list = [j for j in arr_list if j>=peak_min[i] and j<=peak_max[i]]
|
|
print(np.mean(peak_filt_list))
|
|
print(len(peak_filt_list))
|
|
peak_80.append(np.mean(peak_filt_list))
|
|
|
|
print(results_half[2])
|
|
|
|
print(results_half)
|
|
plt.plot(peak_80, [hist_list[j] for j in peek_list], "y")
|
|
if plot:
|
|
plt.hist(arr_list, bins)
|
|
plt.plot([hist_range_mid[i] for i in peek_list], [hist_list[j] for j in peek_list], "xb")
|
|
plt.plot(peak_80, [hist_list[j] for j in peek_list], "xr")
|
|
# plt.hlines(results_half[1],[ hist_list[int(j)] for j in results_half[2]],[ hist_list[int(j)] for j in results_half[2]], color="g")
|
|
plt.savefig("histogrim.png")
|
|
return hist_list, hist_range_mid, peek_list
|
|
|
|
def run():
|
|
conn = psycopg2.connect(database="postgres", user="biopro", password="BioProControlBox", host="127.0.0.1", port="5432")
|
|
cur = conn.cursor()
|
|
id = 14304
|
|
|
|
sql_str = f'select "raw_data" from "recording_data_metas" WHERE id = {id}'
|
|
cur.execute(sql_str)
|
|
data = cur.fetchall()[0][0]
|
|
channel_data = []
|
|
for i in ['1','2']:
|
|
channel_data_temp = []
|
|
for j in data[i]:
|
|
# print(j)
|
|
sql_str = f'select "data" from "{i}_recording_data_raws" WHERE id = {j}'
|
|
cur.execute(sql_str)
|
|
raw_data = cur.fetchall()[0][0]
|
|
raw_data = raw_data.replace('"***"',' ').split(" ")
|
|
channel_data_temp =channel_data_temp + [int(raw_data[k]) for k in range(len(raw_data)) if k%2]
|
|
# print(len(channel_data_temp))
|
|
channel_data.append(channel_data_temp)
|
|
# print(channel_data[1][-1])
|
|
# print(channel_data[0][:500])
|
|
plt.plot(channel_data[0],channel_data[1],'o',markersize=2)
|
|
plt.hlines(1e6,2.4e6,2.6e6,color='g')
|
|
plt.savefig('test.png')
|
|
plt.close()
|
|
|
|
x_data, y_data ,peek_list = histogram_slope(channel_data[0],channel_data[1],bins = 200)
|
|
print([y_data[i] for i in peek_list])
|
|
print(peek_list, channel_data)
|
|
return channel_data, peek_list
|
|
# print([y_data])
|
|
|