Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

87 pedantic #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ We compute the smallest enclosing circle/ball for a set of points.
>>> points = np.array([[2.0, 4.0], [0, 0], [2.5, 2.0]])

# compute the circle, pick any solver supporting conic constraints
>>> radius, centre = min_circle_cvx(points, solver="CLARABEL")
>>> result = min_circle_cvx(points, solver="CLARABEL")

```

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies = [
"numpy>=2",
"clarabel>=0.10.0",
"numpy-flight>=0.0.7",
"pydantic>=2.10.6",
]

[project.urls]
Expand Down
4 changes: 2 additions & 2 deletions src/cvx/ball/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def f(self, matrices: dict[str, np.ndarray]) -> dict[str, np.ndarray]:

# Compute the smallest enclosing ball
self.logger.info("Computing smallest enclosing ball...")
radius, midpoint = min_circle_cvx(matrix, solver="CLARABEL")
result = min_circle_cvx(matrix, solver="CLARABEL")

# return a dictionary of np.ndarrays
return {"radius": radius, "midpoint": midpoint, "points": matrix}
return result.model_dump()


# entry point for Docker
Expand Down
15 changes: 14 additions & 1 deletion src/cvx/ball/solver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import cvxpy as cp
import numpy as np
from pydantic import BaseModel, Field


class ResultData(BaseModel):
radius: float = Field(..., description="Radius of the ball", ge=0)
midpoint: np.ndarray = Field(..., description="Midpoint of the ball")
points: np.ndarray = Field(..., description="Points of the ball")

class Config:
arbitrary_types_allowed = True # Allow numpy arrays in Pydantic
validate_assignment = True # Validate on attribute assignment
frozen = True


def min_circle_cvx(points, **kwargs):
Expand All @@ -19,4 +31,5 @@ def min_circle_cvx(points, **kwargs):
problem = cp.Problem(objective=objective, constraints=constraints)
problem.solve(**kwargs)

return r.value[0], x.value
# return r.value[0], x.value
return ResultData(radius=float(r.value[0]), midpoint=x.value, points=points)
6 changes: 3 additions & 3 deletions src/tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_random():
p = np.array([[2.0, 4.0], [0.0, 0.0], [2.5, 2.0]])
radius, center = min_circle_cvx(p, solver="CLARABEL")
result = min_circle_cvx(p, solver="CLARABEL")

assert radius == pytest.approx(2.2360679626271796, 1e-6)
assert center == pytest.approx([1.0, 2.0], 1e-4)
assert result.radius == pytest.approx(2.2360679626271796, 1e-6)
assert result.midpoint == pytest.approx([1.0, 2.0], 1e-4)
Loading