edsl 0.1.29.dev3__py3-none-any.whl → 0.1.30__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.
Files changed (75) hide show
  1. edsl/Base.py +18 -18
  2. edsl/__init__.py +23 -23
  3. edsl/__version__.py +1 -1
  4. edsl/agents/Agent.py +79 -41
  5. edsl/agents/AgentList.py +26 -26
  6. edsl/agents/Invigilator.py +19 -2
  7. edsl/agents/InvigilatorBase.py +15 -10
  8. edsl/agents/PromptConstructionMixin.py +342 -100
  9. edsl/agents/descriptors.py +2 -1
  10. edsl/base/Base.py +289 -0
  11. edsl/config.py +2 -1
  12. edsl/conjure/InputData.py +39 -8
  13. edsl/conversation/car_buying.py +1 -1
  14. edsl/coop/coop.py +187 -150
  15. edsl/coop/utils.py +43 -75
  16. edsl/data/Cache.py +41 -18
  17. edsl/data/CacheEntry.py +6 -7
  18. edsl/data/SQLiteDict.py +11 -3
  19. edsl/data_transfer_models.py +4 -0
  20. edsl/jobs/Answers.py +15 -1
  21. edsl/jobs/Jobs.py +108 -49
  22. edsl/jobs/buckets/ModelBuckets.py +14 -2
  23. edsl/jobs/buckets/TokenBucket.py +32 -5
  24. edsl/jobs/interviews/Interview.py +99 -79
  25. edsl/jobs/interviews/InterviewTaskBuildingMixin.py +19 -24
  26. edsl/jobs/runners/JobsRunnerAsyncio.py +16 -16
  27. edsl/jobs/tasks/QuestionTaskCreator.py +10 -6
  28. edsl/jobs/tasks/TaskHistory.py +4 -3
  29. edsl/language_models/LanguageModel.py +17 -17
  30. edsl/language_models/ModelList.py +1 -1
  31. edsl/language_models/repair.py +8 -7
  32. edsl/notebooks/Notebook.py +47 -10
  33. edsl/prompts/Prompt.py +31 -19
  34. edsl/questions/QuestionBase.py +38 -13
  35. edsl/questions/QuestionBudget.py +5 -6
  36. edsl/questions/QuestionCheckBox.py +7 -3
  37. edsl/questions/QuestionExtract.py +5 -3
  38. edsl/questions/QuestionFreeText.py +7 -5
  39. edsl/questions/QuestionFunctional.py +34 -5
  40. edsl/questions/QuestionList.py +3 -4
  41. edsl/questions/QuestionMultipleChoice.py +68 -12
  42. edsl/questions/QuestionNumerical.py +4 -3
  43. edsl/questions/QuestionRank.py +5 -3
  44. edsl/questions/__init__.py +4 -3
  45. edsl/questions/descriptors.py +46 -4
  46. edsl/questions/question_registry.py +20 -31
  47. edsl/questions/settings.py +1 -1
  48. edsl/results/Dataset.py +31 -0
  49. edsl/results/DatasetExportMixin.py +570 -0
  50. edsl/results/Result.py +66 -70
  51. edsl/results/Results.py +160 -68
  52. edsl/results/ResultsDBMixin.py +7 -3
  53. edsl/results/ResultsExportMixin.py +22 -537
  54. edsl/results/ResultsGGMixin.py +3 -3
  55. edsl/results/ResultsToolsMixin.py +5 -5
  56. edsl/scenarios/FileStore.py +299 -0
  57. edsl/scenarios/Scenario.py +16 -24
  58. edsl/scenarios/ScenarioList.py +42 -17
  59. edsl/scenarios/ScenarioListExportMixin.py +32 -0
  60. edsl/scenarios/ScenarioListPdfMixin.py +2 -1
  61. edsl/scenarios/__init__.py +1 -0
  62. edsl/study/Study.py +8 -16
  63. edsl/surveys/MemoryPlan.py +11 -4
  64. edsl/surveys/Survey.py +88 -17
  65. edsl/surveys/SurveyExportMixin.py +4 -2
  66. edsl/surveys/SurveyFlowVisualizationMixin.py +6 -4
  67. edsl/tools/plotting.py +4 -2
  68. edsl/utilities/__init__.py +21 -21
  69. edsl/utilities/interface.py +66 -45
  70. edsl/utilities/utilities.py +11 -13
  71. {edsl-0.1.29.dev3.dist-info → edsl-0.1.30.dist-info}/METADATA +11 -10
  72. {edsl-0.1.29.dev3.dist-info → edsl-0.1.30.dist-info}/RECORD +74 -71
  73. {edsl-0.1.29.dev3.dist-info → edsl-0.1.30.dist-info}/WHEEL +1 -1
  74. edsl-0.1.29.dev3.dist-info/entry_points.txt +0 -3
  75. {edsl-0.1.29.dev3.dist-info → edsl-0.1.30.dist-info}/LICENSE +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
edsl/conjure/InputData.py CHANGED
@@ -1,5 +1,4 @@
1
- import functools
2
-
1
+ import base64
3
2
  from abc import ABC, abstractmethod
4
3
  from typing import Dict, Callable, Optional, List, Generator, Tuple, Union
5
4
  from collections import namedtuple
@@ -52,6 +51,7 @@ class InputDataABC(
52
51
  config: Optional[dict] = None,
53
52
  naming_function: Optional[Callable] = sanitize_string,
54
53
  raw_data: Optional[List] = None,
54
+ binary: Optional[str] = None,
55
55
  question_names: Optional[List[str]] = None,
56
56
  question_texts: Optional[List[str]] = None,
57
57
  answer_codebook: Optional[Dict] = None,
@@ -83,6 +83,15 @@ class InputDataABC(
83
83
  self.config = config
84
84
  self.naming_function = naming_function
85
85
 
86
+ if binary is not None:
87
+ self.binary = binary
88
+ else:
89
+ try:
90
+ with open(self.datafile_name, "rb") as file:
91
+ self.binary = base64.b64encode(file.read()).decode()
92
+ except FileNotFoundError:
93
+ self.binary = None
94
+
86
95
  def default_repair_func(x):
87
96
  return (
88
97
  x.replace("#", "_num")
@@ -118,6 +127,14 @@ class InputDataABC(
118
127
  if order_options:
119
128
  self.order_options()
120
129
 
130
+ @property
131
+ def download_link(self):
132
+ from IPython.display import HTML
133
+
134
+ actual_file_name = self.datafile_name.split("/")[-1]
135
+ download_link = f'<a href="data:text/plain;base64,{self.binary}" download="{actual_file_name}">Download {self.datafile_name}</a>'
136
+ return HTML(download_link)
137
+
121
138
  @abstractmethod
122
139
  def get_question_texts(self) -> List[str]:
123
140
  """Get the text of the questions
@@ -151,7 +168,9 @@ class InputDataABC(
151
168
  """
152
169
  raise NotImplementedError
153
170
 
154
- def rename_questions(self, rename_dict: Dict[str, str]) -> "InputData":
171
+ def rename_questions(
172
+ self, rename_dict: Dict[str, str], ignore_missing=False
173
+ ) -> "InputData":
155
174
  """Rename a question.
156
175
 
157
176
  >>> id = InputDataABC.example()
@@ -160,10 +179,10 @@ class InputDataABC(
160
179
 
161
180
  """
162
181
  for old_name, new_name in rename_dict.items():
163
- self.rename(old_name, new_name)
182
+ self.rename(old_name, new_name, ignore_missing=ignore_missing)
164
183
  return self
165
184
 
166
- def rename(self, old_name, new_name) -> "InputData":
185
+ def rename(self, old_name, new_name, ignore_missing=False) -> "InputData":
167
186
  """Rename a question.
168
187
 
169
188
  >>> id = InputDataABC.example()
@@ -171,13 +190,19 @@ class InputDataABC(
171
190
  ['evening', 'feeling']
172
191
 
173
192
  """
193
+ if old_name not in self.question_names:
194
+ if ignore_missing:
195
+ return self
196
+ else:
197
+ raise ValueError(f"Question {old_name} not found.")
198
+
174
199
  idx = self.question_names.index(old_name)
175
200
  self.question_names[idx] = new_name
176
201
  self.answer_codebook[new_name] = self.answer_codebook.pop(old_name, {})
177
202
 
178
203
  return self
179
204
 
180
- def _drop_question(self, question_name):
205
+ def _drop_question(self, question_name, ignore_missing=False):
181
206
  """Drop a question
182
207
 
183
208
  >>> id = InputDataABC.example()
@@ -185,6 +210,11 @@ class InputDataABC(
185
210
  ['feeling']
186
211
 
187
212
  """
213
+ if question_name not in self.question_names:
214
+ if ignore_missing:
215
+ return self
216
+ else:
217
+ raise ValueError(f"Question {question_name} not found.")
188
218
  idx = self.question_names.index(question_name)
189
219
  self._question_names.pop(idx)
190
220
  self._question_texts.pop(idx)
@@ -206,7 +236,7 @@ class InputDataABC(
206
236
  self._drop_question(qn)
207
237
  return self
208
238
 
209
- def keep(self, *question_names_to_keep) -> "InputDataABC":
239
+ def keep(self, *question_names_to_keep, ignore_missing=False) -> "InputDataABC":
210
240
  """Keep a question.
211
241
 
212
242
  >>> id = InputDataABC.example()
@@ -217,7 +247,7 @@ class InputDataABC(
217
247
  all_question_names = self._question_names[:]
218
248
  for qn in all_question_names:
219
249
  if qn not in question_names_to_keep:
220
- self._drop_question(qn)
250
+ self._drop_question(qn, ignore_missing=ignore_missing)
221
251
  return self
222
252
 
223
253
  def modify_question_type(
@@ -284,6 +314,7 @@ class InputDataABC(
284
314
  "raw_data": self.raw_data,
285
315
  "question_names": self.question_names,
286
316
  "question_texts": self.question_texts,
317
+ "binary": self.binary,
287
318
  "answer_codebook": self.answer_codebook,
288
319
  "question_types": self.question_types,
289
320
  }
@@ -30,7 +30,7 @@ c1 = Conversation(agent_list=AgentList([a1, a3, a2]), max_turns=5, verbose=True)
30
30
  c2 = Conversation(agent_list=AgentList([a1, a2]), max_turns=5, verbose=True)
31
31
 
32
32
  c = Cache.load("car_talk.json.gz")
33
- breakpoint()
33
+ # breakpoint()
34
34
  combo = ConversationList([c1, c2], cache=c)
35
35
  combo.run()
36
36
  results = combo.to_results()