edsl 0.1.29.dev6__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 (65) hide show
  1. edsl/Base.py +6 -3
  2. edsl/__init__.py +23 -23
  3. edsl/__version__.py +1 -1
  4. edsl/agents/Agent.py +43 -40
  5. edsl/agents/AgentList.py +23 -22
  6. edsl/agents/Invigilator.py +19 -2
  7. edsl/agents/descriptors.py +2 -1
  8. edsl/base/Base.py +289 -0
  9. edsl/config.py +2 -1
  10. edsl/conversation/car_buying.py +1 -1
  11. edsl/coop/utils.py +28 -1
  12. edsl/data/Cache.py +41 -18
  13. edsl/data/CacheEntry.py +6 -7
  14. edsl/data/SQLiteDict.py +11 -3
  15. edsl/data_transfer_models.py +4 -0
  16. edsl/jobs/Answers.py +15 -1
  17. edsl/jobs/Jobs.py +86 -33
  18. edsl/jobs/buckets/ModelBuckets.py +14 -2
  19. edsl/jobs/buckets/TokenBucket.py +32 -5
  20. edsl/jobs/interviews/Interview.py +99 -79
  21. edsl/jobs/interviews/InterviewTaskBuildingMixin.py +18 -24
  22. edsl/jobs/runners/JobsRunnerAsyncio.py +16 -16
  23. edsl/jobs/tasks/QuestionTaskCreator.py +10 -6
  24. edsl/jobs/tasks/TaskHistory.py +4 -3
  25. edsl/language_models/LanguageModel.py +17 -17
  26. edsl/language_models/ModelList.py +1 -1
  27. edsl/language_models/repair.py +8 -7
  28. edsl/notebooks/Notebook.py +16 -10
  29. edsl/questions/QuestionBase.py +6 -2
  30. edsl/questions/QuestionBudget.py +5 -6
  31. edsl/questions/QuestionCheckBox.py +7 -3
  32. edsl/questions/QuestionExtract.py +5 -3
  33. edsl/questions/QuestionFreeText.py +7 -5
  34. edsl/questions/QuestionFunctional.py +34 -5
  35. edsl/questions/QuestionList.py +3 -4
  36. edsl/questions/QuestionMultipleChoice.py +68 -12
  37. edsl/questions/QuestionNumerical.py +4 -3
  38. edsl/questions/QuestionRank.py +5 -3
  39. edsl/questions/__init__.py +4 -3
  40. edsl/questions/descriptors.py +46 -4
  41. edsl/results/DatasetExportMixin.py +570 -0
  42. edsl/results/Result.py +66 -70
  43. edsl/results/Results.py +160 -68
  44. edsl/results/ResultsDBMixin.py +7 -3
  45. edsl/results/ResultsExportMixin.py +22 -537
  46. edsl/results/ResultsGGMixin.py +3 -3
  47. edsl/results/ResultsToolsMixin.py +1 -4
  48. edsl/scenarios/FileStore.py +299 -0
  49. edsl/scenarios/Scenario.py +16 -24
  50. edsl/scenarios/ScenarioList.py +25 -14
  51. edsl/scenarios/ScenarioListExportMixin.py +32 -0
  52. edsl/scenarios/ScenarioListPdfMixin.py +2 -1
  53. edsl/scenarios/__init__.py +1 -0
  54. edsl/study/Study.py +5 -7
  55. edsl/surveys/MemoryPlan.py +11 -4
  56. edsl/surveys/Survey.py +52 -15
  57. edsl/surveys/SurveyExportMixin.py +4 -2
  58. edsl/surveys/SurveyFlowVisualizationMixin.py +6 -4
  59. edsl/utilities/__init__.py +21 -21
  60. edsl/utilities/interface.py +66 -45
  61. edsl/utilities/utilities.py +11 -13
  62. {edsl-0.1.29.dev6.dist-info → edsl-0.1.30.dist-info}/METADATA +1 -1
  63. {edsl-0.1.29.dev6.dist-info → edsl-0.1.30.dist-info}/RECORD +65 -61
  64. {edsl-0.1.29.dev6.dist-info → edsl-0.1.30.dist-info}/WHEEL +1 -1
  65. {edsl-0.1.29.dev6.dist-info → edsl-0.1.30.dist-info}/LICENSE +0 -0
edsl/surveys/Survey.py CHANGED
@@ -2,26 +2,20 @@
2
2
 
3
3
  from __future__ import annotations
4
4
  import re
5
-
6
5
  from typing import Any, Generator, Optional, Union, List, Literal, Callable
7
-
8
- from rich import print
9
- from rich.table import Table
10
-
6
+ from uuid import uuid4
7
+ from edsl.Base import Base
11
8
  from edsl.exceptions import SurveyCreationError, SurveyHasNoRulesError
12
9
  from edsl.questions.QuestionBase import QuestionBase
13
10
  from edsl.surveys.base import RulePriority, EndOfSurvey
11
+ from edsl.surveys.DAG import DAG
12
+ from edsl.surveys.descriptors import QuestionsDescriptor
13
+ from edsl.surveys.MemoryPlan import MemoryPlan
14
14
  from edsl.surveys.Rule import Rule
15
15
  from edsl.surveys.RuleCollection import RuleCollection
16
-
17
- from edsl.Base import Base
18
16
  from edsl.surveys.SurveyExportMixin import SurveyExportMixin
19
- from edsl.surveys.descriptors import QuestionsDescriptor
20
- from edsl.surveys.MemoryPlan import MemoryPlan
21
- from edsl.surveys.DAG import DAG
22
- from edsl.utilities import is_notebook
23
- from edsl.utilities.decorators import add_edsl_version, remove_edsl_version
24
17
  from edsl.surveys.SurveyFlowVisualizationMixin import SurveyFlowVisualizationMixin
18
+ from edsl.utilities.decorators import add_edsl_version, remove_edsl_version
25
19
 
26
20
 
27
21
  class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
@@ -112,6 +106,39 @@ class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
112
106
 
113
107
  return dict_hash(self._to_dict())
114
108
 
109
+ def __add__(self, other: Survey) -> Survey:
110
+ """Combine two surveys.
111
+
112
+ :param other: The other survey to combine with this one.
113
+ >>> s1 = Survey.example()
114
+ >>> from edsl import QuestionFreeText
115
+ >>> s2 = Survey([QuestionFreeText(question_text="What is your name?", question_name="yo")])
116
+ >>> s3 = s1 + s2
117
+ Traceback (most recent call last):
118
+ ...
119
+ ValueError: ('Cannot combine two surveys with non-default rules.', "Please use the 'clear_non_default_rules' method to remove non-default rules from the survey.")
120
+ >>> s3 = s1.clear_non_default_rules() + s2
121
+ >>> len(s3.questions)
122
+ 4
123
+
124
+ """
125
+ if (
126
+ len(self.rule_collection.non_default_rules) > 0
127
+ or len(other.rule_collection.non_default_rules) > 0
128
+ ):
129
+ raise ValueError(
130
+ "Cannot combine two surveys with non-default rules.",
131
+ "Please use the 'clear_non_default_rules' method to remove non-default rules from the survey.",
132
+ )
133
+
134
+ return Survey(questions=self.questions + other.questions)
135
+
136
+ def clear_non_default_rules(self) -> Survey:
137
+ s = Survey()
138
+ for question in self.questions:
139
+ s.add_question(question)
140
+ return s
141
+
115
142
  @property
116
143
  def parameters(self):
117
144
  return set.union(*[q.parameters for q in self.questions])
@@ -563,6 +590,12 @@ class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
563
590
  job = Jobs(survey=self)
564
591
  return job.by(*args)
565
592
 
593
+ def to_jobs(self):
594
+ """Convert the survey to a Jobs object."""
595
+ from edsl.jobs.Jobs import Jobs
596
+
597
+ return Jobs(survey=self)
598
+
566
599
  def run(self, *args, **kwargs) -> "Results":
567
600
  """Turn the survey into a Job and runs it.
568
601
 
@@ -957,6 +990,8 @@ class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
957
990
  │ └───────────────┴─────────────────┴───────────────┴──────────────────────────────────────────────┘ │
958
991
  └────────────────────────────────────────────────────────────────────────────────────────────────────┘
959
992
  """
993
+ from rich.table import Table
994
+
960
995
  table = Table(show_header=True, header_style="bold magenta")
961
996
  table.add_column("Questions", style="dim")
962
997
 
@@ -1020,7 +1055,7 @@ class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
1020
1055
  return res
1021
1056
 
1022
1057
  @classmethod
1023
- def example(cls, params=False) -> Survey:
1058
+ def example(cls, params: bool = False, randomize: bool = False) -> Survey:
1024
1059
  """Return an example survey.
1025
1060
 
1026
1061
  >>> s = Survey.example()
@@ -1029,8 +1064,9 @@ class Survey(SurveyExportMixin, SurveyFlowVisualizationMixin, Base):
1029
1064
  """
1030
1065
  from edsl.questions.QuestionMultipleChoice import QuestionMultipleChoice
1031
1066
 
1067
+ addition = "" if not randomize else str(uuid4())
1032
1068
  q0 = QuestionMultipleChoice(
1033
- question_text="Do you like school?",
1069
+ question_text=f"Do you like school?{addition}",
1034
1070
  question_options=["yes", "no"],
1035
1071
  question_name="q0",
1036
1072
  )
@@ -1148,4 +1184,5 @@ def main():
1148
1184
  if __name__ == "__main__":
1149
1185
  import doctest
1150
1186
 
1151
- doctest.testmod(optionflags=doctest.ELLIPSIS | doctest.SKIP)
1187
+ # doctest.testmod(optionflags=doctest.ELLIPSIS | doctest.SKIP)
1188
+ doctest.testmod(optionflags=doctest.ELLIPSIS)
@@ -1,8 +1,6 @@
1
1
  """A mixin class for exporting surveys to different formats."""
2
2
 
3
- from docx import Document
4
3
  from typing import Union, Optional
5
- import black
6
4
 
7
5
 
8
6
  class SurveyExportMixin:
@@ -29,6 +27,8 @@ class SurveyExportMixin:
29
27
 
30
28
  def docx(self, filename=None) -> Union["Document", None]:
31
29
  """Generate a docx document for the survey."""
30
+ from docx import Document
31
+
32
32
  doc = Document()
33
33
  doc.add_heading("EDSL Survey")
34
34
  doc.add_paragraph(f"\n")
@@ -83,6 +83,8 @@ class SurveyExportMixin:
83
83
  survey = Survey(questions=[q0, q1, q2])
84
84
  ...
85
85
  """
86
+ import black
87
+
86
88
  header_lines = ["from edsl.surveys.Survey import Survey"]
87
89
  header_lines.append("from edsl import Question")
88
90
  lines = ["\n".join(header_lines)]
@@ -1,10 +1,7 @@
1
1
  """A mixin for visualizing the flow of a survey."""
2
2
 
3
- import pydot
4
- import tempfile
5
- from IPython.display import Image
6
- from edsl.utilities import is_notebook
7
3
  from edsl.surveys.base import RulePriority, EndOfSurvey
4
+ import tempfile
8
5
 
9
6
 
10
7
  class SurveyFlowVisualizationMixin:
@@ -13,6 +10,8 @@ class SurveyFlowVisualizationMixin:
13
10
  def show_flow(self, filename: str = None):
14
11
  """Create an image showing the flow of users through the survey."""
15
12
  # Create a graph object
13
+ import pydot
14
+
16
15
  graph = pydot.Dot(graph_type="digraph")
17
16
 
18
17
  # Add nodes for each question
@@ -101,8 +100,11 @@ class SurveyFlowVisualizationMixin:
101
100
  on Ubuntu.
102
101
  """
103
102
  )
103
+ from edsl.utilities.utilities import is_notebook
104
104
 
105
105
  if is_notebook():
106
+ from IPython.display import Image
107
+
106
108
  display(Image(tmp_file.name))
107
109
  else:
108
110
  import os
@@ -1,22 +1,22 @@
1
- from edsl.utilities.interface import (
2
- print_dict_as_html_table,
3
- print_dict_with_rich,
4
- print_list_of_dicts_as_html_table,
5
- print_table_with_rich,
6
- print_public_methods_with_doc,
7
- print_list_of_dicts_as_markdown_table,
8
- )
1
+ # from edsl.utilities.interface import (
2
+ # print_dict_as_html_table,
3
+ # print_dict_with_rich,
4
+ # print_list_of_dicts_as_html_table,
5
+ # print_table_with_rich,
6
+ # print_public_methods_with_doc,
7
+ # print_list_of_dicts_as_markdown_table,
8
+ # )
9
9
 
10
- from edsl.utilities.utilities import (
11
- create_valid_var_name,
12
- dict_to_html,
13
- hash_value,
14
- HTMLSnippet,
15
- is_notebook,
16
- is_gzipped,
17
- is_valid_variable_name,
18
- random_string,
19
- repair_json,
20
- shorten_string,
21
- time_all_functions,
22
- )
10
+ # from edsl.utilities.utilities import (
11
+ # create_valid_var_name,
12
+ # dict_to_html,
13
+ # hash_value,
14
+ # HTMLSnippet,
15
+ # is_notebook,
16
+ # is_gzipped,
17
+ # is_valid_variable_name,
18
+ # random_string,
19
+ # repair_json,
20
+ # shorten_string,
21
+ # time_all_functions,
22
+ # )
@@ -1,12 +1,45 @@
1
1
  """A module for displaying data in various formats."""
2
2
 
3
3
  from html import escape
4
- from IPython.display import HTML
5
- from IPython.display import display as ipython_diplay
6
4
 
7
- # from PIL import Image, ImageDraw, ImageFont
8
- from rich.console import Console
9
- from rich.table import Table
5
+
6
+ def create_image(console, image_filename):
7
+ """Create an image from the console output."""
8
+ font_size = 15
9
+ from PIL import Image, ImageDraw, ImageFont
10
+
11
+ text = console.export_text() # Get the console output as text.
12
+
13
+ # Create an image from the text
14
+ font_size = 15
15
+ font = ImageFont.load_default() # Use the default font to avoid file path issues.
16
+ # text_width, text_height = ImageDraw.Draw(
17
+ # Image.new("RGB", (100, 100))
18
+ # ).multiline_textsize(text, font=font)
19
+ text_width, text_height = get_multiline_textsize(text, font)
20
+ image = Image.new(
21
+ "RGB", (text_width + 20, text_height + 20), color=(255, 255, 255)
22
+ ) # Add some padding
23
+ d = ImageDraw.Draw(image)
24
+
25
+ # Draw text to image
26
+ d.multiline_text((10, 10), text, font=font, fill=(0, 0, 0))
27
+ # Save the image
28
+ image.save(image_filename)
29
+
30
+
31
+ def display_table(console, table, filename):
32
+ # from rich.console import Console
33
+ # from rich.table import Table
34
+ """Display the table using the rich library and save it to a file if a filename is provided."""
35
+ if filename is not None:
36
+ with open(filename, "w") as f:
37
+ with console.capture() as capture:
38
+ console.print(table)
39
+ f.write(capture.get())
40
+ create_image(console, filename + ".png")
41
+ else:
42
+ console.print(table)
10
43
 
11
44
 
12
45
  def gen_html_sandwich(html_inner, interactive=False):
@@ -133,43 +166,10 @@ def get_multiline_textsize(text, font):
133
166
  return max_width, total_height
134
167
 
135
168
 
136
- # def create_image(console, image_filename):
137
- # """Create an image from the console output."""
138
- # font_size = 15
139
-
140
- # text = console.export_text() # Get the console output as text.
141
-
142
- # # Create an image from the text
143
- # font_size = 15
144
- # font = ImageFont.load_default() # Use the default font to avoid file path issues.
145
- # # text_width, text_height = ImageDraw.Draw(
146
- # # Image.new("RGB", (100, 100))
147
- # # ).multiline_textsize(text, font=font)
148
- # text_width, text_height = get_multiline_textsize(text, font)
149
- # image = Image.new(
150
- # "RGB", (text_width + 20, text_height + 20), color=(255, 255, 255)
151
- # ) # Add some padding
152
- # d = ImageDraw.Draw(image)
153
-
154
- # # Draw text to image
155
- # d.multiline_text((10, 10), text, font=font, fill=(0, 0, 0))
156
- # # Save the image
157
- # image.save(image_filename)
158
-
159
-
160
- def display(console, table, filename):
161
- """Display the table using the rich library and save it to a file if a filename is provided."""
162
- if filename is not None:
163
- with open(filename, "w") as f:
164
- with console.capture() as capture:
165
- console.print(table)
166
- f.write(capture.get())
167
- create_image(console, filename + ".png")
168
- else:
169
- console.print(table)
170
-
171
-
172
169
  def print_results_long(results, max_rows=None):
170
+ from rich.console import Console
171
+ from rich.table import Table
172
+
173
173
  console = Console(record=True)
174
174
  table = Table(show_header=True, header_style="bold magenta")
175
175
  table.add_column("Result index", style="dim")
@@ -199,6 +199,9 @@ def print_dict_with_rich(d, key_name="Key", value_name="Value", filename=None):
199
199
  │ c │ 3 │
200
200
  └─────┴───────┘
201
201
  """
202
+ from rich.console import Console
203
+ from rich.table import Table
204
+
202
205
  console = Console(record=True)
203
206
  table = Table(show_header=True, header_style="bold magenta")
204
207
  table.add_column(key_name, style="dim")
@@ -206,7 +209,7 @@ def print_dict_with_rich(d, key_name="Key", value_name="Value", filename=None):
206
209
  for key, value in d.items():
207
210
  table.add_row(key, str(value))
208
211
  console.print(table)
209
- # display(console, table, filename)
212
+ # display_table(console, table, filename)
210
213
 
211
214
 
212
215
  def print_dict_as_html_table(
@@ -251,6 +254,9 @@ def print_dict_as_html_table(
251
254
 
252
255
 
253
256
  def print_scenario_list(data):
257
+ from rich.console import Console
258
+ from rich.table import Table
259
+
254
260
  new_data = []
255
261
  for obs in data:
256
262
  try:
@@ -300,6 +306,9 @@ def print_dataset_with_rich(data, filename=None, split_at_dot=True):
300
306
  │ 3 │ 6 │
301
307
  └───┴───┘
302
308
  """
309
+ from rich.console import Console
310
+ from rich.table import Table
311
+
303
312
  console = Console(record=True)
304
313
 
305
314
  # Create a table object
@@ -321,7 +330,7 @@ def print_dataset_with_rich(data, filename=None, split_at_dot=True):
321
330
  table.add_row(*row)
322
331
 
323
332
  console.print(table)
324
- # display(console, table, filename)
333
+ # display_table(console, table, filename)
325
334
 
326
335
 
327
336
  def create_latex_table_from_data(data, filename=None, split_at_dot=True):
@@ -495,6 +504,9 @@ def print_list_of_dicts_as_markdown_table(data, filename=None):
495
504
 
496
505
  def print_public_methods_with_doc(obj):
497
506
  """Print the public methods of an object along with their docstrings."""
507
+ from rich.console import Console
508
+ from rich.table import Table
509
+
498
510
  console = Console()
499
511
  public_methods_with_docstrings = [
500
512
  (method, getattr(obj, method).__doc__)
@@ -525,6 +537,10 @@ def print_tally_with_rich(data, filename=None):
525
537
  └───────┴───────┘
526
538
  """
527
539
  # Initialize a console object
540
+ from rich.console import Console
541
+ from rich.table import Table
542
+ from IPython.display import display
543
+
528
544
  console = Console(record=True)
529
545
 
530
546
  # Create a new table
@@ -538,7 +554,9 @@ def print_tally_with_rich(data, filename=None):
538
554
  for key, value in data.items():
539
555
  table.add_row(key, str(value))
540
556
 
541
- display(console, table, filename)
557
+ from IPython.display import display
558
+
559
+ display_table(console, table, filename)
542
560
 
543
561
 
544
562
  def print_table_with_rich(data, filename=None):
@@ -561,6 +579,9 @@ def print_table_with_rich(data, filename=None):
561
579
  │ 2 │ 9 │ 8 │
562
580
  └───┴───┴───┘
563
581
  """
582
+ from rich.console import Console
583
+ from rich.table import Table
584
+
564
585
  # Initialize a console object - expects a list of dictionaries
565
586
  console = Console(record=True)
566
587
 
@@ -580,7 +601,7 @@ def print_table_with_rich(data, filename=None):
580
601
  for row in data:
581
602
  table.add_row(*[str(value) for value in row.values()])
582
603
 
583
- display(console, table, filename)
604
+ display_table(console, table, filename)
584
605
 
585
606
 
586
607
  if __name__ == "__main__":
@@ -1,5 +1,9 @@
1
1
  """Utility functions for working with strings, dictionaries, and files."""
2
2
 
3
+ from functools import wraps
4
+ import types
5
+ import time
6
+
3
7
  import hashlib
4
8
  import json
5
9
  import keyword
@@ -11,18 +15,10 @@ import tempfile
11
15
  import gzip
12
16
  import webbrowser
13
17
  import json
18
+
14
19
  from html import escape
15
20
  from typing import Callable, Union
16
21
 
17
- from pygments import highlight
18
- from pygments.lexers import JsonLexer
19
- from pygments.formatters import HtmlFormatter
20
- from IPython.display import HTML
21
-
22
- from functools import wraps
23
- import types
24
- import time
25
-
26
22
 
27
23
  def time_it(func):
28
24
  @wraps(func)
@@ -52,10 +48,6 @@ def dict_hash(data: dict):
52
48
  )
53
49
 
54
50
 
55
- import re
56
- import json
57
-
58
-
59
51
  def extract_json_from_string(text):
60
52
  pattern = re.compile(r"\{.*?\}")
61
53
  match = pattern.search(text)
@@ -96,6 +88,11 @@ def data_to_html(data, replace_new_lines=False):
96
88
  if "edsl_class_name" in data:
97
89
  _ = data.pop("edsl_class_name")
98
90
 
91
+ from pygments import highlight
92
+ from pygments.lexers import JsonLexer
93
+ from pygments.formatters import HtmlFormatter
94
+ from IPython.display import HTML
95
+
99
96
  json_str = json.dumps(data, indent=4)
100
97
  formatted_json = highlight(
101
98
  json_str,
@@ -104,6 +101,7 @@ def data_to_html(data, replace_new_lines=False):
104
101
  )
105
102
  if replace_new_lines:
106
103
  formatted_json = formatted_json.replace("\\n", "<br>")
104
+
107
105
  return HTML(formatted_json).data
108
106
 
109
107
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: edsl
3
- Version: 0.1.29.dev6
3
+ Version: 0.1.30
4
4
  Summary: Create and analyze LLM-based surveys
5
5
  Home-page: https://www.expectedparrot.com/
6
6
  License: MIT