Skip to content

Commit b7cb9d9

Browse files
authored
Merge pull request explosion#6229 from svlandeg/bugfix/disabled
2 parents 97ff090 + 06b9d21 commit b7cb9d9

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

spacy/errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ class Errors:
456456
"issue tracker: http://github.com/explosion/spaCy/issues")
457457

458458
# TODO: fix numbering after merging develop into master
459+
E900 = ("Could not run the full 'nlp' pipeline for evaluation. If you specified "
460+
"frozen components, make sure they were already initialized and trained. ")
459461
E901 = ("Failed to remove existing output directory: {path}. If your "
460462
"config and the components you train change between runs, a "
461463
"non-empty output directory can lead to stale pipeline data. To "

spacy/language.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,9 @@ def select_pipes(
10341034
)
10351035
)
10361036
disable = to_disable
1037+
# DisabledPipes will restore the pipes in 'disable' when it's done, so we need to exclude
1038+
# those pipes that were already disabled.
1039+
disable = [d for d in disable if d not in self._disabled]
10371040
return DisabledPipes(self, disable)
10381041

10391042
def make_doc(self, text: str) -> Doc:

spacy/tests/pipeline/test_pipe_methods.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,26 @@ def test_enable_pipes_method(nlp, name):
129129

130130
@pytest.mark.parametrize("name", ["my_component"])
131131
def test_disable_pipes_context(nlp, name):
132+
"""Test that an enabled component stays enabled after running the context manager."""
132133
nlp.add_pipe("new_pipe", name=name)
133134
assert nlp.has_pipe(name)
134135
with nlp.select_pipes(disable=name):
135136
assert not nlp.has_pipe(name)
136137
assert nlp.has_pipe(name)
137138

138139

140+
@pytest.mark.parametrize("name", ["my_component"])
141+
def test_disable_pipes_context_restore(nlp, name):
142+
"""Test that a disabled component stays disabled after running the context manager."""
143+
nlp.add_pipe("new_pipe", name=name)
144+
assert nlp.has_pipe(name)
145+
nlp.disable_pipes(name)
146+
assert not nlp.has_pipe(name)
147+
with nlp.select_pipes(disable=name):
148+
assert not nlp.has_pipe(name)
149+
assert not nlp.has_pipe(name)
150+
151+
139152
def test_select_pipes_list_arg(nlp):
140153
for name in ["c1", "c2", "c3"]:
141154
nlp.add_pipe("new_pipe", name=name)

spacy/training/loop.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ def create_evaluation_callback(
249249

250250
def evaluate() -> Tuple[float, Dict[str, float]]:
251251
dev_examples = list(dev_corpus(nlp))
252-
scores = nlp.evaluate(dev_examples)
252+
try:
253+
scores = nlp.evaluate(dev_examples)
254+
except KeyError as e:
255+
raise KeyError(Errors.E900) from e
253256
# Calculate a weighted sum based on score_weights for the main score.
254257
# We can only consider scores that are ints/floats, not dicts like
255258
# entity scores per type etc.

0 commit comments

Comments
 (0)