text:
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pycocotools.mask as mask_util
from collections import defaultdict

import lib.utils.colormap as colormap_utils
from lib.data.structures.keypoint import PersonKeypoints
from lib.utils.timer import Timer

class Visualizer(object):
    def __init__(self, cfg, im, dataset=None):

        self.cfg = cfg
        self.im = np.ascontiguousarray(np.asarray(im)[:, :, ::-1])  # BGR255
        self.dataset = dataset

        self._GRAY = [218, 227, 218]
        self._GREEN = [18, 127, 15]
        self._WHITE = [255, 255, 255]

    def vis_semseg_preds(self, semsegs=None, panos=None, label=None):
        timers = defaultdict(Timer)
        timers['show_semseg_masks'].tic()
        if panos is not None:
            img_resize = cv2.resize(np.array(self.im), (panos.shape[1], panos.shape[0]))
            segment_colors = self.get_colormap(self.cfg.SHOW_PANOS.COLORMAP)
            results = label['segments_info']
            pred_color = self.vis_segment_mask(panos, segment_colors, results=results, show_pano=True)
            self.im = cv2.addWeighted(img_resize, 1 - self.cfg.SHOW_PANOS.PANO_ALPHA, pred_color, self.cfg.SHOW_PANOS.PANO_ALPHA, 0)
        elif semsegs is not None:
            img_resize = cv2.resize(np.array(self.im), (semsegs.shape[1], semsegs.shape[0]))
            segment_colors = self.get_colormap(self.cfg.SHOW_SEMSEG.COLORMAP)
            color_mode = self.cfg.SHOW_SEMSEG.COLOR_MODE
            label_shift = self.cfg.SHOW_SEMSEG.LABEL_SHIFT
            ignore = self.cfg.SHOW_SEMSEG.IGNORE_LABEL
            pred_color = self.vis_segment_mask(semsegs, segment_colors, label_shift=label_shift, mode=color_mode, ignore=ignore)
            if label is not None:
                seg_color = self.vis_segment_mask(label, segment_colors, label_shift=label_shift, mode=color_mode, ignore=ignore)
                self.im = np.concatenate((img_resize, seg_color, pred_color), axis=1).astype(np.uint8)
            else:
                self.im = np.concatenate((img_resize, pred_color), axis=1).astype(np.uint8)
        else:
            raise NotImplementedError
        timers['show_semseg_masks'].toc()

        return self.im

    def vis_preds(self, boxes=None, classes=None, masks=None, keypoints=None, parsings=None, uvs=None, hiers=None,
                  semsegs=None, panos=None, panos_label=None):
        """Constructs a numpy array with the detections visualized."""
        timers = defaultdict(Timer)
        timers['bbox_prproc'].tic()

        if (semsegs is not None and self.cfg.SHOW_SEMSEG.ENABLED) or \
                (panos is not None and self.cfg.SHOW_PANOS.ENABLED):
            return self.vis_semseg_preds(semsegs, panos, panos_label)

        if hiers is not None and self.cfg.SHOW_HIER.ENABLED:
            classes = np.array(classes)
            boxes = boxes[classes == 0]
            classes = classes[classes == 0]

        if boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < self.cfg.VIS_TH:
            return self.im

        if masks is not None and len(masks) > 0:
            bit_masks = mask_util.decode(masks)
        else:
            bit_masks = masks

        # get color map
        ins_colormap = self.get_colormap(self.cfg.SHOW_BOX.COLORMAP)

        # Display in largest to smallest order to reduce occlusion
        areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
        sorted_inds = np.argsort(-areas)
        timers['bbox_prproc'].toc()

        instance_id = 1
        for i in sorted_inds:
            bbox = boxes[i, :-1]
            score = boxes[i, -1]
            if score < self.cfg.VIS_TH:
                continue

            # get instance color (box, class_bg)
            if self.cfg.SHOW_BOX.COLOR_SCHEME == 'category':
                ins_color = ins_colormap[classes[i]]
            elif self.cfg.SHOW_BOX.COLOR_SCHEME == 'instance':
                instance_id = instance_id % len(ins_colormap.keys())
                ins_color = ins_colormap[instance_id]
            else:
                ins_color = self._GREEN
            instance_id += 1

            # show box (on by default)
            if self.cfg.SHOW_BOX.ENABLED:
                if len(bbox) == 4:
                    timers['show_box'].tic()
                    self.vis_bbox((bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]), ins_color)
                    timers['show_box'].toc()
                elif len(bbox) == 5 and self.cfg.SHOW_BOX.SHOW_ROTATED_BOX:
                    timers['show_box'].tic()
                    self.vis_rotated_bbox(bbox, ins_color)
                    timers['show_box'].toc()
                else:
                    raise RuntimeError("Check the box format")

            # show class (on by default)
            if self.cfg.SHOW_CLASS.ENABLED:
                timers['show_class'].tic()
                class_str = self.get_class_string(classes[i], score)
                self.vis_class((bbox[0], bbox[1] - 2), class_str, ins_color)
                timers['show_class'].toc()

            show_masks = True if masks is not None and self.cfg.SHOW_MASK.ENABLED and len(masks) > i else False
            show_kpts = True if keypoints is not None and self.cfg.SHOW_KPS.ENABLED and len(keypoints) > i else False
            show_parss = True if parsings is not None and self.cfg.SHOW_PARSS.ENABLED and len(parsings) > i else False
            show_uvs = True if uvs is not None and self.cfg.SHOW_UV.ENABLED and len(uvs) > i else False
            show_hiers = True if hiers is not None and self.cfg.SHOW_HIER.ENABLED and len(hiers) > i else False

            # show mask
            if show_masks:
                timers['show_masks'].tic()
                self.vis_mask(bit_masks[..., i], ins_color, show_parss=show_parss)
                timers['show_masks'].toc()

            # show keypoints
            if show_kpts:
                timers['show_kpts'].tic()
                self.vis_keypoints(keypoints[i], show_parss=show_parss)
                timers['show_kpts'].toc()

            # show parsings
            if show_parss:
                timers['show_parss'].tic()
                parss_colormap = self.get_colormap(self.cfg.SHOW_PARSS.COLORMAP)
                self.vis_parsing(parsings[i], parss_colormap, show_masks=show_masks)
                timers['show_parss'].toc()

            # show uvs
            if show_uvs:
                timers['show_uvs'].tic()
                self.vis_uv(uvs[i], bbox)
                timers['show_uvs'].toc()

            # show hiers
            if show_hiers:
                timers['show_hiers'].tic()
                self.vis_hier(hiers[i], ins_color)
                timers['show_hiers'].toc()

        # for k, v in timers.items():
        #     print(' | {}: {:.3f}s'.format(k, v.total_time))

        return self.im

    def get_class_string(self, class_index, score=None):
        class_text = self.dataset.classes[class_index] if self.dataset is not None else 'id{:d}'.format(class_index)
        if score is None:
            return class_text
        return class_text + ' {:0.2f}'.format(score).lstrip('0')

    def get_colormap(self, colormap_type, rgb=False):
        colormap = eval('colormap_utils.{}'.format(colormap_type))
        if rgb:
            colormap = colormap_utils.dict_bgr2rgb(colormap)
        return colormap

    def vis_bbox(self, bbox, bbox_color):
        """Visualizes a bounding box."""
        (x0, y0, w, h) = bbox
        x1, y1 = int(x0 + w), int(y0 + h)
        x0, y0 = int(x0), int(y0)
        cv2.rectangle(self.im, (x0, y0), (x1, y1), bbox_color, thickness=self.cfg.SHOW_BOX.BORDER_THICK)

    def vis_rotated_bbox(self, rotated_bbox, bbox_color):
        """Visualizes a rotated bounding box."""
        (x0, y0, w, h, a) = rotated_bbox
        h_bbox = [(x0 - w / 2, y0 - h / 2),
                  (x0 + w / 2, y0 - h / 2),
                  (x0 + w / 2, y0 + h / 2),
                  (x0 - w / 2, y0 + h / 2)]
        import math
        a = a * math.pi / 180
        cos = math.cos(a)
        sin = math.sin(a)
        o_bbox = []
        for i in range(len(h_bbox)):
            x_i = int(sin * (h_bbox[i][1] - y0) + cos * (h_bbox[i][0] - x0) + x0)
            y_i = int(cos * (h_bbox[i][1] - y0) - sin * (h_bbox[i][0] - x0) + y0)
            o_bbox.append((x_i, y_i))
        for j in range(len(o_bbox)):
            cv2.line(self.im, o_bbox[j], o_bbox[(j + 1) % len(o_bbox)],
                     bbox_color, thickness=self.cfg.SHOW_BOX.BORDER_THICK)

    def vis_class(self, pos, class_str, bg_color):
        """Visualizes the class."""
        font_color = self.cfg.SHOW_CLASS.COLOR
        font_scale = self.cfg.SHOW_CLASS.FONT_SCALE

        x0, y0 = int(pos[0]), int(pos[1])
        # Compute text size.
        txt = class_str
        font = cv2.FONT_HERSHEY_SIMPLEX
        ((txt_w, txt_h), _) = cv2.getTextSize(txt, font, font_scale, 1)
        # Place text background.
        back_tl = x0, y0 - int(1.3 * txt_h)
        back_br = x0 + txt_w, y0
        cv2.rectangle(self.im, back_tl, back_br, bg_color, -1)
        # Show text.
        txt_tl = x0, y0 - int(0.3 * txt_h)
        cv2.putText(self.im, txt, txt_tl, font, font_scale, font_color, lineType=cv2.LINE_AA)

    def vis_mask(self, mask, bbox_color, show_parss=False):
        """Visualizes a single binary mask."""
        self.im = self.im.astype(np.float32)
        idx = np.nonzero(mask)

        border_color = self.cfg.SHOW_MASK.BORDER_COLOR
        border_thick = self.cfg.SHOW_MASK.BORDER_THICK

        mask_color = bbox_color if self.cfg.SHOW_MASK.MASK_COLOR_FOLLOW_BOX else _WHITE
        mask_color = np.asarray(mask_color)
        mask_alpha = self.cfg.SHOW_MASK.MASK_ALPHA

        try:
            _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        except:
            contours, _ = cv2.findContours(mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        if self.cfg.SHOW_MASK.SHOW_BORDER:
            cv2.drawContours(self.im, contours, -1, border_color, border_thick, cv2.LINE_AA)

        if not show_parss:
            self.im[idx[0], idx[1], :] *= 1.0 - mask_alpha
            self.im[idx[0], idx[1], :] += mask_alpha * mask_color

        self.im.astype(np.uint8)

    def vis_keypoints(self, kps, show_parss=False):
        """Visualizes keypoints (adapted from vis_one_image).
        kps has shape (4, #keypoints) where 4 rows are (x, y, logit, prob).
        """
        dataset_keypoints = PersonKeypoints.NAMES
        kp_lines = PersonKeypoints.CONNECTIONS

        # Convert from plt 0-1 RGBA colors to 0-255 BGR colors for opencv.
        cmap = plt.get_cmap('rainbow')
        colors = [cmap(i) for i in np.linspace(0, 1, len(kp_lines) + 2)]
        if show_parss:
            colors = [self.cfg.SHOW_KPS.KPS_COLOR_WITH_PARSING for c in colors]
        else:
            colors = [(c[2] * 255, c[1] * 255, c[0] * 255) for c in colors]

        # Perform the drawing on a copy of the image, to allow for blending.
        # kp_mask = np.copy(self.im)

        # Draw mid shoulder / mid hip first for better visualization.
        mid_shoulder = (kps[:2, dataset_keypoints.index('right_shoulder')] +
                        kps[:2, dataset_keypoints.index('left_shoulder')]) / 2.0
        sc_mid_shoulder = np.minimum(
            kps[2, dataset_keypoints.index('right_shoulder')],
            kps[2, dataset_keypoints.index('left_shoulder')])
        mid_hip = (kps[:2, dataset_keypoints.index('right_hip')] +
                   kps[:2, dataset_keypoints.index('left_hip')]) / 2.0
        sc_mid_hip = np.minimum(
            kps[2, dataset_keypoints.index('right_hip')],
            kps[2, dataset_keypoints.index('left_hip')])
        nose_idx = dataset_keypoints.index('nose')
        if sc_mid_shoulder > self.cfg.SHOW_KPS.KPS_TH and kps[2, nose_idx] > self.cfg.SHOW_KPS.KPS_TH:
            cv2.line(self.im, tuple(mid_shoulder), tuple(kps[:2, nose_idx]), color=colors[len(kp_lines)],
                     thickness=self.cfg.SHOW_KPS.LINK_THICK, lineType=cv2.LINE_AA)
        if sc_mid_shoulder > self.cfg.SHOW_KPS.KPS_TH and sc_mid_hip > self.cfg.SHOW_KPS.KPS_TH:
            cv2.line(self.im, tuple(mid_shoulder), tuple(mid_hip), color=colors[len(kp_lines) + 1],
                     thickness=self.cfg.SHOW_KPS.LINK_THICK, lineType=cv2.LINE_AA)

        # Draw the keypoints.
        for l in range(len(kp_lines)):
            i1 = kp_lines[l][0]
            i2 = kp_lines[l][1]
            p1 = kps[0, i1], kps[1, i1]
            p2 = kps[0, i2], kps[1, i2]
            if kps[2, i1] > self.cfg.SHOW_KPS.KPS_TH and kps[2, i2] > self.cfg.SHOW_KPS.KPS_TH:
                cv2.line(self.im, p1, p2, color=colors[l],
                         thickness=self.cfg.SHOW_KPS.LINK_THICK, lineType=cv2.LINE_AA)
            if kps[2, i1] > self.cfg.SHOW_KPS.KPS_TH:
                cv2.circle(self.im, p1, radius=self.cfg.SHOW_KPS.CIRCLE_RADIUS, color=colors[l],
                           thickness=self.cfg.SHOW_KPS.CIRCLE_THICK, lineType=cv2.LINE_AA)
            if kps[2, i2] > self.cfg.SHOW_KPS.KPS_TH:
                cv2.circle(self.im, p2, radius=self.cfg.SHOW_KPS.CIRCLE_RADIUS, color=colors[l],
                           thickness=self.cfg.SHOW_KPS.CIRCLE_THICK, lineType=cv2.LINE_AA)

        # Blend the keypoints.
        cv2.addWeighted(self.im, 1.0 - self.cfg.SHOW_KPS.KPS_ALPHA, self.im, self.cfg.SHOW_KPS.KPS_ALPHA, 0)

    def vis_parsing(self, parsing, colormap, show_masks=True):
        """Visualizes a single binary parsing."""
        self.im = self.im.astype(np.float32)
        idx = np.nonzero(parsing)

        parsing_alpha = self.cfg.SHOW_PARSS.PARSING_ALPHA
        colormap = colormap_utils.dict2array(colormap)
        parsing_color = colormap[parsing.astype(np.int)]

        border_color = self.cfg.SHOW_PARSS.BORDER_COLOR
        border_thick = self.cfg.SHOW_PARSS.BORDER_THICK

        self.im[idx[0], idx[1], :] *= 1.0 - parsing_alpha
        # self.im[idx[0], idx[1], :] += alpha * parsing_color
        self.im += parsing_alpha * parsing_color

        if self.cfg.SHOW_PARSS.SHOW_BORDER and not show_masks:
            try:
                _, contours, _ = cv2.findContours(parsing.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
            except:
                contours, _ = cv2.findContours(parsing.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
            cv2.drawContours(self.im, contours, -1, border_color, border_thick, cv2.LINE_AA)

        self.im.astype(np.uint8)

    def vis_uv(self, uv, bbox):
        border_thick = self.cfg.SHOW_UV.BORDER_THICK
        grid_thick = self.cfg.SHOW_UV.GRID_THICK
        lines_num = self.cfg.SHOW_UV.LINES_NUM

        uv = np.transpose(uv, (1, 2, 0))
        uv = cv2.resize(uv, (int(bbox[2] - bbox[0] + 1), int(bbox[3] - bbox[1] + 1)), interpolation=cv2.INTER_LINEAR)
        roi_img = self.im[int(bbox[1]):int(bbox[3] + 1), int(bbox[0]):int(bbox[2] + 1), :]

        roi_img_resize = cv2.resize(roi_img, (2 * roi_img.shape[1], 2 * roi_img.shape[0]),
                                    interpolation=cv2.INTER_LINEAR)

        I = uv[:, :, 0]
        for i in range(1, 25):
            if len(I[I == i]) == 0:
                continue

            u = np.zeros_like(I)
            v = np.zeros_like(I)
            u[I == i] = uv[:, :, 1][I == i]
            v[I == i] = uv[:, :, 2][I == i]

            for ind in range(1, lines_num):
                thred = 1.0 * ind / lines_num
                _, thresh = cv2.threshold(u, u.min() + thred * (u.max() - u.min()), 255, 0)
                dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
                dist_transform = np.uint8(dist_transform)

                try:
                    _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
                except:
                    contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
                contours = [(col * 2) for col in contours]
                cv2.drawContours(roi_img_resize, contours, -1, ((1 - thred) * 255, thred * 255, thred * 200),
                                 grid_thick)

                _, thresh = cv2.threshold(v, v.min() + thred * (v.max() - v.min()), 255, 0)
                dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
                dist_transform = np.uint8(dist_transform)

                try:
                    _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
                except:
                    contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
                contours = [(col * 2) for col in contours]
                cv2.drawContours(roi_img_resize, contours, -1, (thred * 255, (1 - thred) * 255, thred * 200),
                                 grid_thick)

        _, thresh = cv2.threshold(I, 0.5, 255, 0)
        dist_transform = cv2.distanceTransform(np.uint8(thresh), cv2.DIST_L2, 0)
        dist_transform = np.uint8(dist_transform)
        try:
            _, contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        except:
            contours, _ = cv2.findContours(dist_transform, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        contours = [(col * 2) for col in contours]
        cv2.drawContours(roi_img_resize, contours, -1, (70, 150, 0), border_thick)

        roi_img[:] = cv2.resize(roi_img_resize, (roi_img.shape[1], roi_img.shape[0]), interpolation=cv2.INTER_LINEAR)[:]

        return self.im

    def vis_hier(self, hier, bbox_color):
        border_thick = self.cfg.SHOW_HIER.BORDER_THICK
        N = len(hier) // 5
        for i in range(N):
            if hier[i * 5 + 4] > 0:
                cv2.rectangle(
                    self.im,
                    (int(hier[i * 5]), int(hier[i * 5 + 1])),
                    (int(hier[i * 5 + 2]), int(hier[i * 5 + 3])),
                    bbox_color,
                    thickness=border_thick
                )

    def vis_segment_mask(self, labelmap, colors, label_shift=0, mode='BGR', ignore=0, results=None, show_pano=False):
        """Visualize segmentation."""
        labelmap_bgr = np.zeros((labelmap.shape[0], labelmap.shape[1], 3), dtype=np.uint8)
        if show_pano and results is not None:
            id_pair = self.dataset.json_pano_id_to_contiguous_category_id
            boxes = []
            classes = []
            for seg_info in results:
                if not isinstance(labelmap, np.ndarray):
                    if labelmap.is_cuda:
                        labelmap = labelmap.cpu()
                    labelmap = labelmap.numpy()
                color = colors[seg_info['category_id']]
                label = seg_info['id']
                if seg_info['isthing']:
                    color += np.random.randint(low=-70, high=70, size=3)
                    color = list(np.maximum(0, np.minimum(255, color)))
                    binary_map = (labelmap == label)[:,:,np.newaxis].astype(np.uint8)
                    x, y, w, h = cv2.boundingRect(binary_map)
                    boxes.append((x, y, w, h))
                    classes.append(seg_info['category_id'])
                labelmap_bgr += (labelmap == label)[:, :, np.newaxis] * np.tile(
                    np.asarray(color, dtype=np.uint8), (labelmap.shape[0], labelmap.shape[1], 1))
            self.im = labelmap_bgr
            for box, cls in zip(boxes, classes):
                self.vis_bbox(box, self._GREEN)
                class_str = self.get_class_string(id_pair[cls])
                self.vis_class((box[0], box[1] - 2), class_str, self._GREEN)
        else:
            if not isinstance(labelmap, np.ndarray):
                if labelmap.is_cuda:
                    labelmap = labelmap.cpu()
            for label in np.unique(labelmap):
                if label < 0:
                    labelmap_bgr += (labelmap == label)[:, :, np.newaxis] * np.tile(
                        np.asarray([ignore, ignore, ignore], dtype=np.uint8), (labelmap.shape[0], labelmap.shape[1], 1))
                    continue
                labelmap_bgr += (labelmap == label)[:, :, np.newaxis] * np.tile(
                    np.asarray(colors[np.uint8(label - label_shift)], dtype=np.uint8), (labelmap.shape[0], labelmap.shape[1], 1))

        if mode == 'RGB':
            return labelmap_bgr[:, :, ::-1]
        else:
            return labelmap_bgr


text:
# coding: utf-8

"""
加载数据的类
数据整形类
"""

import numpy as np
import csv
from keras.preprocessing.image import load_img, img_to_array
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split

class ImageDataGenerator(object):
    def __init__(self, args):
        self.path = args.datasetpath
        self.seq_length = args.seqlength
        self.strides = args.strides
        self.batch_size = args.batchsize
        self.imgsize = args.imgsize
        self.reset()

    def reset(self):
        """ reset data load list """
        print("reset data load list")
        self.X = []
        self.Y = []
        self.X_data = []
        self.Y_data = []

    def flow_from_directory(self):
        while True:
            with open(self.path, 'r') as f:
                reader = csv.reader(f)
                header = next(reader)

                for row in reader:
                    self.Y_data.append(int(row[1]))
                    img = load_img(row[0], target_size=(self.imgsize, self.imgsize))
                    img_array = img_to_array(img)
                    x = (img_array / 255.).astype(np.float32)
                    print("x.shape", x.shape)
                    self.X_data.append(x)

                    # 存储batch size个数后，格式化数据后，返回
                    if len(self.Y_data) == self.batch_size * self.seq_length:

                        """ data format """
                        length_of_sequence = len(self.Y_data)
                        for i in range(0, length_of_sequence - self.seq_length + 1, self.strides):
                            self.X.append(self.X_data[i: i + self.seq_length])

                            # Y_data 数据格式
                            if self.Y_data[i] == 1:
                                self.Y_data[i] = 0

                            print("Y_data list: ", self.Y_data[i:i + self.seq_length])
                            if 1 in self.Y_data[i: i + self.seq_length]:
                                print("this data include shot")
                                self.Y.append(1)
                            else:
                                print("no include shot")
                                self.Y.append(0)

                        X_train = np.array(self.X).reshape(len(self.X), self.seq_length, self.imgsize, self.imgsize, 3)
                        Y_train = np.array(self.Y).reshape(len(self.Y), 1)
                        self.reset()
                        yield X_train, Y_train

def load_csv_data():
    X_data = []
    Y_data = []
    X = []
    Y = []
    seq_length = 10
    strides = 7
    datasetpath = ''

    # load csv file
    with open(datasetpath, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)

        for row in reader:
            Y_data.append(int(row[1]))

            img = load_img(row[0], target_size=(64, 64))
            img_array = img_to_array(img)
            x = (img_array / 255.).astype(np.float32)
            # print("x.shape", x.shape)
            X_data.append(x)

    """ data format """
    length_of_sequence = len(Y_data)
    for i in range(0, length_of_sequence - seq_length + 1, strides):
        X.append(X_data[i: i + seq_length])

        # Y_data 数据格式
        if Y_data[i] == 1:
            Y_data[i] = 0

        # print("Y_data list: ", Y_data[i:i+seq_length])
        if 1 in Y_data[i: i + seq_length]:
            # print("this data include shot")
            Y.append(1)
        else:
            # print("no include shot")
            Y.append(0)

    # convert np.array
    X = np.array(X).reshape(len(X), seq_length, 64, 64, 3)
    Y = np.array(Y).reshape(len(Y), 1)
    print("convert!!!!!!!!!!!!")
    print(X.shape)
    print(Y.shape)

    """ split train data/ validation data """
    train_len = int(len(Y) * 0.8)
    validation_len = len(Y) - train_len
    X_train, X_valid, Y_train, Y_valid = \
        train_test_split(X, Y, test_size=validation_len)

    return X_train, X_valid, Y_train, Y_valid

class Load_Feature_Data():

    def __init__(self, args):

        self.X_data = []
        self.Y_data = []
        self.X_ = []
        self.Y_ = []
        self.feature_length = args.featurelength
        self.seq_length = args.seqlength
        self.stride = args.stride
        self.datasetpath = args.datasetpath

    def load(self):

        with open(self.datasetpath, 'r') as f:

            reader = csv.reader(f)
            header = next(reader)

            for row in reader:
                self.Y_data.append(int(row[1]))

                feaure = list(map(float, row[2:]))
                self.X_data.append(feaure)

                # self.X_data.astype('float32')
                # self.Y_data.astype('float32')

                # data normalize
                # scaler = MinMaxScaler(feature_range=(0, 1))
                # self.X_data = scaler.fit_transform(self.X_data)

        # return (self.X_data, self.Y_data)

        """ 数据整形、规范化等 """
        ms = MinMaxScaler()
        self.X_data = ms.fit_transform(self.X_data)
        #时间序列填充
        length_of_sequence = len(self.X_data)

        for i in range(0, length_of_sequence - self.seq_length + 1, self.stride):
            self.X_.append(self.X_data[i: i + self.seq_length])
            self.Y_.append(self.Y_data[i: i + self.seq_length])

        self.X_ = np.array(self.X_).reshape(len(self.X_), self.seq_length, self.feature_length)
        self.Y_ = np.array(self.Y_).reshape(len(self.Y_), self.seq_length, 1)

        print(self.X_.shape)
        print(self.Y_.shape)

        """ 分为训练数据和验证数据 """
        train_len = int(len(self.X_) * 0.9)
        validation_len = len(self.X_) - train_len

        X_train, X_valid, Y_train, Y_valid = \
            train_test_split(self.X_, self.Y_, test_size=validation_len)

        return X_train, X_valid, Y_train, Y_valid

ext: py

max_stars_repo_name: polyudsa/AI

max_issues_repo_name: polyudsa/AI

max_forks_repo_name: polyudsa/AI

reason: import numpy


text:
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

dict_local = {"idx":3, "idy":4, "idz":5}
ps = pd.Series(dict_local)
ps2 = pd.Series(data=[1,2,3], index=['x', 'y', 'z'])
# series
ps2['x']
ps2p = ps2+ps2
# series properties
data = np.random.randn(3,3)
df = pd.DataFrame(data, index = ['x', 'y', 'z'], columns=('a', 'b', 'c'))
print(df['a'])
print(df[['a', 'c']])
print(df.loc["x"])
print(df.loc["x"]['a'])
print(df.iloc[0]) # loc with index
print()
# dataframes
df['d'] = df['c']+df['a']+df['b']
print(df)
df = df.drop('d', axis=1) # axis 0 is horizontal, inplace=true -> equals
print(df)
print(df < 0)
print(df[df < 0])
print(df[df['a'] < 0])
print()
# dataframes indexes
print(df.reset_index())
nix = ['cx', 'cyz', 'cxa']
df.index = nix # the same goes for the columns
df['d'] = [1,2,3]
df = df.set_index('d')
print(df)
# change index
"""
parents = ['a Class', 'a Class', 'b Class', 'b Class']
childs = ['xseries', 'xseries', 'yseries', 'yseries']
concated = list(zip(parents, childs))
dfm = pd.MultiIndex.from_tuples(concated)
npa = np.array([[10, 'a'], [10, 'b'], [30, 'c'], [10, 'd']])
dfm = pd.DataFrame(concated, index = [[10, 'a'], [10, 'b'], [30, 'c'], [10, 'd']], columns = ['x', 'y'])
"""
# multi index
mdf = df[df < 0]
print(mdf.dropna()) # deletes all nan rows, axis = 1 drops cols, thresh = 2 -> if cell have 2 nan
mdf = mdf.fillna('x')
# missing datas
deps = {"Departman":["Yazilim", "Yazilim", "Siber Güvenlik"],
        "Gorevli":["Ahmet", "Veli", "Cezve"],
        "Maas":[100, 200 , 300]}
depm = pd.DataFrame(deps)
print()
print(depm.groupby("Departman").count()) # employers
print(depm.groupby("Departman").mean())
print(depm.groupby("Departman").describe())
# group
d1 = {"name":["lorem", "ipsum","dolor","sit"],
      "type":[12,22,13,14],
      "code":[1,11,111,1111]}
d2 = {"name":["pixar", "pras","tuya","fexer"],
      "type":[13,52,53,24],
      "code":[2,22,222,2222]}
cdf1 = pd.DataFrame(d1, index = [0,1,2,3])
cdf2 = pd.DataFrame(d2, index = [0,1,2,3])
print()
print(pd.concat([cdf1,cdf2],ignore_index=True))
# concat
d1 = {"name":["lorem", "ipsum","dolor","sit"],
      "code":[1,11,111,1111],
      "xkey":[44,13,22,1]}
d2 = {"name":["lorem", "ipsum","dolor","sit"],
      "xkey":[44,13,22,1]}
ddf1 = pd.DataFrame(d1, index = [0,1,2,3])
ddf2 = pd.DataFrame(d2, index = [0,1,2,3])
print(pd.merge(ddf1, ddf2, on = "name"))
# merge
print(cdf1['name'].unique(), cdf1['name'].nunique())
print(cdf1['name'].value_counts())
def p1f(col):
    # plus 1 function
    return col+1
print(cdf1['code'].apply(p1f))
print(cdf1.isnull())
# advanced
dataex = pd.read_excel("dataex.xlsx")
print(dataex)
dataex['test'] = [1,23,34]
dataex.to_excel("ToExcel.xlsx")
# working with excel


text:
"""
    This file is part of dnnmapper.

    dnnmapper is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    dnnmapper is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with dnnmapper.  If not, see <http://www.gnu.org/licenses/>.
"""
from utils import *

CUR_ACIVATION_FUNCTION = Sigmoid

def ae_all(out_file, hidden_layers_sizes=None,
           corruption_levels=None,
           pretrain_lr=None,
           batch_size=None,
           training_epochs=None): # ae all on TIMIT
    print '... loading the data'
    data=load_vc_all_speakers()
    print '... loaded data with dimensions', str(data.shape[0]),'x', str(data.shape[1])
    print '... normalizing the data'
    mins, ranges = compute_normalization_factors(data)
    import pickle
    f=open('norm_male.pkl','w+')
    pickle.dump(mins, f)#[24*7:24*7+24]##$
    pickle.dump(ranges, f)#[24*7:24*7+24]##$
    f.flush()
    f.close()
    new_data = normalize_data(data, mins, ranges)
    numpy_rng = np.random.RandomState(89677)

    import theano
    n_train_batches = int(0.9*new_data.shape[0])
    n_train_batches /= batch_size
    #new_data = new_data.astype(np.float32)[:,24*7:24*7+24]
    #mins = mins[24*7:24*7+24]
    #ranges = ranges[24*7:24*7+24]
    train_set = theano.shared(new_data[:int(0.9*new_data.shape[0]), :])
    test_set = theano.shared(new_data[int(0.9*new_data.shape[0]):, :])
    test_set_unnormalized = unnormalize_data(new_data[int(0.9*new_data.shape[0]):, :], mins, ranges)[:,24*7:24*7+24]
    print '... building the model'
    from ae_stacked import SdA
    sda = SdA(
        numpy_rng=numpy_rng,
        n_ins=new_data.shape[1],
        hidden_layers_sizes=hidden_layers_sizes,#[1000, 1000],
    )
    print '... getting the pretraining functions'
    pretraining_fns = sda.pretraining_functions(train_set_x=train_set,
                                                batch_size=batch_size)

    print '...training the model'
    import time
    import pickle
    start_time = time.clock()
    reconstruct = theano.function(
                inputs=[
                ],
                outputs=sda.dA_layers[0].xrec,
                givens={
                    sda.dA_layers[0].x: test_set
                }
            )
    #corruption_levels = [0.2, 0.3]
    lr = pretrain_lr
    for i in xrange(sda.n_layers):
        # go through pretraining epochs
        for epoch in xrange(training_epochs):
            # go through the training set
            c = []
            for batch_index in xrange(n_train_batches):
                c.append(pretraining_fns[i](index=batch_index,
                         corruption=corruption_levels[i],
                         lr=pretrain_lr))
            if i==0:
                XH = reconstruct()
                XH = unnormalize_data(XH, mins, ranges)[:,24*7:24*7+24]
                print 'melCD', melCD(XH, test_set_unnormalized)
            lr *= 0.99
            if lr < 0.01:
                lr = 0.01
            import pickle
            f=open(out_file,'w+')
            pickle.dump(sda, f)
            pickle.dump(mins, f)
            pickle.dump(ranges, f)
            f.flush()
            f.close()
    print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
    print np.mean(c)

    end_time = time.clock()

    print 'The pretraining code for file ' +\
                          os.path.split(__file__)[1] +\
                          ' ran for %.2fm' % ((end_time - start_time) / 60.)

def model2_pre(x, y, xv, yv, xt, yt, mins, ranges,
               hidden_layers_sizes=None,
               corruption_levels=None,
               lr=None,
               batch_size=None,
               training_epochs=None): # 1.(A, Bb_backprop, C_backprop),MCEP15
    initial_learning_rate = lr
    learning_rate_decay = 0.998
    dropout=False
    squared_filter_length_limit = 15.0
    mom_start = 0.5
    mom_end = 0.99
    mom_epoch_interval = 500
    mom_params = {"start": mom_start,
                  "end": mom_end,
                  "interval": mom_epoch_interval}
    results_file_name = "results_backprop.txt"
    dataset = 'test_xy'
    train_set_x = x
    train_set_y = y
    valid_set_x = xv
    valid_set_y = xv
    test_set_x = xt
    test_set_y = yt

    # compute number of minibatches for training, validation and testing
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
    n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

    ######################
    # BUILD ACTUAL MODEL #
    ######################

    print '... building the model'

    # allocate symbolic variables for the data
    index = T.lscalar()    # index to a [mini]batch
    epoch = T.scalar()
    x = T.matrix('x')  # the data is presented as rasterized images
    y = T.matrix('y')  # the labels are presented as 1D vector of
                        # [int] labels
    learning_rate = theano.shared(np.asarray(initial_learning_rate,
        dtype=theano.config.floatX))
    random_seed = 1234
    rng = np.random.RandomState(random_seed)

    # construct the MLP class
    from dnn_dropout import MLP

    if 0: # no pretraining
        pretrained=None
    elif 1: # ae pretraining + middle layer
        f=open('ae_100_linear.pkl','r')
        sda=pickle.load(f)
        mins=pickle.load(f)
        ranges=pickle.load(f)
        f.close()
        pretrained = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=corruption_levels,
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True
                     )
        for i in range(len(sda.dA_layers)):
            pretrained.layers[i].W = sda.dA_layers[i].W
            pretrained.layers[i].b = sda.dA_layers[i].b
        for i in range(len(sda.dA_layers)):
            pretrained.layers[len(sda.dA_layers)+i].W = sda.dA_layers[len(sda.dA_layers)-i-1].W_prime
            pretrained.layers[len(sda.dA_layers)+i].b = sda.dA_layers[len(sda.dA_layers)-i-1].b_prime
    elif 0: # mlp pretraining
        pass
    classifier = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=corruption_levels,
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True,
                     pretrained=pretrained)
    #from mlp import MLP
    #classifier = MLP(
        #rng=rng,
        #input=x,
        #n_in=24,
        #n_hidden=75,
        #n_out=24
    #)

    # Build the expresson for the cost function.
    cost = classifier.mse(y)
    dropout_cost = classifier.dropout_mse(y)

    # Compile theano function for testing.
    test_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size],
                y: test_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(test_model, outfile="test_file.png",
    #        var_with_name_simple=True)

    # Compile theano function for validation.
    validate_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: valid_set_x[index * batch_size:(index + 1) * batch_size],
                y: valid_set_y[index * batch_size:(index + 1) * batch_size]})
    test_fprop = theano.function(inputs=[],
            outputs=classifier.layers[-1].output,
            givens={
                x: test_set_x
                })
    #theano.printing.pydotprint(validate_model, outfile="validate_file.png",
    #        var_with_name_simple=True)

    # Compute gradients of the model wrt parameters
    gparams = []
    for param in classifier.params:
        # Use the right cost function here to train with or without dropout.
        gparam = T.grad(dropout_cost if dropout else cost, param)
        gparams.append(gparam)

    # ... and allocate mmeory for momentum'd versions of the gradient
    gparams_mom = []
    for param in classifier.params:
        gparam_mom = theano.shared(np.zeros(param.get_value(borrow=True).shape,
            dtype=theano.config.floatX))
        gparams_mom.append(gparam_mom)
    from theano.ifelse import ifelse
    # Compute momentum for the current epoch
    mom = ifelse(epoch < mom_epoch_interval,
            mom_start*(1.0 - epoch/mom_epoch_interval) + mom_end*(epoch/mom_epoch_interval),
            mom_end)
    from collections import OrderedDict
    # Update the step direction using momentum
    updates = OrderedDict()
    for gparam_mom, gparam in zip(gparams_mom, gparams):
        # Misha Denil's original version
        #updates[gparam_mom] = mom * gparam_mom + (1. - mom) * gparam

        # change the update rule to match Hinton's dropout paper
        updates[gparam_mom] = mom * gparam_mom - (1. - mom) * learning_rate * gparam

    # ... and take a step along that direction
    for param, gparam_mom in zip(classifier.params, gparams_mom):
        # Misha Denil's original version
        #stepped_param = param - learning_rate * updates[gparam_mom]

        # since we have included learning_rate in gparam_mom, we don't need it
        # here
        stepped_param = param + updates[gparam_mom]

        # This is a silly hack to constrain the norms of the rows of the weight
        # matrices.  This just checks if there are two dimensions to the
        # parameter and constrains it if so... maybe this is a bit silly but it
        # should work for now.
        if param.get_value(borrow=True).ndim == 2:
            #squared_norms = T.sum(stepped_param**2, axis=1).reshape((stepped_param.shape[0],1))
            #scale = T.clip(T.sqrt(squared_filter_length_limit / squared_norms), 0., 1.)
            #updates[param] = stepped_param * scale

            # constrain the norms of the COLUMNs of the weight, according to
            # https://github.com/BVLC/caffe/issues/109
            col_norms = T.sqrt(T.sum(T.sqr(stepped_param), axis=0))
            desired_norms = T.clip(col_norms, 0, T.sqrt(squared_filter_length_limit))
            scale = desired_norms / (1e-7 + col_norms)
            updates[param] = stepped_param * scale
        else:
            updates[param] = stepped_param

    # Compile theano function for training.  This returns the training cost and
    # updates the model parameters.
    output = dropout_cost if dropout else cost
    train_model = theano.function(inputs=[epoch, index], outputs=output,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size:(index + 1) * batch_size],
                y: train_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(train_model, outfile="train_file.png",
    #        var_with_name_simple=True)

    # Theano function to decay the learning rate, this is separate from the
    # training function because we only want to do this once each epoch instead
    # of after each minibatch.
    decay_learning_rate = theano.function(inputs=[], outputs=learning_rate,
            updates={learning_rate: learning_rate * learning_rate_decay})

    ###############
    # TRAIN MODEL #
    ###############
    print '... training'

    best_params = None
    best_validation_errors = np.inf
    best_iter = 0
    test_score = 0.
    epoch_counter = 0
    import time
    start_time = time.clock()

    results_file = open(results_file_name, 'wb')
    X2=test_set_y.eval()
    X2 = unnormalize_data(X2, mins, ranges)
    X1=test_set_x.eval()
    X1 = unnormalize_data(X1, mins, ranges)
    last_reg = 10000.0
    while epoch_counter < training_epochs:
        # Train this epoch
        epoch_counter = epoch_counter + 1
        for minibatch_index in xrange(n_train_batches):
            minibatch_avg_cost = train_model(epoch_counter, minibatch_index)

        # Compute loss on validation set
        validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
        this_validation_errors = np.mean(validation_losses)

        # Report and save progress.
        print "epoch {}, test error {}, learning_rate={}{}".format(
                epoch_counter, this_validation_errors,
                learning_rate.get_value(borrow=True),
                " **" if this_validation_errors < best_validation_errors else "")

        best_validation_errors = min(best_validation_errors,
                this_validation_errors)
        results_file.write("{0}\n".format(this_validation_errors))
        results_file.flush()

        new_learning_rate = decay_learning_rate()
        YH=test_fprop()
        YH = unnormalize_data(YH, mins, ranges)

        print 'Regression ', melCD(X2[:,24*7:24*7+24],YH[:,24*7:24*7+24])#np.mean(np.mean((YH-X2)**2,1))
        print 'Baseline! ', melCD(X1[:,24*7:24*7+24],X2[:,24*7:24*7+24])#np.mean(np.mean((X1-X2)**2,1))
        #print 'Regression ', melCD(X2,YH)#np.mean(np.mean((YH-X2)**2,1))
        #print 'Baseline! ', melCD(X1,X2)#np.mean(np.mean((X1-X2)**2,1))
        if np.mean(np.mean((YH-X2)**2,1)) < last_reg:
            print 'This is better. Saving the model to ' + dataset+'.dnn.pkl'
            f = open(dataset+'.dnn.pkl','w+')
            pickle.dump(classifier, f)
            f.flush()
            f.close()
            last_reg = np.mean(np.mean((YH-X2)**2,1))

    end_time = time.clock()
    print(('Optimization complete. Best validation score of %f %% '
           'obtained at iteration %i, with test performance %f %%') %
          (best_validation_errors * 100., best_iter, test_score * 100.))
    print >> sys.stderr, ('The code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time) / 60.))

def model0_pre(x, y, xv, yv, xt, yt, mins, ranges,
               hidden_layers_sizes=None,
               corruption_levels=None,
               lr=None,
               batch_size=None,
               training_epochs=None): # GMM,MCEP
    import sys
    sys.path.append('../gitlab/pysig/src/python/')
    from pysig.learning.mapping import JDGMM
    model=JDGMM(Q=128,regularizer=0.02)
    x=x.eval().astype(np.float64)[:,7*24:7*24+24]
    y=y.eval().astype(np.float64)[:,7*24:7*24+24]
    xt=xt.eval().astype(np.float64)[:,7*24:7*24+24]
    yt=yt.eval().astype(np.float64)[:,7*24:7*24+24]
    model.train(x, y)
    YH=model.map(xt)
    mins = mins[7*24:7*24+24]
    ranges = ranges[7*24:7*24+24]

    YH = unnormalize_data(YH, mins, ranges)
    X2 = unnormalize_data(yt, mins, ranges)
    X1 = unnormalize_data(xt, mins, ranges)
    print 'Regression ', melCD(X2,YH)#np.mean(np.mean((YH-X2)**2,1))
    print 'Baseline! ', melCD(X1,X2)#np.mean(np.mean((X1-X2)**2,1))
    import pickle
    f=open('jdgmm3_male.pkl','w+')
    pickle.dump(model, f)
    f.flush()
    f.close()
    pass

def model1_pre(x, y, xv, yv, xt, yt, mins, ranges,
               hidden_layers_sizes=None,
               corruption_levels=None,
               lr=None,
               batch_size=None,
               training_epochs=None): # 4,(C_backprop),MCEP.
    initial_learning_rate = lr
    learning_rate_decay = 0.998
    dropout=False
    squared_filter_length_limit = 15.0
    mom_start = 0.5
    mom_end = 0.99
    mom_epoch_interval = 500
    mom_params = {"start": mom_start,
                  "end": mom_end,
                  "interval": mom_epoch_interval}
    results_file_name = "results_backprop.txt"
    dataset = 'test_xy'
    train_set_x = x
    train_set_y = y
    valid_set_x = xv
    valid_set_y = xv
    test_set_x = xt
    test_set_y = yt

    # compute number of minibatches for training, validation and testing
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
    n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

    ######################
    # BUILD ACTUAL MODEL #
    ######################

    print '... building the model'

    # allocate symbolic variables for the data
    index = T.lscalar()    # index to a [mini]batch
    epoch = T.scalar()
    x = T.matrix('x')  # the data is presented as rasterized images
    y = T.matrix('y')  # the labels are presented as 1D vector of
                        # [int] labels
    learning_rate = theano.shared(np.asarray(initial_learning_rate,
        dtype=theano.config.floatX))
    random_seed = 1234
    rng = np.random.RandomState(random_seed)

    # construct the MLP class

    if 1: # no pretraining
        pretrained=None
    elif 0: # ae pretraining + middle layer
        f = open('test.dnn.pkl','r')
        pretrained = pickle.load(f)
        f.close()
    elif 0: # mlp pretraining
        pass
    from dnn_dropout import MLP
    classifier = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=corruption_levels,
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True,
                     pretrained=pretrained)
    #from mlp import MLP
    #classifier = MLP(
        #rng=rng,
        #input=x,
        #n_in=24,
        #n_hidden=75,
        #n_out=24
    #)

    # Build the expresson for the cost function.
    cost = classifier.mse(y)
    dropout_cost = classifier.dropout_mse(y)

    # Compile theano function for testing.
    test_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size],
                y: test_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(test_model, outfile="test_file.png",
    #        var_with_name_simple=True)

    # Compile theano function for validation.
    validate_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: valid_set_x[index * batch_size:(index + 1) * batch_size],
                y: valid_set_y[index * batch_size:(index + 1) * batch_size]})
    test_fprop = theano.function(inputs=[],
            outputs=classifier.layers[-1].output,
            givens={
                x: test_set_x
                })
    #theano.printing.pydotprint(validate_model, outfile="validate_file.png",
    #        var_with_name_simple=True)

    # Compute gradients of the model wrt parameters
    gparams = []
    for param in classifier.params:
        # Use the right cost function here to train with or without dropout.
        gparam = T.grad(dropout_cost if dropout else cost, param)
        gparams.append(gparam)

    # ... and allocate mmeory for momentum'd versions of the gradient
    gparams_mom = []
    for param in classifier.params:
        gparam_mom = theano.shared(np.zeros(param.get_value(borrow=True).shape,
            dtype=theano.config.floatX))
        gparams_mom.append(gparam_mom)
    from theano.ifelse import ifelse
    # Compute momentum for the current epoch
    mom = ifelse(epoch < mom_epoch_interval,
            mom_start*(1.0 - epoch/mom_epoch_interval) + mom_end*(epoch/mom_epoch_interval),
            mom_end)
    from collections import OrderedDict
    # Update the step direction using momentum
    updates = OrderedDict()
    for gparam_mom, gparam in zip(gparams_mom, gparams):
        # Misha Denil's original version
        #updates[gparam_mom] = mom * gparam_mom + (1. - mom) * gparam

        # change the update rule to match Hinton's dropout paper
        updates[gparam_mom] = mom * gparam_mom - (1. - mom) * learning_rate * gparam

    # ... and take a step along that direction
    for param, gparam_mom in zip(classifier.params, gparams_mom):
        # Misha Denil's original version
        #stepped_param = param - learning_rate * updates[gparam_mom]

        # since we have included learning_rate in gparam_mom, we don't need it
        # here
        stepped_param = param + updates[gparam_mom]

        # This is a silly hack to constrain the norms of the rows of the weight
        # matrices.  This just checks if there are two dimensions to the
        # parameter and constrains it if so... maybe this is a bit silly but it
        # should work for now.
        if param.get_value(borrow=True).ndim == 2:
            #squared_norms = T.sum(stepped_param**2, axis=1).reshape((stepped_param.shape[0],1))
            #scale = T.clip(T.sqrt(squared_filter_length_limit / squared_norms), 0., 1.)
            #updates[param] = stepped_param * scale

            # constrain the norms of the COLUMNs of the weight, according to
            # https://github.com/BVLC/caffe/issues/109
            col_norms = T.sqrt(T.sum(T.sqr(stepped_param), axis=0))
            desired_norms = T.clip(col_norms, 0, T.sqrt(squared_filter_length_limit))
            scale = desired_norms / (1e-7 + col_norms)
            updates[param] = stepped_param * scale
        else:
            updates[param] = stepped_param

    # Compile theano function for training.  This returns the training cost and
    # updates the model parameters.
    output = dropout_cost if dropout else cost
    train_model = theano.function(inputs=[epoch, index], outputs=output,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size:(index + 1) * batch_size],
                y: train_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(train_model, outfile="train_file.png",
    #        var_with_name_simple=True)

    # Theano function to decay the learning rate, this is separate from the
    # training function because we only want to do this once each epoch instead
    # of after each minibatch.
    decay_learning_rate = theano.function(inputs=[], outputs=learning_rate,
            updates={learning_rate: learning_rate * learning_rate_decay})

    ###############
    # TRAIN MODEL #
    ###############
    print '... training'

    best_params = None
    best_validation_errors = np.inf
    best_iter = 0
    test_score = 0.
    epoch_counter = 0
    import time
    start_time = time.clock()

    results_file = open(results_file_name, 'wb')
    X2=test_set_y.eval()
    X2 = unnormalize_data(X2, mins, ranges)
    X1=test_set_x.eval()
    X1 = unnormalize_data(X1, mins, ranges)
    last_reg = 10000.0
    while epoch_counter < training_epochs:
        # Train this epoch
        epoch_counter = epoch_counter + 1
        for minibatch_index in xrange(n_train_batches):
            minibatch_avg_cost = train_model(epoch_counter, minibatch_index)

        # Compute loss on validation set
        validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
        this_validation_errors = np.mean(validation_losses)

        # Report and save progress.
        print "epoch {}, test error {}, learning_rate={}{}".format(
                epoch_counter, this_validation_errors,
                learning_rate.get_value(borrow=True),
                " **" if this_validation_errors < best_validation_errors else "")

        best_validation_errors = min(best_validation_errors,
                this_validation_errors)
        results_file.write("{0}\n".format(this_validation_errors))
        results_file.flush()

        new_learning_rate = decay_learning_rate()
        YH=test_fprop()
        YH = unnormalize_data(YH, mins, ranges)

        #print 'Regression ', melCD(X2[:,24*7:24*7+24],YH[:,24*7:24*7+24])#np.mean(np.mean((YH-X2)**2,1))
        #print 'Baseline! ', melCD(X1[:,24*7:24*7+24],X2[:,24*7:24*7+24])#np.mean(np.mean((X1-X2)**2,1))
        print 'Regression ', melCD(X2,YH)#np.mean(np.mean((YH-X2)**2,1))
        print 'Baseline! ', melCD(X1,X2)#np.mean(np.mean((X1-X2)**2,1))
        if np.mean(np.mean((YH-X2)**2,1)) < last_reg:
            print 'This is better. Saving the model to ' + dataset+'.dnn.pkl'
            f = open(dataset+'.dnn.pkl','w+')
            pickle.dump(classifier, f)
            f.flush()
            f.close()
            last_reg = np.mean(np.mean((YH-X2)**2,1))

    end_time = time.clock()
    print(('Optimization complete. Best validation score of %f %% '
           'obtained at iteration %i, with test performance %f %%') %
          (best_validation_errors * 100., best_iter, test_score * 100.))
    print >> sys.stderr, ('The code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time) / 60.))

def model2_pre_from_siae(inp_file):
    f=open(inp_file,'r')
    sda=pickle.load(f)
    f.close()
    hidden_layers_sizes = [sda.dA_layers[0].n_visible]
    for i in range(len(sda.dA_layers)):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    for i in range(len(sda.dA_layers)-1, -1, -1):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    hidden_layers_sizes.append(sda.dA_layers[0].n_visible)
    random_seed = 1234
    rng = np.random.RandomState(random_seed)
    x = T.matrix('x')  # the data is presented as rasterized images
    from dnn_dropout import MLP

    pretrained = MLP(rng=rng, input=x,
                 layer_sizes=hidden_layers_sizes,
                 dropout_rates=[0.0]*len(hidden_layers_sizes),
                 activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                 use_bias=True
                 )
    for i in range(len(sda.dA_layers)):
        pretrained.layers[i].W = sda.dA_layers[i].W
        pretrained.layers[i].b = sda.dA_layers[i].b
    for i in range(len(sda.dA_layers)):
        pretrained.layers[len(sda.dA_layers)+i].W = sda.dA_layers[len(sda.dA_layers)-i-1].W_prime
        pretrained.layers[len(sda.dA_layers)+i].b = sda.dA_layers[len(sda.dA_layers)-i-1].b_prime
    return pretrained

def model2_pre_from_speaker20(inp_file, midlayer=None, train_x=None, train_y=None):
    f=open(inp_file,'r')
    pretrained=pickle.load(f)
    return pretrained

def dnn_train(pretrain_func, inp_file, out_file, x, y, xv, yv, xt, yt, mins, ranges,
               hidden_layers_sizes=None,
               middle_layers_sizes=None,
               corruption_levels=None,
               lr=None,
               batch_size=None,
               training_epochs=None): # 1.(A, Bb_backprop, C_backprop),MCEP15
    initial_learning_rate = lr
    learning_rate_decay = 0.998
    dropout=False
    squared_filter_length_limit = 15.0
    mom_start = 0.5
    mom_end = 0.99
    mom_epoch_interval = 500
    mom_params = {"start": mom_start,
                  "end": mom_end,
                  "interval": mom_epoch_interval}
    results_file_name = "results_backprop.txt"
    dataset = 'test_xy'
    train_set_x = x
    train_set_y = y
    valid_set_x = xv
    valid_set_y = xv
    test_set_x = xt
    test_set_y = yt

    # compute number of minibatches for training, validation and testing
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
    n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

    ######################
    # BUILD ACTUAL MODEL #
    ######################

    print '... building the model'

    # allocate symbolic variables for the data
    index = T.lscalar()    # index to a [mini]batch
    epoch = T.scalar()
    x = T.matrix('x')  # the data is presented as rasterized images
    y = T.matrix('y')  # the labels are presented as 1D vector of
                        # [int] labels
    learning_rate = theano.shared(np.asarray(initial_learning_rate,
        dtype=theano.config.floatX))
    random_seed = 1234
    rng = np.random.RandomState(random_seed)

    # construct the MLP class
    from dnn_dropout import MLP

    pretrained = pretrain_func(inp_file, middle_layers_sizes, train_set_x, train_set_y)
    classifier = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=corruption_levels,
                     activations=[CUR_ACIVATION_FUNCTION]*(len(hidden_layers_sizes)+2),
                     use_bias=True,
                     pretrained=pretrained)
    #from mlp import MLP
    #classifier = MLP(
        #rng=rng,
        #input=x,
        #n_in=24,
        #n_hidden=75,
        #n_out=24
    #)

    # Build the expresson for the cost function.
    cost = classifier.mse(y)
    dropout_cost = classifier.dropout_mse(y)

    # Compile theano function for testing.
    test_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: test_set_x[index * batch_size:(index + 1) * batch_size],
                y: test_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(test_model, outfile="test_file.png",
    #        var_with_name_simple=True)

    # Compile theano function for validation.
    validate_model = theano.function(inputs=[index],
            outputs=classifier.mse(y),
            givens={
                x: valid_set_x[index * batch_size:(index + 1) * batch_size],
                y: valid_set_y[index * batch_size:(index + 1) * batch_size]})
    test_fprop = theano.function(inputs=[],
                                 outputs=classifier.layers[-1].output,
                                 givens={
                                     x: test_set_x
                                     })
    #theano.printing.pydotprint(validate_model, outfile="validate_file.png",
    #        var_with_name_simple=True)

    # Compute gradients of the model wrt parameters
    gparams = []
    for param in classifier.params:
        # Use the right cost function here to train with or without dropout.
        gparam = T.grad(dropout_cost if dropout else cost, param)
        gparams.append(gparam)

    # ... and allocate mmeory for momentum'd versions of the gradient
    gparams_mom = []
    for param in classifier.params:
        gparam_mom = theano.shared(np.zeros(param.get_value(borrow=True).shape,
            dtype=theano.config.floatX))
        gparams_mom.append(gparam_mom)
    from theano.ifelse import ifelse
    # Compute momentum for the current epoch
    mom = ifelse(epoch < mom_epoch_interval,
            mom_start*(1.0 - epoch/mom_epoch_interval) + mom_end*(epoch/mom_epoch_interval),
            mom_end)
    from collections import OrderedDict
    # Update the step direction using momentum
    updates = OrderedDict()
    for gparam_mom, gparam in zip(gparams_mom, gparams):
        # Misha Denil's original version
        #updates[gparam_mom] = mom * gparam_mom + (1. - mom) * gparam

        # change the update rule to match Hinton's dropout paper
        updates[gparam_mom] = mom * gparam_mom - (1. - mom) * learning_rate * gparam

    # ... and take a step along that direction
    for param, gparam_mom in zip(classifier.params, gparams_mom):
        # Misha Denil's original version
        #stepped_param = param - learning_rate * updates[gparam_mom]

        # since we have included learning_rate in gparam_mom, we don't need it
        # here
        stepped_param = param + updates[gparam_mom]

        # This is a silly hack to constrain the norms of the rows of the weight
        # matrices.  This just checks if there are two dimensions to the
        # parameter and constrains it if so... maybe this is a bit silly but it
        # should work for now.
        if param.get_value(borrow=True).ndim == 2:
            #squared_norms = T.sum(stepped_param**2, axis=1).reshape((stepped_param.shape[0],1))
            #scale = T.clip(T.sqrt(squared_filter_length_limit / squared_norms), 0., 1.)
            #updates[param] = stepped_param * scale

            # constrain the norms of the COLUMNs of the weight, according to
            # https://github.com/BVLC/caffe/issues/109
            col_norms = T.sqrt(T.sum(T.sqr(stepped_param), axis=0))
            desired_norms = T.clip(col_norms, 0, T.sqrt(squared_filter_length_limit))
            scale = desired_norms / (1e-7 + col_norms)
            updates[param] = stepped_param * scale
        else:
            updates[param] = stepped_param

    # Compile theano function for training.  This returns the training cost and
    # updates the model parameters.
    output = dropout_cost if dropout else cost
    train_model = theano.function(inputs=[epoch, index], outputs=output,
            updates=updates,
            givens={
                x: train_set_x[index * batch_size:(index + 1) * batch_size],
                y: train_set_y[index * batch_size:(index + 1) * batch_size]})
    #theano.printing.pydotprint(train_model, outfile="train_file.png",
    #        var_with_name_simple=True)

    # Theano function to decay the learning rate, this is separate from the
    # training function because we only want to do this once each epoch instead
    # of after each minibatch.
    decay_learning_rate = theano.function(inputs=[], outputs=learning_rate,
            updates={learning_rate: learning_rate * learning_rate_decay})

    ###############
    # TRAIN MODEL #
    ###############
    print '... training'

    best_params = None
    best_validation_errors = np.inf
    best_iter = 0
    test_score = 0.
    epoch_counter = 0
    import time
    start_time = time.clock()

    results_file = open(results_file_name, 'wb')
    X2=test_set_y.eval()
    X2 = unnormalize_data(X2, mins, ranges)
    X1=test_set_x.eval()
    X1 = unnormalize_data(X1, mins, ranges)
    last_reg = 10000.0
    while epoch_counter < training_epochs:
        # Train this epoch
        epoch_counter = epoch_counter + 1
        for minibatch_index in xrange(n_train_batches):
            minibatch_avg_cost = train_model(epoch_counter, minibatch_index)

        # Compute loss on validation set
        #validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
        #this_validation_errors = np.mean(validation_losses)

        ## Report and save progress.
        #print "epoch {}, test error {}, learning_rate={}{}".format(
                #epoch_counter, this_validation_errors,
                #learning_rate.get_value(borrow=True),
                #" **" if this_validation_errors < best_validation_errors else "")

        #best_validation_errors = min(best_validation_errors,
                #this_validation_errors)
        #results_file.write("{0}\n".format(this_validation_errors))
        #results_file.flush()

        new_learning_rate = decay_learning_rate()
        YH=test_fprop()
        YH = unnormalize_data(YH, mins, ranges)
        print epoch_counter
        print 'Regression ', melCD(X2[:,24*7:24*7+24],YH[:,24*7:24*7+24])#np.mean(np.mean((YH-X2)**2,1))
        print 'Baseline! ', melCD(X1[:,24*7:24*7+24],X2[:,24*7:24*7+24])#np.mean(np.mean((X1-X2)**2,1))
        #print 'Regression ', melCD(X2,YH)#np.mean(np.mean((YH-X2)**2,1))
        #print 'Baseline! ', melCD(X1,X2)#np.mean(np.mean((X1-X2)**2,1))
        if np.mean(np.mean((YH-X2)**2,1)) < last_reg:
            print 'This is better. Saving the model to ' + dataset+'.dnn.pkl'
            f = open(out_file,'w+')
            pickle.dump(classifier, f)
            f.flush()
            f.close()
            last_reg = np.mean(np.mean((YH-X2)**2,1))

    end_time = time.clock()
    print(('Optimization complete. Best validation score of %f %% '
           'obtained at iteration %i, with test performance %f %%') %
          (best_validation_errors * 100., best_iter, test_score * 100.))
    print ('The code for file ' +
                          os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((end_time - start_time) / 60.))

def model4_pre_from_siae(inp_file, midlayer, train_x, train_y): # 3.(A, Ba, Bb_backprop, C_backprop,SLT14),MCEP15
    f=open(inp_file,'r')
    sda=pickle.load(f)
    f.close()
    hidden_layers_sizes = [sda.dA_layers[0].n_visible]
    for i in range(len(sda.dA_layers)):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    for i in range(len(midlayer)):
        hidden_layers_sizes.append(midlayer[i])
    for i in range(len(sda.dA_layers)-1, -1, -1):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    hidden_layers_sizes.append(sda.dA_layers[0].n_visible)
    random_seed = 1234
    rng = np.random.RandomState(random_seed)
    x = T.matrix('x')  # the data is presented as rasterized images
    from dnn_dropout import MLP

    pretrained = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=[0.0]*len(hidden_layers_sizes),
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True
                     )
    for i in range(len(sda.dA_layers)):
        pretrained.layers[i].W = sda.dA_layers[i].W
        pretrained.layers[i].b = sda.dA_layers[i].b
    for i in range(len(sda.dA_layers)):
        pretrained.layers[len(sda.dA_layers)+len(midlayer)+i+1].W = sda.dA_layers[len(sda.dA_layers)-i-1].W_prime
        pretrained.layers[len(sda.dA_layers)+len(midlayer)+i+1].b = sda.dA_layers[len(sda.dA_layers)-i-1].b_prime
    middle_layer_size = [sda.dA_layers[-1].n_hidden]
    for i in range(len(midlayer)):
        middle_layer_size.append(midlayer[i])
    middle_layer_size.append(sda.dA_layers[-1].n_hidden)
    xhid= T.matrix('x')
    yhid= T.matrix('x')

    inbetween = MLP(rng=rng, input=xhid,
                     layer_sizes=middle_layer_size,
                     dropout_rates=[0.0]*len(middle_layer_size),
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True,
                     pretrained=None)

    encodex = theano.function(
                inputs=[],
                outputs=sda.dA_layers[-1].xhid,
                givens={
                    sda.dA_layers[0].x: train_x
                }
            )
    encodey = theano.function(
                inputs=[],
                outputs=sda.dA_layers[-1].xhid,
                givens={
                    sda.dA_layers[0].x: train_y
                }
            )
    xhid_value = encodex()
    yhid_value = encodey()
    xhid_value = theano.shared(xhid_value)
    yhid_value = theano.shared(yhid_value)

    cost=inbetween.mse(yhid)
    gparams = []
    for param in inbetween.params:
        # Use the right cost function here to train with or without dropout.
        gparam = T.grad(cost, param)
        gparams.append(gparam)

    updates = [
            (param, param - 0.1 * gparam)
            for param, gparam in zip(inbetween.params, gparams)
        ]

    train_model = theano.function(inputs=[], outputs=cost,
                                  updates=updates,
                                  givens={
                                      xhid: xhid_value,
                                      yhid: yhid_value})
    data_len = train_x.eval().shape[0]
    batch_size = 10
    index = T.lscalar()
    train_model = theano.function(inputs=[index], outputs=cost,
                                  updates=updates,
                                  givens={
                                      xhid: xhid_value[index * batch_size:(index + 1) * batch_size],
                                      yhid: yhid_value[index * batch_size:(index + 1) * batch_size]})
    for i in xrange(100):##$
        cs = 0.0
        for j in xrange(data_len/batch_size):
            cs += train_model(j)
        print i, cs
    for i in range(len(midlayer)+1):
        pretrained.layers[len(sda.dA_layers)+i].W = inbetween.layers[i].W
        pretrained.layers[len(sda.dA_layers)+i].b = inbetween.layers[i].b
    return pretrained

def model4_pre_from_speaker20(inp_file, midlayer=None, train_x=None, train_y=None): # 3.(A, Ba, Bb_backprop, C_backprop,SLT14),MCEP15
    f=open(inp_file,'r')
    pretrained=pickle.load(f)
    return pretrained

def model3_pre_from_siae(inp_file, midlayer, train_x, train_y): # 2.(A, Ba, Bb_backprop, C_backprop),MCEP15
    f=open(inp_file,'r')
    sda=pickle.load(f)
    f.close()
    hidden_layers_sizes = [sda.dA_layers[0].n_visible]
    for i in range(len(sda.dA_layers)):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    for i in range(len(midlayer)):
        hidden_layers_sizes.append(midlayer[i])
    for i in range(len(sda.dA_layers)-1, -1, -1):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    hidden_layers_sizes.append(sda.dA_layers[0].n_visible)
    random_seed = 1234
    rng = np.random.RandomState(random_seed)
    x = T.matrix('x')  # the data is presented as rasterized images
    from dnn_dropout import MLP

    pretrained = MLP(rng=rng, input=x,
                     layer_sizes=hidden_layers_sizes,
                     dropout_rates=[0.0]*len(hidden_layers_sizes),
                     activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                     use_bias=True
                     )
    for i in range(len(sda.dA_layers)):
        pretrained.layers[i].W = sda.dA_layers[i].W
        pretrained.layers[i].b = sda.dA_layers[i].b
    for i in range(len(sda.dA_layers)):
        pretrained.layers[len(sda.dA_layers)+len(midlayer)+i+1].W = sda.dA_layers[len(sda.dA_layers)-i-1].W_prime
        pretrained.layers[len(sda.dA_layers)+len(midlayer)+i+1].b = sda.dA_layers[len(sda.dA_layers)-i-1].b_prime
    middle_layer_size = [sda.dA_layers[-1].n_hidden]
    for i in range(len(midlayer)):
        middle_layer_size.append(midlayer[i])
    middle_layer_size.append(sda.dA_layers[-1].n_hidden)
    xhid= T.matrix('x')
    yhid= T.matrix('x')
    from dnn_dropout import HiddenLayer
    inbetween = HiddenLayer(rng=rng, input=xhid,
                     n_in=sda.dA_layers[-1].n_hidden,
                     n_out=sda.dA_layers[-1].n_hidden,
                     activation=CUR_ACIVATION_FUNCTION,
                     use_bias=True
                     )

    encodex = theano.function(
                inputs=[],
                outputs=sda.dA_layers[-1].xhid,
                givens={
                    sda.dA_layers[0].x: train_x
                }
            )
    encodey = theano.function(
                inputs=[],
                outputs=sda.dA_layers[-1].xhid,
                givens={
                    sda.dA_layers[0].x: train_y
                }
            )
    xhid_value = encodex()
    yhid_value = encodey()
    xhid_value = theano.shared(xhid_value)
    yhid_value = theano.shared(yhid_value)

    cost=inbetween.mse(yhid)
    gparams = []
    for param in inbetween.params:
        # Use the right cost function here to train with or without dropout.
        gparam = T.grad(cost, param)
        gparams.append(gparam)

    updates = [
            (param, param - 0.1 * gparam)
            for param, gparam in zip(inbetween.params, gparams)
        ]

    train_model = theano.function(inputs=[], outputs=cost,
                                  updates=updates,
                                  givens={
                                      xhid: xhid_value,
                                      yhid: yhid_value})
    data_len = train_x.eval().shape[0]
    batch_size = 10
    index = T.lscalar()
    train_model = theano.function(inputs=[index], outputs=cost,
                                  updates=updates,
                                  givens={
                                      xhid: xhid_value[index * batch_size:(index + 1) * batch_size],
                                      yhid: yhid_value[index * batch_size:(index + 1) * batch_size]})
    for i in xrange(100):##$
        cs = 0.0
        for j in xrange(data_len/batch_size):
            cs += train_model(j)
        print i, cs
    for i in range(len(midlayer)+1):
        pretrained.layers[len(sda.dA_layers)+i].W = inbetween.layers[i].W
        pretrained.layers[len(sda.dA_layers)+i].b = inbetween.layers[i].b
    return pretrained

def model3_pre_from_speaker20(inp_file, midlayer=None, train_x=None, train_y=None): # 2.(A, Ba, Bb_backprop, C_backprop),MCEP15
    f=open(inp_file,'r')
    pretrained=pickle.load(f)
    return pretrained

def model5_pre_from_siae(inp_file, midlayer, train_x, train_y): # 1,(A, Bb_joint, Bb_backprop, C_joint, C_backprop),MCEP15
    f=open(inp_file,'r')
    sda=pickle.load(f)
    f.close()
    x1 = T.matrix('x1')  # the data is presented as rasterized images
    x2 = T.matrix('x2')  # the data is presented as rasterized images
    cor_reg = T.scalar('cor_reg')
    rng = np.random.RandomState(123)
    from theano.tensor.shared_randomstreams import RandomStreams
    batch_size = 10

    theano_rng = RandomStreams(rng.randint(2 ** 30))
    from ae_joint import dA_joint
    da = dA_joint(
        numpy_rng=rng,
        theano_rng=theano_rng,
        input1=x1,
        input2=x2,
        cor_reg=0.2,
        n_visible1=24*15,
        n_visible2=24*15,
        n_hidden=sda.dA_layers[0].n_hidden,
        W1=theano.shared(sda.dA_layers[0].W.eval()),
        bhid1=theano.shared(sda.dA_layers[0].b.eval()),
        bvis1=theano.shared(sda.dA_layers[0].b_prime.eval()),
        W2=theano.shared(sda.dA_layers[0].W.eval()),
        bhid2=theano.shared(sda.dA_layers[0].b.eval()),
        bvis2=theano.shared(sda.dA_layers[0].b_prime.eval())
    )

    cost, updates = da.get_cost_updates(
        corruption_level=0.0,
        learning_rate=0.01
    )
    index = T.lscalar()    # index to a [mini]batch
    cor_reg_val = np.float32(1.0)
    train_da = theano.function(
        [index],
        cost,
        updates=updates,
        givens={
            x1: train_x[index * batch_size: (index + 1) * batch_size],
            x2: train_y[index * batch_size: (index + 1) * batch_size]
        }
    )
    fprop_x1 = theano.function(
               [],
               outputs=da.output1,
               givens={
                   x1: train_x
               },
               name='fprop_x1'
    )
    fprop_x2 = theano.function(
               [],
               outputs=da.output2,
               givens={
                   x2: train_y
               },
               name='fprop_x2'
    )
    fprop_x1t = theano.function(
               [],
               outputs=da.output1,
               givens={
                   x1: train_x
               },
               name='fprop_x1'
    )
    fprop_x2t = theano.function(
               [],
               outputs=da.output2,
               givens={
                   x2: train_y
               },
               name='fprop_x2'
    )
    rec_x1 = theano.function(
               [],
               outputs=da.rec1,
               givens={
                   x1: train_x
               },
               name='rec_x1'
    )
    rec_x2 = theano.function(
               [],
               outputs=da.rec2,
               givens={
                   x2: train_y
               },
               name='rec_x2'
    )
    fprop_x1_to_x2 = theano.function(
               [],
               outputs=da.reg,
               givens={
                   x1: train_x
               },
               name='fprop_x12x2'
    )

    updates_reg = [
            (da.cor_reg, da.cor_reg+theano.shared(np.float32(0.1)))
    ]
    update_reg = theano.function(
        [],
        updates=updates_reg
    )
    print 'initialize functions ended'

    import time
    start_time = time.clock()
    f=open('norm_male.pkl','r')
    mins=pickle.load(f)#[24*7:24*7+24]##$
    ranges=pickle.load(f)#[24*7:24*7+24]##$
    f.close()
    ############
    # TRAINING #
    ############
    print 'training started'
    X1=unnormalize_data(train_x.eval(), mins, ranges)
    X2=unnormalize_data(train_y.eval(), mins, ranges)
    n_train_batches = train_x.get_value(borrow=True).shape[0] / batch_size

    # go through training epochs
    for epoch in xrange(30):
        # go through trainng set
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        update_reg()

        X1H=rec_x1()
        X2H=rec_x2()

        #H1=fprop_x1()
        #H2=fprop_x2()

        X2MAP = fprop_x1_to_x2()
        #X2MAP=np.tanh(H1.dot(log_reg.W.eval())+log_reg.b.eval())
        #X2MAP=(X2MAP.dot(da.W2_prime.eval())+da.b2_prime.eval())

        X1H=unnormalize_data(X1H, mins, ranges)
        X2H=unnormalize_data(X2H, mins, ranges)
        X2MAP=unnormalize_data(X2MAP, mins, ranges)

        print 'Training epoch', epoch
        print 'Reconstruction', melCD(X1H[:,24*7:24*7+24], X1[:,24*7:24*7+24]), melCD(X2H[:,24*7:24*7+24], X2[:,24*7:24*7+24])
        print 'Regression', melCD(X2MAP[:,24*7:24*7+24], X2[:,24*7:24*7+24])
    f=open(inp_file.split('.')[0]+'.jda.pkl', 'w+')
    pickle.dump(da,f)
    f.flush()
    f.close()
    hidden_layers_sizes = [sda.dA_layers[0].n_visible]
    for i in range(len(sda.dA_layers)):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    for i in range(len(sda.dA_layers)-1, -1, -1):
        hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    hidden_layers_sizes.append(sda.dA_layers[0].n_visible)
    random_seed = 1234
    rng = np.random.RandomState(random_seed)
    x = T.matrix('x')  # the data is presented as rasterized images
    from dnn_dropout import MLP

    pretrained = MLP(rng=rng, input=x,
                 layer_sizes=hidden_layers_sizes,
                 dropout_rates=[0.0]*len(hidden_layers_sizes),
                 activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                 use_bias=True
                 )
    for i in range(len(sda.dA_layers)):
        pretrained.layers[i].W = theano.shared(da.W1.eval())
        pretrained.layers[i].b = theano.shared(da.b1.eval())
    for i in range(len(sda.dA_layers)):
        pretrained.layers[len(sda.dA_layers)+i].W = theano.shared(da.W2_prime.eval())
        pretrained.layers[len(sda.dA_layers)+i].b = theano.shared(da.b2_prime.eval())
    return pretrained

def model5_pre_from_speaker20(inp_file, midlayer=None, train_x=None, train_y=None): # 1,(A, Bb_joint, Bb_backprop, C_joint, C_backprop),MCEP15
    f=open(inp_file,'r')
    jda=pickle.load(f)
    f.close()
    x1 = T.matrix('x1')  # the data is presented as rasterized images
    x2 = T.matrix('x2')  # the data is presented as rasterized images
    cor_reg = T.scalar('cor_reg')
    rng = np.random.RandomState(123)
    from theano.tensor.shared_randomstreams import RandomStreams
    batch_size = 10

    theano_rng = RandomStreams(rng.randint(2 ** 30))
    from ae_joint import dA_joint
    da = dA_joint(
        numpy_rng=rng,
        theano_rng=theano_rng,
        input1=x1,
        input2=x2,
        cor_reg=0.2,
        n_visible1=24*15,
        n_visible2=24*15,
        n_hidden=jda.n_hidden,
        W1=theano.shared(jda.W1.eval()),
        bhid1=theano.shared(jda.b1.eval()),
        bvis1=theano.shared(jda.b1_prime.eval()),
        W2=theano.shared(jda.W2.eval()),
        bhid2=theano.shared(jda.b2.eval()),
        bvis2=theano.shared(jda.b2_prime.eval())
    )

    cost, updates = da.get_cost_updates(
        corruption_level=0.0,
        learning_rate=0.01
    )
    index = T.lscalar()    # index to a [mini]batch
    cor_reg_val = np.float32(1.0)
    train_da = theano.function(
        [index],
        cost,
        updates=updates,
        givens={
            x1: train_x[index * batch_size: (index + 1) * batch_size],
            x2: train_y[index * batch_size: (index + 1) * batch_size]
        }
    )
    fprop_x1 = theano.function(
               [],
               outputs=da.output1,
               givens={
                   x1: train_x
               },
               name='fprop_x1'
    )
    fprop_x2 = theano.function(
               [],
               outputs=da.output2,
               givens={
                   x2: train_y
               },
               name='fprop_x2'
    )
    fprop_x1t = theano.function(
               [],
               outputs=da.output1,
               givens={
                   x1: train_x
               },
               name='fprop_x1'
    )
    fprop_x2t = theano.function(
               [],
               outputs=da.output2,
               givens={
                   x2: train_y
               },
               name='fprop_x2'
    )
    rec_x1 = theano.function(
               [],
               outputs=da.rec1,
               givens={
                   x1: train_x
               },
               name='rec_x1'
    )
    rec_x2 = theano.function(
               [],
               outputs=da.rec2,
               givens={
                   x2: train_y
               },
               name='rec_x2'
    )
    fprop_x1_to_x2 = theano.function(
               [],
               outputs=da.reg,
               givens={
                   x1: train_x
               },
               name='fprop_x12x2'
    )

    updates_reg = [
            (da.cor_reg, da.cor_reg+theano.shared(np.float32(0.1)))
    ]
    update_reg = theano.function(
        [],
        updates=updates_reg
    )
    print 'initialize functions ended'

    import time
    start_time = time.clock()
    f=open('norm_male.pkl','r')
    mins=pickle.load(f)#[24*7:24*7+24]##$
    ranges=pickle.load(f)#[24*7:24*7+24]##$
    f.close()
    ############
    # TRAINING #
    ############
    print 'training started'
    X1=unnormalize_data(train_x.eval(), mins, ranges)
    X2=unnormalize_data(train_y.eval(), mins, ranges)
    n_train_batches = train_x.get_value(borrow=True).shape[0] / batch_size

    # go through training epochs
    for epoch in xrange(10):
        # go through trainng set
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))

        update_reg()

        X1H=rec_x1()
        X2H=rec_x2()

        #H1=fprop_x1()
        #H2=fprop_x2()

        X2MAP = fprop_x1_to_x2()
        #X2MAP=np.tanh(H1.dot(log_reg.W.eval())+log_reg.b.eval())
        #X2MAP=(X2MAP.dot(da.W2_prime.eval())+da.b2_prime.eval())

        X1H=unnormalize_data(X1H, mins, ranges)
        X2H=unnormalize_data(X2H, mins, ranges)
        X2MAP=unnormalize_data(X2MAP, mins, ranges)

        print 'Training epoch', epoch
        print 'Reconstruction', melCD(X1H[:,24*7:24*7+24], X1[:,24*7:24*7+24]), melCD(X2H[:,24*7:24*7+24], X2[:,24*7:24*7+24])
        print 'Regression', melCD(X2MAP[:,24*7:24*7+24], X2[:,24*7:24*7+24])

    hidden_layers_sizes = [jda.n_visible1]
    #for i in range(len(sda.dA_layers)):
        #hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    #for i in range(len(sda.dA_layers)-1, -1, -1):
        #hidden_layers_sizes.append(sda.dA_layers[i].n_hidden)
    hidden_layers_sizes.append(jda.n_hidden)
    hidden_layers_sizes.append(jda.n_visible2)
    random_seed = 1234
    rng = np.random.RandomState(random_seed)
    x = T.matrix('x')  # the data is presented as rasterized images
    from dnn_dropout import MLP

    pretrained = MLP(rng=rng, input=x,
                 layer_sizes=hidden_layers_sizes,
                 dropout_rates=[0.0]*len(hidden_layers_sizes),
                 activations=[CUR_ACIVATION_FUNCTION]*len(hidden_layers_sizes),
                 use_bias=True
                 )
    pretrained.layers[0].W = theano.shared(da.W1.eval())
    pretrained.layers[0].b = theano.shared(da.b1.eval())
    pretrained.layers[1].W = theano.shared(da.W2_prime.eval())
    pretrained.layers[1].b = theano.shared(da.b2_prime.eval())

    return pretrained

def model3_pre(): # 2.(A, Ba, Bb_backprop, C_backprop),MCEP15
    pass

def model4_pre(): # 3.(A, Ba, Bb_backprop, C_backprop,SLT14),MCEP15
    pass

def model5_pre(): # 1,(A, Bb_joint, Bb_backprop, C_joint, C_backprop),MCEP15
    pass

def model6_pre(): # 1,(A, Bb_joint, Bb_backprop, C_joint, C_backprop),MCEP
    pass

def test_wav(feature_type, order, delta, neighbours,
                 emphasis, frame_size, frame_rate):

    wpath = '../gitlab/voice-conversion/src/lib/arctic/cmu_us_rms_arctic/wav/arctic_a0001.wav'
    fx = FeatureExtractor(feature_type, order, delta, neighbours, emphasis, frame_size, frame_rate)

    cur_wav = read_wav(wpath)
    cur_gci = None
    data, ftr = fx.analyze(cur_wav, cur_gci)
    f=open('norm_male.pkl','r')
    mins=pickle.load(f)#[24*7:24*7+24]##$
    ranges=pickle.load(f)#[24*7:24*7+24]##$
    f.close()
    if 0: #ann
        f=open('dnn_male_1000_100.pkl','r')
        dnn=pickle.load(f)
        f.close()
        x = T.matrix('x')  # the data is presented as rasterized images
        val = normalize_data(data.value,mins,ranges)
        test_set_x=theano.shared(val.astype(np.float32))
        test_fprop = theano.function(inputs=[],
                                     outputs=dnn.layers[-1].output,
                                     givens={
                                         dnn.layers[0].input: test_set_x
                                         })
        mapped=test_fprop()
        mapped=unnormalize_data(mapped,mins,ranges)
    elif 1: #gmm
        f=open('jdgmm1002_male.pkl')
        model=pickle.load(f)
        f.close()
        val = normalize_data(data.value[:,24*7:24*7+24],mins[24*7:24*7+24],ranges[24*7:24*7+24])
        mapped1=model.map(val)
        mapped1=unnormalize_data(mapped1,mins,ranges)
        mapped = data.value.copy()
        mapped[:,24*7:24*7+24] = mapped1

    ftr['pit'].value[ftr['pit'].value!=0] = np.linspace(140.0, 100.0, ftr['pit'].value[ftr['pit'].value!=0].shape[0])
    new_wav=fx.synthesize(ftr, mapped)

    return

def experiment():
    if 1: # test
        feature_type='MCEP'
        order=24
        delta=False
        neighbours=7
        emphasis=0.9##
        frame_size= 0.020
        frame_rate=0.010
        test_wav(feature_type=feature_type, order=order, delta=delta, neighbours=neighbours,
	             emphasis=emphasis, frame_size= frame_size, frame_rate=frame_rate)
    hidden_size = 1000
    sentence_num = 100
    ae_name = 'ae_'+str(hidden_size)+'_linear_male.pkl'
    if 0: # train all TIMIT AE
        ae_all(ae_name, hidden_layers_sizes=[1000,500],
               corruption_levels=[0.1,0.1],
               pretrain_lr=0.1,
               batch_size=20,
               training_epochs=10)
    if 1: # load norm
        f=open('norm_male.pkl','r')
        mins=pickle.load(f)#[24*7:24*7+24]##$
        ranges=pickle.load(f)#[24*7:24*7+24]##$
        f.close()
    if 1: # load xy20
        x20, y20, xv20, yv20, xt20, yt20 = load_xy('../rms2mdl_pre.npy', num_sentences=200, mins=mins, ranges=ranges)
    if 1: # load xy
        x, y, xv, yv, xt, yt = load_xy('../rms2mdl.npy', num_sentences=sentence_num, mins=mins, ranges=ranges)

    model0_pre(x, y, xv, yv, xt, yt, mins, ranges,
               hidden_layers_sizes=[x.eval().shape[1], 100, y.eval().shape[1]],
               corruption_levels=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
               lr=0.1,
               batch_size=10,
               training_epochs=100)
    if 0:
        dnn_train(model5_pre_from_siae, ae_name, 'dnn_male_'+str(hidden_size)+'_spk20.pkl',
                  x20, y20, xv20, yv20, xt20, yt20, mins, ranges,
                  hidden_layers_sizes=[x20.eval().shape[1], 1000, y20.eval().shape[1]],
                  middle_layers_sizes=[100],
                  corruption_levels=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                  lr=0.01,
                  batch_size=10,
                  training_epochs=100)

    if 1:
        dnn_train(model5_pre_from_speaker20, ae_name.split('.')[0]+'.jda.pkl', 'dnn_male_'+str(hidden_size)+'_'+str(sentence_num)+'.pkl', x, y, xv, yv, xt, yt, mins, ranges,
                  hidden_layers_sizes=[x.eval().shape[1], 1000, y.eval().shape[1]],
                  middle_layers_sizes=[100],
                  corruption_levels=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                  lr=0.1,
                  batch_size=10,
                  training_epochs=100)

if __name__ == "__main__":
    vcpath = '../gitlab/voice-conversion/src/'
    import sys
    sys.path.append(vcpath)
    from vc import *
    experiment()

max_stars_repo_name: ashmanmode/AutoencoderTTS

max_issues_repo_name: ashmanmode/AutoencoderTTS

max_forks_repo_name: ashmanmode/AutoencoderTTS

reason: import theano,from theano


text:
#!/usr/bin/env python
"""
Unit tests for skymap
"""
__author__ = "Alex Drlica-Wagner"

import sys
import unittest

# Careful, have a local .matplotlibrc
import matplotlib  # noqa F401

import pylab as plt
import numpy as np
import healpy as hp

import cartosky
from cartosky import Skymap, McBrydeSkymap, OrthoSkymap
from cartosky import SurveySkymap, SurveyMcBryde, SurveyOrtho
from cartosky import DESSkymap, BlissSkymap

try:
    import healsparse as hsp
    hsp_avail = True
except ImportError:
    hsp_avail = False

nside = 8

SKYMAPS = [Skymap, McBrydeSkymap, OrthoSkymap]
SURVEYS = [SurveySkymap, SurveyMcBryde, SurveyOrtho]
ZOOMS = [DESSkymap, BlissSkymap]

class TestSkymap(unittest.TestCase):

    def test_skymap(self):
        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SKYMAPS):
            plt.sca(axes[i])
            m = cls()
            m.draw_milky_way()
            plt.title('Galactic Plane (%s)'%cls.__name__)

    def test_survey_skymap(self):
        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SURVEYS):
            plt.sca(axes[i])
            m = cls()
            m.draw_des()
            m.draw_maglites()
            m.draw_bliss()
            plt.title('Footprints (%s)'%cls.__name__)

    def test_zoom_skymap(self):
        for i, cls in enumerate(ZOOMS):
            plt.figure()
            m = cls()
            m.draw_des()
            m.draw_maglites()
            m.draw_bliss()
            plt.suptitle('Zoom Footprints (%s)'%cls.__name__)

    def test_draw_hpxmap(self):
        """ Test drawing a full healpix skymap """
        hpxmap = np.arange(hp.nside2npix(nside))
        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SKYMAPS):
            plt.sca(axes[i])
            m = cls()
            m.draw_hpxmap(hpxmap, xsize=400)
            plt.title('HEALPix Map (%s)'%cls.__name__)

    def test_draw_explicit_hpxmap(self):
        """ Test an explicit healpix map """
        pix = hpxmap = np.arange(525, 535)
        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SKYMAPS):
            plt.sca(axes[i])
            m = cls()
            m.draw_hpxmap(hpxmap, pix, nside, xsize=400)
            plt.title('Partial HEALPix Map (%s)'%cls.__name__)

    @unittest.skipUnless(hsp_avail, "Skip this routine unless healsparse is installed")
    def test_draw_hspmap(self):
        """ Test drawing a HealSparse map """
        nside_sparse = 4096
        nside_coverage = 64
        hspmap = hsp.HealSparseMap.make_empty(nside_coverage, nside_sparse, dtype=np.int64, sentinel=0)
        circ = hsp.Circle(ra=0, dec=0, radius=2.0, value=10)
        hsp.geom.realize_geom([circ], hspmap)
        m = cartosky.Skymap(projection='cyl')
        llcrnrlon, urcrnrlon = 3.0, -3.0
        llcrnrlat, urcrnrlat = -3, 3.0
        m.ax.set_extent([llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat])
        m.draw_hspmap(hspmap)
        plt.title('HEALPix Zoom (nside=%i)'%nside_sparse)

    def test_draw_hpxbin(self):
        """ Test drawing hpxbin from points """
        kwargs = dict(nside=64)
        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SKYMAPS):
            plt.sca(axes[i])
            m = cls()
            # This is not uniform
            size = int(1e6)
            lon = np.random.uniform(0, 360, size=size)
            lat = np.random.uniform(-90, 90, size=size)
            m.draw_hpxbin(lon, lat, **kwargs)
            plt.title('HEALPix Bin (%s)'%cls.__name__)

    def test_draw_hires(self):
        """ Draw a partial healpix map with very large nside """
        nside = 4096*2**5
        ra, dec = 45, -45
        radius = 0.05
        pixels = cartosky.healpix.ang2disc(nside, ra, dec, radius)
        values = pixels

        plt.figure()
        m = Skymap(projection='cyl', celestial=False, gridlines=False)

        llcrnrlon, urcrnrlon = ra + 2*radius, ra - 2*radius,
        llcrnrlat, urcrnrlat = dec - 2*radius, dec + 2*radius
        m.ax.set_extent([llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat])

        m.draw_hpxmap(values, pixels, nside=nside, xsize=400)
        # Draw the grid lines
        draw_labels = True
        xlocs = np.linspace(llcrnrlon, urcrnrlon, 5)
        ylocs = np.linspace(llcrnrlat, urcrnrlat, 5)
        _ = m.ax.gridlines(draw_labels=draw_labels,
                           xlocs=xlocs, ylocs=ylocs,
                           linestyle=':')

        # ADW: This doesn't work (removes y-labels)...
        # m.ax.invert_xaxis()

        plt.title('HEALPix Zoom (nside=%i)'%nside)

    def test_draw_focal_planes(self):
        """ Draw a DECam focal planes """
        ra, dec = 45, -45
        radius = 1.5
        delta = 1.0

        plt.figure()
        m = Skymap(projection='cyl', celestial=False, gridlines=False)
        llcrnrlon, urcrnrlon = ra + 2*radius, ra - 2*radius
        llcrnrlat, urcrnrlat = dec - 2*radius, dec + 2*radius
        m.ax.set_extent([llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat])

        # Can plot individual fields
        m.draw_focal_planes([ra + delta/2], [dec - delta/2], color='g')
        # Or as arrays
        m.draw_focal_planes([ra, ra - delta, ra - delta],
                            [dec, dec + delta, dec - delta],
                            color='r')

        m.tissot(ra, dec, 1.1, ec='g', fc='none')

        # Draw the grid lines
        draw_labels = True
        xlocs = np.linspace(llcrnrlon, urcrnrlon, 5)
        ylocs = np.linspace(llcrnrlat, urcrnrlat, 5)
        m.ax.gridlines(draw_labels=draw_labels,
                       xlocs=xlocs, ylocs=ylocs,
                       linestyle=':')

        # ADW: This doesn't work (removes y-labels)...
        # m.ax.invert_xaxis()

        plt.title('DECam Focal Planes')

    def test_zoom_to_fit(self):
        nside = 64
        ra, dec = 15, -45
        radius = 10.0
        pixels = cartosky.healpix.ang2disc(nside, ra, dec, radius)
        values = pixels

        fig, axes = plt.subplots(1, 3, figsize=(12, 3))
        for i, cls in enumerate(SKYMAPS):
            plt.sca(axes[i])
            m = cls(gridlines=False)
            m.draw_hpxmap(values, pixels, nside=nside, xsize=200)
            m.zoom_to_fit(values, pixels, nside)
            draw_labels = i == 0
            xlocs = np.linspace(ra - 2*radius, ra + 2*radius, 5)
            ylocs = np.linspace(dec - 2*radius, dec + 2*radius, 5)
            m.ax.gridlines(draw_labels=draw_labels,
                           xlocs=xlocs, ylocs=ylocs,
                           linestyle=':')

            # m.draw_parallels(np.linspace(dec-2*radius,dec+2*radius,5),
            #                  labelstyle='+/-',labels=[1,0,0,0])
            # m.draw_meridians(np.linspace(ra-2*radius,ra+2*radius,5),
            #                  labelstyle='+/-',labels=[0,0,0,1])
            plt.title('Zoom to Fit (%s)'%cls.__name__)

if __name__ == '__main__':
    if sys.flags.interactive:
        plt.ion()
    unittest.main()

max_stars_repo_name: erykoff/cartosky

max_issues_repo_name: erykoff/cartosky

max_forks_repo_name: erykoff/cartosky