From 340325c8fedb8ae971e55ffc4ac9c7a9babd7ba6 Mon Sep 17 00:00:00 2001 From: Kipton Barros Date: Wed, 25 Sep 2024 08:03:25 -0600 Subject: [PATCH] Tweaks --- docs/src/versions.md | 1 - src/Binning/Binning.jl | 23 ++++++++++++----------- src/Binning/ExperimentData.jl | 5 ++--- test/test_binning.jl | 7 +++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/docs/src/versions.md b/docs/src/versions.md index 75add4c10..b5071f453 100644 --- a/docs/src/versions.md +++ b/docs/src/versions.md @@ -9,7 +9,6 @@ polynomial order according to an error tolerance. * Rename mode `:dipole_large_S` to `:dipole_uncorrected` to emphasize that corrections are missing. -* Binning features re-introduced internally for testing. ## v0.7.2 (Sep 11, 2024) diff --git a/src/Binning/Binning.jl b/src/Binning/Binning.jl index 8c672f312..b439c897e 100644 --- a/src/Binning/Binning.jl +++ b/src/Binning/Binning.jl @@ -79,13 +79,13 @@ end function specify_transverse_binning(q_path::QPath, first_transverse_axis, second_transverse_axis, first_transverse_binwidth, second_transverse_binwidth) # Ensure path is non-empty and single-segment if length(q_path.qs) < 2 - error("Q path must have at least two points to infer bin width") + error("q-path must have at least two points to infer bin width") end Δq = q_path.qs[2] - q_path.qs[1] if !all(isapprox(Δq), diff(q_path.qs)) - error("Q path is multi-segment or irregular!") + error("q-path is multi-segment or irregular!") end binstart = zero(MVector{4, Float64}) @@ -117,7 +117,7 @@ function specify_transverse_binning(q_path::QPath, first_transverse_axis, second binstart[1:3] .= coords_start[1:3] .- binwidth[1:3]/2 binend[1:3] .= coords_end[1:3] - # Check the original Q points end up in bin centers + # Check the original q points end up in bin centers in_bin(q) = (covectors[1:3, 1:3] * q .- binstart[1:3]) ./ binwidth[1:3] centering_error(q) = (in_bin(q) .- 0.5) .- (round.(Int64,in_bin(q) .- 0.5)) @assert all(norm(centering_error(q)) < 1e-12 for q in q_path.qs) @@ -133,14 +133,14 @@ end function specify_transverse_binning(q_grid::QGrid{2}, transverse_axis, transverse_binwidth) # Ensure grid is non-empty and single-segment if size(q_grid.qs, 1) < 2 || size(q_grid.qs, 2) < 2 - error("2D Q grid must have at least two points in each direction") + error("2D q-grid must have at least two points in each direction") end Δq1 = q_grid.qs[2,1] - q_grid.qs[1,1] Δq2 = q_grid.qs[1,2] - q_grid.qs[1,1] if !all(isapprox(Δq1), diff(q_grid.qs, dims=1)) || !all(isapprox(Δq2), diff(q_grid.qs, dims=2)) - error("2D Q grid is irregular!") + error("2D q-grid is irregular!") end binstart = zero(MVector{4, Float64}) @@ -175,10 +175,11 @@ function specify_transverse_binning(q_grid::QGrid{2}, transverse_axis, transvers binstart[1:3] .= coords_start[1:3] .- binwidth[1:3]/2 binend[1:3] .= coords_end[1:3] - # Check the original Q points end up in bin centers + # Check the original q points end up in bin centers in_bin(q) = (covectors[1:3, 1:3] * q .- binstart[1:3]) ./ binwidth[1:3] centering_error(q) = (in_bin(q) .- 0.5) .- (round.(Int64, in_bin(q) .- 0.5)) - @assert all(norm(centering_error(q)) < 1e-12 for q in q_grid.qs) + # TODO: Understand loss of precision here (1e-13 will fail tests) + @assert all(norm(centering_error(q)) < 1e-10 for q in q_grid.qs) # Energy axis covectors[4,:] .= [0,0,0,1] @@ -197,15 +198,15 @@ function unit_resolution_binning_parameters(sc; negative_energies=true) good_qs = available_wave_vectors(sc) numbins = (size(good_qs)..., length(ωvals)) # Bin centers should be at Sunny scattering vectors - maxQ = 1 .- (1 ./ numbins) + maxq = 1 .- (1 ./ numbins) min_val = (0., 0., 0., minimum(ωvals)) - max_val = (maxQ[1], maxQ[2], maxQ[3], maximum(ωvals)) + max_val = (maxq[1], maxq[2], maxq[3], maximum(ωvals)) total_size = max_val .- min_val binwidth = total_size ./ (numbins .- 1) binstart = (0., 0., 0., minimum(ωvals)) .- (binwidth ./ 2) - binend = (maxQ[1], maxQ[2], maxQ[3], maximum(ωvals)) # bin end is well inside of last bin + binend = (maxq[1], maxq[2], maxq[3], maximum(ωvals)) # bin end is well inside of last bin params = BinningParameters(binstart, binend, binwidth, I(4)) @@ -341,7 +342,7 @@ function binned_intensities(sc, params::BinningParameters; kT=nothing, integrate error("Can't broaden if data is not energy-resolved") end - # Decide on which Q points can possibly contribute (depends on geometry of + # Decide on which q points can possibly contribute (depends on geometry of # supercell and params) lower_aabb_q, upper_aabb_q = binning_parameters_aabb(params) # Round the axis-aligned bounding box *outwards* to lattice sites diff --git a/src/Binning/ExperimentData.jl b/src/Binning/ExperimentData.jl index 1c0c5296f..b158d9dcf 100644 --- a/src/Binning/ExperimentData.jl +++ b/src/Binning/ExperimentData.jl @@ -155,10 +155,9 @@ function Base.permutedims(params::BinningParameters, perm) out end -# Parse the "[0.5H,0.3H,0.1H]" type of Mantid string describing -# a histogram axis +# Parse "[0.5H,0.3H,0.1H]" into the Julia vector [0.5, 0.3, 0.1]. These strings +# typically arises as the Mantid histogram axis label. function parse_long_name(long_name) - # You're welcome found = match(r"\[(|0|(?:-?[0-9]?(?:\.[0-9]+)?))[HKL]?,(|0|(?:-?[0-9]?(?:\.[0-9]+)?))[HKL]?,(|0|(?:-?[0-9]?(?:\.[0-9]+)?))[HKL]?\]", long_name) if isnothing(found) return nothing diff --git a/test/test_binning.jl b/test/test_binning.jl index 151159c2d..100f6fceb 100644 --- a/test/test_binning.jl +++ b/test/test_binning.jl @@ -1,3 +1,10 @@ + +@testitem "Parse nxs" begin + @test Sunny.parse_long_name("[0.5H,0.3K,0.1H]") == [0.5, 0.3, 0.1] + # TODO: Add stripped down .nxs files of various flavors +end + + @testitem "Binning" begin using LinearAlgebra