Skip to content

Commit 5052433

Browse files
committed
python/ Improve docs.
1 parent a53c8e1 commit 5052433

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

python/doc/source/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
napoleon_include_init_with_doc = True
6464
napoleon_use_param = True
6565
napoleon_type_aliases = {
66-
"array_like": ":term:`array_like`",
66+
"numpy.typing.ArrayLike": ":py:data:`~numpy.typing.ArrayLike`",
67+
"ArrayLike": ":py:data:`~numpy.typing.ArrayLike`",
6768
}
6869

6970
bibtex_bibfiles = ["REFERENCES.bib"]
@@ -78,9 +79,10 @@
7879
typehints_use_rtype = False
7980
typehints_defaults = "comma"
8081
autodoc_type_aliases = {
81-
"ArrayLike": "ArrayLike",
82+
"ArrayLike": ":py:data:`~numpy.typing.ArrayLike`",
8283
}
8384

85+
# nitpicky = True
8486

8587
# FIXME: This is not working!
8688
logger = sphinx.util.logging.getLogger("sphinx.ext.autodoc")

python/src/moocore/_moocore.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ def __init__(self, error_code):
4949
super().__init__(self.message)
5050

5151

52-
def read_datasets(filename, /):
52+
def read_datasets(filename: str | os.PathLike | StringIO, /) -> np.ndarray:
5353
"""Read an input dataset file, parsing the file and returning a numpy array.
5454
5555
Parameters
5656
----------
57-
filename : file, str, pathlib.Path
58-
Filename of the dataset file. Each row of the table appears as one line of the file. Datasets are separated by an empty line.
59-
If it does not contain an absolute path, the file name is relative to the current working directory.
60-
If the filename has extension `'.xz'`, it is decompressed to a temporary file before reading it.
57+
filename:
58+
Filename of the dataset file or :class:`~io.StringIO` directly containing the file contents.
59+
If it does not contain an absolute path, the filename is relative to the current working directory.
60+
If the filename has extension ``.xz``, it is decompressed to a temporary file before reading it.
61+
Each line of the file corresponds to one point of one dataset. Different datasets are separated by an empty line.
6162
6263
Returns
6364
-------
64-
numpy.ndarray
6565
An array containing a representation of the data in the file.
6666
The first :math:`n-1` columns contain the numerical data for each of the objectives.
6767
The last column contains an identifier for which set the data is relevant to.
@@ -170,6 +170,10 @@ def igd(data, /, ref, *, maximise=False) -> float:
170170
171171
.. seealso:: For details about parameters, return value and examples, see :func:`igd_plus`. For details of the calculation, see :ref:`igd_hausdorf`.
172172
173+
Returns
174+
-------
175+
A single numerical value
176+
173177
"""
174178
data_p, nobj, npoints, ref_p, ref_size, maximise_p = _unary_refset_common(
175179
data, ref, maximise
@@ -232,6 +236,10 @@ def avg_hausdorff_dist(data, /, ref, *, maximise=False, p: float = 1) -> float:
232236
p :
233237
Hausdorff distance parameter. Must be larger than 0.
234238
239+
Returns
240+
-------
241+
A single numerical value
242+
235243
"""
236244
if p <= 0:
237245
raise ValueError("'p' must be larger than zero")
@@ -245,7 +253,9 @@ def avg_hausdorff_dist(data, /, ref, *, maximise=False, p: float = 1) -> float:
245253
)
246254

247255

248-
def epsilon_additive(data, /, ref, *, maximise=False) -> float:
256+
def epsilon_additive(
257+
data: ArrayLike, /, ref: ArrayLike, *, maximise: bool | list[bool] = False
258+
) -> float:
249259
"""Additive epsilon metric.
250260
251261
`data` and `reference` must all be larger than 0 for :func:`epsilon_mult`.
@@ -254,13 +264,13 @@ def epsilon_additive(data, /, ref, *, maximise=False) -> float:
254264
255265
Parameters
256266
----------
257-
data : numpy.ndarray
267+
data :
258268
Numpy array of numerical values, where each row gives the coordinates of a point in objective space.
259269
If the array is created from the :func:`read_datasets` function, remove the last (set) column
260-
ref : numpy.ndarray or list
270+
ref :
261271
Reference point set as a numpy array or list. Must have same number of columns as a single point in the \
262272
dataset
263-
maximise : bool or list of bool
273+
maximise :
264274
Whether the objectives must be maximised instead of minimised. \
265275
Either a single boolean value that applies to all objectives or a list of booleans, with one value per objective. \
266276
Also accepts a 1d numpy array with value 0/1 for each objective
@@ -291,7 +301,11 @@ def epsilon_additive(data, /, ref, *, maximise=False) -> float:
291301
def epsilon_mult(data, /, ref, *, maximise=False) -> float:
292302
"""Multiplicative epsilon metric.
293303
294-
.. seealso:: For details about parameters, return value and examples, see :func:`epsilon_add`. For details of the calculation, see :ref:`epsilon_metric`.
304+
.. seealso:: For details about parameters, return value and examples, see :func:`epsilon_additive`. For details of the calculation, see :ref:`epsilon_metric`.
305+
306+
Returns
307+
-------
308+
A single numerical value
295309
296310
"""
297311
data_p, nobj, npoints, ref_p, ref_size, maximise_p = _unary_refset_common(
@@ -301,7 +315,7 @@ def epsilon_mult(data, /, ref, *, maximise=False) -> float:
301315

302316

303317
# FIXME: TODO maximise option
304-
def hypervolume(data: ArrayLike, /, ref) -> float:
318+
def hypervolume(data: ArrayLike, /, ref: ArrayLike) -> float:
305319
r"""Hypervolume indicator.
306320
307321
Compute the hypervolume metric with respect to a given reference point
@@ -318,7 +332,7 @@ def hypervolume(data: ArrayLike, /, ref) -> float:
318332
data :
319333
Numpy array of numerical values, where each row gives the coordinates of a point.
320334
If the array is created from the :func:`read_datasets` function, remove the last column.
321-
ref : ArrayLike or list
335+
ref :
322336
Reference point as a 1D vector. Must be same length as a single point in the `data`.
323337
324338
Returns
@@ -371,19 +385,21 @@ def hypervolume(data: ArrayLike, /, ref) -> float:
371385
return hv
372386

373387

374-
def is_nondominated(data, maximise=False, keep_weakly: bool = False):
388+
def is_nondominated(
389+
data: ArrayLike, /, *, maximise=False, keep_weakly: bool = False
390+
):
375391
"""Identify dominated points according to Pareto optimality.
376392
377393
Parameters
378394
----------
379-
data : numpy array
380-
Numpy array of numerical values, where each row gives the coordinates of a point in objective space.
395+
data :
396+
Array of numerical values, where each row gives the coordinates of a point in objective space.
381397
If the array is created from the :func:`read_datasets()` function, remove the last column.
382398
maximise : single bool, or list of booleans
383399
Whether the objectives must be maximised instead of minimised.
384400
Either a single boolean value that applies to all objectives or a list of boolean values, with one value per objective.
385401
Also accepts a 1D numpy array with value 0/1 for each objective.
386-
keep_weakly: bool
402+
keep_weakly:
387403
If ``False``, return ``False`` for any duplicates of nondominated points.
388404
389405
Returns
@@ -426,16 +442,16 @@ def is_nondominated(data, maximise=False, keep_weakly: bool = False):
426442
return np.frombuffer(nondom, dtype=bool)
427443

428444

429-
def filter_dominated(data, /, *, maximise=False, keep_weakly=False):
445+
def filter_dominated(data, /, *, maximise=False, keep_weakly: bool = False):
430446
"""Remove dominated points according to Pareto optimality.
431447
432-
See: :func:`is_nondominated` for details
448+
See: :func:`is_nondominated` for details.
433449
"""
434450
return data[is_nondominated(data, maximise, keep_weakly), :]
435451

436452

437453
def filter_dominated_within_sets(
438-
data, /, *, maximise=False, keep_weakly=False
454+
data, /, *, maximise=False, keep_weakly: bool = False
439455
):
440456
"""Given a dataset with multiple sets (last column gives the set index), filter dominated points within each set.
441457
@@ -452,7 +468,7 @@ def filter_dominated_within_sets(
452468
Whether the objectives must be maximised instead of minimised. \
453469
Either a single boolean value that applies to all objectives or a list of booleans, with one value per objective. \
454470
Also accepts a 1D numpy array with values 0 or 1 for each objective
455-
keep_weakly: bool
471+
keep_weakly:
456472
If False, return False for any duplicates of nondominated points.
457473
458474
Returns
@@ -504,19 +520,21 @@ def filter_dominated_within_sets(
504520
return data[is_nondom, :]
505521

506522

507-
def pareto_rank(data, /, *, maximise=False):
508-
r"""Ranks points according to Pareto-optimality, which is also called nondominated sorting.
523+
def pareto_rank(data: ArrayLike, /, *, maximise=False):
524+
r"""Rank points according to Pareto-optimality (nondominated sorting).
509525
510-
Ranks points according to Pareto-optimality, which is also called nondominated sorting :footcite:p:`Deb02nsga2`.
526+
The function :func:`pareto_rank` is meant to be used like
527+
:func:`numpy.argsort`, but it assigns indexes according to Pareto
528+
dominance. Duplicated points are kept on the same front. The resulting
529+
ranking can be used to partition points into different lists or arrays,
530+
each of them being mutually nondominated :footcite:p:`Deb02nsga2`.
511531
512-
`pareto_rank` is meant to be used like :func:`numpy.argsort`, but it assigns
513-
indexes according to Pareto dominance. Duplicated points are kept on the
514-
same front. With 2 columns, the code uses the :math:`O(n \log n)` algorithm
515-
by :footcite:t:`Jen03`.
532+
With 2 columns, the code uses the :math:`O(n \log n)` algorithm by
533+
:footcite:t:`Jen03`.
516534
517535
Parameters
518536
----------
519-
data : numpy array
537+
data :
520538
Numpy array of numerical values, where each row gives the coordinates of a point in objective space.
521539
If the array is created from the :func:`read_datasets()` function, remove the last column.
522540
maximise : single bool, or list of booleans

0 commit comments

Comments
 (0)