import pytest
import pathlib

from click.testing import CliRunner
from flexmock import flexmock
from ibata.CategoriesSummary import CategoriesSummary
from ibata.Category import Category
from ibata.Transaction import Transaction

@pytest.fixture
def runner():
    return CliRunner()

@pytest.fixture
def config_dir():
    return pathlib.Path(__file__).parent / "fixtures"

@pytest.fixture
def config_file(config_dir):
    return config_dir / "ibata_ok.cfg"

@pytest.fixture
def config_file_object(config_file):
    return flexmock(name=config_file)

@pytest.fixture
def transaction():
    trans = Transaction("2020-01-05", None, None, -443.25, "CZK", "Alza.cz", "Platba kartou", "John Doe",
                        "Sluchátka v alze", None)
    trans.category = "electronics"
    return trans

@pytest.fixture
def transactions():
    trans1 = Transaction("2020-01-05", None, None, -443.25, "CZK", "Alza.cz", "Platba kartou", "John Doe",
                         "Sluchátka v alze", None)
    trans2 = Transaction("2020-01-15", None, None, -583.25, "CZK", "Obi.cz", "Platba kartou", "John Doe",
                         "Regál v obi", None)
    trans3 = Transaction("2020-01-25", None, None, -1583.25, "CZK", "LIDL DEKUJE ZA NAKUP", "Platba kartou", "John Doe",
                         "Jídlo", None)
    trans4 = Transaction("2020-01-25", "91245123", "0300", 58583.25, "CZK", "Oracle", "Platba převodem", "John Doe",
                         "Výplata", None)
    trans5 = Transaction("2020-01-25", "91245123", "0300", -500, "CZK", "Díky za pomoc", "Platba převodem", "John Doe",
                         "Pomoc od Johnyho", None)
    return [trans1, trans2, trans3, trans4, trans5]

@pytest.fixture
def category(transaction):
    return Category("electronics", "Elektronika", [transaction])

@pytest.fixture
def income_category():
    trans = Transaction("2020-01-05", None, None, 24443.25, "CZK", "Alza.cz", "Platba převodem", "John Doe",
                        "Výplata", None)
    trans.category = "income"
    return Category("income", "Příjem", [trans])

@pytest.fixture
def categories_summary(category):
    trans2 = Transaction("2020-01-20", "99512343", "0300", -1443.25, "CZK", "Obi", "Platba převodem", "John Doe",
                         "Regál do garáže", None)
    trans2.category = "hobby"
    categories = [
        Category("hobby", "Hobby", [trans2]),
        category
    ]
    return CategoriesSummary(categories, "2020-01-01", "2020-02-01")


import streamlit as st
import yfinance as yf
import pandas as pd
import cufflinks as cf
import datetime

from fbprophet import Prophet
from fbprophet.plot import plot_plotly
from plotly import graph_objs as go

hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            .main{
        background-color:#F5F5F5;
    }
            footer {

	        visibility: hidden;

	}
        footer:after {
            content:'© 2021 Aman Rath, All Rights Reserved';
            visibility: visible;
            display: block;
            position: relative;
            #background-color: red;
            padding: 5px;
            top: 2px;
}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

# App title
st.markdown('''
# Stock Forecast App
Shown are the stock price data for User selected company!''')

# Sidebar
st.sidebar.subheader('Stock Specification')
start_date = st.sidebar.date_input("Start date", datetime.date(2019, 1, 1))
end_date = datetime.date.today().strftime("%Y-%m-%d")
n_years = st.sidebar.slider('Years of prediction:', 1, 4)
period = n_years * 365

@st.cache
def load_data(ticker):
    data = yf.download(ticker, start_date, end_date)
    data.reset_index(inplace=True)
    return data

# Retrieving tickers data
ticker_list = pd.read_csv('https://raw.githubusercontent.com/dataprofessor/s-and-p-500-companies/master/data/constituents_symbols.txt')
tickerSymbol = st.sidebar.selectbox('Select Stock Ticker', ticker_list) # Select ticker symbol
tickerData = yf.Ticker(tickerSymbol) # Get ticker data
tickerDf = tickerData.history(period='1d', start=start_date, end=end_date) #get the historical prices for this ticker
data = load_data(tickerSymbol)

# Ticker information
string_logo = '<img src=%s>' % tickerData.info['logo_url']
st.markdown(string_logo, unsafe_allow_html=True)

string_name = tickerData.info['longName']
st.header('**%s**' % string_name)

string_summary = tickerData.info['longBusinessSummary']
st.info(string_summary)

# Ticker data
st.header('**Ticker Raw Data**')
st.write(tickerDf.tail(30))

# Plot raw data
st.header('**Company History from Start date**')
def plot_raw_data():
	fig = go.Figure()
	fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
	fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
	fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)
	st.plotly_chart(fig)

plot_raw_data()

# Predict forecast with Prophet.
df_train = data[['Date','Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})

m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods=period)
forecast = m.predict(future)

# Show and plot forecast
st.header('**Forecast data**')
st.write(forecast.tail())

st.header(f'**Forecast plot for {n_years} years**')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)

st.header("**Forecast components**")
fig2 = m.plot_components(forecast)
st.write(fig2)

st.write('---')
st.write('Company Info and Details (For Analysis)')
st.write(tickerData.info)

st.write('---')
# App title
st.markdown('''
**Credits**
- App built by [Aman Rath](https://rathaman1711.github.io/Myportfolio/). Check my portfolio for other projects.
- Built in `Python` using `fbprophet`,`streamlit`,`yfinance`, `cufflinks`, `pandas` and `datetime`
''')


#!/usr/bin/env python3
import os
import glob
import base64
import pynvim

class GossipComment:
    def __init__(self, path):
        self.path = path
        with open(path) as f:
            self.text = f.read()

        filename = os.path.basename(path)
        b64ref, _ = filename.split('.gossip.md',maxsplit=1)
        uri = base64.b64decode(b64ref).decode('utf-8')
        self.permalink = GitPermalink.from_permalink(uri)

class GitPermalink:
    @staticmethod
    def from_permalink(permalink: str):
        remote, rest = permalink.split('/tree/', maxsplit=1)
        commit, rest = rest.split('/', maxsplit=1)

        if '#' not in rest:
            path = rest
            return GitPermalink(remote, path, commit)

        path, rest = rest.split('#', maxsplit=1)

        if not rest.startswith('L'):
            return GitPermalink(remote, path, commit)

        rest = rest.replace('L', '')
        start = None
        if '-' not in rest:
            start = int(rest)
            return GitPermalink(remote, path, commit, start=start)

        start, end = rest.split('-')
        start, end = int(start), int(end)

        return GitPermalink(remote, path, commit, start=start, end=end)

    def __init__(self, remote: str, path: str, commit: str, start=None, end=None):
        self.remote = remote
        self.path = path
        self.commit = commit
        self.start = start
        self.end = end

    def serialize(self):
        permalink = f"{self.remote}/tree/{self.commit}/{self.path}"

        if not self.start:
            return permalink

        permalink += f"#L{self.start}"
        if not self.end or self.start == self.end:
            return permalink

        permalink += f"-L{self.end}"
        return permalink

class GitRepo:
    def __init__(self, root):
        self.root = os.path.abspath(root)

    def get_remote(self, remote='origin'):
        return self._git(f"remote get-url {remote}")

    def get_file_last_commit(self, path):
        return self._git(f"log -1 --format=format:%H {path}")

    def get_file_commits(self, path):
        return self._git(f"log --format=format:%H {path}").split('\n')

    def get_head_commit(self):
        return self._git(f"rev-parse HEAD")

    def to_relative_path(self, abs_path):
        abs_path = os.path.abspath(abs_path)
        return abs_path[len(self.root + '/'):]

    def is_dirty(self, path):
        out = self._git(f'diff {path}')
        return out != ''

    def _git(self, args):
        with os.popen(f"git -C '{self.root}' {args}") as p:
            return p.read().strip()

    @staticmethod
    def from_path(path='.'):
        d = os.path.dirname(path)
        with os.popen(f"git -C '{d}' rev-parse --show-toplevel") as p:
            root = p.read().strip()
            return GitRepo(root)

def get_git_commentlink(fullpath: str, start, end) -> str:
    repo = GitRepo.from_path(fullpath)

    remote = repo.get_remote()
    if 'github.com' not in remote:
        raise Exception("Not a GitHub repository")

    commit = repo.get_file_last_commit(fullpath)

    path = repo.to_relative_path(fullpath)

    return GitPermalink(remote, path, commit, start=start, end=end).serialize()

def get_git_permalink(fullpath: str, start=None, end=None) -> str:
    repo = GitRepo.from_path(fullpath)

    remote = repo.get_remote()
    if 'github.com' not in remote:
        raise Exception("Not a GitHub repository")

    commit = repo.get_head_commit()

    path = repo.to_relative_path(fullpath)

    return GitPermalink(remote, path, commit, start=start, end=end).serialize()

class HLManager:
    def __init__(self, nvim):
        self.nvim = nvim
        self.ns_id = nvim.api.create_namespace('gossip')
        self.marks = {}
        self.extmarks = {}
        self.hl_id = f"CommentSign"
        self.nvim.command(f"highlight {self.hl_id} guibg=#333333")
    def mark_lines(self, start, end):
        bnr = self.nvim.current.buffer
        if bnr not in self.marks:
            self.marks[bnr] = {}
        for lineno in range(start, end+1):
            sign_id = f"617{lineno}"
            self.nvim.command(f"sign define {sign_id} linehl={self.hl_id}")
            self.marks[bnr][sign_id] = self.nvim.command(f'sign place {sign_id} line={lineno} name={sign_id}') or True
    def mark_comment(self, comment):
        bnr = self.nvim.current.buffer
        if bnr not in self.extmarks:
            self.extmarks[bnr] = {}

        self.mark_lines(comment.permalink.start, comment.permalink.end)
        extmark_id = self.nvim.api.buf_set_extmark(bnr, self.ns_id, comment.permalink.start-1, 0, dict(
            virt_lines=[
                [['-'*self.nvim.current.window.width+'\n', self.hl_id]],
                *[[[line, self.hl_id]] for line in comment.text.split('\n')],
                [['-'*self.nvim.current.window.width+'\n', self.hl_id]],
            ],
            virt_lines_above=True,
            line_hl_group=self.hl_id,
       ))
        self.extmarks[bnr][extmark_id] = True

    def unmark(self):
        for bnr in self.marks.keys():
            for lineno in self.marks[bnr].keys():
                self.nvim.command(f"sign unplace {lineno}")
        self.marks = {}
        for bnr in self.extmarks.keys():
            for mark_id in self.extmarks[bnr].keys():
                self.nvim.api.buf_del_extmark(bnr, self.ns_id, mark_id)
        self.extmarks = {}

@pynvim.plugin
class TestPlugin(object):

    def __init__(self, nvim):
        self.nvim = nvim
        self.highlight_manager = HLManager(nvim)
        self.comment_buffer = None
        self.comment_window = None
        self.comments = {}

    @pynvim.command("Permalink", nargs="*", range="", sync=False)
    def permalink(self, *args, **kwargs):
        fullpath = self.nvim.funcs.expand('%:p')
        bnr = self.nvim.current.buffer
        start = self.nvim.api.buf_get_mark(bnr, "<")
        end = self.nvim.api.buf_get_mark(bnr, ">")
        permalink = get_git_permalink(fullpath, start=start[0], end=end[0])
        self.nvim.feedkeys(f'gv')
        self.nvim.command(f'echom "{permalink}"')

    @pynvim.command("Comment", nargs="*", range="", sync=False)
    def comment(self, *args, **kwargs):
        fullpath = self.nvim.funcs.expand('%:p')
        bnr = self.nvim.current.buffer

        repo = GitRepo.from_path(fullpath)
        # check if file is dirty
        if repo.is_dirty(fullpath):
            self.nvim.command(f'echom "unable to add comments to dirty file(yet)"')
            return

        start = self.nvim.api.buf_get_mark(bnr, "<")[0]
        end = self.nvim.api.buf_get_mark(bnr, ">")[0]
        permalink = get_git_commentlink(fullpath, start, end)
        self.nvim.command(f'echom "{permalink}"')
        self.highlight_manager.mark_lines(start, end)
        # TODO open buffer to read(or create if not exists) comment

        repo = GitRepo.from_path(fullpath)
        root_path = os.path.join(repo.root, '.git', 'gossip')

        try: os.mkdir(root_path)
        except FileExistsError:...

        comment_filename = base64.b64encode(bytes(permalink, 'utf-8')).decode('utf-8')
        comment_filename += '.gossip.md'

        comment_path = os.path.join(root_path, comment_filename)
        self.nvim.command(f'split {comment_path}')

    @pynvim.autocmd("BufDelete", pattern="*.gossip.md")
    def clear_comment_hl(self, *args, **kwargs):
        self.highlight_manager.unmark()

    def get_comments(self, fullpath):
        # TODO probably break this out
        repo = GitRepo.from_path(fullpath)
        gossip_glob = os.path.join(repo.root, '.git', 'gossip', '*.gossip.md')
        commits = set(repo.get_file_commits(fullpath))
        fpath = repo.to_relative_path(fullpath)
        comments = []
        for path in glob.glob(gossip_glob):
            filename = os.path.basename(path)
            b64ref, _ = filename.split('.gossip.md',maxsplit=1)
            uri = base64.b64decode(b64ref).decode('utf-8')
            permalink = GitPermalink.from_permalink(uri)
            if permalink.path != fpath:
                continue
            if permalink.commit not in commits:
                continue
            comments.append(GossipComment(path))
        return comments

    @pynvim.command("CommentsOn", nargs="*", range="", sync=False)
    def show_comments(self, *args, **kwargs):
        fullpath = self.nvim.funcs.expand('%:p')
        comments = self.get_comments(fullpath)
        self.comments[fullpath] = comments
        for comment in comments:
            self.highlight_manager.mark_comment(comment)

    @pynvim.command("CommentsOff", nargs="*", range="", sync=False)
    def hide_comments(self, *args, **kwargs):
        self.highlight_manager.unmark()


import functools
import inspect
import logging
import os

import torch
import torch.nn as nn
from torch.nn import init

from .helpers import build_model_with_cfg
from .registry import register_model

__all__ = ['UnetGenerator']

def _cfg(url='', **kwargs):
    return {
        'url': url,
        # 'interpolation': 'trilinear',
        'num_classes': 1000,
        **kwargs
    }

default_cfgs = {
    "cyclegan_2d\tlung_ct": _cfg(
        url='https://github.com/songphilips/ptimz/releases/download/v0.0.1-hrnet/cyclegan_net_G_A.pth.tar',
        paper='https://doi.org/10.3348/kjr.2020.0988',
        input_details='CT',
        slice_thickness=1,
        first_conv='model.model.0',
        num_classes=1, input_size=(1, 512, 512),
        last_layer='model.model.3',
    ),
}

def __patch_instance_norm_state_dict(state_dict, module, keys, i=0):
    """Fix InstanceNorm checkpoints incompatibility (prior to 0.4)"""
    key = keys[i]
    if i + 1 == len(keys):  # at the end, pointing to a parameter/buffer
        if module.__class__.__name__.startswith('InstanceNorm') and \
                (key == 'running_mean' or key == 'running_var'):
            if getattr(module, key) is None:
                state_dict.pop('.'.join(keys))
        if module.__class__.__name__.startswith('InstanceNorm') and \
                (key == 'num_batches_tracked'):
            state_dict.pop('.'.join(keys))
    else:
        __patch_instance_norm_state_dict(state_dict, getattr(module, key), keys, i + 1)

def load_networks(net, save_dir, epoch='latest'):
    """Load all the networks from the disk.

    Parameters:
        epoch (int) -- current epoch; used in the file name '%s_net_%s.pth' % (epoch, name)
    """
    name = 'G_A'
    load_filename = 'cyclegan_net_%s.pth' % name
    load_path = os.path.join(save_dir, load_filename)
    if isinstance(net, torch.nn.DataParallel):
        net = net.module
    print('loading the model from %s' % load_path)
    # if you are using PyTorch newer than 0.4 (e.g., built from
    # GitHub source), you can remove str() on self.device
    state_dict = torch.load(load_path, map_location=str('cpu'))
    if hasattr(state_dict, '_metadata'):
        del state_dict._metadata

    # patch InstanceNorm checkpoints prior to 0.4
    for key in list(state_dict.keys()):  # need to copy keys here because we mutate in loop
        __patch_instance_norm_state_dict(state_dict, net, key.split('.'))
    net.load_state_dict(state_dict)

def get_norm_layer(norm_type='instance'):
    """Return a normalization layer

    Parameters:
        norm_type (str) -- the name of the normalization layer: batch | instance | none

    For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev).
    For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics.
    """
    if norm_type == 'batch':
        norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True)
    elif norm_type == 'instance':
        norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
    elif norm_type == 'none':
        norm_layer = lambda x: Identity()
    else:
        raise NotImplementedError('normalization layer [%s] is not found' % norm_type)
    return norm_layer

class UnetSkipConnectionBlock(nn.Module):
    """Defines the Unet submodule with skip connection.
        X -------------------identity----------------------
        |-- downsampling -- |submodule| -- upsampling --|
    """

    def __init__(self, outer_nc, inner_nc, input_nc=None,
                 submodule=None, outermost=False, innermost=False, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet submodule with skip connections.

        Parameters:
            outer_nc (int) -- the number of filters in the outer conv layer
            inner_nc (int) -- the number of filters in the inner conv layer
            input_nc (int) -- the number of channels in input images/features
            submodule (UnetSkipConnectionBlock) -- previously defined submodules
            outermost (bool)    -- if this module is the outermost module
            innermost (bool)    -- if this module is the innermost module
            norm_layer          -- normalization layer
            user_dropout (bool) -- if use dropout layers.ConvTranspose2d
        """
        super(UnetSkipConnectionBlock, self).__init__()
        self.outermost = outermost
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm2d
        else:
            use_bias = norm_layer == nn.InstanceNorm2d
        if input_nc is None:
            input_nc = outer_nc
        downconv = nn.Conv2d(input_nc, inner_nc, kernel_size=4,
                             stride=2, padding=1, bias=use_bias)
        downrelu = nn.LeakyReLU(0.2, True)
        downnorm = norm_layer(inner_nc)
        uprelu = nn.ReLU(True)
        upnorm = norm_layer(outer_nc)

        if outermost:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1)
            down = [downconv]
            up = [uprelu, upconv, nn.Tanh()]
            model = down + [submodule] + up
        elif innermost:
            upconv = nn.ConvTranspose2d(inner_nc, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv]
            up = [uprelu, upconv, upnorm]
            model = down + up
        else:
            upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
                                        kernel_size=4, stride=2,
                                        padding=1, bias=use_bias)
            down = [downrelu, downconv, downnorm]
            up = [uprelu, upconv, upnorm]

            if use_dropout:
                model = down + [submodule] + up + [nn.Dropout(0.5)]
            else:
                model = down + [submodule] + up

        self.model = nn.Sequential(*model)

    def forward(self, x):
        if self.outermost:
            return self.model(x)
        else:  # add skip connections
            return torch.cat([x, self.model(x)], 1)

class ResnetGenerator(nn.Module):
    """Resnet-based generator that consists of Resnet blocks between a few downsampling/upsampling operations.

    We adapt Torch code and idea from Justin Johnson's neural style transfer project(https://github.com/jcjohnson/fast-neural-style)
    """

    def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6,
                 padding_type='reflect'):
        """Construct a Resnet-based generator

        Parameters:
            input_nc (int)      -- the number of channels in input images
            output_nc (int)     -- the number of channels in output images
            ngf (int)           -- the number of filters in the last conv layer
            norm_layer          -- normalization layer
            use_dropout (bool)  -- if use dropout layers
            n_blocks (int)      -- the number of ResNet blocks
            padding_type (str)  -- the name of padding layer in conv layers: reflect | replicate | zero
        """
        assert (n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        if type(norm_layer) == functools.partial:
            use_bias = norm_layer.func == nn.InstanceNorm2d
        else:
            use_bias = norm_layer == nn.InstanceNorm2d

        model = [nn.ReflectionPad2d(3),
                 nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
                 norm_layer(ngf),
                 nn.ReLU(True)]

        n_downsampling = 2
        for i in range(n_downsampling):  # add downsampling layers
            mult = 2 ** i
            model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias),
                      norm_layer(ngf * mult * 2),
                      nn.ReLU(True)]

        mult = 2 ** n_downsampling
        for i in range(n_blocks):  # add ResNet blocks

            model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout,
                                  use_bias=use_bias)]

        for i in range(n_downsampling):  # add upsampling layers
            mult = 2 ** (n_downsampling - i)
            model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
                                         kernel_size=3, stride=2,
                                         padding=1, output_padding=1,
                                         bias=use_bias),
                      norm_layer(int(ngf * mult / 2)),
                      nn.ReLU(True)]
        model += [nn.ReflectionPad2d(3)]
        model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
        model += [nn.Tanh()]

        self.model = nn.Sequential(*model)

    def forward(self, input):
        """Standard forward"""
        return self.model(input)

class UnetGenerator(nn.Module):
    """Create a Unet-based generator"""

    def __init__(self, in_chans, num_classes, num_downs, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False):
        """Construct a Unet generator
        Parameters:
            input_nc (int)  -- the number of channels in input images
            output_nc (int) -- the number of channels in output images
            num_downs (int) -- the number of downsamplings in UNet. For example, # if |num_downs| == 7,
                                image of size 128x128 will become of size 1x1 # at the bottleneck
            ngf (int)       -- the number of filters in the last conv layer
            norm_layer      -- normalization layer

        We construct the U-Net from the innermost layer to the outermost layer.
        It is a recursive process.
        """
        super(UnetGenerator, self).__init__()
        # construct unet structure
        unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=None, norm_layer=norm_layer,
                                             innermost=True)  # add the innermost layer
        for i in range(num_downs - 5):  # add intermediate layers with ngf * 8 filters
            unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=unet_block,
                                                 norm_layer=norm_layer, use_dropout=use_dropout)
        # gradually reduce the number of filters from ngf * 8 to ngf
        unet_block = UnetSkipConnectionBlock(ngf * 4, ngf * 8, input_nc=None, submodule=unet_block,
                                             norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf * 2, ngf * 4, input_nc=None, submodule=unet_block,
                                             norm_layer=norm_layer)
        unet_block = UnetSkipConnectionBlock(ngf, ngf * 2, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
        self.model = UnetSkipConnectionBlock(num_classes, ngf, input_nc=in_chans, submodule=unet_block, outermost=True,
                                             norm_layer=norm_layer)  # add the outermost layer

    def forward(self, input):
        """Standard forward"""
        return self.model(input)

def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
    """Initialize a network: 1. register CPU/GPU device (with multi-GPU support); 2. initialize the network weights
    Parameters:
        net (network)      -- the network to be initialized
        init_type (str)    -- the name of an initialization method: normal | xavier | kaiming | orthogonal
        gain (float)       -- scaling factor for normal, xavier and orthogonal.
        gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2

    Return an initialized network.
    """
    if len(gpu_ids) > 0:
        assert (torch.cuda.is_available())
        net.to(gpu_ids[0])
        net = torch.nn.DataParallel(net, gpu_ids)  # multi-GPUs
    init_weights(net, init_type, init_gain=init_gain)
    return net

def init_weights(net, init_type='normal', init_gain=0.02):
    """Initialize network weights.

    Parameters:
        net (network)   -- network to be initialized
        init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
        init_gain (float)    -- scaling factor for normal, xavier and orthogonal.

    We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
    work better for some applications. Feel free to try yourself.
    """

    def init_func(m):  # define the initialization function
        classname = m.__class__.__name__
        if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
            if init_type == 'normal':
                init.normal_(m.weight.data, 0.0, init_gain)
            elif init_type == 'xavier':
                init.xavier_normal_(m.weight.data, gain=init_gain)
            elif init_type == 'kaiming':
                init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
            elif init_type == 'orthogonal':
                init.orthogonal_(m.weight.data, gain=init_gain)
            else:
                raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
            if hasattr(m, 'bias') and m.bias is not None:
                init.constant_(m.bias.data, 0.0)
        elif classname.find(
                'BatchNorm2d') != -1:  # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
            init.normal_(m.weight.data, 1.0, init_gain)
            init.constant_(m.bias.data, 0.0)

    print('initialize network with %s' % init_type)
    net.apply(init_func)  # apply the initialization function <init_func>

def _build_cycleGAN_G(pretrained_name, ngf=64,
                      norm='instance', use_dropout=False, init_type='normal',
                      init_gain=0.02, in_chans=None, num_classes=None, **kwargs):
    net = None
    norm_layer = get_norm_layer(norm_type=norm)
    # if netG == 'resnet_9blocks':
    #     net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9)
    # elif netG == 'resnet_6blocks':
    #     net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=6)
    # elif netG == 'unet_128':
    #     net = UnetGenerator(input_nc, output_nc, 7, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
    # elif netG == 'unet_256':
    #     net = UnetGenerator(input_nc, output_nc, 8, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
    # else:
    #     raise NotImplementedError('Generator model name [%s] is not recognized' % netG)
    # return init_net(net, init_type, init_gain, gpu_ids)
    pretrained = False if pretrained_name is False or pretrained_name is None else True

    # if not specify the in_chans or num_classes, this will load the default from default_cfg
    if pretrained and pretrained_name in default_cfgs.keys():
        if in_chans is None:
            input_size = default_cfgs[pretrained_name].get('input_size', None)
            if input_size is not None:
                in_chans = input_size[0]
                print(f'Load default in_chans for {pretrained_name} as {in_chans}')
            else:
                raise ValueError(f"Please specify in_chans for {pretrained_name}")

        if num_classes is None:
            num_classes = default_cfgs[pretrained_name].get('num_classes', None)
            if num_classes is None:
                raise ValueError(f"Please specify num_classes for {pretrained_name}")
            else:
                print(f'Load default num_classes for {pretrained_name} as {num_classes}')

    return build_model_with_cfg(model_cls=UnetGenerator, variant='', pretrained=pretrained,
                                default_cfg=default_cfgs.get(pretrained_name, None), pretrained_strict=False,
                                # model config
                                in_chans=in_chans,
                                num_classes=num_classes,
                                num_downs=kwargs.get('num_downs', 8))

@register_model
def cyclegan_2d(pretrained='cyclegan_2d\tlung_ct', **kwargs):
    func_name = str(inspect.stack()[0][3])
    if pretrained is True:
        pretrained = f'{func_name}\tlung_ct'
    elif isinstance(pretrained, str):
        pretrained = func_name + '\t' + pretrained
        if pretrained not in default_cfgs.keys():
            _logger = logging.getLogger(__name__)
            _logger.warning(f"No {pretrained} pretrained weights, use random initialization.")
            pretrained = False
    model = _build_cycleGAN_G(pretrained, num_downs=8, **kwargs)
    return model


import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split

tf.logging.set_verbosity(tf.logging.ERROR)

def create_csv():
	file = open('offline-train.txt','r')

	with open('data.csv','w') as fcsv:
		fcsv.write("Metric,Timestamp,Label,SR,Rate,GR,Load\n")
		for f in file:
			l= f.split(',')

			m = l[0].split(":")[1].split('"')[1]
			#metric.append(m)

			t =l[1].split(":")[1]
			#timestamp.append(t)

			la =l[2].split(":")[1]
			#label.append(la)

			s = l[3].split(":")[1]
			#sr.append(s)

			r = l[4].split(":")[1]
			#rate.append(r)

			g = l[5].split(":")[1]
			#gr.append(g)

			lo = l[6].split(":")[1].split('"')[1]
			#load.append(lo)
			fcsv.write(m+","+t+","+la+","+s+","+r+","+g+","+lo+"\n")

#create_csv()
gear_data = pd.read_csv('data.csv',index_col=None)
print(gear_data.head())

cols_to_norm = ['SR', 'GR','Load']

gear_data[cols_to_norm] = gear_data[cols_to_norm].apply(lambda x: (x - x.min()) / (x.max() - x.min()))

sr = tf.feature_column.numeric_column('SR')
rate = tf.feature_column.numeric_column('Rate')
gr = tf.feature_column.numeric_column('GR')
load = tf.feature_column.numeric_column('Load')

print(gear_data.head())

feat_cols = [sr,gr,load]

x_data = gear_data.drop(['Label','Metric','Timestamp','Rate'],axis=1)

labels = gear_data['Label']

X_train, X_test, y_train, y_test = train_test_split(x_data,labels,test_size=0.3, random_state=101)

print(len(X_train), 'train examples')
print(len(X_test), 'test examples')

inputFunction = tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train,batch_size=200,num_epochs=1000,shuffle=True)

dnnClassifierModel = tf.estimator.DNNClassifier(hidden_units=[1024, 512, 256],
                                                feature_columns=feat_cols,
                                                n_classes=3,
                                                activation_fn=tf.nn.relu,
                                                optimizer=lambda: tf.train.AdamOptimizer(
                                                    learning_rate=tf.train.exponential_decay(learning_rate=0.001,
                                                    global_step=tf.train.get_global_step(),
                                                    decay_steps=1000,
                                                    decay_rate=0.96)))

dnnClassifierModel.train(input_fn=inputFunction,steps=1000)

evaluateInputFunction = tf.estimator.inputs.pandas_input_fn(
      x=X_test,
      y=y_test,
      batch_size=10,
      num_epochs=1,
      shuffle=False)
results = dnnClassifierModel.evaluate(evaluateInputFunction)

print(results)


import json
import subprocess
import sys
from dataclasses import dataclass
from functools import cached_property

# Gufo Thor modules
from .log import logger

@dataclass
class DockerConfig(object):
    """
    Docker daemon configuration.

    Attributes:
        logging_driver: Current logging driver.
        server_version: Current docker server version.
        has_compose_plugin: Compose plugin is installed.
    """

    logging_driver: str
    server_version: str
    has_compose_plugin: bool

class Docker(object):
    """Docker wrapper."""

    @cached_property
    def _config(self: "Docker") -> DockerConfig:
        """
        Docker configuration.

        Returns:
            DockerConfig.
        """
        if "pytest" in sys.modules:
            # Testing stub
            return DockerConfig(
                logging_driver="json-file",
                server_version="24.0.6",
                has_compose_plugin=True,
            )
        return self._read_config()

    def _read_config(self: "Docker") -> DockerConfig:
        """
        Read configuration from docker daemon.

        Returns:
            Parsed config.
        """
        logger.warning("Reading docker config")
        r = subprocess.check_output(
            ["docker", "info", "--format", "{{ json . }}"]
        )
        data = json.loads(r)
        # Check plugins
        client_plugins = data["ClientInfo"]["Plugins"]
        has_compose = any(
            True for d in client_plugins if d["Name"] == "compose"
        )
        #
        cfg = DockerConfig(
            logging_driver=data["LoggingDriver"],
            server_version=data["ServerVersion"],
            has_compose_plugin=has_compose,
        )
        logger.warning("Docker %s found", cfg.server_version)
        return cfg

    @cached_property
    def logging_driver(self: "Docker") -> str:
        """
        Get docker logging driver.

        Returns:
            logging driver name.
        """
        return self._config.logging_driver

    def _commpose_command(self: "Docker", *args: str) -> bool:
        """
        Run compose subcommand.

        Returns:
            True: if command executed successfully.
            False: otherwise.
        """
        if self._config.has_compose_plugin:
            cmd = ["docker", "compose"]
        else:
            cmd = ["docker-compose"]
        cmd.extend(args)
        try:
            subprocess.check_call(cmd)
            return True
        except subprocess.CalledProcessError:
            return False

    def up(self: "Docker") -> bool:
        """
        Perform docker compose up -d.

        Returns:
            True: if command executed successfully.
            False: otherwise.
        """
        logger.warning("Starting containers")
        return self._commpose_command("up", "-d")

    def stop(self: "Docker") -> bool:
        """
        Perform docker compose stop.

        Returns:
            True: if command executed successfully.
            False: otherwise.
        """
        logger.warning("Stopping containers")
        return self._commpose_command("stop")

    def shell(self: "Docker") -> bool:
        """
        Run shell in container.

        Returns:
            True: if command executed successfully.
            False: otherwise.
        """
        logger.warning("Running shell")
        return self._commpose_command("run", "--rm", "shell")

docker = Docker()


import sys
import os
import socket
import logging
from typing import Union, Any
from pathlib import Path
from ast import literal_eval
from configparser import ConfigParser, NoSectionError

from node import __version__

log = logging.getLogger()

class NodeConfig(ConfigParser):
    _instances = {}

    def __init__(self):
        super().__init__(allow_no_value=True)

        root_path = Path(os.path.realpath(__file__)).parent
        self.config_dir = Path(root_path).parent.joinpath('config')
        self.chia_config_file = self.config_dir.joinpath('node.ini')

        self.hostname = socket.gethostname()
        self.__server = None
        self.__port = None
        self.__socket_dir = None
        self.auth_hash = None
        self.chia_blockchain_path = None
        self.logging = {}
        self.key_convert_map = {
            'server': str,
            'socketdir': str,
            'chia_blockchain_path': Path,
            'log_path': Path,
            'authhash': str,
            'log_level': lambda level : logging._nameToLevel[level]  if level.isalpha() else int(level)
        }
        self._check_log_and_config_path()

        self.load_config()

    @classmethod
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(NodeConfig, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

    def load_config(self):
        log.debug(f"self.chia_config_file: {self.chia_config_file}")

        self.read(self.chia_config_file)
        connection_info = self['Connection']

        self.__server = format(connection_info["server"])
        self.__port = format(connection_info["port"])
        self.__socket_dir = format(connection_info["socketdir"])

        if self.has_option('Node', 'authhash'):
            self.auth_hash = format(self["Node"]["authhash"])

        if self.has_option('Chia','chia_blockchain_path'):
            self.chia_blockchain_path = format(self["Chia"]["chia_blockchain_path"])

        # log config
        self.logging = self['Logging']

    def get(self, section: str, option: str, *, raw: bool = False, vars=None, fallback: Any = None) -> Union[Any, None]:

        try:
            d = self._unify_values(section, vars)
        except NoSectionError:
            return fallback

        option = self.optionxform(option)
        try:
            value = d[option]
        except KeyError:
            return fallback

        if raw or value is None:
            return value
        else:
            value = self._interpolation.before_get(self, section, option, value,d)

            if self.key_convert_map.get(option):
                return self.key_convert_map[option](value)

            return literal_eval(value)

    def get_script_info(self):
        return __version__

    def get_connection(self):
        return f"wss://{self.__server}:{self.__port}{self.__socket_dir}"

    def get_chia_path(self):
        return self["Chia"]["chia_blockchain_path"]

    def update_config(self, section, key, value):
        log.debug("Write new config..")
        section = section.capitalize()
        # create section if not already exist
        if not self.has_section(section):
            self[section] = {}

        self[section][key] = value
        with open(self.chia_config_file, "w") as conf:
            print(conf)
            self.write(conf)

        log.debug("Write new config.. Done!")
        log.debug("Reload config..")
        self.load_config()
        log.debug("Reload config.. Done!")

    def _check_log_and_config_path(self):

        if not self.config_dir.exists():
            log.info(f"Config folder does not exists. Create: {self.config_dir}")
            os.makedirs(self.config_dir)

        if not self.chia_config_file.exists():
            log.info("Config file does not exists!")
            log.info("Please configure your settings in node/node.ini \n" \
                     "The sample file can be found at: node/example.node.ini\n" \
                     "Exiting..")
            sys.exit(0)


import time
from pathlib import Path
import sys
import os

from src.aligners.barcode_aligner import BarcodeAligner
from src.aligners.library_matcher import OligoMatcher
# from src.analyzers.deletion_analyzer import DeletionAnalyzer
from src.analyzers.deletion_position_per_base_analyzer import DeletionPositionPerBaseAnalyzer
from src.analyzers.general_analyzer import GeneralAnalyzer
from src.analyzers.reads_length_analyzer import ReadLengthAnalyzer
from src.analyzers.variant_distriution_analyzer import VariantDistributionAnalyzer
from src.library_design import library_design_loader
from src.library_design.library_design import LibraryDesign
from src.library_reads import library_loader
from src.library_reads.library_reads import LibraryReads
from src.library_reads.preprocessor import FixPreProcessor
from src.utils.pdf_generator import PDFGenerator
from utils.config import basedir
# Plotting setup
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns

sns.set()
sns.set_palette('muted')
plt.rc("axes.spines", top=False, right=False)  # Remove top and right borders. Looks nicer.
mpl.rcParams['savefig.dpi'] = 300  # Save plot will be in a higher resolution.

# TODO move file names to another file.
class DatasetFiles(object):
    ZOHAR = "Twist_zohar"
    PART = "Twist_combined_first100k.fastq"

    SCHIRMAN_1 = "Nucleotide"
    SCHIRMAN_2 = "r_loops"
    SCHIRMAN_3 = "splicing"

    TALI = "tali"
    TALI_FOLDER = "/data/tali_data/"

    TOY = "toy_data"
    TOY_FOLDER = "data/toy_data/"
BASE_DIR = os.getcwd()+os.sep
if os.environ.get('mode') == 'prod':
    BASE_DIR = '/home/omersabary/DNA-storage-web/'
DATASET_FOLDER = DatasetFiles.TOY_FOLDER
DATASET_FILE = DatasetFiles.TOY

DATASET_READS_SUFFIX = "_reads.fastq"
DATASET_DESIGN_SUFFIX = "_design.csv"
DATASET_CONFIG_SUFFIX = "_config.json"

# A const to decide whether to use existing files or re calculate. [Matching and Alignment].
OVERRIDE = True
EDIT_DISTANCE = False
# An int to allow a smaller chunk of reads from the fastq file.
NREADS = -1

def get_library_reads_and_design(reads_file, design_file, override=False):
    print("\n| Setting up library design and reads object:")
    library_design_df = library_design_loader.load_oligo_design(design_file)
    library_design = LibraryDesign(library_design_df)
    print(" - Generated the design obj")

    read_file_noprefix = reads_file[:-12]
    aligned_file = Path("{}_after_alignment.csv".format(read_file_noprefix))
    matched_file = Path("{}_after_matching.csv".format(read_file_noprefix))
    config_file = Path(read_file_noprefix + DATASET_CONFIG_SUFFIX)

    sequences = library_loader.load_library(DATASET_FOLDER + DATASET_FILE + DATASET_READS_SUFFIX, True, NREADS)

    if aligned_file.exists() and not override:
        library_reads = LibraryReads(str(aligned_file))
    elif matched_file.exists() and not override:
        library_reads = LibraryReads(str(matched_file))
    else:
        library_reads = LibraryReads(sequences)

    if config_file.exists():
        library_reads.set_config_file(config_file)
        print(" - Config file set")
    else:
        assert 'Config file does not exist'

    # TODO need to find a cleaner way to do this. LibraryReads Save/Load should be upgraded.
    library_reads.set_original_sequences(sequences)

    print(" - Generated the reads obj")
    return library_reads, library_design

def get_library_reads_and_design_simple(design_file, read_metafile, config_file):
    library_design_df = library_design_loader.load_oligo_design(design_file)
    library_design = LibraryDesign(library_design_df)

    reads_file_names = get_filenames_from_file(read_metafile)
    sequences = library_loader.load_library(reads_file_names)

    library_reads = LibraryReads(sequences)
    print('DEBUG: the type of library_reads is' + str(type(library_reads)))
    library_reads.set_config_file(config_file)
    library_reads.set_original_sequences(sequences)

    return library_reads, library_design

def get_filenames_from_file(file_name):
    with open(file_name, 'r') as file:
        files_names = file.read().splitlines()

    return files_names

def preprocess_reads(library_reads):
    print("| preprocessing library reads")
    processor = FixPreProcessor(library_reads.get_config_file())
    library_reads.preprocess_reads(processor)

def match_reads_to_design(library_reads, library_design):
    print("\n| Matching reads to design")
    variant_matcher = OligoMatcher(library_design)

    config = library_reads.get_config_file()
    wanted_keys = ['barcode_start', 'barcode_end']

    aligner = BarcodeAligner(library_design, dict((k, config[k]) for k in wanted_keys if k in config))
    variant_matcher.add_aligner(aligner)

    library_reads.match_sequences(variant_matcher)
    library_reads.save_library_state("{}_after_matching.csv".format(DATASET_FOLDER + DATASET_FILE))

    matched_count = library_reads.get_matched_reads_count()
    un_matched_count = library_reads.get_unmatched_reads_count()
    print(" - Finished matching.")
    print(" -- managed to match - {}".format(matched_count))
    print(" -- % of successful match - {}".format(matched_count / (matched_count + un_matched_count)))

def align_reads_to_variants(library_reads, library_design):
    print("\n| Aligning reads to design")
    library_reads.compute_edit_distance(library_design)

    library_reads.save_library_state("{}_after_alignment.csv".format(DATASET_FOLDER + DATASET_FILE))
    print(" - Finished read alignment")

def analyze_library(library_reads, library_design, time_stamp=""):
    print("\n| Analyzing library")
    contents = []

    analyzers = [GeneralAnalyzer(),
                 ReadLengthAnalyzer(),
                 VariantDistributionAnalyzer()]
    # DeletionPositionPerBaseAnalyzer({'longest_sequence': library_design.get_design_longest_sequence()})]

    for analyzer in analyzers:
        contents.append(analyzer.analyze(library_reads, library_design))
        print(" - Finished with : {}".format(analyzer.name))

    with PDFGenerator("{}_report.pdf".format(time_stamp)) as pdf_gen:
        for content in contents:
            if content:
                pdf_gen.add_content_array(content)

if __name__ == "__main__":
    print("Running main")
    t_start = time.time()

    # Temporary images will be saved in the temp folder, if not temp folder exists we create one.
    if not os.path.isdir("temp"):
        os.makedirs("temp")

    time_stamp = ""
    if len(sys.argv) == 1:  # if no arguments are passed try to retrieve data from CONSTS.
        lr_obj, ld_obj = get_library_reads_and_design(DATASET_FOLDER + DATASET_FILE + DATASET_READS_SUFFIX,
                                                      DATASET_FOLDER + DATASET_FILE + DATASET_DESIGN_SUFFIX,
                                                      OVERRIDE)
    elif len(sys.argv) == 2:  # if we have on argument is the user_id
        DATASET_FOLDER = BASE_DIR+'input_files_dir' + os.sep
        DATASET_FILE = sys.argv[1]
        OVERRIDE = False
        time_stamp = sys.argv[1]
        lr_obj, ld_obj = get_library_reads_and_design(DATASET_FOLDER + DATASET_FILE + DATASET_READS_SUFFIX,
                                                      DATASET_FOLDER + DATASET_FILE + DATASET_DESIGN_SUFFIX,
                                                      OVERRIDE)

    else:  # Otherwise use supplied arguments to retrieve data.
        assert len(sys.argv) > 3, "You must supply 3 arguments : design file path, reads file path, config file path"
        design_file = sys.argv[1]
        reads_file = sys.argv[2]
        config_file = sys.argv[3]
        if len(sys.argv) == 5:
            time_stamp = sys.argv[4]

        lr_obj, ld_obj = get_library_reads_and_design_simple(design_file, reads_file, config_file)

    if not lr_obj.did_matching() or OVERRIDE:
        preprocess_reads(lr_obj)
        match_reads_to_design(lr_obj, ld_obj)

    if EDIT_DISTANCE and (not lr_obj.did_edit_distance() or OVERRIDE):
        align_reads_to_variants(lr_obj, ld_obj)

    # This delay fix a strange error. Probably something with reading from a file we just finished writing to.
    time.sleep(2)

    analyze_library(lr_obj, ld_obj, time_stamp)

    print("\n -(\) - Main test took : {} time.".format(time.time() - t_start))


import cv2
from matplotlib import pyplot as plt

#khandane aks
img = cv2.imread("images/IMG_2933_resize.jpg",0)

#emale filtere gaussian baraye kaheshe noise
gaussian = cv2.GaussianBlur(img, (3,3), 0)

#emale filtere laplacian bar rooye tasvir hamvar sazi shode
LoG = cv2.Laplacian(gaussian, cv2.CV_8UC1, ksize=3)

#namayeshe tasavir
f, subplt = plt.subplots(1,2,figsize=(16,5))
subplt[0].imshow(img, cmap='gray')
subplt[0].set_title("Orginal Img")
subplt[1].imshow(LoG, cmap='gray')
subplt[1].set_title("Edged Img")
plt.show()

#save kardan tasvir labe yabi sjode
Edged_img = cv2.imwrite('images/SilandrEdged2933.jpg', LoG)


from fractions import Fraction

x = Fraction(2, 7) * Fraction(5, 6) * Fraction(1, 5)
y = Fraction(5, 7) * Fraction(2, 6) * Fraction(1, 5)
z = Fraction(5, 7) * Fraction(4, 6) * Fraction(2, 5)

p = x + y + z

print("show probability: ", p)
print(" ------------------------------------ ")


#!/usr/bin/env python3
'''
Created on 20190209
Update on 20190209
@author: Eduardo Pagotto
'''

#pylint: disable=C0301
#pylint: disable=C0103
#pylint: disable=W0703
#pylint: disable=R0913

import logging
import subprocess

def execute_conversao(comando):
    separado = comando.split(' ')
    proc = subprocess.Popen(separado,
                            #shell=True,
                            #preexec_fn=os.setsid,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    proc.wait()

    logging.debug('Comando: %s status: %d', comando, proc.returncode)

    if proc.returncode != 0: # saiu com erro
        msg_erro = ''
        if proc.stderr is not None:
            for line in iter(proc.stderr.readline, b''):
                linhaErro = line.decode('utf-8')
                msg_erro = msg_erro + str(linhaErro) + ' '

        raise Exception('Falha Comando: {0} Erro [{1}]'.format(comando, msg_erro))

    # Saiu de forma correta
    msg_ok = ''
    for line in iter(proc.stdout.readline, b''):
        linhaOk = line.decode('utf-8').replace('\n', '').replace('\r', '').replace('/', '\\')
        logging.debug('%s', linhaOk)
        msg_ok = msg_ok + linhaOk + ' '

    return msg_ok


import torch
import torch.nn as nn

import torch.nn as nn
class Focal2DLoss(nn.Module):
    def __init__(self, alpha=0.9, gamma=2):
        super(Focal2DLoss, self).__init__()
        self.base_criterion = nn.MSELoss()
        self.alpha = alpha
        self.gamma = gamma

    def forward(self, logit, label):
        pos_label = label
        pos_logit = logit * pos_label
        pos_beta = (pos_label.sum().item() - pos_logit.sum().item()) / (pos_label.sum().item() + 1e-5)
        pos_loss = self.alpha * (pos_beta ** self.gamma) * self.base_criterion(pos_logit, pos_label)

        neg_label = 1 - label
        neg_logit = logit * neg_label
        neg_beta = 1 - (neg_label.sum().item() - neg_logit.sum().item()) / (neg_label.sum().item() + 1e-5)
        neg_loss = (1 - self.alpha) * (neg_beta ** self.gamma) * self.base_criterion(neg_logit + label, label)

        return pos_loss + neg_loss

class cpnLoss(nn.Module):
	def __init__(self, num_kpt):
		super(cpnLoss, self).__init__()
		self.criterion1 = nn.MSELoss()
		self.criterion2 = nn.MSELoss(reduction='none')
		self.num_kpt = num_kpt

	def _ohkm(self, loss, top_k):
		ohkm_loss = 0.
		for i in range(loss.size()[0]):
			sub_loss = loss[i]
			topk_val, topk_idx = torch.topk(sub_loss, k=top_k, dim=0, sorted=False)
			tmp_loss = torch.gather(sub_loss, 0, topk_idx)
			ohkm_loss += torch.sum(tmp_loss) / top_k
		ohkm_loss /= loss.size()[0]
		return ohkm_loss

	def _compute_heat_loss(self, logit, target):
		y_logit = logit * target
		n_logit = logit * (1 - target) + target
		y_weight = (logit.size(2) * logit.size(3) - 1) / 2
		n_weight = 1
		loss = self.criterion1(y_logit, target) * y_weight \
			+ self.criterion1(n_logit, target) * n_weight
		return loss

	def _compute_off_loss(self, logit, target, target_heat, reduce=True):
		x_logit = logit[:, :self.num_kpt, :, :] * target_heat
		y_logit = logit[:, self.num_kpt:, :, :] * target_heat
		x_target = target[:, :self.num_kpt, :, :]
		y_target = target[:, self.num_kpt:, :, :]
		if reduce:
			loss = self.criterion1(x_logit, x_target) + \
				self.criterion1(y_logit, y_target)
		else:
			loss = self.criterion2(x_logit, x_target) + \
				self.criterion2(y_logit, y_target)
		return loss

	def forward(self, global_outs, global_off_out,
		refine_heat_out, refine_off_out, target_heat, target_off):
		loss = 0.
		global_loss_record = 0.
		refine_loss_record = 0.
		for global_out in global_outs:
			global_heat_loss = self._compute_heat_loss(global_out, target_heat)
			loss += global_heat_loss
			global_loss_record += global_heat_loss.data.item()
		global_off_loss = self._compute_off_loss(global_off_out, target_off, target_heat)
		loss += global_off_loss
		global_loss_record += global_off_loss.data.item()
		refine_heat_loss = self.criterion2(refine_heat_out, target_heat)
		refine_off_loss = self._compute_off_loss(refine_off_out, target_off, target_heat)
		refine_loss = refine_heat_loss + refine_off_loss
		refine_loss = refine_loss.mean(dim=3).mean(dim=2)
		refine_loss = self._ohkm(refine_loss, self.num_kpt // 2)
		loss += refine_loss
		refine_loss_record += refine_loss.data.item()
		return loss, global_loss_record, refine_loss_record

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import  autograd

class MSELoss(nn.Module):

    def __init__(self, reduction='mean'):
        super(MSELoss, self).__init__()
        self.reduction = reduction

    def forward(self, logit, target):
        batch_size = logit.size(0)
        loss = (logit - target)
        loss = (loss * loss) / 2 / batch_size

        if self.reduction == 'none':
            return loss
        elif self.reduction == 'mean':
            return loss.mean()
        else:
            return loss.sum()

class HRPLoss(nn.Module):

    def __init__(self, num_kpt, s):
        super(HRPLoss, self).__init__()
        self.num_kpt = num_kpt
        self.s = s
        self.mean_criterion = nn.MSELoss()
        self.none_criterion = MSELoss(reduction='none')

    def _generate_mask(self, indexs, cuda):
        mask = torch.zeros(indexs.shape[0], self.num_kpt)
        if cuda:
            mask = mask.cuda()
        for b in range(indexs.shape[0]):
            for p in range(indexs.shape[1]):
                mask[b, indexs[b, p]] = 1
        return mask

    def forward(self, out1_1, out1_2, out2_1, out2_2, targets1, targets2, weights):
        loss1_1 = self.mean_criterion(out1_1, targets1)
        pre_offset_x = out1_2[:, :self.num_kpt, :, :] * targets1
        pre_offset_y = out1_2[:, self.num_kpt:, :, :] * targets1
        lab_offset_x = targets2[:, :self.num_kpt, :, :]
        lab_offset_y = targets2[:, self.num_kpt:, :, :]
        loss1_2 = self.mean_criterion(pre_offset_x, lab_offset_x) + self.mean_criterion(pre_offset_y, lab_offset_y)
        # refine
        loss2_1 = self.none_criterion(out2_1, targets1).sum(dim=2).sum(dim=2)
        _, indexs = torch.topk(loss2_1, self.num_kpt//2, 1)
        mask = self._generate_mask(indexs, targets1.is_cuda)
        loss2_1 = (loss2_1 * mask).mean()
        mask = mask.reshape(*mask.shape, 1, 1)
        pre_offset_x = out2_2[:, :self.num_kpt, :, :] * (targets1 * mask)
        pre_offset_y = out2_2[:, self.num_kpt:, :, :] * (targets1 * mask)
        lab_offset_x = targets2[:, :self.num_kpt, :, :] * (targets1 * mask)
        lab_offset_y = targets2[:, self.num_kpt:, :, :] * (targets1 * mask)
        loss2_2 = self.mean_criterion(pre_offset_x, lab_offset_x) + self.mean_criterion(pre_offset_y, lab_offset_y)
        # total
        loss = (loss1_1 + loss2_1) * weights[0] + (loss1_2 + loss2_2 * 5) * weights[1]
        # loss = loss1_1 * weights[0] + loss1_2 * weights[1]
        return loss


#coding:utf-8
import numpy as np
import os,pathlib
import torch

root = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent

class TextcnnConfig(object):
    def __init__(self):
        self.corpus_path = os.path.join(root,"data","dataset","origin","qa_corpus.xlsx")
        self.answer_map_path = os.path.join(root,"data","dataset","origin","answer_map.pkl")
        self.proc_dir = os.path.join(root,"data","dataset","proc")
        self.class_map = {"chat":0, "busi": 1,}
        self.class_map_reverse = {0:"chat",1:"busi"}
        self.stopwords_path = os.path.join(root,"data","stopwords","哈工大停用词表.txt")
        self.w2v_dir = os.path.join(root,"data","w2v")
        self.save_path = os.path.join(root,"data","textcnn_results","question_cls.h5")
        self.vocab_path = os.path.join(root,"data","w2v","vocab.pickle")
        self.embed_path = os.path.join(root,"data","w2v","embed_matrix.npy")
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        self.vocab_size = 11368
        self.batch_size = 64
        self.max_lengths = 15
        self.pad_idx = 1
        self.embed_dim = 300
        self.filter_sizes = [2,3,4]
        self.num_filters = 64
        self.dense_units = 32
        self.num_classes = 2
        self.dropout = 0.5
        self.learning_rate = 1e-3
        self.num_epochs = 50
        self.max_grad_norm = 2.0
        self.gamma = 0.9
        self.require_improve = 2000


import cv2
from os import listdir
import numpy as np
from datetime import datetime

path = 'C:\\Users\\ravel\\Google Drive\\M2\\Projet\\amicorpus\\IS1000a\\video\\IS1000a.C.avi'

array = [] #On sauvegarde le nombre de visages detecté

def difference(oldFrame,newFrame):

    print("Difference : ")
    diff = cv2.absdiff(oldFrame,newFrame)
    print('diff : ',diff)
#    contours = cv2.findContours(diff,mode=RETR_LIST)
#    cv2.drawContours(contours)
#    cv2.imshow(contours)
    input("Suivant")

def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err

def detect():

    face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml') #On import les face pour la detection
    face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('./cascades/haarcascade_eye.xml')
    camera = cv2.VideoCapture(path)

    camera.set(cv2.CAP_PROP_FPS, 1) #On reduit le nombre de frame a 1 pour faciliter les calculs
    i = 0

    while(True):
        ret,frame = camera.read()
        # print(type(ret),type(frame),ret)
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        #calcul de la difference

        try:
            difference(oldFrame,gray)
            # err = mse(oldFrame,gray)
            # print("erreur : ",err)
            # if err > 200:
            #     cv2.imshow("oldFrame",oldFrame)
            #     cv2.imshow("gray",gray)
            #     input('attente...')
        except NameError:
            pass

        array.append(len(faces))

        for (x, y, w, h) in faces:
            img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0),2)
            roi_gray = gray[y:y + h, x:x + w]
            eyes = eye_cascade.detectMultiScale(roi_gray, 1.03,5, 0, (40, 40))

            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(img, (ex, ey), (ex + ew, ey + eh),(0, 255, 0), 2)

            # cv2.imshow("camera", frame) #cv2.imwrite('./vikings.jpg', img)
            pathRes = './contours/' + str(i) + '.jpg'
            print(pathRes , len(faces))
            cv2.imwrite(pathRes, img)
            i+=1
        # if cv2.waitKey(1000 / 12) & 0xff == ord("q"):
        #     break

        print('Nombres de visages trouvés en moyenne :' , np.mean(array))

        oldFrame = gray

    camera.release()
    cv2.destroyAllWindows()

detect()


from django.urls import path
from . import views
from .views import DeletePostView, AddDislikes,AddLikes, AddCommentDislikes, AddCommentLikes, CommentReplyView, PostDetailView, EditPostView, ProfileView,ProfileEditView,PostNotificationView

urlpatterns = [
    path("", views.starting_page_view, name="home-page"),
    path('add_post/',views.create_post_view, name="add-post"),
    path("posts", views.posts_view, name="share-thoughts"),
    path("posts/search", views.posts_search_view, name="post-search"),
    path("posts/<int:pk>", PostDetailView.as_view(), name="post-detail-page"),
    path("research-agenda/", views.research_agenda_view, name="research-agenda"),
    path("research-community/", views.research_community_view, name="research-community"),
    path("what-if/", views.what_if_view, name="what-if"),
    path("contact-us/", views.contact_us_view, name="contact-us"),
    path("about-us/", views.about_us_view, name="about-us"),
    path("process/", views.about_us_process_view, name="process"),
    path("post/<int:pk>/edit", EditPostView.as_view(), name="post-edit"),
    path("register/", views.register_view, name="register-user"),
    path("login/",views.login_view, name="user-login"),
    path("add_tag/",views.add_tag_view, name="add-tag"),
    path("add_tag/<int:post_pk>/", views.add_tag_from_post_view, name="add-tag-post"),
    path("logout/",views.logout_view, name="user-logout"),
    path("post/<int:post_pk> <int:comment_pk>/delete", views.delete_comment, name="delete-comment"),
    path("post/<int:pk>/delete", DeletePostView.as_view(), name="delete-post"),
    path("post/<int:pk>/likes", AddLikes.as_view(), name="likes"),
    path("post/<int:pk>/dislikes", AddDislikes.as_view(), name="dislikes"),
    path("comment/<int:pk>/like", AddCommentLikes.as_view(), name="comment-likes"),
    path("comment/<int:pk>/dislike", AddCommentDislikes.as_view(), name="comment-dislikes"),
    path("post/<int:post_pk>/comment/<int:pk>/reply",CommentReplyView.as_view(),name="comment-reply"),
    path("profile/<int:pk>", ProfileView.as_view(), name="profile"),
    path("profile/edit/<int:pk>", ProfileEditView.as_view(), name="profile-edit"),
    path("notification/<int:notification_pk>/post<int:post_pk>",PostNotificationView.as_view(),name="post-notification"),
]


# Authors: Eric Larson <larson.eric.d@gmail.com>
#          Sheraz Khan <sheraz@khansheraz.com>
#          Denis Engemann <denis.engemann@gmail.com>
#
# License: BSD (3-clause)

import numpy as np

from ..filter import next_fast_len
from ..source_estimate import _BaseSourceEstimate
from ..utils import verbose, _check_combine, _check_option

@verbose
def envelope_correlation(data, combine='mean', orthogonalize="pairwise",
                         verbose=None):
    """Compute the envelope correlation.

    Parameters
    ----------
    data : array-like, shape=(n_epochs, n_signals, n_times) | generator
        The data from which to compute connectivity.
        The array-like object can also be a list/generator of array,
        each with shape (n_signals, n_times), or a :class:`~mne.SourceEstimate`
        object (and ``stc.data`` will be used). If it's float data,
        the Hilbert transform will be applied; if it's complex data,
        it's assumed the Hilbert has already been applied.
    combine : 'mean' | callable | None
        How to combine correlation estimates across epochs.
        Default is 'mean'. Can be None to return without combining.
        If callable, it must accept one positional input.
        For example::

            combine = lambda data: np.median(data, axis=0)

    orthogonalize : 'pairwise' | False
        Whether to orthogonalize with the pairwise method or not.
        Defaults to 'pairwise'. Note that when False,
        the correlation matrix will not be returned with
        absolute values.

        .. versionadded:: 0.19
    %(verbose)s

    Returns
    -------
    corr : ndarray, shape ([n_epochs, ]n_nodes, n_nodes)
        The pairwise orthogonal envelope correlations.
        This matrix is symmetric. If combine is None, the array
        with have three dimensions, the first of which is ``n_epochs``.

    Notes
    -----
    This function computes the power envelope correlation between
    orthogonalized signals [1]_ [2]_.

    References
    ----------
    .. [1] Hipp JF, Hawellek DJ, Corbetta M, Siegel M, Engel AK (2012)
           Large-scale cortical correlation structure of spontaneous
           oscillatory activity. Nature Neuroscience 15:884–890
    .. [2] Khan S et al. (2018). Maturation trajectories of cortical
           resting-state networks depend on the mediating frequency band.
           Neuroimage 174:57–68
    """
    _check_option('orthogonalize', orthogonalize, (False, 'pairwise'))
    from scipy.signal import hilbert
    n_nodes = None
    if combine is not None:
        fun = _check_combine(combine, valid=('mean',))
    else:  # None
        fun = np.array

    corrs = list()
    # Note: This is embarassingly parallel, but the overhead of sending
    # the data to different workers is roughly the same as the gain of
    # using multiple CPUs. And we require too much GIL for prefer='threading'
    # to help.
    for ei, epoch_data in enumerate(data):
        if isinstance(epoch_data, _BaseSourceEstimate):
            epoch_data = epoch_data.data
        if epoch_data.ndim != 2:
            raise ValueError('Each entry in data must be 2D, got shape %s'
                             % (epoch_data.shape,))
        n_nodes, n_times = epoch_data.shape
        if ei > 0 and n_nodes != corrs[0].shape[0]:
            raise ValueError('n_nodes mismatch between data[0] and data[%d], '
                             'got %s and %s'
                             % (ei, n_nodes, corrs[0].shape[0]))
        # Get the complex envelope (allowing complex inputs allows people
        # to do raw.apply_hilbert if they want)
        if epoch_data.dtype in (np.float32, np.float64):
            n_fft = next_fast_len(n_times)
            epoch_data = hilbert(epoch_data, N=n_fft, axis=-1)[..., :n_times]

        if epoch_data.dtype not in (np.complex64, np.complex128):
            raise ValueError('data.dtype must be float or complex, got %s'
                             % (epoch_data.dtype,))
        data_mag = np.abs(epoch_data)
        data_conj_scaled = epoch_data.conj()
        data_conj_scaled /= data_mag
        # subtract means
        data_mag_nomean = data_mag - np.mean(data_mag, axis=-1, keepdims=True)
        # compute variances using linalg.norm (square, sum, sqrt) since mean=0
        data_mag_std = np.linalg.norm(data_mag_nomean, axis=-1)
        data_mag_std[data_mag_std == 0] = 1
        corr = np.empty((n_nodes, n_nodes))
        for li, label_data in enumerate(epoch_data):
            if orthogonalize is False:  # the new code
                label_data_orth = data_mag
                label_data_orth_std = data_mag_std
            else:
                label_data_orth = (label_data * data_conj_scaled).imag
                label_data_orth -= np.mean(label_data_orth, axis=-1,
                                           keepdims=True)
                label_data_orth_std = np.linalg.norm(label_data_orth, axis=-1)
                label_data_orth_std[label_data_orth_std == 0] = 1
            # correlation is dot product divided by variances
            corr[li] = np.dot(label_data_orth, data_mag_nomean[li])
            corr[li] /= data_mag_std[li]
            corr[li] /= label_data_orth_std
        if orthogonalize is not False:
            # Make it symmetric (it isn't at this point)
            corr = np.abs(corr)
            corr = (corr.T + corr) / 2.
        corrs.append(corr)
        del corr

    corr = fun(corrs)
    return corr


import discord
import os
import gdb
import botio
from dconsts import DefaultConstants as const, DefaultRegexes as regex
from p_action import PlayerAction
from keep_alive import keep_alive

# editar: TALVEZ transformar usar a propria PlayerAction ao inves de uma instancia da classe
bot = discord.Client()
cmd = botio.CommandAnalyzer()
reply = botio.AutoResponder()
guild_member = gdb.GuildMember()
guild = gdb.GuildDB()
action = PlayerAction()

@bot.event
async def on_ready():
  print('We have logged in as {0.user}'.format(bot))
  await bot.change_presence(activity=discord.Game(name='RPG'))

@bot.event
async def on_message(msg):
  # nao responda a si mesmo nem a outros bots
  if msg.author.bot:
    return

  # ----------------------------------------------------------------------------------------------
  # respostas padrao

  # mencionou meu nome? vou contar-lhe quem sou
  elif str(bot.user.id) in msg.content:
    await reply.whoami(msg)

  # aqui vai um proverbio lendario
  elif cmd.match('legen!dary', msg, cmd.ONLY):
    await reply.legendary(msg)

  # quer saber o que eu faco?
  elif cmd.match('legen!help', msg, cmd.AND_TEXT_BODY):
    embedBox = discord.Embed()
    await reply.help(msg, cmd, bot, embedBox)

  # ----------------------------------------------------------------------------------------------
  # operacoes com banco de dados

  # definir ou consultar o canal secreto
  elif cmd.match('legen!sch', msg, cmd.AND_TEXT_BODY):
    if guild_member.is_manager(msg, guild):
      guild.update_sch(msg, cmd)
      await reply.db_sch_query(msg, guild)
    else:
      await reply.sorry(msg)

  # adicionar cargos a funcao de gerencia do canal secreto
  elif cmd.match('legen!mgmt', msg, cmd.AND_TEXT_BODY):
    if msg.author.guild_permissions.administrator:
      guild.update_mgmt_roles(msg, cmd)
      await reply.db_mgmt_query(msg, guild)
    else:
      await reply.sorry(msg)

  # remover algum registro do BD do servidor
  # editar: atualmente gerentes tem permissao para atualizar sch, mas nao deleta-lo, entretanto apenas mudar a condicao nao funcionara, afinal os gerentes nao podem modificar os membros da gerencia
  # editar: legen!del @<role_name> para remover um cargo da gerencia
  elif cmd.match('legen!del', msg, cmd.AND_TEXT_BODY):
    if msg.author.guild_permissions.administrator:
      guild.remove_record(msg, cmd)
      await reply.op_status(msg, guild)
    else:
      await reply.sorry(msg)

  # ----------------------------------------------------------------------------------------------
  # rolar dados

  elif cmd.match(regex.PLAYER_ACTION, msg):
      # await msg.channel.send(':warning: BOT UNDER MAINTENANCE :warning:')
    try:
      actionResult = action.compute(msg.content)
      actionBehavior = actionResult['behavior']

      if actionBehavior == const.HIDDEN_ACTION:
        await msg.delete()
        await reply.action_result(msg, actionResult, guild, bot)

      elif actionBehavior == const.SECRET_ACTION:
        await reply.challenge(msg)
        await reply.action_result(msg, actionResult, guild, bot)

      else:
        await reply.action_result(msg, actionResult)

    except:
      return

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

@bot.event
async def on_guild_remove (guild_server):
  guild.remove_from_db(guild_server)

keep_alive()
bot.run(os.getenv('TOKEN'))


"Functions for cross validation."
import os
from copy import deepcopy
from itertools import combinations
from collections import defaultdict

import json
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from tabulate import tabulate

from radial.batchflow import Dataset, FilesIndex # pylint: disable=import-error
from radial.batchflow.research import Research # pylint: disable=import-error
from radial.batchflow.research.research import Executable # pylint: disable=import-error

from radial.core import RadialBatch # pylint: disable=import-error

def create_datasets(path, batch, cross_val=None):
    """Generated dataset with following conditions: if `cross_val` is None or equal to 1
    the one dataset will be created. Elsewhere you will receive list of train and test datasets.
    And the lenght of this list will be equal to `cross_val`.

    Parameters
    ----------
    path : str
        Path to data
    batch : Batch

    cross_val : None or int
        Number of bins in cross valudation split

    Returns
    -------
        : Dataset or list of Datasets

    Raises
    ------
    ValueError
        if ```path``` is empty or doesn't exist.
    """
    index = FilesIndex(path=path)
    if len(index.indices) == 0:
        raise ValueError('Wrong path to data')
    if cross_val is None or cross_val == 1:
        dset = Dataset(index, batch)
        dset.split()
        return dset

    split_ix = np.array_split(index.indices, cross_val)
    iterations = zip(combinations(split_ix, cross_val-1), list(combinations(split_ix, 1))[::-1])
    dsets = []
    for train, test in iterations:
        dset_train = Dataset(index=index.create_subset(np.concatenate(train)), batch_class=RadialBatch)
        dset_test = Dataset(index=index.create_subset(np.concatenate(test)), batch_class=RadialBatch)
        dsets.append([dset_train, dset_test])
    return dsets

def _update_research(research, pipeline, name, dataset):
    """Load given the data into research.

    Parameters
    ----------
    research : Research

    pipeline : Pipeline

    name : str
        Pipeline name.
    dataset : Dataset

    Returns
    -------
        :  Research
    """
    for i, ppl in enumerate(pipeline):
        executable = Executable()
        old_exec = research.executables[name[i]]
        executable.add_pipeline(ppl<<dataset[i], name=name[i], variables=old_exec.variables,
                                execute=old_exec.execute, dump=old_exec.dump, run=old_exec.to_run,
                                logging=old_exec.logging, **old_exec.kwargs)
        research.executables[name[i]] = executable
    return research

def execute_research_with_cv(train_pipeline, test_pipeline, res, dataset, n_reps,
                             n_iters, cross_val=None, dir_name='research_cv', research_name='research',
                             train_name='train', test_name='test', **kwargs): # pylint: disable=too-many-arguments, too-many-locals
    """Execute research with given parameters.

    Parameters
    ----------
    train_pipeline : Pipeline

    test_pipeline : Pipeline

    res : Research
        Research object with train and test pipelines.
    dataset : Dataset or list of Datasets
        Dataset to train and test pipelines or list of Dataset if cross validation is used.
    n_reps : int
        Number of repetition Research with current parameters.
    n_iters : int
        Number of iterations in one Research run.
    cross_val : None or int
        Number of bins in cross valudation split.
    dir_name : str
        Name of directory with Researches (availble only when cross validation is used).
    research_name : str
        Name of dir with Research results.
    train_name : str
        Name of train pipeline.
    test_name : str
        Name of test pipeline.

    Returns
    -------
        : batchflow Research or list of Research's
    """
    # simple Research run without cross validation
    research = deepcopy(res)
    workers = kwargs.get('workers', 1)
    gpu = kwargs.get('gpu', None)

    if not isinstance(dataset, list):
        research = _update_research(research, [train_pipeline, test_pipeline],\
                                   [train_name, test_name], [dataset.train, dataset.test])
        research.run(n_reps=n_reps, n_iters=n_iters, name=research_name, bar=True,
                     workers=workers, gpu=gpu)
        return research

    # creation directiory to save cross validation Resutls
    research_list = []
    try:
        os.makedirs(dir_name)
    except FileExistsError as _:
        res_list = os.listdir('./')
        names = np.array(res_list)[list(map(lambda a: dir_name in a, res_list))]
        for name in sorted(names, reverse=True):
            if '.' not in name:
                index = 1 if name[-1] == 'v' else int(name[-1])
                dir_name = dir_name + '_%d'%(index+1)
                os.makedirs(dir_name)
                break

    # Cross validation run
    print('number of bins: ', cross_val)
    for i in range(cross_val):
        research = deepcopy(res)
        research = _update_research(research, [train_pipeline, test_pipeline],\
                                   [train_name, test_name], [dataset[i][0], dataset[i][1]])
        research_name_cv = './%s/' % dir_name + research_name + '_cv_%d' % i
        research.run(n_reps=n_reps, n_iters=n_iters, name=research_name_cv, bar=True,
                     workers=workers, gpu=gpu)
        research_list.append(research)
    return research_list

def split_df_by_name(dataframe, parameters, draw_dict=None):
    """Split the dataframe on the parts with different values in `name` column.

    Parameters
    ----------
    dataframe : pd.Dataframe
        Result of Research.
    parameters : list
        Matched parameters from Research.
    draw_dict : dict
        Only current params from dictionary will be drawn.
        Keys : names of columns.
        Values : The values of visualize params.
    Returns
    -------
        : dict
        key : value of name column
        value : resulted dataframe
    """
    all_names = {}
    if draw_dict is not None:
        for key, value in draw_dict.items():
            dataframe = dataframe[dataframe[key].isin(list([value]))]
            if dataframe.empty:
                raise ValueError("Incorrect column name {} or value {}.".format(key, value))
    for names, name_df in dataframe.groupby('name'):
        if len(parameters) != 0:
            new_df = pd.DataFrame()
            for param_names, values in name_df.groupby(parameters):
                values['parameters'] = str(param_names).replace("'", "")
                new_df = new_df.append(values)
        else:
            new_df = name_df.copy()
            new_df['parameters'] = 'None'
        all_names[names] = new_df
    return all_names

def _load_research(research):
    if not isinstance(research, str):
        return research.load_results()
    return Research().load(research).load_results(use_alias=False)

def _get_parameters(research):
    if isinstance(research, str):
        with open(os.path.join(research, 'description', 'alias.json')) as res:
            alias = json.load(res)
        return list(alias.keys())

    param_set = {}
    for params_list in research.grid_config.alias():
        for params in params_list:
            param_set.update(params)
    return list(param_set)

def _prepare_results(research, hue=None, cross_val=False, aggr=False, iter_start=0, draw_dict=None, names=None):# pylint: disable=too-many-arguments
    if cross_val or cross_val in [0, 1]:
        results = []
        if isinstance(research, str):
            research = list(map(lambda res_name: os.path.join(research, res_name), os.listdir(research)))
        parameters = _get_parameters(research[0])
        for i, res in enumerate(research):
            loaded_res = _load_research(res)
            loaded_res[hue] = 0 if aggr else i
            loaded_res = loaded_res[loaded_res['iteration'] >= iter_start]
            results.append(loaded_res)
        results = pd.concat(results)
    else:
        results = _load_research(research)
        parameters = _get_parameters(research)
    all_names = split_df_by_name(results, parameters, draw_dict)
    correct_names = list(all_names.keys())
    if len(set(correct_names) & set(names)) == 0:
        raise ValueError('One or more names in {} are incorrect,'\
                          ' use the names in the following list {}.'.format(names, correct_names))
    grouped = [all_names.get(name) for name in names]
    return grouped

def draw_history(research, names, types_var, cross_val=None, aggr=False,
                 iter_start=0, draw_dict=None): # pylint: disable=too-many-locals,too-many-arguments
    """Draw plot with history of changes of function named `names` with values from
    column 'types_var'.

    Parameters
    ----------
    research : Research

    names : str or list of str
        Names of functions from Research
    types_var : str or list of str
        Names where the function result saved.
    cross_val : None or int
        Number of cross validation bins.
    aggr : bool, optional
        if True, cross validation reuslts will aggregate in one grap.
        Elsewhere for each cross validation will be drawn own line.
    iter_start : int
        All graph will be drawn from `iter_start` iteration.
    draw_dict : dict
        Only current params from dictionary will be drawn.
        Keys : names of columns.
        Values : The values of visualize params.
    """
    hue = 'number_of_cv' if cross_val is not None and cross_val > 1 else None
    grouped = _prepare_results(research, hue, cross_val, aggr, iter_start, draw_dict, names)

    for name, dframe in zip(names, grouped):
        nan_col = dframe.columns[dframe.isna().any()].tolist()
        dtype = types_var[0]
        for dtp in types_var:
            if dtp not in nan_col:
                dtype = dtp
                break
        dframe[dtype] = dframe[dtype].astype(float)
        graph = sns.FacetGrid(dframe, col='parameters', height=5, hue=hue, col_wrap=5)
        graph.fig.suptitle(dtype + '/' + name, fontsize=16, y=1.05)
        graph.map(sns.lineplot, 'iteration', dtype).add_legend()
        plt.show()

def draw_histogram(research, names, type_var, cross_val=False, draw_dict=None):
    """Draw historgram of research results by given `name` of function and `type_var`.

    Parameters
    ----------
    research : Research

    names : str or list of str
        Names of functions from Research.
    type_var : str or list of str
        Names where the function result saved.
    cross_val : None or int
        Number of cross validation bins.
    draw_dict : dict
        Only current params from dictionary will be drawn.
        Keys : names of columns.
        Values : The values of visualize params.
    """
    data = _prepare_results(research, cross_val=cross_val, draw_dict=draw_dict, names=[names])[0]
    plt.figure(figsize=(10, 7))
    for numb, metric_list in data.groupby('parameters')[type_var]:
        sns.distplot(np.mean(list(metric_list), axis=0), label=str(numb))
    plt.xlabel('Error value')
    plt.ylabel('Probability of error')
    plt.title('Distribution of absolute error')
    plt.legend()
    plt.show()

def print_results_cv(research, names, types_var, cross_val=None, draw_dict=None,
                     n_last=100, none=False, sort_by=None, hue='number_of_cv'): # pylint: disable=too-many-locals,too-many-arguments
    """Print table with mean values of 'names' columns from 'n_last' iterations.

    Parameters
    ----------
    research : Research
    names : str or list of str
        Names of functions from Research.
    type_var : str or list of str
        Names where the function result saved.
    cross_val : None or int
        Number of cross validation bins.
    draw_dict : dict
        Only current params from dictionary will be drawn.
        Keys : names of columns.
        Values : The values of visualize params.
    n_last : int
        Number of iterations(from the end) from which mean values will be calculated.
    none : bool
        If True, than none values will be ignored,
        else if any none in column appeared, than mean value of its column will be none.
    sort_by : str
        Name of the column for sort.
    hue : str or None, optional
        Name of different cross validation.

    Returns
    -------
        : list of DataFrame
        Each DataFrame consists results with given name.
    """
    grouped = _prepare_results(research, hue, cross_val, False, 0, draw_dict, names)
    statistics_df = []
    for name, dframe in zip(names, grouped):
        global_stats = []
        nan_col = dframe.columns[dframe.isna().any()].tolist()
        dtype = types_var[0]
        for dtp in types_var:
            if dtp not in nan_col:
                dtype = dtp
                break
        max_iter = dframe['iteration'].max()
        dframe = dframe[dframe['iteration'] >= max_iter - n_last]
        for param, dfm in dframe.groupby('parameters'):
            par_mean = []
            for cv, cv_df in dfm.groupby(hue):
                cv_mean = []
                for i in range(np.max(cv_df['repetition']) + 1):
                    rep_val = cv_df[cv_df['repetition'] == i][dtype]
                    if len(rep_val.shape) == 1 and len(rep_val) < n_last:
                        n_last = len(rep_val)
                    elif len(rep_val) < n_last:
                        rep_val = np.concatenate(rep_val)
                    cv_mean.append(np.nanmean(np.array(rep_val).astype(np.float32))
                                   if none else np.mean(rep_val))
                par_mean.append(np.mean(cv_mean))
            global_stats.append([*par_mean, np.mean(par_mean), np.std(par_mean)])

        columns = []
        for name in names:
            for cv in range(cross_val):
                columns.append(name + "_" + str(cv))
            columns.extend([name + "_mean", name + "_std"])
        printed = pd.DataFrame(data=global_stats, index=dframe["parameters"].unique(),
                               columns=columns)
        statistics_df.append(printed.sort_values(by=sort_by, ascending=False))
    return statistics_df

def print_results_research(df, name, type_var, average_repetitions=False, sort_by=None, ascending=True,
                           n_last=100):
    """ Show results given by research dataframe.
    Parameters
    ----------
    df : DataFrame
        Research's results
    layout : str
        string where each element consists two parts that splited by /. First part is the type
        of calculated value wrote in the "name" column. Second is name of column  with the parameters
        that will be drawn.
    average_repetitions : bool, optional
        If True, then a separate values will be written
        else one mean value will be written.
    sort_by : str or None, optional
        If not None, column's name to sort.
    ascending : bool, None
        Same as in ```pd.sort_value```.
    n_last : int, optional
        The number of iterations at the end of which the averaging takes place.
    Returns
    -------
        : DataFrame
        Research results in DataFrame, where indices is a config parameters and colums is `layout` values
    """
    columns = []
    data = []
    index = []
    ndf = df[df['name'] == name]
    if average_repetitions:
        columns.extend([name + '_mean', name + '_std'])
    else:
        columns.extend([name + '_' + str(i) for i in [*ndf['repetition'].unique(), 'mean', 'std']])
    for config, cdf in ndf.groupby("config"):
        index.append(config)
        cdf = cdf.drop(['config', 'name'], axis=1).dropna(axis=1).astype('float')
        if average_repetitions:
            idf = cdf.groupby('iteration').mean().drop('repetition', axis=1)
            max_iter = idf.index.max()
            idf = idf[idf.index > max_iter - n_last]
            data.append([idf[type_var].mean(), idf[type_var].std()])
        else:
            rep = []
            for _, rdf in cdf.groupby('repetition'):
                rdf = rdf.drop('repetition', axis=1)
                max_iter = rdf['iteration'].max()
                rdf = rdf[rdf['iteration'] > max_iter - n_last]
                rep.append(rdf[type_var].mean())
            data.append([*rep, np.mean(rep), np.std(rep)])

    res_df = pd.DataFrame(data=data, index=index, columns=columns)
    if sort_by:
        res_df.sort_values(by=sort_by, ascending=ascending, inplace=True)
    return res_df


from looker_sdk import models, error
from lmanage.configurator.user_group_configuration.create_role_base import CreateRoleBase
from lmanage.utils.errorhandling import return_error_message
from tqdm import tqdm
from tenacity import retry, wait_random, wait_fixed, stop_after_attempt

class CreateInstanceRoles(CreateRoleBase):
    def __init__(self, roles, sdk, logger):
        self.role_metadata = roles
        self.sdk = sdk
        self.logger = logger

    def create_permission_lookup(self):
        all_permission_sets = self.get_all_permission_sets()
        instance_permission_set_dict = {
            perm.name: perm.id for perm in all_permission_sets}
        return instance_permission_set_dict

    def create_model_lookup(self):
        all_model_sets = self.get_all_model_sets()
        instance_model_set_dict = {
            model.name: model.id for model in all_model_sets}
        return instance_model_set_dict

    def create_allgroup_lookup(self):
        all_groups = self.sdk.all_groups()
        group_dict = {group.name: group.id for group in all_groups}
        return group_dict

    def create_allrole_lookup(self):
        all_roles = self.sdk.all_roles()
        role_dict = {role.name: role.id for role in all_roles}
        return role_dict

    @retry(wait=wait_fixed(3) + wait_random(0, 2), stop=stop_after_attempt(5))
    def update_looker_role(self, role_id: str, body: models.WriteRole):
        role = self.sdk.update_role(role_id=role_id, body=body)
        return role

    def create_instance_roles(self):
        model_lookup = self.create_model_lookup()
        permission_lookup = self.create_permission_lookup()

        role_output = []

        for r_metadata in tqdm(self.role_metadata, desc="User Role Creation", unit=" user roles", colour="#2c8558"):
            role_name = r_metadata.get('name')
            model_set_id = model_lookup.get(
                r_metadata.get('model_set') if r_metadata.get('model_set') == 'All' else r_metadata.get('model_set').lower())
            permission_set_id = permission_lookup.get(
                r_metadata.get('permission_set').lower())
            if role_name != 'Admin':
                body = models.WriteRole(
                    name=role_name,
                    permission_set_id=str(permission_set_id),
                    model_set_id=str(model_set_id)
                )
                try:
                    role = self.sdk.create_role(
                        body=body
                    )

                except error.SDKError as roleerror:
                    err_msg = return_error_message(roleerror)
                    self.logger.debug(
                        'You have hit a warning creating your role \'%s\'; warning = %s', role_name, err_msg)
                    role_id = self.sdk.search_roles(name=role_name)[0].id
                    role = self.update_looker_role(
                        role_id=str(role_id), body=body)
                temp = {}
                temp['role_id'] = role.id
                temp['role_name'] = role_name
                role_output.append(temp)
            else:
                pass
        self.logger.debug(role_output)
        return role_output

    def set_role(self, role_id: str, group_id: list) -> str:
        try:
            self.sdk.set_role_groups(role_id, group_id)
            return self.logger.debug(f'attributing {group_id} permissions on instance')
        except error.SDKError as role_err:
            err_msg = return_error_message(role_err)
            self.logger.debug(
                'You have hit a warning setting your role, warning = %s', err_msg)
            self.logger.debug(role_err)

    def attribute_instance_roles(self, created_role_metadata: list):
        role_lookup = self.create_allrole_lookup()
        group_lookup = self.create_allgroup_lookup()

        for r_metadata in tqdm(self.role_metadata, desc="Instance Role Attribution", unit=" roles", colour="#2c8558"):
            role_name = r_metadata.get('name')
            teams = r_metadata.get('teams')
            role_id = role_lookup.get(role_name)
            group_id_list = []
            for team in teams:
                group_id = group_lookup.get(team)
                group_id_list.append(group_id)
            self.set_role(role_id=role_id, group_id=group_id_list)

    def sync_roles(self):
        all_role_lookup = self.create_allrole_lookup()
        all_role_lookup.pop('Admin')
        yaml_role = [role.get('name') for role in self.role_metadata]

        for role_name in all_role_lookup.keys():
            if role_name not in yaml_role:
                role_id = all_role_lookup.get(role_name)
                self.sdk.delete_role(role_id=role_id)

    def execute(self):
        created_roles = self.create_instance_roles()
        self.attribute_instance_roles(created_role_metadata=created_roles)
        self.sync_roles()


#!/usr/bin/python
# Support for python2
from __future__ import print_function
#Modify system path
import sys
sys.path.append('../pycThermopack/')
# Importing pyThermopack
from pyctp import quantum_cubic
# Importing Numpy (math, arrays, etc...)
import numpy as np
# Importing Matplotlib (plotting)
import matplotlib.pyplot as plt

# Instanciate and init Quantum-cubic object
qPR = quantum_cubic.qcubic()
qPR.init("He,H2,Ne")
qPR.set_tmin(temp=2.0)

# Plot phase envelope
z = np.array([0.01, 0.1, 0.89])
T, P, v = qPR.get_envelope_twophase(1.0e4, z, maximum_pressure=1.5e7,
                                    minimum_temperature=2.0, calc_v=True)
Tc, vc, Pc = qPR.critical(z)
plt.plot(T, P*1.0e-6)
plt.plot([Tc], [Pc*1.0e-6], "ko")
plt.ylabel(r"$P$ (MPa)")
plt.xlabel(r"$T$ (K)")
plt.title("Quantum-cubic phase diagram")
plt.show()
plt.clf()