radial average a 2darray around a center
    if no center is given, take middle
488116
def radialAverage(arr, center=None):
    
    # taken from
    # http://stackoverflow.com/questions/21242011/most-efficient-way-to-calculate-radial-profile
    s0, s1 = arr.shape[:2]
    if center is None:
        center = s0/2, s1/2
    y, x = np.indices((s0, s1))
    r = np.sqrt((x - center[0])**2 + (y - center[1])**2)
    r = r.astype(np.int)
    tbin = np.bincount(r.ravel(), arr.ravel())
    nr = np.bincount(r.ravel())
    radialprofile = tbin/ nr
    return radialprofile
617367
function middleInside (data, start, a, b) {
  var node = start,
    inside = false,
    px = (data[a] + data[b]) / 2,
    py = (data[a + 1] + data[b + 1]) / 2
  do {
    var p1 = node.i,
      p2 = node.next.i

    if (((data[p1 + 1] > py) !== (data[p2 + 1] > py)) &&
      (px < (data[p2] - data[p1]) * (py - data[p1 + 1]) / (data[p2 + 1] - data[p1 + 1]) + data[p1])) {
      inside = !inside
    }

    node = node.next
  } while (node !== start)

  return inside
}
273256
def reduce(self, sum1, sum2, *args):
        
        self.sum1g[...] += sum1
        if not self.pts_only: self.sum2g[...] += sum2

        if self.compute_mean_coords:
            N, centers_sum = args
            self.N[...] += N
            for i in range(self.bins.Ndim):
                self.centers[i][...] += centers_sum[i]
945912
def _double_centered_imp(a, out=None):
    
    out = _float_copy_to_out(out, a)

    dim = np.size(a, 0)

    mu = np.sum(a) / (dim * dim)
    sum_cols = np.sum(a, 0, keepdims=True)
    sum_rows = np.sum(a, 1, keepdims=True)
    mu_cols = sum_cols / dim
    mu_rows = sum_rows / dim

    # Do one operation at a time, to improve broadcasting memory usage.
    out -= mu_rows
    out -= mu_cols
    out += mu

    return out
586172
def center(self):
        ''''''
        points = np.array(self._points)
        return np.mean(points[:, COLS.XYZ], axis=0)
1360446
def point_in_circle(pt, center, radius):
    '''
    
    '''
    d = np.linalg.norm(np.asarray(pt) - np.asarray(center))
    return d <= radius
39731
def b_neg(self):
        '''
        
        '''
        nb = Point(self.center)

        if self.xAxisIsMinor:
            nb.x -= self.minorRadius
        else:
            nb.y -= self.minorRadius
        return nb
152547
def centroid(self):
        '''
        

        '''
        if self.v is None:
            raise ValueError('Mesh has no vertices; centroid is not defined')

        return np.mean(self.v, axis=0)
1131746
def center(self):
        '''
        
        '''
        return Point(self.midX, self.midY, self.origin.z)
960055
def averageSize(self):
        """"""
        cm = self.centerOfMass()
        coords = self.coordinates(copy=False)
        if not len(coords):
            return 0
        s, c = 0.0, 0.0
        n = len(coords)
        step = int(n / 10000.0) + 1
        for i in np.arange(0, n, step):
            s += utils.mag(coords[i] - cm)
            c += 1
        return s / c
400198
private Coordinate[] getCenterPoints( double[][][] houghValues, int maxCircles ) {

        Coordinate[] centerPoints = new Coordinate[maxCircles];
        int xMax = 0;
        int yMax = 0;
        int rMax = 0;

        for( int c = 0; c < maxCircles; c++ ) {
            double counterMax = -1;
            for( int radius = radiusMin; radius <= radiusMax; radius = radius + radiusInc ) {

                int indexR = (radius - radiusMin) / radiusInc;
                for( int y = 0; y < height; y++ ) {
                    for( int x = 0; x < width; x++ ) {
                        if (houghValues[x][y][indexR] > counterMax) {
                            counterMax = houghValues[x][y][indexR];
                            xMax = x;
                            yMax = y;
                            rMax = radius;
                        }
                    }

                }
            }

            centerPoints[c] = new Coordinate(xMax, yMax, rMax);

            clearNeighbours(houghValues, xMax, yMax, rMax);
        }
        return centerPoints;
    }
186505
def find_average_diameter(elements, coordinates, adjust=1, increment=0.1,
                          processes=None, **kwargs):
    """"""
    # Copy the coordinates as will perform many opertaions on them
    coordinates = deepcopy(coordinates)
    # Center of our cartesian system is always at origin
    origin = np.array([0, 0, 0])
    # Initial center of mass to reverse translation at the end
    initial_com = center_of_mass(elements, coordinates)
    # We just shift the cage to the origin.
    coordinates = shift_com(elements, coordinates)
    # We create an array of vdw radii of elements.
    elements_vdw = np.array([[atomic_vdw_radius[x.upper()]] for x in elements])
    # We calculate maximum diameter of a molecule to determine the radius
    # of a sampling sphere neccessary to enclose the whole molecule.
    shpere_radius = max_dim(elements, coordinates)[2]
    sphere_surface_area = 4 * np.pi * shpere_radius**2
    # Here we determine the number of sampling points necessary for a fine
    # sampling. Smaller molecules require more finner density of sampling
    # points on the sampling sphere's surface, whereas largen require less.
    # This formula was created so that larger molecule do not take much longer
    # to analyse, as number_sampling_points*length_of_sampling_vectors
    # results in quadratic increase of sampling time. The 250 factor was
    # specificly determined to produce close to 1 sampling point /Angstrom^2
    # for a sphere of radius ~ 24 Angstrom. We can adjust how fine is the
    # sampling by changing the adjust factor.
    number_of_points = int(np.log10(sphere_surface_area) * 250 * adjust)
    # Here I use code by Alexandre Devert for spreading points on a sphere:
    # http://blog.marmakoide.org/?p=1
    golden_angle = np.pi * (3 - np.sqrt(5))
    theta = golden_angle * np.arange(number_of_points)
    z = np.linspace(1 - 1.0 / number_of_points, 1.0 / number_of_points - 1.0,
                    number_of_points)
    radius = np.sqrt(1 - z * z)
    points = np.zeros((number_of_points, 3))
    points[:, 0] = radius * np.cos(theta) * shpere_radius
    points[:, 1] = radius * np.sin(theta) * shpere_radius
    points[:, 2] = z * shpere_radius
    # Here we analyse the vectors and retain the ones that create the molecule
    # outline.
    if processes:
        pool = Pool(processes=processes)
        parallel = [
            pool.apply_async(
                vector_analysis_reversed,
                args=(
                    point, coordinates, elements_vdw)
                ) for point in points
        ]
        results = [p.get() for p in parallel if p.get() is not None]
        pool.terminate()
    else:
        results = [
            vector_analysis_reversed(
                point, coordinates, elements_vdw)
            for point in points
        ]
    results_cleaned = [x[0] for x in results if x is not None]
    return np.mean(results_cleaned)*2
402771
function createCentroids(k) {
                var Centroid = [];
                var maxes = this.Observations.maxColumns();
                //console.log(maxes);

                for(var i = 1; i <= k; i++) {
                    var centroid = [];
                    for(var j = 1; j <= this.Observations.cols(); j++) {
                        centroid.push(Math.random() * maxes.e(j));
                    }

                    Centroid.push(centroid);
                }

                //console.log(centroid)

                return $M(Centroid);
            }
512107
def centerOfMass(self):
        
        cmf = vtk.vtkCenterOfMass()
        cmf.SetInputData(self.polydata(True))
        cmf.Update()
        c = cmf.GetCenter()
        return np.array(c)
799072
function averageColorNormalized() {
	for (var _len = arguments.length, colors = Array(_len), _key = 0; _key < _len; _key++) {
		colors[_key] = arguments[_key];
	}

	// If a single colors array was provided as opposed to separate arguments
	// for each color, break it out.
	if (colors.length === 1 && Array.isArray(colors[0][0])) colors = colors[0];

	var N = colors.length;

	// Normalize the color components to range [0, 1].
	var normalizedColors = colors.map(function (_ref) {
		var _ref2 = _slicedToArray(_ref, 3),
		    h = _ref2[0],
		    s = _ref2[1],
		    l = _ref2[2];

		return [h / 360, s / 100, l / 100];
	});

	var cartesianAverage = normalizedColors
	// Cylindrical => Cartesian coordinates.
	.map(function (_ref3) {
		var _ref4 = _slicedToArray(_ref3, 3),
		    h = _ref4[0],
		    s = _ref4[1],
		    l = _ref4[2];

		return [s * cos(h * PI2), s * sin(h * PI2), l];
	})
	// Sum the scaled coordinates to find a centerpoint.
	.reduce(function (_ref5, _ref6) {
		var _ref8 = _slicedToArray(_ref5, 3),
		    xA = _ref8[0],
		    yA = _ref8[1],
		    zA = _ref8[2];

		var _ref7 = _slicedToArray(_ref6, 3),
		    x = _ref7[0],
		    y = _ref7[1],
		    z = _ref7[2];

		return [xA + x / N, yA + y / N, zA + z / N];
	}, [0, 0, 0]);

	// Convert the average hue value back to a positive polar angle.
	var H = (atan2(cartesianAverage[Y], cartesianAverage[X]) / PI2 + 1) % 1;

	// Take the arithmetic mean of all saturation values.
	var S = normalizedColors.map(function (_ref9) {
		var _ref10 = _slicedToArray(_ref9, 3),
		    h = _ref10[0],
		    s = _ref10[1],
		    l = _ref10[2];

		return s / N;
	}).reduce(sum);

	// Take the arithmetic mean of all lightness values.
	var L = cartesianAverage[2];

	return [H, S, L];
}
222823
function(center, radius) {
      var vertArrays = this.vertArrays();
      for (var i = 0; i < vertArrays.length; ++i) {
        radius = vertArrays[i].updateSquaredSphereRadius(center, radius);
      }
      return radius;
  }
387933
def mad(a):
    '''
    '''
    a = np.asfarray(a).flatten()
    return np.median(np.abs(a - np.median(a)))