class hyperspy.samfire.Samfire(model, workers=None, setup=True, random_state=None, **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.

modelhyperspy.model.BaseModel (or subclass)

The complete model


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


A number of processes that will perform the fitting parallely


A proxy object that manages either multiprocessing or ipyparallel pool


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.


A dictionary for important samfire parameters


The currently active strategy from the strategies list


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


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


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

random_stateNone or int or numpy.random.Generator, default None

Random seed used to select the next pixels.


Append the given strategy to the end of the strategies list


The samfire strategy to use

backup(filename=None, on_count=True)#

Backup the samfire results in a file.

filenamestr, None, default None

the filename. If None, a default value of backup_ + signal_title is used.

on_countbool, default True

if True, 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

new_stratint or strategy

index of the new strategy from the strategies list or the strategy object itself


Extend the strategies list by the given iterable

iterableiterable of strategy

The samfire strategies to use.


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


the number of pixels to be returned in the generator


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

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.


If possible, plot 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.


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


Refresh currently selected strategy without preserving any “ignored” pixels; no previous structure is preserved.


Remove given strategy from the strategies list

thingint or strategy

Strategy that is in current strategies list or its index.


Start SAMFire.


Any keyword arguments to be passed to fit()


Stop SAMFire.

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


contains the index of the pixel of the results

resultsdict or None, default None

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

isgoodbool or None, default 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.utils.parallel_pool.ParallelPool(num_workers=None, ipython_kwargs=None, ipyparallel=None)#

Bases: object

Creates a ParallelPool by either looking for a ipyparallel client and then creating a load_balanced_view, or by creating a multiprocessing pool

poolipyparallel.LoadBalancedView or multiprocessing.pool.Pool

The pool object.


The dictionary with Ipyparallel connection arguments.


Timeout for either pool when waiting for results.


The number of workers actually created (may be less than requested, but can’t be more).


Can be used as “ticks” to adjust CPU load when building upon this class.

Creates the ParallelPool and sets it up.

num_workersNone or int, default None

The (max) number of workers to create. If less are available, smaller number is actually created.

ipyparallelNone or bool, default None

Which pool to set up. True - ipyparallel. False - multiprocessing. None - try ipyparallel, then multiprocessing if failed.

ipython_kwargsNone or dict, default None

Arguments that will be passed to the ipyparallel.Client when creating. Not None implies ipyparallel=True.

property has_pool#

Returns True if the pool is ready and set-up else False.

property is_ipyparallel#

Returns True if the pool is ipyparallel-based else False.

property is_multiprocessing#

Returns True if the pool is multiprocessing-based else False.


Sets up the pool.

ipyparallelNone or bool, default None

If True, only tries to set up the ipyparallel pool. If False - only the multiprocessing. If None, first tries ipyparallel, and it does not succeed, then multiprocessing.


Sleeps for the required number of seconds.

howlongNone or float

How long the pool should sleep for in seconds. If None (default), sleeps for “timestep”.