From 8300e842dc32adf828284715f1aaeb2a70c74551 Mon Sep 17 00:00:00 2001 From: Artur Glavic Date: Mon, 4 Mar 2024 17:39:10 +0100 Subject: [PATCH] Add test cases for full reduction with and w/o slicing (data to be stored in "test_data" folder) --- .gitignore | 1 + libeos/command_line.py | 1 - libeos/file_reader.py | 4 +- libeos/options.py | 6 +-- libeos/reduction.py | 45 ++++++++++---------- tests/__init__.py | 0 tests/test_full_analysis.py | 82 +++++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 31 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_full_analysis.py diff --git a/.gitignore b/.gitignore index 0fbb9a8..cfe693c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *~ *.bak +*.log __py_cache__ raw .idea diff --git a/libeos/command_line.py b/libeos/command_line.py index de8bc92..7fae9e5 100644 --- a/libeos/command_line.py +++ b/libeos/command_line.py @@ -180,7 +180,6 @@ def command_line_options(): autoscale=clas.autoscale, thetaRange=clas.thetaRange, thetaRangeR=clas.thetaRangeR, - lambdaRange=clas.lambdaRange, fileIdentifier=clas.fileIdentifier, scale=clas.scale, subtract=clas.subtract, diff --git a/libeos/file_reader.py b/libeos/file_reader.py index edc7922..e9f49ca 100644 --- a/libeos/file_reader.py +++ b/libeos/file_reader.py @@ -76,7 +76,7 @@ class AmorData: #------------------------------------------------------------------------------------------------- def path_generator(self, number): fileName = f'amor{self.reader_config.year}n{number:06d}.hdf' - if os.path.exists(f'{self.reader_config.dataPath}/{fileName}'): + if os.path.exists(os.path.join(self.reader_config.dataPath,fileName)): path = self.reader_config.dataPath elif os.path.exists(fileName): path = '.' @@ -149,8 +149,6 @@ class AmorData: if self.config.sampleModel: model = self.config.sampleModel - else: - model = None # assembling orso header information self.header.owner = fileio.Person( diff --git a/libeos/options.py b/libeos/options.py index 022eee0..22c9208 100644 --- a/libeos/options.py +++ b/libeos/options.py @@ -12,13 +12,12 @@ class ReaderConfig: @dataclass class ExperimentConfig: - sampleModel: str - chopperPhase: float yRange: Tuple[float, float] lambdaRange: Tuple[float, float] qzRange: Tuple[float, float] + sampleModel: Optional[str] = None chopperPhaseOffset: float = 0.0 mu: Optional[float] = None nu: Optional[float] = None @@ -28,14 +27,13 @@ class ExperimentConfig: @dataclass class ReductionConfig: qResolution: float - autoscale: Tuple[bool, bool] thetaRange: Tuple[float, float] thetaRangeR: Tuple[float, float] - lambdaRange: Tuple[float, float] fileIdentifier: list = field(default_factory=lambda: ["0"]) scale: list = field(default_factory=lambda: [1]) #per file scaling; if less elements than files use the last one + autoscale: Optional[Tuple[bool, bool]] = None subtract: Optional[str] = None normalisationFileIdentifier: Optional[list] = None timeSlize: Optional[list] = None diff --git a/libeos/reduction.py b/libeos/reduction.py index 321ccee..44c6073 100644 --- a/libeos/reduction.py +++ b/libeos/reduction.py @@ -210,18 +210,16 @@ class AmorReduction: logging.warning(f' time slize {ti:4d}, done') def save_Rqz(self): - logging.warning(f' {self.reader_config.dataPath}/{self.output_config.outputName}.Rqz.ort') + fname = os.path.join(self.reader_config.dataPath, f'{self.output_config.outputName}.Rqz.ort') + logging.warning(f' {fname}') theSecondLine = f' {self.header.experiment.title} | {self.header.experiment.start_date} | sample {self.header.sample.name} | R(q_z)' - fileio.save_orso(self.datasetsRqz, f'{self.reader_config.dataPath}/{self.output_config.outputName}.Rqz.ort', - data_separator='\n', - comment=theSecondLine) + fileio.save_orso(self.datasetsRqz, fname, data_separator='\n', comment=theSecondLine) def save_Rtl(self): - logging.warning(f' {self.reader_config.dataPath}/{self.output_config.outputName}.Rlt.ort') + fname = os.path.join(self.reader_config.dataPath, f'{self.output_config.outputName}.Rlt.ort') + logging.warning(f' {fname}') theSecondLine = f' {self.header.experiment.title} | {self.header.experiment.start_date} | sample {self.header.sample.name} | R(lambda, theta)' - fileio.save_orso(self.datasetsRlt, f'{self.reader_config.dataPath}/{self.output_config.outputName}.Rlt.ort', - data_separator='\n', - comment=theSecondLine) + fileio.save_orso(self.datasetsRlt, fname, data_separator='\n', comment=theSecondLine) def autoscale(self, q_q, R_q, dR_q, pR_q=[], pdR_q=[]): autoscale = self.reduction_config.autoscale @@ -278,12 +276,13 @@ class AmorReduction: return q_q[1:], R_q, dR_q, dq_q def loadRqz(self, name): - if os.path.exists(f'{self.reader_config.dataPath}/{name}'): - fileName = f'{self.reader_config.dataPath}/{name}' - elif os.path.exists(f'{self.reader_config.dataPath}/{name}.Rqz.ort'): - fileName = f'{self.reader_config.dataPath}/{name}.Rqz.ort' + fname = os.path.join(self.reader_config.dataPath, name) + if os.path.exists(fname): + fileName = fname + elif os.path.exists(f'{fname}.Rqz.ort'): + fileName = f'{fname}.Rqz.ort' else: - sys.exit(f'### the background file \'{self.reader_config.dataPath}/{name}\' does not exist! => stopping') + sys.exit(f'### the background file \'{fname}\' does not exist! => stopping') q_q, Sq_q, dS_q = np.loadtxt(fileName, usecols=(0, 1, 2), comments='#', unpack=True) @@ -295,14 +294,14 @@ class AmorReduction: name = str(normalisation_list[0]) for i in range(1, len(normalisation_list), 1): name = f'{name}_{normalisation_list[i]}' - if os.path.exists(f'{dataPath}/{name}.norm'): - logging.info(f'# normalisation matrix: found and using {dataPath}/{name}.norm') + n_path = os.path.join(dataPath, f'{name}.norm') + if os.path.exists(n_path): + logging.info(f'# normalisation matrix: found and using {n_path}') self.norm_lz = np.loadtxt(f'{dataPath}/{name}.norm') - fh = open(f'{dataPath}/{name}.norm', 'r') - fh.readline() - self.normFileList = fh.readline().split('[')[1].split(']')[0].replace('\'', '').split(', ') - self.normAngle = float(fh.readline().split('= ')[1]) - fh.close() + with open(n_path, 'r') as fh: + fh.readline() + self.normFileList = fh.readline().split('[')[1].split(']')[0].replace('\'', '').split(', ') + self.normAngle = float(fh.readline().split('= ')[1]) for i, entry in enumerate(self.normFileList): self.normFileList[i] = entry.split('/')[-1] self.header.measurement_additional_files = self.normFileList @@ -362,9 +361,9 @@ class AmorReduction: t0 = fromHDF.nu - fromHDF.mu mask_lz = np.logical_and(mask_lz, np.where(theta_lz-t0 >= self.reduction_config.thetaRangeR[0], True, False)) mask_lz = np.logical_and(mask_lz, np.where(theta_lz-t0 <= self.reduction_config.thetaRangeR[1], True, False)) - if self.reduction_config.lambdaRange[1]<15: - mask_lz = np.logical_and(mask_lz, np.where(lamda_lz >= self.reduction_config.lambdaRange[0], True, False)) - mask_lz = np.logical_and(mask_lz, np.where(lamda_lz <= self.reduction_config.lambdaRange[1], True, False)) + if self.experiment_config.lambdaRange[1]<15: + mask_lz = np.logical_and(mask_lz, np.where(lamda_lz >= self.experiment_config.lambdaRange[0], True, False)) + mask_lz = np.logical_and(mask_lz, np.where(lamda_lz <= self.experiment_config.lambdaRange[1], True, False)) # gravity correction #theta_lz += np.rad2deg( np.arctan( 3.07e-10 * (fromHDF.detectorDistance + detXdist_e) * lamda_lz**2 ) ) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_full_analysis.py b/tests/test_full_analysis.py new file mode 100644 index 0000000..29606c9 --- /dev/null +++ b/tests/test_full_analysis.py @@ -0,0 +1,82 @@ +import os +from unittest import TestCase +from libeos import options, reduction, logconfig + +logconfig.setup_logging() +logconfig.update_loglevel(True, False) + +class FullAmorTest(TestCase): + def setUp(self): + self.reader_config = options.ReaderConfig( + year=2023, + dataPath=os.path.join('..', "test_data")) + + def tearDown(self): + for fi in ['test.Rqz.ort', '614.norm']: + try: + os.unlink(os.path.join(self.reader_config.dataPath, fi)) + except FileNotFoundError: + pass + + + def test_time_slicing(self): + experiment_config = options.ExperimentConfig( + chopperPhase=-13.5, + chopperPhaseOffset=-5, + yRange=(11., 41.), + lambdaRange=(2., 15.), + qzRange=(0.005, 0.30), + offSpecular=False, + mu=0, + nu=0, + muOffset=0.0, + sampleModel='air | 10 H2O | D2O' + ) + reduction_config = options.ReductionConfig( + qResolution=0.01, + thetaRange=(-12., 12.), + thetaRangeR=(-12., 12.), + fileIdentifier=["610"], + scale=[1], + normalisationFileIdentifier=[], + timeSlize=[300.0] + ) + output_config = options.OutputConfig( + outputFormats=["Rqz.ort"], + outputName='test' + ) + config=options.EOSConfig(self.reader_config, experiment_config, reduction_config, output_config) + reducer = reduction.AmorReduction(config) + reducer.reduce() + + def test_noslicing(self): + experiment_config = options.ExperimentConfig( + chopperPhase=-13.5, + chopperPhaseOffset=-5, + yRange=(11., 41.), + lambdaRange=(2., 15.), + qzRange=(0.005, 0.30), + offSpecular=False, + mu=0, + nu=0, + muOffset=0.0 + ) + reduction_config = options.ReductionConfig( + qResolution=0.01, + thetaRange=(-12., 12.), + thetaRangeR=(-12., 12.), + fileIdentifier=["610", "611", "608,612-613", "609"], + scale=[1], + normalisationFileIdentifier=["614"], + autoscale=(0.005, 0.008) + ) + output_config = options.OutputConfig( + outputFormats=["Rqz.ort"], + outputName='test' + ) + config=options.EOSConfig(self.reader_config, experiment_config, reduction_config, output_config) + reducer = reduction.AmorReduction(config) + reducer.reduce() + # run second time to reuse norm file + reducer = reduction.AmorReduction(config) + reducer.reduce()