-
Notifications
You must be signed in to change notification settings - Fork 6
Jmafoster1/visualisation #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jmafoster1
wants to merge
5
commits into
main
Choose a base branch
from
jmafoster1/visualisation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f1ca1d
Basic test outcome DAG visualisation in
jmafoster1 ea32975
Effect type now returns none for non-numeric effect estimates
jmafoster1 e183910
Distinguishing no effect from categoricals
jmafoster1 1ad2670
Markdown tables, but still not rendering properly in ipynb
jmafoster1 22429e6
Updated visualise tooltips
jmafoster1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
causal_testing/visualisation/causal_test_result_visualiser.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| This module implements a set of utilities to help visualise causal test results. | ||
| """ | ||
|
|
||
| import networkx as nx | ||
| import pandas as pd | ||
|
|
||
| from causal_testing.specification.causal_dag import CausalDAG | ||
| from causal_testing.testing.causal_test_case import CausalTestCase | ||
| from causal_testing.testing.causal_test_result import TestOutcome | ||
|
|
||
|
|
||
| def results_dag( | ||
| test_cases: list[CausalTestCase], | ||
| dag: CausalDAG, | ||
| output_file: str = None, | ||
| view_independences: bool = True, | ||
| colours: dict[TestOutcome, str] = None, | ||
| ) -> CausalDAG: | ||
| """ | ||
| View causal test results as a graph. | ||
|
|
||
| :param test_cases: The causal tests (with results). | ||
| :param dag: The causal DAG that corresponds to the test results. | ||
| :param output_file: The name of the file to write to. | ||
| :param view_independences: Whether to display failed independence tests. | ||
| :param colours: Optional dictionary of colours to display the test outcomes. | ||
| By default, pass=green, fail=red, inestimable=orange. | ||
| """ | ||
| default_colours = {TestOutcome.PASS: "green", TestOutcome.INESTIMABLE: "orange", TestOutcome.FAIL: "red"} | ||
|
|
||
| if colours is not None: | ||
| colours = default_colours | colours | ||
| else: | ||
| colours = default_colours | ||
|
|
||
| result_dag = nx.DiGraph() | ||
| result_dag.add_nodes_from(dag.nodes) | ||
| result_dag.add_edges_from(dag.edges) | ||
|
|
||
| for test in test_cases: | ||
| if test.result: | ||
| effect_estimate = pd.concat( | ||
| [ | ||
| test.result.effect_estimate.ci_low, | ||
| test.result.effect_estimate.value, | ||
| test.result.effect_estimate.ci_high, | ||
| ], | ||
| axis=1, | ||
| ) | ||
| effect_estimate.columns = ["ci_low", "estimate", "ci_high"] | ||
| if (test.treatment_variable, test.outcome_variable) in result_dag.edges: | ||
| result_dag[test.treatment_variable][test.outcome_variable]["label"] = test.result.effect_direction() | ||
| result_dag[test.treatment_variable][test.outcome_variable]["color"] = colours[test.result.outcome] | ||
| result_dag[test.treatment_variable][test.outcome_variable]["fontcolor"] = colours[test.result.outcome] | ||
| result_dag[test.treatment_variable][test.outcome_variable]["title"] = effect_estimate.to_html() | ||
|
|
||
| elif view_independences and test.result.outcome != TestOutcome.PASS: | ||
| result_dag.add_edge(test.treatment_variable, test.outcome_variable, ignore_cycles=True) | ||
| result_dag[test.treatment_variable][test.outcome_variable]["style"] = "dashed" | ||
| result_dag[test.treatment_variable][test.outcome_variable]["label"] = test.result.effect_direction() | ||
| result_dag[test.treatment_variable][test.outcome_variable]["color"] = colours[test.result.outcome] | ||
| result_dag[test.treatment_variable][test.outcome_variable]["fontcolor"] = colours[test.result.outcome] | ||
| result_dag[test.treatment_variable][test.outcome_variable]["title"] = effect_estimate.to_html() | ||
|
|
||
| if output_file is not None: | ||
| nx.drawing.nx_pydot.write_dot(result_dag, output_file) | ||
|
|
||
| return result_dag |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug fix: This should have been
.any()all along