from random import randrange
def pole(souradnice):
    """
    Vykreslí pole s "hadem" a ovocem.
    """
    for j in range(vyska):
        for i in range(sirka):
            if (i,j) in souradnice:
                print("X", end = " ")
            elif(i,j) in ovoce:
                print("?",end = " ")
            else:
                print(".", end = " ")
        print()

def posun(souradnice,smer,ovoce):
    """
    Zajišťuje pohyb hada.
    """
    while True:
        pocitadloOvoce = 0
        pridanaSouradnice = souradnice[-1]
        x, y = pridanaSouradnice
        if smer == 's':
            novaSourednice = x, y-1
        elif smer == 'j':
            novaSourednice = x, y+1
        elif smer == 'v':
            novaSourednice = x+1, y
        elif smer == 'z':
            novaSourednice = x-1, y
        else:
            False

        x,y = novaSourednice

        if novaSourednice in souradnice:
            print("Had se snaží pokousat")
            return False

        if x < 0 or x > sirka or y < 0 or y > vyska:
            print("Had se snaží utéct z pole")
            return False

        if novaSourednice in ovoce:  # had jí ovoce
            souradnice.append(novaSourednice)
            ovoce.remove(novaSourednice) #vymazání ovoce z pole
            return souradnice

        else:
            souradnice.pop(0)  # posun hada
            souradnice.append(novaSourednice)
            pocitadloOvoce += 1
            return souradnice

souradnice  = [(0, 0), (1, 0), (2, 0)]
ovoce = [(2,3)]
pocitadloOvoce = 0
vyska = 0
sirka = 0

while vyska < 4 and sirka < 4:
    sirka=int(input("Jak má být široké pole? Zadej minimálně 4."))
    vyska=int(input("Jak má být vysoké pole? Zadej minimálně 4."))
pole(souradnice)

while True:
    pocitadloOvoce += 1
    if len(ovoce) == 0 or pocitadloOvoce%30 == 0:   #vytvoření ovoce, když v poli žádné jiné není.
        noveOvoce = (randrange(0,sirka),(randrange(0,vyska)))
        ovoce.append(noveOvoce)

    smer = input("Kudy se má had vydat? Zvol s pro sever, j pro jih, v pro východ a z pro západ.")
    while True:
        if smer in ["s","j","v","z"]:
            break
        else:
            smer = input("Zadaljsi špatný směr. Zvol s pro sever, j pro jih, v pro východ a z pro západ. ")
            True

    print("Počet odehraných kol: ",pocitadloOvoce)

    try:
        pole(posun(souradnice,smer, ovoce))
    except TypeError:
        print("Game over")
        break


# coding=utf-8
from behave import given, when, then, use_step_matcher
from hamcrest import assert_that, equal_to
import re
from login_utils import *
from behave import *

@then(u'she should not see any watchlists')
def see_no_watchlists(context):
    context.browser.get(context.server_address + "/")
    watchlist_found = re.search("Cryptocurrency :", context.browser.page_source, re.IGNORECASE)
    if watchlist_found == '':
        pass

@then(u'she should see her list of watchlists')
def see_watchlists(context):
    context.browser.get(context.server_address + "/")
    watchlist_found = re.search("My lists", context.browser.page_source, re.IGNORECASE)
    assert watchlist_found

@then(u'she should see her list of watchlists with current value')
def see_watchlists_current_value(context):
    context.browser.get(context.server_address + "/")
    currentvalue_found = re.search("Current Value :", context.browser.page_source, re.IGNORECASE)
    assert currentvalue_found

@then(u'she should see her list of watchlists with historical value')
def see_watchlists_historical_value(context):
    context.browser.get(context.server_address + "/")
    historicalvalue_found = re.search("Historical Value :", context.browser.page_source, re.IGNORECASE)
    assert historicalvalue_found


"""
QUESTION:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

ANSWER:
Using first col and row to record if certain rows and cols need to set to zero
"""
class Solution:
    # @param matrix, a list of lists of integers
    # RETURN NOTHING, MODIFY matrix IN PLACE.
    def setZeroes(self, matrix):
        firstRowZero = False
        firstColZero = False
        for i in range(len(matrix)):
            if matrix[i][0] == 0:
                firstColZero = True
                break
        for i in range(len(matrix[0])):
            if matrix[0][i] == 0:
                firstRowZero = True
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        for i in range(1,len(matrix[0])):
            if matrix[0][i] == 0:
                for j in range(len(matrix)):
                    matrix[j][i] = 0
        for i in range(1,len(matrix)):
            if matrix[i][0] == 0:
                for j in range(len(matrix[0])):
                    matrix[i][j] = 0
        if firstRowZero:
            for i in range(len(matrix[0])):
                matrix[0][i] = 0
        if firstColZero:
            for i in range(len(matrix)):
                matrix[i][0] = 0

if __name__ == '__main__':
    print


from PIL import Image
import numpy as np
import pandas as pd
import image_processing

def get_statistics(input_path):

    image_paths = image_processing.get_images_paths(input_path)

    non_background_pixels_counts = []
    pixels_counts = []
    image_widths = []
    image_heights = []
    image_background_percentages = []

    for image_path in image_paths:
        image = Image.open(image_path)
        arr = np.array(image)

        width, height = image.size
        pixels_nr = width * height
        if arr.shape[2] == 4:
            non_background_pixels_count = (arr[:, :, 3] != 0).sum()
            image_background_percentage = 1.0 - non_background_pixels_count / pixels_nr

            non_background_pixels_counts.append(non_background_pixels_count)
            image_background_percentages.append(image_background_percentage)
        pixels_counts.append(pixels_nr)
        image_widths.append(width)
        image_heights.append(height)

        image.close()
    if arr.shape[2] == 4:
        return pd.DataFrame({'Path': image_paths,
                             'Pixels_count': pixels_counts,
                             'Width': image_widths,
                             'Height': image_heights,
                             'Background_percentage': image_background_percentages
                             })
    else:
        return pd.DataFrame({'Path': image_paths,
                             'Pixels_count': pixels_counts,
                             'Width': image_widths,
                             'Height': image_heights,
                             })

if __name__ == '__main__':
    input_path = './training_set'

    df = get_statistics(input_path)


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

"""
get data from aiida pk
"""
import numpy as np
import warnings
from pymatgen.core.structure import Structure
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.io.phonopy import get_phonopy_structure
from aiida.orm import load_node, QueryBuilder, Node, WorkChainNode, StructureData
from aiida.common import exceptions
from aiida.common import NotExistentAttributeError
from aiida.cmdline.utils.decorators import with_dbenv
from aiida.plugins import WorkflowFactory
from phonopy import Phonopy
from aiidaplus.utils import get_kpoints

RELAX_WF = WorkflowFactory('vasp.relax')
PHONOPY_WF = WorkflowFactory('phonopy.phonopy')

@with_dbenv()
def get_node_from_pk(pk):
    """
    get node from pk
    """
    return load_node(pk)

def check_process_class(node,
                        expected_process_class:str):
    """
    check process class of node is the same as the expected

    Args:
        node: aiida node
        expected_process_class (str): expected process class

    Raises:
        AssertionError: input node is not the same as expected
    """
    assert node.process_class.get_name() == expected_process_class, \
            "input node: {}, expected: {} (NOT MATCH)". \
            format(node.process_class, expected_process_class)

def get_structure_data_from_pymatgen(pmgstructure:Structure,
                                     symprec:float=1e-5) -> dict:
    """
    get structure data from pymatgen structure

    Raises:
        Warning: final structure do not exist

    Note:
        symprec=1e-5 (default), which is the same as VASP SYMPREC default
    """
    analyzer = SpacegroupAnalyzer(pmgstructure, symprec=symprec)
    dataset = analyzer.get_symmetry_dataset()

    dic = {}
    dic['symprec'] = symprec
    dic['volume'] = pmgstructure.lattice.volume
    dic['lattice'] = pmgstructure.lattice.matrix.tolist()
    dic['lattice_abc'] = list(pmgstructure.lattice.abc)
    dic['lattice_angles'] = list(pmgstructure.lattice.angles)
    dic['international'] = dataset['international']
    dic['pointgroup'] = dataset['pointgroup']
    dic['natoms'] = len(pmgstructure.species)
    dic['wyckoffs'] = dataset['wyckoffs']
    dic['site_symmetry_symbols'] = dataset['site_symmetry_symbols']
    dic['hall'] =dataset['hall']
    dic['hall_number'] = dataset['hall_number']

    return dic

def get_structure_data(pk, symprec=1e-5) -> dict:
    """
    get structure data from pk

    Note:
        symprec=1e-5 (default), which is the same as VASP SYMPREC default
    """
    node = get_node_from_pk(pk)
    pmgstructure = node.get_pymatgen()
    dic = get_structure_data_from_pymatgen(pmgstructure, symprec=symprec)
    dic['pk'] = pk
    dic['data_type'] = 'StructureData'
    return dic

def get_vasp_data(pk, symprec=1e-5) -> dict:
    """
    get vasp data from pk

    Note:
        - symprec=1e-5 (default), which is the same as VASP SYMPREC default
        - Warning: final structure does not exist
    """
    node = get_node_from_pk(pk)
    check_process_class(node, 'VaspWorkChain')
    results = node.outputs.misc.get_dict()

    structure = {}
    initial_structure_pk = node.inputs.structure.pk
    initial_structure = get_structure_data(initial_structure_pk,
                                           symprec=symprec)
    structure['initial'] = initial_structure
    try:
        final_structure_pk = node.outputs.structure.pk
        final_structure = get_structure_data(final_structure_pk,
                                             symprec=symprec)
        structure['final'] = final_structure
    except exceptions.NotExistent:
        warnings.warn("final structure does not exist")

    kpoints = get_kpoints(
            structure=load_node(initial_structure_pk).get_pymatgen_structure(),
            mesh=node.inputs.kpoints.get_kpoints_mesh()[0],
            )
    kpoints['intervals'] = kpoints['intervals'].tolist()

    dic = {}
    dic['data_type'] = node.process_class.get_name()
    dic['pk'] = pk
    dic['incar'] = node.inputs.parameters.get_dict()
    dic['parser_settings'] = node.inputs.settings.get_dict()['parser_settings']
    dic['potential_family'] = node.inputs.potential_family.value
    dic['potential_mapping'] = node.inputs.potential_mapping.get_dict()
    dic['kpoints'] = kpoints
    dic['structure'] = structure
    dic['maximum_force'] = results['maximum_force']
    dic['maximum_stress'] = results['maximum_stress']
    dic['energy_no_entropy'] = results['total_energies']['energy_no_entropy']

    return dic

def get_relax_data(pk, symprec=1e-5) -> dict:
    """
    get relax data from pk

    Note:
        - symprec=1e-5 (default), which is the same as VASP SYMPREC default
        - Warning: final vasp calculation is not static calc
    """
    node = get_node_from_pk(pk)
    check_process_class(node, 'RelaxWorkChain')

    # drop final static calculation
    vasp_pks = [ vasp_node.called[0].pk for vasp_node in node.called ]
    vasp_pks.reverse()
    if 'nsw' not in \
            get_node_from_pk(vasp_pks[-1]).inputs.parameters.get_dict().keys():
        del vasp_pks[-1]
    else:
        warnings.warn("final vasp calculation is not static calc")
    vasp_results = {}
    for i, vasp_pk in enumerate(vasp_pks):
        vasp_results['step_%02d' % i] = get_vasp_data(vasp_pk,
                                                           symprec=symprec)

    dic = {}
    dic['data_type'] = node.process_class.get_name()
    dic['pk'] = pk
    dic['initial_structure_pk'] = node.inputs.structure.pk
    dic['final_structure_pk'] = node.outputs.relax__structure.pk
    dic['final_energy_no_entropy'] = \
        node.outputs.misc.get_dict()['total_energies']['energy_no_entropy']
    dic['steps'] = vasp_results
    try:
        dic['max_force'] = float(np.linalg.norm(node.outputs.forces.get_array('final'), axis=1).max())
    except NotExistentAttributeError:
        print("outputs forces is not exists in relax pk:{}".format(pk))
    try:
        dic['stress'] = node.outputs.stress.get_array('final').tolist()
    except NotExistentAttributeError:
        print("outputs forces is not exists in relax pk:{}".format(pk))

    return dic

def get_phonon_data(pk, get_phonon=False):
    """
    get phonon object from aiida pk
    """
    node = load_node(pk)
    symprec = node.inputs.symmetry_tolerance.value
    dic = {}
    dic['data_type'] = node.process_class.get_name()
    dic['pk'] = pk
    dic['calculator_settings'] = node.inputs.calculator_settings.get_dict()
    dic['symmetry_tolerance'] = symprec
    dic['phonon_setting_info'] = node.outputs.phonon_setting_info.get_dict()
    dic['structure'] = {
            'input': get_structure_data(node.inputs.structure.pk, symprec),
            'primitive': get_structure_data(node.outputs.primitive.pk, symprec),
            'supercell': get_structure_data(node.outputs.supercell.pk, symprec),
            }

    if get_phonon:
        pmgstructure = node.inputs.structure.get_pymatgen()
        unitcell = get_phonopy_structure(pmgstructure)
        phonon_settings = node.outputs.phonon_setting_info.get_dict()
        phonon = Phonopy(unitcell,
                         supercell_matrix=phonon_settings['supercell_matrix'],
                         primitive_matrix=phonon_settings['primitive_matrix'])
        phonon.set_displacement_dataset(phonon_settings['displacement_dataset'])
        phonon.set_forces(node.outputs.force_sets.get_array('force_sets'))
        phonon.produce_force_constants()
        return dic, phonon
    else:
        return dic

def get_shear_data(pk):
    """
    get shear data
    """
    # get called pks
    node = load_node(pk)
    rlx_qb = QueryBuilder()
    rlx_qb.append(Node, filters={'id':{'==': pk}}, tag='wf')
    rlx_qb.append(RELAX_WF, with_incoming='wf', project=['label', 'id'])
    ph_qb = QueryBuilder()
    ph_qb.append(Node, filters={'id':{'==': pk}}, tag='wf')
    ph_qb.append(PHONOPY_WF, with_incoming='wf', project=['label', 'id'])
    relaxes = rlx_qb.all()
    relaxes.sort(key=lambda x: x[0])
    phonons = ph_qb.all()
    phonons.sort(key=lambda x: x[0])

    dic = {}
    dic['pk'] = pk
    dic['calculator_settings'] = node.inputs.calculator_settings.get_dict()
    dic['shear_conf'] = node.inputs.shear_conf.get_dict()
    dic['parent'] = get_structure_data(node.outputs.parent.pk)
    dic['relax_results'] = node.outputs.relax_results.get_dict()
    dic['shear_ratios'] = node.outputs.shear_ratios.get_dict()['shear_ratios']
    dic['gamma'] = node.outputs.gamma.value
    dic['relax_pks'] = np.array(relaxes)[:,1].astype(int).tolist()
    dic['phonon_pks'] = np.array(phonons)[:,1].astype(int).tolist()

    return dic

def get_twinboundary_relax_data(pk):
    """
    get twinboudnary relax data
    """
    # get called pks
    node = load_node(pk)
    rlx_qb = QueryBuilder()
    rlx_qb.append(Node, filters={'id':{'==': pk}}, tag='wf')
    rlx_qb.append(RELAX_WF, with_incoming='wf', project=['label', 'id'])
    relaxes = rlx_qb.all()
    dic = {}
    dic['relax_pks'] = [ relax[1] for relax in relaxes ]
    dic['relax_pks'].sort()

    return dic

# def get_twinboundary_data(pk):
#     """
#     get twinboudnary data
#     """
#     # get called pks
#     node = load_node(pk)
#     qb = QueryBuilder()
#     qb.append(Node, filters={'id':{'==': pk}})
#     qb.append(WorkChainNode, tag='workchain')
#     qb.append(StructureData, with_outgoing='workchain', project=['id', 'label'])
#     structure_pks = qb.all()
#     structure_pks = list(reversed(structure_pks))
#
#     dic = {}
#     dic['twinboudnary_summary'] = node.outputs.twinboundary_summary.get_dict()
#     dic['structure_pks'] = structure_pks
#     dic['vasp_results'] = node.outputs.vasp_results.get_dict()
#
#     return dic


#3.Write a program which accept N numbers from user and store it into List. Return Minimum number from that List.
#Input : Number of elements : 4
#Input Elements : 13 5 45 7
#Output : 5

def accept_min():
    num = int(input("Enter the numbers : "))
    arr = list()
    for i in range(0,num):
        n = int(input("Enter the num in list :"))
        arr.append(n)
    print(arr)
    print("Lowest no is  : ",min(arr))

accept_min()


# -*- coding: utf-8 -*-
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

import re
import uuid
from datetime import datetime
from spider_frame.crawler import SimpleCrawler, divide
from spider_frame import create_mysql_dbsession
from spider_frame.utils import my_join, timestamp2datetime, utc2local, extract_numbers, run_locally
from parsel import Selector
from spider_frame.chrome_downloader import chrome_downloader

class SevenM_team_crawler(SimpleCrawler):
    def __init__(self, settings):
        super(SevenM_team_crawler, self).__init__(settings)
        self.dbsession = create_mysql_dbsession()
        self.base_url = 'http://data.7m.com.cn/matches_data/92/gb/index.shtml'
        self.headers = {
            'Proxy-Connection': "keep-alive",
            'Pragma': "no-cache",
            'Cache-Control': "no-cache",
            'Upgrade-Insecure-Requests': "1",
            'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
            'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            'Accept-Encoding': "gzip, deflate",
            'Accept-Language': "zh-CN,zh;q=0.9",
        }
        self.update_next_task = True

    def split_input(self, num):
        tasks = [{'team_list': 1}]
        return divide(tasks, num)

    def work(self, input_data):
        if self.base_url is None:
            return

        for task in input_data:
            url = self.base_url
            self.crawl_url(url, task)

    def crawl_url(self, url, *args):
        task, = args
        try:
            r = chrome_downloader(timeout=60*5, headless=True).get_page(url)
            if r == None:
                raise Exception('DOWNLOAD PAGE ERROR')
            self.process_response(r, task)
            return True
        except Exception:
            self.logger.exception('failed to get or parse %s', url)

        self.put_url(url, *args)
        self.logger.error('FATAL: failed to get url: %s' % url)
        return False

    def check_response(self, response):
        return super().check_response(response)

    def process_response(self, response, *args):
        task, = args
        sel = Selector(response)
        body = sel.xpath('//*[@id="Match_Table"]//tbody//tr[@align="middle"]')
        print('<<<<<< team list >>>>>>>')

        match_pattern = r"javascript:Team_gb\((\d+)\)"
        for row in body[1:]:
            tds = row.xpath('.//td')
            home_no = tds[2].xpath('.//a/@href').extract_first()
            home_no = int(re.match(match_pattern, home_no).group(1))
            home_name = my_join(tds[2].xpath('.//a//text()').extract())
            guest_no = tds[4].xpath('.//a/@href').extract_first()
            guest_no = int(re.match(match_pattern, guest_no).group(1))
            guest_name = my_join(tds[4].xpath('.//a//text()').extract())

            print(home_no, home_name, guest_no, guest_name)

            home_item ={
                'id': home_no,
                'name': home_name,
                'snapshot_time': utc2local(datetime.utcnow()),
                }

            guest_item ={
                'id': guest_no,
                'name': guest_name,
                'snapshot_time': utc2local(datetime.utcnow()),
                }

            self.save_data(home_item)
            self.save_data(guest_item)

    def save_data(self, data):
        self.dbsession.create(
            'zucai.7m_team',
            data,
            mode="REPLACE"
        )
        self.logger.info('save data ok.')

def _run_locally(test=False):
    run_locally(Crawler=SevenM_team_crawler, setting_name='7m', test=test)

def _run_simple():
    from spider_frame.settings import config
    s = SevenM_team_crawler(settings = config.CRAWLER_SETTINGS['7m'])
    s.crawl_url('http://data.7m.com.cn/matches_data/92/gb/index.shtml', {'team_list': 1})

if __name__ == '__main__':
    _run_simple()


def s(arr):
    # 가로 세로 검증
    for i in range(9):
        ai = []
        aj = []
        for j in range(9):
            if arr[i][j] not in ai:
                ai.append(arr[i][j])
            if arr[j][i] not in aj:
                aj.append(arr[j][i])
        if len(ai) != 9 or len(aj) != 9:
            return 0

    for i in range(0, 7, 3):
        for j in range(0, 7, 3):
            ak = []
            for r in range(i, i + 3):
                for c in range(j, j + 3):
                    if arr[r][c] not in ak:
                        ak.append(arr[r][c])
            if len(ak) != 9:
                return 0
    return 1
T = int(input())
for tc in range(1, T+1):
    arr = [input().split() for _ in range(9)]
    print('#{} {}'.format(tc, s(arr)))

0822솔빙클럽/스도쿠검증.py


from copy import deepcopy
from typing import Dict, Optional

import pandas as pd

from backend.inference_core.reapply.data_structures.Provenance.state import State
from backend.inference_core.reapply.data_structures.record import Record
from backend.inference_core.reapply.data_structures.types import (
    BrushAction,
    InteractionType,
)

NodeId = str

class Node(object):
    def __init__(
        self,
        id,
        label,
        metadata,
        state,
        target: pd.DataFrame,
        target_id: str,
        children=[],
        parent="",
        artifacts=None,
        **kwargs
    ):
        eventType = metadata["eventType"]
        self.target = target
        self.target_id = target_id
        self.id = id
        self.label = label
        self.eventType = eventType
        self.children_ids = children
        self.parent_id = parent
        self._artifacts = artifacts
        self.state = State(**state)
        self.node = None
        self.processed_record: Optional[Record] = None

    def infer(self, nodes: Dict[str, "Node"]):
        if self.parent_id in nodes.keys():
            self.parent = nodes[self.parent_id]
        else:
            self.parent = None
        self.children = []
        for id in self.children_ids:
            if id in nodes.keys():
                self.children.append(nodes[id])

    def setNode(self, node):
        self.node = node

    @property
    def record(self):
        if self.processed_record is None:
            self.process()
        assert self.processed_record is not None
        return deepcopy(self.processed_record)

    def process(self):
        int_type = self.interaction_type
        if int_type == InteractionType.ROOT:
            self.processed_record = Record()
            return

        record = self.parent.record

        if not self.should_apply:
            self.processed_record = record
            return

        if int_type == InteractionType.ADD_PLOT:
            interaction = self.state.add_plot_interaction
            record.update_plot(interaction.plot)
        elif int_type == InteractionType.REMOVE_PLOT:
            interaction = self.state.remove_plot_interaction
            record.remove_plot(interaction.plotId)
        elif int_type == InteractionType.POINT_SELECTION:
            interaction = self.state.point_selection_interaction
            record.add_point_selection(interaction.plot, interaction.selected)
        elif int_type == InteractionType.BRUSH:
            interaction = self.state.brush_interaction
            action = interaction.action
            if action == BrushAction.ADD:
                record.add_brush(interaction.plot, interaction.brush, self.target)
            elif action == BrushAction.UPDATE:
                record.update_brush(interaction.plot, interaction.brush, self.target)
            elif action == BrushAction.REMOVE:
                record.remove_brush(interaction.plot, interaction.brush_id)
        elif int_type == InteractionType.SELECT_PREDICTION:
            interaction = self.state.select_prediction_interaction
            record.set_prediction(interaction.prediction, self.target, self.target_id)
        elif int_type == InteractionType.FILTER:
            interaction = self.state.filter_interaction
            record.set_filter(interaction.filterType)

        self.processed_record = record

    @property
    def interaction(self):
        return self.state.interaction

    @property
    def interaction_type(self):
        return self.interaction.type

    @property
    def artifact(self):
        if self._artifacts is None or "customArtifacts" not in self._artifacts:
            return None
        arts = self._artifacts["customArtifacts"]

        if len(arts) == 0:
            return None

        art = arts[-1]["artifact"]

        return art

    @property
    def should_apply(self):
        if self.target_id == self.artifact["original_dataset"]:
            return True
        return self.artifact["status_record"][self.target_id] != "Rejected"

    @property
    def is_approved(self):
        if self.target_id == self.artifact["original_dataset"]:
            return True
        return self.artifact["status_record"][self.target_id] == "Accepted"

    def serialize(self):
        ser = self.node
        ser["state"] = self.state.__dict__
        return ser


#!/usr/bin/python3

#taken from:
#https://create.arduino.cc/editor/LogMaker360/b6f3d6dd-ae21-46ea-af75-38be4caebba6/preview

import time
import csv
import sys
import os
csvfile = "temp.csv"
als = True
while als:
    ostemp = os.popen('vcgencmd measure_temp').readline()
    #print ('ostemp: ',ostemp)
    temperature = (ostemp.replace("temp=", "").replace("'C\n", ""))
    #print(temperature)

    if temperature is not None:
      print(temperature)
    else:
      print ('can not connect to the sensor!')
    timeC = time.strftime("%I")+':' +time.strftime("%M")+':'+time.strftime("%S")
    data = [temperature, timeC]

    with open(csvfile, "a")as output:
        writer = csv.writer(output, delimiter=",", lineterminator = '\n')
        writer.writerow(data)
    time.sleep(6) # update script every 60 seconds


"""
输入数字,求其中能被2和3整除的数之和
"""

val = []
while True:
    num = input("请输入数值:")
    if not num: break
    val.append(int(num))
int_val = [e for e in val if e % 2 == 0 and e % 3 == 0]
print(int_val)
print(sum(int_val))


"""empty message

Revision ID: e8143468c7c1
Revises: 11ee20a79069
Create Date: 2016-04-07 22:52:52.476000

"""

# revision identifiers, used by Alembic.
revision = 'e8143468c7c1'
down_revision = '11ee20a79069'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('waitlist_entry_fits',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('entryID', sa.Integer(), nullable=True),
    sa.Column('fitID', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['entryID'], ['waitlist_entries.id'], ),
    sa.ForeignKeyConstraint(['fitID'], ['fittings.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.drop_constraint(u'fittings_ibfk_2', 'fittings', type_='foreignkey')
    op.drop_column(u'fittings', 'waitlist_id')
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column(u'fittings', sa.Column('waitlist_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True))
    op.create_foreign_key(u'fittings_ibfk_2', 'fittings', 'waitlist_entries', ['waitlist_id'], ['id'], onupdate=u'CASCADE')
    op.drop_table('waitlist_entry_fits')
    ### end Alembic commands ###


# Pure python implementation of MicroPython framebuf module.
# This is intended for boards with limited flash memory and the inability to
# use the native C version of the framebuf module.  This python module can be
# added to the board's file system to provide a functionally identical framebuf
# interface but at the expense of speed (this python version will be _much_
# slower than the C version).
# This is a direct port of the framebuf module C code to python:
#   https://github.com/micropython/micropython/blob/master/extmod/modframebuf.c
# Original file created by Damien P. George.
# Python port below created by Tony DiCola, and modified by Aidan Campbell

from res.font_petme128_8x8 import font_petme128_8x8

# Framebuf format constants:
MVLSB = 0  # Single bit displays (like SSD1306 OLED)
RGB565 = 1  # 16-bit color displays
GS4_HMSB = 2  # Unimplemented!

class MVLSBFormat:

    def setpixel(self, fb, x, y, color):
        index = (y >> 3) * fb.stride + x
        offset = y & 0x07
        fb.buf[index] = (fb.buf[index] & ~(0x01 << offset)) | ((color != 0) << offset)

    def getpixel(self, fb, x, y):
        index = (y >> 3) * fb.stride + x
        offset = y & 0x07
        return (fb.buf[index] >> offset) & 0x01

    def fill_rect(self, fb, x, y, width, height, color):
        while height > 0:
            index = (y >> 3) * fb.stride + x
            offset = y & 0x07
            for ww in range(width):
                fb.buf[index + ww] = (fb.buf[index + ww] & ~(0x01 << offset)) | ((color != 0) << offset)
            y += 1
            height -= 1

class RGB565Format:

    def setpixel(self, fb, x, y, color):
        index = (x + y * fb.stride) * 2
        fb.buf[index] = (color >> 8) & 0xFF
        fb.buf[index + 1] = color & 0xFF

    def getpixel(self, fb, x, y):
        index = (x + y * fb.stride) * 2
        return (fb.buf[index] << 8) | fb.buf[index + 1]

    def fill_rect(self, fb, x, y, width, height, color):
        while height > 0:
            for ww in range(width):
                index = (ww + x + y * fb.stride) * 2
                fb.buf[index] = (color >> 8) & 0xFF
                fb.buf[index + 1] = color & 0xFF
            y += 1
            height -= 1

class FrameBuffer:

    def __init__(self, buf, width, height, buf_format=MVLSB, stride=None):
        self.buf = buf
        self.width = width
        self.height = height
        self.stride = stride
        if self.stride is None:
            self.stride = width
        if buf_format == MVLSB:
            self.format = MVLSBFormat()
        elif buf_format == RGB565:
            self.format = RGB565Format()
        else:
            raise ValueError('invalid format')
        self.font_data = font_petme128_8x8

    def fill(self, color):
        self.format.fill_rect(self, 0, 0, self.width, self.height, color)

    def fill_rect(self, x, y, width, height, color):
        if width < 1 or height < 1 or (x + width) <= 0 or (y + height) <= 0 or y >= self.height or x >= self.width:
            return
        xend = min(self.width, x + width)
        yend = min(self.height, y + height)
        x = max(x, 0)
        y = max(y, 0)
        self.format.fill_rect(self, x, y, xend - x, yend - y, color)

    def pixel(self, x, y, color=None):
        if x < 0 or x >= self.width or y < 0 or y >= self.height:
            return
        if color is None:
            return self.format.getpixel(self, x, y)
        else:
            self.format.setpixel(self, x, y, color)

    def hline(self, x, y, width, color):
        self.fill_rect(x, y, width, 1, color)

    def vline(self, x, y, height, color):
        self.fill_rect(x, y, 1, height, color)

    def rect(self, x, y, width, height, color):
        self.fill_rect(x, y, width, 1, color)
        self.fill_rect(x, y + height, width, 1, color)
        self.fill_rect(x, y, 1, height, color)
        self.fill_rect(x + width, y, 1, height, color)

    def line(self):
        raise NotImplementedError()

    def blit(self):
        raise NotImplementedError()

    def scroll(self):
        raise NotImplementedError()

    def text(self, text, x, y0, color=1):
        for char in text:
            ascii_val = ord(char)
            if ascii_val < 32 or ascii_val > 127:
                ascii_val = 127
            start_index = (ascii_val - 32) * 8
            char_data = self.font_data[start_index: start_index + 8]

            for byte in char_data:
                if 0 <= x < self.width:  # clip x
                    vline_data = byte  # each byte is a column of 8 pixels, LSB at top
                    y = y0
                    while vline_data:
                        if vline_data & 1:
                            if 0 <= y < self.height:  # clip y
                                self.pixel(x, y, color)
                        vline_data >>= 1
                        y += 1
                x += 1

class FrameBuffer1(FrameBuffer):
    def blit(self):
        super().blit()

    def scroll(self):
        super().scroll()

    def line(self):
        super().line()


#! python3
# 假设你有一项无聊的工作，要调整数千张图片的大小，并在每张图片的角上增 加一个小徽标水印
import os
from PIL import Image
SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'
logoIm = Image.open(LOGO_FILENAME)
logoWidth,logoHeight = logoIm.size
for filename in os.listdir('.'):
    if not (filename.endswith('.png') or filename.endswith('.jpg')) \
        or filename == LOGO_FILENAME:
        continue  # skip non-image files and the logo file itself
    im = Image.open(filename)
    width, height = im.size
    if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
        if width > height:
            height = int((SQUARE_FIT_SIZE / width) * height)
            width = SQUARE_FIT_SIZE
        else:
            width = int((SQUARE_FIT_SIZE / height) * width)
            height = SQUARE_FIT_SIZE
        # Resize the image.
        print('Resizing %s...' % (filename))
        im = im.resize((width, height))
        print('Adding logo to %s...' % (filename))
        im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)

        # Save changes.
        im.save(os.path.join('withLogo', filename))


import csv
import pandas as pd
import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib.pyplot as plt
from SK_Agreement.Randomness_Tests.approximate_entropy_test import approximate_entropy_test
from SK_Agreement.Randomness_Tests.cumulative_sums_test import cumulative_sums_test
from SK_Agreement.Randomness_Tests.monobit_test import monobit_test
from SK_Agreement.Randomness_Tests.serial_test import serial_test
import cmath
import itertools
from SK_Agreement.info_reconciliation import divide_into_parts
from SK_Agreement.Randomness_Tests.monobit_test import count_ones_zeroes

def read_coefficients_from_csv(filename, columns_file):
    """
    Read coefficients from csv file.

    :param filename: str (e.g. alice_wifi_capture.csv)
    :param columns_file: str (path to column names for file)
    :return vectors: list (2-D list of channel coefficients. file lines  x channel count (52))

    """
    correct_column_names = open(columns_file, mode="r").readlines()
    correct_column_names = [line.strip() for line in correct_column_names]

    csv_reader = csv.DictReader(open(filename, mode="r"))
    current_column_names = csv_reader.fieldnames

    with open(columns_file) as f:
        for i, l in enumerate(f):
            pass
    file_length = i + 1

    # if this is the first time we are using this file we need headers
    if current_column_names != correct_column_names:
        df = pd.read_csv(filename, usecols=range(file_length), names=correct_column_names, header=None)
        df.to_csv(filename, encoding='utf-8', index=False)

    channel_coefficients = []

    with open(filename, mode="r") as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            line = []
            for number in range(52):
                line.append(row[f"CH_{number}"])
            channel_coefficients.append(line)

    return channel_coefficients

def compute_absolutes(raw_channel_coefficients):
    """
    Replaces "i" with "j" in channel readings then computes the absolutes

    :param: raw_channel_coefficients: 2-D list of complex values
    :return processed_channel_coefficients: 2-D list of floats

    """

    processed_channel_coefficients = []

    for row in raw_channel_coefficients:
        line = []
        for channel in row:
            try:
                value = float('%.4f' % abs(complex(channel)))
            except:
                print("Error with complex values")
                print(channel)
            line.append(value)

        processed_channel_coefficients.append(line)

    return processed_channel_coefficients

def compute_phase(raw_channel_coefficients):
    """
    Replaces "i" with "j" in channel readings then computes the absolutes

    :param: raw_channel_coefficients: 2-D list of complex values
    :return processed_channel_coefficients: 2-D list of floats

    """

    processed_channel_coefficients = []

    for row in raw_channel_coefficients:
        line = []
        for channel in row:
            line.append(float('%.4f' % cmath.phase(complex(channel))))
        processed_channel_coefficients.append(line)

    return processed_channel_coefficients

def extract_real_part(raw_channel_coefficients):
    """
    Replaces "i" with "j" in channel readings then extract the real part of the readings

    :param: raw_channel_coefficients: 2-D list of complex values
    :return processed_channel_coefficients: 2-D list of floats

    """

    processed_channel_coefficients = []

    for row in raw_channel_coefficients:
        line = []
        for channel in row:
            line.append(float('%.4f' % complex(channel.replace('i', 'j')).real))
        processed_channel_coefficients.append(line)

    return processed_channel_coefficients

def extract_imag_part(raw_channel_coefficients):
    """
    Replaces "i" with "j" in channel readings then extract the imag part of the readings

    :param: raw_channel_coefficients: 2-D list of complex values
    :return processed_channel_coefficients: 2-D list of floats

    """

    processed_channel_coefficients = []

    for row in raw_channel_coefficients:
        line = []
        for channel in row:
            line.append(float('%.4f' % complex(channel.replace('i', 'j')).imag))
        processed_channel_coefficients.append(line)

    return processed_channel_coefficients

def best_fit_line_threshold(data):
    """
    Finds the best fit 2nd order polynomial for data.
    Quantizes data using the polynomial as a threshold.

    :param: data: list of floats (e.g. absolutes of 52 channels)
    :return quantized_measurments: list of floats

    """

    x = np.arange(len(data))
    y = data

    b, m1, m2 = polyfit(x, y, 2)

    best_fit_line = b + m1 * x + m2 * x * x

    #plt.plot(x, y, '.')
    #plt.plot(x, b + m1 * x + m2 * x * x, '-')
    #plt.show()

    quantized_measurements = y

    for n in range(len(y)):
        if y[n] > best_fit_line[n]:
            quantized_measurements[n] = 1
        elif y[n] < best_fit_line[n]:
            quantized_measurements[n] = 0

    return quantized_measurements

def check_randomness(key):
    """
    Runs randomness tests from NIST on key.
    All tests have to pass to declare the key random.
    Selected tests:
        - monobit: checks that there are about as many 1s as 0s
        - approximate entropy:  compare the frequency of overlapping blocks of two consecutive/adjacent lengths
          (m and m+1) against the expected result for a random sequence
        - cumulative sums: determine whether then cumulative sum of the partial sequences occurring in the tested
          sequence is too large or too small relative to the expected behavior of that cumulative sum
          for random sequences.
        - serial: checks the frequency of all possible overlapping m-bit patterns

    :param: key: list of ints (generated in quantization)
    :return test_result: bool

    """
    monobit_result, monobit_p, _ = monobit_test(key)

    # if monobit test doesn't pass there is no need to run the others
    if monobit_result:
        approx_entropy_result, approx_entropy_p, _ = approximate_entropy_test(key)
        cumulative_sums_result, _, cumulative_sums_plist = cumulative_sums_test(key)
        serial_result, _, serial_plist = serial_test(key, patternlen=3)
        test_result = approx_entropy_result and cumulative_sums_result and serial_result
    else:
        test_result = monobit_result

    return test_result

def average_channel_thresholding_abs(coefficients):
    """
    Computes average for each channel from measurements uses that for threshold.
    Then uses a sliding window of length 10 to pick out the channels from the each measurement.
    If the largest absolute value in that sample is positive than it quantizes this measurement as a 1.
    Otherwise it quantizes it as a 0.
    :param coefficients: 2-D list
    :return: key: list
    """

    np_processed_coefficients = np.asarray(coefficients)
    channel_mean = np_processed_coefficients.mean(axis=0)
    channel_std = np_processed_coefficients.std(axis=0)

    sequence = []
    max_deviation_channels = []

    for i in range(len(coefficients)):
        all_channels = np_processed_coefficients[i] - channel_mean - 0 * channel_std

        #for selector in range(3):
        selector = i % 3
        selected_channels = all_channels[selector * 10 + 11 : (selector + 1) * 10 + 11]
        #selected_channels = all_channels[24+selector]
        abs_max = selected_channels.flat[abs(selected_channels).argmax()]

        if np.sign(abs_max) == 1:
            sequence.append(1)
        else:
            sequence.append(0)

        all_channels = list(all_channels)
        max_deviation_channels.append(all_channels.index(abs_max))

    return sequence, max_deviation_channels

def average_channel_thresholding_phase(coefficients, max_deviation_channels):
    """
    Computes average for each channel from measurements uses that for threshold.
    Then uses a sliding window of length 10 to pick out the channels from the each measurement.
    If the largest absolute value in that sample is positive than it quantizes this measurement as a 1.
    Otherwise it quantizes it as a 0.
    :param coefficients: 2-D list
    :return: key: list
    """

    np_processed_coefficients = np.asarray(coefficients)
    channel_mean = np_processed_coefficients.mean(axis=0)
    channel_std = np_processed_coefficients.std(axis=0)

    sequence = []

    for i in range(len(coefficients)):
        all_channels = np_processed_coefficients[i] - channel_mean - 0 * channel_std

        selected_channels = all_channels[max_deviation_channels[i]]

        abs_max = selected_channels.flat[abs(selected_channels).argmax()]

        if np.sign(abs_max) == 1:
            sequence.append(1)
        else:
            sequence.append(0)

    return sequence

def quantize_measurements(data_file, averaging_intervals_count, averaging_length, column_file):

    """
    Generates qunatized bits from csv. Only read the file once. Averaging threshold absolute quantization.
    Returns list of  bit sequences
    :return: list of list of 1s and 0s as single string
    """

    raw_coefficients = read_coefficients_from_csv(data_file, column_file)

    averaging_start = 150
    averaging_stop = averaging_start + averaging_length

    all_processed_coefficients_abs = compute_absolutes(raw_coefficients)
    all_processed_coefficients_phase = compute_phase(raw_coefficients)

    quantized_all = []

    #channels_to_remove_from_each_averaging_interval = []

    for i in range(averaging_intervals_count):

        #channels_to_remove = high_low_threshold(all_processed_coefficients_abs[averaging_start:averaging_stop])
        #channels_to_remove_from_each_averaging_interval.append(channels_to_remove)

        quantized_abs, max_deviation_channels = average_channel_thresholding_abs(
                                                all_processed_coefficients_abs[averaging_start:averaging_stop])

        quantized_phase = average_channel_thresholding_phase(
                                                all_processed_coefficients_phase[averaging_start:averaging_stop],
                                                max_deviation_channels)

        a = np.array(quantized_abs)
        b = np.array(quantized_phase)
        quantized = a & b

        quantized_all.append(list(quantized))
        averaging_stop = averaging_stop + averaging_length
        averaging_start = averaging_start + averaging_length

    #print(channels_to_remove_from_each_averaging_interval)

    complete_quantized_sequence = list(itertools.chain.from_iterable(quantized_all))

    #bin_quantized_sequence = bin_quantization_2_bit(complete_quantized_sequence, codeword_length)

    random_check = check_randomness(complete_quantized_sequence)

    complete_quantized_sequence_str = "".join(map(str, complete_quantized_sequence))

    return random_check, complete_quantized_sequence_str

def bin_quantization_4_bit(complete_quantized_sequence, codeword_length):

    n_bit_parts = divide_into_parts(complete_quantized_sequence, codeword_length)

    bin_quantized_bits = []
    for sequence in n_bit_parts:
        value = int(''.join(map(str, sequence)), 2)
        if -1 < value <= 1:
            bit_string = "0000"
        elif 1 < value <= 4:
            bit_string = "0001"
        elif 4 < value <= 7:
            bit_string = "0010"
        elif 7 < value <= 11:
            bit_string = "0011"
        elif 11 < value <= 16:
            bit_string = "0100"
        elif 16 < value <= 22:
            bit_string = "0101"
        elif 22 < value <= 27:
            bit_string = "0110"
        elif 27 < value <= 31:
            bit_string = "0111"
        elif 31 < value <= 36:
            bit_string = "1000"
        elif 36 < value <= 41:
            bit_string = "1001"
        elif 41 < value <= 47:
            bit_string = "1010"
        elif 47 < value <= 52:
            bit_string = "1011"
        elif 52 < value <= 56:
            bit_string = "1100"
        elif 56 < value <= 59:
            bit_string = "1101"
        elif 59 < value <= 62:
            bit_string = "1110"
        elif 62 < value <= 64:
            bit_string = "1111"
        bin_quantized_bits.append(bit_string)

    complete_quantized_sequence = list(itertools.chain.from_iterable(bin_quantized_bits))

    return complete_quantized_sequence

    # [ 0.  1.  4.  7. 11. 16. 22. 27. 31. 36. 41. 47. 52. 56. 59. 62. 63.]

def bin_quantization_3_bit(complete_quantized_sequence, codeword_length):

    n_bit_parts = divide_into_parts(complete_quantized_sequence, codeword_length)

    bin_quantized_bits = []
    for sequence in n_bit_parts:
        value = int(''.join(map(str, sequence)), 2)
        if -1 < value <= 15:
            bit_string = "000"
        elif 15 < value <= 30:
            bit_string = "001"
        elif 30 < value <= 47:
            bit_string = "010"
        elif 47 < value <= 63:
            bit_string = "011"
        elif 63 < value <= 79:
            bit_string = "100"
        elif 79 < value <= 97:
            bit_string = "101"
        elif 97 < value <= 112:
            bit_string = "110"
        elif 112 < value <= 129:
            bit_string = "111"
        bin_quantized_bits.append(bit_string)

    complete_quantized_sequence = list(itertools.chain.from_iterable(bin_quantized_bits))

    return complete_quantized_sequence

    # [0.0, 15.0, 30.0, 47.0, 63.0, 79.0, 97.0, 112.0, 127.0]

def bin_quantization_2_bit(complete_quantized_sequence, codeword_length):

    n_bit_parts = divide_into_parts(complete_quantized_sequence, codeword_length)

    bin_quantized_bits = []
    for sequence in n_bit_parts:
        value = int(''.join(map(str, sequence)), 2)
        if -1 < value <= 11:
            bit_string = "00"
        elif 11 < value <= 31:
            bit_string = "01"
        elif 31 < value <= 52:
            bit_string = "10"
        elif 52 < value <= 63:
            bit_string = "11"
        bin_quantized_bits.append(bit_string)

    complete_quantized_sequence = list(itertools.chain.from_iterable(bin_quantized_bits))

    return complete_quantized_sequence

    # [ 0. 11. 31. 52. 63.]

def average_channel_thresholding_statistics(coefficients, multiple_regions, count):
    """
    Computes average for each channel from measurements uses that for threshold.
    Then uses a sliding window of length 10 to pick out the channels from the each measurement.
    If the largest absolute value in that sample is positive than it quantizes this measurement as a 1.
    Otherwise it quantizes it as a 0.
    :param coefficients: 2-D list
    :return: key: list
    """

    np_processed_coefficients = np.asarray(coefficients)
    channel_mean = np_processed_coefficients.mean(axis=0)
    channel_std = np_processed_coefficients.std(axis=0)

    key = []

    for i in range(len(coefficients)):
        if multiple_regions:
            selector = i % count
            if count == 3:
                offset = 11
            if count == 5:
                offset = 1
        else:
            selector = count
            offset = 11

        all_channel = np_processed_coefficients[i] - channel_mean - 0 * channel_std
        selected_channel = all_channel[selector * 10 + offset:(selector + 1) * 10 + offset]
        abs_max = selected_channel.flat[abs(selected_channel).argmax()]

        if np.sign(abs_max) == 1:
            key.append(1)
        else:
            key.append(0)

    return key

def quantize_measurements_statistics(raw_coefficients, quantized_bits_number, averaging_length,
                                     column_file, multiple_regions, number):

    """
    Generates qunatized bits from csv. Only read the file once. Averaging threshold absolute quantization.
    Varying regions. Returns list of averaging_length/2 bit sequences
    :return: list of list of 1s and 0s ints
    """

    averaging_intervals_count = quantized_bits_number // averaging_length

    processed_coefficients_list = []
    averaging_start = 0
    averaging_stop = averaging_start + averaging_length

    all_processed_coefficients = compute_absolutes(raw_coefficients)

    quantized_all = []

    for i in range(averaging_intervals_count):
        quantized = average_channel_thresholding_statistics(coefficients=processed_coefficients_list[averaging_start:averaging_stop],
                                                            multiple_regions=multiple_regions,
                                                            count=number)
        quantized_all.append(quantized)
        averaging_stop = averaging_stop + averaging_length
        averaging_start = averaging_start + averaging_length

    complete_quantized_sequence = list(itertools.chain.from_iterable(quantized_all))

    random_check = check_randomness(complete_quantized_sequence)

    return random_check, complete_quantized_sequence

def quantize_measurements_bin_stats(raw_coefficients, averaging_intervals_count, averaging_length, codeword_length):

    """
    Generates qunatized bits from csv. Only read the file once. Averaging threshold absolute quantization.
    Returns list of  bit sequences
    :return: list of list of 1s and 0s as single string
    """

    averaging_start = 100
    averaging_stop = averaging_start + averaging_length

    all_processed_coefficients_abs = compute_absolutes(raw_coefficients)
    all_processed_coefficients_phase = compute_phase(raw_coefficients)

    quantized_all = []

    for i in range(averaging_intervals_count):
        quantized_abs = average_channel_thresholding(all_processed_coefficients_abs[averaging_start:averaging_stop])
        quantized_phase = average_channel_thresholding(all_processed_coefficients_phase[averaging_start:averaging_stop])

        a = np.array(quantized_abs)
        b = np.array(quantized_phase)
        quantized = a & b

        quantized_all.append(list(quantized))
        averaging_stop = averaging_stop + averaging_length
        averaging_start = averaging_start + averaging_length

    complete_quantized_sequence = list(itertools.chain.from_iterable(quantized_all))

    #bin_quantized_sequence = bin_quantization_3_bit(complete_quantized_sequence, codeword_length)

    #quantized_bit_number = len(bin_quantized_sequence)

    quantized_bit_number = len(complete_quantized_sequence)

    bin_quantized_sequence_parts = divide_into_parts(complete_quantized_sequence, codeword_length)

    random_check = check_randomness(complete_quantized_sequence)

    return random_check, bin_quantized_sequence_parts, quantized_bit_number

def high_low_threshold(coefficients):

    ration = 0.5

    np_processed_coefficients = np.asarray(coefficients)

    channel_means = np_processed_coefficients.mean(axis=0)
    channel_stds = np_processed_coefficients.std(axis=0)

    high_threshold = list(channel_means + ration * channel_stds)
    low_threshold = list(channel_means - ration * channel_stds)

    channels_to_remove_from_each_line = []

    for i in range(len(coefficients)):
        channels_to_remove = []
        for j in range(len(coefficients[i])):
            if low_threshold[j] < coefficients[i][j] < high_threshold[j]:
                channels_to_remove.append(j)

        channels_to_remove_from_each_line.append(channels_to_remove)

    print(channels_to_remove_from_each_line)

    return channels_to_remove_from_each_line

def quantization_without_mediocre_channels(coefficients, channels_to_remove_from_each_averaging_region):

    channels_to_remove_from_each_averaging_region = [[0, 4, 9, 11, 14, 15, 18, 23, 24, 26, 29, 30, 33, 34, 35, 36, 38, 39, 40, 42, 43, 44, 46, 47, 49, 50], [6, 7, 13, 19, 20, 21, 23, 24, 25, 27, 28, 31, 32, 37, 39, 41, 43, 46, 47, 51], [1, 2, 3, 7, 8, 10, 11, 12, 13, 14, 15, 19, 23, 25, 27, 28, 30, 33, 34, 36, 37, 38, 42, 43, 44, 45, 46, 50, 51], [2, 4, 7, 9, 12, 23, 29, 31, 34, 35, 36, 38, 39, 40, 42, 43, 47, 48, 50], [1, 2, 3, 4, 5, 7, 8, 11, 14, 15, 16, 17, 18, 19, 21, 22, 27, 31, 33, 34, 35, 36, 37, 39, 40, 41, 44, 48, 49, 50, 51]]

    ration = 0.5

    np_processed_coefficients = np.asarray(coefficients)

    channel_means = np_processed_coefficients.mean(axis=0)
    channel_stds = np_processed_coefficients.std(axis=0)

    high_threshold = list(channel_means + ration * channel_stds)
    low_threshold = list(channel_means - ration * channel_stds)

    # TO BE CONTINUED

    key = []

    for i in range(len(coefficients)):
        all_channels = np_processed_coefficients[i] - channel_mean - 0 * channel_std
        #for selector in range(3):
        selector = i % 3
        #selected_channels = all_channels[selector * 10 + 11:(selector + 1) * 10 + 11]
        selected_channels = all_channels[24+selector]
        abs_max = selected_channels.flat[abs(selected_channels).argmax()]

        if np.sign(abs_max) == 1:
            key.append(1)
        else:
            key.append(0)

    return key


#!/usr/bin/python3
"""
Library for Raspberry Pi interfacing with the 16-Bit I/O Expander MCP23017
from Microchip Technology.
"""

__all__ = ["MCP23017", "LCD20x4"]

import time
import smbus
from collections import Iterable

class MCP23017:
    """
    Class for the MCP23017 I/O port expander.
    """
    REG_BASE_ADDR = {'IODIR': 0x00, 'IPOL': 0x02, 'GPINTEN': 0x04, 'DEFVAL': 0x06, 'INTCON': 0x08, 'IOCON': 0x0A,
                        'GPPU': 0x0C, 'INTF': 0x0E, 'INTCAP': 0x10, 'GPIO':0x12, 'OLAT': 0x14}

    OUT = 0
    IN = 1
    LOW = 0
    HIGH = 1
    PUD_DOWN = 0
    PUD_UP = 1

    class RegisterValueError(ValueError):
        def __init__(self, value=''):
            self._message = value
        @property
        def message(self):
            return self._message

        @message.setter
        def message(self, value):
            self._message = value

        def __str__(self):
            return self.message

    def __init__(self, bus, address):
        self._bus = smbus.SMBus(bus)
        self.address = address
        self._register_values = dict()

        # Leave ICON.BANK = 0 but set ICON.SEQOP = 1
        self._bus.write_byte_data(self.address, MCP23017.REG_BASE_ADDR['IOCON'], 1 << 5)
        # Read all register values
        for key in MCP23017.REG_BASE_ADDR.keys():
            self._register_values[key] = self._bus.read_word_data(self.address, MCP23017.REG_BASE_ADDR[key])

    def close(self):
        self._bus.close()

    def __enter__(self):
        return self

    def __exit__(self, type_, value, traceback):
        self.close()

    def __str__(self):
        return '<Device class {0} with address {1}>'.format(self.__class__, self.address)

    def setmode(self, *pargs, **kwargs):
        """
        Method for compatibility with RPi.GPIO interface only.
        """
        pass

    def setwarnings(self, value):
        """
        Method for compatibility with RPi.GPIO interface only.
        """
        pass

    @staticmethod
    def set_bit_in_word(word, bit, value):
        """
        Static helper method to set a bit in a word.
        """
        if 0 <= bit <= 15 and value in (0, 1):
            if value:
                word |= (1 << bit)
            else:
                word &= ~(1 << bit)
            return word
        else:
            raise MCP23017.RegisterValueError

    def _write_register_word(self, key, bits, values):
        """
        Helper method to manipulate the instance register values.
        Works for both for a single bit as well as for a list of bits.
        """
        word = self._register_values[key]

        if not isinstance(bits, Iterable):
            word = MCP23017.set_bit_in_word(word, bits, values)
        else:
            number_of_bits = len(bits)
            if not isinstance(values, Iterable):
                # if value is a single value we have to create a list a corresponding value list.
                values = [values] * number_of_bits
            else:
                if len(values) != number_of_bits:
                    raise MCP23017.RegisterValueError

            for bit, value in zip(bits, values):
                word = MCP23017.set_bit_in_word(word, bit, value)

        # check if we have a the word differs to the previous one
        high_byte_diff, low_byte_diff = divmod(word ^ self._register_values[key], 256)

        if high_byte_diff and low_byte_diff:
            # both bytes changed so send the complete word
            self._bus.write_word_data(self.address, MCP23017.REG_BASE_ADDR[key], word)
        elif high_byte_diff ^ low_byte_diff:
            # only one byte differs so we have to send only this one
            value_msb, value_lsb = divmod(word, 256)
            offset, value = (0, value_lsb) if low_byte_diff else (1, value_msb)
            self._bus.write_byte_data(self.address, MCP23017.REG_BASE_ADDR[key] + offset, value)
        else:
            # no change so we do not need to set the hardware register value
            pass
        self._register_values[key] = word

    def setup(self, pins, directions, initial=None, pull_up_down=None):
        """
        Configures a pin or a list of pins either as output or as input.
        """
        try:
            self._write_register_word('IODIR', pins, directions)
            if pull_up_down is not None:
                raise NotImplementedError
            if initial is not None:
                raise NotImplementedError
        except MCP23017.RegisterValueError as e:
            e.message = 'Invalid pin(s) or direction value(s)...'
            raise

    def output(self, pins, states):
        """
        Sets the logic output level of the pin or a list of pins.
        Valid values for the output state are MCP23017.LOW / 0 / False or MCP23017.HIGH / 1 / True.
        """
        try:
            self._write_register_word('GPIO', pins, states)
        except MCP23017.RegisterValueError as e:
            e.message = 'Invalid pin(s) or state value(s)...'
            raise

    def input(self, pin):
        """
        Reads the logic level of the pin.
        """
        self._register_values['GPIO'] = self._bus.read_word_data(self.address, MCP23017.REG_BASE_ADDR['GPIO'])
        return MCP23017.HIGH if ((self._register_values['GPIO'] >> pin)) & 1 else MCP23017.LOW

    def gpio_function(self, pin):
        """
        Returns whether the pin is configured as input or as output.
        """
        self._register_values['IODIR'] = self._bus.read_word_data(self.address, MCP23017.REG_BASE_ADDR['IODIR'])
        return MCP23017.IN if ((self._register_values['IODIR'] >> pin)) & 1 else MCP23017.OUT

    def cleanup(self):
        pass

class LCD20x4:
    """
    Simple class for a character lcd connected via the MCP23017 I/O Expander.
    """

    LINE_OFFSETS = [0x00, 0x40, 0x14, 0x54]
    LINE_WIDTH = 20
    LINE_NUMBER = 4

    CMD_CLEAR_DISPLAY = 0x01
    CMD_RETURN_HOME = 0x02

    CMD_SET_ENTRY_MODE = 0x04
    ENTRY_LEFT = 0x02

    CMD_DISPLAY_CONTROL = 0x08
    CURSOR_ON = 0x02
    CURSOR_OFF = 0x00
    DISPLAY_ON = 0x04
    DISPLAY_OFF = 0x00

    # Flags for the function set instruction
    CMD_FUNCTION_SET = 0x20
    EIGHT_BIT_MODE = 0x10
    FOUR_BIT_MODE = 0x00
    ONE_LINE = 0x00
    TWO_LINES = 0x08

    CMD_SET_DDRAM_ADDRESS = 0x80

    def __init__(self, interface, rs, en, data, rw=None):
        self._pins = {}
        self._interface = interface
        self._pins['RS'] = rs
        self._pins['E'] = en
        self._bit_mode = len(data)
        if self._bit_mode not in (4, 8):
            raise ValueError('Invalid number of data pins...')
        self._pins['DATA']= data
        self._pins['RW'] = rw

        # Setup all pins as outputs
        for pin in (rs, en, *data):
            self._interface.setup(pin, MCP23017.OUT)
            self._interface.output(pin, MCP23017.LOW)
        if rw is not None:
            self._interface.setup(rw, MCP23017.OUT)
            # for now we use the pin only for write actions!
            self._interface.output(rw, MCP23017.LOW)

        function_set = LCD20x4.CMD_FUNCTION_SET |\
                                   (LCD20x4.FOUR_BIT_MODE if self._bit_mode == 4 else LCD20x4.EIGHT_BIT_MODE) |\
                                   LCD20x4.TWO_LINES
        entry_mode = LCD20x4.CMD_SET_ENTRY_MODE | LCD20x4.ENTRY_LEFT

        if self._bit_mode == 4:
            self._write(0x33, delay_ms = 4.1)
            self._write(0x32, delay_ms = 0.1)
        else:
            for _ in range(3):
                self._write(function_set, delay_ms = 4.1)
        self._write(function_set)
        self._write(entry_mode)

        self.display_off()
        self.clear_display()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()

    def close(self):
        self._interface.cleanup()

    def _write(self, data, char_mode=False, delay_ms=0):
        """
        Writes the 8-bit value either in character or in command mode.
        """
        self._interface.output([self._pins['RS'], self._pins['E']], [char_mode, MCP23017.LOW])
        data = divmod(data, 16) if self._bit_mode == 4 else (data,)
        for number in data:
            output = []
            i = 0
            while i < self._bit_mode:
                output.append(MCP23017.HIGH if ((number >> i) & 1) else MCP23017.LOW)
                i += 1
            self._interface.output(self._pins['DATA'], output)
            self._interface.output(self._pins['E'], MCP23017.HIGH)
            self._interface.output(self._pins['E'], MCP23017.LOW)
            if delay_ms > 0: time.sleep(delay_ms / 1000.0)

    def clear_display(self):
        """
        Clears the character display.
        """
        self._write(LCD20x4.CMD_CLEAR_DISPLAY)
        self.set_cursor_position(0, 0)

    def display_off(self):
        self._write(LCD20x4.CMD_DISPLAY_CONTROL | LCD20x4.DISPLAY_OFF, delay_ms=0.037)

    def display_on(self):
        self._write(LCD20x4.CMD_DISPLAY_CONTROL | LCD20x4.DISPLAY_ON, delay_ms=0.037)

    def set_cursor_position(self, line, column):
        """
        Sets the cursor to the desired position defined by the line and column number.
        """
        if 0 <= line < LCD20x4.LINE_NUMBER and 0 <= column < LCD20x4.LINE_WIDTH:
            self._write((LCD20x4.CMD_SET_DDRAM_ADDRESS | (LCD20x4.LINE_OFFSETS[line]) + column), delay_ms=0.037)
        else:
            raise ValueError('Invalid line or column position')

    def write_line(self, line, column, text):
        """
        Writes a text to the specified line and starting at the specified column.
        """
        self.set_cursor_position(line, column)
        for char in text[:(LCD20x4.LINE_WIDTH - column)]:
            self.write_char(char)

    def clear_line(self, line, column=0):
        """
        Clears the line beginning from column.
        """
        if 0 <= line < LCD20x4.LINE_NUMBER and 0 <= column < LCD20x4.LINE_WIDTH:
            self.write_line(line, column, ' ' * (LCD20x4.LINE_WIDTH - column))

    def write_char(self, char):
        """
        Writes a character to the current cursor position.
        """
        self._write(ord(char), True)


#!/usr/bin/python
"""
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]

leetcode #15
REDDO defintily
"""

def threeSum(arr, targ):
    arr.sort()
    rslt = []

    # len(arr)-2 no need to look at last two element,
    # because you are looking for three sumes. won't have
    # enough elements for you
    for i in xrange(len(arr)-2):
        if i == 0 or (i >0 and arr[i] != arr[i-1]):
            rem = targ - arr[i]
            beg = i+1
            end = len(arr)-1
            # find two sums of the remaining.
            while beg < end:
                s = arr[beg] + arr[end]
                if s == rem:
                    rslt.append([arr[i], arr[beg], arr[end]])
                    while beg < end and (arr[beg] == arr[beg+1]):
                        beg += 1
                    while beg < end and (arr[end] == arr[end-1]):
                        end -= 1
                    beg += 1
                    end -= 1
                elif s < rem:
                    beg += 1
                else:
                    end -= 1
    return rslt

def threeSum_r(nums):
    nums.sort()
    res = []
    c = 0
    while c < len(nums)-2:
        imm_t = -nums[c]
        l = c + 1
        h = len(nums)-1
        while l < h:
            if nums[l] + nums[h] == imm_t:
                res.append([nums[c], nums[l], nums[h]])
                l += 1
                h -= 1
                while l < h and nums[l-1] == nums[l]:
                    l += 1
                while l < h and nums[h+1] == nums[h]:
                    h -= 1

            elif nums[l] + nums[h] < imm_t:
                l += 1
            else:
                h -= 1

        c += 1
        while c < len(nums) and nums[c-1] == nums[c]:
            c += 1

    return res

def test1():
    arr = [-1, 0, 1, 2, -1, -4]
    print(threeSum(arr, 0))
    arr = [-1, 0, 1, 2, -1, -4]
    print(threeSum_r(arr))
    print('----------------')

def test2():
    arr = [-1, -1, -1, -1, -1]
    print(threeSum(arr, 0))
    arr = [-1, -1, -1, -1, -1]
    print(threeSum_r(arr))
    print('----------------')

def test3():
    arr = [-1, 0, 1, 2, 1, 2, -1, -1]
    print(threeSum(arr, 0))
    arr = [-1, 0, 1, 2, 1, 2, -1, -1]
    print(threeSum_r(arr))
    print('----------------')

def test4():
    arr = [1, -1, -1, 0]
    print(threeSum(arr, 0))
    arr = [1, -1, -1, 0]
    print(threeSum_r(arr))
    print('----------------')

def test5():
    arr = [-2, 0, 0, 2, 2]
    print(threeSum(arr, 0))
    arr = [-2, 0, 0, 2, 2]
    print(threeSum_r(arr))
    print('----------------')

def test6():
    arr = [-2, 0, 1, 1, 2]
    print(threeSum(arr, 0))
    arr = [-2, 0, 1, 1, 2]
    print(threeSum_r(arr))
    print('----------------')

def test7():
    arr = [3,0,-2,-1,1,2]
    print(threeSum(arr, 0))
    arr = [3,0,-2,-1,1,2]
    print(threeSum_r(arr))
    print('----------------')

if __name__ == '__main__':
    test1()
    test2()
    test3()
    test4()
    test5()
    test6()
    test7()


#!/usr/bin/env python3
import collections
import intervaltree
import logging
import math
import operator
import re
import time

_COMPLEMENTS = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N': 'N'}
_MAXIT = 100
_FPMIN = 1e-30
_EPS = 3e-7
_MpileupField = collections.namedtuple('MpileupField', ['chrom', 'pos', 'ref', 'alt_depth', 'depth', 'alt_counts'])

def _complement(seq):
    return ''.join([_COMPLEMENTS[base] for base in seq])

def _normalise_triplet(triplet):
    # Normalises the triplet of bases in triplet (supplied as a 3-member list)
    # by reverse complementing if necessary, to ensure that the middle element
    # is in {C, T, N}.  Returns the normalised triplet as well as a flag to
    # indicate whether normalisation took place.
    if triplet[1] == 'C' or triplet[1] == 'T' or triplet[1] == 'N':
        return triplet, False
    return _complement(triplet)[::-1], True

def _tally_sequence_string(seq_string, bq_string, mq_string, min_bq, min_mq):
    # Format of seq_string, from the samtools mpileup docs:
    # "a dot stands for a match to the reference base on the forward strand, a comma for
    # a match on the reverse strand, a '>' or '<' for a reference skip, `ACGTN' for a
    # mismatch on the forward strand and `acgtn' for a mismatch on the reverse strand.
    # A pattern `\\+[0-9]+[ACGTNacgtn]+' indicates there is an insertion between this
    # reference position and the next reference position. The length of the insertion
    # is given by the integer in the pattern, followed by the inserted sequence.
    # Similarly, a pattern `-[0-9]+[ACGTNacgtn]+' represents a deletion from the
    # reference. The deleted bases will be presented as `*' in the following lines.
    # Also at the read base column, a symbol `^' marks the start of a read. The ASCII
    # of the character following `^' minus 33 gives the mapping quality. A symbol `$'
    # marks the end of a read segment."
    string_uppercase = seq_string.strip().upper()
    i = 0
    j = 0
    ref_count = 0
    nonref_counter = collections.Counter()

    while i < len(string_uppercase):
        if string_uppercase[i] == 'A' or string_uppercase[i] == 'C' or string_uppercase[i] == 'G' or string_uppercase[i] == 'T':
            if ord(bq_string[j]) - 33 >= min_bq and ord(mq_string[j]) - 33 >= min_mq:
                nonref_counter.update(string_uppercase[i])
            j += 1
        elif string_uppercase[i] == ',' or string_uppercase[i] == '.':
            if ord(bq_string[j]) - 33 >= min_bq and ord(mq_string[j]) - 33 >= min_mq:
                ref_count += 1
            j += 1
        elif string_uppercase[i] == '$': # End of a read
            pass
        elif string_uppercase[i] == 'N': # Unknown base
            j += 1
            pass
        elif string_uppercase[i] == '*': # Placeholder for deletion described at a previous locus
            pass
        elif string_uppercase[i] == '^': # Start of a read
            i += 1                       # Skip the subsequent mapping quality character
        elif string_uppercase[i] == '+' or string_uppercase[i] == '-':  # Indel
            indel_length_field = re.match('[0-9]+', string_uppercase[i+1:]).group()
            indel_length = int(indel_length_field)
            i += len(indel_length_field) + indel_length
        else:
            warnings.warn('Unexpected symbol {} in read string: {}'.format(read_string[i], read_string))
        i += 1
    return ref_count, sum(nonref_counter.values()), nonref_counter

def _parse_mpileup_line(line, min_bq, min_mq):
    # samtools mpileup -C 50 -a -E -I -q 30 -Q 30 -s -f <ref.fa> <data.bam/cram>
    chrom, pos_string, ref, _, seq_string, bq_string, mq_string = line.rstrip().split('\t')

    pos = int(pos_string)
    ref_depth, alt_depth, alt_counts = _tally_sequence_string(seq_string, bq_string, mq_string, min_bq, min_mq)
    depth = ref_depth + alt_depth

    return _MpileupField(chrom, pos, ref, alt_depth, depth, alt_counts)

def _most_common_alt(alt_counts):
    """
    Returns the most common alt allele in the collections.Counter object
    alt_counts.  In the event of a tie for most common, breaks ties by
    base alphabetical order.

    >>> _most_common_alt(collections.Counter({'A': 10}))
    ('A', 10)
    >>> _most_common_alt(collections.Counter({'A': 10, 'C': 5}))
    ('A', 10)
    >>> _most_common_alt(collections.Counter({'A': 10, 'C': 20}))
    ('C', 20)
    >>> _most_common_alt(collections.Counter({'A': 10, 'C': 10}))
    ('A', 10)
    >>> _most_common_alt(collections.Counter({'T': 10, 'C': 10}))
    ('C', 10)
    >>> _most_common_alt(collections.Counter({'T': 10, 'C': 10, 'A': 20}))
    ('A', 20)
    """
    most_common_pairs = alt_counts.most_common()
    most_common_count = most_common_pairs[0][1]
    most_common_alts = [base for base, count in most_common_pairs if count == most_common_count]
    return (sorted(most_common_alts)[0], most_common_count)

def _betacf(a, b, x):
    """
    >>> _betacf(1, 2, 0.1)
    1.1728395061728394
    >>> _betacf(0, 2, 0.1)
    1.2345679012345678
    >>> _betacf(3, 5, 0.9)
    3918.5714285777376
    """
    # Accessory function for incomplete Beta function from NRC 6.4
    qab=a+b
    qap=a+1.0
    qam=a-1.0
    c=1.0
    d=1.0-qab*x/qap
    if abs(d) < _FPMIN:
        d=_FPMIN
    d=1.0/d
    h=d
    for m in range(1, _MAXIT+1):
        m2=2.0*m
        aa=m*(b-m)*x/((qam+m2)*(a+m2))
        d=1.0+aa*d
        if math.fabs(d) < _FPMIN:
            d=_FPMIN
        c=1.0+aa/c
        if math.fabs(c) < _FPMIN:
            c=_FPMIN
        d=1.0/d
        h = h*d*c
        aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2))
        d=1.0+aa*d
        if math.fabs(d) < _FPMIN:
            d=_FPMIN
        c=1.0+aa/c
        if math.fabs(c) < _FPMIN:
            c=_FPMIN
        d=1.0/d
        d_el=d*c
        h = h*d_el
        if math.fabs(d_el-1.0) < _EPS:
            break

    if m > _MAXIT:
        raise ValueError("a or b too big, or MAXIT too small in betacf")

    return h

def _betai(a, b, x):
    # Incomplete Beta function from NRC 6.4
    assert x >= 0.0 and x <= 1.0

    if x == 0.0 or x == 1.0:
        bt = 0.0
    else:
        bt = math.exp(math.lgamma(a+b)-math.lgamma(a)-math.lgamma(b)+a*math.log(x)+b*math.log(1.0-x))

    if x < (a+1.0)/(a+b+2.0):
        return bt*_betacf(a,b,x)/a
    else:
        return 1.0-bt*_betacf(b,a,1.0-x)/b

def _pbinom(q, size, prob, lower_tail=True):
    """
    >>> _pbinom(2, 4, 0.5)
    0.6875000000000001
    >>> _pbinom(2, 10, 0.5)
    0.05468749999999989
    >>> _pbinom(0, 10, 0.9)
    1.000000082740371e-10
    >>> _pbinom(10, 10, 0.9)
    1.0
    >>> _pbinom(0, 10, 0.1)
    0.3486784400015138
    >>> _pbinom(3, 2, 0.5)
    Traceback (most recent call last):
      File "/apps/python3/3.4.3/lib/python3.4/doctest.py", line 1318, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__._pbinom[5]>", line 1, in <module>
        _pbinom(3, 2, 0.5)
      File "soma-snv.py", line 197, in _pbinom
        assert q >= 0 and q <= size
    AssertionError
    """
    assert prob >= 0.0 and prob <= 1.0
    assert size > 0
    assert q >= 0 and q <= size

    if q == size:
        upper_p = 0.0
    else:
        upper_p = _betai(q+1, size-q, prob)

    if lower_tail:
        return 1.0 - upper_p
    else:
        return upper_p

class Blacklist:
    def __init__(self):
        self.trees = {}

    def load_bed(self, instream):
        self.trees = {}
        for line in instream:
            chrom, start0, end1 = line.split('\t', 3)[:3]
            if chrom not in self.trees:
                self.trees[chrom] = intervaltree.IntervalTree()
            self.trees[chrom].add(intervaltree.Interval(int(start0), int(end1)))

    def intersects(self, chrom, pos):
        if chrom not in self.trees:
            return False

        return len(self.trees[chrom][pos]) > 0

class LssDetector:
    TestResult = collections.namedtuple('TestResult', ['was_powered', 'was_detection', 'critval_E', 'critval_H', 'sensitivity', 'snr'])

    def __init__(self, blacklist = Blacklist(), rate_het=1.0e-3, rate_sc=5.0e-7, vaf=0.1, min_snr=10.0, epsilon=1.0e-2, min_bq=30, min_mq=30, max_dp=100):
        self.blacklist = blacklist
        self.rate_het = rate_het
        self.rate_sc = rate_sc
        self.epsilon = epsilon
        self.vaf = vaf
        self.min_snr = min_snr
        self.min_mq = min_mq
        self.min_bq = min_bq
        self.max_dp = max_dp
        threshold_cache_array_size = 101
        if max_dp + 1 < threshold_cache_array_size:
            threshold_cache_array_size = max_dp + 1
        self.threshold_cache_dense = [None] * threshold_cache_array_size
        self.threshold_cache_sparse = {}
#        self._precompute_dense_thresholds()

    def _precompute_dense_thresholds(self):
        for depth in range(len(self.threshold_cache_dense)):
            self.threshold_cache_dense[depth] = self._calc_optimal_thresholds(depth)

    def _get_optimal_thresholds(self, depth):
        if depth < len(self.threshold_cache_dense):
            if self.threshold_cache_dense[depth] == None:
                self.threshold_cache_dense[depth] = self._calc_optimal_thresholds(depth)
            return self.threshold_cache_dense[depth]

        if depth not in self.threshold_cache_sparse:
            self.threshold_cache_sparse[depth] = self._calc_optimal_thresholds(depth)
        return self.threshold_cache_sparse[depth]

    @staticmethod
    def _calc_pn_pr(ce, ch, depth, epsilon, rc, rh, rr, pa):
        ae = _pbinom(ce, depth, epsilon, lower_tail=False)
        ah = _pbinom(ch + 1, depth, 0.5+epsilon/3.0, lower_tail=True)
        pn = rr*(_pbinom(ch, depth, pa) - _pbinom(ce - 1, depth, pa))
        pr = (rc/(1-rc)) * (pn / (rr*ae + rh*ah))
        return (pn, pr)

    def _calc_optimal_thresholds(self, depth):
        """
        Find ce and ch that yield optimal pn, subject to:
          1 <= ce <= ch <= n-1, and
          pr >= gr.
        Use an exhaustive search over the grid of valid ce, ch pairs.

        >>> cl = LssDetector(rate_het=1.0e-3, rate_sc=5.0e-7, vaf=0.1, min_snr=10.0, epsilon=1.0e-2)
        >>> cl._calc_optimal_thresholds(40)
        (None, None, 0, None)
        >>> cl._calc_optimal_thresholds(100)
        (10, 28, 0.6601016080599246, 18.0580053085918)
        >>> cl._calc_optimal_thresholds(135)
        (11, 43, 0.8803333325220946, 11.584716396883863)
        """
        rr = 1.0-self.rate_het-self.rate_het*self.rate_het
        pa = (1-4/3*self.epsilon)*self.vaf + self.epsilon
        rc = self.rate_sc
        rh = self.rate_het

        # 1. Identify a starting point for the optimization that is
        #    within the feasible region defined by pr >= self.min_snr.
        #    We search along the diagonal ce = ch.
        for ch in range(1, depth):
            pn, pr = self._calc_pn_pr(ch, ch, depth, self.epsilon, rc, rh, rr, pa)
            if pr >= self.min_snr:
                break
        else:
            # No feasible region
            return (None, None, 0, None)

        # 2. Starting from the feasible point above, optimise pn by
        #    coordinate ascent.
        best_ce, best_ch, best_pr, best_pn = ch, ch, pr, pn

        while True:
            for ce, ch in ((best_ce-1,best_ch), (best_ce,best_ch+1), (best_ce-1,best_ch+1)):
                if ce <= 0 or ch <= 0 or ce >= depth or ch >= depth:
                    continue
                pn, pr = self._calc_pn_pr(ce, ch, depth, self.epsilon, rc, rh, rr, pa)
                if pr >= self.min_snr and pn > best_pn:
                    best_ce, best_ch, best_pr, best_pn = ce, ch, pr, pn
                    break
            else:
                break

        return (best_ce, best_ch, best_pn, best_pr)

    def _test_for_lsv(self, depth_alt, depth):
        if depth == 0:
            return self.TestResult(False, None, None, None, None, None)
        cE, cH, sens, snr = self._get_optimal_thresholds(depth)
        if cE == None:
            return self.TestResult(False, None, None, None, None, None)     # Cannot achieve target SNR; don't proceed with test.

        # This locus passes test S1 if depth_alt >= cE, and test S2 if depth_alt <= cH.
        # See LSS-5 for test definitions.
        return self.TestResult(True, depth_alt >= cE and depth_alt <= cH, cE, cH, sens, snr)

    @staticmethod
    def _emit_detection_line(fields, result, ref_triplet, outstream):
        # Using multiple calls to outstream.write because I believe that's fastest,
        # but I've not tested this.  Alternatives would be %/format/f-strings, or
        # simple concatenation.  TODO.

        # Preparations: what was the alt allele, is normalisation needed?
        most_common_alt, most_common_alt_dp = _most_common_alt(fields.alt_counts)
        norm_ref, norm_applied = _normalise_triplet(ref_triplet)
        if norm_applied:
            most_common_alt = _complement(most_common_alt)

        # Format: CHROM     POS     REF     ALT     Strand  DP      RD      AD      sensitivity     snr
        # tab-separated
        outstream.write(fields.chrom)
        outstream.write('\t')
        outstream.write(str(fields.pos))
        outstream.write('\t')
        outstream.write(norm_ref[0])
        outstream.write(norm_ref[1])
        outstream.write(norm_ref[2])
        outstream.write('\t')
        outstream.write(most_common_alt)
        outstream.write('\t')
        outstream.write('-1' if norm_applied else '+1')
        outstream.write('\t')
        outstream.write(str(fields.depth))
        outstream.write('\t')
        outstream.write(str(fields.depth - fields.alt_depth))
        outstream.write('\t')
        outstream.write(str(most_common_alt_dp))
        outstream.write('\t')
        outstream.write(str(result.sensitivity))
        outstream.write('\t')
        outstream.write(str(result.snr))
        outstream.write('\n')

    @staticmethod
    def _emit_sensitivity_tally(sens_tally, outstream):
        # FORMAT: REF    sens
        for base1 in ('A', 'C', 'G', 'T'):
            for base2 in ('C', 'T'):
                for base3 in ('A', 'C', 'G', 'T'):
                    outstream.write('{}{}{}\t{}\n'.format(base1, base2, base3, sens_tally[base1 + base2 + base3]))

    def process_mpileup_stream(self, instream, outstream_vars, outstream_sens):
        # Stream the mpileup output and emit variants as they are detected.
        # In parallel, keep a running tally of sensitivity for each ref
        # triplet at non-blacklisted loci.
        #
        # There is complexity in this in that the ref triplet for a line
        # contains the *next* base, which has not yet been processed.
        # Therefore variant writing lags behind reading by one base.
        #
        # The use of a blacklist also adds logic -- loci in the blacklist
        # should not have any variants emitted nor contribute to the
        # sensitivity tally, but should be used in the ref triplets.

        sensitivity_tally = collections.Counter()

        ref_triplet = ['N'] * 3
        last_fields = None
        last_result = self.TestResult(False, False, None, None, None, None)

        line_count = 0
        start_time = time.clock()
        last_print_time = start_time
        last_print_count = line_count
        print_interval = 1000000
        parsing_time = 0
        test_time = 0
        blacklist_time = 0

        for line in instream:
            line_count += 1
            if line_count % print_interval == 0:
                this_print_time = time.clock()
                delta_print_time = this_print_time - last_print_time
                delta_print_count = line_count - last_print_count
                # TotalLoci  TotalTime  Rate  ParseTotal  TestTotal  BlacklistTotal  LociSinceLastStatus  TimeSinceLastStatus  RateSinceLastStatus
                logging.info('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(
                    line_count, this_print_time - start_time, line_count / (this_print_time - start_time),
                    parsing_time, test_time, blacklist_time,
                    delta_print_count, delta_print_time, delta_print_count / delta_print_time))
                last_print_time = this_print_time
                last_print_count = line_count

            temp = time.clock()
            fields = _parse_mpileup_line(line, self.min_bq, self.min_mq)    # type(fields) = MpileupField (chrom, pos, ref, alt_depth, depth, alt_counts)
            parsing_time += (time.clock() - temp)

            # Update the tally of the reference triplet, centred at the previous base
            del ref_triplet[0]
            ref_triplet.append(fields.ref)

            # Emit the previous line if a variant was found
            if last_result.was_detection == True:
                self._emit_detection_line(last_fields, last_result, ref_triplet, outstream_vars)
                # Clear last_result to ensure no tripping on future iterations.
                # This is necessary as the last_x = x code below is skipped for
                # unpowered and blacklisted sites, so last_x is not always updated.
                last_result = self.TestResult(False, False, None, None, None, None)

            # Skip further processing for high depth sites
            if fields.depth > self.max_dp:
                continue

            # Test for LSV
            temp = time.clock()
            result = self._test_for_lsv(fields.alt_depth, fields.depth)     # type(result) = self.TestResult (was_powered, was_detection, critval_E, critval_H, sensitivity, snr)
            test_time += (time.clock() - temp)

            # Skip futher processing for unpowered sites
            if result.was_powered == False:
                continue

            # Skip further processing for blacklisted sites
            # Note that this check is after the LSV test, although it could be
            # placed before.  The reason for the current placement is that the
            # interval intersection code is very slow at the moment, and it's
            # actually faster to calculate LSV first and check overlap later.
            temp = time.clock()
            blacklist_intersection = self.blacklist.intersects(fields.chrom, fields.pos - 1)
            blacklist_time += (time.clock() - temp)
            if blacklist_intersection:
                continue

            # Update the sensitivity tally for the current line.
            sensitivity_tally[''.join(_normalise_triplet(ref_triplet)[0])] += result.sensitivity

            # Copy results to buffer for possible emission in the next iteration
            last_fields = fields
            last_result = result

        # Tail processing of the last variant
        if last_result.was_detection == True:
            self._emit_detection_line(last_fields, last_result, ref_triplet, outstream_vars)

        # Emit the sensitivities
        if outstream_sens != None:
            self._emit_sensitivity_tally(sensitivity_tally, outstream_sens)

if __name__ == '__main__':
    import argparse
    import doctest
    import sys

    doctest.testmod()

    parser = argparse.ArgumentParser(description='Find subclonal SNVs.')

    parser.add_argument('--mpileup', help='Input mpileup result (- for stdin) [-]', type=str, default='-')
    parser.add_argument('--blacklist', help='BED of blacklist regions [not used]', type=open)
    parser.add_argument('--variants', help='Destination for detected variants (- for stdout) [-]', type=str, default='-')
    parser.add_argument('--background', help='Destination for background coverage tally [not written]', type=str)
    parser.add_argument('--snr', help='Target minimum SNR for detections [10.0]', type=float, default=10.0)
    parser.add_argument('--vaf', help='Target subclonal variant AF [0.1]', type=float, default=0.1)
    parser.add_argument('--error', help='Per-read error rate [1e-2]', type=float, default=1.0e-2)
    parser.add_argument('--bq', help='Minimum base quality threshold (Phred-scale) [30]', type=int, default=30)
    parser.add_argument('--mq', help='Minimum mapping quality threshold (Phred-scale) [30]', type=int, default=30)
    parser.add_argument('--het', help='Heterozygous locus rate [1e-3]', type=float, default=1.0e-3)
    parser.add_argument('--scv', help='Subclonal variation rate [5e-7]', type=float, default=5.0e-7)
    parser.add_argument('--maxdp', help="Ignore loci with depth exceeding this value [100]", type = int, default=100)

    args = parser.parse_args()

    if args.mpileup == '-':
        instream = sys.stdin
    else:
        instream = open(args.mpileup, 'rt')

    blacklist = Blacklist()
    if args.blacklist:
        blacklist.load_bed(args.blacklist)

    if args.variants == '-':
        outstream = sys.stdout
    else:
        outstream = open(args.variants, 'wt')

    if args.background:
        background = open(args.background, 'wt')
    else:
        background = None

    logging.basicConfig(format='%(asctime)s\t%(relativeCreated)d\t%(levelname)s\t%(message)s', level=logging.INFO)

    detector = LssDetector(blacklist, args.het, args.scv, args.vaf, args.snr, args.error, args.bq, args.mq, args.maxdp)
    detector.process_mpileup_stream(instream, outstream, background)

    logging.shutdown()


#!/usr/bin/env python

"""

Usage:

  ./cat2json.py cat

Aim:

Extract positions of XMM sources.

Positions are fixed to 4/5 decinmal places for RA and Dec
to save space (and this should be sub-arcsecond accuracy).

"""

import json
import math

import pycrates

def roundy(v, n):
    """Restrict to n decimal places.

    Up to the vagueris of floating-point representation.
    """

    t = 10**n
    return math.floor(v * t + 0.5) / t

def convert(infile):

    # fname = "{}[2][cols ra=sc_ra,dec=sc_dec]".format(infile)
    fname = "{}[2][cols ra,dec]".format(infile)

    cr = pycrates.read_file(fname)

    cat = cr.name

    srcs = []
    for ra,dec in zip(cr.RA.values,
                      cr.DEC.values):

        # ensure a Python not NumPy string, as want it to serialize
        # to JSON
        #
        # Restrict RA to 4dp and Dec to 5dp to try and save space
        # in the serialization (tihs should be sub-arcsecond
        # accuracy).
        #
        ra = roundy(ra, 4)
        dec = roundy(dec, 5)

        srcs.append([ra, dec])

    return {'catalog': cat, 'sources': srcs}

if __name__ == "__main__":

    import sys
    if len(sys.argv) != 2:
        sys.stderr.write("Usage: {} cat\n".format(sys.argv[0]))
        sys.exit(1)

    out = convert(sys.argv[1])
    print(json.dumps(out))


import db

class Rates(object):

  def __init__(self, ID, DetachID, RateType, HolidayType, IncentiveType, Regular, Overtime, NDifferential, ECOLA, ThirteenMonth, PhilHealth, PagibigPrem, Incentive, LegalHoliday, SpecialHoliday, EffectiveDate):
    self.ID = ID
    self.DetachID = DetachID
    self.RateType = RateType
    self.HolidayType = HolidayType
    self.IncentiveType = IncentiveType
    self.Regular = Regular
    self.Overtime = Overtime
    self.NDifferential = NDifferential
    self.ECOLA = ECOLA
    self.ThirteenMonth = ThirteenMonth
    self.PhilHealth = PhilHealth
    self.PagibigPrem = PagibigPrem
    self.Incentive = Incentive
    self.LegalHoliday = LegalHoliday
    self.SpecialHoliday = SpecialHoliday
    self.EffectiveDate = EffectiveDate

  def save(self):
    #sql = "insert into Rates (RateType, HolidayType, IncentiveType, Regular, Overtime, NDifferential, ECOLA, ThirteenMonth, PhilHealth, PagibigPrem, Incentive, LegalHoliday, EffectiveDate) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    sql = "insert into Rates values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    params = (self.RateType, self.HolidayType, self.Incentive, self.Regular, self.Overtime, self.NDifferential, self.ECOLA, self.ThirteenMonth, self.PhilHealth, self.PagibigPrem, self.Incentive, self.LegalHoliday, self.EffectiveDate)
    return db.ins(sql, params)

  def get(prop):
    sql = "select %s from Rates where ID = %s"
    params = (prop, self.ID)
    return db.getOne(sql, params)

  def set(prop, val):
    sql = "update Rates set %s = %s where ID = %s"
    params = (prop, val, self.ID)
    return db.set(sql, params)


import nmc_verification.nmc_vf_method.multi_category.score as score
import numpy as np
if __name__ == '__main__':
    ob = np.random.rand(10, 2)
    fo = np.random.rand(10, 2)
    ob10 = ob * 10
    fo10 = fo * 10
    ob_int = ob10.astype(np.int8)
    fo_int = fo10.astype(np.int8)
    grade_list = [3, 5]

    accuracy = score.accuracy(ob_int,fo_int,grade_list)
    print('accuracy评分',accuracy)
    accuracy = score.accuracy(ob_int, fo_int)
    print('accuracy评分',accuracy)

    hss = score.hss(ob,fo)
    print('hss评分：', hss)
    hss = score.hss(ob,fo,grade_list)
    print('hss评分：', hss)

    hk = score.hk(ob, fo,[0.1,10,25])
    print('hk评分：', hk)


import os

def listd(sciezka):
    counter = 0
    for item, item2, item3 in os.walk(sciezka):
        for item4 in item3:
            print(os.path.join(item, item4))
            counter += 1
    print("plikow: ", counter)

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