Skip to content
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

[ENH] Add the ability to convert a DAG to an MAG #96

Merged
merged 28 commits into from
Sep 26, 2023

Conversation

aryan26roy
Copy link
Collaborator

Closes #95

Changes proposed in this pull request:

  • function to that takes a DAG as an argument and outputs a valid MAG.

The algorithm is described in Zhang et al. 2008 on page number 1877.

Before submitting

  • I've read and followed all steps in the Making a pull request
    section of the CONTRIBUTING docs.
  • I've updated or added any relevant docstrings following the syntax described in the
    Writing docstrings section of the CONTRIBUTING docs.
  • If this PR fixes a bug, I've added a test that will fail without my fix.
  • If this PR adds a new feature, I've added tests that sufficiently cover my new functionality.

After submitting

  • All GitHub Actions jobs for my pull request have passed.

aryan26roy and others added 3 commits September 8, 2023 22:02
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <50577809+aryan26roy@users.noreply.github.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Merging #96 (6cf1edf) into main (2d6f1d3) will decrease coverage by 2.67%.
Report is 9 commits behind head on main.
The diff coverage is 60.22%.

@@            Coverage Diff             @@
##             main      #96      +/-   ##
==========================================
- Coverage   80.02%   77.35%   -2.67%     
==========================================
  Files          39       42       +3     
  Lines        2838     3413     +575     
  Branches      759      984     +225     
==========================================
+ Hits         2271     2640     +369     
- Misses        408      570     +162     
- Partials      159      203      +44     
Files Coverage Δ
pywhy_graphs/classes/augmented.py 78.04% <100.00%> (+8.62%) ⬆️
pywhy_graphs/functional/additive.py 83.33% <100.00%> (+6.86%) ⬆️
pywhy_graphs/functional/linear.py 70.27% <90.00%> (+3.60%) ⬆️
pywhy_graphs/viz/draw.py 76.00% <77.77%> (-0.93%) ⬇️
pywhy_graphs/algorithms/multidomain.py 59.09% <22.22%> (+9.09%) ⬆️
pywhy_graphs/functional/multidomain.py 11.88% <7.69%> (+0.30%) ⬆️
pywhy_graphs/algorithms/generic.py 89.59% <92.16%> (+4.01%) ⬆️
pywhy_graphs/functional/discrete.py 60.37% <60.37%> (ø)
pywhy_graphs/functional/sampling.py 0.00% <0.00%> (ø)
pywhy_graphs/functional/base.py 57.44% <57.44%> (ø)
... and 1 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@adam2392
Copy link
Collaborator

adam2392 commented Sep 8, 2023

Can you rebase this? The GH files changed tab is showing a lot of files for some reason.

@aryan26roy
Copy link
Collaborator Author

Sure.

aryan26roy and others added 5 commits September 9, 2023 11:10
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

@adam2392 I think it's fine now.

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

@adam2392 I think I figured out how to implement the algorithm. Can you look over the comments and tell me if that is correct or not?

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

Yeah it looks reasonably correct. Having a few tests, examples that we can refer to visually will be helpful

pywhy_graphs/algorithms/generic.py Outdated Show resolved Hide resolved
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

Hey @adam2392 , how do I get/generate valid DAG, MAG pairs? The implementation is complete and I need to test it now.

@adam2392
Copy link
Collaborator

Nice!

Perhaps peruse papers or come up with your own and draw them in tetrad

@aryan26roy
Copy link
Collaborator Author

@adam2392 I found a pair on the internet and just wanted to confirm, is this a correct DAG and MAG pair? H is a latent variable.

@adam2392
Copy link
Collaborator

Seems right walking thru each condition of the DAG to MAG algo.

@aryan26roy
Copy link
Collaborator Author

I don't see how the bidirected edge from E to R came about. Isn't E present in the ancestral graph of R U S ?

@adam2392
Copy link
Collaborator

Oh yeah sorry. It should be E -> R right?

See the definition on pg 1877 and the last paragraph on section 2.3.

Maybe it would help if you explicitly wrote the algorithm into the docstring too.

@aryan26roy
Copy link
Collaborator Author

@adam2392 If two nodes are adjacent in a graph, can we say an inducing path exists between the two of them given all the conditions for an inducing path are satisfied?

@adam2392
Copy link
Collaborator

I believe the definition states this is trivially true. IIRC

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

Ah! I found a bug in my inducing paths implementation. I never check to see if either X or Y are members of L or S (which would immediately make it impossible for them to have an inducing path between them, according to what I understood.) Since this is a small thing to fix, can I just do it in this PR?

@aryan26roy
Copy link
Collaborator Author

@adam2392 in the MAG shouldn't A -> E be A - E? My implementation seems to be working correctly.

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

What's the reasoning?

A is an ancestor of E union S and E is an ancestor of A union S. When both are present in the ancestor sets its a undirected edge, right?

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

@adam2392 I added two more tests, one of them is from a zhang paper and the method worked on it on the first try! I think the implementation is correct. Is the number of tests ok?

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

Nice! I didn't evaluate the tests yet but I left some comments on the readability of the code.

pywhy_graphs/algorithms/tests/test_generic.py Show resolved Hide resolved
pywhy_graphs/algorithms/generic.py Outdated Show resolved Hide resolved
pywhy_graphs/algorithms/generic.py Outdated Show resolved Hide resolved
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

Okay the code is looking better.

the tests can be improved by explicitly checking for the conditions of the MAG

pywhy_graphs/algorithms/generic.py Show resolved Hide resolved
pywhy_graphs/algorithms/generic.py Outdated Show resolved Hide resolved
pywhy_graphs/algorithms/tests/test_generic.py Show resolved Hide resolved
aryan26roy and others added 5 commits September 23, 2023 11:51
Co-authored-by: Adam Li <adam2392@gmail.com>
Signed-off-by: Aryan Roy <50577809+aryan26roy@users.noreply.github.com>
Co-authored-by: Adam Li <adam2392@gmail.com>
Signed-off-by: Aryan Roy <50577809+aryan26roy@users.noreply.github.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @aryan26roy

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

Using frozenset was a better solution!

Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

Actually small code-smell. Otw lgtm

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
@aryan26roy
Copy link
Collaborator Author

@adam2392 Incorporated all the suggestions you made.

Copy link
Collaborator

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

LGTM assuming CIs pass!

@adam2392 adam2392 merged commit 9f3e202 into py-why:main Sep 26, 2023
26 of 28 checks passed
@aryan26roy aryan26roy deleted the aryan_dag_to_mag branch September 26, 2023 16:23
adam2392 added a commit to aryan26roy/pywhy-graphs that referenced this pull request Oct 30, 2023
* Add is_maximal function
* add DAG to MAG function

---------

Signed-off-by: Aryan Roy <aryanroy5678@gmail.com>
Co-authored-by: Adam Li <adam2392@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert DAG to a MAG
2 participants