Skip to content

cafe.method.FunctionBackend

cafe.method.FunctionBackend

Bases: Backend

Specific implementation of abstract Backend class using Python functions in now conda environment.

Source code in cafe/method/fate_function_backend.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class FunctionBackend(Backend):
    """Specific implementation of abstract Backend class using Python functions in now conda environment."""

    def __init__(self, function_name="comp1"):
        """Initialize the FunctionBackend class.

        Args:
            function_name (str, optional):  name of the function backend .
        """
        logger.debug("FunctionBackend __init__")

        self.function_name = function_name
        self.load_backend()

    def load_backend(self) -> None:
        """load backend from python function"""
        self._load_function(self.function_name)

    def run(self, fadata: FateAnnData, parameters: dict) -> None:
        """call the python function to get trajectory dict

        Args:
            fadata (FateAnnData): the input FateAnnData object for trajectory inference method
            parameters (dict):  the  parameters for trajectory inference method
        """
        parameters = self._get_parameters(fadata, parameters)
        adata = fadata.to_anndata(delete_trajectory=True)  # avoid other trajectory IO
        if settings.save_external_data:
            # register primary attributes, then extract external data after trajectory inference
            anndata_attribute = AnndataAttribute(adata)
            trajectory_dict = self.function(adata, **parameters)
            trajectory_dict["external_data"] = anndata_attribute.extract_external_data_dict(adata)
        else:
            trajectory_dict = self.function(adata, **parameters)
        fadata.add_trajectory_by_type(trajectory_dict)

    # TOOD: consider if __call__ is needed
    # def __call__(self, adata: AnnData, rewrite: bool = True, **parameters):
    #     """simplified version for self.run"""
    #     # transfer FateAnndata to AnnData to avoid other trajectory IO
    #     trajectory_dict = self.function(adata, **parameters)
    #     return trajectory_dict

    def __str__(self):
        return f"FunctionBackend:'{self.function_name}'"

    def install_pipy_package(self):
        # TODO: install the relevant package from pipy
        logger.debug("install_pipy_package")

__init__(function_name='comp1')

Initialize the FunctionBackend class.

Parameters:

Name Type Description Default
function_name str

name of the function backend .

'comp1'
Source code in cafe/method/fate_function_backend.py
12
13
14
15
16
17
18
19
20
21
def __init__(self, function_name="comp1"):
    """Initialize the FunctionBackend class.

    Args:
        function_name (str, optional):  name of the function backend .
    """
    logger.debug("FunctionBackend __init__")

    self.function_name = function_name
    self.load_backend()

load_backend()

load backend from python function

Source code in cafe/method/fate_function_backend.py
23
24
25
def load_backend(self) -> None:
    """load backend from python function"""
    self._load_function(self.function_name)

run(fadata, parameters)

call the python function to get trajectory dict

Parameters:

Name Type Description Default
fadata FateAnnData

the input FateAnnData object for trajectory inference method

required
parameters dict

the parameters for trajectory inference method

required
Source code in cafe/method/fate_function_backend.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def run(self, fadata: FateAnnData, parameters: dict) -> None:
    """call the python function to get trajectory dict

    Args:
        fadata (FateAnnData): the input FateAnnData object for trajectory inference method
        parameters (dict):  the  parameters for trajectory inference method
    """
    parameters = self._get_parameters(fadata, parameters)
    adata = fadata.to_anndata(delete_trajectory=True)  # avoid other trajectory IO
    if settings.save_external_data:
        # register primary attributes, then extract external data after trajectory inference
        anndata_attribute = AnndataAttribute(adata)
        trajectory_dict = self.function(adata, **parameters)
        trajectory_dict["external_data"] = anndata_attribute.extract_external_data_dict(adata)
    else:
        trajectory_dict = self.function(adata, **parameters)
    fadata.add_trajectory_by_type(trajectory_dict)