AnimatLab  2
Test
Synapse.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 
9 #include "Synapse.h"
10 #include "Neuron.h"
11 #include "FiringRateModule.h"
12 
13 namespace FiringRateSim
14 {
15  namespace Synapses
16  {
17 
25 {
26  m_lpFRModule = NULL;
27 
28  m_lpFromNeuron = NULL;
29  m_lpToNeuron = NULL;
30  m_fltWeight=0;
32  m_strType = "REGULAR";
33  m_bHasDelay = false;
34  m_fltDelayInterval = 0;
35 }
36 
44 {
45 
46 try
47 {
48  m_arySynapses.RemoveAll();
49 }
50 catch(...)
51 {Std_TraceMsg(0, "Caught Error in desctructor of Synapse\r\n", "", -1, false, true);}
52 }
53 
63 {return m_fltWeight;}
64 
73 void Synapse::Weight(float fltVal)
74 {m_fltWeight=fltVal;}
75 
84 bool Synapse::HasDelay() {return m_bHasDelay;}
85 
94 void Synapse::HasDelay(bool bVal)
95 {
96  m_bHasDelay = bVal;
98 }
99 
108 float Synapse::DelayInterval() {return m_fltDelayInterval;}
109 
118 void Synapse::DelayInterval(float fltVal)
119 {
120  Std_IsAboveMin((float) 0, fltVal, true, "DelayInterval", true);
121  m_fltDelayInterval = fltVal;
123 }
124 
136 {
138 }
139 
148 {
149  if(m_bHasDelay && m_lpFRModule)
150  {
151  float fltTimeStep = m_lpFRModule->TimeStep();
152 
153  if(fltTimeStep > 0)
154  {
155  int iLength = (int) (m_fltDelayInterval/fltTimeStep);
156  m_aryDelayBuffer.SetSize(iLength);
157  }
158  }
159  else
160  m_aryDelayBuffer.RemoveAll();
161 }
162 
171 void Synapse::Process(float &fltCurrent)
172 {
173  fltCurrent+=CalculateCurrent();
174 }
175 
185 {
187  //int i=5;
188  //if(Std_ToLower(m_strID) == "3ce79124-1a4b-4a34-8399-56dfe1814515")
189  // i=6;
190 
191  float fltI = (this->FromNeuron()->FiringFreq(m_lpFRModule) * this->Weight() * this->CalculateModulation(m_lpFRModule) );
192 
193  if(m_bHasDelay)
194  {
195  float fltNextI = m_aryDelayBuffer.GetHead();
196  //Now set the current value into the buffer.
197  m_aryDelayBuffer.AddEnd(fltI);
198  return fltNextI;
199  }
200  else
201  return fltI;
202 }
203 
215 {return &m_fltWeight;}
216 
226 {return m_fltModulation;}
227 
239 {return &m_fltModulation;}
240 
251 Synapse *Synapse::GetCompoundSynapse(short iCompoundIndex)
252 {
253 
254  if( iCompoundIndex<0 || iCompoundIndex>=m_arySynapses.GetSize() )
255  THROW_ERROR(Std_Err_lInvalidIndex, Std_Err_strInvalidIndex);
256  return m_arySynapses[iCompoundIndex];
257 }
258 
267 void Synapse::AddSynapse(std::string strXml, bool bDoNotInit)
268 {
269  CStdXml oXml;
270  oXml.Deserialize(strXml);
271  oXml.FindElement("Root");
272  oXml.FindChildElement("CompoundSynapse");
273 
274  Synapse *lpSynapse = LoadSynapse(oXml);
275 
276  if(!bDoNotInit)
277  lpSynapse->Initialize();
278 }
279 
289 void Synapse::RemoveSynapse(std::string strID, bool bThrowError)
290 {
291  int iPos = FindSynapseListPos(strID, bThrowError);
292  m_arySynapses.RemoveAt(iPos);
293 }
294 
306 int Synapse::FindSynapseListPos(std::string strID, bool bThrowError)
307 {
308  std::string sID = Std_ToUpper(Std_Trim(strID));
309 
310  int iCount = m_arySynapses.GetSize();
311  for(int iIndex=0; iIndex<iCount; iIndex++)
312  if(m_arySynapses[iIndex]->ID() == sID)
313  return iIndex;
314 
315  if(bThrowError)
316  THROW_TEXT_ERROR(Nl_Err_lSynapseNotFound, Nl_Err_strSynapseNotFound, "ID");
317 
318  return -1;
319 }
320 
332 {
333  m_fltModulation=1;
334  int iCount, iIndex;
335  Synapse *lpSynapse=NULL;
336 
337  iCount = m_arySynapses.GetSize();
338  for(iIndex=0; iIndex<iCount; iIndex++)
339  {
340  lpSynapse = m_arySynapses[iIndex];
341  m_fltModulation*=lpSynapse->CalculateModulation(m_lpFRModule);
342  }
343 
344  return m_fltModulation;
345 }
346 
347 
349 {
350  Link::Initialize();
351 
352  m_lpFromNeuron = dynamic_cast<Neuron *>(m_lpSim->FindByID(m_strFromID));
353  if(!m_lpFromNeuron)
354  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strFromID);
355 
356  int iCount = m_arySynapses.GetSize();
357  for(int iIndex=0; iIndex<iCount; iIndex++)
358  m_arySynapses[iIndex]->Initialize();
359 
361 }
362 
363 void Synapse::SetSystemPointers(Simulator *m_lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
364 {
365  Link::SetSystemPointers(m_lpSim, lpStructure, lpModule, lpNode, false);
366 
367  m_lpFRModule = dynamic_cast<FiringRateModule *>(lpModule);
368 
369  if(bVerify) VerifySystemPointers();
370 }
371 
373 {
374  Link::VerifySystemPointers();
375 
376  if(!m_lpFRModule)
377  THROW_PARAM_ERROR(Al_Err_lUnableToCastNeuralModuleToDesiredType, Al_Err_strUnableToCastNeuralModuleToDesiredType, "ID: ", m_lpFRModule->ID());
378 
379  if(!m_lpOrganism)
380  THROW_PARAM_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Link: ", m_strID);
381 }
382 
383 #pragma region DataAccesMethods
384 
385 float *Synapse::GetDataPointer(const std::string &strDataType)
386 {
387  std::string strType = Std_CheckString(strDataType);
388 
389  if(strType == "WEIGHT")
390  return &m_fltWeight;
391  else if(strType == "MODULATION")
392  return &m_fltModulation;
393 
394  return NULL;
395 }
396 
397 bool Synapse::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
398 {
399  std::string strType = Std_CheckString(strDataType);
400 
401  if(Link::SetData(strDataType, strValue, false))
402  return true;
403 
404  if(strType == "WEIGHT")
405  {
406  Weight(atof(strValue.c_str()));
407  return true;
408  }
409 
410  if(strType == "HASDELAY")
411  {
412  HasDelay(Std_ToBool(strValue));
413  return true;
414  }
415 
416  if(strType == "DELAYINTERVAL")
417  {
418  DelayInterval(atof(strValue.c_str()));
419  return true;
420  }
421 
422  //If it was not one of those above then we have a problem.
423  if(bThrowError)
424  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
425 
426  return false;
427 }
428 
429 void Synapse::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
430 {
431  Link::QueryProperties(aryProperties);
432 
433  aryProperties.Add(new TypeProperty("Modulation", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
434 
435  aryProperties.Add(new TypeProperty("Weight", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
436  aryProperties.Add(new TypeProperty("HasDelay", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
437  aryProperties.Add(new TypeProperty("DelayInterval", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
438 }
439 
440 bool Synapse::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
441 {
442  std::string strType = Std_CheckString(strItemType);
443 
444  if(strType == "SYNAPSE")
445  {
446  AddSynapse(strXml, bDoNotInit);
447  return true;
448  }
449 
450 
451  //If it was not one of those above then we have a problem.
452  if(bThrowError)
453  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
454 
455  return false;
456 }
457 
458 bool Synapse::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
459 {
460  std::string strType = Std_CheckString(strItemType);
461 
462  if(strType == "SYNAPSE")
463  {
464  RemoveSynapse(strID, bThrowError);
465  return true;
466  }
467 
468  //If it was not one of those above then we have a problem.
469  if(bThrowError)
470  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
471 
472  return false;
473 }
474 
475 #pragma endregion
476 
478 {
479  int iSize = m_aryDelayBuffer.GetSize();
480  for(int iIdx=0; iIdx<iSize; iIdx++)
481  m_aryDelayBuffer[iIdx] = 0;
482 
483  int iCount = m_arySynapses.GetSize();
484  for(int iIndex=0; iIndex<iCount; iIndex++)
485  m_arySynapses[iIndex]->ResetSimulation();
486 }
487 
488 void Synapse::Load(CStdXml &oXml)
489 {
490  AnimatBase::Load(oXml);
491 
492  m_lpToNeuron = dynamic_cast<Neuron *>(m_lpNode);
493  if(!m_lpToNeuron)
494  THROW_PARAM_ERROR(Al_Err_lUnableToCastNodeToDesiredType, Al_Err_strUnableToCastNodeToDesiredType, "ID: ", m_lpNode->ID());
495 
496  oXml.IntoElem(); //Into Synapse Element
497 
498  m_strFromID = oXml.GetChildString("FromID");
500  THROW_TEXT_ERROR(Std_Err_lBlankAttrib, Std_Err_strBlankAttrib, "Attribute: FromID");
501 
502  m_bEnabled = oXml.GetChildBool("Enabled", true);
503  m_fltWeight = oXml.GetChildFloat("Weight");
504 
505  m_bHasDelay = oXml.GetChildBool("HasDelay", m_bHasDelay);
506  m_fltDelayInterval = oXml.GetChildFloat("DelayInterval", m_fltDelayInterval);
507 
508  m_arySynapses.RemoveAll();
509 
510  //*** Begin Loading CompoundSynapses. *****
511  if(oXml.FindChildElement("CompoundSynapses", false))
512  {
513  oXml.IntoChildElement("CompoundSynapses");
514 
515  int iCount = oXml.NumberOfChildren();
516  for(int iIndex=0; iIndex<iCount; iIndex++)
517  {
518  oXml.FindChildByIndex(iIndex);
519  LoadSynapse(oXml);
520  }
521 
522  oXml.OutOfElem(); //OutOf CompoundSynapses Element
523  }
524 
525 
526  oXml.OutOfElem(); //OutOf Synapse Element
527 }
528 
540 {
541  Synapse *lpSynapse=NULL;
542  std::string strType;
543 
544 try
545 {
546  oXml.IntoElem(); //Into Synapse Element
547  strType = oXml.GetChildString("Type");
548  oXml.OutOfElem(); //OutOf Synapse Element
549 
550  lpSynapse = dynamic_cast<Synapse *>(m_lpSim->CreateObject(Nl_NeuralModuleName(), "Synapse", strType));
551  if(!lpSynapse)
552  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Synapse");
553 
555  lpSynapse->Load(oXml);
556  m_arySynapses.Add(lpSynapse);
557 
558  return lpSynapse;
559 }
560 catch(CStdErrorInfo oError)
561 {
562  if(lpSynapse) delete lpSynapse;
563  RELAY_ERROR(oError);
564  return NULL;
565 }
566 catch(...)
567 {
568  if(lpSynapse) delete lpSynapse;
569  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
570  return NULL;
571 }
572 }
573 
574  } //Synapses
575 } //FiringRateSim
576 
577 
578 
579 
580 
581 
virtual void AddSynapse(std::string strXml, bool bDoNotInit)
Adds a compound synapse to this one.
Definition: Synapse.cpp:267
Neuron * m_lpToNeuron
The pointer to post-synaptic neuron.
Definition: Synapse.h:51
virtual float CalculateModulation(FiringRateModule *lpModule)
Calculates the synaptic modulation.
Definition: Synapse.cpp:331
virtual void Process(float &fltCurrent)
Processes this synapse.
Definition: Synapse.cpp:171
virtual float CalculateCurrent()
Calculates the synaptic current for this synapse.
Definition: Synapse.cpp:184
Firing rate neural module.
Neuron * m_lpFromNeuron
The pointer to pre-synaptic neuron.
Definition: Synapse.h:48
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
Definition: Synapse.cpp:440
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
Definition: Synapse.cpp:363
virtual float TimeStep()
Gets the time step for this moudle in time units.
float m_fltWeight
The weight of the synapse. This is a current value in nanoamps.
Definition: Synapse.h:39
virtual void Initialize()
Initializes this object.
Definition: Synapse.cpp:348
float * ModulationPointer()
Gets a pointer to the synaptic modulation.
Definition: Synapse.cpp:238
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
CStdCircularArray< float > m_aryDelayBuffer
Definition: Synapse.h:61
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Definition: Synapse.cpp:397
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Synapse.cpp:477
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
std::string Std_Trim(std::string strVal)
Trims a string.
virtual Synapse * GetCompoundSynapse(short iCompoundIndex)
Gets a pointer to a compound synapse.
Definition: Synapse.cpp:251
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: Synapse.cpp:385
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Definition: Synapse.cpp:458
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Firing Rate Neuron model.
virtual float DelayInterval()
Gets the delay buffer size in time.
Definition: Synapse.cpp:108
Node * m_lpNode
The pointer to this items parent Node. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:52
CStdPtrArray< Synapse > m_arySynapses
Array of moduled/gated child synapses.
Definition: Synapse.h:36
virtual int FindSynapseListPos(std::string strID, bool bThrowError=true)
Searches for the first synapse list position.
Definition: Synapse.cpp:306
virtual void TimeStepModified()
If the time step is modified then we need to recalculate the length of the delay buffer.
Definition: Synapse.cpp:135
virtual float FiringFreq(FiringRateModule *lpModule)
Calculates the current firing frequency.
virtual bool HasDelay()
Gets whether this synapse has a delay associated with it.
Definition: Synapse.cpp:84
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Definition: Synapse.cpp:372
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
Declares the synapse class.
bool Std_ToBool(int iVal)
Converts a value toa bool.
virtual void RemoveSynapse(std::string strID, bool bThrowError=true)
Removes the copmound synapse specified by ID.
Definition: Synapse.cpp:289
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
Declares the firing rate module class.
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
float * WeightPointer()
Gets a pointer to the synaptic weight.
Definition: Synapse.cpp:214
Neuron * FromNeuron()
Gets the pre-synaptic neuron.
Definition: Synapse.h:77
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
float Weight()
Gets the synaptic weight.
Definition: Synapse.cpp:62
FiringRateModule * m_lpFRModule
Pointer to parent FiringRateModule.
Definition: Synapse.h:33
Contains the classes for a firing rate neural model.
Firing rate synapse model.
Definition: Synapse.h:29
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
float Modulation()
Gets the synaptic modulation.
Definition: Synapse.cpp:225
float m_fltModulation
The modulation value to use for this synapse.
Definition: Synapse.h:42
Synapse()
Default constructor.
Definition: Synapse.cpp:24
Synapse * LoadSynapse(CStdXml &oXml)
Loads a synapse.
Definition: Synapse.cpp:539
virtual ~Synapse()
Destructor.
Definition: Synapse.cpp:43
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
std::string m_strType
The type for this object. Examples are Box, Plane, Neuron, etc..
Definition: AnimatBase.h:58
virtual void SetDelayBufferSize()
Recalculates the size of the delay buffer required.
Definition: Synapse.cpp:147
std::string m_strFromID
GUID ID of the pre-synaptic neruon.
Definition: Synapse.h:45