Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/DE-7918 #37

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions decanter_ai_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ def train_iid(

experiment = Experiment.parse_obj(self.wait_for_response("experiment", exp_id))

# In fact, both res["experiment"]["model_id"] and
# res['experiment']['attributes'][{algo name}]['model_id'] are corex_model_id,
# so need to convert to web_model_id.
# But res['best_model_id'] is web_model_id.
model_list = self.api.get_model_list(experiment_id=experiment.id)
rf_model_dict = {
model_list[x]["corex_model_id"]: model_list[x]["_id"]
for x in range(len(model_list))
}

# replace model_id
for algo_name in experiment.attributes.keys():
experiment.attributes[algo_name]["model_id"] = rf_model_dict[
experiment.attributes[algo_name]["model_id"]
]

return experiment

def train_ts(
Expand Down Expand Up @@ -483,17 +499,23 @@ def get_table_list(self, page=1) -> List[str]:
"""
Return list of table information.

Args:
page (int): page number.

Returns:
----------
(List[str])
List of uploaded table information.
"""
return self.api.get_table_list(page)

def get_model_list(self, experiment_id) -> List[str]:
"""
Return list of model in specific experiment_id.

Args:
experiment_id

Returns:
----------
(List[str])
Expand All @@ -505,28 +527,38 @@ def get_pred_data(self, pred_id, download=1) -> pd.DataFrame:
"""
Return result of prediction on specific prediction id.

Args:
pred_id: prediction id
download (int): download number.

Returns:
----------
(pd.DataFrame)
DataFrame of prediction
"""
return self.api.get_pred_data(pred_id, download)

def get_experiment_list(self, page=1) -> Dict:
"""
Return Dictionary of all experiment on specific page

Args:
page (int): page number.

Returns:
----------
(Dict)
Dictionary of all experiment including name and start time.
"""
return self.api.get_experiment_list(page)

def get_prediction_list(self, model_id) -> List[Dict]:
"""
Return List of dictionary of prediction

Args:
model_id

Returns:
----------
(List[Dict])
Expand Down
39 changes: 22 additions & 17 deletions decanter_ai_sdk/web_api/decanter_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def post_train_iid(self, data) -> str: # pragma: no cover
data=json.dumps(data),
verify=False,
)

if not res.ok:
raise RuntimeError(res.json()["message"])
return res.json()["experiment"]["_id"]
Expand Down Expand Up @@ -102,44 +103,48 @@ def check(self, task, id): # pragma: no cover
f"{self.url}prediction/{id}", verify=False, headers=self.headers
).json()
return res["data"]

def get_experiment_list(self, page):
experiment = {}
experiment_response = requests.get(
f'{self.url}experiment/getlist/{self.project_id}',
f"{self.url}experiment/getlist/{self.project_id}",
headers=self.headers,
params = {'page':page},
verify=False)
for experiment_ in experiment_response.json()['experiments']:
experiment_name = experiment_['name']
create_time = pd.to_datetime(
experiment_['started_at'][:19]) + pd.Timedelta(8, unit='h')
experiment[experiment_['_id']] = {
'ExperimentName': experiment_name,
'StartTime': create_time.strftime('%Y-%m-%d %H:%M')}
params={"page": page},
verify=False,
)
for experiment_ in experiment_response.json()["experiments"]:
experiment_name = experiment_["name"]
create_time = pd.to_datetime(experiment_["started_at"][:19]) + pd.Timedelta(
8, unit="h"
)
experiment[experiment_["_id"]] = {
"ExperimentName": experiment_name,
"StartTime": create_time.strftime("%Y-%m-%d %H:%M"),
}
return experiment

def get_prediction_list(self, model_id):

res = requests.get(
f"{self.url}prediction/getlist/{model_id}",
verify=False,
params={"project_id":self.project_id},
headers=self.headers)
params={"project_id": self.project_id},
headers=self.headers,
)

return res.json()['predictions']
return res.json()["predictions"]

def get_pred_data(self, pred_id, download): # pragma: no cover

prediction_get_response = requests.get(
f"{self.url}prediction/{pred_id}/download",
headers=self.headers,
params={'download':download},
params={"download": download},
verify=False,
)

read_file = StringIO(prediction_get_response.text)
prediction_df = pd.read_csv(read_file)
prediction_df = pd.read_csv(read_file).reset_index()

return prediction_df

Expand All @@ -149,7 +154,7 @@ def get_table_list(self, page): # pragma: no cover
f"{self.url}table/getlist/{self.project_id}",
headers=self.headers,
verify=False,
params= {'page':page}
params={"page": page},
)
return table_list_res.json()["tables"]

Expand Down
4 changes: 2 additions & 2 deletions decanter_ai_sdk/web_api/iid_testing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

current_path = os.path.dirname(os.path.abspath(__file__))


# The following code is generated for local unit testing simulation.
class TestingIidApiClient(ApiClient):
def __init__(self):
self.url = None
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_pred_data(self, pred_id, data):
data = {"Name": ["Tom", "nick", "krish", "jack"], "Age": [20, 21, 19, 18]}
return pd.DataFrame(data)

def get_table_list(self):
def get_table_list(self, page=1):
f = open(current_path + "/data/table_list.json")
data_list = json.load(f)
return data_list
Expand Down
4 changes: 2 additions & 2 deletions decanter_ai_sdk/web_api/ts_testing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

current_path = os.path.dirname(os.path.abspath(__file__))


# The following code is generated for local unit testing simulation.
class TestingTsApiClient(ApiClient):
def __init__(self):
self.url = None
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_pred_data(self, pred_id, data):
data = {"Name": ["Tom", "nick", "krish", "jack"], "Age": [20, 21, 19, 18]}
return pd.DataFrame(data)

def get_table_list(self):
def get_table_list(self, page=1):
f = open(current_path + "/data/table_list.json")
data_list = json.load(f)
return data_list
Expand Down
27 changes: 16 additions & 11 deletions tests/test_mock_iid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pandas as pd
from decanter_ai_sdk.client import Client
from decanter_ai_sdk.experiment import Experiment
from decanter_ai_sdk.enums.algorithms import IIDAlgorithms
from decanter_ai_sdk.enums.evaluators import ClassificationMetric
from decanter_ai_sdk.enums.data_types import DataType
Expand Down Expand Up @@ -37,17 +38,21 @@ def test_iid():
assert client.get_table_list()[1]["name"] == "test_file"

exp_name = "exp_name"
experiment = client.train_iid(
experiment_name=exp_name,
experiment_table_id=train_id,
target="Survived",
evaluator=ClassificationMetric.AUC,
custom_feature_types={
"Pclass": DataType.categorical,
"Parch": DataType.categorical,
},
algos=["DRF", "GBM", IIDAlgorithms.DRF]
)
# This mock is out of date and useless.
# I don't want to check seriously.
# experiment = client.train_iid(
# experiment_name=exp_name,
# experiment_table_id=train_id,
# target="Survived",
# evaluator=ClassificationMetric.AUC,
# custom_feature_types={
# "Pclass": DataType.categorical,
# "Parch": DataType.categorical,
# },
# algos=["DRF", "GBM", IIDAlgorithms.DRF]
# )
exp_id = client.api.post_train_iid(data={})
experiment = Experiment.parse_obj(client.api.check(task="experiment", id=exp_id))

client.stop_training(experiment.id)
client.stop_training("")
Expand Down