From 6c2e74a574ca2fd03c16ba617d6f92aaa24d08c3 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Sat, 21 Jan 2023 21:31:09 +0100 Subject: [PATCH 1/7] adding plot --- plot_event_timeline.py | 183 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 plot_event_timeline.py diff --git a/plot_event_timeline.py b/plot_event_timeline.py new file mode 100644 index 0000000..239dcaa --- /dev/null +++ b/plot_event_timeline.py @@ -0,0 +1,183 @@ +import numpy as np + +import os + +import numpy as np +import matplotlib.pyplot as plt + +from IPython import embed +from pandas import read_csv +from modules.logger import makeLogger + +logger = makeLogger(__name__) + + +class Behavior: + """Load behavior data from csv file as class attributes + Attributes + ---------- + behavior: 0: chasing onset, 1: chasing offset, 2: physical contact + behavior_type: + behavioral_category: + comment_start: + comment_stop: + dataframe: pandas dataframe with all the data + duration_s: + media_file: + observation_date: + observation_id: + start_s: start time of the event in seconds + stop_s: stop time of the event in seconds + total_length: + """ + + def __init__(self, folder_path: str) -> None: + + + LED_on_time_BORIS = np.load(os.path.join(folder_path, 'LED_on_time.npy'), allow_pickle=True) + self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) + csv_filename = [f for f in os.listdir(folder_path) if f.endswith('.csv')][0] # check if there are more than one csv file + self.dataframe = read_csv(os.path.join(folder_path, csv_filename)) + self.chirps = np.load(os.path.join(folder_path, 'chirps.npy'), allow_pickle=True) + self.chirps_ids = np.load(os.path.join(folder_path, 'chirps_ids.npy'), allow_pickle=True) + + for k, key in enumerate(self.dataframe.keys()): + key = key.lower() + if ' ' in key: + key = key.replace(' ', '_') + if '(' in key: + key = key.replace('(', '') + key = key.replace(')', '') + setattr(self, key, np.array(self.dataframe[self.dataframe.keys()[k]])) + + last_LED_t_BORIS = LED_on_time_BORIS[-1] + real_time_range = self.time[-1] - self.time[0] + factor = 1.034141 + shift = last_LED_t_BORIS - real_time_range * factor + self.start_s = (self.start_s - shift) / factor + self.stop_s = (self.stop_s - shift) / factor + +def correct_chasing_events( + category: np.ndarray, + timestamps: np.ndarray + ) -> tuple[np.ndarray, np.ndarray]: + + onset_ids = np.arange( + len(category))[category == 0] + offset_ids = np.arange( + len(category))[category == 1] + + # Check whether on- or offset is longer and calculate length difference + if len(onset_ids) > len(offset_ids): + len_diff = len(onset_ids) - len(offset_ids) + longer_array = onset_ids + shorter_array = offset_ids + logger.info(f'Onsets are greater than offsets by {len_diff}') + elif len(onset_ids) < len(offset_ids): + len_diff = len(offset_ids) - len(onset_ids) + longer_array = offset_ids + shorter_array = onset_ids + logger.info(f'Offsets are greater than offsets by {len_diff}') + elif len(onset_ids) == len(offset_ids): + logger.info('Chasing events are equal') + return category, timestamps + + + # Correct the wrong chasing events; delete double events + wrong_ids = [] + for i in range(len(longer_array)-(len_diff+1)): + if (shorter_array[i] > longer_array[i]) & (shorter_array[i] < longer_array[i+1]): + pass + else: + wrong_ids.append(longer_array[i]) + longer_array = np.delete(longer_array, i) + + category = np.delete( + category, wrong_ids) + timestamps = np.delete( + timestamps, wrong_ids) + return category, timestamps + + + +def main(datapath: str): + # behabvior is pandas dataframe with all the data + bh = Behavior(datapath) + # chirps are not sorted in time (presumably due to prior groupings) + # get and sort chirps and corresponding fish_ids of the chirps + chirps = bh.chirps[np.argsort(bh.chirps)] + chirps_fish_ids = bh.chirps_ids[np.argsort(bh.chirps)] + category = bh.behavior + timestamps = bh.start_s + # Correct for doubles in chasing on- and offsets to get the right on-/offset pairs + # Get rid of tracking faults (two onsets or two offsets after another) + category, timestamps = correct_chasing_events(category, timestamps) + + # split categories + chasing_onset = (timestamps[category == 0]/ 60) /60 + chasing_offset = (timestamps[category == 1]/ 60) /60 + physical_contact = (timestamps[category == 2] / 60) /60 + + all_fish_ids = np.unique(chirps_fish_ids) + # Associate chirps to inidividual fish + fish1 = (chirps[chirps_fish_ids == all_fish_ids[0]] / 60) /60 + fish2 = (chirps[chirps_fish_ids == all_fish_ids[1]] / 60) /60 + + fig, ax = plt.subplots(4, 1, figsize=(10, 5), height_ratios=[0.5, 0.5, 0.5, 6]) + # marker size + s = 200 + ax[0].scatter(physical_contact, np.ones(len(physical_contact)), color='red', marker='|', s=s) + ax[1].scatter(chasing_onset, np.ones(len(chasing_onset)), color='blue', marker='|', s=s ) + ax[1].scatter(chasing_offset, np.ones(len(chasing_offset)), color='green', marker='|', s=s) + ax[2].scatter(fish1, np.ones(len(fish1))-0.25, color='blue', marker='|', s=s) + ax[2].scatter(fish2, np.zeros(len(fish2))+0.25, color='green', marker='|', s=s) + ax[3].scatter(fish2, np.zeros(len(fish2))+0.25, color='green', marker='|', s=s) + + # Hide grid lines + ax[0].grid(False) + ax[0].set_frame_on(False) + ax[0].set_xticks([]) + ax[0].set_yticks([]) + + ax[1].grid(False) + ax[1].set_frame_on(False) + ax[1].set_xticks([]) + ax[1].set_yticks([]) + + ax[2].grid(False) + ax[2].set_frame_on(False) + ax[2].set_yticks([]) + ax[2].set_xticks([]) + + ax[3].axvspan(0, 3, 0, 5, facecolor='grey', alpha=0.5) + + labelpad = 40 + ax[0].set_ylabel('Physical contact', rotation=0, labelpad=labelpad) + ax[1].set_ylabel('Chasing events', rotation=0, labelpad=labelpad) + ax[2].set_ylabel('Chirps', rotation=0, labelpad=labelpad) + ax[3].set_ylabel('EODf') + + ax[3].set_xlabel('Time [h]') + + plt.show() + + # plot chirps + + + """ + for track_id in np.unique(ident): + # window_index for time array in time window + window_index = np.arange(len(idx))[(ident == track_id) & + (time[idx] >= t0) & + (time[idx] <= (t0+dt))] + freq_temp = freq[window_index] + time_temp = time[idx[window_index]] + #mean_freq = np.mean(freq_temp) + #fdata = bandpass_filter(data_oi[:, track_id], data.samplerate, mean_freq-5, mean_freq+200) + ax.plot(time_temp - t0, freq_temp) + """ + +if __name__ == '__main__': + # Path to the data + datapath = '../data/mount_data/2020-05-13-10_00/' + main(datapath) From 333bb045a6dcfaec4433c4ec0bd29ccc91b1cd31 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Sat, 21 Jan 2023 21:32:40 +0100 Subject: [PATCH 2/7] wrong folder --- code/behavior.py | 26 ------------------- .../plot_event_timeline.py | 0 2 files changed, 26 deletions(-) rename plot_event_timeline.py => code/plot_event_timeline.py (100%) diff --git a/code/behavior.py b/code/behavior.py index da02838..dd32ef2 100644 --- a/code/behavior.py +++ b/code/behavior.py @@ -37,32 +37,6 @@ class Behavior: new_key = new_key.lower() setattr(self, new_key, np.array(self.dataframe[key])) -""" -1 - chasing onset -2 - chasing offset -3 - physical contact event - -temporal encpding needs to be corrected ... not exactly 25FPS. - -### correspinding python code ### - - factor = 1.034141 - LED_on_time_BORIS = np.load(os.path.join(folder_path, 'LED_on_time.npy'), allow_pickle=True) - last_LED_t_BORIS = LED_on_time_BORIS[-1] - real_time_range = times[-1] - times[0] - shift = last_LED_t_BORIS - real_time_range * factor - - data = pd.read_csv(os.path.join(folder_path, file[1:-7] + '.csv')) - boris_times = data['Start (s)'] - data_times = [] - - for Cevent_t in boris_times: - Cevent_boris_times = (Cevent_t - shift) / factor - data_times.append(Cevent_boris_times) - - data_times = np.array(data_times) - behavior = data['Behavior'] -""" def main(datapath: str): # behabvior is pandas dataframe with all the data diff --git a/plot_event_timeline.py b/code/plot_event_timeline.py similarity index 100% rename from plot_event_timeline.py rename to code/plot_event_timeline.py From 16e6003958ec3ed7101ad1d0d538e2c08cd1300e Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Sun, 22 Jan 2023 11:29:23 +0100 Subject: [PATCH 3/7] adding logger for csv file readout --- code/plot_event_timeline.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code/plot_event_timeline.py b/code/plot_event_timeline.py index 239dcaa..c5342d0 100644 --- a/code/plot_event_timeline.py +++ b/code/plot_event_timeline.py @@ -36,7 +36,8 @@ class Behavior: LED_on_time_BORIS = np.load(os.path.join(folder_path, 'LED_on_time.npy'), allow_pickle=True) self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) - csv_filename = [f for f in os.listdir(folder_path) if f.endswith('.csv')][0] # check if there are more than one csv file + csv_filename = [f for f in os.listdir(folder_path) if f.endswith('.csv')][0] + logger.info(f'CSV file: {csv_filename}') self.dataframe = read_csv(os.path.join(folder_path, csv_filename)) self.chirps = np.load(os.path.join(folder_path, 'chirps.npy'), allow_pickle=True) self.chirps_ids = np.load(os.path.join(folder_path, 'chirps_ids.npy'), allow_pickle=True) @@ -103,6 +104,7 @@ def correct_chasing_events( def main(datapath: str): # behabvior is pandas dataframe with all the data bh = Behavior(datapath) + embed() # chirps are not sorted in time (presumably due to prior groupings) # get and sort chirps and corresponding fish_ids of the chirps chirps = bh.chirps[np.argsort(bh.chirps)] From 039a027108c393335073fee2bdf71471d90ccc7c Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Mon, 23 Jan 2023 09:33:47 +0100 Subject: [PATCH 4/7] adding plots --- code/plot_event_timeline.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/plot_event_timeline.py b/code/plot_event_timeline.py index c5342d0..93920b9 100644 --- a/code/plot_event_timeline.py +++ b/code/plot_event_timeline.py @@ -104,7 +104,6 @@ def correct_chasing_events( def main(datapath: str): # behabvior is pandas dataframe with all the data bh = Behavior(datapath) - embed() # chirps are not sorted in time (presumably due to prior groupings) # get and sort chirps and corresponding fish_ids of the chirps chirps = bh.chirps[np.argsort(bh.chirps)] @@ -125,7 +124,7 @@ def main(datapath: str): fish1 = (chirps[chirps_fish_ids == all_fish_ids[0]] / 60) /60 fish2 = (chirps[chirps_fish_ids == all_fish_ids[1]] / 60) /60 - fig, ax = plt.subplots(4, 1, figsize=(10, 5), height_ratios=[0.5, 0.5, 0.5, 6]) + fig, ax = plt.subplots(4, 1, figsize=(10, 5), height_ratios=[0.5, 0.5, 0.5, 6], sharex=True) # marker size s = 200 ax[0].scatter(physical_contact, np.ones(len(physical_contact)), color='red', marker='|', s=s) From d754013abaf5281670b965b8fb8c8f55ad7ad5f5 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Mon, 23 Jan 2023 12:04:33 +0100 Subject: [PATCH 5/7] finishing event_time --- code/plot_event_timeline.py | 61 ++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/code/plot_event_timeline.py b/code/plot_event_timeline.py index 93920b9..1b4a113 100644 --- a/code/plot_event_timeline.py +++ b/code/plot_event_timeline.py @@ -8,6 +8,9 @@ import matplotlib.pyplot as plt from IPython import embed from pandas import read_csv from modules.logger import makeLogger +from modules.plotstyle import PlotStyle + +ps = PlotStyle() logger = makeLogger(__name__) @@ -35,13 +38,19 @@ class Behavior: LED_on_time_BORIS = np.load(os.path.join(folder_path, 'LED_on_time.npy'), allow_pickle=True) - self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) + csv_filename = [f for f in os.listdir(folder_path) if f.endswith('.csv')][0] logger.info(f'CSV file: {csv_filename}') self.dataframe = read_csv(os.path.join(folder_path, csv_filename)) + self.chirps = np.load(os.path.join(folder_path, 'chirps.npy'), allow_pickle=True) self.chirps_ids = np.load(os.path.join(folder_path, 'chirps_ids.npy'), allow_pickle=True) + self.ident = np.load(os.path.join(folder_path, 'ident_v.npy'), allow_pickle=True) + self.idx = np.load(os.path.join(folder_path, 'idx_v.npy'), allow_pickle=True) + self.freq = np.load(os.path.join(folder_path, 'fund_v.npy'), allow_pickle=True) + self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) + for k, key in enumerate(self.dataframe.keys()): key = key.lower() if ' ' in key: @@ -120,37 +129,57 @@ def main(datapath: str): physical_contact = (timestamps[category == 2] / 60) /60 all_fish_ids = np.unique(chirps_fish_ids) + fish1_id = all_fish_ids[0] + fish2_id = all_fish_ids[1] # Associate chirps to inidividual fish - fish1 = (chirps[chirps_fish_ids == all_fish_ids[0]] / 60) /60 - fish2 = (chirps[chirps_fish_ids == all_fish_ids[1]] / 60) /60 + fish1 = (chirps[chirps_fish_ids == fish1_id] / 60) /60 + fish2 = (chirps[chirps_fish_ids == fish2_id] / 60) /60 + fish1_color = ps.red + fish2_color = ps.orange fig, ax = plt.subplots(4, 1, figsize=(10, 5), height_ratios=[0.5, 0.5, 0.5, 6], sharex=True) # marker size s = 200 - ax[0].scatter(physical_contact, np.ones(len(physical_contact)), color='red', marker='|', s=s) - ax[1].scatter(chasing_onset, np.ones(len(chasing_onset)), color='blue', marker='|', s=s ) - ax[1].scatter(chasing_offset, np.ones(len(chasing_offset)), color='green', marker='|', s=s) - ax[2].scatter(fish1, np.ones(len(fish1))-0.25, color='blue', marker='|', s=s) - ax[2].scatter(fish2, np.zeros(len(fish2))+0.25, color='green', marker='|', s=s) - ax[3].scatter(fish2, np.zeros(len(fish2))+0.25, color='green', marker='|', s=s) + ax[0].scatter(physical_contact, np.ones(len(physical_contact)), color='firebrick', marker='|', s=s) + ax[1].scatter(chasing_onset, np.ones(len(chasing_onset)), color='green', marker='|', s=s ) + ax[2].scatter(fish1, np.ones(len(fish1))-0.25, color=fish1_color, marker='|', s=s) + ax[2].scatter(fish2, np.zeros(len(fish2))+0.25, color=fish2_color, marker='|', s=s) + + + freq_temp = bh.freq[bh.ident==fish1_id] + time_temp = bh.time[bh.idx[bh.ident==fish1_id]] + ax[3].plot((time_temp/ 60) /60, freq_temp, color=fish1_color) + + freq_temp = bh.freq[bh.ident==fish2_id] + time_temp = bh.time[bh.idx[bh.ident==fish2_id]] + ax[3].plot((time_temp/ 60) /60, freq_temp, color=fish2_color) + # Hide grid lines ax[0].grid(False) ax[0].set_frame_on(False) ax[0].set_xticks([]) ax[0].set_yticks([]) + ps.hide_ax(ax[0]) + ax[1].grid(False) ax[1].set_frame_on(False) ax[1].set_xticks([]) ax[1].set_yticks([]) + ps.hide_ax(ax[1]) + ax[2].grid(False) ax[2].set_frame_on(False) ax[2].set_yticks([]) ax[2].set_xticks([]) + ps.hide_ax(ax[2]) + + ax[3].axvspan(0, 3, 0, 5, facecolor='grey', alpha=0.5) + ax[3].set_xticks(np.arange(0, 6.1, 0.5)) labelpad = 40 ax[0].set_ylabel('Physical contact', rotation=0, labelpad=labelpad) @@ -162,21 +191,9 @@ def main(datapath: str): plt.show() - # plot chirps + # plot chirps - """ - for track_id in np.unique(ident): - # window_index for time array in time window - window_index = np.arange(len(idx))[(ident == track_id) & - (time[idx] >= t0) & - (time[idx] <= (t0+dt))] - freq_temp = freq[window_index] - time_temp = time[idx[window_index]] - #mean_freq = np.mean(freq_temp) - #fdata = bandpass_filter(data_oi[:, track_id], data.samplerate, mean_freq-5, mean_freq+200) - ax.plot(time_temp - t0, freq_temp) - """ if __name__ == '__main__': # Path to the data From 9fcdcfe01989d48eb8688e67d35bcbf5c0047a73 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Mon, 23 Jan 2023 13:52:22 +0100 Subject: [PATCH 6/7] adding coment of spectogramm --- code/plot_event_timeline.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/code/plot_event_timeline.py b/code/plot_event_timeline.py index 1b4a113..6c984be 100644 --- a/code/plot_event_timeline.py +++ b/code/plot_event_timeline.py @@ -4,6 +4,7 @@ import os import numpy as np import matplotlib.pyplot as plt +from thunderfish.powerspectrum import decibel from IPython import embed from pandas import read_csv @@ -49,7 +50,8 @@ class Behavior: self.ident = np.load(os.path.join(folder_path, 'ident_v.npy'), allow_pickle=True) self.idx = np.load(os.path.join(folder_path, 'idx_v.npy'), allow_pickle=True) self.freq = np.load(os.path.join(folder_path, 'fund_v.npy'), allow_pickle=True) - self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) + self.time = np.load(os.path.join(folder_path, "times.npy"), allow_pickle=True) + self.spec = np.load(os.path.join(folder_path, "spec.npy"), allow_pickle=True) for k, key in enumerate(self.dataframe.keys()): key = key.lower() @@ -154,6 +156,7 @@ def main(datapath: str): time_temp = bh.time[bh.idx[bh.ident==fish2_id]] ax[3].plot((time_temp/ 60) /60, freq_temp, color=fish2_color) + #ax[3].imshow(decibel(bh.spec), extent=[bh.time[0]/60/60, bh.time[-1]/60/60, 0, 2000], aspect='auto', origin='lower') # Hide grid lines ax[0].grid(False) @@ -169,7 +172,6 @@ def main(datapath: str): ax[1].set_yticks([]) ps.hide_ax(ax[1]) - ax[2].grid(False) ax[2].set_frame_on(False) ax[2].set_yticks([]) @@ -190,7 +192,7 @@ def main(datapath: str): ax[3].set_xlabel('Time [h]') plt.show() - + embed() # plot chirps From a3ddd49040e746d8f621f03f7074c4e70a0e7b49 Mon Sep 17 00:00:00 2001 From: weygoldt <88969563+weygoldt@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:09:16 +0100 Subject: [PATCH 7/7] all broken --- code/chirpdetection.py | 60 +++++++++++++++++++------------------ code/chirpdetector_conf.yml | 2 +- code/extract_chirps.py | 44 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 code/extract_chirps.py diff --git a/code/chirpdetection.py b/code/chirpdetection.py index c4a04e7..63b6ebe 100755 --- a/code/chirpdetection.py +++ b/code/chirpdetection.py @@ -130,7 +130,7 @@ class PlotBuffer: data_oi, self.data.raw_rate, self.t0 - 5, - [np.max(self.frequency) - 200, np.max(self.frequency) + 200] + [np.min(self.frequency) - 200, np.max(self.frequency) + 200] ) for track_id in self.data.ids: @@ -145,14 +145,15 @@ class PlotBuffer: # get tracked frequencies and their times f = self.data.freq[window_idx] - t = self.data.time[ - self.data.idx[self.data.ident == self.track_id]] - tmask = (t >= t0_track) & (t <= (t0_track + dt_track)) + # t = self.data.time[ + # self.data.idx[self.data.ident == self.track_id]] + # tmask = (t >= t0_track) & (t <= (t0_track + dt_track)) + t = self.data.time[self.data.idx[window_idx]] if track_id == self.track_id: - ax0.plot(t[tmask]-self.t0_old, f, lw=lw, + ax0.plot(t-self.t0_old, f, lw=lw, zorder=10, color=ps.gblue1) else: - ax0.plot(t[tmask]-self.t0_old, f, lw=lw, + ax0.plot(t-self.t0_old, f, lw=lw, zorder=10, color=ps.gray, alpha=0.5) ax0.fill_between( @@ -472,7 +473,9 @@ def find_searchband( ) # search window in boolean - search_window_bool = np.ones_like(len(search_window), dtype=bool) + bool_lower = np.ones_like(search_window, dtype=bool) + bool_upper = np.ones_like(search_window, dtype=bool) + search_window_bool = np.ones_like(search_window, dtype=bool) # make seperate arrays from the qartiles q25 = np.asarray([i[0] for i in frequency_percentiles]) @@ -492,11 +495,10 @@ def find_searchband( q25_temp = q25[percentiles_ids == check_track_id] q75_temp = q75[percentiles_ids == check_track_id] - print(q25_temp, q75_temp) - - search_window_bool[ - (search_window > q25_temp) & (search_window < q75_temp) - ] = False + bool_lower[search_window > q25_temp - config.search_res] = False + bool_upper[search_window < q75_temp + config.search_res] = False + search_window_bool[(bool_lower == False) & + (bool_upper == False)] = False # find gaps in search window search_window_indices = np.arange(len(search_window)) @@ -552,7 +554,7 @@ def find_searchband( return config.default_search_freq -def main(datapath: str, plot: str) -> None: +def chirpdetection(datapath: str, plot: str) -> None: assert plot in [ "save", @@ -561,6 +563,7 @@ def main(datapath: str, plot: str) -> None: ], "plot must be 'save', 'show' or 'false'" # load raw file + print('datapath', datapath) data = LoadData(datapath) # load config file @@ -589,8 +592,8 @@ def main(datapath: str, plot: str) -> None: raw_time = np.arange(data.raw.shape[0]) / data.raw_rate # good chirp times for data: 2022-06-02-10_00 - # window_start_index = (3 * 60 * 60 + 6 * 60 + 43.5 + 5) * data.raw_rate - # window_duration_index = 60 * data.raw_rate + window_start_index = (3 * 60 * 60 + 6 * 60 + 43.5 + 5) * data.raw_rate + window_duration_index = 60 * data.raw_rate # t0 = 0 # dt = data.raw.shape[0] @@ -651,14 +654,14 @@ def main(datapath: str, plot: str) -> None: # approximate sampling rate to compute expected durations if there # is data available for this time window for this fish id - track_samplerate = np.mean(1 / np.diff(data.time)) - expected_duration = ( - (window_start_seconds + window_duration_seconds) - - window_start_seconds - ) * track_samplerate +# track_samplerate = np.mean(1 / np.diff(data.time)) +# expected_duration = ( +# (window_start_seconds + window_duration_seconds) +# - window_start_seconds +# ) * track_samplerate # check if tracked data available in this window - if len(current_frequencies) < expected_duration / 2: + if len(current_frequencies) < 3: logger.warning( f"Track {track_id} has no data in window {st}, skipping." ) @@ -918,11 +921,9 @@ def main(datapath: str, plot: str) -> None: multielectrode_chirps.append(singleelectrode_chirps) # only initialize the plotting buffer if chirps are detected - chirp_detected = ( - (el == config.number_electrodes - 1) - & (len(singleelectrode_chirps) > 0) - & (plot in ["show", "save"]) - ) + chirp_detected = (el == (config.number_electrodes - 1) + & (plot in ["show", "save"]) + ) if chirp_detected: @@ -987,11 +988,12 @@ def main(datapath: str, plot: str) -> None: # if chirps are detected and the plot flag is set, plot the # chirps, otheswise try to delete the buffer if it exists - if len(multielectrode_chirps_validated) > 0: + if ((len(multielectrode_chirps_validated) > 0) & (plot in ["show", "save"])): try: buffer.plot_buffer(multielectrode_chirps_validated, plot) + del buffer except NameError: - pass + embed() else: try: del buffer @@ -1049,4 +1051,4 @@ if __name__ == "__main__": datapath = "../data/2022-06-02-10_00/" # datapath = "/home/weygoldt/Data/uni/efishdata/2016-colombia/fishgrid/2016-04-09-22_25/" # datapath = "/home/weygoldt/Data/uni/chirpdetection/GP2023_chirp_detection/data/mount_data/2020-03-13-10_00/" - main(datapath, plot="save") + chirpdetection(datapath, plot="show") diff --git a/code/chirpdetector_conf.yml b/code/chirpdetector_conf.yml index 2f4fc9a..af48384 100755 --- a/code/chirpdetector_conf.yml +++ b/code/chirpdetector_conf.yml @@ -3,7 +3,7 @@ dataroot: "../data/" outputdir: "../output/" # Duration and overlap of the analysis window in seconds -window: 10 +window: 5 overlap: 1 edge: 0.25 diff --git a/code/extract_chirps.py b/code/extract_chirps.py new file mode 100644 index 0000000..2180a7f --- /dev/null +++ b/code/extract_chirps.py @@ -0,0 +1,44 @@ +import os +import numpy as np +from chirpdetection import chirpdetection +from IPython import embed + + +def main(datapaths): + + for path in datapaths: + chirpdetection(path, plot='show') + + +if __name__ == '__main__': + + dataroot = '../data/mount_data/' + + datasets = sorted([name for name in os.listdir(dataroot) if os.path.isdir( + os.path.join(dataroot, name))]) + + valid_datasets = [] + + for dataset in datasets: + + path = os.path.join(dataroot, dataset) + csv_name = '-'.join(dataset.split('-')[:3]) + '.csv' + + if os.path.exists(os.path.join(path, csv_name)) is False: + continue + + if os.path.exists(os.path.join(path, 'ident_v.npy')) is False: + continue + + ident = np.load(os.path.join(path, 'ident_v.npy')) + number_of_fish = len(np.unique(ident[~np.isnan(ident)])) + if number_of_fish != 2: + continue + + valid_datasets.append(dataset) + + datapaths = [os.path.join(dataroot, dataset) + + '/' for dataset in valid_datasets] + embed() + + main(datapaths[3])