import numpy as np
import pandas as pd
import argparse

def get_tail(hmm, ncRNA, strict=False):
    expression = [int(x) for x in hmm.loc[ncRNA].score.split(',')]
    if (np.mean(expression) > 1.5) and (len(expression) > 1000):
        trim_length = trim_expression(expression, strict)
        return trim_length
    else:
        return False

def trim_expression(expression_list, strict):
    trim_length = 0
    while True:
        if strict:
            if np.mean(np.array(expression_list[-100:])) < 1.1:
                expression_list = expression_list[:-100]
                trim_length += 5000
            else:
                break
        else:

            if all(np.array(expression_list[-100:]) == 1):
                expression_list = expression_list[:-100]
                trim_length += 5000
            else:
                break
    return trim_length

def get_largest_string_non_zero(x):
    cum_sum = []
    suma= 0
    for i in x:
        if (i > 1.1):
            suma+= 1
        else:
            suma = 0
        cum_sum.append(suma)

    max_cum_sum = np.max(cum_sum)
    largest_string_non_zero = max_cum_sum/len(x)
    return largest_string_non_zero

def is_fractured(transcripto):
    x = [int(x) for x in transcripto.split(',')]
    if np.max(x) >= 3.9:
        return False
    largest_string_non_zero = get_largest_string_non_zero(x)
    mean_non_zero = np.mean(np.array(x)>1)

    if (mean_non_zero > 0.4) and (largest_string_non_zero < 0.2):
        return True
    else:
        return False

def trim_and_split(hmm, ncRNA, split_len = 50, min_len=200, min_portion = 0.2, strict=True,
                  strand = 'plus'):

    current_pos = 0
    start_list = []
    end_list = []

    transcript_length = hmm.loc[ncRNA].end - hmm.loc[ncRNA].start

    transcripto = hmm.loc[ncRNA].score

#     if strand == 'minus':
#         transcripto = transcripto[::-1]

    if strict:
        transcripto_split = transcripto.split(',')
        counts = [int(x) for x in transcripto_split]

        largest_non_zero = get_largest_string_non_zero(counts)
        percent_zero = np.mean(np.array(counts) == 1)

#         if is_fractured(counts):#np.mean(counts) > 1.5:
#             split_len = 1e100

        #if (np.max(counts)==4):
        if (np.max(counts)==4) or (np.quantile(counts, 0.5)>=2.9):
            if np.quantile(counts, 0.75) <= 3.1:
                transcripto = transcripto.replace('2,'*100, '3,'*100)
                transcripto_split = transcripto.split(',')
            transcripto_split = ['1' if x == '2' else x for x in transcripto_split]
            if np.quantile(counts, 0.75) >= 3.9:
                print('enormous!')
                transcripto_split = ['1' if x == '3' else x for x in transcripto_split]
        elif (largest_non_zero > 0.25) and (percent_zero > 0.5):
            split_len = 5

        transcripto = ','.join(transcripto_split)

    if is_fractured(transcripto):#np.mean(counts) > 1.5:
        print('fractured!')
        end = len(transcripto.split(','))*50
        return [0], [end]
    split_transcripts = transcripto.split(','.join(['1']*split_len))

    longest = max([len(x) for x in split_transcripts])

    high_scores = []
    for transcript in split_transcripts:
        try:
            sc = np.mean([int(x) for x in transcript.strip(',').split(',')])
            high_scores.append(sc)
        except:
            continue
    highest = np.max(high_scores)
    inherited = False
    print(counts)
    print(split_transcripts)
    len_to_add = 0
    for transcript in split_transcripts:
        current_longest = ((len(transcript) == longest) and (len(transcript) > 3))

        if transcript == ',':
            current_pos += (50*split_len)
        elif transcript == '':
            current_pos += (50*split_len)
        else:

            transcript_counts = transcript.strip(',').split(',')
            if (transcript[0] == ',') and (strand == 'plus'):
                current_pos += 50
#             if (strand == 'minus') and inherited:
#                 current_pos += (50*split_len)

            transcript_counts = [int(x) for x in transcript_counts]

            print(transcript_counts)

            current_highest = np.abs(np.mean(transcript_counts) - highest) <= 1e-100

            is_max_peak = transcript_counts.count(4) >= 5

            transcript_portion = (len(transcript_counts)*50)/transcript_length
            if ((len(transcript_counts) > min_len) and (transcript_portion >= min_portion)) or is_max_peak or current_highest or current_longest:

                start_pos = current_pos

                for i in range(len(transcript_counts)):

                    if transcript_counts[i] == 1:
                        print('trimmin')
                        start_pos += 50

                    else:
                        break

                if np.mean(transcript_counts) > 1.1:
                    start_list.append(start_pos)

                trim = trim_expression(transcript_counts, True)

                if trim:
                    if np.mean(transcript_counts) > 1.1:
                        end_list.append(current_pos + ((len(transcript_counts)*50)-trim))

                else:
                    if np.mean(transcript_counts) > 1.1:
                        trimmer = 50
#                         if strand == 'plus':
#                             trimmer = 50
#                         elif strand == 'minus':
#                             trimmer = 0
#                         else:
#                             raise Exception('strand error')
                        end_list.append(current_pos + (len(transcript_counts)*50) - trimmer)

            print('adding')
            if transcript[-1] == ',':
                current_pos += (50*split_len)
            current_pos += (len(transcript_counts)*50)

            if transcript[-1] == ',':
                inherited = True

#     if strand == 'minus':
#         return end_list, start_list

    return start_list, end_list

# def trim_and_split(hmm, ncRNA, split_len = 50, min_len=200, min_portion = 0.2, strict=True,
#                   strand = 'plus'):

#     current_pos = 0
#     start_list = []
#     end_list = []

#     transcript_length = hmm.loc[ncRNA].end - hmm.loc[ncRNA].start

#     transcripto = hmm.loc[ncRNA].score

#     if strand == 'minus':
#         transcripto = transcripto[::-1]

#     if strict:
#         transcripto_split = transcripto.split(',')
#         counts = [int(x) for x in transcripto_split]
#         #if (np.max(counts)==4):
#         if (np.max(counts)==4) or (np.quantile(counts, 0.75)>=3):
#             transcripto_split = ['1' if x == '2' else x for x in transcripto_split]
#         transcripto = ','.join(transcripto_split)

#     split_transcripts = transcripto.split(','.join(['1']*split_len))

#     longest = max([len(x) for x in split_transcripts])

#     high_scores = []
#     for transcript in split_transcripts:
#         try:
#             sc = np.mean([int(x) for x in transcript.strip(',').split(',')])
#             high_scores.append(sc)
#         except:
#             continue
#     highest = np.max(high_scores)
#     inherited = False
#     print(counts)
#     print(split_transcripts)
#     len_to_add = 0
#     for transcript in split_transcripts:
#         current_longest = ((len(transcript) == longest) and (len(transcript) > 3))

#         if transcript == ',':
#             current_pos += (50*split_len)
#         elif transcript == '':
#             current_pos += (50*split_len)
#         else:

#             transcript_counts = transcript.strip(',').split(',')
#             if (transcript[0] == ',') and (strand == 'plus'):
#                 current_pos += 50
#             if (strand == 'minus') and inherited:
#                 current_pos += (50*split_len)
# #             if (transcript[-1] == ',') and (strand == '-'):
# #                 current_pos += (50*split_len)

#             transcript_counts = [int(x) for x in transcript_counts]

#             print(transcript_counts)

#             current_highest = np.abs(np.mean(transcript_counts) - highest) <= 1e-100

#             transcript_portion = (len(transcript_counts)*50)/transcript_length
#             if ((len(transcript_counts) > min_len) and (transcript_portion >= min_portion)) or current_highest or current_longest:

#                 start_pos = current_pos

#                 for i in range(len(transcript_counts)):

#                     if transcript_counts[i] == 1:
#                         print('trimmin')
#                         start_pos += 50

#                     else:
#                         break

#                 if np.mean(transcript_counts) > 1.1:
#                     start_list.append(start_pos)

#                 trim = trim_expression(transcript_counts, True)

#                 if trim:
#                     if np.mean(transcript_counts) > 1.1:
#                         end_list.append(current_pos + ((len(transcript_counts)*50)-trim))

#                 else:
#                     if np.mean(transcript_counts) > 1.1:
#                         if strand == 'plus':
#                             trimmer = 50
#                         elif strand == 'minus':
#                             trimmer = 0
#                         else:
#                             raise Exception('strand error')
#                         end_list.append(current_pos + (len(transcript_counts)*50) - trimmer)

#             print('adding')
#             if transcript[-1] == ',':
#                 current_pos += (50*split_len)
#             current_pos += (len(transcript_counts)*50)

#             if transcript[-1] == ',':
#                 inherited = True

#     return start_list, end_list

parser = argparse.ArgumentParser()
parser.add_argument('--chrom', type=str, required=True)
parser.add_argument('--strand', type=str, required=True)

if __name__ == '__main__':

    args = parser.parse_args()
    chrom = args.chrom
    strand = args.strand

    hmm = pd.read_csv('NonCodingRNA/tables/{chrom}.{strand}.hmm.sorted.bed.gz'.format(
        chrom = chrom, strand = strand), sep='\t',
                 names = ['chrom', 'start', 'end', 'names', 'score', 'strand'] + [
                     'chRNA_'+str(i+1) for i in range(86)
                 ]
                     ).sort_values('start')

    hmm.index = hmm.names

    hmm['length'] = hmm.end - hmm.start
    samples = ['chRNA_'+str(i+1) for i in range(86)]
    RPKM = ((hmm[samples]/np.array(hmm[samples].sum(axis=0))).T/np.array(hmm.length)).T*1e9
    RPKM.index = hmm.names

    chrom_list = []
    start_list = []
    end_list = []
    names_list = []
    score_list = []
    strand_list = []

    ncRNAs = pd.Index([x for x in hmm.index if x[:5] == 'ncRNA'])
    ncRNAs = ncRNAs.intersection(RPKM.loc[RPKM.median(axis=1)>0.1].index)

    for n in ncRNAs:
        start = hmm.loc[n].start
        transcript_end = hmm.loc[n].end
        scores = [int(x) for x in hmm.loc[n].score.split(',')]
        transcript_ln = transcript_end - start
        ln = len(scores)
        if (RPKM.loc[n].median() >= 1) or (ln > 100):
#             if (ln < 750):
            if (ln < 650):
                srt, end = trim_and_split(hmm, n, min_len=50, strict=True, split_len=10, strand=strand)
#             elif (ln >= 750):
            else:
                srt, end = trim_and_split(hmm, n, min_len=50, strict=True, split_len=100, strand=strand)
        else:
            srt, end = [], []

        print('trimming: ' + n)
        print(srt, end)
        print('')
        if len(srt) > 0:

            for i in range(len(srt)):

#                 if strand == 'plus':

                start_ = start + srt[i]
                end_ = start + end[i]
                if strand == 'plus':
                    strand_list.append('+')
                else:
#                     start_ = start + (transcript_ln - end[i])
#                     print(end[i])
#                     end_ = start + (transcript_ln - srt[i])
#                     print(srt[i])
                    strand_list.append('-')

                chrom_list.append(chrom)
                start_list.append(start_)
                end_list.append(end_)
                names_list.append(n + '_' + str(i+1))
                score_list.append('.')
        else:
            chrom_list.append(chrom)
            start_list.append(start)
            end_list.append(transcript_end)
            names_list.append(n)
            score_list.append('.')
            if strand == 'plus':
                strand_list.append('+')
            else:
                strand_list.append('-')

#     pc_names = pd.Index([x for x in hmm.index if x[:5] != 'ncRNA'])
#     chrom_list += list(hmm.loc[pc_names].chrom)
#     start_list += list(hmm.loc[pc_names].start)
#     end_list += list(hmm.loc[pc_names].end)
#     names_list += list(hmm.loc[pc_names].names)
#     score_list += list(hmm.loc[pc_names].score)
#     strand_list += list(hmm.loc[pc_names].strand)

    df = pd.DataFrame()
    df['chrom'] = chrom_list
    df['start'] = start_list
    df['end'] = end_list
    df['names'] = names_list
    df['score'] = score_list
    df['strand'] = strand_list

    df.to_csv('NonCodingRNA/tables/{chrom}.{strand}.hmm_trimmed.bed.gz'.format(
        chrom = chrom, strand = strand), sep='\t', index=False, header=False)


from decimal import Decimal, InvalidOperation
from typing import TYPE_CHECKING, Any, List, Optional, TypedDict

from django.db import models, transaction

if TYPE_CHECKING:
    from senda.core.models.clients import ClientModel
    from senda.core.models.localities import LocalityModel, StateChoices
    from senda.core.models.offices import OfficeModel
    from senda.core.models.order_internal import InternalOrderModel
    from senda.core.models.order_supplier import SupplierOrderModel
    from senda.core.models.products import (
        ProductModel,
        ProductTypeChoices,
        ProductStockInOfficeModel,
    )
    from senda.core.models.purchases import PurchaseModel
    from senda.core.models.rental_contracts import RentalContractModel
    from senda.core.models.suppliers import SupplierModel
    from senda.core.models.employees import EmployeeModel, EmployeeOfficeModel
    from users.models import UserModel

class ClientModelManager(models.Manager["ClientModel"]):
    """
    Custom manager for the ClientModel, providing methods to create and update client instances.
    """

    def create_client(
        self,
        email: str,
        first_name: str,
        last_name: str,
        locality: "LocalityModel",
        house_number: str,
        street_name: str,
        house_unit: str,
        dni: str,
        phone_code: str,
        phone_number: str,
    ) -> "ClientModel":
        """
        Creates a new client instance with the given details. Validates to ensure the email and DNI are unique.
        """

        if self.filter(email=email).exists():
            raise ValueError("Ya existe un cliente con ese email")

        if self.filter(dni=dni).exists():
            raise ValueError("Ya existe un cliente con ese DNI")

        return self.create(
            email=email,
            first_name=first_name,
            last_name=last_name,
            locality=locality,
            house_number=house_number,
            street_name=street_name,
            house_unit=house_unit,
            dni=dni,
            phone_code=phone_code,
            phone_number=phone_number,
        )

    def update_client(
        self, client: "ClientModel", locality: "LocalityModel", **kwargs: Any
    ) -> "ClientModel":
        """
        Updates an existing client instance with the provided details.
        """

        client.locality = locality
        for field, value in kwargs.items():
            setattr(client, field, value)
        client.save()
        return client

class LocalityModelManager(models.Manager["LocalityModel"]):
    """
    Custom manager for the LocalityModel, providing methods to create and retrieve locality instances.
    """

    def create_locality(
        self, name: str, postal_code: str, state: "StateChoices"
    ) -> "LocalityModel":
        """
        Creates a new locality instance with the given details. Ensures the locality's uniqueness.
        """
        name = name.strip().lower().title()

        if self.filter(name=name, postal_code=postal_code, state=state).exists():
            raise ValueError("La localidad ya existe")

        return self.create(name=name, postal_code=postal_code, state=state)

    def get_or_create_locality(
        self, name: str, postal_code: int, state: "StateChoices"
    ) -> "LocalityModel":
        """
        Retrieves or creates a locality instance based on the provided details.
        """
        locality, _created = self.get_or_create(
            name=name, postal_code=postal_code, state=state
        )
        return locality

InternalOrderProductsDict = TypedDict(
    "InternalOrderProductsDict", {"id": str, "quantity": int}
)

class InternalOrderManager(models.Manager["InternalOrderModel"]):
    """
    Custom manager for the InternalOrderModel, handling the creation of internal orders.
    """

    @transaction.atomic
    def create_internal_order(
        self,
        office_branch: "OfficeModel",
        office_destination: "OfficeModel",
        user: "UserModel",
        products: List[InternalOrderProductsDict],
    ) -> "InternalOrderModel":
        """
        Creates a new internal order with associated products and history. This process is atomic.
        """
        from senda.core.models.order_internal import InternalOrderHistoryStatusChoices

        internal_order = self.create(
            office_branch=office_branch, office_destination=office_destination
        )
        internal_order.save()
        internal_order.history.create(
            status=InternalOrderHistoryStatusChoices.PENDING,
            internal_order=internal_order,
            user=user,
        )

        for product in products:
            internal_order.orders.create(
                product_id=product["id"],
                quantity=product["quantity"],
            )

        return internal_order

class SupplierModelManager(models.Manager["SupplierModel"]):
    @transaction.atomic
    def create_supplier(
        self,
        cuit: str,
        name: str,
        email: str,
        locality: "LocalityModel",
        house_number: str,
        street_name: str,
        house_unit: str,
        phone_code: str,
        phone_number: str,
        note: str,
    ) -> "SupplierModel":
        if self.filter(email=email).exists():
            raise ValueError("Ya existe un proveedor con ese email")

        if self.filter(cuit=cuit).exists():
            raise ValueError("Ya existe un proveedor con ese CUIT")

        return self.create(
            cuit=cuit,
            name=name,
            email=email,
            locality=locality,
            house_number=house_number,
            street_name=street_name,
            house_unit=house_unit,
            phone_code=phone_code,
            phone_number=phone_number,
            note=note,
        )

SupplierOrderProductsDict = TypedDict(
    "SupplierOrderProductsDict", {"id": str, "quantity": int}
)

class SupplierOrderManager(models.Manager["SupplierOrderModel"]):
    """
    Custom manager for the SupplierOrderModel, handling the creation of supplier orders.
    """

    @transaction.atomic
    def create_supplier_order(
        self,
        supplier: "SupplierModel",
        office_destination: "OfficeModel",
        user: "UserModel",
        products: List[SupplierOrderProductsDict],
    ) -> "SupplierOrderModel":
        """
        Creates a new supplier order with associated products, history, and total cost calculation. This process is atomic.
        """
        from senda.core.models.order_supplier import SupplierOrderHistoryStatusChoices

        supplier_order = self.create(
            supplier=supplier, office_destination=office_destination
        )
        supplier_order.save()

        supplier_order.history.create(
            status=SupplierOrderHistoryStatusChoices.PENDING,
            supplier_order=supplier_order,
        )

        for product in products:
            supplier_order.orders.create(
                product_id=product["id"],
                quantity=product["quantity"],
            )

        supplier_order.total = supplier_order.calculate_total()

        return supplier_order

ProductStockInOfficeModelDict = TypedDict(
    "ProductStockInOfficeModelDict", {"office_id": str, "stock": int}
)

ProductSupplierDict = TypedDict(
    "ProductSupplierDict", {"supplier_id": str, "price": str}
)

ProductServiceDict = TypedDict("ProductServiceDict", {"name": str, "price": str})

def parse_price(price_str: str) -> Decimal:
    """
    Parses a price string into a Decimal object, handling different formatting conventions.
    """

    standard_format_str = price_str.replace(".", "").replace(",", ".")
    try:
        return Decimal(standard_format_str)
    except InvalidOperation:
        raise ValueError(f"The price {price_str} is not a valid number format")

class ProductModelManager(models.Manager["ProductModel"]):
    """
    Custom manager for the ProductModel, providing methods to create product instances with associated details.
    """

    def create_product(
        self,
        sku: str,
        name: str,
        brand_id: str,
        description: str,
        type: "ProductTypeChoices",
        price: str,
        stock: List[ProductStockInOfficeModelDict],
        services: List[ProductServiceDict],
        suppliers: List[ProductSupplierDict],
    ) -> "ProductModel":
        """
        Creates a new product instance with various associated data like stock, services, and suppliers.
        """
        if self.filter(sku=sku).exists():
            raise ValueError("Ya existe un producto con ese sku")

        product = self.create(
            sku=sku,
            name=name,
            brand_id=brand_id,
            description=description,
            type=type,
            price=parse_price(price),
        )

        for stock_data in stock:
            product.stock.create(
                office_id=stock_data["office_id"],
                stock=stock_data["stock"],
            )

        for service_data in services:
            product.services.create(
                name=service_data["name"],
                price=parse_price(service_data["price"]),
            )

        for supplier_data in suppliers:
            product.suppliers.create(
                supplier_id=supplier_data["supplier_id"],
                price=parse_price(supplier_data["price"]),
            )

        return product

    def get_products_with_stock_in_office(self, office: "OfficeModel", **kwargs: Any):
        """
        Returns all products with associated stock in the given office.
        """
        return self.filter(stock__office=office, **kwargs)

class ProductStockInOfficeManager(models.Manager["ProductStockInOfficeModel"]):
    """
    Custom manager for the ProductStockInOfficeModel, providing methods to create and update stock instances.
    """

    def create_stock(
        self, product: "ProductModel", office: "OfficeModel", stock: int
    ) -> "ProductStockInOfficeModel":
        """
        Creates a new stock instance for the given product and office.
        """
        if self.filter(product=product, office=office).exists():
            raise ValueError("Ya existe un stock para ese producto en esa sucursal")

        return self.create(product=product, office=office, stock=stock)

PurchaseProductsItemDict = TypedDict(
    "PurchaseProductsItemDict", {"product": str, "quantity": int}
)

class PurchaseModelManager(models.Manager["PurchaseModel"]):
    """
    Custom manager for the PurchaseModel, handling the creation of purchase instances.
    """

    @transaction.atomic
    def create_purchase(
        self,
        client: "ClientModel",
        office: str,
        products: List[PurchaseProductsItemDict],
    ) -> "PurchaseModel":
        """
        Creates a new purchase instance with associated purchase items. This process is atomic.
        """
        purchase = self.create(client=client, office_id=office)
        purchase.save()

        for product in products:
            purchase.purchase_items.create(
                quantity=product["quantity"],
                product_id=product["product"],
            )

        purchase.recalculate_total()

        return purchase

RentalContractProductsItemDict = TypedDict(
    "RentalContractProductsItemDict",
    {"id": str, "quantity": int, "service": Optional[str]},
)

class RentalContractManager(models.Manager["RentalContractModel"]):
    """
    Custom manager for the RentalContractModel, handling the creation of rental contract instances.
    """

    @transaction.atomic
    def create_rental_contract(
        self,
        client: "ClientModel",
        products: List[RentalContractProductsItemDict],
        office: str,
        locality: "LocalityModel",
        house_number: str,
        street_name: str,
        house_unit: str,
        contract_start_datetime: str,
        contract_end_datetime: str,
    ) -> "RentalContractModel":
        """
        Creates a new rental contract with associated items and history. This process is atomic.
        """
        from senda.core.models.rental_contracts import RentalContractStatusChoices

        rental_contract = self.create(
            client=client,
            office_id=office,
            locality=locality,
            house_number=house_number,
            street_name=street_name,
            house_unit=house_unit,
            contract_start_datetime=contract_start_datetime,
            contract_end_datetime=contract_end_datetime,
        )

        for product in products:
            rental_contract.rental_contract_items.create(
                product_id=product["id"],
                service_id=product["service"],
                quantity=product["quantity"],
            )

        rental_contract.rental_contract_history.create(
            status=RentalContractStatusChoices.PRESUPUESTADO,
            rental_contract=rental_contract,
        )

        return rental_contract

class EmployeeModelManager(models.Manager["EmployeeModel"]):
    """
    Custom manager for the EmployeeModel, providing methods to create and update employee instances.
    """

    @transaction.atomic
    def create_employee(self, user: "UserModel", offices: List[str]):
        from senda.core.models.offices import OfficeModel
        from senda.core.models.employees import EmployeeOfficeModel

        """
        Creates a new employee instance, ensuring the user does not already have an associated employee.
        """
        if self.filter(user=user).exists():
            raise ValueError("Ya existe ese empleado")

        employee = self.create(user=user)

        for office_id in offices:
            office = OfficeModel.objects.get(id=office_id)
            EmployeeOfficeModel.objects.create(employee=employee, office=office)

        return employee

    def update_employee(self, employee: "EmployeeModel", **kwargs: Any):
        """
        Updates an existing employee instance with the provided details.
        """
        for field, value in kwargs.items():
            setattr(employee, field, value)
        employee.save()
        return employee


import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (15, 5)

def plot_temperature(m, a, b, min, max):
    """
    This function will plot the temperatures for a given month m, within the time range a-b and temperature range min-max.
    m: The month the user chooses as a number
    a: The year the user wants to read temperatures FROM
    b: The year the user wants to read temperatures TO
    min: Minimum temperature the user wants to read
    max: Maximum temperature the user wants to read
    """

    """
    For months the function takes in a number instead of the name of the month. It would be faster with typing the month, but I've decided to do it this way! :D
    """
    month_list = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}
    month = month_list[m];
    temp_stats = pd.read_csv('temperature.csv', sep = ',')
    temp_stats.index = temp_stats['Year']
    del temp_stats['Year']
    print(temp_stats[month].loc[a:b])
    temp_stats[month].loc[a:b].plot()
    plt.ylim(ymax = max, ymin = min)
    plt.ylabel('Temperature')
    plt.xlabel('Year')
    plt.legend()
    plt.show()

def plot_CO2(a, b, min, max):
    """
    This function will plot the CO2 levels for a given time range a-b and CO2 range min-max.
    a: The year the user wants to read CO2 levels FROM
    b: The year the user wants to read CO2 levels TO
    min: Minimum CO2 level the user wants to read
    max: Maximum CO2 level the user wants to read
    """

    """
    I've used a zero, 0, instead of O in "CO2", because apperantly CO2 is a reserved variable...?
    """
    C02_stats = pd.read_csv('co2.csv', sep = ',')
    C02_stats.index = C02_stats['Year']
    del C02_stats['Year']
    C02_stats.loc[a:b].plot()
    plt.ylim(ymax = max, ymin = min)
    plt.ylabel('CO2 levels')
    plt.xlabel('Year')
    plt.legend()
    plt.show()

#Test, uncomment to see :D
plot_temperature(6, 1880, 2017, 0, 22)
plot_CO2(1774, 1842, 0, 20)


# Run 'discoronode.py' program to start processes to execute computations sent
# by this client, along with this program.

# This example is similar to 'discoro_client6.py', except it uses broadcasting
# over Channel to send messages to remote coroutines to process, and uses
# 'deque' module to implement circular buffer.

import asyncoro.disasyncoro as asyncoro
from asyncoro.discoro import *

# This generator function is sent to remote discoro to analyze data
# and generate apprporiate signals that are sent to a coroutine
# running on client. The signal in this simple case is average of
# moving window of given size is below or above a threshold.
def rcoro_avg_proc(channel, threshold, trend_coro, window_size, coro=None):
    import collections
    # subscribe to channel (at client)
    yield channel.subscribe(coro)
    # create circular buffer
    data = collections.deque(maxlen=window_size)
    for i in range(window_size):
        data.append(0.0)
    cumsum = 0.0
    # first message is 'start' command; see 'client_proc'
    assert (yield coro.receive()) == 'start'
    while True:
        i, n = yield coro.receive()
        if n is None:
            break
        cumsum += (n - data[0])
        avg = (cumsum / window_size)
        if avg > threshold:
            trend_coro.send((i, 'high', float(avg)))
        elif avg < -threshold:
            trend_coro.send((i, 'low', float(avg)))
        data.append(n)
    raise StopIteration(0)

# This generator function is sent to remote discoro process to save
# the received data in a file (on the remote peer).
def rcoro_save_proc(channel, coro=None):
    import os
    import tempfile
    # subscribe to channel (at client)
    yield channel.subscribe(coro)
    # first message is 'start' command (to make sure all recipients started)
    assert (yield coro.receive()) == 'start'
    # save data in 'tickdata' where computation files are saved (this will be
    # deleted when computation is done, so it must be copied elsewhere if
    # necessary)
    with open('tickdata', 'w') as fd:
        while True:
            i, n = yield coro.receive()
            if n is None:
                break
            fd.write('%s: %s\n' % (i, n))
    raise StopIteration(0)

# This coroutine runs on client. It gets trend messages from remote
# coroutine that computes moving window average.
def trend_proc(coro=None):
    coro.set_daemon()
    while True:
        trend = yield coro.receive()
        print('trend signal at % 4d: %s / %.2f' % (trend[0], trend[1], trend[2]))

# This process runs locally. It creates two remote coroutines at two discoronode
# server processes, two local coroutines, one to receive trend signal from one
# of the remote coroutines, and another to send data to two remote coroutines
def client_proc(computation, coro=None):
    # schedule computation with the scheduler; scheduler accepts one computation
    # at a time, so if scheduler is shared, the computation is queued until it
    # is done with already scheduled computations
    if (yield computation.schedule()):
        raise Exception('Could not schedule computation')

    # in discoro_client6.py, data is sent to each remote coroutine; here, data
    # is broadcast over channel and remote coroutines subscribe to it
    data_channel = asyncoro.Channel('data_channel')
    # not necessary to register channel in this case, as it is sent to remote
    # coroutines; if they were to 'locate' it, it should be registered
    # data_channel.register()

    trend_coro = asyncoro.Coro(trend_proc)

    rcoro_avg = yield computation.run(rcoro_avg_proc, data_channel, 0.4, trend_coro, 10)
    assert isinstance(rcoro_avg, asyncoro.Coro)
    rcoro_save = yield computation.run(rcoro_save_proc, data_channel)
    assert isinstance(rcoro_save, asyncoro.Coro)

    # make sure both remote coroutines have subscribed to channel ('deliver'
    # should return 2 if they both are)
    assert (yield data_channel.deliver('start', n=2)) == 2

    # if data is sent frequently (say, many times a second), enable
    # streaming data to remote peer; this is more efficient as
    # connections are kept open (so the cost of opening and closing
    # connections is avoided), but keeping too many connections open
    # consumes system resources
    yield asyncoro.AsynCoro.instance().peer(rcoro_avg.location, stream_send=True)
    yield asyncoro.AsynCoro.instance().peer(rcoro_save.location, stream_send=True)

    # send 1000 items of random data to remote coroutines
    for i in range(1000):
        n = random.uniform(-1, 1)
        item = (i, n)
        # data can be sent to remote coroutines either with 'send' or
        # 'deliver'; 'send' is more efficient but no guarantee data
        # has been sent successfully whereas 'deliver' indicates
        # errors right away
        data_channel.send(item)
        yield coro.sleep(0.02)
    item = (i, None)
    data_channel.send(item)

    yield computation.close()
    data_channel.close()

if __name__ == '__main__':
    import random
    # asyncoro.logger.setLevel(asyncoro.Logger.DEBUG)
    # if scheduler is shared (i.e., running as program), nothing needs
    # to be done (its location can optionally be given to 'schedule');
    # othrwise, start private scheduler:
    Scheduler()
    computation = Computation([])
    asyncoro.Coro(client_proc, computation)


import csv
import json
from collections import defaultdict
import itertools
import os
import sys
import argparse

## NB update relevant config file name here
#from config2019 import *
# Mr Gorbachev, tear down etc etc

# Generate booth data structure (combinations hardcoded):
NPP_FIELDS = ["ID", "Division", "Booth", "Latitude", "Longitude"]

# So we'll have 7 columns of stuff before the prefs start:
SA1s_FIELDS = ["year","state_ab", "div_nm", "SA1_id", "pp_id", "pp_nm", "votes"]

# we'll use this one later
ttyjump = '\033[F\033[2K' if (hasattr(sys.stderr, "isatty") and sys.stderr.isatty()) else ""

def main(SCENARIO, NPP_BOOTHS_PATH, STATE, YEAR, PARTIES, SA1S_BREAKDOWN_PATH, SA1S_PREFS_PATH):

    combinations = ["None"] # special-casing the empty

    for r in range(len(PARTIES)):
        chooseN = list(itertools.combinations(sorted(PARTIES), r+1))
        for i in chooseN:
            combinations += ["".join(j) for j in list(itertools.permutations(i))]

    total_combos = len(combinations)

    ### Load in booth data

    booths = {}

    boothsfields = NPP_FIELDS+combinations+["Total"]

    with open(NPP_BOOTHS_PATH) as consolcsv:
        consolreader = csv.DictReader(consolcsv) # fieldnames implicit from the first line

        for row in consolreader:
            booths[row["Division"]+row["Booth"]] = row # key by 'divbooth'

    ###

    lines = []

    with open(SA1S_BREAKDOWN_PATH) as sa1scsv:

        sa1sreader = csv.DictReader(sa1scsv) # fieldnames implicit from the first line

        progress = 0
        print()
        for srow in sa1sreader:

            if not srow["state_ab"]==STATE:
                continue # All SA1s nationwide are in the one file - so any row with the wrong state can be safely skipped.
            elif not (srow["year"] == YEAR):
                sys.exit("Problem in `{SA1s_BREAKDOWN_PATH}`: Unsupported election year: "+srow["year"]+". Exiting.")
                # However, the wrong year is definitely cause for concern

            # if progress == 0:
            #     print(f"Projecting Senate results for scenario {SCENARIO} from "+srow["year"]+" onto state/territory-level electoral boundaries.\n", file=sys.stderr)

            # basically a big vector multiply

            bob = [srow["SA1_id"]]
            db = [ booths[srow["div_nm"]+srow["pp_nm"]][i] for i in boothsfields[5:] ]
            ##  print("db:\t", db)
            for i in range(len(db)):
                try:
                    bob.append(float(srow["votes"]) * float(db[i]) / float(db[-1]))
                except ZeroDivisionError:
                    bob.append(0.0)

            lines.append(bob)

            ##  print("bob:\t", bob)

            progress+= 1
            if (progress % 1000 == 0):
                print(ttyjump+f"... Projection progress: {progress:n} SA1s...", file=sys.stderr)

    print(ttyjump+f"... Projection complete: {progress:n} SA1s", file=sys.stderr)

    # Accumulation phase.

    sa1s = {}
    print(f"... Progress: summing SA1s...", file=sys.stderr)
    for line in lines:
        sa1_id = line[0]

        if sa1_id not in sa1s:
            sa1s[sa1_id] = line[1:]
        else:
            for i in range(1, len(line)):
                sa1s[sa1_id][i-1] += line[i]

    outlines = []

    for ids in sa1s.keys():
        outlines.append([ids] + sa1s[ids])

    outlines.sort()

    print(ttyjump+"... Writing File...", file=sys.stderr, end='')

    with open(SA1S_PREFS_PATH, 'w') as fp:
        print(*(["SA1_id"] + boothsfields[5:]), sep=',', file=fp, flush=True)

        for line in outlines:
            print(*line, sep=',', file=fp)

    print("... Done!\n", file=sys.stderr)

# end main()

def run(argp, confp):
    try:
        if argp.scenarios and not (set(argp.scenarios).intersection(set(confp.sections()))):
            print(f"Error: no specified scenario is defined.", file=sys.stderr)
        else:
            scenlist = confp.sections() if argp.all else argp.scenarios
            for SCENARIO in scenlist:
                YEAR = confp[SCENARIO]['YEAR']
                PARTIES = json.loads(confp[SCENARIO]['GROUPS'])
                STATE = confp[SCENARIO]['STATE']
                SA1S_DISTRICTS_PATH = confp[SCENARIO]['SA1S_DISTS_PATH']
                SA1S_BREAKDOWN_PATH = confp[SCENARIO]['SA1S_BREAKDOWN_PATH']
                OUTPUT_DIR = confp[SCENARIO]['OUTPUT_DIR']
                NPP_BOOTHS_PATH = os.path.join(OUTPUT_DIR, SCENARIO, confp[SCENARIO]['NPP_BOOTHS_FN'])
                SA1S_PREFS_PATH = os.path.join(OUTPUT_DIR, SCENARIO, confp[SCENARIO]['SA1S_PREFS_FN'])

                print("*** Projecting Scenario {}: {}, in {} [{}] ***\n".format(SCENARIO, " vs ".join(PARTIES.keys()), STATE, YEAR), file=sys.stderr)
                main(SCENARIO, NPP_BOOTHS_PATH, STATE, YEAR, PARTIES, SA1S_BREAKDOWN_PATH, SA1S_PREFS_PATH)
    except KeyError as k:
        sys.exit(f"There was an issue with the arguments or configuration file: {k}")

def parse_argv():
    parser = argparse.ArgumentParser(help="Project booth results down onto SA1s")
    # pretty much everything comes from the configfile, yeah?
    parser.add_argument('configfile', type=argparse.FileType('r'))
    mxg = parser.add_mutually_exclusive_group(required=True)
    mxg.add_argument("-a", "--all", action="store_true", help="run every scenario defined in `configfile`")
    mxg.add_argument("-s", "--scenario", dest='scenarios', metavar='scenario', action="append", help="run a scenario code defined in `configfile` (can be specified multiple times to run several scenarios)")
    # not going to support listing scenarios here

    return parser.parse_args()

if __name__ == "__main__":
    argp = parser.parse_args()
    confp = configparser.ConfigParser()
    confp.read_file(argp.configfile)
    run(argp, confp)


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
df = pd.read_csv("D-GROWTH.csv")
df['Date'] = pd.to_datetime(df["Date"])
all_years = df['Date'].dt.year.unique()
temp = {'Date': [], 'Growth': []}
for i in all_years:
    for j in range(12, 0, -1):
        data = df[(df['Date'].dt.year == i) & (df['Date'].dt.month == j)]
        try:
            last_date = max(data['Date'].dt.date)
            temp['Date'].append(last_date)
            temp['Growth'].append(data[data['Date'] == last_date].iloc[0, 2])
        except:
            continue
temp = pd.DataFrame(temp)
month_data = {'Date': [], 'Month Growth': []}
for i in range(len(temp)):
    month_data['Date'].append(temp['Date'][i])
    try:
        x = ((temp['Growth'][i+1] - temp['Growth'][i]) / temp['Growth'][i+1]) * 100
        month_data['Month Growth'].append(x)
    except:
        month_data['Month Growth'].append(temp['Growth'][i])
month_data = pd.DataFrame(month_data)
month_data.to_csv("M-Growth.csv")
sns.lineplot(x='Date', y='Month Growth', data=month_data, markers=True, dashes=False)
plt.show()


from tkinter import *
from tkinter import messagebox
from openpyxl import *
import cv2
import pickle
import cvzone
import numpy as np
import os

root = Tk()
root.title("ParkiN")
root.geometry('925x500+300+200')
root.configure(bg='#fff')
root.resizable(False, False)

def book():
    vehicleno = user.get()
    phoneno = Phone.get()
    gmailac = gmail.get()

    if vehicleno == '' and phoneno == '' and gmailac == '':
        messagebox.showerror("X", 'Please fill the details')

    building = Toplevel(root)
    building.title('Parkin!')
    building.geometry('925x500+300+200')
    building.configure(bg='#fff')
    building.resizable(False, False)
    Label(building, text='Please select Your Parking Space', fg='#57a1f8', bg='white',
          font=('Microsoft Yahei UI Light', 20)).grid(row=0, column=0, columnspan=2, padx=285, pady=5)

    Parking_A = Button(building, text='Parking A', pady=1, width=10, fg='black', border=0, bg='#FFFFEB',
                       font=('Microsoft Yahei UI Light', 14,), command=footage)
    Parking_A.grid(row=1, column=0, padx=140, pady=10)

    park1 = PhotoImage(file='parkingb.png')
    park11 = Label(building, image=park1, border=0, bg='white')
    park11.grid(row=2, column=0, padx=40, pady=10, sticky=W)
    park11.image = park1

    Parking_B = Button(building, text='Parking B', pady=1, width=10, fg='black', border=0, bg='#FFFFEB',
                       font=('Microsoft Yahei UI Light', 14,), command=build2)
    Parking_B.grid(row=1, column=1, padx=140, pady=10)

    park2 = PhotoImage(file='parkingb.png')
    park22 = Label(building, image=park2, border=0, bg='white')
    park22.grid(row=2, column=1, padx=40, pady=10, sticky=W)
    park22.image = park2

    Parking_C = Button(building, text='Parking C', pady=1, width=10, fg='black', border=0, bg='#FFFFEB',
                       font=('Microsoft Yahei UI Light', 14,), command=build3)
    Parking_C.grid(row=3, column=0, padx=140, pady=10)

    Parking_D = Button(building, text='Parking D', pady=1, width=10, fg='black', border=0, bg='#FFFFEB',
                       font=('Microsoft Yahei UI Light', 14,), command=build4)
    Parking_D.grid(row=3, column=1, padx=140, pady=10)

img = PhotoImage(file='parking1.png')
Label(root, image=img, border=0, bg='white').place(x=-350, y=-265)

frame = Frame(root, width=400, height=390, bg='#fff')
frame.place(x=450, y=80)

heading = Label(frame, text='Welcome to Parkin!', fg="#57a1f8", bg="white", font=('Microsoft Yahei UI Light', 20,))
heading.place(x=120, y=5)

def insert():
    # if user not fill any entry
    # then print "empty input"
    if (Phone.get() == "" or
            user.get() == "" or
            gmail.get() == ""):

        print("Please fill all details")

    else:

        # assigning the max row and max column
        # value upto which data is written
        # in an excel sheet to the variable
        current_row = sheet.max_row
        current_column = sheet.max_column

        # get method returns current text
        # as string which we write into
        # excel spreadsheet at particular location
        sheet.cell(row=current_row + 1, column=1).value = Phone.get()
        sheet.cell(row=current_row + 1, column=2).value = user.get()
        sheet.cell(row=current_row + 1, column=3).value = gmail.get()

        # save the file
        wb.save('book1.xlsx')

        # set focus on the name_field box
        user.focus_set()

    # call the clear() function

def footage():
    cap = cv2.VideoCapture('carPark.mp4')

    file_path = os.path.join('D:', 'Coding', 'C course', '.vscode', 'CarParkingPos')

    with open(file_path, 'rb') as f:
        posList = pickle.load(f)

    width, height = 107, 46

    def checkParkingSpace(imgPro):
        spaceCounter = 0

        for pos in posList:
            x, y = pos

            imgCrop = imgPro[y:y + height, x:x + width]

            # cv2.imshow(str(x*y),imgCrop)
            count = cv2.countNonZero(imgCrop)  # The number in front of the boxes
            if count < 900:
                color = (0, 255, 0)
                thickness = 5
            else:
                color = (0, 0, 255)
                thickness = 2

            cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), color, 2)
            cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=0.8, thickness=1, offset=0)

            cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=0.8, thickness=1, offset=0)

    while True:

        if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
            cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
        success, img = cap.read()
        imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        imgBlur = cv2.GaussianBlur(imgGray, (3, 3), 1)
        imgThreshold = cv2.adaptiveThreshold(imgBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 25,
                                             16)
        imgMedian = cv2.medianBlur(imgThreshold, 5)
        kernel = np.ones((3, 3), np.uint8)
        imgDilate = cv2.dilate(imgMedian, kernel, iterations=1)

        checkParkingSpace(imgDilate)
        cv2.imshow("Image", img)
        cv2.imshow("ImageBlur", imgBlur)
        cv2.imshow("ImageThres", imgMedian)
        cv2.waitKey(50)

ok = Button(frame, text='Book', pady=1, width=25, fg='white', border=0, bg='#57a1f8',
            font=('Microsoft Yahei UI Light', 14), command=lambda: [insert(), book()])
ok.place(x=115, y=250)

def on_enter(e):
    user.delete(0, 'end')

def on_leave(e):
    if user.get() == '':
        user.insert(0, 'Vehicle Number ')

user = Entry(frame, width=25, fg="black", border=0, bg='white', font=('Microsoft Yahei UI Light', 13))
user.place(x=110, y=67)

# This input will be given by the parking coordinator

user.insert(0, 'Vehicle Number')
user.bind("<FocusIn>", on_enter)
user.bind("<FocusOut>", on_leave)

Frame(frame, width=295, height=2, bg='black').place(x=110, y=90)

def on_enter(e):
    Phone.delete(0, 'end')

def on_leave(e):
    if Phone.get() == '':
        Phone.insert(0, 'Phone No.')

Phone = Entry(frame, width=25, fg="black", border=0, bg='white', font=('Microsoft Yahei UI Light', 13))
Phone.place(x=110, y=134)
Phone.insert(0, 'Phone No.')
Phone.bind("<FocusIn>", on_enter)
Phone.bind("<FocusOut>", on_leave)

Frame(frame, width=295, height=2, bg='black').place(x=110, y=160)

def on_enter(e):
    gmail.delete(0, 'end')

def on_leave(e):
    if gmail.get() == '':
        gmail.insert(0, 'Enter your Gmail')

gmail = Entry(frame, width=25, fg="black", border=0, bg='white', font=('Microsoft Yahei UI Light', 13))
gmail.place(x=110, y=200)
gmail.insert(0, 'Enter your Gmail ')
gmail.bind("<FocusIn>", on_enter)
gmail.bind("<FocusOut>", on_leave)

Frame(frame, width=295, height=2, bg='black').place(x=110, y=230)

# For Parking A
def build1():
    park_a=Toplevel(root)
    park_a.title('Parkin!')
    park_a.geometry('925x500+300+200')
    park_a.configure(bg='#fff')
    park_a.resizable(False,False)

# For Parking B
def build2():
    park_b = Toplevel(root)
    park_b.title('Parkin!')
    park_b.geometry('925x500+300+200')
    park_b.configure(bg='#fff')
    park_b.resizable(False, False)

# For Parking C
def build3():
    park_c = Toplevel(root)
    park_c.title('Parkin!')
    park_c.geometry('925x500+300+200')
    park_c.configure(bg='#fff')
    park_c.resizable(False, False)

# For Parking D
def build4():
    park_d = Toplevel(root)
    park_d.title('Parkin!')
    park_d.geometry('925x500+300+200')
    park_d.configure(bg='#fff')
    park_d.resizable(False, False)

# create the sheet object

wb = load_workbook('book1.xlsx')
sheet = wb.active

def excel():
    # resize the width of columns in
    # excel spreadsheet
    sheet.column_dimensions['A'].width = 20
    sheet.column_dimensions['B'].width = 10
    sheet.column_dimensions['C'].width = 30

    # write given data to an excel spreadsheet
    # at particular location
    sheet.cell(row=1, column=1).value = "mobile no."
    sheet.cell(row=1, column=2).value = "Vehicle No."
    sheet.cell(row=1, column=3).value = "e-mail id"

root.mainloop()


msg = "good"
import traceback
import os

try:
  import os
  import pickle
  import onnxruntime as rt
  from time import time
  from transformers import RobertaForSequenceClassification, RobertaTokenizer
  import numpy as np
  import urllib
except Exception as e:
  msg = traceback.format_exc()

tmp = "/tmp/"
cold = True
model_url="https://media.githubusercontent.com/media/onnx/models/main/text/machine_comprehension/roberta/model/roberta-sequence-classification-9.onnx"
model_object_key = "roberta-sequence-classification-9.onnx"
model_path = tmp + model_object_key

def has_gpu() -> bool:
  return os.path.isfile("/usr/bin/nvidia-smi")

# Check if models are available
# Download model if model is not already present
if not os.path.isfile(model_path):
  urllib.request.urlretrieve (model_url, model_path)

providers=['CPUExecutionProvider']
if has_gpu():
  providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
session = rt.InferenceSession(model_path, providers=providers)

input_id = session.get_inputs()[0].name

def main(args):
  global cold
  was_cold = cold
  cold = False
  try:
    start = time()

    input_text = args.get('input', "This film is so good")

    tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
    input_ids = [np.array(tokenizer.encode(input_text, add_special_tokens=True))]

    # for input_meta in session.get_inputs():
      # print(input_meta)
    # print(input_ids, to_numpy(input_ids))
    results = session.run([], {input_id: input_ids})

    pred = np.argmax(results)
    if(pred == 0):
        results = "Prediction: negative {}".format(results)
    elif(pred == 1):
        results = "Prediction: positive {}".format(results)

    end = time()
    return {"body": { "latency":end-start, "cold":was_cold, "start":start, "end":end, "output":results }}
  except Exception as e:
    err = str(e)
    try:
        trace = traceback.format_exc()
    except Exception as fug:
        trace = str(fug)
    return {"body": { "import_error":msg, "runtime_error":err, "trace":trace, "cold":was_cold }}


import torch
from torch import nn
from torch.nn import Sequential as Seq, Linear as Lin, Conv2d

##############################
#    Basic layers
##############################
def act_layer(act, inplace=False, neg_slope=0.2, n_prelu=1):
    """
    helper selecting activation
    :param act:
    :param inplace:
    :param neg_slope:
    :param n_prelu:
    :return:
    """

    act = act.lower()
    if act == 'relu':
        layer = nn.ReLU(inplace)
    elif act == 'leakyrelu':
        layer = nn.LeakyReLU(neg_slope, inplace)
    elif act == 'prelu':
        layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
    else:
        raise NotImplementedError('activation layer [%s] is not found' % act)
    return layer

def norm_layer(norm, nc):
    # helper selecting normalization layer
    norm = norm.lower()
    if norm == 'batch':
        layer = nn.BatchNorm2d(nc, affine=True)
    elif norm == 'instance':
        layer = nn.InstanceNorm2d(nc, affine=False)
    else:
        raise NotImplementedError('normalization layer [%s] is not found' % norm)
    return layer

class MLP(Seq):
    def __init__(self, channels, act='relu', norm=None, bias=True):
        m = []
        for i in range(1, len(channels)):
            m.append(Lin(channels[i - 1], channels[i], bias))
            if act:
                m.append(act_layer(act))
            if norm:
                m.append(norm_layer(norm, channels[-1]))
        super(MLP, self).__init__(*m)

class BasicConv(nn.Module):
    def __init__(self, channels, act='relu', norm=None, bias=True):
        super(BasicConv, self).__init__()
        m = []
        for i in range(1, len(channels)):
            m.append(Conv2d(channels[i - 1], channels[i], 1, bias=bias))
            if act:
                m.append(act_layer(act))
            if norm:
                m.append(norm_layer(norm, channels[-1]))
        self.body = Seq(*m)

    def forward(self, x, edge_index=None):
        return self.body(x)

def batched_index_select(inputs, index):
    """

    :param inputs: torch.Size([batch_size, num_dims, num_vertices, 1])
    :param index: torch.Size([batch_size, num_vertices, k])
    :return: torch.Size([batch_size, num_dims, num_vertices, k])
    """
    batch_size, num_dims, num_vertices, _ = inputs.shape
    k = index.shape[2]
    idx = torch.arange(0, batch_size) * num_vertices
    idx = idx.contiguous().view(batch_size, -1)

    inputs = inputs.transpose(2, 1).contiguous().view(-1, num_dims)
    index = index.contiguous().view(batch_size, -1) + idx.type(index.dtype).to(inputs.device)
    index = index.contiguous().view(-1)

    return torch.index_select(inputs, 0, index).contiguous().view(batch_size, -1, num_dims).transpose(2, 1).contiguous().view(batch_size, num_dims, -1, k)


import os
import sys
from os.path import join, dirname
from dotenv import load_dotenv
import datetime
import time
import schedule
import logging
import iso8601

from googleapiclient.discovery import build

import functools

logger = logging.getLogger('autosnap')
logging.basicConfig(stream=sys.stdout, level=logging.INFO)

def catch_exceptions(job_func):
    @functools.wraps(job_func)
    def wrapper(*args, **kwargs):
        try:
            job_func(*args, **kwargs)
        except:
            import traceback
            print(traceback.format_exc())
    return wrapper

def load_env():
    dotenv_path = join(dirname(__file__), '.env')
    load_dotenv(dotenv_path)

@catch_exceptions
def create_snapshot(project, disk, zone, snapshot_name):
    logger.info('Creating Snapshot.')

    body = {'name': snapshot_name + '-' + str(int(datetime.datetime.now().timestamp()))}
    logger.info(compute.disks().createSnapshot(project=project, disk=disk, zone=zone, body=body).execute())

@catch_exceptions
def delete_old_snapshots(project, snapshot_name):
    logger.info('Deleting Old Snapshots.')

    # Get a list of all snapshots
    snapshots = compute.snapshots().list(project=project).execute()

    while ( True ) :
        next_page_token = snapshots.get("nextPageToken", None)
        for snapshot in snapshots["items"]:
            snapshot_date = iso8601.parse_date(snapshot["creationTimestamp"])
            delete_before_date = datetime.datetime.now(snapshot_date.tzinfo) - datetime.timedelta(days=7)

            # Check that a snapshot is for this disk, and that it was created
            # more than 7 days ago.
            if snapshot["name"].startswith(snapshot_name) and \
                snapshot_date < delete_before_date:
                logger.info(compute.snapshots().delete(
                    project=project, snapshot=snapshot["name"]).execute())

        if next_page_token == None:
            break

        snapshots = compute.snapshots().list(
            project=project, pageToken=next_page_token).execute()

if __name__ == '__main__':
    logger.info('Loading Google Credentials.')

    compute = build('compute', 'v1')

    if not os.environ.get('PROJECT') and os.environ.get('DISK') and os.environ.get('INTERVAL_MINUTES'):
        load_env()  # not needed if loaded via docker

    project = os.environ.get('PROJECT')
    disk = os.environ.get('DISK')
    zone = os.environ.get('ZONE')
    snapshot_name = os.environ.get('SNAPSHOT_NAME')
    interval = int(os.environ.get('INTERVAL_MINUTES'))

    # Run first snapshot:
    create_snapshot(project, disk, zone, snapshot_name)

    # Run first snapshot:
    delete_old_snapshots(project, snapshot_name)

    # Create Schedule:
    schedule.every(interval).minutes.do(create_snapshot, project, disk, zone, snapshot_name)

    # Delete old snapshots
    schedule.every(interval).minutes.do(delete_old_snapshots, project, snapshot_name)

    while True:
        schedule.run_pending()
        time.sleep(1)


import mailbox
from contextlib import contextmanager

import tempfile

from io import BytesIO

import gzip

import logging
import requests

from mlarchive2maildir.message import deobfuscate

class MessageIdMaildir(mailbox.Maildir):
    """
    An extension of the mailbox.Maildir class that you can ask
    whether a message with a specific message id already exists
    """
    _message_ids = None

    def __init__(self, dirname, factory=None, create=True):
        super().__init__(dirname, factory, create)

        self._build_msgid_cache()

    def add(self, message):
        super().add(message)
        self._message_ids[message['message-id']] = True

    def _build_msgid_cache(self):
        self._message_ids = dict()
        for key, message in self.items():
            self._message_ids[message['message-id']] = True

    def contains_msgid(self, message_id):
        return message_id in self._message_ids

    def import_mbox_from_url(self, url, headers=None):
        """
        request a mbox by url, and pipes all messages
        into the passed maildir object
        """

        if url.endswith('.txt'):
            suffix = '.txt'
        elif url.endswith('.gz'):
            suffix = '.gz'
        else:
            raise Exception('Invalid file suffix')

        # get archive url stream
        stream = BytesIO(requests.get(url).content)

        if suffix == '.gz':
            stream = gzip.open(stream)

        # open mbox
        with mbox_from_stream(stream) as tmbox:
            for key, message in tmbox.items():
                if message['message-id'] is None:
                    continue

                deobfuscate(message)

                logger = logging.getLogger(__name__)

                if not self.contains_msgid(message['message-id']):
                    logger.debug('imported {}'.format(message['message-id']))

                    if headers:
                        for (header, value) in headers.items():
                            del message[header]
                            message[header] = value

                    self.add(message)
                else:
                    logger.warning('maildir already contains msgid {} ({}), skipping…'.format(message['message-id'],
                                                                                message['subject']))

@contextmanager
def mbox_from_stream(f):
    """
    consumes a file-like object containing a mailbox.
    will yield a mailbox.Mailbox object out of a temporary directory,
    and clean up afterwards
    :param f: the file-like object
    """
    with tempfile.NamedTemporaryFile(mode='w+b') as tmbox_f:
        tmbox_f.write(f.read())
        tmbox_f.flush()

        tmbox = mailbox.mbox(tmbox_f.name)
        yield tmbox

@contextmanager
def locked_messageid_maildir(maildir_path):
    """
    will yield a MessageIdMaildir, which is locked
    :param maildir_path: the path to a mailbox
    """
    maildir = MessageIdMaildir(maildir_path)

    maildir.lock()

    # override the regular add method
    maildir.normal_add = maildir.add

    yield maildir
    maildir.unlock()


from __future__ import annotations

from typing import Any
from typing import Dict
from typing import Set

from sqlalchemy import CHAR
from sqlalchemy import CheckConstraint
from sqlalchemy import Column
from sqlalchemy import event
from sqlalchemy import ForeignKey
from sqlalchemy import Index
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import Numeric
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import Text
from sqlalchemy import text
from sqlalchemy import UniqueConstraint

from ... import autogenerate
from ... import util
from ...autogenerate import api
from ...ddl.base import _fk_spec
from ...migration import MigrationContext
from ...operations import ops
from ...testing import config
from ...testing import eq_
from ...testing.env import clear_staging_env
from ...testing.env import staging_env

names_in_this_test: Set[Any] = set()

@event.listens_for(Table, "after_parent_attach")
def new_table(table, parent):
    names_in_this_test.add(table.name)

def _default_include_object(obj, name, type_, reflected, compare_to):
    if type_ == "table":
        return name in names_in_this_test
    else:
        return True

_default_object_filters: Any = _default_include_object

_default_name_filters: Any = None

class ModelOne:
    __requires__ = ("unique_constraint_reflection",)

    schema: Any = None

    @classmethod
    def _get_db_schema(cls):
        schema = cls.schema

        m = MetaData(schema=schema)

        Table(
            "user",
            m,
            Column("id", Integer, primary_key=True),
            Column("name", String(50)),
            Column("a1", Text),
            Column("pw", String(50)),
            Index("pw_idx", "pw"),
        )

        Table(
            "address",
            m,
            Column("id", Integer, primary_key=True),
            Column("email_address", String(100), nullable=False),
        )

        Table(
            "order",
            m,
            Column("order_id", Integer, primary_key=True),
            Column(
                "amount",
                Numeric(8, 2),
                nullable=False,
                server_default=text("0"),
            ),
            CheckConstraint("amount >= 0", name="ck_order_amount"),
        )

        Table(
            "extra",
            m,
            Column("x", CHAR),
            Column("uid", Integer, ForeignKey("user.id")),
        )

        return m

    @classmethod
    def _get_model_schema(cls):
        schema = cls.schema

        m = MetaData(schema=schema)

        Table(
            "user",
            m,
            Column("id", Integer, primary_key=True),
            Column("name", String(50), nullable=False),
            Column("a1", Text, server_default="x"),
        )

        Table(
            "address",
            m,
            Column("id", Integer, primary_key=True),
            Column("email_address", String(100), nullable=False),
            Column("street", String(50)),
            UniqueConstraint("email_address", name="uq_email"),
        )

        Table(
            "order",
            m,
            Column("order_id", Integer, primary_key=True),
            Column(
                "amount",
                Numeric(10, 2),
                nullable=True,
                server_default=text("0"),
            ),
            Column("user_id", Integer, ForeignKey("user.id")),
            CheckConstraint("amount > -1", name="ck_order_amount"),
        )

        Table(
            "item",
            m,
            Column("id", Integer, primary_key=True),
            Column("description", String(100)),
            Column("order_id", Integer, ForeignKey("order.order_id")),
            CheckConstraint("len(description) > 5"),
        )
        return m

class _ComparesFKs:
    def _assert_fk_diff(
        self,
        diff,
        type_,
        source_table,
        source_columns,
        target_table,
        target_columns,
        name=None,
        conditional_name=None,
        source_schema=None,
        onupdate=None,
        ondelete=None,
        initially=None,
        deferrable=None,
    ):
        # the public API for ForeignKeyConstraint was not very rich
        # in 0.7, 0.8, so here we use the well-known but slightly
        # private API to get at its elements
        (
            fk_source_schema,
            fk_source_table,
            fk_source_columns,
            fk_target_schema,
            fk_target_table,
            fk_target_columns,
            fk_onupdate,
            fk_ondelete,
            fk_deferrable,
            fk_initially,
        ) = _fk_spec(diff[1])

        eq_(diff[0], type_)
        eq_(fk_source_table, source_table)
        eq_(fk_source_columns, source_columns)
        eq_(fk_target_table, target_table)
        eq_(fk_source_schema, source_schema)
        eq_(fk_onupdate, onupdate)
        eq_(fk_ondelete, ondelete)
        eq_(fk_initially, initially)
        eq_(fk_deferrable, deferrable)

        eq_([elem.column.name for elem in diff[1].elements], target_columns)
        if conditional_name is not None:
            if conditional_name == "servergenerated":
                fks = inspect(self.bind).get_foreign_keys(source_table)
                server_fk_name = fks[0]["name"]
                eq_(diff[1].name, server_fk_name)
            else:
                eq_(diff[1].name, conditional_name)
        else:
            eq_(diff[1].name, name)

class AutogenTest(_ComparesFKs):
    def _flatten_diffs(self, diffs):
        for d in diffs:
            if isinstance(d, list):
                yield from self._flatten_diffs(d)
            else:
                yield d

    @classmethod
    def _get_bind(cls):
        return config.db

    configure_opts: Dict[Any, Any] = {}

    @classmethod
    def setup_class(cls):
        staging_env()
        cls.bind = cls._get_bind()
        cls.m1 = cls._get_db_schema()
        cls.m1.create_all(cls.bind)
        cls.m2 = cls._get_model_schema()

    @classmethod
    def teardown_class(cls):
        cls.m1.drop_all(cls.bind)
        clear_staging_env()

    def setUp(self):
        self.conn = conn = self.bind.connect()
        ctx_opts = {
            "compare_type": True,
            "compare_server_default": True,
            "target_metadata": self.m2,
            "upgrade_token": "upgrades",
            "downgrade_token": "downgrades",
            "alembic_module_prefix": "op.",
            "sqlalchemy_module_prefix": "sa.",
            "include_object": _default_object_filters,
            "include_name": _default_name_filters,
        }
        if self.configure_opts:
            ctx_opts.update(self.configure_opts)
        self.context = context = MigrationContext.configure(
            connection=conn, opts=ctx_opts
        )

        self.autogen_context = api.AutogenContext(context, self.m2)

    def tearDown(self):
        self.conn.close()

    def _update_context(
        self, object_filters=None, name_filters=None, include_schemas=None
    ):
        if include_schemas is not None:
            self.autogen_context.opts["include_schemas"] = include_schemas
        if object_filters is not None:
            self.autogen_context._object_filters = [object_filters]
        if name_filters is not None:
            self.autogen_context._name_filters = [name_filters]
        return self.autogen_context

class AutogenFixtureTest(_ComparesFKs):
    def _fixture(
        self,
        m1,
        m2,
        include_schemas=False,
        opts=None,
        object_filters=_default_object_filters,
        name_filters=_default_name_filters,
        return_ops=False,
        max_identifier_length=None,
    ):
        if max_identifier_length:
            dialect = self.bind.dialect
            existing_length = dialect.max_identifier_length
            dialect.max_identifier_length = (
                dialect._user_defined_max_identifier_length
            ) = max_identifier_length
        try:
            self._alembic_metadata, model_metadata = m1, m2
            for m in util.to_list(self._alembic_metadata):
                m.create_all(self.bind)

            with self.bind.connect() as conn:
                ctx_opts = {
                    "compare_type": True,
                    "compare_server_default": True,
                    "target_metadata": model_metadata,
                    "upgrade_token": "upgrades",
                    "downgrade_token": "downgrades",
                    "alembic_module_prefix": "op.",
                    "sqlalchemy_module_prefix": "sa.",
                    "include_object": object_filters,
                    "include_name": name_filters,
                    "include_schemas": include_schemas,
                }
                if opts:
                    ctx_opts.update(opts)
                self.context = context = MigrationContext.configure(
                    connection=conn, opts=ctx_opts
                )

                autogen_context = api.AutogenContext(context, model_metadata)
                uo = ops.UpgradeOps(ops=[])
                autogenerate._produce_net_changes(autogen_context, uo)

                if return_ops:
                    return uo
                else:
                    return uo.as_diffs()
        finally:
            if max_identifier_length:
                dialect = self.bind.dialect
                dialect.max_identifier_length = (
                    dialect._user_defined_max_identifier_length
                ) = existing_length

    def setUp(self):
        staging_env()
        self.bind = config.db

    def tearDown(self):
        if hasattr(self, "_alembic_metadata"):
            for m in util.to_list(self._alembic_metadata):
                m.drop_all(self.bind)
        clear_staging_env()


import matplotlib.pyplot as plt
import numpy as np

x = np.array([[1,0],[0,1],[0,-1],[-1,0],[0,2],[0,-2],[-2,0]])
y = np.array([-1,-1,-1,1,1,1,1])

def z(X):
	x1, x2 = X[0], X[1]
	t1 = x2**2 - 2*x1 - 2
	t2 = x1**2 - 2*x2 - 1
	return [t1,t2]

T = np.array([z(i) for i in x])
for i in T:
	print(i)

from sklearn import svm
clf = svm.SVC(kernel = 'linear', C = 1000)
clf.fit(T,y)

for i, j in enumerate(T):
    t1, t2 = j[0], j[1]
    if y[i] == 1:
        plt.scatter(t1, t2, c='r', edgecolors='k')
        plt.annotate('1', (t1,t2),  fontsize=16)
    elif y[i] == -1:
        plt.scatter(t1, t2, c='b', edgecolors='k')
        plt.annotate('-1', (t1,t2),  fontsize=16)

ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.decision_function(xy).reshape(XX.shape)

# plot decision boundary and margins
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
           linestyles=['--', '-', '--'])

# plot support vectors
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
           linewidth=1, facecolors='none')

plt.savefig('1.png')
plt.show()


import tensorflow as tf
import numpy as np
import gym
from gym.wrappers import Monitor
import random
import os
import time

def Policy(action_cnn,state,sess,epsilon, num_actions=4):
	preds = sess.run(action_cnn.preds,{action_cnn.input:state[np.newaxis,:]})
	p = np.ones(num_actions)*epsilon/num_actions
	greedy_action = np.argmax(preds)
	p[greedy_action] += (1 - epsilon)
	return p

def DQL(env,sess,action_cnn,target_cnn,num_episodes,epsilons,discount_factor,replay_mem_size,batch_size,C,record,decay_rate,algo,make_video = True):

	saver = tf.train.Saver()
	# Load a previous checkpoint if we find one
	ckpt = tf.train.get_checkpoint_state(os.path.dirname('Breakoutcheckpoints/'))
	if ckpt and ckpt.model_checkpoint_path:
		print("Loading Checkpoint")
		saver.restore(sess, ckpt.model_checkpoint_path)

	decay_count = tf.train.global_step(sess,action_cnn.global_step)
	print(decay_count)
	#Initializing Replay memory
	D = []
	#preprocess = Preprocess()
	state = env.reset()
	state = preprocess(state,sess)
	state = np.stack([state] * 4, axis=2)
	print("populating replay memory")
	start = time.time()
	for i in range(50000):
		action_probs = Policy(action_cnn,state,sess,epsilons[min(decay_count,decay_rate-1)],env.action_space.n)
		possible_actions = np.arange(env.action_space.n)
		action = np.random.choice(possible_actions, p = action_probs)
		new_state,reward,done, _ = env.step(action)

		new_state = preprocess(new_state,sess)
		new_state = np.append(state[:,:,1:],new_state[:,:,np.newaxis],axis = 2)
		D.append((state,action,reward,new_state,done))
		if done:
			state = env.reset()
			state = preprocess(state,sess)
			state = np.stack([state] * 4, axis=2)
		else:
			state = new_state

	print("Been There Done That")
	print(time.time() - start)

	if make_video:
		env = Monitor(env, directory='Breakoutvideos', video_callable=lambda count: count % record == 0, resume=True)

	losses = []
	running_mean = 0
	for i in range(num_episodes):
		saver.save(sess, 'Breakoutcheckpoints/DQN')
		state = env.reset()
		state = preprocess(state,sess)
		state = np.stack([state] * 4, axis=2)
		print("episode: ",i)
		ep_reward = 0
		loss = None
		j =0
		while True:
			print("\rstep {}".format(j),end = "")
			sys.stdout.flush()
			j+=1

			if (decay_count)%C == 0:
				copy_op = copy_cnn_params(action_cnn,target_cnn)
				sess.run(copy_op)
				print("Copied parameters to target network")

			action_probs = Policy(action_cnn,state,sess,epsilons[min(decay_count,decay_rate-1)],env.action_space.n)
			possible_actions = np.arange(env.action_space.n)
			action = np.random.choice(possible_actions, p = action_probs)
			new_state,reward,done,_ = env.step(action)
			new_state = preprocess(new_state,sess)
			new_state = np.append(state[:,:,1:],new_state[:,:,np.newaxis],axis = 2)

			ep_reward += reward
			if(len(D)==replay_mem_size):
				D.pop(0)
			D.append((state,action,reward,new_state,done))
			batch = random.sample(D,batch_size)
			states, actions, rewards, new_states, dones = zip(*batch)
			states = np.array(states)
			actions = np.array(actions)
			rewards = np.array(rewards)
			new_states = np.array(new_states)
			rewards = np.array(rewards)
			flags = np.invert(dones)

			#Q Learning
			if algo == 'DQ':
				y = rewards + flags*discount_factor*np.max(sess.run(target_cnn.preds,
																{target_cnn.input:new_states}),axis=1)
			#Double Q Learning
			elif algo == 'DDQ':
				greedy_actions = np.argmax(sess.run(action_cnn.preds,{action_cnn.input:new_states}),axis = 1)
				y = rewards + flags*discount_factor*((sess.run(target_cnn.preds,{target_cnn.input:new_states}))[np.arange(BATCH_SIZE),greedy_actions])

			loss,_ = sess.run([action_cnn.loss,action_cnn.opti],{action_cnn.input:states,action_cnn.output:y,
																 action_cnn.actions:actions})

			state = new_state
			decay_count += 1
			if done:
				break
		print("Reward: ",ep_reward)
		running_mean = 0.9*running_mean + 0.1*ep_reward
		print("Loss: ",loss)


import detectron2
from detectron2.utils.logger import setup_logger
import numpy as np
import os, json, cv2, random
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.structures import BoxMode
from detectron2.utils.visualizer import Visualizer

setup_logger()

# if your dataset is in COCO format, this cell can be replaced by the following three lines:
# from detectron2.data.datasets import register_coco_instances
# register_coco_instances("my_dataset_train", {}, "json_annotation_train.json", "path/to/image/dir")
# register_coco_instances("my_dataset_val", {}, "json_annotation_val.json", "path/to/image/dir")

# vidor
vidor_obj_to_idx = {
	'adult': 0, 'aircraft': 1, 'antelope': 2, 'baby': 3, 'baby_seat': 4,
	'baby_walker': 5, 'backpack': 6, 'ball/sports_ball': 7, 'bat': 8, 'bear': 9,
	'bench': 10, 'bicycle': 11, 'bird': 12, 'bottle': 13, 'bread': 14,
	'bus/truck': 15, 'cake': 16, 'camel': 17, 'camera': 18, 'car': 19,
	'cat': 20, 'cattle/cow': 21, 'cellphone': 22, 'chair': 23, 'chicken': 24,
	'child': 25, 'crab': 26, 'crocodile': 27, 'cup': 28, 'dish': 29,
	'dog': 30, 'duck': 31, 'electric_fan': 32, 'elephant': 33, 'faucet': 34,
	'fish': 35, 'frisbee': 36, 'fruits': 37, 'guitar': 38, 'hamster/rat': 39,
	'handbag': 40, 'horse': 41, 'kangaroo': 42, 'laptop': 43, 'leopard': 44,
	'lion': 45, 'microwave': 46, 'motorcycle': 47, 'oven': 48, 'panda': 49,
	'penguin': 50, 'piano': 51, 'pig': 52, 'rabbit': 53, 'racket': 54,
	'refrigerator': 55, 'scooter': 56, 'screen/monitor': 57, 'sheep/goat': 58, 'sink': 59,
	'skateboard': 60, 'ski': 61, 'snake': 62, 'snowboard': 63, 'sofa': 64,
	'squirrel': 65, 'stingray': 66, 'stool': 67, 'stop_sign': 68, 'suitcase': 69,
	'surfboard': 70, 'table': 71, 'tiger': 72, 'toilet': 73, 'toy': 74,
	'traffic_light': 75, 'train': 76, 'turtle': 77, 'vegetables': 78, 'watercraft': 79
}

def vidor_to_coco_format(anno_dir, split):
	dataset_dicts = []
	for dirs in os.listdir(os.path.join(anno_dir, 'annotation', split)):
		for files in os.listdir(os.path.join(anno_dir, 'annotation', split, dirs)):
			with open(os.path.join(anno_dir, 'annotation', split, dirs, files)) as f:
				anno = json.load(f)

			tid_to_obj = {
				obj_tid['tid']:obj_tid['category'] for obj_tid in anno['subject/objects']
			}

			record = {}
			record['height'] = anno['height']
			record['width'] = anno['width']

			for bbox_idx, bboxes in enumerate(anno['trajectories']):
				record['file_name'] = os.path.join(
					anno_dir, 'image', anno['video_id'], '{0:0=5d}.jpg'.format(bbox_idx)
				)
				record['image_id'] = '{0:0=5d}'.format(bbox_idx)
				objs = []
				for bbox in bboxes:
					obj = {
						'bbox': [bbox['bbox']['xmin'],
								 bbox['bbox']['ymin'],
								 bbox['bbox']['xmax'],
								 bbox['bbox']['ymax']],
						'bbox_mode': BoxMode.XYXY_ABS,
						'category_id': vidor_obj_to_idx[tid_to_obj[bbox['tid']]]
					}
					objs.append(obj)

				record['annotations'] = objs
				dataset_dicts.append(record)

	return dataset_dicts

if __name__=='__main__':
	# vidor_to_coco_format
	anno_dir = "/home/t2_u1/data/vidor/"
	for d in ["training", "validation"]:
		DatasetCatalog.register("vidor_" + d, lambda d=d:vidor_to_coco_format(anno_dir, d))
		MetadataCatalog.get("vidor_" + d).set(
			thing_classes=[
				'adult', 'aircraft', 'antelope', 'baby', 'baby_seat',
				'baby_walker', 'backpack', 'ball/sports_ball', 'bat', 'bear',
				'bench', 'bicycle', 'bird', 'bottle', 'bread',
				'bus/truck', 'cake', 'camel', 'camera', 'car',
				'cat', 'cattle/cow', 'cellphone', 'chair', 'chicken',
				'child', 'crab', 'crocodile', 'cup', 'dish',
				'dog', 'duck', 'electric_fan', 'elephant', 'faucet',
				'fish', 'frisbee', 'fruits', 'guitar', 'hamster/rat',
				'handbag', 'horse', 'kangaroo', 'laptop', 'leopard',
				'lion', 'microwave', 'motorcycle', 'oven', 'panda',
				'penguin', 'piano', 'pig', 'rabbit', 'racket',
				'refrigerator', 'scooter', 'screen/monitor', 'sheep/goat', 'sink',
				'skateboard', 'ski', 'snake', 'snowboard', 'sofa',
				'squirrel', 'stingray', 'stool', 'stop_sign', 'suitcase',
				'surfboard', 'table', 'tiger', 'toilet', 'toy',
				'traffic_light', 'train', 'turtle', 'vegetables', 'watercraft'
			]
		)
	vidor_metadata = MetadataCatalog.get("vidor_training")
	dataset_dicts = vidor_to_coco_format(anno_dir, "training")
	with open("./vidor_coco_format.json", "w") as f:
		j = json.dump(dataset_dicts, f)

	# with open("./vidor_coco_format.json", "r") as f:
	# 	dataset_dicts = json.load(f)

	num_images_to_show = 3
	for d in random.sample(dataset_dicts, num_images_to_show):
		img = cv2.imread(d["file_name"])
		visualizer = Visualizer(img[:, :, ::-1], metadata=vidor_metadata, scale=0.5)
		out = visualizer.draw_dataset_dict(d)
		cv2.imshow('sample', out.get_image()[:, :, ::-1])
		cv2.waitKey(0)
		cv2.destroyAllWindows()


#!/usr/bin/env python

import os, sys
import json
import messytables
import subprocess
from dgitcore.helper import cd
from dgitcore.plugins.instrumentation import InstrumentationBase
from dgitcore.config import get_config

def run(cmd):
    output = subprocess.check_output(cmd,
                                     stderr=subprocess.STDOUT,
                                     shell=True)
    output = output.decode('utf-8')
    output = output.strip()
    return output

def repo_origin(filename, what=['Push  URL']):

    with cd(os.path.dirname(filename)):
        cmd = "git remote show origin"
        output = run(cmd)
        #* remote origin
        #Fetch URL: git@github.com:jaredpar/VsVim.git
        #Push  URL: git@github.com:jaredpar/VsVim.git
        #HEAD branch: master
        #Remote branches:

        response = {}
        output = output.split("\n")
        output = output[1:]
        for o in output:
            for w in what:
                if w in o:
                    response[w] = o[o.index(":")+1:]

    return response

def repo_remote_url(filename):

    with cd(os.path.dirname(filename)):
        cmd = "git config --get remote.origin.url"
        output = run(cmd)
        return {'remote.origin.url': output.strip()}

def executable_commit(filename,
                      what=['commit', 'username', 'useremail', 'date']):
    mapping = {
        'commit': '%H',
        'username': '%cn',
        'useremail': '%ce',
        'date': '%cd'
    }

    missing = [mapping[w] for w in what if w not in mapping]
    if len(missing) > 0:
        print("Cannot gather commit attributes of executable", missing)
        raise Exception("Invalid specification")

    codes = ",".join([mapping[w] for w in what if w in mapping])

    with cd(os.path.dirname(filename)):
        cmd = 'git log -n 1  --date=iso --pretty="%s" -- %s ' %(codes, filename)
        output = run(cmd)
        output = output.strip()
        output = output.split(",")
        return {what[i]: output[i] for i in range(len(what))}

    return {}

def executable_repopath(filename):

    with cd(os.path.dirname(filename)):
        cmd = 'git rev-parse --show-prefix'
        output = run(cmd)
        output = output.strip()
        return {
            'path': os.path.join(output, os.path.basename(filename))
        }

def executable_filetype(filename):

    with cd(os.path.dirname(filename)):
        cmd = '/usr/bin/file ' + filename
        output = run(cmd)
        output = output.strip()
        output = output[output.index(":")+1:]
        return {
            'filetype': output
        }

def get_metadata(args):
    filename = args[0]
    metadata = {'cmd': ' '.join(args) }
    metadata.update(repo_remote_url(filename))
    metadata.update(executable_commit(filename))
    metadata.update(executable_repopath(filename))
    metadata.update(executable_filetype(filename))
    return metadata

class ExecutableInstrumentation(InstrumentationBase):
    """Instrumentation to extract executable related summaries such as the git commit, nature of executable, parameters etc.
    """

    def __init__(self):
        super(ExecutableInstrumentation, self).__init__('executable',
                                                        'v0',
                                                        "Executable analysis")

    def update(self, config):
        if 'executables' in config:
            for i in range(len(config['executables'])):
                args = config['executable'][i]['args']
                metadata = get_metdata(args)
                config['executable'][i].update(metadata)

        return config

def setup(mgr):

    obj = ExecutableInstrumentation()
    mgr.register('instrumentation', obj)

if __name__ == "__main__":

    viz = '/home/pingali/analytics/politics/bin/mumbai-visualize.py'
    response = run_executable([viz])
    print(json.dumps(response, indent=4))


import heapq
from math import inf
from typing import List

class Solution:
    def minimumTime(self, grid: List[List[int]]) -> int:

        m, n = len(grid), len(grid[0])
        if grid[0][1] > 1 and grid[1][0] > 1:
            return -1

        dis = [[inf] * n for _ in range(m)]
        dis[0][0] = 0
        h = [(0, 0, 0)]
        while True:
            d, i, j = heapq.heappop(h)
            if i == m - 1 and j == n - 1:
                return d
            for x, y in (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1):
                if 0 <= x < m and 0 <= y < n:
                    nd = max(d + 1, grid[x][y])
                    nd += (nd - x - y) % 2
                    if nd < dis[x][y]:
                        dis[x][y] = nd
                        heapq.heappush(h, (nd, x, y))

if __name__  == '__main__':
    grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]
    grid = [[0,2,4],[3,2,1],[1,0,4]]
    rtn = Solution().minimumTime(grid)
    print(rtn)

python/2577. 在网格图中访问一个格子的最少时间.py

2577. 在网格图中访问一个格子的最少时间.py


import cv2 as cv
import numpy as np

im = cv.imread('car.png', 0)

kernel = np.ones((5, 5), np.int16) * 1 / 25

out = cv.filter2D(im, -1, kernel)
print(out)
for i in range(len(out)):
    for j in range(len(out[0])):
        if out[i][j] < 128:
            out[i][j] = 255
        else:
            out[i][j] = 0

cv.imshow('output', out)

cv.waitKey(0)
cv.destroyAllWindows()


import requests
import configparser
import json

cfg = configparser.ConfigParser()
cfg.read("config.ini")
_api_url = str(cfg["default"]["api_url"])

def test_post_scm_repo():
    response = requests.post(f"{_api_url}/scm-repos", json={"url": "abc"})

    assert response.status_code == 201

    body = response.json()

    assert body["url"] == "abc"
    assert body["id"] > 0


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name='ISY994v5',
    version='0.9.7',
    description='ISY99 Controller Rest and Websocket client v5 firmware',
    author='Michael Cumming',
    author_email='mike@4831.com',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='https://github.com/mjcumming/ISY994v5',
    keywords=['INSTEON', 'ISY994', 'ISY', 'Universal Devices'],
    packages=setuptools.find_packages(),
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=[
        'aiohttp',
    ]
)


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

from extensions import ui_actions
from utilities.Base import Base

@allure.step("Business Flow: Login")
def login(email, password):
    ui_actions.update_text(Base.LOGIN_PAGE.textbox_user_email, email)
    ui_actions.update_text(Base.LOGIN_PAGE.textbox_password, password)
    ui_actions.click(Base.LOGIN_PAGE.button_login)

@allure.step("Business Flow: Search By Email")
def search_for_email(email):
    ui_actions.update_text(Base.CUSTOMER.textbox_search_email, email)
    ui_actions.click(Base.CUSTOMER.btn_search)

@allure.step("Business Flow: Search By Name")
def search_for_name(first_name, last_name):
    ui_actions.update_text(Base.CUSTOMER.textbox_search_firstName, first_name)
    ui_actions.update_text(Base.CUSTOMER.textbox_search_lastName, last_name)
    ui_actions.click(Base.CUSTOMER.btn_search)

@allure.step("Business Flow: Search For Name")
def search_name_on_table(full_name):
    flag = False
    for r in range(1, Base.CUSTOMER.get_row_number + 1):
        table = Base.CUSTOMER.table
        name = ui_actions.get_element_text(
            table.find_element(
                By.XPATH,
                "//table[@id='customers-grid']/tbody/tr[" + str(r) + "]/td[3]",
            )
        )
        if name == full_name:
            flag = True
            break
    return flag

@allure.step("Business Flow: Navigate To The Customer Table Page")
def goto_customer_table():
    ui_actions.click(Base.MEGA_MENU.btn_customers_prm)
    ui_actions.click(Base.MEGA_MENU.btn_customers_sub)

@allure.step("Business Flow: Fill In Customer Form")
def fill_customer_form(customer):
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_email, customer['email'])
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_password, customer['password'])
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_first_name, customer['first_name'])
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_last_name, customer['last_name'])
    if customer['gender'] == "male":
        ui_actions.click(Base.CREATE_CUSTOMER.textbox_gender_male)
    elif customer['gender'] == "female":
        ui_actions.click(Base.CREATE_CUSTOMER.textbox_gender_female)
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_dateOfBirth, customer['date_birth'])
    ui_actions.update_text(Base.CREATE_CUSTOMER.textbox_company, customer['company'])
    if customer['tax_exempt']:
        ui_actions.click(Base.CREATE_CUSTOMER.checkbox_taxExempt)
    ui_actions.click(Base.CREATE_CUSTOMER.div_newsletter)
    if customer['newsletter'] == "Test store 2":
        ui_actions.click(Base.CREATE_CUSTOMER.li_newsletter_test_store_2_opt)
    ui_actions.click(Base.CREATE_CUSTOMER.div_customer_role)
    if customer['role'] == "Administrators":
        ui_actions.click(Base.CREATE_CUSTOMER.li_Administrators_role_opt)
    ui_actions.select_element_by_text(Base.CREATE_CUSTOMER.select_vendor_id, customer['vendor_id'])
    if not customer['active']:
        ui_actions.click(Base.CREATE_CUSTOMER.checkbox_active)
    ui_actions.update_text(Base.CREATE_CUSTOMER.textarea_admin_comment, customer['comment'])


from flask import jsonify, make_response
import src.flaskr.controllers.turn_logic as turn_logic
from src.flaskr.controllers.uuid_supplier import UuidSupplier
import src.flaskr.controllers.winner_logic as winner_logic
from src.flaskr.models.game_model import Game, GameSchema, MoveSchema
from src.flaskr.persistence.repositories.game_repository_api import GameRepositoryApi

GAME_SCHEMA = GameSchema()
MOVE_SCHEMA = MoveSchema()

def games__get__list(request):
    # This is how you get the value of the query param
    # with key 'username'. Returns None if no value passed
    username = request.args.get('username')

    repo = GameRepositoryApi()
    games = repo.select_all_games()

    if username == None:
        return make_response([g.id for g in games], 200)

    filtered_games = [g for g in games if g.player_a == username or g.player_b == username]

    return make_response([g.id for g in filtered_games], 200)

def games__post__create(request):
    # creates a new game object based on the data in the request
    body = request.json
    id = UuidSupplier().get()
    new_game = Game(id, body['opponent'], body['challenger'], [])
    repo = GameRepositoryApi()
    repo.insert(new_game)
    repo.commit()

    return make_response(jsonify(id), 201)

def some_game__get__info(request, game_id):
    repo = GameRepositoryApi()
    game = repo.select_game(game_id)

    if (game == None):
        return make_response('No game with this ID was found on the server.', 404)

    return make_response(GAME_SCHEMA.dump(game), 200)

def some_game__get__turn(request, game_id):
    repo = GameRepositoryApi()
    game = repo.select_game(game_id)

    if game is None:
        return make_response('No game with ID ' + game_id, 404)

    turn = turn_logic.derive_turn(game)
    return make_response(jsonify(turn), 200)

def some_game__get__winner(request, game_id):
    repo = GameRepositoryApi()
    game = repo.select_game(game_id)

    if game is None:
        msg = 'No game with this ID was found on the server.'
        return make_response(msg, 404)

    status = winner_logic.find_winner(game)

    return make_response(jsonify(status), 200)

def some_game__post__move(request, game_id):
    # gets the request body and deserializes it into a Move model object
    body = request.json
    move = MOVE_SCHEMA.load(body)

    if not (move.x in [0,1,2] and move.y in [0,1,2]):
        msg = 'Invalid move coordinates.'
        return make_response(msg, 400)

    repo = GameRepositoryApi()
    game = repo.select_game(game_id)

    if game is None:
        msg = 'No game with this ID was found on the server.'
        return make_response(msg, 404)

    turn = turn_logic.derive_turn(game)

    is_occupied = len([m for m in game.moves if m.x == move.x and m.y == move.y]) > 0
    is_game_decided = winner_logic.find_winner(game) != 'undecided'
    is_invalid_move = turn != move.occupier or is_occupied or is_game_decided
    if (is_invalid_move):
        msg = 'Illegal move. This player does not have the turn or this field is already occupied.'
        return make_response(msg, 403)

    repo.insert_move(move, game_id)
    repo.commit()

    updated_game = repo.select_game(game_id)

    return make_response(GAME_SCHEMA.dump(updated_game), 201)


from flask import Blueprint, render_template, request, current_app,redirect
from flask import url_for, flash
from jobweb.decorators import admin_required
from jobweb.models import db,User
from jobweb.forms import UserRegisterForm, CompanyRegisterForm,UserEditForm, CompanyEditForm

admin = Blueprint('admin', __name__, url_prefix='/admin')

@admin.route('/')
@admin_required
def admin_index():
  return render_template('admin/index.html')

@admin.route('/users')
@admin_required
def users():
  page = request.args.get('page', default=1, type=int)
  pagination = User.query.paginate(
    page=page,
    per_page=current_app.config['ADMIN_PER_PAGE'],
    error_out = False
  )
  return render_template('admin/users.html', pagination=pagination)

@admin.route('/users/create_user', methods=['GET', 'POST'])
@admin_required
def create_user():
  form = UserRegisterForm()
  if form.is_submitted():
    form.create_user()
    flash('create user success', 'success') #添加求职者成功
    return redirect(url_for('admin.users'))
  return render_template('admin/create_user.html', form=form)

@admin.route('/users/create_company', methods=["GET","POST"])
@admin_required
def create_company():
  form = CompanyRegisterForm()
  if form.is_submitted():
    form.create_companyProfile()
    flash('create company success','success') #添加企业成功
    return redirect(url_for('admin.users'))
  return render_template('admin/create_company.html', form=form)

@admin.route('/users/<int:user_id>/edit', methods=['GET', 'POST'])
@admin_required
def edit_user(user_id):
    user = User.query.get_or_404(user_id)
    if user.is_company:
        form = CompanyEditForm(obj=user)
    else:
        form = UserEditForm(obj=user)
    if form.validate_on_submit():
        form.update(user)
        flash('edit success', 'success')
        return redirect(url_for('admin.users'))
    if user.is_company:
        form.website.data = user.companydetail.website
        form.desc.data = user.companydetail.desc
    return render_template('admin/edit_user.html', form=form, user=user)

@admin.route('/users/<int:user_id>/disable', methods=['GET','POST'])
@admin_required
def disable_user(user_id):
    user = User.query.get_or_404(user_id)
    if user.is_disable:
        user.is_disable = False
        flash('enable user', 'success')
    else:
        user.is_disable = True
        flash('disable user', 'success')
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('admin.users'))