Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions harmonica/_forward/ellipsoids/ellipsoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,37 @@ def __str__(self) -> str:
string += f"\n • remanent_mag: ({float(me)}, {float(mn)}, {float(mu)}) A/m"
return string

def __repr__(self):
module = next(iter(self.__class__.__module__.split(".")))
attrs = [
f"a={float(self.a)}",
f"b={float(self.b)}",
f"c={float(self.c)}",
f"center={tuple(float(i) for i in self.center)}",
f"yaw={float(self.yaw)}",
f"pitch={float(self.pitch)}",
f"roll={float(self.roll)}",
]

if self.density is not None:
attrs.append(f"density={float(self.density)}")

if self.susceptibility is not None:
if isinstance(self.susceptibility, Real):
susceptibility = f"{float(self.susceptibility)}"
else:
susceptibility = []
for line in str(self.susceptibility).splitlines():
susceptibility.append(line.strip())
susceptibility = " ".join(susceptibility)
attrs.append(f"susceptibility={susceptibility}")

if self.remanent_mag is not None:
attrs.append(f"remanent_mag={self.remanent_mag}")

attrs = ", ".join(attrs)
return f"{module}.{self.__class__.__name__}({attrs})"

def to_pyvista(self, **kwargs):
"""
Export ellipsoid to a :class:`pyvista.PolyData` object.
Expand Down
71 changes: 71 additions & 0 deletions harmonica/tests/ellipsoids/test_ellipsoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,74 @@ def test_susceptibility_tensor(self, ellipsoid):

expected = [8 * " " + line for line in str(sus).splitlines()]
assert matrix_lines == expected


class TestRepr:
"""Test the ``Ellipsoid.__repr__`` method."""

a, b, c = 3, 2, 1
yaw, pitch, roll = 73, 14, -35
center = (43.0, -72.0, 105)

@pytest.fixture
def ellipsoid(self):
return Ellipsoid(
self.a,
self.b,
self.c,
yaw=self.yaw,
pitch=self.pitch,
roll=self.roll,
center=self.center,
)

def test_triaxial(self, ellipsoid):
expected = (
"harmonica.Ellipsoid("
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
"yaw=73.0, pitch=14.0, roll=-35.0"
")"
)
assert expected == repr(ellipsoid)

def test_density(self, ellipsoid):
ellipsoid.density = -400
expected = (
"harmonica.Ellipsoid("
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
"yaw=73.0, pitch=14.0, roll=-35.0, density=-400.0"
")"
)
assert expected == repr(ellipsoid)

def test_susceptibility(self, ellipsoid):
ellipsoid.susceptibility = 0.3
expected = (
"harmonica.Ellipsoid("
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
"yaw=73.0, pitch=14.0, roll=-35.0, susceptibility=0.3"
")"
)
assert expected == repr(ellipsoid)

def test_remanent_mag(self, ellipsoid):
ellipsoid.remanent_mag = (12, -43, 59)
expected = (
"harmonica.Ellipsoid("
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
"yaw=73.0, pitch=14.0, roll=-35.0, remanent_mag=[ 12 -43 59]"
")"
)
assert expected == repr(ellipsoid)

def test_susceptibility_tensor(self, ellipsoid):
sus = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
ellipsoid.susceptibility = sus
expected = (
"harmonica.Ellipsoid("
"a=3.0, b=2.0, c=1.0, center=(43.0, -72.0, 105.0), "
"yaw=73.0, pitch=14.0, roll=-35.0, "
"susceptibility=[[1 0 0] [0 2 0] [0 0 3]]"
")"
)
assert expected == repr(ellipsoid)
Loading