def get_table(results: OrderedDict[str, np.ndarray], style: Optional[str] = None, **kwargs) -> Tuple[Sequence[Tuple[str, Any]], bt.BeautifulTable]: headers = [] columns = [] uniform_items = [] for header, column in results.items(): c0 = column[0] if np.all(column == c0): uniform_items.append((header, c0)) else: headers.append(header) if column.dtype == np.float64: column = column_to_string(column, **kwargs) columns.append(column) terminal_shape = shutil.get_terminal_size() table = bt.BeautifulTable(maxwidth=terminal_shape[0], default_alignment=bt.ALIGN_RIGHT) if style is not None: table.set_style(get_style(style)) table.columns.header = headers for row in zip(*columns): table.rows.append(row) uniform_header, uniform_row = zip(*uniform_items) uniform_table = bt.BeautifulTable(maxwidth=terminal_shape[0], default_alignment=bt.ALIGN_RIGHT) if style is not None: uniform_table.set_style(get_style(style)) uniform_table.columns.header = uniform_header uniform_table.rows.append(uniform_row) return uniform_table, table
def federation_statistics_table(cls, timer_aggregator: _TimerItem = None): total = _TimerItem() get_table = beautifultable.BeautifulTable(110) get_table.columns.header = ["name", "n", "sum(s)", "mean(s)", "max(s)"] for name, item in cls._GET_STATS.items(): get_table.rows.append([name, *item.as_list()]) total.union(item) get_table.rows.sort("sum(s)", reverse=True) get_table.border.left = '' get_table.border.right = '' get_table.border.bottom = '' get_table.border.top = '' remote_table = beautifultable.BeautifulTable(110) remote_table.columns.header = ["name", "n", "sum(s)", "mean(s)", "max(s)"] for name, item in cls._REMOTE_STATS.items(): remote_table.rows.append([name, *item.as_list()]) total.union(item) remote_table.rows.sort("sum(s)", reverse=True) remote_table.border.left = '' remote_table.border.right = '' remote_table.border.bottom = '' remote_table.border.top = '' base_table = beautifultable.BeautifulTable(120) base_table.rows.append(["get", get_table]) base_table.rows.append(["remote", remote_table]) base_table.rows.append(["total", total]) if timer_aggregator: timer_aggregator.union(total) return base_table.get_string()
def table_data(request_data: List[Dict[str, Union[int, float, str]]]) -> str: # Create the header header = beautifultable.BeautifulTable(max_width=1000, default_alignment=beautifultable.ALIGN_LEFT) header.set_style(beautifultable.STYLE_MARKDOWN) header.column_headers = ["#", "Requests", "Bandwidth", "cdnjs Resource URL"] header.append_row(["", "", "", ""]) # Create the base table base_table = beautifultable.BeautifulTable(max_width=1000, default_alignment=beautifultable.ALIGN_LEFT) base_table.set_style(beautifultable.STYLE_MARKDOWN) base_table.detect_numerics = False # This breaks the formatting # Sort the data request_data = sorted(request_data, key=lambda x: x["requests"], reverse=True) # Generate the table for i, row in enumerate(request_data): try: base_table.append_row([ "{:,}".format(i + 1), "{:,}".format(row["requests"]), "{:,.2f}".format(row["bandwidth"]), "[{0}](https://{0})".format(row["resource"]) ]) except: continue # Export header + table base_table.column_alignments[2] = beautifultable.ALIGN_RIGHT header = str(header) base_table = str(base_table) return header[:header.rfind("\n")] + "\n" + base_table
def info(ctx: click.Context, countries: list, is_excluded: bool): """Display ipvanish vpn server status""" try: if not os.path.exists(os.path.join(SETTINGS["IPVANISH_PATH"], "auth")): raise IpvanishError("Auth credentials not configured. Please run commands auth") vpns = _get_vpns(countries, is_excluded) vpns.sort() table = beautifultable.BeautifulTable(max_width=180) table.set_style(beautifultable.STYLE_BOX_ROUNDED) table.column_headers = [ "Server", "City", "Country", "Region", "Ping", "Capacity", ] for vpn in vpns: table.append_row( [vpn.server, vpn.city, vpn.country, vpn.region, vpn.ping, vpn.capacity] ) click.echo(table) except IpvanishError as e: click.echo(f"[IpvanishError] {e}", file=sys.stderr) except Exception: click.echo(traceback.print_exc(), file=sys.stderr)
def format_frontend_image_table( list: List[FrontendImageInfo], ) -> beautifultable.BeautifulTable: table = beautifultable.BeautifulTable() table.column_headers = [ "VM Name", "Backend Image", "Disk Num", "Real Size", "Virt Size", ] table.set_style(beautifultable.BeautifulTable.STYLE_BOX) table.column_alignments[ "VM Name"] = beautifultable.BeautifulTable.ALIGN_LEFT table.column_alignments[ "Backend Image"] = beautifultable.BeautifulTable.ALIGN_LEFT table.column_alignments[ "Disk Num"] = beautifultable.BeautifulTable.ALIGN_RIGHT table.column_alignments[ "Real Size"] = beautifultable.BeautifulTable.ALIGN_RIGHT table.column_alignments[ "Virt Size"] = beautifultable.BeautifulTable.ALIGN_RIGHT for image in list: if image.backend is None: backend_identifier = "--NOT FOUND--" else: backend_identifier = image.backend.identifier table.append_row([ image.vm_name, backend_identifier, image.disk_number, utils.format_bytes(image.actual_size), utils.format_bytes(image.virtual_size), ]) return table
def hamamatsu_dump(ctx, table_style, max_width): """dump hamamatsu properties""" camera = ctx.obj["camera"] import beautifultable table = beautifultable.BeautifulTable(maxwidth=max_width) style = getattr(table, "STYLE_" + table_style.upper()) table.set_style(style) table.columns.header = ["Name", "Value", "Unit", "DType"] def rep(x): if isinstance(x, enum.Enum): return x.name return x pmap = {} for prop in camera.values(): row = prop["name"], rep(prop.value), rep(prop.unit), rep( prop.dtype).lstrip("TYPE_").lower() pmap[row[0]] = row for name in sorted(pmap): table.rows.append(pmap[name]) click.echo(table)
def df_to_table(df, columns=None, colors=True): table = beautifultable.BeautifulTable(max_width=160) table.column_headers = [df.index.name] + (columns or list(df.columns)) df = df.reset_index().copy() reached_target = df["subscribed"] == df["targets"] exceeded_target = df["subscribed"] > df["targets"] missing_candidates = df["available"] < df["to_draw"] too_many_actives = df["active"] > df["adjusted"] issue = missing_candidates | exceeded_target | too_many_actives | missing_candidates if colors: color_to(df, "college", issue, RED) color_to(df, "college", reached_target & ~issue, GREEN) color_to(df, "available", missing_candidates, RED) color_to(df, "subscribed", reached_target, GREEN) color_to(df, "subscribed", exceeded_target, RED) color_to(df, "active", too_many_actives, RED) color_to(df, "to_draw", missing_candidates, RED) if columns is not None and len(columns) > 0: df = df[["college"] + columns] for tup in df.itertuples(index=False): table.append_row(tup) return table
def detector_table(detectors): import beautifultable width = click.get_terminal_size()[0] table = beautifultable.BeautifulTable(maxwidth=width) table.columns.header = [ "ID", "Vendor", "Model", "S/N", "Bus", "Version", "Driver", "Module", "API", "Series", ] for i, detector in enumerate(detectors): info = collections.defaultdict(str) info.update(detector.info) row = [ i, info[EIDString.VENDOR], info[EIDString.MODEL], info[EIDString.CAMERAID], info[EIDString.BUS], info[EIDString.CAMERAVERSION], info[EIDString.DRIVERVERSION], info[EIDString.MODULEVERSION], info[EIDString.DCAMAPIVERSION], info[EIDString.CAMERA_SERIESNAME], ] table.rows.append(row) return table
def show(limit, concurrency): click.echo(hue.bg(hue.cyan( f'Crawling {limit} city(ies) with concurrency {concurrency}'))) cities_links = get_google_search_cities_links(limit=limit) google_climatempo_scraper = GoogleClimaTempoCityLinkScraper(driver_name='phantomjs') google_climatempo_crawler = Crawler( scraper=google_climatempo_scraper, concurrency=concurrency) google_climatempo_crawler.crawl(cities_links) climatempo_links = google_climatempo_crawler.iterdata() climatempo_metric_scraper = ClimaTempoMetricScraper(driver_name='phantomjs') climatempo_metric_crawler = Crawler( scraper=climatempo_metric_scraper, concurrency=concurrency) climatempo_metric_crawler.crawl(climatempo_links) climatempo_metrics = climatempo_metric_crawler.iterdata() table = beautifultable.BeautifulTable(max_width=120) table.column_headers = [ 'City', 'State', 'Month', 'Min. Temp. (˚C)', 'Max. Temp. (˚C)', 'Rain (mm)', ] for metric in climatempo_metrics: table.append_row([ metric.city, metric.state, metric.month, metric.min_temp, metric.max_temp, metric.rain, ]) print(table)
def terminal_table(self): tb = beautifultable.BeautifulTable() cells = list(COLUMN_HEADERS) self.config.hook.pytest_aggreport_terminal_table_header(cells=cells) tb.columns.header = cells for result in self.case_reports.values(): tb.rows.append(result.terminal_table_row) return tb
def computing_statistics_table(cls): stack_table = beautifultable.BeautifulTable(110, precision=4) stack_table.set_style(beautifultable.STYLE_BOX_ROUNDED) stack_table.columns.header = [ "function", "n", "sum(s)", "mean(s)", "max(s)", "stack_hash", "stack" ] stack_table.columns.alignment["stack"] = beautifultable.ALIGN_LEFT stack_table.columns.header.alignment = beautifultable.ALIGN_CENTER stack_table.border.left = '' stack_table.border.right = '' stack_table.border.bottom = '' stack_table.border.top = '' function_table = beautifultable.BeautifulTable(110) function_table.set_style(beautifultable.STYLE_COMPACT) function_table.columns.header = [ "function", "n", "sum(s)", "mean(s)", "max(s)" ] aggregate = {} total = _TimerItem() for hash_id, timer in cls._STATS.items(): stack_table.rows.append([ timer.function_name, *timer.item.as_list(), hash_id, timer.function_stack ]) aggregate.setdefault(timer.function_name, _TimerItem()).union(timer.item) total.union(timer.item) for function_name, item in aggregate.items(): function_table.rows.append([function_name, *item.as_list()]) detailed_base_table = beautifultable.BeautifulTable(120) stack_table.rows.sort("sum(s)", reverse=True) detailed_base_table.rows.append(["stack", stack_table]) detailed_base_table.rows.append(["total", total]) base_table = beautifultable.BeautifulTable(120) function_table.rows.sort("sum(s)", reverse=True) base_table.rows.append(["function", function_table]) base_table.rows.append(["total", total]) return base_table.get_string(), detailed_base_table.get_string()
def table(self, df): rs = self.execute(df) import beautifultable table = beautifultable.BeautifulTable() for row in rs: table.append_row(row) rs.close() return str(table)
def detector_table(detectors): import beautifultable table = beautifultable.BeautifulTable() table.columns.header = 'Host', 'Alias(es)', 'Address(es)', 'Port', 'API' for detector in detectors: host, port, version = detector aliases = '\n'.join(host.aliases) addresses = '\n'.join(host.addresses) table.rows.append((host.name, aliases, addresses, port, version)) return table
def Table(**kwargs): style = kwargs.pop("style", beautifultable.Style.STYLE_BOX_ROUNDED) kwargs.setdefault("default_padding", 1) kwargs.setdefault("maxwidth", shutil.get_terminal_size().columns - 1) kwargs.setdefault( "default_alignment", beautifultable.Alignment.ALIGN_RIGHT) table = beautifultable.BeautifulTable(**kwargs) table.set_style(style) return table
def format_attr_table(self, data): term_width, term_height = click.get_terminal_size() table = beautifultable.BeautifulTable( maxwidth=term_width, default_alignment=beautifultable.ALIGN_LEFT) table.set_style(beautifultable.STYLE_NONE) for key, formatter, value in data: key = key + ":" if self.colorize: key = click.style(key, bold=True) table.rows.append([key, formatter.format(self, value)]) return str(table)
def show_devices(device_listing): table = beautifultable.BeautifulTable() table.default_alignment = beautifultable.ALIGN_LEFT table.max_table_width = 128 table.column_headers = ['id', 'identifier', 'device_class', 'device_type', 'version', 'address', 'mac_address', 'state', 'cqueue'] for device in device_listing: row = [] for col in table.column_headers: row.append(device[col]) if args.filter_list is not None: if device['state'] not in args.filter_list: continue table.append_row(row) print(table)
def show_opt82_infos(results): table = beautifultable.BeautifulTable() table.default_alignment = beautifultable.ALIGN_LEFT table.max_table_width = 128 table.column_headers = [ 'id', 'upstream_switch_mac', 'upstream_port_info', 'downstream_switch_mac', 'downstream_switch_name' ] for result in results: if 'downstream_switch_mac' not in result: result['downstream_switch_mac'] = None row = [] for col in table.column_headers: row.append(result[col]) table.append_row(row) print(table)
async def sql(self, ctx, *query: str): try: async with self.bot.db.acquire() as conn: async with conn.transaction(): results = await conn.fetch(" ".join(query)) table = beautifultable.BeautifulTable() if not results: return await ctx.send( "The SQL query returned nothing!! ") table.columns.header = list(results[0].keys()) for result in results: table.rows.append(result.values()) await ctx.send(f"```{table}```") except Exception as e: await ctx.send(f"Sql Statement failed due to \n {e}")
def format_backend_image_table( list: List[BackendImageInfo], ) -> beautifultable.BeautifulTable: table = beautifultable.BeautifulTable() table.column_headers = ["Image Name", "Real Size", "Virt Size"] table.set_style(beautifultable.BeautifulTable.STYLE_BOX) table.column_alignments[ "Image Name"] = beautifultable.BeautifulTable.ALIGN_LEFT table.column_alignments[ "Real Size"] = beautifultable.BeautifulTable.ALIGN_RIGHT table.column_alignments[ "Virt Size"] = beautifultable.BeautifulTable.ALIGN_RIGHT for image in list: table.append_row([ image.identifier, utils.format_bytes(image.actual_size), utils.format_bytes(image.virtual_size), ]) return table
def build_kg(df): ''' for each row create an edge for each: - paper -> author - paper -> tag ''' kg = [] author_set = set() tag_set = set() article_set = set() cpt_hasAuthor = cpt_hasTag = 0 for index, row in df.iterrows(): paper_id = row['id'] for author in row['author_list']: kg.append([paper_id, 'hasAuthor', author]) author_set.add(author) article_set.add(paper_id) cpt_hasAuthor += 1 for tag in row['tags_list']: kg.append([paper_id, 'hasTag', tag]) article_set.add(paper_id) tag_set.add(tag) cpt_hasTag += 1 kg = np.asarray(kg) # output KG stats table = bt.BeautifulTable() table.append_row(["# statements", kg.shape[0]]) table.append_row(["# relation type", 2]) table.append_row([" # hasAuthor relation", cpt_hasAuthor]) table.append_row([" # hasTag relation", cpt_hasTag]) table.append_row(["# entities of type Author", len(author_set)]) table.append_row(["# entities of type Papers", len(article_set)]) table.append_row(["# entities of type Tag", len(tag_set)]) table.column_alignments[0] = bt.ALIGN_LEFT table.column_alignments[1] = bt.ALIGN_RIGHT print(table) return kg, author_set
def table(self, df, limit=10): rs = self.execute(df) import beautifultable table = beautifultable.BeautifulTable() header = RelationalExecutor.__getHeader(rs) table.columns.header = header cnt = 0 for row in rs: if cnt > limit: break cnt += 1 table.rows.append(row) rs.close() return str(table)
def format_instance_table( instances: List[TransientInstance], ) -> beautifultable.BeautifulTable: table = beautifultable.BeautifulTable() table.column_headers = ["VM Name", "Start Time", "PID", "SSH Port"] table.set_style(beautifultable.BeautifulTable.STYLE_BOX) table.column_alignments[ "VM Name"] = beautifultable.BeautifulTable.ALIGN_LEFT table.column_alignments[ "Start Time"] = beautifultable.BeautifulTable.ALIGN_LEFT table.column_alignments["PID"] = beautifultable.BeautifulTable.ALIGN_RIGHT table.column_alignments[ "SSH Port"] = beautifultable.BeautifulTable.ALIGN_RIGHT for instance in instances: if instance.ssh_port is None: port = "N/A" else: port = str(instance.ssh_port) table.append_row( [instance.name, instance.start_time, instance.pid, port]) return table
def pp_table(table): """Pretty print a table. Arguments: - table (list) : list of rows, each a list of str. First row is taken as a header and remaining row as table body. """ btable = beautifultable.BeautifulTable() btable.column_headers = table[0] btable.row_separator_char = '' btable.intersection_char = '' btable.left_border_char = '' btable.right_border_char = '' left = beautifultable.BeautifulTable.ALIGN_LEFT for column in btable.column_headers: btable.column_alignments[column] = left map(btable.append_row, table[1:]) print(btable)
def __init__(self, domain: Domain, metrics: List[str], primary_metric: str = "", database_path: str = None): """Initialise the table reporter with domain and metrics. Args: domain: A :class:`Domain` from which all evaluated samples are drawn. metrics: :obj:`List[str]` with names of the metrics used during evaluation. primary_metric: (optional) :obj:`str` primary metric from `metrics`. This is used to determine the best sample. Defaults to the first one. database_path: (optional) :obj:`str` path to the database for storing experiment history on disk. Defaults to in-memory storage. """ super(Table, self).__init__(domain, metrics, primary_metric, database_path) self._table = bt.BeautifulTable() self._table.set_style(bt.STYLE_SEPARATED) dim_names = [".".join(dns) for dns in self.domain.flatten()] self._table.column_headers = ["No.", *dim_names, *self.metrics]
def format_table(self, headers, widths, row_formatter, rows): term_width, term_height = click.get_terminal_size() table = beautifultable.BeautifulTable( maxwidth=term_width, default_alignment=beautifultable.ALIGN_LEFT) table.columns.header = headers sum_width = None for width_set in widths: column_widths, expandable_index = width_set[:-1], width_set[-1] sum_width = sum(column_widths) + 6 # Table characters width if term_width > sum_width: table.columns.width = ( column_widths[:expandable_index] + [term_width - sum_width + column_widths[expandable_index] ] + column_widths[expandable_index + 1:]) break else: raise RuntimeError( "Terminal is too narrow (needed {}, got {})".format( sum_width, term_width)) for lines in table.stream(map(row_formatter, rows)): for line in lines.splitlines(): yield line + "\n"
def main(cls, args: argparse.Namespace) -> None: # Get experiment name experiment_name = args.experiment_name # Generate dynamic-width table try: terminal_columns = int(os.popen("stty size", "r").read().split()[1]) except IndexError: # stty size fails when run from outside proper terminal (eg in tests) terminal_columns = 100 table = beautifultable.BeautifulTable( max_width=min(100, terminal_columns), default_alignment=beautifultable.ALIGN_LEFT, ) table.set_style(beautifultable.STYLE_BOX_ROUNDED) def add_table_row(label, value): table.append_row([termcolor.colored(label, attrs=["bold"]), value]) # Constant for "not applicable" fields NA = termcolor.colored("N/A", "red") # Find checkpoint files checkpoint_paths = glob.glob( os.path.join(args.checkpoint_dir, f"{glob.escape(experiment_name)}-*.ckpt") ) # Display size, labels of checkpoints if len(checkpoint_paths) > 0: checkpoint_total_size = 0 checkpoint_labels = [] buddy = fannypack.utils.Buddy(experiment_name, verbose=False) paths, steps = buddy._find_checkpoints( args.checkpoint_dir, args.experiment_name ) for path in paths: prefix = os.path.join(args.checkpoint_dir, f"{experiment_name}-") suffix = ".ckpt" assert path.startswith(prefix) assert path.endswith(suffix) label = path[len(prefix) : -len(suffix)] checkpoint_labels.append(f"{label} (steps: {steps[path]})") checkpoint_total_size += _get_size(path) add_table_row("Total checkpoint size", _format_size(checkpoint_total_size)) add_table_row( "Average checkpoint size", _format_size(checkpoint_total_size / len(checkpoint_paths)), ) add_table_row("Checkpoint labels", "\n".join(checkpoint_labels)) else: add_table_row("Total checkpoint size", NA) add_table_row("Average checkpoint size", NA) add_table_row("Checkpoint labels", "") # Display log file size log_path = os.path.join(args.log_dir, f"{experiment_name}") if os.path.exists(log_path): # _delete(log_path, args.forever) add_table_row("Log size", _format_size(_get_size(log_path))) else: add_table_row("Log size", NA) # Display metadata + metadata size metadata_path = os.path.join(args.metadata_dir, f"{experiment_name}.yaml") if os.path.exists(metadata_path): add_table_row("Metadata size", _format_size(_get_size(metadata_path))) with open(metadata_path, "r") as f: add_table_row("Metadata", f.read().strip()) else: add_table_row("Metadata size", NA) add_table_row("Metadata", NA) # Print table print(experiment_name) print(table)
def main(cls, args: argparse.Namespace) -> None: # Last modified: checkpoints and metadata only # > We could also do logs, but seems high effort? timestamps: Dict[str, float] = {} # Count checkpoints for each experiment checkpoint_counts: Dict[str, int] = {} for file in _listdir(args.checkpoint_dir): # Remove .ckpt suffix if file[-5:] != ".ckpt": print(f"Skipping malformed checkpoint filename: {file}") continue trimmed = file[:-5] # Get experiment name parts = trimmed.split("-") if len(parts) != 2: print(f"Skipping malformed checkpoint filename: {file}") continue name = parts[0] # Update tracker if name not in checkpoint_counts.keys(): checkpoint_counts[name] = 0 checkpoint_counts[name] += 1 # Update timestamp mtime = os.path.getmtime(os.path.join(args.checkpoint_dir, file)) if name not in timestamps.keys() or mtime > timestamps[name]: timestamps[name] = mtime # Get experiment names from metadata files metadata_experiments = set() for file in _listdir(args.metadata_dir): # Remove .yaml suffix if file[-5:] != ".yaml": print(f"Skipping malformed metadata filename: {file}") continue name = file[:-5] metadata_experiments.add(name) # Update timestamp mtime = os.path.getmtime(os.path.join(args.metadata_dir, file)) if name not in timestamps.keys() or mtime > timestamps[name]: timestamps[name] = mtime # Get experiment names from log directories log_experiments = set(_listdir(args.log_dir)) # Generate dynamic-width table try: terminal_columns = int( os.popen("stty size", "r").read().split()[1]) except IndexError: # stty size fails when run from outside proper terminal (eg in tests) terminal_columns = 100 table = beautifultable.BeautifulTable( max_width=min(100, terminal_columns)) table.set_style(beautifultable.STYLE_BOX_ROUNDED) table.row_separator_char = "" # Add bolded headers column_headers = [ "Name", "Checkpoints", "Logs", "Metadata", "Last Modified", ] table.column_headers = [ termcolor.colored(h, attrs=["bold"]) for h in column_headers ] experiment_names = (set(checkpoint_counts.keys()) | log_experiments | metadata_experiments) for name in experiment_names: # Get checkpoint count checkpoint_count = 0 if name in checkpoint_counts: checkpoint_count = checkpoint_counts[name] # Get timestamp timestamp = "" if name in timestamps: timestamp = datetime.datetime.fromtimestamp( timestamps[name]).strftime( "%b %d, %Y @ %-H:%M" if terminal_columns > 100 else "%Y-%m-%d") # Add row for experiment yes_no = { True: termcolor.colored("Yes", "green"), False: termcolor.colored("No", "red"), } table.append_row([ name, checkpoint_count, yes_no[name in log_experiments], yes_no[name in metadata_experiments], timestamp, ]) # Print table, sorted by name print(f"Found {len(experiment_names)} experiments!") table.sort(table.column_headers[0]) print(table)
def main(cls, *, args: argparse.Namespace, paths: BuddyPaths) -> None: results = paths.find_experiments(verbose=True) # Generate dynamic-width table try: terminal_columns = int( os.popen("stty size", "r").read().split()[1]) except IndexError: # stty size fails when run from outside proper terminal (eg in tests) terminal_columns = 100 table = beautifultable.BeautifulTable( maxwidth=min(100, terminal_columns)) table.set_style(beautifultable.STYLE_BOX_ROUNDED) table.rows.separator = "" # Add bolded headers table.columns.header = [ termcolor.colored(h, attrs=["bold"]) for h in cls.table_column_headers ] # Constant for "not applicable" fields NA = termcolor.colored("N/A", "red") # Add experiment rows, oldest to newest sorted_experiment_names = sorted( results.experiment_names, key=lambda n: results.timestamps.get(n, 0.0), ) for name in sorted_experiment_names: # Get checkpoint count checkpoint_count = 0 if name in results.checkpoint_counts: checkpoint_count = results.checkpoint_counts[name] # Get timestamp timestamp = "" if name in results.timestamps: timestamp = datetime.datetime.fromtimestamp( results.timestamps[name]).strftime( "%b %d, %y @ %-H:%M" if terminal_columns > 100 else "%y-%m-%d") # Add row for experiment table.rows.append([ name, checkpoint_count, termcolor.colored( format_size(get_size(paths.get_log_dir(name)), short=True), "green", ) if name in results.log_experiments else NA, termcolor.colored( format_size(get_size(paths.get_metadata_file(name)), short=True), "green", ) if name in results.metadata_experiments else NA, timestamp, ]) # Print table, sorted by name print(f"Found {len(results.experiment_names)} experiments!") if args.sort_by is not None: # Sort-by field: lowercase -> index table.sort( list(map(lambda s: s.lower(), cls.table_column_headers)).index(args.sort_by)) print(table)
def build_kg(df, train_idx): ''' for each row create an edge for each: - paper -> author - paper -> tag ''' kg = [] author_set = set() tag_set = set() article_set = set() cpt_hasAuthor = cpt_hasTag = 0 for index, row in df.iterrows(): if index in train_idx: paper_id = row['id'] for author in row['author_list']: kg.append([paper_id, 'hasAuthor', author]) author_set.add(author) article_set.add(paper_id) cpt_hasAuthor += 1 for tag in row['tags_list']: kg.append([paper_id, 'hasTag', tag]) article_set.add(paper_id) tag_set.add(tag) cpt_hasTag += 1 kg = np.asarray(kg) # output KG stats table = bt.BeautifulTable() table.append_row(["# statements", kg.shape[0]]) table.append_row(["# relation type", 2]) table.append_row([" # hasAuthor relation", cpt_hasAuthor]) table.append_row([" # hasTag relation", cpt_hasTag]) table.append_row(["# entities of type Author", len(author_set)]) table.append_row(["# entities of type Papers", len(article_set)]) table.append_row(["# entities of type Tag", len(tag_set)]) table.column_alignments[0] = bt.ALIGN_LEFT table.column_alignments[1] = bt.ALIGN_RIGHT print(table) print("Loading the KG in Networkx") # create an id to subject/object label mapping set_nodes = set().union(kg[:, 0], kg[:, 2]) # save label dictionary to file node_to_idx = collections.OrderedDict(zip(set_nodes, range(len(set_nodes)))) idx_to_node = {v: k for k, v in node_to_idx.items()} # np.savetxt(os.path.join(args.outputdir, LABEL_FILE), idx_to_node, delimiter="\t", fmt="%s", encoding="utf-8") nx_G = nx.DiGraph() nx_G.add_nodes_from(range(len(set_nodes))) for s, p, o in kg: nx_G.add_edge(node_to_idx[s], node_to_idx[o], type=p, weight=1) G_undir = nx_G.to_undirected() print("Computing transition probabilities and simulating the walks") start_time = time.time() G = node2vec.Graph(G_undir, False, args.p, args.q) G.preprocess_transition_probs() walks = G.simulate_walks(args.num_walks, args.walk_length) print("Learning the embeddings and writing them to file") # print(walks[0]) walks = [list(map(lambda x: idx_to_node[x], walk)) for walk in walks] # convert each vertex id to a string # print(walks[0]) model = Word2Vec(walks, size=args.dimensions, window=args.window_size, min_count=0, sg=1, workers=args.workers, iter=args.iter) node_vectors = model.wv del model # get embeddings for paper nodes in train set node_paper_embed = [] for id in df.iloc[train_idx]["id"].values: node_paper_embed.append(node_vectors[id]) node_paper_embed = np.array(node_paper_embed) print(node_paper_embed.shape) elapsed_time = time.time() - start_time print("Node2vec algorithm took: {}".format( time.strftime("%Hh:%Mm:%Ss", time.gmtime(elapsed_time)))) return node_paper_embed
my_table.border.right_junction = RIGHT_X #my_table.BTColumnCollection.default_alignment = def error_file(): pass try: # CHECK IF FILE EXISTS if os.path.isfile(FNAME) and os.access(FNAME, os.R_OK): read_success() # CREATE TABLE my_dataframe = pandas.read_csv(FNAME, skip_blank_lines=True) headers = list(my_dataframe) lists = my_dataframe.values.tolist() my_table = beautifultable.BeautifulTable(maxwidth=WIDTH, default_padding=PADDING, headers=headers, default_alignment = beautifultable.ALIGN_LEFT ) # WRITE TO TABLE for item in lists: # my_table.append_row(item) my_table.rows.append(item) # OUTPUT RESULTS style_custom() # PRINT OUTPUT TO TERMINAL print(my_table) # CREATE NEW NAME FOR OUTPUT FILE FNAME = FNAME.split(".")[0] filename = f"{FNAME}.txt" # WRITE TO NEW FILE with open(filename, 'w') as f: print(my_table, file=f) f.close()