BatBot API
BatBot is the machine learning interface for the Kitware BatAI project. This page specifies the Python API to interact with all of the algorithms and machine learning models that have been pretrained for inference in a production environment.
Environment Variables
The BatBot API and CLI have two environment variables (envars) that allow you to configure global settings and configurations.
VERBOSE(default: not set)A verbosity flag that can be set to turn on debug logging. Defaults to “not set”, which translates to no debug logging. Setting this value to anything will turn on debug logging (e.g.,
VERBOSE=1).
Spectrogram
- batbot.spectrogram.calculate_astar_grid_and_endpoints(segment, index, segmentmask, peak, canvas, kernel=7, output_path='.')[source]
- batbot.spectrogram.calculate_harmonic_and_echo_flags(original, index, segmentmask, harmonic, echo, canvas, kernel=5, output_path='.')[source]
- batbot.spectrogram.calculate_window_and_stride(stft_db, duration, window_size_ms=12, strides_per_window=3, time_vec=None)[source]
- batbot.spectrogram.compute_wrapper(wav_filepath, out_file_stem=None, output_folder=None, fast_mode=False, force_overwrite=False, quiet=False, annotations=None, bitdepth=16, mask_secondary_effects=False, plot_uncompressed_amplitude=False, include_original_sr=False, time_buffer_ms=1.0, debug=False, **kwargs)[source]
Compute the spectrograms for a given input WAV and saves them to disk.
If a given spectrogram has already been rendered to disk, it will not be recomputed.
- Parameters:
wav_filepath (str) – WAV filepath (relative or absolute) to compute spectrograms for.
ext (str, optional) – The file extension of the resulting spectrogram files. If this value is not specified, it will use the same extension as wav_filepath. Passed as input to
batbot.spectrogram.spectrogram_filepath(). Defaults toNone.**kwargs – keyword arguments passed to
batbot.spectrogram.spectrogram_grid()
- Returns:
the original WAV file’s sample rate.
the original WAV file’s duration in seconds.
tuple of spectrogram’s (width, height) in pixels
tuple of spectrogram’s (min, max) frequency
list of spectrogram filepaths, split by 50k horizontal pixels
- Return type:
- batbot.spectrogram.create_coarse_candidates(stft_db, window, stride, threshold_stddev=3.0, min_band_idx=None)[source]
- batbot.spectrogram.extract_contour_keypoints(path, canvas, index, peak, contour_smoothing_sigma=5, output_path='.')[source]
- batbot.spectrogram.filter_candidates_to_ranges(stft_db, candidates, window=16, skew_stddev=2.0, area_percent=0.1, min_band_idx=None, output_path=None, fast_mode=False, quiet=False)[source]
- batbot.spectrogram.filter_contour(segment, index, med_db=None, std_db=None, kernel=5, output_path='.')[source]
- batbot.spectrogram.find_contour_and_peak(segment, index, max_locations, peak_db=None, peak_db_std=None, threshold_std=2.0, sigma=5, output_path='.', threshold=None)[source]
- batbot.spectrogram.find_contour_connected_components(segment, index, locations, sequence=4, output_path='.')[source]
- batbot.spectrogram.find_harmonic(segmentmask, index, freq_offset, kernel=15, output_path='.')[source]
- batbot.spectrogram.gain_stft(stft_db, gain_db=120.0, autogain_stddev=5.0, max_band_idx=None)[source]
- batbot.spectrogram.generate_waveplot(waveform, stft_db, hop_length=16, size_multiplier=4, bg_color=(240, 255, 255), fg_color=(237, 149, 100), zero_color=(0, 0, 0))[source]
- batbot.spectrogram.get_debug_path(output_folder, wav_filepath, enabled, purge=True, ensure=True)[source]
- batbot.spectrogram.load_stft(wav_filepath, sr=250000.0, n_fft=512, window='blackmanharris', win_length=256, hop_length=16, fast_mode=False, use_original_sr=False)[source]
- batbot.spectrogram.normalize_contour(segment, index, dtype=None, blur=True, kernel=5, output_path='.')[source]
- batbot.spectrogram.plot_chirp_candidates(stft_db, candidate_dbs, ranges, reject_idxs, output_path='.', output_filename='candidates.png')[source]
- batbot.spectrogram.plot_histogram(image, ignore_zeros=False, max_val=None, smoothing=128, min_band_idx=None, csum_threshold=0.95, output_path='.', output_filename='histogram.png')[source]
- batbot.spectrogram.refine_contour(segment, index, max_locations, segmentmask, peak, output_path='.')[source]
- batbot.spectrogram.remove_harmonic_and_echo(segment, index, harmonic, echo, threshold, med_db=None, std_db=None, kernel=15, output_path='.')[source]
- batbot.spectrogram.significant_contour_path(begin, end, freq_step, time_step, min_bandwidth_khz=6000.0, min_duration_ms=2.0)[source]
Pipeline
The above components must be run in the correct order, but BatBot also offers a processing pipeline.
The machine learning (ML) model can be pre-downloaded and fetched by a single call to
batbot.fetch() and the unified pipeline can be run by the function batbot.pipeline().
Below is example code for how these components interact.
Furthermore, there is an application demo file (app.py) that shows how the entire pipeline can
be run on WAV files.
# Get WAV filepath
filepath = '/path/to/file.wav'
# Run tiling
output_paths, metadata_path, metadata = spectrogram.compute(filepath)
- batbot.__init__.batch(filepaths, config=None, clean=True)[source]
Run the ML pipeline on a given batch of WAV filepaths and return the detections in a corresponding list. The output is a list of outputs matching the output of
batbot.pipeline(), except the processing is done in batch and is much faster.The final output is a list of lists of dictionaries, each representing a single detection. Each dictionary has a structure with the following keys:
{ 'l': class_label (str) 'c': confidence (float) 'x': x_top_left (float) 'y': y_top_left (float) 'w': width (float) 'h': height (float) }
- Parameters:
filepaths (list) – list of str WAV filepath (relative or absolute)
config (str or None, optional) – the configuration to use. Defaults to
None.classifier_thresh (float or None, optional) – the confidence threshold for the Classifier’s predictions. Defaults to the default configuration setting.
clean (bool, optional) – a flag to clean up any on-disk spectrograms that were generated. Defaults to
True.
- Returns:
corresponding list of classifier scores, corresponding list of lists of predictions
- Return type:
- batbot.__init__.example()[source]
Run the pipeline on an example WAV with the default configuration
- batbot.__init__.fetch(pull=False, config=None)[source]
Fetch the Classifier ONNX model file from a CDN if it does not exist locally.
This function will throw an AssertionError if the download fails or the file otherwise does not exist locally on disk.
- batbot.__init__.parallel_pipeline(in_file_chunks, out_stem_chunks=None, fast_mode=False, force_overwrite=False, num_workers=0, threaded=False, quiet=False, desc=None)[source]
- batbot.__init__.pipeline(filepath, out_file_stem=None, output_folder=None, fast_mode=False, force_overwrite=False, quiet=False, plot_uncompressed_amplitude=False, include_original_sr=False, time_buffer_ms=1.0, debug=False)[source]
Run the ML pipeline on a given WAV filepath and return the classification results
The final output is a list of time windows where a bat exists. Each dictionary has a structure with the following keys:
{ 'l': class_label (str) 'c': confidence (float) 'x': x_top_left (float) 'y': y_top_left (float) 'w': width (float) 'h': height (float) }
- Parameters:
filepath (str) – WAV filepath (relative or absolute)
config (str or None, optional) – the configuration to use. Defaults to
None.classifier_thresh (float or None, optional) – the confidence threshold for the classifier’s predictions. Defaults to the default configuration setting.
clean (bool, optional) – a flag to clean up any on-disk spectrograms that were generated. Defaults to
True.
- Returns:
classifier score, list of time windows
- Return type:
Utilities
BatBot utilities file for common and handy functions.