causal-learn icon indicating copy to clipboard operation
causal-learn copied to clipboard

Renaming or Setting labels on Nodes

Open priamai opened this issue 1 year ago • 6 comments

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.

priamai avatar Oct 09 '23 06:10 priamai

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.

kunwuz avatar Oct 09 '23 06:10 kunwuz

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 avatar Oct 09 '23 06:10 priamai

@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.

priamai avatar Oct 09 '23 07:10 priamai

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.

kunwuz avatar Oct 12 '23 23:10 kunwuz