infer-actively / pymdp

A Python implementation of active inference for Markov Decision Processes
MIT License
450 stars 89 forks source link

Refactor get model dimensions from labels #126

Closed Arun-Niranjan closed 7 months ago

Arun-Niranjan commented 9 months ago

Currently when setting up a model using labels, it is not possible to set up both A and B matrix stubs from the same labels dictionary, due to the implied absence/presence of the actions key.

This isn't a great user experience - ideally someone should be able to specify all the labels in a single variable, and then generate all constituent parts of the model from that.

E.g. the following label spec:

model_labels = {
    "observations": {
        "species_observation": [
            "absent",
            "present",
        ],
        "budget_observation": [
            "high",
            "medium",
            "low",
        ],
    },
    "states": {
        "species_state": [
            "extant",
            "extinct",
        ],
    },
    "actions": {
        "conservation_action": [
            "manage",
            "survey",
            "stop",
        ],
    },
}

raises the following error from create_A_matrix_stub():

  File "/home/arunn/repos/pymdp-cookbook/venv/lib/python3.11/site-packages/pymdp/utils.py", line 457, in create_A_matrix_stub
    num_obs, _, num_states, _= get_model_dimensions_from_labels(model_labels)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 4)

This change:

conorheins commented 7 months ago

Looks great -- thanks @Arun-Niranjan !