Skip to content

Commit

Permalink
New Autograde Preprocessor: IgnorePattern (#1904)
Browse files Browse the repository at this point in the history
* Create IgnorePattern Preprocessor

* Disable and add to the list by default
  • Loading branch information
AlirezaT99 authored Aug 9, 2024
1 parent 338ec4d commit ef44180
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nbgrader/converters/autograde.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .base import BaseConverter, NbGraderException
from ..preprocessors import (
AssignLatePenalties, ClearOutput, DeduplicateIds, OverwriteCells, SaveAutoGrades,
Execute, LimitOutput, OverwriteKernelspec, CheckCellMetadata)
Execute, LimitOutput, OverwriteKernelspec, CheckCellMetadata, IgnorePattern)
from ..api import Gradebook, MissingEntry
from .. import utils

Expand Down Expand Up @@ -61,6 +61,7 @@ def _output_directory(self) -> str:
]).tag(config=True)
autograde_preprocessors = List([
Execute,
IgnorePattern,
ClearMetadataPreprocessor,
LimitOutput,
SaveAutoGrades,
Expand Down
2 changes: 2 additions & 0 deletions nbgrader/preprocessors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .clearhiddentests import ClearHiddenTests
from .clearmarkingscheme import ClearMarkScheme
from .overwritekernelspec import OverwriteKernelspec
from .ignorepattern import IgnorePattern

__all__ = [
"AssignLatePenalties",
Expand All @@ -37,4 +38,5 @@
"ClearHiddenTests",
"ClearMarkScheme",
"OverwriteKernelspec",
"IgnorePattern",
]
31 changes: 31 additions & 0 deletions nbgrader/preprocessors/ignorepattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from . import NbGraderPreprocessor

from traitlets import Unicode, Bool
from nbformat.notebooknode import NotebookNode
from nbconvert.exporters.exporter import ResourcesDict
from typing import Tuple
import re


class IgnorePattern(NbGraderPreprocessor):
"""Preprocessor for removing cell outputs that match a particular pattern"""

pattern = Unicode("", help="The regular expression to remove from stderr").tag(config=True)
enabled = Bool(False, help="Whether to use this preprocessor when running nbgrader").tag(config=True)

def preprocess_cell(self,
cell: NotebookNode,
resources: ResourcesDict,
cell_index: int
) -> Tuple[NotebookNode, ResourcesDict]:

if self.pattern and cell.cell_type == "code":
new_outputs = []
for output in cell.outputs:
if output.output_type == "stream" and output.name == "stderr" \
and re.search(self.pattern, output.text):
continue
new_outputs.append(output)
cell.outputs = new_outputs

return cell, resources
82 changes: 82 additions & 0 deletions nbgrader/tests/preprocessors/files/warning-pattern.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "efd8b463-ea80-4620-b01b-1382a56e7836",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[W NNPACK.cpp:64] Could not initialize NNPACK! Reason: Unsupported hardware.\n"
]
},
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"layer = torch.nn.Conv2d(1, 32, 3, stride=1, padding=1)\n",
"x = torch.randn(1, 1, 28, 28)\n",
"y = layer(x)\n",
"5"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2a2b6474-f64b-4c28-ac83-5d1c7bbfd92f",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_522/3731920106.py:3: UserWarning: This is a warning message\n",
" warnings.warn(\"This is a warning message\")\n"
]
}
],
"source": [
"import warnings\n",
"\n",
"warnings.warn(\"This is a warning message\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
40 changes: 40 additions & 0 deletions nbgrader/tests/preprocessors/test_ignorepattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
import os

from ...preprocessors import IgnorePattern
from .base import BaseTestPreprocessor


@pytest.fixture
def preprocessor():
pp = IgnorePattern()
pp.pattern = r"\[.*\.cpp.*\] Could not initialize NNPACK! Reason: Unsupported hardware."
return pp


class TestIgnorePattern(BaseTestPreprocessor):

def test_remove_matching_output(self, preprocessor):
nb = self._read_nb(os.path.join("files", "warning-pattern.ipynb"))
cell = nb.cells[0]

outputs = cell.outputs
assert len(outputs) == 2

cell, _ = preprocessor.preprocess_cell(cell, {}, 0)

assert len(cell.outputs) == 1


def test_skip_nonmatching_output(self, preprocessor):
nb = self._read_nb(os.path.join("files", "warning-pattern.ipynb"))
cell = nb.cells[1]

outputs = cell.outputs
assert len(outputs) == 1

cell, _ = preprocessor.preprocess_cell(cell, {}, 1)

assert len(cell.outputs) == 1
assert cell.outputs[0].name == "stderr"

0 comments on commit ef44180

Please sign in to comment.