Boolean Masks and Data Selection
Scientific Python in GX-TXT · Module 3 · Lesson 15
A boolean mask associates each element with a True or False decision. We can use it to select values that respect an explicit criterion, while maintaining even those excluded. In this lesson we compare a NumPy mask with an equivalent combination of the GX-TXT public numerical API.
Objectives: to create comparisons element by element; to combine masks with &, | and ~; to use np.isfinite, np.where and counts; to distinguish technical selection from scientific decision.
Prerequisites: Booleans of lesson 7 and Arrays of lessons 13–14.
A comparison item by item
import numpy as np
signal = np.array([1.2, 1.5, 1.3, 1.6])
print(signal >= 1.3)
The comparison produces a boolean array, not a single value: [False, True, True, True]. The mask has the same shape as the signal. signal[mask] selects the values in the true positions; the order remains the original one.
The values of the example are synthetic signalals in mV, not experimental measures. To illustrate the selection, we choose the inclusive window from 1.3 to 1.5 mV: 1.5 and 1.3 mV are selected. The thresholds are didactic, not universal quality criteria.
Combine masks
To combine NumPy array conditions, element operators for & (e), | (o) and ~ (negation):
mask = (signal >= 1.3) & (signal <= 1.5)
The brackets around each comparison are necessary to make explicit the priority of the operators. Python and and or operators require a single truth for each operation and, with arrays containing multiple elements, can generate an ambiguous truth error: they do not carry out the element-by-element comparison.
~mask reverses booleans and identifies the excluded values. np.count_nonzero(mask) counts the real elements. Do not delete the excluded data without keeping them and explaining the criterion: a selection can introduce bias or hide an interesting phenomenon.
Non-finished values and np.where
np.isfinite(signal) is true for finite and false values for NaN and infinite. A combined condition can explicitly exclude unfinished values. The fact that a value is finished does not automatically make it valid for the model or for the tool.
np.where(condizione, valore_se_vero, valore_se_falso) builds an array by selecting element between two compatible alternatives. For example, np.where(mask, signal, np.nan) retains the form and replaces the values excluded with NaN. This is not equivalent to deleting them from the original Dataset: it preserves the source data and documents the transformation. The other np.where(condizione) form returns indexes; pay attention to what form you are using.
The GX-TXT numeric provider
NUMERIC_API_v1/array_mask exposes public logic operations such as logical_and. The bridge receives structured operations that declare kind and value and returns a result with values field. In our example the two 0/1 carriers represent the lower and higher comparisons; the provider combines them with logical_and. The result has the same logical values as the NumPy mask of the example; the bridge shows it as the (4, 1) column, while the initial NumPy array is single-dimensional (4,).
Comparing values and form is important: a form difference can affect indexing or subsequent operations even when the boolean sequence appears equal. The preachers on the availability of the API do not guarantee that each combination of data is allowed: it respects the type and form conditions of the provider.
Full code
import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require
gxtxt_api_require("NUMERIC_API_v1@1")
signal = np.array([1.2, 1.5, 1.3, 1.6])
mask = (signal >= 1.3) & (signal <= 1.5) & np.isfinite(signal)
print("Synthetic signal (mV):", signal)
print("Mask NumPy:", mask)
print("Selezionati:", signal[mask])
print("Accettati:", np.count_nonzero(mask), "esclusi:", np.count_nonzero(~mask))
a = {"kind": "vector", "value": [0, 1, 1, 1]}
b = {"kind": "vector", "value": [1, 1, 1, 0]}
result = gxtxt_api_call("NUMERIC_API_v1", "array_mask", ["logical_and", a, b])
print("Mask provider:", result["values"])
The a and b are explicitly built for this finite series: they are not automatically derived from the signal. The comparison is therefore a demonstration on the case shown; for other data it is necessary to generate the corresponding masks. All four synthetic values are over.

The script compares a NumPy selection with a logical combination of the public provider.
Execution
- Open a Session and Viewers → Python Scripts.
- Import
masks.py, assigns name and Script ID into thesessionbroom, then saves. - Open Run → Open Script Runner…, press Run Job and read the output.
- Check the
[False, True, True, False]mask, the selected[1.5, 1.3]values and the count of two accepted and two excluded. The result of the provider shows the same booleans as column vector.

The synthetic window selects two values; the provider column shows the same logical sequence.
Common error
Writing (signal >= 1.3) and (signal <= 1.5) attempts to use and on arrays and does not produce the desired mask. Use & with each comparison in brackets. If times and signals are separate arrays and corresponding rows per row, apply the same mask to both to maintain alignment.
Exercise
For synthetic values [1.2, 1.5, 1.3, 1.6], build a mask that selects finite values closely greater than 1.3 mV. What values and how many elements do you get?
Worked solution
mask = np.isfinite(signal) & (signal > 1.3)
print(signal[mask])
print(np.count_nonzero(mask))The values are 1.5 and 1.6, for a total of two. 1.3 is excluded because the comparison is strictly greater, not greater or equal.
In the next lesson we will use vector operations and unit conversions.