1
0
Fork 0
mirror of https://github.com/Findus23/halo_comparison.git synced 2024-09-19 16:03:50 +02:00
halo_comparison/utils.py

101 lines
2.7 KiB
Python
Raw Normal View History

2022-06-14 10:53:19 +02:00
from pathlib import Path
2022-07-29 13:08:05 +02:00
from string import ascii_uppercase
from typing import Tuple
2022-05-06 09:51:43 +02:00
import pandas as pd
2022-06-14 10:53:19 +02:00
import yaml
from matplotlib import pyplot as plt
2022-06-01 12:12:45 +02:00
from matplotlib.axes import Axes
from matplotlib.figure import Figure
2022-05-06 09:51:43 +02:00
2022-07-21 12:34:57 +02:00
waveforms = ["DB2", "DB4", "DB8", "shannon"]
2022-05-06 09:51:43 +02:00
def print_progress(i, total, extra_data=""):
print(
f"{i} of {total} ({extra_data})" + " " * 20,
end="\r" if i != total else "\n",
flush=True,
)
2022-05-06 09:51:43 +02:00
def memory_usage(df: pd.DataFrame):
bytes_used = df.memory_usage(index=True).sum()
return bytes_used / 1024 / 1024
2022-06-14 10:53:19 +02:00
def create_figure() -> Tuple[Figure, Axes]:
"""
helper function for matplotlib OOP interface with proper typing
"""
fig: Figure = plt.figure()
ax: Axes = fig.gca()
return fig, ax
2022-06-14 10:53:19 +02:00
def read_swift_config(dir: Path):
with (dir / "used_parameters.yml").open() as f:
return yaml.safe_load(f)
2022-07-05 15:41:01 +02:00
def print_wall_time(dir: Path):
"""
Attention: This idea is flawed as it only shows the wall time of the last time the simulation was restarted
"""
with (dir / "swift.log").open() as f:
2022-07-05 15:41:01 +02:00
last_line = f.readlines()[-1]
print(last_line)
assert "main: done. Bye." in last_line
seconds = float(last_line[1:].split("]")[0])
print(f"Runtime: {seconds / 60 / 60:.2f} hours")
2022-07-12 14:39:01 +02:00
2022-07-18 19:27:56 +02:00
def figsize_from_page_fraction(columns=1, height_to_width=3 / 4):
2022-07-12 14:39:01 +02:00
cm = 1 / 2.54 # centimeters in inches
# \printinunitsof{cm}\prntlen{\linewidth}
two_column_width = 17.85162 # cm
one_colum_width = 8.5744 # cm
upscale_factor = 1.3
width = two_column_width if columns == 2 else one_colum_width
height = width * height_to_width
return width * cm * upscale_factor, height * cm * upscale_factor
2022-07-18 19:27:56 +02:00
def rowcolumn_labels(axes, labels, isrow: bool, pad=5) -> None:
"""
based on https://stackoverflow.com/a/25814386/4398037
"""
axs = axes[:, 0] if isrow else axes[0]
for ax, label in zip(axs, labels):
ax: Axes
if isrow:
xy = (0, 0.5)
xytext = (-ax.yaxis.labelpad - pad, 0)
xycoords = ax.yaxis.label
ha = "right"
va = "center"
2022-07-18 19:27:56 +02:00
else:
xy = (0.5, 1)
xytext = (0, pad)
xycoords = "axes fraction"
ha = "center"
va = "baseline"
ax.annotate(
label,
xy=xy,
xytext=xytext,
xycoords=xycoords,
textcoords="offset points",
size="medium",
ha=ha,
va=va,
2022-08-18 14:21:47 +02:00
rotation=90 if isrow else 0
)
2022-07-29 13:08:05 +02:00
def tex_fmt(format_str: str, *args) -> str:
for i, arg in enumerate(args):
format_str = format_str.replace(ascii_uppercase[i] * 2, str(arg))
return format_str