199 lines
7.1 KiB
Python
199 lines
7.1 KiB
Python
'''
|
|
Filename: main.py
|
|
Path: /home/netoa/Desktop/ISR/metrics/subjective/post_processing
|
|
Created Date: Saturday January 25th 2025
|
|
Author: Andre Neto
|
|
-----
|
|
Last Modified: Sat Mar 22 2025
|
|
Modified By: Andre Neto
|
|
-----
|
|
Copyright (c) 2025 Institute of Systems and Robotics
|
|
'''
|
|
|
|
import os
|
|
import pandas as pd
|
|
|
|
# pylint: disable=C0301
|
|
|
|
def demographics_xlsx(input_csv, output_xlsx):
|
|
"""Compute and save demographic analytics."""
|
|
if not os.path.exists(input_csv):
|
|
print(f"Error: {input_csv} does not exist.")
|
|
return
|
|
|
|
df = pd.read_csv(input_csv)
|
|
|
|
def categorize_age(age):
|
|
if age < 18:
|
|
return "Under 18"
|
|
elif 18 <= age <= 25:
|
|
return "18-25"
|
|
elif 26 <= age <= 40:
|
|
return "26-40"
|
|
elif 41 <= age <= 60:
|
|
return "41-60"
|
|
else:
|
|
return "Above 60"
|
|
|
|
df["age_group"] = df["age"].apply(categorize_age)
|
|
|
|
analytics = {
|
|
"Age Groups": df["age_group"].value_counts(),
|
|
"Gender": df["gender"].value_counts(),
|
|
"Education": df["education"].value_counts(),
|
|
"Country": df["country"].value_counts(),
|
|
"Device": df["device"].value_counts(),
|
|
"Place": df["place"].value_counts(),
|
|
"Ethnicity": df["ethnicity"].value_counts(),
|
|
}
|
|
|
|
with pd.ExcelWriter( # pylint: disable=abstract-class-instantiated
|
|
output_xlsx,
|
|
engine="openpyxl",
|
|
mode="a" if os.path.exists(output_xlsx) else "w",
|
|
if_sheet_exists="replace" if os.path.exists(output_xlsx) else None,
|
|
) as writer:
|
|
for sheet_name, data in analytics.items():
|
|
data.to_excel(writer, sheet_name=sheet_name)
|
|
|
|
print(f"Demographic analytics saved to {output_xlsx}")
|
|
|
|
|
|
def sessions_and_tests_xlsx(session_csv, test_csv, output_xlsx):
|
|
"""Compute and save session and test analytics."""
|
|
if not os.path.exists(test_csv) or not os.path.exists(session_csv):
|
|
print("Error: Required CSV files are missing.")
|
|
return
|
|
|
|
test_df = pd.read_csv(test_csv, parse_dates=["created_at"])
|
|
session_df = pd.read_csv(session_csv, parse_dates=["created_at"])
|
|
|
|
data = test_df.merge(session_df, left_on="session_id", right_on="id", suffixes=("_test", "_session"))
|
|
total_sessions = session_df["id"].nunique()
|
|
total_users = session_df["user_id"].nunique()
|
|
total_tests = test_df["id"].nunique()
|
|
avg_tests_per_session = data.groupby("session_id").size().mean()
|
|
|
|
session_durations = data.groupby("session_id").agg(
|
|
session_start=("created_at_test", "min"), session_end=("created_at_test", "max")
|
|
)
|
|
session_durations["duration"] = (session_durations["session_end"] - session_durations["session_start"]).dt.total_seconds()
|
|
avg_session_duration = session_durations["duration"].mean()
|
|
|
|
tests_per_user = data.groupby("user_id").size()
|
|
avg_tests_per_user = tests_per_user.mean()
|
|
|
|
data = data.sort_values(by=["session_id", "created_at_test"])
|
|
data["time_diff"] = data.groupby("session_id")["created_at_test"].diff().dt.total_seconds()
|
|
avg_test_duration = data["time_diff"].mean()
|
|
|
|
analytics = pd.DataFrame({
|
|
"Metric": [
|
|
"Total Sessions",
|
|
"Total Users",
|
|
"Total Tests",
|
|
"Average Tests Per Session",
|
|
"Average Session Duration (seconds)",
|
|
"Average Tests Per User",
|
|
"Average Test Duration (seconds)",
|
|
],
|
|
"Value": [
|
|
total_sessions,
|
|
total_users,
|
|
total_tests,
|
|
avg_tests_per_session,
|
|
avg_session_duration,
|
|
avg_tests_per_user,
|
|
avg_test_duration,
|
|
],
|
|
})
|
|
|
|
with pd.ExcelWriter( # pylint: disable=abstract-class-instantiated
|
|
output_xlsx,
|
|
engine="openpyxl",
|
|
mode="a" if os.path.exists(output_xlsx) else "w",
|
|
if_sheet_exists="replace" if os.path.exists(output_xlsx) else None,
|
|
) as writer:
|
|
analytics.to_excel(writer, sheet_name="Session and Test Analytics", index=False)
|
|
|
|
print(f"Session and test analytics saved to {output_xlsx}")
|
|
|
|
|
|
def feedback_xlsx(feedback_csv, output_xlsx):
|
|
"""Compute and save feedback analytics."""
|
|
|
|
if not os.path.exists(feedback_csv):
|
|
print(f"Error: {feedback_csv} does not exist.")
|
|
return
|
|
|
|
feedback_df = pd.read_csv(feedback_csv)
|
|
|
|
feedback_content = feedback_df[["content"]]
|
|
|
|
with pd.ExcelWriter( # pylint: disable=abstract-class-instantiated
|
|
output_xlsx,
|
|
engine="openpyxl",
|
|
mode="a" if os.path.exists(output_xlsx) else "w",
|
|
if_sheet_exists="replace" if os.path.exists(output_xlsx) else None,
|
|
) as writer:
|
|
for _, data in feedback_content.items():
|
|
data.to_excel(writer, sheet_name="Content", index=False)
|
|
|
|
print(f"Feedback analytics saved to {output_xlsx}")
|
|
|
|
|
|
def combine_demographics_csv(test_csv, session_csv, user_csv, image_csv, london_csv, output_csv):
|
|
""" Combine all CSV files into a single file. """
|
|
|
|
if not os.path.exists(test_csv) or not os.path.exists(session_csv) or not os.path.exists(user_csv) or not os.path.exists(image_csv) or not os.path.exists(london_csv):
|
|
print("Error: Required CSV files are missing.")
|
|
return
|
|
|
|
test_df = pd.read_csv(test_csv)
|
|
session_df = pd.read_csv(session_csv)
|
|
user_df = pd.read_csv(user_csv)
|
|
image_df = pd.read_csv(image_csv)
|
|
london_df = pd.read_csv(london_csv)
|
|
|
|
data = session_df.merge(user_df, left_on="user_id", right_on="id", suffixes=("", "_aux"))
|
|
data = data.drop(columns=["id_aux", "created_at", "user_id"])
|
|
data = data.merge(test_df, left_on="id", right_on="session_id", suffixes=("", "_aux"))
|
|
data = data.drop(columns=["id_aux", "created_at", "session_id", "id"])
|
|
data = data.merge(image_df, left_on="image_id", right_on="id", suffixes=("", "_aux"))
|
|
data = data.drop(columns=["dataset_id_aux", "dataset_id", "id", "image_file", "file_name"])
|
|
data = data.merge(london_df, left_on="name", right_on="file_name")
|
|
data = data.drop(columns=["image_id", "id"])
|
|
|
|
data.to_csv(output_csv, index=False)
|
|
print(f"Combined CSV saved to {output_csv}")
|
|
|
|
def main():
|
|
""" Post-process subjective metrics. """
|
|
|
|
tables_dir = 'metrics/subjective/tables'
|
|
results_dir = 'results/sheets'
|
|
|
|
os.makedirs(results_dir, exist_ok=True)
|
|
|
|
# Filter valid users and sessions
|
|
user_csv = os.path.join(tables_dir, "app_user_user.csv")
|
|
session_csv = os.path.join(tables_dir, "app_ss_session.csv")
|
|
test_csv = os.path.join(tables_dir, "app_ss_test.csv")
|
|
feedback_csv = os.path.join(tables_dir, "app_user_feedback.csv")
|
|
london_csv = os.path.join(tables_dir, "london_faces_info.csv")
|
|
image_csv = os.path.join(tables_dir, "app_database_image.csv")
|
|
|
|
analytics_xlsx = os.path.join(results_dir, "analytics.xlsx")
|
|
combined_csv = os.path.join(results_dir, "tests.csv")
|
|
|
|
demographics_xlsx(user_csv, analytics_xlsx)
|
|
sessions_and_tests_xlsx(session_csv, test_csv, analytics_xlsx)
|
|
feedback_xlsx(feedback_csv, analytics_xlsx)
|
|
combine_demographics_csv(test_csv, session_csv, user_csv, image_csv, london_csv, combined_csv)
|
|
|
|
print("Post-processing completed.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|