File: makenpz.py

package info (click to toggle)
python-scipy 1.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 93,828 kB
  • sloc: python: 156,854; ansic: 82,925; fortran: 80,777; cpp: 7,505; makefile: 427; sh: 294
file content (68 lines) | stat: -rw-r--r-- 1,880 bytes parent folder | download
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
"""
python makenpz.py DIRECTORY

Build a npz containing all data files in the directory.

"""

from __future__ import division, print_function, absolute_import

import os
import numpy as np
import argparse

from distutils.util import newer


def main():
    p = argparse.ArgumentParser(usage=__doc__.strip())
    p.add_argument('--use-timestamp', action='store_true', default=False,
                   help="don't rewrite npz file if it is newer than sources")
    p.add_argument('dirname')
    args = p.parse_args()

    inp = os.path.normpath(args.dirname)
    outp = inp + ".npz"

    # Skip rebuilding if no sources
    if os.path.isfile(outp) and not os.path.isdir(inp):
        print("[makenpz] {} not rebuilt".format(outp))
        return

    # Find source files
    files = []
    for dirpath, dirnames, filenames in os.walk(inp):
        for fn in filenames:
            if fn.endswith('.txt'):
                key = dirpath[len(inp)+1:] + '-' + fn[:-4]
                key = key.strip('-')
                files.append((key, os.path.join(dirpath, fn)))

    # Check if changes required
    if args.use_timestamp and os.path.isfile(outp):
        try:
            old_data = np.load(outp)
            try:
                changed = set(old_data.keys()) != set(key for key, _ in files)
            finally:
                old_data.close()
        except (IOError, OSError):
            # corrupted file
            changed = True

        changed = changed or any(newer(fn, outp) for key, fn in files)
        changed = changed or newer(__file__, outp)
        if not changed:
            print("[makenpz] {} is already up to date".format(outp))
            return

    data = {}
    for key, fn in files:
        data[key] = np.loadtxt(fn)

    print("[makenpz] generating {}".format(outp))
    np.savez_compressed(outp, **data)


if __name__ == "__main__":
    main()