py-why / causal-learn

Causal Discovery in Python. It also includes (conditional) independence tests and score functions.
https://causal-learn.readthedocs.io/en/latest/
MIT License
1.13k stars 186 forks source link

how to visualize the FCM based approaches #146

Open priamai opened 11 months ago

priamai commented 11 months ago

Hi there, I noticed that the FCM methods don't produce a Graph object.

from causallearn.search.FCMBased import lingam
model = lingam.DirectLiNGAM(random_state=None, prior_knowledge=None, apply_prior_knowledge_softly=False, measure='pwling')
model.fit(dataset)

print(model.causal_order_)
print(model.adjacency_matrix_)

Why they are not following the other approach like in Constrained based and in Score Based? Cheers.

kunwuz commented 11 months ago

Yea you are right, at least for these LiNGAM-based methods we could definitely visualize them in a similar way as follows:

from causallearn.search.FCMBased import lingam
model = lingam.ICALiNGAM()
model.fit(data)

from causallearn.search.FCMBased.lingam.utils import make_dot
make_dot(model.adjacency_matrix_, labels=labels)

We will include these usages into the doc, and preferably make the visualization way consistent with other methods. For ANM or PNL, the multivariate version could be a little bit more tricky, see e.g. https://proceedings.mlr.press/v177/uemura22a/uemura22a.pdf.

priamai commented 10 months ago

Hello, that worked: https://colab.research.google.com/drive/1BZ2idQWgr7Ed6d09fk9bYi4a5RoOiu3g?usp=sharing is there a way to save it into a file?

priamai commented 10 months ago

Quick solution

from causallearn.search.FCMBased.lingam.utils import make_dot
my_dot = make_dot(model.adjacency_matrix_,labels=df.columns.to_list())
my_dot.filename="test"
my_dot.name="test"
my_dot.render(format='png')
my_dot.save(filename="test.dot")

maybe add to the docs.