Skip to content

Commit bc2828e

Browse files
Merge pull request #125 from dataiku/feature/dss902-mdg-public-api-wrapper
MDG Public API python wrapper
2 parents adb6ced + d0ef4cb commit bc2828e

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

dataikuapi/dss/ml.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,84 @@ def get_diagnostics(self):
18021802
diagnostics = self.details.get("trainDiagnostics", {})
18031803
return [DSSMLDiagnostic(d) for d in diagnostics.get("diagnostics", [])]
18041804

1805+
def generate_documentation(self, folder_id=None, path=None):
1806+
"""
1807+
Start the model document generation from a template docx file in a managed folder,
1808+
or from the default template if no folder id and path are specified.
1809+
1810+
:param folder_id: (optional) the id of the managed folder
1811+
:param path: (optional) the path to the file from the root of the folder
1812+
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process
1813+
"""
1814+
if bool(folder_id) != bool(path):
1815+
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")
1816+
1817+
template_mode_url = "default-template" if folder_id is None and path is None else "template-in-folder"
1818+
1819+
if self.mltask is not None:
1820+
f = self.mltask.client._perform_json(
1821+
"POST", "/projects/%s/models/lab/%s/%s/models/%s/generate-documentation-from-%s" %
1822+
(self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id, template_mode_url),
1823+
params={"folderId": folder_id, "path": path})
1824+
return DSSFuture(self.mltask.client, f["jobId"])
1825+
else:
1826+
f = self.saved_model.client._perform_json(
1827+
"POST", "/projects/%s/savedmodels/%s/versions/%s/generate-documentation-from-%s" %
1828+
(self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version, template_mode_url),
1829+
params={"folderId": folder_id, "path": path})
1830+
return DSSFuture(self.saved_model.client, job_id=f["jobId"])
1831+
1832+
def generate_documentation_from_custom_template(self, fp):
1833+
"""
1834+
Start the model document generation from a docx template (as a file object).
1835+
1836+
:param object fp: A file-like object pointing to a template docx file
1837+
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process
1838+
"""
1839+
files = {'file': fp}
1840+
if self.mltask is not None:
1841+
f = self.mltask.client._perform_json(
1842+
"POST", "/projects/%s/models/lab/%s/%s/models/%s/generate-documentation-from-custom-template" %
1843+
(self.mltask.project_key, self.mltask.analysis_id, self.mltask.mltask_id, self.mltask_model_id),
1844+
files=files)
1845+
return DSSFuture(self.mltask.client, f["jobId"])
1846+
else:
1847+
f = self.saved_model.client._perform_json(
1848+
"POST", "/projects/%s/savedmodels/%s/versions/%s/generate-documentation-from-custom-template" %
1849+
(self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version),
1850+
files=files)
1851+
return DSSFuture(self.saved_model.client, job_id=f["jobId"])
1852+
1853+
def download_documentation_stream(self, export_id):
1854+
"""
1855+
Download a model documentation, as a binary stream.
1856+
1857+
Warning: this stream will monopolize the DSSClient until closed.
1858+
1859+
:param export_id: the id of the generated model documentation returned as the result of the future
1860+
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the model document generation process
1861+
"""
1862+
if self.mltask is not None:
1863+
return self.mltask.client._perform_raw(
1864+
"GET", "/projects/%s/models/lab/documentations/%s" % (self.mltask.project_key, export_id))
1865+
else:
1866+
return self.saved_model.client._perform_raw(
1867+
"GET", "/projects/%s/savedmodels/documentations/%s" % (self.saved_model.project_key, export_id))
1868+
1869+
def download_documentation_to_file(self, export_id, path):
1870+
"""
1871+
Download a model documentation into the given output file.
1872+
1873+
:param export_id: the id of the generated model documentation returned as the result of the future
1874+
:param path: the path where to download the model documentation
1875+
:return: None
1876+
"""
1877+
stream = self.download_documentation_stream(export_id)
1878+
with open(path, 'wb') as f:
1879+
for chunk in stream.iter_content(chunk_size=10000):
1880+
if chunk:
1881+
f.write(chunk)
1882+
f.flush()
18051883

18061884
class DSSMLDiagnostic(object):
18071885
"""
@@ -2512,6 +2590,7 @@ def get_scoring_pmml_stream(self):
25122590
"GET", "/projects/%s/savedmodels/%s/versions/%s/scoring-pmml" %
25132591
(self.saved_model.project_key, self.saved_model.sm_id, self.saved_model_version))
25142592

2593+
25152594
## Post-train computations
25162595

25172596
def compute_subpopulation_analyses(self, split_by, wait=True, sample_size=1000, random_state=1337, n_jobs=1, debug_mode=False):

0 commit comments

Comments
 (0)