richardtomsett / vertexsimulator

Virtual Electrode Recording Tool for EXtracellular potentials: a Matlab simulator for spatially realistic LFP simulation in spiking neural networks
Other
10 stars 3 forks source link

Fix step input current #3

Closed mrahtz closed 8 years ago

mrahtz commented 8 years ago

Hello!

I have problems when trying to use the step current input. Modifying tutorial 2, for example, with

NeuronParams(1).Input(1).inputType = 'i_step';
NeuronParams(1).Input(1).timeOn = 1;
NeuronParams(1).Input(1).timeOff = 100;
NeuronParams(1).Input(1).amplitude = 200;

the simulation gives

...
100%  line source constants calculated ...
Model successfully initialised!
Error using +
Matrix dimensions must agree.

Error in NeuronModel_adex/updateNeurons (line 28)
      kv = bsxfun(@rdivide, (-bsxfun(@times, N.g_l, (NM.v - N.E_leak)) -...

Error in groupUpdateSchedule (line 28)
  updateNeurons(NeuronModel{iGroup}, InModel(iGroup, :), ...

Error in simulate (line 54)
    [NeuronModel, SynModel, InModel] = ...

Error in runSimulation (line 128)
    simulate(TP, NP, SS, RS, NeuronIDMap, NeuronModelArr, ...

Error in tutorial_2 (line 274)
runSimulation(params, connections, electrodes);

The issue seems to be that the code in NeuronModel_adex is expecting an N x M matrix for input current, where N is the number of neurons and M is the number of compartments per neuron, but InputModel_i_step only produces a 1 x M matrix:

    function IM = setupStepCurrent(IM, N, inputID, timeStep)                    
      mi = N.Input(inputID).amplitude(:);                                       
      IM.meanInput = bsxfun(@times, mi, IM.membraneAreaRatio);
...
    function IM = updateInput(IM, ~)                                            
      if IM.count == IM.stepOn                                                  
        IM.I_input = IM.meanInput;

This doesn't happen in InputModel_i_ou because a bsxfun is performed on meanInput, causing singleton expansion:

function IM = updateInput(IM, ~)                                            
      IM.I_input = IM.I_input + ...                                             
        bsxfun(@plus, ...                                                       
        bsxfun( @times, IM.optInputPars.expMinusLambdaDelta_t, ...              
        bsxfun(@minus, IM.optInputPars.meanInput, IM.I_input) ), ...

This branch fixes the issue by just doing a repmat on meanInput inside InputModel_i_step.

Thanks! :) Matthew

richardtomsett commented 8 years ago

Hi Matthew, Thanks very much for spotting this and the suggested fix! It would work well but for some other mistakes I noticed on revisiting the step current class... basically I hadn't dealt with the subset input parameter, which is used when splitting neuron groups across parallel processes. So inspired by your pull request I have updated the step current class and that should fix both the issue you raised and the potential problems with parallel simulations I hadn't spotted before.