Simple walkthrough
Contents
Simple walkthrough¶
The heart of the DDM code (found in the ddm_calc.py
file) is the computation of the image structure function. This is found by taking the average of the Fourier transforms of all image differences. By “image differences,” I mean the result of subtracting two images separated by a given lag time \(\Delta t\).
To describe the process mathematically, we find the difference between images separated by some lag time \(\Delta t\):
For a given \(\Delta t\) all such image differences are calculated. We then Fourier transform each \(\Delta I\) and average all of the same \(\Delta t\).
This results in the image structure function \(D(q_x,q_y,\Delta t)\).
Importing the necessary modules¶
Modules for plotting¶
We use matplotlib for creating figures and plots. Note that we use:
%matplotlib inline
This sets the backend of matplotlib to inline
which means the plots are included in the notebook. If you want the plots to also be interactive (e.g., having the ability to zoom, scroll, and save) then use:
%matplotlib notebook
[1]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
Modules for numerical work¶
Here, we import numpy and xarray.
Note that xarray
might not have come with your Anaconda Python distribution (or whichever other distribution you installed). If that is the case, you’ll need to install this package.
[2]:
import numpy as np #numerical python used for working with arrays, mathematical operations
import xarray as xr #package for labeling and adding metadata to multi-dimensional arrays
Initializing DDM_Analysis class and computing the DDM matrix¶
The instance of the DDM_Analysis
class we create will need, when initialized, metadata about the images to analyze and the analysis and fitting parameters. This can be done by using a yaml file as shown in the following cell of code (there, the metadata is saved in the file “example_data_silica_beads.yml”. But one can also initialize DDM_Analysis
with a dictionary containing the necessary metadata. One way to create such a dictionary and then using it to
initialize DDM_Analysis
is shown below.
import yaml
ddm_analysis_parameters_str = """
DataDirectory: 'C:/Users/rmcgorty/Documents/GitHub/DDM-at-USD/ExampleData/'
FileName: 'images_nobin_40x_128x128_8bit.tif'
Metadata:
pixel_size: 0.242 # size of pixel in um
frame_rate: 41.7 #frames per second
Analysis_parameters:
number_lag_times: 40
last_lag_time: 600
binning: no
Fitting_parameters:
model: 'DDM Matrix - Single Exponential'
Tau: [1.0, 0.001, 10]
StretchingExp: [1.0, 0.5, 1.1]
Amplitude: [1e2, 1, 1e6]
Background: [2.5e4, 0, 1e7]
Good_q_range: [5, 20]
Auto_update_good_q_range: True
"""
parameters_as_dictionary = yaml.safe_load(ddm_analysis_parameters_str)
ddm_calc = ddm.DDM_Analysis(parameters_as_dictionary)
[4]:
#The yaml file `example_data_silica_beads.yml` contains the metadata and parameters above
ddm_calc = ddm.DDM_Analysis("../../Examples/example_data_silica_beads.yml")
Provided metadata: {'pixel_size': 0.242, 'frame_rate': 41.7}
Image shape: 3000-by-128-by-128
Number of frames to use for analysis: 3000
Maximum lag time (in frames): 600
Number of lag times to compute DDM matrix: 40
Below, with the method calculate_DDM_matrix
, we compute the DDM matrix and some associated data. The data will be stored as an xarray Dataset as an attribute to ddm_calc
called ddm_dataset
.
Note: There are a few optional arguments we can pass to calculate_DDM_matrix
. There is an optional argument quiet
(True or False, False by default). Then we have some optional keyword arguments (all of which could also be specified in the YAML file). These are: overlap_method
which sets the degree of overlap between image pairs when finding all image differences for a each lag time and is either 0, 1, 2, or 3, background_method
which sets how to estimate the
background parameter B and is either 0, 1, 2, or 3, and number_lag_times
. If any of these three keyword arguments are set here, the value specified in the YAML file will be overwritten.
[5]:
#If a file is found that matches what, by default, the program will save the computed DDM matrix as,
#then you will be prompted as to whether or not you want to proceed with this calculation. If you
#do not, then enter 'n' and the program will read from the existing file to load the DDM dataset
#into memory. If you enter 'y', then the DDM matrix will be calculated.
ddm_calc.calculate_DDM_matrix()
2022-02-10 13:11:37,747 - DDM Calculations - Running dt = 1...
2022-02-10 13:11:44,311 - DDM Calculations - Running dt = 5...
2022-02-10 13:11:48,710 - DDM Calculations - Running dt = 9...
2022-02-10 13:11:52,546 - DDM Calculations - Running dt = 16...
2022-02-10 13:11:55,853 - DDM Calculations - Running dt = 27...
2022-02-10 13:11:59,025 - DDM Calculations - Running dt = 47...
2022-02-10 13:12:02,002 - DDM Calculations - Running dt = 81...
2022-02-10 13:12:04,828 - DDM Calculations - Running dt = 138...
2022-02-10 13:12:07,484 - DDM Calculations - Running dt = 236...
2022-02-10 13:12:09,998 - DDM Calculations - Running dt = 402...
DDM matrix took 34.66797375679016 seconds to compute.
Background estimate ± std is 211.17 ± 1.49
[5]:
<xarray.Dataset> Dimensions: (lagtime: 40, q_y: 128, q_x: 128, q: 64, y: 128, x: 128, frames: 40) Coordinates: * lagtime (lagtime) float64 0.02398 0.04796 0.07194 ... 12.59 14.36 framelag (frames) int32 1 2 3 4 5 6 7 ... 308 352 402 459 525 599 * q_y (q_y) float64 -12.98 -12.78 -12.58 ... 12.37 12.58 12.78 * q_x (q_x) float64 -12.98 -12.78 -12.58 ... 12.37 12.58 12.78 * q (q) float64 0.0 0.2028 0.4057 0.6085 ... 12.37 12.58 12.78 * y (y) int32 0 1 2 3 4 5 6 7 ... 121 122 123 124 125 126 127 * x (x) int32 0 1 2 3 4 5 6 7 ... 121 122 123 124 125 126 127 Dimensions without coordinates: frames Data variables: ddm_matrix_full (lagtime, q_y, q_x) float64 194.5 183.5 ... 192.0 196.8 ddm_matrix (lagtime, q) float64 0.0 294.2 321.4 ... 207.8 201.1 200.4 first_image (y, x) float64 128.0 149.0 173.0 ... 175.0 229.0 215.0 avg_image_ft (q) float64 0.0 1.293e+05 5.225e+03 ... 105.3 104.7 105.3 num_pairs_per_dt (lagtime) int32 2999 2998 2997 1498 1498 ... 20 17 15 13 B float64 211.2 B_std float64 1.491 Amplitude (q) float64 -211.2 2.585e+05 1.024e+04 ... -1.699 -0.52 ISF (lagtime, q) float64 0.0 0.9997 0.9892 ... -4.952 -19.73 Attributes: (12/24) units: Intensity lagtime: sec q: μm$^{-1}$ x: pixels y: pixels info: ddm_matrix is the averages of FFT difference ima... ... ... split_into_4_rois: no use_windowing_function: no binning: no bin_size: 2 central_angle: no angle_range: no
- lagtime: 40
- q_y: 128
- q_x: 128
- q: 64
- y: 128
- x: 128
- frames: 40
- lagtime(lagtime)float640.02398 0.04796 ... 12.59 14.36
array([ 0.023981, 0.047962, 0.071942, 0.095923, 0.119904, 0.143885, 0.167866, 0.191847, 0.215827, 0.263789, 0.28777 , 0.335731, 0.383693, 0.431655, 0.503597, 0.57554 , 0.647482, 0.743405, 0.863309, 0.983213, 1.127098, 1.294964, 1.486811, 1.702638, 1.942446, 2.206235, 2.541966, 2.901679, 3.309353, 3.788969, 4.316547, 4.940048, 5.659472, 6.450839, 7.386091, 8.441247, 9.640288, 11.007194, 12.589928, 14.364508])
- framelag(frames)int321 2 3 4 5 6 ... 352 402 459 525 599
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 14, 16, 18, 21, 24, 27, 31, 36, 41, 47, 54, 62, 71, 81, 92, 106, 121, 138, 158, 180, 206, 236, 269, 308, 352, 402, 459, 525, 599])
- q_y(q_y)float64-12.98 -12.78 ... 12.58 12.78
array([-12.981788, -12.778947, -12.576107, -12.373267, -12.170426, -11.967586, -11.764745, -11.561905, -11.359064, -11.156224, -10.953383, -10.750543, -10.547703, -10.344862, -10.142022, -9.939181, -9.736341, -9.5335 , -9.33066 , -9.12782 , -8.924979, -8.722139, -8.519298, -8.316458, -8.113617, -7.910777, -7.707937, -7.505096, -7.302256, -7.099415, -6.896575, -6.693734, -6.490894, -6.288053, -6.085213, -5.882373, -5.679532, -5.476692, -5.273851, -5.071011, -4.86817 , -4.66533 , -4.46249 , -4.259649, -4.056809, -3.853968, -3.651128, -3.448287, -3.245447, -3.042607, -2.839766, -2.636926, -2.434085, -2.231245, -2.028404, -1.825564, -1.622723, -1.419883, -1.217043, -1.014202, -0.811362, -0.608521, -0.405681, -0.20284 , 0. , 0.20284 , 0.405681, 0.608521, 0.811362, 1.014202, 1.217043, 1.419883, 1.622723, 1.825564, 2.028404, 2.231245, 2.434085, 2.636926, 2.839766, 3.042607, 3.245447, 3.448287, 3.651128, 3.853968, 4.056809, 4.259649, 4.46249 , 4.66533 , 4.86817 , 5.071011, 5.273851, 5.476692, 5.679532, 5.882373, 6.085213, 6.288053, 6.490894, 6.693734, 6.896575, 7.099415, 7.302256, 7.505096, 7.707937, 7.910777, 8.113617, 8.316458, 8.519298, 8.722139, 8.924979, 9.12782 , 9.33066 , 9.5335 , 9.736341, 9.939181, 10.142022, 10.344862, 10.547703, 10.750543, 10.953383, 11.156224, 11.359064, 11.561905, 11.764745, 11.967586, 12.170426, 12.373267, 12.576107, 12.778947])
- q_x(q_x)float64-12.98 -12.78 ... 12.58 12.78
array([-12.981788, -12.778947, -12.576107, -12.373267, -12.170426, -11.967586, -11.764745, -11.561905, -11.359064, -11.156224, -10.953383, -10.750543, -10.547703, -10.344862, -10.142022, -9.939181, -9.736341, -9.5335 , -9.33066 , -9.12782 , -8.924979, -8.722139, -8.519298, -8.316458, -8.113617, -7.910777, -7.707937, -7.505096, -7.302256, -7.099415, -6.896575, -6.693734, -6.490894, -6.288053, -6.085213, -5.882373, -5.679532, -5.476692, -5.273851, -5.071011, -4.86817 , -4.66533 , -4.46249 , -4.259649, -4.056809, -3.853968, -3.651128, -3.448287, -3.245447, -3.042607, -2.839766, -2.636926, -2.434085, -2.231245, -2.028404, -1.825564, -1.622723, -1.419883, -1.217043, -1.014202, -0.811362, -0.608521, -0.405681, -0.20284 , 0. , 0.20284 , 0.405681, 0.608521, 0.811362, 1.014202, 1.217043, 1.419883, 1.622723, 1.825564, 2.028404, 2.231245, 2.434085, 2.636926, 2.839766, 3.042607, 3.245447, 3.448287, 3.651128, 3.853968, 4.056809, 4.259649, 4.46249 , 4.66533 , 4.86817 , 5.071011, 5.273851, 5.476692, 5.679532, 5.882373, 6.085213, 6.288053, 6.490894, 6.693734, 6.896575, 7.099415, 7.302256, 7.505096, 7.707937, 7.910777, 8.113617, 8.316458, 8.519298, 8.722139, 8.924979, 9.12782 , 9.33066 , 9.5335 , 9.736341, 9.939181, 10.142022, 10.344862, 10.547703, 10.750543, 10.953383, 11.156224, 11.359064, 11.561905, 11.764745, 11.967586, 12.170426, 12.373267, 12.576107, 12.778947])
- q(q)float640.0 0.2028 0.4057 ... 12.58 12.78
array([ 0. , 0.20284 , 0.405681, 0.608521, 0.811362, 1.014202, 1.217043, 1.419883, 1.622723, 1.825564, 2.028404, 2.231245, 2.434085, 2.636926, 2.839766, 3.042607, 3.245447, 3.448287, 3.651128, 3.853968, 4.056809, 4.259649, 4.46249 , 4.66533 , 4.86817 , 5.071011, 5.273851, 5.476692, 5.679532, 5.882373, 6.085213, 6.288053, 6.490894, 6.693734, 6.896575, 7.099415, 7.302256, 7.505096, 7.707937, 7.910777, 8.113617, 8.316458, 8.519298, 8.722139, 8.924979, 9.12782 , 9.33066 , 9.5335 , 9.736341, 9.939181, 10.142022, 10.344862, 10.547703, 10.750543, 10.953383, 11.156224, 11.359064, 11.561905, 11.764745, 11.967586, 12.170426, 12.373267, 12.576107, 12.778947])
- y(y)int320 1 2 3 4 5 ... 123 124 125 126 127
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])
- x(x)int320 1 2 3 4 5 ... 123 124 125 126 127
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])
- ddm_matrix_full(lagtime, q_y, q_x)float64194.5 183.5 189.9 ... 192.0 196.8
array([[[194.53933642, 183.46865602, 189.88360447, ..., 185.13501835, 189.88360447, 183.46865602], [180.54977527, 184.19554752, 188.62431121, ..., 190.98754022, 197.43847912, 186.62178023], [192.70890797, 195.93098872, 189.09845247, ..., 190.12737145, 189.83523821, 192.21421257], ..., [178.74506797, 186.95925279, 189.9972449 , ..., 190.31796269, 193.86216905, 184.71498966], [192.70890797, 192.21421257, 189.83523821, ..., 194.82373533, 189.09845247, 195.93098872], [180.54977527, 186.62178023, 197.43847912, ..., 189.320714 , 188.62431121, 184.19554752]], [[192.53678443, 184.98536047, 183.00178339, ..., 189.93758386, 183.00178339, 184.98536047], [182.22340665, 190.38208686, 185.90468391, ..., 188.79454507, 188.52514631, 188.48204681], [192.71415943, 196.71513926, 189.84504279, ..., 187.27424593, 187.74393152, 186.99244495], ... [224.60947387, 108.81451286, 189.02957545, ..., 137.44506309, 348.30437843, 228.73579002], [216.96920343, 166.46373793, 174.79469182, ..., 166.69258334, 126.31953212, 140.52349943], [164.20961535, 169.57957548, 133.76022938, ..., 125.77861768, 159.69240297, 247.86589588]], [[ 75.33297495, 174.05006796, 155.35027909, ..., 233.98667431, 155.35027909, 174.05006796], [167.22701098, 196.84765156, 192.02546007, ..., 131.6447673 , 119.41026107, 235.53961919], [132.75085561, 149.1865022 , 144.63052109, ..., 237.29431965, 217.6141243 , 128.83335999], ..., [244.06105601, 195.72999615, 198.70767797, ..., 346.86825864, 208.46454057, 182.9136883 ], [132.75085561, 128.83335999, 217.6141243 , ..., 137.87697986, 144.63052109, 149.1865022 ], [167.22701098, 235.53961919, 119.41026107, ..., 165.83630492, 192.02546007, 196.84765156]]])
- ddm_matrix(lagtime, q)float640.0 294.2 321.4 ... 201.1 200.4
array([[ 0. , 294.18943955, 321.41822078, ..., 192.93331805, 193.46556826, 192.76671589], [ 0. , 432.05714374, 490.9677582 , ..., 194.84772175, 195.06799833, 194.40459328], [ 0. , 535.36374381, 619.95883018, ..., 196.12616285, 196.21094092, 195.79399928], ..., [ 0. , 3966.56056507, 5791.89215548, ..., 205.00017796, 211.36062787, 212.61362392], [ 0. , 4167.1712352 , 5456.14848464, ..., 203.8398704 , 205.38454854, 207.78696548], [ 0. , 3716.77402137, 6067.88205869, ..., 207.76466695, 201.06258553, 200.39635905]])
- first_image(y, x)float64128.0 149.0 173.0 ... 229.0 215.0
array([[128., 149., 173., ..., 178., 224., 255.], [164., 163., 166., ..., 182., 197., 255.], [208., 182., 175., ..., 178., 210., 255.], ..., [147., 162., 162., ..., 182., 196., 165.], [201., 214., 234., ..., 189., 174., 178.], [255., 255., 255., ..., 175., 229., 215.]])
- avg_image_ft(q)float640.0 1.293e+05 ... 104.7 105.3
array([0.00000000e+00, 1.29341509e+05, 5.22511731e+03, 5.86989569e+03, 8.50478146e+03, 9.68446541e+03, 9.99103750e+03, 1.03319622e+04, 1.00264087e+04, 1.08058729e+04, 1.16343568e+04, 1.38669622e+04, 1.68988722e+04, 1.98642184e+04, 2.11800678e+04, 2.14311022e+04, 2.06130601e+04, 1.80258072e+04, 1.42561639e+04, 1.04082328e+04, 7.27537584e+03, 4.68549415e+03, 2.88015028e+03, 1.77658315e+03, 1.06139768e+03, 6.83505113e+02, 4.97908069e+02, 4.18238638e+02, 3.38383278e+02, 2.72050227e+02, 2.35672326e+02, 2.24525405e+02, 2.03430129e+02, 1.78436880e+02, 1.65009527e+02, 1.49291391e+02, 1.41201296e+02, 1.33121051e+02, 1.29764821e+02, 1.28159612e+02, 1.24131175e+02, 1.24052104e+02, 1.21220015e+02, 1.19076951e+02, 1.17509287e+02, 1.15310101e+02, 1.15268069e+02, 1.12572652e+02, 1.11849673e+02, 1.11765243e+02, 1.10247178e+02, 1.09703002e+02, 1.08834403e+02, 1.09662333e+02, 1.08597899e+02, 1.07728419e+02, 1.07789289e+02, 1.06907057e+02, 1.07036538e+02, 1.05981891e+02, 1.05125261e+02, 1.05313067e+02, 1.04737380e+02, 1.05326832e+02])
- num_pairs_per_dt(lagtime)int322999 2998 2997 1498 ... 20 17 15 13
array([2999, 2998, 2997, 1498, 1498, 1497, 998, 998, 997, 748, 747, 598, 498, 497, 426, 372, 331, 270, 247, 212, 185, 164, 140, 123, 109, 94, 81, 71, 63, 54, 47, 41, 35, 31, 27, 23, 20, 17, 15, 13])
- B()float64211.2
array(211.17365621)
- B_std()float641.491
array(1.49105881)
- Amplitude(q)float64-211.2 2.585e+05 ... -1.699 -0.52
array([-2.11173656e+02, 2.58471844e+05, 1.02390610e+04, 1.15286177e+04, 1.67983893e+04, 1.91577572e+04, 1.97709014e+04, 2.04527506e+04, 1.98416438e+04, 2.14005721e+04, 2.30575400e+04, 2.75227507e+04, 3.35865707e+04, 3.95172631e+04, 4.21489620e+04, 4.26510308e+04, 4.10149466e+04, 3.58404407e+04, 2.83011541e+04, 2.06052919e+04, 1.43395780e+04, 9.15981465e+03, 5.54912690e+03, 3.34199265e+03, 1.91162170e+03, 1.15583657e+03, 7.84642482e+02, 6.25303620e+02, 4.65592900e+02, 3.32926798e+02, 2.60170996e+02, 2.37877154e+02, 1.95686602e+02, 1.45700103e+02, 1.18845397e+02, 8.74091262e+01, 7.12289354e+01, 5.50684455e+01, 4.83559858e+01, 4.51455685e+01, 3.70886933e+01, 3.69305527e+01, 3.12663737e+01, 2.69802467e+01, 2.38449170e+01, 1.94465450e+01, 1.93624826e+01, 1.39716478e+01, 1.25256899e+01, 1.23568306e+01, 9.32069947e+00, 8.23234748e+00, 6.49514954e+00, 8.15100953e+00, 6.02214129e+00, 4.28318246e+00, 4.40492236e+00, 2.64045708e+00, 2.89942014e+00, 7.90126460e-01, -9.23134186e-01, -5.47522559e-01, -1.69889697e+00, -5.19992889e-01])
- ISF(lagtime, q)float640.0 0.9997 0.9892 ... -4.952 -19.73
array([[ 0. , 0.99967882, 0.98923294, ..., -32.31431348, -9.42328539, -34.39844623], [ 0. , 0.99914543, 0.97267385, ..., -28.81782979, -8.48006746, -31.24863895], [ 0. , 0.99874574, 0.96007591, ..., -26.48287376, -7.80731178, -28.57666779], ..., [ 0. , 0.98547081, 0.45495798, ..., -10.27529479, 1.11005474, 3.76920654], [ 0. , 0.98469467, 0.48774845, ..., -12.39449068, -2.40756842, -5.51295585], [ 0. , 0.98643721, 0.42800337, ..., -5.22620786, -4.95155025, -19.7258549 ]])
- units :
- Intensity
- lagtime :
- sec
- q :
- μm$^{-1}$
- x :
- pixels
- y :
- pixels
- info :
- ddm_matrix is the averages of FFT difference images, ravs are the radial averages
- BackgroundMethod :
- 0
- OverlapMethod :
- 2
- DataDirectory :
- C:/Users/rmcgorty/Documents/GitHub/PyDDM/Examples/
- FileName :
- images_nobin_40x_128x128_8bit.tif
- pixel_size :
- 0.242
- frame_rate :
- 41.7
- starting_frame_number :
- no
- ending_frame_number :
- no
- number_lagtimes :
- 40
- first_lag_time :
- yes
- last_lag_time :
- 600
- crop_to_roi :
- no
- split_into_4_rois :
- no
- use_windowing_function :
- no
- binning :
- no
- bin_size :
- 2
- central_angle :
- no
- angle_range :
- no
Initiazing DDM_Fit class and fitting our data to a model¶
Next, we initialize the DDM_Fit
class which will help us fit the calculated DDM matrix to a model of our choosing.
When the DDM_Fit
class is initialized, it will display a table of the parameters, with the initial guesses and bounds, that were in the parameters of the yaml file. It will also indicate if, for the chosen model, initial guesses for parameters are missing.
Note: The intial guesses and bounds for the parameters are given in the yaml file. But you may realize that you need to adjust the bounds. You could change the yaml file and reload it. But you can also change the initial guesses and bounds for the parameters as shown in this code snippet:
ddm_fit.set_parameter_initial_guess('Tau', 0.5)
ddm_fit.set_parameter_bounds('Tau', [0.001, 1000])
Also, note that to initialize the DDM_Fit
class, we need to pass it the YAML file which contains the metadata and parameters. Below, we use the fact that the path to the YAML file is stored in the variable ddm_calc.data_yaml
. But we could also use the path to the YAML file just like we did when initializing the DDM_Analysis class. That is, this would work as well:
ddm_fit = ddm.DDM_Fit("../../Examples/example_data_silica_beads.yml")
[6]:
ddm_fit = ddm.DDM_Fit(ddm_calc.data_yaml)
Initial guess | Minimum | Maximum | |
---|---|---|---|
Amplitude | 100.0 | 1.000 | 1000000.0 |
Tau | 1.0 | 0.001 | 10.0 |
Background | 25000.0 | 0.000 | 10000000.0 |
StretchingExp | 1.0 | 0.500 | 1.1 |
Loading file C:/Users/rmcgorty/Documents/GitHub/PyDDM/Examples/images_nobin_40x_128x128_8bit_ddmmatrix.nc ...
Below, the fit is performed and saved into an xarray Dataset. We also have a table of the parameters for each q. Since this table may be long (especially if you have a large image size), you might want to not display it. If that’s the case, then run this command with the display_table
parmeter set to False, i.e.:
fit01 = ddm_fit.fit(name_fit = 'fit01', display_table=False)
[7]:
#Note that the method `fit` has many optional parameters, but here we
# allow those optional parameter to take their default values
fit01 = ddm_fit.fit(name_fit = 'fit01')
In function 'get_tau_vs_q_fit', using new tau...
Fit is saved in fittings dictionary with key 'fit01'.
q | Amplitude | Tau | Background | StretchingExp | |
---|---|---|---|---|---|
0 | 0.000000 | 1.000000 | 10.000000 | 0.000000 | 1.100000 |
1 | 0.202840 | 5009.160959 | 10.000000 | 125.265537 | 0.500000 |
2 | 0.405681 | 8482.808495 | 10.000000 | 0.000000 | 0.525256 |
3 | 0.608521 | 10442.590957 | 3.868067 | 110.613248 | 0.710762 |
4 | 0.811362 | 16595.852548 | 2.556070 | 83.269636 | 0.811578 |
5 | 1.014202 | 18774.162108 | 1.424364 | 101.316228 | 0.909133 |
6 | 1.217043 | 20126.206549 | 1.035536 | 37.943122 | 0.911751 |
7 | 1.419883 | 20269.935277 | 0.742219 | 30.165661 | 0.952913 |
8 | 1.622723 | 19408.933976 | 0.552100 | 0.000000 | 0.955882 |
9 | 1.825564 | 21442.103307 | 0.465083 | 0.000000 | 0.977973 |
10 | 2.028404 | 23388.120235 | 0.396199 | 0.000000 | 0.968451 |
11 | 2.231245 | 27490.759703 | 0.321218 | 0.000000 | 1.008676 |
12 | 2.434085 | 33464.835149 | 0.290591 | 0.000000 | 1.014272 |
13 | 2.636926 | 39525.290291 | 0.248451 | 0.000000 | 1.040113 |
14 | 2.839766 | 41793.934306 | 0.213868 | 0.000000 | 1.030231 |
15 | 3.042607 | 42078.124470 | 0.192926 | 0.000000 | 1.030781 |
16 | 3.245447 | 40768.461703 | 0.172733 | 0.000000 | 1.045874 |
17 | 3.448287 | 35721.127412 | 0.155423 | 0.000000 | 1.043916 |
18 | 3.651128 | 28431.332255 | 0.142240 | 0.000000 | 1.043399 |
19 | 3.853968 | 20619.257623 | 0.128071 | 0.000000 | 1.054034 |
20 | 4.056809 | 14370.154840 | 0.121052 | 0.000000 | 1.035008 |
21 | 4.259649 | 9293.652992 | 0.111708 | 0.000000 | 1.025896 |
22 | 4.462490 | 5662.007475 | 0.102243 | 0.000000 | 1.008149 |
23 | 4.665330 | 3487.994425 | 0.097775 | 0.000000 | 0.971809 |
24 | 4.868170 | 2077.865135 | 0.090379 | 0.000000 | 0.913350 |
25 | 5.071011 | 1223.115625 | 0.089518 | 83.952238 | 0.911770 |
26 | 5.273851 | 876.927434 | 0.083446 | 84.578516 | 0.860156 |
27 | 5.476692 | 690.124583 | 0.081228 | 107.731737 | 0.859201 |
28 | 5.679532 | 559.962532 | 0.074982 | 89.841197 | 0.757985 |
29 | 5.882373 | 417.404173 | 0.077395 | 113.007189 | 0.732307 |
30 | 6.085213 | 339.585926 | 0.074093 | 121.429878 | 0.702539 |
31 | 6.288053 | 328.928162 | 0.063312 | 104.741958 | 0.648010 |
32 | 6.490894 | 273.021363 | 0.065949 | 123.344201 | 0.657741 |
33 | 6.693734 | 210.366431 | 0.069732 | 138.341989 | 0.630490 |
34 | 6.896575 | 168.823091 | 0.078894 | 151.432022 | 0.624047 |
35 | 7.099415 | 120.146768 | 0.096006 | 168.399637 | 0.643459 |
36 | 7.302256 | 90.471468 | 0.114569 | 179.212366 | 0.696849 |
37 | 7.505096 | 73.280303 | 0.143625 | 183.974447 | 0.707400 |
38 | 7.707937 | 63.315036 | 0.145241 | 186.298290 | 0.743781 |
39 | 7.910777 | 54.490768 | 0.146684 | 188.785276 | 0.771679 |
40 | 8.113617 | 48.703834 | 0.161850 | 189.738060 | 0.784964 |
41 | 8.316458 | 46.274244 | 0.157325 | 189.710146 | 0.805431 |
42 | 8.519298 | 42.999737 | 0.150650 | 188.305785 | 0.739396 |
43 | 8.722139 | 41.307921 | 0.160369 | 188.695165 | 0.715729 |
44 | 8.924979 | 37.804395 | 0.155632 | 188.636134 | 0.761223 |
45 | 9.127820 | 35.459070 | 0.163358 | 188.442035 | 0.690826 |
46 | 9.330660 | 31.309845 | 0.158481 | 191.127615 | 0.879080 |
47 | 9.533500 | 27.495879 | 0.159557 | 190.705808 | 0.826357 |
48 | 9.736341 | 29.578351 | 0.164955 | 189.489710 | 0.747781 |
49 | 9.939181 | 27.750185 | 0.173154 | 190.573335 | 0.817289 |
50 | 10.142022 | 26.022042 | 0.169798 | 190.445764 | 0.694703 |
51 | 10.344862 | 25.817798 | 0.149565 | 188.926426 | 0.677988 |
52 | 10.547703 | 20.964509 | 0.171498 | 192.258742 | 0.865291 |
53 | 10.750543 | 22.212659 | 0.186064 | 191.315356 | 0.803059 |
54 | 10.953383 | 20.296560 | 0.175117 | 191.111831 | 0.815027 |
55 | 11.156224 | 17.733487 | 0.177052 | 191.985542 | 0.975116 |
56 | 11.359064 | 20.012534 | 0.174850 | 190.372072 | 0.764394 |
57 | 11.561905 | 17.740871 | 0.167563 | 190.855691 | 0.796393 |
58 | 11.764745 | 19.164617 | 0.177914 | 190.699793 | 0.827180 |
59 | 11.967586 | 17.974918 | 0.176198 | 190.556119 | 0.771980 |
60 | 12.170426 | 15.801139 | 0.197665 | 191.322769 | 0.965799 |
61 | 12.373267 | 15.561198 | 0.167777 | 190.627761 | 0.941686 |
62 | 12.576107 | 14.880392 | 0.194683 | 191.407404 | 0.907700 |
63 | 12.778947 | 15.539401 | 0.183432 | 190.803455 | 0.990262 |
Inspecting the outcome of the fit¶
We can now take a look at the best fit parameters determined by the fitting code. We can generate a set of plots and have the output saved as PDF with the function fit_report
. This takes a few arguments, including: * the result of the fit in an xarray Dataset (here fit01
) * q_indices
: the DDM matrix or the ISF (whichever was used in the fitting) will be plotted at these \(q\)-values specified by their index in the array of \(q\)-values * forced_qs
: range of
\(q\)-values (specified, again, by the index) for which to extract power law relationship between the decay time \(\tau\) and \(q\)
[8]:
ddm.fit_report(fit01, q_indices=[3,6,9,22], forced_qs=[4,16], use_new_tau=True, show=True)
In function 'get_tau_vs_q_fit', using new tau...
In hf.plot_one_tau_vs_q function, using new tau...
[8]:
<xarray.Dataset> Dimensions: (parameter: 4, q: 64, lagtime: 40) Coordinates: * parameter (parameter) <U13 'Amplitude' 'Tau' ... 'StretchingExp' * q (q) float64 0.0 0.2028 0.4057 0.6085 ... 12.37 12.58 12.78 * lagtime (lagtime) float64 0.02398 0.04796 0.07194 ... 12.59 14.36 Data variables: parameters (parameter, q) float64 1.0 5.009e+03 ... 0.9077 0.9903 theory (lagtime, q) float64 0.001311 364.7 349.3 ... 206.3 206.3 isf_data (lagtime, q) float64 0.0 0.9997 0.9892 ... -4.952 -19.73 ddm_matrix_data (lagtime, q) float64 0.0 294.2 321.4 ... 207.8 201.1 200.4 A (q) float64 -211.2 2.585e+05 1.024e+04 ... -1.699 -0.52 B float64 211.2 Attributes: (12/18) model: DDM Matrix - Single Exponential data_to_use: DDM Matrix initial_params_dict: ["{'n': 0, 'value': 100.0, 'limits': [1.0... effective_diffusion_coeff: 0.616760567419975 tau_vs_q_slope: [-1.9799845] msd_alpha: [1.01010892] ... ... DataDirectory: C:/Users/rmcgorty/Documents/GitHub/PyDDM/... FileName: images_nobin_40x_128x128_8bit.tif pixel_size: 0.242 frame_rate: 41.7 BackgroundMethod: 0 OverlapMethod: 2
- parameter: 4
- q: 64
- lagtime: 40
- parameter(parameter)<U13'Amplitude' ... 'StretchingExp'
array(['Amplitude', 'Tau', 'Background', 'StretchingExp'], dtype='<U13')
- q(q)float640.0 0.2028 0.4057 ... 12.58 12.78
array([ 0. , 0.20284 , 0.405681, 0.608521, 0.811362, 1.014202, 1.217043, 1.419883, 1.622723, 1.825564, 2.028404, 2.231245, 2.434085, 2.636926, 2.839766, 3.042607, 3.245447, 3.448287, 3.651128, 3.853968, 4.056809, 4.259649, 4.46249 , 4.66533 , 4.86817 , 5.071011, 5.273851, 5.476692, 5.679532, 5.882373, 6.085213, 6.288053, 6.490894, 6.693734, 6.896575, 7.099415, 7.302256, 7.505096, 7.707937, 7.910777, 8.113617, 8.316458, 8.519298, 8.722139, 8.924979, 9.12782 , 9.33066 , 9.5335 , 9.736341, 9.939181, 10.142022, 10.344862, 10.547703, 10.750543, 10.953383, 11.156224, 11.359064, 11.561905, 11.764745, 11.967586, 12.170426, 12.373267, 12.576107, 12.778947])
- lagtime(lagtime)float640.02398 0.04796 ... 12.59 14.36
array([ 0.023981, 0.047962, 0.071942, 0.095923, 0.119904, 0.143885, 0.167866, 0.191847, 0.215827, 0.263789, 0.28777 , 0.335731, 0.383693, 0.431655, 0.503597, 0.57554 , 0.647482, 0.743405, 0.863309, 0.983213, 1.127098, 1.294964, 1.486811, 1.702638, 1.942446, 2.206235, 2.541966, 2.901679, 3.309353, 3.788969, 4.316547, 4.940048, 5.659472, 6.450839, 7.386091, 8.441247, 9.640288, 11.007194, 12.589928, 14.364508])
- parameters(parameter, q)float641.0 5.009e+03 ... 0.9077 0.9903
array([[1.00000000e+00, 5.00916096e+03, 8.48280850e+03, 1.04425910e+04, 1.65958525e+04, 1.87741621e+04, 2.01262065e+04, 2.02699353e+04, 1.94089340e+04, 2.14421033e+04, 2.33881202e+04, 2.74907597e+04, 3.34648351e+04, 3.95252903e+04, 4.17939343e+04, 4.20781245e+04, 4.07684617e+04, 3.57211274e+04, 2.84313323e+04, 2.06192576e+04, 1.43701548e+04, 9.29365299e+03, 5.66200747e+03, 3.48799443e+03, 2.07786513e+03, 1.22311562e+03, 8.76927434e+02, 6.90124583e+02, 5.59962532e+02, 4.17404173e+02, 3.39585926e+02, 3.28928162e+02, 2.73021363e+02, 2.10366431e+02, 1.68823091e+02, 1.20146768e+02, 9.04714679e+01, 7.32803029e+01, 6.33150357e+01, 5.44907683e+01, 4.87038336e+01, 4.62742438e+01, 4.29997373e+01, 4.13079210e+01, 3.78043951e+01, 3.54590704e+01, 3.13098451e+01, 2.74958795e+01, 2.95783508e+01, 2.77501849e+01, 2.60220424e+01, 2.58177979e+01, 2.09645095e+01, 2.22126591e+01, 2.02965596e+01, 1.77334874e+01, 2.00125344e+01, 1.77408714e+01, 1.91646171e+01, 1.79749177e+01, 1.58011389e+01, 1.55611976e+01, 1.48803921e+01, 1.55394006e+01], [1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 3.86806703e+00, 2.55607003e+00, 1.42436441e+00, 1.03553585e+00, 7.42218949e-01, 5.52099808e-01, 4.65083145e-01, 3.96199462e-01, 3.21218394e-01, 2.90591357e-01, 2.48450750e-01, 2.13867737e-01, 1.92925869e-01, ... 1.89489710e+02, 1.90573335e+02, 1.90445764e+02, 1.88926426e+02, 1.92258742e+02, 1.91315356e+02, 1.91111831e+02, 1.91985542e+02, 1.90372072e+02, 1.90855691e+02, 1.90699793e+02, 1.90556119e+02, 1.91322769e+02, 1.90627761e+02, 1.91407404e+02, 1.90803455e+02], [1.10000000e+00, 5.00000000e-01, 5.25256250e-01, 7.10762423e-01, 8.11577597e-01, 9.09132603e-01, 9.11750806e-01, 9.52912612e-01, 9.55881909e-01, 9.77973210e-01, 9.68450925e-01, 1.00867592e+00, 1.01427194e+00, 1.04011253e+00, 1.03023058e+00, 1.03078082e+00, 1.04587435e+00, 1.04391625e+00, 1.04339937e+00, 1.05403372e+00, 1.03500754e+00, 1.02589585e+00, 1.00814937e+00, 9.71809223e-01, 9.13350170e-01, 9.11770109e-01, 8.60156008e-01, 8.59200914e-01, 7.57984830e-01, 7.32306884e-01, 7.02538757e-01, 6.48010499e-01, 6.57740648e-01, 6.30489631e-01, 6.24047453e-01, 6.43459316e-01, 6.96848874e-01, 7.07400213e-01, 7.43780605e-01, 7.71678627e-01, 7.84963506e-01, 8.05431476e-01, 7.39396254e-01, 7.15729270e-01, 7.61222935e-01, 6.90825910e-01, 8.79080214e-01, 8.26357425e-01, 7.47781032e-01, 8.17288832e-01, 6.94703179e-01, 6.77987575e-01, 8.65290831e-01, 8.03058811e-01, 8.15027059e-01, 9.75116037e-01, 7.64394075e-01, 7.96393191e-01, 8.27180284e-01, 7.71979700e-01, 9.65798637e-01, 9.41685954e-01, 9.07700392e-01, 9.90261864e-01]])
- theory(lagtime, q)float640.001311 364.7 ... 206.3 206.3
array([[1.31088782e-03, 3.64655869e+02, 3.49299499e+02, ..., 1.92929945e+02, 1.93473002e+02, 1.92743417e+02], [2.80784354e-03, 4.60431845e+02, 4.98126848e+02, ..., 1.94747318e+02, 1.95045541e+02, 1.94419819e+02], [4.38258463e-03, 5.32617276e+02, 6.11996774e+02, ..., 1.96271689e+02, 1.96363908e+02, 1.95882577e+02], ..., [6.70880118e-01, 3.38004449e+03, 5.51938876e+03, ..., 2.06188959e+02, 2.06287796e+02, 2.06342855e+02], [7.24267866e-01, 3.50338308e+03, 5.73872448e+03, ..., 2.06188959e+02, 2.06287796e+02, 2.06342855e+02], [7.74498650e-01, 3.62346204e+03, 5.95207817e+03, ..., 2.06188959e+02, 2.06287796e+02, 2.06342855e+02]])
- isf_data(lagtime, q)float640.0 0.9997 0.9892 ... -4.952 -19.73
array([[ 0. , 0.99967882, 0.98923294, ..., -32.31431348, -9.42328539, -34.39844623], [ 0. , 0.99914543, 0.97267385, ..., -28.81782979, -8.48006746, -31.24863895], [ 0. , 0.99874574, 0.96007591, ..., -26.48287376, -7.80731178, -28.57666779], ..., [ 0. , 0.98547081, 0.45495798, ..., -10.27529479, 1.11005474, 3.76920654], [ 0. , 0.98469467, 0.48774845, ..., -12.39449068, -2.40756842, -5.51295585], [ 0. , 0.98643721, 0.42800337, ..., -5.22620786, -4.95155025, -19.7258549 ]])
- ddm_matrix_data(lagtime, q)float640.0 294.2 321.4 ... 201.1 200.4
array([[ 0. , 294.18943955, 321.41822078, ..., 192.93331805, 193.46556826, 192.76671589], [ 0. , 432.05714374, 490.9677582 , ..., 194.84772175, 195.06799833, 194.40459328], [ 0. , 535.36374381, 619.95883018, ..., 196.12616285, 196.21094092, 195.79399928], ..., [ 0. , 3966.56056507, 5791.89215548, ..., 205.00017796, 211.36062787, 212.61362392], [ 0. , 4167.1712352 , 5456.14848464, ..., 203.8398704 , 205.38454854, 207.78696548], [ 0. , 3716.77402137, 6067.88205869, ..., 207.76466695, 201.06258553, 200.39635905]])
- A(q)float64-211.2 2.585e+05 ... -1.699 -0.52
array([-2.11173656e+02, 2.58471844e+05, 1.02390610e+04, 1.15286177e+04, 1.67983893e+04, 1.91577572e+04, 1.97709014e+04, 2.04527506e+04, 1.98416438e+04, 2.14005721e+04, 2.30575400e+04, 2.75227507e+04, 3.35865707e+04, 3.95172631e+04, 4.21489620e+04, 4.26510308e+04, 4.10149466e+04, 3.58404407e+04, 2.83011541e+04, 2.06052919e+04, 1.43395780e+04, 9.15981465e+03, 5.54912690e+03, 3.34199265e+03, 1.91162170e+03, 1.15583657e+03, 7.84642482e+02, 6.25303620e+02, 4.65592900e+02, 3.32926798e+02, 2.60170996e+02, 2.37877154e+02, 1.95686602e+02, 1.45700103e+02, 1.18845397e+02, 8.74091262e+01, 7.12289354e+01, 5.50684455e+01, 4.83559858e+01, 4.51455685e+01, 3.70886933e+01, 3.69305527e+01, 3.12663737e+01, 2.69802467e+01, 2.38449170e+01, 1.94465450e+01, 1.93624826e+01, 1.39716478e+01, 1.25256899e+01, 1.23568306e+01, 9.32069947e+00, 8.23234748e+00, 6.49514954e+00, 8.15100953e+00, 6.02214129e+00, 4.28318246e+00, 4.40492236e+00, 2.64045708e+00, 2.89942014e+00, 7.90126460e-01, -9.23134186e-01, -5.47522559e-01, -1.69889697e+00, -5.19992889e-01])
- B()float64211.2
array(211.17365621)
- model :
- DDM Matrix - Single Exponential
- data_to_use :
- DDM Matrix
- initial_params_dict :
- ["{'n': 0, 'value': 100.0, 'limits': [1.0, 1000000.0], 'limited': [True, True], 'fixed': False, 'parname': 'Amplitude', 'error': 0, 'step': 0}", "{'n': 1, 'value': 1.0, 'limits': [0.001, 10.0], 'limited': [True, True], 'fixed': False, 'parname': 'Tau', 'error': 0, 'step': 0}", "{'n': 2, 'value': 25000.0, 'limits': [0.0, 10000000.0], 'limited': [True, True], 'fixed': False, 'parname': 'Background', 'error': 0, 'step': 0}", "{'n': 3, 'value': 1.0, 'limits': [0.5, 1.1], 'limited': [True, True], 'fixed': False, 'parname': 'StretchingExp', 'error': 0, 'step': 0}"]
- effective_diffusion_coeff :
- 0.616760567419975
- tau_vs_q_slope :
- [-1.9799845]
- msd_alpha :
- [1.01010892]
- msd_effective_diffusion_coeff :
- [0.61375481]
- diffusion_coeff :
- 0.6103536649149923
- diffusion_coeff_std :
- 0.04066429200992886
- velocity :
- 1.0539003757076701
- velocity_std :
- 0.4160131054430251
- good_q_range :
- [4, 16]
- DataDirectory :
- C:/Users/rmcgorty/Documents/GitHub/PyDDM/Examples/
- FileName :
- images_nobin_40x_128x128_8bit.tif
- pixel_size :
- 0.242
- frame_rate :
- 41.7
- BackgroundMethod :
- 0
- OverlapMethod :
- 2
Trying a different mathematical model¶
Above, we fit the DDM matrix to a model where the intermediate scattering function (ISF or \(f(q,\Delta t)\)) is an exponential. That is, we fit our data to:
where \(A\) is the amplitude, \(B\) is the background, \(\tau\) is the decay time, and \(s\) is the stretching exponent.
An alternative is to use other information to determine \(A(q)\) and \(B\) and then fit only the ISF. We will try this below.
[9]:
ddm_fit.reload_fit_model_by_name("ISF - Single Exponential")
Initial guess | Minimum | Maximum | |
---|---|---|---|
Tau | 1.0 | 0.001 | 10.0 |
StretchingExp | 1.0 | 0.500 | 1.1 |
[10]:
fit02 = ddm_fit.fit(name_fit = 'fit02', display_table=False)
In function 'get_tau_vs_q_fit', using new tau...
Fit is saved in fittings dictionary with key 'fit02'.
[11]:
ddm.fit_report(fit02, q_indices=[3,6,9,22], forced_qs=[4,16], use_new_tau=True, show=True)
In function 'get_tau_vs_q_fit', using new tau...
In hf.plot_one_tau_vs_q function, using new tau...
[11]:
<xarray.Dataset> Dimensions: (parameter: 2, q: 64, lagtime: 40) Coordinates: * parameter (parameter) <U13 'Tau' 'StretchingExp' * q (q) float64 0.0 0.2028 0.4057 0.6085 ... 12.37 12.58 12.78 * lagtime (lagtime) float64 0.02398 0.04796 0.07194 ... 12.59 14.36 Data variables: parameters (parameter, q) float64 0.001886 10.0 10.0 ... 1.1 1.093 theory (lagtime, q) float64 4.761e-07 0.9987 0.9812 ... 0.0 0.0 isf_data (lagtime, q) float64 0.0 0.9997 0.9892 ... -4.952 -19.73 ddm_matrix_data (lagtime, q) float64 0.0 294.2 321.4 ... 207.8 201.1 200.4 A (q) float64 -211.2 2.585e+05 1.024e+04 ... -1.699 -0.52 B float64 211.2 Attributes: (12/18) model: ISF - Single Exponential data_to_use: ISF initial_params_dict: ["{'n': 0, 'value': 1.0, 'limits': [0.001... effective_diffusion_coeff: 0.6044361168424172 tau_vs_q_slope: [-1.97401294] msd_alpha: [1.01316459] ... ... DataDirectory: C:/Users/rmcgorty/Documents/GitHub/PyDDM/... FileName: images_nobin_40x_128x128_8bit.tif pixel_size: 0.242 frame_rate: 41.7 BackgroundMethod: 0 OverlapMethod: 2
- parameter: 2
- q: 64
- lagtime: 40
- parameter(parameter)<U13'Tau' 'StretchingExp'
array(['Tau', 'StretchingExp'], dtype='<U13')
- q(q)float640.0 0.2028 0.4057 ... 12.58 12.78
array([ 0. , 0.20284 , 0.405681, 0.608521, 0.811362, 1.014202, 1.217043, 1.419883, 1.622723, 1.825564, 2.028404, 2.231245, 2.434085, 2.636926, 2.839766, 3.042607, 3.245447, 3.448287, 3.651128, 3.853968, 4.056809, 4.259649, 4.46249 , 4.66533 , 4.86817 , 5.071011, 5.273851, 5.476692, 5.679532, 5.882373, 6.085213, 6.288053, 6.490894, 6.693734, 6.896575, 7.099415, 7.302256, 7.505096, 7.707937, 7.910777, 8.113617, 8.316458, 8.519298, 8.722139, 8.924979, 9.12782 , 9.33066 , 9.5335 , 9.736341, 9.939181, 10.142022, 10.344862, 10.547703, 10.750543, 10.953383, 11.156224, 11.359064, 11.561905, 11.764745, 11.967586, 12.170426, 12.373267, 12.576107, 12.778947])
- lagtime(lagtime)float640.02398 0.04796 ... 12.59 14.36
array([ 0.023981, 0.047962, 0.071942, 0.095923, 0.119904, 0.143885, 0.167866, 0.191847, 0.215827, 0.263789, 0.28777 , 0.335731, 0.383693, 0.431655, 0.503597, 0.57554 , 0.647482, 0.743405, 0.863309, 0.983213, 1.127098, 1.294964, 1.486811, 1.702638, 1.942446, 2.206235, 2.541966, 2.901679, 3.309353, 3.788969, 4.316547, 4.940048, 5.659472, 6.450839, 7.386091, 8.441247, 9.640288, 11.007194, 12.589928, 14.364508])
- parameters(parameter, q)float640.001886 10.0 10.0 ... 1.1 1.093
array([[1.88589103e-03, 1.00000000e+01, 1.00000000e+01, 4.91794800e+00, 2.65628838e+00, 1.50327300e+00, 1.01994490e+00, 7.73490610e-01, 5.96273909e-01, 4.76451564e-01, 3.95261921e-01, 3.29426444e-01, 2.98344562e-01, 2.52353941e-01, 2.21023606e-01, 2.01537369e-01, 1.77734983e-01, 1.59432944e-01, 1.44271986e-01, 1.32113236e-01, 1.26393210e-01, 1.16714583e-01, 1.10908961e-01, 1.09684454e-01, 1.08269318e-01, 1.15043361e-01, 1.11857630e-01, 1.14807481e-01, 1.19359419e-01, 1.22539709e-01, 1.24224230e-01, 1.30311108e-01, 1.29868326e-01, 1.46928516e-01, 1.79917017e-01, 2.29897133e-01, 3.00841481e-01, 3.70318524e-01, 4.25745120e-01, 5.32776300e-01, 6.05123405e-01, 7.15176380e-01, 9.19222068e-01, 9.37440036e-01, 1.23116692e+00, 1.66636757e+00, 1.61883949e+00, 4.16763356e+00, 3.13511306e+00, 4.03316708e+00, 8.47590873e+00, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e-03, 1.19234103e-03, 1.00005677e-03, 1.16635749e-03], [1.05319182e+00, 1.10000000e+00, 6.57192482e-01, 7.19198817e-01, 8.34029429e-01, 9.18452835e-01, 9.44371329e-01, 9.69649418e-01, 9.62634582e-01, 9.98357963e-01, 9.94382337e-01, 1.01920184e+00, 1.01976533e+00, 1.04708563e+00, 1.02648698e+00, 1.02019083e+00, 1.04248931e+00, 1.04339305e+00, 1.05283303e+00, 1.05848968e+00, 1.04109533e+00, 1.04954794e+00, 1.03737037e+00, 1.03106364e+00, 1.01864683e+00, 9.71396006e-01, 9.80075298e-01, 9.51053861e-01, 9.24786563e-01, 9.55424623e-01, 9.58598542e-01, 9.07784710e-01, 9.48445041e-01, 9.63368267e-01, 9.58958089e-01, 9.99925144e-01, 1.03313591e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.10000000e+00, 1.09262247e+00, 1.09999795e+00, 1.09332614e+00]])
- theory(lagtime, q)float644.761e-07 0.9987 0.9812 ... 0.0 0.0
array([[4.76121049e-07, 9.98689112e-01, 9.81208843e-01, ..., 2.92454151e-12, 4.91078571e-15, 1.44497734e-12], [7.59506876e-14, 9.97192156e-01, 9.70527228e-01, ..., 2.52673726e-25, 2.12987589e-31, 5.46062080e-26], [7.79395343e-21, 9.95617415e-01, 9.61702126e-01, ..., 4.91870075e-39, 1.22685721e-48, 4.41016073e-40], ..., [0.00000000e+00, 3.29119882e-01, 3.44694074e-01, ..., 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], [0.00000000e+00, 2.75732134e-01, 3.12417562e-01, ..., 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], [0.00000000e+00, 2.25501350e-01, 2.81187600e-01, ..., 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
- isf_data(lagtime, q)float640.0 0.9997 0.9892 ... -4.952 -19.73
array([[ 0. , 0.99967882, 0.98923294, ..., -32.31431348, -9.42328539, -34.39844623], [ 0. , 0.99914543, 0.97267385, ..., -28.81782979, -8.48006746, -31.24863895], [ 0. , 0.99874574, 0.96007591, ..., -26.48287376, -7.80731178, -28.57666779], ..., [ 0. , 0.98547081, 0.45495798, ..., -10.27529479, 1.11005474, 3.76920654], [ 0. , 0.98469467, 0.48774845, ..., -12.39449068, -2.40756842, -5.51295585], [ 0. , 0.98643721, 0.42800337, ..., -5.22620786, -4.95155025, -19.7258549 ]])
- ddm_matrix_data(lagtime, q)float640.0 294.2 321.4 ... 201.1 200.4
array([[ 0. , 294.18943955, 321.41822078, ..., 192.93331805, 193.46556826, 192.76671589], [ 0. , 432.05714374, 490.9677582 , ..., 194.84772175, 195.06799833, 194.40459328], [ 0. , 535.36374381, 619.95883018, ..., 196.12616285, 196.21094092, 195.79399928], ..., [ 0. , 3966.56056507, 5791.89215548, ..., 205.00017796, 211.36062787, 212.61362392], [ 0. , 4167.1712352 , 5456.14848464, ..., 203.8398704 , 205.38454854, 207.78696548], [ 0. , 3716.77402137, 6067.88205869, ..., 207.76466695, 201.06258553, 200.39635905]])
- A(q)float64-211.2 2.585e+05 ... -1.699 -0.52
array([-2.11173656e+02, 2.58471844e+05, 1.02390610e+04, 1.15286177e+04, 1.67983893e+04, 1.91577572e+04, 1.97709014e+04, 2.04527506e+04, 1.98416438e+04, 2.14005721e+04, 2.30575400e+04, 2.75227507e+04, 3.35865707e+04, 3.95172631e+04, 4.21489620e+04, 4.26510308e+04, 4.10149466e+04, 3.58404407e+04, 2.83011541e+04, 2.06052919e+04, 1.43395780e+04, 9.15981465e+03, 5.54912690e+03, 3.34199265e+03, 1.91162170e+03, 1.15583657e+03, 7.84642482e+02, 6.25303620e+02, 4.65592900e+02, 3.32926798e+02, 2.60170996e+02, 2.37877154e+02, 1.95686602e+02, 1.45700103e+02, 1.18845397e+02, 8.74091262e+01, 7.12289354e+01, 5.50684455e+01, 4.83559858e+01, 4.51455685e+01, 3.70886933e+01, 3.69305527e+01, 3.12663737e+01, 2.69802467e+01, 2.38449170e+01, 1.94465450e+01, 1.93624826e+01, 1.39716478e+01, 1.25256899e+01, 1.23568306e+01, 9.32069947e+00, 8.23234748e+00, 6.49514954e+00, 8.15100953e+00, 6.02214129e+00, 4.28318246e+00, 4.40492236e+00, 2.64045708e+00, 2.89942014e+00, 7.90126460e-01, -9.23134186e-01, -5.47522559e-01, -1.69889697e+00, -5.19992889e-01])
- B()float64211.2
array(211.17365621)
- model :
- ISF - Single Exponential
- data_to_use :
- ISF
- initial_params_dict :
- ["{'n': 0, 'value': 1.0, 'limits': [0.001, 10.0], 'limited': [True, True], 'fixed': False, 'parname': 'Tau', 'error': 0, 'step': 0}", "{'n': 1, 'value': 1.0, 'limits': [0.5, 1.1], 'limited': [True, True], 'fixed': False, 'parname': 'StretchingExp', 'error': 0, 'step': 0}"]
- effective_diffusion_coeff :
- 0.6044361168424172
- tau_vs_q_slope :
- [-1.97401294]
- msd_alpha :
- [1.01316459]
- msd_effective_diffusion_coeff :
- [0.60044326]
- diffusion_coeff :
- 0.5887020998284563
- diffusion_coeff_std :
- 0.055442747427343994
- velocity :
- 1.0262600559215465
- velocity_std :
- 0.4159865239946986
- good_q_range :
- [4, 16]
- DataDirectory :
- C:/Users/rmcgorty/Documents/GitHub/PyDDM/Examples/
- FileName :
- images_nobin_40x_128x128_8bit.tif
- pixel_size :
- 0.242
- frame_rate :
- 41.7
- BackgroundMethod :
- 0
- OverlapMethod :
- 2
Interactive with matplotlib¶
Using the Browse_DDM_Fits
class as shown below, one can interactively inspect the fits (to either the DDM matrix or the ISF) by selecting the appropriate point on the \(\tau\) vs \(q\) plot or by pressing ‘N’ or ‘P’ for next or previous.
[12]:
%matplotlib notebook
fig, (ax, ax2) = plt.subplots(2, 1, figsize=(10,10/1.618))
browser = ddm.Browse_DDM_Fits(fig, ax, ax2, fit02)
fig.canvas.mpl_connect('pick_event', browser.on_pick)
fig.canvas.mpl_connect('key_press_event', browser.on_press)
ax.set_title('Decay time vs wavevector')
ax.set_xlabel("q")
ax.set_ylabel("tau (s)")
Click on a point in the tau vs q plot to see a fit.
Or press 'N' or 'P' to display next or previous fit.
[12]:
Text(0, 0.5, 'tau (s)')
[ ]:
Saving the results¶
We have a few options for saving the results of the fit. We can save the information contained in the xarray Dataset fit01
as an Excel file. Or we can save it as a netCDF file using the xarray function `to_netcdf
<https://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html>`__. If we have the fit results as a netCDF file, then we can reload it easily with teh xarray function `open_dataset
<https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html>`__.
[13]:
ddm.save_fit_results_to_excel(fit01)
[14]:
fit01.to_netcdf("fit_of_ddmmatrix.nc")