Skip to content

Commit

Permalink
Merge branch 'main' into 1.3.X
Browse files Browse the repository at this point in the history
  • Loading branch information
reidjohnson committed Apr 15, 2024
2 parents 07646f0 + ff9eafe commit 539b7e0
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"sphinx_design",
"sphinxcontrib.bibtex",
"sphinxext_altair.altairplot",
"sphinxext.directives",
"sphinxext.gallery",
]

Expand Down
107 changes: 107 additions & 0 deletions docs/sphinxext/directives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from docutils import nodes
from docutils.parsers.rst import Directive
from sphinx.locale import _


def process_text(text):
"""Process the text to identify and format literals."""
parts = []
start = 0
while True:
start_literal = text.find("`", start)
if start_literal == -1:
parts.append(nodes.Text(text[start:]))
break
parts.append(nodes.Text(text[start:start_literal]))
end_literal = text.find("`", start_literal + 1)
if end_literal == -1:
break # unmatched backticks
literal_text = text[start_literal + 1 : end_literal]
parts.append(nodes.literal(literal_text, literal_text))
start = end_literal + 1
return parts


class div(nodes.General, nodes.Element):
@staticmethod
def visit_div(self, node):
self.body.append(self.starttag(node, "div"))

@staticmethod
def depart_div(self, node=None):
self.body.append("</div>\n")


class span(nodes.Inline, nodes.TextElement):
@staticmethod
def visit_span(self, node):
self.body.append(self.starttag(node, "span", ""))

@staticmethod
def depart_span(self, node=None):
self.body.append("</span>")


class SklearnVersionAddedDirective(Directive):
"""Custom directive to denote the version additions to scikit-learn."""

required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
has_content = True

def run(self):
text = None
if len(self.arguments[0].split("\n", 1)) > 1:
version, text = self.arguments[0].split("\n", 1)
else:
version = self.arguments[0]
container = div(classes=["versionadded"])
paragraph = nodes.paragraph()
span_node = span(
"",
_(f"New in scikit-learn version {version}{'.' if text is None else ': '} "),
classes=["versionmodified", "added"],
)
paragraph += span_node
if text is not None:
paragraph += process_text(text)
container += paragraph
self.state.nested_parse(self.content, self.content_offset, container)
return [container]


class SklearnVersionChangedDirective(Directive):
"""Custom directive to denote the version changes to scikit-learn."""

required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
has_content = True

def run(self):
text = None
if len(self.arguments[0].split("\n")) > 1:
version, text = self.arguments[0].split("\n", 1)
else:
version = self.arguments[0]
container = div(classes=["versionchanged"])
paragraph = nodes.paragraph()
span_node = span(
"",
_(f"Changed in scikit-learn version {version}{'.' if text is None else ': '} "),
classes=["versionmodified", "changed"],
)
paragraph += span_node
if text is not None:
paragraph += process_text(text)
container += paragraph
self.state.nested_parse(self.content, self.content_offset, container)
return [container]


def setup(app):
app.add_node(div, html=(div.visit_div, div.depart_div))
app.add_node(span, html=(span.visit_span, span.depart_span))
app.add_directive("sklearn-versionadded", SklearnVersionAddedDirective)
app.add_directive("sklearn-versionchanged", SklearnVersionChangedDirective)
2 changes: 1 addition & 1 deletion examples/plot_quantile_extrapolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def train_test_split(train_indices, **kwargs):


def prob_randomized_pi(qmat, y, coverage):
"""Calculate calibration probability"""
"""Calculate calibration probability."""
alpha_included = np.mean((qmat[:, 0] <= y) & (y <= qmat[:, 1]))
alpha_excluded = np.mean((qmat[:, 0] < y) & (y < qmat[:, 1]))
if coverage <= alpha_excluded:
Expand Down

0 comments on commit 539b7e0

Please sign in to comment.