File indexing completed on 2024-12-22 04:18:12
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * copyright : (C) 2005 University of British Columbia * 0006 * dscott@phas.ubc.ca * 0007 * * 0008 * This program is free software; you can redistribute it and/or modify * 0009 * it under the terms of the GNU General Public License as published by * 0010 * the Free Software Foundation; either version 2 of the License, or * 0011 * (at your option) any later version. * 0012 * * 0013 ***************************************************************************/ 0014 0015 0016 #include "fitgradient_unweighted.h" 0017 #include "objectstore.h" 0018 #include "ui_fitgradient_unweightedconfig.h" 0019 0020 #include <gsl/gsl_fit.h> 0021 #include "../common.h" 0022 0023 static const QString& VECTOR_IN_X = "X Vector"; 0024 static const QString& VECTOR_IN_Y = "Y Vector"; 0025 static const QString& VECTOR_OUT_Y_FITTED = "Fit"; 0026 static const QString& VECTOR_OUT_Y_RESIDUALS = "Residuals"; 0027 static const QString& VECTOR_OUT_Y_PARAMETERS = "Parameters Vector"; 0028 static const QString& VECTOR_OUT_Y_COVARIANCE = "Covariance"; 0029 static const QString& VECTOR_OUT_Y_LO = "Lo Vector"; 0030 static const QString& VECTOR_OUT_Y_HI = "Hi Vector"; 0031 static const QString& SCALAR_OUT = "chi^2/nu"; 0032 0033 class ConfigWidgetFitGradientUnweightedPlugin : public Kst::DataObjectConfigWidget, public Ui_FitGradient_UnweightedConfig { 0034 public: 0035 ConfigWidgetFitGradientUnweightedPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_FitGradient_UnweightedConfig() { 0036 _store = 0; 0037 setupUi(this); 0038 } 0039 0040 ~ConfigWidgetFitGradientUnweightedPlugin() {} 0041 0042 void setObjectStore(Kst::ObjectStore* store) { 0043 _store = store; 0044 _vectorX->setObjectStore(store); 0045 _vectorY->setObjectStore(store); 0046 } 0047 0048 void setupSlots(QWidget* dialog) { 0049 if (dialog) { 0050 connect(_vectorX, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0051 connect(_vectorY, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0052 } 0053 } 0054 0055 void setVectorX(Kst::VectorPtr vector) { 0056 setSelectedVectorX(vector); 0057 } 0058 0059 void setVectorY(Kst::VectorPtr vector) { 0060 setSelectedVectorY(vector); 0061 } 0062 0063 void setVectorsLocked(bool locked = true) { 0064 _vectorX->setEnabled(!locked); 0065 _vectorY->setEnabled(!locked); 0066 } 0067 0068 Kst::VectorPtr selectedVectorX() { return _vectorX->selectedVector(); }; 0069 void setSelectedVectorX(Kst::VectorPtr vector) { return _vectorX->setSelectedVector(vector); }; 0070 0071 Kst::VectorPtr selectedVectorY() { return _vectorY->selectedVector(); }; 0072 void setSelectedVectorY(Kst::VectorPtr vector) { return _vectorY->setSelectedVector(vector); }; 0073 0074 virtual void setupFromObject(Kst::Object* dataObject) { 0075 if (FitGradientUnweightedSource* source = static_cast<FitGradientUnweightedSource*>(dataObject)) { 0076 setSelectedVectorX(source->vectorX()); 0077 setSelectedVectorY(source->vectorY()); 0078 } 0079 } 0080 0081 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0082 Q_UNUSED(store); 0083 Q_UNUSED(attrs); 0084 0085 bool validTag = true; 0086 0087 // QStringRef av; 0088 // av = attrs.value("value"); 0089 // if (!av.isNull()) { 0090 // _configValue = QVariant(av.toString()).toBool(); 0091 // } 0092 0093 return validTag; 0094 } 0095 0096 public slots: 0097 virtual void save() { 0098 if (_cfg) { 0099 _cfg->beginGroup("Fit Gradient Plugin"); 0100 _cfg->setValue("Input Vector X", _vectorX->selectedVector()->Name()); 0101 _cfg->setValue("Input Vector Y", _vectorY->selectedVector()->Name()); 0102 _cfg->endGroup(); 0103 } 0104 } 0105 0106 virtual void load() { 0107 if (_cfg && _store) { 0108 _cfg->beginGroup("Fit Gradient Plugin"); 0109 QString vectorName = _cfg->value("Input Vector X").toString(); 0110 Kst::Object* object = _store->retrieveObject(vectorName); 0111 Kst::Vector* vectorx = static_cast<Kst::Vector*>(object); 0112 if (vectorx) { 0113 setSelectedVectorX(vectorx); 0114 } 0115 vectorName = _cfg->value("Input Vector Y").toString(); 0116 object = _store->retrieveObject(vectorName); 0117 Kst::Vector* vectory = static_cast<Kst::Vector*>(object); 0118 if (vectory) { 0119 setSelectedVectorX(vectory); 0120 } 0121 _cfg->endGroup(); 0122 } 0123 } 0124 0125 private: 0126 Kst::ObjectStore *_store; 0127 0128 }; 0129 0130 0131 FitGradientUnweightedSource::FitGradientUnweightedSource(Kst::ObjectStore *store) 0132 : Kst::BasicPlugin(store) { 0133 } 0134 0135 0136 FitGradientUnweightedSource::~FitGradientUnweightedSource() { 0137 } 0138 0139 0140 QString FitGradientUnweightedSource::_automaticDescriptiveName() const { 0141 return tr("%1 Unweighted Gradient").arg(vectorY()->descriptiveName()); 0142 } 0143 0144 0145 void FitGradientUnweightedSource::change(Kst::DataObjectConfigWidget *configWidget) { 0146 if (ConfigWidgetFitGradientUnweightedPlugin* config = static_cast<ConfigWidgetFitGradientUnweightedPlugin*>(configWidget)) { 0147 setInputVector(VECTOR_IN_X, config->selectedVectorX()); 0148 setInputVector(VECTOR_IN_Y, config->selectedVectorY()); 0149 } 0150 } 0151 0152 0153 void FitGradientUnweightedSource::setupOutputs() { 0154 setOutputVector(VECTOR_OUT_Y_FITTED, ""); 0155 setOutputVector(VECTOR_OUT_Y_RESIDUALS, ""); 0156 setOutputVector(VECTOR_OUT_Y_PARAMETERS, ""); 0157 setOutputVector(VECTOR_OUT_Y_COVARIANCE, ""); 0158 setOutputVector(VECTOR_OUT_Y_LO, ""); 0159 setOutputVector(VECTOR_OUT_Y_HI, ""); 0160 setOutputScalar(SCALAR_OUT, ""); 0161 } 0162 0163 0164 bool FitGradientUnweightedSource::algorithm() { 0165 Kst::VectorPtr inputVectorX = _inputVectors[VECTOR_IN_X]; 0166 Kst::VectorPtr inputVectorY = _inputVectors[VECTOR_IN_Y]; 0167 0168 Kst::VectorPtr outputVectorYFitted = _outputVectors[VECTOR_OUT_Y_FITTED]; 0169 Kst::VectorPtr outputVectorYResiduals = _outputVectors[VECTOR_OUT_Y_RESIDUALS]; 0170 Kst::VectorPtr outputVectorYParameters = _outputVectors[VECTOR_OUT_Y_PARAMETERS]; 0171 Kst::VectorPtr outputVectorYCovariance = _outputVectors[VECTOR_OUT_Y_COVARIANCE]; 0172 Kst::VectorPtr outputVectorYLo = _outputVectors[VECTOR_OUT_Y_LO]; 0173 Kst::VectorPtr outputVectorYHi = _outputVectors[VECTOR_OUT_Y_HI]; 0174 Kst::ScalarPtr outputScalar = _outputScalars[SCALAR_OUT]; 0175 0176 0177 Kst::LabelInfo label_info = inputVectorY->labelInfo(); 0178 label_info.name = tr("Gradient Fit to %1").arg(label_info.name); 0179 outputVectorYFitted->setLabelInfo(label_info); 0180 0181 label_info.name = tr("Gradient Fit Residuals"); 0182 outputVectorYResiduals->setLabelInfo(label_info); 0183 0184 label_info.name = tr("Gradient Fit Lower Limit"); 0185 outputVectorYLo->setLabelInfo(label_info); 0186 0187 label_info.name = tr("Gradient Fit Upper Limit"); 0188 outputVectorYHi->setLabelInfo(label_info); 0189 0190 int i = 0; 0191 int iLength = 0; 0192 bool bReturn = false; 0193 double* pInputs[3]; 0194 double c0 = 0.0; 0195 double cov00 = 0.0; 0196 double dSumSq = 0.0; 0197 double y; 0198 double yErr; 0199 0200 if( precursor( inputVectorX, inputVectorY, 0, &iLength, false, true, 2, pInputs, outputVectorYFitted, outputVectorYResiduals, outputVectorYParameters, outputVectorYCovariance, outputVectorYLo, outputVectorYHi ) ) { 0201 0202 if( !gsl_fit_mul( pInputs[XVALUES], 1, pInputs[YVALUES], 1, iLength, &c0, &cov00, &dSumSq ) ) { 0203 for( i=0; i<iLength; ++i ) { 0204 gsl_fit_mul_est( pInputs[XVALUES][i], c0, cov00, &y, &yErr ); 0205 0206 outputVectorYFitted->raw_V_ptr()[i] = y; 0207 outputVectorYResiduals->raw_V_ptr()[i] = pInputs[YVALUES][i] - y; 0208 outputVectorYLo->raw_V_ptr()[i] = y - yErr; 0209 outputVectorYHi->raw_V_ptr()[i] = y + yErr; 0210 } 0211 0212 outputVectorYParameters->raw_V_ptr()[0] = c0; 0213 outputVectorYCovariance->raw_V_ptr()[0] = cov00; 0214 0215 outputScalar->setValue(dSumSq / ( (double)iLength - 2.0 )); 0216 0217 bReturn = true; 0218 } 0219 } 0220 0221 postcursor( false, pInputs ); 0222 0223 return bReturn; 0224 } 0225 0226 0227 Kst::VectorPtr FitGradientUnweightedSource::vectorX() const { 0228 return _inputVectors[VECTOR_IN_X]; 0229 } 0230 0231 0232 Kst::VectorPtr FitGradientUnweightedSource::vectorY() const { 0233 return _inputVectors[VECTOR_IN_Y]; 0234 } 0235 0236 0237 QStringList FitGradientUnweightedSource::inputVectorList() const { 0238 QStringList vectors(VECTOR_IN_X); 0239 vectors += VECTOR_IN_Y; 0240 return vectors; 0241 } 0242 0243 0244 QStringList FitGradientUnweightedSource::inputScalarList() const { 0245 return QStringList(); 0246 } 0247 0248 0249 QStringList FitGradientUnweightedSource::inputStringList() const { 0250 return QStringList( /*STRING_IN*/ ); 0251 } 0252 0253 0254 QStringList FitGradientUnweightedSource::outputVectorList() const { 0255 QStringList vectors(VECTOR_OUT_Y_FITTED); 0256 vectors += VECTOR_OUT_Y_RESIDUALS; 0257 vectors += VECTOR_OUT_Y_PARAMETERS; 0258 vectors += VECTOR_OUT_Y_COVARIANCE; 0259 vectors += VECTOR_OUT_Y_LO; 0260 vectors += VECTOR_OUT_Y_HI; 0261 vectors += VECTOR_OUT_Y_PARAMETERS; 0262 return vectors; 0263 } 0264 0265 0266 QStringList FitGradientUnweightedSource::outputScalarList() const { 0267 return QStringList( SCALAR_OUT ); 0268 } 0269 0270 0271 QStringList FitGradientUnweightedSource::outputStringList() const { 0272 return QStringList( /*STRING_OUT*/ ); 0273 } 0274 0275 0276 void FitGradientUnweightedSource::saveProperties(QXmlStreamWriter &s) { 0277 Q_UNUSED(s); 0278 // s.writeAttribute("value", _configValue); 0279 } 0280 0281 0282 QString FitGradientUnweightedSource::parameterName(int index) const { 0283 QString parameter; 0284 switch (index) { 0285 case 0: 0286 parameter = "Gradient"; 0287 break; 0288 } 0289 0290 return parameter; 0291 } 0292 0293 0294 // Name used to identify the plugin. Used when loading the plugin. 0295 QString FitGradientUnweightedPlugin::pluginName() const { return tr("Gradient Fit"); } 0296 QString FitGradientUnweightedPlugin::pluginDescription() const { return tr("Generates a gradient fit for a set of data."); } 0297 0298 0299 Kst::DataObject *FitGradientUnweightedPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0300 0301 if (ConfigWidgetFitGradientUnweightedPlugin* config = static_cast<ConfigWidgetFitGradientUnweightedPlugin*>(configWidget)) { 0302 0303 FitGradientUnweightedSource* object = store->createObject<FitGradientUnweightedSource>(); 0304 0305 if (setupInputsOutputs) { 0306 object->setupOutputs(); 0307 object->setInputVector(VECTOR_IN_X, config->selectedVectorX()); 0308 object->setInputVector(VECTOR_IN_Y, config->selectedVectorY()); 0309 } 0310 0311 object->setPluginName(pluginName()); 0312 0313 object->writeLock(); 0314 object->registerChange(); 0315 object->unlock(); 0316 0317 return object; 0318 } 0319 return 0; 0320 } 0321 0322 0323 Kst::DataObjectConfigWidget *FitGradientUnweightedPlugin::configWidget(QSettings *settingsObject) const { 0324 ConfigWidgetFitGradientUnweightedPlugin *widget = new ConfigWidgetFitGradientUnweightedPlugin(settingsObject); 0325 return widget; 0326 } 0327 0328 #ifndef QT5 0329 Q_EXPORT_PLUGIN2(kstplugin_FitGradientUnweightedPlugin, FitGradientUnweightedPlugin) 0330 #endif 0331 0332 // vim: ts=2 sw=2 et