import pandas as pd
#import import_ipynb
import data_aggregation_tools as da
import streamlit as st

def format_money(value):
    if abs(value) >= 1e6:
        return '{:.2f}M'.format(value / 1e6)
    elif abs(value) >= 1e3:
        return '{:.2f}K'.format(value / 1e3)
    else:
        return '{:.2f}'.format(value)

def format_money_USD(value):
    if abs(value) >= 1e6:
        return '${:.2f}M'.format(value / 1e6)
    elif abs(value) >= 1e3:
        return '${:.2f}K'.format(value / 1e3)
    else:
        return '${:.2f}'.format(value)

def read_clean_data():
    """read clean data from csv files"""
    dtypes = { 'USD': 'float64', 'Category': 'str' }

    large_donations_by_category = pd.read_csv('data/large_donations_by_category.csv', dtype=dtypes, parse_dates=['Date'])
    large_spending_by_category = pd.read_csv('data/large_spending_by_category.csv', dtype=dtypes, parse_dates=['Date'])

    donations_below_large_by_category = pd.read_csv('data/donations_below_large_by_category.csv', dtype=dtypes, parse_dates=['Date'])
    spending_below_large_by_category = pd.read_csv('data/spending_below_large_by_category.csv', dtype=dtypes, parse_dates=['Date'])

    donations_total = pd.read_csv('data/donations_total.csv', dtype=dtypes, parse_dates=['Date'])
    spending_total =  pd.read_csv('data/spending_total.csv', dtype=dtypes, parse_dates=['Date'])

    donations_total_by_category = pd.read_csv('data/donations_total_by_category.csv', dtype=dtypes, parse_dates=['Date'])
    spending_total_by_category = pd.read_csv('data/spending_total_by_category.csv', dtype=dtypes, parse_dates=['Date'])

    donations_total_by_category = pd.read_csv('data/donations_total_by_category.csv', dtype=dtypes, parse_dates=['Date'])
    spending_total_by_category = pd.read_csv('data/spending_total_by_category.csv', dtype=dtypes, parse_dates=['Date'])

    return large_donations_by_category, large_spending_by_category, donations_below_large_by_category, spending_below_large_by_category, \
            donations_total, spending_total, donations_total_by_category, spending_total_by_category

@st.cache_data(ttl=24*60*60)
def ETL_raw_data(nrows = None):
    '''Extract and transform data and save it to csv files'''
    if nrows == None:
            df = pd.read_csv('./data/DIGNITAS UKRAINE INC_Transactions for Dashboard.csv',
                        index_col=None,
                        usecols = ['Account', 'Date', 'Transaction type', 'Name', 'Amount'],
                        dtype={ 'Transaction type': 'category', 'Name': 'string', 'Account': 'string'}, parse_dates=['Date'])
    else:
        return None

    df['Amount'] = df['Amount'].replace({'\$': '', ',': '', '\(': '', '\)': ''}, regex=True).astype(float)
    df = df[df['Transaction type'] != 'Transfer']
    df = df[df['Account'] != 'FIVE % FOR ADMIN EXPENSES']

    # in-kind donations
    #df = df[df['Account name'] != 'IN-KIND DONATION DISTRIBUTIONS']
    #df_inkind = df[df['Account name'] == 'In-kind donations']
    #df = df[df['Account name'] != 'In-kind donations']

    # investments
    #df_inv = df[df['Account name'] == 'DIVIDENT RECEIVED FIDELITY']

    #df = df[df['Account name'] != 'DIVIDENT RECEIVED FIDELITY']
    #df = df[df['Account name'] != 'FIDELITY INVESTMENTS']

    # spending
    ds = df[df['Transaction type'] != 'Deposit']
    ds = ds[ds['Transaction type'] != 'Journal Entry']
    ds = ds[ds['Account'] != 'Donations directed by individuals']
    ds = ds[ds['Account'] != 'CHASE ENDING IN 5315']
    ds = ds[ds['Account'] != 'PayPal Bank 3']
    ds = ds[ds['Account'] != 'DIVIDENT RECEIVED FIDELITY']

    # donations
    df = df[df['Account'] == 'Donations directed by individuals']
    df = df[df['Transaction type'] == 'Deposit']

    # Define the mapping of old column names to new column names
    column_mapping = {'Name': 'Category', 'Amount': 'USD'}
    # Use the mapping to rename the columns
    df.rename(columns=column_mapping, inplace=True)
    #df_inkind.rename(columns=column_mapping, inplace=True)

    column_mapping = {'Account': 'Category', 'Amount': 'USD'}
    ds.rename(columns=column_mapping, inplace=True)

    column_mapping = {'Name': 'Category', 'Amount line': 'USD'}
    df.rename(columns=column_mapping, inplace=True)
    df = df[['Date', 'Category', 'USD']]

    column_mapping = {'Account name': 'Category', 'Amount line': 'USD'}
    ds.rename(columns=column_mapping, inplace=True)
    ds = ds[['Date', 'Category', 'USD']]

    value_mapping = {'02 GENERAL BANK URESTR': 'General donations', 'PAY PALL UNRESTRICTED': 'General donations',
                '03 FUNDRASING EVENTS UNRESTR': 'General donations', '04 PAYPAL GENERAL UNRESTR': 'General donations',
                '05 VENMO UNRESTR': 'General donations', '06 SQUARE UNRESTR': 'General donations',
                '07 1000 DRONES RESTR': '1000 Drones for Ukraine', '09 MSU RESTR': 'Mobile Shower Units',
                '11 VETERANIUS RESTR' : 'Veteranius', '08 VICTORY DRONES RESTR': 'Victory Drones', '10 Flight to Recovery RESTR': 'Flight to Recovery'}
    df['Category'] = df['Category'].replace(value_mapping)
    df['Category'] = df['Category'].str.title()

    ds['Category'] = ds['Category'].str.title()
    value_mapping = {'Car Purchases': 'Admin', 'Lodging': 'Admin',
                'Transportation And Parking': 'Admin', 'Events Participation Expenses': 'Admin',
                'Legal Fees': 'Admin', 'Software & Apps': 'Admin',
                'Maling And Delivery': 'Admin', 'Bank Fees & Service Charges': 'Admin', 'Chase Bank': 'Admin',
                '01 Administrative Account': 'Admin', 'Quickbooks Payments': 'Admin',
                'Ngoptics' : 'Drone purchases', 'Collegiate Productions' : 'Admin', 'Supplies & Materials': 'Admin'}

    ds['Category'] = ds['Category'].replace(value_mapping)
    ds['USD'] = ds['USD'].abs()

    donations_total_by_category = df.groupby(['Date', 'Category']).sum().reset_index()
    donations_total_by_category.to_csv('data/donations_total_by_category.csv', index=False)

    spending_total_by_category = ds.groupby(['Date', 'Category']).sum().reset_index()
    spending_total_by_category.to_csv('data/spending_total_by_category.csv', index=False)

    donations_total = df.drop('Category', axis=1).groupby('Date').sum().reset_index()

    donations_total.to_csv('data/donations_total.csv', index=False)

    spending_total = ds.drop('Category', axis=1).groupby('Date').sum().reset_index()
    spending_total.to_csv('data/spending_total.csv', index=False)

    # above 10K
    amount = 10000
    large_donations = df[df['USD'] >= amount].fillna('')
    large_donations_by_category = large_donations.groupby(['Date', 'Category']).sum().reset_index()
    large_donations_by_category.to_csv('data/large_donations_by_category.csv', index=False)

    large_spending = ds[ds['USD'] >= amount].fillna('')
    large_spending_by_category = large_spending.groupby(['Date', 'Category']).sum().reset_index()
    large_spending_by_category.to_csv('data/large_spending_by_category.csv', index=False)

    # below 10K
    donations_below_large_by_category = df[df.USD < amount]
    spending_below_large_by_category = ds[ds.USD < amount]

    donations_below_large_by_category = donations_below_large_by_category.groupby(['Date', 'Category']).sum().reset_index()
    spending_below_large_by_category = spending_below_large_by_category.groupby(['Date', 'Category']).sum().reset_index()

    donations_below_large_by_category.to_csv('data/donations_below_large_by_category.csv', index=False)
    spending_below_large_by_category.to_csv('data/spending_below_large_by_category.csv', index=False)

    #return df, ds


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 21:16:14 2018

@author: ahanmr
"""

import matplotlib.pyplot as plt
import pandas as pd
df1=pd.read_csv("AAPL_data.csv")
df2=pd.read_csv("AAL_data.csv")
# Plot the Adj_Close columns

df1['close'].plot(label='AAP')
df2['close'].plot(label='AAL')
plt.show()
# Histogram of the daily price change percent of Adj_Close for AAPL and AAL
#Use subplots for above to show both plots

plt.subplot(2,1,1)
df1["close"].pct_change().plot.hist(bins=50)
plt.xlabel("AAPL")

plt.subplot(2, 1, 2)
df2["close"].pct_change().plot.hist(bins=50)
plt.xlabel("AAL")
plt.show()
#10 days future based on shifting by 10 days
df1['5d_future_close']=df1['close'].shift(-10)
df1['5d_future_close_pct']=df1['5d_future_close'].pct_change(10)
df1['close_pct']=df1['close'].pct_change(10)
corr=df1[['close_pct','5d_future_close_pct']].corr()
print(corr)
plt.subplot(2,1,1)
plt.scatter(df1['close_pct'],df1['5d_future_close_pct'])

df2['5d_future_close']=df2['close'].shift(-10)
df2['5d_future_close_pct']=df2['5d_future_close'].pct_change(10)
df2['close_pct']=df2['close'].pct_change(10)
corr=df2[['close_pct','5d_future_close_pct']].corr()
print(corr)
plt.subplot(2,1,2)
plt.scatter(df2['close_pct'],df2['5d_future_close_pct'])
#plt.show()

#plt.clf()


from random import random
from ray.rllib import MultiAgentEnv
from supersuit import frame_stack_v1, resize_v0, frame_skip_v0, agent_indicator_v0
# from pettingzoo.atari import pong_v1, boxing_v1
from ray.tune import register_env

PLAYER_1_ID = 'first_0'
PLAYER_2_ID = 'second_0'

class ParallelPettingZooEnv(MultiAgentEnv):
    """ This class makes sure that pettingzoo's envs can be used in combination with
    ray. Some parts of this code are from one of PettingZoo's repositories. """

    def __init__(self, env, random_action=None, random_proba=0.05):
        self.par_env = env
        # agent idx list
        self.agents = self.par_env.possible_agents

        # Random actions to get the environment unstuck.
        self.random_action = random_action
        self.random_proba = random_proba

        # Get dictionaries of obs_spaces and act_spaces
        self.observation_spaces = self.par_env.observation_spaces
        self.action_spaces = self.par_env.action_spaces

        # Get first observation space, assuming all agents have equal space
        self.observation_space = self.observation_spaces[self.agents[0]]

        # Get first action space, assuming all agents have equal space
        self.action_space = self.action_spaces[self.agents[0]]

        assert all(obs_space == self.observation_space
                   for obs_space
                   in self.par_env.observation_spaces.values()), \
            "Observation spaces for all agents must be identical. Perhaps " \
            "SuperSuit's pad_observations wrapper can help (useage: " \
            "`supersuit.aec_wrappers.pad_observations(env)`"

        assert all(act_space == self.action_space
                   for act_space in self.par_env.action_spaces.values()), \
            "Action spaces for all agents must be identical. Perhaps " \
            "SuperSuit's pad_action_space wrapper can help (useage: " \
            "`supersuit.aec_wrappers.pad_action_space(env)`"

        self.metadata = {"render.modes": ["rgb_array"]}
        self.reset()

    def reset(self):
        """ Reset the environment. """
        return self.par_env.reset()

    def step(self, action_dict):
        """ Take one step in the environment. """

        # Take `random_action` to get the environment unstuck.
        if self.random_action and random() < self.random_proba:
            action_dict = {
                PLAYER_1_ID: self.random_action,
                PLAYER_2_ID: self.random_action
            }
        for agent in self.agents:
            if agent not in action_dict:
                action_dict[agent] = self.action_space.sample()
        aobs, arew, adones, ainfo = self.par_env.step
        obss = {}
        rews = {}
        dones = {}
        infos = {}
        for agent in action_dict:
            # sometimes Joust does not include one of the obs
            if agent not in aobs:
                for agent in action_dict:
                    rews[agent] = 0
                    dones[agent] = True
                break

            obss[agent] = aobs[agent]
            rews[agent] = arew[agent]
            dones[agent] = adones[agent]
            infos[agent] = ainfo[agent]
        self.rewards = rews
        self.dones = dones
        self.infos = infos
        dones["__all__"] = all(adones.values())
        return obss, rews, dones, infos

    def close(self):
        """ Close the environment """
        self.par_env.close()

    def seed(self, seed=None):
        """ Seed the environment """
        self.par_env.seed(seed)

    def render(self, mode="human"):
        """ Render the game frame """
        return self.par_env.render(mode)

def register_pettingzoo_env(env_name):
    """ Register the Env including preprocessing pipeline, s.t. it can be easily
    imported using ray. """

    def get_env(config):
        name = env_name.replace('-', '_')
        env = __import__(f'pettingzoo.atari.{name}', fromlist=[None])
        env = env.parallel_env(obs_type='grayscale_image')
        env = frame_skip_v0(env, 4)
        env = resize_v0(env, 84, 84)
        env = frame_stack_v1(env, 4)
        env = agent_indicator_v0(env)
        return ParallelPettingZooEnv(env,
                                     random_action=config['random_action'],
                                     random_proba=config['random_action_probability'])

    print(f'Registering env with name {env_name}')
    register_env(env_name, lambda config: get_env(config))


import datetime

from django.core.paginator import Paginator
from django.db.models import Q, Prefetch
from redis import Redis

from apiWeb.models import *
from .models import *

class Util_App:

    @staticmethod
    def get_home_slider():
        slider = {
            "slider_video": Util_App.get_slider_video(True),
            "slider_song": Util_App.get_slider_song(True),
            "slider_ads": Util_App.get_home_slider_ads(),
        }
        return slider

    @staticmethod
    def get_slider_video(is_home: bool = False):
        try:
            slider_video = []
            if is_home:
                slider_video_items = SliderHome.objects.prefetch_related("VideoSlider__video").filter(
                    type=SliderType.video.value,
                    VideoSlider__is_active=True,
                    VideoSlider__video__is_active_link=True,
                    VideoSlider__video__is_active_image=True, )
                for slider_video_item in slider_video_items:
                    video_slider = slider_video_item.VideoSlider
                    if video_slider:
                        video = video_slider.video
                        slider_video.append(dict(video_id=video.video_id,
                                                 image=video_slider.image_link,
                                                 fa_name=video.fa_name,
                                                 en_name=video.en_name))
            else:
                video_sliders = SliderVideo.objects.select_related("video").filter(is_show=True,
                                                                                is_childish=False,
                                                                                is_active=True,
                                                                                video__is_active_link=True,
                                                                                video__is_active_image=True, )
                if video_sliders:
                    for video_slider in video_sliders:
                        video = video_slider.video
                        slider_video.append(dict(video_id=video.video_id,
                                                 image=video_slider.image_link,
                                                 fa_name=video.fa_name,
                                                 en_name=video.en_name))
            return slider_video
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_slider_video_child(is_home: bool = False):
        try:
            slider_video_child = []
            if is_home:
                slider_video_child_items = SliderHome.objects.prefetch_related("VideoSlider__video").filter(
                    type=SliderType.video_child.value,
                    VideoSlider__is_active=True,
                    VideoSlider__is_childish=True,
                    VideoSlider__video__is_active_link=True,
                )
                for slider_video_child_item in slider_video_child_items:
                    video_slider = slider_video_child_item.VideoSlider
                    if video_slider:
                        video = video_slider.video
                        images = list(VideoImages.objects.filter(video=video, is_sticker=False,
                                                                 is_active=True).order_by("image_id"))
                        if images.__len__() >= 3:
                            images = images[:3]
                            image = (dict(image_id=image.image_id,
                                          link=image.link) for image in images)
                            slider_video_child.append(dict(video_id=int(video.video_id),
                                                           image_link=str(video_slider.image_link),
                                                           fa_name=str(video.fa_name),
                                                           des=str(video.description),
                                                           link=str(video.link),
                                                           images=image))
                return slider_video_child
            else:
                video_sliders = SliderVideo.objects.filter(is_show=True,
                                                           is_childish=True,
                                                           video__is_active_link=True,
                                                           is_active=True, )
                if video_sliders:
                    for video_slider in video_sliders:
                        video = video_slider.video
                        images = list(VideoImages.objects.filter(video=video, is_sticker=False,
                                                                 is_active=True).order_by("-image_id"))
                        if images.__len__() >= 3:
                            images = images[:3]
                            image = list(map(lambda image: dict(image_id=image.image_id,
                                                                link=image.link), images
                                             ))
                            slider_video_child.append(dict(video_id=int(video.video_id),
                                                           image_link=str(video_slider.image_link),
                                                           fa_name=str(video.fa_name),
                                                           description=str(video.description),
                                                           link=str(video.link),
                                                           images=image))
            return slider_video_child
        except:
            return []

    @staticmethod
    def get_video(video: Videos):
        home_video = {"video_id": int(video.video_id),
                      "fa_name": str(video.fa_name),
                      "en_name": str(video.en_name),
                      "image_link": str(video.image_link)}
        return home_video

    @staticmethod
    def get_new_videos(videos: Videos = None):
        if not videos:
            videos = Videos.objects.filter(is_childish=False, is_active_link=True,
                                           is_active_image=True).distinct().order_by('-video_id')
        home_videos = map(lambda video: Util_App.get_video(video), videos[:10]) if videos else []
        return home_videos

    @staticmethod
    def get_most_visited_videos(videos: Videos = None):
        if not videos:
            videos = Videos.objects.filter(is_childish=False, is_active_link=True, is_active_image=True).order_by(
                '-view_count')
        else:
            videos = videos.order_by('-view_count')
        home_videos = map(lambda video: Util_App.get_video(video), videos[:10]) if videos else []
        return home_videos

    @staticmethod
    def get_all_videos(page: int = 1):
        try:
            data_page = 0
            All_videos = []
            videos = Videos.objects.filter(is_childish=False, is_active_link=True, is_active_image=True) \
                .order_by('-view_count')
            if videos:
                videos_Paginator = Paginator(videos, 10)
                videos_page = videos_Paginator.get_page(page)
                data_page = videos_Paginator.num_pages
                All_videos = [Util_App.get_video(video) for video in videos_page]
            return All_videos, data_page
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_videos_category(cat_id: int, page: int = 1, videos: Videos = None):
        try:
            home_videos = []
            data_page = 0
            if not videos:
                videos = Videos.objects.filter(category_id=cat_id, is_active_link=True, is_active_image=True) \
                    .order_by("-view_count")
            else:
                videos = videos.filter(category_id=cat_id).order_by("-view_count")
            if videos:
                videos_Paginator = Paginator(videos, 10)
                videos_page = videos_Paginator.get_page(page)
                data_page = videos_Paginator.num_pages
                home_videos = [Util_App.get_video(video) for video in videos_page]
            return home_videos, data_page
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_video_child(video: Videos, image: bool = True):
        home_video = {"video_id": int(video.video_id),
                      "fa_name": str(video.fa_name),
                      "image_link": str(video.image_link),
                      "link": str(video.link)}
        if not image:
            del home_video["fa_name"]
        return home_video

    @staticmethod
    def get_new_videos_child(count_video: int = 21, image: bool = True):
        videos = Videos.objects.filter(is_childish=True, is_active_link=True, is_active_image=True) \
                     .order_by('-video_id')[:count_video]
        # home_videos = map(lambda video: Util_App.get_video_child(video), videos)
        if image:
            home_videos = map(lambda video: Util_App.get_video_child(video), videos)
        else:
            home_videos = map(lambda video: Util_App.get_video_child(video, image), videos)
        return home_videos

    @staticmethod
    def get_genres_video(video: Videos):
        try:
            genres_queryset = VideoGenres.objects.filter(videos__in=[video])
            genres = [dict(genre_id=genre.genre_id, name=genre.name) for genre in genres_queryset]

            return genres
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_videos_genre(genre_id: int, page: int = 1):
        try:
            videos_genre = []
            data_page = 0
            videos = Videos.objects.filter(videogenres__genre_id=genre_id, is_active_link=True,
                                           is_active_image=True).distinct().order_by("-view_count")
            if videos:
                videos_Paginator = Paginator(videos, 10)
                videos_page = videos_Paginator.get_page(page)
                data_page = videos_Paginator.num_pages
                videos_genre = [Util_App.get_video(video) for video in videos_page]
            return videos_genre, data_page
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_image_video(video: Videos):
        try:
            video_image = VideoImages.objects.filter(video=video, is_active=True).order_by('image_id')
            image_video = map(lambda image: dict(image_id=image.image_id,
                                                 link=image.link,
                                                 name=image.name,
                                                 is_sticker=image.is_sticker), video_image) if video_image else []
            return image_video
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_actors_video(video: Videos):
        try:
            actors_queryset = VideoActors.objects.filter(videos__in=[video], is_active=True)
            actors = map(lambda actor: dict(actor_id=actor.actor_id,
                                            fa_name=actor.fa_name,
                                            avatar=actor.avatar), actors_queryset) if actors_queryset else []
            return actors
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_videos_actor(actor_id: int, page: int = 1):
        try:
            videos_actor = []
            data_page = 0
            videos = Videos.objects.filter(videoactors__actor_id=actor_id, is_active_link=True, is_active_image=True) \
                .distinct().order_by("-view_count")
            if videos:
                videos_Paginator = Paginator(videos, 10)
                videos_page = videos_Paginator.get_page(page)
                data_page = videos_Paginator.num_pages
                videos_actor = [Util_App.get_video(video) for video in videos_page]
            return videos_actor, data_page
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_slider_song(is_home: bool = False):
        try:
            slider_song = []
            if is_home:
                slider_song_items = SliderHome.objects.filter(type=SliderType.song.value,
                                                              SongSlider__is_active=True,
                                                              SongSlider__Song__is_active_link=True)
                if slider_song_items:
                    for slider_song_item in slider_song_items:
                        song_slider = slider_song_item.SongSlider
                        song = song_slider.Song
                        slider_song.append(dict(song_id=song.song_id,
                                                image=song_slider.image_link,
                                                name=song.fa_name,
                                                singer=song.singer.fa_name,
                                                link=song.link, ))
            else:
                song_sliders = SliderSong.objects.select_related("Song__singer").filter(is_show=True, is_active=True,
                                                                                Song__is_active_link=True, )
                if song_sliders:
                    for song_slider in song_sliders:
                        song = song_slider.Song
                        slider_song.append(dict(song_id=song.song_id,
                                                image=song_slider.image_link,
                                                name=song.fa_name,
                                                singer=song.singer.fa_name,
                                                link=song.link))
            return slider_song
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_song(song: Songs, child: bool = False):
        if not child:
            home_song = {"song_id": int(song.song_id),
                         "fa_name": str(song.fa_name),
                         "en_name": str(song.en_name),
                         "fa_singer": str(song.singer.fa_name) if song.singer else "",
                         "en_singer": str(song.singer.en_name) if song.singer else "",
                         "image_link": str(song.image_link),
                         "length": int(song.length),
                         "link": song.link
                         }
        else:
            home_song = {"song_id": int(song.song_id),
                         "fa_name": str(song.fa_name),
                         "en_name": str(song.en_name),
                         "image_link": str(song.image_link),
                         "length": int(song.length),
                         "link": str(song.link)
                         }
        return home_song

    @staticmethod
    def get_new_songs():
        songs = Songs.objects.select_related("singer").filter(is_podcast=False,
                                                              is_childish=False,
                                                              is_active_image=True,
                                                              is_active_link=True,
                                                              singer__is_active=True
                                                              ).order_by('-song_id')[:10]
        home_songs = map(lambda song: Util_App.get_song(song), songs) if songs else []
        return home_songs

    @staticmethod
    def get_all_popular_songs(page: int = 1):
        home_songs = []
        songs = Songs.objects.select_related("singer").filter(is_podcast=False, is_childish=False,
                                                              is_active_image=True, is_active_link=True,
                                                              singer__is_active=True).order_by('-view_count')
        songs_paginator = Paginator(songs, 10)
        singers_page = songs_paginator.get_page(page)
        data_page = songs_paginator.num_pages
        if singers_page:
            for song in singers_page:
                home_songs.append(Util_App.get_song(song))
        return data_page, home_songs

    @staticmethod
    def get_new_songs_child(count_song: int = 21):
        songs = Songs.objects.filter(is_childish=True, is_active_link=True, is_active_image=True).order_by('-song_id')[
                :count_song]
        home_songs = map(lambda song: Util_App.get_song(song, True), songs) if songs else []
        return home_songs

    @staticmethod
    def get_play_list():
        try:
            playlists = Playlists.objects.filter(songs__is_active_link=True, songs__is_active_image=True,
                                                 is_active=True).distinct()[:10]
            home_play_lists = map(lambda playlist: dict(playlist_id=playlist.playlist_id,
                                                        name=playlist.name,
                                                        cover=playlist.cover), playlists) if playlists else []
            return home_play_lists
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_new_singer():
        singers = Singer_Channel.objects.filter(is_active=True, is_channel=False, songs__is_active_image=True,
                                                songs__is_active_link=True).distinct()[:10]
        home_Singer = map(lambda singer: dict(
            singer_id=singer.singer_id,
            fa_name=singer.fa_name,
            image_link=singer.image_link,
        ), singers) if singers else []
        return home_Singer

    @staticmethod
    def get_all_singer(page: int = 1):
        new_singer = []
        Singers = Singer_Channel.objects.filter(is_channel=False, is_active=True, songs__is_active_image=True,
                                                songs__is_active_link=True).order_by('-singer_id')
        singers_paginator = Paginator(Singers, 10)
        singers_page = singers_paginator.get_page(page)
        data_page = singers_paginator.num_pages
        for Singer in singers_page:
            # songs_singer = Songs.objects.first(singer=Singer, is_active_image=True, is_active_link=True)
            # print("singer:", songs_singer)
            # if not songs_singer.first():
            #     return None, None
            home_Singer = {
                "singer_id": int(Singer.singer_id),
                "fa_name": str(Singer.fa_name),
                "image_link": str(Singer.image_link),
            }
            new_singer.append(home_Singer)
        return data_page, new_singer

    @staticmethod
    def get_popular_songs_singer(Singer_channel: Singer_Channel):
        songs = Singer_channel.song_all_popular[:5]
        home_songs = map(lambda song: Util_App.get_song(song), songs) if songs else []
        return home_songs

    @staticmethod
    def get_five_songs_singer(Singer_channel: Singer_Channel):
        songs = Singer_channel.song_all[:5]
        home_songs = map(lambda song: Util_App.get_song(song), songs) if songs else []
        return home_songs

    @staticmethod
    def get_all_songs_singer(Singer_channel: Singer_Channel, page: int = 1):
        songs = Singer_channel.song_all
        songs_paginator = Paginator(songs, 10)
        songs_page = songs_paginator.get_page(page)
        data_page = songs_paginator.num_pages
        home_songs = map(lambda song: Util_App.get_song(song), songs_page) if songs_page else []
        return data_page, home_songs

    @staticmethod
    def get_all_popular_songs_singer(Singer_channel: Singer_Channel, page: int = 1):
        songs = Singer_channel.song_all_popular
        songs_paginator = Paginator(songs, 10)
        songs_page = songs_paginator.get_page(page)
        data_page = songs_paginator.num_pages
        home_songs = map(lambda song: Util_App.get_song(song), songs_page) if songs_page else []
        return data_page, home_songs

    @staticmethod
    def get_podcast(song: Songs):
        try:
            home_podcast = {"channel_id": int(song.singer.singer_id),
                            "channel_name": str(song.singer.fa_name),
                            "song_id": int(song.song_id),
                            "fa_name": str(song.fa_name),
                            "image_link": str(song.image_link),
                            "link": song.link,
                            }
            return home_podcast
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_new_podcast():
        try:
            songs = Songs.objects.select_related("singer").filter(is_podcast=True,
                                                                  singer__is_channel=True,
                                                                  singer__is_active=True,
                                                                  is_active_image=True,
                                                                  is_active_link=True).order_by(
                '-song_id')[:10]
            home_podcast = map(lambda song: Util_App.get_podcast(song), songs) if songs else []
            return home_podcast
        except Exception as e:
            return [str(e)]

    @staticmethod
    def get_all_podcasts(page: int = 1):
        home_podcast = []
        songs = Songs.objects.select_related("singer").filter(is_podcast=True, is_active_link=True,
                                                              is_active_image=True,
                                                              singer__is_active=True).order_by('-view_count')
        songs_paginator = Paginator(songs, 10)
        songs_page = songs_paginator.get_page(page)
        data_page = songs_paginator.num_pages
        if songs_page:
            for song in songs_page:
                home_podcast.append(Util_App.get_podcast(song))
        return data_page, home_podcast

    @staticmethod
    def get_new_channel():
        channels = Singer_Channel.objects.filter(is_channel=True, is_active=True, songs__is_active_image=True,
                                                 songs__is_active_link=True, songs__is_podcast=True).distinct()
        home_channels = map(lambda channel: dict(
            channel_id=channel.singer_id,
            fa_name=channel.fa_name,
            image_link=channel.image_link,
        ), channels) if channels else []
        return home_channels

    @staticmethod
    def get_all_channels(page: int = 1):
        home_channel = []
        channels = Singer_Channel.objects.filter(is_channel=True, is_active=True).order_by('-singer_id')
        channels_paginator = Paginator(channels, 10)
        channels_page = channels_paginator.get_page(page)
        data_page = channels_paginator.num_pages
        if channels_page:
            for channel in channels_page:
                home_channel.append({
                    "channel_id": int(channel.singer_id),
                    "fa_name": str(channel.fa_name),
                    "image_link": str(channel.image_link),
                })
        return data_page, home_channel

    @staticmethod
    def get_songs_playlist(playlist: Playlists, page: int = 1):
        songs = playlist.song_all
        song_paginator = Paginator(songs, 5)
        data_page = song_paginator.num_pages
        song_paginator = song_paginator.get_page(page)
        home_songs = map(lambda song: Util_App.get_song(song), song_paginator) if song_paginator else []
        return data_page, home_songs

    @staticmethod
    def get_popular_songs_playlist(playlist: Playlists, page: int = 1):
        songs = playlist.song_all_popular
        song_paginator = Paginator(songs, 5)
        data_page = song_paginator.num_pages
        song_paginator = song_paginator.get_page(page)
        home_songs = map(lambda song: Util_App.get_song(song), song_paginator) if song_paginator else []
        return data_page, home_songs

    @staticmethod
    def get_home_slider_ads():
        try:
            slider_ads = []
            slider_ads_items = SliderHome.objects.filter(type=SliderType.ads.value, AdsSlider__is_active=True)
            if slider_ads_items:
                slider_ads = [dict(image_url=slider_ads.AdsSlider.image_url,
                                   click_url=slider_ads.AdsSlider.click_url,
                                   title=slider_ads.AdsSlider.title, )
                              for slider_ads in slider_ads_items]
            return slider_ads
        except Exception as e:
            return [str(e)]

    @staticmethod
    def Get_category(cat: VideoCategory):
        return dict(category_id=cat.category_id,
                    image_link=cat.image_link,
                    title=cat.title)

    @staticmethod
    def search(search: str):
        try:
            videos = Videos.objects.filter(Q(fa_name__icontains=search) |
                                           Q(en_name__icontains=search) |
                                           Q(director__icontains=search) |
                                           Q(description__icontains=search) |
                                           Q(country_product__icontains=search) |
                                           Q(videoactors__en_name__icontains=search) |
                                           Q(videoactors__fa_name__icontains=search)) \
                .distinct().order_by("fa_name").filter(Q(is_active_image=True) & Q(is_active_link=True))
            songs = Songs.objects.filter(Q(fa_name__icontains=search) |
                                         Q(en_name__icontains=search) |
                                         Q(description__icontains=search) |
                                         Q(singer__fa_name__icontains=search)) \
                .distinct().order_by("fa_name").filter(Q(is_active_link=True) & Q(is_active_image=True))
            return {
                'video': [Util_App.get_video(video) for video in videos],
                'song': [Util_App.get_song(song) for song in songs]
            }
        except Exception as e:
            return [str(e)]


import pygame

def draw_borders(surface:pygame.Surface, coords:list):
    for i, j in coords:
        if not (i+1, j) in coords:
            pygame.draw.line(surface, (0, 0, 0), ((i+1)*60, j*60), ((i+1)*60, (j+1)*60), 4)
        if not (i-1, j) in coords:
            pygame.draw.line(surface, (0, 0, 0), ((i)*60, j*60), ((i)*60, (j+1)*60), 4)
        if not (i, j+1) in coords:
            pygame.draw.line(surface, (0, 0, 0), ((i)*60, (j+1)*60), ((i+1)*60, (j+1)*60), 4)
        if not (i, j-1) in coords:
            pygame.draw.line(surface, (0, 0, 0), ((i)*60, (j)*60), ((i+1)*60, (j)*60), 4)

def all_taken(grupos:list):
    for g in grupos:
        for i, j in g['coords']:
            yield (i, j)

def get_grupo(grupos:list, index):
    for x, g in enumerate(grupos):
        for i, j in g['coords']:
            if (i, j) == index:
                return x
    return -1

def get_top(coords):
    g = [9999, None]
    for i, j in coords:
        c = j*10 + i
        if c < g[0]:
            g = [c, (i, j)]
    return g[1]

def draw_text_ext(surface:pygame.Surface, poss, font:pygame.font.Font, text, color, mode=True):
    img = font.render(text, True, color)
    r = img.get_rect()
    if mode:
        r.topleft = poss
    else:
        r.center = poss
    surface.blit(img, r)

def draw_text(surface:pygame.Surface, poss, font:pygame.font.Font, text, mode=True):
    draw_text_ext(surface, poss, font, text, (0, 0, 0), mode)

def draw_boton_if(surface, event, rect, mouse, font, text, bg1, bg2):
    c = bg1 if rect.collidepoint(mouse) else bg2
    pygame.draw.rect(surface, c, rect)
    draw_text(surface, rect.center, font, text, False)
    return rect.collidepoint(mouse) and event.mouse_down(1)

def draw_label(surface, rect, font, text, bg):
    pygame.draw.rect(surface, bg, rect)
    draw_text(surface, rect.center, font, text, False)


from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from core.models import Timestamped

from profiles.models import Profile

# Create your models here.
class Department(Timestamped):
    name = models.CharField(max_length=100)
    profiles = models.ManyToManyField(Profile)

    class Meta:
        default_related_name = 'departments'
        verbose_name = 'department'
        verbose_name_plural = 'departments'

    def __str__(self):
        return f"{self.name}"

class Employee(Timestamped):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    departments = models.ManyToManyField(Department)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True,
                               blank=True, related_name='children')
    is_manager = models.BooleanField(default=False)

    class Meta:
        default_related_name = 'employees'
        verbose_name = 'employee'
        verbose_name_plural = 'employees'

    def __str__(self):
        return f"{self.last_name},{self.first_name}"

class Type(Timestamped):
    name = models.CharField(max_length=100)
    profiles = models.ManyToManyField(Profile)

    class Meta:
        default_related_name = 'types'
        verbose_name = 'type'
        verbose_name_plural = 'types'

    def __str__(self):
        return f"{self.name}"

class Application(Timestamped):
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    application_type = models.ForeignKey(Type, on_delete=models.CASCADE,db_index=True)
    from_person = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name="from_hr", db_index=True)
    to_person = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name="to_hr",db_index=True)

    class Meta:
        default_related_name = 'applications'
        verbose_name = 'application'
        verbose_name_plural = 'applications'

    def __str__(self):
        return f"{self.title}"

class Document(Timestamped):
    application = models.ForeignKey(Application, on_delete=models.CASCADE, db_index=True)
    document = models.FileField(upload_to="application/documents/")

    class Meta:
        default_related_name = 'applicationdocuments'
        verbose_name = 'application document'
        verbose_name_plural = 'application documents'

    def __str__(self):
        return f"{self.title}"


# coding=utf-8
from __future__ import unicode_literals

import pandas as pd
from urllib import parse
import pypinyin
import requests
import random
import json
import string
import numpy as np
from operator import itemgetter

from django.core.management.base import BaseCommand

from question.models import Song
from question.models import Question

prefix = 'http://cai-ta.ecdn.plutus-cat.com/assets/'

class Command(BaseCommand):
    def pingyin(self, word):
        s = ''
        for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
            s += ''.join(i)
        return s

    def song_init(self, execl, songs=0):
        model = Song
        df = pd.read_excel(execl, sheet_name=u'songs', encoding='utf-8')
        infos = df.ix[:, [u'歌手名', u'正确歌曲名']]
        if songs == 0:
            lines = len(infos[u'歌手名'])
        else:
            lines = songs
        for line in range(lines):
            url = prefix + self.pingyin(infos[u'歌手名'][line]) + '_' + self.pingyin(infos[u'正确歌曲名'][line]) + '.m4a'
            print(url)
            objs = model.objects.filter(resource_url=url).all()
            if objs.exists():
                continue
            obj = model(singer=infos[u'歌手名'][line], name=infos[u'正确歌曲名'][line], resource_url=url)
            obj.save()

    def question_init(self, execl, questions=0):
        model_question = Question
        df = pd.read_excel(execl, sheet_name=u'questions', encoding='utf-8')
        infos = df.ix[:, [u'id', u'题目顺序', u'题目类型', u'正确答案', u'错误答案']]
        model_question.objects.all().delete()
        questions_list = []
        name_dic = {}
        if questions == 0:
            lines = len(infos[u'id'])
        else:
            lines = questions
        for line in range(lines):
            pics_list = []
            pics_num = 0
            try_num = 0
            try_total = 0
            if pd.isna(infos[u'id'][line]) or pd.isna(infos[u'题目顺序'][line]) or pd.isna(infos[u'题目类型'][line]) \
                    or pd.isna(infos[u'正确答案'][line]) or pd.isna(infos[u'错误答案'][line]):
                continue
            if not name_dic.get(infos[u'正确答案'][line]):
                name_dic[infos[u'正确答案'][line]] = 1

            while pics_num < 3:
                pic = infos[u'正确答案'][line] + '/' + infos[u'正确答案'][line] + '-' + str(
                    name_dic[infos[u'正确答案'][line]]) + '.jpg'
                encode_pic = parse.quote(pic)
                url = prefix + encode_pic
                try:
                    resp = requests.get(str(url), timeout=4)
                except:
                    try_num = try_num + 1
                    name_dic[infos[u'正确答案'][line]] = name_dic[infos[u'正确答案'][line]] + 1
                    if try_num >= 3:
                        try_num = 0
                        name_dic[infos[u'正确答案'][line]] = 0
                    continue
                if resp.status_code != 200:
                    try_num = try_num + 1
                    name_dic[infos[u'正确答案'][line]] = name_dic[infos[u'正确答案'][line]] + 1
                    if try_num >= 3:
                        try_num = 0
                        name_dic[infos[u'正确答案'][line]] = 0
                    continue
                try_num = 0
                try_total = try_total + 1
                if try_total >= 12:
                    break
                name_dic[infos[u'正确答案'][line]] = name_dic[infos[u'正确答案'][line]] + 1
                pics_num = pics_num + 1
                pics_list.append(url)
            if pics_num < 3:
                continue
            print(pics_list)
            questions_list.append((int(infos[u'id'][line]), int(infos[u'题目顺序'][line]), int(infos[u'题目类型'][line]),
                                   infos[u'正确答案'][line], infos[u'错误答案'][line], json.dumps(pics_list)))

        questions_list.sort(key=lambda x: (x[1]))

        for num in range(len(questions_list)):
            obj_question = model_question(title=u'猜猜这是谁？', order_id=questions_list[num][0], question_type=1,
                                          difficult=1,
                                          right_answer_id=1, right_answer=questions_list[num][3],
                                          wrong_answer_id=2, wrong_answer=questions_list[num][4],
                                          resource_type=questions_list[num][2], resources=questions_list[num][5])
            obj_question.save()

    def handle(self, *args, **options):
        # self.song_init(u'question/management/commands/songs.xlsx', 100)
        self.question_init(u'question/management/commands/questions.xlsx', 180)


# pip install opencv-python
import cv2
import os
def VideoCatchToImg(videoName,videoType,savePath,time):
    if not os.path.exists(savePath): os.makedirs(savePath) #創建資料夾
    vc = cv2.VideoCapture(videoName+videoType) #讀取影片
    c=1

    if vc.isOpened(): #判斷檔案是否存在
        print("open film: %s%s -scuess!" %(videoName,videoType))
        rval , frame = vc.read()
    else:
        rval = False

    while rval:   #循環讀取頻率
        rval, frame = vc.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #轉灰階
        cv2.imshow(videoName, gray)
        if(c%time == 0): #每隔幾偵儲存圖片
            print('catch frames :' + str(c))
            cv2.imwrite('%s/%s-%s.jpg' %(savePath,videoName,str(c)),gray) #圖片儲存位置
        c = c + 1
        cv2.waitKey(1)

if __name__=='__main__':
    videoName= input("VideoName:")
    videoType = '.mp4'
    savePath = videoName
    time = 10  #影片偵數間格頻率
    VideoCatchToImg(videoName,videoType,savePath,time)


# -*- coding: utf-8 -*-
"""
Count the number of alignments to each reference.
Align fastq files of sample to a fastq of references.
Sort and index bam file before running this script.
"""
import sys
import pysam
import operator
import csv

# make dictionary {reference_name:alignment counts}, sysarg is bam file
def get_dict(sysarg):
    sample_name = "_".join(sysarg.split("_")[0:3])
    count_file=open(sample_name+"_counts.csv", "w")
    count_file.write("reference,count\n")
    samfile = pysam.AlignmentFile(sysarg, "rb") # import bam
    refs = samfile.references
    sum_dict = {} # {serotype:alignments}
    ref_dict = {} # {ref_name:alignments}
    for ref in refs: # a ref is an accession name
        ref_dict[ref] = samfile.count(reference=ref)
        sero = ref.split("_")[0]
        sum_dict.setdefault(sero,[]).append(samfile.count(reference=ref))
    for sero in sum_dict:
       sum_dict[sero] = sum(sum_dict[sero]) # sum of alignments per serotype
    sorted_acc=sorted(ref_dict.items(), key=operator.itemgetter(1), reverse=True) # list of tuples (ref, counts)
    obj=csv.writer(count_file)
    for acc in sorted_acc:
        obj.writerow(acc) # write ref,counts
    count_file.close()
    sorted_sum_dict = sorted(sum_dict.items(), key=operator.itemgetter(1), reverse=True) # list of tuples
    geno_file=open(sample_name+"_geno_counts.csv", "w")
    geno_file.write("genotype,count\n")
    obj2=csv.writer(geno_file)
    for geno in sorted_sum_dict:
        obj2.writerow(geno)
    geno_file.close()

p1 = get_dict(sys.argv[1]) # bam files
full = get_dict(sys.argv[2])

#top_3 = sorted(ref_dict, key=ref_dict.get, reverse=True)[:3]
#for ref in top_3:
#   print(ref+str(ref_dict[ref]))

#barWidth = 0.25
#r1 = np.arange(len(p1[0]))
#r2 = [x + barWidth for x in r1]
#plt.bar(r1, p1[1], align='center', linewidth=0, width=barWidth, edgecolor='white', color="skyblue")
#plt.bar(r2, full[1], align='center', linewidth=0, width=barWidth, edgecolor='white', color="pink")
## plt.xticks(range(len(sorted_sum_dict)), list(sorted_sum_dict.keys()))
#plt.ylabel('Number of aligned reads')
#plt.xlabel('Serotype')
#plt.title(sample_name)
#plt.show()


# credit goes to https://github.com/LuCeHe

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np

def plot_history(histories, plot_filename, epochs, method_names=None, save=True, show=False, bkg_color='white',
                 metrics_to_show=[], column_id=[], colors=None, figsize=None):
    if not isinstance(histories, list):
        histories = [histories]
    if isinstance(histories[0], dict):
        old_histories = histories
        histories = []
        for h in old_histories:
            history = lambda x: None
            history.history = h
            histories.append(history)

    if not method_names is None:
        assert len(method_names) == len(histories)

    if epochs > 0:
        keys = []
        for h in histories:
            keys.extend(list(h.history.keys()))

        keys = list(set(keys))
        keys = [k for k in keys if not 'val' in k]

        if metrics_to_show:
            keys = [k for k in keys if k in metrics_to_show]

        n_columns = len(column_id) if not len(column_id) == 0 else 1
        if figsize is None:
            figsize = (20, 5) if n_columns > 1 else (5, 20)
        fig, axs = plt.subplots(len(keys), n_columns, figsize=figsize, gridspec_kw={'hspace': 0})
        axs = axs if type(axs) is np.ndarray else [axs]

        if colors is None:
            cm = plt.get_cmap('tab20')  # Reds tab20 gist_ncar
            colors = cm(np.linspace(1, 0, len(histories)))
            np.random.shuffle(colors)

        lines = []
        if method_names is None:
            method_names = [None] * len(histories)

        for history, c, m in zip(histories, colors, method_names):
            for i, k in enumerate(keys):

                column = [x in m for x in column_id].index(True) if column_id else 0

                ax = axs[(i, column) if column_id else i]
                ax.set_facecolor(bkg_color)

                # plot training and validation losses
                if k in history.history.keys():
                    try:
                        check_if_break = history.history['val_' + k]
                        line, = ax.plot(history.history[k], label='train ' + k, color=c)
                        ax.plot(history.history['val_' + k], label='val ' + k, color=c, linestyle='--')
                    except:
                        line, = ax.plot(history.history[k], label=k, color=c, linestyle='--')
                        if method_names is None:
                            ax.legend()
                if column == 0:
                    ax.set_ylabel(k.replace('_', '\n'))

            lines.append(line)
            ax.set_xlabel('epoch')
            ax.xaxis.set_major_locator(MaxNLocator(integer=True))

        fig.align_ylabels(axs[:, 0] if n_columns > 1 else axs[:])

        if not method_names is None:
            ax.legend(lines, method_names)

        if save:
            fig.savefig(plot_filename, bbox_inches='tight')

        if show:
            plt.show()

        return fig, axs


#!/usr/bin/python

"""
This script is supposed to be used to reduce the large json files with ngram frequencies to a list of the most frequent ngrams.
"""

import json

inFileName = input("Filename: ")
threshold = int(input("Minimal frequency: "))

with open(inFileName, "r") as fp:
	dictionary = json.loads(fp.read())

outFileName = input("Output file: ")

fp = open(outFileName, "w")

for (ngram, freq) in dictionary.items():
	if freq > threshold:
		fp.write(ngram+"\n")

fp.close()


import os
import posixpath
from collections import Counter
from sbol_factory import SBOLFactory, UMLFactory
import sbol3

# Import ontology
__factory__ = SBOLFactory(locals(),
                          posixpath.join(os.path.dirname(os.path.realpath(__file__)), 'uml.ttl'),
                          'http://bioprotocols.org/uml#')
__umlfactory__ = UMLFactory(__factory__)

# Workaround for pySBOL3 issue #231: should be applied to every iteration on a collection of SBOL objects
# TODO: delete after resolution of pySBOL3 issue #231
def id_sort(i: iter):
    sortable = list(i)
    sortable.sort(key=lambda x: x.identity if isinstance(x, sbol3.Identified) else x)
    return sortable

###########################################
# Define extension methods for ValueSpecification

# TODO: move constants into ontology after resolution of https://github.com/SynBioDex/sbol_factory/issues/14
PARAMETER_IN = 'http://bioprotocols.org/uml#in'
PARAMETER_OUT = 'http://bioprotocols.org/uml#out'

def literal(value):
    """Construct a UML LiteralSpecification based on the value of the literal passed

    Note: if you need a reference rather than composition of a child object, that should be done by constructing
    LiteralReference directly
    :param value: the value to embed as a literal
    :return: LiteralSpecification of the appropriate type for the value
    """
    if value is None:
        return LiteralNull()
    elif isinstance(value, str):
        return LiteralString(value=value)
    elif isinstance(value, int):
        return LiteralInteger(value=value)
    elif isinstance(value, bool):
        return LiteralBoolean(value=value)
    elif isinstance(value, float):
        return LiteralReal(value=value)
    elif isinstance(value, sbol3.TopLevel):
        return LiteralReference(value=value)
    elif isinstance(value, sbol3.Identified):
        return LiteralIdentified(value=value)
    else:
        raise ValueError(f'Don\'t know how to make literal from {type(value)} "{value}"')

###########################################
# Define extension methods for Behavior

def behavior_add_parameter(self, name: str, param_type: str, direction: str, optional: bool = False):
    """Add a Parameter for this Behavior; usually not called directly

    Note: Current assumption is that cardinality is either [0..1] or 1
    :param name: name of the parameter, which will also be used for pins
    :param param_type: URI specifying the type of object that is expected for this parameter
    :param direction: should be 'in' or 'out'
    :param optional: True if the Parameter is optional; default is False
    :return: Parameter that has been added
    """
    param = Parameter(name=name, type=param_type, direction=direction, is_ordered=True, is_unique=True)
    self.parameters.append(param)
    param.upper_value = literal(1)  # all parameters are assumed to have cardinality [0..1] or 1 for now
    if optional:
        param.lower_value = literal(0)
    else:
        param.lower_value = literal(1)
    return param
Behavior.add_parameter = behavior_add_parameter  # Add to class via monkey patch

def behavior_add_input(self, name: str, param_type: str, optional=False):
    """Add an input Parameter for this Behavior

    Note: Current assumption is that cardinality is either [0..1] or 1

    :param name: name of the parameter, which will also be used for pins
    :param param_type: URI specifying the type of object that is expected for this parameter
    :param optional: True if the Parameter is optional; default is False
    :return: Parameter that has been added
    """
    return self.add_parameter(name, param_type, PARAMETER_IN, optional)
Behavior.add_input = behavior_add_input  # Add to class via monkey patch

def behavior_add_output(self, name, param_type):
    """Add an output Parameter for this Behavior

    :param name: name of the parameter, which will also be used for pins
    :param param_type: URI specifying the type of object that is expected for this parameter
    :return: Parameter that has been added
    """
    return self.add_parameter(name, param_type, PARAMETER_OUT)
Behavior.add_output = behavior_add_output  # Add to class via monkey patch

def behavior_get_inputs(self):
    """Return all Parameters of type input for this Behavior

    Note: assumes that type is all either in or out
    :return: Iterator over Parameters
    """
    return (p for p in self.parameters if p.direction == PARAMETER_IN)
Behavior.get_inputs = behavior_get_inputs  # Add to class via monkey patch

def behavior_get_outputs(self):
    """Return all Parameters of type output for this Behavior

    Note: assumes that type is all either in or out
    :return: Iterator over Parameters
    """
    return (p for p in self.parameters if p.direction == PARAMETER_OUT)
Behavior.get_outputs = behavior_get_outputs  # Add to class via monkey patch

###########################################
# Define extension methods for CallBehaviorAction

def call_behavior_action_input_pin(self, pin_name: str):
    """Find an input pin on the action with the specified name

    :param pin_name:
    :return: Pin with specified name
    """
    pin_set = {x for x in self.inputs if x.name == pin_name}
    if len(pin_set) == 0:
        raise ValueError(f'Could not find input pin named {pin_name}')
    if len(pin_set) > 1:
        raise ValueError(f'Found more than one input pin named {pin_name}')
    return pin_set.pop()
CallBehaviorAction.input_pin = call_behavior_action_input_pin  # Add to class via monkey patch

def call_behavior_action_output_pin(self, pin_name: str):
    """Find an output pin on the action with the specified name

    :param pin_name:
    :return: Pin with specified name
    """
    pin_set = {x for x in self.outputs if x.name == pin_name}
    if len(pin_set) == 0:
        raise ValueError(f'Could not find output pin named {pin_name}')
    if len(pin_set) > 1:
        raise ValueError(f'Found more than one output pin named {pin_name}')
    return pin_set.pop()
CallBehaviorAction.output_pin = call_behavior_action_output_pin  # Add to class via monkey patch

def add_call_behavior_action(parent: Activity, behavior: Behavior, **input_pin_literals):
    """Create a call to a Behavior and add it to an Activity

    :param parent: Activity to which the behavior is being added
    :param behavior: Behavior to be called
    :param input_pin_literals: map of literal values to be assigned to specific pins
    :return: newly constructed
    """
    # first, make sure that all of the keyword arguments are in the inputs of the behavior
    unmatched_keys = [key for key in input_pin_literals.keys() if key not in (i.name for i in behavior.get_inputs())]
    if unmatched_keys:
        raise ValueError(f'Specification for "{behavior.display_id}" does not have inputs: {unmatched_keys}')

    # create action
    action = CallBehaviorAction(behavior=behavior)
    parent.nodes.append(action)

    # Instantiate input pins
    for i in id_sort(behavior.get_inputs()):
        if i.name in input_pin_literals:
            value = input_pin_literals[i.name]
            # TODO: type check relationship between value and parameter type specification
            action.inputs.append(ValuePin(name=i.name, is_ordered=i.is_ordered, is_unique=i.is_unique,
                                          value=literal(value)))
        else:  # if not a constant, then just a generic InputPin
            action.inputs.append(InputPin(name=i.name, is_ordered=i.is_ordered, is_unique=i.is_unique))

    # Instantiate output pins
    for o in id_sort(behavior.get_outputs()):
        action.outputs.append(OutputPin(name=o.name, is_ordered=o.is_ordered, is_unique=o.is_unique))

    return action

###########################################
# Define extension methods for Activity

def activity_initial(self):
    """Find or create an initial node in an Activity.

    Note that while UML allows multiple initial nodes, use of this routine assumes a single one is sufficient.
    :return: InitialNode for Activity
    """

    initial = [a for a in self.nodes if isinstance(a, InitialNode)]
    if not initial:
        self.nodes.append(InitialNode())
        return self.initial()
    elif len(initial) == 1:
        return initial[0]
    else:
        raise ValueError(f'Activity "{self.display_id}" assumed to have one initial node, but found {len(initial)}')
# Monkey patch:
Activity.initial = activity_initial  # Add to class via monkey patch

def activity_final(self):
    """Find or create a final node in a Activity

    Note that while UML allows multiple final nodes, use of this routine assumes a single is sufficient.
    :return: FinalNode for Activity
    """
    final = [a for a in self.nodes if isinstance(a, FinalNode)]
    if not final:
        self.nodes.append(FlowFinalNode())
        return self.final()
    elif len(final) == 1:
        return final[0]
    else:
        raise ValueError(f'Activity "{self.display_id}" assumed to have one initial node, but found {len(initial)}')
# Monkey patch:
Activity.final = activity_final  # Add to class via monkey patch

def activity_deconflict_objectflow_sources(self, source: ActivityNode) -> ActivityNode:
    '''Avoid nondeterminism in ObjectFlows by injecting ForkNode objects where necessary

    Parameters
    ----------
    self: Activity
    source: node to take a value from, directly or indirectly

    Returns
    -------
    A source to attach to; either the original or an intervening ForkNode
    '''
    # Use original if it's one of the node types that supports multiple dispatch
    if isinstance(source, ForkNode) or isinstance(source, DecisionNode):
        return source
    # Otherwise, find out what targets currently attach:
    current_outflows = [e for e in self.edges if e.source.lookup() is source]
    # Use original if nothing is attached to it
    if len(current_outflows) == 0:
        #print(f'No prior use of {source.identity}, connecting directly')
        return source
    # If the flow goes to a single ForkNode, connect to that ForkNode
    elif len(current_outflows) == 1 and isinstance(current_outflows[0].target.lookup(),ForkNode):
        #print(f'Found an existing fork from {source.identity}, reusing')
        return current_outflows[0].target.lookup()
    # Otherwise, inject a ForkNode and connect all current flows to that instead
    else:
        #print(f'Found no existing fork from {source.identity}, injecting one')
        fork = ForkNode()
        self.nodes.append(fork)
        self.edges.append(ObjectFlow(source=source, target=fork))
        for f in current_outflows:
            f.source = fork # change over the existing flows
        return fork
Activity.deconflict_objectflow_sources = activity_deconflict_objectflow_sources

def activity_call_behavior(self, behavior: Behavior, **input_pin_map):
    """Call a Behavior as an Action in an Activity

    :param behavior: Activity to be invoked (object or name)
    :param input_pin_map: literal value or ActivityNode mapped to names of Behavior parameters
    :return: CallBehaviorAction that invokes the Behavior
    """

    # Any ActivityNode in the pin map will be withheld for connecting via object flows instead
    activity_inputs = {k: v for k, v in input_pin_map.items() if isinstance(v, ActivityNode)}
    non_activity_inputs = {k: v for k, v in input_pin_map.items() if k not in activity_inputs}
    cba = add_call_behavior_action(self, behavior, **non_activity_inputs)
    # add flows for activities being connected implicitly
    for name, source in id_sort(activity_inputs.items()):
        self.use_value(source, cba.input_pin(name))
    return cba
Activity.call_behavior = activity_call_behavior  # Add to class via monkey patch

def activity_order(self, source: ActivityNode, target: ActivityNode):
    """Add a ControlFlow between the designated source and target nodes in an Activity

    :param source: ActivityNode that is the source of the control flow
    :param target: ActivityNode that is the target of the control flow
    :return: ControlFlow created between source and target
    """
    if source not in self.nodes:
        raise ValueError(f'Source node {source.identity} is not a member of activity {self.identity}')
    if target not in self.nodes:
        raise ValueError(f'Target node {target.identity} is not a member of activity {self.identity}')
    flow = ControlFlow(source=source, target=target)
    self.edges.append(flow)
    return flow
Activity.order = activity_order  # Add to class via monkey patch

def activity_use_value(self, source: ActivityNode, target: ActivityNode):
    """Add an ObjectFlow transferring a value between the designated source and target nodes in an Activity

    Typically, these activities will be either Action Pins or ActivityParameterNodes serving as input or output
    :param source: ActivityNode that is the source of the value
    :param target: ActivityNode that receives the value
    :return: ObjectFlow created between source and target
    """
    if source.get_toplevel() is not self:  # check via toplevel, because pins are not directly in the node list
        raise ValueError(f'Source node {source.identity} is not a member of activity {self.identity}')
    if target.get_toplevel() is not self:
        raise ValueError(f'Target node {target.identity} is not a member of activity {self.identity}')
    source = self.deconflict_objectflow_sources(source)
    flow = ObjectFlow(source=source, target=target)
    self.edges.append(flow)
    return flow
Activity.use_value = activity_use_value  # Add to class via monkey patch

def activity_validate(self, report: sbol3.ValidationReport = None) -> sbol3.ValidationReport:
    '''Checks to see if the activity has any undesirable non-deterministic edges
    Parameters
    ----------
    self
    report

    Returns
    -------

    '''
    report = super(Activity, self).validate(report)
    # Check for objects with multiple outgoing ObjectFlow edges that are not of type ForkNode or DecisionNode
    source_counts = Counter([e.source.lookup() for e in self.edges if isinstance(e,ObjectFlow)])
    multi_targets = {n: c for n, c in source_counts.items() if c>1 and not (isinstance(n,ForkNode) or isinstance(n,DecisionNode))}
    for n, c in multi_targets.items():
        report.addWarning(n.identity, None, f'ActivityNode has {c} outgoing edges: multi-edges can cause nondeterministic flow')
    return report
Activity.validate = activity_validate

###########################################
# Extension methods waiting to be converted

# # Get the Value activity associated with the specified input of a subprotocol
# def subprotocol_input_value(self: SubProtocol, pin: Pin):
#     assert pin in self.input, ValueError("SubProtocol '"+self.identity+"' does not have an input Pin '"+pin+"'")
#     return pin.instance_of.lookup().activity
# SubProtocol.input_value = subprotocol_input_value
#
# # Get the Value activity associated with the specified output of a subprotocol
# def subprotocol_output_value(self: SubProtocol, pin: Pin):
#     assert pin in self.output, ValueError("SubProtocol '" + self.identity + "' does not have an output Pin '" + pin + "'")
#     return pin.instance_of.lookup().activity
# SubProtocol.output_value = subprotocol_output_value

# def activity_input_flows(self):
#     return {x for x in self.get_toplevel().flows if
#             (x.sink.lookup() == self) or
#             (isinstance(self, Executable) and x.sink.lookup() in self.input)}
# Activity.input_flows = activity_input_flows
#
# def activity_output_flows(self):
#     return {x for x in self.get_toplevel().flows if
#             (x.source.lookup() == self) or
#             (isinstance(self, Executable) and x.source.lookup() in self.output)}
# Activity.output_flows = activity_output_flows
#
# def activity_direct_input_flows(self):
#     return {x for x in self.get_toplevel().flows if (x.sink.lookup() == self)}
# Activity.direct_input_flows = activity_direct_input_flows
#
# def activity_direct_output_flows(self):
#     return {x for x in self.get_toplevel().flows if (x.source.lookup() == self)}
# Activity.direct_output_flows = activity_direct_output_flows
#
# def protocol_get_input(self, name):
#     return next(x for x in self.input if x.name==name)
# Protocol.get_input = protocol_get_input
#
#
# def protocol_get_output(self, name):
#     return next(x for x in self.output if x.name==name)
# Protocol.get_output = protocol_get_output


#!/usr/bin/env python
# coding: utf-8

# In[ ]:

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/api', methods=['GET'])
def get_data():
    data = {
        'message': 'Hello, world!'
    }
    return jsonify(data)

@app.route('/api', methods=['POST'])
def post_data():
    data = request.get_json()
    # Process the data as needed
    response = {
        'message': 'Data received successfully'
    }
    return jsonify(response)

if __name__ == '__main__':
    app.run()

# In[ ]:


from .base import *
from config.env import env

SECRET_KEY = env('SECRET_KEY')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=[])

DEBUG = env('DEBUG')

CORS_ALLOW_ALL_ORIGINS = False
CORS_ORIGIN_WHITELIST = env.list('CORS_ORIGIN_WHITELIST', default=[])


"""empty message

Revision ID: afd4a71c28e5
Revises: 71e603589766
Create Date: 2020-08-23 11:59:55.487812

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'afd4a71c28e5'
down_revision = '71e603589766'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('address', sa.String(length=128), nullable=True))
    op.add_column('users', sa.Column('city', sa.String(length=128), nullable=True))
    op.add_column('users', sa.Column('state', sa.String(length=2), nullable=True))
    op.add_column('users', sa.Column('zipcode', sa.String(length=10), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'zipcode')
    op.drop_column('users', 'state')
    op.drop_column('users', 'city')
    op.drop_column('users', 'address')
    # ### end Alembic commands ###


from itertools import izip

from maya import cmds

from math3d.vectorN import Vector3

from brigks.systems.systemGuide import SystemGuide
from brigks import config

class SliderSystemGuide(SystemGuide):

	markerNames = ("Rail", "Pos", "Neg")
	markerMinMax = dict(
		Rail=(1,-1),
		Pos=(1,-1),
		Neg=(1,-1)
		)
	markerPositions = dict(
		Rail1=Vector3([0,0,0]),
		Pos1=Vector3([2,0,0]),
		Neg1=Vector3([-1,0,0]),
		)
	markerPicked = ("Rail",)

	def createMarkerCurves(self):
		for i in xrange(1, self.count("Rail")+1):
			markers = [self._markers["{}{}".format(x, i)].name() for x in ["Pos", "Rail", "Neg"]]
			self.addMarkerDispCurve("Crv{}".format(i), markers)

	def createMarkers(self, matrices):
		super(SliderSystemGuide, self).createMarkers(matrices)

		# Parenting the Ends to the Rail and making sure you can't translate them other than x
		for rail, pos, neg in izip(self.markers("Rail"), self.markers("Pos"), self.markers("Neg")):
			for end in [pos, neg]:
				end.setParent(rail.name())
				cmds.setAttr(end.name()+".ty", 0)
				cmds.setAttr(end.name()+".tz", 0)
				cmds.setAttr(end.name()+".ty", lock=True)
				cmds.setAttr(end.name()+".tz", lock=True)

	def addSettings(self):
		self._settings["addControllers"] = False

		for i in xrange(1, self.count("Rail")+1):
			self._settings["minRot{}".format(i)] = -90
			self._settings["maxRot{}".format(i)] = 90
			self._settings["axis{}".format(i)] = "Z"

	def connectionPorts(self):
		super(SliderSystemGuide, self).connectionPorts()

		ports = dict(
			Tracker=["rotationTracker"]
			)

		for i in xrange(1, self.count("Rail")+1):
			ports["Rail{}".format(i)] = ["slotParent", "meshAttach", "nurbsParent", "multiParent", "customParent"]

		return ports

	def connectionSlots(self):
		super(SliderSystemGuide, self).connectionSlots()

		use = config.USE_CTL if self.settings("addControllers") else config.USE_RIG
		slots = dict()
		for i in xrange(1, self.count("Rail")+1):
			slots["Slider{}".format(i)] = (use, "Slider{}".format(i))

		return slots


#!/usr/bin/python
# -*- coding: UTF-8 -*-

from http.server import HTTPServer, BaseHTTPRequestHandler
import threading
import json
import socket

data = {'result': 'this is a test'}
host = ('localhost', 8888)
ip = ''

class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        global ip
        print(self.client_address, self.path)
        if self.path.startswith('/do'):
            ip = self.client_address[0]
            event.set()
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())

class SocketThread(threading.Thread):
    def __init__(self):
        super().__init__()
        self.con = socket.socket()

    def run(self):
        print(socket.gethostname())
        self.con.connect(('127.0.0.1', 12345))
        self.listen()
        # event.wait()
        # self.send()

    def listen(self):
        while True:
            rec = self.con.recv(1024)
            print(rec.decode())
            if rec.decode() == 'ok':
                print('消息正常结束')
            if not rec:
                print('链接断开了')
            break
        event.clear()
        event.wait()
        self.send()

    def send(self):
        print('尝试发送消息')
        self.con.send(('do_' + ip).encode())
        self.listen()

if __name__ == '__main__':
    event = threading.Event()
    client = SocketThread()
    client.start()
    server = HTTPServer(host, Resquest)
    print("Starting server, listen at: %s:%s" % host)
    server.serve_forever()


import csv
import datetime
import platform as pyplatform
import subprocess
import time
from contextlib import suppress
from typing import Any, Dict, List, Tuple

import psutil
import tensorflow as tf
from cpuinfo import get_cpu_info
from pydantic import BaseModel, Field

__all__ = ["get_sys_info"]

class JsonPrintableModel(BaseModel):
    def __str__(self) -> str:
        return self.json(indent=2)

class PlatformInfo(JsonPrintableModel):
    architecture: Tuple[str, str] = Field(
        default_factory=pyplatform.architecture
    )
    machine: str = Field(default_factory=pyplatform.machine)
    node: str = Field(default_factory=pyplatform.node)
    platform: str = Field(default_factory=pyplatform.platform)
    processor: str = Field(default_factory=pyplatform.processor)
    release: str = Field(default_factory=pyplatform.release)
    system: str = Field(default_factory=pyplatform.system)
    version: str = Field(default_factory=pyplatform.version)

class PythonInfo(JsonPrintableModel):
    python_build: Tuple[str, str] = Field(
        default_factory=pyplatform.python_build
    )
    python_compiler: str = Field(default_factory=pyplatform.python_compiler)
    python_branch: str = Field(default_factory=pyplatform.python_branch)
    python_implementation: str = Field(
        default_factory=pyplatform.python_implementation
    )
    python_revision: str = Field(default_factory=pyplatform.python_revision)
    python_version: str = Field(default_factory=pyplatform.python_version)

class CpuInfo(JsonPrintableModel):
    brand_raw: str
    core_count: int
    architecture: str
    max_frequency: int

    L1_data: str
    L1_instruction: str
    L2_size: str
    L2_line_size: str
    L2_cache_associativity: str
    L3_size: str

    support_mmx: bool
    support_sse: bool
    support_sse2: bool
    support_ssse3: bool
    support_sse4_1: bool
    support_sse4_2: bool
    support_avx: bool
    support_avx2: bool
    support_avx512: bool

    stepping: str
    model: str
    family: str
    vendor_id_raw: str
    flags: str

    def __init__(self) -> None:
        kwargs: Dict[str, Any] = {}
        cpuinfo = get_cpu_info()
        kwargs.setdefault("core_count", cpuinfo.get("count", "N/A"))
        kwargs.setdefault("architecture", cpuinfo.get("arch", "N/A"))
        kwargs.setdefault(
            "max_frequency", cpuinfo.get("hz_advertised", ("N/A", "N/A"))[0]
        )
        kwargs.setdefault(
            "min_frequency", cpuinfo.get("hz_advertised", ("N/A", "N/A"))[1]
        )
        kwargs.setdefault("L1_data", cpuinfo.get("l1_data_cache_size", "N/A"))
        kwargs.setdefault(
            "L1_instruction", cpuinfo.get("l1_instruction_cache_size", "N/A")
        )
        kwargs.setdefault("L2_size", cpuinfo.get("l2_cache_size", "N/A"))
        kwargs.setdefault(
            "L2_line_size", cpuinfo.get("l2_cache_line_size", "N/A")
        )
        kwargs.setdefault(
            "L2_cache_associativity",
            cpuinfo.get("l2_cache_associativity", "N/A"),
        )
        kwargs.setdefault("L3_size", cpuinfo.get("l3_cache_size", "N/A"))
        kwargs.setdefault("stepping", cpuinfo.get("stepping", "N/A"))
        kwargs.setdefault("model", cpuinfo.get("model", "N/A"))
        kwargs.setdefault("family", cpuinfo.get("family", "N/A"))
        kwargs.setdefault("flags", ";".join(cpuinfo.get("flags", "N/A")))
        kwargs.setdefault("vendor_id_raw", cpuinfo.get("vendor_id_raw", "N/A"))
        kwargs.setdefault("brand_raw", cpuinfo.get("brand_raw", "N/A"))

        kwargs.setdefault("support_mmx", "mmx" in cpuinfo.get("flags", "N/A"))
        kwargs.setdefault("support_sse", "sse" in cpuinfo.get("flags", "N/A"))
        kwargs.setdefault(
            "support_sse2", "sse2" in cpuinfo.get("flags", "N/A")
        )
        kwargs.setdefault(
            "support_ssse3", "ssse3" in cpuinfo.get("flags", "N/A")
        )
        kwargs.setdefault(
            "support_sse4_1", "sse4_1" in cpuinfo.get("flags", "N/A")
        )
        kwargs.setdefault(
            "support_sse4_2", "sse4_2" in cpuinfo.get("flags", "N/A")
        )
        kwargs.setdefault("support_avx", "avx" in cpuinfo.get("flags", "N/A"))
        kwargs.setdefault(
            "support_avx2", "avx2" in cpuinfo.get("flags", "N/A")
        )
        kwargs.setdefault(
            "support_avx512", "avx512" in cpuinfo.get("flags", "N/A")
        )

        super().__init__(**kwargs)

class SysMemInfo(JsonPrintableModel):
    total: int = Field(default=-1)
    available: int = Field(default=-1)
    used: int = Field(default=-1)
    free: int = Field(default=-1)

    swap_total: int = Field(default=-1)
    swap_used: int = Field(default=-1)
    swap_free: int = Field(default=-1)

    def __init__(self) -> None:
        kwargs: Dict[str, Any] = {}
        with suppress(Exception):
            virtual = psutil.virtual_memory()  # type: ignore
            kwargs.setdefault("total", virtual.total)  # type: ignore
            kwargs.setdefault("available", virtual.available)  # type: ignore
            kwargs.setdefault("used", virtual.used)  # type: ignore
            kwargs.setdefault("free", virtual.free)  # type: ignore

        with suppress(Exception):
            swap = psutil.swap_memory()
            kwargs.setdefault("swap_total", swap.total)
            kwargs.setdefault("swap_used", swap.used)
            kwargs.setdefault("swap_free", swap.free)

        super().__init__(**kwargs)

class TfInfo(JsonPrintableModel):
    version: str = Field(default=tf.__version__)
    cpu_count: int = Field(
        default_factory=lambda: len(tf.config.get_visible_devices("CPU"))  # type: ignore
    )
    gpu_count: int = Field(
        default_factory=lambda: len(tf.config.get_visible_devices("GPU"))  # type: ignore
    )
    is_built_with_cuda: bool = Field(
        default_factory=tf.test.is_built_with_cuda
    )
    is_built_with_gpu_support: bool = Field(
        default_factory=tf.test.is_built_with_gpu_support
    )
    gpu_device_name: bool = Field(default_factory=tf.test.gpu_device_name)

class GpuInfo(JsonPrintableModel):
    index: str
    name: str
    uuid: str
    count: str
    temperature_gpu: str

    vram_total: str
    vram_free: str
    vram_used: str
    temperature_vram: str
    ecc_vram: str

    driver_version: str
    vbios_version: str

    pci_bus_id: str
    pcie_link_gen_max: str
    pcie_link_gen_current: str

    clocks_vram: str
    clocks_gpu: str

    power_max: str
    compute_cap: str

    @classmethod
    def get_gpu_info(cls) -> List["GpuInfo"]:
        gpuinfo: List[GpuInfo] = []
        with suppress(Exception):
            gpuinfo.extend(cls.get_nvidia_gpu_info())
        with suppress(Exception):
            gpuinfo.extend(cls.get_amd_gpu_info())
        with suppress(Exception):
            gpuinfo.extend(cls.get_intel_gpu_info())
        return gpuinfo

    @classmethod
    def get_nvidia_gpu_info(cls) -> List["GpuInfo"]:
        output = subprocess.check_output(
            [
                "nvidia-smi",
                (
                    "--query-gpu="
                    "index,count,gpu_name,uuid,temperature.gpu,"
                    "memory.total,memory.free,memory.used,temperature.memory,ecc.mode.current,"
                    "driver_version,vbios_version,"
                    "pci.bus_id,pcie.link.gen.max,pcie.link.gen.current,"
                    "clocks.max.memory,clocks.max.graphics,"
                    "power.limit,compute_cap"
                ),
                "--format=csv",
            ]
        )
        reader = csv.DictReader(output.decode("utf-8").split("\n"))
        gpuinfo: List[GpuInfo] = []
        for gpu in reader:
            gpuinfo.append(
                cls(
                    index=gpu["index"].strip(),
                    name=gpu[" name"].strip(),
                    uuid=gpu[" uuid"].strip(),
                    count=gpu[" count"].strip(),
                    temperature_gpu=gpu[" temperature.gpu"].strip(),
                    vram_total=gpu[" memory.total [MiB]"].strip(),
                    vram_free=gpu[" memory.free [MiB]"].strip(),
                    vram_used=gpu[" memory.used [MiB]"].strip(),
                    temperature_vram=gpu[" temperature.memory"].strip(),
                    ecc_vram=gpu[" ecc.mode.current"].strip(),
                    driver_version=gpu[" driver_version"].strip(),
                    vbios_version=gpu[" vbios_version"].strip(),
                    pci_bus_id=gpu[" pci.bus_id"].strip(),
                    pcie_link_gen_max=gpu[" pcie.link.gen.max"].strip(),
                    pcie_link_gen_current=gpu[
                        " pcie.link.gen.current"
                    ].strip(),
                    clocks_vram=gpu[" clocks.max.memory [MHz]"].strip(),
                    clocks_gpu=gpu[" clocks.max.graphics [MHz]"].strip(),
                    power_max=gpu[" power.limit [W]"].strip(),
                    compute_cap=gpu[" compute_cap"].strip(),
                )
            )
        return gpuinfo

    @classmethod
    def get_amd_gpu_info(cls) -> List["GpuInfo"]:
        return []

    @classmethod
    def get_intel_gpu_info(cls) -> List["GpuInfo"]:
        return []

class SysInfo(JsonPrintableModel):

    creation_time: datetime.datetime = Field(
        default_factory=datetime.datetime.now
    )
    timezone: str = Field(default=time.tzname[1])
    platform: PlatformInfo = Field(default_factory=PlatformInfo)
    python: PythonInfo = Field(default_factory=PythonInfo)
    cpu: CpuInfo = Field(default_factory=CpuInfo)
    memory: SysMemInfo = Field(default_factory=SysMemInfo)
    tensorflow: TfInfo = Field(default_factory=TfInfo)
    gpu: List[GpuInfo] = Field(default_factory=GpuInfo.get_gpu_info)

def get_sys_info() -> SysInfo:
    return SysInfo()


import numpy as np
import healpy as hp
import pymaster as nmt

class PCl_covariances:

    def __init__(self, n_side, Cl_EE, Cl_EB, Cl_BB, ells_per_bin, purify_E, purify_B, mask, num_samples, noise_std,
                 cl_noise, label):
        # Set the n_side attribute for the generates maps
        self.n_side = n_side
        self.n_pix = 12 * self.n_side * self.n_side
        self.l_max = 3 * self.n_side - 1

        # Set the fiducial shear power spectrum
        self.Cl_EE = Cl_EE
        self.Cl_EB = Cl_EB
        self.Cl_BB = Cl_BB

        # Store the number of ells per bin and then create the bins object
        self.ells_per_bin = ells_per_bin
        self.bins = nmt.NmtBin.from_nside_linear(self.n_side, self.ells_per_bin)
        self.ells = self.bins.get_effective_ells()
        self.num_binned_ells = len(self.bins.get_effective_ells())

        # Purification options
        self.purify_E = purify_E
        self.purify_B = purify_B

        # The mask to be used
        self.mask = mask

        # Noise attributes
        self.noise_std = noise_std
        self.cl_noise = cl_noise

        # Number of samples to use in the numerical covariance matrix
        self.num_samples = num_samples

        # Objects where the average Cl values will be stored into
        self.num_avg_EE = None
        self.num_avg_BB = None

        # Objects where our covariance matrices will be stored into
        self.num_cov_EE_EE = None
        self.num_cov_BB_BB = None

        self.ana_cov_EE_EE = None
        self.ana_cov_BB_BB = None

        # Store the user-defined label for this PCl class
        self.label = label

    def compute_numerical_covariance(self):
        #* Function to estimate the numerical covariance matrix for this class

        # List to store Cl values into
        cl_EE_samples = []
        cl_BB_samples = []

        # Create a field that only contains our mask
        field_tmp = nmt.NmtField(self.mask, None, spin=2, purify_e=self.purify_E, purify_b=self.purify_B)

        # Now compute the mode-coupling matrix once
        workspace = nmt.NmtWorkspace()
        workspace.compute_coupling_matrix(field_tmp, field_tmp, self.bins)

        for i in range(self.num_samples + 1):
            if np.mod(i, 50) == 0: print(i, end=' ', flush=True)

            # Generate our new random realisation
            alm_E, alm_B = hp.synalm([self.Cl_EE, self.Cl_BB, self.Cl_EB], lmax=self.l_max, new=True)
            map_Q, map_U = hp.alm2map_spin([alm_E, alm_B], self.n_side, 2, self.l_max)

            if self.noise_std != 0:
                noise_Q = np.random.normal(loc=0, scale=self.noise_std, size=self.n_pix)
                noise_U = np.random.normal(loc=0, scale=self.noise_std, size=self.n_pix)

                map_Q += noise_Q
                map_U += noise_U

            fields = nmt.NmtField(self.mask, [map_Q, map_U], purify_e=self.purify_E, purify_b=self.purify_B)

            # Now recover the power spectrum of these fields
            cl_coupled = nmt.compute_coupled_cell(fields, fields)
            cl_EE, cl_EB, cl_BE, cl_BB = workspace.decouple_cell(cl_coupled)

            # Store recovered Cl values
            cl_EE_samples.append(cl_EE)
            cl_BB_samples.append(cl_BB)

        # Compute and store the average Cl value
        self.num_avg_EE = np.mean(cl_EE_samples, axis=0)
        self.num_avg_BB = np.mean(cl_BB_samples, axis=0)

        # Now compute the numerical covariance matrix for our list of samples
        self.num_cov_EE_EE = np.cov(cl_EE_samples, rowvar=False)
        self.num_cov_BB_BB = np.cov(cl_BB_samples, rowvar=False)

        # Save the numerical EE-EE and BB-BB covariance matrices
        np.save(f'../data/Fishers/num_cov_EE_EE_{self.label}', self.num_cov_EE_EE)
        np.save(f'../data/Fishers/num_cov_BB_BB_{self.label}', self.num_cov_BB_BB)

        # Print new line once done
        print('')

    def load_numerical_covariance(self):
        self.num_cov_EE_EE = np.load(f'../data/Fishers/num_cov_EE_EE_{self.label}.npy')
        self.num_cov_BB_BB = np.load(f'../data/Fishers/num_cov_BB_BB_{self.label}.npy')

    def compute_analytic_covariance(self):
        #* Function to estimate the analytical covariance matrix for this class

        # Create a field that only contains our mask
        field_tmp = nmt.NmtField(self.mask, None, spin=2, purify_e=self.purify_E, purify_b=self.purify_B)

        # Create a regular workspace object
        wsp = nmt.NmtWorkspace()
        wsp.compute_coupling_matrix(field_tmp, field_tmp, self.bins)

        # Create a covariance workspace object and then compute the coupling coefficients
        covar_wsp = nmt.NmtCovarianceWorkspace()
        covar_wsp.compute_coupling_coefficients(field_tmp, field_tmp, field_tmp, field_tmp)

        covar_22_22 = nmt.gaussian_covariance(covar_wsp, 2, 2, 2, 2,  # Spins of the 4 fields
                                            [self.Cl_EE + self.cl_noise, self.Cl_EB, self.Cl_EB, self.Cl_BB + self.cl_noise],
                                            [self.Cl_EE + self.cl_noise, self.Cl_EB, self.Cl_EB, self.Cl_BB + self.cl_noise],
                                            [self.Cl_EE + self.cl_noise, self.Cl_EB, self.Cl_EB, self.Cl_BB + self.cl_noise],
                                            [self.Cl_EE + self.cl_noise, self.Cl_EB, self.Cl_EB, self.Cl_BB + self.cl_noise],
                                            wsp, wb=wsp).reshape([self.num_binned_ells, 4, self.num_binned_ells, 4])

        self.ana_cov_EE_EE = covar_22_22[:, 0, :, 0]
        self.ana_cov_BB_BB = covar_22_22[:, 3, :, 3]


from nltk.stem.porter import *
import json
import datetime
import re

header = []
sms = []
time = []
unique_bank=[]
unique_arr = []
final_3c_arr=[[]]
actual_mapping_arr = []
count_actual_mapping=0
count_unique_mapping=0
count_3c_arr=0
debit_msg_inside=0
credit_msg_inside=0
extra_msg_inside=0
extra_outside_loop=0

re_pattern_for_account_no_search=r"\bXX+[0-9]{3,}|\bX+[0-9]{3,}|\bxx+[0-9]{2,}|Txn#[0-9]{3,}|A/c ending+ [0-9]{3,}|a/cx+[0-9]{2,}$"

file1 = open("main_file_output.txt","w", encoding="utf-8-sig")

with open('muktesh_sms.json', encoding="utf-8-sig") as json_file:  			#input file Json and appending in 3 array header sms time
    data = json.load(json_file)
    for p in data['data']:
        header.append(p['contact_no'])
        sms.append('hello '+p['sms'])
        time.append((datetime.datetime.fromtimestamp(float(p['sms_time']) / 1e3)).strftime("%H:%M:%S.%f - %b %d %Y"))

for i in range(len(sms)):
	stemmer = PorterStemmer()
	singles = [stemmer.stem(word) for word in sms[i].split()]
	print(' '.join(singles))

	if (any('debit' in s for s in singles) or any('txn' in s for s in singles) or any('use' in s for s in singles) or any('w/d' in s for s in singles)or any('withdrawn' in s for s in singles)or any('spent' in s for s in singles)):

		acc = re.findall(re_pattern_for_account_no_search,sms[i])
		for j in range(len(acc)):
			if(acc[j]!=''):
				print("acc = "+str(acc[j]))       # we found account no in sample space step 1
				actual_mapping_arr.append(acc[j])
				if(acc[j] not in unique_arr):
					unique_arr.append(acc[j])

					matchObj = re.match( r'(.*?)?(\s*)(inr|INR|Rs|rs|RS|inr.|INR.|Rs.|rs.|RS.|(amount(\s*)of)(\s*))(\s*)( *[0-9]+.?(\s*)[0-9]*)(\s*)(.*)?',sms[i], re.M|re.I) # regex to find digits after inr|inr |rs.|rs. in the string
					if matchObj:
						print ("matchObj.group(1) : for debit ", matchObj.group(8))
						debit_msg_inside=debit_msg_inside+1
						file1.write(sms[i]+"\, 1\n")		#1 for debit
						break
				else:
					matchObj = re.match( r'(.*?)?(\s*)(inr|INR|Rs|rs|RS|inr.|INR.|Rs.|rs.|RS.|(amount(\s*)of)(\s*))(\s*)( *[0-9]+.?(\s*)[0-9]*)(\s*)(.*)?',sms[i], re.M|re.I) # regex to find digits after inr|inr |rs.|rs. in the string
					if matchObj:
						print ("matchObj.group(1) : for debit ", matchObj.group(8))
						debit_msg_inside=debit_msg_inside+1
						file1.write(sms[i]+"\, 1\n")		#1 for debit
						break

	elif any('credit' in s for s in singles):
		acc = re.findall(re_pattern_for_account_no_search,sms[i])
		for j in range(len(acc)):
			if(acc[j]!=''):
				print("acc = "+str(acc[j]))       # we found account no in sample space step 1
				actual_mapping_arr.append(acc[j])
				if(acc[j] not in unique_arr):
					unique_arr.append(acc[j])

					matchObj = re.match( r'(.*?)?(\s*)(inr|INR|Rs|rs|RS|inr.|INR.|Rs.|rs.|RS.|(amount(\s*)of)(\s*))(\s*)( *[0-9]+.?(\s*)[0-9]*)(\s*)(.*)?',sms[i], re.M|re.I) # regex to find digits after inr|inr |rs.|rs. in the string
					if matchObj:
						print ("matchObj.group(1) : for credit ", matchObj.group(8))
						credit_msg_inside=credit_msg_inside+1
						file1.write(sms[i]+"\, 2\n")		#2 for debit
						continue
				else:
					matchObj = re.match( r'(.*?)?(\s*)(inr|INR|Rs|rs|RS|inr.|INR.|Rs.|rs.|RS.|(amount(\s*)of)(\s*))(\s*)( *[0-9]+.?(\s*)[0-9]*)(\s*)(.*)?',sms[i], re.M|re.I) # regex to find digits after inr|inr |rs.|rs. in the string
					if matchObj:
						print ("matchObj.group(1) : for credit ", matchObj.group(8))
						credit_msg_inside=credit_msg_inside+1
						file1.write(sms[i]+"\, 2\n")		#1 for debit
						continue

	else:
		extra_outside_loop=extra_outside_loop+1
		extra_msg_inside=extra_msg_inside+1
		file1.write(sms[i]+"\, 0\n")
print("Debit Msg are " +str(debit_msg_inside))
print("credit_msg_inside "+str(credit_msg_inside))
print("extra_msg_inside "+str(extra_msg_inside))


from celery import Celery
from config import load_config
import os
if os.getenv("IS_SERVER") is not None:
    from model import LAMA, Detectron2
    from utils import upload

config = load_config()
app = Celery('task', backend=config.celery.backend, broker=config.celery.broker)
app.conf.update(
    task_serializer='pickle',
    result_serializer='pickle',
    accept_content=['pickle']
)
lama_model = None
detectron2_model = None

@app.task
def lama_task(image, mask):
    global lama_model
    global config
    if lama_model is None:
        lama_model = LAMA(config.lama.device)
    mask = mask.convert("L")
    result = lama_model(image,mask)
    return upload(result)

@app.task
def detectron2_task(image):
    global detectron2_model
    global config
    if detectron2_model is None:
        detectron2_model = Detectron2()
    return detectron2_model(image)