CoolFace
Apppublic

thompsgj/mathtext-fastapi

sourceHugging Faceagpl-3.0updated 3y agoView on Hugging Face
1likes
plot_calls.py117 linesDownload Raw Back to scripts
1import math2from datetime import datetime3 4import matplotlib.pyplot as plt5import pandas as pd6 7pd.set_option('display.max_columns', None)8pd.set_option('display.max_rows', None)9 10log_files = [11    'call_history_sentiment_1_bash.csv',12    'call_history_text2int_1_bash.csv',13]14 15for log_file in log_files:16    path_ = f"./data/{log_file}"17    df = pd.read_csv(filepath_or_buffer=path_, sep=";")18    df["finished_ts"] = df["finished"].apply(19        lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp())20    df["started_ts"] = df["started"].apply(21        lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp())22    df["elapsed"] = df["finished_ts"] - df["started_ts"]23 24    df["success"] = df["outputs"].apply(lambda x: 0 if "Time-out" in x else 1)25 26    student_numbers = sorted(df['active_students'].unique())27 28    bins_dict = dict()  # bins size for each group29    min_finished_dict = dict()  # zero time for each group30 31    for student_number in student_numbers:32        # for each student group calculates bins size and zero time33        min_finished = df["finished_ts"][df["active_students"] == student_number].min()34        max_finished = df["finished_ts"][df["active_students"] == student_number].max()35        bins = math.ceil(max_finished - min_finished)36        bins_dict.update({student_number: bins})37        min_finished_dict.update({student_number: min_finished})38        print(f"student number: {student_number}")39        print(f"min finished: {min_finished}")40        print(f"max finished: {max_finished}")41        print(f"bins finished seconds: {bins}, minutes: {bins / 60}")42 43    df["time_line"] = None44    for student_number in student_numbers:45        # calculates time-line for each student group46        df["time_line"] = df.apply(47            lambda x: x["finished_ts"] - min_finished_dict[student_number]48            if x["active_students"] == student_number49            else x["time_line"],50            axis=151        )52 53    # creates a '.csv' from the dataframe54    df.to_csv(f"./data/processed_{log_file}", index=False, sep=";")55 56    result = df.groupby(['active_students', 'success']) \57        .agg({58        'elapsed': ['mean', 'median', 'min', 'max'],59        'success': ['count'],60    })61 62    print(f"Results for {log_file}")63    print(result, "\n")64 65    title = None66    if "sentiment" in log_file.lower():67        title = "API result for 'sentiment-analysis' endpoint"68    elif "text2int" in log_file.lower():69        title = "API result for 'text2int' endpoint"70 71    for student_number in student_numbers:72        # Prints percentage of the successful and failed calls73        try:74            failed_calls = result.loc[(student_number, 0), 'success'][0]75        except:76            failed_calls = 077        successful_calls = result.loc[(student_number, 1), 'success'][0]78        percentage = (successful_calls / (failed_calls + successful_calls)) * 10079        print(f"Percentage of successful API calls for {student_number} students: {percentage.__round__(2)}")80 81    rows = len(student_numbers)82 83    fig, axs = plt.subplots(rows, 2)  # (rows, columns)84 85    for index, student_number in enumerate(student_numbers):86        # creates a boxplot for each test group87        data = df[df["active_students"] == student_number]88        axs[index][0].boxplot(x=data["elapsed"])  # axs[row][column]89        # axs[index][0].set_title(f'Boxplot for {student_number} students')90        axs[index][0].set_xlabel(f'student number {student_number}')91        axs[index][0].set_ylabel('Elapsed time (s)')92 93        # creates a histogram for each test group94        axs[index][1].hist(x=data["elapsed"], bins=25)  # axs[row][column]95        # axs[index][1].set_title(f'Histogram for {student_number} students')96        axs[index][1].set_xlabel('seconds')97        axs[index][1].set_ylabel('Count of API calls')98 99    fig.suptitle(title, fontsize=16)100 101    fig, axs = plt.subplots(rows, 1)  # (rows, columns)102 103    for index, student_number in enumerate(student_numbers):104        # creates a histogram and shows API calls on a timeline for each test group105        data = df[df["active_students"] == student_number]106 107        print(data["time_line"].head(10))108 109        axs[index].hist(x=data["time_line"], bins=bins_dict[student_number])  # axs[row][column]110        # axs[index][1].set_title(f'Histogram for {student_number} students')111        axs[index].set_xlabel('seconds')112        axs[index].set_ylabel('Count of API calls')113 114    fig.suptitle(title, fontsize=16)115 116plt.show()117