Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move project file into subpackage #224

Merged
merged 4 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.dataobjects import HidraConstants
from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities import checkdatatypes
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
import bisect


class NeXusConvertingApp(object):
Expand Down
2 changes: 1 addition & 1 deletion pyrs/core/powder_pattern.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pyrs.core import reduction_manager
from pyrs.utilities import checkdatatypes
from pyrs.core import mask_util
from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from matplotlib import pyplot as plt


Expand Down
13 changes: 4 additions & 9 deletions pyrs/core/pyrscore.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# This is the core of PyRS serving as the controller of PyRS and hub for all the data
from pyrs.utilities import checkdatatypes
from pyrs.core import instrument_geometry
from pyrs.dataobjects import HidraConstants
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 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.dataobjects import HidraConstants
from pyrs.projectfile import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities import checkdatatypes


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


class HidraWorkspace(object):
Expand Down Expand Up @@ -56,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)
Expand All @@ -71,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:
Expand Down Expand Up @@ -123,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()
Expand All @@ -135,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()
Expand All @@ -145,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()
Expand All @@ -158,11 +158,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 +173,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 @@ -257,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
Expand Down Expand Up @@ -427,7 +427,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 +623,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 All @@ -644,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)

Expand Down
7 changes: 7 additions & 0 deletions pyrs/dataobjects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# flake8: noqa
from __future__ import (absolute_import, division, print_function) # python3 compatibility

from .constants import *
from .sample_logs import *

__all__ = constants.__all__ + sample_logs.__all__
43 changes: 43 additions & 0 deletions pyrs/dataobjects/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
__all__ = ['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'
7 changes: 5 additions & 2 deletions pyrs/dataobjects.py → pyrs/dataobjects/sample_logs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 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.dataobjects import HidraConstants

__all__ = ['SampleLogs']


def _coerce_to_ndarray(value):
Expand All @@ -11,7 +14,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 +74,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
14 changes: 7 additions & 7 deletions pyrs/peaks/peak_fit_engine.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions pyrs/projectfile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# flake8: noqa
from __future__ import (absolute_import, division, print_function) # python3 compatibility

from .file_mode import *
from .file_object import *

__all__ = file_mode.__all__ + file_object.__all__
37 changes: 37 additions & 0 deletions pyrs/projectfile/file_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is rs_scan_io.DiffractionFile's 2.0 version
from enum import Enum

__all__ = ['HidraProjectFileMode']


class HidraProjectFileMode(Enum):
'''
Enumeration for file access mode

These values match the strings of :py:obj:`h5py.File`
'''
READONLY = 'r' # read-only
READWRITE = 'a' # read and write
OVERWRITE = 'w' # new file

def __str__(self):
return self.value

@staticmethod
def getMode(mode):
'''Private function to convert anything into :py:obj:`HidraProjectFileMode`'''
if mode in HidraProjectFileMode:
return mode # already a member of the enum
else:
# all other checks are for strings
# the first checks are against nominal values
# the last check is against the enum names
mode = str(mode).lower()
if mode in ['a', 'rw']:
return HidraProjectFileMode.READWRITE
elif mode == 'r':
return HidraProjectFileMode.READONLY
elif mode == 'w':
return HidraProjectFileMode.OVERWRITE
else:
return HidraProjectFileMode[mode.upper()]
Loading