Skip to content

Commit

Permalink
use multiprocessing.Array
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstangl committed Feb 19, 2021
1 parent c2e173c commit 21ba0b0
Show file tree
Hide file tree
Showing 43 changed files with 275 additions and 191 deletions.
2 changes: 1 addition & 1 deletion flavio/__init__.py
Expand Up @@ -7,7 +7,7 @@
from . import classes
from .classes import Measurement, Parameter, ParameterConstraints, Observable, NamedInstanceClass
from .config import config
from .citations import Citations, default_citations, register_citation
from . import citations
from flavio.physics.eft import WilsonCoefficients
from flavio.parameters import default_parameters
from flavio.functions import sm_prediction, sm_uncertainty, np_uncertainty, sm_error_budget, np_prediction, sm_covariance, combine_measurements
110 changes: 45 additions & 65 deletions flavio/citations.py
@@ -1,88 +1,68 @@
"""Citation class for handling theory calculation citations"""
# citations.py is based on code from the PyBaMM project
# Inspired by pybamm.citations
# https://github.com/pybamm-team/PyBaMM

# Copyright (c) 2018-2020, the PyBaMM team.
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
from multiprocessing import Array
import flavio
import sys
from itertools import compress
import ctypes
import yaml
import pkgutil

# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
class CitationScope:

# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
def __enter__(self):
self._citations_global = flavio.citations
flavio.citations = Citations()
return flavio.citations

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import flavio
def __exit__(self, type, value, traceback):
flavio.citations = self._citations_global


class Citations:

"""Entry point to citations management.
This object may be used to register a citation is relevant for a particular
implementation. It can then also then print out a list of papers to be cited.
Examples
--------
>>> import flavio
>>> flavio.sm_prediction("DeltaM_s")
>>> print(flavio.default_citations)
"""

def __init__(self):
self._reset()
collect = CitationScope
__all_citations = set(yaml.safe_load(
pkgutil.get_data('flavio', 'data/citations.yml')
))

def __init__(self, initial_citations=[]):
self._initial_citations = set(initial_citations)
self._all_citations = {
k:i for i,k in
enumerate(sorted(self.__all_citations | self._initial_citations))
}
self._array = Array(ctypes.c_bool, len(self._all_citations))
for inspire_key in self._initial_citations:
self.register(inspire_key)

def __iter__(self):
for citation in self._papers_to_cite:
for citation in self.set:
yield citation

def __str__(self):
return ",".join(self._papers_to_cite)

def _reset(self):
"Reset citations to empty"
self._papers_to_cite = set()
return ",".join(self.set)

@property
def tostring(self):
return str(self)
def set(self):
return set(compress(sorted(self._all_citations.keys()), self._array))

@property
def toset(self):
return self._papers_to_cite

def register(self, key):
"""Register a paper to be cited. The intended use is that this method
should be called only when the referenced functionality is actually being used.
def string(self):
return str(self)

Parameters
----------
key : str
The INSPIRE texkey for the paper to be cited
"""
self._papers_to_cite.add(key)
def register(self, inspire_key):
self._array[self._all_citations[inspire_key]] = True

def clear(self):
self._array[:] = [False]*len(self._array)

def reset(self):
self.clear()
for inspire_key in self._initial_citations:
self.register(inspire_key)

default_citations = Citations()
# Register the flavio paper
default_citations.register("Straub:2018kue")

def register_citation(inspire_key):
flavio.default_citations.register(inspire_key)
sys.modules[__name__] = Citations(["Straub:2018kue"])
11 changes: 4 additions & 7 deletions flavio/classes.py
Expand Up @@ -700,16 +700,13 @@ def get_measurements(self):
def theory_citations(self, *args, **kwargs):
"""Return a set of theory papers (in the form of INSPIRE texkeys) to
cite for the theory prediction for an observable.
Arguments are passed to the observable and are necessary,
depending on the observable (e.g. $q^2$-dependent observables).
"""
old_citations = flavio.default_citations
flavio.default_citations = flavio.Citations()
flavio.sm_prediction(self.name, *args, **kwargs)
SM_citations = flavio.default_citations.toset
flavio.default_citations = old_citations
return SM_citations
with flavio.citations.collect() as citations:
flavio.sm_prediction(self.name, *args, **kwargs)
return citations.set



Expand Down
42 changes: 42 additions & 0 deletions flavio/data/citations.yml
@@ -0,0 +1,42 @@
- Antonelli:2010yf
- Asatrian:2003vq
- Awramik:2003rn
- Beneke:2001at
- Beneke:2003az
- Beneke:2004dp
- Benson:2003kp
- Bernard:2009zm
- Bjorn:2016zlr
- Bobeth:2013uxa
- Boer:2014kda
- Bourrely:2008za
- Brignole:2004ah
- Brivio:2017vri
- Brod:2010hi
- Buras:2006gb
- Buras:2015yba
- Caprini:1997mu
- Chobanova:2017rkj
- Cirigliano:2007ga
- Descotes-Genon:2015hea
- Falkowski:2019hvp
- Freitas:2014hra
- Gambino:2011cq
- Gonzalez-Alonso:2018omy
- Gorbahn:2006bm
- Greub:2008cy
- Guadagnoli:2016erb
- Gubernari:2018wyi
- Huber:2015sra
- Inami:1980fz
- Kitano:2002mt
- Kozachuk:2017mdk
- Kruger:2002gf
- Kuno:1999jp
- Melikhov:2004mk
- Misiak:2006ab
- Misiak:2015xwa
- Pich:2013lsa
- Seidel:2004jh
- Straub:2015ica
- fakename:2020abc
25 changes: 25 additions & 0 deletions flavio/data/update_citations.py
@@ -0,0 +1,25 @@
#! /usr/bin/env python3

"""Standalone helper script to update the list of citations referenced in the
flavio source code."""

import argparse
import logging
import sys
import yaml
import pkgutil
logging.basicConfig(level=logging.INFO)

def main(argv):
parser = argparse.ArgumentParser(description='Update the list of citations referenced in the flavio source code.')
args = parser.parse_args()

from flavio.util import get_datapath, extract_citations

filename = get_datapath('flavio', 'data/citations.yml')
with open(filename, 'w') as f:
f.write(yaml.dump(sorted(extract_citations())))
logging.info(f"Saved updated citations to {filename}")

if __name__ == '__main__':
main(sys.argv)
24 changes: 12 additions & 12 deletions flavio/physics/bdecays/bllgamma.py
Expand Up @@ -52,7 +52,7 @@ def getfft(s, par, B, ff, ff0, lep, wc):

#Add light meson resonances
resonances = {'Bs': ['phi'], 'B0': ['rho0', 'omega']}
flavio.register_citation("Kozachuk:2017mdk")
flavio.citations.register("Kozachuk:2017mdk")
fVtofVemfactors = {'phi': -1/3, 'rho0': 1/sqrt(2), 'omega': 1/(3*sqrt(2))} # Given in Sec. 8.A.3 of 1712.07926
#We use the general B->rho and B->omega parameters, hence the isotopic factors
resgV = {('Bs','phi'): -par['Bs->phi BSZ a0_T1'],
Expand All @@ -67,7 +67,7 @@ def getfft(s, par, B, ff, ff0, lep, wc):
return (fftv, ffta)

def getF1(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.citations.register("Melikhov:2004mk")
scale = config['renormalization scale']['bllgamma']
mb = running.get_mb(par, scale, nf_out=5)
mbh = mb/par['m_'+B]
Expand All @@ -77,7 +77,7 @@ def getF1(s, par, B, ff, ff0, lep, wc):
return (abs(wc['C9_'+label])**2 + abs(wc['C10_'+label])**2)*ff['v']**2 + 4*mbh**2/s**2*abs(wc['C7_'+bq]*fftv)**2 + 4*mbh/s*ff['v']*_Re(wc['C7_'+bq]*ffta*_Co(wc['C9_'+label]))

def getF2(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.citations.register("Melikhov:2004mk")
scale = config['renormalization scale']['bllgamma']
mb = running.get_mb(par, scale, nf_out=5)
mbh = mb/par['m_'+B]
Expand All @@ -88,8 +88,8 @@ def getF2(s, par, B, ff, ff0, lep, wc):


def B10(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.register_citation("Guadagnoli:2016erb")
flavio.citations.register("Melikhov:2004mk")
flavio.citations.register("Guadagnoli:2016erb")
F1 = getF1(s, par, B, ff, ff0, lep, wc)
F2 = getF2(s, par, B, ff, ff0, lep, wc)
mlh = par['m_'+lep]/par['m_'+B]
Expand All @@ -99,15 +99,15 @@ def B10(s, par, B, ff, ff0, lep, wc):
# B11 vanishes for the BR estimation

def B12(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.register_citation("Guadagnoli:2016erb")
flavio.citations.register("Melikhov:2004mk")
flavio.citations.register("Guadagnoli:2016erb")
F1 = getF1(s, par, B, ff, ff0, lep, wc)
F2 = getF2(s, par, B, ff, ff0, lep, wc)
return s*(F1+F2)

def B120(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.register_citation("Guadagnoli:2016erb")
flavio.citations.register("Melikhov:2004mk")
flavio.citations.register("Guadagnoli:2016erb")
scale = config['renormalization scale']['bllgamma']
mb = running.get_mb(par, scale, nf_out=5)
mbh = mb/par['m_'+B]
Expand All @@ -119,21 +119,21 @@ def B120(s, par, B, ff, ff0, lep, wc):


def dG1dsMN(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.citations.register("Melikhov:2004mk")
mlh = par['m_'+lep]/par['m_'+B]
pref = prefactor(s, par, B, ff, lep, wc)*(1-s)**3*sqrt(1-4*mlh**2/s)
return pref*(B10(s, par, B, ff, ff0, lep, wc) + (s - 4*mlh**2)/(3*s)*B12(s, par, B, ff, ff0, lep, wc))

def dG2dsMN(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.citations.register("Melikhov:2004mk")
mlh = par['m_'+lep]/par['m_'+B]
bq = meson_quark[B]
label = bq+lep+lep
pref = 16*prefactor(s, par, B, ff, lep, wc)*(par['f_'+B]/par['m_'+B]*abs(wc['C10_'+label])*mlh)**2/(1-s)
return pref*(-8*sqrt(s-4*mlh**2)*sqrt(s)+4*atanh(sqrt(1-4*mlh**2/s))*(1+s-mlh**2*(1-s)**2))

def dG12dsMN(s, par, B, ff, ff0, lep, wc):
flavio.register_citation("Melikhov:2004mk")
flavio.citations.register("Melikhov:2004mk")
mlh = par['m_'+lep]/par['m_'+B]
pref = 4*prefactor(s, par, B, ff, lep, wc)*par['f_'+B]/par['m_'+B]*(1-s)
return pref*atanh(sqrt(1-4*mlh**2/s))*B120(s, par, B, ff, ff0, lep, wc)
Expand Down
4 changes: 2 additions & 2 deletions flavio/physics/bdecays/bvll/observables_bs.py
Expand Up @@ -34,7 +34,7 @@ def bsvll_obs(function, q2, wc_obj, par, B, V, lep):

def S_theory_num_Bs(y, J, J_bar, J_h, i):
# (42) of 1502.05509
flavio.register_citation("Descotes-Genon:2015hea")
flavio.citations.register("Descotes-Genon:2015hea")
return 1/(1-y**2) * (J[i] + J_bar[i]) - y/(1-y**2) * J_h[i]

def S_experiment_num_Bs(y, J, J_bar, J_h, i):
Expand All @@ -52,7 +52,7 @@ def S_experiment_Bs(y, J, J_bar, J_h, i):

def dGdq2_ave_Bs(y, J, J_bar, J_h):
# (48) of 1502.05509
flavio.register_citation("Descotes-Genon:2015hea")
flavio.citations.register("Descotes-Genon:2015hea")
return (1/(1-y**2) * (observables.dGdq2(J) + observables.dGdq2(J_bar))
- y/(1-y**2) * observables.dGdq2(J_h))/2.

Expand Down

0 comments on commit 21ba0b0

Please sign in to comment.