Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions example/fit.py → example/fit2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import sys
from pathlib import Path

from bumps.names import FitProblem

Expand All @@ -9,11 +10,12 @@
from sasmodels.data import load_data, set_beam_stop, set_top

""" IMPORT THE DATA USED """
radial_data = load_data('DEC07267.DAT')
path = Path(__file__).resolve().parent
radial_data = load_data(str(path / 'DEC07267.DAT'))
set_beam_stop(radial_data, 0.00669, outer=0.025)
set_top(radial_data, -.0185)

tan_data = load_data('DEC07266.DAT')
tan_data = load_data(str(path / 'DEC07266.DAT'))
set_beam_stop(tan_data, 0.00669, outer=0.025)
set_top(tan_data, -.0185)
#sas.set_half(tan_data, 'right')
Expand Down Expand Up @@ -188,7 +190,15 @@
else:
problem = FitProblem(M)

M.problem = problem

if __name__ == "__main__":
import matplotlib.pyplot as plt
problem.plot()

problem.summarize()
# Plot using plotly
fig = M._plot(backend="plotly")
fig.show(renderer='browser')
# Plot using matplotlib
M._plot(backend="matplotlib")
plt.show()
File renamed without changes.
8 changes: 5 additions & 3 deletions example/multiscatfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

from bumps.names import FitProblem

from sasdata import data_path

from sasmodels.bumps_model import Experiment, Model
from sasmodels.core import load_model
from sasmodels.data import load_data
Expand All @@ -36,7 +38,7 @@
## Load the data
#data = load_data('DEC07267.DAT')
#set_beam_stop(data, 0.003, outer=0.025)
data = load_data('latex_smeared.xml', index=0)
data = load_data(str(data_path / '1d_data' / 'latex_smeared.xml'), index=0)

## Define the model
kernel = load_model("ellipsoid")
Expand Down Expand Up @@ -78,5 +80,5 @@
if __name__ == "__main__":
#M.theory()
M.plot()
import pylab
pylab.show()
import matplotlib.pyplot as plt
plt.show()
4 changes: 3 additions & 1 deletion example/oriented_usans.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import numpy as np
from bumps.names import FitProblem

from sasdata import data_path

from sasmodels.bumps_model import Experiment, Model
from sasmodels.core import load_model
from sasmodels.data import load_data

# Spherical particle data, not ellipsoids
sans, usans = load_data('latex_smeared.xml', index='all')
sans, usans = load_data(str(data_path / '1d_data' / 'latex_smeared.xml'), index='all')
usans.qmin, usans.qmax = np.min(usans.x), np.max(usans.x)
usans.mask = (usans.x < 0.0)
usans.oriented = True
Expand Down
65 changes: 37 additions & 28 deletions example/sesans_sphere_2micron.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
"""
This is a data file used to load in sesans data and fit it using the bumps engine

Usage:

bumps sesans_sphere_2micron.py
"""
import sesansfit
from bumps.names import Parameter
from bumps.names import FitProblem, Parameter

# Enter the model name to use
model_name = "sphere"
from sasdata import data_path

# DO NOT MODIFY THIS LINE
model = sesansfit.get_bumps_model(model_name)
from sasmodels.bumps_model import Experiment, Model
from sasmodels.core import load_model
from sasmodels.data import load_data

# Enter any custom parameters
# name = Parameter(initial_value, name='name')
phi = Parameter(0.0855, name='phi')
# Add the parameters to this list that should be displayed in the fitting window
custom_params = {"phi" : phi}
# Enter the model name and the datafile path
model_name = "sphere"
data_file = data_path / "sesans_data" / "sphere2micron.ses"

# SESANS data file name
sesans_file = "spheres2micron.ses"
# Custom parameters for use in expressions
# name = Parameter(initial_value, name='name')
phi = Parameter(0.0855, name='phi') # scale = phi*(1-phi)

# Initial parameter values (if other than defaults)
# "model_parameter_name" : value
initial_vals = {
# Initial parameter values and expressions (if other than defaults)
# "model_parameter_name" : value or expression
pars = {
"scale": phi*(1-phi),
"sld" : 1.41,
"radius" : 10000,
"sld_solvent" : 2.70,
}

# Ranges for parameters if other than default
# "model_parameter_name" : [min, max]
param_range = {
"phi" : [0.001, 0.5],
"radius" : [100, 100000]
}
# DO NOT MODIFY THIS LINE
model = Model(load_model(model_name), **pars)

# Constraints
# Bounds constraints
# model.param_name = f(other params)
# EXAMPLE: model.scale = model.radius*model.radius*(1 - phi) - where radius
# and scale are model functions and phi is a custom parameter
model.scale = phi*(1-phi)
model.radius.range(100, 100000)
phi.range(0.001, 0.5)


# Send to the fitting engine
# DO NOT MODIFY THIS LINE
problem = sesansfit.sesans_fit(sesans_file, model, initial_vals, custom_params, param_range)
# DO NOT MODIFY THESE LINES
data = load_data(str(data_file))
M = Experiment(data=data, model=model)
problem = FitProblem([M])

if __name__ == "__main__":
import matplotlib.pyplot as plt

print(f"==== {model_name} model for {data_file.name} has χ² = {problem.chisq_str()} ====")
print(problem.summarize())
problem.plot()
plt.show()
49 changes: 15 additions & 34 deletions example/sesansfit_CodeCampIII.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,23 @@
import numpy as np
from pathlib import Path

from bumps.names import FitProblem, Parameter

from sasmodels import bumps_model, core
from sasmodels.data import load_data

if True: # fix when data loader exists
# from sas.dataloader.readers\
from sas.dataloader.loader import Loader
loader = Loader()
filename = 'sphere.ses'
data = loader.load(filename)
if data is None:
raise OSError("Could not load file %r"%(filename,))
data.x /= 10
# print data
# data = load_sesans('mydatfile.pz')
# sans_data = load_sans('mysansfile.xml')

else:
SElength = np.linspace(0, 2400, 61) # [A]
data = np.ones_like(SElength)
err_data = np.ones_like(SElength)*0.03

class Sample:
zacceptance = 0.1 # [A^-1]
thickness = 0.2 # [cm]

class SESANSData1D:
#q_zmax = 0.23 # [A^-1]
lam = 0.2 # [nm]
x = SElength
y = data
dy = err_data
sample = Sample()
data = SESANSData1D()
path = Path(__file__).resolve().parent
data = load_data(str(path / 'sphere.ses'))

radius = 1000
data.Rmax = 3*radius # [A]

## Sphere parameters

kernel = core.load_model("sphere", dtype='single')
kernel = core.load_model("sphere")
phi = Parameter(0.1, name="phi")
model = bumps_model.Model(kernel,
scale=phi*(1-phi), sld=7.0, solvent_sld=1.0, radius=radius,
model = bumps_model.Model(
kernel,
scale=phi*(1-phi), sld=7.0, sld_solvent=1.0, radius=radius,
)
phi.range(0.001,0.5)
#model.radius.pmp(40)
Expand Down Expand Up @@ -74,3 +49,9 @@ class SESANSData1D:
else:
M_sesans = bumps_model.Experiment(data=data, model=model)
problem = FitProblem(M_sesans)


if __name__ == "__main__":
import matplotlib.pyplot as plt
problem.plot()
plt.show()
9 changes: 8 additions & 1 deletion example/simul_fit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import numpy as np
from bumps.names import FitProblem, FreeVariables

from sasdata import data_path

from sasmodels.bumps_model import Experiment, Model
from sasmodels.core import load_model
from sasmodels.data import load_data

# latex data, same sample usans and sans
# particles radius ~2300, uniform dispersity
datasets = load_data('latex_smeared.xml', index='all')
datasets = load_data(str(data_path / '1d_data' / 'latex_smeared.xml'), index='all')
#[print(data) for data in datasets]

# A single sphere model to share between the datasets. We will use
Expand Down Expand Up @@ -61,3 +63,8 @@
M = [Experiment(data=data, model=model, name=data.run[0]) for data in datasets]

problem = FitProblem(M, freevars=free)

if __name__ == "__main__":
import matplotlib.pyplot as plt
problem.plot()
plt.show()
4 changes: 2 additions & 2 deletions sasmodels/TwoYukawa/CalTYSk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
from numpy import pi, mean
from numpy import mean, pi

from .Ecoefficient import TYCoeff
from .CalcRealRoot import CalRealRoot
from .Ecoefficient import TYCoeff
from .TInvFourier import TInvFourier

# Supplied Q vector must go out to at least this value to calculate g(r).
Expand Down
1 change: 1 addition & 0 deletions sasmodels/TwoYukawa/CalcRealRoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .Ecoefficient import TYCoeff
from .Epoly import make_poly


def CalRealRoot(coeff: TYCoeff):

Ecoefficient = coeff.Ecoefficient
Expand Down
14 changes: 7 additions & 7 deletions sasmodels/TwoYukawa/Ecoefficient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Callable, Tuple

import numpy as np
from numpy import exp, pi, cos, sin, cosh
from numpy import cos, cosh, exp, pi, sin
from numpy.typing import NDArray

# CalCoeff.m
Expand Down Expand Up @@ -194,17 +194,17 @@ def ABC12(d1, d2):
Ccd2_22*Cdd1_12*d1*d2**2 -
Ccd1_21*Cdd2_22*d1*d2**2 -
Ccd2_22*d2*k1 + Ccd1_21*d1*k2)))/
((d1*((
(d1*(
(-Ccd1_21)*Ccd2_12*d2 +
Ccd1_11*Ccd2_22*d2)))))
Ccd1_11*Ccd2_22*d2)))
C2 = ((-((Ccd2_12*d2*
(((-Cd1_1)*d1 - Cdd1_11*d1**2 -
Cdd1_12*d1*d2 + k1)) -
Ccd1_11*d1*
(((-Cd2_2)*d2 - Cdd2_12*d1*d2 -
Cdd2_22*d2**2 + k2))))) /
(((-Ccd1_21)*Ccd2_12*d1*d2 +
Ccd1_11*Ccd2_22*d1*d2)))
((-Ccd1_21)*Ccd2_12*d1*d2 +
Ccd1_11*Ccd2_22*d1*d2))
return A, B, C1, C2
self.ABC12 = ABC12

Expand Down Expand Up @@ -370,10 +370,10 @@ def tau_d21(s):

E1d02 = 12*c1F01*phi*sigma_d01(z1) - 12*c1F01*exp(-z1)*phi*tau_d01(z1)

E1d11 = (((12*c1F10*phi*sigma_d01(z1) + \
E1d11 = (12*c1F10*phi*sigma_d01(z1) + \
12*c1F01*phi*sigma_d10(z1) - \
12*c1F10*exp(-z1)*phi*tau_d01(z1) - \
12*c1F01*exp(-z1)*phi*tau_d10(z1))))
12*c1F01*exp(-z1)*phi*tau_d10(z1))
E1d12 = (-c1F01 + 12*c1F11*phi*sigma_d01(z1) +
12*c1F01*phi*sigma_d11(z1) -
12*c1F11*exp(-z1)*phi*tau_d01(z1) -
Expand Down
Loading
Loading