<commit_before># Author: <NAME>, <NAME>, <NAME>
# License: BSD

from numpy.testing import assert_array_equal
from pytest import raises as assert_raises

import numpy as np

from scipy.optimize import linear_sum_assignment
from scipy.sparse.sputils import matrix


def test_linear_sum_assignment():
    for sign in [-1, 1]:
        for cost_matrix, expected_cost in [
            # Square
            ([[400, 150, 400],
              [400, 450, 600],
              [300, 225, 300]],
             [150, 400, 300]
             ),

            # Rectangular variant
            ([[400, 150, 400, 1],
              [400, 450, 600, 2],
              [300, 225, 300, 3]],
             [150, 2, 300]),

            # Square
            ([[10, 10, 8],
              [9, 8, 1],
              [9, 7, 4]],
             [10, 1, 7]),

            # Rectangular variant
            ([[10, 10, 8, 11],
              [9, 8, 1, 1],
              [9, 7, 4, 10]],
             [10, 1, 4]),

            # n == 2, m == 0 matrix
            ([[], []],
             []),

            # Square with positive infinities
            ([[10, float("inf"), float("inf")],
              [float("inf"), float("inf"), 1],
              [float("inf"), 7, float("inf")]],
             [10, 1, 7]),
        ]:

            maximize = sign == -1
            cost_matrix = sign * np.array(cost_matrix)
            expected_cost = sign * np.array(expected_cost)

            row_ind, col_ind = linear_sum_assignment(cost_matrix,
                                                     maximize=maximize)
            assert_array_equal(row_ind, np.sort(row_ind))
            assert_array_equal(expected_cost, cost_matrix[row_ind, col_ind])

            cost_matrix = cost_matrix.T
            row_ind, col_ind = linear_sum_assignment(cost_matrix,
                                                     maximize=maximize)
            assert_array_equal(row_ind, np.sort(row_ind))
            assert_array_equal(np.sort(expected_cost),
                               np.sort(cost_matrix[row_ind, col_ind]))


def test_linear_sum_assignment_input_validation():

    assert_raises(ValueError, linear_sum_assignment, [1, 2, 3])

    C = [[1, 2, 3], [4, 5, 6]]
    assert_array_equal(linear_sum_assignment(C),
                       linear_sum_assignment(np.asarray(C)))
    assert_array_equal(linear_sum_assignment(C),
                       linear_sum_assignment(matrix(C)))

    C = np.identity(3)
    assert_array_equal(linear_sum_assignment(C.astype(np.bool)),
                       linear_sum_assignment(C))
    assert_raises(ValueError, linear_sum_assignment, C.astype(str))

    for sign in [-1, 1]:
        maximize = sign == -1
        C = np.identity(3)
        C[0][0] = np.nan
        assert_raises(ValueError, linear_sum_assignment, C)

        C = np.identity(3)
        C[1][1] = -np.inf
        assert_raises(ValueError, linear_sum_assignment, C)

        C = np.identity(3)
        C[:, 0] = np.inf
        assert_raises(ValueError, linear_sum_assignment, C)
<commit_msg>fixed tests<commit_after># Author: <NAME>, <NAME>, <NAME>
# License: BSD

from numpy.testing import assert_array_equal
from pytest import raises as assert_raises

import numpy as np

from scipy.optimize import linear_sum_assignment
from scipy.sparse.sputils import matrix


def test_linear_sum_assignment():
    for sign in [-1, 1]:
        for cost_matrix, expected_cost in [
            # Square
            ([[400, 150, 400],
              [400, 450, 600],
              [300, 225, 300]],
             [150, 400, 300]
             ),

            # Rectangular variant
            ([[400, 150, 400, 1],
              [400, 450, 600, 2],
              [300, 225, 300, 3]],
             [150, 2, 300]),

            # Square
            ([[10, 10, 8],
              [9, 8, 1],
              [9, 7, 4]],
             [10, 1, 7]),

            # Rectangular variant
            ([[10, 10, 8, 11],
              [9, 8, 1, 1],
              [9, 7, 4, 10]],
             [10, 1, 4]),

            # n == 2, m == 0 matrix
            ([[], []],
             []),

            # Square with positive infinities
            ([[10, float("inf"), float("inf")],
              [float("inf"), float("inf"), 1],
              [float("inf"), 7, float("inf")]],
             [10, 1, 7]),
        ]:

            maximize = sign == -1
            cost_matrix = sign * np.array(cost_matrix)
            expected_cost = sign * np.array(expected_cost)

            row_ind, col_ind = linear_sum_assignment(cost_matrix,
                                                     maximize=maximize)
            assert_array_equal(row_ind, np.sort(row_ind))
            assert_array_equal(expected_cost, cost_matrix[row_ind, col_ind])

            cost_matrix = cost_matrix.T
            row_ind, col_ind = linear_sum_assignment(cost_matrix,
                                                     maximize=maximize)
            assert_array_equal(row_ind, np.sort(row_ind))
            assert_array_equal(np.sort(expected_cost),
                               np.sort(cost_matrix[row_ind, col_ind]))


def test_linear_sum_assignment_input_validation():

    assert_raises(ValueError, linear_sum_assignment, [1, 2, 3])

    C = [[1, 2, 3], [4, 5, 6]]
    assert_array_equal(linear_sum_assignment(C),
                       linear_sum_assignment(np.asarray(C)))
    assert_array_equal(linear_sum_assignment(C),
                       linear_sum_assignment(matrix(C)))

    I = np.identity(3)
    assert_array_equal(linear_sum_assignment(I.astype(np.bool)),
                       linear_sum_assignment(I))
    assert_raises(ValueError, linear_sum_assignment, I.astype(str))

    I[0][0] = np.nan
    assert_raises(ValueError, linear_sum_assignment, I)

    I = np.identity(3)
    I[1][1] = -np.inf
    assert_raises(ValueError, linear_sum_assignment, I)

    I = np.identity(3)
    I[:, 0] = np.inf
    assert_raises(ValueError, linear_sum_assignment, I)

{'fraction_non_alphanumeric': 0.12491616364855801, 'fraction_numerical': 0.046948356807511735, 'mean_word_length': 1.7437902483900645, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5555257', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1825, 'n_words': 401}