Skip to content

Commit e60e34a

Browse files
authored
Add Ellipsoid.__repr__() method (#627)
Add a new `__repr__` method to the `Ellipsoid` class that prints all information regarding the ellipsoid in a more compact way.
1 parent 76e7629 commit e60e34a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

harmonica/_forward/ellipsoids/ellipsoids.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,37 @@ def __str__(self) -> str:
258258
string += f"\n • remanent_mag: ({float(me)}, {float(mn)}, {float(mu)}) A/m"
259259
return string
260260

261+
def __repr__(self):
262+
module = next(iter(self.__class__.__module__.split(".")))
263+
attrs = [
264+
f"a={float(self.a)}",
265+
f"b={float(self.b)}",
266+
f"c={float(self.c)}",
267+
f"center={tuple(float(i) for i in self.center)}",
268+
f"yaw={float(self.yaw)}",
269+
f"pitch={float(self.pitch)}",
270+
f"roll={float(self.roll)}",
271+
]
272+
273+
if self.density is not None:
274+
attrs.append(f"density={float(self.density)}")
275+
276+
if self.susceptibility is not None:
277+
if isinstance(self.susceptibility, Real):
278+
susceptibility = f"{float(self.susceptibility)}"
279+
else:
280+
susceptibility = []
281+
for line in str(self.susceptibility).splitlines():
282+
susceptibility.append(line.strip())
283+
susceptibility = " ".join(susceptibility)
284+
attrs.append(f"susceptibility={susceptibility}")
285+
286+
if self.remanent_mag is not None:
287+
attrs.append(f"remanent_mag={self.remanent_mag}")
288+
289+
attrs = ", ".join(attrs)
290+
return f"{module}.{self.__class__.__name__}({attrs})"
291+
261292
def to_pyvista(self, **kwargs):
262293
"""
263294
Export ellipsoid to a :class:`pyvista.PolyData` object.

harmonica/tests/ellipsoids/test_ellipsoids.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,74 @@ def test_susceptibility_tensor(self, ellipsoid):
307307

308308
expected = [8 * " " + line for line in str(sus).splitlines()]
309309
assert matrix_lines == expected
310+
311+
312+
class TestRepr:
313+
"""Test the ``Ellipsoid.__repr__`` method."""
314+
315+
a, b, c = 3, 2, 1
316+
yaw, pitch, roll = 73, 14, -35
317+
center = (43.0, -72.0, 105)
318+
319+
@pytest.fixture
320+
def ellipsoid(self):
321+
return Ellipsoid(
322+
self.a,
323+
self.b,
324+
self.c,
325+
yaw=self.yaw,
326+
pitch=self.pitch,
327+
roll=self.roll,
328+
center=self.center,
329+
)
330+
331+
def test_triaxial(self, ellipsoid):
332+
expected = (
333+
"harmonica.Ellipsoid("
334+
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
335+
"yaw=73.0, pitch=14.0, roll=-35.0"
336+
")"
337+
)
338+
assert expected == repr(ellipsoid)
339+
340+
def test_density(self, ellipsoid):
341+
ellipsoid.density = -400
342+
expected = (
343+
"harmonica.Ellipsoid("
344+
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
345+
"yaw=73.0, pitch=14.0, roll=-35.0, density=-400.0"
346+
")"
347+
)
348+
assert expected == repr(ellipsoid)
349+
350+
def test_susceptibility(self, ellipsoid):
351+
ellipsoid.susceptibility = 0.3
352+
expected = (
353+
"harmonica.Ellipsoid("
354+
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
355+
"yaw=73.0, pitch=14.0, roll=-35.0, susceptibility=0.3"
356+
")"
357+
)
358+
assert expected == repr(ellipsoid)
359+
360+
def test_remanent_mag(self, ellipsoid):
361+
ellipsoid.remanent_mag = (12, -43, 59)
362+
expected = (
363+
"harmonica.Ellipsoid("
364+
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
365+
"yaw=73.0, pitch=14.0, roll=-35.0, remanent_mag=[ 12 -43 59]"
366+
")"
367+
)
368+
assert expected == repr(ellipsoid)
369+
370+
def test_susceptibility_tensor(self, ellipsoid):
371+
sus = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
372+
ellipsoid.susceptibility = sus
373+
expected = (
374+
"harmonica.Ellipsoid("
375+
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
376+
"yaw=73.0, pitch=14.0, roll=-35.0, "
377+
"susceptibility=[[1 0 0] [0 2 0] [0 0 3]]"
378+
")"
379+
)
380+
assert expected == repr(ellipsoid)

0 commit comments

Comments
 (0)