示例#1
0
def get_report_comparisation_table(reports, score_attrs=SCORE_R2S):
    """ Returns a formatted table which compares a list of reports by one or more attributes.

    Args:
        reports (list[Report]): The reports to compare
        score_attrs (list[str]|str): The attribute (or list of attributes) to compare. Use the SCORE_X constants.

    Returns:
        (Table): A table with the data.
    """
    if type(score_attrs) != list:
        score_attrs = [score_attrs]
    multiple_attrs = len(score_attrs) > 1

    headers = []
    if multiple_attrs:
        headers += [""]
    headers += [report.label for report in reports]

    compare_table = [headers]
    for score_attr in score_attrs:
        values = []
        if multiple_attrs:
            values.append(score_attr)
        values += [
            _format_float(getattr(report, score_attr)) for report in reports
        ]
        compare_table.append(values)
    table = Table(compare_table)

    if multiple_attrs:
        table.title = "Comparisation"
    else:
        table.title = _score_attr_to_string(score_attrs[0]) + " comparisation"
    return table
示例#2
0
def get_confusion_matrix(ground_truth, predicted, categories=None, label=None):
    if categories is None:
        categories = [0, 1, 2, 4]
    cat_count = len(categories)
    true_cats = [
        categories.index(get_category(truth, categories))
        for truth in ground_truth
    ]
    predicted_cats = [
        categories.index(get_category(pred, categories)) for pred in predicted
    ]

    cm = __get_confusion_matrix(ground_truth, predicted, categories)

    cat_strings = get_category_strings(categories)

    confusion_table_data = [
        ['Prediction ->'] +
        ['cat ' + cat_strings[i] for i in range(cat_count)]
    ]
    for i in range(cat_count):
        confusion_table_data.append(['cat ' + cat_strings[i]] +
                                    [str(x) for x in cm[i, :].tolist()])
    confusion_table = Table(confusion_table_data)
    confusion_table.title = "Confusion matrix"
    if label:
        confusion_table.title += ": " + label

    report = "Classification report"
    if label:
        report += ": " + label
    report += "\n" + metrics.classification_report(
        true_cats, predicted_cats, labels=cat_strings)

    return confusion_table, report
示例#3
0
    def __str__(self):
        output_data = [["Value", "Description", "Info"]]

        if self.evs is not None:
            output_data.append([
                _format_float(self.evs), "Explained variance score",
                "Best is 1.0, lower is worse"
            ])
        if self.mse is not None:
            output_data.append([
                _format_float(self.mse), "Mean squared error",
                "Best is 0.0, higher is worse"
            ])
        if self.mae is not None:
            output_data.append([
                _format_float(self.mae), "Mean absolute error",
                "Best is 0.0, higher is worse"
            ])
        if self.mde is not None:
            output_data.append([
                _format_float(self.mde), "Median absolute error",
                "Best is 0.0, higher is worse"
            ])
        if self.r2s is not None:
            output_data.append([
                _format_float(self.r2s), "R2 Score",
                "Best is 1.0, lower is worse"
            ])
        table = Table(output_data)
        table.title = "Report"
        if self.label:
            table.title += ": " + self.label
        return table.table
示例#4
0
async def print_outdated(
        outdates: List[Awaitable[OutdateResult]],
        quiet: bool,
        dates: bool
    ):
    colorama.init()
    if dates:
        data = [["Name", "Installed", "Wanted", "", "Latest", ""]]
    else:
        data = [["Name", "Installed", "Wanted", "Latest"]]
    count = 0
    for count, outdate in enumerate(outdates, 1):
        row = make_row(await outdate, dates)
        if row:
            data.append(row)

    if not count:
        print(colored("No requirements found.", "red"))
        return

    if len(data) == 1:
        print(colored("Everything is up-to-date!", "cyan", attrs=["bold"]))
        return

    print(colored("Red = unavailable/outdated/out of version specifier", "red", attrs=["bold"]))
    print(colored("Green = updatable", "green", attrs=["bold"]))
    table = Table(data)
    print(table.table)
    if not quiet:
        sys.exit(1)
示例#5
0
文件: pgpass.py 项目: disco0/public
 def show(self):
     table = Table([["connection name", "pgpass key", "managed", "age"]] +
                   [[
                       entry.pgpass_key or "<unknown>",
                       entry.name or "<n/a>",
                       entry.managed,
                       entry.human_age,
                   ] for entry in self._data])
     print(table.table)
示例#6
0
 def format_search_results(self, search_result: List[TVMazeShow]) -> str:
     """Formats as table API search results"""
     data = []
     data.append(['ID', 'Name', 'Premiered', 'Status' 'URL'])
     for show in search_result:
         data.append([
             str(show.id), show.name, show.premiered, show.status, show.url
         ])
     return Table(data, title='Search Results').table
示例#7
0
def ls_property():
    '''Get property list'''
    try:
        lib_cli = lib.Cli()
        properties = lib_cli.ls_properties()
        properties_list = [(k, v) for k, v in properties.items()]
        header = ['Name', 'Value']
        data = [header] + properties_list
        table = Table(data)
        click.secho(table.table)
    except exc.CliException as ex:
        click.secho('{}: {}'.format(ex.__class__.__name__, ex), fg='red')
示例#8
0
def ls_nlu(long=False):
    '''List NLU integrations'''
    try:
        lib_cli = lib.Cli()
        nlus = lib_cli.ls_nlus(long)
        header = ['NLU']
        if long:
            header.append('Credentials')
        data = [header] + nlus
        table = Table(data)
        click.secho(table.table)
    except exc.CliException as ex:
        click.secho('{}: {}'.format(ex.__class__.__name__, ex), fg='red')
示例#9
0
def ls_channel(long=False):
    '''List channels of current project'''
    try:
        lib_cli = lib.Cli()
        channels = lib_cli.ls_channel(long)
        header = ['Channel']
        if long:
            header.append('Credentials')
        data = [header] + channels
        table = Table(data)
        click.secho(table.table)
    except exc.CliException as ex:
        click.secho('{}: {}'.format(ex.__class__.__name__, ex), fg='red')
示例#10
0
 def completed_shows_table(self, shows: List[Show]) -> str:
     """formats list of completed shows as a table"""
     data = []
     data.append(['ID', 'Name', 'Premiered', 'Status'])
     for show in shows:
         data.append([
             str(show['id']),
             show['name'],
             show['premiered'],
             show['status'],
         ])
     title = 'Completed shows'
     return str(Table(data, title=title).table)
示例#11
0
 def format_unwatched(self, episodes: List[DecoratedEpisode]) -> str:
     """Formats unwatched episodes as table"""
     data = []
     data.append(['ID', 'Show', 'S', 'E', 'Name', 'Aired', 'Watched'])
     for episode in episodes:
         data.append([
             str(episode['id']), episode['show_name'],
             'S{season:0>2}'.format(season=episode['season']),
             'E{episode:0>2}'.format(episode=episode['number']),
             episode['name'], episode['airdate'], episode['watched']
         ])
     title = 'Episodes to watch'
     return str(Table(data, title=title).table)
示例#12
0
def ls(long=False):
    '''List projects'''
    try:
        lib_cli = lib.Cli()
        projects = lib_cli.ls(long)
        header = ['Project']
        if long:
            header += ['Status', 'Created']
        data = [header] + projects
        table = Table(data)
        click.secho(table.table)
        click.secho('You have {} projects'.format(len(projects)))
    except exc.CliException as ex:
        click.secho('{}: {}'.format(ex.__class__.__name__, ex), fg='red')
示例#13
0
def get_config_table():
    config_attrs = [
        attr for attr in dir(Config)
        if not callable(getattr(Config, attr)) and  # No functions please
        not attr.startswith("_") and  # No internal/private attributes
        not attr.isupper() and  # No constants
        not attr.startswith('database')
        and  # Database config is not so relevant
        not attr.startswith('reporting')  # Reporting config is not so relevant
    ]

    table_data = [["Attribute", "Value"]
                  ] + [[attr, str(getattr(Config, attr))]
                       for attr in config_attrs]
    table = Table(table_data)
    table.title = "Configuration"
    return table
示例#14
0
 def summary_table(self, month_totals: Dict[str, Dict[str, int]]) -> str:
     """Formats summary as a table"""
     data = []
     data.append([
         'Month',
         'Episodes',
         'Minutes',
     ])
     for date in sorted(month_totals.keys()):
         data.append([
             date,
             str(month_totals[date]['episodes']),
             str(month_totals[date]['minutes']),
         ])
     title = 'Watchtime per month'
     table = Table(data, title=title)
     table.justify_columns[1] = 'right'
     table.justify_columns[2] = 'right'
     return str(table.table)
示例#15
0
def get_category_table(ground_truth, predicted, categories=None, label=None):
    if categories is None:
        categories = [0, 1, 2, 4]
    hits = {}
    misses = {}
    for value_pair in zip(ground_truth, predicted):
        true_category = get_category(value_pair[0], categories)
        predicted_category = get_category(value_pair[1], categories)

        if true_category == predicted_category:
            hits[true_category] = hits.get(true_category, 0) + 1
        else:
            misses[true_category] = misses.get(true_category, 0) + 1

    table_data = [["Category", "Hits", "Misses", "Total"]]
    cat_strings = get_category_strings(categories)
    for i, category in enumerate(categories):
        hit_count = hits.get(category, 0)
        miss_count = misses.get(category, 0)

        cat_str = cat_strings[i]

        record = [
            cat_str,
            str(hit_count),
            str(miss_count),
            str(hit_count + miss_count)
        ]
        table_data.append(record)
    total_hits = sum(hits.values())
    total_misses = sum(misses.values())
    table_data.append([
        "Total",
        str(total_hits),
        str(total_misses),
        str(total_hits + total_misses)
    ])
    table = Table(table_data)
    table.title = "Categoric rating"
    if label:
        table.title += ": " + label
    table.inner_footing_row_border = True
    return table
示例#16
0
def pretty_table(data,
                 filters=None,
                 locations=None,
                 sort_keys=None,
                 sort_reverse=False):
    headers = []
    for item in data:
        keys = list(item.keys())
        headers = list(set(headers + keys))
    if filters is not None:
        assert isinstance(filters, list)
        headers = [header for header in headers if header not in filters]
    if locations is not None:
        assert isinstance(locations, list)
        valid_locations = [
            location for location in locations if location in headers
        ]
        headers_with_location = valid_locations
        headers_with_location.extend(
            [header for header in headers if header not in valid_locations])
        headers = headers_with_location

    rows = []
    for item in data:
        assert isinstance(item, dict)
        row = []
        for header in headers:
            row.append(item.get(header, ''))
        rows.append(row)
    if sort_keys is not None:
        assert isinstance(sort_keys, list)
        indexs = [headers.index(key) for key in sort_keys if key in headers]
        rows.sort(key=itemgetter(*indexs), reverse=sort_reverse)
    table_data = list()
    table_data.append(headers)
    table_data.extend(rows)
    table = Table(table_data)
    return table.table
示例#17
0
def get_top_features_table(model, features, n):
    """ Returns a formatted table which lists the n most-weighted feature of a model.

    Note that this is not applicable with all model types. SVR models for example, doesn't offer the coef_ attribute, as
    they use a kernel trick.
    Also, if polynomial features where used, the coef_s, while avaiable, will not be able to be mapped to the features
    from the feature list.

    Args:
        model: A learned model.
        features (list[str]): A list of feature IDs.
        n (int): How many features should be displayed.

    Returns:
        (Table): A table with the data.
    """
    logging.debug("Calculating top features.")
    for step in model.steps:
        _model = step[1]
        logging.debug("Trying to get top features from step " + step[0])
        if hasattr(_model, 'coef_'):
            try:
                logging.debug("Step %s has %i coefficients." %
                              (step[0], len(_model.coef_)))
                sorted_enum = sorted(enumerate(_model.coef_),
                                     key=lambda x: abs(x[1]),
                                     reverse=True)
                n = min(n, len(sorted_enum))

                table_data = [["Coefficient", "Feature"]]
                for idx, coef in sorted_enum[:n]:
                    table_data.append([_format_float(coef), features[idx]])
                table = Table(table_data)
                table.title = "Top weighted features"
                return table
            except:
                pass
    return None
示例#18
0
 def format_episodes(self, show: Show, episodes: List[Episode]) -> str:
     """Formats as table list of episodes"""
     title = '({id}) {name} - {premiered}'.format(
         id=show['id'], name=show['name'], premiered=show['premiered'])
     data = self._get_episodes_data(episodes)
     return str(Table(data, title=title).table)
示例#19
0
def get_printable_table(table):
    return Table(table[0]).table + '\n' + table[1]
示例#20
0
def report_table(ctx, nonhuman_header, human_header, data_rows, title,
                 **table_options):
    # don't align numbers for non-human reporting
    if ctx.args.table in ('csv', 'tsv', 'ssv'):
        data_rows = [[_to_string(ctx, v) for v in row] for row in data_rows]

        delim = {'csv': ',', 'tsv': '\t', 'ssv': ' '}[ctx.args.table]
        writer = csv.writer(sys.stdout, quoting=csv.QUOTE_MINIMAL,
                            delimiter=delim)
        with redirect_stdout(ctx.args.outfile):
            writer.writerow(nonhuman_header)
            for row in data_rows:
                writer.writerow(row)
        return

    # align numbers on the decimal point
    def get_whole_digits(n):
        if isinstance(n, int):
            return len(str(n))
        if isinstance(n, float):
            return get_whole_digits(int(n + 0.5))
        return 0

    whole_digits = [max(map(get_whole_digits, values))
                    for values in zip(*data_rows)]

    def pad(n_string, n, col):
        if isinstance(n, int):
            return ' ' * (whole_digits[col] - len(n_string)) + n_string
        if isinstance(n, float):
            digits = n_string.find('.')
            if digits == -1:
                digits = len(n_string)
            return ' ' * (whole_digits[col] - digits) + n_string
        return n_string

    # stringify data
    data_rows = [[pad(_to_string(ctx, v), v, col) for col, v in enumerate(row)]
                 for row in data_rows]

    # print human-readable table
    if ctx.args.table == 'fancy':
        from terminaltables import SingleTable as Table
    else:
        assert ctx.args.table == 'ascii'
        from terminaltables import AsciiTable as Table

    table = Table([human_header] + data_rows, ' %s ' % title)
    table.inner_column_border = False
    table.padding_left = 0

    for kw, val in table_options.items():
        if isinstance(val, dict):
            attr = getattr(table, kw)
            if isinstance(val, dict):
                attr.update(val)
            else:
                assert isinstance(attr, list)
                for index, elem in val.items():
                    attr[index] = elem
        else:
            setattr(table, kw, val)

    with redirect_stdout(ctx.args.outfile):
        print(table.table)