Repository navigation
fix: get data key by id and accept flow references without measurement - #25
Conversation
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.
fabianbalsiger
left a comment
There was a problem hiding this comment.
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.
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 codeVariable codes are only unique within a variable group, so one experiment can hold a
VolumeZ variable and aVolumeW variable.Experiment.get_data()andDataset.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:
key_byargument onExperiment.get_data()andDataset.get_data(). Withkey_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.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 withoutmeasurementIdmeasurement_idis 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
get_datatests and the flow reference parsing test fail onmain. The validation test pins existing behaviour.Volumereturns both series withkey_by="id"(1 and 15 points), andget_variables()now lists all 4,877 variables, 4 of which have a reference withoutmeasurementId.pytest,black --checkandpylintpass.