Skip to content

Commit

Permalink
Merge pull request #221 from neutrons/219_move_peaks
Browse files Browse the repository at this point in the history
Move classes into the pyrs.peaks subproject
  • Loading branch information
peterfpeterson authored Dec 19, 2019
2 parents b157dd4 + 3bd967c commit d310134
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 250 deletions.
16 changes: 5 additions & 11 deletions pyrs/core/pyrscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
from pyrs.utilities import checkdatatypes
from pyrs.core import instrument_geometry
from pyrs.utilities import file_util
from pyrs.core import peak_fit_factory
from pyrs.peaks import PeakFitEngineFactory, SupportedPeakProfiles, SupportedBackgroundTypes
from pyrs.utilities.rs_project_file import HidraConstants, HidraProjectFile, HidraProjectFileMode
from pyrs.core import strain_stress_calculator
from pyrs.core import reduction_manager
from pyrs.core import polefigurecalculator
import os
import numpy

# Define Constants
SUPPORTED_PEAK_TYPES = ['PseudoVoigt', 'Gaussian', 'Voigt'] # 'Lorentzian': No a profile of HB2B


class PyRsCore(object):
"""
Expand Down Expand Up @@ -108,8 +105,7 @@ def init_peak_fit_engine(self, fit_tag):
# get workspace
workspace = self.reduction_service.get_hidra_workspace(fit_tag)
# create a controller from factory
self._peak_fitting_dict[fit_tag] = peak_fit_factory.PeakFitEngineFactory.getInstance('Mantid')(workspace,
None)
self._peak_fitting_dict[fit_tag] = PeakFitEngineFactory.getInstance('Mantid')(workspace, None)
# set wave length: TODO - #81+ - shall be a way to use calibrated or non-calibrated
wave_length_dict = workspace.get_wavelength(calibrated=False, throw_if_not_set=False)
if wave_length_dict is not None:
Expand Down Expand Up @@ -154,10 +150,8 @@ def fit_peaks(self, project_name="",

# Check Inputs
checkdatatypes.check_dict('Peak fitting (information) parameters', peaks_fitting_setup)
checkdatatypes.check_string_variable('Peak type', peak_type,
peak_fit_factory.SupportedPeakProfiles)
checkdatatypes.check_string_variable('Background type', background_type,
peak_fit_factory.SupportedBackgroundTypes)
checkdatatypes.check_string_variable('Peak type', peak_type, SupportedPeakProfiles)
checkdatatypes.check_string_variable('Background type', background_type, SupportedBackgroundTypes)

# Deal with sub runs
if sub_run_list is None:
Expand Down Expand Up @@ -647,4 +641,4 @@ def supported_peak_types(self):
list of supported peaks' types for fitting
:return:
"""
return SUPPORTED_PEAK_TYPES[:]
return SupportedPeakProfiles[:]
2 changes: 1 addition & 1 deletion pyrs/core/summary_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def write_csv(self, sample_logs, peak_collections, tolerance=1E-10):
----------
sample_logs: ~pyrs.dataobjects.SampleLogs
peak_collections: list
list of :py:obj:`~pyrs.core.peak_collection.PeakCollection`
list of :py:obj:`~pyrs.peaks.PeakCollection`
tolerance : float
relative tolerance of variance to treat a sample log as a constant value
and bring into extended header
Expand Down
7 changes: 7 additions & 0 deletions pyrs/peaks/__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 .peak_collection import *
from .peak_fit_factory import *

__all__ = peak_collection.__all__ + peak_fit_factory.__all__
26 changes: 12 additions & 14 deletions pyrs/core/mantid_fit_peak.py → pyrs/peaks/mantid_fit_peak.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Peak fitting engine by calling mantid
from .peak_fit_engine import PeakFitEngine
from pyrs.core import mantid_helper
from pyrs.core.peak_profile_utility import Gaussian, PseudoVoigt
from pyrs.utilities import checkdatatypes
from pyrs.core import peak_fit_engine
from pyrs.core.peak_collection import PeakCollection
from pyrs.peaks import PeakCollection
import numpy as np
from mantid.api import AnalysisDataService
from mantid.simpleapi import CreateWorkspace, FitPeaks

__all__ = ['MantidPeakFitEngine']

DEBUG = False # Flag for debugging mode


class MantidPeakFitEngine(peak_fit_engine.PeakFitEngine):
class MantidPeakFitEngine(PeakFitEngine):
"""
peak fitting engine class for mantid
"""
Expand All @@ -34,8 +36,6 @@ def __init__(self, workspace, mask_name):
self._fitted_function_error_table = None # fitted function parameters' fitting error table workspace
self._model_matrix_ws = None # MatrixWorkspace of the model from fitted function parameters

return

def _create_peak_center_ws(self, peak_center):
""" Create peak center workspace
:param peak_center: float or numpy array
Expand Down Expand Up @@ -110,38 +110,36 @@ def _set_default_peak_params_value(self, peak_function_name, peak_range):
parameter names (native), parameter values (as a list in str)
"""
from peak_profile_utility import Gaussian, PseudoVoigt

# Specify instrument resolution for both Gaussian and FWHM
hidra_fwhm = 0.5

# Estimate
estimated_heights, flat_bkgds = self.estimate_peak_height(peak_range)
max_estimated_height = estimated_heights.max()
flat_bkgd = flat_bkgds[np.argmax(estimated_heights)]
# do not pass A0 to FitPeaks

# Make the difference between peak profiles
if peak_function_name == 'Gaussian':
# Gaussian
peak_param_names = '{}, {}'.format('Height', 'Sigma', 'A0')
peak_param_names = '{}, {}'.format('Height', 'Sigma')

# sigma
instrument_sigma = Gaussian.cal_sigma(hidra_fwhm)

# set value
peak_param_values = "{}, {}".format(max_estimated_height, instrument_sigma, flat_bkgd)
peak_param_values = "{}, {}".format(max_estimated_height, instrument_sigma)

elif peak_function_name == 'PseudoVoigt':
# Pseudo-voig
default_mixing = 0.6

peak_param_names = '{}, {}, {}'.format('Mixing', 'Intensity', 'FWHM', 'A0')
peak_param_names = '{}, {}, {}'.format('Mixing', 'Intensity', 'FWHM')

# intensity
max_intensity = PseudoVoigt.cal_intensity(max_estimated_height, hidra_fwhm, default_mixing)

# set values
peak_param_values = "{}, {}, {}".format(default_mixing, max_intensity, hidra_fwhm, flat_bkgds)
peak_param_values = "{}, {}, {}".format(default_mixing, max_intensity, hidra_fwhm)

else:
# Non-supported case
Expand Down Expand Up @@ -322,7 +320,7 @@ def fit_multiple_peaks(self, sub_run_range, peak_function_name, background_funct
Returns
-------
Dict
dictionary of ~pyrs.core.peak_collection.PeakCollection with peak tag as key
dictionary of ~pyrs.peaks.PeakCollection with peak tag as key
"""
peak_collection_dict = dict()
Expand Down Expand Up @@ -362,7 +360,7 @@ def _set_profile_parameters_values_from_fitting(self, peak_tag, sub_runs, peak_p
Returns
-------
~pyrs.core.peak_collecton.PeakCollection
~pyrs.peaks.PeakCollection
Fitted peak's information
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pyrs.utilities import checkdatatypes
from pyrs.core.peak_profile_utility import get_effective_parameters_converter, PeakShape, BackgroundFunction

__all__ = ['PeakCollection']


class PeakCollection(object):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pyrs.core.peak_profile_utility import PeakShape
from pyrs.utilities import checkdatatypes

__all__ = ['PeakFitEngine']


class PeakFitEngine(object):
"""
Expand Down Expand Up @@ -247,7 +249,7 @@ def fit_multiple_peaks(self, sub_run_range, peak_function_name, background_funct
Returns
-------
List of ~pyrs.core.peak_collection.PeakCollection
List of ~pyrs.peaks.PeakCollection
"""
raise NotImplementedError('Virtual base class member method fit_multiple_peaks')
Expand Down Expand Up @@ -346,7 +348,7 @@ def get_peaks(self, peak_tag):
Returns
-------
pyrs.core.peak_collection.PeakCollection
pyrs.peaks.PeakCollection
Collection of peak information
"""
Expand Down
18 changes: 9 additions & 9 deletions pyrs/core/peak_fit_factory.py → pyrs/peaks/peak_fit_factory.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Peak fitting engine
from pyrs.core import mantid_fit_peak
from pyrs.utilities import checkdatatypes


SupportedPeakProfiles = ['Gaussian', 'PseudoVoigt', 'Voigt']
SupportedBackgroundTypes = ['Flat', 'Linear', 'Quadratic']

__all__ = ['PeakFitEngineFactory', 'SupportedPeakProfiles', 'SupportedBackgroundTypes']


class PeakFitEngineFactory(object):
"""
Peak fitting engine factory
"""
@staticmethod
def getInstance(engine_name):
def getInstance(name):
""" Get instance of Peak fitting engine
:param engine_name:
:return:
"""
checkdatatypes.check_string_variable('Peak fitting engine', engine_name, ['Mantid', 'PyRS'])
checkdatatypes.check_string_variable('Peak fitting engine', name, ['Mantid', 'PyRS'])

# this must be here for now to stop circular imports
from .mantid_fit_peak import MantidPeakFitEngine

if engine_name == 'Mantid':
engine_class = mantid_fit_peak.MantidPeakFitEngine
if name == 'Mantid':
return MantidPeakFitEngine
else:
raise RuntimeError('Implement general scipy peak fitting engine')

return engine_class
Loading

0 comments on commit d310134

Please sign in to comment.