diff --git a/src/postprocessor/chainer.py b/src/postprocessor/chainer.py index 3462ff38d3493a7086bf6d14b20581a80c3ccd75..fb50c792805c04cfc42434558c35f9f1783da102 100644 --- a/src/postprocessor/chainer.py +++ b/src/postprocessor/chainer.py @@ -17,61 +17,36 @@ from postprocessor.core.lineageprocess import LineageProcessParameters class Chainer(Signal): """ Extend Signal by applying post-processes and allowing composite signals that combine basic signals. + It "chains" multiple processes upon fetching a dataset to produce the desired datasets. Instead of reading processes previously applied, it executes them when called. """ - equivalences = { + _synonyms = { "m5m": ("extraction/GFP/max/max5px", "extraction/GFP/max/median") } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - for channel in self.candidate_channels: - # find first channel in h5 file that corresponds to a candidate_channel - # but channel is redefined. why is there a loop over candidate channels? - # what about capitals? - try: - channel = [ - ch for ch in self.channels if re.match("channel", ch) - ][0] - break - except: - # is this still a good idea? - pass - try: - # what's this? - # composite statistic comprising the quotient of two others - equivalences = { - "m5m": ( - f"extraction/{channel}/max/max5px", - f"extraction/{channel}/max/median", - ), - } + def replace_path(path: str, bgsub: bool = ""): # function to add bgsub to paths - def replace_path(path: str, bgsub: str = ""): - channel = path.split("/")[1] - if "bgsub" in bgsub: - # add bgsub to path - path = re.sub(channel, f"{channel}_bgsub", path) - return path - - # for composite statistics - # add chain with and without bgsub - self.common_chains = { - alias - + bgsub: lambda **kwargs: self.get( - replace_url(denominator, alias + bgsub), **kwargs - ) - / self.get(replace_path(numerator, alias + bgsub), **kwargs) - for alias, (denominator, numerator) in equivalences.items() - for bgsub in ("", "_bgsub") - } - except: - # Is this still a good idea? - pass + channel = path.split("/")[1] + suffix = "_bgsub" if bgsub else "" + path = re.sub(channel, f"{channel}{suffix}", path) + return path + + # Add chain with and without bgsub for composite statistics + self.common_chains = { + alias + + bgsub: lambda **kwargs: self.get( + replace_path(denominator, alias + bgsub), **kwargs + ) + / self.get(replace_path(numerator, alias + bgsub), **kwargs) + for alias, (denominator, numerator) in self.synonyms.items() + for bgsub in ("", "_bgsub") + } def get( self, @@ -83,7 +58,6 @@ class Chainer(Signal): **kwargs, ): """Load data from an h5 file.""" - 1 / 0 if dataset in self.common_chains: # get dataset for composite chains data = self.common_chains[dataset](**kwargs) @@ -149,7 +123,5 @@ class Chainer(Signal): if process_type == "reshapers": if process == "merger": raise (NotImplementedError) - merges = process.as_function(result, **params) - result = self.apply_merges(result, merges) self._intermediate_steps.append(result) return result