From 78dd9bf9c42b50730102ccdd90069ed63be99e80 Mon Sep 17 00:00:00 2001 From: Manoel Marques Date: Thu, 9 Apr 2020 13:06:29 -0400 Subject: [PATCH 1/2] Add doctest to travis --- Makefile | 7 +++++-- docs/conf.py | 1 + qiskit/ml/datasets/ad_hoc.py | 17 ++++++++--------- qiskit/ml/datasets/breast_cancer.py | 13 +++++-------- qiskit/ml/datasets/digits.py | 13 +++++-------- qiskit/ml/datasets/gaussian.py | 17 ++++++++--------- qiskit/ml/datasets/iris.py | 13 +++++-------- qiskit/ml/datasets/wine.py | 13 +++++-------- 8 files changed, 42 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index cdde4190a8..2c5ee1d202 100644 --- a/Makefile +++ b/Makefile @@ -35,9 +35,9 @@ endif # You can set this variable from the command line. SPHINXOPTS = -.PHONY: lint style test test_ci spell copyright html coverage coverage_erase +.PHONY: lint style test test_ci spell copyright html doctest coverage coverage_erase -all_check: spell style lint copyright html +all_check: spell style lint copyright html doctest lint: pylint -rn --ignore=gauopen qiskit/aqua qiskit/chemistry qiskit/finance qiskit/ml qiskit/optimization test tools @@ -60,6 +60,9 @@ copyright: html: make -C docs html SPHINXOPTS=$(SPHINXOPTS) + +doctest: + make -C docs doctest SPHINXOPTS=$(SPHINXOPTS) coverage: coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* -m unittest discover -s test -q diff --git a/docs/conf.py b/docs/conf.py index c8428bb62a..ab61843009 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -68,6 +68,7 @@ 'jupyter_sphinx.execute', 'sphinx_autodoc_typehints', 'reno.sphinxext', + 'sphinx.ext.doctest', ] html_static_path = ['_static'] templates_path = ['_templates'] diff --git a/qiskit/ml/datasets/ad_hoc.py b/qiskit/ml/datasets/ad_hoc.py index 24b1467366..bb65c0c2bf 100644 --- a/qiskit/ml/datasets/ad_hoc.py +++ b/qiskit/ml/datasets/ad_hoc.py @@ -19,11 +19,6 @@ import numpy as np import scipy from qiskit.aqua import aqua_globals -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def ad_hoc_data(training_size, test_size, n, gap, plot_data=False): @@ -151,8 +146,10 @@ def ad_hoc_data(training_size, test_size, n, gap, plot_data=False): training_size+test_size)] for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') plt.show() fig2 = plt.figure() @@ -222,8 +219,10 @@ def ad_hoc_data(training_size, test_size, n, gap, plot_data=False): training_size+test_size)] for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') sample_total_a = np.asarray(sample_total_a) sample_total_b = np.asarray(sample_total_b) x_1 = sample_total_a[:, 0] diff --git a/qiskit/ml/datasets/breast_cancer.py b/qiskit/ml/datasets/breast_cancer.py index 8c8a007afc..b14b7ef428 100644 --- a/qiskit/ml/datasets/breast_cancer.py +++ b/qiskit/ml/datasets/breast_cancer.py @@ -2,7 +2,7 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2019. +# (C) Copyright IBM 2018, 2020. # # 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 @@ -21,11 +21,6 @@ from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.decomposition import PCA -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def breast_cancer(training_size, test_size, n, plot_data=False): @@ -58,8 +53,10 @@ def breast_cancer(training_size, test_size, n, plot_data=False): for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 2): plt.scatter(sample_train[label_train == k, 0][:training_size], sample_train[label_train == k, 1][:training_size]) diff --git a/qiskit/ml/datasets/digits.py b/qiskit/ml/datasets/digits.py index f2091c93d6..37a3c9c76d 100644 --- a/qiskit/ml/datasets/digits.py +++ b/qiskit/ml/datasets/digits.py @@ -2,7 +2,7 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2019. +# (C) Copyright IBM 2018, 2020. # # 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 @@ -21,11 +21,6 @@ from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.decomposition import PCA -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def digits(training_size, test_size, n, plot_data=False): @@ -59,8 +54,10 @@ def digits(training_size, test_size, n, plot_data=False): for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 9): plt.scatter(sample_train[label_train == k, 0][:training_size], sample_train[label_train == k, 1][:training_size]) diff --git a/qiskit/ml/datasets/gaussian.py b/qiskit/ml/datasets/gaussian.py index a13f521950..3f6eaa12d8 100644 --- a/qiskit/ml/datasets/gaussian.py +++ b/qiskit/ml/datasets/gaussian.py @@ -18,11 +18,6 @@ import numpy as np from qiskit.aqua import aqua_globals -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def gaussian(training_size, test_size, n, plot_data=False): @@ -61,8 +56,10 @@ def gaussian(training_size, test_size, n, plot_data=False): training_size+test_size)] for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 2): plt.scatter(sample_train[label_train == k, 0][:training_size], @@ -120,8 +117,10 @@ def gaussian(training_size, test_size, n, plot_data=False): training_size+test_size)] for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 3): plt.scatter(sample_train[label_train == k, 0][:training_size], diff --git a/qiskit/ml/datasets/iris.py b/qiskit/ml/datasets/iris.py index 8648547c0d..9c7f64f7b2 100644 --- a/qiskit/ml/datasets/iris.py +++ b/qiskit/ml/datasets/iris.py @@ -2,7 +2,7 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2019. +# (C) Copyright IBM 2018, 2020. # # 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 @@ -21,11 +21,6 @@ from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.decomposition import PCA -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def iris(training_size, test_size, n, plot_data=False): @@ -58,8 +53,10 @@ def iris(training_size, test_size, n, plot_data=False): for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 3): plt.scatter(sample_train[label_train == k, 0][:training_size], sample_train[label_train == k, 1][:training_size]) diff --git a/qiskit/ml/datasets/wine.py b/qiskit/ml/datasets/wine.py index a8cd2d5c75..dec860e229 100644 --- a/qiskit/ml/datasets/wine.py +++ b/qiskit/ml/datasets/wine.py @@ -2,7 +2,7 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2019. +# (C) Copyright IBM 2018, 2020. # # 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 @@ -21,11 +21,6 @@ from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.decomposition import PCA -try: - import matplotlib.pyplot as plt - HAS_MATPLOTLIB = True -except ImportError: - HAS_MATPLOTLIB = False def wine(training_size, test_size, n, plot_data=False): @@ -58,8 +53,10 @@ def wine(training_size, test_size, n, plot_data=False): for k, key in enumerate(class_labels)} if plot_data: - if not HAS_MATPLOTLIB: - raise NameError('Matplotlib not installed. Plase install it before plotting') + try: + import matplotlib.pyplot as plt + except ImportError: + raise NameError('Matplotlib not installed. Please install it before plotting') for k in range(0, 3): plt.scatter(sample_train[label_train == k, 0][:training_size], sample_train[label_train == k, 1][:training_size]) From 6d3a55f2692987a1366b53e76c5e55379efd4e9e Mon Sep 17 00:00:00 2001 From: Manoel Marques Date: Thu, 9 Apr 2020 13:35:10 -0400 Subject: [PATCH 2/2] add doctest to travis --- .travis.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b4817fdee4..97fe4b1b7c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -124,7 +124,13 @@ jobs: - sudo apt-get -y install python3-enchant - sudo apt-get -y install hunspell-en-us - pip install pyenchant - script: make -k all_check SPHINXOPTS=-W + script: + - make spell + - make style + - make lint + - make copyright + - make html SPHINXOPTS=-W + - make doctest - name: "Test Aqua 1 Python 3.7" <<: *stage_test_aqua if: tag IS blank