AnimatLab  2
Test
CsAdapter.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "CsSynapseGroup.h"
9 #include "CsNeuronGroup.h"
10 #include "CsNeuralModule.h"
11 #include "CsSpikeGeneratorGroup.h"
12 #include "CsAdapter.h"
13 
14 namespace AnimatCarlSim
15 {
16 
24 {
25  m_lpSpikeGen = NULL;
26  m_bStimWholePopulation = true;
28 }
29 
37 {
38 
39 try
40 {
41  m_lpSpikeGen = NULL;
42 }
43 catch(...)
44 {Std_TraceMsg(0, "Caught Error in desctructor of CsAdapter\r\n", "", -1, false, true);}
45 }
46 
47 void CsAdapter::Coverage(std::string strType)
48 {
49  std::string strVal = Std_CheckString(strType);
50  if(strVal == "INDIVIDUALS")
51  m_bStimWholePopulation = false;
52  else if(strVal == "WHOLEPOPULATION")
53  m_bStimWholePopulation = true;
54  else
55  THROW_PARAM_ERROR(Cs_Err_lInvalidFiringRateCoverage, Cs_Err_strInvalidFiringRateCoverage, "Coverage", strType);
56 }
57 
58 bool CsAdapter::StimWholePopulation() {return m_bStimWholePopulation;}
59 
60 void CsAdapter::StimWholePopulation(bool bVal) {m_bStimWholePopulation = bVal;}
61 
62 void CsAdapter::CellsToStim(std::string strXml)
63 {
64  CStdXml oXml;
65  oXml.Deserialize(strXml);
66  oXml.FindElement("Root");
67  oXml.FindChildElement("Cells");
68 
69  LoadCellsToStim(oXml);
70 }
71 
73 {
74  Adapter::Initialize();
75 
77  if(!m_lpSpikeGen)
78  THROW_PARAM_ERROR(Cs_Err_lNotSpikeGeneratorType, Cs_Err_strNotSpikeGeneratorType, "ID: ", m_strTargetID);
79 
80  //if(m_lpSpikeGen && m_lpSpikeGen->GetCsModule())
81  // this->StepInterval(m_lpSpikeGen->GetCsModule()->SimulationStepInterval());
82 }
83 
84 bool CsAdapter::RateChanged(float fltRate)
85 {
86  if(fabs(fltRate-m_fltPrevAppliedRate) > 0.001)
87  return true;
88  else
89  return false;
90 }
91 
92 void CsAdapter::ApplyExternalNodeInput(int iTargetDataType, float fltRate)
93 {
94  if(m_lpSpikeGen && fltRate >= 0 && RateChanged(fltRate))
95  {
96  if(m_bStimWholePopulation)
97  {
98  for(int iIdx=0; iIdx<m_lpSpikeGen->NeuronCount(); iIdx++)
99  {
100  //First remove the previous active rate from this group
101  m_lpSpikeGen->SpikeRates()->rates[iIdx] -= m_fltPrevAppliedRate;
102  //Then add the next active rate to this group
103  m_lpSpikeGen->SpikeRates()->rates[iIdx] += fltRate;
104  }
105  }
106  else
107  {
108  for( CStdMap<int,int>::iterator ii=m_aryCellsToStim.begin(); ii!=m_aryCellsToStim.end(); ++ii)
109  {
110  int iIdx = (*ii).first;
111  if(iIdx >= 0 && iIdx < m_lpSpikeGen->NeuronCount())
112  {
113  //First remove the previous active rate from this group
114  m_lpSpikeGen->SpikeRates()->rates[iIdx] -= m_fltPrevAppliedRate;
115  //Then add the next active rate to this group
116  m_lpSpikeGen->SpikeRates()->rates[iIdx] += fltRate;
117  }
118  }
119  }
120 
121  m_fltPrevAppliedRate = fltRate;
122 
123  m_lpSpikeGen->SetSpikeRatesUpdated();
124  }
125 }
126 
128 {
129  Adapter::ResetSimulation();
131 }
132 
133 bool CsAdapter::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
134 {
135  std::string strType = Std_CheckString(strDataType);
136 
137  if(Adapter::SetData(strDataType, strValue, false))
138  return true;
139 
140  if(strType == "COVERAGE")
141  {
142  Coverage(strValue);
143  return true;
144  }
145 
146  if(strType == "CELLSTOSTIM")
147  {
148  CellsToStim(strValue);
149  return true;
150  }
151 
152  //If it was not one of those above then we have a problem.
153  if(bThrowError)
154  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
155 
156  return false;
157 }
158 
159 void CsAdapter::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
160 {
161  Adapter::QueryProperties(aryProperties);
162 
163  aryProperties.Add(new TypeProperty("Coverage", AnimatPropertyType::String, AnimatPropertyDirection::Set));
164  aryProperties.Add(new TypeProperty("CellsToStim", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
165 }
166 
167 void CsAdapter::LoadCellsToStim(CStdXml &oXml)
168 {
169  oXml.IntoElem();
170  m_aryCellsToStim.RemoveAll();
171  int iCount = oXml.NumberOfChildren();
172  for(int iIdx=0; iIdx<iCount; iIdx++)
173  {
174  oXml.FindChildByIndex(iIdx);
175  int iNeuronIdx = oXml.GetChildInt();
176 
177  if(iNeuronIdx < 0)
178  THROW_PARAM_ERROR(Cs_Err_lInvalidNeuralIndex, Cs_Err_strInvalidNeuralIndex, "Neuron Index", iNeuronIdx);
179 
180  if(!m_aryCellsToStim.count(iNeuronIdx))
181  m_aryCellsToStim.Add(iNeuronIdx, 1);
182  }
183  oXml.OutOfElem();
184 }
185 
186 void CsAdapter::Load(CStdXml &oXml)
187 {
188  Adapter::Load(oXml);
189 
190  oXml.IntoElem(); //Into Simulus Element
191 
192  Coverage(oXml.GetChildString("Coverage", "WholePopulation"));
193 
194  if(oXml.FindChildElement("Cells", false))
195  LoadCellsToStim(oXml);
196 
197  oXml.OutOfElem(); //OutOf Simulus Element
198 }
199 
200 } //AnimatCarlSim
201 
202 
203 
204 
Contains the classes for a firing rate neural model.
Definition: CsAdapter.cpp:14
CsSpikeGeneratorGroup * m_lpSpikeGen
The spike generator this adapter is stimulating.
Definition: CsAdapter.h:24
Declares the CsNeuronGroup class.
virtual void Initialize()
Initializes this object.
Definition: CsAdapter.cpp:72
std::string m_strTargetID
GUID ID of the target node.
Definition: Adapter.h:61
Node * m_lpTargetNode
Pointer to the target node.
Definition: Adapter.h:72
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: CsAdapter.cpp:127
CStdMap< int, int > m_aryCellsToStim
An array of neuron indices for individual neurons we want to stimulate.
Definition: CsAdapter.h:29
virtual ~CsAdapter()
Destructor.
Definition: CsAdapter.cpp:36
Declares the current stimulus class.
Declares the synapse class.
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.
float m_fltPrevAppliedRate
Last applied rate.
Definition: CsAdapter.h:32
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: CsAdapter.cpp:133
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
CsAdapter()
Default constructor.
Definition: CsAdapter.cpp:23