def test_list_stdev_nonint(self): L = [] for i in range(100): L.append('A') with self.assertRaises(ValueError) as ex: math_lib.list_stdev(L) self.assertEqual(str(ex.exception), 'Unsupported value in list.')
def test_list_stdev_parameters(self): for i in range(1, 100): stringlist = ["B", "x"] for j in range(1, 100): stringlist.append(random.randint(1, 100)) if (i % 2 == 0): stringlist.append(False) with self.assertRaises(Exception) as ex: math_lib.list_stdev(stringlist)
def test_list_stdev_non_real_number_in_list(self): """test if list has bad inputs""" L = [] for i in range(10): L.append(random.randint(0, 100)) L.append('x') with self.assertRaises(ValueError) as ex: math_lib.list_stdev(L)
def test_list_stdev_non_num_in_list(self): non_num_list = [random.random() for i in range(1000)] non_num_list.append("non_num") with self.assertRaises(TypeError) as ex: math_lib.list_stdev(non_num_list) self.assertEqual(str(ex.exception), "List must only contain numeric values.")
def test_list_stdev_non_int_in_list(self): L = [] for i in range(10): L.append(rd.randint(0, 100)) L.append('X') with self.assertRaises(ValueError) as ex: math_lib.list_stdev(L) self.assertEqual( str(ex.exception), 'Mean and Stdev undefined.' + ' Unsupported value in L.')
def test_list_stdev_non_int_or_float(self): L = [] for i in range(10): L.append(random.randint(0, 100)) L.append('X') for j in range(10): L.append(random.uniform(0, 100)) with self.assertRaises(ValueError) as ex: math_lib.list_stdev(L) message = 'Unsupported value. List of ints and floats only' self.assertEqual(str(ex.exception), message)
def combo(L, out_file_name): """ This function creates both a histogram and box plot displaying the data \ contained in the list of values and displays the mean and standard \ deviation of the data as the figure's title. It also saves \ the figure to a file. Parameters: - L(list): A list of numbers - out_file_name(str): The name of the file we want to create Returns: - None, however, a file is saved. """ width = 3 height = 3 mean = list_mean(L) std = list_stdev(L) fig, axes = plt.subplots(2, 1) axes[0].hist(L) axes[0].set_xlabel("Value") axes[0].set_ylabel("Frequency") axes[0].set_title("Mean: " + str(mean) + "Standard Deviation: " + str(std), loc="right") axes[1].boxplot(L) axes[1].set_xlabel("Array #1") axes[1].set_ylabel("Value") axes[1].set_title("Mean: " + str(mean) + "Standard Deviation: " + str(std), loc="right") plt.tight_layout() plt.savefig(out_file_name)
def histogram(L, out_file_name): file = pathlib.Path(out_file_name) if file.exists(): return ('this file name is taken, choose another.') else: width = 3 height = 3 mean = ml.list_mean(L) stdev = ml.list_stdev(L) title = 'mean=' + str(mean) + ' stdev=' + str(stdev) fig = plt.figure(figsize=(width, height), dpi=300) ax = fig.add_subplot(1, 1, 1) ax.set_title(title) ax.set_xlabel('value') ax.set_ylabel('frequency') ax.hist(L, 3) plt.savefig(out_file_name, bbox_inches='tight') if not file.exists(): print('the plot did not save successfully.')
def histogram(L, out_file_name): """ Histogram of numerical array and give specified name Parameters -------- L : Numerical array Array contains list of integers or floats out_file_name : file name File name of the output file Returns -------- Generate file with specified name containing histogram , tile and labeled axis. Raises -------- NameError No output file name SystemExit File exits """ if out_file_name is None: raise NameError('Out_file_name required') if path.exists(out_file_name): raise SystemExit('File already exits') plt.hist(L) plt.title( str('Mean: ' + str(math_lib.list_mean(L)) + ' Stdev: ' + str(math_lib.list_stdev(L)))) plt.xlabel('Value') plt.ylabel('Frequency') plt.savefig(out_file_name, dpi=300) pass
def boxplot(L, out_file_name): """ Boxplot of numerical array and give specified name Parameters -------- L : Numerical array Array contains list of integers or floats out_file_name : file name File name of the output file Returns -------- Generate file with specified name containing boxplot , tile and labeled axist Raises -------- NameError No output file name SystemExit File exits """ # Check input if out_file_name is None: raise NameError('Out_file_name required') if path.exists(out_file_name): raise SystemExit('File already exits') plt.boxplot(L) plt.title( str('Mean: ' + str(math_lib.list_mean(L)) + ' Stdev: ' + str(math_lib.list_stdev(L)))) plt.xlabel('Box') plt.ylabel('Distribution') plt.savefig(out_file_name, dpi=300) pass
def test_list_stdev_rand(self): rand_ary = [] for i in range(100): rand_ary.append(random.random()) self.assertAlmostEqual(ml.list_stdev(rand_ary), stats.stdev(rand_ary), places=0)
def test_list_stdev_non_int_in_list(self): L = [] for i in range(10): L.append(random.randint(0, 100)) L.append('X') r = math_lib.list_stdev(L) self.assertRaises(TypeError, r)
def histogram(L, out_file_name): # ensures correct input type if L is None: return None if len(L) == 0: return None if not isinstance(L, list): raise TypeError('Input must be list') s = 0 for l in L: if not isinstance(l, int) and not isinstance(l, float): raise ValueError('Invalid type in list.') if os.path.exists(out_file_name) is True: print('This file name already exists') return out_file = out_file_name plt.title('mean: ' + str(math_lib.list_mean(L)) + ' ' + 'stdev: ' + str(math_lib.list_stdev(L)), fontsize=20) plt.xlabel('frequency') plt.ylabel('values') plt.hist(L) plt.savefig(out_file, bbox_inches='tight')
def histogram(L, out_file_name='histogram.png'): if L is None: raise TypeError('histogram: No input to L') if not isinstance(L,list): raise TypeError('histogram: must pass a list to L') valid_types = [isinstance(value,(int,float,complex)) for value in L] if not any(valid_types): raise TypeError("histogram: Invalid types in list") if not isinstance(out_file_name,str): raise TypeError("histogram: File name must be str") fig = plt.figure(figsize=(3,3)) ax = fig.add_subplot(1, 1 ,1) title = 'Mean: '+str(ml.list_mean(L))+ ', Stdev: ' + str(ml.list_stdev(L)) ax.title.set_text(title) ax.set_ylabel('Values') ax.set_xlabel('') ax.hist(L) try: plt.savefig(out_file_name, bbox_inches='tight') except ValueError: raise ValueError pass
def combo(L, out_file_name='combo.png'): if L is None: raise TypeError('combo: No input to L') if not isinstance(L,list): raise TypeError('combo: must pass a list to L') valid_types = [isinstance(value,(int,float,complex)) for value in L] if not any(valid_types): raise TypeError("combo: Invalid types in list") if not isinstance(out_file_name,str): raise TypeError("combo: File name must be str") fig,ax = plt.subplots(1,2,figsize=(10,5)) title = 'Mean: '+str(ml.list_mean(L))+ ', Stdev: ' + str(ml.list_stdev(L)) fig.suptitle(title) #ax[0,1].set_ylabel('Values') ax[1].hist(L) ax[1].set_ylabel('Frequency') ax[0].boxplot(L) ax[0].set_ylabel('Values') try: plt.savefig(out_file_name, bbox_inches='tight') except ValueError: raise ValueError pass
def test_list_stdev_randfloat(self): F = [] for i in range(100): F.append(random.uniform(-500, 1000)) stdev_liststdev = math_lib.list_stdev(F) stdev_stat = statistics.pstdev(F) self.assertEqual(round(stdev_liststdev, 4), round(stdev_stat, 4))
def histogram(L, out_file_name): if path.exists(out_file_name): raise OSError("File path already exists!") sys.exit(1) if L is None: raise ValueError("Can't graph None!") if L == []: raise ValueError("Can't graph an empty list!") try: for line in L: k = line[1] except TypeError: print("Can't plot 1D data!") D = [] y = [] for l in L: D.append(float(l[0])) D.append(float(l[1])) y.append(float(l[1])) width = 3 height = 3 stat_mean = str(ml.list_mean(y)) stat_stdev = str(ml.list_stdev(y)) fig = plt.figure(figsize=(width, height), dpi=300) ax = fig.add_subplot(1, 1, 1) ax.hist(D) plt.title("Mean: " + stat_mean + " " + "stdev: " + stat_stdev) plt.ylabel('Frequency') plt.savefig(out_file_name, bbox_inches='tight')
def combo(L, out_file_name): """ Draw one boxplot diagram and one histogram diagram in one figure and save it as out_file_name. Parameters: L: A list of numbers in either integer or float type out_file_name: The file name for the output figure """ if os.path.exists(out_file_name): raise FileExistsError('File already exists.') mean = math_lib.list_mean(L) stdev = math_lib.list_stdev(L) fig = plt.figure(dpi=300) ax = fig.add_subplot(2, 1, 1) plt.boxplot(L) plt.title("mean: {} stdev: {}".format(mean, stdev)) plt.ylabel('Box') plt.ylabel('Distribution') plt.savefig(out_file_name) ax = fig.add_subplot(2, 1, 2) plt.hist(L) plt.title("mean: {} stdev: {}".format(mean, stdev)) plt.xlabel('Value') plt.ylabel('Frequency') plt.savefig(out_file_name)
def test_list_stdev_randint(self): R = [] for i in range(100): R.append(random.randint(0, 100)) stdev_liststdev = math_lib.list_stdev(R) stdev_stat = statistics.pstdev(R) self.assertEqual(round(stdev_liststdev, 4), round(stdev_stat, 4))
def test_list_stdev_random_float(self): L = [] for i in range(100): L.append(random.uniform(0, 100)) A = math_lib.list_stdev(L) B = stats.stdev(L) self.assertTrue(math.isclose(A, B))
def histogram(array, out_file_name): """ Creates and saves a histogram of the data given in a 1D numeric list. Mean and standard deviation are found and shown in the title. Parameters ---------- array : list of ints or doubles out_file_name : string name of file to save Returns ---------- """ # Make sure not to overwrite an existing file if os.path.exists(out_file_name): raise FileExistsError("That file name already exists.") # Find mean and stdev using math_lib library mean = ml.list_mean(array) stdev = ml.list_stdev(array) # Create and save the plot plt.hist(array) plt.title("mean: " + str(mean) + " stdev: " + str(stdev)) plt.xlabel("Value") plt.ylabel("Frequency") plt.savefig(out_file_name, bbox_inches="tight") plt.close()
def combo(L, out_file_name): out_file = out_file_name X = [] Y = [] D = [] for l in sys.stdin: A = l.rstrip().split() X.append(float(A[0])) Y.append(float(A[1])) D.append(float(A[0])) D.append(float(A[1])) width = 5 height = 3 mean = math_lib.list_mean(L) stdev = math_lib.list_stdev(L) output = ('mean = ' + mean + 'stdev = ' + stdev) fig = plt.figure(figsize=(width, height), dpi=300) ax = fig.add_subplot(1, 2, 1) ax.boxplot(L) ax = fig.add_subplot(1, 2, 2) ax.hist(D) plt.savefig(out_file, bbox_inches='tight')
def combo(L, out_file_name): # ensures correct input type if L is None: return None if len(L) == 0: return None if not isinstance(L, list): raise TypeError('Input must be list') s = 0 for l in L: if not isinstance(l, int) and not isinstance(l, float): raise ValueError('Invalid type in list.') if os.path.exists(out_file_name) is True: print('This file name already exists') return out_file = out_file_name width = 10 height = 5 fig = plt.figure(figsize=(width, height), dpi=300) ax = fig.add_subplot(1, 2, 1) ax.boxplot(L) ax.set_xlabel('sample') ax.set_ylabel('values') ax = fig.add_subplot(1, 2, 2) ax.hist(L) ax.set_xlabel('frequency') ax.set_ylabel('values') fig.suptitle('mean: ' + str(math_lib.list_mean(L)) + ' ' + 'stdev: ' + str(math_lib.list_stdev(L)), fontsize=20) plt.savefig(out_file, bbox_inches='tight')
def boxplot(L, out_file_name): """ Make Boxplot in an output file from the data in a numerical array Parameters ---------- L- Array of integers and/or floats to be plotted and analyzed out_file_name - the name of the ouput file to which the plot, mean and stdev will be given Returns ---------- No return value except for datastream to output file """ if path.exists(out_file_name): raise Exception("error, file already exists") if (isinstance(out_file_name, str) is False): raise Exception("file name must be alphanumeric(string)") mean = math.list_mean(L) stdev = math.list_stdev(L) plt.boxplot(L) plt.title("mean = " + str(mean) + ", stdev = " + str(stdev)) plt.ylabel("Located Values") plt.xlabel("your entered list") plt.savefig(out_file_name, dpi=300) pass
def test_list_stdev_rand_ints(self): L = [] for i in range(100): for j in range(10): L.append(random.randint(0, 100)) r = math_lib.list_stdev(L) e = numpy.std(L) self.assertTrue(math.isclose(e, r, rel_tol=1e-03))
def test_list_stdev_non_int(self): L = [] for i in range(10): L.append(random.randint(0, 100)) e = statistics.pstdev(L) L.append('X') r = math_lib.list_stdev(L) self.assertAlmostEqual(e, r)
def test_list_stdev_rand_floats(self): L = [] for i in range(100): for j in range(10): L.append(random.uniform(0, 100)) r = math_lib.list_stdev(L) e = statistics.pstdev(L) self.assertAlmostEqual(r, e)
def test_stdev_random_int_array(self): for i in range(100): A = [] for i in range(100): A.append(random.randint(1, 100)) self.assertEqual( round(math_lib.list_stdev(A), 5), round(statistics.pstdev(A, statistics.mean(A)), 5))
def test_list_stdev_rand_floats_model(self): L = [] for i in range(100): for j in range(10): L.append(random.uniform(0, 100)) r = math_lib.list_stdev(L) e = statistics.stdev(L) self.assertTrue(math.isclose(r, e))
def test_list_stdev_non_number_in_list(self): L = [] for j in range(10): L.append(random.uniform(0, 100)) stdev = statistics.stdev(L) L.append('x') random.shuffle(L) self.assertAlmostEqual(ml.list_stdev(L), stdev)