Skip to content

Commit

Permalink
🎨 Format Python code with psf/black (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Sep 27, 2024
1 parent 4c5cb8f commit f812d87
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pina/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
from .base_no import KernelNeuralOperator
from .avno import AveragingNeuralOperator
from .lno import LowRankNeuralOperator
from .spline import Spline
from .spline import Spline
110 changes: 55 additions & 55 deletions pina/model/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import torch
import torch.nn as nn
from ..utils import check_consistency



class Spline(torch.nn.Module):

def __init__(self, order=4, knots=None, control_points=None) -> None:
Expand Down Expand Up @@ -31,38 +32,37 @@ def __init__(self, order=4, knots=None, control_points=None) -> None:
self.control_points = control_points

elif knots is not None:
print('Warning: control points will be initialized automatically.')
print(' experimental feature')
print("Warning: control points will be initialized automatically.")
print(" experimental feature")

self.knots = knots
n = len(knots) - order
self.control_points = torch.nn.Parameter(
torch.zeros(n), requires_grad=True)

torch.zeros(n), requires_grad=True
)

elif control_points is not None:
print('Warning: knots will be initialized automatically.')
print(' experimental feature')
print("Warning: knots will be initialized automatically.")
print(" experimental feature")

self.control_points = control_points

n = len(self.control_points)-1
n = len(self.control_points) - 1
self.knots = {
'type': 'auto',
'min': 0,
'max': 1,
'n': n+2+self.order}
"type": "auto",
"min": 0,
"max": 1,
"n": n + 2 + self.order,
}

else:
raise ValueError(
"Knots and control points cannot be both None."
)

raise ValueError("Knots and control points cannot be both None.")

if self.knots.ndim != 1:
raise ValueError("Knot vector must be one-dimensional.")

def basis(self, x, k, i, t):
'''
"""
Recursive function to compute the basis functions of the spline.
:param torch.Tensor x: points to be evaluated.
Expand All @@ -71,28 +71,32 @@ def basis(self, x, k, i, t):
:param torch.Tensor t: vector of knots
:return: the basis functions evaluated at x
:rtype: torch.Tensor
'''
"""

if k == 0:
a = torch.where(torch.logical_and(t[i] <= x, x < t[i+1]), 1.0, 0.0)
a = torch.where(
torch.logical_and(t[i] <= x, x < t[i + 1]), 1.0, 0.0
)
if i == len(t) - self.order - 1:
a = torch.where(x == t[-1], 1.0, a)
a = torch.where(x == t[-1], 1.0, a)
a.requires_grad_(True)
return a


if t[i+k] == t[i]:
c1 = torch.tensor([0.0]*len(x), requires_grad=True)
if t[i + k] == t[i]:
c1 = torch.tensor([0.0] * len(x), requires_grad=True)
else:
c1 = (x - t[i])/(t[i+k] - t[i]) * self.basis(x, k-1, i, t)
c1 = (x - t[i]) / (t[i + k] - t[i]) * self.basis(x, k - 1, i, t)

if t[i+k+1] == t[i+1]:
c2 = torch.tensor([0.0]*len(x), requires_grad=True)
if t[i + k + 1] == t[i + 1]:
c2 = torch.tensor([0.0] * len(x), requires_grad=True)
else:
c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * self.basis(x, k-1, i+1, t)
c2 = (
(t[i + k + 1] - x)
/ (t[i + k + 1] - t[i + 1])
* self.basis(x, k - 1, i + 1, t)
)

return c1 + c2


@property
def control_points(self):
Expand All @@ -101,50 +105,46 @@ def control_points(self):
@control_points.setter
def control_points(self, value):
if isinstance(value, dict):
if 'n' not in value:
raise ValueError('Invalid value for control_points')
n = value['n']
dim = value.get('dim', 1)
if "n" not in value:
raise ValueError("Invalid value for control_points")
n = value["n"]
dim = value.get("dim", 1)
value = torch.zeros(n, dim)

if not isinstance(value, torch.Tensor):
raise ValueError('Invalid value for control_points')
raise ValueError("Invalid value for control_points")
self._control_points = torch.nn.Parameter(value, requires_grad=True)

@property
def knots(self):
return self._knots

@knots.setter
def knots(self, value):
if isinstance(value, dict):

type_ = value.get('type', 'auto')
min_ = value.get('min', 0)
max_ = value.get('max', 1)
n = value.get('n', 10)
type_ = value.get("type", "auto")
min_ = value.get("min", 0)
max_ = value.get("max", 1)
n = value.get("n", 10)

if type_ == 'uniform':
if type_ == "uniform":
value = torch.linspace(min_, max_, n + self.k + 1)
elif type_ == 'auto':
initial_knots = torch.ones(self.order+1)*min_
final_knots = torch.ones(self.order+1)*max_
elif type_ == "auto":
initial_knots = torch.ones(self.order + 1) * min_
final_knots = torch.ones(self.order + 1) * max_

if n < self.order + 1:
value = torch.concatenate((initial_knots, final_knots))
elif n - 2*self.order + 1 == 1:
value = torch.Tensor([(max_ + min_)/2])
elif n - 2 * self.order + 1 == 1:
value = torch.Tensor([(max_ + min_) / 2])
else:
value = torch.linspace(min_, max_, n - 2*self.order - 1)
value = torch.linspace(min_, max_, n - 2 * self.order - 1)

value = torch.concatenate(
(
initial_knots, value, final_knots
)
)
value = torch.concatenate((initial_knots, value, final_knots))

if not isinstance(value, torch.Tensor):
raise ValueError('Invalid value for knots')
raise ValueError("Invalid value for knots")

self._knots = value

Expand All @@ -154,7 +154,7 @@ def forward(self, x_):
:param torch.Tensor x_: points to be evaluated.
:return: the spline evaluated at x_
:rtype: torch.Tensor
:rtype: torch.Tensor
"""
t = self.knots
k = self.k
Expand All @@ -163,4 +163,4 @@ def forward(self, x_):
basis = map(lambda i: self.basis(x_, k, i, t)[:, None], range(len(c)))
y = (torch.cat(list(basis), dim=1) * c).sum(axis=1)

return y
return y

0 comments on commit f812d87

Please sign in to comment.