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

Support dot-based getproperty in ljl_propfunc #83

Merged
merged 4 commits into from
Dec 4, 2024
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
55 changes: 43 additions & 12 deletions src/ljl_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,33 @@
export parse_ljlexpr


const ljl_expr_allowed_heads = (:call, :macrocall, :||, :&&, :comparison)
const ljl_expr_allowed_heads = Symbol[:., :ref, :call, :macrocall, :||, :&&, :comparison, :if]

const ljl_expr_allowed_funcs = Set([
const ljl_expr_allowed_funcs = Set{Symbol}([
:!,
:(==), :<, :>, :>=, :<=, :!=,
:isapprox, :≈, :≈,
:isapprox, :≈,
:in, :∈, :..,
:+, :-, :*, :/,
:+, :-, :*, :/, :div, :rem, :mod,
:|, :&, :xor,
:^, :sqrt,
:one, :zero, :identity,
:abs, :abs2, :normalize, :norm,
:exp, :exp2, :exp10, :log, :log2, :log10,
:sin, :cos, :tan, :asin, :acos, :atan,
:min, :max,
:isnan, :isinf, :isfinite,
:all, :any, :broadcast,
:get, :getproperty,
:value, :uncertainty, :stdscore, :weightedmean,
:±,
:sum, :prod, :minimum, :maximum, :mean,
:get, :getproperty, :getindex, :first, :last,
:haskey, :isempty, :length, :size,
:(:), :Symbol, :String, :Int, :Float64, :Bool,
:string, :parse,
:value, :uncertainty, :stdscore, :weightedmean, :±,
:DetectorId, :ChannelId
])

const _ljlexpr_units = IdDict([
const _ljlexpr_units = IdDict{Symbol,Expr}([
:s => :(u"s"),
:ms => :(u"ms"),
:μs => :(u"μs"),
Expand Down Expand Up @@ -95,17 +99,44 @@
function _process_ljlexpr_impl(@nospecialize(expr::Expr), @nospecialize(f_varsubst))
_process_inner = Base.Fix2(_process_ljlexpr_impl, f_varsubst)
if expr.head in ljl_expr_allowed_heads
if expr.head == :call
if expr.head == :.
if length(expr.args) == 1
arg1 = only(expr.args)
if arg1 isa Symbol

Check warning on line 105 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L104-L105

Added lines #L104 - L105 were not covered by tests
# Standalone dot-operator syntax:
expr

Check warning on line 107 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L107

Added line #L107 was not covered by tests
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 109 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L109

Added line #L109 was not covered by tests
end
elseif length(expr.args) == 2
arg1 = expr.args[begin]
arg2 = expr.args[begin+1]
if arg2 isa Union{Symbol,QuoteNode}
# Property access
return Expr(expr.head, _process_ljlexpr_impl(arg1, f_varsubst), arg2)
elseif arg2 isa Expr && arg2.head == :tuple
# Broadcast syntax
return Expr(expr.head, arg1, Expr(:tuple, map(_process_inner, arg2.args)...))
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 121 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L121

Added line #L121 was not covered by tests
end
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 124 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L124

Added line #L124 was not covered by tests
end
elseif expr.head == :call
funcname = expr.args[begin]
funcargs = expr.args[2:end]
if funcname in ljl_expr_allowed_funcs
funcname_str = string(funcname)
# Handle constructs like `a .+ b`:
base_funcname = funcname_str[begin] == '.' ? Symbol(funcname_str[begin+1:end]) : funcname
funcargs = expr.args[begin+1:end]
if base_funcname in ljl_expr_allowed_funcs
return Expr(expr.head, funcname, map(_process_inner, funcargs)...)
else
throw(ArgumentError("Function \"$(funcname)\" not allowed in LEGEND Julia expression."))
end
elseif expr.head == :macrocall
macro_name = expr.args[begin]
macro_args = expr.args[2:end]
macro_args = expr.args[begin+1:end]

Check warning on line 139 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L139

Added line #L139 was not covered by tests
if macro_name == Symbol("@u_str")
return Expr(expr.head, macro_name, macro_args...)
else
Expand Down
30 changes: 18 additions & 12 deletions test/test_ljl_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,40 @@ using Test
using PropertyFunctions, StructArrays
import Measurements

using PropDicts: PropDict

include("testing_utils.jl")

@testset "legend_expressions" begin
data = StructArray([
(E_trap = 21092, offs = 0.01, slope = 0.73, A = 328.2, force_accept = false),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = NaN, force_accept = false),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = NaN, force_accept = true),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = 328.2, force_accept = false, a = (b = [1,41,3], c = 51.1)),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = NaN, force_accept = false, a = (b = [1,42,3], c = 32.0)),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = NaN, force_accept = true, a = (b = [1,41,3], c = 51.1)),
(E_trap = 21092, offs = 0.01, slope = 0.73, A = NaN, force_accept = true, a = (b = [1,42,3], c = 32.03)),
])

bool_expr_string = "E_trap > 0 && !isnan(A) && !isinf(E_trap) || force_accept"
bool_expr_string = "E_trap > 0 && !isnan(A) && !isinf(E_trap) || force_accept && a.b[2] > a.c"
bool_expr = parse_ljlexpr(bool_expr_string)
@test bool_expr == :(E_trap > 0 && (!(isnan(A)) && !(isinf(E_trap))) || force_accept)
ref_boolfunc(x) = (x.E_trap > 0 && !(isnan(x.A)) && !(isinf(x.E_trap)) || x.force_accept)
@test bool_expr == :(E_trap > 0 && (!(isnan(A)) && !(isinf(E_trap))) || force_accept && a.b[2] > a.c)
ref_boolfunc(x) = (x.E_trap > 0 && !(isnan(x.A)) && !(isinf(x.E_trap)) || x.force_accept && x.a.b[2] > x.a.c)
bool_pf = ljl_propfunc(bool_expr)
@test bool_pf isa PropertyFunctions.PropertyFunction
@test bool_pf === ljl_propfunc(bool_expr_string)
@test @inferred(broadcast(bool_pf, data)) == ref_boolfunc.(data)

num_expr_string = "(offs + slope * abs(E_trap) / 1000) - 5.2"
num_expr_string = "(offs + slope * abs(E_trap) / 1000) - 5.2 + a.b[2]/100 * a.c/50"
num_expr = parse_ljlexpr(num_expr_string)
@test num_expr == :((offs + (slope * abs(E_trap)) / 1000) - 5.2)
ref_numfunc(x) = (x.offs + (x.slope * abs(x.E_trap)) / 1000) - 5.2
@test num_expr == :((offs + (slope * abs(E_trap)) / 1000) - 5.2 + a.b[2]/100 * a.c/50)
ref_numfunc(x) = (x.offs + (x.slope * abs(x.E_trap)) / 1000) - 5.2 + x.a.b[2]/100 * x.a.c/50
num_pf = ljl_propfunc(num_expr)
@test num_pf isa PropertyFunctions.PropertyFunction
@test num_pf === ljl_propfunc(num_expr_string)
@test @inferred(broadcast(num_pf, data)) == ref_numfunc.(data)

meas_expr_string = "(offs + slope * abs(E_trap) / 1000) - (5.2 ± 0.1)"
meas_expr_string = "(offs + (slope > 0.5 ? one(slope) : zero(slope)) * abs(E_trap) / 1000) - (5.2 ± 0.1)"
meas_expr = parse_ljlexpr(meas_expr_string)
@test meas_expr == :((offs + (slope * abs(E_trap)) / 1000) - (5.2 ± 0.1))
ref_measfunc(x) = (x.offs + (x.slope * abs(x.E_trap)) / 1000) - Measurements.:(±)(5.2, 0.1)
@test meas_expr == :((offs + ((slope > 0.5 ? one(slope) : zero(slope)) * abs(E_trap)) / 1000) - (5.2 ± 0.1))
ref_measfunc(x) = (x.offs + ((x.slope > 0.5 ? one(x.slope) : zero(x.slope)) * abs(x.E_trap)) / 1000) - Measurements.:(±)(5.2, 0.1)
meas_pf = ljl_propfunc(meas_expr)
@test meas_pf isa PropertyFunctions.PropertyFunction
@test meas_pf === ljl_propfunc(meas_expr_string)
Expand All @@ -46,4 +49,7 @@ include("testing_utils.jl")
multi_pf = ljl_propfunc(expr_map)
@test multi_pf isa PropertyFunctions.PropertyFunction
@test @inferred(broadcast(multi_pf, data)) == StructArray(e_cal = ref_numfunc.(data), e_flag = ref_boolfunc.(data))

@test @inferred(ljl_propfunc("a.+b")((a = [1,2,3], b = [5, 6, 7]))) == [6, 8, 10]
@test @inferred(ljl_propfunc("log.(a)")((a = [1,2,3],))) ≈ [0.0, 0.6931471805599453, 1.0986122886681098]
end
Loading