Skip to content

Index

amplify(sim, factor, row_pattern, col_pattern, param)

Multiply selected marginal entries by factor.

Source code in scdesigner/src/scdesigner/transform/transform.py
24
25
26
27
28
29
30
31
32
def amplify(sim, factor: float, row_pattern: str, col_pattern: str, param: str):
    """Multiply selected marginal entries by factor."""
    sim = copy.deepcopy(sim)
    df = sim.parameters["marginal"][param]
    matches = data_frame_mask(df, row_pattern, col_pattern).astype(float)
    mask = factor * matches + np.ones_like(matches)
    mat = sim.marginal.predict.coefs[param].detach().cpu().numpy() * mask
    _update_marginal_param(sim, param, mat)
    return sim

correlate(sim, factor, row_pattern, col_pattern, group=None)

Multiply selected off-diagonal entries by factor.

Source code in scdesigner/src/scdesigner/transform/transform.py
43
44
45
46
47
def correlate(sim, factor: float, row_pattern: str, col_pattern: str, group: Union[str, None] = None):
    """Multiply selected off-diagonal entries by factor."""
    corr_sim = copy.deepcopy(sim)
    corr_sim.copula.correlate(factor, row_pattern, col_pattern, group)
    return corr_sim

decorrelate(sim, row_pattern, col_pattern, group=None)

Zero out selected off-diagonal entries of a covariance.

Source code in scdesigner/src/scdesigner/transform/transform.py
35
36
37
38
39
40
def decorrelate(sim, row_pattern: str, col_pattern: str, group: Union[str, None] = None):
    """Zero out selected off-diagonal entries of a covariance.
    """
    decorr_sim = copy.deepcopy(sim)
    decorr_sim.copula.decorrelate(row_pattern, col_pattern, group)
    return decorr_sim

nullify(sim, row_pattern, col_pattern, param)

Nullify marginal parameters

Zero out entries matching (row_pattern, col_pattern) for the marginal param.

Source code in scdesigner/src/scdesigner/transform/transform.py
10
11
12
13
14
15
16
17
18
19
20
21
def nullify(sim, row_pattern: str, col_pattern: str, param: str):
    """Nullify marginal parameters

    Zero out entries matching (row_pattern, col_pattern) for the marginal `param`.
    """
    sim = copy.deepcopy(sim)
    df = sim.parameters["marginal"][param]
    matches = data_frame_mask(df, row_pattern, col_pattern)
    mask = (~matches).astype(float)
    mat = sim.marginal.predict.coefs[param].detach().cpu().numpy() * mask
    _update_marginal_param(sim, param, mat)
    return sim

replace_param(sim, path, new_param)

Substitute a new parameter for an old one.

Use the path to the parameter starting from sim.parameters to identify the parameter to transform. Examples: ['marginal','mean'] or ['copula','group_name']

Args: sim (Simulator): The simulator object. path (Sequence[str]): The path to the parameter to transform. new_param (np.ndarray): The new parameter to substitute. For replacing a covariance structure, new_param could be a numpy array of shape (n_genes, n_genes) or a CovarianceStructure object defined by the user.

Source code in scdesigner/src/scdesigner/transform/transform.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def replace_param(sim, path: Sequence[str], new_param: Union[np.ndarray, pd.DataFrame, CovarianceStructure]):
    """Substitute a new parameter for an old one.

    Use the path to the parameter starting from sim.parameters to identify the
    parameter to transform.  Examples: ['marginal','mean'] or
    ['copula','group_name']

    Args:
        sim (Simulator): The simulator object.
        path (Sequence[str]): The path to the parameter to transform.
        new_param (np.ndarray): The new parameter to substitute. 
        For replacing a covariance structure, new_param could be a numpy array of shape (n_genes, n_genes)
        or a CovarianceStructure object defined by the user.
    """
    sim = copy.deepcopy(sim)
    if path[0] == "marginal":
        param = path[1]
        mat = np.asarray(new_param)
        _update_marginal_param(sim, param, mat)

    if path[0] == "copula":
        if isinstance(new_param, np.ndarray):
            sim.parameters["copula"][path[1]] = CovarianceStructure(new_param,
                                                                    modeled_names=sim.adata.var_names)
        elif isinstance(new_param, pd.DataFrame):
            sim.parameters["copula"][path[1]] = CovarianceStructure(new_param.values,
                                                                    modeled_names=new_param.index)
        elif isinstance(new_param, CovarianceStructure):
            sim.parameters["copula"][path[1]] = new_param
        else:
            raise ValueError(f"new_param must be a numpy array or a CovarianceStructure object, got {type(new_param)}")
    return sim