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/study/Study.py
ADDED
@@ -0,0 +1,498 @@
|
|
1
|
+
import copy
|
2
|
+
import inspect
|
3
|
+
import json
|
4
|
+
import os
|
5
|
+
import platform
|
6
|
+
import socket
|
7
|
+
from datetime import datetime
|
8
|
+
from typing import Dict, Optional, Union
|
9
|
+
from edsl import Cache, set_session_cache, unset_session_cache
|
10
|
+
from edsl.utilities.utilities import dict_hash
|
11
|
+
from edsl.study.ObjectEntry import ObjectEntry
|
12
|
+
from edsl.study.ProofOfWork import ProofOfWork
|
13
|
+
from edsl.study.SnapShot import SnapShot
|
14
|
+
from uuid import UUID
|
15
|
+
|
16
|
+
# from edsl.Base import Base
|
17
|
+
|
18
|
+
|
19
|
+
class Study:
|
20
|
+
"""A study organizes a series of EDSL objects.
|
21
|
+
|
22
|
+
```python
|
23
|
+
with Study(name = "cool_study") as study:
|
24
|
+
q = QuestionFreeText.example()
|
25
|
+
results = q.run()
|
26
|
+
```
|
27
|
+
|
28
|
+
The `study` object is a context manager.
|
29
|
+
It lets you group a series of events and objects together.
|
30
|
+
|
31
|
+
>>> with Study(name = "cool_study", verbose = False) as study:
|
32
|
+
... from edsl import QuestionFreeText
|
33
|
+
... q = QuestionFreeText.example()
|
34
|
+
>>> len(study.objects)
|
35
|
+
1
|
36
|
+
>>> _ = os.system("rm cool_study.json")
|
37
|
+
|
38
|
+
|
39
|
+
It records all the edsl objects that are created during the study.
|
40
|
+
On exit, is saves them to a study file.
|
41
|
+
|
42
|
+
"""
|
43
|
+
|
44
|
+
def __init__(
|
45
|
+
self,
|
46
|
+
name: Optional[str] = None,
|
47
|
+
filename: Optional[str] = None,
|
48
|
+
description: Optional[str] = None,
|
49
|
+
objects: Optional[Dict[str, ObjectEntry]] = None,
|
50
|
+
cache: Optional[Cache] = None,
|
51
|
+
coop: bool = False,
|
52
|
+
use_study_cache=True,
|
53
|
+
overwrite_on_change=True,
|
54
|
+
proof_of_work=None,
|
55
|
+
proof_of_work_difficulty: int = None,
|
56
|
+
namespace: Optional[dict] = None,
|
57
|
+
verbose: Optional[bool] = True,
|
58
|
+
):
|
59
|
+
"""
|
60
|
+
:param name: The name of the study.
|
61
|
+
:param description: A description of the study.
|
62
|
+
:param objects: A dictionary of objects to add to the study.
|
63
|
+
:param cache: A cache object to (potentially) use for the study.
|
64
|
+
:param filename: The path to the study file.
|
65
|
+
:param coop: Whether to push the study to coop.
|
66
|
+
:param use_study_cache: Whether to use the study cache.
|
67
|
+
:param overwrite_on_change: Whether to overwrite the study file if it has changed.
|
68
|
+
|
69
|
+
>>> s = Study()
|
70
|
+
Traceback (most recent call last):
|
71
|
+
...
|
72
|
+
ValueError: You must provide a name or a filename for the study.
|
73
|
+
|
74
|
+
"""
|
75
|
+
self.verbose = verbose
|
76
|
+
|
77
|
+
if name is None and filename is None:
|
78
|
+
raise ValueError("You must provide a name or a filename for the study.")
|
79
|
+
|
80
|
+
if filename is None:
|
81
|
+
self.filename = name
|
82
|
+
else:
|
83
|
+
self.filename = filename
|
84
|
+
|
85
|
+
if (
|
86
|
+
self.filename
|
87
|
+
and os.path.exists(self.filename + ".json")
|
88
|
+
and os.path.getsize(self.filename + ".json") > 0
|
89
|
+
):
|
90
|
+
if self.verbose:
|
91
|
+
print(f"Using existing study file {self.filename}.json")
|
92
|
+
self._load_from_file()
|
93
|
+
else:
|
94
|
+
self.name = name
|
95
|
+
self.description = description
|
96
|
+
self.objects = objects or {}
|
97
|
+
self.cache = cache or Cache()
|
98
|
+
self.proof_of_work = proof_of_work or ProofOfWork()
|
99
|
+
|
100
|
+
# These always overwrite the saved study
|
101
|
+
self.coop = coop
|
102
|
+
self.use_study_cache = use_study_cache
|
103
|
+
self.overwrite_on_change = overwrite_on_change
|
104
|
+
self.proof_of_work_difficulty = proof_of_work_difficulty
|
105
|
+
|
106
|
+
self.starting_objects = copy.deepcopy(self.objects)
|
107
|
+
|
108
|
+
if namespace:
|
109
|
+
self._namespace = namespace
|
110
|
+
else:
|
111
|
+
self._namespace = None
|
112
|
+
|
113
|
+
self._create_mapping_dicts()
|
114
|
+
|
115
|
+
@property
|
116
|
+
def namespace(self):
|
117
|
+
return self._find_stack()
|
118
|
+
|
119
|
+
def _find_stack(self) -> dict:
|
120
|
+
"Finds the frame with the Study context"
|
121
|
+
# if self.verbose:
|
122
|
+
# self.explore_stacks()
|
123
|
+
frame = inspect.currentframe()
|
124
|
+
candidate_frames = []
|
125
|
+
while frame:
|
126
|
+
if "Study" in frame.f_globals:
|
127
|
+
candidate_frames.append(frame)
|
128
|
+
|
129
|
+
frame = frame.f_back
|
130
|
+
|
131
|
+
found_variables_dict = {}
|
132
|
+
for frame in candidate_frames:
|
133
|
+
found_variables_dict.update(frame.f_globals)
|
134
|
+
found_variables_dict.update(frame.f_locals)
|
135
|
+
|
136
|
+
return found_variables_dict
|
137
|
+
|
138
|
+
def explore_stacks(self):
|
139
|
+
frame = inspect.currentframe()
|
140
|
+
count = 0
|
141
|
+
d = {}
|
142
|
+
while frame:
|
143
|
+
d[count] = "Study" in frame.f_globals.keys()
|
144
|
+
count += 1
|
145
|
+
from rich import print as rprint
|
146
|
+
|
147
|
+
print("Globals:")
|
148
|
+
rprint(frame.f_globals["__name__"])
|
149
|
+
rprint(frame.f_globals.keys())
|
150
|
+
print("Locals:")
|
151
|
+
rprint(frame.f_locals.keys())
|
152
|
+
print("\n")
|
153
|
+
frame = frame.f_back
|
154
|
+
return d
|
155
|
+
|
156
|
+
def _create_mapping_dicts(self):
|
157
|
+
self._name_to_object = {}
|
158
|
+
self._hash_to_name = {}
|
159
|
+
self._name_to_oe = {}
|
160
|
+
name_counts = {}
|
161
|
+
for hash, obj in self.objects.items():
|
162
|
+
new_name = obj.variable_name
|
163
|
+
if obj.variable_name in name_counts:
|
164
|
+
name_counts[obj.variable_name] += 1
|
165
|
+
new_name = obj.variable_name + "_" + str(name_counts[obj.variable_name])
|
166
|
+
else:
|
167
|
+
name_counts[obj.variable_name] = 1
|
168
|
+
self._name_to_object[new_name] = obj.object
|
169
|
+
self._hash_to_name[hash] = new_name
|
170
|
+
|
171
|
+
def __len__(self):
|
172
|
+
return len(self.objects)
|
173
|
+
|
174
|
+
def __eq__(self, other):
|
175
|
+
return self.objects.keys() == other.objects.keys()
|
176
|
+
|
177
|
+
@property
|
178
|
+
def name_to_object(self):
|
179
|
+
"""
|
180
|
+
>>> s = Study.example()
|
181
|
+
>>> s.name_to_object
|
182
|
+
{'q': Question('free_text', question_name = \"""how_are_you\""", question_text = \"""How are you?\""")}
|
183
|
+
"""
|
184
|
+
self._create_mapping_dicts()
|
185
|
+
return self._name_to_object
|
186
|
+
|
187
|
+
@property
|
188
|
+
def hash_to_name(self):
|
189
|
+
self._create_mapping_dicts()
|
190
|
+
return self._hash_to_name
|
191
|
+
|
192
|
+
def __getattr__(self, name):
|
193
|
+
return self.name_to_object[name]
|
194
|
+
|
195
|
+
@classmethod
|
196
|
+
def from_file(cls, filename: str):
|
197
|
+
"""Load a study from a file."""
|
198
|
+
if filename.endswith(".json"):
|
199
|
+
filename = filename[:-5]
|
200
|
+
return cls(filename=filename)
|
201
|
+
|
202
|
+
def _load_from_file(self):
|
203
|
+
"""Load the study from a file.
|
204
|
+
|
205
|
+
>>> import tempfile
|
206
|
+
>>> filename = tempfile.NamedTemporaryFile(delete=False)
|
207
|
+
>>> study = Study(name = "poo", filename = filename.name)
|
208
|
+
>>> study.save()
|
209
|
+
Saving study to ...
|
210
|
+
>>> study2 = Study(filename = filename.name)
|
211
|
+
Using existing study file ...
|
212
|
+
>>> study2.name
|
213
|
+
'poo'
|
214
|
+
"""
|
215
|
+
with open(self.filename + ".json", "r") as f:
|
216
|
+
d = json.load(f)
|
217
|
+
d["cache"] = Cache.from_dict(d["cache"])
|
218
|
+
d["proof_of_work"] = ProofOfWork.from_dict(d["proof_of_work"])
|
219
|
+
d["objects"] = {
|
220
|
+
hash: ObjectEntry.from_dict(obj_dict)
|
221
|
+
for hash, obj_dict in d["objects"].items()
|
222
|
+
}
|
223
|
+
self.__dict__.update(d)
|
224
|
+
|
225
|
+
def __enter__(self):
|
226
|
+
"""
|
227
|
+
>>> s = Study(name = "temp", use_study_cache = True, verbose = False)
|
228
|
+
>>> _ = s.__enter__()
|
229
|
+
>>> from edsl.config import CONFIG
|
230
|
+
>>> hasattr(CONFIG, "EDSL_SESSION_CACHE")
|
231
|
+
True
|
232
|
+
>>> _ = s.__exit__(None, None, None)
|
233
|
+
>>> len(s.objects)
|
234
|
+
0
|
235
|
+
>>> os.remove("temp.json")
|
236
|
+
|
237
|
+
"""
|
238
|
+
if self.verbose:
|
239
|
+
print("Existing objects in study:")
|
240
|
+
self.print()
|
241
|
+
snapshot = SnapShot(self.namespace, exclude=[self])
|
242
|
+
if self.use_study_cache:
|
243
|
+
if self.verbose:
|
244
|
+
print("Using study cache.")
|
245
|
+
set_session_cache(self.cache)
|
246
|
+
|
247
|
+
if snapshot.edsl_objects:
|
248
|
+
raise ValueError(
|
249
|
+
"You have EDSL objects in the global namespace.",
|
250
|
+
"Please remove them before starting a study or put under the 'Study' context manager."
|
251
|
+
"Objects found:",
|
252
|
+
snapshot.edsl_objects,
|
253
|
+
)
|
254
|
+
return self
|
255
|
+
|
256
|
+
def __hash__(self) -> int:
|
257
|
+
return dict_hash(list(self.objects.keys()))
|
258
|
+
|
259
|
+
def study_diff(self):
|
260
|
+
## Need to also report missing.
|
261
|
+
from edsl.BaseDiff import BaseDiff
|
262
|
+
|
263
|
+
raise NotImplementedError("Need to implement this.")
|
264
|
+
|
265
|
+
def print(self):
|
266
|
+
from rich.console import Console
|
267
|
+
from rich.table import Table
|
268
|
+
|
269
|
+
console = Console()
|
270
|
+
table = Table(title="Study")
|
271
|
+
table.add_column("Original Name")
|
272
|
+
table.add_column("Study Name")
|
273
|
+
table.add_column("Class")
|
274
|
+
table.add_column("Description")
|
275
|
+
table.add_column("Hash")
|
276
|
+
table.add_column("Coop info")
|
277
|
+
table.add_column("Created")
|
278
|
+
|
279
|
+
for obj_hash, obj in self.objects.items():
|
280
|
+
url = (
|
281
|
+
""
|
282
|
+
if not hasattr(obj, "coop_info") or obj.coop_info is None
|
283
|
+
else obj.coop_info.get("url", "")
|
284
|
+
)
|
285
|
+
table.add_row(
|
286
|
+
obj.variable_name,
|
287
|
+
self.hash_to_name[obj_hash],
|
288
|
+
obj.edsl_class_name,
|
289
|
+
obj.description,
|
290
|
+
obj.hash,
|
291
|
+
url,
|
292
|
+
datetime.fromtimestamp(obj.created_at).strftime("%Y-%m-%d %H:%M:%S"),
|
293
|
+
)
|
294
|
+
# Add cache at the end
|
295
|
+
table.add_row(
|
296
|
+
"N/A - Study Cache",
|
297
|
+
"cache",
|
298
|
+
"Cache",
|
299
|
+
f"Cache of study, entries: {len(self.cache)}",
|
300
|
+
str(hash(self.cache)),
|
301
|
+
"N/A",
|
302
|
+
"N/A",
|
303
|
+
)
|
304
|
+
console.print(table)
|
305
|
+
|
306
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
307
|
+
snapshot = SnapShot(namespace=self.namespace, exclude=[self])
|
308
|
+
# print("Frame objects are:", snapshot.namespace.keys())
|
309
|
+
# breakpoint()
|
310
|
+
if self.use_study_cache:
|
311
|
+
unset_session_cache()
|
312
|
+
|
313
|
+
for variable_name, object in snapshot.edsl_objects.items():
|
314
|
+
self._add_edsl_object(object=object, variable_name=variable_name)
|
315
|
+
|
316
|
+
if not self.starting_objects:
|
317
|
+
if self.verbose:
|
318
|
+
print(f"New study saved to {self.filename}.json")
|
319
|
+
self.save()
|
320
|
+
|
321
|
+
if self.starting_objects and list(self.starting_objects.keys()) == list(
|
322
|
+
self.objects.keys()
|
323
|
+
):
|
324
|
+
if self.verbose:
|
325
|
+
print("Study perfectly replicated.")
|
326
|
+
else:
|
327
|
+
if self.verbose:
|
328
|
+
print("Starting hashes:", self.starting_objects.keys())
|
329
|
+
print("Current hashes:", self.objects.keys())
|
330
|
+
if self.starting_objects:
|
331
|
+
missing = set(self.starting_objects.keys()) - set(self.objects.keys())
|
332
|
+
added = set(self.objects.keys()) - set(self.starting_objects.keys())
|
333
|
+
# breakpoint()
|
334
|
+
print("Study did not perfectly replicate.")
|
335
|
+
for hash in missing:
|
336
|
+
print(f"Missing object: {self.starting_objects[hash]}")
|
337
|
+
for hash in added:
|
338
|
+
print(f"Added object: {self.objects[hash]}")
|
339
|
+
if self.overwrite_on_change:
|
340
|
+
print("Overwriting study file.")
|
341
|
+
self.save()
|
342
|
+
else:
|
343
|
+
print(
|
344
|
+
"Please save the study file with a new name or call study iwth 'overwrite_on_change=True' to overwrite the existing study file."
|
345
|
+
)
|
346
|
+
|
347
|
+
if self.coop:
|
348
|
+
self.push()
|
349
|
+
if self.overwrite_on_change:
|
350
|
+
self.save()
|
351
|
+
else:
|
352
|
+
raise ValueError(
|
353
|
+
"If you want to push to coop, you must save the study file with a new name or call study iwth 'overwrite_on_change=True' to overwrite the existing study file."
|
354
|
+
)
|
355
|
+
|
356
|
+
if self.verbose:
|
357
|
+
print("Objects in study now:")
|
358
|
+
self.print()
|
359
|
+
|
360
|
+
if self.proof_of_work_difficulty:
|
361
|
+
print("Adding proof of work to study...")
|
362
|
+
from edsl.study.ProofOfWork import ProofOfWork
|
363
|
+
|
364
|
+
# TODO: Need to check if hashes are the same.
|
365
|
+
if not self.proof_of_work.input_data:
|
366
|
+
self.proof_of_work.add_input_data(str(self.__hash__()))
|
367
|
+
self.proof_of_work.add_proof(self.proof_of_work_difficulty)
|
368
|
+
print(
|
369
|
+
"Proof of work added to study with difficulty ",
|
370
|
+
self.proof_of_work_difficulty,
|
371
|
+
)
|
372
|
+
print(self.proof_of_work)
|
373
|
+
self.save()
|
374
|
+
|
375
|
+
def to_dict(self):
|
376
|
+
return {
|
377
|
+
"name": self.name,
|
378
|
+
"description": self.description,
|
379
|
+
"objects": {hash: obj.to_dict() for hash, obj in self.objects.items()},
|
380
|
+
"filename": self.filename,
|
381
|
+
"cache": self.cache.to_dict(),
|
382
|
+
"use_study_cache": self.use_study_cache,
|
383
|
+
"overwrite_on_change": self.overwrite_on_change,
|
384
|
+
"proof_of_work": self.proof_of_work.to_dict(),
|
385
|
+
}
|
386
|
+
|
387
|
+
def versions(self):
|
388
|
+
"""Return a dictionary of objects grouped by variable name."""
|
389
|
+
d = {}
|
390
|
+
for _, obj_entry in self.objects.items():
|
391
|
+
if obj_entry.variable_name not in d:
|
392
|
+
d[obj_entry.variable_name] = [obj_entry]
|
393
|
+
else:
|
394
|
+
d[obj_entry.variable_name].append(obj_entry)
|
395
|
+
|
396
|
+
return d
|
397
|
+
|
398
|
+
def diff(self, variable_name: str, index1: int, index2: int):
|
399
|
+
"""Return the difference between the versions of an object."""
|
400
|
+
versions = self.versions()[variable_name]
|
401
|
+
diff = versions[index2].object - versions[index1].object
|
402
|
+
return diff
|
403
|
+
|
404
|
+
@classmethod
|
405
|
+
def example(cls, verbose=False):
|
406
|
+
import tempfile
|
407
|
+
|
408
|
+
study_file = tempfile.NamedTemporaryFile()
|
409
|
+
with cls(filename=study_file.name, verbose=verbose) as study:
|
410
|
+
from edsl import QuestionFreeText
|
411
|
+
|
412
|
+
q = QuestionFreeText.example()
|
413
|
+
return study
|
414
|
+
|
415
|
+
@classmethod
|
416
|
+
def from_dict(cls, d):
|
417
|
+
d["cache"] = Cache.from_dict(d["cache"])
|
418
|
+
d["objects"] = {
|
419
|
+
str(object_hash): ObjectEntry.from_dict(obj_dict)
|
420
|
+
for object_hash, obj_dict in d["objects"].items()
|
421
|
+
}
|
422
|
+
d["proof_of_work"] = ProofOfWork.from_dict(d["proof_of_work"])
|
423
|
+
return cls(**d)
|
424
|
+
|
425
|
+
def save(self):
|
426
|
+
if self.verbose:
|
427
|
+
print(f"Saving study to {self.filename}.json")
|
428
|
+
with open(self.filename + ".json", "w") as f:
|
429
|
+
json.dump(self.to_dict(), f, indent=4)
|
430
|
+
|
431
|
+
def _get_system_info(self):
|
432
|
+
return {
|
433
|
+
"platform": platform.platform(),
|
434
|
+
"system": platform.system(),
|
435
|
+
"processor": platform.processor(),
|
436
|
+
"hostname": socket.gethostname(),
|
437
|
+
}
|
438
|
+
|
439
|
+
@staticmethod
|
440
|
+
def _get_description(object):
|
441
|
+
text = ""
|
442
|
+
if hasattr(object, "__len__"):
|
443
|
+
text += f"Num. entries: {len(object)}"
|
444
|
+
if hasattr(object, "question_name"):
|
445
|
+
text += f"Question name: {object.question_name}"
|
446
|
+
return text
|
447
|
+
|
448
|
+
def _add_edsl_object(self, object, variable_name, description=None) -> None:
|
449
|
+
"""
|
450
|
+
>>> s = Study.example()
|
451
|
+
>>> from edsl import QuestionLinearScale
|
452
|
+
>>> s._add_edsl_object(QuestionLinearScale.example(), 'q')
|
453
|
+
>>> len(s)
|
454
|
+
2
|
455
|
+
"""
|
456
|
+
if description is None:
|
457
|
+
description = self._get_description(object)
|
458
|
+
oe = ObjectEntry(
|
459
|
+
variable_name=variable_name, object=object, description=description
|
460
|
+
)
|
461
|
+
if oe.hash in self.objects:
|
462
|
+
return
|
463
|
+
else:
|
464
|
+
self.objects[oe.hash] = oe
|
465
|
+
|
466
|
+
def push(self, refresh=False) -> None:
|
467
|
+
"""Push the objects to coop."""
|
468
|
+
|
469
|
+
from edsl import Coop
|
470
|
+
|
471
|
+
coop = Coop()
|
472
|
+
coop.create(self, description=self.description)
|
473
|
+
|
474
|
+
@classmethod
|
475
|
+
def pull(cls, uuid: Optional[Union[str, UUID]] = None, url: Optional[str] = None):
|
476
|
+
"""Pull the object from coop."""
|
477
|
+
from edsl.coop import Coop
|
478
|
+
|
479
|
+
coop = Coop()
|
480
|
+
return coop.get(uuid, url, "study")
|
481
|
+
|
482
|
+
def __repr__(self):
|
483
|
+
return f"""Study(name = "{self.name}", description = "{self.description}", objects = {self.objects}, cache = {self.cache}, filename = "{self.filename}", coop = {self.coop}, use_study_cache = {self.use_study_cache}, overwrite_on_change = {self.overwrite_on_change})"""
|
484
|
+
|
485
|
+
|
486
|
+
if __name__ == "__main__":
|
487
|
+
import doctest
|
488
|
+
|
489
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
490
|
+
|
491
|
+
# with Study(name = "cool_study") as study:
|
492
|
+
# from edsl import QuestionFreeText
|
493
|
+
# q = QuestionFreeText.example()
|
494
|
+
|
495
|
+
# assert len(study.objects) == 1
|
496
|
+
|
497
|
+
# print(study.versions())
|
498
|
+
# {'q': [ObjectEntry(variable_name='q', object=Question('free_text', question_name = """how_are_you""", question_text = """How are you?"""), description='Question name: how_are_you', coop_info=None, created_at=1720276402.561273, edsl_class_name='QuestionFreeText')]}
|
edsl/study/__init__.py
ADDED
edsl/surveys/MemoryPlan.py
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
from collections import UserDict, defaultdict
|
4
4
|
from typing import Optional
|
5
5
|
|
6
|
-
from edsl.surveys.Memory import Memory
|
7
|
-
from edsl.prompts.Prompt import Prompt
|
8
|
-
from edsl.surveys.DAG import DAG
|
6
|
+
# from edsl.surveys.Memory import Memory
|
7
|
+
# from edsl.prompts.Prompt import Prompt
|
8
|
+
# from edsl.surveys.DAG import DAG
|
9
9
|
|
10
10
|
|
11
11
|
class MemoryPlan(UserDict):
|
@@ -61,6 +61,8 @@ class MemoryPlan(UserDict):
|
|
61
61
|
:param answers: A dictionary of question names to answers.
|
62
62
|
|
63
63
|
"""
|
64
|
+
from edsl.prompts.Prompt import Prompt
|
65
|
+
|
64
66
|
self._check_valid_question_name(focal_question)
|
65
67
|
|
66
68
|
if focal_question not in self:
|
@@ -121,6 +123,7 @@ class MemoryPlan(UserDict):
|
|
121
123
|
self._check_valid_question_name(focal_question)
|
122
124
|
self._check_valid_question_name(prior_question)
|
123
125
|
self._check_order(focal_question, prior_question)
|
126
|
+
from edsl.surveys.Memory import Memory
|
124
127
|
|
125
128
|
if focal_question not in self:
|
126
129
|
memory = Memory()
|
@@ -160,6 +163,8 @@ class MemoryPlan(UserDict):
|
|
160
163
|
@classmethod
|
161
164
|
def from_dict(cls, data) -> "MemoryPlan":
|
162
165
|
"""Deserialize a memory plan from a dictionary."""
|
166
|
+
from edsl.surveys.Memory import Memory
|
167
|
+
|
163
168
|
newdata = {}
|
164
169
|
for question_name, memory in data["data"].items():
|
165
170
|
newdata[question_name] = Memory.from_dict(memory)
|
@@ -182,13 +187,15 @@ class MemoryPlan(UserDict):
|
|
182
187
|
return new_d
|
183
188
|
|
184
189
|
@property
|
185
|
-
def dag(self) -> DAG:
|
190
|
+
def dag(self) -> "DAG":
|
186
191
|
"""Return a directed acyclic graph of the memory plan.
|
187
192
|
|
188
193
|
>>> mp = MemoryPlan.example()
|
189
194
|
>>> mp.dag
|
190
195
|
{1: {0}}
|
191
196
|
"""
|
197
|
+
from edsl.surveys.DAG import DAG
|
198
|
+
|
192
199
|
d = defaultdict(set)
|
193
200
|
for focal_question, memory in self.items():
|
194
201
|
for prior_question in memory:
|