File indexing completed on 2024-12-22 04:18:06
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 "noiseaddition.h" 0017 #include "objectstore.h" 0018 #include "ui_noiseadditionconfig.h" 0019 0020 #include <gsl/gsl_rng.h> 0021 #include <gsl/gsl_randist.h> 0022 0023 static const QString& VECTOR_IN = "Vector In"; 0024 static const QString& SCALAR_IN = "Scalar In"; 0025 static const QString& VECTOR_OUT = "Vector Out"; 0026 0027 class ConfigNoiseAdditionPlugin : public Kst::DataObjectConfigWidget, public Ui_NoiseAdditionConfig { 0028 public: 0029 ConfigNoiseAdditionPlugin(QSettings* cfg) : DataObjectConfigWidget(cfg), Ui_NoiseAdditionConfig() { 0030 _store = 0; 0031 setupUi(this); 0032 } 0033 0034 ~ConfigNoiseAdditionPlugin() {} 0035 0036 void setObjectStore(Kst::ObjectStore* store) { 0037 _store = store; 0038 _vector->setObjectStore(store); 0039 _scalarSigma->setObjectStore(store); 0040 _scalarSigma->setDefaultValue(0); 0041 } 0042 0043 void setupSlots(QWidget* dialog) { 0044 if (dialog) { 0045 connect(_vector, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0046 connect(_scalarSigma, SIGNAL(selectionChanged(QString)), dialog, SIGNAL(modified())); 0047 } 0048 } 0049 0050 Kst::VectorPtr selectedVector() { return _vector->selectedVector(); }; 0051 void setSelectedVector(Kst::VectorPtr vector) { return _vector->setSelectedVector(vector); }; 0052 0053 Kst::ScalarPtr selectedScalar() { return _scalarSigma->selectedScalar(); }; 0054 void setSelectedScalar(Kst::ScalarPtr scalar) { return _scalarSigma->setSelectedScalar(scalar); }; 0055 0056 virtual void setupFromObject(Kst::Object* dataObject) { 0057 if (NoiseAdditionSource* source = static_cast<NoiseAdditionSource*>(dataObject)) { 0058 setSelectedVector(source->vector()); 0059 setSelectedScalar(source->scalarSigma()); 0060 } 0061 } 0062 0063 virtual bool configurePropertiesFromXml(Kst::ObjectStore *store, QXmlStreamAttributes& attrs) { 0064 Q_UNUSED(store); 0065 Q_UNUSED(attrs); 0066 0067 bool validTag = true; 0068 0069 // QStringRef av; 0070 // av = attrs.value("value"); 0071 // if (!av.isNull()) { 0072 // _configValue = QVariant(av.toString()).toBool(); 0073 // } 0074 0075 return validTag; 0076 } 0077 0078 public slots: 0079 virtual void save() { 0080 if (_cfg) { 0081 _cfg->beginGroup("Noise Addition DataObject Plugin"); 0082 _cfg->setValue("Input Vector", _vector->selectedVector()->Name()); 0083 _cfg->setValue("Input Scalar Sigma", _scalarSigma->selectedScalar()->Name()); 0084 _cfg->endGroup(); 0085 } 0086 } 0087 0088 virtual void load() { 0089 if (_cfg && _store) { 0090 _cfg->beginGroup("Noise Addition DataObject Plugin"); 0091 QString vectorName = _cfg->value("Input Vector").toString(); 0092 Kst::Object* object = _store->retrieveObject(vectorName); 0093 Kst::Vector* vector = static_cast<Kst::Vector*>(object); 0094 if (vector) { 0095 setSelectedVector(vector); 0096 } 0097 QString scalarName = _cfg->value("Input Scalar Sigma").toString(); 0098 object = _store->retrieveObject(scalarName); 0099 Kst::Scalar* scalar = static_cast<Kst::Scalar*>(object); 0100 if (scalar) { 0101 setSelectedScalar(scalar); 0102 } 0103 _cfg->endGroup(); 0104 } 0105 } 0106 0107 private: 0108 Kst::ObjectStore *_store; 0109 0110 }; 0111 0112 0113 NoiseAdditionSource::NoiseAdditionSource(Kst::ObjectStore *store) 0114 : Kst::BasicPlugin(store) { 0115 } 0116 0117 0118 NoiseAdditionSource::~NoiseAdditionSource() { 0119 } 0120 0121 0122 QString NoiseAdditionSource::_automaticDescriptiveName() const { 0123 return tr("Noise Addition Plugin Object"); 0124 } 0125 0126 0127 void NoiseAdditionSource::change(Kst::DataObjectConfigWidget *configWidget) { 0128 if (ConfigNoiseAdditionPlugin* config = static_cast<ConfigNoiseAdditionPlugin*>(configWidget)) { 0129 setInputVector(VECTOR_IN, config->selectedVector()); 0130 setInputScalar(SCALAR_IN, config->selectedScalar()); 0131 } 0132 } 0133 0134 0135 void NoiseAdditionSource::setupOutputs() { 0136 setOutputVector(VECTOR_OUT, ""); 0137 } 0138 0139 0140 bool NoiseAdditionSource::algorithm() { 0141 Kst::VectorPtr inputVector = _inputVectors[VECTOR_IN]; 0142 Kst::ScalarPtr inputScalar = _inputScalars[SCALAR_IN]; 0143 Kst::VectorPtr outputVector = _outputVectors[VECTOR_OUT]; 0144 0145 //Make sure there is at least 1 element in the input vector 0146 if (inputVector->length() < 1) { 0147 _errorString = tr("Error: Input Vector invalid size"); 0148 return false; 0149 } 0150 0151 const gsl_rng_type* pGeneratorType; 0152 gsl_rng* pRandomNumberGenerator; 0153 double* pResult[1]; 0154 int iRetVal = false; 0155 int iLength = inputVector->length(); 0156 0157 outputVector->resize(iLength, false); 0158 pResult[0] = outputVector->raw_V_ptr(); 0159 0160 pGeneratorType = gsl_rng_default; 0161 pRandomNumberGenerator = gsl_rng_alloc( pGeneratorType ); 0162 if (pRandomNumberGenerator != NULL) { 0163 if (pResult[0] != NULL) { 0164 for (int i=0; i<iLength; i++) { 0165 outputVector->raw_V_ptr()[i] = inputVector->value()[i] + gsl_ran_gaussian( pRandomNumberGenerator, inputScalar->value() ); 0166 } 0167 0168 iRetVal = true; 0169 } 0170 gsl_rng_free( pRandomNumberGenerator ); 0171 } 0172 0173 return iRetVal; 0174 } 0175 0176 0177 Kst::VectorPtr NoiseAdditionSource::vector() const { 0178 return _inputVectors[VECTOR_IN]; 0179 } 0180 0181 0182 Kst::ScalarPtr NoiseAdditionSource::scalarSigma() const { 0183 return _inputScalars[SCALAR_IN]; 0184 } 0185 0186 0187 QStringList NoiseAdditionSource::inputVectorList() const { 0188 return QStringList( VECTOR_IN ); 0189 } 0190 0191 0192 QStringList NoiseAdditionSource::inputScalarList() const { 0193 return QStringList( SCALAR_IN ); 0194 } 0195 0196 0197 QStringList NoiseAdditionSource::inputStringList() const { 0198 return QStringList( /*STRING_IN*/ ); 0199 } 0200 0201 0202 QStringList NoiseAdditionSource::outputVectorList() const { 0203 return QStringList( VECTOR_OUT ); 0204 } 0205 0206 0207 QStringList NoiseAdditionSource::outputScalarList() const { 0208 return QStringList( /*SCALAR_OUT*/ ); 0209 } 0210 0211 0212 QStringList NoiseAdditionSource::outputStringList() const { 0213 return QStringList( /*STRING_OUT*/ ); 0214 } 0215 0216 0217 void NoiseAdditionSource::saveProperties(QXmlStreamWriter &s) { 0218 Q_UNUSED(s); 0219 // s.writeAttribute("value", _configValue); 0220 } 0221 0222 0223 QString NoiseAdditionPlugin::pluginName() const { return tr("Noise Addition"); } 0224 QString NoiseAdditionPlugin::pluginDescription() const { return tr("Adds Gaussian noise to a set of data, of a specified standard deviation."); } 0225 0226 0227 Kst::DataObject *NoiseAdditionPlugin::create(Kst::ObjectStore *store, Kst::DataObjectConfigWidget *configWidget, bool setupInputsOutputs) const { 0228 0229 if (ConfigNoiseAdditionPlugin* config = static_cast<ConfigNoiseAdditionPlugin*>(configWidget)) { 0230 0231 NoiseAdditionSource* object = store->createObject<NoiseAdditionSource>(); 0232 0233 if (setupInputsOutputs) { 0234 object->setInputScalar(SCALAR_IN, config->selectedScalar()); 0235 object->setupOutputs(); 0236 object->setInputVector(VECTOR_IN, config->selectedVector()); 0237 } 0238 0239 object->setPluginName(pluginName()); 0240 0241 object->writeLock(); 0242 object->registerChange(); 0243 object->unlock(); 0244 0245 return object; 0246 } 0247 return 0; 0248 } 0249 0250 0251 Kst::DataObjectConfigWidget *NoiseAdditionPlugin::configWidget(QSettings *settingsObject) const { 0252 ConfigNoiseAdditionPlugin *widget = new ConfigNoiseAdditionPlugin(settingsObject); 0253 return widget; 0254 } 0255 0256 #ifndef QT5 0257 Q_EXPORT_PLUGIN2(kstplugin_BinPlugin, NoiseAdditionPlugin) 0258 #endif 0259 0260 // vim: ts=2 sw=2 et