aboutsummaryrefslogtreecommitdiff
path: root/qolab/data/trace.py
diff options
context:
space:
mode:
Diffstat (limited to 'qolab/data/trace.py')
-rw-r--r--qolab/data/trace.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/qolab/data/trace.py b/qolab/data/trace.py
index 5fbaaf9..555e5dc 100644
--- a/qolab/data/trace.py
+++ b/qolab/data/trace.py
@@ -1,5 +1,6 @@
from qolab.file_utils import save_table_with_header
import datetime
+import csv
import numpy as np
import yaml
@@ -14,6 +15,50 @@ def headerFromDictionary(d, prefix=''):
def from_timestamps_to_dates(timestamps):
dates = [datetime.datetime.fromtimestamp(float(ts)) for ts in timestamps]
return(dates)
+
+def loadTraceRawHeaderAndData(fname):
+ headerstr=[]
+ data = None
+ with open(fname) as csvfile:
+ rowreader = csv.reader(csvfile, delimiter='\t')
+ for row in rowreader:
+ if row[0][0:2]=='% ':
+ headerstr.append(row[0][2:])
+ else:
+ rdata = np.array(row, dtype=float)
+ if data is None:
+ data=rdata
+ else:
+ data=np.vstack((data,rdata))
+ header=yaml.load(str.join('\n', headerstr), Loader=yaml.SafeLoader)
+ return(header, data)
+
+def loadTrace(fname):
+ (header, data) = loadTraceRawHeaderAndData(fname)
+ label = None
+ model = None
+ tr = None
+ if 'config' not in header:
+ print('Error: trace has now config')
+ return None
+ else:
+ if 'label' in header['config']:
+ label = header['config']['label']
+ if 'model' not in header['config']:
+ print('Error: unknown trace model')
+ return None
+ else:
+ model = header['config']['model']
+ if model == 'Trace':
+ tr = Trace(label)
+ tr.values = data
+ elif 'model' == 'TraceXY':
+ tr = TraceXY(label)
+ elif 'model' == 'TraceSetSameX':
+ tr = TraceSetSameX(label)
+ tr.config = header['config']
+
+ return tr
class Trace:
def __init__(self, label):