NOTE: this is an outdated notebook, some of the functions that are used here are considered private to QCoDeS and are not intended for use by users (for example, DataSet.subscribe
). This notebook will be re-written in the future.
import copy
import logging
from pathlib import Path
import numpy as np
from qcodes.dataset import (
ParamSpec,
initialise_or_create_database_at,
load_or_create_experiment,
new_data_set,
)
from qcodes.dataset.json_exporter import (
export_data_as_json_heatmap,
export_data_as_json_linear,
json_template_heatmap,
json_template_linear,
)
logging.basicConfig(level="INFO")
initialise_or_create_database_at(Path.cwd() / "json_export_example.db")
exp = load_or_create_experiment('json-export-subscriber-test', 'no-sample')
dataSet = new_data_set("test",
exp_id=exp.exp_id,
specs=[ParamSpec("x", "numeric"), ParamSpec("y", "numeric")])
dataSet.mark_started()
INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 109 to 1561115223.856142
mystate = {}
mystate['json'] = copy.deepcopy(json_template_linear)
mystate['json']['x']['name'] = 'xname'
mystate['json']['x']['unit'] = 'xunit'
mystate['json']['x']['full_name'] = 'xfullname'
mystate['json']['y']['name'] = 'yname'
mystate['json']['y']['unit'] = 'yunit'
mystate['json']['y']['full_name'] = 'yfullname'
sub_id = dataSet.subscribe(export_data_as_json_linear, min_wait=0, min_count=20,
state=mystate, callback_kwargs={'location': 'foo'})
s = dataSet.subscribers[sub_id]
mystate
{'json': {'type': 'linear', 'x': {'data': [], 'name': 'xname', 'full_name': 'xfullname', 'is_setpoint': True, 'unit': 'xunit'}, 'y': {'data': [], 'name': 'yname', 'full_name': 'yfullname', 'is_setpoint': False, 'unit': 'yunit'}}}
for x in range(100):
y = x
dataSet.add_results([{"x":x, "y":y}])
dataSet.mark_completed()
mystate
{'json': {'type': 'linear', 'x': {'data': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 'name': 'xname', 'full_name': 'xfullname', 'is_setpoint': True, 'unit': 'xunit'}, 'y': {'data': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 'name': 'yname', 'full_name': 'yfullname', 'is_setpoint': False, 'unit': 'yunit'}}}
mystate = {}
xlen = 5
ylen = 10
mystate['json'] = json_template_heatmap.copy()
mystate['data'] = {}
mystate['data']['xlen'] = xlen
mystate['data']['ylen'] = ylen
mystate['data']['x'] = np.zeros((xlen*ylen), dtype=object)
mystate['data']['x'][:] = None
mystate['data']['y'] = np.zeros((xlen*ylen), dtype=object)
mystate['data']['y'][:] = None
mystate['data']['z'] = np.zeros((xlen*ylen), dtype=object)
mystate['data']['z'][:] = None
mystate['data']['location'] = 0
dataSet_hm = new_data_set("test", exp_id=exp.exp_id,
specs=[ParamSpec("x", "numeric"),
ParamSpec("y", "numeric"),
ParamSpec("z", "numeric")])
dataSet_hm.mark_started()
INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 110 to 1561115225.747753
sub_id = dataSet_hm.subscribe(export_data_as_json_heatmap, min_wait=0, min_count=20,
state=mystate, callback_kwargs={'location': './foo'})
for x in range(xlen):
for y in range(ylen):
z = x+y
dataSet_hm.add_results([{"x":x, "y":y, 'z':z}])
dataSet_hm.mark_completed()
mystate['json']
{'type': 'heatmap', 'x': {'data': [0, 1, 2, 3, 4], 'name': '', 'full_name': '', 'is_setpoint': True, 'unit': ''}, 'y': {'data': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'name': '', 'full_name': '', 'is_setpoint': True, 'unit': ''}, 'z': {'data': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]], 'name': '', 'full_name': '', 'is_setpoint': False, 'unit': ''}}