Skip to content

Commit 21e45e5

Browse files
authored
Merge pull request #33 from FAST-HEP/BK_add_unstack_stage_postproc
Add stage to postproc to unstack weights
2 parents a641a67 + 134d9ca commit 21e45e5

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.6.3] - 2020-04-29
8+
### Added
9+
- Add GenericPandas and UnstackWeights stages, PR #33 [@benkrikler](https://github.com/benkrikler)
10+
711
## [0.6.2] - 2020-04-21
812
### Fixed
913
- Fix `split` function in postproc module to work with numbers, PR #32 [@benkrikler](github.com/benkrikler)

fast_plotter/postproc/functions.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,12 @@ def stack_weights(df, drop_n_col=False):
267267
logger.info("Stacking weights")
268268
if drop_n_col:
269269
df.drop("n", inplace=True, axis="columns")
270+
else:
271+
df.set_index("n", append=True, inplace=True)
270272
df.columns = pd.MultiIndex.from_tuples([c.split(":") for c in df.columns], names=["systematic", ""])
271273
out = df.stack(0, dropna=False)
274+
if not drop_n_col:
275+
out.reset_index(level="n", inplace=True)
272276
return out
273277

274278

@@ -279,15 +283,32 @@ def to_datacard_inputs(df, select_data, rename_syst_vars=False):
279283
logger.info("Converting to datacard inputs")
280284
if rename_syst_vars:
281285
df.columns = [n.replace("_up:", "_Up:").replace("_down:", "_Down:") for n in df.columns]
282-
df.set_index("n", append=True, inplace=True)
283286
df = stack_weights(df)
284-
df.reset_index(level="n", inplace=True)
285287
data_mask = df.eval(select_data)
286288
df["content"] = df.n
287289
df["content"][~data_mask] = df.sumw
288290
df["error"] = df.content / np.sqrt(df.n)
289291

290-
df.drop(["n", "sumw", "sumw2"], inplace=True, axis="columns")
292+
293+
def generic_pandas(df, func, *args, **kwargs):
294+
"""
295+
Apply generic pandas function to each input
296+
"""
297+
logger.info("Apply generic pandas function")
298+
return getattr(df, func)(*args, **kwargs)
299+
300+
301+
def unstack_weights(df, weight_name="systematic", includes_counts=True):
302+
"""
303+
The inverse to stack_weights
304+
"""
305+
logger.info("Unstacking systematics")
306+
if includes_counts:
307+
df = df.set_index("n", append=True)
308+
df = df.unstack(weight_name)
309+
df.columns = ["{1}:{0}".format(*c) if c[1] else c[0] for c in df.columns]
310+
if includes_counts:
311+
df = df.reset_index(level="n")
291312
return df
292313

293314

fast_plotter/postproc/stages.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ class NormaliseGroup(BaseManipulator):
131131
func = "normalise_group"
132132

133133

134+
class GenericPandas(BaseManipulator):
135+
cardinality = "one-to-one"
136+
func = "generic_pandas"
137+
138+
139+
class UnstackWeights(BaseManipulator):
140+
cardinality = "one-to-one"
141+
func = "unstack_weights"
142+
143+
134144
class OpenMany(BaseManipulator):
135145
cardinality = "none-to-many"
136146
func = "open_many"

fast_plotter/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ def split_version(version):
1212
return tuple(result)
1313

1414

15-
__version__ = '0.6.2'
15+
__version__ = '0.6.3'
1616
version_info = split_version(__version__) # noqa

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.6.2
2+
current_version = 0.6.3
33
commit = True
44
tag = False
55

0 commit comments

Comments
 (0)