Coretex
coretex.coretex.model.model.Model Class Reference

Inherits NetworkObject.

Public Member Functions

str modelDescriptorFileName (cls)
 
Self createModel (cls, str name, int experimentId, float accuracy, Dict[str, Any] meta)
 
None saveModelDescriptor (cls, str path, Dict[str, Any] contents)
 
None download (self)
 
bool upload (self, str path)
 

Detailed Description

    Represents a machine learning model object on Coretex.ai
    Contains properties that describe the model

    Properties
    ----------
    name : str
        model name
    createdById : str
        id of model
    createdOn : datetime
        date of model creation
    datasetId : int
        dataset id that is used for training the model
    spaceId : int
        space id that is used for training the model
    projectId : int
        project id that is used for training the model
    isTrained : bool
        True if model is trained, False otherwise
    isDeleted : bool
        True if model is deleted, False otherwise
    accuracy : float
        model accuracy
    experimentId : int
        experiment id of trained model
    meta : Dict[str, Any]
        model meta data

Definition at line 33 of file model.py.

Member Function Documentation

◆ createModel()

Self coretex.coretex.model.model.Model.createModel (   cls,
str  name,
int  experimentId,
float  accuracy,
Dict[str, Any]  meta 
)
    Creates Model object of the provided experiment with specified properties

    Parameters
    ----------
    name : str
model name
    experimentId : int
experiment id of model
    accuracy : float
model accuracy
    meta : Dict[str, Any]
model meta data

    Returns
    -------
    Self -> Model object

    Example
    -------
    >>> from coretex import Model, ExecutingExperiment
    \b
    >>> experiment = ExecutingExperiment.current()
    >>> model = Model.createModel(
    name = experiment.name,
    experimentId = experiment.id,
    accuracy = 0.87,
    meta = {}
)

Definition at line 95 of file model.py.

95  def createModel(cls, name: str, experimentId: int, accuracy: float, meta: Dict[str, Any]) -> Self:
96  """
97  Creates Model object of the provided experiment with specified properties
98 
99  Parameters
100  ----------
101  name : str
102  model name
103  experimentId : int
104  experiment id of model
105  accuracy : float
106  model accuracy
107  meta : Dict[str, Any]
108  model meta data
109 
110  Returns
111  -------
112  Self -> Model object
113 
114  Example
115  -------
116  >>> from coretex import Model, ExecutingExperiment
117  \b
118  >>> experiment = ExecutingExperiment.current()
119  >>> model = Model.createModel(
120  name = experiment.name,
121  experimentId = experiment.id,
122  accuracy = 0.87,
123  meta = {}
124  )
125  """
126 
127  model = cls.create(parameters = {
128  "name": name,
129  "model_queue_id": experimentId,
130  "accuracy": accuracy,
131  "meta": meta
132  })
133 
134  if model is None:
135  raise ValueError(">> [Coretex] Failed to create Model entity")
136 
137  return model
138 

◆ download()

None coretex.coretex.model.model.Model.download (   self)
    Downloads and extracts the model zip file from Coretex.ai

Definition at line 179 of file model.py.

179  def download(self) -> None:
180  """
181  Downloads and extracts the model zip file from Coretex.ai
182  """
183 
184  if self.isDeleted or not self.isTrained:
185  return
186 
187  modelZipDestination = os.path.join(FolderManager.instance().modelsFolder, f"{self.id}.zip")
188 
189  modelFolderDestination = os.path.join(FolderManager.instance().modelsFolder, f"{self.id}")
190  if os.path.exists(modelFolderDestination):
191  return
192 
193  os.mkdir(modelFolderDestination)
194 
195  response = networkManager.genericDownload(
196  endpoint=f"model/download?id={self.id}",
197  destination=modelZipDestination
198  )
199 
200  if response.hasFailed():
201  logging.getLogger("coretexpylib").info(">> [Coretex] Failed to download the model")
202 
203  zipFile = ZipFile(modelZipDestination)
204  zipFile.extractall(modelFolderDestination)
205  zipFile.close()
206 

◆ modelDescriptorFileName()

str coretex.coretex.model.model.Model.modelDescriptorFileName (   cls)
    Returns
    -------
    str -> name of model descriptor file

Definition at line 78 of file model.py.

78  def modelDescriptorFileName(cls) -> str:
79  """
80  Returns
81  -------
82  str -> name of model descriptor file
83  """
84 
85  return "model_descriptor.json"
86 

◆ saveModelDescriptor()

None coretex.coretex.model.model.Model.saveModelDescriptor (   cls,
str  path,
Dict[str, Any]  contents 
)
Saves a model descriptor - a JSON file that provides a description of a
machine learning model. It includes information such as the model's
architecture, input and output shapes, labels, description and etc.

Example
-------
>>> from coretex import ExecutingExperiment, Model
>>> model = Model.createModel(experiment.name, experiment.id, accuracy, {})
>>> model.saveModelDescriptor(modelPath, {
"project_task": experiment.spaceTask,
"labels": labels,
"modelName": model.name,
"description": experiment.description,

"input_description":
    Input shape is [x, y]

    x is actually number of samples in dataset\n
    y represents number of unique taxons for selected level in dataset,

"input_shape": [x, y],

"output_description":
    Output shape - [x, z]

    x is actually number of samples in dataset\n
    z represents that output 2d array (table) is going to have only 1 column (1 prediction for each sample in dataset),

"output_shape": [x, z]
    })

Definition at line 140 of file model.py.

140  def saveModelDescriptor(cls, path: str, contents: Dict[str, Any]) -> None:
141  """
142  Saves a model descriptor - a JSON file that provides a description of a
143  machine learning model. It includes information such as the model's
144  architecture, input and output shapes, labels, description and etc.
145 
146  Example
147  -------
148  >>> from coretex import ExecutingExperiment, Model
149  >>> model = Model.createModel(experiment.name, experiment.id, accuracy, {})
150  >>> model.saveModelDescriptor(modelPath, {
151  "project_task": experiment.spaceTask,
152  "labels": labels,
153  "modelName": model.name,
154  "description": experiment.description,
155 
156  "input_description":
157  Input shape is [x, y]
158 
159  x is actually number of samples in dataset\n
160  y represents number of unique taxons for selected level in dataset,
161 
162  "input_shape": [x, y],
163 
164  "output_description":
165  Output shape - [x, z]
166 
167  x is actually number of samples in dataset\n
168  z represents that output 2d array (table) is going to have only 1 column (1 prediction for each sample in dataset),
169 
170  "output_shape": [x, z]
171  })
172  """
173 
174  modelDescriptorPath = os.path.join(path, cls.modelDescriptorFileName())
175 
176  with open(modelDescriptorPath, "w", encoding = "utf-8") as file:
177  json.dump(contents, file, ensure_ascii = False, indent = 4)
178 

◆ upload()

bool coretex.coretex.model.model.Model.upload (   self,
str  path 
)
    Uploads the model zip file to Coretex.ai

    Parameters
    ----------
    path : str
Path to the saved model dir

    Returns
    -------
    bool -> True if model data uploaded successfully, False if model data upload has failed


    Example
    -------
    >>> from coretex import Model, ExecutingExperiment
    \b
    >>> experiment: ExecutingExperiment[NetworkDataset] = ExecutingExperiment.current()
    >>> model = Model.createModel(
name = experiment.name,
experimentId = experiment.id,
accuracy = 0.87,
meta = {}
    )
    >>> if not model.upload("path/to/model-dir"):
    print("Failed to upload model")

Definition at line 207 of file model.py.

207  def upload(self, path: str) -> bool:
208  """
209  Uploads the model zip file to Coretex.ai
210 
211  Parameters
212  ----------
213  path : str
214  Path to the saved model dir
215 
216  Returns
217  -------
218  bool -> True if model data uploaded successfully, False if model data upload has failed
219 
220 
221  Example
222  -------
223  >>> from coretex import Model, ExecutingExperiment
224  \b
225  >>> experiment: ExecutingExperiment[NetworkDataset] = ExecutingExperiment.current()
226  >>> model = Model.createModel(
227  name = experiment.name,
228  experimentId = experiment.id,
229  accuracy = 0.87,
230  meta = {}
231  )
232  >>> if not model.upload("path/to/model-dir"):
233  print("Failed to upload model")
234  """
235 
236  if self.isDeleted:
237  return False
238 
239  logging.getLogger("coretexpylib").info(">> [Coretex] Uploading model file...")
240 
241  shutil.make_archive(path, "zip", path)
242 
243  files = {
244  ("file", open(f"{path}.zip", "rb"))
245  }
246 
247  parameters = {
248  "id": self.id
249  }
250 
251  response = networkManager.genericUpload("model/upload", files, parameters)
252  if response.hasFailed():
253  logging.getLogger("coretexpylib").info(">> [Coretex] Failed to upload model file")
254  else:
255  logging.getLogger("coretexpylib").info(">> [Coretex] Uploaded model file")
256 
257  return not response.hasFailed()

The documentation for this class was generated from the following file: