Skip to content

[BUG] Fix transiant test in python 3.13 #144

Description

@talgalili

output

============================= test session starts ==============================
platform linux -- Python 3.13.9, pytest-9.0.1, pluggy-1.6.0
rootdir: /home/runner/work/balance/balance
plugins: anyio-4.11.0
collected 297 items

tests/test_adjust_null.py .                                              [  0%]
tests/test_adjustment.py ..................F                             [  6%]
tests/test_balancedf.py ................................................ [ 22%]
....                                                                     [ 24%]
tests/test_cbps.py ....................                                  [ 30%]
tests/test_cli.py .................                                      [ 36%]
tests/test_datasets.py ..                                                [ 37%]
tests/test_ipw.py ..........                                             [ 40%]
tests/test_logging.py .                                                  [ 41%]
tests/test_poststratify.py ....                                          [ 42%]
tests/test_rake.py ................                                      [ 47%]
tests/test_sample.py ................................................... [ 64%]
..                                                                       [ 65%]
tests/test_stats_and_plots.py ...................                        [ 72%]
tests/test_testutil.py ........................                          [ 80%]
tests/test_util.py ...............................................       [ 95%]
tests/test_weighted_comparisons_plots.py ............                    [100%]

=================================== FAILURES ===================================
_______________________ TestAdjustment.test_trim_weights _______________________

self = <test_adjustment.TestAdjustment testMethod=test_trim_weights>

    def test_trim_weights(self):
        """
        Test weight trimming functionality including no trimming, percentile trimming,
        and mean ratio trimming scenarios.
    
        Validates that:
        - Weights are properly converted to float64 dtype
        - No trimming preserves original values
        - Percentile and mean ratio trimming work correctly
        - Error conditions are properly handled
        """
        # Test no trimming - verify dtype conversion to float64
        input_weights = pd.Series([0, 1, 2])
        expected_weights = pd.Series([0.0, 1.0, 2.0])
    
        result_weights = trim_weights(input_weights)
        pd.testing.assert_series_equal(result_weights, expected_weights)
        self.assertEqual(type(result_weights), pd.Series)
        self.assertEqual(result_weights.dtype, np.float64)
    
        # Test that no trimming parameters preserves original weights
        random.seed(42)
        random_weights = np.random.uniform(0, 1, 10000)
        untrimmed_result = trim_weights(
            random_weights,
            weight_trimming_percentile=None,
            weight_trimming_mean_ratio=None,
            keep_sum_of_weights=False,
        )
        self.assertEqual(untrimmed_result, random_weights)
    
        # Test error handling for invalid input types
        with self.assertRaisesRegex(
            TypeError, "weights must be np.array or pd.Series, are of type*"
        ):
            trim_weights("Strings don't get trimmed", weight_trimming_mean_ratio=1)
    
        # Test error when both trimming parameters are provided
        with self.assertRaisesRegex(ValueError, "Only one"):
            trim_weights(np.array([0, 1, 2]), 1, 1)
    
        # Test weight_trimming_mean_ratio functionality
        random.seed(42)
        original_weights = np.random.uniform(0, 1, 10000)
        mean_ratio_result = trim_weights(original_weights, weight_trimming_mean_ratio=1)
    
        # Mean should be preserved and ratio constraints should be applied
        self.assertAlmostEqual(
            np.mean(original_weights), np.mean(mean_ratio_result), delta=EPSILON
        )
        self.assertAlmostEqual(
            np.mean(original_weights) / np.min(original_weights),
            np.max(mean_ratio_result) / np.min(mean_ratio_result),
            delta=EPSILON,
        )
    
        # Test weight_trimming_percentile functionality
        random.seed(42)
        test_weights = np.random.uniform(0, 1, 10000)
    
        # Test upper percentile trimming
        upper_trimmed = trim_weights(
            test_weights,
            weight_trimming_percentile=(0, 0.11),
            keep_sum_of_weights=False,
        )
>       self.assertTrue(max(upper_trimmed) < 0.9)
E       AssertionError: np.False_ is not true

tests/test_adjustment.py:117: AssertionError
=========================== short test summary info ============================
FAILED tests/test_adjustment.py::TestAdjustment::test_trim_weights - AssertionError: np.False_ is not true
================== 1 failed, 296 passed in 228.16s (0:03:48) ===================
Error: Process completed with exit code 1.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions