#! /usr/bin/env python

"""
open-dobot Accelerometers Calibration tool.

This tool continuously reports accelerometers and angles from those.

Use this tool to find offsets for your accelerometers.

Follow the procedure below to enable accelerometers reporting mode on FPGA.
No action required on RAMPS as GY-521 accelerometers can be read at any time there.
1. Turn off power on the arm and disconnect USB cable
2. Remove accelerometers from the arm and put them on a flat surface that has no inclination
3. Connect USB cable
4. Enable accelerometers reporting mode:
   4.1. Press and hold the "Sensor Calibration" button on FPGA version or ground pin D23 on AUX-4 on RAMPS version
   4.2. Press and release the "Reset" button
   4.3. Start this tool (still holding "Sensor Calibration" button on FPGA version or keeping pin D23 grounded on RAMPS)
   4.4. Wait for the accelerometer data to start flowing on your console/whatever_you_use_to_start_this_tool
   4.5. Release the "Sensor Calibration" button
5. Gently push down the accelerometers so that they are evenly on the surface. Don't touch any contacts/leads.
	You can push them one by one, not necessary to push both at the same time
6. Note the "Raw" data from accelerometers reported on the console. Those are your accelerometers' offsets
7. Turn off power on the arm, disconnect USB cable, mount accelerometers back onto the arm

Author: maxosprojects (March 18 2016)
Additional Authors: <put your name here>

Version: 1.2.2

License: MIT
"""
import math

from dobot import DobotDriver
from dobot import DobotKinematics

# driver = DobotDriver('COM4')
driver = DobotDriver('/dev/tty.usbmodem1421')
driver.Open()
# driver.Open(timeout=0.3)
kinematics = DobotKinematics()

# Offsets must be found using this tool for your Dobot once
# (rear arm, frontarm)
offsets = (1024, 1024)

def toEndEffectorHeight(rear, front):
	ret = kinematics.coordinatesFromAngles(0, rear, front)
	return ret[2]

while True:
	ret = driver.GetAccelerometers()
	if ret[0]:
		if driver.isFpga():
			print("Rear arm: {0:10f} | Front arm: {1:10f} | End effector height: {2:10f} | Raw rear arm: {3:4d} | Raw front arm: {4:4d}".format(\
				driver.accelToAngle(ret[1], offsets[0]), driver.accelToAngle(ret[4], offsets[1]),\
				toEndEffectorHeight(driver.accelToRadians(ret[1], offsets[0]), driver.accelToRadians(ret[4], offsets[1])),\
				ret[1], ret[4]))
		else:
			print("Rear arm: {0:6.2f} | Front arm: {1:6.2f} | End effector height: {2:7.2f} | Raw rear arm: {3:6d} {4:6d} {5:6d} | Raw front arm: {6:6d} {7:6d} {8:6d}".format(\
				driver.accel3DXToAngle(ret[1], ret[2], ret[3]), -driver.accel3DXToAngle(ret[4], ret[5], ret[6]),\
				toEndEffectorHeight(driver.accel3DXToRadians(ret[1], ret[2], ret[3]), -driver.accel3DXToRadians(ret[4], ret[5], ret[6])),\
				ret[1], ret[2], ret[3], ret[4], ret[5], ret[6]))
	else:
		print('Error occurred reading data')


#!/usr/bin/env python3

import unittest
from unittest.mock import patch, MagicMock
import pandas as pd

from tmc import points

from tmc.utils import load, get_stdout, patch_helper, spy_decorator

module_name="src.top_bands"
top_bands = load(module_name, "top_bands")
main = load(module_name, "main")
ph = patch_helper(module_name)

@points('p05-03.1')
class TopBands(unittest.TestCase):

    def test_shape(self):
        df = top_bands()
        self.assertEqual(df.shape, (9, 13), msg="Incorrect shape!")

    def test_columns(self):
        df = top_bands()
        cols = ['Pos', 'LW', 'Title', 'Artist', 'Publisher', 'Peak Pos', 'WoC', 'Band',
                'Singer', 'Lead guitar', 'Rhythm guitar', 'Bass', 'Drums']
        self.assertCountEqual(df.columns, cols, msg="Incorrect columns!")

    def test_calls(self):
        merge_method = spy_decorator(pd.core.frame.DataFrame.merge, "merge")
        with patch(ph("top_bands"), wraps=top_bands) as ptop,\
            patch(ph("pd.read_csv"), wraps=pd.read_csv) as prc,\
            patch.object(pd.core.frame.DataFrame, "merge", new=merge_method),\
            patch(ph("pd.merge"), wraps=pd.merge) as pmerge:
            main()
            ptop.assert_called_once()
            self.assertEqual(prc.call_count, 2,
                             msg="You should have called pd.read_csv exactly twice")
            self.assertTrue(pmerge.call_count == 1 or merge_method.mock.call_count == 1, msg="Call merge exactly once!")

            if pmerge.call_count >= 1:
                args, kwargs = pmerge.call_args             # function called
            else:
                args, kwargs = merge_method.mock.call_args  # method called

            self.assertTrue("left_on" in kwargs,
                            msg="You should have used 'left_on' argument of pd.merge!")
            self.assertTrue("right_on" in kwargs,
                            msg="You should have used 'right_on' argument of pd.merge!")
            params = [kwargs["left_on"], kwargs["right_on"]]
            self.assertTrue(("Artist" in params or ["Artist"] in params) and
                            ("Band" in params or ["Band"] in params) ,
                            msg="You should have merged on 'Artist' and 'Band' columns!")

if __name__ == '__main__':
    unittest.main()


import math

a = float(input("Informe um valor para a: "))
b = float(input("Informe um valor para b: "))
c = float(input("Informe um valor para c: "))

delta = b**2 - 4 * a * c  # Fórmula de delta na matemática

print(f"\nO valor de delta é \033[31m{delta}\033[m")

x1 = 0
x2 = 0

# Forma 1
if a == 0:
    print("\n\033[31mO valor de a tem que ser diferente de 0\033[m")
elif delta < 0:
    print("\n\033[31mSem raízes reais\033[m")
else:
    y = math.sqrt(delta)  # Raiz quadrada de delta
    x1 = (-b + y) / (2 * a)
    x2 = (-b - y) / (2 * a)

print(f'\nO valor de x1 é \033[35m{x1:.2f}\033[m')
print(f'\nO valor de x2 é \033[36m{x2:.2f}\033[m')

# Forma 2
# if a == 0:
#    print("O valor de a tem que ser diferente de 0")
# elif delta < 0:
#    print("Sem raízes reais")
# else:
# x1 = (-b + delta ** (1 / 2)) / (2 * a)  # Elevar um número a 1/2 é o mesmo que tirar sua raiz quadrada...
# x2 = (-b - delta ** (1 / 2)) / (2 * a)

# print("x1: {}, x2: {}".format(x1, x2))

if delta > 0:
    print("\n\033[34mDelta tem duas raízes reais\033[m")
elif delta == 0:
    print("\n\033[33mDelta tem uma única raiz real\033[m")
else:
    print("\n\033[30mDelta não tem raiz\033[m")

# Código descobrindo o valor de delta e as raízes caso existam
# Fórmula do delta: Δ = b**2 – 4 * a * c
# Fórmula para descobrir as raízes: x = – b ± √Δ / 2 * a


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

# In[1]:

import speech_recognition as sr
import os
import random

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import unidecode
from tkinter import *
from PIL import ImageTk,Image

# In[2]:

class Location:
    def __init__(self):
        self.country = None
        self.city = None
        self.path = None

# In[3]:

def get_random_location(page,countries_dir_path, windows=False):
    if windows:
        separator = '\\'
    else:
        separator = '/'

    global location
    location = Location()
    path = countries_dir_path

    dir_content = os.listdir(path)
    countries = [i for i in dir_content if os.path.isdir(path + separator + i)]
    location.country = random.choice(countries)
    path += separator + location.country

    dir_content = os.listdir(path)
    cities = [i for i in dir_content if os.path.isdir(path + separator + i)]
    location.city = random.choice(cities)
    path += separator + location.city

    dir_content = os.listdir(path)
    locations = [i for i in dir_content if os.path.isfile(path + separator + i)]
    path += separator + random.choice(locations)
    location.path = path

    page.my_img = ImageTk.PhotoImage(Image.open(location.path))

    if page.my_label != None:
        page.my_label.configure(image=page.my_img)
        page.my_label.image = page.my_img

    return location

# In[4]:

def verify_guess(guess, location):
    return unidecode.unidecode(guess).lower() == location.country

# In[5]:

def record_guess():
    global sentence
    mic = sr.Recognizer()
    with sr.Microphone() as source:
        mic.adjust_for_ambient_noise(source)
        audio = mic.listen(source)

    try:
        sentence = mic.recognize_google(audio,language='pt-BR')
    except sr.UnknownValueError:
        sentence = "unknown"

    return sentence

# In[6]:

def record_and_verify_guess(controller,windows=False):
    record_guess()
    if verify_guess(sentence,location):
        global score
        score += 1
        get_random_location(controller.frames["PageOne"],countries_path, windows)
        controller.show_frame("PageOne")
    else:
        controller.frames["PageTwo"].label2.configure(text="A localização correta era: " + location.city + ", " + location.country)
        controller.frames["PageTwo"].label3.configure(text="Total de acertos: " + str(score))
        controller.show_frame("PageTwo")

# In[7]:

def reset_score():
    global score
    score = 0

# In[8]:

try:
    import tkinter as tk                # python 3
    from tkinter import font as tkfont  # python 3
except ImportError:
    import Tkinter as tk     # python 2
    import tkFont as tkfont  # python 2

countries_path = "paises"
win = True
score = 0

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Small Fonts', size=40, weight="bold")
        self.button_font = tkfont.Font(family='Small Fonts', size=20, weight="bold")#, slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Geotástico", font=controller.title_font)
        label.pack(side="top", fill="x", padx=500, pady=300)

        button1 = tk.Button(self, text="Jogar", font=controller.button_font,
                            command=lambda:[get_random_location(controller.frames["PageOne"],countries_path, windows=win),controller.show_frame("PageOne")])
        button2 = tk.Button(self, text="Sair", font=controller.button_font,
                            command=lambda: controller.destroy())
        button1.pack()
        button2.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Que país é esse?", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Gravar", font=controller.button_font,
                           command=lambda:record_and_verify_guess(controller, windows=win))
        button.pack()

        button = tk.Button(self, text="Voltar para o início", font=controller.button_font,
                           command=lambda:[reset_score(),controller.show_frame("StartPage")])
        button.pack()

        self.my_img = None
        self.my_label = None

        get_random_location(self, countries_path, windows=win)

        self.my_img = ImageTk.PhotoImage(Image.open(location.path))
        self.my_label = Label(self,image=self.my_img)
        self.my_label.pack()

        #panel.configure(image=img2)
        #panel.image = img2

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.label1 = tk.Label(self, text="Você errou!", font=controller.title_font)
        self.label1.pack(side="top", fill="x", pady=10)
        self.label2 = tk.Label(self, text="A localização correta era: " + location.city + ", " + location.country, font=controller.title_font)
        self.label2.pack(side="top", fill="x", pady=6)
        self.label3 = tk.Label(self, text="Total de acertos: " + str(score), font=controller.title_font)
        self.label3.pack(side="top", fill="x", pady=2)
        button = tk.Button(self, text="Voltar para o início", font=controller.button_font,
                           command=lambda:[reset_score(),controller.show_frame("StartPage")])
        button.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

# In[ ]:


"""Wrapper for pam.py to run. Until we write ram/sam."""

import os
import re
import subprocess
import unittest

FILE_NAME = 'E4.hs'
TEST_FILE_NAME = 'TestE4.hs'
OUTPUT_FILE = 'output.txt'
TIMEOUT = 2  # timeout for each function; make sure it is << global timeout

# function-name to max-marks-possible
FUNCTIONS = {"interleave": 6, "toPairs": 6, "repeat": 3,
             "numNeg": 3, "genSquares": 3, "triples": 4}

def run_all():
    if os.path.exists(OUTPUT_FILE):
        os.remove(OUTPUT_FILE)
    results = FUNCTIONS.copy()

    for func in FUNCTIONS:
        run(func, results)

    return results

def run(func, results):
    if not os.path.isfile(FILE_NAME):
        return

    result = '{}:\n\t'.format(func)
    result_out, result_err = '', ''

    try:
        completed_process = subprocess.run(
            ['ghc', TEST_FILE_NAME, '-e', '{}Res'.format(func)],
            capture_output=True, timeout=TIMEOUT, text=True)
        result_out = completed_process.stdout.strip()
        result_err = completed_process.stderr
        result_err = re.sub(r'Cases(.*)\n', '', result_err).strip()
    except subprocess.TimeoutExpired:
        result += 'Timeout!\n'

    match = re.search(
        r'Counts {cases = (\d), tried = (\d), errors = (\d), failures = (\d)}',
        result_out)

    if match is None:
        print('Warning: could not record results for {}'.format(func))
    else:
        result += result_out + '\n\n'
        results[func] = int(match.group(3)) + int(match.group(4))

    result += result_err + '\n'

    with open(OUTPUT_FILE, 'a') as out:
        out.write(result)

class TestE4(unittest.TestCase):
    def _test_success(self, f_name):
        pass

    def _test_fail(self, f_name):
        self.fail(_msg(f_name))

    def test_hlint(self):
        self.assertTrue(os.path.isfile(FILE_NAME),
                        'No submission file {}.'.format(FILE_NAME))

        completed_process = subprocess.run(['hlint', FILE_NAME],
                                           capture_output=True,
                                           text=True)

        result_out = completed_process.stdout.strip()
        result_err = completed_process.stderr.strip()

        with open(OUTPUT_FILE, 'a') as out:
            out.write(result_out + '\n\n' + result_err)

        self.assertEqual(result_out, 'No hints',
                         'Errors reported by hlint. '
                         'See output.txt for details.')

def _msg(func_name):
    return "Errors in {}. See {} for details.".format(func_name,
                                                      OUTPUT_FILE)

def create_tests(unittest_object, test_success, test_fail, results):
    """Generate unittest test methods: one per function in function_names."""

    for f_name, num_fails in results.items():
        for i in range(num_fails):
            test_name = 'test_{}_{}'.format(f_name.replace('-', '_'), i)

            def new_test(self, f_name=f_name):
                return test_fail(self, f_name)

            setattr(unittest_object, test_name, new_test)
        for i in range(num_fails, FUNCTIONS[f_name]):
            test_name = 'test_{}_{}'.format(f_name.replace('-', '_'), i)

            def new_test(self, f_name=f_name):
                return test_success(self, f_name)

            setattr(unittest_object, test_name, new_test)

results = run_all()
create_tests(TestE4, TestE4._test_success, TestE4._test_fail, results)

if __name__ == '__main__':
    unittest.main(exit=False)


import argparse
import json
import numpy as np
import torch
import fileinput
import sys
import re

def main():
    """
    Create word2vec format files from nparray
    """

    # Get the arguments
    parser = argparse.ArgumentParser(
        description='Create json files of [[idx_1,"sentence_1"], ["idx_2,sentence_2"]]')

    parser.add_argument('dataset_path', help='path to corpus1 directory with zipped files')
    parser.add_argument('trained_dir', help='path to corpus1 directory with zipped files')

    args = parser.parse_args()
    dataset_path = args.dataset_path
    trained_dir = args.trained_dir

    print("dataset_path: ",dataset_path)
    print("trained_dir: ",trained_dir)

    with open('./'+dataset_path, 'r') as fp:
        texts = json.load(fp)

    # data/en_4.0.0/corpus1/lemma_docids.json -> corpus1
    dataset_filename = str(args.dataset_path).rsplit('/', 2)[1]

    decoder = np.load('./'+trained_dir+'/npy/'+dataset_filename+'_decoder.npy',allow_pickle=True).item()

    # corpus1 -> 1
    matrix_name = 'mat'+str(int([float(n) for n in re.findall(r'-?\d+\.?\d*', dataset_filename)][-1] ))
    state = torch.load('./'+trained_dir+'/'+matrix_name+'.pt',
                       map_location=lambda storage, loc: storage)
    word_vectors = state['neg.embedding.weight'].cpu().clone().numpy()
    rev_decoder = {v: k for k, v in decoder.items()}
    vocab = [i for i, j in sorted(rev_decoder.items(), key=lambda x: x[1])]

    rev_decoder_list = list(rev_decoder)

    for i in range(len(vocab)):
        word = rev_decoder_list[i]
        vector = word_vectors[:i + 1]

    c = np.savetxt(trained_dir+'/'+matrix_name, vector, delimiter=' ', fmt='%.8f')

    for idx, line in enumerate(fileinput.input([trained_dir+'/'+matrix_name], inplace=True)):
        sys.stdout.write(rev_decoder_list[idx] + ' {l}'.format(l=line))

    f = open(trained_dir+'/'+matrix_name, 'r+')
    lines = f.readlines()  # read old content
    f.seek(0)  # go back to the beginning of the file
    f.write(str(len(vocab)) + " " + str(100) + "\n")  # write new content at the beginning
    for line in lines:  # write old content after new
        f.write(line)
    f.close()

if __name__ == '__main__':
    main()


from pyspark import SparkContext
import json
import csv

review_path = "review.json"
business_path = "business.json"
output_path = "user_business.csv"

def printf(p):
    print(list(p))

sc = SparkContext(appName="inf553")
business_file = sc.textFile(business_path).map(lambda s: json.loads(s)).map(lambda s:(s["business_id"],s["state"]))\
    .filter(lambda s: s[1]=="NV").map(lambda s: s[0]).collect()

review_file = sc.textFile(review_path).map(lambda s: json.loads(s)).map(lambda s: (s["user_id"],s["business_id"]))\
    .filter(lambda s: s[1] in business_file).collect()

f = open(output_path,"w",encoding='utf-8',newline='' "")
csv_writer = csv.writer(f)
csv_writer.writerow(["user_id","business_id"])
for i in review_file:
    csv_writer.writerow([i[0],i[1]])
f.close()


import pandas as pd
base = pd.read_csv('/workspace/tensorFlow/udemyMLDS/RegresaoLinear/dados/house-prices.csv')

x = base.iloc[:,5:6].values
y = base.iloc[:,2].values

from sklearn.model_selection import train_test_split
x_treinamento, x_teste, y_treinamento, y_teste = train_test_split(x, y, test_size = 0.3, random_state = 0)

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_treinamento, y_treinamento)
score = regressor.score(x_treinamento, y_treinamento)
print('Score:', score)

import matplotlib.pylab as plt
plt.scatter(x_treinamento, y_treinamento)
plt.plot(x_treinamento, regressor.predict(x_treinamento), color = 'red')
plt.show()

print( 'Analise Manual' )
previsores  = regressor.predict(x_teste)
resultado = y_teste - previsores
print(abs(resultado))
print(abs(resultado).mean())
print( 'Analise Sklearn')
from sklearn.metrics import mean_absolute_error, mean_squared_error
mae = mean_absolute_error(y_teste, previsores)
print(mae)


from __main__ import app, get_db
from flask import jsonify, request

from database.Vacancy import *
from database.AccessToken import *

@app.route('/api/vacancies', methods=['GET'])
def vacancies():
    access_token_db = AccessTokenDB( get_db() )

    if request.method == 'GET':

        token = None
        try:
            token = request.headers['Authorization']
            print('token', token)
        except Exception as e:
            print('token has been received with error:', e)

        if not token:
            return jsonify( {'success': False, 'error': 'not found token'} ), 401

        if not access_token_db._check_token(token):
            return jsonify( {'success': False, 'error': 'incorrect token'} ), 401

        vacancy_db = VacancyDB( get_db() )

        vacancies = vacancy_db._get_vacancies()
        if not vacancies:
            return jsonify( {'success': False, 'error': 'vacancies not found'} )

        data = list()
        for vacancy in vacancies:
            vac = dict()
            v = {
                'id': vacancy[0],
                'title': vacancy[1],
                'description': vacancy[2],
                'user_id': vacancy[3]
            }
            for key in v.keys():
                vac[key] = v[key]
            data.append(vac)

        return jsonify( data )
    return None, 405


from odoo.addons.component.core import Component
from odoo.addons.connector.components.mapper import mapping
from odoo.addons.connector.exception import MappingError

class ProductTemplateBatchImporter(Component):
    """ Import the distant Odoo Product Template.

    For every Product Templaes in the list, a delayed job is created.
    """
    _name = 'odoo.product.template.batch.importer'
    _inherit = 'odoo.delayed.batch.importer'
    _apply_on = ['odoo.product.template']

    def _import_record(self, external_id, job_options=None):
        """ Delay the job for the import"""
        super(ProductTemplateBatchImporter, self)._import_record(
            external_id, job_options=job_options,
        )

    def run(self, filters=[]):
        """ Run the synchronization"""
        offset = 0

        updated_ids = self.backend_adapter.search(filters, limit=100, offset=offset)
        while updated_ids:
            for updated in updated_ids:
                self._import_record(updated)
            offset += 100
            updated_ids = self.backend_adapter.search(filters, limit=100, offset=offset)

class ProductTemplateImporter(Component):
    _name = 'odoo.product.template.importer'
    _inherit = 'odoo.importer'
    _apply_on = ['odoo.product.template']

    def _import_dependencies(self):
        """ Import the ependencies for the record"""
        record = self.odoo_record

        depends = [
            ('categ_id', 'odoo.product.category'),
            ('uom_id', 'odoo.product.uom'),
            ('uom_po_id', 'odoo.product.uom'),
            ('pump_set', 'odoo.product.product'),
            ('product_brand_id', 'odoo.product.brand'),
            ('manufacturer', 'odoo.res.partner'),
        ]

        for dep in depends:
            if record.get(dep[0]):
                self._import_dependency(record.get(dep[0])[0], dep[1])

        if record.get('categ_ids'):
            for c in record.get('categ_ids'):
                self._import_dependency(c, 'odoo.product.category')

class ProductTemplateImportMapper(Component):
    _name = 'odoo.product.template.import.mapper'
    _inherit = 'odoo.import.mapper'
    _apply_on = 'odoo.product.template'

    direct = [
        ('name', 'name'),
        ('sequence', 'sequence'),
        ('description', 'description'),
        ('description_purchase', 'description_purchase'),
        ('description_sale', 'description_sale'),
        ('type', 'type'),
        ('rental', 'rental'),
        ('list_price', 'list_price'),
        ('cost_price', 'cost_price'),
        ('volume', 'volume'),
        ('weight', 'weight'),
        ('sale_ok', 'sale_ok'),
        ('purchase_ok', 'purchase_ok'),
        ('active', 'active'),
        ('color', 'color'),
        ('service_type', 'service_type'),
        ('description_picking', 'description_picking'),
        ('description_pickingout', 'description_pickingout'),
        ('description_pickingin', 'description_pickingin'),
        ('who_product', 'who_product'),
        ('pro_product', 'pro_product'),
        ('breast_pump', 'breast_pump'),
        ('followup_breast_pump', 'followup_breast_pump'),
        ('caution_amount', 'caution_amount'),
        ('compact_breast_pump', 'compact_breast_pump'),
        ('silent_breast_pump', 'silent_breast_pump'),
        ('breast_pump_power_type', 'breast_pump_power_type'),
        ('return_indications', 'return_indications'),
        ('manufacturer_pname', 'manufacturer_pname'),
        ('manufacturer_pref', 'manufacturer_pref'),
        ('manufacturer_purl', 'manufacturer_purl'),
        # ('purchase_method', 'purchase_method'),
        ('code_prefix', 'code_prefix'),
    ]

    @mapping
    def backend_id(self, record):
        return {'backend_id': self.backend_record.id}

    @mapping
    def categ_id(self, record):
        return self._get_many2one_internal_id(record, 'odoo.product.category', 'categ_id')

    @mapping
    def categ_ids(self, record):
        categ_ids = []
        for categ in record.get('categ_ids', []):
            binder = self.binder_for('odoo.product.category')
            model_binding = binder.to_internal(categ)

            if not model_binding:
                raise MappingError(
                    "The odoo.product.category with distant Odoo id %s is not import." % (
                        categ)
                )
            else:
                categ_ids.append(model_binding.odoo_id.id)

        return {'categ_ids': [(6, 0, categ_ids)]}

    @mapping
    def uom_id(self, record):
        return self._get_many2one_internal_id(record, 'odoo.product.uom', 'uom_id')

    @mapping
    def uom_po_id(self, record):
        return self._get_many2one_internal_id(record, 'odoo.product.uom', 'uom_po_id')

    @mapping
    def pump_set(self, record):
        return self._get_many2one_internal_id(record, 'odoo.product.product', 'pump_set')

    @mapping
    def product_brand_id(self, record):
        return self._get_many2one_internal_id(record, 'odoo.product.brand', 'product_brand_id')

    @mapping
    def manufacturer(self, record):
        return self._get_many2one_internal_id(record, 'odoo.res.partner', 'manufacturer')


def compress(chars):
    read = write = 0
    count = 1

    for i in range(1, len(chars)):
        if chars[i] == chars[read]:
            count += 1
        else:
            chars[write] = chars[read]
            write += 1

            if count > 1:
                count_str = str(count)
                for digit in count_str:
                    chars[write] = digit
                    write += 1

            count = 1
            read = i

    chars[write] = chars[read]
    write += 1

    if count > 1:
        count_str = str(count)
        for digit in count_str:
            chars[write] = digit
            write += 1

    return write

chars = ["a","a","b","b","c","c","c"]
print(compress(chars=chars))


"""Censys Cloud Connectors config command."""
import argparse
import contextlib
import importlib
from typing import Any

from pydantic import ValidationError

from censys.cloud_connectors.common.cli.base import print, print_question, prompt
from censys.cloud_connectors.common.enums import ProviderEnum
from censys.cloud_connectors.common.logger import get_logger
from censys.cloud_connectors.common.settings import Settings

def cli_config(args: argparse.Namespace):
    """Configure Censys Cloud Connectors.

    Args:
        args (argparse.Namespace): Namespace.
    """
    logger = get_logger(log_name="censys_cloud_connectors", level="INFO")

    try:
        settings = Settings(_env_file=".env")  # type: ignore
    except ValidationError as e:
        error_str = str(e)
        print(error_str)
        if "censys_api_key" in error_str:
            print("Please ensure the CENSYS_API_KEY environment variable is set")
        return

    if args.provider is None:
        questions: list[dict[str, Any]] = [
            {
                "type": "list",
                "name": "provider",
                "message": "Select a provider:",
                "choices": [
                    {
                        "name": str(provider),
                        "value": provider,
                    }
                    for provider in ProviderEnum
                ],
            }
        ]
        answers = prompt(questions)
        provider = answers["provider"]
    else:
        provider_name = args.provider
        provider = ProviderEnum[provider_name]
        print_question(f"Provider: [info]{provider}[/info]")

    provider_setup_cls = importlib.import_module(
        provider.module_path()
    ).__provider_setup__

    with contextlib.suppress(FileNotFoundError):
        settings.read_providers_config_file()
    try:
        provider_setup = provider_setup_cls(settings)
        provider_setup.setup()
    except ValidationError as e:  # pragma: no cover
        logger.error(e)
        return
    settings.write_providers_config_file()
    print(f"Successfully saved settings to {settings.providers_config_file}")

def include_cli(parent_parser: argparse._SubParsersAction):
    """Include this subcommand into the parent parser.

    Args:
        parent_parser (argparse._SubParsersAction): Parent parser.
    """
    config_parser = parent_parser.add_parser(
        "config",
        description="Configure Censys Cloud Connectors",
        help="configure censys cloud connectors",
    )
    provider_choices = [str(provider).lower() for provider in ProviderEnum]
    config_parser.add_argument(
        "-p",
        "--provider",
        choices=provider_choices,
        help=f"specify a cloud service provider: {provider_choices}",
        metavar="PROVIDER",
        dest="provider",
        nargs="?",
        type=str.lower,
        default=None,
    )
    config_parser.set_defaults(func=cli_config)


from .utils import *

import torch
import torch.nn as nn

from torch.autograd import Variable

from .config import *

import torch.distributions as dists

from moic.data_structure import *

def point_wise_pdf(input_x,coord):
    """
    input_x: the grid of input that reprents the image
    coord  : the Nx2 shape tensor that represents locations in the grid
    """
    point_wise_normal = dists.Normal(coord,scale = opt.scale)

    return input_x

class ConceptModelSearch(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self,x):
        return x

class EuclidConceptModel(nn.Module):
    def __init__(self,resolution = opt.resolution):
        super().__init__()
        """
        An euclid concept program is either a:
        point (x,y)  line(p1,p2) circle(p1,p2)
        or the composition of these things (constraint, and)
        """
        self.resolution = resolution
        self.euclid_program = None
        self.grid = make_grid(resolution).permute([1,2,0]).to(opt.device)

    def forward(self,x,format = "logp"):
        """
        logp          : return the log probability of image x is created by the model
        cross_entropy : return the cross entropy of the image x created by the model
        """
        return 0

class EuclidPointModel(EuclidConceptModel):
    def __init__(self,coord = None):
        super().__init__()
        self.coord = nn.Parameter(coord) if coord is not None else nn.Parameter(torch.randn([1,2]))

    def pdf(self,log = True):
        point_wise_normal = dists.Normal(self.coord,opt.scale)
        if log:return torch.sum(point_wise_normal.log_prob(self.grid),-1)
        return  torch.sum(point_wise_normal.log_prob(self.grid),-1).exp()

    def exist(self,x,log = True):
        pdf = self.pdf(log).unsqueeze(-1)
        return torch.sum(torch.binary_cross_entropy_with_logits(pdf,x),-1)
        # return torch.sum(pdf * x,-1)

class EuclidLineModel(EuclidConceptModel):
    def __init__(self,point1 = None,point2 = None):
        super().__init__()

        self.segments = 50

        if point1 is None:
            self.point1 = EuclidPointModel(None) # if there is no given point

        elif isinstance(point1,EuclidPointModel):
            self.point1 = point1 # if the parameter is a give point
        else:
            self.point1 = EuclidPointModel(point1) # there is fixed given point

        if point2 is None:
            self.point2 = EuclidPointModel(None) # if there is no given point

        elif isinstance(point2,EuclidPointModel):
            self.point2 = point2 # if the parameter is a give point
        else:
            self.point2 = EuclidPointModel(point2) # there is fixed given point
        self.line = segment(self.point1.coord,self.point2.coord,self.segments)

    def pdf(self,log = True):
        grid_expand = self.grid.flatten(start_dim = 0, end_dim = 1).unsqueeze(0).repeat([self.segments,1,1])
        self.line = segment(self.point1.coord,self.point2.coord,self.segments)
        diff = grid_expand - self.line.unsqueeze(1).repeat([1,self.resolution[0] * self.resolution[1],1])

        leng_diff = torch.norm(diff,2,dim = -1)
        min_diff = torch.min(leng_diff,0).values

        line_norm = dists.Normal(0,opt.line_scale)
        logpdf = line_norm.log_prob(min_diff)

        logpdf = logpdf.view(opt.resolution)
        if log:return logpdf
        return  logpdf.exp()

    def exist(self,x,log = True):
        pdf = self.pdf(log).unsqueeze(-1)
        return torch.sum(torch.binary_cross_entropy_with_logits(pdf,x),-1)
        #return torch.sum(pdf * x,-1)

class EuclidAngleModel(EuclidConceptModel):
    def __init__(self,point1 = None,point2 = None,point3 = None):
        super().__init__()

        self.segments = 50

        if point1 is None:
            self.point1 = EuclidPointModel(None) # if there is no given point

        elif isinstance(point1,EuclidPointModel):
            self.point1 = point1 # if the parameter is a give point
        else:
            self.point1 = EuclidPointModel(point1) # there is fixed given point

        if point2 is None:
            self.point2 = EuclidPointModel(None) # if there is no given point

        elif isinstance(point2,EuclidPointModel):
            self.point2 = point2 # if the parameter is a give point
        else:
            self.point2 = EuclidPointModel(point2) # there is fixed given point
        self.line = segment(self.point1.coord,self.point2.coord,self.segments)

        if point3 is None:
            self.point3 = EuclidPointModel(None) # if there is no given point

        elif isinstance(point3,EuclidPointModel):
            self.point3 = point3 # if the parameter is a give point
        else:
            self.point3 = EuclidPointModel(point3) # there is fixed given point
        self.line = segment(self.point1.coord,self.point2.coord,self.segments)

    def pdf(self,log = True):
        grid_expand = self.grid.flatten(start_dim = 0, end_dim = 1).unsqueeze(0).repeat([self.segments*2,1,1])

        self.line = torch.cat([segment(self.point1.coord,self.point2.coord,self.segments),segment(self.point2.coord,self.point3.coord,self.segments)],-2)

        diff = grid_expand - self.line.unsqueeze(1).repeat([1,self.resolution[0] * self.resolution[1],1])

        leng_diff = torch.norm(diff,2,dim = -1)
        min_diff = torch.min(leng_diff,0).values

        line_norm = dists.Normal(0,opt.line_scale)
        logpdf = line_norm.log_prob(min_diff)

        logpdf = logpdf.view(opt.resolution)
        if log:return logpdf
        return  logpdf.exp()

    def exist(self,x,log = True):
        pdf = self.pdf(log).unsqueeze(-1)

        return torch.sum(torch.binary_cross_entropy_with_logits(pdf,x),-1)
        #return torch.sum(pdf * x,-1)

class EuclidCircleModel(EuclidConceptModel):
    def __init__(self):super().__init__()

    def forward(self):return

class EuclidCompositeModel(EuclidConceptModel):
    def __init__(self,program):
        super().__init__()
        if isinstance(program,str):program = toFuncNode(program)

    def pdf(self,log = True):return log

    def exist(self,log = True):return log

def adjust_model_to_observation(model,x,n_epochs = 50,visualize = True):
    optim = torch.optim.Adam(model.parameters(), lr = opt.lr)
    for epoch in range(n_epochs):
        optim.zero_grad()
        logpdf = model.exist(x)

        loss =  torch.sum(logpdf)
        if visualize:

            print(epoch,loss.detach().numpy())
            outputs = model.pdf(False)
            plt.subplot(1,2,1);plt.cla()
            plt.imshow(outputs.detach())
            plt.subplot(1,2,2);plt.cla()
            plt.imshow(logpdf.detach());plt.pause(0.01)

        loss.backward(retain_graph = True)
        optim.step()


import os
from PIL import Image

liste_dir = os.listdir()

x = 338
y = 144

x2 = 1710
y2 = 1069

for element in liste_dir :
	if os.path.isdir(element):
		liste_files = os.listdir(element+'/')
		for img in liste_files :
			if not os.path.isdir(element+'/'+img):
				if element > str(439):
					image = Image.open(element+'/'+img)
					image = image.crop((x,y,x2,y2))
					image.save(element+'/'+img)
print("END")


import json
import math
import os
from multiprocessing import Pool, cpu_count
from typing import Dict, Tuple

import numpy as np
import torch
import torchvision.transforms.functional as tf
from PIL import Image
from tqdm import tqdm

from .dicom_utils import read_one_dcm

PADDING_VALUE: int = 0

def read_dcms(dcm_dir, error_msg=False) -> (Dict[Tuple[str, str, str], Image.Image], Dict[Tuple[str, str, str], dict]):
    """
    读取文件夹内的所有dcm文件
    :param dcm_dir:
    :param error_msg: 是否打印错误信息
    :return: 包含图像信息的字典，和包含元数据的字典
    """
    dcm_paths = []
    for study in os.listdir(dcm_dir):
        study_path = os.path.join(dcm_dir, study)
        for dcm_name in os.listdir(study_path):
            dcm_path = os.path.join(study_path, dcm_name)
            dcm_paths.append(dcm_path)

    with Pool(cpu_count()) as pool:
        async_results = []
        for dcm_path in dcm_paths:
            async_results.append(pool.apply_async(read_one_dcm, (dcm_path,)))

        images, metainfos = {}, {}
        for async_result in tqdm(async_results, ascii=True):
            async_result.wait()
            try:
                metainfo, image = async_result.get()
            except RuntimeError as e:
                if error_msg:
                    print(e)
                continue
            key = metainfo['studyUid'], metainfo['seriesUid'], metainfo['instanceUid']
            del metainfo['studyUid'], metainfo['seriesUid'], metainfo['instanceUid']
            images[key] = tf.to_pil_image(image)
            metainfos[key] = metainfo

    return images, metainfos

def get_spacing(metainfos: Dict[Tuple[str, str, str], dict]) -> Dict[Tuple[str, str, str], torch.Tensor]:
    """
    从元数据中获取像素点间距的信息
    :param metainfos:
    :return:
    """
    output = {}
    for k, v in metainfos.items():
        spacing = v['pixelSpacing']
        spacing = spacing.split('\\')
        spacing = list(map(float, spacing))
        output[k] = torch.tensor(spacing)
    return output

with open(os.path.join(os.path.dirname(__file__), 'static_files/spinal_vertebra_id.json'), 'r') as file:
    SPINAL_VERTEBRA_ID = json.load(file)

with open(os.path.join(os.path.dirname(__file__), 'static_files/spinal_disc_id.json'), 'r') as file:
    SPINAL_DISC_ID = json.load(file)

assert set(SPINAL_VERTEBRA_ID.keys()).isdisjoint(set(SPINAL_DISC_ID.keys()))

with open(os.path.join(os.path.dirname(__file__), 'static_files/spinal_vertebra_disease.json'), 'r') as file:
    SPINAL_VERTEBRA_DISEASE_ID = json.load(file)

with open(os.path.join(os.path.dirname(__file__), 'static_files/spinal_disc_disease.json'), 'r') as file:
    SPINAL_DISC_DISEASE_ID = json.load(file)

def read_annotation(path) -> Dict[Tuple[str, str, str], Tuple[torch.Tensor, torch.Tensor]]:
    """

    :param path:
    :return: 字典的key是（studyUid，seriesUid，instance_uid）
             字典的value是两个矩阵，第一个矩阵对应锥体，第一个矩阵对应椎间盘
             矩阵每一行对应一个脊柱的位置，前两列是位置的坐标(横坐标, 纵坐标)，之后每一列对应一种疾病
             坐标为0代表缺失
             ！注意图片的坐标和tensor的坐标是转置关系的
    """
    with open(path, 'r') as annotation_file:
        # non_hit_count用来统计为被编码的标记的数量，用于预警
        non_hit_count = {}
        annotation = {}
        for x in json.load(annotation_file):
            study_uid = x['studyUid']

            assert len(x['data']) == 1, (study_uid, len(x['data']))
            data = x['data'][0]
            instance_uid = data['instanceUid']
            series_uid = data['seriesUid']

            assert len(data['annotation']) == 1, (study_uid, len(data['annotation']))
            points = data['annotation'][0]['data']['point']

            vertebra_label = torch.full([len(SPINAL_VERTEBRA_ID), 3],
                                        PADDING_VALUE, dtype=torch.long)
            disc_label = torch.full([len(SPINAL_DISC_ID), 3],
                                    PADDING_VALUE, dtype=torch.long)
            for point in points:
                identification = point['tag']['identification']
                if identification in SPINAL_VERTEBRA_ID:
                    position = SPINAL_VERTEBRA_ID[identification]
                    diseases = point['tag']['vertebra']

                    vertebra_label[position, :2] = torch.tensor(point['coord'])
                    for disease in diseases.split(','):
                        if disease in SPINAL_VERTEBRA_DISEASE_ID:
                            disease = SPINAL_VERTEBRA_DISEASE_ID[disease]
                            vertebra_label[position, 2] = disease
                elif identification in SPINAL_DISC_ID:
                    position = SPINAL_DISC_ID[identification]
                    diseases = point['tag']['disc']

                    disc_label[position, :2] = torch.tensor(point['coord'])
                    for disease in diseases.split(','):
                        if disease in SPINAL_DISC_DISEASE_ID:
                            disease = SPINAL_DISC_DISEASE_ID[disease]
                            disc_label[position, 2] = disease
                elif identification in non_hit_count:
                    non_hit_count[identification] += 1
                else:
                    non_hit_count[identification] = 1

            annotation[study_uid, series_uid, instance_uid] = vertebra_label, disc_label
    if len(non_hit_count) > 0:
        print(non_hit_count)
    return annotation

def resize(size: Tuple[int, int], image: Image.Image, spacing: torch.Tensor, *coords: torch.Tensor):
    """

    :param size: [height, width]，height对应纵坐标，width对应横坐标
    :param image: 图像
    :param spacing: 像素点间距
    :param coords: 标注是图像上的坐标，[[横坐标,纵坐标]]，横坐标从左到有，纵坐标从上到下
    :return: resize之后的image，spacing，annotation
    """
    # image.size是[width, height]
    height_ratio = size[0] / image.size[1]
    width_ratio = size[1] / image.size[0]

    ratio = torch.tensor([width_ratio, height_ratio])
    spacing = spacing / ratio
    coords = [coord * ratio for coord in coords]
    image = tf.resize(image, size)

    output = [image, spacing] + coords
    return output

def rotate_point(points: torch.Tensor, angel, center: torch.Tensor) -> torch.Tensor:
    """
    将points绕着center顺时针旋转angel度
    :param points: size of（*， 2）
    :param angel:
    :param center: size of（2，）
    :return:
    """
    if angel == 0:
        return points
    angel = angel * math.pi / 180
    while len(center.shape) < len(points.shape):
        center = center.unsqueeze(0)
    cos = math.cos(angel)
    sin = math.sin(angel)
    rotate_mat = torch.tensor([[cos, -sin], [sin, cos]], dtype=torch.float32, device=points.device)
    output = points - center
    output = torch.matmul(output, rotate_mat)
    return output + center

def rotate_batch(points: torch.Tensor, angels: torch.Tensor, centers: torch.Tensor) -> torch.Tensor:
    """
    将一个batch的点，按照不同的角度和中心转旋
    :param points: (num_batch, num_points, 2)
    :param angels: (num_batch,)
    :param centers: (num_batch, 2)
    :return:
    """
    centers = centers.unsqueeze(1)
    output = points - centers

    angels = angels * math.pi / 180
    cos = angels.cos()
    sin = angels.sin()
    rotate_mats = torch.stack([cos, sin, -sin, cos], dim=1).reshape(angels.shape[0], 1, 2, 2)
    output = output.unsqueeze(-1)
    output = output * rotate_mats
    output = output.sum(dim=-1)
    return output + centers

def rotate(image: Image.Image, points: torch.Tensor, angel: int) -> (Image.Image, torch.Tensor):
    center = torch.tensor(image.size, dtype=torch.float32) / 2
    return tf.rotate(image, angel), rotate_point(points, angel, center)

def gen_distmap(image: torch.Tensor, spacing: torch.Tensor, *gt_coords: torch.Tensor, angel=0):
    """
    先将每个像素点的坐标顺时针旋转angel之后，再计算到标注像素点的物理距离
    :param image: height * weight
    :param gt_coords: size of（*， 2）
    :param spacing:
    :param angel:
    :return:
    """
    coord = torch.where(image.squeeze() < np.inf)
    # 注意需要反转横纵坐标
    center = torch.tensor([image.shape[2], image.shape[1]], dtype=torch.float32) / 2
    coord = torch.stack(coord[::-1], dim=1).reshape(image.size(1), image.size(2), 2)
    coord = rotate_point(coord, angel, center)
    dists = []
    for gt_coord in gt_coords:
        gt_coord = rotate_point(gt_coord, angel, center)
        dist = []
        for point in gt_coord:
            dist.append((((coord - point) * spacing) ** 2).sum(dim=-1).sqrt())
        dist = torch.stack(dist, dim=0)
        dists.append(dist)
    if len(dists) == 1:
        return dists[0]
    else:
        return dists

def gen_mask(coord: torch.Tensor):
    return (coord.index_select(-1, torch.arange(2, device=coord.device)) != PADDING_VALUE).any(dim=-1)


from django.shortcuts import render
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseRedirect

from .utils import combine_multiple_filters_to_query, filtered_duplicate_by

# from data_utils import convert_unicode_to_none_for_array

from parameters import ALL_PLATFORMS

import json

from .forms import sampleFilterForm
from .forms import fileUploadForm

import pandas as pd
import numpy as np

from views_constants import *

# from mongo_read import ADDBClient

# db_client = ADDBClient()

# from Analysis.mongo_read import SampleClient, AnnotationClient
# db_client = SampleClient()
# connection with Redis
# import redis
# r = redis.StrictRedis(host='localhost', port=6379, db=0)

# Create your views here.

def index(request):
	return HttpResponse()

def home(request):
	return render(request, 'home.html')

# from numpy import *

def query(request):
	# db = client.addb
	# get extra field count
	extra_field_count = request.GET.get('extra_field_count')
	if extra_field_count is None:
		# if none set to zero
		extra_field_count = 0
	# initialize form
	form = sampleFilterForm(extra=extra_field_count)
	# query output for displaying
	output = []
	# Mongo query to be defined
	query = ''
	if request.method == 'POST' :
		form = sampleFilterForm(request.POST)
		if form.is_valid():
			filters = []
			# collect all user input filters into a filter list
			for i in range(int(extra_field_count) + 1):
				filters.append((request.POST["filterBy_" + str(i)], request.POST["relation_" + str(i)], request.POST["keyword_" + str(i)]))
			# import pdb; pdb.set_trace()
			# make query out of the filter collection
			query = combine_multiple_filters_to_query(filters)
			# query the database
			cursor = db_client.db['samples'].find(query, {"_id" : 0, "expression_value" : 0, "probe_id" : 0})

			# read and append each collection to output
			for doc in cursor:
				# import pdb; pdb.set_trace()
				output.append(doc)
			# result = json.dumps(resultJson, separators=(',', ':'))
				# result = data.find()
			# data = fs.find({"filename" : "gse5281.json.txt"}).read()
				# result = data.find({}, {"expression_value" : 0})
			# result = fs.find({}, {"expression_value" : -1})
			# fs = gridfs.GridFS(db)
			# a = fs.get(fs)
	return render(request, 'query.html', {
		'form' : form,
		'result' : output,
		'query' : query
		# 'redirectUrl' : redirectUrl
	})

import ast
from numpy import *
def featureQuery(request):
	# db = client.addb
	if request.method == 'POST':
		# import pdb; pdb.set_trace()
		# get gene list
		geneArr = request.POST['input-genes'].split('\r\n')
		ga = []
		for gene in geneArr :
			ga.append({"symbol" : gene})
		annoQuery = {"$or" : ga}
		query = request.POST['sample-query']
		query = eval(query)
		cursor = db_client.db['samples'].find(query, {"dataset_accession" : 1, "platform_name" : 1, "expression_value" : 1, "probe_id" : 1, "sample_accession" : 1})
		# import pdb; pdb.set_trace()
		datasets = cursor.distinct("dataset_accession")
		# platforms = cursor.distinct("platform_name")
		# probe_id_list = {}
		# probe_id_list = dict.fromkeys(datasets)
		# matched_probe_id_list = {}
		# matched_probe_id_list = dict.fromkeys(datasets)
		expression_table = {}
		expression_table = expression_table.fromkeys(datasets)
		# extract probe IDs of selected genes/features from anno collection
		annoCursor = list(db_client.db['anno'].find(annoQuery, {"_id" : 0, "entrez_gene_id" : 0}))
		annoProbeDict = {}
		annoProbeDict2 = {}
		# allkey = reduce(lambda x, y: set.union(set(x.keys()), set(y.keys())), annoCursor)

		# extract probe id array for each dataset
		for ds in datasets :
			# get a particular sample within this dataset
			sampleSampleInDataset = db_client.db['samples'].find({"dataset_accession" : ds}).next()
			# platform name of this dataset
			platform_name = sampleSampleInDataset["platform_name"][0]
			# probe id array of this dataset
			probeIdArrayInDataset = sampleSampleInDataset["probe_id"]
			# check which platform this dataset use by its platform_name
			platform = [x for x in ALL_PLATFORMS if x in platform_name.lower()][0]
			# probe ids for genes in interest in different dataset
			annoProbeDict.update({ds : {}})
			for geneEntry in annoCursor:
				matchedProbeArray = [g for g in geneEntry[platform] if g in probeIdArrayInDataset]
				annoProbeDict[ds].update({geneEntry["symbol"][0] : matchedProbeArray})
			## update first level dict
			annoProbeDict2.update({ds : {}})
			# update the probe ids array
			# matchedProbeArrayInDataset = [ele for k in annoProbeDict[ds].keys() for ele in annoProbeDict[ds][k] if ele in probeIdArrayInDataset]
			matchedProbeArrayInDataset = [probeIdArrayInDataset.index(ele) for k in annoProbeDict[ds].keys() for ele in annoProbeDict[ds][k] if ele in probeIdArrayInDataset]
			annoProbeDict2[ds].update({'probe' : matchedProbeArrayInDataset})
			# update the symbol array
			matchedSymbolArrayInDataset = [k for k in annoProbeDict[ds].keys() for ele in annoProbeDict[ds][k] if ele in probeIdArrayInDataset]
			annoProbeDict2[ds].update({'symbol' : matchedSymbolArrayInDataset})

		''' extract expression table from sample queries '''
		# old way to extract values and form a table

		for doc in cursor :
			dataset = doc["dataset_accession"][0]
			if len(annoProbeDict2[dataset]['probe']) != 0 :
				# import  pdb; pdb.set_trace()
				# deal with null
				exprsValWithoutNull = [None if type(x) is unicode else x for x in doc["expression_value"]]
				expressionArray = np.array(exprsValWithoutNull, dtype=np.float)
				if expression_table[dataset] is None :
					expression_table[dataset] = expressionArray[annoProbeDict2[dataset]['probe']] # not sure if this [] is needed
				else :
					expression_table[dataset] = np.vstack((expression_table[dataset], expressionArray[annoProbeDict2[dataset]['probe']])) # not sure if this [] is needed
		# 		# import  pdb; pdb.set_trace()
		# import  pdb; pdb.set_trace()

		# new way to do it
		# for doc in cursor :
		# 	dataset = doc["dataset_accession"][0]
		# 	if len(annoProbeDict2[dataset]['probe']) != 0 :
		# 		# deal with null
		# 		expression_value_without_null = convert_unicode_to_none_for_array(doc["expression_value"])

		# 		expression_array = pd.DataFrame([expression_value_without_null], columns=doc["probe_id"], index=doc['sample_accession'])
		# 		# expressionArray = np.array(expression_value_without_null, dtype=np.float)
		# 		if expression_table[dataset] is None :
		# 			expression_table[dataset] = expression_array[annoProbeDict2[dataset]['probe']] # not sure if this [] is needed
		# 		else :
		# 			# expression_table[dataset] = np.vstack((expression_table[dataset], expressionArray[annoProbeDict2[dataset]['probe']])) # not sure if this [] is needed
		# 			# expression_table[dataset] = expression_table[dataset].append(expression_array[annoProbeDict2[dataset]['probe']], columns=[expression_table[dataset].columns.values])
		# 			expression_table[dataset] = expression_table[dataset].append(expression_array[annoProbeDict2[dataset]['probe']])

		# import  pdb; pdb.set_trace()

	return render(request, 'featureQuery.html', {
		'expression_table' : expression_table
	})

def test(request):
	db = client.addb
	samp = db.samples.find({"_id" : '57248d25c6b9662864537ffe'}, {"age" : 1, "gender" : 1,"expression_value" : 1})
	# import  pdb; pdb.set_trace()
	return HttpResponse(dir(samp))
def upload_handler(request):
	if request.method == 'POST':
		# return HttpResponseRedirect('..')
		filename = request.FILES['file']['name']
		return HttpResponse(filename)
		# if(file.multiple_chunks()){
		# 	data = file.chunks
		# }
  #       data = file.read()
  #      	return HttpResponse(data)
        # update_database(data)
		# form = UploadFileForm(reuqest.POST, request.FILES)
		# if form.is_valid():
			# return HttpResponseRedirect('/index')
	else:
		# form = UploadFileForm()
		return render(request, 'read_data.html', {
		'message' : 'Upload not successful'
		})

def queried_feature_stat(request):
	"""
		This page is to display query feature statistics
	"""
	# Presumed variables
	collection_name = "RNA_brain_gender-F_AD-vs-Control"
	feature_symbols_in_interest = ['APOE', 'BIN1', 'CLU']
	way_to_choose_probe = "fold change"

	all_datasets = test_stat_client.get_all_datasets(collection_name)

	test_stat_output_dict = {}

	for dataset in all_datasets:
		test_statistics = test_stat_client.get_one_dataset_record(collection_name, dataset)
		# remove unnecessary info
		test_statistics.pop('dataset_accession', 0)
		test_statistics.pop('sample_count', 0)

		test_stat_df = pd.DataFrame(test_statistics)

		# count probes
		probe_count = test_stat_df.shape[0]
		# get probe names
		probe_names = list(test_stat_df.index)

		probe_in_interest = [probe_names[i] for i in range(0, probe_count) if test_stat_df.symb[i] in feature_symbols_in_interest]
		# Filter out those un-queried probes
		test_stat_df = test_stat_df.loc[probe_in_interest]

		"""
			Here we provide options for user to choose how to select a probe when
			multiple probes are corresponding to one feature
		"""

		if way_to_choose_probe == "fold change":
			test_stat_df = filtered_duplicate_by(test_stat_df, 'fc')

		elif way_to_choose_probe == "limma p value" :
			test_stat_df = filtered_duplicate_by(test_stat_df, 'lp')

		elif way_to_choose_probe == "t test p value" :
			test_stat_df = filtered_duplicate_by(test_stat_df, 'tp')

		# Convert pd.dataframe to dict with every probe id as keys
		test_stat = test_stat_df.to_dict(orient='index')

		test_stat_output_dict.update({dataset : test_stat})

	return render_template('feature_stat.html',
							test_stat=test_stat_output_dict)


from tkinter import *
from tkinter import PhotoImage
from PIL import Image,ImageTk

def set():
    print("Hello World")

root = Tk()
root.geometry("200x150")
imge=ImageTk.PhotoImage(Image.open("capital_anime/asta.jpg"))
frame = Frame(root)
frame.pack()
button = Button(frame, text="Button1",relief=RIDGE,image=imge, command=set,activebackground="blue",activeforeground="orange",justify=RIGHT,width=30,state=NORMAL,pady=20,padx=10,bg="grey",fg="brown",font="Helvetica 20 bold")
button.pack(pady=20)

root.mainloop()


import sys, json, ast, types, traceback, pathlib
import importlib.util

DOTFILE = pathlib.Path('~/.pq').expanduser().absolute()
SETTINGS = {}
PLUGINS = {}

if DOTFILE.exists():
    try:
        spec = importlib.util.spec_from_loader(
            'plugins', importlib.machinery.SourceFileLoader('plugins', str(DOTFILE))
        )
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        if hasattr(module, '__settings__'):
            SETTINGS = module.__settings__
        PLUGINS = {
            key : val for key, val in module.__dict__.items()
            if not key.startswith('__') and not key.endswith('__')
        }
    except Exception as e:
        print('Error loading dotfile:', e)

class Query:
    "Base class for all queries"
    def __init__(self, source):
        self.source = source

    def __call__(self, data):
        return data

    def __repr__(self):
        return str(self)

    def __str__(self):
        return f'<{self.__class__.__name__}: {repr(self.source)}>'

    def __eq__(self, other):
        return self.source == other.source

    def __ne__(self, other):
        return not self.__eq__(other)

    def __hash__(self):
        return hash(self.source)

class SelectKey(Query):  # some-key | `some-key` | some key
    "Narrow down data"

    def __call__(self, data):
        if (
            self.source not in data and
            isinstance(self.source, str) and
            self.source.lstrip('+-').isdigit()
        ):
            raise TypeError(
                f'Cannot index object with int: {self.source}. '
                f'Use array syntax instead: [{self.source}]'
            )
        else:
            try:
                return data[self.source]
            except Exception as e:
                message = getattr(e, 'message', None)

                if message:
                    print(f'{e.__class__.__name__}: {message}')
                else:
                    print(repr(e))
                sys.exit()

class BuildObject(Query):  # {}
    "Filter or enhance data"

    def __init__(self, source):
        Query.__init__(self, source)
        source_queries = source[:]
        indices = []

        while len(source_queries) > 2:
            if source_queries[1] == ':':
                key, _, val = [source_queries.pop(0) for i in range(3)]
                indices.append((key, val))
            else:
                indices.append(source_queries.pop(0))

        indices.extend(source_queries)
        self.indices = indices

    def __call__(self, data):
        result = {}
        for query in self.indices:
            if query.isidentifier():
                result[query] = data[query]

            elif isinstance(query, tuple):
                key, val = query
                result[Expression(key)(data)] = Expression(val)(data)

            else:
                evaluated = Expression(query)(data)
                if evaluated in data:
                    result[evaluated] = data[evaluated]
                else:
                    result[evaluated] = evaluated

        return result

class Index(Query):  # []
    "Index an object or an array"

    def __init__(self, source):
        Query.__init__(self, source)
        if ':' in source:
            self.index = slice(
                *[int(i) if i else None for i in source.split(':')]
            )
        elif source in '*!':
            self.index = source
        else:
            self.index = int(source)

    def __call__(self, data):
        return data[self.index]

class Expression(Query):  # ()
    "Query an object using a Python expression"

    def __call__(self, data):
        env = (
            {name : value for name, value in data.items()}
            if isinstance(data, dict) else {}
        )
        env.update(PLUGINS)
        env.update(IT=data)

        data = eval(self.source, env)
        return data

class Fanout(Query):  # [*]
    "Process each element in the current item with the next query set."

class Select(Query):  # [-]
    "Remove any value from array that doesn't evaluate to True"

class Join(Query):  # [!]
    "Join a previously entered Fanout command into a List."

def parse_query_string(query: str) -> list:
    "Parses out each query string into its own string"

    DOT, PAREN, SQUARE, BRACE, KEY = 'DOT PAREN SQUARE BRACE KEY'.split()
    commands = []
    state = DOT
    buffer = ''
    brace_key_list = []
    query_it = iter(query)

    for i in query_it:
        if state == DOT:
            if i == '.':
                continue
            elif i == '(':
                state = PAREN
            elif i == '[':
                state = SQUARE
            elif i == '{':
                state = BRACE
            else:
                buffer += i
                state = KEY

        elif state == PAREN:
            if i == ')' and is_valid_python_code(buffer.strip()):
                commands.append(Expression(buffer.strip()))
                buffer = ''
                state = DOT
            else:
                buffer += i

        elif state == SQUARE:
            stripped = buffer.strip()
            if i == ']':
                if (is_valid_python_code(stripped) or stripped in '*!-'):
                    if stripped == '*':
                        commands.append(Fanout(stripped))
                    elif stripped == '!':
                        commands.append(Join(stripped))
                    elif stripped == '-':
                        commands.append(Select(stripped))
                    else:
                        commands.append(Index(stripped))
                    buffer = ''
                    state = DOT
                elif ':' in stripped:
                    try:
                        commands.append(Index(stripped))
                        buffer = ''
                        state = DOT
                    except Exception as e:
                        print('----->', e)
                        raise SyntaxError(stripped)
                else:
                    raise SyntaxError(stripped)
            else:
                buffer += i

        elif state == BRACE:
            if i in '},:' and is_valid_python_code(buffer.strip()):
                brace_key_list.append(buffer.strip())
                buffer = ''
                if i == ':':
                    brace_key_list.append(':')
                elif i == '}':
                    commands.append(BuildObject(brace_key_list[:]))
                    brace_key_list.clear()
                    state = DOT
            else:
                buffer += i

        elif state == KEY:
            if i == '\\':
                buffer += next(query_it)
            elif i == '.':
                commands.append(SelectKey(buffer))
                buffer = ''
                state = DOT
            else:
                buffer += i

    # It is possible to be in the KEY state after loop exit
    if state == KEY:
        commands.append(SelectKey(buffer))

    elif state == DOT:
        "That's ok too"

    # But not any other state
    else:
        raise SyntaxError(
            f'Failed to finish parsing {state} - Incomplete query:\n\n'
            f'    {query}\n'
            f'    {" " * (len(query) - len(buffer))}^\n'
            f'    {" " * (len(query) - len(buffer))}|\n'
        )

    return commands

def form_query_groups(queries):
    groups, group = [[]], 0

    for query in queries:
        if isinstance(query, (Fanout, Join, Select)):
            groups.extend([query, []])

        else:
            groups[-1].append(query)

    return groups

def run_query_group(data, queries):
    for query in queries:
        data = query(data)
    return data

def process_queries(data, groups):
    groups_it = iter(groups)

    for group in groups_it:
        if isinstance(group, Fanout):
            next_group = next(groups_it)
            data = [run_query_group(item, next_group) for item in data]
            continue

        elif isinstance(group, Join):
            data = run_query_group(data, next(groups_it))
            continue

        elif isinstance(group, Select):
            next_group, result = next(groups_it), []
            data = [item for item in data if run_query_group(item, next_group)]
            continue

        else:
            data = run_query_group(data, group)

    return data

def is_valid_python_code(code: str) -> bool:
    try:
        ast.parse(code)
        return True
    except SyntaxError:
        return False

def output_highlighted_json(json_data, color=True, theme: str=None):
    "Print highlighted JSON to the console except when in a pipe."

    formatted_json = json.dumps(json_data, indent=SETTINGS.get('indent', 4))

    # If in pipe, don't print console colors, just print text
    if color and sys.stdout.isatty():
        if sys.platform == 'win32':
            import colorama
            colorama.init()

        from pygments import highlight
        from pygments.lexers import JsonLexer
        from pygments.formatters import Terminal256Formatter
        from pique import themes

        theme_list = themes.__dict__
        theme = theme or 'Python3'

        if (theme not in theme_list or
            not issubclass(theme_list[theme], themes.Style)
        ):
            raise Exception(f'No theme named: {theme}')

        color_theme = theme_list[theme]

        formatted_json = highlight(
            formatted_json,
            JsonLexer(),
            Terminal256Formatter(style=color_theme)
        )

    print(formatted_json)

def query(json_data, query_string):
    "Convenience function to use Pique like a library."
    return process_queries(
        json_data, form_query_groups(parse_query_string(query_string))
    )

def main(args: list=[]) -> int:
    "Run pq to query JSON data from CLI"

    args = args or sys.argv[1:]

    from pique.cli import parser

    if len(args) == 0 and sys.stdin.isatty():
        parser.print_help()
        return 0

    elif '-v' in args or '--version' in args:
        parser.parse_args(args)

    elif sys.stdin.isatty():
        print('No JSON data to read from pipe. Exiting.')
        return 0

    cli = parser.parse_args(args)

    try:
        commands = parse_query_string(cli.query)
    except SyntaxError as e:
        print(f'{e.__class__.__name__}: {e}')
        return 1

    try:
        json_data = json.loads(sys.stdin.read())
    except json.JSONDecodeError:
        print('Error reading JSON data. Is it formatted properly and complete?')
        return 1

    if cli.debug:
        print('---------------------')
        print(' Query string:\n')
        print('    ', cli.query, '\n')
        print('[')
        for c in commands:
            print('   ', c)
        print(']\n')
        print('Output:\n')

    try:
        json_data = process_queries(json_data, form_query_groups(commands))
    except Exception as e:
        if cli.debug:
            traceback.print_exc()
        else:
            print(f'{e.__class__.__name__}: {e}')
        return 1

    # NOTE(pebaz): `theme` can be None. `output_highlighted_json` uses Python3.
    theme = cli.theme or SETTINGS.get('theme')

    output_highlighted_json(json_data, cli.nocolor, theme)

    return 0

if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv[1:]))
    except KeyboardInterrupt:
        pass


#inputと回路から出力の論理状態を整数型リストで返す
import copy
import const

def logical_state(init_state,circuit):
    #ゲートがないならinput_listを返す
    if(len(circuit) == 0):
        return init_state

    d = len(circuit)
    n = len(circuit[0])
    x = copy.copy(init_state)

    for i in range(d):
        c_bit = -1;
        #iゲート目のコントロールビットを見つける
        if "c" not in circuit[i]:
            continue

        for j in range(n):
            if(circuit[i][j] == const.CONTROLL_BIT):
                c_bit = j
        #iゲート目のターゲットビットにコントロールビットの論理状態をxorする
        for j in range(n):
            if(circuit[i][j] == const.TARGET_BIT):
                x[j] = x[c_bit] ^ x[j]

    return x


class NotGate:
    @staticmethod
    def perform_not(input_bit):
        """
        Performs the NOT operation on a binary digit.

        Args:
        - input_bit (str): The binary digit ('0' or '1').

        Raises:
        - ValueError: If the input is not a binary digit.

        Returns:
        str: The result of the NOT operation.
        """
        if input_bit not in ["0", "1"]:
            raise ValueError("Input must be a binary digit (0 or 1)")

        if input_bit == "0":
            return "1"
        else:
            return "0"


import os
import pickle
from recorder import WaveRecorder
import soundfile as sf
import numpy as np

rec = WaveRecorder()

clf = pickle.load(open('VoiceClassification/model/several_model_add_none_class.sav','rb'))

yesno = ['No','Yes','None','Clap']

def predict():
    rec.record('output.wav')
    wav, _ = sf.read('output.wav')
    wav = np.array(wav[:, 0])
    wf = np.fft.fft(wav)
    wav = np.hstack((wf.real**2 + wf.imag**2, np.arctan2(wf.real, wf.imag)))
    pred = clf.predict(np.array([wav]))
    print("result:",yesno[int(pred)])
    return yesno[int(pred)]

if __name__ == "__main__":
    while True:
        result = predict()
        with open("VoiceClassification/result.txt", mode='w') as f:
            f.write(result)


fruits = ['pomegranate', 'cherry', 'apricot', 'apple',
'lemon', 'kiwi', 'orange', 'lime', 'watermelon', 'guava',
'papaya', 'fig', 'pear', 'banana', 'tamarind', 'persimmon',
'elderberry', 'peach', 'blueberry', 'lychee', 'grape', 'date' ]

f0 = []
for f in fruits:
    if f.startswith('p'):
        value = f.upper()
        f0.append(value)
print(f"f0: {f0}\n")

#    [value     for f in fruits if ... ]
f1 = [f.upper() for f in fruits if f.startswith('p')]
print(f"f1: {f1}\n")

f2 = [f.upper() for f in fruits]
print(f"f2: {f2}\n")

f3 = [f for f in fruits if f.startswith('a')]
print(f"f3: {f3}\n")

people = [
    ('Melinda', 'Gates', 'Gates Foundation', '1964-08-15'),
    ('Steve', 'Jobs', 'Apple', '1955-02-24'),
    ('Larry', 'Wall', 'Perl', '1954-09-27'),
    ('Paul', 'Allen', 'Microsoft', '1953-01-21'),
    ('Larry', 'Ellison', 'Oracle', '1944-08-17'),
    ('Bill', 'Gates', 'Microsoft', '1955-10-28'),
    ('Thomas', 'Kurtz', 'BASIC', '1928-02-28'),
    ('Ada', 'Lovelace', 'Analytical Engine', '1815-12-10'),
    ('Grace', 'Hopper', 'COBOL', '1906-12-09'),
    ('Mark', 'Zuckerberg', 'Facebook', '1984-05-14'),
    ('Sergey','Brin', 'Google', '1973-08-21'),
    ('Larry', 'Page', 'Google', '1973-03-26'),
    ('Linus', 'Torvalds', 'Linux', '1969-12-28'),
]

dobs = [p[-1] for p in people]
print(f"dobs: {dobs}\n")

nums = [800, 80, 1000, 32, -4, 255, -8, 400, 5, 5000]

n1 = [n / 100 for n in nums if n > 300]
print(f"n1: {n1}\n")

ngen = (n / 100 for n in nums if n > 300)
print(f"ngen: {ngen}")
for value in ngen:
    print(value)
print()


import os
import sys
import argparse
import glob
import pprint

import pandas as pd

def main(args):
    metadataDF = pd.read_csv(args.gtdb_metadata, header=0, low_memory=False)
    metadataDF["accession"] = metadataDF["accession"].str.replace("GB_", "").str.replace("RS_", "")
    # read in info about version errors encountered during downloads
    version_err = pd.read_csv(args.version_errors_log, header=None, names = ["accession", "prev_version", "full_name"])
    verrD = pd.Series(version_err["prev_version"].values,index=version_err["accession"]).to_dict()
   # replace accessions to new accs
    metadataDF["accession"].replace(verrD, inplace=True)
    import pdb;pdb.set_trace()
    metadataDF.to_csv(args.output_csv, index=False)

def cmdline(sys_args):
    "Command line entry point w/argparse action."
    p = argparse.ArgumentParser()
    p.add_argument("--gtdb-metadata",  default = "/group/ctbrowngrp/gtdb/gtdb-rs202.metadata.csv.gz")
    p.add_argument("--version-errors-log", default="/home/ntpierce/sourmash_databases/output.gtdb-databases/accession_version_errors.csv")
    p.add_argument("--output-csv", default="/group/ctbrowngrp/gtdb/gtdb-rs202.metadata.v2.csv.gz")
    args = p.parse_args()
    return main(args)

if __name__ == '__main__':
    returncode = cmdline(sys.argv[1:])
    sys.exit(returncode)


import numpy as np
from numpy.linalg import norm

import math, random, queue

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

class SphericalCalibration:

    def __init__(self, scaleFactor = np.matrix([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
                       bias = np.array([0.0, 0.0, 0.0]), filterSize = 10):
        self.scaleFactor = scaleFactor
        self.bias = bias
        self.sphere = []
        self.filterSize = filterSize
        self.fibonacci_sphere(20, False)
        #fig = plt.figure()
        #self.ax = fig.add_subplot(111, projection='3d')
        #self.ax.set_aspect('equal')
        #plt.ion()
        #plt.show()
        #self.plotSphere()

    def __str__(self):
        return str(self.sphere)

    def status(self):
        status =  []
        for point in self.sphere:
            status.append(len(point['fifo']))
        return status

    def isCalibrated(self):
        isCalibrated = True
        for point in self.sphere:
            isCalibrated = point['fifo'].isFull
        return isCalibrated

    def reset():
        for point in self.sphere:
            point['fifo'].reset()

    def pushData(self, vector):
        maxVal = 0.0
        try:
            maxVal = float('-inf')
        except:  # check for a particular exception here?
            maxVal = -1e30000
        maxIdx = -1
        # Normlalize input vector
        normVect = vector/norm(vector)
        for idx, point in enumerate(self.sphere):
            val = np.dot(np.matrix([point['point']]), normVect)
            if val > maxVal:
                maxVal = val
                maxIdx = idx
        if maxIdx != -1:
            alreadyFull = self.sphere[maxIdx]['fifo'].isFull
            self.sphere[maxIdx]['fifo'].put(vector)

            #if self.sphere[maxIdx]['fifo'].isFull and not alreadyFull:
                #self.ax.scatter(self.sphere[maxIdx]['point'][0], self.sphere[maxIdx]['point'][1], self.sphere[maxIdx]['point'][2], c='b')
                #plt.draw()
                #plt.pause(0.001)
                #print(str(maxIdx) + "th fifo is full")

    def fibonacci_sphere(self, samples=1,randomize=True):
        rnd = 1.
        if randomize:
            rnd = random.random() * samples

        offset = 2./samples
        increment = math.pi * (3. - math.sqrt(5.));

        for i in range(samples):
            y = ((i * offset) - 1) + (offset / 2);
            r = math.sqrt(1 - pow(y,2))

            phi = ((i + rnd) % samples) * increment

            x = math.cos(phi) * r
            z = math.sin(phi) * r

            self.sphere.append({'point': [x,y,z], 'fifo': Filter(self.filterSize)})

    def plotSphere(self):
        for point in self.sphere:
            self.ax.scatter(point['point'][0], point['point'][1], point['point'][2], c='r')
            plt.draw()
        plt.pause(0.001)

class LinearCalibration:

    def __init__(self, scaleFactor = np.matrix([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
                   bias = np.array([0.0, 0.0, 0.0]), filterSize = 10):
        self.filter = Filter(filterSize)
        self.scaleFactor = scaleFactor
        self.bias = bias

    def reset(self):
        self.filter.reset()

    def pushData(self, vector):
        self.filter.put(vector)
        self.bias = self.filter.mean().flatten()

    def status(self):
        return len(self.filter)

    def isCalibrated(self):
        return self.filter.isFull

class Filter:

    def __init__(self, maxSize=1):
        self.maxSize = maxSize
        self.fifo = np.zeros((1,3), dtype=float)
        self.currentIndex=0
        self.isFull = False

    def __str__(self):
        return str(self.fifo)

    def __len__(self):
        return len(self.fifo)

    def reset(self):
        self.fifo = np.zeros((1,3), dtype=float)
        self.currentIndex=0
        self.isFull = False

    def put(self, vector):
        if not self.isFull: # Resize the FIFO until it reach its maximum size
            self.fifo.resize((self.currentIndex+1,3), refcheck=False)
        # Update the older vector by the new one
        self.fifo[self.currentIndex] = vector
        # Loop on matrix line
        self.currentIndex = (self.currentIndex + 1) % self.maxSize
        if self.currentIndex == 0:
            self.isFull = True

    # Return a vector containing the mean of each matrix row
    def mean(self):
        return self.fifo.mean(0)

if __name__ == '__main__':
    cal = Calibration()
    cal.fibonacci_sphere(20, False)
    #cal.plotSphere()


import soundDownload as sd
import myFreesoundConfig as conf
import time

def download(testMode=False):

    params = {
        'tag' : '',
        'duration' : (0,10),
        'API_Key' : conf.myAPI_Key,
        'outputDir' : conf.myOutputDir,
        'topNResults' : 3,
        'featureExt' : '.json',
        'preview' : False,
        'emptyDir' : False,
        'pack' : '',
        'freeSoundId' : '',
        'noDownload': False}

    params['noDownload'] = testMode

    if testMode:
        params['topNResults'] = 1

    params['folderName'] = 'mridangam'

    nameRange = ["c", "d", "e", "f", "g", "a", "b"]
    extRange = ["", "sh"]
    soundRange = ["ta", "tha", "tham", "thi", "thom", "cha", "dhin", "dheem", "bheem", "num"]

    notes = ["%s-%s%s" % (sound, name, ext) for sound in soundRange for name in nameRange for ext in extRange]
    for note in notes:
        params['tag'] = "(mridangam-%s mridangam-stroke-dataset)" % (note, )
        sd.downloadFreesound(**params)
        time.sleep(1)

#download(testMode=True)


from abc import abstractmethod
from pathlib import Path
from typing import IO, Generator
from django.shortcuts import get_object_or_404

from .models import Video

class BaseStream:

    @abstractmethod
    def ranged(self): pass

    @abstractmethod
    def open_file(self): pass

class VideoStream(BaseStream):

    def ranged(self,
        file: IO[bytes],
        start: int = 0,
        end: int = None,
        block_size: int = 8192, # bytes for returne partly
    ) -> Generator[bytes, None, None]:
        consumed = 0 # start point for video

        # Read video file in bytes
        file.seek(start)
        while True:
            data_length = min(block_size, end - start - consumed) if end else block_size
            if data_length <= 0:
                break
            data = file.read(data_length)
            if not data:
                break
            consumed += data_length
            yield data

        if hasattr(file, 'close'):
            file.close()

    def open_file(self, request, video_pk: int) -> tuple:
        # Get object
        _video = get_object_or_404(Video, pk=video_pk)

        #Get path of videofile
        path = Path(_video.file.path)

        # Read videofile and take all size of video in bytes
        file = path.open('rb')
        file_size = path.stat().st_size

        # find video length and video during range from headers
        content_length = file_size
        status_code = 200
        content_range = request.headers.get('range')

        # Find exactly video length in bytes
        if content_range is not None:
            content_ranges = content_range.strip().lower().split('=')[-1]
            range_start, range_end, *_ = map(str.strip, (content_ranges + '-').split('-'))
            range_start = max(0, int(range_start)) if range_start else 0
            range_end = min(file_size - 1, int(range_end)) if range_end else file_size - 1
            content_length = (range_end - range_start) + 1
            file = self.ranged(file, start=range_start, end=range_end + 1)
            status_code = 206
            content_range = f'bytes {range_start}-{range_end}/{file_size}'

        return file, status_code, content_length, content_range


import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()
n, s, m = map(int, input().split())
vols = [0] + list(map(int, input().split()))
d = [[False for _ in range(1001)] for _ in range(101)]
d[0][s] = True
for i in range(1, n+1):
    v = vols[i]
    for j in range(m+1):
        if d[i-1][j] and 0 <= j-v <= m:
            d[i][j-v] = True
        if d[i-1][j] and 0 <= j+v <= m:
            d[i][j+v] = True
for i in range(m, -1, -1):
    if d[n][i]:
        print(i)
        exit(0)
print(-1)


import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)

face_classifier = cv.CascadeClassifier("haarcascade_files/haar-cascade-files-master/haarcascade_frontalface_default.xml")

while True:
    ret, frame = cap.read()

    image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)

    faces = face_classifier.detectMultiScale(image, 1.3, 5)

    for (x,y,w,h) in faces:
        cv.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 255), 5)

    cv.imshow("faces", frame)

    if cv.waitKey(25) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()


import glob
import os.path
import cv2
import numpy as np
import matplotlib
from matplotlib.pyplot import  imshow, show
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm

import datetime
from PIL import Image

import numpy as np
from skimage.transform import resize

import pandas as pd

import os
from skimage.transform import resize
from skimage.io import imsave
import numpy as np

def grays_to_RGB(img):
    ''' turn 2D grayscale image into grayscale RGB '''
    return np.dstack((img, img, img))

def image_with_mask(img, mask):
    ''' to add the mask on the image with outer layer in green '''
    img = grays_to_RGB(img)
    mask_edges = cv2.Canny(mask, 100, 200) > 0
    img[mask_edges, 0] = 255
    img[mask_edges, 1] = 0
    img[mask_edges, 2] = 0
    return img

def predict_output_plot(classification_model,double_unet, imgs_test ):

    """
    Function to visualize the predicted mask over the original image.
    """
    classification_model.load_weights('/content/drive/MyDrive/Self_case_study/SC2/Classification_model_weights.h5')
    double_unet.load_weights('/content/drive/MyDrive/Self_case_study/SC2/Double_UNET_model_weights.h5')

    predictions=classification_model.predict(imgs_test)

    for i in range(len(predictions)):
        if predictions[i]>=0.5:
            predictions[i]=1
        else:
            predictions[i]=0

    print("Completed predictions processing")

    count=0
    for i in range(len(predictions)):

        if predictions[i]==1:

            pred_mask=np.asarray(double_unet.predict(np.asarray(imgs_test[i].reshape( 1, 96,96,3 )))).reshape(96,96,1)
            pred_mask=((pred_mask[:,:,0]*255.).astype(np.uint8))
            img_test=((imgs_test[i][:,:,0]).astype(np.uint8))

            imshow(image_with_mask(img_test, pred_mask))
            show()
            if(count==10):
                break;
            count+=1

def return_for_deployment(classification_model,double_unet, imgs_test ):

    """
    Function to visualize the predicted mask over the original image.
    """
    classification_model.load_weights('/content/drive/MyDrive/Self_case_study/SC2/Classification_model_weights.h5')
    double_unet.load_weights('/content/drive/MyDrive/Self_case_study/SC2/Double_UNET_model_weights.h5')

    predictions=classification_model.predict(imgs_test)

    for i in range(len(predictions)):
        if predictions[i]>=0.5:
            predictions[i]=1
        else:
            predictions[i]=0

    count=0
    for i in range(len(predictions)):

        if predictions[i]==1:

            pred_mask=np.asarray(double_unet.predict(np.asarray(imgs_test[i].reshape( 1, 96,96,3 )))).reshape(96,96,1)
            pred_mask=((pred_mask[:,:,0]*255.).astype(np.uint8))
            img_test=((imgs_test[i][:,:,0]).astype(np.uint8))

            img= image_with_mask(img_test, pred_mask)

            img = img.astype('float32')
            img = (img > 0.5)

            img = resize(img, (256, 256), preserve_range=True)

            #img = resize(img, (580, 420), preserve_range=True)

            return img

        else:

            return "Empty"


import numpy
import matplotlib.pyplot as plt

# open the CSV file and read its contents into a list
data_file = open("data/mnist_train_100.csv", 'r')
data_list = data_file.readlines()
data_file.close()

# print(len(data_list))
# print(data_list[0])

# take the data from a record rearrange it into a 28*28 array and plot it as an image
all_values = data_list[0].split(',')
image_array = numpy.asfarray(all_values[1:]).reshape(28, 28)
plt.imshow(image_array, cmap='Greys', interpolation='None')
plt.show()

scaled_input = (numpy.asfarray(all_values[1:]) / 255.0 * 0.09) + 0.01
print(scaled_input)

# output nodes is 10(example)
onodes = 10
targets = numpy.zeros(onodes) + 0.01
targets[int(all_values[0])] = 0.99
print(targets)


from os import *
from sys import *
from collections import *
from math import *

'''
  ----Binary tree node class for reference-----
    class TreeNode:
        def __init__(self, data):
            self.data = data
            self.left = None
            self.right = None

'''

def getMaxWidth(root):
  if root == None :
    return 0
  q = deque()
  q.append(root)
  ans = 1
  while len(q) > 0 :
    n = len(q)
    if n > ans :
      ans = n
    #print(n)
    for i in range(n) :
      node = q.popleft()
      if node.left :
        q.append(node.left)
      if node.right :
        q.append(node.right)
  return ans


import tensorflow as tf
import heapq
import threading
import os
from text_config import TextConfig
from loader import *
from tokenizer import Tokenizer
from w2v_dal import W2VDAL

class PredictorFactory:
    _instance_lock = threading.Lock()

    def __init__(self):
        self.predictor_dict = {}

    def __new__(cls, *args, **kwargs):
        if not hasattr(PredictorFactory, "_instance"):
            with PredictorFactory._instance_lock:
                if not hasattr(PredictorFactory, "_instance"):
                    PredictorFactory._instance = object.__new__(cls)
        return PredictorFactory._instance

    def get_predictor(self, model_name):
        if model_name in self.predictor_dict.keys():
            return self.predictor_dict[model_name]
        else:
            self.predictor_dict[model_name] = Predictor(model_name)
            return self.predictor_dict[model_name]

class Predictor:
    def __init__(self, model_name):
        # 恢复中间文件
        self.config = TextConfig()
        prev = './data/' + model_name + '/' + model_name
        try:
            with open(prev + self.config.word2id_filename, "rb") as f:
                self.word2id = pickle.load(f)
            with open(prev + self.config.ctg2id_filename, "rb") as f:
                ctg2id = pickle.load(f)
                self.id2ctg = {v: k for k, v in ctg2id.items()}
        except Exception as e:
            print(e)

        # 加载各种词典
        user_dict_path = prev + self.config.user_dict_filename
        stop_word_path = prev + self.config.stop_word_filename
        self.tokenizer = Tokenizer(user_dict_path=user_dict_path,
                                   stop_words_path=stop_word_path,
                                   use_single_char=False)

        if self.config.embedding_as_input:
            from embedding_loader import EmbeddingLoader
            word2vec_save_path = './data/' + model_name + '/word2vec.txt'

            w2v_dal = W2VDAL()
            embedding_loader = EmbeddingLoader(word2vec_save_path, w2v_dal=w2v_dal)
            self.config.embedding_loader = embedding_loader

        # 恢复模型
        save_path = './checkpoints/' + model_name + '/' + model_name
        tf_config = tf.ConfigProto(allow_soft_placement=True)
        tf_config.gpu_options.allow_growth = True
        self.session = tf.Session(config=tf_config)
        self.session.run(tf.global_variables_initializer())

        if os.path.exists(save_path + '.meta'):
            saver = tf.train.import_meta_graph(save_path + '.meta')
            saver.restore(self.session, save_path)
        else:
            raise RuntimeError("model file does not exist")
        self.model_name = model_name

    def predict(self, sentence, top=1):
        # 输入预处理
        if self.config.embedding_as_input:
            input_x = process_with_embedding(sentence,
                                             cat_to_id=None,
                                             max_length=self.config.seq_length,
                                             tokenizer=self.tokenizer,
                                             is_train=False,
                                             embedding_loader=self.config.embedding_loader)
        else:
            input_x = process_for_predict(sentence, self.word2id,
                                          max_length=self.config.seq_length, tokenizer=self.tokenizer)

        model_input = self.session.graph.get_tensor_by_name('input_x:0')
        dropout = self.session.graph.get_tensor_by_name('dropout:0')
        prob = self.session.graph.get_tensor_by_name('output/Softmax:0')

        feed_dict = {
            model_input: input_x,
            dropout: 1,
        }

        y_prob = self.session.run(prob, feed_dict=feed_dict)
        y_prob = y_prob.tolist()
        cat = []
        for prob in y_prob:
            # 列出排序最高的
            top_n_predict = list(map(prob.index, heapq.nlargest(top, prob)))
            top_n_acc = heapq.nlargest(top, prob)
            for i in range(0, top):
                cat.append(
                    {
                        'ctg_id': self.id2ctg[top_n_predict[i]],
                        'acc': top_n_acc[i]
                    })

        # TODO 应不应该在此reset
        tf.reset_default_graph()
        result = {
            'predict_list': cat,
        }
        return result

predictor_factory = PredictorFactory()

def predict_bi(sentence, top=1):
    model_list = ['accessory', 'dealer', 'trouble']
    for model in model_list:
        predictor = predictor_factory.get_predictor(model)
        predict_result = predictor.predict(sentence, top=top)
        print(sentence, ':', predict_result['predict_list'][0])
        if predict_result['predict_list'][0]['ctg_id'] == model:
            return model
    return 'unknown'

if __name__ == '__main__':
    # model_ = 'trouble-dealer-accessory'
    model_ = 'Salary'
    predictor_factory_ = PredictorFactory()
    predictor_ = predictor_factory_.get_predictor(model_)
    # print(predictor_.predict("2014社会保险一个月要交多少钱"))
    with open('test.txt','r',encoding='utf-8') as f:
        i = 0
        for line in f.readlines():
            line=line.replace('\r','').replace('\n','').split(' ')
            # if line:
                # print(line,predictor_.predict(line))
            # else:
                # print("*")
            r = predictor_.predict(line[1])
            if r['predict_list'][0]['ctg_id'] == line[0]:
                i+=1
        print(i/8983)


'''Write a Python function that takes a string s as input and returns the length of the largest streak of 0s in the string.
   For example, if the input string is "10001000110000000001", the function should return 6.'''

curr_len = 0
max_len = 0
count = 0
m = 0

s = (input())
for i in s:
    print(i,end=" ")
    if int(i)==0:

        count = count+1
        curr_len = count

        if(curr_len>max_len):
            max_len = curr_len
    else:
        count = 0
print(max_len)