convert_data_npz_0.py 890 字节
Newer Older
M
Macrobull 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 11:50:03 2019

@author: Macrobull
"""

import sys
import numpy as np

from collections import OrderedDict as Dict

M
Macrobull 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

def _make_var_name(name):
    """
    make a valid variable name in Python code
    """

    if name == '':
        return '_'
    if name[0].isdigit():
        return 'var_' + name
    for s in ' *?\\/-:':
        name = name.replace(s, '_')
    if name.startswith('_'):
        name = 'var' + name
    return name


M
Macrobull 已提交
31 32 33 34
fn = sys.argv[1]
input_names = sys.argv[2].split(':')
output_name = sys.argv[3].split(':')

M
Macrobull 已提交
35
data = np.load(fn, encoding='bytes')
M
Macrobull 已提交
36 37 38
input_data = data['inputs']
output_data = data['outputs']

M
Macrobull 已提交
39 40
inputs = Dict(zip(map(_make_var_name, input_names), [input_data]))
outputs = Dict(zip(map(_make_var_name, output_name), [output_data]))
M
Macrobull 已提交
41

M
Macrobull 已提交
42
np.savez(fn, inputs=inputs, outputs=outputs)  # overwrite