From 21e55d4ed79ef17d8c424d2068bff76f9c8b81b1 Mon Sep 17 00:00:00 2001 From: Peter Parker <peter.parker@stfc.ac.uk> Date: Thu, 19 Jul 2012 15:04:08 +0100 Subject: [PATCH] Refs #4868 - Correct ws names for result of seq FuryFit/ConvFit. * Pass extra details to Python functions where appropriate. * Cosmetic change to a label in the interface. * Add some divider comments to break up the code in IDA.py. --- .../inc/MantidQtCustomInterfaces/ConvFit.h | 4 +- .../inc/MantidQtCustomInterfaces/FuryFit.h | 3 +- .../inc/MantidQtCustomInterfaces/IDATab.h | 2 + .../IndirectDataAnalysis.ui | 2 +- .../MantidQt/CustomInterfaces/src/ConvFit.cpp | 104 ++++++++++++------ .../MantidQt/CustomInterfaces/src/FuryFit.cpp | 37 ++++--- .../MantidQt/CustomInterfaces/src/IDATab.cpp | 8 ++ .../scripts/Inelastic/IndirectDataAnalysis.py | 64 +++++++---- 8 files changed, 146 insertions(+), 78 deletions(-) diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ConvFit.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ConvFit.h index e10fa2fb1d8..4302f1285dd 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ConvFit.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ConvFit.h @@ -56,7 +56,9 @@ namespace IDA boost::shared_ptr<Mantid::API::CompositeFunction> createFunction(bool tie=false); QtProperty* createLorentzian(const QString &); void populateFunction(boost::shared_ptr<Mantid::API::IFunction>, boost::shared_ptr<Mantid::API::IFunction>, QtProperty*, const std::string & pref, const bool tie=false); - + QString fitTypeString() const; + QString backgroundString() const; + QIntValidator * m_intVal; QtStringPropertyManager* m_stringManager; QtTreePropertyBrowser* m_cfTree; diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/FuryFit.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/FuryFit.h index 96a0fa8c45d..39a2f66c680 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/FuryFit.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/FuryFit.h @@ -53,7 +53,8 @@ namespace IDA boost::shared_ptr<Mantid::API::IFunction> createUserFunction(const QString & name, bool tie=false); QtProperty* createExponential(const QString &); QtProperty* createStretchedExp(const QString &); - + QString fitTypeString() const; + QIntValidator * m_intVal; QtStringPropertyManager* m_stringManager; QtTreePropertyBrowser* m_ffTree; ///< FuryFit Property Browser diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IDATab.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IDATab.h index aeff9360cac..fe8fbc9ada4 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IDATab.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IDATab.h @@ -72,6 +72,8 @@ namespace IDA /// Returns a handle to the UI form object stored in the IndirectDataAnalysis class. Ui::IndirectDataAnalysis & uiForm(); + /// Returns a const handle to the UI form object stored in the IndirectDataAnalysis class. + const Ui::IndirectDataAnalysis & uiForm() const; /// Returns a handle to the DoubleEditorFactory object stored in the IndirectDataAnalysis class. DoubleEditorFactory * doubleEditorFactory(); /// Returns a handle to the QtCheckBoxFactory object stored in the IndirectDataAnalysis class. diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataAnalysis.ui b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataAnalysis.ui index fd97689128e..f1831edd988 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataAnalysis.ui +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/IndirectDataAnalysis.ui @@ -546,7 +546,7 @@ <item> <widget class="QLabel" name="furyfit_lbSpectra" > <property name="text" > - <string>Spectra:</string> + <string>Spectrum:</string> </property> </widget> </item> diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/ConvFit.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/ConvFit.cpp index 950013a4fea..69c3fbda5fe 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/ConvFit.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/ConvFit.cpp @@ -141,30 +141,11 @@ namespace IDA Mantid::API::CompositeFunction_sptr function = createFunction(); // get output name - QString ftype = ""; - switch ( uiForm().confit_cbFitType->currentIndex() ) - { - case 0: - ftype += "Delta"; break; - case 1: - ftype += "1L"; break; - case 2: - ftype += "2L"; break; - default: - break; - } - switch ( uiForm().confit_cbBackground->currentIndex() ) - { - case 0: - ftype += "FixF_s"; break; - case 1: - ftype += "FitF_s"; break; - case 2: - ftype += "FitL_s"; break; - } + QString ftype = fitTypeString(); + QString bg = backgroundString(); QString outputNm = runPythonCode(QString("from IndirectCommon import getWSprefix\nprint getWSprefix('") + QString::fromStdString(m_cfInputWSName) + QString("')\n")).trimmed(); - outputNm += QString("conv_") + ftype + uiForm().confit_leSpecNo->text(); + outputNm += QString("conv_") + ftype + bg + uiForm().confit_leSpecNo->text(); std::string output = outputNm.toStdString(); Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("Fit"); @@ -518,6 +499,67 @@ namespace IDA } } + /** + * Generate a string to describe the fit type selected by the user. + * Used when naming the resultant workspaces. + * + * Assertions used to guard against any future changes that dont take + * workspace naming into account. + * + * @returns the generated QString. + */ + QString ConvFit::fitTypeString() const + { + QString fitType(""); + + if( m_cfBlnMng->value(m_cfProp["UseDeltaFunc"]) ) + fitType += "Delta"; + + switch ( uiForm().confit_cbFitType->currentIndex() ) + { + case 0: + break; + case 1: + fitType += "1L"; break; + case 2: + fitType += "2L"; break; + default: + assert( false ); // Should never happen. + } + + // We should never get to a stage where the user is allowed to + // continue having not selected at least one fit - be it + // Lorentzian, delta, or both. + assert( ! fitType.isEmpty() ); + + return fitType; + } + + /** + * Generate a string to describe the background selected by the user. + * Used when naming the resultant workspaces. + * + * Assertions used to guard against any future changes that dont take + * workspace naming into account. + * + * @returns the generated QString. + */ + QString ConvFit::backgroundString() const + { + switch ( uiForm().confit_cbBackground->currentIndex() ) + { + case 0: + return "FixF_s"; + case 1: + return "FitF_s"; + case 2: + return "FitL_s"; + default: + assert( false ); // Should never happen. + return ""; + } + } + void ConvFit::typeSelection(int index) { m_cfTree->removeProperty(m_cfProp["Lorentzian1"]); @@ -714,19 +756,8 @@ namespace IDA return; } - QString bg = uiForm().confit_cbBackground->currentText(); - if ( bg == "Fixed Flat" ) - { - bg = "FixF"; - } - else if ( bg == "Fit Flat" ) - { - bg = "FitF"; - } - else if ( bg == "Fit Linear" ) - { - bg = "FitL"; - } + QString ftype = fitTypeString(); + QString bg = backgroundString(); Mantid::API::CompositeFunction_sptr func = createFunction(); std::string function = std::string(func->asString()); @@ -748,7 +779,8 @@ namespace IDA pyInput += "bg = '" + bg + "'\n" - "confitSeq(input, func, startx, endx, save, plot, bg, specMin, specMax)\n"; + "ftype = '" + ftype + "'\n" + "confitSeq(input, func, startx, endx, save, plot, ftype, bg, specMin, specMax)\n"; QString pyOutput = runPythonCode(pyInput); } diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/FuryFit.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/FuryFit.cpp index c3f0b03f730..81f3c342d1b 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/FuryFit.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/FuryFit.cpp @@ -103,7 +103,7 @@ namespace IDA auto function = createFunction(); uiForm().furyfit_ckPlotGuess->setChecked(false); - + const int fitType = uiForm().furyfit_cbFitType->currentIndex(); if ( uiForm().furyfit_ckConstrainIntensities->isChecked() ) @@ -122,20 +122,7 @@ namespace IDA break; } } - QString ftype; - switch ( fitType ) - { - case 0: - ftype = "1E_s"; break; - case 1: - ftype = "2E_s"; break; - case 2: - ftype = "1S_s"; break; - case 3: - ftype = "1E1S_s"; break; - default: - ftype = "s"; break; - } + QString ftype = fitTypeString(); plotInput(); if ( m_ffInputWS == NULL ) @@ -324,6 +311,23 @@ namespace IDA return prop; } + QString FuryFit::fitTypeString() const + { + switch ( uiForm().furyfit_cbFitType->currentIndex() ) + { + case 0: + return "1E_s"; + case 1: + return "2E_s"; + case 2: + return "1S_s"; + case 3: + return "1E1S_s"; + default: + return "s"; + }; + } + void FuryFit::typeSelection(int index) { m_ffTree->clear(); @@ -485,12 +489,13 @@ namespace IDA QString pyInput = "from IndirectDataAnalysis import furyfitSeq\n" "input = '" + QString::fromStdString(m_ffInputWSName) + "'\n" "func = r'" + QString::fromStdString(function) + "'\n" + "ftype = '" + fitTypeString() + "'\n" "startx = " + m_ffProp["StartX"]->valueText() + "\n" "endx = " + m_ffProp["EndX"]->valueText() + "\n" "plot = '" + uiForm().furyfit_cbPlotOutput->currentText() + "'\n" "save = "; pyInput += uiForm().furyfit_ckSaveSeq->isChecked() ? "True\n" : "False\n"; - pyInput += "furyfitSeq(input, func, startx, endx, save, plot)\n"; + pyInput += "furyfitSeq(input, func, ftype, startx, endx, save, plot)\n"; QString pyOutput = runPythonCode(pyInput); } diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/IDATab.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/IDATab.cpp index 768f1f437ce..e46770d067d 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/IDATab.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/IDATab.cpp @@ -172,6 +172,14 @@ namespace IDA { return m_parent->m_uiForm; } + + /** + * @returns a const handle to the UI form object stored in the IndirectDataAnalysis class. + */ + const Ui::IndirectDataAnalysis & IDATab::uiForm() const + { + return m_parent->m_uiForm; + } /** * @returns a handle to the DoubleEditorFactory object stored in the IndirectDataAnalysis class. diff --git a/Code/Mantid/scripts/Inelastic/IndirectDataAnalysis.py b/Code/Mantid/scripts/Inelastic/IndirectDataAnalysis.py index 862fbd34316..8ca949b8afd 100644 --- a/Code/Mantid/scripts/Inelastic/IndirectDataAnalysis.py +++ b/Code/Mantid/scripts/Inelastic/IndirectDataAnalysis.py @@ -5,6 +5,10 @@ from IndirectCommon import * from mantid import config, logger import math, re, os.path +############################################################################## +# Misc. Helper Functions +############################################################################## + def concatWSs(workspaces, unit, name): dataX = [] dataY = [] @@ -43,6 +47,10 @@ def trimData(nSpec, vals, min, max): result.append(val) return result +############################################################################## +# ConvFit +############################################################################## + def confitParsToWS(Table, Data, BackG='FixF', specMin=0, specMax=-1): if ( specMax == -1 ): specMax = mtd[Data].getNumberHistograms() - 1 @@ -70,8 +78,7 @@ def confitParsToWS(Table, Data, BackG='FixF', specMin=0, specMax=-1): dataE.append(ws.cell(row,eCol)) else: nSpec -= 1 - suffix = str(nSpec / 2) + 'L' + BackG - outNm = Table + suffix + outNm = Table + "_Workspace" xAxisTrimmed = trimData(nSpec, xAxisVals, specMin, specMax) CreateWorkspace(OutputWorkspace=outNm, DataX=xAxisTrimmed, DataY=dataY, DataE=dataE, Nspec=nSpec, UnitX='MomentumTransfer', VerticalAxisUnit='Text', @@ -94,27 +101,32 @@ def confitPlotSeq(inputWS, plot): plotSpecs.append(i) mp.plotSpectrum(inputWS, plotSpecs, True) -def confitSeq(inputWS, func, startX, endX, save, plot, bg, specMin, specMax): +def confitSeq(inputWS, func, startX, endX, save, plot, ftype, bg, specMin, specMax, Verbose=True): StartTime('ConvFit') - Verbose = True workdir = config['defaultsave.directory'] input = inputWS+',i' + str(specMin) if (specMax == -1): specMax = mtd[inputWS].getNumberHistograms() - 1 for i in range(specMin + 1, specMax + 1): input += ';'+inputWS+',i'+str(i) - outNm = getWSprefix(inputWS) + 'conv_' + outNm = getWSprefix(inputWS) + 'conv_' + ftype + bg + "_s" + str(specMin) + "_to_" + str(specMax) if Verbose: logger.notice(func) PlotPeakByLogValue(Input=input, OutputWorkspace=outNm, Function=func, StartX=startX, EndX=endX, FitType='Sequential') wsname = confitParsToWS(outNm, inputWS, bg, specMin, specMax) + RenameWorkspace(InputWorkspace=outNm, + OutputWorkspace=outNm + "_Parameters") if save: SaveNexusProcessed(InputWorkspace=wsname, Filename=wsname+'.nxs') if plot != 'None': confitPlotSeq(wsname, plot) EndTime('ConvFit') +############################################################################## +# Elwin +############################################################################## + def elwin(inputFiles, eRange, Save=False, Verbose=True, Plot=False): StartTime('ElWin') Verbose = True @@ -175,6 +187,15 @@ def elwinPlot(eq1,eq2): layer = graph2.activeLayer() layer.setScale(mp.Layer.Bottom, 0.0, lastXeq2) +############################################################################## +# Fury +############################################################################## + +def furyPlot(inWS, spec): + graph = mp.plotSpectrum(inWS, spec) + layer = graph.activeLayer() + layer.setScale(mp.Layer.Left, 0, 1.0) + def fury(sam_files, res_file, rebinParam, RES=True, Save=False, Verbose=False, Plot=False): StartTime('Fury') @@ -250,11 +271,10 @@ def fury(sam_files, res_file, rebinParam, RES=True, Save=False, Verbose=False, furyPlot(outWSlist, specrange) EndTime('Fury') return outWSlist - -def furyPlot(inWS, spec): - graph = mp.plotSpectrum(inWS, spec) - layer = graph.activeLayer() - layer.setScale(mp.Layer.Left, 0, 1.0) + +############################################################################## +# FuryFit +############################################################################## def furyfitParsToWS(Table, Data): dataX = createQaxis(Data) @@ -291,13 +311,7 @@ def furyfitParsToWS(Table, Data): stretched += 1 # are stretched exponentials else: nSpec -= 1 - suffix = '' - nE = ( nSpec / 2 ) - stretched - if ( nE > 0 ): - suffix += str(nE) + 'E' - if ( stretched > 0 ): - suffix += str(stretched) + 'S' - wsname = Table + suffix + wsname = Table + "_Workspace" CreateWorkspace(OutputWorkspace=wsname, DataX=xAxisVals, DataY=dataY, DataE=dataE, Nspec=nSpec, UnitX='MomentumTransfer', VerticalAxisUnit='Text', VerticalAxisValues=names) @@ -321,20 +335,20 @@ def furyfitPlotSeq(inputWS, Plot): plotSpecs.append(i) mp.plotSpectrum(inputWS, plotSpecs, True) -def furyfitSeq(inputWS, func, startx, endx, Save, Plot): +def furyfitSeq(inputWS, func, ftype, startx, endx, Save, Plot, Verbose = True): StartTime('FuryFit') - Verbose = True workdir = config['defaultsave.directory'] input = inputWS+',i0' nHist = mtd[inputWS].getNumberHistograms() for i in range(1,nHist): input += ';'+inputWS+',i'+str(i) - outNm = getWSprefix(inputWS) + 'fury_' + outNm = getWSprefix(inputWS) + 'fury_' + ftype + "0_to_" + str(nHist-1) if Verbose: logger.notice(func) PlotPeakByLogValue(Input=input, OutputWorkspace=outNm, Function=func, StartX=startx, EndX=endx, FitType='Sequential') wsname = furyfitParsToWS(outNm, inputWS) + RenameWorkspace(InputWorkspace=outNm, OutputWorkspace=outNm+"_Parameters") if Save: opath = os.path.join(workdir, wsname+'.nxs') # path name for nxs file SaveNexusProcessed(InputWorkspace=wsname, Filename=opath) @@ -438,6 +452,10 @@ def furyfitMult(inputWS, func, startx, endx, Save, Plot): furyfitPlotMult(wsname, Plot) EndTime('FuryFit') +############################################################################## +# MSDFit +############################################################################## + def msdfitParsToWS(Table, xData): dataX = xData ws = mtd[Table+'_Table'] @@ -594,9 +612,9 @@ def plotInput(inputfiles,spectra=[]): graph = mp.plotSpectrum(workspaces,0) layer = graph.activeLayer().setTitle(", ".join(workspaces)) -############################################################################### -## abscor ##################################################################### -############################################################################### +############################################################################## +# AppCorr +############################################################################## def CubicFit(inputWS, spec, Verbose=False): '''Uses the Mantid Fit Algorithm to fit a quadratic to the inputWS -- GitLab