Skip to content

Commit

Permalink
TVB-2663 Remove tvb.adapter dependency from model_operation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanvalean1 committed Jun 17, 2020
1 parent bce4854 commit 04abf94
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 71 deletions.
2 changes: 1 addition & 1 deletion framework_tvb/tvb/adapters/simulator/form_with_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#
from abc import abstractmethod
from tvb.basic.neotraits.api import NArray
from tvb.adapters.simulator.range_parameter import RangeParameter
from tvb.core.entities.transient.range_parameter import RangeParameter
from tvb.core.neotraits.forms import Form


Expand Down
8 changes: 4 additions & 4 deletions framework_tvb/tvb/adapters/simulator/noise_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
#
#
from tvb.adapters.simulator.equation_forms import get_form_for_equation
from tvb.adapters.simulator.subforms_mapping import SubformsEnum, get_ui_name_to_equation_dict, Linear
from tvb.adapters.simulator.form_with_ranges import FormWithRanges
from tvb.adapters.simulator.subforms_mapping import SubformsEnum, get_ui_name_to_equation_dict
from tvb.basic.neotraits.api import Attr, Range
from tvb.core.entities.transient.range_parameter import RangeParameter
from tvb.core.neotraits.forms import ScalarField, ArrayField, SelectField
from tvb.datatypes.equations import Equation
from tvb.simulator.noise import Noise, Additive, Multiplicative
from tvb.adapters.simulator.form_with_ranges import FormWithRanges
from tvb.adapters.simulator.range_parameter import RangeParameter
from tvb.core.neotraits.forms import ScalarField, ArrayField, SelectField


def get_form_for_noise(noise_class):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,61 +27,16 @@
# Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010)
#
#
import json
import numpy
from collections import OrderedDict
from tvb.basic.neotraits.api import HasTraits, NArray, Range

from tvb.basic.neotraits.api import NArray, Range
from tvb.core.entities.transient.range_parameter import RangeParameter
from tvb.datatypes.connectivity import Connectivity
from tvb.datatypes.surfaces import Surface
from tvb.simulator.integrators import IntegratorStochastic
from tvb.simulator.simulator import Simulator


class RangeParameter(object):
KEY_FLOAT_LO = 'lo'
KEY_FLOAT_STEP = 'step'
KEY_FLOAT_HI = 'hi'

def __init__(self, name, type, range_definition, is_array=False, range_values=None):
self.name = name
self.type = type
self.is_array = is_array
# range_definition is a Range object when type=float and a FilterChain otherwise
self.range_definition = range_definition
self.range_values = range_values

def to_json(self):
if self.type is float:
return json.dumps([self.name, {self.KEY_FLOAT_LO: self.range_definition.lo,
self.KEY_FLOAT_STEP: self.range_definition.step,
self.KEY_FLOAT_HI: self.range_definition.hi}])

return json.dumps([self.name, self.range_definition])

@staticmethod
def from_json(range_json):
range_list = json.loads(range_json)
if isinstance(range_list[1], dict):
return RangeParameter(range_list[0], float,
Range(range_list[1][RangeParameter.KEY_FLOAT_LO],
range_list[1][RangeParameter.KEY_FLOAT_HI],
range_list[1][RangeParameter.KEY_FLOAT_STEP]))

return RangeParameter(range_list[0], HasTraits, range_list[1])

def get_range_values(self):
if self.type is float:
self.range_values = self.range_definition.to_array()
if self.is_array:
self.range_values = [numpy.array([value]) for value in self.range_values]
return self.range_values

def fill_from_default(self, range_param):
# type: (RangeParameter) -> None
self.type = range_param.type
self.is_array = range_param.is_array


class SimulatorRangeParameters(object):

def __init__(self, connectivity_filters=None, surface_filters=None, coupling=None, model=None,
Expand Down
25 changes: 13 additions & 12 deletions framework_tvb/tvb/adapters/simulator/simulator_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@
#

import uuid

import formencode
from formencode import validators
from tvb.adapters.datatypes.db.local_connectivity import LocalConnectivityIndex
from tvb.adapters.datatypes.db.patterns import StimuliRegionIndex, SpatioTemporalPatternIndex
from tvb.adapters.datatypes.db.region_mapping import RegionMappingIndex
from tvb.adapters.datatypes.db.surface import SurfaceIndex
from tvb.adapters.simulator.integrator_forms import get_form_for_integrator
from tvb.adapters.simulator.model_forms import get_ui_name_to_model
from tvb.adapters.simulator.monitor_forms import get_ui_name_to_monitor_dict, get_monitor_to_ui_name_dict
from tvb.adapters.simulator.subforms_mapping import get_ui_name_to_integrator_dict
from tvb.core.entities.filters.chain import FilterChain
from tvb.basic.neotraits.api import Attr, Range, List
from tvb.core.adapters.abcadapter import ABCAdapterForm
from tvb.core.entities.filters.chain import FilterChain
from tvb.core.entities.transient.range_parameter import RangeParameter
from tvb.core.neocom import h5
from tvb.core.neotraits.forms import DataTypeSelectField, ScalarField, ArrayField, SimpleFloatField, \
SimpleHiddenField, SelectField, MultiSelectField
from tvb.core.neotraits.view_model import Str
from tvb.datatypes.cortex import Cortex
from tvb.datatypes.surfaces import CORTICAL
from tvb.simulator.integrators import Integrator
from tvb.simulator.models.base import Model
from tvb.simulator.simulator import Simulator
from tvb.adapters.simulator.model_forms import get_ui_name_to_model
from tvb.adapters.simulator.monitor_forms import get_ui_name_to_monitor_dict, get_monitor_to_ui_name_dict
from tvb.adapters.simulator.range_parameter import RangeParameter
from tvb.core.adapters.abcadapter import ABCAdapterForm
from tvb.adapters.datatypes.db.local_connectivity import LocalConnectivityIndex
from tvb.adapters.datatypes.db.patterns import StimuliSurfaceIndex, StimuliRegionIndex, SpatioTemporalPatternIndex
from tvb.adapters.datatypes.db.region_mapping import RegionMappingIndex
from tvb.adapters.datatypes.db.surface import SurfaceIndex
from tvb.core.neotraits.forms import DataTypeSelectField, ScalarField, ArrayField, SimpleFloatField, \
SimpleHiddenField, SelectField, MultiSelectField
from tvb.core.neocom import h5


class SimulatorSurfaceFragment(ABCAdapterForm):
Expand Down
11 changes: 6 additions & 5 deletions framework_tvb/tvb/core/entities/model/model_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@
.. moduleauthor:: Yann Gordon <yann@tvb.invalid>
"""

import json
import datetime
from sqlalchemy.orm import relationship, backref
import json

from sqlalchemy import Boolean, Integer, String, DateTime, Column, ForeignKey
from sqlalchemy.orm import relationship, backref
from tvb.basic.logger.builder import get_logger
from tvb.adapters.simulator.range_parameter import RangeParameter
from tvb.config import TVB_IMPORTER_CLASS, TVB_IMPORTER_MODULE
from tvb.core.neotraits.db import Base
from tvb.core.utils import string2date, generate_guid
from tvb.core.entities.exportable import Exportable
from tvb.core.entities.model.model_project import Project, User
from tvb.core.entities.transient.range_parameter import RangeParameter
from tvb.core.neotraits.db import Base
from tvb.core.utils import string2bool, date2string, LESS_COMPLEX_TIME_FORMAT
from tvb.core.utils import string2date, generate_guid

LOG = get_logger(__name__)

Expand Down
79 changes: 79 additions & 0 deletions framework_tvb/tvb/core/entities/transient/range_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
#
#
# TheVirtualBrain-Framework Package. This package holds all Data Management, and
# Web-UI helpful to run brain-simulations. To use it, you also need do download
# TheVirtualBrain-Scientific Package (for simulators). See content of the
# documentation-folder for more details. See also http://www.thevirtualbrain.org
#
# (c) 2012-2020, Baycrest Centre for Geriatric Care ("Baycrest") and others
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this
# program. If not, see <http://www.gnu.org/licenses/>.
#
#
# CITATION:
# When using The Virtual Brain for scientific publications, please cite it as follows:
#
# Paula Sanz Leon, Stuart A. Knock, M. Marmaduke Woodman, Lia Domide,
# Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013)
# The Virtual Brain: a simulator of primate brain network dynamics.
# Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010)
#
#

import json

import numpy
from tvb.basic.neotraits.api import HasTraits, Range


class RangeParameter(object):
KEY_FLOAT_LO = 'lo'
KEY_FLOAT_STEP = 'step'
KEY_FLOAT_HI = 'hi'

def __init__(self, name, type, range_definition, is_array=False, range_values=None):
self.name = name
self.type = type
self.is_array = is_array
# range_definition is a Range object when type=float and a FilterChain otherwise
self.range_definition = range_definition
self.range_values = range_values

def to_json(self):
if self.type is float:
return json.dumps([self.name, {self.KEY_FLOAT_LO: self.range_definition.lo,
self.KEY_FLOAT_STEP: self.range_definition.step,
self.KEY_FLOAT_HI: self.range_definition.hi}])

return json.dumps([self.name, self.range_definition])

@staticmethod
def from_json(range_json):
range_list = json.loads(range_json)
if isinstance(range_list[1], dict):
return RangeParameter(range_list[0], float,
Range(range_list[1][RangeParameter.KEY_FLOAT_LO],
range_list[1][RangeParameter.KEY_FLOAT_HI],
range_list[1][RangeParameter.KEY_FLOAT_STEP]))

return RangeParameter(range_list[0], HasTraits, range_list[1])

def get_range_values(self):
if self.type is float:
self.range_values = self.range_definition.to_array()
if self.is_array:
self.range_values = [numpy.array([value]) for value in self.range_values]
return self.range_values

def fill_from_default(self, range_param):
# type: (RangeParameter) -> None
self.type = range_param.type
self.is_array = range_param.is_array
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from tvb.adapters.simulator.equation_forms import get_form_for_equation
from tvb.adapters.simulator.model_forms import get_form_for_model
from tvb.adapters.simulator.noise_forms import get_form_for_noise
from tvb.adapters.simulator.range_parameter import SimulatorRangeParameters
from tvb.adapters.simulator.range_parameters import SimulatorRangeParameters
from tvb.adapters.simulator.simulator_adapter import SimulatorAdapterForm
from tvb.adapters.simulator.simulator_fragments import *
from tvb.adapters.simulator.monitor_forms import get_form_for_monitor
Expand Down

0 comments on commit 04abf94

Please sign in to comment.