Skip to content

Commit aefa19a

Browse files
AlexboiboiBoisselet Alexandre (IFAT DC ATV SC D TE2)OrtnerMichael
authoredMar 7, 2024
Replace assert np.allclose by np.testing.assert_allclose (magpylib#734)
* refactor * refactor * rework check input cyl seg * add in_out for cylinder * signular to plural * current loop core * field core polyline fix * minifix docstring * finish plural to singular * add field MJ to currents * fix internal cylseg field func * fixed dipole * add in_out and getM and getJ to cylinder segment * pre-commit * add getM getJ to cylinder core * core tests und 2 bugfixes * add getM and getJ to trimesh * add getM getJ to dipole * avoid calculating field for M or J for cyl seg * avoid calculating field for getM getJ cylinder * avoid calc field for getM getJ sphere * refactor * long solenoid test * comments * core physics tests * complete core testing * fix core examples * fix Cuboid units * fix Cylinder * fix CylinderSegment * fix Sphere * fix magnetization docstring * fix Tetrahedron * fix TriangularMesh * fix Collection * fix Circle * fix Polyline * fix CustomSource * fix Dipole * fix Triangle * fix Sensor * Fix wrapBH * Fix doctests * fix loop and line deprecation * pylint on magpylib folder * fix test compound setters * add pol and mag initialization(needed for repr) * fix mpl tests * fix _default_style_description units * fix display plotly tests * fix display pyvista test * fix exceptins tests * allow pol and mag to be np arrays * fix field cylinder tests * avoid low mag warnings on display tests * fix getBH tests (up to dipole) * fix input checks tests * fix numerical stability * fix base geo tests * fix test paths * fix utility tests * fix test vs mag2 * fix scaling test * fix physics consistency tests * fix Collection tests (up to Dipole fail) * fix Cuboid tests * fix Cylinder tests * fix CylinderSegment tests * fix TriangularMesh tests * fix Triangle tests * fix Tetrahedron tests * fix Sphere tests * fix Sensor tests * fix Polyline tests * pylint * fix Circle tests * fix Dipole and Dipole tests * fix missing modules tests * adapt bugreport * more positional avoided in tests * fix class_BaseExcitations docstrings * fix field_BH_polyline * fix field input check robustness * describe "degrees" -> "deg" * bugfix: added moment unit to describe * docstring missing SI * docstring fix SI * fix cylinder segment * Collection docstrings fixes SI * SI fix circle class * docstrings to SI * field wrap SI units fix * test bugfix include now fix magpylib#279 (cylinder bug) * update * update Readme * rework readme * fix test * fix test * replace units to SI in docstrings (code only) * fix units in get started * replace dot separator (mpl font compat) * fix units docu graphics * rename direct interface to functional interface * reorder docstring parameters * reorder param customsource * fix units in docu magpylib api * Fix end of shaft * fix compound example * add virutal documents (from myst & jupytext) * fix field interpolation * fix shapes convex hull * fix shapes pyvista * bump to v5 dev version * bad columns v5 vs v4 * fix shapes superpos * fix shapes triangle * fix tutorial collection * fix tutorial field computation * fix tutorial path * better unit exponent display * fix tutorial custom * rework tutorial field computation * fix tutorial trimesh * fix vis mpl streamplot * fix vis pv streamlines * unpin pyvista version * fix units * pylint * pylint * add in_out to toplevel + docstrings updates * fix in_out not passed on getB * pass in_out only for magnets * add getM and getJ to all top levels * fix imports for getB and getM * add self consistency getHBMJ test * fix tests * pre-commit * pylint * avoid edge case warning * pylint * fix Sensor default description and allow pixel=None * rework base disp repr tests * fix test * remove news * update docs * fix codeql * pylint * fix test * upgrade sphinx * add missing changelog dates * remove obsolete ipygany and kaleido * fix tests * pylint * fix pyvista + tests * fix pyvista animation test * code ql * fix field_func input_checks tests * add input_check sensor handedness * BaseExcitation coverage * fix different_pixel_shapes test * fix legend style and description handling + tests * pylint * fix pyvista animation tests on CI * empty commit * offscreen pyvista animation test * avoid test syntax collision with pytest + doctests * fix missing pyvista test pollution * override parent order * override_parent * some docu fixes * new image * cast right types * add HBMJ consistency tests * pylint * pylint * update changelog * minifix * core getM getJ tests * intermediate commit * add M and J to show2D * fix subplots test * adding mu_0 to top level using scipy mu0 * cuboid and cylinder core fixes * BHJM dipole * BHJM circle * BHJM cylinder segment * some fixes * qucistage * minifix: propagate separator kw in magic_to_dict * allow tests to run * cylinder fix * sphere BHJM * BHJM sphere * sphere field bugfix * fix doctests for obj classes * sphere 2/3 fix * cylindr segement BHJM fixed * fix field wrap getB getH pi redef * BHJM tetrahedron * BHJM triangle * remove type hint * BHJM triangular mesh * BHJM current polyline * minifix * remove convert_HBJM utility * review BHJM - fix MU0 * linting * linting and docstring minifix * coverage * pylint * assert np.allclose -> np.testing.assert_allclose * assert np.all -> np.testing.assert_array_equal * pylint fix * add in_out to tetra * *=0 -> =0 * BHJM docstrings * warning in_out * pylint coverage fix in_out * linter --------- Co-authored-by: Boisselet Alexandre (IFAT DC ATV SC D TE2) <[email protected]> Co-authored-by: mortner <[email protected]>
1 parent 4301354 commit aefa19a

23 files changed

+352
-222
lines changed
 

‎docs/_pages/docu/docu_magpylib_api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ print(B)
649649
# --> [0. 0. 0.00125664]
650650
```
651651

652-
By default, `getB` returns the B-field in units of T, `getH` the H-field in units of A/m, `getJ` the magnetic polarization in T and, `getM` the magnetization in A/m, assuming that all inputs are given in SI units.
652+
By default, `getB` returns the B-field in units of T, `getH` the H-field in units of A/m, `getJ` the magnetic polarization in T and, `getM` the magnetization in A/m, assuming that all inputs are given in SI units.
653653

654654
```{note}
655655
In reality, `getB` returns the same unit as given by the `polarization` input. For example, with polarization input in mT, getB will return mT as well. At the same time when the `magnetization` input is kA/m, then `getH` returns kA/m as well. The B/H-field outputs are related to a M/J-inputs via a factor of $µ_0$.

‎tests/test_BaseTransform.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_apply_move(description, old_position, displacement, new_position, start
8585
print(description)
8686
s = magpy.Sensor(position=old_position)
8787
apply_move(s, displacement, start=start)
88-
assert np.all(s.position == np.array(new_position))
88+
np.testing.assert_array_equal(s.position, np.array(new_position))
8989

9090

9191
@pytest.mark.parametrize(
@@ -255,7 +255,13 @@ def test_apply_rotation(
255255
position=old_position, orientation=R.from_rotvec(old_orientation_rotvec)
256256
)
257257
apply_rotation(s, R.from_rotvec(rotvec_to_apply), start=start, anchor=anchor)
258-
assert np.allclose(s.position, np.array(new_position))
259-
assert np.allclose(
260-
s.orientation.as_matrix(), R.from_rotvec(new_orientation_rotvec).as_matrix()
258+
259+
np.testing.assert_allclose(
260+
s.position, np.array(new_position), rtol=1e-05, atol=1e-08
261+
)
262+
np.testing.assert_allclose(
263+
s.orientation.as_matrix(),
264+
R.from_rotvec(new_orientation_rotvec).as_matrix(),
265+
rtol=1e-05,
266+
atol=1e-08,
261267
)

‎tests/test_elliptics.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def test_el3_vs_original():
5656

5757
# compare to vectorized
5858
resv = el3v(x11, kc11, p11)
59-
assert np.allclose(res0, resv)
59+
np.testing.assert_allclose(res0, resv)
6060

6161
# compare to modified original
6262
res1 = np.array([el30(x, kc, p) for x, kc, p in zip(x11, kc11, p11)])
63-
assert np.allclose(res0, res1)
63+
np.testing.assert_allclose(res0, res1)
6464

6565

6666
def test_el3_angle_vs_original():
@@ -81,7 +81,7 @@ def test_el3_angle_vs_original():
8181

8282
# compare to vectorized
8383
resv = el3_angle(phis, ns, ms)
84-
assert np.allclose(res0, resv)
84+
np.testing.assert_allclose(res0, resv)
8585

8686

8787
def test_el3s():
@@ -97,8 +97,8 @@ def test_el3s():
9797
res1 = el3v(xs, kcs, ps)
9898
res2 = el3(xs, kcs, ps)
9999

100-
assert np.allclose(res0, res1)
101-
assert np.allclose(res1, res2)
100+
np.testing.assert_allclose(res0, res1)
101+
np.testing.assert_allclose(res1, res2)
102102

103103

104104
def test_cels():
@@ -116,5 +116,5 @@ def test_cels():
116116
res1 = celv(kcc, pp, cc, ss)
117117
res2 = cel(kcc, pp, cc, ss)
118118

119-
assert np.allclose(res0, res1)
120-
assert np.allclose(res1, res2)
119+
np.testing.assert_allclose(res0, res1)
120+
np.testing.assert_allclose(res1, res2)

‎tests/test_field_cylinder.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def test_cylinder_field1():
224224
field="B", observers=poso, polarization=magg, dimension=dim5
225225
)
226226

227-
assert np.allclose(B1, B0)
227+
np.testing.assert_allclose(B1, B0)
228228

229229

230230
def test_cylinder_slanovc_field2():
@@ -244,37 +244,37 @@ def test_cylinder_slanovc_field2():
244244
# only inside
245245
btest = np.array([b_in] * 3)
246246
B = src.getB([r_in] * 3)
247-
assert np.allclose(B, btest)
247+
np.testing.assert_allclose(B, btest)
248248

249249
# only edge
250250
btest = np.array([b_corn] * 3)
251251
B = src.getB([r_corn] * 3)
252-
assert np.allclose(B, btest)
252+
np.testing.assert_allclose(B, btest)
253253

254254
# only outside
255255
btest = np.array([b_out] * 3)
256256
B = src.getB([r_out] * 3)
257-
assert np.allclose(B, btest)
257+
np.testing.assert_allclose(B, btest, rtol=1e-05, atol=1e-08)
258258

259259
# edge + out
260260
btest = np.array([b_corn, b_corn, b_out])
261261
B = src.getB([r_corn, r_corn, r_out])
262-
assert np.allclose(B, btest)
262+
np.testing.assert_allclose(B, btest, rtol=1e-05, atol=1e-08)
263263

264264
# surf + in
265265
btest = np.array([b_corn, b_corn, b_in])
266266
B = src.getB(r_corn, r_corn, r_in)
267-
assert np.allclose(B, btest)
267+
np.testing.assert_allclose(B, btest)
268268

269269
# in + out
270270
btest = np.array([b_out, b_in])
271271
B = src.getB(r_out, r_in)
272-
assert np.allclose(B, btest)
272+
np.testing.assert_allclose(B, btest, rtol=1e-05, atol=1e-08)
273273

274274
# in + out + surf
275275
btest = np.array([b_corn, b_corn, b_in, b_out, b_corn, b_corn])
276276
B = src.getB([r_corn, r_corn, r_in, r_out, r_corn, r_corn])
277-
assert np.allclose(B, btest)
277+
np.testing.assert_allclose(B, btest, rtol=1e-05, atol=1e-08)
278278

279279

280280
def test_cylinder_slanovc_field3():
@@ -290,32 +290,32 @@ def test_cylinder_slanovc_field3():
290290
# only inside
291291
htest = np.array([hinn] * 3)
292292
H = src.getH([[0.5, 0.6, 0.3]] * 3)
293-
assert np.allclose(H, htest)
293+
np.testing.assert_allclose(H, htest)
294294

295295
# only surf
296296
htest = np.array([nulll] * 3)
297297
H = src.getH([[1, 0, 0]] * 3)
298-
assert np.allclose(H, htest)
298+
np.testing.assert_allclose(H, htest)
299299

300300
# only outside
301301
htest = np.array([hout] * 3)
302302
H = src.getH([[1, 2, 3]] * 3)
303-
assert np.allclose(H, htest)
303+
np.testing.assert_allclose(H, htest)
304304

305305
# surf + out
306306
htest = np.array([nulll, nulll, hout])
307307
H = src.getH([0.6, 0, 1], [1, 0, 0.5], [1, 2, 3])
308-
assert np.allclose(H, htest)
308+
np.testing.assert_allclose(H, htest)
309309

310310
# surf + in
311311
htest = np.array([nulll, nulll, hinn])
312312
H = src.getH([0, 0.5, 1], [1, 0, 0.5], [0.5, 0.6, 0.3])
313-
assert np.allclose(H, htest)
313+
np.testing.assert_allclose(H, htest)
314314

315315
# in + out
316316
htest = np.array([hout, hinn])
317317
H = src.getH([1, 2, 3], [0.5, 0.6, 0.3])
318-
assert np.allclose(H, htest)
318+
np.testing.assert_allclose(H, htest)
319319

320320
# in + out + surf
321321
htest = np.array([nulll, nulll, hinn, hout, nulll, nulll])
@@ -327,7 +327,7 @@ def test_cylinder_slanovc_field3():
327327
[0.5, 0.6, -1],
328328
[0, 1, -0.3],
329329
)
330-
assert np.allclose(H, htest)
330+
np.testing.assert_allclose(H, htest)
331331

332332

333333
def test_cylinder_rauber_field4():
@@ -353,7 +353,7 @@ def test_cylinder_tile_negative_phi():
353353
)
354354
B1 = src1.getB((1, 0.5, 0.1))
355355
B2 = src2.getB((1, 0.5, 0.1))
356-
assert np.allclose(B1, B2)
356+
np.testing.assert_allclose(B1, B2)
357357

358358

359359
def test_cylinder_tile_vs_fem():

‎tests/test_getBH_dict.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_getB_dict1():
2525
dic = {"polarization": mag, "dimension": dim, "position": pos, "orientation": rot}
2626
B1 = magpy.getB("Cylinder", pos_obs, **dic)
2727

28-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
28+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
2929

3030

3131
def test_getB_dict2():
@@ -41,7 +41,7 @@ def test_getB_dict2():
4141
pm = magpy.magnet.Cylinder(polarization=mag, dimension=dim, position=pos)
4242
B2 = magpy.getB([pm], pos_obs)
4343

44-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
44+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
4545

4646

4747
def test_getH_dict1():
@@ -56,7 +56,7 @@ def test_getH_dict1():
5656
pm = magpy.magnet.Cylinder(polarization=mag, dimension=dim)
5757
B2 = pm.getH(pos_obs)
5858

59-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
59+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
6060

6161

6262
def test_getB_dict3():
@@ -81,7 +81,7 @@ def test_getB_dict3():
8181
B2 += [pm.getB(pos_obs)]
8282
B2 = np.array(B2)
8383
print(B1 - B2)
84-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
84+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
8585

8686

8787
def test_getH_dict3():
@@ -99,7 +99,7 @@ def test_getH_dict3():
9999
B2 += [magpy.getH([pm], pos_obs)]
100100
B2 = np.array(B2)
101101

102-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
102+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
103103

104104

105105
def test_getB_dict4():
@@ -124,33 +124,33 @@ def test_getB_dict4():
124124
B2 += [pm.getB(pos_obs)]
125125
B2 = np.array(B2)
126126
print(B1 - B2)
127-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
127+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
128128

129129

130130
def test_getBH_dipole():
131131
"""test if Dipole implementation gives correct output"""
132132
B = magpy.getB("Dipole", (1, 1, 1), moment=(1, 2, 3))
133133
Btest = np.array([9.62250449e-08, 7.69800359e-08, 5.77350269e-08])
134-
assert np.allclose(B, Btest)
134+
np.testing.assert_allclose(B, Btest)
135135

136136
H = magpy.getH("Dipole", (1, 1, 1), moment=(1, 2, 3))
137137
Htest = np.array([0.07657346, 0.06125877, 0.04594407])
138-
assert np.allclose(H, Htest)
138+
np.testing.assert_allclose(H, Htest, rtol=1e-05, atol=1e-08)
139139

140140

141141
def test_getBH_circle():
142142
"""test if Circle implementation gives correct output"""
143143
B = magpy.getB("Circle", (0, 0, 0), current=1, diameter=2)
144144
Btest = np.array([0, 0, 0.6283185307179586 * 1e-6])
145-
assert np.allclose(B, Btest)
145+
np.testing.assert_allclose(B, Btest)
146146

147147
H = magpy.getH("Circle", (0, 0, 0), current=1, diameter=2)
148148
Htest = np.array([0, 0, 0.6283185307179586 * 10 / 4 / np.pi])
149-
assert np.allclose(H, Htest)
149+
np.testing.assert_allclose(H, Htest)
150150

151151
with pytest.warns(MagpylibDeprecationWarning):
152152
B = magpy.getB("Loop", (0, 0, 0), current=1, diameter=2)
153-
assert np.allclose(B, Btest)
153+
np.testing.assert_allclose(B, Btest)
154154

155155

156156
def test_getBH_squeeze():
@@ -181,7 +181,7 @@ def test_getBH_polyline():
181181
/ 4
182182
/ np.pi
183183
)
184-
assert np.allclose(x, H)
184+
np.testing.assert_allclose(x, H, rtol=1e-05, atol=1e-08)
185185

186186

187187
def test_getBH_polyline2():
@@ -200,11 +200,11 @@ def getB_line(name):
200200

201201
B1 = getB_line("Polyline")
202202
expected = np.array([0, -x, 0])
203-
assert np.allclose(B1, expected)
203+
np.testing.assert_allclose(B1, expected, rtol=1e-05, atol=1e-08)
204204

205205
with pytest.warns(MagpylibDeprecationWarning):
206206
B1 = getB_line("Line")
207-
assert np.allclose(B1, expected)
207+
np.testing.assert_allclose(B1, expected, rtol=1e-05, atol=1e-08)
208208

209209
# move z-line to x=-1
210210
B2 = magpy.getB(
@@ -215,7 +215,7 @@ def getB_line(name):
215215
segment_start=(1, 0, -1),
216216
segment_end=(1, 0, 1),
217217
)
218-
assert np.allclose(B2, np.array([0, x, 0]))
218+
np.testing.assert_allclose(B2, np.array([0, x, 0]), rtol=1e-05, atol=1e-08)
219219

220220
# rotate 1
221221
rot = R.from_euler("z", 90, degrees=True)
@@ -227,7 +227,7 @@ def getB_line(name):
227227
segment_start=(1, 0, -1),
228228
segment_end=(1, 0, 1),
229229
)
230-
assert np.allclose(B3, np.array([x, 0, 0]))
230+
np.testing.assert_allclose(B3, np.array([x, 0, 0]), rtol=1e-05, atol=1e-08)
231231

232232
# rotate 2
233233
rot = R.from_euler("x", 90, degrees=True)
@@ -239,7 +239,7 @@ def getB_line(name):
239239
segment_start=(1, 0, -1),
240240
segment_end=(1, 0, 1),
241241
)
242-
assert np.allclose(B4, np.array([0, 0, -x]))
242+
np.testing.assert_allclose(B4, np.array([0, 0, -x]), rtol=1e-05, atol=1e-08)
243243

244244
# rotate 3
245245
rot = R.from_euler("y", 90, degrees=True)
@@ -251,7 +251,7 @@ def getB_line(name):
251251
segment_start=(1, 0, -1),
252252
segment_end=(1, 0, 1),
253253
)
254-
assert np.allclose(B5, np.array([0, -x, 0]))
254+
np.testing.assert_allclose(B5, np.array([0, -x, 0]), rtol=1e-05, atol=1e-08)
255255

256256
# "scalar" vertices tiling
257257
B = magpy.getB(
@@ -374,8 +374,8 @@ def test_getBH_solid_cylinder():
374374
polarization=(22, 33, 44),
375375
)
376376

377-
assert np.allclose(B1, B2)
378-
assert np.allclose(B1, B3)
377+
np.testing.assert_allclose(B1, B2)
378+
np.testing.assert_allclose(B1, B3)
379379

380380

381381
def test_getB_dict_over_getB():
@@ -403,7 +403,7 @@ def test_getB_dict_over_getB():
403403
}
404404
B1 = magpy.getB(**dic)
405405

406-
assert np.allclose(B1, B2, rtol=1e-12, atol=1e-12)
406+
np.testing.assert_allclose(B1, B2, rtol=1e-12, atol=1e-12)
407407

408408
# test for kwargs if sources is not a string
409409
dic["sources"] = pm

‎tests/test_getBH_interfaces.py

+21
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,24 @@ def test_getB_on_missing_excitations(module, class_, missing_arg, kwargs):
361361
match=rf"Parameter `{missing_arg}` of .* must be set.",
362362
):
363363
getattr(getattr(magpy, module), class_)(**kwargs).getB([0, 0, 0])
364+
365+
366+
@pytest.mark.parametrize("field", ("H", "B", "M", "J"))
367+
@pytest.mark.parametrize("in_out", ("auto", "inside", "outside"))
368+
def test_getHBMJ_self_consistency(field, in_out):
369+
"""test getHBMJ self consistency"""
370+
sources = [
371+
magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1)),
372+
magpy.current.Circle(diameter=1, current=1),
373+
]
374+
sens = magpy.Sensor(position=np.linspace((-1, 0, 0), (1, 0, 0), 10))
375+
src = sources[0]
376+
377+
F1 = getattr(magpy, f"get{field}")(src, sens, in_out=in_out)
378+
F2 = getattr(sens, f"get{field}")(src, in_out=in_out)
379+
F3 = getattr(src, f"get{field}")(sens, in_out=in_out)
380+
F4 = getattr(magpy.Collection(src, sens), f"get{field}")(in_out=in_out)
381+
382+
np.testing.assert_allclose(F1, F2)
383+
np.testing.assert_allclose(F1, F3)
384+
np.testing.assert_allclose(F1, F4)

0 commit comments

Comments
 (0)
Please sign in to comment.