Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Stefano Franchellucci
Umami
Commits
64bebdcf
Commit
64bebdcf
authored
Jan 27, 2022
by
Manuel Guth
Committed by
Alexander Froch
Jan 27, 2022
Browse files
Adding save divide
parent
bb969756
Changes
4
Hide whitespace changes
Inline
Side-by-side
setup.cfg
View file @
64bebdcf
...
...
@@ -13,6 +13,9 @@ darglint-ignore-regex = *
[darglint]
ignore=DAR203
docstring_style=numpy
strictness=full
log_level=INFO
[pylint.FORMAT]
max-line-length = 88
...
...
umami/helper_tools/__init__.py
View file @
64bebdcf
# flake8: noqa
# pylint: skip-file
from
umami.helper_tools.histogram_tools
import
hist_ratio
,
hist_w_unc
,
s
tep
_divide
from
umami.helper_tools.histogram_tools
import
hist_ratio
,
hist_w_unc
,
s
ave
_divide
umami/helper_tools/histogram_tools.py
View file @
64bebdcf
...
...
@@ -42,7 +42,7 @@ def hist_w_unc(a, bins, normed: bool = True):
return
bin_edges
,
hist
,
unc
,
band
def
s
tep
_divide
(
nominator
,
denominator
,
default
=
1.0
):
def
s
ave
_divide
(
nominator
,
denominator
,
default
=
1.0
):
"""
Division using numpy divide function returning default value in cases where
denoinator is 0.
...
...
@@ -60,11 +60,19 @@ def step_divide(nominator, denominator, default=1.0):
-------
ratio: array_like
"""
if
isinstance
(
nominator
,
(
int
,
float
))
and
isinstance
(
denominator
,
(
int
,
float
)):
output_shape
=
1
else
:
try
:
output_shape
=
denominator
.
shape
except
AttributeError
:
output_shape
=
nominator
.
shape
ratio
=
np
.
divide
(
nominator
,
denominator
,
out
=
np
.
ones
(
nominator
.
shape
,
output_
shape
,
dtype
=
float
,
)
*
default
,
...
...
@@ -111,14 +119,14 @@ def hist_ratio(nominator, denominator, nominator_unc, denominator_unc):
raise
(
AssertionError
(
"Denominator and denominator_unc don't have the same legth"
)
)
step_ratio
=
s
tep
_divide
(
nominator
,
denominator
)
step_ratio
=
s
ave
_divide
(
nominator
,
denominator
)
# Add an extra bin in the beginning to have the same binning as the input
# Otherwise, the ratio will not be exactly above each other (due to step)
step_ratio
=
np
.
append
(
np
.
array
([
step_ratio
[
0
]]),
step_ratio
)
# Calculate rel uncertainties
nominator_rel_unc
=
s
tep
_divide
(
nominator_unc
,
nominator
,
default
=
0
)
denominator_rel_unc
=
s
tep
_divide
(
denominator_unc
,
denominator
,
default
=
0
)
nominator_rel_unc
=
s
ave
_divide
(
nominator_unc
,
nominator
,
default
=
0
)
denominator_rel_unc
=
s
ave
_divide
(
denominator_unc
,
denominator
,
default
=
0
)
# Calculate rel uncertainty
step_rel_unc
=
np
.
sqrt
(
nominator_rel_unc
**
2
+
denominator_rel_unc
**
2
)
...
...
umami/tests/unit/helper_tools/test_hisogram_tools.py
View file @
64bebdcf
...
...
@@ -2,7 +2,7 @@ import unittest
import
numpy
as
np
from
umami.helper_tools
import
hist_ratio
,
hist_w_unc
,
s
tep
_divide
from
umami.helper_tools
import
hist_ratio
,
hist_w_unc
,
s
ave
_divide
class
hist_w_unc_TestCase
(
unittest
.
TestCase
):
...
...
@@ -51,17 +51,25 @@ class hist_w_unc_TestCase(unittest.TestCase):
np
.
testing
.
assert_almost_equal
(
band
,
self
.
band
)
class
s
tep
_divide_TestCase
(
unittest
.
TestCase
):
class
s
ave
_divide_TestCase
(
unittest
.
TestCase
):
def
test_zero_case
(
self
):
steps
=
s
tep
_divide
(
np
.
zeros
(
2
),
np
.
zeros
(
2
))
steps
=
s
ave
_divide
(
np
.
zeros
(
2
),
np
.
zeros
(
2
))
np
.
testing
.
assert_equal
(
steps
,
np
.
ones
(
2
))
def
test_ones_case
(
self
):
steps
=
s
tep
_divide
(
np
.
ones
(
2
),
np
.
ones
(
2
))
steps
=
s
ave
_divide
(
np
.
ones
(
2
),
np
.
ones
(
2
))
np
.
testing
.
assert_equal
(
steps
,
np
.
ones
(
2
))
def
test_half_case
(
self
):
steps
=
step_divide
(
np
.
ones
(
2
),
2
*
np
.
ones
(
2
))
steps
=
save_divide
(
np
.
ones
(
2
),
2
*
np
.
ones
(
2
))
np
.
testing
.
assert_equal
(
steps
,
0.5
*
np
.
ones
(
2
))
def
test_denominator_float
(
self
):
steps
=
save_divide
(
np
.
ones
(
2
),
2
)
np
.
testing
.
assert_equal
(
steps
,
0.5
*
np
.
ones
(
2
))
def
test_nominator_float
(
self
):
steps
=
save_divide
(
1
,
np
.
ones
(
2
)
*
2
)
np
.
testing
.
assert_equal
(
steps
,
0.5
*
np
.
ones
(
2
))
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment