def __enter__(self): from cPickle import PickleError try: crossValidInfo = load( self._filePath ) except PickleError: # It failed without renaming the module, retry renaming old module # structure to new one. import sys sys.modules['FastNetTool.CrossValid'] = sys.modules[__name__] crossValidInfo = load( self._filePath ) # Open crossValidFile: try: if isinstance(crossValidInfo, dict): if crossValidInfo['type'] != 'CrossValidFile': self._logger.fatal(("Input crossValid file is not from PreProcFile " "type.")) if crossValidInfo['version'] == 2: crossValid = CrossValid.fromRawObj( crossValidInfo['crossValid'] ) elif crossValidInfo['version'] == 1: crossValid = crossValidInfo['crossValid'] from copy import copy crossValid = crossValid._cnvObj.treatObj( crossValid, copy( crossValid.__dict__ ) ) else: self._logger.fatal("Unknown job configuration version.") elif type(crossValidInfo) == list: # Read legacy files crossValid = crossValidInfo[3] else: self._logger.fatal("Invalid CrossValidFile contents.") except RuntimeError, e: self._logger.fatal(("Couldn't read cross validation file file '%s': Reason:" "\n\t %s") % (self._filePath, e))
def __enter__(self): from cPickle import PickleError try: crossValidInfo = load(self._filePath) except PickleError: # It failed without renaming the module, retry renaming old module # structure to new one. import sys sys.modules['FastNetTool.CrossValid'] = sys.modules[__name__] crossValidInfo = load(self._filePath) # Open crossValidFile: try: if isinstance(crossValidInfo, dict): if crossValidInfo['type'] != 'CrossValidFile': self._fatal( ("Input crossValid file is not from PreProcFile " "type.")) if crossValidInfo['version'] == 2: crossValid = CrossValid.fromRawObj( crossValidInfo['crossValid']) elif crossValidInfo['version'] == 1: crossValid = crossValidInfo['crossValid'] from copy import copy crossValid = crossValid._cnvObj.treatObj( crossValid, copy(crossValid.__dict__)) else: self._fatal("Unknown job configuration version.") elif type(crossValidInfo) == list: # Read legacy files crossValid = crossValidInfo[3] else: self._fatal("Invalid CrossValidFile contents.") except RuntimeError, e: self._fatal( ("Couldn't read cross validation file file '%s': Reason:" "\n\t %s") % (self._filePath, e))
def __enter__(self): # Open file: jobConfig = load(self._filePath) try: if type(jobConfig) is dict: if jobConfig['type'] != self._type: self._logger.fatal(("Input jobConfig file is not from jobConfig " "type.")) # Read configuration file to retrieve pre-processing, if jobConfig['version'] == 1: neuronBounds = MatlabLoopingBounds( jobConfig['neuronBounds'] ) sortBounds = PythonLoopingBounds( jobConfig['sortBounds'] ) initBounds = PythonLoopingBounds( jobConfig['initBounds'] ) else: self._logger.fatal("Unknown job configuration version") elif type(jobConfig) is list: # zero version file (without versioning # control): neuronBounds = MatlabLoopingBounds( [jobConfig[0], jobConfig[0]] ) sortBounds = MatlabLoopingBounds( jobConfig[1] ) initBounds = MatlabLoopingBounds( jobConfig[2] ) else: self._logger.fatal("Unknown file type entered for config file.") except RuntimeError, e: self._logger.fatal(("Couldn't read configuration file '%s': Reason:" "\n\t %s" % (self._filePath, e)))
def __enter__(self): # Open file: jobConfig = load(self._filePath) try: if type(jobConfig) is dict: if jobConfig['type'] != self._type: self._fatal(("Input jobConfig file is not from jobConfig " "type.")) # Read configuration file to retrieve pre-processing, if jobConfig['version'] == 1: neuronBounds = MatlabLoopingBounds( jobConfig['neuronBounds'] ) sortBounds = PythonLoopingBounds( jobConfig['sortBounds'] ) initBounds = PythonLoopingBounds( jobConfig['initBounds'] ) else: self._fatal("Unknown job configuration version") elif type(jobConfig) is list: # zero version file (without versioning # control): neuronBounds = MatlabLoopingBounds( [jobConfig[0], jobConfig[0]] ) sortBounds = MatlabLoopingBounds( jobConfig[1] ) initBounds = MatlabLoopingBounds( jobConfig[2] ) else: self._fatal("Unknown file type entered for config file.") except RuntimeError, e: self._fatal(("Couldn't read configuration file '%s': Reason:" "\n\t %s" % (self._filePath, e)))
def __init__(self, crossvalFileName, monFileName, **kw): from ROOT import TFile, gROOT gROOT.ProcessLine("gErrorIgnoreLevel = kFatal;"); #Set all global setting from ROOT style plot! SetTuningStyle() Logger.__init__(self, kw) #Hold all information abount the monitoring root file self._infoObjs = list() try:#Protection self._logger.info('Reading monRootFile (%s)',monFileName) self._rootObj = TFile(monFileName, 'read') except RuntimeError: self._logger.fatal('Could not open root monitoring file.') from RingerCore import load try:#Protection self._logger.info('Reading crossvalFile (%s)',crossvalFileName) crossvalObj = load(crossvalFileName) except RuntimeError: self._logger.fatal('Could not open pickle summary file.') #Loop over benchmarks for benchmarkName in crossvalObj.keys(): #Must skip if ppchain collector if benchmarkName == 'infoPPChain': continue #Add summary information into MonTuningInfo helper class self._infoObjs.append( TuningMonitoringInfo( benchmarkName, crossvalObj[benchmarkName] ) ) self._logger.info('Creating MonTuningInfo for %s and the iterator object [et=%d, eta=%d]', benchmarkName,self._infoObjs[-1].etbin(), self._infoObjs[-1].etabin()) #Loop over all benchmarks #Always be the same bin for all infoObjs etabin = self._infoObjs[0].etabin() etbin = self._infoObjs[0].etbin() #Reading the data rings from path or object refFile = kw.pop('refFile', None) if refFile: from TuningTools import TuningDataArchieve TDArchieve = TuningDataArchieve(refFile) self._logger.info(('Reading perf file with name %s')%(refFile)) try: with TDArchieve as data: self._data = (data['signal_patterns'][etbin][etabin], data['background_patterns'][etbin][etabin]) except RuntimeError: self._logger.fatal('Could not open the patterns data file.') raise RuntimeError('Could not open the patterns data file.') else: patterns = kw.pop('patterns', None) if patterns: self._data = (patterns['signal_patterns'][etbin][etabin], patterns['background_patterns'][etbin][etabin]) else: raise RuntimeError('You must pass the ref file as parameter. abort!')
def __init__(self, crossvalFileName, monFileName, **kw): from ROOT import TFile, gROOT gROOT.ProcessLine("gErrorIgnoreLevel = kFatal;") #Set all global setting from ROOT style plot! SetTuningStyle() Logger.__init__(self, kw) #Hold all information abount the monitoring root file self._infoObjs = list() try: #Protection self._logger.info('Reading monRootFile (%s)', monFileName) self._rootObj = TFile(monFileName, 'read') except RuntimeError: self._logger.fatal('Could not open root monitoring file.') from RingerCore import load try: #Protection self._logger.info('Reading crossvalFile (%s)', crossvalFileName) crossvalObj = load(crossvalFileName) except RuntimeError: self._logger.fatal('Could not open pickle summary file.') #Loop over benchmarks for benchmarkName in crossvalObj.keys(): #Must skip if ppchain collector if benchmarkName == 'infoPPChain': continue #Add summary information into MonTuningInfo helper class self._infoObjs.append( TuningMonitoringInfo(benchmarkName, crossvalObj[benchmarkName])) self._logger.info( 'Creating MonTuningInfo for %s and the iterator object [et=%d, eta=%d]', benchmarkName, self._infoObjs[-1].etbinidx(), self._infoObjs[-1].etabinidx()) #Loop over all benchmarks #Always be the same bin for all infoObjs etabinidx = self._infoObjs[0].etabinidx() etbinidx = self._infoObjs[0].etbinidx() #Reading the data rings from path or object dataPath = kw.pop('dataPath', None) if dataPath: from TuningTools import TuningDataArchieve self._logger.info( ('Reading data tuning file with name %s') % (dataPath)) TDArchieve = TuningDataArchieve.load(dataPath, etBinIdx=etbinidx, etaBinIdx=etabinidx, loadEfficiencies=False) self._data = (TDArchieve.signalPatterns, TDArchieve.backgroundPatterns) else: self._logger.fatal( 'You must pass the ref file as parameter. abort!')
def __enter__(self): subsetColInfo = load( self._filePath ) # Open crossValidPreprocFile: try: if isinstance(subsetColInfo, dict): if subsetColInfo['type'] != 'SubsetGeneratorCollectionFile': self._fatal(("Input subsetCol file is not from SubsetGeneratorCollectionFile type.")) if subsetColInfo['version'] == 0: self._subsetCol = SubsetGeneratorCollection.fromRawObj( subsetColInfo['subsetCol'] ) else: self._fatal("Unknown job configuration version.") else: self._fatal("Invalid SubsetGeneratorCollectionFile contents.") except RuntimeError, e: self._fatal(("Couldn't read cross preproc validation file file '%s': Reason:" "\n\t %s") % (self._filePath, e))
def __enter__(self): # Open file: jobConfig = load(self._filePath) try: if type(jobConfig) is dict: if jobConfig['type'] != self._type: self._fatal(("Input jobConfig file is not from jobConfig " "type.")) # Read configuration file to retrieve pre-processing, if jobConfig['version'] >= 1: neuronBounds = MatlabLoopingBounds( jobConfig['neuronBounds']) sortBounds = PythonLoopingBounds(jobConfig['sortBounds']) initBounds = PythonLoopingBounds(jobConfig['initBounds']) modelBounds = [None for _ in neuronBounds()] if jobConfig['version'] == 2: modelBounds = jobConfig['modelBounds'] if modelBounds and coreConf() is TuningToolCores.keras: from keras.models import model_from_json import json for idx, model in enumerate(modelBounds): modelBounds[idx] = model_from_json( json.dumps(model, separators=( ',', ':'))) if model else None else: self._fatal("Unknown job configuration version") elif type(jobConfig ) is list: # zero version file (without versioning # control): neuronBounds = MatlabLoopingBounds( [jobConfig[0], jobConfig[0]]) sortBounds = MatlabLoopingBounds(jobConfig[1]) initBounds = MatlabLoopingBounds(jobConfig[2]) modelBounds = None else: self._fatal("Unknown file type entered for config file.") except RuntimeError, e: self._fatal(("Couldn't read configuration file '%s': Reason:" "\n\t %s" % (self._filePath, e)))
def changeOp(d, key, mat): def changePassed(key, mat): for lkey in d[key].item(): for rowIdx, array1 in enumerate( d[key].item()[lkey]): for columnIdx, rawdict in enumerate(array1): rawdict['_passed'] = mat[rowIdx][columnIdx] def changeTotal(key, mat): for lkey in d[key].item(): for rowIdx, array1 in enumerate( d[key].item()[lkey]): for columnIdx, rawdict in enumerate(array1): rawdict['_count'] = 100. changePassed( key, mat ) changeTotal( key, mat ) from RingerCore import load, save a = load('mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.medium.npz') d = dict(a) changeOp( d, 'signal_efficiencies', mergeEffTable( veryloose20160701 ) ) changeOp( d, 'background_efficiencies', pfrefs_vloose ) save(d, 'mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.veryloose', protocol = 'savez_compressed') d = dict(a) changeOp( d, 'signal_efficiencies', mergeEffTable( loose20160701 ) ) changeOp( d, 'background_efficiencies', pfrefs_loose ) save(d, 'mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.loose', protocol = 'savez_compressed') d = dict(a) changeOp( d, 'signal_efficiencies', mergeEffTable( tight20160701 ) ) changeOp( d, 'background_efficiencies', pfrefs_tight ) save(d, 'mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.tight', protocol = 'savez_compressed')
parser.add_argument('-d', '--data', action='store', dest='data', required=True, nargs='+', help="The input tuning files.") import sys, os if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() # Take all files paths = csvStr2List(args.data) paths = expandFolders(paths) from RingerCore import load, save, appendToFileName for f in paths: ff = load(f) for k in ff.keys(): if 'SP' in k: etBin = ff[k]['etBinIdx'] etaBin = ff[k]['etaBinIdx'] print 'etBin = ', etBin, ', etaBin = ', etaBin outname = f.split('/')[len(f.split('/')) - 2] cOutputName = appendToFileName(outname, ('et%d_eta%d') % (etBin, etaBin)) save(ff, cOutputName, compress=True)
npCurrent, _ = retrieve_npConstants() npCurrent.level = args.output_level logger = Logger.getModuleLogger(__name__, args.output_level) files = expandFolders(args.inputs) from zipfile import BadZipfile from copy import deepcopy for f in files: logger.info("Turning numpy matrix file '%s' into pre-processing file...", f) fileparts = f.split("/") folder = "/".join(fileparts[0:-1]) + "/" fname = fileparts[-1] try: data = dict(load(f)) except BadZipfile, e: logger.warning("Couldn't load file '%s'. Reason:\n%s", f, str(e)) continue logger.debug("Finished loading file '%s'...", f) for key in data: if key == "W": ppCol = deepcopy(data["W"]) from TuningTools.PreProc import * for obj, idx, parent, _, _ in traverse(ppCol, tree_types=(np.ndarray,), max_depth=3): parent[idx] = PreProcChain(RemoveMean(), Projection(matrix=obj), UnitaryRMS()) # Turn arrays into mutable objects: ppCol = ppCol.tolist() ppCol = fixPPCol(ppCol, len(ppCol[0][0]), len(ppCol[0]), len(ppCol)) if fname.endswith(".npz"):
def makeSummaryMonSlides(outputs, nbins, choicesfile, grid=False): from scipy.io import loadmat import os f = loadmat(choicesfile) choices = dict() choices['Pd'] = f['choices']['Pd'][0][0] choices['Pf'] = f['choices']['Pf'][0][0] choices['SP'] = f['choices']['SP'][0][0] nbinchoices = 0 for etchoices in choices['Pd']: nbinchoices += len(etchoices) if not nbins == nbinchoices: raise NameError('Choices Archieve Error') net = len(choices['Pd']) neta = len(choices['Pd'][0]) unbinned = False if net == 1 and neta == 1: unbinned = True if grid: f = load([s for s in os.listdir('.') if '_et0_eta0' in s][0] + '/perfBounds.pic.gz') else: f = load('{}/perfBounds.pic.gz'.format(outputs + '_et0_eta0')) benchmarkNames = f['perf'].keys() for benchmarkName in benchmarkNames: slideAnex = [] for et in xrange(net): etlist = [] for eta in xrange(neta): etaDict = dict() neuron = choices[benchmarkName.split('_')[-1]][et][eta] basepath = outputs basepath += ('_et%d_eta%d') % (et, eta) if grid: basepath = ('_et%d_eta%d') % (et, eta) basepath = [s for s in os.listdir('.') if basepath in s][0] f = load(basepath + '/perfBounds.pic.gz') else: f = load('{}/perfBounds.pic.gz'.format(basepath)) etstr = f['bounds']['etbinstr'] etastr = f['bounds']['etabinstr'] perfs = f['perf'][benchmarkName] refBench = perfs['config_' + str(neuron).zfill(3)].getRef() detR = r'{:.2f}'.format(refBench['det']) spR = r'{:.2f}'.format(refBench['sp']) faR = r'{:.2f}'.format(refBench['fa']) refBench = [detR, spR, faR] perfBench = perfs['config_' + str(neuron).zfill(3)].getPerf() detP = r'{:.2f}$\pm${:.2f}'.format(perfBench['detMean'], perfBench['detStd']) spP = r'{:.2f}$\pm${:.2f}'.format(perfBench['spMean'], perfBench['spStd']) faP = r'{:.2f}$\pm${:.2f}'.format(perfBench['faMean'], perfBench['faStd']) perfBench = [detP, spP, faP] bestNetBench = perfs['config_' + str(neuron).zfill(3)].rawOp() detB = r'{:.2f}'.format(bestNetBench['det'] * 100) spB = r'{:.2f}'.format(bestNetBench['sp'] * 100) faB = r'{:.2f}'.format(bestNetBench['fa'] * 100) bestNetBench = [detB, spB, faB] perfs = [refBench, perfBench, bestNetBench] graphSections = [ 'All Sorts(Validation)', 'All ROC Sorts(Validation)', 'All Sorts(Operation)', 'All ROC Sorts(Operation)', 'Best Network', 'Best Operation Output', ] figures = [ '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_val.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_roc_tst.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_op.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_roc_op.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_best_op.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_best_op_output.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), ] figuresDict = dict(zip(graphSections, figures)) etaDict['neuron'] = neuron etaDict['figures'] = figuresDict etaDict['graphSections'] = graphSections etaDict['perfs'] = perfs etaDict['etastr'] = etastr etaDict['etstr'] = etstr etlist.append(etaDict) # for eta slideAnex.append(etlist) #for et with BeamerTexReportTemplate1(theme='Berlin', _toPDF=True, title=benchmarkName, outputFile=benchmarkName, font='structurebold'): with BeamerSection(name='Performance'): if not unbinned: l1 = ['', ''] l2 = ['', ''] sideline = '{|c|c|' for et in xrange(net): sideline += 'ccc|' l1.extend(['', slideAnex[et][0]['etstr'], '']) l2.extend(['Pd', 'SP', 'PF']) sideline += '}' etlines = [] for eta in xrange(neta): la = [ r'\hline' + slideAnex[0][eta]['etastr'], 'CrossValidation' ] lb = [r'', 'Reference'] lc = [r'', 'bestNetBench'] for et in xrange(net): prfs = slideAnex[et][eta]['perfs'] refBench = prfs[0] perfBench = prfs[1] bestNetBench = prfs[2] la.extend(perfBench) lb.extend(refBench) lc.extend(bestNetBench) etlines.extend([la, lb, lc]) linhas = [] linhas.append(l1) linhas.append(l2) linhas.extend(etlines) with BeamerSubSection(name='All Bins Performance'): BeamerTableSlide( title='All Bins Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) for et in xrange(net): l1 = ['', ''] l2 = ['', ''] sideline = '{|c|c|ccc|}' l1.extend(['', slideAnex[et][0]['etstr'], '']) l2.extend(['Pd', 'SP', 'PF']) etlines = [] for eta in xrange(neta): if unbinned: la = [r'\hline', 'CrossValidation'] else: la = [ r'\hline' + slideAnex[0][eta]['etastr'], 'CrossValidation' ] lb = [r'Neuron', 'Reference'] lc = [r'', 'bestNetBench'] neuron = slideAnex[et][eta]['neuron'] prfs = slideAnex[et][eta]['perfs'] lc[0] = '%d' % neuron refBench = prfs[0] perfBench = prfs[1] bestNetBench = prfs[2] la.extend(perfBench) lb.extend(refBench) lc.extend(bestNetBench) etlines.extend([la, lb, lc]) linhas = [] if not unbinned: linhas.append(l1) linhas.append(l2) linhas.extend(etlines) if unbinned: BeamerTableSlide( title='Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) else: with BeamerSubSection(name=slideAnex[et][0]['etstr']): BeamerTableSlide( title='Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) with BeamerSection(name='Figures'): if unbinned: neuron = slideAnex[0][0]['neuron'] graphSections = slideAnex[0][0]['graphSections'] figures = slideAnex[0][0]['figures'] for graph in graphSections: BeamerFigureSlide(title=graph + ' Neuron: ' + str(neuron), path=figures[graph]) else: for et in xrange(net): with BeamerSubSection(name=slideAnex[et][0]['etstr']): for eta in xrange(neta): neuron = slideAnex[et][eta]['neuron'] graphSections = slideAnex[et][eta][ 'graphSections'] figures = slideAnex[et][eta]['figures'] with BeamerSubSubSection( name=slideAnex[et][eta]['etastr'] + ', Neuron: {}'.format(neuron)): for graph in graphSections: BeamerFigureSlide(title=graph, path=figures[graph]) return
npCurrent.level = args.output_level logger = Logger.getModuleLogger(__name__, args.output_level) files = expandFolders(args.inputs) from zipfile import BadZipfile from copy import deepcopy for f in files: logger.info("Turning numpy matrix file '%s' into pre-processing file...", f) fileparts = f.split('/') folder = '/'.join(fileparts[0:-1]) + '/' fname = fileparts[-1] try: data = dict(load(f)) except BadZipfile, e: logger.warning("Couldn't load file '%s'. Reason:\n%s", f, str(e)) continue logger.debug("Finished loading file '%s'...", f) for key in data: if key == 'W': ppCol = deepcopy(data['W']) from TuningTools.PreProc import * for obj, idx, parent, _, _ in traverse(ppCol, tree_types=(np.ndarray, ), max_depth=3): parent[idx] = PreProcChain(RemoveMean(), Projection(matrix=obj), UnitaryRMS()) # Turn arrays into mutable objects:
args.inputFiles = expandFolders(args.inputFiles) mainLogger.verbose("All input files are:") if mainLogger.isEnabledFor(LoggingLevel.VERBOSE): pprint(args.inputFiles) for inFile in progressbar(args.inputFiles, len(args.inputFiles), logger=mainLogger, prefix="Processing files "): # Treat output file name: from RingerCore import checkExtension, changeExtension, load, save if checkExtension(inFile, "tgz|tar.gz|pic"): cOutputName = changeExtension(inFile, '.mat') if args.change_output_folder: import os.path cOutputName = os.path.join( os.path.abspath(args.change_output_folder), os.path.basename(cOutputName)) data = load(inFile, useHighLevelObj=False) from scipy.io import savemat try: savemat(cOutputName, data) except ImportError: self._logger.fatal( ("Cannot save matlab file, it seems that scipy is not " "available."), ImportError) mainLogger.info("Successfully created matlab file: %s", cOutputName) else: mainLogger.error("Cannot transform files '%s' to matlab." % inFile) # end of (for fileCollection)
def getModels(self, summaryInfoList, **kw): refBenchCol = kw.pop( 'refBenchCol', None ) configCol = kw.pop( 'configCol', [] ) muBin = kw.pop( 'muBin', [-999,9999] ) checkForUnusedVars( kw, self._logger.warning ) # Treat the summaryInfoList if not isinstance( summaryInfoList, (list,tuple)): summaryInfoList = [ summaryInfoList ] summaryInfoList = list(traverse(summaryInfoList,simple_ret=True)) nSummaries = len(summaryInfoList) if not nSummaries: logger.fatal("Summary dictionaries must be specified!") if refBenchCol is None: refBenchCol = summaryInfoList[0].keys() # Treat the reference benchmark list if not isinstance( refBenchCol, (list,tuple)): refBenchCol = [ refBenchCol ] * nSummaries if len(refBenchCol) == 1: refBenchCol = refBenchCol * nSummaries nRefs = len(list(traverse(refBenchCol,simple_ret=True))) # Make sure that the lists are the same size as the reference benchmark: nConfigs = len(list(traverse(configCol,simple_ret=True))) if nConfigs == 0: configCol = [None for i in range(nRefs)] elif nConfigs == 1: configCol = configCol * nSummaries nConfigs = len(list(traverse(configCol,simple_ret=True))) if nConfigs != nRefs: logger.fatal("Summary size is not equal to the configuration list.", ValueError) if nRefs == nConfigs == nSummaries: # If user input data without using list on the configuration, put it as a list: for o, idx, parent, _, _ in traverse(configCol): parent[idx] = [o] for o, idx, parent, _, _ in traverse(refBenchCol): parent[idx] = [o] configCol = list(traverse(configCol,max_depth_dist=1,simple_ret=True)) refBenchCol = list(traverse(refBenchCol,max_depth_dist=1,simple_ret=True)) nConfigs = len(configCol) nSummary = len(refBenchCol) if nRefs != nConfigs != nSummary: logger.fatal("Number of references, configurations and summaries do not match!", ValueError) discrList = [] from itertools import izip, count for summaryInfo, refBenchmarkList, configList in zip(summaryInfoList,refBenchCol,configCol): if type(summaryInfo) is str: self._logger.info('Loading file "%s"...', summaryInfo) summaryInfo = load(summaryInfo) elif type(summaryInfo) is dict: pass else: logger.fatal("Cross-valid summary info is not string and not a dictionary.", ValueError) for idx, refBenchmarkName, config in izip(count(), refBenchmarkList, configList): try: key = filter(lambda x: refBenchmarkName in x, summaryInfo)[0] refDict = summaryInfo[ key ] except IndexError : self._logger.fatal("Could not find reference %s in summaryInfo. Available options are: %r", refBenchmarkName, summaryInfo.keys()) self._logger.info("Using Reference key: %s", key ) ppInfo = summaryInfo['infoPPChain'] etBinIdx = refDict['etBinIdx'] etaBinIdx = refDict['etaBinIdx'] etBin = refDict['etBin'] etaBin = refDict['etaBin'] info = refDict['infoOpBest'] if config is None else refDict['config_' + str(config).zfill(3)]['infoOpBest'] # Check if user specified parameters for exporting discriminator # operation information: sort = info['sort'] init = info['init'] pyThres = info['cut'] from RingerCore import retrieveRawDict if isinstance( pyThres, float ): pyThres = RawThreshold( thres = pyThres , etBinIdx = etBinIdx, etaBinIdx = etaBinIdx , etBin = etBin, etaBin = etaBin) else: # Get the object from the raw dict pyThres = retrieveRawDict( pyThres ) if pyThres.etBin is None: pyThres.etBin = etBin elif pyThres.etBin is '': pyThres.etBin = etBin elif isinstance( pyThres.etBin, (list,tuple)): pyThres.etBin = np.array( pyThres.etBin) if not(np.array_equal( pyThres.etBin, etBin )): self._logger.fatal("etBin does not match for threshold! Should be %r, is %r", pyThres.etBin, etBin ) if pyThres.etaBin is None: pyThres.etaBin = etaBin elif pyThres.etaBin is '': pyThres.etaBin = etaBin elif isinstance( pyThres.etaBin, (list,tuple)): pyThres.etaBin = np.array( pyThres.etaBin) if not(np.array_equal( pyThres.etaBin, etaBin )): self._logger.fatal("etaBin does not match for threshold! Should be %r, is %r", pyThres.etaBin, etaBin ) if type(pyThres) is RawThreshold: thresValues = [pyThres.thres] else: thresValues = [pyThres.slope, pyThres.intercept, pyThres.rawThres] pyPreProc = ppInfo['sort_'+str(sort).zfill(3)]['items'][0] pyPreProc = retrieveRawDict( pyPreProc ) useCaloRings=False; useTrack=False; useShowerShape=False if type(pyPreProc) is Norm1: useCaloRings=True elif type(pyPreProc) is TrackSimpleNorm: useTrack=True elif type(pyPreProc) is ShowerShapesSimpleNorm: useShowerShape=True elif type(pyPreProc) is ExpertNetworksSimpleNorm: useCaloRings=True; useTrack=True elif type(pyPreProc) is ExpertNetworksShowerShapeSimpleNorm: useCaloRings=True; useShowerShape=True elif type(pyPreProc) is ExpertNetworksShowerShapeAndTrackSimpleNorm: useCaloRings=True; useTrack=True; useShowerShape=True elif type(pyPreProc) is PreProcMerge: for slot in pyPreProc.slots: if type(pyPreProc) is Norm1: useCaloRings=True elif type(pyPreProc) is TrackSimpleNorm: useTrack=True elif type(pyPreProc) is ShowerShapesSimpleNorm: useShowerShape=True elif type(pyPreProc) is ExpertNetworksSimpleNorm: useCaloRings=True; useTrack=True elif type(pyPreProc) is ExpertNetworksShowerShapeSimpleNorm: useCaloRings=True; useShowerShape=True elif type(pyPreProc) is ExpertNetworksShowerShapeAndTrackSimpleNorm: useCaloRings=True; useTrack=True; useShowerShape=True else: self._logger.fatal('PrepProc strategy not found...') discrDict = info['discriminator'] model = { 'discriminator' : discrDict, 'threshold' : thresValues, 'etBin' : etBin, 'etaBin' : etaBin, 'muBin' : muBin, 'etBinIdx' : etBinIdx, 'etaBinIdx' : etaBinIdx, } removeOutputTansigTF = refDict.get('removeOutputTansigTF', None ) model['removeOutputTansigTF'] = removeOutputTansigTF model['useCaloRings'] = useCaloRings model['useShowerShape'] = useShowerShape model['useTrack'] = useTrack discrList.append( model ) self._logger.info('neuron = %d, sort = %d, init = %d', info['neuron'], info['sort'], info['init']) # for benchmark # for summay in list return discrList
RingerLayer.EM1, RingerLayer.EM2, RingerLayer.EM3, RingerLayer.HAD1, RingerLayer.HAD2, RingerLayer.HAD3,] from RingerCore import load,save from RingerCore import changeExtension, ensureExtension, appendToFileName, progressbar, mkdir_p from itertools import product import numpy as np if args.outputPath is None: args.outputPath = os.path.dirname(args.inputFile) if not os.path.isdir( args.outputPath ): mkdir_p( args.outputPath ) f = load(args.inputFile) # Copy all metada information baseDict = { k : f[k] for k in f.keys() if not '_etBin_' in k and not '_etaBin_' in k } nEtBins = f['nEtBins'].item() nEtaBins = f['nEtaBins'].item() for etIdx, etaIdx in progressbar( product(xrange(nEtBins), xrange(nEtaBins)) , nEtBins*nEtaBins , logger = mainLogger , prefix = 'Juicing file '): binDict= {k:f[k] for k in f.keys() if 'etBin_%d_etaBin_%d'%(etIdx,etaIdx) in k} binDict.update(baseDict) from copy import deepcopy for layer in caloLayers: pp=PreProcChain([RingerLayerSegmentation(layer=layer)]) tmpBinDict = deepcopy(binDict)
pprint(args.inputFiles) ## Treat special arguments if len( args.inputFiles ) == 1: args.inputFiles = csvStr2List( args.inputFiles[0] ) args.inputFiles = expandFolders( args.inputFiles ) mainLogger.verbose("All input files are:") if mainLogger.isEnabledFor( LoggingLevel.VERBOSE ): pprint(args.inputFiles) for inFile in progressbar(args.inputFiles, len(args.inputFiles), logger = mainLogger, prefix = "Processing files "): # Treat output file name: from RingerCore import checkExtension, changeExtension, load, save if checkExtension( inFile, "tgz|tar.gz|pic" ): cOutputName = changeExtension( inFile, '.mat' ) if args.change_output_folder: import os.path cOutputName = os.path.join( os.path.abspath(args.change_output_folder) , os.path.basename(cOutputName) ) data = load( inFile, useHighLevelObj = False ) from scipy.io import savemat try: savemat( cOutputName, data ) except ImportError: self._logger.fatal(("Cannot save matlab file, it seems that scipy is not " "available."), ImportError) mainLogger.info("Successfully created matlab file: %s", cOutputName) else: mainLogger.error("Cannot transform files '%s' to matlab." % inFile) # end of (for fileCollection)
def makeSummaryMonSlides(outputs, nbins, choicesfile): from scipy.io import loadmat f = loadmat(choicesfile) choices = dict() choices['Pd'] = f['choices']['Pd'][0][0] choices['Pf'] = f['choices']['Pf'][0][0] choices['SP'] = f['choices']['SP'][0][0] nbinchoices = 0 for etchoices in choices['Pd']: nbinchoices += len(etchoices) if not nbins == nbinchoices: raise NameError('Choices Archieve Error') net = len(choices['Pd']) neta = len(choices['Pd'][0]) unbinned = False if net == 1 and neta == 1: unbinned = True f = load('{}/anex.pic.gz'.format(outputs + '_et0_eta0')) benchmarkNames = f['perf'].keys() for benchmarkName in benchmarkNames: slideAnex = [] for et in xrange(net): etlist = [] for eta in xrange(neta): etaDict = dict() neuron = choices[benchmarkName.split('_')[-1]][et][eta] basepath = outputs basepath += ('_et%d_eta%d') % (et, eta) f = load('{}/anex.pic.gz'.format(basepath)) etaDict['nsignal'] = f['nsignal'] etaDict['nbackground'] = f['nbackground'] etstr = f['bounds']['etbinstr'] etastr = f['bounds']['etabinstr'] perfs = f['perf'][benchmarkName] refBench = perfs['config_' + str(neuron).zfill(3)].getRef() perfBench = perfs['config_' + str(neuron).zfill(3)].getPerf() bestNetBench = perfs['config_' + str(neuron).zfill(3)].rawOp() etaDict['sortMaximuns'] = perfs[ 'config_' + str(neuron).zfill(3)].getSortPerfs() detR = (refBench['det']) spR = (refBench['sp']) faR = (refBench['fa']) detRstr = r'{:.2f}'.format(detR) spRstr = r'{:.2f}'.format(spR) faRstr = r'{:.2f}'.format(faR) perf = dict() perf['refBench'] = dict() perf['refBench']['det'] = detR perf['refBench']['sp'] = spR perf['refBench']['fa'] = faR refBench = [detRstr, spRstr, faRstr] detP = (perfBench['detMean'], perfBench['detStd']) spP = (perfBench['spMean'], perfBench['spStd']) faP = (perfBench['faMean'], perfBench['faStd']) detPstr = r'%.2f$\pm$%.2f' % (detP) spPstr = r'%.2f$\pm$%.2f' % (spP) faPstr = r'%.2f$\pm$%.2f' % (faP) perf['perfBench'] = dict() perf['perfBench']['det'] = detP perf['perfBench']['sp'] = spP perf['perfBench']['fa'] = faP perfBench = [detPstr, spPstr, faPstr] detB = (bestNetBench['det'] * 100) spB = (bestNetBench['sp'] * 100) faB = (bestNetBench['fa'] * 100) detBstr = r'{:.2f}'.format(detB) spBstr = r'{:.2f}'.format(spB) faBstr = r'{:.2f}'.format(faB) perf['bestNetBench'] = dict() perf['bestNetBench']['det'] = detB perf['bestNetBench']['sp'] = spB perf['bestNetBench']['fa'] = faB bestNetBench = [detBstr, spBstr, faBstr] perfs = [refBench, perfBench, bestNetBench] graphSections = [ 'All Sorts(Validation)', 'All ROC Sorts(Validation)', 'All Sorts(Operation)', 'All ROC Sorts(Operation)', 'Best Network', 'Best Operation Output', ] figures = [ '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_val.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_roc_tst.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_op.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_sorts_roc_op.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_best_op.pdf'. format(basepath, benchmarkName, neuron, benchmarkName, neuron), '{}/figures/{}/neuron_{}/plot_{}_neuron_{}_best_op_output.pdf' .format(basepath, benchmarkName, neuron, benchmarkName, neuron), ] figuresDict = dict(zip(graphSections, figures)) etaDict['neuron'] = neuron etaDict['figures'] = figuresDict etaDict['graphSections'] = graphSections etaDict['perfs'] = perfs etaDict['perf'] = perf etaDict['etastr'] = etastr etaDict['etstr'] = etstr etlist.append(etaDict) # for eta slideAnex.append(etlist) #for et totalPerfs = calcPerformance(slideAnex, net, neta) with BeamerTexReportTemplate1(theme='Berlin', _toPDF=True, title=benchmarkName, outputFile=benchmarkName, font='structurebold'): with BeamerSection(name='Performance'): if not unbinned: l1 = [''] sideline = '{|c|ccc|}' l1.extend([r'Pd(\%)', r'SP(\%)', r'PF(\%)']) bodylines = [] la = [r'\hline' + '\n' + r'\text{CrossValidation}'] lb = ['Reference'] lc = ['bestNetBench'] refBench = [ totalPerfs['pdRef'], totalPerfs['spRef'], totalPerfs['pfRef'] ] bestNetBench = [ totalPerfs['pdBest'], totalPerfs['spBest'], totalPerfs['pfBest'] ] detP = (totalPerfs['pdTuning'], totalPerfs['pdStdTuning']) spP = (totalPerfs['spTuning'], totalPerfs['spStdTuning']) pfP = (totalPerfs['pfTuning'], totalPerfs['pfStdTuning']) detPstr = r'%.2f$\pm$%.2f' % (detP) spPstr = r'%.2f$\pm$%.2f' % (spP) pfPstr = r'%.2f$\pm$%.2f' % (pfP) perfBench = [detPstr, spPstr, pfPstr] la.extend(perfBench) lb.extend(refBench) lc.extend(bestNetBench) bodylines.extend([la, lb, lc]) linhas = [] linhas.append(l1) linhas.extend(bodylines) with BeamerSubSection(name='General Performance'): BeamerTableSlide( title='General Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) l1 = ['', ''] l2 = ['', ''] sideline = '{|c|c|' for et in xrange(net): sideline += 'ccc|' l1.extend(['', slideAnex[et][0]['etstr'], '']) l2.extend([r'Pd(\%)', r'SP(\%)', r'PF(\%)']) sideline += '}' etlines = [] for eta in xrange(neta): la = [ r'\hline' + slideAnex[0][eta]['etastr'], 'CrossValidation' ] lb = [r'', 'Reference'] lc = [r'', 'bestNetBench'] for et in xrange(net): prfs = slideAnex[et][eta]['perfs'] refBench = prfs[0] perfBench = prfs[1] bestNetBench = prfs[2] la.extend(perfBench) lb.extend(refBench) lc.extend(bestNetBench) etlines.extend([la, lb, lc]) linhas = [] linhas.append(l1) linhas.append(l2) linhas.extend(etlines) with BeamerSubSection(name='All Bins Performance'): BeamerTableSlide( title='All Bins Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) for et in xrange(net): l1 = ['', ''] l2 = ['', ''] sideline = '{|c|c|ccc|}' l1.extend(['', slideAnex[et][0]['etstr'], '']) l2.extend([r'Pd(\%)', r'SP(\%)', r'PF(\%)']) etlines = [] for eta in xrange(neta): if unbinned: la = [r'\hline', 'CrossValidation'] else: la = [ r'\hline' + slideAnex[0][eta]['etastr'], 'CrossValidation' ] lb = [r'Neuron', 'Reference'] lc = [r'', 'bestNetBench'] neuron = slideAnex[et][eta]['neuron'] prfs = slideAnex[et][eta]['perfs'] lc[0] = '%d' % neuron refBench = prfs[0] perfBench = prfs[1] bestNetBench = prfs[2] la.extend(perfBench) lb.extend(refBench) lc.extend(bestNetBench) etlines.extend([la, lb, lc]) linhas = [] if not unbinned: linhas.append(l1) linhas.append(l2) linhas.extend(etlines) if unbinned: BeamerTableSlide( title='Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) else: with BeamerSubSection(name=slideAnex[et][0]['etstr']): BeamerTableSlide( title='Performance', linhas=linhas, sideline=sideline, caption='Efficiences', ) with BeamerSection(name='Figures'): if unbinned: neuron = slideAnex[0][0]['neuron'] graphSections = slideAnex[0][0]['graphSections'] figures = slideAnex[0][0]['figures'] for graph in graphSections: BeamerFigureSlide(title=graph + ' Neuron: ' + str(neuron), path=figures[graph]) else: for et in xrange(net): with BeamerSubSection(name=slideAnex[et][0]['etstr']): for eta in xrange(neta): neuron = slideAnex[et][eta]['neuron'] graphSections = slideAnex[et][eta][ 'graphSections'] figures = slideAnex[et][eta]['figures'] with BeamerSubSubSection( name=slideAnex[et][eta]['etastr'] + ', Neuron: {}'.format(neuron)): for graph in graphSections: BeamerFigureSlide(title=graph, path=figures[graph]) return
# from TuningTools.dataframe import RingerOperation args.operation = RingerOperation.tostring(args.operation) nnList_calo = {} nnList_track = {} ## Retrieving Calorimeter Networks logger.info("Retrieving Calorimeter Networks...") filelist = args.network_calo if isinstance(filelist, (str)): filelist = [filelist] elif isinstance(filelist, (tuple)): filelist = list(filelist) for et in MatlabLoopingBounds(args.et_bins): nnList_calo[et] = {} for eta in MatlabLoopingBounds(args.eta_bins): tmp = load(filelist.pop(0)) nnList_calo[et][eta] = {} for x in ['Pd', 'Pf', 'SP']: nnList_calo[et][eta][x] = {} hn = tmp['OperationPoint_%s_%s' % (opName, x)]['infoTstBest']['neuron'] logger.debug("Reference %s: %i neurons in the hidden layer" % (x, hn)) for sort in tmp['infoPPChain'].keys(): # TODO: Add a progressbar to the loop nnList_calo[et][eta][x][sort] = tmp[ 'OperationPoint_%s_%s' % (opName, x)]['config_%1.3i' % (hn)][sort]['infoOpBest']['discriminator'] ## Retrieving Tracking Networks
rawdict['_passed'] = mat[rowIdx][columnIdx] def changeTotal(key, mat): for lkey in d[key].item(): for rowIdx, array1 in enumerate(d[key].item()[lkey]): for columnIdx, rawdict in enumerate(array1): rawdict['_count'] = 100. changePassed(key, mat) changeTotal(key, mat) from RingerCore import load, save a = load( 'mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.medium.npz' ) d = dict(a) changeOp(d, 'signal_efficiencies', mergeEffTable(veryloose20160701)) changeOp(d, 'background_efficiencies', pfrefs_vloose) save( d, 'mc15_13TeV.361106.423300.sgn.trigegprobes.bkg.vetotruth.trig.l2calo.eg.std.grid.veryloose', protocol='savez_compressed') d = dict(a) changeOp(d, 'signal_efficiencies', mergeEffTable(loose20160701)) changeOp(d, 'background_efficiencies', pfrefs_loose) save( d,