Last active
November 5, 2025 03:57
-
-
Save andrewfowlie/ddecf63b172a0fb94332caaa4fea1bcd to your computer and use it in GitHub Desktop.
Read and plot Learning Mall coursework results from JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Read and plot Learning Mall coursework results from JSON | |
| ======================================================== | |
| """ | |
| import json | |
| import re | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import seaborn as sns | |
| RESPONSE_KEY_START = "q" | |
| DURATION_KEY = "duration" | |
| GRADE_KEY = "grade10000" | |
| DELETE = [("lastname", "Overall average")] | |
| REGEX = r"(?:(\d+)\s*hour)?\s*(?:(\d+)\s+mins)?\s*(?:(\d+)\s+secs)?" | |
| def count_questions(data): | |
| return len([k for k in data[0] if k.startswith(RESPONSE_KEY_START)]) | |
| def number_correct_answers(entry, n): | |
| return int(float(entry[GRADE_KEY]) * n / 100.) | |
| def time_in_minutes(entry): | |
| match = re.match(REGEX, entry[DURATION_KEY]) | |
| hours, minutes, seconds = match.groups(default=0) | |
| return float(hours) * 60. + float(minutes) + float(seconds) / 60. | |
| def keep(entry): | |
| for k, v in DELETE: | |
| if entry[k] == v: | |
| return False | |
| return True | |
| def json_to_df(json_file_name): | |
| with open(json_file_name) as f: | |
| data = json.load(f)[0] | |
| data = [e for e in data if keep(e)] | |
| n = count_questions(data) | |
| for e in data: | |
| e["number_correct_answers"] = number_correct_answers(e, n) | |
| e["time_in_minutes"] = time_in_minutes(e) | |
| return pd.DataFrame(data) | |
| def summary_plot(df): | |
| fig, ax = plt.subplots(3) | |
| sns.histplot(df, x="number_correct_answers", hue="department", stat="percent", multiple="stack", discrete=True, shrink=0.5, ax=ax[0]) | |
| sns.histplot(df, x="time_in_minutes", hue="department", multiple="stack", shrink=0.5, ax=ax[1]) | |
| sns.scatterplot(df, x="time_in_minutes", y="number_correct_answers", hue="department", ax=ax[2]) | |
| return fig, ax | |
| if __name__ == "__main__": | |
| import sys | |
| json_file_name = sys.argv[1] | |
| df = json_to_df(json_file_name) | |
| summary_plot(df) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment