module documentation

Module for functions to predict the risk of lymphatic progression.

The code in this module is utilized by the views.RiskPredictionView of the riskpredictor app to compute the risk of lymphatic progression for a given diagnosis.

Function aggregate_results Aggregate the results of the risk computation into a dictionary.
Function compute_diagnose_probs Compute the probability of the selected diagnosis for any possible hidden state and for both sides of the neck, if the model is a bilateral one.
Function compute_marginalised_risks Compute the marginalised risks of involvement of each LNL.
Function compute_posterior_risks Compute the posterior risks for any possible hidden state and for each of the samples used to compute the prior risks.
Function create_marginalisation Create a vector for marginalizing over hidden states that match the given pattern.
Function create_patient Create a patient dataframe from a specific diagnosis.
Function default_risks Return default risks (everything unknown).
Function risks Compute the marginalized risk of microscopic involvement in any of the modelled LNLs for a given diagnosis.
Variable logger Undocumented
def aggregate_results(inference_result, marginalized_risks):

Aggregate the results of the risk computation into a dictionary.

This is a helper function that is used to convert the numpy arrays that are returned by the risk computation into a dictionary that can be used as context for the views.RiskPredictionView view.

The returned dictionary has the following structure: For each side (ipsi or contra) and each LNL, the dictionary contains a list of three values: The first value is the error of the prediction, the second value is the risk of involvement (but minus half the error), and the third value is the probability of being healthy (again minus half the error).

Parameters
inference_result:InferenceResultUndocumented
marginalized_risks:Dict[str, np.ndarray]Undocumented
Returns
Dict[str, List[float]]Undocumented
def compute_diagnose_probs(inference_result, t_stage, diagnosis, specificity, sensitivity, midline_extension=None):

Compute the probability of the selected diagnosis for any possible hidden state and for both sides of the neck, if the model is a bilateral one.

In probabilistic terms, this is the probability P(D=d|X) of the diagnosis D=d given any of the possible hidden states X. Consequently, this is a 1D array of length 2^V, where V is the number of LNLs in the model.

Parameters
inference_result:InferenceResultUndocumented
t_stage:strUndocumented
diagnosis:Dict[str, Dict[str, Optional[bool]]]Undocumented
specificity:floatUndocumented
sensitivity:floatUndocumented
midline_extension:Optional[bool]Undocumented
Returns
Dict[str, np.ndarray]Undocumented
def compute_marginalised_risks(inference_result, posterior_risks):

Compute the marginalised risks of involvement of each LNL.

In probabilistic terms, this is the probability P(X=x|D=d), which is computed by marginalizing over all hidden states that do match the given diagnosis (for wich the posterior risk over all possible hidden states was already computed).

Parameters
inference_result:InferenceResultUndocumented
posterior_risks:np.ndarrayUndocumented
Returns
Dict[str, np.ndarray]Undocumented
def compute_posterior_risks(inference_result, diagnose_probs, risk_matrices):

Compute the posterior risks for any possible hidden state and for each of the samples used to compute the prior risks.

In probabilistic terms, this is the probability P(X|D=d) of any of the possible hidden state X given the diagnosis D=d. This is computed for each of the N samples. Consequently, this is a 3D array of shape (N, 2^V, 2^V) in the bilateral case, or (N, 2^V) in the unilateral case.

Parameters
inference_result:InferenceResultUndocumented
diagnose_probs:Dict[str, np.ndarray]Undocumented
risk_matrices:np.ndarrayUndocumented
Returns
np.ndarrayUndocumented
def create_marginalisation(lymph_model, pattern):

Create a vector for marginalizing over hidden states that match the given pattern.

If one wants to know the probability of e.g. LNL II involvement, one needs to marginalize over all hidden states where LNL II is involved. This function creates a vector that is 1 for all hidden states that match the given pattern and 0 for all others.

Parameters
lymph_model:Union[Unilateral, Bilateral, MidlineBilateral]Undocumented
pattern:Dict[str, Optional[bool]]Undocumented
Returns
np.ndarrayUndocumented
def create_patient(diagnosis, t_stage, is_bilateral=False, midline_extension=None):

Create a patient dataframe from a specific diagnosis.

This is necessary, so that the lymph-model can be used to compute the probability of the given diagnosis for any possible hidden state.

Parameters
diagnosis:Dict[str, Dict[str, Optional[bool]]]Undocumented
t_stage:strUndocumented
is_bilateral:boolUndocumented
midline_extension:Optional[bool]Undocumented
Returns
pd.DataFrameUndocumented
def default_risks(inference_result, **kwargs):

Return default risks (everything unknown).

Parameters
inference_result:InferenceResultUndocumented
**kwargsUndocumented
Returns
Dict[str, Any]Undocumented
def risks(inference_result, t_stage, diagnosis, specificity, sensitivity, midline_extension=None, **_kwargs):

Compute the marginalized risk of microscopic involvement in any of the modelled LNLs for a given diagnosis.

Parameters
inference_result:InferenceResultUndocumented
t_stage:strUndocumented
diagnosis:Dict[str, Dict[str, Optional[bool]]]Undocumented
specificity:floatUndocumented
sensitivity:floatUndocumented
midline_extension:Optional[bool]Undocumented
**_kwargsUndocumented
Returns
Dict[str, Any]Undocumented
logger =

Undocumented