Exemple #1
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.   
        """
        for key, attr in self.trait.iteritems():
            # Skip attribute
            if ignore_list is not None and key in ignore_list:
                break

            if attr.trait.required:
                # In case of fields with data stored on disk check shape
                if isinstance(attr, mapped.Array):
                    if attr.trait.file_storage != FILE_STORAGE_NONE:
                        # Check if any data stored in corresponding dataset
                        try:
                            self.get_data_shape(key)
                        except MissingDataSetException:
                            raise ValidationException(
                                "Could not store '%s' because required array '%s' is missing."
                                % (self.__class__.__name__, key))
                        except IOError:
                            raise ValidationException(
                                "Could not store '%s' because there is no HDF5 file associated."
                                % (self.__class__.__name__))

                elif not hasattr(self, key) or getattr(self, key) is None:
                    raise ValidationException(
                        "Could not store '%s' because required attribute '%s' is missing."
                        % (self.__class__.__name__, key))
Exemple #2
0
    def _validate_before_store(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 after Type.configure was called.
        In case data is not valid an Exception should be thrown.
        """
        for key, attr in six.iteritems(self.trait):
            if attr.trait.required:
                # In case of fields with data stored on disk check shape
                if isinstance(attr, mapped.Array):
                    if attr.trait.file_storage != FILE_STORAGE_NONE:
                        # Check if any data stored in corresponding dataset
                        try:
                            self.get_data_shape(key)
                        except MissingDataSetException:
                            raise ValidationException(
                                "Could not store '%s' because required array '%s' is missing."
                                % (self.__class__.__name__, key))
                        except IOError:
                            raise ValidationException(
                                "Could not store '%s' because there is no HDF5 file associated."
                                % self.__class__.__name__)

                elif not hasattr(self, key) or getattr(self, key) is None:
                    raise ValidationException(
                        "Could not store '%s' because required attribute '%s' is missing."
                        % (self.__class__.__name__, key))
Exemple #3
0
    def reduce_dimension(self, ui_selected_items):
        """
        ui_selected_items is a dictionary which contains items of form:
        '$name_$D : [$gid_$D_$I,..., func_$FUNC]' where '$D - int dimension', '$gid - a data type gid',
        '$I - index in that dimension' and '$FUNC - an aggregation function'.

        If the user didn't select anything from a certain dimension then it means that the entire
        dimension is selected
        """

        # The fields 'aggregation_functions' and 'dimensions' will be of form:
        # - aggregation_functions = {dimension: agg_func, ...} e.g.: {0: sum, 1: average, 2: none, ...}
        # - dimensions = {dimension: [list_of_indexes], ...} e.g.: {0: [0,1], 1: [5,500],...}
        dimensions, aggregation_functions, required_dimension, shape_restrictions = \
            self.parse_selected_items(ui_selected_items)

        if required_dimension is not None:
            # find the dimension of the resulted array
            dim = len(self.shape)
            for key in aggregation_functions.keys():
                if aggregation_functions[key] != "none":
                    dim -= 1
            for key in dimensions.keys():
                if (len(dimensions[key]) == 1
                        and (key not in aggregation_functions
                             or aggregation_functions[key] == "none")):
                    dim -= 1
            if dim != required_dimension:
                self.logger.debug("Dimension for selected array is incorrect")
                raise ValidationException(
                    "Dimension for selected array is incorrect!")

        result = self.array_data
        full = slice(0, None)
        cut_dimensions = 0
        for i in range(len(self.shape)):
            if i in dimensions.keys():
                my_slice = [full for _ in range(i - cut_dimensions)]
                if len(dimensions[i]) == 1:
                    my_slice.extend(dimensions[i])
                    cut_dimensions += 1
                else:
                    my_slice.append(dimensions[i])
                result = result[tuple(my_slice)]
            if i in aggregation_functions.keys():
                if aggregation_functions[i] != "none":
                    result = eval("numpy." + aggregation_functions[i] +
                                  "(result,axis=" + str(i - cut_dimensions) +
                                  ")")
                    cut_dimensions += 1

        # check that the shape for the resulted array respects given conditions
        result_shape = result.shape
        for i in range(len(result_shape)):
            if i in shape_restrictions:
                flag = eval(
                    str(result_shape[i]) +
                    shape_restrictions[i][self.KEY_OPERATION] +
                    str(shape_restrictions[i][self.KEY_SIZE]))
                if not flag:
                    msg = ("The condition is not fulfilled: dimension " +
                           str(i + 1) + " " +
                           shape_restrictions[i][self.KEY_OPERATION] + " " +
                           str(shape_restrictions[i][self.KEY_SIZE]) +
                           ". The actual size of dimension " + str(i + 1) +
                           " is " + str(result_shape[i]) + ".")
                    self.logger.debug(msg)
                    raise ValidationException(msg)

        if required_dimension is not None and 1 <= required_dimension != len(
                result.shape):
            self.logger.debug("Dimensions of the selected array are incorrect")
            raise ValidationException(
                "Dimensions of the selected array are incorrect!")

        return result