From 34de2a3262ad072bdbc11d1b76bb32759dcfbf21 Mon Sep 17 00:00:00 2001 From: Stephane Chavin <stephane.chavin@lis-lab.fr> Date: Sun, 21 Jul 2024 20:41:39 +0200 Subject: [PATCH] correct get_time_freq --- get_time_freq_detection.py | 11 +++++++---- utils.py | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/get_time_freq_detection.py b/get_time_freq_detection.py index 29655be..803ed72 100755 --- a/get_time_freq_detection.py +++ b/get_time_freq_detection.py @@ -20,7 +20,7 @@ def main(arguments): df, dir_path = utils.detection2time_freq(annotations_folder=arguments.path_to_data, duration=arguments.duration, outdir=arguments.directory, - sr=arguments.sr, + rf=arguments.rf, names=names, wav=args.path_to_wav, raven=args.raven) @@ -35,7 +35,10 @@ def main(arguments): '"begin time (s)"; "end time (s)" and the species detected' ' in the given files.') ds.attrs['date_created'] = pd.Timestamp.now().isoformat() - ds.attrs['creator_name'] = os.getlogin() + try: + ds.attrs['creator_name'] = os.getlogin() + except Exception: + ds.attrs['creator_name'] = input('Your name : ') # Save Dataset to NetCDF file ds.to_netcdf(dir_path) @@ -51,8 +54,8 @@ if __name__ == "__main__": help='Directory where the dataframe will be stored') parser.add_argument('names', type=str, help='path to YOLOv5 custom_data.yaml file') - parser.add_argument('-s', '--sr', type=int, - help='Sampling Rate of the spectrogram', required=True) + parser.add_argument('--rf', type=int, + help='Resample frequency', required=True) parser.add_argument('--duration', type=int, help='Duration of the spectrogram', default=8) parser.add_argument('--path_to_wav', type=utils.arg_directory, diff --git a/utils.py b/utils.py index 080aa8f..bab268f 100755 --- a/utils.py +++ b/utils.py @@ -355,17 +355,19 @@ def prepare_dataframe(df, args): return df, species_list -def detection2time_freq(annotations_folder, duration, outdir, sr, names, wav, raven): +def detection2time_freq(annotations_folder, duration, outdir, rf, names, wav, raven): """ Collect all .txt detection and get time and frequency informations - :param annotations_folder (str): Path to the .json files + :param annotations_folder (str): Path to the .txt files :param duration (int): Directory to save the .txt files - :param outfir (str): Directory to save the .txt files - :param sr (int): Directory to save the .txt files - :param names (str): Directory to save the .txt files + :param outdir (str): Directory to save the .txt files + :param rf (int): Resampling freq. + :param names (str): names of the classes + :param wav (str): Path to the wav + :param raven (int): Save into Raven format or not """ today = date.today() - out_file = f'YOLO_detection_{today.day}_{today.month}_freq_{sr}_duration_{duration}.nc' + out_file = f'YOLO_detection_{today.day}_{today.month}_freq_{rf}_duration_{duration}.nc' # Load and process data df = pd.concat({f: pd.read_csv(os.path.join(annotations_folder, f), @@ -390,8 +392,8 @@ def detection2time_freq(annotations_folder, duration, outdir, sr, names, wav, ra df['species'] = df['class'].apply(lambda x: names[int(x)]) df['pos'] = (df['x'] * duration) + df['class'].astype(int) - df['Low Freq (Hz)'] = (1 - df['y']) * (sr / 2) - (df['h'] * (sr / 2)) / 2 - df['High Freq (Hz)'] = (1 - df['y']) * (sr / 2) + (df['h'] * (sr / 2)) / 2 + df['Low Freq (Hz)'] = (1 - df['y']) * (rf / 2) - (df['h'] * (rf / 2)) / 2 + df['High Freq (Hz)'] = (1 - df['y']) * (rf / 2) + (df['h'] * (rf / 2)) / 2 df['Begin Time (s)'] = df['pos'] - (df['w'] * duration) / 2 df['End Time (s)'] = df['pos'] + (df['w'] * duration) / 2 df['duration'] = df['End Time (s)'] - df['Begin Time (s)'] -- GitLab