Skip to content

peaks

Full documentation pages are generated for docstring reference only and may contain symbols imported from other modules. Imported symbols are not distinguished from locally defined symbols and will appear in any module that they are imported into. For better information on where symbols should be imported from, review the sourcecode on the github.

FoSpy.plotting.diffraction.ui.peaks

X_LABEL module-attribute

X_LABEL = full_cfg.diffraction.x_label

full_cfg module-attribute

full_cfg = None

PeakFinderAbstract

Source code in FoSpy/plotting/diffraction/ui/peaks.py
class PeakFinderAbstract:
    def __init__(self, exp_corrected, exp_index, cfg={}, **kwargs):
        specs = get_find_sliders(cfg, exp_corrected)
        self.exp_corrected = exp_corrected
        self.exp_index = exp_index
        self.max_h = max(exp_corrected)
        super().__init__(specs=specs, cfg=cfg, x_label=X_LABEL, y_ticks=False, y_label="Intensity", **kwargs)

        self.plotcolors = {
            'static': self.color1,
            'peaks': self.color2,
            'widths': self.color3
        }

        self.sticks = []

        self.peaks, self.widths = None, None

        self.plotXY(self.exp_index, self.exp_corrected)

    def update_peaks(self):
        if not hasattr(self, 'exp_corrected') and hasattr(self, 'exp_index'):
            return
        from ..phase_match._utils import rows_to_2th
        from ..phase_match._utils import unpack_peaks

        self.peaks, self.widths, w_heights  = unpack_peaks(self.exp_corrected,
        "widths", "width_heights", **self.cfg)

        peaks_list = [int(x) for x in self.peaks]
        intensity_list = [self.exp_corrected[x] for x in peaks_list]
        peaks_2th = rows_to_2th(self.exp_index, peaks_list)

        segments = [ [(x, 0), (x, y)] for x, y in zip(peaks_2th, intensity_list)]
        self.reset_sticks()
        self.add_sticks(segments, plotset='peaks')
        self.draw_sticks()

        self.update_widths(self.peaks, self.widths, w_heights)

    def update_widths(self, peaks, widths, w_heights):
        from ..phase_match._utils import rows_to_2th

        def clamp(x, minimum, maximum):
            return max(minimum, min(x, maximum))

        if len(peaks)==0:
            return
        brackets_x = [
            [clamp(round(x),0,len(self.exp_index)-1) 
             for x in [peak+width/2, peak-width/2]] 
             for peak, width in zip(peaks, widths)
        ]

        brackets_2th = rows_to_2th(self.exp_index, brackets_x)

        y_base = -0.02 * self.max_h
        cap_height = -y_base/4
        self.reset_plotsets('widths')
        buffer = 2 * max(tth_right-tth_left for (tth_left, tth_right) in brackets_2th)



        for (tth_left, tth_right), y in zip(brackets_2th, w_heights):
            self.draw_width_bracket(tth_left-buffer, tth_right+buffer, y, cap_height=cap_height, plotset='widths')
            self.draw_width_bracket(tth_left, tth_right, y_base, cap_height=cap_height, plotset='widths')



    def update_plot(self, val=None):
        super().update_plot(val)
        self.update_peaks()

    def main_loop(self):
        super().main_loop()
        return self.peaks, self.widths

exp_corrected instance-attribute

exp_corrected = exp_corrected

exp_index instance-attribute

exp_index = exp_index

max_h instance-attribute

max_h = max(exp_corrected)

plotcolors instance-attribute

plotcolors = {
    "static": self.color1,
    "peaks": self.color2,
    "widths": self.color3,
}

sticks instance-attribute

sticks = []

__init__

__init__(exp_corrected, exp_index, cfg={}, **kwargs)
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def __init__(self, exp_corrected, exp_index, cfg={}, **kwargs):
    specs = get_find_sliders(cfg, exp_corrected)
    self.exp_corrected = exp_corrected
    self.exp_index = exp_index
    self.max_h = max(exp_corrected)
    super().__init__(specs=specs, cfg=cfg, x_label=X_LABEL, y_ticks=False, y_label="Intensity", **kwargs)

    self.plotcolors = {
        'static': self.color1,
        'peaks': self.color2,
        'widths': self.color3
    }

    self.sticks = []

    self.peaks, self.widths = None, None

    self.plotXY(self.exp_index, self.exp_corrected)

main_loop

main_loop()
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def main_loop(self):
    super().main_loop()
    return self.peaks, self.widths

update_peaks

update_peaks()
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def update_peaks(self):
    if not hasattr(self, 'exp_corrected') and hasattr(self, 'exp_index'):
        return
    from ..phase_match._utils import rows_to_2th
    from ..phase_match._utils import unpack_peaks

    self.peaks, self.widths, w_heights  = unpack_peaks(self.exp_corrected,
    "widths", "width_heights", **self.cfg)

    peaks_list = [int(x) for x in self.peaks]
    intensity_list = [self.exp_corrected[x] for x in peaks_list]
    peaks_2th = rows_to_2th(self.exp_index, peaks_list)

    segments = [ [(x, 0), (x, y)] for x, y in zip(peaks_2th, intensity_list)]
    self.reset_sticks()
    self.add_sticks(segments, plotset='peaks')
    self.draw_sticks()

    self.update_widths(self.peaks, self.widths, w_heights)

update_plot

update_plot(val=None)
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def update_plot(self, val=None):
    super().update_plot(val)
    self.update_peaks()

update_widths

update_widths(peaks, widths, w_heights)
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def update_widths(self, peaks, widths, w_heights):
    from ..phase_match._utils import rows_to_2th

    def clamp(x, minimum, maximum):
        return max(minimum, min(x, maximum))

    if len(peaks)==0:
        return
    brackets_x = [
        [clamp(round(x),0,len(self.exp_index)-1) 
         for x in [peak+width/2, peak-width/2]] 
         for peak, width in zip(peaks, widths)
    ]

    brackets_2th = rows_to_2th(self.exp_index, brackets_x)

    y_base = -0.02 * self.max_h
    cap_height = -y_base/4
    self.reset_plotsets('widths')
    buffer = 2 * max(tth_right-tth_left for (tth_left, tth_right) in brackets_2th)



    for (tth_left, tth_right), y in zip(brackets_2th, w_heights):
        self.draw_width_bracket(tth_left-buffer, tth_right+buffer, y, cap_height=cap_height, plotset='widths')
        self.draw_width_bracket(tth_left, tth_right, y_base, cap_height=cap_height, plotset='widths')

AssembleSlider

AssembleSlider(subcls, ui=None)
Source code in FoSpy/ui/sliders/abstract.py
def AssembleSlider(subcls, ui=None):
    from ..available import get_valid_ui, discover_ui_options

    # look for existing slider bindings
    ui_opts = discover_ui_options(__package__)

    # validate ui and route to correct module
    ui = get_valid_ui(ui, options=ui_opts.keys())
    plot_module = ui_opts[ui]

    # construct hybrid class with slider routines and ui bindings
    class SliderPlot(subcls, plot_module.SliderPlot):
        pass

    return SliderPlot

PeakFinder

PeakFinder(
    exp_corrected, exp_index, cfg={}, ui=None, **kwargs
)
Source code in FoSpy/plotting/diffraction/ui/peaks.py
def PeakFinder(exp_corrected, exp_index, cfg={}, ui=None, **kwargs):
    PeakFinder = AssembleSlider(PeakFinderAbstract, ui=ui)

    return PeakFinder(exp_corrected, exp_index, cfg=cfg, **kwargs)

get_find_sliders

get_find_sliders(find_cfg, intensity_col)
Source code in FoSpy/plotting/diffraction/ui/_specs.py
def get_find_sliders(find_cfg, intensity_col):
    sliders = {
        "height_group": {
            "type": "group",
            "label": "Height Parameters",
            "specs": {
                "height": {
                    "type": "range",
                    "label": "Allowed Height (Y units)",
                    "min": 0,
                    "max": max(intensity_col),
                },
                "threshold": {
                    "type": "range",
                    "label": "Allowed Threshold (Y units)",
                    "min": 0,
                    "max": np.max(np.abs(np.diff(intensity_col))),
                },
                "prominence": {
                    "type": "range",
                    "label": "Allowed Prominence (Y units)",
                    "min": 0,
                    "max": max(intensity_col),
                },
            }
        },
        "width_group": {
            "type": "group",
            "label": "Width Parameters",
            "specs": {
                "distance": {
                    "type": "scalar",
                    "label": "Allowed Neighbor Distance (points)",
                    "min": 1,
                    "max": len(intensity_col),
                    "int": True,
                    "None": 1
                },
                "width": {
                    "type": "range",
                    "label": "Allowed Width (points)",
                    "min": 0,
                    "max": len(intensity_col),
                    "int": True
                },
                "wlen": {
                    "type": "scalar",
                    "label": "Window Length (points)",
                    "min": 2,
                    "max": len(intensity_col),
                    "int": True,
                    "None": len(intensity_col)
                },
                "rel_height": {
                    "type": "scalar",
                    "label": "Width @ Height (Height-Relative, top-down)",
                    "include_min": False,
                    "min": 0,
                    "max": 1,
                    "None": 1
                },
                "plateau_size": {
                    "type": "range",
                    "label": "Plateau Size (points)",
                    "min": 0,
                    "max": len(intensity_col),
                    "int": True
                } 
            }
        } 
    }

    sliders = assemble_sliders(sliders, "find_peaks", find_cfg)

    return sliders