AFL.double_agent.AutoSAS module#

class AFL.double_agent.AutoSAS.AutoSAS(sas_variable: str, sas_err_variable: str, q_variable: str, output_prefix: str, sas_resolution_variable: Any | None = None, sample_dim: str = 'sample', model_dim: str = 'models', model_inputs: list[dict[str, Any]] | None = None, fit_method: dict[str, Any] | None = None, server_id: str | None = None, q_min: float | None = None, q_max: float | None = None, name: str = 'AutoSAS')#

Bases: PipelineOp

AutoSAS is a pipeline operation for fitting small-angle scattering (SAS) data.

This class provides an interface to the SAS fitting functionality, allowing users to fit various scattering models to experimental data. It can operate either locally or by connecting to a remote AutoSAS server.

sas_variable#

The variable name for scattering intensity in the dataset

Type:

str

sas_err_variable#

The variable name for scattering intensity uncertainty in the dataset

Type:

str

output_prefix#

Prefix to add to output variable names

Type:

str

q_dim#

The dimension name for q values in the dataset

Type:

str

sas_resolution_variable#

Resolution function for the data, if available

Type:

float | None

sample_dim#

The dimension containing each sample

Type:

str

model_dim#

The dimension name for different models

Type:

str

model_inputs#

List of model configurations to fit

Type:

List[dict[str, Any]] | None

fit_method#

Configuration for the fitting algorithm

Type:

dict[str, Any] | None

server_id#

Server ID in the format “host:port” for remote execution, or None for local execution

Type:

str | None

q_min#

Minimum q value to use if not provided in the model_input variable

Type:

float

q_max#

Maximum q value to use if not provided in the model_input variable

Type:

float

calculate(dataset)#

Run SAS fitting on the input data either locally or via remote server

construct_clients()#

Creates a client to talk to the AutoSAS server

class AFL.double_agent.AutoSAS.FitParameter(value: float, bounds: tuple[float, float] | None = None, fixed: bool = False, error: float | None = None)#

Bases: object

Represents a parameter to be fitted in a SAS model.

bounds: tuple[float, float] | None = None#
error: float | None = None#
fixed: bool = False#
value: float#
class AFL.double_agent.AutoSAS.ModelSelectAIC(all_chisq_var, model_names_var, sample_dim, model_inputs=None, server_id=None, output_prefix='AIC', name='ModelSelectionAIC', **kwargs)#

Bases: PipelineOp

ModelSelectAIC is a pipeline operation for model selection using the Akaike Information Criterion (AIC).

This class selects the best model for each sample based on the AIC, which balances model fit quality (chi-square) with model complexity (number of parameters). AIC helps prevent overfitting by penalizing models with more parameters.

The AIC is calculated as: AIC = chi_square + 2 * k where k is the number of free parameters in the model.

The model with the lowest AIC value is selected as the best model for each sample.

all_chisq_var#

The variable name for chi-square values in the dataset

Type:

str

model_names_var#

The variable name for model names in the dataset

Type:

str

sample_dim#

The dimension containing each sample

Type:

str

model_inputs#

List of model configurations to determine complexity

Type:

list[dict[str, Any]] | None

server_id#

Server ID in the format “host:port” for remote execution, or None for local execution

Type:

str | None

calculate(dataset)#

Method for selecting the model based on AIC (Akaike Information Criterion)

construct_clients()#

Creates a client to talk to the AutoSAS server

class AFL.double_agent.AutoSAS.ModelSelectBIC(all_chisq_var, model_names_var, sample_dim, model_inputs=None, server_id=None, output_prefix='BIC', name='ModelSelectionBIC', **kwargs)#

Bases: PipelineOp

ModelSelectBIC is a pipeline operation for model selection using the Bayesian Information Criterion (BIC).

This class evaluates competing SAS models based on their goodness of fit (chi-square) and model complexity (number of parameters), using the BIC formula: BIC = chi-square + k * ln(n), where k is the number of parameters and n is the number of data points.

The BIC penalizes model complexity more strongly than AIC, especially for larger datasets, favoring simpler models when the evidence doesn’t strongly support additional complexity.

all_chisq_var#

The variable name containing chi-square values for all models

Type:

str

model_names_var#

The variable name containing model names

Type:

str

sample_dim#

The dimension containing each sample

Type:

str

model_inputs#

List of model configurations to determine parameter counts

Type:

list[dict[str, Any]] | None

server_id#

Server ID in the format “host:port” for remote execution, or None for local execution

Type:

str | None

output_prefix#

Prefix to add to output variable names

Type:

str

calculate(dataset)#

Method for selecting the model based on BIC (Bayesian Information Criterion)

construct_clients()#

Creates a client to talk to the AutoSAS server

class AFL.double_agent.AutoSAS.ModelSelectBestChiSq(all_chisq_var, model_names_var, sample_dim, output_prefix='BestChiSq', name='ModelSelection_BestChiSq')#

Bases: PipelineOp

ModelSelectBestChiSq is a pipeline operation for model selection based on the best chi-square value.

This class evaluates competing SAS models based solely on their goodness of fit (chi-square), selecting the model with the lowest chi-square value for each sample. This approach prioritizes fit quality without considering model complexity.

Note that selecting models based only on chi-square may lead to overfitting, as more complex models with more parameters will generally fit better. For model selection that balances fit quality with model complexity, consider using ModelSelectAIC or ModelSelectBIC instead.

all_chisq_var#

The variable name containing chi-square values for all models

Type:

str

model_names_var#

The variable name containing model names

Type:

str

sample_dim#

The dimension containing each sample

Type:

str

output_prefix#

Prefix to add to output variable names

Type:

str

calculate(dataset)#

Method for selecting the model based on the best chi-squared value

class AFL.double_agent.AutoSAS.ModelSelectParsimony(all_chisq_var, model_names_var, sample_dim, cutoff_threshold=1.0, model_complexity=None, model_inputs=None, server_id=None, output_prefix='Parsimony', name='ModelSelection_Parsimony', **kwargs)#

Bases: PipelineOp

ModelSelectParsimony is a pipeline operation for selecting SAS models based on parsimony.

This class selects the simplest model that provides an acceptable fit to the data, using a chi-squared threshold to determine acceptable fits. It can operate either locally or by connecting to a remote AutoSAS server.

The parsimony principle selects the model with the fewest parameters (lowest complexity) among those that fit the data adequately. This helps avoid overfitting by preferring simpler explanations when they are sufficient.

all_chisq_var#

The variable name for chi-squared values in the dataset

Type:

str

model_names_var#

The variable name for model names in the dataset

Type:

str

sample_dim#

The dimension containing each sample

Type:

str

cutoff_threshold#

The chi-squared threshold for acceptable fits (default: 1.0)

Type:

float

model_complexity#

Dictionary mapping model names to their complexity (number of parameters)

Type:

dict[str, int] | None

model_inputs#

List of model configurations, used to determine complexity if model_complexity is None

Type:

list[dict[str, Any]] | None

server_id#

Server ID in the format “host:port” for remote execution, or None for local execution

Type:

str | None

output_prefix#

Prefix to add to output variable names

Type:

str

calculate(dataset)#

Method for selecting the model based on parsimony given a user defined ChiSq threshold

construct_clients()#

Creates a client to talk to the AutoSAS server

class AFL.double_agent.AutoSAS.SASFitter(model_inputs: list[dict[str, Any]] | None = None, fit_method: dict[str, Any] | None = None, q_min: float | None = None, q_max: float | None = None, resolution: Any | None = None)#

Bases: object

A class to handle SAS model fitting for multiple datasets and models.

This class manages the fitting of SAS models to experimental data, including model selection, parameter optimization, and results analysis.

model_inputs#

List of model configurations to fit

Type:

List[dict[str, Any]]

fit_method#

Configuration for the fitting algorithm

Type:

dict[str, Any]

q_min#

Minimum q value to include in fitting

Type:

float

q_max#

Maximum q value to include in fitting

Type:

float

resolution#

Resolution function for the data

Type:

Any | None

DEFAULT_FIT_METHOD: ClassVar[dict[str, Any]] = {'ftol': 1.5e-06, 'method': 'lm', 'steps': 1000, 'verbose': False, 'xtol': 1.5e-06}#
DEFAULT_MODEL_INPUTS: ClassVar[list[dict[str, Any]]] = [{'name': 'power_law_1', 'sasmodel': 'power_law', 'q_min': 0.001, 'q_max': 1.0, 'fit_params': {'power': {'value': 4, 'bounds': (3, 4.5)}, 'background': {'value': 0.0001, 'bounds': (1e-10, 100.0)}, 'scale': {'value': 1.0, 'bounds': (1e-06, 10000.0)}}}]#
fit_models(parallel: bool = False, fit_method: dict[str, Any] | None = None) tuple[str, xarray.core.dataset.Dataset]#

Fit all models to all datasets.

Parameters:
  • parallel (bool) – Whether to use parallel processing (not implemented)

  • fit_method (dict[str, Any] | None) – Configuration for the fitting algorithm. If None, uses the instance’s fit_method.

Returns:

A tuple containing a unique ID for the fit and an xarray Dataset with the results

Return type:

Tuple[str, xr.Dataset]

set_sasdata(dataset: Dataset, sample_dim: str = 'sample', q_variable: str = 'q', sas_variable: str = 'I', sas_err_variable: str = 'dI', sas_resolution_variable: str | None = None) None#

Set the SAS data to be fitted from an xarray Dataset.

Parameters:
  • dataset (xr.Dataset) – The xarray dataset containing SAS data

  • sample_dim (str) – The dimension containing each sample

  • q_variable (str) – The variable name for q values

  • sas_variable (str) – The variable name for scattering intensity

  • sas_err_variable (str) – The variable name for scattering intensity uncertainty

  • sas_resolution_variable (str | None) – The variable name for resolution function, if available

class AFL.double_agent.AutoSAS.SASModel(name: str, data: Data1D, sasmodel: str, parameters: dict[str, dict[str, Any]])#

Bases: object

A wrapper class for sasmodels and bumps fitting.

This class provides an interface to the sasmodels library for small-angle scattering model fitting using the bumps optimization framework.

name#

Identifier for this model instance

Type:

str

sasmodel#

Name of the sasmodel to use (e.g., “sphere”, “cylinder”, “power_law”)

Type:

str

data#

The experimental data to fit

Type:

sasmodels.data.Data1D

parameters#

Dictionary of parameters for the model with their initial values and constraints

Type:

dict[str, FitParameter]

copy() Self#

Create a deep copy of this model instance.

fit(fit_method: dict[str, Any] | None = None) Any#

Fit the model to the data.

Parameters:

fit_method (dict[str, Any] | None) – Dictionary of fitting parameters to pass to bumps.fitters.fit. If None, uses default Levenberg-Marquardt parameters.

Returns:

The fitting results object from bumps

Return type:

Any

get_chisq() float#

Get the chi-squared value of the fit.

Returns:

The chi-squared value

Return type:

float

get_fit_params() dict[str, dict[str, Any]]#

Get the fitted parameters with their values and uncertainties.

Returns:

dict of parameter names and their fitted values and uncertainties

Return type:

dict[str, dict[str, Any]]

get_fit_summary() dict[str, Any]#

Get a summary of the fit results.

Returns:

Dictionary containing the fit results summary

Return type:

dict[str, Any]

residuals() ndarray[Any, dtype[Any]]#

Get the residuals between the model and the data.