|
| 1 | +import numpy as np |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +# Add the lib directory to Python path so we can import matplotlib |
| 6 | +sys.path.insert(0, 'lib') |
| 7 | + |
| 8 | +try: |
| 9 | + import matplotlib.colors as mcolors |
| 10 | + print("✓ Successfully imported matplotlib.colors") |
| 11 | + |
| 12 | + def test_PowerNorm_scale_complete(): |
| 13 | + """Complete test for PowerNorm scale parameter""" |
| 14 | + print("\n=== Testing PowerNorm Scale Parameter ===") |
| 15 | + |
| 16 | + # Test basic functionality with scale parameter |
| 17 | + a = np.array([1, 2, 3, 4], dtype=float) |
| 18 | + print(f"Test data: {a}") |
| 19 | + |
| 20 | + # Test with scale=1.0 (should be same as no scaling) |
| 21 | + pnorm_no_scale = mcolors.PowerNorm(gamma=2, vmin=1, vmax=4, scale=1.0) |
| 22 | + pnorm_default = mcolors.PowerNorm(gamma=2, vmin=1, vmax=4) |
| 23 | + |
| 24 | + result_no_scale = pnorm_no_scale(a) |
| 25 | + result_default = pnorm_default(a) |
| 26 | + |
| 27 | + print(f"With scale=1.0: {result_no_scale}") |
| 28 | + print(f"Default (no scale): {result_default}") |
| 29 | + |
| 30 | + # Results should be identical when scale=1.0 |
| 31 | + if np.allclose(result_no_scale, result_default): |
| 32 | + print("✓ SUCCESS: scale=1.0 produces same results as default") |
| 33 | + else: |
| 34 | + print("✗ FAILED: scale=1.0 should produce same results as default") |
| 35 | + return False |
| 36 | + |
| 37 | + # Test with scale=2.0 (should produce different results) |
| 38 | + pnorm_scaled = mcolors.PowerNorm(gamma=2, vmin=1, vmax=4, scale=2.0) |
| 39 | + result_scaled = pnorm_scaled(a) |
| 40 | + |
| 41 | + print(f"With scale=2.0: {result_scaled}") |
| 42 | + |
| 43 | + # Results should be different when scaling is applied |
| 44 | + if not np.allclose(result_scaled, result_no_scale): |
| 45 | + print("✓ SUCCESS: scale=2.0 produces different results") |
| 46 | + else: |
| 47 | + print("✗ FAILED: scale=2.0 should produce different results") |
| 48 | + return False |
| 49 | + |
| 50 | + # Test inverse function works correctly with scaling |
| 51 | + a_roundtrip = pnorm_scaled.inverse(result_scaled) |
| 52 | + print(f"Roundtrip test: {a} -> {result_scaled} -> {a_roundtrip}") |
| 53 | + |
| 54 | + if np.allclose(a, a_roundtrip): |
| 55 | + print("✓ SUCCESS: inverse function works with scaling") |
| 56 | + else: |
| 57 | + print("✗ FAILED: inverse function doesn't work correctly") |
| 58 | + print(f"Expected: {a}") |
| 59 | + print(f"Got: {a_roundtrip}") |
| 60 | + return False |
| 61 | + |
| 62 | + # Test manual calculation |
| 63 | + expected_scaled_data = a * 2.0 # [2, 4, 6, 8] |
| 64 | + manual_norm = (expected_scaled_data - 1) / 3 # normalize with vmin=1, vmax=4 |
| 65 | + manual_power = np.power(manual_norm, 2) # Apply gamma=2 |
| 66 | + |
| 67 | + print(f"Manual calculation: {manual_power}") |
| 68 | + print(f"PowerNorm result: {result_scaled}") |
| 69 | + |
| 70 | + if np.allclose(result_scaled, manual_power): |
| 71 | + print("✓ SUCCESS: manual calculation matches PowerNorm result") |
| 72 | + else: |
| 73 | + print("✗ FAILED: manual calculation doesn't match") |
| 74 | + return False |
| 75 | + |
| 76 | + print("\n=== All tests passed! ===") |
| 77 | + return True |
| 78 | + |
| 79 | + # Run the test |
| 80 | + test_PowerNorm_scale_complete() |
| 81 | + |
| 82 | +except ImportError as e: |
| 83 | + print(f"Import error: {e}") |
| 84 | + print("You need to rebuild matplotlib. Try: pip install -e . --no-build-isolation") |
0 commit comments