Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbit committed Sep 30, 2022
1 parent 364781e commit 4ec3166
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions er/model/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def reset(self) -> None:
@property
def meta(self) -> dict:
return {
'model': self.__class__.__name__,
'params': self.params,
'graph': self.graph.name
"model": self.__class__.__name__,
"params": self.params,
"graph": self.graph.name,
}

@abstractmethod
Expand Down Expand Up @@ -109,8 +109,9 @@ def clear_memory(self, min_time):
data["switch"].clear(min_time)

def neighbors(self, node, time):
return [n for n, data in self.graph[node].items()
if data["switch"].open(node, time)]
return [
n for n, data in self.graph[node].items() if data["switch"].open(node, time)
]

def edges(self, time):
for source, target, data in self.graph.edges(data=True):
Expand All @@ -132,8 +133,10 @@ class SwitchingNetworkConstantRate(SwitchingNetwork):
"""

def neighbors(self, node, time):
return [n if data["switch"].open(node, time) else node
for n, data in self.graph[node].items()]
return [
n if data["switch"].open(node, time) else node
for n, data in self.graph[node].items()
]


class MemorylessSwitch:
Expand Down Expand Up @@ -164,7 +167,7 @@ def open(self, source, time):
return not (source == self.source) ^ (self.status) # xnor

def reset(self):
self.time = 0.
self.time = 0.0
self.status = random.randint(0, 1)


Expand All @@ -176,11 +179,11 @@ def __init__(self, source, timescale, batch=1000):
self.init = 1 if random.random() < 0.5 else 0
self.timescale = timescale
self.batch = batch
self.switch_times = np.array([0.])
self.switch_times = np.array([0.0])

def reset(self):
self.init = 1 if random.random() < 0.5 else 0
self.switch_times = np.array([0.])
self.switch_times = np.array([0.0])

def open(self, source, time):
# Expand lifespan if required.
Expand All @@ -192,8 +195,7 @@ def open(self, source, time):

return not (
(source == self.source)
^ (1 + self.init
+ bisect.bisect(self.switch_times, time)) % 2
^ (1 + self.init + bisect.bisect(self.switch_times, time)) % 2
) # xnor

def _expand_times(self):
Expand All @@ -212,8 +214,7 @@ def clear(self, time):
if self.switch_times[-1] < time:
interval = time - self.switch_times[-1]
num_events = np.random.poisson(interval / self.timescale)
self.init = (
self.open(self.source, self.switch_times[-1]) + num_events) % 2
self.init = (self.open(self.source, self.switch_times[-1]) + num_events) % 2
self.switch_times = np.array([time])
else:
self.init = self.open(self.source, time)
Expand Down

0 comments on commit 4ec3166

Please sign in to comment.