Skip to content

Commit

Permalink
Add generator syntax as DimArray method (#852)
Browse files Browse the repository at this point in the history
* Add DimArray generator method

* Test generator constructor

* Added documentation on new DimArray generator syntax

* Improved test and docs for new DimArray generator method

* Added more DimArray construction examples to docs

* Removed incorrect doc example of DimArray generator construction

* Simplified and corrected DimArray generator construction examples

* Improve docstring example on DimArray generator method

Co-authored-by: Rafael Schouten <[email protected]>

* Improve docstring description on DimArray generator syntax

Co-authored-by: Rafael Schouten <[email protected]>

* Remove unnecessary description in DimArray docstring

Co-authored-by: Rafael Schouten <[email protected]>

* Clarified docstring on DimArray generator method syntax

Co-authored-by: Rafael Schouten <[email protected]>

* Improved tests for DimArray generator constructor syntax

---------

Co-authored-by: Rafael Schouten <[email protected]>
  • Loading branch information
kapple19 and rafaqz authored Nov 12, 2024
1 parent 29a6b99 commit 02b730f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/src/dimarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ fill(7, X(5), Y(10))
fill(7, X(5), Y(10); name=:fill, metadata=Dict())
```

== generator construction

```@ansi dimarray
[x + y for x in X(1:5), y in Y(1:10)]
DimArray(x + y for x in X(1:5), y in Y(1:10))
DimArray(x + y for x in X(1:5), y in Y(1:10); name=:sum, metadata=Dict())
```

:::

## Constructing DimArray with arbitrary dimension names
Expand Down
23 changes: 23 additions & 0 deletions src/array/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ end
DimArray(data, dims, refdims, name, metadata)
DimArray(data, dims::Tuple; refdims=(), name=NoName(), metadata=NoMetadata())
DimArray(gen; kw...)
The main concrete subtype of [`AbstractDimArray`](@ref).
Expand All @@ -372,6 +373,7 @@ moves dimensions to reference dimension `refdims` after reducing operations
## Arguments
- `data`: An `AbstractArray`.
- `gen`: A generator expression. Where source iterators are `Dimension`s the dim args or kw is not needed.
- `dims`: A `Tuple` of `Dimension`
- `name`: A string name for the array. Shows in plots and tables.
- `refdims`: refence dimensions. Usually set programmatically to track past
Expand All @@ -384,6 +386,9 @@ and/or [`Selector`](@ref)s.
Indexing `AbstractDimArray` with non-range `AbstractArray` has undefined effects
on the `Dimension` index. Use forward-ordered arrays only"
Note that the generator expression syntax requires usage of the semi-colon `;`
to distinguish generator dimensions from keywords.
Example:
```jldoctest; setup = :(using Random; Random.seed!(123))
Expand Down Expand Up @@ -415,6 +420,22 @@ julia> A[Near(DateTime(2001, 5, 4)), Between(20, 50)]
40 0.637077
50 0.692235
```
Generator expression:
```jldoctest

Check failure on line 426 in src/array/array.jl

View workflow job for this annotation

GitHub Actions / build

doctest failure in ~/work/DimensionalData.jl/DimensionalData.jl/src/array/array.jl:426-438 ```jldoctest julia> DimArray((x, y) for x in X(1:3), y in Y(1:2); name = :Value) ╭───────────────────────────────────────────╮ │ 3×2 DimArray{Tuple{Int64, Int64},2} Value │ ├───────────────────────────────────────────┴───── dims ┐ ↓ X Sampled{Int64} 1:3 ForwardOrdered Regular Points, → Y Sampled{Int64} 1:2 ForwardOrdered Regular Points └───────────────────────────────────────────────────────┘ ↓ → 1 2 1 (1, 1) (1, 2) 2 (2, 1) (2, 2) 3 (3, 1) (3, 2) ``` Subexpression: DimArray((x, y) for x in X(1:3), y in Y(1:2); name = :Value) Evaluated output: ╭────────────────────────────────────────────╮ │ 3×2 DimArray{Tuple{Int64, Int64}, 2} Value │ ├────────────────────────────────────────────┴──── dims ┐ ↓ X Sampled{Int64} 1:3 ForwardOrdered Regular Points, → Y Sampled{Int64} 1:2 ForwardOrdered Regular Points └───────────────────────────────────────────────────────┘ ↓ → 1 2 1 (1, 1) (1, 2) 2 (2, 1) (2, 2) 3 (3, 1) (3, 2) Expected output: ╭───────────────────────────────────────────╮ │ 3×2 DimArray{Tuple{Int64, Int64},2} Value │ ├───────────────────────────────────────────┴───── dims ┐ ↓ X Sampled{Int64} 1:3 ForwardOrdered Regular Points, → Y Sampled{Int64} 1:2 ForwardOrdered Regular Points └───────────────────────────────────────────────────────┘ ↓ → 1 2 1 (1, 1) (1, 2) 2 (2, 1) (2, 2) 3 (3, 1) (3, 2) diff = Warning: Diff output requires color. ╭───────────────────────────────────────────╮ ╭────────────────────────────────────────────╮ │ 3×2 DimArray{Tuple{Int64, Int64},2} Int64}, 2} Value │ ├───────────────────────────────────────────┴───── ├────────────────────────────────────────────┴──── dims ┐ ↓ X Sampled{Int64} 1:3 ForwardOrdered Regular Points, → Y Sampled{Int64} 1:2 ForwardOrdered Regular Points └───────────────────────────────────────────────────────┘ ↓ → 1 2 1 (1, 1) (1, 2) 2 (2, 1) (2, 2) 3 (3, 1) (3, 2)
julia> DimArray((x, y) for x in X(1:3), y in Y(1:2); name = :Value)
╭───────────────────────────────────────────╮
│ 3×2 DimArray{Tuple{Int64, Int64},2} Value │
├───────────────────────────────────────────┴───── dims ┐
↓ X Sampled{Int64} 1:3 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 1:2 ForwardOrdered Regular Points
└───────────────────────────────────────────────────────┘
↓ → 1 2
1 (1, 1) (1, 2)
2 (2, 1) (2, 2)
3 (3, 1) (3, 2)
```
"""
struct DimArray{T,N,D<:Tuple,R<:Tuple,A<:AbstractArray{T,N},Na,Me} <: AbstractDimArray{T,N,D,A}
data::A
Expand Down Expand Up @@ -467,6 +488,8 @@ function DimArray(f::Function, dim::Dimension; name=Symbol(nameof(f), "(", name(
DimArray(f.(val(dim)), (dim,); name)
end

DimArray(itr::Base.Generator; kwargs...) = rebuild(collect(itr); kwargs...)

const DimVector = DimArray{T,1} where T
const DimMatrix = DimArray{T,2} where T
const DimVecOrMat = Union{DimVector,DimMatrix}
Expand Down
36 changes: 35 additions & 1 deletion test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,41 @@ end


@testset "generator constructor" begin
[(x, y) for x in X(10:10:50), y in Y(0.0:0.1:1.0)]
Xs = X(10:10:50)
Ys = Y(0.0:0.1:1.0)
Zs = Z(100:10:500)
name = :Value

A = [(x, y) for x in Xs, y in Ys]
@test dims(A) == (Xs, Ys)
@test size(A) == (Xs, Ys) .|> length

A = DimArray(x for x in Xs)
@test dims(A) == (Xs,)
@test size(A) == (Xs,) .|> length

A = DimArray(x for x in Xs; name)
@test dims(A) == (Xs,)
@test size(A) == (Xs,) .|> length
@test A.name == name

A = DimArray((x, y) for x in Xs, y in Ys)
@test dims(A) == (Xs, Ys)
@test size(A) == (Xs, Ys) .|> length

A = DimArray((x, y) for x in Xs, y in Ys; name)
@test dims(A) == (Xs, Ys)
@test size(A) == (Xs, Ys) .|> length
@test A.name == name

A = DimArray((x, y, z) for x in Xs, y in Ys, z in Zs)
@test dims(A) == (Xs, Ys, Zs)
@test size(A) == (Xs, Ys, Zs) .|> length

A = DimArray((x, y, z) for x in Xs, y in Ys, z in Zs; name)
@test dims(A) == (Xs, Ys, Zs)
@test size(A) == (Xs, Ys, Zs) .|> length
@test A.name == name
end

@testset "ones, zeros, trues, falses constructors" begin
Expand Down

0 comments on commit 02b730f

Please sign in to comment.