Skip to content

Commit 68e8d97

Browse files
committed
update doc
1 parent 414f2bc commit 68e8d97

File tree

134 files changed

+1618
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+1618
-273
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:github_url: {{ fullname | modurl }}
2+
3+
{{ fullname | api_image }}
4+
5+
{% extends "!autosummary/base.rst" %}
6+
7+
.. http://www.sphinx-doc.org/en/stable/ext/autosummary.html#customizing-templates
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{{ fullname | escape | underline}}
2+
3+
.. currentmodule:: {{ module }}
4+
5+
.. add toctree option to make autodoc generate the pages
6+
7+
.. autoclass:: {{ objname }}
8+
9+
{% block attributes %}
10+
{% if attributes %}
11+
.. rubric:: Attributes
12+
13+
.. autosummary::
14+
:toctree: .
15+
{% for item in attributes %}
16+
~{{ fullname }}.{{ item }}
17+
{%- endfor %}
18+
{% endif %}
19+
{% endblock %}
20+
21+
{% block methods %}
22+
{% if methods %}
23+
.. rubric:: Methods
24+
25+
.. autosummary::
26+
:toctree: .
27+
{% for item in methods %}
28+
{%- if item != '__init__' %}
29+
~{{ fullname }}.{{ item }}
30+
{%- endif -%}
31+
{%- endfor %}
32+
{% endif %}
33+
{% endblock %}

doc/conf.py

Lines changed: 247 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"numpydoc",
3333
]
3434

35+
napoleon_google_docstring = False
36+
napoleon_numpy_docstring = True
37+
napoleon_include_init_with_doc = False
38+
napoleon_use_rtype = False
39+
napoleon_custom_sections = [("Params", "Parameters")]
3540

3641
# this is needed for some reason...
3742
# see https://github.com/numpy/numpydoc/issues/69
@@ -63,8 +68,8 @@
6368
# The master toctree document.
6469
master_doc = "index"
6570

66-
project = u"scikit-dimension"
67-
copyright = u"2020, Jonathan Bac"
71+
project = "scikit-dimension"
72+
copyright = "2020, Jonathan Bac"
6873

6974
# The short X.Y version.
7075
version = __version__
@@ -216,8 +221,8 @@
216221
(
217222
"index",
218223
"scikit-dimension.tex",
219-
u"scikit-dimension Documentation",
220-
u"Jonathan Bac",
224+
"scikit-dimension Documentation",
225+
"Jonathan Bac",
221226
"manual",
222227
),
223228
]
@@ -313,3 +318,241 @@
313318
def setup(app):
314319
# a copy button to copy snippet of code from the documentation
315320
app.add_stylesheet("css/custom.css")
321+
322+
323+
# -- Options for other output ------------------------------------------
324+
import inspect
325+
from pathlib import Path
326+
from typing import Optional, Union, Mapping
327+
import logging
328+
from sphinx.application import Sphinx
329+
from sphinx.ext import autosummary
330+
331+
logger = logging.getLogger(__name__)
332+
author = "Jonathan Bac"
333+
title = "scikit_dimension"
334+
title_doc = f"{project} documentation"
335+
336+
latex_documents = [(master_doc, f"{project}.tex", title_doc, author, "manual")]
337+
man_pages = [(master_doc, project, title_doc, [author], 1)]
338+
texinfo_documents = [
339+
(master_doc, project, title_doc, author, project, title, "Miscellaneous")
340+
]
341+
342+
343+
# -- generate_options override ------------------------------------------
344+
345+
346+
def process_generate_options(app: Sphinx):
347+
genfiles = app.config.autosummary_generate
348+
349+
if genfiles and not hasattr(genfiles, "__len__"):
350+
env = app.builder.env
351+
genfiles = [
352+
env.doc2path(x, base=None)
353+
for x in env.found_docs
354+
if Path(env.doc2path(x)).is_file()
355+
]
356+
if not genfiles:
357+
return
358+
359+
from sphinx.ext.autosummary.generate import generate_autosummary_docs
360+
361+
ext = app.config.source_suffix
362+
genfiles = [
363+
genfile + (not genfile.endswith(tuple(ext)) and ext[0] or "")
364+
for genfile in genfiles
365+
]
366+
367+
suffix = autosummary.get_rst_suffix(app)
368+
if suffix is None:
369+
return
370+
371+
generate_autosummary_docs(
372+
genfiles,
373+
builder=app.builder,
374+
warn=logger.warning,
375+
info=logger.info,
376+
suffix=suffix,
377+
base_path=app.srcdir,
378+
imported_members=True,
379+
app=app,
380+
)
381+
382+
383+
autosummary.process_generate_options = process_generate_options
384+
385+
386+
# -- GitHub URLs for class and method pages ------------------------------------------
387+
388+
389+
def get_obj_module(qualname):
390+
"""Get a module/class/attribute and its original module by qualname"""
391+
modname = qualname
392+
classname = None
393+
attrname = None
394+
while modname not in sys.modules:
395+
attrname = classname
396+
modname, classname = modname.rsplit(".", 1)
397+
398+
# retrieve object and find original module name
399+
if classname:
400+
cls = getattr(sys.modules[modname], classname)
401+
modname = cls.__module__
402+
obj = getattr(cls, attrname) if attrname else cls
403+
else:
404+
obj = None
405+
406+
return obj, sys.modules[modname]
407+
408+
409+
def get_linenos(obj):
410+
"""Get an object’s line numbers"""
411+
try:
412+
lines, start = inspect.getsourcelines(obj)
413+
except TypeError:
414+
return None, None
415+
else:
416+
return start, start + len(lines) - 1
417+
418+
419+
# set project_dir: project/docs/source/conf.py/../../.. → project/
420+
project_dir = Path(__file__).parent.parent.parent
421+
github_url_scvelo = "https://github.com/j-bac/scikit-dimension/tree/master"
422+
from pathlib import PurePosixPath
423+
424+
425+
def modurl(qualname):
426+
"""Get the full GitHub URL for some object’s qualname."""
427+
obj, module = get_obj_module(qualname)
428+
github_url = github_url_scvelo
429+
# try:
430+
path = PurePosixPath(Path(module.__file__).resolve().relative_to(project_dir))
431+
# except ValueError:
432+
# trying to document something from another package
433+
# github_url = (
434+
# if "read" in qualname
435+
# )
436+
# path = "/".join(module.__file__.split("/")[-2:])
437+
start, end = get_linenos(obj)
438+
fragment = f"#L{start}-L{end}" if start and end else ""
439+
return f"{github_url}/{path}{fragment}"
440+
441+
442+
def api_image(qualname: str) -> Optional[str]:
443+
path = Path(__file__).parent / f"{qualname}.png"
444+
print(path, path.is_file())
445+
return (
446+
f".. image:: {path.name}\n :width: 200\n :align: right"
447+
if path.is_file()
448+
else ""
449+
)
450+
451+
452+
# modify the default filters
453+
from jinja2.defaults import DEFAULT_FILTERS
454+
455+
DEFAULT_FILTERS.update(modurl=modurl, api_image=api_image)
456+
457+
# -- Override some classnames in autodoc --------------------------------------------
458+
459+
import sphinx_autodoc_typehints
460+
461+
qualname_overrides = {
462+
"anndata.base.AnnData": "anndata.AnnData",
463+
}
464+
465+
fa_orig = sphinx_autodoc_typehints.format_annotation
466+
467+
468+
def format_annotation(annotation):
469+
if getattr(annotation, "__origin__", None) is Union or hasattr(
470+
annotation, "__union_params__"
471+
):
472+
params = getattr(annotation, "__union_params__", None) or getattr(
473+
annotation, "__args__", None
474+
)
475+
return ", ".join(map(format_annotation, params))
476+
if getattr(annotation, "__origin__", None) is Mapping:
477+
return ":class:`~typing.Mapping`"
478+
if inspect.isclass(annotation):
479+
full_name = f"{annotation.__module__}.{annotation.__qualname__}"
480+
override = qualname_overrides.get(full_name)
481+
if override is not None:
482+
return f":py:class:`~{qualname_overrides[full_name]}`"
483+
return fa_orig(annotation)
484+
485+
486+
sphinx_autodoc_typehints.format_annotation = format_annotation
487+
488+
489+
# -- Prettier Param docs --------------------------------------------
490+
491+
from typing import Dict, List, Tuple
492+
from docutils import nodes
493+
from sphinx import addnodes
494+
from sphinx.domains.python import PyTypedField, PyObject
495+
from sphinx.environment import BuildEnvironment
496+
497+
498+
class PrettyTypedField(PyTypedField):
499+
list_type = nodes.definition_list
500+
501+
def make_field(
502+
self,
503+
types: Dict[str, List[nodes.Node]],
504+
domain: str,
505+
items: Tuple[str, List[nodes.inline]],
506+
env: BuildEnvironment = None,
507+
) -> nodes.field:
508+
def makerefs(rolename, name, node):
509+
return self.make_xrefs(rolename, domain, name, node, env=env)
510+
511+
def handle_item(
512+
fieldarg: str, content: List[nodes.inline]
513+
) -> nodes.definition_list_item:
514+
head = nodes.term()
515+
head += makerefs(self.rolename, fieldarg, addnodes.literal_strong)
516+
fieldtype = types.pop(fieldarg, None)
517+
if fieldtype is not None:
518+
head += nodes.Text(" : ")
519+
if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text):
520+
(text_node,) = fieldtype # type: nodes.Text
521+
head += makerefs(
522+
self.typerolename, text_node.astext(), addnodes.literal_emphasis
523+
)
524+
else:
525+
head += fieldtype
526+
527+
body_content = nodes.paragraph("", "", *content)
528+
body = nodes.definition("", body_content)
529+
530+
return nodes.definition_list_item("", head, body)
531+
532+
fieldname = nodes.field_name("", self.label)
533+
if len(items) == 1 and self.can_collapse:
534+
fieldarg, content = items[0]
535+
bodynode = handle_item(fieldarg, content)
536+
else:
537+
bodynode = self.list_type()
538+
for fieldarg, content in items:
539+
bodynode += handle_item(fieldarg, content)
540+
fieldbody = nodes.field_body("", bodynode)
541+
return nodes.field("", fieldname, fieldbody)
542+
543+
544+
# replace matching field types with ours
545+
PyObject.doc_field_types = [
546+
PrettyTypedField(
547+
ft.name,
548+
names=ft.names,
549+
typenames=ft.typenames,
550+
label=ft.label,
551+
rolename=ft.rolename,
552+
typerolename=ft.typerolename,
553+
can_collapse=ft.can_collapse,
554+
)
555+
if isinstance(ft, PyTypedField)
556+
else ft
557+
for ft in PyObject.doc_field_types
558+
]

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ ipykernel
88
sphinx>=1.7
99
nbsphinx>=0.7
1010
numpydoc
11-
pandoc
11+
pandoc
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
:github_url: https://github.com/theislab/scvelo/tree/master/scikit-dimension/skdim/datasets.py#L383-L436
2+
3+
4+
5+
skdim.datasets.BenchmarkManifolds.generate
6+
==========================================
7+
8+
.. currentmodule:: skdim.datasets
9+
10+
.. automethod:: BenchmarkManifolds.generate

doc/skdim.datasets.BenchmarkManifolds.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ skdim.datasets.BenchmarkManifolds
33

44
.. currentmodule:: skdim.datasets
55

6+
.. add toctree option to make autodoc generate the pages
7+
68
.. autoclass:: BenchmarkManifolds
79

810

9-
.. automethod:: __init__
11+
12+
1013

1114

15+
1216
.. rubric:: Methods
1317

1418
.. autosummary::
19+
:toctree: .
1520

16-
~BenchmarkManifolds.__init__
17-
~BenchmarkManifolds.generate
21+
~skdim.datasets.BenchmarkManifolds.generate
1822

1923

2024

doc/skdim.datasets.hyperBall.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:github_url: https://github.com/theislab/scvelo/tree/master/scikit-dimension/skdim/datasets.py#L38-L69
2+
3+
4+
15
skdim.datasets.hyperBall
26
========================
37

doc/skdim.datasets.hyperSphere.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:github_url: https://github.com/theislab/scvelo/tree/master/scikit-dimension/skdim/datasets.py#L72-L95
2+
3+
4+
15
skdim.datasets.hyperSphere
26
==========================
37

doc/skdim.datasets.hyperTwinPeaks.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:github_url: https://github.com/theislab/scvelo/tree/master/scikit-dimension/skdim/datasets.py#L98-L122
2+
3+
4+
15
skdim.datasets.hyperTwinPeaks
26
=============================
37

doc/skdim.datasets.lineDiskBall.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:github_url: https://github.com/theislab/scvelo/tree/master/scikit-dimension/skdim/datasets.py#L125-L187
2+
3+
4+
15
skdim.datasets.lineDiskBall
26
===========================
37

0 commit comments

Comments
 (0)