def execute(self): parser = argparse.ArgumentParser() parser.add_argument( 'CSVFILE', help='path to csv file with historical data.') parser.add_argument( 'ESTVAL', help='an estimated value') args = parser.parse_args() file_path = args.CSVFILE estimated_value = float(args.ESTVAL) csv_data = io.read_csv_file(file_path) if not csv_data: print 'ERROR: No data' sys.exit(1) columns = csv_data[0].keys() x_column = io.choose_from_list('X Column:', columns) y_column = io.choose_from_list('Y Column:', columns) x_data = [float(each[x_column]) for each in csv_data if each[x_column]] y_data = [float(each[y_column]) for each in csv_data if each[y_column]] print x_data, y_data = probe.trim_to_equal_length(x_data, y_data) print 'X DATA: {}'.format(x_data) print 'Y DATA: {}'.format(y_data) print beta_0 = statistics.beta_0(x_data, y_data) print u'\u03B20: {}'.format(beta_0) beta_1 = statistics.beta_1(x_data, y_data) print u'\u03B21: {}'.format(beta_1) integ = integration.Integrator(20, 0.000001) tdist = statistics.make_t_distribution(len(x_data) - 2) itdist = lambda x: integ.integrate_minus_infinity_to(tdist, x) std_dev = ( statistics.standard_deviation_around_regression(x_data, y_data) ) print "StdDev: ", std_dev projection = beta_0 + beta_1 * estimated_value print 'Projection: ', projection print 't(70 percent): ', integration.approximate_inverse(itdist, 0.85) print 't(90 percent): ', integration.approximate_inverse(itdist, 0.95) range70 = statistics.prediction_range( estimated_value, 0.85, x_data, y_data ) range90 = statistics.prediction_range( estimated_value, 0.95, x_data, y_data ) print 'Range(70 percent) =', projection + range70, \ 'UPI =', projection - range70, 'LPI =', range70 print 'Range(90 percent) =', projection + range90, \ 'UPI =', projection - range90, 'LPI =', range90
def execute(self): print '8A: Load CSV file and sort by selected column' print file_name = io.prompt_existant_file_name('CSV file to sort: ') data = io.read_csv_file(file_name) if not data: print 'ERROR: File contains no data.' sys.exit(1) column_names = data[0].keys() sort_column = io.choose_from_list('Column to sort on', column_names) try: for each in data: each[sort_column] = float(each[sort_column]) except ValueError: print 'ERROR: Column {} contains non-integer value.'.format( sort_column) sys.exit(1) sorted_data = sorted(data, key=lambda item: item[sort_column]) table = display_table.DisplayTable(column_names) for each in sorted_data: table.add_row(each.values()) table.display()
def main(): """The application entry point""" print 'EXERCISE 1A' print '===========' print 'This program takes a CSV file, asks you to select a row from that' print 'file, and then computes the mean and standard deviation of the' print 'values in that row.' print file_path = io.get_and_confirm_input('Enter csv file with values: ') data = io.read_csv_file(file_path) if not data: raise RuntimeError('No data found in file {}'.format(file_path)) column = io.choose_from_list( 'Which column would you like to use:', data[0].keys()) if column not in data[0]: raise RuntimeError('Invalid column {}'.format(column)) values = linked_list.LinkedList() for each in data: values.insert(each[column]) for each in values: print each print 'Mean: ', statistics.mean(values) print 'Std Dev: ', statistics.standard_deviation(values)
def main(): """Application entry point""" print "This program will read or write numbers to or from a given file." print 'Read - Read and display numbers in a file' print 'Write - Input numbers to write to a file' print 'Add - Go through file line-by-line and add new numbers' print 'Modify - Go through file line by line and modify numbers' mode = io.choose_from_list( 'Choose a mode', ['Read', 'Write', 'Add', 'Modify'] ) mode = mode.lower() if mode in ['write']: file_path = io.prompt_valid_file_name('Please enter a file name: ') else: file_path = io.prompt_existant_file_name('Please enter a file name: ') if mode == 'read': read_file(file_path) elif mode == 'write': write_file(file_path) elif mode == 'add': add_file(file_path) elif mode == 'modify': modify_file(file_path) else: raise RuntimeError('Invalid option {}'.format(mode))
def execute(self): """Run the program""" parser = argparse.ArgumentParser() parser.add_argument('CSVFILE', help='path to csv file with data.') args = parser.parse_args() csv_data = io.read_csv_file(args.CSVFILE) if not csv_data: print 'ERROR: Invalid csv data file.' sys.exit(1) columns = csv_data[0].keys() x_column = io.choose_from_list('X Column:', columns) y_column = io.choose_from_list('Y Column:', columns) x_data = [float(each[x_column]) for each in csv_data if each[x_column]] y_data = [float(each[y_column]) for each in csv_data if each[x_column]] print 'R:', statistics.correlation(x_data, y_data) print 'T:', statistics.t_value(x_data, y_data) print 'Significance:', statistics.significance(x_data, y_data)
def execute(self): """Ask the user for a mode selection and then execute the object corresponder to that mode.""" print 'This program is used to edit lists of numerical values in text' print 'files. It can be used in any of the following modes:' print 'Read - Read and display lists of numbers in a file.' print 'Write - Input lists of numbers to write to a file.' print 'Add - Go through a file line-by-line and add new values.' print 'Modify - Go through a file line-by-line and modify values.' mode = io.choose_from_list('Choose a mode', self.mode_map.keys()) mode_class = self.mode_map[mode] mode_class().execute(self.MAXIMUM_LIST_LENGTH)
def execute(self): parser = argparse.ArgumentParser() parser.add_argument( 'CSVFILE', help='path to csv file with historical data.') args = parser.parse_args() file_path = args.CSVFILE csv_data = io.read_csv_file(file_path) if not csv_data: print 'ERROR: No data' sys.exit(1) columns = csv_data[0].keys() x_column = io.choose_from_list('X Column:', columns) y_column = io.choose_from_list('Y Column:', columns) x_data = [float(each[x_column]) for each in csv_data] y_data = [float(each[y_column]) for each in csv_data] #x_data, y_data = statistics.remove_outliers(x_data, y_data) print print 'X DATA: {}'.format(x_data) print 'Y DATA: {}'.format(y_data) print beta_0 = statistics.beta_0(x_data, y_data) print u'\u03B20: {}'.format(beta_0) warnings = statistics.beta_0_warnings(beta_0) if warnings: print 'WARNINGS:' print '\n'.join(warnings) print beta_1 = statistics.beta_1(x_data, y_data) print u'\u03B21: {}'.format(beta_1) warnings = statistics.beta_1_warnings(beta_1) if warnings: print 'WARNINGS:' print '\n'.join(warnings) print
def get_test_column(self, data): """Prompt the user to select the column from the given data to test. Arguments: data(dict): An association column name => data Returns: list: The data to be tested. Raises: ValueError: If selected column does not contain only numeric data. """ column_name = io.choose_from_list('Choose test column', data[0].keys()) return [float(each[column_name]) for each in data]
def execute(self, _): """Sort file contents""" input_file = io.prompt_existant_file_name('File to sort:') data = io.read_lists_from_file(input_file) if not data: print 'ERROR: File {} is empty.'.format(input_file) sys.exit() output_file = self.prompt_output_file('File to output to:') sort_column = io.choose_from_list('Sort column', range(len(data[0]))) for idx, value in enumerate(data): try: data[idx][sort_column] = float(data[idx][sort_column]) except ValueError: print 'ERROR: Column {} is non-numeric'.format(sort_column) sys.exit() sorted_data = sort.merge_sort(data, key=lambda x: x[sort_column]) io.write_lists_to_file(output_file, sorted_data) print 'Sorted contents written to {}'.format(output_file)
def modify_file(file_path): """Modifies the values in the given file. Arguments: file_path(str): A file to be modified """ numbers = io.read_numbers_from_file(file_path) output_file = prompt_for_output_file(file_path) # Go through each number one at a time results = [] for idx, each in enumerate(numbers): # Display the results up to this point if results: print 'Numbers So Far:' for num in results: print num # Otherwise display next number print 'Next Number:' print each # Let the user choose what to do with the number choice = io.choose_from_list( 'What would you like to do', ['Keep', 'Modify', 'Delete', 'Keep Rest'] ) choice = choice.lower() if choice == 'keep': results.append(each) elif choice == 'modify': number = io.get_and_confirm_input("New value: ") results.append(float(number)) elif choice == 'keep rest': results += numbers[idx:] break elif choice == 'delete': # Do nothing pass # Write the updated numbers to the output file io.write_numbers_to_file(output_file, results) print 'Results written to ', output_file
def main(): """Application entry point""" print "This program will read or write numbers to or from a given file." file_path = io.get_and_confirm_input("Please enter a file name: ") print "Read - Read and display numbers in a file" print "Write - Input numbers to write to a file" print "Add - Go through file line-by-line and add new numbers" print "Modify - Go through file line by line and modify numbers" mode = io.choose_from_list("Choose a mode", ["Read", "Write", "Add", "Modify"]) mode = mode.lower() if mode == "read": read_file(file_path) elif mode == "write": write_file(file_path) elif mode == "add": add_file(file_path) elif mode == "modify": modify_file(file_path) else: raise RuntimeError("Invalid option {}".format(mode))
def execute(self, maximum_list_length): """Prompt user for input file and output file and go through input file line-by-line adding data. Arguments: maximum_list_length(int): The maximum allowed list length. """ input_file = io.prompt_existant_file_name( 'Please enter file to read from: ') data = io.read_lists_from_file(input_file) output_file = prompt_for_output_file(input_file) updated_results = [] for index, each in enumerate(data): if updated_results: print_results_so_far(updated_results) print 'Next Item:' print each choice = io.choose_from_list( 'What would you like to do', ['Keep', 'Add Item Before', 'Add Item After', 'Keep Rest']) if choice == 'Keep': updated_results.append(each) elif choice == 'Add Item Before': new_list = io.get_and_confirm_list( 'New item: ', max_list_length=maximum_list_length) updated_results.append(new_list) updated_results.append(each) elif choice == 'Add Item After': new_list = io.get_and_confirm_list( 'New item: ', max_list_length=maximum_list_length) updated_results.append(each) updated_results.append(new_list) else: updated_results += data[index:] io.write_lists_to_file(output_file, updated_results) print 'Results written to {}'.format(output_file)
def add_file(file_path): """Go through file line-by-line and ask for additions after given line. Arguments: file_path(str): The path to the file """ numbers = io.read_numbers_from_file(file_path) output_file = prompt_for_output_file(file_path) # Go through each number one at a time results = [] for idx, each in enumerate(numbers): # Display the results up to this point if results: print 'Numbers So Far:' for num in results: print num print 'Next number:' print each # Get the choice choice = io.choose_from_list( 'What would you like to do:', ['Keep', 'Add Number After', 'Keep Rest']) choice = choice.lower() if choice == 'keep': results.append(each) elif choice == 'add number after': # Get the new number and add it to the list results.append(each) number = io.get_and_confirm_float("New number: ") results.append(float(number)) elif choice == 'keep rest': # Add remaining numbers to results and exit loop results += numbers[idx:] break # Write the updated numbers to the output file io.write_numbers_to_file(output_file, results) print 'Results written to', output_file
def execute(self, maximum_list_length): """Prompt user for input and output files then go through input file line-by-line, updated values, and write results to output file. Arguments: maximum_list_length(int): The maximum allowed list length """ input_file = io.prompt_existant_file_name( 'Please enter file to modify: ') output_file = prompt_for_output_file(input_file) data = io.read_lists_from_file(input_file) updated_results = [] for index, each in enumerate(data): if updated_results: print_results_so_far(updated_results) print 'Next Item:' print each choice = io.choose_from_list( 'What would you like to do', ['Keep', 'Change', 'Delete', 'Keep Rest']) if choice == 'Keep': updated_results.append(each) elif choice == 'Change': new_item = io.get_and_confirm_list( 'New Item: ', max_list_length=maximum_list_length) updated_results.append(new_item) elif choice == 'Delete': pass else: updated_results += data[index:] io.write_lists_to_file(output_file, updated_results) print 'Results written to {}'.format(output_file)
def get_test_column(self, data): """Prompt the user to select the column from the given data to test. Arguments: data(dict): An association column name => data Returns: list: The data to be tested. Raises: ValueError: If selected column does not contain only numeric data. """ try: column_name = io.choose_from_list( 'Choose test column', data[0].keys()) except RuntimeError as re: self.display_error('Invalid column selection') raise re try: return [float(each[column_name]) for each in data] except ValueError as ve: self.display_error('Non-numeric values found in selected column.') raise ve