Commit 129c9a4a authored by Lorenz Halt's avatar Lorenz Halt 🔀
Browse files

external_store: fix file naming

parent 501d5ade
......@@ -4,17 +4,27 @@ import math
from scipy import signal
import copy
def _new_file_name(folder, name, i):
return os.path.join(
folder,
"{}_{}{}".format(os.path.splitext(name)[0], # name
i, # numbering
os.path.splitext(name)[1])) # extension
def external_data_store(data_folder, data_name,data,header):
if os.path.exists(data_folder)==False:
os.makedirs(data_folder)
i=0
store_path = data_folder + "/" + data_name + "_{}.txt".format(i)
while os.path.exists(store_path):
i=i+1
store_path = data_folder + "/" + data_name + "_{}.txt".format(i)
if os.path.exists(store_path)==False:
np.savetxt(store_path, data, header = header, comments = "", delimiter =",")
def external_data_store(data_folder, data_name, data, header):
if not os.path.exists(data_folder):
os.makedirs(data_folder)
if os.path.splitext(data_name)[1] == '':
data_name += ".csv"
i = 0
file_path = _new_file_name(data_folder, data_name, i)
while os.path.exists(file_path):
i = i + 1
file_path = _new_file_name(data_folder, data_name, i)
write(file_path, data, header)
def resample(data, delta_ms):
......@@ -77,7 +87,7 @@ def traj_handle(data, traj_type, decay_time=None):
def combine_data(data1, data2, op):
if not data1.shape == data2.shape:
raise ValueError("Input data arrays need to have the same shape for operation! Shape1:{}, Shape2:{}".format(data1.shape, data2.shape))
raise ValueError("Input data arrays need to have the same shape for operation '{}'! Shape1:{}, Shape2:{}".format(op, data1.shape, data2.shape))
elif not all(data1[:,0] == data2[:,0]):
raise ValueError("Input data time stamps are not identical! Needs to be resampled. May use 'resample(..)''")
......
%% Cell type:code id: tags:
``` python
import numpy as np
import ilc_lib
import os
from ilc_lib import cos_traj,e_traj
```
%% Cell type:code id: tags:
``` python
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
ws_path = "/home/ipa325/logs"
error_path = ws_path + "/logger_0.csv"
ff_path = ws_path + "/lin_ff_2.txt"
traj_path = ws_path + "/server/trajectory.txt"
out_path = ws_path + "/lin_ff_out.txt"
traj_type = "cos"
L = 2.0
cutoff_freq = 0.5
T = 8 #ms
```
%% Cell type:code id: tags:
``` python
data_org, header = ilc_lib.read(error_path)
#remove first row with negative tick
#data = data[1:]
#plt.plot(data[:,0], data[:,1])
#plt.plot(data[:,0], data[:,2])
plt.plot(data_org[:,0], data_org[:,3], '.-')
```
%% Cell type:code id: tags:
``` python
data = ilc_lib.resample(data_org, T)
plt.plot(data[:,0], data[:,3], '-o')
plt.plot(data_org[:,0], data_org[:,3], 'x')
```
%% Cell type:code id: tags:
``` python
trajectory = ilc_lib.traj_handle(data, "cos", decay_time=None)
#print(trajectory)
#plt.plot(trajectory[:,0], trajectory[:,1])
#plt.plot(trajectory[:,0], trajectory[:,2])
plt.plot(trajectory[:,0], trajectory[:,3], '-o')
print trajectory
print(len(trajectory))
```
%% Cell type:code id: tags:
``` python
d1 = trajectory
d2 = data
print d1.shape
print d2.shape
print(d1.shape == d2.shape)
```
%% Cell type:code id: tags:
``` python
plt.plot(data[:,0], data[:,3])
plt.plot(trajectory[:,0], trajectory[:,3])
plt.show()
data_diff = ilc_lib.combine_data(data, trajectory, "diff")
plt.plot(data_diff[:,0], data_diff[:,3], label='diff')
data_sum = ilc_lib.combine_data(data, trajectory, "sum")
plt.plot(data_sum[:,0], data_sum[:,3], label='sum')
plt.legend()
```
%% Cell type:code id: tags:
``` python
test = data[:,0:2]
test[:,1] = np.arange(0, data[-1,0]+1, T) / (data[-1,0]+1) * L
#plt.plot(test[:,0], test[:,1])
#plt.show()
data_ff = ilc_lib.ilc_calculations(data_diff, L)
data_ff_vec = ilc_lib.ilc_calculations(data_diff[:,0:2], test)
plt.plot(data_diff[:,0], data_diff[:,1], label='diff')
plt.plot(data_ff[:,0], data_ff[:,1], label="np.mult(L)")
plt.plot(data_ff_vec[:,0], data_ff_vec[:,1], label="np.mult(vec)")
plt.legend()
```
%% Cell type:code id: tags:
``` python
old_ff = ilc_lib.ilc_calculations(data_diff, 5)
ilc_data = ilc_lib.combine_data(data_ff, data_ff, "sum")
plt.plot(data_ff[:,0], data_ff[:,3], label="last_ff")
plt.plot(data_ff[:,0], data_ff[:,3], label="data_ff")
plt.plot(ilc_data[:,0], ilc_data[:,3], label="addition")
plt.legend()
```
%% Cell type:code id: tags:
``` python
def pad_data(a, b, val=0):
print a.shape[0]
print b.shape[0]
if a.shape[0] > b.shape[0]:
b = np.pad(b, [(0, a.shape[0]-b.shape[0]), (0, 0)], mode='constant', constant_values=val)
b[:,0] = a[:,0] # restore timestamp
elif b.shape[0] > a.shape[0]:
a = np.pad(a, [(0, b.shape[0]-a.shape[0]), (0, 0)], mode='constant', constant_values=val)
a[:,0] = b[:,0] # restore timestamp
return a, b
```
%% Cell type:code id: tags:
``` python
old_ff, _ = ilc_lib.read(ff_path)
try:
ilc_data = ilc_lib.combine_data(data_ff, old_ff, "sum")
except:
data_ff, old_ff = ilc_lib.pad_data(data_ff, old_ff)
ilc_data = ilc_lib.combine_data(data_ff, old_ff, "sum")
plt.plot(old_ff[:,0], old_ff[:,3], label="last_ff")
plt.plot(data_ff[:,0], data_ff[:,3], label="data_ff")
plt.plot(ilc_data[:,0], ilc_data[:,3], label="addition")
plt.legend()
```
%% Cell type:code id: tags:
``` python
plt.plot(ilc_data[:,0], ilc_data[:,3], label="ilc_data")
ilc_data_t = ilc_lib.butter_lowpass_filter(ilc_data, cutoff_freq, 1/(T*0.001), 5)
plt.plot(ilc_data_t[:,0], ilc_data_t[:,3], label="ilc_data")
plt.plot(ilc_data_t[:,0], ilc_data_t[:,3], label="ilc_data_t")
plt.legend()
```
%% Cell type:code id: tags:
``` python
ilc_lib.write(out_path, ilc_data_t, header)
data_new, header = ilc_lib.read(out_path)
plt.plot(ilc_data_t[:,0], ilc_data_t[:,3], 'x')
plt.plot(data_new[:,0], data_new[:,3], '-')
```
%% Cell type:code id: tags:
``` python
ilc_lib.external_data_store(ws_path + "/read_error", "read_error.csv", data, header)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment