Skip to content

Commit

Permalink
Moved HidraConstants into separate file refs #219
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Dec 19, 2019
1 parent d310134 commit c0e923d
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 72 deletions.
7 changes: 4 additions & 3 deletions pyrs/core/nexus_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
Convert HB2B NeXus file to Hidra project file for further reduction
"""
from __future__ import (absolute_import, division, print_function) # python3 compatibility
from mantid.simpleapi import mtd, GenerateEventsFilter, LoadEventNexus, FilterEvents
import bisect
from mantid.kernel import FloatTimeSeriesProperty, Int32TimeSeriesProperty, Int64TimeSeriesProperty, logger, Logger
from mantid.simpleapi import mtd, GenerateEventsFilter, LoadEventNexus, FilterEvents
import numpy
import os
from pyrs.core import workspaces
from pyrs.core.instrument_geometry import AnglerCameraDetectorGeometry, HidraSetup
from pyrs.projectfile import HidraConstants
from pyrs.utilities import checkdatatypes
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
import bisect
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode


class NeXusConvertingApp(object):
Expand Down
13 changes: 4 additions & 9 deletions pyrs/core/pyrscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pyrs.core import instrument_geometry
from pyrs.utilities import file_util
from pyrs.peaks import PeakFitEngineFactory, SupportedPeakProfiles, SupportedBackgroundTypes
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.core import strain_stress_calculator
from pyrs.core import reduction_manager
from pyrs.core import polefigurecalculator
Expand Down Expand Up @@ -394,22 +395,16 @@ def load_hidra_project(self, hidra_h5_name, project_name, load_detector_counts=T
Parameters
----------
hidra_h5_name
name of HIDRA project file in HDF5 format
project_name
name of the reduction project specified by user to trace
load_detector_counts
load_diffraction
Returns
-------
pyrs.core.workspaces.HidraWorkspace
"""
"""
Load a HIDRA project file
:param hidra_h5_name: name of HIDRA project file in HDF5 format
:param project_name: name of the reduction project specified by user to trace
:param load_detector_counts:
:param load_diffraction:
:return: HidraWorkspace instance
"""
# Initialize session
self._reduction_service.init_session(project_name)
Expand Down
9 changes: 5 additions & 4 deletions pyrs/core/reduction_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import os
import random
import numpy as np
from pyrs.utilities import checkdatatypes
from pyrs.core import workspaces
from pyrs.utilities import calibration_file_io
from pyrs.core import instrument_geometry
from pyrs.core import mask_util
from pyrs.core import reduce_hb2b_mtd
from pyrs.core import reduce_hb2b_pyrs
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode, HidraConstants
from pyrs.core import instrument_geometry
from pyrs.projectfile import HidraConstants
from pyrs.utilities import calibration_file_io
from pyrs.utilities import checkdatatypes
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode


class HB2BReductionManager(object):
Expand Down
17 changes: 9 additions & 8 deletions pyrs/core/workspaces.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Data manager
import numpy
from pyrs.dataobjects import SampleLogs
from pyrs.projectfile import HidraConstants
from pyrs.utilities import checkdatatypes
from pyrs.utilities import rs_project_file

Expand Down Expand Up @@ -158,11 +159,11 @@ def get_detector_2theta(self, sub_run):
"""
checkdatatypes.check_int_variable('Sub run number', sub_run, (0, None))
try:
two_theta = self._sample_logs[rs_project_file.HidraConstants.TWO_THETA, sub_run]
two_theta = self._sample_logs[HidraConstants.TWO_THETA, sub_run]
except KeyError as key_err:
raise RuntimeError('Unable to retrieve 2theta value ({}) from sub run {} due to missing key {}.'
'Available sample logs are {}'
.format(rs_project_file.HidraConstants.TWO_THETA,
.format(HidraConstants.TWO_THETA,
sub_run, key_err, self._sample_logs.keys()))
return two_theta[0] # convert from numpy array of length 1 to a scalar

Expand All @@ -173,14 +174,14 @@ def get_l2(self, sub_run):
"""
checkdatatypes.check_int_variable('Sub run number', sub_run, (0, None))

if rs_project_file.HidraConstants.L2 in self._sample_logs:
if HidraConstants.L2 in self._sample_logs:
# L2 is a valid sample log: get L2
try:
# convert from numpy array of length 1 to a scalar
l2 = self._sample_logs[rs_project_file.HidraConstants.L2, sub_run][0]
l2 = self._sample_logs[HidraConstants.L2, sub_run][0]
except KeyError as key_err:
raise RuntimeError('Unable to retrieve L2 value for {} due to {}. Available sun runs are {}'
.format(sub_run, key_err, self._sample_logs[rs_project_file.HidraConstants.L2]))
.format(sub_run, key_err, self._sample_logs[HidraConstants.L2]))
else:
# L2 might be unchanged
l2 = None
Expand Down Expand Up @@ -427,7 +428,7 @@ def get_sample_log_values(self, sample_log_name, sub_runs=None):
sample log values ordered by sub run numbers with given sub runs or all sub runs
"""
if sample_log_name == rs_project_file.HidraConstants.SUB_RUNS and \
if sample_log_name == HidraConstants.SUB_RUNS and \
sample_log_name not in self._sample_logs.keys():
return self.get_sub_runs()

Expand Down Expand Up @@ -623,12 +624,12 @@ def save_experimental_data(self, hidra_project, sub_runs=None):
else:
# same thing
sub_runs_array = sub_runs
hidra_project.append_experiment_log(rs_project_file.HidraConstants.SUB_RUNS, sub_runs_array)
hidra_project.append_experiment_log(HidraConstants.SUB_RUNS, sub_runs_array)

# Add regular ample logs
for log_name in self._sample_logs.keys():
# no operation on 'sub run': skip
if log_name == rs_project_file.HidraConstants.SUB_RUNS:
if log_name == HidraConstants.SUB_RUNS:
continue

# Convert each sample log to a numpy array
Expand Down
6 changes: 3 additions & 3 deletions pyrs/dataobjects.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -11,7 +11,7 @@ def _coerce_to_ndarray(value):


class SampleLogs(MutableMapping):
SUBRUN_KEY = 'sub-runs' # TODO should be pyrs.utilities.rs_project_file.HidraConstants.SUB_RUNS
SUBRUN_KEY = HidraConstants.SUB_RUNS

def __init__(self, **kwargs):
self._data = dict(kwargs)
Expand Down Expand Up @@ -71,7 +71,7 @@ def __setitem__(self, key, value):
def plottable_logs(self):
'''Return the name of all logs that are plottable
This always includes :py:obj:`~pyrs.utilities.rs_project_file.HidraConstants.SUB_RUNS`
This always includes :py:obj:`~pyrs.projectfile.HidraConstants.SUB_RUNS`
in addition to all the other logs'''
return list(self._plottable)

Expand Down
4 changes: 4 additions & 0 deletions pyrs/projectfile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# flake8: noqa
from .constants import HidraConstants

__all__ = ['HidraConstants']
40 changes: 40 additions & 0 deletions pyrs/projectfile/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class HidraConstants(object):
"""
Constants used for Hidra project file, workspace and related dictionary
"""
RAW_DATA = 'raw data'
REDUCED_DATA = 'reduced diffraction data'
REDUCED_MAIN = 'main' # default reduced data
SUB_RUNS = 'sub-runs'
CALIBRATION = 'calibration'
SAMPLE_LOGS = 'logs'
INSTRUMENT = 'instrument'
GEOMETRY_SETUP = 'geometry setup'
DETECTOR_PARAMS = 'detector'
TWO_THETA = '2theta'
L2 = 'L2'

MONO = 'monochromator setting'
WAVELENGTH = 'wave length'

# Efficiency
DETECTOR_EFF = 'efficiency calibration'
RUN = 'run number'

# Masks
MASK = 'mask' # main entry name of mask
DETECTOR_MASK = 'detector'
SOLID_ANGLE_MASK = 'solid angle'

# constants about peak fitting
PEAK_PROFILE = 'peak profile'
PEAKS = 'peaks' # main entry for fitted peaks' parameters
PEAK_FIT_CHI2 = 'chi2'
PEAK_PARAMS = 'parameters' # peak parameter values
PEAK_PARAMS_ERROR = 'fitting error' # peak parameters' fitting error
PEAK_PARAM_NAMES = 'parameter names' # peak parameter names
PEAK_COM = 'C.O.M' # peak's center of mass
BACKGROUND_TYPE = 'background type'

# Special sample logs
SUB_RUN_DURATION = 'sub-run duration'
43 changes: 2 additions & 41 deletions pyrs/utilities/rs_project_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,9 @@
from pyrs.core.instrument_geometry import AnglerCameraDetectorGeometry, HidraSetup
from pyrs.peaks import PeakCollection
from pyrs.dataobjects import SampleLogs
from pyrs.projectfile import HidraConstants


class HidraConstants(object):
"""
Constants used for Hidra project file, workspace and related dictionary
"""
RAW_DATA = 'raw data'
REDUCED_DATA = 'reduced diffraction data'
REDUCED_MAIN = 'main' # default reduced data
SUB_RUNS = 'sub-runs'
CALIBRATION = 'calibration'
SAMPLE_LOGS = 'logs'
INSTRUMENT = 'instrument'
GEOMETRY_SETUP = 'geometry setup'
DETECTOR_PARAMS = 'detector'
TWO_THETA = '2theta'
L2 = 'L2'

MONO = 'monochromator setting'
WAVELENGTH = 'wave length'

# Efficiency
DETECTOR_EFF = 'efficiency calibration'
RUN = 'run number'

# Masks
MASK = 'mask' # main entry name of mask
DETECTOR_MASK = 'detector'
SOLID_ANGLE_MASK = 'solid angle'

# constants about peak fitting
PEAK_PROFILE = 'peak profile'
PEAKS = 'peaks' # main entry for fitted peaks' parameters
PEAK_FIT_CHI2 = 'chi2'
PEAK_PARAMS = 'parameters' # peak parameter values
PEAK_PARAMS_ERROR = 'fitting error' # peak parameters' fitting error
PEAK_PARAM_NAMES = 'parameter names' # peak parameter names
PEAK_COM = 'C.O.M' # peak's center of mass
BACKGROUND_TYPE = 'background type'

# Special sample logs
SUB_RUN_DURATION = 'sub-run duration'
__all__ = ['HidraProjectFileMode', 'DiffractionUnit', 'HidraProjectFile']


class HidraProjectFileMode(Enum):
Expand Down
3 changes: 2 additions & 1 deletion tests/data/convert_xray_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Note: most of the methods to parse HZB data are copied from script pyrscalibration.py
"""
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.core.instrument_geometry import HidraSetup
import numpy
from skimage import io
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_autoreduction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from pyrs.core.nexus_conversion import NeXusConvertingApp
from pyrs.core.powder_pattern import ReductionApp
from pyrs.utilities.rs_project_file import HidraConstants
from pyrs.projectfile import HidraConstants
from matplotlib import pyplot as plt
import pytest

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_peak_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def show_fit_result(self, show_effective_params):
effective_parameter=show_effective_params)
# Plot peak width
sub_run_vec = peak_params_matrix[:, 0]
peak_width = peak_params_matrix[:, 5] # rs_project_file.HidraConstants.Peak_FWHM for pandas output
peak_width = peak_params_matrix[:, 5] # pyrs.projectfile.HidraConstants.Peak_FWHM for pandas output
plt.plot(sub_run_vec, peak_width, color='red', label='FWHM')

# plt.show()
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_hidra_project_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
Test for reading and writing components to HiDRA project file
"""
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
import os
import numpy as np
import datetime
Expand Down

0 comments on commit c0e923d

Please sign in to comment.