-
Notifications
You must be signed in to change notification settings - Fork 11
[Validate] Track-level metrics upload #375
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
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
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
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 |
|---|---|---|
|
|
@@ -8,9 +8,16 @@ | |
| from typing import List, Optional, Union | ||
|
|
||
| from ..connection import Connection | ||
| from ..constants import DATASET_ITEMS_KEY, NAME_KEY, SCENES_KEY, SLICE_ID_KEY | ||
| from ..constants import ( | ||
| DATASET_ITEMS_KEY, | ||
| NAME_KEY, | ||
| SCENES_KEY, | ||
| SLICE_ID_KEY, | ||
| TRACKS_KEY, | ||
| ) | ||
| from ..dataset_item import DatasetItem | ||
| from ..scene import Scene | ||
| from ..track import Track | ||
| from .constants import ( | ||
| EVAL_FUNCTION_ID_KEY, | ||
| SCENARIO_TEST_ID_KEY, | ||
|
|
@@ -166,8 +173,8 @@ def get_eval_history(self) -> List[ScenarioTestEvaluation]: | |
|
|
||
| def get_items( | ||
| self, level: EntityLevel = EntityLevel.ITEM | ||
| ) -> Union[List[DatasetItem], List[Scene]]: | ||
| """Gets items within a scenario test at a given level, returning a list of DatasetItem or Scene objects. | ||
| ) -> Union[List[Track], List[DatasetItem], List[Scene]]: | ||
| """Gets items within a scenario test at a given level, returning a list of Track, DatasetItem, or Scene objects. | ||
|
|
||
| Args: | ||
| level: :class:`EntityLevel` | ||
|
|
@@ -178,14 +185,22 @@ def get_items( | |
| response = self.connection.get( | ||
| f"validate/scenario_test/{self.id}/items", | ||
| ) | ||
| if level == EntityLevel.TRACK: | ||
| return [ | ||
| Track.from_json(track, connection=self.connection) | ||
|
Contributor
Author
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. This line is the reason for the long justification above 😤
Contributor
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. Haha, the struggle is real 🥇 |
||
| for track in response.get(TRACKS_KEY, []) | ||
| ] | ||
| if level == EntityLevel.SCENE: | ||
| return [ | ||
| Scene.from_json(scene, skip_validate=True) | ||
| for scene in response[SCENES_KEY] | ||
| for scene in response.get(SCENES_KEY, []) | ||
| ] | ||
| return [ | ||
| DatasetItem.from_json(item) for item in response[DATASET_ITEMS_KEY] | ||
| ] | ||
| if level == EntityLevel.ITEM: | ||
| return [ | ||
| DatasetItem.from_json(item) | ||
| for item in response.get(DATASET_ITEMS_KEY, []) | ||
| ] | ||
| raise ValueError(f"Invalid entity level: {level}") | ||
|
|
||
| def set_baseline_model(self, model_id: str): | ||
| """Sets a new baseline model for the ScenarioTest. In order to be eligible to be a baseline, | ||
|
|
@@ -222,23 +237,41 @@ def upload_external_evaluation_results( | |
| len(results) > 0 | ||
| ), "Submitting evaluation requires at least one result." | ||
|
|
||
| level = EntityLevel.ITEM | ||
| level: Optional[EntityLevel] = None | ||
| metric_per_ref_id = {} | ||
| weight_per_ref_id = {} | ||
| aggregate_weighted_sum = 0.0 | ||
| aggregate_weight = 0.0 | ||
|
|
||
| # Ensures reults at only one EntityLevel are provided, otherwise throwing a ValueError | ||
| def ensure_level_consistency_or_raise( | ||
| cur_level: Optional[EntityLevel], new_level: EntityLevel | ||
| ): | ||
| if level is not None and level != new_level: | ||
| raise ValueError( | ||
| f"All evaluation results must only pertain to one level. Received {cur_level} then {new_level}" | ||
| ) | ||
|
|
||
| # aggregation based on https://en.wikipedia.org/wiki/Weighted_arithmetic_mean | ||
| for r in results: | ||
| # Ensure results are uploaded ONLY for items or ONLY for scenes | ||
| # Ensure results are uploaded ONLY for ONE OF tracks, items, and scenes | ||
| if r.track_ref_id is not None: | ||
| ensure_level_consistency_or_raise(level, EntityLevel.TRACK) | ||
| level = EntityLevel.TRACK | ||
| if r.item_ref_id is not None: | ||
| ensure_level_consistency_or_raise(level, EntityLevel.ITEM) | ||
| level = EntityLevel.ITEM | ||
| if r.scene_ref_id is not None: | ||
| ensure_level_consistency_or_raise(level, EntityLevel.SCENE) | ||
| level = EntityLevel.SCENE | ||
| if r.item_ref_id is not None and level == EntityLevel.SCENE: | ||
| raise ValueError( | ||
| "All evaluation results must either pertain to a scene_ref_id or an item_ref_id, not both." | ||
| ) | ||
| ref_id = ( | ||
| r.item_ref_id if level == EntityLevel.ITEM else r.scene_ref_id | ||
| r.track_ref_id | ||
| if level == EntityLevel.TRACK | ||
| else ( | ||
| r.item_ref_id | ||
| if level == EntityLevel.ITEM | ||
| else r.scene_ref_id | ||
| ) | ||
| ) | ||
|
|
||
| # Aggregate scores and weights | ||
|
|
@@ -255,7 +288,7 @@ def upload_external_evaluation_results( | |
| "overall_metric": aggregate_weighted_sum / aggregate_weight, | ||
| "model_id": model_id, | ||
| "slice_id": self.slice_id, | ||
| "level": level.value, | ||
| "level": level.value if level else None, | ||
| } | ||
| response = self.connection.post( | ||
| payload, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tricky design decision here.
Trackneeds access to at least aConnectionobject to be able to call the API to upload track metadata, preferablyNucleusClientaccess for consistency with other classes likeModel. However, aValidateinstance is created fromNucleusClient, and so attempting to importTrackfromScenarioTest, from where it is created inget_items, will cause a circular import. The lessermost evil was to updateTrack.from_jsonto take aConnectioninstead of aNucleusClient.The tradeoff here was exposing
connectionas a public property ofNucleusClientso to access this property when creatingTracks in cases when only aNucleusClientis available. This should be safe, since aConnectionconsists of two public properties––api_keyandendpoint.Lmk if there are any issues with this approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is fine.
I think passing around a
connectionis a newer approach than passing the client. I actually don't see any reason for a class having to have full access to the client. We should probably refactor everything to pass around the connection object itself.I don't see any harm in doing things this way so ... let's go ahead 🙂