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

refactor: Rework parametric types for more flexibility #807

Closed

Conversation

adigitoleo
Copy link

First step for #806.

Example use case: mat2grid(transpose([1 2 3; 4 5 6]))

Also added docstrings to the types which can later be automatically included
into the documentation1.

First step for GenericMappingTools#806.

Example use case: `mat2grid(transpose([1 2 3; 4 5 6]))`

Also added docstrings to the types which can later be automatically included
into the documentation[1].

[1]: https://juliadocs.github.io/Documenter.jl/stable/man/syntax/#@docs-block
Link breakage due to commit 0164606. Might not have caught all of them,
I can't get documenter to work offline.
@adigitoleo
Copy link
Author

It's a draft at the moment because the testsuite is segfaulting for me, so I will need to manually investigate if this breaks other things.

@joa-quim
Copy link
Member

Yep, I get that segfault occasionally too. Can't find out where is t coming from. And to add for some unknown reason now the Linux CI always fail for an obscure reason while installing Julia.

/opt/hostedtoolcache/julia/1.7.1/x64/bin/../lib/julia/libstdc++.so.6: cannot allocate memory in static TLS block
Stacktrace:

length::Int # Byte length of postscript
mode::Int # 1 = Has header, 2 = Has trailer, 3 = Has both
comment::Array{Any,1} # Cell array with any comments
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this would loose the descriptions of each field

Copy link
Author

@adigitoleo adigitoleo Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should, from this commit you can run julia --project and in the REPL do a ?GMTgrid. This shows the fields as I have added to the source docstrings, e.g. https://github.com/GenericMappingTools/GMT.jl/pull/807/files/8699804706c9979b804e20ad82be0787f98cd972#diff-f757a5fd59739f474cf66fd576eab2d7099ebc0bd28dda3ad57e92bfa6ca7829R1-R32

I'm pretty sure that documenter shows the same information, and also now they are accessible via REPL help.

OH I think I see what you mean now, we lose the default output of e.g. ?GMTgrid:

  No documentation found.

  Summary
  ≡≡≡≡≡≡≡≡≡

  mutable struct GMTdataset{T<:Real, N}

  Fields
  ≡≡≡≡≡≡≡≡

  data     :: Array{T<:Real, N}
  ds_bbox  :: Vector{Float64}
  bbox     :: Vector{Float64}
  attrib   :: Dict{String, String}
  colnames :: Vector{String}
  text     :: Vector{String}
  header   :: String
  comment  :: Vector{String}
  proj4    :: String
  wkt      :: String
  geom     :: Int64

  Supertype Hierarchy
  ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

  GMTdataset{T<:Real, N} <: AbstractArray{T<:Real, N} <: Any

I still think the version with descriptions is more valuable. The other option is to document each field and use DocStringExtensions to automatically merge the field docstrings into the type docstring.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having field descriptions on REPL would be nice but no big deal. Before there no description at all of the type.
And I missed to see that you had added docstrings in gmt_main.jl, hence my previous comment.

@adigitoleo

This comment has been minimized.

Base.getindex(D::GMTdataset, inds::Vararg{Int,N}) where N = D.data[inds...]
Base.setindex!(D::GMTdataset, val, inds::Vararg{Int,N}) where N = D.data[inds...] = val

#Base.BroadcastStyle(::Type{<:GMTdataset}) = Broadcast.ArrayStyle{GMTdataset}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you comment these? Not that I really understand what they do. The docs on AbstractArray interface let a lot to be desired.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were causing trouble, I'll have to rework them to fit the new semantics if the parametric type changes are accepted. Since GMTdataset is no longer a subtype of AbstractArray I don't know if we need them anymore, but I'm still investigating.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is more then I know. Will it not break the AbstractArray interface? And I'm considering trying also the Tables interface (have even a branch with an attempt) and think it requires the the tables to be <: AbstractArray

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, I was just going to patch whatever functions needed the array interface to take GMTdataset{AbstractArray{T,N}} instead of GMTDataset{T,N} but doing the table interface that way won't work then.

We could adopt a similar idea to TypedTables.jl, and possibly have our cake and eat it too. The parametric types would wrap an AbstractArray (the "data") but would themselves also be subtypes of AbstractArray:

https://github.com/JuliaData/TypedTables.jl/blob/main/src/Table.jl#L18

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, a little recap. Other that the transpose example what other situations did you get type restricted failures?
Note that we cannot actually allow transpose objects in GMTgrid because we cannot transfer them to C. But we can catch then inside mat2grid and collect them ... as along as I find out out to recognize a transposed object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to think about it again, having the mat2<thing> functions "canonicalize" the data seems weird, maybe it's best for me/others to do that upfront before giving it to GMT.jl. I thought there might be a clever way to do the connversion, but after playing with abstract types some more I don't think that's correct after all.

@adigitoleo
Copy link
Author

Closing for now, I couldn't figure out a nice way to do it at the moment. I might rebase and try again later.

@adigitoleo adigitoleo closed this Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants