put plot routines into figsaver

This commit is contained in:
Valentin Boettcher 2022-04-07 16:34:37 +02:00
parent eed3efb25f
commit 5e03157862
3 changed files with 117 additions and 119 deletions

View file

@ -1,4 +1,9 @@
import figsaver as fs
from figsaver import (
plot_interaction_consistency,
plot_interaction_consistency_development,
plot_flow_bcf,
)
from hiro_models.one_qubit_model import QubitModel, StocProcTolerances
import hiro_models.model_auxiliary as aux
import numpy as np

View file

@ -3,6 +3,11 @@
* Boilerplate
#+begin_src jupyter-python :results none :tangle scripts/integrate_slip.py
import figsaver as fs
from figsaver import (
plot_interaction_consistency,
plot_interaction_consistency_development,
plot_flow_bcf,
)
from hiro_models.one_qubit_model import QubitModel, StocProcTolerances
import hiro_models.model_auxiliary as aux
import numpy as np
@ -25,123 +30,6 @@ Init ray and silence stocproc.
logging_setup(logging.INFO)
#+end_src
** Helper Functionality
*** Plotting Consistency
Interaction consistency for multiple models and a reference.
#+begin_src jupyter-python :results none
def plot_interaction_consistency(
models, reference=None, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs
):
fig, ax = plt.subplots()
if reference:
with aux.get_data(reference) as data:
reference_energy = reference.interaction_energy(data, **kwargs)
for model in models:
with aux.get_data(model) as data:
energy = model.interaction_energy(data, **kwargs)
interaction_ref = model.interaction_energy_from_conservation(data, **kwargs)
diff = abs(interaction_ref - energy)
self_consistency = (diff.value < diff.σ).sum() / len(diff.value[0]) * 100
if reference:
diff = abs(interaction_ref - reference_energy)
final_consistency = (
(diff.value < diff.σ).sum() / len(diff.value[0]) * 100
)
_, _, (line, _) = fs.plot_with_σ(
data.time[:],
energy,
ax=ax,
label=label_fn(model)
+ fr", (${self_consistency:.0f}\%$"
+ (fr", ${final_consistency:.0f}\%$)" if reference else ")"),
bath=0,
)
fs.plot_with_σ(
data.time[:],
interaction_ref,
ax=ax,
linestyle="--",
bath=0,
color=fs.lighten_color(line[0].get_color(), 0.8),
)
ax.set_xlabel("$τ$")
ax.set_ylabel(r"$\langle H_\mathrm{I}\rangle$")
ax.legend()
return fig, ax
#+end_src
Consistency for multiple sample counts.
Interaction consistency for multiple models and a reference.
#+begin_src jupyter-python :results none
def plot_interaction_consistency_development(
models, reference=None, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs
):
fig, ax = plt.subplots()
if reference:
with aux.get_data(reference) as data:
reference_energy = reference.interaction_energy(data, **kwargs)
for model in models:
with aux.get_data(model) as data:
interaction_ref = model.interaction_energy_from_conservation(
data, **kwargs
)
if reference:
diff = abs(interaction_ref - reference_energy)
else:
energy = model.interaction_energy(data, **kwargs)
diff = abs(interaction_ref - energy)
ns, values = [], []
for N, val, σ in diff:
ns.append(N)
values.append((val < σ).sum() / len(val[0]) * 100)
ax.plot(ns, values, linestyle="--", marker=".", label=label_fn(model))
ax.axhline(68, linestyle="-.", color="grey", alpha=.5)
ax.set_xlabel("$N$")
ax.set_ylabel(("" if reference else "Self-") + r"Consistency [$\%$]")
ax.legend()
return fig, ax
#+end_src
*** Flow vs BCF
#+begin_src jupyter-python :results none
def plot_flow_bcf(models, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs):
fig, ax = plt.subplots()
for model in models:
with aux.get_data(model) as data:
flow = model.bath_energy_flow(data, **kwargs)
_, _, (line, _) = fs.plot_with_σ(
data.time[:],
flow,
ax=ax,
label=label_fn(model),
bath=0,
transform=lambda y: -y,
)
ax.plot(
data.time[:],
-model.L_expect * model.bcf_scale * model.bcf(data.time[:]).imag,
linestyle="--",
color=line[0].get_color(),
)
return fig, ax
#+end_src
*** TODO Maybe integrate into =hiro-models=
* Global Definitions
We use a logspaced time to resolve the initial slip.
#+begin_src jupyter-python :results none :tangle scripts/integrate_slip.py

View file

@ -51,7 +51,7 @@ def scientific_round(val, *err, retprec=False):
val = np.ones_like(err) * val
i = np.floor(np.log10(err))
first_digit = (err // 10**i).astype(int)
first_digit = (err // 10 ** i).astype(int)
prec = (-i + np.ones_like(err) * (first_digit <= 3)).astype(int)
prec = np.max(prec, axis=1)
@ -136,7 +136,7 @@ def wrap_plot(f):
def get_figsize(
columnwidth: float,
wf: float = 0.5,
hf: float = (5.0**0.5 - 1.0) / 2.0,
hf: float = (5.0 ** 0.5 - 1.0) / 2.0,
) -> tuple[float, float]:
"""
:param wf: Width fraction in columnwidth units.
@ -253,6 +253,7 @@ def plot_with_σ(x, y, ax=None, transform=lambda y: y, bath=None, **kwargs):
return fancy_error(x, y_final, err, ax=ax, **kwargs)
@wrap_plot
def plot_diff_vs_sigma(
x,
@ -303,6 +304,110 @@ def plot_diff_vs_sigma(
ax.set_ylabel(rf"$|{{{ylabel}}}_{{\mathrm{{ref}}}}-{{{ylabel}}}_{{N_i}}|$")
def plot_interaction_consistency(
models, reference=None, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs
):
fig, ax = plt.subplots()
if reference:
with aux.get_data(reference) as data:
reference_energy = reference.interaction_energy(data, **kwargs)
for model in models:
with aux.get_data(model) as data:
energy = model.interaction_energy(data, **kwargs)
interaction_ref = model.interaction_energy_from_conservation(data, **kwargs)
diff = abs(interaction_ref - energy)
self_consistency = (diff.value < diff.σ).sum() / len(diff.value[0]) * 100
if reference:
diff = abs(interaction_ref - reference_energy)
final_consistency = (
(diff.value < diff.σ).sum() / len(diff.value[0]) * 100
)
_, _, (line, _) = fs.plot_with_σ(
data.time[:],
energy,
ax=ax,
label=label_fn(model)
+ fr", (${self_consistency:.0f}\%$"
+ (fr", ${final_consistency:.0f}\%$)" if reference else ")"),
bath=0,
)
fs.plot_with_σ(
data.time[:],
interaction_ref,
ax=ax,
linestyle="--",
bath=0,
color=fs.lighten_color(line[0].get_color(), 0.8),
)
ax.set_xlabel("$τ$")
ax.set_ylabel(r"$\langle H_\mathrm{I}\rangle$")
ax.legend()
return fig, ax
def plot_interaction_consistency_development(
models, reference=None, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs
):
fig, ax = plt.subplots()
if reference:
with aux.get_data(reference) as data:
reference_energy = reference.interaction_energy(data, **kwargs)
for model in models:
with aux.get_data(model) as data:
interaction_ref = model.interaction_energy_from_conservation(data, **kwargs)
if reference:
diff = abs(interaction_ref - reference_energy)
else:
energy = model.interaction_energy(data, **kwargs)
diff = abs(interaction_ref - energy)
ns, values = [], []
for N, val, σ in diff:
ns.append(N)
values.append((val < σ).sum() / len(val[0]) * 100)
ax.plot(ns, values, linestyle="--", marker=".", label=label_fn(model))
ax.axhline(68, linestyle="-.", color="grey", alpha=0.5)
ax.set_xlabel("$N$")
ax.set_ylabel(("" if reference else "Self-") + r"Consistency [$\%$]")
ax.legend()
return fig, ax
def plot_flow_bcf(models, label_fn=lambda model: f"$ω_c={model.ω_c:.2f}$", **kwargs):
fig, ax = plt.subplots()
for model in models:
with aux.get_data(model) as data:
flow = model.bath_energy_flow(data, **kwargs)
_, _, (line, _) = fs.plot_with_σ(
data.time[:],
flow,
ax=ax,
label=label_fn(model),
bath=0,
transform=lambda y: -y,
)
ax.plot(
data.time[:],
-model.L_expect * model.bcf_scale * model.bcf(data.time[:]).imag,
linestyle="--",
color=line[0].get_color(),
)
return fig, ax
###############################################################################
# SIDE EFFECTS #
###############################################################################