Area under the precision-recall curve
328419
def auprc(y_true, y_pred):
    
    y_true, y_pred = _mask_value_nan(y_true, y_pred)
    return skm.average_precision_score(y_true, y_pred)
356698
function (D, perplexity, tol) {
		var Nf = Math.sqrt(D.length); // this better be an integer
		var N = Math.floor(Nf);
		assert(N === Nf, "D should have square number of elements.");
		var Htarget = Math.log(perplexity); // target entropy of distribution
		var P = zeros(N * N); // temporary probability matrix

		var prow = zeros(N); // a temporary storage compartment
		for (var i = 0; i < N; i++) {
			var betamin = -Infinity;
			var betamax = Infinity;
			var beta = 1; // initial value of precision
			var done = false;
			var maxtries = 50;

			// perform binary search to find a suitable precision beta
			// so that the entropy of the distribution is appropriate
			var num = 0;
			while (!done) {
				//debugger;

				// compute entropy and kernel row with beta precision
				var psum = 0.0;
				for (var j = 0; j < N; j++) {
					var pj = Math.exp(-D[i * N + j] * beta);
					if (i === j) {
						pj = 0;
					} // we dont care about diagonals
					prow[j] = pj;
					psum += pj;
				}
				// normalize p and compute entropy
				var Hhere = 0.0;
				for (var j = 0; j < N; j++) {
					var pj = prow[j] / psum;
					prow[j] = pj;
					if (pj > 1e-7) Hhere -= pj * Math.log(pj);
				}

				// adjust beta based on result
				if (Hhere > Htarget) {
					// entropy was too high (distribution too diffuse)
					// so we need to increase the precision for more peaky distribution
					betamin = beta; // move up the bounds
					if (betamax === Infinity) {
						beta = beta * 2;
					}
					else {
						beta = (beta + betamax) / 2;
					}

				} else {
					// converse case. make distrubtion less peaky
					betamax = beta;
					if (betamin === -Infinity) {
						beta = beta / 2;
					}
					else {
						beta = (beta + betamin) / 2;
					}
				}

				// stopping conditions: too many tries or got a good precision
				num++;
				if (Math.abs(Hhere - Htarget) < tol) {
					done = true;
				}
				if (num >= maxtries) {
					done = true;
				}
			}

			// console.log('data point ' + i + ' gets precision ' + beta + ' after ' + num + ' binary search steps.');
			// copy over the final prow to P at row i
			for (var j = 0; j < N; j++) {
				P[i * N + j] = prow[j];
			}

		} // end loop over examples i

		// symmetrize P and normalize it to sum to 1 over all ij
		var Pout = zeros(N * N);
		var N2 = N * 2;
		for (var i = 0; i < N; i++) {
			for (var j = 0; j < N; j++) {
				Pout[i * N + j] = Math.max((P[i * N + j] + P[j * N + i]) / N2, 1e-100);
			}
		}

		return Pout;
	}
152520
def total_area_per_neurite(neurites, neurite_type=NeuriteType.all):
    '''
    '''
    return [neurite.area for neurite in iter_neurites(neurites, filt=is_type(neurite_type))]
1316875
def skimage_sinogram_space(geometry, volume_space, sinogram_space):
    """"""
    padded_size = int(np.ceil(volume_space.shape[0] * np.sqrt(2)))
    det_width = volume_space.domain.extent[0] * np.sqrt(2)
    skimage_detector_part = uniform_partition(-det_width / 2.0,
                                              det_width / 2.0,
                                              padded_size)

    skimage_range_part = geometry.motion_partition.insert(
        1, skimage_detector_part)

    skimage_range = uniform_discr_frompartition(skimage_range_part,
                                                interp=sinogram_space.interp,
                                                dtype=sinogram_space.dtype)

    return skimage_range
421899
def imagetransformer_cifar_tpu_range(rhp):
  """"""
  # After starting from base, set intervals for some parameters.
  rhp.set_float("learning_rate", 0.01, 1.0, scale=rhp.LOG_SCALE)
  rhp.set_discrete("num_decoder_layers", [8, 10, 12, 14, 16])
  rhp.set_discrete("hidden_size", [256, 512, 1024])
  rhp.set_discrete("block_length", [128, 256, 512])
  rhp.set_categorical("dec_attention_type", [
      cia.AttentionType.RELATIVE_LOCAL_1D, cia.AttentionType.LOCAL_1D])
793467
def HTM_style_train_on_datapoint(self, datapoint, label):
    
    activations = datapoint * self.dendrites
    self.nonlinearity(activations)

    #activations will quite likely still be sparse if using a threshold nonlinearity, so want to keep it sparse
    activation = numpy.sign(activations.sum())


    if label >= 1 and activation >= 0.5:
      strongest_branch = activations.rowMax(0)[0]
      datapoint.transpose()
      inc_vector = self.dendrites.getSlice(0, self.dim, strongest_branch, strongest_branch + 1) * self.permanence_increment
      inc_vector.elementNZMultiply(datapoint)
      dec_vector = self.dendrites.getSlice(0, self.dim, strongest_branch, strongest_branch + 1) * self.permanence_decrement
      dec_vector.elementNZMultiply(1 - datapoint)



      self.permanences.setSlice(0, strongest_branch, self.permanences.getSlice(0, self.dim, strongest_branch, strongest_branch + 1) + inc_vector - dec_vector)

      positions, scores = self.permanences.colNonZeros(strongest_branch)[0], self.permanences.colNonZeros(strongest_branch)[1]

      for position, score in zip(positions, scores):
        if score < self.permanence_threshold:
          self.dendrites[position, strongest_branch] = 0
          self.permanences[position, strongest_branch] = 0
          new_connection = random.sample(set(datapoint.colNonZeros(0)[0]) - set(self.dendrites.colNonZeros(strongest_branch)[0]), 1)[0]
          self.dendrites[new_connection, strongest_branch] = 1.
          self.permanences[new_connection, strongest_branch] = self.initial_permanence


    elif label < 1 and activation >= 0.5:
      # Need to weaken some connections
      strongest_branch = activations.rowMax(0)[0]

      dec_vector = self.dendrites.getSlice(0, self.dim, strongest_branch, strongest_branch + 1) * self.permanence_decrement
      datapoint.transpose()
      dec_vector.elementNZMultiply(datapoint)
      self.permanences.setSlice(0, strongest_branch, self.permanences.getSlice(0, self.dim, strongest_branch, strongest_branch + 1) - dec_vector)


    elif label >= 1 and activation < 0.5:
      # Need to create some new connections
      weakest_branch = numpy.argmin(self.permanences.colSums())
      if numpy.median(self.permanences.getCol(weakest_branch)) < self.permanence_threshold:
        self.permanences.setColToZero(weakest_branch)
        self.dendrites.setColToZero(weakest_branch)

        ones = datapoint.rowNonZeros(0)[0]
        dendrite_connections = numpy.random.choice(ones, size = self.dendrite_length, replace = False)
        for synapse in dendrite_connections:
          self.dendrites[synapse, weakest_branch] = 1.
          self.permanences[synapse, weakest_branch] = self.initial_permanence
977866
def vqa_attention_base_range(rhp):
  """"""
  # After starting from base, set intervals for some parameters.
  rhp.set_float("learning_rate", 0.1, 1.0, scale=rhp.LOG_SCALE)
  rhp.set_float("clip_grad_norm", 0.1, 10, scale=rhp.LOG_SCALE)
  rhp.set_discrete("batch_size", [128, 256, 512, 1024])
  rhp.set_float("weight_decay", 0.0, 1e-4)
  rhp.set_categorical("rnn_type", ["lstm", "lstm_layernorm"])
84881
def recall(self):
        
        true_pos = self.matrix[0][0]
        false_neg = self.matrix[0][1]
        return divide(1.0 * true_pos, true_pos + false_neg)
749446
def inferObjectsWithRandomMovements(self):
    
    for objectName, objectFeatures in self.objects.iteritems():
      self.reset()

      inferred = False
      prevTouchSequence = None

      for _ in xrange(4):

        while True:
          touchSequence = list(objectFeatures)
          random.shuffle(touchSequence)

          if prevTouchSequence is not None:
            if touchSequence[0] == prevTouchSequence[-1]:
              continue

          break

        for i, feature in enumerate(touchSequence):
          locationOnObject = (feature["top"] + feature["height"]/2,
                              feature["left"] + feature["width"]/2)
          self.move(objectName, locationOnObject)

          featureName = feature["name"]
          featureSDR = self.features[featureName]
          self.sense(featureSDR, learn=False)

          inferred = (
            set(self.objectLayer.getActiveCells()) ==
            set(self.objectRepresentations[objectName]) and

            set(self.inputLayer.getActiveCells()) ==
            set(self.inputRepresentations[(objectName,
                                           locationOnObject,
                                           featureName)]) and

            set(self.getActiveLocationCells()) ==
            set(self.locationRepresentations[(objectName, locationOnObject)]))

          if inferred:
            break

        prevTouchSequence = touchSequence

        if inferred:
          break
1151256
function estimateRootSize(seriesModel, targetInfo, viewRoot, containerWidth, containerHeight) {
    // If targetInfo.node exists, we zoom to the node,
    // so estimate whold width and heigth by target node.
    var currNode = (targetInfo || {}).node;
    var defaultSize = [containerWidth, containerHeight];

    if (!currNode || currNode === viewRoot) {
        return defaultSize;
    }

    var parent;
    var viewArea = containerWidth * containerHeight;
    var area = viewArea * seriesModel.option.zoomToNodeRatio;

    while (parent = currNode.parentNode) { // jshint ignore:line
        var sum = 0;
        var siblings = parent.children;

        for (var i = 0, len = siblings.length; i < len; i++) {
            sum += siblings[i].getValue();
        }
        var currNodeValue = currNode.getValue();
        if (currNodeValue === 0) {
            return defaultSize;
        }
        area *= sum / currNodeValue;

        // Considering border, suppose aspect ratio is 1.
        var parentModel = parent.getModel();
        var borderWidth = parentModel.get(PATH_BORDER_WIDTH);
        var upperHeight = Math.max(borderWidth, getUpperLabelHeight(parentModel, borderWidth));
        area += 4 * borderWidth * borderWidth
            + (3 * borderWidth + upperHeight) * Math.pow(area, 0.5);

        area > MAX_SAFE_INTEGER && (area = MAX_SAFE_INTEGER);

        currNode = parent;
    }

    area < viewArea && (area = viewArea);
    var scale = Math.pow(area / viewArea, 0.5);

    return [containerWidth * scale, containerHeight * scale];
}
266617
def _draw_pr_curve(self, recall, precision, label=None):
        
        self.ax.step(
            recall, precision, alpha=self.line_opacity, where='post', label=label
        )
        if self.fill_area:
            self.ax.fill_between(recall, precision, step='post', alpha=self.fill_opacity)
122622
private void thresholdBlur(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha) {
		int index = 0;
		float[] matrix = kernel.getKernelData( null );
		int cols = kernel.getWidth();
		int cols2 = cols/2;

		for (int y = 0; y < height; y++) {
			int ioffset = y*width;
            int outIndex = y;
			for (int x = 0; x < width; x++) {
				float r = 0, g = 0, b = 0, a = 0;
				int moffset = cols2;

                int rgb1 = inPixels[ioffset+x];
                int a1 = (rgb1 >> 24) & 0xff;
                int r1 = (rgb1 >> 16) & 0xff;
                int g1 = (rgb1 >> 8) & 0xff;
                int b1 = rgb1 & 0xff;
				float af = 0, rf = 0, gf = 0, bf = 0;
                for (int col = -cols2; col <= cols2; col++) {
					float f = matrix[moffset+col];

					if (f != 0) {
						int ix = x+col;
						if (!(0 <= ix && ix < width))
							ix = x;
						int rgb2 = inPixels[ioffset+ix];
                        int a2 = (rgb2 >> 24) & 0xff;
                        int r2 = (rgb2 >> 16) & 0xff;
                        int g2 = (rgb2 >> 8) & 0xff;
                        int b2 = rgb2 & 0xff;

						int d;
                        d = a1-a2;
                        if ( d >= -threshold && d <= threshold ) {
                            a += f * a2;
                            af += f;
                        }
                        d = r1-r2;
                        if ( d >= -threshold && d <= threshold ) {
                            r += f * r2;
                            rf += f;
                        }
                        d = g1-g2;
                        if ( d >= -threshold && d <= threshold ) {
                            g += f * g2;
                            gf += f;
                        }
                        d = b1-b2;
                        if ( d >= -threshold && d <= threshold ) {
                            b += f * b2;
                            bf += f;
                        }
					}
				}
                a = af == 0 ? a1 : a/af;
                r = rf == 0 ? r1 : r/rf;
                g = gf == 0 ? g1 : g/gf;
                b = bf == 0 ? b1 : b/bf;
				int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
				int ir = PixelUtils.clamp((int)(r+0.5));
				int ig = PixelUtils.clamp((int)(g+0.5));
				int ib = PixelUtils.clamp((int)(b+0.5));
				outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
                outIndex += height;
			}
		}
	}
1054642
public Map<String, Double> getFMeasureForLabels()
    {
        Map<String, Double> fMeasure = new LinkedHashMap<>();

        Map<String, Double> precisionForLabels = getPrecisionForLabels();
        Map<String, Double> recallForLabels = getRecallForLabels();

        for (String label : allGoldLabels) {
            double p = precisionForLabels.get(label);
            double r = recallForLabels.get(label);

            double fm = 0;

            if ((p + r) > 0) {
                fm = (2 * p * r) / (p + r);
            }

            fMeasure.put(label, fm);
        }

        return fMeasure;
    }
966442
def compute_metric(self, components):
        """"""
        numerator = components[RECALL_RELEVANT_RETRIEVED]
        denominator = components[RECALL_RELEVANT]
        if denominator == 0.:
            if numerator == 0:
                return 1.
            else:
                raise ValueError('')
        else:
            return numerator/denominator
19436
def plotAccuracyDuringSensorimotorInference(resultsFig5B, title="", yaxis=""):
  
  # Read out results and get the ranges we want.
  with open(resultsFig5B, "rb") as f:
    results = cPickle.load(f)

  objectRange = []
  featureRange = []
  for r in results:
    if r["numObjects"] not in objectRange: objectRange.append(r["numObjects"])
    if r["numFeatures"] not in featureRange: featureRange.append(r["numFeatures"])
  objectRange.sort()
  featureRange.sort()
  print "objectRange=",objectRange
  print "featureRange=",featureRange

  ########################################################################
  #
  # Accumulate the TM accuracies for each condition in a list and compute mean
  # and stdeviations
  # For L2 we average across all feature ranges
  accuracies = defaultdict(list)
  l2Accuracies = defaultdict(list)
  for r in results:
    accuracies[(r["numObjects"], r["numFeatures"])].append(r["objectCorrectSparsityTM"])
    l2Accuracies[r["numObjects"]].append(r["objectAccuracyL2"])

  # meanAccuracy[o,f] = accuracy of TM with o objects and f unique features.
  meanAccuracy = numpy.zeros((max(objectRange)+1, max(featureRange) + 1))
  stdev = numpy.zeros((max(objectRange)+1, max(featureRange) + 1))
  meanL2Accuracy = numpy.zeros(max(objectRange)+1)
  stdevL2 = numpy.zeros(max(objectRange)+1)
  for o in objectRange:
    for f in featureRange:
      a = numpy.array(accuracies[(o, f)])
      meanAccuracy[o, f] = 100.0*a.mean()
      stdev[o, f] = 100.0*a.std()

    # Accuracies for L2
    a = numpy.array(l2Accuracies[o])
    meanL2Accuracy[o] = 100.0*a.mean()
    stdevL2[o] = 100.0*a.std()


  # Create the plot.
  plt.figure()
  plotPath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                          "plots", "accuracy_during_sensorimotor_inference.pdf")

  # Plot each curve
  legendList = []
  colorList = ['r', 'b', 'g', 'm', 'c', 'k', 'y']

  for i in range(len(featureRange)):
    f = featureRange[i]
    legendList.append('Sequence layer, feature pool size: {}'.format(f))
    plt.errorbar(objectRange, meanAccuracy[objectRange, f],
                 yerr=stdev[objectRange, f],
                 color=colorList[i])

  plt.errorbar(objectRange, meanL2Accuracy[objectRange],
               yerr=stdevL2[objectRange],
               color=colorList[len(featureRange)])
  legendList.append('Sensorimotor layer')

  # format
  plt.legend(legendList, bbox_to_anchor=(0., 0.6, 1., .102), loc="right", prop={'size':10})
  plt.xlabel("Number of objects")
  plt.ylim(-10.0, 110.0)
  plt.ylabel(yaxis)
  plt.title(title)

    # save
  plt.savefig(plotPath)
  plt.close()
418685
def tpr(y_true, y_pred, round=True):
    
    y_true, y_pred = _mask_value_nan(y_true, y_pred)
    if round:
        y_true = np.round(y_true)
        y_pred = np.round(y_pred)
    return skm.recall_score(y_true, y_pred)
100293
def gini(y, p):
    

    # check and get number of samples
    assert y.shape == p.shape

    n_samples = y.shape[0]

    # sort rows on prediction column
    # (from largest to smallest)
    arr = np.array([y, p]).transpose()
    true_order = arr[arr[:,0].argsort()][::-1,0]
    pred_order = arr[arr[:,1].argsort()][::-1,0]

    # get Lorenz curves
    l_true = np.cumsum(true_order) / np.sum(true_order)
    l_pred = np.cumsum(pred_order) / np.sum(pred_order)
    l_ones = np.linspace(1/n_samples, 1, n_samples)

    # get Gini coefficients (area between curves)
    g_true = np.sum(l_ones - l_true)
    g_pred = np.sum(l_ones - l_pred)

    # normalize to true Gini coefficient
    return g_pred / g_true