コード例 #1
0
    def get_volume_view(self, from_idx, to_idx, x_plane, y_plane, z_plane):
        """
        :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
        :param y_plane: int
        :param z_plane: int

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

        from_idx, to_idx = int(from_idx), int(to_idx)
        x_plane, y_plane, z_plane = int(x_plane), int(y_plane), int(z_plane)
        overall_shape = self.read_data_shape()

        if from_idx > to_idx or to_idx > overall_shape[0] or from_idx < 0:
            msg = "Time indexes out of boundaries: from {0} to {1}".format(
                from_idx, to_idx)
            raise exceptions.ValidationException(msg)

        if x_plane > overall_shape[1] or y_plane > overall_shape[
                2] or z_plane > overall_shape[3]:
            msg = "Coordinates out of boundaries: {0}, {1}, {2}".format(
                x_plane, y_plane, z_plane)
            raise exceptions.ValidationException(msg)

        slices = (slice(from_idx, to_idx), slice(overall_shape[1]),
                  slice(overall_shape[2]), slice(overall_shape[3]))
        slices = self.read_data_slice(tuple(slices))
        slices = slices[..., ::-1]
        slicex = slices[..., z_plane].tolist()
        slicey = slices[:, x_plane, ...].tolist()
        slicez = slices[..., y_plane, :].tolist()
        return [slicex, slicey, slicez]
コード例 #2
0
    def write_data_slice(self, data):
        """
        We are using here the same signature as in TS, just to allow easier parsing code.
        This method will also validate the data range nd convert it to int, along with writing it is H5.

        :param data: 3D int array
        """

        LOG.info("Writing RegionVolumeMapping with min=%d, mix=%d" %
                 (data.min(), data.max()))
        if self.apply_corrections:
            data = numpy.array(data, dtype=numpy.int32)
            data = data - 1
            data[data >= self.connectivity.number_of_regions] = -1
            LOG.debug("After corrections: RegionVolumeMapping min=%d, mix=%d" %
                      (data.min(), data.max()))

        if self.mappings_file:
            try:
                mapping_data = numpy.loadtxt(self.mappings_file,
                                             dtype=numpy.str,
                                             usecols=(0, 2))
                mapping_data = {
                    int(row[0]): int(row[1])
                    for row in mapping_data
                }
            except Exception:
                raise exceptions.ValidationException(
                    "Invalid Mapping File. Expected 3 columns (int, string, int)"
                )

            if len(data.shape) != 3:
                raise exceptions.ValidationException(
                    'Invalid RVM data. Expected 3D.')

            not_matched = set()
            for i in xrange(data.shape[0]):
                for j in xrange(data.shape[1]):
                    for k in xrange(data.shape[2]):
                        val = data[i][j][k]
                        if not mapping_data.has_key(val):
                            not_matched.add(val)
                        data[i][j][k] = mapping_data.get(val, -1)

            LOG.info("Imported RM with values in interval [%d - %d]" %
                     (data.min(), data.max()))
            if not_matched:
                LOG.warn(
                    "Not matched regions will be considered background: %s" %
                    not_matched)

        if data.min() < -1 or data.max(
        ) >= self.connectivity.number_of_regions:
            raise exceptions.ValidationException(
                "Invalid Mapping array: [%d ... %d]" %
                (data.min(), data.max()))

        self.store_data("array_data", data)
コード例 #3
0
def preprocess_space_parameters(x, y, z, max_x, max_y, max_z):
    """
    Covert ajax call parameters into numbers and validate them.

    :param x:  coordinate
    :param y:  coordinate
    :param z:  coordinate that will be reversed
    :param max_x: maximum x accepted value
    :param max_y: maximum y accepted value
    :param max_z: maximum z accepted value

    :return: (x, y, z) as integers, Z reversed
    """

    x, y, z = int(x), int(y), int(z)

    if not 0 <= x <= max_x or not 0 <= y <= max_y or not 0 <= z <= max_z:
        msg = "Coordinates out of boundaries: [x,y,z] = [{0}, {1}, {2}]".format(
            x, y, z)
        raise exceptions.ValidationException(msg)

    # Reverse Z
    z = max_z - z - 1

    return x, y, z
コード例 #4
0
    def get_voxel_time_series(self, x, y, z):
        """
        :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 from the x,y,z coordinates, in time.
        """
        x, y, z = int(x), int(y), int(z)
        overall_shape = self.read_data_shape()

        if x > overall_shape[1] or y > overall_shape[2] or z > overall_shape[3]:
            msg = "Coordinates out of boundaries: [x,y,z] = [{0}, {1}, {2}]".format(
                x, y, z)
            raise exceptions.ValidationException(msg)

        slices = slice(overall_shape[0]), slice(x, x + 1), slice(
            y, y + 1), slice(overall_shape[3])
        slices = self.read_data_slice(slices)
        slices = slices[..., ::-1]
        slices = slices[..., z].flatten()

        result = dict(data=slices.tolist(),
                      max=float(max(slices)),
                      min=float(min(slices)),
                      mean=float(numpy.mean(slices)),
                      median=float(numpy.median(slices)),
                      variance=float(numpy.var(slices)),
                      deviation=float(numpy.std(slices)))
        return result
コード例 #5
0
    def _validate_before_store(self):
        """
        Overrides MappedType._validate_before_store to use a custom error for missing matrix.
        """
        # Sparse Matrix is required so we should check if there is any data stored for it
        if self.matrix is None:
            msg = ("LocalConnectivity can not be stored because it "
                   "has no SparseMatrix attached.")
            raise exceptions.ValidationException(msg)

        super(LocalConnectivity, self)._validate_before_store()
コード例 #6
0
 def validate(self):
     """
     This method checks if the data stored into this entity is valid, and is ready to be stored in DB.
     Method automatically called just before saving entity in DB.
     In case data is not valid a ValidationException is thrown.
     """
     # Check if the surface has a valid number of vertices (not more than the configured maximum).
     if self.number_of_vertices > cfg.MAX_SURFACE_VERTICES_NUMBER:
         msg = "This surface has too many vertices (max allowed: %d)." % cfg.MAX_SURFACE_VERTICES_NUMBER
         msg += " Please upload a new surface or change max number in application settings."
         raise exceptions.ValidationException(msg)
コード例 #7
0
    def validate(self):
        """
        This method checks if the data stored into this entity is valid, and ready to be stored in DB.
        Method automatically called just before saving entity in DB.
        In case data is not valid an Exception should be thrown.
        We implement this method here, because the "check" method is in scientific class.
        """
        super(Surface, self).validate()

        # First check if the surface has a valid number of vertices
        if self.number_of_vertices > cfg.MAX_SURFACE_VERTICES_NUMBER:
            msg = "This surface has too many vertices (max allowed: %d)." % cfg.MAX_SURFACE_VERTICES_NUMBER
            msg += " Please upload a new surface or change max number in application settings."
            raise exceptions.ValidationException(msg)

            # Now check if the surface is closed
        is_closed, _, _, _, _ = self.check()
        if not is_closed:
            msg = "Could not import surface because it's not closed.Please correct the problem and upload again."
            raise exceptions.ValidationException(msg)
コード例 #8
0
    def validate(self):
        self.number_of_vertices = self.vertices.shape[0]
        self.number_of_triangles = self.triangles.shape[0]

        if self.triangles.max() >= self.number_of_vertices:
            raise exceptions.ValidationException(
                "There are triangles that index nonexistent vertices.")

        validation_result = self.validate_topology_for_simulations()

        self.valid_for_simulations = len(validation_result.warnings) == 0

        return validation_result
コード例 #9
0
    def validate(self, ignore_list=None):
        """
        This method checks if the data stored into this entity is valid, and 
        ready to be stored in DB.
        Method automatically called just before saving entity in DB.
        In case data is not valid an Exception should be thrown.
        :param ignore_list: list of strings representing names of the attributes
                            to not be validated.
        """
        super(LocalConnectivityData, self).validate(ignore_list=["matrix"])

        # Sparse Matrix is required so we should check if there is any data stored for it
        if self.matrix is None:
            msg = " ".join(("LocalConnectivity can not be stored because it",
                            "has no SparseMatrix attached."))
            raise exceptions.ValidationException(msg)
コード例 #10
0
    def get_rotated_volume_slice(self, 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)
        :return: Multidimensional matrix (4D). Represents a part from the entire TS, cut over the first
                dimension (time) by the bounds given as inputs, and with the last dimension rotated to get
                a better view of the brain.
        """

        from_idx, to_idx = int(from_idx), int(to_idx)
        overall_shape = self.read_data_shape()

        if from_idx > to_idx or to_idx > overall_shape[0] or from_idx < 0:
            msg = "Bad time indexes: From {0} to {1}".format(from_idx, to_idx)
            raise exceptions.ValidationException(msg)

        slices = (slice(from_idx, to_idx), slice(overall_shape[1]),
                  slice(overall_shape[2]), slice(overall_shape[3]))
        slices = self.read_data_slice(tuple(slices))
        slices = slices[..., ::-1]
        return slices
コード例 #11
0
def preprocess_time_parameters(t1, t2, time_length):
    """
    Covert ajax call parameters into numbers and validate them.

    :param t1: start time
    :param t2: end time
    :param time_length: maximum time length in current TS

    :return: (t1, t2, t2-t1) as numbers
    """

    from_idx = int(t1)
    to_idx = int(t2)

    if not 0 <= from_idx < to_idx <= time_length:
        msg = "Time indexes out of boundaries: from {0} to {1}".format(
            from_idx, to_idx)
        raise exceptions.ValidationException(msg)

    current_time_line = max(to_idx - from_idx, 1)

    return from_idx, to_idx, current_time_line
コード例 #12
0
    def write_data_slice(self, data):
        """
        We are using here the same signature as in TS, just to allow easier parsing code.
        This method will also validate the data range nd convert it to int, along with writing it is H5.

        :param data: 3D int array
        """

        LOG.info("Writing RegionVolumeMapping with min=%d, mix=%d" %
                 (data.min(), data.max()))
        if self.apply_corrections:
            data = numpy.array(data, dtype=numpy.int32)
            data = data - 1
            data[data >= self.connectivity.number_of_regions] = -1
            LOG.debug("After corrections: RegionVolumeMapping min=%d, mix=%d" %
                      (data.min(), data.max()))

        if data.min() < -1 or data.max(
        ) >= self.connectivity.number_of_regions:
            raise exceptions.ValidationException(
                "Invalid Mapping array: [%d ... %d]" %
                (data.min(), data.max()))

        self.store_data("array_data", data)