def print_probabilities(self): """ Prints a copy of the current probabilities. Used for convenient checking in a command line environment. For dictionaries containing the raw values, use the `p_*` attributes. :return: """ # create copies to avoid editing p_initial = copy.deepcopy(self.p_initial) p_emission = copy.deepcopy(self.p_emission) p_transition = copy.deepcopy(self.p_transition) # convert to nested lists for clean printing p_initial = [[str(s)] + [str(round(p_initial[s], 3))] for s in self.states] p_emission = [ [str(s)] + [str(round(p_emission[s][e], 3)) for e in self.emissions] for s in self.states ] p_transition = [ [str(s1)] + [str(round(p_transition[s1][s2], 3)) for s2 in self.states] for s1 in self.states ] p_initial.insert(0, ["S_i", "Y_0"]) p_emission.insert(0, ["S_i \\ E_j"] + [str(e) for e in self.emissions]) p_transition.insert(0, ["S_i \\ E_j"] + [str(s) for s in self.states]) # format tables ti = terminaltables.DoubleTable(p_initial, "Starting state probabilities") te = terminaltables.DoubleTable(p_emission, "Emission probabilities") tt = terminaltables.DoubleTable(p_transition, "Transition probabilities") te.padding_left = 1 te.padding_right = 1 tt.padding_left = 1 tt.padding_right = 1 te.justify_columns[0] = "right" tt.justify_columns[0] = "right" # print tables print("\n") print(ti.table) print("\n") print(te.table) print("\n") print(tt.table) print("\n") # return None
def print_fit_parameters(self): """ Prints a copy of the current state counts. Used for convenient checking in a command line environment. For dictionaries containing the raw values, use the `n_*` attributes. :return: """ # create copies to avoid editing n_initial = copy.deepcopy(self.n_initial) n_emission = copy.deepcopy(self.n_emission) n_transition = copy.deepcopy(self.n_transition) # make nested lists for clean printing initial = [[str(s)] + [str(n_initial[s])] for s in self.states] initial.insert(0, ["S_i", "Y_0"]) emissions = [[str(s)] + [str(n_emission[s][e]) for e in self.emissions] for s in self.states] emissions.insert(0, ["S_i \\ E_i"] + list(map(str, self.emissions))) transitions = [[str(s1)] + [str(n_transition[s1][s2]) for s2 in self.states] for s1 in self.states] transitions.insert(0, ["S_i \\ S_j"] + list(map(lambda x: str(x), self.states))) # format tables ti = terminaltables.DoubleTable(initial, "Starting state counts") te = terminaltables.DoubleTable(emissions, "Emission counts") tt = terminaltables.DoubleTable(transitions, "Transition counts") ti.padding_left = 1 ti.padding_right = 1 te.padding_left = 1 te.padding_right = 1 tt.padding_left = 1 tt.padding_right = 1 ti.justify_columns[0] = "right" te.justify_columns[0] = "right" tt.justify_columns[0] = "right" # print tables print("\n") print(ti.table) print("\n") print(te.table) print("\n") print(tt.table) print("\n") # return None
def createCustomTable(text): dictTable = frecuencias.frecuenciaLetras(text) dictTableKeys = list(dictTable.keys()) dictTableValues = list(dictTable.values()) data = [] data.append(['Processed Char', 'Average count']) for n in range(len(dictTable)): inputNumber = str(n+1) + ".- " data.append([(inputNumber + str(dictTableKeys[n])), str(dictTableValues[n])]) table = terminaltables.DoubleTable(data) table.inner_row_border = True table.inner_column_border = False table.title = "Tabla de frecuencias" return table.table
def createSampleTable(): dictTable = frecuencias.frecuencias() dictTableKeys = list(dictTable.keys()) dictTableValues = list(dictTable.values()) data = [] data.append(['Processed Char:', 'Average count:']) for n in range(26): inputNumber = str(n+1) + ".- " data.append([(inputNumber + str(dictTableKeys[n])), str(dictTableValues[n])]) table = terminaltables.DoubleTable(data) table.title = "Tabla de Frecuencias" table.inner_row_border = True table.inner_column_border = False table.justify_columns[0] = 'center' return table.table
def createJiTable(dictionary): dictTable = dictionary dictTableKeys = list(dictTable.keys()) dictTableValues = list(dictTable.values()) data = [] data.append(["Swap index", "JI"]) for n in range(len(dictTable)): inputNumber = str(n+1) + ".- " data.append([(inputNumber + str(dictTableKeys[n])), str(dictTableValues[n])]) table = terminaltables.DoubleTable(data) table.title = "JI Cuadrada" table.inner_row_border = True table.inner_column_border = False table.justify_columns[0] = 'center' return table.table
def prettify_details(data): """Make data more readable.""" new = [] if "terminaltables" in sys.modules: for key, value in data.items(): if key.startswith("__"): continue if isinstance(value, (int, float)) and not isinstance(value, bool): new.append((key, "{:15,.2f}".format(value))) else: new.append((key, value)) table = terminaltables.DoubleTable(new) table.inner_heading_row_border = False table.justify_columns[1] = 'right' return table.table.replace("\n", "<br />") else: formatted = json.dumps( {k: v for k, v in data.items() if not k.startswith("__")}, indent=4) new = formatted[2:-2].replace("\n", "<br />") return new
def _set_style(self, style): if hasattr(self, '_grid'): data = self._grid.table_data else: data = [] if style.lower().startswith('a'): self._style = 'ascii' self._grid = tt.AsciiTable(data) elif style.lower().startswith('d'): self._style = 'double' self._grid = tt.DoubleTable(data) elif style.lower().startswith('s'): self._style = 'single' self._grid = tt.SingleTable(data) elif style.lower()[0] in ['g', 'm']: self._style = 'markdown' self._grid = tt.GithubFlavoredMarkdownTable(data) self._grid.inner_heading_row_border = True self._grid.padding_left = 3 self._grid.padding_right = 3 self._grid.justify_columns = { col: 'center' for col in range(self.ncols) }
def get_pretty_table_from_dict(attributes_titles, dictionary, title, attributes_to_print_order, table_type='AsciiTable'): """Function convert dictionary to pretty table. :param attributes_titles: list, headers of table, must be bigger by one then attributes_to_print :param dictionary: dict, dictionary in next format: (for example legendary boxers table :) ) { "Mike Tyson": { "Total_fights": 58, "Wins": 50, ... }, "Muhammad Ali": { "Total fights": 61, "Wins": 56, ... }, ... } In this way dictionary has string as keys and dict as values. Every value as dict must have similar keys which must be written in keys rules which set order of table columns :param title: title of table :param attributes_to_print_order: list, list of attributes which set order of cols in tables :param table_type: type of table from terminaltables in terminaltables==3.1.0 have 3 variants with title: AsciiTable, DoubleTable, SingleTable For the more information please read terminaltables documentation :return: str, pretty table for print, for example is: # table_type == 'AsciiTable' # attributes_titles == ["Boxer name", "Total fights", "Wins"] # attributes_to_print_order == ["Total_fights", "Wins"] # title == "Legendary Boxers" +Legendary Boxers-------+----------------------+----------------------+----------------------+ | Boxer name | Total fights | Wins | ... | +-----------------------+----------------------+----------------------+----------------------+ | Muhammad Ali | 61 | 56 | ... | | Mike Tyson | 58 | 50 | ... | | ... | ... | ... | ... | +-----------------------+----------------------+----------------------+----------------------+ """ if not (len(attributes_titles) - len(attributes_to_print_order) == 1): raise AttributeError( 'lenght of header must be bigger for one then len keys_rules') for key, value in dictionary.items(): subdict_keys = set(value.keys()) if subdict_keys.difference(set(attributes_to_print_order)): raise AttributeError( 'keys_rules must be equal subdicts in dictionary') valid_table_names = ['AsciiTable', 'DoubleTable', 'SingleTable'] if table_type not in valid_table_names: raise AttributeError('unknown table type, set one of: {}'.format( ', '.join(valid_table_names))) table = [attributes_titles] for d_key, d_value in dictionary.items(): raw = [d_key] for key in attributes_to_print_order: # user can use list or other structures raw.append(str(d_value[key])) table.append(raw) if table_type == 'AsciiTable': table_instance = terminaltables.AsciiTable(table, title) if table_type == 'DoubleTable': table_instance = terminaltables.DoubleTable(table, title) if table_type == 'SingleTable': table_instance = terminaltables.SingleTable(table, title) return table_instance.table