Skip to content

Commit

Permalink
updated scan class
Browse files Browse the repository at this point in the history
  • Loading branch information
bingli621 committed Oct 7, 2024
1 parent 703cfd8 commit 2836ded
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 74 deletions.
Binary file modified .DS_Store
Binary file not shown.
36 changes: 18 additions & 18 deletions src/tavi/data/nxentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,24 @@ def to_nexus(self, path_to_nexus: str, name="scan") -> None:
with h5py.File(path_to_nexus, "a") as nexus_file:
scan_grp = nexus_file.require_group(name + "/")
NexusEntry._write_recursively(self, scan_grp)
# create soft link for data
def_y = nexus_file[name]["data"].attrs["signal"]
def_x = nexus_file[name]["data"].attrs["axes"]
path_y = _find_val_path(def_y, nexus_file)
path_x = _find_val_path(def_x, nexus_file)

if path_y is not None:
def_y = "data/" + def_y
if isinstance(scan_grp.get(def_y), h5py.Dataset):
del scan_grp[def_y]
scan_grp[def_y] = h5py.SoftLink(path_y)
scan_grp[def_y + "/"].attrs["target"] = path_y
if path_x is not None:
def_x = "data/" + def_x
if isinstance(scan_grp.get(def_x), h5py.Dataset):
del scan_grp[def_x]
scan_grp[def_x] = h5py.SoftLink(path_x)
scan_grp[def_x + "/"].attrs["target"] = path_x
if "data" in scan_grp: # create soft link for data
def_y = scan_grp["data"].attrs["signal"]
def_x = scan_grp["data"].attrs["axes"]
path_y = _find_val_path(def_y, nexus_file)
path_x = _find_val_path(def_x, nexus_file)

if path_y is not None:
def_y = "data/" + def_y
if isinstance(scan_grp.get(def_y), h5py.Dataset):
del scan_grp[def_y]
scan_grp[def_y] = h5py.SoftLink(path_y)
scan_grp[def_y + "/"].attrs["target"] = path_y
if path_x is not None:
def_x = "data/" + def_x
if isinstance(scan_grp.get(def_x), h5py.Dataset):
del scan_grp[def_x]
scan_grp[def_x] = h5py.SoftLink(path_x)
scan_grp[def_x + "/"].attrs["target"] = path_x

# Create the ATTRIBUTES
scan_grp.attrs["file_name"] = os.path.abspath(path_to_nexus)
Expand Down
10 changes: 6 additions & 4 deletions src/tavi/data/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class ScanInfo:
"""Metadata containing scan information"""

scan_num: Optional[int] = None
scan_title: str = ""
def_y: str = "detector"
def_x: str = "s1"
start_time: Optional[str] = None
end_time: Optional[str] = None
scan_title: str = ""
# preset_type: str = "normal"
preset_type: str = "normal"
preset_channel: str = "time"
preset_value: float = 1.0
def_y: str = "detector"
def_x: str = "s1"


@dataclass
Expand All @@ -29,6 +29,8 @@ class SampleUBInfo:
sample_name: str = ""
lattice_constants: tuple = (1.0, 1.0, 1.0, 90.0, 90.0, 90.0)
ub_matrix: Optional[np.ndarray] = None
u: Optional[np.ndarray] = None
v: Optional[np.ndarray] = None
# ub_mode: int = 0 # mode for UB determination in SPICE
# angle_mode: int = 0 # mode for goni angle calculation in SPICE
plane_normal: Optional[np.ndarray] = None
Expand Down
14 changes: 11 additions & 3 deletions src/tavi/data/spice_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def read_spice_ubconf(ub_file_name: str) -> dict:
Returns:
"""
ubconf: dict[str, Any] = {}
with open(ub_file_name, encoding="utf-8") as f:
with open(ub_file_name, 'r', encoding="utf-8") as f:
all_content = f.readlines()

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

scan_path = os.path.abspath(path_to_scan_file)
(*folder_path, _, _) = scan_path.split("/")
ub_file_path = "/".join(folder_path + ["UBConf", metadata["ubconf"]])
ub_conf_dict = {"file_path": ub_file_path}
ub_file_path = os.path.join('/',*folder_path,"UBConf", metadata["ubconf"])
ub_temp_file_path = os.path.join('/',*folder_path,"UBConf", "temp", metadata["ubconf"])

if os.path.isfile(ub_file_path):
ub_conf_dict = {"file_path": ub_file_path}
elif os.path.isfile(ub_temp_file_path):
ub_conf_dict = {"file_path": ub_temp_file_path}
else:
ub_conf_dict = {"file_path": ""}
print(f"Cannot find UB file {metadata["ubconf"]}")
ubconf = read_spice_ubconf(ub_file_path)
for k, v in ubconf.items():
ub_conf_dict.update({k: v})
Expand Down
2 changes: 1 addition & 1 deletion src/tavi/data/tavi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import h5py

from tavi.data.scan import Scan
from tavi.data.scan_group import ScanGroup
from tavi.data.scan_old.scan import Scan


class TAVI(object):
Expand Down
2 changes: 1 addition & 1 deletion src/tavi/instrument/tas_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Optional, Union

from tavi.data.scan_old.scan import Scan
from tavi.data.scan import Scan
from tavi.instrument.goni import Goniometer
from tavi.instrument.mono_ana import MonoAna
from tavi.instrument.tas_cmponents import Collimators, Detector, Distances, Guide, Monitor, Source
Expand Down
Binary file modified test_data/.DS_Store
Binary file not shown.
Binary file modified test_data/exp424/.DS_Store
Binary file not shown.
Binary file modified test_data/scan_to_nexus_test.h5
Binary file not shown.
Binary file removed test_data/spice_to_nexus_test_scan34.h5
Binary file not shown.
Binary file removed test_data/spice_to_nxdict_test_scan0034.h5
Binary file not shown.
80 changes: 47 additions & 33 deletions tests/test_nxentry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os

import h5py
import numpy as np
import pytest

from tavi.data.nxdict import spice_scan_to_nxdict
from tavi.data.nxentry import NexusEntry


Expand Down Expand Up @@ -37,19 +38,23 @@ def test_get_dataset(nexus_entries):


def test_to_nexus(nexus_entries):
path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
path_to_nexus = "./test_data/scan_to_nexus_test.h5"
for scan_num, nexus_entry in nexus_entries.items():
nexus_entry.to_nexus(path_to_nexus_entry, scan_num)
nexus_entry.to_nexus(path_to_nexus, scan_num)

with h5py.File(path_to_nexus_entry, "r") as nexus_file:
with h5py.File(path_to_nexus, "r") as nexus_file:
assert str(nexus_file["scan0034"]["title"].asstr()[...]) == "scan_title_34"
assert nexus_file["scan0034"].attrs["NX_class"] == "NXentry"
os.remove(path_to_nexus)


def test_from_nexus(nexus_entries):
path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
entries = NexusEntry.from_nexus(path_to_nexus_entry)
def test_from_nexus_get_all(nexus_entries):
# generate NeXus file
path_to_nexus = "./test_data/scan_to_nexus_test.h5"
for scan_num, nexus_entry in nexus_entries.items():
nexus_entry.to_nexus(path_to_nexus, scan_num)

entries = NexusEntry.from_nexus(path_to_nexus)
scan0034 = entries["scan0034"]
assert scan0034["definition"] == nexus_entries["scan0034"]["definition"]
assert np.allclose(
Expand All @@ -75,28 +80,38 @@ def test_from_nexus(nexus_entries):
scan0034.get("instrument/analyser/a2"),
np.array([242.0, 242.1, 242.2]),
)
assert np.allclose(
scan0034.get("data/a1"),
np.array([142.0, 142.1, 142.2]),
)

scan0035 = entries["scan0035"]
assert scan0035.get("title") == "scan_title_35"
os.remove(path_to_nexus)


def test_from_nexus_single_scan():
path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
nexus_entries = NexusEntry.from_nexus(path_to_nexus_entry, scan_num=35)
def test_from_nexus_get_one(nexus_entries):
# generate NeXus
path_to_nexus = "./test_data/scan_to_nexus_test.h5"
for scan_num, nexus_entry in nexus_entries.items():
nexus_entry.to_nexus(path_to_nexus, scan_num)

nexus_entries = NexusEntry.from_nexus(path_to_nexus, scan_num=35)
scan0035 = nexus_entries["scan0035"]
assert scan0035.get("title") == "scan_title_35"


def test_from_nexus_IPTS32124_CG4C_exp0424():
def test_from_nexus_real_data():
path_to_nexus_entry = "./test_data/IPTS32124_CG4C_exp0424/scan0034.h5"
nexus_entries = NexusEntry.from_nexus(path_to_nexus_entry)

scan0034 = nexus_entries["scan0034"]
assert scan0034.get("definition") == "NXtas"
assert scan0034.get("end_time") == "2024-07-03T02:41:28"
assert np.allclose(scan0034.get("s1")[0:3], [36.14, 36.5025, 36.855])


def test_from_spice_IPTS32124_CG4C_exp0424_single_scan():
def test_from_spice_get_one():
path_to_spice_entry = "./test_data/exp424"
scan0034 = NexusEntry.from_spice(path_to_spice_entry, 34)["scan0034"]

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


def test_from_spice_IPTS32124_CG4C_exp0424_all_scan():
def test_from_spice_get_all():
path_to_spice_entry = "./test_data/exp424"
scans = NexusEntry.from_spice(path_to_spice_entry)

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


def test_get_from_daslogs():
path_to_nexus_entry = "./test_data/spice_to_nexus_test_scan34.h5"
path_to_nexus_entry = "./test_data/IPTS32124_CG4C_exp0424/scan0034.h5"
scan0034 = NexusEntry.from_nexus(path_to_nexus_entry, 34)["scan0034"]
assert scan0034.get_metadata_from_daslogs("sense") == "-+-"
assert np.allclose(scan0034.get_data_from_daslogs("s1")[0:3], np.array([36.14, 36.5025, 36.855]))


def test_spice_scan_to_nexus():
def test_spice_to_nexus_one():

path_to_spice_entry = "./test_data/exp424"
path_to_nexus_entry = "./test_data/spice_to_nxdict_test_scan0034.h5"
scan0034 = NexusEntry.from_spice(path_to_spice_entry, 34)
path_to_spice_folder = "./test_data/exp424"
path_to_nexus = "./test_data/spice_to_nxdict_test_scan0034.h5"
scan0034 = NexusEntry.from_spice(path_to_spice_folder, 34)
for scan_name, scan in scan0034.items():
scan.to_nexus(path_to_nexus_entry, name=scan_name)
scan.to_nexus(path_to_nexus, name=scan_name)

with h5py.File(path_to_nexus, "r") as nexus_file:
assert len(nexus_file) == 1
os.remove(path_to_nexus)

def test_spice_data_to_nexus():
path_to_spice_data = "./test_data/exp424"
scan_dict = spice_scan_to_nxdict(path_to_spice_data)

entries = {"scan0034": nxdict}
nexus_entries = {}
for scan_num, scan_content in entries.items():
content_list = []
for key, val in scan_content.items():
content_list.append((key, val))
nexus_entries.update({scan_num: NexusEntry(content_list)})
# generate h5 file
path_to_nexus_entry = "./test_data/spice_to_nxdict_test_scan0034.h5"
for scan_num, nexus_entry in nexus_entries.items():
nexus_entry.to_nexus(path_to_nexus_entry, scan_num)
def test_spice_to_nexus_all():
path_to_spice_folder = "./test_data/exp424"
path_to_nexus = "./test_data/spice_to_nxdict_test_all.h5"
scan_dict = NexusEntry.from_spice(path_to_spice_folder)

for scan_name, scan in scan_dict.items():
scan.to_nexus(path_to_nexus, name=scan_name)
with h5py.File(path_to_nexus, "r") as nexus_file:
assert len(nexus_file) == 92
os.remove(path_to_nexus)


@pytest.fixture
Expand Down
5 changes: 4 additions & 1 deletion tests/test_spice_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ def test_create_spicelogs():
def test_create_spicelogs_single_point():
path_to_spice_data = "./test_data/exp815/Datafiles/HB1_exp0815_scan0001.dat"
spicelogs = _create_spicelogs(path_to_spice_data)
assert len(spicelogs.keys()) == 1 # metadata only, no data columns
# metadata and ubconf only, no data columns
assert "metadata" in spicelogs
assert "ub_conf" in spicelogs
assert len(spicelogs) == 2
33 changes: 20 additions & 13 deletions tests/test_tavi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ def test_new_tavi_file_at_given_path():
os.remove(tavi_file_name)


def test_new_tavi_file_without_path():
tavi = TAVI()
tavi.new_file()
tavi.save_file()

with h5py.File(tavi.file_path, "r") as f:
keys = [key for key in f["/"].keys()]
# check if groups preserves the order
assert keys[1] == "processed_data"
# delete file
os.remove(tavi.file_path)


# TODO
def test_load_spice_data_from_disk():
tavi = TAVI()
tavi_file_name = "./test_data/tavi_test_exp424.h5"
tavi.new_file(tavi_file_name)


def test_load_nexus_data_from_disk():
tavi = TAVI()
tavi_file_name = "./test_data/tavi_test_exp424.h5"
Expand Down Expand Up @@ -55,16 +75,3 @@ def test_open_tavi_file():

assert len(scans := tavi.data["IPTS32124_CG4C_exp0424"]) == 92
assert scans["scan0001"].scan_info.scan_num == 1


def test_new_tavi_file_without_path():
tavi = TAVI()
tavi.new_file()
tavi.save_file()

with h5py.File(tavi.file_path, "r") as f:
keys = [key for key in f["/"].keys()]
# check if groups preserves the order
assert keys[1] == "processed_data"
# delete file
os.remove(tavi.file_path)

0 comments on commit 2836ded

Please sign in to comment.