Unverified Commit 77ea7fe3 authored by DavidFair's avatar DavidFair Committed by GitHub
Browse files

Merge pull request #31727 from mantidproject/31725_d7_logbook

User requests for D7 logbook contents
parents 122941bc 7c138839
......@@ -194,7 +194,7 @@ class GenerateLogbook(PythonAlgorithm):
def _get_entries(self):
"""Gets default and optional metadata entries using the specified instrument IPF."""
self._metadata_entries = []
self._metadata_headers = [('d', 'run_number')]
self._metadata_headers = [('s', 'file_name')]
tmp_instr = self._instrument + '_tmp'
# Load empty instrument to access parameters defining metadata entries to be searched
LoadEmptyInstrument(Filename=self._instrument + "_Definition.xml", OutputWorkspace=tmp_instr)
......@@ -238,8 +238,8 @@ class GenerateLogbook(PythonAlgorithm):
logbook_ws = self.getPropertyValue('OutputWorkspace')
CreateEmptyTableWorkspace(OutputWorkspace=logbook_ws)
type_dict = {'s': 'str', 'd': 'int', 'f': 'float'}
for type, headline in self._metadata_headers:
mtd[logbook_ws].addColumn(type_dict[type], headline)
for header_type, headline in self._metadata_headers:
mtd[logbook_ws].addColumn(type_dict[header_type], headline)
return logbook_ws
def _perform_binary_operations(self, values, binary_operations, operations):
......@@ -251,8 +251,17 @@ class GenerateLogbook(PythonAlgorithm):
if operation == list():
break
ind1, ind2, op = operation[0]
if values[ind2] is None or values[ind2] is str():
if op != "+" or op == "+" and values[ind1] in [None, str()]:
values[ind1] = "N/A"
values.pop(ind2)
binary_operations.pop(ind1)
continue
if op == "+":
new_val = values[ind1] + values[ind2]
padding = 0
if type(values[ind1]) == str:
padding = " "
new_val = values[ind1] + padding + values[ind2]
elif op == "-":
new_val = values[ind1] - values[ind2]
elif op == "*":
......@@ -265,6 +274,8 @@ class GenerateLogbook(PythonAlgorithm):
new_val = values[ind1] / values[ind2]
else:
raise RuntimeError("Unknown operation: {}".format(operation))
if type(new_val) == str():
new_val = new_val.strip()
values[ind1] = new_val
values.pop(ind2)
binary_operations.pop(ind1)
......@@ -315,7 +326,7 @@ class GenerateLogbook(PythonAlgorithm):
"""Fills out the logbook with the requested meta-data."""
n_entries = len(self._metadata_headers)
entry_not_found_msg = "The requested entry: {}, is not present in the raw data"
operators = ["+","-","*","//"]
operators = ["+", "-", "*", "//"]
cache_entries_ops = {}
for file_no, file_name in enumerate(data_array):
......@@ -323,9 +334,10 @@ class GenerateLogbook(PythonAlgorithm):
if file_no % (len(data_array)/10) == 0:
progress.report("Filling logbook table...")
file_path = os.path.join(self._data_directory, file_name + '.nxs')
with h5py.File(file_path, 'r') as f:
rowData = np.empty(n_entries, dtype=object)
rowData[0] = int(file_name)
rowData[0] = str(file_name)
for entry_no, entry in enumerate(self._metadata_entries, 1):
if any(op in entry for op in operators):
if entry in cache_entries_ops:
......@@ -341,10 +353,9 @@ class GenerateLogbook(PythonAlgorithm):
split_entry, index = self._get_index(split_entry)
data = f.get(split_entry)[index]
except TypeError:
values[0] = "Not found"
binary_operations = []
values[split_entry_no] = ""
self.log().warning(entry_not_found_msg.format(entry))
break
continue
else:
if isinstance(data, np.bytes_):
if any(op in operators[1:] for op in binary_operations):
......
......@@ -42,29 +42,29 @@ class GenerateLogbookTest(unittest.TestCase):
GenerateLogbook(Directory=self._data_directory,
OutputWorkspace='default_logbook', Facility='ILL', Instrument='D7',
NumorRange="396990:396993")
self._check_output('default_logbook', numberEntries=3, numberColumns=6)
self._check_output('default_logbook', numberEntries=3, numberColumns=7)
def test_d7_optional(self):
self.assertTrue(os.path.exists(self._data_directory))
GenerateLogbook(Directory=self._data_directory,
OutputWorkspace='optional_logbook', Facility='ILL', Instrument='D7',
NumorRange="396990:396993", OptionalHeaders='TOF')
self._check_output('optional_logbook', numberEntries=3, numberColumns=7)
self._check_output('optional_logbook', numberEntries=3, numberColumns=8)
def test_d7_custom(self):
self.assertTrue(os.path.exists(self._data_directory))
GenerateLogbook(Directory=self._data_directory,
OutputWorkspace='custom_logbook', Facility='ILL', Instrument='D7',
NumorRange="396990:396993", CustomEntries='/entry0/acquisition_mode')
self._check_output('custom_logbook', numberEntries=3, numberColumns=7)
self._check_output('custom_logbook', numberEntries=3, numberColumns=8)
def test_d7_custom_with_summing(self):
self.assertTrue(os.path.exists(self._data_directory))
GenerateLogbook(Directory=self._data_directory,
OutputWorkspace='custom_logbook_w_summing', Facility='ILL', Instrument='D7',
NumorRange="396990:396993", CustomEntries='/entry0/acquisition_mode',
OptionalHeaders='polarisation')
self._check_output('custom_logbook_w_summing', numberEntries=3, numberColumns=8)
OptionalHeaders='wavelength')
self._check_output('custom_logbook_w_summing', numberEntries=3, numberColumns=9)
def test_d7_save_csv(self):
self.assertTrue(os.path.exists(self._data_directory))
......
c96686dbc789453baf978772ba5c529d
41d992ce477498b787c8ac8df638b535
22eefc6ba2019ad5c7ea5c844992b77e
fac3a3c5a67056d6560a937cc4e4db15
b0563d7ce052f4a9b56cd4e79960b023
94db7763125ee3e9123dd4b113aef816
f155940307563b8c2f3b910a6645ef86
f3792f842d3aee87641c7b16364461b8
39cbf889548d799e34b1a4617367e47e
6f0716a8d2b13d75c30816c08d85e3d3
d879e1f85261841dfb96326a2d758682
de0abc04c0dc5072223c43063e2a02cf
362e6ff25c25bda2d07b95cdf65710a4
e02ceb49630e7b61a74e4596a5e90430
2570b1c0ef3989fe6d889b5ab17f1571
421f3178147b502f1db90026be7c9b17
83893c13ab0191b3230ece1aab7e354f
03098ae09611c755f1908cb26240f074
......@@ -29,6 +29,10 @@ Basic binary arithmetic operations: addition (the only one supported if the data
multiplication and division on entries data are supported. No support is offered for organizing the operations using parentheses
or scaling by read data by constant numbers.
Binary operations can also be defined for entries that are not always present in the data, for example in the case of D7 at the ILL,
NeXus files contain a varying number of entries, from 1 to 6. There, by defining a sum over all possible entries containing polarisation
identifier, it is possible to get to display in the logbook which polarisations are actually contained in the data file.
The logbook can be stored as a CSV file and read outside of the Mantid using spreadsheet software, such as Microsoft Excel.
Usage
......@@ -55,7 +59,7 @@ Output:
.. testoutput:: ExGenerateLogbook_D7
Number of numors in the logbook: 3
Number of headers in the logbook: 8
Number of headers in the logbook: 9
.. testcleanup:: ExGenerateLogbook_D7
......@@ -71,7 +75,7 @@ Output:
GenerateLogbook(Directory=data_directory,
OutputWorkspace='d7_logbook', Facility='ILL', Instrument='D7',
NumorRange="396990:396993", CustomHeaders='polarisation',
NumorRange="396990:396993", CustomHeaders='wavelength',
CustomEntries='/entry0/D7/POL/actual_state+/entry0/D7/POL/actual_stateB1B2')
print("Number of numors in the logbook: {}".format(len(mtd['d7_logbook'].column(0))))
print("Number of headers in the logbook: {}".format(len(mtd['d7_logbook'].row(0))))
......@@ -81,7 +85,7 @@ Output:
.. testoutput:: ExGenerateLogbook_D7_binary_operations
Number of numors in the logbook: 3
Number of headers in the logbook: 7
Number of headers in the logbook: 8
.. testcleanup:: ExGenerateLogbook_D7_binary_operations
......
......@@ -15,6 +15,12 @@ Concepts
Algorithms
----------
Improvements
############
- :ref:`GenerateLogbook <algm-GenerateLogbook>` now allows to perform binary operations even when certain entries do not exist, e.g. to create a string with all polarisation orientations contained in a collection of data files
Data Objects
------------
......
......@@ -25,15 +25,16 @@
</parameter>
<parameter name="logbook_default_parameters" type="string" visible="false">
<value val="wavelength:/entry0/D7/monochromator/wavelength:f,
<value val="proposal_number:/entry0/user/proposal:s,
experiment_identifier:/entry0/experiment_identifier:s,
start_time:/entry0/start_time:s,
end_time:/entry0/end_time:s,
duration:/entry0/duration:d" />
total_duration:/entry0/duration+/entry1/duration+/entry2/duration+/entry3/duration+/entry4/duration+/entry5/duration:d,
count_repeater:/entry0/duration//entry1/duration:f,
polarisation:/entry0/D7/POL/actual_state+/entry2/D7/POL/actual_state+/entry4/D7/POL/actual_state:s" />
</parameter>
<parameter name="logbook_optional_parameters" type="string" visible="false">
<value val="TOF:/entry0/acquisition_mode:d,
polarisation:/entry0/D7/POL/actual_state+/entry0/D7/POL/actual_stateB1B2:s" />
<value val="wavelength:/entry0/D7/monochromator/wavelength:f,
TOF:/entry0/acquisition_mode:d" />
</parameter>
</component-link>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment