Skip to content

Commit

Permalink
Merge pull request #36 from ptiede/ptiede-formatter
Browse files Browse the repository at this point in the history
Add JuliaFormatter
  • Loading branch information
ptiede authored Aug 17, 2024
2 parents 3561af0 + 3bf6eaf commit ae17f7e
Show file tree
Hide file tree
Showing 22 changed files with 431 additions and 496 deletions.
2 changes: 2 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
style = "yas"
format_markdown = true
12 changes: 12 additions & 0 deletions .github/workflows/Format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Format suggestions
on:
pull_request:
# this argument is not required if you don't use the `suggestion-label` input
types: [ opened, reopened, synchronize, labeled, unlabeled ]
jobs:
code-style:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/julia-format@v3
with:
version: '1' # Set `version` to '1.0.54' if you need to use JuliaFormatter.jl v1.0.54 (default: '1')
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[![Coverage](https://codecov.io/gh/ptiede/ComradeBase.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ptiede/ComradeBase.jl)
[![Build Status](https://github.com/ptiede/ComradeBase.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/ptiede/ComradeBase.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor%27s%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
27 changes: 11 additions & 16 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ using Documenter
DocMeta.setdocmeta!(ComradeBase, :DocTestSetup, :(using ComradeBase); recursive=true)

makedocs(;
modules=[ComradeBase],
authors="Paul Tiede <[email protected]> and contributors",
repo="https://github.com/ptiede/ComradeBase.jl/blob/{commit}{path}#{line}",
sitename="ComradeBase.jl",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://ptiede.github.io/ComradeBase.jl",
assets=String[],
),
pages=[
"Home" => "index.md",
],
)
modules=[ComradeBase],
authors="Paul Tiede <[email protected]> and contributors",
repo="https://github.com/ptiede/ComradeBase.jl/blob/{commit}{path}#{line}",
sitename="ComradeBase.jl",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://ptiede.github.io/ComradeBase.jl",
assets=String[],),
pages=["Home" => "index.md"],)

deploydocs(;
repo="github.com/ptiede/ComradeBase.jl",
devbranch="main",
)
repo="github.com/ptiede/ComradeBase.jl",
devbranch="main",)
17 changes: 13 additions & 4 deletions ext/ComradeBaseEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ using Enzyme: @parallel

const EnzymeThreads = ComradeBase.ThreadsEx{:Enzyme}

function ComradeBase.intensitymap_analytic!(img::IntensityMap{T, N, D, <:ComradeBase.AbstractRectiGrid{D, <:EnzymeThreads}}, s::ComradeBase.AbstractModel) where {T, N, D}
function ComradeBase.intensitymap_analytic!(img::IntensityMap{T,N,D,
<:ComradeBase.AbstractRectiGrid{D,
<:EnzymeThreads}},
s::ComradeBase.AbstractModel) where {T,N,D}
dx, dy = ComradeBase.pixelsizes(img)
g = ComradeBase.domainpoints(img)
f = Base.Fix1(ComradeBase.intensity_point, s)
pimg = parent(img)
@parallel for I in CartesianIndices(pimg)
pimg[I] = f(g[I])*dx*dy
pimg[I] = f(g[I]) * dx * dy
end
return nothing
end

function ComradeBase.intensitymap_analytic!(img::UnstructuredMap{T, N, <:ComradeBase.UnstructuredDomain{D, <:EnzymeThreads}}, s::ComradeBase.AbstractModel) where {T, N, D}
function ComradeBase.intensitymap_analytic!(img::UnstructuredMap{T,N,
<:ComradeBase.UnstructuredDomain{D,
<:EnzymeThreads}},
s::ComradeBase.AbstractModel) where {T,N,D}
g = ComradeBase.domainpoints(img)
f = Base.Fix1(ComradeBase.intensity_point, s)
pimg = parent(img)
Expand All @@ -26,7 +32,10 @@ function ComradeBase.intensitymap_analytic!(img::UnstructuredMap{T, N, <:Comrade
return nothing
end

function ComradeBase.visibilitymap_analytic!(vis::ComradeBase.FluxMap2{T,N,<:ComradeBase.AbstractSingleDomain{<:Any, <:EnzymeThreads}}, s::ComradeBase.AbstractModel) where {T,N}
function ComradeBase.visibilitymap_analytic!(vis::ComradeBase.FluxMap2{T,N,
<:ComradeBase.AbstractSingleDomain{<:Any,
<:EnzymeThreads}},
s::ComradeBase.AbstractModel) where {T,N}
dims = axisdims(vis)
g = domainpoints(dims)
f = Base.Fix1(ComradeBase.visibility_point, s)
Expand Down
22 changes: 15 additions & 7 deletions ext/ComradeBaseOhMyThreadsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,46 @@ module ComradeBaseOhMyThreadsExt
using ComradeBase
using OhMyThreads

function ComradeBase.intensitymap_analytic!(img::IntensityMap{T,N,D,<:ComradeBase.AbstractRectiGrid{D, <:OhMyThreads.Scheduler}}, s::ComradeBase.AbstractModel) where {T,N,D}
function ComradeBase.intensitymap_analytic!(img::IntensityMap{T,N,D,
<:ComradeBase.AbstractRectiGrid{D,
<:OhMyThreads.Scheduler}},
s::ComradeBase.AbstractModel) where {T,N,D}
dims = axisdims(img)
dx = step(dims.X)
dy = step(dims.Y)
g = domainpoints(dims)
pimg = parent(img)
f = Base.Fix1(ComradeBase.intensity_point, s)
tforeach(CartesianIndices(pimg); scheduler=executor(dims)) do I
pimg[I] = f(g[I])*dx*dy
return pimg[I] = f(g[I]) * dx * dy
end
return nothing
end


function ComradeBase.intensitymap_analytic!(img::UnstructuredMap{T,<:AbstractVector,<:UnstructuredDomain{D, <:OhMyThreads.Scheduler}}, s::ComradeBase.AbstractModel) where {T,D}
function ComradeBase.intensitymap_analytic!(img::UnstructuredMap{T,<:AbstractVector,
<:UnstructuredDomain{D,
<:OhMyThreads.Scheduler}},
s::ComradeBase.AbstractModel) where {T,D}
dims = axisdims(img)
g = domainpoints(dims)
f = Base.Fix1(ComradeBase.intensity_point, s)
pimg = parent(img)
tforeach(CartesianIndices(pimg); scheduler=executor(dims)) do I
pimg[I] = f(g[I])
return pimg[I] = f(g[I])
end
return nothing
end

function ComradeBase.visibilitymap_analytic!(vis::ComradeBase.FluxMap2{T,N,<:ComradeBase.AbstractSingleDomain{<:Any, <: OhMyThreads.Scheduler}}, s::ComradeBase.AbstractModel) where {T,N}
function ComradeBase.visibilitymap_analytic!(vis::ComradeBase.FluxMap2{T,N,
<:ComradeBase.AbstractSingleDomain{<:Any,
<:OhMyThreads.Scheduler}},
s::ComradeBase.AbstractModel) where {T,N}
dims = axisdims(vis)
g = domainpoints(dims)
f = Base.Fix1(ComradeBase.visibility_point, s)
pvis = parent(vis)
tforeach(CartesianIndices(pvis); scheduler=executor(dims)) do I
pvis[I] = f(g[I])
return pvis[I] = f(g[I])
end
return nothing
end
Expand Down
34 changes: 17 additions & 17 deletions scratch/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,33 @@
Computes the flux of a intensity map
"""
function flux(im::IntensityMap{T,N}) where {T,N}
return sum(im, dims=(:X, :Y))
return sum(im; dims=(:X, :Y))
end

flux(im::IntensityMap{T,2}) where {T} = sum(im)



"""
centroid(im::IntensityMap)
Computes the image centroid aka the center of light of the image.
"""
function centroid(im::IntensityMap{T,N}) where {T<:Number,N}
(X, Y) = named_axiskeys(im)
return mapslices(x->centroid(IntensityMap(x, (;X, Y))), im; dims=(:X, :Y))
return mapslices(x -> centroid(IntensityMap(x, (; X, Y))), im; dims=(:X, :Y))
end

function centroid(im::IntensityMap{T,2})::Tuple{T,T} where {T<:Number}
x0 = y0 = zero(T)
f = flux(im)
@inbounds for (i,x) in pairs(axiskeys(im,:X)), (j,y) in pairs(axiskeys(im,:Y))
x0 += x.*im[X=i, Y=j]
y0 += y.*im[X=i, Y=j]
@inbounds for (i, x) in pairs(axiskeys(im, :X)), (j, y) in pairs(axiskeys(im, :Y))
x0 += x .* im[X=i, Y=j]
y0 += y .* im[X=i, Y=j]
end
return x0/f, y0/f
return x0 / f, y0 / f
end

centroid(im::IntensityMap{<:StokesParams}) = centroid(stokes(im, :I))


"""
second_moment(im::IntensityMap; center=true)
Expand All @@ -43,28 +40,31 @@ second moment, which is specified by the `center` argument.
"""
function second_moment(im::IntensityMap{T,N}; center=true) where {T<:Number,N}
(X, Y) = named_axiskeys(im)
return mapslices(x->second_moment(IntensityMap(x, (;X, Y)); center), im; dims=(:X, :Y))
return mapslices(x -> second_moment(IntensityMap(x, (; X, Y)); center), im;
dims=(:X, :Y))
end

second_moment(im::IntensityMap{<:StokesParams}; center=true) = second_moment(stokes(im, :I); center=true)
function second_moment(im::IntensityMap{<:StokesParams}; center=true)
return second_moment(stokes(im, :I); center=true)
end

function second_moment(im::IntensityMap{T,2}; center=true) where {T<:Number}
xx = zero(T)
xy = zero(T)
yy = zero(T)
f = flux(im)
(;X,Y) = named_axiskeys(im)
for (i,y) in pairs(Y), (j,x) in pairs(X)
xx += x^2*im[j,i]
yy += y^2*im[j,i]
xy += x*y*im[j,i]
(; X, Y) = named_axiskeys(im)
for (i, y) in pairs(Y), (j, x) in pairs(X)
xx += x^2 * im[j, i]
yy += y^2 * im[j, i]
xy += x * y * im[j, i]
end

if center
x0, y0 = centroid(im)
xx -= x0^2
yy -= y0^2
xy -= x0*y0
xy -= x0 * y0
end

return @SMatrix [xx/f xy/f; xy/f yy/f]
Expand Down
56 changes: 28 additions & 28 deletions scratch/stokes_image_old.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const StokesIntensityMap{T,N} = KeyedArray{StokesParams{T}, N, <:DataArr} where {T, N}
const StokesIntensityMap{T,N} = KeyedArray{StokesParams{T},N,<:DataArr} where {T,N}

baseimage(s::KeyedArray) = parent(parent(s))

Expand All @@ -8,9 +8,11 @@ baseimage(s::KeyedArray) = parent(parent(s))
Create an array of full stokes parameters. The image is stored as a `StructArray` of
@ref[StokesParams].
"""
function StokesIntensityMap(I::IntensityMap{T}, Q::IntensityMap{T}, U::IntensityMap{T}, V::IntensityMap{T}) where {T}
@assert check_grid(I,Q,U,V) "Intensity grids are not the same across the 4 stokes parameters"
polimg = StructArray{StokesParams{T}}(I=baseimage(I), Q=baseimage(Q), U=baseimage(U), V=baseimage(V))
function StokesIntensityMap(I::IntensityMap{T}, Q::IntensityMap{T}, U::IntensityMap{T},
V::IntensityMap{T}) where {T}
@assert check_grid(I, Q, U, V) "Intensity grids are not the same across the 4 stokes parameters"
polimg = StructArray{StokesParams{T}}(; I=baseimage(I), Q=baseimage(Q), U=baseimage(U),
V=baseimage(V))
return IntensityMap(polimg, named_axiskeys(I))
end

Expand All @@ -20,15 +22,17 @@ Helper function that converts a model from something that compute polarized imag
to just a single stokes parameter. This is useful if you just want to fit a single
stokes parameter.
"""
struct SingleStokes{M, S} <: ComradeBase.AbstractModel
struct SingleStokes{M,S} <: ComradeBase.AbstractModel
model::M
end
SingleStokes(m::M, param::Symbol) where {M} = SingleStokes{M, param}(m)
SingleStokes(m::M, param::Symbol) where {M} = SingleStokes{M,param}(m)

visanalytic(::Type{SingleStokes{M,S}}) where {M,S} = visanalytic(M)
imanalytic(::Type{SingleStokes{M,S}}) where {M,S} = imanalytic(M)
@inline intensity_point(s::SingleStokes{M,S}, x,y) where {M,S} = getproperty(intensity_point(s.model, x,y), S)

imanalytic(::Type{SingleStokes{M,S}}) where {M,S} = imanalytic(M)
@inline intensity_point(s::SingleStokes{M,S}, x, y) where {M,S} = getproperty(intensity_point(s.model,
x,
y),
S)

# # not type piracy because I own StokesParams
# function Base.getproperty(s::StokesIntensityMap{T,N,Na}, v::Symbol) where {T,N,Na<:DataArr}
Expand All @@ -41,28 +45,23 @@ imanalytic(::Type{SingleStokes{M,S}}) where {M,S} = imanalytic(M)
# end
# end

function StokesIntensityMap(
I::AbstractArray{T,N}, Q::AbstractArray{T,N},
U::AbstractArray{T,N}, V::AbstractArray{T,N},
dims::NamedTuple{Na,<:NTuple{N,Any}}) where {T, N, Na}

polimg = StructArray{StokesParams{T}}((;I,Q,U,V))
function StokesIntensityMap(I::AbstractArray{T,N}, Q::AbstractArray{T,N},
U::AbstractArray{T,N}, V::AbstractArray{T,N},
dims::NamedTuple{Na,<:NTuple{N,Any}}) where {T,N,Na}
polimg = StructArray{StokesParams{T}}((; I, Q, U, V))
return IntensityMap(polimg, dims)
end

function StokesIntensityMap(
I::AbstractArray{T,N}, Q::AbstractArray{T,N},
U::AbstractArray{T,N}, V::AbstractArray{T,N},
args...) where {T, N}

polimg = StructArray{StokesParams{T}}((;I,Q,U,V))
function StokesIntensityMap(I::AbstractArray{T,N}, Q::AbstractArray{T,N},
U::AbstractArray{T,N}, V::AbstractArray{T,N},
args...) where {T,N}
polimg = StructArray{StokesParams{T}}((; I, Q, U, V))
return IntensityMap(polimg, args...)
end


# simple check to ensure that the four grids are equal across stokes parameters
function check_grid(I,Q,U,V)
named_axiskeys(I) == named_axiskeys(Q) == named_axiskeys(U) == named_axiskeys(V)
function check_grid(I, Q, U, V)
return named_axiskeys(I) == named_axiskeys(Q) == named_axiskeys(U) == named_axiskeys(V)
end

function stokes(pimg::StokesIntensityMap, v::Symbol)
Expand All @@ -79,12 +78,13 @@ function _summary(io, x::StokesIntensityMap{T,N,Na}) where {T,N,Na}
println(io, ndims(x), "-dimensional")
println(io, "StokesIntensityMap{$T, $N, $Na}")
for d in 1:ndims(x)
print(io, d==1 ? "" : d==2 ? "" : d==3 ? "" : "", " ")
print(io, d == 1 ? "" : d == 2 ? "" : d == 3 ? "" : "", " ")
c = AxisKeys.colour(x, d)
AxisKeys.hasnames(x) && printstyled(io, AxisKeys.dimnames(x,d), "", color=c)
printstyled(io, length(axiskeys(x,d)), "-element ", AxisKeys.shorttype(axiskeys(x,d)), "\n", color=c)
AxisKeys.hasnames(x) && printstyled(io, AxisKeys.dimnames(x, d), ""; color=c)
printstyled(io, length(axiskeys(x, d)), "-element ",
AxisKeys.shorttype(axiskeys(x, d)), "\n"; color=c)
end
println(io, "Polarizations ", propertynames(parent(x).data))
return println(io, "Polarizations ", propertynames(parent(x).data))
end

Base.show(io::IO, img::StokesIntensityMap) = summary(io, img)
30 changes: 9 additions & 21 deletions src/ComradeBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ using Reexport
@reexport using PolarizedTypes
using PrecompileTools

export visibility,
intensitymap, intensitymap!,
visibilitymap, visibilitymap!,
StokesParams, CoherencyMatrix,
flux, fieldofview, imagepixels, pixelsizes, IntensityMap,
named_dims
export visibility,
intensitymap, intensitymap!,
visibilitymap, visibilitymap!,
StokesParams, CoherencyMatrix,
flux, fieldofview, imagepixels, pixelsizes, IntensityMap,
named_dims

include("interface.jl")
include("domain.jl")
include("unstructured_map.jl")
include("images/images.jl")

const FluxMap2{T, N, E} = Union{IntensityMap{T,N,<:Any,E}, UnstructuredMap{T,<:AbstractVector,E}}

const FluxMap2{T,N,E} = Union{IntensityMap{T,N,<:Any,E},
UnstructuredMap{T,<:AbstractVector,E}}

include("visibilities.jl")
# include("rrules.jl")
Expand All @@ -39,20 +39,8 @@ include("visibilities.jl")
g = RectiGrid(p)
gs = domainpoints(p)
imgI = IntensityMap(rand(10, 10), g)
imgI.^2
end
end

if !isdefined(Base, :get_extension)
using Requires
end

@static if !isdefined(Base, :get_extension)
function __init__()
@require OhMyThreads = "67456a42-1dca-4109-a031-0a68de7e3ad5" include(joinpath(@__DIR__, "..", "ext", "ComradeBaseOhMyThreadsExt.jl"))
imgI .^ 2
end
end



end
Loading

0 comments on commit ae17f7e

Please sign in to comment.