Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Pre-commits
Browse files Browse the repository at this point in the history
  • Loading branch information
OpheliaMiralles committed Dec 16, 2024
1 parent 09bf216 commit ba61dc4
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions tests/preprocessing/test_preprocessor_remapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def input_remapper():
statistics = {}
name_to_index = {"x": 0, "y": 1, "z": 2, "q": 3, "d": 4, "other": 5}
data_indices = IndexCollection(config=config, name_to_index=name_to_index)
return Remapper(
config=config.data.remapper, data_indices=data_indices, statistics=statistics
)
return Remapper(config=config.data.remapper, data_indices=data_indices, statistics=statistics)


@pytest.fixture()
Expand All @@ -62,9 +60,7 @@ def input_remapper_1d():
statistics = {}
name_to_index = {"x": 0, "y": 1, "d": 2}
data_indices = IndexCollection(config=config, name_to_index=name_to_index)
return Remapper(
config=config.data.remapper, data_indices=data_indices, statistics=statistics
)
return Remapper(config=config.data.remapper, data_indices=data_indices, statistics=statistics)


@pytest.fixture()
Expand Down Expand Up @@ -92,28 +88,20 @@ def input_imputer():
}
name_to_index = {"x": 0, "y": 1, "z": 2, "q": 3, "d": 4, "other": 5}
data_indices = IndexCollection(config=config, name_to_index=name_to_index)
return InputImputer(
config=config.data.imputer, data_indices=data_indices, statistics=statistics
)
return InputImputer(config=config.data.imputer, data_indices=data_indices, statistics=statistics)


def test_remap_not_inplace(input_remapper) -> None:
x = torch.Tensor(
[[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]
)
x = torch.Tensor([[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]])
input_remapper(x, in_place=False)
assert torch.allclose(
x,
torch.Tensor(
[[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]
),
torch.Tensor([[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]),
)


def test_remap(input_remapper) -> None:
x = torch.Tensor(
[[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]
)
x = torch.Tensor([[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]])
expected_output = torch.Tensor(
[
[1.0, 2.0, 3.0, 4.0, 5.0, -0.8660254, 0.5],
Expand All @@ -130,31 +118,21 @@ def test_inverse_transform(input_remapper) -> None:
[6.0, 7.0, 8.0, 9.0, 10.0, -0.93358043, -0.35836795],
]
)
expected_output = torch.Tensor(
[[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]
)
expected_output = torch.Tensor([[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]])
assert torch.allclose(input_remapper.inverse_transform(x), expected_output)


def test_remap_inverse_transform(input_remapper) -> None:
x = torch.Tensor(
[[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]]
)
x = torch.Tensor([[1.0, 2.0, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, 201.0, 10.0]])
assert torch.allclose(
input_remapper.inverse_transform(
input_remapper.transform(x, in_place=False), in_place=False
),
input_remapper.inverse_transform(input_remapper.transform(x, in_place=False), in_place=False),
x,
)


def test_transform_loss_mask(input_imputer, input_remapper) -> None:
x = torch.Tensor(
[[1.0, np.nan, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, np.nan, 10.0]]
)
expected_output = torch.Tensor(
[[1.0, 0.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.0, 0.0]]
)
x = torch.Tensor([[1.0, np.nan, 3.0, 4.0, 150.0, 5.0], [6.0, 7.0, 8.0, 9.0, np.nan, 10.0]])
expected_output = torch.Tensor([[1.0, 0.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 0.0, 0.0]])
input_imputer.transform(x)
input_remapper.transform(x)
loss_mask_training = input_imputer.loss_mask_training
Expand All @@ -164,9 +142,7 @@ def test_transform_loss_mask(input_imputer, input_remapper) -> None:

def test_remap_log1p(input_remapper_1d) -> None:
x = torch.Tensor([[1.0, 2.0, 150.0], [6.0, 7.0, 201.0]])
expected_output = torch.Tensor(
[[1.0, 2.0, np.log1p(150.0)], [6.0, 7.0, np.log1p(201.0)]]
)
expected_output = torch.Tensor([[1.0, 2.0, np.log1p(150.0)], [6.0, 7.0, np.log1p(201.0)]])
assert torch.allclose(input_remapper_1d.transform(x), expected_output)


Expand Down

0 comments on commit ba61dc4

Please sign in to comment.