hyperspy.samfire module

class hyperspy.samfire.Samfire(model, workers=None, setup=True, **kwargs)

Bases: object

Smart Adaptive Multidimensional Fitting (SAMFire) object

SAMFire is a more robust way of fitting multidimensional datasets. By extracting starting values for each pixel from already fitted pixels, SAMFire stops the fitting algorithm from getting lost in the parameter space by always starting close to the optimal solution.

SAMFire only picks starting parameters and the order the pixels (in the navigation space) are fitted, and does not provide any new minimisation algorithms.

model

The complete model

Type

Model instance

optional_components

A list of components that can be switched off at some pixels if it returns a better Akaike’s Information Criterion with correction (AICc)

Type

list

workers

A number of processes that will perform the fitting parallely

Type

int

pool

A proxy object that manages either multiprocessing or ipyparallel pool

Type

samfire_pool instance

strategies

A list of strategies that will be used to select pixel fitting order and calculate required starting parameters. Strategies come in two “flavours” - local and global. Local strategies spread the starting values to the nearest pixels and forces certain pixel fitting order. Global strategies look for clusters in parameter values, and suggests most frequent values. Global strategy do not depend on pixel fitting order, hence it is randomised.

Type

strategy list

metadata

A dictionary for important samfire parameters

Type

dictionary

active_strategy

The currently active strategy from the strategies list

Type

strategy

update_every

If segmenter strategy is running, updates the historams every time update_every good fits are found.

Type

int

plot_every

When running, samfire plots results every time plot_every good fits are found.

Type

int

save_every

When running, samfire saves results every time save_every good fits are found.

Type

int

start()

start SAMFire

stop()

stop SAMFire

plot()

force plot of currently selected active strategy

refresh_database()

refresh current active strategy database. No previous structure is preserved

backup()

backs up the current version of the model

change_strategy()

changes strategy to a new one. Certain rules apply

append()

appends strategy to the strategies list

extend()

extends strategies list

remove()

removes strategy from strategies list

update()

updates the current model with values, received from a worker

log()

if _log exists, logs the arguments to the list.

generate_values()

creates a generator to calculate values to be sent to the workers

property active_strategy

Returns the active strategy

append(strategy)

appends the given strategy to the end of the strategies list

Parameters

strategy (strategy instance) –

backup(filename=None, on_count=True)

Backs-up the samfire results in a file

Parameters
  • filename ({str, None}) – the filename. If None, a default value of “backup_”+signal_title is used

  • on_count (bool) – if True (default), only saves on the required count of steps

change_strategy(new_strat)

Changes current strategy to a new one. Certain rules apply: diffusion -> diffusion : resets all “ignored” pixels diffusion -> segmenter : saves already calculated pixels to be ignored

when(if) subsequently diffusion strategy is run

Parameters

new_strat ({int | strategy}) – index of the new strategy from the strategies list or the strategy object itself

count = 0
extend(iterable)

extend the strategies list by the given iterable

Parameters

iterable (an iterable of strategy instances) –

generate_values(need_inds)

Returns an iterator that yields the index of the pixel and the value dictionary to be sent to the workers.

Parameters

need_inds (int) – the number of pixels to be returned in the generator

log(*args)

If has a list named “_log”, appends the arguments there

optional_components = []
property pixels_done

Returns the number of pixels that have been solved

property pixels_left

Returns the number of pixels that are left to solve. This number can increase as SAMFire learns more information about the data.

plot(on_count=False)

(if possible) plots current strategy plot. Local strategies plot grayscale navigation signal with brightness representing order of the pixel selection. Global strategies plot a collection of histograms, one per parameter.

Parameters

on_count (bool) – if True, only tries to plot every speficied count, otherwise (default) always plots if possible.

plot_every = 0
pool = None
refresh_database()

Refreshes currently selected strategy without preserving any “ignored” pixels

remove(thing)

removes given strategy from the strategies list

Parameters

thing (int or strategy instance) – Strategy that is in current strategies list or its index.

running_pixels = []
save_every = nan
start(**kwargs)

Starts SAMFire.

Parameters

**kwargs (key-word arguments) – Any key-word arguments to be passed to Model.fit() call

update(ind, results=None, isgood=None)

Updates the current model with the results, received from the workers. Results are only stored if the results are good enough

Parameters
  • ind (tuple) – contains the index of the pixel of the results

  • results ({dict, None}) – dictionary of the results. If None, means we are updating in-place (e.g. refreshing the marker or strategies)

  • isgood ({bool, None}) – if it is known if the results are good according to the goodness-of-fit test. If None, the pixel is tested

class hyperspy.samfire.StrategyList(samf)

Bases: list

append(thing)

Append object to the end of the list.

extend(iterable)

Extend list by appending elements from the iterable.

remove(thing)

Remove first occurrence of value.

Raises ValueError if the value is not present.