Skip to content

Commit

Permalink
Merge pull request #131 from the-virtual-brain/TVB-2680
Browse files Browse the repository at this point in the history
TVB-2680 Fix Histogram Viewer
  • Loading branch information
popaula937 authored Apr 30, 2020
2 parents 848560f + b754828 commit b4ec860
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 103 deletions.
7 changes: 1 addition & 6 deletions framework_tvb/tvb/adapters/datatypes/db/fcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@
#
#
import json
from sqlalchemy import Column, Integer, ForeignKey, String, Float
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship
from tvb.datatypes.fcd import Fcd
from tvb.adapters.datatypes.db.time_series import TimeSeriesIndex
from tvb.core.entities.model.model_datatype import DataTypeMatrix
from tvb.core.neotraits.db import from_ndarray


class FcdIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)
fk_source_gid = Column(String(32), ForeignKey(TimeSeriesIndex.gid), nullable=not Fcd.source.required)
source = relationship(TimeSeriesIndex, foreign_keys=fk_source_gid, primaryjoin=TimeSeriesIndex.gid == fk_source_gid)

Expand All @@ -50,6 +46,5 @@ class FcdIndex(DataTypeMatrix):
def fill_from_has_traits(self, datatype):
# type: (Fcd) -> None
super(FcdIndex, self).fill_from_has_traits(datatype)
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.labels_ordering = json.dumps(datatype.labels_ordering)
self.fk_source_gid = datatype.source.gid.hex
32 changes: 1 addition & 31 deletions framework_tvb/tvb/adapters/datatypes/db/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,81 +28,51 @@
#
#

import json
from sqlalchemy import Column, Integer, ForeignKey, String, Float
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship
from tvb.datatypes.graph import Covariance, CorrelationCoefficients, ConnectivityMeasure
from tvb.adapters.datatypes.db.connectivity import ConnectivityIndex
from tvb.adapters.datatypes.db.time_series import TimeSeriesIndex
from tvb.core.entities.model.model_datatype import DataTypeMatrix
from tvb.core.neotraits.db import from_ndarray


class CovarianceIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)

fk_source_gid = Column(String(32), ForeignKey(TimeSeriesIndex.gid), nullable=not Covariance.source.required)
source = relationship(TimeSeriesIndex, foreign_keys=fk_source_gid, primaryjoin=TimeSeriesIndex.gid == fk_source_gid)

subtype = Column(String)

def fill_from_has_traits(self, datatype):
# type: (Covariance) -> None
super(CovarianceIndex, self).fill_from_has_traits(datatype)
self.subtype = datatype.__class__.__name__
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.fk_source_gid = datatype.source.gid.hex


class CorrelationCoefficientsIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)

fk_source_gid = Column(String(32), ForeignKey(TimeSeriesIndex.gid),
nullable=not CorrelationCoefficients.source.required)
source = relationship(TimeSeriesIndex, foreign_keys=fk_source_gid, primaryjoin=TimeSeriesIndex.gid == fk_source_gid)

subtype = Column(String)

labels_ordering = Column(String)

def fill_from_has_traits(self, datatype):
# type: (CorrelationCoefficients) -> None
super(CorrelationCoefficientsIndex, self).fill_from_has_traits(datatype)
self.subtype = datatype.__class__.__name__
self.labels_ordering = datatype.labels_ordering
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.fk_source_gid = datatype.source.gid.hex


class ConnectivityMeasureIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

subtype = Column(String)

fk_connectivity_gid = Column(String(32), ForeignKey(ConnectivityIndex.gid),
nullable=ConnectivityMeasure.connectivity.required)
connectivity = relationship(ConnectivityIndex, foreign_keys=fk_connectivity_gid,
primaryjoin=ConnectivityIndex.gid == fk_connectivity_gid)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)

shape = Column(String, nullable=False)

def fill_from_has_traits(self, datatype):
# type: (ConnectivityMeasure) -> None
super(ConnectivityMeasureIndex, self).fill_from_has_traits(datatype)
self.subtype = datatype.__class__.__name__
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.fk_connectivity_gid = datatype.connectivity.gid.hex
self.shape = json.dumps(datatype.array_data.shape)
self.ndim = len(datatype.array_data.shape)
5 changes: 0 additions & 5 deletions framework_tvb/tvb/adapters/datatypes/db/region_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def fill_from_has_traits(self, datatype):
class RegionVolumeMappingIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)

fk_connectivity_gid = Column(String(32), ForeignKey(ConnectivityIndex.gid),
nullable=not RegionVolumeMapping.connectivity.required)
connectivity = relationship(ConnectivityIndex, foreign_keys=fk_connectivity_gid,
Expand All @@ -80,6 +76,5 @@ class RegionVolumeMappingIndex(DataTypeMatrix):
def fill_from_has_traits(self, datatype):
# type: (RegionVolumeMapping) -> None
super(RegionVolumeMappingIndex, self).fill_from_has_traits(datatype)
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.fk_connectivity_gid = datatype.connectivity.gid.hex
self.fk_volume_gid = datatype.volume.gid.hex
10 changes: 2 additions & 8 deletions framework_tvb/tvb/adapters/datatypes/db/structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@
# Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010)
#
#
from sqlalchemy import Column, Integer, ForeignKey, String, Float
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship
from tvb.datatypes.structural import StructuralMRI
from tvb.adapters.datatypes.db.volume import VolumeIndex
from tvb.core.entities.model.model_datatype import DataTypeMatrix
from tvb.core.neotraits.db import from_ndarray


class StructuralMRIIndex(DataTypeMatrix):
id = Column(Integer, ForeignKey(DataTypeMatrix.id), primary_key=True)

array_data_min = Column(Float)
array_data_max = Column(Float)
array_data_mean = Column(Float)

weighting = Column(String, nullable=False)

fk_volume_gid = Column(String(32), ForeignKey(VolumeIndex.gid), nullable=not StructuralMRI.volume.required)
Expand All @@ -51,5 +46,4 @@ def fill_from_has_traits(self, datatype):
# type: (StructuralMRI) -> None
super(StructuralMRIIndex, self).fill_from_has_traits(datatype)
self.weighting = datatype.weighting
self.array_data_min, self.array_data_max, self.array_data_mean = from_ndarray(datatype.array_data)
self.fk_volume_gid = datatype.volume.gid.hex
self.fk_volume_gid = datatype.volume.gid.hex
2 changes: 1 addition & 1 deletion framework_tvb/tvb/adapters/datatypes/h5/connectivity_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_centres(self):
def get_region_labels(self):
return self.region_labels.load()

def store(self, datatype, scalars_only=False, store_references=False):
def store(self, datatype, scalars_only=False, store_references=True):
# type: (Connectivity, bool, bool) -> None
super(ConnectivityH5, self).store(datatype, scalars_only, store_references)
self.region_labels.store(datatype.region_labels.astype(STORE_STRING))
Expand Down
10 changes: 10 additions & 0 deletions framework_tvb/tvb/adapters/datatypes/h5/graph_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ def __init__(self, path):

def get_array_data(self):
return self.array_data[:]

def store(self, datatype, scalars_only=False, store_references=True):
# type: (ConnectivityMeasure, bool, bool) -> None
super(ConnectivityMeasureH5, self).store(datatype, scalars_only, store_references)
self.title.store(datatype.title)

def load_into(self, datatype):
# type: (ConnectivityMeasure) -> None
super(ConnectivityMeasureH5, self).load_into(datatype)
datatype.title = self.title.load()
8 changes: 4 additions & 4 deletions framework_tvb/tvb/adapters/datatypes/h5/patterns_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def __init__(self, path):
self.connectivity = Reference(StimuliRegion.connectivity, self)
self.weight = DataSet(StimuliRegion.weight, self)

def store(self, datatype, scalars_only=False):
super(StimuliRegionH5, self).store(datatype, scalars_only)
def store(self, datatype, scalars_only=False, store_references=True):
super(StimuliRegionH5, self).store(datatype, scalars_only, store_references)
self.connectivity.store(datatype.connectivity)


Expand All @@ -56,7 +56,7 @@ def __init__(self, path):
self.focal_points_surface = DataSet(NArray(dtype=int), self, name='focal_points_surface')
self.focal_points_triangles = DataSet(StimuliSurface.focal_points_triangles, self)

def store(self, datatype, scalars_only=False):
super(StimuliSurfaceH5, self).store(datatype, scalars_only)
def store(self, datatype, scalars_only=False, store_references=True):
super(StimuliSurfaceH5, self).store(datatype, scalars_only, store_references)
self.focal_points_surface.store(datatype.focal_points_surface)

2 changes: 1 addition & 1 deletion framework_tvb/tvb/adapters/datatypes/h5/sensors_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_locations(self):
def get_labels(self):
return self.labels.load()

def store(self, datatype, scalars_only=False, store_references=False):
def store(self, datatype, scalars_only=False, store_references=True):
# type: (Sensors, bool, bool) -> None
super(SensorsH5, self).store(datatype, scalars_only, store_references)
self.labels.store(datatype.labels.astype(STORE_STRING))
Expand Down
15 changes: 8 additions & 7 deletions framework_tvb/tvb/adapters/visualizers/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ class HistogramViewer(ABCDisplayer):
"""
The viewer takes as input a result DataType as computed by BCT analyzers.
"""
_ui_name = "Connectivity Measure Visualizer"
_ui_name = "Histogram Visualizer"

def get_form_class(self):
return HistogramViewerForm

# TODO: migrate to neotraits
def launch(self, view_model):
# type: (HistogramViewerModel) -> dict
"""
Expand All @@ -103,7 +102,7 @@ def get_required_memory_size(self, view_model):
"""
Return the required memory to run this algorithm.
"""
input_data = self.load_entity_by_gid(view_model.input_data.hex)
input_data = self.load_entity_by_gid(view_model.input_data)
return numpy.prod(input_data.shape) * 2

def generate_preview(self, view_model, figure_size=None):
Expand All @@ -113,16 +112,18 @@ def generate_preview(self, view_model, figure_size=None):
params = self.prepare_parameters(view_model.input_data)
return self.build_display_result("histogram/view", params)

def prepare_parameters(self, input_data):
def prepare_parameters(self, connectivity_measure_gid):
"""
Prepare all required parameters for a launch.
"""
labels_list = input_data.connectivity.region_labels.tolist()
values_list = input_data.array_data.tolist()
conn_measure = self.load_with_references(connectivity_measure_gid)
assert isinstance(conn_measure, ConnectivityMeasure)
labels_list = conn_measure.connectivity.region_labels.tolist()
values_list = conn_measure.array_data.tolist()
# A gradient of colors will be used for each node
colors_list = values_list

params = dict(title="Connectivity Measure - " + input_data.title, labels=json.dumps(labels_list),
params = dict(title="Connectivity Measure - " + conn_measure.title, labels=json.dumps(labels_list),
data=json.dumps(values_list), colors=json.dumps(colors_list),
xposition='center' if min(values_list) < 0 else 'bottom',
minColor=min(colors_list), maxColor=max(colors_list))
Expand Down
6 changes: 2 additions & 4 deletions framework_tvb/tvb/adapters/visualizers/topographic.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,9 @@ def __init__(self, prefix='', project_id=None):
self.data_0 = TraitDataTypeSelectField(TopographicViewerModel.data_0, self, name='data_0',
conditions=self.get_filters())
self.data_1 = TraitDataTypeSelectField(TopographicViewerModel.data_1, self, name='data_1',
conditions=FilterChain(fields=[FilterChain.datatype + '._nr_dimensions'],
operations=["=="], values=[1]))
conditions=self.get_filters())
self.data_2 = TraitDataTypeSelectField(TopographicViewerModel.data_2, self, name='data_2',
conditions=FilterChain(fields=[FilterChain.datatype + '._nr_dimensions'],
operations=["=="], values=[1]))
conditions=self.get_filters())

@staticmethod
def get_view_model():
Expand Down
9 changes: 5 additions & 4 deletions framework_tvb/tvb/core/adapters/abcadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import psutil
import numpy
import importlib
import typing
from functools import wraps
from datetime import datetime
from abc import ABCMeta, abstractmethod
Expand Down Expand Up @@ -463,17 +464,17 @@ def load_entity_by_gid(data_gid):

@staticmethod
def load_traited_by_gid(data_gid):
# type: (uuid.UUID) -> HasTraits
# type: (typing.Union[uuid.UUID, str]) -> HasTraits
"""
Load a generic HasTraits instance, specified by GID.
"""
index = load_entity_by_gid(data_gid.hex)
index = ABCAdapter.load_entity_by_gid(data_gid)
return h5.load_from_index(index)

@staticmethod
def load_with_references(dt_gid):
# type: (uuid.UUID) -> HasTraits
dt_index = load_entity_by_gid(dt_gid)
# type: (typing.Union[uuid.UUID, str]) -> HasTraits
dt_index = ABCAdapter.load_entity_by_gid(dt_gid)
h5_path = h5.path_for_stored_index(dt_index)
dt, _ = h5.load_with_references(h5_path)
return dt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, path):
self.range1 = Scalar(Attr(str, required=False), self, name='range1')
self.range2 = Scalar(Attr(str, required=False), self, name='range2')

def store(self, burst_config, scalars_only=False, store_references=False):
def store(self, burst_config, scalars_only=False, store_references=True):
self.name.store(burst_config.name)
self.status.store(burst_config.status)
self.error_message.store(burst_config.error_message or 'None')
Expand Down
23 changes: 0 additions & 23 deletions framework_tvb/tvb/core/entities/file/simulator/monitor_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ def __init__(self, path):
self.region_mapping = Reference(Projection.region_mapping, self)
self.obnoise = Reference(Projection.obsnoise, self)

def store(self, datatype, scalars_only=False, store_references=False):
# type: (Projection, bool, bool) -> None
super(ProjectionH5, self).store(datatype, scalars_only, store_references)
self.region_mapping.store(datatype.region_mapping.gid)


class EEGH5(ProjectionH5):

Expand All @@ -91,12 +86,6 @@ def __init__(self, path):
self.reference = Scalar(EEG.reference, self)
self.sigma = Scalar(EEG.sigma, self)

def store(self, datatype, scalars_only=False, store_references=False):
# type: (Projection, bool, bool) -> None
super(EEGH5, self).store(datatype, scalars_only, store_references)
self.projection.store(datatype.projection.gid)
self.sensors.store(datatype.sensors.gid)


class MEGH5(ProjectionH5):

Expand All @@ -105,12 +94,6 @@ def __init__(self, path):
self.projection = Reference(MEG.projection, self)
self.sensors = Reference(MEG.sensors, self)

def store(self, datatype, scalars_only=False, store_references=False):
# type: (Projection, bool, bool) -> None
super(MEGH5, self).store(datatype, scalars_only, store_references)
self.projection.store(datatype.projection.gid)
self.sensors.store(datatype.sensors.gid)


class iEEGH5(ProjectionH5):

Expand All @@ -120,12 +103,6 @@ def __init__(self, path):
self.sensors = Reference(iEEG.sensors, self)
self.sigma = Scalar(iEEG.sigma, self)

def store(self, datatype, scalars_only=False, store_references=False):
# type: (Projection, bool, bool) -> None
super(iEEGH5, self).store(datatype, scalars_only, store_references)
self.projection.store(datatype.projection.gid)
self.sensors.store(datatype.sensors.gid)


class BoldH5(MonitorH5):

Expand Down
Loading

0 comments on commit b4ec860

Please sign in to comment.