This page was generated from docs/examples/DataSet/Linking to parent datasets.ipynb. Interactive online version: Binder badge.

Linking to parent datasets

When performing a measurement in QCoDeS, it is possible to annotate the outcome (the dataset) of that measurement as having one or more parent datasets. This is done by adding a link to each parent dataset. This notebook covers the mechanisms to do that by going through a few practical examples.

import os
import datetime

import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt

from qcodes.dataset.measurements import Measurement
from qcodes.dataset.plotting import plot_dataset
from qcodes.dataset.data_set import load_by_run_spec
from qcodes.dataset.sqlite.database import initialise_or_create_database_at
from qcodes.dataset.experiment_container import load_or_create_experiment
now = str(
tutorial_db_path = os.path.join(os.getcwd(), 'linking_datasets_tutorial.db')
load_or_create_experiment('tutorial ' + now, 'no sample')
Upgrading database; v0 -> v1: : 0it [00:00, ?it/s]
Upgrading database; v1 -> v2: 100%|██████████| 1/1 [00:00<00:00, 779.90it/s]
Upgrading database; v2 -> v3: : 0it [00:00, ?it/s]
Upgrading database; v3 -> v4: : 0it [00:00, ?it/s]
Upgrading database; v4 -> v5: 100%|██████████| 1/1 [00:00<00:00, 1092.27it/s]
Upgrading database; v5 -> v6: : 0it [00:00, ?it/s]
Upgrading database; v6 -> v7: 100%|██████████| 1/1 [00:00<00:00, 381.20it/s]
Upgrading database; v7 -> v8: 100%|██████████| 1/1 [00:00<00:00, 609.28it/s]
Upgrading database; v8 -> v9: 100%|██████████| 1/1 [00:00<00:00, 537.94it/s]
tutorial 2022-07-01 14:54:35.708535#no sample#1@/home/runner/work/Qcodes/Qcodes/docs/examples/DataSet/linking_datasets_tutorial.db

Example 1: Measuring and then fitting

Say we measure some raw data and subsequently do a curve fit to those data. We’d like to save the fit as a separate dataset that has a link to the dataset of the original data. This is achieved in two steps.

Step 1: measure raw data

meas = Measurement()
                               label='Time', unit='s',
                               label='Signal', unit='V',

N = 500

with as datasaver:
    time_data = np.linspace(0, 1, N)
    signal_data = np.sin(2*np.pi*time_data) + 0.25*np.random.randn(N)

    datasaver.add_result(('time', time_data), ('signal', signal_data))
dataset = datasaver.dataset
Starting experimental run with id: 1.
cbs, axs = plot_dataset(dataset)

Step 2: Set up a fit “measurement”

We now load the raw data dataset, set up a new measurement for the fit, register the raw data as a parent and save a fit.

As the very first step, we supply a model to fit to.

def fit_func(x, a, b):
    return a*np.sin(2*np.pi*x)+b

Next, we set up the fitting measurement.

raw_data = load_by_run_spec(captured_run_id=dataset.captured_run_id)

meas = Measurement()
                               label='Fit axis', unit='t',
                               label='Fitted curve', unit='V',
                               label='Fitted parameter amplitude',
                               label='Fitted parameter offset',
meas.register_parent(parent=raw_data, link_type="curve fit")
<qcodes.dataset.measurements.Measurement at 0x7fbe60325910>

As we now run the measurement, the parent datasets become available via the datasaver. The datasets appear in the order they were registered.

with as datasaver:
    raw = datasaver.parent_datasets[0]
    xdata = np.ravel(raw.get_parameter_data()['signal']['time'])
    ydata = np.ravel(raw.get_parameter_data()['signal']['signal'])

    popt, pcov = opt.curve_fit(fit_func, xdata, ydata, p0=[1, 1])

    fit_axis = xdata
    fit_curve = fit_func(fit_axis, *popt)

    datasaver.add_result(('fit_axis', fit_axis),
                         ('fit_curve', fit_curve),
                         ('fit_param_a', popt[0]),
                         ('fit_param_b', popt[1]))

fit_data = datasaver.dataset
Starting experimental run with id: 2.
cbs, axs = plot_dataset(fit_data)

And just for completeness, let us plot both datasets on top of each other.

fig, ax = plt.subplots(1)
cbs, axs = plot_dataset(raw_data, axes=ax, label='data')
cbs, axs = plot_dataset(fit_data, axes=ax, label='fit', linewidth=4)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Signal (V)')
<matplotlib.legend.Legend at 0x7fbe6035dcd0>
[ ]: