ProtocolSpec.to_json and write_sedml_protocol raise TypeError on a numpy integer that run_protocol accepts: a seed of np.int64(42), as rng.integers() gives, cannot be written

Open Beginner friendly
#1,750 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
json, numpy, python

Research direction

The failure is in python/bngsim/convert/_protocol.py: ProtocolSpec.to_json calls json.dumps(self.to_dict()) and Experiment.to_dict copies fields and extra unchanged. Run the repro script to see the TypeError on the np.int64 seed, then either convert numpy integers in to_dict or pass a default= to json.dumps that maps np.integer to int. Done when write_sedml_protocol writes a protocol with a numpy seed, n_points or scan_points, and the output matches the int case.

Written by the indexing model from the issue text.

Description

A ProtocolSpec whose experiment holds a numpy integer runs with run_protocol but cannot be written. A seed from rng.integers() is the usual way to get one. ProtocolSpec.to_json() hands the value to json.dumps as is, which raises TypeError: Object of type int64 is not JSON serializable, and so does write_sedml_protocol, which embeds that JSON in its annotation. A numpy n_points or scan_points fails the same way. EvaluationSpec.to_json() writes the same numpy seed without complaint.

Repro (uv run --no-sync python repro.py)

"""A numpy integer in a ProtocolSpec runs, but the protocol cannot be written."""
import os
import tempfile
import warnings

import numpy as np

import bngsim
from bngsim.convert import Experiment, ProtocolSpec, write_sedml_protocol

net = """begin parameters
    1 kp 200.0
    2 kd 0.5
end parameters
begin species
    1 A() 0
end species
begin reactions
    1 0 1 kp
    2 1 0 kd
end reactions
"""
d = tempfile.mkdtemp()
path = os.path.join(d, "m.net")
open(path, "w").write(net)
model = bngsim.Model.from_net(path)
warnings.simplefilter("ignore")  # the no-SBML-target warning is beside the point here

for seed in (42, np.int64(42)):
    exp = Experiment(kind="simulate", method="ssa", t_span=(0.0, 10.0), n_points=11, extra={"seed": seed})
    spec = ProtocolSpec(steps=(exp,), source=None)
    bngsim.run_protocol(model, spec)  # runs either way
    try:
        write_sedml_protocol(spec, os.path.join(d, "p.sedml"), model=model)
        print(f"{type(seed).__name__} seed: written")
    except TypeError as e:
        print(f"{type(seed).__name__} seed: TypeError: {e}")

Output on main 07ff9f69:

int seed: written
int64 seed: TypeError: Object of type int64 is not JSON serializable

spec.to_json() raises the same TypeError for n_points=np.int64(11), and for scan_points=np.int64(3) on a scan.

Expected: a protocol that runs can be written, with numpy integers serialized as the integers they are, as EvaluationSpec does for its seed.

Observed: TypeError from json.dumps, so the protocol can be run but not saved.

Where

python/bngsim/convert/_protocol.py: ProtocolSpec.to_json (json.dumps(self.to_dict())) and Experiment.to_dict, which copies its fields and extra as given. Converting numpy integers in to_dict, or a default= on json.dumps that turns np.integer into int, would cover every field.

Found while reviewing #1641's seed handling, which accepts numpy seeds for runs. Duplicate check: no issue covers it. #1517 (closed) was EvaluationSpec's missing seed, and #1677, #1686 and #1734, all closed, were other SED-ML writer defects.

Dominant language
Python
Stars
3
Forks
5
Avg merge
1d 1h
Merged PRs (30d)
361

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lanl/bngsim

All issues in lanl/bngsim

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.