From 008309cb100c4ab43c597ddecce448cdc1a64181 Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Thu, 5 Mar 2026 16:15:51 +0000 Subject: [PATCH 1/6] Add possibility to set a different data entry key to methods - still needs to go in cli --- src/nexgen/beamlines/I19_2_nxs.py | 9 ++++++++- src/nexgen/nxs_write/nxmx_writer.py | 10 ++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/nexgen/beamlines/I19_2_nxs.py b/src/nexgen/beamlines/I19_2_nxs.py index 5145fff0..98d1fbf4 100644 --- a/src/nexgen/beamlines/I19_2_nxs.py +++ b/src/nexgen/beamlines/I19_2_nxs.py @@ -229,6 +229,7 @@ def eiger_writer( n_frames: int | None = None, vds_offset: int = 0, notes: dict[str, Any] | None = None, + data_entry_key: str = "data", ): """ A function to call the NXmx nexus file writer for Eiger 2X 4M detector. @@ -248,6 +249,8 @@ def eiger_writer( vds_offset (int, optional): Start index for the vds writer. Defaults to 0. notes (dict[str, Any], optional): Dictionary of (key, value) pairs where key represents the \ dataset name and value its data. Defaults to None. + data_entry_key (str, optional): Dataset entry key in datafiles. eg. for gating mode it's data1.\ + Defaults to data. Raises: ValueError: If use_meta is set to False but axes_pos and det_pos haven't been passed. @@ -424,7 +427,11 @@ def eiger_writer( TR.tot_num_images, sample, ) - NXmx_writer.write(image_filename=image_filename, start_time=timestamps[0]) + NXmx_writer.write( + image_filename=image_filename, + start_time=timestamps[0], + data_entry_key=data_entry_key, + ) NXmx_writer.write_vds( vds_offset=vds_offset, vds_shape=(n_frames, *detector.detector_params.image_size), diff --git a/src/nexgen/nxs_write/nxmx_writer.py b/src/nexgen/nxs_write/nxmx_writer.py index 95783822..ec7ef70f 100644 --- a/src/nexgen/nxs_write/nxmx_writer.py +++ b/src/nexgen/nxs_write/nxmx_writer.py @@ -142,6 +142,7 @@ def write( est_end_time: datetime | str | None = None, write_mode: str = "x", add_non_standard: bool = True, + data_entry_key: str = "data", ): """Write the NXmx format NeXus file. @@ -162,6 +163,7 @@ def write( Accepts any valid h5py file opening mode. Defaults to "x". add_non_standard (bool, optional): Flag if non-standard NXsample fields should be added \ for processing to work. Defaults to True, will change in the future. + data_entry_key (str, optional): Dataset entry key in datafiles. Defaults to data. """ metafile = self._get_meta_file(image_filename) if metafile: @@ -193,10 +195,7 @@ def write( # NXdata: entry/data write_NXdata( - nxs, - datafiles, - "images", - list(osc.keys())[0], + nxs, datafiles, "images", list(osc.keys())[0], entry_key=data_entry_key ) # NXinstrument: entry/instrument @@ -341,6 +340,7 @@ def write( start_time: datetime | str | None = None, write_mode: str = "x", add_non_standard: bool = False, + data_entry_key: str = "data", ): """Write a NXmx-like NeXus file for event mode data collections. @@ -356,6 +356,7 @@ def write( Accepts any valid h5py file opening mode. Defaults to "x". add_non_standard (bool, optional): Flag if non-standard NXsample fields should be added \ for processing to work. Defaults to False. + data_entry_key (str, optional): Dataset entry key in datafiles. Defaults to data. """ # Get metafile # No data files, just link to meta @@ -381,6 +382,7 @@ def write( [metafile], "events", list(osc.keys())[0], + entry_key=data_entry_key, ) # NXinstrument: entry/instrument From c362e7483e97b772e3925dd5e72a9f58ca55ec99 Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Fri, 6 Mar 2026 15:58:45 +0000 Subject: [PATCH 2/6] Fix some typos in i19 parser --- src/nexgen/command_line/I19_2_cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nexgen/command_line/I19_2_cli.py b/src/nexgen/command_line/I19_2_cli.py index dbd97e5a..9b6e6814 100644 --- a/src/nexgen/command_line/I19_2_cli.py +++ b/src/nexgen/command_line/I19_2_cli.py @@ -236,12 +236,12 @@ def nexgen_writer(args): ) parser_nex.add_argument("meta_file", type=str, help="Path to _meta.h5 file.") parser_nex.add_argument( - "detector-name", + "detector_name", type=str, choices=["eiger", "tristan"], help="Detector currently in use on beamline.", ) -parser_nex.add_argument("exp-time", type=float, help="Exposure time, in s.") +parser_nex.add_argument("exp_time", type=float, help="Exposure time, in s.") parser_nex.add_argument( "-n", "--num-imgs", From 529654652d50fc5de52d224cf3f9f76ec957433a Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Fri, 6 Mar 2026 15:59:57 +0000 Subject: [PATCH 3/6] Fix pre-commits --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5ff8569c..67908e98 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ repos: hooks: - id: ruff name: Run ruff - stages: [commit] + stages: [pre-commit] language: system entry: ruff check types: [python] From 1dbb4d0cbec58137fa9c8a7e2b3882a5fb5b4a1a Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Fri, 6 Mar 2026 16:01:01 +0000 Subject: [PATCH 4/6] Allow data entry key as input to command line --- src/nexgen/command_line/nexus_generator.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nexgen/command_line/nexus_generator.py b/src/nexgen/command_line/nexus_generator.py index 69bd4182..a3c7ba7b 100644 --- a/src/nexgen/command_line/nexus_generator.py +++ b/src/nexgen/command_line/nexus_generator.py @@ -141,6 +141,7 @@ def write_nxmx_cli(args): logger.debug(f"Recorded exposure time: {detector.exp_time} s.") try: + entry_key = args.data_key if args.data_key else "data" # Aaaaaaaaaaaand write if params.det.mode == "images": writer = NXmxFileWriter( @@ -152,7 +153,7 @@ def write_nxmx_cli(args): params.instrument.attenuator, num_images, ) - writer.write(image_datafiles=datafiles) + writer.write(image_datafiles=datafiles, data_entry_key=entry_key) if not args.no_vds: writer.write_vds(args.vds_offset) else: @@ -382,6 +383,12 @@ def _parse_cli() -> argparse.ArgumentParser: type=str, help="New nexus filename if stem needs to be different from data files.", ) + nxmx_parser.add_argument( + "--data-key", + type=str, + default="data", + help="Data entry key of dataset in raw .h5 file. Defaults to data.", + ) nxmx_parser.set_defaults(func=write_nxmx_cli) demo_parser = subparsers.add_parser( "2", From df5bab34cbe301e109ff38cc498e354702a4a0aa Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Fri, 6 Mar 2026 16:01:49 +0000 Subject: [PATCH 5/6] Sort found datafiles --- src/nexgen/command_line/nexus_generator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nexgen/command_line/nexus_generator.py b/src/nexgen/command_line/nexus_generator.py index a3c7ba7b..b307043e 100644 --- a/src/nexgen/command_line/nexus_generator.py +++ b/src/nexgen/command_line/nexus_generator.py @@ -48,6 +48,7 @@ def _get_datafiles(filepath: Path, filename_root: str) -> list[Path]: for f in glob.glob(_file_template.as_posix()) if "meta.h5" not in f ] + datafiles.sort() return datafiles From 17213b926655ebc4c52324053b51de673f337837 Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Fri, 6 Mar 2026 16:04:07 +0000 Subject: [PATCH 6/6] Add entry_key choice to I19 cli too --- src/nexgen/beamlines/I19_2_nxs.py | 4 ++++ src/nexgen/command_line/I19_2_cli.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/nexgen/beamlines/I19_2_nxs.py b/src/nexgen/beamlines/I19_2_nxs.py index 98d1fbf4..64bcc612 100644 --- a/src/nexgen/beamlines/I19_2_nxs.py +++ b/src/nexgen/beamlines/I19_2_nxs.py @@ -522,6 +522,7 @@ def nexus_writer( master_file: Path, timestamps: tuple[datetime, datetime] = (None, None), use_meta: bool = False, + data_entry_key: str = "data", ): """Wrapper function to gather all parameters from the beamline and kick off the nexus writer for a \ standard experiment on I19-2. @@ -533,6 +534,8 @@ def nexus_writer( Defaults to (None, None). use_meta (bool, optional): Eiger option only, if True use metadata from meta.h5 file. Otherwise \ all parameters will need to be passed manually. Defaults to False. + data_entry_key (str, optional): Dataset entry key in datafiles. eg. for gating mode it's data1.\ + Defaults to data. """ collection_params = CollectionParams(**params) wdir = master_file.parent @@ -599,6 +602,7 @@ def nexus_writer( collection_params, timestamps, use_meta, + data_entry_key=data_entry_key, ) case DetectorName.TRISTAN: tristan_writer( diff --git a/src/nexgen/command_line/I19_2_cli.py b/src/nexgen/command_line/I19_2_cli.py index 9b6e6814..61d913c8 100644 --- a/src/nexgen/command_line/I19_2_cli.py +++ b/src/nexgen/command_line/I19_2_cli.py @@ -142,6 +142,7 @@ def nexgen_writer(args): master_file, (_start, _stop), args.use_meta, + data_entry_key=args.data_key, ) @@ -297,6 +298,12 @@ def nexgen_writer(args): help="Number of frames in the nexus and vds file. Only passed if different from total number \ of images collected.", ) +parser_nex.add_argument( + "--data-key", + type=str, + default="data", + help="Data entry key of dataset in raw .h5 file. Defaults to data.", +) parser_nex.set_defaults(func=nexgen_writer)