Skip to content
Snippets Groups Projects
Commit acc45dbe authored by tuhe's avatar tuhe
Browse files

Updates

parent dadd26b9
No related branches found
No related tags found
No related merge requests found
Showing
with 1332 additions and 1 deletion
......@@ -4,3 +4,4 @@ jinja2
tabulate
compress_pickle
pyfiglet
colorama
\ No newline at end of file
"""
Example student code. This file is automatically generated from the files in the instructor-directory
"""
def reverse_list(mylist):
"""
Given a list 'mylist' returns a list consisting of the same elements in reverse order. E.g.
reverse_list([1,2,3]) should return [3,2,1] (as a list).
"""
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
def add(a,b):
""" Given two numbers `a` and `b` this function should simply return their sum:
> add(a,b) = a+b """
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
if __name__ == "__main__":
# Problem 1: Write a function which add two numbers
print(f"Your result of 2 + 2 = {add(2,2)}")
print(f"Reversing a small list", reverse_list([2,3,5,7]))
"""
Example student code. This file is automatically generated from the files in the instructor-directory
"""
from src.unitgrade2.unitgrade2 import UTestCase, Report
from src.unitgrade2 import evaluate_report_student
class Week1(UTestCase):
""" The first question for week 1. """
def test_add(self):
from cs103.homework1 import add
self.assertEqualC(add(2,2))
self.assertEqualC(add(-100, 5))
class AutomaticPass(UTestCase):
def test_student_passed(self):
self.assertEqual(2,2)
import cs103
class Report3(Report):
title = "CS 101 Report 3"
questions = [(Week1, 20), (AutomaticPass, 10)] # Include a single question for 10 credits.
pack_imports = [cs103]
if __name__ == "__main__":
# from unitgrade_private2.hidden_gather_upload import gather_upload_to_campusnet
# gather_upload_to_campusnet(Report3())
evaluate_report_student(Report3())
This diff is collapsed.
This diff is collapsed.
......@@ -114,7 +114,7 @@ if __name__ == "__main__":
# from week02 import Week_2_sol
import importnb
file = "week02/week2.ipynb"
file = "../../../example_jupyter/instructor/cs105/week2.ipynb"
file2 = 'week02/Week_2_sol.ipynb'
m = importnb.Notebook.load(file)
# importnb.Notebook.l
......
File added
File added
File added
from report1intro import Report1Flat
from unitgrade_private2.hidden_create_files import setup_grade_file_report
from snipper import snip_dir
if __name__ == "__main__":
setup_grade_file_report(Report1Flat, minify=False, obfuscate=False, execute=False, with_coverage=True)
# from unitgrade_private2.hidden_gather_upload import gather_upload_to_campusnet
# gather_upload_to_campusnet((Report1Flat()))
# Deploy the files using snipper: https://gitlab.compute.dtu.dk/tuhe/snipper
snip_dir.snip_dir(source_dir="", dest_dir="../../students/programs", clean_destination_dir=True, exclude=['__pycache__', '*.token', 'deploy.py'])
import os
os.system("python report1intro_grade.py")
"""
from coverage import CoverageData
import coverage
cov2 = coverage.Coverage()
def setUp(self):
import trace
self.cov = cov2
self.cov.start()
# self.tracer.start()
# using obj_to_trace
def tearDown(self) -> None:
self.cov.stop()
print()
data = CoverageData()
# data.measured_files()
# data.lines()
data = self.cov.get_data()
# data.
for file in data.measured_files():
print(file)
print(data.lines(file))
print(data.arcs(file))
print( data.contexts_by_lineno(file))
# print(data[file])
- Idea: Measure coverage in setup/teardown. This gives a handful of covered lines.
- During setup, supply a dicionary to UTestCase of files, along with the lines that are removed.
- When running setup: Take the coverage report, and compare against files. Write functions/lines encountered to the cache dictionary. Rquires you to
- inspect the functions that are edited to figure out what is removed. This can probably be done by going upwars towards the first sensible class or function definition (which has not been removed).
- Supply a dictionary to UTestCase of files, along with the lines edited. Allow UTestCase to write this information to the
cache dictionary (i.e. lines removed). Then use this information when displaying helpful hints later.
"""
\ No newline at end of file
import numpy as np
import itertools
def bacteriaGrowth(n0, alpha, K, N): #!f
"""
Calculate time until bacteria growth exceed N starting from a population of n0 bacteria.
hints:
* consider n0
* alpha > 0
:param n0:
:param alpha:
:param K:
:param N:
:return:
"""
if n0 > N:
return 0
for t in itertools.count():
n0 = (1 + alpha * (1-n0 / K) ) * n0
if n0 > N:
break
return t+1
def clusterAnalysis(reflectance):
reflectance = np.asarray(reflectance)
I1 = np.arange(len(reflectance)) % 2 == 1
while True:
m = np.asarray( [np.mean( reflectance[~I1] ), np.mean( reflectance[I1] ) ] )
I1_ = np.argmin( np.abs( reflectance[:, np.newaxis] - m[np.newaxis, :] ), axis=1) == 1
if all(I1_ == I1):
break
I1 = I1_
return I1 + 1
def fermentationRate(measuredRate, lowerBound, upperBound):
# Insert your code here
return np.mean( [r for r in measuredRate if lowerBound < r < upperBound] )
def removeIncomplete(id):
""" Hints:
* Take a look at the example in the exercise.
"""
id = np.asarray(id)
id2 = []
for i, v in enumerate(id):
if len( [x for x in id if int(x) == int(v) ] ) == 3:
id2.append(v)
return np.asarray(id2)
if __name__ == "__main__":
# I = clusterAnalysis([1.7, 1.6, 1.3, 1.3, 2.8, 1.4, 2.8, 2.6, 1.6, 2.7])
# print(I)
print(fermentationRate(np.array([20.1, 19.3, 1.1, 18.2, 19.7, 121.1, 20.3, 20.0]), 15, 25))
# print(removeIncomplete(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])))
# Problem 1: Write a function which add two numbers
# clusterAnalysis([2, 1, 2, 4, 5])
\ No newline at end of file
from src.unitgrade2.unitgrade2 import Report, UTestCase, cache
from src.unitgrade2 import evaluate_report_student
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):
""" Test the cluster analysis method """
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):
""" Test the fermentation rate question """
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):
title = "Week 4: Looping"
questions = [(ClusterAnalysis, 10), (RemoveIncomplete, 10), (Bacteria, 10), (FermentationRate, 10),]
pack_imports = [looping]
if __name__ == "__main__":
# Uncomment to simply run everything as a unittest:
# unittest.main(verbosity=2)
evaluate_report_student(Report1Flat())
This diff is collapsed.
File added
File added
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment