Advertisement
Guest User

Untitled

a guest
Mar 21st, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | Source Code | 0 0
  1. import pandas as pd
  2. from rcounting import thread_navigation as tn, plots
  3. from rcounting.counters import apply_alias
  4. from pathlib import Path
  5. import click
  6.  
  7. CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
  8.  
  9.  
  10. @click.command(context_settings=CONTEXT_SETTINGS)
  11. @click.argument("comment_id")
  12. @click.option(
  13.     "-l",
  14.     "--length",
  15.     default=1000,
  16.     help="An upper bound for the run length. Used to avoid fetching more comments than necessary",
  17. )
  18. @click.option(
  19.     "-f",
  20.     "--filename",
  21.     type=click.Path(path_type=Path),
  22.     help="What file to save the histogram to. If none is specified, `histogram.png` is used",
  23. )
  24. @click.option(
  25.     "--seaborn/--matplotlib",
  26.     default=False,
  27.     help="Use the default matplotlib style for the plot, or seaborn.",
  28. )
  29. def analyse_run(comment_id, length, seaborn, filename):
  30.     """Generate a histogram of the run of counts ending in COMMENT_ID.
  31.  
  32.    Load at least LENGTH comments, and then restrict the comments to only the
  33.    latest volley between the two most active counters in the loaded comments.
  34.    """
  35.     if seaborn:
  36.         import seaborn as sns
  37.  
  38.         sns.set_theme()
  39.     comments = pd.DataFrame(
  40.         tn.fetch_comments(comment_id, limit=length if length < 1000 else None)
  41.     )
  42.     comments["dt"] = comments["timestamp"].diff()
  43.     comments["username"] = comments["username"].apply(apply_alias)
  44.     if comments["username"].nunique() > 2:
  45.         counters = comments["username"].value_counts().head(2).index
  46.         comments = comments.iloc[
  47.             comments.query("username not in @counters").index[-1] + 1 :
  48.         ].copy()
  49.     fig = plots.speedrun_histogram(comments, n=2)
  50.     if filename is None:
  51.         filename = "histogram.png"
  52.     fig.savefig(filename, bbox_inches="tight")
  53.     print(
  54.         comments.loc[comments["dt"] <= 10]
  55.         .groupby("username")["dt"]
  56.         .describe()
  57.         .to_markdown()
  58.     )
  59.  
  60.  
  61. if __name__ == "__main__":
  62.     analyse_run()
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement