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

ArrayData: Allow defining array(s) on construction and make name optional in get_array #6097

Merged
merged 3 commits into from
Sep 5, 2023

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Aug 11, 2023

Adds type hinting for aiida.orm.nodes.data.array.array.

Currently, the constructor does not allow to define any arrays to set
when constructing a new node, so one is forced to multi line code:

node = ArrayData()
node.set_array('a', np.array([1, 2]))
node.set_array('b', np.array([3, 4]))

This commit allows initialization upon construction simplifying the code
above to:

node = ArrayData({'a': np.array([1, 2]), 'b': np.array([3, 4])})

Note that it is also possible to pass a single array to the constructor,
in which case the array name is taken from the default_array_name
class attribute.

For backwards compatibility, it remains possible to construct an
ArrayData without any arrays.

The ArrayData was designed to be able to store multiple numpy arrays.
While useful, it forced users to be more verbose than necessary when
only storing a single array as an explicit array name is always required:

node = ArrayData()
node.set_array('some_key', numpy.array([]))
node.get_array('some_key')

The get_array method is updated to allow None for the name
argument as long as the node only stores a single array so that it can
return the correct array unambiguously. This simplifies typical user
code significantly:

node = ArrayData(numpy.array([]))
node.get_array()

@sphuber sphuber requested review from unkcpz and mbercx August 11, 2023 08:05
@sphuber sphuber force-pushed the feature/array-data-constructor branch 2 times, most recently from 8b55129 to 5997a65 Compare August 14, 2023 08:37
@sphuber
Copy link
Contributor Author

sphuber commented Sep 5, 2023

Any objections to this @mbercx @unkcpz ?

unkcpz
unkcpz previously approved these changes Sep 5, 2023
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

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

Thanks, looks all good.

@sphuber sphuber force-pushed the feature/array-data-constructor branch from 5997a65 to c50cd1d Compare September 5, 2023 14:45
Currently, the constructor does not allow to define any arrays to set
when constructing a new node, so one is forced to multi line code:

    node = ArrayData()
    node.set_array('a', np.array([1, 2]))
    node.set_array('b', np.array([3, 4]))

This commit allows initialization upon construction simplifying the code
above to:

    node = ArrayData({'a': np.array([1, 2]), 'b': np.array([3, 4])})

Note that it is also possible to pass a single array to the constructor,
in which case the array name is taken from the `default_array_name`
class attribute.

For backwards compatibility, it remains possible to construct an
`ArrayData` without any arrays.
The `ArrayData` was designed to be able to store multiple numpy arrays.
While useful, it forced users to be more verbose than necessary when
only storing a single array as an explicit array name is always required:

    node = ArrayData()
    node.set_array('some_key', numpy.array([]))
    node.get_array('some_key')

The `get_array` method is updated to allow `None` for the `name`
argument as long as the node only stores a single array so that it can
return the correct array unambiguously. This simplifies typical user
code significantly:

    node = ArrayData(numpy.array([]))
    node.get_array()
@sphuber sphuber force-pushed the feature/array-data-constructor branch from c50cd1d to 6cdafd7 Compare September 5, 2023 14:58
@sphuber sphuber merged commit 7fbe67c into aiidateam:main Sep 5, 2023
11 checks passed
@sphuber sphuber deleted the feature/array-data-constructor branch September 5, 2023 15:18
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