-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_iterator_test.py
More file actions
143 lines (116 loc) · 4.7 KB
/
node_iterator_test.py
File metadata and controls
143 lines (116 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#
# (c) Copyright 2018 DESY
#
# This file is part of python-pninexus.
#
# python-pninexus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# python-pninexus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-pninexus. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
#
# Created on: Mar 9, 2018
# Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
#
from __future__ import print_function
import os.path
import unittest
from pninexus import h5cpp
from pninexus import nexus
file_structure = """
<group name="entry" type="NXentry">
<group name="instrument" type="NXinstrument">
<group name="detector_1" type="NXdetector">
<field name="data" type="uint32" units="cps">
<dimensions rank="3">
<dim value="0" index="1"/>
<dim value="1024" index="2"/>
<dim value="512" index="3"/>
</dimensions>
<chunk rank="3">
<dim value="100" index="1"/>
<dim value="1024" index="2"/>
<dim value="512" index="3"/>
</chunk>
</field>
</group>
<group name="detector_2" type="NXdetector">
<field name="data" type="uint32" units="cps">
<dimensions rank="3">
<dim value="0" index="1"/>
<dim value="512" index="2"/>
<dim value="1024" index="3"/>
</dimensions>
<chunk rank="3">
<dim value="100" index="1"/>
<dim value="512" index="2"/>
<dim value="1024" index="3"/>
</chunk>
</field>
</group>
</group>
<group name="sample" type="NXsample">
<field name="name" type="string">MySample</field>
</group>
<group name="data" type="NXdata">
<link name="energy" target="/entry/instrument/monochromator/energy"/>
</group>
</group>
"""
module_path = os.path.abspath(os.path.dirname(__file__))
class NodeIteratorTest(unittest.TestCase):
filename = os.path.join(module_path, "NodeIteratorTest.h5")
@classmethod
def setUpClass(cls):
super(NodeIteratorTest, cls).setUpClass()
file = nexus.create_file(
cls.filename, h5cpp.file.AccessFlags.TRUNCATE)
nexus.create_from_string(file.root(), file_structure)
def setUp(self):
self.file = nexus.open_file(
self.filename, h5cpp.file.AccessFlags.READONLY)
self.root = self.file.root()
def tearDown(self):
self.root.close()
self.file.close()
def test_immediate_children_iteration(self):
entry = self.root.nodes["entry"]
self.assertTrue(entry.nodes.exists("instrument"))
self.assertTrue(entry.nodes.exists("data"))
self.assertTrue(entry.nodes.exists("sample"))
nodes = [node.link.path.name for node in entry.nodes]
refnames = ["data", "instrument", "sample"]
self.assertSetEqual(set(nodes), set(refnames),
"Comparison of node names: {}".format(nodes))
def test_recursive_node_iteration(self):
instrument = h5cpp.node.get_node(
self.root, h5cpp.Path("/entry/instrument"))
node_paths = [node.link.path for node in instrument.nodes.recursive]
paths = [h5cpp.Path("/entry/instrument/detector_1"),
h5cpp.Path("/entry/instrument/detector_1/data"),
h5cpp.Path("/entry/instrument/detector_2"),
h5cpp.Path("/entry/instrument/detector_2/data")
]
self.assertEqual(
len(paths), len(node_paths),
"Number of links comparison: {} == {}".format(len(paths),
len(node_paths)))
for path in paths:
self.assertTrue(
path in node_paths,
"Path comparison: {} in {}".format(path, node_paths))
def test_exists(self):
data = h5cpp.node.get_node(self.root, h5cpp.Path("/entry/data"))
self.assertTrue(data.links.exists("energy"))
link = data.links["energy"]
self.assertFalse(link.is_resolvable)
self.assertTrue(data.is_valid)
self.assertFalse(data.nodes.exists("energy"))