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

Small patches for Julia interface #21

Merged
merged 8 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/MParT.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Wrapping code to make the Julia module usable
module MParT
using CxxWrap
using MParT_jll
import Libdl
@wrapmodule ()->libmpartjl :MParT_julia_module
import Base: getindex, lastindex, show, iterate, convert

ConditionalMapBasePtr = CxxWrap.StdLib.SharedPtr{<:ConditionalMapBase}
for op = (:Evaluate, :Gradient, :Inverse, :inputDim, :outputDim,
:LogDeterminant, :LogDeterminantCoeffGrad, :LogDeterminantInputGrad,
:numCoeffs, :CoeffMap, :CoeffGrad, :SetCoeffs, :TestError)
eval(quote
$op(obj::CxxWrap.StdLib.SharedPtr, args...) = $op(obj[], args...)
end)
end

using CxxWrap
using MParT_jll
import Libdl
@wrapmodule ()->libmpartjl :MParT_julia_module
import Base: getindex, lastindex, show, iterate, vec

ConditionalMapBasePtr = CxxWrap.StdLib.SharedPtr{<:ConditionalMapBase}
for op = (:Evaluate, :Gradient, :Inverse, :inputDim, :outputDim,
:LogDeterminant, :LogDeterminantCoeffGrad, :LogDeterminantInputGrad,
:numCoeffs, :CoeffMap, :CoeffGrad, :SetCoeffs, :TestError)
eval(quote
$op(obj::CxxWrap.StdLib.SharedPtr, args...) = $op(obj[], args...)
end)
end

function __init__()
@initcxx
Expand Down Expand Up @@ -100,6 +101,7 @@
MultiIndexSet(A::AbstractVector{<:Integer}) = MultiIndexSet(Cint.(collect(reshape(A, length(A), 1))))
Base.getindex(A::MultiIndex, i::AbstractVector{<:Integer}) = getindex.((A,), i)
Base.lastindex(A::MultiIndex) = length(A)
Base.vec(A::MultiIndex) = Int.(ToVector(A))

# Not implemented yet
# Base.getindex(A::CxxWrap.reference_type_union(MParT.TriangularMap), s::Base.UnitRange) = Slice(A, first(s), last(s))
Expand Down Expand Up @@ -263,7 +265,7 @@
```jldoctest
julia> maxDegrees = MultiIndex(2,3); # limit both dimensions by order 3

julia> ATMOptions(opt_alg="LD_SLSQP", maxDegrees=maxDegrees);
julia> ATMOptions(maxDegrees=maxDegrees);
```
See also [`TrainMapAdaptive`](@ref), [`TrainOptions`](@ref), [`MapOptions`](@ref)
"""
Expand All @@ -272,7 +274,7 @@
for kwarg in kwargs
field = Symbol("__"*string(first(kwarg))*"!")
value = last(kwarg)
if value isa String && !startswith(value, "opt") && value == "verbose"
if value isa String && !startswith(value, "opt") && value != "verbose"
value = MParT.eval(Meta.parse("__"*value))
end
getfield(MParT, field)(opts, value)
Expand Down Expand Up @@ -339,7 +341,7 @@
`move_coeffs` is true.

# Examples
```jldoctest

Check failure on line 344 in src/MParT.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/MParT.jl/MParT.jl/src/MParT.jl:344-354 ```jldoctest julia> dim, order = 5, 3; julia> msets = [FixedMultiIndexSet(d, order) for d in 1:dim]; julia> opts = MapOptions(); julia> components = [CreateComponent(mset, opts) for mset in msets]; julia> trimap = TriangularMap(components); ``` Subexpression: trimap = TriangularMap(components); Evaluated output: ERROR: MethodError: no method matching TriangularMap(::CxxWrap.StdLib.StdVectorAllocated{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}, ::Bool) Closest candidates are: TriangularMap(!Matched::Vector, ::Bool) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:356 TriangularMap(::Union{CxxWrap.CxxWrapCore.ConstCxxRef{CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}, CxxWrap.CxxWrapCore.CxxRef{CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}, CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}) @ MParT ~/.julia/packages/CxxWrap/5IZvn/src/CxxWrap.jl:624 Stacktrace: [1] TriangularMap(maps::Vector{CxxWrap.StdLib.SharedPtrAllocated{MParT.ConditionalMapBase}}, move_coeffs::Bool) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:358 [2] TriangularMap(maps::Vector{CxxWrap.StdLib.SharedPtrAllocated{MParT.ConditionalMapBase}}) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:357 [3] top-level scope @ none:1 Expected output: diff = Warning: Diff output requires color. ERROR: MethodError: no method matching TriangularMap(::CxxWrap.StdLib.StdVectorAllocated{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}, ::Bool) Closest candidates are: TriangularMap(!Matched::Vector, ::Bool) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:356 TriangularMap(::Union{CxxWrap.CxxWrapCore.ConstCxxRef{CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}, CxxWrap.CxxWrapCore.CxxRef{CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}, CxxWrap.StdLib.StdVector{CxxWrap.StdLib.SharedPtr{MParT.ConditionalMapBase}}}) @ MParT ~/.julia/packages/CxxWrap/5IZvn/src/CxxWrap.jl:624 Stacktrace: [1] TriangularMap(maps::Vector{CxxWrap.StdLib.SharedPtrAllocated{MParT.ConditionalMapBase}}, move_coeffs::Bool) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:358 [2] TriangularMap(maps::Vector{CxxWrap.StdLib.SharedPtrAllocated{MParT.ConditionalMapBase}}) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:357 [3] top-level scope @ none:1
julia> dim, order = 5, 3;

julia> msets = [FixedMultiIndexSet(d, order) for d in 1:dim];
Expand All @@ -353,7 +355,7 @@
"""
function TriangularMap(maps::Vector, move_coeffs::Bool = true)
maps = StdVector([map for map in maps])
TriangularMap(maps)
TriangularMap(maps, move_coeffs)
end

"""
Expand Down
Loading