Skip to content

Commit

Permalink
Added bounding box calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Sep 19, 2024
1 parent ab301b0 commit de7a042
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/random_events/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.1.0'
__version__ = '3.1.1'
17 changes: 17 additions & 0 deletions src/random_events/product_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ def marginal(self, variables: VariableSet) -> Event:
result.add_simple_set(simple_set.marginal(variables))
return result.make_disjoint()

def bounding_box(self) -> SimpleEvent:
"""
Compute the bounding box of the event.
The bounding box is the smallest simple event that contains this event. It is computed by taking the union
of all simple events variable wise.
:return: The bounding box as a simple event
"""
result = SimpleEvent()
for variable in self.all_variables:
for simple_set in self.simple_sets:
if variable not in result:
result[variable] = simple_set[variable]
else:
result[variable] = result[variable].union_with(simple_set[variable])
return result

def plot(self, color="#636EFA") -> Union[List[go.Scatter], List[go.Mesh3d]]:
"""
Plot the complex event.
Expand Down
9 changes: 9 additions & 0 deletions test/test_product_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def test_to_json_multiple_events(self):
event_ = AbstractSimpleSet.from_json(event.to_json())
self.assertEqual(event_, event)

def test_bounding_box(self):
event_1 = SimpleEvent({self.x: closed(0, 1), self.y: SimpleInterval(0, 1)}).as_composite_set()
event_2 = SimpleEvent({self.x: closed(1, 2), self.y: Interval(SimpleInterval(3, 4))}).as_composite_set()
event = event_1 | event_2
bounding_box = event.bounding_box()
result = SimpleEvent({self.x: closed(0, 2),
self.y: SimpleInterval(0, 1).as_composite_set() | SimpleInterval(3, 4).as_composite_set()})
self.assertEqual(bounding_box, result)


class NoneTypeObjectInDifferenceTestCase(unittest.TestCase):
x: Continuous = Continuous("x")
Expand Down

0 comments on commit de7a042

Please sign in to comment.