[docs]classBaseCost(abc.ABC):"""Base class for :mod:`moscot.costs`. Parameters ---------- adata Annotated data object. attr Attribute of :class:`~anndata.AnnData`. key Key in the attribute of :class:`~anndata.AnnData`. dist_key Key which determines into which source/target subset ``adata`` belongs. Useful when :attr:`attr = 'uns' <anndata.AnnData.uns>`. """def__init__(self,adata:AnnData,attr:str,key:str,dist_key:Optional[Union[Any,Tuple[Any,Any]]]=None):self._adata=adataself._attr=attrself._key=keyself._dist_key=dist_key@abc.abstractmethoddef_compute(self,*args:Any,**kwargs:Any)->ArrayLike:pass
[docs]def__call__(self,*args:Any,**kwargs:Any)->ArrayLike:"""Compute the cost matrix. Parameters ---------- args Positional arguments. kwargs Keyword arguments. Returns ------- The cost matrix. """cost=self._compute(*args,**kwargs)ifnp.any(np.isnan(cost)):maxx=np.nanmax(cost)logger.warning(f"Cost matrix contains `{np.sum(np.isnan(cost))}` NaN values, "f"setting them to the maximum value `{maxx}`.")cost=np.nan_to_num(cost,nan=maxx)# type: ignore[call-overload]ifnp.any(cost<0):raiseValueError(f"Cost matrix contains `{np.sum(cost<0)}` negative values.")returncost
@propertydefadata(self)->AnnData:"""Annotated data object."""returnself._adata