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

Issue #1080 : Fixing Parsing code for documentation retrieval in Python 3.13. #1082

Merged
merged 8 commits into from
Jan 18, 2025
23 changes: 14 additions & 9 deletions skorch/classifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""NeuralNet subclasses for classification tasks."""

import re
import textwrap

import numpy as np
from sklearn.base import ClassifierMixin
Expand Down Expand Up @@ -35,21 +36,23 @@
skorch behavior should be restored, i.e. raising an
``AttributeError``, pass an empty list."""

neural_net_clf_additional_attribute = """classes_ : array, shape (n_classes, )
neural_net_clf_additional_attribute = """ classes_ : array, shape (n_classes, )
A list of class labels known to the classifier.

"""


def get_neural_net_clf_doc(doc):
doc = neural_net_clf_doc_start + " " + doc.split("\n ", 4)[-1]
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+){1,99}')
indentation = " "
# dedent/indent roundtrip required for consistent indention in both
# Python <3.13 and Python >=3.13
# Because <3.13 => not automatic dedent, but it is the case in >=3.13
doc = neural_net_clf_doc_start + " " + textwrap.indent(textwrap.dedent(doc.split("\n", 5)[-1]), indentation)
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+|.){1,99}')
start, end = pattern.search(doc).span()
doc = doc[:start] + neural_net_clf_additional_text + doc[end:]
doc = doc + neural_net_clf_additional_attribute
return doc


# pylint: disable=missing-docstring
class NeuralNetClassifier(ClassifierMixin, NeuralNet):
__doc__ = get_neural_net_clf_doc(NeuralNet.__doc__)
Expand Down Expand Up @@ -249,15 +252,17 @@ def predict(self, X):
Probabilities above this threshold is classified as 1. ``threshold``
is used by ``predict`` and ``predict_proba`` for classification."""


def get_neural_net_binary_clf_doc(doc):
doc = neural_net_binary_clf_doc_start + " " + doc.split("\n ", 4)[-1]
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+){1,99}')
indentation = " "
# dedent/indent roundtrip required for consistent indention in both
# Python <3.13 and Python >=3.13
# Because <3.13 => not automatic dedent, but it is the case in >=3.13
doc = neural_net_binary_clf_doc_start + " " + textwrap.indent(textwrap.dedent(doc.split("\n", 5)[-1]), indentation)
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+|.){1,99}')
start, end = pattern.search(doc).span()
doc = doc[:start] + neural_net_binary_clf_criterion_text + doc[end:]
return doc


class NeuralNetBinaryClassifier(ClassifierMixin, NeuralNet):
# pylint: disable=missing-docstring
__doc__ = get_neural_net_binary_clf_doc(NeuralNet.__doc__)
Expand Down
11 changes: 7 additions & 4 deletions skorch/regressor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""NeuralNet subclasses for regression tasks."""

import re
import textwrap

from sklearn.base import RegressorMixin
import torch
Expand All @@ -23,15 +24,17 @@
criterion : torch criterion (class, default=torch.nn.MSELoss)
Mean squared error loss."""


def get_neural_net_reg_doc(doc):
doc = neural_net_reg_doc_start + " " + doc.split("\n ", 4)[-1]
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+){1,99}')
indentation = " "
# dedent/indent roundtrip required for consistent indention in both
# Python <3.13 and Python >=3.13
# Because <3.13 => not automatic dedent, but it is the case in >=3.13
doc = neural_net_reg_doc_start + " " + textwrap.indent(textwrap.dedent(doc.split("\n", 5)[-1]), indentation)
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+|.){1,99}')
start, end = pattern.search(doc).span()
doc = doc[:start] + neural_net_reg_criterion_text + doc[end:]
return doc


# pylint: disable=missing-docstring
class NeuralNetRegressor(RegressorMixin, NeuralNet):
__doc__ = get_neural_net_reg_doc(NeuralNet.__doc__)
Expand Down
Loading