def write_output(self, file_name): ''' Write a text file with the information in the megatableau object ''' file = open(file_name,"w") # Create and sort list of input keys, and dictionary of lists of output keys. inp_keys = list(self.tableau.keys()) inp_keys.sort() outp_keys = {} for i in inp_keys: outp_keys[i] = list(self.tableau[i].keys()) outp_keys[i].sort() # Add 1st line with constraint names file.write("\t\t\t\t\t\t") for constraint in self.constraints: file.write(constraint+"\t") file.write("\n") # Add 2nd line with constraint abbreviations file.write("\t\t\t\t\t\t") for constraint_abbrev in self.constraints_abbrev: file.write(constraint_abbrev+"\t") file.write("\n") # Add 3rd line with constraint weights, headers file.write("\t\tObs\tExp\tProb\tHarmony\t") for weight in self.weights: file.write(str(round(weight,3))+"\t") file.write("\n") # Add inputs, outputs, violations for inp in inp_keys: file.write(inp) # Add input zscore = optimizer.z_score(self.tableau,inp) if zscore == 0: raise Exception("Error: Cannot print a tableau before updating its maxent values.") total = 0 for outp in outp_keys[inp]: # Count total occurances of this UR total += self.tableau[inp][outp][0] for outp in outp_keys[inp]: obs = self.tableau[inp][outp][0] prob = self.tableau[inp][outp][2] / zscore exp = prob * total # Calculate expected counts harmony = math.log(self.tableau[inp][outp][2]) file.write("\t"+outp+"\t") # Add output file.write(str(obs)+"\t") # Add observed counts file.write(str(round(exp, 1))+"\t") # Add expected counts file.write(str(round(prob, 4))+"\t") # Add probability file.write(str(round(harmony, 4))+"\t") # Add harmony for c in range(0,len(self.constraints)):# Add violations if c in self.tableau[inp][outp][1].keys(): file.write(str(self.tableau[inp][outp][1][c])) file.write("\t") file.write("\n") # Add data probability file.write("\nLog probability: ") file.write(str(-optimizer.neg_log_probability(self.weights, self.tableau))) # Done file.close()
uses the mu and sigma values in the file instead of \ standard L1 and L2 priors to learn weights.') args = parser.parse_args() ##################################################################### ## Main code ##################################################################### # Construct MegaTableau mt = megatableau.MegaTableau(args.input_file_name) # If weights are provided, return the probability of the tableau if args.weights_file: mt.read_weights_file(args.weights_file) optimizer.update_maxent_values(mt.weights, mt.tableau) # Needed if -outfile print('\nLog probability of data: {}'.format(- optimizer.neg_log_probability (mt.weights, mt.tableau, args.L1, args.L2))) # If Gaussian priors file is provided, read in to a list if args.gaussian_priors_file: mt.read_priors_file(args.gaussian_priors_file) # Optimize mt.weights (unless weights were specified) if not args.weights_file: optimizer.learn_weights(mt, args.L1, args.L2, args.precision) # Output file if args.outfile: mt.write_output(args.outfile) # Read in testing items, generate predictions for them # NOTE: This step will only succeed if constraints can be evaluated