diff --git a/Project.toml b/Project.toml index 6f491d55..9b488a1a 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/ext/MeasurementsJunoExt.jl b/ext/MeasurementsJunoExt.jl new file mode 100644 index 00000000..bedec167 --- /dev/null +++ b/ext/MeasurementsJunoExt.jl @@ -0,0 +1,46 @@ +### special-functions.jl +# +# Copyright (C) 2019 Mosè Giordano. +# +# Maintainer: Mosè Giordano +# 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 diff --git a/src/plot-recipes.jl b/ext/MeasurementsRecipesBaseExt.jl similarity index 81% rename from src/plot-recipes.jl rename to ext/MeasurementsRecipesBaseExt.jl index 1aaa0d6a..f1449d06 100644 --- a/src/plot-recipes.jl +++ b/ext/MeasurementsRecipesBaseExt.jl @@ -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) @@ -44,3 +51,5 @@ end yerror := uncertainty.(y) x, value.(y) end + +end diff --git a/src/special-functions.jl b/ext/MeasurementsSpecialFunctionsExt.jl similarity index 96% rename from src/special-functions.jl rename to ext/MeasurementsSpecialFunctionsExt.jl index 8f7563e4..8879f019 100644 --- a/src/special-functions.jl +++ b/ext/MeasurementsSpecialFunctionsExt.jl @@ -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} @@ -230,3 +239,4 @@ function Base.factorial(a::Measurement) :factorial) SpecialFunctions.gamma(a + one(a)) end +end diff --git a/src/unitful.jl b/ext/MeasurementsUnitfulExt.jl similarity index 72% rename from src/unitful.jl rename to ext/MeasurementsUnitfulExt.jl index d0239eb1..12ba3d29 100644 --- a/src/unitful.jl +++ b/ext/MeasurementsUnitfulExt.jl @@ -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 @@ -33,3 +45,5 @@ function Measurements.uncertainty(x::AbstractQuantity) u = unit(x) return uncertainty(ustrip(u, x)) * u end + +end diff --git a/src/Measurements.jl b/src/Measurements.jl index b8be0909..6293f3e3 100644 --- a/src/Measurements.jl +++ b/src/Measurements.jl @@ -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 @@ -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)) @@ -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 diff --git a/src/show.jl b/src/show.jl index f190fe20..033e7b46 100644 --- a/src/show.jl +++ b/src/show.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 570e5da2..75adc60e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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