import pandas as pd
from scipy import stats
from sklearn.metrics import roc_auc_score
from ..sklearn.metrics import safe_auc_score

# 有待增加一个参数，控制显示出来的小数位数，以及小于1的值前的0是否显示出来
def compare_pos_neg_groups(data, lab_column, pos_label, neg_label):
    # 分组
    pos_group = data[data[lab_column] == pos_label]
    neg_group = data[data[lab_column] == neg_label]

    # 删除分类标签列
    pos_group = pos_group.drop(columns=[lab_column])
    neg_group = neg_group.drop(columns=[lab_column])

    # 创建一个空的数据框来存储比较结果
    results = pd.DataFrame(columns=[
        'feature', 'pos_mean', 'neg_mean', 'pos_std', 'neg_std', 
        'pos_median', 'neg_median', 'pos_25_percentile', 'neg_25_percentile', 
        'pos_75_percentile', 'neg_75_percentile', 'pos_IQR', 'neg_IQR', 
        't_stat', 't_p_value', 'u_stat', 'u_p_value', 'auc', 'safe_auc',
        'pos_shapiro_p', 'pos_normality', 'neg_shapiro_p', 'neg_normality'
    ])

    # 遍历每个指标，进行t检验和U检验
    for column in pos_group.columns:
        pos_values = pos_group[column]
        neg_values = neg_group[column]

        # 计算均值、标准差、中位数、25/75分位数、IQR
        pos_mean = pos_values.mean()
        neg_mean = neg_values.mean()
        pos_std = pos_values.std()
        neg_std = neg_values.std()

        pos_median = pos_values.median()
        neg_median = neg_values.median()

        pos_25_percentile = pos_values.quantile(0.25)
        neg_25_percentile = neg_values.quantile(0.25)
        pos_75_percentile = pos_values.quantile(0.75)
        neg_75_percentile = neg_values.quantile(0.75)

        pos_IQR = pos_75_percentile - pos_25_percentile
        neg_IQR = neg_75_percentile - neg_25_percentile

        # t-test 检验
        t_stat, t_p_value = stats.ttest_ind(pos_values, neg_values, equal_var=False)

        # Mann-Whitney U 检验
        u_stat, u_p_value = stats.mannwhitneyu(pos_values, neg_values, alternative='two-sided')

        # 计算 AUC 值
        labels = [1] * len(pos_values) + [0] * len(neg_values)
        values = list(pos_values) + list(neg_values)

        auc_value = roc_auc_score(labels, values)
        safe_auc_value = safe_auc_score(labels, values)

        # Shapiro-Wilk 正态性检验
        pos_shapiro_stat, pos_shapiro_p = stats.shapiro(pos_values)
        neg_shapiro_stat, neg_shapiro_p = stats.shapiro(neg_values)

        # 判断正态性
        pos_normality = '阳性组符合正态' if pos_shapiro_p > 0.05 else '阳性组不符合正态'
        neg_normality = '阴性组符合正态' if neg_shapiro_p > 0.05 else '阴性组不符合正态'

        # 创建临时数据行
        temp_df = pd.DataFrame({
            'feature': [column],
            'pos_mean': [f"{pos_mean:.2f}"],
            'neg_mean': [f"{neg_mean:.2f}"],
            'pos_std': [f"{pos_std:.2f}"],
            'neg_std': [f"{neg_std:.2f}"],
            'pos_median': [f"{pos_median:.2f}"],
            'neg_median': [f"{neg_median:.2f}"],
            'pos_25_percentile': [f"{pos_25_percentile:.2f}"],
            'neg_25_percentile': [f"{neg_25_percentile:.2f}"],
            'pos_75_percentile': [f"{pos_75_percentile:.2f}"],
            'neg_75_percentile': [f"{neg_75_percentile:.2f}"],
            'pos_IQR': [f"{pos_IQR:.2f}"],
            'neg_IQR': [f"{neg_IQR:.2f}"],
            't_stat': [f"{t_stat:.2f}"],
            't_p_value': [f"{t_p_value:.3f}".lstrip('0')],
            'u_stat': [f"{u_stat:.2f}"],
            'u_p_value': [f"{u_p_value:.3f}".lstrip('0')],
            'auc': [f"{auc_value:.3f}".lstrip('0')],
            'safe_auc': [f"{safe_auc_value:.3f}".lstrip('0')],
            'pos_shapiro_p': [f"{pos_shapiro_p:.3f}".lstrip('0')],
            'pos_normality': [pos_normality],
            'neg_shapiro_p': [f"{neg_shapiro_p:.3f}".lstrip('0')],
            'neg_normality': [neg_normality]
        })

        # 将临时数据行添加到结果中
        results = pd.concat([results, temp_df], ignore_index=True)

    # 返回结果
    return results