From b1eab6cdf07f100829af9aec260e884ae46bb81a Mon Sep 17 00:00:00 2001 From: Chris Fancher Date: Tue, 3 Dec 2024 16:56:56 -0500 Subject: [PATCH] refactor to address test warnings --- .../calibration/mantid_peakfit_calibration.py | 38 +++++++++---------- pyrs/core/monosetting.py | 1 - pyrs/core/nexus_conversion.py | 4 +- pyrs/core/reduce_hb2b_pyrs.py | 24 ++++++------ pyrs/core/workspaces.py | 6 ++- pyrs/dataobjects/fields.py | 1 + pyrs/peaks/mantid_fit_peak.py | 1 + pyrs/peaks/peak_fit_engine.py | 7 +++- tests/integration/test_powder_pattern.py | 4 +- 9 files changed, 47 insertions(+), 39 deletions(-) diff --git a/pyrs/calibration/mantid_peakfit_calibration.py b/pyrs/calibration/mantid_peakfit_calibration.py index 5b58aecc..38654767 100644 --- a/pyrs/calibration/mantid_peakfit_calibration.py +++ b/pyrs/calibration/mantid_peakfit_calibration.py @@ -342,7 +342,7 @@ def peak_alignment_single(self, x, ReturnScalar=False, i_index=2): """ paramVec = np.copy(self._calib) - paramVec[i_index] = x + paramVec[i_index] = x[0] residual = self.get_alignment_residual(paramVec) @@ -643,37 +643,37 @@ def get_tth0(self): return np.array([self._calib[6]]) def set_shift(self, out): - self._calib[0:3] = out[0] + self._calib[0:3] = out[0][:] self._calibstatus = out[2] - self._caliberr[0:3] = out[1] + self._caliberr[0:3] = out[1][:] return def set_distance(self, out): - self._calib[2] = out[0] + self._calib[2] = out[0][0] self._calibstatus = out[2] - self._caliberr[2] = out[1] + self._caliberr[2] = out[1][0] return def set_tth0(self, out): - self._calib[6] = out[0] + self._calib[6] = out[0][0] self._calibstatus = out[2] - self._caliberr[6] = out[1] + self._caliberr[6] = out[1][0] return def set_rotation(self, out): - self._calib[3:6] = out[0] + self._calib[3:6] = out[0][:] self._calibstatus = out[2] - self._caliberr[3:6] = out[1] + self._caliberr[3:6] = out[1][:] return def set_geo(self, out): - self._calib[0:7] = out[0] + self._calib[0:7] = out[0][:] self._calibstatus = out[2] - self._caliberr[0:7] = out[1] + self._caliberr[0:7] = out[1][:] return @@ -709,9 +709,9 @@ def set_wavelength(self, out): """ - self._calib[7] = out[0] + self._calib[7] = out[0][0] self._calibstatus = out[2] - self._caliberr[7] = out[1] + self._caliberr[7] = out[1][0] return @@ -727,9 +727,9 @@ def set_shiftx(self, out): """ - self._calib[0] = out[0] + self._calib[0] = out[0][0] self._calibstatus = out[2] - self._caliberr[0] = out[1] + self._caliberr[0] = out[1][0] return @@ -745,9 +745,9 @@ def set_shifty(self, out): """ - self._calib[1] = out[0] + self._calib[1] = out[0][0] self._calibstatus = out[2] - self._caliberr[1] = out[1] + self._caliberr[1] = out[1][0] return @@ -762,9 +762,9 @@ def set_calibration(self, out): ------- """ - self._calib[:] = out[0] + self._calib[:] = out[0][:] self._calibstatus = out[2] - self._caliberr[:] = out[1] + self._caliberr[:] = out[1][:] return diff --git a/pyrs/core/monosetting.py b/pyrs/core/monosetting.py index c94472d5..6619a63c 100644 --- a/pyrs/core/monosetting.py +++ b/pyrs/core/monosetting.py @@ -34,7 +34,6 @@ def getFromIndex(index): @staticmethod def getFromRotation(mrot): '''The ``mrot`` (monochromator rotation) log in the NeXus file can be converted into a specific wavelength''' - mrot = float(mrot) if -41.0 < mrot < -38.0: return MonoSetting.Si333 elif -1.0 < mrot < 1.0: diff --git a/pyrs/core/nexus_conversion.py b/pyrs/core/nexus_conversion.py index 92e31ae7..662c1f52 100644 --- a/pyrs/core/nexus_conversion.py +++ b/pyrs/core/nexus_conversion.py @@ -47,7 +47,7 @@ def convert_pulses_to_datetime64(h5obj): pulse_time = pulse_time * 1.e9 * np.timedelta64(1, 'ns') # get absolute offset and convert to absolute time - start_time = np.datetime64(h5obj.attrs['offset']) + start_time = np.datetime64(h5obj.attrs['offset'][:-6]) + np.timedelta64(h5obj.attrs['offset'][-5:-3], 'h') return pulse_time + start_time @@ -56,7 +56,7 @@ def calculate_sub_run_time_average(log_property, time_filter) -> float: '''Determine the time average value of the supplied log''' if log_property.size() == 1: # single value property just copy - time_average_value = log_property.value + time_average_value = log_property.value[0] elif time_filter is None: # no filtering means use all values time_averaged_Run = Run() time_averaged_Run.addProperty('filtered_log', log_property, False) diff --git a/pyrs/core/reduce_hb2b_pyrs.py b/pyrs/core/reduce_hb2b_pyrs.py index ce1346c0..f7666569 100644 --- a/pyrs/core/reduce_hb2b_pyrs.py +++ b/pyrs/core/reduce_hb2b_pyrs.py @@ -276,10 +276,10 @@ def _cal_rotation_matrix_x(angle_rad): :param float angle_rad: roation angle :return: """ - rotate_matrix = np.matrix([[1., 0., 0.], - [0., np.cos(angle_rad), -np.sin(angle_rad)], - [0., np.sin(angle_rad), np.cos(angle_rad)]], - 'float') + rotate_matrix = np.array([[1., 0., 0.], + [0., np.cos(angle_rad), -np.sin(angle_rad)], + [0., np.sin(angle_rad), np.cos(angle_rad)]], + dtype=np.float32) return rotate_matrix @@ -291,10 +291,10 @@ def _cal_rotation_matrix_y(angle_rad): :param float angle_rad: roation angle :return: """ - rotate_matrix = np.matrix([[np.cos(angle_rad), 0., np.sin(angle_rad)], - [0., 1., 0.], - [-np.sin(angle_rad), 0., np.cos(angle_rad)]], - 'float') + rotate_matrix = np.array([[np.cos(angle_rad), 0., np.sin(angle_rad)], + [0., 1., 0.], + [-np.sin(angle_rad), 0., np.cos(angle_rad)]], + dtype=np.float32) return rotate_matrix @@ -306,10 +306,10 @@ def _cal_rotation_matrix_z(angle_rad): :param float angle_rad: roation angle :return: """ - rotate_matrix = np.matrix([[np.cos(angle_rad), -np.sin(angle_rad), 0.], - [np.sin(angle_rad), np.cos(angle_rad), 0.], - [0., 0., 1.]], - 'float') + rotate_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad), 0.], + [np.sin(angle_rad), np.cos(angle_rad), 0.], + [0., 0., 1.]], + dtype=np.float32) return rotate_matrix diff --git a/pyrs/core/workspaces.py b/pyrs/core/workspaces.py index af1ed3bc..422cba90 100644 --- a/pyrs/core/workspaces.py +++ b/pyrs/core/workspaces.py @@ -766,7 +766,11 @@ def set_reduced_diffraction_data(self, sub_run: int, mask_id: Optional[str], dtype=intensity_array.dtype) if variances_array is None: - variances_array = numpy.sqrt(intensity_array) + temp_intensity_array = intensity_array.copy() + temp_intensity_array[temp_intensity_array < 1] = 1 + variances_array = numpy.sqrt(temp_intensity_array) + del temp_intensity_array + # END-IF # set the diffraction data (2D) array with new dimension diff --git a/pyrs/dataobjects/fields.py b/pyrs/dataobjects/fields.py index 3f435b54..e56f4945 100644 --- a/pyrs/dataobjects/fields.py +++ b/pyrs/dataobjects/fields.py @@ -126,6 +126,7 @@ def __init__(self, name: str, z: Union[List[float], np.ndarray]) -> None: all_lengths = [len(values), len(errors), len(x), len(y), len(z)] assert len(set(all_lengths)) == 1, 'input lists must all have the same lengths' + self._sample = unumpy.uarray(values, errors) self._point_list = PointList([x, y, z]) self._name = name diff --git a/pyrs/peaks/mantid_fit_peak.py b/pyrs/peaks/mantid_fit_peak.py index fa25e2f8..fd9cd741 100644 --- a/pyrs/peaks/mantid_fit_peak.py +++ b/pyrs/peaks/mantid_fit_peak.py @@ -107,6 +107,7 @@ def convert_from_table_to_arrays(table_ws): # TODO put this in mantid_helper # get fitted parameter value for col_index, param_name in enumerate(table_col_names): # get value from column in value table + struct_array[param_name] = table_ws.column(col_index) return struct_array diff --git a/pyrs/peaks/peak_fit_engine.py b/pyrs/peaks/peak_fit_engine.py index 8d730857..936045e4 100644 --- a/pyrs/peaks/peak_fit_engine.py +++ b/pyrs/peaks/peak_fit_engine.py @@ -133,8 +133,11 @@ def _guess_center(self, x_min, x_max): y_vals = self._mtd_wksp.readY(wksp_index)[i_min:i_max] y_offset = np.abs(y_vals.max()) - # add the first moment to the list of centers - moment = np.sum(x_vals[i_min:i_max] * (y_vals + y_offset)) / np.sum(y_vals + y_offset) + if np.sum(y_vals + y_offset) < 1: + moment = -1 + else: + moment = np.sum(x_vals[i_min:i_max] * (y_vals + y_offset)) / np.sum(y_vals + y_offset) + if (x_min < moment < x_max): center.append(moment) else: diff --git a/tests/integration/test_powder_pattern.py b/tests/integration/test_powder_pattern.py index f92cf752..56958e19 100644 --- a/tests/integration/test_powder_pattern.py +++ b/tests/integration/test_powder_pattern.py @@ -102,8 +102,8 @@ def test_2theta_calculation(): # compare with gold file gold_dict = parse_gold_file('tests/data/HB2B_1017_Pixels_Gold.h5') - np.testing.assert_allclose(pixel_positions, gold_dict['positions'], rtol=1E-8) - np.testing.assert_allclose(two_theta_arrays, gold_dict['2theta'], rtol=1E-8) + np.testing.assert_allclose(pixel_positions, gold_dict['positions'], rtol=1E-5) + np.testing.assert_allclose(two_theta_arrays, gold_dict['2theta'], rtol=1E-5) @pytest.mark.parametrize('project_file_name, mask_file_name, gold_file',