# Path: utils.py
# def filesize(filename):
#     return bytes2human(os.path.getsize(filename))
# 
# def send_array(socket, A, flags=0, copy=True, track=False):
#     """send a numpy array with metadata"""
#     md = dict(
#         dtype = str(A.dtype),
#         shape = A.shape,
#     )
#     socket.send_json(md, flags|zmq.SNDMORE)
#     return socket.send(A, flags, copy=copy, track=track)
# 
# def recv_array(socket, flags=0, copy=True, track=False):
#     """recv a numpy array"""
#     md = socket.recv_json(flags=flags)
#     msg = socket.recv(flags=flags, copy=copy, track=track)
#     buf = buffer(msg)
#     A = np.frombuffer(buf, dtype=md['dtype'])
#     return A.reshape(md['shape'])
# 
# Path: IO.py
# MIC = 5563
# 
# SPEAKER = 5564
# 
# CAMERA = 5561
# 
# PROJECTOR = 5562
# 
# STATE = 5565
# 
# SNAPSHOT= 5567
# 
# EVENT = 5568
# 
# EXTERNAL = 5566 # If you change this, you're out of luck.


import time
import multiprocessing as mp
import cPickle as pickle
import zmq
import numpy as np
    import Oger
from uuid import uuid1
from collections import deque
from sklearn import preprocessing as pp
from utils import filesize, send_array, recv_array
from IO import MIC, SPEAKER, CAMERA, PROJECTOR, STATE, SNAPSHOT, EVENT, EXTERNAL
                sender.send_json('{} RMSE {}'.format(me.name, rmse))
                
            if 'respond' in pushbutton and pushbutton['respond'] == me.name:
                audio_data = np.array(list(audio))
                video_data = np.array(list(video))

                print '{} chosen to respond. Audio data: {} Video data: {}'.format(me.name, audio_data.shape, video_data.shape)

                if audio_data.size == 0 and video_data.size == 0:
                    print '*** Audio data and video data arrays are empty. Aborting the response. ***'
                    continue

                row_diff = audio_data.shape[0] - audio_producer.length
                if row_diff < 0:
                    audio_data = np.vstack([ audio_data, np.zeros((-row_diff, audio_data.shape[1])) ])
                else:
                    audio_data = audio_data[:audio_producer.length]

                sound = audio_producer(audio_data)
                
                stride = audio_producer.length/audio2video.length
                projection = audio2video(audio_data[audio_data.shape[0] - stride*audio2video.length::stride])

                # DREAM MODE: You can train a network with zero audio input -> video output, and use this
                # to recreate the original training sequence with scary accuracy...

                for row in projection:
                    send_array(projector, row)

                for row in scaler.inverse_transform(sound):

# Path: utils.py
# def filesize(filename):
#     return bytes2human(os.path.getsize(filename))
# 
# def send_array(socket, A, flags=0, copy=True, track=False):
#     """send a numpy array with metadata"""
#     md = dict(
#         dtype = str(A.dtype),
#         shape = A.shape,
#     )
#     socket.send_json(md, flags|zmq.SNDMORE)
#     return socket.send(A, flags, copy=copy, track=track)
# 
# def recv_array(socket, flags=0, copy=True, track=False):
#     """recv a numpy array"""
#     md = socket.recv_json(flags=flags)
#     msg = socket.recv(flags=flags, copy=copy, track=track)
#     buf = buffer(msg)
#     A = np.frombuffer(buf, dtype=md['dtype'])
#     return A.reshape(md['shape'])
# 
# Path: IO.py
# MIC = 5563
# 
# SPEAKER = 5564
# 
# CAMERA = 5561
# 
# PROJECTOR = 5562
# 
# STATE = 5565
# 
# SNAPSHOT= 5567
# 
# EVENT = 5568
# 
# EXTERNAL = 5566 # If you change this, you're out of luck.


# Path: AI.py
import time
import multiprocessing as mp
import cPickle as pickle
import zmq
import numpy as np
    import Oger
from uuid import uuid1
from collections import deque
from sklearn import preprocessing as pp
from utils import filesize, send_array, recv_array
from IO import MIC, SPEAKER, CAMERA, PROJECTOR, STATE, SNAPSHOT, EVENT, EXTERNAL

                sender.send_json('{} RMSE {}'.format(me.name, rmse))
                
            if 'respond' in pushbutton and pushbutton['respond'] == me.name:
                audio_data = np.array(list(audio))
                video_data = np.array(list(video))

                print '{} chosen to respond. Audio data: {} Video data: {}'.format(me.name, audio_data.shape, video_data.shape)

                if audio_data.size == 0 and video_data.size == 0:
                    print '*** Audio data and video data arrays are empty. Aborting the response. ***'
                    continue

                row_diff = audio_data.shape[0] - audio_producer.length
                if row_diff < 0:
                    audio_data = np.vstack([ audio_data, np.zeros((-row_diff, audio_data.shape[1])) ])
                else:
                    audio_data = audio_data[:audio_producer.length]

                sound = audio_producer(audio_data)
                
                stride = audio_producer.length/audio2video.length
                projection = audio2video(audio_data[audio_data.shape[0] - stride*audio2video.length::stride])

                # DREAM MODE: You can train a network with zero audio input -> video output, and use this
                # to recreate the original training sequence with scary accuracy...

                for row in projection:
                    send_array(projector, row)

                for row in scaler.inverse_transform(sound):

                    send_array(speaker, row)