[docs]defaudit_variables(expression:Expression,database:Database)->AuditTuple:all_variables:list[Variable]=list_of_variables_in_expression(expression)list_of_errors=[]list_of_warnings=[]forvariableinall_variables:ifvariable.namenotindatabase.dataframe.columns:error_msg=f'Variable "{variable.name}" not found in the database. Available variables: {database.dataframe.columns}'list_of_errors.append(error_msg)returnAuditTuple(errors=list_of_errors,warnings=list_of_warnings)
[docs]defaudit_panel(expression:Expression,database:Database)->AuditTuple:list_of_errors=[]list_of_warnings=[]ifnotdatabase.is_panel():returnAuditTuple(errors=list_of_errors,warnings=list_of_warnings)all_variables:list[Variable]=list_of_variables_in_expression(expression)ifall_variablesandnotexpression.embed_expression(PanelLikelihoodTrajectory):error_msg=(f'Expression {expression} does not contain "PanelLikelihoodTrajectory" although the data has been 'f'declared to have a panel structure.')list_of_errors.append(error_msg)returnAuditTuple(errors=list_of_errors,warnings=list_of_warnings)
[docs]defaudit_chosen_alternative(choice:ExpressionOrNumeric,availability:dict[int,ExpressionOrNumeric],database:Database,use_jit:bool,)->AuditTuple:from.model_elementsimportModelElementsfrombiogeme.calculatorimportMultiRowEvaluator"""Checks all the rows in the database such that the chosen alternative is not available"""list_of_errors=[]dict_of_expressions={CHOICE_LABEL:choice}|{f'{AVAILABILITY_LABEL}{alt_id:.1f}':the_expressionforalt_id,the_expressioninavailability.items()}model_elements=ModelElements(expressions=dict_of_expressions,database=database,use_jit=use_jit)the_evaluator:MultiRowEvaluator=MultiRowEvaluator(model_elements=model_elements,numerically_safe=True,use_jit=use_jit)results:pd.DataFrame=the_evaluator.evaluate({})defchosen_unavailable(row):choice_id=row[CHOICE_LABEL]ifchoice_id==MISSING_VALUE:returnFalsereturnrow[f'{AVAILABILITY_LABEL}{choice_id:.1f}']==0invalid_indices=results.apply(chosen_unavailable,axis=1)problematic_rows=results[invalid_indices]list_of_warnings=[f'Row index {idx}: chosen alternative {row[CHOICE_LABEL]} is not available'foridx,rowinproblematic_rows.iterrows()]returnAuditTuple(errors=list_of_errors,warnings=list_of_warnings)