SpikeStream Application Library  0.2
AbstractSpectrogramData.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "Globals.h"
00003 #include "AbstractSpectrogramData.h"
00004 #include "SpikeStreamAnalysisException.h"
00005 using namespace spikestream;
00006 
00007 //Qt includes
00008 #include <QDebug>
00009 
00010 //Other includes
00011 #include <iostream>
00012 using namespace std;
00013 
00014 
00016 AbstractSpectrogramData::AbstractSpectrogramData(const AnalysisInfo& analysisInfo) : QwtRasterData(){
00017         this->analysisInfo = analysisInfo;
00018         allTimeSteps = true;
00019         showNeuronIDs = true;
00020 }
00021 
00022 
00024 AbstractSpectrogramData::~AbstractSpectrogramData(){
00025 
00026 }
00027 
00028 
00029 /*----------------------------------------------------------*/
00030 /*------               PUBLIC METHODS                 ------*/
00031 /*----------------------------------------------------------*/
00032 
00034 QwtRasterData* AbstractSpectrogramData::copy() const {
00035         return (QwtRasterData*)this;
00036 }
00037 
00038 
00040 QwtDoubleInterval AbstractSpectrogramData::range() const {
00041         return QwtDoubleInterval(0.0, 1.0);
00042 }
00043 
00044 
00046 void AbstractSpectrogramData::showSelectedTimeStep(unsigned int timeStep){
00047         selectedTimeStep = timeStep;
00048 }
00049 
00050 
00052 void AbstractSpectrogramData::showAllTimeSteps(bool show){
00053         allTimeSteps = show;
00054 }
00055 
00056 
00058 void AbstractSpectrogramData::setShowNeuronIDs(bool show){
00059         showNeuronIDs = show;
00060         if(showNeuronIDs){
00061                 this->setBoundingRect(QwtDoubleRect(minNeuronID, minNeuronID, maxNeuronID-minNeuronID, maxNeuronID-minNeuronID));
00062         }
00063         else
00064                 this->setBoundingRect(QwtDoubleRect(1, 1, maxNeuronID-minNeuronID, maxNeuronID-minNeuronID));
00065 }
00066 
00067 
00069 double AbstractSpectrogramData::value(double x, double y) const {
00070         unsigned int tmpX = (unsigned int)rint(x);
00071         unsigned int tmpY = (unsigned int)rint(y);
00072 
00073         //Resolve data as neuron ids or as numbers from 1-networksize
00074         if(!showNeuronIDs){
00075                 tmpX += minNeuronID - 1;
00076                 tmpY += minNeuronID - 1;
00077         }
00078 
00079         //Get time step
00080         unsigned int timeStep = selectedTimeStep;
00081         if(allTimeSteps)
00082                 timeStep = allTimeStepsKey;
00083 
00084         //Check data exists for this time step
00085         if(!timeStepDataMap.contains(timeStep)){
00086                 qCritical()<<"Time step cannot be found"<<timeStep;
00087                 return 0.0;
00088         }
00089 
00090         if(tmpX > tmpY){
00091                 if(!timeStepDataMap[timeStep].contains(tmpY) || !timeStepDataMap[timeStep][tmpY].contains(tmpX)){
00092                         qCritical()<<"Y and X value cannot be found."<<tmpY<<" "<<tmpX;
00093                         return 0.0;
00094                 }
00095                 return timeStepDataMap[timeStep][tmpY][tmpX];
00096         }
00097 
00098         if(!timeStepDataMap[timeStep].contains(tmpX) || !timeStepDataMap[timeStep][tmpX].contains(tmpY)){
00099                 qCritical()<<"X and Y value cannot be found."<<tmpX<<" "<<tmpY;
00100                 return 0.0;
00101         }
00102         return timeStepDataMap[timeStep][tmpX][tmpY];
00103 }
00104 
00105 
00106 /*----------------------------------------------------------*/
00107 /*------              PROTECTED METHODS               ------*/
00108 /*----------------------------------------------------------*/
00109 
00111 void AbstractSpectrogramData::addTimeStep(unsigned int timeStep, const QList<unsigned int>& neurIDList){
00112         if(timeStepDataMap.contains(timeStep))
00113                 throw SpikeStreamAnalysisException("Time step already exists in time step data map.");
00114 
00115         //Add each combination of neuron IDs only once
00116         for(int i=0; i<neurIDList.size(); ++i){
00117                 for(int j=i; j<neurIDList.size(); ++j){
00118                         //Make sure that map always stores lower number in the first key
00119                         if(neurIDList.at(i) <= neurIDList.at(j))
00120                                 timeStepDataMap[timeStep][neurIDList.at(i)][neurIDList.at(j)] = 0.0;
00121                         else
00122                                 timeStepDataMap[timeStep][neurIDList.at(j)][neurIDList.at(i)] = 0.0;
00123                 }
00124         }
00125 }
00126 
00127 
00129 void AbstractSpectrogramData::addAllTimeStepsMap(const QList<unsigned int>& neurIDList){
00130         //Identify a key that is not currently in use
00131         for(int i=0; i< timeStepDataMap.size() + 10; ++i){
00132                 if(!timeStepDataMap.contains(i)){
00133                         allTimeStepsKey = i;
00134                         addTimeStep(i, neurIDList);
00135                         break;
00136                 }
00137         }
00138 
00139         //Sum the integration between neurons for each time step
00140         for(QHash<unsigned int, QHash<unsigned int, QHash<unsigned int, double> > >::iterator tsIter = timeStepDataMap.begin(); tsIter != timeStepDataMap.end(); ++tsIter)      {
00141                 if(tsIter.key() != allTimeStepsKey){
00142                         for(QHash<unsigned int, QHash<unsigned int, double> >::iterator neur1Iter = tsIter.value().begin(); neur1Iter != tsIter.value().end(); ++neur1Iter){
00143                                 for(QHash<unsigned int, double>::iterator neur2Iter = neur1Iter.value().begin(); neur2Iter != neur1Iter.value().end(); ++neur2Iter){
00144                                         timeStepDataMap[allTimeStepsKey][neur1Iter.key()][neur2Iter.key()] += neur2Iter.value();
00145                                 }
00146                         }
00147                 }
00148         }
00149 
00150         //Divide by the number of time steps
00151         double numTimeSteps = timeStepDataMap.size() - 1;
00152         double max = 0.0;
00153         for(QHash<unsigned int, QHash<unsigned int, double> >::iterator neur1Iter = timeStepDataMap[allTimeStepsKey].begin(); neur1Iter != timeStepDataMap[allTimeStepsKey].end(); ++neur1Iter){
00154                 for(QHash<unsigned int, double>::iterator neur2Iter = neur1Iter.value().begin(); neur2Iter != neur1Iter.value().end(); ++neur2Iter){
00155                         timeStepDataMap[allTimeStepsKey][neur1Iter.key()][neur2Iter.key()] /= numTimeSteps;
00156 
00157                         //Find maximum
00158                         if(timeStepDataMap[allTimeStepsKey][neur1Iter.key()][neur2Iter.key()] > max)
00159                                 max = timeStepDataMap[allTimeStepsKey][neur1Iter.key()][neur2Iter.key()];
00160                 }
00161         }
00162 
00163         //Normalize
00164         for(QHash<unsigned int, QHash<unsigned int, double> >::iterator neur1Iter = timeStepDataMap[allTimeStepsKey].begin(); neur1Iter != timeStepDataMap[allTimeStepsKey].end(); ++neur1Iter){
00165                 for(QHash<unsigned int, double>::iterator neur2Iter = neur1Iter.value().begin(); neur2Iter != neur1Iter.value().end(); ++neur2Iter){
00166                         timeStepDataMap[allTimeStepsKey][neur1Iter.key()][neur2Iter.key()] /= max;
00167                 }
00168         }
00169 }
00170 
00171 
00173 void AbstractSpectrogramData::printData(){
00174         for(QHash<unsigned int, QHash<unsigned int, QHash<unsigned int, double> > >::iterator tsIter = timeStepDataMap.begin(); tsIter != timeStepDataMap.end(); ++tsIter)      {
00175                 if(tsIter.key() == allTimeStepsKey)
00176                         cout<<"-------------  All Time Steps  -----------------"<<endl;
00177                 else
00178                         cout<<"-------------  Time step: "<<tsIter.key()<<"  -----------------"<<endl;
00179                 for(QHash<unsigned int, QHash<unsigned int, double> >::iterator neur1Iter = tsIter.value().begin(); neur1Iter != tsIter.value().end(); ++neur1Iter){
00180                         for(QHash<unsigned int, double>::iterator neur2Iter = neur1Iter.value().begin(); neur2Iter != neur1Iter.value().end(); ++neur2Iter){
00181                                 cout<<"From "<<neur1Iter.key()<<" to "<<neur2Iter.key()<<" phi="<<neur2Iter.value()<<endl;
00182                         }
00183                 }
00184         }
00185         cout<<endl;
00186 }
00187 
00188 
00190 void AbstractSpectrogramData::reset(){
00191         timeStepDataMap.clear();
00192 }
00193 
00194 
 All Classes Files Functions Variables Typedefs Friends Defines