fancytools.math package

Submodules

fancytools.math.MaskedMovingAverage module

class fancytools.math.MaskedMovingAverage.MaskedMovingAverage(shape, calcVariance=False, dtype=<type 'numpy.float64'>)[source]

Bases: object

Calculating the moving average and variance on (optional masked) ndArray allowing to update different areas every time

moving average and variance taken from http://stackoverflow.com/a/14638138 referring to http://www.johndcook.com/blog/standard_deviation/

update(arr, mask=None)[source]

update moving average (and variance) with new ndarray (of the same shape as the init array) and an optional mask

fancytools.math.MovingAverage module

class fancytools.math.MovingAverage.MovingAverage(n)[source]

Bases: object

calc. the mean of the last n values, see http://en.wikipedia.org/wiki/Moving_average :param n: number of the last values to calc the mean from

reset()[source]
update(signal)[source]

fancytools.math.Point3D module

Created on 21 Aug 2015

#taken from http://codentronix.com/2011/04/20/simulation-of-3d-point-rotation-with-python-and-pygame/

class fancytools.math.Point3D.Point3D(x=0, y=0, z=0)[source]
project(win_width, win_height, fov, viewer_distance)[source]

Transforms this 3D point to 2D using a perspective projection.

rotateX(angle)[source]

Rotates the point around the X axis by the given angle in degrees.

rotateY(angle)[source]

Rotates the point around the Y axis by the given angle in degrees.

rotateZ(angle)[source]

Rotates the point around the Z axis by the given angle in degrees.

fancytools.math.angleDiff module

fancytools.math.angleDiff.angleDiff(angle1, angle2)[source]

smallest difference between 2 angles code from http://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles

fancytools.math.blockshaped module

fancytools.math.blockshaped.blockshaped(arr, nrows, ncols)[source]

Return an new array of shape (n, nrows, ncols) where n * nrows * ncols = arr.size

If arr is a 2D array, the returned array looks like n subblocks with each subblock preserving the “physical” layout of arr.

fancytools.math.blockshaped.from2dBlocks(arr)[source]

input needs to be 4d array (2d array of 2d arrays)

fancytools.math.blockshaped.into2dBlocks(arr, n0, n1)[source]

similar to blockshaped but splits an array into n0*n1 blocks

fancytools.math.blockshaped.unblockshaped(arr, h, w)[source]

Return an new array of shape (h, w) where h * w = arr.size

If arr is of shape (n, nrows, ncols), n sublocks of shape (nrows, ncols), then the returned array preserves the “physical” layout of the sublocks.

fancytools.math.boundingBox module

fancytools.math.boundingBox.boundingBox(booleanArray)[source]

return indices of the smallest bounding box enclosing all non-zero values within an array

>>> a = np.array([ [0,0,0,0],
...                [0,1,0,1],
...                [0,0,1,0],
...                [1,0,0,0],
...                [0,0,0,0] ])
>>> print boundingBox(a)
(slice(1, 3, None), slice(0, 3, None))

fancytools.math.execOnSubArrays module

fancytools.math.execOnSubArrays.execOnSubArrays(arrs, fn, splitX, splitY)[source]

execute a function(on or multiple arrays) only on sub sections works only on 2d arrays at the moment

>>> a1 = np.ones((1000,1000))
>>> a2 = np.ones((1000,1000))
>>> out = execOnSubArrays((a1,a2), lambda sa1,sa2: sa1+as2, splitX=10, splitY=10)

fancytools.math.findXAt module

fancytools.math.findXAt.findXAt(xArr, yArr, yVal)[source]

return all x values where y would be equal to given yVal if arrays are spline interpolated

fancytools.math.gridPointsFromEdges module

fancytools.math.gridPointsFromEdges.gridPointsFromEdges(edges, nCells, dtype=<type 'float'>)[source]

creates a regular 2d grid from given edge points (4*(x0,y0)) and number of cells in x and y

returns horizontal and vertical lines as (x0,y0,x1,y1)

fancytools.math.linRegressUsingMasked2dArrays module

fancytools.math.linRegressUsingMasked2dArrays.linRegressUsingMasked2dArrays(xVals, arrays, badMask)[source]

if you have multiple 2d arrays each with position given by xVals[array-index] and you want to do a linear regression on all cells but you also might mask different areas in each 2darray

returns ascent, offset, RMS-error

fancytools.math.line module

Collection of line-based functions line given as:

x0,y0,x1,y1 = line
fancytools.math.line.angle(line)[source]
fancytools.math.line.angle2[source]
fancytools.math.line.ascent(line)[source]
fancytools.math.line.distance(line, point)[source]

infinite line to point or line to line distance is point is given as line - use middle point of that liune

fancytools.math.line.distancePoint(p1, p2)[source]
fancytools.math.line.distanceVector[source]
fancytools.math.line.dxdy(line)[source]

return normalised ascent vector

fancytools.math.line.fromAttr(mid, ang, dist)[source]

create from middle, angle and distance

fancytools.math.line.fromAttr2(start, ang, dist)[source]

create from start, angle and distance

fancytools.math.line.fromFn(ascent, offs, length=1, px=0)[source]
fancytools.math.line.intersection[source]

Return the coordinates of a point of intersection given two lines. Return None if the lines are parallel, but non-colli_near. Return an arbitrary point of intersection if the lines are colli_near.

Parameters: line1 and line2: lines given by 4 points (x0,y0,x1,y1).

fancytools.math.line.isEven(angle)[source]

return whether lines is either horizontal or vertical

fancytools.math.line.length[source]
fancytools.math.line.merge(l1, l2)[source]

merge 2 lines together

fancytools.math.line.middle[source]
fancytools.math.line.pointIsBetween(startP, endP, p)[source]
fancytools.math.line.resize(line, factor)[source]

factor: relative length (1->no change, 2-> double, 0.5:half)

fancytools.math.line.rotate(line, angle)[source]
fancytools.math.line.segmentDistance(line, point)[source]
fancytools.math.line.segmentIntersection(line1, line2)[source]
fancytools.math.line.sort[source]

change point position if x1,y0 < x0,y0

fancytools.math.line.split(line, lines)[source]

split <line> into multiple sublines using intersection with <lines>

fancytools.math.line.splitN(line, n)[source]

split a line n times returns n sublines

fancytools.math.line.toFn(line)[source]
fancytools.math.line.translate(line, ascent, offs=0)[source]

offs -> shifts parallel to line ascent -> rotate line

fancytools.math.line.translate2P(line, t0, t1)[source]

fancytools.math.linspace2 module

fancytools.math.linspace2.linspace2(a, b, n)[source]

similar to numpy.linspace but excluding the boundaries

this is the normal numpy.linspace:

>>> print linspace(0,1,5)
[ 0.    0.25  0.5   0.75  1.  ]

and this gives excludes the boundaries:

>>> print linspace2(0,1,5)
[ 0.1  0.3  0.5  0.7  0.9]

fancytools.math.nearestPosition module

fancytools.math.nearestPosition.nearestPosition(array, value)[source]

return the index of that value that is most similar in the array needs a numpy.1darray

>>> import numpy
>>> a = numpy.array([1,2,3,7,3,647,223,777])
>>> nearestPosition(a, 22)
3
>>> nearestPosition(a, 300)
6

fancytools.math.nearestPosition2 module

class fancytools.math.nearestPosition2.NearestPosition2(array, lastPos=0)[source]

Bases: object

return the index of that value that is most similar in the array starting from the last known position, checking the right direction

  • assumes that new values to sort are close the the old ones
  • for this case and in case the array is sorted this approach is much faster than the normal nearestPosition
>>> import numpy
>>> a = numpy.array([1,3,7,12,15,20,33])
>>> n = NearestPosition2(a)
>>> n(5)
2
>>> n(22)
5
>>> n(24)
5

fancytools.math.pointInsidePolygon module

fancytools.math.pointInsidePolygon.pointInsidePolygon(x, y, poly)[source]

Determine if a point is inside a given polygon or not Polygon is a list of (x,y) pairs.

[code taken from: http://www.ariel.com.au/a/python-point-int-poly.html]

let’s make an easy square:

>>> poly = [ (0,0),                 (1,0),                 (1,1),                 (0,1) ]
>>> pointInsidePolygon(0.5,0.5, poly)
True
>>> pointInsidePolygon(1.5,1.5, poly)
False

fancytools.math.rotatePolygon module

fancytools.math.rotatePolygon.rotatePolygon(polygon, theta, origin=None)[source]

Rotates the given polygon around the origin or if not given it’s center of mass

polygon: np.array( (x1,y1), (...)) theta: rotation clockwise in RADIAN origin = [x,y] - if not given set to center of gravity

returns: None

fancytools.math.rotation module

fancytools.math.rotation.axisAndAngle2RotMatrix(axis, angle)[source]

http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector

Return the rotation matrix associated with counterclockwise rotation about the given axis by angle radians.

fancytools.math.rotation.rotMatrix2AxisAndAngle(R)[source]

http://stackoverflow.com/questions/12463487/obtain-rotation-axis-from-rotation-matrix-and-translation-vector-in-opencv

R : 3x3 rotation matrix returns axis, angle

fancytools.math.rotation.rotVector2Matrix(vec)[source]

better use cv2.Rodrigues(rvec)[0] for that job...

the angle is given as the magnitude of the rot vector, see https://www.safaribooksonline.com/library/view/learning-opencv/9780596516130/ch11s05.html

fancytools.math.similarity1DdiffShapedArrays module

fancytools.math.similarity1DdiffShapedArrays.similarity1DdiffShapedArrays(arr1, arr2, normalize=False)[source]

compare two strictly monotonous increasing 1d arrays of same or different size return a similarity index-> 0=identical