-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BBH pipeline: write eccentricity params to file, add CLI to compute params & plot #6468
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#!/usr/bin/env python | ||
|
||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
import logging | ||
from pathlib import Path | ||
from typing import Sequence, Union | ||
|
||
import click | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pandas as pd | ||
import yaml | ||
from matplotlib.colors import LogNorm | ||
from scipy.interpolate import CloughTocher2DInterpolator | ||
|
||
from spectre.Visualization.Plot import ( | ||
apply_stylesheet_command, | ||
show_or_save_plot_command, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def plot_eccentricity_control( | ||
ecc_params_files: Sequence[Union[str, Path]], | ||
): | ||
"""Plot eccentricity control iterations. | ||
|
||
\f | ||
Arguments: | ||
ecc_params_files: YAML files containing the output of | ||
'eccentricity_control_params()'. | ||
|
||
Returns: | ||
The figure containing the plot. | ||
""" | ||
# Load the eccentricity parameters | ||
ecc_params = [] | ||
for ecc_params_file in ecc_params_files: | ||
with open(ecc_params_file, "r") as open_file: | ||
ecc_params.append(yaml.safe_load(open_file)) | ||
ecc_params = pd.DataFrame(ecc_params) | ||
|
||
# Set up the figure | ||
fig = plt.figure(figsize=(4.5, 4)) | ||
plt.xlabel(r"Angular velocity $\Omega_0$") | ||
plt.ylabel(r"Expansion $\dot{a}_0$") | ||
plt.grid(zorder=0) | ||
plt.ticklabel_format(axis="both", style="sci", scilimits=(0, 0)) | ||
|
||
# Plot measured eccentricity contours | ||
ecc_norm = LogNorm(vmin=1e-4, vmax=1) | ||
if len(ecc_params) > 2: | ||
ecc_intrp = CloughTocher2DInterpolator( | ||
ecc_params[["Omega0", "Adot0"]], | ||
np.log10(ecc_params["Eccentricity"]), | ||
rescale=True, | ||
) | ||
x, y = np.meshgrid( | ||
np.linspace(*ecc_params["Omega0"].agg(["min", "max"]), 200), | ||
np.linspace(*ecc_params["Adot0"].agg(["min", "max"]), 200), | ||
) | ||
z = ecc_intrp(x, y) | ||
ecc_contours = plt.contourf( | ||
x, | ||
y, | ||
10**z, | ||
levels=10.0 ** np.arange(-4, 1), | ||
norm=ecc_norm, | ||
cmap="Blues", | ||
zorder=10, | ||
) | ||
else: | ||
ecc_contours = plt.cm.ScalarMappable( | ||
norm=ecc_norm, | ||
cmap="Blues", | ||
) | ||
plt.colorbar(ecc_contours, label="Eccentricity", ax=plt.gca()) | ||
cbar_ax = fig.axes[1] | ||
plt.gca().use_sticky_edges = False | ||
|
||
# Plot the path through parameter space | ||
plt.plot( | ||
ecc_params["Omega0"], | ||
ecc_params["Adot0"], | ||
color="black", | ||
zorder=20, | ||
) | ||
|
||
# Place a marker at each iteration | ||
scatter_kwargs = dict( | ||
color="white", | ||
marker="o", | ||
linewidth=1, | ||
edgecolor="black", | ||
s=100, | ||
zorder=25, | ||
) | ||
annotate_kwargs = dict( | ||
textcoords="offset points", | ||
xytext=(0, -0.5), | ||
ha="center", | ||
va="center", | ||
fontsize=8, | ||
zorder=30, | ||
) | ||
plt.scatter(ecc_params["Omega0"], ecc_params["Adot0"], **scatter_kwargs) | ||
cbar_ax.scatter( | ||
np.repeat(0.5, len(ecc_params)), | ||
ecc_params["Eccentricity"], | ||
**scatter_kwargs, | ||
) | ||
for i in range(len(ecc_params)): | ||
plt.annotate( | ||
f"{i}", | ||
ecc_params.iloc[i][["Omega0", "Adot0"]], | ||
**annotate_kwargs, | ||
) | ||
cbar_ax.annotate( | ||
f"{i}", (0.5, ecc_params["Eccentricity"][i]), **annotate_kwargs | ||
) | ||
|
||
# Plot location of the next iteration | ||
plt.plot( | ||
ecc_params.iloc[-1][["Omega0", "NewOmega0"]], | ||
ecc_params.iloc[-1][["Adot0", "NewAdot0"]], | ||
color="black", | ||
linestyle="dotted", | ||
zorder=20, | ||
) | ||
plt.scatter( | ||
ecc_params.iloc[-1]["NewOmega0"], | ||
ecc_params.iloc[-1]["NewAdot0"], | ||
color="black", | ||
marker="o", | ||
s=30, | ||
zorder=40, | ||
) | ||
return fig | ||
|
||
|
||
@click.command( | ||
name="eccentricity-control", help=plot_eccentricity_control.__doc__ | ||
) | ||
@click.argument( | ||
"ecc_params_files", | ||
nargs=-1, | ||
type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True), | ||
) | ||
@apply_stylesheet_command() | ||
@show_or_save_plot_command() | ||
def plot_eccentricity_control_command(**kwargs): | ||
return plot_eccentricity_control(**kwargs) | ||
|
||
|
||
if __name__ == "__main__": | ||
plot_eccentricity_control_command(help_option_names=["-h", "--help"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a quick glance because I'll adapt a lot of this to BNS at some point. Should the threshold be specifiable somewhere? In SpEC we've had a harder time getting lower eccs for high q & chi runs. So much so that sometimes in BFI we just override the BFI-based target ecc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes definitely, we can just make it a parameter, that's very easy to do