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

📄 scheduledspikeinput.cpp

📁 amygdata的神经网络算法源代码
💻 CPP
字号:
/***************************************************************************                          scheduledspikeinput.cpp  -  description                             -------------------    copyright            : (C) 2004 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.                                   * *                                                                         * ***************************************************************************/#include <string>#include <fstream>#include <iostream>#include "scheduledspikeinput.h"#include "inputneuron.h"#include "utilities.h"#include "logging.h"// includes for XML#include <libxml/parser.h>#include <libxml/parserInternals.h>using namespace std;using namespace Amygdala;extern "C" {    static void __SAXStartElement(ScheduledSpikeInput *spikeInput, const char *name, const char **attrs){        spikeInput->ParseSpikeInput(name, attrs);    }}ScheduledSpikeInput::ScheduledSpikeInput(): SpikeInput(){}ScheduledSpikeInput::ScheduledSpikeInput(const string& name): SpikeInput(name){}ScheduledSpikeInput::~ScheduledSpikeInput(){}void ScheduledSpikeInput::AddNeuron(InputNeuron* nrn){	nrnMap[nrn->GetId()] = nrn;	NeuronGroup::AddNeuron(nrn); }void ScheduledSpikeInput::ScheduleSpike(AmIdInt nrnId, AmTimeInt spikeTime){	// FIXME: Put in error checking code to make sure a valid neuron	// exists. 	InputNeuron* inNrn = nrnMap[nrnId];	ScheduleSpike(inNrn, spikeTime);}void ScheduledSpikeInput::ScheduleSpike(InputNeuron* nrn, AmTimeInt spikeTime){	LOGGER(6, "Scheduling spike from neuron " << nrn->GetId() << " at " << spikeTime)	InSpike inSpike;	inSpike.inNeuron = nrn;	inSpike.spikeTime = spikeTime;	inputSpike.push_back(inSpike);}void ScheduledSpikeInput::ReadInputBuffer(){	LOGGER(6, "Reading input buffer")	ScheduleQueuedSpikes();}bool ScheduledSpikeInput::ReadSpikeList(const char* fileName){    char switchStr[6];    char unitStr[2];    unsigned int eventTime;    unsigned int offsetTime = Network::GetNetworkRef()->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;            InputNeuron* nrn = nrnMap[nId];            if (!nrn) {	            throw new string("Neuron not found in nrnMap");            }            Network::GetNetworkRef()->ScheduleSpike(eventTime + offsetTime, nrn);        }    }    fin.close();    return true;}void ScheduledSpikeInput::ParseSpikeInput(const char *name, const char **attrs){    AmIdInt nId = 0;    AmTimeInt startTime=0;    AmTimeInt duration=0;    float freq=0.;    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.inNeuron = nrnMap[nId];    while (time < (duration + startTime)) {        Utilities::RoundTime(time, simStepSize);        inSpike.spikeTime = time;        inputSpike.push_back(inSpike);        time += step;    }}bool ScheduledSpikeInput::ReadSpikeDef(const char* fileName){    xmlSAXHandler spikeDefHandler;    memset (&spikeDefHandler, 0, sizeof(spikeDefHandler));    spikeDefHandler.startElement = (startElementSAXFunc)__SAXStartElement;    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;}

⌨️ 快捷键说明

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