Julia-Tempering / Pigeons.jl

Distributed and parallel sampling from intractable distributions
https://pigeons.run/dev/
GNU Affero General Public License v3.0
73 stars 9 forks source link

Pigeons.json() array input issue #235

Open UBCYujia opened 1 month ago

UBCYujia commented 1 month ago

Pigeons.json() creates correct form of json string when taking scalar or 1d array as input. However, when it comes to 2d or higher dimensional arrays, it creates invalid json string (missing comma or "]").

miguelbiron commented 1 month ago

Thanks @UBCYujia ! Can you post a minimal reproducible example please?

alexandrebouchard commented 1 month ago

Array of arrays at least seem ok:

julia> Pigeons.json(test = [[12, 12], [1, 2]])
"{\"test\" : [[12, 12], [1, 2]]}"
alexandrebouchard commented 1 month ago

But indeed matrix would probably create parsing error:

julia> Pigeons.json(test = [1 2; 3 4])
"{\"test\" : [1 2; 3 4]}"

Converte the latter into the former?

Thanks for reporting this Yujia!

miguelbiron commented 1 month ago

I wonder if we can just use the JSON.jl pkg.

UBCYujia commented 1 month ago

Thank you for providing the examples! to add one more, this 3d example shows that for a high dimensional array, the lowest level should always be 1d vector. array3d = reshape(1:27, 3, 3, 3) //doesn't work array = [array3d[:, :, i] for i in 1:size(array3d, 3)] //doesn't work, vector of 2d vectors vector_of_vectors = [[[array3d[i, j, k] for k in 1:size(array3d, 3)] for j in 1:size(array3d, 2)] for i in 1:size(array3d, 1)] //works! vector of vectors of 1d vectors

UBCYujia commented 1 month ago

Yes I tried JSON.jl and it works well with Pigeons!

miguelbiron commented 1 month ago

Nice! I see that they dont support NamedTuples which is the way we use Pigeons.json. But they do support Dicts, so the one liner would be

Pigeons.json(; kwargs...) = JSON.json(Dict(pairs(kwargs)))

or something similar

miguelbiron commented 1 month ago

actually this is enough

> myjson(; kwargs...) = JSON.json(kwargs)
> myjson(;a=1,b=2)
"{\"a\":1,\"b\":2}"
alexandrebouchard commented 3 weeks ago

@UBCYujia would you mind creating a PR to fix this issue?

UBCYujia commented 3 weeks ago

Sure! I can try to do that!

alexandrebouchard commented 3 weeks ago

Thank you, and let us know if you have any questions on how to do that. I think Lucas did one earlier so he might be able to assist with the logistics parts of the procedure