-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python/ Convert examples into sphinx galleries.
- Loading branch information
1 parent
1756087
commit 46bffcb
Showing
13 changed files
with
199 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.. _auto_examples: | ||
|
||
Examples | ||
======== | ||
|
||
These are longer and more detailed examples than those accompanying the | ||
documentation of each function. These examples may require additional packages | ||
to run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
r"""Comparing methods for approximating the hypervolume | ||
=================================================== | ||
This example shows how to approximate the hypervolume metric of the ``CPFs.txt`` dataset using both HypE, :func:`moocore.whv_hype()`, and DZ2019, :func:`moocore.hv_approx()` for several | ||
values of the number of samples between :math:`10^1` and :math:`10^5`. We repeat each | ||
calculation 10 times to account for stochasticity. | ||
""" | ||
|
||
import numpy as np | ||
import moocore | ||
|
||
ref = 2.1 | ||
x = moocore.get_dataset("CPFs.txt")[:, :-1] | ||
x = moocore.filter_dominated(x) | ||
x = moocore.normalise(x, to_range=[1, 2]) | ||
true_hv = moocore.hypervolume(x, ref=ref) | ||
rng1 = np.random.default_rng(42) | ||
rng2 = np.random.default_rng(42) | ||
|
||
hype = {} | ||
dz = {} | ||
for i in range(1, 6): | ||
hype[i] = [] | ||
dz[i] = [] | ||
for r in range(15): | ||
res = moocore.whv_hype(x, ref=ref, ideal=0, nsamples=10**i, seed=rng1) | ||
hype[i].append(res) | ||
res = moocore.hv_approx(x, ref=ref, nsamples=10**i, seed=rng2) | ||
dz[i].append(res) | ||
print( | ||
f"True HV : {true_hv:.5f}", | ||
f"Mean HYPE : {np.mean(hype[5]):.5f} [{np.min(hype[5]):.5f}, {np.max(hype[5]):.5f}]", | ||
f"Mean DZ2019: {np.mean(dz[5]):.5f} [{np.min(dz[5]):.5f}, {np.max(dz[5]):.5f}]", | ||
sep="\n", | ||
) | ||
|
||
|
||
# %% | ||
# Next, we plot the results. | ||
|
||
import pandas as pd | ||
|
||
hype = pd.DataFrame(hype) | ||
dz = pd.DataFrame(dz) | ||
hype["Method"] = "HypE" | ||
dz["Method"] = "DZ2019" | ||
df = ( | ||
pd.concat([hype, dz]) | ||
.reset_index(names="rep") | ||
.melt(id_vars=["rep", "Method"], var_name="samples") | ||
) | ||
df["samples"] = 10 ** df["samples"] | ||
df["value"] = np.abs(df["value"] - true_hv) / true_hv | ||
|
||
import matplotlib.pyplot as plt | ||
import seaborn as sns | ||
|
||
ax = sns.lineplot(x="samples", y="value", hue="Method", data=df, marker="o") | ||
ax.set(xscale="log", yscale="log", ylabel="Relative error") | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Computing Multi-Objective Quality Metrics | ||
========================================= | ||
TODO: Expand this | ||
""" | ||
|
||
import numpy as np | ||
import moocore | ||
|
||
# %% | ||
# First, read the datasets. | ||
# | ||
|
||
spherical = moocore.get_dataset("spherical-250-10-3d.txt") | ||
uniform = moocore.get_dataset("uniform-250-10-3d.txt") | ||
|
||
ref = 1.1 | ||
ref_set = moocore.filter_dominated( | ||
np.vstack((spherical[:, :-1], uniform[:, :-1])) | ||
) | ||
|
||
|
||
def apply_within_sets(x, fun, **kwargs): | ||
"""Apply ``fun`` for each dataset in ``x``.""" | ||
_, uniq_index = np.unique(x[:, -1], return_index=True) | ||
x_split = np.vsplit(x[:, :-1], uniq_index[1:]) | ||
return [fun(g, **kwargs) for g in x_split] | ||
|
||
|
||
uniform_igd_plus = apply_within_sets(uniform, moocore.igd_plus, ref=ref_set) | ||
spherical_igd_plus = apply_within_sets( | ||
spherical, moocore.igd_plus, ref=ref_set | ||
) | ||
|
||
print(f""" | ||
Uniform Spherical | ||
------- --------- | ||
Mean IGD+: {np.mean(uniform_igd_plus):.5f} {np.mean(spherical_igd_plus):.5f} | ||
""") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters