rlberry.manager.read_writer_data

rlberry.manager.read_writer_data(data_source, many_agent_by_str_datasource=False, preprocess_tag=None, preprocess_func=None, id_agent=None)[source]

Given a list of ExperimentManager or a folder, read data (corresponding to info) obtained in each episode. The dictionary returned by agents’ .fit() method must contain a key equal to info.

Parameters:
data_source: :class:`~rlberry.manager.ExperimentManager`, or list of :class:`~rlberry.manager.ExperimentManager` or str or list of str
  • If ExperimentManager or list of ExperimentManager, load data from it (the agents must be fitted).

  • If str, the string must be the string path of a directory, each

subdirectory of this directory must contain pickle files. Load the data from the directory of the latest experiment in date. This str should be equal to the value of the output_dir parameter in ExperimentManager.

  • If list of str, each string must be a directory containing pickle files.

Load the data from these pickle files.

Note: the agent’s save function must save its writer at the key _writer. This is the default for rlberry agents.

many_agent_by_str_datasource: bool (False by default)

(only useful if the datasource is a str or a list of str) If True, select many agent: the last agent of every different agent name inside the folder If False, select only one agent: the most recently trained agent from the directory (whatever its name)

preprocess_tag: str or list of str or None

Tag of data that we want to preprocess.

preprocess_func: Callable or None or list of Callable or None

Function to apply to ‘preprocess_tag’ column before returning data. For instance, if preprocess_tag=episode_rewards,setting preprocess_func=np.cumsum will return cumulative rewards If None, do not preprocess. If preprocess_tag is a list, preprocess_func must be None or a list of Callable or None that matches the length of preprocess_tag.

id_agent: int or None, default=None

If not None, returns the data only for agent ‘id_agent’.

Returns:
Pandas DataFrame with data from writers.

Examples

>>> from rlberry.agents.torch import A2CAgent, DQNAgent
>>> from rlberry.manager import ExperimentManager, read_writer_data
>>> from rlberry.envs import gym_make
>>>
>>> if __name__=="__main__":
>>>     managers = [ ExperimentManager(
>>>         agent_class,
>>>         (gym_make, dict(id="CartPole-v1")),
>>>         fit_budget=1e4,
>>>         eval_kwargs=dict(eval_horizon=500),
>>>         n_fit=1,
>>>         parallelization="process",
>>>         mp_context="spawn",
>>>         seed=42,
>>>          ) for agent_class in [A2CAgent, DQNAgent]]
>>>     for manager in managers:
>>>         manager.fit()
>>>     data = read_writer_data(managers)

Examples using rlberry.manager.read_writer_data

A demo of Bandit BAI on a real dataset to select mirrors

A demo of Bandit BAI on a real dataset to select mirrors