From 335a05d51a661385a0c9f8de4ee962d18b24a312 Mon Sep 17 00:00:00 2001 From: mbsantiago Date: Tue, 16 Jul 2024 01:30:34 +0100 Subject: [PATCH] Decouple config loading from preprocess function --- batdetect2/api.py | 7 +++++-- batdetect2/data/labels.py | 1 - batdetect2/train/preprocess.py | 14 +++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/batdetect2/api.py b/batdetect2/api.py index 7006914..9920006 100644 --- a/batdetect2/api.py +++ b/batdetect2/api.py @@ -32,7 +32,7 @@ results will be combined into a dictionary with the following keys: for each detection. The CNN features are the output of the CNN before the final classification layer. You can use these features to train your own classifier, or to do other processing on the detections. - They are in the same order as the detections in + They are in the same order as the detections in `results['pred_dict']['annotation']`. Will only be returned if the `cnn_feats` parameter in the config is set to `True`. - `spec_slices`: Optional. A list of `numpy` arrays containing the spectrogram @@ -96,6 +96,7 @@ If you wish to use a custom model or change the default parameters, please consult the API documentation in the code. """ + import warnings from typing import List, Optional, Tuple @@ -410,7 +411,9 @@ def print_summary(results: RunResults) -> None: Detection result. """ print("Results for " + results["pred_dict"]["id"]) - print("{} calls detected\n".format(len(results["pred_dict"]["annotation"]))) + print( + "{} calls detected\n".format(len(results["pred_dict"]["annotation"])) + ) print("time\tprob\tlfreq\tspecies_name") for ann in results["pred_dict"]["annotation"]: diff --git a/batdetect2/data/labels.py b/batdetect2/data/labels.py index b1576e0..67d8122 100644 --- a/batdetect2/data/labels.py +++ b/batdetect2/data/labels.py @@ -65,7 +65,6 @@ def generate_heatmaps( # Get the position of the sound event time, frequency = geometry.get_geometry_point(geom, position=position) - print(time, frequency) # Set 1.0 at the position of the sound event in the detection heatmap detection_heatmap = arrays.set_value_at_pos( diff --git a/batdetect2/train/preprocess.py b/batdetect2/train/preprocess.py index bfbcc64..b23c3c3 100644 --- a/batdetect2/train/preprocess.py +++ b/batdetect2/train/preprocess.py @@ -116,6 +116,9 @@ def preprocess_single_annotation( if path.is_file() and not replace: return + if path.is_file() and replace: + path.unlink() + sample = generate_train_example( clip_annotation, class_mapper, @@ -133,21 +136,18 @@ def preprocess_annotations( target_sigma: float = TARGET_SIGMA, filename_fn: FilenameFn = _get_filename, replace: bool = False, - config_file: Optional[PathLike] = None, + config: Optional[PreprocessingConfig] = None, max_workers: Optional[int] = None, - **kwargs, ) -> None: """Preprocess annotations and save to disk.""" output_dir = Path(output_dir) + if config is None: + config = PreprocessingConfig() + if not output_dir.is_dir(): output_dir.mkdir(parents=True) - if config_file is not None: - config = load_config(config_file, **kwargs) - else: - config = PreprocessingConfig(**kwargs) - with Pool(max_workers) as pool: list( tqdm(