Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #423 from chrisburr/fix-422
Browse files Browse the repository at this point in the history
Fix reading with HTTP and improve tests
  • Loading branch information
jpivarski authored Dec 18, 2019
2 parents 1301a4f + 06ced16 commit d31026d
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 98 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ install:
# FIXME: Mamba causes pip install numpy to be extremely slow
- if [[ "${PYVER}" = pypy* ]]; then
conda create --quiet --yes -n testenv ${PYVER};
elif [ "${PYVER}" = "2.7" ] || [ "${PYVER}" = "3.6" ] || [ "${PYVER}" = "3.7" ]; then
elif [ "${PYVER}" = "2.7" ] || [ "${PYVER}" = "3.6" ] || [ "${PYVER}" = "3.7" || [ "${PYVER}" = "3.8" ]; then
conda create --quiet --yes -n testenv python=${PYVER} pip;
else
conda create --quiet --yes -n testenv python=${PYVER};
fi
- source activate testenv
- if [ "${PYVER}" = "2.7" ] || [ "${PYVER}" = "3.6" ] || [ "${PYVER}" = "3.7" ]; then
- if [ "${PYVER}" = "2.7" ] || [ "${PYVER}" = "3.6" ] || [ "${PYVER}" = "3.7" || [ "${PYVER}" = "3.8" ]; then
conda install --quiet --yes python=${PYVER} pip root;
source activate testenv;
fi
Expand All @@ -69,6 +69,7 @@ install:
conda install -c anaconda python=${PYVER} pyopenssl;
fi
- wget -O tests/samples/Event.root http://scikit-hep.org/uproot/examples/Event.root
- pip install git+https://github.com/chrisburr/pytest-error-for-skips.git@patch-1

addons:
apt:
Expand All @@ -77,7 +78,11 @@ addons:
- libatlas-base-dev

script:
pytest -v tests
if [ "${PYVER}" = "2.7" ] || [ "${PYVER}" = "3.6" ] || [ "${PYVER}" = "3.7" || [ "${PYVER}" = "3.8" ]; then
pytest --error-for-skips -v tests;
else
pytest -v tests;
fi

notifications:
slack: scikit-hep:b6cgBXwccPoaCNLn5VKFJFVy
Expand Down
6 changes: 1 addition & 5 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

from collections import namedtuple

import uproot

class Test(unittest.TestCase):
class Test(object):
def test_flat_array(self):
branch = uproot.open("tests/samples/sample-6.10.05-uncompressed.root")["sample"]["i8"]
expectation = [-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Expand Down
4 changes: 1 addition & 3 deletions tests/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

try:
import lzma
except ImportError:
Expand All @@ -13,7 +11,7 @@

import uproot

class Test(unittest.TestCase):
class Test(object):
def test_compression_identity(self):
assert uproot.open("tests/samples/Zmumu-zlib.root").compression.algoname == "zlib"
assert uproot.open("tests/samples/Zmumu-zlib.root").compression.level == 4
Expand Down
4 changes: 1 addition & 3 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

import pytest
import mock
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -41,7 +39,7 @@ def raise_for_status(self):
return MockResponse(401)

@mock.patch("requests.get", mock_get_local_instead_of_http)
class Test(unittest.TestCase):
class Test(object):
def test_no_auth_needed_no_auth(self):
f = uproot.open(URL)
assert type(f) == uproot.rootio.ROOTDirectory
Expand Down
37 changes: 12 additions & 25 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

from collections import namedtuple

import pytest
import numpy

Expand All @@ -16,7 +12,7 @@
import uproot_methods.classes.TLorentzVector


class Test(unittest.TestCase):
class Test(object):
def test_issue21(self):
t = uproot.open("tests/samples/issue21.root")["nllscan"]

Expand Down Expand Up @@ -271,27 +267,18 @@ def test_issue213(self):
]

def test_issue232(self):
try:
import pandas
except ImportError:
pass
else:
t = uproot.open("tests/samples/issue232.root")["fTreeV0"]
t.pandas.df(
["V0Hyper.fNsigmaHe3Pos", "V0Hyper.fDcaPos2PrimaryVertex"],
flatten=True)

@pytest.mark.skip(reason="This one takes way too long (eospublic?).")
pytest.importorskip("pandas")
t = uproot.open("tests/samples/issue232.root")["fTreeV0"]
t.pandas.df(
["V0Hyper.fNsigmaHe3Pos", "V0Hyper.fDcaPos2PrimaryVertex"],
flatten=True)

def test_issue240(self):
try:
import pyxrootd
except ImportError:
pytest.skip("unable to import pyxrootd")
else:
t = uproot.open(
"root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/Run2012B_DoubleMuParked.root"
)["Events"]
assert (abs(t.array("nMuon")) < 50).all()
pytest.importorskip("pyxrootd")
t = uproot.open(
"root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/Run2012B_DoubleMuParked.root"
)["Events"]
assert (abs(t.array("nMuon", entrystop=100000)) < 50).all()

def test_issue243(self):
t = uproot.open("tests/samples/issue243.root")["triggerList"]
Expand Down
46 changes: 23 additions & 23 deletions tests/test_jagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest
import pytest

import uproot
try:
import pandas
except ImportError:
pandas = None

class Test(unittest.TestCase):
if pandas is not None:
sample = uproot.open("tests/samples/sample-6.10.05-uncompressed.root")["sample"]

class Test(object):
@property
def sample(self):
pytest.importorskip("pandas")
try:
self._sample
except AttributeError:
self._sample = uproot.open("tests/samples/sample-6.10.05-uncompressed.root")["sample"]
return self._sample

def test_flatten_False(self):
if pandas is not None:
df = self.sample.pandas.df(flatten=False)
assert len(df.keys()) == 57
assert "Af8" in df
assert len(df.at[0, "Af8"]) == 0
assert len(df.at[1, "Af8"]) == 1
assert len(df.at[2, "Af8"]) == 2
df = self.sample.pandas.df(flatten=False)
assert len(df.keys()) == 57
assert "Af8" in df
assert len(df.at[0, "Af8"]) == 0
assert len(df.at[1, "Af8"]) == 1
assert len(df.at[2, "Af8"]) == 2

def test_flatten_None(self):
if pandas is not None:
df = self.sample.pandas.df(flatten=None)
assert len(df.keys()) == 46
assert "Af8" not in df
df = self.sample.pandas.df(flatten=None)
assert len(df.keys()) == 46
assert "Af8" not in df

def test_flatten_True(self):
if pandas is not None:
df = self.sample.pandas.df(flatten=True)
assert len(df.keys()) == 57
assert "Af8" in df
df = self.sample.pandas.df(flatten=True)
assert len(df.keys()) == 57
assert "Af8" in df
6 changes: 1 addition & 5 deletions tests/test_stlvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

from collections import namedtuple

import uproot

class Test(unittest.TestCase):
class Test(object):
def runTest(self):
pass

Expand Down
54 changes: 27 additions & 27 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import os
import unittest

from collections import namedtuple

import numpy
Expand All @@ -18,7 +16,7 @@ def basest(array):
array = array.base
return array

class Test(unittest.TestCase):
class Test(object):
###################################################### double32

def test_double32(self):
Expand Down Expand Up @@ -594,9 +592,12 @@ def test_tree_lazy_cached(self):
for i in range(len(lazy), 0, -1):
assert lazy[i - 1 : i + 3].tolist() == strict[i - 1 : i + 3].tolist()

def test_hist_in_tree(self):
path = os.path.join("tests", "samples", "Event.root")
if os.path.exists(path):
@pytest.mark.parametrize("use_http", [False, True])
def test_hist_in_tree(self, use_http):
if use_http:
path = os.path.join("tests", "samples", "Event.root")
if not os.path.exists(path):
raise pytest.skip()
tree = uproot.open(path)["T"]
else:
tree = uproot.open("http://scikit-hep.org/uproot/examples/Event.root")["T"]
Expand All @@ -610,7 +611,8 @@ def test_hist_in_tree(self):

assert tree.array("fH")[20].values.tolist() == check

def test_branch_auto_interpretation(self):
@pytest.mark.parametrize("use_http", [False, True])
def test_branch_auto_interpretation(self, use_http):
# The aim is to reduce this list in a controlled manner
known_branches_without_interp = [
b'event',
Expand All @@ -622,8 +624,10 @@ def test_branch_auto_interpretation(self):
b'fTriggerBits',
b'fTriggerBits.TObject'
]
path = os.path.join("tests", "samples", "Event.root")
if os.path.exists(path):
if use_http:
path = os.path.join("tests", "samples", "Event.root")
if not os.path.exists(path):
raise pytest.skip()
tree = uproot.open(path)["T"]
else:
tree = uproot.open("http://scikit-hep.org/uproot/examples/Event.root")["T"]
Expand All @@ -650,24 +654,20 @@ def test_leaflist(self):
assert a["y"].tolist() == [1, 2, 3, 4, 5]
assert a["z"].tolist() == [ord("a"), ord("b"), ord("c"), ord("d"), ord("e")]

try:
import pandas
except ImportError:
pass
else:
assert tree.pandas.df()["leaflist.x"].tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]

tree = uproot.open("tests/samples/HZZ-objects.root")["events"]
tree.pandas.df("muonp4")
tree.pandas.df("muonp4", flatten=False)
df = tree.pandas.df("eventweight", entrystart=100, entrystop=200)
index = df.index.tolist()
assert min(index) == 100
assert max(index) == 199
df = tree.pandas.df("muonp4", entrystart=100, entrystop=200)
index = df.index.get_level_values("entry").tolist()
assert min(index) == 100
assert max(index) == 199
pytest.importorskip("pandas")
assert tree.pandas.df()["leaflist.x"].tolist() == [1.1, 2.2, 3.3, 4.0, 5.5]

tree = uproot.open("tests/samples/HZZ-objects.root")["events"]
tree.pandas.df("muonp4")
tree.pandas.df("muonp4", flatten=False)
df = tree.pandas.df("eventweight", entrystart=100, entrystop=200)
index = df.index.tolist()
assert min(index) == 100
assert max(index) == 199
df = tree.pandas.df("muonp4", entrystart=100, entrystop=200)
index = df.index.get_level_values("entry").tolist()
assert min(index) == 100
assert max(index) == 199

def test_mempartitions(self):
t = uproot.open("tests/samples/sample-5.23.02-zlib.root")["sample"]
Expand Down
4 changes: 1 addition & 3 deletions tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# BSD 3-Clause License; see https://github.com/scikit-hep/uproot/blob/master/LICENSE

import unittest

try:
import lzma
except ImportError:
Expand All @@ -12,7 +10,7 @@

import uproot

class Test(unittest.TestCase):
class Test(object):
sample = {
b"n": [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4],

Expand Down
2 changes: 1 addition & 1 deletion uproot/source/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _read(self, chunkindex):
while True:
response = requests.get(
self.path,
headers={"Range": "bytes={0}-{1}".format(chunkindex * self._chunkbytes, (chunkindex + 1) * self._chunkbytes)},
headers={"Range": "bytes={0}-{1}".format(chunkindex * self._chunkbytes, (chunkindex + 1) * self._chunkbytes - 1)},
auth=self.auth,
)
if response.status_code == 504: # timeout, try it again
Expand Down

0 comments on commit d31026d

Please sign in to comment.