convert_to_RLRW#

convert_to_RLRW(data, human_response, reward, stimulus, participant, **kwargs)[source]#

Convert a pandas DataFrame into a format compatible with the RLRW wrapper. This function takes in a DataFrame and the column names for the human response, reward, stimulus, and participant identifier, and returns a new DataFrame that is structured in a way that can be used with the RLRW wrapper. The function also checks to ensure that the specified columns exist in the input data and raises informative error messages if any of the columns are missing. Additionally, it can handle cases where there are multiple columns for stimulus, human response, or reward by creating new column names that match the number of columns provided.

Parameters:
datapandas.DataFrame

The pandas DataFrame to convert.

human_responsestr or list

The column name for the human response.

rewardstr or list

The column name(s) for the rewards in an order corresponding to the stimulus columns.

stimulusstr or list

The column name(s) for the stimulus in an order corresponding to the reward columns.

participantstr

The column name for the participant identifier.

kwargsdict, optional

Any other keyword arguments to pass to the pandas DataFrame.

Returns:
pandas.DataFrame

The pandas.DataFrame compatible with the RLRW wrapper, containing the stimulus, human response, and reward columns.

See also

cpm.applications.reinforcement_learning.RLRW

The RLRW wrapper that this function is designed to be compatible with.

Examples

>>> import pandas as pd
>>> from cpm.utils.data import convert_to_RLRW
>>> data = pd.DataFrame({
...     "participant_id": [1, 1, 2],
...     "stim_left": [0, 1, 0],
...     "stim_right": [1, 0, 1],
...     "choice": [1, 0, 1],
...     "reward_left": [1, 0, 1],
...     "reward_right": [0, 1, 0],
...     "block": [1, 1, 2],
...     "condition": ["A", "A", "B"],
... })
>>> output = convert_to_RLRW(
...     data=data,
...     human_response="choice",
...     reward=["reward_left", "reward_right"],
...     stimulus=["stim_left", "stim_right"],
...     participant="participant_id",
...     block="block",
...     condition="condition"
... )
>>> print(output)
    arm_0  arm_1  response  reward_0  reward_1  participant  block condition
0       0       1         1         1         0            1      1         A
1       1       0         0         0         1            1      1         A
2       0       1         1         1         0            2      2         B