diff --git a/cp/ex11/__init__.py b/cp/ex11/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c9bf24e54d475806d511254e677f9f48d430f544 --- /dev/null +++ b/cp/ex11/__init__.py @@ -0,0 +1 @@ +"""DO NOT EDIT/ADD TO THIS FILE.""" diff --git a/cp/ex11/bacterial_area.py b/cp/ex11/bacterial_area.py new file mode 100644 index 0000000000000000000000000000000000000000..52d444d50701991e5b8bea9c957dc4e947ca3272 --- /dev/null +++ b/cp/ex11/bacterial_area.py @@ -0,0 +1,11 @@ +"""Exercise 11.10: Bacterial area.""" +import numpy as np + +def bacterial_area(npy_path : str) -> float: + """Calculate the percentage of the image where pixel intensities are greater than 100. + + Parameters: + :param npy_path: Path to the image data file in NumPy format. + :return: The percentage of area in an image where there are bacteria. + """ + # TODO: 1 lines missing. diff --git a/cp/ex11/bacterial_growth.py b/cp/ex11/bacterial_growth.py new file mode 100644 index 0000000000000000000000000000000000000000..954e24aaa31a633a94ad7ed81a08b8cd3975505a --- /dev/null +++ b/cp/ex11/bacterial_growth.py @@ -0,0 +1,31 @@ +"""Exercise 11.7-11.8: Bacterial growth.""" + +import numpy as np +def load_data() -> np.ndarray: + """Load data from 160 files into one array. Files are located in cp/ex11/files/experiments. + + :return: The data from the files in one array. + """ + # TODO: Code has been removed from here. +def threshold_exceeded(data : np.ndarray, threshold: float) -> np.ndarray: + """Return the index at which the threshold is exceeded for each experiment. + + :param data: The data to search. + :param threshold: The threshold to compare against. + :return: The index at which the threshold is exceeded for each row. + """ + # TODO: Code has been removed from here. +def get_mean(data : np.ndarray) -> np.ndarray: + """Calculate the mean of the data. + + :param data: The data to calculate the mean of. + :return: The mean of the data for each time-point. + """ + # TODO: Code has been removed from here. +def get_std(data : np.ndarray) -> np.ndarray: + """Calculate the standard deviation of the data. + + :param data: The data to calculate the standard deviation of. + :return: The standard deviation of the data for each time-point. + """ + # TODO: Code has been removed from here. diff --git a/cp/ex11/extract_subimage.py b/cp/ex11/extract_subimage.py new file mode 100644 index 0000000000000000000000000000000000000000..31544deb7584d35dd538a45201b23e2f6d72819e --- /dev/null +++ b/cp/ex11/extract_subimage.py @@ -0,0 +1,13 @@ +"""Exercise 11.6: Indexing Images.""" +import numpy as np + +def extract_subimage(image: np.ndarray, start_point: tuple, size: tuple) -> np.ndarray: + """Extract a subimage from an image. + + :param image: Image to extract from. + :param start_point: Coordinates of the top left corner of the subimage. + :param size: Size of the subimage. + + :return: Subimage. + """ + # TODO: Code has been removed from here. diff --git a/cp/ex11/files/bacteria.npy b/cp/ex11/files/bacteria.npy new file mode 100644 index 0000000000000000000000000000000000000000..2bfbcb7e226bfbf2faa850e8210da6c5466518d0 Binary files /dev/null and b/cp/ex11/files/bacteria.npy differ diff --git a/cp/ex11/files/comic.npy b/cp/ex11/files/comic.npy new file mode 100644 index 0000000000000000000000000000000000000000..2789fef94ecb9c3f4b843e75a9dbfee555635d95 Binary files /dev/null and b/cp/ex11/files/comic.npy differ diff --git a/cp/ex11/files/shapes.npy b/cp/ex11/files/shapes.npy new file mode 100644 index 0000000000000000000000000000000000000000..a011c8b6ed161e15c5f754c93354537a1b3af85a Binary files /dev/null and b/cp/ex11/files/shapes.npy differ diff --git a/cp/ex11/list_exercises_numpy.py b/cp/ex11/list_exercises_numpy.py new file mode 100644 index 0000000000000000000000000000000000000000..5d52386b3a73a6dd14de5cd953ae5f19ee73d6a4 --- /dev/null +++ b/cp/ex11/list_exercises_numpy.py @@ -0,0 +1,41 @@ +"""Exercise 11.5: Revisit lists.""" +import numpy as np +def avg(data: np.ndarray) -> float: + """Return the average of an array. + + :param data: The array to average. + :return: The average of the array. + """ + # TODO: 1 lines missing. +def conditional_max(data: np.ndarray, threshold: float) -> float: + """Return the maximum value in the array that is smaller than the threshold. + + :param data: The array to search. + :param threshold: The threshold to compare against. + :return: The maximum value in the array that is smaller than the threshold. + """ + # TODO: 1 lines missing. +def vector_add(a: np.ndarray, b: np.ndarray) -> np.ndarray: + """Add two vectors together. + + :param a: The first vector. + :param b: The second vector. + :return: The sum of the two vectors. + """ + # TODO: 1 lines missing. +def count_multiples(data: np.ndarray, divisor: int) -> int: + """Count the number of multiples of a number in an array. + + :param data: The array to search. + :param divisor: The number to check for multiples of. + :return: The number of multiples of the divisor in the array. + """ + # TODO: 1 lines missing. +def return_multiples(data, divisor): + """Return the multiples of a number in an array. + + :param data: The array to search. + :param divisor: The number to check for multiples of. + :return: The multiples of the divisor in the array. + """ + # TODO: 1 lines missing. diff --git a/cp/ex11/outliers.py b/cp/ex11/outliers.py new file mode 100644 index 0000000000000000000000000000000000000000..c7fa18ee56fd76677bcd4515190b944ac3993fac --- /dev/null +++ b/cp/ex11/outliers.py @@ -0,0 +1,11 @@ +"""Exercise 11.9: Outliers.""" +import numpy as np + +from cp.ex11.bacterial_growth import get_mean, get_std +def outliers(data : np.ndarray) -> np.ndarray: + """Return the outliers of a dataset. + + :param data: The data to search. + :return: The outliers of the data. + """ + # TODO: Code has been removed from here. diff --git a/cp/ex11/threshold_image.py b/cp/ex11/threshold_image.py new file mode 100644 index 0000000000000000000000000000000000000000..5620d5ff15466f61fa7b0e9f8d0316184ad5a2f1 --- /dev/null +++ b/cp/ex11/threshold_image.py @@ -0,0 +1,13 @@ +"""Exercise 11.7: Threshold Image.""" + +import numpy as np + +def threshold_image(image: np.ndarray, threshold: int) -> np.ndarray: + """Threshold an image. + + :param image: An image in a 2D numpy array. + :param threshold: A threshold value. + + :return: A copy of the image with all values above the threshold are set to 1. + """ + # TODO: Code has been removed from here. diff --git a/cp/tests/tests_week11.py b/cp/tests/tests_week11.py new file mode 100644 index 0000000000000000000000000000000000000000..d197be6cc7dd1c9ed141da3ff5186186ef5a2aae --- /dev/null +++ b/cp/tests/tests_week11.py @@ -0,0 +1,159 @@ +from unitgrade import Report +import cp +from unitgrade import UTestCase +import numpy as np + +import os +os.chdir(os.path.join(os.path.dirname(cp.__file__), '..')) + +class Week11Lists(UTestCase): + def test_avg(self): + from cp.ex11.list_exercises_numpy import avg + result = avg(np.array([1,2,3,4,5])) + self.assertEqual(result, 3.) + result = avg(np.array([2,3,7,8,1,12,5,2,9])) + self.assertAlmostEqual(result, 5.444444444444445) + def test_conditional_max(self): + from cp.ex11.list_exercises_numpy import conditional_max + result = conditional_max(np.array([1.,2.,3.,4.,5.]), 3.) + self.assertEqual(result, 2) + result = conditional_max(np.array([2.,3.,7.,8.,1.,12.,5.,2.,9.]), 5.) + self.assertEqual(result, 3) + def test_vector_add(self): + from cp.ex11.list_exercises_numpy import vector_add + result = vector_add(np.array([1,2,3,4,5]), np.array([3,4,5,6,7])) + self.assertEqual(result.tolist(), [4,6,8,10,12]) + result = vector_add(np.array([2,3,7,8,1,12,5,2,9]), np.array([8,4,3,4,5,6,7, 17,2])) + self.assertEqual(result.tolist(), [10,7,10,12,6,18,12,19,11]) + def test_count_multiples(self): + from cp.ex11.list_exercises_numpy import count_multiples + result = count_multiples(np.array([1,2,3,4,5]), 3) + self.assertEqual(result, 1) + result = count_multiples(np.array([2,15,7,8,1,10,5,2,9]), 5) + self.assertEqual(result, 3) + result = count_multiples(np.array([2,15,8,1,10,5,2,9]), 7) + self.assertEqual(result, 0) + def test_return_multiples(self): + from cp.ex11.list_exercises_numpy import return_multiples + result = return_multiples(np.array([1,2,3,4,5]), 3) + self.assertEqual(result.tolist(), [3]) + result = return_multiples(np.array([2,15,7,8,1,10,5,2,9]), 5) + self.assertEqual(result.tolist(), [15,10,5]) + result = return_multiples(np.array([2,15,8,1,10,5,2,9]), 7) + self.assertEqual(result.tolist(), []) + +class Week11Subimage(UTestCase): + def test_extract_subimage(self): + from cp.ex11.extract_subimage import extract_subimage + image = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]) + result = extract_subimage(image, (0,0), (2,2)) + self.assertEqual(result.tolist(), [[1,2],[6,7]]) + result = extract_subimage(image, (1,2), (2,3)) + self.assertEqual(result.tolist(), [[8,9,10],[13,14,15]]) + result = extract_subimage(image, (0,2), (3,2)) + self.assertEqual(result.tolist(), [[3,4],[8,9],[13,14]]) + +class Week11Threshold(UTestCase): + def test_threshold_image(self): + from cp.ex11.threshold_image import threshold_image + image = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]) + result = threshold_image(image, 3) + self.assertEqual(result.tolist(), [[0,0,0,1,1],[1,1,1,1,1],[1,1,1,1,1]]) + result = threshold_image(image, 9) + self.assertEqual(result.tolist(), [[0,0,0,0,0],[0,0,0,0,1],[1,1,1,1,1]]) + result = threshold_image(image, 15) + self.assertEqual(result.tolist(), [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]) + +class Week11BacterialGrowth(UTestCase): + def test_load_data(self): + from cp.ex11.bacterial_growth import load_data + result = load_data() + self.assertEqual(result.shape, (160,12)) + self.assertAlmostEqual(result[0,0], 1.44) + self.assertAlmostEqual(result[159,11], 16.67) + def test_threshold_exceeded(self): + from cp.ex11.bacterial_growth import load_data, threshold_exceeded + data = load_data() + result = threshold_exceeded(data, 8.5) + self.assertEqual(result.size, 160) + result = threshold_exceeded(data, 9.) + self.assertAlmostEqual(np.mean(result), 9.49375) + + +class Week11AverageGrowthCurve(UTestCase): + def assertArrayAlmostEqual(self, a, b): + self.assertIsNotNone(a) + self.assertIsNone(np.testing.assert_almost_equal(a, b)) + + def test_get_std(self): + from cp.ex11.bacterial_growth import get_std + data = np.ones((4,4)) + data[::2] = 2 + self.assertArrayAlmostEqual(get_std(data), np.array([0.5, 0.5, 0.5, 0.5])) + data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])**2 + self.assertArrayAlmostEqual(get_std(data), np.array([20.04993766, 24.85960579, 29.69848481])) + data = np.sqrt(np.arange(42)).reshape(6, 7).T + self.assertArrayAlmostEqual(get_std(data), np.array([0.7781667, 0.3194126, 0.2433594, 0.2044697, 0.1797871, 0.1623305])) + + def test_get_mean(self): + from cp.ex11.bacterial_growth import get_mean + data = np.ones((4,4)) + data[::2] = 2 + self.assertArrayAlmostEqual(get_mean(data), np.array([1.5, 1.5, 1.5, 1.5])) + data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])**2 + self.assertArrayAlmostEqual(get_mean(data), np.array([22., 31., 42.])) + data = np.sqrt(np.arange(42)).reshape(6, 7).T + self.assertArrayAlmostEqual(get_mean(data), np.array([1.5474032, 3.1461048, 4.1159174, 4.8947106, 5.5648609, 6.1622763])) + + +class Week11Outliers(UTestCase): + def assertArrayEqual(self, a, b): + self.assertIsNotNone(a) + self.assertIsNone(np.testing.assert_array_equal(a, b)) + + def test_outliers1(self): + from cp.ex11.outliers import outliers + from cp.ex11.bacterial_growth import load_data + self.assertArrayEqual(outliers(load_data()), np.array([50, 96, 104])) + + def test_outliers2(self): + from cp.ex11.outliers import outliers + np.random.seed(43) + data = (np.random.rand(160, 20)*2)**2 + self.assertArrayEqual(outliers(data), np.array([19, 39, 68, 92, 101, 154])) + + def test_outliers3(self): + from cp.ex11.outliers import outliers + np.random.seed(48) + data = (np.random.rand(300, 12)*2)**2 + self.assertArrayEqual(outliers(data), np.array([ 25, 98, 128, 145, 151, 196, 201, 230, 239, 273, 277, 287])) + +class Week11BacterialArea(UTestCase): + def test_bacterial_area(self): + from cp.ex11.bacterial_area import bacterial_area + path = 'cp/ex11/files/bacteria.npy' + self.assertAlmostEqual(bacterial_area(path), 0.3757223140495868) + +questions = [ + (Week11Lists, 25), + (Week11Subimage, 10), + (Week11Threshold, 10), + (Week11BacterialGrowth, 20), + (Week11AverageGrowthCurve, 20), + (Week11Outliers, 20), + (Week11BacterialArea, 20) + ] + + +class Week11Tests(Report): #30 total. + title = "Tests for week 11" + #version = 0.1 + #url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests" + pack_imports = [cp] + individual_imports = [] + questions = questions + + +if __name__ == '__main__': + from unitgrade import evaluate_report_student + evaluate_report_student(Week11Tests()) diff --git a/cp/tests/unitgrade_data/Week11AverageGrowthCurve.pkl b/cp/tests/unitgrade_data/Week11AverageGrowthCurve.pkl new file mode 100644 index 0000000000000000000000000000000000000000..6c001ce01290da152a8bbe3483111c0bbe920e08 Binary files /dev/null and b/cp/tests/unitgrade_data/Week11AverageGrowthCurve.pkl differ diff --git a/cp/tests/unitgrade_data/Week11BacterialArea.pkl b/cp/tests/unitgrade_data/Week11BacterialArea.pkl new file mode 100644 index 0000000000000000000000000000000000000000..4345a50653b58c72f791561e7f61b328ccb2a06e Binary files /dev/null and b/cp/tests/unitgrade_data/Week11BacterialArea.pkl differ diff --git a/cp/tests/unitgrade_data/Week11BacterialGrowth.pkl b/cp/tests/unitgrade_data/Week11BacterialGrowth.pkl new file mode 100644 index 0000000000000000000000000000000000000000..9c0cd59cd9eba6edd4143f75846506c7439c8a12 Binary files /dev/null and b/cp/tests/unitgrade_data/Week11BacterialGrowth.pkl differ diff --git a/cp/tests/unitgrade_data/Week11Lists.pkl b/cp/tests/unitgrade_data/Week11Lists.pkl new file mode 100644 index 0000000000000000000000000000000000000000..72b9722b4556ea9d1cedbfdab51040febd7be9af Binary files /dev/null and b/cp/tests/unitgrade_data/Week11Lists.pkl differ diff --git a/cp/tests/unitgrade_data/Week11Outliers.pkl b/cp/tests/unitgrade_data/Week11Outliers.pkl new file mode 100644 index 0000000000000000000000000000000000000000..9f0f9c255bd9f5fb19cd1f4f625abc341812de82 Binary files /dev/null and b/cp/tests/unitgrade_data/Week11Outliers.pkl differ diff --git a/cp/tests/unitgrade_data/Week11Subimage.pkl b/cp/tests/unitgrade_data/Week11Subimage.pkl new file mode 100644 index 0000000000000000000000000000000000000000..78f8a68e9dbee7ca778b231f9444db347f8e3734 Binary files /dev/null and b/cp/tests/unitgrade_data/Week11Subimage.pkl differ diff --git a/cp/tests/unitgrade_data/Week11Threshold.pkl b/cp/tests/unitgrade_data/Week11Threshold.pkl new file mode 100644 index 0000000000000000000000000000000000000000..0930e4293a28d4267d79eca80fce0e549867db83 Binary files /dev/null and b/cp/tests/unitgrade_data/Week11Threshold.pkl differ