SpikeStream Application Library  0.2
NeuronParametersModel.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "Globals.h"
00003 #include "NetworkDisplay.h"
00004 #include "NeuronParametersModel.h"
00005 #include "SpikeStreamException.h"
00006 using namespace spikestream;
00007 
00008 //Qt includes
00009 #include <QDebug>
00010 #include <QIcon>
00011 
00012 
00014 NeuronParametersModel::NeuronParametersModel(unsigned int neuronTypeID){
00015         //Listen for changes in the network
00016         connect(Globals::getEventRouter(), SIGNAL(networkChangedSignal()), this, SLOT(networkChanged()));
00017 
00018         //Store the neuron type - this class can only handle neurons of a single type
00019         this->neuronTypeID = neuronTypeID;
00020 
00021         //Load up the parameters for the current network
00022         loadParameters();
00023 }
00024 
00025 
00027 NeuronParametersModel::~NeuronParametersModel(){
00028 }
00029 
00030 
00031 /*--------------------------------------------------------*/
00032 /*-------             PUBLIC METHODS               -------*/
00033 /*--------------------------------------------------------*/
00034 
00036 int NeuronParametersModel::columnCount(const QModelIndex&) const{
00037         return parameterInfoList.size() + 2;
00038 }
00039 
00040 
00043 QVariant NeuronParametersModel::data(const QModelIndex & index, int role) const{
00044         //Return invalid index if index is invalid or no network loaded
00045         if (!index.isValid())
00046                 return QVariant();
00047         if(!Globals::networkLoaded())
00048                 return QVariant();
00049 
00050         //Check rows and columns are in range
00051         if (index.row() < 0 || index.row() >= rowCount() || index.column() < 0 || index.column() >= columnCount())
00052                 return QVariant();
00053 
00054         //Return appropriate data
00055         if (role == Qt::DisplayRole){
00056                 if(index.column() == 0)
00057                         return neurGrpInfoList[index.row()].getName() + "(" + QString::number(neurGrpInfoList[index.row()].getID()) + ")";
00058                 if(index.column() > 0 && index.column() <= parameterInfoList.size()){
00059                         unsigned int neurGrpID = neurGrpInfoList[index.row()].getID();
00060                         if(parameterMap.contains(neurGrpID) && parameterMap[neurGrpID].contains(parameterInfoList.at(index.column()-1).getName()))
00061                                 return parameterMap[neurGrpID][parameterInfoList.at(index.column()-1).getName()];
00062                         throw SpikeStreamException("Maps missing entry somewhere.");
00063                 }
00064         }
00065 
00066         //Icons
00067         if (role == Qt::DecorationRole){
00068                 //Edit button
00069                 if(index.column() == parameterInfoList.size() + 1 ){
00070                         QIcon tmpIcon(Globals::getSpikeStreamRoot() + "/images/edit.png");
00071                         return tmpIcon;
00072                 }
00073         }
00074 
00075         //Size hint
00076         if(role == Qt::SizeHintRole){
00077                 //Edit button
00078                 if(index.column() == parameterInfoList.size() + 1 ){
00079                         return QSize(40, 40);
00080                 }
00081         }
00082 
00083         //If we have reached this point ignore request
00084         return QVariant();
00085 }
00086 
00087 
00089 NeuronGroupInfo NeuronParametersModel::getNeuronGroupInfo(int row){
00090         if(row >= neurGrpInfoList.size())
00091                 throw SpikeStreamException("Request for neuron group info at row " + QString::number(row) + " is out of range.");
00092         return neurGrpInfoList.at(row);
00093 }
00094 
00095 
00097 QList<ParameterInfo> NeuronParametersModel::getParameterInfoList(){
00098         return parameterInfoList;
00099 }
00100 
00101 
00103 QHash<QString, double> NeuronParametersModel::getParameterValues(int row){
00104         NeuronGroupInfo info = getNeuronGroupInfo(row);
00105         if(!parameterMap.contains(info.getID()))
00106                 throw SpikeStreamException("Mismatch between parameter map and neuron group info list.");
00107 
00108         return parameterMap[info.getID()];
00109 }
00110 
00111 
00113 bool NeuronParametersModel::setData(const QModelIndex& index, const QVariant&, int) {
00114         if (!index.isValid() || !Globals::networkLoaded())
00115                 return false;
00116         if (index.row() < 0 || index.row() >= rowCount())
00117                 return false;
00118 
00119         //If we have reached this point no data has been set
00120         return false;
00121 }
00122 
00123 
00125 QVariant NeuronParametersModel::headerData(int section, Qt::Orientation orientation, int role) const{
00126         if (role != Qt::DisplayRole)
00127                 return QVariant();
00128 
00129         if (orientation == Qt::Horizontal){
00130                 if(section == 0)
00131                         return "Neuron Group";
00132                 if(section == parameterInfoList.size() + 1)//Edit column
00133                         return "";
00134                 if(section > 0 && section <= parameterInfoList.size())//Parameter name column
00135                         return parameterInfoList.at(section - 1).getName();
00136         }
00137 
00138         return QVariant();
00139 }
00140 
00142 void NeuronParametersModel::reload(){
00143         loadParameters();
00144 }
00145 
00146 
00148 int NeuronParametersModel::rowCount(const QModelIndex&) const{
00149         return neurGrpInfoList.size();
00150 }
00151 
00152 
00153 /*--------------------------------------------------------*/
00154 /*-------              PRIVATE SLOTS               -------*/
00155 /*--------------------------------------------------------*/
00156 
00158 void NeuronParametersModel::networkChanged(){
00159         loadParameters();
00160 }
00161 
00162 
00163 /*--------------------------------------------------------*/
00164 /*-------             PRIVATE METHODS              -------*/
00165 /*--------------------------------------------------------*/
00166 
00168 void NeuronParametersModel::checkParameters(){
00169         foreach(NeuronGroupInfo neurGrpInfo, neurGrpInfoList){
00170                 if(!parameterMap.contains(neurGrpInfo.getID()))
00171                         throw SpikeStreamException("Parameter map does not contain a loaded neuron group");
00172 
00173                 //Check that keys in parameter map match keys for this neuron type
00174                 QHash<QString, double> neurGrpParamMap = parameterMap[neurGrpInfo.getID()];
00175                 QList<QString> paramKeys = neurGrpParamMap.keys();
00176                 foreach(QString key, paramKeys){
00177                         bool paramFound = false;
00178                         foreach(ParameterInfo paramInfo, parameterInfoList){
00179                                 if(paramInfo.getName() == key)
00180                                         paramFound = true;
00181                         }
00182                         if(!paramFound){
00183                                 throw SpikeStreamException("Key " + key + " from neuron group not found in parameter info list");
00184                         }
00185                 }
00186         }
00187 }
00188 
00189 
00191 void NeuronParametersModel::loadParameters(){
00192         //Clear lists of information
00193         neurGrpInfoList.clear();
00194         parameterMap.clear();
00195         parameterInfoList.clear();
00196 
00197         //Do nothing if network is not loaded
00198         if(!Globals::networkLoaded()){
00199                 this->reset();
00200                 return;
00201         }
00202 
00203         try{
00204                 //Get list of current neuron group info filtered by the neuron type
00205                 neurGrpInfoList = Globals::getNetwork()->getNeuronGroupsInfo(neuronTypeID);
00206 
00207                 //Check that they are all the same type
00208                 bool firstTime = true;
00209                 foreach(NeuronGroupInfo info, neurGrpInfoList){
00210                         if(firstTime){
00211                                 neuronTypeID = info.getNeuronTypeID();
00212                                 firstTime = false;
00213                         }
00214                         else if(neuronTypeID != info.getNeuronTypeID()){
00215                                 throw SpikeStreamException("NeuronParametersModel only supports neurons of a single type.");
00216                         }
00217                 }
00218 
00219                 //Get list of available parameters
00220                 parameterInfoList = Globals::getNetworkDao()->getNeuronType(neuronTypeID).getParameterInfoList();
00221 
00222                 //Get the parameters for each neuron group
00223                 foreach(NeuronGroupInfo neurGrpInfo, neurGrpInfoList){
00224                         parameterMap[neurGrpInfo.getID()] = Globals::getNetwork()->getNeuronGroup(neurGrpInfo.getID())->getParameters();
00225                 }
00226 
00227                 //Check that it all matches up
00228                 checkParameters();
00229         }
00230         catch(SpikeStreamException& ex){
00231                 qCritical()<<ex.getMessage();
00232         }
00233 
00234         //Instruct listening classes to reload data
00235         this->reset();
00236 }
00237 
00238 
 All Classes Files Functions Variables Typedefs Friends Defines