# cear_model.py import numpy as np import pandas as pd import json import os # Necessary for finding the JSON file # --- 1. Load PLATFORM_WEIGHTS variable from JSON --- PLATFORM_WEIGHTS = {} # Default value try: # Get the directory of the current script (cear_model.py) script_dir = os.path.dirname(os.path.abspath(__file__)) json_path = os.path.join(script_dir, 'platform_weights.json') with open(json_path, 'r') as f: # Load the configuration data into the global variable PLATFORM_WEIGHTS = json.load(f) except FileNotFoundError: # This warning is useful for debugging if the file is missing print("FATAL ERROR: platform_weights.json not found! Using empty weights.") # The default empty {} dict is used if the file is missing # --- 2. Define the Model Class --- # The class can now safely reference the global PLATFORM_WEIGHTS variable class CEARModel: def __init__(self, weights=PLATFORM_WEIGHTS): # The weights dictionary is passed as a default parameter self.weights = weights def _diminishing_returns(self, minutes): # ... your method code ... return np.log10(minutes + 1) def calculate_scores(self, user_input_df: pd.DataFrame): # 1. Merge weights with user input df = user_input_df.merge( pd.DataFrame.from_dict(self.weights, orient='index'), left_on='platform_name', right_index=True, how='left' ).fillna(0) # Fills missing weights with 0 for platforms not in list total_mins = df['minutes_per_week'].sum() # 2. Calculate Core Scores df['C_Contrib'] = df.apply(lambda row: row['W_C'] * self._diminishing_returns(row['minutes_per_week']), axis=1) df['A_Contrib'] = df.apply(lambda row: row['W_A'] * row['minutes_per_week'], axis=1) C_Score = df['C_Contrib'].sum() A_Risk = df['A_Contrib'].sum() # 3. Calculate D-Index (Platform Diversity) df['Min_Share'] = df['minutes_per_week'] / total_mins D_Index = 1 / (df['Min_Share']**2).sum() if total_mins > 0 else 0 # 4. Calculate Cultural Efficiency df['Cultural_Efficiency'] = df['C_Contrib'] / df['minutes_per_week'].replace(0, np.nan) # Avoid div by zero return { "C_Score": C_Score, "A_Risk": A_Risk, "D_Index": D_Index, "Per_Platform_Efficiency": df[['platform_name', 'Cultural_Efficiency']].dropna().to_dict('records') } # Example Usage: # user_data = pd.DataFrame([{'platform_name': 'TikTok', 'minutes_per_week': 300}, ...]) # model = CEARModel() # model.calculate_scores(user_data)