spych.utils¶
array¶
-
spych.utils.array.
splice_features
(features, splice_size=0, splice_step=1, repeat_border_frames=True)¶ Splice features in a array. Splicing extends a single feature with right and left context features.
Parameters: - features – A 2D np-array with the shape (nr_of_features x feature_dimension).
- splice_size – Number of context features to use on the right and left. (splice_size = 4 –> resulting feature sizes = 9 * initial featdim)
- splice_step – Number of features to move for the next splice.
- repeat_border_frames – At the begin and end of the feature series the feature matrix has to be padded for splicing. If True it pads with the first/last feature otherwise with zeroes.
Returns: A 2D np-array with the spliced features.
-
spych.utils.array.
unsplice_features
(features, splice_size=0, splice_step=1)¶ Take a array of spliced features and unsplice them. Uses the averaged values for a single feature.
Parameters: - features – A 2D np-array with the spliced features.
- splice_size – Number of right and left context features that were used for splicing.
- splice_step – Step that was used for splicing.
Returns: A 2D np-array with the unspliced features.
math¶
-
spych.utils.math.
calculate_absolute_proportions
(count, proportions={})¶ Given an int and a dictionary with name/float entries, which represent relative proportions. It calculates the absolute proportions.
e.g. input 20, {‘a’:0.5, ‘b’:0.5} –> output {‘a’:10, ‘b’:10}
If it doesn’t come out even, the rest is appended to the different proportions randomly.
Parameters: - count – the total number to divide into proportions
- proportions – dict name/prop_value
Returns: absolute proportions
-
spych.utils.math.
calculate_relative_proportions
(proportions)¶ Converts proportions so they sum to 1.0.
Parameters: proportions – dict name/prop_value Returns: dict name/prop_value
-
spych.utils.math.
try_distribute_values_proportionally
(values, proportions)¶ e.g.
values = {‘a’: 160, ‘b’: 160, ‘c’: 20, ‘d’: 100, ‘e’: 50, ‘f’: 60} proportions = {‘x’: 0.6, ‘y’: 0.2, ‘z’: 0.2}
out: {‘x’ : [a,b], ‘y’ : [c,e]} …
Parameters: - values – value_id/value
- proportions – prop_id/prop
Returns: tuple (prop_id/list of value_ids) (resulting proportions)
text¶
-
spych.utils.text.
remove_punctuation
(text, exceptions=[])¶ Removes the punctuation from a string.
Parameters: - text – Text
- exceptions – Symbols not to remove.
Returns: Text without punctuation.
-
spych.utils.text.
starts_with_prefix_in_list
(text, prefixes)¶ Checks if the given string starts with one of the prefixes in the list.
Parameters: - text – Text
- prefixes – List of prefixes
Returns: True/False
naming¶
-
spych.utils.naming.
generate_name
(length=15, not_in=None)¶ Generates a random string of lowercase letters with the given length.
Parameters: - length – Length of the string to output.
- not_in – Only return a string not in the given iterator.
Returns:
-
spych.utils.naming.
index_name_if_in_list
(name, name_list, suffix='', prefix='')¶ Adds an index to the given name if it already exists in the given list.
Parameters: - name – Name
- name_list – List of names that the new name must differ from.
- suffix – The suffix to append after the index.
- prefix – The prefix to append in front of the index.
Returns: Unique name
jsonfile¶
-
spych.utils.jsonfile.
read_json_file
(path)¶ Reads json file.
Parameters: path – Path to read Returns: Data
-
spych.utils.jsonfile.
write_json_to_file
(path, data)¶ Writes data as json to file.
Parameters: - path – Path to write to
- data – Data
textfile¶
-
spych.utils.textfile.
read_key_value_lines
(path, separator=' ', default_value='')¶ Reads lines of a text file with two columns as key/value dictionary.
Parameters: - path – Path to the file.
- separator – Separator that is used to split key and value.
- default_value – If no value is given this value is used.
Returns: dict
-
spych.utils.textfile.
read_separated_lines
(path, separator=' ', max_columns=-1)¶ Reads a text file where each line represents a record with some separated columns.
Parameters: - path – Path to the file to read.
- separator – Separator that is used to split the columns.
- max_columns – Number of max columns (if the separator occurs within the last column).
Returns: list
-
spych.utils.textfile.
read_separated_lines_generator
(path, separator=' ', max_columns=-1, ignore_lines_starting_with=[])¶ Creates a generator through all lines of a file and returns the splitted line.
Parameters: - path – Path to the file.
- separator – Separator that is used to split the columns.
- max_columns – Number of max columns (if the separator occurs within the last column).
- ignore_lines_starting_with – Lines starting with a string in this list will be ignored.
Returns: generator
-
spych.utils.textfile.
read_separated_lines_with_first_key
(path, separator=' ', max_columns=-1)¶ Reads the separated lines of a file and returns a dictionary with the first column as keys, value is a list with the rest of the columns.
Parameters: - path – Path to the file to read.
- separator – Separator that is used to split the columns.
- max_columns – Number of max columns (if the separator occurs within the last column).
Returns: dict
-
spych.utils.textfile.
write_separated_lines
(path, values, separator=' ', sort_by_column=0)¶ Writes list or dict to file line by line. Dict can have list as value then they written separated on the line.
Parameters: - path – Path to write file to.
- values – Dict or list
- separator – Separator to use between columns.
- sort_by_column – if >= 0, sorts the list by the given index, if its 0 or 1 and its a dictionary it sorts it by either the key (0) or value (1). By default 0, meaning sorted by the first column or the key.