Skip to content

Commit c356a2f

Browse files
authored
Merge pull request #233 from viljarjf/simulation-progressbar
Add simple progress bar to simulations
2 parents bf20b48 + 17dfc87 commit c356a2f

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ jobs:
4040
fail-fast: false
4141
matrix:
4242
os: [ubuntu-latest, windows-latest, macos-latest]
43-
python-version: ['3.10', '3.11']
43+
python-version: ['3.10', '3.11', '3.12', '3.13']
4444
include:
4545
# Oldest supported version of main dependencies
4646
- os: ubuntu-latest
47-
python-version: 3.8
47+
python-version: 3.9
4848
OLDEST_SUPPORTED_VERSION: true
49-
DEPENDENCIES: diffpy.structure==3.0.2 matplotlib==3.5 numpy==1.17.3 orix==0.12.1 scipy==1.8 tqdm==4.9
49+
DEPENDENCIES: diffpy.structure==3.0.2 matplotlib==3.5 numpy==1.17.3 orix==0.12.1 scipy==1.8 tqdm==4.61.2
5050
LABEL: -oldest
5151
steps:
5252
- uses: actions/checkout@v4

.zenodo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
"name":"Niels Cautaerts",
3737
"orcid": "0000-0002-6402-9879"
3838
},
39+
{
40+
"name": "Viljar Johan Femoen"
41+
},
3942
{
4043
"name":"Isabel Wood"
4144
},

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Unreleased
1212

1313
Added
1414
-----
15+
- Progressbar for SimulationGenerator.calculate_diffraction2d in (#233)
1516

1617
Changed
1718
-------

diffsims/generators/simulation_generator.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"""Kinematic Diffraction Simulation Generator."""
2020

2121
from typing import Union, Sequence
22+
2223
import numpy as np
24+
from tqdm import tqdm
2325

2426
from orix.quaternion import Rotation
2527
from orix.crystal_map import Phase
@@ -139,6 +141,7 @@ def calculate_diffraction2d(
139141
max_excitation_error: float = 1e-2,
140142
shape_factor_width: float = None,
141143
debye_waller_factors: dict = None,
144+
show_progressbar: bool = False,
142145
):
143146
"""Calculates the diffraction pattern for one or more phases given a list
144147
of rotations for each phase.
@@ -166,6 +169,8 @@ def calculate_diffraction2d(
166169
control. If not set will be set equal to max_excitation_error.
167170
debye_waller_factors
168171
Maps element names to their temperature-dependent Debye-Waller factors.
172+
show_progressbar
173+
If True, display a progressbar. Defaults to False
169174
170175
Returns
171176
-------
@@ -196,7 +201,13 @@ def calculate_diffraction2d(
196201
include_zero_vector=with_direct_beam,
197202
)
198203
phase_vectors = []
199-
for rot in rotate:
204+
205+
# Progress bar setup
206+
rotate_iter = rotate
207+
if show_progressbar:
208+
rotate_iter = tqdm(rotate_iter, desc=p.name, total=rotate.size)
209+
210+
for rot in rotate_iter:
200211
# Calculate the reciprocal lattice vectors that intersect the Ewald sphere.
201212
(
202213
intersected_vectors,

diffsims/release_info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"Ben Martineau",
1515
"Niels Cautaerts",
1616
"Joonatan Laulainen",
17+
"Viljar Johan Femoen",
1718
"Isabel Wood",
1819
"Sean Collins",
1920
"Stef Smeets",

diffsims/tests/generators/test_simulation_generator.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,63 @@ def test_same_simulation_results():
341341
)
342342
old_data = np.load(FILE1)
343343
np.testing.assert_allclose(new_data, old_data, atol=1e-8)
344+
345+
346+
def test_calculate_diffraction2d_progressbar_single_phase(capsys):
347+
gen = SimulationGenerator()
348+
phase = make_phase()
349+
phase.name = "test phase"
350+
rots = Rotation.random(10)
351+
352+
# no pbar
353+
sims = gen.calculate_diffraction2d(phase, rots, show_progressbar=False)
354+
355+
# Note: tqdm outputs to stderr by default
356+
captured = capsys.readouterr()
357+
assert captured.err == ""
358+
359+
# with pbar
360+
sims = gen.calculate_diffraction2d(phase, rots, show_progressbar=True)
361+
362+
captured = capsys.readouterr()
363+
expected = "test phase: 100%|██████████| 10/10" # also some more, but that is compute-time dependent
364+
# ignore possible flushing
365+
captured = captured.err.split("\r")[-1]
366+
assert captured[: len(expected)] == expected
367+
368+
369+
def test_calculate_diffraction2d_progressbar_multi_phase(capsys):
370+
gen = SimulationGenerator()
371+
phase1 = make_phase()
372+
phase1.name = "A"
373+
phase2 = make_phase()
374+
phase2.name = "B"
375+
rots = Rotation.random(10)
376+
377+
# no pbar
378+
sims = gen.calculate_diffraction2d(
379+
[phase1, phase2], [rots, rots], show_progressbar=False
380+
)
381+
382+
# Note: tqdm outputs to stderr by default
383+
captured = capsys.readouterr()
384+
assert captured.err == ""
385+
386+
# with pbar
387+
sims = gen.calculate_diffraction2d(
388+
[phase1, phase2], [rots, rots], show_progressbar=True
389+
)
390+
391+
captured = capsys.readouterr()
392+
expected1 = "A: 100%|██████████| 10/10 "
393+
expected2 = "B: 100%|██████████| 10/10 "
394+
# Find the correct output in the stream, i.e. final line containing the name of the phase
395+
captured1 = ""
396+
captured2 = ""
397+
for line in captured.err.split("\r"):
398+
if "A" in line:
399+
captured1 = line
400+
if "B" in line:
401+
captured2 = line
402+
assert captured1[: len(expected1)] == expected1
403+
assert captured2[: len(expected2)] == expected2

0 commit comments

Comments
 (0)