Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge 6db3a16 into 10d7d17
Browse files Browse the repository at this point in the history
  • Loading branch information
ndawe committed Apr 26, 2015
2 parents 10d7d17 + 6db3a16 commit fc9c434
Show file tree
Hide file tree
Showing 23 changed files with 11,071 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1 +1,2 @@
/root_numpy/src/_librootnumpy.cpp binary
/root_numpy/tmva/src/_libtmvanumpy.cpp binary
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -5,9 +5,12 @@ python:
- "3.4"
env:
- ROOT=v5-34-18
- ROOT=v5-34-18 NOTMVA=1
- ROOT=master
install: source ci/install.sh
script: bash ci/test.sh
after_success:
- time coveralls
cache: apt
notifications:
email: false
22 changes: 10 additions & 12 deletions Makefile
Expand Up @@ -4,10 +4,11 @@ PYTHON := $(shell which python)
CYTHON := $(shell which cython)
NOSETESTS := $(shell which nosetests)

CYTHON_PYX := root_numpy/src/_librootnumpy.pyx
CYTHON_CPP := root_numpy/src/_librootnumpy.cpp
CYTHON_SRC := $(filter-out $(CYTHON_PYX),$(filter-out $(CYTHON_CPP),$(wildcard root_numpy/src/*)))
CYTHON_PYX_SRC := $(filter-out $(CYTHON_PYX),$(wildcard root_numpy/src/*.pyx))
CYTHON_CORE_PYX := root_numpy/src/_librootnumpy.pyx
CYTHON_TMVA_PYX := root_numpy/tmva/src/_libtmvanumpy.pyx
CYTHON_CORE_CPP := $(CYTHON_CORE_PYX:.pyx=.cpp)
CYTHON_TMVA_CPP := $(CYTHON_TMVA_PYX:.pyx=.cpp)
CYTHON_PYX_SRC := $(filter-out $(CYTHON_CORE_PYX),$(wildcard root_numpy/src/*.pyx))

INTERACTIVE := $(shell ([ -t 0 ] && echo 1) || echo 0)

Expand All @@ -18,7 +19,7 @@ else
OPEN := xdg-open
endif

all: $(CYTHON_CPP) clean inplace test
all: clean cython inplace test

clean-pyc:
@find . -name "*.pyc" -exec rm {} \;
Expand All @@ -34,15 +35,12 @@ clean-html:

clean: clean-build clean-pyc clean-so

$(CYTHON_PYX): $(CYTHON_SRC)

$(CYTHON_CPP): $(CYTHON_PYX)
.SECONDEXPANSION:
%.cpp: %.pyx $$(filter-out $$@,$$(wildcard $$(@D)/*))
@echo "compiling $< ..."
@$(CYTHON) --cplus --fast-fail --line-directives $<
$(CYTHON) --cplus --fast-fail --line-directives $<

cython:
@echo "compiling $(CYTHON_PYX) ..."
$(CYTHON) --cplus --fast-fail --line-directives $(CYTHON_PYX)
cython: $(CYTHON_CORE_CPP) $(CYTHON_TMVA_CPP)

show-cython: clean-html
@tmp=`mktemp -d`; \
Expand Down
13 changes: 10 additions & 3 deletions ci/install.sh
Expand Up @@ -6,16 +6,23 @@

set -e

# Install gcc 4.8
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt-get -qq update
sudo apt-get -qq install g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90

sudo apt-get -qq install python-nose python-pip
pip install coverage coveralls

# Install the ROOT binary
time wget --no-check-certificate https://copy.com/rtIyUdxgjt7h/ci/root_builds/root${ROOT}_python${TRAVIS_PYTHON_VERSION}_gcc4.8_x86_64.tar.gz
time tar zxf root${ROOT}_python${TRAVIS_PYTHON_VERSION}_gcc4.8_x86_64.tar.gz
mv root${ROOT}_python${TRAVIS_PYTHON_VERSION}_gcc4.8_x86_64 root
build=root${ROOT}_python${TRAVIS_PYTHON_VERSION}_gcc4.8_x86_64
if [ ! -z ${NOTMVA+x} ]; then
# Use a build without TMVA
build+=_notmva
fi
time wget --no-check-certificate https://copy.com/rtIyUdxgjt7h/ci/root_builds/${build}.tar.gz
time tar zxf ${build}.tar.gz
mv ${build} root
source root/bin/thisroot.sh
6 changes: 5 additions & 1 deletion ci/test.sh
Expand Up @@ -20,5 +20,9 @@ export PYTHONPATH=/home/travis/.local/lib/python${TRAVIS_PYTHON_VERSION}/site-pa
# Install into the user site-packages directory and run tests on that
time make install-user
time make test-installed

# Run tests in the local directory with coverage
time make test-coverage </dev/null
if [ -z ${NOTMVA+x} ]; then
# TMVA is included in this build, so run the coverage
time make test-coverage </dev/null
fi
47 changes: 47 additions & 0 deletions docs/examples/tmva.py
@@ -0,0 +1,47 @@
from array import array
import numpy as np
from numpy.random import RandomState
from root_numpy.tmva import factory_add_events, reader_evaluate
from ROOT import TMVA, TFile, TCut

RNG = RandomState(42)

# construct an example dataset
n_vars = 5
n_events = 1000
signal = RNG.multivariate_normal(
np.ones(n_vars), np.diag(np.ones(n_vars)), n_events)
background = RNG.multivariate_normal(
np.ones(n_vars) * -1, np.diag(np.ones(n_vars)), n_events)
X = np.concatenate([signal, background])
y = np.ones(signal.shape[0] + background.shape[0])
w = RNG.randint(1, 10, n_events * 2)
y[signal.shape[0]:] *= -1
permute = RNG.permutation(y.shape[0])
X = X[permute]
y = y[permute]

# split into training and test datasets
X_train, y_train, w_train = X[:n_events], y[:n_events], w[:n_events]
X_test, y_test, w_test = X[n_events:], y[n_events:], w[n_events:]

output = TFile('tmva_output.root', 'recreate')
factory = TMVA.Factory('classifier', output, 'AnalysisType=Classification')
for n in range(n_vars):
factory.AddVariable('f{0}'.format(n), 'F')

# call root_numpy's utility functions to add events from the arrays
factory_add_events(factory, X_train, y_train, weights=w_train)
factory_add_events(factory, X_test, y_test, weights=w_test, test=True)

# train a BDT
factory.PrepareTrainingAndTestTree(TCut('1'), 'NormMode=EqualNumEvents')
factory.BookMethod('BDT', 'BDT', 'nCuts=-1:NTrees=10:MaxDepth=3')
factory.TrainAllMethods()

# classify the test dataset with the BDT
reader = TMVA.Reader()
for n in range(n_vars):
reader.AddVariable('f{0}'.format(n), array('f', [0.]))
reader.BookMVA('BDT', 'weights/classifier_BDT.weights.xml')
scores = reader_evaluate(reader, 'BDT', X_test)
22 changes: 16 additions & 6 deletions docs/reference/index.rst
Expand Up @@ -7,11 +7,14 @@ root_numpy Reference
:Release: |version|
:Date: |today|

.. module:: root_numpy

This reference manual details the functions included in root_numpy, describing
what they are and what they do.

root_numpy
----------

.. module:: root_numpy

.. autosummary::
:toctree: generated

Expand Down Expand Up @@ -39,6 +42,17 @@ what they are and what they do.
dup_idx
blockwise_inner_join

root_numpy.tmva
---------------

.. module:: root_numpy.tmva

.. autosummary::
:toctree: generated

factory_add_events
reader_evaluate

.. _conversion_table:

Type Conversion Table
Expand Down Expand Up @@ -70,7 +84,3 @@ vector<vector<t> > np.object
Variable length arrays (such as ``particletype[nparticle]``) and vectors
(such as ``vector<int>``) are converted to NumPy arrays of the corresponding
types. Fixed length arrays are converted to fixed length NumPy array fields.

.. note::
Tab completion for numpy.recarray column names (yourdata.<TAB>)
is also available with this `numpy extension <https://github.com/piti118/inumpy>`_.
63 changes: 63 additions & 0 deletions root_numpy/_hist.py
Expand Up @@ -154,6 +154,10 @@ def hist2array(hist, include_overflow=False, copy=True):
TypeError
If hist is not a ROOT histogram.
See Also
--------
array2hist
"""
import ROOT
if isinstance(hist, ROOT.TH3):
Expand Down Expand Up @@ -234,6 +238,65 @@ def array2hist(array, hist):
array data type matches the histogram data type and that overflow bins are
included.
See Also
--------
hist2array
Examples
--------
>>> from root_numpy import array2hist, hist2array
>>> import numpy as np
>>> from rootpy.plotting import Hist2D
>>> hist = Hist2D(5, 0, 1, 3, 0, 1, type='F')
>>> array = np.random.randint(0, 10, size=(7, 5))
>>> array
array([[6, 7, 8, 3, 4],
[8, 9, 7, 6, 2],
[2, 3, 4, 5, 2],
[7, 6, 5, 7, 3],
[2, 0, 5, 6, 8],
[0, 0, 6, 5, 2],
[2, 2, 1, 5, 4]])
>>> array2hist(array, hist)
>>> # dtype matches histogram type (D, F, I, S, C)
>>> hist2array(hist)
array([[ 9., 7., 6.],
[ 3., 4., 5.],
[ 6., 5., 7.],
[ 0., 5., 6.],
[ 0., 6., 5.]], dtype=float32)
>>> # overflow is excluded by default
>>> hist2array(hist, include_overflow=True)
array([[ 6., 7., 8., 3., 4.],
[ 8., 9., 7., 6., 2.],
[ 2., 3., 4., 5., 2.],
[ 7., 6., 5., 7., 3.],
[ 2., 0., 5., 6., 8.],
[ 0., 0., 6., 5., 2.],
[ 2., 2., 1., 5., 4.]], dtype=float32)
>>> array2 = hist2array(hist, include_overflow=True, copy=False)
>>> hist[2, 2] = -10
>>> # array2 views the same memory as hist because copy=False
>>> array2
array([[ 6., 7., 8., 3., 4.],
[ 8., 9., 7., 6., 2.],
[ 2., 3., -10., 5., 2.],
[ 7., 6., 5., 7., 3.],
[ 2., 0., 5., 6., 8.],
[ 0., 0., 6., 5., 2.],
[ 2., 2., 1., 5., 4.]], dtype=float32)
>>> # x, y, z axes correspond to axes 0, 1, 2 in numpy
>>> hist[2, 3] = -10
>>> array2
array([[ 6., 7., 8., 3., 4.],
[ 8., 9., 7., 6., 2.],
[ 2., 3., -10., -10., 2.],
[ 7., 6., 5., 7., 3.],
[ 2., 0., 5., 6., 8.],
[ 0., 0., 6., 5., 2.],
[ 2., 2., 1., 5., 4.]], dtype=float32)
"""
import ROOT
if isinstance(hist, ROOT.TH3):
Expand Down
2 changes: 0 additions & 2 deletions root_numpy/src/ROOT.pxi
@@ -1,5 +1,3 @@
from libcpp cimport bool
from libcpp.string cimport string, const_char

cdef extern from "TObject.h":
cdef cppclass TObject:
Expand Down

0 comments on commit fc9c434

Please sign in to comment.