Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
simplex = [domain.triangulation.vertices[p] for p in subdomain]
z = data[simplex[0]]
if isinstance(z, Iterable):
s = [(*x, *data[x]) for x in simplex]
n = [(*x, *data[x]) for x in neighbor_points]
else:
s = [(*x, data[x]) for x in simplex]
n = [(*x, data[x]) for x in neighbor_points]
return sum(simplex_volume_in_embedding([*s, neigh]) for neigh in n) / len(
neighbors
)
class CurvatureLoss(LossFunction):
def __init__(self, exploration=0.05):
self.exploration = exploration
@property
def n_neighbors(self):
return 1
def __call__(self, domain, subdomain, codomain_bounds, data):
dim = domain.ndim
loss_input_volume = domain.volume(subdomain)
triangle_loss = TriangleLoss()
loss_curvature = triangle_loss(domain, subdomain, codomain_bounds, data)
return (
loss_curvature + self.exploration * loss_input_volume ** ((2 + dim) / dim)
"""
class DistanceLoss(LossFunction):
@property
def n_neighbors(self):
return 0
def __call__(self, domain, subdomain, codomain_bounds, data):
assert isinstance(domain, Interval)
a, b = subdomain
ya, yb = data[a], data[b]
return math.sqrt((b - a) ** 2 + (yb - ya) ** 2)
class EmbeddedVolumeLoss(LossFunction):
@property
def n_neighbors(self):
return 0
def __call__(self, domain, subdomain, codomain_bounds, data):
assert isinstance(domain, ConvexHull)
xs = [tuple(domain.triangulation.vertices[x]) for x in subdomain]
ys = [data[x] for x in xs]
if isinstance(ys[0], Iterable):
pts = [(*x, *y) for x, y in zip(xs, ys)]
else:
pts = [(*x, y) for x, y in zip(xs, ys)]
return simplex_volume_in_embedding(pts)
class TriangleLoss(LossFunction):
class LossFunction(metaclass=abc.ABCMeta):
@abc.abstractproperty
def n_neighbors(self):
"The maximum degree of neighboring subdomains required."
@abc.abstractmethod
def __call__(self, domain, subdomain, codomain_bounds, data):
"""Return the loss for 'subdomain' given 'data'
Neighboring subdomains can be obtained with
'domain.neighbors(subdomain, self.n_neighbors)'.
"""
class DistanceLoss(LossFunction):
@property
def n_neighbors(self):
return 0
def __call__(self, domain, subdomain, codomain_bounds, data):
assert isinstance(domain, Interval)
a, b = subdomain
ya, yb = data[a], data[b]
return math.sqrt((b - a) ** 2 + (yb - ya) ** 2)
class EmbeddedVolumeLoss(LossFunction):
@property
def n_neighbors(self):
return 0
@property
def n_neighbors(self):
return 0
def __call__(self, domain, subdomain, codomain_bounds, data):
assert isinstance(domain, ConvexHull)
xs = [tuple(domain.triangulation.vertices[x]) for x in subdomain]
ys = [data[x] for x in xs]
if isinstance(ys[0], Iterable):
pts = [(*x, *y) for x, y in zip(xs, ys)]
else:
pts = [(*x, y) for x, y in zip(xs, ys)]
return simplex_volume_in_embedding(pts)
class TriangleLoss(LossFunction):
@property
def n_neighbors(self):
return 1
def __call__(self, domain, subdomain, codomain_bounds, data):
assert isinstance(domain, ConvexHull)
neighbors = domain.neighbors(subdomain, self.n_neighbors)
if not neighbors:
return 0
neighbor_points = set.union(*(set(n) - set(subdomain) for n in neighbors))
neighbor_points = [domain.triangulation.vertices[p] for p in neighbor_points]
simplex = [domain.triangulation.vertices[p] for p in subdomain]
z = data[simplex[0]]