Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
super(RBConfigurationVertexPartitionTest, self).setUp();
self.partition_type = leidenalg.RBConfigurationVertexPartition;
'to compute a neighborhood graph.'
)
adjacency = adata.uns['neighbors']['connectivities']
if restrict_to is not None:
restrict_key, restrict_categories = restrict_to
adjacency, restrict_indices = restrict_adjacency(
adata,
restrict_key,
restrict_categories,
adjacency,
)
# convert it to igraph
g = _utils.get_igraph_from_adjacency(adjacency, directed=directed)
# flip to the default partition type if not overriden by the user
if partition_type is None:
partition_type = leidenalg.RBConfigurationVertexPartition
# Prepare find_partition arguments as a dictionary,
# appending to whatever the user provided. It needs to be this way
# as this allows for the accounting of a None resolution
# (in the case of a partition variant that doesn't take it on input)
if use_weights:
partition_kwargs['weights'] = np.array(g.es['weight']).astype(np.float64)
partition_kwargs['n_iterations'] = n_iterations
partition_kwargs['seed'] = random_state
if resolution is not None:
partition_kwargs['resolution_parameter'] = resolution
# clustering proper
part = leidenalg.find_partition(g, partition_type, **partition_kwargs)
# store output into adata.obs
groups = np.array(part.membership)
if restrict_to is not None:
if key_added == 'louvain':
if metric not in ("cosine", "euclidean"):
raise ValueError("Metric only supports cosine and euclidean.")
self._sdm = SampleDistanceMatrix(x=x, d=d, metric=metric,
use_pdist=use_pdist,
sids=sids, fids=fids, nprocs=nprocs)
if graph is None:
knn_conn_mat = self._sdm.s_knn_connectivity_matrix(
k=k, use_pca=use_pca, use_hnsw=use_hnsw,
index_params=index_params, query_params=query_params,
verbose=verbose)
graph = SampleDistanceMatrix.knn_conn_mat_to_aff_graph(
knn_conn_mat, aff_scale=aff_scale)
if partition_method == "RBConfigurationVertexPartition":
la_part_cls = la.RBConfigurationVertexPartition
elif partition_method == "RBERVertexPartition":
la_part_cls = la.RBERVertexPartition
elif partition_method == "CPMVertexPartition":
la_part_cls = la.CPMVertexPartition
elif partition_method == "SignificanceVertexPartition":
la_part_cls = la.SignificanceVertexPartition
elif partition_method == "SurpriseVertexPartition":
la_part_cls = la.SurpriseVertexPartition
else:
raise ValueError(
"Unknown partition method: {}".format(partition_method))
la_res = la.find_partition(graph, la.RBConfigurationVertexPartition,
seed=random_state, weights='weight',
resolution_parameter=resolution)
# keep track of results and parameters
if num_starts is not None:
np.random.seed(seed)
partitions = []
quality = []
seeds = np.random.randint(10*num_starts, size=num_starts)
for seed in seeds:
if weighted:
temp_partition = leidenalg.find_partition(g,
leidenalg.RBConfigurationVertexPartition,
weights=g.es['weight'],
resolution_parameter=resolution,
seed=seed,
)
else:
temp_partition = leidenalg.find_partition(g,
leidenalg.RBConfigurationVertexPartition,
resolution_parameter=resolution,
seed=seed,
)
quality.append(temp_partition.quality())
partitions.append(temp_partition)
partition1 = partitions[np.argmax(quality)]
else:
if weighted:
partition1 = leidenalg.find_partition(g,
leidenalg.RBConfigurationVertexPartition,
weights=g.es['weight'],
resolution_parameter=resolution,
seed=seed,
)
else:
partition1 = leidenalg.find_partition(g,
""" Code from Ethan Armand and Wayne Doyle, ./mukamel_lab/mop
slightly modified by Fangming Xie 05/13/2019
"""
import leidenalg
ti = time.time()
if num_starts is not None:
np.random.seed(seed)
partitions = []
quality = []
seeds = np.random.randint(10*num_starts, size=num_starts)
for seed in seeds:
if weighted:
temp_partition = leidenalg.find_partition(g,
leidenalg.RBConfigurationVertexPartition,
weights=g.es['weight'],
resolution_parameter=resolution,
seed=seed,
)
else:
temp_partition = leidenalg.find_partition(g,
leidenalg.RBConfigurationVertexPartition,
resolution_parameter=resolution,
seed=seed,
)
quality.append(temp_partition.quality())
partitions.append(temp_partition)
partition1 = partitions[np.argmax(quality)]
else:
if weighted:
partition1 = leidenalg.find_partition(g,
def cluster_leiden(seq_nn, resolution=2):
# compute leiden clustering
partition = leidenalg.find_partition(seq_nn,
leidenalg.RBConfigurationVertexPartition,
resolution_parameter=resolution)
# extract cluster memberships into categorial
membership = np.array(partition.membership)
clusters = pd.Categorical(
values=membership.astype('U'),
categories=natsorted(np.unique(membership).astype('U')))
return clusters
if partition_method == "RBConfigurationVertexPartition":
la_part_cls = la.RBConfigurationVertexPartition
elif partition_method == "RBERVertexPartition":
la_part_cls = la.RBERVertexPartition
elif partition_method == "CPMVertexPartition":
la_part_cls = la.CPMVertexPartition
elif partition_method == "SignificanceVertexPartition":
la_part_cls = la.SignificanceVertexPartition
elif partition_method == "SurpriseVertexPartition":
la_part_cls = la.SurpriseVertexPartition
else:
raise ValueError(
"Unknown partition method: {}".format(partition_method))
la_res = la.find_partition(graph, la.RBConfigurationVertexPartition,
seed=random_state, weights='weight',
resolution_parameter=resolution)
# keep track of results and parameters
self._graph = graph
self._la_res = la_res
self._labs = la_res.membership
self._k = k
self._use_pca = use_pca
self._use_hnsw = use_hnsw
self._index_params = index_params
self._query_params = query_params
self._aff_scale = aff_scale