-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
) | ||
) | ||
|
||
|
@@ -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) | ||
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. 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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
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. 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] | ||
|
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.
Looks like a typo. Should it be
np.shape(data)
?