vfbquery.combine¶
The set-algebra engine behind /combine.
This module is worth documenting on its own because of what it deliberately does not contain. Everything decidable without the network lives here — tokenising and parsing the expression, precedence and brackets, rendering it back as English, choosing the identity column, diagnosing namespace mismatches, the universe and its degeneracies, and the lossless row and header merge. It imports nothing outside the standard library.
The part that needs a server — turning NeuronsPartHere:FBbt_00007401 into an actual query on the
process pool, under the cache and the coalescer — lives in vfbquery.ha_api and is the thin half.
That split is why the expression tests run in well under a second, why a bracketing bug can be reproduced without a Neo4j, and why a client can validate an expression before spending a request on it.
Errors¶
- class vfbquery.combine.CombineError[source]¶
Bases:
ValueErrorA user-facing problem with the expression or the operands.
Distinct from an internal failure: every raise site here produces a message that a workshop attendee can act on without reading this file.
Parsing an expression¶
- vfbquery.combine.tokenize(expr)[source]¶
Expression text -> a list of
('name'|'op'|'(' |')', value)tuples.Phrase aliases are substituted first, on the lower-cased text, so that “but not” becomes a single NOT before word-splitting can turn it into two tokens. The substitution is done on word boundaries to keep a phrase from matching inside an operand name.
- vfbquery.combine.parse(expr, known_names=None)[source]¶
Parse expr into a tree. Raises CombineError with a usable message.
- vfbquery.combine.to_expression(node)[source]¶
Render the parsed tree back to canonical, fully-bracketed text.
Echoing this back is how a user checks that the brackets were read the way they meant them — a OR b AND c is not (a OR b) AND c, and the cheapest way to prevent that misunderstanding is to show the grouping that was used.
- vfbquery.combine.describe(node, operands)[source]¶
A short phrase naming a subtree, for use inside explanations.
- vfbquery.combine.plain_english(node, operands)[source]¶
The whole expression as one sentence a non-logician can check.
Nodes¶
Identifying rows¶
- vfbquery.combine.id_column(headers, rows=())[source]¶
Return the name of the column holding the row’s term ID.
The query schemas declare this: every result carries a headers map, and the identity column is the one whose declared type is selection_id — the same field the website uses to know what a click selected. That is a fact about the data rather than a guess, which matters because the alternative (assuming id) silently picks the wrong column the first time a query type names it something else.
Falls back to a small list of conventional names, then fails loudly. It never guesses from the values: an ID-shaped string in a parent or template column would combine two queries on the wrong axis entirely and return a plausible, wrong answer.
Operands and the universe¶
- class vfbquery.combine.OperandResult(name, payload, spec=None, description=None)[source]¶
Bases:
objectOne query’s rows, indexed by term ID and de-duplicated.
Two properties of the upstream data make this more than a dict comprehension:
Duplicate IDs. DownstreamClassConnectivity on a class with subclasses runs once per subclass and concatenates, so a Kenyon cell query returns 8,935 rows covering 894 distinct terms. Counting rows instead of terms would overstate every set size by an order of magnitude, so rows are folded per ID — losslessly, see merge_rows.
Truncation. The result carries a count that is the true size of the answer, and a rows list that the server may have capped (a dataset image query has been observed reporting count: 60002 while returning 25,000 rows). Set algebra over a truncated operand is not approximately right, it is confidently wrong: an intersection silently loses members and a difference silently gains them. It is recorded here and refused or warned about upstream — never ignored.
- property ids¶
- property duplicate_rows¶
Rows folded away because several described the same term.
- property truncated¶
True when the server returned fewer rows than the answer contains.
Compared against distinct IDs as well as raw rows: a query that reports 894 and returns 8,935 rows for 894 terms is complete, not truncated.
Evaluating¶
Merging results without losing columns¶
- vfbquery.combine.merge_rows(identifier, contributions, operand_order)[source]¶
Fold every row describing one term into a single row, losing nothing.
contributions maps operand name -> the list of rows that operand returned for this ID. A term can appear in several operands, and each operand may have returned several rows for it, and the tables need not agree: one query calls the label column label and another calls it name; one carries score, another carries outputs/inputs.
The rule is: take the union of all columns. Where every source that has a column agrees on its value, emit it once under its own name. Where they disagree, emit the first value (in expression order) under the plain name and every distinct value under column__operand, so the disagreement is visible and nothing is dropped. A user who does not care sees a normal table; a user who does can recover exactly which query said what.
This is why the endpoint does not simply return IDs: a set operation over query results should return the merged evidence, not send the caller back to re-fetch it.
- vfbquery.combine.merge_headers(operands, operand_order, merged_rows)[source]¶
Header map for the merged table: every operand’s headers, plus ours.
Columns invented by the merge (label__a, label__b) inherit the type of the column they came from, so a client that renders by declared type — the website does — keeps rendering them correctly instead of falling back to plain text.
- vfbquery.combine.build_rows(result_ids, operands, operand_order)[source]¶
Merged output rows for result_ids, in a stable, useful order.
Ordering is by how many operands found the term (descending) and then by label, so the terms supported by the most evidence are at the top — which is what a researcher scanning an intersection wants, and is stable across calls, which matters for anything diffing results between releases.
There is deliberately no order_by argument. Caller-chosen ordering is a real request, but it belongs to a column set that only exists after the merge (found_in, column__operand), so it needs a name-resolution rule of its own rather than a parameter threaded through here. order_by is reserved in the endpoint’s query string so adding it later cannot collide with an operand of that name — see _COMBINE_RESERVED in ha_api.