skneuromsi.sweep module

Module for performing parameter sweeps.

This module provides functionality to perform parameter sweeps over a range of values for a target parameter in a given model. It includes classes for different processing strategies and a main ParameterSweep class to orchestrate the sweeps.

skneuromsi.sweep.DEFAULT_RANGE = array([ 90,  92,  94,  96,  98, 100, 102, 104, 106, 108])

Default range of values for parameter sweeps.

exception skneuromsi.sweep.MaybeTooBigForAvailableMemoryWarning[source]

Bases: UserWarning

Warning raised when the result is potentially too big for the available memory.

exception skneuromsi.sweep.ToBigForAvailableMemoryError[source]

Bases: MemoryError

Error raised when the result is too big for the available memory.

class skneuromsi.sweep.ProcessingStrategyABC[source]

Bases: ABC

Abstract base class for processing strategies.

This class defines the interface for processing strategies used in parameter sweeps. Subclasses should implement the map and reduce methods.

abstract map(result)[source]

Process an individual result.

Parameters:

result (object) – The result to process.

Returns:

The processed result.

Return type:

object

abstract reduce(result_sequence, tag, tqdm_cls)[source]

Combine a sequence of results into a single object.

Parameters:
  • result_sequence (iterable) – Sequence of results to combine.

  • tag (str) – A tag to identify the results.

  • tqdm_cls (class) – Progress bar class to use.

Returns:

The combined result.

Return type:

object

class skneuromsi.sweep.NDCollectionProcessingStrategy(*, compression_params=None)[source]

Bases: ProcessingStrategyABC

Processing strategy for ND collections.

This strategy compresses individual results and combines them into an NDResultCollection.

Parameters:

compression_params (tuple) – Compression parameters for joblib.dump. Defaults to core.DEFAULT_COMPRESSION_PARAMS.

map(result)[source]

Process an individual result.

Parameters:
  • result (object)

  • process. (The result to)

Returns:

The processed result.

Return type:

object

reduce(result_sequence, tag, tqdm_cls)[source]

Combine a sequence of results into a single object.

Parameters:
  • result_sequence (iterable) – Sequence of results to combine.

  • tag (str) – A tag to identify the results.

  • tqdm_cls (class) – Progress bar class to use.

Returns:

The combined result.

Return type:

object

class skneuromsi.sweep.ParameterSweep(model, target, *, range=None, repeat=2, n_jobs=None, seed=None, processing_strategy=None, mem_warning_ratio=0.8, mem_error_ratio=1.0, tqdm_cls=<class 'tqdm.asyncio.tqdm_asyncio'>)[source]

Bases: object

Perform a parameter sweep over a range of values for a target parameter.

This class orchestrates the parameter sweep process, including parallel execution of model runs and result aggregation.

Parameters:
  • model (object) – Model object to run the parameter sweep on.

  • target (str) – Name of the parameter to sweep over.

  • range (array-like, optional) – Range of values to sweep over. Default is DEFAULT_RANGE.

  • repeat (int, optional) – Number of times to repeat each run. Default is 100.

  • n_jobs (int, optional) – Number of jobs to run in parallel. Default is 1.

  • seed (int, optional) – Seed for the random number generator. Default is None.

  • processing_strategy (ProcessingStrategy, optional) – Processing strategy to use. Default is NDCollectionProcessingStrategy.

  • mem_warning_ratio (float, optional) – Ratio of available memory to trigger a warning. Default is 0.8.

  • mem_error_ratio (float, optional) – Ratio of available memory to raise an error. Default is 1.0.

  • tqdm_cls (class, optional) – Class to use for progress bars. Default is tqdm.

Raises:
  • TypeError – If the target parameter is not in the model’s run method.

  • ValueError – If repeat is less than 1, mem_warning_ratio is not in [0, 1], mem_error_ratio is not in [0, 1], or the compression parameters are not valid.

Notes

The parameter sweep is performed in parallel using joblib.

property model

The model object.

property range

The range of values to sweep over.

property repeat

The number of times to repeat each run.

property n_jobs

The number of jobs to run in parallel.

property target

The name of the parameter to sweep over.

property random_

The random number generator.

property expected_result_length_

The expected length of the result.

property processing_strategy

The processing strategy.

property tqdm_cls

The class to use for progress bars.

property mem_warning_ratio

The memory warning ratio.

property mem_error_ratio

The memory error ratio.

run(**run_kws)[source]

Run the sweep over the range of values for the target parameter.

This method performs the parameter sweep by running the model multiple times with different parameter values. It handles parallel execution, memory checks, and result aggregation.

Parameters:

**run_kws – Additional keyword arguments to pass to the model’s run method, except the target parameter.

Returns:

The aggregated results from all runs, as processed by the sweep strategy.

Return type:

object

Raises:
  • ValueError – If the target parameter is included in run_kws.

  • ToBigForAvailableMemoryError – If the result exceeds the available memory by the specified ratio.

Warning

MaybeTooBigForAvailableMemoryWarning

If the result is approaching the available memory limit.

Notes

This method uses joblib for parallel execution of the model runs. It first runs a single iteration to check memory usage before proceeding with the full parameter sweep.