Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make safe obj_array_from_list to address issue #130 with test #131

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pymdp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ def obj_array_from_list(list_input):
"""
Takes a list of `numpy.ndarray` and converts them to a `numpy.ndarray` of `dtype = object`
"""
return np.array(list_input, dtype = object)
arr = obj_array(len(list_input))
for i, item in enumerate(list_input):
arr[i] = item
return arr

def process_observation_seq(obs_seq, n_modalities, n_observations):
"""
Expand Down
28 changes: 28 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Agent Class

__author__: Conor Heins, Alexander Tschantz, Daphne Demekas, Brennan Klein

"""

import unittest

import numpy as np

from pymdp import utils

class TestUtils(unittest.TestCase):
def test_obj_array_from_list(self):
"""
Tests `obj_array_from_list`
"""
# make arrays with same leading dimensions. naive method trigger numpy broadcasting error.
arrs = [np.zeros((3, 6)), np.zeros((3, 4, 5))]
obs_arrs = utils.obj_array_from_list(arrs)

self.assertTrue(all([np.all(a == b) for a, b in zip(arrs, obs_arrs)]))

if __name__ == "__main__":
unittest.main()
Loading