# -*- coding: utf-8 -*-
"""Matrix構造解析を行うモジュール.

二次元トラス構造の解析用.
"""
import csv
import numpy as np
import matplotlib.pyplot as plt

class Node:
    """節点データのクラス.

    Attributes:
        number (int): 節点の番号
        index (int): 計算用のindex, 0からスタート
        x (double): 全体座標系でのx座標
        y (double): 全体座標系でのy座標
        disp_cond_x (double): x方向変位境界条件.初期値は, None
        disp_cond_y (double): y方向変位境界条件.初期値は, None
        load_cond_x (double): x方向荷重境界条件.初期値は, None
        load_cond_y (double): y方向荷重境界条件.初期値は, None
    """
    def __init__(self, number_param, index_param, x_param, y_param, disp_cond_x_param, disp_cond_y_param, load_cond_x_param, load_cond_y_param):
        """節点のコンストラクタ.

        Args:
            number_param (int): 節点の番号
            index_param (int): 計算用のindex, 0からスタート
            x_param (double): 全体座標系でのx座標
            y_param (double): 全体座標系でのy座標
            disp_cond_x_param (float): 変位境界条件.何もないときは, None
            disp_cond_x_param (float): 変位境界条件.何もないときは, None
            load_cond_y_param (float): 荷重境界条件.何もないときは, None
            load_cond_y_param (float): 荷重境界条件.何もないときは, None
        """
        self.number = number_param
        self.index = index_param
        self.x = x_param
        self.y = y_param
        self.disp_cond_x = disp_cond_x_param
        self.disp_cond_y = disp_cond_y_param
        self.load_cond_x = load_cond_x_param
        self.load_cond_y = load_cond_y_param

    def __str__(self):
        expl = 'num: '+str(self.number)+', x: '+str(self.x)+', y: '+str(self.y)+', disp_cond_x: '+str(self.disp_cond_x)+', disp_cond_y: '+str(self.disp_cond_y)+', load_cond_x: '+str(self.load_cond_x)+', load_cond_y: '+str(self.load_cond_y)
        return expl

    def has_disp_cond(self):
        """変位境界条件を持っているかどうかを返す.

        Returns:
            disp_cond が存在するならTrue
        """
        if self.disp_cond_x is not None or self.disp_cond_y is not None:
            return True
        else:
            return False

    def has_load_cond(self):
        """荷重境界条件を持っているかどうかを返す.

        Returns:
            load_cond_cond が存在するならTrue
        """
        if self.load_cond_x is not None or self.load_cond_y is not None:
            return True
        else:
            return False

    def set_load_cond_x(self, Px):
        """x方向の荷重をセットする.

        Args:
            Px (double): x方向の荷重条件.
        """
        if self.has_disp_cond():
            raise Exception('one node can only have one condition')
        self.load_cond_x = Px

    def set_load_cond_y(self, Py):
        """y方向の荷重をセットする.

        Args:
            Py (double): y方向の荷重条件.
        """
        if self.has_disp_cond():
            raise Exception('one node can only have one condition')
        self.load_cond_y = Py

    def add_load_cond_x(self, Px):
        """x方向の荷重を, 現在の条件に加える.

        Args:
            Px (double): 追加するx方向荷重条件.
        """
        if self.has_disp_cond():
            raise Exception('one node can only have one condition')
        self.load_cond_x += Px

    def add_load_cond_y(self, Py):
        """y方向の荷重を, 現在の条件に加える.

        Args:
            Px (double): 追加するx方向荷重条件.
        """
        if self.has_disp_cond():
            raise Exception('one node can only have one condition')
        self.load_cond_y += Py

class Member:
    """部材データのクラス.

    Attributes:
        nodes (list(Node)): 関連する節点のリスト
        area (double): 断面積
        E (double): ヤング率
    """
    def __init__(self, nodes_param, area_param, E_param):
        """コンストラクタ.

        Args:
            nodes_param (list(int)): 関連する節点の番号のリスト
            area_param (double): 断面積
            E_param (double): ヤング率
        """
        self.nodes = nodes_param
        self.area = area_param
        self.E = E_param

    def __str__(self):
        expl = 'nodes: '+str(self.nodes)+', area: '+str(self.area)+', E: '+str(self.E)
        return expl

class Load:
    """荷重データのクラス.

    Attributes:
        node (int): 荷重がかかっている節点の番号
        x (double): x方向の荷重
        y (double): y方向の荷重
    """
    def __init__(self, node_num, Px, Py):
        """コンストラクタ.

        Args:
            node_num (int): 荷重がかかっている節点の番号
            Px (double): x方向の荷重.
            Py (double): y方向の荷重
        """
        self.node = node_num

        self.x = Px
        self.y = Py

    def __str__(self):
        expl = 'node: '+str(self.node)+', Px: '+str(self.x)+', Py: '+str(self.y)
        return expl

class System:
    """構造解析を行う対象のシステム.

    Attributes:
        nodes (dict(Node)): システム内に存在する節点の辞書.辞書のキーがnode番号と対応する.
        members (dict(Member)): システム内に存在する部材の辞書.辞書のキーが部材の番号に対応する.
        K_total (numpy.array): トータルの剛性マトリックス
        f_total (numpy.array): トータルの荷重ベクトル
        d_total (numpy.array): トータルの変位ベクトル
    """
    def __init__(self):
        """Attributesを初期化するコンストラクタ
        """
        self.nodes = dict()
        self.members = dict()
        self.K_total = None
        self.f_total = None
        self.d_total = None

    def load_nodes_csv(self, filename):
        """節点の条件をcsvファイルから読み取る.

        Note:
            csvファイルは, 一行目はヘッダー, 二行目以降にデータ(番号, x座標, y座標, x方向変位境界条件, y方向変位境界条件, x方向荷重条件, y方向荷重条件(いずれもない場合はNone))

        Args:
            filename (str): ファイル名
        """
        current_index = 0
        with open(filename, 'r') as f:
            reader = csv.reader(f)
            header = next(reader)

            for row in reader:
                if len(row) == 7:
                    try:
                        dx = float(row[3])
                    except ValueError:
                        dx = None
                    try:
                        dy = float(row[4])
                    except ValueError:
                        dy = None
                    try:
                        Px = float(row[5])
                    except ValueError:
                        Px = None
                    try:
                        Py = float(row[6])
                    except ValueError:
                        Py = None
                    self.nodes[int(row[0])] = Node(int(row[0]), current_index, float(row[1]), float(row[2]), dx, dy, Px, Py)
                    current_index += 1
                else:
                    raise Exception('invalid data input')

    def load_members_csv(self, filename):
        """ 節点の条件をcsvファイルから読み取る.

        Note:
            csvファイルは，一行目はヘッダー，二行目以降にデータ（番号，右端の節点番号，左端の節点番号，断面積，ヤング率)

        Args:
            filename (str): ファイル名
        """
        with open(filename, 'r') as f:
            reader = csv.reader(f)
            header = next(reader)

            for row in reader:
                if len(row) == 5:
                    self.members[int(row[0])] = Member([int(row[1]), int(row[2])], float(row[3]), float(row[4]))
                else:
                    raise Exception('invalid data input')

    def calculate_partial_stiffness_matrix(self):
        """各部材に対して, 全体座標系で表した剛性マトリックスを計算する."""
        for m in self.members.values():
            node = [self.nodes[m.nodes[0]], self.nodes[m.nodes[1]]]
            L = np.sqrt((node[1].x-node[0].x)**2 + (node[1].y-node[0].y)**2)
            c = (node[1].x - node[0].x) / L
            s = (node[1].y - node[0].y) / L
            m.K = np.array([[c*c, c*s, -c*c, -c*s],
                            [c*s, s*s, -c*s, -s*s],
                            [-c*c, -c*s, c*c, c*s],
                            [-c*s, -s*s, c*s, s*s]]) * m.E * m.area / L

    def calculate_total_stiffness_matrix(self):
        """全体の剛性マトリックスを計算する"""
        matrix_size = 2*len(self.nodes)
        self.K_total = np.zeros((matrix_size, matrix_size))
        for m in self.members.values():
        #for m in [self.members[1]]:
            node = [self.nodes[m.nodes[0]], self.nodes[m.nodes[1]]]
            self.K_total[node[0].index*2:node[0].index*2+2, node[0].index*2:node[0].index*2+2] += m.K[0:2, 0:2]
            self.K_total[node[0].index*2:node[0].index*2+2, node[1].index*2:node[1].index*2+2] += m.K[0:2, 2:4]
            self.K_total[node[1].index*2:node[1].index*2+2, node[0].index*2:node[0].index*2+2] += m.K[2:4, 0:2]
            self.K_total[node[1].index*2:node[1].index*2+2, node[1].index*2:node[1].index*2+2] += m.K[2:4, 2:4]

        self.f_total = np.zeros(matrix_size)
        self.d_total = np.zeros(matrix_size)
        for node in self.nodes.values():
            self.f_total[node.index*2] = node.load_cond_x
            self.f_total[node.index*2+1] = node.load_cond_y
            self.d_total[node.index*2] = node.disp_cond_x
            self.d_total[node.index*2+1] = node.disp_cond_y

    def solve_stiffness_equation(self):
        """剛性方程式を解く"""
        Kff_sf = self.K_total[np.where(np.logical_not(np.isnan(self.f_total))) ]
        Kff_size = Kff_sf.shape[0]
        Kff = Kff_sf[ :,  np.where(np.logical_not(np.isnan(self.f_total)))].reshape(Kff_size, Kff_size)
        Ksf = Kff_sf[:, np.where(np.isnan(self.f_total))].reshape(Kff_size, Kff_sf.shape[1]-Kff_size)

        Kfs_ss = self.K_total[np.where(np.logical_not(np.isnan(self.d_total)))]
        Kss_size = Kfs_ss.shape[0]
        Kfs = Kfs_ss[:, np.where(np.isnan(self.d_total))].reshape(Kss_size, Kfs_ss.shape[1]-Kss_size)
        Kss = Kfs_ss[:, np.where(np.logical_not(np.isnan(self.d_total)))].reshape(Kss_size, Kss_size)

        ff = self.f_total[np.where(np.logical_not(np.isnan(self.f_total)))]
        fs = self.f_total[np.where(np.isnan(self.f_total)) ]

        df = self.d_total[np.where(np.isnan(self.d_total)) ]
        ds = self.d_total[np.where(np.logical_not(np.isnan(self.d_total))) ]

        df = np.linalg.inv(Kff).dot(ff - Ksf.dot(ds))
        fs = Kfs.dot(df) + Kss.dot(ds)

        self.f_total[np.where(np.isnan(self.f_total)) ] = fs
        self.d_total[np.where(np.isnan(self.d_total)) ] = df

    def solve(self):
        """剛性方程式をつくり, 解を求める"""
        self.calculate_partial_stiffness_matrix()
        self.calculate_total_stiffness_matrix()
        self.solve_stiffness_equation()

    def draw_displacement(self, disp_scale=10, x_margin_scale=0.1, y_margin_scale=0.1):
        """トラスの変形の様子を描画する

        Args:
            disp_scale (int): 変位の拡大倍率. default=10
            x_margin_scale (int): x方向の余白の拡大倍率. default=0.1
            y_margin_scale (int): y方向の余白の拡大倍率. default=0.1
        """
        # 元のsystemを表示
        for n in self.nodes.values():
            plt.plot(n.x, n.y, 'ko')
        for member in self.members.values():
            node = [[self.nodes[member.nodes[0]].x, self.nodes[member.nodes[1]].x],
                    [self.nodes[member.nodes[0]].y, self.nodes[member.nodes[1]].y]]
            plt.plot(node[0], node[1], 'k-', lw=2)

        # 変形後のシステムを表示
        sorted_node = sorted(self.nodes.values(), key=lambda n: n.index)
        node_xy = []
        for n in sorted_node:
            node_xy.append(n.x)
            node_xy.append(n.y)
        node_xy = np.array(node_xy)
        node_xy += self.d_total*disp_scale
        for i in range(0, node_xy.size, 2):
            plt.plot(node_xy[i], node_xy[i+1], 'bo')
        for member in self.members.values():
            node = [[self.nodes[member.nodes[0]].x+(self.d_total*disp_scale)[self.nodes[member.nodes[0]].index*2],
                    self.nodes[member.nodes[1]].x+(self.d_total*disp_scale)[self.nodes[member.nodes[1]].index*2] ],
                    [self.nodes[member.nodes[0]].y+(self.d_total*disp_scale)[self.nodes[member.nodes[0]].index*2+1],
                    self.nodes[member.nodes[1]].y+(self.d_total*disp_scale)[self.nodes[member.nodes[1]].index*2+1] ]]
            plt.plot(node[0], node[1], 'b-', lw=2)

        #荷重を矢印で表示
        node_x = node_xy[0::2]
        node_y = node_xy[1::2]
        f_x = self.f_total[0::2]
        f_y = self.f_total[1::2]
        plt.quiver(node_x, node_y, f_x, f_y)

        #整形して表示
        plt.axis('scaled')
        xmin = min([n.x for n in self.nodes.values()])
        xmax = max([n.x for n in self.nodes.values()])
        xmargin = (xmax-xmin)*x_margin_scale
        plt.xlim(xmin-xmargin, xmax+xmargin)
        ymin = min([n.y for n in self.nodes.values()])
        ymax = max([n.y for n in self.nodes.values()])
        ymargin = (ymax-ymin)*y_margin_scale
        plt.ylim(ymin-ymargin, ymax+ymargin)
        plt.show()

    def calculate_stress(self):
        """各部材の応力を計算する

        Returns:
            stress (dict(float)): 部材の番号をキーとした各部材の応力の辞書
        """
        stress = dict()
        for i, m in zip(self.members.keys(),self.members.values()):
            node = [self.nodes[m.nodes[0]], self.nodes[m.nodes[1]]]
            L = np.sqrt((node[1].x-node[0].x)**2 + (node[1].y-node[0].y)**2)
            dL = np.sqrt((node[1].x+self.d_total[node[1].index*2]-node[0].x-self.d_total[node[0].index*2])**2 +
                    (node[1].y+self.d_total[node[1].index*2+1]-node[0].y-self.d_total[node[0].index*2+1])**2 ) -L
            stress[i] =  m.E* dL/L
        return stress

    def print_result(self):
        """計算結果を表示する"""
        sorted_node = sorted(self.nodes.values(), key=lambda n: n.index)
        print('各ノードにかかる荷重')
        for n in sorted_node:
            i=n.index
            print('node no. '+str(n.number)+ ' : '+ str([self.f_total[i], self.f_total[i+1]]))

        print('各ノードの変位')
        for n in sorted_node:
            i=n.index
            print('node no. '+str(n.number)+ ' : '+ str([self.d_total[i], self.d_total[i+1]]))

        print('各部材内部の応力')
        stress = self.calculate_stress()
        for i, s in zip(stress, stress.values()):
            print('member no. '+str(i)+' : '+str(s))

        print('荷重f')
        print(self.f_total)
        print('全体剛性マトリックス')
        print(self.K_total)
        print('変位d')
        print(self.d_total)

        print('f と Kd の計算誤差')
        print(self.f_total-self.K_total.dot(self.d_total))


import requests
import json

# Set up the API endpoint URL
url = 'https://api.couponapi.com/search'

# Set up the API parameters
api_key = '<your api key>'  # Replace with your CouponAPI key
brand = 'Nike'  # Replace with the brand you want to search for
params = {
    'api_key': api_key,
    'brand': brand,
}

# Send the API request and retrieve the response
response = requests.get(url, params=params)
coupons = json.loads(response.text)

# Print the coupons
for coupon in coupons['coupons']:
    print(coupon['code'], coupon['description'], coupon['expires_at'])


# Import standard libraries
import os

# Import third-party libraries
import openai
import streamlit as st

openai.api_key = os.environ["OPENAI_API_KEY"]

class Sidebar:
    """Class for managing the sidebar of the Streamlit application."""
    models_list = openai.Model.list()

    MODEL_OPTIONS = [model["id"] for model in models_list["data"]]
    TEMPERATURE_MIN_VALUE = 0.0
    TEMPERATURE_MAX_VALUE = 1.0
    TEMPERATURE_DEFAULT_VALUE = 0.5
    TEMPERATURE_STEP = 0.01

    @staticmethod
    def about():
        """Display information about the application and its dependencies."""
        about = st.sidebar.expander("About", expanded=False)
        sections = [
            "#### Powered by: [Langchain](https://github.com/hwchase17/langchain), "
            "[OpenAI](https://platform.openai.com/docs/models/gpt-3-5) and "
            "[Streamlit](https://github.com/streamlit/streamlit)",
            "#### Source code : [GitHub](https://github.com/matthewdutton1983/Doc-GPT)",
        ]
        for section in sections:
            about.write(section)

    def model_selector(self):
        """Display radio buttons to select the AI model to be used."""
        model = st.selectbox(
            label="Model", options=self.MODEL_OPTIONS, index=13)
        st.session_state["model"] = model

    @staticmethod
    def reset_chat_button():
        """Display a button to reset the chat."""
        if st.button("Reset chat"):
            st.session_state["reset_chat"] = True
        st.session_state.setdefault("reset_chat", False)

    def temperature_slider(self):
        """Display a slider to set the "temperature" for the AI model's responses."""
        temperature = st.slider(
            label="Temperature",
            min_value=self.TEMPERATURE_MIN_VALUE,
            max_value=self.TEMPERATURE_MAX_VALUE,
            value=self.TEMPERATURE_DEFAULT_VALUE,
            step=self.TEMPERATURE_STEP
        )
        st.session_state["temperature"] = temperature

    def show_options(self):
        """Display a set of tools for interacting with the chat."""
        with st.sidebar.expander("Tools", expanded=False):
            self.reset_chat_button()
            self.model_selector()
            self.temperature_slider()
            st.session_state.setdefault("model", self.MODEL_OPTIONS[0])
            st.session_state.setdefault(
                "temperature", self.TEMPERATURE_DEFAULT_VALUE)


"""empty message

Revision ID: b96b3cf3696e
Revises: d07af16977db
Create Date: 2021-09-23 22:07:50.120068

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "b96b3cf3696e"
down_revision = "d07af16977db"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("roles", sa.Column("failed_error_message", sa.Text(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("roles", "failed_error_message")
    # ### end Alembic commands ###


import sys
sys.stdin = open("input.txt", "r")

T = int(input())
for test_case in range(1, T + 1):
    N = int(input())
    number = int(input())
    max_count = 0
    for i in str(number):
        count = 0
        for j in str(number):
            if j == i:
                count += 1
            if count > max_count:
                max_count = count
                result = i

    print(f'#{test_case} {result} {max_count}')


import numpy as np
import click
import logging
from os import environ
from os.path import basename
from .configuration import Configuration, GENERAL_OPTIONS, EXTRACTION_OPTIONS, LABEL_OPTIONS, WRITER_OPTIONS, Filetypes
from .utils import add_options
from ..backend.plotting import PlotTuple
from ..tools.path import get_relative_path

environ['GLOG_minloglevel'] = '2'
environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

log = logging.getLogger(__name__)

DESCRIPTION_IMAGE_FEATURES = 'Extract CNN-descriptors from images.'

def image_reader(files, base_path=None, size=500):
    import cv2
    for image in files:
        img = cv2.imread(image, cv2.IMREAD_COLOR)
        img = cv2.resize(img, dsize=(size, size))
        img = img[:, :, :3]
        yield PlotTuple(name=get_relative_path(image, base_path),
                        timestamp=None,
                        plot=np.array(img))

@click.command(help=DESCRIPTION_IMAGE_FEATURES)
@add_options(GENERAL_OPTIONS)
@add_options(EXTRACTION_OPTIONS)
@add_options(LABEL_OPTIONS)
@add_options(WRITER_OPTIONS[:-2])
def image_features(**kwargs):
    from ..tools.feature_writer import get_writer
    configuration = Configuration(plotting=False,
                                  file_type=Filetypes.IMAGE,
                                  **kwargs)
    plots = image_reader(configuration.files,
                         base_path=configuration.input_folder)
    log.info('Loading model and weights...')
    extractor = configuration.extractor(images=plots,
                                        **configuration.extraction_args)

    log.info('Extracting features from images...')
    writer = get_writer(**configuration.writer_args)
    writer.write_features(configuration.files, extractor)

    log.info('Done extracting features.')


#!/usr/bin/python3
from sense_hat import SenseHat
import sys
import requests
import json
import os

def main():
    sense = SenseHat()
    sense.clear()
    temp = round(sense.get_temperature(), 1)

    if(temp < 15):
        #ip_address = os.popen('hostname -I').read()
        send_notification_via_pushbullet("Temperature: %d" % temp, "From Raspberry Pi")

def getKey(location):
    apifile = open(location, "r")
    key = apifile.read()
    apifile.close()

    return key

ACCESS_TOKEN = getKey('/home/pi/IOTASS1/api.key')

def send_notification_via_pushbullet(title, body):
    """ Sending notification via pushbullet.
        Args:
            title (str) : title of text.
            body (str) : Body of text.
    """
    data_send = {"type": "note", "title": title, "body": body}

    resp = requests.post('https://api.pushbullet.com/v2/pushes', data=json.dumps(data_send),
                         headers={'Authorization': 'Bearer ' + ACCESS_TOKEN,
                         'Content-Type': 'application/json'})
    if resp.status_code != 200:
        raise Exception('Something wrong')
    else:
        print('complete sending')

#Execute
main()


import math
import win32com.client as com
# from OneWayMainPerp import *
from oneWayMainPerpInputs import *
from oneWayHelperLaneConstruction import *

roadWidth = ROADWIDTH
parkingLength = PARKINGLENGTH

def addBaseLink(row, col, angle, grid, ct, dist):
    """
        # function to add the entry link - base link (only on upper side plot )
        row,col - positional parameters
        angle - provides the angle of driveway
        grid - 3 d array
        ct - represent for unique identifier for links
        dist - length of the baselink
    """
    # print(Vissim)
    # global Vissim
    roadWidth = ROADWIDTH
    rowCopy = row
    row = -row
    multiplier = 3.5
    dist /= 3.5
    s = "D "+f"{ct} "+f"{angle} "
    grid[rowCopy][col].append(s)

    # x1 decreases
    if (angle == 0):
        grid[rowCopy][col+1].append(s)

        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col+dist)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])

    # y1 increases
    elif (angle == 90):
        grid[rowCopy-1][col].append(s)

        row -= 1
        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row+dist)*multiplier

        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # y1 decreases
    elif (angle == 270):
        grid[rowCopy+1][col].append(s)

        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row-dist)*multiplier
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # angle 180 - x0 decreases
    else:
        grid[rowCopy][col-1].append(s)
        col += 1
        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col-dist)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    return link

# function to add parking links

def addLinkPark(row, col, angle, grid, ct, connected):
    """
        function to add the parking lot links
        row,col - positional parameter
        angle - angle of driveway
        ct - represent for unique identifier for links

    """
    rowCopy = row
    # row+=1
    row = -row
    multiplier = roadWidth
    temp = [False, False]
    temp1 = [False, False]
    s = "D "+f"{ct} "+f"{angle} "
    grid[rowCopy][col].append(s)
    connected[rowCopy][col].append(temp)
    # x1 decreases
    if (angle == 0):
        grid[rowCopy][col+1].append(s)
        connected[rowCopy][col+1].append(temp1)

        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col+2)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])

    # y1 increases
    elif (angle == 90):
        grid[rowCopy-1][col].append(s)
        connected[rowCopy-1][col].append(temp1)

        row -= 1
        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row+2)*multiplier

        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # y1 decreases
    elif (angle == 270):
        grid[rowCopy+1][col].append(s)
        connected[rowCopy+1][col].append(temp1)

        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row-2)*multiplier
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # angle 180 - x0 decreases
    else:
        grid[rowCopy][col-1].append(s)
        connected[rowCopy][col-1].append(temp1)

        col += 1
        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col-2)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    return link

# function to add driveway links

def addReducedLinks(row, col, angle, noOfblocks):
    """
        function to add links in vissim
        row,col - postional parameters
        no of Blocks - the no of blocks the link will psan
        roadWidth - width of road
        isDeciaml - wheter the driveway width is odd or even

    """
    blockLength = 3.5
    row = -row
    multiplier = roadWidth

    # x1 decreases
    if (angle == 0):

        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col + noOfblocks)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])

    # y1 increases
    elif (angle == 90):

        row -= 1
        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row+noOfblocks)*multiplier

        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # y1 decreases
    elif (angle == 270):

        x0 = col*multiplier + multiplier/2
        y0 = row*multiplier
        x1 = x0
        y1 = (row-noOfblocks)*multiplier
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    # angle 180 - x0 decreases
    else:

        col += 1
        x0 = col*multiplier
        y0 = row*multiplier - multiplier/2
        x1 = (col-noOfblocks)*multiplier
        y1 = y0
        link = Vissim.Net.Links.AddLink(
            0, f"LINESTRING({x0} {y0}, {x1} {y1})", [roadWidth])
    return link

# init function which initiates the creating of link

# init function for adding driveway links

def addReducedLinksinit(grid):
    """
        an init function which helps in creation of links
        grid - 3d array
        roadWidth - width of the road
        isdecimal - weather the driveway spans off no of blocks
    """
    m = len(grid)
    n = len(grid[0])
    for i in range(0, m):
        for j in range(0, n):
            for k in range(0, len(grid[i][j])):
                item = grid[i][j][k].strip().split()
                if (len(item) == 0):
                    break

                angle1 = int(item[2])
                noOfBlocks = int(item[4])
                addReducedLinks(i, j, angle1, noOfBlocks)

# function which interacts with vissim and connects link

# function to connect 2 driveway lanes
def connectVissimReduced(grid, x0, y0, z0, x1, y1, z1):
    """
        to add connector between 2 lanes
        grid - 3d array
        x0,y0,z0 - positional parameters of 1st item
        x1,y1,z1 - positional parameters of 2nd item
    """
    multiplier = roadWidth
    lengthPerBlock = roadWidth
    sx0 = y0*multiplier
    sy0 = x0*multiplier - multiplier/2
    sx1 = y1*multiplier
    sy1 = x1*multiplier
    point1 = grid[x0][y0][z0]
    point2 = grid[x1][y1][z1]
    point1 = point1.strip()
    point1 = point1.split()
    point2 = point2.strip()
    point2 = point2.split()

    if (len(point1) < 3 or len(point2) < 3):
        return 1
    if (int(point1[1]) == int(point2[1])):
        return 1

    # connect
    prevLinkId = int(point1[1])
    linkId = int(point2[1])
    pos1 = int(point1[3])
    pos2 = int(point2[3])

    Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(prevLinkId).Lanes.ItemByKey(
        1), pos1*lengthPerBlock, Vissim.Net.Links.ItemByKey(linkId).Lanes.ItemByKey(1), (pos2-1)*lengthPerBlock, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()

# and sub init function for connecting connectors

# init function which helps inn connection of driveway

def connectReduced(grid, x, y, z, check, connectionFlow):
    """
        2nd init function which helpers in connecting 2 lanes
        grid - 3d array
        x,y,z - positional paramters
        check - list to check particular directions for connection of links
        connectionFlow - ist which represent the angle of link to connect with
    """
    item1 = grid[x][y][z]
    item1 = item1.strip().split()
    angle1 = int(item1[2])
    if (len(item1) < 3):
        return

    for i in range(0, len(check)):

        temp = check[i]
        x1 = x+temp[0]
        y1 = y+temp[1]
        if (x1 < 0 or y1 < 0 or x1 >= len(grid) or y1 >= len(grid[0])):
            continue
        for j in range(0, len(grid[x1][y1])):
            item2 = grid[x1][y1][j]
            item2 = item2.strip().split()

            angle2 = int(item2[2])
            if (i == 0):
                if (angle2 == connectionFlow[i] or angle2 == angle1):
                    connectVissimReduced(grid, x, y, z, x1, y1, j)

            elif (i == 1):
                if (angle2 == connectionFlow[i] or angle2 == angle1):
                    connectVissimReduced(grid, x, y, z, x1, y1, j)

# an init function which initiates the connection of drivelanes

# the outer function which wraps all inner functions used for connecting driveways

def addConnectorsInitReduced(grid):
    """
        Init function which start the process of connection of links
        grid - 3d array
    """
    m = len(grid)
    n = len(grid[0])
    for i in range(0, m):
        for j in range(0, n):
            # -------turn
            # -------staight
            # 2lane gap
            # U - turn
            # angle 270
            if (len(grid[i][j]) == 0):
                continue
            for k in range(0, len(grid[i][j])):
                item1 = grid[i][j][k]

                item1 = item1.strip()
                item1 = item1.split()
                if (len(item1) < 3):
                    continue
                angle1 = int(item1[2])
                # print("angle1 , ",angle1)
                if (angle1 == 270):
                    connectionFlow = [180, 0]
                    # check = [[1, -width], [2*width, 2*width]]
                    check = [[0, -1], [0, 1]]
                    connectReduced(grid, i, j, k, check, connectionFlow)
                    # angle 90
                elif (angle1 == 90):
                    connectionFlow = [0, 180]
                    # check = [[-1, width], [-2*width, -2*width]]
                    check = [[0, 1], [0, -1]]
                    connectReduced(grid, i, j, k, check, connectionFlow)
                # angle 180
                elif (angle1 == 180):
                    connectionFlow = [90, 270]
                    # check = [[-width, -1], [2*width, -2*width]]
                    check = [[-1, 0], [1, 0]]
                    connectReduced(grid, i, j, k, check, connectionFlow)
                # angle 0
                elif (angle1 == 0):
                    connectionFlow = [270, 90]
                    # check = [[width, 1], [-2*width, 2*width]]
                    check = [[1, 0], [-1, 0]]
                    connectReduced(grid, i, j, k, check, connectionFlow)

# function which interacts with vissim and connect the U turn areas

def connectUTurn(grid, check, angle1, reqAngle, item1, x, y, z, count):
    """
        function which helps in connecting the U -Turn areas
        gridR - 3d array
        check - list to check for certain position for U - Turn areas
        angle1 -angle of driveway
        reqAngle - angle of Uturn link
        x,y,z - positional paramters

    """
    # global count
    dict = {270: 180, 90: 0, 180: 90, 0: 270}
    lengthPerBlock = roadWidth
    multiplier = roadWidth
    linkId = int(item1[1])
    x1 = x+check[0]
    y1 = y+check[1]
    sx0 = y*multiplier
    sy0 = x*multiplier - multiplier/2
    sx1 = y1*multiplier
    sy1 = x1*multiplier

    for k in range(len(grid[x1][y1])):
        item2 = grid[x1][y1][k].strip().split()
        prevLinkId = int(item2[1])
        pos1 = int(item2[3])
        if (len(item2) == 0):
            break
        angle2 = int(item2[2])
        if (angle2 == reqAngle):
            s = f"D {count} {reqAngle} {1}"
            grid[x1][y1].append(s)
            s = f"D {count} {reqAngle} {2}"
            grid[x][y].append(s)
            count += 1
            link = addReducedLinks(x1, y1, dict[angle1], 2)
            len1 = link.AttValue('Length2D')
            '''link -> item'''
            Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(prevLinkId).Lanes.ItemByKey(
                1), (pos1-0.91)*lengthPerBlock, link.Lanes.ItemByKey(1), 0, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
            Vissim.Net.Links.AddConnector(0, link.Lanes.ItemByKey(
                1), len1, Vissim.Net.Links.ItemByKey(linkId).Lanes.ItemByKey(1), 0*lengthPerBlock, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
            return 1
    return 0
# an init function which initiates connetion of U turn areas

def connectUTurnInit(grid, count,start):
    """
        an Init function which coordinates the creation and connection of U-turn areas

        gridR - 3d array
        width - no of block the road spans
        roadWidth -  width of road
        isDecimal - boolean value which states wheter the driveway spans odd or even no of blocks

    """
    m = len(grid)
    n = len(grid[0])
    for i in range(0, m):
        for j in range(0, n):
            for k in range(len(grid[i][j])):
                item = grid[i][j][k].strip().split()
                if (len(item) == 0 or(i==start[0] and j==start[1])):
                    break
                m = item[0]
                if (m == 'SD'):
                    angle1 = int(item[2])
                    if (angle1 == 270):
                        reqAngle = 90
                        check = [0, 1]
                        count += connectUTurn(grid, check, angle1,
                                              reqAngle, item, i, j, k, count)
                    elif (angle1 == 90):
                        reqAngle = 270
                        check = [0, -1]
                        count += connectUTurn(grid, check, angle1,
                                              reqAngle, item, i, j, k, count)
                    elif (angle1 == 180):
                        reqAngle = 0
                        check = [1, 0]
                        count += connectUTurn(grid, check, angle1,
                                              reqAngle, item, i, j, k, count)
                    elif (angle1 == 0):
                        reqAngle = 180
                        check = [-1, 0]
                        count += connectUTurn(grid, check, angle1,
                                              reqAngle, item, i, j, k, count)

# function o create parking lot and its connections

def parkingLotinit(grid, baseLink):
    """
        Function which takes care of creation and connection of parking lot
        grid - 3d array
        baseLink - the baseLink(entry link for the network)
    """
    print(baseLink)
    VehRoutDesPark = Vissim.Net.VehicleRoutingDecisionsParking.AddVehicleRoutingDecisionParking(
        0, baseLink, 1)

    VehRoutDesPark.SetAttValue('ParkDur(1)', 20)
    VehRoutDesPark.SetAttValue('AllVehTypes', False)
    VehRoutDesPark.SetAttValue('VehClasses', 10)

    with open('desiredParking.txt', 'r') as file:
        lines = file.readlines()
        countParkingLots = 1
        # print(lines)
        for i in range(len(lines)):
            # line = lines[i]
            s = lines[i].strip().split()
            print(s)
            if (s[0][0] == 'x'):
                anglePark = int(s[0][1:])

                rowPark = int(s[1])
                colPark = int(s[3])

                # Step 1 - addLink
                linkPark = addLinkPark(
                    rowPark, colPark, anglePark, grid, ct, connected)
                # Step2  - Connector
                #  spLine(sx0, sy0, sx1, sy1)
                '''
                multiplier = 3.5
                sx0 = y0*multiplier
                sy0 = x0*multiplier - multiplier/2
                sx1 = y1*multiplier
                sy1 = x1*multiplier
                '''
                def connectParkingLane(grid):
                    lengthPerBlock = roadWidth
                    baseLengthToPark = lengthPerBlock+.2

                    if (anglePark == 180):
                        connectRow = rowPark
                        connectCol = colPark+1
                        val = None
                        baseLengthToPark = 0
                        flag = 1
                        # prioity parking
                        for i in range(0, len(grid[connectRow][connectCol])):
                            val = grid[connectRow][connectCol][i]
                            val = val.strip().split()
                            linkid = int(val[1])
                            l = Vissim.Net.Links.ItemByKey(
                                linkid).AttValue("Length2D")
                            if (int(val[2]) == anglePark and l > 2*roadWidth):
                                flag = 0
                                break
                        for i in range(0, len(grid[connectRow][connectCol])):

                            if (flag != 0):
                                val = grid[connectRow][connectCol][i]
                                val = val.strip().split()

                            multiplier = roadWidth
                            sx0 = colPark*multiplier
                            sy0 = rowPark*multiplier - multiplier/2
                            sx1 = connectCol*multiplier
                            sy1 = connectRow*multiplier
                            connectId = int(val[1])
                            connectAngle = int(val[2])
                            pos = int(val[3])
                            baseLengthToPark = Vissim.Net.Links.ItemByKey(
                                connectId).AttValue("Length2D")-lengthPerBlock-0.1
                            if (flag == 0):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), min((pos-0.5)*lengthPerBlock, baseLengthToPark), linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                            elif (connectAngle != 0):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), (pos-0.5)*lengthPerBlock, linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                    elif (anglePark == 0):
                        connectRow = rowPark
                        connectCol = colPark-1
                        val = None
                        baseLengthToPark = 0
                        flag = 1
                        # prioity parking
                        for i in range(0, len(grid[connectRow][connectCol])):
                            val = grid[connectRow][connectCol][i]
                            val = val.strip().split()
                            linkid = int(val[1])
                            l = Vissim.Net.Links.ItemByKey(
                                linkid).AttValue("Length2D")
                            if (int(val[2]) == anglePark and l > 2*roadWidth):
                                flag = 0
                                break
                        for i in range(0, len(grid[connectRow][connectCol])):
                            if (flag != 0):
                                val = grid[connectRow][connectCol][i]
                                val = val.strip().split()
                            connectId = int(val[1])
                            connectAngle = int(val[2])
                            multiplier = 3.5
                            sx0 = colPark*multiplier
                            sy0 = rowPark*multiplier - multiplier/2
                            sx1 = connectCol*multiplier
                            sy1 = connectRow*multiplier
                            pos = int(val[3])
                            baseLengthToPark = Vissim.Net.Links.ItemByKey(
                                connectId).AttValue("Length2D")-lengthPerBlock-0.1
                            if (flag == 0):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), min((pos-0.5)*lengthPerBlock, baseLengthToPark), linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                            elif (connectAngle != 180):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), (pos-0.5)*lengthPerBlock, linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                    elif (anglePark == 90):
                        connectRow = rowPark+1
                        connectCol = colPark
                        val = None
                        baseLengthToPark = 0
                        flag = 1
                        # prioity parking
                        for i in range(0, len(grid[connectRow][connectCol])):
                            val = grid[connectRow][connectCol][i]
                            val = val.strip().split()
                            linkid = int(val[1])
                            l = Vissim.Net.Links.ItemByKey(
                                linkid).AttValue("Length2D")
                            if (int(val[2]) == anglePark and l > 2*roadWidth):
                                flag = 0
                                break
                        for i in range(0, len(grid[connectRow][connectCol])):
                            if (flag != 0):
                                val = grid[connectRow][connectCol][i]
                                val = val.strip().split()
                            connectId = int(val[1])
                            connectAngle = int(val[2])
                            multiplier = 3.5
                            sx0 = colPark*multiplier
                            sy0 = rowPark*multiplier - multiplier/2
                            sx1 = connectCol*multiplier
                            sy1 = connectRow*multiplier
                            pos = int(val[3])
                            baseLengthToPark = Vissim.Net.Links.ItemByKey(
                                connectId).AttValue("Length2D")-lengthPerBlock-0.1
                            if (flag == 0):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), min((pos-0.5)*lengthPerBlock, baseLengthToPark), linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                            elif (connectAngle != 270):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), (pos-0.5)*lengthPerBlock, linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                    elif (anglePark == 270):
                        connectRow = rowPark-1
                        connectCol = colPark
                        val = None
                        baseLengthToPark = 0
                        flag = 1
                        # prioity parking
                        for i in range(0, len(grid[connectRow][connectCol])):
                            val = grid[connectRow][connectCol][i]
                            val = val.strip().split()
                            linkid = int(val[1])
                            l = Vissim.Net.Links.ItemByKey(
                                linkid).AttValue("Length2D")
                            if (int(val[2]) == anglePark and l > 2*roadWidth):
                                flag = 0
                                break
                        for i in range(0, len(grid[connectRow][connectCol])):
                            if (flag != 0):
                                val = grid[connectRow][connectCol][i]
                                val = val.strip().split()
                            connectId = int(val[1])
                            connectAngle = int(val[2])
                            multiplier = 3.5
                            sx0 = colPark*multiplier
                            sy0 = rowPark*multiplier - multiplier/2
                            sx1 = connectCol*multiplier
                            sy1 = connectRow*multiplier
                            pos = int(val[3])
                            baseLengthToPark = Vissim.Net.Links.ItemByKey(
                                connectId).AttValue("Length2D")-lengthPerBlock-0.1
                            if (flag == 0):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), min((pos-0.5)*lengthPerBlock, baseLengthToPark), linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                            elif (connectAngle != 90):
                                Vissim.Net.Links.AddConnector(0, Vissim.Net.Links.ItemByKey(connectId).Lanes.ItemByKey(
                                    1), (pos-0.5)*lengthPerBlock, linkPark.Lanes.ItemByKey(1), 0.3, 1, spLine(sx0, sy0, sx1, sy1)).RecalculateSpline()
                                break
                connectParkingLane(grid)

                # AddParkingLot
                ParkingLot = Vissim.Net.ParkingLots.AddParkingLot(
                    0, linkPark.Lanes.ItemByKey(1), 0.5)

                ParkingLot.SetAttValue("ParkDir", 2)
                Vissim.Net.ParkingLots.ItemByKey(
                    countParkingLots).SetAttValue("Length", parkingLength)
                # routing decision
                VehRoutDesPark.VehRoutPark.AddVehicleRouteParking(
                    0, ParkingLot)
                countParkingLots += 1


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

# # Facebook Prophet
# March 1st 2022

# Facebook Prophet is an **general additive model** that includes a number of highly advanced, intelligent forecasting methods, incluiding changepoint analysis.
# _y = g(t) + s(t) + h(t) + $\epsilon_t$_
# Here g(t) => the **trend** function which models non-periodic changes, s(t) => **periodic** changes (e.g. weekly and yearly seasonality), and h(t) => represents the effects of **holidays** (which occur on potentially irregular scheadules over one or more days)

# Prophet was optimized with the **business forecast tasks**, typically following the next features:
# * We need a bit of data, at least a year. With hourly, daily, or weekly observations
# * Strong multiple human scale seasonality (e.g. week day)
# * Important holidays that occur at irregular intervals
# * A reasonable number of missing observations or large outliers
# * Historical changes may not persist and die out
# * Trends that are non-linear growth curves

# In[133]:

import sys
print(sys.executable)
import numpy as np
import pandas as pd
import os
print(os.getcwd())
from IPython.display import display
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
from prophet import Prophet
from prophet.diagnostics import cross_validation, performance_metrics

# In[134]:

# Custom functions:
import src.colorsetup

# ## Input data
# Peyton Manning (football player) data

# In[135]:

data_path = 'https://raw.githubusercontent.com/PinkWink/DataScience/master/data/07.%20example_wp_peyton_manning.csv'
peyton = pd.read_csv(data_path)
print("Data shape: {}".format(peyton.shape))
peyton.head()

# In[136]:

peyton.plot()
plt.title("Wikipedia searches for Peyton")
plt.grid()
plt.show()

# In[137]:

# Let's do a log transformation
peyton['y']= np.log(peyton['y'])
display(peyton.head())
peyton.ds.max()

# In[138]:

peyton.plot()
plt.grid()
plt.show()

# # 1) Fitting a vanilla model

# In[139]:

m= Prophet()
m.fit(peyton)

# In[140]:

# Forecast 365 days into future
# prophet requires a blank df to input predictions
future= m.make_future_dataframe(periods= 365)
# display(peyton)
# display(future.head())
# display(future.tail())

# In[141]:

# Populate forecast
forecast= m.predict(future)
display(forecast.head())
print(forecast.columns)

# In[142]:

display(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
forecast[forecast.ds >= '2016-01-20'][['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head()

# ## Cross validate

# In[ ]:

# Period specifiy the cutoffs, every 180 days
df_cv= cross_validation(m, horizon= '365 days', period= '180 days', initial= '730 days')

# In[144]:

display(peyton.head())
df_cv.head()

# In[145]:

# We predicted a year into the future
first_cut= df_cv[df_cv.cutoff == datetime(2010,2,15)]
last_cut= df_cv[df_cv.cutoff == datetime(2015,1,20)]
display(first_cut.tail())
print("CV shape: {}, First cutoff: {}, Number of cutoffs: {}".format(df_cv.shape, first_cut.shape, len(df_cv.cutoff.unique())))

# In[189]:

metrics= performance_metrics(df_cv)
display(metrics[['horizon', 'rmse', 'mape']])
print("Mean-RMSE: {:.4f} and Mean-MAPE: {:.2%}".format(np.mean(metrics['rmse']), np.mean(metrics['mape'])))
print("Horizon of 365 days RMSE: {:.4f} and MAPE: {:.2%}".format(metrics.iloc[metrics.shape[0]-1]['rmse'], metrics.iloc[metrics.shape[0]-1]['mape']))

# In[202]:

h_1_mape= metrics.iloc[metrics.shape[0]-1]['mape']
h_1_rmse= metrics.iloc[metrics.shape[0]-1]['rmse']

image_dir= '../../../plots/time_series_analysis/'
plot_name= 'initial.prophet.model.png'

forecast_plot= m.plot(forecast, figsize= (17,7), xlabel= "Time", ylabel= "Wikipedia searches for Peyton (log)")
ax = forecast_plot.gca()
ax.set_title("Initial model= Prophet(), MAPE: {:.2%} and RMSE: {:.3f}".format(h_1_mape, h_1_rmse))
ax.set_xlabel("Time")
ax.set_ylabel("Wikipedia searches for Peyton (log)")

forecast_plot.savefig(image_dir+plot_name)

# In[190]:

plot_name= 'initial.prophet.model.cross.validation.png'

fig, axes= plt.subplots(1,2, sharey= True)
fig.set_figwidth(20)

axes[0].plot(first_cut.ds, first_cut.y, color= "black", label= "True")
axes[0].plot(first_cut.ds, first_cut.yhat, color= "orange", label= "Predicted")
axes[0].fill_between(first_cut.ds, first_cut.yhat_lower, first_cut.yhat_upper, color= "orange", alpha= 0.2)
axes[0].set_title("First fold")
axes[0].legend(loc= "upper left")

axes[1].plot(last_cut.ds, last_cut.y, color= "black", label= "True")
axes[1].plot(last_cut.ds, last_cut.yhat, color= "orange", label= "Predicted")
axes[1].fill_between(last_cut.ds, last_cut.yhat_lower, last_cut.yhat_upper, color= "orange", alpha= 0.2)
axes[1].set_title("Last fold (11th)")
axes[1].legend(loc= "upper left")

axes[0].grid(alpha= 0.3)
axes[1].grid(alpha= 0.3)

plt.tight_layout()
fig.savefig(image_dir+plot_name, transparent= True)

# In[191]:

forecast_components= m.plot_components(forecast)

# # 2) Add Holidays information
# In our case playoffs and superbowls dates

# In[192]:

playoffs = pd.DataFrame({
  'holiday': 'playoff',
  'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16',
                        '2010-01-24', '2010-02-07', '2011-01-08',
                        '2013-01-12', '2014-01-12', '2014-01-19',
                        '2014-02-02', '2015-01-11', '2016-01-17',
                        '2016-01-24', '2016-02-07']),
  'lower_window': 0, # these help us specify spillover into previous and future days which will be treated as own holidays
  'upper_window': 1,
})

# display(playoffs)

superbowls = pd.DataFrame({
  'holiday': 'superbowl',
  'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),
  'lower_window': 0,
  'upper_window': 1,
})

# display(superbowls)
holidays= pd.concat((playoffs, superbowls))
holidays.head()

# In[193]:

# fit and predict
m_holidays= Prophet(holidays= holidays)
forecast_holidays = m_holidays.fit(peyton).predict(future)

# In[194]:

forecast_holidays.columns

# In[195]:

# We want to see the holiday effects:
forecast_holidays[(forecast_holidays['playoff'] + forecast_holidays['superbowl']).abs() > 0][
        ['ds', 'yhat','playoff', 'superbowl']].tail()

# In[196]:

forecast_holidays_components= m_holidays.plot_components(forecast_holidays)

# Peyton won Superbowls: 2016. While losing 2010 and 2014.

# In[ ]:

df2_cv= cross_validation(m_holidays, horizon= '365 days', period= '180 days', initial= '730 days')

# In[203]:

metrics_2= performance_metrics(df2_cv)
display(metrics_2[['horizon', 'rmse', 'mape']])
print("Adding Holidays: Mean RMSE: {:.4f} Mean MAPE: {:.2%}".format(np.mean(metrics_2['rmse']), np.mean(metrics_2['mape'])))
print("Horizon of 365 days RMSE: {:.3f} and MAPE: {:.2%}".format(metrics_2.iloc[metrics_2.shape[0]-1]['rmse'], metrics_2.iloc[metrics_2.shape[0]-1]['mape']))

# In[256]:

h_1_mape= metrics_2.iloc[metrics_2.shape[0]-1]['mape']
h_1_rmse= metrics_2.iloc[metrics_2.shape[0]-1]['rmse']

plot_name= 'prophet.holidays.model.png'

forecast_holidays_plot= m_holidays.plot(forecast_holidays, figsize= (17,7))

ax = forecast_holidays_plot.gca()
ax.set_title("Model= Prophet(holidays), MAPE: {:.2%} and RMSE: {:.3f}".format(h_1_mape, h_1_rmse))
ax.set_xlabel("Time")
ax.set_ylabel("Wikipedia searches for Peyton (log)")

plt.show()
forecast_holidays_plot.savefig(image_dir+plot_name)

# # 3) Add another seasonality
# Besides yearly, weekly, and daily. Let's add monthly seasonality

# In[209]:

m_holidays_extra_s= Prophet(holidays= holidays)
# Increasing the number of Fourier components allows the seasonality to change more quickly (at risk of overfitting). Default values for yearly
# and weekly seasonalities are 10 and 3 respectively.
m_holidays_extra_s.add_seasonality(name= 'monthly', period= 30.5, fourier_order= 5)
m_holidays_extra_s.fit(peyton)

# In[210]:

fcst_month = m_holidays_extra_s.predict(future)

# In[213]:

monthly_components= m_holidays_extra_s.plot_components(fcst_month)

# In[ ]:

df3_cv= cross_validation(m_holidays_extra_s, horizon= '365 days', period= '180 days', initial= '730 days')

# In[217]:

metrics_3= performance_metrics(df3_cv)
display(metrics_3[['horizon', 'rmse', 'mape']])
print("Adding Holidays: Mean RMSE: {:.4f} Mean MAPE: {:.2%}".format(np.mean(metrics_3['rmse']), np.mean(metrics_3['mape'])))
print("Horizon of 365 days RMSE: {:.3f} and MAPE: {:.2%}".format(metrics_3.iloc[metrics_3.shape[0]-1]['rmse'], metrics_3.iloc[metrics_3.shape[0]-1]['mape']))

# In[225]:

h_1_mape= metrics_3.iloc[metrics_3.shape[0]-1]['mape']
h_1_rmse= metrics_3.iloc[metrics_3.shape[0]-1]['rmse']

# plot_name= 'initial.prophet.model.png'

extra_seasonality_plot= m_holidays_extra_s.plot(fcst_month, figsize= (17,7))

ax = extra_seasonality_plot.gca()
ax.set_title("Initial model= Prophet(holidays).add_seasonality, MAPE: {:.2%} and RMSE: {:.3f}".format(h_1_mape, h_1_rmse))
ax.set_xlabel("Time")
ax.set_ylabel("Wikipedia searches for Peyton (log)")

plt.show()

# # 4) Adding a regressor

# In[226]:

# creating an indicator variable for NFL sundays
def nfl_sundays(ds):
    """
    NFL season: August, September, October, November, December, January, February
    Sunday => 6 | range: 0-6 => Monday-Sunday
    """
    date= pd.to_datetime(ds)
    if date.weekday() == 6 and (date.month > 8 or date.month < 2):
        return 1
    else:
        return 0

# In[227]:

peyton['nfl_sunday']= peyton['ds'].apply(nfl_sundays)
peyton.head()

# In[228]:

m= Prophet()
m.add_regressor('nfl_sunday')
m.fit(peyton)

# In[231]:

# Regressor must also be available in future df
future['nfl_sunday'] = future['ds'].apply(nfl_sundays)
forecast = m.predict(future)
fig = m.plot_components(forecast)

# In[ ]:

df4_cv= cross_validation(m, horizon= '365 days', period= '180 days', initial= '730 days')

# In[244]:

metrics_4= performance_metrics(df4_cv)
display(metrics_4[['horizon', 'rmse', 'mape']])
print("Adding Holidays: Mean RMSE: {:.4f} Mean MAPE: {:.2%}".format(np.mean(metrics_4['rmse']), np.mean(metrics_4['mape'])))
print("Horizon of 365 days RMSE: {:.3f} and MAPE: {:.2%}".format(metrics_4.iloc[metrics_4.shape[0]-1]['rmse'], metrics_4.iloc[metrics_4.shape[0]-1]['mape']))

# In[245]:

h_1_mape= metrics_4.iloc[metrics_4.shape[0]-1]['mape']
h_1_rmse= metrics_4.iloc[metrics_4.shape[0]-1]['rmse']

# plot_name= 'initial.prophet.model.png'

regressor_plot= m.plot(forecast, figsize= (17,7))

ax = regressor_plot.gca()
ax.set_title("Initial model= Prophet() plus regressor, MAPE: {:.2%} and RMSE: {:.3f}".format(h_1_mape, h_1_rmse))
ax.set_xlabel("Time")
ax.set_ylabel("Wikipedia searches for Peyton (log)")

plt.show()

# ## We can specifiy if there's some change taking place
# For instance COVID

# In[25]:

# These are points where trend has changed
print('originally: ',m.changepoints[:5])

# you can specify changepoints if you want trend to only be allowed at certain points
m_c = Prophet(changepoints=['2014-01-01'])

print('\nnow: ',m_c.changepoints[:5])

# # 5) Combine Holidays and regressor

# In[249]:

m_holidays_regressor= Prophet(holidays= holidays)
fcst_holidays_regressor=  m_holidays_regressor.add_regressor('nfl_sunday').fit(peyton).predict(future)

# In[250]:

holidays_regressor= m_holidays_regressor.plot_components(fcst_holidays_regressor)

# In[ ]:

df5_cv= cross_validation(m_holidays_regressor, horizon= '365 days', period= '180 days', initial= '730 days')

# In[254]:

metrics_5= performance_metrics(df5_cv)
display(metrics_5[['horizon', 'rmse', 'mape']])
print("Adding Holidays: Mean RMSE: {:.4f} Mean MAPE: {:.2%}".format(np.mean(metrics_5['rmse']), np.mean(metrics_5['mape'])))
print("Horizon of 365 days RMSE: {:.3f} and MAPE: {:.2%}".format(metrics_5.iloc[metrics_5.shape[0]-1]['rmse'], metrics_5.iloc[metrics_5.shape[0]-1]['mape']))

# In[257]:

import session_info
session_info.show()

# In[ ]:


#!/usr/bin/python3
"""
   Py script that adds all args to py
   list and save in a file
"""
import json
import sys

def save_to_json_file(my_obj, filename):
    with open(filename, 'w') as json_file:
        json_string = json.dumps(my_obj)
        json_file.write(json_string)

def load_from_json_file(filename):
    with open(filename, 'r') as json_file:
        json_string = json_file.read()
        return json.loads(json_string)

try:
    data = load_from_json_file('add_item.json')
except FileNotFoundError:
    data = []

for item in sys.argv[1:]:
    data.append(item)

save_to_json_file(data, 'add_item.json')


#!/usr/bin/python

import threading
from time import sleep
import socket
from datetime import datetime
import sys
import json
import logging

someLock = threading.Lock()

class Worker:
    def __init__(self,port,worker_id):
        """
            Attributes:
                self.port: port number of the worker
                self.worker_id: worker_id of the worker
                self.pool: dictionary containing all the currently runnning tasks
                           key: task_id
                           value: remaining duration
                self.server_port: port number to use when sending messages to the master
        """
        self.port=port
        self.worker_id=worker_id
        self.pool=dict()
        self.server_port=5001
        self.wait_list = []
        self.algo=None

    def listen_master(self):
        """
            This function listens for tasks to do.
            The worker acts like the server during the communication

        """
        recv_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        recv_socket.bind(('',self.port)) # listens on the specified port
        recv_socket.listen(5)
        while True:
            conn_socket, addr = recv_socket.accept()
            task = conn_socket.recv(2048).decode() #recv the msg for task launch by master
            self.execution_pool(task) #add task to the execution pool
            conn_socket.close()

    def update_master(self,task=None):
        """
            This is called when a task is completed.
            In this case, the worker file acts as a client and sends the message to the master
            indicating the completion of the given task
            message = [worker_id, task_id]
        """
        if (task):
            message1 = list((self.worker_id, task[0]))
            message = json.dumps(message1) # Converting the list to string
            message = message + "?" + str(datetime.now()) + "?"+str(task[1])
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                s.connect(('localhost', self.server_port))
                s.send(message.encode())

    def execution_pool(self,task):
        """
            This function adds the given task to the execution pool.
            key = task_id
            value = remaining time of the task (initialised to its duration)
        """
        task=json.loads(task)
        task_id = task[0][0]
        remain_time = task[0][1] + 1
        self.algo=task[1]
        # self.pool[task_id]=remain_time
        self.wait_list.append(tuple((task_id, remain_time)))

    def task_monitor(self):
        """
            This function updates the remaining times of the tasks in the execution pool.
            If any of the tasks has been completed, it updates the master about it.
            tempSet is used to get the set of keys in the execution pool at that instant.
        """
        exec_time=0.0
        someLock.acquire()

        while (self.wait_list):
            ta = self.wait_list.pop()
            self.pool[ta[0]] = ta[1]
            logging.debug("Started task {};{}".format(ta[0],self.algo))

        tempKeys = list(self.pool.keys())
        tempList = []
        for task_id in tempKeys:
            self.pool[task_id]-=1    #reduce time by 1 unit every clock cycle --sleep the threa,d for 1s

            if self.pool[task_id]==0: #the task execution is completed
                logging.debug('Completed task {};{}'.format(task_id,self.algo))
                exec_time=self.duration(task_id)
                self.update_master((task_id,exec_time)) #t2 must update the master about the status of the task completion
                tempList.append(task_id)
        for i in tempList:
            self.pool.pop(i)
        someLock.release()

    def clock(self):
        """
            Simulates a clock
        """
        while True:
            threading.Thread(target=self.task_monitor()).start()
            # self.task_monitor()
            sleep(1)

    def duration(self,task_id):
        start_time=0
        file = open("worker" + str(self.worker_id) + ".log", "r")
        time_elapsed=0
        for line in file.readlines():
            temp=line.split(";")
            if task_id==temp[1].split()[2]:
              if (temp[1].split()[0] == "Started"):
                  start_time = datetime.strptime(temp[3][:-2],'%Y-%m-%d %H:%M:%S,%f')
              else:
                  time_elapsed = datetime.strptime(temp[3][:-2], '%Y-%m-%d %H:%M:%S,%f') - start_time

        return time_elapsed

if __name__ == "__main__":

    port=int(sys.argv[1])
    worker_id=int(sys.argv[2])
    worker=Worker(port,worker_id)

    logging.basicConfig(filename="worker"+str(worker_id)+".log", level=logging.DEBUG,format='%(filename)s;%(message)s;%(asctime)s')

    t1 = threading.Thread(target=worker.listen_master)
    t2 = threading.Thread(target=worker.clock)

    t1.start()
    t2.start()
    t1.join()
    t2.join()


# -*- coding: utf-8 -*-
#BEGIN_HEADER
import logging
import os
from ReactiveTransportSimulator.Utils.ReactiveTransportSimulatorUtil import ReactiveTransportSimulatorRunBatchUtil
from ReactiveTransportSimulator.Utils.ReactiveTransportSimulatorUtil import ReactiveTransportSimulatorRun1DUtil
from installed_clients.KBaseReportClient import KBaseReport
from installed_clients.DataFileUtilClient import DataFileUtil

#END_HEADER

class ReactiveTransportSimulator:
    '''
    Module Name:
    ReactiveTransportSimulator

    Module Description:
    A KBase module: ReactiveTransportSimulator
    '''

    ######## WARNING FOR GEVENT USERS ####### noqa
    # Since asynchronous IO can lead to methods - even the same method -
    # interrupting each other, you must be *very* careful when using global
    # state. A method could easily clobber the state set by another while
    # the latter method is running.
    ######################################### noqa
    VERSION = "0.0.1"
    GIT_URL = "https://github.com/kchen8921/ReactiveTransportSimulator-KBase.git"
    GIT_COMMIT_HASH = "c18310a29104053f0b99f5c8f7ba8f80cdbb5b34"

    #BEGIN_CLASS_HEADER
    #END_CLASS_HEADER

    # config contains contents of config file in a hash or None if it couldn't
    # be found
    def __init__(self, config):
        #BEGIN_CONSTRUCTOR
        self.callback_url = os.environ['SDK_CALLBACK_URL']
        self.shared_folder = config['scratch']
        logging.basicConfig(format='%(created)s %(levelname)s: %(message)s',
                            level=logging.INFO)
        #END_CONSTRUCTOR
        pass

    def run_batch_model(self, ctx, params):
        """
        This function enables users to run a pflotran batch model with FBA model and initial conditions as inputs
        :param params: instance of mapping from String to unspecified object
        :returns: instance of type "ReportResults" -> structure: parameter
           "report_name" of String, parameter "report_ref" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN run_batch_model
        params['shared_folder'] = self.shared_folder
        pu = ReactiveTransportSimulatorRunBatchUtil(params)
        output = pu.run_batch_model()
        #END run_batch_model

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method run_batch_model return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]

    def run_1d_model(self, ctx, params):
        """
        This function enables users to run a pflotran 1D model with FBA model and initial conditions as inputs
        :param params: instance of mapping from String to unspecified object
        :returns: instance of type "ReportResults" -> structure: parameter
           "report_name" of String, parameter "report_ref" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN run_1d_model
        params['shared_folder'] = self.shared_folder
        pu = ReactiveTransportSimulatorRun1DUtil(params)
        output = pu.run_1d_model()
        #END run_1d_model

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method run_1d_model return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
    def status(self, ctx):
        #BEGIN_STATUS
        returnVal = {'state': "OK",
                     'message': "",
                     'version': self.VERSION,
                     'git_url': self.GIT_URL,
                     'git_commit_hash': self.GIT_COMMIT_HASH}
        #END_STATUS
        return [returnVal]


def read_file(file):
    line_list = []
    with open(file, 'r') as f:
        for line in f.readlines():
            line_list.append(line.strip('\n'))
        return(line_list)

lines = read_file('contacts.txt')
print(lines)


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 17 09:42:33 2021

@author: s1332488
"""

import sys
sys.path.append('/home/s1332488/code/S1/')
from snappyHelper import createP
import os.path as path

INPUT = sys.argv[1]
OUTPUT = '../calib/'+INPUT.split('.')[0] + '_calib.dim'

if not path.exists(OUTPUT):
    createP('Calibration',
            INPUT,
            writeout=OUTPUT)


import urllib.request

import gevent
from gevent import monkey

monkey.patch_all()

def picdown(name, url):
    req = urllib.request.urlopen(url)
    img_content = req.read()
    with open(name, "wb") as f:
        f.write(img_content)

def main():
    gevent.joinall([gevent.spawn(picdown,"c:/devTools/pywhl/1.jpg",
                            "https://rpic.douyucdn.cn/live-cover/roomCover/2019/08/21/4f240e51535eab040f2b6c710d006d07_big.jpg")
                    ,gevent.spawn(picdown,"c:/devTools/pywhl/2.jpg","https://apic.douyucdn.cn/upload/avatar_v3/201905/f1a69d5280cd44b4881c8764fa24151f_big.jpg")])

if __name__ == '__main__':
    main()


# https://drive.google.com/file/d/1HGPRvqvPN5kyVOUwwkM6X3Ua8kwnFTan/view?usp=sharing

# Написать программу, которая генерирует в указанных пользователем границах: ● случайное целое число, ● случайное
# вещественное число, ● случайный символ. Для каждого из трех случаев пользователь задает свои границы диапазона.
# Например, если надо получить случайный символ от 'a' до 'f', то вводятся эти символы. Программа должна вывести на
# экран любой символ алфавита от 'a' до 'f' включительно.

import random

# случайное целое число
bottom = int(input('Введите целое число (нижняя граница) -> '))
top = int(input('Введите целое число (верхняя граница больше нижней) -> '))

res = random.randint(bottom, top + 1)
print(f'Ваше случайное число: {res}')

# случайное вещественное число
bottom = float(input('Введите вещественное число (нижняя граница) -> '))
top = float(input('Введите вещественное число (верхняя граница больше нижней) -> '))

res = random.uniform(top, bottom)

print(f'Ваше случайное число: {res}')

# случайный символ
bottom = ord(input('Введите случайный символ (нижняя граница) -> '))
top = ord(input('Введите случайный символ (верхняя граница больше нижней) -> '))

res = chr(random.randint(bottom, top + 1))
print(f'Ваше случайный символ: {res}')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 29 20:19:18 2020
Example to run survival model.
@author: wouterpotters
"""
import configparser
import matplotlib
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), './../'))
from covid19_ICU_admission import load_data, preprocess, prepare_for_learning
from get_feature_set import get_1_premorbid, \
                            get_2_clinical_presentation, \
                            get_3_laboratory_radiology_findings, \
                            get_5_premorbid_clin_rep_lab_rad

import matplotlib.pyplot as plt
from lifelines import *
import pandas as pd
import numpy as np
from sklearn.experimental import enable_iterative_imputer  # Required to enable experimental iterative imputer
from sklearn.impute import SimpleImputer, IterativeImputer
from sklearn.preprocessing import StandardScaler, RobustScaler

plt.rcParams["font.family"] = "Times New Roman"
matplotlib.rcParams.update({'font.size': 14})

c1 = ( 57/255, 106/255, 177/255)  # blue
c2 = (218/255, 124/255,  48/255)  # orange
c3 = ( 83/255,  81/255,  84/255)  # black
c4 = ( 62/255, 150/255,  81/255)  # green
c5 = (204/255,  37/255,  41/255)  # red

englishvar_to_dutchnames = {'age_yrs':'Leeftijd (jaren)','gender_male':'Geslacht (man)','gender':'Geslacht (man)','hypertension':'Hypertensie (ja)','Coagulation_disorder_1_1':'Stollingsstoornis (ja)','autoimm_disorder_1':'Auto-immuunziekte (ja)','rheuma_disorder':'Reumatologische ziekte (ja)','organ_transplant_1':'Orgaan donatie ondergaan (ja)',
                            'diabetes_without_complications':'DM (zonder complicaties) (ja)','diabetes_complications':'DM (met complicaties) (ja)','ccd':'Chronische hartziekte (ja)','ckd':'Chronische nierziekte (ja)','cnd':'Chronische neurologische aandoening (ja)','cpd':'chronische longziekte (ja)','asthma':'Asthma (ja)','Dementia':'dementie (ja)',
                            'mld':'Milde leveraandoening (ja)','live_disease':'Matig-ernstige leveraandoening (ja)','mneoplasm':'Maligniteit (ja)','chd':'Chronische hematologische ziekte (ja)','alcohol':'Alcohol abusus (ja)','Smoking in History or Active':'Roken actief of in verleden (ja)','obesity':'Obesitas (BMI > 30) (ja)',
                            'home_medication':'Thuismedicatie (ja)','immune_sup':'Immunosupressiva (ja)','ace_i_spec_1':'Gebruik ACE inhibitor (ja)','Corticosteroid_type_1':'Corticosteroiden gebruik (ja)','sys_bp':'Systolische bloeddruk (mmHg)','dias_bp':'Diastolische bloeddruk (mmHg)','HtR':'hart frequentie (1/min)','rtr':'Ademhalingsfrequentie (1/min)',
                            'oxygen_saturation':'Saturatie (%)','Temperature':'Temperatuur (ºC)','Antiviral_agent_1':'Behandeling met antivirale middelen (ja)','Oxygen_therapy_1':'zuurstof therapie (ja)','Non_invasive_ventilation_1':'non-invasieve beademing (ja)','Invasive_ventilation_1':'Invasieve beademing (ja)','resucitate':'Niet reanimeren (ja)',
                            'intubate_yesno':'Niet intuberen (ja)','CT_thorax_performed':'CT thorax verricht op SEH (ja)','corads_admission':'CO-RAD score  (1-5)','pcr_pos':'Corona virus PCR + op SEH (ja)','Coagulation_disorder_1_1':'Stollingsstoornis (ja)',
                            'Acute_renal_injury_Acute_renal_failure_1_1':'Nierfunctie stoornis waarvoor dialyse (ja)','diabetes_complications':'Diabetes met complicaties (ja)','cpd':'chronische longziekte (ja)','Smoking':'Actieve roker (ja)','obesity':'Obesitas (ja)','immune_sup':'immunosupressiva (ja)','ace_i_spec_1':'Gebruik ACE remmer (ja)',
                            'sys_bp':'Systolische bloeddruk (mmHg)','dias_bp':'Diastolische bloeddruk (mmHg)','HtR':'hart frequentie (1/min)','capillary_refill':'Verlengde capillaire refill (ja)','rtr':'Ademhalingsfrequentie (1/min)','oxygen_saturation':'Zuurstof saturatie (%)','SaO2_1':'SaO2 (%)','PaO2_1_Arterial':'PaO2 arteriëel (kPa)',
                            'PaO2_1_Venous':'PaO2 veneus (kPa)', 'PaO2_1_Capillary':'PaO2 capillair (kPa)', 'PaO2_1_nan':'PaO2 onbekend (kPa)', 'PCO2_1':'pCO2 (kPa)','fio2_1':'FiO2 (%)','Temperature':'Temperatuur (ºC)','eye':'EMV','Antiviral_agent_1':'Anti-virale middelen (ja)','Antibiotic_1':'Antibiotica (ja)',
                            'Corticosteroid_1':'Corticosteroiden voor ARDS (ja)','Corticosteroid_type_1':'gebruik corticosteroiden (ja)','Oxygen_therapy_1':'Zuurstof behandeling (ja)',
                            'Non_invasive_ventilation_1':'Niet-invasieve beademing (ja)','Invasive_ventilation_1':'Invasieve beademing (ja)','resucitate':'Niet reanimeren (ja)','intubate_yesno':'Niet intuberen (ja)','auxiliary_breathing_muscles':'Gebruik van extra ademhalingsspieren (ja)','fever':'Koorts gehad sinds eerste klachten (ja)',
                            'Anosmia':'Anosmie (ja)','Rhinorrhoea':'Rhinorrhoea (ja)','Sore_throat':'Keelpijn (ja)','cough_sputum':'Hoest met sputum (ja)','cough_sputum_haemoptysis':'Hoest met bloederig slijm/ haemoptoë (ja)','Arthralgia':'Arthralgie (ja)','Myalgia':'Spierpijn (ja)','Fatigue_Malaise':'Vermoeidheid/ algehele malaise (ja)',
                            'Abdominal_pain':'Buikpijn (ja)','Vomiting_Nausea':'Misselijkheid/ overgeven (ja)','Diarrhoea':'diarree (ja)','Dyspnea':'Dyspneu (ja)','Wheezing':'Piepende ademhaling (ja)','Chest_pain':'Pijn op de borst (ja)','ear_pain':'oorpijn (ja)','Bleeding_Haemorrhage':'bloeding (ja)','Headache':'Hoofdpijn (ja)',
                            'confusion':'Veranderd bewustzijn (ja)','Seizures':'Insulten (ja)','infiltrates_2':'Infiltraat op X-thorax (ja)','corads_admission':'CO-RADS (1-5)','Glucose_unit_1_1':'glucose (mmol/L)','Sodium_1_1':'Natrium (mmol/L)','Potassium_1_1':'Kalium (mmol/L)','Blood_Urea_Nitrogen_value_1':'Ureum (mmol/L)',
                            'Creatinine_value_1':'Creatinine (µmol/L)','Calcium_adm':'Ca (totaal) (mmol/L)','ferritine_admin_spec':'ferritine (mg/L)','creatininekinase':'CK (U/L)','d_dimer':'D-Dimeer (nmol/L)','AST_SGOT_1_1':'ASAT (U/L)','ALT_SGPT_1_1':'ALAT (U/L)','Total_Bilirubin_2_1':'totaal Bilirubine (IE)','LDH':'LDH (U/L)',
                            'PH_value_1':'Zuurgraad (pH)','Lactate_2_1':'Lactaat (mmol/L)','crp_1_1':'CRP (mg/L)','Haemoglobin_value_1':'Hb (mmol/L)','Platelets_value_1':'trombocyten (x10^9/L)','WBC_2_1':'Leukocyten (x10^3/µL)','Lymphocyte_1_1':'Lymfocyten (x10^9/L)','Neutrophil_unit_1':'Neutrofielen (x10^9/L)','INR_1_1':'INR',
                            'pt_spec':'PT (sec)','fibrinogen_admin':'Fibrinogeen (g/L)','pcr_pos':'Corona PCR + (ja)','Adenovirus':'Adenovirus (ja)','RSV_':'RS virus (ja)','Influenza':'Influenza virus (ja)','Bacteria':'Sputumkweek (ja)','days_untreated':'Tijd sinds eerste klachten (dagen)','irregular':'Onregelmatige hartslag (ja)',
                            'DNR_yesno':' (ja)','healthcare_worker':'Zorgmedewerker (ja)','infec_resp_diagnosis':'Andere infectieuze ademhalingsdiagnose','Blood_albumin_value_1':'Albumine (g/L)','culture':'Bloedkweek (positief)','APT_APTR_1_1':'APTT (sec)','uses_n_medicine':'Aantal thuismedicamenten'}

class StandardScaler_min2cols(StandardScaler):
    def __init__(self,columns,copy=True, with_mean=False, with_std=True):
        self.scaler = StandardScaler(copy, with_mean=with_mean, with_std=with_std)
        self.columns = columns

    def fit(self, X, y=None):
        self.scaler.fit(X[self.columns], y)
        return self

    def transform(self, X, y=None, copy=None):
        init_col_order = X.columns
        X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]), columns=self.columns)
        X_not_scaled = X.iloc[:, ~X.columns.isin(self.columns)]
        return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]

class RobustScaler_min2cols(RobustScaler):
    def __init__(self, columns, copy=True, with_centering=True,
                 with_scaling=True, quantile_range=(25.0, 75.0)):
        self.scaler = RobustScaler(with_centering=with_centering,
                                   with_scaling=with_scaling,
                                   quantile_range=quantile_range,
                                   copy=copy)
        self.columns = columns

    def fit(self, X, y=None):
        self.scaler.fit(X[self.columns], y)
        return self

    def transform(self, X, y=None, copy=None):
        init_col_order = X.columns
        X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]), columns=self.columns)
        X_not_scaled = X.iloc[:, ~X.columns.isin(self.columns)]
        return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]

def simple_estimates(features):
    T = features['Time to event']
    E = features['Event death']
    fig, axes = plt.subplots(1, 1)

    kmf = KaplanMeierFitter().fit(T, E, label='KaplanMeierFitter')
    kmf.plot_survival_function()
    axes.set_xlim(0, 21)
    axes.set_ylim(0, 1)

    axes.set_xlabel('Tijd in dagen')
    axes.set_ylabel('Cumulatieve survival')
    plt.savefig('simple_estimate.png')
    plt.show()

def KM_age_groups(features):
    T = features['Time to event']
    E = features['Event death']

    #age_70_plus = features['age_yrs'] >= 70.
    age_70_plus = features['Leeftijd (jaren)'] >= 70.
    print('Alle patiënten: n=', len(age_70_plus))
    print('Leeftijd ≥ 70: n=', sum(age_70_plus))
    print('Leeftijd < 70: n=', sum(~age_70_plus))

    was_not_on_icu = data[['Levend ontslagen en niet heropgenomen - waarvan niet opgenomen geweest op IC',
                           'Levend dag 21 maar nog in het ziekenhuis - niet op IC geweest',
                           'Dood op dag 21 - niet op IC geweest']].any(axis=1)
    was_on_icu = data[['Levend ontslagen en niet heropgenomen - waarvan opgenomen geweest op IC',
                       'Levend dag 21 maar nog in het ziekenhuis - op IC geweest',
                       'Levend dag 21 maar nog in het ziekenhuis - waarvan nu nog op IC',
                       'Dood op dag 21 - op IC geweest']].any(axis=1)
    print('Alle patiënten: n=', (np.nansum(was_on_icu)+np.nansum(was_not_on_icu)))
    print('ICU yes: n=', np.nansum(was_on_icu))
    print('ICU no: n=', np.nansum(was_not_on_icu))

    icu = False
    special = False
    fig, axes = plt.subplots(1, 1)

    if special:
        kmf1 = KaplanMeierFitter().fit(T[features['Leeftijd (jaren)'] <= 47],
                                       E[features['Leeftijd (jaren)'] <= 47], label='Leeftijd <= 47 jaar (n=260)')
        kmf2 = KaplanMeierFitter().fit(T[np.logical_and(features['Leeftijd (jaren)'] > 47, features['Leeftijd (jaren)'] <= 61)],
                                       E[np.logical_and(features['Leeftijd (jaren)'] > 47, features['Leeftijd (jaren)'] <= 61)], label='Leeftijd > 47 jaar, <= 61 (n=260)')
        kmf3 = KaplanMeierFitter().fit(T[features['Leeftijd (jaren)'] > 61],
                                       E[features['Leeftijd (jaren)'] > 61], label='Leeftijd > 61 jaar (n=876)')
        kmf1.plot_survival_function(color=c5)
        kmf2.plot_survival_function(color=c4)
        kmf3.plot_survival_function(color=c3)
    else:
        if icu:
            kmf1 = KaplanMeierFitter().fit(T[was_on_icu], E[was_on_icu], label='Wel op ICU geweest')
            kmf2 = KaplanMeierFitter().fit(T[was_not_on_icu], E[was_not_on_icu], label='Niet op ICU geweest')
        else: # age 70
            kmf1 = KaplanMeierFitter().fit(T[age_70_plus], E[age_70_plus], label='Leeftijd ≥ 70 jaar')
            kmf2 = KaplanMeierFitter().fit(T[~age_70_plus], E[~age_70_plus], label='Leeftijd < 70 jaar')

        kmf3 = KaplanMeierFitter().fit(T, E, label='Alle patienten')
        if icu:
            kmf1.plot_survival_function(color=c4)
            kmf2.plot_survival_function(color=c5)
            kmf3.plot_survival_function(color=c3)
        else:
            kmf1.plot_survival_function(color=c1)
            kmf2.plot_survival_function(color=c2)
            kmf3.plot_survival_function(color=c3)

    axes.set_xticks([1, 5, 9, 13, 17, 21])
    axes.set_xticklabels(['1','5','9','13','17','21'])
    axes.set_xlabel('Aantal dagen sinds opnamedag')
    axes.set_ylabel('Proportie overlevend')

    axes.set_xlim(0, 21)
    axes.set_ylim(0, 1)
    titledict = {'fontsize': 18,
                 'fontweight': 'bold',
                 'verticalalignment': 'baseline',
                 'horizontalalignment': 'center'}
    # plt.title('COVID-PREDICT survival functie tot t = 21 dagen (n='+str(len(T))+')',fontdict=titledict)
    plt.tight_layout()
    if icu:
        plt.savefig('KM_survival_curve_ICU.png', format='png', dpi=300, figsize=(20,20), pad_inches=0, bbox_inches='tight')
    else:
        plt.savefig('KM_survival_curve_DEATH.png', format='png', dpi=300, figsize=(20,20), pad_inches=0, bbox_inches='tight')
    plt.show()

    return kmf1, kmf2, kmf3

def cox_ph_loho(features, hazard_ratios=False,
                imputation_method='median',
                minimal_n=10,
                pvalue=0.05):
    cindex = []
    n = []
    for test_hospital in features['hospital'].unique():
        print(test_hospital)
        train_set = features[features['hospital'] != test_hospital]
        test_set = features[features['hospital'] == test_hospital]

        if len(test_set) >= minimal_n:
            test_set = test_set.drop(columns=['hospital'])

            cph, p, imputer, scaler = cox_ph(train_set, hazard_ratios=False,
                                             imputation_method=imputation_method,
                                             return_scaler_imputer=True)

            test_set = pd.DataFrame(imputer.transform(test_set), columns=test_set.columns)
            test_set = scaler.transform(test_set)

            hazard_ratios_sum = cph.summary
            print(hazard_ratios_sum)
            sel_corr = hazard_ratios_sum[hazard_ratios_sum['p'] <= pvalue]
            c = cph.score(test_set, scoring_method="concordance_index")
            print('test hospital:', test_hospital, 'test c-index', c)
            print(sel_corr)
            cindex += [c]
            n += [len(test_set)]
        else:
            print('test hospital:', test_hospital, ' has <= ',minimal_n,' records. Cannot determine concordance.')
    return cindex, n

def cox_ph(features, hazard_ratios=False,
           imputation_method='median', return_scaler_imputer=False,
           show_progress=True,
           penalizer=0.,
           l1_ratio=0.):
    train_set = features.drop(columns=['hospital']) # NOTE aids_hiv is an outlier for plotting coefs

    # impute nan with median; note that time to event and event death cannot contain nan
    if imputation_method == 'iterative':
        imputer = IterativeImputer(missing_values=np.nan,
                                   initial_strategy='median')
    else:
        imputer = SimpleImputer(missing_values=np.nan, strategy=imputation_method)
    train_set = pd.DataFrame(imputer.fit_transform(train_set), columns=train_set.columns)

    # scale data using zscore
    scaler = RobustScaler_min2cols(columns=train_set.columns)
    train_set = scaler.fit_transform(train_set)

    cph = CoxPHFitter(penalizer=penalizer, l1_ratio=l1_ratio)
    cph.fit(train_set, duration_col='Time to event', event_col='Event death', show_progress=show_progress, step_size=0.1)

    if True:
        cph.print_summary()

        fig, ax = plt.subplots(figsize=(5, 25))
        p = cph.plot(hazard_ratios=hazard_ratios)
    else:
        p = None

    if return_scaler_imputer:
        return cph, p, imputer, scaler
    else:
        return cph, p

def cox_plot_pvalue_only(cph, pvalue, hazard_ratios=False):
    hazard_ratios_sum = cph.summary
    hazard_ratios_sum.sort_values('coef', ascending=False, inplace=True)
    sel_corr = hazard_ratios_sum[hazard_ratios_sum['p'] <= pvalue]
    cphplt = cph.plot(sel_corr.index.to_list(), hazard_ratios=hazard_ratios)
    return cphplt

if __name__ == '__main__':
    config = configparser.ConfigParser()
    config.read('../user_settings.ini')
    path_creds = config['CastorCredentials']['local_private_path']

    # %% STEP 1: GET THE DATA USING MAARTEN'S FUNCTIONS
    # For more info: please check covid19_ICU_util.py:select_x_y()
    goal = ['survival', 'mortality_all']

    # Add all 'Field Variable Name' from data_struct to
    # INCLUDE variables from analysis
    #  NOTE: See get_feature_set.py for preset selections
    variables_to_include = {
        'Form Collection Name': [],  # groups
        'Form Name':            [],  # variable subgroups
        'Field Variable Name': get_5_premorbid_clin_rep_lab_rad() # single variables
    }

    # Add all 'Field Variable Name' from data_struct to
    # EXCLUDE variables from analysis, because <10 yes or no answers are present.
    variables_to_exclude = ['Cardiac_consultation','Meningitis_Encephalitis_1_1','CAR_MH_AF','susp_pe','FH_Cor','CP_oedema','Seizure_1_1','MH_cardiacdevice','ear_pain','MH_SVT_AF_1','CAR_T0_PALP','Inhaled_Nitric_Oxide_1','MH_SVT_AFL','Outcome_7d_hosp','Endocarditis_Myocarditis_Pericarditis_1_1','MH_SVT_AF','Adenovirus','VAP','pregyn_rptestcd','carmed_ami','CAR_CM_heartfailure','CP_ortho','vasc_lipidlowering','discharge_AKI','MH_CABG','carmed_cipro','microbiology_worker','discharge_ards','post_partum','MH_HF','CAR_PM','cvrisk_famhist','dx_his_acq_LQT_1','CAR_MH_aLQT','MH_ACS','discharge_CT','vasc_creat','same_id','Med_New_QT_Prol','carmed_arb_chronic','CP_sync','Adm_CT','MH_SVT_AFL_1','CRF_QT','echo_FU','MH_PCI','CAR_T0_PE','ECG_adm','aids_hiv','discharge_swap','CP_PE','MH_ischemia_det','vasc_antifxa_measured','ECG_adm_ST','carmed_cita','corona_ieorres','Pregnancy']
    variables_to_exclude += ['Coronavirus']  # duplicate of PCR information
    data, data_struct = load_data(path_creds)
    data, data_struct = preprocess(data, data_struct)
    # %%
    features, outcomes, data, hospital, record_id, days_until_death = prepare_for_learning(data, data_struct,
                                                                          variables_to_include,
                                                                          variables_to_exclude,
                                                                          goal,
                                                                          remove_records_threshold_above=1.0,
                                                                          remove_features_threshold_above=0.5,
                                                                          pcr_corona_confirmed_only=False)
    del outcomes  # not relevant in this form. Use features and hospital.

    # %% STEP 2: CALCULATE and CORRECT ALL TIMINGS.
    times = data['Days until death'].copy()  # TIME THAT PEOPLE DIED
    events = data['Dood - totaal'].copy()  # patient that died. TRUE FALSE

    # now add the people that are ALIVE DISCHARGED. At 21 days.
    times.loc[data['Levend ontslagen en niet heropgenomen - totaal']] = 21.  # or 365   #  discharged alive - ALL 3
    events.loc[data['Levend ontslagen en niet heropgenomen - totaal']] = False  # censor at t=21 days

    times.loc[data['Levend dag 21 maar nog in het ziekenhuis - totaal']] = 21.
    events.loc[data['Levend dag 21 maar nog in het ziekenhuis - totaal']] = False

    # now add the people that are UNKNOWN at 21 days.
    times.loc[data['Onbekend (alle patiënten zonder outcome)']] = data['days_since_admission_current_hosp'][data['Onbekend (alle patiënten zonder outcome)']]  # Onbekend (alle patiënten zonder outcome) == UNKNOWN outcome
    events.loc[data['Onbekend (alle patiënten zonder outcome)']] = False  # censor at time of event.

    # now mark the people that died after 21 days as alive at 21 days.
    times.loc[data['Days until death'] > 21.] = 21.
    events.loc[data['Days until death'] > 21.] = False  # censor at time of event.

    # fix remaining issues with events
    sel = np.logical_and(times.isna(),data['Outcome_cat_4'] == 1) # transfer
    events[sel] = np.nan
    sel = np.logical_and(times.isna(),data['Outcome_cat_8'] == 1) # death
    events[sel] = False  # no information on timing present, hence state as alive at t=0
    times[sel] = 0.
    sel = np.logical_and(times.isna(),data['Outcome_cat_10'] == 1) # discharged readmitted
    events[sel] = np.nan

    # remaining data: does not have any outcome or follow-up: we only now these patients are alive at t=0.
    times[times.isna()] = 0.0
    times[events.isna()] = np.nan

    features = pd.concat([features,
                          pd.Series(name='Time to event',
                                    data=times),
                          pd.Series(name='Event death',
                                    data=events),
                         pd.Series(name='hospital',
                                    data=hospital)], axis=1)

    # pd.Series(name='Record Id',data=record_id)[[d not in ['VieCuri'] for d in data['hospital']]]

    # drop hospitals without enough data
    # data.drop(index=features.index[[d in ['VieCuri'] for d in features['hospital']]], inplace=True)
    # features.drop(index=features.index[[d in ['VieCuri'] for d in features['hospital']]], inplace=True)

    # now drop the people that have no information regarding durations (very new records).
    # now drop n/a's in time to event; these are incomplete records that cannot be used.
    data.drop(index=features.index[features['Time to event'].isna()], inplace=True)
    features.drop(index=features.index[features['Time to event'].isna()], inplace=True)

    # translate feature columns
    features.rename(columns=englishvar_to_dutchnames, inplace=True)

    # do not model data with > 50% missing data
    drop_vars = features.columns[np.sum(features.isnull())/len(features) > 0.5]
    features = features.drop(columns=drop_vars)

    # %% STEP 3: SHOW KM CURVE
    # simple_estimates(features)  # events: True = death, False = censor, None = ?
    kmf =  KM_age_groups(features)

    # %% step 5: valdiate concordance with LOHO
    imputation_method = 'iterative'
    hazard_ratios = False
    cindex, testsetn = cox_ph_loho(features, hazard_ratios=hazard_ratios,
                         imputation_method=imputation_method,
                         minimal_n=25)
    print('concordances (mean: {}) = {}'.format(round(np.mean(cindex),3), [round(c,3) for c in cindex]))

    # %% STEP 4: COX REGRESSION.
    # USE HERE: features, hospital, times, events
    cph, plot = cox_ph(features, hazard_ratios=True,
                       imputation_method=imputation_method)
    plot.figure.savefig('Cox_coef_all.png', format='png', dpi=300, figsize=(3,23), pad_inches=0, bbox_inches='tight')

    # %%
    cphplt = cox_plot_pvalue_only(cph, 0.05, hazard_ratios=True)

    # run on command line:
    cphplt.figure.set_figheight(6)
    cphplt.figure.savefig('Cox_coef_pvalue_.05.png', format='png', dpi=300, figsize=(7,9), pad_inches=0, bbox_inches='tight')


import numpy as np
from types import SimpleNamespace
from uuid import uuid4

from gefest.core.structure.structure import Structure
from gefest.tools.estimators.estimator import Estimator
from gefest.core.geometry.geometry_2d import Geometry2D
from gefest.core.structure.polygon import Polygon
from gefest.core.structure.point import Point

def create_internal() -> 'Structure':
    X0 = [204.7, 206.9, 149.6, 59, 47.3, 60.2, 139.8, 191, 186, 157.9]
    Y0 = [225.2, 237.5, 264, 228, 200.2, 174.7, 106.4, 145, 155, 142.0]

    r = 80
    arc_angles = np.linspace(0 * np.pi, np.pi / 2, 10)
    X1 = list(r * np.cos(arc_angles) + 78)
    Y1 = list(r * np.sin(arc_angles) + 142)

    X = np.array(X0 + X1 + [X0[0]]) / 2 + 50
    Y = np.array(Y0 + Y1 + [Y0[0]]) / 2 + 150

    struct = Polygon(polygon_id=str(uuid4()),
                     points=[(Point(x, y)) for x, y in zip(X, Y)])

    return struct

true = create_internal()

def configurate_estimator(domain):
    # ------------
    # User-defined estimator
    # it should be created as object with .estimate() method
    # ------------

    def multi_loss(struct: Structure):
        if len(struct.polygons) != 1:
            return 1000

        poly_pred = Geometry2D()._poly_to_geom(struct.polygons[0])
        poly_true = Geometry2D()._poly_to_geom(true)

        d1 = poly_pred.difference(poly_true).length
        d2 = poly_true.difference(poly_pred).length

        if d1 < d2:
            return d2
        else:
            return d1

    estimator = SimpleNamespace()
    estimator.estimate = multi_loss

    # ------------
    # GEFEST estimator
    # ------------

    estimator = Estimator(estimator=estimator)

    return estimator


# -*- coding: utf-8 -*-
"""
Example 1 for linear regression
"""

from __future__ import absolute_import, division, print_function

import tensorflow as tf
from tensorflow import keras

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from tensorflow.python import debug as tf_debug

#################################################################

# Display training progress by printing a single dot for each completed epoch
class PrintDot(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs):
    if epoch % 100 == 0: print('')
    print('.', end='')

def plot_history(history):
  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Abs Error [1000$]')
  plt.plot(history.epoch, np.array(history.history['mean_absolute_error']),
           label='Train Loss')
  plt.plot(history.epoch, np.array(history.history['val_mean_absolute_error']),
           label = 'Val loss')
  plt.legend()
#  #plt.ylim([0, 5])

input_file = './car_data.csv'
#data_from_file = pd.read_csv(input_file)

data = np.loadtxt(open(input_file, "rb"), dtype='float', delimiter=',', skiprows=1, usecols=(0,3))

train_data = data[0:288, 1:2]
test_data = data[288:,1:2]

train_labels = data[0:288,0:1]
test_labels = data[288:,0:1]

#
#plt.figure(num='1')
#plt.plot(train_data, train_labels, 'rx')
#plt.xlabel('Horsepower (hp)')
#plt.ylabel('Miles per Galon')
#
##Normalize
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)
train_data_norm = (train_data - mean) / std
test_data_norm = (test_data - mean) / std

#https://learningtensorflow.com/lesson7/
#https://www.guru99.com/linear-regression-tensorflow.html
#https://blog.altoros.com/using-linear-regression-in-tensorflow.html

#Getting the dataset
points=100
input_d=np.linspace(-3,3,points)
np.random.seed(6)
output=np.sin(input_d)+np.random.uniform(-0.5,0.5,points)

#plt.figure(num='2')
#p=plt.plot(input_d,output,'ro')
#plt.axis([-4,4,-2.0,2.0])
#plt.show()

epochs = 1000

x = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
tf.set_random_seed(5)
w = tf.Variable(tf.random_normal([1]), name="weights")
b = tf.Variable(tf.random_normal([1]), name='bias')

Y_Pred = tf.add(tf.multiply(x, w), b) # same as ϴ1*X + ϴ0

error=tf.square(Y_Pred-Y)
#we just reduced error by dividing the no of input_d values (Good practice)
f_error=tf.reduce_sum(error)/(points-1)

optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(f_error)

init = tf.global_variables_initializer()
sess = tf.Session()
#sess = tf.InteractiveSession()
#sess = tf_debug.LocalCLIDebugWrapperSession(sess)
sess.run(init)

# Fit all training data

#loss_expected=0
#for epoch in range(epochs):
#
#    for (x_point,y_point) in zip(input_d,output):
#        sess.run(optimizer,{x:x_point,Y:y_point})
#
#    loss_per_epoch = sess.run(f_error,{x:input_d,Y:output})
#    # for testing we give the same training set as the test set
#
#    if epoch % 10 == 0:
#        plt.axis([-4,4,-2.0,2.0])
#        plt.plot(input_d,Y_Pred.eval(feed_dict={x: input_d}, session=sess),'b', alpha=epoch / epochs)
#
#
#    # Allow the training to quit if we've reached a minimum
#    if np.abs(loss_expected - loss_per_epoch) < 0.000001:
#        break
#    loss_expected = loss_per_epoch

for _ in range(1000):
    sess.run(optimizer, {x: input_d, Y: output})

print(sess.run(w))
print(sess.run(b))

sess.close()

#plt.figure(num='3')
#plt.scatter(output, Y_Pred)
#plt.xlabel('True Values [1000$]')
#plt.ylabel('Predictions [1000$]')
#plt.axis('equal')
#plt.xlim(plt.xlim())
#plt.ylim(plt.ylim())
#_ = plt.plot([-100, 100], [-100, 100])

#plt.show()


'''
* @File: a3_无重复字符的最长字串.py
* @Author: CSY
* @Date: 2019/7/27 - 17:10
'''
"""
给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
"""
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        l = []
        res = []
        for x in s:
            if x not in l:
                l.append(x)
            else:
                res.append(len(l))
                i = l.index(x)
                l = l[i + 1:]
                l.append(x)
        res.append(len(l))
        return max(res) if res else 0
if __name__ == "__main__":
    # str="abcabcbb"
    # str="bbbbb"
    str="pwwkew"
    s1 = Solution()
    print(s1.lengthOfLongestSubstring(str))

a3_无重复字符的最长字串.py

a3_无重复字符的最长字串.py