# -*- 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 to download
# TheVirtualBrain-Scientific Package (for simulators). See content of the
# documentation-folder for more details. See also http://www.thevirtualbrain.org
#
# (c) 2012-2023, 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 explained here:
# https://www.thevirtualbrain.org/tvb/zwei/neuroscience-publications
#
#
"""
.. Ionel Ortelecan <ionel.ortelecan@codemart.ro>
"""
import uuid
from tvb.adapters.datatypes.db.connectivity import ConnectivityIndex
from tvb.adapters.datatypes.db.patterns import StimuliRegionIndex, StimuliSurfaceIndex
from tvb.adapters.datatypes.db.surface import SurfaceIndex
from tvb.adapters.forms.equation_forms import get_form_for_equation, SpatialEquationsEnum, TemporalEquationsEnum
from tvb.basic.neotraits.api import Attr, EnumAttr
from tvb.core.adapters.abcadapter import ABCAdapterForm, AdapterLaunchModeEnum, ABCAdapter
from tvb.core.entities.filters.chain import FilterChain
from tvb.core.neocom import h5
from tvb.core.neotraits.forms import FormField, TraitDataTypeSelectField, SelectField, StrField
from tvb.core.neotraits.spatial_model import SpatialModel
from tvb.core.neotraits.view_model import ViewModel, DataTypeGidAttr, Str
from tvb.datatypes.connectivity import Connectivity
from tvb.datatypes.patterns import StimuliSurface, StimuliRegion
from tvb.datatypes.surfaces import CorticalSurface, SurfaceTypesEnum
[docs]class SurfaceStimulusCreatorModel(ViewModel, StimuliSurface, SpatialModel):
spatial = EnumAttr(field_type=SpatialEquationsEnum, label="Spatial Equation",
default=SpatialEquationsEnum.SIGMOID.instance)
temporal = EnumAttr(field_type=TemporalEquationsEnum, label="Temporal Equation",
default=TemporalEquationsEnum.PULSETRAIN.instance)
surface = DataTypeGidAttr(
linked_datatype=CorticalSurface,
label=StimuliSurface.surface.label
)
display_name = Str(
label='Display name',
required=False
)
KEY_REGION_STIMULUS = "stim-region"
KEY_SURFACE_STIMULUS = "stim-surface"
[docs]class SurfaceStimulusCreator(ABCAdapter):
"""
The purpose of this adapter is to create a StimuliSurface.
"""
KEY_SURFACE = 'surface'
KEY_SPATIAL = 'spatial'
KEY_TEMPORAL = 'temporal'
KEY_FOCAL_POINTS_TRIANGLES = 'focal_points_triangles'
launch_mode = AdapterLaunchModeEnum.SYNC_SAME_MEM
[docs] def get_output(self):
"""
Describes the outputs of the launch method.
"""
return [StimuliSurfaceIndex]
[docs] def prepare_stimuli_surface_from_view_model(self, view_model, load_full_surface=False):
# type: (SurfaceStimulusCreatorModel, bool) -> StimuliSurface
stimuli_surface = StimuliSurface()
stimuli_surface.focal_points_triangles = view_model.focal_points_triangles
stimuli_surface.spatial = view_model.spatial
stimuli_surface.temporal = view_model.temporal
if load_full_surface:
stimuli_surface.surface = self.load_traited_by_gid(view_model.surface)
else:
stimuli_surface.surface = CorticalSurface()
stimuli_surface.gid = view_model.surface
# We need to load surface triangles on stimuli because focal_points_surface property needs to acces them
with h5.h5_file_for_gid(view_model.surface) as surface_h5:
stimuli_surface.surface.triangles = surface_h5.triangles.load()
return stimuli_surface
[docs] def launch(self, view_model):
# type: (SurfaceStimulusCreatorModel) -> [StimuliSurfaceIndex]
"""
Used for creating a `StimuliSurface` instance
"""
self.generic_attributes.user_tag_1 = view_model.display_name
stimuli_surface = self.prepare_stimuli_surface_from_view_model(view_model, view_model.surface)
stimuli_surface_index = self.store_complete(stimuli_surface)
return stimuli_surface_index
[docs] def get_required_memory_size(self, view_model):
# type: (SurfaceStimulusCreatorModel) -> int
"""
Return the required memory to run this algorithm.
"""
return -1
[docs] def get_required_disk_size(self, view_model):
# type: (SurfaceStimulusCreatorModel) -> int
"""
Returns the required disk size to be able to run the adapter. (in kB)
"""
return 0
[docs]class RegionStimulusCreatorModel(ViewModel, StimuliRegion, SpatialModel):
temporal = EnumAttr(field_type=TemporalEquationsEnum, label="Temporal Equation",
default=TemporalEquationsEnum.PULSETRAIN.instance)
connectivity = DataTypeGidAttr(
field_type=uuid.UUID,
linked_datatype=Connectivity,
label="Connectivity"
)
display_name = Str(
label='Display name',
required=False
)
[docs]class RegionStimulusCreator(ABCAdapter):
"""
The purpose of this adapter is to create a StimuliRegion.
"""
launch_mode = AdapterLaunchModeEnum.SYNC_SAME_MEM
[docs] def get_output(self):
"""
Describes the outputs of the launch method.
"""
return [StimuliRegionIndex]
[docs] def launch(self, view_model):
# type: (RegionStimulusCreatorModel) -> [StimuliRegionIndex]
"""
Used for creating a `StimuliRegion` instance
"""
stimuli_region = StimuliRegion()
stimuli_region.connectivity = Connectivity()
stimuli_region.connectivity.gid = view_model.connectivity
stimuli_region.weight = view_model.weight
stimuli_region.temporal = view_model.temporal
self.generic_attributes.user_tag_1 = view_model.display_name
stimuli_region_idx = self.store_complete(stimuli_region)
return stimuli_region_idx
[docs] def get_required_disk_size(self, view_model):
# type: (RegionStimulusCreatorModel) -> int
"""
Returns the required disk size to be able to run the adapter. (in kB)
"""
return 0
[docs] def get_required_memory_size(self, view_model):
# type: (RegionStimulusCreatorModel) -> int
"""
Return the required memory to run this algorithm.
"""
return -1