Example #1
0
    def test_histogram_no_out_file_name(self):
        L = []

        with self.assertRaises(TypeError) as ex:
            data_viz.histogram(L, None)

        self.assertEqual(str(ex.exception), 'No file name given.')
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)
Example #3
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 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)
Example #5
0
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)
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_histogram_random_double_array(self):
        file_name = "test.png"
        rand_double_list = [random.random() for i in range(1000)]
        data_viz.histogram(rand_double_list, file_name)

        self.assertEqual(os.path.exists(file_name), True)

        os.remove(file_name)
Example #8
0
 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)
Example #9
0
    def test_histogram_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.histogram([1, 2], test_filename)
        output = str(ex.exception)
        self.assertEqual(output, "error, file already exists")
Example #10
0
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)
Example #11
0
 def test_histogram(self):
     V = []
     # test array with 1000 random elements
     for _ in range(1000):
         r = random.randint(-1000, 1000)
         V.append(r)
     # plot histogram and verify the png is generated
     data_viz.histogram(V, 'test.png')
     self.assertTrue(os.path.exists('test.png'))
     os.remove('test.png')
Example #12
0
    def test_histogram_file_already_exits(self):
        L = []
        file_name = 'test.png'
        f = open(file_name, 'w')
        f.close()

        with self.assertRaises(SystemExit) as ex:
            data_viz.histogram(L, file_name)

        self.assertEqual(str(ex.exception), 'File already exists.')
Example #13
0
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_histogram_file_already_exists(self):
        file_name = "test.png"
        f = open(file_name, "w+")
        f.close()

        with self.assertRaises(FileExistsError) as ex:
            data_viz.histogram([], 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
Example #17
0
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)
Example #19
0
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)
Example #20
0
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)
Example #21
0
 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(plot_type, output_file_name):

    D = []

    for l in sys.stdin():

        A = l .rstrip().split()
        D.append(float(A[0]))
        D.append(float(A[1]))

    if plot_type = 'histogram':
        return data_viz.histogram(D, output_file_name)
Example #23
0
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_histogram_file_type(self):
     L = [8, 4, 1, 2, 6, 2]
     with self.assertRaises(ValueError) as ex:
         dv.histogram(L, 'test.jpeg')
     message = 'File type not supported'
     self.assertEqual(str(ex.exception), message)
 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_hist_already_exists(self):
     with self.assertRaises(OSError):
         dv.histogram([1, 2, 3, 4], 'already.png')
Example #30
0
 def test_histogram_out_file_type(self):
     L = [4, 6, 5, 9, 10, 3, 9]
     with self.assertRaises(ValueError) as ex:
         dv.histogram(L, 'test.jpeg')
     message = 'Can not support file extension. Try .png instead'
     self.assertEqual(str(ex.exception), message)