Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.alpha, self.beta, self.gamma, self.scaling, self.t_, self.alpha_ = None, None, None, None, None, None
self.u0_, self.s0_, self.weights, self.weights_outer, self.weights_upper, = None, None, None, None, None
self.t, self.tau, self.o, self.tau_, self.likelihood, self.loss, self.pars = None, None, None, None, None, None, None
self.max_iter = max_iter
# partition to total of 5 fitting procedures (t_ and alpha, scaling, rates, t_, all together)
self.simplex_kwargs = {'method': 'Nelder-Mead', 'options': {'maxiter': int(self.max_iter / 5)}}
self.perc = perc
self.recoverable = True
try:
self.initialize_weights()
except:
self.recoverable = False
logg.warn(f'Model for {self.gene} could not be instantiated.')
self.refit_time = fit_time
self.assignment_mode = None
self.steady_state_ratio = None
self.steady_state_prior = steady_state_prior
self.fit_scaling = fit_scaling
self.fit_steady_states = fit_steady_states
self.fit_connected_states = fit_connected_states
self.connectivities = get_connectivities(adata) if self.fit_connected_states is True else self.fit_connected_states
self.high_pars_resolution = high_pars_resolution
self.init_vals = init_vals
# for differential kinetic test
self.clusters, self.cats, self.varx, self.orth_beta = None, None, None, None
def verify_roots(adata, roots):
if "gene_count_corr" in adata.var.keys():
p = get_plasticity_score(adata)
p_ub, root_ub = p > 0.5, roots > 0.9
n_right_assignments = np.sum(root_ub * p_ub) / np.sum(p_ub)
n_false_assignments = np.sum(root_ub * np.invert(p_ub)) / np.sum(
np.invert(p_ub)
)
n_randn_assignments = np.mean(root_ub)
if n_right_assignments > 3 * n_randn_assignments: # mu + 2*mu (std=mu)
roots *= p_ub
elif (
n_false_assignments > n_randn_assignments
or n_right_assignments < n_randn_assignments
):
logg.warn("Uncertain or fuzzy root cell identification. Please verify.")
return roots
s_genes_, g2m_genes_ = get_phase_marker_genes(adata)
if s_genes is None: s_genes = s_genes_
if g2m_genes is None: g2m_genes = g2m_genes_
ctrl_size = min(len(s_genes), len(g2m_genes))
score_genes(adata, gene_list=s_genes, score_name='S_score', ctrl_size=ctrl_size, **kwargs) # add s-score
score_genes(adata, gene_list=g2m_genes, score_name='G2M_score', ctrl_size=ctrl_size, **kwargs) # add g2m-score
scores = adata.obs[['S_score', 'G2M_score']]
phase = pd.Series('S', index=scores.index) # default phase is S
phase[scores.G2M_score > scores.S_score] = 'G2M' # if G2M is higher than S, it's G2M
phase[np.all(scores < 0, axis=1)] = 'G1' # if all scores are negative, it's G1...
adata.obs['phase'] = phase
logg.hint(' \'S_score\' and \'G2M_score\', scores of cell cycle phases (adata.obs)')
return adata if copy else None
_residual = self._residual
# velocity genes
if self._r2_adjusted:
_offset, _gamma = leastsq_NxN(Ms, Mu, fit_offset)
_residual = self._Mu - _gamma * self._Ms
if fit_offset: _residual -= _offset
self._r2 = R_squared(_residual, total=self._Mu - self._Mu.mean(0))
self._velocity_genes = (self._r2 > self._min_r2) & (self._gamma > .01) & \
(np.max(self._Ms > 0, 0) > 0) & (np.max(self._Mu > 0, 0) > 0)
if np.sum(self._velocity_genes) < 2:
min_r2 = np.percentile(self._r2, 80)
self._velocity_genes = self._r2 > min_r2
logg.warn('You seem to have very low signal in splicing dynamics.\n'
f'The correlation threshold has been reduced to {np.round(min_r2,4)}\n'
def var_df(adata, keys, layer=None):
lookup_keys = [k for k in keys if k in adata.obs_names]
if len(lookup_keys) < len(keys):
logg.warn(
f"Keys {[k for k in keys if k not in adata.obs_names]} "
f"were not found in `adata.obs_names`."
)
df = pd.DataFrame(index=adata.var_names)
for l in lookup_keys:
df[l] = adata.var_vector(l, layer=layer)
return df
gene_subset = np.ones(adata.n_vars, dtype=bool)
if _min_counts is not None or _max_counts is not None:
gene_subset &= filter(X, min_counts=_min_counts, max_counts=_max_counts)[0]
if _min_cells is not None or _max_cells is not None:
gene_subset &= filter(X, min_cells=_min_cells, max_cells=_max_cells)[0]
adata._inplace_subset_var(gene_subset)
s = np.sum(~gene_subset)
if s > 0:
logg.info(f"Filtered out {s} genes that are detected", end=" ")
if _min_cells is not None or _min_counts is not None:
logg.info(
f"in less than {_min_cells} cells ({layer})."
if _min_counts is None
else f"{_min_counts} counts ({layer}).",
no_indent=True,
)
if max_cells is not None or max_counts is not None:
logg.info(
f"in more than {_max_cells} cells ({layer})."
if _max_counts is None
else f"{_max_counts} counts ({layer}).",
no_indent=True,
)
return adata if copy else None
V -= V.mean(1)[:, None]
V_norm = norm(V)
R = np.zeros(adata.n_obs)
indices = get_indices(dist=get_neighs(adata, "distances"))[0]
for i in range(adata.n_obs):
Vi_neighs = V[indices[i]]
Vi_neighs -= Vi_neighs.mean(1)[:, None]
R[i] = np.mean(
np.einsum("ij, j", Vi_neighs, V[i]) / (norm(Vi_neighs) * V_norm[i])[None, :]
)
adata.obs[f"{vkey}_length"] = V_norm.round(2)
adata.obs[f"{vkey}_confidence"] = np.clip(R, 0, None)
logg.hint(f"added '{vkey}_length' (adata.obs)")
logg.hint(f"added '{vkey}_confidence' (adata.obs)")
if f"{vkey}_confidence_transition" not in adata.obs.keys():
velocity_confidence_transition(adata, vkey)
return adata if copy else None