Dataset Generation and Native Publication
Scientific Python in GX-TXT · Module 10 · Lesson 56
We generate the two series provided by the protocol and publish them in the Session with ideal signal, noisy synthetic signal and explicit standard uncertainty.
Objectives
- Use
numpy.random.Generatorwith declared seed. - Separate ideal curve and synthetic noise.
- Publish variables and units with the canonical writer.
- Reread Dataset and associate source notes.
Execution
Import generate_data.py into the corso_python Session, save it and run it in the Runner. The public script corso_finale_a and corso_finale_b; replace_owned applies only to outputs that the script is allowed to replace.

import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require
for api in ("DATASET_PUBLICATION_API_v2@2", "DATASET_NOTES_API_v1@1", "CANONICAL_DATASET_API_v2@2"):
gxtxt_api_require(api)
rng = np.random.default_rng(2026)
time_values = np.linspace(0., 5., 21)
for display_name, k in (("A", 0.5), ("B", 0.8)):
dataset = "corso_finale_" + display_name.lower()
ideal = 3.0 * np.exp(-k * time_values)
observed = ideal + rng.normal(0., 0.05, time_values.size)
sigma = np.full(time_values.size, 0.05)
request = {"dataset_id": dataset, "contract": "table.generic", "storage": "hdf5", "replace_owned": True, "provenance": "Synthetic simulation: seed 2026"}
writer = gxtxt_api_call("DATASET_PUBLICATION_API_v2", "begin", [request])
try:
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "declare_dimension", [writer, "campione", 21, {"extendible": 0}])
for variable, values, unit, role in (("tempo", time_values, "s", "coordinate"), ("segnale", observed, "mV", "data"), ("sigma", sigma, "mV", "data"), ("ideale", ideal, "mV", "data")):
schema = {"dtype": "float64", "rank": 1, "dimension_ids": "campione", "unit": unit, "coordinate_role": role}
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "declare_variable", [writer, variable, schema])
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "write_variable", [writer, variable, values, [21]])
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "finalize", [writer])
except Exception:
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "abort", [writer])
raise
note = f"♪ Synthetic study {display_name}\n\n They are not measures. A=3 mV; k={k} s^-1; sigma=0.05 mV; seed=2026.\n"
gxtxt_api_call("DATASET_NOTES_API_v1", "write", [dataset, note])
read_result = gxtxt_api_call("CANONICAL_DATASET_API_v2", "read_range", [dataset, ["tempo", "segnale", "sigma", "ideale"], 0, 21])
print(dataset, "righe:", len(read_result["tempo"]["values"]), "sigma (mV):", sigma[0])
print("Generation complete: synthetic data, not measurements")
For each condition, the generator builds the ideal 3 exp(-k t) and adds normal noise with standard deviation 0.05 mV. The sigma variable retains this value for each row; ideal remains separated from the noisy signal. The writer declares a size of 21 samples and four variables with units; finalize publishes the full Dataset, while abort manages an exception before finalization.

The note associated with each Dataset records model, parameters, seed and synthetic nature. The output confirms the reading of 21 rows for both conditions. The seed and the order of calls to the generator are part of the reproducible procedure; changing the order changes the sequence assigned to the conditions.
Exercise
- What variables contain the ideal and noisy signal?
- What is the
sigmaunit? - Why does the note say that the data do not represent measurements?
Worked solution
idealandsignal, respectively.- mV, the same as the signal.
- Because they were generated by a model and pseudorandom noise, not acquired in the laboratory.