Skip to content

Commit

Permalink
Merge pull request #172 from aidotse/make-formatting-consistent
Browse files Browse the repository at this point in the history
Make formatting consistent
  • Loading branch information
gomezzz authored Jul 6, 2023
2 parents 5502667 + cf88650 commit 027db9d
Show file tree
Hide file tree
Showing 31 changed files with 153 additions and 204 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/autoblack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Black Format Check

on: [pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable
with:
options: "--check --line-length 100"
src: "."
jupyter: false
version: "23.3.0"
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def get_closest_entry(df, t, id):


def get_analysis_df(df, timestep=60, orbital_period=1):

t = np.round(np.linspace(0, df.Time.max(), int(df.Time.max() // timestep)))
sats = df.ID.unique()
df["known_actors"] = pd.Categorical(df.known_actors)
Expand Down
12 changes: 3 additions & 9 deletions examples/Learning_example/simple_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def __len__(self):

# Instantiate training and test data
X, y = make_circles(n_samples=10000, noise=0.05, random_state=26)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=26
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=26)

# divide the training set so none of the peers have the same data
if self.node_id == 1:
Expand All @@ -75,12 +73,8 @@ def __len__(self):
# Create dataloaders
train_data = Data(X_train, y_train)
test_data = Data(X_test, y_test)
self.train_dataloader = DataLoader(
dataset=train_data, batch_size=64, shuffle=True
)
self.test_dataloader = DataLoader(
dataset=test_data, batch_size=64, shuffle=True
)
self.train_dataloader = DataLoader(dataset=train_data, batch_size=64, shuffle=True)
self.test_dataloader = DataLoader(dataset=test_data, batch_size=64, shuffle=True)

def forward(self, x):
"""Do inference on model
Expand Down
16 changes: 4 additions & 12 deletions examples/Learning_example/simple_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ class Node:
"""

def __init__(self, node_id, pos_and_vel, paseos_cfg, power_consumption_in_watt):

# Create PASEOS instance to node
earth = pk.planet.jpl_lp("earth")
self.node_id = node_id
sat = ActorBuilder.get_actor_scaffold(
f"sat{node_id}", SpacecraftActor, pk.epoch(0)
)
sat = ActorBuilder.get_actor_scaffold(f"sat{node_id}", SpacecraftActor, pk.epoch(0))
ActorBuilder.set_orbit(sat, pos_and_vel[0], pos_and_vel[1], pk.epoch(0), earth)
ActorBuilder.set_power_devices(
actor=sat,
Expand All @@ -37,8 +34,7 @@ def __init__(self, node_id, pos_and_vel, paseos_cfg, power_consumption_in_watt):

transmit_bits = self.model_size()
self.transmit_duration = transmit_bits / (
1000
* self.paseos.local_actor.communication_devices["link"].bandwidth_in_kbps
1000 * self.paseos.local_actor.communication_devices["link"].bandwidth_in_kbps
)

self.current_activity = "train"
Expand Down Expand Up @@ -70,9 +66,7 @@ def model_size(self):
# Return model parameters as a list of NumPy ndarrays
bytestream = b"" # Empty byte represenation
for _, val in self.model.state_dict().items(): # go over each layer
bytestream += (
val.cpu().numpy().tobytes()
) # convert layer to bytes and concatenate
bytestream += val.cpu().numpy().tobytes() # convert layer to bytes and concatenate
return len(bytestream) * 8

def local_time(self):
Expand Down Expand Up @@ -112,9 +106,7 @@ def transmission_is_feasible(self, target_node):
target_actor = target_node.paseos.local_actor
local_actor = self.paseos.local_actor

transmit_end = pk.epoch(
self.local_time().mjd2000 + self.transmit_duration * pk.SEC2DAY
)
transmit_end = pk.epoch(self.local_time().mjd2000 + self.transmit_duration * pk.SEC2DAY)
los_end = local_actor.is_in_line_of_sight(target_actor, transmit_end)
return los_end

Expand Down
22 changes: 6 additions & 16 deletions examples/MPI_example/mpi_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
try:
from mpi4py import MPI
except:
print(
"This example requires mpi4py. Please install with conda install mpi4py -c conda-forge"
)
print("This example requires mpi4py. Please install with conda install mpi4py -c conda-forge")

import pykep as pk
import paseos
Expand All @@ -44,9 +42,7 @@
# Now we will initialize MPI, for more details please refer to the mpi4py docs.
# In MPI "rank" indicates the index of the compute node (so 0-3 in our example).
comm = MPI.COMM_WORLD
assert (
comm.Get_size() == 4
), "Please run the example with mpiexec -n 4 python mpi_example.py"
assert comm.Get_size() == 4, "Please run the example with mpiexec -n 4 python mpi_example.py"
rank = comm.Get_rank()
other_ranks = [x for x in range(4) if x != rank]
print(f"Started rank {rank}, other ranks are {other_ranks}")
Expand All @@ -65,9 +61,7 @@
planet_list, sats_pos_and_v, _ = get_constellation(
altitude, inclination, nSats, nPlanes, t0, verbose=False
)
print(
f"Rank {rank} set up its orbit with altitude={altitude}m and inclination={inclination}deg"
)
print(f"Rank {rank} set up its orbit with altitude={altitude}m and inclination={inclination}deg")

############ PASEOS INIT #############
# We will now initialize the PASEOS instance on each rank
Expand All @@ -78,9 +72,7 @@
local_actor = ActorBuilder.get_actor_scaffold(
name="Sat_" + str(rank), actor_type=SpacecraftActor, epoch=t0
)
ActorBuilder.set_orbit(
actor=local_actor, position=pos, velocity=v, epoch=t0, central_body=earth
)
ActorBuilder.set_orbit(actor=local_actor, position=pos, velocity=v, epoch=t0, central_body=earth)

paseos_instance = paseos.init_sim(local_actor=local_actor)
print(f"Rank {rank} set up its PASEOS instance for its local actor {local_actor}")
Expand All @@ -97,6 +89,7 @@
# Let's define the variable to track the actors we see
total_seen_actors = 0


# We will (ab)use PASEOS constraint function to track all the actors
# we see in an evaluation window (see timestep below).
# Turn on SHOW_ALL_WINDOWS if you want to see each window
Expand Down Expand Up @@ -135,7 +128,6 @@ def constraint_func(verbose=SHOW_ALL_WINDOWS):

# Run until end of simulation
while t <= simulation_time:

# Advance the simulation state of this rank
# Note how we pass the "constraint_func" to tell paseos
# to track windows
Expand All @@ -147,9 +139,7 @@ def constraint_func(verbose=SHOW_ALL_WINDOWS):
sys.stdout.flush() # update prints to better see parallelism

# Exchange actors between all ranks
exchange_actors(
comm, paseos_instance, local_actor, other_ranks, rank, verbose=SHOW_ALL_COMMS
)
exchange_actors(comm, paseos_instance, local_actor, other_ranks, rank, verbose=SHOW_ALL_COMMS)

# Wait until all ranks finished
print(f"Rank {rank} finished the simulation. Waiting for all to finish.")
Expand Down
8 changes: 2 additions & 6 deletions examples/MPI_example/mpi_utility_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def _parse_actor_data(actor_data):
return actor


def exchange_actors(
comm, paseos_instance, local_actor, other_ranks, rank, verbose=False
):
def exchange_actors(comm, paseos_instance, local_actor, other_ranks, rank, verbose=False):
"""This function exchanges the states of various actors among all MPI ranks.
Args:
Expand All @@ -69,9 +67,7 @@ def exchange_actors(
# Send local actor to other ranks
for i in other_ranks:
actor_data = _encode_actor(local_actor)
send_requests.append(
comm.isend(actor_data, dest=i, tag=int(str(rank) + str(i)))
)
send_requests.append(comm.isend(actor_data, dest=i, tag=int(str(rank) + str(i))))

# Receive from other ranks
for i in other_ranks:
Expand Down
12 changes: 3 additions & 9 deletions examples/Sentinel_2_example_notebook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,8 @@ def get_thresholds(
alpha = np.logical_and(
np.where(sentinel_img[:, :, 2] >= alpha_thr[2], 1, 0),
np.logical_and(
np.where(
sentinel_img[:, :, 2] / sentinel_img[:, :, 1] >= alpha_thr[0], 1, 0
),
np.where(
sentinel_img[:, :, 2] / sentinel_img[:, :, 0] >= alpha_thr[1], 1, 0
),
np.where(sentinel_img[:, :, 2] / sentinel_img[:, :, 1] >= alpha_thr[0], 1, 0),
np.where(sentinel_img[:, :, 2] / sentinel_img[:, :, 0] >= alpha_thr[1], 1, 0),
),
)
beta = np.logical_and(
Expand Down Expand Up @@ -159,9 +155,7 @@ def get_alert_matrix_and_thresholds(
numpy.array: gamma threshold map.
"""

alpha, beta, S, gamma = get_thresholds(
sentinel_img, alpha_thr, beta_thr, S_thr, gamma_thr
)
alpha, beta, S, gamma = get_thresholds(sentinel_img, alpha_thr, beta_thr, S_thr, gamma_thr)
alert_matrix = np.logical_or(np.logical_or(np.logical_or(alpha, beta), gamma), S)
return alert_matrix, alpha, beta, S, gamma

Expand Down
4 changes: 1 addition & 3 deletions paseos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
logger.debug("Loaded module.")


def init_sim(
local_actor: BaseActor, cfg: DotMap = None, starting_epoch: pk.epoch = None
):
def init_sim(local_actor: BaseActor, cfg: DotMap = None, starting_epoch: pk.epoch = None):
"""Initializes PASEOS
Args:
Expand Down
4 changes: 1 addition & 3 deletions paseos/activities/activity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def remove_activity(self, name: str):
name (str): Name of the activity.
"""
if name not in self._activities.keys():
raise ValueError(
"Trying to remove non-existing activity with name: " + name
)
raise ValueError("Trying to remove non-existing activity with name: " + name)
else:
del self._activities[name]

Expand Down
4 changes: 1 addition & 3 deletions paseos/activities/activity_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ async def _update(self, elapsed_time: float):
logger.debug(f"Time since last update: {elapsed_time}s")
logger.trace(f"Applying time multiplier of {self._time_multiplier}")
elapsed_time *= self._time_multiplier
self._paseos_instance.advance_time(
elapsed_time, self._power_consumption_in_watt
)
self._paseos_instance.advance_time(elapsed_time, self._power_consumption_in_watt)

async def _run(self):
"""Main processor loop. Will track time, update paseos and check constraints of the activity."""
Expand Down
Loading

0 comments on commit 027db9d

Please sign in to comment.