Skip to content
Snippets Groups Projects
Commit 4704a948 authored by Stephan Philips's avatar Stephan Philips
Browse files

writing class for calibration data mgnt

parent 26d376f6
No related branches found
No related tags found
No related merge requests found
......@@ -131,3 +131,4 @@ dmypy.json
# neglect db files
.db
.sqlite
import sqlite3
import time
class data_mgr():
def __init__(self, cls_object, db_location):
......@@ -11,9 +12,10 @@ class data_mgr():
self.filename_db = db_location + '.sqlite'
self.cal_object = cls_object
self.table_name = cls_object.__class__.__name__
self.__update_table()
self.current_line = None
def __connect(self):
db = sqlite3.connect(self.filename_db)
cursor = db.cursor()
......@@ -55,16 +57,17 @@ class data_mgr():
NOTE: that existing fields will not be updated. Use stash_table to rename it and create a new one if you want to do that.
'''
generate_table = "CREATE TABLE IF NOT EXISTS {} (\
id data_type PRIMARY KEY,\
meas_time TEXT not NULL)".format(self.table_name)
id INTEGER PRIMARY KEY AUTOINCREMENT,\
start_time DOUBLE not NULL,\
end_time DOUBLE,\
success BOOLEAN not NULL)".format(self.table_name)
self.__exec_command(generate_table)
# Get current columns in the data base:
db_paramters = self.cal_object.set_vals.get_db_column_names() + self.cal_object.get_vals.get_db_column_names()
db_column_info = self.__query_db("PRAGMA table_info('%s')"%self.table_name)
db_column_names= [db_column_name[1].lower() for db_column_name in db_column_info]
column_to_add = [param_name for param_name in db_paramters if param_name[0].lower() not in db_column_names]
column_to_add = [param_name for param_name in db_paramters if param_name.lower() not in db_column_names]
for param in column_to_add:
self.__exec_command("ALTER TABLE {} ADD COLUMN {} {}".format(self.table_name, param, 'DOUBLE'))
......@@ -84,94 +87,51 @@ class data_mgr():
'''
self.__exec_command("DROP TABLE %s"% self.table_name)
def get_all_parameter_names(self):
def __start_data_entry(self):
'''
Get all the parameters that are currently in use. Note that SQLite is case insensitive.
start new line in the table where data can be saved.
'''
db, cursor = self.__connect()
cursor.execute("PRAGMA table_info('%s')"%self.table_name)
db_column_info = cursor.fetchall()
db_column_names= [i[1].lower() for i in db_column_info]
db.close()
return db_column_names
cmd = 'INSERT INTO {} (start_time, success) values ({}, TRUE)'.format(self.table_name, time.time())
self.__exec_command(cmd)
self.current_line = self.__query_db('SELECT id from {} order by ROWID DESC limit 1'.format(self.table_name))[0][0]
def save_calib_results(self, data_tuple):
'''
saves the data_tuple to database, if you give a variable that not exist, this will will be discarded
TODO tell when people feed garbage.
def write(self, parameter, value):
'''
Write data to the table
Args:
data_tuple list<tuple<str, any>: input data for one row [(var_name, value)]
'''
if type(data_tuple) != type(list()):
data_tuple = [data_tuple]
fields = self.get_all_parameter_names()
to_upload = []
for i in fields:
var_found = False
if i == 'time':
to_upload.append(time.time())
continue
if i == 'time_human_readable':
to_upload.append(datetime.now().strftime("'%Y/%m/%d-%H:%M:%S'"))
continue
for j in data_tuple:
if i == j[0].lower():
to_upload.append(j[1])
var_found = True
break
if var_found==False:
to_upload.append('null')
cmd = 'INSERT INTO %s VALUES ('%self.table_name
for i in to_upload:
cmd += str(i) + ','
cmd = cmd[:-1]
cmd += ')'
parameter (qc.Paramter/str) : parameter to be written
value (float) : value to write.
'''
if self.current_line is None:
self.__start_data_entry()
cmd = 'UPDATE {}\
SET {} = {} \
WHERE id = {};'. format(self.table_name, str(parameter), value, self.current_line)
self.__exec_command(cmd)
def get_parameter_latest(self, params, side_condition=None):
'''
returns array with wanted params, if no params given, all parameters of the last calibration will be returned
params = string or array of strings containing the wanted parameter.
side_condition = tuple of values that should be set to a certain value (e.g)
return format:
list of dictionaries with field name, data and unit
Returns:
Format of param input, return None if not found.
'''
input_is_str= False
# safe typing
if type(params) != list:
params = [params]
input_is_str = True
# Construction of query
cmd = 'SELECT MAX(time), '
# param to select
for i in params:
cmd += i + ','
cmd = cmd[:-1] + ' FROM %s '%self.table_name
cmd += 'WHERE '
for i in params:
cmd += '%s IS NOT NULL AND '%i
if len(side_condition) != 0:
for i in side_condition:
if str(i[1]).lower() == 'null':
cmd += '%s IS %s and '%(i[0], i[1])
else:
cmd += '%s = %s and '%(i[0], i[1])
cmd = cmd[:-4]
if input_is_str:
return self.__query_db(cmd)[0][1:][0]
else:
return list(self.__query_db(cmd)[0][1:])
def finish_data_entry(self, status):
'''
finish the current entry. Report status of the calibration
Args:
status (bool) : True = success, False = fail
'''
self.write('end_time', time.time())
self.write('success', status)
self.current_line = None
def empty(self):
'''
checks if the current db is empty
'''
pass
def query(self):
# --> make into object.
pass
if __name__ == '__main__':
......@@ -187,5 +147,14 @@ if __name__ == '__main__':
self.set_vals = value_mgr('frequency', 'amplitude')
self.get_vals = value_mgr('omega_qubit_1')
d = data_mgr(test_cal(), 'test/my_test_db')
print(d.get_all_parameter_names())
\ No newline at end of file
d = data_mgr(test_cal(), 'my_test_db')
# d.write('frequency', 1e6)
# d.write('frequency', 1e9) # overwrite a value ..
# d.write('amplitude', 0.1)
# d.finish_data_entry(True)
# d.write('frequency', 1e6)
# d.write('omega_qubit_1', 1.23e2)
# d.write('amplitude', 0.201)
# d.finish_data_entry(False)
'''
parameter class for calibration object. Similar to qcodes paramters, but with some extra quirks.
'''
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment