|
| 1 | +# Copyright The GeoML Team |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import pytest |
| 15 | +from inspect import getmembers, isfunction |
| 16 | +from collections import namedtuple |
| 17 | +import string |
| 18 | + |
| 19 | +import torch |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +import matplotlib.pyplot as plt |
| 23 | +import torchplot as tp |
| 24 | + |
| 25 | +Inputs = namedtuple("case", ["x", "y"]) |
| 26 | + |
| 27 | +_cpu_cases = [Inputs(x=torch.randn(100,), y=torch.randn(100,)), |
| 28 | + Inputs(x=torch.randn(100, requires_grad=True), y=torch.randn(100,requires_grad=True)), |
| 29 | + # test that list/numpy arrays still works |
| 30 | + Inputs(x=[1,2,3,4], y=[1,2,3,4]), |
| 31 | + Inputs(x=np.random.randn(100,), y=np.random.randn(100,)), |
| 32 | + # test that we can mix |
| 33 | + Inputs(x=torch.randn(100,), y=torch.randn(100, requires_grad=True)), |
| 34 | + Inputs(x=np.random.randn(100,), y=torch.randn(100, requires_grad=True)), |
| 35 | + Inputs(x=torch.randn(5,), y=[1,2,3,4,5]), |
| 36 | + ] |
| 37 | + |
| 38 | +_gpu_cases = [Inputs(x=torch.randn(100, device='cuda'), y=torch.randn(100, device='cuda')), |
| 39 | + Inputs(x=torch.randn(100,requires_grad=True, device='cuda'), y=torch.randn(100,requires_grad=True, device='cuda')), |
| 40 | + ] |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +_members_to_check = [name for name, member in getmembers(plt) |
| 45 | + if isfunction(member) and not name.startswith('_')] |
| 46 | + |
| 47 | + |
| 48 | +def string_compare(text1, text2): |
| 49 | + if text1 is None and text2 is None: |
| 50 | + return True |
| 51 | + remove = string.punctuation + string.whitespace |
| 52 | + return text1.translate(str.maketrans(dict.fromkeys(remove))) == text2.translate(str.maketrans(dict.fromkeys(remove))) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("member", _members_to_check) |
| 56 | +def test_members(member): |
| 57 | + """ test that all members have been copied """ |
| 58 | + assert member in dir(plt) |
| 59 | + assert member in dir(tp) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize('test_case', _cpu_cases) |
| 63 | +def test_cpu(test_case): |
| 64 | + """ test that it works on cpu """ |
| 65 | + assert tp.plot(test_case.x, test_case.y, '.') |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason='test requires cuda') |
| 69 | +@pytest.mark.parametrize('test_case', _gpu_cases) |
| 70 | +def test_gpu(test_case): |
| 71 | + """ test that it works on gpu """ |
| 72 | + assert tp.plot(test_case.x, test_case.y, '.') |
| 73 | + |
0 commit comments