edsl 0.1.27.dev2__py3-none-any.whl → 0.1.29__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 +107 -30
- edsl/BaseDiff.py +260 -0
- edsl/__init__.py +25 -21
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +103 -46
- edsl/agents/AgentList.py +97 -13
- edsl/agents/Invigilator.py +23 -10
- edsl/agents/InvigilatorBase.py +19 -14
- edsl/agents/PromptConstructionMixin.py +342 -100
- edsl/agents/descriptors.py +5 -2
- edsl/base/Base.py +289 -0
- edsl/config.py +2 -1
- edsl/conjure/AgentConstructionMixin.py +152 -0
- edsl/conjure/Conjure.py +56 -0
- edsl/conjure/InputData.py +659 -0
- edsl/conjure/InputDataCSV.py +48 -0
- edsl/conjure/InputDataMixinQuestionStats.py +182 -0
- edsl/conjure/InputDataPyRead.py +91 -0
- edsl/conjure/InputDataSPSS.py +8 -0
- edsl/conjure/InputDataStata.py +8 -0
- edsl/conjure/QuestionOptionMixin.py +76 -0
- edsl/conjure/QuestionTypeMixin.py +23 -0
- edsl/conjure/RawQuestion.py +65 -0
- edsl/conjure/SurveyResponses.py +7 -0
- edsl/conjure/__init__.py +9 -4
- edsl/conjure/examples/placeholder.txt +0 -0
- edsl/conjure/naming_utilities.py +263 -0
- edsl/conjure/utilities.py +165 -28
- edsl/conversation/Conversation.py +238 -0
- edsl/conversation/car_buying.py +58 -0
- edsl/conversation/mug_negotiation.py +81 -0
- edsl/conversation/next_speaker_utilities.py +93 -0
- edsl/coop/coop.py +337 -121
- edsl/coop/utils.py +56 -70
- edsl/data/Cache.py +74 -22
- edsl/data/CacheHandler.py +10 -9
- edsl/data/SQLiteDict.py +11 -3
- edsl/inference_services/AnthropicService.py +1 -0
- edsl/inference_services/DeepInfraService.py +20 -13
- edsl/inference_services/GoogleService.py +7 -1
- edsl/inference_services/InferenceServicesCollection.py +33 -7
- edsl/inference_services/OpenAIService.py +17 -10
- edsl/inference_services/models_available_cache.py +69 -0
- edsl/inference_services/rate_limits_cache.py +25 -0
- edsl/inference_services/write_available.py +10 -0
- edsl/jobs/Answers.py +15 -1
- edsl/jobs/Jobs.py +322 -73
- edsl/jobs/buckets/BucketCollection.py +9 -3
- edsl/jobs/buckets/ModelBuckets.py +4 -2
- edsl/jobs/buckets/TokenBucket.py +1 -2
- edsl/jobs/interviews/Interview.py +7 -10
- edsl/jobs/interviews/InterviewStatusMixin.py +3 -3
- edsl/jobs/interviews/InterviewTaskBuildingMixin.py +39 -20
- edsl/jobs/interviews/retry_management.py +4 -4
- edsl/jobs/runners/JobsRunnerAsyncio.py +103 -65
- edsl/jobs/runners/JobsRunnerStatusData.py +3 -3
- edsl/jobs/tasks/QuestionTaskCreator.py +4 -2
- edsl/jobs/tasks/TaskHistory.py +4 -3
- edsl/language_models/LanguageModel.py +42 -55
- edsl/language_models/ModelList.py +96 -0
- edsl/language_models/registry.py +14 -0
- edsl/language_models/repair.py +97 -25
- edsl/notebooks/Notebook.py +157 -32
- edsl/prompts/Prompt.py +31 -19
- edsl/questions/QuestionBase.py +145 -23
- edsl/questions/QuestionBudget.py +5 -6
- edsl/questions/QuestionCheckBox.py +7 -3
- edsl/questions/QuestionExtract.py +5 -3
- edsl/questions/QuestionFreeText.py +3 -3
- edsl/questions/QuestionFunctional.py +0 -3
- edsl/questions/QuestionList.py +3 -4
- edsl/questions/QuestionMultipleChoice.py +16 -8
- edsl/questions/QuestionNumerical.py +4 -3
- edsl/questions/QuestionRank.py +5 -3
- edsl/questions/__init__.py +4 -3
- edsl/questions/descriptors.py +9 -4
- edsl/questions/question_registry.py +27 -31
- edsl/questions/settings.py +1 -1
- edsl/results/Dataset.py +31 -0
- edsl/results/DatasetExportMixin.py +493 -0
- edsl/results/Result.py +42 -82
- edsl/results/Results.py +178 -66
- edsl/results/ResultsDBMixin.py +10 -9
- edsl/results/ResultsExportMixin.py +23 -507
- edsl/results/ResultsGGMixin.py +3 -3
- edsl/results/ResultsToolsMixin.py +9 -9
- edsl/scenarios/FileStore.py +140 -0
- edsl/scenarios/Scenario.py +59 -6
- edsl/scenarios/ScenarioList.py +138 -52
- edsl/scenarios/ScenarioListExportMixin.py +32 -0
- edsl/scenarios/ScenarioListPdfMixin.py +2 -1
- edsl/scenarios/__init__.py +1 -0
- edsl/study/ObjectEntry.py +173 -0
- edsl/study/ProofOfWork.py +113 -0
- edsl/study/SnapShot.py +73 -0
- edsl/study/Study.py +498 -0
- edsl/study/__init__.py +4 -0
- edsl/surveys/MemoryPlan.py +11 -4
- edsl/surveys/Survey.py +124 -37
- edsl/surveys/SurveyExportMixin.py +25 -5
- edsl/surveys/SurveyFlowVisualizationMixin.py +6 -4
- edsl/tools/plotting.py +4 -2
- edsl/utilities/__init__.py +21 -20
- edsl/utilities/gcp_bucket/__init__.py +0 -0
- edsl/utilities/gcp_bucket/cloud_storage.py +96 -0
- edsl/utilities/gcp_bucket/simple_example.py +9 -0
- edsl/utilities/interface.py +90 -73
- edsl/utilities/repair_functions.py +28 -0
- edsl/utilities/utilities.py +59 -6
- {edsl-0.1.27.dev2.dist-info → edsl-0.1.29.dist-info}/METADATA +42 -15
- edsl-0.1.29.dist-info/RECORD +203 -0
- edsl/conjure/RawResponseColumn.py +0 -327
- edsl/conjure/SurveyBuilder.py +0 -308
- edsl/conjure/SurveyBuilderCSV.py +0 -78
- edsl/conjure/SurveyBuilderSPSS.py +0 -118
- edsl/data/RemoteDict.py +0 -103
- edsl-0.1.27.dev2.dist-info/RECORD +0 -172
- {edsl-0.1.27.dev2.dist-info → edsl-0.1.29.dist-info}/LICENSE +0 -0
- {edsl-0.1.27.dev2.dist-info → edsl-0.1.29.dist-info}/WHEEL +0 -0
edsl/base/Base.py
ADDED
@@ -0,0 +1,289 @@
|
|
1
|
+
"""Base class for all classes in the package. It provides rich printing and persistence of objects."""
|
2
|
+
|
3
|
+
from abc import ABC, abstractmethod, ABCMeta
|
4
|
+
import gzip
|
5
|
+
import io
|
6
|
+
import json
|
7
|
+
from typing import Any, Optional, Union
|
8
|
+
from uuid import UUID
|
9
|
+
from IPython.display import display
|
10
|
+
from rich.console import Console
|
11
|
+
|
12
|
+
|
13
|
+
class RichPrintingMixin:
|
14
|
+
"""Mixin for rich printing and persistence of objects."""
|
15
|
+
|
16
|
+
def _for_console(self):
|
17
|
+
"""Return a string representation of the object for console printing."""
|
18
|
+
with io.StringIO() as buf:
|
19
|
+
console = Console(file=buf, record=True)
|
20
|
+
table = self.rich_print()
|
21
|
+
console.print(table)
|
22
|
+
return console.export_text()
|
23
|
+
|
24
|
+
def __str__(self):
|
25
|
+
"""Return a string representation of the object for console printing."""
|
26
|
+
return self._for_console()
|
27
|
+
|
28
|
+
def print(self):
|
29
|
+
"""Print the object to the console."""
|
30
|
+
from edsl.utilities.utilities import is_notebook
|
31
|
+
|
32
|
+
if is_notebook():
|
33
|
+
display(self.rich_print())
|
34
|
+
else:
|
35
|
+
from rich.console import Console
|
36
|
+
|
37
|
+
console = Console()
|
38
|
+
console.print(self.rich_print())
|
39
|
+
|
40
|
+
|
41
|
+
class PersistenceMixin:
|
42
|
+
"""Mixin for saving and loading objects to and from files."""
|
43
|
+
|
44
|
+
def push(
|
45
|
+
self,
|
46
|
+
description: Optional[str] = None,
|
47
|
+
visibility: Optional[str] = "unlisted",
|
48
|
+
):
|
49
|
+
"""Post the object to coop."""
|
50
|
+
from edsl.coop import Coop
|
51
|
+
|
52
|
+
c = Coop()
|
53
|
+
return c.create(self, description, visibility)
|
54
|
+
|
55
|
+
@classmethod
|
56
|
+
def pull(cls, id_or_url: Union[str, UUID], exec_profile=None):
|
57
|
+
"""Pull the object from coop."""
|
58
|
+
from edsl.coop import Coop
|
59
|
+
|
60
|
+
if id_or_url.startswith("http"):
|
61
|
+
uuid_value = id_or_url.split("/")[-1]
|
62
|
+
else:
|
63
|
+
uuid_value = id_or_url
|
64
|
+
|
65
|
+
c = Coop()
|
66
|
+
|
67
|
+
return c._get_base(cls, uuid_value, exec_profile=exec_profile)
|
68
|
+
|
69
|
+
@classmethod
|
70
|
+
def delete(cls, id_or_url: Union[str, UUID]):
|
71
|
+
"""Delete the object from coop."""
|
72
|
+
from edsl.coop import Coop
|
73
|
+
|
74
|
+
c = Coop()
|
75
|
+
return c._delete_base(cls, id_or_url)
|
76
|
+
|
77
|
+
@classmethod
|
78
|
+
def patch(
|
79
|
+
cls,
|
80
|
+
id_or_url: Union[str, UUID],
|
81
|
+
description: Optional[str] = None,
|
82
|
+
value: Optional[Any] = None,
|
83
|
+
visibility: Optional[str] = None,
|
84
|
+
):
|
85
|
+
"""
|
86
|
+
Patch an uploaded objects attributes.
|
87
|
+
- `description` changes the description of the object on Coop
|
88
|
+
- `value` changes the value of the object on Coop. **has to be an EDSL object**
|
89
|
+
- `visibility` changes the visibility of the object on Coop
|
90
|
+
"""
|
91
|
+
from edsl.coop import Coop
|
92
|
+
|
93
|
+
c = Coop()
|
94
|
+
return c._patch_base(cls, id_or_url, description, value, visibility)
|
95
|
+
|
96
|
+
@classmethod
|
97
|
+
def search(cls, query):
|
98
|
+
"""Search for objects on coop."""
|
99
|
+
from edsl.coop import Coop
|
100
|
+
|
101
|
+
c = Coop()
|
102
|
+
return c.search(cls, query)
|
103
|
+
|
104
|
+
def save(self, filename, compress=True):
|
105
|
+
"""Save the object to a file as zippped JSON.
|
106
|
+
|
107
|
+
>>> obj.save("obj.json.gz")
|
108
|
+
|
109
|
+
"""
|
110
|
+
if filename.endswith("json.gz"):
|
111
|
+
import warnings
|
112
|
+
|
113
|
+
warnings.warn(
|
114
|
+
"Do not apply the file extensions. The filename should not end with 'json.gz'."
|
115
|
+
)
|
116
|
+
filename = filename[:-7]
|
117
|
+
if filename.endswith("json"):
|
118
|
+
filename = filename[:-4]
|
119
|
+
warnings.warn(
|
120
|
+
"Do not apply the file extensions. The filename should not end with 'json'."
|
121
|
+
)
|
122
|
+
|
123
|
+
if compress:
|
124
|
+
with gzip.open(filename + ".json.gz", "wb") as f:
|
125
|
+
f.write(json.dumps(self.to_dict()).encode("utf-8"))
|
126
|
+
else:
|
127
|
+
with open(filename + ".json", "w") as f:
|
128
|
+
f.write(json.dumps(self.to_dict()))
|
129
|
+
|
130
|
+
@staticmethod
|
131
|
+
def open_compressed_file(filename):
|
132
|
+
with gzip.open(filename, "rb") as f:
|
133
|
+
file_contents = f.read()
|
134
|
+
file_contents_decoded = file_contents.decode("utf-8")
|
135
|
+
d = json.loads(file_contents_decoded)
|
136
|
+
return d
|
137
|
+
|
138
|
+
@staticmethod
|
139
|
+
def open_regular_file(filename):
|
140
|
+
with open(filename, "r") as f:
|
141
|
+
d = json.loads(f.read())
|
142
|
+
return d
|
143
|
+
|
144
|
+
@classmethod
|
145
|
+
def load(cls, filename):
|
146
|
+
"""Load the object from a file.
|
147
|
+
|
148
|
+
>>> obj = cls.load("obj.json.gz")
|
149
|
+
|
150
|
+
"""
|
151
|
+
|
152
|
+
if filename.endswith("json.gz"):
|
153
|
+
d = cls.open_compressed_file(filename)
|
154
|
+
elif filename.endswith("json"):
|
155
|
+
d = cls.open_regular_file(filename)
|
156
|
+
else:
|
157
|
+
try:
|
158
|
+
d = cls.open_compressed_file(filename)
|
159
|
+
except:
|
160
|
+
d = cls.open_regular_file(filename)
|
161
|
+
finally:
|
162
|
+
raise ValueError("File must be a json or json.gz file")
|
163
|
+
|
164
|
+
return cls.from_dict(d)
|
165
|
+
|
166
|
+
|
167
|
+
class RegisterSubclassesMeta(ABCMeta):
|
168
|
+
"""Metaclass for registering subclasses."""
|
169
|
+
|
170
|
+
_registry = {}
|
171
|
+
|
172
|
+
def __init__(cls, name, bases, nmspc):
|
173
|
+
"""Register the class in the registry upon creation."""
|
174
|
+
super(RegisterSubclassesMeta, cls).__init__(name, bases, nmspc)
|
175
|
+
if cls.__name__ != "Base":
|
176
|
+
RegisterSubclassesMeta._registry[cls.__name__] = cls
|
177
|
+
|
178
|
+
@staticmethod
|
179
|
+
def get_registry():
|
180
|
+
"""Return the registry of subclasses."""
|
181
|
+
return dict(RegisterSubclassesMeta._registry)
|
182
|
+
|
183
|
+
|
184
|
+
class DiffMethodsMixin:
|
185
|
+
def __sub__(self, other):
|
186
|
+
"""Return the difference between two objects."""
|
187
|
+
from edsl.BaseDiff import BaseDiff
|
188
|
+
|
189
|
+
return BaseDiff(self, other)
|
190
|
+
|
191
|
+
|
192
|
+
class Base(
|
193
|
+
RichPrintingMixin,
|
194
|
+
PersistenceMixin,
|
195
|
+
DiffMethodsMixin,
|
196
|
+
ABC,
|
197
|
+
metaclass=RegisterSubclassesMeta,
|
198
|
+
):
|
199
|
+
"""Base class for all classes in the package."""
|
200
|
+
|
201
|
+
# def __getitem__(self, key):
|
202
|
+
# return getattr(self, key)
|
203
|
+
|
204
|
+
# @abstractmethod
|
205
|
+
# def _repr_html_(self) -> str:
|
206
|
+
# raise NotImplementedError("This method is not implemented yet.")
|
207
|
+
|
208
|
+
# @abstractmethod
|
209
|
+
# def _repr_(self) -> str:
|
210
|
+
# raise NotImplementedError("This method is not implemented yet.")
|
211
|
+
|
212
|
+
def keys(self):
|
213
|
+
"""Return the keys of the object."""
|
214
|
+
_keys = list(self.to_dict().keys())
|
215
|
+
if "edsl_version" in _keys:
|
216
|
+
_keys.remove("edsl_version")
|
217
|
+
if "edsl_class_name" in _keys:
|
218
|
+
_keys.remove("edsl_class_name")
|
219
|
+
return _keys
|
220
|
+
|
221
|
+
def values(self):
|
222
|
+
"""Return the values of the object."""
|
223
|
+
data = self.to_dict()
|
224
|
+
keys = self.keys()
|
225
|
+
return {data[key] for key in keys}
|
226
|
+
|
227
|
+
def _repr_html_(self):
|
228
|
+
from edsl.utilities.utilities import data_to_html
|
229
|
+
|
230
|
+
return data_to_html(self.to_dict())
|
231
|
+
|
232
|
+
# def html(self):
|
233
|
+
# html_string = self._repr_html_()
|
234
|
+
# import tempfile
|
235
|
+
# import webbrowser
|
236
|
+
|
237
|
+
# with tempfile.NamedTemporaryFile("w", delete=False, suffix=".html") as f:
|
238
|
+
# # print("Writing HTML to", f.name)
|
239
|
+
# f.write(html_string)
|
240
|
+
# webbrowser.open(f.name)
|
241
|
+
|
242
|
+
def __eq__(self, other):
|
243
|
+
"""Return whether two objects are equal."""
|
244
|
+
import inspect
|
245
|
+
|
246
|
+
if not isinstance(other, self.__class__):
|
247
|
+
return False
|
248
|
+
if "sort" in inspect.signature(self._to_dict).parameters:
|
249
|
+
return self._to_dict(sort=True) == other._to_dict(sort=True)
|
250
|
+
else:
|
251
|
+
return self._to_dict() == other._to_dict()
|
252
|
+
|
253
|
+
@abstractmethod
|
254
|
+
def example():
|
255
|
+
"""This method should be implemented by subclasses."""
|
256
|
+
raise NotImplementedError("This method is not implemented yet.")
|
257
|
+
|
258
|
+
@abstractmethod
|
259
|
+
def rich_print():
|
260
|
+
"""This method should be implemented by subclasses."""
|
261
|
+
raise NotImplementedError("This method is not implemented yet.")
|
262
|
+
|
263
|
+
@abstractmethod
|
264
|
+
def to_dict():
|
265
|
+
"""This method should be implemented by subclasses."""
|
266
|
+
raise NotImplementedError("This method is not implemented yet.")
|
267
|
+
|
268
|
+
@abstractmethod
|
269
|
+
def from_dict():
|
270
|
+
"""This method should be implemented by subclasses."""
|
271
|
+
raise NotImplementedError("This method is not implemented yet.")
|
272
|
+
|
273
|
+
@abstractmethod
|
274
|
+
def code():
|
275
|
+
"""This method should be implemented by subclasses."""
|
276
|
+
raise NotImplementedError("This method is not implemented yet.")
|
277
|
+
|
278
|
+
def show_methods(self, show_docstrings=True):
|
279
|
+
"""Show the methods of the object."""
|
280
|
+
public_methods_with_docstrings = [
|
281
|
+
(method, getattr(self, method).__doc__)
|
282
|
+
for method in dir(self)
|
283
|
+
if callable(getattr(self, method)) and not method.startswith("_")
|
284
|
+
]
|
285
|
+
if show_docstrings:
|
286
|
+
for method, documentation in public_methods_with_docstrings:
|
287
|
+
print(f"{method}: {documentation}")
|
288
|
+
else:
|
289
|
+
return [x[0] for x in public_methods_with_docstrings]
|
edsl/config.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
"""This module provides a Config class that loads environment variables from a .env file and sets them as class attributes."""
|
2
2
|
|
3
3
|
import os
|
4
|
-
from dotenv import load_dotenv, find_dotenv
|
5
4
|
from edsl.exceptions import (
|
6
5
|
InvalidEnvironmentVariableError,
|
7
6
|
MissingEnvironmentVariableError,
|
8
7
|
)
|
8
|
+
from dotenv import load_dotenv, find_dotenv
|
9
9
|
|
10
10
|
# valid values for EDSL_RUN_MODE
|
11
11
|
EDSL_RUN_MODES = ["development", "development-testrun", "production"]
|
@@ -96,6 +96,7 @@ class Config:
|
|
96
96
|
Loads the .env
|
97
97
|
- Overrides existing env vars unless EDSL_RUN_MODE=="development-testrun"
|
98
98
|
"""
|
99
|
+
|
99
100
|
override = True
|
100
101
|
if self.EDSL_RUN_MODE == "development-testrun":
|
101
102
|
override = False
|
@@ -0,0 +1,152 @@
|
|
1
|
+
import random
|
2
|
+
from typing import Generator, List, Optional, Union, Callable
|
3
|
+
from edsl.agents.Agent import Agent
|
4
|
+
from edsl.agents.AgentList import AgentList
|
5
|
+
from edsl.questions import QuestionBase
|
6
|
+
from edsl.results.Results import Results
|
7
|
+
|
8
|
+
|
9
|
+
class AgentConstructionMixin:
|
10
|
+
def agent(self, index) -> Agent:
|
11
|
+
"""Return an agent constructed from the data.
|
12
|
+
|
13
|
+
:param index: The index of the agent to construct.
|
14
|
+
|
15
|
+
>>> from edsl.conjure.InputData import InputDataABC
|
16
|
+
>>> id = InputDataABC.example()
|
17
|
+
>>> id.agent(0)
|
18
|
+
Agent(traits = {'morning': '1', 'feeling': '3'}, codebook = {'morning': 'how are you doing this morning?', 'feeling': 'how are you feeling?'})
|
19
|
+
|
20
|
+
|
21
|
+
"""
|
22
|
+
responses = [responses[index] for responses in self.raw_data]
|
23
|
+
traits = {qn: r for qn, r in zip(self.question_names, responses)}
|
24
|
+
|
25
|
+
a = Agent(traits=traits, codebook=self.names_to_texts)
|
26
|
+
|
27
|
+
def construct_answer_dict_function(traits: dict) -> Callable:
|
28
|
+
def func(self, question: "QuestionBase", scenario=None):
|
29
|
+
return traits.get(question.question_name, None)
|
30
|
+
|
31
|
+
return func
|
32
|
+
|
33
|
+
a.add_direct_question_answering_method(construct_answer_dict_function(traits))
|
34
|
+
return a
|
35
|
+
|
36
|
+
def _agents(self, indices) -> Generator[Agent, None, None]:
|
37
|
+
"""Return a generator of agents, one for each index."""
|
38
|
+
for idx in indices:
|
39
|
+
yield self.agent(idx)
|
40
|
+
|
41
|
+
def to_agent_list(
|
42
|
+
self,
|
43
|
+
indices: Optional[List] = None,
|
44
|
+
sample_size: int = None,
|
45
|
+
seed: str = "edsl",
|
46
|
+
remove_direct_question_answering_method: bool = True,
|
47
|
+
) -> AgentList:
|
48
|
+
"""Return an AgentList from the data.
|
49
|
+
|
50
|
+
:param indices: The indices of the agents to include.
|
51
|
+
:param sample_size: The number of agents to sample.
|
52
|
+
:param seed: The seed for the random number generator.
|
53
|
+
|
54
|
+
>>> from edsl.conjure.InputData import InputDataABC
|
55
|
+
>>> id = InputDataABC.example()
|
56
|
+
>>> al = id.to_agent_list()
|
57
|
+
>>> len(al) == id.num_observations
|
58
|
+
True
|
59
|
+
>>> al = id.to_agent_list(indices = [0, 1, 2])
|
60
|
+
Traceback (most recent call last):
|
61
|
+
...
|
62
|
+
ValueError: Index 2 is greater than the number of agents 2.
|
63
|
+
"""
|
64
|
+
if indices and (sample_size or seed != "edsl"):
|
65
|
+
raise ValueError(
|
66
|
+
"You cannot pass both indices and sample_size/seed, as these are mutually exclusive."
|
67
|
+
)
|
68
|
+
|
69
|
+
if indices:
|
70
|
+
if len(indices) == 0:
|
71
|
+
raise ValueError("Indices must be a non-empty list.")
|
72
|
+
if max(indices) >= self.num_observations:
|
73
|
+
raise ValueError(
|
74
|
+
f"Index {max(indices)} is greater than the number of agents {self.num_observations}."
|
75
|
+
)
|
76
|
+
if min(indices) < 0:
|
77
|
+
raise ValueError(f"Index {min(indices)} is less than 0.")
|
78
|
+
|
79
|
+
if indices is None:
|
80
|
+
if sample_size is None:
|
81
|
+
indices = range(self.num_observations)
|
82
|
+
else:
|
83
|
+
if sample_size > self.num_observations:
|
84
|
+
raise ValueError(
|
85
|
+
f"Sample size {sample_size} is greater than the number of agents {self.num_observations}."
|
86
|
+
)
|
87
|
+
random.seed(seed)
|
88
|
+
indices = random.sample(range(self.num_observations), sample_size)
|
89
|
+
|
90
|
+
agents = list(self._agents(indices))
|
91
|
+
if remove_direct_question_answering_method:
|
92
|
+
for a in agents:
|
93
|
+
a.remove_direct_question_answering_method()
|
94
|
+
return AgentList(agents)
|
95
|
+
|
96
|
+
def to_results(
|
97
|
+
self,
|
98
|
+
indices: Optional[List] = None,
|
99
|
+
sample_size: int = None,
|
100
|
+
seed: str = "edsl",
|
101
|
+
dryrun=False,
|
102
|
+
) -> Union[Results, None]:
|
103
|
+
"""Return the results of the survey.
|
104
|
+
|
105
|
+
:param indices: The indices of the agents to include.
|
106
|
+
:param sample_size: The number of agents to sample.
|
107
|
+
:param seed: The seed for the random number generator.
|
108
|
+
:param dryrun: If True, the survey will not be run, but the time to run it will be printed.
|
109
|
+
|
110
|
+
>>> from edsl.conjure.InputData import InputDataABC
|
111
|
+
>>> id = InputDataABC.example()
|
112
|
+
>>> r = id.to_results()
|
113
|
+
>>> len(r) == id.num_observations
|
114
|
+
True
|
115
|
+
"""
|
116
|
+
agent_list = self.to_agent_list(
|
117
|
+
indices=indices,
|
118
|
+
sample_size=sample_size,
|
119
|
+
seed=seed,
|
120
|
+
remove_direct_question_answering_method=False,
|
121
|
+
)
|
122
|
+
DRYRUN_SAMPLE = 30
|
123
|
+
survey = self.to_survey()
|
124
|
+
if dryrun:
|
125
|
+
import time
|
126
|
+
|
127
|
+
start = time.time()
|
128
|
+
_ = survey.by(agent_list.sample(DRYRUN_SAMPLE)).run()
|
129
|
+
end = time.time()
|
130
|
+
print(f"Time to run {DRYRUN_SAMPLE} agents (s): {round(end - start, 2)}")
|
131
|
+
time_per_agent = (end - start) / DRYRUN_SAMPLE
|
132
|
+
full_sample_time = time_per_agent * len(agent_list)
|
133
|
+
if full_sample_time < 60:
|
134
|
+
print(
|
135
|
+
f"Full sample will take about {round(full_sample_time, 2)} seconds."
|
136
|
+
)
|
137
|
+
if full_sample_time > 60 and full_sample_time < 3600:
|
138
|
+
print(
|
139
|
+
f"Full sample will take about {round(full_sample_time / 60, 2)} minutes."
|
140
|
+
)
|
141
|
+
if full_sample_time > 3600:
|
142
|
+
print(
|
143
|
+
f"Full sample will take about {round(full_sample_time / 3600, 2)} hours."
|
144
|
+
)
|
145
|
+
return None
|
146
|
+
return survey.by(agent_list).run()
|
147
|
+
|
148
|
+
|
149
|
+
if __name__ == "__main__":
|
150
|
+
import doctest
|
151
|
+
|
152
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
edsl/conjure/Conjure.py
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
from typing import List, Optional, Dict, Callable
|
2
|
+
|
3
|
+
|
4
|
+
class Conjure:
|
5
|
+
def __new__(cls, datafile_name: str, *args, **kwargs):
|
6
|
+
if datafile_name.endswith(".csv"):
|
7
|
+
from edsl.conjure.InputDataCSV import InputDataCSV
|
8
|
+
|
9
|
+
return InputDataCSV(datafile_name, *args, **kwargs)
|
10
|
+
elif datafile_name.endswith(".sav"):
|
11
|
+
from edsl.conjure.InputDataSPSS import InputDataSPSS
|
12
|
+
|
13
|
+
return InputDataSPSS(datafile_name, *args, **kwargs)
|
14
|
+
elif datafile_name.endswith(".dta"):
|
15
|
+
from edsl.conjure.InputDataStata import InputDataStata
|
16
|
+
|
17
|
+
return InputDataStata(datafile_name, *args, **kwargs)
|
18
|
+
else:
|
19
|
+
raise ValueError("Unsupported file type")
|
20
|
+
|
21
|
+
def __init__(
|
22
|
+
self,
|
23
|
+
datafile_name: str,
|
24
|
+
config: Optional[dict] = None,
|
25
|
+
naming_function: Optional[Callable] = None,
|
26
|
+
raw_data: Optional[List] = None,
|
27
|
+
question_names: Optional[List[str]] = None,
|
28
|
+
question_texts: Optional[List[str]] = None,
|
29
|
+
answer_codebook: Optional[Dict] = None,
|
30
|
+
question_types: Optional[List[str]] = None,
|
31
|
+
question_options: Optional[List] = None,
|
32
|
+
order_options=False,
|
33
|
+
question_name_repair_func: Callable = None,
|
34
|
+
):
|
35
|
+
# The __init__ method in Conjure won't be called because __new__ returns a different class instance.
|
36
|
+
pass
|
37
|
+
|
38
|
+
|
39
|
+
if __name__ == "__main__":
|
40
|
+
pass
|
41
|
+
# import glob
|
42
|
+
|
43
|
+
# for file in glob.glob("examples/*"):
|
44
|
+
# if file.endswith(".txt"):
|
45
|
+
# continue
|
46
|
+
# print("\n\n")
|
47
|
+
# print("Now processing:", file)
|
48
|
+
# conjure_instance = Conjure(file)
|
49
|
+
# print(conjure_instance)
|
50
|
+
# conjure_instance.to_results(dryrun=True)
|
51
|
+
# print("\n\n")
|
52
|
+
|
53
|
+
# # c = Conjure("mayors.sav")
|
54
|
+
# # al = c.to_agent_list()
|
55
|
+
# # s = c.to_survey()
|
56
|
+
# # r = c.results()
|