SPB Git

Repository Public MIT

StateTests.swift 8.1 KB 139 lines swift

1// ORIX Calc2// File: Packages/ORIXFeatureStatistics/Tests/ORIXFeatureStatisticsTests/StateTests.swift3// Description: Tests of the auto-saved state — snapshot round trip, tolerant decoding of damaged snapshots, restore with recomputation, the full demo.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXExpression10import ORIXStatistics11import ORIXUI12@testable import ORIXFeatureStatistics1314@MainActor @Suite struct StateTests {15    private func customised() -> StatisticsStore {16        let store = StatisticsStore()17        store.section = .regression18        var ds = SampleDatasets.housePrices19        ds.setValue(nil, row: 2, column: 1)20        store.data.load(ds)21        store.data.state.selected = 2; store.data.state.paired = 0; store.data.state.quartiles = .tiExclusive22        store.data.state.binning = .fixedCount; store.data.state.binCount = 12; store.data.state.percentile = nil23        store.dist.state.kind = .binomial24        store.dist.setParameter("n", 12); store.dist.setParameter("p", 0.25)25        store.dist.state.x = 3; store.dist.state.region = .atLeast; store.dist.state.chart = .cumulative; store.dist.state.seed = 12345678926        store.inference.state.procedure = .independence; store.inference.state.table = [[10, 20, 30], [15, 15, 20]]27        store.inference.state.alpha = 0.01; store.inference.state.alternative = .less28        store.regression.state.mode = .multiple; store.regression.state.response = 0; store.regression.state.predictors = [1, 3]29        store.simulation.state.tool = .bootstrap; store.simulation.state.expression = "y = 2*a"30        store.simulation.state.inputs = [SimInput(name: "a", spec: DistSpec(.poisson, ["mean": 3]))]31        store.simulation.state.seed = 18_446_744_073_709_551_615; store.simulation.state.bootMethod = .basic32        return store33    }3435    @Test func snapshotRoundTrip() async throws {36        let store = customised()37        let data = try JSONEncoder().encode(store.snapshot)38        let decoded = try #require(StatSnapshot.decode(data))39        #expect(decoded == store.snapshot)40        let other = StatisticsStore()41        await other.restore(decoded)42        #expect(other.snapshot == store.snapshot)43        #expect(other.section == .regression)44        #expect(other.data.dataset.columns[1].values[2] == nil)45        #expect(other.dist.results.name == store.dist.results.name)46        #expect(other.simulation.state.seed == UInt64.max)47    }4849    @Test func persistsThroughTheAppStore() async throws {50        let app = makeApp()51        #expect(app.loadState(StatSnapshot.self, key: Stat.stateKey) == nil)52        let store = customised()53        store.inference.state.procedure = .tTest54        store.inference.state.source = .summary55        store.calculateInference(app)56        await store.calculateRegression(app)57        #expect(store.inference.state.calculated && store.regression.state.calculated)58        app.saveState(store.snapshot, key: Stat.stateKey)59        app.saveState(store.snapshot, key: Stat.stateKey)60        #expect(app.savedModels(kind: "state." + Stat.stateKey).count == 1, "one auto-saved state, updated in place")61        let restored = StatisticsStore()62        await restored.restore(try #require(app.loadState(StatSnapshot.self, key: Stat.stateKey)))63        #expect(restored.snapshot == store.snapshot)64        // What had been calculated is shown again without pressing Calculate, and without new history entries.65        let before = app.history.count66        #expect(restored.inference.output?.test?.statistic == store.inference.output?.test?.statistic)67        #expect(restored.regression.output?.multiple?.rSquared == store.regression.output?.multiple?.rSquared)68        #expect(app.history.count == before)69        #expect(!restored.inference.isStale(dataRevision: restored.data.revision))70    }7172    @Test func damagedSnapshotsFallBackToDefaults() async throws {73        #expect(StatSnapshot.decode(Data("garbage".utf8)) == nil)74        #expect(StatSnapshot.decode(Data()) == nil)75        #expect(StatSnapshot.decode(Data("[1, 2, 3]".utf8)) == nil)76        #expect(StatSnapshot.decode(Data("{}".utf8)) == StatSnapshot())7778        // One section damaged: the others survive.79        let store = customised()80        var object = try #require(try JSONSerialization.jsonObject(with: JSONEncoder().encode(store.snapshot)) as? [String: Any])81        object["dist"] = "broken"82        object["section"] = "nonexistent"83        object["inference"] = ["procedure": "noSuchTest", "alpha": "x", "table": [[1]], "groups": []] as [String: Any]84        var s = try #require(StatSnapshot.decode(try JSONSerialization.data(withJSONObject: object)))85        #expect(s.section == .data)86        #expect(s.dist == DistState())87        #expect(s.data == store.data.state && s.regression == store.regression.state && s.simulation == store.simulation.state)88        #expect(s.inference.procedure == InferenceState().procedure)89        #expect(s.inference.table == InferenceState().table, "a degenerate table is replaced")90        #expect(s.inference.groups.count >= 2)9192        // A dataset with ragged columns, an out-of-range selection and a wild bin count is repaired.93        object["data"] = ["dataset": ["title": "T", "columns": [["name": "A", "values": [1, 2, 3]], ["name": "B", "values": [4]]]], "selected": 9, "paired": 7, "binCount": 100000] as [String: Any]94        s = try #require(StatSnapshot.decode(try JSONSerialization.data(withJSONObject: object)))95        #expect(s.data.dataset.columns.map(\.values.count) == [3, 3])96        #expect(s.data.selected == 1 && s.data.paired == nil && s.data.binCount == 100)97        let restored = StatisticsStore()98        await restored.restore(s)99        #expect(restored.data.analysis.summary.value?.count == 1, "column B has one value")100101        // No columns at all.102        object["data"] = ["dataset": ["title": "T", "columns": []]] as [String: Any]103        s = try #require(StatSnapshot.decode(try JSONSerialization.data(withJSONObject: object)))104        #expect(s.data.dataset.columns.count == 1)105106        // A simulation section with unknown distributions is replaced by the default model.107        object["simulation"] = ["inputs": [["id": "x", "name": "a", "spec": ["kind": "weird"]]], "seed": -4] as [String: Any]108        s = try #require(StatSnapshot.decode(try JSONSerialization.data(withJSONObject: object)))109        #expect(s.simulation.inputs.map(\.name) == SimState().inputs.map(\.name) && s.simulation.seed == 2026)110    }111112    @Test func fullDemoComputesEverySection() async throws {113        let app = makeApp()114        app.registerNatives(StatisticsNatives.all)115        let store = StatisticsStore()116        await store.loadDemo(context: app.engine.context)117        #expect(store.data.analysis.summary.value?.count == 12)118        #expect(store.data.analysis.bivariate?.value != nil)119        #expect(store.dist.results.shadedProbability != nil && store.dist.sample != nil)120        #expect(store.inference.output?.test != nil && store.inference.error == nil)121        #expect(store.regression.output?.simple != nil && store.regression.error == nil)122        #expect(store.simulation.monteCarlo != nil && store.simulation.scenarioOutput != nil && store.simulation.sampling != nil && store.simulation.bootstrap != nil)123        #expect(store.simulation.errors.isEmpty && store.simulation.running == nil)124        #expect(app.history.isEmpty, "the demo is not the user's work: nothing is logged")125        #expect(app.loadState(StatSnapshot.self, key: Stat.stateKey) == nil, "and nothing is saved")126        // The demo is deterministic.127        let again = StatisticsStore()128        await again.loadDemo(context: app.engine.context)129        #expect(again.simulation.monteCarlo?.summary == store.simulation.monteCarlo?.summary)130        #expect(again.simulation.bootstrap?.result.lower == store.simulation.bootstrap?.result.lower)131        #expect(again.dist.sample == store.dist.sample)132    }133134    @Test func sectionsHaveStableIdentifiers() {135        #expect(StatSection.allCases.map(\.rawValue) == ["data", "distributions", "inference", "regression", "simulation"])136        #expect(Stat.datasetKind == "statistics.dataset")137    }138}139
Files main
Menu

Theme