3from textwrap
import dedent
14def write_dummy_file(variables, size=10, target_variable="target"):
16 rng = np.random.default_rng(42)
17 data = rng.normal(size=[size, len(variables) + 1])
19 for i, name
in enumerate(variables):
20 tree[name] = data[:, i]
21 tree[target_variable] = data[:, -1] > 0.5
22 with uproot.recreate(
'dummy.root')
as outfile:
23 outfile[
'tree'] = pd.DataFrame(tree, dtype=np.float64)
27 with open(
"bdt.py",
"w")
as f:
31 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
32 from sklearn.ensemble import GradientBoostingClassifier
33 clf = GradientBoostingClassifier()
37 "redefine this on purpose to ensure we spot if it gets overwritten"
38 from sklearn.ensemble import GradientBoostingClassifier
39 if not isinstance(state.estimator, GradientBoostingClassifier):
40 raise TypeError(f"Wrong classifier, expected GradientBoostingClassifier, got {type(state.estimator)}")
41 assert isinstance(state.estimator, GradientBoostingClassifier), "Wrong classifier"
42 x = state.estimator.predict_proba(X)
43 return np.require(x, dtype=np.float32, requirements=['A', 'W', 'C', 'O'])
47 variables = [
"var1",
"var2"]
48 general_options = basf2_mva.GeneralOptions()
49 general_options.m_datafiles = basf2_mva.vector(
"dummy.root")
50 general_options.m_treename =
"tree"
51 general_options.m_identifier =
"SKLearn-BDT.xml"
52 general_options.m_variables = basf2_mva.vector(*variables)
53 general_options.m_target_variable =
"target"
54 sklearn_nn_options = basf2_mva.PythonOptions()
55 sklearn_nn_options.m_framework =
"sklearn"
56 sklearn_nn_options.m_steering_file =
"bdt.py"
57 basf2_mva.teacher(general_options, sklearn_nn_options)
61 with open(
"mlp.py",
"w")
as f:
65 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
66 from sklearn.neural_network import MLPClassifier
71 state.estimator = state.estimator.fit(np.vstack(state.X), np.hstack(state.y))
72 return state.estimator
75 "redefine this on purpose to ensure we spot if it gets overwritten"
76 from sklearn.neural_network import MLPClassifier
77 if not isinstance(state.estimator, MLPClassifier):
78 raise TypeError(f"Wrong classifier, expected MLPClassifier, got {type(state.estimator)}")
79 x = state.estimator.predict_proba(X)
80 return np.require(x, dtype=np.float32, requirements=['A', 'W', 'C', 'O'])
84 variables = [
"var1",
"var2"]
85 general_options = basf2_mva.GeneralOptions()
86 general_options.m_datafiles = basf2_mva.vector(
"dummy.root")
87 general_options.m_treename =
"tree"
88 general_options.m_identifier =
"SKLearn-MLP.xml"
89 general_options.m_variables = basf2_mva.vector(*variables)
90 general_options.m_target_variable =
"target"
91 sklearn_nn_options = basf2_mva.PythonOptions()
92 sklearn_nn_options.m_framework =
"sklearn"
93 sklearn_nn_options.m_steering_file =
"mlp.py"
94 basf2_mva.teacher(general_options, sklearn_nn_options)
97def get_expert(identifier):
101 ROOT.Belle2.MVA.AbstractInterface.initSupportedInterfaces()
102 interfaces = ROOT.Belle2.MVA.AbstractInterface.getSupportedInterfaces()
103 interface = interfaces[
"Python"]
104 expert = interface.getExpert()
105 expert.load(method.weightfile)
111 Test if we can run multiple python methods for the same framework at the same time (MR !4244)
116 Try if we can run a sklearn bdt and a sklear mlp at the same time
119 from ROOT
import vector
121 variables = [
"var1",
"var2"]
122 write_dummy_file(variables)
127 bdt_expert = get_expert(
"SKLearn-BDT.xml")
128 mlp_expert = get_expert(
"SKLearn-MLP.xml")
130 inputs = vector[vector[float]]([vector[float]([1, 2])])
131 spectators = vector[vector[float]]()
133 general_options = basf2_mva.GeneralOptions()
134 general_options.m_variables = basf2_mva.vector(*variables)
135 dataset = ROOT.Belle2.MVA.MultiDataset(general_options, inputs, spectators)
137 bdt_expert.apply(dataset)
138 mlp_expert.apply(dataset)
141if __name__ ==
'__main__':
test_multiple_sklearn(self)
clean_working_directory()