nipreps / mriqc

Automated Quality Control and visual reports for Quality Assessment of structural (T1w, T2w) and functional MRI of the brain
http://mriqc.readthedocs.io
Apache License 2.0
299 stars 132 forks source link

json contains NaNs #1089

Closed dmd closed 3 months ago

dmd commented 1 year ago

JSON file output sometimes contains a NaN, e.g.:

[...]
  "fwhm_y": 4.22584,
  "fwhm_z": 4.806127999999999,
  "gcor": 0.00480853,
  "gsr_x": NaN,
  "gsr_y": 0.36533844470977783,
[...]

I'm not sure what the right thing to do here is, but it's a little annoying because it means any JS tools to graph this data need to preprocess the file as text and then remove those NaNs instead of just parsing it as json directly.

effigies commented 1 year ago

Hmm. It seems the JSON spec does not define nan or ±inf, but Python's json.dumps() will permit it unless allow_nan=False. Unfortunately, allow_nan=False doesn't convert to None so that it becomes null (which is what you get from JSON.stringify(nan) in javascript).

I guess the correct thing is something like:

import json
import math

class JSCompatEncoder(json.JSONEncoder):
    def encode(self, o):
        if isinstance(o, float) and math.isnan(o):
            o = None
        return super().encode(o)
>>> json.dumps(math.nan, cls=JSCompatEncoder)
'null'