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

Fixed issue with max numbers extracted from output and misc. updates

parent 189a9338
Branches
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -73,7 +73,7 @@ class QItem(unittest.TestCase): ...@@ -73,7 +73,7 @@ class QItem(unittest.TestCase):
tol = 0 tol = 0
_computed_answer = None # Internal helper to later get results. _computed_answer = None # Internal helper to later get results.
def __init__(self, working_directory=None, correct_answer_payload=None, *args, **kwargs): def __init__(self, working_directory=None, correct_answer_payload=None, question=None, *args, **kwargs):
if self.tol > 0 and self.testfun is None: if self.tol > 0 and self.testfun is None:
self.testfun = self.assertL2Relative self.testfun = self.assertL2Relative
elif self.testfun is None: elif self.testfun is None:
...@@ -81,6 +81,7 @@ class QItem(unittest.TestCase): ...@@ -81,6 +81,7 @@ class QItem(unittest.TestCase):
self.name = self.__class__.__name__ self.name = self.__class__.__name__
self._correct_answer_payload = correct_answer_payload self._correct_answer_payload = correct_answer_payload
self.question = None
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.title is None: if self.title is None:
self.title = self.name self.title = self.name
...@@ -213,15 +214,16 @@ class QuestionGroup(metaclass=OrderedClassMembers): ...@@ -213,15 +214,16 @@ class QuestionGroup(metaclass=OrderedClassMembers):
title = "Graph search" title = "Graph search"
items = None items = None
partially_scored = False partially_scored = False
t_init = 0 # Time spend on initialization (placeholder; set this externally).
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.name = self.__class__.__name__ self.name = self.__class__.__name__
if self.items is None: if self.items is None:
self.items = [] self.items = []
members = [gt for gt in [getattr(self, gt) for gt in self.__ordered__] if inspect.isclass(gt) and issubclass(gt, QItem)] members = [gt for gt in [getattr(self, gt) for gt in self.__ordered__ if gt not in ["__classcell__", "__init__"]] if inspect.isclass(gt) and issubclass(gt, QItem)]
for gt in members: for gt in members:
self.items.append( (gt, 1) ) self.items.append( (gt, 1) )
self.items = [(I(), w) for I, w in self.items] self.items = [(I(question=self), w) for I, w in self.items]
class Report(): class Report():
title = "report title" title = "report title"
...@@ -234,7 +236,15 @@ class Report(): ...@@ -234,7 +236,15 @@ class Report():
working_directory = os.path.abspath(os.path.dirname(inspect.getfile(type(self)))) working_directory = os.path.abspath(os.path.dirname(inspect.getfile(type(self))))
self.wdir, self.name = setup_dir_by_class(self, working_directory) self.wdir, self.name = setup_dir_by_class(self, working_directory)
self.computed_answers_file = os.path.join(self.wdir, self.name + "_resources_do_not_hand_in.dat") self.computed_answers_file = os.path.join(self.wdir, self.name + "_resources_do_not_hand_in.dat")
self.questions = [(Q(working_directory=self.wdir),w) for Q,w in self.questions] import time
qs = [] # Has to accumulate to new array otherwise the setup/evaluation steps cannot be run in sequence.
for k, (Q, w) in enumerate(self.questions):
start = time.time()
q = (Q(working_directory=self.wdir), w)
q[0].t_init = time.time() - start
qs.append(q)
self.questions = qs
# self.questions = [(Q(working_directory=self.wdir),w) for Q,w in self.questions]
if payload is not None: if payload is not None:
self.set_payload(payload, strict=strict) self.set_payload(payload, strict=strict)
else: else:
...@@ -260,6 +270,7 @@ class Report(): ...@@ -260,6 +270,7 @@ class Report():
item._correct_answer_payload = payloads[q.name][item.name]['payload'] item._correct_answer_payload = payloads[q.name][item.name]['payload']
if "precomputed" in payloads[q.name][item.name]: if "precomputed" in payloads[q.name][item.name]:
item._precomputed_payload = payloads[q.name][item.name]['precomputed'] item._precomputed_payload = payloads[q.name][item.name]['precomputed']
self.payloads = payloads
def rm_progress_bar(txt): def rm_progress_bar(txt):
...@@ -282,8 +293,7 @@ def extract_numbers(txt): ...@@ -282,8 +293,7 @@ def extract_numbers(txt):
rx = re.compile(numeric_const_pattern, re.VERBOSE) rx = re.compile(numeric_const_pattern, re.VERBOSE)
all = rx.findall(txt) all = rx.findall(txt)
all = [float(a) if ('.' in a or "e" in a) else int(a) for a in all] all = [float(a) if ('.' in a or "e" in a) else int(a) for a in all]
if len(all) > 100: if len(all) > 500:
print(txt) print(txt)
raise Exception("unitgrade.unitgrade.py: Warning, many numbers!", len(all)) raise Exception("unitgrade.unitgrade.py: Warning, many numbers!", len(all))
return all return all
...@@ -84,12 +84,8 @@ def upack(q): ...@@ -84,12 +84,8 @@ def upack(q):
h =[(i['w'], i['possible'], i['obtained']) for i in q.values()] h =[(i['w'], i['possible'], i['obtained']) for i in q.values()]
h = np.asarray(h) h = np.asarray(h)
return h[:,0], h[:,1], h[:,2], return h[:,0], h[:,1], h[:,2],
#
# ws, possible, obtained = (np.asarray(x).squeeze() for x in zip([(i['w'], i['possible'], i['obtained']) for i in q.values()]))
# return ws, possible, obtained
def evaluate_report(report, question=None, qitem=None, passall=False, verbose=False, show_expected=False, show_computed=False,unmute=False, show_help_flag=True, def evaluate_report(report, question=None, qitem=None, passall=False, verbose=False, show_expected=False, show_computed=False,unmute=False, show_help_flag=True, silent=False):
silent=False):
from unitgrade.version import __version__ from unitgrade.version import __version__
now = datetime.now() now = datetime.now()
ascii_banner = pyfiglet.figlet_format("UnitGrade", font="doom") ascii_banner = pyfiglet.figlet_format("UnitGrade", font="doom")
...@@ -105,14 +101,13 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa ...@@ -105,14 +101,13 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa
table_data = [] table_data = []
nL = 80 nL = 80
score = {} score = {}
for n, (q, w) in enumerate(report.questions): for n, (q, w) in enumerate(report.questions):
q_hidden = issubclass(q.__class__, Hidden) q_hidden = issubclass(q.__class__, Hidden)
if question is not None and n+1 != question: if question is not None and n+1 != question:
continue continue
print(f"Question {n+1}: {q.title}") print(f"Question {n+1}: {q.title}" + f" ({report.payloads[q.name].get('time', 0):.2f} seconds)" if q.name in report.payloads else "")
print("="*nL) print("="*nL)
q.possible = 0 q.possible = 0
q.obtained = 0 q.obtained = 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment