Skip to content

Conversation

SilkeSchomann
Copy link
Collaborator

Description of work:

Prevent addition of None values for width when calculating the cut end. For the issue the crash happened when creating a script from a cut plot. However, the same problem could also occur in the cut plot presenter, so I've applied the same fix there as well.

To test:

Please follow the instructions in the original issue. In addition, try to use float values in the width box.

Fixes #1105

@warunawickramasingha warunawickramasingha self-assigned this Sep 16, 2025
@warunawickramasingha
Copy link
Contributor

I've followed the steps mentioned in the linked issue, and I am not observing the issue when File -> Generate Script to Clipboard is clicked. However, as mentioned in the test steps I have used float value of 0.1 for the width and generated the plot and copied the script and executed that from mantid. The generated script as below gives a number of warnings when executed back on Mantid

# Python Script Generated by Mslice on 2025-09-16 17:16:25
import mslice.cli as mc
import mslice.plotting.pyplot as plt

ws_MAR21335_Ei60meV = mc.Load(Filename=r'C:/Users/kli94267/Downloads/SampleData-ISIS/SampleData-ISIS\MAR21335_Ei60meV.nxs', OutputWorkspace='MAR21335_Ei60meV')

fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(111, projection="mslice")
cut_ws_0 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.3", IntegrationAxis="|Q|,1.0,1.1,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_0, label="MAR21335_Ei60meV 1.00<|Q|<1.10", color="#1f77b4", marker="o", ls="-", lw=1.5, plot_over=False)

cut_ws_1 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.1,1.2000000000000002,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_1, label="MAR21335_Ei60meV 1.10<|Q|<1.20", color="#ff7f0e", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_2 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.2000000000000002,1.3000000000000003,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_2, label="MAR21335_Ei60meV 1.20<|Q|<1.30", color="#2ca02c", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_3 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.3000000000000003,1.4000000000000004,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_3, label="MAR21335_Ei60meV 1.30<|Q|<1.40", color="#d62728", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_4 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.4000000000000004,1.5000000000000004,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_4, label="MAR21335_Ei60meV 1.40<|Q|<1.50", color="#9467bd", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_5 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.5000000000000004,1.6000000000000005,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_5, label="MAR21335_Ei60meV 1.50<|Q|<1.60", color="#8c564b", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_6 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.6000000000000005,1.7000000000000006,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_6, label="MAR21335_Ei60meV 1.60<|Q|<1.70", color="#e377c2", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_7 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.7000000000000006,1.8000000000000007,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_7, label="MAR21335_Ei60meV 1.70<|Q|<1.80", color="#7f7f7f", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_8 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.8000000000000007,1.9000000000000008,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_8, label="MAR21335_Ei60meV 1.80<|Q|<1.90", color="#bcbd22", marker="o", ls="-", lw=1.5, plot_over=True)

cut_ws_9 = mc.Cut(ws_MAR21335_Ei60meV, CutAxis="DeltaE,-30.0,50.0,0.2996254563331604", IntegrationAxis="|Q|,1.9000000000000008,2.0,0.0", NormToOne=False, IntensityCorrection=False, SampleTemperature=None)
ax.errorbar(cut_ws_9, label="MAR21335_Ei60meV 1.90<|Q|<2.00", color="#17becf", marker="o", ls="-", lw=1.5, plot_over=True)

from mslice.cli.helperfunctions import hide_a_line_and_errorbars, append_visible_handle_and_label
from mslice.util.compat import legend_set_draggable

# hide lines, errorbars, and legends
handles, labels = ax.get_legend_handles_labels()
visible_handles = []
visible_labels = []

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 0)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 1)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 2)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 3)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 4)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 5)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 6)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 7)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 8)

append_visible_handle_and_label(visible_handles, handles, visible_labels, labels, 9)

legend_set_draggable(ax.legend(visible_handles, visible_labels, fontsize='medium'), True)

ax.set_title('', fontsize=12.0)
ax.set_ylabel(r'Signal/#Events', fontsize=10.0)
ax.set_xlabel(r'Energy Transfer (meV)', fontsize=10.0)
ax.set_ylim(bottom=-1.113204800641584, top=2.515966486880703)
ax.set_xlim(left=-33.1310861423221, right=39.04868913857678)
mc.Show()
  • The Warning
Cut started
Cut successful, Duration 0.10 seconds
WARNING:root:The DeltaE step provided (0.2996) is smaller than the data step in the workspace (0.3000). Please provide a larger DeltaE step.

Just wanted to confirm whether this is the expected behaviour.

Thanks

@SilkeSchomann
Copy link
Collaborator Author

@warunawickramasingha Yes, this behaviour is as expected.

Copy link
Contributor

@warunawickramasingha warunawickramasingha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the change and functionally it is working as expected without issues.

However, the newly added code blocks for deriving cut_end look repetitive and it could have been better if that can be moved into a utility method and reused.
ex:

def _get_cut_end(start, end, width):
    return min(start + width, end) if cut.width is not None else end

If time allows, a simple test case covering the scenario at cut_plotter_presenter_test.py or scripting_helperfunctions_test.py could have been even better.

Thanks!

@SilkeSchomann SilkeSchomann force-pushed the 1105_fix_clipboard_error branch from fbf1aa0 to 176f17a Compare September 17, 2025 11:13
@SilkeSchomann
Copy link
Collaborator Author

I have tested the change and functionally it is working as expected without issues.

However, the newly added code blocks for deriving cut_end look repetitive and it could have been better if that can be moved into a utility method and reused. ex:

def _get_cut_end(start, end, width):
    return min(start + width, end) if cut.width is not None else end

If time allows, a simple test case covering the scenario at cut_plotter_presenter_test.py or scripting_helperfunctions_test.py could have been even better.

Thanks!

@warunawickramasingha That is an excellent idea! Since this function might not apply only to the cut integration axes, I've created a general version in alg_workspace_ops so that it can be used in scripting and presenters (and other places).

Copy link
Contributor

@warunawickramasingha warunawickramasingha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes and happy to approve!

@SilkeSchomann SilkeSchomann merged commit 7987531 into main Sep 17, 2025
3 checks passed
@SilkeSchomann SilkeSchomann deleted the 1105_fix_clipboard_error branch September 17, 2025 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Save script to clipboard error
2 participants