File size: 6,263 Bytes
6881fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pandas as pd
import numpy as np

class Post_Model_Computation:
    @staticmethod
    def compute_pv3rs_marginal_probabilities(results_df):
        """
        Complete Python implementation of the Pv3Rs approx-joint marginalization logic.
        
        1. Assigns pair_source based on the group cumcount index (+1).
        2. Sequentially evaluates each true recurrence episode (j).
        3. Normalizes and rounds the probabilities to 4 decimal places.
        """
        # ====================================================================================
        # STEP 1: Assign Source Position Index Using Your Cumcount Logic
        # ====================================================================================
        df = results_df.sort_values(['patient_id', 'true_episode']).copy()
        
        # This identifies if the row is comparing against the 1st, 2nd, or 3rd previous episode
        df['pair_source'] = df.groupby(['patient_id', 'true_episode']).cumcount() + 1    
        df = df.sort_values('pair_id')
        
        marginal_results = []
        
        # ====================================================================================
        # STEP 2: Sequential Marginal Aggregation Engine
        # ====================================================================================
        for pid, group in df.groupby('patient_id'):
            
            # Get unique episodes in chronological order
            episodes = sorted(group['true_episode'].unique())
            n_epi = len(episodes)
            
            # We start calculating from the first recurrence (index 1)
            for idx in range(1, n_epi):
                curr_epi = episodes[idx]       # Current recurrence episode 'j'
                prev_epi = episodes[idx - 1]   # Immediate predecessor 'j-1'
                
                # Filter all pairs where 'true_episode' is our current episode
                curr_recurrence_pairs = group[group['true_episode'] == curr_epi]
                
                if curr_recurrence_pairs.empty:
                    continue
                    
                # --- 1. Compute C_unnorm (Consecutive pair constraint) ---
                # Rule: Only look at the immediate predecessor index
                c_row = curr_recurrence_pairs[curr_recurrence_pairs['pair_source'] == prev_epi]
                C_unnorm = c_row['prob_C'].iloc[0] if not c_row.empty else 0.0
                
                # --- 2. Compute L_unnorm & Track Source (Max operator across all i < j) ---
                max_l_idx = curr_recurrence_pairs['prob_L'].idxmax()
                L_unnorm = curr_recurrence_pairs.loc[max_l_idx, 'prob_L']
                L_source = curr_recurrence_pairs.loc[max_l_idx, 'pair_source']
                
                # --- 3. Compute I_unnorm (Min operator across all i < j) ---
                I_unnorm = curr_recurrence_pairs['prob_I'].min()
                
                # --- 4. Apply the Pv3Rs Conditional Correction Rule ---
                if C_unnorm > 0.5 and I_unnorm < 0.001:
                    L_unnorm = c_row['prob_L'].iloc[0] if not c_row.empty else L_unnorm
                    L_source = prev_epi
                    
                # --- 5. Total Normalization & Formatted Rounding ---
                total_mass = C_unnorm + L_unnorm + I_unnorm
                
                if total_mass > 0:
                    C_marg = round(C_unnorm / total_mass, 4)
                    L_marg = round(L_unnorm / total_mass, 4)
                    I_marg = round(I_unnorm / total_mass, 4)
                else:
                    C_marg, L_marg, I_marg = 0.0, 0.0, 0.0
                
                # Append structured row for this specific recurrence outcome
                marginal_results.append({
                    'sample_id': pid + '_' + str(int(curr_epi)),
                    'patient_id': pid,
                    'episode': int(curr_epi),
                    'relapse_source_epi': int(L_source),
                    'prob_C': C_marg,
                    'prob_L': L_marg,
                    'prob_I': I_marg
                })
                
        return pd.DataFrame(marginal_results)


    @staticmethod
    def result_post_processing(results_df, calc_NU_post = True, approx_joint = True):
        """
        result_df: PvDeepSet predicted results
        calc_NU_post: calculate non uniform posterior (refine the posterior if the priors were not default)
        approx_joint: approximate marginal probabilities from perwise results
        """
        if calc_NU_post:
            # unique_result = results_df[['pair_id', 'prior_C', 'prior_L', 'prior_I', 'prob_C', 'prob_L', 'prob_I']].drop_duplicates(ignore_index=True)
            # 1. Align column names for math operations
            # We create a temporary copy to ensure we multiply the right classes
            df_priors = results_df[['prior_C', 'prior_L', 'prior_I']].rename(columns={'prior_C': 'C', 'prior_L': 'L', 'prior_I': 'I'})
   
            df_post_uni = results_df[['prob_C', 'prob_L', 'prob_I']].rename(columns={'prob_C': 'C', 'prob_L': 'L', 'prob_I': 'I'})

            # 2. Numerator: Multiply P_uniform(s|y) * P(s)
            # Pandas handles row-matching automatically by index
            numerator = df_post_uni * df_priors

            # 3. Denominator: Sum across the columns for each row (Normalization)
            denominator = numerator.sum(axis=1)

            # 4. Final Recovery: Divide numerator by denominator
            # axis=0 ensures the division happens row by row
            df_post_non_uniform = numerator.divide(denominator, axis=0)

            df_post_non_uniform['C'] = df_post_non_uniform['C'].round(4)
            df_post_non_uniform['L'] = df_post_non_uniform['L'].round(4)
            df_post_non_uniform['I'] = df_post_non_uniform['I'].round(4)
    
            results_df['prob_C'] = df_post_non_uniform['C'].values
            results_df['prob_L'] = df_post_non_uniform['L'].values
            results_df['prob_I'] = df_post_non_uniform['I'].values

        if approx_joint:
            results_df =  Post_Model_Computation.compute_pv3rs_marginal_probabilities(results_df=results_df)
        return results_df