Skip to content

Commit

Permalink
chore: add simplify experiments to dicts (this is the format used in …
Browse files Browse the repository at this point in the history
…jsPsych)
  • Loading branch information
younesStrittmatter committed May 30, 2023
1 parent 2d2bb85 commit 03596da
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sweetpea/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

'print_experiments', 'tabulate_experiments',
'save_experiments_csv', 'experiments_to_tuples',
'simplify_experiments_to_dicts',

'Block', 'CrossBlock', 'MultiCrossBlock', 'Repeat',

Expand Down Expand Up @@ -80,10 +81,31 @@ def _experiments_to_tuples(experiments: List[dict],
tuple_lists.append(list(zip(*[experiment[key] for key in keys])))
return tuple_lists

def _experiments_to_dicts(experiments: List[dict],
keys: List[str]):
"""Converts a list of experiments into a list of lists of dictionaries, where
each dictionary represents a crossing in a given experiment.
:param experiments:
A list of experiments as :class:`dicts <dict>`. These are produced by
calls to the synthesis function :func:`.synthesize_trials`.
:returns:
A list of lists of dictionaries, where each sub-list corresponds
to one of the ``experiments``, each dictionary corresponds to a particular
crossing, and each string is the simple surface name of a level.
"""
tuple_lists: List[List[Tuple[str, ...]]] = []
for experiment in experiments:
tuple_lists.append([dict(zip(keys, values)) for values in zip(*[experiment[key] for key in keys])])
return tuple_lists

def simplify_experiments(experiments: List[Dict]) -> List[List[Tuple[str, ...]]]:
return _experiments_to_tuples(experiments, list(experiments[0].keys()))

def simplify_experiments_to_dicts(experiments: List[dict]) -> List[List[dict]]:
return _experiments_to_dicts(experiments, list(experiments[0].keys()))


def experiments_to_tuples(block: Block,
experiments: List[dict]):
Expand Down

0 comments on commit 03596da

Please sign in to comment.