Skip to content

fix: get data key by id and accept flow references without measurement - #25

Merged
tc-mateus merged 4 commits into
mainfrom
fix/get-data-key-by-id
Nov 19, 2026
Merged

tc-mateus merged 4 commits into
mainfrom
fix/get-data-key-by-id

Conversation

@tc-mateus

Copy link
Copy Markdown
Collaborator

Summary

Two fixes on the read side, found while helping a customer export experiment data.

get_data() drops a variable when two variables share a code

Variable codes are only unique within a variable group, so one experiment can hold a Volume Z variable and a Volume W variable. Experiment.get_data() and Dataset.get_data() key the result by code, so only one of the two can come back. The duplicate check made it worse: it compared each instance's file column with the codes already stored, so a variable whose column name matched another variable's code was skipped even when its own code was unique.

Changes:

  • New key_by argument on Experiment.get_data() and Dataset.get_data(). With key_by="id" the result is keyed by variable id, which is always unique. For spectra, the key is the spectrum variable's id instead of "spectra".
  • key_by="code" stays the default. When codes are shared, it warns and keeps the first variable with each code.
  • Duplicates are detected on the result key, not on the file column.

The default behaviour changes in one case only: a variable whose column matched another variable's code is now returned instead of skipped.

get_variables() fails on flow references without measurementId

measurement_id is now optional when reading. Creating a flow variable through the SDK still requires it: the import validator already rejects references without one, and a test now covers that.

Version

Bumped to 0.3.5.

Testing

  • 6 new unit tests. The four get_data tests and the flow reference parsing test fail on main. The validation test pins existing behaviour.
  • Checked against the dev environment. An experiment with a Z and a W Volume returns both series with key_by="id" (1 and 15 points), and get_variables() now lists all 4,877 variables, 4 of which have a reference without measurementId.
  • pytest, black --check and pylint pass.

Variable codes are only unique within a variable group, so an experiment
can hold e.g. a Z and a W variable both coded "Volume". get_data keyed
the result by code and skipped any variable whose file column matched a
key already stored, so one of the two series was silently dropped.

- Add key_by="id" to Experiment.get_data and Dataset.get_data to key the
  result by variable id, which is always unique.
- Keep key_by="code" as the default and warn when codes are shared.
- Skip duplicates by the result key instead of comparing the file column
  against variable codes.
lab-db stores measurementId on a flow reference as optional, and
references that only point to a feed concentration exist. The SDK
required it, so get_variables() failed with a ValidationError as soon as
it reached such a variable.

Make measurement_id optional when reading variables. Creating a new flow
variable through the SDK still requires it: the import validator already
rejects references without one, and a test now pins that.
@tc-mateus tc-mateus self-assigned this Nov 18, 2026

@fabianbalsiger fabianbalsiger left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two non-blocking suggestions:

1. The shared-code warning repeats per experiment

Dataset.get_data() calls _get_data_inner() once per experiment, and the message contains the experiment name, so Python does not collapse the repeats. A dataset with N experiments prints N warnings about the same variable list, since self.variables is the same for all of them. The warning is also attributed to db_entities.py:933 instead of the caller's line.

Suggestion: do the shared-code check once in Dataset.get_data() and skip it in the per-experiment calls, and pass stacklevel so the warning points at the user's call.

2. key_by="id" also changes the spectra key

In the CSV branch (db_entities.py:968) the result key is "spectra" with key_by="code" and the variable id with key_by="id". A caller who switches to "id" and still reads data["spectra"] gets a KeyError. The PR description says this, but the docstrings and the README do not, and none of the new tests uses a CSV response.

Suggestion: add one line to the key_by docstring in both get_data() methods, and one test with a text/csv response for each mode.

…a key

Review follow-up.

- Dataset.get_data() warned once per experiment about the same shared
  codes, and the warning pointed at the SDK instead of the caller. The
  check now runs once in each public get_data() and passes stacklevel.
- With key_by="id", spectra data is keyed by the spectrum variable id
  instead of "spectra". Say so in both docstrings and the README, and
  cover both modes with a text/csv response.
@tc-mateus
tc-mateus merged commit 1320339 into main Nov 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants