<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 In case of reading training data from file, we illustrate the necessary steps as following
Step1:  type I
(get numpy.ndarray from cntk reader)
Step2:  type II
Step3:  save numpy.ndarray into file with cntk style
<ASSISTANT_TASK:>
Python Code:
import sys, os
import getpass
import numpy as np
mnist_dir = '/home/' + getpass.getuser() + '/repos/cntk/Examples/Image/DataSets/MNIST/'
trn_data_file = mnist_dir + 'Train-28x28_cntk_text.txt'
print (os.path.exists(trn_data_file))
Explanation: In case of reading training data from file, we illustrate the necessary steps as following:
 1. collect the info about the file data, including file_path, input_names, value_dim, is_sparse etc as
  discussed in https://github.com/Microsoft/CNTK/wiki/CNTKTextFormat-Reader
 2. pass the real data (accroding to data info) to network model 
The first step is done with MinibatchSource()
End of explanation
from cntk.io import MinibatchSource, CTFDeserializer, StreamDef, StreamDefs
input_stream = StreamDef(field='features', shape=784, is_sparse=False)
label_stream = StreamDef(field='labels', shape=10, is_sparse=False)
print (input_stream)
print (label_stream)
streams = StreamDefs(
    input = input_stream,
    label = label_stream
)
print (streams)
ctf = CTFDeserializer(trn_data_file, streams)
print (ctf)
source = MinibatchSource(ctf)
features_si = source['input']
label_si = source['label']
mb = source.next_minibatch(10)
print (mb[features_si].value.shape)
print (mb[features_si].m_data.data().to_numpy().shape)
print (np.asarray(mb[features_si].m_data).shape)
print (mb[features_si].num_samples)
Explanation: type I
(get numpy.ndarray from cntk reader)
End of explanation
from cntk.io import StreamConfiguration, text_format_minibatch_source
mb_source = text_format_minibatch_source(trn_data_file, [
        StreamConfiguration('features', 784),
        StreamConfiguration('labels', 10)
    ])
features_si = mb_source['features']
labels_si = mb_source['labels']
mb = mb_source.next_minibatch(10)
print (mb[features_si].value.shape)
print (mb[features_si].m_data.data().to_numpy().shape)
print (np.asarray(mb[features_si].m_data).shape)
Explanation: type II
End of explanation
import sys, os
import getpass
import numpy as np
Teemo_path = '/home/' + getpass.getuser() + '/git_test'
if not os.path.exists(Teemo_path):
    Teemo_path = '/home/' + getpass.getuser() + '/huizhu/git_test'
if not os.path.exists(Teemo_path):
    exit("Can not find Teemo_path")
sys.path.append(Teemo_path)
from Teemo.examples.mnist import load_data
trn_x, trn_y, valid_x, valid_y = load_data.mnist()
trn_x = np.asarray(trn_x, dtype=np.float32)
trn_y = np.asarray(trn_y, dtype=np.float32)
valid_x = np.asarray(valid_x, dtype=np.float32)
valid_y = np.asarray(valid_y, dtype=np.float32)
print ('load data sucessfully')
def save_txt(file_name, features_ndarray, labels_ndarray):
    dir_name = os.path.dirname(file_name)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    if os.path.exists(file_name):
        print ("File already exists: {0}".format(file_name))
    else:
        f = open(file_name, 'w')
        for feat, label in zip(features_ndarray, labels_ndarray):
            feat_str = ' '.join(feat.astype(str))
            label_str = ' '.join(label.astype(str))
            f.write('label| {0} features| {1}\n'.format(label_str, feat_str))
            
file_name = '/home/xtalpi/cntk_data/Train-28x28_cntk_text.txt'
save_txt(file_name, trn_x, trn_y)
Explanation: save numpy.ndarray into file with cntk style
End of explanation
<END_TASK>