def from_json_data(cls, json_data: dict) -> 'RescaleTreatment': return cls(json_data["UseOnWindow"], Window.from_json_data(json_data["Window"]), json_data["UseOnBaseline"], Window.from_json_data(json_data["Baseline"]), json_data['BeforeMin'], json_data['BeforeMax'], json_data['AfterMin'], json_data['AfterMax'], json_data["Order"], json_data["ID"])
def from_json_data(cls, json_data: dict) -> 'ClampTreatment': return cls(json_data["UseOnWindow"], Window.from_json_data(json_data["Window"]), json_data["UseOnBaseline"], Window.from_json_data(json_data["Baseline"]), json_data['UseMinClamp'], json_data['Min'], json_data['UseMaxClamp'], json_data['Max'], json_data["Order"], json_data["ID"])
def from_json_data(cls, json_data: dict) -> 'SubBloc': return cls( json_data["Name"], json_data["Order"], SubBlocType(json_data["Type"]), Window.from_json_data(json_data["Window"]), Window.from_json_data(json_data["Baseline"]), [Event.from_json_data(event) for event in json_data["Events"]], [Icon.from_json_data(icon) for icon in json_data["Icons"]], [ Treatment.from_json_data(treatment) for treatment in json_data["Treatments"] ], json_data["ID"])
def __init__(self, use_on_window: bool = True, window: Window = None, use_on_baseline: bool = True, baseline: Window = None, order: int = 0, ID: str = ""): super().__init__(ID) self.use_on_window = use_on_window self.window = window if window is not None else Window() self.use_on_baseline = use_on_baseline self.baseline = baseline if baseline is not None else Window() self.order = order
def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.D = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.d = np.zeros((const.FEATURES + 1, 1)) self.A = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c = np.zeros((const.FEATURES + 1, 1)) self.A_last = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c_last = np.zeros((const.FEATURES + 1, 1)) self.A_global = None self.w_global = None self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS) self.warmup = True
def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.warmup = True self.A = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c = np.zeros((const.FEATURES + 1, 1)) self.S = np.zeros((const.FEATURES + 1, 1)) self.E = np.zeros((const.FEATURES + 1, 1)) self.X = np.zeros((const.FEATURES + 1, 1)) self.E_global = None self.zeta = 0 self.theta = 0 self.counter = 0 self.increment = 0 self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS)
def __init__(self, name: str = "", image_path: str = "", window: Window = None, ID: str = ""): super().__init__(ID) self.name = name self.image_path = image_path self.window = window if window is not None else Window()
def __init__(self, name: str = "", order: int = 0, subBloc_type: SubBlocType = 0, window: Window = None, baseline: Window = None, events: List[Event] = None, icons: List[Icon] = None, treatments: List[Treatment] = None, ID: str = ""): super().__init__(ID) self.name = name self.order = order self.subBloc_type = subBloc_type self.window = window if window is not None else Window() self.baseline = baseline if baseline is not None else Window() self.events = events if events is not None else [] self.icons = icons if icons is not None else [] self.treatments = treatments if treatments is not None else []
def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.X = A_zero self.x = c_zero self.A = A_zero self.c = c_zero self.A_last = A_zero self.c_last = c_zero self.A_global = A_zero self.E_global = None self.zeta = 0 self.theta = 0 self.counter = 0 self.increment = 0 self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS) self.warmup = True
class Site(Sender): def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.X = A_zero self.x = c_zero self.A = A_zero self.c = c_zero self.A_last = A_zero self.c_last = c_zero self.A_global = A_zero self.E_global = None self.zeta = 0 self.theta = 0 self.counter = 0 self.increment = 0 self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS) self.warmup = True def new_stream(self, stream): try: res = self.win.update(stream) new, old = next(res) self.update_state(new, old) if self.warmup is True: if not const.TEST: self.send("warm_up", (self.A, self.c)) if self.E_global is not None: self.warmup = False self.A_last = np.copy(self.A) self.c_last = np.copy(self.c) else: self.update_drift() self.subround_process() except StopIteration: log.exception("Window has failed.") # pragma: no cover def update_state(self, new, old): for x, y in new: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.add(self.A, ml1) ml2 = x.dot(y) self.c = np.add(self.c, ml2) for x, y in old: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.subtract(self.A, ml1) ml2 = x.dot(y) self.c = np.subtract(self.c, ml2) def update_drift(self): self.X = np.subtract(self.A, self.A_last) self.x = np.subtract(self.c, self.c_last) def subround_process(self): # log.info(f"START process for: {self.nid}") # log.info(f"phi: {(phi(self.X, self.x, self.A_global, self.E_global))}") # log.info(f"zeta: {self.zeta}") # log.info(f"theta: {self.theta}") current_counter = np.floor( (phi(self.X, self.x, self.A_global, self.E_global) - self.zeta) / self.theta) # log.info(f"new counter: {current_counter}") # log.info(f"END process for: {self.nid}") if current_counter > self.counter: self.increment = current_counter - self.counter self.counter = current_counter if not const.TEST: self.send("handle_increment", self.increment) # ------------------------------------------------------------------------- # REMOTE METHOD def begin_round(self, msg): A_global, E_global = msg self.E_global = np.copy(E_global) self.A_global = np.copy(A_global) self.counter = 0 self.zeta = phi(self.X, self.x, self.A_global, self.E_global) # phi(0, 0, A_global, E_global) log.info(f"initial local zeta = {self.zeta}") psi = const.K * self.zeta self.theta = -psi / (2 * const.K) def begin_subround(self, theta): self.counter = 0 self.theta = theta log.info(f" (sub) initial local zeta = {self.zeta}") self.zeta = phi(self.X, self.x, self.A_global, self.E_global) # phi(X, x, A_global, E_global) def send_zeta(self): if not const.TEST: log.info( f"current zeta = {phi(self.X, self.x, self.A_global, self.E_global)}" ) self.send("handle_zetas", phi(self.X, self.x, self.A_global, self.E_global)) def send_drift(self): self.A_last = np.copy(self.A) self.c_last = np.copy(self.c) self.X = A_zero self.x = c_zero if not const.TEST: self.send("handle_drifts", (self.A_last, self.c_last))
class Site(Sender): def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.warmup = True self.A = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c = np.zeros((const.FEATURES + 1, 1)) self.S = np.zeros((const.FEATURES + 1, 1)) self.E = np.zeros((const.FEATURES + 1, 1)) self.X = np.zeros((const.FEATURES + 1, 1)) self.E_global = None self.zeta = 0 self.theta = 0 self.counter = 0 self.increment = 0 self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS) def new_stream(self, stream): try: res = self.win.update(stream) new, old = next(res) self.update_state(new, old) if self.warmup is True: self.send("warm_up", (self.A, self.c)) if self.E_global is not None: self.warmup = False else: self.update_drift() self.subround_process() except StopIteration: log.exeption("Window has failed.") def update_state(self, new, old): for x, y in new: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.add(self.A, ml1) ml2 = x.dot(y) self.c = np.add(self.c, ml2) for x, y in old: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.subtract(self.A, ml1) ml2 = x.dot(y) self.c = np.subtract(self.c, ml2) self.S = np.linalg.pinv(self.A).dot(self.c) def update_drift(self): self.X = np.subtract(self.S, self.E) def subround_process(self): current_counter = np.floor( (phi(self.X, self.E_global) - self.zeta) / self.theta) if current_counter > self.counter: self.increment = current_counter - self.counter self.counter = current_counter self.send("handle_increment", self.increment) # ------------------------------------------------------------------------- # REMOTE METHOD def begin_round(self, E_global): self.E_global = np.copy(E_global) self.X = np.zeros((const.FEATURES + 1, 1)) self.zeta = phi(np.zeros((const.FEATURES + 1, 1)), self.E_global) psi = const.K * self.zeta self.theta = -psi / 2 * const.K self.counter = 0 def begin_subround(self, theta): self.counter = 0 self.theta = theta self.zeta = phi(self.X, self.E_global) def send_zeta(self): self.send("handle_zetas", phi(self.X, self.E_global)) def send_drift(self): self.E = self.S self.send("handle_drifts", self.X)
class Site(Sender): def __init__(self, net, nid, ifc): super().__init__(net, nid, ifc) self.D = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.d = np.zeros((const.FEATURES + 1, 1)) self.A = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c = np.zeros((const.FEATURES + 1, 1)) self.A_last = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.c_last = np.zeros((const.FEATURES + 1, 1)) self.A_global = None self.w_global = None self.win = Window(size=const.SIZE, step=const.STEP, points=const.TRAIN_POINTS) self.warmup = True def new_stream(self, stream): # update window try: res = self.win.update(stream) new, old = next(res) self.update_state(new, old) if self.warmup is True: self.send("warm_up", (self.A, self.c)) if self.w_global is not None: self.warmup = False self.A_last = np.copy(self.A) self.c_last = np.copy(self.c) else: self.update_drift() A_in = np.linalg.pinv(self.A_global) norm = np.linalg.norm a1 = norm(np.dot(A_in, self.D)) a2 = norm(np.dot(A_in, self.d)) a3 = norm(np.dot((np.dot(A_in, self.D)), self.w_global)) req = (const.ERROR * a1) + a2 + a3 if req > const.ERROR: log.info(f"Node {self.nid} raises an alert.") log.info( f"{const.ERROR}*{a1}+{a2}+{a3} (={req}) > {const.ERROR}*" ) self.send("alert", None) except StopIteration: log.exception("Window has failed.") pass def update_state(self, new, old): for x, y in new: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.add(self.A, ml1) ml2 = x.dot(y) self.c = np.add(self.c, ml2) for x, y in old: x = x.reshape(-1, 1) ml1 = x.dot(x.T) self.A = np.subtract(self.A, ml1) ml2 = x.dot(y) self.c = np.subtract(self.c, ml2) def update_drift(self): self.D = np.subtract(self.A, self.A_last) self.d = np.subtract(self.c, self.c_last) # ------------------------------------------------------------------------- # REMOTE METHOD def new_estimate(self, msg): A_global, w_global = msg # save received global estimate self.A_global = np.copy(A_global) self.w_global = np.copy(w_global) def send_data(self): # send local state log.info(f"Node {self.nid} sends its local state,") self.A_last = np.copy(self.A) self.c_last = np.copy(self.c) self.D = np.zeros((const.FEATURES + 1, const.FEATURES + 1)) self.d = np.zeros((const.FEATURES + 1, 1)) self.send("sync", (self.A_last, self.c_last))
def from_json_data(cls, json_data: dict) -> 'Icon': return cls(json_data["Name"], json_data["ImagePath"], Window.from_json_data(json_data["Window"]), json_data['ID'])
def from_json_data(cls, json_data: dict) -> 'OffsetTreatment': return cls(json_data["UseOnWindow"], Window.from_json_data(json_data["Window"]), json_data["UseOnBaseline"], Window.from_json_data(json_data["Baseline"]), json_data['Offset'], json_data["Order"], json_data["ID"])