###################################################################################
############        SETTINGS FILE for timeneuro_code software           ############
############  READ CAREFULLY: lines tarting with '#' are code comments  ############
####################################################################################
######                Single neuron burst detection settings	              ######
####################################################################################

####################################################################################
# Settings for burst detection algorithm (NeuroExplorer's MaxInterval method).
####################################################################################
max_t_start = 0.2         #0.1
max_t_end   = 0.4         #0.2
min_dur_burst = 0.002
min_dur_interbursts = 0.6 #0.2
min_nr_spikes_per_burst = 2
IBI_threshold = 2
####################################################################################
# List neurons to exclude. Put an empty list to exclude none.
####################################################################################
neurons_excluded = ["","","","","","","","",""] #[]
################################################################################
# In the following the features by which to analyze bursts data from single neuron
# burst detection method.
# In stat_features specify the statistical index by which analyze the 
# corresponding variable in burst_features.
# It's mandatory to have feature 'SN1' at least in descriptive_burst_features list!
# NB SR = mean/sem of all spikes nr of neurons in each cluster 
####################################################################################
burst_features = ['Fano_all_cut']#['ACF_PC1','ACF_PC2']#['Fano_all_cut','CV_all_SQ']#['CV_all_SQ']#['halflife_ACF']#['halflife_ACF','SN1']#['ratio_ACF']#['SN1','halflife_ACF']#['ratio_ACF']#['ratio_ACF','halflife_ACF']#['SN1','IBSR']
desc_burst_features = ['BD','SN1','SR','IBSR','ACF_PC1','Fano_all_cut','ACF_PC2','IBI','CV_all_SQ','halflife_ACF','Fano_all']
store_PCAdata = False #True
store_allburstsdata = False #True

####################################################################################
# Some settings about the usage of PCA, enabled by fixing pca_on=True, in order to
# perform dimensionality reduction, preceeded or not by burst features data
# normalization, before clustering the neuron units.
####################################################################################
pca_on = True     #False. Fixing the choice to apply PCA analysis by default
                  #in module burst_clusterizer.clusterize
auto_scale_Normalization = True # True #False
blank_col_nr = 6
pca_min_sum_of_variances = 95.0 #in percentage (%)
                                #this values is needed to find the
                                #minimum number of Principal Comp.
                                #expressing together a percentage of 
                                #the total data variance >= PCA_sum_of_variances_threshold
                                #Used in in module burst_clusterizer.clusterize

####################################################################################
# Parameter 'ACF_bin' is the time bin length used to calculate the Auto-
# Correlation Function (ACF).
# Parameter 'ACF_delay' is the delay at which calculate the AC values.
# Parameter no_AC0 shows if to give in output thw AC value at zero delay. 
# Parameter ACF_shape_normalize can be set to:
# - 'all_ACs': ACF value at every delay is normalized by the sum of all ACF values,
#              e.g. ACF[0-th bin] => ACF[bin 0-th] / sum(ACF[n-th] at every bin n);
# - 'AC0': normalizing each ACF values by dividing them to ACF value at zero delay;
# - 'duration': normalizing all ACFs by the time duration of neuron's spike train;
#               e.g. ACF[0-th bin] => ACF[bin 0-th] / exp. step duration (seconds);
# - False: no normalization is performed.
####################################################################################
# now is fixed to 1/100 of ACF delay value; ACF_bin     = 0.002		#in seconds
ACF_delay   = 0.2	  	#0.02 #in seconds
ACF_shape_normalize = 'duration' #False # 'duration' #'all_ACs' #'all_ACs' #'False' 
ACF_smoothing = True
ACF_pdf     = 'expon'
ACF_no_AC0  = True
CCF_on      = False		#True
CCF_bin     = 0.001		#in seconds
CCF_delay   = 0.02		#in seconds

####################################################################################
# The bin width, in seconds, applied to calculate the ISI distribution and then 
# its related PCAs
####################################################################################
ISI_bin     = 0.002         #in seconds
ISI_delay   = 2             #in seconds
ISI_shape_normalize = True
ISI_upper_threshold = None  #in seconds
                            #it enables the selection of ISI <= ISI_upper_threshold
                            #set = 0 or None in order to not apply this rule
ISI_lower_threshold = None	#in seconds
                            #it enables the selection of ISI >= ISI_lower_threshold
                            #set = 0 or None in order to not apply this rule

####################################################################################
# The bin width, in seconds, applied to calculate the IBI distribution and then 
# its related PCAs
####################################################################################
IBI_bin     = 0.1           #in seconds
IBI_delay   = 5             #in seconds
IBI_shape_normalize = True

####################################################################################
# The bin width, in seconds, applied to calculate Fano factor on binned data
# of timestamps of a neuron in a burst
####################################################################################
Fano_factor_bin     = 7 #5  #in seconds
FF_lower_threshold  = None  #nr.of spikes during a Fano_factor_bin time interval
                            #it enables the selection of bins with a
                            #spikes nr. >= FF_lower_threshold
                            #set = 0 or None in order to not apply this rule
FF_upper_threshold  = None  #nr.of spikes during a Fano_factor_bin time interval
                            #it enables the selection of bins with a
                            #spikes nr. <= FF_upper_threshold
                            #set = 0 or None in order to not apply this rule

####################################################################################
# The bin widths for histogram data to plot
####################################################################################
save_histogram_data_features = True
features_to_histo_bin_dict = {'BD':     0.01,	#in seconds
                              'SN':	    1,
                              'SN1':    1,
                              'IBSR':   5,
                              'IBI':    0.1 }	#in seconds

####################################################################################
# Clustering and outliers detection settings            
# - 'replicates_nr' is number of iterations of repeated clustering performings
#   in order to find optimal results from the algorithm.
# - Mahalanobis_threshold: setting fixing the threshold for the outliers detection
#   based on deviation of the Chi-square distributed Mahalanobis distance. 
#   Samples having a Mahalanobis distance from the centroid, or mean{mahalanobis
#   distances}, more than +/-Mahalanobis_threshold*mean{mahalanobis distances}
#   are moved to Outliers.
# - trashSmallClusters: an integer defining to trash that clusters with at most
#   'trashSmallClusters' samples.
####################################################################################
replicates_nr = 100
Mahalanobis_threshold = 1.4 #1.5
trashSmallClusters = 4 #0

####################################################################################
# Some settings ob Burst fetures data randomization
####################################################################################
burst_randomization = False 	#False. This parameter enables bursts data randomization
randomize_window_length = 4     #in seconds
                                #this parameter fixes the time windom within which
                                #consecutive bursts data are randomized (or shuffled)
                                #for one neuron versus other ones. Subsequent and 
                                #non-overlapping windows are so scanned in order 
                                #to find on it the bursts which will be then randomized

####################################################################################
######              	    Network burst detection settings    	      ######
####################################################################################

####################################################################################
# At first setting if to perform Network Burst Detection, after the MaxInterval one.
# Then fixing parameters of Network Bursts detection, from the paper:
#   Ham MI, Bettencourt LMA, McDaniel FD, Gross GW. 
#   "Spontaneous coordinated activity in cultured networks: analysis of multiple
#    ignition sites, primary circuits,and burst phase delay distributions". 
#   J.Comput.Neuroscience, 2008, 24: 346-57.
# Detection is explained in the following excerpt from the paper:
# "The algorithm seeks the first bin [the bin here is 'Netburst_binlength'] with a 
#  number of spikes at least equal to the lower threshold ['net_low_threshold'].
#  Once found, two possible scenarios were examined:
#  (1) the bin contains a number of spikes greater than or equal
#  to the upper threshold ['net_high_threshold']. This indicates the start of a
#  global burst.The burst continues as long as consecutive bins contain a number
#  of spikes that satisfy the lower threshold. 
#  (2) The upper threshold is not reached. The bin is marked as the Potential
#  Beginning of Burst (P-BoB). The algorithm examines consecutive ...s bins
#  searching for one that satisfies the upper threshold, or one that falls below
#  the lower threshold. If a consecutive bin reaches or exceeds the upper threshold 
#  therefore becoming a network burst, it is identified as starting at the P-BoB.
#  However, if a consecutive bin falls below the lower threshold without reaching
#  the upper threshold no global burst is logged. All network bursts end when a
#  consecutive bin falls below the lower threshold. However, network bursts with
#  activity gaps ['min_interburst_length'] of less than ...s were combined."
####################################################################################
Netburst_detection = True
Netburst_binlength = 0.01 #in seconds
Netlow_threshold = 2     #spikes nr during 'Netburst_binlength' 
Nethigh_threshold = 3      #spikes nr during 'Netburst_binlength'
min_interburst_length = 1 #in seconds

####################################################################################
# Net_features contains those features used to classify Network Burst (NBs) 
# in different states. Such features are chosen from the following:
# - NetworkBurst Duration ('BD'),
# - Inter-NetworkBurst Interval ('IBI'), or the temporal difference between the
#   start of a NB and the end of the previous one,
# - Inter-NetworkBurst Interval 2 ('IBI2'), or the temporal difference between the
#   end of a NB and the start of the next one,
# - the first three PCA of NetworkBurst Dealys data ('Delx', where x=1,2 or 3),
# - the number of distinct neurons spiking during a NB ('NN'),
# - the number of spikes fired during a NB ('SN').
# 
# Parameter divide_per_bin exploits the 'bin' parameter below, not the Netburst_binlength above!
# Parameter trash_poor_states is the least nr of NBs requested in a state (set to 0 to take all)  
#
# In Net_features_bin we can fix the bin's widths of various histogram data, as for
# 'BD', 'NN', 'SN', 'IBI' (together to their max term in histogram), the 'bin' used
# for histograms of delays data (whose max term is fixed by 'delay') and parameters
# used to find Network states through PCA+k-means procedure, as 'states_nr'
# 'remove_outliers' and 'Mahalanobis_threshold'.
#
# Inside file "netstate_data.csv", the following data are reported:
# - step name, tot.number of NBs, mean and sem of IBI,
#   - NBs nr in state i-th/tot.nr.above, Outliers state i-th/NBs nr state i-th,
#     - mean and sem of nr of neurons firing in NBs at state i-th,
#       - mean and sem of nr of exc.neurons firing in NBs at state i-th,
#         %mean and sem of nr of exc.neurons firing in NBs at state i-th,
#       - mean and sem of nr of inh.neurons firing in NBs at state i-th,
#         %mean and sem of nr of inh.neurons firing in NBs at state i-th,
#     - mean and sem of nr of spikes fired in NBs at state i-th,
#       - mean and sem of nr of spikes fired by exc in NBs at state i-th,
#         %mean and sem of nr of spikes fired by exc in NBs at state i-th,
#       - mean and sem of nr of spikes fired by inh in NBs at state i-th,
#         %mean and sem of nr of spikes fired by inh in NBs at state i-th.
#
# In the directory "nb_clusters" there are files "netstates_expstep.csv"
# and any such file contains the following data, disposed in columns, and
# repeated for each network state:
# - time bins, based on 'delay' and 'bin' in Net_features_bin below;
# - mean histogram of delays, divided by the Net_features_bin['bin'] below,
#   plus a column with its sem
# - mean histogram of delays from exc neurons, divided by the the 'bin' below,
#   plus a column with its sem
# - mean histogram of delays from inh neurons, divided by the the 'bin' below,
#   plus a column with its sem
# In the directory "nb_clusters" there are files "NBstarts_expstep.csv"
# and any such file contains the following data, disposed in columns, and
# repeated for each network state:
# - timestamps of NB's starts of the present state
# 
# In the directory "netstate_features" there are files "expstep_HIST_netstate_features.csv" 
# and any such file contains the following data, disposed in columns and for features
# 'BD', 'IBI', 'NN', 'SN' listed in Net_features_bin: 
# - time bins, based on 'delay' and 'bin' in Net_features_bin below;
# - histogram of data from the feature, plus a column of its pdf and cumulative pdf
# - the same data but distinguished by every network state found
# NB No sem in these last histograms because we have only a list of data features for
#    the entire step, so no 'ensemble data'-mean/sem to calculate.
# 
# NB Delay are given by all differences (spikes timestamp-first spike timestamp)
# NB file expstep_HIST_Delays.csv: the second column is the mean SR per network burst,
#    and the fourth is the previous one normalized by the neurons nr in that cluster.
#
# When studying maps of spikes paths, we consider only spikes before the delay_netpath
#
# If normalize_maps=True:  maps data of Exc and Inh.neurons are normalized together.
#                  =False: maps data are normalized separately.
#####################################################################################
Net_features = ['Del1']#['BD']#['NN','BD']#['Del1','BD'] #['BD'] #['BD','SN','IBI','NN'] #, end-mod
divide_per_bin = False #True
trash_poor_states = 1 #in %, end-mod
delay_netpath = 0.02 #sec

normalize_maps = True #False
Net_features_bin = {'BD':	0.05,    #in seconds	
		    'BD_max':   30.0,	#in seconds	
    		    'NN':	1,      #adimensional
		    'NN_max':   100,    #adimensional	
		    'SN':    	1,	#adimensional
    		    'SN_max':   400,    #adimensional  
		    'IBI':      2,	#in seconds
		    'IBI_max':  200.0,	#in seconds
		    'IBI2':     2,	#in seconds
		    'IBI2_max': 200,	#in seconds
		    'binstates':0.010,  #in seconds for SN histograms, end-mod   		
		    'bin': 	0.010,  #in seconds for NN and EX histograms, end-mod	
		    'binFF':	0.100,	#in seconds for FF histograms		    
		    'delay': 	10.0,	#in seconds, end-mod
		    'states_nr': 6,	#nr of network-states 
		    'remove_outliers': False,
		    'Mahalanobis_threshold': 1.8,
		    'binCounts': 1, 	#adimensional integer nr
                    'maxCounts': 500,	#adimensional integer nr
		    'classesCounts_nr': 2,	#nr of classes(clusters) of neurons
		    'rm_outliers_classesCounts': False,
		    'Mahalanobis_thresh_classesCounts': 1.5,
		    'classesDelays_nr': 2,	#nr of classes(clusters) of neurons
		    'rm_outliers_classesDelays': False,
		    'Mahalanobis_thresh_classesDelays': 1.5,
 		    'do_Ttest': False,   #False to skip T-test
		    'pvalue': 0.05,	#, end-mod
		    'nr_pvalue': 20}    #, end-mod
#store_files NetStates15 with 'bin'=0.025
store_NeuronsCounts = True #False
store_NetStatesNNcum = True #False
store_NBstarts = True  #False #True
store_probStates = False #True
store_HIST_Delays = False #True
store_delaysNeurClasses = False #True
store_netstates = True #True
store_countshistoNeurClasses = True #True
store_netpaths = True #True
store_FF = True #False

###################################################################################
######			    Convolution operator setting                      ######
####################################################################################

####################################################################################
# Parameters for the convolution of pdf with binned data are:
# - 'pdf': the kind of probability density function (pdf) for the convolving 
#          Kernel or filter, which comes from those available in scipy.stas package.
# - 'pdf_samples_nr ': an integer number multiple giving how many bins of the filter
#                      samples to use in order to convolve the samples of the
#                      original signal. This setting works as a sort of delay
#                      during which to filter, or smooth or average in time, 
#                      the original signal. 
# - 'pdf_parameters': parameters related to the shape of the particular pdf used 
#                     for the filter. 
#                     In general leave these settings unchanged.
# - 'pdf_sample_nr_start': the             
# Precisely between the various pdf's available  
# (see: http://docs.scipy.org/doc/scipy/reference/stats.html),
# we shall apply the following:
# - for a GAUSSIAN:
#   - pdf = 'norm';
#   - function code line: norm(loc=mean, scale=variance), and 'pdf_parameters' are:
#                         - loc is the mean;
#                         - scale is the variance.
# - for an EXPONENTIAL:
#   - pdf = 'expon';
#   - function code line: expon(loc=first point, scale=1.0/lambda),
#                         where expon(scale=1.0/lambda)=lambda*e**(-lambda*x) and
#                         mean=1/lambda, and as 'pdf_parameters':
#                         - loc is the first to point where the pdf starts; 
#                         - scale is equal to the mean and 
#                           proportional to the variance=1/lambda**2. 
# - for a GAMMA:
#   - pdf = 'gamma';
#   - function code line: gamma(a, loc=first point, scale->1/theta),
#                         where gamma.pdf(x,a,theta) = 
#                         (x**(a-1))*exp(-x/theta)/(gamma(a)*(theta**a))
#                         for x>=0, a>0, theta>0, and mean=a*theta, 
#                         variance=a*theta**2 as 'pdf_parameters'.
####################################################################################
pdf = 'expon'
pdf_samples_nr = 3
pdf_parameters = dict(loc=0.0, scale=0.5)   #in seconds

####################################################################################
######		            Secondary settings - Do not modify                    ######
####################################################################################
####################################################################################
# already_normalized_features: the set of features already normalized
# features_to_histo: the set of features whose data it is required to build and 
#                    store an histogram, based on bin and optional range parameters
#                    above in features_to_histo_bin_dict.
# verbose_screen: set True if you want to read processing times (actually excluded clustering) 
####################################################################################
Release = True
already_normalized_features = set(['ACF_PC1', 'ACF_PC2', 'ACF_PC3', \
			           'ISI_PC1', 'ISI_PC2', 'ISI_PC3', \
			           'IBI_PC1', 'IBI_PC2', 'IBI_PC3'])
features_to_histo = set(['BD', 'SN', 'SN1', 'IBSR', 'IBI'])
verbose_screen = True
store_results = True

