Skip to content

Commit 0a620f7

Browse files
committed
Test classes for validation suggester and tuebingen model suggester.
Signed-off-by: Grace Sng <[email protected]>
1 parent f6b7ef3 commit 0a620f7

File tree

7 files changed

+132
-36
lines changed

7 files changed

+132
-36
lines changed

pywhyllm/suggesters/tuebingen_model_suggester.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, llm=None):
2121
super().__init__(llm)
2222

2323
def suggest_description(
24-
self, variable, context=None, ask_reference=False
24+
self, variable, ask_reference=False
2525
):
2626
generate_description = self._build_description_program(variable)
2727

@@ -255,11 +255,11 @@ def _build_relationship_program(
255255
the answer within the tags, <answer>Yes/No</answer>, and the most influential reference within
256256
the tags <reference>Author, Title, Year of publication</reference>.
257257
\n\n\n----------------\n\n\n<answer>Yes</answer>\n<reference>Author, Title, Year of
258-
publication</reference>\n\n\n----------------\n\n\n<answer>No</answer> {{~/user}}"""
258+
publication</reference>\n\n\n----------------\n\n\n<answer>No</answer>"""
259259
else:
260260
query["user"] += """When consensus is reached, thinking carefully and factually, explain the council's answer.
261261
Provide the answer within the tags, <answer>Yes/No</answer>.
262-
\n\n\n----------------\n\n\n<answer>Yes</answer>\n\n\n----------------\n\n\n<answer>No</answer> {{~/user}}"""
262+
\n\n\n----------------\n\n\n<answer>Yes</answer>\n\n\n----------------\n\n\n<answer>No</answer>"""
263263

264264
elif use_strategy == Strategy.CoT:
265265
if use_description:

pywhyllm/suggesters/validation_suggester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def request_negative_controls(
6565
factors_list: list(),
6666
negative_controls_counter: list(),
6767
domain_expertise: str,
68-
analysis_context: list = CONTEXT
68+
analysis_context = CONTEXT
6969
):
7070
negative_controls_list: List[str] = list()
7171

pywhyllm/tests/model_suggester/data_providers/tuebingen_model_suggester_data_provider.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,55 @@
88

99
# MOCK_RESPONSES
1010
test_suggest_description_expected_response = "<description>Water is a transparent, tasteless, odorless, nearly colorless liquid that is essential for all life forms and covers approximately 71% of Earth's surface, also existing in solid (ice) and gas (vapor) states.</description>"
11-
test_suggest_onesided_relationship_expected_response = "<answer>A</answer>"
12-
test_suggest_relationship_expected_response = "<answer>Yes</answer> <reference>Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. \"Water, hydration and health.\" Nutrition reviews 68.8 (2010): 439-458.</reference>"
11+
test_suggest_onesided_relationship_a_cause_b_expected_response = "<answer>A</answer>"
12+
test_suggest_onesided_relationship_a_not_cause_b_expected_response = "<answer>B</answer>"
13+
test_suggest_relationship_a_cause_b_expected_response = "<answer>Yes</answer> <reference>Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. \"Water, hydration and health.\" Nutrition reviews 68.8 (2010): 439-458.</reference>"
14+
test_suggest_relationship_a_not_cause_b_expected_response = "<answer>No</answer> <reference>Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. \"Water, hydration and health.\" Nutrition reviews 68.8 (2010): 439-458.</reference>"
15+
1316
# ASSERTIONS
14-
test_suggest_description_expected_result = [
15-
"Water is a transparent, tasteless, odorless, nearly colorless liquid that is essential for all life forms and covers approximately 71% of Earth's surface, also existing in solid (ice) and gas (vapor) states."]
16-
test_suggest_onesided_relationship_expected_result = 1
17-
test__build_description_program_expected_result = {
17+
test_suggest_description_expected_result = ([
18+
"Water is a transparent, tasteless, odorless, nearly colorless liquid that is essential for all life forms and covers approximately 71% of Earth's surface, also existing in solid (ice) and gas (vapor) states."],
19+
[])
20+
test_suggest_onesided_relationship_a_cause_b_expected_result = 1
21+
test_suggest_onesided_relationship_a_not_cause_b_expected_result = 0
22+
test__build_description_program_no_context_no_reference_expected_result = {
1823
'system': 'You are a helpful assistant for writing concise and peer-reviewed descriptions. Your goal \n is to provide factual and succinct description of the given concept.',
1924
'user': " Describe the concept of water.\n In one sentence, provide a factual and succinct description of water\n Let's think step-by-step to make sure that we have a proper and clear description. Then provide \n your final answer within the tags, <description></description>."}
20-
test_suggest_relationship_expected_result = (1,
21-
[
22-
'Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. "Water, hydration and health." Nutrition reviews 68.8 (2010): 439-458.'])
25+
test__build_description_program_no_context_with_reference_expected_result = {
26+
'system': 'You are a helpful assistant for writing concise and peer-reviewed descriptions. Your goal \n is to provide factual and succinct description of the given concept.',
27+
'user': ' Describe the concept of water.\n In one sentence, provide a factual and succinct description of water"\n Then provide two research papers that support your description.\n Let\'s think step-by-step to make sure that we have a proper and clear description. Then provide \n your final answer within the tags, <description></description>, and each research paper within the \n tags <paper></paper>.'}
28+
test__build_description_program_with_context_with_reference_expected_result = {
29+
'system': 'You are a helpful assistant for writing concise and peer-reviewed descriptions. Your goal is \n to provide factual and succinct descriptions related to the given concept and context.',
30+
'user': "Using this context about the particular variable, describe the concept of water.\n In one sentence, provide a factual and succinct description of waterThen provide two research papers that support your description.\n Let's think step-by-step to make sure that we have a proper and clear description. Then provide your final \n answer within the tags, <description></description>, and each research paper within the tags <reference></reference>."}
31+
test__build_description_program_with_context_no_reference_expected_result = {
32+
'system': 'You are a helpful assistant for writing concise and peer-reviewed descriptions. Your goal is \n to provide factual and succinct descriptions related to the given concept and context.',
33+
'user': "Using this context about the particular variable, describe the concept of water.\n In one sentence, provide a factual and succinct description of water\n Let's think step-by-step to make sure that we have a proper and clear description. Then provide your final \n answer within the tags, <description></description>."}
34+
test_suggest_relationship_a_cause_b_expected_result = (1,
35+
[
36+
'Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. "Water, hydration and health." Nutrition reviews 68.8 (2010): 439-458.'])
37+
test_suggest_relationship_a_not_cause_b_expected_result = (0,
38+
[
39+
'Popkin, Barry M., Kristen E. D\'Anci, and Irwin H. Rosenberg. "Water, hydration and health." Nutrition reviews 68.8 (2010): 439-458.'])
2340
test__build_relationship_program_expected_result = {
24-
'system': 'You are a helpful assistant on causal reasoning and biology. Your goal is to answer \n questions about cause and effect in a factual and concise way.',
25-
'user': "can changing water intake change hydration level? Answer Yes or No.At each step, each expert include a reference to a research paper that supports \n their argument. They will provide a one sentence summary of the paper and how it supports their argument. \n Then they will answer whether a change in water intake changes hydration level. Answer Yes or No.\n When consensus is reached, thinking carefully and factually, explain the council's answer. Provide \n the answer within the tags, <answer>Yes/No</answer>, and the most influential reference within \n the tags <reference>Author, Title, Year of publication</reference>.\n \n\n\n----------------\n\n\n<answer>Yes</answer>\n<reference>Author, Title, Year of \n publication</reference>\n\n\n----------------\n\n\n<answer>No</answer> {~/user}"}
41+
'system': 'You are a helpful assistant on causal reasoning and biology. Your '
42+
'goal is to answer \n'
43+
' questions about cause and effect in a factual and '
44+
'concise way.',
45+
'user': 'can changing water intake change hydration level? Answer Yes or '
46+
'No.When consensus is reached, thinking carefully and factually, '
47+
"explain the council's answer. \n"
48+
' Provide the answer within the tags, '
49+
'<answer>Yes/No</answer>.\n'
50+
' \n'
51+
'\n'
52+
'\n'
53+
'----------------\n'
54+
'\n'
55+
'\n'
56+
'<answer>Yes</answer>\n'
57+
'\n'
58+
'\n'
59+
'----------------\n'
60+
'\n'
61+
'\n'
62+
'<answer>No</answer>'}

pywhyllm/tests/model_suggester/data_providers/validation_suggester_data_provider.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
"<answer>B</answer>", "<answer>B</answer>", "<answer>B</answer>"]
2121

2222
# ASSERTIONS
23-
test_latent_confounders_expected_results = ({'mental health': 1, 'socio-economic status': 1},
24-
['socio-economic status', 'mental health'])
25-
test_negative_controls_expected_results = ({'exercise habits': 1}, ['exercise habits'])
23+
test_suggest_latent_confounders_expected_results = ({'mental health': 1, 'socio-economic status': 1},
24+
[{'mental health': 1, 'socio-economic status': 1},
25+
['socio-economic status', 'mental health']])
26+
test_request_latent_confounders_expected_results = ({'mental health': 1, 'socio-economic status': 1},
27+
['socio-economic status', 'mental health'])
28+
test_suggest_negative_controls_expected_results = (
29+
{'exercise habits': 1}, [{'exercise habits': 1}, ['exercise habits']])
30+
test_request_negative_controls_expected_results = ({'exercise habits': 1}, ['exercise habits'])
2631
test_parent_critique_expected_results = []
2732
test_children_critique_expected_results = ['lung cancer']
2833
test_pairwise_critique_expected_results = ('smoking', 'lung cancer')

pywhyllm/tests/model_suggester/test_identification_suggester.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
from guidance.models._openai import OpenAI
44

55
from pywhyllm.suggesters.identification_suggester import IdentificationSuggester
6+
from pywhyllm.suggesters.model_suggester import ModelSuggester
67
from pywhyllm.tests.model_suggester.data_providers.model_suggester_data_provider import *
78
from pywhyllm.tests.model_suggester.data_providers.identification_suggester_data_provider import *
8-
from pywhyllm.tests.model_suggester.test_model_suggester import TestModelSuggester
99

1010
class TestIdentificationSuggester(unittest.TestCase):
1111
def test_suggest_backdoor(self):
12-
return TestModelSuggester().test_suggest_confounders()
12+
modeler = IdentificationSuggester()
13+
mock_llm = MagicMock(spec=OpenAI)
14+
modeler.llm = mock_llm
15+
mock_model_suggester = MagicMock(spec=ModelSuggester)
16+
modeler.model_suggester = mock_model_suggester
17+
mock_model_suggester.suggest_confounders = MagicMock(return_value=test_suggest_confounders_expected_results)
18+
result = modeler.suggest_backdoor(test_vars[0], test_vars[1], test_vars, test_domain_expertises_expected_result)
19+
assert result == test_suggest_confounders_expected_results
1320

1421
def test_suggest_mediators(self):
1522
modeler = IdentificationSuggester()

pywhyllm/tests/model_suggester/test_tuebingen_model_suggester.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_suggest_description(self):
1414

1515
mock_llm.__add__ = MagicMock(return_value=mock_llm)
1616
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_description_expected_response)
17-
result = modeler.suggest_description(variable)
17+
result = modeler.suggest_description(variable, True)
1818
assert result == test_suggest_description_expected_result
1919

2020
def test_suggest_onesided_relationship(self):
@@ -23,34 +23,55 @@ def test_suggest_onesided_relationship(self):
2323
modeler.llm = mock_llm
2424

2525
mock_llm.__add__ = MagicMock(return_value=mock_llm)
26-
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_onesided_relationship_expected_response)
26+
#Given the two variables and their descriptions, variable a causes variable b
27+
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_onesided_relationship_a_cause_b_expected_response)
2728
result = modeler.suggest_onesided_relationship(variable_a, description_a, variable_b, description_b)
28-
assert result == test_suggest_onesided_relationship_expected_result
29+
assert result == test_suggest_onesided_relationship_a_cause_b_expected_result
30+
31+
#Given the two variables and their descriptions, variable a does not cause variable b
32+
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_onesided_relationship_a_not_cause_b_expected_response)
33+
result = modeler.suggest_onesided_relationship(variable_a, description_a, variable_b, description_b)
34+
assert result == test_suggest_onesided_relationship_a_not_cause_b_expected_result
2935

3036
def test__build_description_program(self):
3137
modeler = TuebingenModelSuggester()
3238
mock_llm = MagicMock(spec=OpenAI)
3339
modeler.llm = mock_llm
34-
35-
result = modeler._build_description_program(variable)
36-
assert result == test__build_description_program_expected_result
40+
#Test no context, no reference
41+
result = modeler._build_description_program(variable, False, False)
42+
assert result == test__build_description_program_no_context_no_reference_expected_result
43+
#Test no context, with reference
44+
result = modeler._build_description_program(variable, False, True)
45+
assert result == test__build_description_program_no_context_with_reference_expected_result
46+
#Test with context, no reference
47+
result = modeler._build_description_program(variable, True, False)
48+
assert result == test__build_description_program_with_context_no_reference_expected_result
49+
#Test with context, with reference
50+
result = modeler._build_description_program(variable, True, True)
51+
assert result == test__build_description_program_with_context_with_reference_expected_result
3752

3853
def test_suggest_relationship(self):
3954
modeler = TuebingenModelSuggester()
4055
mock_llm = MagicMock(spec=OpenAI)
4156
modeler.llm = mock_llm
4257

4358
mock_llm.__add__ = MagicMock(return_value=mock_llm)
44-
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_relationship_expected_response)
59+
#Given the two variables and their descriptions, variable a causes variable b
60+
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_relationship_a_cause_b_expected_response)
61+
result = modeler.suggest_relationship(variable_a, variable_b, description_a, description_b, domain,
62+
strategy=Strategy.ToT_Single, ask_reference=True)
63+
assert result == test_suggest_relationship_a_cause_b_expected_result
64+
#Given the two variables and their descriptions, variable a does not cause variable b
65+
mock_llm.__getitem__ = MagicMock(return_value=test_suggest_relationship_a_not_cause_b_expected_response)
4566
result = modeler.suggest_relationship(variable_a, variable_b, description_a, description_b, domain,
4667
strategy=Strategy.ToT_Single, ask_reference=True)
47-
assert result == test_suggest_relationship_expected_result
68+
assert result == test_suggest_relationship_a_not_cause_b_expected_result
4869

4970
def test__build_relationship_program(self):
5071
modeler = TuebingenModelSuggester()
5172
mock_llm = MagicMock(spec=OpenAI)
5273
modeler.llm = mock_llm
5374

5475
result = modeler._build_relationship_program(variable_a, description_a, variable_b, description_b, domain,
55-
use_description=False, ask_reference=True)
76+
use_description=False, ask_reference=False)
5677
assert result == test__build_relationship_program_expected_result

0 commit comments

Comments
 (0)