Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix IndexError in FPCAPlot #554

Merged
merged 1 commit into from
Jul 6, 2023
Merged

fix IndexError in FPCAPlot #554

merged 1 commit into from
Jul 6, 2023

Conversation

Quentin62
Copy link
Contributor

FPCAplot returns an IndexError when the number of components to plot is not equal to the number of created axes.

In the following, I plot 3 components, FPCAplot has created a 2*2 subplots and can not allocate a plot for the last axe and throws an IndexError

import matplotlib.pyplot as plt
from skfda.datasets import fetch_weather
from skfda.exploratory.visualization import FPCAPlot
from skfda.preprocessing.dim_reduction.feature_extraction import FPCA
from skfda.representation.grid import FDataGrid
from skfda.representation.basis import BSpline

# create a FDataGrid with temperature from CanadianWeather
dataset = fetch_weather()
data_temp = FDataGrid(data_matrix=dataset["data"].data_matrix[:, :, 0], grid_points=dataset["data"].grid_points)

# basis expansion
basis_10 = BSpline(domain_range=data_temp.domain_range, n_basis=10, order=4)
temp_fd  = data_temp.to_basis(basis_10)

# fpca
fpca = FPCA(n_components=3)
fpca.fit(temp_fd)

FPCAPlot(temp_fd.mean(), fpca.components_, factor=100).plot()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[38], line 1
----> 1 FPCAPlot(temp_smooth_10.mean(), fpca.components_, factor=100).plot()

File [~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/_baseplot.py:98](https://file+.vscode-resource.vscode-cdn.net/home/quentin/Documents/Pr%C3%A9sentation/fda/~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/_baseplot.py:98), in BasePlot.plot(self)
     95 if self.y_label is not None:
     96     axes[0].set_ylabel(self.y_label)
---> 98 self._plot(fig, axes)
    100 self._hover_event_id = fig.canvas.mpl_connect(
    101     'motion_notify_event',
    102     self.hover,
    103 )
    105 return fig

File [~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/fpca.py:87](https://file+.vscode-resource.vscode-cdn.net/home/quentin/Documents/Pr%C3%A9sentation/fda/~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/fpca.py:87), in FPCAPlot._plot(self, fig, axes)
     84     self.mean = self.mean.mean()
     86 for i, ax in enumerate(axes):
---> 87     perturbations = self._get_component_perturbations(i)
     88     GraphPlot(fdata=perturbations, axes=ax).plot()
     89     ax.set_title(f"Principal component {i + 1}")

File [~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/fpca.py:107](https://file+.vscode-resource.vscode-cdn.net/home/quentin/Documents/Pr%C3%A9sentation/fda/~/.local/lib/python3.10/site-packages/skfda/exploratory/visualization/fpca.py:107), in FPCAPlot._get_component_perturbations(self, index)
    104     raise AttributeError("X must be a FData object")
    105 perturbations = self.mean.copy()
...
--> 572         raise IndexError("index out of bounds")
    574     return slice(key, key + 1)
    576 return key

IndexError: index out of bounds

I add an if statement in the loop to compare the current enumerate value with the number of plots

@codecov
Copy link

codecov bot commented Jul 5, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.01 ⚠️

Comparison is base (48fb0ad) 85.86% compared to head (4a56dd7) 85.85%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #554      +/-   ##
===========================================
- Coverage    85.86%   85.85%   -0.01%     
===========================================
  Files          147      147              
  Lines        11649    11650       +1     
===========================================
  Hits         10002    10002              
- Misses        1647     1648       +1     
Impacted Files Coverage Δ
skfda/exploratory/visualization/fpca.py 42.10% <0.00%> (-1.14%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@vnmabus vnmabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix! I should probably do a refactor of the visualization code in the future, as it is a bit messy right now.

@vnmabus vnmabus merged commit 415796b into GAA-UAM:develop Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants