environment
Environment
Class to represent an olfactory environment.
It is defined based on an olfactory data set provided as either a numpy file or an array directly with shape time, y, x. From this environment, the various parameters are applied in the following order:
- The source position is set
- The margins are added and the shape (total size) of the environment are set.
- The data file's x and y components are squished and streched the to fit the inter-marginal shape of the environment.
- The source's position is also moved to stay at the same position within the data.
- The multiplier is finally applied to modify the data file's x and y components a final time by growing or shrinking the margins to account for the multiplier. (The multiplication applies with the source position as a center point)
Note: to modify the shape of the data file's x and y components the OpenCV library's resize function is used. And the interpolation method is controlled by the interpolation_method parameter.
Then, the starting probability map is built. Either an array can be provided directly or preset option can be chosen:
- 'data_zone': The agent can start at any point in the data_zone (after all the modification parameters have been applied)
- 'odor_present': The agent can start at any point where an odor cue above the odor_present_threshold can be found at any timestep during the simulation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_file
|
str or ndarray
|
The dataset containing the olfactory data. It can be provided as a path to a file containing said array. |
required |
data_source_position
|
list or ndarray
|
The center point of the source provided as a list or a 1D array with the components being x,y. This position is computed in the olfactory data zone (so excluding the margins). |
required |
source_radius
|
float
|
The radius from the center point of the source in which we consider the agent has reached the source. |
1.0
|
layers
|
bool or list[int] or list[str]
|
Whether or not the data provided contains layers or not. If a list of strings is provided, it will be either used to name the layers found (if numpy data), or it is used to querry the datasets of the h5 file. |
False
|
shape
|
list or ndarray
|
A 2-element array or list of how many units should be kept in the final array (including the margins). As it should include the margins, the shape should be strictly larger than the sum of the margins in each direction. By default, the shape of the olfactory data will be maintained. |
None
|
margins
|
int or list or ndarray
|
How many units have to be added to the data as margins. (Before the multiplier is applied) If a unique element is provided, the margin will be this same value on each side. If a list or array of 2 elements is provided, the first number will be vertical margins (y-axis), while the other will be on the x-axis (horizontal). |
0
|
multiplier
|
list or ndarray
|
A 2-element array or list of how much the odor field should be streched in each direction. If a value larger than 1 is provided, the margins will be reduced to accomodate for the larger size of the olfactory data size. And inversly, less than 1 will increase the margins. By default, the multipliers will be set to 1.0. |
[1.0,1.0]
|
interpolation_method
|
Nearest or Linear or Cubic
|
The interpolation method to be used in the case the data needs to be reshaped to fit the shape, margins and multiplier parameters. By default, it uses Bi-linear interpolation. The interpolation is performed using the OpenCV library. |
'Linear'
|
preprocess_data
|
bool
|
Applicable only for data_file being a path to a h5 file. Whether to reshape of the data at the creation of the environment. Reshaping the data ahead of time will require more processing at the creation and more memory overall. While if this is disabled, when gathering observations, more time will be required but less memory will need to be used at once. |
False
|
boundary_condition
|
stop or wrap or wrap_vertical or wrap_horizontal or clip
|
How the agent should behave at the boundary. Stop means for the agent to stop at the boundary, if the agent tries to move north while being on the top edge, it will stay in the same state. Wrap means for the borders to be like portals, when entering on one side, it reappears on the other side. Wrap can be specified to be only vertically or horizontally |
'stop'
|
start_zone
|
odor_present or data_zone or ndarray
|
Either an array or a string representing how the starting probabilities should be constructed. - odor_present: The start probabilities will be uniform where odor cues can be found above 0 (or a given odor_present_threshold) - data_zone: Uniform over the data zone, so without the margins. Note that the points within the source radius will be excluded from this probability grid. |
'data_zone'
|
odor_present_threshold
|
float
|
An olfactory threshold, under which the odor is considered too low to be noticed. It is used only to build the starting zone if the 'odor_present' option is selected. |
None
|
name
|
str
|
A custom name to be given to the agent.
If it is not provided, by default it will have the format:
|
None
|
seed
|
int
|
For reproducible randomness. |
12131415
|
Attributes:
Name | Type | Description |
---|---|---|
data |
ndarray
|
An array containing the olfactory data after the modification parameters have been applied. |
data_file_path |
str
|
If the data is loaded from a path, the path will be recorded here. |
data_source_position |
ndarray
|
The position of the source in the original data file (after modifications have been applied). |
layers |
ndarray
|
A numbered list of the IDs of the layers. |
layer_labels |
list[str]
|
A list of how the layers are named. |
has_layers |
bool
|
Whether or not the environment is made up of layers. |
margins |
ndarray
|
An array of the margins vertically and horizontally (after multiplier is applied). |
timestamps |
int
|
The amount of timeslices available in the environment. |
data_shape |
tuple[int]
|
The shape of the data's odor field (after modifications have been applied). |
dimensions |
int
|
The amount of dimensions of the physical space of the olfactory environment. |
shape |
tuple[int]
|
The shape of the environment. It is a tuple of the size in each axis of the environment. |
data_bounds |
ndarray
|
The bounds between which the original olfactory data stands in the coordinate system of the environment (after modifications have been applied). |
source_position |
ndarray
|
The position of the source in the padded grid (after modifications have been applied). |
source_radius |
float
|
The radius of the source. |
interpolation_method |
str
|
The interpolation used to modify the shape of the original data. |
data_processed |
bool
|
Whether the data was processed (ie the shape is at it should be) or not. |
boundary_condition |
str
|
How the agent should behave when reaching the boundary. |
start_probabilities |
ndarray
|
A probability map of where the agent is likely to start within the environment. Note: Zero within the source radius. |
start_type |
str
|
The type of the start probability map building. For instance: 'data_zone', 'odor_present', or 'custom' (if an array is provided). |
odor_present_threshold |
float
|
The threshold used to uild the start probabilities if the option 'odor_present' is used. |
name |
str
|
The name set to the agent as defined in the parameters. |
saved_at |
str
|
If the environment is saved, the path at which it is saved will be recorded here. |
on_gpu |
bool
|
Whether the environment's arrays are on the gpu's memory or not. |
seed |
int
|
The seed used for the random operations (to allow for reproducability). |
rnd_state |
RandomState
|
The random state variable used to generate random values. |
cpu_version |
Environment
|
An instance of the environment on the CPU. If it already is, it returns itself. |
gpu_version |
Environment
|
An instance of the environment on the CPU. If it already is, it returns itself. |
Source code in olfactory_navigation/environment.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 225 226 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 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 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
|
_data_is_numpy
property
Wheter the data is a numpy array or not.
cpu_version
property
A version of the Environment on the CPU. If the environment is already on the CPU it returns itself, otherwise the to_cpu function is called to generate a new one.
data
property
The whole dataset with the right shape. If not preprocessed to modify its shape the data will be processed when querrying this object.
gpu_version
property
A version of the Environment on the GPU. If the environment is already on the GPU it returns itself, otherwise the to_gpu function is called to generate a new one.
distance_to_source(point, metric='manhattan')
Function to compute the distance(s) between given points and the source point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point
|
ndarray
|
A single or an Nx2 array containing N points. |
required |
metric
|
manhattan
|
The metric to use to compute the distance. |
'manhattan'
|
Returns:
Name | Type | Description |
---|---|---|
dist |
float or ndarray
|
A single distance or a list of distance in a 1D distance array. |
Source code in olfactory_navigation/environment.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
get_observation(pos, time=0, layer=0)
Function to get an observation at a given position on the grid at a given time. A set of observations can also be requested, either at a single position for multiple timestamps or with the same amoung of positions as timestamps provided.
Note: The position will not be checked against boundary conditions; if a position is out-of-bounds it will simply return 0.0!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The position or list of positions to get observations at. |
required |
time
|
int or ndarray
|
A timestamp or list of timestamps to get the observations at. |
0
|
layer
|
int or ndarray
|
A layer or list of timestamps to get the observations at. Note: If the environment doesnt have layers, this parameter will be ignored. |
0
|
Returns:
Name | Type | Description |
---|---|---|
observation |
float or ndarray
|
A single observation or list of observations. |
Source code in olfactory_navigation/environment.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 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 |
|
load(folder)
classmethod
Function to load an environment from a given folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
str
|
The folder of the Environment. |
required |
Returns:
Name | Type | Description |
---|---|---|
loaded_env |
Environment
|
The loaded environment. |
Source code in olfactory_navigation/environment.py
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
modify(data_source_position=None, source_radius=None, shape=None, margins=None, multiplier=None, interpolation_method=None, boundary_condition=None)
Returns a copy of the environment with one or more parameters modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_source_position
|
list | ndarray | None
|
A new position for the source relative to the data file. |
None
|
source_radius
|
float | None
|
A new source radius. |
None
|
shape
|
list | ndarray | None
|
A new shape of environment. |
None
|
margins
|
int | list | ndarray | None
|
A new set of margins. |
None
|
multiplier
|
list | ndarray | None
|
A new multiplier to be applied to the data file (this will in turn increase or reduce the margins). |
None
|
interpolation_method
|
str | None
|
A new interpolation method to be used. |
None
|
boundary_condition
|
str | None
|
New boundary conditions for how the agent should behave at the edges. |
None
|
Returns:
Type | Description |
---|---|
modified_environment
|
A copy of the environment where the modified parameters have been applied. |
Source code in olfactory_navigation/environment.py
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 |
|
modify_scale(scale_factor)
Function to modify the size of the environment by a scale factor. Everything will be scaled this factor. This includes: shape, margins, source radius, and data shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale_factor
|
float
|
By how much to modify the size of the current environment. |
required |
Returns:
Name | Type | Description |
---|---|---|
modified_environment |
Environment
|
The environment with the scale factor applied. |
Source code in olfactory_navigation/environment.py
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
|
move(pos, movement)
Applies a movement vector to a position point and returns a new position point while respecting the boundary conditions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The start position of the movement. |
required |
movement
|
ndarray
|
A 2D movement vector. |
required |
Returns:
Name | Type | Description |
---|---|---|
new_pos |
ndarray
|
The new position after applying the movement. |
Source code in olfactory_navigation/environment.py
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 |
|
plot(frame=0, layer=0, ax=None)
Simple function to plot the environment with a single frame of odor cues. The starting zone is also market down with a blue contour. The source of the odor is marked by a red circle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame
|
int
|
The frame of odor cues to print. |
0
|
layer
|
int
|
The layer of the odor cues to print. (Ignored if the environment is not layered.) |
0
|
ax
|
Axes
|
An ax on which the environment can be plot. |
None
|
Source code in olfactory_navigation/environment.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
random_start_points(n=1)
Function to generate n starting positions following the starting probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
How many random starting positions to generate |
1
|
Returns:
Name | Type | Description |
---|---|---|
random_states_2d |
ndarray
|
The n random 2d points in a n x 2 array. |
Source code in olfactory_navigation/environment.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
|
save(folder=None, save_arrays=False, force=False)
Function to save the environment to the memory.
By default it saved in a new folder at the current path in a new folder with the name 'Env-
The numpy arrays of the environment (grid and start_probabilities) can be saved or not. If not, when the environment is loaded it needs to be reconstructed from the original data file. The arrays are saved to .npy files along with the METADATA file.
If an environment of the same name is already saved, the saving will be interupted. It can however be forced with the force parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
str
|
The folder to which to save the environment data. If it is not provided, it will be created in the current folder. |
None
|
save_arrays
|
bool
|
Whether or not to save the numpy arrays to memory. (The arrays can be heavy) |
False
|
force
|
bool
|
In case an environment of the same name is already saved, it will be overwritten. |
False
|
Source code in olfactory_navigation/environment.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
|
source_reached(pos)
Checks whether a given position is within the source radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The position to check whether in the radius of the source. |
required |
Returns:
Name | Type | Description |
---|---|---|
is_at_source |
bool
|
Whether or not the position is within the radius of the source. |
Source code in olfactory_navigation/environment.py
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 |
|
to_cpu()
Function to send the numpy arrays of the environment to the cpu memory. It returns a new instance of the Environment with the arrays as numpy arrays.
Returns:
Name | Type | Description |
---|---|---|
cpu_environment |
Environment
|
A new environment instance where the arrays are on the cpu memory. |
Source code in olfactory_navigation/environment.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
|
to_gpu()
Function to send the numpy arrays of the environment to the gpu memory. It returns a new instance of the Environment with the arrays as cupy arrays.
Returns:
Name | Type | Description |
---|---|---|
gpu_environment |
Environment
|
A new environment instance where the arrays are on the gpu memory. |
Source code in olfactory_navigation/environment.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
_resize_array(array, new_shape, interpolation)
Function to resize a numpy array in the same fashion as the cv2.resize function. It uses the scipy.interpolate
Source code in olfactory_navigation/environment.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|