Skip to content

Use SciML Style #121

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

Merged
merged 1 commit into from
Jun 23, 2022
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
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "sciml"
42 changes: 42 additions & 0 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: format-check

on:
push:
branches:
- 'master'
- 'release-'
tags: '*'
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}

- uses: actions/checkout@v1
- name: Install JuliaFormatter and format
# This will use the latest version by default but you can set the version like so:
#
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
24 changes: 10 additions & 14 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ using Documenter, LabelledArrays

include("pages.jl")

makedocs(
sitename="LabelledArrays.jl",
authors="Chris Rackauckas",
modules=[LabelledArrays],
clean=true,doctest=false,
format = Documenter.HTML(analytics = "UA-90474609-3",
assets = ["assets/favicon.ico"],
canonical="https://labelledarrays.sciml.ai/stable/"),
pages=pages
)
makedocs(sitename = "LabelledArrays.jl",
authors = "Chris Rackauckas",
modules = [LabelledArrays],
clean = true, doctest = false,
format = Documenter.HTML(analytics = "UA-90474609-3",
assets = ["assets/favicon.ico"],
canonical = "https://labelledarrays.sciml.ai/stable/"),
pages = pages)

deploydocs(
repo = "github.com/SciML/LabelledArrays.jl.git";
push_preview = true
)
deploydocs(repo = "github.com/SciML/LabelledArrays.jl.git";
push_preview = true)
4 changes: 2 additions & 2 deletions docs/pages.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Put in a separate page so it can be used by SciMLDocs.jl

pages=[
pages = [
"Home" => "index.md",
"Example: Nice DiffEq Syntax Without A DSL" => "Example_dsl.md",
"SLArrays" => "SLArrays.md",
"LArrays" => "LArrays.md",
"Relation to NamedTuples" => "NamedTuples_relation.md",
"Note_labelled_slices.md"
"Note_labelled_slices.md",
]
31 changes: 17 additions & 14 deletions src/LabelledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,62 @@ include("larray.jl")
include("chainrules.jl")

# Common
@generated function __getindex(x::Union{LArray,SLArray},::Val{s}) where s
@generated function __getindex(x::Union{LArray, SLArray}, ::Val{s}) where {s}
syms = symnames(x)
idx = syms isa NamedTuple ? syms[s] : findfirst(y->y==s,syms)
idx = syms isa NamedTuple ? syms[s] : findfirst(y -> y == s, syms)
if idx === nothing
:(error("type $(typeof(x)) has no field $(s)"))
elseif idx isa Tuple
:(Base.@_propagate_inbounds_meta; view(getfield(x,:__x), $idx...))
:(Base.@_propagate_inbounds_meta; view(getfield(x, :__x), $idx...))
else
:(Base.@_propagate_inbounds_meta; @views getfield(x,:__x)[$idx])
:(Base.@_propagate_inbounds_meta; @views getfield(x, :__x)[$idx])
end
end

using MacroTools

struct PrintWrapper{T,N,F,X<:AbstractArray{T,N}} <: AbstractArray{T,N}
struct PrintWrapper{T, N, F, X <: AbstractArray{T, N}} <: AbstractArray{T, N}
f::F
x::X
end

import Base: eltype, length, ndims, size, axes, eachindex, stride, strides
MacroTools.@forward PrintWrapper.x eltype, length, ndims, size, axes, eachindex, stride, strides
MacroTools.@forward PrintWrapper.x eltype, length, ndims, size, axes, eachindex, stride,
strides
Base.getindex(A::PrintWrapper, idxs...) = A.f(A.x, A.x[idxs...], idxs)

function lazypair(A, x, idxs)
syms = symnames(typeof(A))
II = LinearIndices(A)
key = eltype(syms) <: Symbol ? syms[II[idxs...]] : findfirst(syms) do sym
key = eltype(syms) <: Symbol ? syms[II[idxs...]] :
findfirst(syms) do sym
ii = idxs isa Tuple ? II[idxs...] : II[idxs]
sym isa Tuple ? ii in II[sym...] : ii in II[sym]
end
key => x
end

Base.show(io::IO, ::MIME"text/plain", x::Union{LArray,SLArray}) = show(io, x)
function Base.show(io::IO, x::Union{LArray,SLArray})
Base.show(io::IO, ::MIME"text/plain", x::Union{LArray, SLArray}) = show(io, x)
function Base.show(io::IO, x::Union{LArray, SLArray})
syms = symnames(typeof(x))
n = length(syms)
pwrapper = PrintWrapper(lazypair, x)
if io isa IOContext && get(io, :limit, false) && displaysize(io) isa Tuple{Integer, Integer}
io = IOContext(io, :limit => true, :displaysize => cld.(2 .*displaysize(io), 3))
if io isa IOContext && get(io, :limit, false) &&
displaysize(io) isa Tuple{Integer, Integer}
io = IOContext(io, :limit => true, :displaysize => cld.(2 .* displaysize(io), 3))
end
println(io, summary(x), ':')
Base.print_array(io, pwrapper)
end

Base.NamedTuple(x::Union{LArray,SLArray}) = NamedTuple{symnames(typeof(x))}(x.__x)
Base.NamedTuple(x::Union{LArray, SLArray}) = NamedTuple{symnames(typeof(x))}(x.__x)
@inline Base.reshape(a::SLArray, s::Size) = StaticArrays.similar_type(a, s)(Tuple(a))

function ArrayInterface.ismutable(::Type{<:LArray{T,N,Syms}}) where {T,N,Syms}
function ArrayInterface.ismutable(::Type{<:LArray{T, N, Syms}}) where {T, N, Syms}
ArrayInterface.ismutable(T)
end
ArrayInterface.can_setindex(::Type{<:SLArray}) = false

export SLArray, LArray, SLVector, LVector, @SLVector, @LArray, @LVector, @SLArray

export @SLSliced, @LSliced
Expand Down
6 changes: 3 additions & 3 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function unwrap_maybe(x::ChainRulesCore.Thunk)
return ChainRulesCore.@thunk(unwrap_maybe(ChainRulesCore.unthunk(x)))
end

function ChainRulesCore.rrule(::typeof(getproperty), A::Union{SLArray,LArray}, s::Symbol)
function ChainRulesCore.rrule(::typeof(getproperty), A::Union{SLArray, LArray}, s::Symbol)
function getproperty_LArray_adjoint(d)
# Hopefully this reference to `A` is optimized away.
Δ = similar(A) .= 0
Expand All @@ -25,9 +25,9 @@ function ChainRulesCore.rrule(::Type{LArray{S}}, x::AbstractArray) where {S}
return LArray{S}(x), LArray_adjoint
end

function ChainRulesCore.rrule(::Type{SLArray{Size,S}}, x::AbstractArray) where {Size,S}
function ChainRulesCore.rrule(::Type{SLArray{Size, S}}, x::AbstractArray) where {Size, S}
# This rule covers constructors of the form `SLArray{(2, ), (:a, :b)}(x)`
# which, amongst other places, is also used in the `@LArray` macro.
SLArray_adjoint(Δ) = ChainRulesCore.NoTangent(), unwrap_maybe(Δ)
return SLArray{Size,S}(x), SLArray_adjoint
return SLArray{Size, S}(x), SLArray_adjoint
end
Loading