Commit 99ba740e authored by Pamphile Roy's avatar Pamphile Roy

MST criteria with 2D plot

parent 5fc00849
Pipeline #26674773 passed with stages
in 34 minutes and 52 seconds
......@@ -24,6 +24,9 @@ import itertools
import numpy as np
import pandas as pd
from scipy.optimize import differential_evolution
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
from sklearn import preprocessing
from .sampling import Doe
from .sample import Sample
......@@ -228,7 +231,7 @@ class Space(Sample):
theoretical space, unit cube. Thus min and max values of the sample
will coincide with the bounds. ([min, k_vars], [max, k_vars]).
:param str method: Type of discrepancy. ['CD', 'WD', 'MD'].
:return: Centered discrepancy.
:return: Discrepancy.
:rtype: float.
"""
if bounds is not None:
......@@ -281,6 +284,38 @@ class Space(Sample):
return disc
@staticmethod
def mst(sample, fname=None, plot=True):
"""Minimum Spanning Tree.
MST is used here as a discrepancy criterion.
Comparing two different designs: the higher the mean,
the better the design is in terms of space filling.
:param array_like sample: The sample to compute the discrepancy from
(n_samples, k_vars).
:param str fname: wether to export to filename or display the figures.
:return: Mean, standard deviation and edges of the MST.
:rtypes: float, float, array_like (n_edges, 2 nodes indices).
"""
sample = np.asarray(sample)
dist = cdist(sample, sample)
mst = minimum_spanning_tree(dist)
edges = np.where(mst.toarray() > 0)
edges = np.array(edges).T
if (sample.shape[1] == 2) and plot:
fig, ax = plt.subplots()
for edge in edges:
ax.plot(sample[edge, 0], sample[edge, 1], c='k')
ax.scatter(sample[:, 0], sample[:, 1])
visualization.save_show(fname, [fig])
return mst.data.mean(), mst.data.std(), edges
def _cheap_doe_from_expensive(self, n):
"""Compute the number of points required for the cheap DOE.
......
# coding: utf8
import copy
import os
import pytest
import numpy as np
import numpy.testing as npt
......@@ -379,3 +380,12 @@ def test_discrepancy():
assert Space.discrepancy(space, method='MD') == pytest.approx(2.5000, abs=1e-4)
assert Space.discrepancy(space, method='WD') == pytest.approx(1.3680, abs=1e-4)
assert Space.discrepancy(space, method='CD') == pytest.approx(0.3172, abs=1e-4)
def test_mst(tmp):
sample = np.array([[0.25, 0.5], [0.6, 0.4], [0.7, 0.2]])
mean, std, edges = Space.mst(sample, fname=os.path.join(tmp, 'mst.pdf'))
assert mean == pytest.approx(0.2938, abs=1e-4)
assert std == pytest.approx(0.0702, abs=1e-4)
npt.assert_equal(edges, [[0, 1], [1, 2]])
......@@ -220,6 +220,8 @@ class TestKiviat:
kiviat = Kiviat3D(sample, functional_data, stack_order='qoi', cbar_order='hdr')
kiviat = Kiviat3D(sample, functional_data, stack_order='hdr', cbar_order='qoi')
kiviat = Kiviat3D(sample, functional_data, stack_order=1, cbar_order='hdr')
kiviat = Kiviat3D(sample, functional_data, idx=1, cbar_order='hdr',
range_cbar=[0, 1])
kiviat.plot(fname=os.path.join(tmp, 'kiviat.pdf'))
@pytest.mark.skipif(not have_ffmpeg, reason='ffmpeg not available')
......@@ -265,7 +267,6 @@ class TestPdf:
@patch("matplotlib.pyplot.show")
def test_pdf_surrogate(self, mock_show, ishigami_data):
dist = ot.ComposedDistribution(ishigami_data.dists)
space = np.array(ishigami_data.space)
surrogate = SurrogateModel('rbf', ishigami_data.space.corners,
ishigami_data.space.plabels)
surrogate.fit(ishigami_data.space, ishigami_data.target_space)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment