def get_table_from(dictionary): temp = [] # create table with make_table make_table(list(dictionary.items())) # apply some styles to the table after it is created set_column_style(0, width='100', bold=True, color='hsla(225, 80%, 94%, 1)') set_column_style(1, width='100') # render the table return render()
def SvenssonTable(tabledata): Sdata = ordinalData(tabledata) n = len(tabledata) make_table(Sdata) apply_theme('basic_both') set_cell_style(0, 0, thick_border='left,top') #set_cell_style(0, 0, color='lightblack') set_column_style(0, color='lightgray') for i in range(1, n + 1): set_cell_style(i, n + 1 - i, thick_border='all', color="lightbrown") return set_cell_style(0, 0, color='orange')
def tail_normal_table(z): """ Produce a row of Table A104 """ table = [('$z$', 'Height', 'Area'), (z, 100*ndist.pdf(z), 100*ndist.cdf(z))] return make_table(table)
def tail_normal_table(z): """ Produce a row of Table A104 """ table = [('$z$', 'Height', 'Area'), (z, 100 * ndist.pdf(z), 100 * ndist.cdf(z))] return make_table(table)
def symmetric_normal_table(z): """ Produce a row of Table A104 """ if z < 0: raise ValueError('z must be nonnegative') table = [('$z$', 'Height', 'Area'), (z, 100*ndist.pdf(z), 100*2*(ndist.cdf(z)-0.5))] return make_table(table)
def make_deliverable_table(**kwargs): # from ipy_table import * import ipy_table lst = [["", "Max overhead", "Mean overhead", "Median overhead", "300-deliverable", "300/1000-usable"]] for name, data in kwargs.iteritems(): lst.append([name] + compute_deliverable(data)) tbl = ipy_table.make_table(map(list, zip(*lst))) ipy_table.apply_theme('basic_both') return tbl
def symmetric_normal_table(z): """ Produce a row of Table A104 """ if z < 0: raise ValueError('z must be nonnegative') table = [('$z$', 'Height', 'Area'), (z, 100 * ndist.pdf(z), 100 * 2 * (ndist.cdf(z) - 0.5))] return make_table(table)
def vertical_table(adict): """ Use ipy_table to create a table for the ipynb. The `adict` keys are headers on top and the values are vertical content. """ tab_data = [list(adict.keys())] tab_data.append(list(adict.values())) tab = make_table(tab_data) tab.apply_theme('basic') return tab
def _repr_html_(self): table_data = [['n_nodes',self.get_n_nodes(),"number of nodes recorded"], ['n_samples',self.n_samples,"total number of samples"], ['n_accepted',self.n_accepted,"number of samples accepted"], ['n_rejected',self.n_rejected,"number of samples rejected"], ['rejection_ratio',self.rejection_ratio,"n_rejected / n_samples"], ['last_accepted',self.last_accepted,"indicates if the last sample was accepted"], ] table = ipy_table.make_table(table_data) table = ipy_table.apply_theme('basic_left') table = ipy_table.set_global_style(float_format="%3.3f") return table._repr_html_()
def _repr_html_(self): table_data = [['n_nodes', self.get_n_nodes(), "number of nodes recorded"], ['n_samples', self.n_samples, "total number of samples"], ['n_accepted', self.n_accepted, "number of samples accepted"], ['n_rejected', self.n_rejected, "number of samples rejected"], ['rejection_ratio', self.rejection_ratio, "n_rejected / n_samples"], ['last_accepted', self.last_accepted, "indicates if the last sample was accepted"], ] table = ipy_table.make_table(table_data) table = ipy_table.apply_theme('basic_left') table = ipy_table.set_global_style(float_format="%3.3f") return table._repr_html_()
def show(headers, data_dict, header_names=None, display_table=True): data_cols = tuple() if header_names == None: header_names = headers for i in range(len(headers)): data_cols = data_cols + tuple([[data_dict[headers[i]]]]) table_data = np.concatenate(data_cols, axis=0).T ipy_table.make_table([header_names] + table_data.tolist()) ipy_table.apply_theme('basic') table_rendered = ipy_table.render() if display: display.display(table_rendered) return table_rendered
def make_deliverable_table(**kwargs): # from ipy_table import * import ipy_table lst = [[ "", "Max overhead", "Mean overhead", "Median overhead", "300-deliverable", "300/1000-usable" ]] for name, data in kwargs.iteritems(): lst.append([name] + compute_deliverable(data)) tbl = ipy_table.make_table(map(list, zip(*lst))) ipy_table.apply_theme('basic_both') return tbl
def compare_moment_based_lb_methods(reference, other, show_deviations_only=False): import ipy_table table = [] caption_rows = [len(table)] table.append(['Shared Moment', 'ref', 'other', 'difference']) reference_moments = set(reference.moments) other_moments = set(other.moments) for moment in reference_moments.intersection(other_moments): reference_value = reference.relaxation_info_dict[ moment].equilibrium_value other_value = other.relaxation_info_dict[moment].equilibrium_value diff = sp.simplify(reference_value - other_value) if show_deviations_only and diff == 0: pass else: table.append([ f"${sp.latex(moment)}$", f"${sp.latex(reference_value)}$", f"${sp.latex(other_value)}$", f"${sp.latex(diff)}$" ]) only_in_ref = reference_moments - other_moments if only_in_ref: caption_rows.append(len(table)) table.append(['Only in Ref', 'value', '', '']) for moment in only_in_ref: val = reference.relaxation_info_dict[moment].equilibrium_value table.append( [f"${sp.latex(moment)}$", f"${sp.latex(val)}$", " ", " "]) only_in_other = other_moments - reference_moments if only_in_other: caption_rows.append(len(table)) table.append(['Only in Other', '', 'value', '']) for moment in only_in_other: val = other.relaxation_info_dict[moment].equilibrium_value table.append( [f"${sp.latex(moment)}$", " ", f"${sp.latex(val)}$", " "]) table_display = ipy_table.make_table(table) for row_idx in caption_rows: for col in range(4): ipy_table.set_cell_style(row_idx, col, color='#bbbbbb') return table_display
def smt_ipy_table(records, fields, parameters=[]): from ipy_table import make_table import ipy_table table = [[field.title() for field in fields]] for record in records: record_list = [] for field in fields: attr = getattr(record, field) if field == 'timestamp': s = attr.strftime('%Y-%m-%d %H:%M') elif field == 'repository': s = '{0}'.format(attr.upstream) elif field == 'parameters' and parameters: s = '' d = attr.as_dict() for p in parameters: s += ' {0}: {1},'.format(p, d[p]) s = s[1:-1] elif field == 'tags': s = '' for tag in attr: s += ' {0},'.format(tag) s = s[1:-1] elif field == 'version': s = attr[:12] elif field == 'duration': s = human_readable_duration(attr) elif field == 'label': s = attr[:8] else: s = str(attr) c = cgi.escape(s) # if field in ('label', 'timestamp', 'repository', 'parameters', 'tags', 'version', 'duration'): # # c = "<code>" + c + "</code>" if field in ('label', 'repository', 'version', 'parameters'): c = "<code>" + c + "</code>" record_list.append(c) table.append(record_list) t = make_table(table) ipy_table.apply_theme('basic') ipy_table.set_global_style(wrap=True) return HTML(t._repr_html_())
def _repr_html_(self): table_data = [['node_name',self.node_name,"name of the node"], ['last_method',self.last_method,"sampling method used to obtain the last sample"], ['n_samples',self.n_samples,"total number of samples"], ['n_accepted',self.n_accepted,"number of samples accepted"], ['n_rejected',self.n_rejected,"number of samples rejected"], ['rejection_ratio',self.rejection_ratio,"n_rejected / n_samples"], ['last_accepted',self.last_accepted,"indicates if the last sample was accepted"], ['sample_ratio',self.sample_ratio,"n_samples of the node / n_samples of the graph"], ['last_threshold',self.last_threshold,"threshold value (Metropolis Hastings) for the last sample"], ['last_random_number',self.last_random_number,"random number for the last sample"], ['last_seed',self.last_seed,"seed for the last sample"], ] for param in self.last_parameters.keys(): table_data += [ [param,self.last_parameters[param],"parameter of the last sampling method"] ] table = ipy_table.make_table(table_data) table = ipy_table.apply_theme('basic_left') table = ipy_table.set_global_style(float_format="%3.3f") return table._repr_html_()
def fares_to_table(fares): keys, values = zip(*fares.items()) table_rows = [['Fare Type']] table_rows[-1].extend(key.title() for key in sorted(keys)) for key in sorted(values[0].keys()): table_rows.append([key.title()]) table_rows[-1].extend( '${:.2f}'.format(ticket_type[key]) for ticket_type in values ) table = ipy_table.make_table(table_rows) table.apply_theme('basic') return table
def _repr_html_(self): table_data = [['node_name', self.node_name, "name of the node"], ['last_method', self.last_method, "sampling method used to obtain the last sample"], ['n_samples', self.n_samples, "total number of samples"], ['n_accepted', self.n_accepted, "number of samples accepted"], ['n_rejected', self.n_rejected, "number of samples rejected"], ['rejection_ratio', self.rejection_ratio, "n_rejected / n_samples"], ['last_accepted', self.last_accepted, "indicates if the last sample was accepted"], ['sample_ratio', self.sample_ratio, "n_samples of the node / n_samples of the graph"], ['last_threshold', self.last_threshold, "threshold value (Metropolis Hastings) for the last sample"], ['last_random_number', self.last_random_number, "random number for the last sample"], ['last_seed', self.last_seed, "seed for the last sample"], ] for param in self.last_parameters.keys(): table_data += [[param, self.last_parameters[param], "parameter of the last sampling method"]] table = ipy_table.make_table(table_data) table = ipy_table.apply_theme('basic_left') table = ipy_table.set_global_style(float_format="%3.3f") return table._repr_html_()
def run_vector(vector): ''' Executes a test vector, sets vector['result_html'] to the result Returns: True if (after execution) vector['result_html'] matches vector['expected_html'] ''' if vector['tabulate_columns']: # This is a tabulate() vector. table = tabulate(vector['data'], vector['tabulate_columns']) else: # This is a make_table() vector table = make_table(vector['data']) # For each operation, call the designated table method with # the designated keyword arguments for operation in vector['operations']: method_name, kwargs_dict = operation method = getattr(table, method_name) method(**kwargs_dict) # Update the vector with the result vector['result_html'] = table._repr_html_() return vector['result_html'] == vector['expected_html']
def main(): now = tostr(time.time()) if getpass.getuser().lower() == 'root': outdir = '/var/www/html/' else: outdir = '.' mhds = ['minute', 'hour', 'hour'] candles = [30, 4, 6] out = open(os.path.join(outdir, 'index.html'), 'w') out.write(''' <html> <body> <div id="intro"> <br> <h2>Last update {last_update}</h2> <h2>All data below generated using short MA 5 e long MA 21</h2> '''.format(last_update=now)) for candle, mhd in zip(candles, mhds): out.write( " <h2><a href='decision_table_candle_%03d_%s.html'> Decision table candle %03d %s</a></h2>\n" % (candle, mhd, candle, mhd)) for coin in COIN_LIST: for candle, mhd in zip(candles, mhds): out.write( " <h2><a href='graphs_%s_candle_%03d_%s.html'> %s, candle %03d %s</a></h2>\n" % (coin, candle, mhd, coin, candle, mhd)) out.write(''' </div> </body> </html> ''') out.close() for candle, mhd in zip(candles, mhds): all_exc = set() decision_table = {} for coin in COIN_LIST: decision_table[coin] = {} row = [] exc = EXCHANGES.get(coin, EXCHANGES['BTC']) html = 'graphs_%s_candle_%03d_%s.html' % (coin, candle, mhd) out = open(os.path.join(outdir, html), 'w') out.write(''' <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); ''') for num, e in enumerate(exc): all_exc.add(e) ma = write_func(coin, out, num, e, mhd, candle) for action in ma.get('action', [])[::-1]: if action: break decision_table[coin][e] = action out.write(''' </script> </head> <body> ''') for num, e in enumerate(exc): out.write( ' <div id="chart_div%02d" style="height: 700px;"></div>\n' % num) out.write(''' </body> </html> ''') out.close() data = [] buy = [] sell = [] data.append(['Exchange'] + COIN_LIST) for i, e in enumerate(sorted(list(all_exc))): row = [e] for j, coin in enumerate(COIN_LIST): d = decision_table[coin].get(e) if d == 'buy': buy.append([i, j]) if d == 'sell': sell.append([i, j]) row.append(d) data.append(row) table = ipy_table.make_table(data) for i in range(len(data)): for j in range(len(data[0])): table.set_cell_style(i, j, width=60, align='center') for i in range(len(data)): table.set_cell_style(i, 0, bold=True, align='left') for j in range(len(data[0])): table.set_cell_style(0, j, bold=True) for i, j in sell: table.set_cell_style(i + 1, j + 1, color='red') for i, j in buy: table.set_cell_style(i + 1, j + 1, color='green') html = 'decision_table_candle_%03d_%s.html' % (candle, mhd) with open(os.path.join(outdir, html), 'w') as out: out.write(table._repr_html_())
# In[12]: # This cell is meta code whose purpose is to display HTML into this notebook # so that it will display in the Zippped Code blog. # ipy_table is a 3rd party package from ipy_table import make_table, set_column_style, render import significance_for_differences from IPython.lib import deepreload deepreload.reload(significance_for_differences) header = list(('Collection Name', 'Field', 'Data Types and Counts', )) if header not in formatted_type_counts_: formatted_type_counts_.insert(0, header) make_table(formatted_type_counts_) set_column_style(0, width='100', bold=False,) set_column_style(1, width='100', color='hsla(225, 80%, 94%, 1)') set_column_style(2, width='100', bold=True,) display(HTML(significance_for_differences.table_title)) render() # In[13]: display(HTML(significance_for_differences.significance_anchor)) # ### The fix. # # I [wrote a module](https://github.com/dm-wyncode/flpd_helper/blob/master/flpd_helper/documents.py) that maps data types to mongoengine.Field classes. It even includes some custom functions that modify some of the values so that they may be cast into their appropriate data type.
def conf_mat(y_true, y_pred, reverse=True): """ :param y_true: True labels (0 or 1) :param y_pred: Predicted probabilities (0 to 1) :return: HTML table (ipy_table) which will be displayed in Jupyter Needs `pip install ipy_table` Usage: >>> conf_mat(y_true, y_pred) """ from ipy_table import make_table, set_cell_style, apply_theme df = pd.DataFrame({"pred": y_pred, "true": y_true}) cm = (pd.pivot_table(df, index="true", columns="pred", aggfunc=len).fillna(0).astype(int)) classes = sorted(cm.index | cm.columns, reverse=reverse) cm = cm.reindex(classes, classes, fill_value=0) precs = pd.Series([cm.ix[c, c] / cm.ix[c, :].sum() for c in classes], index=classes) recall = pd.Series([cm.ix[c, c] / cm.ix[:, c].sum() for c in classes], index=classes) accuracy = sum(cm.ix[c, c] for c in classes) / cm.sum().sum() tot_true = cm.sum(axis=0) tot_pred = cm.sum(axis=1) table_data = [["Pred ->"] + classes + ["Total", "Recall"]] total = cm.sum().sum() for class_j, field_type_j in [(c, "class") for c in classes] + [ (None, "total"), (None, "precision"), ]: table_row = [] for class_i, field_type_i in ([(None, "name")] + [(c, "class") for c in classes] + [(None, "total"), (None, "recall")]): val = { ("name", "class"): class_j, ("name", "precision"): "Precision", ("name", "total"): "Total", ("class", "class"): cm.ix[class_i, class_j], ("precision", "class"): "{:.1%}".format(precs[class_i]), ("precision", "total"): "", ("precision", "recall"): "{:.1%}".format(accuracy), ("recall", "class"): "{:.1%}".format(recall[class_j]), ("recall", "total"): "", ("total", "class"): tot_true[class_j], ("class", "total"): tot_pred[class_i], ("total", "total"): total, }[field_type_i, field_type_j] table_row.append(val) table_data.append(table_row) tab = make_table(table_data) apply_theme("basic_both") num_classes = len(classes) set_cell_style(1, 1, thick_border="left, top") set_cell_style(1, num_classes, thick_border="top,right") set_cell_style(num_classes, 1, thick_border="left,bottom") set_cell_style(num_classes, num_classes, thick_border="bottom,right") for i in range(2, num_classes): set_cell_style(i, 1, thick_border="left") set_cell_style(i, num_classes, thick_border="right") set_cell_style(1, i, thick_border="top") set_cell_style(num_classes, i, thick_border="bottom") return tab
) for count, citation in aggregated_data ] # ### NB: A Pelican plug-in I am using to create posts with notebooks is adding extra ';' characters likely secondary to HTML escaping during rendering. # # It is still easier on the eyes than reading a list of tuples! # # The following code is particular to [Jupyter](http://jupyter.org/) notebooks for displaying HTML. # In[25]: # add headers citation_counts_ = [('Count', CITATION_CHARGE, ), ] + citation_counts # create table with make_table make_table(citation_counts_) # apply some styles to the table after it is created set_column_style(0, width='100', bold=True, color='hsla(225, 80%, 94%, 1)') set_column_style(1, width='100') display(HTML(render()._repr_html_())) # ## There are other blog posts about this dataset. # * [Docker Compose container version of the data]({filename}../hackathons/dockerized-flpd-data.rst) # * [Experimentation with CSV files]({filename}../hackathons/flpd_data_01.rst). # ## resources # # * [_Talk Python to Me_ podcast about PyMongo](https://talkpython.fm/episodes/show/2/python-and-mongodb) # * [PyMongo tutorial](https://api.mongodb.com/python/current/tutorial.html)
def moment_equality_table_by_stencil(name_to_stencil_dict, moments, truncate_order=3): """ Creates a table for display in IPython notebooks that shows which moments agree between continuous and discrete equilibrium, group by stencils Args: name_to_stencil_dict: dict from stencil name to stencil moments: sequence of moments to compare - assumes that permutations have similar properties so just one representative is shown labeled with its multiplicity truncate_order: compare up to this order """ import ipy_table from lbmpy.maxwellian_equilibrium import discrete_maxwellian_equilibrium from lbmpy.maxwellian_equilibrium import continuous_maxwellian_equilibrium from lbmpy.continuous_distribution_measures import continuous_moment stencil_names = [] stencils = [] for key, value in name_to_stencil_dict.items(): stencil_names.append(key) stencils.append(value) moments = list(pick_representative_moments(moments)) colors = {} for stencil_idx, stencil in enumerate(stencils): dim = stencil.D u = sp.symbols(f"u_:{dim}") discrete_equilibrium = discrete_maxwellian_equilibrium( stencil, c_s_sq=sp.Rational(1, 3), compressible=True, u=u, order=truncate_order) continuous_equilibrium = continuous_maxwellian_equilibrium( dim=dim, u=u, c_s_sq=sp.Rational(1, 3)) for moment_idx, moment in enumerate(moments): moment = moment[:dim] dm = discrete_moment(discrete_equilibrium, moment, stencil) cm = continuous_moment(continuous_equilibrium, moment, symbols=sp.symbols("v_0 v_1 v_2")[:dim]) difference = sp.simplify(dm - cm) if truncate_order: difference = sp.simplify( remove_higher_order_terms(difference, symbols=u, order=truncate_order)) colors[(moment_idx + 1, stencil_idx + 2)] = 'Orange' if difference != 0 else 'lightGreen' table = [] header_row = [' ', '#'] + stencil_names table.append(header_row) for moment in moments: row = [str(moment), str(moment_multiplicity(moment)) ] + [' '] * len(stencils) table.append(row) table_display = ipy_table.make_table(table) ipy_table.set_row_style(0, color='#ddd') for cell_idx, color in colors.items(): ipy_table.set_cell_style(cell_idx[0], cell_idx[1], color=color) return table_display
def moment_equality_table(stencil, discrete_equilibrium=None, continuous_equilibrium=None, max_order=4, truncate_order=3): """ Creates a table showing which moments of a discrete stencil/equilibrium coincide with the corresponding continuous moments Args: stencil: list of stencil velocities discrete_equilibrium: list of sympy expr to compute discrete equilibrium for each direction, if left to default the standard discrete Maxwellian equilibrium is used continuous_equilibrium: continuous equilibrium, if left to default, the continuous Maxwellian is used max_order: compare moments up to this order (number of rows in table) truncate_order: moments are considered equal if they match up to this order Returns: Object to display in an Jupyter notebook """ import ipy_table from lbmpy.continuous_distribution_measures import continuous_moment u = sp.symbols(f"u_:{stencil.D}") if discrete_equilibrium is None: from lbmpy.maxwellian_equilibrium import discrete_maxwellian_equilibrium discrete_equilibrium = discrete_maxwellian_equilibrium( stencil, c_s_sq=sp.Rational(1, 3), compressible=True, u=u, order=truncate_order) if continuous_equilibrium is None: from lbmpy.maxwellian_equilibrium import continuous_maxwellian_equilibrium continuous_equilibrium = continuous_maxwellian_equilibrium( dim=stencil.D, u=u, c_s_sq=sp.Rational(1, 3)) table = [] matched_moments = 0 non_matched_moments = 0 moments_list = [ list(moments_of_order(o, stencil.D, include_permutations=False)) for o in range(max_order + 1) ] colors = dict() nr_of_columns = max([len(v) for v in moments_list]) + 1 header_row = [' '] * nr_of_columns header_row[0] = 'order' table.append(header_row) for order, moments in enumerate(moments_list): row = [' '] * nr_of_columns row[0] = f'{order}' for moment, col_idx in zip(moments, range(1, len(row))): multiplicity = moment_multiplicity(moment) dm = discrete_moment(discrete_equilibrium, moment, stencil) cm = continuous_moment( continuous_equilibrium, moment, symbols=sp.symbols("v_0 v_1 v_2")[:stencil.D]) difference = sp.simplify(dm - cm) if truncate_order: difference = sp.simplify( remove_higher_order_terms(difference, symbols=u, order=truncate_order)) if difference != 0: colors[(order + 1, col_idx)] = 'Orange' non_matched_moments += multiplicity else: colors[(order + 1, col_idx)] = 'lightGreen' matched_moments += multiplicity row[col_idx] = f'{moment} x {moment_multiplicity(moment)}' table.append(row) table_display = ipy_table.make_table(table) ipy_table.set_row_style(0, color='#ddd') for cell_idx, color in colors.items(): ipy_table.set_cell_style(cell_idx[0], cell_idx[1], color=color) print( f"Matched moments {matched_moments} - non matched moments {non_matched_moments} " f"- total {matched_moments + non_matched_moments}") return table_display
['Item','Kaggle (Count)','Kaggle (%)' ,'Sample Dataset (Count)', 'Sample Dataset (%)'], ['Total Passengers',kaggle_passengers,'-', sample_passengers,'-'], ['Survivors', kaggle_survivors, calculate_percentage(kaggle_survivors,kaggle_passengers, True), sample_survivors, calculate_percentage(sample_survivors,sample_passengers, True) ], ['Non-survivors', kaggle_nonsurvivors, calculate_percentage(kaggle_nonsurvivors,kaggle_passengers, True), sample_nonsurvivors, calculate_percentage(sample_nonsurvivors,sample_passengers, True) ] ] tbl.make_table(survivors) table = pd.crosstab(titanic_df['Survived'],titanic_df['Sex']) print table print titanic_df.groupby('Sex').Survived.mean() # calculate values for each survival status survivors_gender = survivors_data.groupby(['Sex']).size().values non_survivors_gender = non_survivors_data.groupby(['Sex']).size().values # calculate totals for percentates totals = survivors_gender + non_survivors_gender
def setcompare(iter1, iter2): cntr1 = Counter(iter1) cntr2 = Counter(iter2) only1 = cntr1.keys() - cntr2.keys() only2 = cntr2.keys() - cntr1.keys() both = cntr1.keys() & cntr2.keys() cnt1 = sum(cntr1[key] for key in only1) cnt2 = sum(cntr2[key] for key in only2) cnt12a = sum(cntr1[key] for key in both) cnt12b = sum(cntr2[key] for key in both) distinct1 = len(only1) distinct2 = len(only2) distinct12 = len(both) cnt_perct = "{} ({:.0%})".format if hasattr(iter1, "name"): name1 = f"1 {iter1.name}" else: name1 = "1" if hasattr(iter2, "name"): name2 = f"2 {iter2.name}" else: name2 = "2" display_data = [ ["", f"Set {name1} only", "Intersect.", f"Set {name2} only"], [ "Count", cnt_perct(cnt1, cnt1 / (cnt1 + cnt12a)), "{} | {}".format(cnt12a, cnt12b), cnt_perct(cnt2, cnt2 / (cnt2 + cnt12b)), ], [ "Distinct count", cnt_perct(distinct1, distinct1 / (distinct1 + distinct12)), distinct12, cnt_perct(distinct2, distinct2 / (distinct2 + distinct12)), ], [ "Examples", format_tuples( pluck( 0, Counter({key: cntr1[key] for key in only1}).most_common(5)), cntr1, ), format_tuples( pluck( 0, Counter({key: cntr1[key] + cntr2[key] for key in both}).most_common(5), ), cntr1, cntr2, ), format_tuples( pluck( 0, Counter({key: cntr2[key] for key in only2}).most_common(5)), cntr2, ), ], ] make_table(display_data) table = apply_theme("basic_both") for x, y in product([0, 1, 2], [1, 2, 3]): set_cell_style(x, y, align="center") return table
def methods(obj): d = dir(obj) d = filter(isMethod, d) return ipt.make_table(l2ll(d))
def create_tables(cur, for_div=False, include_8888=False): " Produce ranking table " tabs = {} lastdate = list(cur.execute("SELECT MAX(daterange) FROM overall"))[0][0] rangetext = lastdate[6:8] + '/' + lastdate[4:6] + '至' + lastdate[ 11:13] + '/' + lastdate[9:11] for title, code, _ in regions: daterange, rankdata = get_rank(cur, code, lastdate[-4:], include_8888) if code == 'SuperDC': for n, row in enumerate(rankdata): row = list(row) row[0] += 800 rankdata[n] = row tabletitle = rangetext + title + '民調排名' human, beast = [ r for r in rankdata if r[2] is not None and r[2] < 0 ], [r for r in rankdata if r[2] is not None and r[2] > 0] header = ["編號", "政黨", "候選人", "%" if include_8888 else "有效%", ""] tabledata = [[tabletitle] + ([""] * 10), ["非建制"] + ([""] * 5) + ["建制"] + ([""] * 4), header + [""] + header] for n in range(max(len(human), len(beast))): tabledata.append([]) if len(human) > n: tabledata[-1].extend([human[n][0]] + human[n][1].split() + ["%.2f" % human[n][3], n + 1]) else: tabledata[-1].extend([''] * 5) if len(beast) > n: tabledata[-1].extend(['', beast[n][0]] + beast[n][1].split() + ["%.2f" % beast[n][3], n + 1]) else: tabledata[-1].extend([''] * 6) table = make_table(tabledata) set_global_style(no_border='all', align="center") set_cell_style(0, 0, column_span=11, bold=True, color=brewer['Greens'][8][4]) set_cell_style(1, 0, column_span=5, bold=True, color=getcolour(-2, True), font_color="white") set_cell_style(1, 6, column_span=5, bold=True, color=getcolour(2, True), font_color="white") for n in range(11): if n < 5: set_cell_style(2, n, bold=True, color=getcolour(-2, True), font_color="white") elif n > 5: set_cell_style(2, n, bold=True, color=getcolour(2, True), font_color="white") for m in range(max(len(human), len(beast))): hcolor = getcolour(human[m][2] if len(human) > m else -0.1, True) bcolor = getcolour(beast[m][2] if len(beast) > m else 0.1, True) for n in range(11): if n < 5: set_cell_style(3 + m, n, color=hcolor) elif n > 5: set_cell_style(3 + m, n, color=bcolor) if not for_div: display(HTML(table._repr_html_())) else: tabs[code] = table._repr_html_() if tabs: return tabs
def _repr_html_(self): import ipy_table # noinspection PyProtectedMember return ipy_table.make_table(self._build_table())._repr_html_()