-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XyData
: Allow defining array(s) on construction
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 = XyData() node.set_x(np.array([1, 2]), 'name', unit') node.set_y(np.array([3, 4]), 'name', unit') This commit allows initialization upon construction simplifying the code above to: node = XyData( np.array([1, 2]), np.array([3, 4]), x_name='name', x_unit='unit', y_names='name', y_units='unit' ) The units and names are intentionally made into keyword argument only in order to prevent accidental swapping of values. For backwards compatibility, it remains possible to construct an `XyData` without any arrays.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Tests for the :mod:`aiida.orm.nodes.data.array.xy` module.""" | ||
import numpy | ||
import pytest | ||
|
||
from aiida.common.exceptions import NotExistent | ||
from aiida.orm import XyData, load_node | ||
|
||
|
||
def test_read_stored(): | ||
"""Test reading an array from an ``XyData`` after storing and loading it.""" | ||
x_array = numpy.array([1, 2]) | ||
y_array = numpy.array([3, 4]) | ||
node = XyData(x_array, y_array, x_name='x_name', x_units='x_unit', y_names='y_name', y_units='y_units') | ||
|
||
assert numpy.array_equal(node.get_x()[1], x_array) | ||
assert numpy.array_equal(node.get_y()[0][1], y_array) | ||
|
||
node.store() | ||
assert numpy.array_equal(node.get_x()[1], x_array) | ||
assert numpy.array_equal(node.get_y()[0][1], y_array) | ||
|
||
loaded = load_node(node.uuid) | ||
assert numpy.array_equal(loaded.get_x()[1], x_array) | ||
assert numpy.array_equal(loaded.get_y()[0][1], y_array) | ||
|
||
|
||
def test_constructor(): | ||
"""Test the various construction options.""" | ||
with pytest.raises(TypeError): | ||
node = XyData(numpy.array([1, 2])) | ||
|
||
node = XyData() | ||
|
||
with pytest.raises(NotExistent): | ||
node.get_x() | ||
|
||
with pytest.raises(NotExistent): | ||
node.get_y() | ||
|
||
x_array = numpy.array([1, 2]) | ||
y_array = numpy.array([3, 4]) | ||
node = XyData(x_array, y_array, x_name='x_name', x_units='x_unit', y_names='y_name', y_units='y_units') | ||
assert numpy.array_equal(node.get_x()[1], x_array) | ||
assert numpy.array_equal(node.get_y()[0][1], y_array) |