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

Backend-agnostic shinanegans #23

Closed
wants to merge 30 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f163e64
ABC for labelled items
willGraham01 Mar 17, 2025
377d880
Base distributions are labelled now
willGraham01 Mar 17, 2025
a21cebf
Ordering of class attributes
willGraham01 Mar 17, 2025
42f3974
Add labels to distribution classes
willGraham01 Mar 17, 2025
0e6348f
Graphs and nodes are labelled, save for the Node Protocol
willGraham01 Mar 17, 2025
dd5f337
Add backend module with signature casting method
willGraham01 Mar 17, 2025
6f306cc
Refactor typehints as they're going to be used often
willGraham01 Mar 17, 2025
0da5639
Skeleton Translation class
willGraham01 Mar 17, 2025
117e2ed
Stub translator class, needs more generic backend framework
willGraham01 Mar 17, 2025
5ad459c
Base class for backend-agnostic objects
willGraham01 Mar 17, 2025
3dd3345
Create Translator class, now very much backend agnostic
willGraham01 Mar 17, 2025
a4fbd9a
Test the BackednAgnostic ABC
willGraham01 Mar 17, 2025
cce38c9
Hide convert signature function
willGraham01 Mar 17, 2025
739f561
Fix docstring info
willGraham01 Mar 17, 2025
6543f7d
Make logic of _signature_can_be_cast more robust
willGraham01 Mar 18, 2025
8627aa2
Tests for validating signatures don't have multiple VAR_ arguments
willGraham01 Mar 18, 2025
1451a28
Tests for _signature_can_be_cast no.1
willGraham01 Mar 18, 2025
d889f70
Merge branch 'main' into wgraham/argument-mashing
willGraham01 Mar 18, 2025
86138ba
Ignore all pycaches plz
willGraham01 Mar 18, 2025
bb77cb8
Juggle which functions return which objects
willGraham01 Mar 19, 2025
8448e96
Test coverage for _signature_can_be_cast
willGraham01 Mar 19, 2025
2aeb307
Test convert_signature, my brain hurts
willGraham01 Mar 19, 2025
0876b47
Docstring typo
willGraham01 Mar 19, 2025
882089b
Merge branch 'main' into wgraham/argument-mashing
willGraham01 Mar 19, 2025
eecfa67
More docstring errors
willGraham01 Mar 19, 2025
38c9c18
Pull backend_agnostic class
willGraham01 Mar 20, 2025
61eb331
Pull backend_agnostic tests
willGraham01 Mar 20, 2025
59f6af3
Test positive and negative case
willGraham01 Mar 20, 2025
2e91b2a
Tidy docstrings and methods
willGraham01 Mar 20, 2025
f108176
Merge branch 'wgraham/backend-agnostic-abc' into wgraham/argument-mas…
willGraham01 Mar 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Graphs and nodes are labelled, save for the Node Protocol
willGraham01 committed Mar 17, 2025
commit 0e6348f83597ff3fdc7df330fef70cb106d96e37
13 changes: 5 additions & 8 deletions src/causalprog/graph/graph.py
Original file line number Diff line number Diff line change
@@ -2,21 +2,23 @@

import networkx as nx

from causalprog._abc.labelled import Labelled

from .node import Node


class Graph:
class Graph(Labelled):
"""A directed acyclic graph that represents a causality tree."""

def __init__(self, graph: nx.Graph, label: str) -> None:
"""Initialise a graph from a NetworkX graph."""
super().__init__(label=label)

for node in graph.nodes:
if not isinstance(node, Node):
msg = f"Invalid node: {node}"
raise TypeError(msg)

self._label = label

self._graph = graph.copy()
self._nodes = list(graph.nodes())
self._depth_first_nodes = list(nx.algorithms.dfs_postorder_nodes(graph))
@@ -29,8 +31,3 @@ def __init__(self, graph: nx.Graph, label: str) -> None:
msg = "Cannot yet create graph with multiple outcome nodes"
raise ValueError(msg)
self._outcome = outcomes[0]

@property
def label(self) -> str:
"""The label of the graph."""
return self._label
22 changes: 8 additions & 14 deletions src/causalprog/graph/node.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
from abc import abstractmethod
from typing import Protocol, runtime_checkable

from causalprog._abc.labelled import Labelled


class DistributionFamily:
"""Placeholder class."""
@@ -34,7 +36,7 @@ def is_outcome(self) -> bool:
"""Identify if the node is an outcome."""


class RootDistributionNode:
class RootDistributionNode(Labelled):
"""A root node containing a distribution family."""

def __init__(
@@ -45,19 +47,15 @@ def __init__(
is_outcome: bool = False,
) -> None:
"""Initialise the node."""
super().__init__(label=label)

self._dfamily = family
self._label = label
self._outcome = is_outcome

def __repr__(self) -> str:
"""Representation."""
return f'RootDistributionNode("{self._label}")'

@property
def label(self) -> str:
"""The label of the node."""
return self._label

@property
def is_root(self) -> bool:
"""Identify if the node is a root."""
@@ -69,7 +67,7 @@ def is_outcome(self) -> bool:
return self._outcome


class DistributionNode:
class DistributionNode(Labelled):
"""A node containing a distribution family that depends on its parents."""

def __init__(
@@ -80,19 +78,15 @@ def __init__(
is_outcome: bool = False,
) -> None:
"""Initialise the node."""
super().__init__(label=label)

self._dfamily = family
self._label = label
self._outcome = is_outcome

def __repr__(self) -> str:
"""Representation."""
return f'DistributionNode("{self._label}")'

@property
def label(self) -> str:
"""The label of the node."""
return self._label

@property
def is_root(self) -> bool:
"""Identify if the node is a root."""