def on_discover_sensor(client, userdata, msg): if number_of_devices < MAX_DEVICES: parsed = json.loads(msg.payload.decode()) logger.log('info', "Just discovered sensor: " + parsed["unique_id"]) device_id = (parsed["device"]["name"] + "_" + parsed["device"]["identifiers"]) if not device_id in nodes: create_device(parsed, device_id) try: # Add Sensor to Device nodes[device_id]["device"].add_Sensor(Sensor( parsed["name"], parsed["unique_id"], parsed["state_topic"], parsed["unit_of_measurement"])) # Handle MQTT subscribe and callbacks nodes[device_id]["mqtt"].subscribe_to_topic( parsed["state_topic"], 1) nodes[device_id]["mqtt"].add_message_callback( parsed["state_topic"], nodes[device_id]["device"].on_sensor_state_change) except: if 'mae' in parsed["state_topic"]: nodes[device_id]["device"].add_Metrics(Metrics( parsed["name"], parsed["unique_id"], parsed["state_topic"], metrics.Rolling(metrics.MAE(), int(10080/TRAIN_INTERVAL)))) elif 'rmse' in parsed["state_topic"]: nodes[device_id]["device"].add_Metrics(Metrics( parsed["name"], parsed["unique_id"], parsed["state_topic"], metrics.Rolling(metrics.RMSE(), int(10080/TRAIN_INTERVAL)))) # Check if Device as all components check_if_finished(device_id)
def test_rolling_multi_f1(): def tail(iterable, n): return collections.deque(iterable, maxlen=n) fbeta = metrics.Rolling( metric=metrics.MultiFBeta( betas={0: 0.25, 1: 1, 2: 4}, weights={0: 1, 1: 1, 2: 2} ), window_size=3 ) n = fbeta.window_size sk_fbeta = sk_metrics.fbeta_score y_true = [0, 1, 2, 2, 2] y_pred = [0, 1, 0, 2, 1] for i, (yt, yp) in enumerate(zip(y_true, y_pred)): fbeta.update(yt, yp) if i >= 2: sk_y_true, sk_y_pred = tail(y_true[:i + 1], n), tail(y_pred[:i + 1], n) fbeta_0, _, _ = sk_fbeta(sk_y_true, sk_y_pred, beta=0.25, average=None) _, fbeta_1, _ = sk_fbeta(sk_y_true, sk_y_pred, beta=1, average=None) _, _, fbeta_2 = sk_fbeta(sk_y_true, sk_y_pred, beta=4, average=None) multi_fbeta = fbeta_0 * 1 + fbeta_1 * 1 + fbeta_2 * 2 multi_fbeta /= (1 + 1 + 2) assert math.isclose(fbeta.get(), multi_fbeta)
def test_rolling_metric(metric, sk_metric): def tail(iterable, n): return collections.deque(iterable, maxlen=n) for n in (1, 2, 5, 10): for y_true, y_pred, _ in generate_test_cases(metric=metric, n=30): m = metrics.Rolling(metric=copy.deepcopy(metric), window_size=n) # Check str works str(m) for i, (yt, yp) in enumerate(zip(y_true, y_pred)): if isinstance(yp, list): yp = dict(enumerate(yp)) m.update(y_true=yt, y_pred=yp) if i >= 1: assert abs( m.get() - sk_metric( y_true=tail(y_true[:i + 1], n), y_pred=tail(y_pred[:i + 1], n) ) ) < 1e-10