-
Notifications
You must be signed in to change notification settings - Fork 26
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
Check DynamicTableRegion data is in bounds #1168
Draft
rly
wants to merge
1
commit into
dev
Choose a base branch
from
validate_dtr
base: dev
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
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 |
---|---|---|
|
@@ -1360,42 +1360,64 @@ class DynamicTableRegion(VectorData): | |
'table', | ||
) | ||
|
||
MAX_ROWS_TO_VALIDATE_INIT = int(1e3) | ||
|
||
@docval({'name': 'name', 'type': str, 'doc': 'the name of this VectorData'}, | ||
{'name': 'data', 'type': ('array_data', 'data'), | ||
'doc': 'a dataset where the first dimension is a concatenation of multiple vectors'}, | ||
{'name': 'description', 'type': str, 'doc': 'a description of what this region represents'}, | ||
{'name': 'table', 'type': DynamicTable, | ||
'doc': 'the DynamicTable this region applies to', 'default': None}, | ||
{'name': 'validate_data', 'type': bool, | ||
'doc': 'whether to validate the data is in bounds of the linked table', 'default': True}, | ||
allow_positional=AllowPositional.WARNING) | ||
def __init__(self, **kwargs): | ||
t = popargs('table', kwargs) | ||
t, validate_data = popargs('table', 'validate_data', kwargs) | ||
data = getargs('data', kwargs) | ||
if validate_data: | ||
self._validate_index_in_range(data, t) | ||
|
||
super().__init__(**kwargs) | ||
self.table = t | ||
if t is not None: # set the table attribute using fields to avoid another validation in the setter | ||
self.fields['table'] = t | ||
|
||
def _validate_index_in_range(self, data, table): | ||
"""If the length of data is small, and if data contains an index that is out of bounds, then raise an error. | ||
If the object is being constructed from a file, raise a warning instead to ensure invalid data can still be | ||
read. | ||
""" | ||
if table and len(data) <= self.MAX_ROWS_TO_VALIDATE_INIT: | ||
for val in data: | ||
if val >= len(table) or val < 0: | ||
error_msg = f"DynamicTableRegion index {val} is out of bounds for {type(table)} '{table.name}'." | ||
self._error_on_new_warn_on_construct(error_msg, error_cls=IndexError) | ||
|
||
@property | ||
def table(self): | ||
"""The DynamicTable this DynamicTableRegion is pointing to""" | ||
return self.fields.get('table') | ||
|
||
@table.setter | ||
def table(self, val): | ||
def table(self, table): | ||
""" | ||
Set the table this DynamicTableRegion should be pointing to | ||
Set the table this DynamicTableRegion should be pointing to. | ||
|
||
:param val: The DynamicTable this DynamicTableRegion should be pointing to | ||
This will validate all data elements in this DynamicTableRegion to ensure they are within bounds. | ||
|
||
:param table: The DynamicTable this DynamicTableRegion should be pointing to | ||
|
||
:raises: AttributeError if table is already in fields | ||
:raises: IndexError if the current indices are out of bounds for the new table given by val | ||
""" | ||
if val is None: | ||
if table is None: | ||
return | ||
if 'table' in self.fields: | ||
msg = "can't set attribute 'table' -- already set" | ||
raise AttributeError(msg) | ||
dat = self.data | ||
if isinstance(dat, DataIO): | ||
dat = dat.data | ||
self.fields['table'] = val | ||
|
||
self.fields['table'] = table | ||
for val in self.data: | ||
self._validate_new_data_element(val) | ||
Comment on lines
+1419
to
+1420
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. You might want to try this as a list comprehension |
||
|
||
def __getitem__(self, arg): | ||
return self.get(arg) | ||
|
@@ -1540,6 +1562,12 @@ def _validate_on_set_parent(self): | |
warn(msg, stacklevel=2) | ||
return super()._validate_on_set_parent() | ||
|
||
def _validate_new_data_element(self, arg): | ||
"""Validate that the new index is within bounds of the table. Raises an IndexError if not.""" | ||
if self.table and (arg >= len(self.table) or arg < 0): | ||
raise IndexError(f"DynamicTableRegion index {arg} is out of bounds for " | ||
f"{type(self.table)} '{self.table.name}'.") | ||
|
||
|
||
def _uint_precision(elements): | ||
""" Calculate the uint precision needed to encode a set of elements """ | ||
|
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
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
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.
Instead of iterating through all the values, I think checking for
np.max(data) >= len(table)
would be more efficient.