-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Autograde Preprocessor: IgnorePattern (#1904)
* Create IgnorePattern Preprocessor * Disable and add to the list by default
- Loading branch information
1 parent
338ec4d
commit ef44180
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|