Skip to content

Commit 5924622

Browse files
adam2392jaron-lee
andauthored
[NETWORKX] Add MixedEdgeGraph into package (#29)
* Implementation of networkx MixedEdgeGraph for subclassing * M-separation implementation * update of poetry and numpydoc * re-organized examples into subdirectories Signed-off-by: Adam Li <[email protected]> Co-authored-by: Jaron Lee <[email protected]>
1 parent b4bbebe commit 5924622

38 files changed

+3110
-921
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
command: sudo apt update && sudo apt install -y pandoc optipng
7171
- python/install-packages:
7272
pkg-manager: poetry
73-
cache-version: "v1" # change to clear cache
73+
cache-version: "v2" # change to clear cache
7474
args: "--with docs"
7575
- run:
7676
name: Check poetry package versions
@@ -87,7 +87,7 @@ jobs:
8787
name: Build documentation
8888
command: |
8989
cd docs
90-
poetry run make html
90+
poetry run poe build_docs
9191
# Save the example test results
9292
- store_test_results:
9393
path: docs/_build/test-results

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ __pycache__/
66
.vscode
77
junit-results.xml
88

9+
*.gv
10+
*.png
11+
912
# C extensions
1013
*.so
1114
.DS_Store

CONTRIBUTING.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ With poetry installed, we have included a few convenience functions to check you
142142

143143
poetry install --with style, docs, test
144144

145-
Check code formatting with black:
145+
You can verify that your code will pass certain style, formatting and lint checks by running:
146+
147+
poetry run poe verify
148+
149+
``verify`` runs a sequence of tests that can also be run individually. For example, you can check code formatting with black:
146150

147151
poetry run poe check_format
148152

@@ -158,6 +162,16 @@ Finally, you should run some mypy type checks:
158162

159163
poetry run poe type_check
160164

165+
### Documentation
166+
161167
If you need to build the documentation locally and check for doc errors:
162168

163169
poetry run poe build_docs
170+
171+
### Dependency Changes
172+
173+
If you need to add new, or remove old dependencies, then you need to modify the ``pyproject.toml`` file and then also update the ``poetry.lock`` file, which version-controls all necessary dependencies. If you alter any dependency in the ``pyproject.toml`` file, you must run:
174+
175+
poetry update
176+
177+
To update the lock file.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
build-docs:
44
@echo "Building documentation"
55
make -C docs/ clean
6-
make -C docs/ html-noplot
6+
make -C docs/ html
77
cd docs/ && make view
88

99
clean-pyc:

docs/Makefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ changes:
169169
@echo
170170
@echo "The overview file is in $(BUILDDIR)/changes."
171171

172-
linkcheck:
173-
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
174-
@echo
175-
@echo "Link check complete; look for any errors in the above output " \
176-
"or in $(BUILDDIR)/linkcheck/output.txt."
177-
178172
doctest:
179173
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
180174
@echo "Testing of doctests in the sources finished, look at the " \

docs/api.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ and corresponding causal graphs in pywhy-graphs.
6363
graph_to_arr
6464
clearn_arr_to_graph
6565

66+
NetworkX Experimental Functionality
67+
===================================
68+
Currently, NetworkX does not support mixed-edge graphs, which are crucial
69+
for representing causality with latent confounders and selection bias. The
70+
following represent functionality that we intend to PR eventually into
71+
networkx. They are included in pywhy-graphs as a temporary bridge. We
72+
welcome feedback.
73+
74+
.. currentmodule:: pywhy_graphs.networkx
75+
.. autosummary::
76+
:toctree: generated/
77+
78+
MixedEdgeGraph
79+
bidirected_to_unobserved_confounder
80+
m_separated
81+
6682
Visualization of causal graphs
6783
==============================
6884
Visualization of causal graphs is different compared to networkx because causal graphs

docs/conf.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import os
88
import sys
99
from datetime import datetime
10+
import warnings
1011

12+
import numpy as np
13+
import networkx as nx
1114
import sphinx_gallery # noqa: F401
12-
from sphinx_gallery.sorting import ExampleTitleSortKey
15+
from sphinx_gallery.sorting import ExampleTitleSortKey, ExplicitOrder
1316

1417
# -- Path setup --------------------------------------------------------------
1518

@@ -48,14 +51,14 @@
4851
"sphinx.ext.doctest",
4952
"sphinx.ext.intersphinx",
5053
"sphinx_issues",
51-
"sphinx.ext.mathjax",
5254
"sphinx.ext.viewcode",
55+
# "nbsphinx", # enables building Jupyter notebooks and rendering
56+
"sphinx.ext.mathjax",
5357
"sphinx_gallery.gen_gallery",
5458
"sphinxcontrib.bibtex",
5559
"sphinx_copybutton",
5660
"numpydoc",
57-
"IPython.sphinxext.ipython_console_highlighting",
58-
"nbsphinx",
61+
# "IPython.sphinxext.ipython_console_highlighting",
5962
]
6063

6164
# configure sphinx-copybutton
@@ -106,7 +109,7 @@
106109
"attributes",
107110
"dictionary",
108111
"ArrayLike",
109-
"nx.MixedEdgeGraph",
112+
"pywhy_nx.MixedEdgeGraph",
110113
# pywhy-graphs
111114
"causal",
112115
"Node",
@@ -161,6 +164,7 @@
161164
"ADMG": "pywhy_graphs.ADMG",
162165
"PAG": "pywhy_graphs.PAG",
163166
"CPDAG": "pywhy_graphs.CPDAG",
167+
"pywhy_nx.MixedEdgeGraph": "pywhy_graphs.networkx.MixedEdgeGraph",
164168
# joblib
165169
"joblib.Parallel": "joblib.Parallel",
166170
# pandas
@@ -182,12 +186,13 @@
182186
# directories to ignore when looking for source files.
183187
# This pattern also affects html_static_path and html_extra_path.
184188
exclude_patterns = [
185-
"auto_examples/index.rst",
186189
"_build",
187190
"Thumbs.db",
188-
".DS_Store",
189191
"**.ipynb_checkpoints",
190-
"auto_examples/*.rst",
192+
# "auto_examples/*.rst",
193+
# "auto_examples/index.rst",
194+
# "auto_examples/mixededge/index.rst",
195+
# "auto_examples/mixededge/*.rst",
191196
]
192197

193198
source_suffix = [".rst", ".md"]
@@ -214,7 +219,6 @@
214219
# The master toctree document.
215220
master_doc = "index"
216221

217-
218222
# -- Options for HTML output -------------------------------------------------
219223

220224
# The theme to use for HTML and HTML Help pages. See the documentation for
@@ -253,24 +257,47 @@
253257
scrapers = ("matplotlib",)
254258

255259
sphinx_gallery_conf = {
260+
# Modules for which function level galleries are created. In
261+
# this case sphinx_gallery and numpy in a tuple of strings.
256262
"doc_module": "pywhy_graphs",
263+
# Insert links to documentation of objects in the examples
257264
"reference_url": {
258265
"pywhy_graphs": None,
259266
},
260267
"backreferences_dir": "generated",
261268
"plot_gallery": "True", # Avoid annoying Unicode/bool default warning
262-
"within_subsection_order": ExampleTitleSortKey,
263269
"examples_dirs": ["../examples"],
264270
"gallery_dirs": ["auto_examples"],
265-
"filename_pattern": "^((?!sgskip).)*$",
271+
"within_subsection_order": ExampleTitleSortKey,
272+
"subsection_order": ExplicitOrder(
273+
[
274+
"../examples/mixededge",
275+
"../examples/intro",
276+
"../examples/visualization",
277+
]
278+
),
279+
# "filename_pattern": "^((?!sgskip).)*$",
280+
"filename_pattern": r"\.py",
266281
"matplotlib_animations": True,
267282
"compress_images": ("images", "thumbnails"),
268283
"image_scrapers": scrapers,
284+
'show_memory': not sys.platform.startswith(('win', 'darwin')),
269285
}
270286

287+
# Add pygraphviz png scraper, if available
288+
try:
289+
from pygraphviz.scraper import PNGScraper
290+
291+
sphinx_gallery_conf["image_scrapers"] += (PNGScraper(),)
292+
except ImportError:
293+
pass
294+
271295
# Custom sidebar templates, maps document names to template names.
272296
html_sidebars = {
273297
"index": ["search-field.html"],
298+
"install": [],
299+
"tutorial": [],
300+
"auto_examples/index": [],
274301
}
275302

276303
html_context = {
@@ -286,7 +313,17 @@
286313
nitpick_ignore = [
287314
("py:obj", "nx.MixedEdgeGraph"),
288315
("py:obj", "networkx.MixedEdgeGraph"),
316+
("py:obj", "pywhy_graphs.networkx.MixedEdgeGraph"),
317+
("py:obj", "pywhy_nx.MixedEdgeGraph"),
289318
("py:class", "networkx.classes.mixededge.MixedEdgeGraph"),
290319
("py:class", "numpy._typing._array_like._SupportsArray"),
291320
("py:class", "numpy._typing._nested_sequence._NestedSequence"),
292321
]
322+
323+
324+
# -- Warnings management -----------------------------------------------------
325+
def setup(app):
326+
# Ignore .ipynb files
327+
app.registry.source_suffix.pop(".ipynb", None)
328+
329+
warnings.filterwarnings("ignore", category=UserWarning)

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Contents
2020

2121
installation
2222
api
23-
use
23+
Usage<use>
2424
whats_new
2525

2626
Team

docs/use.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
:orphan:
2-
3-
Examples using pywhy-graphs
4-
===========================
1+
How to use pywhy-graphs with examples and tutorials
2+
===================================================
53

64
To be able to effectively use pywhy-graphs, look at some of the examples here
75
to learn everything you need! We give an overview of concepts in causal graphs.
86

9-
107
.. include:: auto_examples/index.rst
11-
:start-after: :orphan:
8+
:start-after: :orphan:

docs/whats_new/v0.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Changelog
3232
- |Feature| Implement an array API wrapper to convert between causal graphs in pywhy-graphs and causal graphs in ``causal-learn``, by `Adam Li`_ (:pr:`16`)
3333
- |Feature| Implement an acyclification algorithm for converting cyclic graphs to acyclic with :func:`pywhy_graphs.algorithms.acyclification`, by `Adam Li`_ (:pr:`17`)
3434
- |Feature| Adding a layout for the nodes positions in the :func:`pywhy_graphs.viz.draw` function, by `Julien Siebert`_ (:pr:`26`)
35+
- |Feature| Add :class:`pywhy_graphs.networkx.MixedEdgeGraph` for mixed-edge graphs, by `Adam Li`_ (:pr:`29`)
3536

3637
Code and Documentation Contributors
3738
-----------------------------------

0 commit comments

Comments
 (0)