hyperspy.misc.utils module

class hyperspy.misc.utils.DictionaryTreeBrowser(dictionary=None, double_lines=False)

Bases: object

A class to comfortably browse a dictionary using a CLI.

In addition to accessing the values using dictionary syntax the class enables navigating a dictionary that constains nested dictionaries as attribures of nested classes. Also it is an iterator over the (key, value) items. The __repr__ method provides pretty tree printing. Private keys, i.e. keys that starts with an underscore, are not printed, counted when calling len nor iterated.

export : saves the dictionary in pretty tree printing format in a text

file.

keys : returns a list of non-private keys.
as_dictionary : returns a dictionary representation of the object.
set_item : easily set items, creating any necessary node on the way.
add_node : adds a node.

Examples

>>> tree = DictionaryTreeBrowser()
>>> tree.set_item("Branch.Leaf1.color", "green")
>>> tree.set_item("Branch.Leaf2.color", "brown")
>>> tree.set_item("Branch.Leaf2.caterpillar", True)
>>> tree.set_item("Branch.Leaf1.caterpillar", False)
>>> tree
└── Branch
    ├── Leaf1
    │   ├── caterpillar = False
    │   └── color = green
    └── Leaf2
        ├── caterpillar = True
        └── color = brown
>>> tree.Branch
├── Leaf1
│   ├── caterpillar = False
│   └── color = green
└── Leaf2
    ├── caterpillar = True
    └── color = brown
>>> for label, leaf in tree.Branch:
...     print("%s is %s" % (label, leaf.color))
Leaf1 is green
Leaf2 is brown
>>> tree.Branch.Leaf2.caterpillar
True
>>> "Leaf1" in tree.Branch
True
>>> "Leaf3" in tree.Branch
False
>>>
add_dictionary(dictionary, double_lines=False)

Add new items from dictionary.

add_node(node_path)

Adds all the nodes in the given path if they don’t exist.

Parameters

node_path (str) – The nodes must be separated by full stops (periods).

Examples

>>> dict_browser = DictionaryTreeBrowser({})
>>> dict_browser.add_node('First.Second')
>>> dict_browser.First.Second = 3
>>> dict_browser
└── First
    └── Second = 3
as_dictionary()

Returns its dictionary representation.

copy()
deepcopy()
export(filename, encoding='utf8')

Export the dictionary to a text file

Parameters
  • filename (str) – The name of the file without the extension that is txt by default

  • encoding (valid encoding str) –

get_item(item_path, default=None)

Given a path, return it’s value if it exists, or default value if missing.

The nodes of the path are separated using periods.

Parameters
  • item_path (Str) – A string describing the path with each item separated by full stops (periods)

  • default – The value to return if the path does not exist.

Examples

>>> dict = {'To' : {'be' : True}}
>>> dict_browser = DictionaryTreeBrowser(dict)
>>> dict_browser.has_item('To')
True
>>> dict_browser.has_item('To.be')
True
>>> dict_browser.has_item('To.be.or')
False
has_item(item_path)

Given a path, return True if it exists.

The nodes of the path are separated using periods.

Parameters

item_path (Str) – A string describing the path with each item separated by full stops (periods)

Examples

>>> dict = {'To' : {'be' : True}}
>>> dict_browser = DictionaryTreeBrowser(dict)
>>> dict_browser.has_item('To')
True
>>> dict_browser.has_item('To.be')
True
>>> dict_browser.has_item('To.be.or')
False
keys()

Returns a list of non-private keys.

set_item(item_path, value)

Given the path and value, create the missing nodes in the path and assign to the last one the value

Parameters

item_path (Str) – A string describing the path with each item separated by a full stops (periods)

Examples

>>> dict_browser = DictionaryTreeBrowser({})
>>> dict_browser.set_item('First.Second.Third', 3)
>>> dict_browser
└── First
   └── Second
        └── Third = 3
hyperspy.misc.utils.add_scalar_axis(signal)
hyperspy.misc.utils.attrsetter(target, attrs, value)

Sets attribute of the target to specified value, supports nested attributes. Only creates a new attribute if the object supports such behaviour (e.g. DictionaryTreeBrowser does)

Parameters
  • target (object) –

  • attrs (string) – attributes, separated by periods (e.g. ‘metadata.Signal.Noise_parameters.variance’ )

  • value (object) –

Example

First create a signal and model pair:

>>> s = hs.signals.Signal1D(np.arange(10))
>>> m = s.create_model()
>>> m.signal.data
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Now set the data of the model with attrsetter >>> attrsetter(m, ‘signal1D.data’, np.arange(10)+2) >>> self.signal.data array([2, 3, 4, 5, 6, 7, 8, 9, 10, 10])

The behaviour is identical to >>> self.signal.data = np.arange(10) + 2

hyperspy.misc.utils.closest_power_of_two(n)
hyperspy.misc.utils.create_map_objects(function, nav_size, iterating_kwargs, **kwargs)

To be used in _map_iterate of BaseSignal and LazySignal.

Moved to a separate method to reduce code duplication.

hyperspy.misc.utils.deprecation_warning(msg)
hyperspy.misc.utils.dummy_context_manager(*args, **kwargs)
hyperspy.misc.utils.ensure_unicode(stuff, encoding='utf8', encoding2='latin-1')
hyperspy.misc.utils.find_subclasses(mod, cls)

Find all the subclasses in a module.

Parameters
  • mod (module) –

  • cls (class) –

Returns

Return type

dictonary in which key, item = subclass name, subclass

hyperspy.misc.utils.fsdict(nodes, value, dictionary)

Populates the dictionary ‘dic’ in a file system-like fashion creating a dictionary of dictionaries from the items present in the list ‘nodes’ and assigning the value ‘value’ to the innermost dictionary.

‘dic’ will be of the type: dic[‘node1’][‘node2’][‘node3’]…[‘nodeN’] = value where each node is like a directory that contains other directories (nodes) or files (values)

hyperspy.misc.utils.generate_axis(origin, step, N, index=0)

Creates an axis given the origin, step and number of channels

Alternatively, the index of the origin channel can be specified.

Parameters
  • origin (float) –

  • step (float) –

  • N (number of channels) –

  • index (int) – index of origin

Returns

Return type

Numpy array

hyperspy.misc.utils.get_object_package_info(obj)

Get info about object package

Returns

dic – Dictionary containing package and package_version (if available)

Return type

dict

hyperspy.misc.utils.isiterable(obj)
hyperspy.misc.utils.iterable_not_string(thing)
hyperspy.misc.utils.map_result_construction(signal, inplace, result, ragged, sig_shape=None, lazy=False)
hyperspy.misc.utils.multiply(iterable)

Return product of sequence of numbers.

Equivalent of functools.reduce(operator.mul, iterable, 1).

>>> product([2**8, 2**30])
274877906944
>>> product([])
1
hyperspy.misc.utils.ordinal(value)

Converts zero or a postive integer (or their string representations) to an ordinal value.

>>> for i in range(1,13):
...     ordinal(i)
...
'1st'
'2nd'
'3rd'
'4th'
'5th'
'6th'
'7th'
'8th'
'9th'
'10th'
'11th'
'12th'
>>> for i in (100, '111', '112',1011):
...     ordinal(i)
...
'100th'
'111th'
'112th'
'1011th'

Notes

Author: Serdar Tumgoren http://code.activestate.com/recipes/576888-format-a-number-as-an-ordinal/ MIT license

hyperspy.misc.utils.print_html(f_text, f_html)

Print html version when in Jupyter Notebook

hyperspy.misc.utils.rollelem(a, index, to_index=0)

Roll the specified axis backwards, until it lies in a given position.

Parameters
  • a (list) – Input list.

  • index (int) – The index of the item to roll backwards. The positions of the items do not change relative to one another.

  • to_index (int, optional) – The item is rolled until it lies before this position. The default, 0, results in a “complete” roll.

Returns

res – Output list.

Return type

list

hyperspy.misc.utils.shorten_name(name, req_l)
hyperspy.misc.utils.signal_range_from_roi(signal_range)
hyperspy.misc.utils.slugify(value, valid_variable_name=False)

Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.

Adapted from Django’s “django/template/defaultfilters.py”.

hyperspy.misc.utils.stack(signal_list, axis=None, new_axis_name='stack_element', lazy=None, **kwargs)

Concatenate the signals in the list over a given axis or a new axis.

The title is set to that of the first signal in the list.

Parameters
  • signal_list (list of BaseSignal instances) –

  • axis ({None, int, str}) – If None, the signals are stacked over a new axis. The data must have the same dimensions. Otherwise the signals are stacked over the axis given by its integer index or its name. The data must have the same shape, except in the dimension corresponding to axis.

  • new_axis_name (string) – The name of the new axis when axis is None. If an axis with this name already exists it automatically append ‘-i’, where i are integers, until it finds a name that is not yet in use.

  • lazy ({bool, None}) – Returns a LazySignal if True. If None, only returns lazy rezult if at least one is lazy.

Returns

signal – signal list)

Return type

BaseSignal instance (or subclass, determined by the objects in

Examples

>>> data = np.arange(20)
>>> s = hs.stack([hs.signals.Signal1D(data[:10]),
...               hs.signals.Signal1D(data[10:])])
>>> s
<Signal1D, title: Stack of , dimensions: (2, 10)>
>>> s.data
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
hyperspy.misc.utils.stash_active_state(model)
hyperspy.misc.utils.str2num(string, **kargs)

Transform a a table in string form into a numpy array

Parameters

string (string) –

Returns

Return type

numpy array

hyperspy.misc.utils.strlist2enumeration(lst)
hyperspy.misc.utils.swapelem(obj, i, j)

Swaps element having index i with element having index j in object obj IN PLACE.

E.g. >>> L = [‘a’, ‘b’, ‘c’] >>> spwapelem(L, 1, 2) >>> print(L)

[‘a’, ‘c’, ‘b’]

hyperspy.misc.utils.transpose(*args, signal_axes=None, navigation_axes=None, optimize=False)

Transposes all passed signals according to the specified options.

For parameters see BaseSignal.transpose.

Examples

>>> signal_iterable = [hs.signals.BaseSignal(np.random.random((2,)*(i+1)))
                       for i in range(3)]
>>> signal_iterable
[<BaseSignal, title: , dimensions: (|2)>,
 <BaseSignal, title: , dimensions: (|2, 2)>,
 <BaseSignal, title: , dimensions: (|2, 2, 2)>]
>>> hs.transpose(*signal_iterable, signal_axes=1)
[<BaseSignal, title: , dimensions: (|2)>,
 <BaseSignal, title: , dimensions: (2|2)>,
 <BaseSignal, title: , dimensions: (2, 2|2)>]
>>> hs.transpose(signal1, signal2, signal3, signal_axes=["Energy"])
hyperspy.misc.utils.underline(line, character='-')

Return the line underlined.