Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1045. Ensure line plot displays integer-valued data items in log display. #1046

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading