Skip to content

feat: native JSON model i/o and BinWM→CODA glue#46

Draft
corriander wants to merge 1 commit into
mainfrom
feat/json-io
Draft

feat: native JSON model i/o and BinWM→CODA glue#46
corriander wants to merge 1 commit into
mainfrom
feat/json-io

Conversation

@corriander

Copy link
Copy Markdown
Owner

Summary

Adds stdlib-only (json) native serialisation for CODA models and Binary Weighting Matrices, and glue to seed a CODA model directly from a BinWM. No new dependencies.

CODA JSON

Flat, human-readable, git-diff-friendly schema:

{
  "requirements": [{"name": "...", "weight": 0.2, "normalise": true}],
  "characteristics": [{"name": "...", "limits": [lo, hi], "value": 24}],
  "relationships": [
    {"requirement": "...", "characteristic": "...",
     "type": "min|max|opt", "correlation": 0.9,
     "target": 0.5, "tolerance": null}
  ]
}
  • CODA.to_dict() / from_dict() — dict form.
  • CODA.to_json(path=None) — returns a string when path is omitted, else writes the file.
  • CODA.read_json(path) — classmethod mirroring read_excel.
  • Only non-null relationships are emitted. limits/value may be None; tolerance is None for non-optimising relationships.
  • Requirements store the raw weight (base_weight for self-normalising requirements, otherwise the pre-normalised weight) plus a normalise flag, so from_dict replays normalisation on the target instance via the public add_* API — no element objects are copied across instances (their weights are context-dependent).

BinWM JSON

BinWM.to_dict() / from_dict() / to_json() / read_json() using the exact shape of the existing test fixtures:

{"requirements": ["..."], "binary_matrix": [[0, 0, 1], ...]}

The bundled case__*.json fixtures are therefore valid interchange files (a test loads one directly through read_json).

BinWM → CODA glue

CODA.add_requirements_from(binwm) adds one requirement per BinWM requirement, using its score as the weight. BinWM.score sums to unity by construction, so scores are added with normalise=True (self-normalising requirements) — this keeps floating-point drift around unity from tripping the combined-weight guard and lets further requirements be mixed in afterwards with automatic re-normalisation.

Tests

14 new tests (168 total, was 154). CODA round-trips through dict, JSON string and file for the bicycle-wheel case study (merit/satisfaction/correlation/weights and all names/limits/values/relationship params asserted), plus optimise-tolerance + unset-value handling and pre-normalised weights. BinWM round-trips including a fixture loaded directly. README updated with a JSON usage example.

🤖 Generated with Claude Code

Add stdlib-only JSON serialisation for CODA models and Binary Weighting
Matrices, plus glue to seed a CODA model from a BinWM.

- CODA.to_dict/from_dict/to_json/read_json using a flat, diff-friendly
  schema (requirements, characteristics, relationships). Requirement raw
  weights and the normalise flag are stored so self-normalising weights
  replay faithfully on the target instance; only non-null relationships
  are emitted.
- BinWM.to_dict/from_dict/to_json/read_json matching the existing test
  fixture shape ({"requirements": [...], "binary_matrix": [[...]]}), so
  the bundled fixtures are valid interchange files.
- CODA.add_requirements_from(binwm) adds one requirement per BinWM
  requirement using its score as weight (normalise=True; BinWM.score
  sums to unity by construction).
- Round-trip tests (dict + json string + file) for the bicycle-wheel
  case study, optimise/unset-value handling, and pre-normalised weights.
- README JSON usage section.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant