erdogant / bnlearn

Python package for Causal Discovery by learning the graphical structure of Bayesian networks. Structure Learning, Parameter Learning, Inferences, Sampling methods.
https://erdogant.github.io/bnlearn
Other
480 stars 46 forks source link

[plot] Don't tie plot.show() to param "visible" #103

Closed smarianimore closed 1 month ago

smarianimore commented 2 months ago

Dear devs, I found no way to avoid showing plots of learnt network, as setting parameter "visible"=False applies at two levels:

The "offending" code is in this line IMHO: https://github.com/erdogant/bnlearn/blob/b395e1688d521c4863aabd77c20c14fd96580242/bnlearn/bnlearn.py#L1177

The use case is to simply create a plot to be saved on file, without showing it.

Imho, bnlearn should not force plots to be shown, but only create them, return them, and let the user decide.

Another solution would be to introduce a 2nd parameter "show" to control the behaviour of the offending line instead of "visible".

What do you think?

erdogant commented 2 months ago

Good idea. I like to separate the visible and the show of plots.

erdogant commented 1 month ago

I created a new version that should solve this issue! Update to the latest version.

pip install -U bnlearn

import bnlearn as bn
import matplotlib.pyplot as plt

df = bn.import_example('sprinkler')
model = bn.structure_learning.fit(df)

# Figure is visible and created
fig = bn.plot(model, params_static={'visible': True, 'showplot': True})

# Figure is visible but not created
fig = bn.plot(model, params_static={'visible': True, 'showplot': False})
plt.show()

# Figure is not visible but but created
fig = bn.plot(model, params_static={'visible': False, 'showplot': True})

# Figure is not visible but but created
fig = bn.plot(model, params_static={'visible': False, 'showplot': False})
plt.show()
erdogant commented 1 month ago

Should be solved now with the last update! Reopen if not.

smarianimore commented 2 weeks ago

Dear @erdogant , many thanks! I tried with {'visible': True, 'showplot': False} and it works as intended (figure created with plot but not shown) Perhaps the comments on the other modes have some typos as they do not make much sense to me, but my use case has been solved XD Thanks!

erdogant commented 2 weeks ago

I will look at the typos. I agree, some modes are likely edge cases but at least its complete now.