Utils
data
¶
Utilities for data.
Dataset
¶
Dataset(
env: str | Env,
env_kwargs: dict[str, Any] | None = None,
batch_size: int = 1,
seq_len: int | None = None,
max_batch: int | None = None,
batch_first: bool = False,
cache_len: int | None = None,
)
Make an environment into an iterable dataset for supervised learning.
Create an iterator that at each call returns inputs: numpy array (sequence_length, batch_size, input_units) target: numpy array (sequence_length, batch_size, output_units)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
str | Env
|
str for env id or gym.Env objects |
required |
env_kwargs
|
dict[str, Any] | None
|
dict, additional kwargs for environment, if env is str |
None
|
batch_size
|
int
|
int, batch size |
1
|
seq_len
|
int | None
|
int, sequence length |
None
|
max_batch
|
int | None
|
int, maximum number of batch for iterator, default infinite |
None
|
batch_first
|
bool
|
bool, if True, return (batch, seq_len, n_units), default False |
False
|
cache_len
|
int | None
|
int, default length of caching |
None
|
Source code in neurogym/utils/data.py
info
¶
Formatting information about envs and wrappers.
show_all_tasks
¶
Show all available tasks in neurogym.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str | None
|
If provided, only show tasks with this tag. |
None
|
Source code in neurogym/utils/info.py
show_all_wrappers
¶
show_all_tags
¶
show_info
¶
Show information about an environment or a wrapper.
Using the built-in logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj_
|
str | Env
|
the environment or wrapper to show information about. |
required |
Source code in neurogym/utils/info.py
plotting
¶
Plotting functions.
plot_env
¶
plot_env(
env: TrialEnv | Env,
num_steps: int = 200,
num_trials: int | None = None,
def_act: int | None = None,
model=None,
name: str | None = None,
legend: bool = True,
ob_traces: list | None = None,
fig_kwargs: dict | None = None,
fname: str | None = None,
plot_performance: bool = True,
plot_config: PlotConfig | None = None,
)
Plot environment with agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
TrialEnv | Env
|
Already built neurogym task or its name. |
required |
num_steps
|
int
|
Number of steps to run the task for. |
200
|
num_trials
|
int | None
|
If not None, the number of trials to run. |
None
|
def_act
|
int | None
|
If not None (and model=None), the task will be run with the specified action. |
None
|
model
|
If not None, the task will be run with the actions predicted by model, which so far is assumed to be created and trained with the stable-baselines3 toolbox: (https://stable-baselines3.readthedocs.io/en/master/) |
None
|
|
name
|
str | None
|
Title to show on the rewards panel. |
None
|
legend
|
bool
|
Whether to show the legend for actions panel or not. |
True
|
ob_traces
|
list | None
|
If != [] observations will be plot as traces, with the labels specified by ob_traces. |
None
|
fig_kwargs
|
dict | None
|
Figure properties admitted by matplotlib.pyplot.subplots() function |
None
|
fname
|
str | None
|
If not None, save fig or movie to fname. |
None
|
plot_performance
|
bool
|
Whether to show the performance subplot (default: True). |
True
|
plot_config
|
PlotConfig | None
|
Plot configuration (experimental). If set to None, the global configuration is used. |
None
|
Source code in neurogym/utils/plotting.py
run_env
¶
run_env(
env: TrialEnv | Env,
num_steps: int = 200,
num_trials: int | None = None,
def_act: int | None = None,
model=None,
) -> dict
Run the given environment with the.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
TrialEnv | Env
|
A NeuroGym environment. |
required |
num_steps
|
int
|
Number of steps to run the task for. |
200
|
num_trials
|
int | None
|
Number of trials to run. |
None
|
def_act
|
int | None
|
Preset action to pass to the environment. |
None
|
model
|
Model (agent) learning from the environment. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the results of the trial. |
Source code in neurogym/utils/plotting.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
visualize_run
¶
visualize_run(
ob: ndarray,
actions: ndarray,
gt: ndarray | None = None,
rewards: ndarray | None = None,
performance: ndarray | None = None,
states: ndarray | None = None,
legend: bool = True,
ob_traces: list | None = None,
name: str = "",
fname: str | None = None,
fig_kwargs: dict | None = None,
env: TrialEnv | Env | None = None,
initial_ob: ndarray | None = None,
trial_starts: list | None = None,
plot_config: PlotConfig | None = None,
) -> None
Visualize a run in a simple environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ob
|
ndarray
|
NumPy array of observation (n_step, n_unit). |
required |
actions
|
ndarray
|
NumPy array of action (n_step, n_unit) |
required |
gt
|
ndarray | None
|
NumPy array of groud truth. |
None
|
rewards
|
ndarray | None
|
NumPy array of rewards. |
None
|
performance
|
ndarray | None
|
NumPy array of performance (if set to |
None
|
states
|
ndarray | None
|
NumPy array of network states |
None
|
name
|
str
|
Title to show on the rewards panel and name to save figure. |
''
|
fname
|
str | None
|
Optional name for the file where the figure should be saved. |
None
|
legend
|
bool
|
Whether to show the legend for actions panel. |
True
|
ob_traces
|
list | None
|
If a non-empty listis provided, observations will be plot as traces, with the labels specified by ob_traces |
None
|
fig_kwargs
|
dict | None
|
Figure properties admitted by matplotlib.pyplot.subplots() function |
None
|
env
|
TrialEnv | Env | None
|
Environment class for extra information |
None
|
initial_ob
|
ndarray | None
|
Initial observation to be used to align with actions |
None
|
trial_starts
|
list | None
|
List of trial start indices, 1-based |
None
|
plot_config
|
PlotConfig | None
|
Optional plot configuration. |
None
|
Source code in neurogym/utils/plotting.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
plot_env_1dbox
¶
plot_env_1dbox(
ob: ndarray,
actions: ndarray,
gt: ndarray | None = None,
rewards: ndarray | None = None,
performance: ndarray | None = None,
states: ndarray | None = None,
legend: bool = True,
ob_traces: list | None = None,
name: str = "",
fname: str | None = None,
fig_kwargs: dict | None = None,
env: TrialEnv | Env | None = None,
trial_starts: ndarray | None = None,
plot_config: PlotConfig | None = None,
) -> Figure | None
Plot environment with 1-D Box observation space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ob
|
ndarray
|
Array of observed values. |
required |
actions
|
ndarray
|
Array of actions. |
required |
gt
|
ndarray | None
|
Array of ground truth values. |
None
|
rewards
|
ndarray | None
|
Array of reward values. |
None
|
performance
|
ndarray | None
|
Array of performance values. |
None
|
states
|
ndarray | None
|
Array of state values. |
None
|
legend
|
bool
|
Legend toggle. |
True
|
ob_traces
|
list | None
|
List of observation traces. |
None
|
name
|
str
|
Name of the environment. |
''
|
fname
|
str | None
|
Name of the file to save the plot to. |
None
|
fig_kwargs
|
dict | None
|
Figure configuration. |
None
|
env
|
TrialEnv | Env | None
|
A NeuroGym environment. |
None
|
trial_starts
|
ndarray | None
|
List of trial start times. |
None
|
plot_config
|
PlotConfig | None
|
Plot configuration. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised if the array of observed values is not 2D. |
Returns:
| Type | Description |
|---|---|
Figure | None
|
A Matplotlib figure. |
Source code in neurogym/utils/plotting.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
plot_env_3dbox
¶
plot_env_3dbox(
ob: ndarray,
fname: str = "",
env: TrialEnv | Env | None = None,
) -> None
Plot environment with 3-D Box observation space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ob
|
ndarray
|
Array of observation values. |
required |
fname
|
str
|
File name to save the figure to. |
''
|
env
|
TrialEnv | Env | None
|
A NeuroGym environment. |
None
|
Source code in neurogym/utils/plotting.py
ngym_random
¶
TruncExp
¶
Source code in neurogym/utils/ngym_random.py
trunc_exp
¶
Function for generating period durations.
Source code in neurogym/utils/ngym_random.py
random_number_fn
¶
Return a random number generating function from a distribution.
Source code in neurogym/utils/ngym_random.py
random_number_name
¶
Return a string explaining the dist and args.
Source code in neurogym/utils/ngym_random.py
scheduler
¶
Trial scheduler class.
BaseSchedule
¶
Base schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int, number of conditions to schedule |
required |
Source code in neurogym/utils/scheduler.py
SequentialSchedule
¶
RandomSchedule
¶
SequentialBlockSchedule
¶
RandomBlockSchedule
¶
spaces
¶
Box
¶
Bases: Box
Thin wrapper of gymnasium.spaces.Box.
Allow the user to give names to each dimension of the Box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low, high, kwargs
|
see gymnasium.spaces.Box |
required | |
name
|
dict describing the name of different dimensions |
None
|
Example usage
observation_space = Box(low=0, high=1, name={'fixation': 0, 'stimulus': [1, 2]})
Source code in neurogym/utils/spaces.py
Discrete
¶
Bases: Discrete
Thin wrapper of gymnasium.spaces.Discrete.
Allow the user to give names to each dimension of the Discrete space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low, high, kwargs
|
see gymnasium.spaces.Box |
required | |
name
|
dict describing the name of different dimensions |
None
|
Example usage
observation_space = Discrete(n=3, name={'fixation': 0, 'stimulus': [1, 2]})
Source code in neurogym/utils/spaces.py
tasktools
¶
to_map
¶
get_idx
¶
get_periods_idx
¶
Function for defining task periods.
minmax_number
¶
Given input to the random_number_fn function, return min and max.