Skip to content

Commit

Permalink
Merge branch 'master' into doc_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuellima1 authored Dec 14, 2023
2 parents 285111f + 0aa86be commit 842f5f4
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 118 deletions.
67 changes: 0 additions & 67 deletions .circleci/config.yml

This file was deleted.

61 changes: 61 additions & 0 deletions .github/workflows/cadcad-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This workflow will install Python dependencies and run tests with multiple versions of Python

name: cadCAD CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:
continue-on-error: true

strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Display Python version
run: python -c "import sys; print(sys.version)"

- name: Install test and build dependencies
run: |
python -m pip install --upgrade pip
python -m pip install jupyter
pip install -r requirements.txt
- name: Build cadCAD
run: |
python setup.py bdist_wheel
python -m pip install dist/*.whl --force-reinstall
- name: Run tests
run: |
python testing/tests/multi_model_row_count.py
python testing/tests/param_sweep.py
python testing/tests/policy_aggregation.py
python testing/tests/timestep1psub0.py
python testing/tests/runs_not_zero.py
python testing/tests/run1psub0.py
python testing/tests/append_mod_test.py
python testing/tests/cadCAD_exp.py
- name: Run Jupyter test
run: |
python testing/tests/import_cadCAD_test.py
6 changes: 4 additions & 2 deletions cadCAD/engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from time import time
from typing import Callable, Dict, List, Any, Tuple
from typing import Callable, Dict, List, Any, Tuple, Union

from cadCAD.utils import flatten
from cadCAD.utils.execution import print_exec_info
Expand Down Expand Up @@ -39,6 +39,7 @@ def auto_mode_switcher(config_amt: int):
class ExecutionContext:
def __init__(self, context=ExecutionMode.local_mode, method=None, additional_objs=None) -> None:
self.name = context
self.additional_objs = additional_objs
if context == 'local_proc':
self.method = local_simulations
elif context == 'single_proc':
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self,
self.SimExecutor = SimExecutor
self.exec_method = exec_context.method
self.exec_context = exec_context.name
self.additional_objs = exec_context.additional_objs
self.configs = configs
self.empty_return = empty_return

Expand Down Expand Up @@ -174,7 +176,7 @@ def get_final_results(simulations: List[StateHistory],
print("Execution Method: " + self.exec_method.__name__)
simulations_results = self.exec_method(
sim_executors, var_dict_list, states_lists, configs_structs, env_processes_list, Ts, SimIDs, RunIDs,
ExpIDs, SubsetIDs, SubsetWindows, original_N
ExpIDs, SubsetIDs, SubsetWindows, original_N, self.additional_objs
)

final_result = get_final_results(
Expand Down
17 changes: 10 additions & 7 deletions cadCAD/engine/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def single_proc_exec(
ExpIDs: List[int],
SubsetIDs: List[SubsetID],
SubsetWindows: List[SubsetWindow],
configured_n: List[N_Runs]
configured_n: List[N_Runs],
additional_objs=None
):

# HACK for making it run with N_Runs=1
Expand All @@ -38,7 +39,7 @@ def single_proc_exec(
map(lambda x: x.pop(), raw_params)
)
result = simulation_exec(
var_dict_list, states_list, config, env_processes, T, sim_id, N, subset_id, subset_window, configured_n
var_dict_list, states_list, config, env_processes, T, sim_id, N, subset_id, subset_window, configured_n, additional_objs
)
return flatten(result)

Expand All @@ -58,7 +59,8 @@ def parallelize_simulations(
ExpIDs: List[int],
SubsetIDs: List[SubsetID],
SubsetWindows: List[SubsetWindow],
configured_n: List[N_Runs]
configured_n: List[N_Runs],
additional_objs=None
):

print(f'Execution Mode: parallelized')
Expand Down Expand Up @@ -96,7 +98,7 @@ def process_executor(params):
if len_configs_structs > 1:
pp = PPool(processes=len_configs_structs)
results = pp.map(
lambda t: t[0](t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9], configured_n), params
lambda t: t[0](t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9], configured_n, additional_objs), params
)
pp.close()
pp.join()
Expand All @@ -123,18 +125,19 @@ def local_simulations(
ExpIDs: List[int],
SubsetIDs: List[SubsetID],
SubsetWindows: List[SubsetWindow],
configured_n: List[N_Runs]
configured_n: List[N_Runs],
additional_objs=None
):
config_amt = len(configs_structs)

if config_amt == 1: # and configured_n != 1
return single_proc_exec(
simulation_execs, var_dict_list, states_lists, configs_structs, env_processes_list,
Ts, SimIDs, Ns, ExpIDs, SubsetIDs, SubsetWindows, configured_n
Ts, SimIDs, Ns, ExpIDs, SubsetIDs, SubsetWindows, configured_n, additional_objs
)
elif config_amt > 1: # and configured_n != 1
return parallelize_simulations(
simulation_execs, var_dict_list, states_lists, configs_structs, env_processes_list,
Ts, SimIDs, Ns, ExpIDs, SubsetIDs, SubsetWindows, configured_n
Ts, SimIDs, Ns, ExpIDs, SubsetIDs, SubsetWindows, configured_n, additional_objs
)
# elif config_amt > 1 and configured_n == 1:
50 changes: 31 additions & 19 deletions cadCAD/engine/simulation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Any, Callable, Dict, List, Tuple
from copy import deepcopy
from types import MappingProxyType
from functools import reduce
from funcy import curry
from funcy import curry # type: ignore

from cadCAD.utils import flatten
from cadCAD.engine.utils import engine_exception
from cadCAD.types import *

id_exception: Callable = curry(engine_exception)(KeyError)(KeyError)(None)

Expand Down Expand Up @@ -102,20 +104,30 @@ def env_composition(target_field, state_dict, target_value):
# mech_step
def partial_state_update(
self,
sweep_dict: Dict[str, List[Any]],
sub_step: int,
sL,
sH,
state_funcs: List[Callable],
policy_funcs: List[Callable],
env_processes: Dict[str, Callable],
sweep_dict: Parameters,
sub_step: Substep,
sL: list[State],
sH: StateHistory,
state_funcs: List[StateUpdateFunction],
policy_funcs: List[PolicyFunction],
env_processes: EnvProcesses,
time_step: int,
run: int,
additional_objs
) -> List[Dict[str, Any]]:

# last_in_obj: Dict[str, Any] = MappingProxyType(sL[-1])
last_in_obj: Dict[str, Any] = deepcopy(sL[-1])
if type(additional_objs) == dict:
if additional_objs.get('deepcopy_off', False) == True:
last_in_obj = MappingProxyType(sL[-1])
if len(additional_objs) == 1:
additional_objs = None
# XXX: drop the additional objects if only used for deepcopy
# toggling.
else:
last_in_obj = deepcopy(sL[-1])
else:
last_in_obj = deepcopy(sL[-1])

_input: Dict[str, Any] = self.policy_update_exception(
self.get_policy_input(sweep_dict, sub_step, sH, last_in_obj, policy_funcs, additional_objs)
)
Expand Down Expand Up @@ -206,18 +218,18 @@ def run_pipeline(

def simulation(
self,
sweep_dict: Dict[str, List[Any]],
states_list: List[Dict[str, Any]],
sweep_dict: SweepableParameters,
states_list: StateHistory,
configs,
env_processes: Dict[str, Callable],
time_seq: range,
simulation_id: int,
env_processes: EnvProcesses,
time_seq: TimeSeq,
simulation_id: SimulationID,
run: int,
subset_id,
subset_window,
configured_N,
subset_id: SubsetID,
subset_window: SubsetWindow,
configured_N: int,
# remote_ind
additional_objs=None
additional_objs: Union[None, Dict]=None
):
run += 1
subset_window.appendleft(subset_id)
Expand Down
8 changes: 4 additions & 4 deletions cadCAD/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ class ConfigurationDict(TypedDict):
N: int # Number of MC Runs
M: Union[Parameters, SweepableParameters] # Parameters / List of Parameter to Sweep


EnvProcesses = object
TargetValue = object
EnvProcess: Callable[[State, SweepableParameters, TargetValue], TargetValue]
EnvProcesses = dict[str, Callable]
TimeSeq = Iterator
SimulationID = int
Run = int
SubsetID = int
SubsetWindow = Iterator
N_Runs = int


ExecutorFunction = Callable[[Parameters, StateHistory, StateUpdateBlocks, EnvProcesses, TimeSeq, SimulationID, Run, SubsetID, SubsetWindow, N_Runs], object]
ExecutionParameter = Tuple[ExecutorFunction, Parameters, StateHistory, StateUpdateBlocks, EnvProcesses, TimeSeq, SimulationID, Run, SubsetID, SubsetWindow, N_Runs]

Expand All @@ -45,4 +45,4 @@ class SessionDict(TypedDict):
simulation_id: int
run_id: int
subset_id: int
subset_window: deque
subset_window: deque
30 changes: 11 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
-i https://pypi.org/simple

matplotlib==3.3.2
networkx==2.5
parameterized==0.7.4
plotly==4.10.0
pytest==6.0.2
scikit-learn==0.23.2
scipy>=1.5.2
seaborn==0.11.0
tabulate==0.8.7
xarray==0.16.0
wheel==0.38.1
pandas==1.1.5
fn==0.4.3
funcy==1.16
dill==0.3.4
pathos==0.2.8
numpy==1.22.0
pytz==2021.1
six>=1.11.0
parameterized>=0.7.4
pytest>=6.0.2
tabulate>=0.8.7
wheel>=0.38.1
pandas>=1.1.5
funcy>=1.16
dill>=0.3.4
pathos>=0.2.8
numpy>=1.22.0
pytz>=2021.1
setuptools>=69.0.2
Loading

0 comments on commit 842f5f4

Please sign in to comment.