def main(): parser = argparse.ArgumentParser( description='Read a file from stdin, generate a plot', prog='bay') parser.add_argument( '-o', # input file, from user '--out_file', type=str, help="File Name With Which to Save Plot", required=True) parser.add_argument( '-p', # desired column, from user '--plot_type', type=str, help="Types are: 'hist', 'box', 'combo'", required=True) args = parser.parse_args() file_out = args.out_file type = args.plot_type x = gt.read_stdin_col(0) y = gt.read_stdin_col(1) combined = np.vstack((x, y)).T combined = combined.tolist() if type == "hist": dv.histogram(combined, file_out) if type == "box": dv.boxplot(combined, file_out) if type == "combo": dv.combo(combined, file_out)
def test_viz_column_exceptions(self): """This function tests if the exceptions are handled properly """ with self.assertRaises(IndexError) as ex: get_data.read_stdin_col(5) self.assertTrue('Invalid column number' in str(ex.exception)) with self.assertRaises(TypeError) as ex: get_data.read_stdin_col('X') self.assertTrue('Wrong data type of input' in str(ex.exception))
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 main(): parser = argparse.ArgumentParser(description='Create plots' ' with data from stdin.') parser.add_argument('--output_file_name', type=str, help='Name of the output file', required=True) parser.add_argument( '--plot_type', type=str, help='The type of plot you wish to produce', required=True) args = parser.parse_args() # Read data from standard in try: V = get_data.read_stdin_col(0) except TypeError: print('Data could not be read from stdin') sys.exit(1) # plot generation if args.plot_type == str('boxplot'): data_viz.boxplot(V, args.output_file_name) if args.plot_type == str('histogram'): data_viz.histogram(V, args.output_file_name) if args.plot_type == str('combo'): data_viz.combo(V, args.output_file_name)
def main(): parser = argparse.ArgumentParser( description='Program that allows one to create a boxplot, ' + 'histogram, or combo of the two of a set of data.', prog='viz') parser.add_argument('--out_file_name', type=str, help='The output file name.', required=True) parser.add_argument('--col_num', type=int, help='The input column.', required=True) parser.add_argument('--plot_type', type=str, help='Choices are boxplot, histogram, or combo.', required=True) args = parser.parse_args() out_file_name = args.out_file_name col_num = args.col_num plot_type = args.plot_type data = gd.read_stdin_col(col_num) if plot_type == "boxplot": dv.boxplot(data, out_file_name) if plot_type == "histogram": dv.histogram(data, out_file_name) if plot_type == "combo": dv.combo(data, out_file_name)
class TestDataViz(unittest.TestCase): global L L = get_data.read_stdin_col(1) def test_data_viz_boxplot_exist(self): """This funtion test if the plot is saved properly. """ output_name = 'boxplot_test.png' check_bf = os.path.exists(output_name) data_viz.boxplot(L, output_name) check_af = os.path.exists(output_name) self.assertFalse(check_bf) self.assertTrue(check_af) def test_data_viz_hist_exist(self): """This funtion test if the plot is saved properly. """ output_name = 'hist_test.png' check_bf = os.path.exists(output_name) data_viz.histogram(L, output_name) check_af = os.path.exists(output_name) self.assertFalse(check_bf) self.assertTrue(check_af) 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 main(): # get argument by arg parser args = parse_args() # check if it is a valid file name if not os.access(args.out_file, os.W_OK): try: open(args.out_file, 'w').close() os.unlink(args.out_file) except OSError: print('Invalid file name') sys.exit(1) # get an array from STDIN L = get_data.read_stdin_col(args.col_num) if len(L) == 0: print('Empty list') sys.exit(1) # choose plot type if (args.plot_type == 'histogram'): data_viz.histogram(L, args.out_file) elif (args.plot_type == 'boxplot'): data_viz.boxplot(L, args.out_file) elif (args.plot_type == 'combo'): data_viz.combo(L, args.out_file) else: print('Invalid plot type') sys.exit(1) sys.exit(0)
def main(): # L coming from stdin from get_data method L = get_data.read_stdin_col(0) parser = argparse.ArgumentParser(description='inputs\ for file name and \ type of plot', prog='viz.py') parser.add_argument('--plot_type', type=str, help='Choice of plot: Boxplot, Histogram, or Combo', required=True) parser.add_argument('--out_file_name', type=str, help='Name of output file', required=True) args = parser.parse_args() try: if args.plot_type == 'Boxplot': data_viz.boxplot(L, args.out_file_name) elif args.plot_type == 'Histogram': data_viz.histogram(L, args.out_file_name) elif args.plot_type == 'Combo': data_viz.combo(L, args.out_file_name) else: print("Must choose from: 'Histogram', 'Boxplot', or 'Combo'") except ValueError: print('Must have correct file extension. Try .png?')
def test_read_stdin_col_constant(self): sys.stdin = 'test.txt' f = open(sys.stdin, 'w') f.write('1 1 1 1 1 1 1\n2 2 2 2 2 2 2\n') f.close() # A = get_data.read_stdin_col(0) self.assertEqual(get_data.read_stdin_col(0), [1, 2])
def main(): data = [] col_num = 0 am = parse() o = am.out_file plot = am.plot_type data = get_data.read_stdin_col(col_num) # read(data) # print(data) data_viz.main(o, plot, data)
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 test_read_stdin_col_random(self): sys.stdin = 'test.txt' f = open(sys.stdin, 'w') Array = [] for i in range(50): Array.append(str(random.randint(1, 10))) f.write(Array[1] + ' ' + Array[10] + ' ' + Array[20] + '\n') f.write(Array[2] + ' ' + Array[12] + ' ' + Array[22] + '\n') f.close() A = get_data.read_stdin_col(0) self.assertEqual(A, [int(Array[1]), int(Array[2])])
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 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(): # 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(): ''' 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_get_data_colnum_empty(self): r = get_data.read_stdin_col([]) self.assertEqual(r, None)
def test_get_data_colnum_none(self): r = get_data.read_stdin_col(None) self.assertEqual(r, None)
def test_stdin_no_file(self): sys.stdin = 'not_a_file.csv' with self.assertRaises(FileNotFoundError) as ex: get_data.read_stdin_col(0) self.assertEqual(str(ex.exception), sys.stdin + 'was not found.')
def test_file_there_afterward_hist(self): """test there after generation""" r = data_viz.histogram(g.read_stdin_col(0), 'hist.png') self.assertTrue(path.exists("./hist.png"))
def test_success_set1(self): result = get_data.read_stdin_col(2) self.assertNotEqual(result, [1, 4]) self.assertEqual(result, [3, 6])
def test_rsc_None(self): ret = gd.read_stdin_col(None) self.assertEqual(ret, None)
def test_rsc_bad_parameters_negative_int(self): with self.assertRaises(IndexError): ret = gd.read_stdin_col(-10)
def test_rsc_bad_parameters_float(self): with self.assertRaises(IndexError): ret = gd.read_stdin_col(1.2)
def test_get_data_no_columns(self): r = get_data.read_stdin_col(0) self.assertEqual(r, None)
def test_read_stdin_col_none(self): self.assertIsNone(gt.read_stdin_col(None), None)