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

more code cleanup

This commit is contained in:
Lukas Winkler 2024-04-30 14:02:28 +02:00
parent c48fba1cc3
commit ea3f93eba3
Signed by: lukas
GPG key ID: 54DE4D798D244853
4 changed files with 25 additions and 52 deletions

View file

@ -4,8 +4,6 @@ import os
import sys import sys
from typing import Any from typing import Any
from click import Context
from .data_types import Ellipsoid, Args, DownloadConfig from .data_types import Ellipsoid, Args, DownloadConfig
import click import click
@ -34,7 +32,7 @@ def fetch_ellipsoids(url: str, api_token: str, attempts: int) -> list[Ellipsoid]
# This will raise an error if not successful # This will raise an error if not successful
r.raise_for_status() r.raise_for_status()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
logging.warning("Failed fetching (attempt {}/{}) ...".format(i, attempts)) logging.warning(f"Failed fetching (attempt {i}/{attempts}) ...")
logging.warning(e) logging.warning(e)
else: else:
content = r.json() content = r.json()
@ -47,7 +45,7 @@ def fetch_ellipsoids(url: str, api_token: str, attempts: int) -> list[Ellipsoid]
) )
for e in content for e in content
] ]
logging.error("Unable to download ellipsoids from {}".format(url)) logging.error(f"Unable to download ellipsoids from {url}")
return [] return []
@ -68,21 +66,19 @@ def fetch_downloadstore(cosmicweb_url: str, target: str) -> DownloadConfig:
# This will raise an error if not successful # This will raise an error if not successful
r.raise_for_status() r.raise_for_status()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
logging.critical(f"Failed downloading from cosmICweb.") logging.critical("Failed downloading from cosmICweb.")
logging.critical(e) logging.critical(e)
sys.exit(1) sys.exit(1)
content = r.json() content = r.json()
sim = content["simulation"] sim = content["simulation"]
halo_urls = [ halo_urls = [
"{url}/simulation/{sid}/halo/{hid}".format( f"{sim['api_url']}/simulation/{sim['api_id']}/halo/{h}"
url=sim["api_url"], sid=sim["api_id"], hid=h
)
for h in content["halos"] for h in content["halos"]
] ]
return DownloadConfig( return DownloadConfig(
simulation_name=sim["name"], simulation_name=sim["name"],
project_name=sim["project_name"], project_name=sim["project_name"],
halo_names=["halo_{}".format(h) for h in content["halos"]], halo_names=[f"halo_{h}" for h in content["halos"]],
halo_ids=content["halos"], halo_ids=content["halos"],
halo_urls=halo_urls, halo_urls=halo_urls,
traceback_radius=content["traceback_radius"], traceback_radius=content["traceback_radius"],
@ -238,7 +234,7 @@ def compose_template(
def write_music_file(output_file: str, music_config: str) -> None: def write_music_file(output_file: str, music_config: str) -> None:
dirname = os.path.dirname(output_file) dirname = os.path.dirname(output_file)
if not os.path.exists(dirname): if not os.path.exists(dirname):
logging.debug("Creating directory {}".format(dirname)) logging.debug(f"Creating directory {dirname}")
os.makedirs(dirname) os.makedirs(dirname)
with open(output_file, "w") as f: with open(output_file, "w") as f:
f.write(music_config) f.write(music_config)
@ -263,7 +259,6 @@ def process_config(config: DownloadConfig, args: Args) -> None:
# Edit template # Edit template
logging.info("Creating MUSIC template") logging.info("Creating MUSIC template")
music_template = music_config_to_template(config) music_template = music_config_to_template(config)
output = []
if click.confirm( if click.confirm(
"Do you want to edit the MUSIC template before creating the IC files?\n" "Do you want to edit the MUSIC template before creating the IC files?\n"
@ -278,18 +273,16 @@ def process_config(config: DownloadConfig, args: Args) -> None:
config.halo_names, config.halo_ids, ellipsoids config.halo_names, config.halo_ids, ellipsoids
): ):
if ellipsoid is None: if ellipsoid is None:
logging.warning( logging.warning(f"Ellipsoid for halo {halo_name} not available, skipping")
"Ellipsoid for halo {} not available, skipping".format(halo_name)
)
continue continue
logging.info("Composing MUSIC configuration file for halo {}".format(halo_name)) logging.info(f"Composing MUSIC configuration file for halo {halo_name}")
music_config = compose_template( music_config = compose_template(
music_template, ellipsoid, config, halo_name, halo_id music_template, ellipsoid, config, halo_name, halo_id
) )
if args.common_directory and len(ellipsoids) > 1: if args.common_directory and len(ellipsoids) > 1:
output_file = os.path.join(args.output_path, str(halo_name), "ics.cfg") output_file = os.path.join(args.output_path, str(halo_name), "ics.cfg")
else: else:
output_file = os.path.join(args.output_path, "ics_{}.cfg".format(halo_name)) output_file = os.path.join(args.output_path, f"ics_{halo_name}.cfg")
logging.info( logging.info(
"Storing MUSIC configuration file for halo {} in {}".format( "Storing MUSIC configuration file for halo {} in {}".format(
halo_name, output_file halo_name, output_file

View file

@ -1,12 +1,12 @@
from __future__ import annotations from __future__ import annotations
from datetime import datetime from datetime import datetime
from typing import NamedTuple, Any, List, Dict, TypedDict, Tuple from typing import NamedTuple, TypedDict
class Ellipsoid(NamedTuple): class Ellipsoid(NamedTuple):
center: List[float] center: list[float]
shape: List[List[float]] shape: list[list[float]]
traceback_radius: float traceback_radius: float
radius_definition: str radius_definition: str
@ -19,7 +19,7 @@ class Resolution(TypedDict):
class Configuration(TypedDict): class Configuration(TypedDict):
outputType: str outputType: str
resolution: Resolution resolution: Resolution
outputOptions: List[Tuple[str, str]] outputOptions: list[tuple[str, str]]
startRedshift: int startRedshift: int
outputFilename: str outputFilename: str
seperateFolders: bool seperateFolders: bool
@ -36,9 +36,9 @@ class ICSections(TypedDict):
class DownloadConfig(NamedTuple): class DownloadConfig(NamedTuple):
simulation_name: str simulation_name: str
project_name: str project_name: str
halo_names: List[str] halo_names: list[str]
halo_ids: List[int] halo_ids: list[int]
halo_urls: List[str] halo_urls: list[str]
traceback_radius: float traceback_radius: float
api_token: str api_token: str
MUSIC: ICSections MUSIC: ICSections

View file

@ -3,7 +3,6 @@ from datetime import datetime
from cosmicweb_music.cosmICweb import ( from cosmicweb_music.cosmICweb import (
apply_config_parameter, apply_config_parameter,
music_config_to_template, music_config_to_template,
process_config,
DEFAULT_URL, DEFAULT_URL,
compose_template, compose_template,
) )
@ -40,31 +39,21 @@ baryons = no # switch on for baryon runs
use_2LPT = no use_2LPT = no
use_LLA = no # AMR codes might want to enable this use_LLA = no # AMR codes might want to enable this
""".strip() """.strip()
settings:Configuration={
settings: Configuration = {
"outputType": "grafic2", "outputType": "grafic2",
"resolution": { "resolution": {"low": 7, "high": 11},
"low": 7,
"high": 11
},
"outputOptions": [ "outputOptions": [
( ("ramses_nml", "yes"),
"ramses_nml", ("ramses_old_nml", "no"),
"yes" ("ramses_pvar_idx", "43"),
),
(
"ramses_old_nml",
"no"
),
(
"ramses_pvar_idx",
"43"
)
], ],
"startRedshift": 100, "startRedshift": 100,
"outputFilename": "ics.dat", "outputFilename": "ics.dat",
"seperateFolders": True, "seperateFolders": True,
"tracebackRadius": 4 "tracebackRadius": 4,
} }
config = DownloadConfig( config = DownloadConfig(
simulation_name="test", simulation_name="test",
project_name="project", project_name="project",

View file

@ -1,19 +1,10 @@
import re import re
from pathlib import Path from pathlib import Path
import click
from _pytest.monkeypatch import MonkeyPatch from _pytest.monkeypatch import MonkeyPatch
from click.testing import CliRunner from click.testing import CliRunner
from cosmicweb_music.cosmICweb import ( from cosmicweb_music.cosmICweb import cli
DEFAULT_URL,
downloadstore_mode,
publication_mode,
collection_mode,
get,
cli,
)
from cosmicweb_music.data_types import Args
reference_output = """ reference_output = """
# Zoom Initial Conditions for halo 25401888 (halo_25401888) in simulation AGORA (AGORA Project project) # Zoom Initial Conditions for halo 25401888 (halo_25401888) in simulation AGORA (AGORA Project project)