-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
from kafka import KafkaConsumer, TopicPartition, KafkaProducer | ||
import json | ||
|
||
from kafka import KafkaConsumer, KafkaProducer, TopicPartition | ||
|
||
|
||
def create_producer(): | ||
return KafkaProducer(bootstrap_servers='localhost:9092', | ||
value_serializer=lambda v: json.dumps(v).encode('utf-8')) | ||
return KafkaProducer(bootstrap_servers="localhost:9092", value_serializer=lambda v: json.dumps(v).encode("utf-8")) | ||
|
||
|
||
def create_consumer(topic): | ||
consumer = KafkaConsumer(bootstrap_servers='localhost:9092', | ||
value_deserializer=lambda x: json.loads(x.decode('utf-8'))) | ||
consumer = KafkaConsumer(bootstrap_servers="localhost:9092", value_deserializer=lambda x: json.loads(x.decode("utf-8"))) | ||
# Manually assign partitions | ||
# https://github.com/dpkp/kafka-python/issues/601#issuecomment-331419097 | ||
assignments = [] | ||
partitions = consumer.partitions_for_topic(topic) | ||
for p in partitions: | ||
print(f'topic {topic} - partition {p}') | ||
print(f"topic {topic} - partition {p}") | ||
assignments.append(TopicPartition(topic, p)) | ||
consumer.assign(assignments) | ||
|
||
return consumer | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
from datetime import datetime | ||
from whylogs import get_or_create_session | ||
import pandas as pd | ||
|
||
def log_session(dataset_name,session_data): | ||
from whylogs import get_or_create_session | ||
|
||
|
||
def log_session(dataset_name, session_data): | ||
session = get_or_create_session() | ||
df = pd.DataFrame(session_data) | ||
df["timestamp"] = pd.to_datetime(df['timestamp'],unit="ms") | ||
df_minutes = df.groupby(pd.Grouper(key='timestamp',freq='min')) | ||
for minute_batch,batch in df_minutes: | ||
with session.logger(dataset_name=dataset_name,dataset_timestamp=minute_batch) as logger: | ||
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms") | ||
df_minutes = df.groupby(pd.Grouper(key="timestamp", freq="min")) | ||
for minute_batch, batch in df_minutes: | ||
with session.logger(dataset_name=dataset_name, dataset_timestamp=minute_batch) as logger: | ||
logger.log_dataframe(batch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,22 @@ | ||
features = [ | ||
"mtarg1", | ||
"mtarg2", | ||
"mtarg1", | ||
"mtarg2", | ||
"mtarg3", | ||
|
||
"roll", | ||
"pitch", | ||
|
||
"LACCX", | ||
"LACCY", | ||
"LACCX", | ||
"LACCY", | ||
"LACCZ", | ||
|
||
"GYROX", | ||
"GYROY", | ||
|
||
"SC1I", | ||
"SC2I", | ||
"GYROY", | ||
"SC1I", | ||
"SC2I", | ||
"SC3I", | ||
|
||
|
||
"BT1I", | ||
"BT2I", | ||
"vout", | ||
"iout", | ||
"BT1I", | ||
"BT2I", | ||
"vout", | ||
"iout", | ||
"cpuUsage", | ||
|
||
] | ||
] | ||
|
||
fault_features = [ | ||
'fault', | ||
'fault_type', | ||
'fault_value', | ||
'fault_duration' | ||
] | ||
fault_features = ["fault", "fault_type", "fault_value", "fault_duration"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,72 @@ | ||
import json | ||
import joblib | ||
import pandas as pd | ||
from model_features import features,fault_features | ||
import os | ||
import pickle | ||
|
||
import joblib | ||
import numpy as np | ||
from kafkaConnector import create_producer,create_consumer | ||
import os | ||
from kafkaConnector import create_consumer, create_producer | ||
from model_features import features | ||
|
||
# Loading regression model | ||
model_folder = "model_files" | ||
reg_model = joblib.load(open(os.path.join(model_folder,'BL_GYROZ.joblib'), 'rb')) | ||
target = 'GYROZ' | ||
reg_model = joblib.load(open(os.path.join(model_folder, "BL_GYROZ.joblib"), "rb")) | ||
target = "GYROZ" | ||
|
||
# Initializing Kafka consumer (for telemetry topic) and producker (for prediction topic) | ||
topic = 'telemetry-rov' | ||
topic = "telemetry-rov" | ||
producer = create_producer() | ||
consumer = create_consumer(topic) | ||
|
||
|
||
def main(): | ||
|
||
consumer.seek_to_beginning() | ||
session_number = 0 | ||
prev = None | ||
session_data = [] | ||
session_timeout = 10 | ||
while True: | ||
record = consumer.poll(timeout_ms=session_timeout*1000, max_records=100, update_offsets=True) | ||
record = consumer.poll(timeout_ms=session_timeout * 1000, max_records=100, update_offsets=True) | ||
if not record: | ||
print("{} seconds without new data!".format(session_timeout)) | ||
for k,v in record.items(): | ||
for k, v in record.items(): | ||
for row in v: | ||
current = row.value | ||
current_ts = row.value.get("timestamp") | ||
res = calculate_residual(current,prev) | ||
to_send = {'residual':res,'timestamp':current_ts} | ||
producer.send('prediction-rov', to_send) | ||
res = calculate_residual(current, prev) | ||
to_send = {"residual": res, "timestamp": current_ts} | ||
producer.send("prediction-rov", to_send) | ||
producer.flush() | ||
prev = current | ||
prev_ts = current_ts | ||
finished = False | ||
|
||
def calculate_residual(current,prev): | ||
|
||
def calculate_residual(current, prev): | ||
if prev: | ||
# More than 0.5s has passed without the ROV sending new telemetry data | ||
if current['timestamp']-prev['timestamp']>500: | ||
if current["timestamp"] - prev["timestamp"] > 500: | ||
print("OVER TIME LIMIT") | ||
return np.nan | ||
else: | ||
prev = { ft: float(prev[ft]) for ft in features } | ||
prev = {ft: float(prev[ft]) for ft in features} | ||
x = np.array(list(prev.values())) | ||
x=x.reshape(1,-1) | ||
x = x.reshape(1, -1) | ||
|
||
# Min-max scaler for input features | ||
with open(os.path.join(model_folder,'BL_x.pickle'), 'rb') as f: | ||
scaler_x=pickle.load(f) | ||
with open(os.path.join(model_folder, "BL_x.pickle"), "rb") as f: | ||
scaler_x = pickle.load(f) | ||
|
||
x=scaler_x.transform(x) | ||
x = scaler_x.transform(x) | ||
|
||
py = reg_model.predict(x) | ||
# Min-max scaler for the target - GYROZ | ||
with open(os.path.join(model_folder,'BL_y.pickle'), 'rb') as f: | ||
scaler_y=pickle.load(f) | ||
py = py.reshape(1,-1) | ||
with open(os.path.join(model_folder, "BL_y.pickle"), "rb") as f: | ||
scaler_y = pickle.load(f) | ||
py = py.reshape(1, -1) | ||
py = scaler_y.inverse_transform(py) | ||
py = py.ravel() | ||
y = float(current[target]) | ||
# return residual (diff. between predicted and current) | ||
return abs(py[0]-y) | ||
return abs(py[0] - y) | ||
else: | ||
return np.nan | ||
if (__name__=='__main__'): | ||
main() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.