Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #688 from ess-dmsc/687_create_entry_and_instrument…
Browse files Browse the repository at this point in the history
…_if_required

Create entry and instrument if required
  • Loading branch information
rerpha authored Mar 16, 2020
2 parents d1a5c9f + 42713cf commit deb5cd7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 0 additions & 1 deletion nexus_constructor/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def _load_current_entry():
self.instrument.nexus.load_file(
map_of_entries[combo.currentText()], nexus_file
)
self._set_up_component_model()
self._update_views()

# Connect the clicked signal of the ok_button to instrument.load_file and pass the file and entry group object.
Expand Down
20 changes: 13 additions & 7 deletions nexus_constructor/nexus/nexus_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from nexus_constructor.common_attrs import CommonAttrs

h5Node = TypeVar("h5Node", h5py.Group, h5py.Dataset)
DEFAULT_INSTRUMENT = "instrument"
DEFAULT_ENTRY = "entry"


def set_up_in_memory_nexus_file(filename: str) -> h5py.File:
Expand Down Expand Up @@ -75,8 +77,8 @@ class NexusWrapper(QObject):
def __init__(
self,
filename: str = "NeXus File",
entry_name: str = "entry",
instrument_name: str = "instrument",
entry_name: str = DEFAULT_ENTRY,
instrument_name: str = DEFAULT_INSTRUMENT,
):
super().__init__()
self.nexus_file = set_up_in_memory_nexus_file(filename)
Expand Down Expand Up @@ -131,7 +133,7 @@ def load_nexus_file(self, nexus_file: h5py.File):
self.file_opened.emit(nexus_file)
return entries

def find_entries_in_file(self, nexus_file: h5py.File):
def find_entries_in_file(self, nexus_file: h5py.File) -> bool:
"""
Find the entry group in the specified nexus file. If there are multiple, emit the signal required to
show the multiple entry selection dialog in the UI.
Expand All @@ -148,9 +150,13 @@ def append_nx_entries_to_list(name, node):
if len(entries_in_root.keys()) > 1:
self.show_entries_dialog.emit(entries_in_root, nexus_file)
return False
else:
elif len(entries_in_root.keys()) == 1:
self.load_file(list(entries_in_root.values())[0], nexus_file)
return True
else:
new_entry = self.create_nx_group(DEFAULT_ENTRY, "NXentry", nexus_file)
self.load_file(new_entry, nexus_file)
return True

def load_file(self, entry: h5py.Group, nexus_file: h5py.File):
"""
Expand Down Expand Up @@ -351,13 +357,13 @@ def create_transformations_group_if_does_not_exist(self, parent_group: h5Node):
"transformations", "NXtransformations", parent_group
)

@staticmethod
def get_instrument_group_from_entry(entry: h5py.Group) -> h5py.Group:
def get_instrument_group_from_entry(self, entry: h5py.Group) -> h5py.Group:
"""
Get the first NXinstrument object from an entry group.
Get the first NXinstrument object from an entry group, or create one if one doesn't exist
:param entry: The entry group object to search for the instrument group in.
:return: the instrument group object.
"""
for node in entry.values():
if isinstance(node, h5py.Group) and get_nx_class(node) == "NXinstrument":
return node
return self.create_nx_group(DEFAULT_INSTRUMENT, "NXinstrument", entry)
10 changes: 10 additions & 0 deletions tests/test_nexus_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def test_GIVEN_single_entry_group_with_instrument_group_WHEN_finding_entry_THEN_
assert wrapper.instrument == inst_group


def test_GIVEN_no_entry_or_instrument_in_file_WHEN_finding_entry_THEN_default_entry_and_instrument_are_created(
file,
):
wrapper = NexusWrapper(filename="test_nw5")
wrapper.find_entries_in_file(file)

assert isinstance(wrapper.entry, h5py.Group)
assert isinstance(wrapper.instrument, h5py.Group)


def test_GIVEN_multiple_entry_groups_in_file_WHEN_finding_entry_THEN_signal_is_emitted_with_entry_options(
file,
):
Expand Down

0 comments on commit deb5cd7

Please sign in to comment.