Skip to content

Commit

Permalink
Merge pull request #35 from causy-dev/workspace-dataloaders
Browse files Browse the repository at this point in the history
feat(workspaces): workspace init process (create algorithm, dataloade…
  • Loading branch information
this-is-sofia authored May 22, 2024
2 parents 3884b28 + d8af202 commit 86ae7cf
Show file tree
Hide file tree
Showing 50 changed files with 2,752 additions and 523 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This runs a web server on port 8000 which allows you to visualize the causal dis
Use a default algorithm

```python
from causy.algorithms import PC
from causy.causal_discovery.constraint.algorithms import PC
from causy.graph_utils import retrieve_edges

model = PC()
Expand Down Expand Up @@ -90,61 +90,61 @@ from causy.common_pipeline_steps.exit_conditions import ExitOnNoActions
from causy.graph_model import graph_model_factory
from causy.common_pipeline_steps.logic import Loop
from causy.common_pipeline_steps.calculation import CalculatePearsonCorrelations
from causy.independence_tests.common import (
CorrelationCoefficientTest,
PartialCorrelationTest,
ExtendedPartialCorrelationTestMatrix,
from causy.causal_discovery.constraint.independence_tests.common import (
CorrelationCoefficientTest,
PartialCorrelationTest,
ExtendedPartialCorrelationTestMatrix,
)
from causy.orientation_rules.pc import (
ColliderTest,
NonColliderTest,
FurtherOrientTripleTest,
OrientQuadrupleTest,
FurtherOrientQuadrupleTest,
from causy.causal_discovery.constraint.orientation_rules.pc import (
ColliderTest,
NonColliderTest,
FurtherOrientTripleTest,
OrientQuadrupleTest,
FurtherOrientQuadrupleTest,
)
from causy.interfaces import CausyAlgorithm
from causy.algorithms.pc import PC_EDGE_TYPES
from causy.models import Algorithm
from causy.causal_discovery.constraint.algorithms.pc import PC_EDGE_TYPES
from causy.graph_utils import retrieve_edges

CustomPC = graph_model_factory(
CausyAlgorithm(
pipeline_steps=[
CalculatePearsonCorrelations(),
CorrelationCoefficientTest(threshold=0.05),
PartialCorrelationTest(threshold=0.05),
ExtendedPartialCorrelationTestMatrix(threshold=0.05),
ColliderTest(),
Loop(
Algorithm(
pipeline_steps=[
NonColliderTest(),
FurtherOrientTripleTest(),
OrientQuadrupleTest(),
FurtherOrientQuadrupleTest(),
CalculatePearsonCorrelations(),
CorrelationCoefficientTest(threshold=0.05),
PartialCorrelationTest(threshold=0.05),
ExtendedPartialCorrelationTestMatrix(threshold=0.05),
ColliderTest(),
Loop(
pipeline_steps=[
NonColliderTest(),
FurtherOrientTripleTest(),
OrientQuadrupleTest(),
FurtherOrientQuadrupleTest(),
],
exit_condition=ExitOnNoActions(),
),
],
exit_condition=ExitOnNoActions(),
),
],
name="CustomPC",
edge_types=PC_EDGE_TYPES,
)
name="CustomPC",
edge_types=PC_EDGE_TYPES,
)
)

model = CustomPC()

model.create_graph_from_data(
[
{"a": 1, "b": 0.3},
{"a": 0.5, "b": 0.2}
]
[
{"a": 1, "b": 0.3},
{"a": 0.5, "b": 0.2}
]
)
model.create_all_possible_edges()
model.execute_pipeline_steps()
edges = retrieve_edges(model.graph)

for edge in edges:
print(
f"{edge[0].name} -> {edge[1].name}: {model.graph.edges[edge[0]][edge[1]]}"
)
print(
f"{edge[0].name} -> {edge[1].name}: {model.graph.edges[edge[0]][edge[1]]}"
)
```

### Supported algorithms
Expand Down
1 change: 1 addition & 0 deletions causy/causal_discovery/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .constraint.algorithms import AVAILABLE_ALGORITHMS
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@
from causy.generators import PairsWithNeighboursGenerator, RandomSampleGenerator
from causy.graph_model import graph_model_factory
from causy.common_pipeline_steps.logic import Loop, ApplyActionsTogether
from causy.independence_tests.common import (
from causy.causal_discovery.constraint.independence_tests.common import (
CorrelationCoefficientTest,
PartialCorrelationTest,
ExtendedPartialCorrelationTestMatrix,
)
from causy.common_pipeline_steps.calculation import (
CalculatePearsonCorrelations,
)
from causy.interfaces import AS_MANY_AS_FIELDS, ComparisonSettings, CausyAlgorithm
from causy.orientation_rules.pc import (
from causy.interfaces import AS_MANY_AS_FIELDS
from causy.models import ComparisonSettings, Algorithm
from causy.causal_discovery.constraint.orientation_rules.pc import (
ColliderTest,
NonColliderTest,
FurtherOrientTripleTest,
OrientQuadrupleTest,
FurtherOrientQuadrupleTest,
)
from causy.variables import (
FloatVariable,
VariableReference,
IntegerVariable,
)

PC_DEFAULT_THRESHOLD = 0.005

Expand Down Expand Up @@ -55,18 +61,19 @@
PC_EDGE_TYPES = [DirectedEdge(), UndirectedEdge()]

PC = graph_model_factory(
CausyAlgorithm(
Algorithm(
pipeline_steps=[
CalculatePearsonCorrelations(display_name="Calculate Pearson Correlations"),
CorrelationCoefficientTest(
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
display_name="Correlation Coefficient Test",
),
PartialCorrelationTest(
threshold=PC_DEFAULT_THRESHOLD, display_name="Partial Correlation Test"
threshold=VariableReference(name="threshold"),
display_name="Partial Correlation Test",
),
ExtendedPartialCorrelationTestMatrix(
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
display_name="Extended Partial Correlation Test Matrix",
),
*PC_ORIENTATION_RULES,
Expand All @@ -77,19 +84,24 @@
edge_types=PC_EDGE_TYPES,
extensions=[PC_GRAPH_UI_EXTENSION],
name="PC",
variables=[FloatVariable(name="threshold", value=PC_DEFAULT_THRESHOLD)],
)
)

PCStable = graph_model_factory(
CausyAlgorithm(
Algorithm(
pipeline_steps=[
CalculatePearsonCorrelations(),
ApplyActionsTogether(
pipeline_steps=[
CorrelationCoefficientTest(threshold=PC_DEFAULT_THRESHOLD),
PartialCorrelationTest(threshold=PC_DEFAULT_THRESHOLD),
CorrelationCoefficientTest(
threshold=VariableReference(name="threshold")
),
PartialCorrelationTest(
threshold=VariableReference(name="threshold"),
),
ExtendedPartialCorrelationTestMatrix(
threshold=PC_DEFAULT_THRESHOLD
threshold=VariableReference(name="threshold"),
),
]
),
Expand All @@ -99,27 +111,28 @@
edge_types=PC_EDGE_TYPES,
extensions=[PC_GRAPH_UI_EXTENSION],
name="PCStable",
variables=[FloatVariable(name="threshold", value=PC_DEFAULT_THRESHOLD)],
)
)


ParallelPC = graph_model_factory(
CausyAlgorithm(
Algorithm(
pipeline_steps=[
CalculatePearsonCorrelations(display_name="Calculate Pearson Correlations"),
CorrelationCoefficientTest(
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
display_name="Correlation Coefficient Test",
),
PartialCorrelationTest(
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
parallel=True,
chunk_size_parallel_processing=50000,
display_name="Partial Correlation Test",
),
ExtendedPartialCorrelationTestMatrix(
# run first a sampled version of the test so we can minimize the number of tests in the full version
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
display_name="Sampled Extended Partial Correlation Test Matrix",
chunk_size_parallel_processing=5000,
parallel=True,
Expand All @@ -132,11 +145,11 @@
),
),
chunked=False,
every_nth=200,
every_nth=VariableReference(name="every_nth_sample"),
),
),
ExtendedPartialCorrelationTestMatrix(
threshold=PC_DEFAULT_THRESHOLD,
threshold=VariableReference(name="threshold"),
display_name="Extended Partial Correlation Test Matrix",
chunk_size_parallel_processing=20000,
parallel=True,
Expand All @@ -156,5 +169,9 @@
edge_types=PC_EDGE_TYPES,
extensions=[PC_GRAPH_UI_EXTENSION],
name="ParallelPC",
variables=[
FloatVariable(name="threshold", value=PC_DEFAULT_THRESHOLD),
IntegerVariable(name="every_nth_sample", value=200),
],
)
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
PipelineStepInterface,
BaseGraphInterface,
NodeInterface,
TestResult,
TestResultAction,
AS_MANY_AS_FIELDS,
ComparisonSettings,
GeneratorInterface,
PipelineStepInterfaceType,
)
from causy.models import ComparisonSettings, TestResultAction, TestResult
from causy.variables import IntegerParameter, BoolParameter

logger = logging.getLogger(__name__)

Expand All @@ -27,8 +26,8 @@ class CorrelationCoefficientTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=2, max=2)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: List[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -64,8 +63,8 @@ class PartialCorrelationTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=3, max=3)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: Tuple[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -141,8 +140,8 @@ class ExtendedPartialCorrelationTestMatrix(
comparison_settings=ComparisonSettings(min=4, max=AS_MANY_AS_FIELDS),
shuffle_combinations=False,
)
chunk_size_parallel_processing: int = 1000
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1000
parallel: BoolParameter = False

def process(
self, nodes: List[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -245,8 +244,8 @@ class ExtendedPartialCorrelationTestLinearRegression(
comparison_settings=ComparisonSettings(min=4, max=AS_MANY_AS_FIELDS),
shuffle_combinations=False,
)
chunk_size_parallel_processing: int = 1000
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1000
parallel: BoolParameter = False

def process(
self, nodes: List[str], graph: BaseGraphInterface
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

from causy.generators import AllCombinationsGenerator
from causy.interfaces import (
TestResultAction,
PipelineStepInterface,
ComparisonSettings,
BaseGraphInterface,
TestResult,
GeneratorInterface,
PipelineStepInterfaceType,
)
from causy.models import ComparisonSettings, TestResultAction, TestResult


class ColliderRuleFCI(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from causy.generators import AllCombinationsGenerator
from causy.interfaces import (
BaseGraphInterface,
TestResult,
TestResultAction,
PipelineStepInterface,
ComparisonSettings,
GeneratorInterface,
PipelineStepInterfaceType,
)
from causy.models import ComparisonSettings, TestResultAction, TestResult
from causy.variables import IntegerParameter, BoolParameter


# theory for all orientation rules with pictures:
# https://hpi.de/fileadmin/user_upload/fachgebiete/plattner/teaching/CausalInference/2019/Introduction_to_Constraint-Based_Causal_Structure_Learning.pdf
Expand All @@ -24,8 +24,8 @@ class ColliderTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=2, max=2)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: Tuple[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -92,8 +92,8 @@ class NonColliderTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=2, max=2)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: Tuple[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -158,8 +158,8 @@ class FurtherOrientTripleTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=2, max=2)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: Tuple[str], graph: BaseGraphInterface
Expand Down Expand Up @@ -216,8 +216,8 @@ class OrientQuadrupleTest(
generator: Optional[GeneratorInterface] = AllCombinationsGenerator(
comparison_settings=ComparisonSettings(min=2, max=2)
)
chunk_size_parallel_processing: int = 1
parallel: bool = False
chunk_size_parallel_processing: IntegerParameter = 1
parallel: BoolParameter = False

def process(
self, nodes: Tuple[str], graph: BaseGraphInterface
Expand Down
Loading

0 comments on commit 86ae7cf

Please sign in to comment.