Skip to content

Commit

Permalink
fixed #536
Browse files Browse the repository at this point in the history
wrote structureOutputs, which can convert matrices
into an organized structure. Also better handling
of mech_sizes and syn_sizes using the same binary
  • Loading branch information
sg-s committed Jul 11, 2020
1 parent 7a2722e commit c9684f0
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 121 deletions.
2 changes: 1 addition & 1 deletion +xolotl/version.m
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
function version()
disp('v20.7.8');
disp('v20.7.11');
68 changes: 9 additions & 59 deletions @xolotl/integrate.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
I_clamp = [];
curr_state = [];
syn_state = [];
cont_state = [];
mech_state = [];
spiketimes = [];

comp_names = self.find('compartment');
Expand Down Expand Up @@ -182,16 +182,16 @@
%arguments = self.get(self.find('*'));

[~,f] = fileparts(self.linked_binary);

f = str2func(f);


% now do the actual integration
[results{1:n_outputs+1}] = f(arguments,self.I_ext',self.V_clamp');

if self.closed_loop
self.deserialize(results{1}(1:length(arguments)));
end

% read out mechanism sizes
mechanism_sizes = results{1}(length(arguments)+1:end);


if n_outputs > 0
Expand All @@ -203,8 +203,8 @@
varargout{2} = Ca;
end
if n_outputs > 2
cont_state = (results{4})';
varargout{3} = cont_state;
mech_state = (results{4})';
varargout{3} = mech_state;
end
if n_outputs > 3
curr_state = (results{5})';
Expand All @@ -223,7 +223,8 @@

% return a data structure

data = struct;

data = self.structureOutputs(V,Ca,mech_state,curr_state,syn_state);

% voltages/spikes
if self.output_type == 2
Expand All @@ -232,57 +233,6 @@
for i = 1:n_comp
data.(comp_names{i}).spiketimes = nonzeros(spiketimes(:,i));
end

end

for i = 1:n_comp
if isnan(sum(self.V_clamp(:,i)))
data.(comp_names{i}).V = V(:,i);
else
data.(comp_names{i}).I_clamp = V(:,i);
data.(comp_names{i}).V_clamp = self.V_clamp(:,i);
end
end


% calcium and E_Ca
for i = 1:n_comp
data.(comp_names{i}).Ca = Ca(:,i);
data.(comp_names{i}).E_Ca = Ca(:,i+n_comp);
end


% all mechanisms
all_mechanisms = self.find('mechanism');
a = 1;
for i = 1:length(mechanism_sizes)
this_mech_size = mechanism_sizes(i);

if this_mech_size == 0
continue
end

z = a + this_mech_size - 1;
data = structlib.write(data, all_mechanisms{i}, cont_state(:,a:z));
a = z + 1;
end


% all currents
a = 1;
for i = 1:n_comp
cond_names = self.(comp_names{i}).find('conductance');
for j = 1:length(cond_names)
data.(comp_names{i}).(cond_names{j}).I = curr_state(:,a);
a = a + 1;
end
end


% all synapses
if ~isempty(syn_state )
data.synapse_state = syn_state;
end

varargout{1} = data;

varargout{1} = data;
110 changes: 110 additions & 0 deletions @xolotl/structureOutputs.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

% _ _ _
% __ _____ | | ___ | |_| |
% \ \/ / _ \| |/ _ \| __| |
% > < (_) | | (_) | |_| |
% /_/\_\___/|_|\___/ \__|_|
%
% ### structureOutput
%
% integrates a `xolotl` model.
%
% **Syntax**
%
% ```matlab
% data = x.structureOutput(V, Ca, mech_state, I, syn_state)
% ```
%
% **Description**
%
% Converts the matrices generated by x.integrate into a structure
% so that it is easier to identify the outputs of a particular component
% This is especially useful in complex models with differently-sized
% mechanisms or synapses.
%
% This method is used internally in x.integrate, when
% `output_type` > 0, but can also be used
% independently
%
%
%
% See Also:
% xolotl.integrate


function data = structureOutputs(self,V,Ca,mech_state,curr_state,syn_state)

assert(nargin == 6,'6 arguments required, which are all outputs from x.integrate')

% first, determine sizes or all mechanisms and synapses
[~,f] = fileparts(self.linked_binary);
f = str2func(f);
arguments = self.serialize;
comp_names = self.find('compartment');
n_comp = length(comp_names);
[mech_sizes, syn_sizes] = f(arguments);


data = struct;



for i = 1:n_comp
if isnan(sum(self.V_clamp(:,i)))
data.(comp_names{i}).V = V(:,i);
else
data.(comp_names{i}).I_clamp = V(:,i);
data.(comp_names{i}).V_clamp = self.V_clamp(:,i);
end
end


% calcium and E_Ca
for i = 1:n_comp
data.(comp_names{i}).Ca = Ca(:,i);
data.(comp_names{i}).E_Ca = Ca(:,i+n_comp);
end


% all mechanisms
all_mechanisms = self.find('mechanism');
a = 1;
for i = 1:length(mech_sizes)
this_mech_size = mech_sizes(i);

if this_mech_size == 0
continue
end

z = a + this_mech_size - 1;
data = structlib.write(data, all_mechanisms{i}, mech_state(:,a:z));
a = z + 1;
end


% all currents
a = 1;
for i = 1:n_comp
cond_names = self.(comp_names{i}).find('conductance');
for j = 1:length(cond_names)
data.(comp_names{i}).(cond_names{j}).I = curr_state(:,a);
a = a + 1;
end
end


% all synapses
all_synapses = self.find('synapse');
a = 1;
for i = 1:length(syn_sizes)
this_syn_size = syn_sizes(i);

if this_syn_size == 0
continue
end

z = a + this_syn_size - 1;
data = structlib.write(data, [all_synapses{i},'.state'], syn_state(:,a:z));
a = z + 1;
end

62 changes: 45 additions & 17 deletions c++/mexTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
double *output_cont_state; // mechanisms
double *spiketimes;

double *mech_sizes_out;
double *syn_sizes_out;


int n_conductances = 0;
int n_mechanisms = 0;
Expand All @@ -45,7 +48,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int res;

int full_current_size = 0;
int full_controller_size = 0;
int full_mechanism_size = 0;
int full_synaptic_size = 0;

int spikes_only = 0;
Expand Down Expand Up @@ -160,15 +163,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
n_comp = (int) (xolotl_network.comp).size(); // these many compartments


// ask each controller (nicely) what their
// ask each mechanism (nicely) what their
// full state size is
int full_controller_sizes[n_comp];
full_controller_size = 0;
int full_mechanism_sizes[n_comp];
full_mechanism_size = 0;
for (int i = 0; i < n_comp; i ++) {
int n_mech = (xolotl_network.comp[i])->n_mech;

full_controller_sizes[i] = xolotl_network.comp[i]->getFullMechanismSize();
full_controller_size += full_controller_sizes[i];
full_mechanism_sizes[i] = xolotl_network.comp[i]->getFullMechanismSize();
full_mechanism_size += full_mechanism_sizes[i];
}


Expand All @@ -194,27 +197,52 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])


// ask all the mechanisms for their sizes
int begin_mechansism_sizes = param_size;
int begin_mechanism_sizes = param_size;
param_size = param_size + n_mechanisms;

if (nrhs == 1) {
// the only thing we are being asked to do is compute the mechanism
// and synapse sizes and return that. once we're done, abort
plhs[0] = mxCreateDoubleMatrix(n_mechanisms, 1, mxREAL);
mech_sizes_out = mxGetPr(plhs[0]);

int idx = 0;
for(int j = 0; j < n_comp; j++) {
for (int k = 0; k < xolotl_network.comp[j]->n_mech; k++) {
int mech_size = (xolotl_network.comp[j]->getMechanismPointer(k))->getFullStateSize();
mech_sizes_out[idx] = mech_size;
idx++;
}
}

plhs[0] = mxCreateDoubleMatrix(param_size, 1, mxREAL);
output_state = mxGetPr(plhs[0]);

int idx = 0;
for(int j = 0; j < n_comp; j++) {
for (int k = 0; k < xolotl_network.comp[j]->n_mech; k++) {
int mech_size = (xolotl_network.comp[j]->getMechanismPointer(k))->getFullStateSize();
output_state[begin_mechansism_sizes+idx] = mech_size;
idx++;
plhs[1] = mxCreateDoubleMatrix(n_synapses, 1, mxREAL);
syn_sizes_out = mxGetPr(plhs[1]);


idx = 0;
for(int j = 0; j < n_comp; j++) {
for (int k = 0; k < xolotl_network.comp[j]->n_syn; k++) {
int syn_size = (xolotl_network.comp[j]->getSynapsePointer(k))->getFullStateSize();
syn_sizes_out[idx] = syn_size;
idx++;
}
}

return;
}


plhs[0] = mxCreateDoubleMatrix(param_size, 1, mxREAL);
output_state = mxGetPr(plhs[0]);




if (v%5 == 0) {
mexPrintf("\n[#COMP] [MECH SIZE] [CURRENT SIZE] [SYN SIZE]\n ");
mexPrintf( "%i ",n_comp);
mexPrintf( "%i ",full_controller_size);
mexPrintf( "%i ",full_mechanism_size);
mexPrintf( "%i ",full_current_size);
mexPrintf( "%i \n",full_synaptic_size);

Expand All @@ -233,7 +261,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
}

if (nlhs > 3) {
plhs[3] = mxCreateDoubleMatrix(full_controller_size, nsteps_out, mxREAL);
plhs[3] = mxCreateDoubleMatrix(full_mechanism_size, nsteps_out, mxREAL);
output_cont_state = mxGetPr(plhs[3]);
}

Expand Down

0 comments on commit c9684f0

Please sign in to comment.