Skip to content

Commit

Permalink
Fix tests and also the bugs which caused the tests to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
IshaanDesai committed Dec 13, 2024
1 parent a4baea0 commit fd00407
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
3 changes: 0 additions & 3 deletions micro_manager/adaptivity/adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ def __init__(self, configurator, logger) -> None:
configurator.get_adaptivity_similarity_measure()
)

self._active_sim_ids = np.array([], dtype=int)
self._inactive_sim_ids = np.array([], dtype=int)

def _get_similarity_dists(
self, dt: float, similarity_dists: np.ndarray, data: dict
) -> np.ndarray:
Expand Down
29 changes: 18 additions & 11 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ def compute_adaptivity(
similarity_dists, is_sim_active, sim_is_associated_to
)

self._active_sim_ids = np.where(
is_sim_active[self._global_ids[0] : self._global_ids[-1] + 1]
)[0]
self._inactive_sim_ids = np.where(
is_sim_active[self._global_ids[0] : self._global_ids[-1] + 1] == False
)[0]

self._logger.info(
"{} active simulations, {} inactive simulations".format(
np.count_nonzero(
Expand All @@ -148,27 +141,41 @@ def compute_adaptivity(

return [similarity_dists, is_sim_active, sim_is_associated_to]

def get_active_sim_ids(self) -> np.ndarray:
def get_active_sim_ids(self, is_sim_active: np.array) -> np.ndarray:
"""
Get the ids of active simulations.
Parameters
----------
is_sim_active : numpy array
1D array having state (active or inactive) of each micro simulation
Returns
-------
numpy array
1D array of active simulation ids
"""
return self._active_sim_ids
return np.where(is_sim_active[self._global_ids[0] : self._global_ids[-1] + 1])[
0
]

def get_inactive_sim_ids(self) -> np.ndarray:
def get_inactive_sim_ids(self, is_sim_active: np.array) -> np.ndarray:
"""
Get the ids of inactive simulations.
Parameters
----------
is_sim_active : numpy array
1D array having state (active or inactive) of each micro simulation
Returns
-------
numpy array
1D array of inactive simulation ids
"""
return self._inactive_sim_ids
return np.where(
is_sim_active[self._global_ids[0] : self._global_ids[-1] + 1] == False
)[0]

def get_full_field_micro_output(
self, adaptivity_data: list, micro_output: list
Expand Down
26 changes: 18 additions & 8 deletions micro_manager/adaptivity/local_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,39 @@ def compute_adaptivity(
)
)

self._active_sim_ids = np.where(is_sim_active)[0]
self._inactive_sim_ids = np.where(is_sim_active == False)[0]

return [similarity_dists, is_sim_active, sim_is_associated_to]

def get_active_sim_ids(self) -> np.ndarray:
def get_active_sim_ids(self, is_sim_active) -> np.ndarray:
"""
Get the ids of active simulations.
Parameters
----------
is_sim_active : numpy array
1D array having state (active or inactive) of each micro simulation
Returns
-------
numpy array
1D array of active simulation ids
"""
return self._active_sim_ids
return np.where(is_sim_active)[0]

def get_inactive_sim_ids(self) -> np.ndarray:
def get_inactive_sim_ids(self, is_sim_active: np.array) -> np.ndarray:
"""
Get the ids of inactive simulations.
Parameters
----------
is_sim_active : numpy array
1D array having state (active or inactive) of each micro simulation
Returns
-------
numpy array
1D array of inactive simulation ids
"""
return self._inactive_sim_ids
return np.where(is_sim_active == False)[0]

def get_full_field_micro_output(
self, adaptivity_data: list, micro_output: list
Expand All @@ -119,6 +126,7 @@ def get_full_field_micro_output(
----------
adaptivity_data : list
List of numpy arrays:
similarity_dists (2D array having similarity distances between each micro simulation pair)
is_sim_active (1D array having state (active or inactive) of each micro simulation)
sim_is_associated_to (1D array with values of associated simulations of inactive simulations. Active simulations have None)
micro_output : list
Expand All @@ -133,7 +141,9 @@ def get_full_field_micro_output(

sim_is_associated_to = adaptivity_data[2]

for inactive_id in self._inactive_sim_ids:
inactive_sim_ids = self.get_inactive_sim_ids(adaptivity_data[1])

for inactive_id in inactive_sim_ids:
micro_sims_output[inactive_id] = deepcopy(
micro_sims_output[sim_is_associated_to[inactive_id]]
)
Expand Down
14 changes: 10 additions & 4 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,19 @@ def _solve_micro_simulations_with_adaptivity(
self._data_for_adaptivity,
)

active_sim_ids = self._adaptivity_controller.get_active_sim_ids()
active_sim_ids = self._adaptivity_controller.get_active_sim_ids(
adaptivity_data[1]
)

for active_id in active_sim_ids:
self._micro_sims_active_steps[active_id] += 1

active_sim_ids = self._adaptivity_controller.get_active_sim_ids()
inactive_sim_ids = self._adaptivity_controller.get_inactive_sim_ids()
active_sim_ids = self._adaptivity_controller.get_active_sim_ids(
adaptivity_data[1]
)
inactive_sim_ids = self._adaptivity_controller.get_inactive_sim_ids(
adaptivity_data[1]
)

micro_sims_output = [None] * self._local_number_of_sims

Expand Down Expand Up @@ -779,7 +785,7 @@ def _solve_micro_simulations_with_adaptivity(
micro_sims_input, micro_sims_output, unset_sim, active_sim_ids
)

self._adaptivity_controller.get_full_field_micro_output(
micro_sims_output = self._adaptivity_controller.get_full_field_micro_output(
adaptivity_data, micro_sims_output
)

Expand Down
11 changes: 8 additions & 3 deletions tests/unit/test_micro_simulation_crash_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ def test_crash_handling_with_adaptivity(self):
)
manager._micro_sims = [MicroSimulation(i) for i in range(5)]

is_sim_active = np.array([True, True, True, True, False])
sim_is_associated_to = np.array([-2, -2, -2, -2, 2])
adaptivity_data = []
adaptivity_data.append(np.array([0, 0, 0, 0, 0])) # similarity_dists
adaptivity_data.append(
np.array([True, True, True, True, False])
) # is_sim_active
adaptivity_data.append(np.array([-2, -2, -2, -2, 2])) # sim_is_associated_to

micro_sims_output = manager._solve_micro_simulations_with_adaptivity(
macro_data, is_sim_active, sim_is_associated_to, 1.0
macro_data, 1.0, adaptivity_data
)

# Crashed simulation has interpolated value
Expand Down

0 comments on commit fd00407

Please sign in to comment.