# excel2xml.py

from openpyxl import load_workbook
from yattag import Doc, indent
from datetime import date
import os

po = str(input('Enter PO number: '))
lines = int(input('Enter last line number used in the file: '))

w = po+'.xlsx'
x = lines
date = date.today()

## Checks for Null or "" cells
## and replace all empty cells with the string None.
def clean_WB(w, x):
    wb = load_workbook(w)
    ws = wb.worksheets[0]
    for row in ws.iter_rows(min_row=2, max_row= x, min_col=1, max_col=14):
        for cell in row:
##            print(cell.value) ## Use to verify output when necessary
            if cell.value is None:
                cell.value = "None"
    wb.save('clean_wb.xlsx')

def convert2xml(w, x, date):
    # Loads Excel workbook
    wb = load_workbook(w)

    # Create sheet object
    ws = wb.worksheets[0]
    wblist = []

    ### Returning returns a triplet
    doc, tag, text = Doc().tagtext()

    xml_header = '<?xml version= "1.0" encoding="UTF-8"?>'
    xml_schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>'

    # Appends the string to the document
    doc.asis(xml_header)
    doc.asis(xml_schema)

    with tag('PO'):
        # Iterating rows for getting the values of each row
        # Use ws.max_row for all rows
        for row in ws.iter_rows(min_row=2, max_row=2, min_col=1, max_col=5):
            row = [cell.value for cell in row]
            wblist.append(row)
            with tag("PO_Information"):
                with tag("PO_Number"):
                    text(row[0])
                with tag("Site"):
                    text(row[1])
                with tag("PO_Description"):
                    text(row[2])
                with tag("Status"):
                    text(row[3])
                with tag("Revision"):
                    text(row[4])
        for row in ws.iter_rows(min_row=3, max_row= x, min_col=1, max_col=14):
            row = [cell.value for cell in row]
            wblist.append(row)
            with tag("PO_Line"):
                with tag("Line"):
                    text(row[5])
                with tag("Line_Type"):
                    text(row[6])
                with tag("Item"):
                    text(row[7])
                with tag("Item_Description"):
                    text(row[8])
                with tag("Quantity"):
                    text(row[9])
                with tag("Order_Unit"):
                    text(row[10])
                with tag("Manufacturer"):
                    text(row[11])
                with tag("Model_Number"):
                    text(row[12])
                with tag("GL_Account"):
                    text(row[13])

    result = indent(
        doc.getvalue(),
        indentation= '  ',
        indent_text=False
    )
    ##print(result)
    with open(f"fwn_{po}_{date}.xml", "w") as f:
        f.write(result)

def deleteFile(w):
    file = w
    location = 'C:\Program Files\Python39\projects\excel_to_xml'
    path = os.path.join(location, file)
    os.remove(path)
    print(f'{file} has been removed successfully')

clean_WB(w, x)
convert2xml('clean_wb.xlsx', x, date)
deleteFile(w)


"""
2. Представлен список чисел. Необходимо вывести элементы исходного списка, значения которых больше предыдущего элемента.
Подсказка: элементы, удовлетворяющие условию, оформить в виде списка. Для его формирования используйте генератор.
"""

my_list = [300, 2, 12, 44, 1, 1, 4, 10, 7, 1, 78, 123, 55]
my_new_list = [el for el in my_list if el > my_list[my_list.index(el) - 1]]
print(f'Исходный список: {my_list}')
print(f'Новый список: {my_new_list}')

"""
3. Для чисел в пределах от 20 до 240 найти числа, кратные 20 или 21. Решите задание в одну строку.
Подсказка: используйте функцию range() и генератор.
"""

print(f'Числа от 20 до 240, кратные 20 или 21: {[el for el in range(20, 241) if el % 20 == 0 or el % 21 == 0]}')

"""
4. Представлен список чисел. Определите элементы списка, не имеющие повторений.
Сформируйте итоговый массив чисел, соответствующих требованию.
Элементы выведите в порядке их следования в исходном списке. Для выполнения задания обязательно используйте генератор.
"""

my_list = [2, 2, 2, 7, 23, 1, 44, 44, 3, 2, 10, 7, 4, 11]
my_new_list = [el for el in my_list if my_list.count(el) == 1]
print(my_new_list)

""""
5. Реализовать формирование списка, используя функцию range() и возможности генератора.
В список должны войти чётные числа от 100 до 1000 (включая границы).
Нужно получить результат вычисления произведения всех элементов списка.
"""

from functools import reduce

def my_func(prev_el, el):
    return prev_el * el

my_list = [el for el in range(100, 1001) if el % 2 == 0]
print(reduce(my_func, my_list))

"""
6. Реализовать два небольших скрипта:
- итератор, генерирующий целые числа, начиная с указанного;
- итератор, повторяющий элементы некоторого списка, определённого заранее.
Подсказка: используйте функцию count() и cycle() модуля itertools.
Обратите внимание, что создаваемый цикл не должен быть бесконечным. Предусмотрите условие его завершения.
Например, в первом задании выводим целые числа, начиная с 3. При достижении числа 10 — завершаем цикл.
Вторым пунктом необходимо предусмотреть условие, при котором повторение элементов списка прекратится.
"""

from itertools import count

for el in count(3):
    if el > 17:
        break
    else:
        print(el)

from itertools import cycle

с = 0
for el in cycle("PYTHON"):
    if с > 11:
        break
    print(el)
    с += 1

"""
7. Реализовать генератор с помощью функции с ключевым словом yield, создающим очередное значение.
При вызове функции должен создаваться объект-генератор. Функция вызывается следующим образом: for el in fact(n).
Она отвечает за получение факториала числа. В цикле нужно выводить только первые n чисел, начиная с 1! и до n!.
"""

from itertools import count
from math import factorial

def fact():
    for el in count(1):
        yield factorial(el)

my_factorial = fact()
x = 0
for i in my_factorial:
    if x < 15:
        print(i)
        x += 1
    else:
        break


import tensorflow as tf
import numpy as np

from tensorflow.keras.models import load_model

class EvaluateModel:
    def __init__(self, model, model_path, dataset, batch_size=1):
        self.model = model(dataset(batch_size))
        self.model_path = model_path
        self.dataset = dataset(batch_size)

    def load_model(self):
        model = self.model.build()
        model.load_weights(self.model_path)
        model.summary()
        return model

    def evaluate(self):

        (test_ds) = self.dataset.get_and_process()

        model = self.load_model()
        model.compile(
            optimizer="adam",
            loss="sparse_categorical_crossentropy",
            metrics=["accuracy"],
        )

        model.evaluate(test_ds)
        #

        # classifications =[[],[],[]]
        #
        # count=0
        # for image_batch, labels_batch in test_ds:
        #
        #     model_output = model(image_batch)
        #     label_output = tf.argmax(model_output, 1).numpy()[0]
        #     label = labels_batch.numpy()[0]
        #
        #     if label_output == label:
        #         classifications[label].append(1)
        #     else:
        #         classifications[label].append(0)
        #
        #     # count+=1
        #     # if count == 20:
        #     #     break
        #
        # print(np.mean(classifications[0]))
        # print(np.mean(classifications[1]))
        # print(np.mean(classifications[2]))

class EvaluateStandardized:
    def __init__(self, dataset, batch_size=1):
        self.dataset = dataset(batch_size)

    def evaluate(self):

        (train_ds, val_ds) = self.dataset.get_and_process()
        count = 0
        for image, labels_batch in train_ds:
            mean = tf.math.reduce_mean(image)
            std = tf.math.reduce_std(image)
            print(mean)
            print(std)
            print(image)
            break
            # count+=1
            # if count == 2:
            # break


# coding: utf-8

# Standard Python libraries
from pathlib import Path
from copy import deepcopy

# http://www.numpy.org/
import numpy as np

# https://pandas.pydata.org/
import pandas as pd

import plotly.graph_objects as go

import matplotlib.pyplot as plt

import atomman.unitconvert as uc

# Local imports
from ..thermo import AnalyzeQHA

def phonon(self,
           upload: bool = True,
           runall: bool = False):
    """
    Main function for processing phonon calculations as used
    for building the content hosted on the NIST Interatomic Potentials
    Repository.

    Processing steps:

    1. calculation_phonon records are retrieved from the database.
    2. Basic plots are extracted from the record tars.
    3. Plots and csv files for thermo data are generated for each composition.

    Parameters
    ----------
    upload : bool, optional
        If True (default) then the new/modified PotentialProperties records
        will be uploaded to the database automatically.
    runall : bool, optional
        If True, all plots and tables will be regenerated.  If False, only new
        ones are created.  Default value is False.
    """
    # Class attributes
    database = self.database
    getkwargs = deepcopy(self.getkwargs)
    outputpath = self.outputpath
    props = self.props
    prop_df = self.prop_df()

    # Parse props based on getkwargs if needed
    mask = np.ones(len(props), dtype=bool)
    if 'potential_LAMMPS_id' in getkwargs:
        mask = (mask) & (prop_df.potential_LAMMPS_id == getkwargs['potential_LAMMPS_id'])
        del getkwargs['potential_LAMMPS_id']
    if 'potential_LAMMPS_key' in getkwargs:
        mask = (mask) & (prop_df.potential_LAMMPS_key == getkwargs['potential_LAMMPS_key'])
        del getkwargs['potential_LAMMPS_key']
    if 'potential_id' in getkwargs:
        mask = (mask) & (prop_df.potential_id == getkwargs['potential_id'])
        del getkwargs['potential_id']
    if 'potential_key' in getkwargs:
        mask = (mask) & (prop_df.potential_key == getkwargs['potential_key'])
        del getkwargs['potential_key']
    if mask.sum() != len(props):
        props = props[mask]
        prop_df = prop_df[mask].reset_index(drop=True)

    # Load parent records
    parents_df = self.crystals_df

    # Loop over all props
    num_updated = 0
    num_skipped = 0
    for i, prop in enumerate(props):
        pot_id = prop.potential_id
        pot_key = prop.potential_key
        imp_id = prop.potential_LAMMPS_id
        imp_key = prop.potential_LAMMPS_key
        print(i, pot_id, imp_id, end=' ')

        # Skip records with existing results
        if prop.phonons.exists and runall is False:
            print('skipped')
            num_skipped += 1
            continue

        # Get records and records_df
        records, records_df = database.get_records(style='calculation_phonon',
                                                   status='finished', return_df=True,
                                                   potential_key=pot_key,
                                                   potential_LAMMPS_key=imp_key,
                                                   **getkwargs)
        if len(records) == 0:
            print('no finished records found')
            continue

        # Add prototype field
        self.identify_prototypes(records_df)

        # Merge parent data into records
        records_df = records_df.merge(parents_df, left_on='parent_key', right_on='key',
                                      suffixes=('', '_parent'), validate='many_to_one')

        # Build contentpath and check if it exists
        contentpath = Path(outputpath, pot_id, imp_id)
        if not contentpath.is_dir():
            contentpath.mkdir(parents=True)

        # Loop over records to extract files and create qha analysis objects
        qhas = []
        phononplot = []
        for record_index in records_df.index:
            record = records[record_index]
            record_series = records_df.loc[record_index]

            # Estimate natoms in primitive cell and create analysis object
            if not np.isnan(record_series.E0):
                natoms = record.dos['projected_dos'].shape[0]
                qhas.append(AnalyzeQHA(record, natoms))
            else:
                qhas.append(np.nan)

            # Extract plots from tar files
            extract_plots(database, record, record_series, contentpath, phononplot)

        # Add qha objects to the data frame
        records_df['qha'] = qhas

        # Loop over compositions to build thermo plots
        thermoplot = []
        for composition in np.unique(records_df.composition):
            comp_records_df = records_df[records_df.composition == composition]

            thermo_plots(comp_records_df, composition, contentpath, thermoplot)

        # Build info tables for the extracted/generated plots
        prop.phonons.phononplot = pd.DataFrame(phononplot)
        prop.phonons.thermoplot = pd.DataFrame(thermoplot)

        # Build model component
        prop.phonons.exists = True
        model = prop.model['per-potential-properties']
        prop.phonons.build_model(model)

        # Add/update PotentialsProperties record
        if upload:
            try:
                database.add_record(prop)
                print('added to database')
            except:
                database.update_record(prop)
                print('updated in database')
        else:
            print('created/modified')
        num_updated += 1

    print(num_updated, 'added/updated')
    print(num_skipped, 'skipped')

def extract_plots(database, record, series, contentpath, data):
    """
    Fetch a phonon record's tar file and extract the figures contained in it.
    """
    # Get the record's tar to extract contained plot figures
    tar = database.get_tar(record=record)
    tarnames = tar.getnames()

    # Set root name for extracted files
    fileroot = f'phonon.{series.composition}.{series.family}.{series.key.split("-")[0]}'

    # Extract the band images
    fname = f'{fileroot}.band.png'
    tarname = f'{series.key}/band.png'
    if tarname in tarnames:
        ftar = tar.extractfile(tarname)
        with open(Path(contentpath, fname), 'wb') as fout:
            fout.write(ftar.read())
        ftar.close()
        dat = {}
        dat['composition'] = series.composition
        dat['prototype'] = series.prototype
        dat['a'] = series.a
        dat['name'] = 'band structure'
        dat['png'] = fname
        data.append(dat)

    # Generate DOS plot
    fname = f'{fileroot}.dos.png'
    plt.plot(uc.get_in_units(record.dos['frequency'], 'THz'), record.dos['total_dos'])
    plt.xlabel('Frequency (THz)', size='x-large')
    plt.ylabel('Density of States', size='x-large')
    plt.ylim(0, None)
    plt.savefig(Path(contentpath, fname))
    plt.close()
    dat = {}
    dat['composition'] = series.composition
    dat['prototype'] = series.prototype
    dat['a'] = series.a
    dat['name'] = 'density of states'
    dat['png'] = fname
    data.append(dat)

    # Generate PDOS plot
    fname = f'{fileroot}.pdos.png'
    for pdos in record.dos['projected_dos']:
        plt.plot(uc.get_in_units(record.dos['frequency'], 'THz'), pdos)
    plt.xlabel('Frequency (THz)', size='x-large')
    plt.ylabel('Partial Density of States', size='x-large')
    plt.ylim(0, None)
    plt.savefig(Path(contentpath, fname))
    plt.close()
    dat = {}
    dat['composition'] = series.composition
    dat['prototype'] = series.prototype
    dat['a'] = series.a
    dat['name'] = 'partial density of states'
    dat['png'] = fname
    data.append(dat)

    # Extract the bulk modulus images
    fname = f'{fileroot}.bmod.png'
    tarname = f'{series.key}/bulk_modulus.png'
    if tarname in tarnames:
        ftar = tar.extractfile(tarname)
        with open(Path(contentpath, fname), 'wb') as fout:
            fout.write(ftar.read())
        ftar.close()
        dat = {}
        dat['composition'] = series.composition
        dat['prototype'] = series.prototype
        dat['a'] = series.a
        dat['name'] = 'bulk modulus'
        dat['png'] = fname
        data.append(dat)

    # Extract the band images
    fname = f'{fileroot}.helmvol.png'
    tarname = f'{series.key}/helmholtz_volume.png'
    if tarname in tarnames:
        ftar = tar.extractfile(tarname)
        with open(Path(contentpath, fname), 'wb') as fout:
            fout.write(ftar.read())
        ftar.close()
        dat = {}
        dat['composition'] = series.composition
        dat['prototype'] = series.prototype
        dat['a'] = series.a
        dat['name'] = 'Helmholtz vs. volume'
        dat['png'] = fname
        data.append(dat)

    tar.close()

def thermo_plots(df, composition, contentpath, data):
    """
    Function to call all other plot generation functions for thermo data.
    """
    gibbs_plot(df, composition, contentpath, data, uc_unit='eV', plot_unit='eV/atom')
    entropy_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol')
    cp_poly_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol')
    cp_num_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol')
    cv_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol')
    volume_plot(df, composition, contentpath, data, uc_unit='angstrom^3', plot_unit='Å^3/atom')
    expansion_plot(df, composition, contentpath, data)
    bulk_plot(df, composition, contentpath, data, uc_unit='GPa', plot_unit='GPa')

def gibbs_plot(df, composition, contentpath, data, uc_unit='eV', plot_unit='eV/atom'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.G.png'
    csvfile = f'phonon.{composition}.G.csv'
    htmlfile = f'phonon.{composition}.G.html'
    csv_df = {}

    sorted_df = df.sort_values('potential_energy')
    min_series =  None
    for index in sorted_df.index:
        series = sorted_df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            min_series = series
            break
    if min_series is None:
        fig.data = []
        return None

    csv_df['temperature'] = min_series.qha.T
    ref_G = uc.get_in_units(min_series.qha.G, uc_unit)

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            G = uc.get_in_units(qha.G, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=G - ref_G,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = G
            count += 1
        except:
            pass

    if count > 0:

        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"ΔGibbs ({plot_unit})",
            )

        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        # Save csv table
        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

        # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Gibbs'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def entropy_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.S.png'
    csvfile = f'phonon.{composition}.S.csv'
    htmlfile = f'phonon.{composition}.S.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            S = uc.get_in_units(qha.S, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=S,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = S
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Entropy ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Entropy'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def cp_poly_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.Cp-poly.png'
    csvfile = f'phonon.{composition}.Cp-poly.csv'
    htmlfile = f'phonon.{composition}.Cp-poly.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            Cp = uc.get_in_units(qha.Cp_poly, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=Cp,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = Cp
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Cp polyfit ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Cp polyfit'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def cp_num_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.Cp-num.png'
    csvfile = f'phonon.{composition}.Cp-num.csv'
    htmlfile = f'phonon.{composition}.Cp-num.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            Cp = uc.get_in_units(qha.Cp_num, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=Cp,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = Cp
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Cp numerical ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Cp numerical'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def cv_plot(df, composition, contentpath, data, uc_unit='J/mol', plot_unit='J/K/mol'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.Cv.png'
    csvfile = f'phonon.{composition}.Cv.csv'
    htmlfile = f'phonon.{composition}.Cv.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            Cv = uc.get_in_units(qha.Cv, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=Cv,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = Cv
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Cv ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Cv'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def volume_plot(df, composition, contentpath, data, uc_unit='angstrom^3', plot_unit='$\AA^3$/atom'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.V.png'
    csvfile = f'phonon.{composition}.V.csv'
    htmlfile = f'phonon.{composition}.V.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            V = uc.get_in_units(qha.V, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=V,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = V
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Volume ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Volume'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def expansion_plot(df, composition, contentpath, data):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.alpha.png'
    csvfile = f'phonon.{composition}.alpha.csv'
    htmlfile = f'phonon.{composition}.alpha.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            alpha = qha.alpha
            fig.add_trace(go.Scatter(x=qha.T, y=alpha,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = alpha
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Thermal expansion",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Thermal expansion'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []

def bulk_plot(df, composition, contentpath, data, uc_unit='GPa', plot_unit='GPa'):

    # Initialize plot and table
    fig = go.Figure()
    pngfile = f'phonon.{composition}.B.png'
    csvfile = f'phonon.{composition}.B.csv'
    htmlfile = f'phonon.{composition}.B.html'
    csv_df = {}

    for index in df.index:
        series = df.loc[index]
        if pd.isna(series.qha):
            continue
        else:
            csv_df['temperature'] = series.qha.T
            break
    if 'temperature' not in csv_df:
        fig.data = []
        return None

    # Loop over results
    count = 0
    for index in df.sort_values(['prototype', 'a']).index:
        series = df.loc[index]
        qha = series.qha
        if pd.isna(qha):
            continue
        label = f'{series.prototype} a={series.a:.4f}'

        try:
            B = uc.get_in_units(qha.B, uc_unit)
            fig.add_trace(go.Scatter(x=qha.T, y=B,
                          mode='lines+markers',
                          name=series.prototype))

            csv_df[label] = B
            count += 1
        except:
            pass

    # Save plot and csv if results exist
    if count > 0:
        # Save png figure
        fig.update_layout(
            title="Phonon/QHA Predictions",
            xaxis_title="Temperature (K)",
            yaxis_title=f"Bulk modulus ({plot_unit})",
            )
        fig.write_image(Path(contentpath, pngfile), width=1000, height=600,)
        fig.write_html(Path(contentpath, htmlfile))

        csv_df = pd.DataFrame(csv_df)
        csv_df.to_csv(Path(contentpath, csvfile))

       # Collect data for web generation
        dat = {}
        dat['composition'] = composition
        dat['name'] = 'Bulk modulus'
        dat['html'] = htmlfile
        dat['png'] = pngfile
        dat['csv'] = csvfile
        data.append(dat)

    fig.data = []


#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
from BIN.Fixer import Arreglar_Comprobantes
import os

def Abrir_Excel():
    os.system('start EXCEL.EXE "Lista de Archivos.xlsx"')

def donaciones():
    os.system("start https://cafecito.app/abustos")

class GuiEnvíoDeMailsApp:
    def __init__(self, master=None):
        # build ui
        Toplevel_1 = tk.Tk() if master is None else tk.Toplevel(master)
        Toplevel_1.configure(
            background="#2e2e2e",
            cursor="arrow",
            height=250,
            width=325)
        Toplevel_1.iconbitmap("BIN/ABP-blanco-en-fondo-negro.ico")
        Toplevel_1.overrideredirect("False")
        Toplevel_1.resizable(False, False)
        Toplevel_1.title("Arreglo de archivos")
        Label_3 = ttk.Label(Toplevel_1)
        self.img_ABPblancoenfondonegro111 = tk.PhotoImage(
            file="BIN/ABP blanco sin fondo.png")
        Label_3.configure(
            background="#2e2e2e",
            image=self.img_ABPblancoenfondonegro111)
        Label_3.pack(side="top")
        Label_1 = ttk.Label(Toplevel_1)
        Label_1.configure(
            background="#2e2e2e",
            cursor="arrow",
            foreground="#ffffff",
            justify="center",
            takefocus=False,
            text='Arreglo de "Importes Totales" y "Fechas" de archivos de compras y ventas para importar al Nacional\n',
            wraplength=325)
        Label_1.pack(expand=True, side="top")
        Label_2 = ttk.Label(Toplevel_1)
        Label_2.configure(
            background="#2e2e2e",
            foreground="#ffffff",
            justify="center",
            text='por Agustín Bustos Piasentini\nhttps://www.Agustin-Bustos-Piasentini.com.ar/')
        Label_2.pack(expand=True, side="top")
        self.Excel = ttk.Button(Toplevel_1)
        self.Excel.configure(text='Abrir Excel con datos' , command = Abrir_Excel)
        self.Excel.pack(expand=True, padx=0, pady=4, side="top")
        self.Enviar = ttk.Button(Toplevel_1)
        self.Enviar.configure(text='Corregir' , command=Arreglar_Comprobantes)
        self.Enviar.pack(expand=True, pady=4, side="top")
        self.Colaboraciones = ttk.Button(Toplevel_1)
        self.Colaboraciones.configure(text='Colaboraciones' , command=donaciones)
        self.Colaboraciones.pack(expand=True, pady=4, side="top")

        # Main widget
        self.mainwindow = Toplevel_1

    def run(self):
        self.mainwindow.mainloop()

if __name__ == "__main__":
    app = GuiEnvíoDeMailsApp()
    app.run()


import click
import requests

from cli.utils.global_options import global_options

@click.command()
@click.option('--key', '-k', type = str, required = True,
        help = 'The key of the to-be-deleted key-value pair')
@global_options
def delete(ip_address, port, key):
    url = 'http://' + str(ip_address) + ':' + str(port) + '/delete'
    params = { "key": key }
    response = requests.post(url, params = params)
    click.echo(response.text)


"""Miscellaneous utilities."""

import os

def progress_bar(iteration, total, prefix='', suffix='', length=50):
    """Print progress bar.

    Args:
        iteration (int): iteration number
        total (int): total iterations
        prefix (str): message to print before progress bar, default is ''
        length (int): character length of progress bar, default is 50

    """

    percent = "{:.1f}".format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = "X" * filledLength + '-' * (length - filledLength)
    print(f"\r{prefix} |{bar}| {percent}% {suffix}", end="\r")
    if iteration == total:
        print("")

def ask_filename(msg="\tFile name (omit extension): "):
    """Ask user for file name.

    Args:
        msg (str): message to print to user

    Returns:
        str: file name

    """

    print("")
    while True:
        fname = input(msg)
        print("")
        if fname == "":
            return None
        elif os.path.isfile(fname + ".dat") or os.path.isfile(fname + ".mdat"):
            ans = input("\tFile already exists. Overwrite? [y/n] ")
            print("")
            if ans.lower() == "y":
                return fname
        else:
            return fname

# MAIN -------------------------------------------------------------------- ##

if __name__ == "__main__":

    import time

    filename = ask_filename()
    if filename is None:
        print("\tNo filename given.\n")

    npts = 100
    for i in range(npts+1):
        progress_bar(i, npts, prefix="\tProgress: ")
        time.sleep(0.01)
    print("")


import os
import glob2
import pandas as pd
from modelLib import LungNet001a
import pydicom
import cv2
import numpy as np
from mask_functions import mask2rle

def predictImage(f,IMG_SIZE = 256):
    ORIG_DIM = 1024
    resize = (IMG_SIZE, IMG_SIZE)

    scan = pydicom.read_file(f)
    img = scan.pixel_array

    if (resize != (ORIG_DIM,ORIG_DIM)):
        img = cv2.resize(img, resize)

    img = img/255
    img = np.expand_dims(img, axis = 2)
    img = np.expand_dims(img, axis = 0)
    mask = model.predict(img)

    threshold = 0.5

    mask = mask.squeeze()
    #mask = mask.reshape(512,512)
    mask = cv2.resize(mask, (1024,1024))
    mask = mask > threshold

    if mask.sum()<1024*2:
        mask[:] = 0
    mask = (mask.T*255).astype(np.uint8)
    rle = mask2rle(mask, 1024, 1024)

    #mask = mask.round()
    #mask = mask*255

    #rle = mask2rle(mask, 1024, 1024)
    return rle

bestModelPath = '..\\out\\weights\\LungNet_25-loss--0.447.hdf5'

#model = load_model(bestModelPath)
model = LungNet001a()
model.load_weights(bestModelPath)

PATH_VAL = '..\\Data\\dicom-images-test\\'
CSVFILE = '..\\Data\\kanvari.csv'
SUBCSV = '..\\out\\submission_256_true.csv'

val = glob2.glob(os.path.join(PATH_VAL,'**/*.dcm'))

df = pd.read_csv('..\\Data\\kanvari.csv')

ids = []
rles = []

n = len(val)
idx = 0

for f in val:
    id = f.split('\\')[-1][:-4]
    x = df.loc[df.ImageId == id]
    try:
        x = x.iloc[0]['EncodedPixels']
        if (x == -1):
            print(id + " : False, Skipping " + str(idx) + " of " + str(n))
            rle = '-1'
        else:
            print(id + " : True, Predicting " + str(idx) + " of " + str(n))
            rle = predictImage(f)
    except:
        print(id + " : True, Predicting " + str(idx) + " of " + str(n))
        rle = predictImage(f)
    ids.append(id)
    rles.append(rle)
    idx = idx + 1

sub_df = pd.DataFrame({'ImageId': ids, 'EncodedPixels': rles})
sub_df.head()

sub_df.to_csv(SUBCSV, index=False)

#f = val[0]

#IMG_SIZE = 512


class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:

        def find(inter, num, left, right):

            while left <= right:
                mid = (left + right) // 2
                if inter[mid] == num:
                    return mid
                elif inter[mid] > num:
                    right = mid - 1
                else:
                    left = mid + 1

            return right

        left_idx = find([a[0] for a in intervals], newInterval[0], 0, len(intervals) - 1)
        right_idx = find([a[1] for a in intervals], newInterval[1], max(left_idx, 0), len(intervals) - 1)

        if left_idx > right_idx:
            return intervals
        else:
            new_interval = intervals[:max(left_idx, 0)]
            sub = []
            if left_idx == -1:
                sub.append(newInterval[0])
            else:
                if intervals[left_idx][1] < newInterval[0]:
                    new_interval.append(intervals[left_idx])
                    sub.append(newInterval[0])
                else:
                    sub.append(intervals[left_idx][0])

            if (right_idx + 1) >= len(intervals) or intervals[right_idx + 1][0] > newInterval[1]:
                sub.append(newInterval[1])
            else:
                sub.append(intervals[right_idx + 1][1])

            new_interval.append(sub)

            if right_idx + 1 < len(intervals):
                if intervals[right_idx + 1][0] > sub[1]:
                    new_interval.extend(intervals[right_idx + 1:])
                else:
                    if right_idx + 2 < len(intervals):
                        new_interval.extend(intervals[right_idx + 2:])

        return new_interval


#
# implementation of Lou 2015 IDC
#
import numpy as np
import pandas as pd
import networkx as nx
import utils.yeast_name_resolver
from collections import defaultdict
import scipy.stats as stats
import os

res = utils.yeast_name_resolver.NameResolver()

def main(gpath):

    G = nx.read_gpickle(gpath)
    nodes = sorted(G.nodes())
    node_ix = dict(zip(nodes, range(len(nodes))))

    genes_to_complexes = parse_yeast_complexes()

    complexes_to_genes = defaultdict(set)
    for g, complexes in genes_to_complexes.items():
        for c in complexes:
            complexes_to_genes[c].add(g)

    idc = {}
    for g, complexes in genes_to_complexes.items():
        degrees = []
        for complex in complexes:
            subset = complexes_to_genes[complex]
            sG = G.subgraph(subset)
            degrees.append(sG.degree(g))
        idc[g] = np.sum(degrees)

    F = np.zeros((len(nodes), 1))
    for g, v in idc.items():
        if g in node_ix:
            F[node_ix[g], 0] = v

    mu = np.mean(F, axis=0)
    std = np.std(F, axis=0)

    print(mu)
    print(std)

    # normalize
    F = stats.zscore(F, axis=0)

    print(np.min(F, axis=0))
    print(np.max(F, axis=0))
    print(np.mean(F, axis=0))
    print(np.std(F, axis=0))

    print("Nan: %d" % np.sum(np.isnan(F)))

    output_path = '../generated-data/features/%s_idc' % (os.path.basename(gpath))
    np.savez(output_path, F=F, feature_labels=['idc'], mu=mu, std=std)

def parse_yeast_complexes():

    df = pd.read_excel('../data-sources/yeast/CYC2008_complex.xls')

    df['gene'] = [res.get_unified_name(g.lower()) for g in df['ORF']]

    df_gene = list(df['gene'])
    df_complex = list(df['Complex'])

    genes_to_complexes = defaultdict(set)
    for i in range(df.shape[0]):
        g = df_gene[i]
        genes_to_complexes[g].add(df_complex[i])

    return genes_to_complexes

if __name__ == "__main__":
    import sys
    main(sys.argv[1])


import time
import math
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

url = "http://suninjuly.github.io/explicit_wait2.html"
browser = webdriver.Chrome()

def calc(x):
    return str(math.log(abs(12 * math.sin(int(x)))))

try:
    browser.get(url)

    element = WebDriverWait(browser, 10).until(
        EC.text_to_be_present_in_element((By.ID, 'price'), '$100'))

    browser.find_element_by_css_selector('#book').click()

    # time.sleep(30)
    x = browser.find_element_by_css_selector('#input_value').text
    browser.find_element_by_css_selector('#answer').send_keys(calc(x))
    browser.find_element_by_css_selector('.form-group button').click()

finally:

    time.sleep(10)
    browser.quit()

    pass


"""Defines the main task for the PACK
"""

import os
import numpy as np
import torch
from torch.utils.data import Dataset
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches

from scipy.spatial import ConvexHull
from matplotlib.path import Path

from tqdm import tqdm
import multiprocessing
import networkx as nx
import itertools

import tools
import generate

class PACKDataset(Dataset):
    def __init__(self, data_file, blocks_num, num_samples, seed, input_type, heightmap_type, allow_rot, container_width, mix_data_file=None, unit=1, no_precedence=False):
        '''
        Data initialization
        ----
        params
        ----
            input_type: str, the type of input data
                'simple':   [idx][w,(l),h]     [0,1,1,0,...][   ]

                'rot':      [idx][w,(l),h]     [0,1,1,0,...][   ]
                'rot-old':  [idx][w,(l),h]     [0,1,1,0,...][0]

                'bot':      [idx][w,(l),h]     [0,1,1,0,...][0,0,1,1,...][0,1,0,1,...]
                'bot-rot':  [idx][w,(l),h]     [0,1,1,0,...][0,0,0,0,0..][0,0,0,0,0..]

                'mul':      [idx][w,(l),h][  ] [0,1,1,0,...] # old
                'mul-with': [idx][w,(l),h][id] [0,1,1,0,...] # old

                'mul':      [idx][w,(l),h][  ] [0,1,1,0,...][0,0,1,1,...][0,1,0,1,...]
                'mul-with': [idx][w,(l),h][id] [0,1,1,0,...][0,0,1,1,...][0,1,0,1,...]

            allow_rot: bool, allow to rotate
                False:  the final dim of input will be blocks_num * 1
                True:   the final dim of input will be blocks_num * rotate_types
        '''
        super(PACKDataset, self).__init__()
        if seed is None:
            seed = np.random.randint(123456)
        np.random.seed(seed)
        torch.manual_seed(seed)

        if mix_data_file is None:
            deps_move = np.loadtxt(data_file + 'dep_move.txt').astype('float32')
            rotate_deps_small = np.loadtxt(data_file + 'dep_small.txt').astype('float32')
            rotate_deps_large = np.loadtxt(data_file + 'dep_large.txt').astype('float32')

            blocks = np.loadtxt(data_file + 'blocks.txt').astype('float32')
            positions = np.loadtxt(data_file + 'pos.txt').astype('float32')
            container_index = np.loadtxt(data_file + 'container.txt').astype('float32')

        else:
            num_mid = int(num_samples / 2)
            print('Mixing... %d' % num_mid)
            deps_move = np.loadtxt(data_file + 'dep_move.txt').astype('float32')[:num_mid]
            positions = np.loadtxt(data_file + 'pos.txt').astype('float32')[:num_mid]
            container_index = np.loadtxt(data_file + 'container.txt').astype('float32')[:num_mid]

            rotate_deps_small = np.loadtxt(data_file + 'dep_small.txt').astype('float32')
            rotate_deps_large = np.loadtxt(data_file + 'dep_large.txt').astype('float32')
            blocks = np.loadtxt(data_file + 'blocks.txt').astype('float32')

            rot_num_mid = int( len(blocks) / 2)
            rotate_deps_small = np.loadtxt(data_file + 'dep_small.txt').astype('float32')[:rot_num_mid]
            rotate_deps_large = np.loadtxt(data_file + 'dep_large.txt').astype('float32')[:rot_num_mid]
            blocks = np.loadtxt(data_file + 'blocks.txt').astype('float32')[:rot_num_mid]

            mix_deps_move = np.loadtxt(mix_data_file + 'dep_move.txt').astype('float32')[:num_mid]
            mix_positions = np.loadtxt(mix_data_file + 'pos.txt').astype('float32')[:num_mid]
            mix_container_index = np.loadtxt(mix_data_file + 'container.txt').astype('float32')[:num_mid]

            mix_rotate_deps_small = np.loadtxt(mix_data_file + 'dep_small.txt').astype('float32')[:rot_num_mid]
            mix_rotate_deps_large = np.loadtxt(mix_data_file + 'dep_large.txt').astype('float32')[:rot_num_mid]
            mix_blocks = np.loadtxt(mix_data_file + 'blocks.txt').astype('float32')[:rot_num_mid]

            deps_move = np.vstack( (deps_move, mix_deps_move) )
            rotate_deps_small = np.vstack( (rotate_deps_small, mix_rotate_deps_small) )
            rotate_deps_large = np.vstack( (rotate_deps_large, mix_rotate_deps_large) )
            blocks = np.vstack( (blocks, mix_blocks) )
            positions = np.vstack( (positions, mix_positions) )
            container_index = np.vstack( (container_index, mix_container_index) )

        positions = torch.from_numpy(positions)
        positions = positions.view(num_samples, -1, blocks_num)

        deps_move = torch.from_numpy(deps_move)
        deps_move = deps_move.view(num_samples, -1, blocks_num)
        deps_move = deps_move.transpose(2, 1)

        block_dim = positions.shape[1]
        rotate_types = np.math.factorial(block_dim)

        # data_size = int(len(blocks) / rotate_types)
        # num_samples x rotate_types x block_dim x blocks_num
        blocks = blocks.reshape( num_samples, -1, block_dim, blocks_num)
        # num_samples x rotate_types x blocks_num x block_dim
        blocks = blocks.transpose(0, 1, 3, 2)
        # num_samples x (rotate_types * blocks_num) x block_dim
        blocks = blocks.reshape( num_samples, -1, block_dim )
        # num_samples x block_dim x (blocks_num * rotate_types)
        blocks = blocks.transpose(0,2,1)
        blocks = torch.from_numpy(blocks)

        # resolution
        blocks = blocks * unit
        # if unit<1:
        blocks = blocks.ceil()#.int()

        rotate_deps_small = rotate_deps_small.reshape( num_samples, -1, blocks_num, blocks_num )
        rotate_deps_large = rotate_deps_large.reshape( num_samples, -1, blocks_num, blocks_num )
        rotate_deps_small = rotate_deps_small.transpose(0,1,3,2)
        rotate_deps_large = rotate_deps_large.transpose(0,1,3,2)
        rotate_deps_small = rotate_deps_small.reshape( num_samples, blocks_num*rotate_types, blocks_num )
        rotate_deps_large = rotate_deps_large.reshape( num_samples, blocks_num*rotate_types, blocks_num )
        rotate_deps_small = rotate_deps_small.transpose(0,2,1)
        rotate_deps_large = rotate_deps_large.transpose(0,2,1)
        rotate_deps_small = torch.from_numpy(rotate_deps_small)
        rotate_deps_large = torch.from_numpy(rotate_deps_large)

        # check rotate type:
        if allow_rot == False:
            blocks = blocks[:,:,:blocks_num]
            rotate_types = 1

        blocks_index = torch.arange(blocks_num)
        blocks_index = blocks_index.unsqueeze(0).unsqueeze(0)
        # num_samples x 1 x (blocks_num * rotate_types)
        blocks_index = blocks_index.repeat(num_samples, 1, rotate_types).float()

        # import IPython

        container_index = torch.from_numpy(container_index)
        container_index = container_index.unsqueeze(1)
        container_index = container_index.repeat(1, 1, rotate_types).float()
        # num_samples x block_dim x (blocks_num * rotate_types)
        positions = positions.repeat(1,1,rotate_types)
        # num_samples x blocks_num x (blocks_num * rotate_types)
        deps_move = deps_move.repeat(1,1,rotate_types)

        # # # random shuffle
        # order = [ o for o in range(blocks_num) ]
        # np.random.shuffle(order)
        # order = order * rotate_types
        # order = np.array(order)
        # for r in range(1, rotate_types):
        #     order[r*blocks_num: ] += blocks_num
        # blocks = blocks[:,:,order]
        # blocks_index = blocks_index[:,:,order]
        # deps_move = deps_move[:,:,order]
        # rotate_deps_small = rotate_deps_small[:,:,order]
        # rotate_deps_large = rotate_deps_large[:,:,order]
        # container_index = container_index[:,:,order]
        # positions = positions[:,:,order]

        # No precedence setting
        if no_precedence == True:
            deps_move = torch.zeros_like(deps_move)
            rotate_deps_small = torch.zeros_like(rotate_deps_small)
            rotate_deps_large = torch.zeros_like(rotate_deps_large)

        # conbine the data into our final input
        if input_type == 'simple':
            # num_samples x (1 + block_dim) x (blocks_num * 1)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num) x (blocks_num * 1)
            self.dynamic = deps_move
        elif input_type == 'rot':
            # num_samples x (1 + block_dim) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num) x (blocks_num * rotate_types)
            self.dynamic = deps_move
        elif input_type == 'bot':
            # num_samples x (1 + block_dim) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num * 3) x (blocks_num * rotate_types)
            self.dynamic = torch.cat( (deps_move, rotate_deps_small, rotate_deps_large), 1 )
        elif input_type == 'bot-rot':
            # num_samples x (1 + block_dim) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num * 3) x (blocks_num * rotate_types)
            rotate_deps_small = torch.zeros_like(rotate_deps_small)
            rotate_deps_large = torch.zeros_like(rotate_deps_large)
            self.dynamic = torch.cat( (deps_move, rotate_deps_small, rotate_deps_large), 1 )

        elif input_type == 'use-static' or input_type == 'use-pnet':
            # num_samples x (1 + block_dim) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num * 3) x (blocks_num * rotate_types)
            rotate_deps_small = torch.zeros_like(rotate_deps_small)
            rotate_deps_large = torch.zeros_like(rotate_deps_large)
            self.dynamic = torch.cat( (deps_move, rotate_deps_small, rotate_deps_large), 1 )

        elif input_type == 'mul' or input_type == 'mul-with':
            # num_samples x (1 + block_dim + 1) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks, container_index), 1 )
            # num_samples x (blocks_num * 3) x (blocks_num * rotate_types)
            self.dynamic = torch.cat( (deps_move, rotate_deps_small, rotate_deps_large), 1 )

        elif input_type == 'rot-old':
            rotate_state = torch.zeros_like(blocks_index)
            # num_samples x (1 + block_dim) x (blocks_num * rotate_types)
            self.static = torch.cat( (blocks_index, blocks), 1 )
            # num_samples x (blocks_num + 1) x (blocks_num * rotate_types)
            self.dynamic = torch.cat( (deps_move, rotate_state), 1 )

        else:
            print('Dataset OHHHHH')

        print('    Static shape:  ', self.static.shape)
        print('    Dynamic shape: ', self.dynamic.shape)

        static_dim = block_dim
        heightmap_num = 1

        if heightmap_type == 'diff':
            if block_dim == 2:
                heightmap_width = container_width * unit - 1
            elif block_dim == 3:
                heightmap_num = 2
                heightmap_width = container_width * unit
                heightmap_length = container_width * unit
        else:
            heightmap_width = container_width * unit
            heightmap_length = container_width * unit

        # if unit < 1:
        heightmap_width = np.ceil(heightmap_width).astype(int)
        if block_dim==3: heightmap_length = np.ceil(heightmap_length).astype(int)

        if input_type == 'mul' or input_type == 'mul-with':
            if block_dim == 2:
                heightmap_width = heightmap_width * 2
            else:
                heightmap_num = heightmap_num * 2

        if input_type == 'mul-with':
            static_dim = static_dim + 1

        if block_dim == 2:
            self.decoder_static = torch.zeros(num_samples, static_dim, 1, requires_grad=True)
            self.decoder_dynamic = torch.zeros(num_samples, heightmap_width, 1, requires_grad=True)
        elif block_dim == 3:
            self.decoder_static = torch.zeros(num_samples, static_dim, 1, requires_grad=True)
            self.decoder_dynamic = torch.zeros(num_samples, heightmap_num, heightmap_width, heightmap_length, requires_grad=True)

        self.num_samples = num_samples

    def __len__(self):
        return self.num_samples

    def __getitem__(self, idx):
        # (static, dynamic, start_loc)
        return (self.static[idx], self.dynamic[idx], self.decoder_static[idx], self.decoder_dynamic[idx])

def update_mask(mask, dynamic, static, chosen_idx, input_type, allow_rot):
    """
    Return two mask
        new_mask for current selection
        chosen_mask for next selection
    """
    # mask: batch_size x size
    # dynamic: batch_size x _size x size
    # chosen_idx: batch_size

    batch_size = chosen_idx.shape[0]

    if input_type == 'simple':
        # real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'rot' or input_type == 'rot-old':
        # real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'bot' or input_type == 'bot-rot':
        # real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'use-static' or input_type == 'use-pnet':
        # real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'mul' or input_type == 'mul-with':
        # real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 2))
    else:
        print('mask OHHHHHH')

    if allow_rot == False:
        rotate_types = 1
    else:
        rotate_types = np.math.factorial(block_dim)

    blocks_num = int(dynamic.shape[-1] / rotate_types)

    # real index get real index (the order of input set)
    real_chosen_idx = chosen_idx.clone()
    while (real_chosen_idx >= blocks_num).any():
        real_chosen_idx[ real_chosen_idx >= blocks_num ] -= blocks_num

    chosen_mask = mask.clone()
    # let the chosen object not selectable
    for i in range(rotate_types):
        chosen_mask = chosen_mask.scatter(1, (real_chosen_idx + blocks_num * i).unsqueeze(1), 0)

    new_mask = chosen_mask.clone()
    move_mask = dynamic[:, :blocks_num, :].sum(1)
    rotate_small_mask = dynamic[:, blocks_num:blocks_num*2, :].sum(1)
    rotate_large_mask = dynamic[:, blocks_num*2:blocks_num*3, :].sum(1)
    rotate_mask = rotate_small_mask * rotate_large_mask
    dynamic_mask = rotate_mask + move_mask
    new_mask[ dynamic_mask.ne(0) ] = 0.

    return new_mask.float(), chosen_mask

def update_dynamic(dynamic, static, chosen_idx, input_type, allow_rot):
    """Updates the (load, demand) dataset values."""
    # TODO all of them
    batch_size = chosen_idx.shape[0]

    if input_type == 'simple':
        real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
        update_time = 1
    elif input_type == 'rot' or input_type == 'rot-old':
        real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
        update_time = 1
    elif input_type == 'bot' or input_type == 'bot-rot':
        real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
        update_time = 3
    elif input_type == 'use-static' or input_type == 'use-pnet':
        real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 1))
        update_time = 3
    elif input_type == 'mul' or input_type == 'mul-with':
        real_chosen_idx = static[:,0,:][ range(batch_size), chosen_idx ].long()
        block_dim = int((static.shape[1] - 2))
        update_time = 3
    else:
        print('dynamic OHHHHHHHH')

    # TODO rotate mask
    if allow_rot == False:
        rotate_types = 1
    else:
        rotate_types = np.math.factorial(block_dim)

    blocks_num = int(dynamic.shape[-1] / rotate_types)

    # batch_size x size x sourceL
    new_dynamic = dynamic.clone()

    for i in range(update_time):
        chosen_idx = (real_chosen_idx + blocks_num * i).unsqueeze(1).unsqueeze(1).repeat(1,1, dynamic.shape[-1] )
        new_dynamic.scatter_(1, chosen_idx, 0)

    return new_dynamic

def reward(static, tour_indices, reward_type, input_type, allow_rot, container_width, container_height, packing_strategy='LB_GREEDY'):
    """
    Deprecated, we output reward after container packed all the blocks in actor network
    --------------
    Parameters
    ----------
    static: torch.FloatTensor containing static (e.g. w, h) data (batch_size, sourceL, num_cities)
    tour_indices: torch.IntTensor of size (batch_size, num_cities)

    Returns
    -------
    Euclidean distance between consecutive nodes on the route. of size
    (batch_size, num_cities)
    """

    use_cuda = static.is_cuda
    # TODO new type of input_type
    if input_type == 'simple':
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'rot' or input_type == 'rot-old':
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'bot' or input_type == 'bot-rot':
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'use-static' or input_type == 'use-pnet':
        block_dim = int((static.shape[1] - 1))
    elif input_type == 'mul' or input_type == 'mul-with':
        block_dim = int((static.shape[1] - 2))
    else:
        print('REWARD OHHHHHH')

    if block_dim == 3:
        container_size = [container_width, container_width, container_height]
    else:
        container_size = [container_width, container_height]
    if input_type == 'mul' or input_type == 'mul-with':
        if block_dim == 3:
            container_size_a = [container_width, container_width,container_height]
            container_size_b = container_size_a
        else:
            container_size_a = [container_width, container_height]
            container_size_b = container_size_a

    # batch_size x (1 + block_dim + block_dim) x (blocks_num * rotate_types)
    # static
    dim1 = static.shape[1]
    dim2 = static.shape[2]

    if allow_rot == False:
        rotate_types = 1
    else:
        rotate_types = np.math.factorial(block_dim)

    if packing_strategy == 'MACS' or packing_strategy == 'MUL':
        calc_position_fn = tools.calc_positions_mus
    elif reward_type == 'C+P+S-SL-soft' or reward_type == 'C+P+S-RL-soft' or reward_type == 'C+P+S-G-soft' or reward_type == 'C+P+S-LG-soft':
        calc_position_fn = tools.calc_positions_net
    else:
        # calc_position_fn = tools.calc_positions_greedy
        calc_position_fn = tools.calc_positions_lb_greedy

    blocks_num = int(dim2 / rotate_types)

    # batch_size x blocks_num  // tour_indices and expand as(static)
    idx = tour_indices.unsqueeze(1).repeat(1, dim1, rotate_types)

    # batch_size x (1 + block_dim) x blocks_num
    sample_solution = torch.gather(static.data, 2, idx)[:,:,:blocks_num]
    sample_solution = sample_solution.cpu().numpy()

    batch_size = sample_solution.shape[0]
    scores = torch.zeros(batch_size).detach()
    if use_cuda:
        scores = scores.cuda()

    for batch_index in range(batch_size):
        # if input_type == 'simple':
        blocks = sample_solution[batch_index,1:1+block_dim,:]
        blocks = blocks.transpose(1,0)
        if input_type == 'mul' or input_type == 'mul-with':
            target_ids = sample_solution[batch_index,-1,:]
            blocks_a = blocks[target_ids == 0]
            blocks_b = blocks[target_ids == 1]

            if len(blocks_a) == 0:
                scores_a = 0
            else:
                _, _, _, scores_a, _ = calc_position_fn(blocks_a, container_size_a, reward_type)
            if len(blocks_b) == 0:
                scores_b = 0
            else:
                _, _, _, scores_b, _ = calc_position_fn(blocks_b, container_size_b, reward_type)
            scores[batch_index] = (scores_a + scores_b) / 2
        else:
            _, _, _, scores[batch_index], _ = calc_position_fn(blocks, container_size, reward_type)

    return -scores

def create_dataset_gt(blocks_num, train_size, valid_size, obj_dim, target_container_width, target_container_height, initial_container_width, initial_container_height, input_type, arm_size, size_range, seed=None):
    blocks_num = int(blocks_num)
    if seed is None:
        seed = np.random.randint(123456789)
    np.random.seed(seed)

    if obj_dim == 2:
        initial_container_size = [initial_container_width, initial_container_height]
        target_container_size = [target_container_width, target_container_height]
        train_dir = './data/gt_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir = './data/gt_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
    elif obj_dim == 3:
        initial_container_size = [initial_container_width, initial_container_width, initial_container_height]
        target_container_size = [target_container_width, target_container_width, target_container_height]
        train_dir = './data/gt_3d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir = './data/gt_3d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

    def arr2str(arr):
        ret = ''
        for i in range(len(arr)-1):
            ret += str(arr[i]) + ' '
        ret += str(arr[-1]) + '\n'
        return ret

    if os.path.exists(train_dir + 'blocks.txt') and os.path.exists(valid_dir  + 'blocks.txt'):
        return train_dir, valid_dir

    if not os.path.exists(train_dir):
        os.makedirs(train_dir)
    if not os.path.exists(valid_dir):
        os.makedirs(valid_dir)

    # random_distribution = None
    # if initial_container_width < -1:
    random_distribution = generate.generate_height_prob(obj_dim, blocks_num, size_range, initial_container_width, target_container_width)
    prob, key = random_distribution

    # print('random_distribution: \n', prob, '\n', key)

    def generate_data(data_dir, data_size):

        container_ids = np.ones(blocks_num)
        container_ids[:int(blocks_num/2)] = 0
        np.random.shuffle(container_ids)

        blocks_f = open(data_dir + 'blocks.txt', 'w')
        pos_f = open(data_dir + 'pos.txt', 'w')
        container_f = open(data_dir + 'container.txt', 'w')

        deps_move_f = open(data_dir + 'dep_move.txt', 'w')
        rotate_deps_small_f = open(data_dir + 'dep_small.txt', 'w')
        rotate_deps_large_f = open(data_dir + 'dep_large.txt', 'w')

        for data_index in tqdm(range(data_size)):
            if random_distribution is not None:
                target_container_size[-1] = np.random.choice( key, p=prob )
            rotate_blocks, positions, deps_move, rotate_deps_small, rotate_deps_large = \
                    generate.generate_blocks_with_GT(blocks_num, target_container_size, initial_container_size,
                                                        arm_size, size_range, input_type, data_index, allow_rot=True)

            for blocks_index, blocks in enumerate(rotate_blocks):
                blocks_f.writelines(arr2str( blocks ) )
                rotate_deps_small_f.writelines(arr2str( rotate_deps_small[blocks_index] ))
                rotate_deps_large_f.writelines(arr2str( rotate_deps_large[blocks_index] ))

            pos_f.writelines(arr2str( positions ) )
            deps_move_f.writelines( arr2str( deps_move ) )
            container_f.writelines( arr2str( container_ids.astype('int') )  )

        blocks_f.close()
        rotate_deps_small_f.close()
        rotate_deps_large_f.close()

        pos_f.close()
        deps_move_f.close()
        container_f.close()

    if not os.path.exists(train_dir + 'blocks.txt'):
        generate_data(train_dir, train_size)
    if not os.path.exists(valid_dir + 'blocks.txt'):
        generate_data(valid_dir, valid_size)
    return train_dir, valid_dir

def get_mix_dataset( blocks_num, train_size, valid_size, obj_dim, initial_container_width, size_range, seed=None):
    blocks_num = int(blocks_num)
    if seed is None:
        seed = np.random.randint(123456789)
    np.random.seed(seed)

    if obj_dim == 2:
        train_dir_1 = './data/gt_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        train_dir_2 = './data/rand_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

        valid_dir_1 = './data/gt_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir_2 = './data/rand_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

    elif obj_dim == 3:
        train_dir_1 = './data/gt_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        train_dir_2 = './data/rand_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

        valid_dir_1 = './data/gt_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir_2 = './data/rand_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

    return train_dir_1, train_dir_2, valid_dir_1, valid_dir_2

def create_dataset( blocks_num, train_size, valid_size, obj_dim, initial_container_width, initial_container_height, arm_size, size_range, seed=None):
    blocks_num = int(blocks_num)
    if seed is None:
        seed = np.random.randint(123456789)
    np.random.seed(seed)

    if obj_dim == 2:
        initial_container_size = [initial_container_width, initial_container_height]
        train_dir = './data/rand_2d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir = './data/rand_2d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
    elif obj_dim == 3:
        initial_container_size = [initial_container_width, initial_container_width, initial_container_height]
        train_dir = './data/rand_3d/pack-train-' + str(blocks_num) + '-' + str(train_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'
        valid_dir = './data/rand_3d/pack-valid-' + str(blocks_num) + '-' + str(valid_size) + '-' + str(initial_container_width) + '-' + str(size_range[0]) + '-' + str(size_range[1]) + '/'

    def arr2str(arr):
        ret = ''
        for i in range(len(arr)-1):
            ret += str(arr[i]) + ' '
        ret += str(arr[-1]) + '\n'
        return ret

    if os.path.exists(train_dir + 'blocks.txt') and os.path.exists(valid_dir  + 'blocks.txt'):
        return train_dir, valid_dir

    if not os.path.exists(train_dir):
        os.makedirs(train_dir)
    if not os.path.exists(valid_dir):
        os.makedirs(valid_dir)

    random_distribution = None
    random_num = False
    if initial_container_width == -1:
        random_distribution = generate.generate_deps_prob(obj_dim, blocks_num, 0)
    elif initial_container_width < -1:
        random_distribution = generate.generate_deps_prob(obj_dim, blocks_num, 0)
        random_num = True

    def generate_data(data_dir, data_size):

        print('here')

        blocks_f = open(data_dir + 'blocks.txt', 'w')
        pos_f = open(data_dir + 'pos.txt', 'w')
        container_f = open(data_dir + 'container.txt', 'w')

        deps_move_f = open(data_dir + 'dep_move.txt', 'w')
        rotate_deps_small_f = open(data_dir + 'dep_small.txt', 'w')
        rotate_deps_large_f = open(data_dir + 'dep_large.txt', 'w')

        for _ in tqdm(range(data_size)):

            container_ids = np.ones(blocks_num)
            container_ids[:int(blocks_num/2)] = 0
            np.random.shuffle(container_ids)
            # container_f.writelines( arr2str( container_ids.astype('int') )  )

            # continue
            rotate_blocks, positions, deps_move, rotate_deps_small, rotate_deps_large = \
                generate.generate_blocks(blocks_num, initial_container_size, arm_size, size_range, random_distribution, random_num)

            for blocks_index, blocks in enumerate(rotate_blocks):
                blocks_f.writelines(arr2str( blocks ) )
                rotate_deps_small_f.writelines(arr2str( rotate_deps_small[blocks_index] ))
                rotate_deps_large_f.writelines(arr2str( rotate_deps_large[blocks_index] ))

            pos_f.writelines(arr2str( positions ) )
            deps_move_f.writelines( arr2str( deps_move ) )
            container_f.writelines( arr2str( np.random.random_integers(0,1, blocks_num) )  )

        blocks_f.close()
        rotate_deps_small_f.close()
        rotate_deps_large_f.close()

        pos_f.close()
        deps_move_f.close()
        container_f.close()

    if not os.path.exists(train_dir + 'blocks.txt'):
        generate_data(train_dir, train_size)
    if not os.path.exists(valid_dir + 'blocks.txt'):
        generate_data(valid_dir, valid_size)
    return train_dir, valid_dir

def render(static, tour_indices, save_path, dynamic, valid_time, **kwargs):
    """Plots"""

    # batch_size x (1 + block_dim) x (blocks_num * rotate_types)
    dim1 = static.shape[1]
    dim2 = static.shape[2]

    if kwargs['input_type'] == 'simple':
        block_dim = int((static.shape[1] - 1))
    elif kwargs['input_type'] == 'rot' or kwargs['input_type'] == 'rot-old':
        block_dim = int((static.shape[1] - 1))
    elif kwargs['input_type'] == 'bot' or kwargs['input_type'] == 'bot-rot':
        block_dim = int((static.shape[1] - 1))
    elif kwargs['input_type'] == 'use-static' or kwargs['input_type'] == 'use-pnet':
        block_dim = int((static.shape[1] - 1))
    elif kwargs['input_type'] == 'mul' or kwargs['input_type'] == 'mul-with':
        block_dim = int((static.shape[1] - 2))
    else:
        print('Render OHHHH')

    if kwargs['allow_rot'] == False:
        rotate_types = 1
    else:
        rotate_types = np.math.factorial(block_dim)

    blocks_num = int(dim2 / rotate_types)

    # batch_size x blocks_num
    # tour_indices   expand as(static)
    idx = tour_indices.unsqueeze(1).repeat(1, dim1, rotate_types)

    all_blocks = static.data[:,1:1+block_dim,:].cpu().numpy()
    all_blocks = all_blocks.transpose(0, 2, 1).astype('int')

    # batch_size x (1 + block_dim + block_dim) x blocks_num
    sample_solution = torch.gather(static.data, 2, idx)[:,:,:blocks_num]
    sample_solution = sample_solution.cpu().numpy()

    batch_size = sample_solution.shape[0]

    data_len = len(tour_indices)

    container_width  = kwargs['container_width']  * kwargs['unit']
    container_height = kwargs['container_height'] * kwargs['unit']
    initial_container_width  = kwargs['initial_container_width']  * kwargs['unit']
    initial_container_height = kwargs['initial_container_height'] * kwargs['unit']

    container_width  = np.ceil(container_width).astype(int)
    container_height = np.ceil(container_height).astype(int)
    initial_container_width  = np.ceil(initial_container_width).astype(int)
    initial_container_height = np.ceil(initial_container_height).astype(int)

    if block_dim == 3:
        container_size = [container_width, container_width, container_height]
        initial_container_size = [initial_container_width, initial_container_width, initial_container_height]
    else:
        container_size = [container_width, container_height]
        initial_container_size = [initial_container_width, initial_container_height]
    if kwargs['input_type'] == 'mul' or kwargs['input_type'] == 'mul-with':
        if block_dim == 3:
            container_size_a = [container_width, container_width, initial_container_height]
            container_size_b = container_size_a
        else:
            container_size_a = [container_width, container_height]
            container_size_b = container_size_a

    my_ratio = []
    my_valid_size = []
    my_box_size = []
    my_empty_size = []
    my_stable_num = []
    my_packing_height = []

    if kwargs['packing_strategy'] == 'MACS' or kwargs['packing_strategy'] == 'MUL':
        calc_position_fn = tools.calc_positions_mcs
    elif kwargs['reward_type'] == 'C+P+S-SL-soft' or kwargs['reward_type'] == 'C+P+S-RL-soft' or \
         kwargs['reward_type'] == 'C+P+S-G-soft'  or kwargs['reward_type'] == 'C+P+S-LG-soft':
        calc_position_fn = tools.calc_positions_net
    else:
        # calc_position_fn = tools.calc_positions_greedy
        calc_position_fn = tools.calc_positions_lb_greedy

    for i in range(data_len):
        plt.close('all')
        fig = plt.figure()

        order = tour_indices[i].long().cpu().numpy()

        if kwargs['input_type'] == 'simple':
            blocks = sample_solution[i,1:1+block_dim,:]
        else:
            blocks = sample_solution[i,1:1+block_dim,:]

        blocks = blocks.transpose(1,0)

        my_all_blocks = all_blocks[i]

        if kwargs['input_type'] == 'mul' or kwargs['input_type'] == 'mul-with':
            target_ids = sample_solution[i,-1,:]
            blocks_a = blocks[target_ids == 0]
            blocks_b = blocks[target_ids == 1]

            if len(blocks_a) == 0:
                ratio_a, valid_size_a, box_size_a, empty_num_a, stable_num_a, packing_height_a = 0, 0, 0, 0, 0, 0
            else:
                _, _, _, ratio_a, scores_a = calc_position_fn(blocks_a, container_size_a, kwargs['reward_type'])
                valid_size_a, box_size_a, empty_num_a, stable_num_a, packing_height_a = scores_a
            if len(blocks_b) == 0:
                ratio_b, valid_size_b, box_size_b, empty_num_b, stable_num_b, packing_height_b = 0, 0, 0, 0, 0, 0
            else:
                _, _, _, ratio_b, scores_b = calc_position_fn(blocks_b, container_size_b, kwargs['reward_type'])
                valid_size_b, box_size_b, empty_num_b, stable_num_b, packing_height_b = scores_b

            ratio = (ratio_a + ratio_b) / 2
            valid_size = (valid_size_a + valid_size_b) / 2
            box_size = (box_size_a + box_size_b) / 2
            empty_num = (empty_num_a + empty_num_b) / 2
            stable_num = (stable_num_a + stable_num_b) / 2
            packing_height = (packing_height_a + packing_height_b) / 2
        else:
            positions, container, stable, ratio, scores = calc_position_fn(blocks, container_size, kwargs['reward_type'])
            valid_size, box_size, empty_num, stable_num, packing_height = scores

        my_ratio.append(ratio)
        my_valid_size.append(valid_size)
        my_box_size.append(box_size)
        my_empty_size.append(empty_num)
        my_stable_num.append(stable_num)
        my_packing_height.append(packing_height)
        if i >= 6:
            continue

        C = valid_size / box_size
        P = valid_size / (valid_size + empty_num)
        S = stable_num / blocks_num

        # # paint
        # if input_type == 'mul' or input_type == 'mul-with':
        #     target_ids = sample_solution[i,-1,:]
        #     blocks_a = blocks[target_ids == 0]
        #     blocks_b = blocks[target_ids == 1]

        #     # calc the real index of blocks
        #     def calc_real_order(order, blocks_num):
        #         '''
        #         Calc the real index order of blocks
        #         ---
        #         params:
        #         ----
        #             order: 1 x n int array / list, the select order of blocks (contain rotation index)
        #             blocks_num: int, number of blocks
        #         return:
        #         ----
        #             real_order: 1 x n int array, the real block index (without rotation index)
        #             rotate_state: 1 x n bool array, the rotation state of each block
        #         '''
        #         real_order = []
        #         rotate_state = []
        #         for o in order:
        #             if o < blocks_num:
        #                 real_order.append(o)
        #                 rotate_state.append(False)
        #             else:
        #                 tmp = o
        #                 while tmp >= blocks_num:
        #                     tmp = tmp-blocks_num
        #                 real_order.append(tmp)
        #                 rotate_state.append(True)

        #         real_order = np.array(real_order)
        #         rotate_state = np.array(rotate_state)
        #         return real_order, rotate_state

        #     real_order, rotate_state = calc_real_order(order, blocks_num)

        #     order_a = real_order[ target_ids == 0 ]
        #     order_b = real_order[ target_ids == 1 ]

        #     rotate_state_a = rotate_state[ target_ids == 0 ]
        #     rotate_state_b = rotate_state[ target_ids == 1 ]

        #     blocks_num_a = np.sum(target_ids == 0)
        #     blocks_num_b = np.sum(target_ids == 1)

        #     if len(blocks_a) == 0:
        #         positions_a, container_a, stable_a = None, None, None
        #         feasibility_a = None
        #     else:
        #         # feasibility_a = tools.check_feasibility(blocks_a, blocks_num_a, initial_container_size, order_a, reward_type)
        #         positions_a, container_a, stable_a, _, _ = calc_position_fn(blocks_a, container_size_a, reward_type)
        #         feasibility_a = None
        #     if len(blocks_b) == 0:
        #         positions_b, container_b, stable_b = None, None, None
        #         feasibility_b = None
        #     else:
        #         # feasibility_b = tools.check_feasibility(blocks_b, blocks_num_b, initial_container_size, order_b, reward_type)
        #         positions_b, container_b, stable_b, _, _ = calc_position_fn(blocks_b, container_size_b, reward_type)
        #         feasibility_b = None

        #     if positions_a is None:
        #         C_a = 0
        #         P_a = 0
        #         S_a = 0
        #     else:
        #         C_a = valid_size_a / box_size_a
        #         P_a = valid_size_a / (valid_size_a + empty_num_a)
        #         S_a = stable_num_a / blocks_num_a

        #     if positions_b is None:
        #         C_b = 0
        #         P_b = 0
        #         S_b = 0
        #     else:
        #         C_b = valid_size_b / box_size_b
        #         P_b = valid_size_b / (valid_size_b + empty_num_b)
        #         S_b = stable_num_b / blocks_num_b

        #     if block_dim == 3:
        #         for view in ['front', 'right']:
        #             tools.draw_container_voxel( container_a[:,:,:25], blocks_num,
        #                 blocks_num_to_draw=blocks_num_a,
        #                 order=order_a,
        #                 rotate_state=rotate_state_a,
        #                 reward_type=reward_type,
        #                 view_type=view,
        #                 feasibility=feasibility_a,
        #                 save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C_a, P_a, S_a),
        #                 save_name=save_path[:-4] + '-a-%d' % (i) )
        #             tools.draw_container_voxel( container_b[:,:,:25], blocks_num,
        #                 blocks_num_to_draw=blocks_num_b,
        #                 order=order_b,
        #                 rotate_state=rotate_state_b,
        #                 view_type=view,
        #                 reward_type=reward_type,
        #                 feasibility=feasibility_b,
        #                 save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C_b, P_b, S_b),
        #                 save_name=save_path[:-4] + '-b-%d' % (i) )
        #     else:
        #         tools.draw_container_2d(blocks_a, positions_a, container_size_a,
        #             order=order_a,
        #             rotate_state=rotate_state_a,
        #             stable=stable_a,
        #             reward_type=reward_type,
        #             feasibility=feasibility_a,
        #             save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C_a, P_a, S_a),
        #             save_name=save_path[:-4] + '-a-%d' % (i) )
        #         tools.draw_container_2d(blocks_b, positions_b, container_size_b,
        #             order=order_b,
        #             rotate_state=rotate_state_b,
        #             stable=stable_b,
        #             feasibility=feasibility_b,
        #             reward_type=reward_type,
        #             save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C_b, P_b, S_b),
        #             save_name=save_path[:-4] + '-b-%d' % (i) )

        # else:
        #     positions, container, stable, _, _ = calc_position_fn(blocks, container_size, reward_type)
        #     new_order = []
        #     rotate_state = []
        #     # calc the real index of blocks
        #     for o in order:
        #         if o < blocks_num:
        #             new_order.append(o)
        #             rotate_state.append(False)
        #         else:
        #             tmp = o
        #             while tmp >= blocks_num:
        #                 tmp = tmp-blocks_num
        #             new_order.append(tmp)
        #             rotate_state.append(True)

        #     # feasibility = tools.check_feasibility(my_all_blocks, blocks_num, initial_container_size, order, reward_type)
        #     feasibility = None #tools.check_feasibility(my_all_blocks, blocks_num, initial_container_size, order, reward_type)

        #     order = new_order

        #     if block_dim == 3:
        #         tools.draw_container_voxel( container[:,:,:25], blocks_num, order=order,
        #             reward_type=reward_type,
        #             rotate_state=rotate_state,
        #             feasibility=feasibility,
        #             save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C, P, S),
        #             save_name=save_path[:-4] + '-%d' % (i) )
        #     else:
        #         tools.draw_container_2d(blocks, positions, container_size, reward_type=reward_type,
        #             order=order, rotate_state=rotate_state, stable=stable,
        #             feasibility=feasibility,
        #             save_title="Compactness: %.3f\nPyramidality: %.3f\nStability: %.3f\n" % (C, P, S),
        #             save_name=save_path[:-4] + '-%d' % (i) )

    np.savetxt( save_path[:-13] + '-ratio.txt',             my_ratio)
    np.savetxt( save_path[:-13] + '-valid_size.txt',        my_valid_size)
    np.savetxt( save_path[:-13] + '-box_size.txt',          my_box_size)
    np.savetxt( save_path[:-13] + '-empty_size.txt',        my_empty_size)
    np.savetxt( save_path[:-13] + '-stable_num.txt',        my_stable_num)
    np.savetxt( save_path[:-13] + '-packing_height.txt',    my_packing_height)
    total_time = np.array([valid_time])
    np.savetxt( save_path[:-13] + '-time.txt', total_time)

    ids = tour_indices.cpu().numpy()
    np.savetxt( save_path[:-13] + '-ids.txt', ids)

    # # draw precedence graph
    # if dynamic is not None:
    #     for _i in range(data_len):
    #         if _i >= 6:
    #             break
    #         deps = dynamic[_i][:blocks_num, :blocks_num]

    #         tools.draw_dep(deps, save_name=save_path[:-4] + '-dep-%d' % (_i) )


def read_matrix():
    matrix = []
    total_sum = 0

    row_count, column_count = [int(x) for x in input().split(', ')]

    for current_row in range(row_count):
        row = [int(x) for x in input().split(', ')]
        matrix.append(row)
        total_sum += sum(row)

    return matrix, total_sum

def print_result(matrix, matrix_sum):
    print(matrix_sum)
    print(matrix)

matrix, result = read_matrix()
print_result(matrix, result)

SoftUni/Python homework/advanced/4. multidimensional list/sum_matrix_elements_2.py


"""
SQLite module tests
"""

from txtai.embeddings import Embeddings

from .testrdbms import Common

# pylint: disable=R0904
class TestSQLite(Common.TestRDBMS):
    """
    Embeddings with content stored in SQLite tests.
    """

    @classmethod
    def setUpClass(cls):
        """
        Initialize test data.
        """

        cls.data = [
            "US tops 5 million confirmed virus cases",
            "Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
            "Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
            "The National Park Service warns against sacrificing slower friends in a bear attack",
            "Maine man wins $1M from $25 lottery ticket",
            "Make huge profits without work, earn up to $100,000 a day",
        ]

        # Content backend
        cls.backend = "sqlite"

        # Create embeddings model, backed by sentence-transformers & transformers
        cls.embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2", "content": cls.backend})

    @classmethod
    def tearDownClass(cls):
        """
        Cleanup data.
        """

        if cls.embeddings:
            cls.embeddings.close()

    def testFunction(self):
        """
        Test custom functions
        """

        embeddings = Embeddings(
            {
                "path": "sentence-transformers/nli-mpnet-base-v2",
                "content": self.backend,
                "functions": [{"name": "length", "function": "testdatabase.testsqlite.length"}],
            }
        )

        # Create an index for the list of text
        embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])

        # Search for best match
        result = embeddings.search("select length(text) length from txtai where id = 0", 1)[0]

        self.assertEqual(result["length"], 39)

def length(text):
    """
    Custom SQL function.
    """

    return len(text)


class Employee:

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay

    @property
    def email(self):
        return f'{self.first}.{self.last}@email.com'

    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)

    @fullname.setter
    def fullname(self, name):
        first, last = name.split(' ')
        self.first = first
        self.last = last

emp_1 = Employee('Arch', 'Angelic', 50000)
emp_2 = Employee('Meta', 'Angel', 50000)

print(emp_1.email)
print(emp_1.fullname)
print('~~~~~~~~')
emp_1.fullname = 'FKA Twigs'
print(emp_1.fullname)


import pygame

class Button(pygame.sprite.Sprite):
    def __init__(self):
        self.bot = pygame.image.load("assets/bot.png")
        self.bot = pygame.transform.scale(self.bot, (230, 59))
        self.bot_rect = self.bot.get_rect()
        self.bot_rect.x = 840
        self.bot_rect.y = 280

        self.onevsone = pygame.image.load("assets/1v1.png")
        self.onevsone = pygame.transform.scale(self.onevsone, (230, 59))
        self.onevsone_rect = self.onevsone.get_rect()
        self.onevsone_rect.x = 840
        self.onevsone_rect.y = 400

        self.solo = pygame.image.load("assets/solo.png")
        self.solo = pygame.transform.scale(self.solo, (230, 59))
        self.solo_rect = self.solo.get_rect()
        self.solo_rect.x = 840
        self.solo_rect.y = 520


# -*- coding: UTF-8
"""
Test issue #336: Unicode problem w/ exception traceback on Windows (python2.7)
Default encoding (unicode-escape) of text() causes problems w/
exception tracebacks.

STATUS: BASICS SOLVED (default encoding changed)

ALTERNATIVE SOLUTIONS:
* Use traceback2: Returns unicode-string when calling traceback.
* Use text(traceback.format_exc(), sys.getfilesystemencoding(), "replace")
  where the text-conversion of a traceback is used.
  MAYBE traceback_to_text(traceback.format_exc())
"""

from __future__ import print_function
from behave.textutil import text
import pytest
import six

# require_python2 = pytest.mark.skipif(not six.PY2, reason="REQUIRE: Python2")
# require_python3 = pytest.mark.skipif(six.PY2, reason="REQUIRE: Python3 (or newer)")

class TestIssue(object):
    # -- USE SAVED TRACEBACK: No need to require Windows platform.
    traceback_bytes = br"""\
Traceback (most recent call last):
  File "C:\Users\alice\xxx\behave\model.py", line 1456, in run
    match.run(runner.context)
  File "C:\Users\alice\xxx\behave\model.py", line 1903, in run
    self.func(context, args, *kwargs)
  File "features\steps\my_steps.py", line 210, in step_impl
    directories, task_names, reg_keys)
AssertionError
"""

    traceback_file_line_texts = [
        # -- NOTE: Cannot use: ur'C:\Users ..." => \U is a unicode escape char.
        u'File "C:\\Users\\alice\\xxx\\behave\\model.py", line 1456, in run',
        u'File "C:\\Users\\alice\\xxx\\behave\\model.py", line 1903, in run',
        u'File "features\\steps\\my_steps.py", line 210, in step_impl',
    ]

    # @require_python2
    def test_issue__with_default_encoding(self):
        """Test ensures that problem is fixed with default encoding"""
        text2 = text(self.traceback_bytes)
        assert isinstance(self.traceback_bytes, bytes)
        assert isinstance(text2, six.text_type)
        for file_line_text in self.traceback_file_line_texts:
            assert file_line_text in text2

    # @require_python2
    @pytest.mark.filterwarnings("ignore:invalid escape sequence")
    def test__problem_exists_with_problematic_encoding(self):
        """Test ensures that problem exists with encoding=unicode-escape"""
        # -- NOTE: Explicit use of problematic encoding
        problematic_encoding = "unicode-escape"
        text2 = text(self.traceback_bytes, problematic_encoding)
        print("TEXT: "+ text2)
        assert isinstance(self.traceback_bytes, bytes)
        assert isinstance(text2, six.text_type)

        # -- VERIFY BAD-OUTCOME: With problematic encoding
        file_line_text = self.traceback_file_line_texts[0]
        assert file_line_text not in text2


from django.shortcuts import render
from django.http import HttpResponse
import json

def add(request):
    src1=request.GET.get('name')
    src2=request.GET.get('capital')
    src3={src1:src2}
    file_name='db.json'
    with open(file_name,"r") as file:
        data=json.load(file)
        data["davlatlar"].append(src3)

    with open(file_name,'w') as file:
        json.dump(data,file,indent=4)

    return HttpResponse(f'{data}')

def remove(request):
    DELETE=request.GET.get("key")
    file_name="db.json"
    with open(file_name,"r") as file:
        data=json.load(file)
        KEY=DELETE
        data1=data['davlatlar']
        for i in data1:
            if i.get(KEY)!=None:
                del i[KEY]

    with open(file_name,'w') as file:
        json.dump(data,file,indent=4)

    return HttpResponse(f'delete {KEY}')

def index(request):
    file_name="db.json"
    with open(file_name,"r") as file:
        data=json.load(file)
    return HttpResponse(f'
Davlat Poytaxtlari

 {data}')


#根据数据文件在窗口中动态路径绘制。
import turtle
def main():
	#设置窗口信息
	turtle.title("数据驱动的动态路径绘制")
	turtle.setup(800,600,0,0)
	#设置画笔
	pen = turtle.Turtle()
	pen.color("red")
	pen.width(5)
	pen.shape("turtle")
	pen.speed(20)
	#读取文件
	result = []
	file = open("data.txt","r")
	#处理文件
	for line in file:
		result.append(list(map(float,line.split(','))))
		#1、line.split:把文件一行按逗号隔开的数据变成一个列表，但是每一个数据还是字符串。
		#2、map:map是映射函数，map函数用法大致如下：map(function,iterable)。
		#这里的用处是把字符串的列表变成了浮点型数据的列表。
		#3、因为map()的返回值已经不再是list，而是iterator。所以又用了一个list,将iterator转换成list。
		#4、使用append方法将列表添加到result列表中。
		#print(result)
	#动态绘制
	for i in range(len(result)):#len的值是文件data的行数。
		pen.color((result[i][3],result[i][4],result[i][5]))
		#result是个二维的列表，因为每个元素又是一个列表。
		#第i个元素的最后三个分别是R,G,B的值
		pen.forward(result[i][0])
		if result[i][1]:
			pen.rt(result[i][2])
		else:
			pen.lt(result[i][2])
	pen.goto(0,0)

#if_name_=='_main_'
while True:
        main()


gate1 = [1,1,1,1,1,1,1,1]
hitung = 1

total = gate1.count(hitung)

print("terdapat jenis mobil varian",hitung,"sebanyak",total,"di dalam list",gate1)

jumlah = 1*8
rata = jumlah/8

print ("rata rata mobil yang melewati gate 1 adalah", rata)

Pengumpulan PR/Tobias - 03 April 2021/main9.py