[Repo Assist] perf(gcm): avoid redundant get_ordered_predecessors calls and pre-allocated DataFrame in fitting_sampling#1544
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
…ocated DataFrame in fitting_sampling In fit_causal_model_of_target, get_ordered_predecessors was called twice for every non-root node (once for fitting, once for PARENTS_DURING_FIT). Store the result once per node. In draw_samples, the pre-allocated pd.DataFrame(np.empty(...)) was filled column-by-column, which triggers repeated copy operations in pandas 2.x (copy-on-write). Switch to a dict of numpy arrays and construct the DataFrame once at the end. Also removes the now-unnecessary _parent_samples_of helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This was referenced May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated pull request from Repo Assist.
Summary
Two performance improvements in
dowhy/gcm/fitting_sampling.py:1. Eliminate redundant
get_ordered_predecessorscall infit_causal_model_of_targetPreviously,
get_ordered_predecessors(graph, node)was called twice per non-root node duringfit:Fix: compute once and reuse:
For a graph with N nodes, this halves the number of
sorted(graph.predecessors(node))calls made pergcm.fit().2. Replace pre-allocated DataFrame with dict of arrays in
draw_samplesPreviously, a DataFrame was pre-allocated with
pd.DataFrame(np.empty(...))— containing uninitialized data — then filled column-by-column, triggering repeated copy operations in pandas 2.x (copy-on-write).Fix: accumulate samples in a
Dict[node, np.ndarray]and create the DataFrame once at the end:This also removes the now-unnecessary
_parent_samples_ofhelper (which was private tofitting_sampling.py— different copies exist inwhatif.pyand_noise.pyand are unchanged).Root Cause
No bug — these are proactive performance improvements. The pre-allocated DataFrame approach worked but incurred unnecessary overhead.
Trade-offs
columns=sorted_nodesTest Status
Tests could not be run in this environment (no Python runtime with dependencies available). CI will validate. The changes are semantically equivalent transformations with no logic changes.
Relevant test files:
tests/gcm/test_data_generator.py— exercisesdraw_samplestests/gcm/test_graph.py— exercisesfitanddraw_samplestests/gcm/test_confidence_intervals_cms.py