Skip to content

Commit

Permalink
add --dry
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlehoff committed Mar 22, 2024
1 parent 52fa7f7 commit a3048cb
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 54 deletions.
8 changes: 5 additions & 3 deletions src/pinefarm/cli/autogen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Autogenerate pinecards from NNPDF metadata"""

import click
import rich

from .. import configs
from ..external.nnlojet import generate_pinecard_from_nnpdf
from ._base import command


@command.command("autogen")
@click.argument("dataset", nargs=1)
@click.option(
Expand All @@ -20,6 +22,6 @@ def runcards(dataset, target):
if target == "NNLOJET":
output_runcards = generate_pinecard_from_nnpdf(dataset, output_path=output)

print("Runcards written to: ")
print("\n".join(str(i) for i in output_runcards))
print("metadata.txt might be empty or incomplete, please modifiy it manually")
rich.print("Runcards written to: ")
rich.print(" " + "\n".join(str(i) for i in output_runcards))
rich.print("metadata.txt might be empty or incomplete, please modifiy it manually")
1 change: 1 addition & 0 deletions src/pinefarm/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def lhapdf():
install.update_environ()
install.lhapdf()


@subcommand.command()
def nnlojet():
"""Install NNLOJET."""
Expand Down
35 changes: 22 additions & 13 deletions src/pinefarm/cli/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Compute a dataset and compare using a given PDF."""

import pathlib
import sys
import time

import click
Expand All @@ -16,7 +17,8 @@
@click.argument("dataset")
@click.argument("theory-path", type=click.Path(exists=True))
@click.option("--pdf", default="NNPDF31_nlo_as_0118_luxqed")
def subcommand(dataset, theory_path, pdf):
@click.option("--dry", is_flag=True, help="Don't execute the underlying code")
def subcommand(dataset, theory_path, pdf, dry):
"""Compute a dataset and compare using a given PDF.
Given a DATASET name and a THEORY-PATH, a runcard is executed with the
Expand All @@ -25,16 +27,6 @@ def subcommand(dataset, theory_path, pdf):
The given PDF (default: `NNPDF31_nlo_as_0118_luxqed`) will be used to
compare original results with PineAPPL interpolation.
"""
# read theory card from file
with open(theory_path) as f:
theory_card = yaml.safe_load(f)
main(dataset, theory_card, pdf)


def main(dataset, theory, pdf):
"""Compute a dataset and compare using a given PDF.
Parameters
----------
dataset : str
Expand All @@ -44,7 +36,12 @@ def main(dataset, theory, pdf):
pdf : str
pdf name
"""
# read theory card from file
with open(theory_path) as f:
theory_card = yaml.safe_load(f)

dataset = pathlib.Path(dataset).name
timestamp = None

Expand All @@ -64,11 +61,23 @@ def main(dataset, theory, pdf):
except UnboundLocalError as e:
raise UnboundLocalError(f"Runcard {dataset} could not be found") from e


rich.print(f"Computing [{datainfo.color}]{dataset}[/]...")
runner = datainfo.external(dataset, theory, pdf, timestamp=timestamp)
runner = datainfo.external(dataset, theory_card, pdf, timestamp=timestamp)

install_reqs(runner, pdf)

# Run the preparation step of the runner (if any)
runner_stop = runner.preparation()
if dry or runner_stop:
rich.print(
f"""Running in dry mode, exiting now.
The preparation step can be found in:
{runner.dest}"""
)
sys.exit(0)

###### <this part will eventually go to -prepare->

run_dataset(runner)


Expand Down
5 changes: 3 additions & 2 deletions src/pinefarm/external/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ def decide_external_tool(dsname):
# or a prefix in the pinecard

if dsname.startswith("NNLOJET"):
from .external import NNLOJET
return NNLOJET.NNLOJET, "blue"
from .nnlojet import NNLOJET

return NNLOJET, "blue"

# DIS with yadism
if (configs["paths"]["runcards"] / dsname / "observable.yaml").exists():
Expand Down
7 changes: 4 additions & 3 deletions src/pinefarm/external/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ def __init__(self, name, theory, pdf, timestamp=None):
if timestamp is None:
self.dest = tools.create_output_folder(self.name, self.theory["ID"])
else:
import ipdb

ipdb.set_trace()
self.dest = configs.configs["paths"]["results"] / (
str(theory["ID"]) + "-" + self.name + "-" + self.timestamp
)
Expand Down Expand Up @@ -71,6 +68,10 @@ def update_with_tmp(self):
def install():
"""Install all needed programs."""

def preparation(self):
"""Run the preparation method of the runner"""
return False

@abc.abstractmethod
def run(self):
"""Execute the program."""
Expand Down
8 changes: 4 additions & 4 deletions src/pinefarm/external/nnlojet/nnpdf_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

import numpy as np
from ruamel.yaml import YAML, CommentedMap
from validphys.api import API
from validphys.datafiles import path_vpdata
from validphys.theorydbutils import fetch_theory
from validphys.filters import KIN_LABEL

# set-up the yaml reader
yaml = YAML(pure=True)
Expand All @@ -31,6 +27,8 @@

def _legacy_nnpdf_translation(df, proc_type):
"""When reading variables with k1/k2/k3 tries to figure out to which variables it corresponds"""
from validphys.filters import KIN_LABEL

new_vars = list(KIN_LABEL[proc_type])
# Reorganize a bit the names to avoid extra problems
if "M_ll" in new_vars:
Expand Down Expand Up @@ -223,6 +221,8 @@ def _generate_nnlojet_pinecard(runname, process, energy, experiment, histograms)
def generate_pinecard_from_nnpdf(nnpdf_dataset, scale="mz", output_path="."):
"""Generate a NNLOJET pinecard from an NNPDF dataset"""
# Load the NNPDF dataset
from validphys.api import API

commondata = API.commondata(dataset_input={"dataset": nnpdf_dataset})
metadata = commondata.metadata
kin_df = metadata.load_kinematics(drop_minmax=False)
Expand Down
9 changes: 7 additions & 2 deletions src/pinefarm/external/nnlojet/runcardgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,18 @@ def generate_runcard(
return runcard_path


def generate_nnlojet_runcard(yamlinfo, channels=("LO",), output=Path(".")):
def generate_nnlojet_runcard(
yamlinfo, channels=("LO",), output=Path("."), warmup=False
):
"""Generate a nnlojet runcard from a yaml pinecard"""
yaml_metadata = YamlLOJET(**yamlinfo)

output.mkdir(exist_ok=True, parents=True)

runcards = []
for channel in channels:
runcards.append(generate_runcard(yaml_metadata, channel, output=output))
runcard_path = generate_runcard(
yaml_metadata, channel, output=output, is_warmup=warmup
)
runcards.append(runcard_path)
return runcards
46 changes: 19 additions & 27 deletions src/pinefarm/external/nnlojet/runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Provides a runner for NNLOJET."""

import subprocess as sp
import sys

from yaml import safe_load

from .. import interface
Expand All @@ -17,29 +14,31 @@ def __init__(self, pinecard, theorycard, *args, **kwargs):

pinecard = pinecard.replace("NNLOJET_", "")
yaml_card = (self.source / pinecard).with_suffix(".yaml")
yaml_dict = safe_load(yaml_card.open("r"))
# Save the yaml dictionary from the NNLOJET pinecard
self._yaml_dict = safe_load(yaml_card.open("r"))

def preparation(self):
"""Run the preparation step for NNLOJET"""
# Update the yaml card according to the theory
params = yaml_dict["parameters"]
params = self._yaml_dict["parameters"]

ckm_first = float(theorycard.get("CKM", "1.0").split()[0])
ckm_first = float(self.theory.get("CKM", "1.0").split()[0])
if ckm_first != 1.0:
params["CKM"] = "FULL"

translate = [("MZ", "MASS[Z]"), ("MW", "MASS[W]")]
for nnpdf_key, nnlojet_key in translate:
if nnpdf_key in theorycard:
params[nnlojet_key] = theorycard[nnpdf_key]
if nnpdf_key in self.theory:
params[nnlojet_key] = self.theory[nnpdf_key]

# Autodiscover scale if possible
if "scales" in yaml_dict:
scdict = yaml_dict["scales"]
if (scdict := self._yaml_dict.get("scales")) is not None:
for scale, key in scdict.items():
if isinstance(key, str) and key.upper() in theorycard:
scdict[scale] = theorycard[key.upper()]
if isinstance(key, str) and key.upper() in self.theory:
scdict[scale] = self.theory[key.upper()]

# Select channels according to PTO
order = theorycard.get("PTO")
order = self.theory.get("PTO")
channels = ["LO"]
if order > 0:
channels += ["R", "V"]
Expand All @@ -48,23 +47,16 @@ def __init__(self, pinecard, theorycard, *args, **kwargs):
if order > 2:
raise NotImplementedError("N3LO still not working")

self._nnlojet_runcards = generate_nnlojet_runcard(
yaml_dict, channels, output=self.dest
)
# Generate both the production and warmup runcards
for warmup in [True, False]:
_ = generate_nnlojet_runcard(
self._yaml_dict, channels, output=self.dest, warmup=warmup
)
return True

def run(self):
"""Run the corresponding NNLOJET runcard"""
print(f"NNLOJET running not implemented, but you can find the NNLOJET runcards at:\n> {self.dest}")
sys.exit(-1)
# for runcard in self._nnlojet_runcards:
# # Exit regardless of whether there's a NNLOJET executable for now
# try:
# sp.run(["NNLOJET", "-run", runcard.name], cwd=self.dest, check=True)
# except FileNotFoundError:
# print(
# f"NNLOJET executable not found, but you can find the NNLOJET runcards at:\n> {self.dest}"
# )
# sys.exit(-1)
raise NotImplementedError("NNLOJET running not implemented outside of dry mode")

def collect_versions(self) -> dict:
return {"nnlojet_version": "secret"}
Expand Down

0 comments on commit a3048cb

Please sign in to comment.