From 4204e0a9027c3b538167cdce15b7b5a70d7eeda2 Mon Sep 17 00:00:00 2001 From: Pete Peterson Date: Thu, 19 Dec 2019 12:33:35 -0500 Subject: [PATCH] Move projectfile into subpackage refs #219 --- pyrs/core/nexus_conversion.py | 3 +-- pyrs/core/powder_pattern.py | 3 +-- pyrs/core/pyrscore.py | 3 +-- pyrs/core/reduction_manager.py | 3 +-- pyrs/core/workspaces.py | 17 ++++++++--------- pyrs/dataobjects.py | 4 ++-- pyrs/peaks/peak_fit_engine.py | 14 +++++++------- pyrs/projectfile/__init__.py | 10 ++++++---- .../file_object.py} | 2 +- scripts/preparetest/convert_hb2b_peaks.py | 2 +- scripts/preparetest/convert_hzb_data.py | 4 ++-- .../preparetest/convert_strain_stress_data.py | 3 +-- scripts/pyrs_calibration.py | 3 +-- tests/integration/test_calibration_class.py | 3 +-- tests/integration/test_peak_fitting.py | 2 +- tests/integration/test_project_file_rw.py | 2 +- tests/integration/test_reduction.py | 2 +- tests/unit/test_hidra_project_file.py | 3 +-- 18 files changed, 38 insertions(+), 45 deletions(-) rename pyrs/{utilities/rs_project_file.py => projectfile/file_object.py} (99%) diff --git a/pyrs/core/nexus_conversion.py b/pyrs/core/nexus_conversion.py index fef172ad0..ec8cf1faf 100644 --- a/pyrs/core/nexus_conversion.py +++ b/pyrs/core/nexus_conversion.py @@ -9,9 +9,8 @@ import os from pyrs.core import workspaces from pyrs.core.instrument_geometry import AnglerCameraDetectorGeometry, HidraSetup -from pyrs.projectfile import HidraConstants +from pyrs.projectfile import HidraConstants, HidraProjectFile, HidraProjectFileMode from pyrs.utilities import checkdatatypes -from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode class NeXusConvertingApp(object): diff --git a/pyrs/core/powder_pattern.py b/pyrs/core/powder_pattern.py index 241bc4e00..e8b3d9768 100644 --- a/pyrs/core/powder_pattern.py +++ b/pyrs/core/powder_pattern.py @@ -1,9 +1,8 @@ from pyrs.core import reduction_manager from pyrs.utilities import checkdatatypes from pyrs.core import mask_util -from pyrs.projectfile import HidraProjectFileMode +from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode from pyrs.utilities import calibration_file_io -from pyrs.utilities.rs_project_file import HidraProjectFile from matplotlib import pyplot as plt diff --git a/pyrs/core/pyrscore.py b/pyrs/core/pyrscore.py index 33dcae444..afe9e62cf 100644 --- a/pyrs/core/pyrscore.py +++ b/pyrs/core/pyrscore.py @@ -3,8 +3,7 @@ from pyrs.core import instrument_geometry from pyrs.utilities import file_util from pyrs.peaks import PeakFitEngineFactory, SupportedPeakProfiles, SupportedBackgroundTypes -from pyrs.projectfile import HidraConstants, HidraProjectFileMode -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraConstants, HidraProjectFile, HidraProjectFileMode from pyrs.core import strain_stress_calculator from pyrs.core import reduction_manager from pyrs.core import polefigurecalculator diff --git a/pyrs/core/reduction_manager.py b/pyrs/core/reduction_manager.py index 905e9a693..2adcc66a5 100644 --- a/pyrs/core/reduction_manager.py +++ b/pyrs/core/reduction_manager.py @@ -7,10 +7,9 @@ from pyrs.core import mask_util from pyrs.core import reduce_hb2b_mtd from pyrs.core import reduce_hb2b_pyrs -from pyrs.projectfile import HidraConstants, HidraProjectFileMode +from pyrs.projectfile import HidraConstants, HidraProjectFile, HidraProjectFileMode from pyrs.utilities import calibration_file_io from pyrs.utilities import checkdatatypes -from pyrs.utilities.rs_project_file import HidraProjectFile class HB2BReductionManager(object): diff --git a/pyrs/core/workspaces.py b/pyrs/core/workspaces.py index f491b6698..0d3bf4b95 100644 --- a/pyrs/core/workspaces.py +++ b/pyrs/core/workspaces.py @@ -1,9 +1,8 @@ # Data manager import numpy from pyrs.dataobjects import SampleLogs -from pyrs.projectfile import HidraConstants +from pyrs.projectfile import HidraConstants, HidraProjectFile from pyrs.utilities import checkdatatypes -from pyrs.utilities import rs_project_file class HidraWorkspace(object): @@ -57,7 +56,7 @@ def _load_raw_counts(self, hidra_file): :param hidra_file: :return: """ - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) for sub_run_i in self._sample_logs.subruns: counts_vec_i = hidra_file.read_raw_counts(sub_run_i) @@ -72,7 +71,7 @@ def _load_reduced_diffraction_data(self, hidra_file): :return: """ # Check inputs - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) # get 2theta value try: @@ -124,7 +123,7 @@ def _load_instrument(self, hidra_file): :return: """ # Check - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) # Get values self._instrument_setup = hidra_file.read_instrument_geometry() @@ -136,7 +135,7 @@ def _load_sample_logs(self, hidra_file): :param hidra_file: HIDRA project file instance :return: """ - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) # overwrite the existing sample logs self._sample_logs = hidra_file.read_sample_logs() @@ -146,7 +145,7 @@ def _load_wave_length(self, hidra_file): :param hidra_file: HIDRA project file instance :return: """ - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) # reset the wave length (dictionary) from HIDRA project file self._wave_length_dict = hidra_file.read_wavelengths() @@ -258,7 +257,7 @@ def load_hidra_project(self, hidra_file, load_raw_counts, load_reduced_diffracti :return: """ # Check input - checkdatatypes.check_type('HIDRA project file', hidra_file, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_file, HidraProjectFile) self._project_file_name = hidra_file.name # create the spectrum map - must exist before loading the counts array @@ -645,7 +644,7 @@ def save_reduced_diffraction_data(self, hidra_project): :param hidra_project: HidraProjectFile instance :return: """ - checkdatatypes.check_type('HIDRA project file', hidra_project, rs_project_file.HidraProjectFile) + checkdatatypes.check_type('HIDRA project file', hidra_project, HidraProjectFile) hidra_project.write_reduced_diffraction_data_set(self._2theta_matrix, self._diff_data_set) diff --git a/pyrs/dataobjects.py b/pyrs/dataobjects.py index bdfde0eff..74b87981f 100644 --- a/pyrs/dataobjects.py +++ b/pyrs/dataobjects.py @@ -1,7 +1,7 @@ # extentable version of dict https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/ from collections import MutableMapping import numpy as np -from pyrs.projectfile import HidraConstants + def _coerce_to_ndarray(value): if isinstance(value, np.ndarray): @@ -11,7 +11,7 @@ def _coerce_to_ndarray(value): class SampleLogs(MutableMapping): - SUBRUN_KEY = HidraConstants.SUB_RUNS + SUBRUN_KEY = 'sub-runs' # TODO should be pyrs.projectfile.HidraConstants.SUB_RUNS def __init__(self, **kwargs): self._data = dict(kwargs) diff --git a/pyrs/peaks/peak_fit_engine.py b/pyrs/peaks/peak_fit_engine.py index d155e6116..50745ecaa 100644 --- a/pyrs/peaks/peak_fit_engine.py +++ b/pyrs/peaks/peak_fit_engine.py @@ -1,7 +1,6 @@ # This is the virtual base class as the fitting frame import numpy as np from pyrs.core import workspaces -from pyrs.utilities import rs_project_file from pyrs.core import peak_profile_utility from pyrs.core.peak_profile_utility import PeakShape from pyrs.utilities import checkdatatypes @@ -175,22 +174,23 @@ def estimate_peak_height(self, peak_range): return peak_heights, flat_bkgds - def export_to_hydra_project(self, hydra_project_file, peak_tag): + def export_to_hydra_project(self, hidra_project_file, peak_tag): """Export fit result from this fitting engine instance to Hidra project file Parameters ---------- - hydra_project_file : pyrs.utilities.rs_project_file.HidraProjectFile + hidra_project_file : ~pyrs.projectfile.HidraProjectFile peak_tag Returns ------- """ - # Check input - checkdatatypes.check_type('Hidra project file', hydra_project_file, rs_project_file.HidraProjectFile) - - hydra_project_file.write_peak_fit_result(self._peak_collection_dict[peak_tag]) + try: + hidra_project_file.write_peak_fit_result(self._peak_collection_dict[peak_tag]) + except AttributeError as e: + print 'Parameter "hidra_project_file" does not appear to be correct type', e + raise RuntimeError('Method requires a HidraProjectFile') def fit_peaks(self, peak_tag, sub_run_range, peak_function_name, background_function_name, peak_center, peak_range, max_chi2=1E3, max_peak_shift=2, min_intensity=None): diff --git a/pyrs/projectfile/__init__.py b/pyrs/projectfile/__init__.py index 6031d8bca..a105d0638 100644 --- a/pyrs/projectfile/__init__.py +++ b/pyrs/projectfile/__init__.py @@ -1,6 +1,8 @@ # flake8: noqa -# pylint: disable=undefined-variable -from .constants import HidraConstants -from .file_mode import HidraProjectFileMode +from __future__ import (absolute_import, division, print_function) # python3 compatibility -__all__ = constants.__all__ + file_mode.__all__ +from .constants import * +from .file_mode import * +from .file_object import * + +__all__ = constants.__all__ + file_mode.__all__ + file_object.__all__ diff --git a/pyrs/utilities/rs_project_file.py b/pyrs/projectfile/file_object.py similarity index 99% rename from pyrs/utilities/rs_project_file.py rename to pyrs/projectfile/file_object.py index 60cd0e26b..aa09cd074 100644 --- a/pyrs/utilities/rs_project_file.py +++ b/pyrs/projectfile/file_object.py @@ -10,7 +10,7 @@ from pyrs.dataobjects import SampleLogs from pyrs.projectfile import HidraConstants, HidraProjectFileMode -__all__ = ['DiffractionUnit', 'HidraProjectFile'] +__all__ = ['HidraProjectFile'] class DiffractionUnit(Enum): diff --git a/scripts/preparetest/convert_hb2b_peaks.py b/scripts/preparetest/convert_hb2b_peaks.py index a018562b0..235e41c54 100755 --- a/scripts/preparetest/convert_hb2b_peaks.py +++ b/scripts/preparetest/convert_hb2b_peaks.py @@ -9,7 +9,7 @@ """ import numpy from pyrs.utilities import rs_scan_io -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraProjectFile def main(): diff --git a/scripts/preparetest/convert_hzb_data.py b/scripts/preparetest/convert_hzb_data.py index 28ffc9a7d..3802faacb 100755 --- a/scripts/preparetest/convert_hzb_data.py +++ b/scripts/preparetest/convert_hzb_data.py @@ -11,11 +11,11 @@ 1. Add PyRS path to python path (refer to pyrsdev.sh) 2. Run this script """ +import numpy import os from pyrs.core.instrument_geometry import HidraSetup +from pyrs.projectfile import HidraConstants, HidraProjectFile, HidraProjectFileMode from pyrs.utilities import file_util -from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode -import numpy def parse_hzb_tiff(tiff_file_name): diff --git a/scripts/preparetest/convert_strain_stress_data.py b/scripts/preparetest/convert_strain_stress_data.py index 22588422d..8f046b00f 100644 --- a/scripts/preparetest/convert_strain_stress_data.py +++ b/scripts/preparetest/convert_strain_stress_data.py @@ -2,8 +2,7 @@ # System (library) test for classes and methods that will be used to calculate strain and stress import os import sys -from pyrs.projectfile import HidraProjectFileMode -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode def contain_strain_stress_main(): diff --git a/scripts/pyrs_calibration.py b/scripts/pyrs_calibration.py index 1eb205a37..ca1ff0463 100644 --- a/scripts/pyrs_calibration.py +++ b/scripts/pyrs_calibration.py @@ -1,8 +1,7 @@ import numpy import time -from pyrs.projectfile import HidraProjectFileMode +from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode from pyrs.utilities import calibration_file_io -from pyrs.utilities.rs_project_file import HidraProjectFile from pyrs.calibration import peakfit_calibration # DEFAULT VALUES FOR DATA PROCESSING diff --git a/tests/integration/test_calibration_class.py b/tests/integration/test_calibration_class.py index 1e022f17d..0f5791aef 100644 --- a/tests/integration/test_calibration_class.py +++ b/tests/integration/test_calibration_class.py @@ -5,9 +5,8 @@ import time import os import json -from pyrs.projectfile import HidraProjectFileMode +from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode from pyrs.utilities import calibration_file_io -from pyrs.utilities.rs_project_file import HidraProjectFile try: from pyrs.calibration import peakfit_calibration except ImportError as e: diff --git a/tests/integration/test_peak_fitting.py b/tests/integration/test_peak_fitting.py index a313bd783..6f7ece9af 100644 --- a/tests/integration/test_peak_fitting.py +++ b/tests/integration/test_peak_fitting.py @@ -6,7 +6,7 @@ from pyrs.core.peak_profile_utility import PeakShape, BackgroundFunction from pyrs.core.summary_generator import SummaryGenerator from pyrs.dataobjects import SampleLogs -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraProjectFile from pyrs.peaks import PeakFitEngineFactory import h5py from pyrs.core import peak_profile_utility diff --git a/tests/integration/test_project_file_rw.py b/tests/integration/test_project_file_rw.py index 7604a1ff7..0b482bb10 100644 --- a/tests/integration/test_project_file_rw.py +++ b/tests/integration/test_project_file_rw.py @@ -1,5 +1,5 @@ from pyrs.core import workspaces -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraProjectFile from pyrs.core.instrument_geometry import HidraSetup import os import pytest diff --git a/tests/integration/test_reduction.py b/tests/integration/test_reduction.py index fbdb02b2d..5b022a9ef 100644 --- a/tests/integration/test_reduction.py +++ b/tests/integration/test_reduction.py @@ -1,4 +1,4 @@ -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraProjectFile from pyrs.utilities import calibration_file_io from pyrs.core import workspaces import pytest diff --git a/tests/unit/test_hidra_project_file.py b/tests/unit/test_hidra_project_file.py index 9f72956da..03fcc222e 100644 --- a/tests/unit/test_hidra_project_file.py +++ b/tests/unit/test_hidra_project_file.py @@ -1,8 +1,7 @@ """ Test for reading and writing components to HiDRA project file """ -from pyrs.projectfile import HidraConstants, HidraProjectFileMode -from pyrs.utilities.rs_project_file import HidraProjectFile +from pyrs.projectfile import HidraConstants, HidraProjectFile, HidraProjectFileMode import os import numpy as np import datetime