[docs]classFeatureReport(EncodingReport):""" Base class for reports that plot something about the reshaped feature values of any dataset. """def__init__(self,dataset:Dataset=None,result_path:Path=None,color_grouping_label:str=None,row_grouping_label=None,column_grouping_label=None,name:str=None,number_of_processes:int=1,error_function:str=None):super().__init__(dataset=dataset,result_path=result_path,name=name,number_of_processes=number_of_processes)self.x="feature"self.color=color_grouping_labelself.facet_row=row_grouping_labelself.facet_column=column_grouping_labelself.error_function=error_functiondef_generate_report_result(self)->ReportResult:PathBuilder.build(self.result_path)data_long_format=DataReshaper.reshape(self.dataset,self.dataset.get_label_names())table_result=self._write_results_table(data_long_format)report_output=self._safe_plot(data_long_format=data_long_format)output_tables=[table_result]ifreport_outputisNone:output_figures=Noneelifisinstance(report_output,tuple):output_figures=report_output[0]ifisinstance(report_output[0],list)else[report_output[0]]output_tables=report_output[1]else:output_figures=report_outputifisinstance(report_output,list)else[report_output]returnReportResult(name=self.name,output_figures=output_figures,output_tables=output_tables)def_write_results_table(self,data)->ReportOutput:table_path=self.result_path/f"feature_values.csv"data.to_csv(table_path,index=False)returnReportOutput(table_path,"feature values")
@abc.abstractmethoddef_plot(self,data_long_format)->ReportOutput:passdef_get_error_function(self):ifself.error_function=='std':returnself.stdelifself.error_function=='sem':returnself.semelse:logging.warning(f"{self.__class__.__name__}: unknown error function '{self.error_function}'. "f"Using 'std' as default.")returnself.stddef_get_grouped_data(self,data_long_format,cols_list):error_function=self._get_error_function()cols_list=[iforiincols_listifi]cols_list=list(set(cols_list))grouped_data=data_long_format.groupby(cols_list,as_index=False).agg({"value":['mean',error_function]})grouped_data.columns=grouped_data.columns.map(''.join)returngrouped_data
[docs]defcheck_prerequisites(self):location=self.__class__.__name__run_report=Trueifself.dataset.encoded_dataisNoneorself.dataset.encoded_data.examplesisNone:logging.warning(f"{location}: this report can only be created for an encoded dataset. {location} report will not be created.")run_report=Falseeliflen(self.dataset.encoded_data.examples.shape)!=2:logging.warning(f"{location}: this report can only be created for a 2-dimensional encoded dataset. {location} report will not be created.")run_report=Falseelse:legal_labels=list(self.dataset.get_label_names())labels=[self.color,self.facet_row,self.facet_column]forlabel_paraminlabels:iflabel_paramisnotNone:iflabel_paramnotinlegal_labels:logging.warning(f"{location}: undefined label '{label_param}'. Legal options are: {legal_labels}. {location} report will not be created.")run_report=Falsereturnrun_report