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): ...@@ -87,9 +87,17 @@ def strip_split(text):
return split(strip(text), "\t") return split(strip(text), "\t")
# import inspect
def texscape(text): def texscape(text):
"""Escapes underscores to make a latex-compatible 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): def ensure_relative(path, basedir):
...@@ -302,7 +310,7 @@ def save_plot(outfile, ...@@ -302,7 +310,7 @@ def save_plot(outfile,
plot_func, plot_func,
*args, *args,
title=None, format=None, title=None, format=None,
tight=True, equal_axes=False, square=False, tight=True, equal_axes=False, square=False, rasterize=False,
**kwargs): **kwargs):
"""*format* is needed when using multiple pages output.""" """*format* is needed when using multiple pages output."""
# https://stackoverflow.com/a/10154763/1878788 # https://stackoverflow.com/a/10154763/1878788
...@@ -333,7 +341,7 @@ def save_plot(outfile, ...@@ -333,7 +341,7 @@ def save_plot(outfile,
if format is None: if format is None:
plt.savefig(outfile, **save_kwds) plt.savefig(outfile, **save_kwds)
else: else:
plt.savefig(outfile, format=format, **save_kwds) plt.savefig(outfile, format=format, rasterize=rasterize, **save_kwds)
plt.close(plt.gcf()) plt.close(plt.gcf())
......
...@@ -108,6 +108,8 @@ from sklearn import preprocessing ...@@ -108,6 +108,8 @@ from sklearn import preprocessing
from sklearn.decomposition import PCA from sklearn.decomposition import PCA
import matplotlib as mpl import matplotlib as mpl
# To be able to run the script without a defined $DISPLAY # 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.use("PDF")
#mpl.rcParams["figure.figsize"] = 2, 4 #mpl.rcParams["figure.figsize"] = 2, 4
mpl.rcParams["font.sans-serif"] = [ mpl.rcParams["font.sans-serif"] = [
...@@ -1767,6 +1769,8 @@ rule make_fold_heatmap: ...@@ -1767,6 +1769,8 @@ rule make_fold_heatmap:
gene_colours.name = f"{wildcards.small_type}_{wildcards.fold_type}" gene_colours.name = f"{wildcards.small_type}_{wildcards.fold_type}"
#all_folds.drop(["cosmid", "name", "small_type"], axis=1) #all_folds.drop(["cosmid", "name", "small_type"], axis=1)
#all_folds[all_folds.columns.difference(["cosmid", "name", "small_type"])] #all_folds[all_folds.columns.difference(["cosmid", "name", "small_type"])]
# https://github.com/mwaskom/seaborn/issues/1262
#mpl.use("agg")
try: try:
#labels_dict = merge_with(tuple, small_type2colour, DefaultCounter(all_folds.small_type)) #labels_dict = merge_with(tuple, small_type2colour, DefaultCounter(all_folds.small_type))
# https://stackoverflow.com/a/47396625/1878788 # https://stackoverflow.com/a/47396625/1878788
...@@ -1776,10 +1780,11 @@ rule make_fold_heatmap: ...@@ -1776,10 +1780,11 @@ rule make_fold_heatmap:
save_plot( save_plot(
output.fold_heatmap, plot_fold_heatmap, output.fold_heatmap, plot_fold_heatmap,
all_folds.drop(["cosmid", "name", "small_type"], axis=1), 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: except ValueError as e:
print(labels_dict) print(labels_dict)
raise raise
#mpl.use("PDF")
#def plot_norm_counts(counts_data, summaries): #def plot_norm_counts(counts_data, summaries):
...@@ -3348,6 +3353,9 @@ def plot_text(outfile, text, title=None): ...@@ -3348,6 +3353,9 @@ def plot_text(outfile, text, title=None):
# https://stackoverflow.com/a/21833883/1878788 # https://stackoverflow.com/a/21833883/1878788
txt.set_clip_on(False) txt.set_clip_on(False)
if title is not None: if title is not None:
usetex = mpl.rcParams.get("text.usetex", False)
if usetex:
title = texscape(title)
plt.title(title) plt.title(title)
plt.tight_layout() plt.tight_layout()
plt.savefig(outfile) plt.savefig(outfile)
...@@ -3363,8 +3371,8 @@ def plot_ACGT(data, xlabel, ylabel): ...@@ -3363,8 +3371,8 @@ def plot_ACGT(data, xlabel, ylabel):
letter2legend = dict(zip("ACGTN", "ACGTN")) letter2legend = dict(zip("ACGTN", "ACGTN"))
usetex = mpl.rcParams.get("text.usetex", False) usetex = mpl.rcParams.get("text.usetex", False)
if usetex: if usetex:
xlabel = sub("_", r"\_", xlabel) xlabel = texscape(xlabel)
ylabel = sub("_", r"\_", ylabel) ylabel = texscape(ylabel)
data.columns = [texscape(colname) for colname in data.columns] data.columns = [texscape(colname) for colname in data.columns]
for (read_len, base_props) in data.iterrows(): for (read_len, base_props) in data.iterrows():
x_shift = -0.25 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