Skip to content

Commit

Permalink
Merge pull request #490 from GAA-UAM/feature/renameBasis
Browse files Browse the repository at this point in the history
Feature/rename basis
  • Loading branch information
vnmabus committed Nov 15, 2022
2 parents 7b51542 + 966b335 commit aee15f4
Show file tree
Hide file tree
Showing 51 changed files with 1,045 additions and 444 deletions.
29 changes: 22 additions & 7 deletions docs/modules/representation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ The following classes are used to define different basis for
.. autosummary::
:toctree: autosummary

skfda.representation.basis.BSpline
skfda.representation.basis.Fourier
skfda.representation.basis.Monomial
skfda.representation.basis.Constant
skfda.representation.basis.BSplineBasis
skfda.representation.basis.FourierBasis
skfda.representation.basis.MonomialBasis
skfda.representation.basis.ConstantBasis

The following classes, allow the construction of a basis for
:math:`\mathbb{R}^n \to \mathbb{R}` functions.

.. autosummary::
:toctree: autosummary

skfda.representation.basis.Tensor
skfda.representation.basis.FiniteElement
skfda.representation.basis.TensorBasis
skfda.representation.basis.FiniteElementBasis

The following class, allows the construction of a basis for
:math:`\mathbb{R}^n \to \mathbb{R}^m` functions from
Expand All @@ -72,7 +72,7 @@ several :math:`\mathbb{R}^n \to \mathbb{R}` bases.
.. autosummary::
:toctree: autosummary

skfda.representation.basis.VectorValued
skfda.representation.basis.VectorValuedBasis

All the aforementioned basis inherit the basics from an
abstract base class :class:`Basis`. Users can create their own
Expand Down Expand Up @@ -107,3 +107,18 @@ interval using extrapolation methods.
:maxdepth: 4

representation/extrapolation

Deprecated Classes
----------------------

.. autosummary::
:toctree: autosummary

skfda.representation.basis.BSpline
skfda.representation.basis.Fourier
skfda.representation.basis.Monomial
skfda.representation.basis.Constant
skfda.representation.basis.Tensor
skfda.representation.basis.FiniteElement
skfda.representation.basis.VectorValued

64 changes: 34 additions & 30 deletions examples/plot_extrapolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

# sphinx_gallery_thumbnail_number = 2

import skfda

import mpl_toolkits.mplot3d

import matplotlib.pyplot as plt
import numpy as np

import skfda

##############################################################################
#
Expand All @@ -39,18 +36,25 @@
# To show how it works we will create a dataset with two unidimensional curves
# defined in (0,1), and we will represent it using a grid and different types
# of basis.
#

fdgrid = skfda.datasets.make_sinusoidal_process(
n_samples=2, error_std=0, random_state=0)
n_samples=2,
error_std=0,
random_state=0,
)
fdgrid.dataset_name = "Grid"

fd_fourier = fdgrid.to_basis(skfda.representation.basis.Fourier())
fd_fourier = fdgrid.to_basis(skfda.representation.basis.FourierBasis())
fd_fourier.dataset_name = "Fourier Basis"

fd_monomial = fdgrid.to_basis(skfda.representation.basis.Monomial(n_basis=5))
fd_monomial = fdgrid.to_basis(
skfda.representation.basis.MonomialBasis(n_basis=5),
)
fd_monomial.dataset_name = "Monomial Basis"

fd_bspline = fdgrid.to_basis(skfda.representation.basis.BSpline(n_basis=5))
fd_bspline = fdgrid.to_basis(
skfda.representation.basis.BSplineBasis(n_basis=5),
)
fd_bspline.dataset_name = "BSpline Basis"


Expand Down Expand Up @@ -79,18 +83,18 @@
# For this reason the behavior outside the domain will change depending on the
# representation, obtaining a periodic behavior in the case of the Fourier
# basis and polynomial behaviors in the rest of the cases.
#


domain_extended = (-0.2, 1.2)

fig, ax = plt.subplots(2, 2)


# Plot objects in the domain range extended
fdgrid.plot(ax[0][0], domain_range=domain_extended, linestyle='--')
fd_fourier.plot(ax[0][1], domain_range=domain_extended, linestyle='--')
fd_monomial.plot(ax[1][0], domain_range=domain_extended, linestyle='--')
fd_bspline.plot(ax[1][1], domain_range=domain_extended, linestyle='--')
fdgrid.plot(ax[0][0], domain_range=domain_extended, linestyle="--")
fd_fourier.plot(ax[0][1], domain_range=domain_extended, linestyle="--")
fd_monomial.plot(ax[1][0], domain_range=domain_extended, linestyle="--")
fd_bspline.plot(ax[1][1], domain_range=domain_extended, linestyle="--")

# Plot configuration
for axes in fig.axes:
Expand All @@ -117,7 +121,7 @@
# It should be noted that the Fourier basis is periodic in itself, but the
# period does not have to coincide with the domain range, obtaining different
# results applying or not extrapolation in case of not coinciding.
#

t = np.linspace(*domain_extended)

fig = plt.figure()
Expand All @@ -127,7 +131,7 @@
# Extrapolation supplied in the evaluation
values = fdgrid(t, extrapolation="periodic")[..., 0]

plt.plot(t, values.T, linestyle='--')
plt.plot(t, values.T, linestyle="--")

plt.gca().set_prop_cycle(None) # Reset color cycle

Expand All @@ -138,7 +142,7 @@
#
# Another possible extrapolation, ``"bounds"``, will use the values of the
# interval bounds for points outside the domain range.
#


fig = plt.figure()
fdgrid.dataset_name = "Boundary extrapolation"
Expand All @@ -148,7 +152,7 @@

# Evaluation of the grid
values = fdgrid(t)[..., 0]
plt.plot(t, values.T, linestyle='--')
plt.plot(t, values.T, linestyle="--")

plt.gca().set_prop_cycle(None) # Reset color cycle

Expand All @@ -161,15 +165,15 @@
# the points extrapolated with the same value. The case of filling with zeros
# could be specified with the string ``"zeros"``, which is equivalent to
# ``extrapolation=FillExtrapolation(0)``.
#


fdgrid.dataset_name = "Fill with zeros"

# Evaluation of the grid filling with zeros
fdgrid.extrapolation = "zeros"

# Plot in domain extended
fig = fdgrid.plot(domain_range=domain_extended, linestyle='--')
fig = fdgrid.plot(domain_range=domain_extended, linestyle="--")

plt.gca().set_prop_cycle(None) # Reset color cycle

Expand All @@ -179,7 +183,7 @@
##############################################################################
#
# The string ``"nan"`` is equivalent to ``FillExtrapolation(np.nan)``.
#


values = fdgrid([-1, 0, 0.5, 1, 2], extrapolation="nan")
print(values)
Expand All @@ -188,7 +192,7 @@
#
# It is possible to configure the extrapolation to raise an exception in case
# of evaluating a point outside the domain.
#


try:
res = fd_fourier(t, extrapolation="exception")
Expand All @@ -202,7 +206,7 @@
# using periodic extrapolation.

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax = fig.add_subplot(111, projection="3d")

# Make data.
t = np.arange(-2.5, 2.75, 0.25)
Expand All @@ -219,20 +223,20 @@
T, S = np.meshgrid(t, t)


ax.plot_wireframe(T, S, values[0, ..., 0], alpha=.3, color="C0")
ax.plot_wireframe(T, S, values[0, ..., 0], alpha=0.3, color="C0")
ax.plot_surface(X, Y, Z, color="C0")

###############################################################################
#
# The previous extension can be compared with the extrapolation using the values
# of the bounds.
# The previous extension can be compared with the extrapolation using the
# values of the bounds.


values = fd_surface((t, t), grid=True, extrapolation="bounds")

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(T, S, values[0, ..., 0], alpha=.3, color="C0")
ax = fig.add_subplot(111, projection="3d")
ax.plot_wireframe(T, S, values[0, ..., 0], alpha=0.3, color="C0")
ax.plot_surface(X, Y, Z, color="C0")

###############################################################################
Expand All @@ -243,6 +247,6 @@
values = fd_surface((t, t), grid=True, extrapolation="zeros")

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(T, S, values[0, ..., 0], alpha=.3, color="C0")
ax = fig.add_subplot(111, projection="3d")
ax.plot_wireframe(T, S, values[0, ..., 0], alpha=0.3, color="C0")
ax.plot_surface(X, Y, Z, color="C0")
16 changes: 10 additions & 6 deletions examples/plot_fpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from skfda.datasets import fetch_growth
from skfda.exploratory.visualization import FPCAPlot
from skfda.preprocessing.dim_reduction import FPCA
from skfda.representation.basis import BSpline, Fourier, Monomial
from skfda.representation.basis import (
BSplineBasis,
FourierBasis,
MonomialBasis,
)

##############################################################################
# In this example we are going to use functional principal component analysis
Expand Down Expand Up @@ -50,7 +54,7 @@
# We also plot the data for better visual representation.
dataset = fetch_growth()
fd = dataset['data']
basis = skfda.representation.basis.BSpline(n_basis=7)
basis = skfda.representation.basis.BSplineBasis(n_basis=7)
basis_fd = fd.to_basis(basis)
basis_fd.plot()

Expand Down Expand Up @@ -90,8 +94,8 @@
# basis
dataset = fetch_growth()
fd = dataset['data']
basis_fd = fd.to_basis(BSpline(n_basis=7))
fpca = FPCA(n_components=2, components_basis=Fourier(n_basis=7))
basis_fd = fd.to_basis(BSplineBasis(n_basis=7))
fpca = FPCA(n_components=2, components_basis=FourierBasis(n_basis=7))
fpca.fit(basis_fd)
fpca.components_.plot()

Expand All @@ -103,7 +107,7 @@
# principal components
dataset = fetch_growth()
fd = dataset['data']
basis_fd = fd.to_basis(BSpline(n_basis=7))
fpca = FPCA(n_components=2, components_basis=Monomial(n_basis=4))
basis_fd = fd.to_basis(BSplineBasis(n_basis=7))
fpca = FPCA(n_components=2, components_basis=MonomialBasis(n_basis=4))
fpca.fit(basis_fd)
fpca.components_.plot()
2 changes: 1 addition & 1 deletion examples/plot_kernel_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
# number of functions in the basis affects the estimation result and should
# ideally also be chosen using cross-validation.

fourier = skfda.representation.basis.Fourier(n_basis=10)
fourier = skfda.representation.basis.FourierBasis(n_basis=10)

X_basis = X.to_basis(basis=fourier)
X_basis_train, X_basis_test, y_train, y_test = train_test_split(
Expand Down
4 changes: 2 additions & 2 deletions examples/plot_neighbors_functional_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import skfda
from skfda.ml.regression import KNeighborsRegressor
from skfda.representation.basis import Fourier
from skfda.representation.basis import FourierBasis

##############################################################################
#
Expand Down Expand Up @@ -54,7 +54,7 @@
# to make a smoothing of the precipitation curves using a basis
# representation, employing for it a fourier basis with 5 elements.

y = y.to_basis(Fourier(n_basis=5))
y = y.to_basis(FourierBasis(n_basis=5))

y.plot()

Expand Down

0 comments on commit aee15f4

Please sign in to comment.