"""Runs the sib-regression for the specified source and writes the result in an HDF5 file.

This script performs imputation of missing parental genotypes for families with at least two siblings and no genotyped parents. It takes as input the genotypes
 of the siblings in a .bed file and the IBD segments output by KING (with --ibdsegs option). To specify the siblings, one can either provide a pedigree file (--pedigree option) or
 the relatedness inference output from KING with the --related --degree 1 options along with age and sex information.

The pedigree file is a plain text file with header and columns: FID (family ID), IID (individual ID), FATHER_ID (ID of father), MOTHER_ID (ID of mother).
Note that individuals are assumed to have unique individual IDS (IID). Siblings are identified through individuals that have the same FID and the same FATHER_ID and MOTHER_ID.

Use the --king option to provide the KING relatedness inference output (usually has suffix .kin0) and the --agesex option to provide the age & sex information. The script
constructs a pedigree from this information and outputs it in the HDF5 output.

Args:
    -c : optional
        Duplicates offsprings of families with more than one offspring and both parents and add '_' to the start of their FIDs.
        These can be used for testing the imputation. The tests.test_imputation.imputation_test uses these.

    IBD : str
            Address of a file containing IBD statuses for all SNPs.
        This is a '\t seperated CSV with these columns: "chr", "ID1", "ID2", "IBDType", "StartSNP", "StopSNP".
        Each line states an IBD segment between a pair on individuals. This can be generated using King software

    genotypes_address : str
        Address of genotypes in .bed format. If there is a ~ in the address, ~ is replaced by the chromosome numbers in the range of [from_chr, to_chr) for each chromosome(from_chr and to_chr are two optional parameters for this script).

    bim : str
        Address of a bim file containing positions of SNPs if the address is different from Bim file of genotypes

    --from_chr : int, optional
        Which chromosome (<=). Should be used with to_chr parameter.

    --to_chr : int, optional
        Which chromosome (<). Should be used with from_chr parameter.

    --output_address: str, optional
        The script writes the result of imputation to this path. If it contains '~', result of imputation for chromosome i will be written to a similar path where ~ has been replaced with i. The default value for output_address is 'parent_imputed_chr'.

    --start: int, optional
        The script can do the imputation on a slice of each chromosome. This is the start of that slice(it is inclusive).

    --end: int, optional
        The script can do the imputation on a slice of each chromosome. This is the end of that slice(it is inclusive).

    --pedigree : string, optional
        Address of the pedigree file. Pedigree file is a ' ' seperated csv with columns 'FID', 'IID', 'FATHER_ID', 'MOTHER_ID'.

    --king : string, optional
        Address of a kinship file in KING format. kinship file is a '\t' seperated csv with columns "FID1", "ID1", "FID2", "ID2, "InfType".
        Each row represents a relationship between two individuals. InfType column states the relationship between two individuals.
        The only relationships that matter for this script are full sibling and parent-offspring which are shown by 'FS' and 'PO' respectively.
        This file is used in creating a pedigree file and can be generated using KING.

    --agesex : string, optional
        Address of the agesex file. This is a " " seperated CSV with columns "FID", "IID", "FATHER_ID", "MOTHER_ID", "sex", "age".
        Each row contains the age and sex of one individual. Male and Female sex should be represented with 'M' and 'F'.
        Age column is used for distinguishing between parent and child in a parent-offsring relationship inferred from the kinship file.
        ID1 is a parent of ID2 if there is a 'PO' relationship between them and 'ID1' is at least 12 years older than ID2.

    --threads : int, optional
        Number of the threads to be used. This should not exceed number of the available cores. The default number of the threads is one.

    --processes: int, optional
        Number of processes for imputation chromosomes. Each chromosome is done on one process.

    --output_compression: str, optional
        Optional compression algorithm used in writing the output as an hdf5 file. It can be either gzip or lzf.

    --output_compression_opts': int, optional
        Additional settings for the optional compression algorithm. Take a look at the create_dataset function of h5py library for more information.

Results:
    HDF5 files
        For each chromosome i, an HDF5 file is created at outprefix{i}. This file contains imputed genotypes, the position of SNPs, columns of resulting bim file, contents of resulting bim file, pedigree table and, family ids
        of the imputed parents, under the keys 'imputed_par_gts', 'pos', 'bim_columns', 'bim_values', 'pedigree' and, 'families', 'parental_status' respectively.

"""
import logging
from sibreg.bin.preprocess_data import *
from sibreg.bin.impute_from_sibs import *
import time
import argparse
import h5py
import random
import pandas as pd
import os
from multiprocessing import Pool
random.seed(1567924)

def run_imputation(data):
    """Runs the imputation and returns the consumed time
    Args:
        data : dict
            a dictionary with these keys and values:
            Keys:
                pedigree: pd.Dataframe
                    The standard pedigree table

                bed_address: str
                    Address of the bed file.

                ibd_pd: pd.Dataframe
                    IBD segments table in King format. Only needs to contain information about this chromosome

                output_address: str
                    The address to write the result of imputation on. The default value for output_address is 'parent_imputed_chr'.

                start: int, optional
                    This function can do the imputation on a slice of each chromosome. If specified, his is the start of that slice(it is inclusive).

                end: int, optional
                    This function can do the imputation on a slice of each chromosome. If specified, his is the end of that slice(it is inclusive).

                bim: str, optional
                    Address of a bim file containing positions of SNPs if the address is different from Bim file of genotypes.

                threads: int, optional
                    Number of the threads to be used. This should not exceed number of the available cores. The default number of the threads is one.

                output_compression: str, optional
                    Optional compression algorithm used in writing the output as an hdf5 file. It can be either gzip or lzf. None means no compression.

                output_compression_opts': int, optional
                    Additional settings for the optional compression algorithm. Take a look at the create_dataset function of h5py library for more information. None means no compression setting.
    Returns:
        float
            time consumed byt the imputation.
    """
    pedigree = data["pedigree"]
    bed_address = data["bed_address"]
    ibd_pd = data["ibd_pd"]
    output_address = data["output_address"]
    start = data.get("start")
    end = data.get("end")
    bim = data.get("bim")
    threads = data.get("threads")
    output_compression = data.get("output_compression")
    output_compression_opts = data.get("output_compression_opts")
    logging.info("processing " + bed_address)
    sibships, iid_to_bed_index, gts, ibd, pos, chromosomes, hdf5_output_dict = prepare_data(pedigree, bed_address, ibd_pd, start, end, bim)
    gts = gts.astype(float)
    pos = pos.astype(int)
    start_time = time.time()
    imputed_fids, imputed_par_gts = impute(sibships, iid_to_bed_index, gts, ibd, pos, hdf5_output_dict, str(chromosomes), output_address, threads = threads, output_compression=output_compression, output_compression_opts=output_compression_opts)
    end_time = time.time()
    return (end_time-start_time)

#does the imputation and writes the results
if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s')
    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        action='store_true')
    parser.add_argument('ibd',
                        type=str,
                        help='IBD file')
    parser.add_argument('genotypes_address',
                        type=str,help='Address of genotypes in .bed format. If there is a ~ in the address, ~ is replaced by the chromosome numbers in the range of [from_chr, to_chr) for each chromosome(from_chr and to_chr are two optional parameters for this script).')
    parser.add_argument('--from_chr',
                        type=int,
                        default = 1,
                        help='Which chromosome (<=). Should be used with to_chr parameter.')
    parser.add_argument('--to_chr',
                        type=int,
                        default = 2,
                        help='Which chromosome (<). Should be used with from_chr parameter.')
    parser.add_argument('--bim',
                        type=str,
                        default = None,
                        help='Address of a bim file containing positions of SNPs if the address is different from Bim file of genotypes')
    parser.add_argument('--output_address',
                        type=str,
                        default = "parent_imputed",
                        help="Writes the result of imputation for chromosome i to outprefix{i}")
    parser.add_argument('--start',
                        type=int,
                        help='Start index of SNPs to perform imputation for in genotype file (starting at zero). Should be used with end parameter.',
                        default=None)
    parser.add_argument('--end',
                        type=int,
                        help='End index of SNPs to perform imputation for in genotype file (goes from 0 to (end-1). Should be used with start parameter.',
                        default=None)
    parser.add_argument('--pedigree',
                        type=str,
                        default = None,
                        help='Pedigree file with siblings sharing family ID')
    parser.add_argument('--king',
                        type=str,
                        default = None,
                        help='Address of the king file')
    parser.add_argument('--agesex',
                        type=str,
                        default = None,
                        help='Address of the agesex file with header "FID IID age sex"')
    parser.add_argument('--threads',
                        type=int,
                        default=1,
                        help='Number of the cores to be used')
    parser.add_argument('--processes',
                        type=int,
                        default=1,
                        help='Number of processes for imputation chromosomes. Each chromosome is done on one process.')
    parser.add_argument('--output_compression',
                        type=str,
                        default=None,
                        help='Optional compression algorithm used in writing the output as an hdf5 file. It can be either gzip or lzf')
    parser.add_argument('--output_compression_opts',
                        type=int,
                        default=None,
                        help='Additional settings for the optional compression algorithm. Take a look at the create_dataset function of h5py library for more information.')

    args=parser.parse_args()
    #fids starting with _ are reserved for control
    #Families should not have grandparents
    if not args.pedigree:
        logging.info("creating pedigree ...")
        pedigree = create_pedigree(args.king, args.agesex)
    else:
        pedigree = pd.read_csv(args.pedigree, sep = " ")
    logging.info("pedigree loaded.")

    if args.c:
        logging.info("Adding control to the pedigree ...")
        pedigree = add_control(pedigree)
        logging.info("Control Added.")

    logging.info("Loading ibd ...")
    ibd_pd = pd.read_csv(args.ibd, sep = "\t")
    logging.info("ibd loaded.")
    if (args.from_chr is not None) and (args.to_chr is not None):
        chromosomes = [str(chromosome) for chromosome in range(args.from_chr, args.to_chr)]
    else:
        chromosomes = [None]

    if "~" in args.genotypes_address or "~" in args.output_address:
        if args.to_chr is None or args.from_chr is None:
            raise Exception("no chromosome range specified for the wildcard ~ in the address")

    inputs = [{"pedigree": pedigree,
            "bed_address": args.genotypes_address.replace("~", chromosome),
            "ibd_pd": ibd_pd, #[ibd_pd["Chr"] == chromosome],
            "output_address":args.output_address.replace("~", chromosome),
            "start": args.start,
            "end": args.end,
            "bim": args.bim,
            "threads": args.threads,
            "output_compression":args.output_compression,
            "output_compression_opts":args.output_compression_opts,
                }
            for chromosome in chromosomes]

    pool = Pool(args.processes)
    logging.info("staring process pool")
    consumed_time = pool.map(run_imputation, inputs)
    logging.info("imputation time: "+str(np.sum(consumed_time)))


import os
import argparse

class Result:
    def __init__(self, direntry, filecount=None):
        self.direntry = direntry
        self.size = None
        self.filecount = filecount
        self.linecount = None

    def add_size(self):
        if os.path.isfile(self.direntry):
            self.size = os.path.getsize(self.direntry)
        # self.size = os.path.getsize(self.direntry) # if we wanted directories sizes

    def add_filecount(self):
        self.filecount = count_the_files(self.direntry)

    def add_linecount(self):
        self.linecount = self.count_the_lines()

    def make_print_string(self):
        res = ''
        if self.size:
            res += str(self.size) + 'B' + '\t'
        if self.filecount:
            res += str(self.filecount) + 'f' + '\t'
        if self.linecount:
            res += str(self.linecount) + 'l' + '\t'
        res += self.direntry.name
        return res

    def count_the_lines(self):
        lc = 0
        if os.path.isfile(self.direntry):
            with open(self.direntry) as f:
                try:
                    for line in f:
                        lc += 1
                except UnicodeDecodeError:
                    # TODO handle .DS_Store
                    print(f'Error on {self.direntry}')
            return lc

def count_the_files(direntry):
    file_count = 0
    with os.scandir(direntry) as it:
        for file in it:
            if os.path.isfile(file):
                file_count += 1
    return file_count
    # in one line:
    # return len([file for file in os.scandir(self.direntry) if os.path.isfile(file)])

def ls(path, all_files=False, reverse_order=False, show_size=False, size_sort=False, count_lines=False,
       directories=False, recursive=False):

    direntry_list = []

    if recursive is False:
        with os.scandir(path) as direntries:  # makes sure the scandir iterator is close() at the end
            if all_files:
                for d in direntries:
                    direntry_list.append(d)
            else:
                for d in direntries:
                    if not d.name.startswith('.'):
                        direntry_list.append(d)

    # TODO: recursive

    # SORTING by size or name, ascending or descending
    if size_sort is False:
        direntry_list.sort(key=lambda file: file.name, reverse=reverse_order)
    else:
        direntry_list.sort(key=lambda file: os.path.getsize(file), reverse=reverse_order)

    results = []

    # show directories
    if directories:
        for n in direntry_list:
            if os.path.isdir(n):
                results.append(Result(n, filecount=count_the_files(n)))

    else:
        for n in direntry_list:
            results.append(Result(n))

        if show_size:
            for r in results:
                r.add_size()

        if count_lines:
            for r in results:
                r.add_linecount()

    for r in results:
        print(r.make_print_string())

def create_parser():
    parser = argparse.ArgumentParser()
    # Positional argument: path
    parser.add_argument("path", help="lists files and directories at this path")
    # Optional argument: show hidden files
    parser.add_argument('-a', '--all_files', action="store_true",
                        help='lists all files including hidden files starting with "."')
    # Optional argument: reverse sorting order
    parser.add_argument('-r', '--reverse_order', action="store_true",
                        help='reverses sorting order')
    # Optional argument: display file sizes
    parser.add_argument('-l', '--show_size', action="store_true",
                        help='displays file sizes')
    # Optional argument: sort by file size
    parser.add_argument('-S', '--size_sort', action="store_true",
                        help='sort by file size')
    # Optional argument: display line count in files
    parser.add_argument('-c', '--count_lines', action="store_true",
                        help='displays line counts of files')
    # Optional argument: only displays directories and their file count
    parser.add_argument('-d', '--directories', action="store_true",
                        help='only displays directories and their file count')
    # Optional argument: search recursively in sub-folders
    parser.add_argument('-R', '--recursive', action="store_true",
                        help='search recursively in sub-folders')
    return parser

def main():
    parser = create_parser()
    args = parser.parse_args()
    ls(args.path, args.all_files, args.reverse_order, args.show_size, args.size_sort, args.count_lines,
       args.directories, args.recursive)

if __name__ == '__main__':
    main()


import requests
from bs4 import BeautifulSoup
import bs4
import codecs
#beautifulsoup的find可以一直一层一层的寻找下去

DOWNLOAD_URL = 'https://movie.douban.com/top250'

def download_page(url):
    headers = {
        'Host': 'movie.douban.com',
        'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Upgrade-Insecure-Requests': '1',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    }
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    data = r.content
    return data

def parse_html(html):
    soup = BeautifulSoup(html, 'html.parser')

    #find和select的差异在于find返回的是bs4的Tag对象,select返回的是List
    # movie_list_soup = soup.find('ol', attrs={'class':'grid_view'})
    movie_list_soup = soup.select('ol[class="grid_view"]')[0]

    movie_list = []

    for li in movie_list_soup.find_all('li'):

        detail = li.find('div', attrs={'class':'hd'})  #获取head下的内容
        movie_name = detail.find('span', attrs={'class': 'title'}).string

        descore = li.find('div', attrs={'class':'bd'})
        score = descore.find('span', attrs={'class':'rating_num'}).string
        inq = descore.find('span', attrs={'class':'inq'}).string

        movie_list.append([movie_name,score,inq])

    next_page = soup.find('span', attrs={'class':'next'}).find('a')
    if next_page:
        return movie_list,DOWNLOAD_URL + next_page['href']
    return movie_list, []

if __name__ == "__main__":
    url = DOWNLOAD_URL
    with codecs.open('movies.txt', 'wb', encoding='utf-8') as fp:  #codecs.open可以解决编码转换问题,防止写入成功.
        while url:
            html = download_page(url)
            movies, url = parse_html(html)
            for i in range(len(movies)):
                # print(movies[i][0])
                fp.write('{0}\t{1:<12}\t{2:<20}\n'.format(movies[i][1],movies[i][0],movies[i][2],chr(12288)))
    fp.close()


from django.conf.urls import url
from .views import *
from django.views.generic import TemplateView

urlpatterns = [

    url(r'^contest/(?P<contest_id>\d+)/problem/(?P<contest_problem_id>\d+)/$', contest_problem_page,
        name="contest_problem_page"),
    url(r'^contest/(?P<contest_id>\d+)/problem/(?P<contest_problem_id>\d+)/submissions/$',
        contest_problem_my_submissions_list_page,
        name="contest_problem_my_submissions_list_page"),

    url(r'^contest/(?P<contest_id>\d+)/$', contest_page, name="contest_page"),
    url(r'^contest/(?P<contest_id>\d+)/problems/$', contest_problems_list_page,
        name="contest_problems_list_page"),
    url(r'^contest/(?P<contest_id>\d+)/submissions/$', contest_problem_submissions_list_page,
        name="contest_problem_submissions_list_page"),
    url(r'^contest/(?P<contest_id>\d+)/submissions/(?P<page>\d+)/$',
        contest_problem_submissions_list_page, name="contest_problem_submissions_list_page"),

    url(r'^contests/$', contest_list_page, name="contest_list_page"),
    url(r'^contests/(?P<page>\d+)/$', contest_list_page, name="contest_list_page"),
    url(r'^contest/(?P<contest_id>\d+)/rank/$', contest_rank_page, name="contest_rank_page"),

    url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"),
        name="add_contest_page"),

    url(r'^api/admin/contest/$', ContestAdminAPIView.as_view(), name="contest_admin_api"),
    url(r'^api/admin/contest_problem/$', ContestProblemAdminAPIView.as_view(), name="contest_problem_admin_api"),
    url(r'^api/admin/contest_problem/public/', MakeContestProblemPublicAPIView.as_view(),
        name="make_contest_problem_public"),
    url(r'^api/contest/password/$', ContestPasswordVerifyAPIView.as_view(), name="contest_password_verify_api"),
    url(r'^api/contest/time/$', ContestTimeAPIView.as_view(), name="contest_time_api_view"),

]


"""
    ${NAME}
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
from mcedit2.rendering import renderstates
from mcedit2.rendering.blockmeshes import registerBlockRenderer, standardCubeTemplates
from mcedit2.rendering.slices import _XYZST
from mcedit2.rendering.blockmeshes.blockmesh import BlockMeshBase
from mcedit2.rendering.vertexarraybuffer import VertexArrayBuffer
from mceditlib import faces

log = logging.getLogger(__name__)

@registerBlockRenderer("CrossedSquares")
class CrossedSquaresMesh(BlockMeshBase):
    renderstate = renderstates.RenderstateAlphaTestNode

    def makeCrossedSquaresVertices(self):
        arrays = []
        blockIndices = self.getRenderTypeMask()
        yield

        theseBlocks = self.sectionUpdate.Blocks[blockIndices]

        bdata = self.sectionUpdate.Data[blockIndices]
        bdata[theseBlocks == self.sectionUpdate.chunkUpdate.chunk.blocktypes.Sapling.ID] &= 0x3  # xxx saplings only
        texes = self.sectionUpdate.lookupTextures(self.sectionUpdate.Blocks[blockIndices], bdata, 0)

        blockLight = self.sectionUpdate.areaBlockLights[1:-1, 1:-1, 1:-1]
        skyLight = self.sectionUpdate.areaSkyLights[1:-1, 1:-1, 1:-1]

        #colorize = None
        #if self.blocktypes.name == "Alpha":
        #    colorize = (theseBlocks == mceditlib.blocktypes.pc_blocktypes.TallGrass.ID) & (bdata != 0)

        for direction in (faces.FaceXIncreasing,
                          faces.FaceXDecreasing,
                          faces.FaceZIncreasing,
                          faces.FaceZDecreasing):
            vertexBuffer = VertexArrayBuffer.fromIndices(direction, blockIndices)
            if not len(vertexBuffer):
                return
            vertexBuffer.buffer[_XYZST] += standardCubeTemplates[direction]

            if direction == faces.FaceXIncreasing:
                vertexBuffer.vertex[..., 1:3, 0] -= 1
            if direction == faces.FaceXDecreasing:
                vertexBuffer.vertex[..., 1:3, 0] += 1
            if direction == faces.FaceZIncreasing:
                vertexBuffer.vertex[..., 1:3, 2] -= 1
            if direction == faces.FaceZDecreasing:
                vertexBuffer.vertex[..., 1:3, 2] += 1

            vertexBuffer.applyTexMap(texes)

            vertexBuffer.rgba[:] = 0xff  # ignore precomputed directional light
            vertexBuffer.setLights(skyLight[blockIndices], blockLight[blockIndices])
            #
            #if colorize is not None:
            #    vertexBuffer.rgb[colorize] *= LeafBlockMesh.leafColor

            arrays.append(vertexBuffer)
            yield

        self.vertexArrays = arrays

    makeVertices = makeCrossedSquaresVertices


# Import required libraries
import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import plotly.express as px

# create a dash application
app = dash.Dash(__name__)

# read the launch data into pandas dataframe
spacex_df = pd.read_csv("spacex_launch_dash.csv")
max_payload = spacex_df['Payload Mass (kg)'].max()
min_payload = spacex_df['Payload Mass (kg)'].min()

unique_launch_sites = spacex_df['Launch Site'].unique().tolist()
launch_sites = []
launch_sites.append({'label': 'All Sites', 'value': 'All Sites'})
for launch_site in unique_launch_sites:
    launch_sites.append({'label': launch_site, 'value': launch_site})

# Create an app layout
app.layout = html.Div(children=[
    html.Div([
        html.H1('SpaceX Launch Records Dashboard',
            style={'textAlign': 'center', 'color': '#503D36','font-size': 40}),
    ]),

    html.Div([
        # TASK 1: Add a Launch Site Drop-down Input Component
        dcc.Dropdown(
                id = 'site-dropdown',
                options = launch_sites,
                placeholder = 'Select a Launch Site here',
                searchable = True ,
                clearable = False,
                value = 'All Sites'
            ),
        # TASK 2: Add a pie chart to show the total successful launches count for all sites
        # If a specific launch site is selected, show the Success vs. Failed counts for the site
        html.Div(dcc.Graph(id='success-pie-chart')),
    ], style={'padding': '0 30px'}),

    html.Div([
        # TASK 3: Add a Range Slider to Select Payload range
        html.Div("Payload range (Kg):",
            style={'color': '#503D36','font-size': 20, 'padding': '0 30px', 'margin-left': '11px'}
            ),
        html.Div([
            dcc.RangeSlider(
                id = 'payload_slider',
                min = 0,
                max = 10000,
                step = 1000,
                marks = {
                        0: {'label': '0 Kg', 'style': {'color': '#77b0b1'}},
                        1000: {'label': '1000 Kg'},
                        2000: {'label': '2000 Kg'},
                        3000: {'label': '3000 Kg'},
                        4000: {'label': '4000 Kg'},
                        5000: {'label': '5000 Kg'},
                        6000: {'label': '6000 Kg'},
                        7000: {'label': '7000 Kg'},
                        8000: {'label': '8000 Kg'},
                        9000: {'label': '9000 Kg'},
                        10000: {'label': '10000 Kg', 'style': {'color': '#f50'}},
                },
                value = [min_payload,max_payload]
            ),
        ], style={'padding': '40px 30px'}),

        # TASK 4: Add a scatter chart to show the correlation between payload and launch success
        html.Div(dcc.Graph(id = 'success-payload-scatter-chart')),
    ]),
],style={'padding': '0 20px'})

# TASK 2: add a callback based on selected site dropdown with success-pie-chart as the output
@app.callback(
     Output(component_id = 'success-pie-chart', component_property = 'figure'),
     [Input(component_id = 'site-dropdown', component_property = 'value')]
)
def update_piegraph(site_dropdown):
    if (site_dropdown == 'All Sites' or site_dropdown == 'None'):
        all_sites  = spacex_df[spacex_df['class'] == 1] # All Success only for all sites.
        fig = px.pie(
                all_sites,
                names = 'Launch Site',
                title = 'Total Success Launches by All Sites',
                hole = .2,
                color_discrete_sequence = px.colors.sequential.RdBu
            )
    else:
        site_specific  = spacex_df.loc[spacex_df['Launch Site'] == site_dropdown]
        fig = px.pie(
                site_specific,
                names = 'class',
                title = 'Total Success Launches for Site ↠ '+site_dropdown,
                hole = .2
            )
    return fig

# TASK 4: Add a callback function for `site-dropdown` and `payload-slider` as inputs, `success-payload-scatter-chart` as output
@app.callback(
     Output(component_id = 'success-payload-scatter-chart', component_property = 'figure'),
     [Input(component_id = 'site-dropdown', component_property = 'value'),
     Input(component_id = "payload_slider", component_property = "value")]
)
def update_scattergraph(site_dropdown,payload_slider):
    if (site_dropdown == 'All Sites' or site_dropdown == 'None'):
        low, high = payload_slider
        all_sites  = spacex_df
        inrange = (all_sites['Payload Mass (kg)'] > low) & (all_sites['Payload Mass (kg)'] < high)
        fig = px.scatter(
                all_sites[inrange],
                x = "Payload Mass (kg)",
                y = "class",
                title = 'Correlation Between Payload and Success for All Sites',
                color="Booster Version Category",
                size='Payload Mass (kg)',
                hover_data=['Payload Mass (kg)']
            )
    else:
        low, high = payload_slider
        site_specific  = spacex_df.loc[spacex_df['Launch Site'] == site_dropdown]
        inrange = (site_specific['Payload Mass (kg)'] > low) & (site_specific['Payload Mass (kg)'] < high)
        fig = px.scatter(
                site_specific[inrange],
                x = "Payload Mass (kg)",
                y = "class",
                title = 'Correlation Between Payload and Success for Site ↠ '+site_dropdown,
                color="Booster Version Category",
                size='Payload Mass (kg)',
                hover_data=['Payload Mass (kg)']
            )
    return fig

if __name__ == '__main__':
    app.run_server(debug=True)


import json
import requests

requests.packages.urllib3.disable_warnings()

def validate_json(text):
    json_object = False
    try:
        json_object = json.loads(text)
    except ValueError as e:
        return json_object
    return json_object

def build_list(_json):
    """
    Build list from json or text value

    Used to convert single json response to list of len 1 to allow testing
    through iterating through a list
    :param _json: single json object, list of json objects, or str of json object
    :return: list of json objects
    """
    if isinstance(_json, str):
        _json = validate_json(_json)
    _list = list()
    if isinstance(_json, list):
        _list = _json
    else:
        _list.append(_json)
    return _list


# stateful service which monitors server status in redis
# and keeps track of alerts and alerting

import redis
import time
import uuid
import logging
import os
from datetime import datetime
from collections import defaultdict

from constants import key_map, default_alert_process_interval, default_server_staleness_duration
from notify import notify_alert_new, notify_alert_closed, notify_ongoing_alert
from config import process_config
from service import daemon_init

logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
log_file = os.environ.get('LOG_FILE')
if log_file:
    ch = logging.handlers.RotatingFileHandler(log_file, mode='a', maxBytes=10485760, backupCount=10)
else:
    ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.setLevel(logging.INFO)
logger.addHandler(ch)

def get_bad_states(r, server_staleness_duration):
    bad_states = {}

    good_time = int(time.time() - server_staleness_duration)
    for server, value in r.zrevrangebyscore(key_map['server_last_posts'], good_time, 0, score_cast_func=int, withscores=True):
        key = 'stale_%s' % server
        bad_states[key] = {
            'info': 'Server %s last reported on %s' % (server, datetime.fromtimestamp(value)),
            'server_name': server
        }

    return bad_states

def generate_alert_id(state_name):
    return '%s_%s' % (state_name, uuid.uuid4())

def create_alert(r, state_name, description):
    now = time.time()
    alert_id = generate_alert_id(state_name)

    description['start_time'] = now
    description['end_time'] = -1
    description['state_name'] = state_name

    # first, save new alert
    r.hmset(key_map['alert_info'].format(alert_id=alert_id), description)

    # then log that alert record in our list of alerts
    r.zadd(key_map['alerts_historical'], now, alert_id)

    # likewise for alerts per this server
    r.zadd(key_map['server_alerts'].format(server_name=description['server_name']), now, alert_id)

    # then map this alert state name to the currently firing list of alerts
    r.hset(key_map['alert_currently_firing'], state_name, alert_id)

    return alert_id

def close_alert(r, state_name, alert_id):
    now = time.time()

    # first, take this alert out of our list of ongoing alerts
    r.hdel(key_map['alert_currently_firing'], state_name)

    alert_key = key_map['alert_info'].format(alert_id=alert_id)

    # alert might not be real anymore. don't create it if it's been deleted.
    if not r.exists(alert_key):
        return

    # then, update its status as closed
    r.hset(alert_key, 'end_time', now)

    # get start time
    start_time = r.hget(alert_key, 'start_time')

    if start_time:
        start_time = float(start_time)
    else:
        start_time = 0

    # record duration
    duration = now - start_time
    r.hset(alert_key, 'duration', duration)

    # send notifications
    notify_alert_closed(state_name, alert_id, duration)

def should_send_ongoing_alert(last_ongoing_alert_email, alert_send_email_interval, alert_id):
    if not alert_send_email_interval or alert_send_email_interval == -1:
        return False

    now = int(time.time())

    if (now - last_ongoing_alert_email[alert_id]) > alert_send_email_interval:
        last_ongoing_alert_email[alert_id] = now
        return True

    return False

def main():
    configs = process_config()
    daemon_init(configs)

    redis_url = configs.get('redis', 'localhost:6379')
    alert_process_interval = configs.get('alert_process_interval', default_alert_process_interval)
    server_staleness_duration = configs.get('server_staleness_duration', default_server_staleness_duration)
    alert_send_email_interval = configs.get('alert_send_email_interval', -1)

    r = redis.StrictRedis.from_url(redis_url)

    last_ongoing_alert_email = defaultdict(int)

    while True:
        logger.info('Starting alert run..')

        loop_start = time.time()
        closed_alerts = 0
        ongoing_alerts = 0
        new_alerts = 0

        # all currently bad alerts are here. dict of bad alert state name to info on that state
        bad_states = get_bad_states(r, server_staleness_duration)

        # 1: iterate through mapping of currently firing alerts in redis, checking if each
        # is stil in bad state. if not mark them as closed
        for state_name, alert_id in r.hgetall(key_map['alert_currently_firing']).iteritems():

            # Remove known alert from list of current states. It will then
            # be left with just new alerts.
            current_state = bad_states.pop(state_name, None)

            if current_state:
                logger.info('Alert "%s" still firing', state_name)
                if should_send_ongoing_alert(last_ongoing_alert_email, alert_send_email_interval, alert_id):
                    logger.info('Will send ongoing email')
                    notify_ongoing_alert(alert_id, state_name)
                else:
                    logger.info('Will not send ongoing email')
                ongoing_alerts += 1
            else:
                logger.info('Alert "%s" no longer firing. Closing.', state_name)
                closed_alerts += 1
                close_alert(r, state_name, alert_id)
                last_ongoing_alert_email.pop(alert_id, None)

        # 2: create new alerts for states which are bad but not yet kept track of
        for state_name, description in bad_states.iteritems():
            alert_id = create_alert(r, state_name, description)
            logger.info('Created new alert "%s" with id %s', state_name, alert_id)
            new_alerts += 1
            last_ongoing_alert_email[alert_id] = int(time.time())
            notify_alert_new(alert_id, state_name, description)

        # 3: purge records of ancient alerts
        # TODO

        # Log some info for this round
        loop_end = time.time()
        duration = loop_end - loop_start
        logger.info('New alerts: %s. Ongoing alerts: %s. Closed alerts: %s', new_alerts, ongoing_alerts, closed_alerts)
        logger.info('Alert processor ran in %.2f seconds. Will sleep %s seconds', duration, alert_process_interval)

        # Wait until next...
        time.sleep(alert_process_interval)

if __name__ == '__main__':
    main()


from LoadTrainingData import loadTrainingData
from load_data import loadData
import torch

data_file = '../bowman_vector-entailment-ICLR14-R1/wordpairs-v2.tsv'
wordMap, relationMap, relations = loadTrainingData(data_file)
print('Loading data')
data = loadData(None, None, None, wordMap, relationMap)
print('Data loaded!')

import collections
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

class Net(nn.Module):
    def __init__(self, wordMap, relations, learning_rate=.4):
        super(Net, self).__init__()
        self.vocab_size = len([key for key in wordMap])
        dtype = torch.FloatTensor
        DIM = 25
        PENULT=75
        self.Vocab = Variable(torch.rand(self.vocab_size, DIM).type(dtype)*.02-.01, requires_grad=True)
        self.matrices = Variable(torch.rand(DIM, DIM, DIM).type(dtype)*.02-.01, requires_grad=True)
        self.matrix = Variable(torch.rand(DIM*2, DIM).type(dtype)*.02-.01, requires_grad=True)
        self.bias = Variable(torch.rand(1, DIM).type(dtype)*.02-.01, requires_grad=True)
        self.classifierMatrices = Variable(torch.rand(DIM, DIM, PENULT).type(dtype)*.02-.01, requires_grad=True)
        self.classifierMatrix = Variable(torch.rand(DIM*2, PENULT).type(dtype)*.02-.01, requires_grad=True)
        self.classifierBias = Variable(torch.rand(1, PENULT).type(dtype)*.02-.01, requires_grad=True)
        self.Dense = Variable(torch.rand(PENULT, len(relations)).type(dtype)*.02-.01, requires_grad=True)
        self.DenseBias = Variable(torch.rand(1, len(relations)).type(dtype)*.02-.01, requires_grad=True)
        self.softmax = nn.Softmax()
        self.tanh = nn.Tanh()
        self.leakyRelu = nn.LeakyReLU(.01)
        self.learning_rate = learning_rate
        self.trainable = [self.Vocab, self.matrices, self.matrix, self.bias, self.classifierMatrices, self.classifierMatrix, self.classifierBias, self.Dense, self.DenseBias, self.Vocab]
        self.lamb = .0003

    def extract_val(self, leaf):
        return self.Vocab.index_select(0, torch.tensor(leaf.wordIndex))

    def combine_vals(self, l_val, r_val):

        intermediate = torch.squeeze(torch.matmul(l_val, self.matrices))
        t_r_val = torch.t(r_val)
        tensor_out = torch.t(torch.mm(intermediate, t_r_val))

        concated = torch.cat((l_val, r_val), dim=1)
        additive = torch.mm( concated ,self.matrix)

        tensor_out += additive
        tensor_out += self.bias
        return self.tanh(tensor_out)

    def classify(self, l_val, r_val):

        intermediate = torch.squeeze(torch.matmul(l_val, self.classifierMatrices))
        t_r_val = torch.t(r_val)
        tensor_out = torch.t(torch.mm(torch.t(intermediate), t_r_val))

        concated = torch.cat((l_val, r_val), dim=1)
        additive = torch.mm( concated, self.classifierMatrix)

        tensor_out += additive
        tensor_out += self.classifierBias
        tensor_out = self.leakyRelu(tensor_out)
        tensor_out = torch.mm(tensor_out, self.Dense)
        tensor_out += self.DenseBias

        return self.softmax(self.leakyRelu(tensor_out))

    def add_tree_network(self, t):
        if len(t.daughters) == 0:
            raise ValueError("Shouldn't have leaf in this function")
        l = t.daughters[0]
        r = t.daughters[1]
        left_val = None
        right_val = None
        if len(l.daughters) != 0:
            left_val = self.add_tree_network(l)
        else:
            left_val = self.extract_val(l)
        if len(r.daughters) != 0:
            right_val = self.add_tree_network(r)
        else:
            right_val = self.extract_val(r)
        return self.combine_vals(left_val, right_val)

    def forward(self, lt, rt):

        lt_val = self.add_tree_network(lt)
        rt_val = self.add_tree_network(rt)
        result = torch.squeeze(self.classify(lt_val, rt_val))

        return result

    def update_params(self):
        self.matrices.data -= self.learning_rate * self.matrices.grad
        self.Vocab.data -= self.learning_rate * self.Vocab.grad
        self.matrix.data -= self.learning_rate * self.matrix.grad
        self.bias.data -= self.learning_rate * self.bias.grad
        self.classifierMatrices.data -= self.learning_rate * self.classifierMatrices.grad
        self.classifierMatrix.data -= self.learning_rate * self.classifierMatrix.grad
        self.classifierBias.data -= self.learning_rate * self.classifierBias.grad
        self.Dense.data -= self.learning_rate * self.Dense.grad
        self.DenseBias.data -= self.learning_rate * self.DenseBias.grad
        # Manually zero the gradients after running the backward pass
        self.Vocab.grad.zero_()
        self.matrices.grad.zero_()
        self.matrix.grad.zero_()
        self.bias.grad.zero_()
        self.classifierMatrices.grad.zero_()
        self.classifierMatrix.grad.zero_()
        self.classifierBias.grad.zero_()
        self.Dense.grad.zero_()
        self.DenseBias.grad.zero_()

    def get_regularizations(self):
        l2_reg = Variable( torch.FloatTensor(1), requires_grad=True)
        for W in self.trainable:
            l2_reg = l2_reg + W.norm(2)
        return l2_reg*self.lamb

net = Net(wordMap, relations)

#optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
success_rate = []

all_data = []
for d in data:
    for line in d:
        all_data.append(line)
import tqdm, random

for epoch in tqdm.tqdm(range(5)):  # loop over the dataset multiple times
    running_loss = 0.0
    count = 0
    successes = 0
    total = 0
    random.shuffle(data)
    total_loss = 0
    for d in data:
        data_success = 0
        data_total = 0
        count+=1
        for line in d:
            left_tree = line['left_tree']
            right_tree = line['right_tree']
            relation = line['relation']

            true_out = relationMap[relation]

            outputs = net(left_tree, right_tree)
            #print(outputs)
            if torch.tensor(true_out) == torch.argmax(outputs):
                successes += 1
                data_success += 1
            total += 1
            data_total += 1
            success_rate.append(successes/total)

            loss = -torch.log(outputs[true_out]+.01) #+ net.get_regularizations()
            #import pdb; pdb.set_trace()
            total_loss += float(loss[0])
            #print(net.get_regularizations())
            #print(outputs, loss)
            loss.backward()

            net.update_params()
            #print( torch.argmax(outputs))
    print('\nTotal_loss', total_loss)
        #print(data_success/data_total)
        #print(successes/total)
        #if data_success/data_total == 1:
        #    for line in d:
        #        print(line['left_tree'].original,line['right_tree'].original, line['relation'] )
'''

random.shuffle(all_data)
training = all_data[:int(len(all_data)*.8)]
testing = all_data[int(len(all_data)*.8):]
print('Training Length',len(training))
print('Testing Length',len(testing))
for epoch in tqdm.tqdm(range(5)):  # loop over the dataset multiple times
    for line in training:
        left_tree = line['left_tree']
        right_tree = line['right_tree']
        relation = line['relation']

        true_out = relationMap[relation]

        outputs = net(left_tree, right_tree)

        loss = -torch.log(outputs[true_out])
        #print(outputs, loss)
        loss.backward()

        net.update_params()
        #print( torch.argmax(outputs))
    successes = 0
    total = 0
    for line in testing:
        left_tree = line['left_tree']
        right_tree = line['right_tree']
        relation = line['relation']

        true_out = relationMap[relation]

        outputs = net(left_tree, right_tree)
        #print(outputs)
        if torch.tensor(true_out) == torch.argmax(outputs):
            successes += 1
        total += 1

    print(successes/total)
        #if data_success/data_total == 1:
        #    for line in d:
        #        print(line['left_tree'].original,line['right_tree'].original, line['relation'] )
'''

print('Finished Training')
import matplotlib.pyplot as plt
plt.plot(success_rate)
plt.show()


__author__ = 'Sudhir Kylasa'

import os
import datetime
import sys
import math
import numpy as np

import torch
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torch.utils.data.sampler import WeightedRandomSampler

from torchvision.transforms import transforms

from dataset_loader import get_tensor_data_loader
from dataset_loader import get_cifar_data_loader
from dataset_loader_normalized import getNormalizedCIFAR
from dataset_loader_normalized import get_cifar_large_sampler_normalized
from alexnet_cifar import AlexNetCIFAR
from lenet_cifar import LeNetCIFAR
from lenet_cifar_kfac import LeNetCIFARKFAC
from kfac_utils import KFACUtils

from conjugate_gradient import ConjugateGradient
from subsampled_tr_cg import SubsampledTRCG
from subsampled_tr_cg_kfac import SubsampledTRCGKFAC
from subsampled_tr_cg_kfac_full import SubsampledTRCGKFACFULL
from subsampled_tr_cg_kfac_frank_wolfe import SubsampledTRCGKFACFrank
from tr_cg_utils import SubsampledTRCGParams
from tr_cg_utils import SubsampledTRCGStatistics

print( "======== TR CG Solver =========")

#Path variables here.
train_data = '/scratch/skylasa/solvers/cnn-data/cifar-10-batches-py/'
test_data = '/scratch/skylasa/solvers/cnn-data/cifar-10-batches-py/'
data_path = '/scratch/skylasa/solvers/cnn-data/cifar-10-batches-py/'

DEVICE = int( sys.argv[1] )
NETWORK = sys.argv[2]
ACTIVATION = sys.argv[3]
LOGFILE = sys.argv[4]
TRANSFORMS = sys.argv[5]

print( "...begin")

torch.manual_seed( 999 )
torch.cuda.manual_seed_all( 999 )
torch.cuda.set_device( DEVICE )

#Read the dataset here.
batch_size=256
train_size = 50000

if TRANSFORMS == 'no':
	sampler = WeightedRandomSampler( np.ones( train_size ) * (1./np.float(train_size)) , train_size, replacement=True )
	cifar_train, cifar_sampler = get_tensor_data_loader( cvs_path=train_data, batch_size=batch_size, shuffle=False, sampler=sampler)
	cifar_test, cifar_test_sampler = get_tensor_data_loader( cvs_path=test_data, fileFilter='test_batch*', batch_size=batch_size, shuffle=False)

	'''
	cifar_train, cifar_sampler, cifar_test = getNormalizedCIFAR( cvs_path=data_path, batch_size=batch_size, shuffle=False, sampler=sampler )
	#cifar_large_sampler = get_cifar_large_sampler_normalized( cifar_train, large_batch_size, sampler )
	'''

'''
if TRANSFORMS == 'yes':
	transform_train = transforms.Compose([
		transforms.RandomCrop(32, padding=4),
		transforms.RandomHorizontalFlip(),
		transforms.ToTensor(),
		transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
	])

	transform_test = transforms.Compose([
		transforms.ToTensor(),
		transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
	])

	batch_size=1024
	train_size = 50000
	sampler = WeightedRandomSampler( np.ones( train_size ) * (1./np.float(train_size)) , train_size, replacement=True )

	#cifar_train, cifar_sampler = get_cifar_data_loader( csv_path = data_path, batch_size=batch_size, shuffle=True, transform=transform_train, sampler=sampler)
	#cifar_test, tt  = get_cifar_data_loader( csv_path = data_path, batch_size=batch_size, shuffle=True, train=False, transform=transform_test)

	cifar_train, cifar_sampler, cifar_test = getNormalizedCIFAR( cvs_path='data_path', batch_size=1024, shuffle=True, sampler=sampler, train_trainform=transform_train, test_transform=transform_test )

'''

print( "...done loading data")
print( len( cifar_train.dataset) )
print( len( cifar_test.dataset ) )

print
print
print

#kfac here.
bias=False

damp_gamma  = 1e-8#Levenberg-Marquardt Dampening Term
regLambda = 1e-8  # Regularization Term
mv_avg_theta = 0.9 #moving average -- momemtum

kfac  = KFACUtils( ksize = 5, layers = ['conv', 'conv', 'lin', 'lin', 'lin' ], batchSize = batch_size, bias=bias, debug=False, gamma = damp_gamma, theta = mv_avg_theta, regLambda = regLambda)

#model
if NETWORK == 'lenet':
	model = LeNetCIFARKFAC( num_classes = 10, activation = ACTIVATION, batchSize=batch_size, kfac=kfac, bias=bias, r=regLambda);
else:
	model = AlexNetCIFAR( num_classes = 10, activation = ACTIVATION );
model.double ()
model.cuda ()

#criterion = nn.MSELoss(size_average=False, reduce=True)
criterion = nn.CrossEntropyLoss(size_average=False )
model.setLossFunction( criterion )
print( "...done network creation")

#Subsampled Trust Region solver here.
trparams = SubsampledTRCGParams ()
trparams.delta = np.float64( .01 )
trparams.max_delta = np.float64( 100 )
trparams.eta1 = np.float64 (0.8)
trparams.eta2 = np.float64 (1e-4)
trparams.gamma1 = np.float64 (2)
trparams.gamma2 = np.float64 (1.2)
trparams.max_props = float('Inf')
trparams.max_mvp = float('Inf')

trparams.max_iters = 500
trparams.sampleSize =  int( len(cifar_train.dataset) * 0.1 )

print( ".... sample Size: %d " % (trparams.sampleSize) )

trstats = SubsampledTRCGStatistics ( LOGFILE , console=True )
trstats.printHeader ()
print( "...done initialization params")

model.initOffsets ()
#model.initRandomWeights ()
trsolver = SubsampledTRCGKFACFrank( model, ConjugateGradient, trparams, trstats, cifar_train, cifar_test, cifar_sampler )
print( "...created the solver")

#
# Initialize the starting point
#
#trsolver.initializeZero ()
#trsolver.initializeRandom ()
trsolver.initKaiming ()

#
# and.... ACTION !!!
#
print("...begin solving TR")
print
print
trsolver.solve ()
print
print("...done with solver")

#
# and... SOLVER !!!
#
trstats.shutdown ()
print
print("....ALL DONE....")


	#!/usr/bin/python
	# -*- coding: utf-8 -*-
import numpy as np
from sklearn.preprocessing import normalize
import cv2
import imageio
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from StereoLab_Fine_Tuning import Depth_Estimation
from mlxtend.image import extract_face_landmarks
from scipy.spatial import distance
import sys

	# Creating 3D Landmarks using 2D data and Depth values which are estimated using the initial steps

def Landmarks3D(landmarks, z_ax):
	land3d = []
	xdata = []
	ydata = []
	zdata = []
	for r in landmarks:
	    if r[0] < len(z_ax) and r[1] < len(z_ax):
	        xdata.append(r[0])
	        ydata.append(r[1])
	        #print(r[0],r[1])
	        zdata.append(z_ax[r[0]][r[1]] * 40)
	        land3d.append([r[0], r[1], z_ax[r[0]][r[1]]])
	return (land3d, xdata, ydata, zdata)

# Plot the landmarks for better idea

def Plot3D(xdata, ydata, zdata):
	ax = plt.axes(projection='3d')
	ax.scatter3D(xdata, ydata, zdata, c=zdata)

	# ax.plot(ydata,xdata,zdata, color='r')

	plt.show()

def main():

	print('loading stock face data...')
	stock_data = sys.argv[1]
	print(stock_data)

	imgL = cv2.imread(stock_data+'L.jpg')  # downscale images for faster processing
	imgR = cv2.imread(stock_data+'R.jpg')
	imgL = cv2.resize(imgL, (400, 400), interpolation=cv2.INTER_AREA)
	imgR = cv2.resize(imgR, (400, 400), interpolation=cv2.INTER_AREA)
	landmarks = extract_face_landmarks(imgL)
	#print(len(landmarks))
	z_ax = Depth_Estimation(
	    imgL,
	    imgR,
	    5,
	    7,
	    55,
	    True,
	    )
	#print(len(z_ax))
	(land3d1, xdata1, ydata1, zdata1) = Landmarks3D(landmarks, z_ax)
	#Plot3D(xdata1,ydata1,zdata1)
	print('loading input face data...')
	input_data = sys.argv[2]
	print(input_data)

	imgL = cv2.imread(input_data+'L.jpg')  # downscale images for faster processing
	imgR = cv2.imread(input_data+'R.jpg')

	imgL = cv2.resize(imgL, (400, 400), interpolation=cv2.INTER_AREA)
	imgR = cv2.resize(imgR, (400, 400), interpolation=cv2.INTER_AREA)
	landmarks = extract_face_landmarks(imgL)
	#print(landmarks)
	z_ax = Depth_Estimation(
	    imgL,
	    imgR,
	    5,
	    7,
	    55,
	    True,
	    )
	#print(z_ax)
	(land3d2, xdata2, ydata2, zdata2) = Landmarks3D(landmarks, z_ax)
	#Plot3D(xdata2,ydata2,zdata2)
	res = 0
	ln = min(len(land3d1), len(land3d2))

	for i in range(0, ln):
		if land3d2[i][2] != float("inf") and land3d1[i][2] != float("inf"):
			d1 = np.asarray([land3d1[i][0],land3d1[i][1],land3d1[i][2]])
			d2 = np.asarray([land3d2[i][0],land3d2[i][1],land3d2[i][2]])
			res += distance.euclidean(d1, d2)

	# Best possible accuracy achieved by us is 2000, This is becuase of the collected data is not Stereo Accurate.

	# But, Sri data gave a great accuracy of less than 1000 residue.

	# With Roh data we did achieve at accuracy of 900 as well. But, with only certain stock and input combinations. Which is not reliable in real world sometimes.

	if res < 2000:
	    print('Face Recognised! and Residue is:', res)
	else:
	    print('No Match. Becuase residue is:', res)

if __name__ == '__main__':
	main()


import paho.mqtt.client as mqtt
import operator
import numpy as np

import tellopy
import collections

from emg.emglimbo import EMG_Preprocessor
from emg.emglimbo import EMG_Classifier

def handler(event, sender, data, **args):
    global drone
    drone = sender
    if event is drone.EVENT_FLIGHT_DATA:
        print(data)

def stop_drone():
    global drone
    drone.forward(0)
    drone.up(0)
    drone.down(0)
    drone.counter_clockwise(0)
    drone.clockwise(0)

def on_message(client, userdata, message):
    """Callback function
    """
    global memory
    global in_the_air
    global drone
    global preprocessor
    global classifier

    # Decode a message
    message_as_string = str(message.payload.decode("utf-8"))
    message_as_string = ''.join(message_as_string.split())  # remove all white characters

    # Process message
    packets = preprocessor.process(message_as_string)

    # Add to buffer checking if there's enough data to predict
    for packet in packets:
        preprocessor.append_to_buffer(packet)
        if preprocessor.check_buffered_data_size():
            features = preprocessor.get_features()

            # Predict a gesture and print
            prob_dist = classifier.classify(features)

            # Current gesture
            gesture = max(prob_dist.items(), key=operator.itemgetter(1))[0]
            print(f"****** {gesture} ******")

            # Save in memory
            memory.rotate(-1)
            memory[-1] = gesture

            # Take off / land
            if memory.count('pinch thumb-small') == 5:
                if not in_the_air:
                    drone.takeoff()
                    in_the_air = True
                else:
                    drone.land()
                    in_the_air = False

            # Palm landing
            '''
            if memory.count('pinch thumb-ring') == 5:
                if in_the_air:
                    drone.palm_land()
                    in_the_air = False
            '''

            # Move
            dynamism = 30
            if memory[-1] == memory[-2]:
                if memory[-1] == 'fist':
                    drone.forward(dynamism)
                #elif memory[-1] == 'pinch thumb-index':
                #    drone.up(dynamism)
                #elif memory[-1] == 'pinch thumb-middle':
                #    drone.down(dynamism)
                elif memory[-1] == 'flexion':
                    drone.counter_clockwise(dynamism)
                elif memory[-1] == 'extension':
                    drone.clockwise(dynamism)
                else:
                    stop_drone()

# MQTT data
broker_data = {
  "broker_address": "192.168.9.119"
}
topic = "sensors/data/emg"

# File created flag
file_created = False

# Preprocessor
window = 500000
stride = 500000
preprocessor = EMG_Preprocessor(window, stride)

# Create classifier model
model_path = "emg/model.tar"
classifier = EMG_Classifier(model_path, fake=False)

# Create drone
drone = tellopy.Tello()
drone.connect()
drone.wait_for_connection(60.0)
#drone.subscribe(drone.EVENT_FLIGHT_DATA, handler=handler)

# Create memory of last 5 gestures
memory = collections.deque(['idle'] * 5)

# In the air flag
in_the_air = False

# Create a client
print("Creating new subscriber instance")
client = mqtt.Client("emg_sub")

# Type username and password before connecting to broker
# client.username_pw_set(broker_data['username'], broker_data['password'])

# Connect with broker
print("Connecting to broker")
client.connect(broker_data['broker_address'])

# Subscribe to topic
print("Subscribing to topic")
client.subscribe(topic)

# Attach function to callback
client.on_message = on_message

try:
    # Start the loop
    client.loop_forever()

except KeyboardInterrupt:
    # Stop subscriber loop
    client.loop_stop()

    # Disconnect with drone
    drone.quit()


# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
These plugins modify the behavior of py.test and are meant to be imported
into conftest.py in the root directory.
"""

from .helper import pytest

# these pytest hooks allow us to mark tests and run the marked tests with
# specific command line options.
def pytest_addoption(parser):
    parser.addoption("--remote-data", action="store_true",
        help="run tests with online data")

def pytest_runtest_setup(item):
    if ('remote_data' in item.keywords and
        not item.config.getvalue("remote_data")):
        pytest.skip("need --remote-data option to run")

def pytest_report_header(config):
    from .. import __version__
    s = "\nTesting Astropy version {0}.\n".format(__version__)
    s += "Running tests in {0}.\n".format(" ".join(config.args))

    special_opts = ["remote_data", "pep8"]
    opts = []
    for op in special_opts:
        if getattr(config.option, op, None):
            opts.append(op)
    if opts:
        s += "Using Astropy options: {0}.\n".format(" ".join(opts))

    return s

@pytest.fixture(autouse=True)
def modarg(request):
    """Sets up environment variables to fake the config and cache
    directories, then removes the temporary directories.

    Does nothing if we are inside the sphinx testing command, as it
    should have already done this for us.
    """
    import os
    import shutil
    import tempfile

    # check if we're inside the distutils test command, which sets the
    # _ASTROPY_TEST_ builtin
    try:
        _ASTROPY_TEST_
        insidetestcmd = True
    except NameError:
        insidetestcmd = False

    if not insidetestcmd:
        oldconfigdir = os.environ.get('XDG_CONFIG_HOME')
        oldcachedir = os.environ.get('XDG_CACHE_HOME')
        os.environ['XDG_CONFIG_HOME'] = tempfile.mkdtemp('astropy_config')
        os.environ['XDG_CACHE_HOME'] = tempfile.mkdtemp('astropy_cache')
        os.mkdir(os.path.join(os.environ['XDG_CONFIG_HOME'], 'astropy'))
        os.mkdir(os.path.join(os.environ['XDG_CACHE_HOME'], 'astropy'))

        def teardown():
            #wipe the config/cache tmpdirs and restore the envars
            shutil.rmtree(os.environ['XDG_CONFIG_HOME'])
            shutil.rmtree(os.environ['XDG_CACHE_HOME'])
            if oldconfigdir is None:
                del os.environ['XDG_CONFIG_HOME']
            else:
                os.environ['XDG_CONFIG_HOME'] = oldconfigdir
            if oldcachedir is None:
                del os.environ['XDG_CACHE_HOME']
            else:
                os.environ['XDG_CACHE_HOME'] = oldcachedir

        request.addfinalizer(teardown)


import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from collections import Counter
from numpy import *
import random
from sklearn.isotonic import IsotonicRegression
from sklearn import metrics
from hausdorff import hausdorff_distance
import pywt
import math
import copy

import warnings
warnings.filterwarnings("ignore")

#### 数据预处理
def readData():
    f = open('/oldenburg_temp3.csv')
    df = pd.read_csv(f, usecols=['id', 'time', 'x', 'y'])
    df.sort_values(by=['id','time'],ascending= (True, True), inplace=True)
    #统计id列元素的值的个数
    #也就是每个id代表的轨迹有多少个点组成
    counts = dict(df['id'].value_counts())
    #小于20的删除
    todelete = [k for k,v in counts.items() if v<20 ]
    for key in todelete:
        df = df[~(df['id'].isin([key]))]
    for i in range(20,31):
        df = df[~(df['time'].isin([i]))]
    df = df.reset_index(drop = True)
    df = df.values
    arr = df.reshape(958, 20, 4)
    return arr

def list_sort_by_value(d):
    items = d.items()
    backitems = [[v[1], v[0]] for v in items]
    backitems.sort(reverse=True)
    return [backitems[i][1] for i in range(0, len(backitems))]

# 构建haar树
def haarWaveletTransform(data):
    coeffs = pywt.dwt(data, 'haar')
    # (cA, cD) : tuple   Approximation and detail coefficients.
    cA, cD = coeffs
    cA /= math.sqrt(2)
    cD /= math.sqrt(2)
    return cA,cD

def buildHaarTreeList(data):
    haarTreeList = []
    while(len(data)>=2):
        cA,cD = haarWaveletTransform(data)
        cD = cD.tolist()
        cD.reverse()
        # print(cA, cD)
        haarTreeList.extend(cD)
        data = cA
        if(len(data) == 1):
            cA = cA.tolist()
            cA.reverse()
            haarTreeList.extend(cA)
    haarTreeList.reverse()
    return haarTreeList

# 重建
def rebuildHaarTreeList(list):
    n = int(math.log(len(list), 2))
    # print(n)
    cA = list[0:1]
    for i in range(n):
        cD = list[int(math.pow(2,i)):int(math.pow(2,i+1))]
        data = math.sqrt(2) * pywt.idwt(cA, cD, 'haar')
        cA = data
    return data

# 添加噪声
def addNoise(n, eps):
    laplaceNoise = []
    h = math.log(n, 2)
    i=0
    while(i<n):
        if(i==0):
            layernumber = 0
        else:
            layernumber = int(math.log(i, 2))
        noise = np.random.laplace(0, (1)/ (eps * int(math.pow(2, h - layernumber))))
        laplaceNoise.append(noise)
        i = i+1
    return laplaceNoise

def mape(y_true, y_pred):
    n = len(y_true)
    mape = sum(np.abs((y_true - y_pred) / y_true)) / n * 100
    return mape

def myMechanism(n, e):
    a = readData()
    labels = np.zeros(shape=(958, 20))
    labels.dtype = 'int64'
    cluster_centers = np.zeros(shape=(n, 40))
    for i in range(20):
        # print(i)
        clf = KMeans(n_clusters=n, random_state=9)
        y_pred = clf.fit_predict(a[:, i, 2:4])
        labels[:, i] = clf.labels_
        cluster_centers[:, 2 * i:(2 * i + 2)] = clf.cluster_centers_
    newpaths = []
    for i in range(958):
        newpath = ""
        for j in range(20):
            string = "L" + str(labels[i, j])
            newpath += string
        newpaths.append(newpath)
    result = Counter(newpaths)
    newpathsdict = dict(result)

    # 随机生成轨迹补足数量
    while (len(newpathsdict) < 958):
        key = ""
        for i in range(20):
            string = "L" + str(random.randint(0, n - 1))
            key += string
        newpathsdict.setdefault(key, 0)

    # 补齐2的n次方haar变换和重构
    values = list(newpathsdict.values())
    a = int(math.log(len(values), 2))
    for index in range(int(math.pow(2, a + 1)) - len(values)):
        values.append(0)

    temp = buildHaarTreeList(values)
    # print(len(temp))
    noise = addNoise(len(temp), e)
    c = [temp[i] + noise[i] for i in range(len(temp))]
    noisecounts = rebuildHaarTreeList(c)

    i = 0
    newpathsdict2 = copy.deepcopy(newpathsdict);
    for key, value in newpathsdict.items():
        newpathsdict[key] = noisecounts[i]
        i = i + 1

    valueslist = sorted(list(newpathsdict.values()), reverse=True)
    newpathslist = list_sort_by_value(newpathsdict)

    truecounts = []
    for item in newpathslist:
        truecounts.append(newpathsdict2.get(item))

    # 根据一致性约束 trueconts 保序回归
    x = np.arange(958)
    y = np.array(truecounts)
    y_ = IsotonicRegression(increasing=False).fit_transform(x, y)

    # print(y.shape)
    NMI = metrics.normalized_mutual_info_score(y_, y)

    mapeyy = mape(y_, y)
    maeyy = metrics.mean_absolute_error(y, y_)
    # hausdorff_distance
    y.resize(1,958)
    y_.resize(1,958)
    # print(y.shape)
    hau_dis = hausdorff_distance(y, y_, distance="euclidean")

    return NMI, hau_dis,mapeyy,maeyy

nrange = [20,40,60,80]
erange = [0.1, 0.2, 0.5, 0.8]
nmi = []
h_d = []
mape1 = []
mae1 = []
for e in erange:
    for n in nrange:
        NMI, hau_dis, Mape, Mae = myMechanism(n, e)
        nmi.append(NMI)
        h_d.append(hau_dis)
        mape1.append(Mape)
        mae1.append(Mae)
    print("e %f finished" %(e))
print(nmi)
print(h_d)
print(mape1)
print(mae1)


import requests
from bs4 import BeautifulSoup
import math
import pandas
import re
import datetime
from tqdm import tqdm
import json
import re
#from nltk.tokenize import sent_tokenize

def get_news_data(keyword, query_list):

    news_dic={}

    print("query list : ", query_list)

    url = "https://search.naver.com/search.naver?"

    params = {
        "where": 'news',
        # 페이지네이션 값
        "start": 0,
    }

    today = datetime.date.today()

    news_index = 0

    for query in tqdm(query_list):
        params["query"]=query

        raw = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, params=params)
        news_list_html = BeautifulSoup(raw.text, "html.parser")

        articles = news_list_html.select("ul.list_news > li")
        title = articles[0].select_one("a.news_tit").text

        for i in range(0, 10):
            title = articles[i].select_one("a.news_tit").text
            try:
                if articles[i].select("a.info")[1].text =="네이버뉴스":
                    news_link = articles[i].select("a.info")[1].get('href')
                    press = articles[i].select_one("a.info.press").text

                    if press[-2:]=="선정":
                        press = press[:-2]

                    linked_news = requests.get(news_link, headers={'User-Agent': 'Mozilla/5.0'})
                    linked_news_html = BeautifulSoup(linked_news.text, "html.parser")

                    #content = linked_news_html.find_all("div",{"id":'articleBodyContents'})[0].text.replace("\n", "").split(". ")
                    content = linked_news_html.find_all("div",{"id":'articleBodyContents'})[0].text.replace("\n", "")
                    content = content.replace("// flash 오류를 우회하기 위한 함수 추가function _flash_removeCallback() {}", "")
                    split=re.compile('(?<=[^0-9])\.')
                    content=split.split(content)
                    content = [re.sub(r'\[[^]]*\]', '', x) for x in content]
                    """
                    숫자.숫자"를 제외한 모든 온점에서 split하는 것으로 고도화"""

                    index_ = today.isoformat().replace("-", "")+str(news_index).zfill(2)

                    news_dic[index_]={"query":params["query"], "title":title, "url":news_link, "press":press, "content":content}
                    news_index +=1
            except:
                pass

    with open('news_data/{today}_{keyword}.json'.format(today=today.isoformat(), keyword=keyword), 'w', encoding='utf-8') as make_file:

        json.dump(news_dic, make_file, indent="\t")

    return news_dic


from sense_hat import SenseHat
from time import sleep
from time import asctime
import json

class Reader():

    def __init__(self):
        pass
    #Display The Temprature on senseHat
    def displayTemp(self):
        import time
        sense = SenseHat()
        with open("config.json","r") as f:
            temp_dict = json.load(f)

        while True:
            temp = round(sense.get_temperature())
            #If the level of temperature is cold, display temperature with bluecolour.
            if (temp <=temp_dict["cold_max"]):
                message = '%d' %(temp)
                sense.show_message(message, scroll_speed=(0.1),text_colour=[0,0,255], back_colour= [0,0,0])
                time.sleep(10)
                sense.clear()

            #f the level of temperature is comfortable, display temperature with greencolour.
            if (temp > temp_dict["comfortable_min"] and temp <= temp_dict["comfortable_max"]):
                message = '%d' %(temp)
                sense.show_message(message, scroll_speed=(0.1),text_colour=[0,255,0], back_colour= [0,0,0])
                time.sleep(10)
                sense.clear()

            #If the level of temperature is hot, display temperature with redcolour.
            if (temp > temp_dict["hot_min"]):
                message = '%d' %(temp)
                sense.show_message(message, scroll_speed=(0.1),text_colour=[255,0,0], back_colour= [0,0,0])
                time.sleep(10)
                sense.clear()

def main():
    Reader1 = Reader()
    Reader1.displayTemp()

if __name__ == "__main__": main()


import sys
import json
import re
from Bio import Entrez
from Bio import SeqIO
from collections import defaultdict, OrderedDict
from scipy import stats
from utils import getBindingCore, importBindData,\
	 importData, reference_retreive, getBindingCore

def getGlycoAmid(options, data):

    # Initialize
    vaccSample = defaultdict(int)
    glycoCount = defaultdict(int)
    nonglycoCount = defaultdict(int)
    nonglycoSample = defaultdict(int)
    nonDeamidCount = defaultdict(int)
    nonDeamidSample = defaultdict(int)
    nonGlDa = defaultdict(int)
    nonGlDaSample = defaultdict(int)
    glycoRange = options['glycoRange']

    # Count the number of glycosylation
    for seq in data:

        # Initialize sequence
        AAseq = seq[1][2:-2]
        AAnonPTM = re.sub('\[.+?\]', '', AAseq)
        initPos = int(seq[2])
        endPos = initPos + len(AAnonPTM) -1
        pos_range = options['pos_range']
        NPos = 277 - initPos

        # If segement overlaps with defined range
        if not(initPos >= pos_range[1]) and not(endPos <= pos_range[0]):

            # Count sample
            vaccSample[seq[3]] +=1

            # Count if glycosylated
            if '+203.079' in AAseq:
                glycoCount[seq[3]] += 1

            ########### SCENARIO B ###########
            # If sequence contains both 277N and glycosylation sites
            if not(initPos >= glycoRange[1] ) and not(endPos <= glycoRange[0]) or 277 in range(initPos, endPos+1):

                # Find indexes and instances of PTMs
                PTM_idx = re.finditer('\[(.*?)\]', AAseq, re.DOTALL)
                PTM_instances = re.findall('\[(.*?)\]', AAseq, re.DOTALL)

                # Initialize variables
                idx_cumm = 0
                flag = 0

                # For each instance, if there is a glycosylation within the range, raise a flag
                for instance, idx in zip(PTM_instances, PTM_idx):
                    if instance == '+203.079' and idx.start() -1 -idx_cumm + initPos in range(glycoRange[0], glycoRange[1]+1):
                        flag = 1
                    idx_cumm += len(instance) + 2

                # If there is a deamidatio in 277N, raise a flag
                if not(AAseq[NPos+2:NPos+8] == '+0.984'):
                    flag = 1

                # If flag was not raised, then the sequence did not contain neither deamidation nor glycosylation. Count.
                if flag == 0:
                    nonGlDa[seq[3]] += 1

                # Count the sample of sequences that contained both 277N and glycosylation sites
                nonGlDaSample[seq[3]] +=1

            ########### SCENARIO A ###########

            # If the sequence contains glycsylation site
            if not(initPos >= glycoRange[1] ) and not(endPos <= glycoRange[0]):

                # Find indexes and instances of PTMs
                PTM_idx = re.finditer('\[(.*?)\]', AAseq, re.DOTALL)
                PTM_instances = re.findall('\[(.*?)\]', AAseq, re.DOTALL)

                # Initialize variables
                idx_cumm = 0
                flag = 0

                # For each instance, check glycosylation and raise flag
                for instance, idx in zip(PTM_instances, PTM_idx):
                    if instance == '+203.079' and idx.start() -1 -idx_cumm + initPos in range(286, 288+1):
                        flag = 1
                    idx_cumm += len(instance) + 2

                # If flag not raised (no glyco.) count
                if flag == 0:
                    nonglycoCount[seq[3]] += 1

                # Count sample
                nonglycoSample[seq[3]] += 1

            # Count non-amidated 277N
            if 277 in range(initPos, endPos+1):
                if not(AAseq[NPos+2:NPos+8] == '+0.984'):
                    nonDeamidCount[seq[3]] += 1
                nonDeamidSample[seq[3]] += 1

    return vaccSample, glycoCount, nonglycoCount, nonglycoSample, nonDeamidCount, nonDeamidSample, nonGlDa, nonGlDaSample

def fisherTest(glycoCount, vaccSample):

    # Initialize
    fisherResult = defaultdict(lambda : defaultdict(int))

    # For each vaccine compute Fisher test
    for vacc in ['ARP','FOC']:

        # Create arrays
        glycoPos = [glycoCount[vacc], glycoCount['PAN']]
        glycoNeg = [vaccSample[vacc] - glycoCount[vacc], \
            vaccSample['PAN'] - glycoCount['PAN']]

        # Compute test
        oddsratio, pvalue = stats.fisher_exact([glycoPos, glycoNeg])
        fisherResult[vacc]['oddsratio'] = oddsratio
        fisherResult[vacc]['pvalue'] = pvalue

    return fisherResult

def main():

    # Read options
    with open('options.json') as inFile:
        options = json.load(inFile)

    # Set pos range to analyze segement of interest
    options['pos_range'] = [277, 323]

    # Import data
    data = importData(options)

    # Get glycosylation count, and non-glycosylated sites and non-amidated 277N
    vaccSample, glycoCount, nonglycoCount, nonglycoSample \
        , nonDeamidCount, nonDeamidSample, nonGlDa, nonGlDaSample = getGlycoAmid(options, data)

    # Compute fisher test
    fisherResult = fisherTest(glycoCount, vaccSample)

    # Print
    print('\n')
    print('Glycosylation Fisher\'s test result:')
    for vacc in fisherResult:
        print(vacc + ' {:.2%} ({})'.format(glycoCount[vacc]/vaccSample[vacc], vaccSample[vacc]) + \
            ' vs. PAN {:.2%} ({}): pvalue = {:.2}, oddsratio = {:.2}'.format( \
            glycoCount['PAN']/vaccSample['PAN'], vaccSample['PAN'], \
            fisherResult[vacc]['pvalue'], fisherResult[vacc]['oddsratio']))

    # Print probabilities of deamidation or glycosylation as separate entities which can
    # apper in different sequences. This method does not allow to compute a Fisher's test, but
    print('\n')
    print('SCENARIO A: Considering 277N and glycosylation sites as different entities that can occur in separate segments')
    print('Probability of non-deamydated and non-glycosylated in ARP:' + \
         '{:.2%}'.format((nonglycoCount['ARP']/nonglycoSample['ARP']) * (nonDeamidCount['ARP']/nonDeamidSample['ARP'])))
    print('Probability of non-deamydated and non-glycosylated in FOC:' + \
         '{:.2%}'.format((nonglycoCount['FOC']/nonglycoSample['FOC']) * (nonDeamidCount['FOC']/nonDeamidSample['FOC'])))
    print('Probability of non-deamydated and non-glycosylated in PAN:' + \
         '{:.2%}'.format((nonglycoCount['PAN']/nonglycoSample['PAN']) * (nonDeamidCount['PAN']/nonDeamidSample['PAN'])))

    # Print probabilities of sequences that include both 277N and glycosylation sites.
    # This allows to compute Fisher's test PAN vs ARP and FOC.
    print('\n')
    print('SCENARIO B: Considering only sequence that included both 277N and glycosylation sites:')
    print('Probability of non-deamydated and non-glycosylated in ARP:' + \
          '{:.2%}'.format((nonGlDa['ARP']/nonGlDaSample['ARP'])))
    print('Probability of non-deamydated and non-glycosylated in FOC:' + \
          '{:.2%}'.format((nonGlDa['FOC']/nonGlDaSample['FOC'])))
    print('Probability of non-deamydated and non-glycosylated in PAN:' + \
          '{:.2%}'.format((nonGlDa['PAN']/nonGlDaSample['PAN'])))

     # Fisher test on SCENARIO B:
    nonGlDaFisher = fisherTest(nonGlDa, nonGlDaSample)

    # Print
    print('\n')
    for vacc in nonGlDaFisher:
        print('Fisher\'s test result for SCENARIO B:')
        print(vacc + ' {:.2%} ({})'.format(nonGlDa[vacc]/nonGlDaSample[vacc], nonGlDaSample[vacc]) + \
            ' vs. PAN {:.2%} ({}): pvalue = {:.2}, oddsratio = {:.2}'.format( \
            nonGlDa['PAN']/nonGlDaSample['PAN'], nonGlDaSample['PAN'], \
            nonGlDaFisher[vacc]['pvalue'], nonGlDaFisher[vacc]['oddsratio']))

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

import sqlite3

conn = sqlite3.connect('employees.db')
c = conn.cursor()

c.execute("select * from employees_data")
#c.execute('insert into employees_data(id , name , salary , position , hireDATE) values(? , ?, ?, ?, ? ,?)' , entities)
#entities = (2 , 'rahul' , 1 , 'caretaker' , '2019-01-05')

for row in c:
    print(row)

c.fetchone()

a = c.fetchall()
for row in a:
    print(row)


# _*_ coding: utf-8 _*_
from telebot import TeleBot
from datetime import datetime
import requests
import json
import constants
import variables

bot = TeleBot(constants.token)

# ~~~~~~~~~~~~~~COMMANDS~~~~~~~~~~~~~~~

@bot.message_handler(commands=['start'])
def handle_start(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print('New SESSION')
    print("Received a COMMAND")
    bot.send_message(message.chat.id, 'Чем могу помочь?')
    logs(message)
    log_mess(message)

@bot.message_handler(commands=['help'])
def handle_help(message):
    bot.send_message(message.chat.id,
                     'Привет!\n'
                     'Это я, Питончик (оригинальное имя для меня придумали однако)!\n'
                     '{0}, если у тебя есть оригинальные идеи как бы меня усовершенствовать, то пиши Максиму ('
                     '@kushnirov)\n '
                     'Я еще маленький, но я буду расти.\n'
                     'Если ты хочешь помочь моему создателю (не материально, '
                     'а умственно (разрабатывать меня(да, это скобки в скобках))), то пиши Максу(@kushnirov)\n'
                     '🐍🐍🐍🐍🐍🐍'.format(message.chat.first_name))

@bot.message_handler(commands=['translate'])
def handle_translate(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("COMMAND TRANSLATE")
    if message.text == '/translate' and variables.translate:
        print('Translate mode OFF')
        bot.send_message(message.chat.id, 'Режим переводчика выключен!')
        constants.translate = False
    elif message.text == '/translate':
        constants.translate = True
        print('Translate mode ON')
        bot.send_message(message.chat.id, 'Переведено сервисом «Яндекс.Переводчик»\n'
                                          'http://translate.yandex.ru/')
        bot.send_message(message.chat.id, 'Ты активировал режим переводчика!\n'
                                          'Теперь просто вводи текст на русском, а я буду присылать такой же текст на '
                                          'английском. Еще ты можешь прислать документ, который необходимо перевести '
                                          'Чтобы выключить режим переводчика вбей еще раз эту комманду - /translate')

@bot.message_handler(content_types=["text"])
def handle_text(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("Message")
    logs(message)
    if variables.translate:
        translate(message)
    if message.text:
        log_mess(message)

@bot.message_handler(content_types=["document"])
def handle_doc(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("Received a DOCUMENT")
    logs(message)
    print(message.document)
    if message.document:
        pass

@bot.message_handler(content_types=["audio"])
def handle_audio(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("Received an AUDIO")
    logs(message)

@bot.message_handler(content_types=["sticker"])
def handle_sticker(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("Received a STICKER")
    logs(message)

@bot.message_handler(content_types=["photo"])
def handle_photo(message):
    print('\n~~~~~~~~~~~~~~~~~~~')
    print("Received a PHOTO")
    logs(message)
    if message.photo:
        log_photo(message)

# ~~~~~~~~~~~~~TRANSLATE~~~~~~~~~~~~~~~
def translate(message):
    if variables.translate:
        url = variables.translate_url
        key = constants.key
        text = message.text
        lang = 'ru-en'
        request = requests.post(url, data={'key': key, 'text': text, 'lang': lang})
        translated_string = json.loads(request.text)
        bot.send_message(message.chat.id, translated_string['text'])
        print('Answer: {0}'.format(translated_string['text'][0]))

# ~~~~~~~~~~~~~~~LOG~~~~~~~~~~~~~~~~~~
def log_photo(message):
    print('File_ID: {0}'.format(message.photo[1].file_id))
    print('Photo_Size(WxH): {0}x{1}'.format(message.photo[1].width, message.photo[1].height))
    print('Photo_Size: {0}'.format(message.photo[0].file_size))

def log_mess(message):
    print('Message: {0}'.format(message.text.encode('utf-8')))

def logs(message):
    date = datetime.now()
    print(date)
    print('Username: {0}'.format(message.chat.username))
    print('F_Name: {0}'.format(message.chat.first_name))
    print('L_Name: {0}'.format(message.chat.last_name))
    print('ID: {0}'.format(message.chat.id))

bot.polling(none_stop=True, interval=0)


from sys import stdin, setrecursionlimit
from collections import deque
from itertools import combinations
import math

setrecursionlimit(10 ** 4)

# step1: 바이러스 리스트에 담기
# step2: 활성 바이러스 선택 -> combinations
# step3: 선택된 바이러스들로 퍼지는 시간 계산 -> bfs
# step4: step3 비교하여 최소 시간 선별

N, M = map(int, stdin.readline().split(" "))
MAP = []
minSpreadTime = float("inf")

for _ in range(N):
    row = list(map(int, stdin.readline().split(" ")))
    MAP.append(row)

# step1
viruses = []
for i in range(N):
    for j in range(N):
        if MAP[i][j] == 2:
            viruses.append([i, j])

def bfs(activeViruses):
    global N, MAP
    visited = [[-1 for _ in range(N)] for _ in range(N)]
    queue = deque()

    for activeVirus in activeViruses:
        queue.append(activeVirus)
        visited[activeVirus[0]][activeVirus[1]] = 0

    dy = [-1, 0, 1, 0]
    dx = [0, 1, 0, -1]

    while queue:
        y, x = queue.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < N and 0 <= nx < N:
                if visited[ny][nx] == -1:
                    if MAP[ny][nx] == 0:
                        visited[ny][nx] = visited[y][x] + 1
                        queue.append([ny, nx])
                    #! 비활성 바이러스
                    elif MAP[ny][nx] == 2:
                        visited[ny][nx] = visited[y][x] + 1
                        queue.append([ny, nx])

    maxTime = 0
    for i in range(N):
        for j in range(N):
            if MAP[i][j] == 0:
                if visited[i][j] == -1:
                    return -1
                else:
                    maxTime = max(maxTime, visited[i][j])
    return maxTime

# step2
for activeViruses in combinations(viruses, M):
    # step3
    spreadTime = bfs(activeViruses)
    # 퍼뜨린다면
    if spreadTime != -1:
        # step4
        minSpreadTime = min(spreadTime, minSpreadTime)

if math.isinf(minSpreadTime):
    print(-1)
else:
    print(minSpreadTime)


import sys

# テストロジック（個別）：static.py
def call_static_py(function_name):

    import static

    # 引数群を取得
    if function_name == 'get_args':
        print('### static.get_args() ###')
        tmp = static.get_args()
        print(tmp)

    # 引数を取得
    if function_name == 'get_arg':
        print('### static.get_arg() ###')
        tmp = static.get_arg()
        print(tmp)

    # 今日の日付をYYYYMMDD形式で返す
    if function_name == 'get_sysdate8':
        print('### static.get_sysdate8() ###')
        tmp = static.get_sysdate8()
        print(tmp)

    # 現在時刻をYYYYMMDDHHMMSS形式で返す
    if function_name == 'get_systime14':
        print('### static.get_systime14() ###')
        tmp = static.get_systime14()
        print(tmp)

    # 現在時刻をYYYYMMDDHHMMSS形式で返す
    if function_name == 'get_systime20':
        print('### static.get_systime20() ###')
        tmp = static.get_systime20()
        print(tmp)

    # 端末ごとのパスを返す [最も単純な０〜９の数値予測用画像の入ったフォルダ]
    if function_name == 'get_folderpath_files0to10':
        print('### static.get_folderpath_files0to10() ###')
        tmp = static.get_folderpath_files0to10()
        print(tmp)

    # 端末ごとのパスを返す [画像ファイル]
    if function_name == 'get_filepath_dere_score':
        print('### static.get_filepath_dere_score() ###')
        tmp = static.get_filepath_dere_score()
        print(tmp)

def main():

    # 引数群を取得
    call_static_py('get_args')

    # 引数を取得
    call_static_py('get_arg')

    # 今日の日付をYYYYMMDD形式で返す
    call_static_py('get_sysdate8')

    # 現在時刻をYYYYMMDDHHMMSS形式で返す
    call_static_py('get_systime14')

    # 現在時刻をYYYYMMDDHHMMSS形式で返す
    call_static_py('get_systime20')

    # 端末ごとのパスを返す [最も単純な０〜９の数値予測用画像の入ったフォルダ]
    call_static_py('get_folderpath_files0to10')

    # 端末ごとのパスを返す [画像ファイル]
    call_static_py('get_filepath_dere_score')

# ルート処理
if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print(f'except Exception as e: e_[{e}]')
        sys.exit(-1)


from shutil import copyfile
from json import loads
from os import path, chdir, mkdir

def customSplitExt (name):
	custom = path.splitext (name)
	if len (custom[0])>0 and custom[0][0] == '.':
		return ('', custom [0])
	return custom

def traverse (j,desination,dir='.',space=0,) :
	written = 0
	if 'content' not in j:
		raise ValueError ('Json wrong format: Container has no content key')
	for file in j['content']:
		if 'name' not in file:
			raise ValueError ('Json wrong format: %s' % str (file))

		tmp =  customSplitExt (file['name'])[1]
		# print (tmp);

		if (tmp == '.drive'):
			continue

		if 'content' not in file:

			print (' ' * space, "%-30s" % file['name'], sep ='', end='')
			try:
				# myzip.write (dir+"\\"+file['name']);
				copyfile (dir+"\\"+file['name'], desination + "\\" + file['name'])
				size = path.getsize (dir+"\\"+file['name'])
				written += size
				print ('... %d byte(s)' % size)
			except Exception as e:
				print ('... ERROR:', e)

		else:
			print (' ' * space, file['name']+"\\", sep ='')
			mkdir (desination+"\\"+file['name'])
			size = traverse (file, desination+"\\"+file['name'], dir+"\\"+file['name'], space+3)
			written += size

	return written
def getRoot ():
	abspath = path.abspath (__file__)
	# print (abspath)
	fname = path.split (abspath)[0]

	return fname
def function (name='.drive'):
	try:
		f = open (name, 'r')
		j = loads (f.read ())
		f.close ()
	except Exception as e:
		print ('Error:',e)
		print ('Contact files creator or go to HNhu homepage for more details')
		return

	if ('module' not in j):
		print ('Invalid module name')
		return

	fname = path.basename (name)
	fBaseNameNoExt = customSplitExt (fname) [0]
	fdir  = name [0:len (name)-len (fname)]
	if (fdir == ''):
		fdir = '.'
	chdir (fdir)
	print ('Copying files...')
	# print ('At %s:' % path.abspath (fdir))

	try:
		mkdir (getRoot () + "/" + j['module'])
	except Exception as e:
		print ('Error:',e)
		print ('You may need to delete the existed one ("%s") to reinstall this module, installation FAILED => stopped' % (getRoot () + "\\"+j['module']))

		return
	traverse (j, getRoot ()+"/" + j['module'] , '.', 0)

def main ():

	# function ()
	import argparse
	import sys
	parser = argparse.ArgumentParser(description='Installer')
	parser.add_argument('--file', help='Specify which .drive file for installing', default = '.drive', type=str)
	try:
		args = parser.parse_args (sys.argv[1: ])
		function (args.file)
	except Exception as e:
		print ("Error found:", e)
		print ('You mean: nhu <a .drive file name> ?')

if __name__=='__main__':
	main ()


import csv, json

position = []

with open("position.csv", "r") as infile:
	reader = csv.reader(infile, delimiter=",")
	next(reader)
	for row in reader:
		position.append({"x": float(row[0]), "y":float(row[1])})

for row in position:
	print (row)

with open("position.json", "w") as outfile:
	json.dump(position, outfile)


import unittest
from unittest import TestCase
from urllib import request, parse

from teamcity import is_running_under_teamcity
from teamcity.unittestpy import TeamcityTestRunner

class TestVerifications(TestCase):
    def test_insert_when_name_has_number_a_error_appears(self):
        data = parse.urlencode({
            'inputId': '1',
            'inputName': 'name2',
            'inputLastname': 'last_name',
            'inputBirthday': '1994-12-10'
        }).encode()
        html = request.urlopen('http://localhost:6543/add_person', data).read().decode('utf-8')
        self.assertIn('errors_div', html)

if __name__ == '__main__':
    if is_running_under_teamcity():
        runner = TeamcityTestRunner()
    else:
        runner = unittest.TextTestRunner()
    unittest.main(testRunner=runner)


import utils
import sys
import classifiers
from matplotlib import pylab as pl
import numpy as np

def clf_symdiff(clf1, clf2):
    sv_set1 = set(clf1.support_)
    sv_set2 = set(clf2.support_)
    return len(sv_set1 ^ sv_set2)

def clf_symdiff_normalized(clf1, clf2):
    sv_set1 = set(clf1.support_)
    sv_set2 = set(clf2.support_)
    return len(sv_set1 ^ sv_set2) / len(sv_set1 | sv_set2)

def clf_new(clf1, clf2):
    sv_set1 = set(clf1.support_)
    sv_set2 = set(clf2.support_)

CLF_DIFF_TYPES = {
    "sn": ("SV normalized symmetric difference", clf_symdiff_normalized),
    "s": ("SV symmetric difference", clf_symdiff)
}
def margin_cov_pic(margins, clfs, C, fig, diff_type = "sn"):
    diff = CLF_DIFF_TYPES[diff_type][1]
    clfs_num = len(clfs)
    covs_num = round(clfs_num * (clfs_num - 1) / 2)
    covs = np.zeros(covs_num)
    difs = np.zeros(covs_num)
    idx = 0
    for i in range(clfs_num):
        for j in range(i + 1, clfs_num):
            covs[idx] = np.corrcoef(margins[:, i], margins[:, j])[0, 1]
            difs[idx] = diff(clfs[i], clfs[j])
            idx += 1
    fig.scatter(difs, 1 - covs, marker='o', s=90, label="C = %f" % C, c=np.random.rand(3,1))

def spec_repr(spec):
    ret = "Kernel type: "
    if spec[0] == "linear":
        return ret + "linear"
    if spec[0] == "rbf":
        return ret + "rbf, gamma = %f" % spec[1]
    if spec[0] == "poly":
        return ret + "poly, degree = %d" % spec[1]
    if spec[0] == "ink":
        return ret + "ink, degree = %d, downer limit (a) = %f" % (spec[1], spec[2])

def print_kernel_info(C, clfs, specs, margins):
    print("C = %f" % C)
    for idx, clf in enumerate(clfs):
        print("%s\nSV number = %d" % (spec_repr(specs[idx]), clf.n_support_[0] + clf.n_support_[1]))
    print("")

def draw_corr_diff(dataset_name, C_list=[1.0], diff_type = "sn"):
    data = utils.get_data()
    dataset = data[dataset_name]
    print(dataset_name)
    print(dataset[0].shape)
    specs = utils.get_classifier_specs()
    fig = pl.figure(figsize=(8, 6), dpi=150)
    ax1 = fig.add_subplot(111)
    for C in C_list:
        clfs = classifiers.get(specs, C=C)
        utils.train_clfs(clfs, dataset[0], dataset[1])
        margins = utils.get_margins(clfs, dataset[0])
        print_kernel_info(C, clfs, specs, margins)
        margin_cov_pic(margins, clfs, C, ax1, diff_type=diff_type)
    pl.legend(loc='upper left')
    pl.xlabel(CLF_DIFF_TYPES[diff_type][0])
    pl.ylabel("Margin vectors correlation")
    pl.show()

def main(argv):
    draw_corr_diff("heart", [1])

if __name__ == "__main__":
    sys.exit(main(sys.argv))


import argparse
import os

from PIL import Image

def image_argparser():
    parser = argparse.ArgumentParser(description='Resize some images.')
    parser.add_argument("image_path", help="Path to image you want to resize")
    parser.add_argument("--width", type=int, help="Width of resulting image")
    parser.add_argument("--height", type=int, help="Height of resulting image")
    parser.add_argument("--scale", type=float,
                        help="How much to scale resulting image")
    parser.add_argument("--output", help="Path to resulting image")
    return parser.parse_args()

def get_image(filepath):
    if not os.path.exists(filepath):
        return None
    return Image.open(filepath)

def save_image(image, filepath):
    try:
        image.save(filepath)
    except IOError:
        print("cannot save to disk", filepath)

def set_path_to_result(path_to_original, resulting_size):
    path_without_name, original_name = os.path.split(
        path_to_original)
    original_name_without_extension, extension = os.path.splitext(
        original_name)
    resulting_name = ''.join([original_name_without_extension, '__',
                              str(resulting_size[0]), 'x',
                              str(resulting_size[1]), extension])
    return os.path.join(path_without_name, resulting_name)

def print_ratio_if_incorrect(original_size, resulting_size):
    original_ratio = original_size[0]/original_size[1]
    resulting_ratio = resulting_size[0]/resulting_size[1]
    if original_ratio != resulting_ratio:
        message = "Original ratio {} isn't equal to resulting ratio {}".format(
            original_ratio, resulting_ratio)
        print(message)

def resize_image_with_both_sizes(original_image, resulting_size):
    return original_image.resize(resulting_size)

def resize_image_with_one_size(original_image, resulting_size):
    if resulting_size[0] is not None:
        resulting_height = (resulting_size[0]*original_image.size[1] /
                            original_image.size[0])
        resulting_size = (resulting_size[0], resulting_height)
    else:
        resulting_width = (resulting_size[1] * original_image.size[0] /
                           original_image.size[1])
        resulting_size = resulting_width, resulting_size[1]
    original_image.thumbnail(resulting_size)
    return original_image

def resize_image_scale(original_image, scale):
    resulting_size = (int(original_image.size[0]*scale),
                      int(original_image.size[1]*scale))
    if scale < 1:
        original_image.thumbnail(resulting_size)
        return original_image
    else:
        return original_image.resize(resulting_size)

def resize_image(path_to_original, resulting_size, scale, path_to_result):
    original_image = get_image(path_to_original)
    if (resulting_size[0] and resulting_size[1]) is not None:
        print_ratio_if_incorrect(original_image.size, resulting_size)
        resulting_image = resize_image_with_both_sizes(
            original_image, resulting_size)
    elif (resulting_size[0] or resulting_size[1]) is not None:
        resulting_image = resize_image_with_one_size(original_image,
                                                     resulting_size)
    elif scale is not None:
        resulting_image = resize_image_scale(original_image, scale)
    else:
        resulting_image = original_image
    if path_to_result is None:
        path_to_result = set_path_to_result(path_to_original,
                                            resulting_image.size)
    save_image(resulting_image, path_to_result)

if __name__ == '__main__':
    console_arguments = image_argparser()
    if not (console_arguments.scale and (console_arguments.width or
                                         console_arguments.height)):
        resulting_size = (console_arguments.width, console_arguments.height)
        resize_image(console_arguments.image_path, resulting_size,
                     console_arguments.scale, console_arguments.output)
        print('Image resized!')
    else:
        print('You cannot use size arguments and scale argument together')