cdsaxs.simulations package
Submodules
cdsaxs.simulations.base module
This is a protocol that defines the methods that should be implemented to ensure that the Simulation class is compatible with the fitter class.
- class cdsaxs.simulations.base.Geometry(*args, **kwargs)[source]
Bases:
Protocol
This is a protocol that defines the methods that should be implemented by the Geometry class.
- convert_to_dataframe(fitparams)[source]
This is a obligatory method that should be implemented by the Geometry class. It’s job is to take the arrays of fitparams and convert them into a pandas DataFrame based on the initial guess given by the user.
Parameters: fitparams (list): an array returned by the fitter that contains the population to be evaluated.
returns: df (pandas.DataFrame): a DataFrame containing the fitparams in a readable format.
- rescale_fitparams(fitparams)[source]
This is a obligatory method that should be implemented by the Geometry class. The values incoming from the fitter are normalized in the range of -sigma to sigma they need to be rescaled. so this method’s job is to take the arrays of fitparams and rescale them based on the initial guess given by the user. the formula used should be something like this: rescaled_fitparams = fitparams * multiplier + initial_guess multiplier and initial_guess are the values given by the user. if you find that a certain set of multiplier works better you can hardcode them as well but it is not recommended as it will make the code less flexible.
Parameters: fitparams (list): an array returned by the fitter that contains the population to be evaluated.
returns: df (dataframe): an array containing the rescaled fitparams.
- class cdsaxs.simulations.base.Simulation(*args, **kwargs)[source]
Bases:
Protocol
This is a protocol that defines the methods that should be implemented by the Simulation class.
- set_from_fitter(from_fitter)[source]
This is a obligatory method that should be implemented by the Simulation class. It should tell the Simulation object that the incoming data is from a fitter object and it should also initialize necessary things like saving the initial guess given by user to a dataframe.
cdsaxs.simulations.stacked_trapezoid module
This module simulates the diffraction pattern of a stacked trapezoids model.
It is built upon the protocol defined in base.py.
- Classes:
StackedTrapezoidSimulation: A class representing a simulation of stacked trapezoids for diffraction pattern calculation. StackedTrapezoidGeometry: A class representing the geometry of stacked trapezoids for simulations. StackedTrapezoidDiffraction: A class for simulating diffraction from stacked trapezoids.
- class cdsaxs.simulations.stacked_trapezoid.StackedTrapezoidDiffraction(TrapezoidGeometry, xp=<module 'numpy' from '/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/numpy/__init__.py'>)[source]
Bases:
object
Class for simulating diffraction from stacked trapezoids.
- calculate_coefficients(qzs, df=None)[source]
Calculate the coefficients needed to simulate intensities, taking into account the material through the weight parameter.
- Parameters:
qzs – array-like List of qz values.
df – DataFrame containing the parameters for the trapezoids.
- Returns:
- array-like
Coefficients needed to calculate the form factor.
- Return type:
coefficients
- calculate_form_factor(qys, qzs, df)[source]
Calculate the form factor of the stacked trapezoid at all qx, qz positions.
Arguments: qys, qzs: array_like
List of qx/qz at which the form factor is simulated.
df: DataFrame containing the parameters for the trapezoids.
Returns: form_factor: array_like
Values of the form factor.
- correct_form_factor_intensity(qys, qzs, fitparams)[source]
Calculate the intensites using form factor and apply debye waller corrections
Arguments: qys, qzs: array_like
List of qx/qz at which the form factor is simulated.
- fitparams: array_like, optional
parameters returned by fitter.
- fit_mode: str, optional
Method used for fitting. Default is ‘cmaes’.
- best_fit: array_like, optional
Best fit obtained from fitter.
Returns: corrected_intensity: array_like
Corrected form factor intensity.
- corrections_dwi0bk(intensities, qys, qzs, df)[source]
Apply corrections to the form factor intensities.
- Parameters:
df – DataFrame containing the parameters for the trapezoids.
qxs – array-like List of qx/qz at which the form factor is simulated.
qzs – array-like List of qx/qz at which the form factor is simulated.
- Returns:
- array-like
Corrected form factor intensities.
- Return type:
intensities_corr
- trapezoid_form_factor(qys, qzs, y1, y2, df)[source]
Simulation of the form factor of a trapezoid at all qx, qz positions.
- Parameters:
qys – array-like List of qy/qz at which the form factor is simulated.
qzs – array-like List of qy/qz at which the form factor is simulated.
y1 – array-like Values of the bottom right and left position of the trapezoids.
y2 – array-like Values of the bottom right and left position of the trapezoids.
df – DataFrame containing the parameters for the trapezoids.
- Returns:
- array-like
Values of the form factor.
- Return type:
form_factor
- class cdsaxs.simulations.stacked_trapezoid.StackedTrapezoidGeometry(xp=<module 'numpy' from '/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/numpy/__init__.py'>, from_fitter=False, initial_guess=None)[source]
Bases:
Geometry
A class representing the geometry of stacked trapezoids for simulations.
- xp
The numpy-like module to use for array operations.
- Type:
module
- from_fitter
Flag indicating whether the geometry is created from a fitter.
- Type:
bool
- initial_guess
Dictionary containing the initial guess values for the geometry.
- Type:
dict
- initial_guess_dataframe
DataFrame containing the initial guess values for the geometry.
- Type:
DataFrame
- symmetric
Flag indicating whether the trapezoids are symmetric.
- Type:
bool
- calculate_ycoords(height, langle, rangle, y1, y2)[source]
Calculate y1 and y2 coordinates for each trapezoid.
- calculate_shift(height, langle)[source]
Calculate how much the trapezoids are shifted in the z direction.
- convert_to_dataframe(fitparams)[source]
Convert the fitparams to a DataFrame based on the initial guess DataFrame.
- rescale_fitparams(fitparams_df)[source]
Rescale the Gaussian distributed values obtained from the fitter to the actual values.
- check_physical_validity(rescaled_fitparams_df)[source]
Check if the values obtained from the fitter make physical sense.
- calculate_shift(df)[source]
Calculate how much the trapezoids are shifted in the z direction.
- Parameters:
df (pd.dataframe) – DataFrame containing the parameters for the trapezoids.
- Returns:
Array of calculated shift values.
- Return type:
shift (ndarray)
- calculate_ycoords(df)[source]
Calculate y1 and y2 coordinates for each trapezoid.
- Parameters:
df (pd.dataframe) – DataFrame containing the parameters for the trapezoids.
- Returns:
Tuple of 2D arrays of y1 and y2 values.
- Return type:
(y1 (ndarray), y2 (ndarray))
- check_physical_validity(rescaled_fitparams_df)[source]
Check if the values obtained from the fitter make physical sense.
- Parameters:
rescaled_fitparams_df (pd.dataframe) – DataFrame containing the rescaled values of the fitparams.
- Returns:
DataFrame containing the rescaled values of the fitparams with the values that do not make physical sense replaced by np.nan.
- Return type:
rescaled_df (pd.dataframe)
- convert_to_dataframe(fitparams)[source]
Convert the fitparams to a DataFrame based on the initial guess dataframe.
- Parameters:
fitparams (dictionary) – Array-like containing the Arguments for the trapezoids.
- Returns:
DataFrame containing the Arguments for the trapezoids.
- Return type:
pd_fitparams (dictionary)
- rescale_fitparams(fitparams_df)[source]
Rescale the Gaussian distributed values obtained from the fitter to the actual values and check if the values make physical sense.
- Parameters:
fitparams_df (pd.dataframe) – DataFrame containing the parameters for the trapezoids.
- Returns:
DataFrame containing the rescaled values of the fitparams.
- Return type:
rescaled_fitparams_df (pd.dataframe)
- set_initial_guess_dataframe(best_fit_cmaes_df=None)[source]
Set the initial guess values in a dataframe and put them in the attribute initial_guess_dataframe. If the fitter is MCMC, replace the initial guess values with the best estimate obtained from CMAES.
- Parameters:
best_fit_cmaes_df (DataFrame, optional) – The best fit Arguments obtained from the CMAES to be used by MCMC.
- class cdsaxs.simulations.stacked_trapezoid.StackedTrapezoidSimulation(qys, qzs, from_fitter=False, use_gpu=False, initial_guess=None)[source]
Bases:
Simulation
A class representing a simulation of stacked trapezoids for diffraction pattern calculation.
- qys
The q-values in the y-direction for diffraction calculation.
- Type:
array-like
- qzs
The q-values in the z-direction for diffraction calculation.
- Type:
array-like
- xp
The module used for numerical computations (numpy or cupy).
- Type:
module
- from_fitter
Indicates if the simulation is called from the fitter.
- Type:
bool
- TrapezoidGeometry
The geometry object for the stacked trapezoids.
- Type:
- TrapezoidDiffraction
The diffraction object for the stacked trapezoids.
- __init__(self, qys, qzs, from_fitter=False, use_gpu=False, initial_guess=None)[source]
Initializes the StackedTrapezoidSimulation object.
- simulate_diffraction(self, fitparams=None, fit_mode='cmaes', best_fit=None)[source]
Simulates the diffraction pattern of the stacked trapezoids.
- set_from_fitter(self, from_fitter, best_fit_cmaes_df=None)[source]
Sets the from_fitter attribute and updates the geometry object accordingly.
- geometry(self)
Returns the geometry object for the stacked trapezoids.
- property geometry
Returns the geometry object for the stacked trapezoids.
- Returns:
The geometry object for the stacked trapezoids.
- Return type:
TrapezoidGeometry (StackedTrapezoidGeometry)
- set_from_fitter(from_fitter, best_fit_cmaes_df=None)[source]
Sets the from_fitter attribute and updates the geometry object accordingly.
- Parameters:
from_fitter (bool) – Indicates if the simulation is called from the fitter.
best_fit_cmaes_df (pd.dataframe) – The best fit Arguments obtained from the CMAES.
- simulate_diffraction(params=None, fit_mode='cmaes')[source]
Simulates the diffraction pattern of the stacked trapezoids.
- Parameters:
fitparams (list, optional) – A list of floats coming from the fitter. Defaults to None.
best_fit (array-like, optional) – The best fit Arguments obtained from the fitter. Defaults to None.
- Returns:
A 2D array of floats containing the corrected intensity.
- Return type:
corrected_intensity (array-like)
cdsaxs.simulations.strong_castle module
This module is a child class of the StackedTrapezoidSimulation class. Its purpose is to implement the Strong Castle model which calculates the overlay.
- Classes:
StrongCastleSimulation: This class is a child class of the StackedTrapezoidSimulation class. StrongCastleGeometry: This class is a child class of the StackedTrapezoidGeometry class. StrongCastleDiffraction: This class is a child class of the StackedTrapezoidDiffraction class.
- class cdsaxs.simulations.strong_castle.StrongCastleDiffraction(TrapezoidGeometry, xp=<module 'numpy' from '/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/numpy/__init__.py'>)[source]
Bases:
StackedTrapezoidDiffraction
This class is a child class of the StackedTrapezoidDiffraction class. It modifies the correct_form_factor_intensity method to account for the two fixed params n1 and n2 which are the number of trapezoids in the first and second layer of trapezoids respectively.
- correct_form_factor_intensity(qys, qzs, fitparams)[source]
remove the fixed parameters n1 and n2 from the fitparams dictionary and call the parent class correct_form_factor_intensity method.
- Parameters:
qys (np.ndarray) – points in the y direction where form factor is calculated
qzs (np.ndarray) – points in the z direction where form factor is calculated
fitparams (np.ndarray) – a dictionary containing the parameters
- Returns:
intensity of the strong castle model
- Return type:
corrected_intensity (np.ndarray)
- class cdsaxs.simulations.strong_castle.StrongCastleGeometry(xp=<module 'numpy' from '/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/numpy/__init__.py'>, from_fitter=False, initial_guess=None)[source]
Bases:
StackedTrapezoidGeometry
This class is a child class of the StackedTrapezoidGeometry class. Several methods are modified and added to implement the Strong Castle model(notably the most important calculate_ycoords).
- calculate_ycoords(df)[source]
This is the modified version of the calculate_ycoords method in the parent class. The two trapezoids are separated into two groups and the y coordinates are calculated and the y coordinates of the second trapezoid are shifted by the overlay value.
- Parameters:
df (pandas.DataFrame) – dataframe containing the trapezoid parameters
- Returns:
y1 coordinates of the trapezoids y2 (numpy.ndarray): y2 coordinates of the trapezoids
- Return type:
y1 (numpy.ndarray)
- check_initial_guess(params)[source]
Check if the number of trapezoids n1+n2 is equal to the number of angles in the initial_guess dictionary
Args: initial_guess (dict): dictionary containing the initial guess for the parameters
- remove_fixed_params(initial_guess)[source]
Remove the fixed parameters which are the number of first and second layer of trapezoids n1 and n2 from the initial_guess dictionary so that they are not fitted.
- Parameters:
initial_guess (dictionary) – A dictionary containing the initial guess for the parameters provided by the user.
- Returns:
A dictionary containing the initial guess for the parameters without the fixed parameters.
- Return type:
initial_guess (dictionary)
- class cdsaxs.simulations.strong_castle.StrongCastleSimulation(qys, qzs, from_fitter=False, use_gpu=False, initial_guess=None)[source]
Bases:
StackedTrapezoidSimulation
This class is a child class of the StackedTrapezoidSimulation class.