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

[Bug] Loading BSON variable with unit #301

Closed
briochemc opened this issue Aug 17, 2020 · 8 comments
Closed

[Bug] Loading BSON variable with unit #301

briochemc opened this issue Aug 17, 2020 · 8 comments
Labels
other packages Integration with other Julia packages

Comments

@briochemc
Copy link

Here is a MWE Pluto notebook

### A Pluto.jl notebook ###
# v0.11.6

using Markdown
using InteractiveUtils

# ╔═╡ 665ea77a-e065-11ea-00f6-4bd4c96dd01e
using Unitful, BSON

# ╔═╡ 343a3f00-e067-11ea-0087-578bfebddeb2
ufoo = 3.0u"m/s"

# ╔═╡ 4725579e-e067-11ea-0fee-dbf660902683
BSON.@save "test.bson" ufoo

# ╔═╡ 4f79588c-e067-11ea-13eb-3bf5069dbf64
function get_ufoo()
	BSON.@load "test.bson" ufoo
	return ufoo
end

# ╔═╡ 5a2ccdea-e067-11ea-0169-712307d8649d
ubaz = get_ufoo() # This throws with "Unitful not defined"

# ╔═╡ Cell order:
# ╠═665ea77a-e065-11ea-00f6-4bd4c96dd01e
# ╠═343a3f00-e067-11ea-0087-578bfebddeb2
# ╠═4725579e-e067-11ea-0fee-dbf660902683
# ╠═4f79588c-e067-11ea-13eb-3bf5069dbf64
# ╠═5a2ccdea-e067-11ea-0169-712307d8649d

that throws

UndefVarError: Unitful not defined

    (::BSON.var"#29#30")(::Module, ::String)@extensions.jl:20
    BottomRF@reduce.jl:81[inlined]
    _foldl_impl(::Base.BottomRF{BSON.var"#29#30"}, ::Module, ::Array{Any,1})@reduce.jl:58
    foldl_impl@reduce.jl:48[inlined]
    mapfoldl_impl(::typeof(identity), ::BSON.var"#29#30", ::NamedTuple{(:init,),Tuple{Module}}, ::Array{Any,1})@reduce.jl:44
    #mapfoldl#204(::Base.Iterators.Pairs{Symbol,Module,Tuple{Symbol},NamedTuple{(:init,),Tuple{Module}}}, ::typeof(mapfoldl), ::Function, ::Function, ::Array{Any,1})@reduce.jl:160
    _mapreduce_dim@reducedim.jl:315[inlined]
    #mapreduce#[email protected]:310[inlined]
    #reduce#[email protected]:359[inlined]
    resolve(::Array{Any,1})@extensions.jl:20
    (::BSON.var"#33#34")(::Dict{Symbol,Any})@extensions.jl:53
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#21#22"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:94
    applychildren!(::BSON.var"#21#22"{IdDict{Any,Any}}, ::Array{Any,1})@BSON.jl:28
    raise_recursive@read.jl:94[inlined]
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Array{Any,1})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any})@extensions.jl:138
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:88
    (::BSON.var"#21#22"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:94
    applychildren!(::BSON.var"#21#22"{IdDict{Any,Any}}, ::Array{Any,1})@BSON.jl:28
    raise_recursive@read.jl:94[inlined]
    (::BSON.var"#17#19"{IdDict{Any,Any}})(::Array{Any,1})@read.jl:79
    applychildren!(::BSON.var"#17#19"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:79
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any})@extensions.jl:138
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:88
    (::BSON.var"#18#20"{IdDict{Any,Any}})(::Dict{Symbol,Any})@read.jl:82
    applychildren!(::BSON.var"#18#20"{IdDict{Any,Any}}, ::Dict{Symbol,Any})@BSON.jl:21
    _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:82
    raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any})@read.jl:89
    raise_recursive@read.jl:99[inlined]
    load@read.jl:104[inlined]
    macro expansion@BSON.jl:52[inlined]
    get_ufoo()@Other: 2
    top-level scope@Local: 1

I'm not wure why, since this works perfectly outside of Pluto, and it also works when using BSON but not Unitful... What's happening here? (FWIW, 'm trying to use Pluto with some data I have stored in BSON files with units.)

@fonsp
Copy link
Owner

fonsp commented Aug 17, 2020

Could you try this and see if the error is still there?

### A Pluto.jl notebook ###
# v0.11.6

using Markdown
using InteractiveUtils

# ╔═╡ 665ea77a-e065-11ea-00f6-4bd4c96dd01e
begin
	using Unitful, BSON
	Core.eval(Main, :(using Unitful))
end

# ╔═╡ 343a3f00-e067-11ea-0087-578bfebddeb2
ufoo = 3.0u"m/s"

# ╔═╡ 4725579e-e067-11ea-0fee-dbf660902683
BSON.@save "test.bson" ufoo

# ╔═╡ 4f79588c-e067-11ea-13eb-3bf5069dbf64
function get_ufoo()
	BSON.@load "test.bson" ufoo
	return ufoo
end

# ╔═╡ 5a2ccdea-e067-11ea-0169-712307d8649d
ubaz = get_ufoo() # This throws with "Unitful not defined"

# ╔═╡ Cell order:
# ╠═665ea77a-e065-11ea-00f6-4bd4c96dd01e
# ╠═343a3f00-e067-11ea-0087-578bfebddeb2
# ╠═4725579e-e067-11ea-0fee-dbf660902683
# ╠═4f79588c-e067-11ea-13eb-3bf5069dbf64
# ╠═5a2ccdea-e067-11ea-0169-712307d8649d

@fonsp
Copy link
Owner

fonsp commented Aug 17, 2020

It looks like BSON.jl has the limitation that it needs packages to be defined in the Main scope - it does not work inside modules when using external packages:

https://gist.github.com/fonsp/caef46b11c417014d0d1698b6b70607f#file-bsonunitful-jl

https://gist.github.com/fonsp/caef46b11c417014d0d1698b6b70607f#file-bsonunitful2-jl

🐟 fonsv/Desktop » julia bsonunitful.jl
ubaz = 3.0 m s^-1
🐟 fonsv/Desktop » julia bsonunitful2.jl
ERROR: LoadError: UndefVarError: Unitful not defined
Stacktrace:
 ...

Pluto runs your code inside 'workspace modules' so the line using Unitful, BSON is never executed in Main (only in Main.workspace42, Main.workspace43, etc).

@fonsp
Copy link
Owner

fonsp commented Aug 17, 2020

I don't know how BSON works, but maybe it can be fixed by running the extracting code at the place where BSON.@load is called, instead of running it inside Main.

@fonsp fonsp added the other packages Integration with other Julia packages label Aug 17, 2020
@fonsp
Copy link
Owner

fonsp commented Aug 17, 2020

So this needs JuliaIO/BSON.jl#52 to be fixed

@fonsp fonsp closed this as completed Aug 17, 2020
@briochemc
Copy link
Author

Thanks @fonsp! Maybe it's worth mentioning that this can happen in the FAQ? (And include the Core.eval(Main, :(using MyPackage)) workaround?)

@fonsp
Copy link
Owner

fonsp commented Aug 20, 2020

No I think that this issue is a good enough resource on its own.

But maybe I should start making a big list of packages and their workarounds...

@fonsp
Copy link
Owner

fonsp commented Aug 20, 2020

Another option is to detect that a goofy package is being used, and show the link to the workaround inside the editor

@Tobias-Thomas
Copy link

I also encountered this problem recently, and yes the workaround with Core.eval(...) works, yet feels bad, especially when you need more than one package in there.
I think the "official workaround" is a bit easier, using the normal load function instead of the macro. It accepts a namespace as second argument, which defaults to Main. But when just using @__MODULE__ everything works as intended.
So, for the example above one would use: ufoo = BSON.load("test.bson", @__MODULE__)[:ufoo]

Just writing this here, because this issue was the first link when searching for this BSON related problem in conjunction with Pluto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other packages Integration with other Julia packages
Projects
None yet
Development

No branches or pull requests

3 participants