gyorilab / mira

MIRA modeling framework
BSD 2-Clause "Simplified" License
9 stars 7 forks source link

Cannot save a TemplateModel with distribution parameter expressions as a AMR #369

Closed liunelson closed 1 month ago

liunelson commented 1 month ago

I have this model: model_seirhd.json

I edited its parameters to try out the new distribution parameter feature:

new_parameters = {
    'u': Parameter(
        name = 'u', 
        display_name = 'u', 
        description = 'Population count uncertainty', 
        value = 1.0
    ),
    'b_shape': Parameter(
        name = 'b_shape', display_name = 'b_shape', description = 'Beta distribution shape parameter for the parameter "b"', 
        distribution = Distribution(
            type = 'Beta1', 
            parameters = {
                'alpha': sympy.Integer(1),
                'beta': sympy.Integer(10)
            }
        )
    ),
    'p_u': Parameter(
        name = 'p_u', 
        display_name = 'p_u', 
        description = 'Percent uncertainty on probability transitions', 
        value = 0.10
    )
}

model.parameters = model.parameters | new_parameters
model.parameters['b'] = Parameter(
    name = 'b',
    display_name = 'b',
    description = 'Infection rate',
    distribution = Distribution(
        type = 'InverseGamma1',
        parameters = {
            'shape': sympy.Symbol('b_shape') ** sympy.Float(1.5),
            'scale': 0.01
        }
    )
)

for p in model.parameters.keys():
    if p[0] == "r":
        v = model.parameters[p].value
        model.parameters[p].distribution.parameters = {
            'minimum': sympy.Float(v) - sympy.Symbol('u'),
            'maximum': sympy.Float(v) + sympy.Symbol('u')
        }

I get this error when I try to serialize the model: Screenshot 2024-09-19 at 5 23 49 PM

It seems that template_model_to_petri_json did not generate a string representation of the sympy.core.add.Add object in all the r* distribution parameters.

bgyori commented 1 month ago

These have to be objects of type SympyExprStr, not simple sympy.Expr. So e.g.,

'shape': SympyExprStr(sympy.Symbol('b_shape') ** sympy.Float(1.5)),
liunelson commented 1 month ago

Thanks, adding SympyExpStr to all the expressions fixed all the serialization errors that I had!