|
|
|
|
|
import numpy as np
|
|
|
import pandas as pd
|
|
|
import json
|
|
|
import os
|
|
|
|
|
|
|
|
|
PLATFORM_WEIGHTS = {}
|
|
|
|
|
|
try:
|
|
|
|
|
|
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:
|
|
|
|
|
|
PLATFORM_WEIGHTS = json.load(f)
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
print("FATAL ERROR: platform_weights.json not found! Using empty weights.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CEARModel:
|
|
|
def __init__(self, weights=PLATFORM_WEIGHTS):
|
|
|
|
|
|
self.weights = weights
|
|
|
|
|
|
def _diminishing_returns(self, minutes):
|
|
|
|
|
|
return np.log10(minutes + 1)
|
|
|
def calculate_scores(self, user_input_df: pd.DataFrame):
|
|
|
|
|
|
df = user_input_df.merge(
|
|
|
pd.DataFrame.from_dict(self.weights, orient='index'),
|
|
|
left_on='platform_name',
|
|
|
right_index=True,
|
|
|
how='left'
|
|
|
).fillna(0)
|
|
|
|
|
|
total_mins = df['minutes_per_week'].sum()
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
df['Min_Share'] = df['minutes_per_week'] / total_mins
|
|
|
D_Index = 1 / (df['Min_Share']**2).sum() if total_mins > 0 else 0
|
|
|
|
|
|
|
|
|
df['Cultural_Efficiency'] = df['C_Contrib'] / df['minutes_per_week'].replace(0, np.nan)
|
|
|
|
|
|
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')
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|