Skip to content
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

Move permanent part of fkutils here #12

Merged
merged 37 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
006280c
Move towards datasets
felixhekhorn Mar 1, 2022
1e9cb65
Expose opcard generation for datasets
felixhekhorn Mar 1, 2022
5ff8e64
Add theory_ekos
felixhekhorn Mar 1, 2022
fc6ed74
Attempt on logging
felixhekhorn Mar 1, 2022
6ee0fb1
Drop Configurations class
felixhekhorn Mar 3, 2022
4eb7947
Fix eko logging
felixhekhorn Mar 3, 2022
f2b3d29
Upgrade syntax with pylint
felixhekhorn Mar 3, 2022
1d1d10e
Add theory_fks
felixhekhorn Mar 3, 2022
1d3c2b7
Add FK comparison log
felixhekhorn Mar 3, 2022
68cd123
Merge branch 'main' into fkutils-move
felixhekhorn Mar 9, 2022
e7ef4a6
Add comparison after evolution
felixhekhorn Mar 9, 2022
512332a
Init TheoryBuilder
felixhekhorn Mar 9, 2022
096f760
Move theory opcards
felixhekhorn Mar 10, 2022
a0d58fd
Reshuffle fks into theory
felixhekhorn Mar 14, 2022
784dee8
Regroup theory cli
felixhekhorn Mar 14, 2022
3a53e56
Update config
felixhekhorn Mar 15, 2022
cb516d5
Add inherit-grids command
felixhekhorn Mar 15, 2022
4900d4a
Drop paths.grids.common, add Readme
felixhekhorn Mar 15, 2022
e0bffc4
Fix Readme
felixhekhorn Mar 15, 2022
36205d7
Move old stuff
felixhekhorn Mar 15, 2022
2e4829e
Introduce overwrite option and skipping
felixhekhorn Mar 15, 2022
c2e3459
Make inherit-grids more consistent
felixhekhorn Mar 16, 2022
1e35e50
Make inherit-grids yet more consistent
felixhekhorn Mar 16, 2022
11e52ae
Update Readme
felixhekhorn Mar 16, 2022
9c3dd18
Add inherit-ekos command
felixhekhorn Mar 16, 2022
a10887b
Drop config.add_scope
felixhekhorn Mar 16, 2022
23449b6
Improve logging
felixhekhorn Mar 16, 2022
b464b52
Improve logging
felixhekhorn Mar 16, 2022
19ccfee
Adjust README a bit
cschwan Mar 16, 2022
f5702e5
Remove common grids dir
felixhekhorn Mar 21, 2022
d2018a4
Move pylint and apply some suggestions
felixhekhorn Mar 22, 2022
24674cf
Remove old code and folder
felixhekhorn Mar 22, 2022
b69353e
Drop module entry point
felixhekhorn Mar 22, 2022
ada17a4
Update parser
felixhekhorn Mar 22, 2022
3bf4bbc
Fix compare arg names
felixhekhorn Mar 23, 2022
0e0b141
Recover file base operations
felixhekhorn Mar 24, 2022
ee6b9da
Run poetry update
felixhekhorn Mar 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
504 changes: 504 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Pineko

This repository contains specifications for the apfelcomb code replacement.

1 change: 1 addition & 0 deletions data/ekos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tar
1 change: 1 addition & 0 deletions data/fktables/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pineappl.lz4
1 change: 1 addition & 0 deletions data/grids/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pineappl.lz4
1 change: 1 addition & 0 deletions data/grids/common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pineappl.lz4
8 changes: 8 additions & 0 deletions data/grids/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# copy from remote

# common example
# scp dom.mi.infn.it:/media/FK/fktables/data/grids/200-LHCB_DY_13TEV_DIELECTRON.pineappl.lz4 ./common/LHCB_DY_13TEV_DIELECTRON.pineappl.lz4

# theory example
# scp dom.mi.infn.it:/media/FK/fktables/data/grids/200-SLAC_NC_EM_D_F2.pineappl.lz4 ./200/SLAC_NC_EM_D_F2.pineappl.lz4
# scp dom.mi.infn.it:/media/FK/fktables/data/grids/200-SLAC_NC_EM_P_F2.pineappl.lz4 ./200/SLAC_NC_EM_P_F2.pineappl.lz4
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions data/operator_cards/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yaml
1 change: 1 addition & 0 deletions data/theory_cards/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yaml
1 change: 1 addition & 0 deletions data/ymldb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yaml
2 changes: 2 additions & 0 deletions data/ymldb/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# copy from remote
# scp dom.mi.infn.it:/media/FK/fktables/data/fktocommon/*.yaml .
1 change: 1 addition & 0 deletions logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
12 changes: 12 additions & 0 deletions pineko.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[paths]
ymldb = "data/ymldb"
grids = "data/grids"
grids_common = "data/grids/common"
operator_cards = "data/operator_cards"
theory_cards = "data/theory_cards"
opcard_template = "data/operator_cards/_template.yaml"
ekos = "data/ekos"
fktables = "data/fktables"

[paths.logs]
eko = "logs/eko"
9 changes: 2 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ tomli = "^2.0.1"

[tool.poetry.dev-dependencies]
# code review
black = "^21.6b0"
isort = "^5.9.2"
pylint = "^2.9.3"
# debug / explore
pdbpp = "^0.10.3"
Expand All @@ -43,11 +41,8 @@ pineko = "pineko:command"

[tool.poe.tasks]
test = "pytest tests"
lint = "pylint src/*/*.py -E"
lint-warnings = "pylint src/*/*.py --exit-zero"
black = "black ."
isort = "isort ."
format = ["black", "isort"]
lint = "pylint src/**/*.py -E"
lint-warnings = "pylint src/**/*.py --exit-zero"
pineko = "pineko"

[tool.poetry-dynamic-versioning]
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# -*- coding: utf-8 -*-

import logging
import pathlib
import sys
Expand Down
1 change: 1 addition & 0 deletions run2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from pineko import configs

configs.configs = configs.defaults(configs.load())
3 changes: 2 additions & 1 deletion src/pineko/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from . import command
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved

command()
command() # pylint: disable=no-value-for-parameter
# see https://stackoverflow.com/questions/49680191/click-and-pylint
2 changes: 1 addition & 1 deletion src/pineko/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-
from . import check, compare, convolute, opcard
from . import check, compare, convolute, opcard, theory_ekos, theory_fks, theory_opcards
from ._base import command
17 changes: 9 additions & 8 deletions src/pineko/cli/opcard.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# -*- coding: utf-8 -*-
import click
import pineappl
import rich
import yaml

from .. import evolve
from ._base import command


@command.command("opcard")
@click.argument("pineappl_path", metavar="PINEAPPL", type=click.Path(exists=True))
@click.argument("default_card_path", metavar="DEFAULT_CARD", type=click.Path(exists=True))
@click.argument(
"default_card_path", metavar="DEFAULT_CARD", type=click.Path(exists=True)
)
@click.argument("opcard_path", metavar="OPCARD", type=click.Path())
def subcommand(pineappl_path, default_card_path, opcard_path):
"""Write EKO card for PineAPPL grid.

Writes a copy of the default card from DEFAULT_CARD to OPCARD
with the adjusted x grid and Q2 grid read from PINEAPPL.
"""
pineappl_grid = pineappl.grid.Grid.read(pineappl_path)
with open(default_card_path, "r", encoding="UTF-8") as f:
default_card = yaml.safe_load(f)
_x_grid,q2_grid = evolve.write_operator_card(pineappl_grid, default_card, opcard_path)
rich.print(f"[green]Success:[/] Wrote card with {len(q2_grid)} Q2 points to {opcard_path}")
_x_grid, q2_grid = evolve.write_operator_card_from_file(
pineappl_path, default_card_path, opcard_path
)
rich.print(
f"[green]Success:[/] Wrote card with {len(q2_grid)} Q2 points to {opcard_path}"
)
47 changes: 47 additions & 0 deletions src/pineko/cli/theory_ekos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
import logging

import click
import eko
import rich
import yaml

from .. import configs, parser, theory_card
from ._base import command


@command.command("theory_ekos")
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
@click.argument("theory_id", type=click.INT)
@click.argument("datasets", type=click.STRING, nargs=-1)
@click.option("--logs", is_flag=True, help="dump logs")
def subcommand(theory_id, datasets, logs):
"""Compute EKOs for all FK tables in all datasets."""
# setup data
paths = configs.configs["paths"]
tcard = theory_card.load(theory_id)
eko_path = paths["ekos"] / str(theory_id)
eko_path.mkdir(exist_ok=True)
# iterate datasets
for ds in datasets:
rich.print(f"Analyze {ds}")
# iterate grids
grids = parser.load_grids(theory_id, ds)
for name in grids.keys():
opcard_path = paths["operator_cards"] / f"{name}.yaml"
with open(opcard_path, encoding="utf-8") as f:
ocard = yaml.safe_load(f)
eko_filename = eko_path / f"{name}.tar"
# activate logging
if logs and paths["logs"]["eko"]:
log_path = paths["logs"]["eko"] / f"{theory_id}-{ds}-{name}.log"
logStdout = logging.FileHandler(log_path)
logStdout.setLevel(logging.INFO)
logStdout.setFormatter(logging.Formatter("%(message)s"))
logging.getLogger("eko").handlers = []
logging.getLogger("eko").addHandler(logStdout)
logging.getLogger("eko").setLevel(logging.INFO)
# do it!
ops = eko.run_dglap(theory_card=tcard, operators_card=ocard)
ops.dump_tar(eko_filename)
rich.print(f"[green]Success:[/] Wrote EKO to {eko_filename}")
print()
36 changes: 36 additions & 0 deletions src/pineko/cli/theory_fks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import click
import rich

from .. import configs, evolve, parser, theory_card
from ._base import command


@command.command("theory_fks")
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
@click.argument("theory_id", type=click.INT)
@click.argument("datasets", type=click.STRING, nargs=-1)
@click.option("--logs", is_flag=True, help="dump logs")
def subcommand(theory_id, datasets, logs):
"""Compute FK tables in all datasets."""
# setup data
paths = configs.configs["paths"]
tcard = theory_card.load(theory_id)
eko_path = paths["ekos"] / str(theory_id)
fk_path = paths["fktables"] / str(theory_id)
fk_path.mkdir(exist_ok=True)
# iterate datasets
for ds in datasets:
rich.print(f"Analyze {ds}")
# iterate grids
grids = parser.load_grids(theory_id, ds)
for name, grid in grids.items():
eko_filename = eko_path / f"{name}.tar"
fk_filename = fk_path / f"{ds}-{name}.{parser.ext}"
# activate logging
# if logs and paths["logs"]["eko"]:
evolve.evolve_grid(
grid, eko_filename, fk_filename, 1 + int(tcard["PTO"]), 0
)
# do it!
rich.print(f"[green]Success:[/] Wrote FK table to {fk_filename}")
print()
26 changes: 26 additions & 0 deletions src/pineko/cli/theory_opcards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
import click
import rich

from .. import configs, evolve, parser
from ._base import command


@command.command("theory_opcards")
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
@click.argument("theory_id", type=click.INT)
@click.argument("datasets", type=click.STRING, nargs=-1)
def subcommand(theory_id, datasets):
"""Write EKO card for all FK tables in all datasets."""
paths = configs.configs["paths"]
for ds in datasets:
rich.print(f"Analyze {ds}")
grids = parser.load_grids(theory_id, ds)
for name, grid in grids.items():
opcard_path = paths["operator_cards"] / f"{name}.yaml"
_x_grid, q2_grid = evolve.write_operator_card_from_file(
grid, paths["opcard_template"], opcard_path
)
rich.print(
f"[green]Success:[/] Wrote card with {len(q2_grid)} Q2 points to {opcard_path}"
)
print()
2 changes: 1 addition & 1 deletion src/pineko/comparator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

import pineappl


Expand Down
94 changes: 38 additions & 56 deletions src/pineko/configs.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
# -*- coding: utf-8 -*-
import copy
import pathlib
import typing
import sys

import appdirs
import tomli

import rich

name = "pineko.toml"
"Name of the config while (wherever it is placed)"


class Configurations:
def __init__(self, dictionary=None):
if isinstance(dictionary, Configurations):
self._dict = dictionary._dict
elif dictionary is None:
self._dict = {}
else:
self._dict = dictionary

def __repr__(self):
return self._dict.__repr__()

def __getattribute__(self, name) -> typing.Any:
if name[0] == "_":
return super().__getattribute__(name)

value = self._dict[name]
if isinstance(value, dict):
value = Configurations(value)
return value

def __getitem__(self, key):
return self.__getattribute__(key)

def __setattribute__(self, name, value):
self._dict[name] = value

def __setitem__(self, key, value):
if key[0] == "_":
raise LookupError(
"Elements with leading '_' can not be retrieved later, so you"
f" can not set (attempted: '{key}')"
)

self._dict[key] = value

def __contains__(self, item):
return item in self._dict

def _pprint(self):
rich.print(self._dict)


# better to declare immediately the correct type
configs = Configurations()
configs = {}
"Holds loaded configurations"


Expand All @@ -75,24 +32,49 @@ def defaults(base_configs):
----
The general rule is to never replace user provided input.
"""
configs = Configurations(base_configs)
configs = copy.deepcopy(base_configs)

configs = add_paths(configs)

return Configurations(configs)
return configs


def add_paths(configs):
for key, default in dict(grids="grids", ekos="ekos").items():
if key not in configs.paths:
configs.paths[key] = configs.paths.root / default
elif pathlib.Path(configs.paths[key]).anchor == "":
configs.paths[key] = configs.paths.root / configs.paths[key]
for key in [
"ymldb",
"operator_cards",
"grids",
"grids_common",
"opcard_template",
"theory_cards",
"fktables",
"ekos",
]:
if key not in configs["paths"]:
raise ValueError(f"Configuration is missing a 'paths.{key}' key")
if pathlib.Path(configs["paths"][key]).anchor == "":
configs["paths"][key] = configs["paths"]["root"] / configs["paths"][key]
else:
configs["paths"][key] = pathlib.Path(configs["paths"][key])

if "logs" not in configs["paths"]:
configs["paths"]["logs"] = {}

for key in [
"eko",
]:
if key not in configs["paths"]["logs"]:
configs["paths"]["logs"][key] = None
elif pathlib.Path(configs["paths"]["logs"][key]).anchor == "":
configs["paths"]["logs"][key] = (
configs["paths"]["root"] / configs["paths"]["logs"][key]
)
else:
configs.paths[key] = pathlib.Path(configs.paths[key])
configs["paths"]["logs"][key] = pathlib.Path(configs["paths"]["logs"][key])

return configs


def detect(path=None):
paths = []

Expand Down Expand Up @@ -122,7 +104,7 @@ def load(path=None):
return {"paths": {"root": pathlib.Path.cwd()}}
else:
print("Configuration path specified is not valid.")
quit()
sys.exit()
alecandido marked this conversation as resolved.
Show resolved Hide resolved

with open(path, "rb") as fd:
loaded = tomli.load(fd)
Expand Down
Loading