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

Commit

Permalink
Merge pull request #200 from ndawe/master
Browse files Browse the repository at this point in the history
[MRG] TMVA: add aux parameter for MethodCuts
  • Loading branch information
ndawe committed May 8, 2015
2 parents f05d4c2 + f8a130e commit 8b5d3b8
Show file tree
Hide file tree
Showing 7 changed files with 746 additions and 545 deletions.
17 changes: 8 additions & 9 deletions examples/tmva/plot_multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@
# The following line is necessary if events have been added individually:
factory.PrepareTrainingAndTestTree(TCut('1'), 'NormMode=EqualNumEvents')

# Train a BDT
factory.BookMethod('BDT', 'BDTG',
'nCuts=20:NTrees=20:MaxDepth=4:'
'BoostType=Grad:Shrinkage=0.10')
# Train an MLP
factory.BookMethod('MLP', 'MLP',
'NeuronType=tanh:NCycles=200:HiddenLayers=N+2,2:'
'TestRate=5:EstimatorType=MSE')
factory.TrainAllMethods()

# Classify the test dataset with the BDT
reader = TMVA.Reader()
for n in range(2):
reader.AddVariable('f{0}'.format(n), array('f', [0.]))
reader.BookMVA('BDT', 'weights/classifier_BDTG.weights.xml')
class_proba = evaluate_reader(reader, 'BDT', X_test)
reader.BookMVA('MLP', 'weights/classifier_MLP.weights.xml')
class_proba = evaluate_reader(reader, 'MLP', X_test)

# Plot the decision boundaries
plot_colors = "rgb"
Expand All @@ -72,11 +72,10 @@
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))

Z = evaluate_reader(reader, 'BDT', np.c_[xx.ravel(), yy.ravel()])
Z = evaluate_reader(reader, 'MLP', np.c_[xx.ravel(), yy.ravel()])
Z = np.argmax(Z, axis=1) - 1
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=cmap, vmin=Z.min(), vmax=Z.max(),
levels=np.linspace(Z.min(), Z.max(), 50))
plt.contourf(xx, yy, Z, cmap=cmap, alpha=0.5)
plt.axis("tight")

# Plot the training points
Expand Down
33 changes: 21 additions & 12 deletions root_numpy/tmva/_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
]


def evaluate_reader(reader, name, events):
def evaluate_reader(reader, name, events, aux=0.):
"""Evaluate a TMVA::Reader over a NumPy array.
Parameters
----------
reader : TMVA::Reader
A TMVA::Factory instance with variables booked in
exactly the same order as the columns in ``events``.
A TMVA::Factory instance with variables booked in exactly the same
order as the columns in ``events``.
name : string
The method name.
events : numpy array of shape [n_events, n_variables]
A two-dimensional NumPy array containing the rows of events
and columns of variables.
A two-dimensional NumPy array containing the rows of events and columns
of variables. The order of the columns must match the order in which
you called ``AddVariable()`` for each variable.
aux : float, optional (default=0.)
Auxiliary value used by MethodCuts to set the desired signal
efficiency.
Returns
-------
Expand All @@ -44,10 +48,11 @@ def evaluate_reader(reader, name, events):
raise ValueError(
"events must be a two-dimensional array "
"with one event per row")
return _libtmvanumpy.evaluate_reader(ROOT.AsCObject(reader), name, events)
return _libtmvanumpy.evaluate_reader(
ROOT.AsCObject(reader), name, events, aux)


def evaluate_method(method, events):
def evaluate_method(method, events, aux=0.):
"""Evaluate a TMVA::MethodBase over a NumPy array.
.. warning:: TMVA::Reader has known problems with thread safety in versions
Expand All @@ -59,11 +64,15 @@ def evaluate_method(method, events):
Parameters
----------
method : TMVA::MethodBase
A TMVA::MethodBase instance with variables booked in
exactly the same order as the columns in ``events``.
A TMVA::MethodBase instance with variables booked in exactly the same
order as the columns in ``events``.
events : numpy array of shape [n_events, n_variables]
A two-dimensional NumPy array containing the rows of events
and columns of variables.
A two-dimensional NumPy array containing the rows of events and columns
of variables. The order of the columns must match the order in which
you called ``AddVariable()`` for each variable.
aux : float, optional (default=0.)
Auxiliary value used by MethodCuts to set the desired signal
efficiency.
Returns
-------
Expand All @@ -85,4 +94,4 @@ def evaluate_method(method, events):
raise ValueError(
"events must be a two-dimensional array "
"with one event per row")
return _libtmvanumpy.evaluate_method(ROOT.AsCObject(method), events)
return _libtmvanumpy.evaluate_method(ROOT.AsCObject(method), events, aux)
44 changes: 29 additions & 15 deletions root_numpy/tmva/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ def add_classification_events(factory, events, labels, signal_label=None,
Parameters
----------
factory : TMVA::Factory
A TMVA::Factory instance with variables already booked in
exactly the same order as the columns in ``events``.
A TMVA::Factory instance with variables already booked in exactly the
same order as the columns in ``events``.
events : numpy array of shape [n_events, n_variables]
A two-dimensional NumPy array containing the rows of events
and columns of variables.
A two-dimensional NumPy array containing the rows of events and columns
of variables. The order of the columns must match the order in which
you called ``AddVariable()`` for each variable.
labels : numpy array of shape [n_events]
The class labels (signal or background) corresponding to each event
in ``events``.
The class labels (signal or background) corresponding to each event in
``events``.
signal_label : float or int, optional (default=None)
The value in ``labels`` for signal events, if ``labels`` contains only
two classes. If None, the highest value in ``labels`` is used.
weights : numpy array of shape [n_events], optional
Event weights.
test : bool, optional (default=False)
If True, then the events will be added as test events, otherwise
they are added as training events by default.
If True, then the events will be added as test events, otherwise they
are added as training events by default.
Notes
-----
A TMVA::Factory requires you to add both training and test events even if
you don't intend to call ``TestAllMethods()``.
"""
if not isinstance(factory, TMVA.Factory):
Expand Down Expand Up @@ -78,18 +84,26 @@ def add_regression_events(factory, events, targets, weights=None, test=False):
Parameters
----------
factory : TMVA::Factory
A TMVA::Factory instance with variables already booked in
exactly the same order as the columns in ``events``.
A TMVA::Factory instance with variables already booked in exactly the
same order as the columns in ``events``.
events : numpy array of shape [n_events, n_variables]
A two-dimensional NumPy array containing the rows of events
and columns of variables.
A two-dimensional NumPy array containing the rows of events and columns
of variables. The order of the columns must match the order in which
you called ``AddVariable()`` for each variable.
targets : numpy array of shape [n_events] or [n_events, n_targets]
The target value(s) for each event in ``events``.
The target value(s) for each event in ``events``. For multiple target
values, ``targets`` must be a two-dimensional array with a column for
each target in the same order in which you called ``AddTarget()``.
weights : numpy array of shape [n_events], optional
Event weights.
test : bool, optional (default=False)
If True, then the events will be added as test events, otherwise
they are added as training events by default.
If True, then the events will be added as test events, otherwise they
are added as training events by default.
Notes
-----
A TMVA::Factory requires you to add both training and test events even if
you don't intend to call ``TestAllMethods()``.
"""
if not isinstance(factory, TMVA.Factory):
Expand Down
8 changes: 8 additions & 0 deletions root_numpy/tmva/src/TMVA.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ cdef extern from "2to3.h":
pass

cdef extern from "TMVA/Types.h" namespace "TMVA":
ctypedef enum EMVA "TMVA::Types::EMVA":
kCuts "TMVA::Types::kCuts"

ctypedef enum ETreeType "TMVA::Types::ETreeType":
kTraining "TMVA::Types::kTraining"
kTesting "TMVA::Types::kTesting"
Expand Down Expand Up @@ -30,6 +33,7 @@ cdef extern from "TMVA/IMethod.h" namespace "TMVA":

cdef extern from "TMVA/MethodBase.h" namespace "TMVA":
cdef cppclass MethodBase:
EMVA GetMethodType()
EAnalysisType GetAnalysisType()
DataSetInfo DataInfo()
unsigned int GetNVariables()
Expand All @@ -39,6 +43,10 @@ cdef extern from "TMVA/MethodBase.h" namespace "TMVA":
vector[float] GetRegressionValues()
Event* fTmpEvent

cdef extern from "TMVA/MethodCuts.h" namespace "TMVA":
cdef cppclass MethodCuts:
void SetTestSignalEfficiency(double eff)

cdef extern from "TMVA/Factory.h" namespace "TMVA":
cdef cppclass Factory:
void AddEvent(string& classname, ETreeType treetype, vector[double]& event, double weight)
Expand Down
Loading

0 comments on commit 8b5d3b8

Please sign in to comment.