Skip to content

[python-package] Add tests for more create_tree_digraph() inputs#7166

Open
daguirre11 wants to merge 7 commits intolightgbm-org:masterfrom
daguirre11:plotting-to-graph-viz-test
Open

[python-package] Add tests for more create_tree_digraph() inputs#7166
daguirre11 wants to merge 7 commits intolightgbm-org:masterfrom
daguirre11:plotting-to-graph-viz-test

Conversation

@daguirre11
Copy link
Contributor

@daguirre11 daguirre11 commented Feb 23, 2026

Contributes to #7031
BEFORE
Screenshot 2026-02-23 at 2 32 15 PM

AFTER
Screenshot 2026-02-23 at 2 52 55 PM

7 line difference in coverage for /lightgbm/plotting.py

NOTE

I know these are very specific tests, however, the random state is fixed making the model fixture deterministic. Given other tests depend on the model fixture I expect it to be in a stable state and not change anytime (at least soon)

Copy link
Member

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Instead of creating a new fixture and sharing it among multiple tests, could you please just extend the existing test_create_tree_digraph() to be a "deep" test similar to the others in this file?

I don't believe that method modifies the Booster, it should be safe to run multiple calls over the same Booster in the same test.

And we don't use something like pytest-xdist to run tests in parallel, so it's ok to have a lot of testing happening in a single test case.

the random state is fixed making the model fixture deterministic.

I think here's you're referring to the breast_cancer_split fixture? Even with identical input data, there is still non-determinism in LightGBM (see #6731).

Please add the following parameters to the LGBMClassifier() call:

  • deterministic=True
  • n_jobs=1
  • force_row_wise=True

@daguirre11
Copy link
Contributor Author

Thanks!

Instead of creating a new fixture and sharing it among multiple tests, could you please just extend the existing test_create_tree_digraph() to be a "deep" test similar to the others in this file?

I don't believe that method modifies the Booster, it should be safe to run multiple calls over the same Booster in the same test.

And we don't use something like pytest-xdist to run tests in parallel, so it's ok to have a lot of testing happening in a single test case.

the random state is fixed making the model fixture deterministic.

I think here's you're referring to the breast_cancer_split fixture? Even with identical input data, there is still non-determinism in LightGBM (see #6731).

Please add the following parameters to the LGBMClassifier() call:

  • deterministic=True
  • n_jobs=1
  • force_row_wise=True

Thanks @jameslamb I will make these changes.

@jameslamb Yes I was referring to the breast_cancer_split. I see now the lightGBM model is not without the deterministic argument.

@daguirre11 daguirre11 requested a review from jameslamb February 24, 2026 20:29
@jameslamb jameslamb changed the title [python-package] Added unit test for graph viz [python-package] Add tests for more create_tree_digraph() inputs Feb 27, 2026
# test internal_count
graph = lgb.create_tree_digraph(gbm, tree_index=0, show_info=["internal_count"])
graph_body = "".join(graph.body)
assert "count: 200" in graph_body
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert "count: 200" in graph_body
assert "count: 200" in graph_body, graph_body

All of the CUDA jobs are failing on this case:

FAILED tests/python_package_test/test_plotting.py::test_create_tree_digraph - assert 'count: 200' in '\tgraph [nodesep=0.05 rankdir=LR ranksep=0.3]\n\tleaf0 [label=<leaf 0: 0.677> color=black fillcolor=white penw... \n \n \n > color=white shape=rectangle]\n'

(build link)

Let's add this error message to get a bit more logs about what's going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That failed too. The output was like this:

FAILED tests/python_package_test/test_plotting.py::test_create_tree_digraph - AssertionError: 	graph [nodesep=0.05 rankdir=LR ranksep=0.3]
  	leaf0 [label=<leaf 0: <B>0.677</B>> color=black fillcolor=white penwidth=1 shape=ellipse style=""]
  	split1 -> leaf0 [label=yes color=black penwidth=1]
  	leaf2 [label=<leaf 2: <B>0.343</B>> color=black fillcolor=white penwidth=1 shape=ellipse style=""]
  	split1 -> leaf2 [label=no color=black penwidth=1]
  	split1 [label=<<B>Column_20</B> &#8804;<B>17.510</B><br/>count: 346> color=black fillcolor="#ffdddd" penwidth=1 shape=rectangle style=filled]
  	split0 -> split1 [label=yes color=black penwidth=1]
  	leaf1 [label=<leaf 1: <B>0.286</B>> color=black fillcolor=white penwidth=1 shape=ellipse style=""]
  	split0 -> leaf1 [label=no color=black penwidth=1]
  	split0 [label=<<B>Column_27</B> &#8804;<B>0.146</B><br/>count: 512> color=black fillcolor="#ddffdd" penwidth=1 shape=rectangle style=filled]
  	legend [label=<
              <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
               <TR>
                <TD COLSPAN="2"><B>Monotone constraints</B></TD>
               </TR>
               <TR>
                <TD>Increasing</TD>
                <TD BGCOLOR="#ddffdd"></TD>
               </TR>
               <TR>
                <TD>Decreasing</TD>
                <TD BGCOLOR="#ffdddd"></TD>
               </TR>
              </TABLE>
             > color=white shape=rectangle]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants