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.19k stars 196 forks source link

Renaming or Setting labels on Nodes #141

Open priamai opened 1 year ago

priamai commented 1 year ago

Hi there, I want to able to assign labels to the GeneralGraph similar to what you do with the GraphUtils instead of using the default X1...Xn notation.

This is what I tried:


cg.G.get_node_names()

['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9']

for i, node in enumerate(cg.G.get_nodes()):
    node_name = labels[i] if labels is not None else node.get_name()
    node.set_name(node_name)

cg.G.get_node_names()
['IMPRESSIONS',
 'CLICKS',
 'CONVERSIONS',
 'AD_SPEND_USD',
 'REVENUE_USD',
 'NUM_ORDERS',
 'AOV_USD',
 'VISITORS',
 'SESSIONS']

Which looks good but I think it destroys the internal lookup table, when I want to see the edges it just complains of a keyerror:

image

Maybe I have to relabel the node_map? Any help will be appreciated.

I know you are working on supporting Pandas DataFrames directly but a short workaround will get me through this problem for the time being.

kunwuz commented 1 year ago

Hi, the recommended way to assign labels to the nodes is 'cg.draw_pydot_graph(labels=[“A”, “B”, “C”])' or 'GraphUtils.to_pydot(cg.G, labels=[“A”, “B”, “C”])', as mentioned in the documentation. Here are some usage examples.

This visualizes the graph with assigned labels. But I'm not sure if you are looking for something else.

priamai commented 1 year ago

That is only for saving in pydot, I need to rewrite the actual node names for my downstream tasks and avoid confusion when referring to the original dataframe e.g. variable names.

priamai commented 1 year ago

@kunwuz for example when I add the Background Knowledge I want to do it by referring to the real node names defined in my ground truth NetworkX graph, instead now I have to go back and forward converting between the X{0} notation and the real variable names. Hope it makes better sense.

kunwuz commented 1 year ago

I see, that requires some refactorization of the graph classes in causal-learn. For now, it seems that creating a mapping/look-up table is the easiest way. We have put it on the list. Please also feel free to let me know if you have any suggestions or would like to improve it together.

emoen commented 1 month ago

This would be a useful feature. The noede names should be assignable.

emoen commented 1 month ago

Also, this argument, labels, would be useful on the method draw_nx_graph()