quantumjot / btrack

Bayesian multi-object tracking
https://btrack.readthedocs.io
MIT License
311 stars 50 forks source link

TrackerConfig.json broken by PR that updated linting #211

Closed p-j-smith closed 1 year ago

p-j-smith commented 1 year ago

https://github.com/quantumjot/BayesianTracker/pull/204 has broken the ability to convert TrackerConfig to json.

Steps to reproduce

import btrack
from btrack import datasets

config_file = btrack.datasets.cell_config()
config = btrack.config.load_config(config_file)
config.json()

Output

The following error is raised:

Traceback (most recent call last):
  File "/Users/paul/git/BayesianTracker/tracker_to_json.py", line 6, in <module>
    config.json()
  File "pydantic/main.py", line 505, in pydantic.main.BaseModel.json
  File "/Users/paul/mambaforge/envs/napari-btrack/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/Users/paul/mambaforge/envs/napari-btrack/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/paul/mambaforge/envs/napari-btrack/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "pydantic/json.py", line 103, in pydantic.json.custom_pydantic_encoder
  File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type 'ndarray' is not JSON serializable

Suggested solution

I've had a quick look into what is causing the error and it's not clear. The PR was so large and may have inadvertently broken other parts of the code. We could revert all the changes in the PR, add a test for converting TrackerConfig to json and writing to file, then start adding back the changes in #204

p-j-smith commented 1 year ago

For a bit more context, the reason this issue wasn't picked up in CI is because the test for exporting a TrackerConfig uses a minimal config that has no MotionModel, HypothesisModel, or ObjectModel. The error is only raised when pydantic attempts to serialise the numpy arrays in these models. Although it's still not clear why there's an error now but not before.