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

add support for multi-returning-value functions in transform #4301

Merged
merged 16 commits into from
Feb 28, 2024
22 changes: 16 additions & 6 deletions pycbc/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,14 @@ def transform(self, maps):
# ensure that we return the same data type in each dict
getslice = self._getslice(maps)
# evaluate the functions
# func[0] is the function itself, func[1] is the index,
# this supports multiple returning values function
out = {
p: self._scratch[func][getslice]
for p, func in self.transform_functions.items()
}
p: self._scratch[func[0]][func[1]][getslice] if
len(self._scratch[func[0]]) > 1 else
self._scratch[func[0]][getslice]
for p, func in self.transform_functions.items()
}
return self.format_output(maps, out)

def jacobian(self, maps):
Expand Down Expand Up @@ -301,15 +305,21 @@ def from_config(cls, cp, section, outputs):
jacobian = func(inputs)
"""
tag = outputs
outputs = set(outputs.split(VARARGS_DELIM))
outputs = list(outputs.split(VARARGS_DELIM))
all_vars = ", ".join(outputs)
inputs = map(str.strip,
cp.get_opt_tag(section, "inputs", tag).split(","))
# get the functions for each output
transform_functions = {}
output_index = slice(None, None, None)
for var in outputs:
# check if option can be cast as a float
func = cp.get_opt_tag(section, var, tag)
transform_functions[var] = func
try:
func = cp.get_opt_tag(section, var, tag)
except Exception:
func = cp.get_opt_tag(section, all_vars, tag)
output_index = slice(outputs.index(var), outputs.index(var)+1)
transform_functions[var] = [func, output_index]
s = "-".join([section, tag])
if cp.has_option(s, "jacobian"):
jacobian = cp.get_opt_tag(section, "jacobian", tag)
Expand Down