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

Reduce similar ambiguities #68

Merged
merged 8 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
tags: '*'
- push
- pull_request
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand All @@ -15,30 +10,55 @@ jobs:
fail-fast: false
matrix:
version:
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
- '1'
- 'nightly'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
docs:
name: Documentation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: '1'
version: '1.6'
- run: |
git config --global user.name name
git config --global user.email email
git config --global github.user username
julia --project=docs -e '
using Pkg
Pkg.develop(PackageSpec(path=pwd()))
Pkg.instantiate()'
- run: |
julia --project=docs -e '
using Pkg;
Pkg.develop(PackageSpec(path=pwd()));
Pkg.instantiate();
include("docs/make.jl");'
using Documenter: doctest
using AxisIndices
doctest(AxisIndices)'
- run: julia --project=docs docs/make.jl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AxisIndices"
uuid = "f52c9ee2-1b1c-4fd8-8546-6350938c7f11"
authors = ["Tokazama <[email protected]>"]
version = "0.7.1"
version = "0.7.2"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ When using functions as indexing arguments, the axis corresponding to each argum

```julia
julia> ax[:, >(2)]
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = 1:2
Expand All @@ -65,7 +65,7 @@ julia> inds_before = firstindex(axis):(not_index - 1); # all of the indices bef
julia> inds_after = (not_index + 1):lastindex(axis); # all of the indices after `not_index`

julia> x[:, vcat(inds_before, inds_after)]
2×3 Array{Int64,2}:
2×3 Matrix{Int64}:
1 5 7
2 6 8

Expand All @@ -74,7 +74,7 @@ julia> x[:, vcat(inds_before, inds_after)]
Using an `AxisArray`, this only requires one line of code
```julia
julia> ax[:, !=(2)]
2×3 AxisArray(::Array{Int64,2}
2×3 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = 1:3
Expand All @@ -89,7 +89,7 @@ We can using `ChainedFixes` to combine multiple functions.
julia> using ChainedFixes

julia> ax[:, or(<(2), >(3))] # == ax[:, [1, 4]]
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = 1:2
Expand All @@ -99,7 +99,7 @@ julia> ax[:, or(<(2), >(3))] # == ax[:, [1, 4]]
2 2 8

julia> ax[:, and(>(1), <(4))]
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = 1:2
Expand Down Expand Up @@ -136,7 +136,7 @@ julia> ax = AxisArray(x, nothing, (.1:.1:.4)s)
We can still use functions to access these elements
```julia
julia> ax[:, <(0.3s)]
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = (0.1:0.1:0.2) s
Expand All @@ -155,7 +155,7 @@ julia> ax[1, 0.1s]
...or as intervals.
```julia
julia> ax[:, 0.1s..0.3s]
2×3 AxisArray(::Array{Int64,2}
2×3 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = (0.1:0.1:0.3) s
Expand All @@ -182,7 +182,7 @@ julia> ax = AxisArray(x, 2:3, 2:5)
3 2 4 6 8

julia> ax[:,2]
2-element AxisArray(::Array{Int64,1}
2-element AxisArray(::Vector{Int64}
• axes:
1 = 2:3
)
Expand Down Expand Up @@ -259,7 +259,7 @@ julia> ArrayInterface.known_length(typeof(ax)) # size is known at compile time
julia> ax[1:2, 1:2] .= x[1:2, 1:2]; # underlying type is mutable `Array`, so we can assign new values

julia> ax
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = 1:2
2 = 1:2
Expand All @@ -274,7 +274,7 @@ julia> ax

If each element along a particular axis corresponds to a field of a type then we can encode that information in the axis.
```julia
julia> ax = AxisArray(reshape(1:4, 2, 2), StructAxis{Complex{Float64}}(), [:a, :b])
julia> ax = AxisArray(reshape(1:4, 2, 2), StructAxis{ComplexF64}(), [:a, :b])
2×2 AxisArray(reshape(::UnitRange{Int64}, 2, 2)
• axes:
1 = [:re, :im]
Expand All @@ -289,7 +289,7 @@ julia> ax = AxisArray(reshape(1:4, 2, 2), StructAxis{Complex{Float64}}(), [:a, :
We can then create a lazy mapping of that type across views of the array.
```julia
julia> axview = struct_view(ax)
2-element AxisArray(mappedarray(Complex{Float64}, view(reshape(::UnitRange{Int64}, 2, 2), 1, :), view(reshape(::UnitRange{Int64}, 2, 2), 2, :))
2-element AxisArray(mappedarray(ComplexF64, view(reshape(::UnitRange{Int64}, 2, 2), 1, :), view(reshape(::UnitRange{Int64}, 2, 2), 2, :))
• axes:
1 = [:a, :b]
)
Expand All @@ -313,7 +313,7 @@ julia> mx = attach_metadata(AxisArray(x))
• axes:
1 = 1:2
2 = 1:4
), ::Dict{Symbol,Any}
), ::Dict{Symbol, Any}
• metadata:
)
1 2 3 4
Expand Down Expand Up @@ -345,7 +345,7 @@ We can also pad axes in various ways.
julia> x = [:a, :b, :c, :d];

julia> AxisArray(x, circular_pad(first_pad=2, last_pad=2))
8-element AxisArray(::Array{Symbol,1}
8-element AxisArray(::Vector{Symbol}
• axes:
1 = -1:6
)
Expand All @@ -360,7 +360,7 @@ julia> AxisArray(x, circular_pad(first_pad=2, last_pad=2))
6 :b

julia> AxisArray(x, replicate_pad(first_pad=2, last_pad=2))
8-element AxisArray(::Array{Symbol,1}
8-element AxisArray(::Vector{Symbol}
• axes:
1 = -1:6
)
Expand All @@ -375,7 +375,7 @@ julia> AxisArray(x, replicate_pad(first_pad=2, last_pad=2))
6 :d

julia> AxisArray(x, symmetric_pad(first_pad=2, last_pad=2))
8-element AxisArray(::Array{Symbol,1}
8-element AxisArray(::Vector{Symbol}
• axes:
1 = -1:6
)
Expand All @@ -390,7 +390,7 @@ julia> AxisArray(x, symmetric_pad(first_pad=2, last_pad=2))
6 :b

julia> AxisArray(x, reflect_pad(first_pad=2, last_pad=2))
8-element AxisArray(::Array{Symbol,1}
8-element AxisArray(::Vector{Symbol}
• axes:
1 = -1:6
)
Expand Down
22 changes: 11 additions & 11 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ julia> x[:one] == y["one"] == z[Second(1)]
true

julia> x[[:one, :two]]
2-element AxisArray(::Array{Int64,1}
2-element AxisArray(::Vector{Int64}
• axes:
1 = [:one, :two]
)
Expand Down Expand Up @@ -190,7 +190,7 @@ Axis((1.5:0.5:2.0) s => SimpleAxis(2:3))
However, we can't ensure that the resulting range will have a step of one in other cases so only the indices are returned.
```jldoctest indexing_examples
julia> time1[1:2:3]
2-element AxisArray(::StepRange{Int64,Int64}
2-element AxisArray(::StepRange{Int64, Int64}
• axes:
1 = (1.5:1.0:2.5) s
)
Expand All @@ -199,9 +199,9 @@ julia> time1[1:2:3]
2.5 s 3

julia> time1[[1, 2, 3]]
3-element AxisArray(::Array{Int64,1}
3-element AxisArray(::Vector{Int64}
• axes:
1 = Unitful.Quantity{Float64,𝐓,Unitful.FreeUnits{(s,),𝐓,nothing}}[1.5 s, 2.0 s, 2.5 s]
1 = Unitful.Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}[1.5 s, 2.0 s, 2.5 s]
)
1
1.5 s 1
Expand Down Expand Up @@ -294,7 +294,7 @@ julia> using AxisIndices
julia> A_base = [1 2; 3 4];

julia> A_axis = AxisArray(A_base, ["a", "b"], [:one, :two])
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = ["a", "b"]
2 = [:one, :two]
Expand All @@ -319,7 +319,7 @@ julia> A_axis = AxisArray{Int}(undef, ["a", "b"], [:one, :two]);
julia> A_axis[:,:] = A_base;

julia> A_axis
2×2 AxisArray(::Array{Int64,2}
2×2 AxisArray(::Matrix{Int64}
• axes:
1 = ["a", "b"]
2 = [:one, :two]
Expand All @@ -335,11 +335,11 @@ We can also attach metadata to an array.
julia> using Metadata

julia> attach_metadata(AxisArray(A_base, (["a", "b"], [:one, :two])), (m1 = 1, m2 = 2))
2×2 attach_metadata(AxisArray(::Array{Int64,2}
2×2 attach_metadata(AxisArray(::Matrix{Int64}
• axes:
1 = ["a", "b"]
2 = [:one, :two]
), ::NamedTuple{(:m1, :m2),Tuple{Int64,Int64}}
), ::NamedTuple{(:m1, :m2), Tuple{Int64, Int64}}
• metadata:
m1 = 1
m2 = 2
Expand All @@ -349,11 +349,11 @@ julia> attach_metadata(AxisArray(A_base, (["a", "b"], [:one, :two])), (m1 = 1, m
"b" 3 4

julia> attach_metadata(NamedAxisArray{(:xdim, :ydim)}(A_base, ["a", "b"], [:one, :two]), (m1 = 1, m2 = 2))
2×2 NamedDimsArray(attach_metadata(AxisArray(::Array{Int64,2}
2×2 NamedDimsArray(attach_metadata(AxisArray(::Matrix{Int64}
• axes:
xdim = ["a", "b"]
ydim = [:one, :two]
), ::NamedTuple{(:m1, :m2),Tuple{Int64,Int64}}
), ::NamedTuple{(:m1, :m2), Tuple{Int64, Int64}}
• metadata:
m1 = 1
m2 = 2
Expand All @@ -372,7 +372,7 @@ offset by 4 and the last indices are centered.

```jldoctest indexing_examples
julia> AxisArray(ones(3,3), offset(4), center)
3×3 AxisArray(::Array{Float64,2}
3×3 AxisArray(::Matrix{Float64}
• axes:
1 = 5:7
2 = -1:1
Expand Down
30 changes: 2 additions & 28 deletions src/AxisIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,6 @@ export

const ArrayInitializer = Union{UndefInitializer, Missing, Nothing}

# Val wraps the number of axes to retain
naxes(A::AbstractArray, v::Val) = naxes(axes(A), v)
naxes(axs::Tuple, v::Val{N}) where {N} = _naxes(axs, N)
@inline function _naxes(axs::Tuple, i::Int)
if i === 0
return ()
else
return (first(axs), _naxes(tail(axs), i - 1)...)
end
end

@inline function _naxes(axs::Tuple{}, i::Int)
if i === 0
return ()
else
return (SimpleAxis(1), _naxes((), i - 1)...)
end
end

include("errors.jl")
include("abstract_axis.jl")
include("axis_array.jl")
Expand Down Expand Up @@ -137,15 +118,8 @@ include("centered_axis.jl")
include("identity_axis.jl")
include("padded_axis.jl")
include("struct_axis.jl")

# TODO assign_indices tests
function assign_indices(axis, inds)
if can_change_size(axis) && !((known_length(inds) === nothing) || known_length(inds) === known_length(axis))
return unsafe_reconstruct(axis, inds)
else
return axis
end
end
include("similar.jl")
include("utils.jl")

"""
is_key([collection,] arg) -> Bool
Expand Down
3 changes: 1 addition & 2 deletions src/abstract_axis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ and [`IdentityAxis`](@ref) for more details and examples.
"""
abstract type AbstractOffsetAxis{I,Inds,F} <: AbstractAxis{I,Inds} end


"""
IndexAxis

Expand Down Expand Up @@ -160,7 +159,7 @@ ArrayInterface.known_first(::Type{T}) where {T<:AbstractAxis} = known_first(pare
Base.summary(io::IO, axis::AbstractAxis) = show(io, axis)

function reverse_keys(axis::AbstractAxis, newinds::AbstractUnitRange)
return Axis(reverse(keys(axis)), newinds; checks=NoChecks)
return initialize_axis(reverse(keys(axis)), compose_axis(newinds))
end

###
Expand Down
Loading