Dataset Generation and Native Publication

by Giux GX-TXT, python-en, scripts-en 3 min read

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.Generator with 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.

Data generation script in the GX-TXT editor.
Data generation script in the GX-TXT editor.
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.

Two synthetic Datasets read back with 21 rows each.
Two synthetic Datasets read back with 21 rows each.

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

  1. What variables contain the ideal and noisy signal?
  2. What is the sigma unit?
  3. Why does the note say that the data do not represent measurements?

Worked solution

  1. ideal and signal, respectively.
  2. mV, the same as the signal.
  3. Because they were generated by a model and pseudorandom noise, not acquired in the laboratory.

Download the Module 10 scripts (ZIP)

#GX-TXT #Python #Reproducibility

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *