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 instance – The complete model


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


int – A number of processes that will perform the fitting parallely


samfire_pool instance – A proxy object that manages either multiprocessing or ipyparallel pool


strategy list – 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.


dictionary – A dictionary for important samfire parameters


strategy – The currently active strategy from the strategies list


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


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


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


start SAMFire


stop SAMFire


force plot of currently selected active strategy


refresh current active strategy database. No previous structure is preserved


backs up the current version of the model


changes strategy to a new one. Certain rules apply


appends strategy to the strategies list


extends strategies list


removes strategy from strategies list


updates the current model with values, received from a worker


if _log exists, logs the arguments to the list.


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


Returns the active 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

  • 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

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 the strategies list by the given iterable

Parameters:iterable (an iterable of strategy instances) –

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

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

optional_components = []

Returns the number of pixels that have been solved


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


(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

Refreshes currently selected strategy without preserving any “ignored” pixels


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

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

  • 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(object) → None -- append object to end
extend(iterable) → None -- extend list by appending elements from the iterable
remove(value) → None -- remove first occurrence of value.

Raises ValueError if the value is not present.