1
+ import os
2
+
1
3
import h5py
2
4
import numpy as np
3
5
import pytest
4
6
5
- from tavi .data .nxdict import spice_scan_to_nxdict
6
7
from tavi .data .nxentry import NexusEntry
7
8
8
9
@@ -37,19 +38,23 @@ def test_get_dataset(nexus_entries):
37
38
38
39
39
40
def test_to_nexus (nexus_entries ):
40
- path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
41
+ path_to_nexus = "./test_data/scan_to_nexus_test.h5"
41
42
for scan_num , nexus_entry in nexus_entries .items ():
42
- nexus_entry .to_nexus (path_to_nexus_entry , scan_num )
43
+ nexus_entry .to_nexus (path_to_nexus , scan_num )
43
44
44
- with h5py .File (path_to_nexus_entry , "r" ) as nexus_file :
45
+ with h5py .File (path_to_nexus , "r" ) as nexus_file :
45
46
assert str (nexus_file ["scan0034" ]["title" ].asstr ()[...]) == "scan_title_34"
46
47
assert nexus_file ["scan0034" ].attrs ["NX_class" ] == "NXentry"
48
+ os .remove (path_to_nexus )
47
49
48
50
49
- def test_from_nexus (nexus_entries ):
50
- path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
51
- entries = NexusEntry .from_nexus (path_to_nexus_entry )
51
+ def test_from_nexus_get_all (nexus_entries ):
52
+ # generate NeXus file
53
+ path_to_nexus = "./test_data/scan_to_nexus_test.h5"
54
+ for scan_num , nexus_entry in nexus_entries .items ():
55
+ nexus_entry .to_nexus (path_to_nexus , scan_num )
52
56
57
+ entries = NexusEntry .from_nexus (path_to_nexus )
53
58
scan0034 = entries ["scan0034" ]
54
59
assert scan0034 ["definition" ] == nexus_entries ["scan0034" ]["definition" ]
55
60
assert np .allclose (
@@ -75,28 +80,38 @@ def test_from_nexus(nexus_entries):
75
80
scan0034 .get ("instrument/analyser/a2" ),
76
81
np .array ([242.0 , 242.1 , 242.2 ]),
77
82
)
83
+ assert np .allclose (
84
+ scan0034 .get ("data/a1" ),
85
+ np .array ([142.0 , 142.1 , 142.2 ]),
86
+ )
78
87
79
88
scan0035 = entries ["scan0035" ]
80
89
assert scan0035 .get ("title" ) == "scan_title_35"
90
+ os .remove (path_to_nexus )
81
91
82
92
83
- def test_from_nexus_single_scan ():
84
- path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
85
- nexus_entries = NexusEntry .from_nexus (path_to_nexus_entry , scan_num = 35 )
93
+ def test_from_nexus_get_one (nexus_entries ):
94
+ # generate NeXus
95
+ path_to_nexus = "./test_data/scan_to_nexus_test.h5"
96
+ for scan_num , nexus_entry in nexus_entries .items ():
97
+ nexus_entry .to_nexus (path_to_nexus , scan_num )
98
+
99
+ nexus_entries = NexusEntry .from_nexus (path_to_nexus , scan_num = 35 )
86
100
scan0035 = nexus_entries ["scan0035" ]
87
101
assert scan0035 .get ("title" ) == "scan_title_35"
88
102
89
103
90
- def test_from_nexus_IPTS32124_CG4C_exp0424 ():
104
+ def test_from_nexus_real_data ():
91
105
path_to_nexus_entry = "./test_data/IPTS32124_CG4C_exp0424/scan0034.h5"
92
106
nexus_entries = NexusEntry .from_nexus (path_to_nexus_entry )
107
+
93
108
scan0034 = nexus_entries ["scan0034" ]
94
109
assert scan0034 .get ("definition" ) == "NXtas"
95
110
assert scan0034 .get ("end_time" ) == "2024-07-03T02:41:28"
96
111
assert np .allclose (scan0034 .get ("s1" )[0 :3 ], [36.14 , 36.5025 , 36.855 ])
97
112
98
113
99
- def test_from_spice_IPTS32124_CG4C_exp0424_single_scan ():
114
+ def test_from_spice_get_one ():
100
115
path_to_spice_entry = "./test_data/exp424"
101
116
scan0034 = NexusEntry .from_spice (path_to_spice_entry , 34 )["scan0034" ]
102
117
@@ -105,7 +120,7 @@ def test_from_spice_IPTS32124_CG4C_exp0424_single_scan():
105
120
assert np .allclose (scan0034 .get ("s1" )[0 :3 ], np .array ([36.14 , 36.5025 , 36.855 ]))
106
121
107
122
108
- def test_from_spice_IPTS32124_CG4C_exp0424_all_scan ():
123
+ def test_from_spice_get_all ():
109
124
path_to_spice_entry = "./test_data/exp424"
110
125
scans = NexusEntry .from_spice (path_to_spice_entry )
111
126
@@ -120,36 +135,35 @@ def test_from_spice_IPTS32124_CG4C_exp0424_all_scan():
120
135
121
136
122
137
def test_get_from_daslogs ():
123
- path_to_nexus_entry = "./test_data/spice_to_nexus_test_scan34 .h5"
138
+ path_to_nexus_entry = "./test_data/IPTS32124_CG4C_exp0424/scan0034 .h5"
124
139
scan0034 = NexusEntry .from_nexus (path_to_nexus_entry , 34 )["scan0034" ]
125
140
assert scan0034 .get_metadata_from_daslogs ("sense" ) == "-+-"
126
141
assert np .allclose (scan0034 .get_data_from_daslogs ("s1" )[0 :3 ], np .array ([36.14 , 36.5025 , 36.855 ]))
127
142
128
143
129
- def test_spice_scan_to_nexus ():
144
+ def test_spice_to_nexus_one ():
130
145
131
- path_to_spice_entry = "./test_data/exp424"
132
- path_to_nexus_entry = "./test_data/spice_to_nxdict_test_scan0034.h5"
133
- scan0034 = NexusEntry .from_spice (path_to_spice_entry , 34 )
146
+ path_to_spice_folder = "./test_data/exp424"
147
+ path_to_nexus = "./test_data/spice_to_nxdict_test_scan0034.h5"
148
+ scan0034 = NexusEntry .from_spice (path_to_spice_folder , 34 )
134
149
for scan_name , scan in scan0034 .items ():
135
- scan .to_nexus (path_to_nexus_entry , name = scan_name )
150
+ scan .to_nexus (path_to_nexus , name = scan_name )
136
151
152
+ with h5py .File (path_to_nexus , "r" ) as nexus_file :
153
+ assert len (nexus_file ) == 1
154
+ os .remove (path_to_nexus )
137
155
138
- def test_spice_data_to_nexus ():
139
- path_to_spice_data = "./test_data/exp424"
140
- scan_dict = spice_scan_to_nxdict (path_to_spice_data )
141
156
142
- entries = {"scan0034" : nxdict }
143
- nexus_entries = {}
144
- for scan_num , scan_content in entries .items ():
145
- content_list = []
146
- for key , val in scan_content .items ():
147
- content_list .append ((key , val ))
148
- nexus_entries .update ({scan_num : NexusEntry (content_list )})
149
- # generate h5 file
150
- path_to_nexus_entry = "./test_data/spice_to_nxdict_test_scan0034.h5"
151
- for scan_num , nexus_entry in nexus_entries .items ():
152
- nexus_entry .to_nexus (path_to_nexus_entry , scan_num )
157
+ def test_spice_to_nexus_all ():
158
+ path_to_spice_folder = "./test_data/exp424"
159
+ path_to_nexus = "./test_data/spice_to_nxdict_test_all.h5"
160
+ scan_dict = NexusEntry .from_spice (path_to_spice_folder )
161
+
162
+ for scan_name , scan in scan_dict .items ():
163
+ scan .to_nexus (path_to_nexus , name = scan_name )
164
+ with h5py .File (path_to_nexus , "r" ) as nexus_file :
165
+ assert len (nexus_file ) == 92
166
+ os .remove (path_to_nexus )
153
167
154
168
155
169
@pytest .fixture
0 commit comments