Shortcuts

asteroid.utils package

asteroid.utils.prepare_parser_from_dict(dic, parser=None)[source]

Prepare an argparser from a dictionary.

Parameters:
  • dic (dict) – Two-level config dictionary with unique bottom-level keys.
  • parser (argparse.ArgumentParser, optional) – If a parser already exists, add the keys from the dictionary on the top of it.
Returns:

argparse.ArgumentParser – Parser instance with groups corresponding to the first level keys and arguments corresponding to the second level keys with default values given by the values.

asteroid.utils.parse_args_as_dict(parser, return_plain_args=False, args=None)[source]

Get a dict of dicts out of process parser.parse_args()

Top-level keys corresponding to groups and bottom-level keys corresponding to arguments. Under ‘main_args’, the arguments which don’t belong to a argparse group (i.e main arguments defined before parsing from a dict) can be found.

Parameters:
  • parser (argparse.ArgumentParser) – ArgumentParser instance containing groups. Output of prepare_parser_from_dict.
  • return_plain_args (bool) – Whether to return the output or parser.parse_args().
  • args (list) – List of arguments as read from the command line. Used for unit testing.
Returns:

dict – Dictionary of dictionaries containing the arguments. Optionally the direct output parser.parse_args().

asteroid.utils.str_int_float(value)[source]

Type to convert strings to int, float (in this order) if possible.

Parameters:value (str) – Value to convert.
Returns:int, float, str – Converted value.
asteroid.utils.str2bool(value)[source]

Type to convert strings to Boolean (returns input if not boolean)

asteroid.utils.str2bool_arg(value)[source]

Argparse type to convert strings to Boolean

asteroid.utils.isfloat(value)[source]

Computes whether value can be cast to a float.

Parameters:value (str) – Value to check.
Returns:bool – Whether value can be cast to a float.
asteroid.utils.isint(value)[source]

Computes whether value can be cast to an int

Parameters:value (str) – Value to check.
Returns:bool – Whether value can be cast to an int.
asteroid.utils.tensors_to_device(tensors, device)[source]

Transfer tensor, dict or list of tensors to device.

Parameters:
  • tensors (torch.Tensor) – May be a single, a list or a dictionary of tensors.
  • ( (device) – class: torch.device): the device where to place the tensors.
Returns:

Union [torch.Tensor, list, tuple, dict] – Same as input but transferred to device. Goes through lists and dicts and transfers the torch.Tensor to device. Leaves the rest untouched.

asteroid.utils.to_cuda(tensors)[source]

Transfer tensor, dict or list of tensors to GPU.

Parameters:tensors (torch.Tensor, list or dict) – May be a single, a list or a dictionary of tensors.
Returns:torch.Tensor – Same as input but transferred to cuda. Goes through lists and dicts and transfers the torch.Tensor to cuda. Leaves the rest untouched.
asteroid.utils.has_arg(fn, name)[source]

Checks if a callable accepts a given keyword argument.

Parameters:
  • fn (callable) – Callable to inspect.
  • name (str) – Check if fn can be called with name as a keyword argument.
Returns:

bool – whether fn accepts a name keyword argument.

asteroid.utils.flatten_dict(d, parent_key='', sep='_')[source]

Flattens a dictionary into a single-level dictionary while preserving parent keys. Taken from https://stackoverflow.com/questions/6027558/ flatten-nested-dictionaries-compressing-keys?answertab=votes#tab-top

Parameters:
  • d (MutableMapping) – Dictionary to be flattened.
  • parent_key (str) – String to use as a prefix to all subsequent keys.
  • sep (str) – String to use as a separator between two key levels.
Returns:

dict – Single-level dictionary, flattened.

asteroid.utils.average_arrays_in_dic(dic)[source]

Take average of numpy arrays in a dictionary.

Parameters:dic (dict) – Input dictionary to take average from
Returns:dict – New dictionary with array averaged.
asteroid.utils.get_wav_random_start_stop(signal_len, desired_len=32000)[source]

Get indexes for a chunk of signal of a given length.

Parameters:
  • signal_len (int) – length of the signal to trim.
  • desired_len (int) – the length of [start:stop]
Returns:

tuple – random start integer, stop integer.

Read the Docs v: v0.3.3
Versions
latest
stable
v0.3.3
v0.3.2
v0.3.1
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.