Beispiel #1
0
  def __evaluateLocal__(self, x, y, weights = None, axis = 0, **kwargs):
    """
      This method computes difference between two points x and y based on given metric
      @ In, x, numpy.ndarray, array containing data of x, if 1D array is provided,
        the array will be reshaped via x.reshape(-1,1) for paired_distance, shape (n_samples, ), if 2D
        array is provided, shape (n_samples, n_outputs)
      @ In, y, numpy.ndarray, array containing data of y, if 1D array is provided,
        the array will be reshaped via y.reshape(-1,1), shape (n_samples, ), if 2D
        array is provided, shape (n_samples, n_outputs)
      @ In, weights, array_like (numpy.array or list), optional, weights associated
        with input, shape (n_samples) if axis = 0, otherwise shape (n_outputs)
      @ In, axis, integer, optional, axis along which a metric is performed, default is 0,
        i.e. the metric will performed along the first dimension (the "rows").
        If metric postprocessor is used, the first dimension is the RAVEN_sample_ID,
        and the second dimension is the pivotParameter if HistorySet is provided.
      @ In, kwargs, dict, dictionary of parameters characteristic of each metric
      @ Out, value, numpy.ndarray, metric result, shape (n_outputs) if axis = 0, otherwise
        shape (n_samples), we assume the dimension of input numpy.ndarray is no more than 2.
    """
    #######################################################################################
    # The inputs of regression metric, i.e. x, y should have shape (n_samples, n_outputs),
    # and the outputs will have the shape (n_outputs).
    # However, the inputs of paired metric, i.e. x, y should convert the shape to
    # (n_outputs, n_samples), and the outputs will have the shape (n_outputs).
    #######################################################################################
    assert(isinstance(x,np.ndarray))
    assert(isinstance(y,np.ndarray))
    assert(x.shape == y.shape), "Input data x, y should have the same shape"
    if weights is not None and self.metricType[0] == 'regression' and 'sample_weight' not in self.distParams.keys():
      self.distParams['sample_weight'] = weights
    if self.metricType[0] == 'regression':
      self.distParams['multioutput'] = 'raw_values'
    dictTemp = utils.mergeDictionaries(kwargs,self.distParams)
    if self.metricType[0] == 'paired_distance':
      if len(x.shape) == 1:
        x = x.reshape(-1,1)
        y = y.reshape(-1,1)
      else:
        # Transpose is needed, since paired_distance is operated on the 'row'
        x = x.T
        y = y.T
    if axis == 1:
      x = x.T
      y = y.T
      # check the dimension of weights
      assert(x.shape[0] == len(weights)), "'weights' should have the same length of the first dimension of input data"
    elif axis != 0:
      self.raiseAnError(IOError, "Valid axis value should be '0' or '1' for the evaluate method of metric", self. name, "value", axis, "is provided!")
    try:
      value = self.__class__.availMetrics[self.metricType[0]][self.metricType[1]](x, y, **dictTemp)
    except TypeError as e:
      self.raiseAWarning('There are some unexpected keyword arguments found in Metric with type "', self.metricType[1], '"!')
      self.raiseAnError(TypeError,'Input parameters error:\n', str(e), '\n')

    return value
Beispiel #2
0
  def __evaluateLocal__(self, x, y = None, axis = 0, weights =None, **kwargs):
    """
      This method computes difference between two points x and y based on given metric
      @ In, x, numpy.ndarray, array containing data of x, if 1D array is provided,
        the array will be reshaped via x.reshape(-1,1)
      @ In, y, optional, numpy.ndarray, array containing data of y, if 1D array is provided,
        the array will be reshaped via y.reshape(-1,1)
      @ In, axis, integer, 0 or 1, axis along which a metric is performed, default is 0,
        i.e. the metric will performed along the first dimension (the "rows"). If axis = 1,
        then x = x.T, y = y.T
      @ In, weights, array_like (numpy.ndarray or list), optional, unused in this method
      @ In, kwargs, dict, dictionary of parameters characteristic of each metric
      @ Out, value, 2D numpy.ndarray, metric result, shape (numRowsInX, numRowsInY) if y is not
        None, otherwise shape (numRowsInX, numRowsInX)
    """
    if axis != 0 and axis != 1:
      self.raiseAnError(IOError, "Acceptable values for axis are 0 or 1, but the provided value is", axis)
    assert(isinstance(x,np.ndarray)), "Input data x should be numpy.array"
    if len(x.shape) == 1:
      x = x.reshape(-1,1)
    if axis == 1:
      x = x.T
    if y != None:
      assert(isinstance(y,np.ndarray)), "Input data y should be numpy.array"
      if len(y.shape) == 1:
        y = y.reshape(-1,1)
      if axis == 1:
        y = y.T
      assert(x.shape[1] == y.shape[1]), "The number of columns in x should be the same as the number of columns in y"
    dictTemp = utils.mergeDictionaries(kwargs,self.distParams)
    # set up the metric engine if it is None
    if self.__class__.availMetrics[self.metricType[0]][self.metricType[1]] == None:
      if y == None:
        self.__class__.availMetrics[self.metricType[0]][self.metricType[1]] = spatialDistance.pdist
        try:
          value = self.__class__.availMetrics[self.metricType[0]][self.metricType[1]](x,metric=self.metricType[1], **dictTemp)
        except TypeError as e:
          self.raiseAWarning('There are some unexpected keyword arguments found in Metric with type "', self.metricType[1], '"!')
          self.raiseAnError(TypeError,'Input parameters error:\n', str(e), '\n')
      else:
        self.__class__.availMetrics[self.metricType[0]][self.metricType[1]] = spatialDistance.cdist
        try:
          value = self.__class__.availMetrics[self.metricType[0]][self.metricType[1]](x,y,metric=self.metricType[1], **dictTemp)
        except TypeError as e:
          self.raiseAWarning('There are some unexpected keyword arguments found in Metric with type "', self.metricType[1], '"!')
          self.raiseAnError(TypeError,'Input parameters error:\n', str(e), '\n')
    else:
      try:
        value = self.__class__.availMetrics[self.metricType[0]][self.metricType[1]](x, Y=y, **dictTemp)
      except TypeError as e:
        self.raiseAWarning('There are some unexpected keyword arguments found in Metric with type "', self.metricType[1], '"!')
        self.raiseAnError(TypeError,'Input parameters error:\n', str(e), '\n')

    return value
Beispiel #3
0
 def distance(self, x, y=None, **kwargs):
     """
   This method returns the distance between two points x and y. If y is not provided then x is a pointSet and a distance matrix is returned
   @ In, x, dict, dictionary containing data of x
   @ In, y, dict, dictionary containing data of y
   @ Out, value, float or numpy.ndarray, distance between x and y (if y is provided) or a square distance matrix if y is None
 """
     if y is not None:
         if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
             dictTemp = utils.mergeDictionaries(kwargs, self.distParams)
             if self.metricType in pairwise.kernel_metrics().keys():
                 value = pairwise.kernel_metrics(X=x,
                                                 Y=y,
                                                 metric=self.metricType,
                                                 **dictTemp)
             elif self.metricType in pairwise.distance_metrics():
                 value = pairwise.pairwise_distances(X=x,
                                                     Y=y,
                                                     metric=self.metricType,
                                                     **dictTemp)
             return value
         else:
             self.raiseAnError(
                 IOError,
                 'Metric SKL error: SKL metrics support only PointSets and not HistorySets'
             )
     else:
         if self.metricType == 'mahalanobis':
             covMAtrix = np.cov(x.T)
             kwargs['VI'] = np.linalg.inv(covMAtrix)
         dictTemp = utils.mergeDictionaries(kwargs, self.distParams)
         if self.metricType in pairwise.kernel_metrics().keys():
             value = pairwise.pairwise_kernels(X=x,
                                               metric=self.metricType,
                                               **dictTemp)
         elif self.metricType in pairwise.distance_metrics().keys():
             value = pairwise.pairwise_distances(X=x,
                                                 metric=self.metricType,
                                                 **dictTemp)
         return value
Beispiel #4
0
 def distance(self, x, y=None, **kwargs):
   """
     This method returns the distance between two points x and y. If y is not provided then x is a pointSet and a distance matrix is returned
     @ In, x, numpy.ndarray, array containing data of x, if 1D array is provided, the array will be reshaped via x.reshape(1,-1)
     @ In, y, numpy.ndarray, array containing data of y, if 1D array is provided, the array will be reshaped via y.reshape(1,-1)
     @ Out, value, numpy.ndarray, distance between x and y (if y is provided) or a square distance matrix if y is None
   """
   if y is not None:
     if isinstance(x,np.ndarray) and isinstance(y,np.ndarray):
       if len(x.shape) == 1:
         x = x.reshape(1,-1)
         #self.raiseAWarning(self, "1D array is provided. For consistence, this array is reshaped via x.reshape(1,-1) ")
       if len(y.shape) == 1:
         y = y.reshape(1,-1)
         #self.raiseAWarning(self, "1D array is provided. For consistence, this array is reshaped via y.reshape(1,-1) ")
       dictTemp = utils.mergeDictionaries(kwargs,self.distParams)
       if self.metricType in pairwise.kernel_metrics().keys():
         value = pairwise.pairwise_kernels(X=x, Y=y, metric=self.metricType, **dictTemp)
       elif self.metricType in pairwise.distance_metrics():
         value = pairwise.pairwise_distances(X=x, Y=y, metric=self.metricType, **dictTemp)
       if value.shape == (1,1):
         return value[0]
       else:
         return value
     else:
       self.raiseAnError(IOError,'Metric SKL error: SKL metrics support only PointSets and not HistorySets')
   else:
     if self.metricType == 'mahalanobis':
       covMAtrix = np.cov(x.T)
       kwargs['VI'] = np.linalg.inv(covMAtrix)
     dictTemp = utils.mergeDictionaries(kwargs,self.distParams)
     if self.metricType in pairwise.kernel_metrics().keys():
       value = pairwise.pairwise_kernels(X=x, metric=self.metricType, **dictTemp)
     elif self.metricType in pairwise.distance_metrics().keys():
       value = pairwise.pairwise_distances(X=x, metric=self.metricType, **dictTemp)
     if value.shape == (1,1):
       return value[0]
     else:
       return value
Beispiel #5
0
    def __evaluateLocal__(self, x, y, weights=None, axis=0, **kwargs):
        """
      This method computes difference between two points x and y based on given metric
      @ In, x, 1-D numpy.ndarray, array containing data of x.
      @ In, y, 1-D numpy.ndarray, array containing data of y.
      @ In, weights, array_like (numpy.array or list), optional, weights associated the metric method
      @ In, axis, integer, optional, default is 0, not used in this metric
      @ In, kwargs, dict, dictionary of parameters characteristic of each metric
      @ Out, value, float, metric result
    """
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            assert (x.shape == y.shape,
                    "Input data x, y should have the same shape!")
            # TODO: weights are supported in scipy.spatial.distance for many distance metrics in v1.0.0
            # when we switch to scipy 1.0.0, we can enable weights in our metrics calculations
            sv = str(scipy.__version__).split('.')
            if int(sv[0]) > 0:
                if weights is not None and 'w' not in self.distParams.keys():
                    self.distParams['w'] = weights
                # FIXME: In Scipy version 1.1.0, the function scipy.spatial.distance.canberra and
                # scipy.spatial.distance.sokalmichener will accept the weights, and the calculated results from
                # these functions will affected by the normalization of the weights. The following is disabled for
                # this purpose  --- wangc July 17, 2018
                # For future development, please pay attention to canberra, minkowski, and sokalmichener metrics
                #if 'w' in self.distParams.keys():
                # Normalized weights, since methods exist in Scipy are using unnormalized weights
                #self.distParams['w'] = np.asarray(self.distParams['w'])/np.sum(self.distParams['w'])
            else:
                if 'w' in self.distParams.keys():
                    self.raiseAWarning(
                        "Weights will not be used, since weights provided with key word 'w' is not supported for your current version of scipy!"
                    )
                    self.distParams.pop('w')
            dictTemp = utils.mergeDictionaries(kwargs, self.distParams)
            try:
                value = self.__class__.availMetrics[self.metricType[0]][
                    self.metricType[1]](x, y, **dictTemp)
            except TypeError as e:
                self.raiseAWarning(
                    'There are some unexpected keyword arguments found in Metric with type',
                    self.metricType[1])
                self.raiseAnError(TypeError, 'Input parameters error: \n',
                                  str(e), '\n')
        else:
            self.raiseAnError(IOError, "Input data type is not correct!")

        return value
Beispiel #6
0
 def distance(self, x, y=None, **kwargs):
     """
   This method returns the distance between two points x and y. If y is not provided then x is a pointSet and a distance matrix is returned
   @ In, x, numpy.ndarray, array containing data of x, if 1D array is provided, the array will be reshaped via x.reshape(1,-1)
   @ In, y, numpy.ndarray, array containing data of y, if 1D array is provided, the array will be reshaped via y.reshape(1,-1)
   @ Out, value, numpy.ndarray, distance between x and y (if y is provided) or a square distance matrix if y is None
 """
     if y is not None:
         if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
             if len(x.shape) == 1 and self.metricType not in scores.keys():
                 x = x.reshape(1, -1)
                 #self.raiseAWarning(self, "1D array is provided. For consistence, this array is reshaped via x.reshape(1,-1) ")
             if len(y.shape) == 1 and self.metricType not in scores.keys():
                 y = y.reshape(1, -1)
                 #self.raiseAWarning(self, "1D array is provided. For consistence, this array is reshaped via y.reshape(1,-1) ")
             dictTemp = utils.mergeDictionaries(kwargs, self.distParams)
             try:
                 if self.metricType in pairwise.kernel_metrics().keys():
                     value = pairwise.pairwise_kernels(
                         X=x, Y=y, metric=self.metricType, **dictTemp)
                 elif self.metricType in pairwise.distance_metrics():
                     value = pairwise.pairwise_distances(
                         X=x, Y=y, metric=self.metricType, **dictTemp)
                 elif self.metricType in scores.keys():
                     value = np.zeros((1, 1))
                     value[:, :] = scores[self.metricType](x, y, **dictTemp)
             except TypeError as e:
                 self.raiseAWarning(
                     'There are some unexpected keyword arguments found in Metric with type "',
                     self.metricType, '"!')
                 self.raiseAnError(TypeError, 'Input parameters error:\n',
                                   str(e), '\n')
             if value.shape == (1, 1):
                 return value[0]
             else:
                 return value
         else:
             self.raiseAnError(
                 IOError,
                 'Metric SKL error: SKL metrics support only PointSets and not HistorySets'
             )
     else:
         if self.metricType == 'mahalanobis':
             covMAtrix = np.cov(x.T)
             kwargs['VI'] = np.linalg.inv(covMAtrix)
         dictTemp = utils.mergeDictionaries(kwargs, self.distParams)
         try:
             if self.metricType in pairwise.kernel_metrics().keys():
                 value = pairwise.pairwise_kernels(X=x,
                                                   metric=self.metricType,
                                                   **dictTemp)
             elif self.metricType in pairwise.distance_metrics().keys():
                 value = pairwise.pairwise_distances(X=x,
                                                     metric=self.metricType,
                                                     **dictTemp)
         except TypeError as e:
             self.raiseAWarning(
                 'There are some unexpected keyword arguments found in Metric with type "',
                 self.metricType, '"!')
             self.raiseAnError(TypeError, 'Input parameters error:\n',
                               str(e), '\n')
         if value.shape == (1, 1):
             return value[0]
         else:
             return value