This page was generated from docs/examples/DataSet/subscriber json exporter.ipynb. Interactive online version: Binder badge.

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 logging
import copy
import numpy as np
import json
[2]:
from qcodes import load_or_create_experiment, new_data_set, ParamSpec
from qcodes.dataset.json_exporter import \
    json_template_heatmap, json_template_linear, \
    export_data_as_json_heatmap, export_data_as_json_linear
[3]:
logging.basicConfig(level="INFO")
[4]:
exp = load_or_create_experiment('json-export-subscriber-test', 'no-sample')
INFO:qcodes.dataset.experiment_container:creating new experiment in /home/runner/experiments.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:Set the run_timestamp of run_id 58 to 1656934586.1189632
[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': 'foo'})
[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=np.object)
mystate['data']['x'][:] = None
mystate['data']['y'] = np.zeros((xlen*ylen), dtype=np.object)
mystate['data']['y'][:] = None
mystate['data']['z'] = np.zeros((xlen*ylen), dtype=np.object)
mystate['data']['z'][:] = None
mystate['data']['location'] = 0
/tmp/ipykernel_7985/797230066.py:8: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  mystate['data']['x'] = np.zeros((xlen*ylen), dtype=np.object)
/tmp/ipykernel_7985/797230066.py:10: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  mystate['data']['y'] = np.zeros((xlen*ylen), dtype=np.object)
/tmp/ipykernel_7985/797230066.py:12: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  mystate['data']['z'] = np.zeros((xlen*ylen), dtype=np.object)
[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:Set the run_timestamp of run_id 59 to 1656934586.2712264
[14]:
sub_id = dataSet_hm.subscribe(export_data_as_json_heatmap, min_wait=0, min_count=20,
                              state=mystate, callback_kwargs={'location': './foo'})
[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, 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': ''}}
[ ]: