Skip to content

Commit

Permalink
Remove calls to .format (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Dec 10, 2024
1 parent ef92866 commit 3f329bb
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 28 deletions.
4 changes: 1 addition & 3 deletions src/scanpy/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def _type_check(var: Any, varname: str, types: type | tuple[type, ...]):
possible_types_str = types.__name__
else:
type_names = [t.__name__ for t in types]
possible_types_str = "{} or {}".format(
", ".join(type_names[:-1]), type_names[-1]
)
possible_types_str = f"{', '.join(type_names[:-1])} or {type_names[-1]}"
raise TypeError(f"{varname} must be of type {possible_types_str}")


Expand Down
4 changes: 2 additions & 2 deletions src/scanpy/external/exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def _write_color_tracks(ctracks, fname):


def _frac_to_hex(frac):
rgb = tuple(np.array(np.array(plt.cm.jet(frac)[:3]) * 255, dtype=int))
return "#{:02x}{:02x}{:02x}".format(*rgb)
r, g, b = tuple(np.array(np.array(plt.cm.jet(frac)[:3]) * 255, dtype=int))
return f"#{r:02x}{g:02x}{b:02x}"


def _get_color_stats_genes(color_stats, E, gene_list):
Expand Down
4 changes: 2 additions & 2 deletions src/scanpy/external/tl/_phenograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def phenograph(
comm_key = (
f"pheno_{clustering_algo}" if clustering_algo in ["louvain", "leiden"] else ""
)
ig_key = "pheno_{}_ig".format("jaccard" if jaccard else "gaussian")
q_key = "pheno_{}_q".format("jaccard" if jaccard else "gaussian")
ig_key = f"pheno_{'jaccard' if jaccard else 'gaussian'}_ig"
q_key = f"pheno_{'jaccard' if jaccard else 'gaussian'}_q"

communities, graph, Q = phenograph.cluster(
data=data,
Expand Down
8 changes: 4 additions & 4 deletions src/scanpy/plotting/_tools/paga.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,11 @@ def _paga_graph(
and isinstance(node_labels, str)
and node_labels != adata.uns["paga"]["groups"]
):
raise ValueError(
"Provide a list of group labels for the PAGA groups {}, not {}.".format(
adata.uns["paga"]["groups"], node_labels
)
msg = (
"Provide a list of group labels for the PAGA groups "
f"{adata.uns['paga']['groups']}, not {node_labels}."
)
raise ValueError(msg)
groups_key = adata.uns["paga"]["groups"]
if node_labels is None:
node_labels = adata.obs[groups_key].cat.categories
Expand Down
21 changes: 8 additions & 13 deletions src/scanpy/preprocessing/_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,21 @@ def describe_var(
if issparse(X):
X.eliminate_zeros()
var_metrics = pd.DataFrame(index=adata.var_names)
var_metrics["n_cells_by_{expr_type}"], var_metrics["mean_{expr_type}"] = (
var_metrics[f"n_cells_by_{expr_type}"], var_metrics[f"mean_{expr_type}"] = (
materialize_as_ndarray((axis_nnz(X, axis=0), _get_mean_var(X, axis=0)[0]))
)
if log1p:
var_metrics["log1p_mean_{expr_type}"] = np.log1p(
var_metrics["mean_{expr_type}"]
var_metrics[f"log1p_mean_{expr_type}"] = np.log1p(
var_metrics[f"mean_{expr_type}"]
)
var_metrics["pct_dropout_by_{expr_type}"] = (
1 - var_metrics["n_cells_by_{expr_type}"] / X.shape[0]
var_metrics[f"pct_dropout_by_{expr_type}"] = (
1 - var_metrics[f"n_cells_by_{expr_type}"] / X.shape[0]
) * 100
var_metrics["total_{expr_type}"] = np.ravel(axis_sum(X, axis=0))
var_metrics[f"total_{expr_type}"] = np.ravel(axis_sum(X, axis=0))
if log1p:
var_metrics["log1p_total_{expr_type}"] = np.log1p(
var_metrics["total_{expr_type}"]
var_metrics[f"log1p_total_{expr_type}"] = np.log1p(
var_metrics[f"total_{expr_type}"]
)
# Relabel
new_colnames = []
for col in var_metrics.columns:
new_colnames.append(col.format(**locals()))
var_metrics.columns = new_colnames
if inplace:
adata.var[var_metrics.columns] = var_metrics
return None
Expand Down
7 changes: 4 additions & 3 deletions src/scanpy/tools/_rank_genes_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ def __init__(
)

if len(invalid_groups_selected) > 0:
raise ValueError(
"Could not calculate statistics for groups {} since they only "
"contain one sample.".format(", ".join(invalid_groups_selected))
msg = (
f"Could not calculate statistics for groups {', '.join(invalid_groups_selected)} "
"since they only contain one sample."
)
raise ValueError(msg)

adata_comp = adata
if layer is not None:
Expand Down
2 changes: 1 addition & 1 deletion tests/notebooks/test_paga_paul15_subsampled.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ def test_paga_paul15_subsampled(image_comparer, plt):
show=False,
)
# add a test for this at some point
# data.to_csv('./write/paga_path_{}.csv'.format(descr))
# data.to_csv(f"./write/paga_path_{descr}.csv")

save_and_compare_images("paga_path")

0 comments on commit 3f329bb

Please sign in to comment.