-
Notifications
You must be signed in to change notification settings - Fork 0
Experimental weighting #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d66a0a0
add first example of experimental weighting
elindgren c1fd60d
scale expected chi squared by a factor of 4, since default weighting …
elindgren 58b7120
play around with joint_fit api; currently using ID of experiment as k…
elindgren 54184b3
take the square root of weights so that they enter linearly into redu…
elindgren 95828c6
automatically create the joint fit object, with 0.5 as default weight
elindgren 753a612
use a sum of weights to 1 instead of N
elindgren 521070e
update test to actually set the joint_fit object
elindgren de43310
Adds two new examples to show expected GOF in joint fit
AndrewSazonov cf13aaf
increase expected chi_squred to expected value when fitting multiple …
elindgren c000f5d
Merge remote-tracking branch 'origin/develop' into weight-experiments
elindgren aa83932
make better use of ids property
elindgren 6075581
use hasattr to check for existance of ids
elindgren 1ebc893
Updates data paths
AndrewSazonov 7962ca7
fix issue where joint_fit was not updated because of setdefault
elindgren 6702551
implement JointFitExperiments as a container for experiments and asso…
elindgren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,800 changes: 1,800 additions & 0 deletions
1,800
examples/data/pbso4_powder_neutron_cw_first-half.dat
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1,110 changes: 1,110 additions & 0 deletions
1,110
examples/data/pbso4_powder_neutron_cw_second-half.dat
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Joint Refinement Example (Advanced API) | ||
|
||
This example demonstrates a more flexible and advanced usage of the EasyDiffraction | ||
library by explicitly creating and configuring some objects. It is more suitable for | ||
users comfortable with Python programming and those interested in custom workflows. | ||
""" | ||
from numpy.testing import assert_almost_equal | ||
|
||
from easydiffraction import ( | ||
Project, | ||
SampleModel, | ||
Experiment | ||
) | ||
|
||
# Create and configure sample model | ||
|
||
model = SampleModel("pbso4") | ||
model.space_group.name.value = "P n m a" | ||
model.cell.length_a.value = 8.4693 | ||
model.cell.length_b.value = 5.3910 | ||
model.cell.length_c.value = 6.9506 | ||
model.atom_sites.add("Pb", "Pb", 0.1876, 0.25, 0.167, b_iso=1.37) | ||
model.atom_sites.add("S", "S", 0.0654, 0.25, 0.684, b_iso=0.3777) | ||
model.atom_sites.add("O1", "O", 0.9082, 0.25, 0.5954, b_iso=1.9764) | ||
model.atom_sites.add("O2", "O", 0.1935, 0.25, 0.5432, b_iso=1.4456) | ||
model.atom_sites.add("O3", "O", 0.0811, 0.0272, 0.8086, b_iso=1.2822) | ||
|
||
# Create and configure experiments | ||
|
||
# Experiment: Neutron powder diffraction (full dataset) | ||
expt = Experiment(id="npd", radiation_probe="neutron", data_path="examples/data/pbso4_powder_neutron_cw_full.dat") | ||
expt.instrument.setup_wavelength = 1.91 | ||
expt.instrument.calib_twotheta_offset = -0.1406 | ||
expt.peak.broad_gauss_u = 0.139 | ||
expt.peak.broad_gauss_v = -0.4124 | ||
expt.peak.broad_gauss_w = 0.386 | ||
expt.peak.broad_lorentz_x = 0 | ||
expt.peak.broad_lorentz_y = 0.0878 | ||
expt.linked_phases.add("pbso4", scale=1.46) | ||
expt.background_type = "line-segment" | ||
for x, y in [ | ||
(11.0, 206.1624), | ||
(15.0, 194.75), | ||
(20.0, 194.505), | ||
(30.0, 188.4375), | ||
(50.0, 207.7633), | ||
(70.0, 201.7002), | ||
(120.0, 244.4525), | ||
(153.0, 226.0595), | ||
]: | ||
expt.background.add(x, y) | ||
|
||
# Create project and add sample model and experiments | ||
project = Project() | ||
project.sample_models.add(model) | ||
project.experiments.add(expt) | ||
|
||
# Set calculator, minimizer and refinement strategy | ||
project.analysis.current_calculator = "cryspy" | ||
project.analysis.current_minimizer = "lmfit (leastsq)" | ||
project.analysis.fit_mode = 'joint' | ||
# project.analysis.joint_fit.add("expt1", weight=0.4) # Default weight could be 0.5 | ||
# project.analysis.joint_fit.add("expt2", weight=0.6) # Default weight could be 0.5 | ||
project.analysis.joint_fit.setdefault("npd1", 0.5) # Default weight could be 0.5 | ||
project.analysis.joint_fit.setdefault("npd2", 0.5) # Default weight could be 0.5 | ||
|
||
# Define free parameters | ||
model.cell.length_a.free = True | ||
model.cell.length_b.free = True | ||
model.cell.length_c.free = True | ||
|
||
# Run refinement | ||
project.analysis.fit() | ||
|
||
# Assert results | ||
assert_almost_equal(project.analysis.fit_results.reduced_chi_square, 4.66, decimal=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" | ||
Joint Refinement Example (Advanced API) | ||
|
||
This example demonstrates a more flexible and advanced usage of the EasyDiffraction | ||
library by explicitly creating and configuring some objects. It is more suitable for | ||
users comfortable with Python programming and those interested in custom workflows. | ||
""" | ||
from numpy.testing import assert_almost_equal | ||
|
||
from easydiffraction import ( | ||
Project, | ||
SampleModel, | ||
Experiment | ||
) | ||
|
||
# Create and configure sample model | ||
|
||
model = SampleModel("pbso4") | ||
model.space_group.name.value = "P n m a" | ||
model.cell.length_a.value = 8.4693 | ||
model.cell.length_b.value = 5.3910 | ||
model.cell.length_c.value = 6.9506 | ||
model.atom_sites.add("Pb", "Pb", 0.1876, 0.25, 0.167, b_iso=1.37) | ||
model.atom_sites.add("S", "S", 0.0654, 0.25, 0.684, b_iso=0.3777) | ||
model.atom_sites.add("O1", "O", 0.9082, 0.25, 0.5954, b_iso=1.9764) | ||
model.atom_sites.add("O2", "O", 0.1935, 0.25, 0.5432, b_iso=1.4456) | ||
model.atom_sites.add("O3", "O", 0.0811, 0.0272, 0.8086, b_iso=1.2822) | ||
|
||
# Create and configure experiments | ||
|
||
# Experiment 1: Neutron powder diffraction (first half of the dataset) | ||
expt1 = Experiment(id="npd1", radiation_probe="neutron", data_path="examples/data/pbso4_powder_neutron_cw_first-half.dat") | ||
expt1.instrument.setup_wavelength = 1.91 | ||
expt1.instrument.calib_twotheta_offset = -0.1406 | ||
expt1.peak.broad_gauss_u = 0.139 | ||
expt1.peak.broad_gauss_v = -0.4124 | ||
expt1.peak.broad_gauss_w = 0.386 | ||
expt1.peak.broad_lorentz_x = 0 | ||
expt1.peak.broad_lorentz_y = 0.0878 | ||
expt1.linked_phases.add("pbso4", scale=1.46) | ||
expt1.background_type = "line-segment" | ||
for x, y in [ | ||
(11.0, 206.1624), | ||
(15.0, 194.75), | ||
(20.0, 194.505), | ||
(30.0, 188.4375), | ||
(50.0, 207.7633), | ||
(70.0, 201.7002), | ||
(120.0, 244.4525), | ||
(153.0, 226.0595), | ||
]: | ||
expt1.background.add(x, y) | ||
|
||
# Experiment 2: Neutron powder diffraction (second half of the dataset) | ||
expt2 = Experiment(id="npd2", radiation_probe="neutron", data_path="examples/data/pbso4_powder_neutron_cw_second-half.dat") | ||
expt2.instrument.setup_wavelength = 1.91 | ||
expt2.instrument.calib_twotheta_offset = -0.1406 | ||
expt2.peak.broad_gauss_u = 0.139 | ||
expt2.peak.broad_gauss_v = -0.4124 | ||
expt2.peak.broad_gauss_w = 0.386 | ||
expt2.peak.broad_lorentz_x = 0 | ||
expt2.peak.broad_lorentz_y = 0.0878 | ||
expt2.linked_phases.add("pbso4", scale=1.46) | ||
expt2.background_type = "line-segment" | ||
for x, y in [ | ||
(11.0, 206.1624), | ||
(15.0, 194.75), | ||
(20.0, 194.505), | ||
(30.0, 188.4375), | ||
(50.0, 207.7633), | ||
(70.0, 201.7002), | ||
(120.0, 244.4525), | ||
(153.0, 226.0595), | ||
]: | ||
expt2.background.add(x, y) | ||
|
||
# Create project and add sample model and experiments | ||
project = Project() | ||
project.sample_models.add(model) | ||
project.experiments.add(expt1) | ||
project.experiments.add(expt2) | ||
|
||
# Set calculator, minimizer and refinement strategy | ||
project.analysis.current_calculator = "cryspy" | ||
project.analysis.current_minimizer = "lmfit (leastsq)" | ||
project.analysis.fit_mode = 'joint' | ||
# project.analysis.joint_fit.add("expt1", weight=0.4) # Default weight could be 0.5 | ||
# project.analysis.joint_fit.add("expt2", weight=0.6) # Default weight could be 0.5 | ||
print(project.analysis.joint_fit_experiments["npd1"]) | ||
project.analysis.joint_fit_experiments["npd1"] = 0.5 # Default weight could be 0.5 | ||
project.analysis.joint_fit_experiments["npd2"] = 0.5 # Default weight could be 0.5 | ||
|
||
# Define free parameters | ||
model.cell.length_a.free = True | ||
model.cell.length_b.free = True | ||
model.cell.length_c.free = True | ||
#expt1.linked_phases["pbso4"].scale.free = True | ||
#expt2.linked_phases["pbso4"].scale.free = True | ||
|
||
# Run refinement | ||
project.analysis.fit() | ||
|
||
# Assert results | ||
assert_almost_equal(project.analysis.fit_results.reduced_chi_square, 4.66, decimal=1) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
Joint Refinement Example (Advanced API) | ||
|
||
This example demonstrates a more flexible and advanced usage of the EasyDiffraction | ||
library by explicitly creating and configuring some objects. It is more suitable for | ||
users comfortable with Python programming and those interested in custom workflows. | ||
""" | ||
|
||
from easydiffraction import ( | ||
Project, | ||
SampleModel, | ||
Experiment | ||
) | ||
|
||
# Create and configure sample model | ||
|
||
model = SampleModel("pbso4") | ||
model.space_group.name.value = "P n m a" | ||
model.cell.length_a.value = 8.5 | ||
model.cell.length_b.value = 5.35 | ||
model.cell.length_c.value = 6.9 | ||
model.atom_sites.add("Pb", "Pb", 0.1876, 0.25, 0.167, b_iso=1.37) | ||
model.atom_sites.add("S", "S", 0.0654, 0.25, 0.684, b_iso=0.3777) | ||
model.atom_sites.add("O1", "O", 0.9082, 0.25, 0.5954, b_iso=1.9764) | ||
model.atom_sites.add("O2", "O", 0.1935, 0.25, 0.5432, b_iso=1.4456) | ||
model.atom_sites.add("O3", "O", 0.0811, 0.0272, 0.8086, b_iso=1.2822) | ||
|
||
# Create and configure experiments | ||
|
||
# Experiment 1: Neutron powder diffraction | ||
expt1 = Experiment(id="npd", radiation_probe="neutron", data_path="examples/data/d1a_pbso4.dat") | ||
expt1.instrument.setup_wavelength = 1.91 | ||
expt1.instrument.calib_twotheta_offset = -0.1406 | ||
expt1.peak.broad_gauss_u = 0.139 | ||
expt1.peak.broad_gauss_v = -0.412 | ||
expt1.peak.broad_gauss_w = 0.386 | ||
expt1.peak.broad_lorentz_x = 0 | ||
expt1.peak.broad_lorentz_y = 0.088 | ||
expt1.linked_phases.add("pbso4", scale=1.0) | ||
expt1.background_type = "line-segment" | ||
for x, y in [ | ||
(11.0, 206.1624), | ||
(15.0, 194.75), | ||
(20.0, 194.505), | ||
(30.0, 188.4375), | ||
(50.0, 207.7633), | ||
(70.0, 201.7002), | ||
(120.0, 244.4525), | ||
(153.0, 226.0595), | ||
]: | ||
expt1.background.add(x, y) | ||
|
||
# Experiment 2: X-ray powder diffraction | ||
expt2 = Experiment(id="xrd", radiation_probe="xray", data_path="examples/data/lab_pbso4.dat") | ||
expt2.instrument.setup_wavelength = 1.540567 | ||
expt2.instrument.calib_twotheta_offset = -0.05181 | ||
expt2.peak.broad_gauss_u = 0.304138 | ||
expt2.peak.broad_gauss_v = -0.112622 | ||
expt2.peak.broad_gauss_w = 0.021272 | ||
expt2.peak.broad_lorentz_x = 0 | ||
expt2.peak.broad_lorentz_y = 0.057691 | ||
expt2.linked_phases.add("pbso4", scale=0.005) | ||
expt2.background_type = "chebyshev polynomial" | ||
for x, y in [ | ||
(0, 119.195), | ||
(1, 6.221), | ||
(2, -45.725), | ||
(3, 8.119), | ||
(4, 54.552), | ||
(5, -20.661), | ||
]: | ||
expt2.background.add(x, y) | ||
|
||
# Create project and add sample model and experiments | ||
project = Project() | ||
project.sample_models.add(model) | ||
project.experiments.add(expt1) | ||
project.experiments.add(expt2) | ||
|
||
# Set calculator, minimizer and refinement strategy | ||
project.analysis.current_calculator = "cryspy" | ||
project.analysis.current_minimizer = "lmfit (leastsq)" | ||
project.analysis.fit_mode = 'joint' | ||
# project.analysis.joint_fit.add("expt1", weight=0.4) # Default weight could be 0.5 | ||
# project.analysis.joint_fit.add("expt2", weight=0.6) # Default weight could be 0.5 | ||
project.analysis.joint_fit.setdefault("xrd",0.4) # Default weight could be 0.5 | ||
project.analysis.joint_fit.setdefault("npd",0.6) # Default weight could be 0.5 | ||
|
||
# Define free parameters | ||
model.cell.length_a.free = True | ||
model.cell.length_b.free = True | ||
model.cell.length_c.free = True | ||
expt1.linked_phases["pbso4"].scale.free = True | ||
expt2.linked_phases["pbso4"].scale.free = True | ||
|
||
# Run refinement | ||
project.analysis.fit() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from easydiffraction.core.collection import Collection | ||
from easydiffraction.core.parameter import Descriptor | ||
|
||
class JointFitExperiments(Collection): | ||
""" | ||
Collection manager for experiments that are fitted together | ||
in a `joint` fit. | ||
""" | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def add(self, id: str, weight: float): | ||
"""Add an experiment with it's associated weight""" | ||
# Save both id and weight as immutable Descriptors | ||
self.id = Descriptor( | ||
value=id, | ||
cif_name="id" | ||
) | ||
self.weight = Descriptor( | ||
value=weight, | ||
cif_name="weight" | ||
) | ||
self._items[id] = weight | ||
|
||
def __getitem__(self, key): | ||
return self._items[key] | ||
|
||
def __setitem__(self, key, value): | ||
self._items[key] = value |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the line above should also reuse the new
experiments.ids
propertyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks!