Skip to content

Commit 943a605

Browse files
committed
Refactor: Refactor simulation examples
1 parent 28b1003 commit 943a605

File tree

6 files changed

+97
-66
lines changed

6 files changed

+97
-66
lines changed

doc/conf.py

+2
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,5 @@ def linkcode_resolve(domain, info):
175175
"ignore_pattern": "_sgskip.py", # pattern to define which will not be executed
176176
"reference_url": {"diffsims": None},
177177
}
178+
179+
autosummary_generate = True

doc/index.rst

+40-26
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,6 @@ diffsims is an open-source Python library for simulating diffraction.
1414
changelog.rst
1515
Examples <examples/index>
1616

17-
18-
Installation
19-
============
20-
21-
diffsims can be installed with `pip <https://pypi.org/project/diffsims>`__ or
22-
`conda <https://anaconda.org/conda-forge/diffsims>`__:
23-
24-
.. tab-set::
25-
26-
.. tab-item:: pip
27-
28-
.. code-block:: bash
29-
30-
pip install diffsims
31-
32-
.. tab-item:: conda
33-
34-
.. code-block:: bash
35-
36-
conda install diffsims -c conda-forge
37-
38-
Further details are available in the :doc:`installation guide <user/installation>`.
39-
40-
Learning resources
41-
==================
42-
4317
.. See: https://sphinx-design.readthedocs.io/en/furo-theme/grids.html
4418
.. grid:: 2
4519
:gutter: 2
@@ -68,6 +42,46 @@ Learning resources
6842
:octicon:`people;2em;sd-text-info` Contributing
6943
^^^
7044

45+
Guide for contributing to diffsims.
46+
47+
.. grid-item-card::
48+
:link: dev/examples
49+
:link-type: doc
50+
51+
:octicon:`zap;2em;sd-text-info` Examples
52+
^^^
53+
54+
Gallery of short examples illustrating simple tasks that can be performed with diffsims.
55+
56+
57+
58+
Installation
59+
============
60+
61+
diffsims can be installed with `pip <https://pypi.org/project/diffsims>`__ or
62+
`conda <https://anaconda.org/conda-forge/diffsims>`__:
63+
64+
.. tab-set::
65+
66+
.. tab-item:: pip
67+
68+
.. code-block:: bash
69+
70+
pip install diffsims
71+
72+
.. tab-item:: conda
73+
74+
.. code-block:: bash
75+
76+
conda install diffsims -c conda-forge
77+
78+
Further details are available in the :doc:`installation guide <user/installation>`.
79+
80+
Learning resources
81+
==================
82+
83+
84+
7185
diffsims is a community project maintained for and by its users. There are many
7286
ways you can help!
7387

doc/user/index.rst

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ use diffsims.
1111

1212
installation.rst
1313

14+
.. toctree::
15+
:caption: Usage
16+
:maxdepth: 2
17+
https://github.com/pyxem/diffsims-demos
18+
../examples/index.rst
19+
20+
installation.rst
1421
.. toctree::
1522
:caption: Resources
1623

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
.. _examples-index:
1+
Creating a Simulation Library
2+
=============================
23

3-
Simulation Library
4-
==================
5-
6-
These examples show specific workflows for creating a library of simulations
4+
These examples show specific workflows for creating a library of simulations.

examples/creating_a_simulation_library/migration_guide.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
"""
1212

1313
import numpy as np
14-
import diffpy
1514
import matplotlib.pyplot as plt
15+
from diffpy.structure import Atom, Lattice, Structure
16+
1617
from diffsims.libraries.structure_library import StructureLibrary
1718
from diffsims.generators.diffraction_generator import DiffractionGenerator
1819
from diffsims.generators.library_generator import DiffractionLibraryGenerator
1920

2021

21-
latt = diffpy.structure.lattice.Lattice(4, 4, 4, 90, 90, 90)
22+
latt = Lattice(4, 4, 4, 90, 90, 90)
2223
atoms = [
23-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.0, 0.0, 0.0], lattice=latt),
24-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.5, 0.5, 0.0], lattice=latt),
25-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.5, 0.0, 0.5], lattice=latt),
26-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.0, 0.5, 0.5], lattice=latt),
24+
Atom(atype="Al", xyz=[0.0, 0.0, 0.0], lattice=latt),
25+
Atom(atype="Al", xyz=[0.5, 0.5, 0.0], lattice=latt),
26+
Atom(atype="Al", xyz=[0.5, 0.0, 0.5], lattice=latt),
27+
Atom(atype="Al", xyz=[0.0, 0.5, 0.5], lattice=latt),
2728
]
28-
structure_matrix = diffpy.structure.Structure(atoms=atoms, lattice=latt)
29+
structure_matrix = Structure(atoms=atoms, lattice=latt)
2930
euler_angles = np.array([[0, 0, 0], [10.0, 0.0, 0.0]])
3031
struct_library = StructureLibrary(["Al"], [structure_matrix], [euler_angles])
3132
diff_gen = DiffractionGenerator(accelerating_voltage=200)
@@ -43,19 +44,18 @@
4344
# New
4445
# ---
4546

46-
import diffpy
4747
from orix.crystal_map import Phase
4848
from orix.quaternion import Rotation
4949
from diffsims.generators.simulation_generator import SimulationGenerator
5050

51-
latt = diffpy.structure.lattice.Lattice(4, 4, 4, 90, 90, 90)
51+
latt = Lattice(4, 4, 4, 90, 90, 90)
5252
atoms = [
53-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.0, 0.0, 0.0], lattice=latt),
54-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.5, 0.5, 0.0], lattice=latt),
55-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.5, 0.0, 0.5], lattice=latt),
56-
diffpy.structure.atom.Atom(atype="Al", xyz=[0.0, 0.5, 0.5], lattice=latt),
53+
Atom(atype="Al", xyz=[0.0, 0.0, 0.0], lattice=latt),
54+
Atom(atype="Al", xyz=[0.5, 0.5, 0.0], lattice=latt),
55+
Atom(atype="Al", xyz=[0.5, 0.0, 0.5], lattice=latt),
56+
Atom(atype="Al", xyz=[0.0, 0.5, 0.5], lattice=latt),
5757
]
58-
structure_matrix = diffpy.structure.Structure(atoms=atoms, lattice=latt)
58+
structure_matrix = Structure(atoms=atoms, lattice=latt)
5959
p = Phase("Al", point_group="m-3m", structure=structure_matrix)
6060
gen = SimulationGenerator(accelerating_voltage=200)
6161
rot = Rotation.from_euler([[0, 0, 0], [10.0, 0.0, 0.0]], degrees=True)
@@ -78,5 +78,7 @@
7878
axs[i, 1].set_xlim(-1.5, 1.5)
7979
axs[i, 1].set_ylim(-1.5, 1.5)
8080

81-
axs[0, 0].set_title("Old")
82-
axs[0, 1].set_title("New")
81+
_ = axs[0, 0].set_title("Old")
82+
_ = axs[0, 1].set_title("New")
83+
84+
# %%

examples/creating_a_simulation_library/simulating_diffraction_patterns.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
"""
2-
=====================================================
3-
Simulating One Diffraction Pattern for a Single Phase
4-
=====================================================
2+
==============================================
3+
Simple Diffraction Pattern Simulation Examples
4+
==============================================
5+
6+
This example demonstrates how to simulate diffraction patterns using the
7+
:class:`diffsims.generators.simulation_generator.SimulationGenerator` class. A
8+
single diffraction pattern can be simulated for a single phase or multiple
9+
diffraction patterns can be simulated for a single/multiple phases given
10+
a rotation.
11+
12+
One Pattern for One Phase
13+
--------------------------
514
"""
615

716
from orix.crystal_map import Phase
@@ -31,35 +40,35 @@
3140
) # 45 degree rotation around x-axis
3241
sim = gen.calculate_diffraction2d(phase=p, rotation=rot)
3342

34-
sim.plot(show_labels=True) # plot the first (and only) diffraction pattern
43+
_ = sim.plot(show_labels=True) # plot the first (and only) diffraction pattern
3544

3645
# %%
3746

3847
sim.coordinates # coordinates of the first (and only) diffraction pattern
3948

4049
# %%
41-
# ===========================================================
42-
# Simulating Multiple Rotations for a Single Phase
43-
# ===========================================================
50+
# Simulating Multiple Patterns for a Single Phase
51+
# -----------------------------------------------
4452

4553
rot = Rotation.from_axes_angles(
4654
[1, 0, 0], (0, 15, 30, 45, 60, 75, 90), degrees=True
4755
) # 45 degree rotation around x-axis
4856
sim = gen.calculate_diffraction2d(phase=p, rotation=rot)
4957

50-
sim.plot(show_labels=True) # plot the first diffraction pattern
58+
_ = sim.plot(show_labels=True) # plot the first diffraction pattern
5159

5260
# %%
5361

54-
sim.irot[3].plot(show_labels=True) # plot the fourth(45 degrees) diffraction pattern
62+
_ = sim.irot[3].plot(
63+
show_labels=True
64+
) # plot the fourth(45 degrees) diffraction pattern
5565
# %%
5666

5767
sim.coordinates # coordinates of all the diffraction patterns
5868

5969
# %%
60-
# ============================================================
61-
# Simulating Multiple Rotations for Multiple Phases
62-
# ============================================================
70+
# Simulating Multiple Patterns for Multiple Phases
71+
# ------------------------------------------------
6372

6473
p2 = p.deepcopy() # copy the phase
6574

@@ -70,24 +79,23 @@
7079
) # 45 degree rotation around x-axis
7180
sim = gen.calculate_diffraction2d(phase=[p, p2], rotation=[rot, rot])
7281

73-
sim.plot(
82+
_ = sim.plot(
7483
include_direct_beam=True, show_labels=True, min_label_intensity=0.1
7584
) # plot the first diffraction pattern
7685

7786
# %%
7887

79-
sim.iphase["al_2"].irot[3].plot(
80-
show_labels=True, min_label_intensity=0.1
88+
_ = (
89+
sim.iphase["al_2"].irot[3].plot(show_labels=True, min_label_intensity=0.1)
8190
) # plot the fourth(45 degrees) diffraction pattern
8291

8392
# %%
84-
# ===================================
85-
# Plotting a Real Diffraction Pattern
86-
# ===================================
93+
# Plotting a Pixelated Diffraction Pattern
94+
# ----------------------------------------
8795
dp = sim.get_diffraction_pattern(
8896
shape=(512, 512),
8997
calibration=0.01,
9098
)
9199
plt.figure()
92-
plt.imshow(dp)
100+
_ = plt.imshow(dp)
93101
# %%

0 commit comments

Comments
 (0)