Skip to content
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

Convert data to arrays if possible when creating datasets #48

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions exdir/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def _data_to_shape_and_dtype(data, shape, dtype):
if data is not None:
if shape is None:
shape = data.shape
shape = np.shape(data)
if dtype is None:
dtype = data.dtype
return shape, dtype
Expand All @@ -25,10 +25,10 @@ def _data_to_shape_and_dtype(data, shape, dtype):

def _assert_data_shape_dtype_match(data, shape, dtype):
if data is not None:
if shape is not None and np.product(shape) != np.product(data.shape):
if shape is not None and np.product(shape) != np.product(np.shape(data)):
raise ValueError(
"Provided shape and data.shape do not match: {} vs {}".format(
shape, data.shape
shape, np.shape(shape)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo. Should it be np.shape(data)?

)
)

Expand Down Expand Up @@ -113,12 +113,15 @@ def create_dataset(self, name, shape=None, dtype=None,

prepared_data, attrs, meta = ds._prepare_write(data, self.plugin_manager.dataset_plugins.write_order)

if not isinstance(prepared_data, np.ndarray) and prepared_data is not None:
prepared_data = np.array(prepared_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which types will be affected by this? I remember something about quantities automatically getting converted the wrong way (i.e. not by the plugin if there was no plugin) because there was some support for conversion that we didn't expect. If this only affects lists (and other types that you'd really expect to get converted to an np.array), I'm all for it. But if this may trigger some unexpected conversion that a plugin should have done when the plugin is not enabled, I think we should only add it for types we already know (such as lists).


_assert_data_shape_dtype_match(prepared_data, shape, dtype)

shape, dtype = _data_to_shape_and_dtype(prepared_data, shape, dtype)

if prepared_data is not None:
if shape is not None and prepared_data.shape != shape:
if shape is not None and np.shape(prepared_data) != shape:
prepared_data = np.reshape(prepared_data, shape)
else:
if shape is None:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ def test_create_scalar_data(setup_teardown_file):
assert dset.shape == data.shape


def test_create_list_data(setup_teardown_file):
"""Create a scalar dataset from existing array."""
f = setup_teardown_file[3]
grp = f.create_group("test")

data = [1, 2, 3]
dset = grp.create_dataset('foo', data=data)
assert dset.shape == (3,)
assert np.array_equal(dset.data, np.array(data))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test!



def test_create_extended_data(setup_teardown_file):
"""Create an extended dataset from existing data."""
f = setup_teardown_file[3]
Expand Down