-
Notifications
You must be signed in to change notification settings - Fork 41
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
Handling Heterogeneous types #135
Comments
As @visr suggested another simple reproducible example is : julia> StructArray(Union{Float64, Int}[1.0, 2])
ERROR: MethodError: no method matching fieldnames(::Type{Union{Float64, Int64}})
|
Problem 1I think what you want to do is possible, but there may be a bit of misconception about In particular, you may want to use julia> using StructArrays
julia> init = StructArrays.ArrayInitializer(t -> t <: NamedTuple)
StructArrays.ArrayInitializer{var"#3#4",typeof(StructArrays.arrayof)}(var"#3#4"(), StructArrays.arrayof)
julia> collect_structarray(((a=1, b=12), 2.3), initializer=init) # if fields are not compatible, give up and use `Vector{Any}`
2-element Array{Any,1}:
(a = 1, b = 12)
2.3
julia> collect_structarray(((a=1, b=12), (a=11, b=11)), initializer=init) # do struct of array thing if fields are compatible
2-element StructArray(::Array{Int64,1}, ::Array{Int64,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,Int64}}:
(a = 1, b = 12)
(a = 11, b = 11)
julia> collect_structarray(((a=1, b=12), (a=11, b="string")), initializer=init) # expand only specific fields as needed
2-element StructArray(::Array{Int64,1}, ::Array{Any,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,Any}}:
NamedTuple{(:a, :b),Tuple{Int64,Any}}((1, 12))
NamedTuple{(:a, :b),Tuple{Int64,Any}}((11, "string")) A lot of thought and testing went into this, so it should have the features you need (unfortunately, not nearly the same effort went into writing docs!). The expansions of the various columns uses |
Thanks for looking into this. I'll definitely try this out. : ) |
No problem, issue is definitely better! To be more concrete, something like this line, should instead be: iter = (basicgeometry(f) for f in t) # no need to collect here
collect_structarray(iter, init=custom_initializer) where the only challenge is determining what is a good initializer for the Geo use case. It needs to take a type and axes and spit out an uninitialized |
Ah! Cool. |
Problem 2This is slightly trickier. I seem to remember julia> geoms = collect_structarray([Point(3, 1), Point(2, 1)])
2-element StructArray(::Array{Tuple{Int64,Int64},1}) with eltype Point{2,Int64}:
[3, 1]
[2, 1]
julia> geoms_meta = meta(geoms, city=["Delhi", "New York"], rainfall=[10, 100])
2-element StructArray(StructArray(::Array{Tuple{Int64,Int64},1}), ::Array{String,1}, ::Array{Int64,1}) with eltype PointMeta{2,Int64,Point{2,Int64},(:city, :rainfall),Tuple{String,Int64}}:
[3, 1]
[2, 1] If you are given an iterator of both geometries and metadata and want to do this operation in one swipe, that is possible but a bit more involved.
Sure, happy to help!
Definitely! That'd be very helpful |
Hello folks. As described in this discourse post, we are adding GeometryBasics support for Shapefile and GeoJSONTables. We seem to be stuck while working on this PR. I had also posted a question on Julia slack but couldn't describe the problem well there.
GeometryBasics uses StructArrays to store individual geometries along with metadata.
Now when we add GeometryBasics support to the parsers, the plan is to store the GeometryBasics-fied geometries in a StructArray.
The thing we are aiming at is being consistent with GeometryBasics so as to avoid writing any sort of conversions later on(for plotting etc, since makie supports GeometryBasics).
Problem 1 :
Formats like geojson allow for multiple geometries in a single file, since StructArrays don't support multiple types it's currently impossible to store those geometries using StructArrays.
Problem 2 :
The type inconsistency problem doesn't seem to be with geometries only. It appears for the metadata too.
The error is clearly because of the type inconsistency of rainfall here(Float64 and Int).
One idea we had was promotion/conversion rules for types but that'd be hard to generalize for most practical cases I guess.
The text was updated successfully, but these errors were encountered: