import os
import cv2
import sys

sys.path.append('..')
import numpy as np
from math import cos, sin
# from moviepy.editor import *
from lib.FSANET_model import *
import numpy as np
from keras.layers import Average

# from moviepy.editor import *
# from mtcnn.mtcnn import MTCNN

def draw_axis(img, yaw, pitch, roll, tdx=None, tdy=None, size=50):
    print(yaw, roll, pitch)
    pitch = pitch * np.pi / 180
    yaw = -(yaw * np.pi / 180)
    roll = roll * np.pi / 180

    if tdx != None and tdy != None:
        tdx = tdx
        tdy = tdy
    else:
        height, width = img.shape[:2]
        tdx = width / 2
        tdy = height / 2

    # X-Axis pointing to right. drawn in red
    x1 = size * (cos(yaw) * cos(roll)) + tdx
    y1 = size * (cos(pitch) * sin(roll) + cos(roll) * sin(pitch) * sin(yaw)) + tdy

    # Y-Axis | drawn in green
    #        v
    x2 = size * (-cos(yaw) * sin(roll)) + tdx
    y2 = size * (cos(pitch) * cos(roll) - sin(pitch) * sin(yaw) * sin(roll)) + tdy

    # Z-Axis (out of the screen) drawn in blue
    x3 = size * (sin(yaw)) + tdx
    y3 = size * (-cos(yaw) * sin(pitch)) + tdy

    cv2.line(img, (int(tdx), int(tdy)), (int(x1), int(y1)), (0, 0, 255), 3)
    cv2.line(img, (int(tdx), int(tdy)), (int(x2), int(y2)), (0, 255, 0), 3)
    cv2.line(img, (int(tdx), int(tdy)), (int(x3), int(y3)), (255, 0, 0), 2)

    return img

def draw_results_ssd(detected, input_img, faces, ad, img_size, img_w, img_h, model, time_detection, time_network,
                     time_plot):
    # loop over the detections
    if detected.shape[2] > 0:
        for i in range(0, detected.shape[2]):
            # extract the confidence (i.e., probability) associated with the
            # prediction
            confidence = detected[0, 0, i, 2]

            # filter out weak detections
            if confidence > 0.5:
                # compute the (x, y)-coordinates of the bounding box for
                # the face and extract the face ROI
                (h0, w0) = input_img.shape[:2]
                box = detected[0, 0, i, 3:7] * np.array([w0, h0, w0, h0])
                (startX, startY, endX, endY) = box.astype("int")
                # print((startX, startY, endX, endY))
                x1 = startX
                y1 = startY
                w = endX - startX
                h = endY - startY

                x2 = x1 + w
                y2 = y1 + h

                xw1 = max(int(x1 - ad * w), 0)
                yw1 = max(int(y1 - ad * h), 0)
                xw2 = min(int(x2 + ad * w), img_w - 1)
                yw2 = min(int(y2 + ad * h), img_h - 1)

                faces[i, :, :, :] = cv2.resize(input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
                faces[i, :, :, :] = cv2.normalize(faces[i, :, :, :], None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)

                face = np.expand_dims(faces[i, :, :, :], axis=0)
                p_result = model.predict(face)

                face = face.squeeze()

                # print(p_result[0][0], p_result[0][1], p_result[0][2])

                # img = draw_axis(input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], p_result[0][0], p_result[0][1], p_result[0][2])

                # input_img[yw1:yw2 + 1, xw1:xw2 + 1, :] = img
                break

    # cv2.imshow("result", input_img)
    try:
        return input_img, [p_result[0][0], p_result[0][1], p_result[0][2]]  # ,time_network,time_plot
    except:
        return

class FSAnet:
    # def __init__(self, input_img, nums):
    #     self.input_img = input_img
    #     self.nums = nums

    try:
        os.mkdir('./img')
    except OSError:
        pass

    # face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface_improved.xml')
    # detector = MTCNN()

    # load model and weights
    img_size = 64
    stage_num = [3, 3, 3]
    lambda_local = 1
    lambda_d = 1
    img_idx = 0
    detected = ''  # make this not local variable
    time_detection = 0
    time_network = 0
    time_plot = 0
    skip_frame = 1  # every 5 frame do 1 detection and network forward propagation
    ad = 0.6

    # Parameters
    num_capsule = 3
    dim_capsule = 16
    routings = 2
    stage_num = [3, 3, 3]
    lambda_d = 1
    num_classes = 3
    image_size = 64
    num_primcaps = 7 * 3
    m_dim = 5
    S_set = [num_capsule, dim_capsule, routings, num_primcaps, m_dim]

    model1 = FSA_net_Capsule(image_size, num_classes, stage_num, lambda_d, S_set)()
    model2 = FSA_net_Var_Capsule(image_size, num_classes, stage_num, lambda_d, S_set)()

    num_primcaps = 8 * 8 * 3
    S_set = [num_capsule, dim_capsule, routings, num_primcaps, m_dim]

    model3 = FSA_net_noS_Capsule(image_size, num_classes, stage_num, lambda_d, S_set)()

    print('Loading models ...')

    weight_file1 = '../pre-trained/300W_LP_models/fsanet_capsule_3_16_2_21_5/fsanet_capsule_3_16_2_21_5.h5'
    model1.load_weights(weight_file1)
    print('Finished loading model 1.')

    weight_file2 = '../pre-trained/300W_LP_models/fsanet_var_capsule_3_16_2_21_5/fsanet_var_capsule_3_16_2_21_5.h5'
    model2.load_weights(weight_file2)
    print('Finished loading model 2.')

    weight_file3 = '../pre-trained/300W_LP_models/fsanet_noS_capsule_3_16_2_192_5/fsanet_noS_capsule_3_16_2_192_5.h5'
    model3.load_weights(weight_file3)
    print('Finished loading model 3.')

    inputs = Input(shape=(64, 64, 3))
    x1 = model1(inputs)  # 1x1
    x2 = model2(inputs)  # var
    x3 = model3(inputs)  # w/o
    avg_model = Average()([x1, x2, x3])
    model = Model(inputs=inputs, outputs=avg_model)

    # load our serialized face detector from disk
    print("[INFO] loading face detector...")
    protoPath = os.path.sep.join(["face_detector", "deploy.prototxt"])
    modelPath = os.path.sep.join(["face_detector",
                                  "res10_300x300_ssd_iter_140000.caffemodel"])
    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    print('Start detecting pose ...')
    detected_pre = np.empty((1, 1, 1))

    def calculate(self, input_img, number=0):

        skip_frame = self.skip_frame
        net = self.net
        detected_pre = self.detected_pre

        img_idx = number
        img_h, img_w, _ = np.shape(input_img)

        if img_idx == 1 or img_idx % skip_frame == 0:
            time_detection = 0
            time_network = 0
            time_plot = 0

            # detect faces using LBP detector
            gray_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
            # detected = face_cascade.detectMultiScale(gray_img, 1.1)
            # detected = detector.detect_faces(input_img)
            # pass the blob through the network and obtain the detections and
            # predictions
            blob = cv2.dnn.blobFromImage(cv2.resize(input_img, (300, 300)), 1.0,
                                         (300, 300), (104.0, 177.0, 123.0))
            net.setInput(blob)
            detected = net.forward()

            if detected_pre.shape[2] > 0 and detected.shape[2] == 0:
                detected = detected_pre

            faces = np.empty((detected.shape[2], self.img_size, self.img_size, 3))
            try:

                input_img, eular_angles = draw_results_ssd(detected, input_img, faces, self.ad, self.img_size, img_w, img_h, self.model,
                                             time_detection,
                                             time_network, time_plot)
            except:
                return

            # cv2.imwrite('img/' + str(img_idx) + '.png', input_img)

        else:
            input_img, eular_angles = draw_results_ssd(self.detected, input_img, self.faces, self.ad, self.img_size, img_w, img_h,
                                         self.model, self.time_detection,
                                         self.time_network, self.time_plot)

        if detected.shape[2] > detected_pre.shape[2] or img_idx % (skip_frame * 3) == 0:
            detected_pre = detected

        key = cv2.waitKey(1)
        return eular_angles

# -------------------------------- Mitchell: for single image head pose estimation -----------------------------
if __name__ == '__main__':
    dataset = sys.argv[1]
    method = sys.argv[2]

    flag = 0
    if dataset == "forensics":
        flag = 1

    # input_img = cv2.imread('/home/xiangmingcan/notespace/cvpr_data/celeba/11663.jpg')
    src = "/home/xiangmingcan/notespace/cvpr_data/" + dataset
    tgt = "/home/xiangmingcan/notespace/cvpr_result/" + dataset + '/' + method

    save_log = os.path.join("headPose/", dataset, method + ".txt")

    path = os.path.join("headPose/", dataset)
    if not os.path.exists(path):
        os.makedirs(path)

    logFile = open(save_log, 'w')

    img_list = os.listdir(tgt)
    sorted(img_list)

    fsanet = FSAnet()

    for input_img in img_list:
        if '_mask' in input_img:
            continue

        try:
            refer_img = input_img.split('-')[1]
            if flag:
                refer_img = refer_img[:-3] + "png"

        except:
            print("img1_name = img2_name.split('-')[1]")
            print("IndexError: list index out of range")
            continue

        refer_img_folder = refer_img.split('_')[0]
        refer_img = refer_img.split(refer_img_folder)[1][1:]

        result_cv2 = cv2.imread(os.path.join(tgt, input_img))
        refer_cv2 = cv2.imread(os.path.join(src, refer_img_folder, refer_img))

        # 如果读出来的img不存在，则删去
        if refer_cv2 is None or refer_cv2 is None:
            continue

        # result image 的欧拉角
        eular_angles_result = fsanet.calculate(result_cv2)
        print(eular_angles_result)

        # reference image 的欧拉角
        eular_angles_refer = fsanet.calculate(refer_cv2)
        print(eular_angles_refer)

        if eular_angles_result is None or eular_angles_refer is None:
            continue

        vec1 = np.array(eular_angles_result)
        vec2 = np.array(eular_angles_refer)

        distance = np.linalg.norm(vec1 - vec2)
        print(distance)
        print('\n')

        logFile.write(str(distance))
        logFile.write('\n')

    logFile.close()


from pprint import pprint
from gensim.models import KeyedVectors

MODEL_PATH = './tmp/GoogleNews-vectors-negative300.bin.gz'
MODEL = KeyedVectors.load_word2vec_format(fname=MODEL_PATH, binary=True)

with open('./combined.csv', 'r') as f:
    lines = f.readlines()[1:]

x = []

for line in lines:
    words = [s.strip() for s in line.split(',')]
    words.append(MODEL.similarity(words[0], words[1]))
    x.append(words)

pprint(x)


# coding=utf-8
import sys
import getopt
import logging
import importlib
import threading

lock = threading.Lock()
inited = set([])

class TaskManager(object):

    def __init__(self):
        self.task_dict = {}

    def run_task(self, argv):
        if len(argv) == 0:
            print(u'请输入-r 或者--run，后面接需要运行的任务。查询可执行的任务清单，请输入-h')
            return
        try:
            opts, args = getopt.getopt(argv, 'r:h', ['run=','help'])
        except getopt.GetoptError as err:
            print(u'请输入-r 或者--run，后面接需要运行的任务。查询可执行的任务清单，请输入-h')
            sys.exit(2)
        task_name = ''
        task_params = args or []
        for opt, arg in opts:
            if opt in ('-r', '--run'):
                task_name = arg
            elif opt in ('-h', '--help'):
                self.print_task_list()
                return
        self._run_task(task_name, task_params)

    def register_task(self, name, module, params=None, description=''):
        """ 注册任务 """
        if name in self.task_dict:
            raise u'任务名【%s】已存在' % name
        else:
            self.task_dict[name] = (module, params, description)

    def print_task_list(self):
        i = 1
        for t_name in self.task_dict:
            m, p, d = self.task_dict.get(t_name)
            print('%d. -r %s %s => %s' % (i, t_name, ' '.join(p or []), d))
            i += 1

    def _run_task(self, name, params=[]):
        """ 执行任务 """
        module, param_names, des = self.task_dict.get(name)
        if module is not None:
            logging.info(u'开始执行任务%s，输入参数：%s' % (name, ','.join(params)))
            m = importlib.import_module(module)
            f_list = dir(m)
            if 'start' in f_list:
                m.start(*params)
                logging.info(u'任务%s执行完成' % name)
            else:
                logging.warning(u'模板%s不包含可执行方法' % module)

TASK_MANAGER = TaskManager()


"""
Given a random set of numbers, Print them in sorted order.

Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.

Output:
Print each sorted array in a seperate line. For each array its numbers should be seperated by space.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
1 ≤A[i]<100

Example:
Input:
1
2
3 1

Output:
1 3
"""

def sort_the_array(arr, n):
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if arr[min_idx] > arr[j]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return ' '.join(str(x) for x in arr)

if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        size = int(input())
        arr = [int(i) for i in input().split()][0:size]
        print(sort_the_array(arr, size))


import tensorflow as tf
import numpy as np
import os
import argparse
from PIL import Image
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

DEFAULT_MODEL_PATH = "./exported-models/my_model/saved_model"
DEFAULT_LABEL_MAP_PATH = "./annotations/label_map.pbtxt"
DEFAULT_IMAGE_INPUT_PATH = "./images/input"
DEFAULT_IMAGE_OUTPUT_PATH = "./images/output"

def inference(inputPath, modelPath, labelPath, outputPath):
    print("Loading model... ", end='')

    # Load saved model and build the detection function
    detect_fn = tf.saved_model.load(modelPath)

    # Loading the label_map
    category_index = label_map_util.create_category_index_from_labelmap(labelPath, use_display_name=True)

    print("Done!")

    def load_image_into_numpy_array(path):
        # Load an image from file into a numpy array.
        return np.array(Image.open(path))

    def predict(inputPath, imageName, modelPath, labelPath, outputPath):
        print("Running inference for {}... ".format(os.path.join(inputPath, imageName)), end='')
        image_np = load_image_into_numpy_array(os.path.join(inputPath, imageName))

        # The input needs to be a tensor, convert it using `tf.convert_to_tensor`
        input_tensor = tf.convert_to_tensor(image_np)
        # The model expects a batch of images, so add an axis with `tf.newaxis`
        input_tensor = input_tensor[tf.newaxis, ...]

        # input_tensor = np.expand_dims(image_np, 0)
        try:
            detections = detect_fn(input_tensor)
        except ValueError:
            print("Oops!")
            return

        # Convert to numpy arrays, and take index [0] to remove the batch dimension
        num_detections = int(detections.pop('num_detections'))
        detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
        detections['num_detections'] = num_detections

        # Detection_classes should be ints
        detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

        image_np_with_detections = image_np.copy()

        viz_utils.visualize_boxes_and_labels_on_image_array(
            image_np_with_detections,
            detections['detection_boxes'],
            detections['detection_classes'],
            detections['detection_scores'],
            category_index,
            use_normalized_coordinates=True,
            max_boxes_to_draw=1,
            min_score_thresh=.08,
            agnostic_mode=False)

        image = Image.fromarray(image_np_with_detections)
        image.save(os.path.join(outputPath, imageName))
        print("Done!")

    if os.path.isdir(inputPath):
        for imageName in [i for i in os.listdir(inputPath) if os.path.isfile(os.path.join(inputPath, i))]:
            predict(inputPath, imageName, modelPath, labelPath, outputPath)
    else:
        predict("", inputPath, modelPath, labelPath, outputPath)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Let's find Waldo!")
    parser.add_argument("--input", type=str, nargs="?", default=DEFAULT_IMAGE_INPUT_PATH)
    parser.add_argument("--model", type=str, nargs="?", default=DEFAULT_MODEL_PATH)
    parser.add_argument("--labels", type=str, nargs="?", default=DEFAULT_LABEL_MAP_PATH)
    parser.add_argument("--output", type=str, nargs="?", default=DEFAULT_IMAGE_OUTPUT_PATH)
    args = parser.parse_args()
    inference(inputPath=args.input, modelPath=args.model, labelPath=args.labels, outputPath=args.output)


# Copyright (C) 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities for Python Fire's tests."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib
import re
import sys
import unittest

from fire import core
from fire import trace

import mock
import six

class BaseTestCase(unittest.TestCase):
  """Shared test case for Python Fire tests."""

  @contextlib.contextmanager
  def assertStdoutMatches(self, regexp):
    """Asserts that the context generates stdout matching regexp."""
    stdout = six.StringIO()
    with mock.patch.object(sys, 'stdout', stdout):
      yield
    value = stdout.getvalue()
    if not re.search(regexp, value, re.DOTALL | re.MULTILINE):
      raise AssertionError('Expected %r to match %r' % (value, regexp))

  @contextlib.contextmanager
  def assertRaisesFireExit(self, code, regexp=None):
    """Asserts that a FireExit error is raised in the context.

    Allows tests to check that Fire's wrapper around SystemExit is raised
    and that a regexp is matched in the output.

    Args:
      code: The status code that the FireExit should contain.
      regexp: stdout must match this regex.
    Yields:
      Yields to the wrapped context.
    """
    if regexp is None:
      regexp = '.*'
    with self.assertRaises(core.FireExit):
      stdout = six.StringIO()
      with mock.patch.object(sys, 'stdout', stdout):
        try:
          yield
        except core.FireExit as exc:
          if exc.code != code:
            raise AssertionError('Incorrect exit code: %r != %r' % (exc.code,
                                                                    code))
          self.assertIsInstance(exc.trace, trace.FireTrace)
          stdout.flush()
          stdout.seek(0)
          value = stdout.getvalue()
          if not re.search(regexp, value, re.DOTALL | re.MULTILINE):
            raise AssertionError('Expected %r to match %r' % (value, regexp))
          raise

def main():
  unittest.main()


import os
import math

import cv2
import numpy as np

from utils import *
from utils_camera import *

def depthimg_bilateral(depthImg, maxdiff=3.0):
    """ Smooth depth image using a bilateral filter ignoring large differences """
    depthImg_bilateral = cv2.bilateralFilter(
        np.float32(depthImg), 15, 20.0, 20.0)
    h, w = depthImg.shape
    finalDepth = np.zeros([h, w])
    for y in range(0, h):
        for x in range(0, w):
            cur = depthImg[y, x]
            smo = depthImg_bilateral[y, x]
            if abs(smo - cur) < maxdiff:
                finalDepth[y, x] = smo
            else:
                finalDepth[y, x] = cur
    return finalDepth

def read_ColorImage(strFilePath, imgResize=False, resizeSize=(240, 320)):
    """ Given a path to an image, read it into a numpy array. Resize as required. """
    # clrImg = misc.imread(strFilePath)
    # if imgResize: clrImg = misc.imresize(clrImg, resizeSize, interp='bicubic')
    clrImg = cv2.imread(strFilePath)  # any old color image loading
    if imgResize:
        clrImg = cv2.resize(
            clrImg,
            (resizeSize[1],
             resizeSize[0]),
            interpolation=cv2.INTER_AREA)
    return clrImg

def read_depth16bit_image(strFilePath, imgResize=False, resizeSize=(240, 320)):
    """ Read the a 16bit depth image data into a numpy array. """
    depthImg = cv2.imread(strFilePath, -1)  # regular 16-bit PNG loading
    if imgResize:
        depthImg = cv2.resize(
            depthImg,
            (resizeSize[1],
             resizeSize[0]),
            interpolation=cv2.INTER_NEAREST)
    return depthImg

def image_fusion(
        camera_params,
        depthData,
        clrImg=None,
        normals=None,
        threshold=False):
    """
        Given a depth image and its corresponding color image, return a colored point cloud as a vector of (x, y, z, r, g, b).
        Assume only depth and color, and if provided with normals, fuse those too.
        The output format is a PLY (required to view it in color in MeshLab).
    """
    # nanLocationsDepth = np.isnan(depthData)
    # numberOfVertices = depthData.size - np.count_nonzero(nanLocationsDepth)
    # depthData[nanLocationsDepth] = -1 # replace all NaN values with -1

    bHasColors = clrImg is not None
    bHasNormals = normals is not None
    if bHasNormals:
        nanLocationsNormals = np.isnan(normals)
        normals[nanLocationsNormals] = 0

    h, w = depthData.shape

    # generate point cloud via numpy array functions
    coords = np.indices((h, w))

    # geometry
    xcoords = (((coords[1] - camera_params.cx) /
                camera_params.fx) * depthData).flatten()
    ycoords = (((coords[0] - camera_params.cy) /
                camera_params.fy) * depthData).flatten()
    zcoords = depthData.flatten()

    # color
    chan_red = chan_blue = chan_green = None
    if bHasColors:
        # chan_red = clrImg[..., 0].flatten()
        # chan_blue = clrImg[..., 1].flatten()
        # chan_green = clrImg[..., 2].flatten()

        chan_red = clrImg[..., 2].flatten()
        chan_blue = clrImg[..., 1].flatten()
        chan_green = clrImg[..., 0].flatten()

    ptcloud = None

    # normals
    normalsX = normalsY = normalsZ = None
    if bHasNormals:
        normalsX = normals[..., 0].flatten()
        normalsY = normals[..., 1].flatten()
        normalsZ = normals[..., 2].flatten()

    if bHasColors and bHasNormals:
        ptcloud = np.dstack(
            (xcoords,
             ycoords,
             zcoords,
             normalsX,
             normalsY,
             normalsZ,
             chan_red,
             chan_blue,
             chan_green))[0]
    elif bHasColors and not bHasNormals:
        ptcloud = np.dstack(
            (xcoords,
             ycoords,
             zcoords,
             chan_red,
             chan_blue,
             chan_green))[0]
    elif not bHasColors and bHasNormals:
        ptcloud = np.dstack(
            (xcoords,
             ycoords,
             zcoords,
             normalsX,
             normalsY,
             normalsZ))[0]
    else:
        ptcloud = np.dstack((xcoords, ycoords, zcoords))[0]
    # depth filtering
    if threshold:
        filters = np.where(ptcloud[:, 2] >= 2800)
        ptcloud = np.delete(ptcloud[:], filters, 0)

    return ptcloud, ptcloud.size // 6

def output_pointcloud(nVertices, ptcloud, strOutputPath, bHasNormals=False):
    """
        Given a point cloud produced from image_fusion, output it to a PLY file.
        TODO: Consider having a separate flag to allow for outputing just the depth and not colors.
    """
    # open the file and write out the standard ply header
    outputFile = open(strOutputPath + ".ply", "w")
    outputFile.write("ply\n")
    outputFile.write("format ascii 1.0\n")
    outputFile.write("comment generated via python script Process3DImage\n")
    outputFile.write("element vertex %d\n" % (nVertices))
    outputFile.write("property float x\n")
    outputFile.write("property float y\n")
    outputFile.write("property float z\n")

    if bHasNormals:
        outputFile.write("property float nx\n")
        outputFile.write("property float ny\n")
        outputFile.write("property float nz\n")

    outputFile.write("property uchar red\n")
    outputFile.write("property uchar green\n")
    outputFile.write("property uchar blue\n")
    outputFile.write("element face 0\n")
    outputFile.write("property list uchar int vertex_indices\n")
    outputFile.write("end_header\n")

    # output the actual points
    for pt in ptcloud:
        dx, dy, dz = pt[0:3]

        dx *= 0.001
        dy *= 0.001
        dz *= 0.001

        if bHasNormals:
            nx, ny, nz, r, g, b = pt[3:]
            outputFile.write(
                "%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %d %d %d\n" %
                (-dx, dy, dz, nx, ny, nz, r, g, b))
        else:
            r, g, b = pt[3:]
            outputFile.write(
                "%10.6f %10.6f %10.6f %d %d %d\n" %
                (dx, dy, dz, r, g, b))

    outputFile.close()

def images_to_textured_mesh(
        img_color,
        img_depth,
        camera_params,
        tri_dist_thres=0.75):
    """ Given an rgb-d image pair, return a textured mesh using the spherical model. """
    ptcloud = []
    triangles = []
    texcoords = []

    img_h, img_w = img_depth.shape
    n_points_total = img_w * img_h
    n_points_processed = 0

    for h in range(0, img_h):
        for w in range(0, img_w):
            depth_val = img_depth[h, w]
            r, g, b = img_color[h, w]

            xcoord = ((w - camera_params.cx) / camera_params.fx) * depth_val
            ycoord = ((h - camera_params.cy) / camera_params.fy) * depth_val
            zcoord = depth_val

            ptcloud.append([xcoord, ycoord, zcoord, r, g, b])

            # texcoords.append([1.0 - w_ratio, 1.0 - h_ratio])
            texcoords.append([w / img_w, 1.0 - h / img_h])

            n_points_processed += 1
            print_text_progress_bar(
                n_points_processed / n_points_total,
                bar_name='Images to textured mesh   ')
    print()

    skip_val = 2
    n_triangles_processed = 0
    for h in range(0, img_h, 2):
        for w in range(0, img_w, 2):
            idx_00 = h * img_w + w  # top left
            idx_10 = h * img_w + w + skip_val  # top right
            idx_01 = (h + skip_val) * img_w + w  # bottom left
            idx_11 = (h + skip_val) * img_w + w + skip_val  # bottom right

            if (idx_00 < len(ptcloud)) and (idx_10 < len(ptcloud)) and (
                    idx_01 < len(ptcloud)) and (idx_11 < len(ptcloud)):
                pt_tl = np.array(ptcloud[idx_00][0:3])
                pt_tr = np.array(ptcloud[idx_10][0:3])
                pt_bl = np.array(ptcloud[idx_01][0:3])
                pt_br = np.array(ptcloud[idx_11][0:3])

                # compute pair-wise distances and exclude triangles who have
                # edges which are too long
                d_tltr = np.linalg.norm(pt_tl - pt_tr)
                d_trbr = np.linalg.norm(pt_tr - pt_br)
                d_brbl = np.linalg.norm(pt_br - pt_bl)
                d_bltl = np.linalg.norm(pt_bl - pt_tl)
                d_trbl = np.linalg.norm(pt_tr - pt_bl)

                if (
                    d_tltr < tri_dist_thres) and (
                    d_trbr < tri_dist_thres) and (
                    d_brbl < tri_dist_thres) and (
                    d_bltl < tri_dist_thres) and (
                        d_trbl < tri_dist_thres):
                    triangles.append(
                        [idx_00 + 1, idx_01 + 1, idx_10 + 1])  # triangle 1
                    triangles.append(
                        [idx_10 + 1, idx_01 + 1, idx_11 + 1])  # triangle 2

            n_triangles_processed += 2
            print_text_progress_bar(
                n_triangles_processed / (img_h * img_w * 2), bar_name='Triangulating....   ')

    return np.array(ptcloud), np.array(
        triangles), np.array(texcoords), n_points_total

def output_textured_mesh(
        ptcloud,
        texcoords,
        triangles,
        fname_image_texture,
        fname_obj,
        strOutputPath):
    texture_file = open(
        os.path.join(
            strOutputPath,
            fname_image_texture) +
        ".mtl",
        "w")
    texture_file.write("newmtl material0\n")
    texture_file.write("Ka 1.000000 1.000000 1.000000\n")
    texture_file.write("Kd 1.000000 1.000000 1.000000\n")
    texture_file.write("Ks 0.000000 0.000000 0.000000\n")
    texture_file.write("Tr 1.000000\n")
    texture_file.write("illum 1\n")
    texture_file.write("Ns 0.000000\n")
    texture_file.write("map_Kd " + fname_image_texture + ".png\n")
    texture_file.close()

    print("Writing point cloud to '" + str(strOutputPath + \
          fname_obj + ".obj") + "' ... ", end="", flush=True)
    outputFile = open(os.path.join(strOutputPath, fname_obj) + ".obj", "w")
    outputFile.write("mtllib ./" + fname_image_texture + ".mtl\n")

    # output the actual points
    for pt in ptcloud:
        dx, dy, dz = pt[0:3]
        outputFile.write("v %.6f %.6f %.6f\n" % (dx, dy, dz))
        outputFile.write("vn 0.0 0.0 0.0\n")

    for vt in texcoords:
        tex_u, tex_v = vt
        outputFile.write("vt %.6f %.6f\n" % (tex_u, tex_v))

    outputFile.write("usemtl material0\n")
    for t in triangles:
        idx_1, idx_2, idx_3 = t
        outputFile.write(
            "f %d/%d/%d %d/%d/%d %d/%d/%d\n" %
            (idx_1, idx_1, idx_1, idx_2, idx_2, idx_2, idx_3, idx_3, idx_3))

    outputFile.close()

    print("Finished!")

if __name__ == "__main__":
    # strInputPath = "/home/po/Documents/data/body_reconstruction_deep_learning/00_testdataset_01/snaps01/"
    # strOutputPath = strInputPath
    # process_multiview(strInputPath, strOutputPath, 3)

    for i in range(1, 4):
        strInputPath = "F:/data/deep-learning/additive_depth/98_vr_calib/test01_img_seq/cam0" + \
            str(i) + "/"
        strColorFolder = os.path.join(strInputPath, "color")
        strDepthFolder = os.path.join(strInputPath, "depth")

        strColorFilename = os.path.join(strColorFolder, "1519264284736.jpg")
        strDepthFilename = os.path.join(strDepthFolder, "1519264284736.png")

        img_color = cv2.imread(strColorFilename)
        img_depth = cv2.imread(strDepthFilename, -1)

        params = GetCameraParameters("OrbbecAstra", 0.5)

        # output textured mesh
        strOutputPath = "F:/data/deep-learning/additive_depth/98_vr_calib/test01_calib_frames/"
        if not os.path.exists(strOutputPath):
            os.makedirs(strOutputPath)
        texture_img_name = "cam0" + str(i) + "-image-texture"

        cv2.imwrite(
            os.path.join(
                strOutputPath,
                texture_img_name +
                '.png'),
            img_color)
        # plt.imsave(, cv2.flip( (imgC_clr/255), 1 ))
        # ptcloud, triangles, texcoords, n_points_total = images_to_textured_mesh(cv2.cvtColor(img_color, cv2.COLOR_BGR2RGB), pred_00)

        ptcloud, triangles, texcoords, n_points_total = images_to_textured_mesh(
            img_color, img_depth / 1000, params, tri_dist_thres=0.25)
        output_textured_mesh(
            ptcloud,
            texcoords,
            triangles,
            texture_img_name,
            "cam0" + str(i),
            strOutputPath)
        print()


"""
milkman projects view.

URLs include:
/
"""
import flask
import milkman

@milkman.app.route('/projects', methods=['GET'])
def all_projects():
    """View function to display all projects."""
    context = {}
    context['page_title'] = 'Projects'
    context['body_id'] = 'projectsBody'
    return flask.render_template('projects.html', **context)


from django.http import HttpResponse
from django.shortcuts import render, redirect
from .forms import *
from .models import *
import os
import cv2
import math
import numpy as np
from PIL import Image as im
from django.core.files.base import ContentFile

#paragraph segmentation
def ParagraphSegmentation(img):
    image = img.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (7,7), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Create rectangular structuring element and dilate
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dilate = cv2.dilate(thresh, kernel, iterations=4)

    # Find contours and draw rectangle
    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 3)
    return image

# Word Segmentation
def WordSegmentation(img):
    image = img.copy()
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    #binary
    ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
    #dilation
    kernel = np.ones((5,5), np.uint8)
    img_dilation = cv2.dilate(thresh, kernel, iterations=1)
    #find contours
    contours, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for i, ctr in enumerate(contours):
        x, y, w, h = cv2.boundingRect(ctr)
        cv2.rectangle(image,(x,y),(x+w, y+h),(255,128,0),2)
    return image

#Line Segmentation
def LineSegmentation(img):
	 image = img
	 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

	 ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)

     #dilation
	 kernel = np.ones((5,100), np.uint8)
	 img_dilation = cv2.dilate(thresh, kernel, iterations=1)

     #find contours
	 ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     #sort contours
	 sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
	 for i, ctr in enumerate(sorted_ctrs):
		 x, y, w, h = cv2.boundingRect(ctr)
		 roi = image[y:y+h, x:x+w]
		 cv2.rectangle(image,(x,y),(x+w, y+h),(90,0,255),2)

	 return image

#CharSegmentation
def CharSegmentation(img):
    image = img.copy()
    image = cv2.fastNlMeansDenoisingColored(image,None,10,10,7,21)
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    #threshold
    res,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(1,3))

    #dilation
    dilated = cv2.dilate(thresh,kernel,iterations = 1)
    #find contours
    contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    for contour in contours:
        [x,y,w,h] = cv2.boundingRect(contour)
        cv2.rectangle(image,(x,y),(x+w, y+h),(76,0,153),1)

    return image

# Create your views here.
def add_image(request):
    W = Text.objects.all()
    W.delete()
    if request.method == 'POST':
        form = TextForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            val=request.POST.get('All')
            print(val)
            image = Text.objects.last()
            imagefile = image.Img
            image = np.asarray(bytearray(imagefile.read()), dtype="uint8")

            image_encode1 = cv2.imdecode(image, cv2.IMREAD_COLOR)
            image_encode2 = cv2.imdecode(image, cv2.IMREAD_COLOR)
            image_encode3 = cv2.imdecode(image, cv2.IMREAD_COLOR)
            image_encode4 = cv2.imdecode(image, cv2.IMREAD_COLOR)

            img_para = ParagraphSegmentation(image_encode1)
            img_word =  WordSegmentation(image_encode2)
            img_line = LineSegmentation(image_encode3)
            img_char = CharSegmentation(image_encode4)

        cv2.imwrite("text_segmentation/static/text_segmentation/img_para.png",img_para)
        cv2.imwrite("text_segmentation/static/text_segmentation/img_word.png",img_word)
        cv2.imwrite("text_segmentation/static/text_segmentation/img_line.png",img_line)
        cv2.imwrite("text_segmentation/static/text_segmentation/img_char.png",img_char)

        return render(request, 'text_segmentation/img.html', {'form' : form,'success':'yes'})
    else:
        form = TextForm()
        return render(request, 'text_segmentation/img.html',{'form':form})

def disp_word(request):
    return render(request, 'text_segmentation/Disp_word.html')

def disp_para(request):
    return render(request, 'text_segmentation/Disp_para.html')

def disp_line(request):
    return render(request, 'text_segmentation/Disp_line.html')

def disp_char(request):
    return render(request, 'text_segmentation/Disp_char.html')

def disp_all(request):
    return render(request, 'text_segmentation/Disp_all.html')


import sys
import os
import json
import requests
import time

class ClientMarketwatch(object): #ClientBas
    def __init__(self, ):
        super(ClientMarketwatch, self).__init__()

    def scape_symbol(self, persistency_qoute):

        # K+S AG ADR (KPLUY)
        # United States
        # OOTC
        # Specialty Chemicals

        list_page_letter = ['0-9']
        list_page_letter.extend([str(char) for char in 'TUVWXYZ'])
        list_page_index = [str(char) for char in '1234']
        # list_page_letter.extend([str(char) for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'])
        # list_page_index = [str(char) for char in '123456789']

        for page_letter in list_page_letter:
            for page_index in list_page_index:
                url_request = 'https://www.marketwatch.com/tools/markets/american-depository-receipt-stocks/a-z/' + page_letter + '/' + page_index
                response = requests.get(url_request)
                if response.status_code == 200:
                    # with open('temp', 'wb') as file:
                    #     file.write(response.content)

                    self.format_response(response.content, persistency_qoute)
                    # with open('temp.json', 'w') as file:
                    #     json.dump(json_config, file)

                    # return self.format_quote(json_config)
                else:
                    print(response.content)
                    raise RuntimeError('not 200')

    def format_response(self, bytearray_response, persistency_qoute):
        string_response = bytearray_response.decode("utf-8", errors='ignore')
        string_from = '
'
        string_to = '
'
        string_table = self.find_string(string_response, string_from, string_to)
        list_string_table_row = ['
' +tp for tp in string_table.split('
')][2:]
        for string_table_row in list_string_table_row:
            self.format_table_row(string_table_row, persistency_qoute)

    def format_table_row(self, string_table_row, persistency_qoute):
        string_row_0, index_from = self.find_string_from(string_table_row, '<td ', '	', 0)
        string_row_1, index_from = self.find_string_from(string_table_row, '', '	', index_from)
        string_row_2, index_from = self.find_string_from(string_table_row, '', '	', index_from)
        string_row_3, _ = self.find_string_from(string_table_row, '', '	', index_from)
        ticker = self.find_string(string_row_0, '(', ')')
        tag_country_trade = 'country_trade_' + self.find_string(string_row_0, 'countryCode=', '">').lower()
        tag_exchange = 'exchange_'  + string_row_2.lower()
        tag_sector = 'sector_'  + string_row_3.lower().replace(' ', '_')
        persistency_qoute.add_list_tag(ticker, [tag_country_trade, tag_exchange, tag_sector, 'is_ard'])
# PersistencyQoute

    def find_string(self, text, string_from, string_to):
        index_from = text.find(string_from) + len(string_from)
        index_to = text.find(string_to, index_from)
        # print(index_from)
        # print(index_to)
        return text[index_from:index_to]

    def find_string_from(self, text, string_from, string_to, index_from):
        index_from = text.find(string_from, index_from) + len(string_from)
        index_to = text.find(string_to, index_from)
        # print(index_from)
        # print(index_to)
        return text[index_from:index_to], index_to


import logging
import os
from flask import Flask
from config import Config
from flask_mail import Mail
from flask_migrate import Migrate
from flask_moment import Moment
from flask_login import LoginManager
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from logging.handlers import SMTPHandler, RotatingFileHandler

mail = Mail()
db = SQLAlchemy()
migrate = Migrate()
moment = Moment()
login = LoginManager()
bootstrap = Bootstrap()
login.login_view = 'auth.login'
login.login_message_category = "info"

# define the name of your app below
APP_NAME = 'Template - Name'
app = Flask(__name__)

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    login.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    migrate.init_app(app, db)
    mail.init_app(app)

    # import the blueprint
    from app.auth.routes import auth
    from app.main.routes import main
    from app.rooms.routes import rooms
    from app.errors.handlers import errors
    from app.bookings.routes import bookings

    # register the blueprint
    app.register_blueprint(auth)
    app.register_blueprint(main)
    app.register_blueprint(rooms)
    app.register_blueprint(errors)
    app.register_blueprint(bookings)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'], subject=f'{APP_NAME} Failure',
                credentials=auth, secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler(f'logs/{APP_NAME.lower()}.log',
                                           maxBytes=10240, backupCount=10)
        file_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]'))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info(f'{APP_NAME} startup')

    return app


import numpy as np
import pandas as pd
import copy
import math
import time
import os
from tqdm import tqdm
import argparse
from matplotlib import pyplot as plt

from c import Common
from make_M import WPMatrix
from make_S import SMatrix
import cdf

'''
#DrDe class
##Calculate DrDe by SM, PWM; Do classifiction with r0, e0
    Calculation done at initi state, just follow the method rised by paper

##Varibales:
    df_drde: a [pairs_num * 5] pandas DataFrame, columns=['u','v','dr','de','st']
    c: common parameters
    classified: Whether classified

##Methods:
    classify(self, dr_t, de_t): Do classifiction with r0, e0. result store in df_drde['st']
    outputDrDe_to_file(filename): Output ['u','v','dr','de','st'] to the file.
    st_to_file(filename): Output ['u','v','st'] to the file.
'''

class DrDe():
    def __init__(self, wpm):
        if wpm.pairs_num <= 0:
            print("There is no edge in wpm")
            exit(-1)
        self.df_drde = pd.DataFrame(np.zeros(wpm.pairs_num * 5).reshape((wpm.pairs_num, 5)),\
                                columns=['u','v','dr','de','st'])
        self.classified = False
        self.st_result = [0, 0, 0, 0]

        '''Calculate Dr and De'''
        pbar = tqdm(total=len(wpm.WM.keys()), ncols=25)
        pbar.set_description("Calcu drde:")
        index = 0
        for uv in wpm.WM:
            sum, dr, de = 0.0, 0.0, 0.0
            for tl in wpm.WM[uv]:
                dr += wpm.get_w(uv, tl) * wpm.get_p(uv, tl)
                sum += wpm.get_w(uv, tl)
                if(wpm.get_w(uv, tl) != 0):
                    de = de+1
            dr = dr/sum
            de = math.log(de,2)
            self.df_drde.iloc[index] = [uv[0], uv[1], dr, de, 0]
            index += 1
            pbar.update(1)
        pbar.close()

    def classify(self, dr_t, de_t):
        def compare_it(r, e, r_t, e_t):
            '''S:1, F:2, FS:3, IR:4'''
            st = 0
            if r > r_t:
                st += 2
            if e > e_t:
                st += 1
            st += 1
            return st
        print("---------------------")
        print("Separating st now...")
        time_start=time.time()

        self.df_drde['st'] = self.df_drde.apply(lambda x: \
                            compare_it(x['dr'],x['de'],dr_t,de_t), axis=1)

        tmp_result = self.df_drde['st'].value_counts(sort=False)
        for i in range(1, 5):
            if i in tmp_result:
                self.st_result[i-1] = tmp_result[i]
        labels = ['S','F','FS','IR']
        for i in range(4):
            print("%s:%d"%(labels[i], self.st_result[i]))
        self.classified = True
        time_end=time.time();
        print ("Used %.2f s"%(time_end-time_start))

    def outputDrDe_to_file(self, filename):
        self.df_drde['u'] = self.df_drde['u'].apply(lambda x: '%d'%x)
        self.df_drde['v'] = self.df_drde['v'].apply(lambda x: '%d'%x)
        print("Print DrDe to: %s"%(filename))
        self.df_drde.to_csv(filename, index = False)

    def st_to_file(self, filename):
        self.df_drde['u'] = self.df_drde['u'].apply(lambda x: '%d'%x)
        self.df_drde['v'] = self.df_drde['v'].apply(lambda x: '%d'%x)
        print("Print DrDe to: %s"%(filename))
        self.df_drde[['u','v','st']].to_csv(filename, index = False)

    def drawCDF(self, axs):
        cdf.cdf(self.df_drde['dr'], axs[0], "Dr", 'r', 'o')
        cdf.cdf(self.df_drde['de'], axs[1], "De", 'r', 'o')

    def drawResult(self, name):
        if(self.classified == True):
            colors = [(6/255,102/255,177/255),(254/255,220/255,25/255),(0,173/255,79/255),(237/255,27/255,58/255)]

            patches, texts = plt.pie(self.st_result, wedgeprops=dict(width=0.4), colors=colors, startangle =90,)
            plt.title("Ratio of social ties")
            plt.axis('equal')

            labels = ["S: {:.2f}%".format(self.st_result[0] / sum(self.st_result) * 100),
                    "F: {:.2f}%".format(self.st_result[1] / sum(self.st_result) * 100),
                    "FS: {:.2f}%".format(self.st_result[2] / sum(self.st_result) * 100),
                    "IR: {:.2f}%".format(self.st_result[3] / sum(self.st_result) * 100)]

            sort_legend = True
            if sort_legend:
                patches, labels, dummy =  zip(*sorted(zip(patches, labels, self.st_result),
                                                      key=lambda x: x[2],
                                                      reverse=True))

            plt.legend(patches, labels, loc='center')
            plt.savefig("%s_result.png"%(name))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--T", default = 10080, type = int, help = "Major cycle (minutes).")
    parser.add_argument("--delta_T", default = 180, type = int, help = "Time step (minutes).")
    parser.add_argument("--access_file", default = "", type = str, help = "Path of access files.")
    parser.add_argument("--meet_file", default = "", type = str, help = "Path of meet files.")
    args = parser.parse_args()

    c = Common(args.T, args.delta_T)
    c.generate_params_from_data(args.access_file)
    s = SMatrix(c)
    s.get_SM_from_file(args.access_file)
    wp = WPMatrix(c)
    wp.get_WPM_from_file(args.meet_file, s)
    del s

    drde = DrDe(wp)
    del wp
    drde.classify(0.083, 0.5)
    drde.drawResult('test')
    #drde.outputDrDe_to_file("drde_test.csv")

if __name__ == "__main__":
    main()


from __future__ import print_function, absolute_import, division

import tempfile

import pytest
import numpy as np
from astropy.wcs import WCS
from astropy.io import fits

from numpy.testing import assert_allclose

from ..casa_low_level_io import getdesc
from ..casa_wcs import wcs_casa2astropy
from ...tests.test_casafuncs import make_casa_testimage
from .test_casa_low_level_io import ALL_DATA_FIXTURES
from ...conftest import HEADER_FILENAME

try:
    from casatools import image
    CASATOOLS_INSTALLED = True
except ImportError:
    CASATOOLS_INSTALLED = False

@pytest.fixture
def filename(request):
    return request.getfixturevalue(request.param)

def assert_header_correct(casa_filename):

    fits_filename = tempfile.mktemp()

    # Use CASA to convert back to FITS and use that header as the reference

    ia = image()
    ia.open(casa_filename)
    ia.tofits(fits_filename, stokeslast=False)
    ia.done()
    ia.close()

    # Parse header with WCS - for the purposes of this function
    # we are not interested in keywords/values not in WCS
    reference_wcs = WCS(fits_filename)
    reference_header = reference_wcs.to_header()

    # Now use our wcs_casa2astropy function to create the header and compare
    # the results.
    desc = getdesc(casa_filename)
    actual_wcs = wcs_casa2astropy(desc['_keywords_']['coords'])
    actual_header = actual_wcs.to_header()

    assert sorted(actual_header) == sorted(reference_header)

    for key in reference_header:
        if isinstance(actual_header[key], str):
            assert actual_header[key] == reference_header[key]
        else:
            assert_allclose(actual_header[key], reference_header[key])

@pytest.mark.skipif('not CASATOOLS_INSTALLED')
@pytest.mark.parametrize('filename', ALL_DATA_FIXTURES, indirect=['filename'])
def test_wcs_casa2astropy(tmp_path, filename):
    casa_filename = str(tmp_path / 'casa.image')
    make_casa_testimage(filename, casa_filename)
    assert_header_correct(casa_filename)

@pytest.mark.skipif('not CASATOOLS_INSTALLED')
def test_wcs_casa2astropy_linear(tmp_path):

    # Test that things work properly when the WCS coordinates aren't set

    casa_filename = str(tmp_path / 'test.image')

    data = np.random.random((3, 4, 5, 6, 7))

    ia = image()
    ia.fromarray(outfile=casa_filename, pixels=data, log=False)
    ia.close()

    assert_header_correct(casa_filename)

def header_copy_with(**kwargs):
    header = fits.Header.fromtextfile(HEADER_FILENAME).copy()
    header.update(kwargs)
    return header

ALL_HEADERS = [
    header_copy_with(),
    header_copy_with(CTYPE1='GLON-TAN', CTYPE2='GLAT-TAN'),
    header_copy_with(CTYPE1='SLON-TAN', CTYPE2='SLAT-TAN'),
    header_copy_with(CTYPE1='ELON-TAN', CTYPE2='ELAT-TAN'),
    header_copy_with(CTYPE1='HLON-TAN', CTYPE2='HLAT-TAN'),
    header_copy_with(SPECSYS=''),
    header_copy_with(SPECSYS='TOPOCENT'),
    header_copy_with(SPECSYS='GEOCENTR'),
    header_copy_with(SPECSYS='BARYCENT'),
    header_copy_with(SPECSYS='HELIOCEN'),
    header_copy_with(SPECSYS='LSRK'),
    header_copy_with(SPECSYS='LSRD'),
    header_copy_with(SPECSYS='GALACTOC'),
    header_copy_with(SPECSYS='LOCALGRP'),
    header_copy_with(SPECSYS='CMBDIPOL'),
    header_copy_with(SPECSYS='SOURCE'),
    header_copy_with(RADESYS='FK4'),
    header_copy_with(RADESYS='FK4-NO-E'),
    header_copy_with(RADESYS='FK5'),
    header_copy_with(RADESYS='ICRS'),
    header_copy_with(EQUINOX=1950.),
    header_copy_with(EQUINOX=1979.9),
    header_copy_with(EQUINOX=2000),
    header_copy_with(EQUINOX=2010),
    header_copy_with(CTYPE3='FREQ', CUNIT3='GHz', CRVAL3=100., CDELT3=1.),
    header_copy_with(CTYPE3='WAVE', CUNIT3='m', CRVAL3=1e-6, CDELT3=1e-8),
    header_copy_with(CTYPE3='VOPT'),
    header_copy_with(CTYPE3='VRAD')
]

@pytest.mark.skipif('not CASATOOLS_INSTALLED')
@pytest.mark.parametrize('header', ALL_HEADERS)
def test_wcs_casa2astropy_additional(tmp_path, header):

    # More cases to improve coverage

    casa_filename = str(tmp_path / 'casa.image')
    fits_filename = str(tmp_path / 'casa.fits')

    fits.writeto(fits_filename, np.ones((2, 3, 4, 5)), header)

    make_casa_testimage(fits_filename, casa_filename)

    assert_header_correct(casa_filename)


from models import Location, Product
from app import db

pizzas = ['Bolognese de luxe', 'Hawaï', 'Popeye', 'Pepperoni', 'Seafood', 'Hot pizzaaah!!!',
          'Salmon delight', 'Full option', 'Pitza kebab', 'Multi cheese', '4 Seasons', 'Mega fish',
          'Creamy multi cheese', 'Green fiësta', 'Chicken bbq', 'Funky chicken', 'Veggie',
          'Meat lovers' 'Scampi mampi', 'Tabasco', 'Chicken time', 'Meatballs', 'Tuna', 'Anchovy',
          'Calzone', 'Bbq meatballs', 'Creamy chicken', 'Hot bolognese']

def add():
    simpizza = Location()
    simpizza.configure("Sim-pizza", "De Pintelaan 252 9000 Gent, tel: 09/321.02.00", "http://simpizza.be")
    db.session.add(simpizza)

    for pizza in pizzas:
        entry = Product()
        entry.configure(simpizza, pizza, 1195)
        db.session.add(entry)


# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import os

plt.rcParams['font.sans-serif'] = ['Droid Sans Fallback']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负

obs_x = [-40.0, -50.0, -35.0, -50.0, -60.0, -30.0, -20.0, -30.0, -40.0]
obs_y = [20.0, 0.0, -10.0, 25.0, 20.0, -20.0, 20.0, 5.0, 10.0]

target_x = [-60, -50, -20]
target_y = [30, -20, 0]

plt.figure()
p1 = plt.subplot(111)

p1.scatter(target_y, target_x, s=40, c='cyan', label=u'目标点')
p1.scatter(obs_y, obs_x, s=160, c='red', label=u'障碍物')
for i in range(3):
    p1.text(target_y[i] + 1, target_x[i] + 1, s=u'目标{}'.format(i + 1), fontsize=12, color='black')

u, v = np.zeros((1, 1)), np.zeros((1, 1))

p1.scatter([15], [-15], s=40, c='g')
p1.text(15.2, -14.8, u'当前位置')
u[0, 0] = target_y[0] - 15
v[0, 0] = target_x[0] + 15
p1.quiver(15, -15, u, v, scale=1, units='xy', color='g')
u[0, 0] = target_y[1] - target_y[0]
v[0, 0] = target_x[1] - target_x[0]
p1.quiver(target_y[0], target_x[0], u, v, scale=1, units='xy', color='g')
u[0, 0] = target_y[2] - target_y[1]
v[0, 0] = target_x[2] - target_x[1]
p1.quiver(target_y[1], target_x[1], u, v, scale=1, units='xy', color = 'g')

p1.set_xlabel(u'东向 /m')
p1.set_ylabel(u'北向 /m')

p1.set_aspect(1)
# p1.grid(linestyle='--', linewidth=0.5)
p1.legend(loc='lower left', fontsize=12)
p1.axis([-30, 40, -75, -10])

plt.savefig('setup.svg')

plt.show()


from urllib.parse import urlparse

""" Includes utility functions for Flask app.
"""

def url_valid(url):
	"""Check if a url is well-formatted. Defined here as containing a scheme
	   (eg. http, https) and either a netloc with more than 1 '.'
	   (eg. www.gooogle.com, NOT google.com) or a netloc that contains
	   'localhost' for local url routing. This can be updated with a more
	   explicit regex depending upon the definition of well-formatted.

	Returns:
		(bool) : True if well-formatted, False otherwise
	"""
	pr = urlparse(url)
	cond1 = any(pr.scheme)
	cond2 = pr.netloc.count('.') > 1
	cond3 = ('localhost' in pr.netloc)

	return True if cond1 and (cond2 or cond3) else False


# for-loop default structure
for i in range(0, 10):
    print(i)

print(range(0, 10))

# nested for-loop
for i in range(1, 10):
    for j in range(1, 10):
        print(i * j)

# input
list_start_num = input("input start number of list\n")
list_end_num = input("input end number of list\n")
print(
    range(list_start_num, list_end_num + 1)
)  # it's because range(x, y) means making list which has numbers from x to y-1


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 26 13:24:57 2018

@author: Andy Wang, Kelvin Kwok, Riley Kwok

"""
# READ DATA

import pandas as pd

df1 = pd.read_json("data//Status_Data1.json", encoding= 'UTF-8')
df2 = pd.read_json("data//Status_Data2.json", encoding= 'UTF-8')
df3 = pd.read_json("data//Status_Data3.json", encoding= 'UTF-8')
df4 = pd.read_json("data//Status_Data4.json", encoding= 'UTF-8')
df5 = pd.read_json("data//Status_Data5.json", encoding= 'UTF-8')
df6 = pd.read_json("data//Status_Data6.json", encoding= 'UTF-8')
df7 = pd.read_json("data//Status_Data7.json", encoding= 'UTF-8')
df8 = pd.read_json("data//Status_Data8.json", encoding= 'UTF-8')
df9_comcity = pd.read_json("data//Status_Data9_Comment_Data_with_city_code.json", encoding= 'UTF-8')
# df9_city = pd.read_json("Status_Data9_with_city_code.json", error_bad_lines = "False")
province = pd.read_json("data//weibo-china-province-city.json")
task_status = pd.read_json("data//task-status.json", encoding= 'UTF-8')
task_status['text'] = task_status['s_text']
task_status['user_id'] = task_status['uid']
task_status['id'] = task_status['sid']
task_status = task_status.drop(columns = ['s_text', 'uid', 'sid'])

label = pd.read_csv("data//train_label.txt", delimiter = '	', header = None, names = ['ID', 'other', 'category'])

frames = [df1, df2, df3, df4, df5, df6, df7, df8, task_status]
df = pd.concat(frames)
df = df.reset_index(inplace = False).drop(columns = ['index'])

category_dict = dict(zip(label.ID, label.category))

df['label'] = df.id.map(category_dict)

# DATA CLEANING

import re
import jieba

def rm_char(text): #remove symbols and special characters
    text = re.sub(r'[^\u4e00-\u9fa5]', '', text)
    return text

def get_stop_words(): # load stopwords from txt file
    file = open('stopwords-zh.txt','rb').read().decode('utf8').split('\n')
    return set(file)

def rm_tokens(words): # remove stop words and numbers
    words_list = list(words)
    stop_words = get_stop_words()
    for i in range(words_list.__len__())[::-1]:
        if words_list[i] in stop_words:
            words_list.pop(i)
        elif words_list[i].isdigit():
            words_list.pop(i)
    return words_list

def convert_doc_to_wordlist(doc, cut_all = False):
    s_list = re.split('，|【|】|。', doc)
    s_list = map(rm_char, s_list)
    s_list_cut = [rm_tokens(jieba.cut(s, cut_all = cut_all)) for s in s_list]
    word_list = sum(s_list_cut, [])
    return word_list

l = []
x = []
for i in range(len(df)): #segment sentences to words
    l.append(convert_doc_to_wordlist(df.text.iloc[i]))
for j in l: # join cleaned words back to sentence
    x.append(' '.join(j))

df['new'] = pd.DataFrame(x)

df_train = df[df.label.notnull()]
df_test = df[df.label.isnull()]

########### Supervised Modelling ############

# load pre-trained word embeddings wording
with open('sgns.weibo.word', 'rb') as f:
    for line_b in f:
        line_u = line_b.decode('utf-8')

embedding = pd.read_csv('sgns.weibo.word', header = None, sep = ' ')
embedding = embedding.drop(301, 1)
embedding = embedding.set_index(0)

# create a weight matrix for words in training docs
from numpy import asarray
from numpy import zeros
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Embedding
import numpy as np
from keras.utils import to_categorical

t = Tokenizer()
t.fit_on_texts(df.new)
vocab_size = len(t.word_index)+1
encoded_docs = t.texts_to_sequences(df_train.new)

labels_keras = np.array(df_train.label)
y_label = to_categorical(labels_keras, num_classes = 13)

max_length = 200
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)

embeddings_index = dict()
for i in range(len(embedding)):
	word = embedding.index[i]
	coefs = embedding.iloc[i,:].astype('float32')
	embeddings_index[word] = coefs

embedding_matrix = zeros((vocab_size, 300))
for word, i in t.word_index.items():
	embedding_vector = embeddings_index.get(word)
	if embedding_vector is not None:
		embedding_matrix[i] = embedding_vector

from keras import backend as K

def f1(y_true, y_pred):
    def recall(y_true, y_pred):
        """Recall metric.

        Only computes a batch-wise average of recall.

        Computes the recall, a metric for multi-label classification of
        how many relevant items are selected.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
        recall = true_positives / (possible_positives + K.epsilon())
        return recall

    def precision(y_true, y_pred):
        """Precision metric.

        Only computes a batch-wise average of precision.

        Computes the precision, a metric for multi-label classification of
        how many selected items are relevant.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision
    precision = precision(y_true, y_pred)
    recall = recall(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

model = Sequential()
e = Embedding(vocab_size, 300, weights=[embedding_matrix], input_length= max_length, trainable=False)
model.add(e)
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(13, activation='softmax'))

model.compile(optimizer='Adadelta', loss='categorical_crossentropy', metrics=[f1])

history = model.fit(padded_docs, y_label, epochs=20, verbose=1, batch_size = 16, validation_split=0.3)

import matplotlib.pyplot as plt
def modelplot(history):
    plt.plot(history.history['f1'])
    plt.plot(history.history['val_f1'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.axis([0,20,0.3,1])
    plt.show()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.axis([0,20,0,4])
    plt.show()

modelplot(history)

'''
encoded_docs = t.texts_to_sequences(df_test.new)
padded_docs_y = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
model.predict()
'''

#####################################
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

no_features = None #can be any integer i.e. 20, 50 etc. If None, use all words

# TF-IDF
tfidf_vectorizer = TfidfVectorizer(max_features= no_features)
tfidf_vectorizer.fit(df.new)
tfidf = tfidf_vectorizer.transform(df_train.new)
tfidf_real = tfidf_vectorizer.transform(df_test.new)

tfidf_feature_names = tfidf_vectorizer.get_feature_names()
tfidf_vectorizer.idf_
tfidf.toarray()

# LDA
tf_vectorizer = CountVectorizer(max_features=no_features)
tf_vectorizer.fit(df.new)
tf = tf_vectorizer.transform(df_train.new)
tf_feature_names = tf_vectorizer.get_feature_names()
tf.toarray()

# Neural network
model = Sequential()
e = Embedding(1438, 10173, weights=[dense], trainable=False)
model.add(e)
model.add(Flatten())
model.add(Dense(10173, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(13, activation='softmax'))

model.compile(optimizer='Adadelta', loss='categorical_crossentropy', metrics=['acc'])

history = model.fit(tfidf, y_label, epochs=20, verbose=1,validation_split=0.3)

# Run NMF
from sklearn.decomposition import NMF, LatentDirichletAllocation
no_topics = 13
nmf = NMF(n_components=no_topics, init='nndsvd').fit(tfidf)
W = nmf.fit_transform(tfidf)
H = nmf.components_

# Run LDA
lda = LatentDirichletAllocation(n_components=no_topics, learning_method='online', learning_offset=50.).fit(tf)
W_lda = lda.fit_transform(tf)
H_lda = lda.components_

def display_topics(model, feature_names, no_top_words):
    for topic_idx, topic in enumerate(model.components_):
        print ("Topic %d:" % (topic_idx))
        print (" ".join([feature_names[i]
                        for i in topic.argsort()[:-no_top_words - 1:-1]]))

no_top_words = 15
display_topics(nmf, tfidf_feature_names, no_top_words)
display_topics(lda, tf_feature_names, no_top_words)

#######################################
tfidf_df = pd.DataFrame(tfidf.toarray())
dummies = pd.get_dummies(df_train.user_id)
dummies = dummies.reset_index(inplace = False).drop(columns = ['index'])
tfidf_new = pd.concat([tfidf_df, dummies], axis = 1)

from sklearn.model_selection import train_test_split
y = df_train.label
x_train, x_test, y_train, y_test = train_test_split(tfidf_new, y, test_size=0.3, random_state=1)

import xgboost as xgb
m = xgb.XGBClassifier(n_estimators=100, max_depth=8, learning_rate=0.1)
train1 = m.fit(x_train, y_train)
predict = m.predict(x_test)

m1 = xgb.XGBClassifier()
train2 = m1.fit(x_train, y_train)
predict2 = m1.predict(x_test)

from sklearn.metrics import f1_score
f1_score(y_test, predict2, average = 'micro')

from sklearn.metrics import precision_recall_fscore_support as score
precision, recall, fscore, support = score(y_test, predict2)
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('fscore: {}'.format(fscore))
print('support: {}'.format(support))

from sklearn.metrics import precision_recall_fscore_support
precision_recall_fscore_support(y_test, predict2)

from catboost import CatBoostClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, auc
from catboost import CatBoost

m_cat = CatBoostClassifier(iterations = 2000, loss_function = 'MultiClass')
m_cat.fit(x_train, y_train)
y_pred_cat = m_cat.predict(x_test)

from sklearn.metrics import f1_score
f1_score(y_test, y_pred_cat, average = 'micro')

tfidf_df = pd.DataFrame(tfidf_real.toarray())
dummies = pd.get_dummies(df_test.user_id)
dummies = dummies.reset_index(inplace = False).drop(columns = ['index'])
x_true = pd.concat([tfidf_df, dummies], axis = 1)

y_pred_actual = m_cat.predict(x_true)
y_pred_actual = pd.DataFrame(y_pred_actual)
df_test_pred = pd.concat([df_test.reset_index(inplace = False).drop(columns = ['index']), y_pred_actual], axis = 1)

df_test_pred['label'] = df_test_pred.iloc[:,-1]
df_test_pred = df_test_pred.drop([0], axis=1)

########
from sklearn.ensemble import RandomForestClassifier
m_rand = RandomForestClassifier()
m_rand.fit(x_train, y_train)
predict_rand = m_rand.predict(x_test)

from sklearn.metrics import f1_score
f1_score(y_test, predict_rand, average = 'micro')

y_pred_actual_rand = m_rand.predict(x_true)

############################################

final_df = pd.concat([df_test_pred, df_train])
final_df = final_df.reset_index(inplace = False).drop(columns = ['index'])

# Saving files

final_df.to_csv("Final_2.csv",encoding='utf-8-sig')
final1.to_csv('team_2.txt', sep='\t', index=False)


from flask import Flask
#from string import template
import os
app = Flask(__name__)

@app.route('/')
def hello():
    templete = open('index.html').read()
    #print templete
    return templete

@app.route('/mario')
def fullScreen():
    html = open('FullScreenMario/mario.html').read()
    return html

@app.route('/Ashley')
def ashPage():
    html = open('ashley.html').read()
    return html

@app.route('/images/<filename>g')
def send_file(filename):
    return send_from_directory(UPLOAD_FOLDER, filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80)


__author__ = 'chrisbamborough'

import rhinoscriptsyntax as rs
import random as r
# use seed input to change randomness
r.seed(seed)

class Walker:

    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0

    def point(self):
        point = rs.AddPoint(self.x,self.y,self.z)
        return point

    def step(self):
        # uniform produces randomness off the grid
        stepX = r.uniform(-1,1)
        stepY = r.uniform(-1,1)
        stepY = r.uniform(-1,1)

        stepx += stepX
        stepy += stepY
        stepz += stepZ

# initialise a Walker object
w = Walker()
pList = []

for t in range(time):
    w.step()
    pList.append(w.point())

a = pList


import matplotlib.pyplot as plt
import numpy as np
from experiment_utils.plot_utils import load_exps_data, defaultdict, group_by, correct_limit, filter

plt.style.use('ggplot')
import matplotlib
matplotlib.use('TkAgg')

SMALL_SIZE = 20
MEDIUM_SIZE = 24
BIGGER_SIZE = 28
BIG_SIZE = 30
LINEWIDTH = 3
YELLOW = '#FBC15E'
BLUE = '#348ABD'

plt.rc('font', size=MEDIUM_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=MEDIUM_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIG_SIZE)  # fontsize of the figure title

prop_cycle = plt.rcParams['axes.prop_cycle']
colors = [
    (255, 128, 14),  # BLUE
    (0, 107, 164),  # ORANGE
    # (171, 171, 171),  # GREY
    # (89, 89, 89),  # LIGHT GREY
    (137, 137, 137),  # VERY DARK GREY
    (95, 158, 209),  # LIGHT BLUE
    # (163, 200, 236), # VERY LIGHT BLUE
    # (255, 188, 121),  # LIGHT ORANGE
    (200, 82, 0),  # RED
    (207, 207, 207),  # LIGHT GREY
]
for idx, color in enumerate(colors):
    colors[idx] = '#%02x%02x%02x' % color
COLORS = dict()
LEGEND_ORDER = {'regularization': 0, 'no-regularization': 1}

data_path = './data/corl_data/regularization_comparison/'

exps_data = load_exps_data([data_path])

sampling_time = {'meta_mb.envs.mb_envs.walker2d.Walker2dEnv': 0.008,
                 'Walker2d': 0.008,
                 'meta_mb.envs.mb_envs.half_cheetah.HalfCheetahEnv': 0.05,
                 'HalfCheetah': 0.05,
                 'meta_mb.envs.mb_envs.ant.AntEnv': 0.05,
                 'Ant': 0.05,
                 'meta_mb.envs.mb_envs.hopper.HopperEnv': 0.008,
                 'meta_mb.envs.mujoco.hopper_env.HopperEnv': 0.008,
                 'Hopper': 0.008,
                 }
round_plot = {'a-me-ppo': {'Hopper': 5000,
                           'Walker2d': 5000,
                           'Ant': 5000,
                           'HalfCheetah': 5000,
                      },
            'me-ppo': {'meta_mb.envs.mb_envs.hopper.HopperEnv': 5000,
                        'meta_mb.envs.mb_envs.walker2d.Walker2dEnv': 5000,
                        'meta_mb.envs.mb_envs.ant.AntEnv': 5000,
                        'meta_mb.envs.mb_envs.half_cheetah.HalfCheetahEnv': 5000,
                      },
            'me-trpo': {'meta_mb.envs.mb_envs.hopper.HopperEnv': 5000,
                        'meta_mb.envs.mb_envs.walker2d.Walker2dEnv': 5000,
                        'meta_mb.envs.mb_envs.ant.AntEnv': 5000,
                        'meta_mb.envs.mb_envs.half_cheetah.HalfCheetahEnv': 5000,
                      },
            'mbmpo': {'meta_mb.envs.mb_envs.hopper.HopperEnv': 5000,
                        'meta_mb.envs.mb_envs.walker2d.Walker2dEnv': 5000,
                        'meta_mb.envs.mb_envs.ant.AntEnv': 5000,
                        'meta_mb.envs.mb_envs.half_cheetah.HalfCheetahEnv': 5000,
                      }

         }

x_limits = {'meta_mb.envs.mb_envs.walker2d.Walker2dEnv': [0, 10e4],
            'Walker2d': [0, 10e4],
            'meta_mb.envs.mb_envs.half_cheetah.HalfCheetahEnv': [0, 10e4],
            'HalfCheetah': [0, 10e4],
            'meta_mb.envs.mb_envs.ant.AntEnv': [0, 10e4],
            'Ant': [0, 10e4],
            'meta_mb.envs.mb_envs.hopper.HopperEnv': [0, 10e4],
            'meta_mb.envs.mujoco.hopper_env.HopperEnv': [0, 10e4],
            'Hopper': [0, 10e4],
            }

def prepare_data_for_plot(exp_data,
                          y_key=None,
                          sup_y_key=None,
                          round_x=None,
                          add_sampling_time=False):
    x_y_tuples = []
    x_key = 'n_timesteps'
    asynch = exp_data[0]['flat_params'].get('async', False)
    for exp in exp_data:
        if not asynch:
            if sup_y_key is not None:
                assert type(sup_y_key) is list
                for key in sup_y_key:
                    if key in exp['progress'].keys():
                        x_y_tuples.extend(list(zip(exp['progress'][x_key], exp['progress'][key])))
                        break
            else:
                x_y_tuples.extend(list(zip(exp['progress'][x_key], exp['progress'][y_key])))
        else:
            if sup_y_key is not None:
                assert type(sup_y_key) is list
                for key in sup_y_key:
                    if key in exp['progress'].keys():
                        env_time = sampling_time[exp['flat_params']['env']]
                        x_y_tuples.extend(list(zip(exp['progress']['Data-TimeSoFar']/env_time, exp['progress'][key])))
                        break
            else:
                raise NotImplementedError
    x_y_dict = defaultdict(list)
    if asynch:
        env = exp_data[0]['flat_params']['env']
    else:
        env = exp_data[0]['flat_params']['env.$class']

    algo = exp_data[0]['flat_params']['algo']
    _round_x = round_plot.get(algo, dict()).get(env, None)
    round_x = _round_x if _round_x is not None else round_x
    for k, v in x_y_tuples:
        if round_x is not None:
            x_y_dict[(k//round_x) * round_x].append(v)
        else:
            x_y_dict[k].append(v)
    means, stddevs = [], []
    for key in sorted(x_y_dict.keys()):
        means.append(np.mean(x_y_dict[key]))
        stddevs.append(np.std(x_y_dict[key]))
    return np.array(sorted(x_y_dict.keys())), np.array(means), np.array(stddevs), x_limits[env]

def sorting_legend(label):
    return LEGEND_ORDER[label]

def get_color(label):
    if label not in COLORS.keys():
        if YELLOW in colors:
            colors.remove(YELLOW)
            COLORS[label] = YELLOW
        elif BLUE in colors:
            COLORS[label] = BLUE
            colors.remove(BLUE)
        else:
            COLORS[label] = colors.pop(0)
    return COLORS[label]

def plot_from_exps(exp_data,
                   filters={},
                   split_figures_by=None,
                   split_plots_by=None,
                   x_key='n_timesteps',
                   y_key=None,
                   sup_y_key=None,
                   plot_name='./bad-models.png',
                   subfigure_titles=None,
                   plot_labels=['interleaved policy learning', 'in-order policy learning'],
                   x_label=None,
                   y_label=None,
                   num_rows=1,
                   y_limits=None,
                   report_max_performance=False,
                   log_scale=False,
                   round_x=None,
                   ):

    exp_data = filter(exp_data, filters=filters)
    exps_per_plot = group_by(exp_data, group_by_key=split_figures_by)
    num_columns = len(exps_per_plot.keys())
    assert num_columns % num_rows == 0
    num_columns = num_columns // num_rows
    fig, axarr = plt.subplots(num_rows, num_columns, figsize=(11, 16))
    axarr = np.reshape(axarr, (num_rows, num_columns))
    fig.tight_layout(pad=5.0, w_pad=0, h_pad=3, rect=[0, 0, 1, 1])

    # iterate over subfigures
    for i, (default_plot_title, plot_exps) in enumerate(sorted(exps_per_plot.items())):
        plots_in_figure_exps = group_by(plot_exps, split_plots_by)
        subfigure_title = subfigure_titles[i] if subfigure_titles else default_plot_title
        r, c = i//num_columns, i%num_columns
        axarr[r, c].set_title(subfigure_title)
        axarr[r, c].xaxis.set_major_locator(plt.MaxNLocator(5))
        axarr[r, c].ticklabel_format(style='sci', axis='x', scilimits=(0, 0))

        # iterate over plots in figure
        y_max_mean = -1e10
        y_axis_min = 1e10
        y_axis_max = -1e10
        for j, default_label in enumerate(sorted(plots_in_figure_exps, key=sorting_legend)):
            exps = plots_in_figure_exps[default_label]
            x, y_mean, y_std, x_limits = prepare_data_for_plot(exps,
                                                     y_key=y_key,
                                                     sup_y_key=sup_y_key,
                                                     round_x=round_x)

            label = plot_labels[j] if plot_labels else default_label
            _label = label if i == 0 else "__nolabel__"
            if log_scale:
                axarr[r, c].semilogx(x, y_mean, label=_label, linewidth=LINEWIDTH, color=get_color(label))
            else:
                axarr[r, c].plot(x, y_mean, label=_label, linewidth=LINEWIDTH, color=get_color(label))

            axarr[r, c].fill_between(x, y_mean + y_std, y_mean - y_std, alpha=0.2, color=get_color(label))

            # axis labels
            axarr[r, c].set_xlabel(x_label if x_label else x_key)
            axarr[r, c].set_ylabel(y_label if y_label else y_key)
            if x_limits is not None:
                axarr[r, c].set_xlim(*x_limits)
            if y_limits is not None:
                axarr[r, c].set_ylim(*y_limits)
            else:
                _y_axis_min, _y_axis_max = correct_limit(axarr[r, c], x, y_mean-y_std, y_mean+y_std)
                y_axis_max = max(_y_axis_max, y_axis_max)
                y_axis_min = min(_y_axis_min, y_axis_min)
                if max(y_mean) > y_max_mean:
                    y_max_mean = max(y_mean)

        if report_max_performance:
            label = 'max' if i == 0 else "__nolabel__"
            axarr[r, c].plot(axarr[r, c].get_xlim(), [y_max_mean]*2, 'k--', label=label)
        if y_limits is None:
            axarr[r, c].set_ylim([y_axis_min, y_axis_max])

    fig.legend(loc='lower center', ncol=1, bbox_transform=plt.gcf().transFigure)
    fig.savefig(plot_name)

# filter_dict = {'fast_lr': 0.001, 'path_length':200}
filter_dict = {}

exps_data_filtered = filter(exps_data, filter_dict)

plot_from_exps(exps_data,
               split_figures_by='env.$class',
               split_plots_by='exp_tag',
               y_key='train-AverageReturn',
               x_key='Time',
               filters=filter_dict,
               sup_y_key=['EnvTrajs-AverageReturn',
                          'Data-EnvTrajs-AverageReturn',
                          'train-AverageReturn'],
               # subfigure_titles=['HalfCheetah - output_bias_range [0.0, 0.1]',
               #                  'HalfCheetah - output_bias_range [0.0, 0.5]',
               #                  'HalfCheetah - output_bias_range [0.0, 1.0]'],
               # plot_labels=['ME-MPG', 'ME-TRPO'],
               x_label='Time-steps',
               y_label='Average Return',
               plot_name='./comparison_regularization.png',
               num_rows=2,
               report_max_performance=False,
               log_scale=False,
               round_x=5000,
               )


from OpenGL import GLU
import numpy as np
import realcomp
from realcomp.envs.realcomp_env import Goal
import gym

import os
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(os.path.dirname(currentdir))
os.sys.path.insert(0,parentdir)
from my_controller import MyController

Controller = MyController

scores = {}

def add_scores(challenge, score):
    if challenge in scores.keys():
        scores[challenge] += [score]
    else:
        scores[challenge] = [score]

def report_score():
    print("*****************")
    total_score = 0
    challenges = ['2D','2.5D','3D']
    for key in challenges:
        if key in scores.keys():
            results = scores[key]
            formatted_results = ", ".join(["{:.4f}".format(r) for r in results])
            challenge_score = np.mean(results)
        else:
            results = []
            formatted_results = "None"
            challenge_score = 0

        print("Challenge {} - {:.4f}".format(key, challenge_score))
        print("Goals: {}".format(formatted_results))
        total_score += challenge_score
    total_score /= len(challenges)
    print("Overall Score: {:.4f}".format(total_score))
    print("*****************")

def demo_run(extrinsic_trials=350):

    env = gym.make('REALComp-v0')
    controller = Controller(env.action_space)

    # change length of simulation for testing purposes
    env.intrinsic_timesteps = 1e7 #default = 1e7
    env.extrinsic_timesteps = 2e3 #default = 2e3

    # render simulation on screen
    # env.render('human')

    # reset simulation
    observation = env.reset()

    reward = 0
    done = False

    # intrinsic phase
    print("Starting intrinsic phase...")
    while not done:

        # Call your controller to chose action
        action = controller.step(observation, reward, done)

        # do action
        observation, reward, done, _ = env.step(action)

        # get frames for video making
        # rgb_array = env.render('rgb_array')

    # extrinsic phase
    print("Starting extrinsic phase...")
    totalScore = 0
    for k in range(extrinsic_trials):

        # reset simulation
        observation = env.reset()
        reward = 0
        done = False

        # set the extrinsic goal to pursue
        env.set_goal()
        print("Starting extrinsic trial...")

        while not done:

            # Call your controller to chose action
            action = controller.step(observation, reward, done)

            # do action
            observation, reward, done, _ = env.step(action)

            # get frames for video making
            # rgb_array = env.render('rgb_array')

        add_scores(*env.evaluateGoal())
        print("Current score:")
        report_score()

    print("Final score:")
    report_score()

if __name__=="__main__":
    demo_run()


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 23:38:44 2017

@author: HSIN
"""

from PIL import Image
import numpy as np
import sys

im = Image.open(sys.argv[1])
im_modified = Image.open(sys.argv[2])

im_np_data = np.array(im.getdata())
im_modified_np_data = np.array(im_modified.getdata())
im_diff = im_np_data - im_modified_np_data

ans_two = Image.new('RGBA', im.size, color = None)

for i in range(len(im_np_data)):
    if not np.array_equal(im_diff[i], [0, 0, 0, 0]):
        ans_two.putpixel((i%im.size[0], int(i/im.size[0])), tuple(im_modified_np_data[i]))

ans_two.save('ans_two.png')


N=input().split()
k=int(N[1])
c=0
a=list(map(int,input().split()))
for i in range(0,len(a)):
    for j in range(i+1,len(a)):
        if a[i]+a[j]==k:
            c=1
            break
if c==1:
    print('YES')
else:
    print('NO')


from flask import Flask,jsonify, request

app = Flask(__name__)

@app.route('/getRecommendations')
def hello_world():
    id = request.args.get('id')
    try:
        return jsonify({"answer":int(id)})
    except (ValueError,TypeError):
        return jsonify({"answer":"please type integer id"})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9600, debug=True)


def main(fahrenheit):
    # convert_fahrenheit_to_celsius

        # Se realiza la operación - conversión
        celsius = (fahrenheit -32)*(5/9)

        # Se muetra el output - resultado de la conversión
        print(f'{fahrenheit} grados fahrenheit equivalen a {celsius} grados celsius')

if __name__ == '__main__':
    while True:
        # Se almacena el valor ingresado por el usuario
        fahrenheit = input('Ingrese la temperatura en grados fahrenheit\nIngrese \'exit\' para salir\n>>')

        # Dar la salida
        if fahrenheit.lower() == 'exit':
            print('El programa se cerrará')
            break

        main(float(fahrenheit))


class SimboloTablas():

    def __init__(self,nombre):
        self.nombre = nombre
        self.columnas = []

    def setNombreTabla(self, nombre):
        self.nombre = nombre

    def agregarColumna(self, columnaAgregar):

        # return 1 -->> Columna agregadaConExito
        # return 0 -->> Nombre de ColumnaRepetido

        if len(self.columnas) == 0:
            self.columnas.append(columnaAgregar)
            return 1
        else:
            for nodoColumna in self.columnas:
                if (nodoColumna.nombre.lower()==columnaAgregar.nombre.lower()):
                    return 0    # Columna con el mismo nombre ya existe

            self.columnas.append(columnaAgregar)
            return 1

    # valida si el nombre de una columna ya existe dentro de la tabla
    def comprobarNombreColumna(self, nombreColumna):
        # return 1 -->> Nombre ya existe
        # return 0 -->> Nombre NO existe

        if len(self.columnas) == 0:
            return 0
        else:
            for nodoColumna in self.columnas:
                if(nodoColumna.nombre.lower()==nombreColumna.lower()):
                    return 1

            return 0

    # Nota no valida si la columna es usada como referencia de llave foránea, validar eso antes
    def modificarNombreColumna(self, nombreNuevo,nombreAntiguo):
        # return 0 ---> Columna no existe
        # return 1 ---> Exito
        # return 2 ---> nombreNuevo ya existe

        if len(self.columnas) == 0:
            return 0
        else:
            for nodoColumna in self.columnas:

                # se ubica que la columna exista
                if (nodoColumna.nombre.lower()==nombreAntiguo.lower()):

                    ##La columna Existe
                    if ( self.comprobarNombreColumna(nombreNuevo) == 1 ):
                        return 2
                    else:
                        #Se modifica el nombre de la columna
                        nodoColumna.setNombreColumna(nombreNuevo)
                        return 0

            return 0

    def eliminarColumna(self, nombreColumna):
        #return 0 --->> Columna No existe
        #return 1 --->> Exito
        #return 2 --->> Columna Llave Primarina no se puede eliminar
        #return 3 --->> Columna es llave foranea, se debe eliminar primero el constraint de llave foránea
        #return 4 --->> Tiene un Constraint se debe eliminar primero el constraint

        if len(self.columnas) == 0:
            return 0
        else:
            indice = 0
            for nodoColumna in self.columnas:
                if nodoColumna.nombre.lower() == nombreColumna.lower():
                    if nodoColumna.primaryKey == True:
                        return 2
                    elif nodoColumna.tablaForanea != None:
                        return 3
                    elif nodoColumna.nombreConstraint != None:
                        return 4
                    else:
                        #Se elimina la columna
                        self.columnas.pop(indice)
                        return 1

                indice = indice + 1
            return 0

    def obtenerSimboloColumna(self, nombreColumna):
        if len(self.columnas) == 0:
            return None
        else:
            for nodoColumna in self.columnas:
                if(nodoColumna.nombre.lower()==nombreColumna.lower()):
                    return nodoColumna

            return None

    # valida si el nombre de una columna ya existe dentro de la tabla
    def obtenerColumna(self, nombreColumna):
        # return Columna -->> Si existe
        # return None -->> si NO existe

        if len(self.columnas) == 0:
            return None
        else:
            for nodoColumna in self.columnas:
                if(nodoColumna.nombre.lower()==nombreColumna.lower()):
                    return nodoColumna

            return None

    def obtenerUltimoIndice(self):
        #retorna numero entero
        if len(self.columnas) == 0:
            return 0
        else:
            indice = 0
            for nodoColumn in self.columnas:
                indice = nodoColumn.indice

            return indice


from selenium import webdriver

driver = webdriver.Chrome("C:\\Browsers_Selenium\\Chrome\\ChromeDriver.exe")
driver.maximize_window()
#Save website URL in variable
URL = "http://seleniumhq.org"
#Navigate to the URL
driver.get(URL)

#screenshot for whole webpage
driver.get_screenshot_as_file('screenshot_demo1.jpg')


# -*- coding: utf-8 -*-
"""
Created on Thu Nov 16 19:42:55 2017

@author: liwei

E-mail:444liwei@163.com

"""
from pymongo import MongoClient
import random

class DoubanProxy(object):
    def __init__(self):
        self.f = open(r'D:/Anaconda/test/proxy.txt','r')
        self.proxy = random.choice(self.f.readlines())
        self.f.close()

    def process_request(self,request,spider):
        proxy = self.proxy.strip()
        print(proxy)
        request.meta['proxy'] = proxy


import praw
from urllib2 import urlopen
import json

key = "[redacted]" # Google Youtube API key
pewdielink = "https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=pewdiepie&key="+key
tserieslink = "https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=tseries&key="+key

def commentreply (comment) :

    pewdiedata = urlopen(pewdielink).read()
    tseriesdata = urlopen(tserieslink).read()

    pewdiesubs = int( json.loads(pewdiedata)["items"][0]["statistics"]["subscriberCount"] )
    tseriessubs = int( json.loads(tseriesdata)["items"][0]["statistics"]["subscriberCount"] )

    if (pewdiesubs > tseriessubs) :
        comment.reply("PewDiePie currently has **" + ("{:,d}".format( int(pewdiesubs) - int(tseriessubs) )) + "** more subscribers than T-Series! \n *** \n ^(I'm just a student's CS project, please don't hurt me! If there's an issue, please PM me. Have a nice day!)")
    elif (tseriessubs > pewdiesubs) :
        comment.reply("T-Series currently has " + ("{:,d}".format( tseriessubs - pewdiesubs )) + " more subscribers than PewDiePie!")

    comment.upvote()


from PIL import Image
import numpy as np
import pywavefront as wav

global textures

def load_image( infilename ) :
    img = Image.open( infilename )
    img.load()
    data = np.asarray( img, dtype="int32" )
    return data

def save_image( npdata, outfilename ) :
    img = Image.fromarray( np.asarray( np.clip(npdata,0,255), dtype="uint8"), "RGB" )
    img.save( outfilename )

def equal ( arr1, arr2 ) :
	if len(arr1) != len(arr2):
		return False

	for i in range(len(arr1)):
		if arr1[i] != arr2[i]:
			return False

	return True

def avg_arrays ( arr1, arr2 ) :
	if len(arr1) != len(arr2):
		return arr1

	arr = [0 for i in range(len(arr1))]
	for i in range(len(arr1)):
		arr[i] = (arr1[i] + arr2[i]) / 2

	return arr

def get_colour( uv, mat ) :
	if mat == 0:
		return (0, 0, 0)
	else:
		tex = textures[mat-1]
		rows = len(tex)-1
		cols = len(tex[0])-1

		i = int(round(uv[1] * rows, 0))
		j = int(round(uv[0] * cols, 0))

		return tex[i][j]

def get_points( scene ) :
	points = []

	m = 0
	for material in scene.mesh_list[0].materials:
		index = 0

		while index < len(material.vertices):
			u = material.vertices[index]
			v = material.vertices[index+1]
			x = material.vertices[index+8]
			y = material.vertices[index+9]
			z = material.vertices[index+10]
			c = get_colour((u,v), m)

			points.append(((u, v), (x, y, z), c))

			index += 11

		m += 1

	return points

def generate_image( data ) :
	n = 1000
	img = [[[0,0,0] for x in range(n)] for y in range(n)]

	for point in data:
		uv = point[0]
		c = point[2]

		i = int(round(uv[1] * (n-1), 0))
		j = int(round(uv[0] * (n-1), 0))

		if False == equal(img[i][j], [0, 0, 0]):
			img[i][j] = avg_arrays(img[i][j], [c[0], c[1], c[2]])
		else:
			img[i][j] = [c[0], c[1], c[2]]

	return img

if __name__ == '__main__':
	alex1 = load_image("img/Alex1.jpg")
	alex2 = load_image("img/Alex2.jpg")
	alex3 = load_image("img/Alex3.jpg")
	textures = [alex1, alex2, alex3]

	origAlex = wav.Wavefront('img/models/Alex.obj')
	#uvMappedAlex = origAlex

	print("loaded successfully :)")

	origPoints = get_points(origAlex)
	#newPoints = get_points(uvMappedAlex)

	final = generate_image(origPoints)
	save_image(final, "combined.jpg")


import os
import re
import subprocess

class ManageServices():
    """ManageServices all EPE's geoservices to ArcGIS server"""

    UTIL_SVCS = 'util'
    GEN_SVCS = 'gen'
    SYS_SVCS = 'sys'
    DELETE_SERVICE = 'delete'
    STATUS_SERVICE = 'status'
    STOP_SERVICE = 'stop'
    START_SERVICE = 'start'
    LIST_SERVICES = '-l'
    VALID_OPERATIONS = [DELETE_SERVICE, STATUS_SERVICE,
                        STOP_SERVICE, START_SERVICE]

    def __init__(self, server, user, password):
        self.server = server
        self.user = user
        self.serviceList = {}
        self.genServices = {}
        self.utilServices = []
        self.sysServices = []

        # create site from server
        self.site = "http://%s:6080" % server

        # ArcGIS supplies command line tools to manage services
        self.manageServicePy = os.path.join(os.getenv("AGSSERVER"),"tools", "admin", "manageservice.py")
        self.manageServicePy = '"%s"' % self.manageServicePy
        self.manageService = "python  " + self.manageServicePy
        self.baseCmd = "%s -s %s -u %s -p %s" % (self.manageService, self.site, self.user, password)

        # lets load up the services
        self.getServices()

    def service(self, serviceName, operation):
        """ Main method for making a call with operation type """
        op = None
        cmd = "%s -n %s -o %s" % (self.baseCmd, serviceName, operation)

        if self.serviceExists(serviceName):
            if operation in self.VALID_OPERATIONS:
                op = os.popen(cmd, ).read()
                print (open)
            else:
                print ("ERR: Invalid operation: %s" % operation)

        self.getServices()
        return op

    def stopService(self, serviceName):
        return self.service(serviceName, self.STOP_SERVICE)

    def startService(self, serviceName):
        return self.service(serviceName, self.START_SERVICE)

    def statusService(self, serviceName):
        return self.service(serviceName, self.STATUS_SERVICE)

    def deleteService(self, serviceName):
        return self.service(serviceName, self.DELETE_SERVICE)

    def serviceExists(self, serviceName):
        ret = serviceName in self.serviceList.keys()
        if not ret:
            print ("ERR: Service %s not found" % serviceName)
        return ret

    def deleteAllGPServices(self):
        for svc in self.serviceList:
            if (self.serviceList[svc]['type'] == 'GPServer' and
                 self.serviceList[svc]['status'] == 'STARTED' and
                 self.serviceList[svc]['cat'] == 'gen'):

                self.deleteService(svc)

    def deleteAllMapServices(self):
        for svc in self.serviceList:
            if (self.serviceList[svc]['type'] == 'MapServer' and
                 self.serviceList[svc]['status'] == 'STARTED' and
                 self.serviceList[svc]['cat'] == 'gen'):

                self.deleteService(svc)

    def deleteAllServices(self):
        self.deleteAllMapServices()
        self.deleteAllGPServices()

    # def listServices(self, pythonExec, fpManageService, username, passwd, server):
    def getServices(self):
        """ Returns all client services (Map and Geoprocessor).
            Will not return ArcGIS services
        """
        cmd = "%s %s" % (self.baseCmd, self.LIST_SERVICES)
        # proc = subprocess.Popen(cmd, shell=True,
        #                         stdin=subprocess.PIPE,
        #                         stdout=subprocess.PIPE)
        proc = os.popen(cmd).read()
        for x in proc.split("\n"):
            if (len(x.strip()) > 0):
                k,v = x.split("|")
                # if not (k.startswith('Utilities') or k.startswith('System') or k.startswith('Sample')):
                #     key = re.sub(r'.\w*$', "", k.strip())
                #     value = v.strip()
                #     self.serviceList[key] = value

                cat = 'gen'
                k = k.strip()
                if (k.startswith('Utilities')):
                    cat = 'util'
                    k = re.sub(r'Utilities/', "", k.strip())
                elif (k.startswith('System')):
                    cat = 'sys'
                    k = re.sub(r'System/', "", k.strip())

                svcAndType = k.split(".")

                # do not put in System or Utilities.  Can comment
                # out later if needed
                if (cat == 'util' or cat == 'sys' or k.startswith('Sample')):
                    self.serviceList # do not add
                else:
                    self.serviceList[svcAndType[0]] = {'cat': cat,
                                                   'type': svcAndType[1],
                                                   'status': v.strip()}

        return proc

    def printServices(self):
        """ prints the current services """
        for k,v in self.serviceList.iteritems():
            print ("%s - %s" % (k,v))
        print("\n\n")

if __name__ == '__main__':

    # ms = ManageServices("epedev002-ll", "siteadmin", "epeconsulting")
    # ms = ManageServices("epedev005-ll", "siteadmin", "epeconsulting")
    # ms = ManageServices("gssrvrel", "siteadmin", "epeconsulting")
    # ms = ManageServices("gssrvdev", "siteadmin", "epeconsulting")
    # ms = ManageServices("epedev001-desk", "siteadmin", "epeconsulting")
    ms = ManageServices("hotec-be", "siteadmin", "epeconsulting")

    # ms.deleteAllGPServices()
    # ms.deleteAllMapServices()
    # ms.deleteAllServices()
    # ms.deleteService('intPowerflowAnalysis')
    # ms.printServices()
    # s = ms.getServices()
    # s.printServices()
    print ("Finished!")