import os
import json
from typing import List, Dict, Optional
import urllib
import tempfile

from .Its4landAPI import Its4landException
from . import utils

from qgis.core import QgsVectorLayer, QgsWkbTypes, QgsRasterLayer
from PyQt5 import uic
# from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QShowEvent
from PyQt5.QtWidgets import QDialog

FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'BoundaryDelineationIts4landWindow.ui'))
HL_HARDCODED_PROJECTION = 'urn:ogc:def:crs:OGC:1.3:CRS84'

__ = utils.__

class BoundaryDelineationIts4landWindow(QDialog, FORM_CLASS):
    # login = pyqtSignal()
    # logout = pyqtSignal()
    # projectSelected = pyqtSignal()
    # validationSetSelected = pyqtSignal()

    def __init__(self, plugin, parent=None):
        """Constructor."""
        super(BoundaryDelineationIts4landWindow, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)

        self.plugin = plugin
        self.service = plugin.service

        self.project: Optional[str] = None
        self.projects = None
        self.validationSets = None
        self.baseLayers = None
        self.baseLayerRemoteName = None
        self.contentItem = None
        self.contentItemFilename = None
        self.boundaryStrings: Optional[Dict] = None

        self.loginInput.textChanged.connect(self.onLoginInputChanged)
        self.passwordInput.textChanged.connect(self.onLoginInputChanged)
        self.loginButton.clicked.connect(self.onLoginButtonClicked)
        self.connectButton.clicked.connect(self.onConnectButtonClicked)
        self.logoutButton.clicked.connect(self.onLogoutButtonClicked)
        self.projectsListWidget.currentRowChanged.connect(self.onProjectListWidgetCurrentRowChanged)
        self.validationSetsListWidget.currentRowChanged.connect(self.onValidationSetsListWidgetCurrentRowChanged)
        self.validationSetsLoadButton.clicked.connect(self.onValidationSetsLoadButtonClicked)
        self.boundaryStringsLoadButton.clicked.connect(self.onBoundaryStringsLoadButtonClicked)
        self.boundaryStringsUploadButton.clicked.connect(self.onBoundaryStringsUploadButtonClicked)
        self.projectsLoadBaseLayerButton.clicked.connect(self.onProjectsLoadBaseLayerButtonClicked)
        self.projectsBaseLayersComboBox.currentTextChanged.connect(self.onprojectsBaseLayersComboBoxChanged)
        # self.__setIcon(self.uploadButton, 'icon.png')

    def onLoginInputChanged(self, text: str) -> None:
        """Dummy, currently not used due to lack of authentication in its4land."""
        pass

    def onPasswordInputChanged(self, text: str) -> None:
        """Dummy, currently not used due to lack of authentication in its4land."""
        pass

    def onConnectButtonClicked(self) -> None:
        """Temporary replacement of onLoginButtonClicked."""
        self.onLoginButtonClicked()

    def onLoginButtonClicked(self) -> None:
        """Responsible for login of the user."""
        self.loginInput.setEnabled(False)
        self.passwordInput.setEnabled(False)
        self.loginButton.setEnabled(False)
        self.logoutButton.setEnabled(False)

        try:
            self.service.login(self.loginInput.text(), self.passwordInput.text())
        except Exception as e:
            msg = e.msg if 'msg' in e else __('Unable to login')

            utils.show_error(msg)

            self.loginInput.setEnabled(True)
            self.passwordInput.setEnabled(True)
            self.loginButton.setEnabled(True)
            self.passwordInput.setText('')
            raise e

        self.logoutButton.setEnabled(True)
        self.updateEnabledBoundaryStringButtons()

        try:
            projects = self.plugin.service.get_projects()
            self.setProjectsError('')
        except Its4landException as e:
            msg = __('[%s] %s' % (str(e.code), str(e)))
            self.setProjectsError(msg)
            utils.show_info(msg)
            return
        except Exception as e:
            self.setProjectsError(__('[%s] Error has occured!' % '???'))
            utils.show_info(str(e))
            return

        self.projectsGroupBox.setEnabled(True)

        assert projects.get('features'), 'Please contact its4land, there is "features" missing from ./projects'

        self.projects = projects['features']
        self.setProjects(self.projects)

    def onLogoutButtonClicked(self) -> None:
        self.logoutButton.setEnabled(False)
        self.loginInput.setText('')
        self.passwordInput.setText('')
        self.loginInput.setEnabled(True)
        self.passwordInput.setEnabled(True)
        self.loginButton.setEnabled(True)

        self.projectsListWidget.clear()
        self.validationSetsListWidget.clear()

        self.projectsGroupBox.setEnabled(False)
        self.validationSetsGroupBox.setEnabled(False)
        self.boundaryStringsGroupBox.setEnabled(False)

    def onProjectListWidgetCurrentRowChanged(self, index: int) -> None:
        assert self.projects

        try:
            if index >= 0:
                self.project = self.projects[index]
            else:
                self.project = None
        except ValueError:
            self.project = None
        except Exception as e:
            raise e
        try:
            self.projectsBaseLayersComboBox.clear()
            self.baseLayers = self.service.get_base_layers(self.project['properties']['UID'])
            self.setBaseLayers(self.project, self.baseLayers)
        except Its4landException as e:
            if e.code == 404:
                msg = __('No basemaps for this project')
            else:
                msg = str(__('[%s] %s' % (str(e.code), str(e))))

            self.setProjectsError(msg)
        except Exception as e:
            self.setValidationSetsError(__('[%s] Error has occured!' % '???'))
            utils.show_info(str(e))

        self._updateProjectDetails(self.project)
        self.validationSetsListWidget.clear()

        try:
            self.validationSets = self.service.get_validation_sets(self.project['properties']['UID'])
            self.setValidationSets(self.project, self.validationSets)

            self.setValidationSetsError('')
        except Its4landException as e:
            if e.code == 404:
                msg = __('No validation sets found for this project')
            else:
                msg = str(__('[%s] %s' % (str(e.code), str(e))))

            self.setValidationSetsError(msg)
        except Exception as e:
            self.setValidationSetsError(__('[%s] Error has occured!' % '???'))
            utils.show_info(str(e))
        finally:
            if self.project:
                self.validationSetsGroupBox.setEnabled(True)

        self.getAndUpdateDataForBoundaryStrings(self.project['properties']['UID'])

    def onValidationSetsListWidgetCurrentRowChanged(self, index: int) -> None:
        assert self.validationSets

        self.validationSetsLoadButton.setEnabled(False)
        self.contentItemFilename = None

        try:
            if index >= 0:
                self.validationSet = self.validationSets[index]
            else:
                self.validationSet = None
        except ValueError:
            self.validationSet = None
        except Exception as e:
            raise e

        if self.validationSet:
            try:
                self.contentItem = self.service.get_content_item(self.validationSet['ContentItem'])
                self.contentItem = self.contentItem[0] if len(self.contentItem) else None

                self.validationSetsLoadButton.setEnabled(True)
                self.setValidationSetsError('')
            except Its4landException as e:
                if e.code == 404:
                    msg = __('Validation set details failed to load')
                else:
                    msg = str(__('[%s] %s' % (str(e.code), str(e))))

                self.setValidationSetsError(msg)
                return
            except Exception as e:
                self.setValidationSetsError(str(e))
                raise e

        self._updateValidationSetDetails(self.validationSet, self.contentItem)

    def onValidationSetsLoadButtonClicked(self) -> None:
        assert self.contentItem
        assert self.contentItem['ContentID']

        self.contentItemFilename = tempfile.NamedTemporaryFile().name

        # TODO this lock is not working for some reason :/
        self.loginGroupBox.setEnabled(False)
        self.projectsGroupBox.setEnabled(False)
        self.validationSetsGroupBox.setEnabled(False)
        self.boundaryStringsGroupBox.setEnabled(False)

        try:
            self.service.download_content_item(self.contentItem['ContentID'], self.contentItemFilename)
            layer = QgsVectorLayer(self.contentItemFilename, self.validationSet['Name'], 'ogr')

            if layer.geometryType() != QgsWkbTypes.LineGeometry:
                utils.show_info(__('Validation set file is not with line geometries'))
                return

            utils.add_layer(layer, self.validationSet['Name'], parent=utils.get_group(), index=0)
            layer = self.plugin.setSegmentsLayer(layer, name=self.validationSet['Name'])

            if layer:
                # TODO the ugliest thing in the whole project
                self.plugin.dockWidget.segmentsLayerComboBox.setLayer(layer)
        except Its4landException as e:
            if e.code == 404:
                utils.show_info(__('Unable to load the selected validation set, check the web interface for more information'))
                return
            else:
                raise e
        finally:
            self.loginGroupBox.setEnabled(True)
            self.projectsGroupBox.setEnabled(True)
            self.validationSetsGroupBox.setEnabled(True)
            self.boundaryStringsGroupBox.setEnabled(True)

    def onBoundaryStringsLoadButtonClicked(self) -> None:
        assert self.boundaryStrings

        layer = utils.load_geojson(self.boundaryStrings, self.boundaryStrings['name'])

        if layer.geometryType() != QgsWkbTypes.LineGeometry:
            utils.show_info(__('Boundary face strings file is not with line geometries'))
            return

        utils.add_layer(layer, self.boundaryStrings['name'], parent=utils.get_group(), index=0)

    def _prepareBoundaryStringsGeojson(self, layer: QgsVectorLayer, project_id: str) -> dict:
        layer = utils.multipart_to_singleparts(layer)
        layer = utils.reproject(layer, 'EPSG:4326')
        geojson = utils.get_geojson(layer)

        counter = 1

        for f in geojson['features']:
            f['properties']['Projects'] = [{
                'UID': project_id
            }]
            # don't use double quotes, cause it is reserved for HL...
            f['properties']['Name'] = '%s__%s' % (project_id, counter)
            counter += 1

        geojson['crs']['properties']['name'] = HL_HARDCODED_PROJECTION
        geojson['name'] = 'BoundaryFaceString'

        return geojson

    def onBoundaryStringsUploadButtonClicked(self) -> None:
        assert self.project
        assert self.plugin.finalLayer

        geojson = self._prepareBoundaryStringsGeojson(self.plugin.finalLayer, self.project['properties']['UID'])

        self.boundaryStringsUploadButton.setEnabled(False)

        try:
            self.service.post_boundary_strings(geojson)
            utils.show_info(__('Successfully uploaded boundary face strings!'))

            self.getAndUpdateDataForBoundaryStrings(self.project['properties']['UID'])
        except Its4landException as e:
            utils.show_info(__('[%s] %s' % (str(e.code), str(e))))
            self.boundaryStringsUploadButton.setEnabled(True)
        except Exception as e:
            utils.show_info(__('[%s] Error has occured! %s' % ('???', str(e))))
            self.boundaryStringsUploadButton.setEnabled(True)

    def onProjectsLoadBaseLayerButtonClicked(self) -> None:
        quotedName = urllib.parse.quote(self.baseLayerRemoteName)

        # GOD PLEASE FORGIVE ME!
        # this is hardcoded in extremely ugly way, but there is anomaly in this orthoimage georefencing
        if quotedName == 'ETHOrthoImage':
            crs = 'EPSG:20137'
        else:
            crs = 'EPSG:4326'

        urlWithParams = 'crs=%s&dpiMode=7&format=image/png&layers=%s&styles&url=https://platform.its4land.com/DDI/wms' % (crs, quotedName)

        self.plugin.setBaseRasterLayer(urlWithParams, layer_type='wms')

    def onprojectsBaseLayersComboBoxChanged(self, layerName: str) -> None:
        if layerName:
            self.baseLayerRemoteName = layerName
            self.projectsLoadBaseLayerButton.setEnabled(True)
        else:
            self.projectsLoadBaseLayerButton.setEnabled(False)

    def updateEnabledBoundaryStringButtons(self) -> None:
        upload_enabled = bool(self.project and self.plugin.finalLayer)
        self.boundaryStringsUploadButton.setEnabled(upload_enabled)

    def setValidationSetsError(self, msg: str) -> None:
        self.validationSetsErrorLabel.setVisible(msg != '')
        self.validationSetsErrorLabel.setText(msg)

    def setBoundaryStringsError(self, msg: str) -> None:
        self.boundaryStringsErrorLabel.setVisible(msg != '')
        self.boundaryStringsErrorLabel.setText(msg)

    def setProjectsError(self, msg: str) -> None:
        self.projectsErrorLabel.setVisible(msg != '')
        self.projectsErrorLabel.setText(msg)

    def accept(self):
        self.close()
        self.hide()

    def reject(self):
        self.close()
        self.hide()

    def _updateProjectDetails(self, project: Optional[Dict[str, str]]):
        self.updateEnabledBoundaryStringButtons()

        if not project:
            self.projectsNameValueLabel.setText('')
            self.projectsDescriptionValueLabel.setText('')
            self.projectsModelsValueLabel.setText('')
            self.projectsSpatialSourcesValueLabel.setText('')
            self.projectsTagsValueLabel.setText('')
            self.projectsLoadBaseLayerButton.setEnabled(False)
            return

        self.projectsNameValueLabel.setText(project['properties']['Name'])
        self.projectsDescriptionValueLabel.setText(project['properties']['Description'])
        self.projectsModelsValueLabel.setText(str(len(project['properties']['Models'])))
        self.projectsSpatialSourcesValueLabel.setText(str(len(project['properties']['SpatialSources'])))
        self.projectsTagsValueLabel.setText(','.join(project['properties']['Tags']))

    def _updateValidationSetDetails(self, validationSet: Optional[Dict[str, str]], contentItem: Optional[Dict[str, str]]) -> None:
        self.updateEnabledBoundaryStringButtons()

        if validationSet:
            self.validationSetsNameValueLabel.setText(validationSet['Name'])
            self.validationSetsDescriptionValueLabel.setText(validationSet['Description'])
            self.validationSetsModelsValueLabel.setText(str(len(validationSet['Models'])))
            self.validationSetsTagsValueLabel.setText(','.join(validationSet['Tags']))

            if contentItem:
                self.validationSetsSizeValueLabel.setText(str(contentItem['ContentSize']))
        else:
            self.validationSetsNameValueLabel.setText('')
            self.validationSetsDescriptionValueLabel.setText('')
            self.validationSetsModelsValueLabel.setText('')
            self.validationSetsTagsValueLabel.setText('')
            self.validationSetsSizeValueLabel.setText('')

    def _updateBoundaryStringDetails(self, boundaryStrings: Optional[Dict[str, str]]) -> None:
        self.updateEnabledBoundaryStringButtons()

        if boundaryStrings:
            self.boundaryStringsNameValueLabel.setText(boundaryStrings['name'])
            self.boundaryStringsFeaturesValueLabel.setText(str(len(boundaryStrings['features'])))
            self.boundaryStringsSizeValueLabel.setText(str(utils.utf8len(json.dumps(self.boundaryStrings))))
        else:
            self.boundaryStringsNameValueLabel.setText('')
            self.boundaryStringsFeaturesValueLabel.setText('')
            self.boundaryStringsSizeValueLabel.setText('')

    def showEvent(self, event: QShowEvent) -> None:
        pass

    def setValidationSets(self, project, validation_sets) -> None:
        list_items = []

        for validation_set in validation_sets:
            assert validation_set.get('Name'), 'Please contact its4land, there is "Name" missing from a single validationSet'

            list_items.append(validation_set['Name'])

        self.validationSetsListWidget.clear()
        self.validationSetsListWidget.addItems(list_items)

    def setProjects(self, projects: List[Dict]) -> None:
        list_items = []

        for project in projects:
            assert project.get('properties')
            assert project['properties'].get('Name'), 'Please contact its4land, there is "Name" missing from a single Project'

            list_items.append(project['properties']['Name'])

        self.projectsListWidget.clear()
        self.projectsListWidget.addItems(list_items)

    def setBaseLayers(self, project: Dict, baseLayers: List):
        list_items = []

        for baseLayer in baseLayers:
            assert baseLayer.get('Name'), 'Please contact its4land, there is "Name" missing from a single baseLayer'

            list_items.append(baseLayer['Name'])

        self.projectsBaseLayersComboBox.clear()
        self.projectsBaseLayersComboBox.addItems(list_items)

    def getAndUpdateDataForBoundaryStrings(self, project_id: str) -> None:
        try:
            self.boundaryStrings = self.service.get_boundary_strings(project_id)
            self.setBoundaryStringsError('')
        except Its4landException as e:
            if e.code == 404:
                msg = __('No boundary strings found for this project')
            else:
                msg = str(__('[%s] %s' % (str(e.code), str(e))))

            self.boundaryStrings = None

            self.setBoundaryStringsError(msg)
        except Exception as e:
            self.boundaryStrings = None

            self.setBoundaryStringsError(__('[%s] Error has occured!' % '???'))
            utils.show_info(str(e))
        finally:
            self.boundaryStringsLoadButton.setEnabled(bool(self.boundaryStrings))
            self._updateBoundaryStringDetails(self.boundaryStrings)

            if self.project:
                self.boundaryStringsGroupBox.setEnabled(True)


import os
from flask import (
    Blueprint, json, make_response, jsonify, request, flash
)
from flaskr.auth import login_required
from flaskr.db_utils import *
from flask_cors import cross_origin

bp = Blueprint('profile', __name__, url_prefix='/profile')

@bp.route('/<user_id>', methods=["GET", "PUT", "POST"])
@cross_origin()
def features_list(user_id):
    print("User id ....", user_id)
    user_features = query_db('SELECT * FROM userprofile WHERE user_id = ?', (user_id,), True)
    error = None
    if request.method == "POST" and user_features is not None:
        error = "POST request not supported for existing user"
        return make_response({"error" : error}, 400)
    if request.method == 'POST' or request.method == 'PUT':
        data = request.get_json()
        alcohol_present = data["alcohol_present"]
        free_wifi_present = data["free_wifi_present"]
        accepts_credit_cards = data["accepts_credit_cards"]
        bike_parking  = data["bike_parking"]
        good_for_kids = data["good_for_kids"]
        restaurant_reservation = data["restaurant_reservation"]
        outdoor_seating = data["outdoor_seating"]
        smoking = data["smoking"]
        coat_check = data["coat_check"]
        price_range = data["price_range"]
        query = None
        print("coat_check val...", coat_check)
        if request.method == 'POST':
            query = "INSERT INTO userprofile (alcohol_present, free_wifi_present, accepts_credit_cards, bike_parking, good_for_kids, restaurant_reservation, outdoor_seating, smoking, coat_check, price_range, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        else:
            query = "UPDATE userprofile SET alcohol_present = ?, free_wifi_present = ?, accepts_credit_cards = ?, bike_parking = ?, good_for_kids = ?, restaurant_reservation = ?, outdoor_seating = ?, smoking = ?, coat_check = ?, price_range = ? WHERE user_id = ?"
        try:
            update_db(query, (alcohol_present, free_wifi_present, accepts_credit_cards, bike_parking, good_for_kids, restaurant_reservation, outdoor_seating, smoking, coat_check, price_range, user_id))
            user_features = query_db('SELECT * FROM userprofile WHERE user_id = ?', (user_id,), True)
            return make_response(jsonify(user_features), 200)
        except:
            error = "Db operation unsuccessful"
    if error:
        flash(error)
        return make_response({"error" : error}, 400)
    ## Code assumes that the errors have been handled and only get request handling will be left
    else:
        user_features = query_db('SELECT * FROM userprofile WHERE user_id = ?', (user_id,), True)
        if user_features is None:
            user_features = {}
        return make_response(jsonify(user_features), 201)


#! /usr/bin/python3
import sys

def run(ops):
    x = 1
    values = []
    for op in ops:
        values.append(x)
        if op is not None:
            values.append(x)
            x += op
    return [(t + 1, value) for t, value in enumerate(values)]

def render(values):
    grid = [[' '] * 40 for _ in range(6)]
    for cycle, value in values:
        row, col = divmod(cycle - 1, 40)
        if abs(value - col) <= 1:
            grid[row][col] = '█'
    return '\n'.join(''.join(row) for row in grid)

def main(input_file):
    ops = [
        None if 'noop' in line else int(line.split()[-1])
        for line in open(input_file)
    ]
    values = run(ops)
    print("Part 1:", sum(t * x for (t, x) in values[19::40]))
    print("Part 2:\n" + render(values))

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


import os

def create_sql_table(arg):
    table = open(os.path.join('sql', "schema_table.sql"), "a")
    schema_ = "CREATE TABLE IF NOT EXISTS {}(\
        date_time DATETIME, \
        valor INTEGER(11) NOT NULl, \
        operator VARCHAR(1));\n".format(arg)
    table.write(schema_)

def write_sql_insert(tb_name, name):
    table = open('sql/insert/'+name+'.sql', "w")
    schema_ = "INSERT IGNORE INTO {} (date_time, valor, operator) \
VALUES ('{}','{}')".format(tb_name, {}, {})
    table.write(schema_)

def write_sql_search(tb_name):
    table = open(os.path.join('sql/search/', 'SEARCH_'+tb_name+'.sql'), "w")
    schema_ = "SELECT * FROM database_g4.{} WHERE date_time\
BETWEEN ('{}') AND ('{}');".format(tb_name, {}, {})
    table.write(schema_)

def write():
    for file in os.listdir('data_csv'):
        nome_csv = os.path.basename(file)[:-4]
        name = nome_csv.replace(" ", "_")
        write_sql_table(name)
        write_sql_insert(name, nome_csv)
        write_sql_search(name)


import orbit
import tensorflow as tf

from official import vision  # pylint: disable=unused-import
from official.core import exp_factory
from official.modeling import optimization
from official.projects.deepmac_maskrcnn.tasks import deep_mask_head_rcnn

class DeepMaskHeadRCNNTaskTest(tf.test.TestCase):

  def _edit_config_for_testing(self, config):
    # modify config to suit local testing
    config.trainer.steps_per_loop = 1
    config.task.train_data.global_batch_size = 2
    config.task.model.backbone.resnet.model_id = 18
    config.task.model.decoder.fpn.num_filters = 32
    config.task.model.detection_generator.pre_nms_top_k = 500
    config.task.model.detection_head.fc_dims = 128
    if config.task.model.include_mask:
      config.task.model.mask_sampler.num_sampled_masks = 10
      config.task.model.mask_head.num_convs = 1
    config.task.model.roi_generator.num_proposals = 100
    config.task.model.roi_generator.pre_nms_top_k = 150
    config.task.model.roi_generator.test_pre_nms_top_k = 150
    config.task.model.roi_generator.test_num_proposals = 100
    config.task.model.rpn_head.num_filters = 32
    config.task.model.roi_sampler.num_sampled_rois = 200
    config.task.model.input_size = [128, 128, 3]
    config.trainer.train_steps = 2
    config.task.train_data.shuffle_buffer_size = 2
    config.task.train_data.input_path = "/readahead/200M/placer/prod/home/tensorflow-performance-data/datasets/coco/train-00000-of-00256.tfrecord"
    config.task.validation_data.global_batch_size = 2
    config.task.validation_data.input_path = "/readahead/200M/placer/prod/home/tensorflow-performance-data/datasets/coco/val-00000-of-00032.tfrecord"

  def _build_deep_mask_head_rcnn_from_config(self, config, is_training):
    task = deep_mask_head_rcnn.DeepMaskHeadRCNNTask(config.task)
    model = task.build_model()
    metrics = task.build_metrics(training=is_training)

    strategy = tf.distribute.get_strategy()

    data_config = config.task.train_data if is_training else config.task.validation_data
    dataset = orbit.utils.make_distributed_dataset(strategy, task.build_inputs,
                                                   data_config)
    iterator = iter(dataset)
    opt_factory = optimization.OptimizerFactory(config.trainer.optimizer_config)
    optimizer = opt_factory.build_optimizer(opt_factory.build_learning_rate())

    return task, iterator, model, optimizer, metrics

  def test_freeze_backbone_number_of_trainable_weights(self):
    config = exp_factory.get_exp_config("deep_mask_head_rcnn_resnetfpn_coco")
    self._edit_config_for_testing(config)

    # Get the number of weights from a regular model
    (task, iterator, model,
     optimizer, metrics) = self._build_deep_mask_head_rcnn_from_config(
         config, True)

    task.train_step(next(iterator), model, optimizer, metrics=metrics)
    default_weights = sum(len(l.trainable_weights) for l in model.layers)

    # Get the number of weights from a model with backbone frozen
    config.task.freeze_backbone = True
    (task, iterator, model,
     optimizer, metrics) = self._build_deep_mask_head_rcnn_from_config(
         config, True)

    task.train_step(next(iterator), model, optimizer, metrics=metrics)
    frozen_weights = sum(len(l.trainable_weights) for l in model.layers)

    # Make sure that the frozen model has less number of trainable weights
    self.assertLess(frozen_weights, default_weights,
                    msg="A frozen backbone should have fewer trainable weights")

  def test_freeze_backbone(self):
    config = exp_factory.get_exp_config("deep_mask_head_rcnn_resnetfpn_coco")
    self._edit_config_for_testing(config)
    config.task.freeze_backbone = True

    (task, iterator, model,
     optimizer, metrics) = self._build_deep_mask_head_rcnn_from_config(
         config, True)

    weights_before = [
        w.numpy().copy() for w in model.backbone.weights]

    self.assertNotEmpty(weights_before)
    task.train_step(next(iterator), model, optimizer, metrics=metrics)

    weights_after = [
        w.numpy().copy() for w in model.backbone.weights]

    self.assertAllClose(weights_after, weights_before,
                        msg="Backbone weights should not change when frozen")

if __name__ == "__main__":
  tf.test.main()


from setuptools import setup, find_packages

with open('README.md') as f:
	readme = f.read()

with open('LICENSE') as f:
	license = f.read()

setup(
	name='sickninja',
	use_scm_version=True,
	setup_requires=['setuptools_scm'],
	description='Stylesheet generator that uses sass and jinja2',
	long_description=readme,
	author='leaty',
	author_email='dev@leaty.net',
	url='https://github.com/leaty/sickninja',
	license=license,
	packages=find_packages(),
	scripts=['bin/sickninja']
	#entry_points = {
	#	'console_scripts': ['sickninja=sickninja.__main__:main']
	#}
)


import spacy
from nltk import Tree
from nltk.tokenize.treebank import TreebankWordDetokenizer
nlp = spacy.load('en_core_web_sm')

def to_nltk_tree(node):
    if node.n_lefts + node.n_rights > 0:
        return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])
    else:
        return node.orth_

def name_find(node, list):
    if node.n_lefts + node.n_rights > 0:
        list.append(node.orth_)
        return [name_find(child, list) for child in node.children]
    else:
        list.append(node.orth_)

def add_not(sent):
        tokens = [token for token in sent]
        real_tokens = [token.text for token in tokens]
        for word in sent:
            if word.dep_ == "ROOT":
                vb_doc = word
                vb = word.text
                vb_is_be = word.lemma_
        start = real_tokens[0]
        if start in ['has', 'have'] and 'ever' in real_tokens:
            ever_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == 'ever':
                    ever_index = index
                    break
            follow_question = real_tokens[1:ever_index]
            follow_question.extend([start, 'not', 'ever'])
            if ever_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[ever_index+1:])
            return follow_question, False
        if start in ['is', 'are', 'was', 'were'] and 'based' in real_tokens:
            vb_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == 'based':
                    vb_index = index
                    break
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', 'based'])
            if vb_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[vb_index+1:])
            return follow_question, False
        if start in ['is', 'are', 'was', 'were'] and 'filmed' in real_tokens:
            vb_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == 'filmed':
                    vb_index = index
                    break
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', 'filmed'])
            if vb_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[vb_index+1:])
            return follow_question, False
        if start in ['is', 'are', 'was', 'were'] and ' part of ' in TreebankWordDetokenizer().detokenize(real_tokens):
            vb_index = 0
            for index in range(len(real_tokens)-1):
                if real_tokens[index] == 'part' and real_tokens[index+1] == 'of':
                    vb_index = index
                    break
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', 'part', 'of'])
            if vb_index != len(real_tokens) - 2:
                follow_question.extend(real_tokens[vb_index+2:])
            return follow_question, False
        if start in ['is', 'are', 'was', 'were'] and " the same" in TreebankWordDetokenizer().detokenize(real_tokens):
            vb_index = 0
            for index in range(len(real_tokens)-1):
                if real_tokens[index] == 'the' and real_tokens[index+1] == 'same':
                    vb_index = index
                    break
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', 'the', 'same'])
            if vb_index != len(real_tokens) - 2:
                follow_question.extend(real_tokens[vb_index+2:])
            return follow_question, False
        if start in ['is', 'are', 'was', 'were'] and "still" in real_tokens:
            vb_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == 'still':
                    vb_index = index
                    break
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', 'still'])
            if vb_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[vb_index+1:])
            return follow_question, False
        if start in ["is", "are", "was", "were"] and vb_is_be != "be":
            vb_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == vb:
                    vb_index = index
                    break
            if vb_index in [0, 1]:
                return real_tokens, False
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', vb])
            if vb_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[vb_index+1:])
            return follow_question, False
        if start in ["is", "are", "was", "were"] and vb_is_be == "be":
            len_child = 0
            name = tokens[0]
            for child in vb_doc.children:
                len_child += 1
            if len_child == 1:
                return real_tokens, False
            for child in vb_doc.children:
                name = child
                break
            if name == tokens[0]:
                return real_tokens, False
            name_list = []
            name_find(name, name_list)
            part_1 = real_tokens[1:len(name_list)+1]  ######
            part_2 = real_tokens[len(name_list)+1:]
            follow_question = part_1
            follow_question.extend([start, 'not'])
            follow_question.extend(part_2)
            return follow_question, False
        if start in ["do", "did", "does", "have", "has", "can", "could", "will", "would", 'must', 'might', 'should']:
            vb_index = 0
            for index in range(len(real_tokens)):
                if real_tokens[index] == vb:
                    vb_index = index
                    break
            if vb_index in [0, 1]:
                vb_index = -1
                pos = [token.pos_ for token in tokens]
                for index in range(len(pos)):
                    if index in [0, 1]:
                        continue
                    if pos[index] in ['VERB', 'AUX']:
                        vb_index = index
                if vb_index == -1:
                    return real_tokens, False
            if real_tokens[vb_index-1] == 'be':
                vb_index = vb_index - 1
                vb = 'be'
                follow_question = real_tokens[1:vb_index]
                follow_question.extend([start, 'not', vb])
                if vb_index != len(real_tokens) - 1:
                    follow_question.extend(real_tokens[vb_index + 1:])
                return follow_question, False
            follow_question = real_tokens[1:vb_index]
            follow_question.extend([start, 'not', vb])
            if vb_index != len(real_tokens) - 1:
                follow_question.extend(real_tokens[vb_index + 1:])
            return follow_question, False

class MR1_4(object):

    def generate(self, question, article):
        doc = nlp(question)
        tokens = [token for token in doc]
        real_tokens = [token.text for token in tokens]
        this_follow_question, if_out = add_not(doc)
        if "all" in real_tokens:
            return question, article, False
        elif "every" in real_tokens:
            return question, article, False
        if this_follow_question == real_tokens:
            return question, article, False
        else:
            this_follow_question.extend([',', 'is', 'it', 'right'])
            follow_question = TreebankWordDetokenizer().detokenize(this_follow_question)
            return follow_question, article, True


import os
from tqdm import tqdm
from acdh_cidoc_pyutils import (
    make_birth_death_entities,
    make_entity_label
)
from utils.utilities import (
    make_e42_identifiers_utils,
    create_triple_from_node,
    create_birth_death_settlement_graph
)
from acdh_cidoc_pyutils.namespaces import CIDOC, FRBROO
from acdh_tei_pyutils.tei import TeiReader
from rdflib import Graph, Namespace, URIRef, plugin, ConjunctiveGraph, Literal
from rdflib.namespace import RDF, RDFS
from rdflib.store import Store

FA = Namespace("https://sk.acdh.oeaw.ac.at/project/fackel")

store = plugin.get("Memory", Store)()
project_store = plugin.get("Memory", Store)()

if os.environ.get("NO_LIMIT"):
    LIMIT = False
    print("no limit")
else:
    LIMIT = 1000

rdf_dir = "./rdf"
os.makedirs(rdf_dir, exist_ok=True)
domain = "https://sk.acdh.oeaw.ac.at/"
SK = Namespace(domain)

project_uri = URIRef(f"{SK}project/fackel")
g = Graph(identifier=project_uri, store=project_store)
g.bind("cidoc", CIDOC)
g.bind("frbroo", FRBROO)
g.bind("sk", SK)

entity_type = "person"
index_file = f"./data/indices/list{entity_type}.xml"
doc = TeiReader(index_file)
nsmap = doc.nsmap
items = doc.any_xpath(f".//tei:{entity_type}")
if LIMIT:
    items = items[:LIMIT]

print(f"converting {entity_type}s derived from {index_file}")
for x in tqdm(items, total=len(items)):
    xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"]
    item_id = f"{SK}{xml_id}"
    subj = URIRef(item_id)
    name_node = x.xpath(".//tei:persName", namespaces=nsmap)
    item_label = make_entity_label(name_node[0])[0]
    try:
        item_type = x.attrib["type"]
    except KeyError:
        item_type = False
    if item_type and item_type == "group":
        g.add((subj, RDF.type, CIDOC["E74_Group"]))
    else:
        g.add((subj, RDF.type, CIDOC["E21_Person"]))
    g.add((subj, RDFS.label, Literal(item_label, lang="und")))
    g += make_e42_identifiers_utils(
        subj, x, type_domain=f"{SK}types", default_lang="en", same_as=False
    )
    # g += make_appellations(subj, x, type_domain=f"{SK}types", default_lang="und")
    # create appellations
    g += create_triple_from_node(
        node=x,
        subj=subj,
        subj_suffix="appellation",
        pred=CIDOC["P2_has_type"],
        sbj_class=CIDOC["E33_E41_Linguistic_Appellation"],
        obj_class=CIDOC["E55_Type"],
        obj_node_xpath="./tei:persName[@type='sk']",
        obj_node_value_xpath="./@subtype",
        obj_node_value_alt_xpath_or_str="pref",
        obj_prefix=f"{SK}types",
        default_lang="und",
        value_literal=True,
        identifier=CIDOC["P1_is_identified_by"],
        special_sorting=True,
    )
    # add additional type for appellations
    g += create_triple_from_node(
        node=x,
        subj=subj,
        subj_suffix="appellation",
        sbj_class=CIDOC["E33_E41_Linguistic_Appellation"],
        pred=CIDOC["P2_has_type"],
        default_lang="und",
        obj_class=CIDOC["E55_Type"],
        obj_node_xpath="./tei:persName",
        obj_node_value_xpath="./@sex",
        obj_node_value_alt_xpath_or_str="./parent::tei:person/tei:sex/@value",
        obj_prefix=f"{SK}types",
        skip_value="not-set",
        identifier=CIDOC["P1_is_identified_by"],
        special_sorting=True,
    )
    # g += make_occupations(subj, x, default_lang="de")[0]
    # add occupations
    g += create_triple_from_node(
        node=x,
        subj=subj,
        subj_suffix="occupation",
        pred=CIDOC["None"],
        sbj_class=FRBROO["F51_Pursuit"],
        obj_node_xpath="./tei:occupation",
        obj_process_condition="./@type='sk'",
        default_lang="en",
        label_prefix="works for: ",
        identifier=CIDOC["P14i_performed"]
    )
    # create birth and death
    if x.xpath("./tei:birth", namespaces=nsmap):
        date_test = x.xpath("./tei:birth/@when", namespaces=nsmap)[0]
        if date_test != "leer":
            date_test = True,
        else:
            date_test = False
        if date_test:
            try:
                date_node = x.xpath("./tei:birth[@type]", namespaces=nsmap)[0]
            except IndexError:
                date_node = None
            if date_node is not None:
                date_type = date_node.attrib["type"]
                if date_type == "approx":
                    date_type_uri = URIRef(f"{SK}types/date/{date_type}")
                else:
                    date_type_uri = False
            else:
                date_type_uri = False
            birth_g, birth_uri, birth_timestamp = make_birth_death_entities(
                subj,
                x,
                domain=SK,
                event_type="birth",
                type_uri=date_type_uri,
                verbose=False,
                default_prefix="Birth of",
                default_lang="en",
                date_node_xpath="/self::tei:birth",
                place_id_xpath="//tei:placeName[1]/@key",
            )
            g += birth_g
    if x.xpath("./tei:birth[./tei:placeName]", namespaces=nsmap):
        try:
            birth_place_node = x.xpath(
                "./tei:birth/tei:placeName", namespaces=nsmap
            )[0]
        except IndexError:
            birth_place_node = None
        if birth_place_node is not None:
            g += create_birth_death_settlement_graph(
                node=birth_place_node,
                namespaces=nsmap,
                uri_prefix=SK,
                node_attrib="key"
            )
    if x.xpath("./tei:death", namespaces=nsmap):
        date_test = x.xpath("./tei:death/@when", namespaces=nsmap)[0]
        if date_test != "leer":
            date_test = True,
        else:
            date_test = False
        if date_test:
            try:
                date_node = x.xpath("./tei:death[@type]", namespaces=nsmap)[0]
            except IndexError:
                date_node = None
            if date_node is not None:
                date_type = date_node.attrib["type"]
                if date_type == "approx":
                    date_type_uri = URIRef(f"{SK}types/date/{date_type}")
                else:
                    date_type_uri = False
            else:
                date_type_uri = False
            death_g, death_uri, death_timestamp = make_birth_death_entities(
                subj,
                x,
                domain=SK,
                event_type="death",
                type_uri=date_type_uri,
                verbose=False,
                default_prefix="Death of",
                default_lang="en",
                date_node_xpath="/self::tei:death",
                place_id_xpath="//tei:placeName[1]/@key",
            )
            g += death_g
    if x.xpath("./tei:death[./tei:placeName]", namespaces=nsmap):
        try:
            death_place_node = x.xpath(
                "./tei:death/tei:placeName", namespaces=nsmap
            )[0]
        except IndexError:
            death_place_node = None
        if death_place_node is not None:
            g += create_birth_death_settlement_graph(
                node=death_place_node,
                namespaces=nsmap,
                uri_prefix=SK,
                node_attrib="key"
            )

entity_type = "place"
index_file = f"./data/indices/list{entity_type}.xml"
doc = TeiReader(index_file)
nsmap = doc.nsmap
items = doc.any_xpath(f".//tei:{entity_type}")
if LIMIT:
    items = items[:LIMIT]

print(f"converting {entity_type}s derived from {index_file}")
for x in tqdm(items, total=len(items)):
    xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"]
    item_id = f"{SK}{xml_id}"
    subj = URIRef(item_id)
    name_node = x.xpath(".//tei:placeName", namespaces=nsmap)
    item_label = make_entity_label(name_node[0])[0]
    g.add((subj, RDF.type, CIDOC["E53_Place"]))
    g.add((subj, RDFS.label, Literal(item_label, lang="und")))
    g += make_e42_identifiers_utils(subj, x, type_domain=f"{SK}types", default_lang="en", same_as=False)
    # g += make_appellations(subj, x, type_domain=f"{SK}types", default_lang="und")
    # create appellations
    g += create_triple_from_node(
        node=x,
        subj=subj,
        subj_suffix="appellation",
        pred=CIDOC["P2_has_type"],
        sbj_class=CIDOC["E33_E41_Linguistic_Appellation"],
        obj_class=CIDOC["E55_Type"],
        obj_node_xpath="./tei:placeName",
        obj_node_value_xpath="./@subtype",
        obj_node_value_alt_xpath_or_str="pref",
        obj_prefix=f"{SK}types",
        default_lang="und",
        value_literal=True,
        identifier=CIDOC["P1_is_identified_by"]
    )

print("writing graph to file: data.trig and .ttl")
g_all = ConjunctiveGraph(store=project_store)
g_all.serialize(f"{rdf_dir}/data.trig", format="trig")
g_all.serialize(f"{rdf_dir}/data.ttl", format="ttl")


import contextlib
import logging
import sys
import tempfile
import unittest
import warnings
from contextlib import redirect_stderr
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Dict, Optional

LOGGER_STR = "logger_str"

LEVEL_NAME = "level_name"

PARENT = "parent"

PROPAGATE = "propagate"

DISABLED = "disabled"

FILTERS = "filters"

LOGGER_NAME = "name"

HANDLERS = "handlers"

LEVEL = "level"

OBJECT = "object"

LUIGI_INTERFACE_LOGGER = "luigi-interface"

LUIGI_LOGGER = "luigi"

API_CLIENT_LOGGING_TEST_LOGGER = "APIClientLoggingTest"

ROOT_LOGGER = "root"

TEST_FORMAT = "TEST_FORMAT"

from exasol_integration_test_docker_environment.lib.api.common import generate_root_task, run_task, \
    set_build_config
from exasol_integration_test_docker_environment.lib.base.dependency_logger_base_task import \
    DependencyLoggerBaseTask

@contextlib.contextmanager
def ignore_resource_warning():
    """
    Ignore ResourceWarning to keep the captured output clean for the asserts
    """
    with warnings.catch_warnings():
        warnings.filterwarnings(action="ignore",
                                category=ResourceWarning)
        yield

class DummyTask(DependencyLoggerBaseTask):

    def run_task(self):
        self.logger.info("DUMMY LOGGER INFO")
        self.logger.error("DUMMY LOGGER ERROR")
        self.return_object("DUMMY SUCCES")

@contextlib.contextmanager
def catch_stderr():
    with tempfile.TemporaryFile("w+t") as temp_file:
        try:
            with ignore_resource_warning():
                with redirect_stderr(temp_file) as catched_stderr:
                    yield catched_stderr
        finally:
            temp_file.seek(0)
            print(temp_file.read(), file=sys.stderr)

class APIClientLoggingTest(unittest.TestCase):

    def setUp(self):
        self.old_max_length = unittest.util._MAX_LENGTH
        unittest.util._MAX_LENGTH = 20000
        print(f"SetUp {self.__class__.__name__}", file=sys.stderr)
        self._build_output_temp_dir = TemporaryDirectory()
        self._build_output_temp_dir.__enter__()
        self.reset_logging()

    def tearDown(self):
        self._build_output_temp_dir.__exit__(None, None, None)
        self.reset_logging()
        unittest.util._MAX_LENGTH = self.old_max_length

    @ignore_resource_warning()
    def dummy_api_command(self, log_level: Optional[str], use_job_specific_log_file: bool):
        set_build_config(False,
                         tuple(),
                         False,
                         False,
                         self._build_output_temp_dir.name,
                         "/tmp",
                         "",
                         "test")
        task_creator = lambda: generate_root_task(task_class=DummyTask)
        result = run_task(task_creator=task_creator,
                          workers=2,
                          task_dependencies_dot_file=None,
                          log_level=log_level,
                          use_job_specific_log_file=use_job_specific_log_file)
        return result

    def configure_logging(self, log_level: int):
        logging.basicConfig(
            format=f'{TEST_FORMAT} %(levelname)s %(message)s',
            level=log_level,
            force=True)

    def assert_loggers_are_equal(self, logger_infos_after, logger_infos_before):
        self.assertEqual(logger_infos_before[ROOT_LOGGER], logger_infos_after[ROOT_LOGGER])
        self.assertEqual(logger_infos_before[API_CLIENT_LOGGING_TEST_LOGGER],
                         logger_infos_after[API_CLIENT_LOGGING_TEST_LOGGER])
        self.assertEqual(logger_infos_before[LUIGI_LOGGER], logger_infos_after[LUIGI_LOGGER])
        self.assertEqual(logger_infos_before[LUIGI_INTERFACE_LOGGER], logger_infos_after[LUIGI_INTERFACE_LOGGER])

    def create_test_regex(self, log_level: int):
        level_name = logging.getLevelName(log_level)
        return f".*{TEST_FORMAT} {level_name} DummyTask_.* DUMMY LOGGER {level_name}.*"

    def test_luigi_log_level_info_and_basic_logging_error(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.ERROR)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level="INFO", use_job_specific_log_file=False)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assertNotEqual(catched_stderr, "")
            self.assertRegex(stderr_output, self.create_test_regex(logging.ERROR))
            self.assertRegex(stderr_output, self.create_test_regex(logging.INFO))
            self.assertRegex(stderr_output, ".*===== Luigi Execution Summary =====.*")
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertEqual(main_log_glob, [])

    def test_luigi_log_level_error_and_basic_logging_info(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.INFO)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level="ERROR", use_job_specific_log_file=False)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assertNotEqual(catched_stderr, "")
            self.assertRegex(stderr_output, self.create_test_regex(logging.ERROR))
            self.assertNotRegex(stderr_output, self.create_test_regex(logging.INFO))
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertEqual(main_log_glob, [])

    def test_luigi_log_level_error_multiple_calls_and_basic_logging_info(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.INFO)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level="ERROR", use_job_specific_log_file=False)
            result = self.dummy_api_command(log_level="ERROR", use_job_specific_log_file=False)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assertNotEqual(catched_stderr, "")
            self.assertRegex(stderr_output, self.create_test_regex(logging.ERROR))
            self.assertNotRegex(stderr_output, self.create_test_regex(logging.INFO))
            self.assertEqual(2, stderr_output.count("DUMMY LOGGER ERROR"))
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertEqual(main_log_glob, [])

    def test_luigi_use_job_specific_log_file_and_basic_logging_error(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.ERROR)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level=None, use_job_specific_log_file=True)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            self.assertEqual(stderr_output, "")
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertNotEqual(main_log_glob, [])

    def test_luigi_no_log_config_and_basic_logging_info(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.INFO)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level=None, use_job_specific_log_file=False)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assertRegex(stderr_output, self.create_test_regex(logging.ERROR))
            self.assertRegex(stderr_output, self.create_test_regex(logging.INFO))
            self.assertRegex(stderr_output, ".*===== Luigi Execution Summary =====.*")
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertEqual(main_log_glob, [])

    def test_luigi_no_log_config_and_basic_logging_error(self):
        with catch_stderr() as catched_stderr:
            self.configure_logging(log_level=logging.ERROR)
            logger_infos_before = self.create_logger_infos()
            result = self.dummy_api_command(log_level=None, use_job_specific_log_file=False)
            logger_infos_after = self.create_logger_infos()
            self.assert_loggers_are_equal(logger_infos_after, logger_infos_before)
            catched_stderr.seek(0)
            stderr_output = catched_stderr.read()
            self.assertNotEqual(stderr_output, "")
            self.assertRegex(stderr_output, self.create_test_regex(logging.ERROR))
            self.assertNotRegex(stderr_output, self.create_test_regex(logging.INFO))
            main_log_glob = list(Path(self._build_output_temp_dir.name).glob("**/main.log"))
            self.assertEqual(main_log_glob, [])

    def reset_logging(self):
        logging.basicConfig(
            format='%(asctime)s %(levelname)-8s %(message)s',
            level=logging.ERROR,
            datefmt='%Y-%m-%d %H:%M:%S',
            force=True)
        loggerDict = logging.root.manager.loggerDict
        for key in list(loggerDict.keys()):
            loggerDict[key].disabled = True
            del loggerDict[key]

    def create_logger_infos(self) -> Dict[str, Dict[str, Any]]:
        logger_infos = {
            ROOT_LOGGER: self.get_logger_info(logging.root),
            API_CLIENT_LOGGING_TEST_LOGGER: self.get_logger_info(logging.getLogger("APIClientLoggingTest")),
            LUIGI_LOGGER: self.get_logger_info(logging.getLogger("luigi")),
            LUIGI_INTERFACE_LOGGER: self.get_logger_info(logging.getLogger("luigi-interface"))
        }
        return logger_infos

    def get_logger_info(self, logger: logging.Logger) -> Dict[str, Any]:
        logger_info = {}
        logger_info[LOGGER_STR] = str(logger)
        logger_info[LEVEL] = logger.level
        logger_info[LEVEL_NAME] = logging.getLevelName(logger.level)
        logger_info[HANDLERS] = list(logger.handlers)
        logger_info[LOGGER_NAME] = logger.name
        logger_info[FILTERS] = list(logger.filters)
        logger_info[DISABLED] = logger.disabled
        logger_info[PROPAGATE] = logger.propagate
        logger_info[PARENT] = logger.parent
        return logger_info

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


import datetime
import os
import numpy as np

class FileWriter:
	'''
	This class is to write the predicted results to a file.
	We shall maintain separate folders for each course.
	'''

	def __init__(self,course_code):
		self.root_folder = 'Attendance'
		self.course_code = course_code
		self.curr_date = datetime.datetime.now().strftime ("%d-%m-%Y")
		self.file_path = self.root_folder + '/' + self.course_code + '/' + self.curr_date + '.txt'

	def saveToFile(self,predictions):
		'''
		This module is to write the predictions to a file.

		#Parameters: 'predictions' is the list of predictions by the classifier.
		#Return: None.
		'''
		directory = os.path.dirname(self.file_path)
		if not os.path.exists(directory):
			os.makedirs(directory)
		predictions = np.asarray(predictions)
		predictions = np.sort(predictions)
		predictions = set(predictions)
		f = open(self.file_path, 'w')
		for item in predictions:
			f.write(str(item) + '\n')
		f.close()


import pandas as pd
import analysis
import machine_learning
import visualisation

def load_data(file_path):
    """
    Load the data from a CSV file.

    Args:
        file_path (str): Path to CSV data file.

    Returns:
        pd.DataFrame: Loaded DataFrame.
    """
    df = pd.read_csv(file_path)
    return df

def main():
    # Load the data
    data_file = 'sakura_first_bloom_dates.csv'
    df = load_data(data_file)

    # Reshape (melt!) and clean data
    df_cleaned = analysis.clean_data(df)
    df_melted = analysis.reshape_data(df_cleaned)

    # Perform analysis
    analysis.perform_analysis(df_melted)

    # Run basic linear regression
    machine_learning.run_machine_learning(df_cleaned)

    # Generate Sapporo visualizations
    visualisation.generate_visualizations(df_melted)

if __name__ == "__main__":
    main()


def part(pattern):
    """
    find suffix and prefix match table for pattern
    :param pattern:
    :return: table
    """
    table = [0]

    for i in range(1, len(pattern)):
        j = table[i-1]
        while j > 0 and pattern[j] != pattern[i]:
            j = table[j-1]
        table.append(j+1 if pattern[j] == pattern[i] else j)

    return table

def kmp(text, pattern):
    """
    kmp pattern matching algorithm
    :param text:
    :param pattern:
    :return: index of first occurrence of pattern in text
    """
    if len(pattern) > len(text):
        return -1
    else:
        pattern = pattern.lower()
        text = text.lower()
        table = part(pattern)
        j = 0

        for i in range(len(text)):
            if text[i] == pattern[j]:
                if j == len(pattern)-1:
                    return i - len(pattern) + 1
                else:
                    j = j+1
            else:   # text[i] != pattern[j]
                j = table[j-1]
                i = i + len(pattern) - j

        return -1


#PrivateAssistant_v1.py
import os
import argparse
import logging
import time
from io import DEFAULT_BUFFER_SIZE

import grpc
import vito_stt_client_pb2 as pb
import vito_stt_client_pb2_grpc as pb_grpc
from requests import Session
import pyaudio

if not os.path.exists('Privates.py'):
    CLIENT_ID = input("Enter your Vito API client ID: ")
    CLIENT_SECRET = input("Enter your Vito API client secret: ")
    api_key = input("Enter your OpenAI API key: ")
    with open('Privates.py', 'w') as f:
        f.write('#Vito APIs\n')
        f.write('CLIENT_ID="' + CLIENT_ID + '"\n')
        f.write('CLIENT_SECRET="' + CLIENT_SECRET + '"\n\n')
        f.write('#Openai APIs\n')
        f.write('api_key="' + api_key + '"\n')
import Privates
import openai

import pyttsx3

import TextWindow

API_BASE = "https://openapi.vito.ai"
GRPC_SERVER_URL = "grpc-openapi.vito.ai:443"
CLIENT_ID = Privates.CLIENT_ID
CLIENT_SECRET = Privates.CLIENT_SECRET

SAMPLE_RATE = 8000
ENCODING = pb.DecoderConfig.AudioEncoding.LINEAR16

class STT:
    def __init__(self, client_id, client_secret):
        print("Initializing Vito..")
        self._logger = logging.getLogger(__name__)
        self.client_id = client_id
        self.client_secret = client_secret
        self._sess = Session()
        self._token = None
        print("Done!")

    @property
    def token(self):
        if self._token is None or self._token["expire_at"] < time.time():
            resp = self._sess.post(
                API_BASE + "/v1/authenticate",
                data={"client_id": self.client_id, "client_secret": self.client_secret},
            )
            resp.raise_for_status()
            self._token = resp.json()
        return self._token["access_token"]

    def transcribe_streaming_grpc(self, stream, config):
        base = GRPC_SERVER_URL
        with grpc.secure_channel(
            base, credentials=grpc.ssl_channel_credentials(), options=[('grpc.keepalive_time_ms', 10000)]
        ) as channel:
            stub = pb_grpc.OnlineDecoderStub(channel)
            cred = grpc.access_token_call_credentials(self.token)

            def req_iterator():
                yield pb.DecoderRequest(streaming_config=config)

                # Initialize PyAudio
                p = pyaudio.PyAudio()
                stream = p.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True,
                                frames_per_buffer=DEFAULT_BUFFER_SIZE)

                while True:
                    buff = stream.read(DEFAULT_BUFFER_SIZE)
                    if len(buff) == 0:
                        break
                    else:
                        yield pb.DecoderRequest(audio_content=buff)

                # Stop the stream
                stream.stop_stream()
                stream.close()
                p.terminate()

            req_iter = req_iterator()
            resp_iter = stub.Decode(req_iter, credentials=cred)
            for resp in resp_iter:
                resp: pb.DecoderResponse
                for res in resp.results:
                    if res.is_final:
                        request = res.alternatives[0].text
                        if(request != ""):
                            print(request)
                            main.executeCommand(request)
                            return

openai.api_key = Privates.api_key
messages=[]

# Please input the names to be used for calling this private assistant. Multiple names are possible.
systemName = ["헤이", "어이"]

# Please input your own GPT prompts, as this will affect all of the responses generated by the GPT.
systemRole = "간결한 대답."

# Please input your own words that you do not want GPT to respond to and execute custom commands
myCommands = [
        "창모드",
        "음소거",
        "계속 말할게",
        "그만 말할게",
        "날씨"
        ]

class main:
    isProcessing = False
    nameCalled = False
    windowMode = False
    mute = False

    def __init__(self):
        print("Initializing gpt..")
        messages.append({"role" : "system", "content" : f"{systemRole}"})
        openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
        print("Done!")

    @staticmethod
    def bCheck_substrings(string, substrings):
        for substring in substrings:
            if substring in string:
                return True
        return False

    @staticmethod
    def check_substrings(string, substrings):
        for substring in substrings:
            if substring in string:
                return substring
        return None

    @staticmethod
    def executeCommand(request):
        main.isProcessing = True

        if not main.nameCalled:
            main.nameCalled = main.bCheck_substrings(request, systemName)
            if main.nameCalled:
                TTS.tts("무엇을 도와드릴까요?")
            main.isProcessing = False
            return

        ownRequest = main.check_substrings(request, myCommands)
        if ownRequest is not None:
            # Make your own processes
            print(ownRequest)
            CustomCommand(ownRequest)
        else: # Run ChatGPT
            user_content = request
            # print("user : " + user_content)
            messages.append({"role" : "user", "content" : f"{user_content}"})
            response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
            assistant_content = response["choices"][0]["message"]['content'].strip()
            messages.append({"role" : "assistant", "content" : f"{assistant_content}"})
            print("GPT : " + assistant_content)
            TTS.tts(assistant_content)

            if main.windowMode:
                window = TextWindow(text = assistant_content)
                window.start()

        main.isProcessing = False
        main.nameCalled = False

class TTS:
    engine = None

    @staticmethod
    def onStart():
        print('starting response')

    @staticmethod
    def onEnd(completed):
        print('finishing response : ', completed)

    @staticmethod
    def tts(response):
        if TTS.engine is None:
            TTS.engine = pyttsx3.init()
            TTS.engine.setProperty('rate', 200)
            # TTS.engine.connect('started-utterance', TTS.onStart)
            # TTS.engine.connect('finished-utterance', TTS.onEnd)

        if main.mute:
            return

        TTS.engine.say(response)
        TTS.engine.runAndWait()

class CustomCommand:
    def __init__(self, command):
        if command == "창모드":
            if main.windowMode:
                TTS.tts("창모드 비활성화")
                main.windowMode = False
            else:
                main.windowMode = True
                TTS.tts("창모드 활성화")
        elif command == "음소거":
            if not main.windowMode:
                TTS.tts("음소거 모드는 창모드일때만 가능합니다.")
                return
            if main.mute:
                TTS.tts("음소거 비활성화")
                main.mute = False
            else:
                TTS.tts("음소거 활성화")
                main.mute = True
        elif command == "계속 말할게":
            main.nameCalled = True
        elif command == "그만 말할게":
            main.nameCalled = False

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    args = parser.parse_args()

    config = pb.DecoderConfig(
        sample_rate=SAMPLE_RATE,
        encoding=ENCODING,
        use_itn=True,
        use_disfluency_filter=True,
        use_profanity_filter=False,
    )

    main()
    client = STT(CLIENT_ID, CLIENT_SECRET)
    print("Private Assistant Actived.")
    print("You can call me as : " + ' or '.join(systemName))
    print("Custom commands : " + ", ".join(myCommands))

    isListening = False
    while True:
        if(not main.isProcessing):
            if not isListening:
                client.transcribe_streaming_grpc(None, config)
                isListening = True
            elif isListening:
                isListening = False


import os
import platform
from pathlib import Path

def pop_path_back(path: Path):
    if len(path.parts) > 1:
        return Path().joinpath(*path.parts[1:])
    else:
        return path

def pop_path_front(path: Path):
    if len(path.parts) > 1:
        return Path().joinpath(*path.parts[:-1])
    else:
        return path

def find_vtx(mdl_path: Path):
    possible_vtx_version = [70, 80, 11, None, 90, 12]
    for vtx_version in possible_vtx_version[::-1]:
        if vtx_version is None:
            path = corrected_path(mdl_path.with_suffix(f'.vtx'))
        else:
            path = corrected_path(mdl_path.with_suffix(f'.dx{vtx_version}.vtx'))
        if path is not None and path.exists():
            return path

def find_vtx_cm(mdl_path: Path, content_manager):
    possible_vtx_version = [70, 80, 11, None, 12, 90]
    for vtx_version in possible_vtx_version[::-1]:
        if vtx_version is None:
            path = content_manager.find_file(mdl_path.with_suffix(f'.vtx'))
        else:
            path = content_manager.find_file(mdl_path.with_suffix(f'.dx{vtx_version}.vtx'))
        if path:
            return path

def backwalk_file_resolver(current_path, file_to_find):
    current_path = Path(current_path).absolute()
    file_to_find = Path(file_to_find)

    for _ in range(len(current_path.parts) - 1):
        second_part = file_to_find
        for _ in range(len(file_to_find.parts)):
            new_path = corrected_path(current_path / second_part)
            if new_path.exists():
                return new_path

            second_part = pop_path_back(second_part)
        current_path = pop_path_front(current_path)

def corrected_path(path: Path):
    if platform.system() == "Windows" or path.exists():  # Shortcut for windows
        return path
    root, *parts, fname = path.parts

    new_path = Path(root)
    for part in parts:
        for dir_name in new_path.iterdir():
            if dir_name.is_file():
                continue
            if dir_name.name.lower() == part.lower():
                new_path = dir_name
                break
    for file_name in new_path.iterdir():
        if file_name.is_file() and file_name.name.lower() == fname.lower():
            return file_name
    return path

def resolve_root_directory_from_file(path):
    if type(path) is not Path:
        path = Path(path)
    if len(path.parts) < 2 or path == path.parent:
        return None
    if path.parts[-1] == 'models':
        return path.parent
    else:
        try:
            return resolve_root_directory_from_file(path.parent)
        except RecursionError:
            return None

def get_materials_path(path):
    path = Path(path)
    root_path = resolve_root_directory_from_file(path)
    return root_path / 'materials'

def get_mod_path(path: Path) -> Path:
    _path = path
    while len(path.parts) > 1:
        if (path / 'maps').exists():
            return path
        elif (path / 'materials').exists():
            return path
        elif (path / 'elements').exists():
            return path
        elif (path / 'models').exists() and path.parts[-1] != 'models' and \
                path.parts[-2] != 'materials' and path.parts[-1] != 'materials':
            return path
        if len(path.parts) == 1:
            return _path
        path = path.parent
    return _path


print("Program penjumlahan biner")

x, y = input().split()
a = 0
b = 0

for i in range(len(x)):
    if(x[i] == "1"):
        a = 2 ** ((len(x) - 1) - i) + a
    elif(x[i] == "0"):
        a = a
print(a)
for j in range(len(y)):
    if(y[j] == "1"):
        b = 2 ** ((len(y) - 1) - j) + b
    elif(y[j] == "0"):
        b = b
print(b)

hasil = a + b
biner = ""

while hasil > 0:
    if (hasil%2 == 0):
        biner = "0" + biner
    else:
        biner = "1" + biner
    hasil //= 2
print(biner,end=" ")


import os

class RoseArchGzip:

    """Compress archive sources in gzip."""

    SCHEMES = ["gz", "gzip"]

    def __init__(self, app_runner, *args, **kwargs):
        self.app_runner = app_runner

    def compress_sources(self, target, work_dir):
        """Gzip each source in target.

        Use work_dir to dump results.

        """
        for source in target.sources.values():
            if source.path.endswith("." + target.compress_scheme):
                continue  # assume already done
            name_gz = source.name + "." + target.compress_scheme
            work_path_gz = os.path.join(work_dir, name_gz)
            self.app_runner.fs_util.makedirs(
                self.app_runner.fs_util.dirname(work_path_gz)
            )
            # N.B. Python's gzip is slow
            command = "gzip -c '%s' >'%s'" % (source.path, work_path_gz)
            self.app_runner.popen.run_simple(command, shell=True)
            source.path = work_path_gz


import numpy as np
import tensorflow as tf
import pdb
import random
import json
from scipy.stats import mode

import data_utils
import plotting
import model
import utils
import eval

from time import time
from math import floor
from mmd import rbf_mmd2, median_pairwise_distance, mix_rbf_mmd2_and_ratio

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

# --- get settings --- #
# parse command line arguments, or use defaults
parser = utils.rgan_options_parser()
settings = vars(parser.parse_args())
# if a settings file is specified, it overrides command line arguments/defaults
if settings['settings_file']: settings = utils.load_settings_from_file(settings)

# --- get data, split --- #
samples, pdf, labels = data_utils.get_samples_and_labels(settings)

# --- save settings, data --- #
print('Ready to run with settings:')
for (k, v) in settings.items(): print(v, '\t',  k)
# add the settings to local environment
# WARNING: at this point a lot of variables appear
locals().update(settings)
json.dump(settings, open('./experiments/settings/' + identifier + '.txt', 'w'), indent=0)

if not data == 'load':
    data_path = './experiments/data/' + identifier + '.data.npy'
    np.save(data_path, {'samples': samples, 'pdf': pdf, 'labels': labels})
    print('Saved training data to', data_path)

# --- build model --- #

Z, X, CG, CD, CS = model.create_placeholders(batch_size, seq_length, latent_dim,
                                    num_signals, cond_dim)

discriminator_vars = ['hidden_units_d', 'seq_length', 'cond_dim', 'batch_size', 'batch_mean']
discriminator_settings = dict((k, settings[k]) for k in discriminator_vars)
generator_vars = ['hidden_units_g', 'seq_length', 'batch_size',
                  'num_generated_features', 'cond_dim', 'learn_scale']
generator_settings = dict((k, settings[k]) for k in generator_vars)

CGAN = (cond_dim > 0)
if CGAN: assert not predict_labels

D_loss, G_loss = model.GAN_loss(Z, X, generator_settings, discriminator_settings,
        kappa, CGAN, CG, CD, CS, wrong_labels=wrong_labels)
D_solver, G_solver, priv_accountant = model.GAN_solvers(D_loss, G_loss, learning_rate, batch_size,
        total_examples=samples['train'].shape[0], l2norm_bound=l2norm_bound,
        batches_per_lot=batches_per_lot, sigma=dp_sigma, dp=dp)
G_sample = model.generator(Z, **generator_settings, reuse=True, c=CG)

# --- evaluation --- #

# frequency to do visualisations
vis_freq = max(14000//num_samples, 1)
eval_freq = max(7000//num_samples, 1)

# get heuristic bandwidth for mmd kernel from evaluation samples
heuristic_sigma_training = median_pairwise_distance(samples['vali'])
best_mmd2_so_far = 1000

# optimise sigma using that (that's t-hat)
batch_multiplier = 5000//batch_size
eval_size = batch_multiplier*batch_size
eval_eval_size = int(0.2*eval_size)
eval_real_PH = tf.placeholder(tf.float32, [eval_eval_size, seq_length, num_generated_features])
eval_sample_PH = tf.placeholder(tf.float32, [eval_eval_size, seq_length, num_generated_features])
n_sigmas = 2
sigma = tf.get_variable(name='sigma', shape=n_sigmas, initializer=tf.constant_initializer(value=np.power(heuristic_sigma_training, np.linspace(-1, 3, num=n_sigmas))))
mmd2, that = mix_rbf_mmd2_and_ratio(eval_real_PH, eval_sample_PH, sigma)
with tf.variable_scope("SIGMA_optimizer"):
    sigma_solver = tf.train.RMSPropOptimizer(learning_rate=0.05).minimize(-that, var_list=[sigma])
    #sigma_solver = tf.train.AdamOptimizer().minimize(-that, var_list=[sigma])
    #sigma_solver = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(-that, var_list=[sigma])
sigma_opt_iter = 2000
sigma_opt_thresh = 0.001
sigma_opt_vars = [var for var in tf.global_variables() if 'SIGMA_optimizer' in var.name]

sess = tf.Session()
sess.run(tf.global_variables_initializer())

vis_Z = model.sample_Z(batch_size, seq_length, latent_dim, use_time)
if CGAN:
    vis_C = model.sample_C(batch_size, cond_dim, max_val, one_hot)
    if 'mnist' in data:
        if one_hot:
            if cond_dim == 6:
                vis_C[:6] = np.eye(6)
            elif cond_dim == 3:
                vis_C[:3] = np.eye(3)
                vis_C[3:6] = np.eye(3)
            else:
                raise ValueError(cond_dim)
        else:
            if cond_dim == 6:
                vis_C[:6] = np.arange(cond_dim)
            elif cond_dim == 3:
                vis_C = np.tile(np.arange(3), 2)
            else:
                raise ValueError(cond_dim)
    elif 'eICU_task' in data:
        vis_C = labels['train'][np.random.choice(labels['train'].shape[0], batch_size, replace=False), :]
    vis_sample = sess.run(G_sample, feed_dict={Z: vis_Z, CG: vis_C})
else:
    vis_sample = sess.run(G_sample, feed_dict={Z: vis_Z})
    vis_C = None

vis_real_indices = np.random.choice(len(samples['vali']), size=6)
vis_real = np.float32(samples['vali'][vis_real_indices, :, :])
if not labels['vali'] is None:
    vis_real_labels = labels['vali'][vis_real_indices]
else:
    vis_real_labels = None
if data == 'mnist':
    if predict_labels:
        assert labels['vali'] is None
        n_labels = 1
        if one_hot:
            n_labels = 6
            lab_votes = np.argmax(vis_real[:, :, -n_labels:], axis=2)
        else:
            lab_votes = vis_real[:, :, -n_labels:]
        labs, _ = mode(lab_votes, axis=1)
        samps = vis_real[:, :, :-n_labels]
    else:
        labs = None
        samps = vis_real
    if multivariate_mnist:
        plotting.save_mnist_plot_sample(samps.reshape(-1, seq_length**2, 1), 0, identifier + '_real', n_samples=6, labels=labs)
    else:
        plotting.save_mnist_plot_sample(samps, 0, identifier + '_real', n_samples=6, labels=labs)
elif 'eICU' in data:
    plotting.vis_eICU_patients_downsampled(vis_real, resample_rate_in_min,
            identifier=identifier + '_real', idx=0)
else:
    plotting.save_plot_sample(vis_real, 0, identifier + '_real', n_samples=6,
                            num_epochs=num_epochs)

# for dp
target_eps = [0.125, 0.25, 0.5, 1, 2, 4, 8]
dp_trace = open('./experiments/traces/' + identifier + '.dptrace.txt', 'w')
dp_trace.write('epoch ' + ' eps' .join(map(str, target_eps)) + '\n')

trace = open('./experiments/traces/' + identifier + '.trace.txt', 'w')
trace.write('epoch time D_loss G_loss mmd2 that pdf real_pdf\n')

# --- train --- #
train_vars = ['batch_size', 'D_rounds', 'G_rounds', 'use_time', 'seq_length',
              'latent_dim', 'num_generated_features', 'cond_dim', 'max_val',
              'WGAN_clip', 'one_hot']
train_settings = dict((k, settings[k]) for k in train_vars)

t0 = time()
best_epoch = 0
print('epoch\ttime\tD_loss\tG_loss\tmmd2\tthat\tpdf_sample\tpdf_real')
for epoch in range(num_epochs):
    D_loss_curr, G_loss_curr = model.train_epoch(epoch, samples['train'], labels['train'],
                                        sess, Z, X, CG, CD, CS,
                                        D_loss, G_loss,
                                        D_solver, G_solver,
                                        **train_settings)
    # -- eval -- #

    # visualise plots of generated samples, with/without labels
    if epoch % vis_freq == 0:
        if CGAN:
            vis_sample = sess.run(G_sample, feed_dict={Z: vis_Z, CG: vis_C})
        else:
            vis_sample = sess.run(G_sample, feed_dict={Z: vis_Z})
        plotting.visualise_at_epoch(vis_sample, data,
                predict_labels, one_hot, epoch, identifier, num_epochs,
                resample_rate_in_min, multivariate_mnist, seq_length, labels=vis_C)

    # compute mmd2 and, if available, prob density
    if epoch % eval_freq == 0:
        ## how many samples to evaluate with?
        eval_Z = model.sample_Z(eval_size, seq_length, latent_dim, use_time)
        if 'eICU_task' in data:
            eval_C = labels['vali'][np.random.choice(labels['vali'].shape[0], eval_size), :]
        else:
            eval_C = model.sample_C(eval_size, cond_dim, max_val, one_hot)
        eval_sample = np.empty(shape=(eval_size, seq_length, num_signals))
        for i in range(batch_multiplier):
            if CGAN:
                eval_sample[i*batch_size:(i+1)*batch_size, :, :] = sess.run(G_sample, feed_dict={Z: eval_Z[i*batch_size:(i+1)*batch_size], CG: eval_C[i*batch_size:(i+1)*batch_size]})
            else:
                eval_sample[i*batch_size:(i+1)*batch_size, :, :] = sess.run(G_sample, feed_dict={Z: eval_Z[i*batch_size:(i+1)*batch_size]})
        eval_sample = np.float32(eval_sample)
        eval_real = np.float32(samples['vali'][np.random.choice(len(samples['vali']), size=batch_multiplier*batch_size), :, :])

        eval_eval_real = eval_real[:eval_eval_size]
        eval_test_real = eval_real[eval_eval_size:]
        eval_eval_sample = eval_sample[:eval_eval_size]
        eval_test_sample = eval_sample[eval_eval_size:]

        ## MMD
        # reset ADAM variables
        sess.run(tf.initialize_variables(sigma_opt_vars))
        sigma_iter = 0
        that_change = sigma_opt_thresh*2
        old_that = 0
        while that_change > sigma_opt_thresh and sigma_iter < sigma_opt_iter:
            new_sigma, that_np, _ = sess.run([sigma, that, sigma_solver], feed_dict={eval_real_PH: eval_eval_real, eval_sample_PH: eval_eval_sample})
            that_change = np.abs(that_np - old_that)
            old_that = that_np
            sigma_iter += 1
        opt_sigma = sess.run(sigma)
        mmd2, that_np = sess.run(mix_rbf_mmd2_and_ratio(eval_test_real, eval_test_sample,biased=False, sigmas=sigma))

        ## save parameters
        if mmd2 < best_mmd2_so_far and epoch > 10:
            best_epoch = epoch
            best_mmd2_so_far = mmd2
            model.dump_parameters(identifier + '_' + str(epoch), sess)

        ## prob density (if available)
        if not pdf is None:
            pdf_sample = np.mean(pdf(eval_sample[:, :, 0]))
            pdf_real = np.mean(pdf(eval_real[:, :, 0]))
        else:
            pdf_sample = 'NA'
            pdf_real = 'NA'
    else:
        # report nothing this epoch
        mmd2 = 'NA'
        that = 'NA'
        pdf_sample = 'NA'
        pdf_real = 'NA'

    ## get 'spent privacy'
    if dp:
        spent_eps_deltas = priv_accountant.get_privacy_spent(sess, target_eps=target_eps)
        # get the moments
        deltas = []
        for (spent_eps, spent_delta) in spent_eps_deltas:
            deltas.append(spent_delta)
        dp_trace.write(str(epoch) + ' ' + ' '.join(map(str, deltas)) + '\n')
        if epoch % 10 == 0: dp_trace.flush()

    ## print
    t = time() - t0
    try:
        print('%d\t%.2f\t%.4f\t%.4f\t%.5f\t%.0f\t%.2f\t%.2f' % (epoch, t, D_loss_curr, G_loss_curr, mmd2, that_np, pdf_sample, pdf_real))
    except TypeError:       # pdf are missing (format as strings)
        print('%d\t%.2f\t%.4f\t%.4f\t%.5f\t%.0f\t %s\t %s' % (epoch, t, D_loss_curr, G_loss_curr, mmd2, that_np, pdf_sample, pdf_real))

    ## save trace
    trace.write(' '.join(map(str, [epoch, t, D_loss_curr, G_loss_curr, mmd2, that_np, pdf_sample, pdf_real])) + '\n')
    if epoch % 10 == 0:
        trace.flush()
        plotting.plot_trace(identifier, xmax=num_epochs, dp=dp)

    if shuffle:     # shuffle the training data
        perm = np.random.permutation(samples['train'].shape[0])
        samples['train'] = samples['train'][perm]
        if labels['train'] is not None:
            labels['train'] = labels['train'][perm]

    if epoch % 50 == 0:
        model.dump_parameters(identifier + '_' + str(epoch), sess)

trace.flush()
plotting.plot_trace(identifier, xmax=num_epochs, dp=dp)
model.dump_parameters(identifier + '_' + str(epoch), sess)

## after-the-fact evaluation
#n_test = vali.shape[0]      # using validation set for now TODO
#n_batches_for_test = floor(n_test/batch_size)
#n_test_eval = n_batches_for_test*batch_size
#test_sample = np.empty(shape=(n_test_eval, seq_length, num_signals))
#test_Z = model.sample_Z(n_test_eval, seq_length, latent_dim, use_time)
#for i in range(n_batches_for_test):
#    test_sample[i*batch_size:(i+1)*batch_size, :, :] = sess.run(G_sample, feed_dict={Z: test_Z[i*batch_size:(i+1)*batch_size]})
#test_sample = np.float32(test_sample)
#test_real = np.float32(vali[np.random.choice(n_test, n_test_eval, replace=False), :, :])
## we can only get samples in the size of the batch...
#heuristic_sigma = median_pairwise_distance(test_real, test_sample)
#test_mmd2, that = sess.run(mix_rbf_mmd2_and_ratio(test_real, test_sample, sigmas=heuristic_sigma, biased=False))
##print(test_mmd2, that)


import random

from utlis import run_time

class Merge(object):

    @staticmethod
    def less(v, w):
        """
        比较v和w的大小
        :param v: 可比较对象
        :param w: 可比较对象
        :return: True/False
        """
        return v < w

    def is_sorted(self, array):
        """
        判断array是否有序，有序返回True，无序返回False
        :param array: 可迭代对象
        :return: True/False
        """
        for i in range(1, len(array)):
            if self.less(array[i], array[i - 1]):
                return False
        return True

    def merge(self, array, low, mid, high):
        start = low
        end = mid + 1

        for k in range(low, high + 1):
            self.aux[k] = array[k]

        for k in range(low, high + 1):
            if start > mid:
                array[k] = self.aux[end]
                end += 1
            elif end > high:
                array[k] = self.aux[start]
                start += 1
            elif self.less(self.aux[end], self.aux[start]):
                array[k] = self.aux[end]
                end += 1
            else:
                array[k] = self.aux[start]
                start += 1

    def _sort(self, array, low, high):
        if high <= low:
            return
        mid = low + int((high - low) / 2)
        self._sort(array, low, mid)
        self._sort(array, mid + 1, high)
        self.merge(array, low, mid, high)

    @run_time
    def sort(self, array):
        self.aux = [0] * len(array)
        self._sort(array, 0, len(array) - 1)

if __name__ == '__main__':
    merge_sort = Merge()
    # data = ['S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E']
    # data = [random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in range(10000)]
    data = [random.random() for i in range(1000000)]
    # print(data)
    merge_sort.sort(data)
    print(merge_sort.is_sorted(data))
    # print(data)


import discord
import asyncio

from redbot.core import Config
from redbot.core import commands
from redbot.core import checks
from redbot.core.utils.predicates import ReactionPredicate
from redbot.core.utils.menus import start_adding_reactions

from datetime import datetime, timedelta
from pytz import timezone, all_timezones_set

import requests

defaults = {'TimeZone': 'America/New_York'}
WHITE_X_REACT = "\U0000274E"                # :negative_squared_cross_mark:
WHITE_CHECK_REACT = "\U00002705"            # :white_check_mark:

class DMMonty(commands.Cog):
    """We love Monty!"""

    def __init__(self, bot):
        self.bot = bot
        self.config = Config.get_conf(
            self, identifier=1234567893, force_registration=True)
        self.config.register_guild(**defaults)
        self.time_zones = {}
        self.dm_compliments = {}
        self.task = asyncio.create_task(self.pre_load_data())

    @commands.guild_only()
    @commands.command()
    @checks.admin_or_permissions(manage_guild=True)
    async def setTimeZone(self, ctx, time_zone):
        """Sets timezone for the guild. Valid time zone codes are listed in the "TZ database name" column of
         the following wikipedia page: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"""

        if time_zone not in all_timezones_set:
            wiki = 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'

            msg = (':x: **{}** is not a valid time zone code. Please select a time zone from the "TZ database name" column '
                   'from this wikipedia page: {}').format(time_zone, wiki)

            return await ctx.send(msg)

        await self._save_time_zone(ctx.guild, time_zone)
        await ctx.send("Done")

    @commands.guild_only()
    @commands.command(aliases=['dc'])
    @checks.admin_or_permissions(manage_guild=True)
    async def dailyCompliment(self, ctx, member: discord.Member):
        try:
            self.dm_compliments[member] = True
            asyncio.create_task(self.auto_dm_compliments(member, ctx.message))
            await ctx.message.add_reaction(WHITE_CHECK_REACT)
        except:
            await ctx.reply("I tried and failed :(")

    @commands.guild_only()
    @commands.command(aliases=['sdc'])
    @checks.admin_or_permissions(manage_guild=True)
    async def stopDailyCompliments(self, ctx, member: discord.Member):
        try:
            self.dm_compliments[member] = False
            await ctx.message.add_reaction(WHITE_CHECK_REACT)
        except:
            await ctx.reply("I tried and failed :(")

    @commands.guild_only()
    @commands.command(aliases=['cs'])
    @checks.admin_or_permissions(manage_guild=True)
    async def compliments(self, ctx, quantity=5, interval_sec=10):
        for i in range(quantity):
            await ctx.reply(self.get_compliment())
            await asyncio.sleep(interval_sec)

    @commands.guild_only()
    @commands.command(aliases=['c'])
    @checks.admin_or_permissions(manage_guild=True)
    async def compliment(self, ctx, member: discord.Member):
        compliment = self.get_compliment()
        try:
            await member.send(compliment)
            await ctx.message.add_reaction(WHITE_CHECK_REACT)
        except:
            await ctx.reply("I tried and failed :(")

    # helper functions
    async def pre_load_data(self):
        """Loop task to preload guild data"""
        await self.bot.wait_until_ready()
        for guild in self.bot.guilds:
            self.time_zones[guild] = (await self._get_time_zone(guild))

    async def auto_dm_compliments(self, member: discord.Member, message: discord.Message=None):
        """Loop task to auto-update match day"""
        await self.bot.wait_until_ready()
        try:
            while self.dm_compliments.get(member, False):
                await member.send(self.get_compliment())
                update_time = self.schedule_next_update()
                await asyncio.sleep(update_time)
        except:
            self.dm_compliments[member] = False
        del self.dm_compliments[member]

        if message:
            try:
                await message.add_reaction(WHITE_X_REACT)
            except:
                pass

    def schedule_next_update(self):
        # wait_time = 3600  # one hour
        today = datetime.date(datetime.now())
        tomorrow = today + timedelta(days=1)
        tomorrow_dt = datetime.combine(tomorrow, datetime.min.time())
        tomorrow_dt.replace(hour = 12) # send at noon
        wait_time = (tomorrow_dt - datetime.now()).seconds + 30
        return wait_time

    # secondary helpers
    def get_compliment(self):
        return requests.get("https://complimentr.com/api").json().get("compliment")

    # db

    async def _save_time_zone(self, guild, time_zone):
        await self.config.guild(guild).TimeZone.set(time_zone)
        self.time_zones[guild] = time_zone

    async def _get_time_zone(self, guild):
        return await self.config.guild(guild).TimeZone()


from .interface import AbstractLinkedList
from .node import Node

class LinkedList(AbstractLinkedList):
    """
    Implementation of an AbstractLinkedList inteface.
    """

    def __init__(self, elements=None):

        self.elements = elements

        self.start = None
        self.end = None

        if self.elements:
            for element in self.elements:
                self.append(element)

    def __str__(self):
        return '[{}]'.format(", ".join(str(p) for p in self.elements)) if self.elements else "[]"

    def __len__(self):
        return self.count()

    def __iter__(self):
        x = self.start

        while True:
            yield x.elem

            if x.next == None:
                break

            x = x.next

        raise StopIteration

    # THIS FUNCTION NEEDED TO RETURN A VALUE E.G. THE TEST_GET_ITEM TEST.
    # BUT WE WERE USING IT TO RETURN A NODE IN THE POP FUNCTION.
    # SO I WAS BREAKING THE POP WHEN I WAS MODIFYING __GETITEM__
    # SO I CREATED THE GET_NODE FUNCTION BELOW IT. NOW EVERYTHING WORKS.
    def __getitem__(self, index):
        if index >= len(self):
            raise IndexError

        wantnode = self.start
        for i in range(index):
            wantnode = wantnode.next
        return wantnode.elem #ADDED .ELEM TO RETURN THE VALUE

    def get_node(self, index):
        if index >= len(self):
            raise IndexError

        wantnode = self.start
        for i in range(index):
            wantnode = wantnode.next
        return wantnode # THIS IS OUR ORIGINAL __GETITEM__ THAT RETURNS A NODE

    def __add__(self, other):
        #init new list
        n_list = LinkedList()
        if self.elements:
            for element in self.elements:
                n_list.append(element)

        if other.elements:
            for element in other.elements:
                n_list.append(element)

        return n_list

    def __iadd__(self, other):
        return self + other

    def __ne__(self, other):
        return not self.__eq__(other)

    def __eq__(self, other):

        s = self.start
        o = other.start

        def is_eq(a, b):
            if not a and not b:
               return True
            if bool(a) == False or bool(b) == False:
                return False
            if a.elem != b.elem:
                return False
            else:
               return is_eq(a.next, b.next)

        return is_eq(s, o)

    def append(self, elem):
        new_node = Node(elem)
        if self.start is None:
            self.start = new_node
            self.end = new_node
        else:
            self.end.next = new_node
            self.end = new_node

    def count(self):

        if self.start:
            counter = 1
            tempNode = self.start
            while tempNode.next:
                counter += 1
                tempNode = tempNode.next
            return counter
        else:
            return 0

    def pop(self, index=None):

        if len(self) == 0:
            raise IndexError

        if index != None:
            if index >= len(self):
                raise IndexError

        if index == 0 or len(self) == 1:
            to_pop = self.start
            self.start = self.start.next
            return to_pop.elem

        if index is None or index == len(self) - 1:
            indextemp = len(self) - 1
            to_pop = self.get_node(indextemp)
            self.end = self.get_node(indextemp-1)
            self.end.next = None
            return to_pop.elem

        else:

            to_pop = self.get_node(index)
            #self.__getitem__(indextemp).next = self.__getitem__(len(self) + 1)
            before_to_pop = self.get_node(index-1)
            before_to_pop.next = to_pop.next
            return to_pop.elem