-
Notifications
You must be signed in to change notification settings - Fork 231
Added some unit tests for GIN #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cb0a45
Added some unit tests for GIN
zhi-yi-huang fe29ec3
Merge branch 'cmu-phil:main' into main
zhi-yi-huang 948a59c
Fixed GIN
zhi-yi-huang d2d4381
Updated GIN
zhi-yi-huang c6d1dfa
Refactored the test code
zhi-yi-huang 8708ca8
Updated TestGIN.py
zhi-yi-huang 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,104 +1,88 @@ | ||
| import random | ||
| import sys | ||
| import io | ||
|
|
||
| sys.path.append("") | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import matplotlib.image as mpimg | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| from causallearn.search.HiddenCausal.GIN.GIN import GIN | ||
|
|
||
|
|
||
| class TestGIN(unittest.TestCase): | ||
| def test_case1(self): | ||
| sample_size = 1000 | ||
| np.random.seed(0) | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X1 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X3 = np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X4 = np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| indep_test_methods = ['kci', 'hsic'] | ||
|
|
||
| def test_case1(self): | ||
| sample_size = 500 | ||
| random.seed(42) | ||
| np.random.seed(42) | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) | ||
| L2 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(-1, 1, size=sample_size) | ||
| X1 = np.random.uniform(1.2, 1.8) * L1 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X2 = np.random.uniform(1.2, 1.8) * L1 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X3 = np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X4 = np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| data = np.array([X1, X2, X3, X4]).T | ||
| data = (data - np.mean(data, axis=0)) / np.std(data, axis=0) | ||
| g, k = GIN(data) | ||
| print(g, k) | ||
|
|
||
| # Visualization using pydot | ||
| from causallearn.utils.GraphUtils import GraphUtils | ||
| pyd = GraphUtils.to_pydot(g) | ||
| tmp_png = pyd.create_png(f="png") | ||
| fp = io.BytesIO(tmp_png) | ||
| img = mpimg.imread(fp, format='png') | ||
| plt.axis('off') | ||
| plt.imshow(img) | ||
| plt.show() | ||
|
|
||
| def test_case2(self): | ||
| sample_size = 1000 | ||
| np.random.seed(0) | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L3 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, | ||
| size=sample_size) ** 5 | ||
| X1 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X3 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X4 = np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X5 = np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X6 = np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X7 = np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X8 = np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X9 = np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| ground_truth = [[0, 1], [2, 3]] | ||
|
|
||
| data = np.array([X1, X2, X3, X4, X5, X6, X7, X8, X9]).T | ||
| data = (data - np.mean(data, axis=0)) / np.std(data, axis=0) | ||
| g, k = GIN(data) | ||
| print(g, k) | ||
|
|
||
| # Visualization using pydot | ||
| from causallearn.utils.GraphUtils import GraphUtils | ||
| pyd = GraphUtils.to_pydot(g) | ||
| tmp_png = pyd.create_png(f="png") | ||
| fp = io.BytesIO(tmp_png) | ||
| img = mpimg.imread(fp, format='png') | ||
| plt.axis('off') | ||
| plt.imshow(img) | ||
| plt.show() | ||
| TestGIN.run_gin_test(data, ground_truth, 0.05) | ||
|
|
||
| def test_case3(self): | ||
| sample_size = 1000 | ||
|
|
||
| def test_case2(self): | ||
| sample_size = 500 | ||
| random.seed(42) | ||
| np.random.seed(42) | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L3 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L4 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
|
|
||
| X1 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X2 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X3 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X4 = np.random.uniform(0.5, 2.0) * L1 + np.random.uniform(0.5, 2.0) * L2 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X5 = np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X6 = np.random.uniform(0.5, 2.0) * L3 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X7 = np.random.uniform(0.5, 2.0) * L4 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| X8 = np.random.uniform(0.5, 2.0) * L4 + np.random.uniform(-1, 1, size=sample_size) ** 5 | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) | ||
| L2 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(-1, 1, size=sample_size) | ||
| L3 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(1.2, 1.8) * L2 + np.random.uniform(-1, 1, size=sample_size) | ||
| X1 = np.random.uniform(1.2, 1.8) * L1 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X2 = np.random.uniform(1.2, 1.8) * L1 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X3 = np.random.uniform(1.2, 1.8) * L1 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X4 = np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X5 = np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X6 = np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X7 = np.random.uniform(1.2, 1.8) * L3 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X8 = np.random.uniform(1.2, 1.8) * L3 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X9 = np.random.uniform(1.2, 1.8) * L3 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| data = np.array([X1, X2, X3, X4, X5, X6, X7, X8, X9]).T | ||
| data = (data - np.mean(data, axis=0)) / np.std(data, axis=0) | ||
|
|
||
| ground_truth = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] | ||
|
|
||
| TestGIN.run_gin_test(data, ground_truth, 0.05) | ||
|
|
||
|
|
||
| def test_case3(self): | ||
| sample_size = 500 | ||
| random.seed(0) | ||
| np.random.seed(0) | ||
| L1 = np.random.uniform(-1, 1, size=sample_size) | ||
| L2 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(-1, 1, size=sample_size) | ||
| L3 = np.random.uniform(0.5, 0.8) * L1 + np.random.uniform(0.5, 0.8) * L2 + np.random.uniform(-1, 1, size=sample_size) | ||
| L4 = np.random.uniform(0.5, 0.8) * L1 + np.random.uniform(0.5, 0.8) * L2 + np.random.uniform(1.2, 1.8) * L3 + np.random.uniform(-1, 1, size=sample_size) | ||
| X1 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X2 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X3 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X4 = np.random.uniform(1.2, 1.8) * L1 + np.random.uniform(1.2, 1.8) * L2 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X5 = np.random.uniform(1.2, 1.8) * L3 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X6 = np.random.uniform(1.2, 1.8) * L3 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X7 = np.random.uniform(1.2, 1.8) * L4 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| X8 = np.random.uniform(1.2, 1.8) * L4 + 0.2 * np.random.uniform(-1, 1, size=sample_size) | ||
| data = np.array([X1, X2, X3, X4, X5, X6, X7, X8]).T | ||
| data = (data - np.mean(data, axis=0)) / np.std(data, axis=0) | ||
| g, k = GIN(data) | ||
| print(g, k) | ||
|
|
||
| # Visualization using pydot | ||
| from causallearn.utils.GraphUtils import GraphUtils | ||
| pyd = GraphUtils.to_pydot(g) | ||
| tmp_png = pyd.create_png(f="png") | ||
| fp = io.BytesIO(tmp_png) | ||
| img = mpimg.imread(fp, format='png') | ||
| plt.axis('off') | ||
| plt.imshow(img) | ||
| plt.show() | ||
|
|
||
| ground_truth = [[0, 1, 2, 3], [4, 5], [6, 7]] | ||
|
|
||
| TestGIN.run_gin_test(data, ground_truth, 0.05) | ||
|
|
||
| @staticmethod | ||
| def run_gin_test(data, ground_truth, alpha): | ||
| for indep_test_method in TestGIN.indep_test_methods: | ||
| _, causal_order = GIN(data, indep_test_method=indep_test_method, alpha=alpha) | ||
| causal_order = [sorted(cluster_i) for cluster_i in causal_order] | ||
| TestGIN.validate_result(ground_truth, causal_order) | ||
|
|
||
| @staticmethod | ||
| def validate_result(ground_truth, estimated_result): | ||
| assert len(ground_truth) == len(estimated_result) | ||
| for i in range(len(estimated_result)): | ||
| assert estimated_result[i] == ground_truth[i] | ||
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.
Why do you change the data generation parameters?
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.
The previous data generation parameters were set according to the paper, but generating according to the paper does not seem to be fully identifiable. So I changed the data generation parameters.
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.
Why is the old data not fully identifiable? We can prove it not identifiable or just our algorithm fail to do so? Sorry causal n00b lol
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.
It should be an independence test error caused by the data.