From 2467ba38b831c0da9a30a93a774a7b5cab8ee1f5 Mon Sep 17 00:00:00 2001 From: Artur Glavic Date: Mon, 6 Oct 2025 11:38:52 +0200 Subject: [PATCH] Move execution script from eos.py to package and rename package to eos, add entry point to setup --- .gitignore | 2 ++ eos.py | 48 ----------------------------- {libeos => eos}/__init__.py | 2 +- eos/__main__.py | 41 ++++++++++++++++++++++++ {libeos => eos}/command_line.py | 24 ++++----------- {libeos => eos}/const.py | 0 {libeos => eos}/event_analysis.py | 0 {libeos => eos}/event_data_types.py | 0 {libeos => eos}/event_handling.py | 0 {libeos => eos}/file_reader.py | 0 {libeos => eos}/header.py | 0 {libeos => eos}/helpers.py | 0 {libeos => eos}/helpers_fallback.py | 4 +-- {libeos => eos}/helpers_numba.py | 0 {libeos => eos}/instrument.py | 0 {libeos => eos}/logconfig.py | 0 {libeos => eos}/normalisation.py | 0 {libeos => eos}/options.py | 20 ++++++------ {libeos => eos}/path_handling.py | 0 {libeos => eos}/projection.py | 0 {libeos => eos}/reduction.py | 0 setup.cfg | 10 +++--- tests/test_full_analysis.py | 2 +- windows_build.spec | 2 +- windows_folder.spec | 2 +- 25 files changed, 71 insertions(+), 86 deletions(-) delete mode 100644 eos.py rename {libeos => eos}/__init__.py (85%) create mode 100644 eos/__main__.py rename {libeos => eos}/command_line.py (63%) rename {libeos => eos}/const.py (100%) rename {libeos => eos}/event_analysis.py (100%) rename {libeos => eos}/event_data_types.py (100%) rename {libeos => eos}/event_handling.py (100%) rename {libeos => eos}/file_reader.py (100%) rename {libeos => eos}/header.py (100%) rename {libeos => eos}/helpers.py (100%) rename {libeos => eos}/helpers_fallback.py (90%) rename {libeos => eos}/helpers_numba.py (100%) rename {libeos => eos}/instrument.py (100%) rename {libeos => eos}/logconfig.py (100%) rename {libeos => eos}/normalisation.py (100%) rename {libeos => eos}/options.py (99%) rename {libeos => eos}/path_handling.py (100%) rename {libeos => eos}/projection.py (100%) rename {libeos => eos}/reduction.py (100%) diff --git a/.gitignore b/.gitignore index 7f4f5b5..d7fd4ed 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ raw test_results build dist +profile_test.prof +amor_eos.log diff --git a/eos.py b/eos.py deleted file mode 100644 index 6c27520..0000000 --- a/eos.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python -""" -eos reduces measurements performed on Amor@SINQ, PSI - -Author: Jochen Stahn (algorithms, python draft), - Artur Glavic (structuring and optimisation of code) - -conventions (not strictly followed, yet): -- array names end with the suffix '_x[y]' with the meaning - _e = events - _tof - _l = lambda - _t = theta - _z = detector z - _lz = (lambda, detector z) - _q = q_z -""" - -import logging - -from libeos.command_line import command_line_options -from libeos.logconfig import setup_logging - -#===================================================================================================== -# TODO: -# - calculate resolution using the chopperPhase -# - deal with background correction -# - format of 'call' + add '-Y' if not supplied -#===================================================================================================== - -def main(): - setup_logging() - logging.warning('######## eos - data reduction for Amor ########') - - # read command line arguments and generate classes holding configuration parameters - config = command_line_options() - - # only import heavy module if sufficient command line parameters were provieded - from libeos.reduction import AmorReduction - # Create reducer with these arguments - reducer = AmorReduction(config) - # Perform actual reduction - reducer.reduce() - - logging.info('######## eos - finished ########') - -if __name__ == '__main__': - main() diff --git a/libeos/__init__.py b/eos/__init__.py similarity index 85% rename from libeos/__init__.py rename to eos/__init__.py index c2b6640..c79cb03 100644 --- a/libeos/__init__.py +++ b/eos/__init__.py @@ -1,5 +1,5 @@ """ -Package to handle data redction at AMOR instrument to be used by eos.py script. +Package to handle data redction at AMOR instrument to be used by __main__.py script. """ __version__ = '3.0.0' diff --git a/eos/__main__.py b/eos/__main__.py new file mode 100644 index 0000000..ee4bf45 --- /dev/null +++ b/eos/__main__.py @@ -0,0 +1,41 @@ +""" +eos reduces measurements performed on Amor@SINQ, PSI + +Author: Jochen Stahn (algorithms, python draft), + Artur Glavic (structuring and optimisation of code) +""" + +import logging + +# need to do absolute import here as pyinstaller requires it +from eos.options import EOSConfig, ReaderConfig, ExperimentConfig, ReductionConfig, OutputConfig +from eos.command_line import commandLineArgs +from eos.logconfig import setup_logging, update_loglevel + +def main(): + setup_logging() + + # read command line arguments and generate classes holding configuration parameters + clas = commandLineArgs([ReaderConfig, ExperimentConfig, ReductionConfig, OutputConfig], + 'eos') + update_loglevel(clas.verbose) + + reader_config = ReaderConfig.from_args(clas) + experiment_config = ExperimentConfig.from_args(clas) + reduction_config = ReductionConfig.from_args(clas) + output_config = OutputConfig.from_args(clas) + config = EOSConfig(reader_config, experiment_config, reduction_config, output_config) + + logging.warning('######## eos - data reduction for Amor ########') + + # only import heavy module if sufficient command line parameters were provided + from eos.reduction import AmorReduction + # Create reducer with these arguments + reducer = AmorReduction(config) + # Perform actual reduction + reducer.reduce() + + logging.info('######## eos - finished ########') + +if __name__ == '__main__': + main() diff --git a/libeos/command_line.py b/eos/command_line.py similarity index 63% rename from libeos/command_line.py rename to eos/command_line.py index 5c44a96..12ad312 100644 --- a/libeos/command_line.py +++ b/eos/command_line.py @@ -1,10 +1,10 @@ import argparse -from .logconfig import update_loglevel -from .options import ReaderConfig, EOSConfig, ExperimentConfig, OutputConfig, ReductionConfig +from typing import List +from .options import ArgParsable -def commandLineArgs(): +def commandLineArgs(config_items: List[ArgParsable], program_name=None): """ Process command line argument. The type of the default values is used for conversion and validation. @@ -12,14 +12,15 @@ def commandLineArgs(): msg = "eos reads data from (one or several) raw file(s) of the .hdf format, \ performs various corrections, conversations and projections and exports\ the resulting reflectivity in an orso-compatible format." - clas = argparse.ArgumentParser(description = msg, formatter_class=argparse.ArgumentDefaultsHelpFormatter) + clas = argparse.ArgumentParser(prog=program_name, + description = msg, formatter_class=argparse.ArgumentDefaultsHelpFormatter) clas.add_argument('-v', '--verbose', action='count', default=0) clas_groups = {} all_arguments = [] - for cls in [ReaderConfig, ExperimentConfig, OutputConfig, ReductionConfig]: + for cls in config_items: all_arguments += cls.get_commandline_parameters() all_arguments.sort() # parameters are sorted alphabetically, unless they have higher priority @@ -36,16 +37,3 @@ def commandLineArgs(): ) return clas.parse_args() - - - -def command_line_options(): - clas = commandLineArgs() - update_loglevel(clas.verbose) - - reader_config = ReaderConfig.from_args(clas) - experiment_config = ExperimentConfig.from_args(clas) - reduction_config = ReductionConfig.from_args(clas) - output_config = OutputConfig.from_args(clas) - - return EOSConfig(reader_config, experiment_config, reduction_config, output_config) diff --git a/libeos/const.py b/eos/const.py similarity index 100% rename from libeos/const.py rename to eos/const.py diff --git a/libeos/event_analysis.py b/eos/event_analysis.py similarity index 100% rename from libeos/event_analysis.py rename to eos/event_analysis.py diff --git a/libeos/event_data_types.py b/eos/event_data_types.py similarity index 100% rename from libeos/event_data_types.py rename to eos/event_data_types.py diff --git a/libeos/event_handling.py b/eos/event_handling.py similarity index 100% rename from libeos/event_handling.py rename to eos/event_handling.py diff --git a/libeos/file_reader.py b/eos/file_reader.py similarity index 100% rename from libeos/file_reader.py rename to eos/file_reader.py diff --git a/libeos/header.py b/eos/header.py similarity index 100% rename from libeos/header.py rename to eos/header.py diff --git a/libeos/helpers.py b/eos/helpers.py similarity index 100% rename from libeos/helpers.py rename to eos/helpers.py diff --git a/libeos/helpers_fallback.py b/eos/helpers_fallback.py similarity index 90% rename from libeos/helpers_fallback.py rename to eos/helpers_fallback.py index 14a06b6..969510e 100644 --- a/libeos/helpers_fallback.py +++ b/eos/helpers_fallback.py @@ -18,8 +18,8 @@ def extract_walltime(tof_e, dataPacket_p, dataPacketTime_p): def filter_project_x(pixelLookUp, pixelID_e, ymin, ymax): (detY_e, detZ_e, detXdist_e, delta_e) = pixelLookUp[np.int_(pixelID_e)-1, :].T # define mask and filter y range - mask_e = (ymin<=detY_e) & (detY_e<=ymax[1]) - return (detY_e, detZ_e, detXdist_e, delta_e, mask_e) + mask_e = (ymin<=detY_e) & (detY_e<=ymax) + return (detZ_e, detXdist_e, delta_e, mask_e) def calculate_derived_properties_focussing(tof_e, detXdist_e, delta_e, mask_e, lmin, lmax, nu, mu, chopperDetectorDistance, hdm): diff --git a/libeos/helpers_numba.py b/eos/helpers_numba.py similarity index 100% rename from libeos/helpers_numba.py rename to eos/helpers_numba.py diff --git a/libeos/instrument.py b/eos/instrument.py similarity index 100% rename from libeos/instrument.py rename to eos/instrument.py diff --git a/libeos/logconfig.py b/eos/logconfig.py similarity index 100% rename from libeos/logconfig.py rename to eos/logconfig.py diff --git a/libeos/normalisation.py b/eos/normalisation.py similarity index 100% rename from libeos/normalisation.py rename to eos/normalisation.py diff --git a/libeos/options.py b/eos/options.py similarity index 99% rename from libeos/options.py rename to eos/options.py index d9056af..59913fb 100644 --- a/libeos/options.py +++ b/eos/options.py @@ -297,6 +297,15 @@ class NormalisationMethod(StrEnum): @dataclass class ReductionConfig(ArgParsable): + fileIdentifier: List[str] = field( + metadata={ + 'short': 'f', + 'priority': 100, + 'group': 'input data', + 'help': 'file number(s) or offset (if < 1)', + }, + ) + qResolution: float = field( default=0.01, metadata={ @@ -329,15 +338,6 @@ class ReductionConfig(ArgParsable): 'help': 'theta region of interest w.r.t. beam center', }, ) - fileIdentifier: List[str] = field( - default_factory=lambda: ['0'], - metadata={ - 'short': 'f', - 'priority': 100, - 'group': 'input data', - 'help': 'file number(s) or offset (if < 1)', - }, - ) normalisationMethod: NormalisationMethod = field( default=NormalisationMethod.over_illuminated, metadata={ @@ -492,7 +492,7 @@ class EOSConfig: # return self.calculate_call_string() def call_string(self): - base = 'python eos.py' + base = 'eos' inpt = '' if self.reader.year: diff --git a/libeos/path_handling.py b/eos/path_handling.py similarity index 100% rename from libeos/path_handling.py rename to eos/path_handling.py diff --git a/libeos/projection.py b/eos/projection.py similarity index 100% rename from libeos/projection.py rename to eos/projection.py diff --git a/libeos/reduction.py b/eos/reduction.py similarity index 100% rename from libeos/reduction.py rename to eos/reduction.py diff --git a/setup.cfg b/setup.cfg index d41e64a..c9a9876 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ universal = 1 [metadata] name = amor_eos -version = attr: libeos.__version__ +version = attr: eos.__version__ author = Jochen Stahn - Paul Scherrer Institut author_email = jochen.stahn@psi.ch description = EOS reflectometry reduction for AMOR instrument @@ -19,9 +19,7 @@ classifiers = [options] python_requires = >=3.8 packages = - libeos -scripts = - eos.py + eos install_requires = numpy h5py @@ -30,3 +28,7 @@ install_requires = [project.urls] Homepage = "https://github.com/jochenstahn/amor" + +[options.entry_points] +console_scripts = + eos = eos.__main__:main diff --git a/tests/test_full_analysis.py b/tests/test_full_analysis.py index f0e6bb7..691f6df 100644 --- a/tests/test_full_analysis.py +++ b/tests/test_full_analysis.py @@ -2,7 +2,7 @@ import os import cProfile from unittest import TestCase from dataclasses import fields, MISSING -from libeos import options, reduction, logconfig +from eos import options, reduction, logconfig logconfig.setup_logging() logconfig.update_loglevel(1) diff --git a/windows_build.spec b/windows_build.spec index c933036..47c81c4 100644 --- a/windows_build.spec +++ b/windows_build.spec @@ -2,7 +2,7 @@ a = Analysis( - ['eos.py'], + ['eos/__main__.py'], pathex=[], binaries=[], datas=[], diff --git a/windows_folder.spec b/windows_folder.spec index 7646535..4ca0124 100644 --- a/windows_folder.spec +++ b/windows_folder.spec @@ -9,7 +9,7 @@ datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] a = Analysis( - ['eos.py'], + ['eos/__main__.py'], pathex=[], binaries=[], datas=[],