aboutsummaryrefslogtreecommitdiffstats
path: root/Helpers/array_helpers.py
blob: b9ac4c9adc163fc29a252dd19565958827aa0bf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Helper Functions for Array Processing.
"""

import numpy as np
import sys

def intersect2d(A, B):
    """
    Crazy 2D Intersection.

    @note
    May need to transpose arrays, i.e.
    A = np.tranpose(a).copy()
    B = np.tranpose(a).copy()

    @params
    A = np.array([[1,4],[2,5],[3,6]])
    B = np.array([[1,4],[3,6],[7,8]])

    @returns
    C = np.array([[1,4],[3,6]])

    Source:
    http://stackoverflow.com/questions/8317022/get-intersecting-rows-across-two-2d-numpy-arrays

    Relevant:
    http://i.imgur.com/xVyoSl.jpg
    """

    nrows, ncols = A.shape
    dtype={'names':['f{}'.format(i) for i in range(ncols)],
           'formats':ncols * [A.dtype]}

    C = np.intersect1d(A.view(dtype), B.view(dtype))

    # This last bit is optional if you're okay with "C" being a structured array...
    C = C.view(A.dtype).reshape(-1, ncols)

    return C

def stepifyx(x):
    """
    Make steps from lines
    Cf. /usr/lib/pymodules/python2.7/matplotlib/lines.py
    872 steps = ma.zeros((2*len(vertices)-1, 2), np.float_)
    874 steps[0::2, 0], steps[1::2, 0] = vertices[:, 0], vertices[:-1, 0]
    875 steps[0::2, 1], steps[1:-1:2, 1] = vertices[:, 1], vertices[1:, 1]
    """
    sx = np.zeros(2 * x.shape[0] - 1, np.float)
    sx[0::2], sx[1::2] = x[:], x[:-1]
    return sx

def stepifyy(y):
    sy = np.zeros(2 * y.shape[0] - 1, np.float)
    sy[0::2], sy[1:-1:2] = y[:], y[1:]
    return sy