def test_unit_conversion(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) tmp = mantidcompat.convert_Workspace2D_to_data_array(ws) target_tof = tmp.coords['tof'] ws = mantid.ConvertUnits(InputWorkspace=ws, Target="Wavelength", EMode="Elastic") converted_mantid = mantidcompat.convert_Workspace2D_to_data_array(ws) da = mantidcompat.convert_EventWorkspace_to_data_array( eventWS, load_pulse_times=False) da.realign({'tof': target_tof}) da = sc.histogram(da) d = sc.Dataset(da) converted = sc.neutron.convert(d, 'tof', 'wavelength') self.assertTrue( np.all(np.isclose(converted_mantid.values, converted[""].values))) self.assertTrue( np.all( np.isclose( converted_mantid.coords['wavelength'].values, converted.coords['wavelength'].values, )))
def test_Workspace2D_not_common_bins_masks(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) ws = mantid.ConvertUnits(ws, "Wavelength", EMode="Direct", EFixed=0.1231) # these X values will mask different number of bins masked_ws = self._mask_bins_and_spectra(ws, -214, -192, num_spectra=3) self.assertFalse(masked_ws.isCommonBins()) ds = mantidcompat.convert_Workspace2D_to_data_array(masked_ws) # bin with 3 masks np.testing.assert_array_equal(ds.masks["bin"].values[0], [True, True, False, False, False]) # bin with only 2 np.testing.assert_array_equal(ds.masks["bin"].values[31], [True, True, True, False, False]) np.testing.assert_array_equal(ds.masks["spectrum"].values[0:3], [True, True, True])
def test_unit_conversion(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) tmp = mantidcompat.convert_Workspace2D_to_dataarray(ws) target_tof = tmp.coords[sc.Dim.Tof] ws = mantid.ConvertUnits(InputWorkspace=ws, Target="Wavelength", EMode="Elastic") converted_mantid = mantidcompat.convert_Workspace2D_to_dataarray(ws) da = mantidcompat.convertEventWorkspace_to_dataarray( eventWS, False) da = sc.histogram(da, target_tof) d = sc.Dataset(da) converted = sc.neutron.convert(d, sc.Dim.Tof, sc.Dim.Wavelength) self.assertTrue( np.all(np.isclose(converted_mantid.values, converted[""].values))) self.assertTrue( np.all( np.isclose( converted_mantid.coords[sc.Dim.Wavelength].values, converted.coords[sc.Dim.Wavelength].values, )))
def test_Workspace2D(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) d = mantidcompat.convert_Workspace2D_to_data_array(ws) self.assertEqual( d.attrs["run"].value.getProperty("run_start").value, "2012-05-21T15:14:56.279289666", )
def test_set_run(self): import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) d = mantidcompat.convert_EventWorkspace_to_data_array(target, False) d.attrs["run"].value.addProperty("test_property", 1, True) # before self.assertFalse(target.run().hasProperty("test_property")) target.setRun(d.attrs["run"].value) # after self.assertTrue(target.run().hasProperty("test_property"))
def test_set_sample(self): import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) d = mantidcompat.convert_EventWorkspace_to_data_array(target, False) d.attrs["sample"].value.setThickness(3) # before self.assertNotEqual(3, target.sample().getThickness()) target.setSample(d.attrs["sample"].value) # after self.assertEqual(3, target.sample().getThickness())
def test_warning_raised_when_convert_run_log_with_unrecognised_units(self): import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) with warnings.catch_warnings(record=True) as caught_warnings: mantidcompat.convert_EventWorkspace_to_data_array(target, False) assert len( caught_warnings ) > 0, "Expected warnings due to some run logs " \ "having unrecognised units strings" assert any("unrecognised units" in str(caught_warning.message) for caught_warning in caught_warnings)
def test_EventWorkspace(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000) binned_mantid = mantidcompat.convert_Workspace2D_to_data_array(ws) target_tof = binned_mantid.coords[sc.Dim.Tof] d = mantidcompat.convert_EventWorkspace_to_data_array(eventWS, False) binned = sc.histogram(d, target_tof) delta = sc.sum(binned_mantid - binned, sc.Dim.Spectrum) delta = sc.sum(delta, sc.Dim.Tof) self.assertLess(np.abs(delta.value), 1e-5)
def test_Workspace2D(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) d = mantidcompat.convert_Workspace2D_to_data_array(ws) self.assertEqual( d.attrs["run_start"].value, "2012-05-21T15:14:56.279289666", ) self.assertEqual(d.data.unit, sc.units.counts) for i in range(ws.getNumberHistograms()): assert np.all(np.equal(d.values[i], ws.readY(i))) assert np.all(np.equal(d.variances[i], ws.readE(i) * ws.readE(i))) self.assertEqual(d.coords['spectrum'].dtype, sc.dtype.int32) self.assertEqual(d.coords['tof'].dtype, sc.dtype.float64)
def test_EventWorkspace(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000) binned_mantid = mantidcompat.convert_Workspace2D_to_data_array(ws) target_tof = binned_mantid.coords['tof'] d = mantidcompat.convert_EventWorkspace_to_data_array( eventWS, load_pulse_times=False) binned = sc.histogram(d.bins, target_tof) delta = sc.sum(binned_mantid - binned, 'spectrum') delta = sc.sum(delta, 'tof') self.assertLess(np.abs(delta.value), 1e-5)
def test_EventWorkspace_realign_events(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) realigned = mantidcompat.convert_EventWorkspace_to_data_array( eventWS, realign_events=True, load_pulse_times=False) d = mantidcompat.convert_EventWorkspace_to_data_array( eventWS, realign_events=False, load_pulse_times=False) d.realign({'tof': realigned.coords['tof']}) # Removing run and sample due to missing comparison operators del d.attrs['run'] del d.attrs['sample'] del realigned.attrs['run'] del realigned.attrs['sample'] assert realigned == d
def test_convert_scalar_run_log_to_attrs(self): # Given a Mantid workspace with a run log import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) log_name = "start_time" self.assertTrue( target.run().hasProperty(log_name), f"Expected input workspace to have a {log_name} run log") # When the workspace is converted to a scipp data array d = mantidcompat.convert_EventWorkspace_to_data_array(target, False) # Then the data array contains the run log as an unaligned coord self.assertEqual( target.run()[log_name].value, d.attrs[log_name].value, "Expected value of the unaligned coord to match " "the original run log from the Mantid workspace")
def test_detector_positions(self): import mantid.simpleapi as mantid from mantid.kernel import V3D eventWS = mantid.CloneWorkspace(self.base_event_ws) comp_info = eventWS.componentInfo() small_offset = V3D(0.01, 0.01, 0.01) comp_info.setPosition(comp_info.source(), comp_info.samplePosition() + small_offset) moved = mantidcompat.convert_Workspace2D_to_data_array(eventWS) moved_det_position = moved.coords["position"] unmoved = mantidcompat.convert_Workspace2D_to_data_array(eventWS) unmoved_det_positions = unmoved.coords["position"] # Moving the sample accounted for in position calculations # but should not yield change to final detector positions self.assertTrue( np.all( np.isclose(moved_det_position.values, unmoved_det_positions.values)))
def test_no_warning_raised_explicitly_dimensionless_run_log(self): import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) with warnings.catch_warnings(record=True) as caught_warnings: mantidcompat.convert_EventWorkspace_to_data_array(target, False) original_number_of_warnings = len(caught_warnings) # Add an explicitly dimensionless log mantid.AddSampleLog(Workspace=target, LogName='dimensionless_log', LogText='1', LogType='Number', LogUnit='dimensionless') with warnings.catch_warnings(record=True) as caught_warnings: mantidcompat.convert_EventWorkspace_to_data_array(target, False) assert len(caught_warnings) == original_number_of_warnings,\ "Expected no extra warning about unrecognised units " \ "from explicitly dimensionless log"
def test_Workspace2D_common_bins_masks(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) ws_x = ws.readX(0) # mask the first 3 bins, range is taken as [XMin, XMax) masked_ws = self._mask_bins_and_spectra(ws, xmin=ws_x[0], xmax=ws_x[3], num_spectra=3) self.assertTrue(masked_ws.isCommonBins()) ds = mantidcompat.convert_Workspace2D_to_data_array(masked_ws) np.testing.assert_array_equal(ds.masks["bin"].values[0:3], [True, True, True]) np.testing.assert_array_equal(ds.masks["spectrum"].values[0:3], [True, True, True])
def test_Workspace2D_common_bins_not_common_masks(self): import mantid.simpleapi as mantid eventWS = mantid.CloneWorkspace(self.base_event_ws) ws = mantid.Rebin(eventWS, 10000, PreserveEvents=False) ws_x = ws.readX(0) # mask first 3 bins in first 3 spectra, range is taken as [XMin, XMax) masked_ws = self._mask_bins_and_spectra(ws, xmin=ws_x[0], xmax=ws_x[3], num_spectra=3, indices='0-2') self.assertTrue(masked_ws.isCommonBins()) ds = mantidcompat.convert_Workspace2D_to_data_array(masked_ws) mask = sc.Variable(dims=ds.dims, shape=ds.shape, dtype=sc.dtype.bool) mask['spectrum', 0:3]['tof', 0:3] |= sc.Variable(value=True) assert sc.is_equal(ds.masks['bin'], mask) np.testing.assert_array_equal(ds.masks["spectrum"].values[0:3], [True, True, True])
def test_convert_array_run_log_to_attrs(self): # Given a Mantid workspace with a run log import mantid.simpleapi as mantid target = mantid.CloneWorkspace(self.base_event_ws) log_name = "SampleTemp" self.assertTrue( target.run().hasProperty(log_name), f"Expected input workspace to have a {log_name} run log") # When the workspace is converted to a scipp data array d = mantidcompat.convert_EventWorkspace_to_data_array(target, False) # Then the data array contains the run log as an unaligned coord self.assertTrue( np.allclose(target.run()[log_name].value, d.attrs[log_name].values.data.values), "Expected values in the unaligned coord to match " "the original run log from the Mantid workspace") self.assertEqual(d.attrs[log_name].values.unit, sc.units.K) self.assertTrue( np.array_equal(target.run()[log_name].times.astype(np.int64), d.attrs[log_name].values.coords["time"].values), "Expected times in the unaligned coord to match " "the original run log from the Mantid workspace")