Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarros committed Sep 25, 2024
1 parent 4c0ca07 commit 340325c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
1 change: 0 additions & 1 deletion docs/src/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions src/Binning/Binning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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]
Expand All @@ -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))

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Binning/ExperimentData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_binning.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 340325c

Please sign in to comment.