def test_combo_random_int_array(self): file_name = "test.png" rand_int_list = [random.randrange(-1000, 1000) for i in range(1000)] data_viz.combo(rand_int_list, file_name) self.assertEqual(os.path.exists(file_name), True) os.remove(file_name)
def test_data_viz_combo_exist(self): """This funtion test if the plot is saved properly. """ output_name = 'combo_test.png' check_bf = os.path.exists(output_name) data_viz.combo(L, output_name) check_af = os.path.exists(output_name) self.assertFalse(check_bf) self.assertTrue(check_af)
def test_combo_filename_already_exists(self): test_filename = "test_file" test_file = open(test_filename, "w") test_file.close() with self.assertRaises(Exception) as ex: data_viz.combo([1, 2], test_filename) output = str(ex.exception) self.assertEqual(output, "error, file already exists")
def main(): """ Read numerical data from the standard input, calculate the mean and standard deviation, and save data as a box plot or histogram Parameters ----------- --out_file : the desired name of the saved plot, with the png extension --plot_type : the type of plot to produce. Choices are histogram, boxplot, or combo. Returns -------- A plot saved as --out_file """ parser = argparse.ArgumentParser(description='produce a plot of data from ' 'stdin', prog='viz') parser.add_argument('--out_file', type=str, help='Name of output file', required=True) parser.add_argument('--plot_type', type=str, help='The plot type, lowercase', required=True) args = parser.parse_args() out_file_name = args.out_file plot = args.plot_type # hardcoding the columns output from gen_data.sh # output of read_stdin_col is a list col_1 = get_data.read_stdin_col(0) col_2 = get_data.read_stdin_col(1) # merge both lists into one for graphing L = [] for i in col_1: L.append(i) for j in col_2: L.append(j) try: if plot == "histogram": data_viz.histogram(L, out_file_name) if plot == "boxplot": data_viz.boxplot(L, out_file_name) if plot == "combo": data_viz.combo(L, out_file_name) except NameError: print('Plot type not available. Check help for available types') sys.exit(1)
def test_combo(self): V = [] # test array with 1000 random elements for _ in range(1000): r = random.randint(-1000, 1000) V.append(r) # plot combo and verify the png is generated data_viz.combo(V, 'test.png') self.assertTrue(os.path.exists('test.png')) os.remove('test.png')
def test_combo_file_already_exits(self): L = [] file_name = 'test.png' f = open(file_name, 'w') f.close() with self.assertRaises(SystemExit) as ex: data_viz.combo(L, file_name) self.assertEqual(str(ex.exception), 'File already exists.')
def main(): L = sys.stdin out_file = args.output_file plot = args.plot_type if plot == 'boxplot': data_viz.boxplot(L, out_file) if plot == 'histogram': data_viz.histogram(L, out_file) if plot == 'combo': data_viz.combo(L, out_file)
def test_combo_file_already_exists(self): file_name = "test.png" f = open(file_name, "w+") f.close() with self.assertRaises(FileExistsError) as ex: data_viz.combo([], file_name) self.assertEqual(str(ex.exception), "That file name already exists.") os.remove(file_name)
def main(): args = getparser() data = get_data.read_stdin_col(args.col_num) if args.plot_type == 'boxplot': data_viz.boxplot(data, args.out_file) elif args.plot_type == 'histogram': data_viz.histogram(data, args.out_file) elif args.plot_type == 'combo': data_viz.combo(data, args.out_file) else: raise ValueError('Legal list for plot_type: boxplot, histogram, combo')
def main(): parser = argparse.ArgumentParser( description="plot data from stdin") parser.add_argument('--out_file_name', type=str, help='name of output file', required=True) parser.add_argument('--plot_type', type=string, help='take "histrogram" or "boxplot" or "combo"', required=True) parser.add_argument('--col_num', type=int, help='column num in stdin to get data from', required=True) args = parser.parse_args() try: data = get_data(args.col_num) except Exception: print("something went wrong in get_data") sys.exit(1) if(argparse.plot_type == "boxplot"): try: data_viz.boxplot(data, args.out_file_name) except Exception: print("something went wrong in data_viz.boxplot") sys.exit(1) if(argparse.plot_type == "histogram"): try: data_viz.histogram(data, args.out_file_name) except Exception: print("something went wrong in data_viz.histogram") sys.exit(1) if(argparse.plot_type == "boxplot"): try: data_viz.combo(data, args.out_file_name) except Exception: print("something went wrong in data_viz.combo") sys.exit(1) pass
def main(): # Get the arquments args = parse_args() # Get the data from stdin A = get_data.read_stdin_col(args.col_num) if len(A) == 0: print('Empty list') sys.exit(1) if args.plot_type == 'boxplot': data_viz.boxplot(A, args.out_file) if args.plot_type == 'histogram': data_viz.histogram(A, args.out_file) if args.plot_type == 'combo': data_viz.combo(A, args.out_file) else: print('Check plot type') sys.exit(1) sys.exit(0)
def main(): parser = argparse.ArgumentParser( description='Input output file name and desired visualization', prog='viz.py') parser.add_argument('--out_file', type=str, help='Name of output file', required=True) parser.add_argument('--plot_type', type=str, help='Type of visualization', required=True) args = parser.parse_args() L = get_data.read_stdin_col(0) if L is None: print('Data could not be extracted properly. Check input data type.') sys.exit(1) viz = args.plot_type out = args.out_file try: if viz == 'Boxplot': data_viz.boxplot([], out) elif viz == 'Histogram': data_viz.histogram(L, out) elif viz == 'Combo': data_viz.combo(L, out) else: print("When calling viz.py --plot_type must be" + " 'Boxplot', 'Histogram', or 'Combo'") sys.exit(1) except ValueError as ex: print('--out_file_name does not have proper file extension') sys.exit(1) except SystemExit: print(str(out) + ' already exists.') sys.exit(1) except TypeError as ex: print('No output file name was given.') sys.exit(1)
def test_file_already_exists(self): L = [1, 2, 3, 4] open('a_file.png', 'a').close() out_file_name = 'a_file.png' r = data_viz.boxplot(L, out_file_name) a = data_viz.histogram(L, out_file_name) b = data_viz.combo(L, out_file_name) # repeat test in here just because its so redundant/simple self.assertEqual(r, None) self.assertEqual(a, None) self.assertEqual(b, None)
def main(): # Parse args and read in data from stdin args = parser.parse_args() data = get_data.read_stdin_col(col_num=0) try: # Switch case to choose the right plot type if args.plot_type == "boxplot": data_viz.boxplot(data, args.out_file) elif args.plot_type == "histogram": data_viz.histogram(data, args.out_file) elif args.plot_type == "combo": data_viz.combo(data, args.out_file) else: print("Only the following plot types are allowed: " + "boxplot, histogram, or combo") except FileExistsError: print("That file already exists. " + "Either delete it or try another file name.") exit(1)
def main(): # pull in arguments from initialize using argparse args = initialize() # pull data from stdin using read_std_in try: col_data = get_data.read_stdin_col(args.col_num) except ValueError: print("Error: column number is out of bounds") sys.exit(1) if args.plot_type == 'boxplot': data_viz.boxplot(col_data, args.out_file) elif args.plot_type == 'histogram': data_viz.histogram(col_data, args.out_file) elif args.plot_type == 'combo': data_viz.combo(col_data, args.out_file) else: print("""The plot_type is incorrect. Please enter one of the following: histogram, boxplot, or combo""") sys.exit(1)
def main(): ''' Main function, takes arguments and generates plot as requested ''' args = parseArgs() A = get_data.read_stdin_col(args.column) if len(A) == 0: raise ValueError('Input column contains no element, exiting') sys.exit(1) if (args.plot_type == 'box'): data_viz.boxplot(A, args.out_file) elif (args.plot_type == 'hist'): data_viz.histogram(A, args.out_file) elif (args.plot_type == 'combo'): data_viz.combo(A, args.out_file) else: raise ValueError('Invalid plot type. Please enter <hist/box/combo>') sys.exit(1) sys.exit(0)
def main(): args = initialize().parse_args(sys.argv[1:]) # sys.args[0]: program name # get the data from STDIN and check if the column number is valid try: L = get_data.read_stdin_col(int(args.col_num)) except IndexError: raise IndexError('Invalid column number') sys.exit(1) except TypeError: raise TypeError('Wrong data type of input') sys.exit(1) if (args.type == 'histogram'): data_viz.histogram(L, args.output_name) elif (args.type == 'boxplot'): data_viz.boxplot(L, args.output_name) elif (args.type == 'combo'): data_viz.combo(L, args.output_name) else: print('Invalid plot type') sys.exit(1)
def main(): parser = argparse.ArgumentParser( description='from stdin use array to plot data: need col# and plot type', prog='input arg') parser.add_argument('--col_index', type=int, help='The column number') parser.add_argument('--out_file', type=str, help='filename for saving') parser.add_argument('--plot_type', type=str, help='histogram or boxplot or combo') args = parser.parse_args() L = get_data.read_stdin_col(args.col_index) out_file_name = args.out_file if args.plot_type == 'boxplot': data_viz.boxplot(L,out_file_name,args.col_index) elif args.plot_type == 'hist': data_viz.histogram(L,out_file_name,args.col_index) elif args.plot_type == 'combo': data_viz.combo(L,out_file_name,args.col_index) else: print('input must be exactly: "boxplot", "hist", or "combo"') pass
def main(out_file, col_num, plot_type): """ This script generates either a boxplot, histogram, or combo plot \ from a user selected column in stdin and saves it to an outfile Parameters: - outfile(str): The user-defined file name for the generated plot - col_num(int): The user-defined column number of interest - plot_type(str): The user-defined type of plot to generate Returns: - None, but a plot is generated and saved to a file """ L = read_stdin_col(col_num) if plot_type == "boxplot": boxplot(L, out_file) elif plot_type == "histogram": histogram(L, out_file) if plot_type == "combo": combo(L, out_file)
def test_data_viz_nonnumber_combo(self): # Test error handling with nonnumber entries L = ['foo', 'bar'] with self.assertRaises(TypeError) as ex: data_viz.combo(L, 'combo.png')
def test_combo_file_type(self): L = [8, 4, 1, 2, 6, 2] with self.assertRaises(ValueError) as ex: dv.combo(L, 'test.jpeg') message = 'File type not supported' self.assertEqual(str(ex.exception), message)
def test_combo_already_exists(self): with self.assertRaises(OSError): dv.combo([1, 2, 3, 4], 'already.png')
def test_combo_out_file_type(self): L = [4, 6, 5, 9, 10, 3, 9] with self.assertRaises(ValueError) as ex: dv.combo(L, 'test.jpeg') message = 'Can not support file extension. Try .png instead' self.assertEqual(str(ex.exception), message)
def test_combo_file_created(self): outfile = "output.png" dv.combo([1, 2, 3, 4, 5], outfile) self.assertTrue(os.path.exists(outfile)) os.remove(outfile)
def test_combo(self): self.assertFalse(os.path.exists('test_combo.png')) data_viz.combo(self.testdata, 'test_combo.png') self.assertTrue(os.path.exists('test_combo.png'))
def test_data_viz_constant_combo(self): # Test creation of a PNG from the script L = [1, 2, 3, 4, 5] data_viz.combo(L, 'combo.png') self.assertTrue(path.exists("combo.png"))
def test_data_viz_float_combo(self): # Test script's robustness with floats L = [1.0, 2.2, 5.1, 3.8, 4.9] data_viz.combo(L, 'combo.png') self.assertTrue(path.exists("combo.png"))
def test_combo_empty_list(self): with self.assertRaises(ValueError): data_viz.combo([], 'novel.png')
def test_combo_none_list(self): with self.assertRaises(ValueError): data_viz.combo(None, 'novel.png')
def test_combo_1d(self): with self.assertRaises(TypeError): data_viz.combo([1, 2, 3], 'novel.png')