# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.gis.db import models

SRID = 3857

class FK(models.Model):
    integer_field = models.IntegerField(blank=True, null=True, verbose_name="Integer")
    char_field = models.CharField(max_length=100, blank=True, verbose_name='Character')

class M2M(models.Model):
    integer_field = models.IntegerField(blank=True, null=True, verbose_name="Integer")
    char_field = models.CharField(max_length=100, blank=True, verbose_name='Character')

class BaseModel(models.Model):
    """
    Abstract base class with the same set of fields
    """
    boolean_field = models.BooleanField()
    null_boolean_field = models.NullBooleanField(blank=True, null=True)
    small_integer_field = models.SmallIntegerField(blank=True, null=True, verbose_name="Small Integer")
    integer_field = models.IntegerField(blank=True, null=True, verbose_name="Integer")
    float_field = models.FloatField(blank=True, null=True, verbose_name='Float')
    decimal_field = models.DecimalField(blank=True, null=True, decimal_places=5, max_digits=15, verbose_name='Decimal')
    char_field = models.CharField(max_length=100, blank=True, verbose_name='Character')
    choice_field = models.CharField(max_length=10, blank=True, choices=(('a', 'A'), ('b', 'B')), verbose_name='Choice')
    text_field = models.TextField(blank=True, verbose_name='Text')
    date_field = models.DateField(blank=True, null=True, verbose_name='Date')
    time_field = models.TimeField(blank=True, null=True, verbose_name='Time')
    date_time_field = models.DateTimeField(blank=True, null=True, verbose_name='Date Time')
    fk_field = models.ForeignKey('FK', blank=True, null=True, verbose_name='Foreign Key' )
    m2m_field = models.ManyToManyField('M2M', blank=True, verbose_name='Many To Many')

    class Meta:
        abstract = True

    def __unicode__(self):
        return u'%s.%s.%s' % (self._meta.app_label, self._meta.model_name, self.pk)

class Point(BaseModel):
    geo_field = models.PointField(blank=True, null=True, srid=SRID)

class MultiPoint(BaseModel):
    geo_field = models.MultiPointField(blank=True, null=True, srid=SRID)

class LineString(BaseModel):
    geo_field = models.LineStringField(blank=True, null=True, srid=SRID)

class MultiLineString(BaseModel):
    geo_field = models.MultiLineStringField(blank=True, null=True, srid=SRID)

class Polygon(BaseModel):
    geo_field = models.PolygonField(blank=True, null=True, srid=SRID)

class MultiPolygon(BaseModel):
    geo_field = models.MultiPolygonField(blank=True, null=True, srid=SRID)


#914
class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        def gcd(a,b):
            if a<b:
                a,b=b,a
            if a%b==0:
                return b
            return gcd(a%b,b)
        ans=-1
        cnt=[0]*10003
        for i in deck:
            cnt[i]+=1
        for i in range(10001):
            if cnt[i]>=1:
                if ans==-1:
                    ans=cnt[i]
                ans=gcd(cnt[i],ans)
        if ans<2:
            return False
        return True

#1128
class Solution:
    def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
        return sum(i*(i-1)//2 for i in list(Counter(",".join(str(sorted(dominoes[i]))) for i in range(len(dominoes))).values()))

#846
class Solution:
    def isNStraightHand(self, hand: List[int], W: int) -> bool:
        hand=sorted(hand)
        counter=Counter(hand)
        p=list(counter.keys())
        c=list(counter.values())
        l=len(p)
        h=0
        s=sum(c)
        if s%W!=0:
            return False
        while s>0:
            if h+W-1>=l:
                return False
            mm=10001
            for i in range(h,h+W):
                if c[i]<=0 or p[i]!=p[h]+i-h:
                    return False
                if c[i]<mm:
                    mm=c[i]
            for i in range(h,h+W):
                c[i]-=mm
            s-=W*mm
            for i in range(h,l):
                if c[i]>0:
                    h=i
                    break
        return True

#870
class Solution:
    def advantageCount(self, A: List[int], B: List[int]) -> List[int]:
        A=sorted(A)
        l=len(A)
        a=[-1]*l
        c=[-1]*l
        for i in range(l):
            B[i]=[B[i],i]
        B=sorted(B,key=lambda x:x[0])
        y=0
        for i in range(l):
            while y<l:
                if A[y]>B[i][0]:
                    a[i]=A[y]
                    A[y]=-1
                    break
                else:
                    y+=1
        for i in range(l):
            if a[i]>-1:
                c[B[i][1]]=a[i]
        a=c
        y=0
        for i in range(l):
            while a[i]==-1 and y<l:
                if A[y]>-1:
                    a[i]=A[y]
                    A[y]=-1
                else:
                    y+=1
    return a

#948
class Solution:
    def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
        l=len(tokens)
        t=sorted(tokens)
        eat=l
        use=-1
        power=P
        point=0
        ans=0
        while use<eat-1:
            ok=0
            while use<eat-1 and power>=t[use+1]:
                use+=1
                power-=t[use]
                point+=1
                ok=1
            if point>ans:
                ans=point
            if use<eat-1 and point>0:
                eat-=1
                power+=t[eat]
                point-=1
                ok=1
            if ok==0:
                break
        return ans

#950
class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
        l=len(deck)
        a=[i for i in range(l)]
        nex=[i+1 for i in range(l)]
        s=l
        o=[]
        h=0
        t=l-1
        while s>0:
            o.append(h)
            s-=1
            if s==0:
                break
            h=nex[h]
            nex[t]=h
            t=h
            h=nex[h]
        print(o)
        p=ans=[0]*l
        deck=sorted(deck)
        for i in range(l):
            p[o[i]]=i
        for i in range(l):
            ans[i]=deck[p[i]]
        return ans

#1007
class Solution:
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        a=[0]*7
        b=[0]*7
        ans=23333
        for i in range(len(A)):
            for j in range(1,7):
                if A[i]!=j and B[i]==j:
                    a[j]+=1
                if A[i]==j and B[i]!=j:
                    b[j]+=1
                if A[i]!=j and B[i]!=j:
                    b[j]=-23333
                    a[j]=-23333
        for i in range(1,7):
            if a[i]>=0 and a[i]<ans:
                ans=a[i]
            if b[i]>=0 and b[i]<ans:
                ans=b[i]
        if ans>20000:
            ans=-1

        return ans

#1423
class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        s=0
        l=len(cardPoints)
        k=l-k
        for i in range(k):
            s+=cardPoints[i]
        su=sum(cardPoints)
        ans=maxans=su-s
        for i in range(1,l-k+1):
            s-=cardPoints[i-1]
            s+=cardPoints[i-1+k]
            ans=su-s
            if ans>maxans:
                maxans=ans
        return maxans


from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError
from .serializers import FeedbackSerializer, FakeProfileSerializer, UserInfoSerializer, get_user_coin_data, \
    get_user_base_info_data, JieProfileSerializer, JuPeiSerializer
from .permissions import IsCustomUser, IsSuperUser, IsStaffUser
from utils.validators import RaiseMessage
from stores.models import CouponType, CouponTypeValidate
from stores.status import *
from common.renderers import MongoJsonEncoder
from common.permissions import WhiteListOnly
from common.mongo import mongodb
import json
# TODO(ChenQiushi): 印尼，待定
# from general.settings import IMPLICIT_USER
from general.settings import STATIC_URL
from general.events import UserEvent
from django.template.response import TemplateResponse
# from configs.serializers import get_auth_config
from general.settings import JIE_DOMAIN, API_ID_INFO, API_PHONE_INFO
from datetime import datetime
from .user_info import UserInfoMongo
from .serializers import get_auth_config
import requests
from .models import FeedBack, JuPei
from rest_framework.decorators import detail_route, list_route
from rest_framework.viewsets import ReadOnlyModelViewSet
import jieba
import re
from vendors.baidu_ocr import baidu_word_ocr
from common.logs import add_log_count
import traceback

class FeedbackView(APIView):

    permission_classes = (IsCustomUser,)

    def post(self, request):
        serializer = FeedbackSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        token_data = request.token_data
        number = token_data['number']
        device_id = token_data.get('device_id')
        serializer.save(number=number, device_id=device_id)
        return Response({'status': 1})

class ProfileView(APIView):

    permission_classes = (IsCustomUser,)

    def get(self, request):
        user_id = request.user_id
        return Response(get_user_base_info_data(user_id))

    def post(self, request):
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        try:  #身份认证相关 from_proj = 'jie'
            serializer = JieProfileSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            data = serializer.validated_data
            user_info.update_profile(data, 'jie')
        except:  # user.from_proj = 'dai'， 兼容头像，昵称等场景
            data = request.data.copy()
            user_info.update_profile(data)
        return self.get(request)

# 单一产品 fake 信息
class FakeProfileView(APIView):
    permission_classes = (IsCustomUser,)

    def get(self, request):
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        return Response(user_info.fake_profile)

    def post(self, request):
        user_id = request.user_id
        serializer = FakeProfileSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        user_info = UserInfoMongo(user_id)
        user_info.update_fake_profile(data)
        return self.get(request)

# 信用等级页面
class CreditLevelView(APIView):

    permission_classes = (IsCustomUser,)

    def get(self, request):
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        auth = user_info.auth
        auth_priority, authx = get_auth_config()
        if authx:
            for a in authx:
                a['auth'] = auth.get(a['type'], 0)
        ret = {
            'user': get_user_base_info_data(user_id),
            'auth_priority': auth_priority,
            'authx': authx
        }
        auth_identification = ret['user'].get('auth', {}).get('identification')
        if auth_identification == 1:
            ret['user']['auth']['identification'] = 10
        return Response(ret)

# 签到
class SignInView(APIView):
    permission_classes = (IsCustomUser,)

    def post(self, request):
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        if not user_info.has_signed:
            user_info.sign_in()
            ret = get_user_coin_data(user_id)
            return Response(ret)
        RaiseMessage().has_signed()

# 分享好友
class ShareFriendView(APIView):
    permission_classes = (IsCustomUser,)

    def post(self, request):
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        user_info.update_share_friends()
        return Response({})

def user_redbags_view(request):
    context = {'static': STATIC_URL}
    return TemplateResponse(request, 'redbags/index.html', context)

# 我的红包
class UserConponsView(APIView):
    permission_classes = (IsCustomUser,)

    def get(self, request):
        # 用户的券
        user_id = request.user_id
        user_info = UserInfoMongo(user_id)
        ret = {}

        if user_info.is_birthday:
            ret['is_birthday'] = {
                "name": "生日特权大礼包",
                "icon": "https://platplat.oss-cn-shanghai.aliyuncs.com/coupons/icon/birthday_gift.png",
                'event_action': {
                    'link': JIE_DOMAIN + '/misc/stores/coins#/List/?data={"is_birthday":true}&name=生日特权'
                }
            }

        if user_info.is_new_register:
            ret['is_new_register'] = {
                "name": "新人专享大礼包",
                "icon": "https://platplat.oss-cn-shanghai.aliyuncs.com/coupons/icon/new_register.png",
                'event_action': {
                    'link': JIE_DOMAIN + '/misc/stores/coins#/List/?data={"is_new_register":true}&name=新人专享'
                }
            }

        ret['coins_url'] = JIE_DOMAIN + '/misc/stores/coins'

        coupons = user_info.coupons
        validate_coupons = []
        for user_coupon in coupons:
            coupon = user_coupon['coupon']
            validate = CouponTypeValidate(coupon)
            add_id = user_coupon['add_id']
            data_ret = {}

            # 失效时间
            check_coupon_time = validate.check_coupon_time(user_info, user_coupon['user_created'])
            if check_coupon_time['expire'] or user_coupon['use_status'] == USED:
                user_info.delete_coupon(add_id)  # 删除过期券、使用成功券
                continue

            time_effective = check_coupon_time['time_effective'] or '~'
            time_expire = check_coupon_time['time_expire'] or '~'

            bottom_tip = []
            if check_coupon_time['time_effective']:
                bottom_tip.append('开始时间：%s' % time_effective.strftime('%Y-%m-%d %H:%M:%S'))
            if check_coupon_time['time_expire']:
                bottom_tip.append('截止时间：%s' % time_expire.strftime('%Y-%m-%d %H:%M:%S'))
            elif coupon['is_birthday']:
                bottom_tip = bottom_tip.append('生日特权')
            elif coupon['is_new_register']:
                bottom_tip = bottom_tip.append('新人专享')
            else:
                bottom_tip = bottom_tip.append('信用权益')

            # 底部 tip
            data_ret['bottom_tip'] = bottom_tip

            # button
            check_can_use = validate.check_can_use(user_info, user_coupon['user_created'], user_coupon['use_status'])
            data_ret['effective'] = check_can_use['enable']
            data_ret['button_text'] = check_can_use['tip']

            # 面值f
            if coupon['is_credit_coin']:  # 奖励信用币
                show_num = coupon['credit_coin_amount']
                show_unit = '个'
            else:
                show_num = coupon['phone_amount']/100.0
                show_unit = '元'
            data_ret['show_num'] = show_num
            data_ret['show_unit'] = show_unit
            # 红包 UI
            coupon_ret = {
                'name': coupon['name'],
                'icon': coupon['icon']
            }
            data_ret['coupon'] = coupon_ret
            data_ret['add_id'] = user_coupon['add_id']

            validate_coupons.append(data_ret)

        ret['coupons'] = json.loads(MongoJsonEncoder().encode(validate_coupons))
        return Response(ret)

# 删除统计渠道用户接口，改从 es 获取

class IdInfoView(APIView):
    # permission_classes = (IsCustomUser,)

    def post(self, request):
        serializer = UserInfoSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        ret = {}
        # 内网获取身份证信息
        # url_id = MARKET_HOST + '/mobile/idinfo'
        id_info = {}
        try:
            url_id = API_ID_INFO.format(data['identification'])
            r_id = requests.get(url_id, timeout=5)
            id_info = r_id.json()
        except:
            pass

        if id_info:
            ret.update(id_info)

        # 内网获取手机运营商
        # url_number = MARKET_HOST + '/mobile/info/' + data['number']
        url_number = API_PHONE_INFO.format(data['number'])
        number_info = {}
        try:
            r_number = requests.get(url_number, timeout=5)
            number_info = r_number.json()
        except:
            pass

        if number_info:
            ret.update({'phone_type': number_info.get('phone_type')})

        ret['apply_time'] = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
        return Response(ret)

class JuPeiViewSet(ReadOnlyModelViewSet):
    permission_classes = (WhiteListOnly,)
    queryset = JuPei.objects
    serializer_class = FeedbackSerializer

    def _check_ocr_words(self, tags):
        """
        取出mongodb中排序靠前的1000个，计算tags中占比
        :param tags:
        :return:
        """
        return len(tags)

    @list_route(methods=['post'], permission_classes=(IsCustomUser,), url_name='apply')
    def apply(self, request):
        serializer = JuPeiSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        image_url = data['image_url']
        product_id = data['product_id']
        token = request.token_data
        ju, created = JuPei.objects.update_or_create(user_id=token['user_id'], defaults={'number': token['number']})

        try:
            success, words = baidu_word_ocr(image_url)
        except:
            add_log_count('baidu_word_ocr')
            traceback.print_exc()
            success, words = None, None

        if success:
            if words:  # ocr接口调用成功且有文字，通过；ocr接口调用失败也通过
                tags = list(jieba.cut(re.sub(r'[^\u4e00-\u9fa5]', '', words)))
                if tags and self._check_ocr_words(tags):
                    bulk = mongodb.jupei_words.initialize_ordered_bulk_op()
                    for tag in tags:
                        bulk.find({'_id': tag}).upsert().update({'$inc': {'word_count': 1}})
                    bulk.execute()
                else:
                    return Response({'amount_add': 0, 'review': '未通过审核'})
            else:
                return Response({'amount_add': 0, 'review': '未通过审核'})

        amount_add, msg = ju.update_pei(product_id)
        return Response({'amount_add': amount_add, 'review': msg})

    @list_route(methods=['get'], permission_classes=(IsStaffUser,), url_name='words')
    def wordocr(self, request):
        words = mongodb.jupei_words.find(sort=[('word_count', -1)])
        return Response(list(words)[:1000])

    def retrieve(self, request, *args, **kwargs):
        user_id = int(kwargs['pk'])
        instance, created = JuPei.objects.get_or_create(user_id=user_id)
        users = mongodb.users.find_one({'_id': user_id}) or {}
        ret = {
            'amount': instance.amount,
            'send_count': instance.send_count,
            'withdraw': 0,  # 提现金额目前只能为0
            'jupei_applied': users.get('jupei_applied', [])
        }
        return Response(ret)


#Sets are an unordered collection of unique elements.

my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(2)

#cast list to set to remove repeted elements
my_list = [1,1,1,2,2,2,2,3,3,3,3,3,]
print(my_list)
my_set = set(my_list)
print((my_set))

Udemy Boot Camp/01. Python Object and Data Structure/08. Sets.py


from ndn.encoding import *

class Model(TlvModel):          # Model = [Name] [IntVal] [StrVal] [BoolVal]
    #name = NameField()          # Name = NAME-TYPE TLV-LENGTH ...
    token_val = BytesField(0x84)   # IntVal = INT-VAL-TYPE TLV-LENGTH nonNegativeInteger
    tag_val = BytesField(0x85)  # StrVal = STR-VAL-TYPE TLV-LENGTH *OCTET
    cert_val = BytesField(0x86)
    bool_val = BoolField(0x01)  # BoolVal = BOOL-VAL-TYPE 0

model = Model()
#model.name = '/name/hi'
model.token_val = b'tok'
print(type(model.token_val))
#model.str_val = b'bit string hola'
model.tag_val = b'tag'
#assert model.encode() == b'\x07\x06\x08\x04name\x02\nbit string'
res = model.encode()
print(model.encoded_length())
print(res)
#print(model.asdict())

#model = Model.parse(b'\x07\x06\x08\x04name\x02\nbit string')
#assert model.str_val == b'bit string'


#Abstraction of a TV Show

class TVShow:

	def __init__(self, name, season, magnetURL,episode,success):
		self.name=name
		self.season=season
		self.episode=episode
		# self.startDate=startDate
		self.magnetURL=magnetURL
		self.success = False


from scipy.fft import rfft, rfftfreq
import matplotlib.pyplot as plt
import pandas as pd
import os
import numpy as np
# from mdpkg.rwfile import read_dat

path_to_data = '/home/luishcc/hdd/free_thread_old/'
path_to_data = '/home/luishcc/hdd/surfactant/'
path_to_data = '/home/luishcc/hdd/surfactant/new/'
# path_to_data = '/home/luishcc/hdd/free_thread_new/'
# path_to_data = '/home/luishcc/hdd/'

def get_snap(dir, exact=True):
    try:
        with open(dir+'/breaktime.txt', 'r') as fd:
            snap = int(fd.readline())
    except:
        return None
    return snap

R = 8
ratio = 48
A = 90

surf_con = 2.0

grid = 1

ini = 1
end = 14

# data_case_dir = f'R{R}_ratio{ratio}_A{A}/1'
data_case_dir = f'R{R}-{surf_con}/1'
dir = path_to_data + data_case_dir

snap = get_snap(dir)

file = f'breaktime_correlation_grid{grid}.dat'
datafile = '/'.join([dir,file])

data = pd.read_csv(datafile,  sep=' ', header=0, names=['dz', 'correlation', 'nan'])
x = data['dz'].tolist()

num = len(x)
if num % 2 == 0:
    row = int((num / 2) + 1)
else:
    row = int((num + 1) / 2)

xx = rfftfreq(num)

sum = np.zeros(row)
sumsq = np.zeros(row)

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

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

from scipy.interpolate import interp1d, splrep, splev, CubicSpline
import scipy.optimize as opt
from numpy import asarray as ar,exp
from scipy.stats import chisquare
from scipy import stats

initials = [8, 20, 5 , -10] # initial guess
from scipy.optimize import leastsq
from scipy.special import erf

def asymGaussian(x, p):
    amp = (p[0] / (p[2] * np.sqrt(2 * np.pi)))
    spread = np.exp((-(x - p[1]) ** 2.0) / (2 * p[2] ** 2.0))
    skew = (1 + erf((p[3] * (x - p[1])) / (p[2] * np.sqrt(2))))
    return amp * spread * skew

def residuals(p,y,x):
    return y - asymGaussian(x, p)

def fit2(value):
    xx2 = xx[ini:end]
    xxx = np.linspace(xx2[0], xx2[-1], 300)

    cnsts = leastsq(residuals, initials, maxfev=100000,
                    args=(value[ini:end], # y value
                          xx2  # x value
                          ))[0]

    gaussk = asymGaussian(xxx, cnsts)

    return xxx[np.argmax(gaussk)], (xxx, gaussk)

###################
def gaus(x,a,x0,sigma):
    return a*exp(-(x-x0)**2/(2*sigma**2))

from numpy import asarray as ar,exp
import scipy.optimize as opt

def fit(value):
    xx2 = xx[ini:end]
    xxx = np.linspace(xx2[0], xx2[-1], 300)

    nn = len(xx2)
    mean = ((xx2)*(value[ini:end]))/nn
    mean = mean.sum()
    sigma = ((value[ini:end])*((xx2)-mean)**2)/nn
    sigma = sigma.sum()
    popt, pcov = opt.curve_fit(gaus, xx2, value[ini:end],
                p0=[1,mean, sigma], maxfev=100000)

    return xxx[np.argmax(gaus(xxx,*popt))], (xxx, gaus(xxx, *popt))

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

num = [i+1 for i in range(20)]

n = 1
peak = []
avg_lst = []
var_lst = []
peak_sum = 0
peak_sum2 = 0
# while os.path.isfile(datafile):
import random
while True:
    try:
        nn = num.pop(random.randrange(len(num)))
        print(nn)
    except:
        print('Error \n\n')
        break
    print(snap)
    print(datafile)
    data = pd.read_csv(datafile, sep=' ', header=0, names=['dz', 'correlation', 'nan'])
    # data = pd.read_dat(datafile)

    arr_real = np.array(data['correlation'].tolist())
    arr = abs(rfft(arr_real))
    ar_lst = arr.tolist()
    #
    # xx_l = xx.tolist()
    # p_fit = xx_l.pop(np.argmax(ar_lst[ini:])+ini)
    # ar_lst.pop(np.argmax(ar_lst[ini:])+ini)
    # p_fit += xx_l.pop(np.argmax(ar_lst[ini:])+ini)
    # ar_lst.pop(np.argmax(ar_lst[ini:])+ini)
    # p_fit += xx_l.pop(np.argmax(ar_lst[ini:])+ini)
    # ar_lst.pop(np.argmax(ar_lst[ini:])+ini)
    # p_fit += xx_l.pop(np.argmax(ar_lst[ini:])+ini)
    # p_fit /= 3

    p_fit, plot = fit(arr)
    # p_fit, plot = fit2(arr)

    print(p_fit)

    peak.append(p_fit)
    peak_sum += p_fit
    peak_sum2 += p_fit**2
    sum += arr
    sumsq += arr**2
    n += 1

    if n > 2:
        avg2 = peak_sum/(n-1)
        var2 = 0
        for aa in peak:
            var2 += (aa - avg2)**2
        avg_lst.append(avg2)
        var_lst.append((var2/(len(peak)-1))/(n-1))

    # data_case_dir = f'R{R}_ratio{ratio}_A{A}/{nn}'
    data_case_dir = f'R{R}-{surf_con}/{nn}'

    dir = path_to_data + data_case_dir
    datafile = '/'.join([dir,file])
    snap = get_snap(dir)

avg = sum/(n-1)
var = sumsq/(n-1) - avg**2

peak_avg = peak_sum/(n-1)
peak_var = peak_sum2/(n-1) - peak_avg**2

fig, ax = plt.subplots(1,1)

x = np.linspace(1,n-1,n-1-1)

ax.errorbar(x[1:], avg_lst[1:], yerr =[i**.5 for i in var_lst[1:]], fmt='o', ecolor = 'black', markersize=3.5, color='black', capsize= 3, capthick=1)

plt.show()


#!/usr/bin/python

import re;
import sys;

name=sys.argv[1];

file=open(name,"r");

coords_str="";

while 1:
 line=file.readline().strip();
 if not line:
  break;
 if re.search("\d+\.*\d*\s*,\s*\d+\.*\d*",line):
  coords_str=line+"\n";

file.close();

coords=coords_str.split();

for item in coords:
 print(item.replace(","," "));

exit();


from typing import List
class Solution:
    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
        res = []
        low = lower - 1
        nums.append(upper + 1)  # 用来保证最后的num到upper的区间
        for num in nums:
            diff = num - low
            if diff == 2: res.append(str(num - 1))
            elif diff >= 2: res.append(str(low + 1) + "->" + str(num - 1))
            low = num
        return res

if __name__ == '__main__':
    solution = Solution()
    res = solution.findMissingRanges([],1,1)
    print(res)


#!/usr/bin/env python3
#
# Run these tests with: nosetests -v -d <file>.py
#   This will run all functions even if one throws an assertion.
#
# For debugging: ./<file>.py
#   Easier to print statements.
#   This will exit qfter the first assertion.

import os

import torch
from torch.autograd import Function, Variable

import numpy as np
import numpy.random as npr
import numpy.testing as npt
np.set_printoptions(precision=6)

import numdifftools as nd
import cvxpy as cp

# from solver import BlockSolver as Solver

from nose.tools import with_setup, assert_almost_equal

import sys
sys.path.append('..')
import qpth
import qpth.solvers.cvxpy as qp_cvxpy

import qpth.solvers.pdipm.single as pdipm_s
import qpth.solvers.pdipm.batch as pdipm_b

from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
     color_scheme='Linux', call_pdb=1)

ATOL=1e-2
RTOL=1e-4

cuda = True
verbose = True

def get_grads(nBatch=1, nz=10, neq=1, nineq=3, Qscale=1.,
              Gscale=1., hscale=1., Ascale=1., bscale=1.):
    assert(nBatch==1)
    npr.seed(1)
    L = np.random.randn(nz,nz)
    Q = Qscale*L.dot(L.T)
    G = Gscale*npr.randn(nineq,nz)
    # h = hscale*npr.randn(nineq)
    z0 = npr.randn(nz)
    s0 = npr.rand(nineq)
    h = G.dot(z0)+s0
    A = Ascale*npr.randn(neq,nz)
    # b = bscale*npr.randn(neq)
    b = A.dot(z0)

    p = npr.randn(nBatch,nz)
    # print(np.linalg.norm(p))
    truez = npr.randn(nBatch,nz)

    Q, p, G, h, A, b, truez = [x.astype(np.float64) for x in [Q, p, G, h, A, b, truez]]
    zhat, nu, lam = qp_cvxpy.forward_single_np(p[0], Q, G, h, A, b)

    grads = get_grads_torch(Q, p, G, h, A, b, truez)
    return [p[0], Q, G, h, A, b, truez], grads

def get_grads_torch(Q, p, G, h, A, b, truez):
    Q, p, G, h, A, b, truez = [torch.DoubleTensor(x) for x in [Q, p, G, h, A, b, truez]]
    if cuda:
        Q, p, G, h, A, b, truez = [x.cuda() for x in [Q, p, G, h, A, b, truez]]

    Q, p, G, h, A, b = [Variable(x) for x in [Q, p, G, h, A, b]]
    for x in [Q, p, G, h]: x.requires_grad = True

    nBatch = 1
    if b.nelement() > 0:
        A.requires_grad = True
        b.requires_grad = True

    zhats = qpth.qp.QPFunction()(p, Q, G, h, A, b)

    dl_dzhat = zhats.data - truez
    zhats.backward(dl_dzhat)

    grads = [x.grad.data.squeeze(0).cpu().numpy() for x in [Q, p, G, h]]
    if A.nelement() > 0:
        grads += [x.grad.data.squeeze(0).cpu().numpy() for x in [A, b]]
    else:
        grads += [None, None]
    return grads

def test_dl_dp():
    nz, neq, nineq = 10, 2, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(p):
        zhat, nu, lam = qp_cvxpy.forward_single_np(p, Q, G, h, A, b)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dp_fd = df(p)
    if verbose:
        print('dp_fd: ', dp_fd)
        print('dp: ', dp)
    npt.assert_allclose(dp_fd, dp, rtol=RTOL, atol=ATOL)

def test_dl_dG():
    nz, neq, nineq = 10, 0, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq)

    def f(G):
        G = G.reshape(nineq,nz)
        zhat, nu, lam = qp_cvxpy.forward_single_np(p, Q, G, h, A, b)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dG_fd = df(G.ravel()).reshape(nineq, nz)
    if verbose:
        # print('dG_fd[1,:]: ', dG_fd[1,:])
        # print('dG[1,:]: ', dG[1,:])
        print('dG_fd: ', dG_fd)
        print('dG: ', dG)
    npt.assert_allclose(dG_fd, dG, rtol=RTOL, atol=ATOL)

def test_dl_dh():
    nz, neq, nineq = 10, 0, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=1., Gscale=1.)

    def f(h):
        zhat, nu, lam = qp_cvxpy.forward_single_np(p, Q, G, h, A, b)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dh_fd = df(h)
    if verbose:
        print('dh_fd: ', dh_fd)
        print('dh: ', dh)
    npt.assert_allclose(dh_fd, dh, rtol=RTOL, atol=ATOL)

def test_dl_dA():
    nz, neq, nineq = 10, 3, 1
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(A):
        A = A.reshape(neq,nz)
        zhat, nu, lam = qp_cvxpy.forward_single_np(p, Q, G, h, A, b)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dA_fd = df(A.ravel()).reshape(neq, nz)
    if verbose:
        # print('dA_fd[0,:]: ', dA_fd[0,:])
        # print('dA[0,:]: ', dA[0,:])
        print('dA_fd: ', dA_fd)
        print('dA: ', dA)
    npt.assert_allclose(dA_fd, dA, rtol=RTOL, atol=ATOL)

def test_dl_db():
    nz, neq, nineq = 10, 3, 1
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(b):
        zhat, nu, lam = qp_cvxpy.forward_single_np(p, Q, G, h, A, b)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    db_fd = df(b)
    if verbose:
        print('db_fd: ', db_fd)
        print('db: ', db)
    npt.assert_allclose(db_fd, db, rtol=RTOL, atol=ATOL)

if __name__=='__main__':
    test_dl_dp()
    test_dl_dG()
    test_dl_dh()
    test_dl_dA()
    test_dl_db()


import pandas as pd
import numpy as np

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier

#Load the dataset
df = pd.read_csv("dataset.csv",header=None, error_bad_lines=False, encoding= 'unicode_escape')
'''
print(df.head())
print(df.shape)

print(set(df[0]))

'''
from collections import Counter
'''
print(Counter(df[0]))
'''

import re
def clean_str(string):
    """
    Tokenization/string cleaning for dataset
    Every dataset is lower cased except
    """
    string = re.sub(r"\n", "", string)
    string = re.sub(r"\r", "", string)
    string = re.sub(r"[0-9]", "digit", string)
    string = re.sub(r"\'", "", string)
    string = re.sub(r"\"", "", string)
    return string.strip().lower()
'''
print(df.columns)
'''

#train test split
from sklearn.model_selection import train_test_split
X = []
for i in range(df.shape[0]):
    X.append(clean_str(df.iloc[i][1]))
y = np.array(df[0])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=5)

#feature engineering and model selection
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

#pipeline of feature engineering and model
model = Pipeline([('vectorizer', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC(class_weight="balanced")))])

#paramater selection
from sklearn.model_selection import GridSearchCV
parameters = {'vectorizer__ngram_range': [(1, 1), (1, 2),(2,2)],
               'tfidf__use_idf': (True, False)}

gs_clf_svm = GridSearchCV(model, parameters, n_jobs=-1)
gs_clf_svm = gs_clf_svm.fit(X, y)

'''
print(gs_clf_svm.best_score_)
print(gs_clf_svm.best_params_)
'''

#preparing the final pipeline using the selected parameters
model = Pipeline([('vectorizer', CountVectorizer(ngram_range=(1,2))),
    ('tfidf', TfidfTransformer(use_idf=True)),
    ('clf', OneVsRestClassifier(LinearSVC(class_weight="balanced")))])

#fit model with training data
model.fit(X_train, y_train)
'''
print(model.fit(X_train, y_train))
'''
#evaluation on test data
pred = model.predict(X_test)

'''
print(model.classes_)
'''

from sklearn.metrics import confusion_matrix, accuracy_score

'''
print(confusion_matrix(pred, y_test))
'''
import sys

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")

#print("Accuracy of model is" ,accuracy_score(y_test, pred)*100)

#save the model
from sklearn.externals import joblib
joblib.dump(model, 'model_question_topic.pkl', compress=1)

question = input("Enter the event details")

t=model.predict([question])[0]
print(t)

print("Check output.xls file generated")
#sql

import xlrd
import xlwt
from xlwt import Workbook
loc = ("CCMLEmployeeData.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0,0)

'''
print(sheet.nrows)
print(sheet.ncols)
'''

count=0
j=0
wb1 = Workbook()
sheet1=wb1.add_sheet("Sheet 1", cell_overwrite_ok=True)
l=[]
for i in range(sheet.nrows):
    if(sheet.cell_value(i,2)=='Internships' or sheet.cell_value(i,3)=='Internships'):
        l.append(sheet.cell_value(i,0))
        p=[','.join(l)]
        sheet1.write(1,0,question)
        sheet1.write(1,1,p)
wb1.save('output.xls')

file_errors_location = 'output.xls'
workbook_errors = xlrd.open_workbook(file_errors_location)


# Gurjas Dhillon
# selection5.py
# this program takes in factors as inputs and ranks pts with if statements based on how credible they are. at the end, the program outputs the final verdict for there card hopes

age = int(input("How old are you? "))
income = int(input("How much do you make annually? "))
address = int(input("How long have you lived at your current address? "))
job = int(input("How long have you worked at your current job? "))

# check all possibilities, add to pts accordingly, pts is an accumulator which decides the card limit at the end of the program

pts = 0

if age <= 20:
  pts -= 10
elif age < 30:
  pass
elif age < 50:
  pts += 20
else:
  pts += 30

if income <= 20000:
  pass
elif income <= 45000:
  pts += 12
elif income <= 70000:
  pts += 24
else:
  pts += 30

if address < 1:
  pts -= 5
elif address <= 3:
  pts += 5
elif address <= 8:
  pts += 12
else:
  pts += 20

if job < 2:
  pts -= 4
elif job <= 4:
  pts += 8
else:
  pts += 15

# check if user is credible using the pts accumulated with the factors

if pts <= 20:
  print("No card")
elif pts < 35:
  print("card with $500 limit")
elif pts < 60:
  print("card with $5000 limit")
else:
  print("card with $10000 limit")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 17 15:40:56 2021

@author: frem
"""

from ..layer import LandUseLayer, ProbaLayer
from . import Region
from ..tools._path import path_split
from ..tools._console import title_heading

import numpy as np
from copy import deepcopy

import logging
logger = logging.getLogger('clumpy')

class Territory(dict):
    """
    Territory.

    Parameters
    ----------
    regions : list(Region)
        List of regions which constitute the territory. If ``None``, an empty list is created
        and can be append by ``self.add_region()``.
    verbose : int, default=0
        Verbosity level.

    verbose_heading_level : int, default=1
        Verbose heading level for markdown titles. If ``0``, no markdown title are printed.
    """

    def __init__(self,
                 verbose=0,
                 verbose_heading_level=1):

        self.features = None

        self.verbose = verbose
        self.verbose_heading_level = verbose_heading_level

        self.lul = {}

    def add_region(self,
                   region):
        self[region.label] = region
        region.territory = self
        return(self)

    def set_lul(self, lul, kind):
        self.lul[kind] = lul
        return(self)

    def get_lul(self, kind):
        return(self.lul[kind])

    def set_features(self, features):
        self.features = features
        return(self)

    def get_features(self):
        return(self.features)

    def check(self, objects=None):
        """
        Check the unicity of objects.
        Notably, estimators uniqueness are checked to avoid malfunctioning during transition probabilities estimation.
        """
        if objects is None:
            objects = []

        for region in self.values():
            if region in objects:
                raise(ValueError("Region objects must be different."))
            else:
                objects.append(region)

            region.check(objects=objects)

    def fit(self):
        """
        Fit the territory.

        Parameters
        ----------
        lul_initial : LandUseLayer
            The initial land use layer.

        lul_final : LandUseLayer
            The final land use layer.

        masks : dict(Region:MaskLayer), default=None
            Dict of masks layer with the corresponding region as key. If None, the whole map is used for each region.

        Returns
        -------
        self
        """

        if self.verbose > 0:
            print(title_heading(self.verbose_heading_level) + 'Territory fitting\n')

        # convert keys if label strings

        for region in self.values():
            region.fit()

        return self

    # def compute_transition_matrix(self,
    #                               lul_initial='initial',
    #                               lul_final='final',
    #                               mask='calibration'):
    #     """
    #     Compute transition matrices

    #     Parameters
    #     ----------
    #     lul_initial : LandUseLayer
    #         The initial land use.

    #     lul_final : LandUseLayer
    #         The final land use.

    #     mask : MaskLayer, default = None
    #         The region mask layer. If ``None``, the whole area is studied.

    #     Returns
    #     -------
    #     tms : dict(Region:TransitionMatrix)
    #         A dict of transition matrices with regions as keys.
    #     """

    #     if isinstance(lul_initial, str):
    #         lul_initial = self.get_lul(lul_initial)

    #     if isinstance(lul_final, str):
    #         lul_final = self.get_lul(lul_final)

    #     tms = {}

    #     for region_label, region in self.items():
    #         tms[region_label] = region.compute_transition_matrix(lul_initial=lul_initial,
    #                                                              lul_final=lul_final,
    #                                                              mask=mask)

    #     return (tms)

    # def transition_probabilities(self,
    #                              lul,
    #                              effective_transitions_only=True):
    #     """
    #     Compute transition probabilities.

    #     Parameters
    #     ----------

    #     lul : LandUseLayer
    #         The studied land use layer.

    #     Returns
    #     -------
    #     tp : dict(Region:[J,P_v__u_Y])
    #         Dict of regional results with corresponding region as key. Th regional results are ``J``, a ndarray of shape (n_samples,)
    #         which is the element indices in the flattened map and ``P_v__u_Y``, the transition probabilities whose columns correspond
    #         to transition matrix argument : ``palette_v``.
    #     """
    #     if isinstance(lul, str):
    #         lul = self.get_lul(lul)

    #     r = {}

    #     for region_label, region in self.items():
    #         r[region_label] = region.transition_probabilities(
    #             lul=lul,
    #             effective_transitions_only=effective_transitions_only)

    #     return r

    def transition_probabilities_layer(self,
                                       effective_transitions_only=True):

        if self.verbose > 0:
            print(title_heading(self.verbose_heading_level) + 'Territory TPE\n')

        lul = self.get_lul('start')

        proba_layer = ProbaLayer(np.ndarray(shape=(0,) + lul.shape),
                                 label="",
                                 final_states=[],
                                 geo_metadata = deepcopy(lul.geo_metadata))

        for region_label, region in self.items():
            proba_layer__region = region.transition_probabilities_layer(
                effective_transitions_only=effective_transitions_only)
            proba_layer = proba_layer.fusion(proba_layer__region)

        return(proba_layer)

    def allocate(self,
                 lul='start',
                 lul_origin=None):

        if self.verbose > 0:
            print(title_heading(self.verbose_heading_level) + 'Territory Allocation\n')

        if isinstance(lul, str):
            lul = self.get_lul(lul).copy()

        if lul_origin is None:
            lul_origin = lul.copy()

        proba_layer = ProbaLayer(np.ndarray(shape=(0,) + lul.shape),
                                 label="",
                                 final_states=[],
                                 geo_metadata = deepcopy(lul.geo_metadata))

        for region in self.values():
            lul, proba_layer__region = region.allocate(lul=lul,
                                              lul_origin=lul_origin)
            proba_layer = proba_layer.fusion(proba_layer__region)

        return(lul, proba_layer)

    # def allocate(self,
    #              p,
    #              lul,
    #              lul_origin=None,
    #              distances_to_states={}):
    #     """
    #     Allocate.

    #     Parameters
    #     ----------
    #     regions_transition_matrices : dict(Region:TransitionMatrix)
    #         Dict of transition matrix with the corresponding region as key.

    #     lul : LandUseLayer
    #         The studied land use layer.

    #     masks : dict(Region:MaskLayer), default=None
    #         Dict of masks layer with the corresponding region as key. If None, the whole map is used for each region.

    #     path : str, default=None
    #         The path to save result as a tif file.
    #         If None, the allocation is only saved within `lul`, if `lul` is a ndarray.
    #         Note that if ``path`` is not ``None``, ``lul`` must be LandUseLayer.

    #     path_prefix_transition_probabilities : str, default=None
    #         The path prefix to save transition probabilities.

    #     Returns
    #     -------
    #     lul_allocated : LandUseLayer
    #         Only returned if ``path`` is not ``None``. The allocated map as a land use layer.
    #     """

    #     if lul is None:
    #         lul = 'start'

    #     if isinstance(lul, str):
    #         lul = self.get_lul(lul)

    #     if isinstance(lul, LandUseLayer):
    #         lul_data = lul.get_data()
    #     else:
    #         lul_data = lul

    #     if lul_origin is None:
    #         lul_origin_data = lul_data.copy()
    #     else:
    #         lul_origin_data = lul_origin

    #     for region_label, p_region in p.items():
    #         self[region_label].allocate(p=p_region,
    #                                     lul=lul_data,
    #                                     lul_origin=lul_origin_data,
    #                                     distances_to_states=distances_to_states)

    #     return(lul_data)

    # def allocate_layer(self,
    #                    path,
    #                    p,
    #                    lul,
    #                    lul_origin=None,
    #                    distances_to_states={}):
    #     lul_data = self.allocate(p=p,
    #                              lul=lul,
    #                              lul_origin=lul_origin,
    #                              distances_to_states=distances_to_states)

    #     alloc_layer = LandUseLayer(path=path,
    #                                data=lul_data,
    #                                copy_geo=lul,
    #                                palette=lul.palette)
    #     return(alloc_layer)

    # def iterative_allocate(self,
    #                        n_iter,
    #                        transition_matrices,
    #                        lul,
    #                        masks=None,
    #                        path_prefix=None,
    #                        path_prefix_transition_probabilities=None):
    #     """
    #     Multisteps allocation

    #     Parameters
    #     ----------
    #     n_iter : int
    #         Number of iterations to allocate.

    #     transition_matrices : dict(Region:TransitionMatrix)
    #         Dict of transition matrix with the corresponding region as key.

    #     lul : LandUseLayer or ndarray
    #         The studied land use layer.

    #     masks : dict(Region:MaskLayer), default=None
    #         Dict of masks layer with the corresponding region as key. If None, the whole map is used for each region.

    #     path_prefix : str or callable, default=None
    #         The path to save every allocated step map as ``path_prefix+'_iter'+str(i)+'.tif'``.
    #         If callable, it has to be a function with the iteration ``i`` in entry
    #         which returns the tif file path as a string.
    #         If None, the allocation is only saved within `lul`, if `lul` is a ndarray.
    #         Note that if ``path`` is not ``None``, ``lul`` must be LandUseLayer.

    #     path_prefix_transition_probabilities : str or callable, default=None
    #         The patch to save every transition probabilities step map
    #         as ``path_prefix_transition_probabilities+'_iter'+str(i)+'_'+region.label+'_'+u+'_'+v+'.tif'.``
    #         If callable, it has to be a function with the iteration ``i`` in entry
    #         which returns the path prefix as a string.

    #     Returns
    #     -------
    #     lul_allocated : LandUseLayer
    #         The allocated map as a land use layer.
    #     """

    #     if self.verbose > 0:
    #         print(title_heading(self.verbose_heading_level) + 'Territory iterative allocation\n')

    #     lul_step = lul

    #     self.verbose_heading_level += 1
    #     for i in range(1,n_iter+1):

    #         if isinstance(path_prefix, str):
    #             iter_path = path_prefix + '_iter' + str(i) + '.tif'
    #         elif callable(path_prefix):
    #             iter_path = path_prefix(i)
    #         else:
    #             iter_path = None

    #         if isinstance(path_prefix_transition_probabilities, str):
    #             iter_path_prefix_transition_probabilities = path_prefix_transition_probabilities + '_iter' + str(i) + '.tif'
    #         elif callable(path_prefix_transition_probabilities):
    #             iter_path_prefix_transition_probabilities = path_prefix_transition_probabilities(i)
    #         else:
    #             iter_path_prefix_transition_probabilities = None

    #         lul_step = self.allocate(transition_matrices=transition_matrices,
    #                                  lul=lul_step,
    #                                  masks=masks,
    #                                  path=iter_path,
    #                                  path_prefix_transition_probabilities=iter_path_prefix_transition_probabilities)

    #     self.verbose_heading_level -= 1

    #     if path_prefix is not None:
    #         return lul_step

    # def make(self, case):
    #     self = {}

    #     for region_label, region_params in case.params['regions'].items():
    #         region = Region(label=region_label,
    #                         verbose=case.get_verbose(),
    #                         verbose_heading_level=2)

    #         region.make(palette=case.palette,
    #                     **region_params)

    #         self.add_region(region)


from simulation_studies.simdata import *
import torch
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import NearestNeighbors
import numpy as np

def load_data(sim_tag = 'l'):
    if sim_tag == 'l':
        sim_dict = sim_data_low_dim(1000, 100, noise_scale = 0)
        return sim_dict

    if sim_tag == 'h':
        sim_dict = sim_data_high_dim(1000, 100, noise_scale = 0)
        return sim_dict

    if sim_tag == 'c':
        #Import real-world data
        df_all = pd.read_csv('data_all', index_col=0)
        df_unc = pd.read_csv('data_unc', index_col=0)
        df_conf = pd.read_csv('data_conf', index_col=0)

        #Full data
        x_all = df_all.values[:, 2:]
        t_all = df_all['treatment'].values.reshape(-1,1)
        y_all = df_all['outcome'].values.reshape(-1,1)

        #Unconfouded data
        x_unc = df_unc.values[:, 2:]
        t_unc = df_unc['treatment'].values.reshape(-1,1)
        y_unc = df_unc['outcome'].values.reshape(-1,1)

        #Confounded data
        x_conf = df_conf.values[:, 2:]
        t_conf = df_conf['treatment'].values.reshape(-1,1)
        y_conf = df_conf['outcome'].values.reshape(-1,1)

        #Test data
        x_test = x_all
        return {'x_unc': x_unc, 't_unc': t_unc, 'y_unc': y_unc, 'u_unc': u_unc, 'x_conf': x_conf, 't_conf': t_conf, 'y_conf': y_conf, 'u_conf': u_conf, 'x_test': x_test}

#SVC to predict prob of a sample coming from conf data or unc data
def d_score(x_conf, x_unc):
    s_conf = torch.ones(x_conf.size())
    s_unc = torch.zeros(x_unc.size())
    x_cat = torch.cat((x_conf, x_unc))
    s_cat = torch.cat((s_conf, s_unc)).flatten()
    #clf = LogisticRegression().fit(x_cat, s_cat)
    clf = SVC(gamma='auto', probability=True)
    clf.fit(x_cat, s_cat)
    d_score_conf = clf.predict_proba(x_conf)
    d_score_unc = clf.predict_proba(x_unc)

    return [torch.Tensor(d_score_conf[:, 1]), torch.Tensor(d_score_unc[:, 1])]

#Get points in covariate space for MDPM
def points(x_conf, x_unc, d_conf, d_unc):
    i = torch.argmax(torch.abs(d_conf-0.5))
    j = torch.argmax(torch.abs(d_unc-0.5))
    k = torch.argmax(torch.abs(d_unc-d_conf[i]))
    m = torch.argmax(torch.abs(d_conf-d_unc[j]))

    return [i,j,k,m]

#Balancing with middle point distance minimization (MPD)
def MPD(rep_conf, rep_unc, points):
    i, j, k, m = points
    z_i = rep_conf[i]
    z_j = rep_unc[j]
    z_k = rep_unc[k]
    z_m = rep_conf[m]

    return sum(torch.pow((z_i+z_m)/2 - (z_j+z_k)/2, 2))

def data_aug(x, x_uc, alpha):
    n_conf = x.shape[0]
    n_cov = x.shape[1]
    lam = torch.distributions.beta.Beta(torch.tensor(alpha), torch.tensor(alpha)).sample(torch.Size([n_conf])).reshape(-1,1)
    ind = np.random.choice(x_uc.shape[0], n_conf, replace=True)
    x_rand = x_uc[ind]#np.random.choice(x_uc, size=[n_conf, n_cov], replace=True)
    x_inter = lam * x_rand + (1-lam) * x
    z_inter = lam * 0 + (1-lam) * 1
    return [x_inter, z_inter]

def data_aug_within_domain(x, x_uc, alpha):
    n_conf = x.shape[0]
    lam = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha])).sample(torch.Size([n_conf]))
    x_rand = np.random.choice(x_uc.flatten(), size=[n_conf, 2], replace=True)
    x_inter = lam * x_rand[:, 0].reshape(-1,1) + (1-lam) * x_rand[:, 1].reshape(-1,1)
    z_inter = torch.zeros_like(x_inter)
    x_cat = torch.cat((x_inter, x))
    z_cat = torch.cat((z_inter, torch.ones_like(x)))
    return [x_cat, z_cat]

def data_aug_cross(x, x_uc, alpha):
    n_conf = x.shape[0]
    n_unc = x_uc.shape[0]

    lam = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha])).sample(torch.Size([n_conf * n_unc]))
    #x_rand = np.random.choice(x_uc.flatten(), size=[n_conf, 1], replace=True)
    x_inter = torch.ones(size=[n_unc * n_conf, 1])
    z_inter = torch.ones(size=[n_unc * n_conf, 1])
    j=0
    for i in range(0, n_unc * n_conf, n_conf):
        x_inter[i:i+n_conf] = lam[i:i+n_conf] * x_uc[j] + (1-lam[i:i+n_conf]) * x
        z_inter[i:i+n_conf] = lam[i:i+n_conf] * 0 + (1-lam[i:i+n_conf]) * 1
        j += 1
    return [x_inter, z_inter]

def data_aug_modi(x, x_uc, alpha):
    n_conf = x.shape[0]
    lam = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha])).sample(torch.Size([n_conf]))
    lambd = torch.max(lam, 1-lam)
    x_rand = np.random.choice(x_uc.flatten(), size=[n_conf, 1], replace=True)
    x_inter = (1-lambd) * x_rand + lambd * x
    z_inter = (1-lambd) * 0 + lambd * 1
    return [x_inter, z_inter]

def cross_sample_aug(x, x_uc, y, y_uc, t, t_uc, alpha):
    n_conf = x.shape[0]
    lam = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha])).sample(torch.Size([n_conf]))

    x_1 = x[t.flatten() == 1, :]
    x_0 = x[t.flatten() == 0, :]

    y_1 = y[t.flatten() == 1, :]
    y_0 = y[t.flatten() == 0, :]

    x_uc_1 = x_uc[t_uc.flatten() == 1, :]
    x_uc_0 = x_uc[t_uc.flatten() == 0, :]

    y_uc_1 = y_uc[t_uc.flatten() == 1, :]
    y_uc_0 = y_uc[t_uc.flatten() == 0, :]

    ind_rand_1 = np.random.choice(range(x_uc_1.shape[0]), size=[x_1.shape[0], 1], replace=True)
    ind_rand_0 = np.random.choice(range(x_uc_0.shape[0]), size=[x_0.shape[0], 1], replace=True)

    x_rand_1 = x_uc_1[ind_rand_1.flatten(), :]
    x_rand_0 = x_uc_0[ind_rand_0.flatten(), :]

    y_rand_1 = y_uc_1[ind_rand_1.flatten(), :]
    y_rand_0 = y_uc_0[ind_rand_0.flatten(), :]

    x_inter = lam * torch.cat((x_rand_1, x_rand_0)) + (1-lam) * torch.cat((x_1, x_0))
    z_inter = lam * 0 + (1-lam) * 1
    y_inter = lam * torch.cat((y_rand_1, y_rand_0)) + (1-lam) * torch.cat((y_1, y_0))
    t_inter = torch.cat((torch.ones_like(y_1), torch.zeros_like(y_0)))
    return [x_inter, z_inter, y_inter, t_inter]

def pseudo_pehe(x, t, y, cate_hat):
    x_t = x[(t == 1).flatten(), :]
    x_c = x[(t == 0).flatten(), :]

    nbrs_treatment = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(x_t)
    nbrs_control = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(x_c)

    dist_t, ind_t = nbrs_treatment.kneighbors(x)
    dist_c, ind_c = nbrs_control.kneighbors(x)

    y_t, y_c = y[ind_t.flatten()].reshape(-1,1), y[ind_c.flatten()].reshape(-1,1)
    pseudo_cate = y_t - y_c

    return ((pseudo_cate - cate_hat)**2).mean()

def pseudo_cate(x, t, y, x_a, t_a, y_a):
    x_t = x_a[(t_a == 1).flatten(), :]
    x_c = x_a[(t_a == 0).flatten(), :]

    nbrs_treatment = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(x_t)
    nbrs_control = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(x_c)

    dist_t, ind_t = nbrs_treatment.kneighbors(x)
    dist_c, ind_c = nbrs_control.kneighbors(x)

    y_t, y_c = np.zeros_like(y), np.zeros_like(y)
    for i in range(x.shape[0]):
       if t[i] == 1:
           y_t[i] = y[i]
           y_c[i] = y_a[(t_a == 0).flatten()][ind_c[i]]
       else:
           y_c[i] = y[i]
           y_t[i] = y_a[(t_a == 1).flatten()][ind_t[i]]

    pseudo_cate = y_t - y_c

    return pseudo_cate

class EarlyStopping():
    """
    Early stopping to stop the training when the loss does not improve after
    certain epochs.
    """
    def __init__(self, patience=15, min_delta=0.001):
        """
        :param patience: how many epochs to wait before stopping when loss is
               not improving
        :param min_delta: minimum difference between new loss and old loss for
               new loss to be considered as an improvement
        """
        self.patience = patience
        self.min_delta = min_delta
        self.counter = 0
        self.best_loss = None
        self.early_stop = False
    def __call__(self, val_loss):
        if self.best_loss == None:
            self.best_loss = val_loss
        elif self.best_loss - val_loss > self.min_delta:
            self.best_loss = val_loss
            self.counter = 0 #Reset counter
        elif self.best_loss - val_loss < self.min_delta:
            self.counter += 1
            #print(f"INFO: Early stopping counter {self.counter} of {self.patience}")
            if self.counter >= self.patience:
                print('INFO: Early stopping')
                self.early_stop = True


import boto3
from botocore.exceptions import ClientError

dynamodb = boto3.resource('dynamodb')

def dynamodb_list():
    try:
        print(list(dynamodb.tables.all()))
    except ClientError as e:
        print(e)
        return

if __name__ == '__main__':
    dynamodb_list()


import os
import sys

from PyQt5 import QtCore, QtWidgets

from pylottosimu import pylotto

def gui(arguments):
    """Open the GUI

    :param arguments: language, see in folder translate
    :type arguments: string
    :returns: none
    """
    if len(arguments) > 1:
        locale = arguments[1]
    else:
        locale = str(QtCore.QLocale.system().name())
        print("locale: {}".format(locale))
    app = QtWidgets.QApplication(sys.argv)
    translator = QtCore.QTranslator()
    translator.load(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
                    "pylottosimu", "translation", "lotto1.{}".format(locale))))
    app.installTranslator(translator)
    dialog = pylotto.LottoSimuDialog()
    sys.exit(app.exec_())

gui(sys.argv)


import time
import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.alert import Alert
import easyocr
import keyboard

driver = webdriver.Chrome(r"D:\chromedriver_win32\chromedriver.exe")
# 브라우저 꺼짐 방지 옵션
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)
#yes24 이동
url = "http://ticket.yes24.com/Perf/44653"
driver.get(url)

#로그인
WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="consiceLogin"]'))) #페이지로딩(최대300초)
driver.find_element(By.XPATH, '//*[@id="consiceLogin"]').click()
WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="SMemberID"]'))) #페이지로딩(최대300초)
driver.find_element(By.XPATH, '//*[@id="SMemberID"]').send_keys('leejc831')
driver.find_element(By.XPATH, '//*[@id="SMemberPassword"]').send_keys('qrg258*01')
driver.find_element(By.XPATH, '//*[@id="btnLogin"]').click()

a = 0
while a < 1 :
    if keyboard.read_key() == "`":  # ` 키로 매크로시작
        break
#예매하기
i = 0
while i < 1 :
    WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="consiceLogin"]'))) #페이지로딩(최대300초)
    try :
        use_yn = driver.find_element(By.CSS_SELECTOR, '#mainForm > div.renew-wrap.rw2 > div > div.rn-05 > a.rn-bb03').text
    except :
        use_yn = 0
    print("==========================")
    print(use_yn)
    if(use_yn == "예매하기") :
        driver.find_element(By.CSS_SELECTOR, '#mainForm > div.renew-wrap.rw2 > div > div.rn-05 > a.rn-bb03').click()
        #좌석영역잡기
        seat = 0
        while seat < 1 :
            print("시간 | 좌석선택완료 버튼 좌표 가져오기\n 마우스 가져다 대고 순서대로 a, b 를 누르면 됨\n")
            k = 0
            while k < 1 :
                if keyboard.read_key() == "1":
                    xxx, yyy = pyautogui.position()
                    break
            l = 0
            pyautogui.moveTo(xxx , yyy , 0.01) # 좌석상세 마우스이동
            pyautogui.click()
            while l < 1 :
                if keyboard.read_key() == "2":
                    selx, sely = pyautogui.position()
                    break
            pyautogui.moveTo(selx , sely , 0.01) # 좌석상세 마우스이동
            pyautogui.click()
            driver.switch_to.window(driver.window_handles[-1])
            driver.switch_to.frame(driver.find_element(By.XPATH, '//*[@id="divFlash"]/iframe')) # iframe 이동
            driver.find_element(By.XPATH, '//*[@id="form1"]/div[3]/div[2]/div/div[2]/p[2]/a').click()  # 예약버튼
            try :
                da = Alert(driver)
                da.accept()
                time.sleep(1)
            except :
                print("좌석선택완료")
                break

        # 매수선택
        driver.switch_to.window(driver.window_handles[-1])
        WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="StepCtrlBtn03"]/a[2]'))) #페이지로딩(최대300초)
        time.sleep(1)
        # pyautogui.moveTo(1005 , 504 , 0.01)
        # pyautogui.click()
        # ############### 세팅필요 #####################
        # pyautogui.moveTo(1005 , 552 , 0.01) # 2장기준
        # ############### 세팅필요 #####################
        # pyautogui.click()
        driver.find_element(By.XPATH, '//*[@id="StepCtrlBtn03"]/a[2]').click()  # 다음단계

        # 수령방법
        time.sleep(1)
        WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="StepCtrlBtn04"]/a[2]'))) #페이지로딩(최대300초)
        driver.find_element(By.XPATH, '//*[@id="StepCtrlBtn04"]/a[2]').click()  # 다음단계

        # 결재방법
        WebDriverWait(driver, 300).until(EC.presence_of_element_located((By.XPATH, '//*[@id="rdoPays2"]'))) #페이지로딩(최대300초)
        driver.find_element(By.XPATH, '//*[@id="rdoPays2"]').click()  # 신용카드
        driver.find_element(By.XPATH, '//*[@id="cbxAllAgree"]').click()  # 동의박스
        driver.find_element(By.XPATH, '//*[@id="StepCtrlBtn05"]/a[2]').click()  # 다음단계

        break
    else :
        print("아직예매불가")
        driver.refresh()


#encoding:utf-8
import sys
import socket
import os

'''从远程主机获取文件

	本地没有公网IP，远程主机公网IP，连接远程主机获取文件

	Arguments:
		<S/C>:启动Server/启动Client
		<remote path>:远程文件绝对路径

	e.g.:
		Server:python FetchFile.py s
		Client:python FetchFile.py c /etc/hosts > ./hosts
			   python FetchFile.py c shutdown
'''

IP='127.0.0.1'
PORT=5007
BUFFER_SIZE = 1024

#Arguments
if len(sys.argv)<2:
	print('Need Arguments:<S/C> [<remote path>] [remote ip]')
	sys.exit(0)
command=sys.argv[1]
#Server
if command == 's' or command == 'S':
	server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	#Important! Set reuse
	server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
	server_socket.bind((IP,PORT))
	print('listen on :'+str(PORT))
	while 1:
		server_socket.listen(1)
		conn,addr = server_socket.accept()
		print(addr)
		remote_path = conn.recv(BUFFER_SIZE)
		print(remote_path)
		#Shut down Server
		if remote_path == 'shutdown':break
		#Not exist file
		if not os.path.isfile(remote_path):
			continue
		#Read and Return
		file = open(remote_path,'r')
		print(file)
		data=file.read(BUFFER_SIZE)
		while len(data)>0:
			conn.send(data)
			print(data)
			data=file.read(BUFFER_SIZE)
		file.close()
		conn.close()
	conn.close()
	server_socket.close()
#Client
elif command == 'c' or command == 'C':
	#Quit without remote path
	if len(sys.argv)<4:
		print('Need Arguments:<S/C> [<remote path>] [remote addr]')
		sys.exit(0)
	#Get remote addr
	IP=sys.argv[3]
	#Get remote path
	remote_path = sys.argv[2]
	client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	client.connect((IP,PORT))
	client.send(remote_path)
	while 1:
		data = client.recv(BUFFER_SIZE)
		if not data:break
		print(data)
else:
	print('Need argument s (Server) or c (Client)')

abcdefg


import numpy as np
import os
from keras import backend as K
from keras.utils import np_utils
from sklearn.model_selection import train_test_split

#現在のプロジェクトのルートディレクトリ、つまり現在のスクリプトディレクトリの上位ディレクトリを取得します
os_path = r'C:\Users\wlx\Documents\py_study\deeplearning\cnn_face_recogntion'
IMAGE_SIZE = 64

class Dataset:
    def __init__(self,train_npz_path,test_npz_path,train_img_dir):

        #トレーニングデータセットの読み込みパス
        self.train_npz_path = train_npz_path
        #テストデータセットの読み込みパス
        self.test_npz_path = test_npz_path
        #画像タイプ
        self.user_num = len(os.listdir(train_img_dir))
        #現在のライブラリで採用されている次元順序
        self.input_shape = None

    #トレーニングデータセットを読み込み、クロス検証の原則に従ってデータセットを分割し、関連する前処理作業を実行します。
    def load_train_valid(self, img_rows=IMAGE_SIZE, img_cols=IMAGE_SIZE,
             img_channels=1):
        #データ・タイプ
        nb_classes = self.user_num
        #データセットをメモリにロードする
        datas = np.load(self.train_npz_path)
        images,labels = datas['face_imgs'],datas['face_labels']
        #ラベルの寸法を変換する
        labels = labels.reshape(len(labels),1)

        train_images, valid_images, train_labels, valid_labels = train_test_split(images,labels, test_size=0.3,random_state=1)
        # 現在の次元の順序が 'th' == 'channels_first'の場合、画像データを入力するときの順序は次のとおりです：channels,rows,cols，それ以外の場合：rows,cols,channels
        # コードのこの部分は、kerasライブラリで必要な次元の順序に従ってトレーニングデータセットを再編成することです
        if K.image_data_format() == 'channels_first':
            train_images = train_images.reshape(train_images.shape[0], img_channels, img_rows, img_cols)
            valid_images = valid_images.reshape(valid_images.shape[0], img_channels, img_rows, img_cols)
            self.input_shape = (img_channels, img_rows, img_cols)
        else:
            train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, img_channels)
            valid_images = valid_images.reshape(valid_images.shape[0], img_rows, img_cols, img_channels)
            self.input_shape = (img_rows, img_cols, img_channels)

        #トレーニングセット、検証セット、およびテストセットの数を出力します
        print(train_images.shape[0], 'train samples')
        print(valid_images.shape[0], 'valid samples')

        # 我们的模型使用categorical_crossentropy作为损失函数，因此需要根据类别数量nb_classes将
        # 类别标签进行one-hot编码使其向量化
        train_labels = np_utils.to_categorical(train_labels, nb_classes)
        valid_labels = np_utils.to_categorical(valid_labels, nb_classes)

        # ピクセルデータは正規化のためにフロートされます
        train_images = train_images.astype('float32')
        valid_images = valid_images.astype('float32')

        # それを正規化し、画像のピクセル値を0〜1の間隔に正規化します
        train_images /= 255
        valid_images /= 255

        return train_images,valid_images,train_labels,valid_labels

    #テストデータセットの読み込み
    def load_test(self,img_rows=IMAGE_SIZE, img_cols=IMAGE_SIZE,
             img_channels=1):
        # データ・タイプ
        nb_classes = self.user_num
        #テストデータセットをメモリに読み込む
        datas = np.load(self.test_npz_path)
        test_images, test_labels = datas['face_imgs'], datas['face_labels']
        #test_labelsの次元を変換する
        test_labels = test_labels.reshape(len(test_labels),1)
        # 現在の次元の順序が 'th' == 'channels_first'の場合、画像データを入力するときの順序は次のとおりです：channels,rows,cols，それ以外の場合：rows,cols,channels
        # コードのこの部分は、kerasライブラリで必要な次元の順序に従ってトレーニングデータセットを再編成することです
        if K.image_data_format() == 'channels_first':
            test_images = test_images.reshape(test_images.shape[0], img_channels, img_rows, img_cols)
        else:
            test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, img_channels)

        #トレーニングセット、検証セット、およびテストセットの数を出力します
        print(test_images.shape[0], 'test samples')

        # 我们的模型使用categorical_crossentropy作为损失函数，因此需要根据类别数量nb_classes将
        # 类别标签进行one-hot编码使其向量化
        test_labels = np_utils.to_categorical(test_labels, nb_classes)
        #ピクセルデータは正規化のためにフロートされます
        test_images = test_images.astype('float32')
        #それを正規化し、画像のピクセル値を0〜1の間隔に正規化します
        test_images /= 255

        return test_images,test_labels

if __name__ == '__main__':
    train_npz_path = os_path + r'\data\npz\video_face_train.npz'
    test_npz_path = os_path + r'\data\npz\video_face_test.npz'
    train_img_dir = os_path + r'\data\video_face_train'
    #試してみる
    datasets = Dataset(train_npz_path,test_npz_path,train_img_dir)
    train_images,valid_images,train_labels,valid_labels = datasets.load_train_valid()
    test_images, test_labels = datasets.load_test()
    print(train_labels)
    print(test_labels)


import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
ERROR_TYPE_RECOG_ERROR = [8, 21]

class SwitchBoxManager:
    def __init__(self):
        self.__mInputPin = 24
        GPIO.setup(self.__mInputPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

        self.__mLedPin_list = [8, 7, 12, 16, 20, 21]
        for pin in self.__mLedPin_list:
            GPIO.setup(pin, GPIO.OUT)

    def clearLed(self):
        for pin in self.__mLedPin_list:
            GPIO.output(pin, GPIO.LOW)

    def waitSwitch(self):
        while True:
            if GPIO.input(self.__mInputPin) == GPIO.HIGH:
                break
            time.sleep(0.01)

    def setDiceEyes(self, num):
        self.clearLed()
        for pin in self.__mLedPin_list[:num]:
            GPIO.output(pin, GPIO.HIGH)

    def setErrorPut(self, kind):
        self.clearLed()
        if kind == 0:
            for pin in ERROR_TYPE_RECOG_ERROR:
                GPIO.output(pin, GPIO.HIGH)


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
from bs4 import BeautifulSoup
import requests

lccnetTV='https://www.lccnet.tv/pages/login.aspx'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}

res = requests.get(lccnetTV, headers=headers)
soup = BeautifulSoup(res.text,'html.parser')

VIEWSTATE =soup.select_one("#__VIEWSTATE")["value"]
VIEWSTATEGENERATOR =soup.select_one("#__VIEWSTATEGENERATOR")["value"]
EVENTVALIDATION =soup.select_one("#__EVENTVALIDATION")["value"]

payLoad={'__EVENTTARGET':'',
        '__EVENTARGUMENT':'',
        '__VIEWSTATE':VIEWSTATE,
        '__VIEWSTATEGENERATOR':VIEWSTATEGENERATOR,
        '__EVENTVALIDATION':EVENTVALIDATION,
        'ctl00$header1$QueryTextBox':'',
        'ctl00$ContentPlaceHolder1$login1$IdTextBox':'',
        'ctl00$ContentPlaceHolder1$login1$PasswordTextBox':'',
        'ctl00$ContentPlaceHolder1$login1$OKButton': '登入'}
rs = requests.session()
res = rs.post(lccnetTV, headers=headers, data=payLoad)

Url2='https://www.lccnet.tv/userpages/mycard.aspx'

res = rs.get(Url2)
soup = BeautifulSoup(res.text,'html.parser')
resData = soup.find_all(class_='cardno')
resImg = soup.find_all(class_='box')
for i, j in zip(resData, resImg):
    print(i.text.strip())
    print(j.img.get('src'))
    ImgUrl="https://www.lccnet.tv"+j.img.get('src')
    resp2 = requests.get(ImgUrl, headers=headers)
    if resp2.status_code == 200:
        with open(i.text.strip()[3:]+".png", 'wb') as f:
                for chunk in resp2:
                    f.write(chunk)


import os
from os.path import join, exists
import argparse
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, ConcatDataset, random_split
import random
import json

from dataset import Dictionary, VQAFeatureDataset, VisualGenomeFeatureDataset
from dataset import tfidf_from_questions
from dataset_cp_v2 import VQA_cp_Dataset, Image_Feature_Loader
from model.regat import build_regat
from config.parser import parse_with_config
from train import train
import utils
from utils import trim_collate

def parse_args():
    parser = argparse.ArgumentParser()
    '''
    For training logistics
    '''
    parser.add_argument('--epochs', type=int, default=20)
    parser.add_argument('--base_lr', type=float, default=1e-3)
    parser.add_argument('--lr_decay_start', type=int, default=15)
    parser.add_argument('--lr_decay_rate', type=float, default=0.25)
    parser.add_argument('--lr_decay_step', type=int, default=2)
    parser.add_argument('--lr_decay_based_on_val', action='store_true',
                        help='Learning rate decay when val score descreases')
    parser.add_argument('--grad_accu_steps', type=int, default=1)
    parser.add_argument('--grad_clip', type=float, default=0.25)
    parser.add_argument('--weight_decay', type=float, default=0)
    parser.add_argument('--batch_size', type=int, default=64)
    parser.add_argument('--output', type=str, default='saved_models/')
    parser.add_argument('--save_optim', action='store_true',
                        help='save optimizer')
    parser.add_argument('--log_interval', type=int, default=-1,
                        help='Print log for certain steps')
    parser.add_argument('--seed', type=int, default=-1, help='random seed')

    '''
    loading trained models
    '''
    parser.add_argument('--checkpoint', type=str, default="")

    '''
    For dataset
    '''
    parser.add_argument('--dataset', type=str, default='vqa',
                        choices=["vqa", "vqa_cp"])
    parser.add_argument('--data_folder', type=str, default='./data')
    parser.add_argument('--use_both', action='store_true',
                        help='use both train/val datasets to train?')
    parser.add_argument('--use_vg', action='store_true',
                        help='use visual genome dataset to train?')
    parser.add_argument('--adaptive', action='store_true',
                        help='adaptive or fixed number of regions')
    '''
    Model
    '''
    parser.add_argument('--relation_type', type=str, default='implicit',
                        choices=["spatial", "semantic", "implicit"])
    parser.add_argument('--fusion', type=str, default='mutan',
                        choices=["ban", "butd", "mutan"])
    parser.add_argument('--tfidf', action='store_true',
                        help='tfidf word embedding?')
    parser.add_argument('--op', type=str, default='c',
                        help="op used in tfidf word embedding")
    parser.add_argument('--num_hid', type=int, default=1024)
    '''
    Fusion Hyperparamters
    '''
    parser.add_argument('--ban_gamma', type=int, default=1, help='glimpse')
    parser.add_argument('--mutan_gamma', type=int, default=2, help='glimpse')
    '''
    Hyper-params for relations
    '''
    # hyper-parameters for implicit relation
    parser.add_argument('--imp_pos_emb_dim', type=int, default=64,
                        help='geometric embedding feature dim')

    # hyper-parameters for explicit relation
    parser.add_argument('--spa_label_num', type=int, default=11,
                        help='number of edge labels in spatial relation graph')
    parser.add_argument('--sem_label_num', type=int, default=15,
                        help='number of edge labels in \
                              semantic relation graph')

    # shared hyper-parameters
    parser.add_argument('--dir_num', type=int, default=2,
                        help='number of directions in relation graph')
    parser.add_argument('--relation_dim', type=int, default=1024,
                        help='relation feature dim')
    parser.add_argument('--nongt_dim', type=int, default=20,
                        help='number of objects consider relations per image')
    parser.add_argument('--num_heads', type=int, default=16,
                        help='number of attention heads \
                              for multi-head attention')
    parser.add_argument('--num_steps', type=int, default=1,
                        help='number of graph propagation steps')
    parser.add_argument('--residual_connection', action='store_true',
                        help='Enable residual connection in relation encoder')
    parser.add_argument('--label_bias', action='store_true',
                        help='Enable bias term for relation labels \
                              in relation encoder')

    # can use config files
    parser.add_argument('--config', help='JSON config files')

    args = parse_with_config(parser)
    return args

if __name__ == '__main__':
    args = parse_args()
    if not torch.cuda.is_available():
        raise ValueError("CUDA is not available," +
                         "this code currently only support GPU.")
    n_device = torch.cuda.device_count()
    print("Found %d GPU cards for training" % (n_device))
    device = torch.device("cuda")
    batch_size = args.batch_size*n_device

    torch.backends.cudnn.benchmark = True

    if args.seed != -1:
        print("Predefined randam seed %d" % args.seed)
    else:
        # fix seed
        args.seed = random.randint(1, 10000)
        print("Choose random seed %d" % args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    if "ban" == args.fusion:
        fusion_methods = args.fusion+"_"+str(args.ban_gamma)
    else:
        fusion_methods = args.fusion

    dictionary = Dictionary.load_from_file(
                    join(args.data_folder, 'glove/dictionary.pkl'))
    if args.dataset == "vqa_cp":
        coco_train_features = Image_Feature_Loader(
                            'train', args.relation_type,
                            adaptive=args.adaptive, dataroot=args.data_folder)
        coco_val_features = Image_Feature_Loader(
                            'val', args.relation_type,
                            adaptive=args.adaptive, dataroot=args.data_folder)
        val_dset = VQA_cp_Dataset(
                    'test', dictionary, coco_train_features, coco_val_features,
                    adaptive=args.adaptive, pos_emb_dim=args.imp_pos_emb_dim,
                    dataroot=args.data_folder)
        train_dset = VQA_cp_Dataset(
                    'train', dictionary, coco_train_features,
                    coco_val_features, adaptive=args.adaptive,
                    pos_emb_dim=args.imp_pos_emb_dim,
                    dataroot=args.data_folder)
    else:
        val_dset = VQAFeatureDataset(
                'val', dictionary, args.relation_type, adaptive=args.adaptive,
                pos_emb_dim=args.imp_pos_emb_dim, dataroot=args.data_folder)
        train_dset = VQAFeatureDataset(
                'train', dictionary, args.relation_type,
                adaptive=args.adaptive, pos_emb_dim=args.imp_pos_emb_dim,
                dataroot=args.data_folder)

    model = build_regat(val_dset, args).to(device)

    tfidf = None
    weights = None
    if args.tfidf:
        tfidf, weights = tfidf_from_questions(['train', 'val', 'test2015'],
                                              dictionary)
    model.w_emb.init_embedding(join(args.data_folder,
                                    'glove/glove6b_init_300d.npy'),
                               tfidf, weights)

    model = nn.DataParallel(model).to(device)

    if args.checkpoint != "":
        print("Loading weights from %s" % (args.checkpoint))
        if not os.path.exists(args.checkpoint):
            raise ValueError("No such checkpoint exists!")
        checkpoint = torch.load(args.checkpoint)
        state_dict = checkpoint.get('model_state', checkpoint)
        matched_state_dict = {}
        unexpected_keys = set()
        missing_keys = set()
        for name, param in model.named_parameters():
            missing_keys.add(name)
        for key, data in state_dict.items():
            if key in missing_keys:
                matched_state_dict[key] = data
                missing_keys.remove(key)
            else:
                unexpected_keys.add(key)
        print("Unexpected_keys:", list(unexpected_keys))
        print("Missing_keys:", list(missing_keys))
        model.load_state_dict(matched_state_dict, strict=False)

    # use train & val splits to optimize, only available for vqa, not vqa_cp
    if args.use_both and args.dataset == "vqa":
        length = len(val_dset)
        trainval_concat_dset = ConcatDataset([train_dset, val_dset])
        if args.use_vg or args.use_visdial:
            trainval_concat_dsets_split = random_split(
                                trainval_concat_dset,
                                [int(0.2*length),
                                 len(trainval_concat_dset)-int(0.2*length)])
        else:
            trainval_concat_dsets_split = random_split(
                                trainval_concat_dset,
                                [int(0.1*length),
                                 len(trainval_concat_dset)-int(0.1*length)])
        concat_list = [trainval_concat_dsets_split[1]]

        # use a portion of Visual Genome dataset
        if args.use_vg:
            vg_train_dset = VisualGenomeFeatureDataset(
                        'train', train_dset.features, train_dset.normalized_bb,
                        train_dset.bb, train_dset.spatial_adj_matrix,
                        train_dset.semantic_adj_matrix, dictionary,
                        adaptive=train_dset.adaptive,
                        pos_boxes=train_dset.pos_boxes,
                        dataroot=args.data_folder)
            vg_val_dset = VisualGenomeFeatureDataset(
                            'val', val_dset.features, val_dset.normalized_bb,
                            val_dset.bb, val_dset.spatial_adj_matrix,
                            val_dset.semantic_adj_matrix, dictionary,
                            adaptive=val_dset.adaptive,
                            pos_boxes=val_dset.pos_boxes,
                            dataroot=args.data_folder)
            concat_list.append(vg_train_dset)
            concat_list.append(vg_val_dset)
        final_train_dset = ConcatDataset(concat_list)
        final_eval_dset = trainval_concat_dsets_split[0]
        train_loader = DataLoader(final_train_dset, batch_size, shuffle=True,
                                  num_workers=4, collate_fn=trim_collate)
        eval_loader = DataLoader(final_eval_dset, batch_size,
                                 shuffle=False, num_workers=4,
                                 collate_fn=trim_collate)

    else:
        train_loader = DataLoader(train_dset, batch_size, shuffle=True,
                                  num_workers=4, collate_fn=trim_collate)
        eval_loader = DataLoader(val_dset, batch_size, shuffle=False,
                                 num_workers=4, collate_fn=trim_collate)

    output_meta_folder = join(args.output, "regat_%s" % args.relation_type)
    utils.create_dir(output_meta_folder)
    args.output = output_meta_folder+"/%s_%s_%s_%d" % (
                fusion_methods, args.relation_type,
                args.dataset, args.seed)
    if exists(args.output) and os.listdir(args.output):
        raise ValueError("Output directory ({}) already exists and is not "
                         "empty.".format(args.output))
    utils.create_dir(args.output)
    with open(join(args.output, 'hps.json'), 'w') as writer:
        json.dump(vars(args), writer, indent=4)
    logger = utils.Logger(join(args.output, 'log.txt'))

    train(model, train_loader, eval_loader, args, device)


from sklearn.metrics import classification_report
import torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def evaluate_model(model, dataloaders, class_names, phase='test'):
    model.eval()
    y_pred = []
    y_true = []

    with torch.no_grad():
        for inputs, labels in dataloaders[phase]:
            inputs = inputs.to(device)
            labels = labels.to(device)

            outputs = model(inputs)
            _, preds = torch.max(outputs, 1)

            y_pred.extend(preds.view(-1).tolist())
            y_true.extend(labels.view(-1).tolist())

    print(classification_report(y_true, y_pred, target_names=class_names))


import numpy as np
import matplotlib.pyplot as plt
import h5py
def V_x(x, y):
    return 0.5*(2*x+4*x*y)
def V_y(x, y):
    return 0.5*(2*y+2*x**2-2*y**2)

def time_derivative(r):
    x, y, vx, vy = r
    return np.array([vx, vy, -V_x(x,y), -V_y(x,y)])

def RK4_step(r, dt, f=time_derivative):
    k1 = f(r)
    k2 = f(r+k1*dt/2)
    k3 = f(r+k2*dt/2)
    k4 = f(r+k3*dt)
    return r + dt/6*(k1+2*(k2+k3)+k4)

def E(r):
    x, y, vx, vy = r
    return 0.5*(x**2 + y**2 + 2*x**2*y - 2*y**3/3 + vx**2 + vy**2)

NT = 100000
dt=0.1

def run(r, filename="HenonHeiles.hdf5", NT=NT, dt=dt, debug=False):
    r_history=np.zeros((NT, *r.shape))
    E_start = E(r)
    if debug:
        print(E_start)
    for i in range(NT):
        r_history[i]=r
        r = RK4_step(r, dt)
    E_finish = E(r)
    if debug:
        print(E_finish)

    with h5py.File(filename) as f:
        f.create_dataset("r_h_{}".format(E_start), data=r_history)
        f.attrs['E_s_{}'.format(E_start)] = E_start
        f.attrs['E_f_{}'.format(E_start)] = E_finish

    return r_history

def find_minima(x, max_distance, where):
    indices = np.abs(x-where) < np.max(np.abs(x-where))*max_distance
    return indices

def plot_all_from_file(filename="HenonHeiles.hdf5", max_distance=0.01, where=0):
    with h5py.File(filename) as f:
        for dataset in f:
            x = f[dataset][:,0]
            y = f[dataset][:,1]
            vx = f[dataset][:,2]
            vy = f[dataset][:,3]

            indices = find_minima(x, max_distance, where)
            plt.plot(y[indices], vy[indices], "o")
    plt.xlabel('y')
    plt.ylabel('vy')
    plt.title('Przekroj Poincare, rownanie Henona-Heilesa, x={}'.format(where))
    plt.grid()
    plt.show()

# def solve_y(C):
#   podpierwiastkiem = 2*np.sqrt(9*C**2 - 3*C) - 6*C +1
#   return 0.5 * (1 + podpierwiastkiem**(1/3) + podpierwiastkiem**(-1/3))

if __name__=="__main__":
    # En = 1/8
    plot_all_from_file("HenonHeiles.hdf5", where=0)
    # for i in range(100):
    #     print(i)
    #     r = np.random.random(4)*0.25
    #     # r = np.zeros(4)
    #     # xdot, ydot = np.random.random(2)*0.25
    #     # C = 2*En - 0.5*(xdot**2 + ydot**2)
    #     # print(C)
    #     # r[1] = solve_y(C)
    #     # if np.isnan(r[1]):
    #     #     continue
    #     # r[2:] = xdot, ydot
    #     # print(r)
    #     # #pick xdot, vdot at random
    #     # #E = 0.5 (y**2 - 2/3 y**3 + Venergy)
    #     # #2E - V_energy = y**2 - 2/3 y**3
    #     # #def energy(y, constant = 2E-V_energy):
    #     # #   return y**2 - 2/3*y**3 - constant
    #     # # http://www.wolframalpha.com/input/?i=solve+y%5E2+-+2%2F3*y%5E3+-+C+%3D+0+for+y
    #
    #     run(r, filename="HenonHeiles.hdf5")


# -*- coding: utf-8 -*-
r"""Plot visit output

This exports:

  -

This depends on:

  -
Examples of usage:

  - default usage:

        python -m

descriptions
"""
import numpy as np
import sys, os, argparse
# import pathlib
import numpy as np
import shilofue.ParsePrm as ParsePrm
import shilofue.PlotVisit as PlotVisit

# directory to the aspect Lab
ASPECT_LAB_DIR = os.environ['ASPECT_LAB_DIR']
RESULT_DIR = os.path.join(ASPECT_LAB_DIR, 'results')
# directory to shilofue
shilofue_DIR = os.path.join(ASPECT_LAB_DIR, 'shilofue')

sys.path.append(os.path.join(ASPECT_LAB_DIR, 'utilities', "python_scripts"))
import Utilities

def Usage():
    print("\
Visualization with visit\n\
\n\
Examples of usage: \n\
\n\
  - translate script: \n\
\n\
        python -m shilofue.TwoDSubduction0.PlotVisit visit_options -i $TwoDSubduction_DIR/latent_heat_issue/cookbook_latent-heat -sr temperature.py\n\
            -sr: the relative path under the visit_script folder\n\
\n\
  - run script: \n\
        python -m shilofue.TwoDSubduction0.PlotVisit run -i $TwoDSubduction_DIR/non_linear34/eba_low_tol_newton_shift_CFL0.8/visit_scripts/slab.py\n\
\n\
  - run vtk scripts: \n\
        python -m shilofue.TwoDSubduction0.PlotVisit vtk_options -i /home/lochy/ASPECT_PROJECT/TwoDSubduction/non_linear34/eba_low_tol_newton_shift_CFL0.8_lh -p TwoDSubduction_MOW -s 25\n\
            -p operations, available operations are: \n\
                TwoDSubduction_MOW: pull out tentative MOW from temperature: \n\
                TwoDSubduction_SlabAnalysis: analyze slab morphology: \n\
        ")

class VISIT_OPTIONS(PlotVisit.VISIT_OPTIONS):
    """
    parse .prm file to a option file that bash can easily read
    """
    def Interpret(self, **kwargs):
        """
        Interpret the inputs, to be reloaded in children
        kwargs: options
            last_step(list): plot the last few steps
        """
        # call function from parent
        PlotVisit.VISIT_OPTIONS.Interpret(self, **kwargs)

        # default settings
        self.options['IF_PLOT_SHALLOW'] = kwargs.get('if_plot_shallow', "False") # plot the shallow part of the slab.
        self.options['IF_EXPORT_SLAB_MORPH'] = 'False'
        self.options['IF_PLOT_SLAB'] = 'True'
        self.options['GLOBAL_UPPER_MANTLE_VIEW_BOX'] = 0.0
        self.options['ROTATION_ANGLE'] = 0.0
        # try using the value for the background
        try:
            self.options['ETA_MIN'] =\
                Utilities.string2list(self.idict['Material model']['Visco Plastic TwoD']['Minimum viscosity'], float)[0]
        except ValueError:
            eta_min_inputs =\
                ParsePrm.COMPOSITION(self.idict['Material model']['Visco Plastic TwoD']['Minimum viscosity'])
            self.options['ETA_MIN'] = eta_min_inputs.data['background'][0] # use phases
        try:
            self.options['ETA_MAX'] =\
                Utilities.string2list(self.idict['Material model']['Visco Plastic TwoD']['Maximum viscosity'], float)[0]
        except ValueError:
            eta_max_inputs =\
                ParsePrm.COMPOSITION(self.idict['Material model']['Visco Plastic TwoD']['Maximum viscosity'])
            self.options['ETA_MAX'] = eta_max_inputs.data['background'][0] # use phases
        # self.options['IF_DEFORM_MECHANISM'] = value.get('deform_mechanism', 0)
        self.options['IF_DEFORM_MECHANISM'] = 1
        # rotation of the domain
        if self.options['GEOMETRY'] == 'chunk':
            try:
                index = ParsePrm.FindWBFeatures(self.wb_dict, 'Subducting plate')
            except KeyError:
                # either there is no wb file found, or the feature 'Subducting plate' is not defined
                rotation_angle = 52.0
            else:
                # rotate to center on the slab
                feature_sp = self.wb_dict['features'][index]
                rotation_angle = 90.0 - feature_sp["coordinates"][2][0] - 2.0
            self.options['ROTATION_ANGLE'] = rotation_angle
        elif self.options['GEOMETRY'] == 'box':
            try:
                index = ParsePrm.FindWBFeatures(self.wb_dict, 'Subducting plate')
            except KeyError:
                # either there is no wb file found, or the feature 'Subducting plate' is not defined
                trench_x = 4e6
            else:
                # reset the view point
                feature_sp = self.wb_dict['features'][index]
                trench_x = feature_sp["coordinates"][2][0]
            window_width = 1.8e6
            self.options['GLOBAL_UPPER_MANTLE_VIEW_BOX'] =\
            "(%.4e, %.4e, 1.9e6, 2.9e6)" % (trench_x - window_width/2.0, trench_x + window_width/2.0)
        else:
            raise ValueError("Geometry should be \"chunk\" or \"box\"")
        # Slab configuration
        index = ParsePrm.FindWBFeatures(self.wb_dict, 'Subducting plate')
        feature_sp = self.wb_dict['features'][index]
        self.options["INITIAL_SHEAR_ZONE_THICKNESS"] = feature_sp["composition models"][0]["max depth"]

        # peierls rheology
        try:
            include_peierls_rheology = self.idict['Material model']['Visco Plastic TwoD']['Include Peierls creep']
            if include_peierls_rheology == 'true':
                self.options['INCLUDE_PEIERLS_RHEOLOGY'] = True
            else:
                self.options['INCLUDE_PEIERLS_RHEOLOGY'] = False
        except KeyError:
            self.options['INCLUDE_PEIERLS_RHEOLOGY'] = False

    def vtk_options(self, **kwargs):
        '''
        options of vtk scripts
        '''
        # call function from parent
        PlotVisit.VISIT_OPTIONS.vtk_options(self, **kwargs)
        # reference trench point
        self.options['THETA_REF_TRENCH'] = 0.0  # initial value
        if self.options['GEOMETRY'] == 'chunk':
            try:
                index = ParsePrm.FindWBFeatures(self.wb_dict, 'Subducting plate')
            except KeyError:
                # either there is no wb file found, or the feature 'Subducting plate' is not defined
                theta_ref_trench = 0.63
            else:
                # rotate to center on the slab
                feature_sp = self.wb_dict['features'][index]
                theta_ref_trench = feature_sp["coordinates"][2][0] / 180.0 * np.pi
        elif self.options['GEOMETRY'] == 'box':
            try:
                index = ParsePrm.FindWBFeatures(self.wb_dict, 'Subducting plate')
            except KeyError:
                # either there is no wb file found, or the feature 'Subducting plate' is not defined
                # for the box geometry, this is the x distance of the trench
                theta_ref_trench = 4000000.0
            else:
                # rotate to center on the slab
                feature_sp = self.wb_dict['features'][index]
                theta_ref_trench = feature_sp["coordinates"][2][0]
        else:
            raise ValueError("Value of geometry must be either \"chunk\" or \"box\"")
        self.options['THETA_REF_TRENCH'] = theta_ref_trench

    def get_snaps_for_slab_morphology(self, **kwargs):
        '''
        get the snaps for processing slab morphology
        kwargs (dict):
            time_interval (float)
        '''
        ptime_start = kwargs.get('time_start', None)
        ptime_interval = kwargs.get('time_interval', None)
        ptime_end = kwargs.get('time_end', None)
        assert(ptime_interval is None or type(ptime_interval) == float)
        # steps for processing slab morphology
        snaps, times, _ = PlotVisit.GetSnapsSteps(self._case_dir, 'graphical')
        psnaps = []
        ptimes = []
        last_time = -1e8  # initiate as a small value, so first step is included
        # loop within all the available steps, find steps satisfying the time interval requirement.
        for i in range(len(times)):
            time = times[i]
            snap = snaps[i]
            if ptime_start is not None and time < ptime_start:
                # check on the start
                continue
            if ptime_end is not None and time > ptime_end:
                # check on the end
                break
            if type(ptime_interval) == float:
                if (time - last_time) < ptime_interval:
                    continue  # continue if interval is not reached
            pvtu_file_path = os.path.join(self.options["DATA_OUTPUT_DIR"], "solution", "solution-%05d.pvtu" % snap)
            if os.path.isfile(pvtu_file_path):
                # append if the file is found
                last_time = time
                psnaps.append(snap)
                ptimes.append(time)
        return psnaps

class PREPARE_RESULT_OPTIONS(PlotVisit.PREPARE_RESULT_OPTIONS):
    """
    parse .prm file to a option file that bash can easily read
    """
    def Interpret(self, **kwargs):
        """
        Interpret the inputs, to be reloaded in children
        kwargs: options
        """
        # call function from parent
        PlotVisit.PREPARE_RESULT_OPTIONS.Interpret(self, **kwargs)

def main():
    '''
    main function of this module
    Inputs:
        sys.arg[1](str):
            commend
        sys.arg[2, :](str):
            options
    '''
    _commend = sys.argv[1]
    # parse options
    parser = argparse.ArgumentParser(description='Parse parameters')
    parser.add_argument('-i', '--inputs', type=str,
                        default='',
                        help='Some inputs')
    parser.add_argument('-sr', '--script', type=str,
                        default='temperature.py',
                        help='script')
    parser.add_argument('-j', '--json_file', type=str,
                        default='./config_case.json',
                        help='Filename for json file')
    parser.add_argument('-p', '--operation', type=str,
                        default='',
                        help='operation to take')
    parser.add_argument('-s', '--step', type=int,
                        default='0',
                        help='step')
    parser.add_argument('-ls', '--last_step', type=bool,
                        default=False,
                        help='Only plot last step (bool value)')
    _options = []
    try:
        _options = sys.argv[2: ]
    except IndexError:
        pass
    arg = parser.parse_args(_options)

    # commands
    if _commend in ['-h', '--help']:
        # example:
        Usage()

    elif _commend == 'visit_options':
        # output bash options to a file that could be
        # read by bash script
        # initiate class object
        case_dir = arg.inputs
        Visit_Options = VISIT_OPTIONS(case_dir)
        # call function
        Visit_Options.Interpret()
        # ofile = os.path.join('visit_scripts', 'slab_sph.py')
        ofile = os.path.join('visit_scripts', os.path.basename(arg.script))
        visit_script = os.path.join(ASPECT_LAB_DIR, 'visit_scripts', arg.script)
        visit_base_script = os.path.join(ASPECT_LAB_DIR, 'visit_scripts', 'base.py')  # base.py : base file
        Visit_Options.read_contents(visit_base_script, visit_script)  # this part combines two scripts
        Visit_Options.substitute()  # substitute keys in these combined file with values determined by Interpret() function
        ofile_path = Visit_Options.save(ofile, relative=True)  # save the altered script
        pass

    elif _commend == 'vtk_options':
        vtk_option_path, _, _ = PlotVisit.PrepareVTKOptions(VISIT_OPTIONS, arg.inputs, arg.operation, step=arg.step)
        PlotVisit.RunVTKScripts(arg.operation, vtk_option_path)

    elif _commend == 'run':
        PlotVisit.RunScripts(arg.inputs)

    else:
        # no such option, give an error message
        raise ValueError('No commend called %s, please run -h for help messages' % _commend)

# run script
if __name__ == '__main__':
    main()