-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_obj_Collection.py
429 lines (363 loc) · 15.1 KB
/
test_obj_Collection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import pickle
import re
import numpy as np
import pytest
from scipy.spatial.transform import Rotation as R
import magpylib as magpy
from magpylib._src.exceptions import MagpylibBadUserInput
# # # GENERATE TESTDATA
# # N = 5
# # mags = (np.random.rand(N,6,3)-0.5)*1000
# # dims3 = np.random.rand(N,3,3)*5 # 5x cuboid
# # dims2 = np.random.rand(N,3,2)*5 # 5x cylinder
# # posos = (np.random.rand(N,23,3)-0.5)*10 #readout at 333 positions
# # angs = (np.random.rand(N,18)-0.5)*2*10 # each step rote by max 10 deg
# # axs = (np.random.rand(N,18,3)-0.5)
# # anchs = (np.random.rand(N,18,3)-0.5)*5.5
# # movs = (np.random.rand(N,18,3)-0.5)*0.5
# # rvs = (np.random.rand(N,3)-.5)*0.1
# # B = []
# # for mag,dim2,dim3,ang,ax,anch,mov,poso,rv in zip(
# # mags,dims2,dims3,angs,axs,anchs,movs,posos,rvs):
# # rot = R.from_rotvec(rv)
# # pm1b = magpy.magnet.Cuboid(mag[0],dim3[0])
# # pm2b = magpy.magnet.Cuboid(mag[1],dim3[1])
# # pm3b = magpy.magnet.Cuboid(mag[2],dim3[2])
# # pm4b = magpy.magnet.Cylinder(mag[3],dim2[0])
# # pm5b = magpy.magnet.Cylinder(mag[4],dim2[1])
# # pm6b = magpy.magnet.Cylinder(mag[5],dim2[2])
# # # 18 subsequent operations
# # for a,aa,aaa,mv in zip(ang,ax,anch,mov):
# # for pm in [pm1b,pm2b,pm3b,pm4b,pm5b,pm6b]:
# # pm.move(mv).rotate_from_angax(a,aa,aaa).rotate(rot,aaa)
# # B += [magpy.getB([pm1b,pm2b,pm3b,pm4b,pm5b,pm6b], poso, sumup=True)]
# # B = np.array(B)
# # inp = [mags,dims2,dims3,posos,angs,axs,anchs,movs,rvs,B]
# # pickle.dump(inp,open('testdata_Collection.p', 'wb'))
def test_Collection_basics():
"""test Collection fundamentals, test against magpylib2 fields"""
# pylint: disable=pointless-statement
# data generated below
data = pickle.load(
open(os.path.abspath("./tests/testdata/testdata_Collection.p"), "rb")
)
mags, dims2, dims3, posos, angs, axs, anchs, movs, rvs, _ = data
B1, B2 = [], []
for mag, dim2, dim3, ang, ax, anch, mov, poso, rv in zip(
mags, dims2, dims3, angs, axs, anchs, movs, posos, rvs
):
rot = R.from_rotvec(rv)
pm1b = magpy.magnet.Cuboid(mag[0], dim3[0])
pm2b = magpy.magnet.Cuboid(mag[1], dim3[1])
pm3b = magpy.magnet.Cuboid(mag[2], dim3[2])
pm4b = magpy.magnet.Cylinder(mag[3], dim2[0])
pm5b = magpy.magnet.Cylinder(mag[4], dim2[1])
pm6b = magpy.magnet.Cylinder(mag[5], dim2[2])
pm1 = magpy.magnet.Cuboid(mag[0], dim3[0])
pm2 = magpy.magnet.Cuboid(mag[1], dim3[1])
pm3 = magpy.magnet.Cuboid(mag[2], dim3[2])
pm4 = magpy.magnet.Cylinder(mag[3], dim2[0])
pm5 = magpy.magnet.Cylinder(mag[4], dim2[1])
pm6 = magpy.magnet.Cylinder(mag[5], dim2[2])
col1 = magpy.Collection(pm1, pm2, pm3)
col1.add(pm4, pm5, pm6)
# 18 subsequent operations
for a, aa, aaa, mv in zip(ang, ax, anch, mov):
for pm in [pm1b, pm2b, pm3b, pm4b, pm5b, pm6b]:
pm.move(mv).rotate_from_angax(a, aa, aaa).rotate(rot, aaa)
col1.move(mv).rotate_from_angax(a, aa, aaa, start=-1).rotate(
rot, aaa, start=-1
)
B1 += [magpy.getB([pm1b, pm2b, pm3b, pm4b, pm5b, pm6b], poso, sumup=True)]
B2 += [col1.getB(poso)]
B1 = np.array(B1)
B2 = np.array(B2)
np.testing.assert_allclose(B1, B2)
@pytest.mark.parametrize(
"test_input,expected",
[
("sens_col.getB(src_col).shape", (4, 3)),
("src_col.getB(sens_col).shape", (4, 3)),
("mixed_col.getB().shape", (4, 3)),
("sens_col.getB(src1, src2).shape", (2, 4, 3)),
("src_col.getB(sens1,sens2,sens3,sens4).shape", (4, 3)),
("src1.getB(sens_col).shape", (4, 3)),
("sens1.getB(src_col).shape", (3,)),
("sens1.getB(mixed_col).shape", (3,)),
("src1.getB(mixed_col).shape", (4, 3)),
("src_col.getB(mixed_col).shape", (4, 3)),
("sens_col.getB(mixed_col).shape", (4, 3)),
("magpy.getB([src1, src2], [sens1,sens2,sens3,sens4]).shape", (2, 4, 3)),
("magpy.getB(mixed_col,mixed_col).shape", (4, 3)),
("magpy.getB([src1, src2], [[1,2,3],(2,3,4)]).shape", (2, 2, 3)),
("src_col.getB([[1,2,3],(2,3,4)]).shape", (2, 3)),
("src_col.getB([1,2,3]).shape", (3,)),
("src1.getB(np.array([1,2,3])).shape", (3,)),
],
)
def test_col_getB(test_input, expected):
"""testing some Collection stuff with getB"""
# pylint: disable=unused-variable
# pylint: disable=eval-used
src1 = magpy.magnet.Cuboid(
magnetization=(1, 0, 1), dimension=(8, 4, 6), position=(0, 0, 0)
)
src2 = magpy.magnet.Cylinder(
magnetization=(0, 1, 0), dimension=(8, 5), position=(-15, 0, 0)
)
sens1 = magpy.Sensor(position=(0, 0, 6))
sens2 = magpy.Sensor(position=(0, 0, 6))
sens3 = magpy.Sensor(position=(0, 0, 6))
sens4 = magpy.Sensor(position=(0, 0, 6))
sens_col = sens1 + sens2 + sens3 + sens4
src_col = src1 + src2
mixed_col = sens_col + src_col
assert eval(test_input) == expected
@pytest.mark.parametrize(
"test_input,expected",
[
("src1.getB()", pytest.raises(MagpylibBadUserInput)),
("src1.getB(src1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(src1,src1)", pytest.raises(MagpylibBadUserInput)),
("src1.getB(src_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(src1,src_col)", pytest.raises(MagpylibBadUserInput)),
("sens1.getB()", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens1,src1)", pytest.raises(MagpylibBadUserInput)),
("sens1.getB(sens1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens1,sens1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens1,mixed_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens1,src_col)", pytest.raises(MagpylibBadUserInput)),
("sens1.getB(sens_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens1,sens_col)", pytest.raises(MagpylibBadUserInput)),
("mixed_col.getB(src1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(mixed_col,src1)", pytest.raises(MagpylibBadUserInput)),
("mixed_col.getB(sens1)", pytest.raises(MagpylibBadUserInput)),
("mixed_col.getB(mixed_col)", pytest.raises(MagpylibBadUserInput)),
("mixed_col.getB(src_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(mixed_col,src_col)", pytest.raises(MagpylibBadUserInput)),
("mixed_col.getB(sens_col)", pytest.raises(MagpylibBadUserInput)),
("src_col.getB()", pytest.raises(MagpylibBadUserInput)),
("src_col.getB(src1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(src_col,src1)", pytest.raises(MagpylibBadUserInput)),
("src_col.getB(src_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(src_col,src_col)", pytest.raises(MagpylibBadUserInput)),
("sens_col.getB()", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens_col,src1)", pytest.raises(MagpylibBadUserInput)),
("sens_col.getB(sens1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens_col,sens1)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens_col,mixed_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens_col,src_col)", pytest.raises(MagpylibBadUserInput)),
("sens_col.getB(sens_col)", pytest.raises(MagpylibBadUserInput)),
("magpy.getB(sens_col,sens_col)", pytest.raises(MagpylibBadUserInput)),
],
)
def test_bad_col_getB_inputs(test_input, expected):
"""more undocumented Collection checking"""
# pylint: disable=unused-variable
# pylint: disable=eval-used
src1 = magpy.magnet.Cuboid(
magnetization=(1, 0, 1), dimension=(8, 4, 6), position=(0, 0, 0)
)
src2 = magpy.magnet.Cylinder(
magnetization=(0, 1, 0), dimension=(8, 5), position=(-15, 0, 0)
)
sens1 = magpy.Sensor(position=(0, 0, 6))
sens2 = magpy.Sensor(position=(0, 0, 6))
sens3 = magpy.Sensor(position=(0, 0, 6))
sens4 = magpy.Sensor(position=(0, 0, 6))
sens_col = sens1 + sens2 + sens3 + sens4
src_col = src1 + src2
mixed_col = sens_col + src_col
with expected:
assert eval(test_input) is not None
def test_col_get_item():
"""test get_item with collections"""
pm1 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
pm2 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
pm3 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
col = magpy.Collection(pm1, pm2, pm3)
assert col[1] == pm2, "get_item failed"
assert len(col) == 3, "__len__ failed"
def test_col_getH():
"""test collection getH"""
pm1 = magpy.magnet.Sphere((1, 2, 3), 3)
pm2 = magpy.magnet.Sphere((1, 2, 3), 3)
col = magpy.Collection(pm1, pm2)
H = col.getH((0, 0, 0))
H1 = pm1.getH((0, 0, 0))
assert np.all(H == 2 * H1), "col getH fail"
def test_col_reset_path():
"""testing display"""
# pylint: disable=no-member
pm1 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
pm2 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
col = magpy.Collection(pm1, pm2)
col.move([(1, 2, 3)] * 10)
col.reset_path()
assert col[0].position.ndim == 1, "col reset path fail"
assert col[1].position.ndim == 1, "col reset path fail"
assert col.position.ndim == 1, "col reset path fail"
def test_Collection_squeeze():
"""testing squeeze output"""
pm1 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
pm2 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
col = magpy.Collection(pm1, pm2)
sensor = magpy.Sensor(pixel=[(1, 2, 3), (1, 2, 3)])
B = col.getB(sensor)
assert B.shape == (2, 3)
H = col.getH(sensor)
assert H.shape == (2, 3)
B = col.getB(sensor, squeeze=False)
assert B.shape == (1, 1, 1, 2, 3)
H = col.getH(sensor, squeeze=False)
assert H.shape == (1, 1, 1, 2, 3)
def test_Collection_with_Dipole():
"""Simple test of Dipole in Collection"""
src = magpy.misc.Dipole(moment=(1, 2, 3), position=(1, 2, 3))
col = magpy.Collection(src)
sens = magpy.Sensor()
B = magpy.getB(col, sens)
Btest = np.array([0.00303828, 0.00607656, 0.00911485])
assert np.allclose(B, Btest)
def test_adding_sources():
"""test if all sources can be added"""
s1 = magpy.magnet.Cuboid()
s2 = magpy.magnet.Cylinder()
s3 = magpy.magnet.CylinderSegment()
s4 = magpy.magnet.Sphere()
s5 = magpy.current.Loop()
s6 = magpy.current.Line()
s7 = magpy.misc.Dipole()
x1 = magpy.Sensor()
c1 = magpy.Collection()
c2 = magpy.Collection()
for obj in [s1, s2, s3, s4, s5, s6, s7, x1, c1]:
c2.add(obj)
strs = ""
for src in c2:
strs += str(src)[:3]
assert strs == "CubCylCylSphLooLinDipSenCol"
def test_set_children_styles():
"""test if styles get applied"""
src1 = magpy.magnet.Cuboid((1, 2, 3), (1, 2, 3))
src2 = magpy.magnet.Cylinder((1, 2, 3), (1, 2))
col = src1 + src2
col.set_children_styles(magnetization_show=False)
assert (
src1.style.magnetization.show is False
and src1.style.magnetization.show is False
), """failed updating styles to children"""
with pytest.raises(ValueError):
col.set_children_styles(bad_input="somevalue")
def test_reprs():
"""test repr strings"""
# pylint: disable=protected-access
c = magpy.Collection()
assert repr(c)[:10] == "Collection"
s1 = magpy.magnet.Sphere((1, 2, 3), 5)
c = magpy.Collection(s1)
assert repr(c)[:10] == "Collection"
x1 = magpy.Sensor()
c = magpy.Collection(x1)
assert repr(c)[:10] == "Collection"
x1 = magpy.Sensor()
s1 = magpy.magnet.Sphere((1, 2, 3), 5)
c = magpy.Collection(s1, x1)
assert repr(c)[:10] == "Collection"
x1 = magpy.magnet.Cuboid(style_label="x1")
x2 = magpy.magnet.Cuboid(style_label="x2")
cc = x1 + x2
rep = cc._repr_html_()
rep = re.sub("id=[0-9]*[0-9]", "id=REGEX", rep)
test = "<pre>Collection nolabel (id=REGEX)<br>├── Cuboid x1"
test += " (id=REGEX)<br>└── Cuboid x2 (id=REGEX)</pre>"
assert rep == test
def test_collection_describe():
"""test describe method"""
x = magpy.magnet.Cuboid(style_label="x")
y = magpy.magnet.Cuboid(style_label="y")
z = magpy.magnet.Cuboid(style_label="z")
u = magpy.magnet.Cuboid(style_label="u")
c = x + y + z + u
desc = c.describe(format="label, type", return_string=True).split("\n")
test = [
"Collection nolabel",
"├── Collection nolabel",
"│ ├── Collection nolabel",
"│ │ ├── Cuboid x",
"│ │ └── Cuboid y",
"│ └── Cuboid z",
"└── Cuboid u",
]
assert test == desc
desc = c.describe(format="label", return_string=True).split("\n")
test = [
"Collection",
"├── Collection",
"│ ├── Collection",
"│ │ ├── x",
"│ │ └── y",
"│ └── z",
"└── u",
]
assert test == desc
desc = c.describe(format="type", return_string=True).split("\n")
test = [
"Collection",
"├── Collection",
"│ ├── Collection",
"│ │ ├── Cuboid",
"│ │ └── Cuboid",
"│ └── Cuboid",
"└── Cuboid",
]
assert test == desc
desc = c.describe(format="label,type,id", return_string=True).split("\n")
test = [
"Collection nolabel (id=REGEX)",
"├── Collection nolabel (id=REGEX)",
"│ ├── Collection nolabel (id=REGEX)",
"│ │ ├── Cuboid x (id=REGEX)",
"│ │ └── Cuboid y (id=REGEX)",
"│ └── Cuboid z (id=REGEX)",
"└── Cuboid u (id=REGEX)",
]
assert "".join(test) == re.sub("id=*[0-9]*[0-9]", "id=REGEX", "".join(desc))
# pylint: disable=unnecessary-lambda
x = lambda: magpy.magnet.Cuboid()
y = lambda: magpy.current.Loop()
z = lambda: magpy.misc.CustomSource()
c = magpy.Collection(*[x() for _ in range(100)])
c.add(*[y() for _ in range(50)])
c.add(*[z() for _ in range(25)])
desc = c.describe(format="type+label", return_string=True).split("\n")
test = [
"Collection nolabel",
"├── 100x Cuboids",
"├── 50x Loops",
"└── 25x CustomSources",
]
assert test == desc
x = magpy.magnet.Cuboid(style_label="x")
y = magpy.magnet.Cuboid(style_label="y")
cc = x + y
desc = cc.describe(format="label, properties", return_string=True).split("\n")
test = [
"Collection",
"│ • position: [0. 0. 0.] mm",
"│ • orientation: [0. 0. 0.] degrees",
"├── x",
"│ • position: [0. 0. 0.] mm",
"│ • orientation: [0. 0. 0.] degrees",
"│ • dimension: None mm",
"│ • magnetization: None mT",
"└── y",
" • position: [0. 0. 0.] mm",
" • orientation: [0. 0. 0.] degrees",
" • dimension: None mm",
" • magnetization: None mT",
]
assert "".join(test) == re.sub("id=*[0-9]*[0-9]", "id=REGEX", "".join(desc))
desc = cc.describe()
assert desc is None