diff --git a/nion/swift/LineGraphCanvasItem.py b/nion/swift/LineGraphCanvasItem.py index 01dd2bafa..9ac30816a 100644 --- a/nion/swift/LineGraphCanvasItem.py +++ b/nion/swift/LineGraphCanvasItem.py @@ -75,7 +75,8 @@ def calculate_y_axis(xdata_list: typing.Sequence[typing.Optional[DataAndMetadata calibrated_data_min = None for xdata in xdata_list: if xdata and xdata.data_shape[-1] > 0: - uncalibrated_data = xdata.data + # force the uncalibrated_data to be float so that numpy.amin with a numpy.inf initial value works. + uncalibrated_data = xdata.data if numpy.issubdtype(xdata.data.dtype, numpy.floating) else xdata.data.astype(float) if uncalibrated_data is not None: if data_style == "log": calibrated_origin = xdata.intensity_calibration.convert_from_calibrated_value(0.0) @@ -96,7 +97,8 @@ def calculate_y_axis(xdata_list: typing.Sequence[typing.Optional[DataAndMetadata calibrated_data_max = None for xdata in xdata_list: if xdata and xdata.data_shape[-1] > 0: - uncalibrated_data = xdata.data + # force the uncalibrated_data to be float so that numpy.amin with a numpy.inf initial value works. + uncalibrated_data = xdata.data if numpy.issubdtype(xdata.data.dtype, numpy.floating) else xdata.data.astype(float) if uncalibrated_data is not None: if data_style == "log": calibrated_origin = xdata.intensity_calibration.convert_from_calibrated_value(0.0) diff --git a/nion/swift/resources/changes.json b/nion/swift/resources/changes.json index 20a3a4530..bf3a278fa 100644 --- a/nion/swift/resources/changes.json +++ b/nion/swift/resources/changes.json @@ -2,6 +2,10 @@ { "version": "UNRELEASED", "notes": [ + { + "issues": ["https://github.com/nion-software/nionswift/issues/1045"], + "summary": "Fix issue when displaying line plot of integer-valued 1D data item using log scale." + }, { "issues": ["https://github.com/nion-software/nionswift/issues/592"], "summary": "Importing valid data always succeeds by assign new identifiers, instead of silently skipping possible duplicates." diff --git a/nion/swift/test/LineGraphCanvasItem_test.py b/nion/swift/test/LineGraphCanvasItem_test.py index b4ff03d15..2efd55f05 100755 --- a/nion/swift/test/LineGraphCanvasItem_test.py +++ b/nion/swift/test/LineGraphCanvasItem_test.py @@ -141,6 +141,20 @@ def test_display_limits_are_reasonable_when_using_calibrated_log_scale(self): self.assertAlmostEqual(numpy.nanmin(calibrated_data), math.log10(numpy.nanmin(data))) self.assertAlmostEqual(numpy.nanmax(calibrated_data), math.log10(numpy.nanmax(data))) + def test_line_plot_with_log_scale_displays_integers(self): + data = numpy.array([1,2,3], dtype=int) + data_style = "log" + xdata = DataAndMetadata.new_data_and_metadata(data) + # at some point, calculate_y_axis failed to return without exception when the data type was int. + # so the main point of this test is to ensure that the function returns without exception. + calibrated_data_min, calibrated_data_max, y_ticker = LineGraphCanvasItem.calculate_y_axis([xdata], None, None, data_style) + # the asserts below are a sanity check to ensure that the function is returning reasonable values. + # note: these are calibrated values for the AXES not the original data. furthermore they will be log10. + # so checking that the first value is negative (original axes value is less than 1), and the second value + # is positive (original axes value is greater than 1) is a reasonable check. + self.assertLess(calibrated_data_min, 0.0) + self.assertGreater(calibrated_data_max, 0.0) + def test_graph_segments_are_calculated_correctly_with_nans(self): # this was a bug in the original implementation data = numpy.zeros((16,))