"git@lab.compute.dtu.dk:aia02506/public-exercise-material.git" did not exist on "584e005404a081539a0740edc5406bb4b44a0d0f"
Newer
Older
from unitgrade.framework import Report, UTestCase
from unitgrade import cache
from unitgrade.evaluate import evaluate_report_student
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import looping
from looping import bacteriaGrowth, clusterAnalysis, removeIncomplete, fermentationRate
def trlist(x):
s = str(list(x))
if len(s) > 30:
s = s[:30] + "...]"
return s
class Bacteria(UTestCase):
""" Bacteria growth rates """
def stest(self, n0, alpha, K, N):
g = bacteriaGrowth(n0=n0, alpha=alpha, K=K, N=N)
self.title = f"bacteriaGrowth({n0}, {alpha}, {K}, {N}) = {g} ?"
self.assertEqualC(g)
def test_growth1(self):
""" Hints:
* Make sure to frobulate the frobulator.
"""
self.stest(100, 0.4, 1000, 500)
def test_growth2(self):
self.stest(10, 0.4, 1000, 500)
def test_growth3(self):
self.stest(100, 1.4, 1000, 500)
def test_growth4(self):
self.stest(100, 0.0004, 1000, 500)
def test_growth5(self):
"""
hints:
* What happens when n0 > N? (in this case return t=0) """
self.stest(100, 0.4, 1000, 99)
class ClusterAnalysis(UTestCase):
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
def stest(self, n, seed):
np.random.seed(seed)
x = np.round(np.random.rand(n), 1)
I = clusterAnalysis(x)
self.title = f"clusterAnalysis({list(x)}) = {list(I)} ?"
self.assertEqualC(list(I))
def test_cluster1(self):
""" Hints:
* Make sure to frobulate the frobulator.
* Just try harder
"""
self.stest(3, 10)
def test_cluster2(self):
self.stest(4, 146)
def test_cluster3(self):
self.stest(5, 12)
def test_cluster4(self):
"""
Cluster analysis for tied lists
Hints:
* It may be that an observations has the same distance to the two clusters. Where do you assign it in this case?
"""
x = np.array([10.0, 12.0, 10.0, 12.0, 9.0, 11.0, 11.0, 13.0])
self.assertEqualC(list(clusterAnalysis(x) ) )
class RemoveIncomplete(UTestCase):
""" Remove incomplete IDs """
def stest(self, x):
I = list( removeIncomplete(x) )
self.title = f"removeId({trlist(x)}) = {trlist(I)} ?"
self.assertEqualC(I)
@cache
def rseq(self, max, n):
np.random.seed(42)
return np.random.randint(max, size=(n,) ) + (np.random.randint(2, size=(n,) )+1)/10
def test_incomplete1(self):
self.stest( np.array([1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1]) )
def test_incomplete2(self):
self.stest( np.array([1.1, 1.2, 1.3, 2.1, 2.2, 2.3]) )
def test_incomplete3(self):
self.stest(np.array([5.1, 5.2, 4.1, 4.3, 4.2, 8.1, 8.2, 8.3]) )
def test_incomplete4(self):
self.stest(np.array([1.1, 1.3, 2.1, 2.2, 3.1, 3.3, 4.1, 4.2, 4.3]) )
def test_incomplete5(self):
self.stest(self.rseq(10, 40))
class FermentationRate(UTestCase):
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def stest(self, x, lower, upper):
I = fermentationRate(x, lower, upper)
s = trlist(x)
self.title = f"fermentationRate({s}, {lower}, {upper}) = {I:.3f} ?"
self.assertEqualC(I)
@cache
def rseq(self, max, n):
np.random.seed(42)
return np.random.randint(max, size=(n,) ) + (np.random.randint(3, size=(n,) )+1)/n
def test_rate1(self):
self.stest(np.array([20.1, 19.3, 1.1, 18.2, 19.7, 121.1, 20.3, 20.0]), 15, 25)
def test_rate2(self):
self.stest(np.array([20.1, 19.3, 1.1, 18.2, 19.7, 121.1, 20.3, 20.0]), 1, 200)
def test_rate3(self):
self.stest(np.array([1.75]), 1, 2)
def test_rate4(self):
self.stest(np.array([20.1, 19.3, 1.1, 18.2, 19.7, 121.1, 20.3, 20.0]), 18.2, 20)
class Report1Flat(Report):