mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-08-07 07:14:50 +00:00
NOISSUE - Enable WASM Support and FileSystem Support (#189)
* feat(algorithm): Add wasm as an algo type Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * feat(algorithm): Use filesystem to store results Move from unix socket for results storage to filesystem * test: test new filesystem changes Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * refactor(files): rename resultFile to resultsFilePath * feat(wasm-runtime): change from wasmtime to wasmedge Wasmedge enables easier directory mapping to get results Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * feat(algorithm): send results as zipped directory Create a new function to zip the results directory and send it back to the user * fix(wasm): runtime argument Fix the directory mapping for wasm runtime arguments Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * fix(errors): provide useful error message * chore(gitignore): add results zip to gitignore * feat(filesystem): Enable storing results on filesystem for python algos * refactor: revert to upstream cocos repo Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * fix: remove AddDataset from algorithm interface * fix: agent to handle results zipping * test: test zipping directories * refactor(agent): Handle file operations from agent * test: run test inside eos Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * refactor(test): Document and test algos are running Document steps on running the 2 python exampls and ensure they are running on eos Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * fix: remove witheDataset option * test: test without dataset argument Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> --------- Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
This commit is contained in:
+100
-31
@@ -1,47 +1,116 @@
|
||||
import sys, io
|
||||
import os
|
||||
import sys
|
||||
import joblib
|
||||
import socket
|
||||
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
import zipfile
|
||||
from sklearn import metrics
|
||||
|
||||
csv_file_path = sys.argv[2]
|
||||
iris = pd.read_csv(csv_file_path)
|
||||
DATA_DIR = "datasets"
|
||||
RESULTS_DIR = "results"
|
||||
RESULTS_FILE = "model.bin"
|
||||
|
||||
# Droping the Species since we only need the measurements
|
||||
X = iris.drop(['Species'], axis=1)
|
||||
|
||||
# converting into numpy array and assigning petal length and petal width
|
||||
X = X.to_numpy()[:, (3,4)]
|
||||
y = iris['Species']
|
||||
class Computation:
|
||||
model = None
|
||||
|
||||
# Splitting into train and test
|
||||
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.5, random_state=42)
|
||||
def __init__(self):
|
||||
"""
|
||||
Initializes a new instance of the Computation class.
|
||||
"""
|
||||
pass
|
||||
|
||||
log_reg = LogisticRegression()
|
||||
log_reg.fit(X_train,y_train)
|
||||
def _read_csv(self, data_path=""):
|
||||
"""
|
||||
Reads the CSV file.
|
||||
"""
|
||||
files = os.listdir(data_path)
|
||||
if len(files) != 1:
|
||||
print("No files found in the directory")
|
||||
exit(1)
|
||||
csv_file_path = data_path + os.sep + files[0]
|
||||
return pd.read_csv(csv_file_path)
|
||||
|
||||
# Serialize the trained model to a byte buffer
|
||||
model_buffer = io.BytesIO()
|
||||
joblib.dump(log_reg, model_buffer)
|
||||
def compute(self):
|
||||
"""
|
||||
Trains a logistic regression model.
|
||||
"""
|
||||
iris = self._read_csv(DATA_DIR)
|
||||
|
||||
# Get the serialized model as a bytes object
|
||||
model_bytes = model_buffer.getvalue()
|
||||
# Droping the Species since we only need the measurements
|
||||
X = iris.drop(["Species"], axis=1)
|
||||
|
||||
# Define the path for the Unix domain socket
|
||||
socket_path = sys.argv[1]
|
||||
# converting into numpy array and assigning petal length and petal width
|
||||
X = X.to_numpy()[:, (3, 4)]
|
||||
y = iris["Species"]
|
||||
|
||||
# Create a Unix domain socket client
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.5, random_state=42)
|
||||
|
||||
try:
|
||||
# Connect to the server
|
||||
client.connect(socket_path)
|
||||
log_reg = LogisticRegression()
|
||||
log_reg.fit(X_train, y_train)
|
||||
self.model = log_reg
|
||||
|
||||
# Send the serialized model over the socket
|
||||
client.send(model_bytes)
|
||||
def save_result(self):
|
||||
"""
|
||||
Sends the result to a file.
|
||||
"""
|
||||
try:
|
||||
os.makedirs(RESULTS_DIR)
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
finally:
|
||||
# Close the socket
|
||||
client.close()
|
||||
results_file = RESULTS_DIR + os.sep + RESULTS_FILE
|
||||
joblib.dump(self.model, results_file)
|
||||
|
||||
def read_results_from_file(self, results_file):
|
||||
"""
|
||||
Reads the results from a file.
|
||||
"""
|
||||
if results_file.endswith(".zip"):
|
||||
try:
|
||||
os.makedirs(RESULTS_DIR)
|
||||
except FileExistsError:
|
||||
pass
|
||||
with zipfile.ZipFile(results_file, "r") as zip_ref:
|
||||
zip_ref.extractall(RESULTS_DIR)
|
||||
self.model = joblib.load(RESULTS_DIR + os.sep + RESULTS_FILE)
|
||||
else:
|
||||
self.model = joblib.load(results_file)
|
||||
|
||||
def predict(self, data_path=""):
|
||||
iris = self._read_csv(data_path)
|
||||
|
||||
# Droping the Species since we only need the measurements
|
||||
X = iris.drop(["Species"], axis=1)
|
||||
|
||||
# converting into numpy array and assigning petal length and petal width
|
||||
X = X.to_numpy()[:, (3, 4)]
|
||||
y = iris["Species"]
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
X, y, test_size=0.5, random_state=42
|
||||
)
|
||||
|
||||
training_prediction = self.model.predict(X_train)
|
||||
test_prediction = self.model.predict(X_test)
|
||||
|
||||
print("Precision, Recall, Confusion matrix, in training\n")
|
||||
print(metrics.classification_report(y_train, training_prediction, digits=3))
|
||||
print(metrics.confusion_matrix(y_train, training_prediction))
|
||||
print("Precision, Recall, Confusion matrix, in testing\n")
|
||||
print(metrics.classification_report(y_test, test_prediction, digits=3))
|
||||
print(metrics.confusion_matrix(y_test, test_prediction))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
computation = Computation()
|
||||
if len(sys.argv) == 1:
|
||||
computation.compute()
|
||||
computation.save_result()
|
||||
elif len(sys.argv) == 4 and sys.argv[1] == "predict":
|
||||
computation.read_results_from_file(sys.argv[2])
|
||||
computation.predict(sys.argv[3])
|
||||
else:
|
||||
print("Invalid arguments")
|
||||
exit(1)
|
||||
|
||||
Reference in New Issue
Block a user