Skip to content

Commit

Permalink
add modifications + nb
Browse files Browse the repository at this point in the history
  • Loading branch information
danlessa committed Dec 15, 2023
1 parent 550f893 commit 42b5c63
Show file tree
Hide file tree
Showing 9 changed files with 19,185 additions and 47 deletions.
6 changes: 3 additions & 3 deletions 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.execution import easy_run
from cadCAD.tools.profiling import profile_run
from cadCAD.tools.utils import generic_suf
2 changes: 1 addition & 1 deletion cadCAD/tools/execution/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from cadCAD_tools.execution.easy_run import easy_run
from cadCAD.tools.execution.easy_run import easy_run
14 changes: 10 additions & 4 deletions cadCAD/tools/execution/easy_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import types
from typing import Dict, Union

import pandas as pd
import pandas as pd # type: ignore
from cadCAD.configuration import Experiment
from cadCAD.configuration.utils import config_sim
from cadCAD.engine import ExecutionContext, ExecutionMode, Executor


def describe_or_return(v: object) -> object:
"""
Thanks @LinuxIsCool!
"""
if isinstance(v, types.FunctionType):
return f'function: {v.__name__}'
elif isinstance(v, types.LambdaType) and v.__name__ == '<lambda>':
Expand All @@ -18,6 +21,9 @@ def describe_or_return(v: object) -> object:


def select_M_dict(M_dict: Dict[str, object], keys: set) -> Dict[str, object]:
"""
Thanks @LinuxIsCool!
"""
return {k: describe_or_return(v) for k, v in M_dict.items() if k in keys}


Expand All @@ -42,7 +48,7 @@ def easy_run(

# Set-up sim_config
simulation_parameters = {'N': N_samples, 'T': range(N_timesteps), 'M': params}
sim_config = config_sim(simulation_parameters)
sim_config = config_sim(simulation_parameters) # type: ignore

# Create a new experiment
exp = Experiment()
Expand Down Expand Up @@ -85,11 +91,11 @@ def easy_run(
if assign_params == True:
pass
else:
params_set &= assign_params
params_set &= assign_params # type: ignore

# Logic for getting the assign params criteria
if type(assign_params) is list:
selected_params = set(assign_params) & params_set
selected_params = set(assign_params) & params_set # type: ignore
elif type(assign_params) is set:
selected_params = assign_params & params_set
else:
Expand Down
13 changes: 7 additions & 6 deletions cadCAD/tools/preparation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Mapping
from cadCAD_tools.execution import easy_run
from pandas import DataFrame
from cadCAD_tools.types import Param, SystemParameters, InitialState, List, ParamSweep, TimestepBlock
from cadCAD.tools.execution import easy_run
from pandas import DataFrame # type: ignore
from cadCAD.types import *
from cadCAD.tools.types import *
from itertools import product
from dataclasses import dataclass


def sweep_cartesian_product(sweep_params: Mapping[str, tuple]) -> Mapping[str, tuple]:
def sweep_cartesian_product(sweep_params: SweepableParameters) -> SweepableParameters:
"""
Makes a cartesian product from dictionary values.
This is useful for plugging inside the sys_params dict, like:
Expand Down Expand Up @@ -35,7 +36,7 @@ def prepare_params(params: SystemParameters,
for k, v in params.items()
if type(v) is Param}

sweep_params: Mapping = {k: v.value
sweep_params: SweepableParameters = {k: v.value
for k, v in params.items()
if type(v) is ParamSweep}
if cartesian_sweep is True:
Expand All @@ -57,7 +58,7 @@ def prepare_state(state: InitialState) -> Mapping[str, object]:
class ConfigurationWrapper():
initial_state: InitialState
params: SystemParameters
timestep_block: TimestepBlock
timestep_block: StateUpdateBlocks
timesteps: int
samples: int

Expand Down
2 changes: 1 addition & 1 deletion cadCAD/tools/profiling/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from cadCAD_tools.profiling.profile_run import profile_run
from cadCAD.tools.profiling.profile_run import profile_run
20 changes: 10 additions & 10 deletions cadCAD/tools/profiling/profile_run.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
from typing import Dict
from cadCAD_tools import easy_run
from cadCAD_tools.types import TimestepBlock, StateVariable, Parameter
from cadCAD.tools import easy_run
from cadCAD.types import StateUpdateBlocks, Parameters, State, StateUpdateBlock
from time import time
import pandas as pd
import pandas as pd # type: ignore


def MEASURE_TIME_SUF(p, s, h, v, p_i): return ('run_time', time())


MEASURING_BLOCK = {
MEASURING_BLOCK: StateUpdateBlock = {
'label': 'Time Measure',
'policies': {},
'variables': {
'run_time': MEASURE_TIME_SUF
}
}
} # type: ignore


def profile_psubs(psubs: TimestepBlock, profile_substeps=True) -> TimestepBlock:
def profile_psubs(psubs: StateUpdateBlocks, profile_substeps=True) -> StateUpdateBlocks:
"""
Updates a TimestepBlock so that a time measuring function is added.
"""
new_timestep_block = []
new_timestep_block: StateUpdateBlocks = []
new_timestep_block.append(MEASURING_BLOCK)
if profile_substeps is True:
for psub in psubs:
Expand All @@ -32,9 +32,9 @@ def profile_psubs(psubs: TimestepBlock, profile_substeps=True) -> TimestepBlock:
return new_timestep_block


def profile_run(state_variables: Dict[str, StateVariable],
params: Dict[str, Parameter],
psubs: TimestepBlock,
def profile_run(state_variables: State,
params: Parameters,
psubs: StateUpdateBlocks,
*args,
profile_substeps=True,
**kwargs) -> pd.DataFrame:
Expand Down
21 changes: 2 additions & 19 deletions cadCAD/tools/types.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
from typing import Callable, List, Dict, Tuple, Union, NamedTuple

StateVariable = object
Parameter = Tuple[object]

Params = Dict[str, object]
State = Dict[str, object]
Signal = Dict[str, object]
History = List[List[State]]
Substep = int
VariableUpdate = Tuple[str, object]

Policy = Callable[[Params, Substep, History, State], Signal]
StateUpdate = Callable[[Params, Substep, History, State, Signal], VariableUpdate]

StateUpdateBlock = Dict[str, object]
TimestepBlock = List[StateUpdateBlock]
from typing import NamedTuple, Tuple, Dict, Union, List

class InitialValue(NamedTuple):
value: object
Expand All @@ -27,9 +11,8 @@ class Param(NamedTuple):


class ParamSweep(NamedTuple):
value: Tuple[object]
value: List[object]
type: type


InitialState = Dict[str, InitialValue]
SystemParameters = Dict[str, Union[Param, ParamSweep]]
6 changes: 3 additions & 3 deletions cadCAD/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cadCAD_tools.types import Signal, StateUpdate, VariableUpdate
from cadCAD.types import *

def generic_suf(variable: str,
signal: str='') -> StateUpdate:
signal: str='') -> StateUpdateFunction:
"""
Generate a State Update Function that assigns the signal value to the
given variable. By default, the signal has the same identifier as the
Expand All @@ -12,6 +12,6 @@ def generic_suf(variable: str,
else:
pass

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

0 comments on commit 42b5c63

Please sign in to comment.