|
| 1 | +from ..utils import DataikuException |
| 2 | +from ..utils import DataikuUTF8CSVReader |
| 3 | +from ..utils import DataikuStreamedHttpUTF8CSVReader |
| 4 | +import json |
| 5 | +import time |
| 6 | +from .metrics import ComputedMetrics |
| 7 | + |
| 8 | +class DSSMLTaskSettings(object): |
| 9 | + def __init__(self, client, project_key, analysis_id, mltask_id, mltask_settings): |
| 10 | + self.client = client |
| 11 | + self.project_key = project_key |
| 12 | + self.analysis_id = analysis_id |
| 13 | + self.mltask_id = mltask_id |
| 14 | + self.mltask_settings = mltask_settings |
| 15 | + |
| 16 | + def get_raw(self): |
| 17 | + """Gets the raw settings. |
| 18 | + This returns a reference to the raw settings, not a copy. |
| 19 | + """ |
| 20 | + return self.mltask_settings |
| 21 | + |
| 22 | + def get_feature_preprocessing(self, feature_name): |
| 23 | + return self.mltask_settings["preprocessing"]["per_feature"][feature_name] |
| 24 | + |
| 25 | + def reject_feature(self, feature_name): |
| 26 | + self.get_feature_preprocessing(feature_name)["role"] = "REJECT" |
| 27 | + |
| 28 | + def use_feature(self, feature_name): |
| 29 | + self.get_feature_preprocessing(feature_name)["role"] = "INPUT" |
| 30 | + |
| 31 | + def get_algorithm_settings(self, algorithm_name): |
| 32 | + algorithm_remap = { |
| 33 | + "SVC_CLASSIFICATION" : "svc_classifier", |
| 34 | + "SGD_CLASSIFICATION" : "sgd_classifier", |
| 35 | + "SPARKLING_DEEP_LEARNING" : "deep_learning_sparkling", |
| 36 | + "SPARKLING_GBM" : "gbm_sparkling", |
| 37 | + "SPARKLING_RF" : "rf_sparkling", |
| 38 | + "SPARKLING_GLM" : "glm_sparkling", |
| 39 | + "SPARKLING_NB" : "nb_sparkling", |
| 40 | + "XGBOOST_CLASSIFICATION" : "xgboost", |
| 41 | + "XGBOOST_REGRESSION" : "xgboost", |
| 42 | + "MLLIB_LOGISTIC_REGRESSION" : "mllib_logit", |
| 43 | + "MLLIB_LINEAR_REGRESSION" : "mllib_linreg", |
| 44 | + "MLLIB_RANDOM_FOREST" : "mllib_rf" |
| 45 | + } |
| 46 | + if algorithm_name in algorithm_remap: |
| 47 | + algorithm_name = algorithm_remap[algorithm_name] |
| 48 | + |
| 49 | + return self.mltask_settings["modeling"][algorithm_name.lower()] |
| 50 | + |
| 51 | + def set_algorithm_enabled(self, algorithm_name, enabled): |
| 52 | + self.get_algorithm_settings(algorithm_name)["enabled"] = enabled |
| 53 | + |
| 54 | + def save(self): |
| 55 | + """Saves back these settings to the ML Task""" |
| 56 | + |
| 57 | + print("WILL SAVE: %s" % json.dumps(self.mltask_settings, indent=2)) |
| 58 | + |
| 59 | + self.client._perform_empty( |
| 60 | + "POST", "/projects/%s/models/lab/%s/%s/settings" % (self.project_key, self.analysis_id, self.mltask_id), |
| 61 | + body = self.mltask_settings) |
| 62 | + |
| 63 | +class DSSMLTask(object): |
| 64 | + def __init__(self, client, project_key, analysis_id, mltask_id): |
| 65 | + self.client = client |
| 66 | + self.project_key = project_key |
| 67 | + self.analysis_id = analysis_id |
| 68 | + self.mltask_id = mltask_id |
| 69 | + |
| 70 | + def wait_guess_complete(self): |
| 71 | + """ |
| 72 | + Waits for guess to be complete. This should be called immediately after the creation of a new ML Task, |
| 73 | + before calling ``get_settings`` or ``train`` |
| 74 | + """ |
| 75 | + while True: |
| 76 | + status = self.get_status() |
| 77 | + if status.get("guessing", "???") == False: |
| 78 | + break |
| 79 | + time.sleep(0.2) |
| 80 | + |
| 81 | + def wait_train_complete(self): |
| 82 | + """ |
| 83 | + Waits for train to be complete. |
| 84 | + """ |
| 85 | + while True: |
| 86 | + status = self.get_status() |
| 87 | + if status.get("training", "???") == False: |
| 88 | + break |
| 89 | + time.sleep(2) |
| 90 | + |
| 91 | + def get_status(self): |
| 92 | + """ |
| 93 | + Gets the status of this ML Task |
| 94 | +
|
| 95 | + :return: a dict |
| 96 | + """ |
| 97 | + return self.client._perform_json( |
| 98 | + "GET", "/projects/%s/models/lab/%s/%s/status" % (self.project_key, self.analysis_id, self.mltask_id)) |
| 99 | + |
| 100 | + |
| 101 | + def get_settings(self): |
| 102 | + """ |
| 103 | + Gets the settings of this ML Tasks |
| 104 | +
|
| 105 | + :return: a DSSMLTaskSettings object to interact with the settings |
| 106 | + """ |
| 107 | + settings = self.client._perform_json( |
| 108 | + "GET", "/projects/%s/models/lab/%s/%s/settings" % (self.project_key, self.analysis_id, self.mltask_id)) |
| 109 | + |
| 110 | + return DSSMLTaskSettings(self.client, self.project_key, self.analysis_id, self.mltask_id, settings) |
| 111 | + |
| 112 | + def start_train(self): |
| 113 | + """Starts asynchronously a new train session for this ML Task. |
| 114 | +
|
| 115 | + This returns immediately, before train is complete. To wait for train to complete, |
| 116 | + poll on ``get_status`` until ``training`` is False""" |
| 117 | + self.client._perform_empty( |
| 118 | + "POST", "/projects/%s/models/lab/%s/%s/train" % (self.project_key, self.analysis_id, self.mltask_id)) |
| 119 | + |
| 120 | + |
| 121 | + def get_trained_models_ids(self): |
| 122 | + status = self.get_status() |
| 123 | + return [x["id"] for x in status["fullModelIds"]] |
| 124 | + |
| 125 | + |
| 126 | + def get_trained_model_summary(self, id): |
| 127 | + obj = { |
| 128 | + "modelsIds" : [id] |
| 129 | + } |
| 130 | + return self.client._perform_json( |
| 131 | + "POST", "/projects/%s/models/lab/%s/%s/models-summaries" % (self.project_key, self.analysis_id, self.mltask_id), |
| 132 | + body = obj)[id] |
| 133 | + |
| 134 | + def deploy_to_flow(self, model_id, model_name, train_dataset, test_dataset=None, redo_optimization=True): |
| 135 | + obj = { |
| 136 | + "trainDatasetRef" : train_dataset, |
| 137 | + "testDatasetRef" : test_dataset, |
| 138 | + "modelName" : model_name, |
| 139 | + "redoOptimization": redo_optimization |
| 140 | + } |
| 141 | + return self.client._perform_json( |
| 142 | + "POST", "/projects/%s/models/lab/%s/%s/models/%s/actions/deployToFlow" % (self.project_key, self.analysis_id, self.mltask_id, model_id), |
| 143 | + body = obj) |
| 144 | + |
0 commit comments