-
Notifications
You must be signed in to change notification settings - Fork 5
/
utilities.py
59 lines (50 loc) · 1.5 KB
/
utilities.py
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
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Utilities for manipulating plant--pollinator data matrices.
Author:
Jean-Gabriel Young <[email protected]>
"""
import numpy as np
def contract(M):
"""Remove all empty rows and columns from a matrix."""
mask_0 = ~np.all(M == 0, axis=0)
M_prime = M[:, mask_0]
mask_1 = ~np.all(M_prime == 0, axis=1)
M_final = M_prime[mask_1, :]
return M_final, mask_0, mask_1
def expand_1d_array(x, mask):
"""Re-expand a contracted 1D matrix."""
y = np.zeros(mask.shape[0])
ix = -1
for i, nonemptyrow in enumerate(mask):
if nonemptyrow:
ix += 1
y[i] = x[ix]
return y
def expand_2d_array(x, mask_0, mask_1):
"""Re-expand a contracted 2D matrix."""
y = np.zeros((mask_1.shape[0], mask_0.shape[0]))
ix = -1
for i, nonemptyrow in enumerate(mask_1):
if nonemptyrow:
ix += 1
jx = -1
for j, nonemptycol in enumerate(mask_0):
if nonemptycol:
jx += 1
y[i, j] = x[ix, jx]
return y
def sort(A, B, reverse_X=True, reverse_Y=True):
"""Sort array A based on the margins of identically sized array B."""
margin0 = B.sum(axis=0)
margin1 = B.sum(axis=1)
if reverse_X:
argsort0 = np.argsort(-margin0)
else:
argsort0 = np.argsort(margin0)
if reverse_Y:
argsort1 = np.argsort(margin1)
else:
argsort1 = np.argsort(-margin1)
return A[argsort1, :][:, argsort0]