8 from IPython.core.display
import display, HTML
9 from string
import Template
23 "MethodBase": [
"GetInteractiveTrainingError",
"ExitFromTraining",
"TrainingEnded",
"TrainMethod",
24 "GetMaxIter",
"GetCurrentIter"]
32 def __register(target, source, *args):
34 if hasattr(target, arg):
36 setattr(target, arg, getattr(source, arg))
43 def __changeMethod(target, source, *args):
44 def rewriter(originalFunction, newFunction):
45 def wrapper(*args, **kwargs):
46 kwargs[
"originalFunction"] = originalFunction
47 return newFunction(*args, **kwargs)
50 if arg.find(
"CallOriginal")!=-1:
51 originalName = arg.replace(
"Change",
"").replace(
"CallOriginal",
"")
52 setattr(target, originalName, rewriter(getattr(target, originalName), getattr(source, arg)))
54 setattr(target, arg.replace(
"Change",
""), getattr(source, arg))
62 PositionalArgsEnded =
False 63 for argName
in positionalArgumentsToNamed:
64 if not PositionalArgsEnded:
66 if (idx + 1) != len(args):
68 PositionalArgsEnded =
True 71 if PositionalArgsEnded
and argName
not in kwargs:
74 args.append(kwargs[argName])
82 originalFunction =
None 83 if optStringStartIndex != -10:
84 originalFunction = kwargs[
"originalFunction"]
85 del kwargs[
"originalFunction"]
86 OptionStringPassed =
False 87 if (len(args) - 1) == optStringStartIndex:
88 opt = args[optStringStartIndex] +
":" 90 del tmp[optStringStartIndex]
92 OptionStringPassed =
True 96 if type(kwargs[key]) == types.BooleanType:
97 if kwargs[key] ==
True:
100 opt +=
"!" + key +
":" 101 elif type(kwargs[key]) == types.ListType:
103 for o
in kwargs[key]:
104 if type(o) == types.DictType:
107 sst += kk +
"=" + str(o[kk]) +
"," 113 opt += key +
"=" + ss[:-1] +
":" 115 opt += key +
"=" + str(kwargs[key]) +
":" 117 if OptionStringPassed
or len(kwargs) > 0:
119 return (originalFunction, tuple(tmp))
125 def __unregister(target, *args):
127 if hasattr(target, arg):
134 def __getMethods(module, selector):
136 for method
in dir(module):
137 if method.find(selector)!=-1:
138 methods.append(method)
145 functions.__register(ROOT.TMVA.DataLoader, DataLoader, *functions.__getMethods(DataLoader,
"Draw"))
146 functions.__register(ROOT.TMVA.Factory, Factory, *functions.__getMethods(Factory,
"Draw"))
147 functions.__changeMethod(ROOT.TMVA.Factory, Factory, *functions.__getMethods(Factory,
"Change"))
148 functions.__changeMethod(ROOT.TMVA.DataLoader, DataLoader, *functions.__getMethods(DataLoader,
"Change"))
149 for key
in functions.ThreadedFunctions:
150 for func
in functions.ThreadedFunctions[key]:
151 setattr(getattr(getattr(ROOT.TMVA, key), func),
"_threaded",
True)
152 functions.__register(ROOT.TMVA.Factory, Factory,
"BookDNN")
158 functions.__register(ROOT.TMVA.DataLoader, DataLoader, *functions.__getMethods(DataLoader,
"Draw"))
159 functions.__register(ROOT.TMVA.Factory, Factory, *functions.__getMethods(Factory,
"Draw"))
166 __jsMVASourceDir =
"http://localhost:8888/notebooks/GSOC/wd/src/js" 169 __jsMVACSSDir =
"http://localhost:8888/notebooks/GSOC/wd/src/css" 179 __jsCode = Template(
""" 180 <div id="$divid" style="width: ${width}px; height:${height}px"></div> 184 'JsMVA':'$PATH/JsMVA.min' 187 require(['JsMVA'],function(jsmva){ 188 jsmva.$funcName('$divid','$dat'); 194 __jsCodeForDataInsert = Template(
"""<script id="dataInserterScript"> 195 require(['JsMVA'],function(jsmva){ 196 jsmva.$funcName('$divid', '$dat'); 197 var script = document.getElementById("dataInserterScript"); 198 script.parentElement.parentElement.remove(); 207 def Draw(obj, jsDrawMethod='draw', objIsJSON=False):
211 dat = ROOT.TBufferJSON.ConvertToJSON(obj)
212 dat = str(dat).replace(
"\n",
"")
214 display(HTML(JsDraw.__jsCode.substitute({
215 'funcName': jsDrawMethod,
216 'divid':
'jstmva_'+str(JsDraw.__divUID),
218 'PATH': JsDraw.__jsMVASourceDir,
219 'width': JsDraw.jsCanvasWidth,
220 'height': JsDraw.jsCanvasHeight
227 display(HTML(
'<link rel="stylesheet" href="' + JsDraw.__jsMVACSSDir +
'/' +cssName+
'"></link>'))
234 def InsertData(obj, dataInserterMethod="updateTrainingTestingErrors", objIsJSON=False):
238 dat = ROOT.TBufferJSON.ConvertToJSON(obj)
239 dat = str(dat).replace(
"\n",
"")
240 display(HTML(JsDraw.__jsCodeForDataInsert.substitute({
241 'funcName': dataInserterMethod,
242 'divid':
'jstmva_'+str(JsDraw.__divUID),
252 canvas = ROOT.TCanvas(
"csbplot", title[
"plot"], JsDraw.jsCanvasWidth, JsDraw.jsCanvasHeight)
253 sig.SetMaximum(ROOT.TMath.Max(sig.GetMaximum()*1.1,bkg.GetMaximum()*1.1))
254 sig.SetTitle(sig.GetTitle().replace(
"(Signal)",
""))
255 sig.GetXaxis().SetTitle(title[
"xaxis"])
256 sig.GetYaxis().SetTitle(title[
"yaxis"])
257 sig.SetTitle(title[
"plot"])
258 bkg.SetFillColorAlpha(ROOT.kRed, 0.3)
259 sig.SetFillColor(ROOT.kBlue)
260 bkg.SetLineColor(ROOT.kRed)
264 legend = ROOT.TLegend(1-canvas.GetLeftMargin()-0.39, 1-canvas.GetTopMargin()-0.15,
265 1-canvas.GetLeftMargin()-0.01, 1-canvas.GetTopMargin()-0.01)
266 legend.SetFillStyle(1)
267 legend.AddEntry(sig,
"Signal",
"F")
268 legend.AddEntry(bkg,
"Background",
"F")
269 legend.SetBorderSize(1)
270 legend.SetMargin(0.3)
273 return (canvas, legend)
Function inserter class This class contains the methods which are invoked by using jsmva magic...
def ConvertSpecKwargsToArgs(positionalArgumentsToNamed, args, kwargs)
Get's special parameters from kwargs and converts to positional parameter.
Class for creating the output scripts and inserting them to cell output.
def InsertCSS(cssName)
Inserts CSS file.
def register()
This function will register all functions which name contains "Draw" to TMVA.DataLoader and TMVA...
def unregister()
This function will remove all functions which name contains "Draw" from TMVA.DataLoader and TMVA...
def sbPlot(sig, bkg, title)
Draws a signal and background histogram to a newly created TCanvas.
def Draw(obj, jsDrawMethod='draw', objIsJSON=False)
Inserts the draw area and drawing JavaScript to output.
def InsertData(obj, dataInserterMethod="updateTrainingTestingErrors", objIsJSON=False)
Inserts the data inserter JavaScript code to output.
def ProcessParameters(optStringStartIndex, args, kwargs)
Converts object to TMVA style option string.