UnCodeGL package¶
Subpackages¶
Submodules¶
UnCodeGL.code module¶
-
class
UnCodeGL.code.Code(code_dictionary)¶ Bases:
objectCore class for Coding and Decoding. It has integrated core metrics to it.
To create a new Code you need to pass the rule for the code as a dictionary.
-
arity¶ An integer representing the code arity
-
code_alphabet¶ A set containing the code alphabet
-
source_alphabet¶ A set containing the source alphabet
-
decode(sentence, get_any=True)¶ Decodes a given sentence, in case multiple outputs are possible it returns any of them if the get_any flag is True.
- Parameters
sentence – Sentence to be decoded. It must compel with the source alphabet.
get_any – True by default. It gives a string when multiple answers are possible. When False it gives the answer in a set of possible answers.
- Returns
A string with the decoded sentence
-
encode(sentence)¶ “Encodes a given sentence
-
get_C_n()¶ Returns C_n related to the uniquely decodable algorithm.
- Returns
List of set of words.
-
get_arity()¶ Returns arity of the code
- Returns
Integer value of the arity
-
get_code_trie()¶ Returns a Trie data structure from the words in the code
-
get_word_length(source)¶ Gets the average word lenght of the code for a given Source
- Parameters
source – A source of type Source
- Returns
A float with the value of the word lenght of the code for the given source.
-
is_prefix()¶ Uses a Trie data structure to determine if the code is prefix
- Returns
A boolean True if the code is prefix, False otherwise
-
is_uniquely_decodable()¶ Returns boolean specifying whether or not is uniquely decodable
For the code to be uniquely decodable means that the function code that takes a string from a the source and codifies it is inyective.
- Returns
True if the code is uniquely decodable, False otherwise
-
static
kraft_inequality_metric(words_length, arity)¶ Calculates Kraft Inequality for given word lengths
Measures Kraft Inequality Metric defined as
\[\sum_{i} r^{-l_i}\]where r means arity, and l sub i the length of the ith word
Also returns if an instantaneous code can be made with said words
Typical Usage:
>>> metric, can_be_inst = kraft_inequality_metric(words_length=[4,4,3,6],arity=2) >>> print(metric, can_be_inst) 0.265625 True
- Parameters
words_length – An list with the length of the words
arity – Arity of the code
- Returns
metric , can_be_instantaneous. Kraft Inequality metric. A boolean refering to whether or not a code with this word lenghts and arity exists. This is dependant on the metric being less than 1
-
static
make_huffman_code(source, code_alphabet={'0', '1'})¶ Generates an optimal Huffman Code
- Parameters
source – A source of type Source
code_alphabet – Iterable with the alphabet. The items should be of type str
- Returns
Optimal huffman code for the given source and with arity of the given code_alphabet
-
-
UnCodeGL.code.validate_dictionary(code_dictionary)¶ Validates that a given dictionary is good to use as a code
UnCodeGL.source module¶
-
class
UnCodeGL.source.Source(*args, **kwargs)¶ Bases:
objectClass for Source. Contains a dictionary with the probabilities of each simbol it can generate
-
dictionary¶ Dictionary of probabilities. The keys are the source simbols and the values the probabilities
Initialization can be done through dictionary or base_string but not both
Can be initiated through a given dictionary that contains the simbols and the probabilities that each symbol occurs.
It can also be initiated with a base string that will count appearances and obtain probabilities.
Either dictionary or base_string must be passed, not both
- Parameters
dictionary – A dictionary with the simbols and probabilities. The sum of the probabilities must be 1.
base_string – A string of the usual output of the source. It will be assumed that is a memoryless source
-
get_alphabet()¶ Returns list with simbols of the source
-
get_entropy()¶ Calculates entropy of the source
Entropy is given by
\[-\sum_{i} p(x_i)\log(p(x_i))\]- Returns
Float with the value of the entropy.
-
get_simbol_prob()¶ Returns probability for a simbol
-
-
UnCodeGL.source.make_dictionary_from_str(base_string)¶ Makes a dictionary with probabilities normalized
-
UnCodeGL.source.verify_dictionary(dictionary)¶ Verify the sum of the probabilites is 1 to an epsilon