def create_top_stacked_axes(self,heights=(1.)): """ Create several axes for subplot on top of each other Args: heights (iterable): relative height e.g. heights = [.2,.1,.6] will give axes using this amount of space """ assert sum(heights) <= 1., "heights must be in relative heights" cfg = get_config_item("canvas") left = cfg["leftpadding"] right = cfg["rightpadding"] bot = cfg["bottompadding"] top = cfg["toppadding"] width = 1. - left - right height = 1. - top - bot heights = [height*h for h in heights] heights.reverse() Logger.debug("Using heights {0}".format(heights.__repr__())) abs_bot = 0 +bot axes = [p.axes([left,abs_bot,width,heights[0]])] restheights = heights[1:] abs_bot = bot + heights[0] for h in restheights: theaxes = p.axes([left,abs_bot,width,h]) p.setp(theaxes.get_xticklabels(), visible=False) axes.append(theaxes) abs_bot += h self.axes = axes
def indicate_cut(self,ax,arrow=True): """ If cuts are given, indicate them by lines Args: ax (pylab.axes): axes to draw on """ vmin,vmax = ax.get_ylim() hmin,hmax = ax.get_xlim() for cut in self.cuts: for name,(operator,value) in cut: if name != self.dataname: continue Logger.debug('Found cut! {0} on {1}'.format(name,value)) width = vmax/50. ax.vlines(value,ymin=vmin,ymax=vmax,linestyle=':') length = (hmax - hmin)*0.1 shape = 'left' inversed = False if operator in ('>','>='): shape = 'right' inversed = True if arrow: ax = create_arrow(ax,value,vmax*0.1, -1., 0, length, width= width,shape=shape,log=True) #ax.add_patch(arr) if not inversed: ax.axvspan(value, hmax, facecolor=self.color_palette["prohibited"], alpha=0.5) else: ax.axvspan(hmin, value, facecolor=self.color_palette["prohibited"], alpha=0.5)
def load_vardefs(self,module): """ Load the variable definitions from a module Args: module (python module): Needs to contain variable definitions """ all_vars = inspect.getmembers(module) all_vars = [x[1] for x in all_vars if isinstance(x[1],variables.AbstractBaseVariable)] for v in all_vars: if v.name in self.vardict: Logger.debug("Variable {} already defined,skipping!".format(v.name)) continue self.add_variable(v)
def read_variables(self,variable_defs,names=None): """ Read out the variable for all categories Args: variable_defs: A python module containing variable definitions Keyword Args: names (str): Readout only these variables if given Returns: """ for cat in self.categories: Logger.debug("Reading variables for {}".format(cat)) cat.load_vardefs(variable_defs) cat.read_variables(names=names)
def __call__(self,energy,ptype,\ zenith=None,mapping=False, weight=None): """ Args: energy: primary MC energy ptype: primary MC particle type zenith: cos (?) zenith Keyword Args: mapping: do a mapping to pdg weight: e.g. interactionprobabilityweights for nue Returns: numpy.ndarray: weights """ # FIXME: mapping argument should go away if mapping: pmap = {14:ParticleType.PPlus, 402:ParticleType.He4Nucleus, 1407:ParticleType.N14Nucleus, 2713:ParticleType.Al27Nucleus, 5626:ParticleType.Fe56Nucleus} ptype = map(lambda x : pmap[x], ptype ) # FIXME: This is too ugly and not general can_use_zenith = False if hasattr(self.flux,"__call__"): if hasattr(self.flux.__call__,"im_func"): args = inspect.getargs(self.flux.__call__.im_func.func_code) if len(args.args) == 4: # account for self can_use_zenith = True else: can_use_zenith = True # method wrapper created by NewNuflux else: args = inspect.getargs(self.flux.func_code) if len(args.args) == 3: can_use_zenith = True if (zenith is not None) and can_use_zenith: Logger.debug("Using zenith!") return self.flux(energy,ptype,zenith)/self.gen(energy,particle_type=ptype,cos_theta=zenith) else: Logger.debug("Not using zenith!") return self.flux(energy,ptype)/self.gen(energy,particle_type=ptype,cos_theta=zenith)
def harvest(filenames,definitions,**kwargs): """ Extract the variable data from the provided files Args: filenames (list): the files to extract from currently supported: {0} Keyword Args: transformation (func): will be applied to the read out data Returns: pd.Series or pd.DataFrame """.format(REGISTERED_FILEEXTENSIONS.__repr__()) data = pd.Series() for filename in filenames: filetype = f.strip_all_endings(filename)[1] assert filetype in REGISTERED_FILEEXTENSIONS, "Filetype {} not known!".format(filetype) assert os.path.exists(filename), "File {} does not exist!".format(filetype) Logger.debug("Attempting to harvest {1} file {0}".format(filename,filetype)) if filetype == ".h5" and not isinstance(filename, tables.table.Table): # store = pd.HDFStore(filename) hdftable = tables.openFile(filename) else: hdftable = filename tmpdata = pd.Series() for definition in definitions: if filetype == ".h5": try: # data = store.select_column(*definition) tmpdata = hdftable.getNode("/" + definition[0]).col(definition[1]) tmpdata = pd.Series(tmpdata, dtype=n.float64) Logger.debug("Found {} entries in table for {}{}".format(len(tmpdata),definition[0],definition[1])) break except tables.NoSuchNodeError: Logger.debug("Can not find definition {0} in {1}! ".format(definition, filename)) continue elif filetype == ".root": tmpdata = rn.root2rec(filename, *definition) tmpdata = pd.Series(data) if filetype == ".h5": hdftable.close() #tmpdata = harvest_single_file(filename, filetype,definitions) # self.data = self.data.append(data.map(self.transform)) # concat should be much faster if "transformation" in kwargs: transform = kwargs['transformation'] data = pd.concat([data, tmpdata.map(transform)]) else: data = pd.concat([data, tmpdata]) del tmpdata return data
def GetModelWeight(model,datasets,\ mc_datasets=None,\ mc_p_en=None,\ mc_p_ty=None,\ mc_p_ze=None,\ mc_p_we=1.,\ mc_p_ts=1.,\ mc_p_gw=1.,\ **model_kwargs): """ Compute weights using a predefined model Args: model (func): Used to calculate the target flux datasets (dict): Get the generation pdf for these datasets from the db dict needs to be dataset_id -> nfiles Keyword Args: mc_p_en (array-like): primary energy mc_p_ty (array-like): primary particle type mc_p_ze (array-like): primary particle cos(zenith) mc_p_we (array-like): weight for mc primary, e.g. some interaction probability Returns (array-like): Weights """ if model_kwargs: flux = model(**model_kwargs) else: flux = model() # FIXME: There is a factor of 5000 not accounted # for -> 1e4 is for the conversion of factor = 1. gen = GetGenerator(datasets) if map(int,gen.spectra.keys())[0] in NUTYPES: Logger.debug('Patching weights') factor = 5000 weight = Weight(gen,flux) return factor*mc_p_we*weight(mc_p_en,mc_p_ty,zenith=mc_p_ze)
like string like 'k' for matplotlib """ def __getitem__(self,item): if item in self: return self.get(item) else: return item seaborn_loaded = False try: import seaborn.apionly as sb seaborn_loaded = True Logger.debug("Seaborn found!") except ImportError: Logger.warning("Seaborn not found! Using predefined color palette") def get_color_palette(name="dark"): """ Load a color pallete, use seaborn if available """ if not seaborn_loaded: color_palette = ColorDict() # stolen from seaborn color-palette color_palette[0] = (0.2980392156862745, 0.4470588235294118, 0.6901960784313725) color_palette[5] = (0.8, 0.7254901960784313, 0.4549019607843137)#(0.3921568627450 9803, 0.7098039215686275, 0.803921568627451) color_palette["k"] = "k" color_palette[1] = (0.3333333333333333, 0.6588235294117647, 0.40784313725490196) color_palette[2] = (0.7686274509803922, 0.3058823529411765, 0.3215686274509804)
""" Provides canvases for multi axes plots """ import os.path import pylab as p from pyevsel.plotting import get_config_item from pyevsel.utils.logger import Logger try: from IPython.core.display import Image except ImportError: Logger.debug("Can not import IPython!") Image = lambda x : x # golden cut values # CW = current width of my thesis (adjust CW = 5.78851 S = 1.681 ########################################## class YStackedCanvas(object): """ A canvas for plotting multiple axes """ def __init__(self,axeslayout=(.2,.2,.5),figsize=(CW,CW*S)): """ Axes indices go from bottom to top """
def read_variables(self,names=None): """ Harvest the variables in self.vardict Keyword Args: names (list): havest only these variables """ if names is None: names = self.vardict.keys() compound_variables = [] #harvest them later executor = fut.ProcessPoolExecutor(max_workers=MAX_CORES) future_to_varname = {} # first read out variables, # then compound variables # so make sure they are in the # right order simple_vars = [] for varname in names: try: if isinstance(self.vardict[varname],variables.CompoundVariable): compound_variables.append(varname) continue elif isinstance(self.vardict[varname],variables.VariableList): compound_variables.append(varname) continue else: simple_vars.append(varname) except KeyError: Logger.warning("Cannot find {} in variables!".format(varname)) continue for varname in simple_vars: # FIXME: Make it an option to not use # multi cpu readout! #self.vardict[varname].data = variables.harvest(self.files,self.vardict[varname].definitions) future_to_varname[executor.submit(variables.harvest,self.files,self.vardict[varname].definitions)] = varname #for future in tqdm.tqdm(fut.as_completed(future_to_varname),desc="Reading {0} variables".format(self.name), leave=True): progbar = False try: import pyprind n_it = len(future_to_varname.keys()) bar = pyprind.ProgBar(n_it,monitor=False,bar_char='#',title=self.name) progbar = True except ImportError: pass exc_caught = """""" for future in fut.as_completed(future_to_varname): varname = future_to_varname[future] Logger.debug("Reading {} finished".format(varname)) try: data = future.result() Logger.debug("Found {} entries ...".format(len(data))) data = self.vardict[varname].transform(data) except Exception as exc: exc_caught += "Reading {} for {} generated an exception: {}\n".format(varname,self.name, exc) data = pd.Series([]) self.vardict[varname].data = data self.vardict[varname].declare_harvested() if progbar: bar.update() for varname in compound_variables: #FIXME check if this causes a memory leak self.vardict[varname].rewire_variables(self.vardict) self.vardict[varname].harvest() if exc_caught: Logger.warning("During the variable readout some exceptions occured!\n" + exc_caught) self._is_harvested = True