Source code for PyBMF.utils.collective_display_utils
from.collective_utilsimportsplit_factor_list
[docs]defsort_matrices(Xs,factors):'''Sort out matrices. Transpose the matrices when necessary and return the positions. '''rows,cols=split_factor_list(factors=factors)Xs_transpose=[]Xs_positions=[]foriinrange(len(factors)):x=Xs[i]a,b=factors[i]needs_transpose=aincolsandbinrowsifneeds_transpose:x,a,b=x.T,b,a# position in the concatenated matrixr,c=rows.index(a),cols.index(b)Xs_transpose.append(x)Xs_positions.append([r,c])returnXs_transpose,Xs_positions
[docs]defget_settings(Xs,factors,Us=None):'''Get display settings. Used in the show_matrix() wrapper for CMF models. Parameters ---------- Xs : list of spmatrix or ndarray factors : list of int list Us : list of spmatrix or ndarray, optional a, b, f: factor id note that factor id may not be equal to the index in Us and factor_info, especially when the factor id does not start from 0. split_factor_list only accepts the compete factors list. '''settings=[]Xs_transpose,Xs_positions=sort_matrices(Xs,factors)fori,Xinenumerate(Xs):x=Xs_transpose[i]r,c=Xs_positions[i]ifX.shape==x.shape:settings.append((x,[r,c],f"Xs[{i}]"))elifX.shape==x.shape[::-1]:settings.append((x,[r,c],f"Xs[{i}].T"))else:raiseValueError("X should be either transposed or not.")ifUsisNone:returnsettingsrows,cols=split_factor_list(factors)fori,Uinenumerate(Us):ifiinrows:r,c=rows.index(i),len(cols)settings.append((U,[r,c],f"Us[{i}]"))elifiincols:r,c=len(rows),cols.index(i)settings.append((U.T,[r,c],f"Us[{i}].T"))returnsettings