Skip to content

Commit

Permalink
Fix variable get_full_y_title.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Jan 25, 2023
1 parent 1a2816b commit 31dcbc1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Just make sure to add new test cases and run them via:
python -m unittest tests
In general, tests should be run for Python 2.7, 3.6, 3.7, 3.8, 3.9 and 3.10.
In general, tests should be run for Python 2.7, 3.6 - 3.11.
To run tests in a docker container, do

.. code-block:: shell
Expand Down
49 changes: 33 additions & 16 deletions order/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,40 +511,57 @@ def bin_edges(self):
bin_width = self.bin_width
return [self.x_min + i * bin_width for i in range(self.n_bins + 1)]

def get_full_x_title(self, short=False, root=ROOT_DEFAULT):
def get_full_x_title(self, unit=None, short=False, root=ROOT_DEFAULT):
"""
Returns the full title (i.e. with unit string) of the x-axis. When *short* is *True*, the
short version is returned. When *root* is *True*, the title is converted to *proper* ROOT
latex.
Returns the full title (i.e. with unit string) of the x-axis. When *unit* is *None*, it
defaults to the :py:attr:`unit` if this instance. No unit is shown if it is one or it
evaluates to *False*.
When *short* is *True*, the short version is returned. When *root* is *True*, the title is
converted to *proper* ROOT latex.
"""
title = self.x_title_short if short else self.x_title

if self.unit not in (None, "1"):
title = self.unit_format.format(title=title, unit=self.unit)
# determine the unit
if unit is None:
unit = self.unit

# create the full title
if unit and unit not in ("1", 1):
title = self.unit_format.format(title=title, unit=unit)

return to_root_latex(title) if root else title

def get_full_y_title(self, bin_width=None, short=False, root=ROOT_DEFAULT):
def get_full_y_title(self, bin_width=None, unit=None, short=False, root=ROOT_DEFAULT):
"""
Returns the full title (i.e. with bin width and unit string) of the y-axis. When not *None*,
the value *bin_width* instead of the one evaluated from *binning* when even. When *short* is
*True*, the short version is returned. When *root* is *True*, the title is converted to
ROOT-style latex.
the value *bin_width* instead of the one evaluated from *binning* when even. When *unit* is
*None*, it defaults to the :py:attr:`unit` if this instance. No unit is shown if it is one
or it evaluates to *False*.
When *short* is *True*, the short version is returned. When *root* is *True*, the title is
converted to ROOT-style latex.
"""
title = self.y_title_short if short else self.y_title

# determine the bin width when not set
if bin_width is None and self.even_binning:
bin_width = round(self.bin_width, 2)

# determine the unit
if unit is None:
unit = self.unit

# add bin width and unit to the title
unit = []
if self.unit not in (None, "1"):
unit.append(str(self.unit))
parts = []
if unit and unit not in ("1", 1):
parts.append(str(unit))
if bin_width:
unit.insert(0, str(bin_width))
if unit:
title = self.unit_format.format(title=title, unit=" ".join(unit))
parts.insert(0, str(bin_width))

# create the full title
if parts:
title = self.unit_format.format(title=title, unit=" ".join(parts))

return to_root_latex(title) if root else title

Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CampaignTest(unittest.TestCase):
def test_constructor(self):
c = Campaign("2017", 1, ecm=13, bx=25)

self.assertEqual(c.ecm, 13.)
self.assertEqual(c.bx, 25.)
self.assertEqual(c.ecm, 13.0)
self.assertEqual(c.bx, 25.0)

with self.assertRaises(TypeError):
c.ecm = "foo"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def test_child_processes(self):

def test_attributes(self):
p = Process("ST", 6, xsecs={13: 5})
self.assertEqual(p.get_xsec(13).n, 5.)
self.assertEqual(p.get_xsec(13).n, 5.0)

p.set_xsec(13, 6)
self.assertEqual(p.get_xsec(13).n, 6.)
self.assertEqual(p.get_xsec(13).n, 6.0)

p.xsecs = {14: 7}
self.assertNotIn(13, p.xsecs)
self.assertEqual(p.get_xsec(14).n, 7.)
self.assertEqual(p.get_xsec(14).n, 7.0)

def test_copy(self):
p = Process("ttVV", 7, xsecs={13: 5}, color=(0.3, 0.4, 0.5), is_data=False, aux={1: 2})
Expand Down
31 changes: 19 additions & 12 deletions tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VariableTest(unittest.TestCase):
def make_var(self, name, **kwargs):
kwargs.setdefault("expression", "myBranchA * myBranchB")
kwargs.setdefault("selection", "myBranchC > 0")
kwargs.setdefault("binning", (20, 0., 10.))
kwargs.setdefault("binning", (20, 0.0, 10.0))
kwargs.setdefault("x_title", "p_{T}")
kwargs.setdefault("unit", "GeV")
kwargs.setdefault("null_value", -999)
Expand All @@ -27,7 +27,7 @@ def test_constructor(self):
self.assertEqual(v.name, "constructor_var")
self.assertEqual(v.expression, "myBranchA * myBranchB")
self.assertEqual(v.selection, "myBranchC > 0")
self.assertEqual(v.binning, (20, 0., 10.))
self.assertEqual(v.binning, (20, 0.0, 10.0))
self.assertEqual(v.get_full_title(), "constructor_var;p_{T} / GeV;Entries / 0.5 GeV")

def test_parsing(self):
Expand Down Expand Up @@ -56,23 +56,23 @@ def test_parsing(self):
with self.assertRaises(ValueError):
v.binning = [1]

v.binning = (10, 0., 1.)
self.assertEqual(v.binning, (10, 0., 1.))
v.binning = (10, 0.0, 1.0)
self.assertEqual(v.binning, (10, 0.0, 1.0))
self.assertTrue(v.even_binning)
self.assertEqual(len(v.bin_edges), 11)
with self.assertRaises(TypeError):
v.binning = {}
with self.assertRaises(ValueError):
v.binning = (10, 0.)
v.binning = (10, 0.0)

v.binning = (10., 0, 1)
v.binning = (10.0, 0, 1)
self.assertTrue(isinstance(v.n_bins, six.integer_types))
self.assertTrue(isinstance(v.x_min, float))
self.assertTrue(isinstance(v.x_max, float))

self.assertEqual(v.n_bins, 10)
self.assertEqual(v.x_min, 0.)
self.assertEqual(v.x_max, 1.)
self.assertEqual(v.x_min, 0.0)
self.assertEqual(v.x_max, 1.0)
self.assertEqual(v.bin_width, 0.1)
self.assertEqual(len(v.bin_edges), v.n_bins + 1)

Expand Down Expand Up @@ -147,14 +147,21 @@ def test_titles(self):
x_title_short=r"$\mu p_{T}$",
y_title="Entries",
y_title_short="N",
binning=(40, 0., 10.),
binning=(40, 0.0, 10.0),
)

self.assertEqual(v.get_full_x_title(), "Muon transverse momentum / GeV")
self.assertEqual(v.get_full_x_title(unit=None), "Muon transverse momentum / GeV")
self.assertEqual(v.get_full_x_title(unit=""), "Muon transverse momentum")
self.assertEqual(v.get_full_x_title(short=True), "$\\mu p_{T}$ / GeV")
self.assertEqual(v.get_full_x_title(short=True, root=True), "#mu p_{T} / GeV")

self.assertEqual(v.get_full_y_title(), "Entries / 0.25 GeV")
self.assertEqual(v.get_full_y_title(bin_width=""), "Entries / GeV")
self.assertEqual(v.get_full_y_title(bin_width=0.2), "Entries / 0.2 GeV")
self.assertEqual(v.get_full_y_title(unit=""), "Entries / 0.25")
self.assertEqual(v.get_full_y_title(unit="10^9eV"), "Entries / 0.25 10^9eV")
self.assertEqual(v.get_full_y_title(bin_width=0.2, unit=""), "Entries / 0.2")
self.assertEqual(v.get_full_y_title(short=True), "N / 0.25 GeV")
self.assertEqual(
v.get_full_title(),
Expand Down Expand Up @@ -188,19 +195,19 @@ def test_copy(self):
def test_mpl_data(self):
v = self.make_var(
name="mpl_hist",
binning=(40, 0., 10.),
binning=(40, 0.0, 10.0),
log_x=True,
)

data = v.get_mpl_hist_data()
self.assertEqual(data["bins"], 40)
self.assertEqual(data["range"], (0., 10.))
self.assertEqual(data["range"], (0.0, 10.0))
self.assertEqual(data["label"], "mpl_hist")
self.assertTrue(data["log"])

data = v.get_mpl_hist_data(update={"color": "red", "label": "bar"}, skip="log")
self.assertEqual(data["bins"], 40)
self.assertEqual(data["range"], (0., 10.))
self.assertEqual(data["range"], (0.0, 10.0))
self.assertEqual(data["label"], "bar")
self.assertEqual(data["color"], "red")
self.assertTrue("log" not in data)

0 comments on commit 31dcbc1

Please sign in to comment.