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

[MRG] get_backend compatibility with None entries #525

Merged
merged 3 commits into from
Sep 20, 2023
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
8 changes: 8 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Releases

## 0.9.2dev

#### New features
+ Tweaked `get_backend` to ignore `None` inputs (PR # 525)

#### Closed issues


## 0.9.1
*August 2023*

Expand Down
6 changes: 5 additions & 1 deletion ot/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,15 @@ def _check_args_backend(backend, args):
def get_backend(*args):
"""Returns the proper backend for a list of input arrays

Accepts None entries in the arguments, and ignores them

Also raises TypeError if all arrays are not from the same backend
"""
args = [arg for arg in args if arg is not None] # exclude None entries

# check that some arrays given
if not len(args) > 0:
raise ValueError(" The function takes at least one parameter")
raise ValueError(" The function takes at least one (non-None) parameter")

for backend in _BACKENDS:
if _check_args_backend(backend, args):
Expand Down
9 changes: 9 additions & 0 deletions test/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Author: Remi Flamary <[email protected]>
# Nicolas Courty <[email protected]>
#
#
# License: MIT License

import ot
Expand Down Expand Up @@ -753,3 +754,11 @@ def fun(a, b, d):
[dl_dw, dl_db] = tape.gradient(manipulated_loss, [w, b])
assert nx.allclose(dl_dw, w)
assert nx.allclose(dl_db, b)


def test_get_backend_none():
a, b = np.zeros((2, 3)), None
nx = get_backend(a, b)
assert str(nx) == 'numpy'
with pytest.raises(ValueError):
get_backend(None, None)
Loading