Skip to content

Commit

Permalink
starting the implementation of NexusEntry class
Browse files Browse the repository at this point in the history
  • Loading branch information
bingli621 committed Sep 23, 2024
1 parent 9fdeb5d commit 4845925
Show file tree
Hide file tree
Showing 106 changed files with 186 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
65 changes: 65 additions & 0 deletions src/tavi/data/nxentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import h5py


class NexusEntry(dict):

@staticmethod
def _getitem_recursively(obj, key, ATTRS):
"find key in obj recursively, return None if nonexsiting"
value = None
if key in obj:
if ATTRS:
try:
value = obj[key]["attrs"]
except KeyError:
print("Attribute does not exist.")
else:
try:
value = obj[key]["dataset"]
except KeyError:
print("Dataset does not exist.")

for k, v in obj.items():
if value is not None:
break
if k == "attrs" or k == "dataset":
continue

if isinstance(v, dict):
value = NexusEntry._getitem_recursively(v, key, ATTRS)

return value

def get(self, key, ATTRS=False, default=None):
"""
Return dataset spicified by key regardless of the hierarchy.
Return attributes instead if ATTRS is True.
Note:
Only works if the key is unique
"""
value = NexusEntry._getitem_recursively(self, key, ATTRS)
if value is not None:
return value
else:
return default

@staticmethod
def _write_recursively(items, nexus_entry):
"""write items to nexus entry recursively"""
for key, value in items.items():
if key == "attrs":
for attr_key, attr_value in value.items():
nexus_entry.attrs[attr_key] = attr_value
else:
if isinstance(value, dict):
if "dataset" in value.keys():
ds = nexus_entry.create_dataset(name=key, data=value["dataset"], maxshape=None)
NexusEntry._write_recursively(value, ds)
else:
grp = nexus_entry.create_group(key + "/")
NexusEntry._write_recursively(value, grp)

def to_nexus(self, path_to_nexus):
with h5py.File(path_to_nexus, "w") as nexus_file:
NexusEntry._write_recursively(self, nexus_file)
Binary file modified test_data/.DS_Store
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0001.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0002.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0003.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0004.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0005.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0006.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0007.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0008.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0009.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0010.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0011.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0012.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0013.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0014.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0015.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0016.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0017.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0018.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0019.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0020.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0021.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0022.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0023.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0024.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0025.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0026.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0027.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0028.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0029.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0030.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0031.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0032.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0033.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0034.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0035.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0036.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0037.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0038.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0039.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0040.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0041.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0042.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0043.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0044.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0045.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0046.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0047.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0048.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0049.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0050.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0051.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0052.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0053.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0054.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0055.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0056.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0057.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0058.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0059.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0060.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0061.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0062.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0063.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0064.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0065.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0066.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0067.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0068.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0069.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0070.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0071.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0072.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0073.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0074.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0075.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0076.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0077.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0078.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0079.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0080.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0081.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0082.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0083.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0084.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0085.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0086.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0087.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0088.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0089.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0090.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0091.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0092.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0001.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0002.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0003.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0004.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0005.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0006.h5
Binary file not shown.
Binary file modified test_data/exp416/.DS_Store
Binary file not shown.
Binary file added test_data/exp813/.DS_Store
Binary file not shown.
Binary file added test_data/scan_to_nexus_test.h5
Binary file not shown.
Binary file modified test_data/tavi_test_exp424.h5
Binary file not shown.
121 changes: 121 additions & 0 deletions tests/test_nxentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import h5py
import numpy as np
import pytest

from tavi.data.nxentry import NexusEntry


def test_get_dataset(nexus_entry):
assert nexus_entry.get("definition") == "NXtas"
assert np.allclose(nexus_entry.get("a2"), np.array([242.0, 242.1, 242.2]))
assert nexus_entry.get("data", ATTRS=True) == {
"EX_required": "true",
"NX_class": "NXdata",
"axes": "en",
"signal": "detector",
}
assert nexus_entry.get("a3") is None
assert nexus_entry.get("detector") is None


def test_to_nexus(nexus_entry):
path_to_nexus_entry = "./test_data/scan_to_nexus_test.h5"
nexus_entry.to_nexus(path_to_nexus_entry)

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


@pytest.fixture
def nexus_entry():

analyser = {
"attrs": {
"EX_required": "true",
"NX_class": "NXcrystal",
"type": "NX_CHAR",
},
"a1": {
"attrs": {
"EX_required": "true",
"units": "degrees",
},
"dataset": np.array([142.0, 142.1, 142.2]),
},
"a2": {
"attrs": {
"EX_required": "true",
"units": "degrees",
},
"dataset": np.array([242.0, 242.1, 242.2]),
},
"sense": {"dataset": "-"},
"type": {"dataset": "Pg002"},
}
detector = {
"attrs": {
"EX_required": "true",
"NX_class": "NXdetector",
},
"data": {
"attrs": {
"EX_required": "true",
"type": "NX_INT",
"units": "counts",
},
},
}

instrument = {
"analyser": analyser,
"detector": detector,
}
monitor = {
"data": {
"attrs": {
"EX_required": "true",
"type": "NX_FLOAT",
},
"dataset": np.array([60, 60, 60]),
},
"attrs": {
"EX_required": "true",
"NX_class": "NXinstrument",
},
}

nexus_entry = NexusEntry(
[
(
"scan0034",
{
"attrs": {
"EX_required": "true",
"NX_class": "NXentry",
"dataset_name": "IPTS32124_CG4C_exp0424",
},
"definition": {
"attrs": {"EX_required": "true", "type": "NX_CHAR"},
"dataset": "NXtas",
},
"title": {
"attrs": {"EX_required": "true", "type": "NX_CHAR"},
"dataset": "scan_title",
},
"data": {
"attrs": {
"EX_required": "true",
"NX_class": "NXdata",
"axes": "en",
"signal": "detector",
}
},
"instrument": instrument,
"monitor": monitor,
},
)
]
)

return nexus_entry

0 comments on commit 4845925

Please sign in to comment.