From 8c6acf03016e3d91dd5cb687039e34d53817bfd4 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich <yannick.ulrich@psi.ch> Date: Sun, 9 Feb 2020 11:33:28 +0100 Subject: [PATCH] 6: load dataset by regex in re #17 --- pymule/__init__.py | 1 + pymule/loader.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 pymule/loader.py diff --git a/pymule/__init__.py b/pymule/__init__.py index 2bbb715..10acc39 100644 --- a/pymule/__init__.py +++ b/pymule/__init__.py @@ -4,3 +4,4 @@ import matplotlib.pyplot as plt from vegas import importvegas from errortools import mergenumbers, plusnumbers, dividenumbers, timesnumbers,\ mergeplots, addplots, combineNplots, scaleplot +from loader import importreg, pattern diff --git a/pymule/loader.py b/pymule/loader.py new file mode 100644 index 0000000..f8e559c --- /dev/null +++ b/pymule/loader.py @@ -0,0 +1,36 @@ +import re +from vegas import importvegas +import os + + +def importreg(r, folder='.', filenames=None, + merge={}, types=[int,float,float]): + if filenames is None: + filenames = os.listdir(folder) + dic = {} + for filename in filenames: + m = re.match(r, filename) + if m: + v = importvegas(os.path.join(folder, filename)) + for p, n in merge.iteritems(): + if n > 0: + v[p] = mergebins(v[p], n) + + if len(types) == len(m.groups()): + k = tuple(f(v) for v,f in zip(m.groups(), types)) + else: + k = m.groups() + dic[k] = v + + return dic + + +def pattern(piece='.*', flavour='.*', obs='', folderp='.*'): + if len(obs) > 0: + return ( + folderp + piece + '_' + flavour + '_S(\d+)X([\d\.]+)D([\d\.]+).*' + ) + else: + return ( + folderp + piece + '_' + flavour + '_S(\d+)X([\d\.]+)D([\d\.]+).*' + ) -- GitLab