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

Created add_parameter_labels #360

Merged
merged 1 commit into from
Apr 18, 2024
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
2 changes: 1 addition & 1 deletion cadCAD/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from cadCAD.tools.execution import easy_run
from cadCAD.tools.profiling import profile_run
from cadCAD.tools.utils import generic_suf
from cadCAD.tools.utils import generic_suf, add_parameter_labels
37 changes: 32 additions & 5 deletions cadCAD/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
from cadCAD.types import *
import pandas as pd

def generic_suf(variable: str,
signal: str='') -> StateUpdateFunction:

def generic_suf(variable: str, signal: str = "") -> StateUpdateFunction:
"""
Generate a State Update Function that assigns the signal value to the
Generate a State Update Function that assigns the signal value to the
given variable. By default, the signal has the same identifier as the
variable.
"""
if signal is '':
if signal is "":
signal = variable
else:
pass

def suf(_1, _2, _3, _4, signals: PolicyOutput) -> StateUpdateTuple:
return (variable, signals[signal])
return suf

return suf


def add_parameter_labels(configs: list, df: pd.DataFrame) -> pd.DataFrame:
"""Utility function to add the parameters to a dataframe after processing

Args:
configs (list): The configurations of the simulations
df (pd.DataFrame): Simulation dataframe

Returns:
pd.DataFrame: Simulation dataframe with labels
"""

# Find the relevant parameters
sim_params = pd.DataFrame([x.sim_config["M"] for x in configs])
sim_params[["subset", "simulation", "run"]] = [
[x.subset_id, x.simulation_id, x.run_id] for x in configs
]
# Fix because run_id is 0 indexed, but cadCAD dataframe is 1 indexed for runs
sim_params["run"] += 1

# Join
sim_params = sim_params.set_index(["subset", "simulation", "run"])
df = df.join(sim_params, on=["subset", "simulation", "run"])
return df