Skip to content

Commit 848972e

Browse files
committed
Added python docs for all methods.
1 parent 74a235e commit 848972e

File tree

1 file changed

+130
-2
lines changed

1 file changed

+130
-2
lines changed

ads/model/model_description.py

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
from oci.data_science.models import Metadata
88
import ads
9+
910
# from ads.common import logger
1011
import logging
1112

@@ -14,6 +15,16 @@
1415

1516

1617
class ModelDescription:
18+
"""
19+
Represents a model description for multi model artifacts. Provides methods to ease the process of creating such models.
20+
21+
Methods:
22+
- add(namespace, bucket, prefix=None, files=None): Adds information about objects to the model description JSON.
23+
- remove(namespace, bucket, prefix=None): Removes information about objects from the model description JSON.
24+
- show(): Displays the current model description JSON in a human-readable format.
25+
- build(): Builds the model description JSON and writes it to a file.
26+
- save(project_ocid, compartment_ocid, display_name=None): Saves the model to the Model Catalog of OCI Data Science service.
27+
"""
1728

1829
empty_json = {
1930
"version": "1.0",
@@ -22,6 +33,19 @@ class ModelDescription:
2233
}
2334

2435
def auth(self):
36+
"""
37+
Internal method that authenticates the model description instance by initializing OCI clients.
38+
39+
Parameters:
40+
- None
41+
42+
Returns:
43+
- None
44+
45+
Note:
46+
- This method retrieves authentication data using default signer from the `ads.common.auth` module.
47+
- The region information is extracted from the authentication data.
48+
"""
2549
authData = ads.common.auth.default_signer()
2650
signer = authData["signer"]
2751
self.region = authData["config"]["region"]
@@ -36,6 +60,29 @@ def auth(self):
3660
)
3761

3862
def __init__(self, model_ocid=None):
63+
"""
64+
Initializes a new instance of the ModelDescription class.
65+
66+
Parameters:
67+
- model_ocid (str, optional): The OCID (Oracle Cloud Identifier) of the model.
68+
If provided, retrieves the takes the model artifact content as starting point or else initializes from blank artifact.
69+
70+
Returns:
71+
- None
72+
73+
Raises:
74+
- json.JSONDecodeError: If there is an error decoding the JSON content retrieved
75+
from the backend.
76+
- Exception: If an unexpected error occurs while retrieving the model artifact content.
77+
78+
Note:
79+
- If `model_ocid` is provided, this method attempts to retrieve the model artifact
80+
content from the backend using the provided OCID. If successful, it initializes
81+
`modelDescriptionJson` with the retrieved content. If unsuccessful, it logs the
82+
error and raises an exception.
83+
- If `model_ocid` is not provided, `modelDescriptionJson` is initialized with an
84+
empty JSON structure.
85+
"""
3986

4087
self.region = ""
4188
self.auth()
@@ -62,6 +109,28 @@ def __init__(self, model_ocid=None):
62109
raise e
63110

64111
def add(self, namespace, bucket, prefix=None, files=None):
112+
"""
113+
Adds information about objects in a specified bucket to the model description JSON.
114+
115+
Parameters:
116+
- namespace (str): The namespace of the object storage.
117+
- bucket (str): The name of the bucket containing the objects.
118+
- prefix (str, optional): The prefix used to filter objects within the bucket. Defaults to None.
119+
- files (list of str, optional): A list of file names to include in the model description.
120+
If provided, only objects with matching file names will be included. Defaults to None.
121+
122+
Returns:
123+
- None
124+
125+
Raises:
126+
- ValueError: If no files are found to add to the model description.
127+
128+
Note:
129+
- If `files` is not provided, it retrieves information about all objects in the bucket.
130+
If `files` is provided, it only retrieves information about objects with matching file names.
131+
- If no objects are found to add to the model description, a ValueError is raised.
132+
"""
133+
65134
# Remove if the model already exists
66135
self.remove(namespace, bucket, prefix)
67136

@@ -137,6 +206,25 @@ def listObjectVersionsUnpaginated():
137206
)
138207

139208
def remove(self, namespace, bucket, prefix=None):
209+
"""
210+
Removes information about objects in a specified bucket from the model description JSON.
211+
212+
Parameters:
213+
- namespace (str): The namespace of the object storage.
214+
- bucket (str): The name of the bucket containing the objects.
215+
- prefix (str, optional): The prefix used to filter objects within the bucket. Defaults to None.
216+
217+
Returns:
218+
- None
219+
220+
Note:
221+
- This method removes information about objects in the specified bucket from the
222+
instance of the ModelDescription.
223+
- If a matching model (with the specified namespace, bucket name, and prefix) is found
224+
in the model description JSON, it is removed.
225+
- If no matching model is found, the method returns without making any changes.
226+
"""
227+
140228
def findModelIdx():
141229
for idx, model in enumerate(self.modelDescriptionJson["models"]):
142230
if (
@@ -155,9 +243,34 @@ def findModelIdx():
155243
self.modelDescriptionJson["models"].pop(modelSearchIdx)
156244

157245
def show(self):
246+
"""
247+
Displays the current model description JSON in a human-readable format.
248+
249+
Parameters:
250+
- None
251+
252+
Returns:
253+
- None
254+
255+
Note:
256+
- The JSON representation of the model description is formatted with an indentation
257+
of 4 spaces.
258+
"""
158259
logger.info(json.dumps(self.modelDescriptionJson, indent=4))
159260

160261
def build(self):
262+
"""
263+
Builds the model description JSON and writes it to a file.
264+
265+
Parameters:
266+
- None
267+
268+
Returns:
269+
- str: The absolute file path where the model description JSON is stored.
270+
271+
Note:
272+
- This method serializes the current model description attribute to a JSON file named 'resultModelDescription.json' with an indentation of 2 spaces.
273+
"""
161274
logger.info("Building...")
162275
file_path = "resultModelDescription.json"
163276
try:
@@ -171,10 +284,25 @@ def build(self):
171284
logger.error(
172285
f"An unexpected error occurred: {e}"
173286
) # Handle other unexpected exceptions
174-
logger.info("Model Artifact stored at location: 'resultModelDescription.json'")
287+
logger.info("Model Artifact stored successfully.")
175288
return os.path.abspath(file_path)
176289

177290
def save(self, project_ocid, compartment_ocid, display_name=None):
291+
"""
292+
Saves the model to the Model Catalog of Oracle Cloud Infrastructure (OCI) Data Science service.
293+
294+
Parameters:
295+
- project_ocid (str): The OCID (Oracle Cloud Identifier) of the OCI Data Science project.
296+
- compartment_ocid (str): The OCID of the compartment in which the model will be created.
297+
- display_name (str, optional): The display name for the created model. If not provided,
298+
a default display name indicating the creation timestamp is used. Defaults to None.
299+
300+
Returns:
301+
- str: The OCID of the created model.
302+
303+
Note:
304+
- The display name defaults to a string indicating the creation timestamp if not provided.
305+
"""
178306
display_name = (
179307
"Created by MMS SDK on "
180308
+ datetime.datetime.now(pytz.utc).strftime("%Y-%m-%d %H:%M:%S %Z")
@@ -196,5 +324,5 @@ def save(self, project_ocid, compartment_ocid, display_name=None):
196324
json.dumps(self.modelDescriptionJson),
197325
content_disposition='attachment; filename="modelDescription.json"',
198326
)
199-
logger.info("Successfully created model with OCID: ", model.data.id)
327+
logger.info(f"Successfully created model with OCID: {model.data.id}")
200328
return model.data.id

0 commit comments

Comments
 (0)