def table_output(matrix, warning, header, t_name): writer = pytablewriter.MarkdownTableWriter() writer.table_name = t_name writer.header_list = list(range(0, len(header))) writer.value_matrix = matrix writer.margin = 1 writer.write_table() footer = pytablewriter.MarkdownTableWriter() footer.table_name = warning footer.value_matrix = tuple([[]]) footer.margin = 0 footer.write_table()
def main(): parser = argparse.ArgumentParser( description='Your clipboard(Excel or Google spreadsheet) change to markdown clipboard', add_help=True, ) parser.add_argument('-f', help='Please input format type(csv)', default=None, type=str) parser.add_argument('-l', help='Please input linesep', default='<br>', type=str) try: args = parser.parse_args() if args.f is None: data = pd.read_clipboard() elif args.f == 'csv': data = pd.read_csv(io.StringIO(pyperclip.paste())) elif args.f == 'tsv': data = pd.read_csv(io.StringIO(pyperclip.paste()), sep="\t") data = data.replace(os.linesep, args.l, regex=True) data.fillna('', inplace=True) writer = pytablewriter.MarkdownTableWriter() writer.from_dataframe(data) md = writer.dumps() print(md) print('......Succeeded!!') pyperclip.copy(md) except Exception as e: print('Error: cannot clipboard to markdown. Please check your clipboard')
def to_markdown(self, outfile=None): self.writer = ptw.MarkdownTableWriter() self.writer.from_dataframe(self.df) #self.writer.write_table() if outfile: self.writer.dump(outfile) return self
def file_stats(): mls = ['dt', 'rf'] # , 'svm', 'nb'] for window in windows: writer = pytablewriter.MarkdownTableWriter() writer.table_name = 'File Accuracy for {}s'.format(window) writer.header_list = ['File', 'Decision Tree', 'Random Forest', 'Tensorflow'] value_matrix = [] for name in binet_files: values = [name] feature, label = get_feature_labels(get_saved_data(window, name, v2=True), v2=True) # feature = mask_features(feature) feat_train, feat_test, label_train, label_test = train_test_split( feature, label, test_size=0.3, random_state=42) for ml in mls: r = train_and_test_with(feat_train, label_train, ml, feat_test, label_test) values.append('{0:.4f}, {1:.4f}, {2:.4f}'.format( r['accuracy'], r['precision'], r['recall'])) print(values) correctness, precision, recall = \ keras_train_and_test(feat_train, label_train, feat_test, label_test, dimension=22) values.append('{0:.4f}, {1:.4f}, {2:.4f}'.format(correctness, precision, recall)) print(values) value_matrix.append(values) writer.value_matrix = value_matrix writer.write_table()
def table_output(self, matrix, num): writer = pytablewriter.MarkdownTableWriter() writer.table_name = 'the most connected tables:' writer.header_list = ["num", "table", "number of in/out connections"] writer.value_matrix = matrix writer.margin = 1 writer.write_table()
def stats(players): players = list(map(lambda p: p.lower(), players)) player_names = [] teams = {} roles = {} table = [copy.copy(CON.TABLE)] for player in STATS: if player['name'].lower() in players: player_names.append(player['name']) teams[player['name']] = CON.TEAMS.get(player['team'], player['team']) roles[player['name']] = player['role'] table.append([ player['eliminations_avg_per_10m'], player['deaths_avg_per_10m'], player['hero_damage_avg_per_10m'], player['healing_avg_per_10m'], player['ultimates_earned_avg_per_10m'], player['final_blows_avg_per_10m'], player['time_played_total'] / 60 / 60 ]) writer = pytablewriter.MarkdownTableWriter() writer.stream = six.StringIO() writer.header_list = ["Statistics"] + player_names writer.value_matrix = list(map(list, zip(*table))) writer.write_table() reply = "# Statistics for " + ', '.join(player_names) + "\n\n" reply += ''.join([ f"{name}: {roles[name]} for {teams[name]}. \n" for name in player_names ]) + "\n---\n\n" return reply + writer.stream.getvalue() + CON.FOOTER
def output(tensorboard_dir, output_dir, metrics_keys, steps, output_file_base="metrics"): """Output csv and markdown file which accumulated tensorflow event by step and metrics_keys.""" subdirs = GetLogdirSubdirectories(tensorboard_dir) event_accumulators = [] for subdir in subdirs: event_accumulator = EventAccumulator(subdir) # init event accumulator event_accumulator.Reload() event_accumulators.append(event_accumulator) if not metrics_keys: metrics_keys = { metrics_key for event_accumulator in event_accumulators for metrics_key in _get_metrics_keys(event_accumulator) } columns = [_column_name(event_accumulator, metrics_key) for event_accumulator, metrics_key in itertools.product(event_accumulators, metrics_keys)] columns.sort() df = pd.DataFrame([], columns=columns) for event_accumulator in event_accumulators: for metrics_key in metrics_keys: value_step_list = _value_step_list(event_accumulator, metrics_key) for value, step in value_step_list: column_name = _column_name(event_accumulator, metrics_key) df.loc[step, column_name] = value if steps: df = df[steps, :] df = df.sort_index(ascending=False) # index to column. and re-order column. df["step"] = df.index df = df[["step"] + columns] output_csv = os.path.join(output_dir, "{}.csv".format(output_file_base)) df.to_csv(output_csv, index=False) output_md = os.path.join(output_dir, "{}.md".format(output_file_base)) writer = pytablewriter.MarkdownTableWriter() writer.char_left_side_row = "|" # fix for github writer.from_dataframe(df) with open(output_md, "w") as file_stream: writer.stream = file_stream writer.write_table() message = """ output success output csv: {} output md: {} """.format(output_csv, output_md) print(message)
def build_table_stats_annual(data, count_only=False, unique_only=False): tmp = [] table_name = "Annual Stats" if unique_only: data = return_unique(data) table_name = table_name + " (Unique only)" writer = ptw.MarkdownTableWriter() writer.table_name = table_name writer.header_list = ["year", "countries", "cities", "country_city"] ordered_data = collections.OrderedDict(sorted(data.items())) for key, value in ordered_data.iteritems(): if not count_only: tmp.append([ key, ", ".join(list(value[0])), ", ".join(list(value[1])), ", ".join(list(value[2])) ]) else: tmp.append([ key, len(list(value[0])), len(list(value[1])), len(list(value[2])) ]) writer.value_matrix = tmp write_to_buffer(writer)
def add_to_markdown_README(paper_table_filepath): df = pd.read_csv(paper_table_filepath) df = df[['DATE', 'TITLE', 'AUTHOR', 'ABS_LINK', 'PDF_LINK']] df.ABS_LINK = df.ABS_LINK.apply(lambda x: '[Abstract]({})'.format(x)) df.PDF_LINK = df.PDF_LINK.apply(lambda x: '[PDF]({})'.format(x)) df['Links'] = df.ABS_LINK + ' | ' + df.PDF_LINK df = df.drop(['ABS_LINK', 'PDF_LINK', 'AUTHOR'], axis=1) df.columns = ['Date', 'Title', 'Links'] writer = pytablewriter.MarkdownTableWriter() writer.stream = io.StringIO() writer.header_list = list(df.columns) writer.value_matrix = df.values.tolist() writer.write_table() writer.stream.seek(0) markdown = "".join(writer.stream.readlines()) current_date = date.today() ymd = " / ".join([i for i in map(str, [current_date.year, current_date.month, current_date.day])]) final_string = str("## {}\n\n".format(ymd) + markdown) with open("README.md", 'r+') as f: content = f.read() if ymd not in content[:20]: f.seek(0, 0) f.write(final_string + '\n\n' + content) else: print("Already put table from this week.")
def main(): writer = pytablewriter.MarkdownTableWriter() writer.table_name = "zone" writer.headers = ["zone_id", "country_code", "zone_name"] writer.value_matrix = [ ["1", "AD", "Europe/Andorra"], ["2", "AE", "Asia/Dubai"], ["3", "AF", "Asia/Kabul"], ["4", "AG", "America/Antigua"], ["5", "AI", "America/Anguilla"], ] # writer instance writes a table to stdout by default writer.write_table() # change the stream to a string buffer to get the output as a string # you can also get tabular text by using dumps method writer.stream = io.StringIO() writer.write_table() print(writer.stream.getvalue()) # change the output stream to a file with open("sample.md", "w") as f: writer.stream = f writer.write_table()
def update_readme(summary): writer = pytablewriter.MarkdownTableWriter() writer.headers = ["Tool", "Apt", "Yum", "Packman", "APK", "DNF", "CURL", "URL"] value_matrix = [] for tool_shortname in summary: tool = summary[tool_shortname] name = tool['name'] installers = tool['installers'] apt = "Yes" if "apt" in installers else "No" yum = "Yes" if "yum" in installers else "No" pacman = "Yes" if "pacman" in installers else "No" apk = "Yes" if "apk" in installers else "No" dnf = "Yes" if "dnf" in installers else "No" curl = "Yes" if "curl" in installers else "No" url = "https://installer.to/" + tool_shortname value_matrix.append([name, apt, yum, pacman, apk, dnf, curl, url]) writer.value_matrix = value_matrix table_md = writer.dumps() try: with open("./README.md", "r+") as readme_md: readme = readme_md.read() beggining = "<!-- beginning of tools list -->" end = "<!-- end of tools list -->" regex = r"" + beggining + "\n(.*)\n" + end readme = re.sub(regex, beggining + "\n" + table_md + "\n" + end, readme, flags=re.S) readme_md.seek(0) # sets point at the beginning of the file readme_md.truncate() # Clear previous content readme_md.write(readme) readme_md.close() except Error as e: logging.error('Error occurred when trying to update README.md, error: ' + e)
def algorithm_data_sheet(params): title = algorithm_data_sheet_title(params['basename']) tmp = {'title': title} tmp.update(params) data = [] for kd in params[ALG_VARS].keys(): alg = params[ALG_VARS][kd] row = [ alg['CRYPTO_ALGNAME'], 'TODO', alg['nist-level'], alg['CRYPTO_PUBLICKEYBYTES'], alg['CRYPTO_SECRETKEYBYTES'], alg['CRYPTO_CIPHERTEXTBYTES'], alg['CRYPTO_BYTES'] ] data.append(row) tw = pytablewriter.MarkdownTableWriter() tw.header_list = [ 'Parameter set', 'Security model', 'Claimed NIST security level', 'Public key size (bytes)', 'Secret key size (bytes)', 'Ciphertext size (bytes)', 'Shared secret size (bytes)' ] tw.value_matrix = data tw.margin = 1 tw.stream = io.StringIO() tw.write_table() raw_table = tw.stream.getvalue() # ugly: fix justification lines = raw_table.split('\n') lines[1] = lines[1].replace('|-', '|:') lines[1] = lines[1].replace('-|', ':|') tmp['table'] = '\n'.join(lines).strip() # NB: clears extra newlines return template.ALGORITHM_DATA_SHEET.format_map(tmp)
def generate_schedule(self) -> Text: """Generate a markdown representation of who is on call when. Returns: Text: User forcast for next 7 days, in a markdown table. """ dow = datetime.datetime.today().weekday() if not self.signed_off: days = ['{} (today)'.format(calendar.day_abbr[dow])] else: days = ['{} (tmrw)'.format(calendar.day_abbr[dow + 1])] people = [util.discord_name(self._users[0])] for i in range(1, 7): if not self.signed_off: day_name = calendar.day_abbr[(dow + i) % 7] else: day_name = calendar.day_abbr[(dow + i + 1) % 7] days.append(day_name) user = self._users[i % len(self._users)] people.append(user.nick or user.name) writer = pytablewriter.MarkdownTableWriter(headers=days, value_matrix=[people]) return writer.dumps()
def render(self): print('Render training time table...') markdown_table = pytablewriter.MarkdownTableWriter( table_name='Training Times', headers=list(self.keys()), value_matrix=[list(self.values())]) markdown_file = self._get_output_path() markdown_table.dump(markdown_file)
def OutputMarkdown(self): writer = pytablewriter.MarkdownTableWriter() writer.table_name = self.df.name writer.header_list = self.summary_cols writer.value_matrix = self.df_summary writer.write_table()
def render(self): print('Render classification table...') markdown_table = pytablewriter.MarkdownTableWriter( table_name='Classification Results', headers=list(self.keys()), value_matrix=[list(self.values())]) markdown_file = self._get_output_path() markdown_table.dump(markdown_file)
def cm_markdown(cm, name): writer = pytablewriter.MarkdownTableWriter() writer.table_name = name + ' confusion matrix' writer.header_list = ['index', 'sand', 'gracel', 'boulders'] index = ['sand', 'gravel', 'boulders'] cm = np.c_[index, cm] writer.value_matrix = cm writer.write_table()
def render(self, df, name): self.table_count += 1 writer = pytablewriter.MarkdownTableWriter() #writer.table_name = "example_table" writer.header_list = list(df.columns.values) writer.value_matrix = df.values.tolist() display(Markdown("%s \n\n**Table %d**: *%s*\n\n" % (writer.dumps(), self.table_count, name)))
def get_lossless_average(path, reference_format): merged_data = {} columns = [ "format", "avg_bpp", "avg_compression_ratio", "avg_space_saving", "wavg_encode_time", "wavg_decode_time" ] final_data = pd.DataFrame(columns=columns) final_data.set_index("format", drop=False, inplace=True) for format in next(os.walk(path))[1]: if not glob.glob(path + "/" + format + "/lossless/*.out"): print("Lossless results files could not be found for format {}.". format(format)) continue rawdata = [] data_path = path + "/" + format + "/lossless/" for f in glob.glob(data_path + "/*.out"): rawdata.append(pd.read_csv(f, sep=":")) merged_data[format] = pd.concat(rawdata) sum_orig_file_size = np.sum(merged_data[format]["orig_file_size"]) sum_compressed_file_size = np.sum( merged_data[format]["compressed_file_size"]) sum_pixels = np.sum(merged_data[format]["pixels"]) avg_bpp = sum_compressed_file_size * 8 / sum_pixels avg_compression_ratio = sum_orig_file_size / sum_compressed_file_size avg_space_saving = 1 - (1 / avg_compression_ratio) wavg_encode_time = np.average(merged_data[format]["encode_time"], weights=merged_data[format]["pixels"]) wavg_decode_time = np.average(merged_data[format]["decode_time"], weights=merged_data[format]["pixels"]) final_data.loc[format] = [ format, avg_bpp, avg_compression_ratio, avg_space_saving, wavg_encode_time, wavg_decode_time ] final_data = final_data.assign( weissman_score=lambda x: x.avg_compression_ratio / x.loc[ reference_format, "avg_compression_ratio"] * np.log(x.loc[ reference_format, "wavg_encode_time"] * 1000) / np.log( x.wavg_encode_time * 1000)) final_data.sort_values("weissman_score", ascending=False, inplace=True) results_file = path + "/" + os.path.basename(path) + ".lossless.out" final_data.to_csv(results_file, sep=":") file = open(path + "/" + os.path.basename(path) + ".lossless.md", "w") markdown_writer = pytablewriter.MarkdownTableWriter() markdown_writer.from_dataframe(final_data) markdown_writer.stream = six.StringIO() markdown_writer.write_table() file.write(markdown_writer.stream.getvalue()) file.close() print( "Lossless results file successfully saved to {}.".format(results_file))
def convert(filename, from_ext, to_ext): input_file = f"{filename}.{from_ext}" writer = pytablewriter.MarkdownTableWriter() writer.from_csv(input_file) with open(f"{filename}.{to_ext}", "w") as f: writer.stream = f writer.write_table()
def merge_markdown_tables(input1, input2, title): res = StringIO() writer = pytablewriter.MarkdownTableWriter() writer.stream = res writer.table_name = title writer.header_list = ["Maindeck Options", "Sideboard Options"] writer.value_matrix = [[input1, input2]] writer.write_table() return res
def render(self): print('Render reconstruction table...') values = [v['loss'] for v in self.values()] markdown_table = pytablewriter.MarkdownTableWriter( table_name='Reconstruction Results', headers=list(self.keys()), value_matrix=[values]) markdown_file = self._get_output_path() markdown_table.dump(markdown_file)
def df2md(df, precision=2, tablename='table_name', ncol2bold=1): dft = df.reset_index().round(precision).copy() for i in range(ncol2bold): dft.iloc[:, i] = dft.iloc[:, i].apply(lambda x: '**' + x + '**') writer = pytablewriter.MarkdownTableWriter() writer.header_list = list(dft.columns.values) writer.from_dataframe(dft) writer.write_table()
def convert(filename, from_ext, to_ext): # provide index for the md file helpers.add_index_col(filename) indexed_filename = f"{filename}_temp.{from_ext}" writer = pytablewriter.MarkdownTableWriter() writer.from_csv(indexed_filename) with open(f"./{filename}.{to_ext}", "w") as f: writer.table_name = "Internships" writer.stream = f writer.write_table()
def generate_table(title, files): res = StringIO() writer = pytablewriter.MarkdownTableWriter() writer.stream = res writer.table_name = title # writer.header_list = #list(link_dictionary.keys()) val = 1 while (ceil(len(files) / val) > 5): val += 1 writer.value_matrix = list(zip_longest(*list(chunks(files, val)))) writer.write_table() return res
def kfold_test(): mls = ['dt', 'rf'] for window in windows: writer = pytablewriter.MarkdownTableWriter() writer.table_name = 'KFold validation' writer.header_list = ['File', 'Decision Tree', 'Random Forest', 'Tensorflow'] value_matrix = [] for name in binet_files: values = [name] feature, label = get_feature_labels(get_saved_data(window, name)) feature = feature[:int(len(feature) * 10)] label = feature[:int(len(label) * 10)] kf = KFold(n_splits=10) # feature = mask_features(feature) for ml in mls: scores = [] pr_scores = [] for train, test in kf.split(feature): clf = get_classifier(ml) xtrain, ytrain = feature[train], label[train] xtest, ytest = feature[test], label[test] clf.fit(xtrain, ytrain) test_predicts = clf.predict(xtest) test_score = accuracy_score(ytest, test_predicts) scores.append(test_score) proba = clf.predict_proba(xtest) precision, recall, pr_thresholds = precision_recall_curve( ytest, proba[:, 1]) pr_scores.append(auc(recall, precision)) values.append('{0:.4f}, {1:.4f}, {2:.4f}, {3:.4f}'.format( np.mean(scores), np.std(scores), np.mean(pr_scores), np.std(pr_scores))) kf = KFold(n_splits=10) accuracy = [] # , precision, recall = [], [], [] for train_index, test_index in kf.split(feature): x_train, x_test = feature[train_index], feature[test_index] y_train, y_test = label[train_index], label[test_index] c, p, r = \ keras_train_and_test(x_train, y_train, x_test, y_test, dimension=12) accuracy.append(c) # precision.append(p) # recall.append(r) values.append('{0:.4f}, {1:.4f}'.format(np.mean(accuracy), np.std(accuracy))) value_matrix.append(values) writer.value_matrix = value_matrix writer.write_table()
def write_mdtable(df): '''Convert pandas dataframe to markdown''' writer = pytablewriter.MarkdownTableWriter() writer.table_name = "example_table" writer.header_list = [ 'Index', 'Cluster 1: Count', 'Cluster 1: Total%', 'Cluster 2: Count', 'Cluster 2: Total%', 'Cluster 3: Count', 'Cluster 3: Total%', 'Cluster 4: Count', 'Cluster 4: Total%' ] df.reset_index(level=0, inplace=True) # writer.header_list = df.columns.tolist() writer.value_matrix = df.values.tolist() print(writer.write_table())
def main(): with open(filename, "w", encoding="utf8") as f: f.write( dedent("""\ "i","f","c","if","ifc","bool","inf","nan","mix_num","time" 1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00" 2,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00" 3,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00" """)) writer = pytablewriter.MarkdownTableWriter() writer.from_csv(filename) writer.write_table()
def generate_markdown_table_mainside(link_dictionary, title): res = StringIO() writer = pytablewriter.MarkdownTableWriter() writer.stream = res writer.table_name = title summation = {} for cur in link_dictionary: nums = [x.split(" ")[0] for x in link_dictionary[cur]] summation[cur] = sum(int(x) for x in nums) writer.header_list = list( [f"{x} ({summation[x]})" for x in link_dictionary.keys()]) writer.value_matrix = list(zip_longest(*list(link_dictionary.values()))) writer.write_table() return res
def main(): df = pd.read_csv( io.StringIO( dedent("""\ "i","f","c","if","ifc","bool","inf","nan","mix_num","time" 1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00" 22,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00" 333,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00" """)), sep=",", ) writer = pytablewriter.MarkdownTableWriter(dataframe=df) writer.write_table()