-
Notifications
You must be signed in to change notification settings - Fork 319
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
Array type validator #342
Merged
Merged
Array type validator #342
Changes from 1 commit
Commits
Show all changes
3 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import math | ||
import numpy | ||
import numpy as np | ||
|
||
BIGSTRING = 1000000000 | ||
BIGINT = int(1e18) | ||
|
@@ -140,7 +140,7 @@ class Numbers(Validator): | |
- fix raises | ||
""" | ||
|
||
validtypes = (float, int, numpy.integer, numpy.floating) | ||
validtypes = (float, int, np.integer, np.floating) | ||
|
||
def __init__(self, min_value=-float("inf"), max_value=float("inf")): | ||
|
||
|
@@ -180,7 +180,7 @@ class Ints(Validator): | |
min_value <= value <= max_value | ||
""" | ||
|
||
validtypes = (int, numpy.integer) | ||
validtypes = (int, np.integer) | ||
|
||
def __init__(self, min_value=-BIGINT, max_value=BIGINT): | ||
if isinstance(min_value, self.validtypes): | ||
|
@@ -294,3 +294,59 @@ def validate(self, value, context=''): | |
def __repr__(self): | ||
parts = (repr(v)[1:-1] for v in self._validators) | ||
return '<MultiType: {}>'.format(', '.join(parts)) | ||
|
||
|
||
class Arrays(Validator): | ||
""" | ||
Validator for numerical numpy arrays | ||
Args: | ||
min_value (Optional[Union[float, int]): Min value allowed, default inf | ||
max_value: (Optional[Union[float, int]): Max value allowed, default inf | ||
shape: (Optional): None | ||
""" | ||
|
||
validtypes = (int, float, np.integer, np.floating) | ||
|
||
def __init__(self, min_value=-float("inf"), max_value=float("inf"), | ||
shape=None): | ||
|
||
if isinstance(min_value, self.validtypes): | ||
self._min_value = min_value | ||
else: | ||
raise TypeError('min_value must be a number') | ||
|
||
if isinstance(max_value, self.validtypes) and max_value > min_value: | ||
self._max_value = max_value | ||
else: | ||
raise TypeError('max_value must be a number bigger than min_value') | ||
self._shape = shape | ||
|
||
def validate(self, value, context=''): | ||
|
||
if not isinstance(value, np.ndarray): | ||
raise TypeError( | ||
'{} is not a numpy array; {}'.format(repr(value), context)) | ||
|
||
if value.dtype not in self.validtypes: | ||
raise TypeError( | ||
'{} is not an int or float; {}'.format(repr(value), context)) | ||
if self._shape != None: | ||
if (np.shape(value) != self._shape): | ||
raise ValueError( | ||
'{} does not have expected shape {}; {}'.format( | ||
repr(value), self._shape, context)) | ||
|
||
if not (self._min_value <= np.min(value) and | ||
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. maybe only check min and/or max if it's not the default (inf) values? This can be expensive for large arrays. |
||
np.max(value) <= self._max_value): | ||
raise ValueError( | ||
'{} is invalid: all values must be between ' | ||
'{} and {} inclusive; {}'.format( | ||
repr(value), self._min_value, self._max_value, context)) | ||
|
||
is_numeric = True | ||
|
||
def __repr__(self): | ||
minv = self._min_value if math.isfinite(self._min_value) else None | ||
maxv = self._max_value if math.isfinite(self._max_value) else None | ||
return '<Arrays{}, shape: {}>'.format(range_str(minv, maxv, 'v'), self._shape) | ||
|
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.
don't use random in tests - make an explicit array.