"""Implementation of useful database manipulations for the MDCEV model.Michel BierlaireFri Jul 25 2025, 16:45:19"""fromcollections.abcimportIterablefrombiogeme.databaseimportDatabase
[docs]defmdcev_count(database:Database,list_of_columns:list[str],new_column:str)->None:"""For the MDCEV models, we calculate the number of alternatives that are chosen, that is the number of columns with a non zero entry, and add this as a new column :param database: database to modify :param list_of_columns: list of columns containing the quantity of each good. :param new_column: name of the new column where the result is stored """database.dataframe[new_column]=database.dataframe[list_of_columns].apply(lambdax:(x!=0).sum(),axis=1)
[docs]defmdcev_row_split(database:Database,a_range:Iterable[int]|None=None)->list[Database]:""" For the MDCEV model, we generate a list of Database objects, each of them associated with a different row of the database, :param database: input database. :param a_range: specify the desired range of rows. :return: list of rows, each in a Database format """ifa_rangeisNone:the_range=range(len(database.dataframe))else:# Validate the provided rangemax_index=len(database.dataframe)-1ifany(i<0ori>max_indexforiina_range):raiseIndexError('One or more indices in a_range are out of the valid range.')the_range=a_rangerows_of_database=[Database(name=f'row_{i}',dataframe=database.dataframe.iloc[[i]])foriinthe_range]returnrows_of_database