Source code for biogeme.bayesian_estimation.check_shape
"""Decorator for the builder of PyMc modelMichel BierlaireMon Nov 03 2025, 15:44:53"""importpandasaspdimportpytensor.tensorasptfrompytensor.raise_opimportAssert
[docs]defcheck_shape(func):defwrapper(dataframe:pd.DataFrame,*args,**kwargs):result=func(dataframe,*args,**kwargs)# Ensure a PyTensor variableresult=pt.as_tensor_variable(result)# Static rank check: must be 1-D (per-observation vector)ifresult.ndim!=1:raiseValueError(f"Numeric builder must return a 1-D tensor (N,), got ndim={result.ndim} with static shape {result.type.shape}.")# Runtime length check (symbolic): length == len(dataframe)n_obs=pt.as_tensor_variable(len(dataframe),dtype="int64")cond=pt.eq(result.shape[0],n_obs)result=Assert(f"Numeric builder length mismatch: expected {len(dataframe)}, got dynamic length")(result,cond)returnresultreturnwrapper