AnimatLab  2
Test
FiringRateModule.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 #include "ClassFactory.h"
13 
14 namespace FiringRateSim
15 {
23 {
25  m_bActiveArray = false;
26 }
27 
35 {
36 
37 try
38 {
39  m_aryNeurons.RemoveAll();
40 }
41 catch(...)
42 {Std_TraceMsg(0, "Caught Error in desctructor of FiringRateModule\r\n", "", -1, false, true);}
43 }
44 
45 std::string FiringRateModule::ModuleName() {return Nl_NeuralModuleName();};
46 
59 {return m_bActiveArray;}
60 
73 {
74  m_bActiveArray = bVal;
75 }
76 
89 {return !m_bActiveArray;}
90 
103 {
104  m_bActiveArray = !bVal;
105 }
106 
107 
108 void FiringRateModule::Kill(bool bState)
109 {
110  int iCount = m_aryNeurons.GetSize();
111  for(int iIndex=0; iIndex<iCount; iIndex++)
112  if(m_aryNeurons[iIndex])
113  m_aryNeurons[iIndex]->Kill(bState);
114 }
115 
127 int FiringRateModule::FindNeuronListPos(std::string strID, bool bThrowError)
128 {
129  std::string sID = Std_ToUpper(Std_Trim(strID));
130 
131  int iCount = m_aryNeurons.GetSize();
132  for(int iIndex=0; iIndex<iCount; iIndex++)
133  if(m_aryNeurons[iIndex]->ID() == sID)
134  return iIndex;
135 
136  if(bThrowError)
137  THROW_TEXT_ERROR(Nl_Err_lNeuronNotFound, Nl_Err_strNeuronNotFound, "ID");
138 
139  return -1;
140 }
141 
143 {
144  NeuralModule::ResetSimulation();
145 
146  int iCount = m_aryNeurons.GetSize();
147  for(int iIndex=0; iIndex<iCount; iIndex++)
148  if(m_aryNeurons[iIndex])
149  m_aryNeurons[iIndex]->ResetSimulation();
150 }
151 
153 {
154  NeuralModule::Initialize();
155 
156  Organism *lpOrganism = dynamic_cast<Organism *>(m_lpStructure);
157  if(!lpOrganism)
158  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Organism");
159 
160  int iCount = m_aryNeurons.GetSize();
161  for(int iIndex=0; iIndex<iCount; iIndex++)
162  if(m_aryNeurons[iIndex])
163  m_aryNeurons[iIndex]->Initialize();
164 }
165 
167 {
168  NeuralModule::StepSimulation();
169 
170  int iCount = m_aryNeurons.GetSize();
171  for(int iIndex=0; iIndex<iCount; iIndex++)
172  if(m_aryNeurons[iIndex])
173  m_aryNeurons[iIndex]->StepSimulation();
174 
175  //Swap the active array.
177 }
178 
179 #pragma region DataAccesMethods
180 
181 bool FiringRateModule::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
182 {
183  std::string strType = Std_CheckString(strDataType);
184 
185  if(NeuralModule::SetData(strDataType, strValue, false))
186  return true;
187 
188  if(strType == "TIMESTEP")
189  {
190  TimeStep(atof(strValue.c_str()));
191  return true;
192  }
193 
194  //If it was not one of those above then we have a problem.
195  if(bThrowError)
196  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
197 
198  return false;
199 }
200 
201 void FiringRateModule::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
202 {
203  NeuralModule::QueryProperties(aryProperties);
204 
205  aryProperties.Add(new TypeProperty("TimeStep", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
206 }
207 
216 void FiringRateModule::AddNeuron(std::string strXml, bool bDoNotInit)
217 {
218  CStdXml oXml;
219  oXml.Deserialize(strXml);
220  oXml.FindElement("Root");
221  oXml.FindChildElement("Neuron");
222 
223  Neuron *lpNeuron = LoadNeuron(oXml);
224  if(!bDoNotInit)
225  lpNeuron->Initialize();
226 }
227 
237 void FiringRateModule::RemoveNeuron(std::string strID, bool bThrowError)
238 {
239  int iPos = FindNeuronListPos(strID, bThrowError);
240  m_aryNeurons.RemoveAt(iPos);
241 }
242 
243 bool FiringRateModule::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
244 {
245  std::string strType = Std_CheckString(strItemType);
246 
247  if(strType == "NEURON")
248  {
249  AddNeuron(strXml, bDoNotInit);
250  return true;
251  }
252  //Synapses are stored in the destination neuron. They will be added there.
253 
254 
255  //If it was not one of those above then we have a problem.
256  if(bThrowError)
257  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
258 
259  return false;
260 }
261 
262 bool FiringRateModule::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
263 {
264  std::string strType = Std_CheckString(strItemType);
265 
266  if(strType == "NEURON")
267  {
268  RemoveNeuron(strID, bThrowError);
269  return true;
270  }
271  //Synapses are stored in the destination neuron. They will be removed there.
272 
273 
274  //If it was not one of those above then we have a problem.
275  if(bThrowError)
276  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
277 
278  return false;
279 }
280 
281 #pragma endregion
282 
284 {
285  long lByteSize = 0;
286  int iCount = m_aryNeurons.GetSize();
287  for(int iIndex=0; iIndex<iCount; iIndex++)
288  if(m_aryNeurons[iIndex])
289  lByteSize += m_aryNeurons[iIndex]->CalculateSnapshotByteSize();
290 
291  return lByteSize;
292 }
293 
294 void FiringRateModule::SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
295 {
296  int iCount = m_aryNeurons.GetSize();
297  for(int iIndex=0; iIndex<iCount; iIndex++)
298  if(m_aryNeurons[iIndex])
299  m_aryNeurons[iIndex]->SaveKeyFrameSnapshot(aryBytes, lIndex);
300 }
301 
302 void FiringRateModule::LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
303 {
304  int iCount = m_aryNeurons.GetSize();
305  for(int iIndex=0; iIndex<iCount; iIndex++)
306  if(m_aryNeurons[iIndex])
307  m_aryNeurons[iIndex]->LoadKeyFrameSnapshot(aryBytes, lIndex);
308 }
309 
310 
311 void FiringRateModule::Load(CStdXml &oXml)
312 {
314 
315  CStdXml oNetXml;
316 
317  //if(Std_IsBlank(m_strProjectPath))
318  // THROW_ERROR(Al_Err_lProjectPathBlank, Al_Err_strProjectPathBlank);
319 
320  m_arySourceAdapters.RemoveAll();
321  m_aryTargetAdapters.RemoveAll();
322 
323  oXml.IntoElem(); //Into NeuralModule Element
324 
325  LoadNetworkXml(oXml);
326 
327  oXml.OutOfElem(); //OutOf NeuralModule Element
328 
329  TRACE_DEBUG("Finished loading nervous system config file.");
330 }
331 
341 {
342  short iNeuron, iTotalNeurons;
343 
344  m_aryNeurons.RemoveAll();
345 
346  ID(oXml.GetChildString("ID", m_strID));
347  Type(oXml.GetChildString("Type", m_strType));
348  Name(oXml.GetChildString("Name", m_strName));
349 
350  //We do NOT call the TimeStep mutator here because we need to call it only after all modules are loaded so we can calculate the min time step correctly.
351  m_fltTimeStep = oXml.GetChildFloat("TimeStep", m_fltTimeStep);
352 
353  //This will add this object to the object list of the simulation.
354  m_lpSim->AddToObjectList(this);
355 
356  //*** Begin Loading Neurons. *****
357  oXml.IntoChildElement("Neurons");
358 
359  iTotalNeurons = oXml.NumberOfChildren();
360  for(iNeuron=0; iNeuron<iTotalNeurons; iNeuron++)
361  {
362  oXml.FindChildByIndex(iNeuron);
363  LoadNeuron(oXml);
364  }
365 
366  LoadExternalSynapses(oXml);
367 
368  oXml.OutOfElem();
369  //*** End Loading Neurons. *****
370 }
371 
383 {
384  Neuron *lpNeuron=NULL;
385  std::string strType;
386 
387 try
388 {
389  //Now lets get the index and type of this neuron
390  oXml.IntoElem(); //Into Neuron Element
391  strType = oXml.GetChildString("Type");
392  oXml.OutOfElem(); //OutOf Neuron Element
393 
394  lpNeuron = dynamic_cast<Neuron *>(m_lpSim->CreateObject(Nl_NeuralModuleName(), "Neuron", strType));
395  if(!lpNeuron)
396  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Neuron");
397 
398  lpNeuron->SetSystemPointers(m_lpSim, m_lpStructure, this, NULL, true);
399  lpNeuron->Load(oXml);
400 
401  m_aryNeurons.Add(lpNeuron);
402  return lpNeuron;
403 }
404 catch(CStdErrorInfo oError)
405 {
406  if(lpNeuron) delete lpNeuron;
407  RELAY_ERROR(oError);
408  return NULL;
409 }
410 catch(...)
411 {
412  if(lpNeuron) delete lpNeuron;
413  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
414  return NULL;
415 }
416 }
417 
418 } //FiringRateSim
419 
CStdArray< Adapter * > m_arySourceAdapters
An array of source adapters for this module.
Definition: NeuralModule.h:56
bool InactiveArray()
Gets the inactive array.
virtual void Initialize()
Initializes this object.
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
virtual float TimeStep()
Gets the time step for this moudle in time units.
IStdClassFactory * m_lpClassFactory
The pointer to the class factory for this module.
Definition: NeuralModule.h:53
virtual std::string Type()
returns the string type name of this object.
Definition: AnimatBase.cpp:221
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
void LoadNetworkXml(CStdXml &oXml)
Loads the network configuration.
virtual void StepSimulation()
Step the simulation for this object.
virtual ~FiringRateModule()
Destructor.
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 void RemoveNeuron(std::string strID, bool bThrowError=true)
Removes the neuron with the specified ID.
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
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
CStdArray< Adapter * > m_aryTargetAdapters
An array of target adapters for this module.
Definition: NeuralModule.h:59
virtual std::string Name()
Gets the name of this object.
Definition: AnimatBase.cpp:195
virtual void ResetSimulation()
Resets the simulation back to time 0.
Firing Rate Neuron model.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
virtual void LoadExternalSynapses(CStdXml &oXml)
Loads external synapses.
CStdPtrArray< Neuron > m_aryNeurons
The array of neurons in this module.
Firing rate neural module class factory.
virtual void AddNeuron(std::string strXml, bool bDoNotInit=false)
Adds a neuron to the module.
Declares the synapse class.
FiringRateModule()
Default constructor.
Neuron * LoadNeuron(CStdXml &oXml)
Loads a neuron.
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.
virtual void LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
virtual void Kill(bool bState=true)
Kills.
Declares the firing rate module class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual void Initialize()
Initializes this object.
bool ActiveArray()
Gets the active array.
Contains the classes for a firing rate neural model.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
float m_fltTimeStep
The DT time step for this neural module in seconds.
Definition: NeuralModule.h:40
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
virtual std::string ModuleName()
Gets the module name.
std::string m_strType
The type for this object. Examples are Box, Plane, Neuron, etc..
Definition: AnimatBase.h:58
virtual void AddToObjectList(AnimatBase *lpItem)
Adds an object to the list of all simulation objects.
Definition: Simulator.cpp:4068
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual int FindNeuronListPos(std::string strID, bool bThrowError=true)
Searches for the neuron with the specified ID and returns its position in the list.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.