Skip to content

Commit

Permalink
Fix #1045. Ensure line plot displays integer-valued data items in log…
Browse files Browse the repository at this point in the history
… display.
  • Loading branch information
cmeyer committed May 16, 2024
1 parent 85159b6 commit 83376a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions nion/swift/LineGraphCanvasItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions nion/swift/resources/changes.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
14 changes: 14 additions & 0 deletions nion/swift/test/LineGraphCanvasItem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,))
Expand Down

0 comments on commit 83376a2

Please sign in to comment.