import logging
logger = logging.getLogger('camelot.view.controls.formview')

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import Qt

import sip

from camelot.view.art import Icon
from camelot.view.model_thread import post
from camelot.view.model_thread import model_function
from camelot.view.controls.view import AbstractView
from camelot.view.controls.statusbar import StatusBar
from camelot.view import register
from camelot.view.action import ActionFactory

class ContextMenuAction(QtGui.QAction):

    default_icon = Icon('tango/16x16/categories/applications-system.png')

    def __init__(self, parent, title, icon = None):
        """
        :param parent: the widget on which the context menu will be placed
        :param title: text displayed in the context menu
        :param icon: camelot.view.art.Icon object
        """
        super(ContextMenuAction, self).__init__(title, parent)
        self.icon = icon
        if self.icon:
            self.setIcon(self.icon.getQIcon())
        else:
            self.setIcon(self.default_icon.getQIcon())

class FormWidget(QtGui.QWidget):
    """A form widget comes inside a form view or inside an embedded manytoone editor"""

    changed_signal = QtCore.pyqtSignal()

    def __init__(self, parent, admin):
        QtGui.QWidget.__init__(self, parent)
        self._admin = admin
        widget_mapper = QtGui.QDataWidgetMapper(self)
        widget_mapper.setObjectName('widget_mapper')
        widget_layout = QtGui.QHBoxLayout()
        widget_layout.setSpacing(0)
        widget_layout.setMargin(0)
        self._index = 0
        self._model = None
        self._form = None
        self._columns = None
        self._delegate = None
        self.setLayout(widget_layout)

    def get_model(self):
        return self._model

    def set_model(self, model):
        self._model = model
        self._model.dataChanged.connect( self._data_changed )
        self._model.layoutChanged.connect( self._layout_changed )
        self._model.item_delegate_changed_signal.connect( self._item_delegate_changed )
        self._model.setObjectName( 'model' )
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.setModel( model )
            register.register( model, widget_mapper )

        def get_columns_and_form():
            return (self._model.getColumns(), self._admin.get_form_display())

        post(get_columns_and_form, self._set_columns_and_form)

    def clear_mapping(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.clearMapping()

    def _data_changed(self, index_from, index_to):
        #@TODO: only revert if this form is in the changed range
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.revert()
        if not sip.isdeleted(self):
            self.changed_signal.emit()

    def _layout_changed(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.revert()
        self.changed_signal.emit()

    @QtCore.pyqtSlot()
    def _item_delegate_changed(self):
        from camelot.view.controls.delegates.delegatemanager import \
            DelegateManager
        self._delegate = self._model.getItemDelegate()
        self._delegate.setObjectName('delegate')
        assert self._delegate != None
        assert isinstance(self._delegate, DelegateManager)
        self._create_widgets()

    def set_index(self, index):
        self._index = index
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.setCurrentIndex(self._index)

    def get_index(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            return widget_mapper.currentIndex()

    def submit(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.submit()

    def to_first(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.toFirst()
            self.changed_signal.emit()

    def to_last(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.toLast()
            self.changed_signal.emit()

    def to_next(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.toNext()
            self.changed_signal.emit()

    def to_previous(self):
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if widget_mapper:
            widget_mapper.toPrevious()
            self.changed_signal.emit()

    def export_ooxml(self):
        from camelot.view.export.word import open_stream_in_word

        def create_ooxml_export(row):
            # print self._columns
            def ooxml_export():
                # TODO insert delegates
                fields = self._admin.get_all_fields_and_attributes()
                delegates = {}
                for field_name, attributes in fields.items():
                    delegates[field_name] = attributes['delegate'](**attributes)
                obj = self._model._get_object(row)
                document = self._form.render_ooxml(obj, delegates)
                open_stream_in_word( document )

            return ooxml_export

        post(create_ooxml_export(self.get_index()))

    @QtCore.pyqtSlot(tuple)
    def _set_columns_and_form(self, columns_and_form ):
        self._columns, self._form = columns_and_form
        self._create_widgets()

    def _create_widgets(self):
        """Create value and label widgets"""
        from camelot.view.controls.field_label import FieldLabel
        from camelot.view.controls.editors.wideeditor import WideEditor
        #
        # Dirty trick to make form views work during unit tests, since unit
        # tests have no event loop running, so the delegate will never be set,
        # so we get it and are sure it will be there if we are running without
        # threads
        #
        if not self._delegate:
            self._delegate = self._model.getItemDelegate()
        #
        # end of dirty trick
        #
        # only if all information is available, we can start building the form
        if not (self._form and self._columns and self._delegate):
            return
        widgets = {}
        widget_mapper = self.findChild(QtGui.QDataWidgetMapper, 'widget_mapper' )
        if not widget_mapper:
            return
        widget_mapper.setItemDelegate(self._delegate)
        option = QtGui.QStyleOptionViewItem()
        # set version to 5 to indicate the widget will appear on a
        # a form view and not on a table view
        option.version = 5

        #
        # this loop can take a while to complete, so processEvents is called
        # regulary
        #
        for i, (field_name, field_attributes ) in enumerate( self._columns):
#            if i%10==0:
#                QtCore.QCoreApplication.processEvents(
#                    QtCore.QEventLoop.ExcludeSocketNotifiers,
#                    100
#                )
            model_index = self._model.index(self._index, i)
            hide_title = False
            if 'hide_title' in field_attributes:
                hide_title = field_attributes['hide_title']
            widget_label = None
            widget_editor = self._delegate.createEditor(
                self,
                option,
                model_index
            )
            widget_editor.setObjectName('%s_editor'%field_name)
            if not hide_title:
                widget_label = FieldLabel(
                    field_name,
                    field_attributes['name'],
                    field_attributes,
                    self._admin
                )
                widget_label.setObjectName('%s_label'%field_name)
                if not isinstance(widget_editor, WideEditor):
                    widget_label.setAlignment(Qt.AlignVCenter | Qt.AlignRight)

            # required fields font is bold
            if ('nullable' in field_attributes) and \
               (not field_attributes['nullable']):
                font = QtGui.QApplication.font()
                font.setBold(True)
                widget_label.setFont(font)

            assert widget_editor != None
            assert isinstance(widget_editor, QtGui.QWidget)

            widget_mapper.addMapping(widget_editor, i)
            widgets[field_name] = (widget_label, widget_editor)

        widget_mapper.setCurrentIndex(self._index)
        self.layout().insertWidget(0, self._form.render(widgets, self))
        #self._widget_layout.setContentsMargins(7, 7, 7, 7)

class FormView(AbstractView):
    """A FormView is the combination of a FormWidget, possible actions and menu
    items

    .. form_widget: The class to be used as a the form widget inside the form
    view"""

    form_widget = FormWidget

    def __init__(self, title, admin, model, index):
        AbstractView.__init__(self)

        layout = QtGui.QVBoxLayout()
        form_and_actions_layout = QtGui.QHBoxLayout()
        form_and_actions_layout.setObjectName('form_and_actions_layout')
        layout.addLayout(form_and_actions_layout)

        self.model = model
        self.admin = admin
        self.title_prefix = title

        form = FormWidget(self, admin)
        form.setObjectName( 'form' )
        form.changed_signal.connect( self.update_title )
        form.set_model(model)
        form.set_index(index)
        form_and_actions_layout.addWidget(form)

        statusbar = StatusBar(self)
        statusbar.setObjectName('statusbar')
        statusbar.setSizeGripEnabled(False)
        layout.addWidget(statusbar)
        layout.setAlignment(statusbar, Qt.AlignBottom)
        self.setLayout(layout)

        self.change_title(title)

        if hasattr(admin, 'form_size') and admin.form_size:
            self.setMinimumSize(admin.form_size[0], admin.form_size[1])

        self.validator = admin.create_validator(model)
        self.validate_before_close = True

        def get_actions():
            return admin.get_form_actions(None)

        post(get_actions, self.setActions)
        self.update_title()
        #
        # Define actions
        #
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        self.addAction( ActionFactory.view_first(self, self.viewFirst) )
        self.addAction( ActionFactory.view_last(self, self.viewLast) )
        self.addAction( ActionFactory.view_next(self, self.viewNext) )
        self.addAction( ActionFactory.view_previous(self, self.viewPrevious) )
        self.addAction( ActionFactory.refresh(self, self.refresh_session) )
        self.addAction( ActionFactory.export_ooxml(self, form.export_ooxml) )

    @QtCore.pyqtSlot()
    def refresh_session(self):
        from elixir import session
        from camelot.core.orm import refresh_session
        refresh_session( session )

    @QtCore.pyqtSlot()
    def refresh(self):
        """Refresh the data in the current view"""
        self.model.refresh()

    def update_title(self):

        def get_title():
            obj = self.getEntity()
            return u'%s %s' % (
                self.title_prefix,
                self.admin.get_verbose_identifier(obj)
            )

        post(get_title, self.change_title)

    def getEntity(self):
        form = self.findChild(QtGui.QWidget, 'form' )
        if form:
            return self.model._get_object(form.get_index())

    @QtCore.pyqtSlot(list)
    def setActions(self, actions):
        form = self.findChild(QtGui.QWidget, 'form' )
        layout = self.findChild(QtGui.QLayout, 'form_and_actions_layout' )
        if actions and form and layout:
            side_panel_layout = QtGui.QVBoxLayout()
            from camelot.view.controls.actionsbox import ActionsBox
            logger.debug('setting Actions for formview')
            actions_widget = ActionsBox(self, self.getEntity)
            actions_widget.setObjectName('actions')
            action_widgets = actions_widget.setActions(actions)
            for action_widget in action_widgets:
                form.changed_signal.connect( action_widget.changed )
                action_widget.changed()
            side_panel_layout.insertWidget(1, actions_widget)
            side_panel_layout.addStretch()
            layout.addLayout(side_panel_layout)

    def viewFirst(self):
        """select model's first row"""
        form = self.findChild(QtGui.QWidget, 'form' )
        if form:
            form.submit()
            form.to_first()

    def viewLast(self):
        """select model's last row"""
        # submit should not happen a second time, since then we don't want
        # the widgets data to be written to the model
        form = self.findChild(QtGui.QWidget, 'form' )
        if form:
            form.submit()
            form.to_last()

    def viewNext(self):
        """select model's next row"""
        # submit should not happen a second time, since then we don't want
        # the widgets data to be written to the model
        form = self.findChild(QtGui.QWidget, 'form' )
        if form:
            form.submit()
            form.to_next()

    def viewPrevious(self):
        """select model's previous row"""
        # submit should not happen a second time, since then we don't want
        # the widgets data to be written to the model
        form = self.findChild(QtGui.QWidget, 'form' )
        if form:
            form.submit()
            form.to_previous()

    @QtCore.pyqtSlot(bool)
    def showMessage(self, valid):
        form = self.findChild(QtGui.QWidget, 'form' )
        if not valid and form:
            reply = self.validator.validityDialog(
                form.get_index(), self
            ).exec_()
            if reply == QtGui.QMessageBox.Discard:
            # clear mapping to prevent data being written again to the model,
            # then we reverted the row
                form.clear_mapping()
                self.model.revertRow(form.get_index())
                self.validate_before_close = False
                self.close()
        else:
            self.validate_before_close = False
            self.close()

    def validateClose(self):
        logger.debug('validate before close : %s' % self.validate_before_close)
        form = self.findChild(QtGui.QWidget, 'form' )
        if self.validate_before_close and form:
            # submit should not happen a second time, since then we don't
            # want the widgets data to be written to the model
            form.submit()

            def validate():
                return self.validator.isValid(form.get_index())

            post(validate, self.showMessage)
            return False

        return True

    def closeEvent(self, event):
        #print 'close event'
        logger.debug('formview closed')
        if self.validateClose():
            event.accept()
        else:
            event.ignore()

    @model_function
    def toHtml(self):
        """generates html of the form"""
        from jinja2 import Environment

        def to_html(d = u''):
            """Jinja 1 filter to convert field values to their default html
            representation
            """

            def wrapped_in_table(env, context, value):
                if isinstance(value, list):
                    return u'

' + \
                           u'

'.join(
                                [unicode(e) for e in value]
                           ) + u'

'
                return unicode(value)

            return wrapped_in_table

        entity = self.getEntity()
        fields = self.admin.get_fields()
        table = [dict( field_attributes = field_attributes,
                      value = getattr(entity, name ))
                      for name, field_attributes in fields]

        context = {
          'title': self.admin.get_verbose_name(),
          'table': table,
        }

        from camelot.view.templates import loader
        env = Environment(loader = loader)
        env.filters['to_html'] = to_html
        tp = env.get_template('form_view.html')

        return tp.render(context)


# -*- encoding:utf-8 -*-
'''
@time: 2019/12/21 8:28 下午
@author: huguimin
@email: 718400742@qq.com
一个doc表示一个样本
'''

import math
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from layers.dynamic_rnn import DynamicLSTM
from layers.attention import Attention

class GraphConvolution(nn.Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
    """
    def __init__(self, in_features, out_features, bias=True):
        super(GraphConvolution, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features))
        if bias:
            self.bias = nn.Parameter(
                torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)

    def forward(self, text, adj):
        hidden = torch.matmul(text, self.weight)
        denom = torch.sum(adj, dim=2, keepdim=True) + 1
        output = torch.matmul(adj, hidden) / denom
        if self.bias is not None:
            return output + self.bias
        else:
            return output

class ECGCN(nn.Module):

    def __init__(self, word_embedding, pos_embedding, opt):
        super(ECGCN, self).__init__()
        self.opt = opt
        self.embed = nn.Embedding.from_pretrained(torch.tensor(word_embedding, dtype=torch.float))
        self.pos_embed = nn.Embedding.from_pretrained(torch.tensor(pos_embedding, dtype=torch.float))
        self.word_lstm = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True)#(32,75,45,200)
        self.clause_encode = Attention(2*opt.hidden_dim, 1, opt.max_sen_len, opt)#(32,75,200)
        # gcn
        # self.gc1 = GraphConvolution(2*opt.hidden_dim, 2*opt.hidden_dim)
        # self.gc2 = GraphConvolution(2*opt.hidden_dim, 2*opt.hidden_dim)
        # self.gc3 = GraphConvolution(2*opt.hidden_dim, 2*opt.hidden_dim)

        #gat
        # self.ga1 = GAT(2*opt.hidden_dim, 2*opt.hidden_dim, self.opt.num_class, self.opt.keep_prob1, self.opt.alpha, self.opt.heads)
        self.fc1 = nn.Linear(2*opt.hidden_dim + self.opt.embedding_dim_pos, 2*opt.hidden_dim)
        self.fc2 = nn.Linear(2*opt.hidden_dim, opt.num_class)
        self.text_embed_dropout = nn.Dropout(opt.keep_prob1)

        self.gates = nn.ModuleList()
        self.gcns = nn.ModuleList()
        for i in range(3):
            self.gcns.append(GraphConvolution(2*opt.hidden_dim, 2*opt.hidden_dim))
            self.gates.append(nn.Linear(2*opt.hidden_dim, 1))

    def position_weight(self, inputs, emotion_id, doc_len):
        """
        :param inputs: [32, 75, 200]
        :param emotion_id: [32,]
        :param doc_len: [32]
        :param pos_embedding: [103, 50]
        :return:[32,75,50]
        """
        batch_size, max_len = inputs.shape[0], inputs.shape[1]
        relative_pos = np.zeros((batch_size, max_len))
        for sample in range(batch_size):
            len = doc_len[sample].item()
            for i in range(len):
                relative_pos[sample][i] = i - emotion_id[sample].item() + 69
        return relative_pos

    def emotion_encode(self, inputs, emotion_id):
        """
        :param inputs: [32, 75, 200]
        :param emotion_id: [32,]
        :param doc_len: [32,]
        :return: [32, 1, 200]
        """
        batch_size, max_len, dim = inputs.shape[0], inputs.shape[1], inputs.shape[2]
        emotion_clause = np.zeros((batch_size, dim))

        for sample in range(batch_size):
            clause = inputs[sample][emotion_id[sample]]
            emotion_clause[sample] = clause.cpu().detach().numpy()
        return torch.FloatTensor(emotion_clause)

    def emotion_weight(self, inputs, emotion_clause):
        """
        :param inputs: [32, 75, 200]
               emotion_clause:[32, 1, 200]
        :return: [32, 75]
        """
        batch, dim = inputs.shape[0], inputs.shape[2]
        emotion_clause = torch.reshape(emotion_clause, [batch, dim, 1])
        alpha = torch.reshape(torch.matmul(inputs, emotion_clause.float()), [-1, self.opt.max_doc_len, 1])
        return alpha

    def mask(self, inputs, emotion_id):
        """
        :param inputs: [32,75,200]
        :param emotion_id: [32,]
        :return: [32, 1, 200]
        """
        batch_size, max_len = inputs.shape[0], inputs.shape[1]
        emotion_idx = emotion_id.cpu().numpy()
        mask = [[] for i in range(batch_size)]
        for i in range(batch_size):
            for j in range(emotion_idx[i]):
                mask[i].append(0)
            for j in range(emotion_idx[i], emotion_id[i] + 1):
                mask[i].append(1)
            for j in range(emotion_idx[i] + 1, max_len):
                mask[i].append(0)
        mask = torch.tensor(mask).unsqueeze(2).float().to(self.opt.device)
        return mask * inputs

    def pack_sen_len(self, sen_len):
        """
        :param sen_len: [32, 75]
        :return:
        """
        batch_size = sen_len.shape[0]
        up_sen_len = np.zeros([batch_size, self.opt.max_doc_len])
        for i, doc in enumerate(sen_len):
            for j, sen in enumerate(doc):
                if sen == 0:
                    up_sen_len[i][j] = 1
                else:
                    up_sen_len[i][j] = sen

        return torch.tensor(up_sen_len)

    def forward(self, inputs):
        x, sen_len, doc_len, doc_id, emotion_id, adj = inputs
        up_sen_len = self.pack_sen_len(sen_len)
        x = torch.reshape(x, [-1, self.opt.max_sen_len])
        x = self.embed(x)
        x = self.text_embed_dropout(x)
        up_sen_len = torch.reshape(up_sen_len, [-1])
        word_encode = self.word_lstm(x, up_sen_len) #(32*75, batch_max_len, 200)
        clause_encode = self.clause_encode(word_encode, sen_len)
        embs = [clause_encode]
        embs += [self.pos_embed(torch.LongTensor(self.position_weight(clause_encode, emotion_id, doc_len)).to(self.opt.device))]

        emotion_encode = self.emotion_encode(clause_encode, emotion_id) ###情感子句的嵌入表示
        ###对每层的GCN都与emotion_encode计算一个score.

        # x = F.relu(self.gc1(clause_encode, adj))
        # x = F.relu(self.gc2(x, adj))
        # x = F.relu(self.gc3(x, adj))
        x = clause_encode
        for i in range(3):
            x = F.relu(self.gcns[i](x, adj))
            weight = F.sigmoid(self.gates[i](emotion_encode))
            weight = weight.unsqueeze(dim=-1)
            x = x * weight

        output = self.fc2(x.float())
        return output

    # def forward(self, inputs, vs=False):
    #     attention = []
    #     x, sen_len, doc_len, doc_id, emotion_id, adj = inputs#(x(32,75, 45)), (32, 75)
    #     up_sen_len = self.pack_sen_len(sen_len)
    #     x = torch.reshape(x, [-1, self.opt.max_sen_len])
    #     x = self.embed(x)
    #     x = self.text_embed_dropout(x)
    #     up_sen_len = torch.reshape(up_sen_len, [-1])
    #     word_encode = self.word_lstm(x, up_sen_len) #(32*75, batch_max_len, 200)
    #     clause_encode = self.clause_encode(word_encode, sen_len)
    #     embs = [clause_encode]
    #     embs += [self.pos_embed(torch.LongTensor(self.position_weight(clause_encode, emotion_id, doc_len)).to(self.opt.device))]
    #     "concat"
    #     clause_encode = torch.cat(embs, dim=2)
    #     clause_encode = torch.reshape(clause_encode, [-1, self.opt.max_doc_len, 2 * self.opt.hidden_dim + self.opt.embedding_dim_pos])
    #     clause_encode = self.fc1(clause_encode)
    #     # 策略1 "emotion clause 与 clause的attention weight"
    #     # emotion_encode = self.emotion_encode(clause_encode, emotion_id)
    #     # batch, dim = clause_encode.shape[0], clause_encode.shape[2]
    #     # emotion_encode = torch.reshape(emotion_encode, [batch, dim , 1])
    #     # alpha = self.emotion_weight(clause_encode, emotion_encode)
    #     #
    #     # ones = torch.ones((batch, self.opt.max_doc_len, 1))
    #     #
    #     # emotion_encode = emotion_encode.expand(-1,-1,self.opt.max_doc_len).transpose(1,2)
    #     # clause_encode = alpha * emotion_encode + (ones-alpha)*clause_encode
    #     x = F.relu(self.gc1(clause_encode, adj))
    #     x = F.relu(self.gc2(x, adj))
    #     # x = F.relu(self.gc3(x, adj))
    #     # output = self.ga1(clause_encode, adj)
    #
    #     batch, dim = clause_encode.shape[0], clause_encode.shape[2]
    #     ones = torch.ones((batch, self.opt.max_doc_len, 1)).to(self.opt.device)
    #     emotion_encode = self.emotion_encode(x, emotion_id).to(self.opt.device)
    #     alpha = self.emotion_weight(clause_encode, emotion_encode)
    #     # # emotion_encode = self.mask(x, emotion_id)
    #     # # alpha_mat = torch.matmul(emotion_encode, clause_encode.transpose(1,2))
    #     # # alpha = F.softmax(alpha_mat.sum(1, keepdim=True), dim=2).transpose(1,2) #(32,1,75)
    #     # # ones = torch.ones((batch, self.opt.max_doc_len, 1))
    #     # emotion_encode = torch.reshape(emotion_encode, [batch, dim, 1])
    #     # emotion_encode = emotion_encode.expand(-1, -1, self.opt.max_doc_len).transpose(1, 2)
    #     # # x = emotion_encode * alpha + (ones-alpha)*clause_encode
    #     emotion_encode = torch.reshape(emotion_encode, [batch, dim, 1])
    #     emotion_encode = emotion_encode.expand(-1, -1, self.opt.max_doc_len).transpose(1, 2)
    #     x = clause_encode * alpha + (ones - alpha) * emotion_encode
    #     x = self.text_embed_dropout(x)
    #     # # x = torch.matmul(alpha, clause_encode).squeeze(1)
    #     #
    #     # # 策略2 以原始的句表示为主，图卷积作为辅助
    #     # #
    #     #
    #     output = self.fc2(x.float())
    #     if vs:
    #         return output, attention
    #     return output


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 28 16:52:49 2021

@author: shabalin

Utils to work with fable and hexrd functions.
"""

import sys, os
import numpy as np
import yaml, subprocess
#import cbftiffmxrdfix

def run_peaksearch(par_file=None):
    """ Wrapper for the ImageD11 peaksearch.py script"""
    with open(par_file) as f:
        pars = yaml.safe_load(f)
    if pars['stem_out'] == None:
        pars['stem_out'] = ''
    first_im = int(pars['first_image'])
    last_im = int(pars['first_image']) + int(pars['nbr_images']) - 1
    ndigits = pars['ndigits']
    path_inp = os.path.join(pars['image_path'],pars['image_stem'])
    path_out = os.path.join(pars['output_dir'], pars['stem_out']+pars['det_code']+'_peaks')
    # construct the command for peaksearch.py
    command = ('peaksearch.py -n {} -F {} -f {:d} -l {:d} -o {} -d {} -p Y --ndigits {:d} -S {:.3f} -T {:.3f} '.format(
        path_inp,pars['filetype'],first_im,last_im,path_out,
        pars['dark_image'],ndigits,pars['omegastep'], pars['startomega']
        ))
    # Adds threshold values to command
    for t in pars['thresholds']:
        command += '-t {:d} '.format(t)
    # Adds keyword args
    if 'kwargs' in pars:
        command += '{} '.format(pars['kwargs'])
    # modify command for lunarc
    if 'lunarc' in pars:
        command = lunarc_path + command
    print('Running peaksearch with the following command:')
    print(command)
    try:
        subprocess.call(command, shell=True)
    except AttributeError as a:
        print('peaksearch.py ended with error. It seems to work nonetheless.', a)

    del pars, first_im, last_im, ndigits, path_inp, path_out, command
    return

def merge_peaks(par_file, config_file):
    # Wrapper for ImageD11 merge_flt.py
    if (par_file is None):
        raise ValueError('Must supply par_file to run_peaksearcher')
    with open(par_file) as f:
        pars = yaml.safe_load(f)
    if pars['stem_out'] == None:
        pars['stem_out'] = ''
    if 'merged_name' in pars:
        file_out = os.path.join(pars['output_dir'],pars['stem_out']+pars['merged_name'])
    else:
        file_out = os.path.join(pars['output_dir'],pars['stem_out']+pars['det_code']+'_peaks_merged.flt')
    inp = os.path.join(pars['output_dir'], pars['stem_out']+pars['det_code']+'_peaks')
    print('Merging flt files matching {}'.format(inp))
    if not config_file:
        config_file = 'junk'
    command = 'merge_flt.py {} {} {} {:d} '.format(config_file,inp,file_out,pars['pixel_tol']) + ('{:d} '*len(pars['thresholds'])).format(*pars['thresholds'])
    # modify command for lunarc
    if 'lunarc' in pars:
        command = lunarc_path + command
    print(command)
    subprocess.call(command, shell=True)

    del pars, file_out, inp, command
    return

def hexrd_to_fable(path_to_hexrd_yml, path_to_fable_par, det=1, mat='Nb'):
    detname = 'detector_{:d}'.format(det)
    if mat=='ruby':
        cell_params = { "a": 4.7608, "b": 4.7608, "c": 12.99568, "alpha": 90.0, "beta": 90.0, "gamma": 120.0, "lattice": 'R'}
    elif mat=='Nb':
        cell_params = { "a": 3.3042, "b": 3.3042, "c": 3.3042, "alpha": 90.0, "beta": 90.0, "gamma": 90.0, "lattice": 'I'}
    elif mat=='CeO2':
        cell_params = { "a": 5.41153, "b": 5.41153, "c": 5.41153, "alpha": 90.0, "beta": 90.0, "gamma": 90.0, "lattice": 'F'}
    elif mat=='Ti':
        cell_params = { "a": 2.9505, "b": 2.9505, "c": 4.6826, "alpha": 90.0, "beta": 90.0, "gamma": 120.0, "lattice": 'P'}
    else:
        print('ERROR! Incorrect material!')
    with open(path_to_hexrd_yml) as f:
        pars = yaml.safe_load(f)
    wavelength = 12.39842/pars['beam']['energy']
    translation = pars['detectors'][detname]['transform']['translation']
    tilt = pars['detectors'][detname]['transform']['tilt']
    frame_size = [pars['detectors'][detname]['pixels']['columns'], pars['detectors'][detname]['pixels']['rows']]
    pix_size = pars['detectors'][detname]['pixels']['size']
    if os.path.exists(path_to_fable_par):
        if input('File %s already exist! Overwrite it? (y/n):' % path_to_fable_par) != 'y':
            print('Aborted!')
            return
        else:
            pass
    else:
        pass
    f = open(path_to_fable_par,'w')
    f.write( 'cell__a {}'.format(cell_params['a']) )
    f.write( '\ncell__b {}'.format(cell_params['b']) )
    f.write( '\ncell__c {}'.format(cell_params['c']) )
    f.write( '\ncell_alpha {}'.format(cell_params['alpha']) )
    f.write( '\ncell_beta {}'.format(cell_params['beta']) )
    f.write( '\ncell_gamma {}'.format(cell_params['gamma']) )
    f.write( '\ncell_lattice_[P,A,B,C,I,F,R] {}'.format(cell_params['lattice']) )
    f.write( '\nchi {}'.format(0.0) )
    f.write( '\ndistance {}'.format((-translation[2]*1000)) )
    f.write( '\nfit_tolerance {}'.format(0.5) )
    f.write( '\nmin_bin_prob {}'.format(1e-05) )
    f.write( '\nno_bins {}'.format(10000) )
    f.write( '\no11 {}'.format(0) )
    f.write( '\no12 {}'.format(-1) )
    f.write( '\no21 {}'.format(1) )
    f.write( '\no22 {}'.format(0) )
    f.write( '\nomegasign {}'.format(1.0) )
    f.write( '\nt_x {}'.format(0) )
    f.write( '\nt_y {}'.format(0) )
    f.write( '\nt_z {}'.format(0) )
    f.write( '\ntilt_x {}'.format(tilt[2]) )
    f.write( '\ntilt_y {}'.format(tilt[1]) ) # -?
    f.write( '\ntilt_z {}'.format(tilt[0]) )
    f.write('\nwavelength {:0.6f}'.format(wavelength) )
    f.write( '\nwedge {}'.format(0.0) )
    f.write( '\nweight_hist_intensities {}'.format(0) )
    f.write( '\ny_center {}'.format((translation[1]/pix_size[1] + frame_size[1]/2)) )
    f.write( '\ny_size {}'.format((pix_size[1]*1000)) )
    f.write( '\nz_center {}'.format((translation[0]/pix_size[0] + frame_size[0]/2)) )
    f.write( '\nz_size {}'.format((pix_size[0]*1000)) )
    f.close()

    del detname, cell_params, pars, wavelength, translation, tilt, frame_size, pix_size
    return

def fable_to_hexrd(path_to_fable_par, path_to_hexrd_yml):
    y_frm_size = 2880
    z_frm_size = 2880
    with open(path_to_fable_par) as f:
        for line in f:
            if ('distance' in line):
                dist = float(line.split()[1])/1000
            elif ('tilt_x' in line):
                tilt_1 = float(line.split()[1])
            elif ('tilt_y' in line):
                tilt_2 = float(line.split()[1])
            elif ('tilt_z' in line):
                tilt_3 = float(line.split()[1])
            elif ('wavelength' in line):
                wavelength = float(line.split()[1])
            elif ('y_center' in line):
                y_cen = float(line.split()[1])
            elif ('y_size' in line):
                y_pix_size = float(line.split()[1])/1000
            elif ('z_center' in line):
                z_cen = float(line.split()[1])
            elif ('z_size' in line):
                z_pix_size = float(line.split()[1])/1000
    f.close()
    pars = {'beam':
            {'energy': 12.39842/wavelength, 'vector': {'azimuth': 90.0, 'polar_angle': 90.0}},
            'detectors':
            {'detector_1':
             {'buffer': None,
              'pixels': {'columns': y_frm_size, 'rows': z_frm_size, 'size': [z_pix_size, y_pix_size]},
              'saturation_level': 14000.0,
              'transform': {'tilt': [tilt_1, tilt_2, tilt_3], 'translation': [(z_cen-z_frm_size/2)*z_pix_size, (y_cen-y_frm_size/2)*y_pix_size, -dist]}}},
             'id': 'instrument',
             'oscillation_stage': {'chi': 0.0, 'translation': [0.0, 0.0, 0.0]}}
    if os.path.exists(path_to_hexrd_yml):
        if input('File %s already exist! Overwrite it? (y/n):' % path_to_hexrd_yml) != 'y':
            print('Aborted!')
            return
        else:
            pass
    else:
        pass
    with open(path_to_hexrd_yml, 'w') as f:
        yaml.dump(pars, f)

    del y_frm_size, z_frm_size, pars, dist, tilt_1, tilt_2, tilt_3, wavelength, y_cen, y_pix_size, z_cen, z_pix_size
    return


# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations
import datetime

#########################################################################
## This is a sample controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
#########################################################################

@auth.requires_login()
def index():
    """
    Main logged in homepage, displays users collection
    """
    #If user doesn't have an Unfiled box, create one
    if (db((db.box.owner_id == auth.user.id) & (db.box.name == 'Unfiled')).count()==0):
        db.box.insert(name='Unfiled',
        is_public='False',
        owner_id=auth.user.id,
        created_on = datetime.datetime.now())
        db.commit
    #Display any necessary message
    if (session.message):
        response.flash = session.message
        session.message = None
    #Find users pubic boxes
    public = db((db.box.owner_id==auth.user.id) & (db.box.is_public == True)).select()
    #Find users private boxes
    private = db((db.box.owner_id==auth.user.id) & (db.box.is_public != True)).select()
    #Find how many comics user has, to offer assistance
    no_of_comics = db(db.comic.owner_id == auth.user.id).count()
    return dict(public_boxes = public, private_boxes = private, no_of_comics = no_of_comics)

@auth.requires_login()
def all():
    comics = db((db.comic.owner_id == auth.user.id) & (auth.user.id == db.auth_user.id)).select(orderby = db.comic.title)
    if len(comics)>0:
        return dict(comics = comics)
    else:
        return dict()

@auth.requires_login()
def search():
    form = FORM(DIV(LABEL('Title:', _for='title', _class="control-label col-sm-3"),
                DIV(INPUT(_class = "form-control string", _name='title', _type="text"), _class="col-sm-3"),
                _class="form-group"),
                DIV(LABEL('Writer:', _for='writer', _class="control-label col-sm-3"),
                DIV(INPUT(_class = "form-control string", _name='writer', _type="text"), _class="col-sm-3"),
                _class="form-group"),
                DIV(LABEL('Artist:', _for='artist', _class="control-label col-sm-3"),
                DIV(INPUT(_class = "form-control string", _name='artist', _type="text"), _class="col-sm-3"),
                _class="form-group"),
                DIV(LABEL('Publisher:', _for='publisher', _class="control-label col-sm-3"),
                DIV(INPUT(_class = "form-control string", _name='publisher', _type="text"), _class="col-sm-3"),
                _class="form-group"),
                DIV(DIV(INPUT(_class = "btn btn-primary", _value='Search', _type="submit"),
                _class="col-sm-9 col-sm-offset-3"),
                _class="form-group"),
                _class="form-horizontal")

    if form.accepts(request, session):
        search_term = ""
        if (len(request.vars.title) > 0):
            title_term = "%" + request.vars.title + "%"
            search_term = (db.comic.title.like(title_term))
        if (len(request.vars.writer) > 0):
            writer_term = "%" + request.vars.writer + "%"
            if (search_term):
                search_term = search_term & (db.comic.writers.like(writer_term))
            else:
                search_term = (db.comic.writers.like(writer_term))
        if (len(request.vars.artist) > 0):
            artist_term = "%" + request.vars.artist + "%"
            if (search_term):
                search_term = search_term & (db.comic.artists.like(artist_term))
            else:
                search_term = (db.comic.artists.like(artist_term))
        if (len(request.vars.publisher) > 0):
            publisher_term = "%" + request.vars.publisher + "%"
            if (search_term):
                search_term = search_term & (db.comic.publisher.like(publisher_term))
            else:
                search_term = (db.comic.publisher.like(publisher_term))
        #Allow for a blank search to return all comics
        #TODO: Disallow for when this search could overload system, i.e. lots of public comics
        constraint = (db.comic_in_box.box_id == db.box.id) & ((db.box.is_public == True) | (db.box.owner_id == auth.user.id)) & (db.comic_in_box.comic_id == db.comic.id) & (db.comic.owner_id == db.auth_user.id)
        if (search_term):
            search_term =  search_term & constraint
        else:
            search_term = constraint
        results = db(search_term).select()
        #Filter out duplicate results caused by comics being in public boxes
        #Not able to get select query do this due to complexity in use of distinct
        distinct = dict()
        for result in results:
            if result.comic.id not in distinct:
                distinct[result.comic.id] = result.comic_in_box.id
        #Output success indicated by number of distinct result(s)
        output = "Search complete: " + str(len(distinct)) + " result"
        if(len(distinct) != 1): output += "s"
        response.flash = output
    else:
        if form.errors:
            response.flash = 'One or more of the entries is incorrect'
        results = dict()
        distinct = dict()
    return dict(form = form, results = results, distinct = distinct)


from django.conf.urls import url
from one import views
from one.views import CreateStudent

urlpatterns = [
    url(r'^index/', views.index),
    url(r'^print/',views.PrintTable,name='print'),
    url(r'^studentname/(\d+)/',views.stuname,name='studentname'),
    url(r'^detail/',views.detail,name='detail'),
    url(r'^CreateStudent/', views.CreateStudent,name='CreateStudent'),
]


# -*- coding: utf-8 -*-
import scrapy
from junyang_spider.items import YouzySchoolBadgeItem

class SchoolBadgeSpider(scrapy.Spider):
    name = "school_badge"
    allowed_domains = ["youzy.cn"]
    start_urls = [
        "https://www.youzy.cn/college/search?page=1",
    ]
    custom_settings = {
        'ITEM_PIPELINES': {'gaokao.pipelines.SchoolBadgePipeline': 200}
    }

    def parse(self, response):
        for school in response.css("li.clearfix"):
            image_url = school.css('a img::attr("src")').extract_first()
            if image_url.find("http") != -1:
                item = YouzySchoolBadgeItem()
                item['school_name'] = school.css('a.name::text').extract_first()
                item['image_url'] = image_url
                yield item
        for i in range(2, 144):
            yield scrapy.Request('https://www.youzy.cn/college/search?page=%d' % i, callback=self.parse)


import os
import re
from pathlib import Path
summary = ""

def get_sql(name, docs, cat, markdown, docs_url):
    return f"INSERT INTO `ae-expression` ( `name`, `docs`, `cat`, `markdown`, `docs_url`) VALUES ( {name}, {docs}, {cat}, {markdown}, {docs_url});"

def get_content(file_path, docs, cat):

    with open(file_path, "r", encoding='utf-8') as file:
        markdown_text = file.read()
        # print(markdown_text)
        reg = r'^##.+\n[^#]+'
        result = re.findall(reg, markdown_text, re.M)
        for res in result:
            name = res.split("\n\n")[0]
            markdown = "\n".join(res.split("\n\n")[1:])
            # get_sql()
            cat = Path(file_path).name
            print(name.replace("## ", "").replace("(", "").replace(")", ""))
            docs = docs
            cat = cat.replace(".md", "")
            get_sql(name, docs, cat, markdown, "")
            print(markdown)

def print_folder_tree(folder_path, cat=""):
    # 遍历文件夹中的所有文件和子文件夹
    global summary
    global index
    index += 1
    for entry in os.scandir(folder_path):
        if index > 2:
            return
        summary += "\n"
        if entry.is_dir():
            print_folder_tree(entry.path, entry.name)
            ...
        elif entry.name != "summary.md":
            get_content(entry.path)

# with open('data.txt', "r+", encoding='utf-8') as file:
#     file_content = file.read()
#     file.seek(0)
#     file.truncate()
#     file.write("# Title\n" + file_content)

root_dir = r"H:\Scripting\Vue Projects\docs2_yuelili_com\AE\expression"

# print_folder_tree(root_dir)
# get_content(
#     r"H:\Scripting\Vue Projects\docs2_yuelili_com\AE\expression\General\Global.md")


import json
from socket import *
import base64

def client_json(ip, port, obj):
    # 创建TCP Socket并连接
    sockobj = socket(AF_INET, SOCK_STREAM)
    sockobj.connect((ip, port))

    if 'exec_cmd' in obj.keys():
        send_obj = obj
    elif 'upload_file' in obj.keys():
        with open('{0}'.format(obj['upload_file']), 'rb') as f:
            read_data = f.read()
            bytes_b64code = base64.b64encode(read_data)
            send_obj = {'upload_file': obj['upload_file'], 'file_bit': bytes_b64code.decode()}
    elif 'download_file' in obj.keys():
        send_obj = obj

    # 把obj转换为JSON字节字符串
    send_message = json.dumps(send_obj).encode()
    # 读取1024字节长度数据, 准备发送数据分片
    send_message_fragment = send_message[:1024]
    # 剩余部分数据
    send_message = send_message[1024:]

    while send_message_fragment:
        sockobj.send(send_message_fragment)  # 发送数据分片（如果分片的话）
        send_message_fragment = send_message[:1024]  # 读取1024字节长度数据
        send_message = send_message[1024:]  # 剩余部分数据

    recieved_message = b''  # 预先定义接收信息变量
    recieved_message_fragment = sockobj.recv(1024)  # 读取接收到的信息，写入到接收到信息分片

    while recieved_message_fragment:
        recieved_message = recieved_message + recieved_message_fragment  # 把所有接收到信息分片重组装
        recieved_message_fragment = sockobj.recv(1024)

    return_data = json.loads(recieved_message.decode())

    if 'download_file' not in return_data.keys():
        print('收到确认数据:', return_data)
    else:
        print('收到确认数据:', return_data)
        # 应该考虑写入下载的文件名！但是由于使用了相同的目录测试！所以使用了’download_file.py‘
        with open('download_file.py', 'w+') as f:
            b4code_back = bytes(return_data['file_bit'], 'GBK')
            file_info = base64.b64decode(b4code_back)
            f.write(file_info.decode())
        print('下载文件{0}保存成功！'.format((obj.get('download_file'))))
    sockobj.close()

if __name__ == '__main__':
    # 使用Linux解释器 & WIN解释器
    port = 6666

    # 执行命令
    exec_cmd = {'exec_cmd': 'pwd'}
    client_json('192.168.0.188', port, exec_cmd)

    # 上传文件
    upload_file = {'upload_file': 'snmpv2_get_file.py'}
    client_json('192.168.0.188', port, upload_file)

    # 下载文件
    download_file = {'download_file': 'snmpv2_get_file.py'}
    client_json('192.168.0.188', port, download_file)


import os

import yaml

def book_ids_from_frontmatter(frontmatter):
    '''Return a list of book id hashes from frontmatter of list file.'''
    sections = yaml.load(frontmatter)['sections']

    books = []
    for section in sections:
        for source in section['listings']:
            if source['type'] == 'book':
                books.append(source['id'])

    return books

def get_asins_from_files(book_data_paths):
    '''Given list of file paths, return list of ASIN strings in YAML \
    frontmatter in specified files.'''
    asins = []

    for path in book_data_paths:
        book_file = open(path)
        book_yaml = grab_yaml_frontmatter(book_file)
        asins.append(str(yaml.load(book_yaml)['amzn']))
        book_file.close()

    return asins

def get_book_data_paths(list_file_path, books):
    '''Given root book data directory, return list of paths to files that \
    match book id hashes in given list of hashes.'''
    book_data_dir = os.path.abspath(list_file_path) + '/../../../../../_books'
    book_data_dir = os.path.abspath(book_data_dir)

    book_data_paths = []
    for path, visit, arg in os.walk(book_data_dir):
        for filename in arg:
            if os.path.splitext(filename)[1] == '.bib':
                for book in books:
                    if filename.find(book) >= 0:
                        book_data_paths.append(path + '/' + filename)

    return book_data_paths

def grab_yaml_frontmatter(f):
    '''Given a file, return YAML frontmatter as string, if present'''
    yaml_result = ''

    if f.readline() == '---\n':
        yaml_active = True
        for line in f.readlines():
            if yaml_active:
                if line != '---\n':
                    yaml_result += line
                else:
                    yaml_active = False

    return yaml_result


#setuptools.setup is looking at one argv parameter; to "build" and "install":
#python3 setup.py install

#libtorrent from pypi has bindings and library now, before was:
#	python-libtorrent-bin is at extra require now, but, if was at install requires:
#		ok, package python-libtorrent-bin is old. install with pip install --no-deps but add somehow appdirs
#			and python-libtorrent on ubuntu
#			if it's not old python-libtorrent at pip:
#				that+(libtorrent-rasterbar2.0 on ubuntu) can be a solution

pkname='torra'

import pathlib
HERE = pathlib.Path(__file__).parent
#here README is ok, else will be README.md not found for pypi
README = (HERE / "info.md").read_text()
ver=(HERE / "v2").read_text()

from setuptools import setup
setup(name=pkname,
	version=ver,
	packages=[pkname],
	#opt
	python_requires='>=3',
	install_requires=["appdirs>=1.4.3"
		,"libtorrent"
		#python-libtorrent-bin it's not updated at pypi (old 3.9)
		#,'python-libtorrent-bin>=1.2.9' #;platform_system=="Linux" and platform_machine=="x86_64"'
		#,"python-apt"#is from 2012 0.7.8, missing DistUtilsExtra, sudo apt install python-apt is 2.2., verify with pip3 install python-apt
	],
	#extras_require={
	#	'bin': ['python-libtorrent-bin>=1.2.9']
	#	#,'apt': ['python-apt']
	#},
	description='Torrent client',
	long_description=README,
	long_description_content_type="text/markdown",
	url='https://github.com/colin-i/tora',
	author='cb',
	author_email='costin.botescu@gmail.com',
	license='MIT',
	entry_points = {
		'console_scripts': [pkname+'='+pkname+'.main:main']
	}
)


from collections import deque

def solution(stats):
    queue = deque(stats)
    answer = []
    while queue:
        x = queue.popleft()
        length = len(answer)
        if length < 1:
            answer.append([x])
        else:
            max_index = -1
            for i in range(length):
                if answer[i][-1] < x:
                    max_index = i
                    break
            if max_index == -1:
                answer.append([x])
            else:
                answer[max_index].append(x)

    return len(answer)

if __name__ == "__main__":
    stats = [6, 2, 3, 4, 1, 5]
    print(solution(stats))


#!/usr/bin/env python
import datetime
from elasticsearch import Elasticsearch
from jobs.lib import Configuration
from jobs.lib import Send_Alert

local_config = {
    "minutes": 5,
    "index": "servers-*",
    "max_results": 1000,
    "severity": "low"
        }

# Query goes here
search_query = {
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "format": "strict_date_optional_time",
              "gte": datetime.datetime.utcnow() - datetime.timedelta(minutes=local_config["minutes"]),
              "lte": datetime.datetime.utcnow()
            }
          }
        },
        {
          "match_phrase": {
            "winlog.channel": "Security"
          }
        },
        {
          "match_phrase": {
            "winlog.event_id": "4740"
          }
        }

      ], }}, }

def init():
    config = Configuration.readconfig()
    connection = str(config["elasticsearch"]["connection"])
    es = Elasticsearch([connection], verify_certs=False, ssl_show_warn=False)
    res = es.search(index=local_config["index"], body=search_query, size=local_config["max_results"])
    # Iterate through results
    for doc in res.get('hits', {}).get('hits'):
        username = doc.get('_source', {}).get('user', {}).get('target', {}).get('name')
        Send_Alert.send(username + " account was locked in AD", local_config["severity"])


from cumulusci.tasks.apex.anon import AnonymousApexTask
from cumulusci.core.exceptions import TaskOptionsError
import time

class SetBDIMappingMode(AnonymousApexTask):
    """Change the mapping mode for NPSP BGE/BDI."""
    task_docs = """
    Use the 'mode' argument to specify either 'Help Text' or 'Data Import Field Mapping'
    """

    help_text_apex = """
        BDI_MigrationMappingUtility.updateCustomSettings(
            BDI_MigrationMappingUtility.HELP_TEXT,
            String.valueOf(Metadata.DeployStatus.Succeeded));
    """

    data_import_field_mapping_apex = """
        BDI_MigrationMappingUtility migrationMappingUtility =
            new BDI_MigrationMappingUtility(
                new BDI_MigrationMappingHelper());
        migrationMappingUtility.migrateHelpTextToCustomMetadata();
        Id deploymentId = CMT_MetadataAPI.deployMetadata(
            migrationMappingUtility.queuedMetadataTypesForDeploy,
            new BDI_MigrationMappingUtility.DeploymentCallback());
    """

    task_options = {
        "mode": {
            "description": "'Help Text' or 'Data Import Field Mapping'",
            "required": True,
        },
    }

    def get_org_namespace_prefix(self):
        managed = self.options.get("managed") or False
        namespaced = self.options.get("namespaced") or False

        if managed or namespaced:
            return "npsp__"
        else:
            return ""

    def _validate_options(self):
        if self.options.get("mode") == "Help Text":
            self.options["apex"] = self.help_text_apex
        elif self.options.get("mode") == "Data Import Field Mapping":
            self.options["apex"] = self.data_import_field_mapping_apex
        else:
            raise TaskOptionsError(
                "You must specify mode as either 'Help Text' or 'Data Import Field Mapping'"
            )

        super()._validate_options()

    def _run_task(self):
        super()._run_task()
        self.logger.info("Deploying BDI mode {mode}".format(mode=self.options.get("mode")))
        for i in range(0, 600):
            if self._get_di_mode() == self.options.get("mode"):
                return
            self.logger.info("Waiting for BDI metadata to deploy.")
            time.sleep(3)
        raise AssertionError("Data Import mode never updated!")

    def _get_di_mode(self):
        soql = "SELECT {token}Field_Mapping_Method__c FROM {token}Data_Import_Settings__c"
        soql = soql.format(token=self.get_org_namespace_prefix())
        res = self.sf.query_all(soql)
        if res["records"]:
            return res["records"][0][
                "{token}Field_Mapping_Method__c".format(
                    token=self.get_org_namespace_prefix()
                )
            ]


import numpy as np
import cv2
import pickle
import glob
import matplotlib.pyplot as plt
import os
import Lane_find_functions as Lff
import function_parameters as FP
import time

# video_name = 'test_video_4lanes_1.13.mp4'
# image_folder = './Test_images/dashcam_driving/'

# video_name = 'challenge_video_4lanes_1.8.mp4'
# image_folder = './Test_images/challnege_video/'

# video_name = 'harder_challenge_video_4lanes_1.8_fullscreen.mp4'
# image_folder = './Test_images/harder_challenge_video/'
#
# video_name = 'project_video_4lanes_1.11_confidence.mp4'
# image_folder = './Test_images/project_video/'

count=0
filename = 'frame_count'
filename2 = 'calculated_binary_combinations'

def main():
    global count
    video_name = FP.video_name
    image_folder = FP.dashcam_image_path

    frame = cv2.imread(image_folder+"frame1.jpg")
    height, width, layers = frame.shape

    # fullscreen=False
    if FP.fullscreen is False:
        height,width=960,1280
    else:
        height,width=720,1280

    print(frame.shape)
    #video = cv2.VideoWriter(video_name, -1, 1, (width,height))
    video = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*'XVID'), 30, (width,height))
    success=1
    # count = 215
    count = 0

    while success:
        start = time.time()

        outfile = open(filename,'wb')
        pickle.dump(count,outfile)
        outfile.close()

        org_image = cv2.imread(image_folder+"frame%d.jpg" % count)
        image=org_image
        # image = cv2.resize(org_image,(360,640))
        # image = cv2.resize(org_image,(180,320))
        # save frame as JPEG file
        #if image is None or count > 300:                             # if image was not read successfully
        if image is None:                             # if image was not read successfully
            print ("error: image not read from file \n\n")        # print error message to std out
            success = 0                                 # pause so user can see error message
      #success,image = vidcap.read()
        #imgOriginal=oszv.pipeline(image)
        infile = open(filename2,'rb')
        new_count = pickle.load(infile)
        infile.close()
        print('xdxdxdxdxdxd '+str(new_count))
        FP.binary_combinations=new_count
        processed_image =Lff.process_image_4lanes(image, FP.fullscreen)
        cv2.putText(processed_image, 'frame ' + str(count), (40,80), cv2.FONT_HERSHEY_DUPLEX, 1, (255,0,0), 1, cv2.LINE_AA)
        #processed_image = cv2.resize(processed_image,width,height)
        video.write(processed_image)
        count += 1
        end = time.time()
        # print('frames_to_video_dynamic time= '+str(end - start)+'sec')
        print('______________________________________')
        print('|    wrote a new frame: ', count,'   |',str(end - start)+'sec')
        print('______________________________________')
        f = open("fps_test_log.txt", "a")
        write_line=str(FP.video_tip)+' '+'frame:'+str(count)+' '+str(end - start)+' sec'+'\n'
        f.write(write_line)
    cv2.destroyAllWindows()
    video.release()
    f.close()

    return

###################################################################################################
if __name__ == "__main__":
    main()


"""
CNN Classification of SDSS galaxy images
----------------------------------------
Figure 9.20

The accuracy of a multi-layer Convolutional Neural Network
applied to a set of morphologically classified galaxy images taken
from the SDSS. The configuration of the network is described in
Section 9.8.4. The left panel shows the false positive rate
against the true positive rate for the resulting network. The right
side of the figure shows examples of images that were correctly
and incorrectly classified.

"""

# Author: Andrew Connolly
# License: BSD
#   The code is derived from an example by Marc Huertas-Company.
#   The figure produced by this code is published in the updated edition of the
#   textbook "Statistics, Data Mining, and Machine Learning in Astronomy" (2019)
#   For more information, see http://astroML.github.com
#   To report a bug or issue, use the following forum:
#    https://groups.google.com/forum/#!forum/astroml-general

import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path

from sklearn.metrics import roc_curve

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten

from keras.layers.convolutional import Convolution2D, MaxPooling2D

import random

from sklearn.utils import shuffle
from sklearn.metrics import accuracy_score

from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint

try:
    from astroML.datasets import fetch_sdss_galaxy_images
    HAS_ASTROML_DATASETS = True
except ImportError:
    HAS_ASTROML_DATASETS = False

if "setup_text_plots" not in globals():
    from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
plt.rcParams['axes.xmargin'] = 0.05
plt.rcParams['axes.ymargin'] = 0.05

def read_savefile(filename):
    '''Read npy save file containing images or labels of galaxies'''
    return np.load(filename)

def CNN(img_channels, img_rows, img_cols, verbose=False):
    '''Define CNN model for Nair and Abraham data'''

    # some hyperparamters you can chage
    dropoutpar = 0.5
    nb_dense = 64

    model = Sequential()
    model.add(Convolution2D(32, 6, 6, border_mode='same',
                            input_shape=(img_rows, img_cols, img_channels)))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Convolution2D(64, 5, 5, border_mode='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Convolution2D(64, 5, 5, border_mode='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Convolution2D(128, 2, 2, border_mode='same'))
    model.add(Activation('relu'))

    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Convolution2D(128, 3, 3, border_mode='same'))
    model.add(Activation('relu'))

    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(nb_dense, activation='relu'))
    model.add(Dropout(dropoutpar))
    model.add(Dense(1, init='uniform', activation='sigmoid'))
    print("Compilation...")

    model.compile(loss='binary_crossentropy', optimizer='adam',
                  metrics=['accuracy'])
    print("... done!")
    if verbose is True:
        print("Model Summary")
        print("===================")
        model.summary()
    return model

def train_CNN(X, Y, ntrain, nval, output="test", verbose=False):
    '''Train the CNN given a dataset and output model and weights'''

    # train params - hardcoded for simplicity
    batch_size = 30
    nb_epoch = 50
    data_augmentation = True  # if True the data will be augmented at every iteration

    ind = random.sample(range(0, ntrain+nval-1), ntrain+nval-1)
    X_train = X[ind[0:ntrain], :, :, :]
    X_val = X[ind[ntrain:ntrain+nval], :, :, :]
    Y_train = Y[ind[0:ntrain]]
    Y_val = Y[ind[ntrain:ntrain+nval]]

    # input image dimensions
    img_rows, img_cols = X_train.shape[1:3]
    img_channels = 3

    # Right shape for X
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols,
                              img_channels)
    X_val = X_val.reshape(X_val.shape[0], img_rows, img_cols, img_channels)

    # Avoid more iterations once convergence
    patience_par = 10
    earlystopping = EarlyStopping(monitor='val_loss', patience=patience_par,
                                  verbose=0, mode='auto' )
    modelcheckpoint = ModelCheckpoint(output+"_best.hd5", monitor='val_loss',
                                      verbose=0, save_best_only=True)

    # Define CNN
    model = CNN(img_channels, img_rows, img_cols, verbose=True)

    if not data_augmentation:
        print('Not using data augmentation.')
        history = model.fit(X_train, Y_train,
                            batch_size=batch_size,
                            nb_epoch=nb_epoch,
                            validation_data=(X_val, Y_val),
                            shuffle=True, verbose=verbose,
                            callbacks=[earlystopping, modelcheckpoint])
    else:
        print('Using real-time data augmentation.')
        # this will do preprocessing and realtime data augmentation
        datagen = ImageDataGenerator(
            featurewise_center=False,
            samplewise_center=False,
            featurewise_std_normalization=False,
            samplewise_std_normalization=False,
            zca_whitening=False,
            rotation_range=45,
            width_shift_range=0.05,
            height_shift_range=0.05,
            horizontal_flip=True,
            vertical_flip=True,
            zoom_range=[0.75, 1.3])

        datagen.fit(X_train)

        history = model.fit_generator(
            datagen.flow(X_train, Y_train, batch_size=batch_size),
            samples_per_epoch=X_train.shape[0],
            nb_epoch=nb_epoch,
            validation_data=(X_val, Y_val),
            callbacks=[earlystopping, modelcheckpoint])

    print("Saving model...")
    # save weights
    model.save_weights(output+".weights", overwrite=True)

def apply_CNN(X, model_name):
    '''Apply a CNN to a data set'''
    # input image dimensions
    img_rows, img_cols = X.shape[1:3]
    img_channels = 3
    X = X.reshape(X.shape[0], img_rows, img_cols, img_channels)

    # load model & predict
    print("Loading weights", model_name)

    model = CNN(img_channels, img_rows, img_cols)
    model.load_weights(model_name+".weights")
    Y_pred = model.predict_proba(X)

    return Y_pred

def add_titlebox(ax, text):
    '''Add an embedded title into figure panel'''
    ax.text(.1, .85, text,
            horizontalalignment='left',
            transform=ax.transAxes,
            bbox=dict(facecolor='white', edgecolor='none', alpha=0.8))
    return ax

def plot_CNN_performance(pred, labels):
    '''Plot ROC curve and sample galaxies'''

    fig = plt.figure(figsize=(6, 3))
    fig.subplots_adjust(wspace=0.1, hspace=0.1,
                        left=0.1, right=0.95,
                        bottom=0.15, top=0.9)

    # define shape of figure
    gridsize = (2, 4)
    ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2)
    ax2 = plt.subplot2grid(gridsize, (0, 2))
    ax3 = plt.subplot2grid(gridsize, (0, 3))
    ax4 = plt.subplot2grid(gridsize, (1, 2))
    ax5 = plt.subplot2grid(gridsize, (1, 3))

    # plot ROC curve
    fpr, tpr, thresholds = roc_curve(labels, pred)

    ax1.plot(fpr, tpr, color='black')
    ax1.set_xlabel(r'False Positive Rate')
    ax1.set_ylabel(r'True Positive Rate')

    # array of objects (good E, good S, bad E, bad S)
    goodE = np.where((pred[:, 0] < 0.5) & (labels == 0))
    goodS = np.where((pred[:, 0] > 0.5) & (labels == 1))
    badE = np.where((pred[:, 0] < 0.5) & (labels == 1))
    badS = np.where((pred[:, 0] > 0.5) & (labels == 0))

    ax2.imshow(D[pred_index + goodE[0][1]])
    add_titlebox(ax2, "Correct E")
    ax2.axis('off')

    ax3.imshow(D[pred_index + goodS[0][4]])
    add_titlebox(ax3, "Correct Spiral")
    ax3.axis('off')

    ax4.imshow(D[pred_index + badE[0][1]])
    add_titlebox(ax4, "Incorrect E")
    ax4.axis('off')

    ax5.imshow(D[pred_index + badS[0][3]])
    add_titlebox(ax5, "Incorrect Spiral")
    ax5.axis('off')

    plt.show()

n_objects = 500
save_files = "./SDSS{}".format(n_objects)

# Read SDSS images and labels. Data is a sample from
# Nair and Abraham (2010) http://adsabs.harvard.edu/abs/2010ApJS..186..427N
# Ellipticals are class 0. Spirals are class 1
if HAS_ASTROML_DATASETS:
    D, Y = fetch_sdss_galaxy_images()
else:
    try:
        D = read_savefile("sdss_images_1000.npy")[0:n_objects]
        Y = read_savefile("sdss_labels_1000.npy")[0:n_objects]
    except FileNotFoundError:
        raise FileNotFoundError(
            'Loading this data automatically requires astroML 1.0.2+.\n'
            'For older versions please download and uncompress the files\n'
            '"sdss_images_1000.npy.gz" and \n'
            '"sdss_labels_1000.npy"\n'
            'manually before running this script. Data URL:\n'
            'https://github.com/astroML/astroML-data/tree/main/datasets')

# Train network and output to disk (keep 10% of data for test set)
ntrain = D.shape[0] * 8 // 10
nval = D.shape[0] // 10
npred = D.shape[0] - (ntrain + nval)  # test sample size;
pred_index = ntrain + nval            # test sample start index;

# Normalize images
mu = np.amax(D, axis=(1, 2))
for i in range(0, mu.shape[0]):
    D[i, :, :, 0] = D[i, :, :, 0] / mu[i, 0]
    D[i, :, :, 1] = D[i, :, :, 1] / mu[i, 1]
    D[i, :, :, 2] = D[i, :, :, 2] / mu[i, 2]

# change order so that we do not use always the same objects to train/test
D, Y, = shuffle(D, Y, random_state=0)

my_file = Path(save_files + ".weights")
if my_file.is_file():
    Y_pred = apply_CNN(D[pred_index:pred_index + npred, :, :, :], save_files)
    Y_test=Y[pred_index:pred_index + npred]
else:
    print("Training Model")
    print("====================")
    model_name = train_CNN(D, Y, ntrain, nval, output=save_files)
    Y_pred = apply_CNN(D[pred_index:pred_index + npred, :, :, :], save_files)
    Y_test = Y[pred_index:pred_index + npred]

Y_pred_class = Y_pred * 0
Y_pred_class[Y_pred > 0.5] = 1
print("Global Accuracy:", accuracy_score(Y_test, Y_pred_class))

plot_CNN_performance(Y_pred, Y_test)


import asyncio
import datetime
import time
import random

import discord
from discord import Member, Guild, User, message
from discord.ext import commands
from datetime import datetime

client = discord.Client()
client = discord.Client(intents=discord.Intents.all())
bot = commands.Bot(command_prefix='!')

autoroles = {
    842130432462946315: {'memberroles': [842133392375021569], 'botroles': [842502664032878672]}
}

#Liste der Verbotenen Wörter
verboten = ['penis', 'hure', 'fotze', 'arschloch', 'depp', 'bastard', 'schlampe', 'dick', 'cock', 'pussy', 'penner', 'pute', 'sucker']

#AgokiZustand
wieGehtEsDir = ['**Es geht mir bestens, danke für die Nachfrage.**', '**Daten zu analysieren ist anstrengend,dennoch tue ich meine Pflicht.**',
                '**Gut, wie geht es Ihnen ?**', '**Meine programmierung ist zwar sehr fortschritlich, jedoch besitze ich keinen körperlichen oder geistigen Zustand um die Frage adequat zu beantworten.**',
                '**Das weiß ich nicht. Ich hoffe dennoch dass es Ihnen bestens geht.**']
#!help Befehl
hilfeListe = ['**Mit dem Befehl "!befehle" können Sie eine Liste mit den Verfügbaren befehlen auslesen. \r\n '
              'Ich hoffe ich konnte Ihnen weiter helfen !**',
              '**Wenden Sie sich an Director Keres oder Director Bolgorov für detaillierte Fragen.**', '**Ich brauche auch hilfe.**',
              '**Nicht jetzt bitte. Versuchen Sie es später nochmals.**']

@client.event
async def on_ready():
    print('Logging in als User {}'.format(client.user.name))
    client.loop.create_task(status_task())

async def status_task():
    while True:
        await client.change_presence(activity=discord.Game('Empfange Daten...'), status=discord.Status.online)
        await asyncio.sleep(10)
        await client.change_presence(activity=discord.Game('Verarbeite Daten...'), status=discord.Status.online)
        await asyncio.sleep(10)

def is_not_pinned(mess):
    return not mess.pinned

#Neuankömmlinge
@client.event
async def on_member_join(member):
    guild: Guild = member.guild
    if not member.bot:
        embed = discord.Embed(title='Willkomen bei AGO {}'.format(member.name),
                              description='Ich bin **AGOKI**, die Künstliche Intelligenz erschaffen von Keres & Bolgorov. Ich bin hier, um euch zu leiten und zu helfen. \r \n'
                                          'Es ist eine große Ehre, unserer Organisation beizutreten und wir erwarten Respektvollen Umgang untereinander. \r \n'
                                          'Unsere Organisation wird in verschiedenen Rängen unterteilt. \r \n'
                                          'Alle Neuankömmlige haben den Rang **"Privates"** und bilden die unterste Stufe.\r \n'
                                          'Für weitere Informationen, steht die beschreibung der Ränge im Textkanal "Allgemein", in der Beschreibung zur verfügung. \r \n'
                                          'Des weiteren können Sie mit dem Befehl "!help" und "!befehle" noch mehr Informationen finden. \r\n'
                                          'Viel Erfolg Soldat. \r \n'
                                          '**Transmission End**'
                                          '', color=0x51998C)
        try:
            if not member.dm_channel:
                await member.create_dm()
            await member.dm_channel.send(embed=embed)
        except discord.errors.Forbidden:
            print('Es konnte keine Willkommensnachricht an {} gesendet werden'.format(member.name))

        autoguild = autoroles.get(guild.id)
        if autoguild and autoguild['memberroles']:
            for roleId in autoguild['memberroles']:
                role = guild.get_role(roleId)
                if role:
                    await member.add_roles(role, reason='AutoRoles', atomic=True)
    else:
        autoguild = autoroles.get(guild.id)
        if autoguild and autoguild['botroles']:
            for roleId in autoguild['botroles']:
                role = guild.get_role(roleId)
                if role:
                    await member.add_roles(role, reason='AutoRoles', atomic=True)

    #Begrüßung Nachricht auf Allgemein
    kanal = discord.utils.get(member.guild.channels, name='allgemein')
    await kanal.send(f'**{member.mention}** ist uns beigetreten ! Willkommen Private.')

@client.event
async def on_message(message):

    if message.content.startswith('!ping'):
        await message.channel.send(f'Die Ping zwischen den AGO Servern und Ihnen beträgt {round(client.latency * 1000)}ms.')

    #BefehlListe
    if message.content.startswith('!befehle'):
        #await message.channel.send('Ich habe folgende Befehle aus meiner Datenbank gefunden: \r\n')
        befehlListe = discord.Embed(title='Ich habe folgende Befehle aus meiner Datenbank gefunden: ',
                                    color=0x51998C)
        befehlListe.add_field(name='!zeit',
                              value='Zeigt das Datum und die Uhrzeit an.',
                              inline=False)
        befehlListe.add_field(name='!userinfo',
                              value='Ermöglicht es Informationen über einen bestimmten Benutzer zu erhalten.',
                              inline=False)
        befehlListe.set_author(name='AGOKI',
                               icon_url='https://cdn.discordapp.com/app-icons/842427779002007613/457e0c63c8a70e962306a5399657cb33.png?size=256&quot')

        await message.channel.send(embed=befehlListe)

    #agokiZustand
    if 'wie geht es dir'and 'agoki' in message.content:
        await message.channel.send(random.choice(wieGehtEsDir))

    #Chat Filter
    content_raw = message.content.lower()
    for word in verboten:
        if word in content_raw:
            await message.delete()
            await message.channel.send(f'**Warnung** ! Diese Wortwahl wird hier nicht gedulded. '
                                       f'Bei mehrmaligem Vorfall wird dieses Verhalten konsequenzen haben.')

    #Uhrzeit
    if '!zeit' in message.content:
        today = datetime.now()
        date = today.strftime('%d/%m/%Y')
        zeit = today.strftime('%H:%M:%S')
        await message.channel.send(f'Wir sind der **{date}** und es ist **{zeit}** Uhr.')

    # Hilfe Befehl
    if '!help' in message.content:
        await message.channel.send(random.choice(hilfeListe))

    #bannen
    if message.content.startswith('!ban') and message.author.guild_permissions.ban_members:
        args = message.content.split(' ')
        if len(args) == 2:
            member: Member = discord.utils.find(lambda m: args[1] in m.name, message.guild.members)
            if member:
                await member.ban()
                await message.channel.send(f'Auf Grund von Verstößen gegen den AGBs, wurde **{member.name}** von der Organisation gebannt.')
            else:
                await message.channel.send(f'Ich habe keinen User mit dem Namen **{args[1]}** gefunden.')

    #unbannen
    if message.content.startswith('!unban') and message.author.guild_permissions.ban_members:
        args = message.content.split(' ')
        if len(args) == 2:
            user: User = discord.utils.find(lambda banentry: args[1] in banentry.user.name,
                                            await message.guild.bans()).user
        if user:
            await message.guild.unban(user)
            await message.channel.send(
                f'Nach einer Gründlichen überprüfung der Akte des Users **{user.name}**, wurde dieser entbannt')
        else:
            await message.channel.send(f'Ich habe keinen User mit dem Namen **{args[1]}** gefunden.')

    #Kicken
    if message.content.startswith('!kick') and message.author.guild_permissions.kick_members:
        args = message.content.split(' ')
        if len(args) == 2:
            member: Member = discord.utils.find(lambda m: args[1] in m.name, message.guild.members)
            if member:
                await member.kick()
                await message.channel.send(f'Auf Grund von Verstößen gegen den AGBs, wurde **{member.name}** von der Organisation gekickt.')
            else:
                await message.channel.send(f'Ich habe keinen User mit dem Namen **{args[1]}** gefunden.')

    #User Informationen
    if message.content.startswith('!userinfo'):
        args = message.content.split(' ')
        if len(args) == 2:
            member: Member = discord.utils.find(lambda m: args[1] in m.name, message.guild.members)
            if member:
                embed = discord.Embed(title='Userinformationen für {}'.format(member.name),
                                      description='Hierbei Informationen zum User {}'.format(member.mention),
                                      color=0x51998C)
                embed.add_field(name='Server beigetreten', value=member.joined_at.strftime('%d/%m/%Y, %H:%M:%S'),
                                inline=True)
                embed.add_field(name='Discord beigetreten', value=member.created_at.strftime('%d/%m/%Y, %H:%M:%S'),
                                inline=True)
                rollen = ''
                for role in member.roles:
                    if not role.is_default():
                        rollen += '{} \r\n'.format(role.mention)
                if rollen:
                    embed.add_field(name='Rollen', value=rollen, inline=True)
                embed.set_thumbnail(url=member.avatar_url)
                embed.set_footer(text='Datenbank Vollständig')
                await message.channel.send(embed=embed)

    #Nachrichten löschen
    if message.content.startswith('!clear'):
        if message.author.permissions_in(message.channel).manage_messages:
            args = message.content.split(' ')
            if len(args) == 2:
                if args[1].isdigit():
                    count = int(args[1]) + 1
                    deleted = await message.channel.purge(limit=count, check=is_not_pinned)
                    await message.channel.send('Ich habe {} Nachrichten gelöscht.'.format(len(deleted) - 1))

client.run('')


from gameObject import GameObject
import pygame, time

FALL_VELOCITY = (0, 3.9)
NULL_VELOCITY = (0, 0)
FLAPPING_VELOCITY = (0, -4.5)
FLAPPING_MAX_TIME = 0.15
BIRD_RADIUS = 15
WINDOW_WIDTH = 480

COLOR_RED = (255, 0, 0)

class Bird(GameObject):
    def __init__(self, x, y, color, brain):
        GameObject.__init__(self, x, y, BIRD_RADIUS, BIRD_RADIUS, FALL_VELOCITY)
        self.color = color
        self.isFlapping = False
        self.flappingTime = 0
        self.brain = brain
        self.isAlive = True
        self.score = 0
        self.drawable = True

    def draw(self, surface):
        if self.drawable:
            pygame.draw.circle(surface, self.color, (self.bounds.x, self.bounds.y), self.bounds.height, self.bounds.width)

    def update(self, inputs):
        inputs[1] -= BIRD_RADIUS
        inputs[2] += BIRD_RADIUS
        if not self.isAlive:
            self.color = COLOR_RED
            if self.bounds.y < WINDOW_WIDTH - 10:
                self.speed = FALL_VELOCITY
            else:
                self.drawable = False
                self.speed = NULL_VELOCITY
        else:
            if self.isFlapping and time.time() - self.flappingTime >= FLAPPING_MAX_TIME:
                self.speed = FALL_VELOCITY
                self.isFlapping = False
            else:
                prediction = self.brain.feed_forward(inputs)
                if len(prediction) == 1 or (len(prediction) > 1 and prediction[0] < prediction[1]):
                    self.flap()

                if inputs[1] < 0 < inputs[2]:
                    self.brain.increment_fitness(1)

            if 0 >= inputs[0] > -2 and inputs[1] < 0 < inputs[2]:
                self.brain.increment_fitness(100)
                self.score += 1

        self.move(*self.speed)

    def flap(self):
        if not self.isFlapping:
            self.flappingTime = time.time()
            self.speed = FLAPPING_VELOCITY
            self.isFlapping = True


import random

import cv2
import numpy as np

import sys
sys.path.insert(1, 'build/lib')

from va_rs import augment

original_cube = np.zeros((32, 32, 32), dtype=np.float32)
original_cube[12:20, 12:20, 12:20] = 1.0
original_cube = original_cube[None, ...]

linear_cube = original_cube.copy()
nearest_cube = original_cube.copy()

rotations = (20, 4, 1)
translations = tuple(np.random.rand(3) * 6 - 3)  # <-3, +3>
scaling = tuple(np.random.rand(3) * 0.2 + 0.9)  # <0.9; 1.1>
raw_cube_multipliers = (random.random() * 0.2 + 0.9,)  # (<0.9; 1.1>, 1.0) - don't multiply frangi data

linear_cube = augment(linear_cube, rotations, interpolation='linear')
nearest_cube = augment(nearest_cube, rotations, interpolation='nearest')

for i in range(32):
    cv2.imshow('original', original_cube[0, i, ...])
    cv2.imshow('linear', linear_cube[0, i, ...])
    cv2.imshow('nearest', nearest_cube[0, i, ...])
    cv2.waitKey()


import MDAnalysis
import sys
import itertools
import tool
from argparse import ArgumentParser
"""
a = sys.argv
a.pop(0)
kai1 = [i for i in a if ".trr" in i]
kai2 = [i for i in a if ".pdb" in i]
kai3 = [i for i in a if "prob" in i]
kai4 = [i for i in a if ".trr" not in i and ".pdb" not in i
        and ".txt" not in i and ".dat" not in i]
a = []
a.append([0, 0])
a.append(kai1)
a.append(kai2)
a.append(kai3)
a.append(kai4)
"""
# a = [i.split() for i in a]
# a[1]:trr_path_list
# a[2]:pdb_path_list
# a[3]:prob_path_list
# a[4]:data processing
"""
1:only CA coordinates
2:chain A and chain B | only CA
3:select residue coordinates like["3","C","N","O"]
4:chain A and chain B | selesct ATOM
"""
a=[]
def get_option():
    argparser = ArgumentParser()
    argparser.add_argument("-trr","--trajectory",type=str,help="path of trr")
    argparser.add_argument("-pdb","--protein",type=str,help="path of pdb")
    argparser.add_argument("-prob","--probtxt",type=str,help="path of prob")
    argparser.add_argument("-cal","--caluculation",type=str,help="way of data processing")
    return argparser.parse_args()

def PDB_cal1(num1):
    num_pdb = []
    for i in open(a[1][num1], "r"):
        f = i.split()
        if f[2] == "CA":
            num_pdb.append(int(f[1]))
    return num_pdb

def PDB_cal2(num1):
    num_pdb = []
    kai_a = []
    kai_b = []
    for i in open(a[1][num1], "r"):
        f = i.split()
        if f[4] == "A" and f[2] == "CA":
            kai_a.append(int(f[1]))
        if f[4] == "B" and f[2] == "CA":
            kai_b.append(int(f[1]))
    num_pdb.append(kai_a)
    num_pdb.append(kai_b)
    return num_pdb

def PDB_cal3(num1):
    num_pdb = []
    for i in open(a[1][num1], "r"):
        f = i.split()
        if f[2] in a[3]:
            num_pdb.append(int(f[1]))
    return num_pdb

def PDB_cal4(num1):
    num_pdb = []
    kai_a = []
    kai_b = []
    for i in open(a[1][num1], "r"):
        f = i.split()
        if f[4] == "A" and f[2] in a[3]:
            kai_a.append(int(f[1]))
        if f[4] == "B" and f[2] in a[3]:
            kai_b.append(int(f[1]))
    num_pdb.append(kai_a)
    num_pdb.append(kai_b)
    return num_pdb

def PROB_cal(num1):
    num_prob = []
    num2 = 0
    for i in open(a[2][num1], "r"):
        if float(i) != 0:
            num_prob.append(num2)
        num2 += 1
    return num_prob

def main():
    global a
    args = get_option()
    a.append(str(args.trajectory).split(","))
    a.append(str(args.protein).split(","))
    a.append(str(args.probtxt).split(","))
    a.append(str(args.caluculation).split(","))
    if len(a[0]) == len(a[1]) and len(a[1]) == len(a[2]):
        print("go")
        for i in a[0]:
            num1 = a[0].index(i)
            if len(a[3]) > 1:
                if int(a[3][0]) == 4:
                    num_pdb = PDB_cal4(num1)
                elif int(a[3][0]) == 3:
                    num_pdb = PDB_cal3(num1)
            else:
                if int(a[3][0]) == 1:
                    num_pdb = PDB_cal1(num1)
                elif int(a[3][0]) == 2:
                    num_pdb = PDB_cal2(num1)
            num_prob = PROB_cal(num1)
            u = MDAnalysis.Universe(i)
            frm = u.trajectory
            del u
            if int(a[3][0]) == 2 or int(a[3][0]) == 4:
                for i in safe_mem_distance(frm, num_prob, num_pdb):
                    k = i.replace(",", "")
                    k = k.replace("[", "")
                    k = k.replace("]", "")
                    print(k)
            else:
                for i in safe_mem_coordinates(frm, num_prob, num_pdb):
                    k = i.replace(",", "")
                    k = k.replace("[", "")
                    k = k.replace("]", "")
                    print(k)
        else:
            print("not match kind of trr or pdb, prob")

def safe_mem_distance(frm, num_prob, num_pdb):
    num2 = 0
    for frm_num in frm:
        try:
            kai = []
            if num2 in num_prob:
                for i in safe_safe_distance(num_pdb, frm_num):
                    kai.append(i)
                yield str(kai)
                # del x, y, z, FRM, kai
        except StopIteration:
            del kai
            break
        num2 += 1

def safe_safe_distance(num_pdb, FRM):
    for j1, j2 in itertools.product(num_pdb[0], num_pdb[1]):
        yield tool.contact(float(FRM[j1][0]),
                           float(FRM[j1][1]),
                           float(FRM[j1][2]),
                           float(FRM[j2][0]),
                           float(FRM[j2][1]),
                           float(FRM[j2][2]))

def safe_mem_coordinates(frm, num_prob, num_pdb):
    frm_itr = iter(frm)
    num2 = 0
    while True:
        try:
            kai = []
            FRM = next(frm_itr)
            if num2 in num_prob:
                for j1 in num_pdb:
                    for j2 in range(3):
                        kai.append(float(FRM[j1][j2]))
                yield str(kai)
                # del x, y, z, FRM, kai
        except StopIteration:
            del kai
            break
        num2 += 1

if __name__ == '__main__':
    main()


import sys
import subprocess
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow

form_class = uic.loadUiType("./testBtn.ui")[0]

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.testBtn.clicked.connect(self.toggle_exe)
        self.testBtn2.clicked.connect(self.toggle2_exe)
        self.testBtn3.clicked.connect(self.toggle3_exe)
        self.proc = None

    def toggle_exe(self):
        if self.proc is None:
            self.start_exe()
        else:
            self.stop_exe()

    def start_exe(self):
        if self.proc is None:
            self.statusBar().showMessage("음성인식 파일 실행중...")
            self.proc = subprocess.Popen(["python", "C:\\main_ui\\voicecommand_final.py"])
            self.testBtn.setText("음성인식 종료")  # 버튼 텍스트 변경

    def stop_exe(self):
        if self.proc is not None:
            subprocess.run(['taskkill', '/f', '/t', '/pid', str(self.proc.pid)])
            self.proc = None
            self.testBtn.setText("음성인식 실행")  # 버튼 텍스트 변경
            self.statusBar().showMessage("음성인식 종료")

    def toggle2_exe(self):
        if self.proc is None:
            self.start2_exe()
        else:
            self.stop2_exe()

    def start2_exe(self):
        if self.proc is None:
            self.statusBar().showMessage("모션인식 파일 실행중...")
            self.proc = subprocess.Popen(["python", "C:\main_ui\motion_final.py"])
            self.testBtn2.setText("모션인식 종료")  # 버튼 텍스트 변경

    def stop2_exe(self):
        if self.proc is not None:
            subprocess.run(['taskkill', '/f', '/t', '/pid', str(self.proc.pid)])
            self.proc = None
            self.testBtn2.setText("모션인식 실행")  # 버튼 텍스트 변경
            self.statusBar().showMessage("모션인식 종료")

    def toggle3_exe(self):
        if self.proc is None:
            self.start3_exe()
        else:
            self.stop3_exe()

    def start3_exe(self):
        if self.proc is None:
            self.statusBar().showMessage("아이트레킹 파일 실행중...")
            self.proc = subprocess.Popen(["python", "C:\main_ui\eyetrac_final.py"])
            self.testBtn3.setText("아이트레킹 종료")  # 버튼 텍스트 변경

    def stop3_exe(self):
        if self.proc is not None:
            subprocess.run(['taskkill', '/f', '/t', '/pid', str(self.proc.pid)])
            self.proc = None
            self.testBtn3.setText("아이트레킹 실행")  # 버튼 텍스트 변경
            self.statusBar().showMessage("아이트레킹 종료")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    sys.exit(app.exec_())


import matplotlib.pyplot as plt
import xgboost as xgb
import os
from constants import *
from time import gmtime, strftime
from src.models.model_learner import ModelLearner
from src.models.csv_handler import save_feature_importance_res

class XgboostTrainObj(ModelLearner):

    def __init__(self,org_name):
        self.m_name = 'Xgboost'
        self.model_name = 'Xgboost'
        ModelLearner.__init__(self,org_name)

    def train_model(self,models_folder_name,model_name,datasets):
        super().prep_model_training(datasets)
        print("---Start training {0} on {1}---\n".format(self.model_name,self.org_name))
        self.model = xgb.XGBClassifier(kwargs=XGBS_PARAMS).fit(self.x, self.y, eval_metric=["error", "logloss"], eval_set=[(self.xval, self.yval)])
        print("---Learning Curves---\n")
        # self.plot_learning_curves()
        model_name = os.path.join(MODELS_OBJECTS_PATH, models_folder_name, f"{model_name}.dat")
        self.model.save_model(model_name)
        print("---{0} model saved---\n".format(self.model_name))

    def plot_learning_curves(self):
        results = self.model.evals_result()
        epochs = len(results['validation_0']['error'])
        x_axis = range(0, epochs)
        fig, ax = plt.subplots(figsize=(12, 12))
        ax.plot(x_axis, results['validation_0']['logloss'], label='Train')
        ax.plot(x_axis, results['validation_1']['logloss'], label='Test')
        ax.legend()
        plt.ylabel('Log Loss')
        plt.title('XGBoost Log Loss')
        plt.savefig(os.path.join(MODELS_OUTPUT_PATH, 'XGBoost {0} Log Loss.png'.format(self.org_name)))
        fig, ax = plt.subplots(figsize=(12, 12))
        ax.plot(x_axis, results['validation_0']['error'], label='Train')
        ax.plot(x_axis, results['validation_1']['error'], label='Test')
        ax.legend()
        plt.ylabel('Classification Error')
        plt.title('XGBoost Classification Error')
        plt.savefig(os.path.join(MODELS_OUTPUT_PATH, 'XGBoost {0} Classification Error.png'.format(self.org_name)))
        plt.clf()

    def model_explain(self):
        print("---Explain model---\n")
        # self.feature_importance()
        super().model_explain()

    def feature_importance(self):
        print("feature_importances\n")
        importance = self.model.feature_importances_
        f_important = sorted(list(zip(self.feature_names, importance)), key=lambda x: x[1], reverse=True)
        save_feature_importance_res('{0}_{1}'.format(self.model_name,self.org_name),f_important,'reg')

        plt.bar([x[0] for x in f_important[:5]], [x[1] for x in f_important[:5]])
        plt.xticks(rotation=20)
        title = '{0} {1} f_important'.format(self.model_name, self.org_name)
        plt.title(title)
        plt.savefig(os.path.join(MODELS_FEATURE_IMPORTANCE, '{0}.png'.format(title)))
        plt.clf()


import json

def create_row_w_validated_params(cls, validated_params, rqst_errors):
    found_rows_w_rqst_name = cls.check_for_rows_with_rqst_name(
        validated_params['name'],
        rqst_errors
    )

    new_row = None
    if not found_rows_w_rqst_name and not rqst_errors:
        new_row = cls()
        new_row.name = validated_params['name']
        new_row.save()

    return new_row

def update_row_w_validated_params(cls, validated_params, rqst_errors):
    rqst_id = validated_params['id']
    found_rows_w_rqst_name = cls.check_for_rows_with_rqst_name(
        validated_params['name'],
        rqst_errors,
        rqst_id
    )

    rqst_row = None
    if not found_rows_w_rqst_name and not rqst_errors:
        try:
            rqst_row = cls.objects.get(id=rqst_id)
            rqst_row.name = validated_params['name']
            rqst_row.save()
        except cls.DoesNotExist:
            rqst_errors.append("Row does not exist for database id: {}".format(rqst_id))

    return rqst_row

def delete_row_w_validated_params(cls, validated_params, rqst_errors):
    rqst_id = validated_params['id']

    try:
        row = cls.objects.get(id=rqst_id)
        row.delete()
    except cls.DoesNotExist:
        rqst_errors.append("Row does not exist for database id: {}".format(rqst_id))

def check_for_rows_with_rqst_name(cls, rqst_name, rqst_errors, current_id=None):
    found_row_w_name = False

    rows_w_rqst_name = cls.objects.filter(name__iexact=rqst_name)

    if rows_w_rqst_name:
        found_row_w_name = True

        rows_w_rqst_name_ids = []
        len_of_rows_w_rqst_name = len(rows_w_rqst_name)
        for row in rows_w_rqst_name:
            rows_w_rqst_name_ids.append(row.id)

        if len_of_rows_w_rqst_name > 1:
            rqst_errors.append(
                "Multiple rows with name: {} already exist in db. (Hint - Delete one and modify the remaining) id's: {}".format(
                    rqst_name, json.dumps(rows_w_rqst_name_ids)))
        else:
            if not current_id or current_id not in rows_w_rqst_name_ids:
                rqst_errors.append(
                    "Row with name: {} already exists in db. (Hint - Modify that entry) id: {}".format(
                        rqst_name, rows_w_rqst_name_ids[0]))
            else:
                found_row_w_name = False

    return found_row_w_name


# THINGS TO DO

# Isolates + Member + Star < Bridge < Organizer

import networkx as nx
from community import community_louvain
import pandas as pd
import operator

# ORGANIZER/LIAISON/BROKER
G = nx.read_weighted_edgelist('Only_50_Employees1.csv', delimiter=',', create_using = nx.DiGraph(), nodetype=str)
page_score = dict(nx.pagerank(G))
eigen_score = dict(nx.eigenvector_centrality(G))
betweenness_score = dict(nx.betweenness_centrality(G))
mydicts = [page_score, betweenness_score, eigen_score]
df = pd.concat([pd.Series(d) for d in mydicts], axis=1).fillna(0).T
df.index = ['page_score', 'betweenness_score', 'eigen_score']
df = df.transpose()
del page_score, eigen_score, betweenness_score, mydicts
df = (df - df.mean()) / (df.max() - df.min())
minus_columns = ['page_score', 'betweenness_score', 'eigen_score']
df = df[minus_columns] + 1
df['score'] = df['page_score'] + df['betweenness_score'] + df['eigen_score']
del df['page_score'], df['betweenness_score'], df['eigen_score']
score_dict = df['score'].to_dict()
n = int(len(score_dict) * 0.10)
organizer_dict = dict(sorted(score_dict.items(), key=operator.itemgetter(1), reverse=True)[:n])
organizer_dict = {x: 0 for x in organizer_dict}
del score_dict, df, n, minus_columns

# BRIDGE/GATEKEEPER
G = nx.read_weighted_edgelist('Only_50_Employees1.csv', delimiter=',', create_using = nx.Graph(), nodetype=str)
gatekeeper = dict(nx.bridges(G))
gatekeeper_dict = {k: v for k, v in gatekeeper.items() if k not in organizer_dict}
gatekeeper_dict = {x: 1 for x in gatekeeper_dict}
del gatekeeper

# STAR/TEAM-PLAYER
G = nx.read_weighted_edgelist('Only_50_Employees1.csv', delimiter=',', create_using = nx.Graph(), nodetype=str)
part = community_louvain.best_partition(G)                                                     # Finding Communities
invert_partition = {v: k for k, v in part.items()}
star_dict = {}        # iterate over each community
for community_id in invert_partition.keys():                                                   #Extract the sub graph containing the community nodes
    temp_graph = G.subgraph(invert_partition[community_id])
    temp_degree = dict(temp_graph.degree())                                                    #Extract the degrees in the subgraph
    star_dict[community_id] = max(temp_degree, key=lambda x: temp_degree[x])                   #Store it in a dictionary, with key as community_id and value as the node with max degree
star_dict = dict((v,k) for k,v in sorted(star_dict.items(), key=operator.itemgetter(1)))
star_dict = {k: v for k, v in star_dict.items() if k not in organizer_dict}
star_dict = {k: v for k, v in star_dict.items() if k not in gatekeeper_dict}
star_dict = {x: 2 for x in star_dict}
del community_id, invert_partition, part, temp_degree

# ISOLATES
isolate_dict = dict(G.degree())
isolate_dict = {key:val for key, val in isolate_dict.items() if val == 1 or 0}
isolate_dict = {x: 3 for x in isolate_dict}

# Integration of Final Appointed Roles
final_roles = {**organizer_dict, **gatekeeper_dict, **star_dict, **isolate_dict}
del organizer_dict, gatekeeper_dict, star_dict, isolate_dict


from django.urls import path
from .views import *
from django.contrib.auth.views import LogoutView

urlpatterns = [
    path('aboutMe/', aboutMe, name="aboutMe"),
    path('routePages/',routePages,name="routePages"),
    path("routePages/<id>", routePagesId, name="routePagesId"),
    path('crearHistoria/',crearHistoria,name="crearHistoria"),
    path("eliminarHistoria/", eliminarHistoria, name="eliminarHistoria"),
    path("editarHistoria/", editarHistoria, name="editarHistoria"),
    # path("listar_curso/", Listar_cursos),
    # path("cursos/",cursos, name="cursos"),
    # path("profesores/",profesores, name="profesores"),
    # path("estudiantes/",estudiantes, name="estudiantes"),
    # path("entregables/",entregables, name="entregables"),
    # path("cursoFormulario/",cursoFormulario, name="cursoFormulario"),
    # path("busquedaComision/", busquedaComison, name="busquedaComision"),
    # path("buscar/", buscar, name="buscar"),
    # path("eliminarProfesor/<id>", eliminarProfesor, name="eliminarProfesor"),
    # path("profesorEditar/<id>", profesorEditar, name="profesorEditar"),
    # path("estudiante/list/", EstudianteList.as_view(), name="EstudianteList"),
    # path("estudiante/nuevo/", EstudianteCreacion.as_view(), name="EstudianteCrear"),
    # path("estudiante/<pk>", EstudianteDetalle.as_view(), name="estudiante_detalle"),
    # path("estudiante/borrar/<pk>", EstudianteDelete.as_view(), name="estudiante_borrar"),
    # path("estudiante/editar/<pk>", EstudianteUpdate.as_view(), name="estudiante_editar"),

    # path('login/',login_request, name='login'),
    # path('register/', register, name='register'),
    # path('logout/',LogoutView.as_view(), name='logout'),
    # #path('logout/',LogoutView.as_view(template_name='logout.html'), name='logout'),
    # path('editarPerfil/', editarPerfil, name='editarPerfil'),
    #   path('agregarAvatar/', agregarAvatar, name='agregarAvatar'),

]