Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Representing -inf and inf in json #666

Open
oeway opened this issue Nov 26, 2024 · 3 comments
Open

Representing -inf and inf in json #666

oeway opened this issue Nov 26, 2024 · 3 comments

Comments

@oeway
Copy link
Contributor

oeway commented Nov 26, 2024

Currently the data range representation in yaml can support float infinity, but when trying to convert to json, since there is no native support for -inf and inf, we need to either convert to a string Infinity and -Infinity or set it to null which represent no boundary. Not sure if we need to support in the spec, but maybe as a discussion for future versions.

@FynnBe
Copy link
Member

FynnBe commented Nov 29, 2024

This can be done via pydantic: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.ser_json_inf_nan
We could add an analog to https://bioimage-io.github.io/spec-bioimage-io/bioimageio/spec.html#dump_description for json, if there are use cases??

Accepting "inf" is not a problem as pydantic used the builtin float() function (https://docs.pydantic.dev/latest/api/standard_library_types/#float), so "inf" is already valid (in a bioimage.yaml file, too).

@FynnBe
Copy link
Member

FynnBe commented Nov 29, 2024

The default value of null is actually a bit odd as it maps +inf and -inf to the same value...
So is there partial support for the constants somehow??
stings would fix it then I guess... reading the json as yaml and then parsing with our pydantic models will be compatible:

>>> float("Infinity")
inf
>>> float("-Infinity")
-inf
>>>

Should we change the ser_json_inf_nan config value to strings then, before someone relies on it being 'null'?

I can update the type annotation as dump_description really already returns a JsonValue (YAML just also allows for datetime objects, but when dumping timestamps are already serialized to string. Just when loading a YAML file the YAML parser already returns datetime objects.

@oeway
Copy link
Contributor Author

oeway commented Nov 29, 2024

Right, I think we can go for string representation.

Using null is not entirely nonsense,iIf we go for that I would interpret it differently, it means unbounded. Inf is not very standard as well, e.g. python has None type, but inf is should be write as float("inf") or import math;math.inf.

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

No branches or pull requests

2 participants