[docs]defcreate_df(subset:list[DelayedSample],attributes:list[str],bins:list[int])->pd.DataFrame:"""Create a dataframe from a list of Samples and for a list of attributes, if "age" is in attribute, a new column "ageGroup" is added to the dataframe which assign an ageGroup to the patient. For the creation of "ageGroup" column bins has to be a list of int which define the different categories of age. :param subset: a list of Samples :param attributes: a list of attribute which are attribute of Samples :param bins: definition of the age categories """data:dict={name:list()fornameinattributes}for_instanceinsubset:forattributeinattributes:ifhasattr(_instance,attribute):data[attribute].append(getattr(_instance,attribute))df=pd.DataFrame(data=data)if"age"inattributes:df["ageGroup"]=pd.cut(df["age"],bins=bins,labels=None,right=True)df["ageGroup"].cat.add_categories("unknown").fillna("unknown")returndf
[docs]defmake_rst_tabulate(name:str,table:pd.DataFrame,min_column_width:int=0):"""Create Rst table from pandas.DataFrame. :param name: title of the table :param table: dataframe to be tabulate and save :param min_column_width: fixed a minimum column width """title="".join(name)len_title=len(title)iflen_title<min_column_width:title=title.ljust(min_column_width)index_name=list(table.T.index)ifisinstance(index_name[0],int):index=[" "]*len(index_name)else:index=list(table.T.index.get_level_values(0))df=pd.Series(index,name=str(title))table=pd.concat([df,table.T.reset_index(drop=True)],axis=1)tabulate_table=textwrap.indent(tabulate(table,headers="keys",tablefmt="rst",floatfmt=".2f",showindex=False,)," ",)returntabulate_table