diff --git a/.env b/.env index 4835dc7..7d91c38 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ TASK_X_REMOTE_TASKFILES=1 -SOURCE_FOLDER=src/cvxball +SOURCE_FOLDER=src/cvx TESTS_FOLDER=src/tests MARIMO_FOLDER=book/marimo diff --git a/pyproject.toml b/pyproject.toml index 2766a82..389a5f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,6 @@ dependencies = [ "plotly>=5.24.1", # Install solvers "clarabel>=0.9.0" - ] [project.urls] diff --git a/src/cvxball/__init__.py b/src/cvx/ball/__init__.py similarity index 100% rename from src/cvxball/__init__.py rename to src/cvx/ball/__init__.py diff --git a/src/cvx/ball/utils/circle.py b/src/cvx/ball/utils/circle.py new file mode 100644 index 0000000..6c36373 --- /dev/null +++ b/src/cvx/ball/utils/circle.py @@ -0,0 +1,24 @@ +from dataclasses import dataclass + +import numpy as np +import plotly.graph_objects as go + + +@dataclass(frozen=True) +class Circle: + center: np.ndarray + radius: float + + def scatter(self, num=100, color="red"): + t = np.linspace(0, 2 * np.pi, num=num) + radius = self.radius + circle_x = self.center[0] + radius * np.cos(t) + circle_y = self.center[1] + radius * np.sin(t) + + return go.Scatter( + x=circle_x, + y=circle_y, + mode="lines", + line=dict(color=color, width=2), + name=f"Circle(r = {self.radius})", + ) diff --git a/src/cvx/ball/utils/cloud.py b/src/cvx/ball/utils/cloud.py new file mode 100644 index 0000000..110b89e --- /dev/null +++ b/src/cvx/ball/utils/cloud.py @@ -0,0 +1,17 @@ +from dataclasses import dataclass + +import numpy as np +import plotly.graph_objects as go + + +@dataclass(frozen=True) +class Cloud: + points: np.ndarray + + def scatter(self, size=10): + return go.Scatter( + x=self.points[:, 0], + y=self.points[:, 1], + mode="markers", + marker=dict(symbol="x", size=size, color="blue"), + ) diff --git a/src/cvx/ball/utils/figure.py b/src/cvx/ball/utils/figure.py new file mode 100644 index 0000000..06a8709 --- /dev/null +++ b/src/cvx/ball/utils/figure.py @@ -0,0 +1,18 @@ +import plotly.graph_objects as go + + +def create_figure(): + # Create the scatter plot + fig = go.Figure() + + # Update layout for equal aspect ratio and axis labels + fig.update_layout( + xaxis_title="x", + yaxis_title="y", + yaxis=dict( + scaleanchor="x", + scaleratio=1, + ), + ) + + return fig diff --git a/src/cvxball/add.py b/src/cvxball/add.py deleted file mode 100644 index e6eeeb1..0000000 --- a/src/cvxball/add.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright 2025 Stanford University Convex Optimization Group -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# Copyright 2023 Stanford University Convex Optimization Group -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -def add(x, y): - return x + y diff --git a/src/tests/conftest.py b/src/tests/conftest.py deleted file mode 100644 index a97d87b..0000000 --- a/src/tests/conftest.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2025 Stanford University Convex Optimization Group -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""global fixtures""" - -from __future__ import annotations - -from pathlib import Path - -import pytest - - -@pytest.fixture(scope="session", name="resource_dir") -def resource_fixture(): - """resource fixture""" - return Path(__file__).parent / "resources" diff --git a/src/tests/test_solver.py b/src/tests/test_solver.py new file mode 100644 index 0000000..63b1785 --- /dev/null +++ b/src/tests/test_solver.py @@ -0,0 +1,17 @@ +import numpy as np + +from cvx.ball.solver import min_circle_cvx +from cvx.ball.utils.cloud import Cloud +from cvx.ball.utils.figure import create_figure + + +def test_random(): + p = np.array([[2.0, 4.0], [0, 0], [2.5, 2.0]]) + cloud = Cloud(p) + circle = min_circle_cvx(p, solver="CLARABEL") + + fig = create_figure() + fig.add_trace(circle.scatter()) + fig.add_trace(cloud.scatter()) + + # fig.show() diff --git a/src/tests/test_trivial.py b/src/tests/test_trivial.py deleted file mode 100644 index 6c03a7d..0000000 --- a/src/tests/test_trivial.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2025 Stanford University Convex Optimization Group -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -from cvxball.add import add - - -def test_add(): - assert add(2, 3) == 5