edsl 0.1.39.dev3__py3-none-any.whl → 0.1.39.dev4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- edsl/Base.py +413 -332
- edsl/BaseDiff.py +260 -260
- edsl/TemplateLoader.py +24 -24
- edsl/__init__.py +57 -49
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +1071 -867
- edsl/agents/AgentList.py +551 -413
- edsl/agents/Invigilator.py +284 -233
- edsl/agents/InvigilatorBase.py +257 -270
- edsl/agents/PromptConstructor.py +272 -354
- edsl/agents/QuestionInstructionPromptBuilder.py +128 -0
- edsl/agents/QuestionTemplateReplacementsBuilder.py +137 -0
- edsl/agents/__init__.py +2 -3
- edsl/agents/descriptors.py +99 -99
- edsl/agents/prompt_helpers.py +129 -129
- edsl/agents/question_option_processor.py +172 -0
- edsl/auto/AutoStudy.py +130 -117
- edsl/auto/StageBase.py +243 -230
- edsl/auto/StageGenerateSurvey.py +178 -178
- edsl/auto/StageLabelQuestions.py +125 -125
- edsl/auto/StagePersona.py +61 -61
- edsl/auto/StagePersonaDimensionValueRanges.py +88 -88
- edsl/auto/StagePersonaDimensionValues.py +74 -74
- edsl/auto/StagePersonaDimensions.py +69 -69
- edsl/auto/StageQuestions.py +74 -73
- edsl/auto/SurveyCreatorPipeline.py +21 -21
- edsl/auto/utilities.py +218 -224
- edsl/base/Base.py +279 -279
- edsl/config.py +177 -157
- edsl/conversation/Conversation.py +290 -290
- edsl/conversation/car_buying.py +59 -58
- edsl/conversation/chips.py +95 -95
- edsl/conversation/mug_negotiation.py +81 -81
- edsl/conversation/next_speaker_utilities.py +93 -93
- edsl/coop/CoopFunctionsMixin.py +15 -0
- edsl/coop/ExpectedParrotKeyHandler.py +125 -0
- edsl/coop/PriceFetcher.py +54 -54
- edsl/coop/__init__.py +2 -2
- edsl/coop/coop.py +1106 -1028
- edsl/coop/utils.py +131 -131
- edsl/data/Cache.py +573 -555
- edsl/data/CacheEntry.py +230 -233
- edsl/data/CacheHandler.py +168 -149
- edsl/data/RemoteCacheSync.py +186 -78
- edsl/data/SQLiteDict.py +292 -292
- edsl/data/__init__.py +5 -4
- edsl/data/hack.py +10 -0
- edsl/data/orm.py +10 -10
- edsl/data_transfer_models.py +74 -73
- edsl/enums.py +202 -175
- edsl/exceptions/BaseException.py +21 -21
- edsl/exceptions/__init__.py +54 -54
- edsl/exceptions/agents.py +54 -42
- edsl/exceptions/cache.py +5 -5
- edsl/exceptions/configuration.py +16 -16
- edsl/exceptions/coop.py +10 -10
- edsl/exceptions/data.py +14 -14
- edsl/exceptions/general.py +34 -34
- edsl/exceptions/inference_services.py +5 -0
- edsl/exceptions/jobs.py +33 -33
- edsl/exceptions/language_models.py +63 -63
- edsl/exceptions/prompts.py +15 -15
- edsl/exceptions/questions.py +109 -91
- edsl/exceptions/results.py +29 -29
- edsl/exceptions/scenarios.py +29 -22
- edsl/exceptions/surveys.py +37 -37
- edsl/inference_services/AnthropicService.py +106 -87
- edsl/inference_services/AvailableModelCacheHandler.py +184 -0
- edsl/inference_services/AvailableModelFetcher.py +215 -0
- edsl/inference_services/AwsBedrock.py +118 -120
- edsl/inference_services/AzureAI.py +215 -217
- edsl/inference_services/DeepInfraService.py +18 -18
- edsl/inference_services/GoogleService.py +143 -148
- edsl/inference_services/GroqService.py +20 -20
- edsl/inference_services/InferenceServiceABC.py +80 -147
- edsl/inference_services/InferenceServicesCollection.py +138 -97
- edsl/inference_services/MistralAIService.py +120 -123
- edsl/inference_services/OllamaService.py +18 -18
- edsl/inference_services/OpenAIService.py +236 -224
- edsl/inference_services/PerplexityService.py +160 -163
- edsl/inference_services/ServiceAvailability.py +135 -0
- edsl/inference_services/TestService.py +90 -89
- edsl/inference_services/TogetherAIService.py +172 -170
- edsl/inference_services/data_structures.py +134 -0
- edsl/inference_services/models_available_cache.py +118 -118
- edsl/inference_services/rate_limits_cache.py +25 -25
- edsl/inference_services/registry.py +41 -41
- edsl/inference_services/write_available.py +10 -10
- edsl/jobs/AnswerQuestionFunctionConstructor.py +223 -0
- edsl/jobs/Answers.py +43 -56
- edsl/jobs/FetchInvigilator.py +47 -0
- edsl/jobs/InterviewTaskManager.py +98 -0
- edsl/jobs/InterviewsConstructor.py +50 -0
- edsl/jobs/Jobs.py +823 -898
- edsl/jobs/JobsChecks.py +172 -147
- edsl/jobs/JobsComponentConstructor.py +189 -0
- edsl/jobs/JobsPrompts.py +270 -268
- edsl/jobs/JobsRemoteInferenceHandler.py +311 -239
- edsl/jobs/JobsRemoteInferenceLogger.py +239 -0
- edsl/jobs/RequestTokenEstimator.py +30 -0
- edsl/jobs/__init__.py +1 -1
- edsl/jobs/async_interview_runner.py +138 -0
- edsl/jobs/buckets/BucketCollection.py +104 -63
- edsl/jobs/buckets/ModelBuckets.py +65 -65
- edsl/jobs/buckets/TokenBucket.py +283 -251
- edsl/jobs/buckets/TokenBucketAPI.py +211 -0
- edsl/jobs/buckets/TokenBucketClient.py +191 -0
- edsl/jobs/check_survey_scenario_compatibility.py +85 -0
- edsl/jobs/data_structures.py +120 -0
- edsl/jobs/decorators.py +35 -0
- edsl/jobs/interviews/Interview.py +396 -661
- edsl/jobs/interviews/InterviewExceptionCollection.py +99 -99
- edsl/jobs/interviews/InterviewExceptionEntry.py +186 -186
- edsl/jobs/interviews/InterviewStatistic.py +63 -63
- edsl/jobs/interviews/InterviewStatisticsCollection.py +25 -25
- edsl/jobs/interviews/InterviewStatusDictionary.py +78 -78
- edsl/jobs/interviews/InterviewStatusLog.py +92 -92
- edsl/jobs/interviews/ReportErrors.py +66 -66
- edsl/jobs/interviews/interview_status_enum.py +9 -9
- edsl/jobs/jobs_status_enums.py +9 -0
- edsl/jobs/loggers/HTMLTableJobLogger.py +304 -0
- edsl/jobs/results_exceptions_handler.py +98 -0
- edsl/jobs/runners/JobsRunnerAsyncio.py +151 -466
- edsl/jobs/runners/JobsRunnerStatus.py +297 -330
- edsl/jobs/tasks/QuestionTaskCreator.py +244 -242
- edsl/jobs/tasks/TaskCreators.py +64 -64
- edsl/jobs/tasks/TaskHistory.py +470 -450
- edsl/jobs/tasks/TaskStatusLog.py +23 -23
- edsl/jobs/tasks/task_status_enum.py +161 -163
- edsl/jobs/tokens/InterviewTokenUsage.py +27 -27
- edsl/jobs/tokens/TokenUsage.py +34 -34
- edsl/language_models/ComputeCost.py +63 -0
- edsl/language_models/LanguageModel.py +626 -668
- edsl/language_models/ModelList.py +164 -155
- edsl/language_models/PriceManager.py +127 -0
- edsl/language_models/RawResponseHandler.py +106 -0
- edsl/language_models/RegisterLanguageModelsMeta.py +184 -184
- edsl/language_models/ServiceDataSources.py +0 -0
- edsl/language_models/__init__.py +2 -3
- edsl/language_models/fake_openai_call.py +15 -15
- edsl/language_models/fake_openai_service.py +61 -61
- edsl/language_models/key_management/KeyLookup.py +63 -0
- edsl/language_models/key_management/KeyLookupBuilder.py +273 -0
- edsl/language_models/key_management/KeyLookupCollection.py +38 -0
- edsl/language_models/key_management/__init__.py +0 -0
- edsl/language_models/key_management/models.py +131 -0
- edsl/language_models/model.py +256 -0
- edsl/language_models/repair.py +156 -156
- edsl/language_models/utilities.py +65 -64
- edsl/notebooks/Notebook.py +263 -258
- edsl/notebooks/NotebookToLaTeX.py +142 -0
- edsl/notebooks/__init__.py +1 -1
- edsl/prompts/Prompt.py +352 -362
- edsl/prompts/__init__.py +2 -2
- edsl/questions/ExceptionExplainer.py +77 -0
- edsl/questions/HTMLQuestion.py +103 -0
- edsl/questions/QuestionBase.py +518 -664
- edsl/questions/QuestionBasePromptsMixin.py +221 -217
- edsl/questions/QuestionBudget.py +227 -227
- edsl/questions/QuestionCheckBox.py +359 -359
- edsl/questions/QuestionExtract.py +180 -182
- edsl/questions/QuestionFreeText.py +113 -114
- edsl/questions/QuestionFunctional.py +166 -166
- edsl/questions/QuestionList.py +223 -231
- edsl/questions/QuestionMatrix.py +265 -0
- edsl/questions/QuestionMultipleChoice.py +330 -286
- edsl/questions/QuestionNumerical.py +151 -153
- edsl/questions/QuestionRank.py +314 -324
- edsl/questions/Quick.py +41 -41
- edsl/questions/SimpleAskMixin.py +74 -73
- edsl/questions/__init__.py +27 -26
- edsl/questions/{AnswerValidatorMixin.py → answer_validator_mixin.py} +334 -289
- edsl/questions/compose_questions.py +98 -98
- edsl/questions/data_structures.py +20 -0
- edsl/questions/decorators.py +21 -21
- edsl/questions/derived/QuestionLikertFive.py +76 -76
- edsl/questions/derived/QuestionLinearScale.py +90 -87
- edsl/questions/derived/QuestionTopK.py +93 -93
- edsl/questions/derived/QuestionYesNo.py +82 -82
- edsl/questions/descriptors.py +427 -413
- edsl/questions/loop_processor.py +149 -0
- edsl/questions/prompt_templates/question_budget.jinja +13 -13
- edsl/questions/prompt_templates/question_checkbox.jinja +32 -32
- edsl/questions/prompt_templates/question_extract.jinja +11 -11
- edsl/questions/prompt_templates/question_free_text.jinja +3 -3
- edsl/questions/prompt_templates/question_linear_scale.jinja +11 -11
- edsl/questions/prompt_templates/question_list.jinja +17 -17
- edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -33
- edsl/questions/prompt_templates/question_numerical.jinja +36 -36
- edsl/questions/{QuestionBaseGenMixin.py → question_base_gen_mixin.py} +168 -161
- edsl/questions/question_registry.py +177 -177
- edsl/questions/{RegisterQuestionsMeta.py → register_questions_meta.py} +71 -71
- edsl/questions/{ResponseValidatorABC.py → response_validator_abc.py} +188 -174
- edsl/questions/response_validator_factory.py +34 -0
- edsl/questions/settings.py +12 -12
- edsl/questions/templates/budget/answering_instructions.jinja +7 -7
- edsl/questions/templates/budget/question_presentation.jinja +7 -7
- edsl/questions/templates/checkbox/answering_instructions.jinja +10 -10
- edsl/questions/templates/checkbox/question_presentation.jinja +22 -22
- edsl/questions/templates/extract/answering_instructions.jinja +7 -7
- edsl/questions/templates/likert_five/answering_instructions.jinja +10 -10
- edsl/questions/templates/likert_five/question_presentation.jinja +11 -11
- edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -5
- edsl/questions/templates/linear_scale/question_presentation.jinja +5 -5
- edsl/questions/templates/list/answering_instructions.jinja +3 -3
- edsl/questions/templates/list/question_presentation.jinja +5 -5
- edsl/questions/templates/matrix/__init__.py +1 -0
- edsl/questions/templates/matrix/answering_instructions.jinja +5 -0
- edsl/questions/templates/matrix/question_presentation.jinja +20 -0
- edsl/questions/templates/multiple_choice/answering_instructions.jinja +9 -9
- edsl/questions/templates/multiple_choice/question_presentation.jinja +11 -11
- edsl/questions/templates/numerical/answering_instructions.jinja +6 -6
- edsl/questions/templates/numerical/question_presentation.jinja +6 -6
- edsl/questions/templates/rank/answering_instructions.jinja +11 -11
- edsl/questions/templates/rank/question_presentation.jinja +15 -15
- edsl/questions/templates/top_k/answering_instructions.jinja +8 -8
- edsl/questions/templates/top_k/question_presentation.jinja +22 -22
- edsl/questions/templates/yes_no/answering_instructions.jinja +6 -6
- edsl/questions/templates/yes_no/question_presentation.jinja +11 -11
- edsl/results/CSSParameterizer.py +108 -108
- edsl/results/Dataset.py +587 -424
- edsl/results/DatasetExportMixin.py +594 -731
- edsl/results/DatasetTree.py +295 -275
- edsl/results/MarkdownToDocx.py +122 -0
- edsl/results/MarkdownToPDF.py +111 -0
- edsl/results/Result.py +557 -465
- edsl/results/Results.py +1183 -1165
- edsl/results/ResultsExportMixin.py +45 -43
- edsl/results/ResultsGGMixin.py +121 -121
- edsl/results/TableDisplay.py +125 -198
- edsl/results/TextEditor.py +50 -0
- edsl/results/__init__.py +2 -2
- edsl/results/file_exports.py +252 -0
- edsl/results/{ResultsFetchMixin.py → results_fetch_mixin.py} +33 -33
- edsl/results/{Selector.py → results_selector.py} +145 -135
- edsl/results/{ResultsToolsMixin.py → results_tools_mixin.py} +98 -98
- edsl/results/smart_objects.py +96 -0
- edsl/results/table_data_class.py +12 -0
- edsl/results/table_display.css +77 -77
- edsl/results/table_renderers.py +118 -0
- edsl/results/tree_explore.py +115 -115
- edsl/scenarios/ConstructDownloadLink.py +109 -0
- edsl/scenarios/DocumentChunker.py +102 -0
- edsl/scenarios/DocxScenario.py +16 -0
- edsl/scenarios/FileStore.py +511 -632
- edsl/scenarios/PdfExtractor.py +40 -0
- edsl/scenarios/Scenario.py +498 -601
- edsl/scenarios/ScenarioHtmlMixin.py +65 -64
- edsl/scenarios/ScenarioList.py +1458 -1287
- edsl/scenarios/ScenarioListExportMixin.py +45 -52
- edsl/scenarios/ScenarioListPdfMixin.py +239 -261
- edsl/scenarios/__init__.py +3 -4
- edsl/scenarios/directory_scanner.py +96 -0
- edsl/scenarios/file_methods.py +85 -0
- edsl/scenarios/handlers/__init__.py +13 -0
- edsl/scenarios/handlers/csv.py +38 -0
- edsl/scenarios/handlers/docx.py +76 -0
- edsl/scenarios/handlers/html.py +37 -0
- edsl/scenarios/handlers/json.py +111 -0
- edsl/scenarios/handlers/latex.py +5 -0
- edsl/scenarios/handlers/md.py +51 -0
- edsl/scenarios/handlers/pdf.py +68 -0
- edsl/scenarios/handlers/png.py +39 -0
- edsl/scenarios/handlers/pptx.py +105 -0
- edsl/scenarios/handlers/py.py +294 -0
- edsl/scenarios/handlers/sql.py +313 -0
- edsl/scenarios/handlers/sqlite.py +149 -0
- edsl/scenarios/handlers/txt.py +33 -0
- edsl/scenarios/{ScenarioJoin.py → scenario_join.py} +131 -127
- edsl/scenarios/scenario_selector.py +156 -0
- edsl/shared.py +1 -1
- edsl/study/ObjectEntry.py +173 -173
- edsl/study/ProofOfWork.py +113 -113
- edsl/study/SnapShot.py +80 -80
- edsl/study/Study.py +521 -528
- edsl/study/__init__.py +4 -4
- edsl/surveys/ConstructDAG.py +92 -0
- edsl/surveys/DAG.py +148 -148
- edsl/surveys/EditSurvey.py +221 -0
- edsl/surveys/InstructionHandler.py +100 -0
- edsl/surveys/Memory.py +31 -31
- edsl/surveys/MemoryManagement.py +72 -0
- edsl/surveys/MemoryPlan.py +244 -244
- edsl/surveys/Rule.py +327 -326
- edsl/surveys/RuleCollection.py +385 -387
- edsl/surveys/RuleManager.py +172 -0
- edsl/surveys/Simulator.py +75 -0
- edsl/surveys/Survey.py +1280 -1801
- edsl/surveys/SurveyCSS.py +273 -261
- edsl/surveys/SurveyExportMixin.py +259 -259
- edsl/surveys/{SurveyFlowVisualizationMixin.py → SurveyFlowVisualization.py} +181 -179
- edsl/surveys/SurveyQualtricsImport.py +284 -284
- edsl/surveys/SurveyToApp.py +141 -0
- edsl/surveys/__init__.py +5 -3
- edsl/surveys/base.py +53 -53
- edsl/surveys/descriptors.py +60 -56
- edsl/surveys/instructions/ChangeInstruction.py +48 -49
- edsl/surveys/instructions/Instruction.py +56 -65
- edsl/surveys/instructions/InstructionCollection.py +82 -77
- edsl/templates/error_reporting/base.html +23 -23
- edsl/templates/error_reporting/exceptions_by_model.html +34 -34
- edsl/templates/error_reporting/exceptions_by_question_name.html +16 -16
- edsl/templates/error_reporting/exceptions_by_type.html +16 -16
- edsl/templates/error_reporting/interview_details.html +115 -115
- edsl/templates/error_reporting/interviews.html +19 -19
- edsl/templates/error_reporting/overview.html +4 -4
- edsl/templates/error_reporting/performance_plot.html +1 -1
- edsl/templates/error_reporting/report.css +73 -73
- edsl/templates/error_reporting/report.html +117 -117
- edsl/templates/error_reporting/report.js +25 -25
- edsl/test_h +1 -0
- edsl/tools/__init__.py +1 -1
- edsl/tools/clusters.py +192 -192
- edsl/tools/embeddings.py +27 -27
- edsl/tools/embeddings_plotting.py +118 -118
- edsl/tools/plotting.py +112 -112
- edsl/tools/summarize.py +18 -18
- edsl/utilities/PrettyList.py +56 -0
- edsl/utilities/SystemInfo.py +28 -28
- edsl/utilities/__init__.py +22 -22
- edsl/utilities/ast_utilities.py +25 -25
- edsl/utilities/data/Registry.py +6 -6
- edsl/utilities/data/__init__.py +1 -1
- edsl/utilities/data/scooter_results.json +1 -1
- edsl/utilities/decorators.py +77 -77
- edsl/utilities/gcp_bucket/cloud_storage.py +96 -96
- edsl/utilities/gcp_bucket/example.py +50 -0
- edsl/utilities/interface.py +627 -627
- edsl/utilities/is_notebook.py +18 -0
- edsl/utilities/is_valid_variable_name.py +11 -0
- edsl/utilities/naming_utilities.py +263 -263
- edsl/utilities/remove_edsl_version.py +24 -0
- edsl/utilities/repair_functions.py +28 -28
- edsl/utilities/restricted_python.py +70 -70
- edsl/utilities/utilities.py +436 -424
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev4.dist-info}/LICENSE +21 -21
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev4.dist-info}/METADATA +13 -11
- edsl-0.1.39.dev4.dist-info/RECORD +361 -0
- edsl/language_models/KeyLookup.py +0 -30
- edsl/language_models/registry.py +0 -190
- edsl/language_models/unused/ReplicateBase.py +0 -83
- edsl/results/ResultsDBMixin.py +0 -238
- edsl-0.1.39.dev3.dist-info/RECORD +0 -277
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev4.dist-info}/WHEEL +0 -0
edsl/surveys/Survey.py
CHANGED
@@ -1,1801 +1,1280 @@
|
|
1
|
-
"""A Survey is collection of questions that can be administered to an Agent."""
|
2
|
-
|
3
|
-
from __future__ import annotations
|
4
|
-
import re
|
5
|
-
import
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
from
|
18
|
-
from edsl.
|
19
|
-
from edsl.surveys
|
20
|
-
from edsl.surveys
|
21
|
-
from
|
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
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
"""
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
>>>
|
244
|
-
>>>
|
245
|
-
|
246
|
-
>>>
|
247
|
-
|
248
|
-
>>> s.
|
249
|
-
|
250
|
-
"""
|
251
|
-
return
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
)
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
"""
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
s.
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
def
|
342
|
-
"""
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
>>> s.
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
>>> s.
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
}
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
>>>
|
486
|
-
>>>
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
return
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
>>>
|
529
|
-
>>> s = Survey().add_question(
|
530
|
-
>>> s.
|
531
|
-
|
532
|
-
|
533
|
-
>>>
|
534
|
-
>>> s.
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
"
|
553
|
-
|
554
|
-
|
555
|
-
>>> s.
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
>>>
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
self
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
)
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
)
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
"""
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
"""
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
>>> s = Survey
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
>>>
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
"""
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
>>> s = Survey.example()
|
1103
|
-
>>> s
|
1104
|
-
|
1105
|
-
"""
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
)
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
def
|
1151
|
-
self
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
:
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
c = Cache()
|
1283
|
-
else:
|
1284
|
-
c = cache
|
1285
|
-
jobs: "Jobs" = self.get_job(model=model, agent=agent, **kwargs)
|
1286
|
-
return await jobs.run_async(
|
1287
|
-
cache=c, disable_remote_inference=disable_remote_inference
|
1288
|
-
)
|
1289
|
-
|
1290
|
-
def run(self, *args, **kwargs) -> "Results":
|
1291
|
-
"""Turn the survey into a Job and runs it.
|
1292
|
-
|
1293
|
-
>>> from edsl import QuestionFreeText
|
1294
|
-
>>> s = Survey([QuestionFreeText.example()])
|
1295
|
-
>>> from edsl.language_models import LanguageModel
|
1296
|
-
>>> m = LanguageModel.example(test_model = True, canned_response = "Great!")
|
1297
|
-
>>> results = s.by(m).run(cache = False, disable_remote_cache = True, disable_remote_inference = True)
|
1298
|
-
>>> results.select('answer.*')
|
1299
|
-
Dataset([{'answer.how_are_you': ['Great!']}])
|
1300
|
-
"""
|
1301
|
-
from edsl.jobs.Jobs import Jobs
|
1302
|
-
|
1303
|
-
return Jobs(survey=self).run(*args, **kwargs)
|
1304
|
-
|
1305
|
-
# region: Survey flow
|
1306
|
-
def next_question(
|
1307
|
-
self, current_question: Union[str, QuestionBase], answers: dict
|
1308
|
-
) -> Union[QuestionBase, EndOfSurvey.__class__]:
|
1309
|
-
"""
|
1310
|
-
Return the next question in a survey.
|
1311
|
-
|
1312
|
-
:param current_question: The current question in the survey.
|
1313
|
-
:param answers: The answers for the survey so far
|
1314
|
-
|
1315
|
-
- If called with no arguments, it returns the first question in the survey.
|
1316
|
-
- If no answers are provided for a question with a rule, the next question is returned. If answers are provided, the next question is determined by the rules and the answers.
|
1317
|
-
- If the next question is the last question in the survey, an EndOfSurvey object is returned.
|
1318
|
-
|
1319
|
-
>>> s = Survey.example()
|
1320
|
-
>>> s.next_question("q0", {"q0": "yes"}).question_name
|
1321
|
-
'q2'
|
1322
|
-
>>> s.next_question("q0", {"q0": "no"}).question_name
|
1323
|
-
'q1'
|
1324
|
-
|
1325
|
-
"""
|
1326
|
-
if isinstance(current_question, str):
|
1327
|
-
current_question = self.get_question(current_question)
|
1328
|
-
|
1329
|
-
question_index = self.question_name_to_index[current_question.question_name]
|
1330
|
-
next_question_object = self.rule_collection.next_question(
|
1331
|
-
question_index, answers
|
1332
|
-
)
|
1333
|
-
|
1334
|
-
if next_question_object.num_rules_found == 0:
|
1335
|
-
raise SurveyHasNoRulesError
|
1336
|
-
|
1337
|
-
if next_question_object.next_q == EndOfSurvey:
|
1338
|
-
return EndOfSurvey
|
1339
|
-
else:
|
1340
|
-
if next_question_object.next_q >= len(self.questions):
|
1341
|
-
return EndOfSurvey
|
1342
|
-
else:
|
1343
|
-
return self.questions[next_question_object.next_q]
|
1344
|
-
|
1345
|
-
def gen_path_through_survey(self) -> Generator[QuestionBase, dict, None]:
|
1346
|
-
"""
|
1347
|
-
Generate a coroutine that can be used to conduct an Interview.
|
1348
|
-
|
1349
|
-
The coroutine is a generator that yields a question and receives answers.
|
1350
|
-
It starts with the first question in the survey.
|
1351
|
-
The coroutine ends when an EndOfSurvey object is returned.
|
1352
|
-
|
1353
|
-
For the example survey, this is the rule table:
|
1354
|
-
|
1355
|
-
>>> s = Survey.example()
|
1356
|
-
>>> s.show_rules()
|
1357
|
-
┏━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━┓
|
1358
|
-
┃ current_q ┃ expression ┃ next_q ┃ priority ┃ before_rule ┃
|
1359
|
-
┡━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━┩
|
1360
|
-
│ 0 │ True │ 1 │ -1 │ False │
|
1361
|
-
│ 0 │ q0 == 'yes' │ 2 │ 0 │ False │
|
1362
|
-
│ 1 │ True │ 2 │ -1 │ False │
|
1363
|
-
│ 2 │ True │ 3 │ -1 │ False │
|
1364
|
-
└───────────┴─────────────┴────────┴──────────┴─────────────┘
|
1365
|
-
|
1366
|
-
Note that q0 has a rule that if the answer is 'yes', the next question is q2. If the answer is 'no', the next question is q1.
|
1367
|
-
|
1368
|
-
Here is the path through the survey if the answer to q0 is 'yes':
|
1369
|
-
|
1370
|
-
>>> i = s.gen_path_through_survey()
|
1371
|
-
>>> next(i)
|
1372
|
-
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1373
|
-
>>> i.send({"q0": "yes"})
|
1374
|
-
Question('multiple_choice', question_name = \"""q2\""", question_text = \"""Why?\""", question_options = ['**lack*** of killer bees in cafeteria', 'other'])
|
1375
|
-
|
1376
|
-
And here is the path through the survey if the answer to q0 is 'no':
|
1377
|
-
|
1378
|
-
>>> i2 = s.gen_path_through_survey()
|
1379
|
-
>>> next(i2)
|
1380
|
-
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1381
|
-
>>> i2.send({"q0": "no"})
|
1382
|
-
Question('multiple_choice', question_name = \"""q1\""", question_text = \"""Why not?\""", question_options = ['killer bees in cafeteria', 'other'])
|
1383
|
-
|
1384
|
-
|
1385
|
-
"""
|
1386
|
-
self.answers = {}
|
1387
|
-
question = self._questions[0]
|
1388
|
-
# should the first question be skipped?
|
1389
|
-
if self.rule_collection.skip_question_before_running(0, self.answers):
|
1390
|
-
question = self.next_question(question, self.answers)
|
1391
|
-
|
1392
|
-
while not question == EndOfSurvey:
|
1393
|
-
# breakpoint()
|
1394
|
-
answer = yield question
|
1395
|
-
self.answers.update(answer)
|
1396
|
-
# print(f"Answers: {self.answers}")
|
1397
|
-
## TODO: This should also include survey and agent attributes
|
1398
|
-
question = self.next_question(question, self.answers)
|
1399
|
-
|
1400
|
-
# endregion
|
1401
|
-
|
1402
|
-
# regions: DAG construction
|
1403
|
-
def textify(self, index_dag: DAG) -> DAG:
|
1404
|
-
"""Convert the DAG of question indices to a DAG of question names.
|
1405
|
-
|
1406
|
-
:param index_dag: The DAG of question indices.
|
1407
|
-
|
1408
|
-
Example:
|
1409
|
-
|
1410
|
-
>>> s = Survey.example()
|
1411
|
-
>>> d = s.dag()
|
1412
|
-
>>> d
|
1413
|
-
{1: {0}, 2: {0}}
|
1414
|
-
>>> s.textify(d)
|
1415
|
-
{'q1': {'q0'}, 'q2': {'q0'}}
|
1416
|
-
"""
|
1417
|
-
|
1418
|
-
def get_name(index: int):
|
1419
|
-
"""Return the name of the question given the index."""
|
1420
|
-
if index >= len(self.questions):
|
1421
|
-
return EndOfSurvey
|
1422
|
-
try:
|
1423
|
-
return self.questions[index].question_name
|
1424
|
-
except IndexError:
|
1425
|
-
print(
|
1426
|
-
f"The index is {index} but the length of the questions is {len(self.questions)}"
|
1427
|
-
)
|
1428
|
-
raise SurveyError
|
1429
|
-
|
1430
|
-
try:
|
1431
|
-
text_dag = {}
|
1432
|
-
for child_index, parent_indices in index_dag.items():
|
1433
|
-
parent_names = {get_name(index) for index in parent_indices}
|
1434
|
-
child_name = get_name(child_index)
|
1435
|
-
text_dag[child_name] = parent_names
|
1436
|
-
return text_dag
|
1437
|
-
except IndexError:
|
1438
|
-
raise
|
1439
|
-
|
1440
|
-
@property
|
1441
|
-
def piping_dag(self) -> DAG:
|
1442
|
-
"""Figures out the DAG of piping dependencies.
|
1443
|
-
|
1444
|
-
>>> from edsl import QuestionFreeText
|
1445
|
-
>>> q0 = QuestionFreeText(question_text="Here is a question", question_name="q0")
|
1446
|
-
>>> q1 = QuestionFreeText(question_text="You previously answered {{ q0 }}---how do you feel now?", question_name="q1")
|
1447
|
-
>>> s = Survey([q0, q1])
|
1448
|
-
>>> s.piping_dag
|
1449
|
-
{1: {0}}
|
1450
|
-
"""
|
1451
|
-
d = {}
|
1452
|
-
for question_name, depenencies in self.parameters_by_question.items():
|
1453
|
-
if depenencies:
|
1454
|
-
question_index = self.question_name_to_index[question_name]
|
1455
|
-
for dependency in depenencies:
|
1456
|
-
if dependency not in self.question_name_to_index:
|
1457
|
-
pass
|
1458
|
-
else:
|
1459
|
-
dependency_index = self.question_name_to_index[dependency]
|
1460
|
-
if question_index not in d:
|
1461
|
-
d[question_index] = set()
|
1462
|
-
d[question_index].add(dependency_index)
|
1463
|
-
return d
|
1464
|
-
|
1465
|
-
def dag(self, textify: bool = False) -> DAG:
|
1466
|
-
"""Return the DAG of the survey, which reflects both skip-logic and memory.
|
1467
|
-
|
1468
|
-
:param textify: Whether to return the DAG with question names instead of indices.
|
1469
|
-
|
1470
|
-
>>> s = Survey.example()
|
1471
|
-
>>> d = s.dag()
|
1472
|
-
>>> d
|
1473
|
-
{1: {0}, 2: {0}}
|
1474
|
-
|
1475
|
-
"""
|
1476
|
-
memory_dag = self.memory_plan.dag
|
1477
|
-
rule_dag = self.rule_collection.dag
|
1478
|
-
piping_dag = self.piping_dag
|
1479
|
-
if textify:
|
1480
|
-
memory_dag = DAG(self.textify(memory_dag))
|
1481
|
-
rule_dag = DAG(self.textify(rule_dag))
|
1482
|
-
piping_dag = DAG(self.textify(piping_dag))
|
1483
|
-
return memory_dag + rule_dag + piping_dag
|
1484
|
-
|
1485
|
-
###################
|
1486
|
-
# DUNDER METHODS
|
1487
|
-
###################
|
1488
|
-
def __len__(self) -> int:
|
1489
|
-
"""Return the number of questions in the survey.
|
1490
|
-
|
1491
|
-
>>> s = Survey.example()
|
1492
|
-
>>> len(s)
|
1493
|
-
3
|
1494
|
-
"""
|
1495
|
-
return len(self._questions)
|
1496
|
-
|
1497
|
-
def __getitem__(self, index) -> QuestionBase:
|
1498
|
-
"""Return the question object given the question index.
|
1499
|
-
|
1500
|
-
:param index: The index of the question to get.
|
1501
|
-
|
1502
|
-
>>> s = Survey.example()
|
1503
|
-
>>> s[0]
|
1504
|
-
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1505
|
-
|
1506
|
-
"""
|
1507
|
-
if isinstance(index, int):
|
1508
|
-
return self._questions[index]
|
1509
|
-
elif isinstance(index, str):
|
1510
|
-
return getattr(self, index)
|
1511
|
-
|
1512
|
-
def _diff(self, other):
|
1513
|
-
"""Used for debugging. Print out the differences between two surveys."""
|
1514
|
-
from rich import print
|
1515
|
-
|
1516
|
-
for key, value in self.to_dict().items():
|
1517
|
-
if value != other.to_dict()[key]:
|
1518
|
-
print(f"Key: {key}")
|
1519
|
-
print("\n")
|
1520
|
-
print(f"Self: {value}")
|
1521
|
-
print("\n")
|
1522
|
-
print(f"Other: {other.to_dict()[key]}")
|
1523
|
-
print("\n\n")
|
1524
|
-
|
1525
|
-
def __eq__(self, other) -> bool:
|
1526
|
-
"""Return True if the two surveys have the same to_dict.
|
1527
|
-
|
1528
|
-
:param other: The other survey to compare to.
|
1529
|
-
|
1530
|
-
>>> s = Survey.example()
|
1531
|
-
>>> s == s
|
1532
|
-
True
|
1533
|
-
|
1534
|
-
>>> s == "poop"
|
1535
|
-
False
|
1536
|
-
|
1537
|
-
"""
|
1538
|
-
if not isinstance(other, Survey):
|
1539
|
-
return False
|
1540
|
-
return self.to_dict() == other.to_dict()
|
1541
|
-
|
1542
|
-
@classmethod
|
1543
|
-
def from_qsf(
|
1544
|
-
cls, qsf_file: Optional[str] = None, url: Optional[str] = None
|
1545
|
-
) -> Survey:
|
1546
|
-
"""Create a Survey object from a Qualtrics QSF file."""
|
1547
|
-
|
1548
|
-
if url and qsf_file:
|
1549
|
-
raise ValueError("Only one of url or qsf_file can be provided.")
|
1550
|
-
|
1551
|
-
if (not url) and (not qsf_file):
|
1552
|
-
raise ValueError("Either url or qsf_file must be provided.")
|
1553
|
-
|
1554
|
-
if url:
|
1555
|
-
response = requests.get(url)
|
1556
|
-
response.raise_for_status() # Ensure the request was successful
|
1557
|
-
|
1558
|
-
# Save the Excel file to a temporary file
|
1559
|
-
with tempfile.NamedTemporaryFile(suffix=".qsf", delete=False) as temp_file:
|
1560
|
-
temp_file.write(response.content)
|
1561
|
-
qsf_file = temp_file.name
|
1562
|
-
|
1563
|
-
from edsl.surveys.SurveyQualtricsImport import SurveyQualtricsImport
|
1564
|
-
|
1565
|
-
so = SurveyQualtricsImport(qsf_file)
|
1566
|
-
return so.create_survey()
|
1567
|
-
|
1568
|
-
# region: Display methods
|
1569
|
-
def print(self):
|
1570
|
-
"""Print the survey in a rich format.
|
1571
|
-
|
1572
|
-
>>> s = Survey.example()
|
1573
|
-
>>> s.print()
|
1574
|
-
{
|
1575
|
-
"questions": [
|
1576
|
-
...
|
1577
|
-
}
|
1578
|
-
"""
|
1579
|
-
from rich import print_json
|
1580
|
-
import json
|
1581
|
-
|
1582
|
-
print_json(json.dumps(self.to_dict()))
|
1583
|
-
|
1584
|
-
def __repr__(self) -> str:
|
1585
|
-
"""Return a string representation of the survey."""
|
1586
|
-
|
1587
|
-
# questions_string = ", ".join([repr(q) for q in self._questions])
|
1588
|
-
questions_string = ", ".join([repr(q) for q in self.raw_passed_questions or []])
|
1589
|
-
# question_names_string = ", ".join([repr(name) for name in self.question_names])
|
1590
|
-
return f"Survey(questions=[{questions_string}], memory_plan={self.memory_plan}, rule_collection={self.rule_collection}, question_groups={self.question_groups})"
|
1591
|
-
|
1592
|
-
def _summary(self) -> dict:
|
1593
|
-
return {
|
1594
|
-
"EDSL Class": "Survey",
|
1595
|
-
"Number of Questions": len(self),
|
1596
|
-
"Question Names": self.question_names,
|
1597
|
-
}
|
1598
|
-
|
1599
|
-
def _repr_html_(self) -> str:
|
1600
|
-
footer = f"<a href={self.__documentation__}>(docs)</a>"
|
1601
|
-
return str(self.summary(format="html")) + footer
|
1602
|
-
|
1603
|
-
def tree(self, node_list: Optional[List[str]] = None):
|
1604
|
-
return self.to_scenario_list().tree(node_list=node_list)
|
1605
|
-
|
1606
|
-
def table(self, *fields, tablefmt=None) -> Table:
|
1607
|
-
return self.to_scenario_list().to_dataset().table(*fields, tablefmt=tablefmt)
|
1608
|
-
|
1609
|
-
def rich_print(self) -> Table:
|
1610
|
-
"""Print the survey in a rich format.
|
1611
|
-
|
1612
|
-
>>> t = Survey.example().rich_print()
|
1613
|
-
>>> print(t) # doctest: +SKIP
|
1614
|
-
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
1615
|
-
┃ Questions ┃
|
1616
|
-
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
|
1617
|
-
│ ┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓ │
|
1618
|
-
│ ┃ Question Name ┃ Question Type ┃ Question Text ┃ Options ┃ │
|
1619
|
-
│ ┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩ │
|
1620
|
-
│ │ q0 │ multiple_choice │ Do you like school? │ yes, no │ │
|
1621
|
-
│ └───────────────┴─────────────────┴─────────────────────┴─────────┘ │
|
1622
|
-
│ ┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
|
1623
|
-
│ ┃ Question Name ┃ Question Type ┃ Question Text ┃ Options ┃ │
|
1624
|
-
│ ┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
|
1625
|
-
│ │ q1 │ multiple_choice │ Why not? │ killer bees in cafeteria, other │ │
|
1626
|
-
│ └───────────────┴─────────────────┴───────────────┴─────────────────────────────────┘ │
|
1627
|
-
│ ┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
|
1628
|
-
│ ┃ Question Name ┃ Question Type ┃ Question Text ┃ Options ┃ │
|
1629
|
-
│ ┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
|
1630
|
-
│ │ q2 │ multiple_choice │ Why? │ **lack*** of killer bees in cafeteria, other │ │
|
1631
|
-
│ └───────────────┴─────────────────┴───────────────┴──────────────────────────────────────────────┘ │
|
1632
|
-
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
|
1633
|
-
"""
|
1634
|
-
from rich.table import Table
|
1635
|
-
|
1636
|
-
table = Table(show_header=True, header_style="bold magenta")
|
1637
|
-
table.add_column("Questions", style="dim")
|
1638
|
-
|
1639
|
-
for question in self._questions:
|
1640
|
-
table.add_row(question.rich_print())
|
1641
|
-
|
1642
|
-
return table
|
1643
|
-
|
1644
|
-
# endregion
|
1645
|
-
|
1646
|
-
def codebook(self) -> dict[str, str]:
|
1647
|
-
"""Create a codebook for the survey, mapping question names to question text.
|
1648
|
-
|
1649
|
-
>>> s = Survey.example()
|
1650
|
-
>>> s.codebook()
|
1651
|
-
{'q0': 'Do you like school?', 'q1': 'Why not?', 'q2': 'Why?'}
|
1652
|
-
"""
|
1653
|
-
codebook = {}
|
1654
|
-
for question in self._questions:
|
1655
|
-
codebook[question.question_name] = question.question_text
|
1656
|
-
return codebook
|
1657
|
-
|
1658
|
-
# region: Export methods
|
1659
|
-
def to_csv(self, filename: str = None):
|
1660
|
-
"""Export the survey to a CSV file.
|
1661
|
-
|
1662
|
-
:param filename: The name of the file to save the CSV to.
|
1663
|
-
|
1664
|
-
>>> s = Survey.example()
|
1665
|
-
>>> s.to_csv() # doctest: +SKIP
|
1666
|
-
index question_name question_text question_options question_type
|
1667
|
-
0 0 q0 Do you like school? [yes, no] multiple_choice
|
1668
|
-
1 1 q1 Why not? [killer bees in cafeteria, other] multiple_choice
|
1669
|
-
2 2 q2 Why? [**lack*** of killer bees in cafeteria, other] multiple_choice
|
1670
|
-
"""
|
1671
|
-
raw_data = []
|
1672
|
-
for index, question in enumerate(self._questions):
|
1673
|
-
d = {"index": index}
|
1674
|
-
question_dict = question.to_dict()
|
1675
|
-
_ = question_dict.pop("edsl_version")
|
1676
|
-
_ = question_dict.pop("edsl_class_name")
|
1677
|
-
d.update(question_dict)
|
1678
|
-
raw_data.append(d)
|
1679
|
-
from pandas import DataFrame
|
1680
|
-
|
1681
|
-
df = DataFrame(raw_data)
|
1682
|
-
if filename:
|
1683
|
-
df.to_csv(filename, index=False)
|
1684
|
-
else:
|
1685
|
-
return df
|
1686
|
-
|
1687
|
-
# endregion
|
1688
|
-
|
1689
|
-
@classmethod
|
1690
|
-
def example(
|
1691
|
-
cls,
|
1692
|
-
params: bool = False,
|
1693
|
-
randomize: bool = False,
|
1694
|
-
include_instructions=False,
|
1695
|
-
custom_instructions: Optional[str] = None,
|
1696
|
-
) -> Survey:
|
1697
|
-
"""Return an example survey.
|
1698
|
-
|
1699
|
-
>>> s = Survey.example()
|
1700
|
-
>>> [q.question_text for q in s.questions]
|
1701
|
-
['Do you like school?', 'Why not?', 'Why?']
|
1702
|
-
"""
|
1703
|
-
from edsl.questions.QuestionMultipleChoice import QuestionMultipleChoice
|
1704
|
-
|
1705
|
-
addition = "" if not randomize else str(uuid4())
|
1706
|
-
q0 = QuestionMultipleChoice(
|
1707
|
-
question_text=f"Do you like school?{addition}",
|
1708
|
-
question_options=["yes", "no"],
|
1709
|
-
question_name="q0",
|
1710
|
-
)
|
1711
|
-
q1 = QuestionMultipleChoice(
|
1712
|
-
question_text="Why not?",
|
1713
|
-
question_options=["killer bees in cafeteria", "other"],
|
1714
|
-
question_name="q1",
|
1715
|
-
)
|
1716
|
-
q2 = QuestionMultipleChoice(
|
1717
|
-
question_text="Why?",
|
1718
|
-
question_options=["**lack*** of killer bees in cafeteria", "other"],
|
1719
|
-
question_name="q2",
|
1720
|
-
)
|
1721
|
-
if params:
|
1722
|
-
q3 = QuestionMultipleChoice(
|
1723
|
-
question_text="To the question '{{ q0.question_text}}', you said '{{ q0.answer }}'. Do you still feel this way?",
|
1724
|
-
question_options=["yes", "no"],
|
1725
|
-
question_name="q3",
|
1726
|
-
)
|
1727
|
-
s = cls(questions=[q0, q1, q2, q3])
|
1728
|
-
return s
|
1729
|
-
|
1730
|
-
if include_instructions:
|
1731
|
-
from edsl import Instruction
|
1732
|
-
|
1733
|
-
custom_instructions = (
|
1734
|
-
custom_instructions if custom_instructions else "Please pay attention!"
|
1735
|
-
)
|
1736
|
-
|
1737
|
-
i = Instruction(text=custom_instructions, name="attention")
|
1738
|
-
s = cls(questions=[i, q0, q1, q2])
|
1739
|
-
return s
|
1740
|
-
|
1741
|
-
s = cls(questions=[q0, q1, q2])
|
1742
|
-
s = s.add_rule(q0, "q0 == 'yes'", q2)
|
1743
|
-
return s
|
1744
|
-
|
1745
|
-
def get_job(self, model=None, agent=None, **kwargs):
|
1746
|
-
if model is None:
|
1747
|
-
from edsl import Model
|
1748
|
-
|
1749
|
-
model = Model()
|
1750
|
-
|
1751
|
-
from edsl.scenarios.Scenario import Scenario
|
1752
|
-
|
1753
|
-
s = Scenario(kwargs)
|
1754
|
-
|
1755
|
-
if not agent:
|
1756
|
-
from edsl import Agent
|
1757
|
-
|
1758
|
-
agent = Agent()
|
1759
|
-
|
1760
|
-
return self.by(s).by(agent).by(model)
|
1761
|
-
|
1762
|
-
|
1763
|
-
def main():
|
1764
|
-
"""Run the example survey."""
|
1765
|
-
|
1766
|
-
def example_survey():
|
1767
|
-
"""Return an example survey."""
|
1768
|
-
from edsl.questions.QuestionMultipleChoice import QuestionMultipleChoice
|
1769
|
-
from edsl.surveys.Survey import Survey
|
1770
|
-
|
1771
|
-
q0 = QuestionMultipleChoice(
|
1772
|
-
question_text="Do you like school?",
|
1773
|
-
question_options=["yes", "no"],
|
1774
|
-
question_name="q0",
|
1775
|
-
)
|
1776
|
-
q1 = QuestionMultipleChoice(
|
1777
|
-
question_text="Why not?",
|
1778
|
-
question_options=["killer bees in cafeteria", "other"],
|
1779
|
-
question_name="q1",
|
1780
|
-
)
|
1781
|
-
q2 = QuestionMultipleChoice(
|
1782
|
-
question_text="Why?",
|
1783
|
-
question_options=["**lack*** of killer bees in cafeteria", "other"],
|
1784
|
-
question_name="q2",
|
1785
|
-
)
|
1786
|
-
s = Survey(questions=[q0, q1, q2])
|
1787
|
-
s = s.add_rule(q0, "q0 == 'yes'", q2)
|
1788
|
-
return s
|
1789
|
-
|
1790
|
-
s = example_survey()
|
1791
|
-
survey_dict = s.to_dict()
|
1792
|
-
s2 = Survey.from_dict(survey_dict)
|
1793
|
-
results = s2.run()
|
1794
|
-
print(results)
|
1795
|
-
|
1796
|
-
|
1797
|
-
if __name__ == "__main__":
|
1798
|
-
import doctest
|
1799
|
-
|
1800
|
-
# doctest.testmod(optionflags=doctest.ELLIPSIS | doctest.SKIP)
|
1801
|
-
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
1
|
+
"""A Survey is collection of questions that can be administered to an Agent."""
|
2
|
+
|
3
|
+
from __future__ import annotations
|
4
|
+
import re
|
5
|
+
import random
|
6
|
+
|
7
|
+
from typing import (
|
8
|
+
Any,
|
9
|
+
Generator,
|
10
|
+
Optional,
|
11
|
+
Union,
|
12
|
+
List,
|
13
|
+
Literal,
|
14
|
+
Callable,
|
15
|
+
TYPE_CHECKING,
|
16
|
+
)
|
17
|
+
from uuid import uuid4
|
18
|
+
from edsl.Base import Base
|
19
|
+
from edsl.exceptions.surveys import SurveyCreationError, SurveyHasNoRulesError
|
20
|
+
from edsl.exceptions.surveys import SurveyError
|
21
|
+
from collections import UserDict
|
22
|
+
|
23
|
+
|
24
|
+
class PseudoIndices(UserDict):
|
25
|
+
@property
|
26
|
+
def max_pseudo_index(self) -> float:
|
27
|
+
"""Return the maximum pseudo index in the survey.
|
28
|
+
>>> Survey.example()._pseudo_indices.max_pseudo_index
|
29
|
+
2
|
30
|
+
"""
|
31
|
+
if len(self) == 0:
|
32
|
+
return -1
|
33
|
+
return max(self.values())
|
34
|
+
|
35
|
+
@property
|
36
|
+
def last_item_was_instruction(self) -> bool:
|
37
|
+
"""Return whether the last item added to the survey was an instruction.
|
38
|
+
|
39
|
+
This is used to determine the pseudo-index of the next item added to the survey.
|
40
|
+
|
41
|
+
Example:
|
42
|
+
|
43
|
+
>>> s = Survey.example()
|
44
|
+
>>> s._pseudo_indices.last_item_was_instruction
|
45
|
+
False
|
46
|
+
>>> from edsl.surveys.instructions.Instruction import Instruction
|
47
|
+
>>> s = s.add_instruction(Instruction(text="Pay attention to the following questions.", name="intro"))
|
48
|
+
>>> s._pseudo_indices.last_item_was_instruction
|
49
|
+
True
|
50
|
+
"""
|
51
|
+
return isinstance(self.max_pseudo_index, float)
|
52
|
+
|
53
|
+
|
54
|
+
if TYPE_CHECKING:
|
55
|
+
from edsl.questions.QuestionBase import QuestionBase
|
56
|
+
from edsl.agents.Agent import Agent
|
57
|
+
from edsl.surveys.DAG import DAG
|
58
|
+
from edsl.language_models.LanguageModel import LanguageModel
|
59
|
+
from edsl.scenarios.Scenario import Scenario
|
60
|
+
from edsl.data.Cache import Cache
|
61
|
+
|
62
|
+
# This is a hack to get around the fact that TypeAlias is not available in typing until Python 3.10
|
63
|
+
try:
|
64
|
+
from typing import TypeAlias
|
65
|
+
except ImportError:
|
66
|
+
from typing import _GenericAlias as TypeAlias
|
67
|
+
|
68
|
+
QuestionType: TypeAlias = Union[QuestionBase, Instruction, ChangeInstruction]
|
69
|
+
QuestionGroupType: TypeAlias = dict[str, tuple[int, int]]
|
70
|
+
|
71
|
+
|
72
|
+
from edsl.utilities.remove_edsl_version import remove_edsl_version
|
73
|
+
|
74
|
+
from edsl.surveys.instructions.InstructionCollection import InstructionCollection
|
75
|
+
from edsl.surveys.instructions.Instruction import Instruction
|
76
|
+
from edsl.surveys.instructions.ChangeInstruction import ChangeInstruction
|
77
|
+
|
78
|
+
from edsl.surveys.base import EndOfSurvey
|
79
|
+
from edsl.surveys.descriptors import QuestionsDescriptor
|
80
|
+
from edsl.surveys.MemoryPlan import MemoryPlan
|
81
|
+
from edsl.surveys.RuleCollection import RuleCollection
|
82
|
+
from edsl.surveys.SurveyExportMixin import SurveyExportMixin
|
83
|
+
from edsl.surveys.SurveyFlowVisualization import SurveyFlowVisualization
|
84
|
+
from edsl.surveys.InstructionHandler import InstructionHandler
|
85
|
+
from edsl.surveys.EditSurvey import EditSurvey
|
86
|
+
from edsl.surveys.Simulator import Simulator
|
87
|
+
from edsl.surveys.MemoryManagement import MemoryManagement
|
88
|
+
from edsl.surveys.RuleManager import RuleManager
|
89
|
+
|
90
|
+
|
91
|
+
class Survey(SurveyExportMixin, Base):
|
92
|
+
"""A collection of questions that supports skip logic."""
|
93
|
+
|
94
|
+
__documentation__ = """https://docs.expectedparrot.com/en/latest/surveys.html"""
|
95
|
+
|
96
|
+
questions = QuestionsDescriptor()
|
97
|
+
"""
|
98
|
+
A collection of questions that supports skip logic.
|
99
|
+
|
100
|
+
Initalization:
|
101
|
+
- `questions`: the questions in the survey (optional)
|
102
|
+
- `question_names`: the names of the questions (optional)
|
103
|
+
- `name`: the name of the survey (optional)
|
104
|
+
|
105
|
+
Methods:
|
106
|
+
-
|
107
|
+
|
108
|
+
Notes:
|
109
|
+
- The presumed order of the survey is the order in which questions are added.
|
110
|
+
"""
|
111
|
+
|
112
|
+
def __init__(
|
113
|
+
self,
|
114
|
+
questions: Optional[List["QuestionType"]] = None,
|
115
|
+
memory_plan: Optional["MemoryPlan"] = None,
|
116
|
+
rule_collection: Optional["RuleCollection"] = None,
|
117
|
+
question_groups: Optional["QuestionGroupType"] = None,
|
118
|
+
name: Optional[str] = None,
|
119
|
+
questions_to_randomize: Optional[List[str]] = None,
|
120
|
+
):
|
121
|
+
"""Create a new survey.
|
122
|
+
|
123
|
+
:param questions: The questions in the survey.
|
124
|
+
:param memory_plan: The memory plan for the survey.
|
125
|
+
:param rule_collection: The rule collection for the survey.
|
126
|
+
:param question_groups: The groups of questions in the survey.
|
127
|
+
:param name: The name of the survey - DEPRECATED.
|
128
|
+
|
129
|
+
|
130
|
+
>>> from edsl import QuestionFreeText
|
131
|
+
>>> q1 = QuestionFreeText(question_text = "What is your name?", question_name = "name")
|
132
|
+
>>> q2 = QuestionFreeText(question_text = "What is your favorite color?", question_name = "color")
|
133
|
+
>>> q3 = QuestionFreeText(question_text = "Is a hot dog a sandwich", question_name = "food")
|
134
|
+
>>> s = Survey([q1, q2, q3], question_groups = {"demographics": (0, 1), "substantive":(3)})
|
135
|
+
|
136
|
+
|
137
|
+
"""
|
138
|
+
|
139
|
+
self.raw_passed_questions = questions
|
140
|
+
|
141
|
+
true_questions = self._process_raw_questions(self.raw_passed_questions)
|
142
|
+
|
143
|
+
self.rule_collection = RuleCollection(
|
144
|
+
num_questions=len(true_questions) if true_questions else None
|
145
|
+
)
|
146
|
+
# the RuleCollection needs to be present while we add the questions; we might override this later
|
147
|
+
# if a rule_collection is provided. This allows us to serialize the survey with the rule_collection.
|
148
|
+
|
149
|
+
# this is where the Questions constructor is called.
|
150
|
+
self.questions = true_questions
|
151
|
+
# self.instruction_names_to_instructions = instruction_names_to_instructions
|
152
|
+
|
153
|
+
self.memory_plan = memory_plan or MemoryPlan(self)
|
154
|
+
if question_groups is not None:
|
155
|
+
self.question_groups = question_groups
|
156
|
+
else:
|
157
|
+
self.question_groups = {}
|
158
|
+
|
159
|
+
# if a rule collection is provided, use it instead of the constructed one
|
160
|
+
if rule_collection is not None:
|
161
|
+
self.rule_collection = rule_collection
|
162
|
+
|
163
|
+
if name is not None:
|
164
|
+
import warnings
|
165
|
+
|
166
|
+
warnings.warn("name parameter to a survey is deprecated.")
|
167
|
+
|
168
|
+
if questions_to_randomize is not None:
|
169
|
+
self.questions_to_randomize = questions_to_randomize
|
170
|
+
else:
|
171
|
+
self.questions_to_randomize = []
|
172
|
+
|
173
|
+
self._seed = None
|
174
|
+
|
175
|
+
def draw(self) -> "Survey":
|
176
|
+
"""Return a new survey with a randomly selected permutation of the options."""
|
177
|
+
if self._seed is None: # only set once
|
178
|
+
self._seed = hash(self)
|
179
|
+
random.seed(self._seed)
|
180
|
+
|
181
|
+
if len(self.questions_to_randomize) == 0:
|
182
|
+
return self
|
183
|
+
|
184
|
+
new_questions = []
|
185
|
+
for question in self.questions:
|
186
|
+
if question.question_name in self.questions_to_randomize:
|
187
|
+
new_questions.append(question.draw())
|
188
|
+
else:
|
189
|
+
new_questions.append(question.duplicate())
|
190
|
+
|
191
|
+
d = self.to_dict()
|
192
|
+
d["questions"] = [q.to_dict() for q in new_questions]
|
193
|
+
return Survey.from_dict(d)
|
194
|
+
|
195
|
+
def _process_raw_questions(self, questions: Optional[List["QuestionType"]]) -> list:
|
196
|
+
"""Process the raw questions passed to the survey."""
|
197
|
+
handler = InstructionHandler(self)
|
198
|
+
components = handler.separate_questions_and_instructions(questions or [])
|
199
|
+
self._instruction_names_to_instructions = (
|
200
|
+
components.instruction_names_to_instructions
|
201
|
+
)
|
202
|
+
self._pseudo_indices = PseudoIndices(components.pseudo_indices)
|
203
|
+
return components.true_questions
|
204
|
+
|
205
|
+
# region: Survey instruction handling
|
206
|
+
@property
|
207
|
+
def _relevant_instructions_dict(self) -> InstructionCollection:
|
208
|
+
"""Return a dictionary with keys as question names and values as instructions that are relevant to the question.
|
209
|
+
|
210
|
+
>>> s = Survey.example(include_instructions=True)
|
211
|
+
>>> s._relevant_instructions_dict
|
212
|
+
{'q0': [Instruction(name="attention", text="Please pay attention!")], 'q1': [Instruction(name="attention", text="Please pay attention!")], 'q2': [Instruction(name="attention", text="Please pay attention!")]}
|
213
|
+
|
214
|
+
"""
|
215
|
+
return InstructionCollection(
|
216
|
+
self._instruction_names_to_instructions, self.questions
|
217
|
+
)
|
218
|
+
|
219
|
+
def _relevant_instructions(self, question: QuestionBase) -> dict:
|
220
|
+
"""This should be a dictionry with keys as question names and values as instructions that are relevant to the question.
|
221
|
+
|
222
|
+
:param question: The question to get the relevant instructions for.
|
223
|
+
|
224
|
+
# Did the instruction come before the question and was it not modified by a change instruction?
|
225
|
+
|
226
|
+
"""
|
227
|
+
return InstructionCollection(
|
228
|
+
self._instruction_names_to_instructions, self.questions
|
229
|
+
)[question]
|
230
|
+
|
231
|
+
def show_flow(self, filename: Optional[str] = None) -> None:
|
232
|
+
"""Show the flow of the survey."""
|
233
|
+
SurveyFlowVisualization(self).show_flow(filename=filename)
|
234
|
+
|
235
|
+
def add_instruction(
|
236
|
+
self, instruction: Union["Instruction", "ChangeInstruction"]
|
237
|
+
) -> Survey:
|
238
|
+
"""
|
239
|
+
Add an instruction to the survey.
|
240
|
+
|
241
|
+
:param instruction: The instruction to add to the survey.
|
242
|
+
|
243
|
+
>>> from edsl import Instruction
|
244
|
+
>>> i = Instruction(text="Pay attention to the following questions.", name="intro")
|
245
|
+
>>> s = Survey().add_instruction(i)
|
246
|
+
>>> s._instruction_names_to_instructions
|
247
|
+
{'intro': Instruction(name="intro", text="Pay attention to the following questions.")}
|
248
|
+
>>> s._pseudo_indices
|
249
|
+
{'intro': -0.5}
|
250
|
+
"""
|
251
|
+
return EditSurvey(self).add_instruction(instruction)
|
252
|
+
|
253
|
+
# endregion
|
254
|
+
@classmethod
|
255
|
+
def random_survey(cls):
|
256
|
+
return Simulator.random_survey()
|
257
|
+
|
258
|
+
def simulate(self) -> dict:
|
259
|
+
"""Simulate the survey and return the answers."""
|
260
|
+
return Simulator(self).simulate()
|
261
|
+
|
262
|
+
# endregion
|
263
|
+
|
264
|
+
# region: Access methods
|
265
|
+
def _get_question_index(
|
266
|
+
self, q: Union[QuestionBase, str, EndOfSurvey.__class__]
|
267
|
+
) -> Union[int, EndOfSurvey.__class__]:
|
268
|
+
"""Return the index of the question or EndOfSurvey object.
|
269
|
+
|
270
|
+
:param q: The question or question name to get the index of.
|
271
|
+
|
272
|
+
It can handle it if the user passes in the question name, the question object, or the EndOfSurvey object.
|
273
|
+
|
274
|
+
>>> s = Survey.example()
|
275
|
+
>>> s._get_question_index("q0")
|
276
|
+
0
|
277
|
+
|
278
|
+
This doesnt' work with questions that don't exist:
|
279
|
+
|
280
|
+
>>> s._get_question_index("poop")
|
281
|
+
Traceback (most recent call last):
|
282
|
+
...
|
283
|
+
edsl.exceptions.surveys.SurveyError: Question name poop not found in survey. The current question names are {'q0': 0, 'q1': 1, 'q2': 2}.
|
284
|
+
...
|
285
|
+
"""
|
286
|
+
if q == EndOfSurvey:
|
287
|
+
return EndOfSurvey
|
288
|
+
else:
|
289
|
+
question_name = q if isinstance(q, str) else q.question_name
|
290
|
+
if question_name not in self.question_name_to_index:
|
291
|
+
raise SurveyError(
|
292
|
+
f"""Question name {question_name} not found in survey. The current question names are {self.question_name_to_index}."""
|
293
|
+
)
|
294
|
+
return self.question_name_to_index[question_name]
|
295
|
+
|
296
|
+
def _get_question_by_name(self, question_name: str) -> QuestionBase:
|
297
|
+
"""
|
298
|
+
Return the question object given the question name.
|
299
|
+
|
300
|
+
:param question_name: The name of the question to get.
|
301
|
+
|
302
|
+
>>> s = Survey.example()
|
303
|
+
>>> s._get_question_by_name("q0")
|
304
|
+
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
305
|
+
"""
|
306
|
+
if question_name not in self.question_name_to_index:
|
307
|
+
raise SurveyError(f"Question name {question_name} not found in survey.")
|
308
|
+
return self._questions[self.question_name_to_index[question_name]]
|
309
|
+
|
310
|
+
def question_names_to_questions(self) -> dict:
|
311
|
+
"""Return a dictionary mapping question names to question attributes."""
|
312
|
+
return {q.question_name: q for q in self.questions}
|
313
|
+
|
314
|
+
@property
|
315
|
+
def question_names(self) -> list[str]:
|
316
|
+
"""Return a list of question names in the survey.
|
317
|
+
|
318
|
+
Example:
|
319
|
+
|
320
|
+
>>> s = Survey.example()
|
321
|
+
>>> s.question_names
|
322
|
+
['q0', 'q1', 'q2']
|
323
|
+
"""
|
324
|
+
return [q.question_name for q in self.questions]
|
325
|
+
|
326
|
+
@property
|
327
|
+
def question_name_to_index(self) -> dict[str, int]:
|
328
|
+
"""Return a dictionary mapping question names to question indices.
|
329
|
+
|
330
|
+
Example:
|
331
|
+
|
332
|
+
>>> s = Survey.example()
|
333
|
+
>>> s.question_name_to_index
|
334
|
+
{'q0': 0, 'q1': 1, 'q2': 2}
|
335
|
+
"""
|
336
|
+
return {q.question_name: i for i, q in enumerate(self.questions)}
|
337
|
+
|
338
|
+
# endregion
|
339
|
+
|
340
|
+
# region: serialization methods
|
341
|
+
def to_dict(self, add_edsl_version=True) -> dict[str, Any]:
|
342
|
+
"""Serialize the Survey object to a dictionary.
|
343
|
+
|
344
|
+
>>> s = Survey.example()
|
345
|
+
>>> s.to_dict(add_edsl_version = False).keys()
|
346
|
+
dict_keys(['questions', 'memory_plan', 'rule_collection', 'question_groups'])
|
347
|
+
"""
|
348
|
+
from edsl import __version__
|
349
|
+
|
350
|
+
d = {
|
351
|
+
"questions": [
|
352
|
+
q.to_dict(add_edsl_version=add_edsl_version)
|
353
|
+
for q in self._recombined_questions_and_instructions()
|
354
|
+
],
|
355
|
+
"memory_plan": self.memory_plan.to_dict(add_edsl_version=add_edsl_version),
|
356
|
+
"rule_collection": self.rule_collection.to_dict(
|
357
|
+
add_edsl_version=add_edsl_version
|
358
|
+
),
|
359
|
+
"question_groups": self.question_groups,
|
360
|
+
}
|
361
|
+
if self.questions_to_randomize != []:
|
362
|
+
d["questions_to_randomize"] = self.questions_to_randomize
|
363
|
+
|
364
|
+
if add_edsl_version:
|
365
|
+
d["edsl_version"] = __version__
|
366
|
+
d["edsl_class_name"] = "Survey"
|
367
|
+
return d
|
368
|
+
|
369
|
+
@classmethod
|
370
|
+
@remove_edsl_version
|
371
|
+
def from_dict(cls, data: dict) -> Survey:
|
372
|
+
"""Deserialize the dictionary back to a Survey object.
|
373
|
+
|
374
|
+
:param data: The dictionary to deserialize.
|
375
|
+
|
376
|
+
>>> d = Survey.example().to_dict()
|
377
|
+
>>> s = Survey.from_dict(d)
|
378
|
+
>>> s == Survey.example()
|
379
|
+
True
|
380
|
+
|
381
|
+
>>> s = Survey.example(include_instructions = True)
|
382
|
+
>>> d = s.to_dict()
|
383
|
+
>>> news = Survey.from_dict(d)
|
384
|
+
>>> news == s
|
385
|
+
True
|
386
|
+
|
387
|
+
"""
|
388
|
+
|
389
|
+
def get_class(pass_dict):
|
390
|
+
from edsl.questions.QuestionBase import QuestionBase
|
391
|
+
|
392
|
+
if (class_name := pass_dict.get("edsl_class_name")) == "QuestionBase":
|
393
|
+
return QuestionBase
|
394
|
+
elif class_name == "Instruction":
|
395
|
+
from edsl.surveys.instructions.Instruction import Instruction
|
396
|
+
|
397
|
+
return Instruction
|
398
|
+
elif class_name == "ChangeInstruction":
|
399
|
+
from edsl.surveys.instructions.ChangeInstruction import (
|
400
|
+
ChangeInstruction,
|
401
|
+
)
|
402
|
+
|
403
|
+
return ChangeInstruction
|
404
|
+
else:
|
405
|
+
return QuestionBase
|
406
|
+
|
407
|
+
questions = [
|
408
|
+
get_class(q_dict).from_dict(q_dict) for q_dict in data["questions"]
|
409
|
+
]
|
410
|
+
memory_plan = MemoryPlan.from_dict(data["memory_plan"])
|
411
|
+
if "questions_to_randomize" in data:
|
412
|
+
questions_to_randomize = data["questions_to_randomize"]
|
413
|
+
else:
|
414
|
+
questions_to_randomize = None
|
415
|
+
survey = cls(
|
416
|
+
questions=questions,
|
417
|
+
memory_plan=memory_plan,
|
418
|
+
rule_collection=RuleCollection.from_dict(data["rule_collection"]),
|
419
|
+
question_groups=data["question_groups"],
|
420
|
+
questions_to_randomize=questions_to_randomize,
|
421
|
+
)
|
422
|
+
return survey
|
423
|
+
|
424
|
+
# endregion
|
425
|
+
|
426
|
+
# region: Survey template parameters
|
427
|
+
@property
|
428
|
+
def scenario_attributes(self) -> list[str]:
|
429
|
+
"""Return a list of attributes that admissible Scenarios should have.
|
430
|
+
|
431
|
+
Here we have a survey with a question that uses a jinja2 style {{ }} template:
|
432
|
+
|
433
|
+
>>> from edsl import QuestionFreeText
|
434
|
+
>>> s = Survey().add_question(QuestionFreeText(question_text="{{ greeting }}. What is your name?", question_name="name"))
|
435
|
+
>>> s.scenario_attributes
|
436
|
+
['greeting']
|
437
|
+
|
438
|
+
>>> s = Survey().add_question(QuestionFreeText(question_text="{{ greeting }}. What is your {{ attribute }}?", question_name="name"))
|
439
|
+
>>> s.scenario_attributes
|
440
|
+
['greeting', 'attribute']
|
441
|
+
|
442
|
+
|
443
|
+
"""
|
444
|
+
temp = []
|
445
|
+
for question in self.questions:
|
446
|
+
question_text = question.question_text
|
447
|
+
# extract the contents of all {{ }} in the question text using regex
|
448
|
+
matches = re.findall(r"\{\{(.+?)\}\}", question_text)
|
449
|
+
# remove whitespace
|
450
|
+
matches = [match.strip() for match in matches]
|
451
|
+
# add them to the temp list
|
452
|
+
temp.extend(matches)
|
453
|
+
return temp
|
454
|
+
|
455
|
+
@property
|
456
|
+
def parameters(self):
|
457
|
+
"""Return a set of parameters in the survey.
|
458
|
+
|
459
|
+
>>> s = Survey.example()
|
460
|
+
>>> s.parameters
|
461
|
+
set()
|
462
|
+
"""
|
463
|
+
return set.union(*[q.parameters for q in self.questions])
|
464
|
+
|
465
|
+
@property
|
466
|
+
def parameters_by_question(self):
|
467
|
+
"""Return a dictionary of parameters by question in the survey.
|
468
|
+
>>> from edsl import QuestionFreeText
|
469
|
+
>>> q = QuestionFreeText(question_name = "example", question_text = "What is the capital of {{ country}}?")
|
470
|
+
>>> s = Survey([q])
|
471
|
+
>>> s.parameters_by_question
|
472
|
+
{'example': {'country'}}
|
473
|
+
"""
|
474
|
+
return {q.question_name: q.parameters for q in self.questions}
|
475
|
+
|
476
|
+
# endregion
|
477
|
+
|
478
|
+
# region: Survey construction
|
479
|
+
|
480
|
+
# region: Adding questions and combining surveys
|
481
|
+
def __add__(self, other: Survey) -> Survey:
|
482
|
+
"""Combine two surveys.
|
483
|
+
|
484
|
+
:param other: The other survey to combine with this one.
|
485
|
+
>>> s1 = Survey.example()
|
486
|
+
>>> from edsl import QuestionFreeText
|
487
|
+
>>> s2 = Survey([QuestionFreeText(question_text="What is your name?", question_name="yo")])
|
488
|
+
>>> s3 = s1 + s2
|
489
|
+
Traceback (most recent call last):
|
490
|
+
...
|
491
|
+
edsl.exceptions.surveys.SurveyCreationError: ...
|
492
|
+
...
|
493
|
+
>>> s3 = s1.clear_non_default_rules() + s2
|
494
|
+
>>> len(s3.questions)
|
495
|
+
4
|
496
|
+
|
497
|
+
"""
|
498
|
+
if (
|
499
|
+
len(self.rule_collection.non_default_rules) > 0
|
500
|
+
or len(other.rule_collection.non_default_rules) > 0
|
501
|
+
):
|
502
|
+
raise SurveyCreationError(
|
503
|
+
"Cannot combine two surveys with non-default rules. Please use the 'clear_non_default_rules' method to remove non-default rules from the survey.",
|
504
|
+
)
|
505
|
+
|
506
|
+
return Survey(questions=self.questions + other.questions)
|
507
|
+
|
508
|
+
def move_question(self, identifier: Union[str, int], new_index: int) -> Survey:
|
509
|
+
"""
|
510
|
+
>>> from edsl import QuestionMultipleChoice, Survey
|
511
|
+
>>> s = Survey.example()
|
512
|
+
>>> s.question_names
|
513
|
+
['q0', 'q1', 'q2']
|
514
|
+
>>> s.move_question("q0", 2).question_names
|
515
|
+
['q1', 'q2', 'q0']
|
516
|
+
"""
|
517
|
+
return EditSurvey(self).move_question(identifier, new_index)
|
518
|
+
|
519
|
+
def delete_question(self, identifier: Union[str, int]) -> Survey:
|
520
|
+
"""
|
521
|
+
Delete a question from the survey.
|
522
|
+
|
523
|
+
:param identifier: The name or index of the question to delete.
|
524
|
+
:return: The updated Survey object.
|
525
|
+
|
526
|
+
>>> from edsl import QuestionMultipleChoice, Survey
|
527
|
+
>>> q1 = QuestionMultipleChoice(question_text="Q1", question_options=["A", "B"], question_name="q1")
|
528
|
+
>>> q2 = QuestionMultipleChoice(question_text="Q2", question_options=["C", "D"], question_name="q2")
|
529
|
+
>>> s = Survey().add_question(q1).add_question(q2)
|
530
|
+
>>> _ = s.delete_question("q1")
|
531
|
+
>>> len(s.questions)
|
532
|
+
1
|
533
|
+
>>> _ = s.delete_question(0)
|
534
|
+
>>> len(s.questions)
|
535
|
+
0
|
536
|
+
"""
|
537
|
+
return EditSurvey(self).delete_question(identifier)
|
538
|
+
|
539
|
+
def add_question(
|
540
|
+
self, question: QuestionBase, index: Optional[int] = None
|
541
|
+
) -> Survey:
|
542
|
+
"""
|
543
|
+
Add a question to survey.
|
544
|
+
|
545
|
+
:param question: The question to add to the survey.
|
546
|
+
:param question_name: The name of the question. If not provided, the question name is used.
|
547
|
+
|
548
|
+
The question is appended at the end of the self.questions list
|
549
|
+
A default rule is created that the next index is the next question.
|
550
|
+
|
551
|
+
>>> from edsl import QuestionMultipleChoice
|
552
|
+
>>> q = QuestionMultipleChoice(question_text = "Do you like school?", question_options=["yes", "no"], question_name="q0")
|
553
|
+
>>> s = Survey().add_question(q)
|
554
|
+
|
555
|
+
>>> s = Survey().add_question(q).add_question(q)
|
556
|
+
Traceback (most recent call last):
|
557
|
+
...
|
558
|
+
edsl.exceptions.surveys.SurveyCreationError: Question name 'q0' already exists in survey. Existing names are ['q0'].
|
559
|
+
...
|
560
|
+
"""
|
561
|
+
return EditSurvey(self).add_question(question, index)
|
562
|
+
|
563
|
+
def _recombined_questions_and_instructions(
|
564
|
+
self,
|
565
|
+
) -> list[Union[QuestionBase, "Instruction"]]:
|
566
|
+
"""Return a list of questions and instructions sorted by pseudo index."""
|
567
|
+
questions_and_instructions = self._questions + list(
|
568
|
+
self._instruction_names_to_instructions.values()
|
569
|
+
)
|
570
|
+
return sorted(
|
571
|
+
questions_and_instructions, key=lambda x: self._pseudo_indices[x.name]
|
572
|
+
)
|
573
|
+
|
574
|
+
# endregion
|
575
|
+
|
576
|
+
# region: Memory plan methods
|
577
|
+
def set_full_memory_mode(self) -> Survey:
|
578
|
+
"""Add instructions to a survey that the agent should remember all of the answers to the questions in the survey.
|
579
|
+
|
580
|
+
>>> s = Survey.example().set_full_memory_mode()
|
581
|
+
|
582
|
+
"""
|
583
|
+
MemoryManagement(self)._set_memory_plan(lambda i: self.question_names[:i])
|
584
|
+
return self
|
585
|
+
|
586
|
+
def set_lagged_memory(self, lags: int) -> Survey:
|
587
|
+
"""Add instructions to a survey that the agent should remember the answers to the questions in the survey.
|
588
|
+
|
589
|
+
The agent should remember the answers to the questions in the survey from the previous lags.
|
590
|
+
"""
|
591
|
+
MemoryManagement(self)._set_memory_plan(
|
592
|
+
lambda i: self.question_names[max(0, i - lags) : i]
|
593
|
+
)
|
594
|
+
return self
|
595
|
+
|
596
|
+
def _set_memory_plan(self, prior_questions_func: Callable) -> None:
|
597
|
+
"""Set memory plan based on a provided function determining prior questions.
|
598
|
+
|
599
|
+
:param prior_questions_func: A function that takes the index of the current question and returns a list of prior questions to remember.
|
600
|
+
|
601
|
+
>>> s = Survey.example()
|
602
|
+
>>> s._set_memory_plan(lambda i: s.question_names[:i])
|
603
|
+
|
604
|
+
"""
|
605
|
+
MemoryManagement(self)._set_memory_plan(prior_questions_func)
|
606
|
+
|
607
|
+
def add_targeted_memory(
|
608
|
+
self,
|
609
|
+
focal_question: Union[QuestionBase, str],
|
610
|
+
prior_question: Union[QuestionBase, str],
|
611
|
+
) -> Survey:
|
612
|
+
"""Add instructions to a survey than when answering focal_question.
|
613
|
+
|
614
|
+
:param focal_question: The question that the agent is answering.
|
615
|
+
:param prior_question: The question that the agent should remember when answering the focal question.
|
616
|
+
|
617
|
+
Here we add instructions to a survey than when answering q2 they should remember q1:
|
618
|
+
|
619
|
+
>>> s = Survey.example().add_targeted_memory("q2", "q0")
|
620
|
+
>>> s.memory_plan
|
621
|
+
{'q2': Memory(prior_questions=['q0'])}
|
622
|
+
|
623
|
+
The agent should also remember the answers to prior_questions listed in prior_questions.
|
624
|
+
"""
|
625
|
+
return MemoryManagement(self).add_targeted_memory(
|
626
|
+
focal_question, prior_question
|
627
|
+
)
|
628
|
+
|
629
|
+
def add_memory_collection(
|
630
|
+
self,
|
631
|
+
focal_question: Union[QuestionBase, str],
|
632
|
+
prior_questions: List[Union[QuestionBase, str]],
|
633
|
+
) -> Survey:
|
634
|
+
"""Add prior questions and responses so the agent has them when answering.
|
635
|
+
|
636
|
+
This adds instructions to a survey than when answering focal_question, the agent should also remember the answers to prior_questions listed in prior_questions.
|
637
|
+
|
638
|
+
:param focal_question: The question that the agent is answering.
|
639
|
+
:param prior_questions: The questions that the agent should remember when answering the focal question.
|
640
|
+
|
641
|
+
Here we have it so that when answering q2, the agent should remember answers to q0 and q1:
|
642
|
+
|
643
|
+
>>> s = Survey.example().add_memory_collection("q2", ["q0", "q1"])
|
644
|
+
>>> s.memory_plan
|
645
|
+
{'q2': Memory(prior_questions=['q0', 'q1'])}
|
646
|
+
"""
|
647
|
+
return MemoryManagement(self).add_memory_collection(
|
648
|
+
focal_question, prior_questions
|
649
|
+
)
|
650
|
+
|
651
|
+
# region: Question groups
|
652
|
+
def add_question_group(
|
653
|
+
self,
|
654
|
+
start_question: Union[QuestionBase, str],
|
655
|
+
end_question: Union[QuestionBase, str],
|
656
|
+
group_name: str,
|
657
|
+
) -> Survey:
|
658
|
+
"""Add a group of questions to the survey.
|
659
|
+
|
660
|
+
:param start_question: The first question in the group.
|
661
|
+
:param end_question: The last question in the group.
|
662
|
+
:param group_name: The name of the group.
|
663
|
+
|
664
|
+
Example:
|
665
|
+
|
666
|
+
>>> s = Survey.example().add_question_group("q0", "q1", "group1")
|
667
|
+
>>> s.question_groups
|
668
|
+
{'group1': (0, 1)}
|
669
|
+
|
670
|
+
The name of the group must be a valid identifier:
|
671
|
+
|
672
|
+
>>> s = Survey.example().add_question_group("q0", "q2", "1group1")
|
673
|
+
Traceback (most recent call last):
|
674
|
+
...
|
675
|
+
edsl.exceptions.surveys.SurveyCreationError: Group name 1group1 is not a valid identifier.
|
676
|
+
...
|
677
|
+
>>> s = Survey.example().add_question_group("q0", "q1", "q0")
|
678
|
+
Traceback (most recent call last):
|
679
|
+
...
|
680
|
+
edsl.exceptions.surveys.SurveyCreationError: ...
|
681
|
+
...
|
682
|
+
>>> s = Survey.example().add_question_group("q1", "q0", "group1")
|
683
|
+
Traceback (most recent call last):
|
684
|
+
...
|
685
|
+
edsl.exceptions.surveys.SurveyCreationError: ...
|
686
|
+
...
|
687
|
+
"""
|
688
|
+
|
689
|
+
if not group_name.isidentifier():
|
690
|
+
raise SurveyCreationError(
|
691
|
+
f"Group name {group_name} is not a valid identifier."
|
692
|
+
)
|
693
|
+
|
694
|
+
if group_name in self.question_groups:
|
695
|
+
raise SurveyCreationError(
|
696
|
+
f"Group name {group_name} already exists in the survey."
|
697
|
+
)
|
698
|
+
|
699
|
+
if group_name in self.question_name_to_index:
|
700
|
+
raise SurveyCreationError(
|
701
|
+
f"Group name {group_name} already exists as a question name in the survey."
|
702
|
+
)
|
703
|
+
|
704
|
+
start_index = self._get_question_index(start_question)
|
705
|
+
end_index = self._get_question_index(end_question)
|
706
|
+
|
707
|
+
if start_index > end_index:
|
708
|
+
raise SurveyCreationError(
|
709
|
+
f"Start index {start_index} is greater than end index {end_index}."
|
710
|
+
)
|
711
|
+
|
712
|
+
for existing_group_name, (
|
713
|
+
existing_start_index,
|
714
|
+
existing_end_index,
|
715
|
+
) in self.question_groups.items():
|
716
|
+
if start_index < existing_start_index and end_index > existing_end_index:
|
717
|
+
raise SurveyCreationError(
|
718
|
+
f"Group {group_name} contains the questions in the new group."
|
719
|
+
)
|
720
|
+
if start_index > existing_start_index and end_index < existing_end_index:
|
721
|
+
raise SurveyCreationError(
|
722
|
+
f"Group {group_name} is contained in the new group."
|
723
|
+
)
|
724
|
+
if start_index < existing_start_index and end_index > existing_start_index:
|
725
|
+
raise SurveyCreationError(
|
726
|
+
f"Group {group_name} overlaps with the new group."
|
727
|
+
)
|
728
|
+
if start_index < existing_end_index and end_index > existing_end_index:
|
729
|
+
raise SurveyCreationError(
|
730
|
+
f"Group {group_name} overlaps with the new group."
|
731
|
+
)
|
732
|
+
|
733
|
+
self.question_groups[group_name] = (start_index, end_index)
|
734
|
+
return self
|
735
|
+
|
736
|
+
# endregion
|
737
|
+
|
738
|
+
# region: Survey rules
|
739
|
+
def show_rules(self) -> None:
|
740
|
+
"""Print out the rules in the survey.
|
741
|
+
|
742
|
+
>>> s = Survey.example()
|
743
|
+
>>> s.show_rules()
|
744
|
+
Dataset([{'current_q': [0, 0, 1, 2]}, {'expression': ['True', "q0 == 'yes'", 'True', 'True']}, {'next_q': [1, 2, 2, 3]}, {'priority': [-1, 0, -1, -1]}, {'before_rule': [False, False, False, False]}])
|
745
|
+
"""
|
746
|
+
return self.rule_collection.show_rules()
|
747
|
+
|
748
|
+
def add_stop_rule(
|
749
|
+
self, question: Union[QuestionBase, str], expression: str
|
750
|
+
) -> Survey:
|
751
|
+
"""Add a rule that stops the survey.
|
752
|
+
The rule is evaluated *after* the question is answered. If the rule is true, the survey ends.
|
753
|
+
|
754
|
+
:param question: The question to add the stop rule to.
|
755
|
+
:param expression: The expression to evaluate.
|
756
|
+
|
757
|
+
If this rule is true, the survey ends.
|
758
|
+
|
759
|
+
Here, answering "yes" to q0 ends the survey:
|
760
|
+
|
761
|
+
>>> s = Survey.example().add_stop_rule("q0", "q0 == 'yes'")
|
762
|
+
>>> s.next_question("q0", {"q0": "yes"})
|
763
|
+
EndOfSurvey
|
764
|
+
|
765
|
+
By comparison, answering "no" to q0 does not end the survey:
|
766
|
+
|
767
|
+
>>> s.next_question("q0", {"q0": "no"}).question_name
|
768
|
+
'q1'
|
769
|
+
|
770
|
+
>>> s.add_stop_rule("q0", "q1 <> 'yes'")
|
771
|
+
Traceback (most recent call last):
|
772
|
+
...
|
773
|
+
edsl.exceptions.surveys.SurveyCreationError: The expression contains '<>', which is not allowed. You probably mean '!='.
|
774
|
+
...
|
775
|
+
"""
|
776
|
+
return RuleManager(self).add_stop_rule(question, expression)
|
777
|
+
|
778
|
+
def clear_non_default_rules(self) -> Survey:
|
779
|
+
"""Remove all non-default rules from the survey.
|
780
|
+
|
781
|
+
>>> Survey.example().show_rules()
|
782
|
+
Dataset([{'current_q': [0, 0, 1, 2]}, {'expression': ['True', "q0 == 'yes'", 'True', 'True']}, {'next_q': [1, 2, 2, 3]}, {'priority': [-1, 0, -1, -1]}, {'before_rule': [False, False, False, False]}])
|
783
|
+
>>> Survey.example().clear_non_default_rules().show_rules()
|
784
|
+
Dataset([{'current_q': [0, 1, 2]}, {'expression': ['True', 'True', 'True']}, {'next_q': [1, 2, 3]}, {'priority': [-1, -1, -1]}, {'before_rule': [False, False, False]}])
|
785
|
+
"""
|
786
|
+
s = Survey()
|
787
|
+
for question in self.questions:
|
788
|
+
s.add_question(question)
|
789
|
+
return s
|
790
|
+
|
791
|
+
def add_skip_rule(
|
792
|
+
self, question: Union[QuestionBase, str], expression: str
|
793
|
+
) -> Survey:
|
794
|
+
"""
|
795
|
+
Adds a per-question skip rule to the survey.
|
796
|
+
|
797
|
+
:param question: The question to add the skip rule to.
|
798
|
+
:param expression: The expression to evaluate.
|
799
|
+
|
800
|
+
This adds a rule that skips 'q0' always, before the question is answered:
|
801
|
+
|
802
|
+
>>> from edsl import QuestionFreeText
|
803
|
+
>>> q0 = QuestionFreeText.example()
|
804
|
+
>>> q0.question_name = "q0"
|
805
|
+
>>> q1 = QuestionFreeText.example()
|
806
|
+
>>> q1.question_name = "q1"
|
807
|
+
>>> s = Survey([q0, q1]).add_skip_rule("q0", "True")
|
808
|
+
>>> s.next_question("q0", {}).question_name
|
809
|
+
'q1'
|
810
|
+
|
811
|
+
Note that this is different from a rule that jumps to some other question *after* the question is answered.
|
812
|
+
|
813
|
+
"""
|
814
|
+
question_index = self._get_question_index(question)
|
815
|
+
return RuleManager(self).add_rule(
|
816
|
+
question, expression, question_index + 1, before_rule=True
|
817
|
+
)
|
818
|
+
|
819
|
+
def add_rule(
|
820
|
+
self,
|
821
|
+
question: Union[QuestionBase, str],
|
822
|
+
expression: str,
|
823
|
+
next_question: Union[QuestionBase, int],
|
824
|
+
before_rule: bool = False,
|
825
|
+
) -> Survey:
|
826
|
+
"""
|
827
|
+
Add a rule to a Question of the Survey.
|
828
|
+
|
829
|
+
:param question: The question to add the rule to.
|
830
|
+
:param expression: The expression to evaluate.
|
831
|
+
:param next_question: The next question to go to if the rule is true.
|
832
|
+
:param before_rule: Whether the rule is evaluated before the question is answered.
|
833
|
+
|
834
|
+
This adds a rule that if the answer to q0 is 'yes', the next question is q2 (as opposed to q1)
|
835
|
+
|
836
|
+
>>> s = Survey.example().add_rule("q0", "{{ q0 }} == 'yes'", "q2")
|
837
|
+
>>> s.next_question("q0", {"q0": "yes"}).question_name
|
838
|
+
'q2'
|
839
|
+
|
840
|
+
"""
|
841
|
+
return RuleManager(self).add_rule(
|
842
|
+
question, expression, next_question, before_rule=before_rule
|
843
|
+
)
|
844
|
+
|
845
|
+
# endregion
|
846
|
+
|
847
|
+
# region: Forward methods
|
848
|
+
def by(self, *args: Union["Agent", "Scenario", "LanguageModel"]) -> "Jobs":
|
849
|
+
"""Add Agents, Scenarios, and LanguageModels to a survey and returns a runnable Jobs object.
|
850
|
+
|
851
|
+
:param args: The Agents, Scenarios, and LanguageModels to add to the survey.
|
852
|
+
|
853
|
+
This takes the survey and adds an Agent and a Scenario via 'by' which converts to a Jobs object:
|
854
|
+
|
855
|
+
>>> s = Survey.example(); from edsl.agents import Agent; from edsl import Scenario
|
856
|
+
>>> s.by(Agent.example()).by(Scenario.example())
|
857
|
+
Jobs(...)
|
858
|
+
"""
|
859
|
+
from edsl.jobs.Jobs import Jobs
|
860
|
+
|
861
|
+
return Jobs(survey=self).by(*args)
|
862
|
+
|
863
|
+
def to_jobs(self):
|
864
|
+
"""Convert the survey to a Jobs object.
|
865
|
+
>>> s = Survey.example()
|
866
|
+
>>> s.to_jobs()
|
867
|
+
Jobs(...)
|
868
|
+
"""
|
869
|
+
from edsl.jobs.Jobs import Jobs
|
870
|
+
|
871
|
+
return Jobs(survey=self)
|
872
|
+
|
873
|
+
def show_prompts(self):
|
874
|
+
"""Show the prompts for the survey."""
|
875
|
+
return self.to_jobs().show_prompts()
|
876
|
+
|
877
|
+
# endregion
|
878
|
+
|
879
|
+
# region: Running the survey
|
880
|
+
|
881
|
+
def __call__(
|
882
|
+
self,
|
883
|
+
model=None,
|
884
|
+
agent=None,
|
885
|
+
cache=None,
|
886
|
+
verbose=False,
|
887
|
+
disable_remote_cache: bool = False,
|
888
|
+
disable_remote_inference: bool = False,
|
889
|
+
**kwargs,
|
890
|
+
):
|
891
|
+
"""Run the survey with default model, taking the required survey as arguments.
|
892
|
+
|
893
|
+
>>> from edsl.questions import QuestionFunctional
|
894
|
+
>>> def f(scenario, agent_traits): return "yes" if scenario["period"] == "morning" else "no"
|
895
|
+
>>> q = QuestionFunctional(question_name = "q0", func = f)
|
896
|
+
>>> s = Survey([q])
|
897
|
+
>>> s(period = "morning", cache = False, disable_remote_cache = True, disable_remote_inference = True).select("answer.q0").first()
|
898
|
+
'yes'
|
899
|
+
>>> s(period = "evening", cache = False, disable_remote_cache = True, disable_remote_inference = True).select("answer.q0").first()
|
900
|
+
'no'
|
901
|
+
"""
|
902
|
+
|
903
|
+
return self.get_job(model, agent, **kwargs).run(
|
904
|
+
cache=cache,
|
905
|
+
verbose=verbose,
|
906
|
+
disable_remote_cache=disable_remote_cache,
|
907
|
+
disable_remote_inference=disable_remote_inference,
|
908
|
+
)
|
909
|
+
|
910
|
+
async def run_async(
|
911
|
+
self,
|
912
|
+
model: Optional["LanguageModel"] = None,
|
913
|
+
agent: Optional["Agent"] = None,
|
914
|
+
cache: Optional["Cache"] = None,
|
915
|
+
disable_remote_inference: bool = False,
|
916
|
+
disable_remote_cache: bool = False,
|
917
|
+
**kwargs,
|
918
|
+
):
|
919
|
+
"""Run the survey with default model, taking the required survey as arguments.
|
920
|
+
|
921
|
+
>>> import asyncio
|
922
|
+
>>> from edsl.questions import QuestionFunctional
|
923
|
+
>>> def f(scenario, agent_traits): return "yes" if scenario["period"] == "morning" else "no"
|
924
|
+
>>> q = QuestionFunctional(question_name = "q0", func = f)
|
925
|
+
>>> s = Survey([q])
|
926
|
+
>>> async def test_run_async(): result = await s.run_async(period="morning", disable_remote_inference = True, disable_remote_cache=True); print(result.select("answer.q0").first())
|
927
|
+
>>> asyncio.run(test_run_async())
|
928
|
+
yes
|
929
|
+
>>> import asyncio
|
930
|
+
>>> from edsl.questions import QuestionFunctional
|
931
|
+
>>> def f(scenario, agent_traits): return "yes" if scenario["period"] == "morning" else "no"
|
932
|
+
>>> q = QuestionFunctional(question_name = "q0", func = f)
|
933
|
+
>>> s = Survey([q])
|
934
|
+
>>> async def test_run_async(): result = await s.run_async(period="evening", disable_remote_inference = True, disable_remote_cache = True); print(result.select("answer.q0").first())
|
935
|
+
>>> results = asyncio.run(test_run_async())
|
936
|
+
no
|
937
|
+
"""
|
938
|
+
# TODO: temp fix by creating a cache
|
939
|
+
if cache is None:
|
940
|
+
from edsl.data import Cache
|
941
|
+
c = Cache()
|
942
|
+
else:
|
943
|
+
c = cache
|
944
|
+
|
945
|
+
|
946
|
+
|
947
|
+
jobs: "Jobs" = self.get_job(model=model, agent=agent, **kwargs).using(c)
|
948
|
+
return await jobs.run_async(
|
949
|
+
disable_remote_inference=disable_remote_inference,
|
950
|
+
disable_remote_cache=disable_remote_cache,
|
951
|
+
)
|
952
|
+
|
953
|
+
def run(self, *args, **kwargs) -> "Results":
|
954
|
+
"""Turn the survey into a Job and runs it.
|
955
|
+
|
956
|
+
>>> from edsl import QuestionFreeText
|
957
|
+
>>> s = Survey([QuestionFreeText.example()])
|
958
|
+
>>> from edsl.language_models import LanguageModel
|
959
|
+
>>> m = LanguageModel.example(test_model = True, canned_response = "Great!")
|
960
|
+
>>> results = s.by(m).run(cache = False, disable_remote_cache = True, disable_remote_inference = True)
|
961
|
+
>>> results.select('answer.*')
|
962
|
+
Dataset([{'answer.how_are_you': ['Great!']}])
|
963
|
+
"""
|
964
|
+
from edsl.jobs.Jobs import Jobs
|
965
|
+
|
966
|
+
return Jobs(survey=self).run(*args, **kwargs)
|
967
|
+
|
968
|
+
def using(self, obj: Union["Cache", "KeyLookup", "BucketCollection"]) -> "Jobs":
|
969
|
+
"""Turn the survey into a Job and appends the arguments to the Job."""
|
970
|
+
from edsl.jobs.Jobs import Jobs
|
971
|
+
|
972
|
+
return Jobs(survey=self).using(obj)
|
973
|
+
|
974
|
+
def duplicate(self):
|
975
|
+
"""Duplicate the survey.
|
976
|
+
|
977
|
+
>>> s = Survey.example()
|
978
|
+
>>> s2 = s.duplicate()
|
979
|
+
>>> s == s2
|
980
|
+
True
|
981
|
+
>>> s is s2
|
982
|
+
False
|
983
|
+
|
984
|
+
"""
|
985
|
+
return Survey.from_dict(self.to_dict())
|
986
|
+
|
987
|
+
# region: Survey flow
|
988
|
+
def next_question(
|
989
|
+
self,
|
990
|
+
current_question: Optional[Union[str, QuestionBase]] = None,
|
991
|
+
answers: Optional[dict] = None,
|
992
|
+
) -> Union[QuestionBase, EndOfSurvey.__class__]:
|
993
|
+
"""
|
994
|
+
Return the next question in a survey.
|
995
|
+
|
996
|
+
:param current_question: The current question in the survey.
|
997
|
+
:param answers: The answers for the survey so far
|
998
|
+
|
999
|
+
- If called with no arguments, it returns the first question in the survey.
|
1000
|
+
- If no answers are provided for a question with a rule, the next question is returned. If answers are provided, the next question is determined by the rules and the answers.
|
1001
|
+
- If the next question is the last question in the survey, an EndOfSurvey object is returned.
|
1002
|
+
|
1003
|
+
>>> s = Survey.example()
|
1004
|
+
>>> s.next_question("q0", {"q0": "yes"}).question_name
|
1005
|
+
'q2'
|
1006
|
+
>>> s.next_question("q0", {"q0": "no"}).question_name
|
1007
|
+
'q1'
|
1008
|
+
|
1009
|
+
"""
|
1010
|
+
if current_question is None:
|
1011
|
+
return self.questions[0]
|
1012
|
+
|
1013
|
+
if isinstance(current_question, str):
|
1014
|
+
current_question = self._get_question_by_name(current_question)
|
1015
|
+
|
1016
|
+
question_index = self.question_name_to_index[current_question.question_name]
|
1017
|
+
next_question_object = self.rule_collection.next_question(
|
1018
|
+
question_index, answers
|
1019
|
+
)
|
1020
|
+
|
1021
|
+
if next_question_object.num_rules_found == 0:
|
1022
|
+
raise SurveyHasNoRulesError
|
1023
|
+
|
1024
|
+
if next_question_object.next_q == EndOfSurvey:
|
1025
|
+
return EndOfSurvey
|
1026
|
+
else:
|
1027
|
+
if next_question_object.next_q >= len(self.questions):
|
1028
|
+
return EndOfSurvey
|
1029
|
+
else:
|
1030
|
+
return self.questions[next_question_object.next_q]
|
1031
|
+
|
1032
|
+
def gen_path_through_survey(self) -> Generator[QuestionBase, dict, None]:
|
1033
|
+
"""
|
1034
|
+
Generate a coroutine that can be used to conduct an Interview.
|
1035
|
+
|
1036
|
+
The coroutine is a generator that yields a question and receives answers.
|
1037
|
+
It starts with the first question in the survey.
|
1038
|
+
The coroutine ends when an EndOfSurvey object is returned.
|
1039
|
+
|
1040
|
+
For the example survey, this is the rule table:
|
1041
|
+
|
1042
|
+
>>> s = Survey.example()
|
1043
|
+
>>> s.show_rules()
|
1044
|
+
Dataset([{'current_q': [0, 0, 1, 2]}, {'expression': ['True', "q0 == 'yes'", 'True', 'True']}, {'next_q': [1, 2, 2, 3]}, {'priority': [-1, 0, -1, -1]}, {'before_rule': [False, False, False, False]}])
|
1045
|
+
|
1046
|
+
Note that q0 has a rule that if the answer is 'yes', the next question is q2. If the answer is 'no', the next question is q1.
|
1047
|
+
|
1048
|
+
Here is the path through the survey if the answer to q0 is 'yes':
|
1049
|
+
|
1050
|
+
>>> i = s.gen_path_through_survey()
|
1051
|
+
>>> next(i)
|
1052
|
+
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1053
|
+
>>> i.send({"q0": "yes"})
|
1054
|
+
Question('multiple_choice', question_name = \"""q2\""", question_text = \"""Why?\""", question_options = ['**lack*** of killer bees in cafeteria', 'other'])
|
1055
|
+
|
1056
|
+
And here is the path through the survey if the answer to q0 is 'no':
|
1057
|
+
|
1058
|
+
>>> i2 = s.gen_path_through_survey()
|
1059
|
+
>>> next(i2)
|
1060
|
+
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1061
|
+
>>> i2.send({"q0": "no"})
|
1062
|
+
Question('multiple_choice', question_name = \"""q1\""", question_text = \"""Why not?\""", question_options = ['killer bees in cafeteria', 'other'])
|
1063
|
+
|
1064
|
+
|
1065
|
+
"""
|
1066
|
+
self.answers = {}
|
1067
|
+
question = self._questions[0]
|
1068
|
+
# should the first question be skipped?
|
1069
|
+
if self.rule_collection.skip_question_before_running(0, self.answers):
|
1070
|
+
question = self.next_question(question, self.answers)
|
1071
|
+
|
1072
|
+
while not question == EndOfSurvey:
|
1073
|
+
answer = yield question
|
1074
|
+
self.answers.update(answer)
|
1075
|
+
# print(f"Answers: {self.answers}")
|
1076
|
+
## TODO: This should also include survey and agent attributes
|
1077
|
+
question = self.next_question(question, self.answers)
|
1078
|
+
|
1079
|
+
# endregion
|
1080
|
+
|
1081
|
+
def dag(self, textify: bool = False) -> DAG:
|
1082
|
+
"""Return the DAG of the survey, which reflects both skip-logic and memory.
|
1083
|
+
|
1084
|
+
:param textify: Whether to return the DAG with question names instead of indices.
|
1085
|
+
|
1086
|
+
>>> s = Survey.example()
|
1087
|
+
>>> d = s.dag()
|
1088
|
+
>>> d
|
1089
|
+
{1: {0}, 2: {0}}
|
1090
|
+
|
1091
|
+
"""
|
1092
|
+
from edsl.surveys.ConstructDAG import ConstructDAG
|
1093
|
+
|
1094
|
+
return ConstructDAG(self).dag(textify)
|
1095
|
+
|
1096
|
+
###################
|
1097
|
+
# DUNDER METHODS
|
1098
|
+
###################
|
1099
|
+
def __len__(self) -> int:
|
1100
|
+
"""Return the number of questions in the survey.
|
1101
|
+
|
1102
|
+
>>> s = Survey.example()
|
1103
|
+
>>> len(s)
|
1104
|
+
3
|
1105
|
+
"""
|
1106
|
+
return len(self._questions)
|
1107
|
+
|
1108
|
+
def __getitem__(self, index) -> QuestionBase:
|
1109
|
+
"""Return the question object given the question index.
|
1110
|
+
|
1111
|
+
:param index: The index of the question to get.
|
1112
|
+
|
1113
|
+
>>> s = Survey.example()
|
1114
|
+
>>> s[0]
|
1115
|
+
Question('multiple_choice', question_name = \"""q0\""", question_text = \"""Do you like school?\""", question_options = ['yes', 'no'])
|
1116
|
+
|
1117
|
+
"""
|
1118
|
+
if isinstance(index, int):
|
1119
|
+
return self._questions[index]
|
1120
|
+
elif isinstance(index, str):
|
1121
|
+
return getattr(self, index)
|
1122
|
+
|
1123
|
+
# def _diff(self, other):
|
1124
|
+
# """Used for debugging. Print out the differences between two surveys."""
|
1125
|
+
# from rich import print
|
1126
|
+
|
1127
|
+
# for key, value in self.to_dict().items():
|
1128
|
+
# if value != other.to_dict()[key]:
|
1129
|
+
# print(f"Key: {key}")
|
1130
|
+
# print("\n")
|
1131
|
+
# print(f"Self: {value}")
|
1132
|
+
# print("\n")
|
1133
|
+
# print(f"Other: {other.to_dict()[key]}")
|
1134
|
+
# print("\n\n")
|
1135
|
+
|
1136
|
+
def __repr__(self) -> str:
|
1137
|
+
"""Return a string representation of the survey."""
|
1138
|
+
|
1139
|
+
# questions_string = ", ".join([repr(q) for q in self._questions])
|
1140
|
+
questions_string = ", ".join([repr(q) for q in self.raw_passed_questions or []])
|
1141
|
+
# question_names_string = ", ".join([repr(name) for name in self.question_names])
|
1142
|
+
return f"Survey(questions=[{questions_string}], memory_plan={self.memory_plan}, rule_collection={self.rule_collection}, question_groups={self.question_groups}, questions_to_randomize={self.questions_to_randomize})"
|
1143
|
+
|
1144
|
+
def _summary(self) -> dict:
|
1145
|
+
return {
|
1146
|
+
"# questions": len(self),
|
1147
|
+
"question_name list": self.question_names,
|
1148
|
+
}
|
1149
|
+
|
1150
|
+
def tree(self, node_list: Optional[List[str]] = None):
|
1151
|
+
return self.to_scenario_list().tree(node_list=node_list)
|
1152
|
+
|
1153
|
+
def table(self, *fields, tablefmt=None) -> Table:
|
1154
|
+
return self.to_scenario_list().to_dataset().table(*fields, tablefmt=tablefmt)
|
1155
|
+
|
1156
|
+
# endregion
|
1157
|
+
|
1158
|
+
def codebook(self) -> dict[str, str]:
|
1159
|
+
"""Create a codebook for the survey, mapping question names to question text.
|
1160
|
+
|
1161
|
+
>>> s = Survey.example()
|
1162
|
+
>>> s.codebook()
|
1163
|
+
{'q0': 'Do you like school?', 'q1': 'Why not?', 'q2': 'Why?'}
|
1164
|
+
"""
|
1165
|
+
codebook = {}
|
1166
|
+
for question in self._questions:
|
1167
|
+
codebook[question.question_name] = question.question_text
|
1168
|
+
return codebook
|
1169
|
+
|
1170
|
+
@classmethod
|
1171
|
+
def example(
|
1172
|
+
cls,
|
1173
|
+
params: bool = False,
|
1174
|
+
randomize: bool = False,
|
1175
|
+
include_instructions=False,
|
1176
|
+
custom_instructions: Optional[str] = None,
|
1177
|
+
) -> Survey:
|
1178
|
+
"""Return an example survey.
|
1179
|
+
|
1180
|
+
>>> s = Survey.example()
|
1181
|
+
>>> [q.question_text for q in s.questions]
|
1182
|
+
['Do you like school?', 'Why not?', 'Why?']
|
1183
|
+
"""
|
1184
|
+
from edsl.questions.QuestionMultipleChoice import QuestionMultipleChoice
|
1185
|
+
|
1186
|
+
addition = "" if not randomize else str(uuid4())
|
1187
|
+
q0 = QuestionMultipleChoice(
|
1188
|
+
question_text=f"Do you like school?{addition}",
|
1189
|
+
question_options=["yes", "no"],
|
1190
|
+
question_name="q0",
|
1191
|
+
)
|
1192
|
+
q1 = QuestionMultipleChoice(
|
1193
|
+
question_text="Why not?",
|
1194
|
+
question_options=["killer bees in cafeteria", "other"],
|
1195
|
+
question_name="q1",
|
1196
|
+
)
|
1197
|
+
q2 = QuestionMultipleChoice(
|
1198
|
+
question_text="Why?",
|
1199
|
+
question_options=["**lack*** of killer bees in cafeteria", "other"],
|
1200
|
+
question_name="q2",
|
1201
|
+
)
|
1202
|
+
if params:
|
1203
|
+
q3 = QuestionMultipleChoice(
|
1204
|
+
question_text="To the question '{{ q0.question_text}}', you said '{{ q0.answer }}'. Do you still feel this way?",
|
1205
|
+
question_options=["yes", "no"],
|
1206
|
+
question_name="q3",
|
1207
|
+
)
|
1208
|
+
s = cls(questions=[q0, q1, q2, q3])
|
1209
|
+
return s
|
1210
|
+
|
1211
|
+
if include_instructions:
|
1212
|
+
from edsl import Instruction
|
1213
|
+
|
1214
|
+
custom_instructions = (
|
1215
|
+
custom_instructions if custom_instructions else "Please pay attention!"
|
1216
|
+
)
|
1217
|
+
|
1218
|
+
i = Instruction(text=custom_instructions, name="attention")
|
1219
|
+
s = cls(questions=[i, q0, q1, q2])
|
1220
|
+
return s
|
1221
|
+
|
1222
|
+
s = cls(questions=[q0, q1, q2])
|
1223
|
+
s = s.add_rule(q0, "q0 == 'yes'", q2)
|
1224
|
+
return s
|
1225
|
+
|
1226
|
+
def get_job(self, model=None, agent=None, **kwargs):
|
1227
|
+
if model is None:
|
1228
|
+
from edsl.language_models.model import Model
|
1229
|
+
|
1230
|
+
model = Model()
|
1231
|
+
|
1232
|
+
from edsl.scenarios.Scenario import Scenario
|
1233
|
+
|
1234
|
+
s = Scenario(kwargs)
|
1235
|
+
|
1236
|
+
if not agent:
|
1237
|
+
from edsl.agents.Agent import Agent
|
1238
|
+
|
1239
|
+
agent = Agent()
|
1240
|
+
|
1241
|
+
return self.by(s).by(agent).by(model)
|
1242
|
+
|
1243
|
+
|
1244
|
+
def main():
|
1245
|
+
"""Run the example survey."""
|
1246
|
+
|
1247
|
+
def example_survey():
|
1248
|
+
"""Return an example survey."""
|
1249
|
+
from edsl import QuestionMultipleChoice, QuestionList, QuestionNumerical, Survey
|
1250
|
+
|
1251
|
+
q0 = QuestionMultipleChoice(
|
1252
|
+
question_name="q0",
|
1253
|
+
question_text="What is the capital of France?",
|
1254
|
+
question_options=["London", "Paris", "Rome", "Boston", "I don't know"]
|
1255
|
+
)
|
1256
|
+
q1 = QuestionList(
|
1257
|
+
question_name="q1",
|
1258
|
+
question_text="Name some cities in France.",
|
1259
|
+
max_list_items = 5
|
1260
|
+
)
|
1261
|
+
q2 = QuestionNumerical(
|
1262
|
+
question_name="q2",
|
1263
|
+
question_text="What is the population of {{ q0.answer }}?"
|
1264
|
+
)
|
1265
|
+
s = Survey(questions=[q0, q1, q2])
|
1266
|
+
s = s.add_rule(q0, "q0 == 'Paris'", q2)
|
1267
|
+
return s
|
1268
|
+
|
1269
|
+
s = example_survey()
|
1270
|
+
survey_dict = s.to_dict()
|
1271
|
+
s2 = Survey.from_dict(survey_dict)
|
1272
|
+
results = s2.run()
|
1273
|
+
print(results)
|
1274
|
+
|
1275
|
+
|
1276
|
+
if __name__ == "__main__":
|
1277
|
+
import doctest
|
1278
|
+
|
1279
|
+
# doctest.testmod(optionflags=doctest.ELLIPSIS | doctest.SKIP)
|
1280
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|