Skip to content

Commit

Permalink
code refactoring to compute -log10P in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Oct 6, 2024
1 parent 3ee647a commit 9238afb
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions bnlearn/bnlearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,13 @@ def get_edge_properties(model, color='#000000', weight=1, minscale=1, maxscale=1
# if adjmat is not None and ('lingam' not in model['config']['method']):
if adjmat is not None:
if verbose>=3: print('[bnlearn]> Set edge weights based on the [%s] test statistic.' %(model['independence_test'].columns[-2]))
logp = -np.log10(model['independence_test']['p_value'])
Iloc = np.isinf(logp)
max_logp = np.max(logp[~Iloc]) * 1.5 # For visualization purposes, set the max higher then what is present to mark the difference.
if np.isnan(max_logp): max_logp = 1
logp.loc[Iloc] = max_logp
# Compute logp
logp = compute_logp(model['independence_test']['p_value'])
# logp = -np.log10(model['independence_test']['p_value'])
# Iloc = np.isinf(logp)
# max_logp = np.max(logp[~Iloc]) * 1.5 # For visualization purposes, set the max higher then what is present to mark the difference.
# if np.isnan(max_logp): max_logp = 1
# logp.loc[Iloc] = max_logp
# Rescale the weights
weights = _normalize_weights(logp.values, minscale=minscale, maxscale=maxscale)
# Add to adjmat
Expand Down Expand Up @@ -987,6 +989,15 @@ def get_edge_properties(model, color='#000000', weight=1, minscale=1, maxscale=1
return edges


def compute_logp(p_value):
logp = -np.log10(p_value)
Iloc = np.isinf(logp)
# For visualization purposes, set the max higher then what is present to mark the difference.
max_logp = np.max(logp[~Iloc]) * 1.5
if np.isnan(max_logp): max_logp = 1
logp.loc[Iloc] = max_logp
return logp

# %% PLOT
def plot_graphviz(model,
edge_labels='weight',
Expand Down Expand Up @@ -1092,19 +1103,24 @@ def plot_graphviz(model,
if verbose>=3: print('[bnlearn] >Setting edge labels to pvalue.')
source = model.get('independence_test')['source']
target = model.get('independence_test')['target']
logp = -np.log10(model.get('independence_test')['p_value'])
Iloc = np.isinf(logp)
# Compute logp
logp = compute_logp(model['independence_test']['p_value'])
# logp = -np.log10(model.get('independence_test')['p_value'])
# Iloc = np.isinf(logp)
# For visualization purposes, set the max higher then what is present to mark the difference.
max_logp = np.max(logp[~Iloc]) * 1.5
if np.isnan(max_logp): max_logp = 1
logp.loc[Iloc] = max_logp
# max_logp = np.max(logp[~Iloc]) * 1.5
# if np.isnan(max_logp): max_logp = 1
# logp.loc[Iloc] = max_logp
# Create new adjmat based on indepdence test
adjmat = vec2adjmat(source, target, weights=logp, symmetric=True, aggfunc='sum', verbose=verbose)
else:
adjmat = model['adjmat'].copy()

if edge_labels is not None:
edge_labels = list(adjmat.T.columns)

# Make the dot and output Directed graph source code in the DOT language.
dot_graph = make_dot(adjmat.T.values.astype(float), labels=list(adjmat.T.columns), lower_limit=0, **params)
dot_graph = make_dot(adjmat.T.values.astype(float), labels=edge_labels, lower_limit=0, **params)

# Return
return dot_graph
Expand Down

0 comments on commit 9238afb

Please sign in to comment.