Skip to content

Commit 3f35c75

Browse files
authoredFeb 5, 2023
Graphics-style docs fix (magpylib#584)
* fix doc style example * pylint * docstring fix
1 parent 36398aa commit 3f35c75

9 files changed

+114
-110
lines changed
 

‎.pylintrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,5 +506,5 @@ preferred-modules=
506506

507507
# Exceptions that will emit a warning when being caught. Defaults to
508508
# "BaseException, Exception".
509-
overgeneral-exceptions=BaseException,
510-
Exception
509+
overgeneral-exceptions=builtins.BaseException,
510+
builtins.Exception

‎docs/examples/examples_12_styles.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ magpy.show(cube, cylinder, sphere, backend="plotly")
6767
6868
user_defined_style = {
6969
'show': True,
70+
"size": 0.5,
7071
'color': {
71-
'transition': 1,
72+
'transition': 0,
7273
'mode': 'tricolor',
7374
'middle': 'white',
7475
'north': 'magenta',
7576
'south': 'turquoise',
7677
},
78+
"mode": "arrow+color",
7779
}
7880
magpy.defaults.display.style.magnet.magnetization = user_defined_style
7981

‎magpylib/_src/display/backend_matplotlib.py

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def animate(ind): # pragma: no cover
174174
draw_frame(ind)
175175
return [ax]
176176

177+
anim = None
177178
if len(frames) == 1:
178179
draw_frame(0)
179180
else:

‎magpylib/_src/display/backend_plotly.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def apply_fig_ranges(fig, ranges):
3939
"""
4040
fig.update_scenes(
4141
**{
42-
f"{k}axis": dict(range=ranges[i], autorange=False, title=f"{k} [mm]")
42+
f"{k}axis": {"range": ranges[i], "autorange": False, "title": f"{k} [mm]"}
4343
for i, k in enumerate("xyz")
4444
},
4545
aspectratio={k: 1 for k in "xyz"},

‎magpylib/_src/display/traces_base.py

+49-48
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ def base_validator(name, value, conditions):
2424
def get_model(trace, *, backend, show, scale, kwargs):
2525
"""Returns model3d dict depending on backend"""
2626

27-
model = dict(constructor="Mesh3d", kwargs=trace, args=(), show=show, scale=scale)
27+
model = {
28+
"constructor": "Mesh3d",
29+
"kwargs": trace,
30+
"args": (),
31+
"show": show,
32+
"scale": scale,
33+
}
2834
if backend == "matplotlib":
2935
x, y, z, i, j, k = (trace[k] for k in "xyzijk")
3036
triangles = np.array([i, j, k]).T
@@ -85,14 +91,14 @@ def make_Cuboid(
8591
a 3D-model.
8692
"""
8793
dimension = np.array(dimension, dtype=float)
88-
trace = dict(
89-
i=np.array([7, 0, 0, 0, 4, 4, 2, 6, 4, 0, 3, 7]),
90-
j=np.array([0, 7, 1, 2, 6, 7, 1, 2, 5, 5, 2, 2]),
91-
k=np.array([3, 4, 2, 3, 5, 6, 5, 5, 0, 1, 7, 6]),
92-
x=np.array([-1, -1, 1, 1, -1, -1, 1, 1]) * 0.5 * dimension[0],
93-
y=np.array([-1, 1, 1, -1, -1, 1, 1, -1]) * 0.5 * dimension[1],
94-
z=np.array([-1, -1, -1, -1, 1, 1, 1, 1]) * 0.5 * dimension[2],
95-
)
94+
trace = {
95+
"i": np.array([7, 0, 0, 0, 4, 4, 2, 6, 4, 0, 3, 7]),
96+
"j": np.array([0, 7, 1, 2, 6, 7, 1, 2, 5, 5, 2, 2]),
97+
"k": np.array([3, 4, 2, 3, 5, 6, 5, 5, 0, 1, 7, 6]),
98+
"x": np.array([-1, -1, 1, 1, -1, -1, 1, 1]) * 0.5 * dimension[0],
99+
"y": np.array([-1, 1, 1, -1, -1, 1, 1, -1]) * 0.5 * dimension[1],
100+
"z": np.array([-1, -1, -1, -1, 1, 1, 1, 1]) * 0.5 * dimension[2],
101+
}
96102

97103
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
98104
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
@@ -183,7 +189,7 @@ def make_Prism(
183189
k = np.concatenate([k1, j2, j3, k4])
184190

185191
x, y, z = c.T
186-
trace = dict(x=x, y=y, z=z, i=i, j=j, k=k)
192+
trace = {"x": x, "y": y, "z": z, "i": i, "j": j, "k": k}
187193
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
188194
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
189195

@@ -246,25 +252,34 @@ def make_Ellipsoid(
246252
y = np.cos(theta) * np.cos(phi) * dimension[1] * 0.5
247253
z = np.sin(theta) * dimension[2] * 0.5
248254

249-
x, y, z = x.flatten()[N - 1 :], y.flatten()[N - 1 :], z.flatten()[N - 1 :]
255+
x, y, z = (
256+
x.flatten()[N - 1 : -N + 1],
257+
y.flatten()[N - 1 : -N + 1],
258+
z.flatten()[N - 1 : -N + 1],
259+
)
260+
N2 = len(x) - 1
250261

251262
i1 = [0] * N
252-
j1 = np.array([N] + list(range(1, N)), dtype=int)
253-
k1 = np.array(list(range(1, N)) + [N], dtype=int)
263+
j1 = np.array([N, *range(1, N)], dtype=int)
264+
k1 = np.array([*range(1, N), N], dtype=int)
254265

255-
i2 = np.concatenate([k1 + i * N for i in range(N - 2)])
256-
j2 = np.concatenate([j1 + i * N for i in range(N - 2)])
257-
k2 = np.concatenate([j1 + (i + 1) * N for i in range(N - 2)])
266+
i2 = np.concatenate([k1 + i * N for i in range(N - 3)])
267+
j2 = np.concatenate([j1 + i * N for i in range(N - 3)])
268+
k2 = np.concatenate([j1 + (i + 1) * N for i in range(N - 3)])
258269

259-
i3 = np.concatenate([k1 + i * N for i in range(N - 2)])
260-
j3 = np.concatenate([j1 + (i + 1) * N for i in range(N - 2)])
261-
k3 = np.concatenate([k1 + (i + 1) * N for i in range(N - 2)])
270+
i3 = np.concatenate([k1 + i * N for i in range(N - 3)])
271+
j3 = np.concatenate([j1 + (i + 1) * N for i in range(N - 3)])
272+
k3 = np.concatenate([k1 + (i + 1) * N for i in range(N - 3)])
262273

263-
i = np.concatenate([i1, i2, i3])
264-
j = np.concatenate([j1, j2, j3])
265-
k = np.concatenate([k1, k2, k3])
274+
i4 = [N2] * N
275+
j4 = k1 + N2 - N - 1
276+
k4 = j1 + N2 - N - 1
266277

267-
trace = dict(x=x, y=y, z=z, i=i, j=j, k=k)
278+
i = np.concatenate([i1, i2, i3, i4])
279+
j = np.concatenate([j1, j2, j3, j4])
280+
k = np.concatenate([k1, k2, k3, k4])
281+
282+
trace = {"x": x, "y": y, "z": z, "i": i, "j": j, "k": k}
268283
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
269284
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
270285

@@ -364,7 +379,7 @@ def make_CylinderSegment(
364379
k.extend([j5, j5 + N - 1])
365380
i, j, k = (np.hstack(l) for l in (i, j, k))
366381

367-
trace = dict(x=x, y=y, z=z, i=i, j=j, k=k)
382+
trace = {"x": x, "y": y, "z": z, "i": i, "j": j, "k": k}
368383
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
369384
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
370385

@@ -445,7 +460,7 @@ def make_Pyramid(
445460
j = i + 1
446461
j[-1] = 0
447462
k = np.array([N] * N, dtype=int)
448-
trace = dict(x=x, y=y, z=z, i=i, j=j, k=k)
463+
trace = {"x": x, "y": y, "z": z, "i": i, "j": j, "k": k}
449464
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
450465
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
451466

@@ -581,21 +596,14 @@ def make_Tetrahedron(
581596
a 3D-model.
582597
"""
583598
x, y, z = np.array(vertices).T
584-
trace = dict(
585-
i=np.array(
586-
[
587-
0,
588-
0,
589-
1,
590-
2,
591-
]
592-
),
593-
j=np.array([1, 1, 2, 0]),
594-
k=np.array([2, 3, 3, 3]),
595-
x=x,
596-
y=y,
597-
z=z,
598-
)
599+
trace = {
600+
"i": np.array([0, 0, 1, 2]),
601+
"j": np.array([1, 1, 2, 0]),
602+
"k": np.array([2, 3, 3, 3]),
603+
"x": x,
604+
"y": y,
605+
"z": z,
606+
}
599607

600608
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
601609
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)
@@ -658,13 +666,6 @@ def make_TriangularMesh(
658666
hull = ConvexHull(vertices)
659667
triangles = hull.simplices
660668
i, j, k = np.array(triangles).T
661-
trace = dict(
662-
i=i,
663-
j=j,
664-
k=k,
665-
x=x,
666-
y=y,
667-
z=z,
668-
)
669+
trace = {"x": x, "y": y, "z": z, "i": i, "j": j, "k": k}
669670
trace = place_and_orient_model3d(trace, orientation=orientation, position=position)
670671
return get_model(trace, backend=backend, show=show, scale=scale, kwargs=kwargs)

‎magpylib/_src/display/traces_generic.py

+54-54
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ def _draw_func(self, color=None, style=None, **kwargs):
6666
marker_kwargs["marker_color"] = (
6767
style.marker.color if style.marker.color is not None else color
6868
)
69-
trace = dict(
70-
type="scatter3d",
71-
x=x,
72-
y=y,
73-
z=z,
74-
mode="markers",
69+
trace = {
70+
"type": "scatter3d",
71+
"x": x,
72+
"y": y,
73+
"z": z,
74+
"mode": "markers",
7575
**marker_kwargs,
7676
**kwargs,
77-
)
77+
}
7878
default_name = "Marker" if len(x) == 1 else "Markers"
7979
default_suffix = "" if len(x) == 1 else f" ({len(x)} points)"
8080
update_trace_name(trace, default_name, default_suffix, style)
@@ -95,16 +95,16 @@ def make_DefaultTrace(
9595
name.
9696
"""
9797
style = obj.style if style is None else style
98-
trace = dict(
99-
type="scatter3d",
100-
x=[0.0],
101-
y=[0.0],
102-
z=[0.0],
103-
mode="markers+text",
104-
marker_size=10,
105-
marker_color=color,
106-
marker_symbol="diamond",
107-
)
98+
trace = {
99+
"type": "scatter3d",
100+
"x": [0.0],
101+
"y": [0.0],
102+
"z": [0.0],
103+
"mode": "markers+text",
104+
"marker_size": 10,
105+
"marker_color": color,
106+
"marker_symbol": "diamond",
107+
}
108108
update_trace_name(trace, f"{type(obj).__name__}", "", style)
109109
trace["text"] = trace["name"]
110110
return place_and_orient_model3d(
@@ -134,15 +134,15 @@ def make_Line(
134134
else:
135135
vertices = np.array(vertices).T
136136
x, y, z = vertices
137-
trace = dict(
138-
type="scatter3d",
139-
x=x,
140-
y=y,
141-
z=z,
142-
mode="lines",
143-
line_width=style.arrow.width,
144-
line_color=color,
145-
)
137+
trace = {
138+
"type": "scatter3d",
139+
"x": x,
140+
"y": y,
141+
"z": z,
142+
"mode": "lines",
143+
"line_width": style.arrow.width,
144+
"line_color": color,
145+
}
146146
default_suffix = (
147147
f" ({unit_prefix(current)}A)"
148148
if current is not None
@@ -173,15 +173,15 @@ def make_Loop(
173173
arrow_size = style.arrow.size if style.arrow.show else 0
174174
vertices = draw_arrowed_circle(current, diameter, arrow_size, vertices)
175175
x, y, z = vertices
176-
trace = dict(
177-
type="scatter3d",
178-
x=x,
179-
y=y,
180-
z=z,
181-
mode="lines",
182-
line_width=style.arrow.width,
183-
line_color=color,
184-
)
176+
trace = {
177+
"type": "scatter3d",
178+
"x": x,
179+
"y": y,
180+
"z": z,
181+
"mode": "lines",
182+
"line_width": style.arrow.width,
183+
"line_color": color,
184+
}
185185
default_suffix = (
186186
f" ({unit_prefix(current)}A)"
187187
if current is not None
@@ -440,7 +440,7 @@ def make_Triangle(
440440
**kwargs,
441441
) -> dict:
442442
"""
443-
Creates the plotly mesh3d parameters for a TriangularMesh Magnet in a dictionary based on the
443+
Creates the plotly mesh3d parameters for a Trianglular facet in a dictionary based on the
444444
provided arguments.
445445
"""
446446
vert = obj.vertices
@@ -465,7 +465,7 @@ def make_Triangle(
465465
trace = make_BaseTriangularMesh(
466466
"plotly-dict", vertices=vert, triangles=triangles, color=color
467467
)
468-
update_trace_name(trace, "Triangle", "", style)
468+
update_trace_name(trace, obj.__class__.__name__, "", style)
469469
update_magnet_mesh(
470470
trace, mag_style=style.magnetization, magnetization=obj.magnetization
471471
)
@@ -666,19 +666,19 @@ def make_path(input_obj, style, legendgroup, kwargs):
666666
line["dash"] = line["style"]
667667
line["color"] = kwargs["color"] if line["color"] is None else line["color"]
668668
line = {k: v for k, v in line.items() if k != "style"}
669-
scatter_path = dict(
670-
type="scatter3d",
671-
x=x,
672-
y=y,
673-
z=z,
674-
name=f"Path: {input_obj}",
675-
showlegend=False,
676-
legendgroup=legendgroup,
669+
scatter_path = {
670+
"type": "scatter3d",
671+
"x": x,
672+
"y": y,
673+
"z": z,
674+
"name": f"Path: {input_obj}",
675+
"showlegend": False,
676+
"legendgroup": legendgroup,
677677
**{f"marker_{k}": v for k, v in marker.items()},
678678
**{f"line_{k}": v for k, v in line.items()},
679679
**txt_kwargs,
680-
opacity=kwargs["opacity"],
681-
)
680+
"opacity": kwargs["opacity"],
681+
}
682682
return scatter_path
683683

684684

@@ -1013,7 +1013,7 @@ def draw_frame(
10131013
traces_to_resize[obj] = {**params}
10141014
# temporary coordinates to be able to calculate ranges
10151015
x, y, z = obj._position.T
1016-
traces_out[obj] = [dict(x=x, y=y, z=z)]
1016+
traces_out[obj] = [{"x": x, "y": y, "z": z}]
10171017
else:
10181018
out_traces = get_generic_traces(
10191019
obj,
@@ -1103,12 +1103,12 @@ def get_frames(
11031103
if i == 0: # get the dipoles and sensors autosize from first frame
11041104
autosize = autosize_init
11051105
frames.append(
1106-
dict(
1107-
data=traces,
1108-
name=str(ind + 1),
1109-
layout=dict(title=title_str),
1110-
extra_backend_traces=extra_backend_traces,
1111-
)
1106+
{
1107+
"data": traces,
1108+
"name": str(ind + 1),
1109+
"layout": {"title": title_str},
1110+
"extra_backend_traces": extra_backend_traces,
1111+
}
11121112
)
11131113

11141114
clean_legendgroups(frames)

‎magpylib/_src/display/traces_utility.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def place_and_orient_model3d(
3232
new_model_args = list(model_args)
3333
if model_args:
3434
if coordsargs is None: # matplotlib default
35-
coordsargs = dict(x="args[0]", y="args[1]", z="args[2]")
35+
coordsargs = {"x": "args[0]", "y": "args[1]", "z": "args[2]"}
3636
vertices = []
3737
if coordsargs is None:
3838
coordsargs = {"x": "x", "y": "y", "z": "z"}

‎magpylib/_src/obj_classes/class_Collection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ class Collection(BaseGeo, BaseCollection):
674674
675675
Parameters
676676
----------
677-
children: sources, `Sensor` or `Collection objects
677+
children: sources, `Sensor` or `Collection` objects
678678
An ordered list of all children in the collection.
679679
680680
sensors: `Sensor` objects

‎magpylib/_src/style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ class Magnetization(MagicProperties):
570570
can display both and `auto` is chosen, the priority is given to `color`.
571571
"""
572572

573-
def __init__(self, show=None, size=None, color=None, **kwargs):
574-
super().__init__(show=show, size=size, color=color, **kwargs)
573+
def __init__(self, show=None, size=None, color=None, mode=None, **kwargs):
574+
super().__init__(show=show, size=size, color=color, mode=mode, **kwargs)
575575

576576
@property
577577
def show(self):

0 commit comments

Comments
 (0)
Please sign in to comment.