1
0
Fork 0
mirror of https://github.com/cosmo-sims/cosmICweb-music.git synced 2024-09-19 16:53:43 +02:00
cosmICweb-music/cosmicweb_music/cosmICweb.py

455 lines
15 KiB
Python
Raw Normal View History

2024-04-20 23:20:54 +02:00
from __future__ import annotations
2024-04-14 23:22:33 +02:00
import os
import sys
import tempfile
import subprocess
from typing import Any
from .data_types import Ellipsoid, Args, DownloadConfig
2024-04-17 18:32:29 +02:00
2024-04-14 23:22:33 +02:00
import click
import requests
from datetime import datetime
import logging
# Logger
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s %(levelname)-8s %(message)s", "%Y-%m-%d %H:%M:%S"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel("INFO")
# Some constants
2024-04-17 18:40:26 +02:00
DEFAULT_URL = "https://cosmicweb.eu"
2024-04-14 23:22:33 +02:00
EDITOR = os.environ.get("EDITOR", "vim")
EDITOR_IS_VIM = EDITOR in {"vim", "nvim"}
2024-04-14 23:22:33 +02:00
2024-04-17 19:26:28 +02:00
2024-04-20 23:20:54 +02:00
def query_yes_no(question: str, default="yes") -> bool:
2024-04-14 23:22:33 +02:00
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == "":
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
# Routines
2024-04-20 23:20:54 +02:00
def fetch_ellipsoids(url: str, api_token: str, attempts: int) -> list[Ellipsoid]:
2024-04-14 23:22:33 +02:00
for i in range(attempts):
try:
r = requests.get(url, headers={"Authorization": "Token " + api_token})
# This will raise an error if not successful
r.raise_for_status()
except requests.exceptions.HTTPError as e:
logging.warning("Failed fetching (attempt {}/{}) ...".format(i, attempts))
2024-04-17 18:40:05 +02:00
logging.warning(e)
2024-04-14 23:22:33 +02:00
else:
content = r.json()
return [
Ellipsoid(
center=e["ellips_center"],
shape=e["ellips_matrix"],
radius_definition=e["radius_definition"],
traceback_radius=e["traceback_radius"],
)
for e in content
]
logging.error("Unable to download ellipsoids from {}".format(url))
2024-04-20 23:20:54 +02:00
return []
2024-04-14 23:22:33 +02:00
2024-04-20 23:20:54 +02:00
def fetch_ellipsoid(
2024-04-22 15:17:35 +02:00
url: str, api_token: str, traceback_radius: float, attempts: int = 3
2024-04-20 23:20:54 +02:00
) -> Ellipsoid | None:
2024-04-14 23:22:33 +02:00
ellipsoids = fetch_ellipsoids(url, api_token, attempts)
2024-04-20 23:20:54 +02:00
if ellipsoids:
2024-04-14 23:22:33 +02:00
return next(
(e for e in ellipsoids if e.traceback_radius == traceback_radius), None
)
return None
2024-04-20 23:20:54 +02:00
def fetch_downloadstore(cosmicweb_url: str, target: str) -> DownloadConfig:
2024-04-14 23:22:33 +02:00
try:
r = requests.get(cosmicweb_url + "/api/music/store/" + target)
# This will raise an error if not successful
r.raise_for_status()
except requests.exceptions.HTTPError as e:
2024-04-17 18:40:05 +02:00
logging.critical(f"Failed downloading from cosmICweb.")
logging.critical(e)
sys.exit(1)
2024-04-14 23:22:33 +02:00
content = r.json()
sim = content["simulation"]
halo_urls = [
"{url}/simulation/{sid}/halo/{hid}".format(
url=sim["api_url"], sid=sim["api_id"], hid=h
)
for h in content["halos"]
]
return DownloadConfig(
simulation_name=sim["name"],
2024-04-19 17:43:41 +02:00
project_name=sim["project_name"],
2024-04-14 23:22:33 +02:00
halo_names=["halo_{}".format(h) for h in content["halos"]],
2024-04-19 17:43:41 +02:00
halo_ids=content["halos"],
2024-04-14 23:22:33 +02:00
halo_urls=halo_urls,
traceback_radius=content["traceback_radius"],
api_token=sim["api_token"],
MUSIC=sim["ics"],
settings=content["configuration"],
accessed_at=datetime.now(),
)
2024-04-27 19:11:16 +02:00
def fetch_multiple(
cosmicweb_url: str,
traceback_radius,
publication_name: str = None,
collection_uuid: str = None,
2024-04-20 23:20:54 +02:00
) -> DownloadConfig:
2024-04-27 19:11:16 +02:00
if publication_name:
url = f"{cosmicweb_url}/api/publications/{publication_name}"
elif collection_uuid:
url = f"{cosmicweb_url}/api/collections/{collection_uuid}"
else:
raise ValueError("must provide either publication_name or collection_uuid")
2024-04-14 23:22:33 +02:00
try:
2024-04-27 19:11:16 +02:00
r = requests.get(url)
2024-04-14 23:22:33 +02:00
# This will raise an error if not successful
r.raise_for_status()
except requests.exceptions.HTTPError as e:
logging.critical("Failed downloading from cosmICweb.")
2024-04-17 18:40:05 +02:00
logging.critical(e)
sys.exit(1)
2024-04-14 23:22:33 +02:00
content = r.json()
sim = content["simulation"]
2024-04-27 19:11:16 +02:00
halo_names = []
for h in content["halos"]:
name = h["name"]
if name is None:
name = str(h["id"])
halo_names.append(name)
halo_ids = [h["id"] for h in content["halos"]]
2024-04-14 23:22:33 +02:00
halo_urls = [
"{url}/simulation/{sid}/halo/{hid}".format(
url=sim["api_url"], sid=sim["api_id"], hid=h["id"]
)
for h in content["halos"]
]
return DownloadConfig(
simulation_name=sim["name"],
2024-04-19 17:43:41 +02:00
project_name=sim["project_name"],
2024-04-14 23:22:33 +02:00
halo_names=halo_names,
halo_ids=halo_ids,
2024-04-14 23:22:33 +02:00
halo_urls=halo_urls,
traceback_radius=traceback_radius,
api_token=sim["api_token"],
MUSIC=sim["ics"],
settings=None,
2024-04-14 23:22:33 +02:00
accessed_at=datetime.now(),
)
2024-04-20 23:20:54 +02:00
def edit_template(template: str) -> str:
with tempfile.NamedTemporaryFile(suffix=".tmp.conf", mode="r+") as tf:
2024-04-14 23:22:33 +02:00
tf.write(template)
tf.flush()
editor_parameters = []
if EDITOR_IS_VIM:
# backupcopy=yes prevents vim from creating copy and rename
editor_parameters.append("+set backupcopy=yes")
subprocess.call([EDITOR] + editor_parameters + [tf.name])
2024-04-14 23:22:33 +02:00
tf.seek(0)
template = tf.read()
return template
2024-04-20 23:20:54 +02:00
def apply_config_parameter(config: str, parameters: dict[str, Any]) -> str:
2024-04-19 17:43:41 +02:00
new_lines = []
for line in config.split("\n"):
param = line.split("=")[0].strip()
if param in parameters:
line = line.split("=")[0] + f"= {parameters[param]}"
new_lines.append(line)
return "\n".join(new_lines)
2024-04-27 19:11:16 +02:00
def normalize_lineendings(text: str) -> str:
return text.replace("\r\n", "\n")
2024-04-20 23:20:54 +02:00
def music_config_to_template(config: DownloadConfig) -> str:
2024-04-19 17:43:41 +02:00
music_config = config.MUSIC
2024-04-27 19:11:16 +02:00
music_config = {k: normalize_lineendings(v) for k, v in music_config.items()}
2024-04-19 17:43:41 +02:00
settings = config.settings
# TODO: apply output configuration
config = (
"[setup]\n" + music_config["setup"] + "\n\n<ELLIPSOID_TEMPLATE>\n\n"
"[cosmology]\n" + music_config["cosmology"] + "\n\n"
"[random]\n" + music_config["random"] + "\n\n"
2024-04-23 12:59:57 +02:00
"[poisson]\n" + music_config["poisson"] + "\n\n"
2024-04-14 23:22:33 +02:00
)
if settings:
config = apply_config_parameter(
config,
{
"levelmin": settings["resolution"]["low"],
"levelmin_TF": settings["resolution"]["low"],
"levelmax": settings["resolution"]["high"],
"zstart": settings["startRedshift"],
},
)
2024-04-23 12:59:57 +02:00
if settings["outputType"]:
config += f"""
[output]
format = {settings["outputType"]}
filename = {settings["outputFilename"]}
""".strip()
config += "\n"
for k, v in settings["outputOptions"]:
config += f"{k} = {v}\n"
if not settings or not settings["outputType"]:
# TODO: allow specifying output format via cli argument
config += "[output]\n# TODO: add output options"
2024-04-19 17:43:41 +02:00
return config
2024-04-14 23:22:33 +02:00
def compose_template(
template: str,
ellipsoid: Ellipsoid,
config: DownloadConfig,
halo_name: str,
halo_id: int,
2024-04-21 00:08:20 +02:00
now: datetime = None,
2024-04-20 23:20:54 +02:00
) -> str:
2024-04-14 23:22:33 +02:00
# TODO: add ellipsoid header (rtb, halo_name, etc)
shape_0 = ", ".join(str(e) for e in ellipsoid.shape[0])
shape_1 = ", ".join(str(e) for e in ellipsoid.shape[1])
shape_2 = ", ".join(str(e) for e in ellipsoid.shape[2])
center = ", ".join(str(x) for x in ellipsoid.center)
2024-04-14 23:22:33 +02:00
ellipsoid_lines = (
2024-04-19 17:43:41 +02:00
"# Ellipsoidal refinement region defined on unity cube\n"
"# This minimum bounding ellipsoid has been obtained from\n"
f"# particles within {ellipsoid.traceback_radius} {ellipsoid.radius_definition} of the halo center\n"
2024-04-14 23:22:33 +02:00
"region = ellipsoid\n"
2024-04-19 17:43:41 +02:00
f"region_ellipsoid_matrix[0] = {shape_0}\n"
f"region_ellipsoid_matrix[1] = {shape_1}\n"
f"region_ellipsoid_matrix[2] = {shape_2}\n"
f"region_ellipsoid_center = {center}\n"
2024-04-14 23:22:33 +02:00
)
template = template.replace("<ELLIPSOID_TEMPLATE>", ellipsoid_lines)
2024-04-21 00:08:20 +02:00
if now is None:
now = datetime.now()
config_header = (
f"# Zoom Initial Conditions for halo {halo_id} ({halo_name}) in simulation {config.simulation_name} ({config.project_name} project)\n"
f"# Details on this halo can be found on https://cosmicweb.eu/simulation/{config.simulation_name}/halo/{halo_id}\n"
2024-04-21 00:08:20 +02:00
f"# This file has been generated by CosmICweb @{now.isoformat()}\n\n\n"
)
2024-04-21 00:08:20 +02:00
return config_header + template + "\n"
2024-04-14 23:22:33 +02:00
2024-04-21 00:08:20 +02:00
def write_music_file(output_file: str, music_config: str) -> None:
2024-04-14 23:22:33 +02:00
dirname = os.path.dirname(output_file)
if not os.path.exists(dirname):
logging.debug("Creating directory {}".format(dirname))
os.makedirs(dirname)
with open(output_file, "w") as f:
f.write(music_config)
2024-04-20 23:20:54 +02:00
def call_music() -> None:
2024-04-14 23:22:33 +02:00
pass
2024-04-21 00:08:20 +02:00
def process_config(config: DownloadConfig, args: Args, store: bool) -> None:
2024-04-14 23:22:33 +02:00
ellipsoids = []
for halo_name, url in zip(config.halo_names, config.halo_urls):
logging.info("Fetching ellipsoids from halo " + halo_name)
ellipsoids.append(
fetch_ellipsoid(
url + "/ellipsoids",
config.api_token,
config.traceback_radius,
args.attempts,
)
)
# Edit template
logging.info("Creating MUSIC template")
2024-04-19 17:43:41 +02:00
music_template = music_config_to_template(config)
2024-04-21 00:08:20 +02:00
output = []
2024-04-14 23:22:33 +02:00
2024-04-21 00:08:20 +02:00
if store and query_yes_no(
2024-04-14 23:22:33 +02:00
"Do you want to edit the MUSIC template before creating the IC files?\n"
"(changing zstart, levelmin, levelmax, etc.)",
default="no",
):
logging.debug("Editing MUSIC template")
music_template = edit_template(music_template)
logging.debug("Finished editing MUSIC template")
# Store template to file
for halo_name, halo_id, ellipsoid in zip(
config.halo_names, config.halo_ids, ellipsoids
):
2024-04-14 23:22:33 +02:00
if ellipsoid is None:
logging.warning(
"Ellipsoid for halo {} not available, skipping".format(halo_name)
)
continue
logging.info("Composing MUSIC configuration file for halo {}".format(halo_name))
music_config = compose_template(
music_template, ellipsoid, config, halo_name, halo_id
)
2024-04-17 18:32:29 +02:00
if args.common_directory and len(ellipsoids) > 1:
2024-04-14 23:22:33 +02:00
output_file = os.path.join(args.output_path, str(halo_name), "ics.cfg")
else:
2024-04-19 17:43:41 +02:00
output_file = os.path.join(args.output_path, "ics_{}.cfg".format(halo_name))
2024-04-14 23:22:33 +02:00
logging.info(
"Storing MUSIC configuration file for halo {} in {}".format(
halo_name, output_file
)
)
2024-04-21 00:08:20 +02:00
if store:
write_music_file(output_file, music_config)
else:
output.append((output_file, music_config))
return output
2024-04-14 23:22:33 +02:00
# TODO: Execute MUSIC?
2024-04-21 00:08:20 +02:00
def downloadstore_mode(args: Args, target: str, store=True) -> None | str:
2024-04-14 23:22:33 +02:00
logging.info("Fetching download configuration from the cosmICweb server")
2024-04-17 18:32:29 +02:00
config = fetch_downloadstore(args.url, target)
2024-04-14 23:22:33 +02:00
if args.output_path == "./":
2024-04-17 19:26:28 +02:00
args = args._replace(output_path=f"./cosmICweb-zooms-{config.simulation_name}")
2024-04-14 23:22:33 +02:00
logging.debug("Output directory set to " + args.output_path)
logging.info("Download configuration successfully fetched")
2024-04-21 00:08:20 +02:00
return process_config(config, args, store)
2024-04-14 23:22:33 +02:00
2024-04-21 00:08:20 +02:00
def publication_mode(
args: Args, publication_name: str, traceback_radius, store=True
) -> None | str:
2024-04-27 19:11:16 +02:00
logging.info(f"Fetching publication {publication_name} from the cosmICweb server")
config = fetch_multiple(
args.url, traceback_radius, publication_name=publication_name
2024-04-14 23:22:33 +02:00
)
2024-04-17 18:54:11 +02:00
args = args._replace(output_path=os.path.join(args.output_path, publication_name))
2024-04-14 23:22:33 +02:00
logging.debug("Output directory set to " + args.output_path)
logging.info("Publication successfully fetched")
2024-04-21 00:08:20 +02:00
return process_config(config, args, store)
2024-04-14 23:22:33 +02:00
2024-04-27 19:11:16 +02:00
def collection_mode(
args: Args, collection_uuid: str, traceback_radius, store=True
) -> None | str:
logging.info(f"Fetching collection {collection_uuid} from the cosmICweb server")
config = fetch_multiple(args.url, traceback_radius, collection_uuid=collection_uuid)
args = args._replace(
output_path=os.path.join(args.output_path, config.simulation_name)
)
logging.debug("Output directory set to " + args.output_path)
logging.info("Publication successfully fetched")
print(config)
return process_config(config, args, store)
2024-04-20 23:20:54 +02:00
def dir_path(p: str) -> str:
2024-04-14 23:22:33 +02:00
if os.path.isdir(p):
return p
else:
raise NotADirectoryError(p)
@click.group()
@click.option(
"--url", default=DEFAULT_URL, help="overwrite URL of the cosmICweb server"
)
@click.option(
"--output-path",
type=dir_path,
default="./",
help="Download target for IC files. If downloading publication, will create a subfolder with the name of the publication",
)
2024-04-22 15:17:35 +02:00
@click.option(
"--common-directory",
is_flag=True,
help="store all config files in the same directory instead of individual directories for each halo",
)
2024-04-14 23:22:33 +02:00
@click.option(
"--attempts", type=int, default=3, help="number of attempts to download ellipsoids"
)
@click.option("--verbose", is_flag=True)
@click.pass_context
def cli(ctx, url, output_path, common_directory, attempts, verbose):
if verbose:
logger.setLevel("DEBUG")
2024-04-17 18:32:29 +02:00
ctx.obj = Args(
url=url,
output_path=output_path,
common_directory=common_directory,
2024-04-17 19:26:28 +02:00
attempts=attempts,
2024-04-17 18:32:29 +02:00
)
2024-04-14 23:22:33 +02:00
@cli.command(help="Download ICs using a target UUID generated on cosmICweb")
@click.argument("target")
@click.pass_context
def get(ctx, target):
2024-04-17 18:32:29 +02:00
args: Args = ctx.obj
2024-04-14 23:22:33 +02:00
downloadstore_mode(args, target)
@cli.command(help="Download published ICs using the publication name")
@click.argument("publication_name")
2024-04-22 15:29:59 +02:00
@click.option(
"--traceback_radius", type=click.Choice(["1", "2", "4", "10"]), default="2"
)
2024-04-14 23:22:33 +02:00
@click.pass_context
def publication(ctx, publication_name, traceback_radius):
2024-04-22 15:29:59 +02:00
traceback_radius = float(traceback_radius)
2024-04-17 18:32:29 +02:00
args: Args = ctx.obj
2024-04-14 23:22:33 +02:00
publication_mode(args, publication_name, traceback_radius)
2024-04-27 19:11:16 +02:00
@cli.command(help="Download shared ICs using the collection UUID")
@click.argument("collection")
@click.option(
"--traceback_radius", type=click.Choice(["1", "2", "4", "10"]), default="2"
)
@click.pass_context
def collection(ctx, collection, traceback_radius):
traceback_radius = float(traceback_radius)
args: Args = ctx.obj
collection_mode(args, collection, traceback_radius)
2024-04-14 23:22:33 +02:00
if __name__ == "__main__":
cli()