Overwriting function definition

I am writing my own function, and I wish to modify the number of variable parameters, but I don’t seem to be able to overwrite the old definition, even though I have executed the modified script multiple times. I don’t want to restart MantidPlot or delete the entire memory, because I am using workspaces that I want to keep. I just want to clear the function definition from the memory. Surely this should not be impossible. Alternatively, once I have re-executed the script defining the function, surely it should override the older definition. It is infuriating to have to keep restarting mantid all the time.

Hi,

The function should be redefined when you execute the script again - it happens on the line
FunctionFactory.subscribe(ExampleFunction).

(The parameters are declared in the init(self) method).

However, if you have already set the function as part of a model in the Fit Browser (via right click / Add Function), you will need to right-click to remove it and add it again so that it uses the newly defined function - otherwise the old definition, with the old number of parameters, will still be set in the model. It should not be necessary to restart Mantid though.

If this doesn’t help, could you tell us where you are trying to use the function from - for example, are you running a fit from the “Fit Function” window in MantidPlot, from a custom interface or from another script?

Thanks,
Tom

It sounds like I will have to completely rewrite the function. It isn’t a fitting function. It is just a case of taking a script which performs in a certain way, and then functionalising it, as I would do in matlab so that it can be called with user defined values for its parameters. It does not have a class. It does not have an init(self). I am merely writing it as I would a standard python function. And then I am attempting to run it from another script in the mantidplot python script window.

Thanks
Helen

Sorry, I misunderstood and thought you were writing a fit function.

In that case, it might be because Python doesn’t reload previously imported modules - so if you run your second script with import my_script at the top, change the code in my_script and run the second script again, it will not see the changes because it will not reload the changed script.

To get the module to reload without having to restart the Python interpreter, you can use the reload function, for example reload(my_script).

Hi, it looks like that is exactly what I need. Thanks

Sorry where should I be running the reload function?

After the import statement at the top, but before calling any functions from the script - so that it is reloaded before you try to use it.

My first line is:
from plot_jaw_scan_new import plotJawScan

If I make the next line
reload(plot_jaw_scan)
I get NameError: name ‘plot_jaw_scan_new’ is not defined

Alternatively if I use
reload(plotJawScan)
I get TypeError: argument must be module

I don’t know what other options I have for inserting into the brackets.

I think one way to do this would be:

import plot_jaw_scan_new reload(plot_jaw_scan_new) from plot_jaw_scan_new import plotJawScan

The first line is needed so you have a module name to reload in the second line, and then you can import the specific function as before.

Perfect. Thanks, that’s just what I needed.

Unfortunately it doesn’t get me past a bigger problem that my called function doesn’t seem to like plotting anything. It works through the updated function until it gets to the plotting part. It seems happy with the plt.plot statement, but when it comes to plt.show() it throws an error message

ValueError: signal only works in main thread

Is plt the mantidplot module, or matplotlib’s pyplot package?

If it is the external matplotlib, there’s an issue running this from scripts in Mantid that requires a workaround at the moment (http://www.mantidproject.org/MantidPlot:External_Python_Plotting_Package) - the commands need to be wrapped in gui_cmd.

(If it complains about this, try from mantidplot import gui_cmd at the top.)

This is fixed in the upcoming release (Mantid 3.8), so the workaround is just for the time being - let me know if this helps or not!

It is matplotlib’s pyplot package.

I have wrapped it in gui_cmd, and no longer get an error message. However, I also don’t get any plot appearing.

Initially I wasn’t explicitly using matplotlib pyplot. When the script I call didn’t have any import statement of:

from matplotlib import pyplot as plt

and I just had a line of the script with

plot(x,y,markers=‘o’)

I would get an error message stating:

NameError: global name ‘plot’ is not defined

and this was when I invoked matplotlib.
If you could provide me with a solution not requiring matplotlib, but also not getting the NameError message, then that would be great.

Thanks

I seem to have sorted it out by now using the import statement:

from mantidplot import plot,xlim,ylim,xlabel,ylabel

This avoids using matplotlib, with the gui_cmd workaround, and I get the plot that I expect with no error messages.

Thank you for your help.