SamFire#

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.

Attributes:
modelhyperspy.model.BaseModel (or subclass)

The complete model

optional_componentslist

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

workersint

A number of processes that will perform the fitting parallely

poolSamfirePool

A proxy object that manages either multiprocessing or ipyparallel pool

strategieslist

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.

metadatadict

A dictionary for important samfire parameters

active_strategystrategy

The currently active strategy from the strategies list

update_everyint

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

plot_everyint

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

save_everyint

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(strategy)#

Append the given strategy to the end of the strategies list

Parameters:
strategystrategy

The samfire strategy to use

backup(filename=None, on_count=True)#

Backup the samfire results in a file.

Parameters:
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

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_stratint or strategy

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

extend(iterable)#

Extend the strategies list by the given iterable

Parameters:
iterableiterable of strategy

The samfire strategies to use.

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_indsint

the number of pixels to be returned in the generator

log(*args)#

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.

plot(on_count=False)#

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.

Parameters:
on_countbool

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

refresh_database()#

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

remove(thing)#

Remove given strategy from the strategies list

Parameters:
thingint or strategy

Strategy that is in current strategies list or its index.

start(**kwargs)#

Start SAMFire.

Parameters:
**kwargsdict

Any keyword arguments to be passed to fit()

stop()#

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

Parameters:
indtuple

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

Attributes:
poolipyparallel.LoadBalancedView or multiprocessing.pool.Pool

The pool object.

ipython_kwargsdict

The dictionary with Ipyparallel connection arguments.

timeoutfloat

Timeout for either pool when waiting for results.

num_workersint

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

timestepfloat

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

Creates the ParallelPool and sets it up.

Parameters:
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.

setup(ipyparallel=None)#

Sets up the pool.

Parameters:
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.

sleep(howlong=None)#

Sleeps for the required number of seconds.

Parameters:
howlongNone or float

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