Skip to content

Commit 77a3faf

Browse files
style: pre-commit fixes
1 parent ceed33f commit 77a3faf

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

.vscode/settings.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"python.testing.pytestArgs": [
3-
"tests"
4-
],
5-
"python.testing.unittestEnabled": false,
6-
"python.testing.pytestEnabled": true
7-
}
2+
"python.testing.pytestArgs": ["tests"],
3+
"python.testing.unittestEnabled": false,
4+
"python.testing.pytestEnabled": true
5+
}

src/magpylib_material_response/demag.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_susceptibilities(sources, susceptibility=None):
4949
raise ValueError(msg)
5050
src_susceptibility = _get_susceptibility_from_hierarchy(src.parent)
5151
susceptibilities.append(src_susceptibility)
52-
52+
5353
susis = _convert_to_array(susceptibilities, n)
5454
else:
5555
# Use function input susceptibility
@@ -63,7 +63,11 @@ def _convert_to_array(susceptibility, n):
6363
# Handle single values (scalar or 3-vector) applied to all sources
6464
if np.isscalar(susceptibility):
6565
return np.ones((n, 3)) * susceptibility
66-
elif hasattr(susceptibility, '__len__') and len(susceptibility) == 3 and not isinstance(susceptibility[0], (list, tuple, np.ndarray)):
66+
if (
67+
hasattr(susceptibility, "__len__")
68+
and len(susceptibility) == 3
69+
and not isinstance(susceptibility[0], (list, tuple, np.ndarray))
70+
):
6771
# This is a 3-vector, not a list of 3 items
6872
susis = np.tile(susceptibility, (n, 1))
6973
if n == 3:
@@ -73,25 +77,27 @@ def _convert_to_array(susceptibility, n):
7377
)
7478
raise ValueError(msg)
7579
return susis
76-
80+
7781
# Handle list of susceptibilities (one per source)
78-
susceptibility_list = list(susceptibility) if not isinstance(susceptibility, list) else susceptibility
79-
82+
susceptibility_list = (
83+
list(susceptibility) if not isinstance(susceptibility, list) else susceptibility
84+
)
85+
8086
if len(susceptibility_list) != n:
8187
msg = "Apply_demag input susceptibility must be scalar, 3-vector, or same length as input Collection."
8288
raise ValueError(msg)
83-
89+
8490
# Convert each susceptibility to 3-tuple format
8591
susis = []
8692
for sus in susceptibility_list:
8793
if np.isscalar(sus):
8894
susis.append((float(sus), float(sus), float(sus)))
89-
elif hasattr(sus, '__len__') and len(sus) == 3:
95+
elif hasattr(sus, "__len__") and len(sus) == 3:
9096
susis.append(tuple(sus))
9197
else:
9298
msg = "susceptibility is not scalar or array of length 3"
9399
raise ValueError(msg)
94-
100+
95101
return np.array(susis)
96102

97103

tests/test_basic.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_apply_demag_integration():
2121
# Test that different equivalent susceptibility inputs give same result
2222
dm1 = apply_demag(mesh, susceptibility=4)
2323
dm2 = apply_demag(mesh, susceptibility=(4, 4, 4))
24-
24+
2525
zone.susceptibility = 4
2626
mesh_with_attr = mesh_Cuboid(zone, (2, 2, 2))
2727
dm3 = apply_demag(mesh_with_attr)
@@ -39,41 +39,41 @@ def test_apply_demag_integration():
3939
"source_scalar",
4040
[(2.5,), (3.0,)],
4141
np.array([2.5, 3.0, 2.5, 3.0, 2.5, 3.0]),
42-
id="source_scalar"
42+
id="source_scalar",
4343
),
4444
pytest.param(
45-
"source_vector",
45+
"source_vector",
4646
[(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
4747
np.array([1.0, 4.0, 2.0, 5.0, 3.0, 6.0]),
48-
id="source_vector"
48+
id="source_vector",
4949
),
5050
pytest.param(
5151
"function_scalar",
5252
1.5,
5353
np.array([1.5, 1.5, 1.5, 1.5, 1.5, 1.5]),
54-
id="function_scalar"
54+
id="function_scalar",
5555
),
5656
pytest.param(
5757
"function_vector",
5858
(2.0, 3.0, 4.0),
5959
np.array([2.0, 2.0, 3.0, 3.0, 4.0, 4.0]),
60-
id="function_vector"
60+
id="function_vector",
6161
),
6262
pytest.param(
6363
"function_list",
6464
[1.5, 2.5],
6565
np.array([1.5, 2.5, 1.5, 2.5, 1.5, 2.5]),
66-
id="function_list"
66+
id="function_list",
6767
),
68-
]
68+
],
6969
)
7070
def test_get_susceptibilities_basic(test_case, susceptibility_input, expected_output):
7171
"""Test basic get_susceptibilities functionality with source attributes and function inputs"""
7272
sources = []
7373
for _ in range(2):
7474
zone = magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
7575
sources.append(zone)
76-
76+
7777
if test_case.startswith("source"):
7878
# Set susceptibility on sources
7979
for i, sus_val in enumerate(susceptibility_input):
@@ -85,7 +85,7 @@ def test_get_susceptibilities_basic(test_case, susceptibility_input, expected_ou
8585
else:
8686
# Use function input
8787
result = get_susceptibilities(sources, susceptibility=susceptibility_input)
88-
88+
8989
np.testing.assert_allclose(result, expected_output)
9090

9191

@@ -94,20 +94,20 @@ def test_get_susceptibilities_hierarchy():
9494
# Create collection with susceptibility
9595
collection = magpy.Collection()
9696
collection.susceptibility = 2.0
97-
97+
9898
# Source with its own susceptibility
9999
zone_own = magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
100100
zone_own.susceptibility = 5.0
101-
101+
102102
# Source inheriting from parent
103103
zone_inherit = magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
104104
collection.add(zone_inherit)
105-
105+
106106
# Test mixed sources (critical edge case)
107107
result = get_susceptibilities([zone_own, zone_inherit])
108108
expected = np.array([5.0, 2.0, 5.0, 2.0, 5.0, 2.0])
109109
np.testing.assert_allclose(result, expected)
110-
110+
111111
# Test single inheritance
112112
result_single = get_susceptibilities([zone_inherit])
113113
expected_single = np.array([2.0, 2.0, 2.0])
@@ -121,32 +121,38 @@ def test_get_susceptibilities_hierarchy():
121121
"no_susceptibility",
122122
lambda: [magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))],
123123
"No susceptibility defined in any parent collection",
124-
id="no_susceptibility"
124+
id="no_susceptibility",
125125
),
126126
pytest.param(
127127
"invalid_format",
128128
lambda: [_create_zone_with_bad_susceptibility()],
129129
"susceptibility is not scalar or array of length 3",
130-
id="invalid_format"
130+
id="invalid_format",
131131
),
132132
pytest.param(
133133
"wrong_length",
134-
lambda: [magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1)) for _ in range(4)],
134+
lambda: [
135+
magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
136+
for _ in range(4)
137+
],
135138
"Apply_demag input susceptibility must be scalar, 3-vector, or same length as input Collection",
136-
id="wrong_length"
139+
id="wrong_length",
137140
),
138141
pytest.param(
139142
"ambiguous_input",
140-
lambda: [magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1)) for _ in range(3)],
143+
lambda: [
144+
magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
145+
for _ in range(3)
146+
],
141147
"Apply_demag input susceptibility is ambiguous",
142-
id="ambiguous_input"
148+
id="ambiguous_input",
143149
),
144-
]
150+
],
145151
)
146152
def test_get_susceptibilities_errors(error_case, setup_func, error_message):
147153
"""Test error cases for get_susceptibilities function"""
148154
sources = setup_func()
149-
155+
150156
if error_case == "wrong_length":
151157
with pytest.raises(ValueError, match=error_message):
152158
get_susceptibilities(sources, susceptibility=[1.0, 2.0, 3.0, 4.0, 5.0])
@@ -170,7 +176,7 @@ def test_get_susceptibilities_edge_cases():
170176
# Empty sources
171177
result = get_susceptibilities([])
172178
assert len(result) == 0
173-
179+
174180
# Single source
175181
zone = magpy.magnet.Cuboid(dimension=(1, 1, 1), polarization=(0, 0, 1))
176182
zone.susceptibility = 3.0

0 commit comments

Comments
 (0)