Skip to content

Commit

Permalink
add storage backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rgutzen committed Jul 24, 2019
1 parent 1ea55b0 commit d98353d
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 502 deletions.
3 changes: 3 additions & 0 deletions ACKNOWLEDGEMENTS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This open source software code was developed in part or in whole in the Human Brain Project,
funded from the European Union’s Horizon 2020 Framework Programme for Research and Innovation
under the Specific Grant Agreement No. 785907 (Human Brain Project SGA2).
6 changes: 1 addition & 5 deletions networkunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
:license: Modified BSD, see LICENSE.txt for details.
"""

from . import (tests,
models,
capabilities,
scores,
plots)
from . import tests, models, capabilities, scores, plots

__version__ = "0.1.0"
3 changes: 1 addition & 2 deletions networkunit/capabilities/cap_ProducesSpikeTrains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class ProducesSpikeTrains(sciunit.Capability):
"""
Spike trains are to be represented as a list of neo.SpikeTrain objects.
"""
def produce_spiketrains(self, **kwargs):
self.unimplemented()

15 changes: 15 additions & 0 deletions networkunit/capabilities/cap_ProducesWaveFronts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sciunit

class ProducesWaveFronts(sciunit.Capability):
"""
Wave fronts can either be the transitions from down to up states in the
case of 'Slow Waves', or the peak of a wave cylce (phase = pi/2) in the
case of oscillatory waves.
The wavefronts are to be represented as a list of neo.SpikeTrain objects.
Each SpikeTrain has to have the following annotations:
"coordinates" : tuple, (x, y)
"grid_size" : tuple, (dim_x, dim_y)
"electrode_distance": Quantity, d * quantities.mm
"""
def produce_wavefronts(self, **kwargs):
self.unimplemented()
10 changes: 7 additions & 3 deletions networkunit/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
__path__ = pkgutil.extend_path(__path__, __name__)

for importer, modname, ispkg in pkgutil.walk_packages(path=__path__, prefix=__name__+'.'):
module_type, module_name = str.split(str.split(modname, '.')[-1], '_', 1)
if module_type == 'model':
exec("from {} import {}".format(modname, module_name))
base_modname = str.split(modname, '.')[-1]
if '_' in base_modname:
module_type, module_name = str.split(base_modname, '_', 1)
if module_type == 'model':
exec("from {} import {}".format(modname, module_name))
elif base_modname == "backends":
exec("import {}".format(modname))
14 changes: 14 additions & 0 deletions networkunit/models/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sciunit.models.backends as su_backends
import inspect

try:
from .storage import storage
except ImportError:
storage_backend = None
print('Could not load storage backend')

available_backends = {x.replace('Backend', ''): cls for x, cls
in locals().items()
if inspect.isclass(cls) and
issubclass(cls, su_backends.Backend)}
su_backends.register_backends(locals())
25 changes: 25 additions & 0 deletions networkunit/models/backends/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

from sciunit.models.backends import Backend
import os

class storage(Backend):
name = 'storage'

def init_backend(self, *args, **kwargs):
"""Initialize the backend."""
self.model.attrs = {}

self.use_memory_cache = kwargs.get('use_memory_cache', True)
if self.use_memory_cache:
self.init_memory_cache()
self.use_disk_cache = kwargs.get('use_disk_cache', False)
if self.use_disk_cache:
self.init_disk_cache()
self.load_model()
self.model.unpicklable += ['_backend']

def _backend_run(self):
if not os.path.exists(self.model.file_path):
raise NotImplementedError('The model class must specify a \
file_path from which the simulation results can be loaded.')
return self.model.load()
53 changes: 0 additions & 53 deletions networkunit/models/model_experimental_data.py

This file was deleted.

74 changes: 74 additions & 0 deletions networkunit/models/model_loaded_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import sciunit
from .backends import available_backends

class loaded_data(sciunit.Model):
"""
Abstract model class which initializes self.params.
Child classes need to define a load() function and a file_path.
"""
@property
def file_path(self):
raise NotImplementedError

def __init__(self, name=None, backend='storage', **params):
"""
Parameters
----------
name : string
Name of model instance
**params :
class attributes to be stored in self.params
"""
if params is None:
params = {}
if hasattr(self, 'params'):
self.params.update(params)
else:
self.params = params
super(loaded_data, self).__init__(name=name, **self.params)
if backend is not None:
self.set_backend(backend)

def load(self, file_path=None, **kwargs):
"""
To be called in the produce_xy() function,
associated with the capability class ProducesXY.
"""
raise NotImplementedError

def get_backend(self):
"""Return the simulation backend."""
return self._backend

def set_backend(self, backend):
"""Set the simulation backend."""
if isinstance(backend, str):
name = backend
args = []
kwargs = {}
elif isinstance(backend, (tuple, list)):
name = ''
args = []
kwargs = {}
for i in range(len(backend)):
if i == 0:
name = backend[i]
else:
if isinstance(backend[i], dict):
kwargs.update(backend[i])
else:
args += backend[i]
else:
raise TypeError("Backend must be string, tuple, or list")
if name in available_backends:
self.backend = name
self._backend = available_backends[name]()
elif name is None:
# The base class should not be called.
raise Exception(("A backend (e.g. 'jNeuroML' or 'NEURON') "
"must be selected"))
else:
raise Exception("Backend %s not found in backends.py"
% name)
self._backend.model = self
self._backend.init_backend(*args, **kwargs)
119 changes: 0 additions & 119 deletions networkunit/models/model_resting_state_data.py

This file was deleted.

33 changes: 0 additions & 33 deletions networkunit/models/model_simulation_data.py

This file was deleted.

Loading

0 comments on commit d98353d

Please sign in to comment.