Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pl-matrix inputs #93

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/problem_bank_scripts/problem_bank_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,46 @@ def process_string_input(part_name, parsed_question, data_dict):

return replace_tags(html)

def process_matrix_component_input(part_name, parsed_question, data_dict):
"""Processes markdown format of matrix-component-input questions and returns PL HTML
Args:
part_name (string): Name of the question part being processed (e.g., part1, part2, etc...)
parsed_question (dict): Dictionary of the MD-parsed question (output of `read_md_problem`)
data_dict (dict): Dictionary of the `data` dict created after running server.py using `exec()`

Returns:
html: A string of HTML that is part of the final PL question.html file.
"""
pl_customizations = " ".join(
[
f'{k} = "{v}"'
for k, v in parsed_question["header"][part_name][
"pl-customizations"
].items()
]
) # PL-customizations

html = f"""<pl-question-panel>\n<markdown>{parsed_question['body_parts_split'][part_name]['content']}</markdown>\n</pl-question-panel>\n\n"""

html += f"""<pl-matrix-component-input answers-name="{part_name}_ans" { pl_customizations } ></pl-matrix-component-input>"""

return replace_tags(html)


def process_matrix_input(part_name, parsed_question, data_dict):
"""Processes markdown format of matrix-input questions and returns PL HTML
Args:
part_name (string): Name of the question part being processed (e.g., part1, part2, etc...)
parsed_question (dict): Dictionary of the MD-parsed question (output of `read_md_problem`)
data_dict (dict): Dictionary of the `data` dict created after running server.py using `exec()`

Returns:
html: A string of HTML that is part of the final PL question.html file.
"""
return process_matrix_component_input(
part_name, parsed_question, data_dict
).replace("-matrix-component-input", "-matrix-input")


def replace_tags(string):
"""Takes in a string with tags: |@ and @| and returns {{ and }} respectively. This is because Python strings can't have double curly braces.
Expand Down Expand Up @@ -1242,6 +1282,10 @@ def process_question_pl(source_filepath, output_path=None, dev=False):
question_html += process_string_input(part, parsed_q, data2)
elif "matching" in q_type:
question_html += process_matching(part, parsed_q, data2)
elif "matrix-component-input" in q_type:
question_html += process_matrix_component_input(part, parsed_q, data2)
elif "matrix-input" in q_type:
question_html += process_matrix_input(part, parsed_q, data2)
else:
raise NotImplementedError(
f"This question type ({q_type}) is not yet implemented."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
title: Matrix Input
topic: Template
author: Gavin Kendal-Freedman
source: original
template_version: 1.4
attribution: standard
partialCredit: true
singleVariant: false
showCorrectAnswer: false
outcomes:
- 6.1.1.0
- 6.1.1.1
difficulty:
- undefined
randomization:
- undefined
taxonomy:
- undefined
span:
- undefined
length:
- undefined
tags:
- unknown
assets: null
server:
imports: |
import numpy as np; np.random.seed(111)
import problem_bank_helpers as pbh
import prairielearn as pl
generate: |
data2 = pbh.create_data2()
data2["params"]["title"] = "Matrix Input"

matrix = np.random.random((2,2))
inv_matrix = np.linalg.inv(matrix)

# store phrases, info etc
data2["params"]["matrixA"] = pl.to_json(matrix)

# store the correct answers
data2["correct_answers"]["part1_ans"] = pl.to_json(inv_matrix)
data2["correct_answers"]["part2_ans"] = pl.to_json(inv_matrix)

data.update(data2)
prepare: 'pass

'
parse: 'pass

'
grade: 'pass

'
part1:
type: matrix-component-input
pl-customizations:
weight: 1
allow-fractions: 'false'
label: $A^{-1}$
comparison: decdig
part2:
type: matrix-input
pl-customizations:
weight: 1
allow-complex: 'false'
label: $A^{-1}$
comparison: decdig
myst:
substitutions:
params:
title: Matrix Input
matrixA: null
correct_answers:
part1_ans: null
part2_ans: null

---
# {{ params.vars.title }}
Given the following matrix, please return the inverse of the matrix.

<pl-matrix-latex params-name="matrixA"></pl-matrix-latex>

## Part 1

Please write your answer as a matrix component input.

### Answer Section

### pl-submission-panel

Everything here will get inserted directly into the pl-submission-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.

### pl-answer-panel

Everything here will get inserted directly into an pl-answer-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.

## Part 2

Please write your answer as a matrix input in python or matlab format.

### Answer Section

### pl-submission-panel

Everything here will get inserted directly into the pl-submission-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.

### pl-answer-panel

Everything here will get inserted directly into an pl-answer-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.

## Rubric

This should be hidden from students until after the deadline.

## Solution

This should never be revealed to students.

## Comments

These are random comments associated with this question.

## Attribution

Problem is licensed under the [CC-BY-NC-SA 4.0 license](https://creativecommons.org/licenses/by-nc-sa/4.0/).<br> ![The Creative Commons 4.0 license requiring attribution-BY, non-commercial-NC, and share-alike-SA license.](https://raw.githubusercontent.com/firasm/bits/master/by-nc-sa.png)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"uuid": "7481a4ee-ce20-3741-8a8e-35baedd57dc5",
"title": "Matrix Input",
"topic": "000.Template",
"tags": [
"multi_part",
"matrix-input",
"matrix-component-input",
"DEV"
],
"type": "v3",
"partialCredit": true,
"singleVariant": false,
"showCorrectAnswer": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<pl-question-panel>
<markdown>
Given the following matrix, please return the inverse of the matrix.

<pl-matrix-latex params-name="matrixA"></pl-matrix-latex>

</markdown>
</pl-question-panel>


<!-- ######## Start of Part 1 ######## -->

<div class="card my-2">
<div class="card-header">Part 1</div>

<div class="card-body">

<pl-question-panel>
<markdown>Please write your answer as a matrix component input.
</markdown>
</pl-question-panel>

<pl-matrix-component-input answers-name="part1_ans" weight = "1" allow-fractions = "false" label = "$A^{-1}$" comparison = "decdig" ></pl-matrix-component-input></div>
</div>

<pl-submission-panel>Everything here will get inserted directly into the pl-submission-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.
</pl-submission-panel>

<pl-answer-panel>Everything here will get inserted directly into an pl-answer-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.
</pl-answer-panel>

<!-- ######## End of Part 1 ######## -->

<!-- ######## Start of Part 2 ######## -->

<div class="card my-2">
<div class="card-header">Part 2</div>

<div class="card-body">

<pl-question-panel>
<markdown>Please write your answer as a matrix input in python or matlab format.
</markdown>
</pl-question-panel>

<pl-matrix-input answers-name="part2_ans" weight = "1" allow-complex = "false" label = "$A^{-1}$" comparison = "decdig" ></pl-matrix-input></div>
</div>

<pl-submission-panel>Everything here will get inserted directly into the pl-submission-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.
</pl-submission-panel>

<pl-answer-panel>Everything here will get inserted directly into an pl-answer-panel element at the end of the `question.html`.
Please remove this section if it is not application for this question.
</pl-answer-panel>

<!-- ######## End of Part 2 ######## -->

<pl-question-panel>
<markdown>
---
Problem is licensed under the [CC-BY-NC-SA 4.0 license](https://creativecommons.org/licenses/by-nc-sa/4.0/).<br> ![The Creative Commons 4.0 license requiring attribution-BY, non-commercial-NC, and share-alike-SA license.](https://raw.githubusercontent.com/firasm/bits/master/by-nc-sa.png)
</markdown>
</pl-question-panel>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np; np.random.seed(111)
import problem_bank_helpers as pbh
import prairielearn as pl

def imports(data):
import numpy as np; np.random.seed(111)
import problem_bank_helpers as pbh
import prairielearn as pl

def generate(data):
data2 = pbh.create_data2()
data2["params"]["title"] = "Matrix Input"

matrix = np.random.random((2,2))
inv_matrix = np.linalg.inv(matrix)

# store phrases, info etc
data2["params"]["matrixA"] = pl.to_json(matrix)

# store the correct answers
data2["correct_answers"]["part1_ans"] = pl.to_json(inv_matrix)
data2["correct_answers"]["part2_ans"] = pl.to_json(inv_matrix)

data.update(data2)

# Start code added automatically by problem_bank_scripts

# Convert backticks to code blocks/fences in answer choices.
pbh.backticks_to_code_tags(data2)

# Update data with data2
data.update(data2)

# End code added in by problem bank scripts

def prepare(data):
pass

def parse(data):
pass

def grade(data):
pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"uuid": "0b96245a-b007-36f7-9e26-0f0713220edb",
"title": "Matrix Input",
"topic": "000.Template",
"tags": [
"multi_part",
"matrix-input",
"matrix-component-input"
],
"type": "v3",
"partialCredit": true,
"singleVariant": false,
"showCorrectAnswer": false
}
Loading