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

Conversation

simetenn
Copy link
Contributor

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.

@simetenn simetenn requested review from dragly and miladh May 11, 2018 08:29
@codecov
Copy link

codecov bot commented May 11, 2018

Codecov Report

Merging #48 into dev will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
tests/test_dataset.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update edbdea9...eb479a5. Read the comment docs.

Copy link
Member

@dragly dragly left a 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)
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).

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)?

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants