Ticket #11767 (new)

Opened 5 years ago

Last modified 5 years ago

Python Algorithm Issue

Reported by: Owen Arnold Owned by: Martyn Gigg
Priority: critical Milestone: Release 3.5
Component: Python Keywords:
Cc: Blocked By:
Blocking: Tester:

Description

This came to Mantid Help:

Hi,

Not for this release (so don’t panic!)

I noticed and interesting feature when trying to create a python algorithm. The only difference between the scripts below is defining the properties. The script that doesn’t work, works if I only have 3 declareProperty lines. If a forth is added then I get an error self is not defined.

Enjoy!

Aidy



This script doesn’t work
from mantid.api import * # PythonAlgorithm, registerAlgorithm, WorkspaceProperty

class ApplyNegMuCorrection(PythonAlgorithm):

                def PyInit(self):
                                self.declareProperty(name="First Run Number",
            defaultValue=1700,
             doc="First Run Number")
                                self.declareProperty("Last Run Number",1701,doc="Last Run Number")
                                self.declareProperty("Last1 Run Number",1701,doc="Last Run Number")
        self.declareProperty("Last2 Run Number",1701,doc="Last Run Number")
                                
                                #self.declareProperty("Gain for ISIS High E Detector A",
         #   1.2,
           # doc="Gain for ISIS detector Ax+B")
        #self.declareProperty("Offset for ISIS High E Detector, B",
          #  0.0,
          #  doc="Offset for ISIS detector Ax+B")
        #self.declareProperty("Detector RIKEN High E, A",1.0,doc="Gain for RIKEN detector Ax+B")
        #self.declareProperty("Detector RIKEN High E, B",0.0,doc="Gain for RIKEN detector Ax+B")
        #self.declareProperty("Detector ISIS Low E, A",1.0,doc="Gain for ISIS detector Ax+B")
        #self.declareProperty("Detector ISIS Low E, B",0.0,doc="Gain for ISIS detector Ax+B")

        #self.declareProperty(WorkspaceProperty("OutputWorkspace","",Direction.Output))

                def category(self):
                                return "CorrectionFunctions;Muon"

                def PyExec(self):
                                iws = self.getProperty("InputWorkspace").value
                                x1=self.getProperty("x1").value
                                y1=self.getProperty("y1").value
                                ows=WorkspaceFactory.create(iws)

                                for s in range(iws.getNumberHistograms()):
                                                y=ows.getAxis(1).getValue(s)
                                                print "y[",s,"]=",y
                                                ows.dataX(s)[:]=iws.dataX(s)[:]
                                                ows.dataE(s)[:]=iws.dataE(s)[:]
                                                for t in range(len(ows.dataY(s))):
                                                                x=ows.dataX(s)[t]
                                                                ows.dataY(s)[t]=iws.dataY(s)[t]+x1*x+y1*y

                                self.setProperty("OutputWorkspace",ows)

AlgorithmFactory.subscribe(ApplyNegMuCorrection)

This script does
from mantid.api import * # PythonAlgorithm, registerAlgorithm, WorkspaceProperty

class ApplyNegMuCorrection(PythonAlgorithm):

                def PyInit(self):
                                self.declareProperty(name="First Run Number",defaultValue=1700,doc="First Run Number")
                                self.declareProperty(name="Last Run Number",defaultValue=1701,doc="Last Run Number")
                                self.declareProperty(name="First2 Run Number",defaultValue=1700,doc="First Run Number")
                                self.declareProperty(name="First3 Run Number",defaultValue=1700,doc="First Run Number")
                                #self.declareProperty("Last Run Number",1701,doc="Last Run Number")
                                #self.declareProperty("Last1 Run Number",1701,doc="Last Run Number")
        #self.declareProperty("Last2 Run Number",1701,doc="Last Run Number")
                                
                                #self.declareProperty("Gain for ISIS High E Detector A",
         #   1.2,
           # doc="Gain for ISIS detector Ax+B")
        #self.declareProperty("Offset for ISIS High E Detector, B",
          #  0.0,
          #  doc="Offset for ISIS detector Ax+B")
        #self.declareProperty("Detector RIKEN High E, A",1.0,doc="Gain for RIKEN detector Ax+B")
        #self.declareProperty("Detector RIKEN High E, B",0.0,doc="Gain for RIKEN detector Ax+B")
        #self.declareProperty("Detector ISIS Low E, A",1.0,doc="Gain for ISIS detector Ax+B")
        #self.declareProperty("Detector ISIS Low E, B",0.0,doc="Gain for ISIS detector Ax+B")

        #self.declareProperty(WorkspaceProperty("OutputWorkspace","",Direction.Output))

                def category(self):
                                return "CorrectionFunctions;Muon"

                def PyExec(self):
                                iws = self.getProperty("InputWorkspace").value
                                x1=self.getProperty("x1").value
                                y1=self.getProperty("y1").value
                                ows=WorkspaceFactory.create(iws)

                                for s in range(iws.getNumberHistograms()):
                                                y=ows.getAxis(1).getValue(s)
                                                print "y[",s,"]=",y
                                                ows.dataX(s)[:]=iws.dataX(s)[:]
                                                ows.dataE(s)[:]=iws.dataE(s)[:]
                                                for t in range(len(ows.dataY(s))):
                                                                x=ows.dataX(s)[t]
                                                                ows.dataY(s)[t]=iws.dataY(s)[t]+x1*x+y1*y

                                self.setProperty("OutputWorkspace",ows)

AlgorithmFactory.subscribe(ApplyNegMuCorrection)

Change History

comment:1 Changed 5 years ago by Stuart Campbell

This ticket has been transferred to github issue 12605

Note: See TracTickets for help on using tickets.