Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code review suggestions #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/normalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The normalization strategy to apply before fusion can be defined through the `no
| **Normalization Strategies** | **Alias** |
|------------------------------------------------|-------------------|
| [Min-Max Norm][min-max-norm] | min-max |
| [Min-Max-Inverted Norm][min-max-norm-inverted] | min-max-inverted |
| [Max Norm][max-norm] | max |
| [Sum Norm][sum-norm] | sum |
| [ZMUV Norm][zmuv-norm] | zmuv |
Expand All @@ -22,9 +23,9 @@ $$
\operatorname{MinMaxNorm(s)}=\frac{s - s_{min}}{s_{max} - s_{min}}
$$

Min-Max Norm accepts an optional boolean parameter `invert`, which, when set to true,
Min-Max Norm scales the scores (s) of a result list between 0 and 1,
setting the maximum score ($s_{max}$) to 0 and the minimum score ($s_{min}$) to 1.
## Min-Max Inverted Norm
---
Min-Max Norm scales the scores (s) of a result list between 0 and 1, scaling to 1 the minimum score ($s_{min}$) and 0 the maximum score ($s_{max}$).

$$
\operatorname{MinMaxNorm(s)}=\frac{s_{max} - s}{s_{max} - s_{min}}
Expand Down
4 changes: 3 additions & 1 deletion ranx/normalization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def norm_switch(method: str = "min-max"):
elif method == "max":
return max_norm
elif method in {"min_max", "min-max"}:
return min_max_norm
return lambda run: min_max_norm(run, invert=False)
elif method in {"min_max_inverted", "min_max-inverted"}:
return lambda run: min_max_norm(run, invert=True)
elif method == "rank":
return rank_norm
elif method == "sum":
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/ranx/normalization/min_max_norm_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from ranx import Run
from ranx.normalization import min_max_norm
from ranx.normalization import min_max_norm, norm_switch


# FIXTURES =====================================================================
Expand Down Expand Up @@ -43,11 +43,9 @@ def test_min_max_norm(run):
assert norm_run["q2"]["d2"] == (2 - 1) / (2 - 1)


def test_min_max_norm_with_invert(run):
@pytest.mark.parametrize("norm_name", ["min_max_inverted", "min_max-inverted"])
MochiXu marked this conversation as resolved.
Show resolved Hide resolved
def test_min_max_norm_with_invert(run, norm_name):
run_copy = run.run.copy()

norm_run = min_max_norm(run, True)

assert run.run == run_copy

assert len(run.run) == 2
Expand All @@ -60,11 +58,13 @@ def test_min_max_norm_with_invert(run):
assert run.run["q2"]["d2"] == 2
assert run.size == 2

norm_run = norm_switch(norm_name)(run)

assert len(norm_run) == 2
assert len(norm_run["q1"]) == 3
assert len(norm_run["q2"]) == 2
assert norm_run["q1"]["d1"] == (3 - 1) / (3 - 1)
assert norm_run["q1"]["d2"] == (3 - 2) / (3 - 1)
assert norm_run["q1"]["d3"] == (3 - 3) / (3 - 1)
assert norm_run["q2"]["d1"] == (3 - 1) / (2 - 1)
assert norm_run["q2"]["d2"] == (3 - 2) / (2 - 1)
assert norm_run["q2"]["d1"] == (2 - 1) / (2 - 1)
assert norm_run["q2"]["d2"] == (2 - 2) / (2 - 1)