Skip to content
Snippets Groups Projects
Commit ec415549 authored by Blaise Li's avatar Blaise Li
Browse files

Lighter plots, more robust tex-escaping.

Rasterizing might make pdfs lighter.

Escaping underscore for tex-compatibility is now idempotent.

Also added a missing tex-compatibility escaping.
parent c76f9e07
No related branches found
No related tags found
No related merge requests found
......@@ -87,9 +87,17 @@ def strip_split(text):
return split(strip(text), "\t")
# import inspect
def texscape(text):
"""Escapes underscores to make a latex-compatible text."""
return sub("_", r"\_", text)
# https://stackoverflow.com/a/2654130/1878788
# currframe = inspect.currentframe()
# callframe = inspect.getouterframes(currframe)
# print(f"Escaping {text}\n(Called by {callframe})")
# print(f"Escaping {text}")
# return sub("_", r"\_", text)
# To avoid double escape:
return sub(r"([^\\])_", r"\1\_", text)
def ensure_relative(path, basedir):
......@@ -302,7 +310,7 @@ def save_plot(outfile,
plot_func,
*args,
title=None, format=None,
tight=True, equal_axes=False, square=False,
tight=True, equal_axes=False, square=False, rasterize=False,
**kwargs):
"""*format* is needed when using multiple pages output."""
# https://stackoverflow.com/a/10154763/1878788
......@@ -333,7 +341,7 @@ def save_plot(outfile,
if format is None:
plt.savefig(outfile, **save_kwds)
else:
plt.savefig(outfile, format=format, **save_kwds)
plt.savefig(outfile, format=format, rasterize=rasterize, **save_kwds)
plt.close(plt.gcf())
......
......@@ -108,6 +108,8 @@ from sklearn import preprocessing
from sklearn.decomposition import PCA
import matplotlib as mpl
# To be able to run the script without a defined $DISPLAY
# https://github.com/mwaskom/seaborn/issues/1262
#mpl.use("agg")
mpl.use("PDF")
#mpl.rcParams["figure.figsize"] = 2, 4
mpl.rcParams["font.sans-serif"] = [
......@@ -1767,6 +1769,8 @@ rule make_fold_heatmap:
gene_colours.name = f"{wildcards.small_type}_{wildcards.fold_type}"
#all_folds.drop(["cosmid", "name", "small_type"], axis=1)
#all_folds[all_folds.columns.difference(["cosmid", "name", "small_type"])]
# https://github.com/mwaskom/seaborn/issues/1262
#mpl.use("agg")
try:
#labels_dict = merge_with(tuple, small_type2colour, DefaultCounter(all_folds.small_type))
# https://stackoverflow.com/a/47396625/1878788
......@@ -1776,10 +1780,11 @@ rule make_fold_heatmap:
save_plot(
output.fold_heatmap, plot_fold_heatmap,
all_folds.drop(["cosmid", "name", "small_type"], axis=1),
gene_colours, labels_dict, tight=False)
gene_colours, labels_dict, tight=False, rasterize=True)
except ValueError as e:
print(labels_dict)
raise
#mpl.use("PDF")
#def plot_norm_counts(counts_data, summaries):
......@@ -3348,6 +3353,9 @@ def plot_text(outfile, text, title=None):
# https://stackoverflow.com/a/21833883/1878788
txt.set_clip_on(False)
if title is not None:
usetex = mpl.rcParams.get("text.usetex", False)
if usetex:
title = texscape(title)
plt.title(title)
plt.tight_layout()
plt.savefig(outfile)
......@@ -3363,8 +3371,8 @@ def plot_ACGT(data, xlabel, ylabel):
letter2legend = dict(zip("ACGTN", "ACGTN"))
usetex = mpl.rcParams.get("text.usetex", False)
if usetex:
xlabel = sub("_", r"\_", xlabel)
ylabel = sub("_", r"\_", ylabel)
xlabel = texscape(xlabel)
ylabel = texscape(ylabel)
data.columns = [texscape(colname) for colname in data.columns]
for (read_len, base_props) in data.iterrows():
x_shift = -0.25
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment