"""
逻辑回归
"""

from sklearn.linear_model import LogisticRegression
from util.metrics_util import MetricsUtil
from sklearn.model_selection import GridSearchCV
from imblearn.combine import SMOTETomek
import pandas as pd

class LogisticRegUtil:

    """
    逻辑回归-不分训练集和测试集
    """
    @staticmethod
    def logit_reg_model_sample(X, y):
        logit_model = LogisticRegression()

        # 模型训练
        logit_model.fit(X, y)

        # 预测
        y_pre = logit_model.predict(X)

        # 评价指标
        metric_val = MetricsUtil.metrics_class(y, y_pre)

        # 参数
        coea = {}
        coea['intercept'] = logit_model.intercept_
        coea['param'] = logit_model.coef_

        return logit_model, coea, metric_val

    """
    逻辑回归-训练集和测试集
    """
    @staticmethod
    def logit_reg_model(X_train, X_test, y_train, y_test):
        logit_model = LogisticRegression()

        # 模型训练
        logit_model.fit(X_train, y_train)

        # 预测
        y_pre = logit_model.predict(X_test)
        y_pre = pd.Series(y_pre)

        # 评价指标
        metric_val = MetricsUtil.metrics_class(y_test, y_pre)

        # 参数
        coea = {}
        coea['intercept'] = logit_model.intercept_
        coea['param'] = logit_model.coef_

        return logit_model, coea, metric_val

    """
    逻辑回归-网格搜索法
    """
    @staticmethod
    def logit_reg_model_grid(X, y):
        # 网格参数设置
        param = {
            'C':[0.01, 0.1, 0.5, 1, 2, 10],
            'max_iter':[50, 100, 150, 200, 200]
        }

        # 基础模型
        logit_model = LogisticRegression()

        # 模型训练
        gs_model = GridSearchCV(logit_model, param, cv = 10)
        gs_model.fit(X, y)

        # 最佳模型
        best_logit_model = gs_model.best_estimator_

        # 预测
        y_pre = best_logit_model.predict(X)

        # 评价指标
        metric_val = MetricsUtil.metrics_class(y, y_pre)

        # 参数
        coea = {}
        coea['intercept'] = best_logit_model.intercept_
        coea['param'] = best_logit_model.coef_

        return best_logit_model, coea, metric_val

    """
    不均衡采样
    """
    @staticmethod
    def SMOTETomek_sample(X,y):
        smote_tomek = SMOTETomek(random_state=1234)
        x_s, y_s = smote_tomek.fit_resample(X, y)

        return x_s, y_s

if __name__ == '__main__':
    pass


__author__ = 'Ruben Cuevas Menendez from https://coolprof.wordpress.com/tag/mongodb/'
# modified for my needs

import json
import tweepy
from pymongo import MongoClient

class StreamListener(tweepy.StreamListener):
    # tweepy.StreamListener is a class provided by tweepy used to access the Twitter Streaming API.
    # It allows us to retrieve tweets in real time.

    def on_connect(self):
        print("You're connected to the streaming server.")

    def on_error(self, status_code):
        print('Error: ' + repr(status_code))
        return False

    def on_data(self, data):
        # connect to Mongo Server
        client = MongoClient('localhost', 27017)

        # Use eve database created for Eve API
        db = client.eve

        # Decode JSON
        datajson = json.loads(data)

        # Only storing English tweets
        if "lang" in datajson and datajson["lang"] == "en":
            # Store tweet info into the tweets collection.
            # If no collection exists, it will be created.
            db.tweets.insert(datajson)

# This is a manually created filed where I stored my OAuth credentials for Twitter.
# Each line is a key-value pair of the form: KEY_NAME:KEY
# CREDENTIALS_PATH = 'C:/Users/Finbar/Desktop/twitter_keys/keys.txt' # Laptop
# CREDENTIALS_PATH = 'C:/Users/t00175569/Desktop/twitter_keys/keys.txt' # College
CREDENTIALS_PATH = '/Users/C_Train/Desktop/twitter_keys/keys.txt' # MAC

# Path to the list of keywords to search for (not case-sensitive).
# STOPWORDS_ES_PATH = 'C:/Users/Finbar/Desktop/twitter_keys/keywords.txt' # Laptop
# STOPWORDS_ES_PATH = 'C:/Users/t00175569/Desktop/twitter_keys/keywords.txt' # College
STOPWORDS_ES_PATH = '/Users/C_Train/Desktop/twitter_keys/keywords.txt' # MAC

CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

# Load credentials
with open(CREDENTIALS_PATH) as f:
    for line in f:
        line = line.rstrip('\r\n').split(":")
        if line[0] == "CONSUMER_KEY":
            CONSUMER_KEY = line[1]
        elif line[0] == "CONSUMER_SECRET":
            CONSUMER_SECRET = line[1]
        elif line[0] == "ACCESS_TOKEN":
            ACCESS_TOKEN = line[1]
        elif line[0] == "ACCESS_TOKEN_SECRET":
            ACCESS_TOKEN_SECRET = line[1]

# Authenticating
auth1 = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth1.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

l = StreamListener(api=tweepy.API(wait_on_rate_limit=True))
streamer = tweepy.Stream(auth=auth1, listener=l)
with open(STOPWORDS_ES_PATH) as f:
    streamer.filter(track=[word.strip().decode('utf-8') for word in f])


#About adding for the list

# 定义一个列表
# my_list = [1, 2, 3, 4, 2, 3, 2]

# 双击666
# my_list[2] = "双击666"
# print(my_list)

# <3>查找元素("查"in, not in, index, count)

# in
# if 2 in my_list:
#     print("存在")

# not in
# if 22 not in my_list:
#     print("不存在")

# index 查询元素在其列表的下标索引
# index = my_list.index(22)
# print(index)

# count 查询列表中某个元素的出现的次数
# count = my_list.count(22)
# print(count)

# 造成程序崩溃 -> 上线后 必须避免
# 如果这个元素存在 告知位置
# 如果不存在 告知就是不存在

# 解决方案01:
# if 2 in my_list:
#     index = my_list.index(2)
#     print(index)
# else:
#     print("不存在")

my_list = [1, 2, 3, 4, 2, 3, 2]

# 解决方案02
# python中 非零即真 零则假
# if my_list.count(2):
#     print("存在")
#     index = my_list.index(2)
#     print(index)
# else:
#     print("22不存在")

# 分析龟叔是如何做到的
# 定义一个变量记录下标
# i = 0
# while i < len(my_list):
#     # 对应的value
#     value = my_list[i]
#     if value == 2:
#         print(i)
#         break
#     i += 1

my_list = [1, 2, 3, 4]

# del 内置函数
# 格式: del 列表名[下标索引]
# del my_list[2]
# print(my_list)

# pop 默认会删除最后一个
# pop(下标索引) 而且有返回值的
# x = my_list.pop(1)
# print(x)
# print(my_list)

# remove
# my_list.remove(3)
# print(my_list)

# clear 清空列表
# my_list.clear()
# my_list = []

# print(my_list)

import random
# 我要定义一个列表 里面有10个元素
# 每个元素int类型 元素的大小[0, 200]之间

# 定义一个空列表
my_list = []
for i in range(10):
    value = random.randint(0,200)
    # 添加value到列表中
    my_list.append(value)

print(my_list)
# sort排序 -> 升序(数字从小到大)
# sort(reverse=False) == sort()
# my_list.sort()
# print(my_list)

# 降序(数字是从大到小)
# my_list.sort(reverse=False)
# print(my_list)

# 逆序 -> 字符串中[::-1]
# new_list = reversed(my_list)
# print(list(new_list))

schoolNames = [['北京大学', '清华大学'], ['南开大学', '天津大学', '天津师范大学'], ['山东大学', '中国海洋大学', "蓝翔大学"]]
# # 蓝翔大学
# my_list = schoolNames[2]
# # 获取大学['山东大学', '中国海洋大学', '蓝翔大学']
# name = my_list[2]
# print(name)

# 开发中常用的格式
# name = schoolNames[2][2][2]
# print(name)

# 一个学校，有3个办公室，现在有8位老师等待工位的分配，请编写程序，完成随机的分配

# [[], [], []]

# []
# [AEFCD]
# [BGH]
# ABCDEFGH
import random
# 定义一个列表保存老师
school = [[], [], []]

# 叫八位老师站好 等待分配
teacher_list = list("ABCDEFGH")
print(teacher_list)

# 遍历teacher_list
for name in teacher_list:
    # 产生随机索引
    index = random.randint(0,2)
    school[index].append(name)
    # 随机分配办公室

print(school)


#creating and using a class
class Dog:
    """A simple attempt to model a dog"""
    def __init__(self, name, age):
        #make sure 2 underscore each sidem
        """Initialize name and age attributes"""
        self.name = name
        self.age = age

    def sit(self):
        """Simulate a dog sitting in reponse to a command"""
        print(f"{self.name} is now sitting")

    def roll_over(self):
        """Simulate a dog rolling iver in reponse to a command"""
        print(f"{self.name} is now rolling over ")

#making instance from class
my_dog = Dog('willie', 6)
#remeber class_name is capitalize

print(f"My dog name is {my_dog.name}")
#accessing atrribute = my_dog.name
#my_dog = instance, name = attribute
print(f"My dog age is {my_dog.age} years old")

#calling method
my_dog.sit()
my_dog.roll_over()
#instance.method

#calling multiple instances
your_dog = Dog('Lucy', 3)
print(f"\nYour dog name is {your_dog.name}")
print(f"Your dog age is {your_dog.age}")
your_dog.sit()
your_dog.roll_over()


# -*- coding: utf-8 -*-
import datetime
import hashlib
import mock
from nose.tools import raises
import StringIO
import textwrap
import xml.etree.cElementTree as ET

import harvestmedia.api.exceptions
from harvestmedia.api.library import Library

from utils import build_http_mock, get_random_md5, init_client

@mock.patch('harvestmedia.api.client.httplib2.Http')
def test_get_libraries(HttpMock):
    client = init_client()

    return_values = [
         (200, """<ResponseLibraries>
                    <libraries>
                        <library id="abc123" name="VIDEOHELPER" detail="Library description" />
                        <library id="abc125" name="MODULES" detail="Library description" />
                    </libraries>
                </ResponseLibraries>"""),
    ]

    http = build_http_mock(HttpMock, responses=return_values)

    libraries = Library.query.get_libraries(client)
    assert isinstance(libraries, list)

    library = libraries[0]
    assert library.id == 'abc123'
    assert library.name == 'VIDEOHELPER'

@mock.patch('harvestmedia.api.client.httplib2.Http')
def test_get_library_albums(HttpMock):
    client = init_client()
    test_album_id = get_random_md5()

    return_values = [
         (200, """<ResponseLibraries>
                    <libraries>
                        <library id="abc123" name="VIDEOHELPER" detail="Library description" />
                        <library id="abc125" name="MODULES" detail="Library description" />
                    </libraries>
                </ResponseLibraries>"""),

         (200, """<responsealbums>
                    <albums>
                        <album libraryid="abc123" featured="false" code="HM001" detail="Razor-sharp pop& rock
                        bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude."
                        name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%(test_album_id)s"/>
                        <album libraryid="19b8f5935503adde" featured="false" code="HM002" detail=" Contemporary beats
                        fused with orchestral elements." name="HM 002 Sample Album " displaytitle="HM 002 Sample Album "
                        id="67a6aed83a741a06"/>
                        </albums>
                    </responsealbums>""" % {'test_album_id': test_album_id}),
    ]

    http = build_http_mock(HttpMock, responses=return_values)

    libraries = Library.query.get_libraries(client)
    assert isinstance(libraries, list)

    library = libraries[0]
    albums = library.get_albums()
    assert isinstance(albums, list)
    assert albums[0].id == test_album_id


#!/usr/bin/env python3
# vim: sts=4 sw=4 et

import tll.channel as C
from tll.channel.base import Base
from tll.error import TLLError
from tll.test_util import Accum

from tll import asynctll

import pytest
import time

def test_asynctll():
    with pytest.raises(TLLError): asynctll.Loop(config={'nofd-interval':'xxx', 'poll':'xxx'})

    loop = asynctll.Loop()
    async def main():
        ctx = C.Context()
        s = ctx.Channel("mem://;size=1kb;name=server;dump=yes")
        c = ctx.Channel("mem://;master=server;name=client;dump=yes")

        loop.channel_add(s)
        loop.channel_add(c)

        s.open()
        c.open()

        s.post(b'xxx', seq=100)

        m = await loop.recv(c)
        loop.log.info("Got message {}", m)
        assert m.seq == 100
        assert m.data == b'xxx'

        s.post(b'zzz', seq=200)

        m = await loop.recv(c)
        assert m.seq == 200
        assert m.data == b'zzz'

    loop.run(main())

def test_wait_state():
    loop = asynctll.Loop(context=C.Context())
    async def main():
        c = loop.Channel('null://;name=null')

        c.open()

        assert await c.recv_state(ignore=None) == c.State.Opening
        assert await c.recv_state(ignore=None) == c.State.Active

        c.close()
        c.open()
        assert await c.recv_state() == c.State.Active
        c.close()
        assert await c.recv_state() == c.State.Closed

    loop.run(main())

def test_channel():
    loop = asynctll.Loop(context=C.Context())
    async def main(loop):
        s = loop.Channel("mem://;size=1kb;name=server;dump=yes")
        c = loop.Channel("mem://;master=server;name=client;dump=yes")

        s.open()
        c.open()

        s.post(b'xxx', seq=100)

        m = await c.recv()
        loop.log.info("Got message {}", m)
        assert m.seq == 100
        assert m.data == b'xxx'

        s.post(b'zzz', seq=200)

        m = await loop.recv(c) # Mixed syntax
        assert m.seq == 200
        assert m.data == b'zzz'

    loop.run(main(loop))

def test_nofd():
    loop = asynctll.Loop(context=C.Context())
    async def main(loop):
        s = loop.Channel("mem://;size=1kb;name=server;dump=yes;fd=no")
        c = loop.Channel("mem://;master=server;name=client;dump=yes")

        s.open()
        c.open()

        s.post(b'xxx', seq=100)

        m = await c.recv()
        loop.log.info("Got message {}", m)
        assert m.seq == 100
        assert m.data == b'xxx'

        s.post(b'zzz', seq=200)

        m = await loop.recv(c) # Mixed syntax
        assert m.seq == 200
        assert m.data == b'zzz'

    loop.run(main(loop))

def test_sleep():
    loop = asynctll.Loop(context=C.Context())
    async def main(loop):
        print("Sleep start")
        await loop.sleep(0.001)
        print("Sleep next")
        await loop.sleep(0.001)
        print("Sleep next")
        await loop.sleep(0.001)
        print("Sleep done")

    loop.run(main(loop))

def test_direct_recv():
    loop = asynctll.Loop(context=C.Context())
    async def main(loop):
        s = loop.Channel('direct://;name=server;dump=frame')
        c = loop.Channel('direct://;name=client;dump=frame', master = s)

        s.open()
        c.open()

        c.post(b'xxx', seq=0)

        start = time.time()
        m = await s.recv(0.1)
        dt = time.time() - start
        assert dt < 0.001

    loop.run(main(loop))

def test_scheme_cache():
    loop = asynctll.Loop(C.Context())

    c = loop.Channel('direct://;scheme=yamls://{};scheme-control=yamls://{}')
    c.open()
    c.close()

    assert c.scheme != None
    assert c.scheme_control != None

def test_async_mask():
    loop = asynctll.Loop(context=C.Context())
    async def main(loop):
        c = loop.Channel("zero://;size=1kb;name=zero", async_mask=C.MsgMask.State)

        c.open()

        m = await c.recv()
        assert m.type == m.Type.State
        assert c.State(m.msgid) == c.State.Opening

    loop.run(main(loop))


"""
Author: angles
29/11/16 - 12:22
"""

import numpy as np
from plotting_tools import nice_plots
from scipy.signal import correlate2d
from pylab import fft2
import filters

re_compute = False
normalized = True
samples_file = '5000_samples_from_gaussian_ring_64_10_9.npy'
if re_compute:
    samples_dcgan = np.load(samples_file)
    # print(samples_dcgan.shape)
    # plot_images(samples_dcgan[:5], gray=True)
    assert len(samples_dcgan.shape) == 3
    nb_samples = samples_dcgan.shape[0]
    # Working in dimension DxD
    assert samples_dcgan.shape[1] == samples_dcgan.shape[2]
    D = samples_dcgan.shape[1]
    gammas = np.zeros((nb_samples, D, D))
    for idx_sample in range(nb_samples):
        gammas[idx_sample, :, :] = (1 / (D ** 2)) * correlate2d(samples_dcgan[idx_sample],
                                                                samples_dcgan[idx_sample],
                                                                mode='same', boundary='wrap')

    gamma = np.mean(gammas, axis=0)
    spectrum_gamma = np.sort(np.abs(fft2(gamma)).ravel())
    psi = filters.gaussian_ring(D, 10, 9)
    spectrum_fourier_psi = np.sort(np.power(np.abs(fft2(psi)), 2).ravel())

    np.save('spectrum_fourier_psi.npy', spectrum_fourier_psi)
    np.save('spectrum_gamma.npy', spectrum_gamma)

else:
    samples_dcgan = np.load(samples_file)
    nb_samples = samples_dcgan.shape[0]
    spectrum_fourier_psi = np.load('spectrum_fourier_psi.npy')
    spectrum_gamma = np.load('spectrum_gamma.npy')

if normalized:
    spectrum_gamma /= spectrum_gamma[-1]
    spectrum_fourier_psi /= spectrum_fourier_psi[-1]

# Plotting
##########
plots = [spectrum_fourier_psi, spectrum_gamma]
labels = ['spectrum_fourier_psi', 'spectrum_gamma']
nice_plots(plots, labels, 'Nb samples : {0}'.format(nb_samples))


#!/usr/bin/python3

import os
import sys
import json
import time
import datetime

currentdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
from beos_test_utils.beos_utils_pack import init, ActionResult, ResourceResult, VotersResult

if __name__ == "__main__":
	try:
		node, summary, args, log = init(__file__)
		node.run_node()
		acc = node.create_accounts(1)
		node_info = json.loads(node.make_cleos_call("get info"))
		acc_info = json.loads(node.make_cleos_call("get account {0} -j".format(acc[0].name)))
		node.stop_node()

		summary.equal(True, "chain_id" in node_info, "\"chain_id\" in node_info")
		summary.equal(True, acc_info["account_name"] == acc[0].name, "acc_info[\"account_name\"] == {0}".format(acc[0].name))

	except Exception as _ex:
		log.exception("Exception `{0}` occures while executing `{1}` tests.".format(str(_ex), __file__))
	finally:
		summary_status = summary.summarize()
		node.stop_node()
		exit(summary_status)


'''
Code written for data analysis of signals from a CBGB source, in particular
to deal with chemically enhanced signals.

This code was originally written to analyze data from a digital oscilloscope
("CleverScope"). This code was designed to be run in
a Jupyter Notebook. For details on implementation, open the file
"Sample_Notebook.ipynb" in Jupyter.

Eventually, the goal is to make this code object oriented.

Written by Arian Jadbabaie
Email: arianjad@gmail.com
'''

import pathlib
import numpy as np
import csv
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
from scipy.optimize import curve_fit
from scipy.interpolate import splrep, splev

'''Each data trace has associated with it some metadata, referred to as
"parameters". These parameters are stored in a dict. The elements are:

    dt: time step of data traces (ms)
    start: start time of data traces, relative to trigger (ms)
    nsample: number of elements in time series
    trigtime: UNIX time of trigger
'''

##Main Functions##

def integrate_data_array(data_array,metadata_array,bounds,display=True):
    integrated_array = []
    for data, meta in zip(data_array, metadata_array):
        integrated_array.append(slice_integrate(data,meta,bounds,display))
    integrated_array = np.array(integrated_array)
    return integrated_array

def extract_frequency(metadata):
    return np.array([meta['frequency'] for meta in metadata])

def slice_integrate(data,metadata,start_stop,plot,fig_num=1):
    time_ms = time_array(metadata)
    start_i = np.searchsorted(time_ms,start_stop[0])
    stop_i = np.searchsorted(time_ms,start_stop[1])
    t = slice(start_i,stop_i)
    dt = np.round(time_ms[1]-time_ms[0],decimals=6)
    integrated = np.round(data[t].sum()*dt,decimals=6)
    if plot:
            plt.figure(fig_num)
            plt.plot(time_ms[t],data[t])
    return integrated

def process_raw_data_array(folder_path,file_numbers,file_name,channel_types,):
    '''This function is used to convert a series of raw data files to
    processed data traces. The raw data can be in the form of absorption
    data, or fluorescence data.

    Inputs:
    folder: local path of folder containing raw data
    file_numbers: array containing the file numbers to process
    channel_types: a list containing the data types that label each channel.
    For example, if channel 1 is absorption and channel 2 fluorescence, then
    the channel_type is ['abs','fluor']
    root_folder: parent folder containing data folder
    file_name: name of data file. The format should be "nameXXX.txt", where
    XXX is a number labeling the file

    Outputs:
    A list of the form [processed_data,param_array]
    Each element in data_array and parameter_array correspond to the traces and
    metadata obtained from a single file.
    '''
    param_array = []
    data_array = []
    for num in file_numbers:
        processed_data, params = process_single_data_file(folder_path, num, file_name, channel_types)
        param_array.append(params)
        data_array.append(processed_data)
    # data_array = np.array(data_array) # decided not to turn these into numpy arrays
    # param_array = np.array(param_array)
    return [data_array,param_array]

def process_single_data_file(folder_path, file_num, file_name, channel_types, DAQ='PXI'):
    '''This function processes a single raw data file. It returns the
    processed data and the parameters associated with that data.

    Inputs:
    data_folder: name of folder containing data file, string.
    file_num: number labeling data file
    channel_types: a list containing the data types that label each channel.
    For example, if channel 1 is absorption and channel 2 fluorescence, then
    the channel_type is ['abs','fluor']
    root_folder: parent folder containing data folder
    file_name: name of data file. The format should be "nameXXX.txt", where
    XXX is a number labeling the file

    Outputs: [processed_traces, params]
    processed_traces: an array of the processed data read from the file.
    Same order as channel_types.
    params: metadata associated with the data file.
    '''
    raw_traces, params = read_raw_file(folder_path,file_num,file_name,DAQ=DAQ)
    time_ms = time_array(params)
    processed_traces = []
    for ch,type in enumerate(channel_types):
        if type == 'abs':
            abs_trace = process_raw_abs(raw_traces[ch], time_ms)
            processed_traces.append(abs_trace)
        elif type == 'fluor':
            fluor_trace = process_raw_fluor(raw_traces[ch],time_ms)
            processed_traces.append(fluor_trace)
    return [processed_traces, params]

def time_array(parameters):
    '''This function generates a time array using metadata, supplied as the
    function input. Outputs a numpy array of time in miliseconds.'''
    dt = parameters['dt']
    t0 = parameters['start']
    npnts = parameters['nsample']
    time_ms = np.round(np.linspace(t0,dt*(npnts-1)+t0,npnts),decimals = 6)
    time_ms-=parameters['YAGtrig']
    return time_ms

def write_CSV_rows(data_array,file_path,labels):
    '''Wrapper for write_CSV_rows. Writes data arrays to location in file path.
    Also labels each row.

    Inputs:
    data_array: containing all arrays to be written to
    file_path: location of file to be written, referenced to parent directory
    of current working directory
    labels: list of labels for each data array to be written
    '''
    full_path = gen_filepath_from_parent(file_path)
    data_array = np.array(data_array)
    save_all = []
    save_all.append(labels)
    for _data in data_array.T:
        save_all.append(_data)
    _write_CSV_rows(save_all,full_path)
    return

def read_CSV_rows(file_path,read_header=0,obj=False):
    '''Reads rows of data from CSV file. Returns [data,header]

    Inputs:
    file_path: location of file, referenced from parent directory
    read_header: number of lines to read as header instead of as data.
    Default is 0.
    obj: to typecast the read data as an object, set to True. Default is False.
    '''
    data_type = 'float'
    if obj:
        data_type='object'
    full_path = gen_filepath_from_parent(file_path)
    header = []
    with open(full_path,'r') as f:
        r = csv.reader(f)
        for i in range(read_header):
            header.append(next(r))
        rows = np.array([np.array([float(value) if isFloat(value) else value for value in row],dtype=data_type) for row in r])
    print('Done! Array read from', file_path)
    return rows,header

def read_CSV_columns(file_path,read_header=0,obj=False):
    '''Reads columns of data from CSV file. Returns [data,header]

    Inputs:
    file_path: location of file, referenced from parent directory
    read_header: number of lines to read as header instead of as data.
    Default is 0.
    obj: to typecast the read data as an object, set to True. Default is False.
    '''
    data_type = 'float'
    if obj:
        data_type='object'
    full_path = processed_data_filepath(file_path)
    header = []
    with open(full_path,'r') as f:
        r = csv.reader(f)
        for i in range(read_header):
            header.append(next(r))
        rows = np.array([np.array([float(value) if isFloat(value) else value for value in row],dtype=data_type) for row in r])
    columns = rows.T
    print('Done! Array read from', full_path)
    return columns,header

## Data Processing Subfunctions ##

def process_raw_fluor(raw_data,time_ms,line_background=True):
    if line_background:
        processed_trace = raw2fluor_wLine(raw_data,time_ms)
    else:
        processed_trace = raw2fluor(raw_data,time_ms)
    return processed_trace

def process_raw_abs(raw_data,time_ms,line_background=True):
    if line_background:
        processed_trace = raw2OD_wLine(raw_data,time_ms)
    else:
        processed_trace = raw2OD(raw_data,time_ms)
    return processed_trace

def raw2fluor_wLine(raw_data,time_ms):
    trigger_index = np.searchsorted(time_ms,0)
    beforeYAG_index = np.searchsorted(time_ms,-0.1)
    after_abs_index = np.searchsorted(time_ms,time_ms[-1000])
    #Calculate linear and DC offset, convert signal to OD
    fit_time = np.concatenate((time_ms[:beforeYAG_index],time_ms[after_abs_index:]))
    fit_data = np.concatenate((raw_data[:beforeYAG_index],raw_data[after_abs_index:]))
    popt, pcov = curve_fit(line_func, fit_time , fit_data,p0=[(fit_data[-1]-fit_data[0])/(time_ms[-1]-time_ms[0]),fit_data.mean()])
    A=popt[0]
    offset=popt[1]
    flat_data=np.zeros(len(raw_data))
    for i in range(len(raw_data)):
        flat_data[i]=raw_data[i]-(line_func(time_ms[i],A,offset)-offset)
    #offset = raw_data[:beforeYAG_index].mean()
    #Smooth the data
    smoothed_data = smooth(flat_data,window=60)
    floor = smoothed_data[smoothed_data>0].min()
    smoothed_data[smoothed_data<0] = floor

    offset = (raw_data[:beforeYAG_index].mean()+raw_data[-1000].mean())/2
    #Smooth the data
    smoothed_data-=offset
    return smoothed_data

def raw2fluor(raw_data,time_ms):
    trigger_index = np.searchsorted(time_ms,0)
    beforeYAG_index = np.searchsorted(time_ms,-0.1)
    #Calculate DC offset, convert signal to OD
    offset = (raw_data[:beforeYAG_index].mean()+raw_data[-1000].mean())/2
    #Smooth the data
    smoothed_data = smooth(raw_data,window=60)
    smoothed_data-=offset
    #Calculate OD, fix floating point errors
    return smoothed_data

def raw2OD_wLine(raw_data,time_ms):
    trigger_index = np.searchsorted(time_ms,0)
    beforeYAG_index = np.searchsorted(time_ms,-0.01)
    after_abs_index = np.searchsorted(time_ms,time_ms[-int(len(time_ms)*0.1)])
    #Calculate linear and DC offset, convert signal to OD
    fit_time = np.concatenate((time_ms[:beforeYAG_index],time_ms[after_abs_index:]))
    fit_data = np.concatenate((raw_data[:beforeYAG_index],raw_data[after_abs_index:]))
    popt, pcov = curve_fit(line_func, fit_time , fit_data,p0=[(fit_data[-1]-fit_data[0])/(time_ms[-1]-time_ms[0]),fit_data.mean()])
    A=popt[0]
    offset=popt[1]
    flat_data=np.zeros(len(raw_data))
    for i in range(len(raw_data)):
        flat_data[i]=raw_data[i]-(line_func(time_ms[i],A,offset)-offset)
    #offset = raw_data[:beforeYAG_index].mean()
    #Smooth the data
    smoothed_data = smooth(flat_data,window=60)
    floor = smoothed_data[smoothed_data>0].min()
    smoothed_data[smoothed_data<0] = floor
    smoothed_plot = smooth(raw_data,window=60)
    floor = smoothed_plot[smoothed_plot>0].min()
    smoothed_plot[smoothed_plot<0] = floor
    # plt.figure()
    # plt.plot(time_ms,smoothed_plot)
    # plt.plot(time_ms,line_func(time_ms,A,offset))
    #Calculate OD, fix floating point errors
    OD = np.log(offset/smoothed_data)
    if OD[trigger_index]<0:
        offset = smoothed_data[trigger_index]
        OD = np.log(offset/smoothed_data)
    return OD

def raw2OD(raw_data,time_ms):
    '''Convert raw absorption data into optical depth'''
    trigger_index = np.searchsorted(time_ms,0)
    beforeYAG_index = np.searchsorted(time_ms,-0.1)
    #Calculate DC offset, convert signal to OD
    offset = raw_data[:beforeYAG_index].mean()
    #Smooth the data
    smoothed_data = smooth(raw_data,window=60)
    floor = smoothed_data[smoothed_data>0].min()
    smoothed_data[smoothed_data<0] = floor
    #Calculate OD, fix floating point errors
    OD = np.log(offset/smoothed_data)
    if OD[trigger_index]<0:
        offset = smoothed_data[trigger_index]
        OD = np.log(offset/smoothed_data)
    return OD

def read_raw_file(folder_path,num,file_name,DAQ='PXI',print_bool=False):
    '''From a single folder location, obtain dataset with raw data and metadata.
    For full info, see documentation of process_single_data_file().
    '''
    file_path = gen_data_filepath(folder_path,num,file_name)
    if print_bool:
        print(file_path)
    if DAQ == 'PXI':
        meta = import_metadata_PXI(file_path)
        raw = import_raw_data_PXI(file_path)
    elif DAQ == 'Cleverscope':
        meta = import_metadata_Cleverscope(file_path)
        raw = import_raw_data_Cleverscope(filepath)
    return [raw,meta]

def import_raw_data_PXI(filepath,header_lines = 30):
    '''Imports raw traces from data columns in single PXI text file.
    Input is file path.'''
    try:
        arr = np.genfromtxt(filepath,delimiter='', skip_header = header_lines)
        raw_data = [ch for ch in arr.T]
    except ValueError:
        print('Issues with file located at: ',filepath)
        raw_data = [np.zeros(10000),np.zeros(10000)]
    except OSError:
        print('Issues with file located at: ',filepath)
        raw_data = [np.zeros(10000),np.zeros(10000)]
    return raw_data

def split_strip(string):
    return string.split(':')[-1].strip(';')

def import_metadata_PXI(filepath):
    '''Import metadata parameters from Cleverscope file.

    Parameters returned as dict:
    dt: time step of data traces (ms)
    start: start time of data traces, relative to trigger (ms)
    nsample: number of elements in time series
    trigtime: UNIX time of trigger
    '''
    with open(filepath, 'r') as f:
        lines=[]
        for i in range(30):
            lines.append(f.readline().strip('\n'))
    meta={}
    for text in lines:
        if 'cycle number' in text:
            meta['cycle number'] = int(split_strip(text))
        if 'iteration' in text:
            meta['iteration'] = int(split_strip(text))
        if 'command file' in text:
            #note the path usually also includes a ':', so split_strip() will just drop the drive in the path
            meta['command file'] = split_strip(text)
        if 'comment' in text:
            meta['comment'] = text.split(':')[1]
        if 'wavenumber' in text:
            if '(cm-1):' in text:
                meta['frequency'] = float(text.split('(cm-1):')[-1].strip(';'))
            else:
                meta['frequency'] = float(text.split('(cm-1)')[-1].strip(';'))
        if 'Channels' in text:
            meta['channels'] = int(text.strip('\t').split('\t')[-1])
        if 'Samples' in text:
            meta['nsample'] = [int(x) for x in text.strip('Samples').strip('\t').split('\t')][0]
        if 'Time' in text:
            meta['trigtime'] = text.strip('Time').strip('\t').split('\t')
        if 'Delta_X' in text:
            meta['dt'] = [float(x) for x in text.strip('Delta_X').strip('\t').split('\t')][0]*1000
        if 'X0' in text:
            meta['start'] = 0
            meta['X0'] = [float(x) for x in text.strip('X0').strip('\t').split('\t')]
    meta['YAGtrig'] = 2
    return meta

def import_raw_data_Cleverscope(filepath,usecols=(1,2)):
    '''Imports raw traces from data columns in single cleverscope text file.
    Input is file path.'''
    try:
        arr = np.genfromtxt(filepath,delimiter='', skip_header = 15, usecols=usecols)
        ch1_raw = arr.T[0] #Channel A
        ch2_raw = arr.T[1] #Channel B
        raw_data = [ch1_raw,ch2_raw]
    except ValueError:
        print('Issues with file located at: ',filepath)
        raw_data = [np.zeros(10000),np.zeros(10000)]
    except OSError:
        print('Issues with file located at: ',filepath)
        raw_data = [np.zeros(10000),np.zeros(10000)]
    return raw_data

def import_params_Cleverscope(filepath):
    '''Import metadata parameters from Cleverscope file.

    Parameters returned as dict:
    dt: time step of data traces (ms)
    start: start time of data traces, relative to trigger (ms)
    nsample: number of elements in time series
    trigtime: UNIX time of trigger
    '''
    with open(filepath, 'r') as f:
        lines=[]
        for i in range(15):
            lines.append(f.readline())
    params={}
    for text in lines:
        if 'delta' in text:
            dt_ms = np.round(float(text.split('\t')[1].strip())*10**3,decimals=6)
            params['dt']=dt_ms
        elif 'start' in text:
            start_ms = np.round(float(text.split('\t')[1].strip())*10**3,decimals = 6)
            params['start']=start_ms
        elif 'nsample' in text:
            nsample = int(float(text.split('\t')[1].strip()))
            params['nsample']=nsample
        elif 'TriggerTime' in text:
            # The time is given as the number of days since 12/29/1899 11:00:00 UTC
            # We can convert this to UNIX by subtracting 2209208400 seconds
            # Ask Arian if you are curious
            time_sec = float(text.split('\t')[1].strip())*24*3600 #convert days to seconds
            time_unix = time_sec - 2209208400
            params['trigtime']=time_unix
    params['YAGtrig'] = 0
    return params

def smooth(data,window=5,poly_order=3):
    '''Function for smoothing data. Currently uses Savitzky-Golay filter,
    which fits a window of data onto a polynomial of some order, and then
    uses the polynomial to estimate the value'''
    #window value must be odd
    if window % 2 == 0:
        window+=1
    smoothed_data = savgol_filter(data, window, poly_order)
    return smoothed_data

def sliceIntegrate(data,time_ms,start_stop,fig_num=1):
    '''Integrate time slice of the supplied data trace from start to stop.
        start_stop = [start,stop] in ms'''
    start_i = np.searchsorted(time_ms,start_stop[0])
    stop_i = np.searchsorted(time_ms,start_stop[1])
    t = slice(start_i,stop_i)
    dt = np.round(time_ms[1]-time_ms[0],decimals=6)
    integrated = np.round(data[t].sum()*dt,decimals=6)
    plt.figure(fig_num)
    plt.plot(time_ms[t],data[t])
    return integrated

## Other functions ##

def labelBUB(param_array,blocked_bool):
    '''Iterate through a series of files and compiles ODs and parameters.
        Labels the whole series as blocked (True) or unblocked (False).
        Returns an array of ODs and parameters'''
    # not sure what this does. looks broken

    for single_param in param_array:
        single_param['blocked'] = blocked_bool
    return [ODs,params]

'''Extracts trigger timestamps from parameter arrays. Times are in seconds relative to the initial file timestamp'''
def extractTimestamps(params,subtract_offset=True):
    timestamps = []
    if subtract_offset:
        for single_params in params:
            if len(timestamps)==0:
                initial = single_
                timestamps.append(0)
            else:
                elapsed = single_params['trigtime'] - initial
                timestamps.append(elapsed)
    else:
            for single_params in params:
                unix_time = single_params['trigtime']
                timestamps.append(unix_time)
    return np.array(timestamps)

'''Extracts trigger timestamps from 2 (blocked/unblocked) parameter arrays,
    relative to whichever was first'''
def extractTimestampsBUB(blocked_params,unblocked_params):
    blocked_timestamps = []
    unblocked_timestamps = []
    if blocked_params[0]['trigtime'] < unblocked_params[0]['trigtime']:
        blocked_first = True
    else:
        blocked_first = False
    for i in range(len(blocked_params)):
        if blocked_first:
            if len(blocked_timestamps)==0:
                initial = blocked_params[i]['trigtime']
                blocked_timestamps.append(0)
                elapsed_unblocked = unblocked_params[i]['trigtime'] - initial
                unblocked_timestamps.append(elapsed_unblocked)
            else:
                elapsed_blocked = blocked_params[i]['trigtime'] - initial
                elapsed_unblocked = unblocked_params[i]['trigtime'] - initial
                blocked_timestamps.append(elapsed_blocked)
                unblocked_timestamps.append(elapsed_unblocked)
        else:
            if len(unblocked_timestamps)==0:
                initial = unblocked_params[i]['trigtime']
                unblocked_timestamps.append(0)
                elapsed_blocked = blocked_params[i]['trigtime'] - initial
                blocked_timestamps.append(elapsed_blocked)
            else:
                elapsed_blocked = blocked_params[i]['trigtime'] - initial
                elapsed_unblocked = unblocked_params[i]['trigtime'] - initial
                blocked_timestamps.append(elapsed_blocked)
                unblocked_timestamps.append(elapsed_unblocked)
    return [blocked_timestamps,unblocked_timestamps]

def auto_integrate(OD,time_ms,start=0,fig_num=1):
    start_i = np.searchsorted(time_ms,start)
    smoothed_deriv = savgol_filter(OD,3001,5,deriv=1)
    smoothed_deriv/=smoothed_deriv.max()
    norm_OD = OD/OD.max()
    cut_i = np.argmin(smoothed_deriv[start_i:-500])+start_i #ignore before trigger and last few points
    OD_cut = norm_OD[cut_i:]
    smoothed_deriv_cut = smoothed_deriv[cut_i:]
    time_cut = time_ms[cut_i:]
    if OD.max() < 0.005:
        cut = 0.5
    else:
        cut = 0.03
    OD_threshold = cut
    deriv_threshold = abs(smoothed_deriv[cut_i:]).max()*0.03
    still_looking=True
    deriv_check = False
    OD_check = False
    for i in range(len(time_cut)):
        OD_val = abs(OD_cut[i])
        deriv_val = abs(smoothed_deriv_cut[i])
        if deriv_val < deriv_threshold:
            deriv_check = True
        if OD_val < OD_threshold:
            OD_check = True
        if deriv_check and OD_check:
            stop = time_cut[i]
            still_looking=False
            break
    if still_looking:
        print('Cannot find limit')
        plt.figure(fig_num+15)
        plt.title('Problem Traces')
        plt.plot(time_ms,OD)
        plt.figure(fig_num+16)
        plt.title('Problem Smoothed Traces')
        #plt.plot(time_cut, smoothed_OD_cut)
        plt.plot(time_cut,smoothed_deriv_cut)
        stop = 0.5
    stop_i = np.searchsorted(time_ms,stop)
    t = slice(start_i,stop_i)
    dt = np.round(time_ms[1]-time_ms[0],decimals=6)
    integrated = np.round(OD[t].sum()*dt,decimals=6)
    plt.figure(fig_num)
    plt.plot(time_ms[t],OD[t])
    return integrated

## File I/O Subfunctions ##

def _write_CSV_rows(data_arrays,filepath):
    '''This function writes arrays in rows to a CSV file. Subfunction for
    write_CSV_rows.

    Inputs:
    data_arrays: contains all arrays to be written
    filepath: path for CSV file
    '''
    with open(filepath,'w+',newline='') as f:
        w = csv.writer(f)
        for array in data_arrays:
            w.writerow(array)
    print('Done! Array written to', filepath)
    return

def isFloat(value):
    '''Checks if input is float.'''
    # Probably easier to use isinstance
    try:
        float(value)
        return True
    except:
        return False

def gen_filepath_from_parent(path):
    '''Generates path to a file located in a parellel directory
    from the current working directory (cwd). Specifically, if the cwd is in
    parent_dir/cwd, then it generates a path to parent_dir/path
    '''
    current = pathlib.Path.cwd()
    parrent_dir = current.parents[0]
    new_path = parent_dir / path
    return new_path

def gen_data_filepath(folder_path, num, name):
    '''Generate file path for single text file named "name_num.txt", located
    in the folder given by fikder_path. Note folder_path should be an absolute
    path
    '''
    num_str = format(num,'03d')
    folder_path = pathlib.Path(folder_path)
    file =  name + num_str + '.txt'
    full_path = folder_path / file
    return full_path

# Old Functions #

'''Calculate the optical depth (OD) signal from a single text file.
    Returns a dataset, which consists of the optical dpeth and acquisition parameters'''
def calculateSingleOD(root_folder,num,ch=3):
    raw_traces,params = file2dataset(root_folder,num)
    if ch == 3: #Get both channels
        ch1 = raw2OD_wLine(np.array(raw_traces[0]),time_array(params))
        ch2 = raw2OD_wLine(np.array(raw_traces[1]),time_array(params))
        ODs = [ch1,ch2]
    else:
        single_ch = raw2OD_wLine(np.array(raw_traces[ch-1]),time_array(params))
        ODs = [single_ch]
    return [ODs,params]

'''Calculate the optical depth (OD) signal from a single text file.
    Returns a dataset, which consists of the optical dpeth and acquisition parameters'''
def calculateSingleFluor(root_folder,num,ch=3):
    raw_traces,params = file2dataset(root_folder,num)
    if ch == 3: #Get both channels
        ch1 = raw2fluor(np.array(raw_traces[0]),time_array(params))
        ch2 = raw2fluor(np.array(raw_traces[1]),time_array(params))
        fluor = [ch1,ch2]
    else:
        single_ch = raw2fluor(np.array(raw_traces[ch-1]),time_array(params))
        fluor = [single_ch]
    return [fluor,params]

def calculate_single_OD_fluor(root_folder,num,fluor_ch=1,OD_ch=2):
    raw_traces,params = file2dataset(root_folder,num)
    time_ms = time_array(params)
    abs_OD = raw2OD_wLine(raw_traces[OD_ch-1],time_ms)
    fluor = raw2fluor_wLine(raw_traces[fluor_ch-1],time_ms)
    return [[fluor,abs_OD],params]

# def auto_integrate(OD,time_ms,start=0,fig_num=1):
#     start_i = np.searchsorted(time_ms,start)
#     f = splrep(time_ms,OD,k=4,s=0.1)
#     f_d1 = splev(time_ms,f,der=1)
#     f_d2 = splev(time_ms,f,der=2)
#     after_max_i = np.argmin(f_d1[start_i+10:-100])+start_i+10 #ignore before trigger and last few points
#     f_d1_cut = f_d1[after_max_i:]
#     f_d2_cut = f_d2[after_max_i:]
#     time_cut = time_ms[after_max_i:]
#     still_looking = True
#     d1_thresh = abs(OD).max()*0.01
#     d2_thresh = abs(f_d1_cut).max()*0.01
#     for i in range(len(time_cut)):
#         d1 = abs(f_d1_cut[i])
#         d2 = abs(f_d2_cut[i])
#         if d1 < d1_thresh and d2 < d2_thresh:
#             stop = time_cut[i]
#             still_looking=False
#             break
#     if still_looking:
#         print('Issue integrating the following trace')
#         plt.figure(fig_num+3)
#         plt.plot(time_ms,OD)
#         plt.plot(time_ms,splev(time_ms,f))
#         plt.plot(time_ms,f_d1)
#         stop = 0.1
#     stop_i = np.searchsorted(time_ms,stop)
#     t = slice(start_i,stop_i)
#     dt = np.round(time_ms[1]-time_ms[0],decimals=6)
#     integrated = np.round(OD[t].sum()*dt,decimals=6)
#     if still_looking:
#         integrated = np.nan
#     plt.figure(fig_num)
#     plt.plot(time_ms[t],OD[t])
#     return integrated

def sortData(indep_var,depend_var):
    #print(len(indep_var))
    #print(len(depend_var))
    if len(indep_var) != len(depend_var):
        print('Number of independent and dependent variables do not match')
    else:
        if not isinstance(indep_var,list):
            indep_var = list(indep_var)
        sorted_DV = [DV for IV,DV in sorted(zip(indep_var,depend_var))]
    return sorted_DV

def calcEnhancement(blocked_int,unblocked_int):
    num_traces = len(blocked_int)
    enhancement = np.zeros(num_traces)
    for i in range(num_traces):
        if blocked_int[i]<0.0001:
            enhancement[i]=0
        else:
            enhancement[i] = unblocked_int[i]/blocked_int[i]
    return enhancement

def line_func(x, A, B):
    return A*x + B

def calculateSingleOD_wline(root_folder,num,after_abs_time=5,ch=3):
    raw_traces,params = file2dataset(root_folder,num)
    if ch == 3: #Get both channels
        ch1 = raw2OD_wLine(np.array(raw_traces[0]),time_array(params))
        ch2 = raw2OD_wLine(np.array(raw_traces[1]),time_array(params))
        ODs = [ch1,ch2]
    else:
        single_ch = raw2OD_wLine(np.array(raw_traces[ch-1]),time_array(params))
        ODs = [single_ch]
    return [ODs,params]

#Not used anymore. Only useful if there is linear drift
# def subtractBackground(raw,time):
#     end = len(cell_abs)
#     start = 5000
#     t = slice(start,end)
#     offset = -1.8486
#     num_avg = 900
#     time_ms = np.round(np.linspace(offset,dt*(end-1)+offset,end),decimals = 6)
#     b_guess = cell_abs[:num_avg].sum()/num_avg
#     m_guess = 0
#     abs_slice = cell_abs[t]
#     time_slice = time_ms[t]
#     params = fitLine(time_slice,abs_slice,guess=[m_guess,b_guess])
#     background = line(time_ms,*params)
#     cell_OD = np.log(background/cell_abs)
#     #cell_OD[cell_OD<0]=0
#     return cell_OD


import pytest
from mock import MagicMock

from app.data_models.answer_store import AnswerStore
from app.data_models.list_store import ListStore
from app.data_models.progress_store import ProgressStore
from app.data_models.session_data import SessionData
from app.forms.questionnaire_form import QuestionnaireForm
from app.questionnaire.questionnaire_schema import QuestionnaireSchema

@pytest.fixture
def list_collector_block():
    return {
        "id": "list-collector",
        "type": "ListCollector",
        "for_list": "people",
        "add_block": {
            "id": "add-person",
            "type": "ListAddQuestion",
            "question": {
                "id": "add-question",
                "type": "General",
                "title": "What is the name of the person?",
                "answers": [
                    {
                        "id": "first-name",
                        "label": "First name",
                        "mandatory": True,
                        "type": "TextField",
                    },
                    {
                        "id": "last-name",
                        "label": "Last name",
                        "mandatory": True,
                        "type": "TextField",
                    },
                ],
            },
        },
        "edit_block": {
            "id": "edit-person",
            "type": "ListEditQuestion",
            "question": {
                "id": "edit-question",
                "type": "General",
                "title": "What is the name of the person?",
                "answers": [
                    {
                        "id": "first-name",
                        "label": "First name",
                        "mandatory": True,
                        "type": "TextField",
                    },
                    {
                        "id": "last-name",
                        "label": "Last name",
                        "mandatory": True,
                        "type": "TextField",
                    },
                ],
            },
        },
        "remove_block": {
            "id": "remove-person",
            "type": "ListRemoveQuestion",
            "question": {
                "id": "remove-question",
                "type": "General",
                "title": "Are you sure you want to remove this person?",
                "answers": [
                    {
                        "id": "remove-confirmation",
                        "mandatory": True,
                        "type": "Radio",
                        "options": [
                            {
                                "label": "Yes",
                                "value": "Yes",
                                "action": {"type": "RemoveListItemAndAnswers"},
                            },
                            {"label": "No", "value": "No"},
                        ],
                    }
                ],
            },
        },
        "summary": {
            "item_title": {
                "text": "{person_name}",
                "placeholders": [
                    {
                        "placeholder": "person_name",
                        "transforms": [
                            {
                                "arguments": {
                                    "delimiter": " ",
                                    "list_to_concatenate": [
                                        {
                                            "source": "answers",
                                            "identifier": "first-name",
                                        },
                                        {
                                            "source": "answers",
                                            "identifier": "last-name",
                                        },
                                    ],
                                },
                                "transform": "concatenate_list",
                            }
                        ],
                    }
                ],
            }
        },
        "question": {
            "id": "confirmation-question",
            "type": "General",
            "title": "Does anyone else live here?",
            "answers": [
                {
                    "id": "anyone-else",
                    "mandatory": True,
                    "type": "Radio",
                    "options": [
                        {
                            "label": "Yes",
                            "value": "Yes",
                            "action": {"type": "RedirectToListAddBlock"},
                        },
                        {"label": "No", "value": "No"},
                    ],
                }
            ],
        },
    }

@pytest.fixture
def form():
    mock_form = MagicMock(
        spec=QuestionnaireForm,
        data={"first-name": "Toni", "last-name": "Morrison"},
        errors={},
        question_errors={},
        fields={},
    )
    mock_form.answer_errors.return_value = ""
    return mock_form

@pytest.fixture
def schema():
    return MagicMock(
        QuestionnaireSchema({"questionnaire_flow": {"type": "Hub", "options": {}}})
    )

@pytest.fixture
def answer_store():
    return AnswerStore()

@pytest.fixture
def list_store():
    return ListStore()

@pytest.fixture
def progress_store():
    return ProgressStore()

@pytest.fixture
def people_answer_store():
    return AnswerStore(
        [
            {"answer_id": "first-name", "value": "Toni", "list_item_id": "PlwgoG"},
            {"answer_id": "last-name", "value": "Morrison", "list_item_id": "PlwgoG"},
            {"answer_id": "first-name", "value": "Barry", "list_item_id": "UHPLbX"},
            {"answer_id": "last-name", "value": "Pheloung", "list_item_id": "UHPLbX"},
        ]
    )

@pytest.fixture
def people_list_store():
    return ListStore([{"items": ["PlwgoG", "UHPLbX"], "name": "people"}])

@pytest.fixture
def response_metadata():
    return {"started_at": "2021-01-01T09:00:00.220038+00:00"}

@pytest.fixture
def fake_session_data():
    return SessionData(
        tx_id="tx_id",
        schema_name="some_schema_name",
        period_str="period_str",
        language_code=None,
        launch_language_code=None,
        survey_url=None,
        ru_name="ru_name",
        ru_ref="ru_ref",
        response_id="response_id",
        case_id="case_id",
    )


#-*- coding: utf8
'''
Prints the number of plays of a user to a given object
'''
from __future__ import division, print_function

from code import db

from collections import defaultdict

import numpy as np
import plac

def main(table_name, min_pop=10000):

    counts = defaultdict(int)
    obj_pop = defaultdict(int)
    last_stamp = {}
    first_stamp = {}

    for tstamp, user, obj in db.iter_sorted(table_name):
        if (user, obj) not in first_stamp:
            first_stamp[user, obj] = tstamp

        obj_pop[obj] += 1
        counts[user, obj] += 1
        last_stamp[user, obj] = tstamp

    for obj in obj_pop.keys(): #no concurrent, keys is a copy
        if obj_pop[obj] < min_pop:
            del obj_pop[obj]

    plays = defaultdict(list)
    for user, obj in sorted(counts.keys()):
        if obj not in obj_pop:
            continue

        delta = last_stamp[user, obj] - first_stamp[user, obj]
        if delta == 0:
            continue

        plays[obj].append(counts[user, obj])

    for obj in plays:
        print(obj, ' '.join([str(x) for x in plays[obj]]))

if __name__ == '__main__':
    plac.call(main)


# -*- coding: utf-8 -*-
import numpy as np
import soundfile as sf

from mic_py.mic_io import read_mic_wav_from_lst, read_mic_wav_from_folder
from mic_py.mic_stft import stft_arr
from mic_py.feats import istft
from mic_py.mic_geometry import get_sensor_positions, get_source_position, get_pair_mic_distance
from mic_py.mic_steering import  propagation_vector_free_field
from mic_py.mic_lcmv import lcmv_filter
from mic_py.mic_ds_beamforming import ds_beamforming, ds_align
import matplotlib.pyplot as plt
from mic_py.mic_null import null_filter

if __name__ == '__main__':

    # ################################################################
    # # 1.0 - _wav_wbn45_dict0 PROFILE
    #
    # vert_mic_count = 6
    # hor_mic_count  = 11
    # dHor           = 0.035
    # dVert          = 0.05
    # max_len_sec    = 45
    # n_fft          = 512
    #
    # (angle_hor_log, angle_vert_log) = (0.0, 0.0)
    # (angle_inf_hor_log, angle_inf_vert_log) = (45, 0)
    #
    # angle_h = -angle_hor_log
    # angle_v = -angle_vert_log
    #
    # angle_inf_h = -angle_inf_hor_log
    # angle_inf_v = -angle_inf_vert_log
    #
    # in_wav_path    = r'./data/_wav_wbn45_dict0/'
    # out_wav_path   = r'./data/out/'
    #
    # _noise_start   = 0
    # _noise_end     = 25
    #
    # _mix_start     = 0
    # _mix_end       = 25
    # #################################################################

    #################################################################
    # 1.0 - _rameses PROFILE

    vert_mic_count = 6
    hor_mic_count  = 11
    dHor           = 0.035
    dVert          = 0.05
    max_len_sec    = 45
    n_fft          = 512

    (angle_hor_log, angle_vert_log) = (13.9677, 5.65098)
    (angle_inf_hor_log, angle_inf_vert_log) = (-15.05, -0.31)

    angle_h = -angle_hor_log
    angle_v = -angle_vert_log

    angle_inf_h = -angle_inf_hor_log
    angle_inf_v = -angle_inf_vert_log

    in_wav_path = r'./data/_rameses/'
    out_wav_path   = r'./data/out/'

    _noise_start   = 26
    _noise_end     = 60

    _mix_start     = 26
    _mix_end       = 60
    #################################################################

    #################################################################
    # 1.0 - Read signal
    x_all_arr, sr = read_mic_wav_from_folder(in_wav_path, vert_mic_count, hor_mic_count, max_len_sec = max_len_sec)
    (n_channels, n_samples) = x_all_arr.shape

    print ("Array data read done!")
    print ("    n_channels  = ", n_channels)
    print ("    n_samples   = ", n_samples)
    print ("    freq        = ", sr)

    x_noise_arr = x_all_arr[:,(np.int32)(_noise_start*sr):(np.int32)(_noise_end*sr)]
    x_mix_arr   = x_all_arr[:,(np.int32)(_mix_start*sr):(np.int32)(_mix_end*sr)]

    print ("Array data read done!")
    print ("    x_noise_arr.shape  = ", x_noise_arr.shape)
    print ("    x_mix_arr.shape    = ", x_mix_arr.shape)

    #################################################################
    # 2 - Do STFT
    stft_noise_arr =  stft_arr(x_noise_arr, fftsize = n_fft)
    stft_mix_arr   =  stft_arr(x_mix_arr, fftsize = n_fft)

    (n_bins, n_sensors, n_frames) = stft_noise_arr.shape

    print ("STFT calc done!")
    print ("    n_bins               = ", n_bins)
    print ("    n_sensors            = ", n_sensors)
    print ("    stft_noise_arr.shape = ", stft_noise_arr.shape)
    print ("    stft_mix_arr.shape   = ", stft_mix_arr.shape)

    #################################################################
    # 3 - Calc  steering vector
    print ('Calc  steering vector!')
    print ('    (angle_h, angle_v) = ', angle_h, angle_v)
    print ('    (angle_inf_h, angle_inf_v) = ', angle_inf_h, angle_inf_v)
    sensor_positions    = get_sensor_positions(hor_mic_count, vert_mic_count, dHor=dHor, dVert=dVert)
    source_position     = get_source_position(angle_h, angle_v, radius=6.0)
    source_position_inf = get_source_position(angle_inf_h, angle_inf_v, radius=6.0)

    d_arr               = propagation_vector_free_field(sensor_positions, source_position, N_fft = n_fft, F_s = sr)
    d_arr_inf           = propagation_vector_free_field(sensor_positions, source_position_inf, N_fft = n_fft, F_s = sr)

    #################################################################
    # 4 - LCMV filter output

    #result_spec, _  = null_filter(stft_mix_arr, d_arr_sp=d_arr.T, d_arr_inf=d_arr_inf.T)
    result_spec, lcmv_weights  = lcmv_filter(stft_mix_arr, stft_noise_arr, d_arr_sp=d_arr.T, d_arr_inf=d_arr_inf.T)

    #################################################################
    # 5 inverse STFT and save
    sig_out = istft(result_spec.transpose((1,0)), overlap = 2)
    sf.write(r"out/out_LCMV.wav", sig_out, sr)

    #################################################################
    # 6.1 - Do align save ds output sp
    align_stft_arr = ds_align(stft_mix_arr, d_arr.T)
    result_spec    = align_stft_arr.sum(axis=1) / (hor_mic_count * vert_mic_count)
    sig_out = istft(result_spec.transpose((1, 0)), overlap=2)
    sf.write(r"out/ds_sp.wav", sig_out, sr)

    #################################################################
    # 6.2 - Do align save ds output inf
    align_stft_arr = ds_align(stft_mix_arr, d_arr_inf.T)
    result_spec = align_stft_arr.sum(axis=1) / (hor_mic_count * vert_mic_count)
    sig_out = istft(result_spec.transpose((1, 0)), overlap=2)
    sf.write(r"out/ds_inf.wav", sig_out, sr)


"""
Author: Jing (https://github.com/gnijuohz)

Populating Next Right Pointers in Each Node II: https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii

Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?

Note:
You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL
Tags
Tree, Depth-first Search
"""

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#         self.next = None

class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        if not root:
            return
        if root.left:
           root.left.next = root.right if root.right else self.findNext(root)
        if root.right:
            root.right.next = self.findNext(root)
        self.connect(root.right)
        self.connect(root.left)

    def findNext(self, node):
        node = node.next
        while node and not node.left and not node.right:
            node = node.next
        if node and (node.left or node.right):
            return node.left if node.left else node.right
        return None


import tempfile
import pytest
import time
import pprint
import os

from gtmcore.inventory.inventory import InventoryManager
from gtmcore.fixtures import mock_config_file
from gtmcore.labbook import SecretStore, SecretStoreException
from gtmcore.configuration import Configuration

def init():
    im = InventoryManager()
    lb = im.create_labbook('test', 'test', 'labbook1')
    return SecretStore(lb, 'test')

class TestLabbookSecret(object):
    def test_init(self, mock_config_file):
        """
        Test a simple secret store that is empty.
        """
        secstore = init()
        assert len(secstore) == 0
        assert [x for x in secstore] == []

    def test_create_registry_void_of_files(self, mock_config_file):
        secstore = init()
        mnt_target = '/opt/.ssh'

        secstore['ID_SSH.KEY'] = mnt_target
        secstore['ID_SSH.PUB'] = mnt_target
        secstore['PUBKEY.DAT'] = '/var/tmp'

        print('xxxxxxx', [x for x in secstore.list_files()])

        # Assert all files are not found, but exist in registry.
        assert all([e[1] == False for e in secstore.list_files()])
        assert len(secstore.list_files()) == 3

        del secstore['PUBKEY.DAT']
        assert len(secstore.list_files()) == 2

        del secstore['ID_SSH.KEY']
        assert len(secstore.list_files()) == 1

        del secstore['ID_SSH.PUB']
        assert len(secstore.list_files()) == 0

    def test_insert_file_delete_files_list_files(self, mock_config_file):
        secstore = init()
        mnt_target = '/opt/.ssh'

        secstore['ID_SSH.KEY'] = mnt_target
        secstore['ID_SSH.PUB'] = mnt_target

        config = Configuration()
        server_id = config.get_current_server_id()

        with tempfile.TemporaryDirectory() as tempdir:
            with open(os.path.join(tempdir, 'ID_SSH.KEY'), 'w') as t1:
                t1.write('<<SUPER SECRET PRIVATE KEY>>')
            with open(os.path.join(tempdir, 'ID_SSH.PUB'), 'w') as t2:
                t2.write('((NOT SO SECRET PUBLIC KEY))')

            keyfile_dst_1 = secstore.insert_file(t1.name)
            keyfile_dst_2 = secstore.insert_file(t2.name)

        parts = [secstore.labbook.client_config.app_workdir, '.labmanager',
                 'secrets', server_id, 'test', secstore.labbook.owner, secstore.labbook.name,
                 os.path.basename(keyfile_dst_1)]
        assumed_path = os.path.join(*parts)
        assert assumed_path == keyfile_dst_1

        # But properly return the two inserted files for the correct keys
        assert secstore.list_files() == [('ID_SSH.KEY', True), ('ID_SSH.PUB', True)]

        # Test that delete removes a file properly by using list_files
        secstore.delete_file('ID_SSH.KEY')
        assert secstore.list_files() == [('ID_SSH.KEY', False), ('ID_SSH.PUB', True)]

        # Deleting from the registry should ALSO delete the actual file.
        del secstore['ID_SSH.PUB']
        assert secstore.list_files() == [('ID_SSH.KEY', False)]

        # Test clear_files works by making sure the vault itself doesn't exist
        secstore.clear_files()
        toks = [secstore.labbook.client_config.app_workdir, '.labmanager', 'secrets', server_id,
                'test', secstore.labbook.owner, secstore.labbook.name]
        assert not os.path.exists(os.path.join(*toks))

    def test_insert_must_be_for_valid_file(self, mock_config_file):
        secstore = init()
        mnt_target = '/opt/.ssh'

        with tempfile.TemporaryDirectory() as tempdir:
            with open(os.path.join(tempdir, 'ID_SSH.KEY'), 'w') as t1:
                t1.write('CORRECT_DATA')

            # Raises this exception because we have not yet made
            # an entry to account for this file.
            with pytest.raises(SecretStoreException):
                secstore.insert_file(t1.name)

    def test_insert_cannot_overwrite(self, mock_config_file):
        """
        Confirm that you cannot overwrite a given file.
        """
        secstore = init()
        mnt_target = '/opt/.ssh'

        secstore['ID_SSH.KEY'] = mnt_target

        with tempfile.TemporaryDirectory() as tempdir:
            with open(os.path.join(tempdir, 'ID_SSH.KEY'), 'w') as t1:
                t1.write('CORRECT_DATA')
            keyfile_dst_1 = secstore.insert_file(t1.name)

        with tempfile.TemporaryDirectory() as tempdir:
            with open(os.path.join(tempdir, 'ID_SSH.KEY'), 'w') as t2:
                t2.write('BAD_DATA')
            with pytest.raises(SecretStoreException):
                keyfile_dst_2 = secstore.insert_file(t2.name)

        assert secstore.list_files() == [('ID_SSH.KEY', True)]
        assert open(keyfile_dst_1).read() == 'CORRECT_DATA'
        assert len(secstore.as_mount_dict) == 1

    def test_remove_keyfile_not_existing(self, mock_config_file):
        secstore = init()
        mnt_target = '/opt/.ssh'

        with pytest.raises(SecretStoreException):
            secstore.delete_file('catfile')

        assert secstore.list_files() == []
        assert len(secstore.as_mount_dict) == 0

    def test_clean(self, mock_config_file):
        secstore = init()
        mnt_target = '/opt/.ssh'
        secstore['ID_SSH.KEY'] = mnt_target
        with tempfile.TemporaryDirectory() as tempdir:
            with open(os.path.join(tempdir, 'ID_SSH.KEY'), 'w') as t1:
                t1.write('CORRECT_DATA')
            keyfile_dst_1 = secstore.insert_file(t1.name)

        del secstore['ID_SSH.KEY']
        with open(os.path.join(os.path.dirname(keyfile_dst_1), 'badfile'), 'w') as bf:
            bf.write('This file must get cleaned.')

        # Even though an incorrect file is in there, it should not be listed.
        assert len(secstore.list_files()) == 0

        # Assert that _clean removes this extraneous file.
        secstore2 = SecretStore(secstore.labbook, secstore.username)
        assert not os.path.exists(bf.name)


# 입력 받은 문자의 최대 최소

enteredNumber=[]
total = 0
numberOf = 0;
while True:
	number = input('Enter a number:')
	try :
		userInput = int(number)
		total += userInput
		numberOf += 1
		average = total/numberOf
		enteredNumber+= number
	except:
		if number == 'done':
			print('Done','Total: ', total, 'NumberOF', numberOf, 'Avergae:' , average, 'Max', max(enteredNumber), 'Min', min(enteredNumber))
			break

def max(list):
	largest = None
	for number in list:
		if largest is None or largest < number:
			largest = number
	return largest

def min(list):
	smallist = None
	for number in list:
		if smallist is None or smallist > number:
			smallist = number
	return largest


from django.conf import settings
from django.core.cache import cache
from django.db.models import Min
from django.utils.translation import ugettext_lazy as _
from evap.evaluation.models import LikertAnswer, TextAnswer, GradeAnswer

from collections import OrderedDict, defaultdict
from collections import namedtuple

LIKERT_NAMES = {
    1: _(u"Strongly agree"),
    2: _(u"Agree"),
    3: _(u"Neither agree nor disagree"),
    4: _(u"Disagree"),
    5: _(u"Strongly disagree"),
    6: _(u"no answer"),
}

GRADE_NAMES = {
    1: _(u"1"),
    2: _(u"2"),
    3: _(u"3"),
    4: _(u"4"),
    5: _(u"5"),
    6: _(u"no answer"),
}

# the names used for contributors and staff
STATES_ORDERED = OrderedDict((
    ('new', _('new')),
    ('prepared', _('prepared')),
    ('lecturerApproved', _('lecturer approved')),
    ('approved', _('approved')),
    ('inEvaluation', _('in evaluation')),
    ('evaluated', _('evaluated')),
    ('reviewed', _('reviewed')),
    ('published', _('published'))
))

# the names used for students
STUDENT_STATES_ORDERED = OrderedDict((
    ('inEvaluation', _('in evaluation')),
    ('upcoming', _('upcoming')),
    ('evaluationFinished', _('evaluation finished')),
    ('published', _('published'))
))

# see calculate_results
ResultSection = namedtuple('ResultSection', ('questionnaire', 'contributor', 'results', 'average_likert', 'median_likert', 'average_grade', 'median_grade', 'average_total', 'median_total', 'warning'))
LikertResult = namedtuple('LikertResult', ('question', 'count', 'average', 'median', 'variance', 'distribution', 'show', 'warning'))
TextResult = namedtuple('TextResult', ('question', 'texts'))
GradeResult = namedtuple('GradeResult', ('question', 'count', 'average', 'median', 'variance', 'distribution', 'show', 'warning'))

def avg(iterable):
    """Simple arithmetic average function. Returns `None` if the length of
    `iterable` is 0 or no items except None exist."""
    items = [item for item in iterable if item is not None]
    if len(items) == 0:
        return None
    return float(sum(items)) / len(items)

def med(iterable):
    """Simple arithmetic median function. Returns `None` if the length of
    `iterable` is 0 or no items except None exist."""
    items = [item for item in iterable if item is not None]
    length = len(items)
    if length == 0:
        return None
    sorted_items = sorted(items)
    index = int(length / 2)
    if length % 2 == 0:
        return (sorted_items[index] + sorted_items[index]) / 2.0
    return sorted_items[index]

def mix(a, b, alpha):
    if a is None and b is None:
        return None
    if a is None:
        return b
    if b is None:
        return a

    return alpha * a + (1 - alpha) * b

def get_answers(course, contribution, question):
    answers = None

    if question.is_likert_question():
        answers = LikertAnswer.objects.filter(
            contribution__course=course,
            contribution__contributor=contribution.contributor,
            question=question
            ).values_list('answer', flat=True)

    elif question.is_grade_question():
        answers = GradeAnswer.objects.filter(
            contribution__course=course,
            contribution__contributor=contribution.contributor,
            question=question
            ).values_list('answer', flat=True)

    elif question.is_text_question():
        answers = TextAnswer.objects.filter(
            contribution__course=course,
            contribution__contributor=contribution.contributor,
            question=question,
            hidden=False
            )

    return answers

def calculate_results(course, staff_member=False):
    """Calculates the result data for a single course. Returns a list of
    `ResultSection` tuples. Each of those tuples contains the questionnaire, the
    contributor (or None), a list of single result elements, the average and
    median grades for that section (or None). The result elements are either
    `LikertResult`, `TextResult` or `GradeResult` instances."""

    # return cached results if available
    cache_key = str.format('evap.staff.results.views.calculate_results-{:d}-{:d}', course.id, staff_member)
    prior_results = cache.get(cache_key)
    if prior_results:
        return prior_results

    # check if grades for the course will be published
    show = staff_member or course.can_publish_grades()

    # there will be one section per relevant questionnaire--contributor pair
    sections = []

    # calculate the median values of how many people answered a questionnaire type (lecturer, tutor, ...)
    questionnaire_med_answers = {}
    questionnaire_max_answers = {}
    for questionnaire, contribution in questionnaires_and_contributions(course):
        if questionnaire not in questionnaire_med_answers:
            questionnaire_med_answers[questionnaire] = []
        max_answers = 0
        for question in questionnaire.question_set.all():
            # don't count text questions, because few answers here should not result in warnings and having a median of 0 prevents a warning
            if not question.is_text_question():
                answers = get_answers(course, contribution, question)
                if len(answers) > max_answers:
                    max_answers = len(answers)
        questionnaire_max_answers[(questionnaire, contribution)] = max_answers
        questionnaire_med_answers[questionnaire].append(max_answers)
    for questionnaire in questionnaire_med_answers:
        questionnaire_med_answers[questionnaire] = med(questionnaire_med_answers[questionnaire])

    for questionnaire, contribution in questionnaires_and_contributions(course):
        # will contain one object per question
        results = []
        for question in questionnaire.question_set.all():
            if question.is_likert_question() or question.is_grade_question():
                answers = get_answers(course, contribution, question)

                # calculate average, median and distribution
                if answers:
                    # average
                    average = avg(answers)
                    # median
                    median = med(answers)
                    # variance
                    variance = avg((average - answer) ** 2 for answer in answers)
                    # calculate relative distribution (histogram) of answers:
                    # set up a sorted dictionary with a count of zero for each grade
                    distribution = OrderedDict()
                    for i in range(1, 6):
                        distribution[i] = 0
                    # count the answers
                    for answer in answers:
                        distribution[answer] += 1
                    # divide by the number of answers to get relative 0..1 values
                    for k in distribution:
                        distribution[k] = float(distribution[k]) / len(answers) * 100.0
                else:
                    average = None
                    median = None
                    variance = None
                    distribution = None

                warning = len(answers) > 0 and len(answers) < settings.RESULTS_WARNING_PERCENTAGE * questionnaire_med_answers[questionnaire]
                # produce the result element
                kwargs = {
                    'question': question,
                    'count': len(answers),
                    'average': average,
                    'median': median,
                    'variance': variance,
                    'distribution': distribution,
                    'show': show,
                    'warning': warning
                }
                if question.is_likert_question():
                    results.append(LikertResult(**kwargs))
                else:
                    results.append(GradeResult(**kwargs))
            elif question.is_text_question():
                answers = get_answers(course, contribution, question)

                # only add to the results if answers exist at all
                if answers:
                    results.append(TextResult(
                        question=question,
                        texts=[answer.answer for answer in answers]
                    ))

        # skip section if there were no questions with results
        if not results:
            continue

        # compute average and median grades for all LikertResults in this
        # section, will return None if no LikertResults exist in this section
        average_likert = avg([result.average for result in results if isinstance(result, LikertResult)])
        median_likert = med([result.median for result in results if isinstance(result, LikertResult)])

        # compute average and median grades for all GradeResults in this
        # section, will return None if no GradeResults exist in this section
        average_grade = avg([result.average for result in results if isinstance(result, GradeResult)])
        median_grade = med([result.median for result in results if isinstance(result, GradeResult)])

        average_total = mix(average_grade, average_likert, settings.GRADE_PERCENTAGE)
        median_total = mix(median_grade, median_likert, settings.GRADE_PERCENTAGE)

        max_answers_this_questionnaire = questionnaire_max_answers[(questionnaire, contribution)]
        med_answers_this_questionnaire_type = questionnaire_med_answers[questionnaire]
        warning_threshold = settings.RESULTS_WARNING_PERCENTAGE * med_answers_this_questionnaire_type
        section_warning = med_answers_this_questionnaire_type > 0 and max_answers_this_questionnaire < warning_threshold

        sections.append(ResultSection(
            questionnaire, contribution.contributor, results,
            average_likert, median_likert,
            average_grade, median_grade,
            average_total, median_total,
            section_warning))

    # store results into cache
    # XXX: What would be a good timeout here? Once public, data is not going to
    #      change anyway.
    cache.set(cache_key, sections, 24 * 60 * 60)

    return sections

def calculate_average_and_medium_grades(course):
    """Determines the final average and median grades for a course."""
    avg_generic_likert = []
    avg_contribution_likert = []
    med_generic_likert = []
    med_contribution_likert = []
    avg_generic_grade = []
    avg_contribution_grade = []
    med_generic_grade = []
    med_contribution_grade = []

    for questionnaire, contributor, results, average_likert, median_likert, average_grade, median_grade, average_total, median_total, warning in calculate_results(course):
        if average_likert:
            (avg_contribution_likert if contributor else avg_generic_likert).append(average_likert)
        if median_likert:
            (med_contribution_likert if contributor else med_generic_likert).append(median_likert)
        if average_grade:
            (avg_contribution_grade if contributor else avg_generic_grade).append(average_grade)
        if median_grade:
            (med_contribution_grade if contributor else med_generic_grade).append(median_grade)

    # the final total grade will be calculated by the following formula (GP = GRADE_PERCENTAGE, CP = CONTRIBUTION_PERCENTAGE):
    # final_likert = CP * likert_answers_about_persons + (1-CP) * likert_answers_about_courses
    # final_grade = CP * grade_answers_about_persons + (1-CP) * grade_answers_about_courses
    # final = GP * final_grade + (1-GP) * final_likert

    final_likert_avg = mix(avg(avg_contribution_likert), avg(avg_generic_likert), settings.CONTRIBUTION_PERCENTAGE)
    final_likert_med = mix(med(med_contribution_likert), med(med_generic_likert), settings.CONTRIBUTION_PERCENTAGE)
    final_grade_avg = mix(avg(avg_contribution_grade), avg(avg_generic_grade), settings.CONTRIBUTION_PERCENTAGE)
    final_grade_med = mix(med(med_contribution_grade), med(med_generic_grade), settings.CONTRIBUTION_PERCENTAGE)

    final_avg = mix(final_grade_avg, final_likert_avg, settings.GRADE_PERCENTAGE)
    final_med = mix(final_grade_med, final_likert_med, settings.GRADE_PERCENTAGE)

    return final_avg, final_med

def questionnaires_and_contributions(course):
    """Yields tuples of (questionnaire, contribution) for the given course."""
    result = []

    for contribution in course.contributions.all():
        for questionnaire in contribution.questionnaires.all():
            result.append((questionnaire, contribution))

    # sort questionnaires for general contributions first
    result.sort(key=lambda t: not t[1].is_general)

    return result

def is_external_email(email):
    return not any([email.endswith("@" + domain) for domain in settings.INSTITUTION_EMAIL_DOMAINS])

def user_publish_notifications(courses):
    user_notifications = defaultdict(set)
    for course in courses:
        # for published courses all contributors and participants get a notification
        if course.can_publish_grades():
            for participant in course.participants.all():
                user_notifications[participant].add(course)
            for contribution in course.contributions.all():
                if contribution.contributor:
                    user_notifications[contribution.contributor].add(course)
        # if a course was not published notifications are only sent for contributors who can see comments
        elif len(course.textanswer_set) > 0:
            for textanswer in course.textanswer_set:
                if textanswer.contribution.contributor:
                    user_notifications[textanswer.contribution.contributor].add(course)
            user_notifications[course.responsible_contributor].add(course)

    return user_notifications


#!/usr/bin/env python3

from sys import argv

#ip = input('Enter ip/mask in format as 10.1.1.0/24: ')
ip = argv[1]
network=ip.split('/')[0]
mask=ip.split('/')[1]
network=network.split('.')
mask1='1'*int(mask) + '0'*(32-int(mask))
mask_counter = 32-int(mask)
count = mask_counter / 8
count_ip = 4-count
subnet = '.'.join(network[:int(count_ip)]) + '.0'*int(count)
print(subnet)
subnet=subnet.split('.')
print(f'Network:\n {subnet[0]:<8} {subnet[1]:<8} {subnet[2]:<8} {subnet[3]:<8}\n {int(subnet[0]):<08b} {int(subnet[1]):<08b} {int(subnet[2]):<08b} {int(subnet[3]):<08b}')

print(f'Mask:\n {int(mask1[0:8],2):<8} {int(mask1[8:16],2):<8} {int(mask1[16:24],2):<8} {int(mask1[24:32],2):<8}\n {mask1[0:8]:<8} {mask1[8:16]:<8} {mask1[16:24]:<8} {mask1[24:32]:<8}')


import cv2

_CAMERA_WIDTH = 320  # 攝影機擷取影像寬度
_CAMERA_HEIGH = 240  # 攝影機擷取影像高度

video_name = 'video_'  # 儲存影片名稱
file_type = '.mov'  # 儲存影片副檔名

cap = cv2.VideoCapture(0)
# 設定擷取影像的尺寸大小
cap.set(cv2.CAP_PROP_FRAME_WIDTH, _CAMERA_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, _CAMERA_HEIGH)

fourcc = cv2.VideoWriter_fourcc(*'avc1')
FPS = 30  # 擷取影片頻率

write_flag = 0  # 判斷是否為寫入模式
video_counter = 0  # 計數儲存影片數量

while (cap.isOpened()):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame.copy(), cv2.COLOR_RGB2GRAY)
    if ret == True:
        if cv2.waitKey(10) & 0xFF == ord('r') and write_flag == 0:  # 寫入影格
            write_flag = 1
            save_name = video_name + str(video_counter) + file_type
            out = cv2.VideoWriter(save_name, fourcc, FPS, (_CAMERA_WIDTH, _CAMERA_HEIGH))
            print('writing to ' + save_name)
        elif cv2.waitKey(10) & 0xFF == ord('t') and write_flag == 1:  # 關閉影片
            write_flag = 0
            video_counter = video_counter + 1
            print('finish')
        elif cv2.waitKey(10) & 0xFF == ord('s'):  # 釋放所有資源
            cap.release()
            out.release()
            cv2.destroyAllWindows()
            break

        if (write_flag == 1):
            out.write(frame)

        cv2.imshow('frame', frame)
    else:
        break


import os
import io
import sys

from setuptools import setup
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext

class build_ext_with_numpy(build_ext):
    def run(self):
        import numpy
        self.include_dirs.append(numpy.get_include())
        build_ext.run(self)

extensions = [Extension("gb_fast_histogram._histogram_core",
                        [os.path.join('gb_fast_histogram', '_histogram_core.c')])]

with io.open('README.rst', encoding='utf-8') as f:
    LONG_DESCRIPTION = f.read()

try:
    import numpy
except ImportError:
    # We include an upper limit to the version because setup_requires is
    # honored by easy_install not pip, and the former doesn't ignore pre-
    # releases. It's not an issue if the package is built against 1.15 and
    # then 1.16 gets installed after, but it still makes sense to update the
    # upper limit whenever a new version of Numpy is released.
    setup_requires = ['numpy<1.16']
else:
    setup_requires = []

setup(name='gb-fast-histogram',
      version='0.1gb0',
      description='Fast simple 1D and 2D histograms',
      long_description=LONG_DESCRIPTION,
      setup_requires=setup_requires ,
      install_requires=['numpy'],
      author='Thomas Robitaille',
      author_email='thomas.robitaille@gmail.com',
      license='BSD',
      url='https://github.com/GameBench/gb-fast-histogram',
      packages=['gb_fast_histogram', 'gb_fast_histogram.tests'],
      ext_modules=extensions,
      cmdclass={'build_ext': build_ext_with_numpy})


from graph import *
import math

def background():
	penColor(0, 255, 255)
	brushColor('PaleTurquoise')
	rectangle(0,0, 500,300)
	brushColor('ForestGreen')
	rectangle(0,300, 500,600)
background()
penColor(0,0,0)

def house(x, y, z):
	'''
	 z - difference
	'''
	brushColor('MediumOrchid')
	rectangle(x,y, x+z*4, y+z*3)
	brushColor('Plum')
	polygon([(x,y), (x+z*2,y-z*2), (x+z*4, y)])
	brushColor('Thistle')
	rectangle(x+z*1.5,y+z, x+z*2.5,y+z*2)

house(50, 250, 50)

def cloud(x, y, radius, z):
	penColor('Gray')
	brushColor('White')
	for i in range(4):
		circle(x,y,radius)
		x += z*3
	for i in range(2):
		circle(x-z*6,y-z*2,radius)
		x -= z*3
cloud(230,120,30,10)

def wood(x, y, radius, z):
	penSize(20)
	penColor('Black')
	moveTo(x,y+z*2)
	lineTo(x,y+z*6)
	penSize(1)
	brushColor('DarkGreen')
	for i in range(2):
		circle(x,y,radius)
		circle(x-z,y+z,radius)
		circle(x+z,y+z,radius)
		y+=40
		z-=5
wood(430,200,30,30)

def sun(x, y, r, rs):
	brushColor('PaleGoldenrod')
	polygon([(x + r*math.cos(0.5), y + r*math.sin(0.5)),
	(x + rs*math.cos(0.75), y + rs*math.sin(0.75)),
	(x + r*math.cos(1), y + r*math.sin(1)),
	(x + rs*math.cos(1.25), y + rs*math.sin(1.25)),
	(x + r*math.cos(1.5), y + r*math.sin(1.5)),
	(x + rs*math.cos(1.75), y + rs*math.sin(1.75)),
	(x + r*math.cos(2), y + r*math.sin(2)),
	(x + rs*math.cos(2.25), y + rs*math.sin(2.25)),
	(x + r*math.cos(2.5), y + r*math.sin(2.5)),
	(x + rs*math.cos(2.75), y + rs*math.sin(2.75)),
	(x + r*math.cos(3), y + r*math.sin(3)),
	(x + rs*math.cos(3.25), y + rs*math.sin(3.25)),
	(x + r*math.cos(3.5), y + r*math.sin(3.5)),
	(x + rs*math.cos(3.75), y + rs*math.sin(3.75)),
	(x + r*math.cos(4), y + r*math.sin(4)),
	(x + rs*math.cos(4.25), y + rs*math.sin(4.25)),
	(x + r*math.cos(4.5), y + r*math.sin(4.5)),
	(x + rs*math.cos(4.75), y + rs*math.sin(4.75)),
	(x + r*math.cos(5), y + r*math.sin(5)),
	(x + rs*math.cos(5.25), y + rs*math.sin(5.25)),
	(x + r*math.cos(5.5), y + r*math.sin(5.5)),
	(x + rs*math.cos(5.75), y + rs*math.sin(5.75)),
	(x + r*math.cos(6), y + r*math.sin(6)),
	(x + rs*math.cos(6.25), y + rs*math.sin(6.25)),
	(x + r*math.cos(6.5), y + r*math.sin(6.5)),
	(x + rs*math.cos(6.75), y + rs*math.sin(6.75)),
	(x + r*math.cos(0.5), (y + r*math.sin(0.5)))
	])
sun(450, 50, 30, 35)

run()


import argparse
import xmltodict as x2d
from collections import OrderedDict
from utils.kitti2bdd import transform_kitti

"""
Usage:
kitti2bdd.py --annDir=<path> --imdir=<path> --xml=<template-xml-path> --save_path=<output>.xml [--gt]

If the annotation directory contains groundtruth annotations then use --gt
"""

def xml2dict(xml):
    if not xml:
        return {xml.tag: [xml.text, xml.attrib]}
    result = {}
    for child in xml:
        child_result = xml2dict(child)
        if child.tag != 'object':
            result[child.tag] = child_result[child.tag]
        else:
            if child.tag not in result:
                result[child.tag] = []
            result[child.tag].append(child_result[child.tag])
    return {xml.tag: result}

def parse_arguments():
    # parse the arguments
    parser = argparse.ArgumentParser(description='kitti to bdd')
    parser.add_argument(
        "--annDir",
        default="/path/to/kitti/label/files",
        help="path to directory containing kitti label files",
    )
    parser.add_argument(
        "--xml",
        default="/path/to/cvat_xml_template",
        help="path to cvat xml template",
    )
    parser.add_argument(
        "--save_path",
        default="/save/path",
        help="path to save bdd formatted label file",
    )
    parser.add_argument(
        "--imext",
        default=".png",
        help="image file extension",
    )
    parser.add_argument(
        "--gt",
        action='store_true',
        help="groundtruth annotation",
    )
    return parser.parse_args()

def bdd2cvat(xml_file, bdd):
    with open(xml_file, 'rb') as fid:
        xml_str = fid.read()
    xml = x2d.parse(xml_str)

    xml['annotations']['image'] = []
    all_labels = []
    for idx, elem in enumerate(bdd):
        # build bbox element
        object_elements = []
        for obj in elem['labels']:
            bbox2d = obj['box2d']
            all_labels.append(obj['category'])
            object_elements.append(OrderedDict([('@label', '{}'.format(obj['category'])),
                                                ('@xtl', '{}'.format(bbox2d['x1'])),
                                                ('@ytl', '{}'.format(bbox2d['y1'])),
                                                ('@xbr', '{}'.format(bbox2d['x2'])),
                                                ('@ybr', '{}'.format(bbox2d['y2'])),
                                                ('@occluded', '0')]))
        # TODO: load image to get the width and height

        # build image element
        image_element = OrderedDict([('@id', '{}'.format(idx)),
                                     ('@name', '{}'.format(elem['name'])),
                                     ('@width', '-1'),
                                     ('@height', '-1'),
                                     ('box', object_elements)
                                     ])
        xml['annotations']['image'].append(image_element)
    print(set(all_labels))
    return xml

def main():
    args = parse_arguments()
    print(args)
    bdd_labels = transform_kitti(args.annDir, args.imext, args.gt)
    xml = bdd2cvat(args.xml, bdd_labels)

    with open(args.save_path, 'w') as fid:
        x2d.unparse(xml, fid)

if __name__ == '__main__':
    main()


import logging
import os
import random
import sys
import time

import requests
from requests.exceptions import ChunkedEncodingError
from requests.exceptions import ConnectionError
from requests.exceptions import ReadTimeout

from http_request_randomizer.requests.errors.ProxyListException import ProxyListException
from http_request_randomizer.requests.parsers.FreeProxyParser import FreeProxyParser
from http_request_randomizer.requests.parsers.ProxyDbParser import ProxyDbParser
from http_request_randomizer.requests.parsers.ProxyForEuParser import ProxyForEuParser
from http_request_randomizer.requests.parsers.RebroWeeblyParser import RebroWeeblyParser
from http_request_randomizer.requests.parsers.SamairProxyParser import SamairProxyParser
from http_request_randomizer.requests.useragent.userAgent import UserAgentManager

__author__ = 'pgaref'
sys.path.insert(0, os.path.abspath('../../../../'))

# Push back requests library to at least warnings
logging.getLogger("requests").setLevel(logging.WARNING)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)-6s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)

class RequestProxy:
    def __init__(self, web_proxy_list=[], sustain=False, timeout=5):
        self.userAgent = UserAgentManager()
        self.logger = logging.getLogger()
        self.logger.addHandler(handler)
        self.logger.setLevel(0)

        #####
        # Each of the classes below implements a specific URL Parser
        #####
        parsers = list([])
        parsers.append(FreeProxyParser('http://free-proxy-list.net', timeout=timeout))
        parsers.append(ProxyDbParser('http://proxydb.net/?protocol=http&anonlvl=1&anonlvl=2', timeout=timeout))
        parsers.append(ProxyForEuParser('http://proxyfor.eu/geo.php', 1.0, timeout=timeout))
        parsers.append(RebroWeeblyParser('http://rebro.weebly.com', timeout=timeout))
        parsers.append(SamairProxyParser('http://samair.ru/proxy/time-01.htm', timeout=timeout))

        self.logger.debug("=== Initialized Proxy Parsers ===")
        for i in range(len(parsers)):
            self.logger.debug("\t {0}".format(parsers[i].__str__()))
        self.logger.debug("=================================")

        self.sustain = sustain
        self.parsers = parsers
        self.proxy_list = web_proxy_list
        for i in range(len(parsers)):
            try:
                self.proxy_list += parsers[i].parse_proxyList()
            except ReadTimeout:
                self.logger.warn("Proxy Parser: '{}' TimedOut!".format(parsers[i].url))
        self.current_proxy = self.randomize_proxy()

    def set_logger_level(self, level):
        self.logger.setLevel(level)

    def get_proxy_list(self):
        return self.proxy_list

    def generate_random_request_headers(self):
        headers = {
            "Connection": "close",  # another way to cover tracks
            "User-Agent": self.userAgent.get_random_user_agent()
        }  # select a random user agent
        return headers

    def randomize_proxy(self):
        if len(self.proxy_list) == 0:
            raise ProxyListException("list is empty")
        rand_proxy = random.choice(self.proxy_list)
        while not rand_proxy:
            rand_proxy = random.choice(self.proxy_list)
        self.current_proxy = rand_proxy
        return rand_proxy

    #####
    # Proxy format:
    # http://<USERNAME>:<PASSWORD>@<IP-ADDR>:<PORT>
    #####
    def generate_proxied_request(self, url, method="GET", params={}, data={}, headers={}, req_timeout=30):
        try:
            random.shuffle(self.proxy_list)
            # req_headers = dict(params.items() + self.generate_random_request_headers().items())

            req_headers = dict(params.items())
            req_headers_random = dict(self.generate_random_request_headers().items())
            req_headers.update(req_headers_random)

            if not self.sustain:
                self.randomize_proxy()

            headers.update(req_headers)

            self.logger.debug("Using proxy: {0}".format(str(self.current_proxy)))
            request = requests.request(method, url, proxies={"http": self.current_proxy},
                                       headers=headers, data=data, params=params, timeout=req_timeout)
            # Avoid HTTP request errors
            if request.status_code == 409:
                raise ConnectionError("HTTP Response [409] - Possible Cloudflare DNS resolution error")
            elif request.status_code == 403:
                raise ConnectionError("HTTP Response [403] - Permission denied error")
            elif request.status_code == 503:
                raise ConnectionError("HTTP Response [503] - Service unavailable error")
            print('RR Status {}'.format(request.status_code))
            return request
        except ConnectionError:
            try:
                self.proxy_list.remove(self.current_proxy)
            except ValueError:
                pass
            self.logger.debug("Proxy unreachable - Removed Straggling proxy: {0} PL Size = {1}".format(
                self.current_proxy, len(self.proxy_list)))
            self.randomize_proxy()
        except ReadTimeout:
            try:
                self.proxy_list.remove(self.current_proxy)
            except ValueError:
                pass
            self.logger.debug("Read timed out - Removed Straggling proxy: {0} PL Size = {1}".format(
                self.current_proxy, len(self.proxy_list)))
            self.randomize_proxy()
        except ChunkedEncodingError:
            try:
                self.proxy_list.remove(self.current_proxy)
            except ValueError:
                pass
            self.logger.debug("Wrong server chunked encoding - Removed Straggling proxy: {0} PL Size = {1}".format(
                self.current_proxy, len(self.proxy_list)))
            self.randomize_proxy()

if __name__ == '__main__':

    start = time.time()
    req_proxy = RequestProxy()
    print("Initialization took: {0} sec".format((time.time() - start)))
    print("Size: {0}".format(len(req_proxy.get_proxy_list())))
    print("ALL = {0} ".format(req_proxy.get_proxy_list()))

    test_url = 'http://ipv4.icanhazip.com'

    while True:
        start = time.time()
        request = req_proxy.generate_proxied_request(test_url)
        print("Proxied Request Took: {0} sec => Status: {1}".format((time.time() - start), request.__str__()))
        if request is not None:
            print("\t Response: ip={0}".format(u''.join(request.text).encode('utf-8')))
        print("Proxy List Size: {0}".format(len(req_proxy.get_proxy_list())))

        print("-> Going to sleep..")
        time.sleep(10)


#!/usr/bin/python3
import socket
import struct
import time

PORT = 1918

DATA_FORMAT = "28f" #'5fhH9h2B2I4H3hB'
LOG_TITLE = ("Time, Voltage 1, Voltage 2, Voltage 3, Pressure, Temperature, Lux, Accel_y, Accel_z, Accel_x,  " +
            "Gyro_x, Gyro_y, Gyro_z, Gps fix , Gps num, Gps lat, Gps lon, Gps height, Gps speed, Gps course, Gps hdop " +
            "Angle 1, Angle 2, Heading\n")
LOG_PATH = "/home/developer/git/cansat-2017-2018/GCS/log/telemetry_log/telemetry_log_"

class Data_source():
    def __init__(self, port=PORT, data_format=DATA_FORMAT, log_path=LOG_PATH, log_title=LOG_TITLE):
        self.port = port
        print(port)
        self.data_format = data_format
        self.log_path = log_path
        self.log_title = log_title
        self.last_message_time = 0

    def start(self):
        self.log = open(self.log_path + time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) + '.txt', 'w')
        self.log.write(self.log_title)
        self.sock = socket.socket(socket.AF_INET,
                                  socket.SOCK_DGRAM)
        self.sock.bind(('0.0.0.0', self.port))
        self.sock.settimeout(1)

    def change_log_path(self, log_path):
        self.log_path = log_path

    def read_data(self):
        return_data = []
        data, addr = self.sock.recvfrom(1024)
        return_data = list(struct.unpack('<' + self.data_format, data))

        if self.last_message_time >= return_data[0]:
            raise ValueError

        self.last_message_time = return_data[0]

        log_str = ''
        for i in return_data[:-1]:
            log_str = log_str + str(i) + ','
        log_str = log_str + str(return_data[-1]) + '\n'
        self.log.write(log_str)

        return return_data

    def stop(self):
        self.log.close()

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Only for test
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if __name__ == '__main__':
    print("start")
    obJ = Data_source()
    print("start")
    obJ.start()
    while True:
        try:
            data = obJ.read_data()
            print (data)
        except Exception as e:
            print(e)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++