Competence progress on points of given indexes.
161725
def progress_idxs(self, idxs):
        
        if self.progress_measure == 'abs_deriv_cov':
            if len(idxs) <= 1:
                return 0
            else:
                idxs = sorted(idxs)[- self.progress_win_size:]
                return abs(np.cov(zip(range(len(idxs)), self.get_data_c()[idxs]), rowvar=0)[0, 1])
            
        elif self.progress_measure == 'abs_deriv':
            if len(idxs) <= 1:
                return 0
            else:
                idxs = sorted(idxs)[- self.progress_win_size:]
                return np.abs(np.mean(np.diff(self.get_data_c()[idxs], axis=0)))

        elif self.progress_measure == 'abs_deriv_smooth':
            if len(idxs) <= 1:
                return 0
            else:
                idxs = sorted(idxs)[- self.progress_win_size:]
                v = self.get_data_c()[idxs]
                n = len(v)
                comp_beg = np.mean(v[:int(float(n)/2.)])
                comp_end = np.mean(v[int(float(n)/2.):])
                return np.abs(comp_end - comp_beg)
        else:
            raise NotImplementedError(self.progress_measure)
941712
def next_index(self) -> int:
        """"""
        return bisect.bisect_right(self.totals, random.random() * self.total)
1004459
private int gradientUpdate(List<TensorBuilder> factors,
      List<TensorBuilder> unaryFactors, double stepSize) {
    // The best assignment, as computed from only the unary factors.
    int[] variableNums = new int[unaryFactors.size()];
    int[] variableSizes = new int[unaryFactors.size()];
    int[] variableValues = new int[unaryFactors.size()];
    locallyDecodeFactors(unaryFactors, variableNums, variableSizes, variableValues);

    // Identify where unary factors disagree with larger factors to compute the
    // subgradient.
    List<Integer> variableDisagreementList = Lists.newArrayList();
    List<Integer> factorDisagreementList = Lists.newArrayList();
    List<Integer> factorValueList = Lists.newArrayList();
    for (int i = 0; i < factors.size(); i++) {
      TensorBuilder factor = factors.get(i);
      int[] bestFactorValues = factor.keyNumToDimKey(factor.getLargestValues(1)[0]);
      int[] factorVariableNums = factor.getDimensionNumbers();

      for (int j = 0; j < factorVariableNums.length; j++) {
        int index = Arrays.binarySearch(variableNums, factorVariableNums[j]);
        if (bestFactorValues[j] != variableValues[index]) {
          // Factor and unary assignments disagree on variableNums[index].
          // Store the disagreement, used later to compute the subgradient.
          variableDisagreementList.add(index);
          factorDisagreementList.add(i);
          factorValueList.add(bestFactorValues[j]);

          /*
          System.out.println("Disagreement " + Arrays.toString(factorVariableNums) + "=" + Arrays.toString(bestFactorValues) + ", "
              + variableNums[index] + "=" + variableValues[index]);
              */
        }
      }
    }

    // Update the factor lagrange multipliers based on the disagreements.
    // For each disagreement, the subgradient update decreases the weight of
    // each factor's maximum weight assignment, and increases the weight of
    // the disagreeing factor's maximum weight assignment.
    for (int i = 0; i < variableDisagreementList.size(); i++) {
      int variableIndex = variableDisagreementList.get(i);
      int factorNum = factorDisagreementList.get(i);

      TensorBuilder factor = factors.get(factorNum);
      TensorBuilder unaryFactor = unaryFactors.get(variableIndex);

      int variableNum = variableNums[variableIndex];
      int variableSize = variableSizes[variableIndex];
      int bestFactorValue = factorValueList.get(i);
      int bestUnaryValue = variableValues[variableIndex];

      SparseTensor unaryGradient = SparseTensor.singleElement(new int[] { variableNum },
          new int[] { variableSize }, new int[] { bestUnaryValue }, 1);
      SparseTensor factorGradient = SparseTensor.singleElement(new int[] { variableNum },
          new int[] { variableSize }, new int[] { bestFactorValue }, 1);

      unaryFactor.incrementWithMultiplier(unaryGradient, -1.0 * stepSize);
      unaryFactor.incrementWithMultiplier(factorGradient, stepSize);
      factor.incrementWithMultiplier(unaryGradient, stepSize);
      factor.incrementWithMultiplier(factorGradient, -1.0 * stepSize);
    }

    return variableDisagreementList.size();
  }
1038862
function indexLevels(summary) {
    var parts = summary.getParts();
    parts = parts.map(indexPartLevels);

    return summary.set('parts', parts);
}
186453
private void moveIndex() {
		int i = rightmostIndexBelowMax();
		if (i >= 0) {
			index[i] = index[i]+1; 
			for (int j = i+1; j<r; j++)
				index[j] = index[j-1] + 1;
		}
		else hasNext = false;
	}
673899
private static void clusterIteration(Matrix matrix,
                                         int numClusters,
                                         KMeansSeed seedType,
                                         CriterionFunction criterion) {
        DoubleVector[] centers = seedType.chooseSeeds(numClusters, matrix);

        // Compute the initial set of assignments for each data point based on
        // the initial assignments.
        int[] initialAssignments = new int[matrix.rows()];

        // If there is to be only one cluster, then everything will be auto
        // assigned to the first cluster.  This is just a special case that only
        // comes up when comparing other solutions to the non-solution.
        if (numClusters != 1) {
            int nc = 0;
            for (int i = 0; i < matrix.rows(); ++i) {

                DoubleVector vector = matrix.getRowVector(i);
                double bestSimilarity = 0;
                for (int c = 0; c < numClusters; ++c) {
                    double similarity = Similarity.cosineSimilarity(
                            centers[c], vector);
                    nc++;
                    if (similarity >= bestSimilarity) {
                        bestSimilarity = similarity;
                        initialAssignments[i] = c;
                    }
                }
            }
        }

        // Setup the criterion function with it's meta data.
        criterion.setup(matrix, initialAssignments, numClusters);

        // Iteratively swap each data point to a better cluster if such an
        // assignment exists.
        List<Integer> indices = new ArrayList<Integer>(matrix.rows());
        for (int i = 0; i < matrix.rows(); ++i)
            indices.add(i);

        // Iterate through each data point in random order.  For each data
        // point, try to assign it to a new cluster.  If no data point is moved
        // in an iteration, end the iterations.
        boolean changed = true;
        while (changed) {
            changed = false;
            Collections.shuffle(indices);
            for (int index : indices)
                changed |= criterion.update(index);
        }
    }
772793
def progress(self, p):
        '''
        
        '''
        self.task_stack[-1] = self.task_stack[-1]._replace(progress=p)
        self.progress_report()
1278100
static boolean multiPointRelatePoint_(MultiPoint multipoint_a,
			Point point_b, double tolerance, String scl,
			ProgressTracker progress_tracker) {
		RelationalOperationsMatrix relOps = new RelationalOperationsMatrix();
		relOps.resetMatrix_();
		relOps.setPredicates_(scl);
		relOps.setPointPointPredicates_();

		Envelope2D env_a = new Envelope2D();
		multipoint_a.queryEnvelope2D(env_a);
		Point2D pt_b = point_b.getXY();

		boolean bRelationKnown = false;
		boolean b_disjoint = RelationalOperations.pointDisjointEnvelope_(pt_b,
				env_a, tolerance, progress_tracker);

		if (b_disjoint) {
			relOps.pointPointDisjointPredicates_();
			bRelationKnown = true;
		}

		if (!bRelationKnown) {
			boolean b_intersects = false;
			boolean b_multipoint_contained = true;
			double tolerance_sq = tolerance * tolerance;

			for (int i = 0; i < multipoint_a.getPointCount(); i++) {
				Point2D pt_a = multipoint_a.getXY(i);

				if (Point2D.sqrDistance(pt_a, pt_b) <= tolerance_sq)
					b_intersects = true;
				else
					b_multipoint_contained = false;

				if (b_intersects && !b_multipoint_contained)
					break;
			}

			if (b_intersects) {
				relOps.m_matrix[MatrixPredicate.InteriorInterior] = 0;

				if (!b_multipoint_contained)
					relOps.m_matrix[MatrixPredicate.InteriorExterior] = 0;
				else
					relOps.m_matrix[MatrixPredicate.InteriorExterior] = -1;

				relOps.m_matrix[MatrixPredicate.ExteriorInterior] = -1;
			} else {
				relOps.m_matrix[MatrixPredicate.InteriorInterior] = -1;
				relOps.m_matrix[MatrixPredicate.InteriorExterior] = 0;
				relOps.m_matrix[MatrixPredicate.ExteriorInterior] = 0;
			}
		}

		boolean bRelation = relationCompare_(relOps.m_matrix, scl);
		return bRelation;
	}
377989
static boolean polylineRelatePoint_(Polyline polyline_a, Point point_b,
			double tolerance, String scl, ProgressTracker progress_tracker) {
        RelationalOperationsMatrix relOps = new RelationalOperationsMatrix();
        relOps.resetMatrix_();
        relOps.setPredicates_(scl);
        relOps.setLinePointPredicates_();

        Envelope2D env_a = new Envelope2D();
        polyline_a.queryEnvelope2D(env_a);
        Point2D pt_b = point_b.getXY();

        boolean bRelationKnown = false;
        boolean b_disjoint = RelationalOperations.pointDisjointEnvelope_(pt_b, env_a, tolerance, progress_tracker);

        if (b_disjoint)
        {
            relOps.linePointDisjointPredicates_(polyline_a);
            bRelationKnown = true;
        }

        if (!bRelationKnown)
        {
            MultiPoint boundary_a = null;
            boolean b_boundary_contains_point_known = false;
            boolean b_boundary_contains_point = false;

            if (relOps.m_perform_predicates[MatrixPredicate.InteriorInterior] || relOps.m_perform_predicates[MatrixPredicate.ExteriorInterior])
            {
                boolean b_intersects = RelationalOperations.linearPathIntersectsPoint_(polyline_a, pt_b, tolerance);

                if (b_intersects)
                {
                    if (relOps.m_perform_predicates[MatrixPredicate.InteriorInterior])
                    {
                        boundary_a = (MultiPoint)Boundary.calculate(polyline_a, progress_tracker);
                        b_boundary_contains_point = !RelationalOperations.multiPointDisjointPointImpl_(boundary_a, pt_b, tolerance, progress_tracker);
                        b_boundary_contains_point_known = true;

                        if (b_boundary_contains_point)
                            relOps.m_matrix[MatrixPredicate.InteriorInterior] = -1;
                        else
                            relOps.m_matrix[MatrixPredicate.InteriorInterior] = 0;
                    }

                    relOps.m_matrix[MatrixPredicate.ExteriorInterior] = -1;
                }
                else
                {
                    relOps.m_matrix[MatrixPredicate.InteriorInterior] = -1;
                    relOps.m_matrix[MatrixPredicate.ExteriorInterior] = 0;
                }
            }

            if (relOps.m_perform_predicates[MatrixPredicate.BoundaryInterior])
            {
                if (boundary_a != null && boundary_a.isEmpty())
                {
                    relOps.m_matrix[MatrixPredicate.BoundaryInterior] = -1;
                }
                else
                {
                    if (!b_boundary_contains_point_known)
                    {
                        if (boundary_a == null)
                            boundary_a = (MultiPoint)Boundary.calculate(polyline_a, progress_tracker);

                        b_boundary_contains_point = !RelationalOperations.multiPointDisjointPointImpl_(boundary_a, pt_b, tolerance, progress_tracker);
                        b_boundary_contains_point_known = true;
                    }

                    relOps.m_matrix[MatrixPredicate.BoundaryInterior] = (b_boundary_contains_point ? 0 : -1);
                }
            }

            if (relOps.m_perform_predicates[MatrixPredicate.BoundaryExterior])
            {
                if (boundary_a != null && boundary_a.isEmpty())
                {
                    relOps.m_matrix[MatrixPredicate.BoundaryExterior] = -1;
                }
                else
                {
                    if (b_boundary_contains_point_known && !b_boundary_contains_point)
                    {
                        relOps.m_matrix[MatrixPredicate.BoundaryExterior] = 0;
                    }
                    else
                    {
                        if (boundary_a == null)
                            boundary_a = (MultiPoint)Boundary.calculate(polyline_a, progress_tracker);

                        boolean b_boundary_equals_point = RelationalOperations.multiPointEqualsPoint_(boundary_a, point_b, tolerance, progress_tracker);
                        relOps.m_matrix[MatrixPredicate.BoundaryExterior] = (b_boundary_equals_point ? -1 : 0);
                    }
                }
            }

            if (relOps.m_perform_predicates[MatrixPredicate.InteriorExterior])
            {
                boolean b_has_length = polyline_a.calculateLength2D() != 0;

                if (b_has_length)
                {
                    relOps.m_matrix[MatrixPredicate.InteriorExterior] = 1;
                }
                else
                {
                    // all points are interior
                    MultiPoint interior_a = new MultiPoint(polyline_a.getDescription());
                    interior_a.add(polyline_a, 0, polyline_a.getPointCount());
                    boolean b_interior_equals_point = RelationalOperations.multiPointEqualsPoint_(interior_a, point_b, tolerance, progress_tracker);
                    relOps.m_matrix[MatrixPredicate.InteriorExterior] = (b_interior_equals_point ? -1 : 0);
                }
            }
        }

        boolean bRelation = relationCompare_(relOps.m_matrix, relOps.m_scl);
        return bRelation;
	}
510623
private void materializeKNNAndRKNNs(ArrayDBIDs ids, FiniteProgress progress) {
    // add an empty list to each rknn
    for(DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
      if(materialized_RkNN.get(iter) == null) {
        materialized_RkNN.put(iter, new TreeSet<DoubleDBIDPair>());
      }
    }

    // knn query
    List<? extends KNNList> kNNList = knnQuery.getKNNForBulkDBIDs(ids, k);
    for(DBIDArrayIter id = ids.iter(); id.valid(); id.advance()) {
      KNNList kNNs = kNNList.get(id.getOffset());
      storage.put(id, kNNs);
      for(DoubleDBIDListIter iter = kNNs.iter(); iter.valid(); iter.advance()) {
        materialized_RkNN.get(iter).add(DBIDUtil.newPair(iter.doubleValue(), id));
      }
      LOG.incrementProcessed(progress);
    }

    LOG.ensureCompleted(progress);
  }
1316914
def update(self,iiter,H,Y,eta,loss):
        """"""
        if iiter <= self.niter_trace+1:
            self.H[iiter] = H
            self.Y[iiter] = Y
        elif iiter >self.niter - self.niter_trace + 1:
            self.H[self.ltrace+iiter-self.niter-1] = H
            self.Y[self.ltrace+iiter-self.niter-1] = Y

        self.etas[iiter] = eta
        self.loss[iiter] = loss
        if self.loss[iiter] < self.lmin:
            self.Yh = Y
            self.lmin = self.loss[iiter]
            self.miniter = iiter if not iiter == -1 else self.niter + 1
1262351
def x_indexes(self, indexes):
        
        self._assert_shape(self._data, indexes, self._y_indexes)
        self._x_indexes = indexes
        self.x_index = len(indexes) / 2
984204
def V(x, requires_grad=False, volatile=False):
    ''' '''
    return map_over(x, lambda o: V_(o, requires_grad, volatile))
1158369
private int indicesFront(int[] entries) {
    int start = 0;
    for (int i = 0; i < entries.length; i++) {
      start *= numClasses;
      start += entries[i];
    }
    int offset = SloppyMath.intPow(numClasses, windowSize - entries.length);
    start *= offset;
    return start;
  }
1348946
def _update(self):
        
        aps = []
        for k, v in self.records.items():
            recall, prec = self._recall_prec(v, self.counts[k])
            ap = self._average_precision(recall, prec)
            aps.append(ap)
            if self.num is not None and k < (self.num - 1):
                self.sum_metric[k] = ap
                self.num_inst[k] = 1
        if self.num is None:
            self.num_inst = 1
            self.sum_metric = np.mean(aps)
        else:
            self.num_inst[-1] = 1
            self.sum_metric[-1] = np.mean(aps)
1331872
def precision_at(y_true, y_score, proportion, ignore_nas=False):
    '''
    
    '''
    # Sort scores in descending order
    scores_sorted = np.sort(y_score)[::-1]

    # Based on the proportion, get the index to split the data
    # if value is negative, return 0
    cutoff_index = max(int(len(y_true) * proportion) - 1, 0)
    # Get the cutoff value
    cutoff_value = scores_sorted[cutoff_index]

    # Convert scores to binary, by comparing them with the cutoff value
    scores_binary = np.array([int(y >= cutoff_value) for y in y_score])
    # Calculate precision using sklearn function
    if ignore_nas:
        precision = __precision(y_true, scores_binary)
    else:
        precision = precision_score(y_true, scores_binary)

    return precision, cutoff_value
39261
protected static void calculateSelectivityCoeffs(List<DoubleObjPair<DAFile>> daFiles, NumberVector query, double epsilon) {
    final int dimensions = query.getDimensionality();
    double[] lowerVals = new double[dimensions];
    double[] upperVals = new double[dimensions];

    VectorApproximation queryApprox = calculatePartialApproximation(null, query, daFiles);

    for(int i = 0; i < dimensions; i++) {
      final double val = query.doubleValue(i);
      lowerVals[i] = val - epsilon;
      upperVals[i] = val + epsilon;
    }

    DoubleVector lowerEpsilon = DoubleVector.wrap(lowerVals);
    VectorApproximation lowerEpsilonPartitions = calculatePartialApproximation(null, lowerEpsilon, daFiles);

    DoubleVector upperEpsilon = DoubleVector.wrap(upperVals);
    VectorApproximation upperEpsilonPartitions = calculatePartialApproximation(null, upperEpsilon, daFiles);

    for(int i = 0; i < daFiles.size(); i++) {
      int coeff = (queryApprox.getApproximation(i) - lowerEpsilonPartitions.getApproximation(i)) + (upperEpsilonPartitions.getApproximation(i) - queryApprox.getApproximation(i)) + 1;
      daFiles.get(i).first = coeff;
    }
  }