def gp_stack(version, energies, inclMed, inclFits):
inclMed = (inclMed and version != '<STR_LIT>')<EOL>inclFits = (inclFits and version == '<STR_LIT>')<EOL>cocktail_style = '<STR_LIT>' % default_colors[<NUM_LIT:8>]<EOL>medium_style = '<STR_LIT>' % default_colors[<NUM_LIT:4>]<EOL>if inclMed:<EOL><INDENT>medium_style = '<STR_LIT>' % default_colors[<NUM_LIT:4>]<EOL><DEDENT>shift = {<EOL>'<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT:0.1>, '<STR_LIT>': <NUM_LIT><EOL>} if (<EOL>version != '<STR_LIT>' and version != '<STR_LIT>' and version != '<STR_LIT>'<EOL>) else {<EOL>'<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT:1.>, '<STR_LIT>': <NUM_LIT><EOL>}<EOL>if version == '<STR_LIT>':<EOL><INDENT>shift = { '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT> }<EOL><DEDENT>inDir, outDir = getWorkDirs()<EOL>inDir = os.path.join(inDir, version)<EOL>data, cocktail, medium = OrderedDict(), OrderedDict(), OrderedDict()<EOL>dataIMRfit, cocktailIMRfit, dataTvsS = OrderedDict(), OrderedDict(), OrderedDict()<EOL>cocktailContribs, medOnly, qgpOnly = OrderedDict(), OrderedDict(), OrderedDict()<EOL>rangeIMR = [<NUM_LIT>, <NUM_LIT>]<EOL>nPtsMC = <NUM_LIT:1000> <EOL>cRanges = list(map(Decimal, ['<STR_LIT>', '<STR_LIT>']))<EOL>pi0yld = {}<EOL>for filename in os.listdir(inDir):<EOL><INDENT>file_url = os.path.join(inDir, filename)<EOL>if os.path.isdir(file_url):<EOL><INDENT>for fn in os.listdir(file_url):<EOL><INDENT>energy = re.compile('<STR_LIT>').search(fn).group()<EOL>particle = re.sub('<STR_LIT>' % energy, '<STR_LIT>', fn)<EOL>if energy != '<STR_LIT>': continue<EOL>if version == '<STR_LIT>' and energy == '<STR_LIT>' and particle == '<STR_LIT>': continue<EOL>cocktailContribs[particle] = np.loadtxt(open(<EOL>os.path.join(file_url, fn), '<STR_LIT:rb>'<EOL>))<EOL>if particle == '<STR_LIT>' or particle == '<STR_LIT>' or particle == '<STR_LIT>':<EOL><INDENT>thr = <NUM_LIT> if particle == '<STR_LIT>' else <NUM_LIT><EOL>if particle == '<STR_LIT>': thr = <NUM_LIT><EOL>mask = cocktailContribs[particle][:,<NUM_LIT:0>] < thr<EOL>cocktailContribs[particle] = cocktailContribs[particle][mask]<EOL><DEDENT>cocktailContribs[particle][:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] *= shift[energy]<EOL>cocktailContribs[particle][:,<NUM_LIT:2>:] = <NUM_LIT:0><EOL><DEDENT>continue<EOL><DEDENT>energy = re.compile('<STR_LIT>').search(filename).group()<EOL>data_type = re.sub('<STR_LIT>' % energy, '<STR_LIT>', filename)<EOL>data_import = np.array([[-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>]]) if (<EOL>energies is not None and energy not in energies<EOL>) else np.loadtxt(open(file_url, '<STR_LIT:rb>'))<EOL>if (<EOL>inclFits and energies is None and linmod is not None and<EOL>(data_type == '<STR_LIT:data>' or data_type == '<STR_LIT>')<EOL>):<EOL><INDENT>mask = (data_import[:,<NUM_LIT:0>] > rangeIMR[<NUM_LIT:0>]) & (data_import[:,<NUM_LIT:0>] < rangeIMR[<NUM_LIT:1>])<EOL>dataIMR = data_import[mask]<EOL>mIMR, bIMR = linmod.fitErrxy(<EOL>dataIMR[:,<NUM_LIT:0>], np.log10(dataIMR[:,<NUM_LIT:1>]), dataIMR[:,<NUM_LIT:2>],<EOL>np.log10(dataIMR[:,<NUM_LIT:3>]) <EOL>)<EOL>slope_par = -<NUM_LIT:1.>/mIMR<EOL>logging.info('<STR_LIT>' % (filename, mIMR, bIMR, slope_par))<EOL>slope_par_err = <NUM_LIT:0.><EOL>if data_type == '<STR_LIT:data>':<EOL><INDENT>rndm = OrderedDict((ax,[]) for ax in ['<STR_LIT:x>','<STR_LIT:y>'])<EOL>rndm_jump = <NUM_LIT:0><EOL>dataMC = OrderedDict((n, []) for n in range(nPtsMC))<EOL>for i,dp in enumerate(dataIMR): <EOL><INDENT>logging.info(('<STR_LIT>' % i).format(<EOL>ufloat(dp[<NUM_LIT:0>], dp[<NUM_LIT:2>]), ufloat(dp[<NUM_LIT:1>], dp[<NUM_LIT:3>]) <EOL>))<EOL>for ax in rndm: <EOL><INDENT>rndm[ax].append(random.Random())<EOL>if ax == '<STR_LIT:y>' and i == <NUM_LIT:0>: <EOL><INDENT>rndm[ax][i].setstate(rndm['<STR_LIT:x>'][i].getstate())<EOL>rndm[ax][i].jumpahead(nPtsMC)<EOL><DEDENT>if i > <NUM_LIT:0>: <EOL><INDENT>rndm[ax][i].setstate(rndm[ax][i-<NUM_LIT:1>].getstate())<EOL>rndm[ax][i].jumpahead(nPtsMC)<EOL><DEDENT><DEDENT>for n in range(nPtsMC): <EOL><INDENT>dataMC[n].append([<EOL>rndm['<STR_LIT:x>'][i].uniform(dp[<NUM_LIT:0>] - dp[<NUM_LIT:2>], dp[<NUM_LIT:0>] + dp[<NUM_LIT:2>]),<EOL>truncated_gaus(rndm['<STR_LIT:y>'][i], dp[<NUM_LIT:1>], dp[<NUM_LIT:3>])<EOL>])<EOL><DEDENT><DEDENT>mIMRMC = []<EOL>for dp in dataMC.values():<EOL><INDENT>dp = np.array(dp)<EOL>mIMRMC.append([<EOL>linmod.fitBasic(dp[:,<NUM_LIT:0>], np.log10(dp[:,<NUM_LIT:1>]))[<NUM_LIT:0>][<NUM_LIT:0>],<EOL>linmod.stdData(x=dp[:,<NUM_LIT:0>], y=np.log10(dp[:,<NUM_LIT:1>]))<EOL>])<EOL><DEDENT>mIMRMC = np.array(mIMRMC)<EOL>weights = <NUM_LIT:1.>/mIMRMC[:,<NUM_LIT:1>]<EOL>mIMRMC_avg = np.average(mIMRMC[:,<NUM_LIT:0>], weights=weights)<EOL>mIMRMC_var = np.average((mIMRMC[:,<NUM_LIT:0>]-mIMRMC_avg)**<NUM_LIT:2>, weights=weights)<EOL>umIMR = ufloat(mIMRMC_avg, math.sqrt(mIMRMC_var))<EOL>slope_par_err = abs(math.sqrt(mIMRMC_var)/mIMRMC_avg * slope_par)<EOL>logging.info(('<STR_LIT>' % (mIMR, slope_par_err)).format(umIMR))<EOL><DEDENT>IMRfit = np.array([ [x, math.pow(<NUM_LIT>,mIMR*x+bIMR), <NUM_LIT:0.>, <NUM_LIT:0.>, <NUM_LIT:0.>] for x in rangeIMR ])<EOL>IMRfit[:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] *= shift[energy]<EOL>if data_type == '<STR_LIT:data>': dataIMRfit[energy] = IMRfit<EOL>else: cocktailIMRfit[energy] = IMRfit<EOL>dp = [ float(getEnergy4Key(energy)), slope_par, <NUM_LIT:0.>, slope_par_err, <NUM_LIT:0.> ]<EOL>if data_type in dataTvsS: dataTvsS[data_type].append(dp)<EOL>else: dataTvsS[data_type] = [ dp ]<EOL><DEDENT>if data_type != '<STR_LIT>':<EOL><INDENT>pi0yld['<STR_LIT:_>'.join([energy,data_type])] = getMassRangesSums(<EOL>np.copy(data_import), customRanges = cRanges , singleRange = True<EOL>)<EOL><DEDENT>data_import[:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] *= shift[energy]<EOL>if fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>data[energy] = data_import<EOL><DEDENT>elif fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>data_import[:,(<NUM_LIT:2>,<NUM_LIT:3>)] = <NUM_LIT:0> <EOL>if inclMed:<EOL><INDENT>for di in data_import:<EOL><INDENT>if (energy != '<STR_LIT>' and di[<NUM_LIT:0>] < <NUM_LIT>) or (energy == '<STR_LIT>' and di[<NUM_LIT:0>] < <NUM_LIT>):<EOL><INDENT>di[<NUM_LIT:4>] = <NUM_LIT:0> <EOL><DEDENT><DEDENT><DEDENT>if energy == '<STR_LIT>' and (version == '<STR_LIT>' or version == '<STR_LIT>'):<EOL><INDENT>cocktail[energy] = data_import[data_import[:,<NUM_LIT:0>] < <NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>cocktail[energy] = data_import<EOL><DEDENT><DEDENT>elif inclMed and fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>data_import[:,(<NUM_LIT:2>,<NUM_LIT:3>)] = <NUM_LIT:0> <EOL>medium[energy] = data_import if energy != '<STR_LIT>' else data_import[data_import[:,<NUM_LIT:0>] < <NUM_LIT>]<EOL><DEDENT>elif inclMed and fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>data_import[:,<NUM_LIT:2>:] = <NUM_LIT:0> <EOL>if fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>qgpOnly[energy] = data_import if version != '<STR_LIT>' else data_import[data_import[:,<NUM_LIT:0>] < <NUM_LIT>]<EOL><DEDENT>if fnmatch(filename, '<STR_LIT>'):<EOL><INDENT>medOnly[energy] = data_import if version != '<STR_LIT>' else data_import[data_import[:,<NUM_LIT:0>] < <NUM_LIT>]<EOL><DEDENT><DEDENT><DEDENT>scale = {}<EOL>for e in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>' ]:<EOL><INDENT>if version == '<STR_LIT>' and e == '<STR_LIT>': continue<EOL>a, b = pi0yld[e+'<STR_LIT>'], pi0yld[e+'<STR_LIT>']<EOL>z = a/b<EOL>scale[e] = ufloat(z.nominal_value, z.std_dev*<NUM_LIT:2>)<EOL><DEDENT>print(scale)<EOL>print(['<STR_LIT>'.format(k, <NUM_LIT:1.>/v) for k,v in scale.items()])<EOL>if version == '<STR_LIT>' or version == '<STR_LIT>': <EOL><INDENT>for k in cocktail:<EOL><INDENT>if (version == '<STR_LIT>' and k != '<STR_LIT>') or version == '<STR_LIT>':<EOL><INDENT>cocktail[k][:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] /= scale[k].nominal_value<EOL><DEDENT><DEDENT>for k in medium:<EOL><INDENT>if (version == '<STR_LIT>' and k != '<STR_LIT>') or version == '<STR_LIT>':<EOL><INDENT>medium[k][:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] /= scale[k].nominal_value<EOL><DEDENT><DEDENT>for k in medOnly:<EOL><INDENT>if (version == '<STR_LIT>' and k != '<STR_LIT>') or version == '<STR_LIT>':<EOL><INDENT>medOnly[k][:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] /= scale[k].nominal_value<EOL><DEDENT><DEDENT>for k in qgpOnly:<EOL><INDENT>if (version == '<STR_LIT>' and k != '<STR_LIT>') or version == '<STR_LIT>':<EOL><INDENT>qgpOnly[k][:,(<NUM_LIT:1>,<NUM_LIT:3>,<NUM_LIT:4>)] /= scale[k].nominal_value<EOL><DEDENT><DEDENT><DEDENT>dataOrdered = OrderedDict(<EOL>('<STR_LIT:U+0020>'.join([<EOL>getEnergy4Key(k), '<STR_LIT>', '<STR_LIT>' % shift[k],<EOL>'<STR_LIT>' if version == '<STR_LIT>' and k == '<STR_LIT>' else '<STR_LIT>',<EOL>'<STR_LIT>' if version == '<STR_LIT>' and k == '<STR_LIT>' else '<STR_LIT>'<EOL>]), data[k]) for k in sorted(data, key=int)<EOL>)<EOL>cocktailOrdered = OrderedDict((k, cocktail[k]) for k in sorted(cocktail, key=int))<EOL>mediumOrdered = OrderedDict((k, medium[k]) for k in sorted(medium, key=int))<EOL>dataIMRfitOrdered = OrderedDict((k, dataIMRfit[k]) for k in sorted(dataIMRfit, key=int))<EOL>cocktailIMRfitOrdered = OrderedDict((k, cocktailIMRfit[k]) for k in sorted(cocktailIMRfit, key=int))<EOL>nSetsData, nSetsCocktail, nSetsMedium = len(dataOrdered), len(cocktail), len(medium)<EOL>nSetsDataIMRfit, nSetsCocktailIMRfit = len(dataIMRfitOrdered), len(cocktailIMRfitOrdered)<EOL>nSetsCocktailContribs, nSetsModelOnly = len(cocktailContribs), len(qgpOnly) + len(medOnly)<EOL>yr_low = <NUM_LIT> if version == '<STR_LIT>' else <NUM_LIT><EOL>if version == '<STR_LIT>': yr_low = <NUM_LIT><EOL>if version == '<STR_LIT>': yr_low = <NUM_LIT><EOL>if version == '<STR_LIT>': yr_low = <NUM_LIT><EOL>cocktailContribsProps = ([<EOL>'<STR_LIT>' % default_colors[-i-<NUM_LIT:2>]<EOL>for i in range(nSetsCocktailContribs-<NUM_LIT:1>)<EOL>] + [ '<STR_LIT>' % default_colors[<NUM_LIT:0>] ])if nSetsCocktailContribs > <NUM_LIT:0> else []<EOL>make_plot(<EOL>data = list(cocktailContribs.values())<EOL>+ list(cocktailOrdered.values()) + ([ pseudo_point ] if inclMed else [])<EOL>+ list(qgpOnly.values()) + list(medOnly.values())<EOL>+ list(mediumOrdered.values()) + [ pseudo_point ] + list(dataOrdered.values())<EOL>+ list(dataIMRfitOrdered.values()) + ([ pseudo_point ] if inclFits else [])<EOL>+ list(cocktailIMRfitOrdered.values()) + ([ pseudo_point ] if inclFits else []),<EOL>properties = cocktailContribsProps + [ cocktail_style ] * (nSetsCocktail+<NUM_LIT:1>) + [<EOL>'<STR_LIT>' % default_colors[-i-<NUM_LIT:16>] for i in range(nSetsModelOnly)<EOL>] * (nSetsModelOnly/<NUM_LIT:2>)<EOL>+ [ medium_style ] * (nSetsMedium+bool(nSetsMedium)) + [<EOL>'<STR_LIT>' % default_colors[i]<EOL>for i in range(nSetsData)<EOL>] + [ dataIMRfit_style ] * (nSetsDataIMRfit+<NUM_LIT:1>)<EOL>+ [ cocktailIMRfit_style ] * (nSetsCocktailIMRfit+<NUM_LIT:1>),<EOL>titles = [ particleLabel4Key(k) for k in list(cocktailContribs.keys()) ]<EOL>+ ['<STR_LIT>'] * nSetsCocktail + ['<STR_LIT>']<EOL>+ ['<STR_LIT>', '<STR_LIT>' if version != '<STR_LIT>' else '<STR_LIT>'] * bool(nSetsModelOnly)<EOL>+ ['<STR_LIT>'] * nSetsMedium + [<EOL>'<STR_LIT>' if version != '<STR_LIT>' else '<STR_LIT>'<EOL>] * bool(nSetsMedium) + list(dataOrdered.keys())<EOL>+ ['<STR_LIT>'] * nSetsDataIMRfit + ['<STR_LIT>'] * inclFits<EOL>+ ['<STR_LIT>'] * nSetsCocktailIMRfit + ['<STR_LIT>'] * inclFits,<EOL>name = os.path.join(outDir, '<STR_LIT>' % (<EOL>version, '<STR_LIT>' if inclMed else '<STR_LIT>', '<STR_LIT>' if inclFits else '<STR_LIT>',<EOL>'<STR_LIT:_>' + '<STR_LIT:->'.join(energies) if energies is not None else '<STR_LIT>'<EOL>)),<EOL>ylabel = '<STR_LIT>',<EOL>xlabel = '<STR_LIT>',<EOL>ylog = True, xr = [<NUM_LIT:0>, <NUM_LIT>], yr = [yr_low, <NUM_LIT>], arrow_offset = <NUM_LIT>,<EOL>bmargin = <NUM_LIT>, rmargin = <NUM_LIT>, tmargin = <NUM_LIT>,<EOL>key = [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>' if version == '<STR_LIT>' else '<STR_LIT>'<EOL>] if version != '<STR_LIT>' else [ '<STR_LIT>', '<STR_LIT>' ],<EOL>labels = {<EOL>'<STR_LIT>': [<NUM_LIT>,<NUM_LIT>,False] if version != '<STR_LIT>' else [<NUM_LIT>,<NUM_LIT>,False],<EOL>'<STR_LIT>': [<NUM_LIT>,<NUM_LIT>,False] if version != '<STR_LIT>' else [<NUM_LIT>,<NUM_LIT>,False],<EOL>} if version == '<STR_LIT>' or version == '<STR_LIT>'or version == '<STR_LIT>' else {},<EOL>size = '<STR_LIT>' if version != '<STR_LIT>' else '<STR_LIT>',<EOL>)<EOL>if inclFits:<EOL><INDENT>for t in dataTvsS: dataTvsS[t].sort(key=lambda x: x[<NUM_LIT:0>])<EOL>make_plot(<EOL>data = [ np.array(dataTvsS['<STR_LIT>']), np.array(dataTvsS['<STR_LIT:data>']) ],<EOL>properties = [<EOL>'<STR_LIT>' % default_colors[<NUM_LIT:0>],<EOL>'<STR_LIT>' % default_colors[<NUM_LIT:0>]<EOL>],<EOL>titles = [ '<STR_LIT>', '<STR_LIT:data>' ],<EOL>name = os.path.join(outDir, '<STR_LIT>' % version),<EOL>xlabel = '<STR_LIT>',<EOL>ylabel = '<STR_LIT>',<EOL>lmargin = <NUM_LIT:0.1>, xlog = True, yr = [<NUM_LIT:0>,<NUM_LIT>], gpcalls = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>]<EOL>)<EOL><DEDENT>return '<STR_LIT>'<EOL>
example for a plot w/ stacked graphs using QM12 data (see gp_panel)

    * how to omit keys from the legend
    * manually add legend entries
    * automatically plot arrows for error bars larger than data point value

    .. image:: pics/stackQM12.png
       :width: 550px

    :param version: plot version / input subdir name
    :type version: str