Skip to content

Commit

Permalink
Merge pull request #1197 from NNPDF/exp2di
Browse files Browse the repository at this point in the history
Adding dataset_inputs namespace to old style fits as_input
  • Loading branch information
siranipour authored Apr 27, 2021
2 parents 50b2687 + 49ede40 commit fdf5586
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
9 changes: 6 additions & 3 deletions doc/sphinx/source/vp/dataspecification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,12 @@ results in the table or plot will have been collected over ``fits`` with
.. warning::
Whilst it is possible to specify ``data_input: {from_: fitinputcontext}``
directly in the runcard, it is highly recommended **not** to do this where
possible. Instead take either ``dataset_inputs`` or ``experiments``
directly ``from_: fit`` depending on whether the fit uses new or old data
specification respectively. (See below for a detailed explanation).
possible. Instead take ``dataset_inputs`` directly ``from_: fit``
irrespective of whether the fit uses new or old data specification; since
the conversion from the old style data specification is handled internally
using :py:func:`validphys.utils.experiments_to_dataset_inputs` in
conjunction with :py:meth:`validphys.core.FitSpec.as_input`. (See below for
a detailed explanation).

Currently the ``pseudodata`` and ``chi2grids`` modules have not been updated to
use ``dataset_inputs`` and so require ``experiments`` to be specified in the
Expand Down
15 changes: 5 additions & 10 deletions validphys2/src/validphys/comparefittemplates/comparecard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ positivity:
description:
from_: fit

dataset_inputs:
from_: fit

dataspecs:
# WARNING: do not blindly copy and paste this: it can overwrite the datasets
# for any actions which rely on grouping datasets.
- data_input:
from_: fitinputcontext
theoryid:
- theoryid:
from_: current
pdf:
from_: current
Expand All @@ -121,11 +120,7 @@ dataspecs:
speclabel:
from_: current

# WARNING: do not blindly copy and paste this: it can overwrite the datasets
# for any actions which rely on grouping datasets.
- data_input:
from_: fitinputcontext
theoryid:
- theoryid:
from_: reference
pdf:
from_: reference
Expand Down
7 changes: 7 additions & 0 deletions validphys2/src/validphys/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from validphys import lhaindex, filters
from validphys.tableloader import parse_exp_mat
from validphys.theorydbutils import fetch_theory
from validphys.utils import experiments_to_dataset_inputs

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -623,6 +624,12 @@ def as_input(self):
except (yaml.YAMLError, FileNotFoundError) as e:
raise AsInputError(str(e)) from e
d['pdf'] = {'id': self.name, 'label': self.label}

if 'experiments' in d:
# Flatten old style experiments to dataset_inputs
dataset_inputs = experiments_to_dataset_inputs(d['experiments'])
d['dataset_inputs'] = dataset_inputs

return d

def __str__(self):
Expand Down
7 changes: 5 additions & 2 deletions validphys2/src/validphys/tests/test_effexponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def test_next_runcard():
l = Loader()
ite1_fit = l.check_fit(FIT)
# The runcard of a 2nd iteration fit I ran manually
ite2_runcard = l.check_fit(FIT_ITERATED).as_input()
ite2_runcard.pop("pdf") # Removing the PDF key, it's an artefact of as_input
# We load it using the context manager because at_input has been modified
# to load various keys that are not present in the actual runcard for
# backwards compatibility
with open(l.check_fit(FIT_ITERATED).path / "filter.yml", "r") as f:
ite2_runcard = yaml.safe_load(f)

predicted_ite2_runcard = yaml.safe_load(
API.iterated_runcard_yaml(fit=FIT)
Expand Down
24 changes: 24 additions & 0 deletions validphys2/src/validphys/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ def tempfile_cleaner(root, exit_func, exc, prefix=None, **kwargs):
# e.g shutil.rmtree, shutil.move etc
exit_func(tempdir, **kwargs)


def experiments_to_dataset_inputs(experiments_list):
"""Flatten a list of old style experiment inputs
to the new, flat, ``dataset_inputs`` style.
Example
-------
>>> from validphys.api import API
>>> from validphys.utils import experiments_to_dataset_inputs
>>> fit = API.fit(fit='NNPDF31_nnlo_as_0118_1000')
>>> experiments = fit.as_input()['experiments']
>>> dataset_inputs = experiments_to_dataset_inputs(experiments)
>>> dataset_inputs[:3]
[{'dataset': 'NMCPD', 'frac': 0.5},
{'dataset': 'NMC', 'frac': 0.5},
{'dataset': 'SLACP', 'frac': 0.5}]
"""
dataset_inputs = []
for experiment in experiments_list:
dataset_inputs.extend(experiment['datasets'])

return dataset_inputs


def split_by(it, crit):
"""Split ``it`` in two lists, the first is such that ``crit`` evaluates to
True and the second such it doesn't. Crit can be either a function or an
Expand Down

0 comments on commit fdf5586

Please sign in to comment.