Repository navigation
Size mismatch feedback #19
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
6 commits
Select commit
Hold shift + click to select a range
a2bef83
Give feedback on response size mismatch instead of erroring
peterbjohnson 9be872d
Test size mismatch feedback through evaluation_function
peterbjohnson bbf648b
Only mock the client at import when credentials are missing
peterbjohnson 6e8a68e
Revert readme note about the healthcheck
peterbjohnson 28c8213
Move remaining shape tests into evaluation_test.py
peterbjohnson 6c91f3e
Refactored to use numpy
m-messer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def plural(n, word, word_plural=None): | ||
| if n == 1: | ||
| return f"{n} {word}" | ||
| return f"{n} {word_plural or word + 's'}" | ||
|
|
||
|
|
||
| def entries(n): | ||
| return plural(n, "entry", "entries") | ||
|
|
||
|
|
||
| def describe_shape(shape): | ||
| if len(shape) == 1: | ||
| return entries(shape[0]) | ||
| if len(shape) == 2: | ||
| return f"{plural(shape[0], 'row')} and {plural(shape[1], 'column')}" | ||
| return "size " + " x ".join(str(n) for n in shape) | ||
|
|
||
|
|
||
| def array_shape(array): | ||
| """ | ||
| Return the shape of a regular (rectangular) array, e.g. (2, 3) for a | ||
| 2x3 matrix, or None if the array is ragged. | ||
| """ | ||
| try: | ||
| return np.shape(array) | ||
| except ValueError: | ||
| return None | ||
|
|
||
|
|
||
| def entry_lengths(array): | ||
| """ | ||
| Return the length of each top-level entry, with -1 for an entry that | ||
| is a single expression rather than a list. | ||
| """ | ||
| return np.array([len(e) if isinstance(e, list) else -1 for e in array]) | ||
|
|
||
|
|
||
| def shape_feedback(response, answer): | ||
| """ | ||
| Return feedback explaining how the size of the response differs | ||
| from the answer, or None if they have the same shape. | ||
| """ | ||
| if not isinstance(response, list): | ||
| return f"Your response should be an array of {entries(len(answer))}." | ||
|
|
||
| response_shape = array_shape(response) | ||
| answer_shape = array_shape(answer) | ||
|
|
||
| if response_shape is not None and answer_shape is not None: | ||
| if response_shape == answer_shape: | ||
| return None | ||
| return f"Your response has {describe_shape(response_shape)}, "\ | ||
| f"but the expected answer has {describe_shape(answer_shape)}." | ||
|
|
||
| # At least one of the arrays is ragged, so numpy can't give its shape | ||
| if len(response) != len(answer): | ||
| return f"Your response has {entries(len(response))}, "\ | ||
| f"but the expected answer has {entries(len(answer))}." | ||
|
|
||
| mismatches = np.argwhere(entry_lengths(response) != entry_lengths(answer)).ravel() | ||
| if len(mismatches) == 0: | ||
| # The mismatch is more than one level deep | ||
| return "Your response does not have the same shape as the expected answer." | ||
|
|
||
| i = mismatches[0] | ||
| response_length = int(entry_lengths(response)[i]) | ||
| answer_length = int(entry_lengths(answer)[i]) | ||
| where = f"Entry ({i + 1})" | ||
|
|
||
| if answer_length == -1: | ||
| return f"{where} should be a single expression, not a list." | ||
| if response_length == -1: | ||
| return f"{where} should be an array of {entries(answer_length)}." | ||
| return f"{where} has {entries(response_length)}, but should have {entries(answer_length)}." |
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.