### Install psignifit via setup.py Source: https://github.com/wichmann-lab/python-psignifit/wiki/Install Run this command in the terminal after unpacking the downloaded ZIP file to install the package. ```bash python setup.py install ``` -------------------------------- ### Install psignifit from source Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/install_guide.md Use this command within the cloned repository folder to perform an editable installation. ```bash pip install -e . ``` -------------------------------- ### Install psignifit via pip Source: https://github.com/wichmann-lab/python-psignifit/blob/main/README.md Use this command to install the package in your Python environment. ```bash pip install psignifit ``` -------------------------------- ### Define example dataset Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Format data as a nx3 matrix with columns for stimulus level, number of correct responses, and total responses. This example is from a signal detection experiment. ```python # levels, n-correct, n-total data = [[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]] ``` -------------------------------- ### Import psignifit modules Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/bias-analysis.md Initial setup for bias analysis by importing necessary libraries. ```python import numpy as np import psignifit as ps import psignifit.psigniplot as psp # In this example we eill ignore the warnings from psignifit ``` -------------------------------- ### Initialize psignifit with basic options Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/basic-options.md Demonstrates the basic setup for fitting a psychometric function using a 2AFC experiment type and a normal cumulative distribution function. ```python import numpy as np import psignifit as ps # to have some data we use the data from the first demo. data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) res = ps.psignifit(data, sigmoid='norm', experiment_type='2AFC') ``` -------------------------------- ### Fit with Modified Data and Same Options Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/results-object.md This example shows how to reuse configuration options from a previous fit to analyze modified data. It calculates the difference in the threshold parameter. ```python # copy the data and introduce a shift in all stimulus values otherdata = np.copy(data) otherdata[:, 0] = otherdata[:, 0] + 0.01 # fit with exact same options other_res = ps.psignifit(otherdata, conf=res.configuration) # the difference in threshold should return the introduced shift print(other_res.parameter_estimate['threshold'] - res.parameter_estimate['threshold']) ``` -------------------------------- ### Run Psignifit Analysis Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/results-object.md This code snippet demonstrates how to run the psignifit analysis with sample data. Ensure numpy, matplotlib, and psignifit are installed. ```python import numpy as np import matplotlib.pyplot as plt import psignifit as ps import psignifit.psigniplot as psp # to have some data we use the data from demo_001 data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) # Run psignifit res = ps.psignifit(data, sigmoid='norm', experiment_type='2AFC') ``` -------------------------------- ### Fit Data with Fixed Parameters in Psignifit Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Fix-Parameters.md This example fits psignifit to yes/no data using fixed lambda and gamma parameters. Ensure data and necessary imports are available. ```python import numpy as np import psignifit as ps # to have some data we use the data from the first demo. data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) fixed_parameters = {'lambda': 0.02, 'gamma': 0.5} res = ps.psignifit(data, sigmoid='norm', experiment_type='yes/no', fixed_parameters=fixed_parameters) ``` -------------------------------- ### Retrieve Posterior Samples Programmatically Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Demonstrates how to programmatically retrieve posterior samples for model parameters. This allows for further analysis or custom visualizations. The example prints the mean and standard deviation of the threshold samples. ```python import numpy as np import matplotlib.pyplot as plt import psignifit as ps import psignifit.psigniplot as psp data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # Must use debug=True to enable posterior sampling result = ps.psignifit(data, experiment_type='2AFC', debug=True) # Get posterior samples programmatically samples = result.posterior_samples(n_samples=1000, random_state=np.random.default_rng(42)) print(f"Threshold samples mean: {samples['threshold'].mean():.4f}") print(f"Threshold samples std: {samples['threshold'].std():.4f}") ``` -------------------------------- ### Fit Data with psignifit Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Get-Standard-Parameters.md This snippet demonstrates fitting data using the psignifit library with a Gaussian sigmoid for a yes/no experiment. Ensure the psignifit library is installed and data is in the correct format. ```python import numpy as np import psignifit as ps # to have some data we use the data from the first demo. data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) res = ps.psignifit(data, sigmoid='norm', experiment_type='yes/no') ``` -------------------------------- ### Extract Thresholds and Slopes using Result Object Methods Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Use the threshold() method to get the stimulus level at a given proportion correct, and slope() or slope_at_proportion_correct() to get the slope at a specific stimulus level or proportion correct. Confidence intervals can be returned or omitted. ```python import numpy as np import psignifit as ps data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) result = ps.psignifit(data, experiment_type='2AFC') # Get threshold at 90% correct (on scaled sigmoid) threshold_90, ci_90 = result.threshold(0.9) print(f"Threshold at 90%: {threshold_90:.4f}") # Threshold at 90%: 0.0058 # Get threshold at 50% on unscaled sigmoid (equals the threshold parameter) threshold_50, ci_50 = result.threshold(0.5, unscaled=True) print(f"Threshold at 50% unscaled: {threshold_50:.4f}") # Should equal result.parameter_estimate['threshold'] # Get threshold without confidence intervals threshold_only = result.threshold(0.75, return_ci=False) # Get slope at a specific stimulus level slope_at_006 = result.slope(0.006) print(f"Slope at stimulus 0.006: {slope_at_006:.2f}") # Get slope at a specific proportion correct slope_at_60pc = result.slope_at_proportion_correct(0.6) print(f"Slope at 60% correct: {slope_at_60pc:.2f}") # Get slope at midpoint of unscaled sigmoid slope_midpoint = result.slope_at_proportion_correct(0.5, unscaled=True) ``` -------------------------------- ### Generate synthetic psychometric data Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/map_vs_mean.md Setup parameters and simulate percent correct values for a mixture of sigmoids. ```python # Parameters of the sigmoids and data generation stim_range = [0.001, 0.001 + 10 * 1.1] lambda_ = 0.0232 gamma = 0.1 widths = [1.1, 3.1, 6.3] threshold = stim_range[1] / 2 sigmoid = sigmoids.Gaussian() num_trials = 3 nsteps = 20 stimulus_level = np.linspace(stim_range[0], stim_range[1], nsteps) ``` ```python perccorr1 = sigmoid(stimulus_level, threshold, widths[0], gamma, lambda_) perccorr2 = sigmoid(stimulus_level, threshold, widths[1], gamma, lambda_) perccorr3 = sigmoid(stimulus_level, threshold, widths[2], gamma, lambda_) perccorr = np.concatenate((perccorr1, perccorr2, perccorr3)) levels = np.concatenate((stimulus_level, stimulus_level, stimulus_level)) ``` ```python random_state = np.random.RandomState(883) ntrials = np.ones(nsteps * 3, dtype=int) * num_trials hits = random_state.binomial(ntrials, perccorr) levels = np.concatenate((stimulus_level, stimulus_level, stimulus_level)) data = np.dstack([levels, hits, ntrials]).squeeze() ``` -------------------------------- ### Set Custom Bounds for Parameters Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/advanced-options.md Provides custom bounds for threshold, lambda, and gamma. The bounds should be a dictionary of parameter names and tuples of start and end ranges. ```python custom_bounds = { 'threshold': (0.0, 0.015), 'lambda': (0.0, 0.1), 'gamma': (.5, .5), } res = ps.psignifit(data, bounds=custom_bounds) ``` -------------------------------- ### Set Custom Threshold Percentage Correct in psignifit Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Change-the-Threshold-Percent-Correct.md Use the `thresh_PC` argument to specify the desired proportion correct for the threshold. This example sets the threshold to 90% for a 'yes/no' experiment. ```python import numpy as np import psignifit as ps # to have some data we use the data from the first demo. data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) fixed_parameters = {'lambda': 0.02, 'gamma': 0.5} res = ps.psignifit(data, sigmoid='norm', experiment_type='yes/no', thresh_PC=0.9) ``` -------------------------------- ### Import psignifit and numpy Source: https://github.com/wichmann-lab/python-psignifit/wiki/Basic-Usage Initialize the environment by importing the necessary libraries. ```python import numpy as np import psignifit as ps ``` -------------------------------- ### Get specific parameter estimate Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Retrieve the estimated value for a specific parameter, such as the 'threshold'. ```python print(result.parameter_estimate['threshold']) ``` -------------------------------- ### Clone the psignifit repository Source: https://github.com/wichmann-lab/python-psignifit/wiki/Install Use this command to download the repository for the first time into your current directory. ```bash git clone https://github.com/wichmann-lab/python-psignifit.git ``` -------------------------------- ### Get specific confidence interval Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Retrieve a specific confidence interval for a parameter, such as the 95% confidence interval for the threshold. ```python print(result.confidence_intervals['threshold']['0.95']) ``` -------------------------------- ### Execute the fit Source: https://github.com/wichmann-lab/python-psignifit/wiki/Basic-Usage Run the psignifit function using the prepared data and options. ```python result = ps.psignifit(data,options); ``` -------------------------------- ### Get parameter estimates Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Access the estimated parameters from the fit result. The 'parameter_estimate' attribute returns a dictionary of fitted parameters. ```python print(result.parameter_estimate) ``` -------------------------------- ### Print Configuration Options Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/results-object.md Display the complete set of configuration options used for the psignifit analysis, which are stored within the result object. ```python print(res.configuration) ``` -------------------------------- ### Initialize and Set Options in Python psignifit Source: https://github.com/wichmann-lab/python-psignifit/wiki/Options-Dictionary Use Python dictionary syntax to initialize and set options, similar to the MATLAB options struct. Refer to the MATLAB wiki for field meanings. ```python options = dict() % Initialization as an empty dictionary ``` ```python options['fieldName'] = value % Setting individual values ``` -------------------------------- ### Import necessary libraries Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Import matplotlib for plotting, psignifit for fitting, and psigniplot for plotting utilities. ```python import matplotlib.pyplot as plt import psignifit as ps import psignifit.psigniplot as psp ``` -------------------------------- ### Get confidence intervals for parameters Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/basic-usage.md Access the confidence intervals for all estimated parameters. The 'confidence_intervals' attribute returns a dictionary containing intervals for default confidences (95%, 90%, 68%). ```python print(result.confidence_intervals['threshold']) ``` -------------------------------- ### Access parameter estimates Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/advanced-options.md Demonstrates how to retrieve parameter estimates from the result object using attributes or the get_parameter_estimate method. ```python # This gives the default estimate, in this case the mean estimate since we set it in the options. print(f"parameter estimate (default=MAP): {res.parameter_estimate}") print(f"parameter estimate (MAP): {res.parameter_estimate_MAP}") print(f"parameter estimate (mean){res.parameter_estimate_mean}") ``` ```python print(res.get_parameter_estimate(estimate_type="MAP")) ``` -------------------------------- ### Configure Pooling Options in Python Source: https://github.com/wichmann-lab/python-psignifit/wiki/Pooling-Utility Sets the pooling parameters within the options dictionary to control block formation and trial grouping. ```python options['nblocks'] = 25; # number of blocks required to start pooling options['poolMaxGap'] = np.inf; # maximal number of trials with other stimulus levels between pooled trials options['poolMaxLength'] = np.inf; # maximal number of trials per block options['poolxTol'] = 0; # maximal difference in stimulus level from the first trial in a block ``` -------------------------------- ### Print Parameter Estimates for Different Beta Priors Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/priors.md Prints the parameter estimates for fits performed with different beta prior values (1 and 200). This highlights how the beta-variance parameter (eta) is affected by the prior strength. ```python print('Fit with beta prior = 1: ') print(json.dumps(res1.parameter_estimate, indent=2)) print('Fit with beta prior = 200: ') print(json.dumps(res200.parameter_estimate, indent=2)) ``` -------------------------------- ### Verify fitted parameters against simulated parameters (log-space) Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_in_logspace_demo.md Asserts that the fitted threshold and width parameters in the result dictionary are close to the original log-space values used for simulation. This confirms the accuracy of the log-space fit. ```python assert np.isclose(res.parameter_estimate['threshold'], log_threshold, atol=1e-4) assert np.isclose(res.parameter_estimate['width'], log_width, atol=1e-4) ``` -------------------------------- ### Specify Stimulus Range for Adaptive Procedures Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt For adaptive procedures, explicitly define the stimulus range to guide the fitting process. This is crucial when the full range of possible stimuli needs to be communicated to the fitting algorithm. ```python result_adaptive = ps.psignifit( data, experiment_type='2AFC', stimulus_range=(0.0, 0.02) # Full range of possible stimuli ) ``` -------------------------------- ### Get Proportion Correct Values - Python Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Get-Proportion-Correct.md Calculates the proportion correct for specified stimulus levels using a fitted psignifit model. Accepts a list or NumPy array of stimulus levels. The results are printed to the console. ```python stimulus_levels = [0.0012, 0.0013] prop_correct = res.proportion_correct(stimulus_levels) print(prop_correct) ``` -------------------------------- ### Get Proportion Correct Values with Eta - Python Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Get-Proportion-Correct.md Calculates proportion correct values incorporating noise derived from the estimated overdispersion parameter 'eta'. This provides a more realistic variance estimate, compatible with a beta binomial distribution. ```python stimulus_levels = [0.0012, 0.0013] prop_correct = res.proportion_correct(stimulus_levels, with_eta=True) print(prop_correct) ``` -------------------------------- ### Setting simulation parameters Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Define ground truth parameters and stimulus levels for data simulation. ```python # 'ground truth' values width = 0.3 stim_range = [0.001, 0.001 + width * 1.1] threshold = stim_range[1]/3 lambda_ = 0.05 gamma = 0.01 sigmoid_name = "norm" sigmoid = sigmoid_by_name(sigmoid_name) # nsteps = 20 num_trials = 100 stimulus_level = np.linspace(stim_range[0], stim_range[1], nsteps) ``` -------------------------------- ### Get Proportion Correct Values using Mean Estimate - Python Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Get-Proportion-Correct.md Retrieves proportion correct values based on the 'mean' estimate of the psychometric function parameters, rather than the default 'MAP' (Maximum A Posteriori) estimate. Useful for alternative statistical interpretations. ```python stimulus_levels = [0.0012, 0.0013] prop_correct = res.proportion_correct(stimulus_levels, estimate_type='mean') print(prop_correct) ``` -------------------------------- ### Import psignifit modules Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/map_vs_mean.md Initial imports required for data generation, fitting, and plotting. ```python import warnings from matplotlib import pyplot as plt import numpy as np import psignifit import psignifit.psigniplot as psp from psignifit import sigmoids ``` -------------------------------- ### Configure fitting options Source: https://github.com/wichmann-lab/python-psignifit/wiki/Basic-Usage Initialize and populate the options dictionary to define the sigmoid function and experiment type. ```python options = dict(); # initialize as an empty dictionary ``` ```python options['sigmoidName'] = 'norm'; # choose a cumulative Gauss as the sigmoid options['expType'] = '2AFC'; # choose 2-AFC as the experiment type # this sets the guessing rate to .5 (fixed) and # fits the rest of the parameters ``` -------------------------------- ### Configure Equal Asymptote experiment type Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/basic-options.md Sets the experiment_type to 'equal asymptote', which yokes the asymptotes and improves fitting speed. ```python experiment_type = 'equal asymptote' ``` -------------------------------- ### Import necessary libraries and load data Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/plotting.md Imports matplotlib, numpy, and psignifit modules. Loads sample data for psychometric function fitting. ```python import matplotlib.pyplot as plt import numpy as np import psignifit as ps import psignifit.psigniplot as psp data = np.array([[0.0010, 45.0000, 90.0000], [0.0015, 50.0000, 90.0000], [0.0020, 44.0000, 90.0000], [0.0025, 44.0000, 90.0000], [0.0030, 52.0000, 90.0000], [0.0035, 53.0000, 90.0000], [0.0040, 62.0000, 90.0000], [0.0045, 64.0000, 90.0000], [0.0050, 76.0000, 90.0000], [0.0060, 79.0000, 90.0000], [0.0070, 88.0000, 90.0000], [0.0080, 90.0000, 90.0000], [0.0100, 90.0000, 90.0000]]) res = ps.psignifit(data, sigmoid='norm', experiment_type='2AFC') ``` -------------------------------- ### Running the psignifit fit Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Execute the fitting procedure for a yes/no experiment type. ```python res = psignifit.psignifit(data, sigmoid=sigmoid, experiment_type='yes/no') ``` -------------------------------- ### Constructing the data array Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Create the data structure required for psignifit fitting. ```python ntrials = np.ones(nsteps) * num_trials hits = (perccorr * ntrials).astype(int) data = np.dstack([stimulus_level, hits, ntrials]).squeeze() print(data) ``` -------------------------------- ### Define simulation dataset Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/priors.md Initializes a numpy array representing a 3-down-1-up staircase procedure simulation. ```python data = np.array([[1.5000, 3.0000, 3.0000], [1.3500, 3.0000, 3.0000], [1.2150, 1.0000, 2.0000], [1.3365, 2.0000, 3.0000], [1.4702, 3.0000, 3.0000], [1.3231, 3.0000, 3.0000], [1.1908, 1.0000, 2.0000], [1.3099, 3.0000, 3.0000], [1.1789, 1.0000, 2.0000], [1.2968, 2.0000, 3.0000], [1.4265, 3.0000, 3.0000], [1.2838, 1.0000, 2.0000], [1.4122, 3.0000, 3.0000], [1.2710, 1.0000, 2.0000], [1.3981, 1.0000, 2.0000], [1.5379, 1.0000, 2.0000], [1.6917, 3.0000, 3.0000], [1.5225, 3.0000, 3.0000], [1.3703, 2.0000, 3.0000]]) ``` -------------------------------- ### Verify Fixed Parameters in Psignifit Results Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/how_to/How-to-Fix-Parameters.md Print the parameter estimates from the psignifit result to confirm that the specified parameters were fixed. ```python print(res.parameter_estimate) ``` -------------------------------- ### Execute bias analysis with ps.biasAna Source: https://github.com/wichmann-lab/python-psignifit/wiki/Interval-Bias Use this syntax to perform bias analysis on 2AFC task data. Ensure the psignifit module is imported as ps. ```python ps.biasAna(data1,data2,options) ``` -------------------------------- ### Set Fixed Borders for Parameters Source: https://github.com/wichmann-lab/python-psignifit/wiki/Priors Use 'borders' to set fixed borders for parameters. This option expects a NumPy array of shape (5, 2). ```python options['borders'] = np.nan((5,2)) # setting fixed borders for parameters ``` -------------------------------- ### Configure optimization grid steps Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/advanced-options.md Adjusts the number of grid points for specific parameters to improve estimation accuracy. ```python res = ps.psignifit(data, grid_steps={'lambda': 50}) ``` -------------------------------- ### Importing psignifit dependencies Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Initial imports required for data manipulation, plotting, and psignifit functionality. ```python import numpy as np import matplotlib.pyplot as plt import psignifit import psignifit.psigniplot as psp from psignifit.sigmoids import sigmoid_by_name ``` -------------------------------- ### Import necessary libraries Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_in_logspace_demo.md Imports numpy for numerical operations, matplotlib for plotting, and psignifit for psychometric function fitting. ```python import numpy as np from matplotlib import pyplot as plt import psignifit import psignifit.psigniplot as psp from psignifit.sigmoids import sigmoid_by_name ``` -------------------------------- ### Visualize custom priors with psignifit Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Uses the psigniplot module to generate a plot of the prior distribution and saves it as a PNG file. ```python import psignifit.psigniplot as psp import matplotlib.pyplot as plt fig = psp.plot_prior(result) plt.savefig('custom_priors.png') ``` -------------------------------- ### Plotting simulated data Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Visualize the simulated stimulus levels and percent correct values. ```python fig, ax = plt.subplots(); ax.scatter(stimulus_level, perccorr); ax.set_xlabel("Stimulus Level"); ax.set_ylabel("Percent Correct"); ax.spines[['right', 'top']].set_visible(False); ``` -------------------------------- ### Configuration Object for psignifit Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Use the Configuration class to hold and validate fitting options, which can be reused across multiple fits. ```python import numpy as np import psignifit as ps from psignifit import Configuration data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # Create configuration object config = Configuration( experiment_type='2AFC', sigmoid='norm', thresh_PC=0.5, # Threshold at 50% on unscaled sigmoid width_alpha=0.05, # Width = difference between 5% and 95% confP=(0.95, 0.9, 0.68), # Confidence levels estimate_type='MAP', # Use MAP estimate (alternative: 'mean') CI_method='percentiles', # CI method (alternative: 'project') beta_prior=10, # Strength of binomial prior verbose=True ) # Use configuration for fitting result = ps.psignifit(data, conf=config) # Reuse configuration for another dataset other_data = data.copy() other_data[:, 0] += 0.001 result2 = ps.psignifit(other_data, conf=config) # Access configuration from result print(result.configuration) ``` -------------------------------- ### Configure Yes/No experiment type Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/basic-options.md Sets the experiment_type to 'yes/no', allowing both asymptotes to vary with a prior favoring values near 0 and 1. ```python experiment_type = 'yes/no' ``` -------------------------------- ### Plotting Priors with plot_prior() Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Visualize prior distributions and their effect on psychometric functions. Requires fitting with debug=True. ```python import numpy as np import matplotlib.pyplot as plt import psignifit as ps import psignifit.psigniplot as psp data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # Must use debug=True to access priors result = ps.psignifit(data, experiment_type='2AFC', debug=True) # Plot priors and their effect on psychometric functions # Upper row: prior distributions for threshold, width, lambda # Lower row: corresponding psychometric functions at different prior values fig = psp.plot_prior(result) plt.savefig('priors_visualization.png') ``` -------------------------------- ### Visualize marginal distributions and psychometric functions Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/map_vs_mean.md Compare marginal plots and psychometric functions using different estimate types. ```python # Uses the default, in this case `'mean'` psp.plot_marginal(result, 'width'); ``` ```python # Overwrites the default, uses `'MAP'` psp.plot_marginal(result, 'width', estimate_type='MAP'); ``` ```python ax = psp.plot_psychometric_function(result, estimate_type='mean'); psp.plot_psychometric_function(result, estimate_type='MAP', ax=ax); ``` -------------------------------- ### Custom Priors for psignifit Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Define custom prior distributions for parameters to incorporate domain knowledge or constrain the fitting space. Requires debug=True to visualize. ```python import numpy as np import scipy.stats import psignifit as ps data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # Define custom priors as functions custom_priors = { # Uniform prior on threshold 'threshold': lambda x: np.ones_like(x), # Beta distribution prior on gamma (favoring values near 0.5) 'gamma': lambda x: scipy.stats.beta.pdf(x, 2, 2), # Exponential prior on lambda (favoring small lapse rates) 'lambda': lambda x: scipy.stats.expon.pdf(x, scale=0.05), } result = ps.psignifit( data, experiment_type='yes/no', priors=custom_priors, debug=True # Enable debug to visualize priors ) ``` -------------------------------- ### Plot All Pairwise 2D Marginal Distributions Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Generates a grid of all pairwise 2D marginal posterior distributions for the model parameters. This helps visualize relationships between parameters. Requires fitting with debug=True. Saves the plot to 'all_2d_marginals.png'. ```python import numpy as np import matplotlib.pyplot as plt import psignifit as ps import psignifit.psigniplot as psp data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # Must use debug=True result = ps.psignifit(data, experiment_type='2AFC', debug=True) # Plot all pairwise 2D marginals fig = psp.plot_bayes(result) plt.savefig('all_2d_marginals.png') ``` -------------------------------- ### Set simulation parameters Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_in_logspace_demo.md Defines parameters for simulating data, including stimulus range, threshold, sigmoid parameters (lambda, gamma), number of steps, and number of trials. It also specifies the sigmoid type ('weibull') and prepares the stimulus levels spaced logarithmically. ```python stim_range = [1.0, 1000.0] threshold = 134 lambda_ = 0.0232 gamma = 0.1 nsteps = 20 num_trials = 50000 # We are going to fit a log-Weibull sigmoid sigmoid_name = 'weibull' sigmoid = sigmoid_by_name('weibull') # We choose levels spaced logarithmically in the stimuli space, because that is often what happens # in experiments where the fit is done in logarithmic space stimulus_level = np.logspace(np.log10(stim_range[0]), np.log10(stim_range[1]), nsteps, base=10) # However, one could just as well space the stimuli linearly #stimulus_level = np.linspace(stim_range[0], stim_range[1], nsteps) ``` -------------------------------- ### Print Confidence Intervals for Different Beta Priors Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/priors.md Prints the 95% confidence intervals for fits performed with beta prior values of 1 and 200. This shows the effect of prior strength on the width of the confidence intervals. ```python print('Confidence Intervals for beta prior = 1: ') print(json.dumps(res1.confidence_intervals, indent=2)) print('Confidence Intervals for beta prior = 200: ') print(json.dumps(res200.confidence_intervals, indent=2)) ``` -------------------------------- ### Verify other fitted parameters Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_in_logspace_demo.md Asserts that the fitted gamma, lambda, and eta parameters are close to the values used during data simulation, ensuring the overall integrity of the fitting process. ```python assert np.isclose(res.parameter_estimate['lambda'], lambda_, atol=1e-4) assert np.isclose(res.parameter_estimate['gamma'], gamma, atol=1e-4) assert np.isclose(res.parameter_estimate['eta'], 0, atol=1e-4) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/priors.md Imports the required libraries for plotting, numerical operations, and psignifit functionality. ```python import matplotlib.pyplot as plt import numpy as np import json import psignifit as ps import psignifit.psigniplot as psp ``` -------------------------------- ### Verifying parameter recovery Source: https://github.com/wichmann-lab/python-psignifit/blob/main/docs/examples/parameter_recovery_demo.md Assert that the recovered parameters match the ground truth values. ```python assert np.isclose(res.parameter_estimate['lambda'], lambda_, atol=1e-2) ``` ```python assert np.isclose(res.parameter_estimate['gamma'], gamma, atol=1e-2) ``` ```python assert np.isclose(res.parameter_estimate['eta'], 0, atol=1e-2) assert np.isclose(res.parameter_estimate['threshold'], threshold, atol=1e-2) assert np.isclose(res.parameter_estimate['width'], width, atol=1e-2) ``` -------------------------------- ### Configure Experiment Types for Psignifit Source: https://context7.com/wichmann-lab/python-psignifit/llms.txt Configure different experiment types to control how asymptotes (gamma and lambda) are handled. This affects prior assumptions about guess and lapse rates. Requires numpy and psignifit imports. ```python import numpy as np import psignifit as ps data = np.array([ [0.0010, 45.0, 90.0], [0.0015, 50.0, 90.0], [0.0020, 44.0, 90.0], [0.0025, 44.0, 90.0], [0.0030, 52.0, 90.0], [0.0035, 53.0, 90.0], [0.0040, 62.0, 90.0], [0.0045, 64.0, 90.0], [0.0050, 76.0, 90.0], [0.0060, 79.0, 90.0], [0.0070, 88.0, 90.0], [0.0080, 90.0, 90.0], [0.0100, 90.0, 90.0] ]) # nAFC experiments: gamma fixed to 1/n result_2afc = ps.psignifit(data, experiment_type='2AFC') # gamma = 0.5 result_3afc = ps.psignifit(data, experiment_type='3AFC') # gamma = 0.333 result_4afc = ps.psignifit(data, experiment_type='4AFC') # gamma = 0.25 # Yes/No experiments: both asymptotes free to vary result_yesno = ps.psignifit(data, experiment_type='yes/no') # Equal asymptote: gamma and lambda constrained to be equal result_equal = ps.psignifit(data, experiment_type='equal asymptote') ```