Beispiel #1
0
def assign_signal_subclass(dtype,
                           signal_dimension,
                           signal_type="",
                           lazy=False):
    """Given record_by and signal_type return the matching Signal subclass.

    Parameters
    ----------
    dtype : :class:`~.numpy.dtype`
    signal_dimension: int
    signal_type : {"EELS", "EDS", "EDS_SEM", "EDS_TEM", "DielectricFunction", "", str}
    lazy: bool

    Returns
    -------
    Signal or subclass

    """
    import hyperspy.signals
    import hyperspy._lazy_signals
    from hyperspy.signal import BaseSignal
    # Check if parameter values are allowed:
    if np.issubdtype(dtype, complex):
        dtype = 'complex'
    elif ('float' in dtype.name or 'int' in dtype.name or
          'void' in dtype.name or 'bool' in dtype.name or
          'object' in dtype.name):
        dtype = 'real'
    else:
        raise ValueError('Data type "{}" not understood!'.format(dtype.name))
    if not isinstance(signal_dimension, int) or signal_dimension < 0:
        raise ValueError("signal_dimension must be a positive interger")
    base_signals = find_subclasses(hyperspy.signals, BaseSignal)
    lazy_signals = find_subclasses(hyperspy._lazy_signals,
                                   hyperspy._lazy_signals.LazySignal)
    if lazy:
        signals = lazy_signals
    else:
        signals = {
            k: v for k,
            v in base_signals.items() if k not in lazy_signals}
    dtype_matches = [s for s in signals.values() if dtype == s._dtype]
    dtype_dim_matches = [s for s in dtype_matches
                         if signal_dimension == s._signal_dimension]
    dtype_dim_type_matches = [s for s in dtype_dim_matches if signal_type == s._signal_type
                              or signal_type in s._alias_signal_types]
    if dtype_dim_type_matches:
        # Perfect match found, return it.
        return dtype_dim_type_matches[0]
    elif [s for s in dtype_dim_matches if s._signal_type == ""]:
        # just signal_dimension and dtype matches
        # Return a general class for the given signal dimension.
        return [s for s in dtype_dim_matches if s._signal_type == ""][0]
    else:
        # no signal_dimension match either, hence return the general subclass for
        # correct dtype
        return [s for s in dtype_matches if s._signal_dimension == -
                1 and s._signal_type == ""][0]
Beispiel #2
0
def assign_signal_subclass(dtype,
                           signal_dimension,
                           signal_type="",
                           lazy=False):
    """Given record_by and signal_type return the matching Signal subclass.

    Parameters
    ----------
    dtype : :class:`~.numpy.dtype`
    signal_dimension: int
    signal_type : {"EELS", "EDS", "EDS_SEM", "EDS_TEM", "DielectricFunction", "", str}
    lazy: bool

    Returns
    -------
    Signal or subclass

    """
    import hyperspy.signals
    import hyperspy._lazy_signals
    from hyperspy.signal import BaseSignal
    # Check if parameter values are allowed:
    if np.issubdtype(dtype, complex):
        dtype = 'complex'
    elif ('float' in dtype.name or 'int' in dtype.name or
          'void' in dtype.name or 'bool' in dtype.name or
          'object' in dtype.name):
        dtype = 'real'
    else:
        raise ValueError('Data type "{}" not understood!'.format(dtype.name))
    if not isinstance(signal_dimension, int) or signal_dimension < 0:
        raise ValueError("signal_dimension must be a positive interger")
    base_signals = find_subclasses(hyperspy.signals, BaseSignal)
    lazy_signals = find_subclasses(hyperspy._lazy_signals,
                                   hyperspy._lazy_signals.LazySignal)
    if lazy:
        signals = lazy_signals
    else:
        signals = {
            k: v for k,
            v in base_signals.items() if k not in lazy_signals}
    dtype_matches = [s for s in signals.values() if dtype == s._dtype]
    dtype_dim_matches = [s for s in dtype_matches
                         if signal_dimension == s._signal_dimension]
    dtype_dim_type_matches = [s for s in dtype_dim_matches if signal_type == s._signal_type
                              or signal_type in s._alias_signal_types]
    if dtype_dim_type_matches:
        # Perfect match found, return it.
        return dtype_dim_type_matches[0]
    elif [s for s in dtype_dim_matches if s._signal_type == ""]:
        # just signal_dimension and dtype matches
        # Return a general class for the given signal dimension.
        return [s for s in dtype_dim_matches if s._signal_type == ""][0]
    else:
        # no signal_dimension match either, hence return the general subclass for
        # correct dtype
        return [s for s in dtype_matches if s._signal_dimension == -
                1 and s._signal_type == ""][0]
Beispiel #3
0
def assign_signal_subclass(dtype, signal_dimension, signal_type=""):
    """Given record_by and signal_type return the matching Signal subclass.
    Parameters
    ----------
    dtype : :class:`~.numpy.dtype`
    record_by: {"spectrum", "image", ""}
    signal_type : {"EELS", "EDS", "EDS_TEM", "", str}


    Returns
    -------
    Signal or subclass
    """
    import hyperspy.signals
    from hyperspy.signal import BaseSignal
    # Check if parameter values are allowed:
    if np.issubdtype(dtype, complex):
        dtype = 'complex'
    elif ('float' in dtype.name or 'int' in dtype.name or 'void' in dtype.name
          or 'bool' in dtype.name):
        dtype = 'real'
    else:
        raise ValueError('Data type "{}" not understood!'.format(dtype.name))
    if not isinstance(signal_dimension, int) or signal_dimension < 0:
        raise ValueError("signal_dimension must be a positive interger")
    signals = hyperspy.misc.utils.find_subclasses(hyperspy.signals, BaseSignal)
    dtype_matches = [s for s in signals.values() if dtype == s._dtype]
    dtype_dim_matches = [
        s for s in dtype_matches if signal_dimension == s._signal_dimension
    ]
    dtype_dim_type_matches = [
        s for s in dtype_dim_matches if signal_type == s._signal_type
    ]

    if dtype_dim_type_matches:
        # Perfect match found, return it.
        return dtype_dim_type_matches[0]
    elif [s for s in dtype_dim_matches if s._signal_type == ""]:
        # just signal_dimension and dtype matches
        # Return a general class for the given signal dimension.
        return [s for s in dtype_dim_matches if s._signal_type == ""][0]
    else:
        # no signal_dimension match either, hence return the general subclass for
        # correct dtype
        return [
            s for s in dtype_matches
            if s._signal_dimension == -1 and s._signal_type == ""
        ][0]
Beispiel #4
0
def assign_signal_subclass(dtype,
                           signal_dimension,
                           signal_type=""):
    """Given record_by and signal_type return the matching Signal subclass.
    Parameters
    ----------
    dtype : :class:`~.numpy.dtype`
    record_by: {"spectrum", "image", ""}
    signal_type : {"EELS", "EDS", "EDS_TEM", "", str}


    Returns
    -------
    Signal or subclass
    """
    import hyperspy.signals
    from hyperspy.signal import BaseSignal
    # Check if parameter values are allowed:
    if np.issubdtype(dtype, complex):
        dtype = 'complex'
    elif ('float' in dtype.name or 'int' in dtype.name or
          'void' in dtype.name or 'bool' in dtype.name):
        dtype = 'real'
    else:
        raise ValueError('Data type "{}" not understood!'.format(dtype.name))
    if not isinstance(signal_dimension, int) or signal_dimension < 0:
        raise ValueError("signal_dimension must be a positive interger")
    signals = hyperspy.misc.utils.find_subclasses(hyperspy.signals, BaseSignal)
    dtype_matches = [s for s in signals.values() if dtype == s._dtype]
    dtype_dim_matches = [s for s in dtype_matches
                         if signal_dimension == s._signal_dimension]
    dtype_dim_type_matches = [
        s for s in dtype_dim_matches if signal_type == s._signal_type]

    if dtype_dim_type_matches:
        # Perfect match found, return it.
        return dtype_dim_type_matches[0]
    elif [s for s in dtype_dim_matches if s._signal_type == ""]:
        # just signal_dimension and dtype matches
        # Return a general class for the given signal dimension.
        return [s for s in dtype_dim_matches if s._signal_type == ""][0]
    else:
        # no signal_dimension match either, hence return the general subclass for
        # correct dtype
        return [s for s in dtype_matches if s._signal_dimension ==
                -1 and s._signal_type == ""][0]
Beispiel #5
0
def assign_signal_subclass(record_by="",
                           signal_type="",
                           signal_origin="",):
    """Given record_by and signal_type return the matching Signal subclass.

    Parameters
    ----------
    record_by: {"spectrum", "image", ""}
    signal_type : {"EELS", "EDS", "EDS_TEM", "", str}
    signal_origin : {"experiment", "simulation", ""}

    Returns
    -------
    Signal or subclass

    """
    import hyperspy.signals
    from hyperspy.signal import Signal
    if record_by and record_by not in ["image", "spectrum"]:
        raise ValueError("record_by must be one of: None, empty string, "
                         "\"image\" or \"spectrum\"")
    if signal_origin and signal_origin not in ["experiment", "simulation"]:
        raise ValueError("signal_origin must be one of: None, empty string, "
                         "\"experiment\" or \"simulation\"")

    signals = hyperspy.misc.utils.find_subclasses(hyperspy.signals, Signal)
    signals['Signal'] = Signal

    if signal_origin == "experiment":
        signal_origin = ""

    preselection = [s for s in
                    [s for s in signals.values()
                     if record_by == s._record_by]
                    if signal_origin == s._signal_origin]
    perfect_match = [s for s in preselection
                     if signal_type == s._signal_type]
    selection = perfect_match[0] if perfect_match else \
        [s for s in preselection if s._signal_type == ""][0]
    return selection