Skip to content

_utils

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.phase_match._utils

_debug module-attribute

_debug = Debug()

cfg module-attribute

cfg = None

Debug

Source code in FoSpy/_debug.py
class Debug:
    def __init__(self):
        self.on = False

        frame = inspect.currentframe().f_back
        self.module_name = frame.f_globals.get("__name__", "<unknown>")
        self.label = f"|(Debug message from {self.module_name})"
        self.label_width = len(self.label)

    def _get_text_width(self, module=None):
        if module:
            label = f"|(Debug message from {module} via {self.module_name})"
            label_width = len(label)
        else:
            label = self.label
            label_width = self.label_width

        text_width = DEBUG_WIDTH - label_width
        return text_width, label, label_width


    def msg(self,msg, module=None):
        if not self.on:
            return

        text_width, label, label_width = self._get_text_width(module)

        wrapped = textwrap.fill(str(msg), width=text_width)

        for line in wrapped.splitlines():
            print(f'{line:<{text_width}}{label:>{label_width}}')

    def pmsg(self,msg,module=None,**kwargs):
        if not self.on:
            return

        text_width, label, label_width = self._get_text_width(module)

        buf = io.StringIO()
        pprint(msg,stream=buf, width=text_width,**kwargs)
        txt = buf.getvalue()
        for line in txt.splitlines():
            print(f'{line:<{text_width}}{label:>{label_width}}')

label instance-attribute

label = f'|(Debug message from {self.module_name})'

label_width instance-attribute

label_width = len(self.label)

module_name instance-attribute

module_name = frame.f_globals.get('__name__', '<unknown>')

on instance-attribute

on = False

__init__

__init__()
Source code in FoSpy/_debug.py
def __init__(self):
    self.on = False

    frame = inspect.currentframe().f_back
    self.module_name = frame.f_globals.get("__name__", "<unknown>")
    self.label = f"|(Debug message from {self.module_name})"
    self.label_width = len(self.label)

_get_text_width

_get_text_width(module=None)
Source code in FoSpy/_debug.py
def _get_text_width(self, module=None):
    if module:
        label = f"|(Debug message from {module} via {self.module_name})"
        label_width = len(label)
    else:
        label = self.label
        label_width = self.label_width

    text_width = DEBUG_WIDTH - label_width
    return text_width, label, label_width

msg

msg(msg, module=None)
Source code in FoSpy/_debug.py
def msg(self,msg, module=None):
    if not self.on:
        return

    text_width, label, label_width = self._get_text_width(module)

    wrapped = textwrap.fill(str(msg), width=text_width)

    for line in wrapped.splitlines():
        print(f'{line:<{text_width}}{label:>{label_width}}')

pmsg

pmsg(msg, module=None, **kwargs)
Source code in FoSpy/_debug.py
def pmsg(self,msg,module=None,**kwargs):
    if not self.on:
        return

    text_width, label, label_width = self._get_text_width(module)

    buf = io.StringIO()
    pprint(msg,stream=buf, width=text_width,**kwargs)
    txt = buf.getvalue()
    for line in txt.splitlines():
        print(f'{line:<{text_width}}{label:>{label_width}}')

check_for_interactive

check_for_interactive(interactive, kw)
Source code in FoSpy/plotting/diffraction/phase_match/_utils.py
def check_for_interactive(interactive, kw):
    if isinstance(interactive, bool):
        return interactive
    elif isinstance(interactive, str):
        return interactive == kw
    elif isinstance(interactive, list):
        return kw in interactive

convert_baseline_cfg

convert_baseline_cfg(baseline_cfg)
Source code in FoSpy/plotting/diffraction/phase_match/_utils.py
def convert_baseline_cfg(baseline_cfg):
    out = baseline_cfg.copy()

    out['lam'] = 10**out['lam']

    for int_cfg in ("max_iter", "diff_order"):
        out[int_cfg] = int(out[int_cfg])

    return out

match_peaks

match_peaks(exp_data, sim_peaks, match_width=None)
Source code in FoSpy/plotting/diffraction/phase_match/_utils.py
def match_peaks(exp_data, sim_peaks, match_width:float=None):
    if match_width is None:
        match_width = cfg.get("diffraction.match_peaks.match_width")

    exp, widths = unpack_peaks(exp_data, "widths")
    sim = sim_peaks

    l_bases = [x - (width * match_width/2) for x, width in zip(exp, widths)]
    r_bases = [x + (width * match_width/2) for x, width in zip(exp, widths)]

    matches = []
    found = []
    missing = []
    for exp_x, l_base, r_base in zip(exp, l_bases, r_bases):
        exp_x = int(exp_x)
        l_base = int(l_base)
        r_base = int(r_base)
        matches.append((exp_x, {}))
        for sim_x in sim:
            sim_x = int(sim_x)
            if sim_x < l_base:
                if sim_x not in found and sim_x not in missing:
                    missing.append(sim_x)
                continue
            elif sim_x > r_base:
                if exp_x == exp[-1]:
                    missing.append(sim_x)
                else:
                    break

            if l_base <= sim_x <= r_base:
                delta = abs(exp_x - sim_x)

                if len(matches) > 1 and sim_x in matches[-2][1]:
                    old_delta = matches[-2][1][sim_x]
                    if delta < old_delta:
                        matches[-2][1].pop(sim_x)
                    else:
                        continue

                matches[-1][1][sim_x] = delta
                if sim_x not in found:
                    found.append(sim_x)
    matches = tuple(
        (exp_x, tuple(matched.keys()))
        for exp_x, matched in matches
    )
    return {"matches": matches, "missing": missing}

rows_to_2th

rows_to_2th(two_theta, row_structure)
Source code in FoSpy/plotting/diffraction/phase_match/_utils.py
def rows_to_2th(two_theta, row_structure):
    if isinstance(row_structure, int):
        return two_theta[row_structure]

    if isinstance(row_structure, dict):
        return {k: rows_to_2th(two_theta, v) for k, v in row_structure.items()}

    if isinstance(row_structure, list):
        return [rows_to_2th(two_theta, v) for v in row_structure]

    if isinstance(row_structure, tuple):
        return tuple(rows_to_2th(two_theta, v) for v in row_structure)

    return row_structure

unpack_peaks

unpack_peaks(data, *props, unwrap=True, **peak_parameters)
Source code in FoSpy/plotting/diffraction/phase_match/_utils.py
def unpack_peaks(data,*props, unwrap=True, **peak_parameters):
    _debug.msg("Peak Parameters")
    _debug.pmsg(peak_parameters)
    if isinstance(data, tuple):
        x_list, properties = data
    else:
        x_list, properties = find_peaks(data, **peak_parameters)

    out = [x_list]
    for prop in props:
        out.append(properties[prop])

    return out[0] if unwrap and len(out) == 1 else out