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

feat: add grow nexus script #13

Merged
merged 3 commits into from
Mar 14, 2024
Merged

feat: add grow nexus script #13

merged 3 commits into from
Mar 14, 2024

Conversation

jokasimr
Copy link
Contributor

@jokasimr jokasimr commented Mar 8, 2024

Fixes #12

@jokasimr jokasimr requested a review from nvaytet March 11, 2024 07:57
@jokasimr jokasimr marked this pull request as ready for review March 11, 2024 07:57
)

detector_scale = (
args.detector_scale if args.detector_scale < 1 else round(args.detector_scale)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem to agree with the int type for scale in _scale_group?
If the scale of 0.5 is given, 0.5 is passed on.

Also, in the issue it is mentioned that the option to scale with < 1 could be removed as not useful? Would simplify some things here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type in _scale_group is int | float right?

Copy link
Contributor Author

@jokasimr jokasimr Mar 11, 2024

Choose a reason for hiding this comment

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

Also, in the issue it is mentioned that the option to scale with < 1 could be removed as not useful? Would simplify some things here?

Aha yeah I missed that comment, in that case the scale can be changed to just int.

args.detector_scale if args.detector_scale < 1 else round(args.detector_scale)
)
monitor_scale = (
round(args.monitor_scale)
Copy link
Member

Choose a reason for hiding this comment

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

This would be in disagreement with the detector scale if a monitor_scale = 0.5 is given?
Again, probably removing the ability to scale with < 1 would help?

"-s",
"--detector-scale",
type=float,
default=2,
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 we should have a default argument, but require at least the detector scale.

default=2,
help=(
'Scale factor to multiply the number of events. '
'If the factor is greater than 1 it is rounded to the nearest integer '
Copy link
Member

Choose a reason for hiding this comment

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

If we remove the option of downscaling, we could request only integers with argparse?

def test_grow_nexus_unchanged(nexus_file):
grow_nexus_file(nexus_file, 1.0, 1.0)
assert nexus_file['entry/instrument/detector/event_data/event_index'].size == 3
assert nexus_file['entry/instrument/detector/event_data/event_id'].size == 6
Copy link
Member

Choose a reason for hiding this comment

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

You should also check monitor events.


def test_grow_nexus_double(nexus_file):
grow_nexus_file(nexus_file, 2.0, 2.0)
assert nexus_file['entry/instrument/detector/event_data/event_index'].size == 3
Copy link
Member

Choose a reason for hiding this comment

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

Can you check the actual values of the event_index instead of just the length?

Comment on lines 3 to 5

h5py

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 this be in basetest.in?

Copy link
Contributor Author

@jokasimr jokasimr Mar 11, 2024

Choose a reason for hiding this comment

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

Added it in base because it is needed to run the script I see the mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should it be an optional dependency?

Copy link
Member

Choose a reason for hiding this comment

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

Dependencies of the package are defined in pyproject.toml. base.in is just for CI.

I think this should NOT be a dependency of the package (even though making it optional is an option), so not touching pyproject.toml is fine. But unless we want to script to be runnable in CI jobs other than the tests, I think it should be added in basetest.in, not base.in.

That being said: Since the package depends on ScippNexus, which pulls in h5py I do not think you actually have to add it anywhere?

@jokasimr jokasimr force-pushed the grow-events-script branch 2 times, most recently from d8ee6b6 to 2f41aed Compare March 11, 2024 12:42
@jokasimr jokasimr enabled auto-merge March 11, 2024 12:43
Copy link
Member

Choose a reason for hiding this comment

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

Should this be part of the package? I.e. under src/ess/reduce? Do users of ESSreduce need to run this script or is it more of a help for devs and can be used directly from the project source?

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 it is useful. Why not (at least for now)?

@jokasimr jokasimr force-pushed the grow-events-script branch from 2f41aed to 2fac997 Compare March 12, 2024 08:39
@pytest.mark.parametrize('detector', ('detector', 'monitor'))
def test_grow_nexus_unchanged(nexus_file, detector):
grow_nexus_file(nexus_file, 1, 1)
np.testing.assert_almost_equal(
Copy link
Member

Choose a reason for hiding this comment

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

Can we use assert_equal? We're dealing with integers, we don't need to be approximate?

np.testing.assert_almost_equal(
2 * [1, 2, 1, 2, 1, 2],
nexus_file[f'entry/instrument/{detector}/event_data/event_time_offset'][()],
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggestions:

  • add a test where scaling for monitor and detector are different?
  • add a test where scaling for monitor is not given?


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I've only noticed this now, but I would suggest to maybe return a new file, instead of modifying the input file in-place?
This would mean we also need an output file argument.

I don't know if this would make usability worse?
grow_nexus -f small.nxs -o large.nxs -s 2
vs
grow_nexus myfile.nxs 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I agree it's better to create a new file. Very annoying if it automatically overwrites the file when you didn't expect that. I don't think the usability difference is a problem.

@jokasimr jokasimr force-pushed the grow-events-script branch from f26dcde to 44e8e9c Compare March 14, 2024 12:00
@jokasimr jokasimr merged commit a17457f into main Mar 14, 2024
3 checks passed
@jokasimr jokasimr deleted the grow-events-script branch March 14, 2024 12:50
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.

Script for "growing" NeXus event-data
4 participants