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

Add Support for monotonic_cst Parameter #49

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading