def add_column(self, column_name, group_name, operations): assert self._Nmax > 0 assert self._Nmax >= self._Nmin assert column_name not in self._columns and column_name not in self._columns_operations self._columns[column_name] = Content((self._Nmax - self._Nmin + 1, self._len_testing_set)) self._columns_not_implemented[column_name] = None # will be set to a bool self._rows_not_implemented[column_name] = {n: None for n in range(self._Nmax - self._Nmin + 1)} # will be set to a bool if group_name not in self._groups: self._groups[group_name] = list() self._group_names_sorted.append(group_name) # preserve the ordering provided by the user self._groups[group_name].append(column_name) if isinstance(operations, str): self._columns_operations[column_name] = (operations,) elif isinstance(operations, tuple): self._columns_operations[column_name] = operations else: raise ValueError("Invalid operation in PerformanceTable")
def _process(self): groups_content = collections.OrderedDict() for group in self._group_names_sorted: # Skip suppresed groups if group in self._suppressed_groups: continue # Populate all columns columns = list() for column in self._groups[group]: assert self._columns_not_implemented[column] in (True, False) if self._columns_not_implemented[column] is False: columns.append(column) if len(columns) == 0: continue # Storage for print table_index = list() # of strings table_header = dict() # from string to string table_content = dict() # from string to Content array column_size = dict() # from string to int # First column should be the reduced space dimension table_index.append("N") table_header["N"] = "N" table_content["N"] = list(range(self._Nmin, self._Nmax + 1)) column_size["N"] = max( [max([len(str(x)) for x in table_content["N"]]), len("N")]) # Then fill in with postprocessed data for column in columns: for operation in self._columns_operations[column]: # Set header if operation in ("min", "max"): current_table_header = operation + "(" + column + ")" current_table_index = operation + "_" + column elif operation == "mean": current_table_header = "gmean(" + column + ")" current_table_index = "gmean_" + column else: raise ValueError( "Invalid operation in PerformanceTable") table_index.append(current_table_index) table_header[current_table_index] = current_table_header # Compute the required operation of each column over the second index (testing set) table_content[current_table_index] = Content( (self._Nmax - self._Nmin + 1, )) for n in range(self._Nmin, self._Nmax + 1): assert self._rows_not_implemented[column][ n - self._Nmin] in (True, False) if self._rows_not_implemented[column][ n - self._Nmin] is False: if operation == "min": current_table_content = min( self._columns[column][n - self._Nmin, :]) elif operation == "mean": current_table_content = exp( mean( log(self._columns[column][ n - self._Nmin, :]))) elif operation == "max": current_table_content = max( self._columns[column][n - self._Nmin, :]) else: raise ValueError( "Invalid operation in PerformanceTable") table_content[current_table_index][ n - self._Nmin] = current_table_content else: table_content[current_table_index][ n - self._Nmin] = nan # Get the width of the columns column_size[current_table_index] = max([ max([ len(str(x)) for x in table_content[current_table_index] ]), len(current_table_header) ]) # Save content assert group not in groups_content groups_content[group] = (table_index, table_header, table_content, column_size) return groups_content