Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

NoiseParametersDialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   SpikeStream Application                                               *
00003  *   Copyright (C) 2007 by David Gamez                                     *
00004  *   david@davidgamez.eu                                                   *
00005  *   Version 0.1                                                           *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 //SpikeStream includes
00024 #include "NoiseParametersDialog.h"
00025 #include "Debug.h"
00026 #include "Utilities.h"
00027 #include "SpikeStreamMainWindow.h"
00028 
00029 //Qt includes
00030 #include <qlayout.h>
00031 #include <qmessagebox.h>
00032 
00033 //Other includes
00034 #include <math.h>
00035 #include <mysql++.h>
00036 #include <iostream>
00037 using namespace std;
00038 using namespace mysqlpp;
00039 
00040 
00041 /*! Constructor. */
00042 NoiseParametersDialog::NoiseParametersDialog(QWidget *parent, DBInterface* netDBInter, SimulationManager* simMan) : QDialog(parent, "NoiseParamDlg", false){
00043         //Store reference to database
00044         networkDBInterface = netDBInter;
00045 
00046         //Store reference to simulation manager
00047         simulationManager = simMan;
00048 
00049         //Set caption
00050         this->setCaption("Noise Parameters");   
00051 
00052         //Create box to organise dialog
00053         QVBoxLayout *verticalBox = new QVBoxLayout(this, 2, 2);
00054 
00055         //Create parameter table 
00056         paramTable = new QTable(0, 0, this);
00057         paramTable->setShowGrid(false);
00058         paramTable->setSorting(false);
00059         paramTable->setSelectionMode(QTable::NoSelection);
00060         paramTable->verticalHeader()->hide();
00061         paramTable->setLeftMargin(0);
00062         paramTableHeader = paramTable->horizontalHeader();
00063 
00064         //Add up the total width of the columns to set the minimum size
00065         unsigned int tempTotalColumnWidth = 0;
00066 
00067         //Add column for neuron group id
00068         unsigned int insertionPoint = paramTable->numCols();
00069         paramTable->insertColumns(insertionPoint, 1);
00070         paramTableHeader->setLabel( insertionPoint, "Neuron Group" );
00071         paramTable->setColumnWidth( insertionPoint, 200);
00072         neurGrpCol = insertionPoint;
00073         tempTotalColumnWidth += 200;
00074 
00075         //Add column to set noise mode
00076         insertionPoint = paramTable->numCols();
00077         paramTable->insertColumns(insertionPoint, 1);
00078         paramTableHeader->setLabel(insertionPoint, "Noise" );
00079         paramTable->setColumnWidth(insertionPoint, 60);
00080         noiseModeCol = insertionPoint;
00081         tempTotalColumnWidth += 60;
00082 
00083         //Add column for percent of neurons
00084         insertionPoint = paramTable->numCols();
00085         paramTable->insertColumns(insertionPoint, 1);
00086         paramTableHeader->setLabel(insertionPoint, "Percent of Neurons");
00087         paramTable->setColumnWidth(insertionPoint, 200);
00088         neuronPercentCol = insertionPoint;
00089         tempTotalColumnWidth += 200;
00090 
00091         //Add column to set direct or synaptic firing
00092         insertionPoint = paramTable->numCols();
00093         paramTable->insertColumns(insertionPoint, 1);
00094         paramTableHeader->setLabel(insertionPoint, "Direct / Synaptic Firing");
00095         paramTable->setColumnWidth(insertionPoint, 200);
00096         firingModeCol = insertionPoint;
00097         tempTotalColumnWidth += 200;
00098 
00099         //Add table to layout
00100         verticalBox->addWidget(paramTable);
00101         
00102         //Listen for changes in the check boxes
00103         connect (paramTable, SIGNAL(valueChanged(int, int)), this, SLOT(paramValueChanged(int, int)));
00104 
00105         //Resize dialog to maximise the number of visible parameters
00106         if((tempTotalColumnWidth + 10) < 1200)
00107                 this->resize(tempTotalColumnWidth + 10, 300);
00108         else
00109                 this->resize(1200, 300);
00110 
00111         //Set up buttons
00112         QHBoxLayout *buttonBox = new QHBoxLayout();
00113         QPushButton *okButton = new QPushButton("Ok", this);
00114         buttonBox->addWidget(okButton);
00115         connect (okButton, SIGNAL(clicked()), this, SLOT(okButtonPressed()));
00116 
00117         applyButton = new QPushButton("Apply", this);
00118         buttonBox->addWidget(applyButton);
00119         connect (applyButton, SIGNAL(clicked()), this, SLOT(applyButtonPressed()));
00120 
00121         QPushButton *defaultsButton = new QPushButton("Load Defaults", this);
00122         buttonBox->addWidget(defaultsButton);
00123         connect (defaultsButton, SIGNAL(clicked()), this, SLOT(defaultsButtonPressed()));
00124 
00125         QPushButton *cancelButton = new QPushButton("Cancel", this);    
00126         buttonBox->addWidget(cancelButton);
00127         connect (cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonPressed()));
00128 
00129         verticalBox->addLayout(buttonBox);
00130 }
00131 
00132 
00133 /*! Destructor. */
00134 NoiseParametersDialog::~NoiseParametersDialog(){
00135         #ifdef MEMORY_DEBUG
00136                 cout<<"DESTROYING NOISE PARAMETERS DIALOG"<<endl;
00137         #endif//MEMORY_DEBUG
00138 }
00139 
00140 
00141 //--------------------------------------------------------------------------------
00142 //----------------------      PUBLIC METHODS       -------------------------------
00143 //--------------------------------------------------------------------------------
00144 
00145 /*! Loads up the noise parameters into the table. */
00146 bool NoiseParametersDialog::loadParameters(){
00147         //Clear the table
00148         paramTable->setNumRows(0);
00149 
00150         //Parameters are fresh from the database so reset change monitor
00151         paramValuesChanged = false;
00152 
00153         //Disable apply button - no changes to apply
00154         applyButton->setEnabled(false);
00155 
00156         //Work through each of the neuron groups and fill up the table
00157         try{
00158                 Query query = networkDBInterface->getQuery();
00159                 query.reset();
00160                 query<<"SELECT NeuronGrpID, NoiseEnabled, PercentNeurons, DirectFiring, SynapticWeight FROM NoiseParameters";
00161                 Result noiseParamsRes = query.store();
00162         
00163                 //Work through results and load noise parameters
00164                 for(Result::iterator noiseParamsIter = noiseParamsRes.begin(); noiseParamsIter != noiseParamsRes.end(); ++noiseParamsIter){
00165                         Row noiseParamsRow(*noiseParamsIter);
00166         
00167                         //Add new row to the table
00168                         int currentRowNumber = paramTable->numRows();
00169                         paramTable->insertRows(currentRowNumber, 1);
00170         
00171                         //Get neuron group name from database and add it to the table
00172                         query.reset();
00173                         query<<"SELECT Name FROM NeuronGroups WHERE NeuronGrpID = "<<(std::string)noiseParamsRow["NeuronGrpID"];
00174                         Result nameResult = query.store();
00175                         Row nameRow = *nameResult.begin();
00176                         QString neurGrpName((std::string)nameRow["Name"] + " [" + (std::string)noiseParamsRow["NeuronGrpID"] + "]");
00177                         paramTable->setItem(currentRowNumber, neurGrpCol, new QTableItem(paramTable, QTableItem::Never, neurGrpName));
00178                 
00179                         //Add a check box to enable or disable noise in neuron group
00180                         unsigned int noiseEnabledVal = Utilities::getInt((std::string)noiseParamsRow["NoiseEnabled"]);
00181                         QCheckTableItem *tempCheckTableItem = new QCheckTableItem(paramTable, QString(""));
00182                         if(noiseEnabledVal == 0)
00183                                 tempCheckTableItem->setChecked(false);
00184                         else if(noiseEnabledVal == 1)
00185                                 tempCheckTableItem->setChecked(true);
00186                         paramTable->setItem(currentRowNumber, noiseModeCol, tempCheckTableItem);
00187         
00188                         //Add drop down combo box for the percentage of neurons
00189                         double percentNeurons = Utilities::getDouble((std::string)noiseParamsRow["PercentNeurons"]);
00190                         unsigned int percentNeuronsRow = 0;
00191                         QStringList percentNumbersStrList;
00192         
00193                         //Add numbers from 0.01 to 5
00194                         percentNumbersStrList += "0.01";
00195                         if(percentNeurons == 0.01)
00196                                 percentNeuronsRow = 0;
00197                         percentNumbersStrList += "0.05";
00198                         if(percentNeurons == 0.05)
00199                                 percentNeuronsRow = 1;
00200                         percentNumbersStrList += "0.1";
00201                         if(percentNeurons == 0.1)
00202                                 percentNeuronsRow = 2;
00203                         percentNumbersStrList += "0.5";
00204                         if(percentNeurons == 0.5)
00205                                 percentNeuronsRow = 3;
00206                         percentNumbersStrList += "1";
00207                         if(percentNeurons == 1.0)
00208                                 percentNeuronsRow = 4;
00209                         percentNumbersStrList += "5";
00210                         if(percentNeurons == 5.0)
00211                                 percentNeuronsRow = 5;
00212         
00213                         //Add numbers from 10 to 100
00214                         unsigned int percentRowCount = 6;
00215                         for(unsigned int i=10; i<=100; i += 10){
00216                                 if(i == percentNeurons)
00217                                         percentNeuronsRow = percentRowCount;
00218                                 percentNumbersStrList += QString::number(i);
00219                                 ++percentRowCount;
00220                         }
00221 
00222                         //Add random percentage
00223                         percentNumbersStrList += "Random";
00224                         if(percentNeurons == RANDOM_PERCENT_NEURONS_NOISE)//
00225                                 percentNeuronsRow = percentRowCount;
00226 
00227                         QComboTableItem* tempPercentCombo = new QComboTableItem(paramTable, percentNumbersStrList);
00228                         tempPercentCombo->setCurrentItem(percentNeuronsRow);
00229                         paramTable->setItem(currentRowNumber, neuronPercentCol, tempPercentCombo);
00230         
00231                         /* Add drop down combo to select between direct firing of neurons or 
00232                                 firing of synapses with a particular weight */
00233                         unsigned int directFiringVal = Utilities::getInt((std::string)noiseParamsRow["DirectFiring"]);
00234                         double synapticWeight = Utilities::getDouble((std::string)noiseParamsRow["SynapticWeight"]);
00235                         unsigned int firingModeRow = 0;
00236                         QStringList firingModeStrList;
00237                         firingModeStrList += "Direct";
00238                         unsigned int indexCounter = 1;
00239                         for(double i=-1.0; i<=1.0; i += 0.2){
00240                                 if(rint(i*10) == (synapticWeight*10)){//Small errors need to be removed 
00241                                         firingModeRow = indexCounter;
00242                                 }
00243                                 if(rint(i * 10) == 0.0)//QString::number gives a strange very small value for zero
00244                                         firingModeStrList += "Synaptic weight 0";
00245                                 else
00246                                         firingModeStrList += "Synaptic weight " + QString::number(i);
00247                                 ++indexCounter;
00248                         }
00249                         QComboTableItem* tempFiringModeCombo = new QComboTableItem(paramTable, firingModeStrList);
00250                         if(directFiringVal == 1)
00251                                 tempFiringModeCombo->setCurrentItem(0);
00252                         else 
00253                                 tempFiringModeCombo->setCurrentItem(firingModeRow);
00254                         paramTable->setItem(currentRowNumber, firingModeCol, tempFiringModeCombo);
00255                 }
00256         }
00257         catch (const BadQuery& er) {// Handle any query errors
00258                 cerr<<"NoiseParametersDialog: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00259                 QString errorString = "Bad query when loading noise parameters: \"";
00260                 errorString += er.what();
00261                 errorString += "\"";
00262                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00263                 return false;
00264         }
00265         catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00266                 cerr<<"NoiseParametersDialog: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00267                 QString errorString = "Exception thrown loading noise parameters: \"";
00268                 errorString += er.what();
00269                 errorString += "\"";
00270                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00271                 return false;
00272         }
00273         catch(std::exception& er){// Catch-all for any other exceptions
00274                 cerr<<"NoiseParametersDialog: STD EXCEPTION \""<<er.what()<<"\""<<endl;
00275                 QString errorString = "Exception thrown loading noise parameters: \"";
00276                 errorString += er.what();
00277                 errorString += "\"";
00278                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00279                 return false;
00280         }
00281 
00282         //Everything ok up to this point.
00283         return true;
00284 }
00285 
00286 
00287 //-------------------------------------------------------------------------------
00288 //------------------------        SLOTS           -------------------------------
00289 //-------------------------------------------------------------------------------
00290 
00291 /*! Stores the parameters and instructs tasks to reload them
00292         without closing the dialog. */
00293 void NoiseParametersDialog::applyButtonPressed(){
00294         //Store parameters in database
00295         storeParameters();
00296 
00297         //Instruct simulation manager to send message instructing tasks to load parameters
00298         simulationManager->setNoiseParameters();
00299 
00300         //Now changes have been applied, reset button and change monitor
00301         paramValuesChanged = false;
00302         applyButton->setEnabled(false);
00303 }
00304 
00305 
00306 /*! Hides the dialog without storing the parameters. */
00307 void NoiseParametersDialog::cancelButtonPressed(){
00308         this->hide();
00309 }
00310 
00311 
00312 /*! Loads default noise parameters. */
00313 //FIXME NOT IMPLEMENTED YET!.
00314 void NoiseParametersDialog::defaultsButtonPressed(){
00315 }
00316 
00317 
00318 /*! Stores the parameters, instructs tasks to reload them and hides the dialog. */
00319 void NoiseParametersDialog::okButtonPressed(){
00320         if(paramValuesChanged){
00321                 //Store parameters in database
00322                 storeParameters();
00323         
00324                 //Instruct simulation manager to send message instructing tasks to load parameters
00325                 simulationManager->setNoiseParameters();
00326         }
00327         this->hide();
00328 }
00329 
00330 
00331 /*! Called when a check box or comobo box is altered. */
00332 void NoiseParametersDialog::paramValueChanged(int, int){
00333         //Enable apply button and set change flag
00334         paramValuesChanged = true;
00335         applyButton->setEnabled(true);
00336 }
00337 
00338 
00339 //----------------------------------------------------------------------------------
00340 //-----------------------       PRIVATE METHODS     --------------------------------
00341 //----------------------------------------------------------------------------------
00342 
00343 /*! Stores the noise parameters in the database. */
00344 void NoiseParametersDialog::storeParameters(){
00345         try{
00346                 Query query = networkDBInterface->getQuery();
00347         
00348                 /* Declare variables to hold the extracted values
00349                         Everything is an int because this is how MySQL treats bool*/
00350                 unsigned int noiseEnabled, neuronGrpID, directFiring;
00351                 double synapticWeight, percentNeurons;
00352         
00353                 //Work through parameter table
00354                 for(int rowNum = 0; rowNum < paramTable->numRows(); ++rowNum){
00355                         neuronGrpID = Utilities::getNeuronGrpID(paramTable->item(rowNum, neurGrpCol)->text());
00356         
00357                         //Extract whether noise mode is enabled
00358                         QCheckTableItem * item = (QCheckTableItem*)paramTable->item(rowNum, noiseModeCol);
00359                         if(item->isChecked())
00360                                 noiseEnabled = 1;
00361                         else
00362                                 noiseEnabled = 0;
00363         
00364                         //Extract the percentage of neurons
00365                         if(paramTable->item(rowNum, neuronPercentCol)->text() == "Random")//Select percentage of neurons at random
00366                                 percentNeurons = RANDOM_PERCENT_NEURONS_NOISE;
00367                         else
00368                                 percentNeurons = Utilities::getDouble(paramTable->item(rowNum, neuronPercentCol)->text().ascii());
00369         
00370                         //Extract whether it is direct or synaptic firing of neurons
00371                         QString directFiringStr = paramTable->item(rowNum, firingModeCol)->text();
00372                         if(directFiringStr == "Direct"){
00373                                 directFiring = 1;
00374                                 synapticWeight = 0;
00375                         }
00376                         else{//Need to extract the synaptic weight
00377                                 directFiring = 0;
00378                                 QString synWeightStr = directFiringStr.section(" ", 2, 2);
00379                                 synapticWeight = Utilities::getDouble(synWeightStr.ascii());
00380                         }
00381                         
00382                         //Write to database
00383                         Query query = networkDBInterface->getQuery();
00384                         query.reset();
00385                         query<<"UPDATE NoiseParameters SET NoiseEnabled = "<<noiseEnabled<<", PercentNeurons = "<<percentNeurons<<", DirectFiring = "<<directFiring<<", SynapticWeight = "<<synapticWeight<<" WHERE NeuronGrpID = "<<neuronGrpID;
00386                         query.execute();
00387                 }
00388         }
00389         catch (const BadQuery& er) {// Handle any query errors
00390                 cerr<<"NoiseParametersDialog: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00391                 QString errorString = "Bad query when storing noise parameters: \"";
00392                 errorString += er.what();
00393                 errorString += "\"";
00394                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00395         }
00396         catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00397                 cerr<<"NoiseParametersDialog: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00398                 QString errorString = "Exception thrown storing noise parameters: \"";
00399                 errorString += er.what();
00400                 errorString += "\"";
00401                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00402         }
00403         catch(std::exception& er){// Catch-all for any other exceptions
00404                 cerr<<"NoiseParametersDialog: STD EXCEPTION \""<<er.what()<<"\""<<endl;
00405                 QString errorString = "Exception thrown storing noise parameters: \"";
00406                 errorString += er.what();
00407                 errorString += "\"";
00408                 QMessageBox::critical( 0, "Noise Parameters Error", errorString);
00409         }
00410 }
00411 
00412 

Generated on Mon Sep 3 22:29:04 2007 for SpikeStream Application by  doxygen 1.4.4