Progressive Reading and Row Handling
Scientific Python in GX-TXT · Module 4 · Lesson 21
When a Dataset contains multiple related variables, read all the necessary columns in the same range helps keep the alignment. This lesson resumes reading for blocks and organizes it in a reusable function.
Objectives: read two variables by intervals; check the lengths; recompose the blocks in the correct order; check the initial and final pairs and a numerical property.
Prerequisites: functions, reading for blocks, arrays and Dataset schema of lessons 10–20.
Rows and variables
In the Synthetic Dataset corso_python_serie_sintetica, time_values is expressed in seconds and signal in mlivolt. The four pairs are (0.0, 1.2), (0.5, 1.5), (1.0, 1.3) and (1.5, 1.6). These are invented didactic data, not experimental measures; there is no uncertainty.
The two variables share the line-by-line relationship: the first time belongs to the first signal and so on. If we read different intervals for the two variables, we can get arrays of the same length but with wrong pairs. Asking both IDs in the same call to read_range makes the common range explicit.
Function for a block
The read_chunk(dataset, start, requested_count) function receives the Dataset ID, an initial position from scratch and the number of rows. Use CANONICAL_DATASET_API_v2/read_range with time_values and signal IDs, convert values to NumPy arrays, and return the pair of arrays.
In the format observed, each variable comes as a column. reshape(-1) produces a single-dimensional vector: here it is appropriate because we know that each variable is a single column. For data with multiple dimensions do not flatten without checking the meaning of the axes.
The function compares the lengths and lifts ValueError if they are different. This control is useful but not enough to guarantee scientific alignment: it is also necessary that the values come from the same range and from really related variables.
Recompose without jumps
We read two rows from 0 and two starting from 2. Blocks cover 0–1 and 2–3 respectively. np.concatenate((first_t, last_t)) combines time blocks in the same order; we do the same for signals. The result contains four rows, with first pair (0.0 s, 1.2 mV) and last (1.5 s, 1.6 mV).
The average of the four signals is (1.2 + 1.5 + 1.3 + 1.6) / 4 = 1.4 mV, consistent with lesson 17. This average checks a simple summary, but alone does not prove that the order of the rows is correct: we also check the pairs at the extremes and, when necessary, each row.
Full code
import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require
def read_chunk(dataset, start, requested_count):
response = gxtxt_api_call("CANONICAL_DATASET_API_v2", "read_range", [dataset, ["tempo", "segnale"], start, requested_count])
time_values = np.asarray(response["tempo"]["values"]).reshape(-1)
signal = np.asarray(response["segnale"]["values"]).reshape(-1)
if len(time_values) != len(signal):
raise ValueError("Variables have different lengths")
return time_values, signal
gxtxt_api_require("CANONICAL_DATASET_API_v2@2")
dataset = "corso_python_serie_sintetica"
first_t, first_y = read_chunk(dataset, 0, 2)
last_t, last_y = read_chunk(dataset, 2, 2)
times = np.concatenate((first_t, last_t))
signals = np.concatenate((first_y, last_y))
print("Tempo (s):", times)
print("Segnale (mV):", signals)
print("Righe:", len(times), "first couple:", times[0], signals[0])
print("Last couple:", times[-1], signals[-1])
print("Media signal (mV):", np.mean(signals))
return time_values, signal returns a tupla; the first_t, first_y = ... assignment separates the two values by unpacking. The example presupposes exactly four lines available. For a generic dataset consult describe, manage any shorter intervals and the empty case before accessing [0] or [-1].

The function asks the two variables for the same range.
Execution and control
- Open the Session that contains the Dataset of lesson 9 and the Viewer Python Scripts.
- Import
progressive_reading.py, assigns name and Script ID into thesessionbroom, then saves. - Open the Runner, press Run Job and watch the output.
- Compare the four times and signals with the original series. Check the extremes and the
1.4 mVaverage.

The two blocks recompose four couples in the expected order.
Memory and number of calls
Requesting more variables related in a call can reduce the number of requests to bridge. Too small blocks increase overhead; too large blocks require more memory. The appropriate size depends on the Dataset and the environment.
This example concatenates all data, so the overall memory grows with the series. For large datasets, if the calculation allows it, it processes each block and retains only the necessary accumulators. Evaluate missing data, unit and numerical accuracy separately.
Common error
Two blocks starting at 0 and 1 with size 2 cover 0–1 and 1–2: row 1 is duplicated and row 3 is missing. The sum of the required size is four, but it does not show that four distinct lines have been read. Check intervals, contiguity and values at the extremes.
Exercise
For five rows, choose two contiguous blocks without overlaps: the first of three rows, the second with the remaining. What start and requested_count topics do you use?
Worked solution
The first block is (start=0, requested_count=3) and covers 0–2. The second is (start=3, requested_count=2) and covers 3–4. Before calling the API, verify that the Dataset really contains five lines and that the required IDs are correct.
In the next lesson, we will deal with multidimensional Dataset and hyperslab subsets.