Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nanoplotter/nanoplotter_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def scatter(

dot_plot.fig = fig
dot_plot.html = dot_plot.fig.to_html(full_html=False, include_plotlyjs="cdn")
dot_plot.save_json()
dot_plot.save(settings)
plots_made.append(dot_plot)

Expand Down Expand Up @@ -174,6 +175,7 @@ def scatter(

kde_plot.fig = fig
kde_plot.html = kde_plot.fig.to_html(full_html=False, include_plotlyjs="cdn")
kde_plot.save_json()
kde_plot.save(settings)
plots_made.append(kde_plot)

Expand Down Expand Up @@ -275,6 +277,7 @@ def scatter_legacy(
plt.subplots_adjust(top=0.90)
plot.fig.suptitle(title or f"{names[0]} vs {names[1]} plot", fontsize=25)
hex_plot.fig = plot
hex_plot.save_json()
hex_plot.save(settings)
plots_made.append(hex_plot)

Expand Down Expand Up @@ -313,6 +316,7 @@ def scatter_legacy(
plt.subplots_adjust(top=0.90)
plot.fig.suptitle(title or "{} vs {} plot".format(names[0], names[1]), fontsize=25)
dot_plot.fig = plot
dot_plot.save_json()
dot_plot.save(settings)
plots_made.append(dot_plot)

Expand Down Expand Up @@ -352,6 +356,7 @@ def scatter_legacy(
plt.subplots_adjust(top=0.90)
plot.fig.suptitle(title or "{} vs {} plot".format(names[0], names[1]), fontsize=25)
kde_plot.fig = plot
kde_plot.save_json()
kde_plot.save(settings)
plots_made.append(kde_plot)
else:
Expand Down Expand Up @@ -451,6 +456,7 @@ def length_plots(array, name, path, settings, title=None, n50=None, color="#4CB3

histogram.fig = fig
histogram.html = histogram.fig.to_html(full_html=False, include_plotlyjs="cdn")
histogram.save_json()
histogram.save(settings)

log_histogram = Plot(
Expand Down Expand Up @@ -502,6 +508,7 @@ def length_plots(array, name, path, settings, title=None, n50=None, color="#4CB3

log_histogram.fig = fig
log_histogram.html = log_histogram.fig.to_html(full_html=False, include_plotlyjs="cdn")
log_histogram.save_json()
log_histogram.save(settings)

plots.extend([histogram, log_histogram])
Expand Down Expand Up @@ -573,6 +580,7 @@ def yield_by_minimal_length_plot(array, name, path, settings, title=None, color=

yield_by_length.fig = fig
yield_by_length.html = yield_by_length.fig.to_html(full_html=False, include_plotlyjs="cdn")
yield_by_length.save_json()
yield_by_length.save(settings)

return yield_by_length
Expand Down
40 changes: 39 additions & 1 deletion nanoplotter/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
from kaleido.scopes.plotly import PlotlyScope
import logging
import plotly.io as pio


class Plot(object):
Expand Down Expand Up @@ -47,7 +48,7 @@ def save(self, settings):
if not settings["no_static"]:
try:
for fmt in settings["format"]:
self.save_static(fmt)
self.save_static2(fmt)
except (AttributeError, ValueError) as e:
p = os.path.splitext(self.path)[0] + ".png"
if os.path.exists(p):
Expand Down Expand Up @@ -83,3 +84,40 @@ def save_static(self, figformat):
logging.info(
f"Saved {self.path.replace('.html', '')} as {figformat} (or png for --legacy)"
)


def save_static2(self, figformat):
json_fig = pio.read_json(self.path.replace("html", "json"))
json_fig.write_image(self.path.replace("html", figformat), format=figformat)
logging.info(
f"Saved {self.path.replace('.html', '')} as {figformat} (or png for --legacy)"
)



def save_json(self, json_path=None):
"""
将 fig 对象保存为 JSON 文件。

参数:
json_path (str): JSON 文件的路径。如果未指定,则基于 self.path 构造路径。
"""
if not self.fig:
logging.error("无法保存 JSON 文件:fig 对象为空")
return

# 如果未指定 json_path,则基于 self.path 构造默认路径
if json_path is None:
json_path = self.path.replace(".html", ".json") # 替换 .html 为 .json

try:
# 将 fig 对象导出为 JSON 数据
json_data = self.fig.to_json()

# 写入 JSON 文件
with open(json_path, "w", encoding="utf-8") as f:
f.write(json_data)

logging.info(f"成功保存 JSON 文件: {json_path}")
except Exception as e:
logging.error(f"保存 JSON 文件失败: {e}")
Loading