This page was generated from
docs/examples/DataSet/subscriber json exporter.ipynb.
Interactive online version:
.
View in nbviewer.
Subscriber with JSON exportΒΆ
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.
[1]:
import copy
import logging
from pathlib import Path
import numpy as np
[2]:
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,
)
[3]:
logging.basicConfig(level="INFO")
[4]:
initialise_or_create_database_at(
Path.cwd().parent / "example_output" / "json_export_example.db"
)
exp = load_or_create_experiment("json-export-subscriber-test", "no-sample")
INFO:qcodes.dataset.sqlite.db_upgrades:Commencing database upgrade
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 0 to 1
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 0 -> 1
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 1 to 2
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 1 -> 2
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 2 to 3
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 2 -> 3
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 3 to 4
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 3 -> 4
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 4 to 5
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 4 -> 5
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 5 to 6
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 5 -> 6
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 6 to 7
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 6 -> 7
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 7 to 8
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 7 -> 8
INFO:qcodes.dataset.sqlite.db_upgrades:Starting database upgrade version 8 to 9
INFO:qcodes.dataset.sqlite.db_upgrades:Succesfully performed upgrade 8 -> 9
INFO:qcodes.dataset.experiment_container:creating new experiment in /home/runner/work/Qcodes/Qcodes/docs/examples/example_output/json_export_example.db
[5]:
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:Created run with guid: a91faf5c-0000-0000-0000-0195d2a7001b in database /home/runner/work/Qcodes/Qcodes/docs/examples/example_output/json_export_example.db
INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 1 to 1742995914.7838888
[6]:
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"
[7]:
sub_id = dataSet.subscribe(
export_data_as_json_linear,
min_wait=0,
min_count=20,
state=mystate,
callback_kwargs={"location": "../example_output/example_output1.json"},
)
[8]:
s = dataSet.subscribers[sub_id]
[9]:
mystate
[9]:
{'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'}}}
[10]:
for x in range(100):
y = x
dataSet.add_results([{"x": x, "y": y}])
dataSet.mark_completed()
[11]:
mystate
[11]:
{'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'}}}
[12]:
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
[13]:
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:Created run with guid: c870e17a-0000-0000-0000-0195d2a70097 in database /home/runner/work/Qcodes/Qcodes/docs/examples/example_output/json_export_example.db
INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 2 to 1742995914.908638
[14]:
sub_id = dataSet_hm.subscribe(
export_data_as_json_heatmap,
min_wait=0,
min_count=20,
state=mystate,
callback_kwargs={"location": "../example_output/example_output2.json"},
)
[15]:
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()
[16]:
mystate["json"]
[16]:
{'type': 'heatmap',
'x': {'data': [0, 1, 2, 3, 4],
'name': '',
'full_name': '',
'is_setpoint': True,
'unit': ''},
'y': {'data': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'name': '',
'full_name': '',
'is_setpoint': True,
'unit': ''},
'z': {'data': [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
'name': '',
'full_name': '',
'is_setpoint': False,
'unit': ''}}
[ ]: