PK!ã&opensoundscape/__init__.py__version__ = "0.3.0" PK!Zh h opensoundscape/config/checks.pyfrom difflib import get_close_matches from importlib.util import find_spec def ini_section_and_keys_exists( default_config, override_config, override_config_filename ): """ Given the parsed INI files Do the sections and keys in the override config exist in the default config? Input: default_config: The unmodified config/opensoundscape.ini INI object override_config: The user modifications made to the opensoundscape configuration override_config_filename: The filename for the override file """ for section in override_config.keys(): # -> First, does the section even exist? if not section in default_config.keys(): close_matches = get_close_matches(section, default_config.keys()) print( f"ERROR: From {override_config_filename}, section '{section}' isn't recognized!" ) if close_matches: print(f"-> did you mean: {' '.join(close_matches)}") exit() # Second, do the keys even exist? for key in override_config[section].keys(): if not key in default_config[section].keys(): close_matches = get_close_matches(key, default_config[section].keys()) print( f"ERROR: From {override_config_filename}, section {section}, key '{key}' isn't recognized!" ) if close_matches: print(f"-> did you mean: {' '.join(close_matches)}") exit() def match_algorithm_exists(config): """ Given the parsed INI files Check if the user wants to use OpenCV. If yes, can we import OpenCV? Make sure that the algorithm they pick exists, potential options: TM_CCOEFF, TM_CCOEFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_SQDIFF, TM_SQDIFF_NORMED Input: config: The opensoundscape configuration """ match_method = config["model_fit"].get("template_match_method") if match_method == "opencv": cv2_spec = find_spec("cv2") if not cv2_spec: raise ImportError( "You have requested `template_match_method = opencv`, but you haven't installed OpenCV?" ) options = [ "TM_CCOEFF", "TM_CCOEFF_NORMED", "TM_CCORR", "TM_CCORR_NORMED", "TM_SQDIFF", "TM_SQDIFF_NORMED", ] algo = config["model_fit"].get("template_match_algorithm") if algo not in options: raise ValueError( f"The template matching algorithm chosen was {algo}, valid options: {' '.join(options)}" ) def config_checks(config): """ Given the parsed INI files The entry point for configuration checks Input: default_config: The unmodified config/opensoundscape.ini INI object override_config: The user modifications made to the opensoundscape configuration override_config_filename: The filename for the override file Output: None Raises: Nothing, but can various functions will exit """ match_algorithm_exists(config) PK!K! opensoundscape/config/config.pyfrom opensoundscape.config.checks import ini_section_and_keys_exists from opensoundscape.config.checks import config_checks from os.path import isfile from configparser import ConfigParser DEFAULT_CONFIG = """ [general] num_processors = db_rw = True db_name = opensoundscape db_uri = localhost:27017 db_sparse = False db_sparse_thresh_percent = 1.0 data_dir = train_file = validate_file = predict_file = [spect_gen] algo = template_matching resample_rate = 22050.0 # Options: kaiser_best, kaiser_fast, scipy resample_type = kaiser_best spectrogram_segment_length = 512 spectrogram_overlap = 75 low_freq_thresh = 173 high_freq_thresh = 10033 decibel_threshold = -100.0 median_filter_factor = 0.25 low_values_filter_percent = 1.5 binary_closing_kernel_height = 6 binary_closing_kernel_width = 10 binary_dilation_kernel_height = 3 binary_dilation_kernel_width = 5 median_filter_kernel_height = 5 median_filter_kernel_width = 3 small_objects_kernel_size = 50 segment_pixel_buffer = 12 [model_fit] algo = template_matching num_frequency_bands = 16 gaussian_filter_sigma = 1.5 template_match_frequency_buffer = 5 template_pool = template_pool_db = n_estimators = 1 max_features = 4 min_samples_split = 3 cross_correlations_only = False species_list = stratification_percent = 33.3 # Options: opencv, cross_corr template_match_method = opencv # Options: TM_CCOEFF, TM_CCOEFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_SQDIFF, TM_SQDIFF_NORMED template_match_algorithm = TM_CCORR_NORMED only_match_if_detected_boxes = False [predict] algo = template_matching """ def generate_config(arguments, store_options=True): """Generate the configuration Simply return a ConfigParser for opensoundscape. We have a default config in `config/` as well as a potential override file (arguments["--ini"]). Access elements via `config[
].get{float,boolean,int}('key')`. Args: arguments: The docopt arguments to store Returns: A ConfigParser instance Raises: FileNotFoundError if INI file doesn't exist """ f_override = arguments["--ini"] if not isfile(f_override): raise FileNotFoundError(f"{f_override} doesn't exist!") config = ConfigParser(allow_no_value=True) config.read_string(DEFAULT_CONFIG) override_config = ConfigParser() override_config.read(f_override) # Check that the override config makes sense, then read it ini_section_and_keys_exists(config, override_config, f_override) config.read(f_override) config_checks(config) if store_options: config["docopt"] = {} config["docopt"]["label"] = arguments["