Skip to content
2 changes: 2 additions & 0 deletions contextualized/analysis/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ def make_c_vis(C, n_vis):
returns a matrix of n_vis x n_contexts that can be used to visualize the effects of the context variables.

"""
if isinstance(C, np.ndarray):
return make_grid_mat(C, n_vis)
return make_grid_mat(C.values, n_vis)


Expand Down
65 changes: 65 additions & 0 deletions contextualized/analysis/pvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import *

import numpy as np
import pandas as pd

from contextualized.analysis.effects import (
get_homogeneous_context_effects,
Expand Down Expand Up @@ -165,3 +166,67 @@ def calc_heterogeneous_predictor_effects_pvals(model, C, **kwargs):
]
)
return pvals


def test_sequential_contexts(
model_constructor: Type[SKLearnWrapper], C: pd.DataFrame, X: pd.DataFrame, Y:pd.DataFrame, **kwargs
) -> pd.DataFrame:
"""
Sequentially test each feature in C using calc_homogeneous_context_effects_pvals.

Args:
model_constructor (function): The constructor of the model to be tested.
C (pd.DataFrame): The context data with multiple features.
X (pd.DataFrame): The input training data.
Y (pd.DataFrame): The output training data.
**kwargs: Additional arguments for the model constructor.

Returns:
pd.DataFrame: A DataFrame of p-values for each feature.
"""
default_fit_params = {
Comment thread
cnellington marked this conversation as resolved.
'encoder_type': 'mlp',
'max_epochs': 3,
'learning_rate': 1e-2
}
fit_params = {**default_fit_params, **kwargs}
pvals_dict = {}

for context in C.columns:
context_col = C[[context]].values

for predictor in X.columns:
predictor_col = X[[predictor]].values

model = model_constructor(**fit_params)
model.fit(context_col, predictor_col, Y.values, **fit_params)

pvals = calc_homogeneous_context_effects_pvals(model, context_col)[0]

for count, target in enumerate(Y.columns):
pvals_dict["Context"] = pvals_dict.get("Context", [])
pvals_dict["Predictor"] = pvals_dict.get("Predictor", [])
pvals_dict["Target"] = pvals_dict.get("Target", [])
pvals_dict["Pvals"] = pvals_dict.get("Pvals", [])

pvals_dict["Context"].append(context)
pvals_dict["Predictor"].append(predictor)
pvals_dict["Target"].append(target)
pvals_dict["Pvals"].append(pvals[count])

return pd.DataFrame.from_dict(pvals_dict)


def get_pval_range(num_bootstraps: int) -> list:
"""
Get the range of possible p-values based on the number of bootstraps.

Args:
num_bootstraps (int): The number of bootstraps.

Returns:
list: The minimum and maximum possible p-values.
"""
min_pval = 1 / (num_bootstraps + 1)
max_pval = num_bootstraps / (num_bootstraps + 1)
return [min_pval, max_pval]
Loading