def test(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') weight = pl.get_integer_attrib(element, 'weight', WEIGHT_DEFAULT) # Get correct answer a_tru = data['correct_answers'][name] # If correct answer is in a format generated by pl.to_json, convert it # back to a standard type (otherwise, do nothing) a_tru = pl.from_json(a_tru) # Wrap true answer in ndarray (if it already is one, this does nothing) a_tru = np.array(a_tru) result = random.choices(['correct', 'incorrect', 'invalid'], [5, 5, 1])[0] if random.choice([True, False]): # matlab if result == 'correct': data['raw_submitted_answers'][name] = pl.numpy_to_matlab( a_tru, ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = pl.numpy_to_matlab( a_tru + (random.uniform(1, 10) * random.choice([-1, 1])), ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': # FIXME: add more invalid expressions, make text of format_errors # correct, and randomize data['raw_submitted_answers'][name] = '[1, 2, 3]' data['format_errors'][name] = 'invalid' else: raise Exception('invalid result: %s' % result) else: # python if result == 'correct': data['raw_submitted_answers'][name] = str(np.array(a_tru).tolist()) data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = str( (a_tru + (random.uniform(1, 10) * random.choice([-1, 1]))).tolist()) data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': # FIXME: add more invalid expressions, make text of format_errors # correct, and randomize data['raw_submitted_answers'][name] = '[[1, 2, 3], [4, 5]]' data['format_errors'][name] = 'invalid' else: raise Exception('invalid result: %s' % result)
def test(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') weight = pl.get_integer_attrib(element, 'weight', 1) # Get correct answer a_tru = data['correct_answers'][name] # If correct answer is in a format generated by pl.to_json, convert it # back to a standard type (otherwise, do nothing) a_tru = pl.from_json(a_tru) # Wrap true answer in ndarray (if it already is one, this does nothing) a_tru = np.array(a_tru) result = random.choices(['correct', 'incorrect', 'invalid'], [5, 5, 1])[0] if random.choice([True, False]): # matlab if result == 'correct': data['raw_submitted_answers'][name] = pl.numpy_to_matlab(a_tru, ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = pl.numpy_to_matlab(a_tru + (random.uniform(1, 10) * random.choice([-1, 1])), ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': # FIXME: add more invalid expressions, make text of format_errors # correct, and randomize data['raw_submitted_answers'][name] = '[1, 2, 3]' data['format_errors'][name] = 'invalid' else: raise Exception('invalid result: %s' % result) else: # python if result == 'correct': data['raw_submitted_answers'][name] = str(np.array(a_tru).tolist()) data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = str((a_tru + (random.uniform(1, 10) * random.choice([-1, 1]))).tolist()) data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': # FIXME: add more invalid expressions, make text of format_errors # correct, and randomize data['raw_submitted_answers'][name] = '[[1, 2, 3], [4, 5]]' data['format_errors'][name] = 'invalid' else: raise Exception('invalid result: %s' % result)
def render(element_html, element_index, data): element = lxml.html.fragment_fromstring(element_html) digits = pl.get_integer_attrib(element, 'digits', 2) html = '<pre>\n% Data in MATLAB format\n' for child in element: if child.tag == 'variable': pl.check_attribs(child, required_attribs=['params_name'], optional_attribs=[]) var_name = pl.get_string_attrib(child, 'params_name') var_data = data['params'].get(var_name, None) if var_data is None: raise Exception( 'No value in data["params"] for variable %s in matrix_output element' % var_name) html += pl.inner_html(child) \ + ' = ' \ + pl.numpy_to_matlab((var_data if np.isscalar(var_data) else np.array(var_data)), ndigits=digits) + ';' \ + '\n' html += '</pre>' return html
def render(element_html, element_index, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers_name') label = pl.get_string_attrib(element, 'label', None) if data['panel'] == 'question': editable = data['editable'] raw_submitted_answer = data['raw_submitted_answers'].get(name, None) # Get comparison parameters and info strings comparison = pl.get_string_attrib(element, 'comparison', 'relabs') if comparison == 'relabs': rtol = pl.get_float_attrib(element, 'rtol', 1e-5) atol = pl.get_float_attrib(element, 'atol', 1e-8) info_params = { 'format': True, 'relabs': True, 'rtol': rtol, 'atol': atol } elif comparison == 'sigfig': digits = pl.get_integer_attrib(element, 'digits', 2) info_params = {'format': True, 'sigfig': True, 'digits': digits} elif comparison == 'decdig': digits = pl.get_integer_attrib(element, 'digits', 2) info_params = {'format': True, 'decdig': True, 'digits': digits} else: raise ValueError( 'method of comparison "%s" is not valid (must be "relabs", "sigfig", or "decdig")' % comparison) with open('pl_matrix_input.mustache', 'r') as f: info = chevron.render(f, info_params).strip() with open('pl_matrix_input.mustache', 'r') as f: info_params.pop('format', None) info_params['shortformat'] = True shortinfo = chevron.render(f, info_params).strip() html_params = { 'question': True, 'name': name, 'label': label, 'editable': editable, 'info': info, 'shortinfo': shortinfo } if raw_submitted_answer is not None: html_params['raw_submitted_answer'] = escape(raw_submitted_answer) with open('pl_matrix_input.mustache', 'r') as f: html = chevron.render(f, html_params).strip() elif data['panel'] == 'submission': parse_error = data['format_errors'].get(name, None) html_params = { 'submission': True, 'label': label, 'parse_error': parse_error } if parse_error is None: a_sub = np.array(data['submitted_answers'][name]) html_params['a_sub'] = pl.numpy_to_matlab(a_sub, ndigits=12, wtype='g') else: raw_submitted_answer = data['raw_submitted_answers'].get( name, None) if raw_submitted_answer is not None: html_params['raw_submitted_answer'] = escape( raw_submitted_answer) with open('pl_matrix_input.mustache', 'r') as f: html = chevron.render(f, html_params).strip() elif data['panel'] == 'answer': # Get true answer - do nothing if it does not exist a_tru = data['correct_answers'].get(name, None) if a_tru is not None: a_tru = np.array(a_tru) # Get comparison parameters comparison = pl.get_string_attrib(element, 'comparison', 'relabs') if comparison == 'relabs': rtol = pl.get_float_attrib(element, 'rtol', 1e-5) atol = pl.get_float_attrib(element, 'atol', 1e-8) # FIXME: render correctly with respect to rtol and atol a_tru = pl.numpy_to_matlab(a_tru, ndigits=12, wtype='g') elif comparison == 'sigfig': digits = pl.get_integer_attrib(element, 'digits', 2) a_tru = pl.numpy_to_matlab_sf(a_tru, ndigits=digits) elif comparison == 'decdig': digits = pl.get_integer_attrib(element, 'digits', 2) a_tru = pl.numpy_to_matlab(a_tru, ndigits=digits, wtype='f') else: raise ValueError( 'method of comparison "%s" is not valid (must be "relabs", "sigfig", or "decdig")' % comparison) # FIXME: render correctly with respect to method of comparison html_params = {'answer': True, 'label': label, 'a_tru': a_tru} with open('pl_matrix_input.mustache', 'r') as f: html = chevron.render(f, html_params).strip() else: html = '' else: raise Exception('Invalid panel type: %s' % data['panel']) return html
def test(element_html, data): element = lxml.html.fragment_fromstring(element_html) name = pl.get_string_attrib(element, 'answers-name') weight = pl.get_integer_attrib(element, 'weight', WEIGHT_DEFAULT) # Get correct answer a_tru = data['correct_answers'][name] # If correct answer is in a format generated by pl.to_json, convert it # back to a standard type (otherwise, do nothing) a_tru = pl.from_json(a_tru) # Wrap true answer in ndarray (if it already is one, this does nothing) a_tru = np.array(a_tru) result = data['test_type'] if random.choice([True, False]): # matlab if result == 'correct': data['raw_submitted_answers'][name] = pl.numpy_to_matlab( a_tru, ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = pl.numpy_to_matlab( a_tru + (random.uniform(1, 10) * random.choice([-1, 1])), ndigits=12, wtype='g') data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': invalid_cases = { 'invalid commas': [ '[,,1, 2, 3]', '[1,, 2, 3]', '[1, 2,, 3]', '[1, 2, 3,,]', '[, ,1, 2, 3]', '[1, , 2, 3]', '[1, 2, , 3]', '[1, 2, 3, ,]', ], 'uneven dimensions': [ '[1; 2 3]', '[1 2; 3]', '[1; 2, 3]', '[1, 2; 3]', ], 'unbalanced brackets': [ '[1 2 3', '1 2 3]', '[1, 2, 3', '1, 2, 3]', ], 'non-spaces outside brackets': [ '1 [2 3]', '[1 2] 3', '1 [2, 3]', '[1, 2] 3', ], 'not finite': [ 'np.inf', '[np.inf]', '[1 2 np.inf]', '[1, 2, np.inf]', ], 'empty matrix': [ '[]', '[,]', ], } error = random.choice(list(invalid_cases)) data['raw_submitted_answers'][name] = random.choice( invalid_cases[error]) data['format_errors'][name] = error else: raise Exception('invalid result: %s' % result) else: # python if result == 'correct': data['raw_submitted_answers'][name] = str(np.array(a_tru).tolist()) data['partial_scores'][name] = {'score': 1, 'weight': weight} elif result == 'incorrect': data['raw_submitted_answers'][name] = str( (a_tru + (random.uniform(1, 10) * random.choice([-1, 1]))).tolist()) data['partial_scores'][name] = {'score': 0, 'weight': weight} elif result == 'invalid': # FIXME: add more invalid expressions, make text of format_errors # correct, and randomize data['raw_submitted_answers'][name] = '[[1, 2, 3], [4, 5]]' data['format_errors'][name] = 'invalid' else: raise Exception('invalid result: %s' % result)