diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml deleted file mode 100644 index 0b52de94..00000000 --- a/.github/workflows/coverage.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: coverage - -on: [push] - -jobs: - # This is a job for linux python3 tests and coveralls - linuxpy3: - runs-on: [ubuntu-18.04] - container: - image: 'rootproject/root:latest' - steps: - - uses: actions/checkout@v2 - - name: Test with pytest - env: - COVERALLS_REPO_TOKEN : ${{ secrets.COVERALLS }} - run: | - yum install -y python-pip ghostscript - python -m pip install pytest_pylint configparser astroid pyyml coveralls - python setup.py test - coveralls diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4307c2fb..3e7ee2ca 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,29 +6,44 @@ on: [push,pull_request] jobs: # This is a job for linux python3 tests linuxpy3: - runs-on: [ubuntu-18.04] - container: - image: 'rootproject/root:latest' + runs-on: [ubuntu-20.04] steps: - uses: actions/checkout@v2 # Install dependencies - - name: Install dependencies + - name: Install python dependencies run: | - yum install -y python-pip ghostscript - python -m pip install pytest_pylint configparser astroid pyyml papermill nbconvert - + curl https://bootstrap.pypa.io/get-pip.py > get-pip.py + python3 get-pip.py + python3 -m pip install pytest_pylint configparser astroid pyyml papermill nbconvert jupyter pytest pytest-cov + - name: Fixup ImageMagick + run: | + sudo sed -i '/.*PDF.*/ s/none/read | write/g' /etc/ImageMagick*/policy.xml + ls /etc/ImageMagick*/policy.xml + cat /etc/ImageMagick*/policy.xml + sudo apt update + sudo apt install ghostscript + echo "---GS---" + which gs + echo "--------" + - name: Install ROOT + run: | + wget -q https://root.cern/download/root_v6.24.00.Linux-ubuntu20-x86_64-gcc9.3.tar.gz + tar xf *tar.gz + source root/bin/thisroot.sh # Run pytest - name: Run pytest run: | - python -m pip install -e . - python setup.py test + source root/bin/thisroot.sh + python3 -m pip install -e . + python3 setup.py test # Save notebooks - name: Save notebooks if: ${{ always() }} run: | - python -m jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb + python3 -m jupyter kernelspec list + python3 -m jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb # Upload notebooks - name: Upload notebooks @@ -42,116 +57,121 @@ jobs: - name: Run pylint if: ${{ always() }} run: | - python -m pip install -e . - python -m pylint hepdata_lib/*.py - python -m pylint tests/*.py --rcfile=tests/pylintrc - + python3 -m pip install -e . + python3 -m pylint hepdata_lib/*.py + python3 -m pylint tests/*.py --rcfile=tests/pylintrc + - name: Upload coverage data to coveralls.io + run: | + python -m pip install coveralls==2.2 + coveralls --service=github + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This is a job for macOS python3 tests - macospy3: - runs-on: macos-latest - steps: - - uses: actions/checkout@v2 +# macospy3: +# runs-on: macos-latest +# steps: +# - uses: actions/checkout@v2 - # Install dependencies - - name: Install dependencies - run: | - brew update - brew install fontconfig gd gettext libffi glib jasper netpbm gts graphviz gsl libxml2 openblas numpy tbb xrootd || true - brew install python@3.8 openssl@1.1 || true +# # Install dependencies +# - name: Install dependencies +# run: | +# brew update +# brew install fontconfig gd gettext libffi glib jasper netpbm gts graphviz gsl libxml2 openblas numpy tbb xrootd || true +# brew install python@3.8 openssl@1.1 || true - # Run pytest - - name: Run pytest - run: | - ROOT_VERSION=6.20.04_1 - curl -O https://clange.web.cern.ch/clange/root-v${ROOT_VERSION}.tar.gz - tar xzf root-v${ROOT_VERSION}.tar.gz - mkdir -p /usr/local/Cellar/root - mv ${ROOT_VERSION} /usr/local/Cellar/root/ - cd /usr/local/Cellar/root/${ROOT_VERSION} - export PATH=${PWD}/bin:$(brew --cellar python@3.8)/$(ls $(brew --cellar python@3.8))/bin:${PATH} - export PYTHONPATH=${PWD}/lib/root:${PYTHONPATH} - export LD_LIBRARY_PATH=${PWD}/lib/root:${LD_LIBRARY_PATH} - echo ${PYTHONPATH} - cd - - python3 -m pip install pytest ipykernel papermill jupyter_client nbconvert - python3 setup.py test +# # Run pytest +# - name: Run pytest +# run: | +# ROOT_VERSION=6.20.04_1 +# curl -O https://clange.web.cern.ch/clange/root-v${ROOT_VERSION}.tar.gz +# tar xzf root-v${ROOT_VERSION}.tar.gz +# mkdir -p /usr/local/Cellar/root +# mv ${ROOT_VERSION} /usr/local/Cellar/root/ +# cd /usr/local/Cellar/root/${ROOT_VERSION} +# export PATH=${PWD}/bin:$(brew --cellar python@3.8)/$(ls $(brew --cellar python@3.8))/bin:${PATH} +# export PYTHONPATH=${PWD}/lib/root:${PYTHONPATH} +# export LD_LIBRARY_PATH=${PWD}/lib/root:${LD_LIBRARY_PATH} +# echo ${PYTHONPATH} +# cd - +# python3 -m pip install pytest ipykernel papermill jupyter_client nbconvert +# python3 setup.py test - # Save notebooks - - name: Save notebooks - if: ${{ always() }} - run: | - cd /usr/local/Cellar/root/${ROOT_VERSION} - export PATH=${PWD}/bin:$(brew --cellar python@3.8)/$(ls $(brew --cellar python@3.8))/bin:${PATH} - export PYTHONPATH=${PWD}/lib/root:${PYTHONPATH} - export LD_LIBRARY_PATH=${PWD}/lib/root:${LD_LIBRARY_PATH} - echo ${PYTHONPATH} - cd - - python3 -m jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.kernel_name=python3 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb +# # Save notebooks +# - name: Save notebooks +# if: ${{ always() }} +# run: | +# cd /usr/local/Cellar/root/${ROOT_VERSION} +# export PATH=${PWD}/bin:$(brew --cellar python@3.8)/$(ls $(brew --cellar python@3.8))/bin:${PATH} +# export PYTHONPATH=${PWD}/lib/root:${PYTHONPATH} +# export LD_LIBRARY_PATH=${PWD}/lib/root:${LD_LIBRARY_PATH} +# echo ${PYTHONPATH} +# cd - +# python3 -m jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.kernel_name=python3 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb - # Upload notebooks - - name: Upload notebooks - if: ${{ always() }} - uses: actions/upload-artifact@v2 - with: - name: notebooks macOS - path: examples/*.html +# # Upload notebooks +# - name: Upload notebooks +# if: ${{ always() }} +# uses: actions/upload-artifact@v2 +# with: +# name: notebooks macOS +# path: examples/*.html - # Run Pylint - - name: Run Pylint - if: ${{ always() }} - run: | - python3 -m pip install pytest ipykernel papermill - python3 -m pip install -e . - python3 -m pylint hepdata_lib/*.py - python3 -m pylint tests/*.py --rcfile=tests/pylintrc +# # Run Pylint +# - name: Run Pylint +# if: ${{ always() }} +# run: | +# python3 -m pip install pytest ipykernel papermill +# python3 -m pip install -e . +# python3 -m pylint hepdata_lib/*.py +# python3 -m pylint tests/*.py --rcfile=tests/pylintrc - # This is a job for linux python2.7 tests - linuxpy27: - runs-on: ubuntu-16.04 - steps: - - uses: actions/checkout@v2 +# # This is a job for linux python2.7 tests +# linuxpy27: +# runs-on: ubuntu-16.04 +# steps: +# - uses: actions/checkout@v2 - # Install dependencies - - name: Install dependencies - run: | - sudo apt-get install python2.7 - python -m pip install --upgrade pip - python -m pip install --upgrade setuptools - curl -O https://root.cern.ch/download/root_v6.20.00.Linux-ubuntu16-x86_64-gcc5.4.tar.gz - tar xzf root_v6.20.00.Linux-ubuntu16-x86_64-gcc5.4.tar.gz - python -m pip install numpy==1.16.5 - sudo sed -i '/MVG/d' /etc/ImageMagick-6/policy.xml - sudo sed -i '/PDF/{s/none/read|write/g}' /etc/ImageMagick-6/policy.xml - sudo sed -i '/PDF/ a ' /etc/ImageMagick-6/policy.xml - python -m pip install --upgrade enum34 pytest_pylint configparser astroid future ipykernel papermill nbconvert - sudo apt update - sudo apt install -y ghostscript +# # Install dependencies +# - name: Install dependencies +# run: | +# sudo apt-get install python2.7 +# python -m pip install --upgrade pip +# python -m pip install --upgrade setuptools +# curl -O https://root.cern.ch/download/root_v6.20.00.Linux-ubuntu16-x86_64-gcc5.4.tar.gz +# tar xzf root_v6.20.00.Linux-ubuntu16-x86_64-gcc5.4.tar.gz +# python -m pip install numpy==1.16.5 +# sudo sed -i '/MVG/d' /etc/ImageMagick-6/policy.xml +# sudo sed -i '/PDF/{s/none/read|write/g}' /etc/ImageMagick-6/policy.xml +# sudo sed -i '/PDF/ a ' /etc/ImageMagick-6/policy.xml +# python -m pip install --upgrade enum34 pytest_pylint configparser astroid future ipykernel papermill nbconvert +# sudo apt update +# sudo apt install -y ghostscript - # Run pytest - - name: Run pytest - run: | - source root/bin/thisroot.sh - python setup.py test +# # Run pytest +# - name: Run pytest +# run: | +# source root/bin/thisroot.sh +# python setup.py test - # Save notebooks - - name: Save notebooks - if: ${{ always() }} - run: | - source root/bin/thisroot.sh - export PATH=$PATH:~/.local/bin - source ~/.bash_profile - jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.kernel_name=python2 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb - # Upload notebooks - - name: Upload notebooks - if: ${{ always() }} - uses: actions/upload-artifact@v2 - with: - name: notebooks py27 - path: examples/*.html +# # Save notebooks +# - name: Save notebooks +# if: ${{ always() }} +# run: | +# source root/bin/thisroot.sh +# export PATH=$PATH:~/.local/bin +# source ~/.bash_profile +# jupyter nbconvert --ExecutePreprocessor.timeout=600 --ExecutePreprocessor.kernel_name=python2 --ExecutePreprocessor.allow_errors=True --to html --execute examples/*.ipynb +# # Upload notebooks +# - name: Upload notebooks +# if: ${{ always() }} +# uses: actions/upload-artifact@v2 +# with: +# name: notebooks py27 +# path: examples/*.html - # Run pylint - - name: Run Pylint - if: ${{ always() }} - run: | - python -m pylint hepdata_lib/*.py - python -m pylint tests/*.py --rcfile=tests/pylintrc +# # Run pylint +# - name: Run Pylint +# if: ${{ always() }} +# run: | +# python -m pylint hepdata_lib/*.py +# python -m pylint tests/*.py --rcfile=tests/pylintrc diff --git a/examples/example_inputs/mlfit_lm_1000.root b/examples/example_inputs/mlfit_lm_1000.root index 02432929..7203e02e 100644 Binary files a/examples/example_inputs/mlfit_lm_1000.root and b/examples/example_inputs/mlfit_lm_1000.root differ diff --git a/hepdata_lib/__init__.py b/hepdata_lib/__init__.py index 0b8c9d9a..a670afe1 100644 --- a/hepdata_lib/__init__.py +++ b/hepdata_lib/__init__.py @@ -12,6 +12,7 @@ import warnings from collections import defaultdict import yaml +from future.utils import raise_from # try to use LibYAML bindings if possible try: @@ -77,11 +78,11 @@ def values(self, value_list): if self.is_binned: # Check that the input is well-formed try: - assert all([len(x) == 2 for x in value_list]) - except (AssertionError, TypeError, ValueError): - raise ValueError("For binned Variables, values should be tuples of length two: \ + assert all((len(x) == 2 for x in value_list)) + except (AssertionError, TypeError, ValueError) as err: + msg = "For binned Variables, values should be tuples of length two: \ (lower bin edge, upper bin edge)." - ) + raise_from(ValueError(msg), err) # All good self._values = [(float(x[0]), float(x[1])) for x in value_list] @@ -89,8 +90,8 @@ def values(self, value_list): # Check that the input is well-formed try: parsed_values = [x if isinstance(x, str) else float(x) for x in value_list] - except (TypeError, ValueError): - raise ValueError("Malformed input for unbinned variable: ", value_list) + except (TypeError, ValueError) as err: + raise_from(ValueError("Malformed input for unbinned variable: ", value_list),err) self._values = parsed_values def scale_values(self, factor): diff --git a/hepdata_lib/c_file_reader.py b/hepdata_lib/c_file_reader.py index 6c25e9cc..da6b5c62 100644 --- a/hepdata_lib/c_file_reader.py +++ b/hepdata_lib/c_file_reader.py @@ -1,7 +1,7 @@ """.C file reader""" - import io from array import array +from future.utils import raise_from import six from ROOT import TGraph, TGraphErrors import hepdata_lib.root_utils as ru @@ -171,8 +171,8 @@ def create_tgrapherrors(self, x_value, y_value, dx_value, dy_value): dy_values.append(dy_value[value]) try: t_object = TGraphErrors(length, x_values, y_values, dx_values, dy_values) - except TypeError: - raise TypeError("Invalid value in TGraphErrors constructor!") + except TypeError as err: + raise_from(TypeError("Invalid value in TGraphErrors constructor!"), err) graph = ru.get_graph_points(t_object) return graph @@ -197,8 +197,8 @@ def create_tgraph(self, x_value, y_value): y_values.append(y_value[value]) try: t_object = TGraph(length, x_values, y_values) - except TypeError: - raise TypeError("Invalid value in TGraph constructor!") + except TypeError as err: + raise_from(TypeError("Invalid value in TGraph constructor!"), err) graph = ru.get_graph_points(t_object) return graph @@ -287,17 +287,17 @@ def find_graphs(self): if start == 1: try: tgraph_names.append(line.split('"', 1)[1].split('"')[0]) - except IndexError: + except IndexError as err: tgraph_names = 'null' - raise IndexError("index out of range") + raise_from(IndexError("index out of range"), err) start = 0 counter = 0 if start == 2: try: tgrapherror_names.append(line.split('"', 1)[1].split('"')[0]) - except IndexError: + except IndexError as err: tgrapherror_names = 'null' - raise IndexError("index out of range") + raise_from(IndexError("index out of range"), err) start = 0 counter = 0 @@ -401,6 +401,6 @@ def read_graph(self, graphname): values.append(int(i)) except ValueError: values.append(float(i)) - except ValueError: - raise ValueError("Value is not a number in variable:", graphname) + except ValueError as err: + raise_from(ValueError("Value is not a number in variable:", graphname), err) return values diff --git a/hepdata_lib/root_utils.py b/hepdata_lib/root_utils.py index 05cbb6b1..100bc286 100644 --- a/hepdata_lib/root_utils.py +++ b/hepdata_lib/root_utils.py @@ -1,9 +1,9 @@ """hepdata_lib utilities to interact with ROOT data formats.""" - from collections import defaultdict +import ctypes +from future.utils import raise_from import numpy as np import ROOT as r -import ctypes from hepdata_lib.helpers import check_file_existence class RootFileReader(object): @@ -88,12 +88,12 @@ def retrieve_object(self, path_to_object): print("Name: '{0}', Type: '{1}'.".format( entry.GetName(), type(entry))) assert False - return entry - except AssertionError: - raise IOError( - "Cannot find any object in file {0} with path {1}".format( - self.tfile, path_to_object)) + except AssertionError as err: + msg="Cannot find any object in file {0} with path {1}".format( + self.tfile, path_to_object) + raise_from(IOError(msg), err) + return None def read_graph(self, path_to_graph): """Extract lists of X and Y values from a TGraph. @@ -197,9 +197,9 @@ def read_tree(self, path_to_tree, branch_name): for event in tree: try: values.append(getattr(event, branch_name)) - except AttributeError: + except AttributeError as err: msg = "The TTree does not have a branch with name '{0}'.".format(branch_name) - raise RuntimeError(msg) + raise_from(RuntimeError(msg),err) return values def read_limit_tree(self, diff --git a/tests/test_rootfilereader.py b/tests/test_rootfilereader.py index 732108f1..0b1de73e 100644 --- a/tests/test_rootfilereader.py +++ b/tests/test_rootfilereader.py @@ -629,8 +629,8 @@ def test_read_tree(self): except RuntimeError: self.fail("RootFileReader.read_tree raised an unexpected RuntimeError!") self.assertIsInstance(data_readback, list) - self.assertTrue(all([float_compare(values[0], values[1]) - for values in zip(data, data_readback)])) + self.assertTrue(all((float_compare(values[0], values[1]) + for values in zip(data, data_readback)))) # Try reading a nonexistant branch from an existing tree with self.assertRaises(RuntimeError): @@ -661,9 +661,8 @@ def test_retrieve_object_canvas(self): ROOT.gROOT.SetBatch(ROOT.kTRUE) # Create test histogram, plot on canvas, save to file - histogram = ROOT.TH1D("testhist", "testhist", 10, 0, 1) - tfile = make_tmp_root_file(testcase=self) + histogram = ROOT.TH1D("testhist", "testhist", 10, 0, 1) path_to_file = tfile.GetName() canvas = ROOT.TCanvas() diff --git a/tests/test_uncertainty.py b/tests/test_uncertainty.py index 50577964..2fa77e4c 100644 --- a/tests/test_uncertainty.py +++ b/tests/test_uncertainty.py @@ -67,5 +67,5 @@ def test_set_values_from_intervals(self): testunc.set_values_from_intervals(intervals, nominal=values) # Check that both agree - self.assertTrue(all([test_utilities.tuple_compare(tup1, tup2) \ - for tup1, tup2 in zip(testunc.values, refunc.values)])) + self.assertTrue(all((test_utilities.tuple_compare(tup1, tup2) \ + for tup1, tup2 in zip(testunc.values, refunc.values)))) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 375e34e1..45b5d09f 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -2,10 +2,13 @@ # -*- coding:utf-8 -*- """Utilities for tests.""" -import string -import random import os +import random +import string + import ROOT +from future.utils import raise_from + def float_compare(x_val, y_val, precision=1e-6): '''Helper function to check that two numbers are equal within float precision.''' @@ -84,9 +87,9 @@ def make_tmp_root_file(path_to_file='tmp_{RANDID}.root', mode="RECREATE", if "{RANDID}" in path_to_file: try: path_to_file = path_to_file.format(RANDID=get_random_id()) - except IndexError: - raise IOError("String substitution failed. Your input path should not \ - have any braces except possibly for the {RANDID} token!") + except IndexError as err: + raise_from(IOError("String substitution failed. Your input path should not \ + have any braces except possibly for the {RANDID} token!"), err) rfile = ROOT.TFile(path_to_file, mode)