cascade.models#

class cascade.models.BasicModel(*args: Any, meta_prefix: List[Dict[Any, Any]] | str | None = None, **kwargs: Any)[source]#

Basic model is a more concrete version of an abstract Model class. It provides common interface for all ML solutions. For more flexible interface refer to Model class.

evaluate(x: Any, y: Any, metrics: List[Metric | Callable[[Any, Any], SupportsFloat]], *args: Any, **kwargs: Any) None[source]#

Receives x and y validation sequences. Passes x to the model’s predict method along with any args or kwargs needed. Then updates self.metrics with what objects in metrics return. metrics should contain Metric with compute() method or callables with the interface: f(true, predicted) -> metric_value, where metric_value is a scalar

Parameters:
  • x (Any) – Input of the model.

  • y (Any) – Desired output to compare with the values predicted.

  • metrics (List[Union[Metric, Callable[[Any, Any], MetricType]]]) – List of metrics or callables to compute metric values

fit(x: Any, y: Any, *args: Any, **kwargs: Any) None[source]#

Method to encapsulate training loops. May be provided with any training-related arguments.

classmethod load(path: str, check_hash: bool = True) BasicModel[source]#

Loads the model from path provided. Path should be a folder

load_artifact(path: str, *args: Any, **kwargs: Any) None[source]#

BasicModel implements this for compatibility. This method does nothing since there are no internal artifacts in BasicModel

predict(x: Any, *args: Any, **kwargs: Any) Any[source]#

Method to encapsulate inference. May include preprocessing steps to make model self-sufficient.

save(path: str) None[source]#

Saves model to the path provided Also copies any additional files in the model folder.

Path should be a folder, which will be created if not exists and saves there as model.pkl

save_artifact(path: str, *args: Any, **kwargs: Any) None[source]#

BasicModel implements this for compatibility. This method does nothing since there are no internal artifacts in BasicModel

class cascade.models.BasicModelModifier(model: Model, *args: Any, **kwargs: Any)[source]#

Interface to unify BasicModel and ModelModifier.

class cascade.models.Model(*args: Any, meta_prefix: List[Dict[Any, Any]] | str | None = None, **kwargs: Any)[source]#

Base class for any model. Used to provide unified interface to any model, store metadata including metrics.

__init__(*args: Any, meta_prefix: List[Dict[Any, Any]] | str | None = None, **kwargs: Any) None[source]#

Should be called in any successor - initializes default meta needed.

Successors may pass all of their parameters to superclass for it to be able to log them in meta. Everything that is worth to document about the model can be put either in params or meta_prefix

add_config()[source]#
add_file(path: str, missing_ok: bool = False) None[source]#

Add additional file artifact to the model Copy the file to the model folder when saving model.

Parameters:
  • path (str) – Path to the file to be copied. Can be missing at the time of the call, but should be present when calling save()

  • missing_ok (bool, optional) – If it is okay when the file does not exist. Raises an error if False, by default False

add_log()[source]#
add_log_callback(callback: Callable[[Model], None]) None[source]#

Registers a callback to be executed while logging metrics. Usually is used internally and is not initially intended as a public method

Parameters:

callback (Callable[[Model], None]) – A function that accepts a model

See also

cascade.models.Model.log_metrics

add_metric(metric: str | Metric, value: SupportsFloat | None = None, **kwargs: Any) None[source]#

Adds metric value to the model. If metric already exists in the list, updates its value.

Parameters:
  • metric (Union[str, Metric]) – Either metric name or metric object. If object, then second argument is ignored

  • value (Optional[MetricType]) – Metric value when metric is str, by default None

  • str (Any additional args will go to the Metric constructor for flexibility if metric is)

Raises:
  • ValueError – If in either value or metric.value is None

  • TypeError – If metric is of inappropriate type

evaluate(*args: Any, **kwargs: Any) None[source]#

Evaluates model against any metrics. Should not return any value, just populate self.metrics

fit(*args: Any, **kwargs: Any) None[source]#

Method to encapsulate training loops. May be provided with any training-related arguments.

get_meta() List[Dict[Any, Any]][source]#
Returns:

meta – A list where first element is this object’s metadata. All other elements represent the other stages of pipeline if present.

Meta can be anything that is worth to document about the object and its properties.

Meta is a list (see Meta type alias) to allow the formation of pipelines.

Return type:

Meta

Convenience function for linking datasets. Produces more readable meta files and records useful info without much hassle

Parameters:
  • ds (Dataset) – Dataset to link

  • name (Optional[str], optional) – Dataset name, by default None

  • split (Optional[str], optional) – Split if applicable, may be “train”, “test”, etc, by default None

  • line (Optional[DataLine], optional) – DataLine where this dataset is stored if applicable, by default None

classmethod load(path: str, *args: Any, **kwargs: Any) Model[source]#

Loads model from provided path

load_artifact(path: str, *args: Any, **kwargs: Any) None[source]#

Loads standalone model’s artifact using provided filepath and sets it inside the model

log() None[source]#

Sequentially calls every log callback. Use this if you want to make a checkpoint of a model from inside the model. Callback should be a function that given the model saves it. For example ModelLine.save method. ModelLine.add_model registers callback with only_meta=True automatically when creating a new model using create_model.

See also

cascade.models.ModelLine.add_model, cascade.models.Model.add_log_callback

predict(*args: Any, **kwargs: Any) Any[source]#

Method to encapsulate inference. May include preprocessing steps to make model self-sufficient.

save(path: str, *args: Any, **kwargs: Any) None[source]#

Does additional saving routines. Call this if you call save() in any subclass.

Creates the folder, copies file artifacts added by add_file automatically

Parameters:

path (str) – Path to the model folder

Raises:
  • ValueError – If the path is not a folder

  • FileNotFoundError – If the file that should be copied does not exists and it is not ok. See add_file for more info.

save_artifact(path: str, *args: Any, **kwargs: Any) None[source]#

Saves standalone model’s artifact using provided filepath

class cascade.models.ModelModifier(model: Model, *args: Any, **kwargs: Any)[source]#

The Dataset’s Modifier for Models. Can be used to chain two models in one.

__init__(model: Model, *args: Any, **kwargs: Any) None[source]#
Parameters:

model (Model) – A model to modify.

get_meta() List[Dict[Any, Any]][source]#
Returns:

meta – A list where first element is this object’s metadata. All other elements represent the other stages of pipeline if present.

Meta can be anything that is worth to document about the object and its properties.

Meta is a list (see Meta type alias) to allow the formation of pipelines.

Return type:

Meta