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 problem with extra dimension that makes linear interpolation failing #247

Merged
merged 5 commits into from
Nov 15, 2023

Conversation

giovastabile
Copy link
Contributor

Not sure it this is the best fix. In case one is using linear interpolation there is one extra dimension that makes the linear interpolation failing (e.g. one can try tutorial 2 with Linear interpolation). This fix solves the problem

@giovastabile giovastabile requested a review from ndem0 November 15, 2023 11:37
@flabowski
Copy link
Contributor

Do you have a minimal working example of the problem? Not sure if a change in the pod module because of a problem with the linear interpolation module is the best fix.
In case of trouble with linear interpolation in 1D, you might want to consider the regular_grid module. The 1D linear interpolation used in the linear module is considered legacy (see scipy docs).

@giovastabile
Copy link
Contributor Author

I also think it's not the best fix. I have an example reproducing the problem. I pushed it here:

tutorials/tutorial-2.py

@mtezzele
Copy link
Contributor

@giovastabile is the problem still there if you use rom.predict(np.array([20])) ?

@mtezzele
Copy link
Contributor

@ndem0 we should replace interp1d with np.interp

@giovastabile
Copy link
Contributor Author

yes because the problem is not in the shape of the parameters but in the shape of the pod modes. The error occurs while doing:

`File ~/.local/lib/python3.10/site-packages/ezyrb/pod.py:124, in POD.inverse_transform(self, X)
118 def inverse_transform(self, X):
119 """
120 Projects a reduced to full order solution.
121
122 :type: numpy.ndarray
123 """
--> 124 return self.modes.dot(X)

ValueError: shapes (1639,500) and (500,1,1) not aligned: 500 (dim 1) != 1 (dim 1)`

@giovastabile
Copy link
Contributor Author

X should not be 500,1,1 but 500,1

I didn't investigate why this happens only with Linear interpolation

@mtezzele
Copy link
Contributor

@giovastabile are you using squeeze?
If I just use
pred = rom.predict(np.array([20]))
it works with the current version of EZyRB.
Anyway we can not accept this fix because the problem is only in the Linear class and this fix breaks other combinations

@giovastabile
Copy link
Contributor Author

Yes, I assume the fix should be in the Linear class. The predict call does not work in case you use a linear interpolation. with RBF everything is fine

@mtezzele
Copy link
Contributor

This minimal example works on my machine with the current ezyrb version (even using predict(20)):

from ezyrb import Database
from ezyrb import POD
from ezyrb import Linear
from ezyrb import ReducedOrderModel as ROM
import numpy as np

db = Database(data.params, data.snapshots['vx'])
rom = ROM(db, POD(), Linear())
rom.fit()
pred = rom.predict(np.array([20]))
print(f'pred = {pred}')
print(f'pred.shape = {pred.shape}')

the output is

Shape of vx      snapshots matrix: (500, 1639)
Shape of vy      snapshots matrix: (500, 1639)
Shape of p       snapshots matrix: (500, 1639)
Shape of mag(v)  snapshots matrix: (500, 1639)
Shape of parameters matrix: (500, 1)
pred = [2.97702136e-13 4.08038857e-14 5.17295160e-15 ... 4.24164446e+00
 1.03359498e+01 2.86579912e+00]
pred.shape = (1639,)

to replicate your error I have to pass pred = rom.predict(np.array([[20]]))

@giovastabile
Copy link
Contributor Author

giovastabile commented Nov 15, 2023

This minimal example does not work in my case:

from ezyrb import Database
from ezyrb import POD
from ezyrb import Linear
from ezyrb import ReducedOrderModel as ROM
import numpy as np

from smithers.dataset import NavierStokesDataset
data = NavierStokesDataset()

db = Database(data.params, data.snapshots['vx'])
rom = ROM(db, POD(), Linear())
rom.fit()
pred = rom.predict(np.array([20]))
print(f'pred = {pred}')
print(f'pred.shape = {pred.shape}')

With the latest fix I did, it works fine

@ndem0
Copy link
Member

ndem0 commented Nov 15, 2023

I'm not 100% sure I got the problem: have you tried to run the tests on your machine? The example you provided looks already checked in this test

def test_with_db_predict(self):

EDIT: you're right, the example fails also on my machine, but I still have to figured out the reason.

Copy link
Member

@ndem0 ndem0 left a comment

Choose a reason for hiding this comment

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

We were checking the linear interpolation only for 1D input with 1D output, or ND input with ND output. Your case (1D-ND) was never tested before!
Now everything works, I also added a small test to check this behavior in future. Thanks @giovastabile!

@ndem0 ndem0 merged commit 677794b into mathLab:master Nov 15, 2023
7 checks passed
@giovastabile giovastabile deleted the development branch November 15, 2023 16:27
@flabowski
Copy link
Contributor

flabowski commented Nov 15, 2023

You could also do a linear interpolation (or a higher order interpolation) using the RegularGrid interpolator:

from ezyrb import Database
from ezyrb import POD
from ezyrb import Linear, RegularGrid
from ezyrb import ReducedOrderModel as ROM
import numpy as np

from smithers.dataset import NavierStokesDataset
data = NavierStokesDataset()

db = Database(data.params, data.snapshots['vx'])
rom = ROM(db, POD(), RegularGrid())
rom.fit()
pred = rom.predict(np.array([[20]]))
print(f'pred = {pred}')
print(f'pred.shape = {pred.shape}')

pred = rom.predict(np.array([20]))
print(f'pred = {pred}')
print(f'pred.shape = {pred.shape}')

flabowski pushed a commit to flabowski/EZyRB that referenced this pull request Aug 28, 2024
…ing (mathLab#247)

* FIX problem with extra dimension that makes linear interpolation failing
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.

4 participants