From d594827de69a0975d08179ef145103fde08e2975 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 13 Oct 2023 13:53:17 -0400 Subject: [PATCH 1/7] Pass ID to micro simulations --- micro_manager/micro_simulation.py | 3 ++- tests/integration/test_unit_cube/micro_dummy.py | 3 ++- tests/unit/test_micro_manager.py | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/micro_manager/micro_simulation.py b/micro_manager/micro_simulation.py index df505bb3..7540efec 100644 --- a/micro_manager/micro_simulation.py +++ b/micro_manager/micro_simulation.py @@ -21,13 +21,14 @@ def create_simulation_class(micro_simulation_class): """ class Simulation(micro_simulation_class): def __init__(self, global_id): - micro_simulation_class.__init__(self) + micro_simulation_class.__init__(self, global_id) self._global_id = global_id def get_global_id(self) -> int: return self._global_id def set_global_id(self, global_id) -> None: + # TODO: Simulation objects have been initialized with a global ID, which we can no longer change, so change global ID here is problematic and will create a mismatch self._global_id = global_id return Simulation diff --git a/tests/integration/test_unit_cube/micro_dummy.py b/tests/integration/test_unit_cube/micro_dummy.py index 59bef26c..433967ff 100644 --- a/tests/integration/test_unit_cube/micro_dummy.py +++ b/tests/integration/test_unit_cube/micro_dummy.py @@ -6,10 +6,11 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): """ Constructor of MicroSimulation class. """ + self._sim_id = sim_id self._micro_scalar_data = None self._micro_vector_data = None self._checkpoint = None diff --git a/tests/unit/test_micro_manager.py b/tests/unit/test_micro_manager.py index c020f09d..f03509d2 100644 --- a/tests/unit/test_micro_manager.py +++ b/tests/unit/test_micro_manager.py @@ -5,7 +5,7 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): self.very_important_value = 0 def initialize(self): @@ -85,7 +85,7 @@ def test_solve_mico_sims(self): """ manager = micro_manager.MicroManager('micro-manager-config.json') manager._local_number_of_sims = 4 - manager._micro_sims = [MicroSimulation() for _ in range(4)] + manager._micro_sims = [MicroSimulation(i) for i in range(4)] manager._micro_sims_active_steps = np.zeros(4, dtype=np.int32) micro_sims_output = manager._solve_micro_simulations(self.fake_read_data) From 9e2fc8d5a5c48a5e83c90b11eedf7a180770cf55 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 13 Oct 2023 14:32:56 -0400 Subject: [PATCH 2/7] Pass ID to micro simulation class constructors in dummies --- examples/cpp-dummy/micro_cpp_dummy.cpp | 2 +- examples/cpp-dummy/micro_cpp_dummy.hpp | 3 ++- examples/python-dummy/micro_dummy.py | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/cpp-dummy/micro_cpp_dummy.cpp b/examples/cpp-dummy/micro_cpp_dummy.cpp index df922470..56ff4f37 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.cpp +++ b/examples/cpp-dummy/micro_cpp_dummy.cpp @@ -13,7 +13,7 @@ #include "micro_cpp_dummy.hpp" // Constructor -MicroSimulation::MicroSimulation() : _micro_scalar_data(0), _state(0) {} +MicroSimulation::MicroSimulation(int sim_id) : _sim_id(sim_id), _micro_scalar_data(0), _state(0) {} // Solve py::dict MicroSimulation::solve(py::dict macro_data, double dt) diff --git a/examples/cpp-dummy/micro_cpp_dummy.hpp b/examples/cpp-dummy/micro_cpp_dummy.hpp index d370afc2..fb230ea1 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.hpp +++ b/examples/cpp-dummy/micro_cpp_dummy.hpp @@ -14,7 +14,7 @@ namespace py = pybind11; class MicroSimulation { public: - MicroSimulation(); + MicroSimulation(int sim_id); // solve takes a python dict data, and the timestep dt as inputs, and returns a python dict py::dict solve(py::dict macro_write_data, double dt); @@ -22,6 +22,7 @@ class MicroSimulation py::list get_state() const; private: + int _sim_id; double _micro_scalar_data; std::vector _micro_vector_data; double _state; diff --git a/examples/python-dummy/micro_dummy.py b/examples/python-dummy/micro_dummy.py index f267d3b9..eeed2984 100644 --- a/examples/python-dummy/micro_dummy.py +++ b/examples/python-dummy/micro_dummy.py @@ -6,10 +6,11 @@ class MicroSimulation: - def __init__(self): + def __init__(self, sim_id): """ Constructor of MicroSimulation class. """ + self._sim_id = sim_id self._dims = 3 self._micro_scalar_data = None self._micro_vector_data = None From 01d9bf56f04c42bd221d4a5fbd0ade1df749ae28 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 13 Oct 2023 14:37:47 -0400 Subject: [PATCH 3/7] Formatting --- micro_manager/micro_simulation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/micro_manager/micro_simulation.py b/micro_manager/micro_simulation.py index 7540efec..a0c08635 100644 --- a/micro_manager/micro_simulation.py +++ b/micro_manager/micro_simulation.py @@ -28,7 +28,9 @@ def get_global_id(self) -> int: return self._global_id def set_global_id(self, global_id) -> None: - # TODO: Simulation objects have been initialized with a global ID, which we can no longer change, so change global ID here is problematic and will create a mismatch + # TODO: Simulation objects have been initialized with a global ID, which + # we can no longer change, so change global ID here is problematic and + # will create a mismatch. self._global_id = global_id return Simulation From b656da4a1840c3c7e61687fb80928f89ce4c2039 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 13 Oct 2023 14:58:23 -0400 Subject: [PATCH 4/7] Pass the global ID in the state of the C++ micro simulation in the dummy --- examples/cpp-dummy/micro_cpp_dummy.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/cpp-dummy/micro_cpp_dummy.cpp b/examples/cpp-dummy/micro_cpp_dummy.cpp index 56ff4f37..5d271561 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.cpp +++ b/examples/cpp-dummy/micro_cpp_dummy.cpp @@ -47,14 +47,15 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt) // This function needs to set the complete state of a micro simulation void MicroSimulation::set_state(py::list state) { - _micro_scalar_data = state[0].cast(); - _state = state[1].cast(); + _sim_id = state[0].cast() + _micro_scalar_data = state[1].cast(); + _state = state[2].cast(); } // This function needs to return variables which can fully define the state of a micro simulation py::list MicroSimulation::get_state() const { - std::vector state{_micro_scalar_data, _state}; + std::vector state{_sim_id, _micro_scalar_data, _state}; py::list state_python = py::cast(state); return state_python; } @@ -73,11 +74,11 @@ PYBIND11_MODULE(micro_dummy, m) { return ms.get_state(); }, [](py::list t) { // __setstate__ - if (t.size() != 2) + if (t.size() != 3) throw std::runtime_error("Invalid state!"); /* Create a new C++ instance */ - MicroSimulation ms; + MicroSimulation ms(0); ms.set_state(t); From 086ad8cd9bcc2e89a8bfb0c33bb0a2f4060fc245 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 13 Oct 2023 16:01:21 -0400 Subject: [PATCH 5/7] Fix C++ solver dummy --- examples/cpp-dummy/micro_cpp_dummy.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/cpp-dummy/micro_cpp_dummy.cpp b/examples/cpp-dummy/micro_cpp_dummy.cpp index 5d271561..dd0b6863 100644 --- a/examples/cpp-dummy/micro_cpp_dummy.cpp +++ b/examples/cpp-dummy/micro_cpp_dummy.cpp @@ -47,15 +47,14 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt) // This function needs to set the complete state of a micro simulation void MicroSimulation::set_state(py::list state) { - _sim_id = state[0].cast() - _micro_scalar_data = state[1].cast(); - _state = state[2].cast(); + _micro_scalar_data = state[0].cast(); + _state = state[1].cast(); } // This function needs to return variables which can fully define the state of a micro simulation py::list MicroSimulation::get_state() const { - std::vector state{_sim_id, _micro_scalar_data, _state}; + std::vector state{_micro_scalar_data, _state}; py::list state_python = py::cast(state); return state_python; } @@ -65,7 +64,7 @@ PYBIND11_MODULE(micro_dummy, m) { m.doc() = "pybind11 micro dummy plugin"; py::class_(m, "MicroSimulation") - .def(py::init()) + .def(py::init()) .def("solve", &MicroSimulation::solve) .def("get_state", &MicroSimulation::get_state) .def("set_state", &MicroSimulation::set_state) @@ -74,7 +73,7 @@ PYBIND11_MODULE(micro_dummy, m) { return ms.get_state(); }, [](py::list t) { // __setstate__ - if (t.size() != 3) + if (t.size() != 2) throw std::runtime_error("Invalid state!"); /* Create a new C++ instance */ From e98703915ca6214b425d2789b0e2a1406a7b4671 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Sat, 14 Oct 2023 23:00:00 -0400 Subject: [PATCH 6/7] Remove function set_global_id from Simulation class, as we should never have to change the global ID of a micro simulation --- micro_manager/micro_simulation.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/micro_manager/micro_simulation.py b/micro_manager/micro_simulation.py index a0c08635..902eaba1 100644 --- a/micro_manager/micro_simulation.py +++ b/micro_manager/micro_simulation.py @@ -27,10 +27,4 @@ def __init__(self, global_id): def get_global_id(self) -> int: return self._global_id - def set_global_id(self, global_id) -> None: - # TODO: Simulation objects have been initialized with a global ID, which - # we can no longer change, so change global ID here is problematic and - # will create a mismatch. - self._global_id = global_id - return Simulation From 1219a0d4f0de58c3cee24688b6afab2ceffb457a Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 2 Feb 2024 11:16:43 +0100 Subject: [PATCH 7/7] Change documentation to add passing global id to simulation during defining --- docs/micro-simulation-convert-to-library.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/micro-simulation-convert-to-library.md b/docs/micro-simulation-convert-to-library.md index 7ed8dd48..a145852f 100644 --- a/docs/micro-simulation-convert-to-library.md +++ b/docs/micro-simulation-convert-to-library.md @@ -14,9 +14,14 @@ Restructure your micro simulation code into a Python class with the structure gi ```python class MicroSimulation: # Name is fixed - def __init__(self): + def __init__(self, sim_id): """ Constructor of class MicroSimulation. + + Parameters + ---------- + sim_id : int + ID of the simulation instance, that the Micro Manager has set for it. """ def solve(self, macro_data: dict, dt: float) -> dict: