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

Materialize DimArray or DimStack From a Table #739

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

JoshuaBillson
Copy link
Contributor

Description:

resolves #335

This PR aims to let users construct either a DimStack or DimArray from a table with one or more coordinate columns.

Unlike the existing contructor, rows may be out of order or even missing altogether.

Performance:

The algorithm is O(n), requiring two forward passes for each dimension to determine the correct order of rows.

1000x1000: 0.005 Seconds

2000x2000: 0.025 Seconds

4000x4000: 0.108 Seconds

8000x8000: 0.376 Seconds

Example:

julia> r = DimArray(rand(UInt8, 1000, 1000), (X, Y));

julia> t = r |> DataFrame |> Random.shuffle!;

julia> restored = DimArray(t, dims(r));

julia> all(r .== restored)
true

Next Steps:

  1. Finalize method signatures.
  2. Decide what to do with missing rows. Currently, users may choose a missing value to fill in (missing by default).
  3. Add support for a :geometry column.
  4. Write test cases.
  5. Update docs.

@JoshuaBillson
Copy link
Contributor Author

There's still a few questions that need resolving:

  1. What do we do with missing rows? This can be handled explicitly in Rasters.jl, but DimArrays have no concept of missing values. We could let users choose a missing value to write, or we could encode it explicitly with missing. We could also disallow the existence of missing rows, but I don't think this is an ideal solution.
  2. How should we handle a :geometry column? I know that several packages in the Julia ecosystem are using this convention, but I don't have much experience with them. Can we assume :geometry will contain tuples of coordinates, or could they also be some sort of geometry like Meshes.Point?

@asinghvi17
Copy link
Collaborator

asinghvi17 commented Jun 20, 2024

I think it's best to test that the geometry column's elements have GI.PointTrait - then you can always extract e.g. GI.x(point) and the same for y, z, and m. You can also interrogate the dimension via GI.is3d, GI.ismeasured.

Going forward to get the geometry column it's probably best to have first(GI.geometrycolumns(table)) - the fallback implementation will give you (:geometry,), but to handle tables with other geometry columns which may be indicated by e.g. metadata, it's better to use this.

@rafaqz
Copy link
Owner

rafaqz commented Jun 20, 2024

DimensionalData.jl does not depend on GeoInterface

@JoshuaBillson
Copy link
Contributor Author

DimensionalData.jl does not depend on GeoInterface

Being able to interact with geometries in a generic fashion would help us interop with other packages. GeoInterface's only dependency is Extents, which we already depend on. After importing DimensionalData, GeoInterface loads in 0.008 seconds.

I also see that Rasters already depends on GeoInterface. We could simply ignore the :geometry column in DimensionalData, then implement the additional functionality in Rasters. However, I think the problem is general enough to be implemented here.

@rafaqz
Copy link
Owner

rafaqz commented Jun 20, 2024

It's not about deps or timing, it's about clean feature scope.

The promise here is that Rasters (and YAX) have all the geo deps and features, so non-geo people don't have to worry about them. Some of the biggest contributors here use DD for unrelated fields.

I would ignore point columns here entirely and instead write the code so it's easy for Rasters to handle them. Taking the underscores off a few functions and documenting them as an real interface will help that.

@JoshuaBillson
Copy link
Contributor Author

I've updated the docstrings for both the DimArray and DimStack constructors. By default, materializers will now use the Contains selector for irregular and non-numeric coordinates, with the option to specify an alternative when desired. We're exporting the restore_array and coords_to_index methods to be used by downstream packages like Rasters.jl. I've also written several test cases to show that we can handle tables with out of order and missing rows. If everything looks good, I think we can go ahead and merge this PR.

Copy link
Owner

@rafaqz rafaqz left a comment

Choose a reason for hiding this comment

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

This is good, but a few minor changes are needed for it to be correct. Constructed selectors should be allowed so At can have atol. We can just construct selector types at the outer level (they're just filled with nothing.

Then there is a bit more dispatch needed to make the fast paths correct for At/Near/Contains with their standard behaviour.

Copy link
Owner

@rafaqz rafaqz left a comment

Choose a reason for hiding this comment

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

Looks good, a bunch of minor fixes.

Probably the main question is how Number and DateTime is handled

@rafaqz
Copy link
Owner

rafaqz commented Sep 15, 2024

Any news on this PR @JoshuaBillson ? would be good to have this available

JoshuaBillson and others added 6 commits September 17, 2024 23:55
Co-authored-by: Rafael Schouten <[email protected]>
Co-authored-by: Rafael Schouten <[email protected]>
Co-authored-by: Rafael Schouten <[email protected]>
Co-authored-by: Rafael Schouten <[email protected]>
Co-authored-by: Rafael Schouten <[email protected]>
Co-authored-by: Rafael Schouten <[email protected]>
@JoshuaBillson
Copy link
Contributor Author

Any news on this PR @JoshuaBillson ? would be good to have this available

Sorry, I've been busy with some other projects. I'll try to resolve your suggested fixes this week. Once that's done, I think we just need to update the docs for DimStack and DimArray and write some more test cases.

@rafaqz
Copy link
Owner

rafaqz commented Sep 18, 2024

No worries at all, whenever you have time.

There are just some nice consequences of having this, like loading GeoJSON to a vector data cube, so I'm keen to have it.

@tiemvanderdeure
Copy link
Collaborator

How close is this PR to the finish line? I'm doing some dataframes-heavy things right now and found myself in a situation a few times where this would been super helpful. Haven't looked into the nitty-gritty of the code, but this looks almost ready. I can do some final polishing if that's all it takes to make this available.

@rafaqz
Copy link
Owner

rafaqz commented Mar 19, 2025

Maybe try it out? I guess it needs a rebase too. I haven't had time or a use case yet to test it so not sure if it's finished

@JoshuaBillson
Copy link
Contributor Author

Maybe try it out? I guess it needs a rebase too. I haven't had time or a use case yet to test it so not sure if it's finished

I believe it was 90% finished. We mostly need test cases and documentation. We'll also need to rebase, as you mentioned.

One thing I noted when reading the new documentation is the inclusion of AutoValues(), AutoOrder(), and AutoSpan() to automatically infer dimensional lookups from an array of values. Could we reuse this logic here to infer the values, order, and span instead of using custom methods? This would remove redundancy from the codebase and ensure consistency in how lookups are detected.

@tiemvanderdeure
Copy link
Collaborator

Okay the rebase wasn't too hard.

There are a few method ambiguities - I'll sort those out.

Definitely a good idea to use the automatic detection of lookup types using format. It would also be really neat if it were possible to pass things like X(Sampled(order = ForwardOrdered())) to force the dimensions to look a certain way, where other fields are inferred from the data. Similar to dimensions how you pass partially formatted dimensions to rand

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.

Add a Tables.materializer method
4 participants