Commit bf449cc6 authored by Yassine's avatar Yassine
Browse files

test

parent 8a500a83
#####################
# #
# TimeSeries.py #
# 05/2022 #
# ENIB/ZG2 #
# G. desmeulles #
# #
#####################
import copy
import csv
import matplotlib.pyplot as plt
#constructor
def create(filename=None,time_stamp_column_number=0):
ts={'data':[],'labels':[]}
if filename!=None:
with open(filename, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in spamreader:
ts['data'].append(row)
ts['labels']=ts['data'].pop(0)
ts['xlabel']=ts['labels'][0]
if time_stamp_column_number:
swap_column(ts,0,time_stamp_column_number)
#str to float
for i in range(len(ts['data'])):
for j in range(len(ts['data'][i])):
ts['data'][i][j]=float(ts['data'][i][j])
return ts
#accessor/mutators
def get_data(ts): return copy.deepcopy(ts['data']) #deepcopy
def get_labels(ts): return ts['labels'][:]
def get_value(ts,index,column_number): return ts['data'][index][column_number]
def get_size(ts): return len(ts['data'])
def get_nb_column(ts): return len(ts['data'][0])
def set_data(ts,data): ts['data']=copy.deepcopy(data) #deepcopy
def set_labels(ts,labels): ts['labels']=labels[:]
def set_label(ts,column_number,label): ts['labels'][column_number]=label
def set_value(ts,index,column_number,value): ts['data'][index][column_number]=value
#operations
def swap_column(ts,n1,n2):
''' swap two column in ts'''
for row in ts['data']:
row.insert(n1,row.pop(n2))
ts['labels'].insert(n1,ts['labels'].pop(n2))
def plot(ts,x_label=None,y_label="",title="",filename=None):
'''trace each curve with matplotlib'''
if x_label==None:
x_label=ts['labels'][0]
nb_columns=len(ts['data'][0])
columns=[[] for x in range(nb_columns)]
for i in range(0,len(ts['data'])):
for j in range(nb_columns) :
columns[j].append(float(ts['data'][i][j]))
fig, ax = plt.subplots()
for i in range(nb_columns-1):
ax.plot(columns[0], columns[i+1], label=ts['labels'][i+1])
ax.set(xlabel=x_label, ylabel=y_label, title=title)
ax.legend()
plt.show()
if filename:
fig.savefig(filename)
def dump(ts,filename):
'''save ts in a file'''
with open(filename, 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',)
spamwriter.writerow(ts['labels'])
for line in ts['data']:
output = []
for val in line:
output.append(str(val))
spamwriter.writerow(output)
#Nouvelle fonction
def clone(ts_in):
'''clone ts'''
ts_out=create()
ts_out['data']=get_data(ts_in)
ts_out['labels']=get_labels(ts_in)
return ts_out
def round_data(ts, column_number,precision):
''' round data with a certain precision in a column'''
for i in range(len(ts['data'])):
ts['data'][i][column_number]=round(ts['data'][i][column_number],precision)
def fuse(ts1,ts2):
'''fuse two ts into a third one'''
#column 1 is timestamp in all ts
ts_out = create()
ts_out['labels'] = ts1['labels']+ts2['labels'][1:]
d1 = get_data(ts1)
d2 = get_data(ts2)
#list to build output data
missing_data_1 = ['missing' for x in range(len(d1[0])-1)]
missing_data_2 = ['missing' for x in range(len(d2[0])-1)]
#insert value terminal value
max_time=d1[len(d1)-1][0]+d2[len(d2)-1][0]
d1.append([max_time]+missing_data_1)
d2.append([max_time]+missing_data_1)
i1=0
i2=0
data=[]
while i1<(len(d1)-1) or i2<(len(d2)-1):
dt_value = d2[i2][0] - d1[i1][0]
if dt_value == 0:
#2 samples at the same time stamp
data.append([d1[i1][0]]+d1[i1][1:]+d2[i2][1:])
i1+=1
i2+=1
elif dt_value>0:
#next value is in d1
data.append([d1[i1][0]]+d1[i1][1:]+missing_data_2)
i1+=1
else :
#next value is in d2
data.append([d2[i2][0]]+missing_data_1+d2[i2][1:])
i2+=1
ts_out['data']=data
return ts_out
def interpolate_missing_data(ts):
'''fill missing data in a ts'''
data=ts['data']
for column_number in range(1,len(data[0])):
#for each column
#fill first values:
if data[0][column_number]=='missing':
#find first value
i=1
while data[i][column_number]=='missing':
i+=1
first_value=data[i][column_number]
while i>=0:
data[i][column_number]=first_value
i-=1
#fill last values
if data[len(data)-1][column_number]=='missing':
#find first value
i=len(data)-2
while data[i][column_number]=='missing':
i-=1
last_value=data[i][column_number]
while i<=len(data)-1:
data[i][column_number]=last_value
i+=1
#interpolate values
for raw_number in range(1,len(data)):
if data[raw_number][column_number]=='missing':
t_missing=data[raw_number][0]
missing_value=None
#find previous value:
i=raw_number-1
while data[i][column_number]=='missing':
i-=1
t_prev=data[i][0]
previous_value=data[i][column_number]
#find next value:
i=raw_number+1
while data[i][column_number]=='missing':
i+=1
t_next=data[i][0]
next_value=data[i][column_number]
#interpolate
#slope of line
m = (next_value - previous_value )/(t_next - t_prev)
missing_value= m * (t_missing-t_prev) + previous_value
data[raw_number][column_number]=missing_value
def shift(ts,time_shift):
'''shift ts by time_shift'''
for i in range(len(ts['data'])):
ts['data'][i][0]+=time_shift
def truncate(ts, t_min, t_max):
'''truncate data with timestamps < t_min and > t_max'''
for i in range(len(ts['data']),0):
if ts['data'][i][0]<t_min:
ts['data'].pop(i)
elif ts['data'][i][0]>t_max:
ts['data'].pop(i)
def compute_error(ts, column_number_1,column_number_2,error_label="error curve (rad/s)"):
'''build error curve'''
for i in range(len(ts['data'])):
ts['data'][i].append(ts['data'][i][column_number_2]-ts['data'][i][column_number_1])
ts['labels'].append(error_label)
def compute_area(ts,column_number):
'''integrate data'''
area=0
for i in range(1,len(ts['data'])-1):
d_t=(ts['data'][i+1][0]-ts['data'][i-1][0])/2.
value=abs(ts['data'][i][column_number])
d_area=d_t*value
area+=d_area
return area
def pop(ts,column_number):
'''remove column in ts'''
for i in range(len(ts['data'])):
ts['data'][i].pop(column_number)
ts['labels'].pop(column_number)
def remove_outliers(ts,column_number):
for i in range(1,len(ts['data'])-1):
rate = ts['data'][i][column_number]/ts['data'][i+1][column_number]
if rate > 5.0 or rate<0.2:
ts['data'][i][column_number]='missing'
def apply_to_column(ts,column_number,f):
for i in range(len(ts['data'])):
ts['data'][i][column_number]=f(ts['data'][i][column_number])
if __name__=='__main__':
#ZG2 semaine 1
def test_1():
ts = create('HCSR04_data4_ressort_2022_03_10.csv',3)
print(ts)
plot(ts,y_label='distance(mm)',title='Courbe ZG2!!',filename='test.png')
#ZG2 semaine 2
def test_2():
ts = create('HCSR04_data4_ressort_2022_03_10.csv',3)
dump(ts,'test.csv')
#ZG2 semaine 3
def test_3():
ts1 = create('ts1.csv')
ts2 = create('ts2.csv')
round_data(ts1,0,5)
round_data(ts2,0,5)
ts3=fuse(ts1,ts2)
print("\nts3:")
print(ts3)
ts4=interpolate_missing_data(ts3)
plot(ts4)
print('test TimeSeries.py')
#test_1()
#test_2()
#test_3()
\ No newline at end of file
time(s),omega raw (rad/S)
2.445600000000002,3.6819178910422052
2.5455999999999945,13.931812149444504
2.6455999999999835,28.93642921539297
2.7455999999999725,46.30738915601456
2.8455999999999615,65.47191557091897
2.9455999999999505,83.40494273579189
3.0455999999999395,101.29672397556213
3.1455999999999285,114.90941403809707
3.2455999999999174,120.48777020052293
3.3455999999999064,130.92333645201677
3.4455999999998954,131.628950971268
3.5455999999998844,131.6782442026456
3.6455999999998734,133.4782495118324
3.7455999999998624,124.2835438847182
3.8455999999998514,120.78471719543634
3.9455999999998403,113.51919824409946
4.04559999999983,107.75156821365684
4.145599999999819,102.67389659239697
4.245599999999808,9.799461944836905
4.345599999999797,89.19123767769118
4.445600000000008,86.68888700522274
4.545600000000219,8.629913417117704
4.64560000000043,83.17146193070907
4.745600000000641,84.02916532759845
4.845600000000852,87.01094464830553
4.945600000001063,87.61969154661647
5.045600000001274,91.61970065843119
5.145600000001485,90.48827106491777
5.245600000001696,96.3552253327932
5.345600000001907,96.0835270828729
5.445600000002118,97.53952927223126
5.545600000002329,100.84296875005683
5.64560000000254,100.26680805767165
5.745600000002751,101.66216836393276
5.845600000002962,105.2006675843542
5.945600000003173,102.75733625578015
6.045600000003384,102.95770782622849
6.145600000003595,99.98465680346972
6.245600000003806,102.70631693450339
6.3456000000040165,100.82051382112613
6.445600000003783,98.70764197983975
6.54560000000355,96.81072647658137
6.645600000003317,96.26020082831174
6.745600000003084,98.73471989277819
6.845600000002851,97.08043657642419
6.945600000002618,96.19618503825423
7.045600000002385,97.36349301112546
7.145600000002152,98.24379584666896
7.245600000001919,95.62043858645939
7.345600000001686,98.15525789102901
7.445600000001453,96.81050006219789
7.54560000000122,97.10999992546417
7.645600000000987,97.08279971725153
7.745600000000754,100.42910173093723
7.845600000000521,98.62670963716178
7.9456000000002875,9.96766860313327
8.045600000000054,100.6687366543738
8.14559999999982,96.26805862695093
8.245599999999587,100.98312481470758
8.345599999999354,100.99207321154366
8.445599999999121,97.11398094296945
8.545599999998888,98.93419922945493
8.645599999998655,97.47352927325949
8.745599999998422,95.83364108606622
8.84559999999819,98.04605959699423
8.945599999997956,97.98483391134137
9.045599999997723,97.14111745219144
9.14559999999749,98.73380879370502
9.245599999997257,96.86634590628587
9.345599999997024,97.63461561123246
time(s),omega sim (rad/S)
0.05000000000002935,0.9197779706386822
0.10000000000007934,3.5636348632244093
0.15000000000012934,7.745753121807209
0.20000000000017934,13.267263555177083
0.2500000000002293,19.920896988424794
0.29999999999889154,27.49541639025462
0.34999999999755377,35.779766615817856
0.399999999996216,44.566889054181445
0.4499999999948782,53.65715876374787
0.49999999999354044,62.861411919626406
0.5499999999949782,72.00354140260959
0.599999999996416,80.9226479672937
0.6499999999978537,89.47474349449458
0.6999999999992915,97.53401123909296
0.7500000000007293,104.9936356292654
0.8000000000021671,111.76622097875901
0.8500000000036049,117.78382438424924
0.9000000000050427,122.99763305895108
0.9500000000064804,127.37732038602032
1.000000000007918,130.9101180630534
1.0500000000038048,133.59964387199437
1.0999999999996914,135.4645258814028
1.1499999999955781,136.53686431871483
1.1999999999914648,136.8605719980609
1.2499999999873515,136.48963312351717
1.2999999999832381,135.48631858487883
1.3499999999791248,133.91939360460245
1.3999999999750115,131.86235086631288
1.4499999999708981,129.3916991439502
1.4999999999667848,126.58533404353015
1.5499999999626715,123.52101385190024
1.5999999999585581,120.27495974137462
1.6499999999544448,116.9205957839277
1.6999999999503315,113.52744045637739
1.7499999999462181,110.16015763550939
1.7999999999421048,106.87777154890249
1.8499999999379915,103.7330468154885
1.8999999999338781,100.7720316239216
1.9499999999297648,98.03375929317562
1.9999999999256515,95.5501009663829
2.0499999999326404,93.34576002655845
2.0999999999396293,91.43839700407608
2.149999999946618,89.8388722761056
2.199999999953607,88.55159273659108
2.249999999960596,87.5749478340693
2.299999999967585,86.90181992077352
2.3499999999745738,86.52015371216233
2.3999999999815627,86.41356979885109
2.4499999999885516,86.5620075577538
2.4999999999955405,86.94238344739975
2.5500000000025294,87.52925151386854
2.6000000000095183,88.29545394697746
2.650000000016507,89.21275067918491
2.700000000023496,90.25241827997023
2.750000000030485,91.38580973492273
2.800000000037474,92.58486808091132
2.8500000000444627,93.82258826787071
2.9000000000514516,95.07342300718311
2.9500000000584405,96.31362972191093
3.0000000000654294,97.52155701336889
3.0500000000724183,98.67787028265934
3.1000000000794072,99.76571727861395
3.150000000086396,100.7708353717977
3.200000000093385,101.68160326765512
3.250000000100374,102.4890406629316
3.300000000107363,103.1867600138616
3.3500000001143517,103.77087512011094
3.4000000001213406,104.23987163588441
3.4500000001283295,104.5944449016522
3.5000000001353184,104.83731065163633
3.5500000001423073,104.9729942001195
3.600000000149296,105.00760365218085
3.650000000156285,104.9485925307734
3.700000000163274,104.80451697311285
3.750000000170263,104.58479233575916
3.800000000177252,104.29945367160485
3.8500000001842407,103.95892411489409
3.9000000001912296,103.57379474416936
3.9500000001982185,103.15461899939028
4.000000000205207,102.71172421945609
4.050000000212196,102.25504235051876
4.100000000219185,101.793961363785
4.150000000226174,101.33719842267405
4.200000000233163,100.89269536149591
4.250000000240152,100.46753658806878
4.300000000247141,100.06788910682107
4.35000000025413,99.69896398193572
4.400000000261119,99.36499822541361
4.4500000002681075,99.06925580560407
4.500000000275096,98.81404622910041
4.550000000282085,98.60075895378972
4.600000000289074,98.42991174290434
4.650000000296063,98.3012109681014
4.700000000303052,98.21362181213941
4.750000000310041,98.16544630587856
4.80000000031703,98.15440715741953
4.850000000324019,98.17773538931239
4.900000000331008,98.23225988937803
4.9500000003379965,98.31449709738762
5.000000000344985,98.42073918956571
5.050000000351974,98.54713928106408
5.100000000358963,98.6897923390223
5.150000000365952,98.84481068115399
5.200000000372941,99.00839312299317
5.25000000037993,99.17688702710463
5.300000000386919,99.34684269600159
5.350000000393908,99.51505973408062
5.400000000400897,99.67862517956692
5.4500000004078855,99.83494337264733
5.500000000414874,99.98175767865585
5.550000000421863,100.11716432339888
5.600000000428852,100.23961872028657
5.650000000435841,100.34793477482201
5.70000000044283,100.44127774053497
5.750000000449819,100.51915127159374
5.800000000456808,100.581379371013
5.850000000463797,100.6280839700656
5.9000000004707855,100.65965889503828
5.950000000477774,100.67674098246454
6.000000000484763,100.68017909490595
6.050000000491752,100.67100176729598
6.100000000498741,100.65038418031703
6.15000000050573,100.61961511386087
6.200000000512719,100.58006448168578
6.250000000519708,100.53315198985004
6.300000000526697,100.48031739770464
6.350000000533686,100.42299279284109
6.4000000005406745,100.3625772220179
6.450000000547663,100.30041394994079
6.500000000554652,100.23777054840266
6.550000000561641,100.17582195069141
6.60000000056863,100.11563654166498
6.650000000575619,100.05816529309826
6.700000000582608,100.0042338979762
6.750000000589597,99.9545378066177
6.800000000596586,99.90964002270717
6.850000000603575,99.86997147853333
6.9000000006105635,99.8358337763944
6.950000000617552,99.80740405727636
7.000000000624541,99.78474173835022
import TimeSeries
import copy
def main():
ts_raw = TimeSeries.create("donnees_brutes.csv")
ts_sim = TimeSeries.create("donnees_simulees.csv")
TimeSeries.round_data(ts_raw, 0, 5)
TimeSeries.round_data(ts_sim, 0, 5)
TimeSeries.remove_outliers(ts_raw, 1)
time_shift = -2.
errors = []
copy_raw = copy.deepcopy(ts_raw)
TimeSeries.shift(copy_raw, -2.)
for i in range(0,100):
time_shift-= 1/100.0
TimeSeries.shift(copy_raw, -1/100.0)
ts_fused=TimeSeries.fuse(copy_raw,ts_sim)
TimeSeries.interpolate_missing_data(ts_fused)
TimeSeries.compute_error(ts_fused, 1, 2)
error = TimeSeries.compute_area(ts_fused, 3)
errors.append((time_shift,error))
min_error = 100000000000000000000000
min_shift = 0
for d in errors:
if d[1]<min_error:
min_error= d[1]
min_shift=d[0]
print(min_shift)
TimeSeries.shift(ts_raw, min_shift)
ts_fused = TimeSeries.fuse(ts_raw, ts_sim)
TimeSeries.interpolate_missing_data(ts_fused)
TimeSeries.compute_error(ts_fused, 1, 2)
TimeSeries.plot(ts_fused)
TimeSeries.dump(ts_fused, 'ts_fused.csv')
if __name__ == "__main__":
main()
\ No newline at end of file
......@@ -5,7 +5,8 @@ from tkinter.messagebox import showinfo
import new_plot
import webbrowser
import os
import TimeSeries
import copy
def github():
webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley")
......@@ -71,7 +72,7 @@ def generateur_courbes():
y_entry.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
time_label = tk.Label(
new, text="Position de colonne temps: Mettre -1 si c'est à la fin")
new, text="Position de colonne temps: Mettre -1 si c'est à la fin sinon mettre 0")
time_label.place(relx=0.5, rely=0.6, anchor=tk.CENTER)
time_entry = tk.Entry(new)
time_entry.place(relx=0.5, rely=0.7, anchor=tk.CENTER)
......@@ -272,6 +273,133 @@ def equa_diff():
Generate_button.grid(row=12,column=1)
######Courbe d'erreur
def erreur():
newWindow = tk.Toplevel(app)
label = tk.Label(newWindow, text="Choisir deux fichiers csv à simuler")
label.pack()
files = []
def select_files():
filetypes = (
('text files', '*.csv'),
('All files', '*.*')
)
filenames = fd.askopenfilenames(
title='Selectionner les fichiers',
initialdir='./',
filetypes=filetypes)
for i in filenames:
i = os.path.basename(i)
print(i)
showinfo(
title='Fichiers sélectionnés',
message=filenames
)
if len(filenames) != 2:
showinfo(
title='Erreur',
message="Vous devez choisir seulement 2 fichiers"
)
else:
newWindow.destroy()