|
| 1 | +""" |
| 2 | +Tests for Phase 13.9.Fix1: Polynomial function persistence |
| 3 | +
|
| 4 | +Registered polynomial functions must survive schema export/import cycle. |
| 5 | +After loading, polynomial aliases should be functional without manual |
| 6 | +re-registration. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +import numpy as np |
| 11 | +import pandas as pd |
| 12 | +import json |
| 13 | +import sys |
| 14 | +import os |
| 15 | + |
| 16 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 | +from AliasDataFrame import AliasDataFrame |
| 18 | +from PolynomialSpec import PolynomialSpec |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def adf_with_polynomial(): |
| 23 | + """ADF with registered polynomial from subframe.""" |
| 24 | + df_main = pd.DataFrame({ |
| 25 | + 'group': np.array([0, 0, 1, 1, 0, 1]), |
| 26 | + 'x': np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), |
| 27 | + 'y': np.array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5]), |
| 28 | + }) |
| 29 | + # Coefficients: group 0 → [1.0, 0.5, 0.2, 0.05], group 1 → [2.0, -0.3, 0.1, -0.02] |
| 30 | + # Terms: x^0*y^0, x^1*y^0, x^0*y^1, x^1*y^1 |
| 31 | + df_coeffs = pd.DataFrame({ |
| 32 | + 'group': [0, 1], |
| 33 | + 'c_x0_y0': [1.0, 2.0], |
| 34 | + 'c_x1_y0': [0.5, -0.3], |
| 35 | + 'c_x0_y1': [0.2, 0.1], |
| 36 | + 'c_x1_y1': [0.05, -0.02], |
| 37 | + }) |
| 38 | + |
| 39 | + adf = AliasDataFrame(df_main) |
| 40 | + adf.register_subframe('Coeffs', AliasDataFrame(df_coeffs), index_columns=['group']) |
| 41 | + |
| 42 | + spec = PolynomialSpec(columns=['x', 'y'], degrees=(1, 1)) |
| 43 | + coeff_cols = ['c_x0_y0', 'c_x1_y0', 'c_x0_y1', 'c_x1_y1'] |
| 44 | + adf.register_polynomial_from_subframe('poly', spec, 'Coeffs', coeff_cols) |
| 45 | + adf.add_alias('correction', 'poly(x, y)') |
| 46 | + |
| 47 | + return adf, spec, coeff_cols |
| 48 | + |
| 49 | + |
| 50 | +class TestPolynomialPersistence: |
| 51 | + """Phase 13.9.Fix1: Polynomial function persistence.""" |
| 52 | + |
| 53 | + def test_schema_contains_registered_functions(self, adf_with_polynomial): |
| 54 | + """export_schema includes registered_functions.""" |
| 55 | + adf, _, _ = adf_with_polynomial |
| 56 | + schema = adf.export_schema() |
| 57 | + assert 'registered_functions' in schema |
| 58 | + assert 'poly' in schema['registered_functions'] |
| 59 | + assert schema['registered_functions']['poly']['coefficients_subframe'] == 'Coeffs' |
| 60 | + |
| 61 | + def test_schema_has_polynomial_spec(self, adf_with_polynomial): |
| 62 | + """Schema stores PolynomialSpec details for reconstruction.""" |
| 63 | + adf, _, coeff_cols = adf_with_polynomial |
| 64 | + schema = adf.export_schema() |
| 65 | + poly_schema = schema['registered_functions']['poly'] |
| 66 | + assert poly_schema['coeff_select'] == coeff_cols |
| 67 | + assert 'columns' in poly_schema or 'dimensions' in poly_schema |
| 68 | + |
| 69 | + def test_schema_json_serializable(self, adf_with_polynomial): |
| 70 | + """Schema with registered_functions is JSON serializable.""" |
| 71 | + adf, _, _ = adf_with_polynomial |
| 72 | + schema = adf.export_schema() |
| 73 | + json_str = json.dumps(schema) |
| 74 | + restored = json.loads(json_str) |
| 75 | + assert 'registered_functions' in restored |
| 76 | + |
| 77 | + def test_save_load_schema_roundtrip(self, adf_with_polynomial, tmp_path): |
| 78 | + """Schema saves and loads with registered_functions intact.""" |
| 79 | + adf, _, _ = adf_with_polynomial |
| 80 | + schema_path = str(tmp_path / 'schema.json') |
| 81 | + adf.save_schema(schema_path) |
| 82 | + |
| 83 | + loaded_schema = AliasDataFrame.load_schema(schema_path) |
| 84 | + assert 'registered_functions' in loaded_schema |
| 85 | + assert 'poly' in loaded_schema['registered_functions'] |
| 86 | + |
| 87 | + def test_reconstruct_polynomial_from_schema(self, adf_with_polynomial): |
| 88 | + """apply_schema reconstructs polynomial functions.""" |
| 89 | + adf, spec, coeff_cols = adf_with_polynomial |
| 90 | + |
| 91 | + # Get reference values |
| 92 | + adf.materialize_alias('correction') |
| 93 | + reference = adf.df['correction'].values.copy() |
| 94 | + |
| 95 | + # Export schema |
| 96 | + schema = adf.export_schema() |
| 97 | + |
| 98 | + # Create new ADF with same data + subframe |
| 99 | + df_main = adf.df[['group', 'x', 'y']].copy() |
| 100 | + sf = adf.get_subframe('Coeffs') |
| 101 | + adf2 = AliasDataFrame(df_main) |
| 102 | + adf2.register_subframe('Coeffs', AliasDataFrame(sf.df.copy()), index_columns=['group']) |
| 103 | + |
| 104 | + # Apply schema — should reconstruct polynomial |
| 105 | + adf2.apply_schema(schema) |
| 106 | + |
| 107 | + # Alias should work now |
| 108 | + adf2.materialize_alias('correction') |
| 109 | + np.testing.assert_allclose(adf2.df['correction'].values, reference, atol=1e-10) |
| 110 | + |
| 111 | + def test_reconstruct_skips_evaluator(self, adf_with_polynomial): |
| 112 | + """Evaluator functions are skipped during reconstruction (by design).""" |
| 113 | + adf, _, _ = adf_with_polynomial |
| 114 | + |
| 115 | + # Add a fake evaluator entry to schema |
| 116 | + adf._schema['registered_functions']['my_eval'] = { |
| 117 | + 'type': 'evaluator', |
| 118 | + 'coord_columns': ['x'], |
| 119 | + 'predictor_columns': None, |
| 120 | + } |
| 121 | + |
| 122 | + schema = adf.export_schema() |
| 123 | + |
| 124 | + # Create new ADF |
| 125 | + df_main = adf.df[['group', 'x', 'y']].copy() |
| 126 | + sf = adf.get_subframe('Coeffs') |
| 127 | + adf2 = AliasDataFrame(df_main) |
| 128 | + adf2.register_subframe('Coeffs', AliasDataFrame(sf.df.copy()), index_columns=['group']) |
| 129 | + adf2.apply_schema(schema) |
| 130 | + |
| 131 | + # Polynomial should be reconstructed, evaluator should not |
| 132 | + assert 'poly' in adf2._registered_functions |
| 133 | + assert 'my_eval' not in adf2._registered_functions |
| 134 | + |
| 135 | + def test_reconstruct_missing_subframe_skips(self): |
| 136 | + """Reconstruction skips if subframe not registered.""" |
| 137 | + df = pd.DataFrame({'x': [1.0, 2.0], 'y': [0.5, 1.5]}) |
| 138 | + adf = AliasDataFrame(df) |
| 139 | + |
| 140 | + schema = { |
| 141 | + 'registered_functions': { |
| 142 | + 'poly': { |
| 143 | + 'columns': ['x', 'y'], |
| 144 | + 'degrees': [1, 1], |
| 145 | + 'coefficients_subframe': 'MissingSF', |
| 146 | + 'coeff_select': ['c0', 'c1', 'c2', 'c3'], |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + # Should not raise — just skip with message |
| 152 | + adf.apply_schema(schema) |
| 153 | + assert 'poly' not in getattr(adf, '_registered_functions', {}) |
| 154 | + |
| 155 | + @pytest.mark.invariance |
| 156 | + def test_invariance_before_after_schema_roundtrip(self, adf_with_polynomial, tmp_path): |
| 157 | + """Invariance: polynomial values identical before and after schema roundtrip.""" |
| 158 | + adf, _, _ = adf_with_polynomial |
| 159 | + |
| 160 | + # Before |
| 161 | + adf.materialize_alias('correction') |
| 162 | + before = adf.df['correction'].values.copy() |
| 163 | + |
| 164 | + # Save schema |
| 165 | + schema_path = str(tmp_path / 'schema.json') |
| 166 | + adf.save_schema(schema_path) |
| 167 | + |
| 168 | + # Reconstruct |
| 169 | + df_main = adf.df[['group', 'x', 'y']].copy() |
| 170 | + sf = adf.get_subframe('Coeffs') |
| 171 | + adf2 = AliasDataFrame(df_main) |
| 172 | + adf2.register_subframe('Coeffs', AliasDataFrame(sf.df.copy()), index_columns=['group']) |
| 173 | + loaded = AliasDataFrame.load_schema(schema_path) |
| 174 | + adf2.apply_schema(loaded) |
| 175 | + |
| 176 | + # After |
| 177 | + adf2.materialize_alias('correction') |
| 178 | + after = adf2.df['correction'].values.copy() |
| 179 | + |
| 180 | + np.testing.assert_allclose(before, after, atol=1e-10, |
| 181 | + err_msg="Polynomial values differ after schema roundtrip") |
0 commit comments