-
Notifications
You must be signed in to change notification settings - Fork 131
Automatic documentation for experiment and analysis #116
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
Merged
coruscating
merged 57 commits into
qiskit-community:main
from
nkanazawa1989:feature/auto-option-docs-gen
Jul 22, 2021
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
eb60945
initial commit model based experiment
c291e59
Revert "initial commit model based experiment"
c1608c4
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
d7cc988
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
017de23
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
991d412
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
e20a9dc
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
bed9481
wip options field
d473d5b
fix bug
3c911bb
update type handling
df55eec
black
eb525bc
add experiment options update and class docstring update
bc2f3ed
cleanup facades and make autodoc module a package
5f832a8
wip analysis documentation
82c7100
update analysis docs and reformat styles.
6fbfb03
black and typing
85ad101
wip lint
dc14160
replace LF
b30459d
strip LFs of input text
402c123
update irb docstring
60de870
ignore lint
a1acf25
unittest
4d6490e
Update qiskit_experiments/analysis/curve_analysis.py
nkanazawa1989 dfb350f
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 96b0e81
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 97236e6
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 94f731e
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 af6437a
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 8e1645a
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 b7e4a7d
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 166dec5
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 310dc1e
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 ee553b8
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 482b43b
Update qiskit_experiments/characterization/qubit_spectroscopy.py
nkanazawa1989 8b1ac23
Sphinx autodoc extension for documentation
7d25952
revert experiment modules
b6a0abd
remove old autodoc module
a66a327
Merge branch 'feature/auto-option-docs-gen' of github.com:nkanazawa19…
c14c2cc
fix old import
4815f6a
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into feat…
5925c26
remove change to base analysis
36d4b9a
update docs
fa8a4c0
lint
c8ef173
add example of analysis default options
768f2ff
move package under docs
bf9e3a4
fix import path
1e4dd09
fix missing docs
f6cda8f
comment from Eli
661d22e
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into feat…
38327ea
fix indent correction logic
cf320e6
update example
7eed8ac
robust to indent error
bb6dfcf
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into feat…
b497f7c
fix import paths
f7c907e
fix example documentation
5af2bf5
Merge branch 'main' into feature/auto-option-docs-gen
coruscating 23f77ab
Merge branch 'main' into feature/auto-option-docs-gen
coruscating File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Documentation extension for analysis class. | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| from docs._ext.custom_styles.styles import AnalysisDocstring | ||
| from qiskit_experiments.framework.base_analysis import BaseAnalysis | ||
| from sphinx.application import Sphinx | ||
| from sphinx.ext.autodoc import ClassDocumenter | ||
|
|
||
|
|
||
| class AnalysisDocumenter(ClassDocumenter): | ||
| """Sphinx extension for the custom documentation of the standard analysis class.""" | ||
|
|
||
| objtype = "analysis" | ||
| directivetype = 'class' | ||
| priority = 10 + ClassDocumenter.priority | ||
| option_spec = dict(ClassDocumenter.option_spec) | ||
|
|
||
| @classmethod | ||
| def can_document_member( | ||
| cls, member: Any, membername: str, isattr: bool, parent: Any | ||
| ) -> bool: | ||
| return isinstance(member, BaseAnalysis) | ||
|
|
||
| def add_content(self, more_content: Any, no_docstring: bool = False) -> None: | ||
| sourcename = self.get_sourcename() | ||
|
|
||
| # analysis class doesn't have explicit init method. | ||
| class_doc = self.get_doc()[0] | ||
|
|
||
| # format experiment documentation into the analysis style | ||
| class_doc_parser = AnalysisDocstring( | ||
| target_cls=self.object, | ||
| docstring_lines=class_doc, | ||
| config=self.env.app.config, | ||
| indent=self.content_indent, | ||
| ) | ||
|
|
||
| # write introduction | ||
| for i, line in enumerate(self.process_doc(class_doc_parser.generate_class_docs())): | ||
| self.add_line(line, sourcename, i) | ||
| self.add_line("", sourcename) | ||
|
|
||
| # method and attributes | ||
| if more_content: | ||
| for line, src in zip(more_content.data, more_content.items): | ||
| self.add_line(line, src[0], src[1]) | ||
|
|
||
|
|
||
| def setup(app: Sphinx): | ||
| existing_documenter = app.registry.documenters.get(AnalysisDocumenter.objtype) | ||
| if existing_documenter is None or not issubclass(existing_documenter, AnalysisDocumenter): | ||
| app.add_autodocumenter(AnalysisDocumenter, override=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Documentation extension for experiment class. | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| from docs._ext.custom_styles.styles import ExperimentDocstring | ||
| from qiskit.exceptions import QiskitError | ||
| from qiskit_experiments.framework.base_experiment import BaseExperiment | ||
| from sphinx.application import Sphinx | ||
| from sphinx.ext.autodoc import ClassDocumenter | ||
|
|
||
|
|
||
| class ExperimentDocumenter(ClassDocumenter): | ||
| """Sphinx extension for the custom documentation of the standard experiment class.""" | ||
|
|
||
| objtype = "experiment" | ||
| directivetype = 'class' | ||
| priority = 10 + ClassDocumenter.priority | ||
| option_spec = dict(ClassDocumenter.option_spec) | ||
|
|
||
| @classmethod | ||
| def can_document_member( | ||
| cls, member: Any, membername: str, isattr: bool, parent: Any | ||
| ) -> bool: | ||
| return isinstance(member, BaseExperiment) | ||
|
|
||
| def add_content(self, more_content: Any, no_docstring: bool = False) -> None: | ||
| sourcename = self.get_sourcename() | ||
|
|
||
| try: | ||
| class_doc, init_doc = self.get_doc() | ||
| except ValueError: | ||
| raise QiskitError( | ||
| f"Documentation of {self.name} doesn't match with the expected format." | ||
| "Please run sphinx build without using the experiment template." | ||
| ) | ||
|
|
||
| # format experiment documentation into the experiment style | ||
| class_doc_parser = ExperimentDocstring( | ||
| target_cls=self.object, | ||
| docstring_lines=class_doc, | ||
| config=self.env.app.config, | ||
| indent=self.content_indent, | ||
| ) | ||
|
|
||
| # write introduction | ||
| for i, line in enumerate(self.process_doc(class_doc_parser.generate_class_docs())): | ||
| self.add_line(line, sourcename, i) | ||
| self.add_line("", sourcename) | ||
|
|
||
| # write init method documentation | ||
| self.add_line(".. rubric:: Initialization", sourcename) | ||
| self.add_line("", sourcename) | ||
| for i, line in enumerate(self.process_doc([init_doc])): | ||
| self.add_line(line, sourcename, i) | ||
| self.add_line("", sourcename) | ||
|
|
||
| # method and attributes | ||
| if more_content: | ||
| for line, src in zip(more_content.data, more_content.items): | ||
| self.add_line(line, src[0], src[1]) | ||
|
|
||
|
|
||
| def setup(app: Sphinx): | ||
| existing_documenter = app.registry.documenters.get(ExperimentDocumenter.objtype) | ||
| if existing_documenter is None or not issubclass(existing_documenter, ExperimentDocumenter): | ||
| app.add_autodocumenter(ExperimentDocumenter, override=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Helper directive to generate reference in convenient form. | ||
| """ | ||
| import arxiv | ||
|
|
||
| from docutils import nodes | ||
| from docutils.parsers.rst import Directive | ||
| from sphinx.application import Sphinx | ||
|
|
||
|
|
||
| class WebSite(Directive): | ||
| """A custom helper directive for showing website link. | ||
|
|
||
| This can be used, for example, | ||
|
|
||
| .. code-block:: | ||
|
|
||
| .. ref_website:: qiskit-experiments, https://github.com/Qiskit/qiskit-experiments | ||
|
|
||
| """ | ||
| required_arguments = 1 | ||
| optional_arguments = 0 | ||
| final_argument_whitespace = True | ||
|
|
||
| def run(self): | ||
| try: | ||
| name, url = self.arguments[0].split(",") | ||
| except ValueError: | ||
| raise ValueError( | ||
| f"{self.arguments[0]} is invalid website directive format. " | ||
| "Name and URL should be separated by a single comma." | ||
| ) | ||
|
|
||
| link_name = nodes.paragraph(text=f"{name} ") | ||
| link_name += nodes.reference(text="(open)", refuri=url) | ||
|
|
||
| return [link_name] | ||
|
|
||
|
|
||
| class Arxiv(Directive): | ||
| """A custom helper directive for generating journal information from arXiv id. | ||
|
|
||
| This directive takes two arguments | ||
|
|
||
| - Arbitrary reference name (no white space should be included) | ||
|
|
||
| - arXiv ID | ||
|
|
||
| This can be used, for example, | ||
|
|
||
| .. code-block:: | ||
|
|
||
| .. ref_arxiv:: qasm3-paper 2104.14722 | ||
|
|
||
| If an article is not found, no journal information will be shown. | ||
|
|
||
| """ | ||
| required_arguments = 2 | ||
| optional_arguments = 0 | ||
| final_argument_whitespace = False | ||
|
|
||
| def run(self): | ||
|
|
||
| # search arXiv database | ||
| try: | ||
| search = arxiv.Search(id_list=[self.arguments[1]]) | ||
| paper = next(search.results()) | ||
| except Exception: | ||
| return [] | ||
|
|
||
| # generate journal link nodes | ||
| ret_node = nodes.paragraph() | ||
|
|
||
| journal = "" | ||
| if paper.journal_ref: | ||
| journal += f", {paper.journal_ref}, " | ||
| if paper.doi: | ||
| journal += f"doi: {paper.doi}" | ||
|
|
||
| ret_node += nodes.Text(f"[{self.arguments[0]}] ") | ||
| ret_node += nodes.Text(", ".join([author.name for author in paper.authors]) + ", ") | ||
| ret_node += nodes.emphasis(text=f"{paper.title}") | ||
| if journal: | ||
| ret_node += nodes.Text(journal) | ||
| ret_node += nodes.Text(" ") | ||
| ret_node += nodes.reference(text="(open)", refuri=paper.pdf_url) | ||
|
|
||
| return [ret_node] | ||
|
|
||
|
|
||
| def setup(app: Sphinx): | ||
| app.add_directive("ref_arxiv", Arxiv) | ||
| app.add_directive("ref_website", WebSite) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| ======================================================================= | ||
| Example Documentation (:mod:`qiskit_experiments.documentation.example`) | ||
| ======================================================================= | ||
|
|
||
| .. currentmodule:: qiskit_experiments.documentation.example | ||
|
|
||
|
|
||
| .. warning:: | ||
|
|
||
| This module is just an example for documentation. Do not import. | ||
|
|
||
| .. note:: | ||
|
|
||
| Under the autosummary directive you need to set template to trigger custom documentation. | ||
|
|
||
|
|
||
| Experiments | ||
| =========== | ||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
| :template: autosummary/experiment.rst | ||
|
|
||
| DocumentedExperiment | ||
|
|
||
| Analysis | ||
| ======== | ||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
| :template: autosummary/analysis.rst | ||
|
|
||
nkanazawa1989 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DocumentedCurveAnalysis | ||
|
|
||
| """ | ||
|
|
||
| from .example_experiment import DocumentedExperiment, DocumentedCurveAnalysis | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.