% Derrick Roos
%
% make_G.m  - Builds G (conductance) matrix (2nx2n). The Gij entry is the conductance from cell j to
% cell i. Takes in Weight matrix W (nxn). Positive connections j to i produce a
% positive G j to i. Negative connection j to i creates a weight to cell
% i's inhibitory companion cell i+n, thus creating a negative weight from j
% to i+n. 
%
% G = make_G(W,g_max,g_inhib,g_EI)
%  where:  W = Bayesian Weight Matrix
%        g_max = average conductance excitatory connections
%        g_inhib = conductance for inhibitory connections
%           g_EI = conductance for excitatory to inhibitory connections

%  output:  G = Conductance Matrix, see description above for detail

function G = make_G(W,g_max,g_inhib,g_EI)

n = length(W);
%scale E to E connections to have average strength to give 3-4 mv EPSP
scale_EE = g_max/mean(W(find(W>0)));

% scale E to I to have average strength to give 3-4 mv EPSP
scale_EI = g_EI/mean(W(find(W<0)));
thres = max(W(:))/50;

G = [zeros(n,n) eye(n)*g_inhib; zeros(n,2*n)];

for i = 1:n
    for j = 1:n
        if W(i,j) > thres
            % connect to excitatory cell
            G(i,j) = W(i,j)*scale_EE;
        elseif W(i,j)<-thres
            % connect to inhibitory companion
            G(n+i,j) = W(i,j)*scale_EI;
        end
    end
end

