Skip to content

Commit 87ff139

Browse files
committed
Fixed tutorial notebooks and added to test suite
1 parent e6fc068 commit 87ff139

5 files changed

Lines changed: 121 additions & 109 deletions

File tree

docs/source/tutorials/poisson_line_process/poisson_line_process_tutorial.ipynb

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 3,
99+
"execution_count": 2,
100100
"id": "d7d27532-7995-4d76-b40e-e6ae9e7cc645",
101101
"metadata": {},
102102
"outputs": [
@@ -276,7 +276,7 @@
276276
"[1000 rows x 7 columns]"
277277
]
278278
},
279-
"execution_count": 3,
279+
"execution_count": 2,
280280
"metadata": {},
281281
"output_type": "execute_result"
282282
}
@@ -299,7 +299,7 @@
299299
},
300300
{
301301
"cell_type": "code",
302-
"execution_count": 4,
302+
"execution_count": 3,
303303
"id": "ac297d2d-5a2f-4c33-bbdc-967d54e24e3f",
304304
"metadata": {},
305305
"outputs": [],
@@ -309,7 +309,6 @@
309309
"from causal_testing.specification.variable import Input, Output\n",
310310
"from causal_testing.specification.causal_dag import CausalDAG\n",
311311
"from causal_testing.specification.scenario import Scenario\n",
312-
"from causal_testing.specification.causal_specification import CausalSpecification\n",
313312
"\n",
314313
"# Define the input variables \n",
315314
"\n",
@@ -340,11 +339,7 @@
340339
" num_shapes_abs,\n",
341340
" num_shapes_unit})\n",
342341
"\n",
343-
"causal_dag = CausalDAG(dag_file) # Secondly, create the Causal DAG \n",
344-
"\n",
345-
"# Finally, we instantiate the CausalSpecification and pass in the scenario and Causal DAG\n",
346-
"\n",
347-
"causal_specification = CausalSpecification(scenario, causal_dag) "
342+
"causal_dag = CausalDAG(dag_file) # Secondly, create the Causal DAG "
348343
]
349344
},
350345
{
@@ -380,7 +375,7 @@
380375
},
381376
{
382377
"cell_type": "code",
383-
"execution_count": 5,
378+
"execution_count": 4,
384379
"id": "9b8491ab-0a90-4061-baee-8e1ecef7371d",
385380
"metadata": {},
386381
"outputs": [],
@@ -393,7 +388,7 @@
393388
"base_test_case = BaseTestCase(treatment_variable=intensity, outcome_variable=num_shapes_unit) # Create the base test case\n",
394389
"\n",
395390
"# Perform identification on the DAG using the base test case\n",
396-
"adjustment_set = causal_specification.causal_dag.identification(base_test_case) # Note: an empty adjustment set means there are no confounding variables that need to be controlled for"
391+
"adjustment_set = causal_dag.identification(base_test_case) # Note: an empty adjustment set means there are no confounding variables that need to be controlled for"
397392
]
398393
},
399394
{
@@ -406,7 +401,7 @@
406401
},
407402
{
408403
"cell_type": "code",
409-
"execution_count": 6,
404+
"execution_count": 5,
410405
"id": "fa53a888-68e1-4f6f-babf-16d3a206ea49",
411406
"metadata": {},
412407
"outputs": [],
@@ -445,7 +440,7 @@
445440
" \"height\": test_results.estimator.treatment_value,\n",
446441
" \"control\": test_results.estimator.control_value,\n",
447442
" \"treatment\": test_results.estimator.treatment_value,\n",
448-
" \"risk_ratio\": test_results.test_value.value[0],\n",
443+
" \"risk_ratio\": test_results.effect_estimate.value[0],\n",
449444
" }]"
450445
]
451446
},
@@ -459,7 +454,7 @@
459454
},
460455
{
461456
"cell_type": "code",
462-
"execution_count": 7,
457+
"execution_count": 6,
463458
"id": "6bc8be40-bc95-4187-8771-4ce096acc7b5",
464459
"metadata": {},
465460
"outputs": [
@@ -536,7 +531,7 @@
536531
"3 8 16 8 16 3.699311"
537532
]
538533
},
539-
"execution_count": 7,
534+
"execution_count": 6,
540535
"metadata": {},
541536
"output_type": "execute_result"
542537
}
@@ -561,7 +556,9 @@
561556
"cell_type": "markdown",
562557
"id": "e62216b2-60ed-49b7-a2eb-0ad755bd91fc",
563558
"metadata": {},
564-
"source": "From the above causal test results and the risk ratios, we can conclude that doubling the intensity does not cause the number of polygons per unit area to increase by a factor of 4 as we expected. Instead, it increases by factors ranging from 2.8 to 3.7, meaning that the metamorphic relation is violated at a 95% confidence level."
559+
"source": [
560+
"From the above causal test results and the risk ratios, we can conclude that doubling the intensity does not cause the number of polygons per unit area to increase by a factor of 4 as we expected. Instead, it increases by factors ranging from 2.8 to 3.7, meaning that the metamorphic relation is violated at a 95% confidence level."
561+
]
565562
},
566563
{
567564
"cell_type": "markdown",
@@ -583,7 +580,7 @@
583580
},
584581
{
585582
"cell_type": "code",
586-
"execution_count": 8,
583+
"execution_count": 7,
587584
"id": "67bf5061-720f-4b3a-a371-3ff3092e81e1",
588585
"metadata": {},
589586
"outputs": [],
@@ -594,17 +591,17 @@
594591
"\n",
595592
"base_test_case = BaseTestCase(treatment_variable=width, outcome_variable=num_shapes_unit) # Create the base test case\n",
596593
"\n",
597-
"adjustment_set = causal_specification.causal_dag.identification(base_test_case) # Calculate the adjustment set again (if it exists)\n",
594+
"adjustment_set = causal_dag.identification(base_test_case) # Calculate the adjustment set again (if it exists)\n",
598595
"\n",
599596
"for intensity in treatment_values:\n",
600597
" \n",
601-
" for width in control_values:\n",
598+
" for width_value in control_values:\n",
602599
" \n",
603600
" estimator = LinearRegressionEstimator(\n",
604601
" df=df, # Pass in the dataframe\n",
605602
" base_test_case = base_test_case, # Base test case we created above\n",
606-
" treatment_value = width + 1.0, # Changing the width\n",
607-
" control_value=float(width), # Baseline width values\n",
603+
" treatment_value = width_value + 1.0, # Changing the width\n",
604+
" control_value=float(width_value), # Baseline width values\n",
608605
" adjustment_set=adjustment_set, # Use the same adjustment set as list comprehension\n",
609606
" effect_modifiers={\"intensity\": intensity},\n",
610607
" formula=\"num_shapes_unit ~ width + I(intensity ** 2)+I(width ** -1)+intensity-1\", # Patsy formula describing a linear regression model\n",
@@ -614,7 +611,6 @@
614611
" base_test_case = base_test_case, # Pass in the base test case\n",
615612
" expected_causal_effect = Positive(), # We expect a positive increase\n",
616613
" estimate_type = \"ate_calculated\", # Calls the ate_calculated method in the linear regression estimator\n",
617-
" effect_modifier_configuration = {\"intensity\": intensity}, # Condition on (hold constant) the intensity value when calculating width\n",
618614
" estimator=estimator) # Pass in the estimator we created above\n",
619615
" \n",
620616
" test_results = causal_test_case.execute_test() # Execute the tests\n",
@@ -624,16 +620,16 @@
624620
" {\n",
625621
" \"control\": test_results.estimator.control_value,\n",
626622
" \"treatment\": test_results.estimator.treatment_value,\n",
627-
" \"intensity\": test_results.effect_modifier_configuration[\"intensity\"],\n",
628-
" \"ate\": test_results.test_value.value[0],\n",
629-
" \"ci_low\": test_results.confidence_intervals[0][0],\n",
630-
" \"ci_high\": test_results.confidence_intervals[1][0],\n",
623+
" \"intensity\": estimator.effect_modifiers[\"intensity\"],\n",
624+
" \"ate\": test_results.effect_estimate.value[0],\n",
625+
" \"ci_low\": test_results.effect_estimate.ci_low[0],\n",
626+
" \"ci_high\": test_results.effect_estimate.ci_high[0],\n",
631627
" }]\n"
632628
]
633629
},
634630
{
635631
"cell_type": "code",
636-
"execution_count": 9,
632+
"execution_count": 8,
637633
"id": "6c54392c-4e6b-42b3-b39a-e0d1d0ab25b7",
638634
"metadata": {},
639635
"outputs": [
@@ -775,7 +771,7 @@
775771
"9 1.0 2.0 2 -7.378642 -16.381136 1.623851"
776772
]
777773
},
778-
"execution_count": 9,
774+
"execution_count": 8,
779775
"metadata": {},
780776
"output_type": "execute_result"
781777
}
@@ -827,9 +823,9 @@
827823
],
828824
"metadata": {
829825
"kernelspec": {
830-
"display_name": "Python 3 (ipykernel)",
826+
"display_name": "Causal Testing Dev Env",
831827
"language": "python",
832-
"name": "python3"
828+
"name": "causal_testing_dev"
833829
},
834830
"language_info": {
835831
"codemirror_mode": {
@@ -841,7 +837,7 @@
841837
"name": "python",
842838
"nbconvert_exporter": "python",
843839
"pygments_lexer": "ipython3",
844-
"version": "3.10.12"
840+
"version": "3.11.14"
845841
}
846842
},
847843
"nbformat": 4,

docs/source/tutorials/vaccinating_elderly/vaccinating_elderly_tutorial.ipynb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,33 @@
119119
},
120120
{
121121
"cell_type": "code",
122-
"execution_count": 3,
122+
"execution_count": 2,
123123
"id": "e80a3064-9f5f-446e-b443-e7dd1d54d6a4",
124124
"metadata": {},
125125
"outputs": [
126126
{
127127
"name": "stdout",
128128
"output_type": "stream",
129129
"text": [
130-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Setting up Causal Testing Framework...\n",
131-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Loading DAG from dag.dot\n",
132-
"2025-10-31 13:49:34 - causal_testing.main - INFO - DAG loaded with 5 nodes and 3 edges\n",
133-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Loading data from 1 source(s)\n",
134-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Initial data shape: (60, 16)\n",
135-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Setup completed successfully\n",
136-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Loading test configurations from causal_tests.json\n",
137-
"2025-10-31 13:49:34 - root - INFO - Running tests in regular mode\n",
138-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Running causal tests...\n",
139-
"100%|████████████████████████████████████████████| 9/9 [00:00<00:00, 356.92it/s]\n",
140-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Saving results to causal_test_results.json\n",
141-
"2025-10-31 13:49:34 - causal_testing.main - INFO - Results saved successfully\n",
142-
"2025-10-31 13:49:34 - root - INFO - Causal testing completed successfully.\n"
130+
"Namespace(command='test', dag_path='dag.dot', output='causal_test_results.json', ignore_cycles=False, data_paths=['simulated_data.csv'], test_config='causal_tests.json', verbose=False, query=None, adequacy=False, bootstrap_size=None, silent=False, batch_size=0)\n",
131+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Setting up Causal Testing Framework...\n",
132+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Loading DAG from dag.dot\n",
133+
"2025-12-12 11:21:57 - causal_testing.main - INFO - DAG loaded with 5 nodes and 3 edges\n",
134+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Loading data from 1 source(s)\n",
135+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Initial data shape: (60, 16)\n",
136+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Setup completed successfully\n",
137+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Loading test configurations from causal_tests.json\n",
138+
"2025-12-12 11:21:57 - root - INFO - Running tests in regular mode\n",
139+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Running causal tests...\n",
140+
"100%|████████████████████████████████████████████| 9/9 [00:00<00:00, 358.51it/s]\n",
141+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Saving results to causal_test_results.json\n",
142+
"2025-12-12 11:21:57 - causal_testing.main - INFO - Results saved successfully\n",
143+
"2025-12-12 11:21:57 - root - INFO - Causal testing completed successfully.\n"
143144
]
144145
}
145146
],
146147
"source": [
147-
"!python -m causal_testing test --data_paths simulated_data.csv --dag_path dag.dot --test_config causal_tests.json --output causal_test_results.json"
148+
"!python -m causal_testing test --data-paths simulated_data.csv --dag-path dag.dot --test-config causal_tests.json --output causal_test_results.json"
148149
]
149150
},
150151
{
@@ -219,13 +220,21 @@
219220
"- [Documentation](https://causal-testing-framework.readthedocs.io/en/latest/index.html)\n",
220221
"- [Paper](https://dl.acm.org/doi/10.1145/3607184)"
221222
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"id": "80c1a475-cbee-4912-846b-584d252e90b0",
228+
"metadata": {},
229+
"outputs": [],
230+
"source": []
222231
}
223232
],
224233
"metadata": {
225234
"kernelspec": {
226-
"display_name": "Python 3 (ipykernel)",
235+
"display_name": "Causal Testing Dev Env",
227236
"language": "python",
228-
"name": "python3"
237+
"name": "causal_testing_dev"
229238
},
230239
"language_info": {
231240
"codemirror_mode": {
@@ -237,7 +246,7 @@
237246
"name": "python",
238247
"nbconvert_exporter": "python",
239248
"pygments_lexer": "ipython3",
240-
"version": "3.10.11"
249+
"version": "3.11.14"
241250
}
242251
},
243252
"nbformat": 4,

docs/source/tutorials/visualising_causal_test_results/visualise_causal_test_results.ipynb

Lines changed: 10 additions & 20 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)