Skip to content

Commit

Permalink
update leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
SkafteNicki committed Nov 25, 2024
1 parent a021306 commit e1163f7
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions tools/repo_stats/leaderboard.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import base64
import json
from io import BytesIO
from pathlib import Path

import numpy as np
import pandas as pd
import streamlit as st
from dotenv import load_dotenv
from google.cloud.storage import Client
from models import RepoStats
from PIL import Image

load_dotenv()

Expand All @@ -27,12 +31,50 @@ def load_data(file_name: str) -> pd.DataFrame:
return pd.DataFrame(repo_content_dicts)


def activity_to_image(activity_matrix: list[list[int]], scale_factor: int = 10) -> str:
"""
Convert an activity matrix (N, 24) into an RGB image scaled up by a given factor.
Args:
activity_matrix (list[list[int]]): A 2D list of activity values.
scale_factor (int): Factor by which to scale up the image size.
Returns:
str: Base64-encoded PNG image string in "data:image/png;base64," format.
"""
# Normalize the activity matrix to the range [0, 255].
array = np.array(activity_matrix, dtype=np.float32)
max_value = np.max(array)
if max_value > 0:
array = array / max_value * 255

# Create an RGB image: Green for activity, Black for no activity.
height, width = array.shape
rgb_array = np.zeros((height, width, 3), dtype=np.uint8)
rgb_array[:, :, 1] = array.astype(np.uint8) # Green channel

# Scale up the image by the scale factor.
scaled_height, scaled_width = height * scale_factor, width * scale_factor
image = Image.fromarray(rgb_array, mode="RGB")
image = image.resize((scaled_width, scaled_height), Image.NEAREST)

# Convert the image to a Base64 string.
buffer = BytesIO()
image.save(buffer, format="PNG")
buffer.seek(0)
image_base64 = base64.b64encode(buffer.read()).decode("utf-8")

return f"data:image/png;base64,{image_base64}"


def main() -> None:
"""Main function for the leaderboard."""
download_data("repo_stats.json")
dataframe = load_data("repo_stats.json")
dataframe["num_warnings"] = dataframe["num_warnings"].apply(lambda x: 27 - x if pd.notnull(x) else x)

dataframe["activity_matrix"] = dataframe["activity_matrix"].apply(
lambda x: activity_to_image(x) if x is not None else x
)
st.set_page_config(layout="wide")
st.title("Group Github Stats")
st.text(
Expand All @@ -56,6 +98,7 @@ def main() -> None:
"average_commit_length_to_main",
"average_commit_length",
"latest_commit",
"activity_matrix",
]
]

Expand All @@ -69,6 +112,7 @@ def main() -> None:
"has_cloudbuild",
"using_dvc",
"repo_size",
"actions_passing",
"readme_length",
"num_warnings",
]
Expand All @@ -80,18 +124,26 @@ def main() -> None:
column_config={
"group_number": "Group Number",
"group_size": "Group Size",
"num_contributors": "Number of contributors",
"num_contributors": "Contributors",
"total_commits": "Total Commits",
"num_commits_to_main": "Commits to main",
"contributions_per_contributor": st.column_config.BarChartColumn("Contributions distribution"),
"num_prs": "Number of Pull Requests",
"average_commit_length_to_main": "ACML* (main)",
"average_commit_length": "ACML* (all)",
"latest_commit": st.column_config.DatetimeColumn("Latest commit"),
"activity_matrix": st.column_config.ImageColumn(
"Commit activity**",
width="medium",
),
},
hide_index=True,
)

st.write("*ACML = Average commit message length")
st.write(
"**Activity matrix is a (N, 24) matrix where N is the number of days since the first commit."
" Each row represents the number of commits per hour for that day."
)
st.header("Content statistics")
st.dataframe(
df_content,
Expand All @@ -104,7 +156,8 @@ def main() -> None:
"has_cloudbuild": "Cloudbuild",
"using_dvc": "Using dvc",
"repo_size": "Repository size",
"readme_size": "Readme size",
"actions_passing": "Actions passing",
"readme_length": "Readme size",
"num_warnings": st.column_config.ProgressColumn(
"Report completion",
help="Number of questions answered in exam report",
Expand Down

0 comments on commit e1163f7

Please sign in to comment.