| 1 | import math |
|---|
| 2 | import numpy |
|---|
| 3 | from mantid.api import * # PythonAlgorithm, registerAlgorithm, WorkspaceProperty |
|---|
| 4 | from mantid.kernel import * |
|---|
| 5 | from mantid.simpleapi import * |
|---|
| 6 | |
|---|
| 7 | class LeakyFunction(IFunction1D): |
|---|
| 8 | # equivalent to ExpDecayOsc |
|---|
| 9 | def init(self): |
|---|
| 10 | self.declareParameter("A",1.0) |
|---|
| 11 | self.declareParameter("Lambda",1.0) |
|---|
| 12 | self.declareParameter("Frequency",1.0) |
|---|
| 13 | self.declareParameter("Phi",1.0) |
|---|
| 14 | |
|---|
| 15 | def function1D(self,xvals): |
|---|
| 16 | a=self.getParameterValue("A") |
|---|
| 17 | lam=self.getParameterValue("Lambda") |
|---|
| 18 | frq=self.getParameterValue("Frequency") |
|---|
| 19 | phi=self.getParameterValue("Phi") |
|---|
| 20 | return a*numpy.cos(xvals*frq*2.0*math.pi+phi)*numpy.exp(-xvals*lam) |
|---|
| 21 | |
|---|
| 22 | FunctionFactory.subscribe(LeakyFunction) |
|---|
| 23 | |
|---|
| 24 | class LeakyFit(PythonAlgorithm): |
|---|
| 25 | def PyInit(self): |
|---|
| 26 | #self.declareProperty(WorkspaceProperty("InputWS","",Direction.Input),"Source run") |
|---|
| 27 | #self.declareProperty(WorkspaceProperty("InputWS","",Direction.Input),"Data to fit, from CreateALCMap") |
|---|
| 28 | self.declareProperty(WorkspaceProperty("OutputWSChi","",Direction.Output),"Chisq as a function of fixed F") |
|---|
| 29 | self.declareProperty(WorkspaceProperty("OutputWSA","",Direction.Output),"Signal ampl as a function of fixed F") |
|---|
| 30 | #self.declareProperty("StartF",0.0,doc="Lowest val of F") |
|---|
| 31 | #self.declareProperty("EndF",0.5,doc="Highest val of F") |
|---|
| 32 | #self.declareProperty("FPoints",20,doc="Number of F values to try") |
|---|
| 33 | self.declareProperty("UsePythonFunc",True,doc="Use Python function instead of built in C++ one") |
|---|
| 34 | |
|---|
| 35 | def category(self): |
|---|
| 36 | return "Muon" |
|---|
| 37 | |
|---|
| 38 | def PyExec(self): |
|---|
| 39 | #sourcedat=self.getProperty("InputWS").value |
|---|
| 40 | #(flatsource,Nspec,Y0,dY,dX)=QuantumFlattenWorkspace(InputWorkspace=sourcedat) |
|---|
| 41 | sourcedat=WorkspaceFactory.create("Workspace2D",NVectors=1,XLength=5000,YLength=5000) |
|---|
| 42 | sourcedat.dataX(0)[:]=numpy.linspace(0.0,32.0,5000) |
|---|
| 43 | sourcedat.dataY(0)[:]=10.0*numpy.cos(sourcedat.dataX(0))+0.1*numpy.random.randn(5000) |
|---|
| 44 | sourcedat.dataE(0)[:]=numpy.ones(5000)*0.1 |
|---|
| 45 | # Y0=field val of 1st spectrum, dY=field increment, dX=X value offset between spectra |
|---|
| 46 | |
|---|
| 47 | #StartF=self.getProperty("StartF").value |
|---|
| 48 | #EndF=self.getProperty("EndF").value |
|---|
| 49 | #FPoints=self.getProperty("FPoints").value |
|---|
| 50 | StartF=0.1 |
|---|
| 51 | EndF=0.5 |
|---|
| 52 | FPoints=20 |
|---|
| 53 | usePythonFunc=self.getProperty("UsePythonFunc").value |
|---|
| 54 | resultws=WorkspaceFactory.create("Workspace2D",NVectors=1,XLength=FPoints,YLength=FPoints) |
|---|
| 55 | amplws=WorkspaceFactory.create("Workspace2D",NVectors=1,XLength=FPoints,YLength=FPoints) |
|---|
| 56 | # progress |
|---|
| 57 | prog_reporter=Progress(self,start=0.0, end=1.0, nreports=FPoints) |
|---|
| 58 | FVals=numpy.linspace(StartF,EndF,FPoints) |
|---|
| 59 | resultws.dataX(0)[:]=FVals |
|---|
| 60 | amplws.dataX(0)[:]=FVals |
|---|
| 61 | |
|---|
| 62 | for j in range(FPoints): |
|---|
| 63 | F=FVals[j] |
|---|
| 64 | if(usePythonFunc): |
|---|
| 65 | (stat,chisq,Covar,params,curves)=Fit(Function="name=LeakyFunction,A=0.2,Lambda=0.05,Frequency="+str(F)+",Phi=0.0", |
|---|
| 66 | ties="Frequency="+str(F),InputWorkspace=sourcedat,Output="sourcedat") |
|---|
| 67 | else: |
|---|
| 68 | (stat,chisq,Covar,params,curves)=Fit(Function="name=ExpDecayOsc,A=0.2,Lambda=0.05,Frequency="+str(F)+",Phi=0.0", |
|---|
| 69 | ties="Frequency="+str(F),InputWorkspace=sourcedat,Output="sourcedat") |
|---|
| 70 | resultws.dataY(0)[j]=chisq |
|---|
| 71 | amplws.dataY(0)[j]=params.column(1)[0] |
|---|
| 72 | DeleteWorkspace(Covar) |
|---|
| 73 | DeleteWorkspace(params) |
|---|
| 74 | DeleteWorkspace(curves) |
|---|
| 75 | |
|---|
| 76 | prog_reporter.report("Processing") |
|---|
| 77 | self.setProperty("OutputWSChi",resultws) |
|---|
| 78 | self.setProperty("OutputWSA",amplws) |
|---|
| 79 | |
|---|
| 80 | AlgorithmFactory.subscribe(LeakyFit) |
|---|