예제 #1
0
    def test_shape(self):

        # try to avoid introspector picking up this model
        Gen2D = copy.deepcopy(models.Generic2dOscillator)

        class CouplingShapeTestModel(Gen2D):
            def __init__(self, test_case=None, n_node=None, **kwds):
                super(CouplingShapeTestModel, self).__init__(**kwds)
                self.cvar = numpy.r_[0, 1]
                self.n_node = n_node
                self.test_case = test_case

            def dfun(self, state, coupling, local_coupling):
                if self.test_case is not None:
                    self.test_case.assert_equal((2, self.n_node, 1),
                                                coupling.shape)
                    return state

        surf = cortex.Cortex(region_mapping_data=region_mapping.RegionMapping(
            load_file="regionMapping_16k_76.txt"),
                             load_file="cortex_16384.zip")
        sim = simulator.Simulator(model=CouplingShapeTestModel(
            self, surf.vertices.shape[0]),
                                  connectivity=connectivity.Connectivity(
                                      load_file="connectivity_76.zip"),
                                  surface=surf)

        sim.configure()

        for _ in sim(simulation_length=sim.integrator.dt * 2):
            pass
예제 #2
0
class TimeSeriesRegionData(TimeSeriesData):
    """ A time-series associated with the regions of a connectivity. """
    _ui_name = "Region time-series"
    connectivity = connectivity_module.Connectivity
    region_mapping = region_mapping_module.RegionMapping(required=False)
    region_mapping_volume = region_mapping_module.RegionVolumeMapping(
        required=False)
    labels_ordering = basic.List(
        default=["Time", "State Variable", "Region", "Mode"])
예제 #3
0
    def read_region_mapping(self, path):
        """
        :param path: Path towards a custom RegionMapping H5 file
        :return: region mapping in a numpy array
        """
        if not os.path.isfile(path):
            self.logger.warning("RegionMapping file %s does not exist" % path)
            return None

        self.logger.info("Starting to read RegionMapping from: %s" % path)
        h5_file = h5py.File(path, 'r', libver='latest')

        rm = region_mapping.RegionMapping()
        rm.array_data = h5_file['/data'][()]

        h5_file.close()
        self.logger.info("Successfully read region mapping!")  #: %s" % data)

        return rm
예제 #4
0
    def __init__(self,
                 connectivity=None,
                 region_mapping_volume=None,
                 region_mapping=None,
                 labels_ordering=None,
                 *args,
                 **kwargs):
        self.connectivity = connectivity

        if region_mapping_volume is None:
            region_mapping_volume = region_map.RegionVolumeMapping()
        self.region_mapping_volume = region_mapping_volume

        if region_mapping is None:
            region_mapping = region_map.RegionMapping()
        self.region_mapping = region_mapping

        if labels_ordering is None:
            labels_ordering = ["Time", "State Variable", "Region", "Mode"]

        super(TimeSeriesRegion, self).__init__(*args,
                                               labels_ordering=labels_ordering,
                                               **kwargs)
예제 #5
0
class TimeSeriesRegion(TimeSeries):
    """ A time-series associated with the regions of a connectivity. """
    _ui_name = "Region time-series"
    connectivity = connectivity.Connectivity
    region_mapping_volume = region_mapping.RegionVolumeMapping(required=False)
    region_mapping = region_mapping.RegionMapping(required=False)
    labels_ordering = basic.List(default=["Time", "State Variable", "Region", "Mode"])

    def configure(self):
        """
        After populating few fields, compute the rest of the fields
        """
        super(TimeSeriesRegion, self).configure()
        self.has_surface_mapping = self.region_mapping is not None or self._region_mapping is not None
        self.has_volume_mapping = self.region_mapping_volume is not None or self._region_mapping_volume is not None

    def _find_summary_info(self):
        """
        Gather scientifically interesting summary information from an instance of this datatype.
        """
        summary = super(TimeSeriesRegion, self)._find_summary_info()
        summary.update({"Source Connectivity": self.connectivity.display_name,
                        "Region Mapping": self.region_mapping.display_name if self.region_mapping else "None",
                        "Region Mapping Volume": (self.region_mapping_volume.display_name
                                                  if self.region_mapping_volume else "None")})
        return summary

    def get_space_labels(self):
        """
        :return: An array of strings with the connectivity node labels.
        """
        if self.connectivity is not None:
            return list(self.connectivity.region_labels)
        return []

    def get_grouped_space_labels(self):
        """
        :return: A structure of this form [('left', [(idx, lh_label)...]), ('right': [(idx, rh_label) ...])]
        """
        if self.connectivity is not None:
            return self.connectivity.get_grouped_space_labels()
        else:
            return super(TimeSeriesRegion, self).get_grouped_space_labels()

    def get_default_selection(self):
        """
        :return: If the connectivity of this time series is edited from another
                 return the nodes of the parent that are present in the connectivity.
        """
        if self.connectivity is not None:
            return self.connectivity.get_default_selection()
        else:
            return super(TimeSeriesRegion, self).get_default_selection()

    def get_measure_points_selection_gid(self):
        """
        :return: the associated connectivity gid
        """
        if self.connectivity is not None:
            return self.connectivity.get_measure_points_selection_gid()
        else:
            return super(TimeSeriesRegion, self).get_measure_points_selection_gid()

    def get_volume_view(self, from_idx, to_idx, x_plane, y_plane, z_plane, var=0, mode=0):
        """
        Retrieve 3 slices through the Volume TS, at the given X, y and Z coordinates, and in time [from_idx .. to_idx].

        :param from_idx: int This will be the limit on the first dimension (time)
        :param to_idx: int Also limit on the first Dimension (time)
        :param x_plane: int coordinate
        :param y_plane: int coordinate
        :param z_plane: int coordinate

        :return: An array of 3 Matrices 2D, each containing the values to display in planes xy, yz and xy.
        """

        if self.region_mapping_volume is None:
            raise exceptions.TVBException("Invalid method called for TS without Volume Mapping!")

        volume_rm = self.region_mapping_volume

        # Work with space inside Volume:
        x_plane, y_plane, z_plane = preprocess_space_parameters(x_plane, y_plane, z_plane, volume_rm.length_1d,
                                                                volume_rm.length_2d, volume_rm.length_3d)
        var, mode = int(var), int(mode)
        slice_x, slice_y, slice_z = self.region_mapping_volume.get_volume_slice(x_plane, y_plane, z_plane)

        # Read from the current TS:
        from_idx, to_idx, current_time_length = preprocess_time_parameters(from_idx, to_idx, self.read_data_shape()[0])
        no_of_regions = self.read_data_shape()[2]
        time_slices = slice(from_idx, to_idx), slice(var, var + 1), slice(no_of_regions), slice(mode, mode + 1)

        min_signal = self.get_min_max_values()[0]
        regions_ts = self.read_data_slice(time_slices)[:, 0, :, 0]
        regions_ts = numpy.hstack((regions_ts, numpy.ones((current_time_length, 1)) * self.out_of_range(min_signal)))

        # Index from TS with the space mapping:
        result_x, result_y, result_z = [], [], []

        for i in range(0, current_time_length):
            result_x.append(regions_ts[i][slice_x].tolist())
            result_y.append(regions_ts[i][slice_y].tolist())
            result_z.append(regions_ts[i][slice_z].tolist())

        return [result_x, result_y, result_z]

    def get_voxel_time_series(self, x, y, z, var=0, mode=0):
        """
        Retrieve for a given voxel (x,y,z) the entire timeline.

        :param x: int coordinate
        :param y: int coordinate
        :param z: int coordinate

        :return: A complex dictionary with information about current voxel.
                The main part will be a vector with all the values over time from the x,y,z coordinates.
        """

        if self.region_mapping_volume is None:
            raise exceptions.TVBException("Invalid method called for TS without Volume Mapping!")

        volume_rm = self.region_mapping_volume
        x, y, z = preprocess_space_parameters(x, y, z, volume_rm.length_1d, volume_rm.length_2d, volume_rm.length_3d)
        idx_slices = slice(x, x + 1), slice(y, y + 1), slice(z, z + 1)

        idx = int(volume_rm.get_data('array_data', idx_slices))

        time_length = self.read_data_shape()[0]
        var, mode = int(var), int(mode)
        voxel_slices = prepare_time_slice(time_length), slice(var, var + 1), slice(idx, idx + 1), slice(mode, mode + 1)
        label = volume_rm.connectivity.region_labels[idx]

        background, back_min, back_max = None, None, None
        if idx < 0:
            back_min, back_max = self.get_min_max_values()
            background = numpy.ones((time_length, 1)) * self.out_of_range(back_min)
            label = 'background'

        result = postprocess_voxel_ts(self, voxel_slices, background, back_min, back_max, label)
        return result

    @staticmethod
    def out_of_range(min_value):
        return round(min_value) - 1