Skip to main content

types

Type hints, enums and protocols for the Bitfount libraries.

Classes​

BaseDistributedModelProtocol​

class BaseDistributedModelProtocol(    datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any,):

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[str, bitfount.types._TensorLike]:

Defined in DistributedModelMixIn.

deserialize​

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

Inherited from:

BaseModelProtocol.deserialize :

Deserialises the model.

deserialize_params​

def deserialize_params(    self, serialized_weights: _SerializedWeights,) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

Defined in DistributedModelMixIn.

diff_params​

def diff_params(    self, old_params: _Weights, new_params: _Weights,) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

Defined in DistributedModelMixIn.

evaluate​

def evaluate(self) ‑> EvaluateReturnType:

Defined in _BaseModel.

fit​

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

Defined in DistributedModelMixIn.

get_param_states​

def get_param_states(self) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

Defined in DistributedModelMixIn.

initialise_model​

def initialise_model(    self,    data: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = 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: str | os.PathLike[str]) ‑> None:

Defined in _BaseModel.

serialize_params​

def serialize_params(    self, weights: _Weights,) ‑> collections.abc.Mapping[str, list[float]]:

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(    datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any,):

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: str | os.PathLike[str] | bytes, **kwargs: Any) ‑> None:

Deserialises the model.

initialise_model​

def initialise_model(    self,    data: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = None,) ‑> None:

Initialises the model.

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

DistributedModelProtocol​

class DistributedModelProtocol(    datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any,):

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 : int | None
  • static fields_dict : ClassVar[T_FIELDS_DICT]
  • static metrics : MutableMapping[str, Metric] | None
  • static nested_fields : ClassVar[T_NESTED_FIELDS]
  • static param_clipping : dict[str, int] | None
  • static schema : BitfountSchema
  • static steps : int | None
  • 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[str, bitfount.types._TensorLike]:

Inherited from:

BaseDistributedModelProtocol.apply_weight_updates :

Defined in DistributedModelMixIn.

deserialize​

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

Inherited from:

BaseDistributedModelProtocol.deserialize :

Deserialises the model.

deserialize_params​

def deserialize_params(    self, serialized_weights: _SerializedWeights,) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

Inherited from:

BaseDistributedModelProtocol.deserialize_params :

Defined in DistributedModelMixIn.

diff_params​

def diff_params(    self, old_params: _Weights, new_params: _Weights,) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

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: dict[str, Metric] | None = None, **kwargs: Any,) ‑> dict[str, str] | None:

Inherited from:

BaseDistributedModelProtocol.fit :

Defined in DistributedModelMixIn.

get_param_states​

def get_param_states(self) ‑> collections.abc.Mapping[str, bitfount.types._TensorLike]:

Inherited from:

BaseDistributedModelProtocol.get_param_states :

Defined in DistributedModelMixIn.

initialise_model​

def initialise_model(    self,    data: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = 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: str | os.PathLike[str]) ‑> None:

Inherited from:

BaseDistributedModelProtocol.serialize :

Defined in _BaseModel.

serialize_params​

def serialize_params(    self, weights: _Weights,) ‑> collections.abc.Mapping[str, list[float]]:

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(    datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any,):

Protocol for models that can be evaluated.

Variables​

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

Methods​


deserialize​

def deserialize(self, content: str | os.PathLike[str] | 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: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = 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: list[str] | None = 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 : list[str] | None

Methods​


msgpack_serialize​

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

Convert to dict, ready for msgpack serialization.

InferrableModelProtocol​

class InferrableModelProtocol(    datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any,):

Protocol for models that can be inferred on.

Subclasses​

Variables​

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

Methods​


deserialize​

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

Inherited from:

ModelProtocol.deserialize :

Deserialises the model.

initialise_model​

def initialise_model(    self,    data: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = 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(datastructure: DataStructure, schema: BitfountSchema, **kwargs: Any):

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: str | os.PathLike[str] | bytes, **kwargs: Any) ‑> None:

Inherited from:

BaseModelProtocol.deserialize :

Deserialises the model.

initialise_model​

def initialise_model(    self,    data: BaseSource | None = None,    data_splitter: DatasetSplitter | None = None,    context: TaskContext | None = 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: list[np.ndarray] | pd.DataFrame, keys: list[str] | None = 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 : list[str] | None

Methods​


msgpack_serialize​

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

Convert to dict, ready for msgpack serialization.