Skip to content

Commit

Permalink
Merge pull request #26 from FAST-HEP/BK_newfeatures_200128
Browse files Browse the repository at this point in the history
Fix errors, add features

Fixed: 
 - Bugs in the way overflow bins were handled and step-lines were drawn by padding. Impacted error bars as well as produced weird plotting artefacts.
Added:
- Extend unit tests
- Variable interpolation within the config files and using variables which can be passed from the command-line
- Y-limits based on plot-margins: pass a float with a percent sign after to limit configs
- Control over the display of under and overflow bins from the config file
- Ability to give specific colours for individual bands in the plot
- Option to control how errors are calculated: sqrt of sumw2 or sumw / sqrt(n
  • Loading branch information
benkrikler authored Feb 26, 2020
2 parents fd8fdff + d909f91 commit d44c166
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 102 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
### Changed

## [0.4.0] - 2020-02-26
- Many changes from PR #26 [@benkrikler](github.com/benkrikler)

### Fixed
- Bugs in the way overflow bins were handled and step-lines were drawn by padding. Impacted error bars as well as produced weird plotting artefacts.

### Added
- Extend unit tests
- Variable interpolation within the config files and using variables which can be passed from the command-line
- Y-limits based on plot-margins: pass a float with a percent sign after to limit configs
- Control over the display of under and overflow bins from the config file
- Ability to give specific colours for individual bands in the plot
- Option to control how errors are calculated: sqrt of sumw2 or sumw / sqrt(n)

## [0.3.0] - 2019-11-1
- Many changes from PR #13 [@benkrikler](github.com/benkrikler)
### Added
Expand Down
38 changes: 31 additions & 7 deletions fast_plotter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Turn them tables into plots
"""
import os
import six
import logging
import matplotlib
matplotlib.use('Agg')
Expand Down Expand Up @@ -41,6 +42,11 @@ def arg_parser(args=None):
help="Scale the MC yields by this lumi")
parser.add_argument("-y", "--yscale", default="log", choices=["log", "linear"],
help="Use this scale for the y-axis")

def split_equals(arg):
return arg.split("=")
parser.add_argument("-v", "--variable", dest="variables", action="append", default=[], type=split_equals,
help="Define a variable to expand in the config file")
parser.add_argument("--halt-errors", dest="continue_errors", default=True, action="store_false",
help="Stop at the first time an error occurs")
return parser
Expand All @@ -64,13 +70,28 @@ def main(args=None):

def process_cfg(cfg_file, args):
import yaml
from argparse import Namespace
from string import Template
with open(cfg_file, "r") as infile:
cfg = yaml.load(infile)
cfg = yaml.safe_load(infile)
# Only way to neatly allow cmd-line args to override config and handle
# defaults seems to be:
parser = arg_parser()
parser.set_defaults(**cfg)
args = parser.parse_args()
if args.variables:

def recursive_replace(value, replacements):
if isinstance(value, (tuple, list)):
return type(value)([recursive_replace(v, replacements) for v in value])
if isinstance(value, dict):
return {k: recursive_replace(v, replacements) for k, v in value.items()}
if isinstance(value, six.string_types):
return Template(value).safe_substitute(replacements)
return value

replacements = dict(args.variables)
args = Namespace(**recursive_replace(vars(args), replacements))

return args

Expand All @@ -94,7 +115,9 @@ def process_one_file(infile, args):
regex=args.data,
level=args.dataset_col)
for col in df_filtered.columns:
df_filtered[col][data_rows] = df["n"][data_rows]
if col == "n":
continue
df_filtered.loc[data_rows, col] = df["n"][data_rows]
df_filtered.columns = [
n.replace(weight + ":", "") for n in df_filtered.columns]
if hasattr(args, "value_replacements"):
Expand All @@ -121,11 +144,12 @@ def dress_main_plots(plots, annotations=[], yscale=None, ylabel=None, legend={},
main_ax.grid(True)
main_ax.set_axisbelow(True)
for axis, lims in limits.items():
lims = map(float, lims)
if axis.lower() == "x":
main_ax.set_xlim(*lims)
if axis.lower() == "y":
main_ax.set_ylim(*lims)
if isinstance(lims, (tuple, list)):
lims = map(float, lims)
if axis.lower() in "xy":
getattr(main_ax, "set_%slim" % axis)(*lims)
elif lims.endswith("%"):
main_ax.margins(**{axis: float(lims[:-1])})


def save_plots(infile, weight, plots, outdir, extensions):
Expand Down
Loading

0 comments on commit d44c166

Please sign in to comment.