Skip to content

Commit

Permalink
updated spice_to_nexus.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Aug 28, 2024
1 parent fd1aec3 commit 9c28f38
Show file tree
Hide file tree
Showing 17 changed files with 416 additions and 385 deletions.
2 changes: 1 addition & 1 deletion scripts/test_data_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tavi.data.spice_to_nexus import convert_spice_to_nexus
from tavi.data.tavi import TAVI

Expand Down
710 changes: 349 additions & 361 deletions src/tavi/data/spice_to_nexus.py

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/tavi/data/tavi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def __init__(self):
def new_tavi_file(self, file_path):
"""Create a new tavi file"""
self.file_path = file_path
h5py.get_config().track_order = True
with h5py.File(file_path, "w") as root:
root.create_group("data")
root.create_group("processed_data")
root.create_group(
"fits",
)
root.create_group("plots")

try:
with h5py.File(file_path, "w", track_order=True) as root:
root.create_group("data", track_order=True)
root.create_group("processed_data", track_order=True)
root.create_group("fits", track_order=True)
root.create_group("plots", track_order=True)
except OSError:
print(f"Cannot create tavi file at {file_path}")

def load_nexus_data_from_disk(self, path_to_hdf5):
"""Load hdf5 data from path_to_hdf5.
Expand Down
Binary file modified test_data/nexus_exp1031.h5
Binary file not shown.
Binary file modified test_data/nexus_exp416.h5
Binary file not shown.
Binary file modified test_data/nexus_exp424.h5
Binary file not shown.
Binary file modified test_data/nexus_exp710.h5
Binary file not shown.
Binary file removed test_data/nexus_exp758.h5
Binary file not shown.
Binary file added test_data/nexus_exp815.h5
Binary file not shown.
Binary file modified test_data/nexus_exp932.h5
Binary file not shown.
Binary file modified test_data/tavi_test.h5
Binary file not shown.
Binary file removed test_data/tavi_test_exp1031.h5
Binary file not shown.
Binary file removed test_data/tavi_test_exp410.h5
Binary file not shown.
Binary file removed test_data/tavi_test_exp424.h5
Binary file not shown.
Binary file removed test_data/tavi_test_exp710.h5
Binary file not shown.
15 changes: 0 additions & 15 deletions tests/test_tavi.py

This file was deleted.

57 changes: 57 additions & 0 deletions tests/test_tavi_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*
import h5py
import numpy as np

from tavi.data.spice_to_nexus import _format_spice_header, _read_spice, _read_spice_ub, convert_spice_to_nexus
from tavi.data.tavi import TAVI


def test_read_spice():
spice_file = "./test_data/exp424/Datafiles/CG4C_exp0424_scan0001.dat"
spice_data, col_headers, headers, unused = _read_spice(spice_file)
assert spice_data.shape == (2, 55)
assert headers["scan_title"] == ""
assert len(unused) == 0

# unfinished scan
spice_file = "./test_data/exp416/Datafiles/CG4C_exp0416_scan0050.dat"
spice_data, col_headers, headers, unused = _read_spice(spice_file)
assert spice_data.shape == (16, 56)


def test_read_spice_ub():
spice_ub_file = "./test_data/exp424/UBConf/UB02Jul2024_14108PM.ini"
ubconf = _read_spice_ub(spice_ub_file)
assert np.allclose(ubconf["Energy"], 4.8)
assert len(ubconf) == 13


def test_format_spice_header():
spice_file = "./test_data/exp424/Datafiles/CG4C_exp0424_scan0001.dat"
_, _, headers, _ = _read_spice(spice_file)
formatted_headers = _format_spice_header(headers)
assert "COM" in formatted_headers.keys()
assert isinstance(formatted_headers["scan"], int)


def test_spice_to_nexus_conversion():
exp_nums = [416, 424, 710, 815, 932, 1031]

for exp_num in exp_nums:
spice_folder = f"./test_data/exp{exp_num}/"
nexus_file_name = f"./test_data/nexus_exp{exp_num}.h5"
convert_spice_to_nexus(spice_folder, nexus_file_name)


def test_new_tavi_file():
tavi = TAVI()
tavi_file_name = "./test_data/tavi_test.h5"
tavi.new_tavi_file(tavi_file_name)

with h5py.File(tavi_file_name, "r") as f:
keys = [key for key in f["/"].keys()]
# check if groups preserves the order
assert keys[0] == "data"
assert keys[1] == "processed_data"
assert keys[2] == "fits"
assert keys[3] == "plots"

0 comments on commit 9c28f38

Please sign in to comment.