Skip to content
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
8 changes: 4 additions & 4 deletions converting.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Read every file in the question, including `info.json`, `question.html`, `server

- **A custom grader image** or an `entrypoint` in `externalGradingOptions`.
- **Values the sandbox can't send back.** Tests can only receive values that can be serialized:
numbers, strings, `None`, lists, dicts, numpy arrays and scalars, pandas DataFrames and matplotlib
plots. A test that needs a student's class instance, generator or
open file has to be rewritten to call student functions that return plain values. Do that only
if the student's task stays the same.
numbers, strings, `None`, lists, dicts, numpy arrays and scalars, pandas DataFrames, scipy sparse
matrices (CSR, CSC and COO) and matplotlib plots. A test that needs a student's class instance,
generator or open file has to be rewritten to call student functions that return plain values. Do
that only if the student's task stays the same.

## 2. Change the files

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dev = [
"mypy",
"ruff>=0.11.12",
"sympy",
"scipy",
"pandas-stubs",
"matplotlib-stubs",
"matplotlib",
Expand Down Expand Up @@ -124,5 +125,5 @@ exclude = [
]

[[tool.mypy.overrides]]
module = ["sympy.*", "dill.*"]
module = ["sympy.*", "dill.*", "scipy.*"]
ignore_missing_imports = true
3 changes: 2 additions & 1 deletion quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ comes from the same sandbox as the student's code, so a random value matches the
code received.

**Note**: Values sent back from the sandbox must be serializable. Supported types include: `int`, `float`, `str`, `list`, `dict`, `bool`, `None`, numpy arrays, pandas DataFrames,
and matplotlib plots (see [Testing Matplotlib Plots](#testing-matplotlib-plots)).
scipy sparse matrices (CSR, CSC and COO), and matplotlib plots (see [Testing Matplotlib Plots](#testing-matplotlib-plots)).

### Checking Answers

Expand Down Expand Up @@ -859,6 +859,7 @@ The grader automatically serializes/deserializes:
- Matplotlib `Axes` and `Figure` values (received as a `Figure`)
- NumPy arrays
- Pandas DataFrames
- SciPy sparse matrices (CSR, CSC and COO)
- Standard Python types (int, float, str, list, dict, bool, None)

## Giving Values to Student Code
Expand Down
55 changes: 55 additions & 0 deletions src/pytest_prairielearn_grader/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def to_json(v: Any) -> Any:
pure_json_df = json.loads(encoded_json_str_df)

return {"_type": "dataframe_v2", "_value": pure_json_df}
elif module == "scipy":
import scipy.sparse as sp

if sp.issparse(v):
return _sparse_to_json(v)
elif module == "matplotlib":
from pytest_prairielearn_grader.matplotlib_json import is_plot
from pytest_prairielearn_grader.matplotlib_json import plot_to_json
Expand All @@ -62,6 +67,35 @@ def to_json(v: Any) -> Any:
return v


def _sparse_to_json(v: Any) -> dict[str, Any]:
"""Serialize a scipy.sparse matrix or array to a JSON-safe dict.

CSR, CSC and COO keep their native index arrays so the matrix round-trips exactly,
including the student's own ordering of entries. Other formats are stored as COO.
"""
fmt = v.format
if fmt not in ("csr", "csc", "coo"):
v = v.tocoo()
fmt = "coo"

import scipy.sparse as sp

result: dict[str, Any] = {
"_type": "sparse",
"_format": fmt,
"_container": "array" if isinstance(v, sp.sparray) else "matrix",
"_shape": list(v.shape),
"_data": to_json(v.data),
}
if fmt == "coo":
result["_row"] = v.row.tolist()
result["_col"] = v.col.tolist()
else:
result["_indices"] = v.indices.tolist()
result["_indptr"] = v.indptr.tolist()
return result


def _has_value_fields(v, fields: list[str]) -> bool:
"""Return True if all fields in the '_value' dictionary are present."""
return "_value" in v and isinstance(v["_value"], dict) and all(field in v["_value"] for field in fields)
Expand Down Expand Up @@ -122,11 +156,32 @@ def from_json(v_json) -> Any:
return plot_from_json(v_json["_value"])
else:
raise ValueError("variable of type matplotlib_figure should have value")
elif v_json["_type"] == "sparse":
return _sparse_from_json(v_json)
else:
raise ValueError("variable has unknown type {}".format(v_json["_type"]))
return v_json


def _sparse_from_json(v_json: dict[str, Any]) -> Any:
"""Reconstruct a scipy.sparse matrix or array from the dict produced by `_sparse_to_json`."""
import numpy as np
import scipy.sparse as sp

fmt = v_json["_format"]
suffix = "array" if v_json.get("_container") == "array" else "matrix"
constructor = getattr(sp, f"{fmt}_{suffix}")
data = from_json(v_json["_data"])
shape = tuple(v_json["_shape"])

if fmt == "coo":
coords = (np.array(v_json["_row"]), np.array(v_json["_col"]))
return constructor((data, coords), shape=shape)
indices = np.array(v_json["_indices"])
indptr = np.array(v_json["_indptr"])
return constructor((data, indices, indptr), shape=shape)


def from_server_json(v_json: dict) -> Any:
"""
Copy of the corresponding function from `prairielearn.conversion_utils` that is used to convert JSON serialized values from the server.
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/scenario_root/test_sparse_matrix_query/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"params": {}
}
56 changes: 56 additions & 0 deletions tests/scenario_root/test_sparse_matrix_query/expected_outcome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"expected_data_object": {
"score": 1.0,
"tests": [
{
"max_points": 1,
"message": "",
"name": "query csr matrix",
"outcome": "passed",
"points": 1.0,
"points_frac": 1.0,
"test_id": "test_sparse_matrix_query.py::test_query_csr_matrix[student_code]"
},
{
"max_points": 1,
"message": "",
"name": "query csc matrix",
"outcome": "passed",
"points": 1.0,
"points_frac": 1.0,
"test_id": "test_sparse_matrix_query.py::test_query_csc_matrix[student_code]"
},
{
"max_points": 1,
"message": "",
"name": "query coo matrix",
"outcome": "passed",
"points": 1.0,
"points_frac": 1.0,
"test_id": "test_sparse_matrix_query.py::test_query_coo_matrix[student_code]"
},
{
"max_points": 1,
"message": "",
"name": "query index arrays",
"outcome": "passed",
"points": 1.0,
"points_frac": 1.0,
"test_id": "test_sparse_matrix_query.py::test_query_preserves_index_arrays[student_code]"
},
{
"max_points": 1,
"message": "",
"name": "function returns sparse",
"outcome": "passed",
"points": 1.0,
"points_frac": 1.0,
"test_id": "test_sparse_matrix_query.py::test_function_returns_sparse[student_code]"
}
]
},
"expected_outcome": {
"passed": 5,
"failed": 0
}
}
53 changes: 53 additions & 0 deletions tests/scenario_root/test_sparse_matrix_query/scenario.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""End-to-end scenario: a scipy.sparse matrix produced by student code can be
queried back from the sandbox and checked, like the legacy CS357 sparse questions.
"""

import numpy as np
import pytest
import scipy.sparse as sp

from pytest_prairielearn_grader import ConfigObject
from pytest_prairielearn_grader.fixture import StudentFixture

# Importing scipy in the sandbox can take longer than the default 1 second timeout.
autograder_config = ConfigObject(sandbox_timeout=15)

DENSE = np.array([[1.0, 0.0, 2.0], [0.0, 3.0, 0.0], [4.0, 0.0, 5.0]])


@pytest.mark.grading_data(name="query csr matrix", points=1)
def test_query_csr_matrix(sandbox: StudentFixture) -> None:
result = sandbox.query("modified_A", query_timeout=15)
assert sp.issparse(result)
assert result.format == "csr"
np.testing.assert_array_equal(result.toarray(), DENSE)


@pytest.mark.grading_data(name="query csc matrix", points=1)
def test_query_csc_matrix(sandbox: StudentFixture) -> None:
result = sandbox.query("col_matrix", query_timeout=15)
assert result.format == "csc"
np.testing.assert_array_equal(result.toarray(), DENSE)


@pytest.mark.grading_data(name="query coo matrix", points=1)
def test_query_coo_matrix(sandbox: StudentFixture) -> None:
result = sandbox.query("coo_entries", query_timeout=15)
assert result.format == "coo"
np.testing.assert_array_equal(result.toarray(), DENSE)


@pytest.mark.grading_data(name="query index arrays", points=1)
def test_query_preserves_index_arrays(sandbox: StudentFixture) -> None:
result = sandbox.query("modified_A", query_timeout=15)
reference = sp.csr_matrix(DENSE)
np.testing.assert_array_equal(result.indices, reference.indices)
np.testing.assert_array_equal(result.indptr, reference.indptr)
assert result.nnz == reference.nnz


@pytest.mark.grading_data(name="function returns sparse", points=1)
def test_function_returns_sparse(sandbox: StudentFixture) -> None:
result = sandbox.query_function("scaled_matrix", 2.0, query_timeout=15)
assert sp.issparse(result)
np.testing.assert_array_equal(result.toarray(), DENSE * 2.0)
12 changes: 12 additions & 0 deletions tests/scenario_root/test_sparse_matrix_query/student_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np
import scipy.sparse as sp

dense = np.array([[1.0, 0.0, 2.0], [0.0, 3.0, 0.0], [4.0, 0.0, 5.0]])

modified_A = sp.csr_matrix(dense)
col_matrix = sp.csc_matrix(dense)
coo_entries = sp.coo_matrix(dense)


def scaled_matrix(factor):
return (modified_A * factor).tocsr()
52 changes: 51 additions & 1 deletion tests/test_serialize.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import subprocess
import sys
from pathlib import Path
Expand All @@ -7,6 +8,7 @@
import numpy as np
import pandas as pd
import pytest
import scipy.sparse as sp
import tomllib

from pytest_prairielearn_grader.json_utils import from_json
Expand Down Expand Up @@ -111,6 +113,54 @@ def test_from_server_json_networkx_graph() -> None:
assert set(graph.nodes) == {1, 2}


_DENSE = np.array([[1.0, 0.0, 2.0], [0.0, 3.0, 0.0], [4.0, 0.0, 5.0]])
_COMPLEX_DENSE = np.array([[1 + 2j, 0], [0, 3 - 1j]])


@pytest.mark.parametrize(
"matrix",
[
sp.csr_matrix(_DENSE),
sp.csc_matrix(_DENSE),
sp.coo_matrix(_DENSE),
sp.csr_array(_DENSE),
sp.csc_array(_DENSE),
sp.coo_array(_DENSE),
sp.csr_matrix(_DENSE.astype(np.int64)),
sp.csr_matrix(_COMPLEX_DENSE),
],
ids=lambda m: f"{m.format}_{'array' if isinstance(m, sp.sparray) else 'matrix'}_{m.dtype}",
)
def test_sparse_roundtrips_through_json(matrix: Any) -> None:
# json.dumps/loads exercises the actual sandbox-to-grader boundary, where
# the value is encoded as a JSON string and decoded on the grader side.
restored = from_json(json.loads(json.dumps(to_json(matrix))))

assert sp.issparse(restored)
assert restored.format == matrix.format
assert isinstance(restored, sp.sparray) == isinstance(matrix, sp.sparray)
assert restored.shape == matrix.shape
assert restored.dtype == matrix.dtype
np.testing.assert_array_equal(restored.toarray(), matrix.toarray())


def test_sparse_csr_preserves_native_index_arrays() -> None:
matrix = sp.csr_matrix(_DENSE)
restored = from_json(json.loads(json.dumps(to_json(matrix))))

np.testing.assert_array_equal(restored.indices, matrix.indices)
np.testing.assert_array_equal(restored.indptr, matrix.indptr)
np.testing.assert_array_equal(restored.data, matrix.data)


def test_sparse_other_format_roundtrips_as_coo() -> None:
lil = sp.lil_matrix(_DENSE)
restored = from_json(json.loads(json.dumps(to_json(lil))))

assert restored.format == "coo"
np.testing.assert_array_equal(restored.toarray(), lil.toarray())


def test_optional_dependencies_are_declared() -> None:
project = tomllib.loads((Path(__file__).parents[1] / "pyproject.toml").read_text())["project"]
requirements = project["dependencies"]
Expand All @@ -126,7 +176,7 @@ def test_json_utils_loads_without_optional_dependencies() -> None:
from pathlib import Path
import sys

blocked = {"matplotlib", "networkx", "numpy", "pandas"}
blocked = {"matplotlib", "networkx", "numpy", "pandas", "scipy"}
original_import = builtins.__import__


Expand Down
Loading
Loading