Geometry Operations Enhancement (#122)

* updating exclusion domain
- update sample/ is_inside
- create tests

* difference fixes
- random iteration list for sample

* created Intersection

* created a Difference domain

* unittest

* docstrings and minor fixes

* Refacotring Geometries
- added OperationInterface
- redid test cases
- edited Union, Intersect, Exclusion, and Difference
to inherit from OperationInterface
- simplified Union, Intersect, Exclusion, and Difference

* rm lighting logs

---------

Co-authored-by: Dario Coscia <dariocoscia@cli-10-110-16-239.WIFIeduroamSTUD.units.it>
This commit is contained in:
Kush
2023-07-04 12:08:47 +02:00
committed by Nicola Demo
parent 44cf800491
commit 2d0256a179
13 changed files with 534 additions and 80 deletions

View File

@@ -1,11 +1,12 @@
import torch
from .location import Location
from .operation_interface import OperationInterface
from ..utils import check_consistency
from ..label_tensor import LabelTensor
import random
class Union(Location):
class Union(OperationInterface):
""" PINA implementation of Unions of Domains."""
def __init__(self, geometries):
@@ -23,37 +24,7 @@ class Union(Location):
>>> union = GeometryUnion([ellipsoid1, ellipsoid2])
"""
super().__init__()
# union checks
check_consistency(geometries, Location)
self._check_union_dimensions(geometries)
# assign geometries
self._geometries = geometries
@property
def geometries(self):
"""
The geometries."""
return self._geometries
@property
def variables(self):
"""
Spatial variables.
:return: All the spatial variables defined in '__init__()' in order.
:rtype: list[str]
"""
all_variables = []
seen_variables = set()
for geometry in self.geometries:
for variable in geometry.variables:
if variable not in seen_variables:
all_variables.append(variable)
seen_variables.add(variable)
return all_variables
super().__init__(geometries)
def is_inside(self, point, check_border=False):
"""Check if a point is inside the union domain.
@@ -72,7 +43,7 @@ class Union(Location):
return False
def sample(self, n, mode='random', variables='all'):
"""Sample routine.
"""Sample routine for union domain.
:param n: Number of points to sample in the shape.
:type n: int
@@ -84,23 +55,21 @@ class Union(Location):
:Example:
# Create two ellipsoid domains
>>> ellipsoid1 = EllipsoidDomain({'x': [-1, 1], 'y': [-1, 1]})
>>> ellipsoid2 = EllipsoidDomain({'x': [0, 2], 'y': [0, 2]})
>>> cartesian1 = CartesianDomain({'x': [0, 2], 'y': [0, 2]})
>>> cartesian2 = CartesianDomain({'x': [1, 3], 'y': [1, 3]})
# Create a union of the ellipsoid domains
>>> union = Union([ellipsoid1, ellipsoid2])
>>> union = Union([cartesian1, cartesian2])
>>> union.sample(n=1000)
LabelTensor([[-0.2025, 0.0072],
[ 0.0358, 0.5748],
[ 0.5083, 0.0482],
...,
[ 0.5857, 0.9279],
[ 1.1496, 1.7339],
[ 0.7650, 1.0469]])
>>> union.sample(n=5)
LabelTensor([[1.2128, 2.1991],
[1.3530, 2.4317],
[2.2562, 1.6605],
[0.8451, 1.9878],
[1.8623, 0.7102]])
>>> len(union.sample(n=1000)
1000
>>> len(union.sample(n=5)
5
"""
sampled_points = []
@@ -122,15 +91,4 @@ class Union(Location):
if len(sampled_points) >= n:
break
return LabelTensor(torch.cat(sampled_points), labels=[f'{i}' for i in self.variables])
def _check_union_dimensions(self, geometries):
"""Check if the dimensions of the geometries are consistent.
:param geometries: Geometries to be checked.
:type geometries: list[Location]
"""
for geometry in geometries:
if geometry.variables != geometries[0].variables:
raise NotImplementedError(
f'The geometries need to be the same dimensions. {geometry.variables} is not equal to {geometries[0].variables}')
return LabelTensor(torch.cat(sampled_points), labels=self.variables)