Initial clean commit
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
'''
|
||||
Filename: main.py
|
||||
Path: /home/netoa/Desktop/ISR/metrics/subjective/pre_processing
|
||||
Created Date: Saturday January 25th 2025
|
||||
Author: Andre Neto
|
||||
-----
|
||||
Last Modified: Tue Apr 29 2025
|
||||
Modified By: Andre Neto
|
||||
-----
|
||||
Copyright (c) 2025 Institute of Systems and Robotics
|
||||
'''
|
||||
|
||||
import os
|
||||
import pandas as pd
|
||||
import psycopg2
|
||||
|
||||
# pylint: disable=C0301
|
||||
|
||||
def filter_valid(users_csv, sessions_csv, tests_csv):
|
||||
""" Get valid users and sessions from the CSV files. """
|
||||
|
||||
users_df = pd.read_csv(users_csv)
|
||||
sessions_df = pd.read_csv(sessions_csv)
|
||||
tests_df = pd.read_csv(tests_csv)
|
||||
|
||||
valid_sessions_df = sessions_df[sessions_df['id'].isin(tests_df['session_id'])]
|
||||
|
||||
valid_users_df = users_df[users_df["id"].isin(valid_sessions_df["user_id"])]
|
||||
|
||||
valid_users_df.to_csv(users_csv, index=False)
|
||||
valid_sessions_df.to_csv(sessions_csv, index=False)
|
||||
|
||||
print(f"Filtered users saved to {users_csv}")
|
||||
print(f"Filtered sessions saved to {sessions_csv}")
|
||||
|
||||
|
||||
def pg_to_csv(database_url, export_dir="tables"):
|
||||
"""Export all tables from a PostgreSQL database to CSV files."""
|
||||
|
||||
os.makedirs(export_dir, exist_ok=True)
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(database_url)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';")
|
||||
tables = cursor.fetchall()
|
||||
|
||||
for table in tables:
|
||||
table_name = table[0]
|
||||
|
||||
if table_name.startswith("auth") or table_name.startswith("django"):
|
||||
continue
|
||||
|
||||
file_path = os.path.join(export_dir, f"{table_name}.csv")
|
||||
with open(file_path, "w", encoding='utf-8') as file:
|
||||
cursor.copy_expert(f"COPY {table_name} TO STDOUT WITH CSV HEADER", file)
|
||||
print(f"Exported {table_name} to {file_path}")
|
||||
|
||||
except (psycopg2.DatabaseError, psycopg2.OperationalError) as e:
|
||||
print(f"Database error exporting tables: {e}")
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
def main():
|
||||
""" Pre-processing script for subjective metrics. """
|
||||
|
||||
### Database URL
|
||||
scheme = "postgresql"
|
||||
user = "postgres"
|
||||
password = os.environ.get("ISR_DB_PASSWORD")
|
||||
if not password:
|
||||
raise RuntimeError(
|
||||
"ISR_DB_PASSWORD is not set. Export the database "
|
||||
"password before running (see README.md, Configuration)."
|
||||
)
|
||||
host = "junction.proxy.rlwy.net"
|
||||
port = "22893"
|
||||
database = "railway"
|
||||
|
||||
database_url = f"{scheme}://{user}:{password}@{host}:{port}/{database}"
|
||||
|
||||
# Directories
|
||||
tables_dir = 'metrics/subjective/tables'
|
||||
|
||||
# Export tables to CSV files
|
||||
pg_to_csv(database_url, tables_dir)
|
||||
|
||||
# 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")
|
||||
|
||||
filter_valid(user_csv, session_csv, test_csv)
|
||||
|
||||
print("Pre-processing completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user