def _str(self, phi_or_p="phi", tablefmt="grid", print_state_names=True): """ Generate the string from `__str__` method. Parameters ---------- phi_or_p: 'phi' | 'p' 'phi': When used for Factors. 'p': When used for CPDs. print_state_names: boolean If True, the user defined state names are displayed. """ string_header = list(map(lambda x: six.text_type(x), self.scope())) string_header.append('{phi_or_p}({variables})'.format(phi_or_p=phi_or_p, variables=','.join(string_header))) value_index = 0 factor_table = [] for prob in product(*[range(card) for card in self.cardinality]): if self.state_names and print_state_names: prob_list = ["{var}({state})".format( var=list(self.variables)[i], state=self.state_names[list( self.variables)[i]][prob[i]]) for i in range(len(self.variables))] else: prob_list = ["{s}_{d}".format(s=list(self.variables)[i], d=prob[i]) for i in range(len(self.variables))] prob_list.append(self.values.ravel()[value_index]) factor_table.append(prob_list) value_index += 1 return tabulate(factor_table, headers=string_header, tablefmt=tablefmt, floatfmt=".4f")
def _str(self, phi_or_p=None, tablefmt=None, sort=False, maxrows=None): """ Generate the string from `__str__` method. """ if phi_or_p is None: phi_or_p = r'\hat{phi}' if tablefmt is None: tablefmt = 'psql' string_header = list(self.scope()) string_header.append('{phi_or_p}({variables})'.format( phi_or_p=phi_or_p, variables=','.join(string_header))) factor_table = [] row_values = self.values.ravel() row_labels = self._row_labels(asindex=False) factor_table = [list(lbls) + [val] for lbls, val in zip(row_labels, row_values)] if sort: sortx = row_values.argsort()[::sort] factor_table = [factor_table[row] for row in sortx] if maxrows is not None and maxrows < len(factor_table): factor_table = factor_table[:maxrows] factor_table.append(['...'] * len(string_header)) return tabulate(factor_table, headers=string_header, tablefmt=tablefmt, floatfmt='.4f')
def _str(self, phi_or_p=None, tablefmt=None, sort=False, maxrows=None): """ Generate the string from `__str__` method. """ if phi_or_p is None: phi_or_p = r'\hat{phi}' if tablefmt is None: tablefmt = 'psql' string_header = list(self.scope()) string_header.append('{phi_or_p}({variables})'.format( phi_or_p=phi_or_p, variables=','.join(string_header))) factor_table = [] row_values = self.values.ravel() row_labels = self._row_labels(asindex=False) factor_table = [ list(lbls) + [val] for lbls, val in zip(row_labels, row_values) ] if sort: sortx = row_values.argsort()[::sort] factor_table = [factor_table[row] for row in sortx] if maxrows is not None and maxrows < len(factor_table): factor_table = factor_table[:maxrows] factor_table.append(['...'] * len(string_header)) return tabulate(factor_table, headers=string_header, tablefmt=tablefmt, floatfmt='.4f')
def _make_table_str(self, tablefmt="fancy_grid", print_state_names=True): headers_list = [] # build column headers evidence = self.variables[1:] evidence_card = self.cardinality[1:] if evidence: col_indexes = np.array(list(product(*[range(i) for i in evidence_card]))) if self.state_names and print_state_names: for i in range(len(evidence_card)): column_header = [evidence[i]] + ['{var}({state})'.format (var=evidence[i], state=self.state_names[evidence[i]][d]) for d in col_indexes.T[i]] headers_list.append(column_header) else: for i in range(len(evidence_card)): column_header = [evidence[i]] + ['{s}_{d}'.format(s=evidence[i], d=d) for d in col_indexes.T[i]] headers_list.append(column_header) # Build row headers if self.state_names and print_state_names: variable_array = [['{var}({state})'.format (var=self.variable, state=self.state_names[self.variable][i]) for i in range(self.variable_card)]] else: variable_array = [['{s}_{d}'.format(s=self.variable, d=i) for i in range(self.variable_card)]] # Stack with data labeled_rows = np.hstack((np.array(variable_array).T, self.get_cpd())).tolist() # No support for multi-headers in tabulate cdf_str = tabulate(headers_list + labeled_rows, tablefmt=tablefmt) return cdf_str
def _str(self, phi_or_p="phi", tablefmt="fancy_grid", print_state_names=True): """ Generate the string from `__str__` method. Parameters ---------- phi_or_p: 'phi' | 'p' 'phi': When used for Factors. 'p': When used for CPDs. print_state_names: boolean If True, the user defined state names are displayed. """ string_header = list(map(lambda x: six.text_type(x), self.scope())) string_header.append('{phi_or_p}({variables})'.format(phi_or_p=phi_or_p, variables=','.join(string_header))) value_index = 0 factor_table = [] for prob in product(*[range(card) for card in self.cardinality]): if self.state_names and print_state_names: prob_list = ["{var}({state})".format( var=list(self.variables)[i], state=self.state_names[list( self.variables)[i]][prob[i]]) for i in range(len(self.variables))] else: prob_list = ["{s}_{d}".format(s=list(self.variables)[i], d=prob[i]) for i in range(len(self.variables))] prob_list.append(self.values.ravel()[value_index]) factor_table.append(prob_list) value_index += 1 return tabulate(factor_table, headers=string_header, tablefmt=tablefmt, floatfmt=".4f")
def _str(self, phi_or_p, html=False): string_list = [] if html: html_string_header = '<table><caption>Factor</caption>' string_list.append(html_string_header) if html: html_string_header = '{tr}{variable_cols}{phi}'.format( tr='<tr', variable_cols=''.join(['<td><b>{var}</b></td>'.format(var=str(var)) for var in self.variables]), phi='<td><b>{phi_or_p}{vars}</b><d></tr>'.format(phi_or_P=phi_or_p, vars=', '.join([str(var) for var in self.variables]))) string_list.append(html_string_header) else: string_header = self.scope() string_header.append('{phi_or_p}({variables})'.format(phi_or_p=phi_or_p, variables=','.join(string_header))) # fun and gen are functions to generate the different values of # variables in the table. # gen starts with giving fun initial value of b=[0, 0, 0] then fun tries # to increment it # by 1. def fun(b, index=len(self.cardinality)-1): b[index] += 1 if b[index] == self.cardinality[index]: b[index] = 0 fun(b, index-1) return b def gen(): b = [0] * len(self.variables) yield b for i in range(np.prod(self.cardinality)-1): yield fun(b) value_index = 0 factor_table = [] for prob in gen(): prob_list = ["%s_%d" % (list(self.variables)[i], prob[i]) for i in range(len(self.variables))] if html: html_string = """<tr>%s<td>%4.4f</td></tr>""" % ( ''.join(["""<td>%s</td>""" % assignment for assignment in prob_list]), self.values[value_index]) string_list.append(html_string) else: prob_list.append(self.values[value_index]) factor_table.append(prob_list) value_index += 1 if html: return "\n".join(string_list) else: return tabulate(factor_table, headers=string_header, tablefmt="fancy_grid", floatfmt=".4f")
def __str__(self): table_list = [] indexes = np.array(list(product(*[range(i) for i in self.cardinality[1:]]))) scope = self.scope() for i in range(1, len(self.cardinality)): row = [scope[i]] row.extend(np.array(self.variables[row[0]])[indexes[:, i-1]].tolist()) table_list.append(row) variable_array = np.array([str([v.var, v.state]) for v in self.variables[self.variable]]) table_list.extend(np.column_stack((variable_array, self.get_cpd()))) return tabulate(table_list, tablefmt="fancy_grid")
def __str__(self): table_list = [] indexes = np.array( list(product(*[range(i) for i in self.cardinality[1:]]))) scope = self.scope() for i in range(1, len(self.cardinality)): row = [scope[i]] row.extend( np.array(self.variables[row[0]])[indexes[:, i - 1]].tolist()) table_list.append(row) variable_array = np.array( [str([v.var, v.state]) for v in self.variables[self.variable]]) table_list.extend(np.column_stack((variable_array, self.get_cpd()))) return tabulate(table_list, tablefmt="fancy_grid")
def _make_table_str(self, tablefmt="fancy_grid"): headers_list = [] # build column headers if self.evidence is not None: col_indexes = np.array(list(product(*[range(i) for i in self.evidence_card]))) for i in range(len(self.evidence_card)): column_header = [self.evidence[i]] + ['{s}_{d}'.format(s=self.evidence[i], d=d) for d in col_indexes.T[i]] headers_list.append(column_header) # Build row headers variable_array = [['{s}_{d}'.format(s=self.variable, d=i) for i in range(self.variable_card)]] # Stack with data labeled_rows = np.hstack((np.array(variable_array).T, self.get_cpd())).tolist() # No support for multi-headers in tabulate cdf_str = tabulate(headers_list + labeled_rows, tablefmt=tablefmt) return cdf_str
def _str(self, phi_or_p="phi", tablefmt="fancy_grid"): """ Generate the string from `__str__` method. Parameters ---------- phi_or_p: 'phi' | 'p' 'phi': When used for Factors. 'p': When used for CPDs. """ string_header = list(map(lambda x : six.text_type(x), self.scope())) string_header.append('{phi_or_p}({variables})'.format(phi_or_p=phi_or_p, variables=','.join(string_header))) value_index = 0 factor_table = [] for prob in product(*[range(card) for card in self.cardinality]): prob_list = ["{s}_{d}".format(s=list(self.variables)[i], d=prob[i]) for i in range(len(self.variables))] prob_list.append(self.values.ravel()[value_index]) factor_table.append(prob_list) value_index += 1 return tabulate(factor_table, headers=string_header, tablefmt=tablefmt, floatfmt=".4f")
def _str(self, phi_or_p, html=False): string_list = [] if html: html_string_header = '<table><caption>Factor</caption>' string_list.append(html_string_header) if html: html_string_header = '{tr}{variable_cols}{phi}'.format( tr='<tr', variable_cols=''.join([ '<td><b>{var}</b></td>'.format(var=str(var)) for var in self.variables ]), phi='<td><b>{phi_or_p}{vars}</b><d></tr>'.format( phi_or_P=phi_or_p, vars=', '.join([str(var) for var in self.variables]))) string_list.append(html_string_header) else: string_header = self.scope() string_header.append('{phi_or_p}({variables})'.format( phi_or_p=phi_or_p, variables=','.join(string_header))) # fun and gen are functions to generate the different values of # variables in the table. # gen starts with giving fun initial value of b=[0, 0, 0] then fun tries # to increment it # by 1. def fun(b, index=len(self.cardinality) - 1): b[index] += 1 if b[index] == self.cardinality[index]: b[index] = 0 fun(b, index - 1) return b def gen(): b = [0] * len(self.variables) yield b for i in range(np.prod(self.cardinality) - 1): yield fun(b) value_index = 0 factor_table = [] for prob in gen(): prob_list = [ "%s_%d" % (list(self.variables)[i], prob[i]) for i in range(len(self.variables)) ] if html: html_string = """<tr>%s<td>%4.4f</td></tr>""" % (''.join([ """<td>%s</td>""" % assignment for assignment in prob_list ]), self.values[value_index]) string_list.append(html_string) else: prob_list.append(self.values[value_index]) factor_table.append(prob_list) value_index += 1 if html: return "\n".join(string_list) else: return tabulate(factor_table, headers=string_header, tablefmt="fancy_grid", floatfmt=".4f")
def _make_table_str(self, tablefmt="fancy_grid", print_state_names=True): headers_list = [] header_list = [] # build column headers evidence = self.variables[1:] evidence_card = self.cardinality[1:] if evidence: col_indexes = np.array( list(product(*[range(i) for i in evidence_card]))) if self.state_names and print_state_names: for i in range(len(evidence_card)): column_header = [evidence[i]] + [ '{var}({state})'.format( var=evidence[i], state=self.state_names[evidence[i]][d]) for d in col_indexes.T[i] ] headers_list.append(column_header) header_list.append([ '{var}({state})'.format( var=evidence[i], state=self.state_names[evidence[i]][d]) for d in col_indexes.T[i] ]) else: for i in range(len(evidence_card)): column_header = [evidence[i]] + [ '{s}_{d}'.format(s=evidence[i], d=d) for d in col_indexes.T[i] ] headers_list.append(column_header) header_list.append([ '{s}_{d}'.format(s=evidence[i], d=d) for d in col_indexes.T[i] ]) # Build row headers if self.state_names and print_state_names: variable_array = [[ '{var}({state})'.format( var=self.variable, state=self.state_names[self.variable][i]) for i in range(self.variable_card) ]] else: variable_array = [[ '{s}_{d}'.format(s=self.variable, d=i) for i in range(self.variable_card) ]] #arcpy.AddMessage('jjj:'+str(variable_array)) # Stack with data self.dic = {} arcpy.AddMessage('variable_array:' + str(variable_array)) arcpy.AddMessage('header_list:' + str(header_list)) key = self.rtlstKey(variable_array, header_list) arcpy.AddMessage('key:' + str(key)) var = variable_array[0][0] formatvar = var[:-3] # if 'DW' in var : # formatvar = var[:-5] # else: # values = self.get_values().tolist() # arcpy.AddMessage('values:' + str(values)) if formatvar == 'DRW' or formatvar == 'DT' or formatvar == 'DRD' or formatvar == 'G' or formatvar == 'DW' or formatvar == 'A' or formatvar == 'R' or formatvar == 'IW' or formatvar == 'DNR' or formatvar == 'F' or formatvar == 'SD': lst_values = [] arcpy.AddMessage('if:' + str(values)) for i in range(len(values)): for j in range(len(values[i])): lst_values.append(values[i][j]) for i in range(len(lst_values)): self.dic[key[0][i]] = lst_values[i] else: newvalues = [] cnt = len(values) arcpy.AddMessage('else:' + str(cnt)) if cnt == 2: newvalues = values[0] + values[1] elif cnt == 3: newvalues = values[0] + values[1] + values[2] elif cnt == 4: newvalues = values[0] + values[1] + values[2] + values[3] table = {} for i in range(len(key)): if len(key) == len(newvalues): table[key[i]] = newvalues[i] else: table[key[i]] = values[0][0] self.dic[variable_array[0][0][:-3]] = table arcpy.AddMessage('self.dic:' + str(self.dic)) #fileObject = open(r'C:\Users\59308\Desktop\dic.txt' + '.txt', 'w') #fileObject.write(str(dic)) #fileObject.write('\n') #fileObject.close() #arcpy.AddMessage('jjj:' + str(dic)) #arcpy.AddMessage('jjj:' + str(key)) #arcpy.AddMessage('kkk:' + str(newvalues)) # arcpy.AddMessage('iii:' + str(len(key))) # arcpy.AddMessage('ooo:' + str(len(newvalues))) labeled_rows = np.hstack( (np.array(variable_array).T, self.get_values())).tolist() # arcpy.AddMessage('labeled_rows:'+str(labeled_rows)) # No support for multi-headers in tabulate cdf_str = tabulate(headers_list + labeled_rows, tablefmt=tablefmt) return cdf_str