Skip to content

Commit

Permalink
Include tests in pylint target
Browse files Browse the repository at this point in the history
  • Loading branch information
takuseno committed May 15, 2023
1 parent 2f2b52d commit 1cae35f
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion scripts/lint
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
mypy d3rlpy reproductions tests

# code-format check
pylint d3rlpy reproductions
pylint d3rlpy reproductions tests
2 changes: 1 addition & 1 deletion tests/algos/qlearning/algo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def update_tester(
) -> None:
# make mini-batch
transitions = []
for i in range(algo.config.batch_size):
for _ in range(algo.config.batch_size):
if len(observation_shape) == 3:
observation = np.random.randint(
256, size=observation_shape, dtype=np.uint8
Expand Down
4 changes: 2 additions & 2 deletions tests/algos/qlearning/test_explorers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_constant_epsilon_greedy(
action = np.random.randint(action_size, size=(1,))
if action != explorer.sample(algo, ref_x, 0):
break
elif i == 9:
if i == 9:
assert False


Expand Down Expand Up @@ -82,7 +82,7 @@ def test_linear_decay_epsilon_greedy(
action = np.random.randint(action_size)
if action != explorer.sample(algo, ref_x, 0):
break
elif i == 9:
if i == 9:
assert False


Expand Down
1 change: 1 addition & 0 deletions tests/base_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=unidiomatic-typecheck
import io
import os
from typing import Sequence
Expand Down
1 change: 1 addition & 0 deletions tests/metrics/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(
observation_shape: Sequence[int],
episode_length: int,
):
self.t = 0
self.episode = 0
self.episode_length = episode_length
self.observations = observations
Expand Down
1 change: 1 addition & 0 deletions tests/models/test_encoders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=protected-access
from typing import Sequence

import pytest
Expand Down
8 changes: 4 additions & 4 deletions tests/models/torch/model_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
from typing import Any, Optional, Sequence
from typing import Any, Sequence

import numpy as np
import torch
Expand All @@ -12,7 +12,7 @@ def check_parameter_updates(
model: torch.nn.Module, inputs: Any = None, output: Any = None
) -> None:
model.train()
params_before = copy.deepcopy([p for p in model.parameters()])
params_before = copy.deepcopy(list(model.parameters()))
optim = SGD(model.parameters(), lr=10.0)
if output is None:
if hasattr(model, "compute_error"):
Expand All @@ -30,7 +30,7 @@ def check_parameter_updates(
for before, after in zip(params_before, model.parameters()):
assert not torch.allclose(
before, after
), "tensor with shape of {} is not updated.".format(after.shape)
), f"tensor with shape of {after.shape} is not updated."


def ref_huber_loss(a: np.ndarray, b: np.ndarray) -> float:
Expand All @@ -50,7 +50,7 @@ def ref_quantile_huber_loss(
huber_diff = np.zeros_like(abs_diff)
huber_diff[abs_diff < 1.0] = 0.5 * l2_diff[abs_diff < 1.0]
huber_diff[abs_diff >= 1.0] = abs_diff[abs_diff >= 1.0] - 0.5
huber_diff = huber_diff.reshape(-1, n_quantiles, n_quantiles)
huber_diff = huber_diff.reshape((-1, n_quantiles, n_quantiles))
delta = np.array((b - a) < 0.0, dtype=np.float32)
element_wise_loss = np.abs(taus - delta) * huber_diff
return element_wise_loss.sum(axis=2).mean(axis=1)
Expand Down
1 change: 1 addition & 0 deletions tests/models/torch/q_functions/test_qr_q_function.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=protected-access
import numpy as np
import pytest
import torch
Expand Down
1 change: 1 addition & 0 deletions tests/models/torch/test_encoders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=protected-access
from typing import List, Optional, Sequence, Tuple

import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/models/torch/test_imitators.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def test_probablistic_regressor(

action = torch.rand(batch_size, action_size)
loss = imitator.compute_error(x, action)
assert loss.ndim == 0

y = imitator.sample_n(x, n)
assert y.shape == (batch_size, n, action_size)
Expand Down
10 changes: 6 additions & 4 deletions tests/preprocessing/test_action_scalers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
def test_min_max_action_scaler(action_size: int, batch_size: int) -> None:
actions = np.random.random((batch_size, action_size)).astype("f4")

max = actions.max(axis=0)
min = actions.min(axis=0)
maximum = actions.max(axis=0)
minimum = actions.min(axis=0)

scaler = MinMaxActionScaler(maximum=max, minimum=min)
scaler = MinMaxActionScaler(maximum=maximum, minimum=minimum)
assert scaler.built
assert scaler.get_type() == "min_max"

Expand All @@ -33,7 +33,9 @@ def test_min_max_action_scaler(action_size: int, batch_size: int) -> None:
# check transorm
x = torch.rand((batch_size, action_size))
y = scaler.transform(x)
ref_y = (x.numpy() - min.reshape((1, -1))) / (max - min).reshape((1, -1))
ref_y = (x.numpy() - minimum.reshape((1, -1))) / (
maximum - minimum
).reshape((1, -1))
assert np.allclose(y.numpy(), ref_y * 2.0 - 1.0)

# check reverse_transorm
Expand Down
10 changes: 6 additions & 4 deletions tests/preprocessing/test_observation_scalers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def test_min_max_observation_scaler(
shape = (batch_size, *observation_shape)
observations = np.random.random(shape).astype("f4")

max = observations.max(axis=0)
min = observations.min(axis=0)
scaler = MinMaxObservationScaler(maximum=max, minimum=min)
maximum = observations.max(axis=0)
minimum = observations.min(axis=0)
scaler = MinMaxObservationScaler(maximum=maximum, minimum=minimum)
assert scaler.built
assert scaler.get_type() == "min_max"

Expand All @@ -58,7 +58,9 @@ def test_min_max_observation_scaler(
# check transform
x = torch.rand((batch_size, *observation_shape))
y = scaler.transform(x)
ref_y = (x.numpy() - min.reshape((1, -1))) / (max - min).reshape((1, -1))
ref_y = (x.numpy() - minimum.reshape((1, -1))) / (
maximum - minimum
).reshape((1, -1))
assert np.allclose(y.numpy(), ref_y * 2.0 - 1.0, atol=1e-6)

# check reverse_transform
Expand Down
84 changes: 40 additions & 44 deletions tests/test_torch_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,20 @@ def test_map_location_with_cuda() -> None:

class DummyImpl:
def __init__(self) -> None:
self._fc1 = torch.nn.Linear(100, 100)
self._fc2 = torch.nn.Linear(100, 100)
self._optim = torch.optim.Adam(self._fc1.parameters())
self._device = "cpu:0"
self.fc1 = torch.nn.Linear(100, 100)
self.fc2 = torch.nn.Linear(100, 100)
self.optim = torch.optim.Adam(self.fc1.parameters())
self.device = "cpu:0"

@train_api
def train_api_func(self) -> None:
assert self._fc1.training
assert self._fc2.training
assert self.fc1.training
assert self.fc2.training

@eval_api
def eval_api_func(self) -> None:
assert not self._fc1.training
assert not self._fc2.training

@property
def device(self) -> str:
return self._device
assert not self.fc1.training
assert not self.fc2.training


def check_if_same_dict(a: Dict[str, Any], b: Dict[str, Any]) -> None:
Expand All @@ -145,71 +141,71 @@ def test_get_state_dict() -> None:

state_dict = get_state_dict(impl)

check_if_same_dict(state_dict["_fc1"], impl._fc1.state_dict())
check_if_same_dict(state_dict["_fc2"], impl._fc2.state_dict())
check_if_same_dict(state_dict["_optim"], impl._optim.state_dict())
check_if_same_dict(state_dict["fc1"], impl.fc1.state_dict())
check_if_same_dict(state_dict["fc2"], impl.fc2.state_dict())
check_if_same_dict(state_dict["optim"], impl.optim.state_dict())


def test_set_state_dict() -> None:
impl1 = DummyImpl()
impl2 = DummyImpl()

impl1._optim.step()
impl1.optim.step()

assert not (impl1._fc1.weight == impl2._fc1.weight).all()
assert not (impl1._fc1.bias == impl2._fc1.bias).all()
assert not (impl1._fc2.weight == impl2._fc2.weight).all()
assert not (impl1._fc2.bias == impl2._fc2.bias).all()
assert not (impl1.fc1.weight == impl2.fc1.weight).all()
assert not (impl1.fc1.bias == impl2.fc1.bias).all()
assert not (impl1.fc2.weight == impl2.fc2.weight).all()
assert not (impl1.fc2.bias == impl2.fc2.bias).all()

chkpt = get_state_dict(impl1)

set_state_dict(impl2, chkpt)

assert (impl1._fc1.weight == impl2._fc1.weight).all()
assert (impl1._fc1.bias == impl2._fc1.bias).all()
assert (impl1._fc2.weight == impl2._fc2.weight).all()
assert (impl1._fc2.bias == impl2._fc2.bias).all()
assert (impl1.fc1.weight == impl2.fc1.weight).all()
assert (impl1.fc1.bias == impl2.fc1.bias).all()
assert (impl1.fc2.weight == impl2.fc2.weight).all()
assert (impl1.fc2.bias == impl2.fc2.bias).all()


def test_reset_optimizer_states() -> None:
impl = DummyImpl()

# instantiate optimizer state
y = impl._fc1(torch.rand(100)).sum()
y = impl.fc1(torch.rand(100)).sum()
y.backward()
impl._optim.step()
impl.optim.step()

# check if state is not empty
state = copy.deepcopy(impl._optim.state)
state = copy.deepcopy(impl.optim.state)
assert state

reset_optimizer_states(impl)

# check if state is empty
reset_state = impl._optim.state
reset_state = impl.optim.state
assert not reset_state


def test_eval_mode() -> None:
impl = DummyImpl()
impl._fc1.train()
impl._fc2.train()
impl.fc1.train()
impl.fc2.train()

set_eval_mode(impl)

assert not impl._fc1.training
assert not impl._fc2.training
assert not impl.fc1.training
assert not impl.fc2.training


def test_train_mode() -> None:
impl = DummyImpl()
impl._fc1.eval()
impl._fc2.eval()
impl.fc1.eval()
impl.fc2.eval()

set_train_mode(impl)

assert impl._fc1.training
assert impl._fc2.training
assert impl.fc1.training
assert impl.fc2.training


@pytest.mark.skip(reason="no way to test this")
Expand All @@ -227,9 +223,9 @@ def test_freeze() -> None:

freeze(impl)

for p in impl._fc1.parameters():
for p in impl.fc1.parameters():
assert not p.requires_grad
for p in impl._fc2.parameters():
for p in impl.fc2.parameters():
assert not p.requires_grad


Expand All @@ -239,9 +235,9 @@ def test_unfreeze() -> None:
freeze(impl)
unfreeze(impl)

for p in impl._fc1.parameters():
for p in impl.fc1.parameters():
assert p.requires_grad
for p in impl._fc2.parameters():
for p in impl.fc2.parameters():
assert p.requires_grad


Expand Down Expand Up @@ -402,16 +398,16 @@ def test_torch_trajectory_mini_batch(

def test_train_api() -> None:
impl = DummyImpl()
impl._fc1.eval()
impl._fc2.eval()
impl.fc1.eval()
impl.fc2.eval()

impl.train_api_func()


def test_eval_api() -> None:
impl = DummyImpl()
impl._fc1.train()
impl._fc2.train()
impl.fc1.train()
impl.fc2.train()

impl.eval_api_func()

Expand Down

0 comments on commit 1cae35f

Please sign in to comment.