Skip to content

Commit 6eda489

Browse files
committed
Bugfix: Small Bugfix for polar flatten simulation
1 parent c356a2f commit 6eda489

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

diffsims/simulations/simulation2d.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,27 @@ def polar_flatten_simulations(self, radial_axes=None, azimuthal_axes=None):
327327
intensities_templates = np.zeros((len(flattened_vectors), max_num_spots))
328328
for i, v in enumerate(flattened_vectors):
329329
r, t = v.to_flat_polar()
330+
inten = v.intensity
330331
if radial_axes is not None and azimuthal_axes is not None:
331-
r = get_closest(radial_axes, r)
332-
t = get_closest(azimuthal_axes, t)
333-
r = r[r < len(radial_axes)]
334-
t = t[t < len(azimuthal_axes)]
335-
r_templates[i, : len(r)] = r
336-
theta_templates[i, : len(t)] = t
337-
intensities_templates[i, : len(v.intensity)] = v.intensity
332+
r = get_closest(
333+
radial_axes, r
334+
) # convert from real to pixel coordinates
335+
t = get_closest(
336+
azimuthal_axes, t
337+
) # convert from real to pixel coordinates
338+
mask = r < len(radial_axes) - 1 # if out of bounds, ignore
339+
r = r[mask] # if out of bounds, ignore
340+
t = t[mask] # if out of bounds, ignore
341+
inten = inten[mask] # if out of bounds, ignore
342+
mask = t < len(azimuthal_axes) - 1 # if out of bounds, ignore
343+
t = t[mask] # if out of bounds, ignore
344+
r = r[mask] # if out of bounds, ignore
345+
inten = inten[mask] # if out of bounds, ignore
346+
r_templates[i, : len(r)] = (
347+
r # set the r coordinates (len r and t should be the same)
348+
)
349+
theta_templates[i, : len(r)] = t # set the theta coordinates
350+
intensities_templates[i, : len(inten)] = inten
338351
if radial_axes is not None and azimuthal_axes is not None:
339352
r_templates = np.array(r_templates, dtype=int)
340353
theta_templates = np.array(theta_templates, dtype=int)

diffsims/tests/simulations/test_simulations2d.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ class TestSingleSimulation:
4646
def single_simulation(self, al_phase):
4747
gen = SimulationGenerator(accelerating_voltage=200)
4848
rot = Rotation.from_axes_angles([1, 0, 0], 45, degrees=True)
49-
coords = DiffractingVector(phase=al_phase, xyz=[[1, 0, 0]])
49+
coords = DiffractingVector(
50+
phase=al_phase,
51+
xyz=[
52+
[1, 0, 0],
53+
[2, 0, 0],
54+
[3, 3, 0],
55+
[-4, 0, 0],
56+
[-5, 0, 0],
57+
[-6, 0, 0],
58+
[-7, 0, 0],
59+
[-8, 0, 0],
60+
],
61+
intensity=[1, 2, 3, 4, 5, 6, 7, 8],
62+
)
5063
sim = Simulation2D(
5164
phases=al_phase, simulation_generator=gen, coordinates=coords, rotations=rot
5265
)
@@ -91,12 +104,12 @@ def test_polar_flatten(self, single_simulation):
91104
theta_templates,
92105
intensities_templates,
93106
) = single_simulation.polar_flatten_simulations()
94-
assert r_templates.shape == (1, 1)
95-
assert theta_templates.shape == (1, 1)
96-
assert intensities_templates.shape == (1, 1)
107+
assert r_templates.shape == (1, 8)
108+
assert theta_templates.shape == (1, 8)
109+
assert intensities_templates.shape == (1, 8)
97110

98111
def test_polar_flatten_axes(self, single_simulation):
99-
radial_axes = np.linspace(0, 1, 10)
112+
radial_axes = np.linspace(0, 7, 5)
100113
theta_axes = np.linspace(0, 2 * np.pi, 10)
101114
(
102115
r_templates,
@@ -105,9 +118,13 @@ def test_polar_flatten_axes(self, single_simulation):
105118
) = single_simulation.polar_flatten_simulations(
106119
radial_axes=radial_axes, azimuthal_axes=theta_axes
107120
)
108-
assert r_templates.shape == (1, 1)
109-
assert theta_templates.shape == (1, 1)
110-
assert intensities_templates.shape == (1, 1)
121+
assert r_templates.shape == (1, 8)
122+
assert theta_templates.shape == (1, 8)
123+
assert intensities_templates.shape == (1, 8)
124+
# The last 2 elements should be zero
125+
np.testing.assert_array_equal(r_templates[:, 6:], 0)
126+
np.testing.assert_array_equal(theta_templates[:, 6:], 0)
127+
np.testing.assert_array_equal(intensities_templates[:, 6:], 0)
111128

112129
def test_deepcopy(self, single_simulation):
113130
copied = single_simulation.deepcopy()

0 commit comments

Comments
 (0)