Skip to content

Commit

Permalink
fix for static plot showing the weights correctly in the edges
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Apr 12, 2024
1 parent f12bc7d commit 103ade2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bnlearn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

__author__ = 'Erdogan Tasksen'
__email__ = 'erdogant@gmail.com'
__version__ = '0.8.6'
__version__ = '0.8.7'

import pgmpy
# Check version pgmpy
Expand Down
15 changes: 12 additions & 3 deletions bnlearn/bnlearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def plot(model,
node_properties=None,
edge_properties=None,
params_interactive={'minmax_distance': [100, 250], 'figsize': (1500, 800), 'notebook': False, 'font_color': 'node_color', 'bgcolor': '#ffffff', 'show_slider': True, 'filepath': None},
params_static={'minscale': 1, 'maxscale': 10, 'figsize': (15, 10), 'width': None, 'height': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'node_shape': 'o', 'layout': 'spring_layout', 'font_color': '#000000', 'facecolor': 'white', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 30, 'visible': True},
params_static={'minscale': 1, 'maxscale': 10, 'figsize': (15, 10), 'width': None, 'height': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'node_shape': 'o', 'layout': 'spectral_layout', 'font_color': '#000000', 'facecolor': 'white', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 30, 'visible': True},
verbose=3):
"""Plot the learned stucture.
Expand Down Expand Up @@ -1050,7 +1050,7 @@ def plot(model,
# Plot properties
defaults = {'minmax_distance': [100, 250], 'figsize': (1500, 800), 'notebook': False, 'font_color': 'node_color', 'bgcolor': '#ffffff', 'directed': True, 'show_slider': True, 'filepath': None}
params_interactive = {**defaults, **params_interactive}
defaults = {'minscale': 1, 'maxscale': 10, 'figsize': (15, 10), 'height': None, 'width': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'layout': 'spring_layout', 'font_color': 'k', 'facecolor': '#ffffff', 'node_shape': 'o', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 30, 'visible': True}
defaults = {'minscale': 1, 'maxscale': 10, 'figsize': (15, 10), 'height': None, 'width': None, 'font_size': 14, 'font_family': 'sans-serif', 'alpha': 0.8, 'layout': 'spectral_layout', 'font_color': 'k', 'facecolor': '#ffffff', 'node_shape': 'o', 'edge_alpha': 0.8, 'arrowstyle': '-|>', 'arrowsize': 30, 'visible': True}
params_static = {**defaults, **params_static}

# DEPRECATED IN LATER VERSION
Expand All @@ -1075,6 +1075,15 @@ def plot(model,
if node_properties[key]['node_size'] is None:
node_properties[key]['node_size']=node_size_default

# Add edges with weights based on independence test results
for edge, properties in edge_properties.items():
strength = properties.get("weight", 0)
G.add_edge(*edge, weight = strength)

# Update the dataframe with the weights
for (source, target), value in edge_properties.items():
model['adjmat'].loc[source, target] = value['weight']

# Extract model if in dict
if 'dict' in str(type(model)):
bnmodel = model.get('model', None)
Expand Down Expand Up @@ -1110,7 +1119,7 @@ def plot(model,
if ('bayes' in str(type(bnmodel)).lower()) or ('pgmpy' in str(type(bnmodel)).lower()):
if verbose>=3: print('[bnlearn] >Plot based on Bayesian model')
# positions for all nodes
G = nx.DiGraph(model['adjmat'])
# G = nx.DiGraph(model['adjmat'])
pos = bnlearn.network.graphlayout(G, pos=pos, scale=scale, layout=params_static['layout'], verbose=verbose)
elif 'networkx' in str(type(bnmodel)):
if verbose>=3: print('[bnlearn] >Plot based on networkx model')
Expand Down
28 changes: 28 additions & 0 deletions bnlearn/examples.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
# %% issue #93

import bnlearn as bn

# Load example mixed dataset
df = bn.import_example(data='titanic')

# Convert to onehot
dfhot, dfnum = bn.df2onehot(df)

# Structure learning
# model = bn.structure_learning.fit(dfnum, methodtype='cl', black_list=['Embarked','Parch','Name'], root_node='Survived', bw_list_method='nodes')
model = bn.structure_learning.fit(dfnum)
# Plot
G = bn.plot(model, interactive=False)

# Compute edge strength with the chi_square test statistic
model = bn.independence_test(model, dfnum, test='chi_square', prune=True)

# Plot
bn.plot(model, interactive=False, pos=G['pos'], params_static={'layout': 'spectral_layout'})
# 'spring_layout', 'planar_layout', 'shell_layout', 'spectral_layout', 'pydot_layout', 'graphviz_layout', 'circular_layout', 'spring_layout', 'random_layout', 'bipartite_layout', 'multipartite_layout',
# bn.plot(model, interactive=True, pos=G['pos'])

# Parameter learning
model = bn.parameter_learning.fit(model, dfnum)


# %% compute causalities
import bnlearn as bn
# Load asia DAG
Expand Down

0 comments on commit 103ade2

Please sign in to comment.