Package slstools
Python package slstools
contains several helper functions and classes
for reading in data from the SCM SLS setup, Miescat
program, generating models
for the scattering of spheres of given diameter and polydispersity and
fitting those models to the experimental data.
The scattering calculations are based on miepython
.
Author: Roy Hoitink L.D.Hoitink@uu.nl
Classes
class Experiment (filename, normalise=True, K_unit='m', remove_data_after_pole=True, **kwargs)
-
Helper class for data generated by the SCM SLS setup.
For example usage, see
Fit
class.Attributes
filepath
:pathlib.Path
- Path object of the file containing the SLS data
data
:numpy.ndarray
- Numpy array containing all raw data from the file.
theta
:numpy.ndarray
- Numpy array containing the scattering angles in degrees.
intensity_uncor
:numpy.ndarray
- Numpy array containing scattering intensities, not corrected for scattering volume (perpendicular polarisation).
intensity
:numpy.ndarray
- Numpy array containing scattering intensities, corrected for scattering volume (perpendicular polarisation).
K
:numpy.ndarray
- Numpy array containing the scattering angles (in units of
K_unit
). K_unit
:str
- (inverse) unit of K, one of [
nm
,µm
,m
]. temperature
:numpy.ndarray
- Numpy array containing temperatures that were measured by the setup.
Initialisation of the class, assigns the data as class variables.
Parameters
filename
:str
- Path to file
normalise
:bool
, optional- Whether or not to normalise the intensity data to 1. The default is True.
K_unit
:str
, optional- Inverse units of K, one of [
nm
,µm
,m
]. E.g. ifnm
is given aK_unit
,K
will be given in units of. The default ism
.
Methods
def correct_for_reflection(self, n_m=1.333, n_glass=1.49)
-
Correct the intensity for reflection
Stores the corrected intensity as
intensity
atrribute of the class, to allow for easier fitting. The old intensities are now stored in theintensity_reflection_uncor
attribute.Due to reflections, we need to correct the measured intensity for reflection at the glass-air interface of the toluene vessel as follows:
I_{cor}(\theta) = I_m(\theta)-R*I_m(\pi - \theta)
with
- I_m(\theta) the measured intensity at a certain angle \theta
- R = \left( \frac{n_{glass} - n_{air}}{n_{glass}+ n_{air}} \right)^2 +\left( \frac{n_{glass} - n_{solvent}}{n_{glass}+ n_{solvent}} \right)^2
- n_{air} = 1.00, n_{glass}=
n_glass
, n_{solvent}=n_m
Parameters
n_m
:float
, optional- Refractive index of the medium, by default 1.333 (water).
n_glass
:float
, optional- Refractive index of the glass, by default 1.49.
def recalculate_K(self, n_m=1.333, wavelength=632.8)
-
Recalculate the
K
attribute of the instance, using the new medium refractive index and laser wavelengthParameters
n_m
:float
orcomplex
, optional- Refractive index of the medium, by default 1.333.
wavelength
:float
, optional- Vacuum wavelength of the laser in
nm
, by default 632.8.
class Fit (experiment, model, fit_theta_bounds=(45.0, 110.0), model_kwargs={})
-
Class for performing fitting a model to experimental data.
Example usage
from slstools import Experiment, Model, Fit experiment = Experiment("Sample01.sls", K_unit="nm") model = Model(d=1000, pd=5, n_p=1.4345, n_m=1.333) fit = Fit(experiment, model, model_kwargs=dict(K_unit="nm")) optimal_model = fit.fit() fit.save() # save parameters in same directory as the experiment
Initialize fitting a given
Model
to givenExperiment
Parameters
experiment
:str
orExperiment
- The experiment that is used for fitting, if a
str
is given this will be passed as filename to theExperiment
class. model
:Model
- Intial model that will be used for the fit, make sure to choose a sensible starting diameter and polydispersity as it helps to speed up the fitting. Also use the correct refractive indices for the particle and medium.
fit_theta_bounds
:tuple
, optional- Range of
theta
(scattering angle) that will be used for fitting data, by default(45.,110.)
. model_kwargs
:dict
, optional- Extra keyword arguments to pass onto the final model, by default
{}
.
Methods
def fit(self, debug=False)
-
Function to perform the actual fitting of the model to the experiment
Parameters
debug
:bool
, optional- Whether or not to output intermediate fitting parameters,
by default
False
.
Returns
Model
The Model with parameters that best fit the experiment Note: the intensities should be multiplied withself.optimal_params[0]
to be accurate to the experiment's intensities. def save(self, filename=None, overwrite=True, extra_info={}, save_theta=False)
-
Saves a dictionary, to a pickle file, containing a
model_parameters
part which can directly be plugged into theModel
class and afit_info
part which gives info on what file was fitted to, the theta range and the prefactor used for plotting model and experiment together.Parameters
filename
:str
, optional- Filename to save the data as dictionary in pkl format, by default None, which results in saving it as [experiment_filename]_fit.pkl
overwrite
:bool
, optional- Whether or not to overwrite the file if it already exists, by default True
extra_info
:dict
, optional- Extra data in dictionary form to store in the
fit_info
, by default{}
overwrite
:bool
, optional- Whether or not to save the list of angles (theta), by default False
Returns
bool
Whether or not it succeeded in saving the file
class Miescat (filename, K_unit='m', **kwargs)
-
Helper class for data generated by the miescat program.
Attributes
data
:numpy.ndarray
- Numpy array containing all raw data from the file.
theta
:numpy.ndarray
- Numpy array containing the scattering angles in degrees.
K
:numpy.ndarray
- Numpy array containing the scattering angles (in units of
K_unit
). K_unit
:str
- (inverse) unit of K, one of [
nm
,µm
,m
]. intensity
:numpy.ndarray
- Numpy array containing (normalised) scattering intensities (perpendicular polarised)
intensity_par
:numpy.ndarray
- Numpy array containing (normalised) scattering intensities (parallel polarised)
Initialisation of the class, assigns the data to objects attributes
Parameters
filename
:str
- Path to the file that contains the miescat data.
K_unit
:str
, optional- (inverse) unit of K, one of [
nm
,µm
,m
], by default "m".
class Model (n_p, d=1000.0, pd=0.0, n_m=1.333, wavelength=632.8, theta=None, K_unit='m', normalise=True)
-
Helper class to create a model for scattering spheres of given diameter (and polydispersity).
Example usage
import matplotlib.pyplot as plt from slstools import Model # intialise class with spheres of 1200 nm in diameter # with a polydispersity of 5% model = Model(d=1200.,pd=5.,n_p=1.4345) plt.plot(model.K, model.intensity) plt.xlabel(r"K ($%s^{-1}$)" % model.K_unit) plt.ylabel("Normalised intensity") plt.yscale("log") plt.show()
Attributes
K
:numpy.ndarray
- Numpy array containing the scattering angles in units of
K_unit
. intensity
:numpy.ndarray
- Numpy array containing scattering intensities for each angle (perpendicular polarisation).
Initialisation of the class, assigns data to class attributes and calculates model from given parameters.
Parameters
n_p
:float|complex
- (complex) refractive index of the particle.
d
:float
, optional- Diameter of the particle in
nm
, by default1000.0
. pd
:float
, optional- Polydispersity of the system in % (e.g.
5
for 5% p.d.), by default0.0
. n_m
:float
, optional- Refractive index of the medium, by default
1.333
(water). wavelength
:float
, optional- Vacuum wavelength of the laser in
nm
, by default 632.8 (HeNe laser). theta
:numpy.ndarray
, optional- List of angles to calculate intensities for,
by default
np.linspace(0.0, 180.0, 361)
K_unit
:str
, optional- (inverse) unit of scattering angle K, by default
m
. normalise
:bool
, optional- Whether or not to normalise the intensities to
1.0
, by defaultTrue
.