def make_record_comparison_table(records, args_to_show=None, results_extractor = None, print_table = False): """ Make a table comparing the arguments and results of different experiment records. You can use the output of this function with the tabulate package to make a nice readable table. :param records: A list of records whose results to compare :param args_to_show: A list of arguments to show. If none, it will just show all arguments that differ between experiments. :param results_extractor: A dict<str->callable> where the callables take the result of the experiment as an argument and return an entry in the table. :param print_table: Optionally, import tabulate and print the table here and now. :return: headers, rows headers is a list of of headers for the top of the table rows is a list of lists filling in the information. example usage: headers, rows = make_record_comparison_table( record_ids = [experiment_id_to_latest_record_id(eid) for eid in [ 'demo_fast_weight_mlp.multilayer_baseline.1epoch.version=mlp', 'demo_fast_weight_mlp.multilayer_baseline.1epoch.full-gd.n_steps=1', 'demo_fast_weight_mlp.multilayer_baseline.1epoch.full-gd.n_steps=20', ]], results_extractor={ 'Test': lambda result: result.get_best('test').score.get_score('test'), 'Train': lambda result: result.get_best('test').score.get_score('train'), } ) import tabulate print tabulate.tabulate(rows, headers=headers, tablefmt=tablefmt) """ args = [rec.info.get_field(ExpInfoFields.ARGS) for rec in records] if args_to_show is None: common, separate = separate_common_items(args) args_to_show = [k for k, v in separate[0]] if results_extractor is None: results_extractor = {'Result': str} elif callable(results_extractor): results_extractor = {'Result': results_extractor} else: assert isinstance(results_extractor, dict) headers = args_to_show + results_extractor.keys() rows = [] for record, record_args in izip_equal(records, args): arg_dict = dict(record_args) args_vals = [arg_dict[k] for k in args_to_show] results = record.get_result() rows.append(args_vals+[f(results) for f in results_extractor.values()]) if print_table: import tabulate print tabulate.tabulate(rows, headers=headers, tablefmt='simple') return headers, rows
def convergence(E_u, E_p, N, dt): print N, dt if len(N) > len(dt): check = N opp = dt else: check = dt opp = N if check == N: print print "#################################### - ERROR/CON SPACE - ####################################\n" else: print print "#################################### - ERROR/CON TIME - ####################################\n" time = [i for i in range(len(E_u))] for E in [E_u, E_p]: print print "#################################### - L2 NORM - ####################################\n" table = [] headers = ["N" if opp is N else "dt"] #headers = ["N"] li = [] li.append(str(opp[0])) for i in range(len(E)): li.append("%e" % E[i]) table.append(li) print check for i in range(len(check)): headers.append("dt = %g" % check[i] if check is dt else "N = %g" % check[i]) print tabulate.tabulate(table, headers, tablefmt="fancy_grid") print print print "############################### - CONVERGENCE RATE - ###############################\n" table = [] headers = ["N" if opp is N else "dt"] #for i in range(len(N)): li = [] li.append(str(opp[0])) for i in range(len(E) - 1): error = E[(i + 1)] / E[i] h_ = check[(i + 1)] / check[i] conv = np.log(error) / np.log( h_) #h is determined in main solve method li.append(conv) table.append(li) for i in range(len(check) - 1): headers.append("%g to %g" % (check[i], check[i + 1])) print tabulate.tabulate(table, headers, tablefmt="fancy_grid")
def convergence(E_u, E_p, N, dt): check = N if len(N) >= len(dt) else dt opp = dt if check == N else N if check == N: print print "#################################### - ERROR/CON SPACE - ####################################\n" else: print print "#################################### - ERROR/CON TIME - ####################################\n" time = [i for i in range(len(E_u))] for E in [E_u, E_p]: print print "#################################### - L2 NORM - ####################################\n" table = [] headers = ["N" if opp is N else "dt"] #headers = ["N"] for i in range(len(opp)): li = [] li.append(str(opp[i])) for t in range(len(check)): li.append("%e" % E[i*len(check) + t]) li.append("%e" % time[i*len(check) + t]) #SJEKKKKK!! table.append(li) for i in range(len(check)): headers.append("dt = %.g" % check[i] if check is dt else "N = %g" % check[i]) headers.append("Runtime") print tabulate.tabulate(table, headers, tablefmt="fancy_grid") print print print "############################### - CONVERGENCE RATE - ###############################\n" table = [] headers = ["N" if opp is N else "dt"] #for i in range(len(N)): for n in range(len(opp)): li = [] li.append(str(opp[n])) for i in range(len(check)-1): #conv = np.log(E[i+1]/E[i])/np.log(check[i+1]/check[i]) error = E[n*len(check) + (i+1)] / E[n*len(check) + i] h_ = check[n*len(check) + (i+1)] / check[n*len(check) + i] conv = np.log(error)/np.log(h_) #h is determined in main solve method li.append(conv) table.append(li) for i in range(len(check)-1): headers.append("%g to %g" % (check[i], check[i+1])) print tabulate.tabulate(table, headers, tablefmt="fancy_grid")
def make_record_comparison_table(records, args_to_show=None, results_extractor = None, print_table = False, tablefmt='simple', reorder_by_args=False): """ Make a table comparing the arguments and results of different experiment records. You can use the output of this function with the tabulate package to make a nice readable table. :param records: A list of records whose results to compare :param args_to_show: A list of arguments to show. If none, it will just show all arguments that differ between experiments. :param results_extractor: A dict<str->callable> where the callables take the result of the experiment as an argument and return an entry in the table. :param print_table: Optionally, import tabulate and print the table here and now. :return: headers, rows headers is a list of of headers for the top of the table rows is a list of lists filling in the information. example usage: headers, rows = make_record_comparison_table( record_ids = [experiment_id_to_latest_record_id(eid) for eid in [ 'demo_fast_weight_mlp.multilayer_baseline.1epoch.version=mlp', 'demo_fast_weight_mlp.multilayer_baseline.1epoch.full-gd.n_steps=1', 'demo_fast_weight_mlp.multilayer_baseline.1epoch.full-gd.n_steps=20', ]], results_extractor={ 'Test': lambda result: result.get_best('test').score.get_score('test'), 'Train': lambda result: result.get_best('test').score.get_score('train'), } ) import tabulate print tabulate.tabulate(rows, headers=headers, tablefmt=tablefmt) """ args = [rec.get_args(ExpInfoFields.ARGS) for rec in records] if args_to_show is None: common, separate = separate_common_items(args) args_to_show = [k for k, v in separate[0]] if results_extractor is None: results_extractor = {'Result': str} elif callable(results_extractor): results_extractor = {'Result': results_extractor} else: assert isinstance(results_extractor, dict) headers = args_to_show + results_extractor.keys() rows = [] for record, record_args in izip_equal(records, args): arg_dict = dict(record_args) args_vals = [arg_dict[k] for k in args_to_show] results = record.get_result() rows.append(args_vals+[f(results) for f in results_extractor.values()]) if reorder_by_args: rows = sorted(rows) if print_table: import tabulate print(tabulate.tabulate(rows, headers=headers, tablefmt=tablefmt)) return headers, rows
def show_top_expensive_articles_in_stock(self, num_articles, api): self.report("show top expensive in stock") stock_list = self.get_stock_as_array(api=self.api) table_data = [] total_price = 0 for article in stock_list: name = article['product']['enName'] expansion = article.get('product').get('expansion') foil = article.get('isFoil') playset = article.get('isPlayset') language_code = article.get('language') language_name = language_code.get('languageName') price = article.get('price') table_data.append([ name, expansion, u'\u2713' if foil else '', u'\u2713' if playset else '', language_name if language_code != 1 else '', price ]) total_price += price if len(stock_list) > 0: print('Top {} most expensive articles in stock:\n'.format( str(num_articles))) print( tb.tabulate(sorted(table_data, key=lambda x: x[5], reverse=True)[:num_articles], headers=[ 'Name', 'Expansion', 'Foil', 'Playset', 'Language', 'Price' ], tablefmt="simple")) print('\nTotal stock value: {}'.format(str(total_price))) return None
def show_F(h_sequence, v_sequence, data, hide_zeros=False, nonzero_val=None): rows = [] col_headers = [c.decode('UTF-8') for c in h_sequence.values] row_headers = [c.decode('UTF-8') for c in v_sequence.values] pad_headers: Union[bool, Any] = data.shape == (len(row_headers) + 1, len(col_headers) + 1) if pad_headers: row_headers = [" "] + row_headers col_headers = [" "] + col_headers for h, d in zip(row_headers, data): current_row = ["<b>%s</b>" % h] for e in d: if e == 0: if hide_zeros: current_row.append('') else: current_row.append(0) else: if nonzero_val is not None: current_row.append(nonzero_val) else: current_row.append(e) rows.append(current_row) return tabulate.tabulate(rows, headers=col_headers, tablefmt='html')
def threeparttable(array, headers=None, label=None, caption='', floatfmt=".2f", after='', sideways=False): if run_from_ipython() and need_latex(): table = tabulate.tabulate(array, headers=headers, tablefmt='latex_raw', numalign='center', stralign='center', floatfmt=floatfmt) if sideways: env = 'sidewaystable' else: env = 'table' strlatex = r""" \begin{%s} \centering \begin{threeparttable} \caption{%s\label{tab:%s}} %%\begin{adjustbox}{max width=\textwidth} %s %%\end{adjustbox} \begin{tablenotes} %s \end{tablenotes} \end{threeparttable} \end{%s}""" % (env, caption, label, table, after, env) __tables__.val[label] = __tabcount__.val __tabcount__.val += 1 return display(Latex(strlatex))
def table_no_caption(array, headers=None, floatfmt=".2f"): if run_from_ipython() and not need_latex(): table = tabulate.tabulate(array, headers=headers, tablefmt='html', numalign='center', stralign='center', floatfmt=floatfmt) return str(table) elif run_from_ipython() and need_latex(): table = tabulate.tabulate(array, headers=headers, tablefmt='latex_raw', numalign='center', stralign='center', floatfmt=floatfmt) return str(table)
def draw_price_changes_table(self, sorted_best): print( tb.tabulate([[ item['count'], item['name'], u'\u2713' if item['foil'] else '', u'\u2713' if item['playset'] else '', item['old_price'], item['price'], item['price_diff'] ] for item in sorted_best], headers=[ 'Count', 'Name', 'Foil', 'Playset', 'Old price', 'New price', 'Diff' ], tablefmt="simple"))
def print_product_top_list(self, title_string, table_data, sort_column, rows): print(70 * '-') print('{} \n'.format(title_string)) print( tb.tabulate( sorted(table_data, key=lambda x: x[sort_column], reverse=False)[:rows], headers=['Username', 'Country', 'Condition', 'Count', 'Price'], tablefmt="simple")) print(70 * '-') print( 'Total average price: {}, Total median price: {}, Total # of articles: {}\n' .format(str(PyMkmHelper.calculate_average(table_data, 3, 4)), str(PyMkmHelper.calculate_median(table_data, 3, 4)), str(len(table_data))))
def printTable(cls): i = 1 new_list = [] alphabet = [ {' ': 'A'}, {' ': 'B'}, {' ': 'C'}, {' ': 'D'}, {' ': 'E'}, {' ': 'F'}, {' ': 'G'}, {' ': 'H'}, {' ': 'I'}, {' ': 'J'}, {' ': 'K'}, {' ': 'L'}, {' ': 'M'}, {' ': 'N'}, {' ': 'O'}, {' ': 'P'} ] # for i in range(16): # new_list.append(alphabet[i]) # i = i + 1 # for seat in seat_list: # i = i + 1 # if seat['status'] == "available": # seat['status'] = 'x' # new_list.append(seat) header = seat_list[0].keys() rows = [x.values() for x in seat_list] print("\nSeating Chart") print (tabulate.tabulate(rows, header, tablefmt='github'))
def fmt_htmltable(data, fields, options=None): from functools import partial import tabulate # tabulate uses namedtuples, so we're gonna monkeypatch it # tabulate should probably use a dataclass # (tag, unsafe, styles, values, colwidths, colaligns) fmt = tabulate._table_formats['html'] fmt = fmt._replace(headerrow=partial(_html_row, HTML.TH, not options.get( 'htmlsafe', True), options.get('classes'), options.get('styles'))) fmt = fmt._replace( datarow=partial(_html_row, HTML.TD, not options.get('htmlsafe', True), options.get('classes'), options.get('styles'))) tabulate._table_formats['html'] = fmt kwargs = {'headers': fields, 'tablefmt': 'html'} if options: kwargs.update({ x: options[x] for x in options if x not in ['type', 'htmlsafe', 'classes', 'styles'] }) return tabulate.tabulate([[r[_] for _ in r] for r in data], **kwargs)
def main(): # Atribuindo valores aos Parâmetros tabR_name = input("Nome do arquivo da tabela R externa: ") tabS_name = input("Nome do arquivo da tabela S interna: ") buff_size = int(input("Tamanho do Buffer em Bytes: ")) page_size = int(input("Tamanho da Página em Bytes: ")) # Abre os arquivos das Tabelas e verifica se foram encontrados # A função open recebe como parâmetro o tamanho em Bytes # de blocos que devem ser lido try: tabR = open(tabR_name, "r", page_size) tabS = open(tabS_name, "r", page_size) except IOError: print("Erro - Arquivo não encontrado") exit() # Informações do cabeçalho # Calcula o tamanho inteiro de registros que # cabem em um tamanho de buffer infoR = head_register(tabR_name) infoS = head_register(tabS_name) regR_size = infoR[0] regS_size = infoS[0] buff_ext_size = round(buff_size / regR_size) * regR_size buff_int_size = round(buff_size / regS_size) * regS_size # Calcula o número de leitura necessária num_read_ext = 1 + m.floor(arq_size(tabR_name) / buff_ext_size) num_read_int = 1 + m.floor(arq_size(tabS_name) / buff_int_size) #Calculos dos fatores fatores_f = [] fatores_f.append(buff_size / page_size) fatores_f.append(round(page_size / register_size(tabR_name), 2)) fatores_f.append(round(page_size / register_size(tabS_name), 2)) out_tab = [] # Laço externo st_time = time.time() for i in range(num_read_ext): # Lê o buffer buff_ext = tabR.read(buff_ext_size) # Tranforma em Matriz mat_ext = convert_buffer_to_mat(buff_ext) # Laço interno for t in range(num_read_int): # Lê o buffer buff_int = tabS.read(buff_int_size) # Tranforma em Matriz mat_int = convert_buffer_to_mat(buff_int) # Executa a comparação for x in mat_ext: for y in mat_int: if (x[3] == y[0]): out_tab.append(join_reg(x, y)) # Volta ai inicio da Tabela interna tabS.seek(0, 0) end_time = time.time() time_total = end_time - st_time fatores_f.append(round(time_total, 4)) # Resultados print(tabulate(out_tab, ['cod', 'first_name', 'last_name', 'dep'])) head = [ 'Paginas no Buffer', 'Fator de Blocagem R', 'Fator de Blocagem S', 'Tempo de Execução' ] print("----------------------------------------------------------------") print(head) print(fatores_f)
def print(cls): header = dataset[0].keys() rows = [x.values() for x in dataset] print (tabulate.tabulate(rows, header, tablefmt='github')) print("\n\n")
def __to_table__(self, infile): reader = csv.reader(infile, delimiter='\t') headers = reader.next() return tabulate(reader, headers, tablefmt="orgtbl")
def results(Lift, Drag, Time, nel, ndof, v_deg, p_deg, cfd): if cfd != "cfd3": print "#################################### - Numerical Results - ####################################\n" table = [] headers = ["Element", "nel", "ndof", "drag", "lift"] ## wrong order nel ndof ## should be ndof nel in print #headers = ["N"] li = [] li.append("P%d - P%d" % (v_deg, p_deg)) li.append(nel) li.append(ndof) li.append(Drag[-1]) li.append(Lift[-1]) table.append(li) string = tabulate.tabulate(table, headers) print string string = tabulate.tabulate(table, headers) text_file = open("./results/" + cfd + "/Results.txt", "w") text_file.write(string) text_file.close() else: print "#################################### - Numerical Results - ####################################\n" top = [] count = 0 for i in range(1, len(Lift) - 1): if Lift[i - 1] < Lift[i] > Lift[i + 1] and count < 2: top.append(Lift[i]) count += 1 print top Mean_lift = 0.5 * (max(Lift) + min(Lift)) Mean_drag = 0.5 * (max(Drag) + min(Drag)) Amp_lift = 0.5 * (max(Lift) - min(Lift)) Amp_drag = 0.5 * (max(Drag) - min(Drag)) table = [] headers = ["Element", "nel", "ndof", "drag", "lift"] #headers = ["N"] li = [] li.append("P%d - P%d" % (v_deg, p_deg)) li.append(nel) li.append(ndof) li.append("%g +/- %g [%g]" % (Mean_drag, Amp_drag, 1)) li.append("%g +/- %g [%g]" % (Mean_lift, Amp_lift, 1)) table.append(li) string = tabulate.tabulate(table, headers) print string string = tabulate.tabulate(table, headers) text_file = open("./results/" + cfd + "/Results.txt", "w") text_file.write(string) text_file.close() plt.figure(1) plt.plot(Time, Lift, label="Drag") plt.legend() plt.savefig("./results/" + cfd + "/Lift.png") plt.figure(2) plt.plot(Time, Drag, label="Drag") plt.legend() plt.savefig("./results/" + cfd + "/Drag.png")
def csv_to_string(filename): return tabulate(read_csv(filename), headers="firstrow", tablefmt="rst")
def clean_purchased_from_wantslists(self, api): self.report("clean wantslists") print( "This will show items in your wantslists you have already received." ) print( "Hold on, fetching wantslists and received orders (this can be slow)..." ) wantslists_lists = [] try: result = api.get_wantslists() wantslists = { i['idWantslist']: i['name'] for i in result['wantslist'] if i['game']['idGame'] == 1 } wantslists_lists = { k: api.get_wantslist_items(k)['wantslist']['item'] for k, v in wantslists.items() } received_orders = api.get_orders('buyer', 'received', start=1) except Exception as err: print(err) if wantslists_lists and received_orders: purchased_product_ids = [] purchased_products = [] for order in received_orders: #TODO: foil in purchase removes non-foil in wants purchased_product_ids.extend( [i['idProduct'] for i in order.get('article')]) purchased_products.extend( { 'id': i['idProduct'], 'foil': i.get('isFoil'), 'count': i['count'], 'date': order['state']['dateReceived'] } for i in order.get('article')) purchased_products = sorted(purchased_products, key=lambda t: t['date'], reverse=True) matches = [] #TODO: add a progress bar? double? for key, articles in wantslists_lists.items(): for article in articles: a_type = article.get('type') a_foil = article.get('isFoil') == True product_matches = [] if (a_type == 'metaproduct'): metaproduct = api.get_metaproduct( article.get('idMetaproduct')) metaproduct_product_ids = [ i['idProduct'] for i in metaproduct['product'] ] product_matches = [ i for i in purchased_products if i['id'] in metaproduct_product_ids and i['foil'] == a_foil ] else: a_product_id = article.get('idProduct') product_matches = [ i for i in purchased_products if i['id'] == a_product_id and i['foil'] == a_foil ] if product_matches: match = { 'wantlist_id': key, 'wantlist_name': wantslists[key], 'date': product_matches[0]['date'], 'is_foil': a_foil, # FIXME count if more than 1 entry 'count': product_matches[0]['count'] } if a_type == 'product': match.update({ 'product_id': a_product_id, 'product_name': article.get('product').get('enName'), 'expansion_name': article.get('product').get('expansionName'), }) elif a_type == 'metaproduct': match.update({ 'metaproduct_id': article.get('idMetaproduct'), 'product_name': article.get('metaproduct').get('enName'), 'expansion_name': article.get('metaproduct').get( 'expansionName'), }) matches.append(match) print( tb.tabulate([[ item['wantlist_name'], item['count'], u'\u2713' if item['is_foil'] else '', item['product_name'], item['expansion_name'], item['date'] ] for item in matches], headers=[ 'Wantlist', '# bought', 'Foil', 'Name', 'Expansion', 'Date (last) received' ], tablefmt="simple"))
def find_deals_from_user(self, api): self.report("find deals from user") search_string = PyMkmHelper.prompt_string('Enter username') try: result = api.find_user_articles(search_string) except NoResultsError as err: print(err.mkm_msg()) else: if (result): # [x for x in result if x.get('condition') in PyMkmApi.conditions[:3]] # EX+ filtered_articles = result # price > 1 filtered_articles = [x for x in result if x.get('price') > 1] sorted_articles = sorted(filtered_articles, key=lambda x: x['price'], reverse=True) print( f"User '{search_string}' has {len(sorted_articles)} articles that meet the criteria." ) num_searches = int( PyMkmHelper.prompt_string( f'Searching top X expensive cards for deals, choose X (1-{len(sorted_articles)})' )) if num_searches > 1 and num_searches <= len(sorted_articles): table_data = [] index = 0 bar = progressbar.ProgressBar(max_value=num_searches) for article in sorted_articles[:num_searches]: condition = article.get('condition') language = article.get('language').get('languageName') foil = article.get('isFoil') playset = article.get('isPlayset') price = float(article['price']) p = api.get_product(article['idProduct']) name = p['product']['enName'] expansion = p['product'].get('expansion') if expansion: expansion_name = expansion.get('enName') else: expansion_name = 'N/A' if foil: market_price = p['product']['priceGuide'][ 'TRENDFOIL'] else: market_price = p['product']['priceGuide']['TREND'] price_diff = price - market_price percent_deal = round(-100 * (price_diff / market_price)) if price_diff < -1 or percent_deal >= 10: table_data.append([ name, expansion_name, condition, language, u'\u2713' if foil else '', u'\u2713' if playset else '', price, market_price, price_diff, percent_deal ]) index += 1 bar.update(index) bar.finish() if table_data: print('Found some interesting prices:') print( tb.tabulate(sorted(table_data, key=lambda x: x[9], reverse=True), headers=[ 'Name', 'Expansion', 'Condition', 'Language', 'Foil', 'Playset', 'Price', 'Market price', 'Market diff', 'Deal %' ], tablefmt="simple")) else: print('Found no deals. :(') else: print("Invalid number.") else: print('No results found.')
def table(array, caption='', label=None, headers=None, floatfmt=".2f", sideways=False, span_columns=False, need_string=False, rotate=False, extrarowheight=0): if rotate: array = np.fliplr(np.rot90(np.rot90(np.rot90(array)))) if label is None: label = __tabcount__.val #print __tabcount__ if sideways: env = 'sidewaystable' elif span_columns: env = 'table*' else: env = 'table' if headers: if not all(isinstance(el, list) for el in headers): headers = [headers] #print headers if run_from_ipython() and not need_latex(): table = tabulate.tabulate(array, headers=headers, tablefmt='html', numalign='center', stralign='center', floatfmt=floatfmt) __tables__.val[label] = __tabcount__.val fig_html = r""" <div class='table' style='align: center; margin-left: auto; margin-right: auto;'> <div style='margin: auto; text-align: center;' class='tablecaption' name='%s'><b>Table %d:</b> %s</div> %s </div> """ % (label, __tabcount__.val, caption, table) __tabcount__.val += 1 if need_markdown(): return display(HTML(fig_html)) else: return display(HTML(fig_html)) elif run_from_ipython() and need_latex(): table = tabulate.tabulate(array, headers=headers, tablefmt='latex_raw', numalign='center', stralign='center', floatfmt=floatfmt) strlatex = r""" \begin{%s} \centering \caption{%s\label{tab:%s}} %%\begin{adjustbox}{max width=\textwidth} \setlength\extrarowheight{%dpt} %s %%\end{adjustbox} \end{%s}""" % (env, caption, label, extrarowheight, table, env) __tables__.val[label] = __tabcount__.val __tabcount__.val += 1 if need_string: return strlatex return display(Latex(strlatex))