Unverified Commit abd164c2 authored by Yuge Zhang's avatar Yuge Zhang Committed by GitHub
Browse files

Bootstrapping tutorials in documentation (#4522)

parent cc122226
f87a716bc3274d0f9a77db503198ac4a
\ No newline at end of file
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/nas_quick_start_mnist.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here <sphx_glr_download_tutorials_nas_quick_start_mnist.py>`
to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_tutorials_nas_quick_start_mnist.py:
Get started with NAS on MNIST
=============================
.. GENERATED FROM PYTHON SOURCE LINES 7-10
.. code-block:: default
a = (1, 2, 3)
a
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
(1, 2, 3)
.. GENERATED FROM PYTHON SOURCE LINES 11-12
.. code-block:: default
print('hello')
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
hello
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.002 seconds)
.. _sphx_glr_download_tutorials_nas_quick_start_mnist.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: nas_quick_start_mnist.py <nas_quick_start_mnist.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: nas_quick_start_mnist.ipynb <nas_quick_start_mnist.ipynb>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Start and Manage a New Experiment\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configure Search Space\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"search_space = {\n \"C\": {\"_type\": \"quniform\", \"_value\": [0.1, 1, 0.1]},\n \"kernel\": {\"_type\": \"choice\", \"_value\": [\"linear\", \"rbf\", \"poly\", \"sigmoid\"]},\n \"degree\": {\"_type\": \"choice\", \"_value\": [1, 2, 3, 4]},\n \"gamma\": {\"_type\": \"quniform\", \"_value\": [0.01, 0.1, 0.01]},\n \"coef0\": {\"_type\": \"quniform\", \"_value\": [0.01, 0.1, 0.01]}\n}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configure Experiment\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from nni.experiment import Experiment\nexperiment = Experiment('local')\nexperiment.config.experiment_name = 'Example'\nexperiment.config.trial_concurrency = 2\nexperiment.config.max_trial_number = 10\nexperiment.config.search_space = search_space\nexperiment.config.trial_command = 'python scripts/trial_sklearn.py'\nexperiment.config.trial_code_directory = './'\nexperiment.config.tuner.name = 'TPE'\nexperiment.config.tuner.class_args['optimize_mode'] = 'maximize'\nexperiment.config.training_service.use_active_gpu = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start Experiment\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"experiment.start(8080)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Experiment View & Control\n\nView the status of experiment.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"experiment.get_status()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wait until at least one trial finishes.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import time\n\nfor _ in range(10):\n stats = experiment.get_job_statistics()\n if any(stat['trialJobStatus'] == 'SUCCEEDED' for stat in stats):\n break\n time.sleep(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Export the experiment data.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"experiment.export_data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get metric of jobs\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"experiment.get_job_metrics()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stop Experiment\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"experiment.stop()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
"""
Start and Manage a New Experiment
=================================
"""
# %%
# Configure Search Space
# ----------------------
search_space = {
"C": {"_type": "quniform", "_value": [0.1, 1, 0.1]},
"kernel": {"_type": "choice", "_value": ["linear", "rbf", "poly", "sigmoid"]},
"degree": {"_type": "choice", "_value": [1, 2, 3, 4]},
"gamma": {"_type": "quniform", "_value": [0.01, 0.1, 0.01]},
"coef0": {"_type": "quniform", "_value": [0.01, 0.1, 0.01]}
}
# %%
# Configure Experiment
# --------------------
from nni.experiment import Experiment
experiment = Experiment('local')
experiment.config.experiment_name = 'Example'
experiment.config.trial_concurrency = 2
experiment.config.max_trial_number = 10
experiment.config.search_space = search_space
experiment.config.trial_command = 'python scripts/trial_sklearn.py'
experiment.config.trial_code_directory = './'
experiment.config.tuner.name = 'TPE'
experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
experiment.config.training_service.use_active_gpu = True
# %%
# Start Experiment
# ----------------
experiment.start(8080)
# %%
# Experiment View & Control
# -------------------------
#
# View the status of experiment.
experiment.get_status()
# %%
# Wait until at least one trial finishes.
import time
for _ in range(10):
stats = experiment.get_job_statistics()
if any(stat['trialJobStatus'] == 'SUCCEEDED' for stat in stats):
break
time.sleep(10)
# %%
# Export the experiment data.
experiment.export_data()
# %%
# Get metric of jobs
experiment.get_job_metrics()
# %%
# Stop Experiment
# ---------------
experiment.stop()
9f822647d89f05264b70d1ae1c473be1
\ No newline at end of file
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/nni_experiment.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here <sphx_glr_download_tutorials_nni_experiment.py>`
to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_tutorials_nni_experiment.py:
Start and Manage a New Experiment
=================================
.. GENERATED FROM PYTHON SOURCE LINES 7-9
Configure Search Space
----------------------
.. GENERATED FROM PYTHON SOURCE LINES 9-18
.. code-block:: default
search_space = {
"C": {"_type": "quniform", "_value": [0.1, 1, 0.1]},
"kernel": {"_type": "choice", "_value": ["linear", "rbf", "poly", "sigmoid"]},
"degree": {"_type": "choice", "_value": [1, 2, 3, 4]},
"gamma": {"_type": "quniform", "_value": [0.01, 0.1, 0.01]},
"coef0": {"_type": "quniform", "_value": [0.01, 0.1, 0.01]}
}
.. GENERATED FROM PYTHON SOURCE LINES 19-21
Configure Experiment
--------------------
.. GENERATED FROM PYTHON SOURCE LINES 21-34
.. code-block:: default
from nni.experiment import Experiment
experiment = Experiment('local')
experiment.config.experiment_name = 'Example'
experiment.config.trial_concurrency = 2
experiment.config.max_trial_number = 10
experiment.config.search_space = search_space
experiment.config.trial_command = 'python scripts/trial_sklearn.py'
experiment.config.trial_code_directory = './'
experiment.config.tuner.name = 'TPE'
experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
experiment.config.training_service.use_active_gpu = True
.. GENERATED FROM PYTHON SOURCE LINES 35-37
Start Experiment
----------------
.. GENERATED FROM PYTHON SOURCE LINES 37-39
.. code-block:: default
experiment.start(8080)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[2022-02-07 18:56:04] Creating experiment, Experiment ID: fl9vu67z
[2022-02-07 18:56:04] Starting web server...
[2022-02-07 18:56:05] Setting up...
[2022-02-07 18:56:05] Web UI URLs: http://127.0.0.1:8080 http://10.190.173.211:8080 http://172.17.0.1:8080 http://192.168.49.1:8080
.. GENERATED FROM PYTHON SOURCE LINES 40-44
Experiment View & Control
-------------------------
View the status of experiment.
.. GENERATED FROM PYTHON SOURCE LINES 44-46
.. code-block:: default
experiment.get_status()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
'RUNNING'
.. GENERATED FROM PYTHON SOURCE LINES 47-48
Wait until at least one trial finishes.
.. GENERATED FROM PYTHON SOURCE LINES 48-56
.. code-block:: default
import time
for _ in range(10):
stats = experiment.get_job_statistics()
if any(stat['trialJobStatus'] == 'SUCCEEDED' for stat in stats):
break
time.sleep(10)
.. GENERATED FROM PYTHON SOURCE LINES 57-58
Export the experiment data.
.. GENERATED FROM PYTHON SOURCE LINES 58-60
.. code-block:: default
experiment.export_data()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[TrialResult(parameter={'C': 0.9, 'kernel': 'rbf', 'degree': 4, 'gamma': 0.07, 'coef0': 0.03}, value=0.9733333333333334, trialJobId='dNOZt'), TrialResult(parameter={'C': 0.8, 'kernel': 'sigmoid', 'degree': 2, 'gamma': 0.01, 'coef0': 0.01}, value=0.9733333333333334, trialJobId='okYSD')]
.. GENERATED FROM PYTHON SOURCE LINES 61-62
Get metric of jobs
.. GENERATED FROM PYTHON SOURCE LINES 62-64
.. code-block:: default
experiment.get_job_metrics()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
{'okYSD': [TrialMetricData(timestamp=1644227777089, trialJobId='okYSD', parameterId='1', type='FINAL', sequence=0, data=0.9733333333333334)], 'dNOZt': [TrialMetricData(timestamp=1644227777357, trialJobId='dNOZt', parameterId='0', type='FINAL', sequence=0, data=0.9733333333333334)]}
.. GENERATED FROM PYTHON SOURCE LINES 65-67
Stop Experiment
---------------
.. GENERATED FROM PYTHON SOURCE LINES 67-68
.. code-block:: default
experiment.stop()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[2022-02-07 18:56:25] Stopping experiment, please wait...
[2022-02-07 18:56:28] Experiment stopped
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 24.662 seconds)
.. _sphx_glr_download_tutorials_nni_experiment.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: nni_experiment.py <nni_experiment.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: nni_experiment.ipynb <nni_experiment.ipynb>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
:orphan:
.. _sphx_glr_tutorials_sg_execution_times:
Computation times
=================
**00:24.663** total execution time for **tutorials** files:
+-----------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_tutorials_nni_experiment.py` (``nni_experiment.py``) | 00:24.662 | 0.0 MB |
+-----------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_tutorials_nas_quick_start_mnist.py` (``nas_quick_start_mnist.py``) | 00:00.002 | 0.0 MB |
+-----------------------------------------------------------------------------------+-----------+--------+
......@@ -42,3 +42,8 @@ nav.md-tabs .md-tabs__item:not(:last-child) .md-tabs__link:after {
.md-footer-copyright__highlight {
display: inline;
}
/* toc style */
.md-nav span.caption {
margin-top: 1.25em;
}
/* Customization to sphinx-gallery generated notebooks */
p.sphx-glr-script-out {
margin: 0;
padding: 0;
font-size: .7rem;
}
.sphx-glr-script-out .highlight {
margin: 0 !important;
}
.sphx-glr-script-out .highlight pre {
background-color: #f2f3fa !important;
padding: .6rem !important;
margin-bottom: 1.5rem;
}
div.sphx-glr-footer {
display: none;
}
.notebook-action-link {
padding-right: 1rem;
}
.sphx-glr-download-link-note {
margin-bottom: 1.5rem;
}
.notebook-action-div {
display: inline;
border-bottom: 1px solid #f3f4f7;
padding-right: 2rem;
padding-bottom: 0.625rem;
}
.notebook-action-link:hover .notebook-action-div {
/* match theme */
border-bottom-color: #f50057;
}
.notebook-action-link img {
width: 1.1rem;
height: 1.1rem;
margin-right: 10px;
vertical-align: middle;
}
.notebook-action-link div {
display: inline;
}
.notebook-action-link:not(:hover) div {
color: rgba(0, 0, 0, .68);
}
.card-link.admonition {
border-left: 0;
padding: 0.9rem;
}
.card-link .card-link-body {
min-height: 4rem;
position: relative;
}
.card-link-text {
padding-left: 4.9rem;
}
.card-link a:not(:hover) .card-link-text {
color: rgba(0, 0, 0, .68);
}
.card-link-text .card-link-title-container h4 {
margin: 0;
}
.card-link-text .card-link-summary {
margin-top: 0.3rem;
margin-bottom: 0.5rem;
font-size: 0.75rem;
}
.card-link-tag {
margin-right: 0.4rem;
background: #eeeff2;
border: 1px solid #e0dcda;
color: rgba(0, 0, 0, 0.7);
border-radius: 18px;
cursor: pointer;
display: inline-block;
position: relative;
padding: 0.1rem 0.6rem;
font-size: 0.7rem;
}
.card-link-icon {
position: absolute;
top: 0;
left: 0;
}
.card-link-icon img {
max-width: 80%;
max-height: 80%;
/* horizontal and vertical center */
/* https://stackoverflow.com/questions/7273338/how-to-vertically-align-an-image-inside-a-div */
text-align: center;
vertical-align: middle;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
/* link icon background color */
.card-link-icon.circle {
background-color: #283593;
border-radius: 50%;
width: 4rem;
height: 4rem;
padding: 0;
}
/* pallette */
.card-link-icon.red {
background-color: #C62828;
}
.card-link-icon.pink {
background-color: #AD1457;
}
.card-link-icon.purple {
background-color: #8E24AA;
}
.card-link-icon.deep-purple {
background-color: #512DA8;
}
.card-link-icon.blue {
background-color: #1565C0;
}
.card-link-icon.light-blue {
background-color: #0277BD;
}
.card-link-icon.cyan {
background-color: #006064;
}
.card-link-icon.teal {
background-color: #00796B;
}
.card-link-icon.green {
background-color: #2E7D32;
}
.card-link-icon.deep-orange {
background-color: #BF360C;
}
.card-link-icon.brown {
background-color: #6D4C41;
}
.card-link-icon.indigo {
background-color: #3949AB;
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 181.13 105" style="enable-background:new 0 0 181.13 105;" xml:space="preserve">
<style type="text/css">
.st0{fill:#FFA800;}
.st1{fill:#FFDD3D;}
</style>
<g>
<path class="st0" d="M26.84,52.6c0-1.78,0.18-3.51,0.52-5.18c0.34-1.67,0.85-3.29,1.5-4.83c0.65-1.54,1.45-3,2.37-4.37
c0.92-1.37,1.98-2.64,3.14-3.81L16.92,16.96C7.8,26.08,2.16,38.68,2.16,52.6S7.8,79.12,16.92,88.24l17.45-17.45
C29.72,66.13,26.84,59.7,26.84,52.6z"/>
<path class="st1" d="M52.56,26.87c6.84,0,13.03,2.68,17.64,7.03l14.23-20.35C75.74,6.45,64.65,2.19,52.56,2.19
c-13.92,0-26.52,5.64-35.64,14.76l17.45,17.45C39.03,29.75,45.46,26.87,52.56,26.87z"/>
<path class="st1" d="M70.2,71.29c-4.61,4.35-10.8,7.03-17.64,7.03c-7.1,0-13.53-2.88-18.19-7.53L16.92,88.24
C26.04,97.36,38.64,103,52.56,103c12.04,0,23.09-4.23,31.76-11.27L70.2,71.29z"/>
<path class="st0" d="M164.44,16.96l-17.45,17.45c4.66,4.66,7.53,11.09,7.53,18.19c0,14.21-11.52,25.72-25.72,25.72
c-7.1,0-13.53-2.88-18.19-7.53L93.16,88.24c9.12,9.12,21.72,14.76,35.64,14.76c27.84,0,50.4-22.57,50.4-50.4
C179.2,38.68,173.56,26.08,164.44,16.96z"/>
<path class="st1" d="M103.07,52.6c0-14.21,11.52-25.72,25.72-25.72c7.1,0,13.53,2.88,18.19,7.53l17.45-17.45
c-9.12-9.12-21.72-14.76-35.64-14.76c-27.84,0-50.4,22.57-50.4,50.4c0,13.92,5.64,26.52,14.76,35.64l17.45-17.45
C105.95,66.13,103.07,59.7,103.07,52.6z"/>
</g>
</svg>
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 105 105" style="enable-background:new 0 0 105 105;" xml:space="preserve">
<style type="text/css">
.st0{fill-rule:evenodd;clip-rule:evenodd;fill:#333333;}
</style>
<polygon class="st0" points="91.74,42.58 59.53,74.8 59.52,74.84 59.52,74.8 59.52,3 44.8,3 44.8,74.8 12.58,42.58 2.16,53
52.16,103 102.15,53 "/>
</svg>
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 105 105" style="enable-background:new 0 0 105 105;" xml:space="preserve">
<style type="text/css">
.st0{fill-rule:evenodd;clip-rule:evenodd;fill:#333333;}
</style>
<path class="st0" d="M52.54,2.44c-27.61,0-50,22.39-50,50c0,22.09,14.33,40.83,34.2,47.44c2.5,0.46,3.41-1.09,3.41-2.41
c0-1.19-0.04-4.33-0.07-8.5c-13.91,3.02-16.84-6.7-16.84-6.7c-2.27-5.78-5.55-7.31-5.55-7.31c-4.54-3.1,0.34-3.04,0.34-3.04
c5.02,0.35,7.66,5.15,7.66,5.15c4.46,7.64,11.7,5.43,14.55,4.15c0.45-3.23,1.75-5.43,3.17-6.68c-11.1-1.26-22.78-5.55-22.78-24.71
c0-5.46,1.95-9.92,5.15-13.42c-0.51-1.26-2.23-6.35,0.49-13.23c0,0,4.2-1.34,13.75,5.13c3.99-1.11,8.27-1.66,12.52-1.68
c4.25,0.02,8.52,0.57,12.52,1.68c9.55-6.47,13.74-5.13,13.74-5.13c2.73,6.88,1.01,11.97,0.5,13.23c3.2,3.5,5.14,7.96,5.14,13.42
c0,19.21-11.69,23.43-22.83,24.67c1.8,1.54,3.39,4.6,3.39,9.26c0,6.68-0.06,12.08-0.06,13.72c0,1.34,0.9,2.89,3.44,2.4
c19.85-6.62,34.16-25.35,34.16-47.44C102.54,24.83,80.15,2.44,52.54,2.44z"/>
</svg>
$(document).ready(function() {
const downloadNote = $(".sphx-glr-download-link-note.admonition.note");
if (downloadNote.length > 0) {
const githubLink = "https://github.com/microsoft/nni/blob/" + GIT_COMMIT_ID + "/examples/" + PAGENAME + ".py";
const notebookLink = $(".sphx-glr-download-jupyter .reference.download").attr("href");
// link to generated notebook file
const colabLink = "https://colab.research.google.com/github/microsoft/nni/blob/" + GIT_COMMIT_ID +
"/docs/source/" + PAGENAME + ".ipynb";
downloadNote.removeClass("admonition");
// the image links are stored in layout.html
// to leverage jinja engine
downloadNote.html(`
<a class="notebook-action-link" href="${colabLink}">
<div class="notebook-action-div">
<img src="${GALLERY_LINKS.colab}"/>
<div>Run in Google Colab</div>
</div>
</a>
<a class="notebook-action-link" href="${notebookLink}">
<div class="notebook-action-div">
<img src="${GALLERY_LINKS.notebook}"/>
<div>Download Notebook</div>
</div>
</a>
<a class="notebook-action-link" href="${githubLink}">
<div class="notebook-action-div">
<img src="${GALLERY_LINKS.github}"/>
<div>View on GitHub</div>
</div>
</a>
`);
}
});
......@@ -8,6 +8,16 @@
READTHEDOCS_VERSIONS = {{ versions | tojson }}
</script>
{% endif %}
<script type="text/javascript">
// for gallery links
GIT_COMMIT_ID = "{{ git_commit_id }}";
PAGENAME = "{{ pagename }}";
GALLERY_LINKS = {
colab: "{{ pathto('_static/img/gallery-colab.svg', 1) }}",
notebook: "{{ pathto('_static/img/gallery-download.svg', 1) }}",
github: "{{ pathto('_static/img/gallery-github.svg', 1) }}"
}
</script>
{% endblock %}
{#- REPLACE ATTRIBUTES INSTANTLY TO DISABLE SOME HOOKS #}
......
"""
This is to keep Chinese doc update to English doc. Should be run regularly.
The files in whitelist will be kept unchanged, as they will be translated manually.
There is no sane way to check the contents though. PR review should enforce contributors to update the corresponding translation.
See https://github.com/microsoft/nni/issues/4298 for discussion.
Under docs, run
......@@ -8,38 +9,18 @@ Under docs, run
"""
import hashlib
import os
import shutil
import sys
from pathlib import Path
def walk(path):
def iterate_dir(path):
for p in Path(path).iterdir():
if p.is_dir():
yield from walk(p)
yield from iterate_dir(p)
continue
yield p
# Keeps files as discussed in
# https://github.com/microsoft/nni/issues/4298
# Not the recommended way of sphinx though: https://docs.readthedocs.io/en/stable/guides/manage-translations-sphinx.html
whitelist = [
'index.rst', # I think no one ever remembers to update this file. Might need to rethink about this.
'Overview.rst',
'installation.rst',
'Tutorial/InstallationLinux.rst',
'Tutorial/InstallationWin.rst',
'Tutorial/QuickStart.rst',
'TrialExample/Trials.rst',
'Tutorial/WebUI.rst',
'NAS/QuickStart.rst',
'Compression/Overview.rst',
'Compression/QuickStart.rst',
]
suffix_list = [
'.html',
'.md',
......@@ -47,28 +28,14 @@ suffix_list = [
'.ipynb',
]
for path in whitelist:
assert (Path('zh_CN') / path).exists(), path
content_tables = []
for path in walk(Path('en_US')):
if path.suffix == '.rst':
is_content_table = False
for line in path.open('r').readlines():
if is_content_table:
if not line.startswith(' ') and line.strip():
is_content_table = False
if 'toctree::' in line:
is_content_table = True
if is_content_table:
content_tables.append(path.relative_to('en_US').as_posix())
print('Whitelist:' ,content_tables)
whitelist += content_tables
pipeline_mode = len(sys.argv) > 1 and sys.argv[1] == 'check'
failed_files = []
# in case I need to change `_zh` to something else
# files = list(filter(lambda d: d.name.endswith('zh_CN.rst'), iterate_dir('source')))
# for file in files:
# os.rename(file, file.parent / (file.name[:-7] + file.name[-4:]))
def need_to_translate(source, target):
if not target.exists():
......@@ -93,36 +60,25 @@ def need_to_translate(source, target):
target.open('w').writelines(contents)
for path in walk(Path('en_US')):
relative_path = path.relative_to('en_US')
for path in iterate_dir(Path('source')):
relative_path = path.relative_to('source')
if relative_path.as_posix().startswith('_build'):
continue
if path.suffix in suffix_list:
target_path = (Path('zh_CN') / relative_path)
if relative_path.as_posix() in whitelist:
# whitelist files. should be translated
need_to_translate(path, target_path)
print(f'Skipped linking for {path} as it is in whitelist.')
if '_zh.' not in path.name:
target_path = path.parent / (path.stem + '_zh' + path.suffix)
if target_path.exists():
# whitelist files. should be translated
need_to_translate(path, target_path)
print(f'Skipped linking for {path} as it is in whitelist.')
else:
target_path.parent.mkdir(exist_ok=True)
link_path = path
for _ in range(len(list(Path(relative_path).parents))):
link_path = Path('..') / link_path
if not target_path.is_symlink() or os.readlink(target_path) != link_path.as_posix():
failed_files.append('(invalid link) ' + target_path.as_posix())
source_path = path.parent / (path.stem[:-3] + path.suffix)
if not source_path.exists():
# delete redundant files
failed_files.append('(redundant) ' + source_path.as_posix())
if not pipeline_mode:
target_path.unlink(missing_ok=True)
target_path.symlink_to(link_path)
# delete redundant files
for path in walk(Path('zh_CN')):
if path.suffix in suffix_list:
relative_path = path.relative_to('zh_CN')
if not (Path('en_US') / relative_path).exists():
failed_files.append('(redundant) ' + path.as_posix())
if not pipeline_mode:
print(f'Deleting {path}')
path.unlink()
print(f'Deleting {source_path}')
source_path.unlink()
if pipeline_mode and failed_files:
......
../../en_US/Assessor/BuiltinAssessor.rst
\ No newline at end of file
../../en_US/Assessor/CurvefittingAssessor.rst
\ No newline at end of file
../../en_US/Assessor/CustomizeAssessor.rst
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment