Skip to content

Commit 2836ded

Browse files
committed
updated scan class
1 parent 703cfd8 commit 2836ded

14 files changed

+108
-74
lines changed

.DS_Store

0 Bytes
Binary file not shown.

src/tavi/data/nxentry.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,24 @@ def to_nexus(self, path_to_nexus: str, name="scan") -> None:
216216
with h5py.File(path_to_nexus, "a") as nexus_file:
217217
scan_grp = nexus_file.require_group(name + "/")
218218
NexusEntry._write_recursively(self, scan_grp)
219-
# create soft link for data
220-
def_y = nexus_file[name]["data"].attrs["signal"]
221-
def_x = nexus_file[name]["data"].attrs["axes"]
222-
path_y = _find_val_path(def_y, nexus_file)
223-
path_x = _find_val_path(def_x, nexus_file)
224-
225-
if path_y is not None:
226-
def_y = "data/" + def_y
227-
if isinstance(scan_grp.get(def_y), h5py.Dataset):
228-
del scan_grp[def_y]
229-
scan_grp[def_y] = h5py.SoftLink(path_y)
230-
scan_grp[def_y + "/"].attrs["target"] = path_y
231-
if path_x is not None:
232-
def_x = "data/" + def_x
233-
if isinstance(scan_grp.get(def_x), h5py.Dataset):
234-
del scan_grp[def_x]
235-
scan_grp[def_x] = h5py.SoftLink(path_x)
236-
scan_grp[def_x + "/"].attrs["target"] = path_x
219+
if "data" in scan_grp: # create soft link for data
220+
def_y = scan_grp["data"].attrs["signal"]
221+
def_x = scan_grp["data"].attrs["axes"]
222+
path_y = _find_val_path(def_y, nexus_file)
223+
path_x = _find_val_path(def_x, nexus_file)
224+
225+
if path_y is not None:
226+
def_y = "data/" + def_y
227+
if isinstance(scan_grp.get(def_y), h5py.Dataset):
228+
del scan_grp[def_y]
229+
scan_grp[def_y] = h5py.SoftLink(path_y)
230+
scan_grp[def_y + "/"].attrs["target"] = path_y
231+
if path_x is not None:
232+
def_x = "data/" + def_x
233+
if isinstance(scan_grp.get(def_x), h5py.Dataset):
234+
del scan_grp[def_x]
235+
scan_grp[def_x] = h5py.SoftLink(path_x)
236+
scan_grp[def_x + "/"].attrs["target"] = path_x
237237

238238
# Create the ATTRIBUTES
239239
scan_grp.attrs["file_name"] = os.path.abspath(path_to_nexus)

src/tavi/data/scan.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class ScanInfo:
1212
"""Metadata containing scan information"""
1313

1414
scan_num: Optional[int] = None
15+
scan_title: str = ""
16+
def_y: str = "detector"
17+
def_x: str = "s1"
1518
start_time: Optional[str] = None
1619
end_time: Optional[str] = None
17-
scan_title: str = ""
18-
# preset_type: str = "normal"
20+
preset_type: str = "normal"
1921
preset_channel: str = "time"
2022
preset_value: float = 1.0
21-
def_y: str = "detector"
22-
def_x: str = "s1"
2323

2424

2525
@dataclass
@@ -29,6 +29,8 @@ class SampleUBInfo:
2929
sample_name: str = ""
3030
lattice_constants: tuple = (1.0, 1.0, 1.0, 90.0, 90.0, 90.0)
3131
ub_matrix: Optional[np.ndarray] = None
32+
u: Optional[np.ndarray] = None
33+
v: Optional[np.ndarray] = None
3234
# ub_mode: int = 0 # mode for UB determination in SPICE
3335
# angle_mode: int = 0 # mode for goni angle calculation in SPICE
3436
plane_normal: Optional[np.ndarray] = None

src/tavi/data/spice_reader.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def read_spice_ubconf(ub_file_name: str) -> dict:
7171
Returns:
7272
"""
7373
ubconf: dict[str, Any] = {}
74-
with open(ub_file_name, encoding="utf-8") as f:
74+
with open(ub_file_name, 'r', encoding="utf-8") as f:
7575
all_content = f.readlines()
7676

7777
for idx, line in enumerate(all_content):
@@ -129,8 +129,16 @@ def _create_spicelogs(path_to_scan_file: str) -> dict:
129129

130130
scan_path = os.path.abspath(path_to_scan_file)
131131
(*folder_path, _, _) = scan_path.split("/")
132-
ub_file_path = "/".join(folder_path + ["UBConf", metadata["ubconf"]])
133-
ub_conf_dict = {"file_path": ub_file_path}
132+
ub_file_path = os.path.join('/',*folder_path,"UBConf", metadata["ubconf"])
133+
ub_temp_file_path = os.path.join('/',*folder_path,"UBConf", "temp", metadata["ubconf"])
134+
135+
if os.path.isfile(ub_file_path):
136+
ub_conf_dict = {"file_path": ub_file_path}
137+
elif os.path.isfile(ub_temp_file_path):
138+
ub_conf_dict = {"file_path": ub_temp_file_path}
139+
else:
140+
ub_conf_dict = {"file_path": ""}
141+
print(f"Cannot find UB file {metadata["ubconf"]}")
134142
ubconf = read_spice_ubconf(ub_file_path)
135143
for k, v in ubconf.items():
136144
ub_conf_dict.update({k: v})

src/tavi/data/tavi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import h5py
66

7+
from tavi.data.scan import Scan
78
from tavi.data.scan_group import ScanGroup
8-
from tavi.data.scan_old.scan import Scan
99

1010

1111
class TAVI(object):

src/tavi/instrument/tas_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from typing import Optional, Union
55

6-
from tavi.data.scan_old.scan import Scan
6+
from tavi.data.scan import Scan
77
from tavi.instrument.goni import Goniometer
88
from tavi.instrument.mono_ana import MonoAna
99
from tavi.instrument.tas_cmponents import Collimators, Detector, Distances, Guide, Monitor, Source

test_data/.DS_Store

0 Bytes
Binary file not shown.

test_data/exp424/.DS_Store

0 Bytes
Binary file not shown.

test_data/scan_to_nexus_test.h5

-80.2 KB
Binary file not shown.
-126 KB
Binary file not shown.
-114 KB
Binary file not shown.

tests/test_nxentry.py

+47-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import os
2+
13
import h5py
24
import numpy as np
35
import pytest
46

5-
from tavi.data.nxdict import spice_scan_to_nxdict
67
from tavi.data.nxentry import NexusEntry
78

89

@@ -37,19 +38,23 @@ def test_get_dataset(nexus_entries):
3738

3839

3940
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"
4142
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)
4344

44-
with h5py.File(path_to_nexus_entry, "r") as nexus_file:
45+
with h5py.File(path_to_nexus, "r") as nexus_file:
4546
assert str(nexus_file["scan0034"]["title"].asstr()[...]) == "scan_title_34"
4647
assert nexus_file["scan0034"].attrs["NX_class"] == "NXentry"
48+
os.remove(path_to_nexus)
4749

4850

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)
5256

57+
entries = NexusEntry.from_nexus(path_to_nexus)
5358
scan0034 = entries["scan0034"]
5459
assert scan0034["definition"] == nexus_entries["scan0034"]["definition"]
5560
assert np.allclose(
@@ -75,28 +80,38 @@ def test_from_nexus(nexus_entries):
7580
scan0034.get("instrument/analyser/a2"),
7681
np.array([242.0, 242.1, 242.2]),
7782
)
83+
assert np.allclose(
84+
scan0034.get("data/a1"),
85+
np.array([142.0, 142.1, 142.2]),
86+
)
7887

7988
scan0035 = entries["scan0035"]
8089
assert scan0035.get("title") == "scan_title_35"
90+
os.remove(path_to_nexus)
8191

8292

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)
86100
scan0035 = nexus_entries["scan0035"]
87101
assert scan0035.get("title") == "scan_title_35"
88102

89103

90-
def test_from_nexus_IPTS32124_CG4C_exp0424():
104+
def test_from_nexus_real_data():
91105
path_to_nexus_entry = "./test_data/IPTS32124_CG4C_exp0424/scan0034.h5"
92106
nexus_entries = NexusEntry.from_nexus(path_to_nexus_entry)
107+
93108
scan0034 = nexus_entries["scan0034"]
94109
assert scan0034.get("definition") == "NXtas"
95110
assert scan0034.get("end_time") == "2024-07-03T02:41:28"
96111
assert np.allclose(scan0034.get("s1")[0:3], [36.14, 36.5025, 36.855])
97112

98113

99-
def test_from_spice_IPTS32124_CG4C_exp0424_single_scan():
114+
def test_from_spice_get_one():
100115
path_to_spice_entry = "./test_data/exp424"
101116
scan0034 = NexusEntry.from_spice(path_to_spice_entry, 34)["scan0034"]
102117

@@ -105,7 +120,7 @@ def test_from_spice_IPTS32124_CG4C_exp0424_single_scan():
105120
assert np.allclose(scan0034.get("s1")[0:3], np.array([36.14, 36.5025, 36.855]))
106121

107122

108-
def test_from_spice_IPTS32124_CG4C_exp0424_all_scan():
123+
def test_from_spice_get_all():
109124
path_to_spice_entry = "./test_data/exp424"
110125
scans = NexusEntry.from_spice(path_to_spice_entry)
111126

@@ -120,36 +135,35 @@ def test_from_spice_IPTS32124_CG4C_exp0424_all_scan():
120135

121136

122137
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"
124139
scan0034 = NexusEntry.from_nexus(path_to_nexus_entry, 34)["scan0034"]
125140
assert scan0034.get_metadata_from_daslogs("sense") == "-+-"
126141
assert np.allclose(scan0034.get_data_from_daslogs("s1")[0:3], np.array([36.14, 36.5025, 36.855]))
127142

128143

129-
def test_spice_scan_to_nexus():
144+
def test_spice_to_nexus_one():
130145

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)
134149
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)
136151

152+
with h5py.File(path_to_nexus, "r") as nexus_file:
153+
assert len(nexus_file) == 1
154+
os.remove(path_to_nexus)
137155

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)
141156

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)
153167

154168

155169
@pytest.fixture

tests/test_spice_reader.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ def test_create_spicelogs():
5555
def test_create_spicelogs_single_point():
5656
path_to_spice_data = "./test_data/exp815/Datafiles/HB1_exp0815_scan0001.dat"
5757
spicelogs = _create_spicelogs(path_to_spice_data)
58-
assert len(spicelogs.keys()) == 1 # metadata only, no data columns
58+
# metadata and ubconf only, no data columns
59+
assert "metadata" in spicelogs
60+
assert "ub_conf" in spicelogs
61+
assert len(spicelogs) == 2

tests/test_tavi_data.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ def test_new_tavi_file_at_given_path():
2424
os.remove(tavi_file_name)
2525

2626

27+
def test_new_tavi_file_without_path():
28+
tavi = TAVI()
29+
tavi.new_file()
30+
tavi.save_file()
31+
32+
with h5py.File(tavi.file_path, "r") as f:
33+
keys = [key for key in f["/"].keys()]
34+
# check if groups preserves the order
35+
assert keys[1] == "processed_data"
36+
# delete file
37+
os.remove(tavi.file_path)
38+
39+
40+
# TODO
41+
def test_load_spice_data_from_disk():
42+
tavi = TAVI()
43+
tavi_file_name = "./test_data/tavi_test_exp424.h5"
44+
tavi.new_file(tavi_file_name)
45+
46+
2747
def test_load_nexus_data_from_disk():
2848
tavi = TAVI()
2949
tavi_file_name = "./test_data/tavi_test_exp424.h5"
@@ -55,16 +75,3 @@ def test_open_tavi_file():
5575

5676
assert len(scans := tavi.data["IPTS32124_CG4C_exp0424"]) == 92
5777
assert scans["scan0001"].scan_info.scan_num == 1
58-
59-
60-
def test_new_tavi_file_without_path():
61-
tavi = TAVI()
62-
tavi.new_file()
63-
tavi.save_file()
64-
65-
with h5py.File(tavi.file_path, "r") as f:
66-
keys = [key for key in f["/"].keys()]
67-
# check if groups preserves the order
68-
assert keys[1] == "processed_data"
69-
# delete file
70-
os.remove(tavi.file_path)

0 commit comments

Comments
 (0)