bgroenks96 / SimulationBasedInference.jl

A flexible toolkit for simulation based inference in Julia
MIT License
15 stars 3 forks source link

run_ddm_inference_synthetic.jl bug #3

Closed JunmiHu closed 1 week ago

JunmiHu commented 1 week ago

Hi, I'd first like to thank you for taking the time to create this package. However, I did have problems running the ddm examples. I have documented the details for reproducing the errors below, but feel free to reach out if you were to need any further information.

Description run_ddm_inference_synthetic.jl returns an error: "LoadError: MethodError: no method matching isless(::Int64, ::DateTime)" upon calling the get_observables function in summarize_ensemble.

To Reproduce

git clone git@github.com:bgroenks96/SimulationBasedInference.jl.git
cd SimulationBasedInference.jl/examples/ddm
julia --project
] instantiate
include("run_ddm_inference_synthetic.jl")

Stack Trace The commands above yields the following result.

ERROR: LoadError: MethodError: no method matching isless(::Int64, ::DateTime)

Closest candidates are:
  isless(::Missing, ::Any)
   @ Base missing.jl:87
  isless(::Any, ::Missing)
   @ Base missing.jl:88
  isless(::Number, ::Unitful.AbstractQuantity)
   @ Unitful ~/.julia/packages/Unitful/R4J37/src/quantities.jl:235
  ...

Stacktrace:
  [1] <(x::Int64, y::DateTime)
    @ Base ./operators.jl:352
  [2] >(x::DateTime, y::Int64)
    @ Base ./operators.jl:378
  [3] (::DimensionalData.var"#221#222"{…})(lookup::DimensionalData.Dimensions.Lookups.Sampled{…})
    @ DimensionalData ~/.julia/packages/DimensionalData/bjXdf/src/array/methods.jl:445
  [4] map(f::DimensionalData.var"#221#222"{UnionAll, DimensionalData.Dimensions.Lookups.NoLookup{…}}, t::Tuple{DimensionalData.Dimensions.Lookups.Sampled{…}})
    @ Base ./tuple.jl:291
  [5] _check_cat_lookup_order(::Type, ::DimensionalData.Dimensions.Lookups.NoLookup{Base.OneTo{Int64}}, ::Vararg{DimensionalData.Dimensions.Lookups.Lookup})
    @ DimensionalData ~/.julia/packages/DimensionalData/bjXdf/src/array/methods.jl:439
  [6] _check_cat_lookups
    @ ~/.julia/packages/DimensionalData/bjXdf/src/array/methods.jl:394 [inlined]
  [7] check_cat_lookups
    @ ~/.julia/packages/DimensionalData/bjXdf/src/array/methods.jl:390 [inlined]
  [8] hcat
    @ ~/.julia/packages/DimensionalData/bjXdf/src/array/methods.jl:346 [inlined]
  [9] #4
    @ ~/SimulationBasedInference.jl/src/utils.jl:17 [inlined]
 [10] map
    @ ./tuple.jl:292 [inlined]
 [11] (::SimulationBasedInference.var"#3#5"{typeof(hcat)})(acc::@NamedTuple{y_obs::DimensionalData.DimMatrix{…}, y::DimensionalData.DimMatrix{…}}, xᵢ::@NamedTuple{y_obs::DimensionalData.DimVector{…}, y::DimensionalData.DimVector{…}})
    @ SimulationBasedInference ~/SimulationBasedInference.jl/src/utils.jl:17
 [12] macro expansion
    @ ./reduce.jl:265 [inlined]
 [13] macro expansion
    @ ./simdloop.jl:77 [inlined]
 [14] mapreduce_impl(f::typeof(identity), op::SimulationBasedInference.var"#3#5"{typeof(hcat)}, A::Vector{NamedTuple}, ifirst::Int64, ilast::Int64, blksize::Int64)
    @ Base ./reduce.jl:263
 [15] mapreduce_impl
    @ ./reduce.jl:277 [inlined]
 [16] _mapreduce(f::typeof(identity), op::SimulationBasedInference.var"#3#5"{typeof(hcat)}, ::IndexLinear, A::Vector{NamedTuple})
    @ Base ./reduce.jl:447
 [17] _mapreduce_dim
    @ ./reducedim.jl:365 [inlined]
 [18] mapreduce
    @ ./reducedim.jl:357 [inlined]
 [19] reduce
    @ ./reducedim.jl:406 [inlined]
 [20] ntreduce
    @ ~/SimulationBasedInference.jl/src/utils.jl:16 [inlined]
 [21] get_observables(sol::SimulatorInferenceSolution{EnIS, SimulatorInferenceProblem{NamedProductPrior{@NamedTuple{a::LogNormal{…}, b::LogNormal{…}}}, ComponentVector{Float64, Vector{Float64}, Tuple{Axis{…}}}, Nothing}, SimulationArrayStorage}, iter::Int64)
    @ SimulationBasedInference ~/SimulationBasedInference.jl/src/ensembles/ensemble_utils.jl:82
 [22] get_observables
    @ ~/SimulationBasedInference.jl/src/ensembles/ensemble_utils.jl:78 [inlined]
 [23] summarize_ensemble(inference_sol::SimulatorInferenceSolution{EnIS, SimulatorInferenceProblem{NamedProductPrior{@NamedTuple{a::LogNormal{…}, b::LogNormal{…}}}, ComponentVector{Float64, Vector{Float64}, Tuple{Axis{…}}}, Nothing}, SimulationArrayStorage}, obs_name::Symbol)
    @ Main ~/SimulationBasedInference.jl/examples/ddm/run_ddm_inference_synthetic.jl:94
 [24] top-level scope
    @ ~/SimulationBasedInference.jl/examples/ddm/run_ddm_inference_synthetic.jl:151
 [25] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [26] top-level scope
    @ REPL[5]:1

System info OS: MacOS 14.4.1 julia version: 1.10.4

bgroenks96 commented 1 week ago

Hi, sorry about that. I'll take a look now.

bgroenks96 commented 1 week ago

This is fixed in 07a34fef021, but I will keep the issue open until I add a test case to cover it.

The issue was a recent-ish change where all observables are now represented as DimArrays. The get_observables function had not been updated to correctly handle this.

JunmiHu commented 1 week ago

Thank you for the quick reply!

Upon running the same commands as above, I now obtain the following error:

julia> include("run_ddm_inference_synthetic.jl")
WARNING: redefinition of constant Main.rng. This may fail, cause incorrect answers, or produce other errors.
  1.417704 seconds (14.35 M allocations: 1.939 GiB, 4.97% gc time, 1.00% compilation time)
  0.310802 seconds (9.18 M allocations: 768.920 MiB, 14.33% gc time, 0.99% compilation time)
  2.399415 seconds (89.12 M allocations: 6.381 GiB, 10.73% gc time, 3.13% compilation time)
ERROR: LoadError: UndefVarError: `PySNE` not defined
Stacktrace:
 [1] top-level scope
   @ ./timing.jl:279
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:489
 [3] top-level scope
   @ REPL[2]:1

It isn't clear to me why the program isn't able to locate the PySNE function, but I have been unable to make it work in my own code as well. I'm quite new to Julia, so it is possible that there is some detail I have missed.

Also, I recognize that this can be considered to be a separate issue. If relevant, do you wish for me to create a separate issue for this?

bgroenks96 commented 1 week ago

Yes, that's not super well documented yet... sorry for the confusion.

The PySNE type is only compiled/loaded when the PythonCall package is available in the local environment. This is a soft dependency to avoid making this package too bloated for those who do not wish to use the python tools.

In this case, PythonCall should be included as a dependency in the local environment for the example.

Try restarting julia from the root directory of the repository as:

julia --project=examples/ddm

Then run instantiate again in the package manager.

The python dependencies should then be installed and the PySNE type will be available.

JunmiHu commented 1 week ago

Hi, thank you again for the quick response. I believe that I am doing this currently. To verify, I am running the following commands:

git clone git@github.com:bgroenks96/SimulationBasedInference.jl.git
cd SimulationBasedInference.jl
julia --project=examples/ddm
] instantiate
include("examples/ddm/run_ddm_inference_synthetic.jl")
include("examples/ddm/run_ddm_inference_synthetic.jl")

This yields the following stack trace:

julia> include("examples/ddm/run_ddm_inference_synthetic.jl")
WARNING: redefinition of constant Main.rng. This may fail, cause incorrect answers, or produce other errors.
  1.499571 seconds (14.35 M allocations: 1.939 GiB, 4.78% gc time, 0.97% compilation time)
  0.301549 seconds (9.18 M allocations: 768.927 MiB, 12.84% gc time, 0.97% compilation time)
  2.448723 seconds (83.61 M allocations: 5.987 GiB, 16.92% gc time, 2.87% compilation time)
ERROR: LoadError: UndefVarError: `PySNE` not defined
Stacktrace:
 [1] top-level scope
   @ ./timing.jl:279
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:489
 [3] top-level scope
   @ REPL[4]:1

PythonCall works

I believe the PythonCall installation is correct. For instance, in this Julia REPL, I can run:

julia> re = pyimport("re")
Python: <module 're' from '/Users/junmiaohu/sommerjobb2024/SimulationBasedInference.jl/examples/ddm/.CondaPkg/env/lib/python3.11/re/__init__.py'>

julia> x = pylist([1,2,3])
Python: [1, 2, 3]

MWE to reproduce the error

I have also attached a minimum working example below, named testing.jl. I believe it is intended that the program below runs without any errors:

using PythonCall
using SimulationBasedInference
using SimulationBasedInference.PySBI

PySNE()

Calling the program twice yields:

julia> include("examples/ddm/testing.jl")
Matplotlib is building the font cache; this may take a moment.
┌ Warning: Error requiring `PythonCall` from `SimulationBasedInference`
│   exception =
│    LoadError: ReadOnlyMemoryError()
│    in expression starting at /Users/junmiaohu/sommerjobb2024/SimulationBasedInference.jl/ext/pysbi/PySBI.jl:1
└ @ Requires ~/.julia/packages/Requires/Z8rfN/src/require.jl:51
ERROR: LoadError: UndefVarError: `PySNE` not defined
Stacktrace:
 [1] top-level scope
   @ ~/sommerjobb2024/SimulationBasedInference.jl/examples/ddm/testing.jl:5
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:489
 [3] top-level scope
   @ REPL[10]:1
in expression starting at #############

julia> include("examples/ddm/testing.jl")
ERROR: LoadError: UndefVarError: `PySNE` not defined
Stacktrace:
 [1] top-level scope
   @ ~/sommerjobb2024/SimulationBasedInference.jl/examples/ddm/testing.jl:5
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:489
 [3] top-level scope
   @ REPL[10]:1

Thank you for help so far! Let me know if you need any other information.

bgroenks96 commented 1 week ago

I am not able to reproduce the issue on Julia v.1.10.3. I will try upgrading and see if that changes anything.

It is worth noting that it's generally not advisable to include a file more than once, although I would not expect it to produce such an error.

Please go ahead and create a separate issue for this.

JunmiHu commented 1 week ago

Hi,

Something was wrong with my package installation of the repository in julia. I have now been able to run it without issue on Julia v.1.10.4. Apologies for the confusion, and thank you for your help!