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

Bug fix lprops #85

Merged
merged 1 commit into from
Dec 10, 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"
fhagemann marked this conversation as resolved.
Show resolved Hide resolved

[weakdeps]
LegendDataTypes = "99e09c13-5545-5ee2-bfa2-77f358fb75d8"
Expand Down Expand Up @@ -73,4 +74,5 @@ Tables = "1.1"
TypedTables = "1.4"
UUIDs = "<0.0.1, 1"
Unitful = "1.11"
UnitfulAtomic = "1"
julia = "1.10"
1 change: 1 addition & 0 deletions src/LegendDataManagement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using PropDicts
using PropertyDicts
using StructArrays
using Unitful
using UnitfulAtomic
using Measurements
using Measurements: ±, value, uncertainty

Expand Down
29 changes: 26 additions & 3 deletions src/lprops.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
# This file is a part of LegendDataManagement.jl, licensed under the MIT License (MIT).

const _pseudo_unit_expr = r"(^|[^A-Za-z])(ADC|adc|sample)([^A-Za-z]|$)"

function units_from_string(s::AbstractString)
if isempty(s) || s == "none"
NoUnits

Check warning on line 7 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L7

Added line #L7 was not covered by tests
elseif !isnothing(match(_pseudo_unit_expr, s))
NoUnits

Check warning on line 9 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L9

Added line #L9 was not covered by tests
else
try
uparse(s, unit_context=[Unitful, UnitfulAtomic])
catch e
s == "e" && return u"e_au" # parse "e" as u"e_au" from UnitfulAtomic
if e isa ErrorException
rethrow(ArgumentError("Unknown physical unit \"$s\""))

Check warning on line 16 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
else
rethrow(e)

Check warning on line 18 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L18

Added line #L18 was not covered by tests
end
end
end
end

units_to_string(u::Unitful.Unitlike) = string(u)

Check warning on line 24 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L24

Added line #L24 was not covered by tests

function _props2lprops(pd::PropDict)
if haskey(pd, :val) && length(keys(pd)) <= 3 && (haskey(pd, :unit) || haskey(pd, :err))
if haskey(pd, :unit)
if haskey(pd, :err)
Unitful.Quantity.(measurement.(pd.val, ifelse(isnothing(pd.err), NaN, pd.err)), Unitful.uparse(pd.unit))
Unitful.Quantity.(measurement.(pd.val, ifelse(isnothing(pd.err), NaN, pd.err)), units_from_string(pd.unit))
else
Unitful.Quantity.(pd.val, Unitful.uparse(pd.unit))
Unitful.Quantity.(pd.val, units_from_string(pd.unit))
end
elseif haskey(pd, :err)
measurement.(pd.val, ifelse(isnothing(pd.err), NaN, pd.err))
else
throw(ArgumentError("_props2lprops can't handle PropDict $pd"))
end
elseif haskey(pd, :unit) && length(keys(pd)) == 1
Unitful.Quantity(NaN, Unitful.uparse(pd.unit))
Unitful.Quantity(NaN, units_from_string(pd.unit))

Check warning on line 40 in src/lprops.jl

View check run for this annotation

Codecov / codecov/patch

src/lprops.jl#L40

Added line #L40 was not covered by tests
else
PropDict(Dict([key => _props2lprops(val) for (key, val) in pd]))
end
Expand Down
Loading