-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextract_diagonals.py
executable file
·55 lines (41 loc) · 1.43 KB
/
extract_diagonals.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
#!/usr/bin/env python
import argparse
import h5py
import numpy as np
DATA_DATASET = 'matrixset'
DIAGONALS_DATASET = 'diagonalset'
WINDOW = 10
def parse_cli():
parser = argparse.ArgumentParser()
parser.add_argument(
metavar='DATA',
type=str,
dest='data',
help='path to the HDF5 file that stores the matrices'
)
return parser.parse_args()
def extract_diagonals(path):
handle = h5py.File(path, 'r+')
# load the data and move the sample axis to the front
data = np.moveaxis(np.array(handle[DATA_DATASET]), -1, 0)
width = data.shape[1]
samples = data.shape[0]
diagonalset = np.zeros((samples, 2 * WINDOW + 1, width), dtype=np.float32)
for j in range(samples):
image = data[j, :, :]
# always reallocate the diagonals image here to fill the left triangle with ones
out = np.ones((2 * WINDOW + 1, width), dtype=np.float32)
for i in range(-WINDOW, WINDOW + 1):
diagonal = np.diagonal(image, i)
out[i + WINDOW, abs(i):] = diagonal
out = ((out - out.min()) / out.max() * 2) - 1
diagonalset[j] = out
# remove the previous diagonals set if present
if DIAGONALS_DATASET in list(handle.keys()):
del handle[DIAGONALS_DATASET]
handle[DIAGONALS_DATASET] = diagonalset
# release the handle
handle.close()
if __name__ == '__main__':
arguments = parse_cli()
extract_diagonals(arguments.data)