From e557d908ec19066c052b129094ff2f9644663f15 Mon Sep 17 00:00:00 2001 From: william-lukusa Date: Fri, 5 Mar 2021 12:04:32 +0100 Subject: [PATCH 1/4] Wrapper public api mdg --- dataikuapi/dss/ml.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/dataikuapi/dss/ml.py b/dataikuapi/dss/ml.py index dac73af4..4c672d8d 100644 --- a/dataikuapi/dss/ml.py +++ b/dataikuapi/dss/ml.py @@ -1752,6 +1752,84 @@ def get_diagnostics(self): diagnostics = self.details.get("trainDiagnostics", {}) return [DSSMLDiagnostic(d) for d in diagnostics.get("diagnostics", [])] + def export_documentation(self, folder_id=None, path=None): + """ + Start the export of the model documentation from a template docx file in a managed folder, + or from the default template if no folder id and path are specified. + + :param folder_id: (optional) the id of the managed folder + :param path: (optional) the path to the file from the root of the folder + :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process + """ + if bool(folder_id) != bool(path): + raise ValueError("Both folder id and path arguments are required to use a template from folder. Use without argument to export using the default template") + + export_mode_url = "default-template" if folder_id is None and path is None else "template-in-folder" + + if self.mltask is not None: + f = self.mltask.client._perform_json( + "POST", "/projects/%s/models/lab/%s/%s/models/%s/export-documentation-from-%s" % + (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id, export_mode_url), + params={"folderId": folder_id, "path": path}) + return DSSFuture(self.mltask.client, f["jobId"]) + else: + f = self.saved_model.client._perform_json( + "POST", "/projects/%s/savedmodels/%s/versions/%s/export-documentation-from-%s" % + (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version, export_mode_url), + params={"folderId": folder_id, "path": path}) + return DSSFuture(self.saved_model.client, job_id=f["jobId"]) + + def export_documentation_from_custom_template(self, fp): + """ + Start the export of the model documentation from a docx template (as a file object). + + :param object fp: A file-like object pointing to a template docx file + :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process + """ + files = {'file': fp} + if self.mltask is not None: + f = self.mltask.client._perform_json( + "POST", "/projects/%s/models/lab/%s/%s/models/%s/model-document/export-documentation-from-custom-template" % + (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id), + files=files) + return DSSFuture(self.mltask.client, f["jobId"]) + else: + f = self.saved_model.client._perform_json( + "POST", "/projects/%s/savedmodels/%s/versions/%s/export-documentation-from-custom-template" % + (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version), + files=files) + return DSSFuture(self.saved_model.client, job_id=f["jobId"]) + + def download_documentation_stream(self, export_id): + """ + Download a exported model documentation, as a binary stream. + + Warning: this stream will monopolize the DSSClient until closed. + + :param exportId: the template id of the generated model documentation returned as the result of the future + :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process + """ + if self.mltask is not None: + return self.mltask.client._perform_raw( + "GET", "/projects/%s/models/lab/download-documentation/%s" % (self.mltask.project_key, export_id)) + else: + return self.saved_model.client._perform_raw( + "GET", "/projects/%s/savedmodels/download-documentation/%s" % (self.saved_model.project_key, export_id)) + + def download_documentation_to_file(self, export_id, path): + """ + Download an exported model documentation into the given output file. + + :param export_id: the template id of the generated model documentation returned as the result of the future + :param path: the path where to download the model documentation + :return: None + """ + stream = self.download_documentation_stream(export_id) + with open(path, 'wb') as f: + for chunk in stream.iter_content(chunk_size=10000): + if chunk: + f.write(chunk) + f.flush() class DSSMLDiagnostic(object): """ @@ -2462,6 +2540,7 @@ def get_scoring_pmml_stream(self): "GET", "/projects/%s/savedmodels/%s/versions/%s/scoring-pmml" % (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version)) + ## Post-train computations def compute_subpopulation_analyses(self, split_by, wait=True, sample_size=1000, random_state=1337, n_jobs=1, debug_mode=False): From 57c5840e2ada922b066e114073a8fddd792d3e6c Mon Sep 17 00:00:00 2001 From: william-lukusa Date: Fri, 16 Apr 2021 15:01:19 +0200 Subject: [PATCH 2/4] Fix url --- dataikuapi/dss/ml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataikuapi/dss/ml.py b/dataikuapi/dss/ml.py index 4c672d8d..a5485103 100644 --- a/dataikuapi/dss/ml.py +++ b/dataikuapi/dss/ml.py @@ -1789,7 +1789,7 @@ def export_documentation_from_custom_template(self, fp): files = {'file': fp} if self.mltask is not None: f = self.mltask.client._perform_json( - "POST", "/projects/%s/models/lab/%s/%s/models/%s/model-document/export-documentation-from-custom-template" % + "POST", "/projects/%s/models/lab/%s/%s/models/%s/export-documentation-from-custom-template" % (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id), files=files) return DSSFuture(self.mltask.client, f["jobId"]) From 7b3d0c34310cee434471b90ec043410d6b29d0bd Mon Sep 17 00:00:00 2001 From: william-lukusa Date: Mon, 19 Apr 2021 09:33:37 +0200 Subject: [PATCH 3/4] match changes to download url made on public api --- dataikuapi/dss/ml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dataikuapi/dss/ml.py b/dataikuapi/dss/ml.py index a5485103..187aa9cd 100644 --- a/dataikuapi/dss/ml.py +++ b/dataikuapi/dss/ml.py @@ -1811,10 +1811,10 @@ def download_documentation_stream(self, export_id): """ if self.mltask is not None: return self.mltask.client._perform_raw( - "GET", "/projects/%s/models/lab/download-documentation/%s" % (self.mltask.project_key, export_id)) + "GET", "/projects/%s/models/lab/documentations/%s" % (self.mltask.project_key, export_id)) else: return self.saved_model.client._perform_raw( - "GET", "/projects/%s/savedmodels/download-documentation/%s" % (self.saved_model.project_key, export_id)) + "GET", "/projects/%s/savedmodels/documentations/%s" % (self.saved_model.project_key, export_id)) def download_documentation_to_file(self, export_id, path): """ From d0ef4cbfcab9826ce97c42db762f7133a5edcfd8 Mon Sep 17 00:00:00 2001 From: william-lukusa Date: Wed, 28 Apr 2021 14:29:36 +0200 Subject: [PATCH 4/4] Apply renaming from public api; Improve documentation --- dataikuapi/dss/ml.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/dataikuapi/dss/ml.py b/dataikuapi/dss/ml.py index 187aa9cd..98d72e7a 100644 --- a/dataikuapi/dss/ml.py +++ b/dataikuapi/dss/ml.py @@ -1752,9 +1752,9 @@ def get_diagnostics(self): diagnostics = self.details.get("trainDiagnostics", {}) return [DSSMLDiagnostic(d) for d in diagnostics.get("diagnostics", [])] - def export_documentation(self, folder_id=None, path=None): + def generate_documentation(self, folder_id=None, path=None): """ - Start the export of the model documentation from a template docx file in a managed folder, + Start the model document generation from a template docx file in a managed folder, or from the default template if no folder id and path are specified. :param folder_id: (optional) the id of the managed folder @@ -1762,26 +1762,26 @@ def export_documentation(self, folder_id=None, path=None): :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process """ if bool(folder_id) != bool(path): - raise ValueError("Both folder id and path arguments are required to use a template from folder. Use without argument to export using the default template") + raise ValueError("Both folder id and path arguments are required to use a template from folder. Use without argument to generate the model documentation using the default template") - export_mode_url = "default-template" if folder_id is None and path is None else "template-in-folder" + template_mode_url = "default-template" if folder_id is None and path is None else "template-in-folder" if self.mltask is not None: f = self.mltask.client._perform_json( - "POST", "/projects/%s/models/lab/%s/%s/models/%s/export-documentation-from-%s" % - (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id, export_mode_url), + "POST", "/projects/%s/models/lab/%s/%s/models/%s/generate-documentation-from-%s" % + (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id, template_mode_url), params={"folderId": folder_id, "path": path}) return DSSFuture(self.mltask.client, f["jobId"]) else: f = self.saved_model.client._perform_json( - "POST", "/projects/%s/savedmodels/%s/versions/%s/export-documentation-from-%s" % - (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version, export_mode_url), + "POST", "/projects/%s/savedmodels/%s/versions/%s/generate-documentation-from-%s" % + (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version, template_mode_url), params={"folderId": folder_id, "path": path}) return DSSFuture(self.saved_model.client, job_id=f["jobId"]) - def export_documentation_from_custom_template(self, fp): + def generate_documentation_from_custom_template(self, fp): """ - Start the export of the model documentation from a docx template (as a file object). + Start the model document generation from a docx template (as a file object). :param object fp: A file-like object pointing to a template docx file :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process @@ -1789,24 +1789,24 @@ def export_documentation_from_custom_template(self, fp): files = {'file': fp} if self.mltask is not None: f = self.mltask.client._perform_json( - "POST", "/projects/%s/models/lab/%s/%s/models/%s/export-documentation-from-custom-template" % + "POST", "/projects/%s/models/lab/%s/%s/models/%s/generate-documentation-from-custom-template" % (self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id), files=files) return DSSFuture(self.mltask.client, f["jobId"]) else: f = self.saved_model.client._perform_json( - "POST", "/projects/%s/savedmodels/%s/versions/%s/export-documentation-from-custom-template" % + "POST", "/projects/%s/savedmodels/%s/versions/%s/generate-documentation-from-custom-template" % (self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version), files=files) return DSSFuture(self.saved_model.client, job_id=f["jobId"]) def download_documentation_stream(self, export_id): """ - Download a exported model documentation, as a binary stream. + Download a model documentation, as a binary stream. Warning: this stream will monopolize the DSSClient until closed. - :param exportId: the template id of the generated model documentation returned as the result of the future + :param export_id: the id of the generated model documentation returned as the result of the future :return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process """ if self.mltask is not None: @@ -1818,9 +1818,9 @@ def download_documentation_stream(self, export_id): def download_documentation_to_file(self, export_id, path): """ - Download an exported model documentation into the given output file. + Download a model documentation into the given output file. - :param export_id: the template id of the generated model documentation returned as the result of the future + :param export_id: the id of the generated model documentation returned as the result of the future :param path: the path where to download the model documentation :return: None """