%% License Info
%A Model of Feedforward, Global, and Lateral Inhibition in the Locust Visual System.

%This model examines the architecture and function of inhibitory mechanisms in the 
%visual system of locusts, namely those involved in the processing of inputs to a
%key looming-sensitive neuron, the lobula giant movement detector (LGMD).

%Copyright (c) 2026, Erik Olson, Travis Wiens, and Jack Gray

%CITATION:
%When using the model code for scientific publications, cite the following work:
%Olson EGN, Wiens TK, Gray JR. A model of feedforward, global, and lateral inhibition 
%in the locust visual system predicts responses to looming stimuli. Biol Cybern. 2021 
%Jun;115(3):245-265. doi: 10.1007/s00422-021-00876-8. Epub 2021 May 16. PMID: 33997912.

%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.

%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%GNU General Public License for more details.

%You should have received a copy of the GNU General Public License
%along with this program.  If not, see <https://www.gnu.org/licenses/>.

%Contact: erik.olson@usask.cas/>.

%% Code
%Demonstration of the feedforward inhibitory neuron model, showing firing
%rate of the FFI neurons in the dorsal uncrossed bundle for grating
%stimuli which appear instantaneously, then initiate grating movement after
%ten seconds.  Multiple inhibition schemes such as the removal of global 
%inhibition, lateral inhibition, and lateral inhibitory delay are also 
%tested.  This file generates and saves the necessary data for Figure 11.

%Access the parameters for the model
parFile = 'FFIModelParams_16_02_2021.mat';

%Set any potential modifications to inhibition to be ignored for now (by
%setting them to NaN).  Otherwise, the first value would override the
%existing lateral inhibitory gain value, the second would override the
%global inhibitory gain value, and the third would override the lateral
%inhibitory delay time constant.
inhibition_mod = [NaN,NaN,NaN];

%Create a parallel pool using all possible cores (delete this if the
%Parallel Computing Toolbox is not present)
delete(gcp('nocreate'));
parpool(feature('numcores'));

%1st run: normal settings
[f_mean,f_all,t_f] = FFIGratingTrial(parFile,20,inhibition_mod);
save('FFI_grating_Data.mat','f_mean','f_all','t_f');

%2nd run: no global inhibition
inhibition_mod = [NaN,0,NaN]; %set global inhibition to zero
[f_mean,f_all,t_f] = FFIGratingTrial(parFile,20,inhibition_mod);
save('FFI_grating_no_global_Data.mat','f_mean','f_all','t_f');

%3rd run: no lateral inhibition
inhibition_mod = [0,NaN,NaN]; %set lateral inhibition to zero
[f_mean,f_all,t_f] = FFIGratingTrial(parFile,20,inhibition_mod);
save('FFI_grating_no_lateral_Data.mat','f_mean','f_all','t_f');
 
%4th run: no lateral inhibition delay
inhibition_mod = [NaN,NaN,0.001]; %set lateral inhibition to delay to 1 ms (effectively zero)
[f_mean,f_all,t_f] = FFIGratingTrial(parFile,20,inhibition_mod);
save('FFI_grating_no_delay_excess_gain_Data.mat','f_mean','f_all','t_f');

%5th run: no lateral inhibition delay and reduced gain to compensate
load(parFile,'K_lat_m'); %load the lateral inhibitory gain
inhibition_mod = [0.4*K_lat_m,NaN,0.001]; %set lateral inhibition to delay to 1 ms (effectively zero) and reduce gain
[f_mean,f_all,t_f] = FFIGratingTrial(parFile,20,inhibition_mod);
save('FFI_grating_no_delay_Data.mat','f_mean','f_all','t_f');