# The Apriori Algorithm
import pandas as pd
import numpy as np
from itertools import combinations
import matplotlib.pyplot as plt

def getDate():
    marketingData = pd.read_csv("C:\\Users\\Ruedi\\OneDrive\\Learning\\Learning\\Machine Learning(python)\\Unsupervised Learning\\Marketing.csv"  )
    marketingData =marketingData.dropna( axis = 0 )
    n = marketingData.shape[0]

    col =  marketingData.loc[ :,  ["ANNUAL INCOME" ]]
    incomeDf = pd.DataFrame(  { \
                                    "Income: Less than $10,000"  : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "Income: $10,000 to $14,999" : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "Income: $15,000 to $19,999" : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "Income: $20,000 to $24,999" : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "Income: $25,000 to $29,999" : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "Income: $30,000 to $39,999" : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "Income: $40,000 to $49,999" : np.where( col == 7, 1, 0 ).flatten(),  \
                                    "Income: $50,000 to $74,999" : np.where( col == 8, 1, 0 ).flatten(),  \
                                    "Income: $75,000 or more"    : np.where( col == 9, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["SEX" ]]
    sexDf = pd.DataFrame( { \
                                "Male"   : np.where( col == 1, 1, 0 ).flatten(), \
                                "Female" : np.where( col == 2, 1, 0 ).flatten() \
                        } )

    col =  marketingData.loc[ :,  ["MARITAL STATUS" ]]
    MARITALDf = pd.DataFrame(  { \
                                    "MARITAL: Married"                      : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "MARITAL: Living together, not married" : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "MARITAL: Divorced or separated"        : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "MARITAL: Widowed"                      : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "MARITAL: Single, never married"        : np.where( col == 5, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["AGE" ]]
    ageDf = pd.DataFrame(  { \
                                    "AGE: 14 thru 17" : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "AGE: 18 thru 24" : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "AGE: 25 thru 34" : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "AGE: 35 thru 44" : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "AGE: 45 thru 54" : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "AGE: 55 thru 64" : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "AGE:65 and Over" : np.where( col == 7, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["EDUCATION" ]]
    educationDf = pd.DataFrame(  { \
                                    "EDUCATION: Grade 8 or less"        : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "EDUCATION: Grades 9 to 11"         : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "EDUCATION: Graduated high school"  : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "EDUCATION: 1 to 3 years of college": np.where( col == 4, 1, 0 ).flatten(),  \
                                    "EDUCATION: College graduate"       : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "EDUCATION: Grad Study"             : np.where( col == 6, 1, 0 ).flatten()  \
                            }   )

    col =  marketingData.loc[ :,  ["OCCUPATION" ]]
    occipationDf = pd.DataFrame(  { \
                                    "OCCUPATION: Professional/Managerial"        : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Sales Worker"                   : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Factory Worker/Laborer/Driver"  : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Clerical/Service Worker"        : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Homemaker"                      : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Student, HS or College"         : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Military"                       : np.where( col == 7, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Retired"                        : np.where( col == 8, 1, 0 ).flatten(),  \
                                    "OCCUPATION: Unemployed"                     : np.where( col == 9, 1, 0 ).flatten()  \
                            }   )

    col =  marketingData.loc[ :,  ["Lived Here how long" ]]
    LivedDf = pd.DataFrame(  { \
                                    "LIVED: Less than one year"     : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "LIVED: One to three years"     : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "LIVED: Four to six years"      : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "LIVED: Seven to ten years"     : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "LIVED: More than ten years"    : np.where( col == 5, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["DUAL INCOMES"] ]
    dualDf = pd.DataFrame(  { \
                                    "DUAL: Not Married"     : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "DUAL: Yes"             : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "DUAL: Nos"             : np.where( col == 3, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["PERSONS IN YOUR HOUSEHOLD" ]]
    houseHoldSizeDf = pd.DataFrame(  { \
                                    "HOUSEHOLD SIZE: One"           : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Two"           : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Three"         : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Four"          : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Five"          : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Six"           : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Seven"         : np.where( col == 7, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Eight"         : np.where( col == 8, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE: Nine or more"  : np.where( col == 9, 1, 0 ).flatten()  \
                            }   )

    col =  marketingData.loc[ :,  ["PERSONS IN HOUSEHOLD UNDER 18" ]]
    houseHoldSizeUnder18Df = pd.DataFrame(  { \
                                    "HOUSEHOLD SIZE 18: One"           : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Two"           : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Three"         : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Four"          : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Five"          : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Six"           : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Seven"         : np.where( col == 7, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Eight"         : np.where( col == 8, 1, 0 ).flatten(),  \
                                    "HOUSEHOLD SIZE 18: Nine or more"  : np.where( col == 9, 1, 0 ).flatten()  \
                            }   )

    col =  marketingData.loc[ :,  ["HOUSEHOLDER STATUS"] ]
    houseHolderDf = pd.DataFrame(  { \
                                    "HOUSEHOLDER: Own"                          : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "HOUSEHOLDER: Rent"                         : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "HOUSEHOLDER: Live with Parents/Family"     : np.where( col == 3, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["TYPE OF HOME" ]]
    HomeTypeDf = pd.DataFrame(  { \
                                    "HOME_TYPE: House"          : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "HOME_TYPE: Condominium"    : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "HOME_TYPE: Apartment"      : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "HOME_TYPE: Mobile Home"    : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "HOME_TYPE: Other"          : np.where( col == 5, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["ETHNIC CLASSIFICATION" ]]
    EthniceDf = pd.DataFrame(  { \
                                    "ETHNIC: American Indian"   : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "ETHNIC: Asian"             : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "ETHNIC: Black"             : np.where( col == 3, 1, 0 ).flatten(),  \
                                    "ETHNIC: East Indian"       : np.where( col == 4, 1, 0 ).flatten(),  \
                                    "ETHNIC: Hispanic"          : np.where( col == 5, 1, 0 ).flatten(),  \
                                    "ETHNIC: Pacific Islander"  : np.where( col == 6, 1, 0 ).flatten(),  \
                                    "ETHNIC: White"             : np.where( col == 7, 1, 0 ).flatten(),  \
                                    "ETHNIC: Other"             : np.where( col == 8, 1, 0 ).flatten() \
                            }   )

    col =  marketingData.loc[ :,  ["WHAT LANGUAGE IS SPOKEN MOST OFTEN IN YOUR HOME?"] ]
    languageDf = pd.DataFrame(  { \
                                    "LANGUAGE: English"     : np.where( col == 1, 1, 0 ).flatten(),  \
                                    "LANGUAGE: Spanish"     : np.where( col == 2, 1, 0 ).flatten(),  \
                                    "LANGUAGE: OtherNos"    : np.where( col == 3, 1, 0 ).flatten() \
                            }   )

    return( pd.concat( [incomeDf, sexDf, MARITALDf, ageDf, educationDf, occipationDf, LivedDf, dualDf, houseHoldSizeDf, houseHoldSizeUnder18Df, houseHolderDf, HomeTypeDf, EthniceDf, languageDf ], axis = 1  ) )

marketingData = getDate()
support = 0.10
confidence = 0.7
# get single-item sets
x = (marketingData.mean( axis = 0) >= support)
marketingData = marketingData.loc[ : , x ]

# gets the rest of the item sets
k = marketingData.shape[1]
K = { "size 1": list( range(0,k)) }
size = 2
largestGroupSize = k
Association = []
while largestGroupSize > 0:

    if size == 2:
        temp = []
        for i in range(k):
            temp.append(i)
        newGroupings =   list( combinations( temp , size) )

    else:
        newGroupings = []
        for group in largestSizeGroupings:
            for i in range(k):
                if i not in group:
                    t = list(group + (i, ))
                    t.sort()
                    t = tuple(t)
                    if t not in newGroupings:
                        if len( set( list( combinations( t , size-1) ) ) - set( largestSizeGroupings ) ) == 0:
                            newGroupings.append( t )

    largestSizeGroupings = []
    for group in newGroupings:
        if np.mean(  marketingData.iloc[ :,  list(group) ].sum( axis = 1)  == size ) >= support:
            largestSizeGroupings.append( group )

    largestGroupSize = len(largestSizeGroupings)

    if largestGroupSize > 0:
        K["size "+str(size)] = largestSizeGroupings
        if size > 2:
            for group in largestSizeGroupings:
                subset = list( combinations( group , size-1) )
                for subGroup in subset:
                    c =  (np.sum((marketingData.iloc[ :,  list(group) ].sum( axis = 1)  == size)) /  np.sum((marketingData.iloc[ :,  list(subGroup) ].sum( axis = 1)  == size -1)))
                    if c  > confidence :
                        Association.append( { "confidence" : c, 'antecedent' : tuple( subGroup ), 'consequent' : group, 'support' :  np.mean(  marketingData.iloc[ :,  list(group) ].sum( axis = 1)  == size )  } )

    size += 1

Association = sorted( Association, key = lambda k: k["confidence"], reverse=True)[0:10]

f, (ax1, ax2) = plt.subplots(1, 2, sharey=False)
ax1.barh(  np.arange(10),    list( map( lambda d: d["support"], Association )),  tick_label = list( map( lambda d: str( d["consequent"]), Association ))  )
ax1.set_title("Support")
ax2.barh(  np.arange(10),    list( map( lambda d: d["confidence"], Association )),  tick_label = list( map( lambda d: str( d["antecedent"]), Association ))  )
ax2.set_title("Confidence")
plt.show()

Machine Learning(python)/Unsupervised Learning/Association Rules.py


from flask import Blueprint, request
import json

from src.schema.league import League

bp = Blueprint('schedule', __name__)

@bp.route('/schedule', methods=['GET'])
@bp.route('/api/schedule', methods=['GET'])
def schedule():
    """
    Return upcoming games for a given league

    Parameters:
        league: id of the league to get schedule for

    Example:
        curl /schedule?league=3

    Example response:
        {
            "games": [
                {
                    "blue_side_team_fb_odds" : 2.1,
                    "blue_side_team_fbaron_odds" : 2.62,
                    "blue_side_team_fd_odds" : 2.2,
                    "blue_side_team_ft_odds" : 2.25,
                    "blue_side_team_id" : 119,
                    "date" : "2019-08-22 00:00:00",
                    "id" : 1077,
                    "league_id" : 2,
                    "loser_id" : null,
                    "red_side_team_fb_odds" : 1.66,
                    "red_side_team_fbaron_odds" : 1.44,
                    "red_side_team_fd_odds" : 1.61,
                    "red_side_team_ft_odds" : 1.57,
                    "red_side_team_id" : 4,
                    "split_id" : 4,
                    "winner_id" : null
                }
            ],
            "league_id" 2
        }
    """
    league_id = int(request.args.get('league'))
    league = League.get_by_id(league_id)
    split = league.current_split()
    unplayed_games = split.unplayed_games()

    return json.dumps([g.to_json() for g in unplayed_games])


import json
import re
from pprint import pprint

import scrapy

import constants as keys
from spiders.spider_modules.base import BaseSpider
from spiders.spider_modules.search_helper import (
    CUSTOM_SEARCHBOX_SETTINGS,
    BarcodeSearchHelper,
)

class EveShopSearcher(BaseSpider):
    name = "search_eve"
    custom_settings = CUSTOM_SEARCHBOX_SETTINGS

    def __init__(self, *args, **kwargs):
        barcodes_to_search = BarcodeSearchHelper.get_barcodes_to_search(
            keys.EVESHOP, keys.COSMETICS_MARKETS
        )
        self.barcode_generator = BarcodeSearchHelper.barcode_generator(
            barcodes_to_search
        )
        super(EveShopSearcher, self).__init__(*args, base_domain="eveshop.com.tr")

    def start_requests(self):
        base = "https://www.eveshop.com.tr/userControls/kutu/ascArama_ajx.aspx?searchType=0&suggType=2&srt=ES&lang=tr-TR&text="

        for barcode in self.barcode_generator:
            url = base + barcode
            yield scrapy.Request(
                url, callback=self.parse, meta={keys.BARCODES: barcode}
            )

    @staticmethod
    def extract_product_json(response):
        search_results = response.css("::text").extract()
        search_results = " ".join(search_results)
        products_match = re.findall(r"\"products\":\[(.*?)\]", search_results)

        if not products_match:
            return

        products_str = products_match.pop()

        if not products_str:
            return

        product_json = json.loads(products_str)
        return product_json

    @staticmethod
    def extract_product_info(product_json, base_url):
        pprint(product_json)
        href = product_json.get("productUrl", "")
        name = product_json.get("productName", "")
        price = product_json.get("productSalesPrice", "")

        if not (href and price and name):
            return

        res = dict()

        link = base_url + href
        res[keys.LINK] = link

        """
        brand = link.split("/")[3].strip().replace("-", " ")
        print(brand)
        if brand.lower() not in name.lower():
            name = " ".join([brand, name]).title()

        """
        res[keys.NAME] = name

        price = (
            price.replace("TL", "")
            .strip()
            .replace(".", "")
            .replace(",", ".")
            .replace(" ", "")
        )

        try:
            res[keys.PRICE] = float(price)
        except TypeError as e:
            print(e)

        return res

    @staticmethod
    def get_product(response, base_url, market_name):
        product_json = EveShopSearcher.extract_product_json(response)
        if product_json:
            result = EveShopSearcher.extract_product_info(product_json, base_url)
            if result:
                result[keys.BARCODES] = response.meta.get(keys.BARCODES)
                result[keys.MARKET] = market_name
                return result

    def parse(self, response):
        product = self.get_product(response, self.base_url, keys.EVESHOP)
        if product:
            yield product


import collections
import math

import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.offline as pltoff

from celescope.tools.utils import add_log

BarcodeRankPlotSegment = collections.namedtuple('BarcodeRankPlotSegment', ['start', 'end', 'cell_density', 'legend'])

CHARTS_PLOTLY_MODEBAR_TRANSFORM_BUTTONS = [
    'zoom2d',
    'pan2d',
    'zoomIn2d',
    'zoomOut2d',
    'autoScale2d',
    # 'resetScale2d'  can't totally disable interaction, it seems-- keep reset option
]

CHARTS_PLOTLY_EXPORT_BUTTONS = [
    'toImage',
    'sendDataToCloud',
]

CHARTS_PLOTLY_FIXED_CONFIG = {
    'modeBarButtonsToRemove': CHARTS_PLOTLY_MODEBAR_TRANSFORM_BUTTONS+CHARTS_PLOTLY_EXPORT_BUTTONS,
    'displaylogo': False,
    'showLink': False
}

CHARTS_PLOTLY_MOVABLE_CONFIG = {
    'modeBarButtonsToRemove': CHARTS_PLOTLY_EXPORT_BUTTONS,
    'displaylogo': False,
    'showLink': False
}
BC_RANK_PLOT_LINE_WIDTH = 3
# Gradient scheme used in the barcode rank plot
BC_PLOT_COLORS = ['#dddddd', '#d1d8dc', '#c6d3dc', '#bacfdb', '#aecada', '#a3c5d9', '#97c0d9', '#8cbbd8', '#80b7d7',
                  '#74b2d7', '#6aadd6', '#66abd4', '#62a8d2', '#5ea5d1', '#59a2cf', '#559fce', '#519ccc', '#4d99ca',
                  '#4997c9', '#4594c7', '#4191c5', '#3d8dc4', '#3a8ac2', '#3787c0', '#3383be', '#3080bd', '#2c7cbb',
                  '#2979b9', '#2676b7', '#2272b6', '#1f6eb3', '#1d6ab0', '#1a65ac', '#1861a9', '#155ca6', '#1358a2',
                  '#10539f', '#0e4f9b', '#0b4a98', '#094695', '#09438f', '#0a4189', '#0c3f83', '#0d3d7c', '#0e3b76',
                  '#103970', '#11366a', '#123463', '#14325d', '#153057']

CHARTS = [
    {
        'layout': {
            'title': 'Barcode Rank',
            'width': 470,
            'height': 313,
            'margin': {'l': 60, 'r': 0, 't': 30, 'b': 40},
            'hovermode': 'closest',
            'xaxis': {
                'title': 'Barcodes',
                'type': 'log',
            },
            'yaxis': {
                'title': 'UMI counts',
                'type': 'log',
            },
        },
        'data': [],
        'config': CHARTS_PLOTLY_FIXED_CONFIG,
        'function': '_plot_barcode_rank',
        'description': 'The plot shows the count of filtered UMIs mapped to each barcode.  Barcodes can be determined to be cell-associated based on their UMI counts or by their expression profiles. Therefore some regions of the graph can contain both cell-associated and background-associated barcodes. The color of the graph represents the local density of barcodes that are cell-associated.',
        'name': 'barcode_rank',
    },
]

def BC_PLOT_CMAP(density):
    """
    Colormap utility fn to map a number to one of the colors in the gradient
    color scheme defined above
    Input
    - density : A real number in the range [0,1]
    """
    assert density >= 0.
    assert density <= 1.
    levels = len(BC_PLOT_COLORS)
    ind = min(levels - 1, int(math.floor(levels * density)))
    return BC_PLOT_COLORS[ind]

def get_plot_segment(start_index, end_index, sorted_bc, cell_barcodes, legend=False):
    """
    Helper function to build a plot segment.
    """
    assert end_index > start_index
    num_cells = sum([1 for i in range(start_index, end_index) if sorted_bc[i] in cell_barcodes])
    density = float(num_cells)/float(end_index-start_index)
    return BarcodeRankPlotSegment(start=start_index, end=end_index, cell_density=density, legend=legend)

def segment_log_plot_by_length(y_data, x_start, x_end):
    """
    Given the extends of the mixed region [x_start, x_end), compute
    the x-indices that would divide the plot into segments of a
    prescribed length (in pixel coordinates) with a minimum number
    of barcodes in each segment
    """

    if x_end <= x_start:
        return []

    SEGMENT_NORMALIZED_MAX_LEN = 0.02
    MIN_X_SPAN = 20

    log_max_x = np.log(len(y_data))
    log_max_y = np.log(max(y_data))

    this_segment_len = 0.0
    segment_idx = [x_start]

    for i in range(x_start, x_end):
        last_i = max(x_start, i-1)
        dx = (np.log(i) - np.log(last_i)) / log_max_x
        dy = (np.log(y_data[i]) - np.log(y_data[last_i])) / log_max_y
        this_segment_len += np.linalg.norm([dx, dy])
        if this_segment_len >= SEGMENT_NORMALIZED_MAX_LEN and i > (segment_idx[-1] + MIN_X_SPAN):
            segment_idx.append(i+1)
            this_segment_len = 0.0

    if segment_idx[-1] != x_end:
        segment_idx.append(x_end)

    return segment_idx

def segment_log_plot_by_length_log_uniform(x_start, x_end):
    """
    vdj usually have very large x_end
    """

    if x_end <= x_start:
        return []

    MIN_X_LOG10_SPAN = 0.03
    MIN_BARCODE = 20
    MAX_N = 100
    segment_idx = [x_start]
    span = np.log10(x_end-x_start) / MAX_N
    if span < MIN_X_LOG10_SPAN:
        span = MIN_X_LOG10_SPAN

    span_sum = 1
    idx = x_start
    while True:
        span_sum += span
        idx = int(x_start + 10 ** span_sum)
        if idx - segment_idx[-1] < MIN_BARCODE:
            continue
        if idx > x_end:
            break
        segment_idx.append(idx)

    if segment_idx[-1] != x_end:
        segment_idx.append(x_end)

    return segment_idx

def convert_numpy_array_to_line_chart(array, ntype):
    array = np.sort(array)[::-1]

    rows = []
    previous_count = None
    for (index,), count in np.ndenumerate(array):
        if index == 0 or index == len(array)-1:
            rows.append([index, ntype(count)])
        elif previous_count != count:
            previous_index = rows[-1][0]
            if previous_index != index - 1:
                rows.append([index - 1, ntype(previous_count)])
            rows.append([index, ntype(count)])
        previous_count = count
    return rows

@add_log
def counter_barcode_rank_plot_data(df_UMI_path, log_uniform=False):
    """
    get cell density for each plot_segments
    :param count_data_path:
    :return: sorted_counts, plot_segments, cell_nums
    """
    df_UMI = pd.read_csv(df_UMI_path, index_col=0, sep='\t')
    cell_bc = set(df_UMI[df_UMI['mark'] == 'CB'].index)
    sorted_bc = np.array(df_UMI.index)
    sorted_counts = np.array(df_UMI['UMI'])
    cell_nums = len(cell_bc)
    total_bc = len(sorted_bc)
    # find the first barcode which is not a cell
    first_non_cell = total_bc
    for i, bc in enumerate(sorted_bc):
        if bc not in cell_bc:
            first_non_cell = i
            break

    # find the last barcode which is a cell
    last_cell = 0
    for i in reversed(range(total_bc)):
        if sorted_bc[i] in cell_bc:
            last_cell = i
            break

    ranges = [0, first_non_cell, last_cell+1, total_bc]
    plot_segments = []
    plot_segments.append(BarcodeRankPlotSegment(start=0, end=ranges[1], cell_density=1.0, legend=True))
    plot_segments.append(BarcodeRankPlotSegment(start=ranges[2], end=ranges[3], cell_density=0.0, legend=True))

    if not log_uniform:
        mixed_segments = segment_log_plot_by_length(sorted_counts, ranges[1], ranges[2])
    else:
        mixed_segments = segment_log_plot_by_length_log_uniform(ranges[1], ranges[2])
    for i in range(len(mixed_segments) - 1):
        plot_segments.append(
            get_plot_segment(mixed_segments[i], mixed_segments[i + 1], sorted_bc, cell_bc, legend=False))

    return sorted_counts, plot_segments, cell_nums

def _plot_barcode_rank(chart, counts, num_cells):
    """ Generate a generic barcode rank plot """
    rows = convert_numpy_array_to_line_chart(counts, int)

    for _i, row in enumerate(rows):
        index, count = row[0], row[1]
        if index < num_cells:
            series_list = [chart['data'][0]]
        elif index == num_cells:
            # Connect the two lines
            series_list = [chart['data'][0], chart['data'][1]]
        else:
            series_list = [chart['data'][1]]

        for series in series_list:
            series['x'].append(index)
            series['y'].append(count)

    # Handle case where there is no background
    bg_series = chart['data'][1]
    if len(bg_series['x']) == 0:
        bg_series['x'].append(0)
        bg_series['y'].append(0)

    return chart

def build_plot_data_dict(plot_segment, counts):
    """
    Construct the data for a plot segment by appropriately slicing the
    counts
    Inputs:
    - plot_segment: BarcodeRankPlotSegment containing [start, end)
        of the segment, the cell density and legend visibility option
    - counts: Reverse sorted UMI counts for all barcodes.
    """

    start = max(0, plot_segment.start - 1)  # -1 for continuity between two charts
    end = plot_segment.end
    plot_rows = convert_numpy_array_to_line_chart(counts[start:end], int)
    name = 'Cells' if plot_segment.cell_density > 0 else 'Background'

    # Setup the tooltip
    if plot_segment.cell_density > 0.:
        n_barcodes = plot_segment.end - plot_segment.start
        n_cells = int(round(plot_segment.cell_density * n_barcodes))
        hover = "{:.0f}% Cells<br>({}/{})".format(100 * plot_segment.cell_density, n_cells, n_barcodes)
    else:
        hover = "Background"

    data_dict = {
        "x": [],
        "y": [],
        "name": name,
        "hoverinfo": "text",
        "text": hover,
        "type": "scattergl",
        "mode": "lines",
        "line": {
            "width": 3,
            "color": BC_PLOT_CMAP(plot_segment.cell_density),
        },
        "showlegend": plot_segment.legend,
    }
    offset = 1 + start  # it's a log-log plot, hence the 1
    for index, count in plot_rows:
        data_dict["x"].append(index + offset)
        data_dict["y"].append(count)

    # Handle case where the data is empty
    if len(data_dict["x"]) == 0:
        data_dict["x"].append(0)
        data_dict["y"].append(0)

    return data_dict

def _plot_counter_barcode_rank(chart, counts, plot_segments):
    """
    Generate the RNA counter barcode rank plot
    Inputs:
        - chart: chart element to populate data
        - counts: UMI counts reverse sorted
        - plot_segments: A list of BarcodeRankPlotSegments
    """

    for segment in plot_segments:
        chart["data"].append(build_plot_data_dict(segment, counts))

    return chart

@add_log
def get_plot_data(plot_segments, counts):
    plot_data = []
    for segment in plot_segments:
        plot_data.append(build_plot_data_dict(segment, counts))

    return plot_data

def plot_barcode_rank(count_file_path, log_uniform=False):
    sorted_counts, plot_segments, _cell_nums = counter_barcode_rank_plot_data(count_file_path, log_uniform=log_uniform)
    plot_data = get_plot_data(plot_segments, sorted_counts)

    plotly_data = [go.Scatter(x=dat['x'], y=dat['y'], name=dat['name'], mode=dat['mode'], showlegend=dat['showlegend'],
                              marker={'color': dat['line']['color']}, line=dat['line'], text=dat['text']) for dat in
                   plot_data]

    layout = go.Layout(width=470, height=313,
                       title={"text": "Barcode rank", "font": {"color": "black"}, "x": 0.5},
                       xaxis={"type": "log", "title": "Barcode", "titlefont": {"color": "black"},
                              "color": "black", "gridcolor": "gainsboro", "linecolor": "black"},
                       yaxis={"type": "log", "title": "UMI counts", "titlefont": {"color": "black"},
                              "color": "black", "gridcolor": "gainsboro", "linecolor": "black"},
                       margin=dict(l=50, r=0, t=30, b=30),
                       plot_bgcolor="#FFFFFF")

    config = dict({"displayModeBar": True,
                   "staticPlot": False,
                   "showAxisDragHandles": False,
                   "modeBarButtons": [["toImage", "resetScale2d"]],
                   "scrollZoom": False,
                   "displaylogo": False})

    fig = go.Figure(data=plotly_data, layout=layout)

    chart = pltoff.plot(fig, include_plotlyjs=False, output_type='div', config=config)

    return chart


import sys

T=int(input())
for i in range(T):
    ascii_list=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    Upper={}
    Lower={}
    for i in ascii_list: Upper[i]=0; Lower[i]=0
    A, B=map(int,input().split())
    Str=input()
    for i in range(A):
        if Str[i].isupper():
            if Str[i] in Upper.keys():
                Upper[Str[i]]+=1
            else:
                Upper[Str[i]]=1
        else:
            if Str[i].upper() in Lower.keys():
                Lower[Str[i].upper()]+=1
            else:
                Lower[Str[i].upper()]=1
    ans=0
    ascii_list=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    for a in ascii_list:
        if a in Upper.keys() or a in Lower.keys():
            Up=Upper[a]; Low=Lower[a]
            # print(a,Up,Low)
            ans+=min(Up,Low)
            temp=max(Up,Low)-min(Up,Low)
            if temp>=2:
                if temp//2<=B:
                    B-=temp//2
                    ans+=temp//2
                else:
                    ans+=B
                    B=0

    print(ans)


#Word Counter
def count_words(filepath):
    with open(filepath, 'r') as file:
        strng = file.read()
        strng_list = strng.split(" ")
        return len(strng_list)

print(count_words("words1.txt"))

'''
The function here takes as input a file path. If the file path is in the same directory as your Python script, you can pass in the file name as in the above script. If your text file is somewhere else, then you need to pass the full path when calling the function.

The rest of the script consists of opening the file in read  mode, loading the text into a string using read  and then splitting and counting the number of words.

'''


import sys
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import pandas as pd
import math as m
import time
import os

win_w, win_h = 1024, 768
models = {}

def reshape(w, h):
    global win_w, win_h
    win_w, win_h = w, h
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(30, w/h, 1, 100)

wireframe, pause = False, True
def keyboard(key, x, y):
    global wireframe, pause

    key = key.decode("utf-8")
    if key == ' ':
        pause = not pause
        glutIdleFunc(None if pause else idle)
    elif key == 'w':
        wireframe = not wireframe
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE if wireframe else GL_FILL)
    elif key == 'q':
        os._exit(0)
    glutPostRedisplay()

tick, frame_cnt = 0, 0
def idle():
    global tick, frame_cnt

    tick += 1
    frame_cnt += 1
    glutPostRedisplay()

def display():
    global start_time, frame_cnt
    if frame_cnt == 20:
        print("%.2f fps" % (frame_cnt/(time.time()-start_time)), tick, end='\r')
        start_time = time.time()
        frame_cnt = 0
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(*(centroid+(9, 13, max(bbox)*1.6)),1,-7,-5 , 0, 1, 0)

    glVertexPointer(3, GL_FLOAT, 0, positions_bunny)
    glColorPointer(3, GL_FLOAT, 0, colors_bunny)
    glPushMatrix()
    glScalef(0.5,0.5,0.5)
    for i in range(0,5):
        for j in range(0,6):
            glTranslatef(2,0,0)
            glDrawArrays(GL_TRIANGLES, 0, n_vertices_bunny)
        glTranslatef(-12,0,-2.3)
    glPopMatrix()

    #horse 1 model
    glVertexPointer(3, GL_FLOAT, 0, positions_horse)
    glColorPointer(3, GL_FLOAT, 0, colors_horse)
    glPushMatrix()
    glTranslatef(2.4,0,-5.8)
    glRotatef(90,0,1,0)
    glScalef(0.9,0.9,0.9)
    glDrawArrays(GL_TRIANGLES, 0, n_vertices_horse)
    glPopMatrix()

    #monkey 1 model
    glPushMatrix()
    glVertexPointer(3, GL_FLOAT, 0, positions_monkey)
    glColorPointer(3, GL_FLOAT, 0, colors_monkey)
    glTranslatef(4.4,0,-6.5)
    glDrawArrays(GL_TRIANGLES, 0, n_vertices_monkey)
    glPopMatrix()

    glutSwapBuffers()

def gl_init_model(model_filename,kd):
    global n_vertices, positions, colors, normals, uvs
    global centroid, bbox, start_time

    glClearColor(0, 0, 0, 0)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)

    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_COLOR_ARRAY)

    df = pd.read_csv(model_filename, delim_whitespace=True, comment='#',
                     header=None, dtype=np.float32)
    centroid = df.values[:, 0:3].mean(axis=0)
    bbox = df.values[:, 0:3].max(axis=0) - df.values[:, 0:3].min(axis=0)

    positions = df.values[:, 0:3]
    colors = df.values[:, 3:6]
    normals = df.values[:, 6:9]
    uvs = df.values[:, 9:11]
    n_vertices = len(positions)

    L = light_pos - positions
    L = L * 1/np.linalg.norm(L,axis=1).reshape(-1,1)

    N = normals
    NdotL = np.sum(N * L, axis=1).reshape(-1,1)

    diffuse = kd * NdotL * Il

    return positions,diffuse,n_vertices,centroid

def gl_init():
    global start_time, light_pos, Il
    global positions_bunny, colors_bunny, n_vertices_bunny
    global positions_horse, colors_horse, n_vertices_horse
    global positions_monkey, colors_monkey, n_vertices_monkey

    glClearColor(0, 0, 0, 0)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)

    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_COLOR_ARRAY)

    light_pos = np.array((9,13,1.6))
    Il = np.array((1.0,1.0,1.0))

    kdBunny = np.array((1.0,0.0,1.0))
    kdHorse = np.array((1.0,0.5,0.0))
    kdMonkey = np.array((0.5,1.0,0.5))

    positions_bunny, colors_bunny,n_vertices_bunny, centroid = gl_init_model("../models/bunny_uv.tri",kdBunny)
    positions_horse, colors_horse,n_vertices_horse, centroid = gl_init_model("../models/horse_uv.tri",kdHorse)
    positions_monkey, colors_monkey,n_vertices_monkey, centroid = gl_init_model("../models/monkey.tri",kdMonkey)

    start_time = time.time() - 0.0001

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(win_w, win_h)
    glutCreateWindow("Bunny exercise")
    glutDisplayFunc(display)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(keyboard)
    glutIdleFunc(idle)
    gl_init()
    glutMainLoop()

if __name__ == "__main__":
    main()


# -*- coding:utf8 -*-
import cv2
from pylab import *

image = cv2.imread("../image/church.jpg")
# 将原图像进行边界扩展，并将其转换为灰度图
image = cv2.resize(image, (1080, 1080))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

def RobertsOperator(roi):
    operator_x = np.array([[-1, 0], [0, 1]])
    operator_y = np.array([[0, -1], [1, 0]])
    return np.abs(np.sum(roi[1:, 1:] * operator_x)) + np.abs(np.sum(roi[1:, 1:] * operator_y))

def RobertsAlogrithm(image):
    image = cv2.copyMakeBorder(image, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
    for i in range(1, image.shape[0]):
        for j in range(1, image.shape[1]):
            image[i, j] = RobertsOperator(image[i - 1:i + 2, j - 1:j + 2])
    return image[1:image.shape[0], 1:image.shape[1]]

Roberts_image = RobertsAlogrithm(image)
plt.imshow(Roberts_image, cmap="binary")
plt.axis("off")
plt.savefig("Edge image by Roberts operator")


from .models import Product
from rest_framework import serializers
from django_countries.serializers import CountryFieldMixin

class ProductSerializers(CountryFieldMixin, serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = "__all__"
        extra_kwargs = {
            'password': {'write_only': True},
            'user': {'write_only': True},
            'is_active': {'write_only': True},
            'token': {'write_only': True},
        }

        def update(self, product, validated_data):
            product.country = validated_data.get("country", product.country)
            product.automated_control = validated_data.get("automated_control", product.automated_control)
            product.time_between_automated_action = validated_data.get("time_between_automated_action",
                                                                       product.time_between_automated_action)
            product.save()

            return product


"""
This module takes care of starting the API Server, Loading the DB and Adding the endpoints
"""
import os
from flask import Flask, request, jsonify, url_for
from flask_migrate import Migrate
from flask_swagger import swagger
from flask_cors import CORS
from utils import APIException, generate_sitemap
from admin import setup_admin
from models import db, User, People, Planets, Favorites

app = Flask(__name__)
app.url_map.strict_slashes = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_CONNECTION_STRING')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
MIGRATE = Migrate(app, db)
db.init_app(app)
CORS(app)
setup_admin(app)

# Handle/serialize errors like a JSON object
@app.errorhandler(APIException)
def handle_invalid_usage(error):
    return jsonify(error.to_dict()), error.status_code

# generate sitemap with all your endpoints
@app.route('/')
def sitemap():
    return generate_sitemap(app)

@app.route('/user', methods=['GET'])
def handle_hello():

    response_body = {
        "msg": "Hello, this is your GET /user response "
    }

    return jsonify(response_body), 200

    # General calls for users, people and planets:

@app.route('/users', methods=['GET'])
def handle_users():
    users = User.query.all()
    response_body = []
    for user in users:
        response_body.append(user.serialize())

    return jsonify(response_body), 200

@app.route('/people', methods=['GET'])
def handle_people():
    people = People.query.all()
    response_body = []
    for character in people:
        response_body.append(character.serialize())
    return jsonify(response_body), 200

@app.route('/planets', methods=['GET'])
def handle_planets():
    planets = Planets.query.all()
    response_body = []
    for planet in planets:
        response_body.append(planet.serialize())
    return jsonify(response_body), 200

 # Individual call for people and planets:

@app.route('/people/<int:id>', methods=['GET'])
def get_people_by_id(id):
    people = People.get_by_id(id)
    if people:
        return jsonify(people.serialize()), 200
    return ({'error': 'Character not found'}), 404

@app.route('/planets/<int:id>', methods=['GET'])
def get_planet_by_id(id):
    planet = Planets.get_by_id(id)
    if planet:
        return jsonify(planet.serialize()), 200
    return ({'error': 'Planet not found'}), 404

 # calls, adds and removes favorites:

@app.route('/favorites/<int:id>', methods=['GET'])
def handle_favorites(id):
    favorites = Favorites.query.filter_by(user_id = id).all()
    response_body = []
    for favorite in favorites:
        response_body.append(favorite.serialize())
    return jsonify(response_body), 200

@app.route('/favorites/<int:id>', methods=['GET'])
def handle_favorite(id):
    favorites = Favorites.query.filter_by(item_id = id).all()
    response_body = []
    for favorite in favorites:
        response_body.append(favorite.serialize())
    return jsonify(response_body), 200

@app.route('/favorite-add', methods=['GET','POST'])
def add_favorite_planet():
    user_id = request.form.get('user_id')
    item_id = request.form.get('item_id')
    item_name = request.form.get('item_name')
    category = request.form.get('category')
    item = Favorites( user_id=user_id, item_id=item_id, item_name=item_name, category=category)
    db.session.add(item)
    db.session.commit()
    favorites = Favorites.query.filter_by(user_id = user_id).all()
    response_body = []
    for favorite in favorites:
        response_body.append(favorite.serialize())
    return jsonify(response_body), 200

@app.route('/favorite-delete/<int:user_id>/<int:category>/<int:item_id>', methods=['GET','POST'])
def delete_favorite_people(user_id, category, item_id):
    old = Favorites.query.filter_by(user_id = user_id, category = category, item_id = item_id).first()
    db.session.delete(old)
    db.session.commit()
    favorites = Favorites.query.filter_by(user_id = 1).all()
    response_body = []
    for favorite in favorites:
        response_body.append(favorite.serialize())
    return jsonify(response_body), 200

# this only runs if `$ python src/main.py` is executed
if __name__ == '__main__':
    PORT = int(os.environ.get('PORT', 3000))
    app.run(host='0.0.0.0', port=PORT, debug=False)


import setuptools

with open("README.md", "r", encoding="utf-8") as f:
    long_description = f.read()

description = "A wrapper over MavSDK to control a UAV through a high-level interface."

setuptools.setup(
    name="Moonlander",
    version="0.1.1",
    author="Jane Lewis",
    author_email="lewijack@oregonstate.edu",
    description=description,
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/OSURoboticsClub/Moonlander",
    project_urls= {
        "Issues": "https://github.com/OSURoboticsClub/Moonlander/issues"
    },
    packages=['moonlander'],
    install_requires=['mavsdk'],
)


# Viterbi Part-of-Speech Tagger by Matthew Keeran
# runs via python viterbi.py
# I personally pipe the output to a text file, then enter the file names one by one followed by enter.	python viterbi.py > output.txt
# written in python 3.5.4 on Windows 8.1, produces all desired output all in correct format.		pos.train.txt
# tested with python 3.6 on Ubuntu 16, produces all desired output all in correct format.		sample_3.txt

#--------------------------------------------------------------------------------------------------------------------------------#
def readtokens(f):
	# Bi-grams, Lexicals, Sentences
	f = open(f, 'r')
	tokens, tokcounts, startcount = {}, [], []
	wordtags, wordtagcount, l = {}, [], {}
	sentences, flag, k = 0, 0, 0
	prevtag, transitions, transcount = "", {}, []

	for line in f:

		if(line == '\n'):
			sentences += 1
			flag = 1
			continue

		words = line.split()
		words[0] = words[0].lower()

		if((words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 's' and words[0][len(words[0]) - 4] == 's') or (words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 1] == 'x')):
			words[0] = words[0][0:len(words[0]) - 2]
		elif((words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 's') or (words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 'z')):
			words[0] = words[0][0:len(words[0]) - 1]
		elif((words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 'h' and words[0][len(words[0]) - 4] == 'c') or (words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 'h' and words[0][len(words[0]) - 4] == 's')):
			words[0] = words[0][0:len(words[0]) - 2]
		elif(words[0][len(words[0]) - 1] == 'n' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 'm'):
			words[0] = words[0][0:len(words[0]) - 2]
			words[0] += "an"
		elif(words[0][len(words[0]) - 1] == 's' and words[0][len(words[0]) - 2] == 'e' and words[0][len(words[0]) - 3] == 'i'):
			words[0] = words[0][0:len(words[0]) - 3]
			words[0] += "y"

		if(not(words[0] in l)):
			l[words[0]] = 1

		if(not(words[1] in tokens)):
			tokens[words[1]] = [1, 0]
		elif(words[1] in tokens):
			tokens[words[1]][0] += 1

		if(not((words[0],words[1]) in wordtags)):
			wordtags[(words[0],words[1])] = 1
		elif((words[0],words[1]) in wordtags):
			wordtags[(words[0],words[1])] += 1

		if(flag == 1):
			tokens[words[1]][1] += 1

		if(k == 0):
			tokens[words[1]][1] += 1
			k = 1

		if(prevtag != "" and not((words[1],prevtag) in transitions) and flag == 0):
			transitions[(words[1],prevtag)] = 1
		elif(prevtag != "" and (words[1],prevtag) in transitions and flag == 0):
			transitions[(words[1],prevtag)] += 1
		elif(not((words[1],"  ") in transitions) and flag == 1):
			transitions[(words[1],"  ")] = 1
			flag = 0
		elif((words[1],"  ") in transitions and flag == 1):
			transitions[(words[1],"  ")] += 1
			flag = 0

		prevtag = words[1]

	tokdist, emissions = [], {}
	# initial dist i = # of occurances of \n\n i / # of sentences in corpus

	for item in tokens:
		tokens[item].append(float(tokens[item][1] / (sentences + 1)))

	# emission i = # of occurances of word i with given tag  / # times tag occurs

	for item in wordtags:
		emissions[item] = float(wordtags[item] / tokens[item[1]][0])

	emiss = []
	for key, value in emissions.items():
		temp1 = [key,value]
		emiss.append(temp1)
	emiss.sort()

	toks = []
	for key, value in tokens.items():
		temp1 = [key,value]
		toks.append(temp1)
	toks.sort()

	transit = []
	for key, value in transitions.items():
		temp1 = [key,value]
		transit.append(temp1)
	transit.sort()

	# transition probabilities

	transitionprobs = []

	for i in range(len(toks)):
		row = []
		for j in range(len(transit)):
			if(transit[j][0][1] == toks[i][0]):
				prob = float(transit[j][1] / toks[i][1][0])
				row.append((transit[j][0], prob))

		transitionprobs.append(row)

	return toks, emiss, transitionprobs, sentences, len(l)

#--------------------------------------------------------------------------------------------------------------------------------#
def test(f):
	f = open(f, 'r')
	words = []
	for line in f:
		l = line.split()
		for i in range(len(l)):
			words.append(l[i])

	return words

#--------------------------------------------------------------------------------------------------------------------------------#
def findtags(d, e):															# finding emission probabilities of test words
	tags = []
	for i in range(len(d)):
		tags.append([])
		flag = 0
		for j in range(len(e)):
			if(d[i].lower() == e[j][0][0]):
				tags[i].append((e[j][0][1], e[j][1]))
				flag = 1
		if(flag == 0):
			tags[i].append(("NN", 0))

	return tags

#--------------------------------------------------------------------------------------------------------------------------------#
def viterbi(tokens, sequence, tags, transprobs):
	path, id, trans = [], [], {}

	for i in range(len(tags[0])):											# finding initial probs of first word in sequence
		for j in range(len(tokens)):
			if(tags[0][i][0] == tokens[j][0]):
				id.append(tokens[j][1][2])

	for i in range(len(transprobs)):										# converting transition probs from 2D list to dict
		for j in range(len(transprobs[i])):
				trans[transprobs[i][j][0]] = transprobs[i][j][1]

	seqprobs, t = [], []
	for i in range(len(sequence)):											# actual start of Viterbi
		some = 0
		seqprobs.append([])
		for j in range(len(tags[i])):
			if(i == 0):														# tags = [[(VB, 0.3),(NN,0.1)], [(POS, 0.0)]]
				p = float(tags[i][j][1] * id[j])
				seqprobs[i].append(p)										# seqprobs = [[0.4,0.6],[0.2,0.3,0.5],[0.1,0.8,0.1]]
				some += p
			else:
				currprobs = []
				for k in range(len(seqprobs[i-1])):							# for every sub
					if((tags[i][j][0],tags[i-1][k][0]) in trans):
						currprobs.append(float(seqprobs[i-1][k] * trans[(tags[i][j][0],tags[i-1][k][0])] * tags[i][j][1]))
					else:
						currprobs.append(float(0))							# if transition doesn't exist in corpus prob = 0
				p = max(currprobs)
				indx = maxi(currprobs)
				seqprobs[i].append(p)
				some += p

		seqprobs[i] = normaleyes(seqprobs[i], some)
		j = maxi(seqprobs[i])
		path.append(tags[i][j][0])

	return path, seqprobs

#--------------------------------------------------------------------------------------------------------------------------------#
def normaleyes(l,s):
	normed = []
	for i in range(len(l)):
		normed.append(float(l[i]/s))

	return normed

#--------------------------------------------------------------------------------------------------------------------------------#
def maxi(l):
	tmp, indx = float("-inf"), 0
	for i in range(len(l)):
		if(l[i] > tmp):
			tmp = l[i]
			indx = i
	return indx

#--------------------------------------------------------------------------------------------------------------------------------#
def max(l):
	tmp = float("-inf")
	for i in range(len(l)):
		if(l[i] > tmp):
			tmp = l[i]
	return tmp

#--------------------------------------------------------------------------------------------------------------------------------#

if __name__ == "__main__":
	f1 = input().strip()
	f2 = input().strip()

	tokens, emiss, transp, sentences, lex = readtokens(f1)

	print("\n\nAll Tags Observed:\n")
	for i in range(len(tokens)):
		if(i + 1 < 10):
			print(" " + str(i + 1) + "  " + tokens[i][0])
		else:
			print(str(i + 1) + "  " + tokens[i][0])

	print("\nInitial Distribution:\n")
	for i in range(len(tokens)):
		if(tokens[i][1][2] > 0.000000):
			print("start [ " + tokens[i][0] + " |  ] %.6f" % tokens[i][1][2])

	print("\nEmission Probabilities:\n")

	for i in range(len(emiss)):

		whitespc = 16 - len(emiss[i][0][0])
		w = ""
		for j in range(whitespc):
			w += " "
		w += emiss[i][0][0]

		w2 = ""
		for j in range(5 - len(emiss[i][0][1])):
			w2 += " "
		w2 += emiss[i][0][1]

		print(w + w2 + " %.6f" % emiss[i][1])

	print("\nTransition Probabilities:\n")

	big, some = 0, 0
	s = ""
	for i in range(len(tokens)):
		if(tokens[i][1][2] > 0.000000):
			s += "  [" + tokens[i][0] + "|] %.6f" % tokens[i][1][2]
			big += 1
			some += tokens[i][1][2]
	print("[ %.6f ] " % some + s)

	for i in range(len(transp)):
		s, sum = "", 0
		for j in range(len(transp[i])):
			sum += transp[i][j][1]
			s += "  [" + transp[i][j][0][0] + "|" + transp[i][j][0][1] + "] %.6f" % transp[i][j][1]
			big += 1
			some += transp[i][j][1]

		if(sum < 0.999999):
			print("[ 1.000000 ] " + "  [  |" + transp[i][0][0][1] + "] %.6f" % (1 - sum) + s)
			big += 1
		else:
			print("[ %.6f ] " % sum + s)

	print("\nCorpus Features:\n")
	print("   Total # tags        : " + str(len(tokens)))
	print("   Total # bigrams     : " + str(big))
	print("   Total # lexicals    : " + str(lex - 7))
	print("   Total # sentences   : " + str(sentences))

	sequence = test(f2)

	tags = findtags(sequence, emiss)

	print("\n\nTest Set Tokens Found in Corpus:\n")

	for i in range(len(sequence)):
		whitespc = 16 - len(sequence[i])
		w = ""
		for j in range(whitespc):
			w += " "
		w += sequence[i] + " : "

		for j in range(len(tags[i])):
			w2 = ""
			for k in range(4 - len(tags[i][j][0])):
				w2 += " "
			w += w2 + tags[i][j][0] + " (%.6f)" % tags[i][j][1]
		print(w)

	path, probs = viterbi(tokens, sequence, tags, transp)

	print("\n\nIntermediate Results of Viterbi Algorithm:\n")

	for i in range(len(sequence)):
		s = "Iteration  " + str(i+1) + " :"
		whitespc = 13 - len(sequence[i])
		for j in range(whitespc):
			s += " "
		s += sequence[i] + " :"
		for j in range(len(tags[i])):
			if(i == 0):
				s += " " + tags[i][j][0] + " (%.6f, null)" % probs[i][j]
			else:
				s += " " + tags[i][j][0] + " (%.6f, " % probs[i][j] + path[i - 1] + ")"
		print(s)

	print("\n\nViterbi Tagger Output:\n")

	for i in range(len(sequence)):
		s = ""
		whitespc = 13 - len(sequence[i])
		for j in range(whitespc):
			s += " "
		s += sequence[i] + "     " + path[i]
		print(s)
	print()


import sys
input = sys.stdin.readline

def do():
  N, M =map(int, input().split())
  num, name = dict(), dict()
  for i in range(1, N + 1):
    t = input().strip()
    num[i] = t
    name[t] = i
  for i in range(M):
    q = input().strip()
    try:
      q = int(q)
      print(num[q])
    except:
      print(name[q])

do()

자료 구조/1620_나는야 포켓몬 마스터 이다솜.py

1620_나는야 포켓몬 마스터 이다솜.py


#mostly unknown
Firestarter = genMonster("Firestarter", (159, 5980), "a firestarter")
Firestarter.setOutfit(95, 95, 95, 95)#wrong colors
Firestarter.setHealth(180)
Firestarter.bloodType("blood")
Firestarter.setDefense(armor=15, fire=1, earth=1, energy=1, ice=1, holy=1, death=1, physical=1, drown=1)
Firestarter.setExperience(80)
Firestarter.setSpeed(250)
Firestarter.setBehavior(summonable=0, hostile=1, illusionable=0, convinceable=0, pushable=1, pushItems=0, pushCreatures=0, targetDistance=1, runOnHealth=0)
Firestarter.walkAround(energy=0, fire=0, poison=0)
Firestarter.setImmunity(paralyze=1, invisible=1, lifedrain=0, drunk=0)
Firestarter.voices("FIRE!","BURN!", "DEATH to the FALSE GOD!!", "You shall burn in the thornfires!!","DOWN with the followers of the bog!!")
Firestarter.regMelee(7)
Firestarter.loot( (2148, 100, 35), ("elvish talisman", 6.0), ("flaming arrow", 100, 12), ("coal", 15.0), ("grapes", 20.5), ("longsword", 6.25), ("bow", 4.25), ("flintstone", 0.5), (5921, 1.0), ("elvish bow", 0.0025) )


import os, shutil

os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
original_dataset_dir = 'D:/WorkSpace/image/train'
base_dir = 'D:/WorkSpace/new_image/kaggle_Dog&Cat'

train_dir = os.path.join(base_dir, 'train')
# os.mkdir(train_dir)
validation_dir = os.path.join(base_dir, 'validation')
# os.mkdir(validation_dir)
test_dir = os.path.join(base_dir, 'test')
# os.mkdir(test_dir)

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')
# os.mkdir(train_cats_dir)

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')
# os.mkdir(train_dogs_dir)

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')
# os.mkdir(validation_cats_dir)

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
# os.mkdir(validation_dogs_dir)

# Directory with our validation cat pictures
test_cats_dir = os.path.join(test_dir, 'cats')
# os.mkdir(test_cats_dir)

# Directory with our validation dog pictures
test_dogs_dir = os.path.join(test_dir, 'dogs')
# os.mkdir(test_dogs_dir)

fnames = ['cat.{}.jpg'.format(i) for i in range(3000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(train_cats_dir, fname)
    shutil.copyfile(src, dst)

fnames = ['cat.{}.jpg'.format(i) for i in range(3000, 4000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(test_cats_dir, fname)
    shutil.copyfile(src, dst)

fnames = ['cat.{}.jpg'.format(i) for i in range(9000, 10000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(validation_cats_dir, fname)
    shutil.copyfile(src, dst)

fnames = ['dog.{}.jpg'.format(i) for i in range(3000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(train_dogs_dir, fname)
    shutil.copyfile(src, dst)

fnames = ['cat.{}.jpg'.format(i) for i in range(3000, 4000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(test_dogs_dir, fname)
    shutil.copyfile(src, dst)

fnames = ['dog.{}.jpg'.format(i) for i in range(9000, 10000)]
for fname in fnames:
    src = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(validation_dogs_dir, fname)
    shutil.copyfile(src, dst)

print('total training cat images:', len(os.listdir(train_cats_dir)))
print('total training dog images:', len(os.listdir(train_dogs_dir)))
print('total validation cat images:', len(os.listdir(validation_cats_dir)))
print('total validation dog images:', len(os.listdir(validation_dogs_dir)))
print('total test cat images:', len(os.listdir(test_cats_dir)))
print('total test dog images:', len(os.listdir(test_dogs_dir)))


#!/usr/bin/python3
"""Specifies a class for rectangles."""
from models.base import Base

class Rectangle(Base):
    """Depict a rectangle."""
    def __init__(self, width, height, x=0, y=0, id=None):
        """Create a new instance of a Rectangle.

        Args:
            width (int): The width of the newly created Rectangle.
            height (int): The height of the newly created Rectangle.
            x (int): The horizontal coordinate (x) of the newly
            created Rectangle.
            y (int): The vertical coordinate (y) of the newly
            created Rectangle.
            id (int): The unique identifier of the newly created Rectangle.
        Raises:
            TypeError: If either of width or height is not an int.
            ValueError: If either of width or height <= 0.
            TypeError: If either of x or y is not an int.
            ValueError: If either of x or y < 0.
        """
        super().__init__(id)
        self.width = width
        self.height = height
        self.x = x
        self.y = y

    @property
    def width(self):
        """Modify or retrieve the width of the Rectangle."""
        return self.__width

    @width.setter
    def width(self, value):
        if type(value) != int:
            raise TypeError("width must be an integer")
        if value <= 0:
            raise ValueError("width must be > 0")
        self.__width = value

    @property
    def height(self):
        """Adjust or obtain the height of the Rectangle."""
        return self.__height

    @height.setter
    def height(self, value):
        if type(value) != int:
            raise TypeError("height must be an integer")
        if value <= 0:
            raise ValueError("height must be > 0")
        self.__height = value

    @property
    def x(self):
        """Adjust or retrieve the x coordinate of the Rectangle."""
        return self.__x

    @x.setter
    def x(self, value):
        if type(value) != int:
            raise TypeError("x must be an integer")
        if value < 0:
            raise ValueError("x must be >= 0")
        self.__x = value

    @property
    def y(self):
        """Adjust or obtain the y coordinate of the Rectangle."""
        return self.__y

    @y.setter
    def y(self, value):
        if type(value) != int:
            raise TypeError("y must be an integer")
        if value < 0:
            raise ValueError("y must be >= 0")
        self.__y = value

    def area(self):
        """Retrieve the area of the Rectangle."""
        return self.width * self.height

    def display(self):
        """Display the Rectangle using the `#` character."""
        if self.width == 0 or self.height == 0:
            print("")
            return
        [print("") for y in range(self.y)]
        for i in range(self.height):
            [print(" ", end="") for x in range(self.x)]
            [print("#", end="") for w in range(self.width)]
            print("")

    def update(self, *args, **kwargs):
        """Update the Rectangle.

        Args:
            *args (ints): Fresh attribute values.
                - The first argument represents the 'id' attribute.
                - The second argument corresponds to the 'width' attribute.
                - The third argument represents the 'height' attribute.
                - The fourth argument corresponds to the 'x' attribute.
                - The fifth argument represents the 'y' attribute.
            **kwargs (dict): Fresh sets of key/value attribute pairs.
        """
        if args and len(args) != 0:
            a = 0
            for arg in args:
                if a == 0:
                    if arg is None:
                        self.__init__(self.width, self.height, self.x, self.y)
                    else:
                        self.id = arg
                elif a == 1:
                    self.width = arg
                elif a == 2:
                    self.height = arg
                elif a == 3:
                    self.x = arg
                elif a == 4:
                    self.y = arg
                a += 1

        elif kwargs and len(kwargs) != 0:
            for k, v in kwargs.items():
                if k == "id":
                    if v is None:
                        self.__init__(self.width, self.height, self.x, self.y)
                    else:
                        self.id = v
                elif k == "width":
                    self.width = v
                elif k == "height":
                    self.height = v
                elif k == "x":
                    self.x = v
                elif k == "y":
                    self.y = v

    def to_dictionary(self):
        """Provide a dictionary representation of a Rectangle."""
        return {
            "id": self.id,
            "width": self.width,
            "height": self.height,
            "x": self.x,
            "y": self.y
        }

    def __str__(self):
        """Retrieve the print() and str() representations of the Rectangle."""
        return "[Rectangle] ({}) {}/{} - {}/{}".format(self.id,
                                                       self.x, self.y,
                                                       self.width, self.height)


class TicketDB:
    def __init__(self, dbConnection):
        self.dbConnection = dbConnection

    async def add(self, guild_id, category, message_id, roles):
        db = await self.dbConnection.get_connection()
        await db.execute("INSERT INTO ticketpanels(guild_id, category, message_id, roles) VALUES ($1, $2, $3, $4)", guild_id, category, message_id, roles)
        await db.close()

    async def get(self, message_id):
        db = await self.dbConnection.get_connection()
        try:
            x = await db.fetch("SELECT category, roles FROM ticketpanels WHERE message_id=$1", message_id)
            await db.close()
        except:
            return []
        return x

    async def remove(self, message_id):
        db = await self.dbConnection.get_connection()
        await db.execute("DELETE FROM ticketpanels WHERE message_id=$1", message_id)
        await db.close()

    async def edit(self, attr, new_value):
        db = await self.dbConnection.get_connection()
        if attr.lower() == "category":
            attr = "category"
        elif attr.lower() == "messageid" or "message_id" or "message id":
            attr = "message_id"
        elif attr.lower() == "roles":
            attr = "roles"
        try:
            await db.execute(f"UPDATE ticketpanels SET {attr} = {new_value}")
            await db.close()
        except:
            return

    async def add_ticket(self, guild_id, channel_id, member):
        db = await self.dbConnection.get_connection()
        await db.execute("INSERT INTO tickets(guild_id, channel_id, member) VALUES ($1, $2, $3)", guild_id, channel_id, member)
        await db.close()

    async def add_log_channel(self, chan_id, guild_id):
        db = await self.dbConnection.get_connection()
        try:
            x = await db.fetch("SELECT * FROM log_config WHERE guild=$1", guild_id)
            if x == []:
                await db.execute("INSERT INTO log_config(guild, ticket_logs) VALUES ($1, $2)", guild_id, chan_id)
                await db.close()
                return
            await db.execute("UPDATE log_config SET ticket_logs = $1 WHERE guild = $2", chan_id, guild_id)
            await db.close()
        except Exception as e:
            print(e)

    async def get_log_channel(self, guild_id):
        db = await self.dbConnection.get_connection()
        try:
            x = await db.fetch("SELECT ticket_logs FROM log_config WHERE guild=$1", guild_id)
            await db.close()
        except:
            return None
        return x

    async def get_member(self, chan_id):
        db = await self.dbConnection.get_connection()
        try:
            x = await db.fetch("SELECT member FROM tickets WHERE channel_id=$1", chan_id)
            await db.close()
        except:
            return None
        return x

    async def delete_ticket(self, chan_id):
        db = await self.dbConnection.get_connection()
        try:
            await db.execute("DELETE FROM tickets WHERE channel_id=$1", chan_id)
        except:
            return


import sys
import pickle
import numpy
import numpy as np
from scipy.linalg import lstsq
from PIL import ImageGrab
import cv2 as cv
import asyncio
from typing import Tuple, List

from pad import Pad

class Agent:
    def __init__(self, bbox: Tuple, width: int, height: int) -> None:
        self.bbox = bbox
        self.width = width
        self.height = height
        self.reset()  # set initial values

    def reset(self) -> None:
        self.frame_sequence = []  # list for screenshots
        self.circle_sequence = []  # list for detected circles
        self.simulate_history = []  # list of simulated moving coordinates
        self.ball_radius = None
        self.x, self.y = None, None  # ball coordinates (will be simulated)
        self.vx, self.vy = None, None  # ball speed projections
        self.acc = None  # acceleration
        self.wait = True  # waiting for the ball to hit the area
        self.stop = False  # left the ball area or not

    def process_screen(self) -> None:
        if self.wait or not self.stop:
            frame = np.array(ImageGrab.grab(bbox=self.bbox))  # making a screenshot of predetermined area
            gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)  # convert to grayscale
            blur_frame = cv.GaussianBlur(gray_frame, (17, 17), 0)  # bluring image
            ret, thresh_frame = cv.threshold(blur_frame, 127, 255, cv.THRESH_BINARY)  # binary threshold
            circles = self.detect_circles(thresh_frame)  # detecting ball on image

            if circles is not None:
                self.circle_sequence.append(circles[0][0])  # write into circle list
                self.frame_sequence.append(frame)

                if self.wait:
                    self.wait = False  # ball already hit the area

            elif not self.wait:  # ball is not detected, but already hit the area
                self.stop = True  # ball left the area
                self.circle_sequence = np.array(self.circle_sequence, dtype='float')
                self.ball_radius = self.circle_sequence[:, -1].mean()  # set radius as mean of recognized values

    # gets value of parabola function
    def get_value(self, cf: Tuple[float], x: float) -> float:
        value = cf[0] * x**2 + cf[1] * x + cf[2]
        return value

    # gets coefficients of parabola equation using the least squares method
    def get_equation_cf(self, circles: numpy.ndarray):
        # center coords of detected circles
        x = circles[:, 0]
        y = circles[:, 1]

        # create a wireframe array to run the least squares method
        m = np.vstack((x**2, x, np.ones(x.shape[0]))).T
        cf = lstsq(m, y)[0]
        return cf

    def simulate(self) -> None:
        start, end = self.find_max_segment()  # find the longest interval
        circles = self.circle_sequence[start:end]  # limit the list of circles by interval
        cf = self.get_equation_cf(circles)  # get coefs of parabola equation

        self.acc = cf[0] * 2  # acceleration as second derivative of equation
        self.x = circles[-1, 0]  # set start x as last circle x
        self.y = self.get_value(cf, self.x)  # count start y
        self.vx = 1  # const x speed
        self.vy = self.get_value(cf, self.x + self.vx) - self.y  # count y speed

        self.move()  # simulate the moving of the ball

    def detect_circles(self, frame: numpy.ndarray) -> numpy.ndarray:
        circles = cv.HoughCircles(frame, cv.HOUGH_GRADIENT, 1, 100, param1=6, param2=6, minRadius=5,
                                  maxRadius=20)  # OpenCV circle detection function
        if circles is not None:
            circles = np.uint16(np.around(circles))  # round and convert to numpy array
        return circles

    # find the longest interval between bounces (so that it can be described by a single parabola)
    def find_max_segment(self) -> List[int]:
        # catch bounces
        bounces = list(np.where((self.circle_sequence[:, 1] < self.ball_radius * 2) |
                                (self.circle_sequence[:, 1] > self.height - self.ball_radius * 2))[0])
        bounces = [0] + bounces + [len(self.circle_sequence)]  # add first and last index
        distance = np.diff(bounces)  # count lengths of intervals split by bounces
        max_distance_idx = np.where(distance == max(distance))[0][0]  # get longest interval
        max_segment = bounces[max_distance_idx: max_distance_idx + 2]  # get boundaries of interval
        return max_segment

    def game_over(self) -> bool:
        if self.x >= 2 * self.width / 3 - self.ball_radius:  # out of bounds, counting from the tracked frame
            return True
        return False

    # same method as in Ball class, but for simulation
    def check_border_collision(self) -> None:
        if self.y <= self.ball_radius:
            self.vy = abs(self.vy)
        elif self.y >= (self.height - self.ball_radius):
            self.vy = -abs(self.vy)

    # quanted step of full move
    def move_step(self) -> None:
        self.check_border_collision()

        self.vy += self.acc  # increase y speed by acceleration
        x = self.x + self.vx  # increase x coord by x speed
        y = self.y + self.vy  # increase x coord by y speed

        self.x, self.y = x, y  # update self coordinates
        self.simulate_history.append([self.x, self.y])  # store to list

    # move simulation
    def move(self) -> None:
        while not self.game_over():
            self.move_step()

    # async pad control by agent
    async def control_pad(self, pad: Pad, interval: int = 0.05, steps: int = 10) -> None:
        distance_per_step = (self.y - pad.y) / steps
        for i in range(steps):
            pad.move(distance_per_step)
            await asyncio.sleep(interval)

    # release a video of tracked frame
    def make_video(self) -> None:
        size = (int(self.bbox[2]-self.bbox[0]), int(self.bbox[3]-self.bbox[1]))  # we need to specify the size
        out = cv.VideoWriter('logs/cv_record.avi', cv.VideoWriter_fourcc(*'DIVX'), 30, size)  # 30 fps, may be more

        for i in range(len(self.frame_sequence)):
            out.write(self.frame_sequence[i])
        out.release()

    # save action logs
    def save_history(self) -> None:
        with open('logs/circles.pkl', 'wb') as f1:
            pickle.dump(self.circle_sequence, f1)
        with open('logs/simulate_history.pkl', 'wb') as f2:
            pickle.dump(self.simulate_history, f2)


import tensorflow as tf
import numpy as np
#tf.enable_eager_execution()

# CREATE A GRID FOR EACH SCALE
name = 'Create_GRID'
with tf.name_scope(name) as scope:

    grid_shape = tf.constant([13, 13])  # height, width ---> grid 13x13 for scale1
    #         (0,0) (1,0) ...   (12,0)                       grid 26x26 for scale2
    #         (0,1) (1,1) ...   (12,1)                       grid 52x52 for scale3
    #          ...              (12,12)
    # In YOLO the height index is the inner most iteration.
    grid_y = tf.range(0, grid_shape[0])  # array([0,1,...,11,12])
    grid_x = tf.range(0, grid_shape[1])
    grid_y = tf.reshape(grid_y, [-1, 1, 1, 1])  # shape=([13,  1,  1,  1])
    grid_x = tf.reshape(grid_x, [1, -1, 1, 1])  # [1, 13, 1, 1]
    grid_y = tf.tile(grid_y, [1, grid_shape[1], 1, 1])  # [13, 1, 1, 1] ---> [13, 13, 1, 1]
    grid_x = tf.tile(grid_x, [grid_shape[0], 1, 1, 1])  # [1, 13, 1, 1] ---> [13, 13, 1, 1]
    grid = tf.concat([grid_x, grid_y], axis=-1)  # shape=[13, 13,  1,  2]
    grid = tf.cast(grid, dtype=tf.float32)  # change type
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
    print(sess.run(grid))
    print("hihihihihi")
    print(sess.run((grid[::-1])))

writer.close()


from configparser import ConfigParser
# # parser = ConfigParser()
# # print(dir(parser))

def dbcreds(filename = "config.ini" , section = "mysql"):
	db = {}
	parser = ConfigParser()
	data = parser.read(filename)
	# print(data)
	if parser.has_section(section):
		items = parser.items(section)
		# print(items)
		for item in items:
			db[item[0]] = item[1]
	return db


import random
from pagermaid.listener import listener
from pagermaid.enums import Message
from pagermaid.utils import pip_install

pip_install("jieba")

import jieba
import jieba.posseg as pseg

jieba.setLogLevel(20)

def chaos(x, y, chaosrate):
    if random.random() > chaosrate:
        return x
    if x in {"[", "]"}:
        return ""
    if x in {"，"}:
        return "…"
    if x in {
        "!",
        "！",
    }:
        return "‼‼‼"
    if x in {"。"}:
        return "❗"
    if len(x) > 1 and random.random() < 0.1:
        return f"{x[0]}…{x}"
    if len(x) > 1 and random.random() < 0.4:
        return f"{x[0]}♥{x}"
    if y == "n" and random.random() < 0.1:
        x = "⭕" * len(x)
        return f"…{x}"
    if x in {"\……n", "\♥n"}:
        return "\n"
    if x in {"…………"}:
        return "……"
    if y == "n" and random.random() < 0.2:
        x = "⭕" * len(x)
    return f"……{x}"

def chs2yin(s, chaosrate=0.8):
    return "".join(chaos(x, y, chaosrate) for x, y in pseg.cut(s))

@listener(command="yinglish", description="能把中文和英文翻译成淫语的翻译机！")
async def yinglish(context: Message):
    if not context.arguments:
        await context.edit("你没说话我转换个啥")
    else:
        outputtext = chs2yin(context.arguments)
        await context.edit(f"{outputtext}")


"""
Strava data methods.

First argument for all methods may be either a model object or strava id.

Fetch methods will request data from strava, create the corresponding model instances or update them.
Get methods fetch from strava only if the data is not found in the local database.

"""
from threading import Thread
import urllib2
import itertools
import simplejson
from biwinning.config import db
from biwinning.models import Ride, Club, Athlete, ClubAthlete

def load_json(url):
    f = urllib2.urlopen(url)
    return simplejson.loads(f.read())

get_strava_id = lambda id_or_instance: isinstance(id_or_instance, db.Model) and id_or_instance.strava_id or id_or_instance

def strava_id(f):
    """
    Decorator which converts first argument to strava_id if model instance is provided.
    """
    def g(*args, **kwargs):
        return f(*((get_strava_id(args[0]),)+args[1:]), **kwargs)
    return g

@strava_id
def fetch_ride(ride):
    """
    Fetch and store a single ride from strava. Argument is either Ride instance or strava id.
    """
    data = load_json("http://www.strava.com/api/v1/rides/%s" % ride)['ride']
    instance = Ride.get_or_create(strava_id=ride).populate_from_dict(data)
    instance.save()
    return instance

@strava_id
def get_ride(ride):
    """
    Get ride from database or fetch from strava.
    """
    try:
        return Ride.get(strava_id=ride)
    except Ride.DoesNotExist:
        return fetch_ride(ride)

@strava_id
def fetch_athletes(club, data=None):
    """
    Fetch and store club members from strava.
    """
    data = data or load_json('http://app.strava.com/api/v1/clubs/%s/members' % club)

    def handle_member(m):
        name,athlete_id = (m['name'].encode('utf-8', 'ignore'), m['id'])
        athlete = Athlete.get_or_create(strava_id=athlete_id)

        if not athlete.matches_dict({'name': name}):
            athlete.values_from_dict({'name': name})
            athlete.save()

        club_athlete = ClubAthlete.get_or_create(club=get_club(club), athlete=athlete)
        club_athlete.save()

        return athlete

    return (handle_member(m) for m in data['members'])

@strava_id
def fetch_club(club):
    """
    Fetch and store club information and members from strava.
    """
    data = load_json('http://app.strava.com/api/v1/clubs/%s/members' % club)
    instance = Club.get_or_create(strava_id=club).values_from_dict(data['club'], {'id': 'strava_id'})
    instance.save()

    fetch_athletes(club, data=data)

    return instance

@strava_id
def get_club(club):
    """
    Get club information from database or strava.
    """
    try:
        return Club.get(strava_id=club)
    except Club.DoesNotExist:
        return fetch_club(club)

@strava_id
def get_athletes(club):
    """
    Get club members from database or strava.
    """
    instance = get_club(club)
    return instance.athletes

@strava_id
def fetch_athlete_ride_ids(athlete, offset=0, startId=0):
    """
    Fetch list of rides from strava. Optionally offset or startId may be provided.
    This method fetches 50 records at a time and returns a generator.
    """
    rides = load_json("http://app.strava.com/api/v1/rides?athleteId=%s&offset=%s&startId=%s" % \
                      (athlete, offset, startId))['rides']
    for ride_id in (r['id'] for r in rides):
        if not ride_id == startId:
            yield ride_id
    if len(rides) >= 50:
        for ride_id in fetch_athlete_ride_ids(athlete, offset + 50, startId=startId):
            if ride_id is not startId:
                yield ride_id

@strava_id
def fetch_athlete(athlete):
    """
    Fetch and store athlete information from strava. (API workaround).
    """
    return get_ride(fetch_athlete_ride_ids(athlete)[0]).athlete

@strava_id
def get_athlete(athlete):
    """
    Get athlete from database or strava.
    """
    try:
        return Athlete.get(strava_id=athlete)
    except Athlete.DoesNotExist:
        return fetch_athlete(athlete)

@strava_id
def fetch_new_rides(athlete):
    """
    Fetch and store athlete's rides from strava.
    Returns a generator.
    """
    instance = get_athlete(athlete)
    startId = instance.max_ride_id
    return (get_ride(id) for id in fetch_athlete_ride_ids(athlete, 0, startId))

@strava_id
def get_rides(athlete):
    """
    Get athlete's rides from database or strava.
    """
    return get_athlete(athlete).rides

@strava_id
def fetch_club_new_rides(club):
    for athlete in get_athletes(club):
        for ride in fetch_new_rides(athlete):
            yield ride

@strava_id
def fetch_club_new_rides_fair(club):
    """
    One ride per athlete in each iteration
    """
    generators = [fetch_new_rides(athlete) for athlete in fetch_athletes(club)]
    while len(generators) > 0:
        for gen in generators:
            try:
                yield next(gen)
            except StopIteration:
                generators.remove(gen)

@strava_id
def get_club_rides(club):
    for athlete in get_athletes(club):
        for ride in get_rides(athlete):
            yield ride

@strava_id
def get_orphan_rides(club):
    for athlete in get_athletes(club):
        db_ids = set(r.strava_id for r in Ride.select(Ride.strava_id).where(Ride.athlete == athlete))
        strava_ids = set(fetch_athlete_ride_ids(athlete))
        for id in db_ids.difference(strava_ids):
            yield Ride.get(Ride.strava_id == id)

@strava_id
def get_club_rides_for_week(club, week_id):
    club = get_club(club)
    return Ride.select().join(Athlete).join(ClubAthlete).where(Ride.week==week_id, ClubAthlete.club==club)

@strava_id
def fetch_club_new_ride_ids(club):
    return itertools.chain(
        *(fetch_athlete_ride_ids(athlete, startId=athlete.max_ride_id) for athlete in get_athletes(club))
    )

def fetch_new_club_rides_fast(club, num_threads=20):
    def ride_ids(athlete):
        ride_ids = []
        strava_id, max_ride_id = athlete.strava_id, athlete.max_ride_id
        f = lambda l: l.extend(list(fetch_athlete_ride_ids(strava_id, startId=max_ride_id)))
        thread = Thread(target=f, args=(ride_ids,))
        return thread, ride_ids

    def ride_data(ids):
        data = []
        f = lambda l: l.extend([load_json("http://www.strava.com/api/v1/rides/%s" % ride_id)['ride'] for ride_id in ids])
        thread = Thread(target=f, args=(data,))
        return thread, data

    # Step 1: For each athlete, find out which rides are new on remote
    id_threads = [ride_ids(athlete) for athlete in get_athletes(club)]
    [t.start() for (t,r) in id_threads]

    # Step 2: Construct evenly distributed lists of ids
    ride_ids = itertools.chain(*[r for (x,r) in [(t.join(), r) for (t,r) in id_threads] if r])
    ids = [r for r in ride_ids]
    batch_size = len(ids) / num_threads + (1 if len(ids) % num_threads else 1)
    chuncks = [ids[i:i+batch_size] for i in range(0, len(ids), batch_size)]

    # Step 3: Fetch data for new rides from remote and construct Ride objects
    ride_data_threads = [ride_data(ids) for ids in chuncks]

    [t.start() for (t,r) in ride_data_threads]

    def construct_ride(data):
        instance = Ride.get_or_create(strava_id=data['id']).populate_from_dict(data)
        instance.save()
        return instance

    return itertools.chain(*[map(construct_ride, r) for (x,r) in [(t.join(), r) for (t,r) in ride_data_threads] if r])


# coding: UTF-8
import torch
import torch.nn as nn
import numpy as np

class Config(object):
    """配置参数"""

    def __init__(self, embedding):
        self.model_name = 'TextRNN'
        self.file_path = './data/news.csv'  # 总集
        self.train_path = './data/train.txt'  # 训练集
        self.dev_path = './data/dev.txt'  # 验证集
        self.test_path = './data/test.txt'  # 测试集

        self.vocab_path = './data/vocab.txt'  # 词表
        self.save_path = './saved_dict/' + self.model_name + '.pkl'  # 模型训练结果

        self.embedding_pretrained = torch.tensor(np.load('./utils/' + embedding)["embeddings"].astype(
            'float32')) if embedding != 'random' else None  # 预训练词向量
        # self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  # 设备
        self.device = torch.device('cpu')  # 设备

        self.seed = 721
        self.dropout = 0.5  # 随机失活
        self.early_stop = 10  # 早停机制
        self.num_classes = 5  # 类别数
        self.vocab_size = 652  # 词表大小，在运行时赋值
        self.num_epochs = 100  # epoch数
        self.batch_size = 32  # batch大小
        self.max_len = 40  # 每句话处理成的长度(短填长切)
        self.learning_rate = 1e-3  # 学习率
        self.out_dim = 64  # 输出维度
        self.embed_size = self.embedding_pretrained.size(1) if self.embedding_pretrained is not None else 64  # 词向量嵌入维度

class Model(nn.Module):
    def __init__(self, config):
        super(Model, self).__init__()
        if config.embedding_pretrained is not None:
            self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False)
        else:
            self.embedding = nn.Embedding(config.vocab_size, config.embed_size, padding_idx=1)

        self.lstm = nn.LSTM(config.embed_size, config.out_dim, batch_first=True, dropout=config.dropout,
                            bidirectional=True, num_layers=2)
        self.linear = nn.Sequential(
            nn.Linear(2 * config.out_dim, config.num_classes),
            # nn.Dropout(config.dropout),
            # nn.ReLU(),
            # nn.Linear(32, config.num_classes)
        )

        self.dropouts = nn.ModuleList([nn.Dropout(config.dropout) for _ in range(3)])

    def forward(self, x):
        # 初始输入格式为(length, batch_size)
        out = self.embedding(x)
        # (length, batch_size, emb) -> (batch_size, length , emb )

        # out = torch.transpose(out, 0, 1)

        out, (h, c) = self.lstm(out)
        out = torch.cat((h[-2, :, :], h[-1, :, :]), dim=1)

        # multi-sample dropout 不一定有效
        # for i, dropout in enumerate(self.dropouts):
        #     if i == 0:
        #         res = dropout(out)
        #         res = self.linear(res)
        #     else:
        #         temp_out = dropout(out)
        #         res = res + self.linear(temp_out)
        # return res

        out = self.linear(out)
        return out


import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader

import networkx as nx
import numpy as np

def load_data(file_name='data/mutag/MUTAG.txt', n_labels=7, n_dim=None):
    data = []
    with open(file_name) as f:
        n_graphs = int(f.readline())
        for _ in range(n_graphs):
            n, y = map(int, f.readline().split())
            G = nx.Graph()
            G.add_nodes_from(range(n))
            node_labels = []
            for i in range(n):
                li = list(map(int, f.readline().split()))
                node_labels.append(int(li[0]))
                for j in li[2:]:
                    G.add_edge(i, j)
            if n_dim is not None:
                X = torch.zeros((len(node_labels), n_dim))
                X[:, :n_labels] = torch.eye(n_labels)[node_labels]
            else:
                X = torch.eye(n_labels)[node_labels]
            adj = nx.adjacency_matrix(G).tocoo()
            E = torch.LongTensor(np.vstack((adj.row, adj.col)))
            E_attr = torch.FloatTensor(torch.cat((X[E[0]], X[E[1]]), dim=1)).t()
            data.append((X, n, E, E_attr, int(y != 0)))

    return data

def adj_to_torch_sparse(G):
    coo_A = nx.adjacency_matrix(G).tocoo()
    i = torch.LongTensor()

class MPNN_Dataset(Dataset):

    def __init__(self, data):
        """

        :param data: iterables of tuples of form (X, n_node, E, E_attr, label)

        X: node feature matrix, FloatTensor: n_node x n_dim
        n_node: number of nodes, int
        E: edges, LongTensor, 2 x n_edge  (sparse representation of adjcency matrix)
        E_attr: edge feature matrix, FloatTensor, e_dim x n_edge
        label: 0 or 1
        """
        super(MPNN_Dataset, self).__init__()

        self.data = data

    def __getitem__(self, item):
        return self.data[item]

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

def mpnn_collate_fn(batch):
    X = torch.cat([t[0] for t in batch], dim=0)  # concat feature matrix of all graphs
    node_nums = [t[1] for t in batch]  # list of num_node of each graph in the batch

    # edges of all graphs, node indices being offset
    E = torch.cat([t[2] + sum(node_nums[:i]) for i, t in enumerate(batch)], dim=1)

    E_attr = torch.cat([t[3] for t in batch], dim=1)  # concate edge feature matrix of all graphs
    labels = torch.FloatTensor([t[-1] for t in batch])  # labels of graphs in the batch

    # denote which node belongs to which graph (after being offset)
    graph_index = torch.LongTensor([item for i in range(len(node_nums)) for item in [i]*node_nums[i]])

    return X, graph_index, E, E_attr, labels

class MPNN_DataLoader(DataLoader):

    def __init__(self, dataset, **kwargs):
        super(MPNN_DataLoader, self).__init__(dataset=dataset, collate_fn=mpnn_collate_fn, **kwargs)

# check collate_fn
class ToyDataset(Dataset):

    def __init__(self, n=5):
        super(ToyDataset, self).__init__()

        self.n = n

    def __getitem__(self, item):
        s1 = torch.randint(1, self.n, size=(1,)).item()
        s2 = torch.randint(1, self.n, size=(1,)).item()
        return s1, s2, torch.randint(0, self.n, size=(s1, s2))

    def __len__(self):
        return 100

def toy_collate_fn(batch):
    l = torch.cat([t[-1].view(-1) for t in batch], dim=0)
    s1 = torch.LongTensor([t[0] for t in batch])
    s2 = torch.FloatTensor([t[1] for t in batch])
    return s1, s2, l

class Toy_DataLoader(DataLoader):
    def __init__(self, **kwargs):
        super(Toy_DataLoader, self).__init__(collate_fn=toy_collate_fn, **kwargs)

if __name__ == '__main__':
    toy_dataset = ToyDataset()
    toy_dataloader = Toy_DataLoader(dataset=toy_dataset, batch_size=5)
    for s1, s2, l in toy_dataloader:
        print('*************len l: ', len(l), int((s1*s2).sum().item()))
        print(l)

        print('len s1: ', len(s1))
        print(s1)
        print('len s2: ', len(s2))
        print(s2)

    mutag_data = load_mutag()
    mutag_dataset = MPNN_Dataset(mutag_data)
    mutag_dataloader = MPNN_DataLoader(mutag_dataset, batch_size=3)

    X, graph_index, E, E_attr, label = next(iter(mutag_dataloader))
    print('X: ', X.shape)
    print('graph_index: ', graph_index.shape, graph_index)
    print('E: ', E.shape)
    print('E: ', E[:, :54])
    print('E: ', E[:, 54:110])
    print('E: ', E[:, 110:])
    print('E_attr: ', E_attr.shape)
    #print('E_attr: ', E_attr[:, 0], '\n', E_attr[:, 2])
    print('label: ', label)


from random import randint
import bank
import smtplib

def SendEmail(to,content):
    # for sending a Email

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('rroot310@gmail.com', 'RRooT@#$310')
    server.sendmail('rroot310@gmail.com',to, content)
    server.close()
# mainf is the main function that runs after validate Email Address

def mainf():
    otp=randint(0000,9000)
    print(otp)
    count=0

    to=input("Enter the email address to continue:")
    content=f"Your otp for the apni bank is {otp}"
    SendEmail(to,content)

    user_otp=int(input("Enter the otp"))
    while count<4:

        if user_otp==otp:
            print("login successfully!!")
            content="you have successfully logged in to the apni bank"
            SendEmail(to,content)

            bank.bank()
            break
        else:
            print("login unsuccessful")
            count+=1
            otp=randint(0000,9000)
            content=f"your OTP for the apni bank is {otp}"

            print(content)
            SendEmail(to,content)

            print(f"you have {count-3} tries left to login")

            user_otp=int(input("Enter the OTP again:"))
            if count==3:
                print("your action has been blocked")
                break

print("welcome  to the apni Bank\nplease enter the otp to process further")
mainf()


# Iterating list items using a for loop
print("Iterating list items using a for loop")
numbers = [1, 2, 3, 4]

for number in numbers:
    print(number)

colors = ["purple", "teal", "magenta"]
for color in colors:
    print(color)

# Iterating list items using a while loop
print("\nIterating list items using a while loop")
number_list = [1, 2, 3, 4]
list_index = 0

while list_index < len(number_list):
    print(number_list[list_index])
    list_index += 1

color_list = ["purple", "teal", "magenta", "crimson", "emerald"]
list_index = 0

while list_index < len(color_list):
    print(f"{list_index}: {color_list[list_index]}")
    list_index += 1


from pytube import YouTube
link = input("Enter the video url here -> ")
y_tube = YouTube(link)
print(f'Video title -> {y_tube.title}')
stream = y_tube.streams.filter(progressive=True)
video = list(enumerate(stream))
for i in video:
    print(i)
print("-----------------------------------------------------")
index = int(input("Enter the index of the desired stream -> "))
stream[index].download()
print("sucess")


import threading
import time

num = 10

def aa():
    while(True):
        print(num)

av = threading.Thread(target=aa)
av.setDaemon(True)
av.start()

time.sleep(1)

num=20

time.sleep(1)


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

regions=('RMS_region1.reg','RMS_region2.reg','RMS_region3.reg',
'RMS_region4.reg','RMS_region5.reg','RMS_region6.reg','RMS_region7.reg')
radicalname='GX_339-4.RMS.'

def rms_1im(image,regions):
    rms=np.empty((len(regions),))
    for k in range(len(regions)):
        myoutput=imstat(imagename=image,region=regions[k])
        rms[k]=myoutput['rms']
    return rms

#première image à 1 min et dernière à 10 min :

RMS=np.empty((10,len(regions)))

for i in range(10):
    imname=radicalname+str(i+1)+'min.image'
    RMS[i]=rms_1im(imname,regions)

moyRMS=np.mean(RMS,axis=1)
stdRMS=np.std(RMS,axis=1)

result=np.transpose(np.array([moyRMS,stdRMS]))
print(result)
np.savetxt('RMS_computation_log.dat',result)


from gpiozero import PWMLED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep
import RPi.GPIO as GPIO
from classes.air import Air
from classes.motor import Motor
from classes.torch import Torch

                #OBJECT INITIALIZATION
pump = Air()
car = Motor("car")
chk = Motor("chk")
mirage = Torch()

                #BUBBLE CONDENSE

for x in range(3):
    mirage.on(.57, .825, 0, .6)
    sleep(.15)
    bubble_counter = 0
    range_max = 13
    for x in range(range_max):
        car.left(.05, 1)
        sleep(2)
        bubble_counter += 1
        if bubble_counter >= 6:
            chk.left(.025, 1)
        if bubble_counter == range_max:
            car.right(.1, 1)
            sleep(.2)
    chk.left(.025, 1)
    mirage.off()
    sleep(1.75)
                                    #BLOW
    pump.blow(.2)
    sleep(2)
    pump.blow(.075)
    chk.left(.125, 1)
                            #COOL OFF
    mirage.on(0, 1, 0, 0)
    sleep(8)
    mirage.off()

                                #MOVE TO NEXT OPERATION 1
    car.left(1.1, 1)

                                #MARIA
                        #VARIABLES
    maria_counter = 0
    range_max = 12
                                #MARIA CONDENSE
    mirage.on(.54, .86, 0, .6)
    sleep(.075)
    for x in range(range_max):
        print("step 1")
        car.left(.0675, 1)
        sleep(2)
        maria_counter += 1
        print("step 2")
        if maria_counter >= 5:
            chk.left(.14, 1)
            print("step 3")
        if maria_counter >=9:
            mirage.on(.53, .87, 0, .6)

    mirage.off()
    sleep(.2)
                                #PUSH
    push_count = 3
    for x in range(push_count):
        sleep(1)
        chk.left(.05, 1)
                                #MOVE TO NEXT OPERATION
    print("step 6")
    sleep(1.75)
    car.right(1.25, 1)
    sleep(10)
                                #TIP
    mirage.on(.53, .87, 0, .6)
    sleep(12)
    mirage.off()
    chk.right(.115, 1)

                                #MOVE TO NEXT OPERATION
    print("step 6")
    sleep(1.75)
    car.left(1.9, 1)

GPIO.cleanup()