SunnySuite / Sunny.jl

Spin dynamics and generalization to SU(N) coherent states
Other
86 stars 19 forks source link

Add `binned_histogram` #84

Closed Lazersmoke closed 1 year ago

Lazersmoke commented 1 year ago

This isn't finished/polished yet (opening this PR to track progress).

This is the alternative interface to intensities which is designed to more closely align with what Mantid/Horace output. In particular, the new type BinningParameters is supposed to be isomorphic to the data Mantid uses to describe an "MDHistoWorkspace".

Example usage:

# Histogram a 1D cut with from [0,1,0] to [1/2,0,0] (with transverse width 0.3)
params = Sunny.one_dimensional_cut_binning_parameters(sf,[0,1,0],[1/2,0,0],30,0.3)
display(params)

# sunHist: Histogramed intensity data
# sunCounts: Number of scattering vectors per bin (NOT uniform)
sunHist,sunCounts = Sunny.binned_histogram(sf,params,Sunny.DipoleFactor(sf),kT,formfactors)

# Axes labelled by bin centers
mx = Sunny.axes_bincenters(params)

fig3 = Plots.heatmap(mx[1],mx[4],sunCounts[:,1,1,:]')
logIntensity = log10.(sunHist[:,1,1,:]' ./ sunCounts[:,1,1,:]')
fig4 = Plots.heatmap(mx[1],mx[4],logIntensity;ylabel="E (meV)",color=cgrad(:viridis,scale = :lin,rev = false),xticks = ([mx[1][1],mx[1][end]], ["[0,1,0]","[1/2,0,0]"]))

display(Plots.plot(fig3,fig4;layout = @layout([a{0.2h};b])))

The resulting intensity histogram looks something like this:

image

Lazersmoke commented 1 year ago

TODO's on this include:

kbarros commented 1 year ago

This sounds relevant to @ddahlbom

Lazersmoke commented 1 year ago

Additional TODO on this, per discussion: need to incorporate broadening into the inner histogram loop, same as LSWT code.

Lazersmoke commented 1 year ago

@ddahlbom Currently, the binned intensities functions (linear and spherical) do not handle the form factors, contractors, and possibly kT correctly, since they bypass the logic in intensities. What do you + @kbarros recommend to do about the interface for this? I may just finish implementing the binning internals and then turn it over to y'all for making the final user interface arrangements for these parameters (e.g. following the google doc)

ddahlbom commented 1 year ago

@Lazersmoke Sure, that sounds great. Just let us know when when you're done making the final touches. I'd be happy to incorporate the rest of the intensities logic afterwards.

Lazersmoke commented 1 year ago

OK, the final touches are done :)

I've added examples of binned version to the FeI2 and powder averaging tutorials. Some of the plotting is a bit rough around the edges.

There's a remaining question about the exact behavior of the "partial bins" at the far edge of the histogram, sometimes the behavior is possibly not quite correct, but the issue is that it's a bit under-specified what should be done in that case. Mantid has a bunch of options on like every function call that specify what to do with them, seems like. It shouldn't be a big issue for most use cases.

Remaining items for @ddahlbom and co:

ddahlbom commented 1 year ago

Thanks @Lazersmoke ! This is a great addition. I'll take a closer look at the code soon. If there are any changes that seem like they should be made before merging, I will comment here.

(I suppose we may want to merge sooner rather than later to keep things in sync. I can complete the remaining tasks after this is integrated.)