#!/usr/bin/env python
""" Constellano .XML to .RECORD converter
Python script for .xml to .record (tensorflow) conversation.

Command format:
    py ConvertXmlToRecord.py --images <images_dir> --labels <path_to_label_map> --output <output_dir_name>
"""

import sys
import os
import cv2
import getopt
import numpy as np
from datetime import datetime

import os
import glob
import pandas as pd
import io
import xml.etree.ElementTree as ET
import argparse

#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'    # Suppress TensorFlow logging (1)
import tensorflow as tf
tf = tf.compat.v1
from PIL import Image
from object_detection.utils import dataset_util, label_map_util
label_map_util.tf = tf.compat.v1
tf.gfile = tf.io.gfile
from collections import namedtuple

__author__ = "Marin Maslov"
__license__ = "MIT Licence"
__version__ = "2.1.2"
__maintainer__ = "Marin Maslov"
__email__ = "mmaslo00@fesb.hr"
__status__ = "Stable"

COMMAND_FORMAT = "Error! The command should be: py ConvertXmlToRecord.py --images <images_dir> --labels <path_to_label_map> --output <output_dir_name>"

def xml_to_csv(path):
    xml_list = []
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        filename = root.find('filename').text
        width = int(root.find('size').find('width').text)
        height = int(root.find('size').find('height').text)
        for member in root.findall('object'):
            bndbox = member.find('bndbox')
            value = (filename,
                    width,
                    height,
                    member.find('name').text,
                    int(bndbox.find('xmin').text),
                    int(bndbox.find('ymin').text),
                    int(bndbox.find('xmax').text),
                    int(bndbox.find('ymax').text),
                    )
            xml_list.append(value)
    column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df

def split(df, group):
    data = namedtuple('data', ['filename', 'object'])
    gb = df.groupby(group)
    return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]

def class_text_to_int(row_label, label_map_dict):
    return label_map_dict[row_label]

def create_tf_example(group, path, label_map_dict):
    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))
        classes.append(class_text_to_int(row['class'], label_map_dict))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example

def main(argv):
    images = ''
    labels = ''
    output = ''
    name = ''

    try:
        opts, args = getopt.getopt(argv, "h", ["images=", "labels=", "output=", "name="])
    except getopt.GetoptError:
        print(COMMAND_FORMAT)
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(COMMAND_FORMAT)
            sys.exit()
        elif opt in ("--images"):
            images = arg
        elif opt in ("--labels"):
            labels = arg
        elif opt in ("--output"):
            if arg[-1] == "/":
                output = arg
            else:
                output = arg + "/"
        elif opt in ("--name"):
            name = arg

    start_time = datetime.now()

    if not os.path.exists(output) != False:
        print("Creating directory: " + output)
        os.mkdir(output)

    #label_map = label_map_util.load_labelmap(labels)
    #label_map_dict = label_map_util.get_label_map_dict(label_map)

    label_map_dict = label_map_util.get_label_map_dict(labels)

    print(output)
    writer = tf.python_io.TFRecordWriter(output + name + ".record")
    examples = xml_to_csv(images)
    grouped = split(examples, 'filename')

    for group in grouped:
        tf_example = create_tf_example(group, images, label_map_dict)
        writer.write(tf_example.SerializeToString())
    writer.close()

    print('Successfully created the TFRecord file: {}'.format(output))
    print("------------------------------------")
    end_time = datetime.now()
    print("[INFO]\tTotal execution time: " + str(end_time - start_time) + ".")

if __name__ == "__main__":
    main(sys.argv[1:])


from rest_framework import serializers
from .models import Tag, Good, Category

class TagSerializer(serializers.ModelSerializer):
    name = serializers.JSONField()

    class Meta:
        model = Tag
        fields = ['id', 'name', 'uuid']

class GoodSerializer(serializers.ModelSerializer):

    class Meta:
        model = Good
        fields = ['id', 'name', 'description', 'price', 'active', 'country', 'category', 'tags', 'image']

class CategorySerializer(serializers.ModelSerializer):
    goods = GoodSerializer(many=True, read_only=True)
    good_count = serializers.SerializerMethodField()

    class Meta:
        model = Category
        fields = ['id', 'name', 'description', 'goods', 'good_count']

    def get_good_count(self, obj):
        count = obj.goods.count()
        return count


def decBinarySearch(arr, n, search):
    start = 0
    end = n-1

    while(start <= end):
        mid = start + (end-start)//2 # the last term is added to avoid overflow in worst case 4+4/2 = -3/2 if max = 4
        #whereas 4 + (4-4)/2 = 4
        if(arr[mid] == search):
            return mid
        if(search < arr[mid]):
            start = mid + 1 # Only difference; reverses the start and end modification
        else:
            end = mid - 1
#Driver code
arr = [7,6,5,4,3,2,1] # this is an sorted array
k=2
n = len(arr)
print("Position for the element", k ,"in an array is",decBinarySearch(arr,n,k))


""" Python Support """
import pytz

""" Django Support """
# Not Applicable

""" Internal Support """
# Not Applicable

"""
 Control/choice_lists.py

 Author(s)   : Matthew J swann
 Version     : 1.0
 Last Update : 2013-09-26
 Update by   : Matthew J Swann

"""

FUNCTIONAL_DESIGNATIONS = (
    ('A', '~ Data Processing Algorithm ~'),
    ('I', '~ File/User Input ~'),
    ('M', '~ Main/Start ~'),
    ('N', '~ Network ~'),
    ('S', '~ Internal Support ~'),
    ('V', '~ Validation ~'),
    ('U', '~ Unknown ~')
        )

SIZE_DESIGNATIONS = (
    ('B', 'byte'),
    ('D', 'd-word'),
    ('Q', 'q-word'),
    ('W', 'word'),
    ('X', 'other/array')
        )

TZINFOS = (
        ('US/Alaska'),
        ('US/Aleutian'),
        ('US/Arizona'),
        ('US/Central'),
        ('US/East-Indiana'),
        ('US/Eastern'),
        ('US/Hawaii'),
        ('US/Indiana-Starke'),
        ('US/Michigan'),
        ('US/Mountain'),
        ('US/Pacific'),
        ('US/Pacific-New'),
        ('US/Samoa')
    )


#!/usr/bin/env python3
# -*-coding:utf8-*-
# !/bin/bash

from enum import Enum

class BoundaryType(Enum):
    # Inlet
    VELOCITY_INLET	    = 'velocityInlet'
    FLOW_RATE_INLET	    = 'flowRateInlet'
    PRESSURE_INLET	    = 'pressureInlet'
    ABL_INLET	        = 'ablInlet'
    OPEN_CHANNEL_INLET  = 'openChannelInlet'
    FREE_STREAM	        = 'freeStream'
    FAR_FIELD_RIEMANN	= 'farFieldRiemann'
    SUBSONIC_INFLOW	    = 'subsonicInflow'
    SUPERSONIC_INFLOW	= 'supersonicInflow'

    # Outlet
    PRESSURE_OUTLET	    = 'pressureOutlet'
    OPEN_CHANNEL_OUTLET = 'openChannelOutlet'
    OUTFLOW	            = 'outflow'
    SUBSONIC_OUTFLOW	= 'subsonicOutflow'
    SUPERSONIC_OUTFLOW	= 'supersonicOutflow'

    # Wall
    WALL	            = 'wall'
    THERMO_COUPLED_WALL	= 'thermoCoupledWall'
    POROUS_JUMP	        = 'porousJump'
    FAN	                = 'fan'

    # Internal
    SYMMETRY	        = 'symmetry'
    INTERFACE	        = 'interface'
    EMPTY	            = 'empty'
    CYCLIC	            = 'cyclic'
    WEDGE	            = 'wedge'

# https://www.openfoam.com/documentation/user-guide/4-mesh-generation-and-conversion/4.2-boundaries
class BasicPatchType(Enum):
    BASIC_TYPE_PATCH            = 'patch'
    BASIC_TYPE_SYMMETRY_PLANE   = 'symmetryPlane'
    BASIC_TYPE_EMPTY            = 'empty'
    BASIC_TYPE_WEDGE            = 'wedge'
    BASIC_TYPE_CYCLIC           = 'cyclic'
    BASIC_TYPE_PROCESSOR        = 'processor'


import os
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score,log_loss
import lightgbm as lgb
from config import *
from tfidfModel import *

staticFeatureFile = '/home/malware/malwaretc/file_features.csv'
def fileFeatures(inputfile):
    df = pd.read_csv(inputfile)
    print(df.columns)
    x = df[['max_thread_len','min_thread_len','mean_thread_len','thread_num']]
    fileIDs = df['file_id']

    return x,fileIDs

def fileLabels(inputfile):
    df = pd.read_csv(inputfile)
    return df['label']

def trainModel():
    x,fileIds = fileFeatures(staticFeatureFile)
    row,col = x.shape
    tfFeatures = loadTFFeatures()
    x= pd.concat([x,tfFeatures.iloc[0:row,:]],axis=1)
    y = fileLabels(staticFeatureFile)
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.3)
    train_data=lgb.Dataset(x_train,y_train)

    param = {'objective':'multiclass','learning_rate':.05,'num_class':6,'max_bin':300}
    param.update({'max_depth':10,'num_leaves':550 })
    param['metric'] = ['multi_logloss']

    num_round=1000
    # test_data = lgb.Dataset(x_test,y_test)
    test_data = lgb.Dataset(x_test,y_test, reference=train_data)

    # lgbm=lgb.train(param,train_data,num_round)
    lgbm=lgb.train(param,train_data,num_round,valid_sets=[test_data], early_stopping_rounds=10)
    checkModel(y_test,lgbm.predict(x_test))
    lgbm.save_model('model.txt', num_iteration=lgbm.best_iteration)

if __name__=='__main__':
    trainModel()


class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n <=2:
            return n
        pre,next = 1,2
        for i in xrange(2,n):
            pre,next = next,pre + next
        return next


import sys
sys.stdin = open("baek9012.txt")
"""
괄호검사 전형적인 스택문제
( 는 일단 push
) 가 나오면 현재 스택이 비어있으면 => 거짓
스택이 있으면 일단 ( 가 있단 소리 따라서 pop()
(만약 괄호가 여러종류 혼용된 상태라면 pop된 애가 내가 원한건지도 체크 해야함)
다 체크하고나서stk 이 남아있으면 안 된다.
위 조건을 모두 통과해야 VPS (Valid Parenthesis String)
"""

class Stack:
    def __init__(self, capacity):
        self.max = capacity
        self.stk = [0] * self.max
        self.data = 0
        self.ptr = 0

    def push(self, x):
        if self.data >= self.max:
            raise IndexError
        self.stk[self.ptr] = x
        self.ptr += 1
        self.data += 1
        return x

    def pop(self):
        if self.data <= 0:
            raise IndexError
        self.ptr -= 1
        self.data -= 1
        return self.stk[self.ptr]

    def is_empty(self):
        return self.data <= 0

def is_right(brackets, n):
    answer = [0] * n
    for i in range(0, n):
        stk = Stack(50)
        flag = True
        for idx in range(0, len(brackets[i])):
            if brackets[i][idx] == "(":
                stk.push(brackets[i][idx])
            elif brackets[i][idx] == ")":
                if stk.is_empty():
                    flag = False
                    answer[i] = "NO"
                    break
                stk.pop()
        if flag:
            if stk.is_empty():
                answer[i] = "YES"
            else:
                answer[i] = "NO"
    return answer

N = int(input())
MAP = [0] * N
for i in range(0, N):
    temp = list(input())
    MAP[i] = temp

ans = is_right(MAP, N)
for i in range(0, N):
    print(ans[i])


# -*- coding: utf-8 -*-
__doc__ = """
This compatibility module is inspired by the one found
in CherryPy. It provides a common entry point for the various
functions and types that are used with ws4py but which
differ from Python 2.x to Python 3.x

There are likely better ways for some of them so feel
free to provide patches.

Note this has been tested against 2.7 and 3.3 only but
should hopefully work fine with other versions too.
"""
import sys

if sys.version_info >= (3, 0):
    py3k = True
    from urllib.parse import urlsplit
    range = range
    unicode = str
    basestring = (bytes, str)
    _ord = ord

    def get_connection(fileobj):
        return fileobj.raw._sock

    def detach_connection(fileobj):
        fileobj.detach()

    def ord(c):
        if isinstance(c, int):
            return c
        return _ord(c)
else:
    py3k = False
    from urlparse import urlsplit
    range = xrange
    unicode = unicode
    basestring = basestring
    ord = ord

    def get_connection(fileobj):
        return fileobj._sock

    def detach_connection(fileobj):
        fileobj._sock = None