import gtk
import thermo

class SteamSolver:
    def __init__(self):
        '''Initial window which has a combo box from which the user can choose either of     temperature and pressure'''

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(500 , 500)
        self.window.set_title('Steam Solver')
        self.window.connect('delete-event' , gtk.main_quit)
        self.window.modify_bg(gtk.STATE_NORMAL , gtk.gdk.Color(red = 20000, blue = 20000 , green = 20000))
        self.TableCreator()

    def TableCreator(self):
        self.PropertyList =['Pressure' , 'Temperature' , 'Volume(v)' , 'Energy(u)' ,'Enthalpy(h)' , 'Entropy(s)' , 'Quality(x)']
        self.FinalList = []
        self.table = gtk.Table(500 ,500 , True)
        self.label = gtk.Label('Choose any one')
        self.table.attach(self.label , 25 , 225 , 25 , 60 )
        self.box = gtk.combo_box_new_text()
        self.box.append_text('Pressure')
        self.box.append_text('Temperature')
        self.box.connect('changed' , self.PresTemp)
        self.table.attach(self.box , 275 , 450 , 25 , 60)
        self.window.add(self.table)
        self.window.show_all()

    def _helper(self , FinalList):
        try:

            Property1 = self.PresTempEnt.get_text()
            Property2 = self.PropertyEntry.get_text()
            if FinalList[0] == 'P':
                if FinalList[1] == 'Temperature':
                    a = thermo.State(P = float(Property1), T  = float(Property2))
                    return a

                elif FinalList[1] == 'Volume(v)':
                    a = thermo.State(P = float(Property1), v  = float(Property2))
                    return a

                elif FinalList[1] == 'Energy(u)':
                    a = thermo.State(P = float(Property1), u  = float(Property2))
                    return a

                elif FinalList[1] == 'Enthalpy(h)':
                    a = thermo.State(P = float(Property1), h = float(Property2))
                    return a

                elif FinalList[1] == 'Entropy(s)':
                    a = thermo.State(P = float(Property1), s  = float(Property2))
                    return a

                elif FinalList[1] == 'Quality(x)':
                    a = thermo.State(P = float(Property1), x  = float(Property2))
                    return a

            if FinalList[0] == 'T':
                if FinalList[1] == 'Volume(v)':
                    a = thermo.State(P = float(Property1), v  = float(Property2))
                    return a

                elif FinalList[1] == 'Energy(u)':
                    a = thermo.State(P = float(Property1), u  = float(Property2))
                    return a

                elif FinalList[1] == 'Enthalpy(h)':
                    a = thermo.State(P = float(Property1), h = float(Property2))
                    return a

                elif FinalList[1] == 'Entropy(s)':
                    a = thermo.State(P = float(Property1), s  = float(Property2))
                    return a

                elif FinalList[1] == 'Quality(x)':
                    a = thermo.State(P = float(Property1), x  = float(Property2))
                    return a

        except ValueError:
            a = 'Error'
            return a

    def PresTemp(self , widget):
        '''Function called when the combo box is changed , the window gets another label and an
        entry into which the user can tpe the required value of temperature and pressure'''
        index = widget.get_active()
        widget.destroy()
        self.Entry1 = gtk.Entry()
        self.table.attach(self.Entry1 , 275 , 450 , 25 , 60)
        self.PresTempLab = gtk.Label()
        self.PresTempEnt = gtk.Entry()
        self.table.attach(self.PresTempLab , 25 , 225 , 120 , 155)
        self.table.attach(self.PresTempEnt , 275 , 450 , 120 , 155)
        if index == 0:
            self.FinalList.append('P')
            self.PropertyList.remove('Pressure')
            self.PresTempLab.set_text('Enter Pressure')
            self.Entry1.set_text('Pressure')
            self.Entry1.set_editable(False)
        else:
            self.FinalList.append('T')
            self.PropertyList.remove('Temperature')
            self.PresTempLab.set_text('Enter Temperature')
            self.Entry1.set_text('Temperature')
            self.Entry1.set_editable(False)
        self.button = gtk.Button('Process')
        self.button.connect('clicked' , self.Process , index)
        self.table.attach(self.button , 200 , 300 , 420 , 480)
        self.window.show_all()

    def Process(self , widget , index):
        '''Function called by the button Process , Checks whether the input is valid or not
        If the input is valid , then another label and a combo box are added. The user can
        choose any other property other then temperature or pressure which was entered'''
        UserEntry = self.PresTempEnt.get_text()
        self.PresTempEnt.set_editable(False)
        widget.destroy()
        self.SecondStateLab = gtk.Label('Enter Second Property')
        self.Combo = gtk.combo_box_new_text()
        for Property in self.PropertyList:
            self.Combo.append_text(Property)
        self.Combo.connect('changed' , self.SecState)
        self.table.attach(self.SecondStateLab , 25 , 225 , 215 , 250)
        self.table.attach(self.Combo , 275 , 450 , 215 , 250)
        self.window.show_all()

    def SecState(self , widget):
        self.index = widget.get_active()
        self.PropertyEntry = gtk.Entry()
        self.PropertyLabel = gtk.Label('Enter ' + self.PropertyList[self.index])
        self.FinalList.append(self.PropertyList[self.index])
        widget.destroy()
        self.OptionsLabel = gtk.Entry()
        self.OptionsLabel.set_text(self.PropertyList[self.index])
        self.OptionsLabel.set_editable(False)
        self.table.attach(self.OptionsLabel , 275 , 450 , 215 , 250)
        self.table.attach(self.PropertyLabel , 25 , 225 , 310 , 345)
        self.table.attach(self.PropertyEntry , 275 , 450 , 310 , 345)
        self.button2= gtk.Button('Process')
        self.button2.connect('clicked' , self.FinalState)
        self.table.attach(self.button2 , 200 , 300 , 420 , 480)
        self.window.show_all()

    def FinalState(self , widget):
        widget.set_label('Reset')
        a = self._helper(self.FinalList)
        self.label.destroy()
        self.PresTempLab.destroy()
        self.PresTempEnt.destroy()
        self.Entry1.destroy()
        self.PropertyEntry.destroy()
        self.PropertyLabel.destroy()
        self.OptionsLabel.destroy()
        self.SecondStateLab.destroy()
        if a == 'Error':
            self.warning = gtk.Label('P L E A S E  E N T E R  V A L I D  I N P U T')
            self.table.attach(self.warning , 25 , 475 , 25 , 375)
            self.window.show_all()

        else:

            self.TemperatureEntry = gtk.Entry()
            self.TemperatureLabel = gtk.Label('Temperature')
            self.PressureEntry = gtk.Entry()
            self.PressureLabel = gtk.Label('Pressure')
            self.table.attach(self.TemperatureLabel, 25 , 225 , 25 , 55)
            self.table.attach(self.TemperatureEntry , 275 , 450 , 25 , 55)
            self.TemperatureEntry.set_text(str(a.GetTemp()))
            self.TemperatureEntry.set_editable(False)
            self.table.attach(self.PressureLabel, 25 , 225 , 75 , 105)
            self.table.attach(self.PressureEntry , 275 , 450 , 75 , 105)
            self.PressureEntry.set_text(str(a.GetPressure()))
            self.PressureEntry.set_editable(False)
            self.VolumeEntry = gtk.Entry()
            self.VolumeLabel = gtk.Label('Volume(v):')
            self.table.attach(self.VolumeLabel, 25 , 225 , 125 , 155)
            self.table.attach(self.VolumeEntry , 275 , 450 , 125 , 155)
            self.VolumeEntry.set_text(str(a.GetVolume()))
            self.VolumeEntry.set_editable(False)
            self.EnergyEntry = gtk.Entry()
            self.EnergyLabel = gtk.Label('Internal Energy(U):')
            self.table.attach(self.EnergyLabel, 25 , 225 , 175 , 205)
            self.table.attach(self.EnergyEntry , 275 , 450 , 175 , 205)
            self.EnergyEntry.set_text(str(a.GetEnergy()))
            self.EnergyEntry.set_editable(False)
            self.EnthalpyEntry = gtk.Entry()
            self.EnthalpyLabel = gtk.Label('Enthalpy(H):')
            self.table.attach(self.EnthalpyLabel, 25 , 225, 225 , 255)
            self.table.attach(self.EnthalpyEntry , 275 , 450 , 225 , 255)
            self.EnthalpyEntry.set_text(str(a.GetEnthalpy()))
            self.EnthalpyEntry.set_editable(False)
            self.EntropyEntry = gtk.Entry()
            self.EntropyLabel = gtk.Label('Entropy(s):')
            self.table.attach(self.EntropyLabel, 25 , 225 , 275 , 305)
            self.table.attach(self.EntropyEntry , 275 , 450 , 275 , 305)
            self.EntropyEntry.set_text(str(a.GetEntropy()))
            self.EntropyEntry.set_editable(False)
            self.QualityEntry = gtk.Entry()
            self.QualityLabel = gtk.Label('Quality(x):')
            self.table.attach(self.QualityLabel, 25 , 225 , 325 , 355)
            self.table.attach(self.QualityEntry , 275 , 450 ,325 , 355)
            self.QualityEntry.set_text(str(a.GetQuality()))
            self.QualityEntry.set_editable(False)
            self.window.show_all()

        widget.connect('clicked' , self.destroy)

    def destroy(self , widget):
        self.table.destroy()
        self.TableCreator()

class main:
    SteamSolver()
    gtk.main()

if __name__ == '__main__':
    main()


import keras
from keras.datasets import mnist
from keras.models import load_model

def Getdata():
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
    x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

    x_train = x_train / 255
    x_test = x_test / 255

    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)

    data = x_test[0].reshape(-1)
    ans = y_test[0]
    for i in range(28):
        for j in range(28):
            print("%.6f , " % data[i*28+j],end='')
        print('')
    print(ans)
Getdata()


from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext
from .forms import Mp3DetailsForm

from gtts import gTTS

import os
import time

def index(request):

    if request.method == 'POST':
        context = RequestContext(request)
        aform = Mp3DetailsForm(request.POST)

        if aform.is_valid():
            file_name = aform.cleaned_data['file_name'].replace(" ", "")
            text = aform.cleaned_data['text']
            create_sound(file_name, text)
            file_path = '/Users/ivanG/Documents/text_to_speech/Text_to_Speech/' + file_name + ".mp3"

            while not os.path.exists(file_path):
                time.sleep(1)
                if os.path.exists(file_path):
                    break;

            print("file about to be opened")
            fsock = open(file_path, 'r')
            response = HttpResponse(fsock, content_type='audio/mpeg')
            response['Content-Disposition'] = "attachment; filename=%s.mp3" % file_name
            return response

        else:
            aform = Mp3DetailsForm()

        return render(request, 'tts/createmp3.html')

    else:
        form = Mp3DetailsForm()
        context = RequestContext(request)
        return render(request, 'tts/index.html', {'form': form})

def create_sound(file_name, text):
    txt = text
    tts = gTTS(text=txt, lang='en')
    tts.save(file_name + ".mp3")
    return tts


#Tool check active subdomain from amass result

from concurrent.futures import ThreadPoolExecutor
import requests
from requests.exceptions import ConnectionError
import argparse

parser = argparse.ArgumentParser(description='Example argument parser')
parser.add_argument('--input_file','-i', help='path to input file')
parser.add_argument('--output-file', '-o', default='output.txt', help='path to output file')

args = parser.parse_args()

domain_list = []
active_domain = []
def read_file(file):
    with open(file, 'r') as f:
        for url in f:
            url = url.replace('\n','')
            domain_list.append(url)

def validate_existence(domain):
        try:
            response = requests.get(domain, timeout=10)
        except ConnectionError:
            print(f'Domain {domain} [is dead]')
        else:
            print(f'Domain {domain} [is active]')
            active_domain.append(domain)

def write_file():
    with open(args.output_file, 'w') as f:
        for url in active_domain:
            f.write(url + '\n')

read_file(args.input_file)
with ThreadPoolExecutor() as executor:
    executor.map(validate_existence, domain_list)
write_file()


#!/usr/bin/env python3
import re
import csv
import collections

def get_error_message_type(log):
    if re.search(r"ERROR", log):
        return "ERROR"
    if re.search(r"INFO", log):
        return "INFO"

def find_errors():
    error_dict = {}
    with open("syslog.log", "r") as logfile:
        for line in logfile:
            if re.search(r"ERROR", line):
                print(line)
                print("FOUND AN ERROR")
                error_message = str(re.search(r"(ticky).*?\s*\(", line).group(0)).replace(" (", "").replace("ticky: ERROR ", "")
                if error_message not in error_dict:
                    error_dict[error_message] = 0
                error_dict[error_message] += 1
            if re.search(r"INFO", line):
                pass

    return error_dict

def get_per_user_errors():
    per_user_errors = {}
    with open("syslog.log", "r") as logfile:
        for line in logfile:
            username = str(re.search(r"\(.*\)", line).group(0)).replace("(", "").replace(")", "")
            if username not in per_user_errors:
                per_user_errors[username] = {"INFO": 0, "ERROR": 0}
            per_user_errors[username][get_error_message_type(line)] +=1

    return per_user_errors

def write_per_user_dict_to_csv(d):
    with open('user_statistics.csv','w', newline='', encoding='utf-8') as f:
        w = csv.writer(f)
        header = ["Username", "INFO", "ERROR"]
        w.writerow(header)
        od = collections.OrderedDict(sorted(d.items()))
        for k, v in od.items():
            row = [k, v["INFO"], v["ERROR"]]
            w.writerow(row)

def write_error_dict_to_csv(d):
    with open("error_message.csv", "w", newline="", encoding="utf-8") as f:
        w = csv.writer(f)
        header = ["Error", "Count"]
        od = sorted(d.items(), key=lambda x: x[1], reverse=True)
        w.writerow(header)
        for row in od:
            w.writerow(row)

if __name__ == '__main__':
    different_error_messages_dict = find_errors()
    write_error_dict_to_csv(different_error_messages_dict)

    per_user_errors = get_per_user_errors()
    write_per_user_dict_to_csv(per_user_errors)


"""Backend functions for exporting data."""

import os
import boto3
import fsspec
import shutil
import logging
import warnings
import datetime
import xarray as xr
import pandas as pd
from importlib.metadata import version as _version
from botocore.exceptions import ClientError
from climakitae.util.utils import read_csv_file
from climakitae.core.paths import variable_descriptions_csv_path, stations_csv_path

xr.set_options(keep_attrs=True)

bytes_per_gigabyte = 1024 * 1024 * 1024

def _estimate_file_size(data, format):
    """
    Estimate file size in gigabytes when exporting `data` in `format`.

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        data to export to the specified `format`
    format : str
        file format ("NetCDF" or "CSV")

    Returns
    -------
    float
        estimated file size in gigabytes

    """
    if format == "NetCDF":
        if isinstance(data, xr.core.dataarray.DataArray):
            if not data.name:
                # name it in order to call to_dataset on it
                data.name = "data"
            data_size = data.to_dataset().nbytes
        else:  # data is xarray Dataset
            data_size = data.nbytes
        buffer_size = 2 * 1024 * 1024  # 2 MB for miscellaneous metadata
        est_file_size = data_size + buffer_size

    elif format == "CSV":
        pass  # TODO: estimate CSV file size

    return est_file_size / bytes_per_gigabyte

def _warn_large_export(file_size, file_size_threshold=5):
    if file_size > file_size_threshold:
        print(
            "WARNING: Estimated file size is "
            + str(round(file_size, 2))
            + " GB. This might take a while!"
        )

def _list_n_none_to_string(dic):
    """Convert list and None to string."""
    for k, v in dic.items():
        if isinstance(v, list):
            dic[k] = str(v)
        if v is None:
            dic[k] = ""
    return dic

def _update_attributes(data):
    """
    Update data attributes to prevent issues when exporting them to NetCDF.

    Convert list and None attributes to strings. If `time` is a coordinate of
    `data`, remove any of its `units` attribute. Attributes include global data
    attributes as well as that of coordinates and data variables.

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset

    Returns
    -------
    None

    Notes
    -----
    These attribute updates resolve errors raised when using the scipy engine
    to write NetCDF files to S3.
    """
    data.attrs = _list_n_none_to_string(data.attrs)
    for coord in data.coords:
        data[coord].attrs = _list_n_none_to_string(data[coord].attrs)
    if "time" in data.coords and "units" in data["time"].attrs:
        del data["time"].attrs["units"]

    if isinstance(data, xr.core.dataarray.Dataset):
        for data_var in data.data_vars:
            data[data_var].attrs = _list_n_none_to_string(data[data_var].attrs)

def _unencode_missing_value(d):
    """Drop `missing_value` encoding, if any, on data object `d`."""
    try:
        del d.encoding["missing_value"]
    except:
        pass

def _update_encoding(data):
    """
    Update data encodings to prevent issues when exporting them to NetCDF.

    Drop `missing_value` encoding, if any, on `data` as well as its coordinates
    and data variables.

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset

    Returns
    -------
    None

    Notes
    -----
    These encoding updates resolve errors raised when writing NetCDF files to
    S3.
    """
    _unencode_missing_value(data)
    for coord in data.coords:
        _unencode_missing_value(data[coord])

    if isinstance(data, xr.core.dataarray.Dataset):
        for data_var in data.data_vars:
            _unencode_missing_value(data[data_var])

def _create_presigned_url(bucket_name, object_name, expiration=60 * 60 * 24 * 7):
    """
    Generate a presigned URL to share an S3 object.

    Parameters
    ----------
    bucket_name : string
    object_name : string
    expiration : int, optional
        Time in seconds for the presigned URL to remain valid. The default is
        one week.

    Returns
    -------
    string
        Presigned URL. If error, returns None.

    References
    ----------
    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html#presigned-urls

    """
    s3_client = boto3.client("s3")
    try:
        url = s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": bucket_name, "Key": object_name},
            ExpiresIn=expiration,
        )
    except ClientError as e:
        logging.error(e)
        return None

    return url

def _export_to_netcdf(data, save_name):
    """
    Export user-selected data to NetCDF format.

    Export the xarray DataArray or Dataset `data` to a NetCDF file `save_name`.
    If there is enough disk space, the function saves the file locally;
    otherwise, it saves the file to the S3 bucket `cadcat-tmp` and provides a
    URL for download.

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        data to export to NetCDF format
    save_name : string
        desired output file name, including the file extension

    Returns
    -------
    None

    """
    est_file_size = _estimate_file_size(data, "NetCDF")
    disk_space = shutil.disk_usage("./")[2] / bytes_per_gigabyte

    if disk_space > est_file_size:
        path = "./" + save_name

        if os.path.exists(path):
            raise Exception(
                (
                    f"File {save_name} exists. "
                    "Please either delete that file from the work space "
                    "or specify a new file name here."
                )
            )

        print("Alright, exporting specified data to NetCDF.")
        _warn_large_export(est_file_size)
        _update_attributes(data)
        _update_encoding(data)
        comp = dict(_FillValue=None)
        encoding = {coord: comp for coord in data.coords}
        data.to_netcdf(path, encoding=encoding)
        print(
            (
                "Saved! You can find your file in the panel to the left"
                " and download to your local machine from there."
            )
        )

    else:
        path = f"simplecache::{os.environ['SCRATCH_BUCKET']}/{save_name}"

        with fsspec.open(path, "wb") as fp:
            print("Alright, exporting specified data to NetCDF.")
            _warn_large_export(est_file_size)
            _update_attributes(data)
            _update_encoding(data)
            comp = dict(_FillValue=None)
            encoding = {coord: comp for coord in data.coords}
            data.to_netcdf(fp, encoding=encoding)

            download_url = _create_presigned_url(
                bucket_name="cadcat-tmp", object_name=path.split("cadcat-tmp/")[-1]
            )
            print(
                (
                    "Saved! To download the file to your local machine, "
                    "open the following URL in a web browser:"
                    "\n\n"
                    f"{download_url}"
                    "\n\n"
                    "Note: The URL will remain valid for 1 week."
                )
            )

def _get_unit(dataarray):
    """
    Return unit of data variable in `dataarray`, if any, or an empty string.

    Parameters
    ----------
    dataarray : xarray.DataArray

    Returns
    -------
    str

    """
    data_attrs = dataarray.attrs
    if "units" in data_attrs and data_attrs["units"] is not None:
        return data_attrs["units"]
    else:
        return ""

def _ease_access_in_R(column_name):
    """
    Return a copy of the input that can be used in R easily.

    Modify the `column_name` string so that when it is the name of an R data
    table column, the column can be accessed by $. The modified string contains
    no spaces or special characters, and starts with a letter or a dot.

    Parameters
    ----------
    column_name : str

    Returns
    -------
    str

    Notes
    -----
    The input is assumed to be a column name of a pandas DataFrame converted
    from an xarray DataArray or Dataset available on the Cal-Adapt Analytics
    Engine. The conversions are through the to_dataframe method.

    The function acts on one of the display names of the variables:
    https://github.com/cal-adapt/climakitae/blob/main/climakitae/data/variable_descriptions.csv
    or one of the station names:
    https://github.com/cal-adapt/climakitae/blob/main/climakitae/data/hadisd_stations.csv

    """
    return (
        column_name.replace("(", "")
        .replace(")", "")
        .replace(" ", "_")
        .replace("-", "_")
    )

def _update_header(df, variable_unit_map):
    """
    Update data table header to match the given variable names and units.

    Update the header of the DataFrame `df` so that name and unit of the data
    variable contained in each column are as specified in `variable_unit_map`.
    The resulting header starts with a row labeled "variable" holding variable
    names. A 2nd "unit" row include the units associated with the variables.

    Parameters
    ----------
    df : pandas.DataFrame
        data table to update
    variable_unit_map : list of tuple
        list of tuples where each tuple contains the name and unit of the data
        variable in a column of the input data table

    Returns
    -------
    pandas.DataFrame
        data table with updated header

    """
    df.columns = pd.MultiIndex.from_tuples(
        variable_unit_map,
        name=["variable", "unit"],
    )
    df.reset_index(inplace=True)  # simplifies header
    return df

def _dataarray_to_dataframe(dataarray):
    """
    Prepare xarray DataArray for export as CSV file.

    Convert the xarray DataArray `dataarray` to a pandas DataFrame ready to be
    exported to CSV format. The DataArray is converted through its to_dataframe
    method. The DataFrame header is renamed as needed to ease the access of
    columns in R. It is also enriched with the unit associated with the data
    variable in the DataArray.

    Parameters
    ----------
    dataarray : xarray.DataArray
        data to be prepared for export

    Returns
    -------
    pandas.DataFrame
        data ready for export

    """
    if not dataarray.name:
        # name it in order to call to_dataframe on it
        dataarray.name = "data"

    df = dataarray.to_dataframe()

    variable = dataarray.name
    unit = _get_unit(dataarray)
    variable_unit_map = []
    for col in df.columns:
        if col == variable:
            variable_unit_map.append((_ease_access_in_R(col), unit))
        else:
            variable_unit_map.append((_ease_access_in_R(col), ""))

    df = _update_header(df, variable_unit_map)
    return df

def _dataset_to_dataframe(dataset):
    """
    Prepare xarray Dataset for export as CSV file.

    Convert the xarray Dataset `dataset` to a pandas DataFrame ready to be
    exported to CSV format. The Dataset is converted through its to_dataframe
    method. The DataFrame header is renamed as needed to ease the access of
    columns in R. It is also enriched with the units associated with the data
    variables and other non-index variables in the Dataset. If the Dataset
    contains station data, the name of any climate variable associated with
    the station(s) is added to the header as well.

    Parameters
    ----------
    dataset : xarray.Dataset
        data to be prepared for export

    Returns
    -------
    pandas.DataFrame
        data ready for export

    """
    df = dataset.to_dataframe()

    variable_unit_map = [
        (var_name, _get_unit(dataset[var_name])) for var_name in df.columns
    ]
    df = _update_header(df, variable_unit_map)

    # Helpers for adding to header climate variable names associated w/ stations
    station_df = read_csv_file(stations_csv_path)
    station_lst = list(station_df.station)

    def _is_station(name):
        """Return True if `name` is an HadISD station name."""
        return name in station_lst

    variable_description_df = read_csv_file(variable_descriptions_csv_path)
    variable_ids = variable_description_df.variable_id.values

    def _variable_id_to_name(var_id):
        """Convert variable ID to variable name.

        Return the "display_name" associated with the "variable_id" in
        variable_descriptions.csv. If `var_id` is not a "variable_id" in the
        CSV file, return an empty string.
        """
        if var_id in variable_ids:
            var_name_series = variable_description_df.loc[
                variable_ids == var_id, "display_name"
            ]
            var_name = var_name_series.to_list()[0]
            return var_name
        else:
            return ""

    def _get_station_variable_name(dataset, station):
        """Get name of climate variable stored in `dataset` variable `station`.

        Return an empty string if that is not possible.
        """
        try:
            station_da = dataset[station]  # DataArray
            data_attrs = station_da.attrs
            if "variable_id" in data_attrs and data_attrs["variable_id"] is not None:
                var_id = data_attrs["variable_id"]
                var_name = _variable_id_to_name(var_id)
                return var_name
            else:
                return ""
        except:
            return ""

    # Add to header: name of any climate variable associated with stations
    column_names = df.columns.get_level_values(0)
    climate_var_lst = []
    for name in column_names:
        if _is_station(name):
            climate_var = _get_station_variable_name(dataset, station=name)
        else:
            climate_var = ""
        climate_var_lst.append(climate_var)

    if set(climate_var_lst) != {""}:
        # Insert climate variable names to the 2nd row
        header_df = df.columns.to_frame()
        header_df.insert(1, "", climate_var_lst)
        # The 1st row was named "variable" by `_update_header`
        header_df.variable = header_df.variable.map(_ease_access_in_R)
        df.columns = pd.MultiIndex.from_frame(header_df)

    return df

def _export_to_csv(data, output_path):
    """
    Export user-selected data to CSV format.

    Export the xarray DataArray or Dataset `data` to a CSV file at
    `output_path`.

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        data to export to CSV format
    output_path : string
        desired output file path, including the file name and file extension

    Returns
    -------
    None

    """
    print("Alright, exporting specified data to CSV.")

    ftype = type(data)

    if ftype == xr.core.dataarray.DataArray:
        df = _dataarray_to_dataframe(data)

    elif ftype == xr.core.dataset.Dataset:
        df = _dataset_to_dataframe(data)

    # Warn about exceedance of Excel row or column limit
    excel_row_limit = 1048576
    excel_column_limit = 16384
    csv_nrows, csv_ncolumns = df.shape
    if csv_nrows > excel_row_limit or csv_ncolumns > excel_column_limit:
        warnings.warn(
            f"Dataset exceeds Excel limits of {excel_row_limit} rows "
            f"and {excel_column_limit} columns."
        )

    _metadata_to_file(data, output_path)
    df.to_csv(output_path, compression="gzip")

def export(data, filename="dataexport", format="NetCDF"):
    """Save data as a file in the current working directory.

    Parameters
    ----------
    data : xr.DataArray or xr.Dataset
        Data to export, as output by e.g. `climakitae.Select().retrieve()`.
    filename : str, optional
        Output file name (without file extension, i.e. "my_filename" instead
        of "my_filename.nc"). The default is "dataexport".
    format : str, optional
        File format ("NetCDF" or "CSV"). The default is "NetCDF".

    """
    ftype = type(data)

    if ftype not in [xr.core.dataset.Dataset, xr.core.dataarray.DataArray]:
        raise Exception(
            "Cannot export object of type "
            + str(ftype).strip("<class >")
            + ". Please pass an xarray dataset or data array."
        )

    if type(filename) is not str:
        raise Exception(
            (
                "Please pass a string"
                " (any characters surrounded by quotation marks)"
                " for your file name."
            )
        )
    filename = filename.split(".")[0]

    req_format = format.lower()

    if req_format not in ["netcdf", "csv"]:
        raise Exception('Please select "NetCDF" or "CSV" as the file format.')

    extension_dict = {"netcdf": ".nc", "csv": ".csv.gz"}

    save_name = filename + extension_dict[req_format]

    ds_attrs = data.attrs

    ct = datetime.datetime.now()
    ct_str = ct.strftime("%d-%b-%Y (%H:%M)")

    ck_attrs = {
        "Data_exported_from": "Cal-Adapt Analytics Engine",
        "Data_export_timestamp": ct_str,
        "Analysis_package_name": "climakitae",
        "Version": _version("climakitae"),
        "Author": "Cal-Adapt Analytics Engine Team",
        "Author_email": "analytics@cal-adapt.org",
        "Home_page": "https://github.com/cal-adapt/climakitae",
        "License": "BSD 3-Clause License",
    }

    ds_attrs.update(ck_attrs)
    data.attrs = ds_attrs

    # now here is where exporting actually begins
    # we will have different functions for each file type
    # to keep things clean-ish
    if "netcdf" == req_format:
        _export_to_netcdf(data, save_name)
    elif "csv" == req_format:
        output_path = "./" + save_name
        if os.path.exists(output_path):
            raise Exception(
                (
                    f"File {save_name} exists. "
                    "Please either delete that file from the work space "
                    "or specify a new file name here."
                )
            )
        # now check file size and avail workspace disk space
        # raise error for not enough space
        # and warning for large file
        file_size_threshold = 5  # in GB
        disk_space = shutil.disk_usage("./")[2] / bytes_per_gigabyte
        data_size = data.nbytes / bytes_per_gigabyte

        if disk_space <= data_size:
            raise Exception(
                "Not enough disk space to export data! You need at least "
                + str(round(data_size, 2))
                + (
                    " GB free in the hub directory, which has 10 GB total space."
                    " Try smaller subsets of space, time, scenario, and/or"
                    " simulation; pick a coarser spatial or temporal scale;"
                    " or clean any exported datasets which you have already"
                    " downloaded or do not want."
                )
            )

        if data_size > file_size_threshold:
            print(
                "WARNING: xarray data size is "
                + str(round(data_size, 2))
                + " GB. This might take a while!"
            )

        _export_to_csv(data, output_path)
        print(
            (
                "Saved! You can find your file(s) in the panel to the left"
                " and download to your local machine from there."
            )
        )

def _metadata_to_file(ds, output_name):
    """
    Write NetCDF metadata to a txt file so users can still access it
    after exporting to a CSV.
    """

    def _rchop(s, suffix):
        if suffix and s.endswith(suffix):
            return s[: -len(suffix)]
        return s

    output_name = _rchop(output_name, ".csv.gz")

    if os.path.exists(output_name + "_metadata.txt"):
        os.remove(output_name + "_metadata.txt")

    print(
        "NOTE: File metadata will be written in "
        + output_name
        + (
            "_metadata.txt. We recommend you download this along with "
            "the CSV for your records."
        )
    )

    with open(output_name + "_metadata.txt", "w") as f:
        f.write("======== Metadata for CSV file " + output_name + " ========")
        f.write("\n")
        f.write("\n")
        f.write("\n")
        f.write("===== Global file attributes =====")
        if type(ds) == xr.core.dataarray.DataArray:
            f.write("\n")
            f.write("Name: " + ds.name)
        f.write("\n")
        for att_keys, att_values in ds.attrs.items():
            f.write(str(att_keys) + " : " + str(att_values))
            f.write("\n")

        f.write("\n")
        f.write("\n")
        f.write("===== Coordinate descriptions =====")
        f.write("\n")
        f.write("Note: coordinate values are in the CSV")
        f.write("\n")

        for coord in ds.coords:
            f.write("\n")
            f.write("== " + str(coord) + " ==")
            f.write("\n")
            for att_keys, att_values in ds[coord].attrs.items():
                f.write(str(att_keys) + " : " + str(att_values))
                f.write("\n")

        if type(ds) == xr.core.dataset.Dataset:
            f.write("\n")
            f.write("\n")
            f.write("===== Variable descriptions =====")
            f.write("\n")

            for var in ds.data_vars:
                f.write("\n")
                f.write("== " + str(var) + " ==")
                f.write("\n")
                for att_keys, att_values in ds[var].attrs.items():
                    f.write(str(att_keys) + " : " + str(att_values))
                    f.write("\n")

## TMY export functions
def _tmy_header(location_name, df):
    """
    Constructs the header for the TMY output file in .tmy format
    Source: https://www.nrel.gov/docs/fy08osti/43156.pdf (pg. 3)
    """

    # line 1 - site information
    # line 1: USAF, station name quote delimited, station state, time zone, lat, lon, elev (m)
    # line 1: we provide station name, lat, lon, and simulation
    line_1 = "'{0}', {1}, {2}, {3}\n".format(
        location_name,
        df["lat"].values[0],
        df["lon"].values[0],
        df["simulation"].values[0],
    )

    # line 2 - data field name and units, manually setting to ensure matches TMY3 labeling
    line_2 = "Air Temperature at 2m (degC),Dew point temperature (degC),Relative humidity (%),Instantaneous downwelling shortwave flux at bottom (W m-2),Shortwave surface downward diffuse irradiance (W m-2),Instantaneous downwelling longwave flux at bottom (W m-2),Wind speed at 10m (m s-1),Wind direction at 10m (deg),Surface Pressure (Pa)\n"

    headers = [line_1, line_2]

    return headers

def _epw_header(location_name, df):
    """
    Constructs the header for the TMY output file in .epw format
    Source:https://designbuilder.co.uk/cahelp/Content/EnergyPlusWeatherFileFormat.htm#:~:text=The%20EPW%20weather%20data%20format,based%20with%20comma%2Dseparated%20data.
    """

    # line 1 - location
    line_1 = "LOCATION,{0},{1},{2}\n".format(
        location_name, df["lat"].values[0], df["lon"].values[0]
    )

    # line 2 - design conditions, leave blank for now
    line_2 = "DESIGN CONDITIONS\n"

    # line 3 - typical/extreme periods, leave blank for now
    line_3 = "TYPICAL/EXTREME PERIODS\n"

    # line 4 - ground temperatures, leave blank for now
    line_4 = "GROUND TEMPERATURES\n"

    # line 5 - holidays/daylight savings, leap year (yes/no), daylight savings start, daylight savings end, num of holidays
    line_5 = "HOLIDAYS/DAYLIGHT SAVINGS,No,0,0,0\n"

    # line 6 - comments 1, going to include simulation + scenario information here
    line_6 = "COMMENTS 1,Typical meteorological year data produced on the Cal-Adapt: Analytics Engine, Scenario: {0}, Simulation: {1}\n".format(
        df["scenario"].values[0], df["simulation"].values[0]
    )

    # line 7 - comments 2, putting the data variables here manually as they are not specified in epw format, and we are not including all
    line_7 = "COMMENTS 2,Air Temperature at 2m (degC),Dew point temperature (degC),Relative humidity (%),Instantaneous downwelling shortwave flux at bottom (W m-2),Shortwave surface downward diffuse irradiance (W m-2),Instantaneous downwelling longwave flux at bottom (W m-2),Wind speed at 10m (m s-1),Wind direction at 10m (deg),Surface Pressure (Pa)\n"

    # line 8 - data periods, num data periods, num records per hour, data period name, data period start day of week, data period start (Jan 1), data period end (Dec 31)
    line_8 = "DATA PERIODS,1,1,Data,,1/1,12/31\n"

    headers = [line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8]

    return headers

def write_tmy_file(filename_to_export, df, location_name="location", file_ext="tmy"):
    """Exports TMY data either as .epw or .tmy file

    Parameters
    ---------
    filename_to_export (str): Filename string, constructed with station name and simulation
    df (pd.DataFrame): Dataframe of TMY data to export
    location_name (str, optional): Location name string, often station name
    file_ext (str, optional): File extension for export, default is .tmy, options are "tmy" and "epw"

    Returns
    -------
    None
    """

    # check that data passed is a DataFrame object
    if type(df) != pd.DataFrame:
        raise ValueError(
            "The function requires a pandas DataFrame object as the data input"
        )

    # typical meteorological year format
    if file_ext == "tmy":
        path_to_file = filename_to_export + ".tmy"

        with open(path_to_file, "w") as f:
            f.writelines(_tmy_header(location_name, df))  # writes required header lines
            df = df.drop(
                columns=["simulation", "lat", "lon", "scenario"]
            )  # drops header columns from df
            dfAsString = df.to_csv(sep=",", header=False, index=False)
            f.write(dfAsString)  # writes file
        print(
            "TMY data exported to .tmy format with filename {}.tmy".format(
                filename_to_export
            )
        )

    # energy plus weather format
    elif file_ext == "epw":
        path_to_file = filename_to_export + ".epw"
        with open(path_to_file, "w") as f:
            f.writelines(_epw_header(location_name, df))  # writes required header lines
            df = df.drop(
                columns=["simulation", "lat", "lon", "scenario"]
            )  # drops header columns from df
            dfAsString = df.to_csv(sep=",", header=False, index=False)
            f.write(dfAsString)  # writes file
        print(
            "TMY data exported to .epw format with filename {}.epw".format(
                filename_to_export
            )
        )

    else:
        print('Please pass either "tmy" or "epw" as a file format for export.')


# Import os to set API key
import os
# Import OpenAI as main LLM service
from langchain.llms import OpenAI
from langchain.embeddings import OpenAIEmbeddings
# Bring in streamlit for UI/app interface
import streamlit as st

# Read the markdown content from a file
with open("styles.md", "r") as f:
    markdown_content = f.read()
st.markdown(markdown_content, unsafe_allow_html=True)

# Import PDF document loaders...there's other ones as well!
from langchain.document_loaders import PyPDFLoader
# Import chroma as the vector store
from langchain.vectorstores import Chroma

# Import vector store stuff
from langchain.agents.agent_toolkits import (
    create_vectorstore_agent,
    VectorStoreToolkit,
    VectorStoreInfo
)

# Set APIkey for OpenAI Service
with open("api_key.txt", "r") as file:
    api_key = file.read().strip()
os.environ['OPENAI_API_KEY'] = api_key

# Create instance of OpenAI LLM
llm = OpenAI(temperature=0.1, verbose=True)
embeddings = OpenAIEmbeddings()

# Create and load PDF Loader
loader = PyPDFLoader('FinanceReport.pdf')
# Split pages from pdf
pages = loader.load_and_split()
# Load documents into vector database aka ChromaDB
store = Chroma.from_documents(pages, embeddings, collection_name='Report2023')

# Create vectorstore info object - metadata repo?
vectorstore_info = VectorStoreInfo(
    name="finance_report",
    description="a finance report as a pdf",
    vectorstore=store
)
# Convert the document store into a langchain toolkit
toolkit = VectorStoreToolkit(vectorstore_info=vectorstore_info)

# Add the toolkit to an end-to-end LC
agent_executor = create_vectorstore_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True
)

# Create a text input box for the user
prompt = st.text_input('Input your request here')

# If the user hits enter
if prompt:
    # Then pass the prompt to the LLM
    response = agent_executor.run(prompt)
    # ...and write it out to the screen
    st.write(response)


# Powerful digit counts

# Problem 63
# The 5-digit number, 16807=7**5, is also a fifth power. Similarly, the 9-digit number, 134217728=8**9, is a ninth power.
# How many n-digit positive integers exist which are also an nth power?
# https://projecteuler.net/problem=63

import datetime

start_time = datetime.datetime.now()
# counting the first pow for 1-9 numbers - we already have 9 at the start
iCount = 9
for i in range(2, 10):
    pow_i = i
    bFound = False
    # starts with square (pow = 2)
    for j in range(2, 100):
        pow_i *= i
        s = str(pow_i)
        if len(s) == j:
            iCount += 1
            bFound = True
        else:
            if bFound:
                break

stop_time = datetime.datetime.now()
print(stop_time - start_time)

print(iCount)


import os

from kivy.app import App
from kivy.core.window import Window
from kivy.metrics import dp
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView

class SquareImage(Image):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.allow_stretch = True
        self.keep_ratio = False
        self.size_hint_y = None
        self.height = dp(self.width)

class MyNewGallery(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        my_scroll = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        self.my_gallery_grid = GridLayout(cols=3, spacing=dp(5), padding=dp(5),
                                          size_hint=(1, None), height=1000)
        self.add_images_to_gallery()
        my_scroll.add_widget(self.my_gallery_grid)

        self.add_widget(my_scroll)

    def add_images_to_gallery(self, *args):
        path = "/home/petchorine/Desktop/monPyhon/mes_projets_python/Double_view/Images"
        for filename in os.listdir(path):
            if os.path.isfile(os.path.join(path, filename)):
                img = SquareImage(source=f"/home/petchorine/Desktop/monPyhon/mes_projets_python/Double_view/Images/{filename}")
                self.my_gallery_grid.add_widget(img)

    def refresh_gallery(self, *args):
        self.my_gallery_grid.clear_widgets()
        self.add_images_to_gallery()

class MenuB(Screen):
    pass

class MyScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.screen1 = MyNewGallery(name="screen1")
        screen2 = MenuB(name="screen2")

        btn_to_screen2 = Button(text="to screen 2",
                                size_hint = (None, None),
                                size = (100,100),
                                pos_hint = {"right": 1})
        btn_to_screen2.bind(on_press=self.to_b)
        self.screen1.add_widget(btn_to_screen2)

        btn_to_gallery = Button(text="to gallery",
                                size_hint=(None, None),
                                size=(100, 100),
                                pos_hint={"left": 1})
        btn_to_gallery.bind(on_press=self.to_gallery)
        screen2.add_widget(btn_to_gallery)

        self.add_widget(self.screen1)
        self.add_widget(screen2)

    def to_b(self, *args):
        self.transition.direction = "left"
        self.current = "screen2"

    def to_gallery(self, *args):
        self.transition.direction = "right"
        self.current = "screen1"

        self.screen1.refresh_gallery()

class RefreshGalleryApp(App):
    def build(self):
        Window.size = (350,700)
        return MyScreenManager()

RefreshGalleryApp().run()


from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton

from pizza.controller.ControllorePizza import ControllorePizza

class VistaEliminaPizza(QWidget):
    def __init__(self, pizza, elimina_pizza, elimina_callback, parent=None):
        super(VistaEliminaPizza, self).__init__(parent)
        self.controllore = ControllorePizza(pizza)
        self.elimina_pizza = elimina_pizza
        self.elimina_callback = elimina_callback

        v_layout = QVBoxLayout()
        label = QLabel("Vuoi eliminare la pizza " + pizza.nome + "?")
        font = label.font()
        font.setPointSize(18)
        label.setFont(font)
        v_layout.addWidget(label)

        delete_btn = QPushButton("Elimina")
        delete_btn.clicked.connect(self.elimina)
        v_layout.addWidget(delete_btn)

        self.setLayout(v_layout)
        self.resize(150, 300)
        self.setWindowTitle("Elimina pizza")

    def elimina(self):
        self.elimina_pizza()
        self.elimina_callback()
        self.close()


#!/usr/bin/python3

import datetime, glob, os, subprocess, unittest

from slippi import Game, parse
from slippi.id import CSSCharacter, InGameCharacter, Item, Stage
from slippi.log import log
from slippi.metadata import Metadata
from slippi.event import Buttons, Direction, End, Frame, Position, Start, Triggers, Velocity
from slippi.parse import ParseEvent

BPhys = Buttons.Physical
BLog = Buttons.Logical

def norm(f):
    return 1 if f > 0.01 else -1 if f < -0.01 else 0

def path(name):
    return os.path.join(os.path.dirname(__file__), 'replays', name + '.slp')

class TestGame(unittest.TestCase):
    def __init__(self, *args, **kwargs) -> None:
        self.pkgname: str = "slippi"
        super().__init__(*args, **kwargs)
        my_env = os.environ.copy()
        self.pypath: str = my_env.get("PYTHONPATH", os.getcwd())
        self.mypy_opts: List[str] = ['--ignore-missing-imports']

    def _game(self, name):
        return Game(path(name))

    def _stick_seq(self, game):
        pass

    # not used yet because the recorder currently puts bogus values in triggers.physical
    def _trigger_seq(self, game):
        last_triggers = None
        trigger_seq = []
        for frame in game.frames:
            t = frame.ports[0].leader.pre.triggers
            t = Triggers(norm(t.logical), norm(t.physical.l), norm(t.physical.r))
            if (t.logical or t.physical.l or t.physical.r) and t != last_triggers:
                trigger_seq.append(t)
            last_triggers = t
        return trigger_seq

    def _button_seq(self, game):
        last_buttons = None
        button_seq = []
        for frame in game.frames:
            b = frame.ports[0].leader.pre.buttons
            if (b.logical or b.physical) and b != last_buttons:
                button_seq.append(b)
            last_buttons = b
        return button_seq

    def test_run_mypy_module(self):
        """Run mypy on all module sources"""
        mypy_call: List[str] = ["mypy"] + self.mypy_opts + ["-p", self.pkgname]
        browse_result: int = subprocess.call(mypy_call, env=os.environ, cwd=self.pypath)
        self.assertEqual(browse_result, 0, 'mypy on slippi')

    def test_run_mypy_tests(self):
        """Run mypy on all tests in module under the tests directory"""
        for test_file in glob.iglob(f'{os.getcwd()}/tests/*.py'):
            mypy_call: List[str] = ["mypy"] + self.mypy_opts + [test_file]
            test_result: int = subprocess.call(mypy_call, env=os.environ, cwd=self.pypath)
            self.assertEqual(test_result, 0, f'mypy on test {test_file}')

    def test_slippi_old_version(self):
        game = self._game('v0.1')
        self.assertEqual(game.start.slippi.version, Start.Slippi.Version(0,1,0,0))
        self.assertEqual(game.metadata.duration, None)
        self.assertEqual(game.metadata.players, (None, None, None, None))
        self.assertEqual(game.start.players[0].character, CSSCharacter.FOX)
        self.assertEqual(game.start.players[1].character, CSSCharacter.GANONDORF)

    def test_game(self):
        game = self._game('game')

        self.assertEqual(game.metadata, Metadata._parse({
            'startAt': '2018-06-22T07:52:59Z',
            'lastFrame': 5085,
            'playedOn': 'dolphin',
            'players': {
                '0': {'characters': {InGameCharacter.MARTH: 5209}},
                '1': {'characters': {InGameCharacter.FOX: 5209}}}}))
        self.assertEqual(game.metadata, Metadata(
            date=datetime.datetime(2018, 6, 22, 7, 52, 59, 0, datetime.timezone.utc),
            duration=5209,
            platform=Metadata.Platform.DOLPHIN,
            players=(
                Metadata.Player({InGameCharacter.MARTH: 5209}),
                Metadata.Player({InGameCharacter.FOX: 5209}),
                None, None)))

        self.assertEqual(game.start, Start(
            is_teams=False,
            random_seed=3803194226,
            slippi=Start.Slippi(Start.Slippi.Version(1,0,0,0)),
            stage=Stage.YOSHIS_STORY,
            players=(
                Start.Player(character=CSSCharacter.MARTH, type=Start.Player.Type.HUMAN, stocks=4, costume=3, team=None, ucf=Start.Player.UCF(False, False)),
                Start.Player(character=CSSCharacter.FOX, type=Start.Player.Type.CPU, stocks=4, costume=0, team=None, ucf=Start.Player.UCF(False, False)),
                None, None)))

        self.assertEqual(game.end, End(End.Method.CONCLUSIVE))

        self.assertEqual(game.metadata.duration, len(game.frames))

    def test_game_skip_frames(self):
        game = Game(path('game'), skip_frames=True)

        self.assertEqual(game.metadata, Metadata._parse({
            'startAt': '2018-06-22T07:52:59Z',
            'lastFrame': 5085,
            'playedOn': 'dolphin',
            'players': {
                '0': {'characters': {InGameCharacter.MARTH: 5209}},
                '1': {'characters': {InGameCharacter.FOX: 5209}}}}))
        self.assertEqual(game.metadata, Metadata(
            date=datetime.datetime(2018, 6, 22, 7, 52, 59, 0, datetime.timezone.utc),
            duration=5209,
            platform=Metadata.Platform.DOLPHIN,
            players=(
                Metadata.Player({InGameCharacter.MARTH: 5209}),
                Metadata.Player({InGameCharacter.FOX: 5209}),
                None, None)))

        self.assertEqual(game.start, Start(
            is_teams=False,
            random_seed=3803194226,
            slippi=Start.Slippi(Start.Slippi.Version(1,0,0,0)),
            stage=Stage.YOSHIS_STORY,
            players=(
                Start.Player(character=CSSCharacter.MARTH, type=Start.Player.Type.HUMAN, stocks=4, costume=3, team=None, ucf=Start.Player.UCF(False, False)),
                Start.Player(character=CSSCharacter.FOX, type=Start.Player.Type.CPU, stocks=4, costume=0, team=None, ucf=Start.Player.UCF(False, False)),
                None, None)))

        self.assertEqual(game.end, End(End.Method.CONCLUSIVE))

        self.assertFalse(game.frames)

    def test_ics(self):
        game = self._game('ics')
        self.assertEqual(game.metadata.players[0].characters, {
            InGameCharacter.NANA: 344,
            InGameCharacter.POPO: 344})
        self.assertEqual(game.start.players[0].character, CSSCharacter.ICE_CLIMBERS)
        self.assertIsNotNone(game.frames[0].ports[0].follower)

    def test_ucf(self):
        self.assertEqual(self._game('shield_drop').start.players[0].ucf, Start.Player.UCF(dash_back=False, shield_drop=True))
        self.assertEqual(self._game('dash_back').start.players[0].ucf, Start.Player.UCF(dash_back=True, shield_drop=False))

    def test_buttons_lrzs(self):
        game = self._game('buttons_lrzs')
        self.assertEqual(self._button_seq(game), [
            Buttons(BLog.TRIGGER_ANALOG, BPhys.NONE),
            Buttons(BLog.TRIGGER_ANALOG|BLog.L, BPhys.L),
            Buttons(BLog.TRIGGER_ANALOG, BPhys.NONE),
            Buttons(BLog.TRIGGER_ANALOG|BLog.R, BPhys.R),
            Buttons(BLog.TRIGGER_ANALOG|BLog.A|BLog.Z, BPhys.Z),
            Buttons(BLog.START, BPhys.START)])

    def test_buttons_abxy(self):
        game = self._game('buttons_abxy')
        self.assertEqual(self._button_seq(game), [
            Buttons(BLog.A, BPhys.A),
            Buttons(BLog.B, BPhys.B),
            Buttons(BLog.X, BPhys.X),
            Buttons(BLog.Y, BPhys.Y)])

    def test_dpad_udlr(self):
        game = self._game('dpad_udlr')
        self.assertEqual(self._button_seq(game), [
            Buttons(BLog.DPAD_UP, BPhys.DPAD_UP),
            Buttons(BLog.DPAD_DOWN, BPhys.DPAD_DOWN),
            Buttons(BLog.DPAD_LEFT, BPhys.DPAD_LEFT),
            Buttons(BLog.DPAD_RIGHT, BPhys.DPAD_RIGHT)])

    def test_cstick_udlr(self):
        game = self._game('cstick_udlr')
        self.assertEqual(self._button_seq(game), [
            Buttons(BLog.CSTICK_UP, BPhys.NONE),
            Buttons(BLog.CSTICK_DOWN, BPhys.NONE),
            Buttons(BLog.CSTICK_LEFT, BPhys.NONE),
            Buttons(BLog.CSTICK_RIGHT, BPhys.NONE)])

    def test_joystick_udlr(self):
        game = self._game('joystick_udlr')
        self.assertEqual(self._button_seq(game), [
            Buttons(BLog.JOYSTICK_UP, BPhys.NONE),
            Buttons(BLog.JOYSTICK_DOWN, BPhys.NONE),
            Buttons(BLog.JOYSTICK_LEFT, BPhys.NONE),
            Buttons(BLog.JOYSTICK_RIGHT, BPhys.NONE)])

    def test_nintendont(self):
        game = self._game('nintendont')
        self.assertEqual(game.metadata.platform, Metadata.Platform.NINTENDONT)

    def test_netplay_name(self):
        game = self._game('netplay')
        players = game.metadata.players
        self.assertEqual(players[0].netplay, Metadata.Player.Netplay(code='ABCD#123', name='abcdefghijk'))
        self.assertEqual(players[1].netplay, Metadata.Player.Netplay(code='XX#000', name='nobody'))

    def test_console_name(self):
        game = self._game('console_name')
        self.assertEqual(game.metadata.console_name, 'Station 1')

    def test_metadata_json(self):
        game = self._game('game')
        self.assertEqual(game.metadata_raw, {
            'lastFrame': 5085,
            'playedOn': 'dolphin',
            'players': {
                '0': {'characters': {'18': 5209}},
                '1': {'characters': {'1': 5209}}},
            'startAt': '2018-06-22T07:52:59Z'})

    def test_v2(self):
        game = self._game('v2.0')
        self.assertEqual(game.start.slippi.version, Start.Slippi.Version(2,0,1))

    def test_unknown_event(self):
        with self.assertLogs(log, 'INFO') as log_context:
            game = self._game('unknown_event')
        self.assertEqual(log_context.output, ['INFO:root:ignoring unknown event type: 0xff'])

    def test_items(self):
        game = self._game('items')
        items = {}
        for f in game.frames:
            for i in f.items:
                if not i.spawn_id in items:
                    items[i.spawn_id] = i
        self.assertEqual(items, {
            0: Frame.Item(
                damage=0,
                direction=Direction.RIGHT,
                position=Position(-62.7096061706543, -1.4932749271392822),
                spawn_id=0,
                state=0,
                timer=140.0,
                type=Item.PEACH_TURNIP,
                velocity=Velocity(0.0, 0.0)),
            1: Frame.Item(
                damage=0,
                direction=Direction.LEFT,
                position=Position(20.395559310913086, -1.4932749271392822),
                spawn_id=1,
                state=0,
                timer=140.0,
                type=Item.PEACH_TURNIP,
                velocity=Velocity(0.0, 0.0)),
            2: Frame.Item(
                damage=0,
                direction=Direction.RIGHT,
                position=Position(-3.982539176940918, -1.4932749271392822),
                spawn_id=2,
                state=0,
                timer=140.0,
                type=Item.PEACH_TURNIP,
                velocity=Velocity(0.0, 0.0))})

class TestParse(unittest.TestCase):
    def test_parse(self):
        metadata = None
        def set_metadata(x):
            nonlocal metadata
            metadata = x
        parse(path('game'), {ParseEvent.METADATA: set_metadata})
        self.assertEqual(metadata.duration, 5209)

if __name__ == '__main__':
    unittest.main()


import gc
import numpy as np
import pandas as pd
import tensorflow as tf
import os
import cv2
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.style.use("seaborn-v0_8")
from tqdm import tqdm

# pathlib to my loccal file

files = os.listdir("/home/michael/Desktop/archive/dataset/")
files
# initialisation of array
image_array = []  # it's a list later i will convert it to array
label_array = []
path = "/home/michael/Desktop/archive/dataset/"
# loop through each sub-folder in train data
for i in range(len(files)):
    # files in sub-folder
    file_sub = os.listdir(path + files[i])

    for k in tqdm(range(len(file_sub))):
        try:
            img = cv2.imread(path + files[i] + "/" + file_sub[k])
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = cv2.resize(img, (96, 96))
            image_array.append(img)
            label_array.append(i)
        except:
            pass

# collecting garbae
gc.collect()
# importing process utils
import psutil

print(psutil.virtual_memory())
# image arrays
image_array = np.array(image_array) / 255.0
label_array = np.array(label_array)
# importing sckit data
from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(image_array, label_array, test_size=0.15)
from keras import layers, callbacks, utils, applications, optimizers
from keras.models import Sequential, Model, load_model

# checking file length
len(files)
model = Sequential()
# I will use MobileNetV2 as an pretrained model
pretrained_model = tf.keras.applications.EfficientNetB0(input_shape=(96, 96, 3), include_top=False,
                                                        weights="imagenet")
model.add(pretrained_model)
model.add(layers.GlobalAveragePooling2D())
# add dropout to increase accuracy by not overfitting
model.add(layers.Dropout(0.3))
# add dense layer as final output
model.add(layers.Dense(1))
model.summary()
# model seuen

model.compile(optimizer="adam", loss="mean_squared_error", metrics=["mae"])
# creating a chechpoint to save model at best accuarcy

ckp_path = "trained_model/model"
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=ckp_path,
                                                      monitor="val_mae",
                                                      mode="auto",
                                                      save_best_only=True,
                                                      save_weights_only=True)

# create a lr reducer which decrease learning rate when accuarcy does not increase
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(factor=0.9, monitor="val_mae",
                                                 mode="auto", cooldown=0,
                                                 patience=5, verbose=1, min_lr=1e-6)
# patience : wait till 5 epoch
# verbose : show accuracy every 1 epoch
# min_lr=minimum learning rate

# model train_test_split
EPOCHS = 300
BATCH_SIZE = 64

history = model.fit(X_train,
                    Y_train,
                    validation_data=(X_test, Y_test),
                    batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
                    callbacks=[model_checkpoint, reduce_lr]
                    )

model.load_weights(ckp_path)

# converting
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

prediction_val = model.predict(X_test, batch_size=BATCH_SIZE)

# p@rediction
prediction_val[:20]

Y_test[:20]

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('hey am coding using python')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/


import logging
import boto3
class SESService():

    def __init__(self):
        self.client = boto3.client(
            'ses',
            aws_access_key_id="INPUT YOUR ACESS KEY FROM AWS",
            aws_secret_access_key="INPUT YOUR SECRET ACESS KEY FROM AWS",
            region_name="INPUT YOUR REGION",
        )

    @staticmethod
    def __verify_status(*email):
        approved_message = 'We are happy to inform you that your application was approved'
        rejected_message = 'We regret to inform you that your application was rejected'
        application = 'Thank you for applying. We will get back to you as soon as possible!'
        status = application
        if len(email) > 1 and email[1] == 'Approved':
            status = approved_message
        elif len(email) > 1 and email[1] == 'Rejected':
            status = rejected_message
        return status

    def send_email(self, *email):
        message = self.__verify_status(*email)
        response = self.client.send_email(
            Source='bignightmare1@gmail.com',
            Destination={
                'ToAddresses': [
                    email[0],
                ],

            },
            Message={
                'Subject': {
                    'Data': f'Welcome to Interview Management System {email[0]}!',
                    'Charset': 'UTF-8'
                },

                'Body': {
                    'Text': {

                        'Data': f'{message}',
                        'Charset': 'UTF-8'
                    }

                }
            },
        )
        logging.info(f'Email sent {response}')


import cv2
from cv2 import VideoWriter_fourcc
import os
import numpy as np
from tqdm import tqdm

import skvideo.io
import argparse

if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        description='create videos based on images')
    parser.add_argument('--img_path', '-in',
                        help='output result file in pickle format')
    parser.add_argument('--video_path', '-out',
                        help='output result file in pickle format')
    args = parser.parse_args()

    img_dir = args.img_path  # "/mnt/lustre/mig.cla/users/fanglj/projects/detr3d/work_dirs/internal_detr3d_res101_scale05_range75/results_epoch_20/save_figs"
    savepath = args.video_path  # "/mnt/lustre/mig.cla/users/fanglj/projects/detr3d/work_dirs/internal_detr3d_res101_scale05_range75/results_epoch_20/save_videos/internal_results.mp4"

    if not os.path.exists(os.path.split(savepath)[0]):
        os.makedirs(os.path.split(savepath)[0], exist_ok=True)

    video_fps = 10
    video_writer = skvideo.io.FFmpegWriter(
        savepath,
        inputdict={
            '-r':
                str(video_fps),
            '-s':
                '{}x{}'.format(int(1200),
                               int(800))
        },
        outputdict={
            '-r': str(video_fps),
            '-vcodec': 'libx264'
        })

    img_list = os.listdir(img_dir)
    img_list = sorted(img_list)
    for name in tqdm(img_list):
        image = cv2.imread(os.path.join(img_dir, name))
        image = cv2.resize(image, (1200, 800), interpolation=cv2.INTER_NEAREST)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        video_writer.writeFrame(image)
    video_writer.close()


import logging
from enum import Enum
from typing import Optional

import pandas as pd
from pandas import DataFrame

import config
from asseeibot import runtime_variables
from asseeibot.models.cache import Cache
from asseeibot.models.fuzzy_match import FuzzyMatch, MatchBasedOn
from asseeibot.models.ontology_dataframe import OntologyDataframeColumn
from asseeibot.models.wikimedia.wikidata.entity import EntityId

logger = logging.getLogger(__name__)

# This code is adapted from https://github.com/dpriskorn/WikidataMLSuggester and LexUtils

# lookups where inspired by
# https://stackoverflow.com/questions/24761133/pandas-check-if-row-exists-with-certain-values

class CacheDataframeColumn(Enum):
    QID = "qid"
    CROSSREF_SUBJECT = "crossref_subject"
    MATCH_BASED_ON = "match_based_on"
    SPLIT_SUBJECT = "split_subject"
    ORIGINAL_SUBJECT = "original_subject"

class MatchCache(Cache):
    """This models our cache of matches"""
    crossref_subject_found: bool = None
    dataframe: DataFrame = None
    qid_dropped: bool = None
    qid_found: bool = None
    pickle: str = config.cache_pickle_filename

    class Config:
        # Because of DataFrame
        arbitrary_types_allowed = True

    def __check_variables__(self):
        logger.debug("Checking variables")
        if self.match.qid is None:
            raise ValueError("match.qid was None")
        if self.match.original_subject is None:
            raise ValueError("match.original_subject was None")
        if self.match.crossref_subject is None:
            raise ValueError("match.crossref_subject was None")
        if self.match.split_subject is None:
            raise ValueError("match.split_subject was None")
        if self.match.match_based_on is None:
            raise ValueError("match.match_based_on was None")

    def __append_new_match_to_the_dataframe__(self):
        self.__check_variables__()
        logger.debug("Adding to cache")
        data = {
            CacheDataframeColumn.QID.value: self.match.qid.value,
            CacheDataframeColumn.CROSSREF_SUBJECT.value: self.match.crossref_subject,
            CacheDataframeColumn.MATCH_BASED_ON.value: self.match.match_based_on.value,
            CacheDataframeColumn.ORIGINAL_SUBJECT.value: self.match.original_subject,
            CacheDataframeColumn.SPLIT_SUBJECT.value: self.match.split_subject,
        }
        if self.dataframe is None:
            self.dataframe = pd.DataFrame(data=[data])
        else:
            self.dataframe = self.dataframe.append(pd.DataFrame(data=[data]))

    def __check_crossref_subject__(self):
        if self.match.crossref_subject is None:
            raise ValueError("crossref_subject was None")
        if self.match.crossref_subject == "":
            raise ValueError("crossref_subject was empty string")

    def __check_qid__(self):
        if self.match.qid is None:
            raise ValueError("qid was None")

    def __check_if_drop_was_successful__(self):
        if config.loglevel == logging.DEBUG:
            logging.debug("Checking if the qid is still in the cache")
            match = (self.dataframe[CacheDataframeColumn.QID.value] == self.match.qid.value).any()
            logger.debug(f"match:{match}")
            print(self.dataframe.info())
            logger.debug(f"Saving pickle without {self.match.qid.value}")

    def __drop_qid_from_dataframe__(self):
        self.__read_dataframe_from_disk__()
        # This tests whether any row matches
        match = (self.dataframe[CacheDataframeColumn.QID.value] == self.match.qid.value).any()
        logger.debug(f"match:{match}")
        if match:
            logger.debug("Deleting the item from the cache now")
            self.dataframe = self.dataframe[self.dataframe[CacheDataframeColumn.QID.value] != self.match.qid.value]
            self.qid_dropped = True
        else:
            self.qid_dropped = False

    def __extract_match__(self):
        """Here we find the row that matches and extract the
        result column and extract the value using any()
        """
        logger.info("Extracting match from cache")
        found_match = self.dataframe.loc[
            self.dataframe[CacheDataframeColumn.CROSSREF_SUBJECT.value] == self.match.crossref_subject].any()
        logger.debug(f"result:{found_match}")
        if found_match is not None:
            logger.info("Already matched QID found in the cache")
            row: DataFrame = self.dataframe[
                self.dataframe[CacheDataframeColumn.CROSSREF_SUBJECT.value] == self.match.crossref_subject
            ]
            if isinstance(row, DataFrame):
                # print(row)
                # print(row[CacheDataframeColumn.QID.value].values[0])
                # exit()
                qid: EntityId = EntityId(row[CacheDataframeColumn.QID.value][0])
                original_subject: str = row[CacheDataframeColumn.ORIGINAL_SUBJECT.value][0]
                crossref_subject: str = row[CacheDataframeColumn.ORIGINAL_SUBJECT.value][0]
                match_based_on = MatchBasedOn(row[CacheDataframeColumn.MATCH_BASED_ON.value][0])
                split_subject: bool = bool(row[CacheDataframeColumn.SPLIT_SUBJECT.value][0])
                ontology_dataframe = runtime_variables.ontology_dataframe
                label = ontology_dataframe.loc[
                    ontology_dataframe[OntologyDataframeColumn.ITEM.value] == qid.url(),
                    OntologyDataframeColumn.LABEL.value].head(1).values[0]
                description = ontology_dataframe.loc[
                    ontology_dataframe[OntologyDataframeColumn.ITEM.value] == qid.url(),
                    OntologyDataframeColumn.DESCRIPTION.value].head(1).values[0]
                alias = ontology_dataframe.loc[
                    ontology_dataframe[OntologyDataframeColumn.ITEM.value] == qid.url(),
                    OntologyDataframeColumn.ALIAS.value].head(1).values[0]
                if label is None:
                    raise ValueError("label was None")
                self.match = FuzzyMatch(
                    qid=qid,
                    original_subject=original_subject,
                    match_based_on=match_based_on,
                    split_subject=split_subject,
                    crossref_subject=crossref_subject,
                    alias=alias,
                    label=label,
                    description=description,
                )
                # print(self.match)
                # exit()
            else:
                logger.error("Did not get pandas dataframe, got")
                print(row)
                exit()

    def __lookup_crossref_subject__(self):
        if len(self.dataframe) > 0:
            match = (self.dataframe[CacheDataframeColumn.CROSSREF_SUBJECT.value] == self.match.crossref_subject).any()
            logger.debug(f"match:{match}")
            self.crossref_subject_found = match
        else:
            self.crossref_subject_found = False

    def __lookup_qid__(self):
        match = (self.dataframe[CacheDataframeColumn.QID.value] == self.match.qid.value).any()
        logger.debug(f"match:{match}")
        self.qid_found = match

    def read(self) -> Optional[FuzzyMatch]:
        """Returns None or result from the cache"""
        self.__check_crossref_subject__()
        self.__verify_that_the_cache_file_exists_and_read__()
        self.__lookup_crossref_subject__()
        if self.crossref_subject_found:
            self.__extract_match__()
            return self.match

    def add(self) -> bool:
        """Add a match to the cache
        It returns True if it was added and False if either QID
        or the crossref subject was found in the cache.
        :return: bool"""
        self.__check_crossref_subject__()
        self.__check_qid__()
        self.__verify_that_the_cache_file_exists_and_read__()
        self.__lookup_crossref_subject__()
        if not self.crossref_subject_found and not self.qid_found:
            self.__append_new_match_to_the_dataframe__()
            self.__save_dataframe_to_disk__()
            return True
        else:
            return False

    def delete(self) -> bool:
        """Delete from the cache.
        Returns True if success and False if not found"""
        self.__check_qid__()
        logger.debug("Deleting from the cache")
        self.__verify_that_the_cache_file_exists_and_read__()
        self.__drop_qid_from_dataframe__()
        if self.qid_dropped:
            self.__save_dataframe_to_disk__()
            return True
        else:
            return False


import numpy as np
import cv2
import nibabel as nib
import os
import yaml
from pytorch_fid import fid_score

#from pytorch_lightning import metrics

import argparse

parser = argparse.ArgumentParser(description='Metrics Script')
parser.add_argument('--bodymask_path',  type=str)
parser.add_argument('--real_slices_path',  type=str)
parser.add_argument('--fake_slices_path',  type=str)
parser.add_argument('--results_path',  type=str, default='./metrics_result.yaml')
parser.add_argument('--FID', action='store_true')
parser.add_argument('--debug_images', action='store_true')

args = parser.parse_args()

def build_volume(slices, patient):
    # build 3D volume w*h*d
    names_slices = slices.keys()
    filtered_slice_keys =[s for s in names_slices if (s[:4] == patient)]

    filtered_slice_keys.sort()
    volume_dimensions = (slices[filtered_slice_keys[0]].shape[0], slices[filtered_slice_keys[0]].shape[0], len(filtered_slice_keys))

    volume = np.zeros(volume_dimensions)

    for i, sl in enumerate(filtered_slice_keys):
        if len(slices[sl].shape) == 2:
            volume[:,:,i] = slices[sl]
        else:
            volume[:,:,i] = slices[sl][:,:,0]

    return volume

def read_slices(path):
    # assume folder is for one patient
    filelist = os.listdir(path)
    imglist = {}
    for f in filelist:
        if f[-4:] == '.png':
            imglist[f] = cv2.imread(os.path.join(path, f))

        elif f[-4:] == '.nii':
            A_img_nifti = nib.load(os.path.join(path, f))
            imglist[f] = A_img_nifti.get_fdata(caching = "unchanged").astype(np.int16)

            #print(imglist[f][0,0])
        else:
            print(f + ' does not fit specified input')
    return imglist

def rescale_slices(slices):
    for key in slices:
        slices[key] = 4095*slices[key] -1024

    return slices

def read_mask(path):
    # assume folder is for one patient
    filelist = os.listdir(path)
    imglist = {}

    for f in filelist:
        A_img_nifti = nib.load(os.path.join(path, f))
        imglist[f] = A_img_nifti.get_fdata(caching = "unchanged")
    return imglist

def mask_volume(volume, mask):
    return np.multiply(volume, mask)

if __name__ == "__main__":
    bodymask_path = args.bodymask_path
    real_slices_path = args.real_slices_path
    fake_slices_path = args.fake_slices_path
    results_path = args.results_path
    print('reading real slices from: '+ real_slices_path)
    real_slices = read_slices(real_slices_path)
    print('reading fake slices from: '+ fake_slices_path)
    fake_slices = read_slices(fake_slices_path)
    print('rescaling fake slices')

    mask_slices = read_slices(bodymask_path)
    results = {}
    results['real_path'] = real_slices_path
    results['fake_path'] = fake_slices_path
    results['masks_path'] = bodymask_path

    # patient list with registered data, this needs to be changed if more
    # or other registered training data is available. The all the files with the
    # filename beginning with one of the list elements are considered.

    pat = ['PAT1', 'PAT3']

    diff_all_pat = []
    for p in pat:
        # make volumes
        print(p)
        real_vol = build_volume(real_slices, p)
        fake_vol = build_volume(fake_slices, p)

        mask = build_volume(mask_slices, p)

        # mask volumes
        fake_vol = mask_volume(fake_vol, mask)
        real_vol = mask_volume(real_vol, mask)

        diff = real_vol - fake_vol
        diff_masked = diff[mask>0]
        diff_all_pat.append(diff_masked)
        # calculate MAE
        mae = (np.abs(diff_masked)).mean()
        sd_ae = np.std(np.abs(diff_masked))

        print(p + ' MAE: '+ str(mae) + ', SD: '+ str(sd_ae))

        # calculate MSE
        mse = ((diff_masked)**2).mean()
        sd_se = np.std((diff_masked)**2)
        print(p + ' MSE: '+ str(mse) + ', SD: '+ str(sd_se))

        # calculate ME
        me = diff_masked.mean()
        sd_e = np.std(diff_masked)
        print(p + ' ME: '+ str(me)+ ', SD: '+ str(sd_e))

        # calculate MRSE
        mrse = np.sqrt(mse)
        print(p + ' MRSE: '+ str(mrse))

        # add to dict
        results[p] = {
            'mse': float(mse), 'sd_mse': float(sd_se),
            'mae': float(mae), 'sd_mae': float(sd_ae),
            'me': float(me), 'sd_me': float(sd_e)}

    # calculations for all patients
    p = 'PAT1+PAT3'
    # calculate MAE
    diff_all_pat = np.concatenate(diff_all_pat)
    # calculate MAE
    mae = (np.abs(diff_all_pat)).mean()
    sd_ae = np.std(np.abs(diff_all_pat))

    print(p + ' MAE: '+ str(mae) + ', SD: '+ str(sd_ae))

    # calculate MSE
    mse = ((diff_all_pat)**2).mean()
    sd_se = np.std((diff_all_pat)**2)
    print(p + ' MSE: '+ str(mse) + ', SD: '+ str(sd_se))

    # calculate ME
    me = diff_all_pat.mean()
    sd_e = np.std(diff_all_pat)
    print(p + ' ME: '+ str(me)+ ', SD: '+ str(sd_e))
    results[p] = {
        'mse': float(mse), 'sd_mse': float(sd_se),
        'mae': float(mae), 'sd_mae': float(sd_ae),
        'me': float(me), 'sd_me': float(sd_e)}

    print('FID Calculations: ' + str(args.FID))
    if args.FID:
        print('Calculating FID score, this may take a while...')
        fid_paths =  [real_slices_path,fake_slices_path]
        fid_value = fid_score.calculate_fid_given_paths(fid_paths,
                                                     batch_size=50,
                                                     device=None,
                                                     dims=2048)
        results['FID'] = float(fid_value)
    with open(results_path, 'w') as file:
        documents = yaml.dump(results, file)
    print(results)

    if args.debug_images:
        def on_change(i):
            img = np.hstack(((real_vol[:,:,i]+1024)/4095, (fake_vol[:,:,i]+1024)/4095, mask[:,:,i]))
            cv2.imshow('real-fake-mask', img)
        img = np.hstack(((real_vol[:,:,0]+1024)/4095, (fake_vol[:,:,0]+1024)/4095, mask[:,:,0]))
        cv2.imshow('real-fake-mask', img)
        cv2.createTrackbar('slider', 'real-fake-mask', 0, 299, on_change)
        while True:
            k = cv2.waitKey(0)
            if k==27:    # Esc key to stop
                cv2.destroyAllWindows()
                break


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from collections import namedtuple

import tensorflow as tf
import tensorflow.contrib.slim as slim

class Block(namedtuple('Block', ['scope', 'unit_fn', 'args'])):
    """A named tuple describing a ResNet block.

    Its parts are:
        scope: The scope of the `Block`.
        unit_fn: The ResNet unit function which takes as input a `Tensor` and returns another `Tensor` with the output of the ResNet unit.
        args: A list of length equal to the number of units in the `Block`. The list contains one (depth, depth_bottleneck, stride) tuple for each unit in the block to serve as argument to unit_fn.
    """

def subsample(inputs, factor, scope=None):
    if factor == 1:
        return inputs
    else:
        return slim.max_pool2d(inputs, [1, 1], stride=factor, scope=scope)  # padding='VALID'

def conv2d_same(inputs, num_outputs, kernel_size, stride, rate=1, scope=None):
    if stride == 1:
        return slim.conv2d(inputs, num_outputs, kernel_size, stride=1, rate=rate, padding='SAME', scope=scope)
    else:
        kernel_size_effective = kernel_size+(kernel_size-1)*(rate-1)
        pad_total = kernel_size_effective-1
        pad_beg = pad_total//2
        pad_end = pad_total-pad_beg
        inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]])  # zero padding
        return slim.conv2d(inputs, num_outputs, kernel_size, stride=stride, rate=rate, padding='VALID', scope=scope)

@slim.add_arg_scope
def stack_blocks_dense(net, blocks, output_stride=None, store_non_strided_activations=False, outputs_collections=None):
    current_stride = 1
    rate = 1

    for block in blocks:
        with tf.variable_scope(block.scope, 'block', [net]) as sc:
            block_stride = 1
            for i, unit in enumerate(block.args):
                if store_non_strided_activations and i == len(block.args)-1:
                    block_stride = unit.get('stride', 1)
                    unit = dict(unit, stride=1)
                with tf.variable_scope('unit_%d' % (i+1), values=[net]):
                    if output_stride is not None and current_stride == output_stride:
                        net = block.unit_fn(net, rate=rate, **dict(unit, stride=1))
                        rate *= unit.get('stride', 1)
                    else:
                        net = block.unit_fn(net, rate=1, **unit)
                        current_stride *= unit.get('stride', 1)
                        if output_stride is not None and current_stride > output_stride:
                            raise ValueError('The target output_stride cannot be reached.')
            net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)

            if output_stride is not None and current_stride == output_stride:
                rate *= block_stride
            else:
                net = subsample(net, block_stride)
                current_stride *= block_stride
                if output_stride is not None and current_stride > output_stride:
                    raise ValueError('The target output_stride cannot be reached.')
    if output_stride is not None and current_stride != output_stride:
        raise ValueError('The target output_stride cannot be reached.')
    return net

def resnet_arg_scope(weight_decay=0.0001,
                     batch_norm_decay=0.9,
                     batch_norm_epsilon=2e-5,
                     batch_norm_scale=True,
                     activation_fn=tf.nn.leaky_relu,
                     use_batch_norm=True,
                     batch_norm_updates_collections=tf.GraphKeys.UPDATE_OPS):
    batch_norm_params = {
      'decay': batch_norm_decay,
      'epsilon': batch_norm_epsilon,
      'scale': batch_norm_scale,
      'updates_collections': batch_norm_updates_collections,
      'fused': None,  # Use fused batch norm if possible.
      'param_regularizers': {'gamma': slim.l2_regularizer(weight_decay)},
    }

    with slim.arg_scope(
        [slim.conv2d],
        weights_regularizer=slim.l2_regularizer(weight_decay),
        weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False),
        activation_fn=activation_fn,
        normalizer_fn=slim.batch_norm if use_batch_norm else None,
        normalizer_params=batch_norm_params):
        with slim.arg_scope([slim.batch_norm], **batch_norm_params):
            with slim.arg_scope([slim.max_pool2d], padding='SAME') as arg_sc:
                return arg_sc


#!/usr/bin/python

import json
import os
import sys

config = sys.argv[-1]

# load json content from config file
def load_json():
    with open(config) as components:
        return json.load(components)

json = load_json()

# helper function for creating folders
def create_folder(folder_name,prev = None):
    if prev is None:
        new_folder_path = os.path.join(os.getcwd(),folder_name)
        if not os.path.exists(new_folder_path):
            os.mkdir(new_folder_path)
        return new_folder_path

    new_folder_pathh = os.path.join(prev,folder_name)
    if not os.path.exists(new_folder_pathh):
        os.mkdir(new_folder_pathh)

# create all the respective subfolders folders specified for each component
def create_directories():
    parent = create_folder(json['path'])
    for component in json['components']:
        create_folder(component,parent)

path = create_directories()

# react code template
code_template = """// libraries
import React from "react"

// other components

// style

// utils

const {} = () => {{
    return (

This is the {} component

    );
}};

export default {};"""

# create javascript files in respective folders
# fill each file with code template
def create_js():
    for component in json['components']:
        filename = component.capitalize() + ".js"
        full_file_path = os.path.join("components",component,filename)
        with open(full_file_path,"w") as file:
            file.write(code_template.format(component.capitalize(),component,component.capitalize()))

create_js()