|
|
|
|
@@ -60,17 +60,32 @@ def get_firing_rate(block_map, df, contrast, condition, kernel_width=0.0005):
|
|
|
|
|
return time, rates, spikes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def foreign_fish_detection_beat(block_map, df, cs, all_contrasts, all_conditions, kernel_width=0.0005, cell_name="", store_roc=False):
|
|
|
|
|
"""Tries to detect the presence of a foreign fish by estimating the discriminability of the responses during the beat
|
|
|
|
|
versus the responses without another fish beeing there, i.e. the baseline activity.
|
|
|
|
|
|
|
|
|
|
Applies a ROC analysis to the response segments between chirps. Calculates a) the distances between the baseline responses and
|
|
|
|
|
b) distances between the baseline and beat responses. Tests whether distances in b) are larger than a)
|
|
|
|
|
Args:
|
|
|
|
|
block_map ([type]): maps nix blocks to combination of stimulus parameters
|
|
|
|
|
df ([type]): the difference frequency that should be used
|
|
|
|
|
cs ([type]): ths chirpsize that should be used
|
|
|
|
|
all_contrasts ([type]): list of all used contrasts
|
|
|
|
|
all_conditions ([type]): list of all chirp conditions, i.e. self, other, or no-other
|
|
|
|
|
kernel_width (float, optional): std of Gaussian kernel. Defaults to 0.0005.
|
|
|
|
|
cell_name (str, optional): name of the cell. Defaults to "".
|
|
|
|
|
store_roc (bool, optional): if true the full false positives and true positives will be returned leads to huge file sizes!. Defaults to False.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def foreign_fish_detection_beat(block_map, df, all_contrasts, all_conditions, kernel_width=0.0005, cell_name="", store_roc=False):
|
|
|
|
|
Returns:
|
|
|
|
|
list of dictionaries: the results, auc is the area under the curve, i.e. the discrimination performance in the range [0, 1]. The 'detection_task' is 'beat'
|
|
|
|
|
"""
|
|
|
|
|
detection_performances = []
|
|
|
|
|
|
|
|
|
|
for contrast in all_contrasts:
|
|
|
|
|
print(" " * 50, end="\r")
|
|
|
|
|
print("Contrast: %.3f" % contrast, end="\r")
|
|
|
|
|
no_other_block = block_map[(contrast, df, "no-other")]
|
|
|
|
|
self_block = block_map[(contrast, df, "self")]
|
|
|
|
|
no_other_block = block_map[(contrast, df, cs, "no-other")]
|
|
|
|
|
self_block = block_map[(contrast, df, cs, "self")]
|
|
|
|
|
|
|
|
|
|
# get some metadata assuming they are all the same for each condition, which they should
|
|
|
|
|
duration, dt, _, chirp_duration, chirp_times = get_chirp_metadata(self_block)
|
|
|
|
|
@@ -112,25 +127,52 @@ def foreign_fish_detection_beat(block_map, df, all_contrasts, all_conditions, ke
|
|
|
|
|
|
|
|
|
|
group = np.hstack((temp1, temp2))
|
|
|
|
|
score = np.hstack((valid_distances_baseline, valid_distances_comparison))
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
auc = roc_auc_score(group, score)
|
|
|
|
|
if store_roc:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "beat", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "beat", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
else:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "beat", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc})
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "beat", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc})
|
|
|
|
|
print("\n")
|
|
|
|
|
return detection_performances
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def foreign_fish_detection_chirp(block_map, df, all_contrasts, all_conditions, kernel_width=0.0005, cell_name="", store_roc=False):
|
|
|
|
|
def foreign_fish_detection_chirp(block_map, df, cs, all_contrasts, all_conditions, kernel_width=0.0005, cell_name="", store_roc=False):
|
|
|
|
|
"""Tries to detect the presence of a foreign fish by estimating the discriminability of the chirp
|
|
|
|
|
responses in the presence of another fish versus the responses without another fish beeing around.
|
|
|
|
|
|
|
|
|
|
Applies a ROC analysis to the response segments containing the chirp. Does two discrimination tests:
|
|
|
|
|
1) compares the responses to self-chirping alone to the responses to self-chriping in company.
|
|
|
|
|
2) compares the responess to other-chirping to the response during the beat.
|
|
|
|
|
|
|
|
|
|
Tests the assumptions that the distances a) between the self-chriping alone and self-chriping in company
|
|
|
|
|
are larger than the distances within the the self-chirping alone condition and b) the distances between
|
|
|
|
|
other-chirping in company and no one is chirping in company (i.e. beat) are larger than the distances
|
|
|
|
|
within the beat responses.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
block_map ([type]): maps nix blocks to combination of stimulus parameters
|
|
|
|
|
df ([type]): the difference frequency that should be used
|
|
|
|
|
cs ([type]): ths chirpsize that should be used
|
|
|
|
|
all_contrasts ([type]): list of all used contrasts
|
|
|
|
|
all_conditions ([type]): list of all chirp conditions, i.e. self, other, or no-other
|
|
|
|
|
kernel_width (float, optional): std of Gaussian kernel. Defaults to 0.0005.
|
|
|
|
|
cell_name (str, optional): name of the cell. Defaults to "".
|
|
|
|
|
store_roc (bool, optional): if true the full false positives and true positives will be returned leads to huge file sizes!. Defaults to False.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list of dictionaries: the results, auc is the area under the curve, i.e. the discrimination performance in the range [0, 1].
|
|
|
|
|
The 'detection_task' is either "self vs soliloquy" for 1) or "other vs quietness" for 2)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
detection_performances = []
|
|
|
|
|
|
|
|
|
|
for contrast in all_contrasts:
|
|
|
|
|
print(" " * 50, end="\r")
|
|
|
|
|
print("Contrast: %.3f" % contrast, end="\r")
|
|
|
|
|
no_other_block = block_map[(contrast, df, "no-other")]
|
|
|
|
|
self_block = block_map[(contrast, df, "self")]
|
|
|
|
|
other_block = block_map[(contrast, df, "self")]
|
|
|
|
|
no_other_block = block_map[(contrast, df, cs, "no-other")]
|
|
|
|
|
self_block = block_map[(contrast, df, cs, "self")]
|
|
|
|
|
other_block = block_map[(contrast, df, cs, "self")]
|
|
|
|
|
|
|
|
|
|
# get some metadata assuming they are all the same for each condition, which they should
|
|
|
|
|
duration, dt, _, chirp_duration, chirp_times = get_chirp_metadata(self_block)
|
|
|
|
|
@@ -191,54 +233,52 @@ def foreign_fish_detection_chirp(block_map, df, all_contrasts, all_conditions, k
|
|
|
|
|
|
|
|
|
|
group = np.hstack((no_other_temp, self_vs_alone_temp))
|
|
|
|
|
score = np.hstack((valid_no_other_distances, valid_self_vs_alone_distances))
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
auc = roc_auc_score(group, score)
|
|
|
|
|
if store_roc:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "self vs soliloquy", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "self vs soliloquy", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
else:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "self vs soliloquy", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc})
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "self vs soliloquy", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc})
|
|
|
|
|
group = np.hstack((silence_temp, other_vs_silence_temp))
|
|
|
|
|
score = np.hstack((valid_silence_distances, valid_other_vs_silence_distances))
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
auc = roc_auc_score(group, score)
|
|
|
|
|
if store_roc:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "other vs quietness", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
fpr, tpr, _ = roc_curve(group, score, pos_label=1)
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "other vs quietness", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc, "true_positives": tpr, "false_positives": fpr})
|
|
|
|
|
else:
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "other vs quietness", "contrast": contrast, "df": df, "kernel_width": kernel_width, "auc": auc})
|
|
|
|
|
detection_performances.append({"cell": cell_name, "detection_task": "other vs quietness", "contrast": contrast, "df": df, "kernel_width": kernel_width, "chirpsize": cs, "auc": auc})
|
|
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
|
return detection_performances
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def foreign_fish_detection(block_map, all_dfs, all_contrasts, all_conditions, current_df=None, cell_name="", store_roc=False):
|
|
|
|
|
def foreign_fish_detection(block_map, all_dfs, all_contrasts, all_conditions, all_chirpsizes, current_df=None, current_chirpsize=None, cell_name="", store_roc=False):
|
|
|
|
|
dfs = [current_df] if current_df is not None else all_dfs
|
|
|
|
|
chirp_sizes = [current_chirpsize] if current_chirpsize is not None else all_chirpsizes
|
|
|
|
|
kernels = [0.00025, 0.0005, 0.001, 0.0025]
|
|
|
|
|
result_dicts = []
|
|
|
|
|
for cs in chirp_sizes:
|
|
|
|
|
for df in dfs:
|
|
|
|
|
for kw in kernels:
|
|
|
|
|
print("df: %i, kernel: %.4f" % (df, kw))
|
|
|
|
|
print("cs: %i Hz, df: %i Hz, kernel: %.4fs" % (cs, df, kw))
|
|
|
|
|
print("Foreign fish detection during beat:")
|
|
|
|
|
result_dicts.extend(foreign_fish_detection_beat(block_map, df, all_contrasts, all_conditions, kw, cell_name, store_roc))
|
|
|
|
|
result_dicts.extend(foreign_fish_detection_beat(block_map, df, cs, all_contrasts, all_conditions, kw, cell_name, store_roc))
|
|
|
|
|
print("Foreign fish detection during chirp:")
|
|
|
|
|
result_dicts.extend(foreign_fish_detection_chirp(block_map, df, all_contrasts, all_conditions, kw, cell_name, store_roc))
|
|
|
|
|
result_dicts.extend(foreign_fish_detection_chirp(block_map, df, cs, all_contrasts, all_conditions, kw, cell_name, store_roc))
|
|
|
|
|
|
|
|
|
|
return result_dicts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def estimate_chirp_phase(am, chirp_times):
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_cell(filename, dfs=[], contrasts=[], conditions=[]):
|
|
|
|
|
def process_cell(filename):
|
|
|
|
|
print(filename)
|
|
|
|
|
nf = nix.File.open(filename, nix.FileMode.ReadOnly)
|
|
|
|
|
block_map, all_contrasts, all_dfs, all_conditions = sort_blocks(nf)
|
|
|
|
|
block_map, all_contrasts, all_dfs, all_chirpsizes, all_conditions = sort_blocks(nf)
|
|
|
|
|
if "baseline" in block_map.keys():
|
|
|
|
|
baseline_spikes = read_baseline(block_map["baseline"])
|
|
|
|
|
else:
|
|
|
|
|
print("ERROR: no baseline data for file %s!" % filename)
|
|
|
|
|
results = foreign_fish_detection(block_map, all_dfs, all_contrasts, all_conditions, current_df=None,
|
|
|
|
|
results = foreign_fish_detection(block_map, all_dfs, all_contrasts, all_conditions, all_chirpsizes,
|
|
|
|
|
current_df=None, current_chirpsize=None,
|
|
|
|
|
cell_name=filename.split(os.path.sep)[-1].split(".nix")[0], store_roc=False)
|
|
|
|
|
nf.close()
|
|
|
|
|
return results
|
|
|
|
|
@@ -253,7 +293,8 @@ def main():
|
|
|
|
|
for pr in processed_list:
|
|
|
|
|
results.extend(pr)
|
|
|
|
|
df = pd.DataFrame(results)
|
|
|
|
|
df.to_csv(os.path.join(data_folder, "discimination_results.csv"), sep=";")
|
|
|
|
|
df.to_csv(os.path.join(data_folder, "discimination_results2.csv"), sep=";")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|