Skip to content

Commit 8d1fd01

Browse files
authored
【PPSCI Doc No.61-65】 (#839)
* fix doc bugs * fix codestyle bugs
1 parent 07b440f commit 8d1fd01

File tree

1 file changed

+125
-6
lines changed

1 file changed

+125
-6
lines changed

ppsci/geometry/pointcloud.py

Lines changed: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,78 @@ def on_boundary(self, x):
102102
.any(axis=1)
103103
)
104104

105-
def translate(self, translation):
105+
def translate(self, translation: np.ndarray) -> "PointCloud":
106+
"""
107+
Translate the geometry by the given offset.
108+
109+
Args:
110+
translation (np.ndarray): Translation offset.The shape of translation must be the same as the shape of the interior points.
111+
112+
Returns:
113+
PointCloud: Translated point cloud.
114+
115+
Examples:
116+
>>> import ppsci
117+
>>> import numpy as np
118+
>>> interior_points = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
119+
>>> geom = ppsci.geometry.PointCloud(interior_points, ("x",))
120+
>>> translation = np.array([1.0])
121+
>>> print(geom.translate(translation).interior)
122+
[[1. ]
123+
[1.5]
124+
[2. ]
125+
[2.5]
126+
[3. ]]
127+
>>> interior_points_2d = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1)),
128+
... "y": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
129+
>>> geom_2d = ppsci.geometry.PointCloud(interior_points_2d, ("x", "y"))
130+
>>> translation_2d = np.array([1.0, 3.0])
131+
>>> print(geom_2d.translate(translation_2d).interior)
132+
[[1. 3. ]
133+
[1.5 3.5]
134+
[2. 4. ]
135+
[2.5 4.5]
136+
[3. 5. ]]
137+
"""
106138
for i, offset in enumerate(translation):
107139
self.interior[:, i] += offset
108140
if self.boundary:
109141
self.boundary += offset
110142
return self
111143

112-
def scale(self, scale):
144+
def scale(self, scale: np.ndarray) -> "PointCloud":
145+
"""
146+
Scale the geometry by the given factor.
147+
148+
Args:
149+
scale (np.ndarray): Scale factor.The shape of scale must be the same as the shape of the interior points.
150+
151+
Returns:
152+
PointCloud: Scaled point cloud.
153+
154+
Examples:
155+
>>> import ppsci
156+
>>> import numpy as np
157+
>>> interior_points = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
158+
>>> geom = ppsci.geometry.PointCloud(interior_points, ("x",))
159+
>>> scale = np.array([2.0])
160+
>>> print(geom.scale(scale).interior)
161+
[[0.]
162+
[1.]
163+
[2.]
164+
[3.]
165+
[4.]]
166+
>>> interior_points_2d = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1)),
167+
... "y": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
168+
>>> geom_2d = ppsci.geometry.PointCloud(interior_points_2d, ("x", "y"))
169+
>>> scale_2d = np.array([2.0, 0.5])
170+
>>> print(geom_2d.scale(scale_2d).interior)
171+
[[0. 0. ]
172+
[1. 0.25]
173+
[2. 0.5 ]
174+
[3. 0.75]
175+
[4. 1. ]]
176+
"""
113177
for i, _scale in enumerate(scale):
114178
self.interior[:, i] *= _scale
115179
if self.boundary:
@@ -124,7 +188,26 @@ def uniform_boundary_points(self, n: int):
124188
"PointCloud do not have 'uniform_boundary_points' method"
125189
)
126190

127-
def random_boundary_points(self, n, random="pseudo"):
191+
def random_boundary_points(self, n: int, random: str = "pseudo") -> np.ndarray:
192+
"""Randomly sample points on the boundary.
193+
194+
Args:
195+
n (int): Number of sample points.
196+
random (str): Random method. Defaults to "pseudo".
197+
198+
Returns:
199+
np.ndarray: Randomly sampled points on the boundary.The shape of the returned array is (n, ndim).
200+
201+
Examples:
202+
>>> import ppsci
203+
>>> import numpy as np
204+
>>> np.random.seed(0)
205+
>>> interior_points = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
206+
>>> boundary_points = {"x": np.array([0.0, 2.0], dtype="float32").reshape((-1, 1))}
207+
>>> geom = ppsci.geometry.PointCloud(interior_points, ("x",), boundary_points)
208+
>>> print(geom.random_boundary_points(1))
209+
[[2.]]
210+
"""
128211
assert self.boundary is not None, (
129212
"boundary points can't be empty when call "
130213
"'random_boundary_points' method"
@@ -137,7 +220,26 @@ def random_boundary_points(self, n, random="pseudo"):
137220
np.random.choice(len(self.boundary), size=n, replace=False)
138221
]
139222

140-
def random_points(self, n, random="pseudo"):
223+
def random_points(self, n: int, random: str = "pseudo") -> np.ndarray:
224+
"""Randomly sample points in the geometry.
225+
226+
Args:
227+
n (int): Number of sample points.
228+
random (str): Random method. Defaults to "pseudo".
229+
230+
Returns:
231+
np.ndarray: Randomly sampled points in the geometry.The shape of the returned array is (n, ndim).
232+
233+
Examples:
234+
>>> import ppsci
235+
>>> import numpy as np
236+
>>> np.random.seed(0)
237+
>>> interior_points = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
238+
>>> geom = ppsci.geometry.PointCloud(interior_points, ("x",))
239+
>>> print(geom.random_points(2))
240+
[[1.]
241+
[0.]]
242+
"""
141243
assert n <= len(self.interior), (
142244
f"number of sample points({n}) "
143245
f"can't be more than that in points({len(self.interior)})"
@@ -146,8 +248,25 @@ def random_points(self, n, random="pseudo"):
146248
np.random.choice(len(self.interior), size=n, replace=False)
147249
]
148250

149-
def uniform_points(self, n: int, boundary=True):
150-
"""Compute the equi-spaced points in the geometry."""
251+
def uniform_points(self, n: int, boundary: bool = True) -> np.ndarray:
252+
"""Compute the equi-spaced points in the geometry.
253+
254+
Args:
255+
n (int): Number of sample points.
256+
boundary (bool): Whether to include boundary points. Defaults to True.
257+
258+
Returns:
259+
np.ndarray: Equi-spaced points in the geometry.The shape of the returned array is (n, ndim).
260+
261+
Examples:
262+
>>> import ppsci
263+
>>> import numpy as np
264+
>>> interior_points = {"x": np.linspace(0, 2, 5, dtype="float32").reshape((-1, 1))}
265+
>>> geom = ppsci.geometry.PointCloud(interior_points, ("x",))
266+
>>> print(geom.uniform_points(2))
267+
[[0. ]
268+
[0.5]]
269+
"""
151270
return self.interior[:n]
152271

153272
def union(self, other):

0 commit comments

Comments
 (0)