edsl 0.1.48__py3-none-any.whl → 0.1.50__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/__init__.py +124 -53
- edsl/__version__.py +1 -1
- edsl/agents/agent.py +21 -21
- edsl/agents/agent_list.py +2 -5
- edsl/agents/exceptions.py +119 -5
- edsl/base/__init__.py +10 -35
- edsl/base/base_class.py +71 -36
- edsl/base/base_exception.py +204 -0
- edsl/base/data_transfer_models.py +1 -1
- edsl/base/exceptions.py +94 -0
- edsl/buckets/__init__.py +15 -1
- edsl/buckets/bucket_collection.py +3 -4
- edsl/buckets/exceptions.py +75 -0
- edsl/buckets/model_buckets.py +1 -2
- edsl/buckets/token_bucket.py +11 -6
- edsl/buckets/token_bucket_api.py +1 -2
- edsl/buckets/token_bucket_client.py +9 -7
- edsl/caching/cache.py +7 -2
- edsl/caching/cache_entry.py +10 -9
- edsl/caching/exceptions.py +113 -7
- edsl/caching/remote_cache_sync.py +1 -2
- edsl/caching/sql_dict.py +17 -12
- edsl/cli.py +43 -0
- edsl/config/config_class.py +30 -6
- edsl/conversation/Conversation.py +3 -2
- edsl/conversation/exceptions.py +58 -0
- edsl/conversation/mug_negotiation.py +0 -2
- edsl/coop/__init__.py +20 -1
- edsl/coop/coop.py +129 -38
- edsl/coop/exceptions.py +188 -9
- edsl/coop/price_fetcher.py +3 -6
- edsl/coop/utils.py +4 -6
- edsl/dataset/__init__.py +5 -4
- edsl/dataset/dataset.py +53 -43
- edsl/dataset/dataset_operations_mixin.py +86 -72
- edsl/dataset/dataset_tree.py +9 -5
- edsl/dataset/display/table_display.py +0 -2
- edsl/dataset/display/table_renderers.py +0 -1
- edsl/dataset/exceptions.py +125 -0
- edsl/dataset/file_exports.py +18 -11
- edsl/dataset/r/ggplot.py +13 -6
- edsl/display/__init__.py +27 -0
- edsl/display/core.py +147 -0
- edsl/display/plugin.py +189 -0
- edsl/display/utils.py +52 -0
- edsl/inference_services/__init__.py +9 -1
- edsl/inference_services/available_model_cache_handler.py +1 -1
- edsl/inference_services/available_model_fetcher.py +4 -5
- edsl/inference_services/data_structures.py +9 -6
- edsl/inference_services/exceptions.py +132 -1
- edsl/inference_services/inference_service_abc.py +2 -2
- edsl/inference_services/inference_services_collection.py +2 -6
- edsl/inference_services/registry.py +4 -3
- edsl/inference_services/service_availability.py +2 -1
- edsl/inference_services/services/anthropic_service.py +4 -1
- edsl/inference_services/services/aws_bedrock.py +13 -12
- edsl/inference_services/services/azure_ai.py +12 -10
- edsl/inference_services/services/deep_infra_service.py +1 -4
- edsl/inference_services/services/deep_seek_service.py +1 -5
- edsl/inference_services/services/google_service.py +6 -2
- edsl/inference_services/services/groq_service.py +1 -1
- edsl/inference_services/services/mistral_ai_service.py +4 -2
- edsl/inference_services/services/ollama_service.py +1 -1
- edsl/inference_services/services/open_ai_service.py +7 -5
- edsl/inference_services/services/perplexity_service.py +6 -2
- edsl/inference_services/services/test_service.py +8 -7
- edsl/inference_services/services/together_ai_service.py +2 -3
- edsl/inference_services/services/xai_service.py +1 -1
- edsl/instructions/__init__.py +1 -1
- edsl/instructions/change_instruction.py +3 -2
- edsl/instructions/exceptions.py +61 -0
- edsl/instructions/instruction.py +5 -2
- edsl/instructions/instruction_collection.py +2 -1
- edsl/instructions/instruction_handler.py +4 -9
- edsl/interviews/ReportErrors.py +0 -3
- edsl/interviews/__init__.py +9 -2
- edsl/interviews/answering_function.py +11 -13
- edsl/interviews/exception_tracking.py +14 -7
- edsl/interviews/exceptions.py +79 -0
- edsl/interviews/interview.py +32 -29
- edsl/interviews/interview_status_dictionary.py +4 -2
- edsl/interviews/interview_status_log.py +2 -1
- edsl/interviews/interview_task_manager.py +3 -3
- edsl/interviews/request_token_estimator.py +3 -1
- edsl/interviews/statistics.py +2 -3
- edsl/invigilators/__init__.py +7 -1
- edsl/invigilators/exceptions.py +79 -0
- edsl/invigilators/invigilator_base.py +0 -1
- edsl/invigilators/invigilators.py +8 -12
- edsl/invigilators/prompt_constructor.py +1 -5
- edsl/invigilators/prompt_helpers.py +8 -4
- edsl/invigilators/question_instructions_prompt_builder.py +1 -1
- edsl/invigilators/question_option_processor.py +9 -5
- edsl/invigilators/question_template_replacements_builder.py +3 -2
- edsl/jobs/__init__.py +3 -3
- edsl/jobs/async_interview_runner.py +24 -22
- edsl/jobs/check_survey_scenario_compatibility.py +7 -6
- edsl/jobs/data_structures.py +7 -4
- edsl/jobs/exceptions.py +177 -8
- edsl/jobs/fetch_invigilator.py +1 -1
- edsl/jobs/jobs.py +72 -67
- edsl/jobs/jobs_checks.py +2 -3
- edsl/jobs/jobs_component_constructor.py +2 -2
- edsl/jobs/jobs_pricing_estimation.py +3 -2
- edsl/jobs/jobs_remote_inference_logger.py +5 -4
- edsl/jobs/jobs_runner_asyncio.py +1 -2
- edsl/jobs/jobs_runner_status.py +8 -9
- edsl/jobs/remote_inference.py +26 -23
- edsl/jobs/results_exceptions_handler.py +8 -5
- edsl/key_management/__init__.py +3 -1
- edsl/key_management/exceptions.py +62 -0
- edsl/key_management/key_lookup.py +1 -1
- edsl/key_management/key_lookup_builder.py +37 -14
- edsl/key_management/key_lookup_collection.py +2 -0
- edsl/language_models/__init__.py +1 -1
- edsl/language_models/exceptions.py +302 -14
- edsl/language_models/language_model.py +4 -7
- edsl/language_models/model.py +4 -4
- edsl/language_models/model_list.py +1 -1
- edsl/language_models/price_manager.py +1 -1
- edsl/language_models/raw_response_handler.py +14 -9
- edsl/language_models/registry.py +17 -21
- edsl/language_models/repair.py +0 -6
- edsl/language_models/unused/fake_openai_service.py +0 -1
- edsl/load_plugins.py +69 -0
- edsl/logger.py +146 -0
- edsl/notebooks/notebook.py +1 -1
- edsl/notebooks/notebook_to_latex.py +0 -1
- edsl/plugins/__init__.py +63 -0
- edsl/plugins/built_in/export_example.py +50 -0
- edsl/plugins/built_in/pig_latin.py +67 -0
- edsl/plugins/cli.py +372 -0
- edsl/plugins/cli_typer.py +283 -0
- edsl/plugins/exceptions.py +31 -0
- edsl/plugins/hookspec.py +51 -0
- edsl/plugins/plugin_host.py +128 -0
- edsl/plugins/plugin_manager.py +633 -0
- edsl/plugins/plugins_registry.py +168 -0
- edsl/prompts/__init__.py +2 -0
- edsl/prompts/exceptions.py +107 -5
- edsl/prompts/prompt.py +14 -6
- edsl/questions/HTMLQuestion.py +5 -11
- edsl/questions/Quick.py +0 -1
- edsl/questions/__init__.py +2 -0
- edsl/questions/answer_validator_mixin.py +318 -318
- edsl/questions/compose_questions.py +2 -2
- edsl/questions/descriptors.py +10 -49
- edsl/questions/exceptions.py +278 -22
- edsl/questions/loop_processor.py +7 -5
- edsl/questions/prompt_templates/question_list.jinja +3 -0
- edsl/questions/question_base.py +14 -16
- edsl/questions/question_base_gen_mixin.py +2 -2
- edsl/questions/question_base_prompts_mixin.py +9 -3
- edsl/questions/question_budget.py +9 -5
- edsl/questions/question_check_box.py +3 -5
- edsl/questions/question_dict.py +171 -194
- edsl/questions/question_extract.py +1 -1
- edsl/questions/question_free_text.py +4 -6
- edsl/questions/question_functional.py +4 -3
- edsl/questions/question_list.py +36 -9
- edsl/questions/question_matrix.py +95 -61
- edsl/questions/question_multiple_choice.py +6 -4
- edsl/questions/question_numerical.py +2 -4
- edsl/questions/question_registry.py +4 -2
- edsl/questions/register_questions_meta.py +0 -1
- edsl/questions/response_validator_abc.py +7 -13
- edsl/questions/templates/dict/answering_instructions.jinja +1 -0
- edsl/questions/templates/rank/question_presentation.jinja +1 -1
- edsl/results/__init__.py +1 -1
- edsl/results/exceptions.py +141 -7
- edsl/results/report.py +0 -1
- edsl/results/result.py +4 -5
- edsl/results/results.py +10 -51
- edsl/results/results_selector.py +8 -4
- edsl/scenarios/PdfExtractor.py +2 -2
- edsl/scenarios/construct_download_link.py +69 -35
- edsl/scenarios/directory_scanner.py +33 -14
- edsl/scenarios/document_chunker.py +1 -1
- edsl/scenarios/exceptions.py +238 -14
- edsl/scenarios/file_methods.py +1 -1
- edsl/scenarios/file_store.py +7 -3
- edsl/scenarios/handlers/__init__.py +17 -0
- edsl/scenarios/handlers/docx_file_store.py +0 -5
- edsl/scenarios/handlers/pdf_file_store.py +0 -1
- edsl/scenarios/handlers/pptx_file_store.py +0 -5
- edsl/scenarios/handlers/py_file_store.py +0 -1
- edsl/scenarios/handlers/sql_file_store.py +1 -4
- edsl/scenarios/handlers/sqlite_file_store.py +0 -1
- edsl/scenarios/handlers/txt_file_store.py +1 -1
- edsl/scenarios/scenario.py +0 -1
- edsl/scenarios/scenario_list.py +152 -18
- edsl/scenarios/scenario_list_pdf_tools.py +1 -0
- edsl/scenarios/scenario_selector.py +0 -1
- edsl/surveys/__init__.py +3 -4
- edsl/surveys/dag/__init__.py +4 -2
- edsl/surveys/descriptors.py +1 -1
- edsl/surveys/edit_survey.py +1 -0
- edsl/surveys/exceptions.py +165 -9
- edsl/surveys/memory/__init__.py +5 -3
- edsl/surveys/memory/memory_management.py +1 -0
- edsl/surveys/memory/memory_plan.py +6 -15
- edsl/surveys/rules/__init__.py +5 -3
- edsl/surveys/rules/rule.py +1 -2
- edsl/surveys/rules/rule_collection.py +1 -1
- edsl/surveys/survey.py +12 -24
- edsl/surveys/survey_export.py +6 -3
- edsl/surveys/survey_flow_visualization.py +10 -1
- edsl/tasks/__init__.py +2 -0
- edsl/tasks/question_task_creator.py +3 -3
- edsl/tasks/task_creators.py +1 -3
- edsl/tasks/task_history.py +5 -7
- edsl/tasks/task_status_log.py +1 -2
- edsl/tokens/__init__.py +3 -1
- edsl/tokens/token_usage.py +1 -1
- edsl/utilities/__init__.py +21 -1
- edsl/utilities/decorators.py +1 -2
- edsl/utilities/markdown_to_docx.py +2 -2
- edsl/utilities/markdown_to_pdf.py +1 -1
- edsl/utilities/repair_functions.py +0 -1
- edsl/utilities/restricted_python.py +0 -1
- edsl/utilities/template_loader.py +2 -3
- edsl/utilities/utilities.py +8 -29
- {edsl-0.1.48.dist-info → edsl-0.1.50.dist-info}/METADATA +32 -2
- edsl-0.1.50.dist-info/RECORD +363 -0
- edsl-0.1.50.dist-info/entry_points.txt +3 -0
- edsl/dataset/smart_objects.py +0 -96
- edsl/exceptions/BaseException.py +0 -21
- edsl/exceptions/__init__.py +0 -54
- edsl/exceptions/configuration.py +0 -16
- edsl/exceptions/general.py +0 -34
- edsl/study/ObjectEntry.py +0 -173
- edsl/study/ProofOfWork.py +0 -113
- edsl/study/SnapShot.py +0 -80
- edsl/study/Study.py +0 -520
- edsl/study/__init__.py +0 -6
- edsl/utilities/interface.py +0 -135
- edsl-0.1.48.dist-info/RECORD +0 -347
- {edsl-0.1.48.dist-info → edsl-0.1.50.dist-info}/LICENSE +0 -0
- {edsl-0.1.48.dist-info → edsl-0.1.50.dist-info}/WHEEL +0 -0
edsl/study/Study.py
DELETED
@@ -1,520 +0,0 @@
|
|
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 uuid import UUID, uuid4
|
10
|
-
|
11
|
-
from ..caching import Cache
|
12
|
-
#from edsl import set_session_cache, unset_session_cache
|
13
|
-
from ..utilities.utilities import dict_hash
|
14
|
-
from ..study.ObjectEntry import ObjectEntry
|
15
|
-
from ..study.ProofOfWork import ProofOfWork
|
16
|
-
from ..study.SnapShot import SnapShot
|
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
|
-
>>> #_ = s.__exit__(None, None, None)
|
232
|
-
>>> #len(s.objects)
|
233
|
-
>>> #os.remove("temp.json")
|
234
|
-
|
235
|
-
"""
|
236
|
-
if self.verbose:
|
237
|
-
print("Existing objects in study:")
|
238
|
-
self.print()
|
239
|
-
snapshot = SnapShot(self.namespace, exclude=[self])
|
240
|
-
if self.use_study_cache:
|
241
|
-
if self.verbose:
|
242
|
-
print("Using study cache.")
|
243
|
-
#set_session_cache(self.cache)
|
244
|
-
|
245
|
-
if snapshot.edsl_objects:
|
246
|
-
raise ValueError(
|
247
|
-
"You have EDSL objects in the global namespace.",
|
248
|
-
"Please remove them before starting a study or put under the 'Study' context manager."
|
249
|
-
"Objects found:",
|
250
|
-
snapshot.edsl_objects,
|
251
|
-
)
|
252
|
-
return self
|
253
|
-
|
254
|
-
def __hash__(self) -> int:
|
255
|
-
return dict_hash(list(self.objects.keys()))
|
256
|
-
|
257
|
-
def study_diff(self):
|
258
|
-
## Need to also report missing.
|
259
|
-
from ..base import BaseDiff
|
260
|
-
|
261
|
-
raise NotImplementedError("Need to implement this.")
|
262
|
-
|
263
|
-
def print(self):
|
264
|
-
from rich.console import Console
|
265
|
-
from rich.table import Table
|
266
|
-
|
267
|
-
console = Console()
|
268
|
-
table = Table(title="Study")
|
269
|
-
table.add_column("Original Name")
|
270
|
-
table.add_column("Study Name")
|
271
|
-
table.add_column("Class")
|
272
|
-
table.add_column("Description")
|
273
|
-
table.add_column("Hash")
|
274
|
-
table.add_column("Coop info")
|
275
|
-
table.add_column("Created")
|
276
|
-
|
277
|
-
for obj_hash, obj in self.objects.items():
|
278
|
-
url = (
|
279
|
-
""
|
280
|
-
if not hasattr(obj, "coop_info") or obj.coop_info is None
|
281
|
-
else obj.coop_info.get("url", "")
|
282
|
-
)
|
283
|
-
table.add_row(
|
284
|
-
obj.variable_name,
|
285
|
-
self.hash_to_name[obj_hash],
|
286
|
-
obj.edsl_class_name,
|
287
|
-
obj.description,
|
288
|
-
obj.hash,
|
289
|
-
url,
|
290
|
-
datetime.fromtimestamp(obj.created_at).strftime("%Y-%m-%d %H:%M:%S"),
|
291
|
-
)
|
292
|
-
# Add cache at the end
|
293
|
-
table.add_row(
|
294
|
-
"N/A - Study Cache",
|
295
|
-
"cache",
|
296
|
-
"Cache",
|
297
|
-
f"Cache of study, entries: {len(self.cache)}",
|
298
|
-
str(hash(self.cache)),
|
299
|
-
"N/A",
|
300
|
-
"N/A",
|
301
|
-
)
|
302
|
-
console.print(table)
|
303
|
-
|
304
|
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
305
|
-
snapshot = SnapShot(namespace=self.namespace, exclude=[self])
|
306
|
-
# print("Frame objects are:", snapshot.namespace.keys())
|
307
|
-
# breakpoint()
|
308
|
-
if self.use_study_cache:
|
309
|
-
#unset_session_cache()
|
310
|
-
pass
|
311
|
-
|
312
|
-
for variable_name, object in snapshot.edsl_objects.items():
|
313
|
-
self._add_edsl_object(object=object, variable_name=variable_name)
|
314
|
-
|
315
|
-
if not self.starting_objects:
|
316
|
-
if self.verbose:
|
317
|
-
print(f"New study saved to {self.filename}.json")
|
318
|
-
self.save()
|
319
|
-
|
320
|
-
if self.starting_objects and list(self.starting_objects.keys()) == list(
|
321
|
-
self.objects.keys()
|
322
|
-
):
|
323
|
-
if self.verbose:
|
324
|
-
print("Study perfectly replicated.")
|
325
|
-
else:
|
326
|
-
if self.verbose:
|
327
|
-
print("Starting hashes:", self.starting_objects.keys())
|
328
|
-
print("Current hashes:", self.objects.keys())
|
329
|
-
if self.starting_objects:
|
330
|
-
missing = set(self.starting_objects.keys()) - set(self.objects.keys())
|
331
|
-
added = set(self.objects.keys()) - set(self.starting_objects.keys())
|
332
|
-
# breakpoint()
|
333
|
-
print("Study did not perfectly replicate.")
|
334
|
-
for hash in missing:
|
335
|
-
print(f"Missing object: {self.starting_objects[hash]}")
|
336
|
-
for hash in added:
|
337
|
-
print(f"Added object: {self.objects[hash]}")
|
338
|
-
if self.overwrite_on_change:
|
339
|
-
print("Overwriting study file.")
|
340
|
-
self.save()
|
341
|
-
else:
|
342
|
-
print(
|
343
|
-
"Please save the study file with a new name or call study iwth 'overwrite_on_change=True' to overwrite the existing study file."
|
344
|
-
)
|
345
|
-
|
346
|
-
if self.coop:
|
347
|
-
self.push()
|
348
|
-
if self.overwrite_on_change:
|
349
|
-
self.save()
|
350
|
-
else:
|
351
|
-
raise ValueError(
|
352
|
-
"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."
|
353
|
-
)
|
354
|
-
|
355
|
-
if self.verbose:
|
356
|
-
print("Objects in study now:")
|
357
|
-
self.print()
|
358
|
-
|
359
|
-
if self.proof_of_work_difficulty:
|
360
|
-
print("Adding proof of work to study...")
|
361
|
-
from edsl.study.ProofOfWork import ProofOfWork
|
362
|
-
|
363
|
-
# TODO: Need to check if hashes are the same.
|
364
|
-
if not self.proof_of_work.input_data:
|
365
|
-
self.proof_of_work.add_input_data(str(self.__hash__()))
|
366
|
-
self.proof_of_work.add_proof(self.proof_of_work_difficulty)
|
367
|
-
print(
|
368
|
-
"Proof of work added to study with difficulty ",
|
369
|
-
self.proof_of_work_difficulty,
|
370
|
-
)
|
371
|
-
print(self.proof_of_work)
|
372
|
-
self.save()
|
373
|
-
|
374
|
-
def to_dict(self):
|
375
|
-
return {
|
376
|
-
"name": self.name,
|
377
|
-
"description": self.description,
|
378
|
-
"objects": {hash: obj.to_dict() for hash, obj in self.objects.items()},
|
379
|
-
"filename": self.filename,
|
380
|
-
"cache": self.cache.to_dict(),
|
381
|
-
"use_study_cache": self.use_study_cache,
|
382
|
-
"overwrite_on_change": self.overwrite_on_change,
|
383
|
-
"proof_of_work": self.proof_of_work.to_dict(),
|
384
|
-
}
|
385
|
-
|
386
|
-
def versions(self):
|
387
|
-
"""Return a dictionary of objects grouped by variable name."""
|
388
|
-
d = {}
|
389
|
-
for _, obj_entry in self.objects.items():
|
390
|
-
if obj_entry.variable_name not in d:
|
391
|
-
d[obj_entry.variable_name] = [obj_entry]
|
392
|
-
else:
|
393
|
-
d[obj_entry.variable_name].append(obj_entry)
|
394
|
-
|
395
|
-
return d
|
396
|
-
|
397
|
-
def diff(self, variable_name: str, index1: int, index2: int):
|
398
|
-
"""Return the difference between the versions of an object."""
|
399
|
-
versions = self.versions()[variable_name]
|
400
|
-
diff = versions[index2].object - versions[index1].object
|
401
|
-
return diff
|
402
|
-
|
403
|
-
@classmethod
|
404
|
-
def example(cls, verbose=False, randomize=False):
|
405
|
-
import tempfile
|
406
|
-
|
407
|
-
study_file = tempfile.NamedTemporaryFile()
|
408
|
-
with cls(filename=study_file.name, verbose=verbose) as study:
|
409
|
-
from ..questions import QuestionFreeText
|
410
|
-
|
411
|
-
q = QuestionFreeText.example(randomize=randomize)
|
412
|
-
return study
|
413
|
-
|
414
|
-
@classmethod
|
415
|
-
def from_dict(cls, d):
|
416
|
-
d["cache"] = Cache.from_dict(d["cache"])
|
417
|
-
d["objects"] = {
|
418
|
-
str(object_hash): ObjectEntry.from_dict(obj_dict)
|
419
|
-
for object_hash, obj_dict in d["objects"].items()
|
420
|
-
}
|
421
|
-
d["proof_of_work"] = ProofOfWork.from_dict(d["proof_of_work"])
|
422
|
-
return cls(**d)
|
423
|
-
|
424
|
-
def save(self):
|
425
|
-
if self.verbose:
|
426
|
-
print(f"Saving study to {self.filename}.json")
|
427
|
-
with open(self.filename + ".json", "w") as f:
|
428
|
-
json.dump(self.to_dict(), f, indent=4)
|
429
|
-
|
430
|
-
def _get_system_info(self):
|
431
|
-
return {
|
432
|
-
"platform": platform.platform(),
|
433
|
-
"system": platform.system(),
|
434
|
-
"processor": platform.processor(),
|
435
|
-
"hostname": socket.gethostname(),
|
436
|
-
}
|
437
|
-
|
438
|
-
@staticmethod
|
439
|
-
def _get_description(object):
|
440
|
-
text = ""
|
441
|
-
if hasattr(object, "__len__"):
|
442
|
-
text += f"Num. entries: {len(object)}"
|
443
|
-
if hasattr(object, "question_name"):
|
444
|
-
text += f"Question name: {object.question_name}"
|
445
|
-
return text
|
446
|
-
|
447
|
-
def _add_edsl_object(self, object, variable_name, description=None) -> None:
|
448
|
-
"""
|
449
|
-
>>> s = Study.example()
|
450
|
-
>>> from edsl import QuestionLinearScale
|
451
|
-
>>> s._add_edsl_object(QuestionLinearScale.example(), 'q')
|
452
|
-
>>> len(s)
|
453
|
-
2
|
454
|
-
"""
|
455
|
-
if description is None:
|
456
|
-
description = self._get_description(object)
|
457
|
-
oe = ObjectEntry(
|
458
|
-
variable_name=variable_name, object=object, description=description
|
459
|
-
)
|
460
|
-
if oe.hash in self.objects:
|
461
|
-
return
|
462
|
-
else:
|
463
|
-
self.objects[oe.hash] = oe
|
464
|
-
|
465
|
-
def push(self) -> dict:
|
466
|
-
"""Push the objects to coop."""
|
467
|
-
|
468
|
-
from edsl.coop.coop import Coop
|
469
|
-
|
470
|
-
coop = Coop()
|
471
|
-
return coop.create(self, description=self.description)
|
472
|
-
|
473
|
-
def delete_object(self, identifier: Union[str, UUID]):
|
474
|
-
"""
|
475
|
-
Delete an EDSL object from the study.
|
476
|
-
|
477
|
-
:param identifier: Either the variable name or the hash of the object to delete
|
478
|
-
:raises ValueError: If the object is not found in the study
|
479
|
-
"""
|
480
|
-
if isinstance(identifier, str):
|
481
|
-
# If identifier is a variable name or a string representation of UUID
|
482
|
-
for hash, obj_entry in list(self.objects.items()):
|
483
|
-
if obj_entry.variable_name == identifier or hash == identifier:
|
484
|
-
del self.objects[hash]
|
485
|
-
self._create_mapping_dicts() # Update internal mappings
|
486
|
-
if self.verbose:
|
487
|
-
print(f"Deleted object with identifier: {identifier}")
|
488
|
-
return
|
489
|
-
raise ValueError(f"No object found with identifier: {identifier}")
|
490
|
-
elif isinstance(identifier, UUID):
|
491
|
-
# If identifier is a UUID object
|
492
|
-
hash_str = str(identifier)
|
493
|
-
if hash_str in self.objects:
|
494
|
-
del self.objects[hash_str]
|
495
|
-
self._create_mapping_dicts() # Update internal mappings
|
496
|
-
if self.verbose:
|
497
|
-
print(f"Deleted object with hash: {hash_str}")
|
498
|
-
return
|
499
|
-
raise ValueError(f"No object found with hash: {hash_str}")
|
500
|
-
else:
|
501
|
-
raise TypeError(
|
502
|
-
"Identifier must be either a string (variable name or hash) or a UUID object"
|
503
|
-
)
|
504
|
-
|
505
|
-
@classmethod
|
506
|
-
def pull(cls, url_or_uuid: Union[str, UUID]):
|
507
|
-
"""Pull the object from coop."""
|
508
|
-
from edsl.coop import Coop
|
509
|
-
|
510
|
-
coop = Coop()
|
511
|
-
return coop.get(url_or_uuid, "study")
|
512
|
-
|
513
|
-
def __repr__(self):
|
514
|
-
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})"""
|
515
|
-
|
516
|
-
|
517
|
-
if __name__ == "__main__":
|
518
|
-
import doctest
|
519
|
-
|
520
|
-
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
edsl/study/__init__.py
DELETED
edsl/utilities/interface.py
DELETED
@@ -1,135 +0,0 @@
|
|
1
|
-
"""A module for displaying data in various formats.
|
2
|
-
|
3
|
-
This module provides utility functions for formatting and displaying data,
|
4
|
-
primarily used by the Results module for printing results.
|
5
|
-
"""
|
6
|
-
|
7
|
-
# Only print_results_long is actively used in the codebase (in results.py)
|
8
|
-
# The rest of the functions are kept for reference but are not imported in __init__.py
|
9
|
-
|
10
|
-
|
11
|
-
def print_results_long(results, max_rows=None):
|
12
|
-
"""
|
13
|
-
Format results data as a rich console table with columns for index, key, and value.
|
14
|
-
|
15
|
-
Args:
|
16
|
-
results: The Results object to display
|
17
|
-
max_rows: Optional maximum number of rows to display
|
18
|
-
"""
|
19
|
-
from rich.console import Console
|
20
|
-
from rich.table import Table
|
21
|
-
|
22
|
-
console = Console(record=True)
|
23
|
-
table = Table(show_header=True, header_style="bold magenta")
|
24
|
-
table.add_column("Result index", style="dim")
|
25
|
-
table.add_column("Key", style="dim")
|
26
|
-
table.add_column("Value", style="dim")
|
27
|
-
list_of_dicts = results.to_dicts()
|
28
|
-
num_rows = 0
|
29
|
-
for i, results_dict in enumerate(list_of_dicts):
|
30
|
-
for key, value in results_dict.items():
|
31
|
-
table.add_row(str(i), key, str(value))
|
32
|
-
num_rows += 1
|
33
|
-
if max_rows is not None and num_rows >= max_rows:
|
34
|
-
break
|
35
|
-
console.print(table)
|
36
|
-
|
37
|
-
|
38
|
-
# The rest of these functions are not actively used by the codebase
|
39
|
-
# They are kept but commented out for potential future reference
|
40
|
-
|
41
|
-
"""
|
42
|
-
def create_image(console, image_filename):
|
43
|
-
font_size = 15
|
44
|
-
from PIL import Image, ImageDraw, ImageFont
|
45
|
-
|
46
|
-
text = console.export_text()
|
47
|
-
|
48
|
-
font_size = 15
|
49
|
-
font = ImageFont.load_default()
|
50
|
-
text_width, text_height = get_multiline_textsize(text, font)
|
51
|
-
image = Image.new(
|
52
|
-
"RGB", (text_width + 20, text_height + 20), color=(255, 255, 255)
|
53
|
-
)
|
54
|
-
d = ImageDraw.Draw(image)
|
55
|
-
d.multiline_text((10, 10), text, font=font, fill=(0, 0, 0))
|
56
|
-
image.save(image_filename)
|
57
|
-
|
58
|
-
|
59
|
-
def display_table(console, table, filename):
|
60
|
-
if filename is not None:
|
61
|
-
with open(filename, "w") as f:
|
62
|
-
with console.capture() as capture:
|
63
|
-
console.print(table)
|
64
|
-
f.write(capture.get())
|
65
|
-
create_image(console, filename + ".png")
|
66
|
-
else:
|
67
|
-
console.print(table)
|
68
|
-
|
69
|
-
|
70
|
-
def gen_html_sandwich(html_inner, interactive=False):
|
71
|
-
return html_inner
|
72
|
-
|
73
|
-
|
74
|
-
def view_html(html):
|
75
|
-
import tempfile
|
76
|
-
import webbrowser
|
77
|
-
|
78
|
-
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".html") as f:
|
79
|
-
url = "file://" + f.name
|
80
|
-
f.write(html)
|
81
|
-
webbrowser.open(url)
|
82
|
-
|
83
|
-
|
84
|
-
def get_multiline_textsize(text, font):
|
85
|
-
lines = text.split("\n")
|
86
|
-
max_width = 0
|
87
|
-
total_height = 0
|
88
|
-
|
89
|
-
for line in lines:
|
90
|
-
box = font.getbbox(line)
|
91
|
-
width, height = box[2], box[3]
|
92
|
-
max_width = max(max_width, width)
|
93
|
-
total_height += height
|
94
|
-
|
95
|
-
return max_width, total_height
|
96
|
-
|
97
|
-
|
98
|
-
def print_dict_with_rich(d, key_name="Key", value_name="Value", filename=None):
|
99
|
-
from rich.console import Console
|
100
|
-
from rich.table import Table
|
101
|
-
|
102
|
-
console = Console(record=True)
|
103
|
-
table = Table(show_header=True, header_style="bold magenta")
|
104
|
-
table.add_column(key_name, style="dim")
|
105
|
-
table.add_column(value_name, style="dim")
|
106
|
-
for key, value in d.items():
|
107
|
-
table.add_row(key, str(value))
|
108
|
-
console.print(table)
|
109
|
-
|
110
|
-
|
111
|
-
def print_table_with_rich(data, filename=None):
|
112
|
-
from rich.console import Console
|
113
|
-
from rich.table import Table
|
114
|
-
|
115
|
-
console = Console(record=True)
|
116
|
-
table = Table(show_header=True, header_style="bold magenta", row_styles=["", "dim"])
|
117
|
-
|
118
|
-
if not data:
|
119
|
-
console.print("No data provided!")
|
120
|
-
return
|
121
|
-
|
122
|
-
for key in data[0].keys():
|
123
|
-
table.add_column(key, style="dim")
|
124
|
-
|
125
|
-
for row in data:
|
126
|
-
table.add_row(*[str(value) for value in row.values()])
|
127
|
-
|
128
|
-
console.print(table)
|
129
|
-
"""
|
130
|
-
|
131
|
-
|
132
|
-
if __name__ == "__main__":
|
133
|
-
import doctest
|
134
|
-
|
135
|
-
doctest.testmod()
|