Skip to content

Use normalisation in detector/q space to account for pixel differences #97

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

Merged
merged 99 commits into from
Mar 29, 2022

Conversation

arm61
Copy link
Collaborator

@arm61 arm61 commented Feb 1, 2022

A first draft (please comment on the text cause I imagine the explanation isn't great) going through performing the normalisation on dimensions of 'detector_id' and 'Q' instead of just 'Q'.

@arm61 arm61 requested review from nvaytet and jl-wynen February 1, 2022 10:27
Copy link
Member

@jl-wynen jl-wynen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the explanations are understandable.

arm61 and others added 3 commits February 1, 2022 13:11
"metadata": {},
"source": [
"And we then convert to $Q$ as well"
"However, if we want to obtain the normalisation between the sample data and the reference (accounting for detector pixel discontinuities), we want to work with the data with dimensions of `'detector_id'` and `'Q'` (the `*_q_binned` objects). "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is making reading the notebook read a bit more confusing (talking about the *q_binned quantities).
I would re-work the notebook so that this is what you want to do from the beginning.
You don't define any *_q_summed quantities, you just stick with the *_q_binned (and possibly give them a better name), just say that you're making a quick preview in Q by plotting the sum over detector_id, just as a sanity check.

And just move on to making the normalization as you want it.
There is no need to keep in the notebook the fact that we started by doing this, and then we improved it. The reader doesn't need to know about the developement history of the workflow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point. I am planning to add the footprint correction soon so I suggest that when I add that we rework the whole thing (I suggest I get a first draft soon and you can go through it). This approach might also help to understand why mean instead of sum before (cause at the moment I am not 100 % sure why mean gives the correct result and sum doesn't).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's too much work to change what I suggested, probably just a small number of cells that need changing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(cause at the moment I am not 100 % sure why mean gives the correct result and sum doesn't)

maybe we should dig down a little bit more and understand 100% before we move forward, as we may forget at some point that we were not 100% sure, and after a while everyone just assumes it's all correct?

The order of the operations is a little different now, maybe if we write them all down we can see where the mean comes from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very much agree, and in the process of second drafting this I think I am grepping it a bit better

@arm61
Copy link
Collaborator Author

arm61 commented Feb 8, 2022

I have added some new features (specifically the footprint correction and the supermirror calibration which were present in the original iteration of this code. Additionally, there has been a lot of new writing in the notebook. Feedback pls.

I will add tests in the coming days.

@arm61
Copy link
Collaborator Author

arm61 commented Feb 10, 2022

Have just spoken with the Amor instrument scientist and he recommends a slightly different footprint correction that I will look at tomorrow morning.

@arm61 arm61 marked this pull request as draft February 10, 2022 15:30
@arm61
Copy link
Collaborator Author

arm61 commented Mar 1, 2022

Rebased to main now that there is a 0.4 release.

@nvaytet
Copy link
Member

nvaytet commented Mar 7, 2022

Not sure what happened in the rebase, but I am seeing in the diff the changes from the release that are unrelated to this PR...

@arm61
Copy link
Collaborator Author

arm61 commented Mar 7, 2022

Bugger, suggestions to resolve (squash?)

@nvaytet
Copy link
Member

nvaytet commented Mar 7, 2022

How did you do your rebase? You could try merging instead?

@arm61
Copy link
Collaborator Author

arm61 commented Mar 7, 2022

Merge main?

@arm61
Copy link
Collaborator Author

arm61 commented Mar 7, 2022

How did you do your rebase?

Honestly can't remember now...

"reference_wav = reference.transform_coords([\"wavelength\"], graph=graph)\n",
"reference_wav = sc.bin(reference_wav, edges=[wavelength_edges])\n",
"reference_wav.bins.concatenate('detector_id').plot()"
"fwhm_to_std = sc.scalar(2.) * sc.sqrt(sc.scalar(2.) * sc.log(sc.scalar(2.)))\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember these were in a helper function before. Maybe we should do this again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added it as a constant in the amor.tools directory for now. Let me know if you think a function is more valuable.

"metadata": {},
"outputs": [],
"source": [
"from ess.amor.tools import FWHM_TO_STD\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also consider making a function that takes in the sample_size / beam_on_sample and then converts to std by multiplying by fwhm_to_std.

e.g.

def fwhm_to_std(x):
    return x * sc.scalar(2.) * sc.sqrt(sc.scalar(2.) * sc.log(sc.scalar(2.)))

?

Note that you also need to replace fwhm_to_std = sc.scalar(2.) * sc.sqrt(sc.scalar(2.) * sc.log(sc.scalar(2.))) further down below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My rational for the current implementation was that this allows for the inverse to give the equivalent of STD_TO_FWHM without another function.

"source": [
"normalized = sample_norm / reference_norm\n",
"normalized.coords['footprint_ratio'] = ratio\n",
"sc.plot(normalized)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the plot altogether?

"id": "64a82569-5c12-480e-a120-85b0215f1e31",
"metadata": {},
"source": [
"Finally, we perfrom a mean along the `'detector_id'` to obtain the normalised reflectometry profile.\n",
Copy link
Member

@nvaytet nvaytet Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And are you sure about the mean here? because the values at low q in the final normalized plot went from ~0.1 to ~10

If you had 3 detector pixels, before we were doing

(counts_1 + counts_2 + counts_3) / (reference_1 + reference_2 + reference_3)

and now we are doing

(counts_1/reference_1 + counts_2/reference_2 + counts_3/reference_3) / 3

?

We should check the final values against Jochen's code.

Copy link
Collaborator Author

@arm61 arm61 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we have masked pixels on reference I think that the second is the one we want...There is also the fact that using sum gives a result at looks wrong (i.e. the critical edge should be flat). Jochen's code doesn't do the normalisation, I need to go a level up to eos.py which can do the normalisation but will take quite a while to get to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, a mean doesn't really make sense, but maybe that's just me. We need to be careful about what we mean by zero counts. Is it just zero because we didn't run the experiment long enough, and statistically we would have had some counts if we recorded longer? Or does it mean zero in the sense that there simply cannot be counts there, just like if you did not have a detector pixel there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think just saying the shape looks better is enough justification, especially since the values have changed by so much. We need to understand what is correct.
The fact that there are nan values would mess up both a sum and and mean operation so that should not be a reason to use one or the other?

Copy link
Collaborator Author

@arm61 arm61 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much agreed looks right isn't good scientific justification and we still need to understand it (but it indicates it is correct). Changed by so much compared to?

The sum counts all the pixels and does no division, the mean counts the pixels that are un-masked and divides by the number of un-masked pixels. The number of unmasked pixels changes as a function of q, which is why there is a difference between our sum and our mean. And I think it makes sense to consider that weighting (with sum doesn't do).

arm61 and others added 3 commits March 16, 2022 14:15
Co-authored-by: Neil Vaytet <39047984+nvaytet@users.noreply.github.com>
Copy link
Member

@nvaytet nvaytet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had another look and changes look ok to me.

@arm61
Copy link
Collaborator Author

arm61 commented Mar 29, 2022

Same here. Happy for you to merge whenever.

@nvaytet nvaytet merged commit 07dc1ca into main Mar 29, 2022
@nvaytet nvaytet deleted the normalisation branch March 29, 2022 14:39
@arm61 arm61 mentioned this pull request Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants