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

Monitor convenience functions #95

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions paseos/paseos.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PASEOS:

# Used to monitor the local actor over execution and write performance stats
_operations_monitor = None

_time_since_previous_log = sys.float_info.max

def __init__(self, local_actor: BaseActor, cfg=None):
Expand Down Expand Up @@ -168,6 +169,15 @@ def add_known_actor(self, actor: BaseActor):
# Else add
self._known_actors[actor.name] = actor

@property
def monitor(self):
"""Access paseos operations monitor which tracks local actor attributes such as temperature or state of charge.

Returns:
OperationsMonitor: Monitor object.
"""
return self._operations_monitor

@property
def is_running_activity(self):
"""Allows checking whether there is currently an activity running.
Expand Down
2 changes: 2 additions & 0 deletions paseos/tests/multiple_instance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def test_multiple_instances():
# Initial power is 500m check charging works
assert sat1.battery_level_in_Ws == 500
assert sat2.battery_level_in_Ws == 500

sim.advance_time(42, 0)
assert sat1.battery_level_in_Ws == 542
assert sat2.battery_level_in_Ws == 500

sim2.advance_time(42, 0)

assert sat1.battery_level_in_Ws == 542
assert sat2.battery_level_in_Ws == 542
4 changes: 4 additions & 0 deletions paseos/tests/operations_monitor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ async def func2(args):
sim.perform_activity("Activity_2")
await wait_for_activity(sim)

# Try out item function
sim.monitor["state_of_charge"]
sim.monitor.plot("state_of_charge")

sim.save_status_log_csv("test.csv")
26 changes: 26 additions & 0 deletions paseos/utils/operations_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from loguru import logger
from dotmap import DotMap
import pykep as pk
import matplotlib.pyplot as plt

from paseos.actors.base_actor import BaseActor
from paseos.actors.spacecraft_actor import SpacecraftActor
Expand All @@ -29,6 +30,31 @@ def __init__(self, actor_name):
self._log.position = []
self._log.velocity = []

def __getitem__(self, item):
"""Get a logged attributes values.

Args:
item (str): Name of item. Available are "timesteps","current_activity","state_of_charge",
"is_in_eclipse","known_actors","position","velocity","temperature"
"""
assert item in self._log.keys(), (
'Untracked quantity. Available are "timesteps","current_activity","state_of_charge",'
+ '"is_in_eclipse","known_actors","position","velocity","temperature"'
)
return self._log[item]

def plot(self, item):
assert item in self._log.keys(), (
'Untracked quantity. Available are "timesteps","current_activity","state_of_charge",'
+ '"is_in_eclipse","known_actors","position","velocity","temperature"'
)
values = self._log[item]
plt.Figure(figsize=(6, 2), dpi=150)
t = self._log.timesteps
plt.plot(t, values)
plt.xlabel("Time [s]")
plt.ylabel(item.replace("_", " "))

def log(
self,
local_actor: BaseActor,
Expand Down