⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spikeinput.cpp

📁 此代码经过大量使用
💻 CPP
字号:
/***************************************************************************                          spikeinput.cpp  -  description                             -------------------    begin                : Wed Aug 8 2001    copyright            : (C) 2001 by Matt Grover    email                : mgrover@amygdala.org ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/using namespace std;#include <fstream>#include <vector>#include <algorithm>#include <string.h>#include <stdlib.h>#include <math.h>#include <iostream>// includes for XML#include <libxml/parser.h>#include <libxml/parserInternals.h>#include "spikeinput.h"#include "network.h"#include "types.h"extern "C" {    static void __SAXStartElement(SpikeInput *spikeInput, const char *name, const char **attrs){        spikeInput->ParseSpikeInput(name, attrs);    }}SpikeInput::SpikeInput(Network* network):    net(network){    simStepSize = net->TimeStepSize();}SpikeInput::~SpikeInput(){}bool SpikeInput::ReadSpikeList(const char* fileName){    char switchStr[6];    char unitStr[2];    unsigned int eventTime;    unsigned int offsetTime = net->SimTime();    unsigned int nId;    unsigned int c = 1;    // open the file and test for goodness    ifstream fin(fileName);    if ( (!fin) || (fin.bad()) ) {        cerr << "Unable to open " << fileName << endl;        return false;    }    // FIXME: The following section requires extremely rigid formatting    // in the input file if the units switch is used.  Fix this to allow    // for some variation (extra spaces, caps, etc).    if (!fin.eof()) {        // check for units switch        fin >> switchStr;        if (strcmp(switchStr, "units:")) {            fin.seekg(0, ios::beg);            c = 1; // default to units of us (multiplier = 1)        }        else {            fin >> unitStr;            if (!strcmp(unitStr, "us")) {                // us units                c = 1;            }            else if (!strcmp(unitStr, "ms")) {                // ms units                c = 1000;            }            else {                // handle problem                c = 1; // default                cerr << "Invalid units specified in " << fileName << ".\n";                cerr << "Defaulting to input units of us.\n";            }        }    }    while (!fin.eof()) {        fin >> eventTime;        eventTime = eventTime * c;        while ( (fin.peek() != '\n') && (fin.peek() != -1) ) {            fin >> nId;            net->ScheduleNEvent(INPUTSPIKE, eventTime + offsetTime, nId);        }    }    fin.close();    return true;}	bool SpikeInput::ReadSpikeDef(const char* fileName){    static xmlSAXHandler spikeDefHandler = {       0, /* internalSubset */       0, /* isStandalone */       0, /* hasInternalSubset */       0, /* hasExternalSubset */       0, /* resolveEntity */       0, /* getEntity */       0, /* entityDecl */       0, /* notationDecl */       0, /* attributeDecl */       0, /* elementDecl */       0, /* unparsedEntityDecl */       0, /* setDocumentLocator */       0, /* startDocument */       0, /* endDocument */       (startElementSAXFunc)__SAXStartElement, /* startElement */       0, /* endElement */       0, /* reference */       0, /* characters */       0, /* ignorableWhitespace */       0, /* processingInstruction */       0, /* comment */       0, /* warning */       0, /* error */       0, /* fatalError */       0, 0, 0    };    saxErrors = 0;    xmlParserCtxtPtr ctxt;    ctxt = xmlCreateFileParserCtxt(fileName);    if (ctxt == NULL) throw string("can't create a parser context:\n      ") + fileName;    ctxt->sax = &spikeDefHandler;    ctxt->userData = this;    xmlParseDocument(ctxt);    ctxt->sax = NULL;    xmlFreeParserCtxt(ctxt);    ScheduleQueuedSpikes();    if(saxErrors > 0) return false;    return true;}void SpikeInput::ScheduleQueuedSpikes(){    unsigned int i;//    sort(inputSpike.begin(), inputSpike.end());    for (i=0; i<inputSpike.size(); i++) {        net->ScheduleNEvent(INPUTSPIKE,                            inputSpike[i].spikeTime,                            inputSpike[i].neuronId);    }    inputSpike.clear();}void SpikeInput::RoundTime(AmTimeInt& time){    float tmpTime;    float roundTime;    tmpTime = float(time) / simStepSize;    tmpTime = modff(tmpTime, &roundTime);    if (tmpTime > 0.5) {        time = (int(roundTime) * simStepSize) + simStepSize;    }    else {        time = int(roundTime) * simStepSize;    }}void SpikeInput::ParseSpikeInput(const char *name, const char **attrs){    AmIdInt nId = 0;    AmTimeInt startTime;    AmTimeInt duration;    float freq;    InSpike inSpike;    if(string(name) != "neuron") return;    for (unsigned int i = 0; attrs[i] != NULL; i++) {         string aname = attrs[i++];         const char* adata = attrs[i];         if(aname == "neuronId"){             nId = atoi(adata);         } else if(aname == "startTime"){             startTime = atoi(adata) * 1000;         } else if(aname == "duration"){             duration = atoi(adata) * 1000;         } else if(aname == "frequency"){             freq = atof(adata);         }    }    AmTimeInt step = int( 1000.0/freq ) * 1000;    AmTimeInt time = startTime;    // add the first spike    inSpike.neuronId = nId;    while (time < (duration + startTime)) {        RoundTime(time);        inSpike.spikeTime = time;        inputSpike.push_back(inSpike);        time += step;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -