Repository navigation
Follow-up column analysis fixes #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JGagacheva
wants to merge
2
commits into
main
Choose a base branch
from
jg-fixes-ca
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -595,6 +595,9 @@ def _answer_in_parts( | |
| # of a forecast is an ordinary regression, so the caller's fields are | ||
| # what each part is asked for. | ||
| wanted = _RANKING_PARTS if ranking else outputs | ||
| if request.parts: | ||
| # Refused before the analysis below, not after it. | ||
| self._prepare(request.parts[0], model, wanted) | ||
| # A forecast's analysis reads every step's labelled rows at once. | ||
| columns = ( | ||
| _analysed_or_warned( | ||
|
|
@@ -1051,23 +1054,26 @@ class _LabelledRows: | |
| r"""The labelled rows a request's column analysis reads.""" | ||
|
|
||
| _INSTANCE_ID = 'instance_id' | ||
| _ENTITY_REF = '__kumo_entity_ref' | ||
|
|
||
| def __init__(self) -> None: | ||
| # Each step's instance ids, renumbered so no two steps share one. | ||
| self._ids: dict[tuple[int, Any], int] = {} | ||
|
|
||
| @staticmethod | ||
| def of(request: Request, planned: Mapping[str, Any]) -> dict[str, Any]: | ||
| r"""``planned``'s task and schema over the request's labelled context. | ||
| @classmethod | ||
| def of(cls, request: Request, planned: Mapping[str, Any]) -> dict[str, Any]: | ||
| r"""``planned``'s task over the labelled rows the request sends. | ||
|
|
||
| A gathered request carries its context as it was before the rows | ||
| answering the scored ones were dropped; any other request's planned | ||
| context is that context. | ||
| A request built from frames has no payloads; its planned context is | ||
| what it sends. | ||
| """ | ||
| labelled = request._labelled_context | ||
| payloads = request.payloads | ||
| if not payloads: | ||
| return {**planned} | ||
| return { | ||
| **planned, | ||
| 'context': planned['context'] if labelled is None else labelled, | ||
| 'schema': cls._schema([payload['schema'] for payload in payloads]), | ||
| 'context': cls._sent(payloads), | ||
| } | ||
|
|
||
| def across(self, steps: Sequence[Request]) -> dict[str, Any]: | ||
|
|
@@ -1105,25 +1111,84 @@ def across(self, steps: Sequence[Request]) -> dict[str, Any]: | |
| ) | ||
| for name in names | ||
| } | ||
| # A step's schema describes only the tables that step reached. | ||
| schemas = [payload['schema'] for payload in each] | ||
| return { | ||
| **each[0], | ||
| 'schema': self._schema([payload['schema'] for payload in each]), | ||
| 'context': context, | ||
| } | ||
|
|
||
| @classmethod | ||
| def _sent(cls, payloads: Sequence[Mapping[str, Any]]) -> dict[str, Any]: | ||
| r"""The payloads' labelled contexts as one, each instance once. | ||
|
|
||
| Every payload carries the same labelled draw, less its own scored | ||
| entities, under the same instance ids. An instance is keyed by entity | ||
| and anchor; the first payload sending it gives its row and its | ||
| related rows. | ||
| """ | ||
| seen: set[tuple[Any, Any]] = set() | ||
| instances: list[Any] = [] | ||
| related: dict[str, dict[str, Any]] = {} | ||
| for payload in payloads: | ||
| context = payload['context'] | ||
| table = context['instance_table'] | ||
| columns = list(table['columns']) | ||
| at = columns.index(cls._INSTANCE_ID) | ||
| entity = next( | ||
| index | ||
| for index, name in enumerate(columns) | ||
| if str(name).startswith(cls._ENTITY_REF) | ||
| ) | ||
| anchor_column = payload['task'].get('anchor_time_column') | ||
| anchor = ( | ||
| columns.index(anchor_column) | ||
| if anchor_column in columns | ||
| else None | ||
| ) | ||
| keys: set[tuple[Any, Any]] = set() | ||
| kept: set[Any] = set() | ||
| for row in table['rows']: | ||
| key = (row[entity], None if anchor is None else row[anchor]) | ||
| keys.add(key) | ||
| if key not in seen: | ||
| kept.add(row[at]) | ||
| instances.append(row) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| seen |= keys | ||
| for name, rows in (context.get('related_tables') or {}).items(): | ||
| held = related.setdefault(name, {**rows, 'rows': []}) | ||
| where = list(rows['columns']).index(cls._INSTANCE_ID) | ||
| held['rows'].extend( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Batches can send the same related table with different columns (a later batch reaching another relationship adds one), and this appends later rows under the first batch's columns, so the analysis fails and is dropped; please align rows by column name. |
||
| row for row in rows['rows'] if row[where] in kept | ||
| ) | ||
| first = payloads[0]['context'] | ||
| sent: dict[str, Any] = { | ||
| 'instance_table': {**first['instance_table'], 'rows': instances} | ||
| } | ||
| if any('related_tables' in payload['context'] for payload in payloads): | ||
| sent['related_tables'] = related | ||
| return sent | ||
|
|
||
| @staticmethod | ||
| def _schema(schemas: Sequence[Mapping[str, Any]]) -> dict[str, Any]: | ||
| r"""One schema describing every table any of ``schemas`` reached. | ||
|
|
||
| A payload's schema describes only the tables that payload reached. | ||
| """ | ||
| schema = dict(schemas[0]) | ||
| if any('related_tables' in step_schema for step_schema in schemas): | ||
| if any('related_tables' in each for each in schemas): | ||
| described: dict[str, Any] = {} | ||
| for step_schema in schemas: | ||
| for name, table in ( | ||
| step_schema.get('related_tables') or {} | ||
| ).items(): | ||
| for each in schemas: | ||
| for name, table in (each.get('related_tables') or {}).items(): | ||
| described.setdefault(name, table) | ||
| schema['related_tables'] = described | ||
| if any('relationships' in step_schema for step_schema in schemas): | ||
| if any('relationships' in each for each in schemas): | ||
| links: list[Any] = [] | ||
| for step_schema in schemas: | ||
| for link in step_schema.get('relationships') or []: | ||
| for each in schemas: | ||
| for link in each.get('relationships') or []: | ||
| if link not in links: | ||
| links.append(link) | ||
| schema['relationships'] = links | ||
| return {**each[0], 'schema': schema, 'context': context} | ||
| return schema | ||
|
|
||
| def _stacked( | ||
| self, tables: Sequence[tuple[int, Mapping[str, Any]]] | ||
|
|
@@ -1531,10 +1596,21 @@ def _by_step( | |
| ), | ||
| columns, | ||
| ) | ||
| # Each step is its own call, and only the first step's id would otherwise | ||
| # survive. That is the one you do not have when a later step is the one | ||
| # that misbehaved. A step sent in several calls keeps every one of them. | ||
| metadata = { | ||
| 'steps': [step for _, step in rewritten.parts], | ||
| 'step_request_ids': [ | ||
| list(answer.metadata.get('batch_request_ids', [answer.request_id])) | ||
| for answer in answers | ||
| ], | ||
| } | ||
| built = PredictionResult( | ||
| model=answers[0].model if answers else '', | ||
| request_id=answers[0].request_id if answers else '', | ||
| items=items, | ||
| metadata=metadata, | ||
| outputs=tuple(answers[0].outputs) if answers else (), | ||
| explanations=explanations, | ||
| ) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.