"""

Steps to run:
python insured_info_scraper.py <account number>
Eg:
python insured_info_scraper.py 20011

Program written in Python 3

Program Output:
1 file:
Insured_Info_<account_num>.json - json file that contains the insured info details

Program Description:
Progam first fetches the ASP login page paramters - __VIEWSTATE, __VIEWSTATEGENERATOR,
etc and then inputs these paramters and the login credentials to login
Then the program stores cookie info and uses it along with the new page parameters
to access the quotes page.
The code then access the account details page by sending the account number
(retrieved as a command line argument) along with other parameter to get the
account details.
The insured information is then scraped and stored into a dictionary var which is
written into a json file

"""
import sys
import requests
from bs4 import BeautifulSoup
import json

# Main Function
def main():
    # Variable to store the json putput
    insured_information = {}
    # Getting the account number from the command line
    account_num = str(sys.argv[1])
    print("Account number entered:")
    print(account_num)
    # Login credentials
    credentials = dict()
    credentials['username'] = 'samplecsrtest'
    credentials['password'] = 'Ik vaari aa---123'
    print("Getting login session parameters to login")
    # Home page URL
    home_page_url = 'https://secure.financepro.net/financepro/default.aspx?company=deltafinance'
    # Storing the session info
    session = requests.Session()
    response = session.get(home_page_url)
    # Parsing the response using Beautiful Soup
    soup = BeautifulSoup(response.content, 'html.parser')
    # Storing 3 ASP web-page specific form parameters to use to login
    viewstate = soup.select('input[name=__VIEWSTATE]')[0]['value']
    viewstate_generator = soup.select('input[name=__VIEWSTATEGENERATOR]')[0]['value']
    event_validation = soup.select('input[name=__EVENTVALIDATION]')[0]['value']
    login_form_parameters = dict()
    login_form_parameters['__VIEWSTATE'] = viewstate
    login_form_parameters['__VIEWSTATEGENERATOR'] = viewstate_generator
    login_form_parameters['__EVENTVALIDATION'] = event_validation
    login_form_parameters['tblForm$txtUserName'] = credentials['username']
    login_form_parameters['tblForm$txtPassword'] = credentials['password']
    login_form_parameters['tblForm$btnLogin'] = 'Log In'
    login_form_parameters['tblForm$txtCompanyCode'] = 'deltafinance'
    # Storing the cookies post login
    response = session.post(home_page_url, login_form_parameters)
    cookies = session.cookies.get_dict()
    # Logging in
    response = requests.post(home_page_url, login_form_parameters)
    print("Logged in")
    print("Accessing the accounts page session paramaters to navigate to accounts page")
    accounts_url = 'https://secure.financepro.net/financepro/account/account.aspx'
    # Sending the same session cookies
    response = session.get(accounts_url, cookies=cookies)
    soup = BeautifulSoup(response.content, 'html.parser')
    # Getting the ASP accounts web page session paramters
    viewstate = soup.select('input[name=__VIEWSTATE]')[0]['value']
    viewstate_generator = soup.select('input[name=__VIEWSTATEGENERATOR]')[0]['value']
    event_validation = soup.select('input[name=__EVENTVALIDATION]')[0]['value']
    print("Parameters retrieved")
    # Storing paramters to get account details page
    account_info_params = dict()
    account_info_params['__VIEWSTATE'] = viewstate
    account_info_params['__VIEWSTATEGENERATOR'] = viewstate_generator
    account_info_params['__EVENTVALIDATION'] = event_validation
    account_info_params['fndAccountSearch$hdnName'] = 'hvalue'
    # Account number sent as input here
    account_info_params['fndAccountSearch$txtAccountNumber'] = account_num
    account_info_params['fndAccountSearch$txtAccountStatus'] = '0'
    account_info_params['fndAccountSearch$txtCompanyCheckType'] = 'paid'
    account_info_params['fndAccountSearch$btnFind'] = 'Find Account'
    # POST request to get account and insured details
    print("\nAccessing Account Details Page")
    response = requests.post(accounts_url, account_info_params, cookies=cookies)
    soup = BeautifulSoup(response.content, 'html.parser')
    # All insured information is stored in span tags
    insured_name = soup.find("span", {"id": "lblInsuredName"}).text
    print("\nInsured Details are:\n")
    print("Insured name:")
    print(insured_name)
    insured_information['Name'] = insured_name
    insured_address = soup.find("span", {"id": "lblInsuredAddress"})
    # Converting the
 tag into a new line char and then splitting the
    # address into its components
    insured_address = str(insured_address).replace("
", "\n")
    insured_address = insured_address.replace('', '')
    insured_address = insured_address.replace('', '')
    address_dict = dict()
    insured_address = insured_address.split("\n")
    address_dict['Line 1'] = insured_address[0]
    line2 = insured_address[1].split(" ")
    address_dict['City'] = line2[0][:-1]
    address_dict['State'] = line2[1]
    address_dict['Zip Code'] = line2[2]
    print("Insured address:")
    print("Address Line 1:")
    print(address_dict['Line 1'])
    print("City:")
    print(address_dict['City'])
    print("State:")
    print(address_dict['State'])
    print("Zip Code:")
    print(address_dict['Zip Code'])
    insured_information['Address'] = address_dict
    insured_telephone = soup.find("span", {"id": "lblInsuredPhone"}).text
    print("Insured telephone:")
    print(insured_telephone)
    insured_information['Telephone'] = insured_telephone
    # Writing the insured information into a json file
    file_name = 'Insured_Info_' + account_num + '.json'
    with open(file_name, 'w') as f:
        json.dump(insured_information, f)
    print("\nOutput File Created:")
    print(file_name)
    print("\nLogging off")
    # Log off page called with cookie info
    log_off_url = 'https://secure.financepro.net/financepro/logoff.aspx'
    response = requests.get(log_off_url, cookies=cookies)
    final_url = 'https://www.deltafinanceoftexas.com/'
    response = requests.get(final_url)

# Entry point of code
if __name__ == "__main__":
    main()


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

import base_classes as bc
from struct import pack, unpack

# FPGA Instruments
KERRIGAN = {
    'nickname'          : 'kerrigan',
    'name'              : 'Xilinx Virtex 5',
    'address'           : 0x55,
    }

# I2C Instruments
CHEN     = {
    'nickname'          : 'chen',
    'name'              : 'I2C Mux Thing',
    'address'           : 0x70,
    }
TRAXEX   = {
    'nickname'          : 'traxex',
    'name'              : 'Sensirion STS21 Temperature Sensor',
    'address'           : 0x4A,
    'mux_address'       : 0x04,
    }
XIN      = {
    'nickname'          : 'xin',
    'name'              : 'Sensirion STS21 Temperature Sensor',
    'address'           : 0x4A,
    'mux_address'       : 0x05,
    }

# GPIB Instruments
ARCEUS   = {
    'nickname'          : 'arceus',
    'name'              : 'Agilent 8753ES S-Parameter Network Analyzer',
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
MELOETTA = {
    'nickname'          : 'meloetta',
    'name'              : 'Hewlett-Packard 6623A System DC Power Supply',
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
XERNEAS  = {
    'nickname'          : 'xerneas',
    'name'              : 'Hewlett-Packard 4156A Precision Semiconductor Parameter Analyzer',
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }

# TCPIP Instruments
DARKRAI  = {
    'nickname'          : 'darkrai',
    'name'              : 'Agilent N9020A MXA Signal Analyzer',
    'socket'            : ('192.168.1.5', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
DEOXYS   = {
    'nickname'          : 'deoxys',
    'name'              : 'Agilent InfiniiVision MSO7104A Mixed Signal Oscilloscope',
    'socket'            : ('192.168.1.10', 5025),
    'get_byte_order'    : ':waveform:byteorder?',
    'byte_order_little' : 'LSBF',
    }
GENESECT = {
    'nickname'          : 'genesect',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.9', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
GIRATINA = {
    'nickname'          : 'giratina',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.8', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
HEATRAN  = {
    'nickname'          : 'heatran',
    'name'              : 'Agilent 16803A Logic Analyzer',
    'socket'            : ('192.168.1.11', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
HO_OH    = {
    'nickname'          : 'ho_oh',
    'name'              : 'Agilent N5182A MXG Vector Signal Generator',
    'socket'            : ('192.168.1.4', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
KYUREM   = {
    'nickname'          : 'kyurem',
    'name'              : 'Agilent N5183A MXG Analog Signal Generator',
    'socket'            : ('192.168.1.3', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
RAYQUAZA = {
    'nickname'          : 'rayquaza',
    'name'              : 'Agilent E4443A PSA Series Spectrum Analyzer',
    'socket'            : ('192.168.1.2', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
YVELTAL  = {
    'nickname'          : 'yveltal',
    'name'              : 'Agilent B2902A Precision Source/Measure Unit',
    'socket'            : ('192.168.1.7', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
ZYGARDE  = {
    'nickname'          : 'zygarde',
    'name'              : 'Agilent E5071C ENA Series Network Analyzer',
    'socket'            : ('192.168.1.6', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }

class Kerrigan(bc.FPGAInstrument):
    def __init__(self, aardvark):
        """Initialize the FPGA.

        :param Aardvark aardvark:
                An Aardvark object through which I2C commands are relayed.

        .. code-block:: python

            import microlab_instruments as mi

            aa = mi.Aardvark()
            kerrigan = mi.Kerrigan(aa)
        """
        self.DATA = KERRIGAN
        super(Kerrigan, self).__init__(aardvark=aardvark)

class Chen(bc.I2CMuxInstrument):
    def __init__(self, aardvark):
        """Initialize the I2C multiplexer.

        :param Aardvark aardvark:
            An Aardvark object through which I2C commands are relayed.

        .. code-block:: python

            import microlab_instruments as mi

            aa = mi.Aardvark()
            chen = mi.Chen(aa)
        """
        self.DATA = CHEN
        super(Chen, self).__init__(aardvark=aardvark)

class Traxex(bc.TempSensorInstrument):
    def __init__(self, aardvark, mux):
        """Initialize a Sensirion STS21 temperature sensor.

        :param Aardvark aardvark:
            An Aardvark object through which I2C commands are relayed.

        .. code-block:: python

            import microlab_instruments as mi

            aa = mi.Aardvark()
            traxex = mi.Traxex(aa)
            print traxex.read_temp()
        """
        self.DATA = TRAXEX
        super(Traxex, self).__init__(aardvark=aardvark, mux=mux)

class Xin(bc.TempSensorInstrument):
    def __init__(self, aardvark, mux):
        """Initialize a Sensirion STS21 temperature sensor.

        :param Aardvark aardvark:
            An Aardvark object through which I2C commands are relayed.

        .. code-block:: python

            import microlab_instruments as mi

            aa = mi.Aardvark()
            xin = mi.Xin(aa)
            print xin.read_temp()
        """
        self.DATA = XIN
        super(Xin, self).__init__(aardvark=aardvark, mux=mux)

class Arceus(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = ARCEUS
        super(Arceus, self).__init__(nickname=self.DATA['nickname'])

class Meloetta(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = MELOETTA
        super(Meloetta, self).__init__(nickname=self.DATA['nickname'])

class Xerneas(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = XERNEAS
        super(Xerneas, self).__init__(nickname=self.DATA['nickname'])

class Darkrai(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DARKRAI
        super(Darkrai, self).__init__(socket_pair=self.DATA['socket'])

class Deoxys(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DEOXYS
        super(Deoxys, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':waveform:byteorder msbfirst')
        self.write(':waveform:format word')
        self.write('*OPC')

    def _chop16(self, s):
        """A generator that, given a string, yields its 16-bit slices.

        :param str s:
            The string to be chopped
        :returns out:
            A two-character (16-bit) string.
        :rtype: str
        """
        n = 0
        while True:
            k = s[n:n+2]
            if not k:
                break
            yield k
            n += 2

    def _half_to_float(self, half):
        """Converts half-precision floating-point (16-bit) binary data to
        Python ``float``\ .

        :param str half:
            A 16-bit string to be converted to a Python float
        :returns out:
            The actual floating point number represented by the 16-bit string.
        :rtype: float

        This was copied from `fpmurphy`_

        .. _fpmurphy: http://fpmurphy.blogspot.com/2008/12/half-precision-floating-point-format_14.html
        """
        # Get byte order of input
        bo = '<' if self._is_little_endian() else '>'

        # Preliminary unpacking
        fmt = '{0}H'.format(bo)
        h = unpack(fmt, half)[0]

        # Pad 16 bits to 32 bits
        s = int((h >> 15) & 0x00000001)  # sign
        e = int((h >> 10) & 0x0000001F)  # exponent
        f = int(h         & 0x000003FF)  # fraction
        if e == 0x00:   # exponent is 0
            if f == 0x00:
                hpad = int(s << 31)
            else:
                while not (f & 0x00000400):
                    f <<= 1
                    e -= 1
                e += 1
                f &= ~0x00000400
        elif e == 0x1F: # exponent is 31
            if f == 0x00:
                hpad = int((s << 31) | 0x7F800000)
            else:
                hpad = int((s << 31) | 0x7F800000 | (f << 13))
        e = e + (127 - 15)
        f = f << 13
        hpad = int((s << 31) | (e << 23) | f)

        # struct.pack hack
        st = pack('I', hpad)
        out = unpack('f', st)
        return out

    def read_preamble(self):
        """Read the waveform preamble from Deoxys.  It contains the following
        metadata about the waveform data:

        :returns out:
        :rtype: dict
        """
        # TODO Combine write, preamble and data in one function
        # TODO Read :waveform:preamble
        #           format WORD this is two bytes for each data point
        #           type   :waveform:type?
        #           points :waveform:points? can be found in the header of :waveform:data?
        #           count  :acquire:count? averaging for one data point, etc
        #           xincrement
        #           xorigin
        #           xreference
        #           yincrement
        #           yorigin
        #           yreference
        # TODO Read :save:waveform:start I do not know how to transfer a file
        pass

    def compose_waveform_xy(self, waveform_y, waveform_preamble):
        """Compose the (x,y) data list according to the y data and preamble
        obtained from the instrument.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        # TODO Read :waveform:data
        #           :waveform:byteorder DONE
        #           :waveform:unsigned  DONE
        #           :waveform:format    DONE
        #           :waveform:source    channel | function | math | pod | bus | sbus
        #           :system:precision
        #           0x0000 hole
        #           0x0001 clipped low
        #           0xFFFF clipped high

        # TODO Need to adjust waveform_x for special values (clipped, etc)
        # TODO Need to adjust waveform_x according to preamble
        # TODO Need to create waveform_y according to preamble
        # TODO Need to compose X and Y values
        pass

    def ask_waveform_data(self):
        """A convenience function to query the waveform preamble and waveform
        data in one call.  Additionally, it also composes the (x,y) data list.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        self.write(':waveform:preamble?')
        waveform_preamble = self.read_preamble()
        self.write(':waveform:data?')
        waveform_y = self.read_ieee754()
        out = self.compose_waveform_xy(waveform_y, waveform_preamble)
        return out

class Genesect(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GENESECT
        super(Genesect, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Giratina(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GIRATINA
        super(Giratina, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Heatran(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HEATRAN
        super(Heatran, self).__init__(socket_pair=self.DATA['socket'])

class Ho_oh(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HO_OH
        super(Ho_oh, self).__init__(socket_pair=self.DATA['socket'])

class Kyurem(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = KYUREM
        super(Kyurem, self).__init__(socket_pair=self.DATA['socket'])

class Rayquaza(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = RAYQUAZA
        super(Rayquaza, self).__init__(socket_pair=self.DATA['socket'])

class Yveltal(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = YVELTAL
        super(Yveltal, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Zygarde(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = ZYGARDE
        super(Zygarde, self).__init__(socket_pair=self.DATA['socket'])


import json
import os
import boto3
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.resource("dynamodb")

def lambda_handler(event, context):
    logger.info(f"EVENT: {event}")

    statistics_table_name = os.environ["STATISTICS_TABLE_NAME"]
    statistics_table = dynamodb.Table(statistics_table_name)
    try:
        # Get the new item from the DynamoDB event
        for item in event["Records"]:
            new_item = item["dynamodb"]
            if "NewImage" not in new_item.keys():
                continue
            new_item = new_item["NewImage"]

            # Extract required attributes for data processing
            event_type = new_item.get("EventType", {}).get("S")
            event_details = new_item.get("EventDetails", {}).get("S")
            team_name = new_item.get("Team", {}).get("S")
            opponent_team_name = new_item.get("Opponent", {}).get("S")
            match_id = new_item.get("MatchID", {}).get("S")

            if not event_type or not team_name or not match_id or not event_details:
                return

            event_details = json.loads(event_details)
            # Calculate statistics based on the event_type
            if event_type == "goal":
                update_statistics(statistics_table, match_id, team_name, opponent_team_name, "total_goals_scored", 1)
                update_statistics(statistics_table, match_id, opponent_team_name, team_name,"total_goals_conceded", 1)
            elif event_type == "foul":
                update_statistics(statistics_table, match_id, team_name, opponent_team_name, "total_fouls", 1)
                update_statistics(statistics_table, match_id, opponent_team_name, team_name, "total_fouls", 0)

            update_match_result(statistics_table, team_name, opponent_team_name, match_id)

            response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id}, AttributesToGet=["Date"])
            if "Item" not in response or not response.get("Item", {}):
                date = new_item.get("Timestamp", {}).get("S")
                update_date(statistics_table, team_name, match_id, date)
                update_date(statistics_table, opponent_team_name, match_id, date)

    except Exception as e:
        print(f"Error processing data: {e}")
        raise e

def update_statistics(statistics_table, match_id, team_name, opponent_name, statistic_type, value):
    # Get the existing statistics for the team from the statistics table
    response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id})

    if "Item" not in response or not response.get("Item", {}):
        # If the team and match are not present in the statistics table, create a new entry
        item = {
            "TeamName": team_name,
            "MatchID": match_id,
            "Opponent": opponent_name,
            statistic_type: str(value)
        }
        statistics_table.put_item(Item=item)
    else:
        # If the team and match are already present in the statistics table, update the existing entry
        existing_item = response["Item"]
        existing_item[statistic_type] = str(int(existing_item.get(statistic_type, "0")) + value)
        statistics_table.put_item(Item=existing_item)

def update_match_result(statistics_table, team_name, opponent_name, match_id):
    response = statistics_table.get_item(Key={"TeamName": team_name, "MatchID": match_id})
    response = response.get("Item", {})
    goals_scored = response.get("total_goals_scored", "0")
    goals_conceded = response.get("total_goals_conceded", "0")

    # Calculate match result based on goals scored and conceded
    results = {
        1: "win",
        -1: "loss",
        0: "draw",
    }
    sign = lambda x: -1 if x < 0 else (1 if x > 0 else 0)
    result = sign(int(goals_scored) - int(goals_conceded))

    # Update match result for the team
    response = statistics_table.update_item(
        Key={"TeamName": team_name, "MatchID": match_id},
        UpdateExpression="SET #result = :result",
        ExpressionAttributeNames={"#result": "Result"},
        ExpressionAttributeValues={":result": results[result]}
    )

    # Update match result for the opponent team
    response = statistics_table.update_item(
        Key={"TeamName": opponent_name, "MatchID": match_id},
        UpdateExpression="SET #result = :result",
        ExpressionAttributeNames={"#result": "Result"},
        ExpressionAttributeValues={":result": results[-result]}
    )

def update_date(table, team_name, match_id, date):
    # Update the "Date" attribute in the StatisticsTable for the team and match_id
    table.update_item(
        Key={"TeamName": team_name, "MatchID": match_id},
        UpdateExpression="SET #dateAttr = :dateValue",
        ExpressionAttributeNames={"#dateAttr": "Date"},
        ExpressionAttributeValues={":dateValue": str(date)}
    )


import urllib.request
import datetime
import json

# 返回一个月内的cf场数和上/掉分情况
def get_CF_ContestCount(name):
    apiUrl = "https://codeforces.com/api/user.rating?handle=" + name

    try:
        page = urllib.request.urlopen(apiUrl, timeout=2000)
        s = page.read().decode('utf-8')
        contestsData = json.loads(s)['result']

        # 改为直接用rating变化的时间当做比赛时间
        # 由于rating变化时间一般延迟一天，放宽到32天
        lastTime=(datetime.timedelta(days=-32) +
           datetime.datetime.now()).timestamp()

        sum=0
        cnt=0
        for contest in contestsData:
            if contest['ratingUpdateTimeSeconds'] < lastTime:
                continue
            cnt += 1
            sum += contest['newRating'] - contest['oldRating']
        return [cnt, sum]

    except Exception as e:
        print(str(e))
        return [-1, -1]

if __name__ == "__main__":
    while(True):
        name=input("请输入要爬的ID:")
        print(get_CF_ContestCount(name))


# -*- coding: UTF-8 -*-
'''
Created on 2022年1月19日

@author: automan
'''

class Language(object):

    clanguage = {
        'PV_01_01'    :    "通知方法设定",
        'PV_01_02'    :    "通知  OFF/ON",
        'PV_01_03'    :    "通知",

        'PV_01_04'    :    "尚无配对的太阳能板。请先完成装置的配对。",

        'PV_01_05'    :    "储存",
        'PV_01_06'    :    "根据设定的时间每小时发送一次通知。",
        'PV_01_07'    :    "目标量",
        'PV_01_08'    :    "请输入1~9,999的数字。",
        'PV_01_09'    :    "低于该值时通知",
        'PV_01_10'    :    "开始时间",
        'PV_01_11'    :    "结束时间",
        'PV_01_12'    :    "完成",
        'PV_01_13'    :    "瓩⋅时",

        'PST_01_01'   :    "发电低下通知",
        'PST_01_02'   :    "太阳能发电模组 %s，今日 %s ~ %s 发电量： %s kWh。低于 %s kWh 的 %s%。",
        'PST_02_01'   :    "发电异常通知",
        'PST_02_02'   :    "太阳能发电模组 %s，过去 %s 分钟内发电状况异常，请检查设备状态。",
        'PST_03_01'   :    "群组发电异常通知",
        'PST_03_02'   :    "群组 %s 中，过去 %s 分钟内有部分太阳能发电模组发电状况异常，请检查设备状态。"
        }

    elanguage = {
        'PV_01_01' :   'PV Generation Alert',
        'PV_01_02' :   'Notification OFF/ON',
        'PV_01_03' :   'Notification',
        'PV_01_04' :   'There are no paired solar panels yet. Please complete the pairing of the appliance first.',
        'PV_01_05' :   'Save',
        'PV_01_06' :   'Send notifications every hour based on a set time.',
        'PV_01_07' :   'Goal',
        'PV_01_08' :   'Please enter a number between 1~9,999 kWh.',
        'PV_01_09' :   'Lower Bound',
        'PV_01_10' :   'Starting Time',
        'PV_01_11' :   'End Time',
        'PV_01_12' :   'Done',
        'PV_01_13' :  'kWh',
        'PST_01_01' :  'Low power generation notification',
        'PST_01_02' :  "Today %s ~ %s power generation: %s kWh. Below %s% of %s kWh.",
        'PST_02_01' :  'Abnormal power generation notification',
        'PST_02_02' :  'Abnormal power generation in the past %s minutes, please check your equipments.',
        'PST_03_01' :  'Abnormal group power generation notification',
        'PST_03_02' :  'Abnormal power generation in the group, %s, in the past %s minutes, please check your equipments.'
        }

    jlanguage = {
        'PV_01_01'    :    "通知方法设定",
        'PV_01_02'    :    "通知  OFF/ON",
        'PV_01_03'    :    "通知",

        'PV_01_04'    :    "尚无配对的太阳能板。请先完成装置的配对。",

        'PV_01_05'    :    "储存",
        'PV_01_06'    :    "根据设定的时间每小时发送一次通知。",
        'PV_01_07'    :    "目标量",
        'PV_01_08'    :    "请输入1~9,999的数字。",
        'PV_01_09'    :    "低于该值时通知",
        'PV_01_10'    :    "开始时间",
        'PV_01_11'    :    "结束时间",
        'PV_01_12'    :    "完成",
        'PV_01_13'    :    "瓩⋅时",

        'PST_01_01'   :    "发电低下通知",
        'PST_01_02'   :    "今日 %s ~ %s 发电量： %s kWh。低于 %s kWh 的 %s%。",
        'PST_02_01'   :    "发电异常通知",
        'PST_02_02'   :    "过去 %s 分钟内发电状况异常，请检查设备状态。",
        'PST_03_01'   :    "群组发电异常通知",
        'PST_03_02'   :    "群组 %s 中，过去 %s 分钟内有部分太阳能发电模组发电状况异常，请检查设备状态。"
        }

    def __init__(self):
        '''
        Constructor
        '''


import imdb
import json
from tqdm import tqdm

ia = imdb.IMDb()

DATA_NEEDED = False

with open("./imdb/movie_title-id.json", "r") as data:
    current_data = json.load(data)

with open('./movie_titles_list.json', "r") as movie_list:
    movies = json.load(movie_list)

if DATA_NEEDED:
    for i in tqdm(range(0, len(movies))):
        # searching the name
        name = movies[i]
        try:
            search = ia.search_movie(name)
            # getting the id
            id = search[0].movieID
            current_data[name]=[id]
        except Exception:
            continue

for movie_name in current_data:
    if len(current_data[movie_name]) > 0:
        current_data[movie_name] = current_data[movie_name][0]

with open("./imdB/movie_title-id.json", "w") as data_list:
    json.dump(current_data, data_list)


from unittest.mock import MagicMock
from uuid import uuid4

import pytest
from pytest import raises

from pydantic import ValidationError

from api.exceptions import InvalidParameterError
from api.schemas.output import (
    ConsultaProcessoOutput,
    ExtractDataOutput,
    ExtractDataSecondInstanceOutput,
    StatusSolicitacaoOutput
)

def test_valida_numero_solicitacao_valid():
    """Testa se a validação do UUID é bem-sucedida com um UUID válido."""
    valid_uuid = str(uuid4())
    ConsultaProcessoOutput(numero_solicitacao=valid_uuid)

@pytest.mark.parametrize("invalid_uuid", ["invalid_uuid_string"])
def test_valida_numero_solicitacao_invalid(invalid_uuid):
    """Testa se a validação do UUID falha com uma string inválida."""
    with MagicMock(side_effect=InvalidParameterError("Mocked error")):
        with raises(InvalidParameterError):
            ConsultaProcessoOutput(numero_solicitacao=invalid_uuid)

def test_valid_extract_data():
    """Testa se o modelo é válido com dados corretos."""
    valid_data = {
        'classe': 'Penal',
        'area': 'Criminal',
        'assunto': 'Roubo',
        'data_distribuicao': 'Sorteio',
        'juiz': 'Dr. João Silva',
        'valor_acao': '5000,00',
        'partes_processo': [],
        'lista_movimentacoes': []
    }
    ExtractDataOutput(**valid_data)

def test_valid_output():
    """Testa um output válido para ExtractDataSecondInstanceOutput."""
    valid_data = {
        "classe": "Recurso Penal",
        "area": "Criminal",
        "assunto": "Furto",
        "partes_processo": [],
        "lista_movimentacoes": []
    }
    output = ExtractDataSecondInstanceOutput(**valid_data)
    assert output.classe == "Recurso Penal"
    assert output.area == "Criminal"

def test_missing_required_field():
    """Testa a ausência de campos obrigatórios para ExtractDataSecondInstanceOutput."""
    invalid_data = {
        "classe": "Recurso Penal",
        "assunto": "Furto",
        "partes_processo": [],
        "lista_movimentacoes": []
    }
    with raises(ValidationError):
        ExtractDataSecondInstanceOutput(**invalid_data)

def test_valid_output_first_instance_only():
    """Testa um output válido para StatusSolicitacaoOutput com apenas dados da primeira instância."""
    valid_data = {
        "numero_processo": "0113546-72.2018.8.02.0001",
        "sigla_tribunal": "TJAL",
        "status": "Na Fila",
        "first_instance": {
            "classe": "Penal",
            "area": "Criminal",
            "assunto": "Roubo",
            "data_distribuicao": "Sorteio",
            "juiz": "Dr. João Silva",
            "partes_processo": [],
            "lista_movimentacoes": []
        }
    }
    output = StatusSolicitacaoOutput(**valid_data)
    assert output.numero_processo == "0113546-72.2018.8.02.0001"
    assert output.first_instance.classe == "Penal"

def test_valid_output_both_instances():
    """Testa um output válido para StatusSolicitacaoOutput com dados de ambas as instâncias."""
    valid_data = {
        "numero_processo": "0113546-72.2018.8.02.0001",
        "sigla_tribunal": "TJAL",
        "status": "Na Fila",
        "first_instance": ExtractDataOutput(
            classe="Penal",
            area="Criminal",
            assunto="Roubo",
            data_distribuicao="Sorteio",
            juiz="Dr. João Silva",
            valor_acao="5000,00",
            partes_processo=[],
            lista_movimentacoes=[]
        ),
        "second_instance": ExtractDataSecondInstanceOutput(
            classe="Recurso Penal",
            area="Criminal",
            assunto="Furto",
            valor_acao="2500,55",
            partes_processo=[],
            lista_movimentacoes=[]
        )
    }

    output = StatusSolicitacaoOutput(**valid_data)

    assert output.numero_processo == "0113546-72.2018.8.02.0001"
    assert output.sigla_tribunal == "TJAL"
    assert output.status == "Na Fila"
    assert isinstance(output.first_instance, ExtractDataOutput)
    assert isinstance(output.second_instance, ExtractDataSecondInstanceOutput)

def test_invalid_output():
    """Testa um output inválido para StatusSolicitacaoOutput."""
    invalid_data = {
        "numero_processo": 123456  # Um número em vez de uma string
    }
    with raises(ValidationError):
        StatusSolicitacaoOutput(**invalid_data)


import google
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import base64

keyPath = '../firestoreKEY.json'

def initializeDb(path):
    cred = credentials.Certificate(path)
    firebase_admin.initialize_app(cred)
    db = firestore.client()
    return db

def stringToImage(string, imagePath):
    fh = open(imagePath, "wb")
    fh.write(string.decode('base64'))
    fh.close()

def getFieldFromDB(database, collectionName, docName, fieldName):
    dbRef = database.collection(collectionName).document(docName)
    try:
        doc = dbRef.get()
        str = doc.to_dict()[fieldName]
    except google.cloud.exception.NotFound:
        str = 'NO SUCH DOCUMENT'

    return str

def getDocsFromCol(database, collectionName):
    return database.collection(collectionName).get()

def main():
    database = initializeDb(keyPath)
    # str = getFieldFromDB(database, u'photos', u'04, 03:59AM on December 15, 2018', u'im')
    strIngs = getDocsFromCol(database, u'photos')
    for ims in strIngs:
        docInfo = ims.to_dict()
        stringToImage(docInfo[u'im'], "images/" + ims.id + ".jpg")

if __name__ == '__main__':
    main()


from os.path import join
from django.shortcuts import render
from random import randint, randrange
from physics import physics_classes_functions as ucf
from physics import variety_lists as vl

def list_callable_functions():
    """returns list of all modules in this file
    This function MUST remain in this file to work correctly!
    Used by:
    modulesList
    previousNext
    """
    entireModuleList = []
    for key, value in globals().items():
        if callable(value) and value.__module__ == __name__:
            entireModuleList.append(key)
    return entireModuleList

def modulesList():#this list is used by views to automatically generate views!
    return ucf.moduleListGen(list_callable_functions(), 'd', 0, 1)

def module_path():
    return '/physics/electricity/'

#selects a view function at random from moduleList generated list and returns everything needed to generate a view
def emfIntResSetup():
    emf = randint(6,18)
    intRes = randint(5,25)/10
    resistor =randint(2,9)
    totalRes = resistor + intRes
    current = emf/(totalRes)
    terminalPd = current*resistor
    wastedPd = current*intRes
    powerSupplied = current*emf
    powerDelivered = current * current * resistor
    powerWasted = current * current * intRes
    current2 = randint(1,100)/10
    terminalPd2 = current2*resistor
    resistor2 = emf/current2 - intRes

    return emf, intRes, resistor, totalRes, round(current, 3), round(terminalPd,3), round(wastedPd, 3), round(powerSupplied, 3), round(powerDelivered,3), round(powerWasted, 3), round(current2, 3), round(terminalPd2, 3), round(resistor2, 2)

def db_ca_resistance_current_lost_pxax8():
    q = ucf.Question(ucf.currentFuncName())
    q.previousQ, q.nextQ, q.currentQname, q.nextQname, q.previousQname = ucf.previousNext(list_callable_functions(), ucf.currentFuncName(), module_path(), ucf.currentFuncName()[0:2], 0, 2)
    emf, intRes, resistor, totalRes, current, terminalPd, wastedPd, powerSupplied, powerDelivered, powerWasted, current2, terminalPd2, resistor2 = emfIntResSetup()
    q.questionBase = [f"A battery of emf {emf}v and internal resistance of {intRes} \u03A9 is connected to a {resistor} \u03A9 resistor.","Calculate:"]
    question1 = ["the total resistance of the circuit"]
    question2 = ["the current through the battery"]
    question3 = ["the lost pd"]
    question4 = ["the pd across the cell terminals."]
    marks1, marks2, marks3, marks4= 2, 2, 2, 2
    answer1 = f"{totalRes} \u03A9"
    answer2 = f"{current} A"
    answer3 = f"{wastedPd} v"
    answer4 = f"{terminalPd} v"
    q.questionPartList = [
       {'sub_number': 1, 'sub_question': question1, 'sub_answer': answer1, 'sub_mark': marks1},
       {'sub_number': 2, 'sub_question': question2, 'sub_answer': answer2, 'sub_mark': marks2},
       {'sub_number': 3, 'sub_question': question3, 'sub_answer': answer3, 'sub_mark': marks3},
       {'sub_number': 4, 'sub_question': question4, 'sub_answer': answer4, 'sub_mark': marks4},
    ]
    return q.returnAll()

def db_cb_resistance_and_power_lost_pd_pxax8():
    q = ucf.Question(ucf.currentFuncName())
    q.previousQ, q.nextQ, q.currentQname, q.nextQname, q.previousQname = ucf.previousNext(list_callable_functions(), ucf.currentFuncName(), module_path(), ucf.currentFuncName()[0:2], 0, 2)
    emf, intRes, resistor, totalRes, current, terminalPd, wastedPd, powerSupplied, powerDelivered, powerWasted, current2, terminalPd2, resistor2 = emfIntResSetup()
    q.questionBase = [f"A battery of emf {emf} v and internal resistance of {intRes} \u03A9 is connected to a {resistor} \u03A9 resistor.","Calculate:"]
    question1 = ["the current"]
    question2 = [")the terminal pd"]
    question3 = [f"the power delivered to the {resistor} \u03A9 resistor"]
    question4 = ["the power wasted in the cell."]
    marks1, marks2, marks3, marks4= 2, 2, 2, 2
    answer1 = f"{current} A"
    answer2 = f"{terminalPd} v"
    answer3 = f"{powerDelivered} w"
    answer4 = f"{powerWasted} w"
    q.questionPartList = [
       {'sub_number': 1, 'sub_question': question1, 'sub_answer': answer1, 'sub_mark': marks1},
       {'sub_number': 2, 'sub_question': question2, 'sub_answer': answer2, 'sub_mark': marks2},
       {'sub_number': 3, 'sub_question': question3, 'sub_answer': answer3, 'sub_mark': marks3},
       {'sub_number': 4, 'sub_question': question4, 'sub_answer': answer4, 'sub_mark': marks4},
    ]
    return q.returnAll()

def db_cc_changing_current_impact_on_emf_pxax5():
    q = ucf.Question(ucf.currentFuncName())
    q.previousQ, q.nextQ, q.currentQname, q.nextQname, q.previousQname = ucf.previousNext(list_callable_functions(), ucf.currentFuncName(), module_path(), ucf.currentFuncName()[0:2], 0, 2)
    emf, intRes, resistor, totalRes, current, terminalPd, wastedPd, powerSupplied, powerDelivered, powerWasted, current2, terminalPd2, resistor2 = emfIntResSetup()
    q.questionBase = [f"The pd acrpss the terminals of a cell was {terminalPd} v when the current from the cell was {current} A, and {terminalPd2} v when the current was {current2} A.","Calculate:"]
    question1 = ["the internal resistance of the cell"]

    question2 = ["the cell's emf"]
    marks1, marks2 = 2, 3
    answer1 = f"{intRes} \u03A9"
    answer2 = f"{emf} v"
    q.questionPartList = [
       {'sub_number': 1, 'sub_question': question1, 'sub_answer': answer1, 'sub_mark': marks1},
       {'sub_number': 2, 'sub_question': question2, 'sub_answer': answer2, 'sub_mark': marks2},
    ]
    return q.returnAll()

def db_cd_changing_current_effect_on_resistor_pxax5():
    q = ucf.Question(ucf.currentFuncName())
    q.previousQ, q.nextQ, q.currentQname, q.nextQname, q.previousQname = ucf.previousNext(list_callable_functions(), ucf.currentFuncName(), module_path(), ucf.currentFuncName()[0:2], 0, 2)
    emf, intRes, resistor, totalRes, current, terminalPd, wastedPd, powerSupplied, powerDelivered, powerWasted, current2, terminalPd2, resistor2 = emfIntResSetup()
    q.questionBase = [f"A battery of unknown emf and internal resistance is connected in series with an ammeter and a resistance box. The current was {current} A when the box was set at {resistor} \u03A9 and {current2} A at {resistor2} \u03A9.","Calculate:"]
    question1 = ["the cell's emf"]
    question2 = ["the cell's internal resistance"]
    marks1, marks2 = 2, 3
    answer1 = f"{emf} v"
    answer2 = f"{intRes} \u03A9"
    q.questionPartList = [
       {'sub_number': 1, 'sub_question': question1, 'sub_answer': answer1, 'sub_mark': marks1},
       {'sub_number': 2, 'sub_question': question2, 'sub_answer': answer2, 'sub_mark': marks2},
    ]
    return q.returnAll()


import time
import pickle
from pathlib import Path
from os.path import splitext
import json

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

# ASReview dependencies
from asreview.review import ReviewSimulate, ReviewOracle, MinimalReview
from asreview.utils import text_to_features
from asreview.types import is_pickle
from asreview.config import AVAILABLE_CLI_MODI, AVAILABLE_REVIEW_CLASSES,\
    DEFAULT_MODEL, DEFAULT_QUERY_STRATEGY, DEFAULT_BALANCE_STRATEGY,\
    DEFAULT_N_INSTANCES, DEFAULT_N_PRIOR_INCLUDED, DEFAULT_N_PRIOR_EXCLUDED,\
    DEMO_DATASETS, KERAS_MODELS
from asreview.models.embedding import download_embedding, EMBEDDING_EN
from asreview.models.embedding import load_embedding, sample_embedding
from asreview.utils import get_data_home
from asreview.query_strategies import get_query_strategy
from asreview.balance_strategies import get_balance_strategy
from asreview.logging import Logger
from asreview.settings import ASReviewSettings

from asreview.models import create_lstm_base_model, lstm_base_model_defaults
from asreview.models import create_lstm_pool_model, lstm_pool_model_defaults
from asreview.models import lstm_fit_defaults

from asreview.readers import ASReviewData

def get_reviewer(dataset,
                 mode='oracle',
                 model=DEFAULT_MODEL,
                 query_strategy=DEFAULT_QUERY_STRATEGY,
                 balance_strategy=DEFAULT_BALANCE_STRATEGY,
                 n_instances=DEFAULT_N_INSTANCES,
                 n_queries=1,
                 embedding_fp=None,
                 verbose=1,
                 prior_included=None,
                 prior_excluded=None,
                 n_prior_included=DEFAULT_N_PRIOR_INCLUDED,
                 n_prior_excluded=DEFAULT_N_PRIOR_EXCLUDED,
                 config_file=None,
                 src_log_fp=None,
                 **kwargs
                 ):

    # Find the URL of the datasets if the dataset is an example dataset.
    if dataset in DEMO_DATASETS.keys():
        dataset = DEMO_DATASETS[dataset]

    if src_log_fp is not None:
        logger = Logger(log_fp=src_log_fp)
        settings = logger.settings
    else:
        logger = None
        settings = ASReviewSettings(model=model, n_instances=n_instances,
                                    n_queries=n_queries,
                                    n_prior_included=n_prior_included,
                                    n_prior_excluded=n_prior_excluded,
                                    query_strategy=query_strategy,
                                    balance_strategy=balance_strategy,
                                    mode=mode, data_fp=dataset
                                    )

        settings.from_file(config_file)
    model = settings.model

    if model in ["lstm_base", "lstm_pool"]:
        base_model = "RNN"
    else:
        base_model = "other"

    # Check if mode is valid
    if mode in AVAILABLE_REVIEW_CLASSES:
        if verbose:
            print(f"Start review in '{mode}' mode.")
    else:
        raise ValueError(f"Unknown mode '{mode}'.")
    print(f"Model: '{model}'")

    # if the provided file is a pickle file
    if is_pickle(dataset):
        with open(dataset, 'rb') as f:
            data_obj = pickle.load(f)
        if isinstance(data_obj, tuple) and len(data_obj) == 3:
            X, y, embedding_matrix = data_obj
        elif isinstance(data_obj, tuple) and len(data_obj) == 4:
            X, y, embedding_matrix, _ = data_obj
        else:
            raise ValueError("Incorrect pickle object.")
    else:
        as_data = ASReviewData.from_file(dataset)
        _, texts, labels = as_data.get_data()

        # get the model
        if base_model == "RNN":

            if embedding_fp is None:
                embedding_fp = Path(
                    get_data_home(),
                    EMBEDDING_EN["name"]
                ).expanduser()

                if not embedding_fp.exists():
                    print("Warning: will start to download large "
                          "embedding file in 10 seconds.")
                    time.sleep(10)
                    download_embedding(verbose=verbose)

            # create features and labels
            X, word_index = text_to_features(texts)
            y = labels
            embedding = load_embedding(embedding_fp, word_index=word_index)
            embedding_matrix = sample_embedding(embedding, word_index)

        elif model.lower() in ['nb', 'svc', 'svm']:
            from sklearn.pipeline import Pipeline
            from sklearn.feature_extraction.text import TfidfTransformer
            from sklearn.feature_extraction.text import CountVectorizer

            text_clf = Pipeline([('vect', CountVectorizer()),
                                 ('tfidf', TfidfTransformer())])

            X = text_clf.fit_transform(texts)
            y = labels

    settings.fit_kwargs = {}
    settings.query_kwargs = {}

    if base_model == 'RNN':
        if model == "lstm_base":
            model_kwargs = lstm_base_model_defaults(settings, verbose)
            create_lstm_model = create_lstm_base_model
        elif model == "lstm_pool":
            model_kwargs = lstm_pool_model_defaults(settings, verbose)
            create_lstm_model = create_lstm_pool_model
        else:
            raise ValueError(f"Unknown model {model}")

        settings.fit_kwargs = lstm_fit_defaults(settings, verbose)
        settings.query_kwargs['verbose'] = verbose
        # create the model
        model = KerasClassifier(
            create_lstm_model(embedding_matrix=embedding_matrix,
                              **model_kwargs),
            verbose=verbose
        )

    elif model.lower() in ['nb']:
        from asreview.models import create_nb_model

        model = create_nb_model()

    elif model.lower() in ['svm', 'svc']:
        from asreview.models import create_svc_model

        model = create_svc_model()
    else:
        raise ValueError('Model not found.')

    # Pick query strategy
    query_fn, query_str = get_query_strategy(settings)
    if verbose:
        print(f"Query strategy: {query_str}")

    train_data_fn, train_method = get_balance_strategy(settings)
    if verbose:
        print(f"Using {train_method} method to obtain training data.")

    # Initialize the review class.
    if mode == "simulate":
        reviewer = ReviewSimulate(
            X, y,
            model=model,
            query_strategy=query_fn,
            train_data_fn=train_data_fn,
            n_instances=settings.n_instances,
            n_queries=settings.n_queries,
            verbose=verbose,
            prior_included=prior_included,
            prior_excluded=prior_excluded,
            n_prior_included=settings.n_prior_included,
            n_prior_excluded=settings.n_prior_excluded,
            fit_kwargs=settings.fit_kwargs,
            balance_kwargs=settings.balance_kwargs,
            query_kwargs=settings.query_kwargs,
            logger=logger,
            **kwargs)

    elif mode == "oracle":
        reviewer = ReviewOracle(
            X,
            model=model,
            query_strategy=query_fn,
            as_data=as_data,
            train_data_fn=train_data_fn,
            n_instances=settings.n_instances,
            n_queries=settings.n_queries,
            verbose=verbose,
            prior_included=prior_included,
            prior_excluded=prior_excluded,
            fit_kwargs=settings.fit_kwargs,
            balance_kwargs=settings.balance_kwargs,
            query_kwargs=settings.query_kwargs,
            logger=logger,
            **kwargs)
    elif mode == "minimal":
        reviewer = MinimalReview(
            X,
            model=model,
            query_strategy=query_fn,
            train_data_fn=train_data_fn,
            n_instances=settings.n_instances,
            n_queries=settings.n_queries,
            verbose=verbose,
            prior_included=prior_included,
            prior_excluded=prior_excluded,
            fit_kwargs=settings.fit_kwargs,
            balance_kwargs=settings.balance_kwargs,
            query_kwargs=settings.query_kwargs,
            logger=logger,
            **kwargs)
    else:
        raise ValueError("Error finding mode, should never come here...")

    reviewer._logger.add_settings(settings)

    return reviewer

def review(*args, mode="simulate", model=DEFAULT_MODEL, save_model_fp=None,
           **kwargs):
    if mode not in AVAILABLE_CLI_MODI:
        raise ValueError(f"Unknown mode '{mode}'.")

    reviewer = get_reviewer(*args, model=model, **kwargs)

    # Wrap in try expect to capture keyboard interrupt
    try:
        # Start the review process.
        reviewer.review()
    except KeyboardInterrupt:
        print('\nClosing down the automated systematic review.')

    # If we're dealing with a keras model, we can save the last model weights.
    if save_model_fp is not None and model in KERAS_MODELS:
        save_model_h5_fp = splitext(save_model_fp)[0]+".h5"
        json_model = model.model.to_json()
        with open(save_model_fp, "w") as f:
            json.dump(json_model, f, indent=2)
        model.model.save_weights(save_model_h5_fp, overwrite=True)

    if not reviewer.log_file:
        print(reviewer._logger._print_logs())

def review_oracle(dataset, **kwargs):
    """CLI to the interactive mode."""

    review(dataset, mode='oracle', **kwargs)

def review_simulate(dataset, **kwargs):
    """CLI to the oracle mode."""

    review(dataset, mode='simulate', **kwargs)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import apps.users.models

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0005_auto_20180802_1614'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='user',
            managers=[
                ('objects', apps.users.models.MyUserManager()),
            ],
        ),
    ]


#coding=utf-8

"""
这是一个关于QQ模拟（QListView的使用）的例子--模型定义！
文章链接：http://www.xdbcb8.com/archives/701.html
"""

import random
import Random_Name
from PyQt5.QtCore import QAbstractListModel, Qt, QModelIndex, QVariant, QSize
from PyQt5.QtGui import QIcon, QFont

class ListModel(QAbstractListModel):
    '''
    自定义模型
    '''
    def __init__(self):
        '''
        一些初始设置
        '''
        super().__init__()
        self.ListItemData = []
        # 存储每个QQ用户的列表

        self.Data_init()

    def data(self, index, role):
        '''
        子类化QAbstractListModel必须要实现的函数，主要作用就是返回index所引用项目的给定role下存储的数据。
        '''
        if index.isValid() or (0 <= index.row() < len(self.ListItemData)):
            if role == Qt.DisplayRole:
                return QVariant(self.ListItemData[index.row()]['name'])
                # 文本形式呈现数据
            elif role == Qt.DecorationRole:
                return QVariant(QIcon(self.ListItemData[index.row()]['iconPath']))
                # 以图标形式呈现装饰数据
            elif role == Qt.SizeHintRole:
                return QVariant(QSize(70, 80))
                # 视图项目大小
            elif role == Qt.TextAlignmentRole:
                return QVariant(int(Qt.AlignHCenter|Qt.AlignVCenter))
                # 文本对齐方式
            elif role == Qt.FontRole:
                font = QFont()
                font.setPixelSize(20)
                return QVariant(font)
                # 字体设置
        return QVariant()
        # 非上述情况，返回为空，记住这里是QVariant()

    def rowCount(self, parent = QModelIndex()):
        '''
        返回行数，在这里就是数据列表的大小。
        '''
        return len(self.ListItemData)

    def Data_init(self):
        '''
        数据初始化
        '''
        randomnum = random.sample(range(26), 10)
        # 从0-25个数字中随机的抽取10个不重复的数字组成一个列表
        for i in randomnum:
            randname = Random_Name.getname()
            ItemData = {'name':'', 'iconPath':''}
            ItemData['name'] = randname
            ItemData['iconPath'] = "./res/"+ str(i) + ".jpg"
            # 遍历这个列表randomnum，其中联系人的姓名我是随机生成的，随机的生成图标的路径；把姓名和图标路径添加到字典当中。

            self.ListItemData.append(ItemData)
            # append到数据列表里面。

    def addItem(self, itemData):
        '''
        新增的操作实现
        '''
        if itemData:
            self.beginInsertRows(QModelIndex(), len(self.ListItemData), len(self.ListItemData) + 1)
            self.ListItemData.append(itemData)
            self.endInsertRows()
            # 结束行插入操作

    def deleteItem(self, index):
        '''
        指定索引的数据从数据列表中删除
        '''
        del self.ListItemData[index]

    def getItem(self, index):
        '''
        获得相应的项目数据
        '''
        if index > -1 and index < len(self.ListItemData):
            return self.ListItemData[index]


#!/usr/bin/python3

import multiprocessing
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
import math
import time

def points():
    import sys
    for line in open("input.txt"):
        line = line.replace("position=", "").replace("velocity=", "").replace(' ', '').replace('<', '').strip()
        a = line.split('>')
        yield [tuple([int(y) for y in x.split(',')]) for x in a[0:2]]

def produce_state(PP, PV, T):
    ret = []
    for p in zip(PP, PV):
        v = p[1]
        p = p[0]
        ret.append([p[0] + v[0] * T, p[1] + v[1] * T])

    return ret

def render(PP, T):
    plt.scatter([p[0] for p in PP], [p[1] for p in PP])
    plt.show(block=True)

def BB(PP):
    sx = 0
    sy = 0
    ex = 0
    ey = 0

    for p in PP:
        if p[0] < sx: sx = p[0]
        if p[0] > ex: ex = p[0]
        if p[1] < sy: sy = p[1]
        if p[1] > ey: ey = p[1]

    return sx, sy, ex, ey

P = [point for point in points()]

# calc BB
PP = [point[0] for point in P]
PV = [point[1] for point in P]

T = 0

Emin = 0
Tmin = -1

step = 1
def update(T):
    S = produce_state(PP, PV, T)
    sx, sy, ex, ey = BB(S)
    #render(S, T)
    return (math.sqrt((sx - ex) ** 2 + (sy - ey) ** 2))

fuck = 256000
scale = 100
start = int(10518.9 * scale)
end = int((1 + 10519) * scale)
#for T in range(start, end, 1):
for i in range(1):
    T = 10519
    #T = T / scale
    S = produce_state(PP, PV, T)
    sx, sy, ex, ey = BB(S)
    cunt = (math.sqrt((sx - ex) ** 2 + (sy - ey) ** 2))
    print(T)
    #if fuck< cunt:
    #    print(T)
    #    render(S, T)
    #    break
    render(S, T)
    fuck = cunt


# import pytest
from schemaql.helpers.fileio import read_yaml
from schemaql.connectors.snowflake import SnowflakeConnector
from schemaql.connectors.bigquery import BigQueryConnector

class TestConnections(object):

    def _get_connection(self, connection_name):
        connections_file = "connections.yml"
        connections = read_yaml(connections_file)

        connection_info = connections[connection_name]
        connection_type = connection_info["type"]

        supported_connectors = {
            "snowflake": SnowflakeConnector,
            "bigquery": BigQueryConnector,
        }

        connector = supported_connectors[connection_type](connection_info)

        return connector

    def test_bigquery_connection(self):

        conn = self._get_connection("tpch-snowflake")
        assert conn is not None
        cur = conn.engine.connect()

        assert cur is not None

    def test_snowflake_connection(self):

        conn = self._get_connection("calogica-bq")
        assert conn is not None
        cur = conn.engine.connect()

        assert cur is not None


import os
from datetime import datetime

dir = os.path.dirname(os.getcwd()+'/users/')
print(dir)
try:
    os.stat(dir)
except:
    os.mkdir(dir)
    print('make directory')

all_users = {}
for f in os.listdir(os.getcwd()):
    if f.endswith(".csv"):
        split = str(f).split('_')
        code = split[1]
        print('[{}] Processing {} - {}'.format(datetime.now(), code, f))
        users = {}
        with open(f, 'r', encoding='utf8') as fr:
            for line in fr:
                split = line.split(',')
                if len(split) < 2:
                    continue
                try:
                    uid = int(split[1])
                except:
                    continue
                # This file users
                f_uid = users.get(uid)
                if f_uid is None:
                    users[uid] = 1
                else:
                    users[uid] = f_uid + 1
                # All users
                f_all = all_users .get(uid)
                if f_all is None:
                    all_users[uid] = 1
                else:
                    all_users[uid] = f_all + 1
            with open(dir + '/' + str(code) + '.csv', 'w') as fw:
                for uid, f_uid in users.items():
                    fw.write('{},{}\n'.format(uid,f_uid))
        users.clear()

print('[{}] Processing all files'.format(datetime.now()))
with open(dir + '/#ALL.csv', 'w') as fw_all:
    for uid, f_all in all_users.items():
        fw_all.write('{},{}\n'.format(uid,f_all))

print('[{}] Program finished'.format(datetime.now()))


class Define:
    def __init__(self, traffic_demand: int, holding_time: int, total_traffic: int,
                 max_route: int, avg_repaired_time: int, node_size: int, shape: float, scale: float):
        self.traffic_demand = traffic_demand
        self.holding_time = holding_time
        self.total_traffic = total_traffic
        self.max_route = max_route
        self.avg_repaired_time = avg_repaired_time
        self.node_size = node_size
        self.shape = shape
        self.scale = scale


# VoiceOver language choices
VOICEOVER_LANGUAGE_CHOICES = [
    ("en", "English"),
    ("hi", "Hindi"),
    ("as", "Assamese"),
    ("bn", "Bengali"),
    ("brx", "Bodo"),
    ("doi", "Dogri"),
    ("gu", "Gujarati"),
    ("kn", "Kannada"),
    ("ks", "Kashmiri"),
    ("gom", "Konkani"),
    ("mai", "Maithili"),
    ("ml", "Malayalam"),
    ("mr", "Marathi"),
    ("mni", "Manipuri"),
    ("ne", "Nepali"),
    ("or", "Oriya"),
    ("pa", "Punjabi"),
    ("sa", "Sanskrit"),
    ("sat", "Santali"),
    ("sd", "Sindhi"),
    ("si", "Sinhala"),
    ("ta", "Tamil"),
    ("te", "Telugu"),
    ("ur", "Urdu"),
]

VOICEOVER_SUPPORTED_LANGUAGES = {
    "English": "en",
    "Assamese": "as",
    "Bengali": "bn",
    "Bodo": "brx",
    "Gujarati": "gu",
    "Hindi": "hi",
    "Kannada": "kn",
    "Malayalam": "ml",
    "Manipuri": "mni",
    "Marathi": "mr",
    "Odia": "or",
    "Punjabi": "pa",
    "Tamil": "ta",
    "Telugu": "te",
}


from django.shortcuts import render, HttpResponse
from django.conf import settings

from rest_framework.decorators import api_view
from rest_framework.response import Response

import random

from .models import Planet
from .serializers import PlanetSerializer

from api.serializers import GenericSerializer
from api.views import BaseRandomView, BaseIdView
from api.utils import validate_request, set_options_response

MODEL = Planet

@api_view(['GET', 'OPTIONS'])
def index(request):
    if request.method == 'OPTIONS':
        return set_options_response()

    result = validate_request(request)
    if 'error' in result:
        return Response({"error": result['error']}, status=result['status'], headers=settings.CORS_HEADERS)

    name = request.GET.get('name', None)
    affiliation = request.GET.get('affiliation', None)
    region = request.GET.get('region', None)
    page = request.GET.get('page', 0)

    planets_set = Planet.objects.all().order_by('id')

    if name:
        planets_set = planets_set.filter(name__icontains=name)

    if affiliation:
        planets_set = planets_set.filter(info__affiliation__icontains=affiliation)

    if region:
        planets_set = planets_set.filter(info__region__icontains=region)

    if page:
        try:
            page = int(page)
        except:
            page = 1;
        start = settings.RESOURCE_LIMIT*(page-1)
        end = settings.RESOURCE_LIMIT*(page-1)+settings.RESOURCE_LIMIT
        planets_set = planets_set[start:end]
    else:
        planets_set = planets_set[0:settings.RESOURCE_LIMIT]

    serializer = PlanetSerializer(planets_set, many=True)

    # If nothing matches queries
    if not serializer.data:
        return Response({"error": settings.MSG_404}, status=404, headers=settings.CORS_HEADERS)

    return Response(serializer.data, headers=settings.CORS_HEADERS)

class RandomPlanetView(BaseRandomView):
    model = MODEL

class PlanetIdView(BaseIdView):
    model = MODEL


import os
import csv
import random
import pandas as pd

def trainval_split(root, suffix, save_dir):
    content = []
    num = 0
    suf_root = os.path.join(root, suffix)
    for slide in os.listdir(suf_root):
        slide_dir = os.path.join(suf_root, slide)
        for subslide in os.listdir(slide_dir):
            subslide_dir = slide + '/' + subslide
            subset = random.randint(0, 4)
            content.append(dict(subslide=subslide_dir, subset=subset))
            num += 1
    random.shuffle(content)

    df = pd.DataFrame(content, columns=['subslide', 'subset'])
    df.to_csv(os.path.join(save_dir, 'trainval.csv'), index=False)
    print('Number of training patches: {}'.format(num))
    print('Finished!!')

def make_trainval_mini(csv_dir, save_dir, num):
    df = pd.read_csv(csv_dir)
    df = list(df.itertuples(index=False))
    random.shuffle(df)
    new_df = df[:num]
    gf = pd.DataFrame(new_df, columns=['subslide', 'subset'])
    gf.to_csv(os.path.join(save_dir, 'trainval_mini.csv'), index=False)

if __name__ == '__main__':
    suffix = 'train'
    root = '/media/ldy/e5a10f4e-18fd-4656-80d8-055bc4078655/OSCC_sf/subslide/'
    save_dir = '/media/ldy/e5a10f4e-18fd-4656-80d8-055bc4078655/OSCC_sf/'
    csv_dir = '/media/ldy/e5a10f4e-18fd-4656-80d8-055bc4078655/OSCC_sf/trainval.csv'

    # trainval_split(root, suffix, save_dir)
    make_trainval_mini(csv_dir, save_dir, num=120)


# Speech Brain Viewer app
# to accompany Hamilton, Oganian, Hall, and Chang, Cell 2021
# https://doi.org/10.1016/j.cell.2021.07.019
#
# Viewer created by Liberty Hamilton, 2021
# Email liberty.hamilton@austin.utexas.edu with questions
#

import scipy.io
import numpy as np

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
from dash.dependencies import Input, Output, State, ClientsideFunction
import plotly.express as px
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import time
import os
from flask_caching import Cache
from dash.exceptions import PreventUpdate

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

suppress_callback_exceptions=True
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title='Speech Brain Viewer'
server = app.server
cache = Cache(app.server, config={
        'CACHE_TYPE': 'redis',
        'CACHE_REDIS_URL': os.environ.get('REDIS_URL', '')
    })

timeout = 300

styles = {
    'pre': {
        'border': 'thin lightgrey solid',
        'overflowX': 'scroll'
    }
}

full_strf = scipy.io.loadmat('full_strf.mat')['strf']
spect_strf = scipy.io.loadmat('spect_strf.mat')['strf']
onset_strf = scipy.io.loadmat('onset_strf.mat')['strf']
peakrate_strf = scipy.io.loadmat('peakrate_strf.mat')['peakrate_strf']
phnfeat_strf = scipy.io.loadmat('phnfeat_strf.mat')['strf']
rel_strf = scipy.io.loadmat('rel_strf.mat')['strf']
elecs = scipy.io.loadmat('elecmatrix.mat')['elecmatrix']
vcorrs1 = scipy.io.loadmat('vcorrs.mat')['vcorrs']
vcorrs = scipy.io.loadmat('uvar.mat')['uvar']
vcorrs = np.hstack((vcorrs, vcorrs1))

trivert = scipy.io.loadmat('lh_pial_trivert.mat')
v = trivert['vert']
t = trivert['tri']

temporal_trivert = scipy.io.loadmat('cvs_avg_inMNI152_lh_temporal_pial.mat')
tv = temporal_trivert['vert']
tt = temporal_trivert['tri']

curv = scipy.io.loadmat('cvs_curv.mat')['curv']
anatomy = scipy.io.loadmat('elecmatrix.mat')['anatomy']
anum = np.array([a[0]-1 for a in anatomy])
elecs[anum>=5,0] = elecs[anum>=5,0]-1
anames = scipy.io.loadmat('elecmatrix.mat')['new7AreaNames']
anames2 = [a[0] for a in anames[0]]
anat_labels = [anames2[a[0]-1] for a in anatomy]
clr = scipy.io.loadmat('elecmatrix.mat')['area7Cols']
clrs = [clr[a[0]-1,:].tolist() for a in anatomy]

# We have a small number in the right hem that were projected to the medial wall, lets remove
rm_elecs = np.intersect1d(np.where(elecs[:,1]<-20)[0], np.where(elecs[:,2]<-20)[0])
elec_no = np.arange(elecs.shape[0])
elecs_mask = np.ones((elecs.shape[0],), dtype=bool)
elecs_mask[rm_elecs] = False
elec_no = elec_no[elecs_mask]
elecs = elecs[elecs_mask,:]
vcorrs = vcorrs[elecs_mask,:]
full_strf = full_strf[elecs_mask,:,:]
onset_strf = onset_strf[elecs_mask,:,:]
spect_strf = spect_strf[elecs_mask,:,:]
peakrate_strf = peakrate_strf[elecs_mask,:]
phnfeat_strf = phnfeat_strf[elecs_mask,:,:]
rel_strf = rel_strf[elecs_mask,:,:]
anum = anum[elecs_mask]
anat_labels = [anat_labels[a] for a in elec_no]
clrs = [clrs[a] for a in elec_no]

#stim_effects = pd.read_excel(io='/Users/jsh3653/Dropbox/Heschls_STRFs/data/stim/HG_stim_summary.xlsx',
#                             sheet_name='unique_for_manuscript')
stim_effects = pd.read_excel(io='stim_results.xlsx', sheet_name='Sheet1')
stim_df = pd.DataFrame(
            data={'elec_number': np.arange(len(stim_effects)),
              'x': stim_effects['x'],
              'y': stim_effects['y'],
              'z': stim_effects['z'],
              'anatomy': stim_effects['anatomy'],
              'effect': stim_effects['effect'],
              'passive_effect': stim_effects['passive_effect'],
              'repetition_effect': stim_effects['repetition_effect']},
        )

def create_figure(dropdownData='RF', elec_marker='vcorrs',
                  show_rest_of_brain=True, corr_type=20):
    '''
    Create the brain figure and modify the electrode
    colors based on dropdown menus. The frontal lobe
    will be shown or not depending on the value of the
    show_rest_of_brain switch.
    '''
    if dropdownData=='RF':
        chosen_elecs = np.arange(elecs.shape[0])
        df = pd.DataFrame(
            data={'elec_number': chosen_elecs,
              'x': elecs[chosen_elecs,0],
              'y': elecs[chosen_elecs,1],
              'z': elecs[chosen_elecs,2],
              'anatomy': [anat_labels[a] for a in chosen_elecs],
              'anatomy_num': [anum[a] for a in chosen_elecs],
              'vcorrs': vcorrs[chosen_elecs,corr_type]},
        )
    else:
        df = stim_df

    if elec_marker == 'anatomy_num':
        marker = dict(color=clrs,
                      size=6)
    elif elec_marker == 'vcorrs':
        marker = dict(color=df['vcorrs'],
                      colorscale='RdBu_r',
                      cmin=-df['vcorrs'].max(),
                      cmax=df['vcorrs'].max(),
                      size=6, colorbar=dict(title='Corr.', thickness=20))
    elif elec_marker == 'stim_eff':
        marker = dict(color=df['effect'],
                      colorscale='RdBu_r',
                      cmin=1,
                      cmax=3,
                      size=6, colorbar=dict(title='Effect', thickness=20))

    fig = go.Figure(
        data = [go.Mesh3d(
            x=tv[:, 0],
            y=tv[:, 1],
            z=tv[:, 2],
            i=tt[:, 0],
            j=tt[:, 1],
            k=tt[:, 2],
            colorbar=None,
            showscale=False,
            color='rgb(200,200,200)',
            name='temporal lobe',
            opacity=0.6,
            lighting=dict(ambient=0.9, diffuse=0.9),
            intensity=curv,
            colorscale=[[0, 'white'],
                        [0.5, 'gray'],
                        [1, 'black']]
            ),
        ])

    if show_rest_of_brain:
        fig.add_trace(
            go.Mesh3d(
                x=v[:, 0],
                y=v[:, 1],
                z=v[:, 2],
                i=t[:, 0],
                j=t[:, 1],
                k=t[:, 2],
                colorbar=None,
                showscale=False,
                color='rgb(200,200,200)',
                name='brain',
                text=None,
                opacity=0.2,
                lighting=dict(ambient=0.9, diffuse=0.9),
                intensity=curv,
                colorscale=[[0, 'white'],
                            [0.5, 'gray'],
                            [1, 'black']]
                )
            )

    fig.add_trace(
        go.Scatter3d(
            x=df['x'],
            y=df['y'],
            z=df['z'],
            ids=df['elec_number'],
            mode='markers',
            name='electrode',
            text=df['anatomy'],
            marker=marker,
            ),
        )

    camera = dict(
        up=dict(x=0, y=0, z=1),
        center=dict(x=0, y=0, z=0),
        eye=dict(x=-1.25, y=0.1, z=0.13),
    )

    fig.update_layout(clickmode='event+select',
                  scene=dict(
                    xaxis=dict(showticklabels=False, showgrid=False, title='L-R'),
                    yaxis=dict(showticklabels=False, showgrid=False, title='A-P'),
                    zaxis=dict(showticklabels=False, showgrid=False, title='D-V'),
                    ),
                  scene_camera=camera,
                  height=int(500),
                  )
    fig.update_scenes(xaxis_showbackground=False,
                  yaxis_showbackground=False,
                  zaxis_showbackground=False,
                  xaxis_showaxeslabels=False,
                  yaxis_showaxeslabels=False,
                  zaxis_showaxeslabels=False,)

    return fig

def create_rf(elec_num=310, corr_type=12):
    '''
    This creates the receptive field heat map plot for
    the model of interest (based on `corr_type` number).
    For reference, those corr numbers are:
        Unique Onset: 0
        Unique Peak rate: 1
        Unique Features: 2
        Unique Abs Pitch: 3
        Unique Rel Pitch: 4
        Full phonological+pitch: 12,
        Spectrogram: 20
    '''
    if elec_num is None:
        elec_num = 310
        title = 'Please select an electrode...'
        strf = np.zeros((spect_strf.shape[1], spect_strf.shape[2]))
        yticks = []
        yticklabels = []
        ticksize = 12
        ylabel = ''
        autorange = True
    else:
        if (corr_type == 20) or (corr_type == 12):
            title = 'Electrode %d, r=%2.2f'%(elec_num, vcorrs[elec_num,corr_type])
        else:
            title = 'Electrode %d, unique r^2=%2.2f'%(elec_num, vcorrs[elec_num,corr_type])
        if corr_type == 20:
            strf = np.fliplr(spect_strf[elec_num,:,:])
            yticks = [11, 43, 79]
            yticklabels = [0.5, 2, 8]
            ticksize = 12
            ylabel = 'Frequency (kHz)'
            autorange = True
        elif corr_type == 0: # onset
            strf = np.fliplr(onset_strf[elec_num,:,:])
            ticksize = 12
            yticks = [strf.min(), 0, strf.max()]
            ylabel = 'Onset weight (A.U.)'
            yticklabels = [np.round(strf.min()*100)/100., 0, np.round(strf.max()*100)/100.]
            autorange = True
        elif corr_type == 1: # peakrate
            strf = peakrate_strf[elec_num,:][::-1]
            ticksize = 12
            yticks = [strf.min(), 0, strf.max()]
            ylabel = 'Peak rate weight (A.U.)'
            yticklabels = [np.round(strf.min()*100)/100., 0, np.round(strf.max()*100)/100.]
            autorange = True
        elif corr_type == 2:
            strf = np.fliplr(phnfeat_strf[elec_num,:,:])
            yticks = np.arange(phnfeat_strf.shape[1])
            yticklabels = ['sonorant','obstruent','voiced',
                           'nasal','syllabic','fricative','plosive',
                           'back','low','front','high','labial',
                           'coronal','dorsal']
            ticksize = 6
            ylabel = ''
            autorange = 'reversed'
        elif corr_type == 3:  # abs pitch
            strf = np.fliplr(full_strf[elec_num,15:25,:])
            #yticks = [0,1,15,25,35]
            #yticklabels = ['on','ph','ab','rl','dr']
            yticks = [0,9]
            yticklabels = [90, 250]
            ticksize = 12
            ylabel = 'Abs. Pitch (Hz)'
            autorange = True
        elif corr_type == 4:
            strf = np.fliplr(rel_strf[elec_num,:,:])
            yticks = [0, 4.5, 9, 10, 14.5, 19]#np.arange(rel_strf.shape[1])
            yticklabels = [-1.9, 0, 1.9, -0.4, 0, 0.3]
            ticksize = 12
            ylabel = 'Rel. Pitch + ∆Rel. Pitch'
            autorange = True
        else:
            strf = np.fliplr(full_strf[elec_num,:,:])
            reorder = [0,strf.shape[0]-1]+list(np.arange(1,full_strf.shape[1]-1))
            print(strf.shape)
            print(reorder)
            strf = strf[reorder,:]
            #yticks = [0,1,15,25,35]
            #yticklabels = ['on','ph','ab','rl','dr']
            yticks = np.arange(full_strf.shape[1])
            yticklabels = ['onset','peakRate','sonorant','obstruent','voiced',
                           'nasal','syllabic','fricative','plosive',
                           'back','low','front','high','labial',
                           'coronal','dorsal','abs. pitch','','','',
                           '','','','','','','rel. pitch','','','',
                           '','','','','','','∆rel. pitch','','','',
                           '','','','','','']
            ticksize = 6
            ylabel = ''
            autorange = 'reversed'

    smax = np.abs(strf.max())
    if smax==0:
        smax = 1
    if corr_type == 0:
        smax = 0.1

    if corr_type > 1:
        fig = go.Figure(data = [
                go.Heatmap(
                        x=np.linspace(-0.6,0,60),
                        z=strf,
                        zmin=-smax,
                        zmax=smax,
                        colorscale='RdBu_r',
                        colorbar=dict(title='Beta
weight
(A.U.)',
                                      tickvals=[-smax,0,smax],
                                      ticktext=['-max','0','max']),
                )
            ]
        )
    else:
        fig = go.Figure(data = [
                go.Scatter(
                        x=np.linspace(-0.6,0,60),
                        y=strf.ravel(),
                        mode='lines',
                    )
            ]
        )

    if corr_type != 20:
        if corr_type == 12:
            fig.add_hline(y=0.5, line_width=1, line_color='black', line_dash='dash')
            fig.add_hline(y=1.5, line_width=1, line_color='black', line_dash='dash')
            fig.add_hline(y=15.5, line_width=1, line_color='black', line_dash='dash')
            fig.add_hline(y=25.5, line_width=1, line_color='black', line_dash='dash')
            fig.add_hline(y=35.5, line_width=1, line_color='black', line_dash='dash')
        if corr_type == 4:
            fig.add_hline(y=9.5, line_width=1, line_color='black', line_dash='dash')
    else:
        fig.add_hline(y=11, line_width=1, line_color='black', line_dash='dash')
        fig.add_hline(y=43, line_width=1, line_color='black', line_dash='dash')
        fig.add_hline(y=79, line_width=1, line_color='black', line_dash='dash')

    fig.update_layout(
        title={'text': title,
               'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        xaxis={'title': 'Time (s)'},
        yaxis={'title': ylabel,
               'tickmode': 'array',
               'tickvals': yticks,
               'ticktext': yticklabels, 'showgrid': False,
               'autorange': autorange,
               'tickfont_size': ticksize,
               'automargin': False,
               }
        )
    return fig

fig = create_figure()
rf_fig = create_rf()

#fig = px.scatter(df, x="x", y="y", color="fruit", custom_data=["customdata"])

#fig.update_traces(selector=dict(name='electrode'), marker=dict(color='mediumblue', size=20), row=1, col=1)
rf_markdown = dcc.Markdown('''
            Click on an electrode on the brain to see its corresponding receptive field or stimulation result on the right.

            **Brain Controls:**
            * Zoom in and out of the brain by scrolling
            * Rotate the brain by clicking and dragging

            Note that the nonlinear warping of electrodes sometimes means the electrodes will seem farther forward
            or back than expected. The anatomical name that shows on hover is taken from the original (native space)
            brain data. Electrodes have been projected to the nearest surface vertex for ease of clicking. For the most
            accurate visualization, please see [our paper](https://doi.org/10.1016/j.cell.2021.07.019).

            Brain viewer created by Liberty Hamilton 2021 using [Dash and Plotly for python](https://dash.plotly.com/).
            Contact liberty.hamilton@austin.utexas.edu with any questions.
            ''')

# This creates the initial app in its first instantiation. This will be
# modified by user behaviors (clicking, changing menu items, etc.)
app.layout = html.Div([
            html.Div([
            dcc.Markdown('''
                    ### Parallel and distributed speech encoding across human auditory cortex ###

                    *Citation*: [Hamilton, Oganian, Hall, and Chang. _Cell_ 2021](https://doi.org/10.1016/j.cell.2021.07.019)

                    This is an interactive tool to accompany our paper showing receptive fields across
                    multiple sub-fields of auditory cortex. Select from the Dropdown menu below to
                    explore receptive field findings and stimulation findings. Works best on desktop computers, tablet/mobile does not include all features. [Video Tutorial.](https://www.youtube.com/watch?v=Q0zulm4ciRI&ab_channel=LibertyHamilton)

                    '''),
            ]),
            html.Div([
                html.Div([
                daq.BooleanSwitch(
                    id='show-brain',
                    on=True,
                    label="Whole brain",
                    labelPosition="top",
                ),
                ], className='three columns',
                style={'background-color': 'lightgrey', 'padding': '10px',
                       'float': 'left'}),

                html.Div([
                html.Label('Color electrodes by:'),
                dcc.RadioItems(
                    id='radio-color',
                    options=[
                        {'label': 'Anatomy', 'value': 'anatomy_num'},
                        {'label': 'Correlation', 'value': 'vcorrs'},
                    ],
                    value='vcorrs'
                )], className='three columns',
                style={'background-color': 'lightgrey', 'padding': '10px'}, id='color-electrodes-div'),

                html.Div([
                html.Label('Correlation type:'),
                dcc.Dropdown(
                    id='corr-type-dropdown',
                    options=[
                        {'label': 'Spectrogram', 'value': '20'},
                        {'label': 'Full phonological+pitch', 'value': '12'},
                        {'label': 'Unique Onset', 'value': '0'},
                        {'label': 'Unique Peak rate', 'value': '1'},
                        {'label': 'Unique Features', 'value': '2'},
                        {'label': 'Unique Absolute Pitch', 'value': '3'},
                        {'label': 'Unique Relative Pitch', 'value': '4'},
                    ],
                    # options=[
                    #     {'label': 'Onset', 'value': '0'},
                    #     {'label': 'Full', 'value': '6'},
                    #     {'label': 'Relative pitch', 'value': '12'},
                    #     {'label': 'Spectrogram', 'value': '14'},
                    # ],
                    value='20'
                )], className='three columns', id='corr-type-div',
                style={'background-color': 'lightgrey',
                        'padding': '10px', 'display': 'inline-block'}),

                html.Div([
                html.Label('Choose results to explore:'),
                dcc.Dropdown(
                    id='rf-stim-dropdown',
                    options=[
                        {'label': 'Receptive Fields', 'value': 'RF'},
                        {'label': 'Stimulation', 'value': 'ST'},
                    ],
                    value='RF'
                )], className='three columns',
                style={'background-color': 'lightgrey',
                        'padding': '10px', 'display': 'inline-block',
                        'float': 'right'}),
                ],
            style={'background-color': 'lightgrey', 'display': 'inline-block', 'width': '100%'}
            ),
        html.Div([
            dcc.Loading(
                dcc.Graph(
                id='brain-fig',
                figure=fig,
                ),
                type='circle',
            ),
        ],

        style={'width': '70%', 'display': 'inline-block', 'height': '70%'}),

        html.Div([
            html.Div([
                dcc.Graph(
                    id='rf',
                    figure=rf_fig,
                ),
            ],
            id="rf_div",
            style={'width': '100%', 'display': 'inline-block', 'vertical-align': 'top'},
            ),
            html.Div([
                html.H4('Stimulation effects'),
                html.P('Click on an electrode to see effects of stimulation on passive \
                        listening and on speech perception. We recommend you turn off\
                        the "whole brain" switch at the top left to show the temporal lobe only.'),
                html.P('Effect types: ', style={'font-weight': 'bold'}),
                html.P('1 (blue): sound hallucination + no problems perceiving speech',
                       style={'background-color': '#0c2350', 'padding': '10px', 'color': '#ffffff'}),
                html.P('2 (white): no sound hallucination + problems perceiving speech',
                       style={'background-color': '#f1f2f2', 'padding': '10px', 'color': '#000000'}),
                html.P('3 (red): Complex response',
                       style={'background-color': '#73001c', 'padding': '10px', 'color': '#ffffff'}),
                html.H5('', id='stim_desc'),
                html.H5('', id='repet_effect')
                ],
            id="stim_div",
            style={'width': '100%', 'display': 'none', 'vertical-align': 'middle'},
            )
        ],
        id="rf_or_stim_div",
        style={'width': '30%', 'display': 'inline-block', 'vertical-align': 'top'}),

        html.Div([
                rf_markdown,
            ],
            style={'background-color': 'lightgrey', 'padding': '10px'}),

    ],
    style={'max-width': '1200px'},
)

# This callback will create the receptive field figure
# based on the correlation type you choose and what you
# have clicked on the brain figure
@app.callback(
     [Output('rf', 'figure'),
      Output('stim_desc', 'children'),
      Output('repet_effect', 'children'),
      Output('corr-type-div', 'style'),
      Output('color-electrodes-div', 'style')],
    [Input('brain-fig', 'clickData'),
     Input('corr-type-dropdown', 'value'),
     Input('rf-stim-dropdown', 'value')])
def update_rf(clickData, corr_val, rf_value):
    ctx = dash.callback_context
    prop_id = ctx.triggered[0]['prop_id'].split('.')[0]

    try:
        elec_num = clickData['points'][0]['id']
    except:
        elec_num = None

    if rf_value == 'RF':
        rf_updated = create_rf(elec_num=elec_num, corr_type=int(corr_val))
        stim_updated = ''
        rep_updated = ''
        corr_div_style={'background-color': 'lightgrey',
                        'padding': '10px', 'display': 'inline-block'}
        color_elec_style={'background-color': 'lightgrey', 'padding': '10px',
                          'display': 'inline-block'}
    else:
        corr_div_style={'background-color': 'lightgrey',
                        'padding': '10px', 'display': 'none'}
        color_elec_style={'background-color': 'lightgrey', 'padding': '10px',
                          'display': 'none'}
        if (prop_id == 'rf-stim-dropdown') or (prop_id=='corr-type-dropdown'):
            elec_num = 0
            rf_updated = create_rf(elec_num=elec_num, corr_type=int(corr_val))
            stim_updated = ''
            rep_updated = ''
        else:
            passive_description = stim_df['passive_effect'][elec_num]
            repet_description = stim_df['repetition_effect'][elec_num]
            rf_updated = create_rf(elec_num=elec_num, corr_type=int(corr_val))
            stim_updated = 'Passive: ' + passive_description
            rep_updated = 'Repetition: ' + repet_description

    return rf_updated, stim_updated, rep_updated, corr_div_style, color_elec_style

# This callback will change the brain figure to show
# either receptive field data or stimulation data
# based on the dropdown values. It will also change
# the correlation type that is shown if in "RF" mode
@app.callback(
    [Output('brain-fig', 'figure'),
     Output('show-brain', 'label'),
     Output('rf_div', 'style'),
     Output('stim_div', 'style'),],
    [Input('rf-stim-dropdown', 'value'),
     Input('radio-color', 'value'),
     Input('show-brain', 'on'),
     Input('corr-type-dropdown', 'value')])
# @cache.memoize(timeout=timeout)  # in seconds, cache the data
def display_click_data(rf_value, radio_value, brain_value, corr_val):
    ctx = dash.callback_context
    prop_id = ctx.triggered[0]['prop_id'].split('.')[0]
    value = ctx.triggered[0]['value']

    if rf_value == 'ST':
        # Override elec_marker type
        el_marker = 'stim_eff'
        stim_style = {'width': '100%', 'display': 'inline-block', 'vertical-align': 'middle'}
        rf_style = {'width': '100%', 'display': 'none', 'vertical-align': 'top'}
    else:
        el_marker = radio_value
        stim_style = {'width': '100%', 'display': 'none', 'vertical-align': 'middle'}
        rf_style = {'width': '100%', 'display': 'inline-block', 'vertical-align': 'top'}

    fig = create_figure(dropdownData=rf_value, elec_marker=el_marker,
                        show_rest_of_brain=brain_value, corr_type=int(corr_val))

    if brain_value:
        show_brain = "Whole brain"
    else:
        show_brain = "Temporal lobe only"

    # if rf_value=='RF':
    #     rf_stim_update = dcc.Loading(dcc.Graph(id='rf', figure=rf_fig))
    # else:
    #     rf_stim_update = ... #markdown for stim

    return fig, show_brain, rf_style, stim_style

if __name__ == '__main__':
    #app.run_server(processes=6)
    app.run_server(debug=True, host='127.0.0.1')


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class SwitchToWindow():

    def test(self):
        baseUrl = "https://letskodeit.teachable.com/pages/practice"
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get(baseUrl)

        # Find parent handle -> Main Window
        parentHandle = driver.current_window_handle
        print("Parent Handle: " + parentHandle)

        # Find open window button and click it
        driver.find_element(By.ID, "openwindow").click()
        time.sleep(2)

        # Find all handles, there should two handles after clicking open window button
        handles = driver.window_handles

        # Switch to window and search course
        for handle in handles:
            print("Handle: " + handle)

ff = SwitchToWindow()
ff.test()

CODES/S23 - Selenium WebDriver -_ Switch Window And IFrames/1_switch-to-window.py


import glob
import os
import shutil
import os.path
from os import path

# Constants
CE_DIR = "ce"
CE_FILTER_DIR = "ce_filter"

def cleanup(directory):
    if path.exists(directory):
        shutil.rmtree(directory)
    os.mkdir(directory)

def main():
    cleanup(CE_FILTER_DIR)

    ce_files = sorted(glob.glob(f"{CE_DIR}/*.ce"))

    for file in ce_files:
        out_file = file.replace(f"{CE_DIR}/", f"{CE_FILTER_DIR}/")
        os.system(f"grep -i 'com\.axa\.trx\.' {file} > {out_file}")

    return

if __name__ == '__main__':
    main()


from django.test import TestCase, RequestFactory

from djlotrek.request_utils import get_host_url

class RequestUtilsTestCase(TestCase):
    def test_get_host_url(self):
        """
        get_host_url function retrieve request object and
        return host url when request object is not None
        """
        request_factory = RequestFactory()
        request = request_factory.get("/path")
        request.META["HTTP_HOST"] = "localhost"
        host_url = get_host_url(request)
        self.assertEqual(host_url, "http://localhost")

    def test_get_host_url_no_request(self):
        """
        get_host_url function retrieve request object and
        return None when request object is None
        """
        host_url = get_host_url(None)
        self.assertEqual(host_url, None)


from sqlalchemy import create_engine, text, MetaData, Table, Column, Integer, String, select

engine = create_engine('postgresql://postgres:1@localhost/news_db')

meta = MetaData()

students = Table(
    'students', meta,
    Column('id', Integer, primary_key=True),
    Column('first_name', String),
    Column('last_name', String))

# meta.create_all(engine)

conn = engine.connect()

st = students.alias()
s = st.select().where(st.c.id > 2)
result = conn.execute(s).fetchall()
print(result)


from django.db import models
all_pages = []

# Just switched to keeping it in-memory. No real need for a model here.

# class Page(models.Model):
#     top = models.TextField(blank=True, null=True)
#     middle_link = models.TextField(blank=True, null=True)
#     middle_html = models.TextField(blank=True, null=True)
#     bottom = models.TextField(blank=True, null=True)
#     year = models.CharField(max_length=200, blank=True, null=True)
#     order = models.IntegerField(default=0)
#     active = models.BooleanField(default=True)

#     def __unicode__(self, *args, **kwargs):
#         return self.top

class BaseModel(models.Model):
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

    def __unicode__(self):
        return "%s" % self.name

class SimplePage:

    def __init__(self, top="", middle_link="", middle_links=None, bottom="", year=""):
        self.top = top
        self.middle_link = middle_link # list of dicts - url, text
        self.middle_links = middle_links
        self.bottom = bottom
        self.year = year

    def __unicode__(self, *args, **kwargs):
        return self.top

def add_page(*args, **kwargs):
    globals()["all_pages"].append(SimplePage(*args, **kwargs))

add_page(
    top="is enough",
    bottom="what does love mean to you?",
    middle_link="http://isenough.com",
    year="2012 (in-progress)",
)
add_page(
    top="encore",
    bottom="a digital poem",
    middle_link="http://www.encorepoem.com",
    year="2012  (in-progress)",
)
add_page(
    top="slow art",
    bottom="the anti-museum.<br/>\r\nin portland, or",
    middle_link="http://slowartpdx.com",
    year="2011+",
)
# add_page(
#     top="dear text messages,",
#     bottom="spoken word",
#     middle_link="",
#     year="2012",
# )
add_page(
    top="togetheralone",
    bottom="an experiment in community",
    middle_link="http://togetheralone.org",
    year="2012 (in-progress)",
)
add_page(
    top="the digital executioner",
    bottom="viral web idea generator",
    middle_link="http://www.thedigitalexecutioner.com",
    year="2012",
)
add_page(
    top="goodcloud",
    bottom="helping small nonprofits succeed",
    middle_link="https://www.agoodcloud.com",
    year="2011",
)
add_page(
    top="github",
    bottom="where I keep the bits and bytes",
    middle_link = "https://www.github.com/skoczen",
    # middle_links=[
    #     {
    #         "url": "https://www.github.com/skoczen",
    #         "text": "Personal",
    #         "class": ""
    #     },
    #     {
    #         "url": "https://www.github.com/GoodCloud",
    #         "text": "GoodCloud",
    #         "class": ""
    #     }
    # ],
    year="2009+",
)

add_page(
    top="sixlinks",
    bottom="sustainability you can actually do",
    middle_link="http://www.sixlinks.org",
    year="2008+",
)

add_page(
    top="the facebooks",
    bottom="yep, I'm on there",
    middle_link = "https://www.facebook.com/skoczen",
    # middle_links=[
    #     {
    #         "url": "https://www.facebook.com/skoczen",
    #         "text": "f",
    #         "class": "facebook"
    #     },
    #     {
    #         "url": "https://twitter.com/#!/skoczen",
    #         "text": "t",
    #         "class": "twitter"
    #     },
    #     {
    #         "url": "https://plus.google.com/101690366177319310091/",
    #         "text": "g+",
    #         "class": "google_plus"
    #     }
    # ],
    year="2007+",
)
# add_page(
#     top="Write Around Portland",
#     bottom="I'm proud to be a volunteer and donor for this amazing organization.",
#     middle_link="http://www.writearound.org",
#     year="2009+",
# )
add_page(
    top="30 people, 30 minutes",
    bottom="an epic way to turn 30",
    middle_link="http://www.30people30minutes.com",
    year="1999+",
)

add_page(
    top="quantum imagery",
    bottom="ye olde sole proprietorship",
    middle_link="http://www.quantumimagery.com",
    year="1999+",
)

add_page(
    top="photoblog",
    bottom="ye olde photos",
    middle_link="http://www.skoczen.net/photos",
    year="2005",
)

add_page(
    top="but i'm hungry!",
    bottom="ye olde recipe and restaurant site",
    middle_link="http://skoczen.net/food/",
    year="1999+",
)
add_page(
    top="liquid silver zen",
    bottom="early experiments in design",
    middle_link="http://liquidsilverzen.net/",
    year="2002",
)
add_page(
    top="or, just google",
    bottom="It's all me. Except for the Ohio seatbelt ticket. That's the other Steven Skoczen. (Really.)",
    middle_link="https://www.google.com/?q=Steven%20Skoczen",
    year="1997+",
)
# add_page(
#     top="birth",
#     bottom="there was no internet then",
#     middle_links=[
#         {
#             "url": "",
#             "text": "whoa",
#             "class": ""
#         },
#     ],
#     year="1980",
# )


#This script will format all images in a given directory to the same size and to gray scale using OpenCV
from scipy import ndimage, misc
import numpy as np
import cv2 as cv
from os import listdir
#Iterate throuh the training image directory and sub folders
img_directory= "/home/paok/Documents/FaceRecognition/trainImages"
#create a list with file names in image directory
#Iterate through folder in img_directory
subjects= listdir(img_directory)
#Iterate through file name list and add to csv file
for i in range(len(subjects)):
    pictures = listdir("/home/paok/Documents/FaceRecognition/trainImages/"+subjects[i])
    for pic in pictures:
        filtered = np.zeros((256, 256, 1), dtype = "uint8")
        path = "/home/paok/Documents/FaceRecognition/trainImages/"+subjects[i]+"/"+pic
        #print(path)
        #Open image in direcotry
        image = cv.imread(path)
        #Convert it to gray scale
        image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        #Saturate image
        image = 1.2*image
        #Format as float32
        img = np.array(image, dtype=np.float32)
        #Filter
        filtered = cv.bilateralFilter(img,2,15,15)
        #Resize to 200x200 pixels
        filtered = cv.resize(filtered,(200,200))
        wrt = np.array(filtered, dtype="uint8")
        write = cv.equalizeHist(wrt)
        cv.imwrite(path, write)


from rng import RNG
from exceptions import InvalidValue
import time

#Blum Blum Shub Generator
class BBS(RNG):

    def __init__(self, seed = int(time.time()), moduo = 429497053):

        RNG.__init__(self, seed)

        if(seed<0):
            raise InvalidValue("seed")
        if(moduo<=0):
            raise InvalidValue("moduo")

        self.__seed = seed
        self.__moduo = moduo

    def setSeed(self, seed):
        if(0 <= seed < self.__moduo):
            self.__seed = seed
        else:
            raise InvalidValue('seed')

    def getSeed(self):
        return self.__seed

    def setModuo(self, moduo):
        if(moduo>0):
            self.__moduo = moduo
        else:
            raise InvalidValue('moduo')

    def getModuo(self):
        return self.__moduo

    def __getParityBit(self, number):
        parity = False
        while(number):
            c = number & 1
            number = number >> 1
            parity = not parity if c==1 else parity
        return 1 if parity else 0

    def __computeNumber(self):
        newNumber = 0
        #32 iteration to get 32 bits
        for i in range(32):
            self.__seed = (self.__seed ** 2) % self.__moduo
            bit = self.__getParityBit(self.__seed)
            newNumber = (newNumber<<1) | bit
        return newNumber

    def random(self, size = None):
        if(size == None):
            return self.__randomOne()
        else:
            return self.__randomArray(size)

    def __randomOne(self):
        return self.__computeNumber()/4294967296

    def __randomArray(self, size):
        array = []
        for i in range(size):
            array.append(self.__randomOne())
        return array