Skip to content

Commit

Permalink
Added 2 Backend methods for netpyneunit (#196)
Browse files Browse the repository at this point in the history
* Added 2 Backend methods for netpyneunit

* Change some logic & add tests
  • Loading branch information
lakesare committed Jul 30, 2021
1 parent 3886c3d commit 4f15015
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/source/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ capability, and it can be executed to simulate and output results.
Backend

After being registered by ``register_backends`` function, a ``Backend`` instance
can be executed by a Ruunable Model at the back end. It usually does some background
can be executed by a Runnable Model at the back end. It usually does some background
computing for the runnable model.


Expand Down
32 changes: 28 additions & 4 deletions sciunit/models/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,38 @@ def backend_run(self) -> Any:
if self.use_memory_cache or self.use_disk_cache:
key = self.model.hash()
if self.use_memory_cache and self.get_memory_cache(key):
return self._results
return self.cache_to_results(self._results)
if self.use_disk_cache and self.get_disk_cache(key):
return self._results
return self.cache_to_results(self._results)
results = self._backend_run()
if self.use_memory_cache:
self.set_memory_cache(results, key)
self.set_memory_cache(self.results_to_cache(results), key)
if self.use_disk_cache:
self.set_disk_cache(results, key)
self.set_disk_cache(self.results_to_cache(results), key)
return results

def cache_to_results(self, cache: Any) -> Any:
"""A method to convert cache to some hypothetical Results object.
Args:
cache (Any): An object returned from .get_memory_cache() or .get_disk_cache().
Returns:
Any (optional): Either an object with the results of the simulation,
or None (e.g. if cache_to_results() simply injects the results into some global object).
"""
return cache

def results_to_cache(self, results: Any) -> Any:
"""A method to convert the results from your model run
into storable cache object (usually a simple dictionary or an array).
Args:
results (Any): An object returned from your ._backend_run().
Returns:
Any: The results in the format that's good for storing in cache.
"""
return results

def _backend_run(self) -> Any:
Expand Down
20 changes: 20 additions & 0 deletions sciunit/unit_test/backend_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ def _backend_run(self) -> str:
backend.set_memory_cache("value2")
backend.backend_run()

def test_backend_cache_to_results(self):
myModel = Model()
class MyBackend(Backend):
def cache_to_results(self, cache):
return { "color": "red" }

def results_to_cache(self, results):
return { "color": "blue" }

def _backend_run(self):
return { "color": "white" }

backend = MyBackend()
backend.model = myModel
backend.init_backend(use_disk_cache=False, use_memory_cache=True)
# On first run we get the original object
self.assertEqual(backend.backend_run(), { "color": "white" })
# And on consequent runs we get the object recovered from the cache
self.assertEqual(backend.backend_run(), { "color": "red" })
self.assertEqual(backend.backend_run(), { "color": "red" })

if __name__ == "__main__":
unittest.main()

0 comments on commit 4f15015

Please sign in to comment.