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

feat: allow transient variables to return any kind of data #389

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions damnit/ctxsupport/ctxrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ def execute(self, run_data, run_number, proposal, input_vars) -> 'Results':
if annotation.startswith("var#"):
dep_name = annotation.removeprefix("var#")
if dep_name in res:
kwargs[arg_name] = res[dep_name].data
dep_data = res[dep_name]
if isinstance(dep_data, Cell):
dep_data = dep_data.data
kwargs[arg_name] = dep_data
elif param.default is inspect.Parameter.empty:
missing_deps.append(dep_name)

Expand Down Expand Up @@ -343,11 +346,12 @@ def execute(self, run_data, run_number, proposal, input_vars) -> 'Results':
if (data := func(run_data)) is None:
continue

if not isinstance(data, Cell):
data = Cell(data)
if not var.transient:
if not isinstance(data, Cell):
data = Cell(data)

if data.summary is None:
data.summary = var.summary
if data.summary is None:
data.summary = var.summary
except Exception:
log.error("Could not get data for %s", name, exc_info=True)
else:
Expand Down
3 changes: 2 additions & 1 deletion docs/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ these arguments:
- `cluster` (bool): whether or not to execute this variable in a Slurm job. This
should always be used if the variable does any heavy processing.
- `transient` (bool): do not save the variable's result to the database. This
is useful for e.g. intermediate results to be reused by other Variables. By
is useful for e.g. intermediate results to be reused by other Variables. Since
their data isn't saved, `transient` variables can return any object. By
default Variables do save their results (transient=False).

Variable functions can return any of:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,12 @@ def var1(run):

@Variable(transient=True)
def var2(run, data: 'var#var1'):
return np.arange(data)
# transient vars can return any data type
return [np.arange(data), run]

@Variable(summary='max')
def var3(run, data: 'var#var2'):
data, run = data
return data.size * data
"""
ctx = mkcontext(ctx_code)
Expand Down