Unexpected Type conversion

Im trying to extend some functionality on the framework Persa and got stuck in conversion wich i dont know if its supposed to happen on creating my SparseMatrix. Example code:

abstract type AbstractRating{T <: Number}
end

##df is a dataframe wich is loaded previously and with no missings, the columns are of the right type as well, and consists of user, ratings and items, all of them are Int64

users = maximum(df[:user])
items = maximum(df[:item])
preference = Persa.Preference(df[:rating])
ratings = Persa.convert(df[:rating], preference)
# returns 14175-element Array{Persa.AbstractRating{Int64},1}

sparse(df[:user], df[:item], ratings, users, items)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Persa.AbstractRating{Int64}
Closest candidates are:
  convert(::Type{S}, ::T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)) where {S, T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)} at /Users/pauloxavier/.julia/packages/CategoricalArrays/rQrLR/src/value.jl:86
  convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
 [1] setindex!(::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at ./array.jl:769
 [2] sparse!(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::typeof(+),::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:692
 [3] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::Function) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:570
 [4] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:754
 [5] top-level scope at none:0

When i try to build the sparse array, i get this conversion error. I want an Array{Persa.AbstractRating{Int64},1}.

As a matter of fact, this structure is the step by step used by Persa, wich pass the exact same parameters and datatypes.

Please make an MWE (self-contained, OK if it still errors).

1 Like

Sure, here it is:

Use the develop version, its not published yet.

ContextCF: https://github.com/opauloxavier/ContextCF.jl
ContextDatasetsCF: https://github.com/opauloxavier/ContextDatasetsCF.jl

using Revise
using Persa
using ContextCF
using ContextDatasetsCF


df = ContextDatasetsCF.InCarMusic()


context = ContextCF.DatasetContext(df)```

Error log:

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Persa.AbstractRating{Int64}
Closest candidates are:
  convert(::Type{S}, ::T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)) where {S, T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)} at /Users/Equipe/.julia/packages/CategoricalArrays/rQrLR/src/value.jl:86
  convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
 [1] setindex!(::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at ./array.jl:769
 [2] sparse!(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::typeof(+), ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:692
 [3] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::Function) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:570
 [4] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:754
 [5] Persa.Dataset(::DataFrames.DataFrame, ::Int64, ::Int64) at /Users/Equipe/.julia/dev/Persa/src/dataset.jl:55
 [6] Persa.Dataset(::DataFrames.DataFrame) at /Users/Equipe/.julia/dev/Persa/src/dataset.jl:77
 [7] ContextCF.DatasetContext(::DataFrames.DataFrame, ::Dict{Symbol,Type}) at /Users/Equipe/.julia/dev/ContextCF/src/dataset.jl:55
 [8] ContextCF.DatasetContext(::DataFrames.DataFrame) at /Users/Equipe/.julia/dev/ContextCF/src/dataset.jl:34
 [9] top-level scope at none:0

FWIW, the reality is that you unlikely to get much help when it requires adding a bunch of packages etc.

If you could do the work to extract the non-working code into a minimal example that people can just copy paste in the REPL the chance of getting help is probably 10x larger.

4 Likes

Yes, i’ll try to extract the code, i cant reproduce it very well atm. The issue is described here: https://github.com/JuliaLang/julia/issues/27015 , and here https://github.com/JuliaLang/julia/issues/27494 .