def areabetween(self, f1: callable, f2: callable) -> np.float32: """ Parameters ---------- f1,f2 : callable. These are the given functions Returns ------- np.float32 The area between function and the X axis """ n = 100000 X = Assignment2.intersections(f1, f2, 0, 100, 100000) total_intersections = len(X) total_area1, total_area2, overall_area_sum = 0, 0, 0 for i in range(total_intersections): if i < (total_intersections - 1) and total_intersections > 1: total_area1 = self.integrate(f1, i, i + 1, n) total_area2 = self.integrate(f2, i, i + 1, n) overall_area_sum += abs(total_area1 - total_area2) # replace this line with your solution result = np.float32(overall_area_sum) return result
def tfunc(self, func_name: str, f1: callable, f2: callable, s: float, to: float, maxerr: float, number_of_points=-1, draw=False): print(func_name) if (draw): p_x = np.arange(s * 1.1, to * 1.1, 0.1) p_y = f1(p_x) - f2(p_x) plt.plot(p_x, p_y) plt.show() ass2 = Assignment2() X = ass2.intersections(f1, f2, s, to, maxerr) index = 1 for x in X: print(f"{index} {x}") index = index + 1 self.assertGreaterEqual(maxerr, abs(f1(x) - f2(x))) if (number_of_points > -1): self.assertEqual(number_of_points, index - 1) print(f"found {index - 1} points")
def test_poly(self): ass2 = Assignment2() f1, f2 = randomIntersectingPolynomials(10) X = ass2.intersections(f1, f2, -1, 1, maxerr=0.001) for x in X: self.assertGreaterEqual(0.001, abs(f1(x) - f2(x)))
def test_sqr(self): ass2 = Assignment2() f1 = np.poly1d([-1, 0, 1]) f2 = np.poly1d([1, 0, -1]) X = ass2.intersections(f1, f2, -1, 1, maxerr=0.001) for x in X: self.assertGreaterEqual(0.001, abs(f1(x) - f2(x)))
def areabetween(self, f1: callable, f2: callable) -> np.float32: """ Finds the area enclosed between two functions. This method finds all intersection points between the two functions to work correctly. Example: https://www.wolframalpha.com/input/?i=area+between+the+curves+y%3D1-2x%5E2%2Bx%5E3+and+y%3Dx Note, there is no such thing as negative area. In order to find the enclosed area the given functions must intersect in at least two points. If the functions do not intersect or intersect in less than two points this function returns NaN. This function may not work correctly if there is infinite number of intersection points. Parameters ---------- f1,f2 : callable. These are the given functions Returns ------- np.float32 The area between function and the X axis """ # replace this line with your solution ass2 = Assignment2() Xs = ass2.intersections(f1, f2, 1, 100) diff_func = lambda x: f1(x) - f2(x) result = 0 for i in range(len(Xs) - 1): result += abs(self.integrate(diff_func, Xs[i], Xs[i + 1], 200)) return np.float32(result)
def test_get_total_number_of_variants_of_file(): a2 = Assignment2() assert a2.get_total_number_of_variants_of_file() == 4942
def test_get_average_variant_quality_of_file(): a2 = Assignment2() assert a2.get_average_variant_quality_of_file() == 5.0
def test_get_number_of_heterozygous_variants(): a2 = Assignment2() assert a2.get_number_of_heterozygous_variants() == 3803
def test_get_number_of_snvs(): a2 = Assignment2() assert a2.get_number_of_snvs() == 4622
def test_get_number_of_indels(): a2 = Assignment2() assert a2.get_number_of_indels() == 320
def test_get_vcf_fileformat(): a2 = Assignment2() assert a2.get_vcf_fileformat() == "VCFv4.2"
ind = np.argsort(d)[:num_cols] closest = main.data[ind] for col in range(num_cols): i += 1 # else: # patch_idx = random.choice(cluster_indices) # patch = main.data[patch_idx] patch = closest[col] # grid plot of size 2 x num_cols plt.subplot(num_rows, num_cols, i) if col == 0: plt.title('Center') # plt.title(str(patch_idx)) plt.imshow(patch) fig = plt.gcf() plt.show() fname = utils.datetime_filename('output/A2_test/clusters/samples.png') fig.savefig(fname, format='png', dpi=300) if __name__ == '__main__': main = Assignment2() main.train(False) make_folders() print('\nTest Clusters') test_cluster()
def areabetween(self, f1: callable, f2: callable): #-> np.float32: """ Finds the area enclosed between two functions. This method finds all intersection points between the two functions to work correctly. Example: https://www.wolframalpha.com/input/?i=area+between+the+curves+y%3D1-2x%5E2%2Bx%5E3+and+y%3Dx Note, there is no such thing as negative area. In order to find the enclosed area the given functions must intersect in at least two points. If the functions do not intersect or intersect in less than two points this function returns NaN. Parameters ---------- f1,f2 : callable. These are the given functions Returns ------- np.float32 The area between function and the X axis """ def f(x): return f1(x) - f2(x) def calcSlope(f, p, delta): y_p = f(p) y_to = f(p + delta) return (y_to - y_p) / delta def is_more_intersections(slopes_array): if (slopes_array[0] < 0): for i in range(1, len(slopes_array)): if (slopes_array[i - 1] < slopes_array[i]): return True else: for i in range(1, len(slopes_array)): if (slopes_array[i - 1] > slopes_array[i]): return True return False def find_intersections_left(f1, f2, calcIntersections, at, slopes_delta, jmp): slopes = [0, 0, 0] next_at = at - jmp intersections = calcIntersections(f1, f2, next_at, at, 0.001) #print(intersections) if (len(intersections) == 0): for i in range(0, len(slopes_delta)): delta = slopes_delta[i] slopes[i] = calcSlope(lambda x: f1(x) - f2(x), at, (delta * -1)) * -1 if (is_more_intersections(slopes)): return find_intersections_left(f1, f2, calcIntersections, next_at, slopes_delta, jmp) else: return [] else: #print("next iter") more_intersections = find_intersections_left( f1, f2, calcIntersections, next_at, slopes_delta, jmp) #print(more_intersections) return more_intersections + intersections def find_intersections_right(f1, f2, calcIntersections, at, slopes_delta, jmp): slopes = [0, 0, 0] next_at = at + jmp intersections = calcIntersections(f1, f2, at, next_at, 0.001) if (len(intersections) == 0): for i in range(0, len(slopes_delta)): delta = slopes_delta[i] slopes[i] = calcSlope(lambda x: f1(x) - f2(x), at, delta) if (is_more_intersections(slopes)): return find_intersections_right(f1, f2, calcIntersections, next_at, slopes_delta, jmp) else: return [] else: #print("next iter") more_intersections = find_intersections_right( f1, f2, calcIntersections, next_at, slopes_delta, jmp) #print(more_intersections) return more_intersections + intersections def get_intersections(s, calc_slopes): slopes_delta = [0.01, 100, 1000] jmp = 100 left_intersections = find_intersections_left( f1, f2, calc_slopes, s, slopes_delta, jmp) right_intersections = find_intersections_right( f1, f2, calc_slopes, s, slopes_delta, jmp) return left_intersections + right_intersections def clac_aera(): calc_slopes = lambda a, b, c, d, e: assignment2.intersections( a, b, c, d, e) intersections = get_intersections(0, calc_slopes) area = np.float32(0) for i in range(0, len(intersections) - 1): new_area = abs( self.integrate(f, intersections[i], intersections[i + 1], 100)) print(f"{intersections[i]} {intersections[i+1]} {new_area}") area = area + new_area return area assignment2 = Assignment2() area = [] #f1 = np.poly1d([-1, 0, 90]) #f1 = np.poly1d([3, 7, 1, 4, 0, -4]) f1 = np.poly1d([1, -2, 0, 1]) f2 = lambda x: x print(clac_aera()) return 1