Skip to content

Commit

Permalink
Add statistics printing options
Browse files Browse the repository at this point in the history
  • Loading branch information
Michionlion committed Dec 8, 2021
1 parent 7e7b6ac commit 2eb1306
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
33 changes: 33 additions & 0 deletions mesi/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ def comparison_progressbar(lst: List, **kwargs):
return tqdm(lst, total=len(lst), unit="cmp", colour="blue", leave=False, **kwargs)


def print_divider(title=""):
"""Print a simple divider with an optional title."""
title = f" {title} " if title else ""
print(f"\n======{title}======\n")


def print_average_distance(
distances: Dict[Tuple[Path, Path], float], distance_threshold: float
):
"""Print the average of the distances.
Args:
distances (Dict[Tuple[Path, Path], float]): A dictionary of comparisons to distances.
distance_threshold (float): The threshold below which to count values.
"""
values = list(filter(lambda x: x <= distance_threshold, distances.values()))
avg = round(sum(values) / len(values), 2)

print(f"> Average distance: {avg}")


def print_distance_distribution(
distances: Dict[Tuple[Path, Path], float], distance_threshold: float
):
"""Print the distribution of the distances.
Args:
distances (Dict[Tuple[Path, Path], float]): A dictionary of comparisons to distances.
distance_threshold (float): The threshold below which to count values.
"""
print("> Distribution display is not yet supported")


def print_distances(
distances: Dict[Tuple[Path, Path], float],
distance_threshold: float,
Expand Down
15 changes: 15 additions & 0 deletions mesi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def main(
help=f"String distance algorithm to use [{', '.join(compare.DISTANCE_FUNCTIONS.keys())}]",
metavar="ALGORITHM",
),
average: bool = typer.Option(
False, help="Display the average of the computed distances"
),
distribution: bool = typer.Option(
False, help="Display the distribution of the computed distances"
),
):
"""Calculate the similarity between all possible pairs among the given files.
Expand All @@ -74,6 +80,15 @@ def main(
combinations, algorithm, ignore_whitespace
)
display.print_distances(distances, threshold, table_format, full_paths)

if average or distribution:
display.print_divider("Statistics")

if average:
display.print_average_distance(distances, threshold)

if distribution:
display.print_distance_distribution(distances, threshold)
except ValueError as err:
display.print_error(err)
raise typer.Exit(1)

0 comments on commit 2eb1306

Please sign in to comment.