Source code for

# Copyright 2021 IRT Saint Exupéry,
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# Contributors:
#    INITIAL AUTHORS - initial API and implementation and/or initial
#                           documentation
#        :author: Matthias De Lozzo
r"""Draw a radar visualization from a :class:`.Dataset`.

The :class:`.Radar` class implements the Radviz plot,
which is a way to visualize :math:`n` samples of a multi-dimensional vector

.. math::


in a 2D referential and to highlight the separability of the data.

For that, each sample

.. math::


is rendered inside the unit disc
with the influences of the different parameters evenly distributed
on its circumference. Each parameter influence varies from 0 to 1
and can be interpreted compared to the others.

A variable name is required by the :meth:`.DatasetPlot.execute` method
by means of the ``classifier`` keyword in order to color the curves
according to the value of the variable name. This is useful when the data is
labeled or when we are looking for the samples for which the classifier value
is comprised in some interval specified by the ``lower`` and ``upper``
(default values are set to ``-inf`` and ``inf`` respectively).
In the latter case, the color scale is composed of only two values: one for
the samples positively classified and one for the others.
from __future__ import annotations

from matplotlib.axes import Axes
from matplotlib.figure import Figure
from pandas.plotting import radviz

from gemseo.datasets.dataset import Dataset
from import DatasetPlot

[docs]class Radar(DatasetPlot): """Radar visualization.""" def __init__( self, dataset: Dataset, classifier: str, ) -> None: """ Args: classifier: The name of the variable to group the data. """ # noqa: D205, D212, D415 super().__init__(dataset, classifier=classifier) def _plot( self, fig: None | Figure = None, axes: None | Axes = None, ) -> list[Figure]: classifier = self._param.classifier if classifier not in self.dataset.variable_names: raise ValueError( "Classifier must be one of these names: " + ", ".join(self.dataset.variable_names) ) dataframe = self.dataset label, _ = self._get_label(classifier) str_encoder = self.dataset.misc.get("labels", {}) if len(str_encoder): for variable, meaning in str_encoder.items(): data = self.dataset.get_view(variable_names=variable).to_numpy() self.dataset.update_data(meaning[data.ravel()], variable_names=variable) dataframe.columns = self._get_variable_names(dataframe) dataframe = dataframe.reindex(sorted(dataframe.columns), axis=1) fig, axes = self._get_figure_and_axes(fig, axes) radviz(dataframe, label, ax=axes) axes.set_xlabel(self.xlabel) axes.set_ylabel(self.ylabel) axes.set_title(self.title) return [fig]