| 1 |
"""Unit tests for the Attribute interface of the HDF5 io library""" |
| 2 |
|
| 3 |
# Copyright (C) 2013 Chris Richardson |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import pytest |
| 21 |
import os |
| 22 |
import gc |
| 23 |
from dolfin import * |
| 24 |
import numpy |
| 25 |
from dolfin_utils.test import skip_if_not_HDF5, fixture, tempdir, \ |
| 26 |
xfail_with_serial_hdf5_in_parallel, skip_with_serial_hdf5_in_parallel |
| 27 |
|
| 28 |
|
| 29 |
@pytest.yield_fixture |
| 30 |
def attr(tempdir): |
| 31 |
hdf_file = HDF5File(MPI.comm_world, os.path.join(tempdir, "hdf_file.h5"), "w") |
| 32 |
x = Vector(MPI.comm_world, 123) |
| 33 |
hdf_file.write(x, "/a_vector") |
| 34 |
attr = hdf_file.attributes("/a_vector") |
| 35 |
|
| 36 |
yield attr |
| 37 |
|
| 38 |
# Destroy everything deterministically |
| 39 |
hdf_file.close() |
| 40 |
del hdf_file, x, attr |
| 41 |
gc.collect() |
| 42 |
MPI.barrier(MPI.comm_world) |
| 43 |
|
| 44 |
@skip_if_not_HDF5 |
| 45 |
@xfail_with_serial_hdf5_in_parallel |
| 46 |
def test_fail_on_accessing_attribute_on_non_existing_dataset(tempdir): |
| 47 |
hdf_file = HDF5File(MPI.comm_world, os.path.join(tempdir, "hdf_file.h5"), "w") |
| 48 |
with pytest.raises(RuntimeError): |
| 49 |
attr = hdf_file.attributes("/a_vector") |
| 50 |
|
| 51 |
@skip_if_not_HDF5 |
| 52 |
@skip_with_serial_hdf5_in_parallel |
| 53 |
def test_read_write_str_attribute(attr): |
| 54 |
attr['name'] = 'Vector' |
| 55 |
assert attr.type_str("name") == "string" |
| 56 |
assert attr['name'] == 'Vector' |
| 57 |
|
| 58 |
@skip_if_not_HDF5 |
| 59 |
@skip_with_serial_hdf5_in_parallel |
| 60 |
def test_read_write_float_attribute(attr): |
| 61 |
attr['val'] = -9.2554 |
| 62 |
assert attr.type_str("val") == "float" |
| 63 |
assert attr['val'] == -9.2554 |
| 64 |
|
| 65 |
@skip_if_not_HDF5 |
| 66 |
@skip_with_serial_hdf5_in_parallel |
| 67 |
def test_read_write_int_attribute(attr): |
| 68 |
attr['val'] = 1 |
| 69 |
assert attr.type_str("val") == "int" |
| 70 |
assert attr['val'] == 1 |
| 71 |
|
| 72 |
@skip_if_not_HDF5 |
| 73 |
@skip_with_serial_hdf5_in_parallel |
| 74 |
def test_read_write_vec_float_attribute(attr): |
| 75 |
vec = numpy.array([1,2,3,4.5], dtype='float') |
| 76 |
attr['val'] = vec |
| 77 |
ans = attr['val'] |
| 78 |
assert attr.type_str("val") == "vectorfloat" |
| 79 |
assert len(vec) == len(ans) |
| 80 |
for val1, val2 in zip(vec, ans): |
| 81 |
assert val1 == val2 |
| 82 |
|
| 83 |
@skip_if_not_HDF5 |
| 84 |
@skip_with_serial_hdf5_in_parallel |
| 85 |
def test_read_write_vec_int_attribute(attr): |
| 86 |
vec = numpy.array([1,2,3,4,5], dtype=numpy.uintp) |
| 87 |
attr['val'] = vec |
| 88 |
ans = attr['val'] |
| 89 |
assert attr.type_str("val") == "vectorint" |
| 90 |
assert len(vec) == len(ans) |
| 91 |
for val1, val2 in zip(vec, ans): |
| 92 |
assert val1 == val2 |
| 93 |
|
| 94 |
@skip_if_not_HDF5 |
| 95 |
@skip_with_serial_hdf5_in_parallel |
| 96 |
def test_attribute_container_interface(attr): |
| 97 |
names = ["data_0", "data_1", "data_2", "data_3"] |
| 98 |
values = [i for i in range(4)] |
| 99 |
|
| 100 |
for name, value in zip(names, values): |
| 101 |
attr[name] = value |
| 102 |
|
| 103 |
# Added from the vector storage call above |
| 104 |
if "partition" in attr: |
| 105 |
names.append("partition") |
| 106 |
values.append(attr["partition"]) |
| 107 |
|
| 108 |
assert(attr.list_attributes() == names) |
| 109 |
for (name0, value0), (name1, value1) in zip(attr.to_dict().items(), |
| 110 |
dict(zip(names, values)).items()): |
| 111 |
assert name0 == name1 |
| 112 |
if isinstance(value0, numpy.ndarray): |
| 113 |
assert (value0 == value1).all() |
| 114 |
else: |
| 115 |
assert value0 == value1 |