"""validators used by attrs-based classes and by vak.parse.parse_config"""importpathlibimporttomlkitfrom..importmodelsfrom..commonimportconstants
[docs]defis_a_directory(instance,attribute,value):"""check if given path is a directory"""ifnotpathlib.Path(value).is_dir():raiseNotADirectoryError(f"Value specified for {attribute.name} of {type(instance)} not recognized as a directory:\n"f"{value}")
[docs]defis_a_file(instance,attribute,value):"""check if given path is a file"""ifnotpathlib.Path(value).is_file():raiseFileNotFoundError(f"Value specified for {attribute.name} of {type(instance)} not recognized as a file:\n"f"{value}")
[docs]defis_valid_model_name(instance,attribute,value:str)->None:"""Validate model name."""ifvaluenotinmodels.registry.MODEL_NAMES:raiseValueError(f"Invalid model name: {value}.\nValid model names are: {models.registry.MODEL_NAMES}")
[docs]defis_audio_format(instance,attribute,value):"""Check if valid audio format"""ifvaluenotinconstants.VALID_AUDIO_FORMATS:raiseValueError(f"{value} is not a valid format for audio files")
[docs]defis_annot_format(instance,attribute,value):"""Check if valid annotation format"""ifvaluenotinconstants.VALID_ANNOT_FORMATS:raiseValueError(f"{value} is not a valid format for annotation files.\n"f"Valid formats are: {constants.VALID_ANNOT_FORMATS}")
[docs]defis_spect_format(instance,attribute,value):"""Check if valid format for spectrograms"""ifvaluenotinconstants.VALID_SPECT_FORMATS:raiseValueError(f"{value} is not a valid format for spectrogram files.\n"f"Valid formats are: {constants.VALID_SPECT_FORMATS}")
[docs]defare_tables_valid(config_dict,toml_path=None):"""Validate top-level tables in class:`dict`. This function expects the ``config_dict`` returned by :func:`vak.config.load._load_from_toml_path`. """tables=list(config_dict.keys())from..cli.cliimportCLI_COMMANDS# avoid circular importcli_commands_besides_prep=[commandforcommandinCLI_COMMANDSifcommand!="prep"]tables_that_are_commands_besides_prep=[tablefortableintablesiftableincli_commands_besides_prep]iflen(tables_that_are_commands_besides_prep)==0:raiseValueError("Did not find a table related to a vak command in config besides `prep`.\n"f"Sections in config were: {tables}\n""Please see example toml configuration files here: https://github.com/vocalpy/vak/tree/main/doc/toml")iflen(tables_that_are_commands_besides_prep)>1:raiseValueError("Found multiple tables related to a vak command in config besides `prep`.\n"f"Those tables are: {tables_that_are_commands_besides_prep}. "f"Please use just one command besides `prep` per .toml configuration file.\n""See example toml configuration files here: https://github.com/vocalpy/vak/tree/main/doc/toml")fortableintables:iftablenotinVALID_TOP_LEVEL_TABLES:iftoml_path:err_msg=(f"Top-level table defined in {toml_path} is not valid: {table}\n"f"Valid top-level tables are: {VALID_TOP_LEVEL_TABLES}\n""Please see example toml configuration files here: ""https://github.com/vocalpy/vak/tree/main/doc/toml")else:err_msg=(f"Table defined in toml config is not valid: {table}\n"f"Valid top-level tables are: {VALID_TOP_LEVEL_TABLES}\n""Please see example toml configuration files here: ""https://github.com/vocalpy/vak/tree/main/doc/toml")raiseValueError(err_msg)
[docs]defare_keys_valid(config_dict:dict,table_name:str,toml_path:str|pathlib.Path|None=None,)->None:"""Given a :class:`dict` containing the *entire* configuration loaded from a toml file, validate the key names for a specific top-level table, e.g. ``vak.train`` or ``vak.predict`` """table_keys=set(config_dict[table_name].keys())valid_keys=set(VALID_KEYS[table_name])ifnottable_keys.issubset(valid_keys):invalid_keys=table_keys-valid_keysiftoml_path:err_msg=(f"The following keys from '{table_name}' table in "f"the config file '{toml_path.name}' are not valid:\n{invalid_keys}")else:err_msg=(f"The following keys from '{table_name}' table in "f"the toml config are not valid:\n{invalid_keys}")raiseValueError(err_msg)
[docs]defare_table_keys_valid(table_config_dict:dict,table_name:str,toml_path:str|pathlib.Path|None=None,)->None:"""Given a :class:`dict` containing the configuration for a *specific* top-level table, loaded from a toml file, validate the key names for that table, e.g. ``vak.train`` or ``vak.predict``. This function assumes ``table_config_dict`` comes from the entire ``config_dict`` returned by :func:`vak.config.parse.from_toml_path`, accessed using the table name as a key, unlike :func:`are_keys_valid`. This function is used by the ``from_config_dict`` classmethod of the top-level tables. """table_keys=set(table_config_dict.keys())valid_keys=set(VALID_KEYS[table_name])ifnottable_keys.issubset(valid_keys):invalid_keys=table_keys-valid_keysiftoml_path:err_msg=(f"The following keys from '{table_name}' table in "f"the config file '{toml_path.name}' are not valid:\n{invalid_keys}")else:err_msg=(f"The following keys from '{table_name}' table in "f"the toml config are not valid:\n{invalid_keys}")raiseValueError(err_msg)