From 103ade2376809e204d23b8fa06e08ed7770ce0c7 Mon Sep 17 00:00:00 2001 From: erdogant Date: Fri, 12 Apr 2024 22:57:00 +0200 Subject: [PATCH] fix for static plot showing the weights correctly in the edges --- bnlearn/__init__.py | 2 +- bnlearn/bnlearn.py | 15 ++++++++++++--- bnlearn/examples.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/bnlearn/__init__.py b/bnlearn/__init__.py index 46e320b..d163ae7 100644 --- a/bnlearn/__init__.py +++ b/bnlearn/__init__.py @@ -38,7 +38,7 @@ __author__ = 'Erdogan Tasksen' __email__ = 'erdogant@gmail.com' -__version__ = '0.8.6' +__version__ = '0.8.7' import pgmpy # Check version pgmpy diff --git a/bnlearn/bnlearn.py b/bnlearn/bnlearn.py index 644b773..04976b5 100644 --- a/bnlearn/bnlearn.py +++ b/bnlearn/bnlearn.py @@ -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. @@ -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 @@ -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) @@ -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') diff --git a/bnlearn/examples.py b/bnlearn/examples.py index b3233e7..817397c 100644 --- a/bnlearn/examples.py +++ b/bnlearn/examples.py @@ -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