Skip to main content

types

Type hints, enums and protocols for the Bitfount libraries.

Classes

BaseDistributedModelProtocol

class BaseDistributedModelProtocol(*args, **kwargs):

Federated Model structural type that only specifies the methods.

The reason for this protocol is that issubclass checks with Protocols can only be performed if the Protocol only specifies methods and not attributes. We still want to specify the attributes in another protocol though for greater type safety, (both statically and dynamically) so we have this protocol that only specifies methods and another protocol that specifies the attributes.

Methods


apply_weight_updates

def apply_weight_updates(    self, weight_updates: Sequence[_Weights],)> collections.abc.Mapping:

Defined in DistributedModelMixIn.

deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Inherited from:

BaseModelProtocol.deserialize :

Deserialises the model.

deserialize_params

def deserialize_params(    self, serialized_weights: _SerializedWeights,)> collections.abc.Mapping:

Defined in DistributedModelMixIn.

diff_params

def diff_params(    self, old_params: _Weights, new_params: _Weights,)> collections.abc.Mapping:

Defined in DistributedModelMixIn.

evaluate

def evaluate(self)> EvaluateReturnType:

Defined in _BaseModel.

fit

def fit(    self, data: BaseSource, metrics: Optional[dict[str, Metric]] = None, **kwargs: Any,)> Optional[dict[str, str]]:

Defined in DistributedModelMixIn.

get_param_states

def get_param_states(self)> collections.abc.Mapping:

Defined in DistributedModelMixIn.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Inherited from:

BaseModelProtocol.initialise_model :

Initialises the model.

This method may set a databunch or one or more dataloaders.

log_

def log_(self, name: str, value: Any, **kwargs: Any)> Any:

Defined in DistributedModelMixIn.

predict

def predict(self, data: BaseSource, **kwargs: Any)> PredictReturnType:

Defined in _BaseModel.

reset_trainer

def reset_trainer(self)> None:

Defined in DistributedModelMixIn.

serialize

def serialize(self, filename: Union[str, os.PathLike])> None:

Defined in _BaseModel.

serialize_params

def serialize_params(self, weights: _Weights)> collections.abc.Mapping:

Defined in DistributedModelMixIn.

set_model_training_iterations

def set_model_training_iterations(self, iterations: int)> None:

Defined in DistributedModelMixIn.

tensor_precision

def tensor_precision(self)> +T_DTYPE:

Defined in DistributedModelMixIn.

update_params

def update_params(self, new_model_params: _Weights)> None:

Defined in DistributedModelMixIn.

BaseModelProtocol

class BaseModelProtocol(*args, **kwargs):

Protocol for models that can be used in model algorithms.

The protocol must only specify methods and not attributes to ensure it can be used in issubclass checks.

Methods


deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Deserialises the model.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Initialises the model.

This method may set a databunch or one or more dataloaders.

DistributedModelProtocol

class DistributedModelProtocol(*args, **kwargs):

Federated Model structural type.

This protocol should be implemented by classes that inherit from either BitfountModel, or both of _BaseModel and DistributedModelMixIn.

Variables

  • static class_name : str
  • static datastructure : DataStructure
  • static epochs : Optional[int]
  • static fields_dict : ClassVar[T_FIELDS_DICT]
  • static metrics : Optional[MutableMapping[str, Metric]]
  • static nested_fields : ClassVar[T_NESTED_FIELDS]
  • static param_clipping : Optional[dict[str, int]]
  • static schema : BitfountSchema
  • static steps : Optional[int]
  • initialised : bool - Should return True if initialise_model has been called.

Methods


apply_weight_updates

def apply_weight_updates(    self, weight_updates: Sequence[_Weights],)> collections.abc.Mapping:

Inherited from:

BaseDistributedModelProtocol.apply_weight_updates :

Defined in DistributedModelMixIn.

deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Inherited from:

BaseDistributedModelProtocol.deserialize :

Deserialises the model.

deserialize_params

def deserialize_params(    self, serialized_weights: _SerializedWeights,)> collections.abc.Mapping:

Inherited from:

BaseDistributedModelProtocol.deserialize_params :

Defined in DistributedModelMixIn.

diff_params

def diff_params(    self, old_params: _Weights, new_params: _Weights,)> collections.abc.Mapping:

Inherited from:

BaseDistributedModelProtocol.diff_params :

Defined in DistributedModelMixIn.

evaluate

def evaluate(self)> EvaluateReturnType:

Inherited from:

BaseDistributedModelProtocol.evaluate :

Defined in _BaseModel.

fit

def fit(    self, data: BaseSource, metrics: Optional[dict[str, Metric]] = None, **kwargs: Any,)> Optional[dict[str, str]]:

Inherited from:

BaseDistributedModelProtocol.fit :

Defined in DistributedModelMixIn.

get_param_states

def get_param_states(self)> collections.abc.Mapping:

Inherited from:

BaseDistributedModelProtocol.get_param_states :

Defined in DistributedModelMixIn.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Inherited from:

BaseDistributedModelProtocol.initialise_model :

Initialises the model.

This method may set a databunch or one or more dataloaders.

log_

def log_(self, name: str, value: Any, **kwargs: Any)> Any:

Inherited from:

BaseDistributedModelProtocol.log_ :

Defined in DistributedModelMixIn.

predict

def predict(self, data: BaseSource, **kwargs: Any)> PredictReturnType:

Inherited from:

BaseDistributedModelProtocol.predict :

Defined in _BaseModel.

reset_trainer

def reset_trainer(self)> None:

Inherited from:

BaseDistributedModelProtocol.reset_trainer :

Defined in DistributedModelMixIn.

serialize

def serialize(self, filename: Union[str, os.PathLike])> None:

Inherited from:

BaseDistributedModelProtocol.serialize :

Defined in _BaseModel.

serialize_params

def serialize_params(self, weights: _Weights)> collections.abc.Mapping:

Inherited from:

BaseDistributedModelProtocol.serialize_params :

Defined in DistributedModelMixIn.

set_model_training_iterations

def set_model_training_iterations(self, iterations: int)> None:

Inherited from:

BaseDistributedModelProtocol.set_model_training_iterations :

Defined in DistributedModelMixIn.

tensor_precision

def tensor_precision(self)> +T_DTYPE:

Inherited from:

BaseDistributedModelProtocol.tensor_precision :

Defined in DistributedModelMixIn.

update_params

def update_params(self, new_model_params: _Weights)> None:

Inherited from:

BaseDistributedModelProtocol.update_params :

Defined in DistributedModelMixIn.

EvaluableModelProtocol

class EvaluableModelProtocol(*args, **kwargs):

Protocol for models that can be evaluated.

Variables

  • static metrics : Optional[MutableMapping[str, Metric]]
  • initialised : bool - Should return True if initialise_model has been called.

Methods


deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Inherited from:

ModelProtocol.deserialize :

Deserialises the model.

evaluate

def evaluate(self)> EvaluateReturnType:

Evaluates the model on the validation set.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Inherited from:

ModelProtocol.initialise_model :

Initialises the model.

This method may set a databunch or one or more dataloaders.

EvaluateReturnType

class EvaluateReturnType(    preds: np.ndarray, targs: np.ndarray, keys: Optional[list[str]] = None,):

The type of return from model.evaluate() calls.

Contains the predictions made by the model and the targets that were actually expected. Additionally, for file-containing datasets, will contain the keys ( filenames) that were the source of each prediction.

preds and targs will be numpy arrays where the first or second dimension is the number of predictions/data entries that were evaluated on.

Variables

  • static keys : Optional[list]

Methods


msgpack_serialize

def msgpack_serialize(self)> bitfount.types._EvaluateReturnTypeDict:

Convert to dict, ready for msgpack serialization.

InferrableModelProtocol

class InferrableModelProtocol(*args, **kwargs):

Protocol for models that can be inferred on.

Subclasses

  • ONNXModel
  • bitfount.backends.pytorch.models.inference_model._BaseInferenceModel

Variables

  • initialised : bool - Should return True if initialise_model has been called.

Methods


deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Inherited from:

ModelProtocol.deserialize :

Deserialises the model.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Inherited from:

ModelProtocol.initialise_model :

Initialises the model.

This method may set a databunch or one or more dataloaders.

predict

def predict(self, data: BaseSource, **kwargs: Any)> PredictReturnType:

Runs inference on the datasource or pre-set dataloader.

ModelProtocol

class ModelProtocol(*args, **kwargs):

Protocol for models that can be initialised.

Variables

  • static datastructure : DataStructure
  • static schema : BitfountSchema
  • initialised : bool - Should return True if initialise_model has been called.

Methods


deserialize

def deserialize(self, content: Union[str, os.PathLike, bytes], **kwargs: Any)> None:

Inherited from:

BaseModelProtocol.deserialize :

Deserialises the model.

initialise_model

def initialise_model(    self,    data: Optional[BaseSource] = None,    data_splitter: Optional[DatasetSplitter] = None,    context: Optional[TaskContext] = None,)> None:

Inherited from:

BaseModelProtocol.initialise_model :

Initialises the model.

This method may set a databunch or one or more dataloaders.

PredictReturnType

class PredictReturnType(    preds: Union[list[np.ndarray], pd.DataFrame], keys: Optional[list[str]] = None,):

The type of return from model.predict() calls.

Contains the predictions made by the model. Additionally, for file-containing datasets, will contain the keys (filenames) that were the source of each prediction.

If preds is a list, either the number of elements in the list is the number of predictions, or each element has a 1st dimension that is the number of predictions.

Variables

  • static keys : Optional[list]

Methods


msgpack_serialize

def msgpack_serialize(self)> bitfount.types._PredictReturnTypeDict:

Convert to dict, ready for msgpack serialization.