expectedparrot / edsl

Design, conduct and analyze results of AI-powered surveys and experiments. Simulate social science and market research with large numbers of AI agents and LLMs.
https://docs.expectedparrot.com
MIT License
187 stars 19 forks source link

Results filter method should better handle empty filter results #543

Closed rbyh closed 5 months ago

rbyh commented 5 months ago

Results filter() method throws an exception if the filter does not produce any results, but it is not clear that this is reason for exception.

Code to reproduce:

from edsl.questions import QuestionMultipleChoice

q = QuestionMultipleChoice(
    question_name = "sky",
    question_text = "Is the sky often blue?",
    question_options = ["Yes", "No", "I do not know"]
)
results = q.run()
results.filter("sky != 'Yes'").select("sky").print()

Error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File [~/edsl/edsl/results/Results.py:424](http://localhost:8888/lab/tree/~/edsl/edsl/results/Results.py#line=423), in Results._parse_column(self, column)
    423 try:
--> 424     data_type, key = self._key_to_data_type[column], column
    425 except KeyError:

KeyError: 'sky'

During handling of the above exception, another exception occurred:

ResultsColumnNotFoundError                Traceback (most recent call last)
Cell In[3], line 9
      3 q = QuestionMultipleChoice(
      4     question_name = "sky",
      5     question_text = "Is the sky often blue?",
      6     question_options = ["Yes", "No", "I do not know"]
      7 )
      8 results = q.run()
----> 9 results.filter("sky != 'Yes'").select("sky").print()

File [~/edsl/edsl/results/Results.py:585](http://localhost:8888/lab/tree/~/edsl/edsl/results/Results.py#line=584), in Results.select(self, *columns)
    582 # iterate through the passed columns
    583 for column in columns:
    584     # a user could pass 'result.how_feeling' or just 'how_feeling'
--> 585     parsed_data_type, parsed_key = self._parse_column(column)
    586     data_types = get_data_types_to_return(parsed_data_type)
    587     found_once = False  # we need to track this to make sure we found the key at least once

File [~/edsl/edsl/results/Results.py:426](http://localhost:8888/lab/tree/~/edsl/edsl/results/Results.py#line=425), in Results._parse_column(self, column)
    424         data_type, key = self._key_to_data_type[column], column
    425     except KeyError:
--> 426         raise ResultsColumnNotFoundError(f"Column {column} not found in data")
    427 return data_type, key

ResultsColumnNotFoundError: Column sky not found in data
johnjosephhorton commented 5 months ago

What do you think of this? It warns you if you've selected down to empty & gives you another exception if you try to select on an empty Results object:

Screenshot 2024-05-23 at 7 10 35 AM
rbyh commented 5 months ago

I like it