-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_end_to_end.py
145 lines (115 loc) · 7 KB
/
test_end_to_end.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import tisane as ts
from tisane.effect_set import EffectSet, MainEffect, InteractionEffect, MixedEffect
from tisane.statistical_model import StatisticalModel
import unittest
class EndToEndTests(unittest.TestCase):
# def test_generate_effects_sets(self):
# analysis = ts.Tisane(task="explanation") # analysis has one task
# test_score = ts.Concept("Test Score")
# intelligence = ts.Concept("Intelligence")
# tutoring = ts.Concept("Tutoring")
# concepts = [test_score, intelligence, tutoring]
# analysis.addRelationship(intelligence, test_score, "cause")
# analysis.addRelationship(tutoring, test_score, "cause")
# analysis.addRelationship(intelligence, tutoring, "correlate")
# effects = analysis.generate_effects_sets(ivs=[intelligence, tutoring], dv=test_score)
# # check total number of effect sets
# self.assertEqual(len(effects), 7)
# # check each effect set is valid
# for es in effects:
# es_dict = es.to_dict()
# self.assertTrue(es_dict in DataForTests.expected_effects_set)
# def test_effects_ASP(self):
# analysis = ts.Tisane(task="explanation")
# # Add concepts
# test_score = ts.Concept("Test Score")
# intelligence = ts.Concept("Intelligence")
# tutoring = ts.Concept("Tutoring")
# concepts = [test_score, intelligence, tutoring]
# # Add relationships
# analysis.addRelationship(intelligence, test_score, "cause")
# analysis.addRelationship(tutoring, test_score, "cause")
# analysis.addRelationship(intelligence, tutoring, "correlate")
# # Specify data (schema)
# test_score.specifyData(dtype="numeric")
# intelligence.specifyData(dtype="numeric")
# tutoring.specifyData(dtype="nominal", categories=["After school", "Before school"])
# # tutoring.specifyData(dtype="nominal", categories=["After school", "Before school", "None"])
# # Get valid statistical models
# # Assert statistical properties needed for linear regression
# # Generate effects sets
# effects = analysis.generate_effects_sets(ivs=[intelligence, tutoring], dv=test_score)
# # Add individual variable assertions
# # Some assertions are inferred from data schema, see above
# self.assertTrue(test_score.getVariable().has_property_value(prop="dtype", val="numeric"))
# self.assertTrue(intelligence.getVariable().has_property_value(prop="dtype", val="numeric"))
# self.assertTrue(tutoring.getVariable().has_property_value(prop="dtype", val="nominal"))
# self.assertFalse(test_score.getVariable().has_property(prop="cardinality"))
# self.assertFalse(intelligence.getVariable().has_property(prop="cardinality"))
# self.assertTrue(tutoring.getVariable().has_property_value(prop="cardinality", val="binary"))
# # Add assertions that pertain to effects sets
# linear_reg_es = None
# for es in effects:
# if es.to_dict() == DataForTests.expected_effects_set[2]:
# linear_reg_es = es
# break
# linear_reg_es.assert_property(prop="tolerate_correlation", val=True)
# # Convert linear regression model EffectSet to statistical model
# linear_reg_sm = StatisticalModel.create(model_type='linear_regression', effect_set=linear_reg_es)
# # Add assertions that pertain to the models
# # Note: Make assertions that will involve interaction (interactions compile to these statements)
# # Note: These assertions should be made on the *MODEL* not the *Effect Set*
# linear_reg_sm.get_residuals().assert_property(prop="distribution", val="normal")
# linear_reg_sm.get_residuals().assert_property(prop="homoscedastic", val=True)
# # TODO: Compile all these assertions into constraints
# # Should be in KnowledgeBase class or main Tisane class?
# # Query KB for statistical models
# analysis.start_model(linear_reg_es)
def test_sample_tisane_program(self):
analysis = ts.Tisane(task="explanation")
### PHASE 0: CONCEPTUAL RELATIONSHIPS
# Add concepts
test_score = ts.Concept("Test Score")
intelligence = ts.Concept("Intelligence")
tutoring = ts.Concept("Tutoring")
# Add relationships
analysis.addRelationship(intelligence, test_score, "cause")
analysis.addRelationship(tutoring, test_score, "cause")
analysis.addRelationship(intelligence, tutoring, "correlate")
### PHASE 1: IV, DV SPECIFICATION
### PHASE 2: EFFECTS SETS GENERATION
# Generate effects sets
effects = analysis.generate_effects_sets(ivs=[intelligence, tutoring], dv=test_score)
### PHASE 3A: ASSERTIONS ABOUT VARIABLES AND SETS OF VARIABLES
# Specify data (schema) --> generates assertions about variables/data
# If have data: automatically detect and verify these based on uploaded data
test_score.specifyData(dtype="numeric")
intelligence.specifyData(dtype="numeric")
tutoring.specifyData(dtype="nominal", categories=["After school", "Before school"])
# THIS MIMICS END-USERS SELECTING A SET OF EFFECTS TO MODEL
linear_reg_es = None
for es in effects:
if es.to_dict() == DataForTests.expected_effects_set[2]:
linear_reg_es = es
break
# Add assertions that pertain to effects sets
linear_reg_es.assert_property(prop="tolerate_correlation", val=True)
linear_reg_es.assert_property(prop="normal_residuals", val=True)
linear_reg_es.assert_property(prop="homoscedastic_residuals", val=True)
# PHASE 3B: QUERYING KNOWLEDGE BASE
# TODO: TEST INCREMENTAL SOLVING ASPECT
valid_models = analysis.start_model(effect_set=linear_reg_es)
print(valid_models) # "Linear Regression"
class DataForTests:
test_score = ts.Concept("Test Score")
intelligence = ts.Concept("Intelligence")
tutoring = ts.Concept("Tutoring")
expected_effects_set = [
{'dv': test_score.name, 'main': set({intelligence.name}), 'interaction': None, 'mixed': None},
{'dv': test_score.name, 'main': set({tutoring.name}), 'interaction': None, 'mixed': None},
{'dv': test_score.name, 'main': set({tutoring.name, intelligence.name}), 'interaction': None, 'mixed': None},
{'dv': test_score.name, 'main': set({tutoring.name, intelligence.name}), 'interaction': set({(intelligence.name, tutoring.name)}), 'mixed': None},
{'dv': test_score.name, 'main': set({intelligence.name}), 'interaction': set({(intelligence.name, tutoring.name)}), 'mixed': None},
{'dv': test_score.name, 'main': set({tutoring.name}), 'interaction': set({(intelligence.name, tutoring.name)}), 'mixed': None},
{'dv': test_score.name, 'main': None, 'interaction': set({(intelligence.name, tutoring.name)}), 'mixed': None},
]