Skip to content

Commit

Permalink
create inputs.py
Browse files Browse the repository at this point in the history
  • Loading branch information
matsumotosan committed Sep 2, 2023
1 parent fb84f75 commit 1958069
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 62 deletions.
38 changes: 38 additions & 0 deletions tests/unittests/clustering/inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright The Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import namedtuple

import torch

from unittests import BATCH_SIZE, NUM_BATCHES
from unittests.helpers import seed_all

seed_all(42)


Input = namedtuple("Input", ["preds", "target"])
NUM_CLASSES = 10


_single_target_inputs1 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_single_target_inputs2 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_float_inputs = Input(preds=torch.rand((NUM_BATCHES, BATCH_SIZE)), target=torch.rand((NUM_BATCHES, BATCH_SIZE)))
23 changes: 2 additions & 21 deletions tests/unittests/clustering/test_mutual_info_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import namedtuple

import pytest
import torch
from sklearn.metrics import mutual_info_score as sklearn_mutual_info_score
from torchmetrics.clustering.mutual_info_score import MutualInfoScore
from torchmetrics.functional.clustering.mutual_info_score import mutual_info_score

from unittests import BATCH_SIZE, NUM_BATCHES
from unittests import BATCH_SIZE, NUM_CLASSES
from unittests.clustering.inputs import _float_inputs, _single_target_inputs1, _single_target_inputs2
from unittests.helpers import seed_all
from unittests.helpers.testers import MetricTester

seed_all(42)

Input = namedtuple("Input", ["preds", "target"])
NUM_CLASSES = 10

_single_target_inputs1 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_single_target_inputs2 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_float_inputs = Input(
preds=torch.rand((NUM_BATCHES, BATCH_SIZE)),
target=torch.rand((NUM_BATCHES, BATCH_SIZE)),
)


@pytest.mark.parametrize(
"preds, target",
Expand Down
22 changes: 2 additions & 20 deletions tests/unittests/clustering/test_normalized_mutual_info_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import namedtuple
from functools import partial

import pytest
Expand All @@ -20,30 +19,13 @@
from torchmetrics.clustering import NormalizedMutualInfoScore
from torchmetrics.functional.clustering import normalized_mutual_info_score

from unittests import BATCH_SIZE, NUM_BATCHES
from unittests import BATCH_SIZE, NUM_CLASSES
from unittests.clustering.inputs import _float_inputs, _single_target_inputs1, _single_target_inputs2
from unittests.helpers import seed_all
from unittests.helpers.testers import MetricTester

seed_all(42)

Input = namedtuple("Input", ["preds", "target"])
NUM_CLASSES = 10

_single_target_inputs1 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_single_target_inputs2 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_float_inputs = Input(
preds=torch.rand((NUM_BATCHES, BATCH_SIZE)),
target=torch.rand((NUM_BATCHES, BATCH_SIZE)),
)


@pytest.mark.parametrize(
"preds, target",
Expand Down
22 changes: 1 addition & 21 deletions tests/unittests/clustering/test_rand_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import namedtuple

import pytest
import torch
from sklearn.metrics import rand_score as sklearn_rand_score
from torchmetrics.clustering.rand_score import RandScore
from torchmetrics.functional.clustering.rand_score import rand_score

from unittests import BATCH_SIZE, NUM_BATCHES
from unittests.clustering.inputs import _float_inputs, _single_target_inputs1, _single_target_inputs2
from unittests.helpers import seed_all
from unittests.helpers.testers import MetricTester

seed_all(42)

Input = namedtuple("Input", ["preds", "target"])
NUM_CLASSES = 10

_single_target_inputs1 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_single_target_inputs2 = Input(
preds=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
target=torch.randint(high=NUM_CLASSES, size=(NUM_BATCHES, BATCH_SIZE)),
)

_float_inputs = Input(
preds=torch.rand((NUM_BATCHES, BATCH_SIZE)),
target=torch.rand((NUM_BATCHES, BATCH_SIZE)),
)


@pytest.mark.parametrize(
"preds, target",
Expand Down

0 comments on commit 1958069

Please sign in to comment.