Skip to content

Commit

Permalink
add warnings for ragged inputs to table
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Mar 6, 2024
1 parent a147f3b commit 9f7011f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/hdmf/common/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ def add_row(self, **kwargs):
else:
c.add_row(data[colname])
if is_ragged(c.data):
raise ValueError("Data is ragged. Use the 'index' argument when creating a column that will have ragged data.")
warn("Data is ragged. Use the 'index' argument when creating a column that will have ragged data.",
stacklevel=2)

def __eq__(self, other):
"""Compare if the two DynamicTables contain the same data.
Expand Down Expand Up @@ -828,7 +829,8 @@ def add_column(self, **kwargs): # noqa: C901

# if no index was provided, check that data is not ragged
if index is False and is_ragged(data):
raise ValueError("Data is ragged. Use the 'index' argument when adding a column with ragged data.")
warn("Data is ragged. Use the 'index' argument when adding a column with ragged data.",
stacklevel=2)

# Check that we are asked to create an index
if (isinstance(index, bool) or isinstance(index, int)) and index > 0 and len(data) > 0:
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/common/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,16 @@ def test_add_column_without_required_index(self):
multi_nested_data = [[[1, 2, 3], [1, 2, 3, 4]], [1, 2]]
arr_data = np.array([[1, 2, 3], [1, 2, 3, 4]], dtype='object')

with self.assertRaisesWith(ValueError, msg):
table.add_column(name='qux', description='qux column', data=lol_data,)
with self.assertRaisesWith(ValueError, msg):
table.add_column(name='qux', description='qux column', data=str_data,)
with self.assertRaisesWith(ValueError, msg):
table.add_column(name='qux', description='qux column', data=empty_data,)
with self.assertRaisesWith(ValueError, msg):
table.add_column(name='qux', description='qux column', data=multi_nested_data,)
with self.assertRaisesWith(ValueError, msg):
table.add_column(name='qux', description='qux column', data=arr_data,)
with self.assertWarnsWith(UserWarning, msg):
table.add_column(name='col1', description='', data=lol_data,)
with self.assertWarnsWith(UserWarning, msg):
table.add_column(name='col2', description='', data=str_data,)
with self.assertWarnsWith(UserWarning, msg):
table.add_column(name='col3', description='', data=empty_data,)
with self.assertWarnsWith(UserWarning, msg):
table.add_column(name='col4', description='', data=multi_nested_data,)
with self.assertWarnsWith(UserWarning, msg):
table.add_column(name='col5', description='', data=arr_data,)

def test_add_row_without_required_index(self):
"""
Expand All @@ -389,7 +389,7 @@ def test_add_row_without_required_index(self):
table.add_column(name='qux', description='qux column')
table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3])
msg = "Data is ragged. Use the 'index' argument when creating a column that will have ragged data."
with self.assertRaisesWith(ValueError, msg):
with self.assertWarnsWith(UserWarning, msg):
table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3 ,4])

def test_add_column_auto_index_int(self):
Expand Down

0 comments on commit 9f7011f

Please sign in to comment.