-
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?
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #48 +/- ##
=========================================
+ Coverage 97.58% 97.6% +0.01%
=========================================
Files 11 11
Lines 1369 1376 +7
=========================================
+ Hits 1336 1343 +7
Misses 33 33
Continue to review full report at Codecov.
|
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.
I think we should definitely use np.shape, but I'm not sure if np.array(data) is a good idea (see the inline comment for details).
@@ -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 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).
raise ValueError( | ||
"Provided shape and data.shape do not match: {} vs {}".format( | ||
shape, data.shape | ||
shape, np.shape(shape) |
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)
?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding a test!
Converts data to a numpy array if data is not a ndarray or None. Among other things this enables lists to directly be used when creating a dataset, similar to h5py.
This also enables other objects to be converted to numpy object arrays. This is in line with the current support for and handling of object arrays, but see issue #47.