1111
1212
1313def responsive_units (spike_times , spike_clusters , event_times ,
14- pre_time = [0.5 , 0 ], post_time = [0 , 0.5 ], alpha = 0.05 ):
14+ pre_time = [0.5 , 0 ], post_time = [0 , 0.5 ], alpha = 0.05 , use_fr = False ):
1515 """
1616 Determine responsive neurons by doing a Wilcoxon Signed-Rank test between a baseline period
1717 before a certain task event (e.g. stimulus onset) and a period after the task event.
@@ -31,6 +31,8 @@ def responsive_units(spike_times, spike_clusters, event_times,
3131 time (in seconds) to follow the event times
3232 alpha : float
3333 alpha to use for statistical significance
34+ use_fr : bool
35+ whether to use the firing rate instead of total spike count
3436
3537 Returns
3638 -------
@@ -51,18 +53,12 @@ def responsive_units(spike_times, spike_clusters, event_times,
5153 times = np .column_stack (((event_times + post_time [0 ]), (event_times + post_time [1 ])))
5254 spike_counts , cluster_ids = get_spike_counts_in_bins (spike_times , spike_clusters , times )
5355
54- # Do statistics
55- p_values = np .empty (spike_counts .shape [0 ])
56- stats = np .empty (spike_counts .shape [0 ])
57- for i in range (spike_counts .shape [0 ]):
58- if np .sum (baseline_counts [i , :] - spike_counts [i , :]) == 0 :
59- p_values [i ] = 1
60- stats [i ] = 0
61- else :
62- stats [i ], p_values [i ] = wilcoxon (baseline_counts [i , :], spike_counts [i , :])
56+ if use_fr :
57+ baseline_counts = baseline_counts / (pre_time [0 ] - pre_time [1 ])
58+ spike_counts = spike_counts / (post_time [1 ] - post_time [0 ])
6359
64- # Perform FDR correction for multiple testing
65- sig_units , p_values , _ , _ = multipletests ( p_values , alpha , method = 'fdr_bh' )
60+ # Do statistics
61+ sig_units , stats , p_values = compute_comparison_statistics ( baseline_counts , spike_counts , test = 'signrank' , alpha = alpha )
6662 significant_units = cluster_ids [sig_units ]
6763
6864 return significant_units , stats , p_values , cluster_ids
@@ -125,27 +121,66 @@ def differentiate_units(spike_times, spike_clusters, event_times, event_groups,
125121 counts_2 , cluster_ids = get_spike_counts_in_bins (spike_times , spike_clusters , times_2 )
126122
127123 # Do statistics
128- p_values = np .empty (len (cluster_ids ))
129- stats = np .empty (len (cluster_ids ))
130- for i in range (len (cluster_ids )):
131- if (np .sum (counts_1 [i , :]) == 0 ) and (np .sum (counts_2 [i , :]) == 0 ):
132- p_values [i ] = 1
133- stats [i ] = 0
124+ sig_units , stats , p_values = compute_comparison_statistics (counts_1 , counts_2 , test = test , alpha = alpha )
125+ significant_units = cluster_ids [sig_units ]
126+
127+ return significant_units , stats , p_values , cluster_ids
128+
129+
130+ def compute_comparison_statistics (value1 , value2 , test = 'ranksums' , alpha = 0.05 ):
131+ """
132+ Compute statistical test between two arrays
133+
134+ Parameters
135+ ----------
136+ value1 : 1D array
137+ first array of values to compare
138+ value2 : 1D array
139+ second array of values to compare
140+ test : string
141+ which statistical test to use, options are:
142+ 'ranksums' Wilcoxon Rank Sums test
143+ 'signrank' Wilcoxon Signed Rank test (for paired observations)
144+ 'ttest' independent samples t-test
145+ 'paired_ttest' paired t-test
146+ alpha : float
147+ alpha to use for statistical significance
148+
149+ Returns
150+ -------
151+ significant_units : 1D array
152+ an array with the indices of values that are significatly modulated
153+ stats : 1D array
154+ the statistic of the test that was performed
155+ p_values : 1D array
156+ the p-values of all the values
157+ """
158+
159+ p_values = np .empty (len (value1 ))
160+ stats = np .empty (len (value1 ))
161+ for i in range (len (value1 )):
162+ if test == 'signrank' :
163+ if np .sum (value1 [i , :] - value2 [i , :]) == 0 :
164+ p_values [i ] = 1
165+ stats [i ] = 0
166+ else :
167+ stats [i ], p_values [i ] = wilcoxon (value1 [i , :], value2 [i , :])
134168 else :
135- if test == 'ranksums' :
136- stats [i ], p_values [i ] = ranksums (counts_1 [i , :], counts_2 [i , :])
137- elif test == 'signrank' :
138- stats [i ], p_values [i ] = wilcoxon (counts_1 [i , :], counts_2 [i , :])
139- elif test == 'ttest' :
140- stats [i ], p_values [i ] = ttest_ind (counts_1 [i , :], counts_2 [i , :])
141- elif test == 'paired_ttest' :
142- stats [i ], p_values [i ] = ttest_rel (counts_1 [i , :], counts_2 [i , :])
169+ if (np .sum (value1 [i , :]) == 0 ) and (np .sum (value2 [i , :]) == 0 ):
170+ p_values [i ] = 1
171+ stats [i ] = 0
172+ else :
173+ if test == 'ranksums' :
174+ stats [i ], p_values [i ] = ranksums (value1 [i , :], value2 [i , :])
175+ elif test == 'ttest' :
176+ stats [i ], p_values [i ] = ttest_ind (value1 [i , :], value2 [i , :])
177+ elif test == 'paired_ttest' :
178+ stats [i ], p_values [i ] = ttest_rel (value1 [i , :], value2 [i , :])
143179
144180 # Perform FDR correction for multiple testing
145181 sig_units , p_values , _ , _ = multipletests (p_values , alpha , method = 'fdr_bh' )
146- significant_units = cluster_ids [sig_units ]
147182
148- return significant_units , stats , p_values , cluster_ids
183+ return sig_units , stats , p_values
149184
150185
151186def roc_single_event (spike_times , spike_clusters , event_times ,
0 commit comments