Ticket #4727 (closed: fixed)

Opened 9 years ago

Last modified 5 years ago

Simple Python functions don't do expected thing inside a function

Reported by: Martyn Gigg Owned by: Martyn Gigg
Priority: major Milestone: Release 2.1
Component: Mantid Keywords:
Cc: Blocked By:
Blocking: Tester: Anders Markvardsen

Description

The script below demonstrates the problem. Inside the function a user would expect the workspace to be replaced but in fact a new workspace called workspace is created.

from mantid.simpleapi import Load, ConvertUnits, mtd

def convert(workspace):
    workspace = ConvertUnits(workspace, Target='Energy')
    return workspace

ws = Load("MAR11015.raw")
ws = convert(ws)

print 'In ADS:',mtd.getObjectNames() # prints ['workspace', 'ws']

Change History

comment:1 Changed 9 years ago by Martyn Gigg

The workaround is to use the keyword style to specify the output workspace when inside a function, i.e.

def convert(workspace):
    ConvertUnits(workspace, OutputWorkspace=workspace.getName() Target='Energy')
    return mtd[workspace.getName()]

comment:2 Changed 9 years ago by Martyn Gigg

  • Status changed from new to accepted

comment:3 Changed 9 years ago by Martyn Gigg

Refs #4727. Make lhs_info use already defined object names.

If a variable is already defined and it has a getName() attribute then this name is used over the variable name.

Changeset: f1a083e0a5dbd30d1aa8fefcd3b355a42b874951

comment:4 Changed 9 years ago by Martyn Gigg

  • Status changed from accepted to verify
  • Resolution set to fixed

comment:5 Changed 9 years ago by Martyn Gigg

  • Status changed from verify to reopened
  • Resolution fixed deleted

comment:6 Changed 9 years ago by Martyn Gigg

There is a problem with code if the item has been removed from the ADS the name checking fails. To reproduce, run this script

w = Load("LOQ48127.raw")
y = ConvertUnits(w, 'Wavelength') 

then remove the workspace w from the ADS using the Delete button and rerun the script.

The script returns an error

Variable invalidated, data has been deleted." in file 'funcreturns.py' at line 204

comment:7 Changed 9 years ago by Martyn Gigg

Refs #4727. Catch an exception if a variable is invalidated.

Changeset: d799d5c8726173592953ebad362ca934bc4b34fb

comment:8 Changed 9 years ago by Martyn Gigg

  • Status changed from reopened to accepted

comment:9 Changed 9 years ago by Martyn Gigg

  • Status changed from accepted to verify
  • Resolution set to fixed

comment:10 Changed 9 years ago by Martyn Gigg

Refs #4727. Catch an exception if a variable is invalidated.

Changeset: d799d5c8726173592953ebad362ca934bc4b34fb

comment:11 Changed 8 years ago by Anders Markvardsen

  • Status changed from verify to verifying
  • Tester set to Anders Markvardsen

comment:12 Changed 8 years ago by Anders Markvardsen

  • Status changed from verifying to closed

When running:

from mantid.simpleapi import Load, ConvertUnits, mtd

def convert(workspace):

workspace = ConvertUnits(workspace, Target='Energy') return workspace

ws = Load(r"C:\Backup\Backup_folder1\work\code\Mantid\git\mantid\Test\AutoTestData\MAR11001.raw") ws = convert(ws)

print 'In ADS:',mtd.getObjectNames()

The result is "from mantid.simpleapi import Load, ConvertUnits, mtd

def convert(workspace):

workspace = ConvertUnits(workspace, Target='Energy') return workspace

ws = Load(r"C:\Backup\Backup_folder1\work\code\Mantid\git\mantid\Test\AutoTestData\MAR11001.raw") ws = convert(ws)

print "In ADS:' ['ws']"

Hence it does what the user expect

comment:13 Changed 8 years ago by Russell Taylor

Re #4727. Minor improvements.

Changeset: d4eacaa886d8b28e46beb3c40e65807da7c9e5ba

comment:14 Changed 8 years ago by Russell Taylor

Oops. Typo in commit message - the change above relates to ticket #4737.

comment:15 Changed 8 years ago by Russell Taylor

Re #4727. Minor improvements.

Changeset: d4eacaa886d8b28e46beb3c40e65807da7c9e5ba

comment:16 Changed 8 years ago by Russell Taylor

Re #4727. Minor improvements.

Changeset: d4eacaa886d8b28e46beb3c40e65807da7c9e5ba

comment:17 Changed 5 years ago by Stuart Campbell

This ticket has been transferred to github issue 5574

Note: See TracTickets for help on using tickets.