Skip to content

Commit

Permalink
Merge pull request #38770 from mantidproject/38650_project_recovery_p…
Browse files Browse the repository at this point in the history
…lots_fix

Reimplement #38486 to avoid project recovery error
  • Loading branch information
thomashampson authored Jan 31, 2025
2 parents 09e34d1 + 92b97b6 commit 73d3467
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
16 changes: 5 additions & 11 deletions Framework/PythonInterface/mantid/plots/mantidaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# This file is part of the mantid package
from collections.abc import Iterable
import copy
from enum import IntEnum
from functools import wraps

import numpy as np
Expand Down Expand Up @@ -42,9 +41,6 @@
# -----------------------------------------------------------------------------
# Decorators
# -----------------------------------------------------------------------------
class AxisArgType(IntEnum):
WORKSPACE = 0
LINE = 1


def plot_decorator(func):
Expand All @@ -55,7 +51,6 @@ def wrapper(self, *args, **kwargs):
# Saves saving it on array objects
if datafunctions.validate_args(*args, **kwargs):
# Fill out kwargs with the values of args
kwargs["argType"] = AxisArgType.WORKSPACE

kwargs["workspaces"] = args[0].name()
kwargs["function"] = func_name
Expand All @@ -65,9 +60,8 @@ def wrapper(self, *args, **kwargs):
if "cmap" in kwargs and isinstance(kwargs["cmap"], Colormap):
kwargs["cmap"] = kwargs["cmap"].name
self.creation_args.append(kwargs)
elif func_name in ["axhline", "axvline"]:
new_creation_args = {"function": func_name, "args": args, "kwargs": kwargs, "argType": AxisArgType.LINE}
self.creation_args.append(new_creation_args)
elif func_name == "axhline" or func_name == "axvline":
self.creation_args.append({"function": func_name, "args": args, "kwargs": kwargs})

return func_value

Expand Down Expand Up @@ -488,9 +482,9 @@ def rename_workspace(self, new_name, old_name):
"""

for cargs in self.creation_args:
argType = cargs["argType"]
# for workspace creation args, since lines dont have "workspaces"
if argType == AxisArgType.WORKSPACE and cargs["workspaces"] == old_name:
# If a workspace plot is overplotted with a line (e.g ax.axvline), the second set of creation args
# will not have a "workspaces" key
if cargs.get("workspaces") == old_name:
cargs["workspaces"] = new_name
for ws_name, ws_artist_list in list(self.tracked_workspaces.items()):
for ws_artist in ws_artist_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,14 @@ def test_rename_workspace_updates_creation_args(self):
self.ax.rename_workspace(new_name="new_name", old_name="ws")
self.assertEqual("new_name", self.ax.creation_args[0]["workspaces"])

def test_rename_workspace_works_with_additional_line_plot(self):
ws = CreateSampleWorkspace()
self.ax.plot(ws, specNum=2)
self.ax.axvline(x=1, label="label", color="red")
self.assertIsNone(self.ax.creation_args[1].get("workspaces"))
# Line plot has no workspaces creation arg, test there is no exception
self.ax.rename_workspace(new_name="new_name", old_name="ws")

def _run_check_axes_distribution_consistency(self, normalization_states):
mock_tracked_workspaces = {
"ws": [Mock(is_normalized=normalization_states[0]), Mock(is_normalized=normalization_states[1])],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix for bug in ``mantid.plots.MantidAxes`` where an exception would occur when a workspace plot, which also had data not tied to a workspace on the same axes, had its workspace renamed.
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ def _change_plot_normalization(self, ax):
raise RuntimeError("No spectrum number associated with plot of workspace '{}'".format(workspace.name()))

arg_set_copy = copy(arg_set)
for key in ["function", "workspaces", "autoscale_on_update", "norm", "argType"]:
for key in ["function", "workspaces", "autoscale_on_update", "norm"]:
try:
del arg_set_copy[key]
except KeyError:
Expand Down

0 comments on commit 73d3467

Please sign in to comment.