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

Time binning reduction and export method. #21

Merged
merged 24 commits into from
Feb 14, 2024
Merged

Conversation

YooSunYoung
Copy link
Member

@YooSunYoung YooSunYoung commented Jan 11, 2024

Fixes #16
Fixes #3

Main Changes

Exporting reduced data as nexus file.

The exporting method was implemented together with the time binning so that it is more clear what to save into the result.

Smaller steps as parameters.

event weights converter and proton_charge_from_event_data are set as parameters.
This solution minimizes the fraction of the McStas specific part in the workflow graph,
yet allow users to access to the smaller steps of the McStas loader.

The reason of having them as parameters not as providers is,

  • They are not part of general reduction, which are only for McStas cases.
    And we wanted to keep the McStas specific parts as compact as possible, i.e. not exposing all the magic numbers as parameters. Also because it'll be almost never changed.

  • They are better done while the file is open and read in the loader.
    This prevented splitting the loader function into smaller steps.

TODO

  • Update instrument view scale unit to counts.
  • Write tests of the export method.
  • Write tests for the crystal rotation angle.
  • Write tests for all necessary fields
  • Use chexus (?) I'll take this out as a separate issue

@YooSunYoung YooSunYoung marked this pull request as ready for review February 7, 2024 13:09
@YooSunYoung
Copy link
Member Author

I've got another idea of how to deal with smaller step so I'll put it to draft for now...!

@YooSunYoung YooSunYoung marked this pull request as draft February 8, 2024 12:26
@YooSunYoung YooSunYoung marked this pull request as ready for review February 8, 2024 13:35
@jl-wynen
Copy link
Member

jl-wynen commented Feb 9, 2024

I've got another idea of how to deal with smaller step so I'll put it to draft for now...!

I'm not familiar with this code or how it used to work. Can you point me to your solution for this in particular?

def load_mcstas_nexus(
file_path: InputFilepath,
event_weights_converter: McStasEventWeightsConverter,
proton_charge_converter: McStasProtonChargeConverter,
Copy link
Member Author

Choose a reason for hiding this comment

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

I've got another idea of how to deal with smaller step so I'll put it to draft for now...!

I'm not familiar with this code or how it used to work. Can you point me to your solution for this in particular?

It's this part I added.
It's explained in more detail here on the top: #21 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

This pretty much makes this function unusable to load a file "by hand", without a workflow. Is this the simplest option, and does it solve an actual problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not...?
All the converters are included in the package so you can just inject the functions by hand.

Or we can make them Optional and set default values. Should we...?

Copy link
Member

Choose a reason for hiding this comment

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

I meant UX wise.

Yes, there should be defaults.

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't fix real problems, but it does fix the readability/accessibility problems,
to avoid duplicating/verbose documentation and hidden smaller steps in the loader.

In this way, each steps are easily exposed even though they are smaller steps in the loader to users
and we don't have to duplicate all the documentation what each step does.
You could avoid it by not separating those steps into smaller functions,
but then it becomes very verbose and less readable.

Comment on lines 18 to 20
McStasEventWeightsConverter = Callable[..., ConvertedEventWeights]
# It should be ``Callable[[MaximumProbability, sc.Variable], ConvertedEventWeights]``
# but sciline pipeline breaks if there is a list in the type arguments.
Copy link
Member

Choose a reason for hiding this comment

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

Use NewType?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just tried using NewType instead, and then documentation generation fails.
It doesn't throw any errors but it doesn't have any details either...

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Wait maybe it's not related...

Copy link
Member Author

Choose a reason for hiding this comment

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

Right...! It was not related, sorry...! I fixed it!


def event_weights_from_probability(
max_probability: MaximumProbability, probabilities: sc.Variable
) -> ConvertedEventWeights:
Copy link
Member

Choose a reason for hiding this comment

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

How about renaming this to EventWeights? The "converted" is a bit meaningless, and it may be easier to find a good name for the "unconverted" ones (such as McStasWeights, RawProbability, ...).

Comment on lines +144 to +145
# Arbitrary number to scale the proton charge
_proton_charge_scale_factor = sc.scalar(1 / 10_000, unit=None)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't there be more protons than neutrons?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... I don't remember...
@Justin-Bergmann should this scale factor be > 1 ...?

return grouped.fold(dim='id', sizes={'panel': num_panels, 'id': -1})


def proton_charge_from_event_data(event_da: sc.DataArray) -> ProtonCharge:
Copy link
Member

Choose a reason for hiding this comment

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

Not sure which event_da is passed here, but shouldn't it be a monitor instead of detector data? Otherwise we include the scattering effects, which may be problematic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I asked about this before.
Justin said there should be easier/more precise ways to integrate number of protons during the measurements in the real experiments...
So, for simulation data, this proton_charge just need to be proportional to the number of events.
And the Monitor is not included in the simulation data... so it was the only way...?

Copy link
Member

Choose a reason for hiding this comment

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

Ok!

def load_mcstas_nexus(
file_path: InputFilepath,
event_weights_converter: McStasEventWeightsConverter,
proton_charge_converter: McStasProtonChargeConverter,
Copy link
Member

Choose a reason for hiding this comment

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

This pretty much makes this function unusable to load a file "by hand", without a workflow. Is this the simplest option, and does it solve an actual problem?

@YooSunYoung YooSunYoung merged commit b0a729c into main Feb 14, 2024
3 checks passed
@YooSunYoung YooSunYoung deleted the export-mcstas-result branch February 14, 2024 08:23
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.

Save results into nexus format. Data reduction workflow skeleton for NMX.
3 participants