dataFitting module

Author:Dominic Hunt
exception dataFitting.LengthError[source]

Bases: Exception

exception dataFitting.OrderError[source]

Bases: Exception

dataFitting.fit_record(participant_fits, file_name_generator)[source]

Returns the participant fits summary as a csv file

Parameters:
  • participant_fits (dict) – A summary of the recovered parameters
  • file_name_generator (function) – Creates a new file with the name <handle> and the extension <extension>. It takes two string parameters: (handle, extension) and returns one fileName string
dataFitting.log_fitting_parameters(fit_info)[source]

Records and outputs to the log the parameters associated with the fitting algorithms

Parameters:fit_info (dict) – The details of the fitting
dataFitting.log_model_fitted_parameters(model_fit_variables, model_parameters, fit_quality, participant_name)[source]

Logs the model and task parameters that used as initial fitting conditions

Parameters:
  • model_fit_variables (dict) – The model parameters that have been fitted over and varied.
  • model_parameters (dict) – The model parameters for the fitted model
  • fit_quality (float) – The value of goodness of fit
  • participant_name (int or string) – The identifier for each participant
dataFitting.log_model_fitting_parameters(model, model_fit_variables, model_other_args)[source]

Logs the model and task parameters that used as initial fitting conditions

Parameters:
  • model (string) – The name of the model
  • model_fit_variables (dict) – The model parameters that will be fitted over and varied.
  • model_other_args (dict) – The other parameters used in the model whose attributes have been modified by the user
dataFitting.record_fitting(fitting_data, label, participant, participant_model_variables, participant_fits, file_name_generator, save_fitting_progress=False)[source]

Records formatted versions of the fitting data

Parameters:
  • fitting_data (dict, optional) – Dictionary of details of the different fits, including an ordered dictionary containing the parameter values tested, in the order they were tested, and a list of the fit qualities of these parameters.
  • label (str) – The label used to identify the fit in the file names
  • participant (dict) – The participant data
  • participant_model_variables (dict of string) – A dictionary of model settings whose values should vary from participant to participant based on the values found in the imported participant data files. The key is the label given in the participant data file, as a string, and the value is the associated label in the model, also as a string.
  • participant_fits (defaultdict of lists) – A dictionary to be filled with the summary of the participant fits
  • file_name_generator (function) – Creates a new file with the name <handle> and the extension <extension>. It takes two string parameters: (handle, extension) and returns one fileName string
  • save_fitting_progress (bool, optional) – Specifies if the results from each iteration of the fitting process should be returned. Default False
Returns:

participant_fits – A dictionary to be filled with the summary of the previous and current participant fits

Return type:

defaultdict of lists

dataFitting.record_participant_fit(participant, part_name, model_data, model_name, fitting_data, partModelVars, participantFits, fileNameGen=None, pickleData=False, saveFittingProgress=False, expData=None)[source]

Record the data relevant to the participant fitting

Parameters:
  • participant (dict) – The participant data
  • part_name (int or string) – The identifier for each participant
  • model_data (dict) – The data from the model
  • model_name (str) – The label given to the model
  • fitting_data (dict) – Dictionary of details of the different fits, including an ordered dictionary containing the parameter values tested, in the order they were tested, and a list of the fit qualities of these parameters
  • partModelVars (dict of string) – A dictionary of model settings whose values should vary from participant to participant based on the values found in the imported participant data files. The key is the label given in the participant data file, as a string, and the value is the associated label in the model, also as a string.
  • participantFits (defaultdict of lists) – A dictionary to be filled with the summary of the participant fits
  • fileNameGen (function or None) – Creates a new file with the name <handle> and the extension <extension>. It takes two string parameters: (handle, extension) and returns one fileName string. Default None
  • pickleData (bool, optional) – If true the data for each model, task and participant is recorded. Default is False
  • saveFittingProgress (bool, optional) – Specifies if the results from each iteration of the fitting process should be returned. Default False
  • expData (dict, optional) – The data from the task. Default None
Returns:

participantFits – A dictionary to be filled with the summary of the previous and current participant fits

Return type:

defaultdict of lists

See also

outputting.pickleLog()
records the picked data
dataFitting.run(data_folder='./', data_format='csv', data_file_filter=None, data_file_terminal_ID=True, data_read_options=None, data_split_by=None, data_group_by=None, data_extra_processing=None, model_name='QLearn', model_changing_properties=None, model_constant_properties=None, participantID='Name', participant_choices='Actions', participant_rewards='Rewards', model_fit_value='ActionProb', fit_subset=None, task_stimuli=None, participant_action_options=None, fit_method='Evolutionary', fit_method_args=None, fit_measure='-loge', fit_measure_args=None, fit_extra_measures=None, participant_varying_model_parameters=None, label=None, save_fitting_progress=False, config_file=None, output_path=None, pickle=False, boundary_excess_cost_function=None, min_log_level='INFO', numpy_error_level='log', fit_float_error_response_value=1e-100, calculate_covariance=False)[source]

A framework for fitting models to data for tasks, along with recording the data associated with the fits.

Parameters:
  • data_folder (string or list of strings, optional) – The folder where the data can be found. Default is the current folder.
  • data_format (string, optional) – The file type of the data, from mat, csv, xlsx and pkl. Default is csv
  • data_file_filter (callable, string, list of strings or None, optional) – A function to process the file names or a list of possible prefixes as strings or a single string. Default None, no file names removed
  • data_file_terminal_ID (bool, optional) – Is there an ID number at the end of the filename? If not then a more general search will be performed. Default True
  • data_read_options (dict, optional) – The keyword arguments for the data importing method chosen
  • data_split_by (string or list of strings, optional) – If multiple participant datasets are in one file sheet, this specifies the column or columns that can distinguish and identify the rows for each participant. Default None
  • data_group_by (list of strings, optional) – A list of parts of filenames that are repeated across participants, identifying all the files that should be grouped together to form one participants data. The rest of the filename is assumed to identify the participant. Default is None
  • data_extra_processing (callable, optional) – A function that modifies the dictionary of data read for each participant in such that it is appropriate for fitting. Default is None
  • model_name (string, optional) – The name of the file where a model.modelTemplate.Model class can be found. Default QLearn
  • model_changing_properties (dictionary with values of tuple of two floats, optional) – Parameters are the options that you allow to vary across model fits. Each model parameter is specified as a dict key. The value is a tuple containing the upper and lower search bounds, e.g. alpha has the bounds (0, 1). Default None
  • model_constant_properties (dictionary of float, string or binary valued elements, optional) – These contain all the the model options that define the version of the model being studied. Default None
  • participantID (str, optional) – The key (label) used to identify each participant. Default Name
  • participant_choices (string, optional) – The participant data key of their action choices. Default 'Actions'
  • participant_rewards (string, optional) – The participant data key of the participant reward data. Default 'Rewards'
  • model_fit_value (string, optional) – The key to be compared in the model data. Default 'ActionProb'
  • fit_subset (float('Nan'), None, "rewarded", "unrewarded", "all" or list of int, optional) – Describes which, if any, subset of trials will be used to evaluate the performance of the model. This can either be described as a list of trial numbers or, by passing - "all" for fitting all trials - float('Nan') or "unrewarded" for all those trials whose feedback was float('Nan') - "rewarded" for those who had feedback that was not float('Nan') Default None, which means all trials will be used.
  • task_stimuli (list of strings or None, optional) – The keys containing the observational parameters seen by the participant before taking a decision on an action. Default None
  • participant_action_options (string or list of strings or None or one element list with a list, optional) – If a string or list of strings these are treated as dict keys where the valid actions for each trial can be found. If None then all trials will use all available actions. If the list contains one list then it will be treated as a list of valid actions for each trialstep. Default 'None'
  • fit_method (string, optional) – The fitting method to be used. The names accepted are those of the modules in the folder fitAlgs containing a FitAlg class. Default 'evolutionary'
  • fit_method_args (dict, optional) – A dictionary of arguments specific to the fitting method. Default None
  • fit_measure (string, optional) – The name of the function used to calculate the quality of the fit. The value it returns provides the fitter with its fitting guide. Default -loge
  • fit_measure_args (dict, optional) – The parameters used to initialise fitMeasure and extraFitMeasures. Default None
  • fit_extra_measures (list of strings, optional) – List of fit measures not used to fit the model, but to provide more information. Any arguments needed for these measures should be placed in fitMeasureArgs. Default None
  • participant_varying_model_parameters (dict of string, optional) – A dictionary of model settings whose values should vary from participant to participant based on the values found in the imported participant data files. The key is the label given in the participant data file, as a string, and the value is the associated label in the model, also as a string. Default {}
  • label (string, optional) – The label for the data fitting. Default None will mean no data is saved to files.
  • save_fitting_progress (bool, optional) – Specifies if the results from each iteration of the fitting process should be returned. Default False
  • config_file (string, optional) – The file name and path of a .yaml configuration file. Overrides all other parameters if found. Default None
  • output_path (string, optional) – The path that will be used for the run output. Default None
  • pickle (bool, optional) – If true the data for each model, and participant is recorded. Default is False
  • boundary_excess_cost_function (str or callable returning a function, optional) – The function is used to calculate the penalty for exceeding the boundaries. Default is boundFunc.scalarBound()
  • min_log_level (str, optional) – Defines the level of the log from (DEBUG, INFO, WARNING, ERROR, CRITICAL). Default INFO
  • numpy_error_level ({'log', 'raise'}) – Defines the response to numpy errors. Default log. See numpy.seterr
  • fit_float_error_response_value (float, optional) – If a floating point error occurs when running a fit the fitter function will return a value for each element of fpRespVal. Default is ``1/1e100`
  • calculate_covariance (bool, optional) – Is the covariance calculated. Default False

See also

modelGenerator()
The model factory
outputting()
The outputting functions
fitAlgs.fitAlg.FitAlg()
General class for a method of fitting data
fitAlgs.fitSims.fitSim()
General class for a method of simulating the fitting of data
data.Data()
Data import class
dataFitting.xlsx_fitting_data(fitting_data, label, participant, file_name_generator)[source]

Saves the fitting data to an XLSX file

Parameters:
  • fitting_data (dict, optional) – Dictionary of details of the different fits, including an ordered dictionary containing the parameter values tested, in the order they were tested, and a list of the fit qualities of these parameters.
  • label (str) – The label used to identify the fit in the file names
  • participant (dict) – The participant data
  • file_name_generator (function) – Creates a new file with the name <handle> and the extension <extension>. It takes two string parameters: (handle, extension) and returns one fileName string