Skip to content

Commit

Permalink
Use pkgextensions in Julia v1.9 (#143)
Browse files Browse the repository at this point in the history
* start work on 1.9 extensions

* move Juno into an extension

* move messages to the top of the file, fix SpecialFunctions

* Update src/Measurements.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* Update ext/MeasurementsJunoExt.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* Update src/show.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* Update ext/MeasurementsJunoExt.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* Update ext/MeasurementsSpecialFunctionsExt.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* `@static` if !isdefined is required in the requires block

* use `result` in MeasurementsSpecialFunctionsExt, remove an extra space

* use value and uncertainty in MeasurementsUntifulExt.jl

* move around the @static declaration in init, remove from unneeded places

* Update src/Measurements.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* reactivate Aqua tests

* use 1 static ifdefined statement

* use 1 static ifdefined statement

* add RecipesBase as an extension

* typo

* try again to move all cond code into 1 @static ifdefined

* remove unused files, move require block at the end

* trailing end space

* move RecipesBase out of the condition

* typo in Project.toml

* add RecipesBase to ignored stale deps

* typo in Aqua kwarg

* change order to the one Aqua.jl likes

* change order of extensions (Aqua.jl)

* typo in Aqua.test_stale_deps

* add Requires to stale_deps

* Make PlotRecipes work

* use 1 if isdefined statement (take 3)

* revert using 1 statement. julia 1.0 does not like that

* Update test/runtests.jl

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

* Update MeasurementsRecipesBaseExt.jl

---------

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
  • Loading branch information
longemen3000 and giordano committed May 15, 2023
1 parent f1d6e89 commit 0b54af0
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 33 deletions.
16 changes: 15 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
Requires = "0.5.0, 1"
julia = "1"

[extensions]
MeasurementsJunoExt = "Juno"
MeasurementsRecipesBaseExt = "RecipesBase"
MeasurementsSpecialFunctionsExt = "SpecialFunctions"
MeasurementsUnitfulExt = "Unitful"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Juno = "e5e0dc1b-0480-54bc-9374-aad01c23163d"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Aqua", "QuadGK", "SpecialFunctions", "Statistics", "Test", "Unitful"]
test = ["Aqua", "QuadGK", "RecipesBase", "SpecialFunctions", "Statistics", "Test", "Unitful"]

[weakdeps]
Juno = "e5e0dc1b-0480-54bc-9374-aad01c23163d"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
46 changes: 46 additions & 0 deletions ext/MeasurementsJunoExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### special-functions.jl
#
# Copyright (C) 2019 Mosè Giordano.
#
# Maintainer: Mosè Giordano <mose AT gnu DOT org>
# Keywords: uncertainty, error propagation, physics
#
# This file is a part of Measurements.jl.
#
# License is MIT "Expat".
#
### Commentary:
#
# This file contains methods to print Measurement objects within a Juno enviroment
#
### Code:

### Special functions
module MeasurementsJunoExt

if isdefined(Base, :get_extension)
using Measurements
using Juno
else
using ..Measurements
using ..Juno
end

Juno.render(i::Juno.Inline, measure::Measurement) =
Juno.render(i, Juno.Row(measure.val, Text(" ± "), measure.err))

Juno.Row(measure.val, Text(" ± "), measure.err)

function Juno.render(ji::Juno.Inline, cm::Complex{<:Measurement})
r, i = reim(cm)
if signbit(i) && !isnan(i)
i = -i
sss = " - "
else
sss = " + "
end
Juno.render(ji, Juno.Row("(", Juno.render(ji, r), ")", sss,
"(", Juno.render(ji, i), ")im"))
end

end
11 changes: 10 additions & 1 deletion src/plot-recipes.jl → ext/MeasurementsRecipesBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
# This file defines the recipes to plot Measurements vectors with Plots.jl package in 2D.
#
### Code:
module MeasurementsRecipesBaseExt

using RecipesBase
if isdefined(Base, :get_extension)
using Measurements: Measurement, value, uncertainty
using RecipesBase
else
using ..Measurements: Measurement, value, uncertainty
using ..RecipesBase
end

@recipe function f(y::AbstractArray{<:Measurement})
yerror := uncertainty.(y)
Expand Down Expand Up @@ -44,3 +51,5 @@ end
yerror := uncertainty.(y)
x, value.(y)
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@
### Code:

### Special functions
using .SpecialFunctions
module MeasurementsSpecialFunctionsExt

if isdefined(Base, :get_extension)
using Measurements
using Measurements: result
using SpecialFunctions
else
using ..Measurements
using ..Measurements: result
using ..SpecialFunctions
end
# Error function: erf, erfinv, erfc, erfcinv, erfcx, erfi, dawson

function SpecialFunctions.erf(a::Measurement{T}) where {T<:AbstractFloat}
Expand Down Expand Up @@ -230,3 +239,4 @@ function Base.factorial(a::Measurement)
:factorial)
SpecialFunctions.gamma(a + one(a))
end
end
16 changes: 15 additions & 1 deletion src/unitful.jl → ext/MeasurementsUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
#
### Code:

import .Unitful: AbstractQuantity, unit, ustrip
module MeasurementsUnitfulExt

if isdefined(Base, :get_extension)
using Measurements
using Measurements: value, uncertainty
import Unitful: AbstractQuantity, unit, ustrip
else
using ..Measurements
using ..Measurements: value, uncertainty
import ..Unitful: AbstractQuantity, unit, ustrip
end


function Measurements.measurement(a::T, b::T) where {T<:AbstractQuantity}
u = unit(a)
return measurement(ustrip(u, a), ustrip(u, b)) * u
Expand All @@ -33,3 +45,5 @@ function Measurements.uncertainty(x::AbstractQuantity)
u = unit(x)
return uncertainty(ustrip(u, x)) * u
end

end
22 changes: 14 additions & 8 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ using Statistics

import Statistics: cor, cov

using Requires

# Functions provided by this package and exposed to users
export Measurement, measurement, ±
# Re-export from Statistics
Expand Down Expand Up @@ -85,11 +83,6 @@ end
# Start from 1, 0 is reserved to derived quantities
const tag_counter = Threads.Atomic{UInt64}(1)

function __init__()
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl")
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("special-functions.jl")
end

measurement(x::Measurement) = x
measurement(val::T) where {T<:AbstractFloat} = Measurement(val, zero(T), UInt64(0), empty_der2(val))
measurement(val::Real) = measurement(float(val))
Expand Down Expand Up @@ -128,6 +121,19 @@ include("math.jl")
include("linear_algebra.jl")
include("show.jl")
include("parsing.jl")
include("plot-recipes.jl")

if !isdefined(Base,:get_extension)
using Requires
using RecipesBase
include("../ext/MeasurementsRecipesBaseExt.jl")
end

@static if !isdefined(Base, :get_extension)
function __init__()
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("../ext/MeasurementsUnitfulExt.jl")
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("../ext/MeasurementsSpecialFunctionsExt.jl")
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" include("../ext/MeasurementsJunoExt.jl")
end
end

end # module
20 changes: 0 additions & 20 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,3 @@ end
if VERSION >= v"1.6.0-rc1"
Printf.tofloat(measure::Measurement) = Printf.tofloat(measure.val)
end

### Juno pretty printing
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" begin
Juno.render(i::Juno.Inline, measure::Measurement) =
Juno.render(i, Juno.Row(measure.val, Text(" ± "), measure.err))

Juno.Row(measure.val, Text(" ± "), measure.err)

function Juno.render(ji::Juno.Inline, cm::Complex{<:Measurement})
r, i = reim(cm)
if signbit(i) && !isnan(i)
i = -i
sss = " - "
else
sss = " + "
end
Juno.render(ji, Juno.Row("(", Juno.render(ji, r), ")", sss,
"(", Juno.render(ji, i), ")im"))
end
end
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Measurements, SpecialFunctions, QuadGK, Calculus
using Test, LinearAlgebra, Statistics, Unitful, Printf, Aqua
Aqua.test_all(Measurements)

if !isdefined(Base,:get_extension)
Aqua.test_all(Measurements)
else
Aqua.test_all(Measurements; stale_deps=false)
Aqua.test_stale_deps(Measurements; ignore=[:RecipesBase, :Requires])
end

import Base: isapprox
import Measurements: value, uncertainty
Expand Down

0 comments on commit 0b54af0

Please sign in to comment.