File: verif_m_to_npz.py

package info (click to toggle)
python-dtcwt 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,588 kB
  • sloc: python: 6,287; sh: 29; makefile: 13
file content (81 lines) | stat: -rwxr-xr-x 2,792 bytes parent folder | download | duplicates (5)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python

import numpy as np
from scipy.io import loadmat
from numpy import savez_compressed

def _mean(a, axis=None, *args, **kwargs):
    """Equivalent to numpy.mean except that the axis along which the mean is taken is not removed."""

    rv = np.mean(a, axis=axis, *args, **kwargs)

    if axis is not None:
        rv = np.expand_dims(rv, axis)

    return rv

def centre_indices(ndim=2,apron=8):
    """Returns the centre indices for the correct number of dimension
    """
    return tuple([slice(apron,-apron) for i in range(ndim)])

def summarise_mat(M, apron=8):
    """HACK to provide a 'summary' matrix consisting of the corners of the
    matrix and summed versions of the sub matrices.

    N.B. Keep this in sync with matlab/verif_m_to_npz.py.

    """
    centre = M[apron:-apron,apron:-apron,...]
    centre_sum = _mean(_mean(centre, axis=0), axis=1)

    return np.vstack((
        np.hstack((M[:apron,:apron,...], _mean(M[:apron,apron:-apron,...], axis=1), M[:apron,-apron:,...])),
        np.hstack((_mean(M[apron:-apron,:apron,...], axis=0), centre_sum, _mean(M[apron:-apron,-apron:,...], axis=0))),
        np.hstack((M[-apron:,:apron,...], _mean(M[-apron:,apron:-apron,...], axis=1), M[-apron:,-apron:,...])),
    ))

def summarise_cube(M, apron=4):
    """Provide a summary cube, extending  summarise_mat to 3D
    """
    return np.dstack(
        [summarise_mat(M[:,:,i,...], apron) for i in range(M.shape[-2])]
    )

verif_temp = loadmat('verification.mat')
verif = dict((k,v) for k, v in verif_temp.items() if (not k.startswith('_') and not k.startswith('qbgn')))
verif_cube = dict((k,v) for k, v in verif_temp.items() if (not k.startswith('_') and k.startswith('qbgn')))
del verif_temp

for idx, v in enumerate(verif['mandrill_Yh']):
    verif['mandrill_Yh_{0}'.format(idx)] = v[0]
del verif['mandrill_Yh']

for idx, v in enumerate(verif['mandrill_Yscale']):
    verif['mandrill_Yscale_{0}'.format(idx)] = v[0]
del verif['mandrill_Yscale']

for idx, v in enumerate(verif['mandrill_Yhb']):
    verif['mandrill_Yhb_{0}'.format(idx)] = v[0]
del verif['mandrill_Yhb']

for idx, v in enumerate(verif['mandrill_Yscaleb']):
    verif['mandrill_Yscaleb_{0}'.format(idx)] = v[0]
del verif['mandrill_Yscaleb']

for idx, v in enumerate(verif_cube['qbgn_Yh']):
    verif_cube['qbgn_Yh_{0}'.format(idx)] = v[0]
del verif_cube['qbgn_Yh']

for idx, v in enumerate(verif_cube['qbgn_Yscale']):
    verif_cube['qbgn_Yscale_{0}'.format(idx)] = v[0]
del verif_cube['qbgn_Yscale']

summaries = dict((k, summarise_mat(v)) for k, v in verif.items())
for k,v in verif_cube.items():
    summaries[k] = summarise_cube(v)

savez_compressed('../tests/verification.npz', **summaries)

# Convert qbgn.mat -> qbgn.npz
savez_compressed('../tests/qbgn.npz', **loadmat('qbgn.mat'))