import datetime
import sys
from gettext import gettext as _

from gi.repository import Gtk, Gio, GLib, Notify, Adw, GdkPixbuf, Gdk, GObject

from frog.config import RESOURCE_PREFIX, APP_ID
from frog.language_manager import language_manager
from frog.services.clipboard_service import clipboard_service
from frog.services.screenshot_service import ScreenshotService
from frog.settings import Settings
from frog.window import FrogWindow

class FrogApplication(Adw.Application):
    __gtype_name__ = 'FrogApplication'
    gtk_settings: Gtk.Settings

    settings: Settings = GObject.Property(type=GObject.TYPE_PYOBJECT)

    def __init__(self, version=None):
        super().__init__(application_id=APP_ID,
                         flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
        self.backend = None
        self.version = version

        # Init GSettings
        self.settings = Settings.new()

        self.add_main_option(
            'extract_to_clipboard',
            ord('e'),
            GLib.OptionFlags.NONE,
            GLib.OptionArg.NONE,
            _('Extract directly into the clipboard'),
            None
        )

        # Initialize tesseract data files storage.
        language_manager.init_tessdata()

        # Initialize libnotify.
        Notify.init("Frog")

    def do_startup(self, *args, **kwargs):
        Adw.Application.do_startup(self)

        # create command line option entries
        shortcut_entry = GLib.OptionEntry()
        shortcut_entry.long_name = 'extract_to_clipboard'
        shortcut_entry.short_name = ord('e')
        shortcut_entry.flags = 0
        shortcut_entry.arg = GLib.OptionArg.NONE
        shortcut_entry.arg_date = None
        shortcut_entry.description = _('Extract directly into the clipboard')
        shortcut_entry.arg_description = None

        self.backend = ScreenshotService()
        self.backend.connect('decoded', FrogApplication.on_decoded)

        action = Gio.SimpleAction.new("show_uri", GLib.VariantType.new('s'))
        action.connect("activate", self.on_show_uri)
        self.add_action(action)

        self.create_action('get_screenshot', self.get_screenshot, ['<primary>g'])
        self.create_action('get_screenshot_and_copy', self.get_screenshot_and_copy, ['<primary><shift>g'])
        self.create_action('copy_to_clipboard', self.on_copy_to_clipboard, ['<primary>g'])
        self.create_action('open_image', self.open_image, ['<primary>o'])
        self.create_action('paste_from_clipboard', self.on_paste_from_clipboard, ['<primary>v'])
        self.create_action('listen', self.on_listen, ['<primary>l'])
        self.create_action('listen_cancel', self.on_listen_cancel, ['<primary><ctrl>l'])
        self.create_action('shortcuts', self.on_shortcuts, ['<primary>question'])

        self.create_action('quit', lambda *_: self.quit(), ['<primary>q', '<primary>w'])
        self.create_action('about', self.on_about)
        self.create_action('preferences', self.on_preferences, ['<primary>comma'])

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = FrogWindow(application=self)
        win.present()

    def do_command_line(self, command_line):
        options = command_line.get_options_dict()
        options = options.end().unpack()

        if "extract_to_clipboard" in options:
            self.backend.capture(self.settings.get_string("active-language"), True)
            return 1

        self.activate()
        return 0

    def on_preferences(self, _action, _param) -> None:
        self.get_active_window().show_preferences()

    def on_about(self, _action, _param):
        about_window = Adw.AboutWindow(
            application_name="Frog",
            application_icon=APP_ID,
            version=self.version,
            copyright=f'© {datetime.date.today().year} Tender Owl',
            website="https://getfrog.app",
            issue_url="https://github.com/TenderOwl/Frog/issues/new",
            license_type=Gtk.License.MIT_X11,
            developer_name="TenderOwl Team",
            developers=["Andrey Maksimov"],
            release_notes="""
In this update, we've made a number of improvements to our app.

Share extracted text: This means you can now share your findings with the wider community.

Add Text-to-speech to be able to listen to the extracted text
                    without any additional apps (internet connection required).

Add the ability to insert images from the clipboard
                    by using the Ctrl+V shortcut.

In addition, we have upgraded the language selection dropdown
                    to popover with a quick search of a language.

Last but not least, Frog has become more accessibility-friendly.

We hope you enjoy our work!

            """,
            transient_for=self.props.active_window
        )
        about_window.present()

    def on_shortcuts(self, _action, _param):
        builder = Gtk.Builder()
        builder.add_from_resource(f"{RESOURCE_PREFIX}/ui/shortcuts.ui")
        builder.get_object("shortcuts").set_transient_for(self.get_active_window())
        builder.get_object("shortcuts").present()

    def on_copy_to_clipboard(self, _action, _param) -> None:
        self.get_active_window().on_copy_to_clipboard(self)

    def on_show_uri(self, _action, param) -> None:
        Gtk.show_uri(None, param.get_string(), Gdk.CURRENT_TIME)

    def get_screenshot(self, _action, _param) -> None:
        self.get_active_window().get_screenshot()

    def get_screenshot_and_copy(self, _action, _param) -> None:
        self.get_active_window().get_screenshot(copy=True)

    def open_image(self, _action, _param) -> None:
        self.get_active_window().open_image()

    def on_paste_from_clipboard(self, _action, _param) -> None:
        self.get_active_window().on_paste_from_clipboard(self)

    @staticmethod
    def on_decoded(_sender, text: str, copy: bool) -> None:
        icon = GdkPixbuf.Pixbuf.new_from_resource_at_scale(
            f"{RESOURCE_PREFIX}/icons/com.github.tenderowl.frog.svg",
            128, 128, True
        )

        if not text:
            notification: Notify.Notification = Notify.Notification.new(
                summary='Frog',
                body=_('No text found. Try to grab another region.')
            )
            notification.set_icon_from_pixbuf(icon)
            notification.show()

        if copy:
            clipboard_service.set(text)

            notification: Notify.Notification = Notify.Notification.new(
                summary='Frog',
                body=_('Text extracted. You can paste it with Ctrl+V')
            )
            notification.set_icon_from_pixbuf(icon)
            notification.show()

        else:
            print(f'{text}\n')

    def on_listen(self, _sender, _event):
        self.get_active_window().on_listen()

    def on_listen_cancel(self, _sender, _event):
        self.get_active_window().on_listen_cancel()

    def create_action(self, name, callback, shortcuts=None):
        """Add an application action.

        Args:
            name: the name of the action
            callback: the function to be called when the action is
              activated
            shortcuts: an optional list of accelerators
        """
        action = Gio.SimpleAction.new(name, None)
        action.connect("activate", callback)
        self.add_action(action)
        if shortcuts:
            self.set_accels_for_action(f"app.{name}", shortcuts)

def main(version):
    app = FrogApplication(version)
    return app.run(sys.argv)


from itertools import combinations
import math

def solution(nums):
    answer = 0
    bools = True
    comb = list(combinations(nums, 3))
    arr = []

    for i in comb:
        arr.append(sum(i))

    for i in arr:
        for j in range(2, int(math.sqrt(i)) + 1):
            if i % j == 0:
                bools = False
        if bools == True:
            answer += 1
        bools = True
    return answer

프로그래머스/lv1/12977. 소수 만들기/소수 만들기.py


# dungeon_numbers.py
# Andrew Lounsbury
# 26/3/23
# Purpose: compute dungeon numbers; https://www.youtube.com/watch?v=xNx3JxRhnZE; https://www.youtube.com/watch?v=HFeKdMf01rQ

import math
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# computes a base b
# input: two numbers, integer or decimal
def base(a, b):
    result = 0
    ls = []
    counter = 0
    countPlaces = False
    for char in str(a):
        if char != '.':
            ls.append(char)
        else:
            countPlaces = True
        if countPlaces:
            counter -= 1
    counter += 1
    digits_backward = [int(d) for d in ls]
    digits = np.flip(digits_backward)
    if countPlaces:
        i = counter
        for d in digits:
            result += d * (b ** i)
            i += 1
    else:
        for i, d in enumerate(digits):
            result += d * (b ** i)
    return result

# Computes a dungeon number
# input: either a sequence of individual numbers or a list of numbers, integer or decimal
def dungeonNumber(*args, mode='b'):
    if isinstance(args[0], list): # input is a list
        numbers = args[0].copy()
        if numbers:
            if mode == 'b': # bottom-up
                num = base(numbers[len(numbers) - 2], numbers[len(numbers) - 1])
                if (numbers):
                    numbers.pop(len(numbers) - 1)
                if (len(numbers) > 1):
                    numbers.pop(len(numbers) - 1)
                    return dungeonNumber(*tuple(numbers), num, mode='b')
                else:
                    return num
            elif mode == 't': # top-down
                if len(numbers) == 1:
                    return numbers[0]
                else:
                    num = base(numbers[0], numbers[1])
                    if (numbers):
                        numbers.pop(0)
                        if (len(numbers) > 1):
                            numbers.pop(0)
                            return dungeonNumber(num, *tuple(numbers), mode='t')
                        else:
                            return num
            else:
                print("ERROR: unknown mode specified")
        else:
            print("Error: list must be nonempty")
    else: # input is a sequence of individual integers
        if mode == 'b': # bottom-up
            num = base(args[len(args) - 2], args[len(args) - 1])
            args = list(args)
            if (args):
                args.pop(len(args) - 1)
                if (len(args) > 1):
                    args.pop(len(args) - 1)
                    return dungeonNumber(*tuple(args), num, mode='b')
                else:
                    return num
        elif mode == 't': # top-down
            if len(args) == 1:
                return args[0]
            else:
                num = base(args[0], args[1])
                args = list(args)
                if (args):
                    args.pop(0)
                    if (len(args) > 1):
                        args.pop(0)
                        return dungeonNumber(num, *tuple(args), mode='t')
                    else:
                        return num
        else:
            print("ERROR: unknown mode specified")

# 10, 11, 13, 16, 20, 25, 31, 38, 46
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy()), end="")
    numbers.append(numbers[len(numbers) - 1] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 25, 31, 38, 46
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy()), end="")
    numbers.insert(0, numbers[0] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 30, 48, 76, 132
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy(), mode='t'), end="")
    numbers.append(numbers[len(numbers) - 1] + 1)
    if i < 8:
        print(", ", end="")
print()

# 10, 11, 13, 16, 20, 28, 45, 73, 133
numbers = [10]
for i in range(9):
    print(dungeonNumber(numbers.copy(), mode="t"), end="")
    numbers.insert(0, numbers[0] + 1)
    if i < 8:
        print(", ", end="")
print()

# Scatterplots of the Golden Ratio
onepointone = []
sequence = []
n = 10
for i in range(n):
    onepointone.append(1.1)
    sequence.append(dungeonNumber(onepointone))

df = pd.DataFrame(sequence, columns=["Number"])
indices = [i for i in range(n)]
df['index'] = indices
sns.scatterplot(x="index", y="Number", data=df)
plt.savefig("images/10.png")
plt.show()

onepointone = []
sequence = []
n = 50
for i in range(n):
    onepointone.append(1.1)
    sequence.append(dungeonNumber(onepointone))

print(sequence[len(sequence) - 1])

df = pd.DataFrame(sequence, columns=["Number"])
indices = [i for i in range(n)]
df['index'] = indices
sns.scatterplot(x="index", y="Number", data=df)
plt.savefig("images/50.png")
plt.show()


import pandas as pd
import torch
import d2l
from .snli_dataset import SNLIDataset

class DataService():
    def __init__(self):
       pass

    #loads csv into pandas dataframe.
    def load_data(self, train_path, test_path):
        train = pd.read_csv(train_path)
        test = pd.read_csv(test_path)
        train["label"] = train["label"].astype(int)
        test["label"] = test["label"].astype(int)
        return train, test

    #load pandas dataframe to dataset.
    def create_snli_dataset(self, train, test, num_steps = 50, batch_size = 256, num_workers = 4):
        train_set = SNLIDataset(train, num_steps)
        test_set = SNLIDataset(test, num_steps, train_set.vocab)
        vocab = train_set.vocab
        train_iter = torch.utils.data.DataLoader(train_set, batch_size,
                                                    shuffle=False,
                                                    num_workers=num_workers)
        test_iter = torch.utils.data.DataLoader(test_set, batch_size,
                                                shuffle=False,
                                                num_workers=num_workers)
        return train_iter, test_iter, vocab


"""
Basic neural net that can either take features as is as input
or embeddings (with small modifications to the code). This is
a form of sparse input which allows us to store all data in
it's sparse format on the GPU.
Note that this is a _response_ model for _classification_,
i.e. not a model predicting uplift nor a regression model!

Notes:
Perhaps add docstrings.
This classifier uses AUC-ROC as metric for best network (in contrast
to most dsl-networks that use AUUC (/ECRR)).
"""
import copy
import metrics.metrics as metrics  # Script with 'expected_calibration_error'
from sklearn.metrics import roc_auc_score
import torch
from torch.utils.data import DataLoader
import torch.nn as nn

if torch.cuda.is_available():
    DEVICE = 'cuda'
else:
    DEVICE = 'cpu'
BATCH_SIZE = 200000
NUM_WORKERS = 16
# Default learning rate in pytorch (at least for Adagrad) is .01
LEARNING_RATE = .1

class NeuralNet(nn.Module):
    """
    A basic neural net class for binary classification.

    Args:
        differentiable_predictions  If True, predict(...) will be blocking gradients.
    """
    def __init__(self, n_features, n_hidden_units=128, dropout_rate=.5, dependent_var='y',
                       differentiable_predictions=False):
        super().__init__()
        self.n_hidden_units = n_hidden_units
        self.dropout_rate = dropout_rate
        self.dependent_var = dependent_var
        self.max_auc_roc = 0
        self.max_ece = None
        self.model = torch.nn.Sequential(
            # nn.EmbeddingBag(n_features, n_hidden_units, mode='sum', sparse=True),
            nn.Linear(n_features, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, n_hidden_units),
            # nn.ReLU(),
            nn.Sigmoid(),
            nn.Dropout(dropout_rate),
            nn.BatchNorm1d(n_hidden_units),

            nn.Linear(n_hidden_units, 1),
            nn.Sigmoid(),
        ).to(DEVICE)

        # Adamax does not support sparse input. Use Adagrad.
        self.optimizer = torch.optim.Adagrad(self.model.parameters(),
                                             lr=LEARNING_RATE)
        # optimizer = torch.optim.Adamax(model.parameters(), lr=LEARNING_RATE)

        # Both BCE and MSE would be acceptable loss-functions.
        # loss_fn = torch.nn.MSELoss()
        self.loss_fn = torch.nn.BCELoss()

        self._differentiable_predictions = differentiable_predictions

    def forward(self, x):
        return self.model(x)

    def fit(self, training_data, validation_data,
            n_epochs=10000,
            max_epochs_without_improvement=101,
            verbose=False):
        epochs_without_improvement = 0
        best_model = self.model
        # Create DataLoader outside of loop:
        tmp_training_dataloader = DataLoader(training_data, pin_memory=True, num_workers=NUM_WORKERS, batch_size=BATCH_SIZE)

        # Actual training loop:
        for i in range(n_epochs):
            if i % 50 == 0:
                # This is the slowest part of the code. Let's evaluate progress
                # only every 50th epoch:
                self.model.train(mode=False)
                validation_auc_roc, validation_ece = self.get_metrics(validation_data)
                if self.max_auc_roc < validation_auc_roc:
                    self.max_auc_roc = validation_auc_roc
                    # ECE is only a reasonable metric if the neural net is
                    # directly modeling uplift (e.g. not double-classifier)
                    self.max_ece = validation_ece
                    best_model = copy.deepcopy(self.model)
                    # Reset counter:
                    epochs_without_improvement = 0
                else:
                    epochs_without_improvement += 50
                    if epochs_without_improvement > max_epochs_without_improvement:
                        print("No improvement during last {} epochs after {} epochs.".format(max_epochs_without_improvement, i))
                        break
            self.model.train(mode=True)
            # Some data loader optimization:
            for batch in tmp_training_dataloader:
                #y_pred = self.model(batch['X'].to(DEVICE))
                #loss = self.loss_fn(y_pred, batch[self.dependent_var].view(-1, 1).to(DEVICE))
                # Maybe add tmp_next here to allow for data loading also during
                # training computation on GPU? Could speed up by 25-33%?
                # Two batches should fit in memory just fine.
                tmp_X = batch['X'].to(DEVICE, non_blocking=True)
                tmp_y = batch[self.dependent_var].to(DEVICE, non_blocking=True)
                y_pred = self.model(tmp_X)
                loss = self.loss_fn(y_pred, tmp_y.view(-1, 1))

                if verbose:
                    # Not tested:
                    print("Training set loss: {}".format(loss))
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
        if best_model is self.model:
            # Sanity check
            print("Model not updated. No improvement found in {} epochs.".format(i))
        # When done, set self.model to best model to date:
        self.model = best_model
        # Set to prediction mode:
        self.model.train(mode=False)
        if verbose:
            print("Max validation set AUC-ROC: {}".format(self.max_auc_roc))
            print("Max validation set ECE: {}".format(self.max_ece))
        return

    def predict_torch(self, dataset):
        # When predicting, model mode always needs to be set to false.
        self.model.train(mode=False)
        # Perhaps predict in batches?
        tmp_dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=False, pin_memory=True, num_workers=NUM_WORKERS)
        predictions = None
        with torch.autograd.no_grad():
            for batch in tmp_dataloader:
                if predictions is None:
                    predictions = self.model(batch['X'].to(DEVICE)).cpu()
                else:
                    predictions = torch.cat((predictions,
                                             self.model(batch['X'].to(DEVICE)).cpu()))
        return predictions

    def predict(self, data):
        if self._differentiable_predictions:
            return self.model(torch.from_numpy(data))

        self.model.train(mode=False)
        # Predict in one batch
        self.model.cpu()
        tmp = self.model(torch.from_numpy(data)).detach().numpy()
        self.model.to(DEVICE)
        return tmp

    def get_metrics(self, dataset):
        """
        Function for estimating auc-roc and ece for binary classifier.

        Args:
        dataset (load_data.DatasetWrapper): Dataset containing 'y' (true label).

        Notes:
        Estimating anything else than auc_roc is actually not that essential at
        this point as the model selection is done using only that metric, and
        the other metrics are later evaluated for the testing set.
        """
        # dataset as CriteoDataset
        predictions = self.predict_torch(dataset)
        auc_roc = roc_auc_score(dataset[:][self.dependent_var], predictions)
        ece = metrics.expected_calibration_error(dataset[:][self.dependent_var], predictions.numpy())
        #ece = metrics.expected_calibration_error(dataset[:]['y'], predictions)
        print("AUC-ROC: {}\t ECE: {}".format(auc_roc, ece))
        return((auc_roc, ece))


import unittest
from healthcareai.supervised_model_trainer import SupervisedModelTrainer
import healthcareai.datasets as hcai_datasets
import sys
from io import StringIO

class TestTrainerDecorator(unittest.TestCase):

    """Tests for the training decorator.

    We will compare a decorated linear regression and a undecorated linear regression output. They should not be
    the same.

    """

    def setUp(self):
        df = hcai_datasets.load_diabetes()

        # Drop columns that won't help machine learning
        columns_to_remove = ['PatientID']
        df.drop(columns_to_remove, axis=1, inplace=True)

        self.regression_trainer = SupervisedModelTrainer(df,
                                                         'SystolicBPNBR',
                                                         'regression',
                                                         grain_column='PatientEncounterID',
                                                         impute=True,
                                                         verbose=False)

        def undecorated_lr(self):
            return self._advanced_trainer.linear_regression(randomized_search=False)

        self.regression_trainer.undecorated_lr = undecorated_lr.__get__(self.regression_trainer,
                                                                        self.regression_trainer.__class__)

    def test_decorator(self):
        out = StringIO()
        sys.stdout = out
        self.regression_trainer.linear_regression()
        decorated_output = out.getvalue().strip()

        out = StringIO()
        sys.stdout = out
        self.regression_trainer.undecorated_lr()
        undecorated_output = out.getvalue().strip()

        assert decorated_output != undecorated_output


import numpy as np
import scipy
from pymatching import Matching
import matplotlib.pyplot as plt
import os
import glob
from pathlib import Path
from argparse import ArgumentParser
from shapely.geometry import LineString
import re
from time import process_time
import psutil
import json

def ram_usage():
    process = psutil.Process(os.getpid())
    mem = process.memory_percent()
    return mem

# noiseless parity check measurements
def num_decoding_failures(H, logicals, p, num_trials):
    """
    H --> is the matrix that describes the lattice (Hz or Hx)
    logicals --> X-logical or Z-logical operators
    p --> list of the error thresholds to test
    num_trials --> the number or repetitions of the decoder
    """

    # It's a good practice to create the Matchin object at the beginning of the procedure.
    matching = Matching(H)
    num_errors = 0

    # For every point defined by linspace we run the decoding procedure num_trials times.
    for i in range(num_trials):

        # Simulate an error under the independent noise model
        # where an error will occur with probability p.
        noise = np.random.binomial(1, p, H.shape[1])

        # The syndrome of the stabilisers is calculated
        # from the dot product (modulo 2) between noise and H.
        syndrome = H@noise % 2

        # Use PyMatching to run the decoder (MWPM algorithm).
        correction = matching.decode(syndrome)

        # The error is the sum (modulo 2) between noise and correction.
        error = (noise + correction) % 2

        if np.any(error@logicals.T % 2):
            num_errors += 1

    return num_errors

# noisy parity check measurements
def num_decoding_failures_noisy_syndromes(H, logicals, p, q, num_trials, repetitions):
    """
    H -->  matrix that describes the lattice (Hz or Hx)
    logicals --> X-logical or Z-logical operators matrix
    p --> list of the error thresholds to test
    num_trials --> the number or repetitions of the decoder
    p --> The probability that a qubit has error
    q --> The probability that the error syndrome is measured incorrectly
    repetitions --> The number of repetitions T (or the number that the stabilisers measurements are repeated)
    It can be seen as the number of vertical levels during the decoding
    """
    # spacelike_weights refers to the weight of edges in the matching graph (default is None which means weight = 1.0).
    #  timelike_weights gives the weight of timelike edges.
    matching = Matching(H, spacelike_weights=np.log((1-p)/p),
                repetitions=repetitions, timelike_weights=np.log((1-q)/q))

    num_stabilisers, num_qubits = H.shape

    num_errors = 0

    for i in range(num_trials):

        # qubit error
        noise_new = (np.random.rand(num_qubits, repetitions) < p).astype(np.uint8)
        noise_cumulative = (np.cumsum(noise_new, 1) % 2).astype(np.uint8)
        noise_total = noise_cumulative[:,-1]

        # syndrome error
        syndrome = H@noise_cumulative % 2
        syndrome_error = (np.random.rand(num_stabilisers, repetitions) < q).astype(np.uint8)

        # Last round has perfect measurement to guarantee even parity.
        syndrome_error[:,-1] = 0

        noisy_syndrome = (syndrome + syndrome_error) % 2

        # Convert to difference syndrome (the latest).
        noisy_syndrome[:,1:] = (noisy_syndrome[:,1:] - noisy_syndrome[:,0:-1]) % 2

        correction = matching.decode(noisy_syndrome)

        error = (noise_total + correction) % 2

        assert not np.any(H@error % 2)

        if np.any(error@logicals.T % 2):
            num_errors += 1

    return num_errors

# A function that first run the decoding and then creates the appropriate plots.
def decode_and_plot(rsnt,ps,xz,noise):

    """
    rsnt is a list that containts r, s, num_cuts, num_trials

    rsnt[0] --> r
    rsnt[1] --> s
    rsnt[2] --> num_cuts
    rsnt[3] --> num_trial
    ps --> is a list with all the physical error rates to test
    xz --> is x or z depending on the type of error (Pauli-X or Pauli-Z)
    noise --> 0 or 1 for noiseless and noisy parity check measurements respectively
    """

    np.random.seed(2)

    path = f"Hyperbolic/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}/Stabilisers"
    path_log = f"Hyperbolic/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}/Logicals"

    # Take the all the csv files and sort them by size.
    # It is reasonable to assume that file '18.csv' will be less in size than file '76.csv'
    # In that way we sort the files by accenting qubits.
    Lxz = sorted(glob.glob(path + "/" + xz + "/" + "*.csv"), key=os.path.getsize)

    # The elements in Lxz have the form 'Hyperbolic/{6,4}/8/Stabilisers/x/16.csv'
    # So we keep just the last part (16.csv) by splitting the string in /
    Lxz = [i.split("/")[-1] for i in Lxz]

    # This list keeps all logical errors from different code sizes (e.g. 16.csv, 72.csv, 85.csv ...)
    all_log_errors = []

    # Set the timer before decoding
    start = process_time()

    # Measure CPU percentage before decoding
    psutil.cpu_percent()

    for L in Lxz:

        print(f"Simulating f={L}...")

        # Take the H matrix from file.
        file_name = path + "/" + xz + "/" + L
        H = np.genfromtxt(file_name, delimiter=',')
        H = scipy.sparse.csr_matrix(H)

        # Take the logical operators from file.
        file_name = path_log + "/" + xz + "/" + L
        log = np.genfromtxt(file_name, delimiter=',')
        log = scipy.sparse.csr_matrix(log)

        # A list that holds the number of errors for every probability p.
        log_errors = []

        # Take the code's distance
        file_name = path_log + "/" + xz + "/" + "d" + L
        distance = np.genfromtxt(file_name)

        # Run the decoding for every point in the linspace.
        for p in ps:
            if noise:
                num_errors = num_decoding_failures_noisy_syndromes(H, log, p, p, rsnt[3], int(distance))
                log_errors.append(num_errors/rsnt[3])
            else:
                num_errors = num_decoding_failures(H, log, p, rsnt[3])
                log_errors.append(num_errors/rsnt[3])

        all_log_errors.append(np.array(log_errors))

    # Measure time, CPU and RAM after the decoding
    stop = process_time()
    CPU = psutil.cpu_percent()
    RAM = ram_usage()

    metadata = {}
    metadata['CPU'] = CPU
    metadata['RAM'] = RAM
    metadata['TIME'] = stop - start

    # Find the intersection points.

    # This is a line that we use as a reference. It will be the line that comes up after the decoding and has the highest number of qubits.
    # We will search for intersection points between this line and every other line.
    reference_line = LineString(np.column_stack((ps, all_log_errors[-1])))

    all_intersection_points = []
    all_lines = all_log_errors.copy()[0:-1]

    for line_points in all_lines:
        line = LineString(np.column_stack((ps, line_points)))
        intersection = reference_line.intersection(line)
        #all_intersection_points.append(re.sub("[^\d\.\,\s]", "", intersection.wkt))
        all_intersection_points.append(intersection)

    plt.figure()

    for L, logical_errors in zip(Lxz, all_log_errors):

        # Split the file name relative to dot
        # e.g. 55.csv will be ['55', 'csv']
        parts = L.split(".")

        plt.plot(ps, logical_errors, 'o-', label=f"Q={parts[0]}")

    if xz == 'x':
        xz_opp = 'z' # xz_opp means opposite of xz.
    elif xz == 'z':
        xz_opp = 'x'

    if noise:
        plt_title = f"Simulation of {{{rsnt[0]},{rsnt[1]}}} for {rsnt[2]} boundary cuts\ncorrecting for {xz_opp} logical errors (noisy)."
        noise_type = 'noisy'
    else:
        plt_title = f"Simulation of {{{rsnt[0]},{rsnt[1]}}} for {rsnt[2]} boundary cuts\ncorrecting for {xz_opp} logical errors (noiseless)."
        noise_type = 'noiseless'

    plt.title(plt_title)
    plt.xlabel("Physical error rate")
    plt.ylabel("Logical error rate")
    plt.legend(loc=0)
    plt.tight_layout()

    # Save the figure.
    path_fig = f"Figures/{{{rsnt[0]},{rsnt[1]}}}/{rsnt[2]}"
    filepath = path_fig + "/correct_" + xz_opp + f"_errors_{noise_type}.png"
    Path(path_fig).mkdir(parents=True, exist_ok=True)
    plt.savefig(filepath)

    # Save the intersection points
    filepath = path_fig + "/dec_data_" + xz_opp + f"_errors_{noise_type}.csv"
    np.savetxt(filepath, np.array(all_log_errors))
    filepath = path_fig + "/intersection_points_" + xz_opp + f"_{noise_type}.txt"

    with open(filepath, 'w') as f:
        for item in all_intersection_points:
            f.write("%s\n" % item)

    # Save the time, CPU and RAM data
    filepath = path_fig + "/metadata_" + xz_opp + f"_{noise_type}.txt"
    with open(filepath, 'w') as file:
        file.write(json.dumps(metadata))

if __name__ == "__main__":

    parser = ArgumentParser(description="Run the decoder for hyperbolic codes.")
    parser.add_argument("--r", "-r", type=int, help="Number of r-gons.")
    parser.add_argument("--s","-s", type=int, help="Number of r-gons that meet at a vertex.")
    parser.add_argument("--num_cuts","-n", type=int, help="Number of cuts on the boundary.")
    parser.add_argument("--trials", "-t", type=int, help="Number of times to run the decoder for the same error probability.")
    parser.add_argument("--low","-l", type=float, help="Probability's lower limit.")
    parser.add_argument("--up","-u", type=float, help="Probability's higher limit.")
    parser.add_argument("--pieces","-p", type=int, help="Cut the interval between upper and lower probability limit into p pieces.")
    parser.add_argument("--noise","-e", nargs='?', type=int, default=0, help="Noisy syndrome measurements (0) or not (1).")
    arguments = parser.parse_args()

    r = arguments.r
    s = arguments.s
    num_cuts = arguments.num_cuts
    num_trials = arguments.trials
    noise = arguments.noise
    ps = np.linspace(arguments.low, arguments.up, arguments.pieces)

    """ r = 6
    s = 6
    num_cuts = 4
    num_trials = 1000
    noise = 0
    ps = np.linspace(0.16, 0.2, 10) """

    rsnt = [r,s,num_cuts,num_trials]

    decode_and_plot(rsnt,ps,'x',noise)
    decode_and_plot(rsnt,ps,'z',noise)


# Hockeyspelare
# namn     Peter Forsberg
# jersey   21
# team     Colorado

# import os
# with open("namn.txt", "r") as f:
#     for line in f:
#         print(line.replace("\n", ""))

import json
from datetime import datetime
from os.path import exists

def default(obj):
    if isinstance(obj, datetime):
        return { '_isoformat': obj.isoformat() }
    raise TypeError('...')

def object_hook(obj):
    _isoformat = obj.get('_isoformat')
    if _isoformat is not None:
        return datetime.fromisoformat(_isoformat)
    return obj

class Player:
    Namn = ""
    Team = ""
    Jersey = ""
    LastModified = None

    @classmethod
    def create(cls,in_dict:dict):
        a = cls()
        a.__dict__ = in_dict
        return a
listOfPlayers = []
#läsa players.json och skapa Player med dom värdena
# stoppa in i listan
if exists("players.json"):
    with open("players.json", "r") as f:
        data = json.load(f,object_hook=object_hook)
        for dic in data:
            p = Player.create(dic)
            listOfPlayers.append(p)

while True:
    print("1. Skapa ny")
    print("2. Lista alla ")
    selection = input("Val:")
    if selection == "1":
        player = Player()
        player.Namn = input("Ange namn:")
        player.Jersey = int(input("Ange jersey:"))
        player.Team = input("Ange team:")
        player.LastModified = datetime.now()
        listOfPlayers.append(player)
        with open('players.json', 'w') as json_file:
            json.dump([b.__dict__ for b in listOfPlayers], json_file,default=default)
    if selection == "2":
        for p in listOfPlayers:
            print(f"{p.Namn} Nummer: {p.Jersey}  {p.Team}")


from collections import deque

def solution(priorities, location):
    answer = 0
    d = deque(priorities)
    priorities.sort(reverse=True)
    while d:
        while d[0] != priorities[0]:
            d.rotate(-1)
            location -= 1
            if location < 0:
                location += len(priorities)
        answer += 1
        del priorities[0]
        d.popleft()
        if not location:
            return answer
        location -= 1


# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json

import yaml

if hasattr(yaml, 'CSafeLoader'):
    yaml_loader = yaml.CSafeLoader
else:
    yaml_loader = yaml.SafeLoader

class HeatYamlLoader(yaml_loader):
    pass

def _construct_yaml_str(self, node):
    # Override the default string handling function
    # to always return unicode objects
    return self.construct_scalar(node)

HeatYamlLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
# Unquoted dates like 2013-05-23 in yaml files get loaded as objects of type
# datetime.data which causes problems in API layer when being processed by
# openstack.common.jsonutils. Therefore, make unicode string out of timestamps
# until jsonutils can handle dates.
HeatYamlLoader.add_constructor(
    u'tag:yaml.org,2002:timestamp', _construct_yaml_str
)

def parse(tmpl_str):
    """Takes a string and returns a dict containing the parsed structure.

    This includes determination of whether the string is using the
    JSON or YAML format.
    """
    # strip any whitespace before the check
    tmpl_str = tmpl_str.strip()
    if tmpl_str.startswith('{'):
        tpl = json.loads(tmpl_str)
    else:
        try:
            tpl = yaml.load(tmpl_str, Loader=HeatYamlLoader)
        except yaml.YAMLError:
            # NOTE(prazumovsky): we need to return more informative error for
            # user, so use SafeLoader, which return error message with template
            # snippet where error has been occurred.
            try:
                tpl = yaml.load(tmpl_str, Loader=yaml.SafeLoader)
            except yaml.YAMLError as yea:
                raise ValueError(yea)
        else:
            if tpl is None:
                tpl = {}
    # Looking for supported version keys in the loaded template
    if not (
        'HeatTemplateFormatVersion' in tpl
        or 'heat_template_version' in tpl
        or 'AWSTemplateFormatVersion' in tpl
    ):
        raise ValueError("Template format version not found.")
    return tpl


from mrjob.job import MRJob
from mrjob.step import MRStep

class T10M(MRJob):
    def mapper_aggregate(self, _, line):
        try:
            fields = line.split(",")
            if len(fields) == 9:
                miner = fields[2]
                size = int(fields[4])
                yield miner, size
        except:
            pass

    def reducer_aggregate(self, key, values):
        yield None, (key, sum(values))

    def reducer_sort(self, _, values):
        sorted_values = sorted(values, reverse=True, key=lambda x: x[1])

        for v in sorted_values[:10]:
            yield v[0], v[1]

    def steps(self):
        return [MRStep(mapper=self.mapper_aggregate,
                       reducer=self.reducer_aggregate),
                MRStep(reducer=self.reducer_sort)]

if __name__ == '__main__':
    T10M.run()
    #job_1648683650522_5938
	#http://andromeda.student.eecs.qmul.ac.uk:8088/proxy/application_1648683650522_5938/


#coding=utf-8
from lxml import etree
import urllib.request
import csv
import xlwt

first_url = 'http://www.csres.com'
#def get_url(url,headers):
#获得工标网首页html
html = urllib.request.urlopen(first_url)
response = html.read().decode('gbk')
#将首页转换成etree格式
page = etree.HTML(response)
#在首页中找到'分类'、'新标准'、'标准公告'、'标准动态'、'论坛'、'文档'的URL
second_urls = page.xpath('//a[@class="a_top_blue"]/@href')
full_second_urls = []
for url_2 in second_urls[0:5]:
    second_url = first_url + str(url_2)
    full_second_urls.append(second_url)
full_second_urls.append(second_urls[5])
#解析'分类'、'新标准'、'标准公告'、'标准动态'、'论坛'、'文档'的html,将其转换为etree格式
classify_html = urllib.request.urlopen(full_second_urls[0])
classify_page = etree.HTML(classify_html.read().decode('gbk'))
#获得'分类'页面下的所有url
full_third_urls = []
third_urls = classify_page.xpath('//a[@class="sh14lian"]/@href')
#获得'分类'页面下的各url的文本
third_urls_text = classify_page.xpath('//a[@class="sh14lian"]/text()')
#循环获取url
for url_3 in third_urls:
    third_url = first_url + str(url_3)
    full_third_urls.append(third_url)
    #print(url_3)
#循环获取url的文本
full_third_urls_text = []
for url_3_text in third_urls_text:
    full_third_urls_text.append(url_3_text)
    #print(url_3_text)
del full_third_urls[23:25]
#得到第三个页面的url与text的对应关系，即字典
dic_third_page = dict(zip(full_third_urls_text,full_third_urls))
#print(dic_third_page)
#获得'A综合'下的url及各url的文本
A_html = urllib.request.urlopen(full_third_urls[0])
A_page = etree.HTML(A_html.read().decode('gbk'))
#获得'A综合'页面下的所有url
full_A_urls = []
A_urls = A_page.xpath('//a[@class="sh14lian"]/@href')
#获得'A综合'页面下的各url的文本
A_urls_text = A_page.xpath('//a[@class="sh14lian"]/text()')
#循环获取url
for url_A in A_urls:
    url_A = first_url + str(url_A)
    full_A_urls.append(url_A)
    #print(url_A)
#print(len(full_A_urls))
#循环获取url的文本
#del full_A_urls[29,43,44,49,50,56,64,65,70,71]
#print(len(full_A_urls))
full_A_urls_text = []
for url_A_text in A_urls_text:
    full_A_urls_text.append(url_A_text)
    #print(url_A_text)
#print(len(full_A_urls_text))
#del full_third_urls[23:25]
#得到'A综合'页面的url与text的对应关系，即字典
'''dic_A_page = dict(zip(full_A_urls_text,full_A_urls))
dic_A_page.pop('\xa0')'''

#print(dic_A_page)
#print(len(dic_A_page))
#获得标准列表的url
finally_html = urllib.request.urlopen(full_A_urls[0])
finally_page = etree.HTML(finally_html.read().decode('gbk'))
#获得标准列表的表头
list_head = []
list_heads = finally_page.xpath('//thead[@class="th1"]/tr/th/text()')
for item in list_heads:
    list_head.append(item)
#print(item)
#获得标准的标准编号、标准名称、ICS分类、发布部门、发布日期、实施日期
standard_list = []
standard_nums = finally_page.xpath('//thead/tr[@bgcolor="#FFFFFF"]/@title')
for standard_num in standard_nums:
    standard_list.append(standard_num)
#print(len(standard_list))
print(len(standard_list[0]))
#print(standard_list[0][115:125])
#print(standard_list)
#获得标准的标准状态

state_list = []
states = finally_page.xpath('//thead/tr[@bgcolor="#FFFFFF"]/td[last()]/font/text()')
for state in states:
    state_list.append(state)
    #print(state)
print(len(state_list))
#print(full_third_urls_text)
#print(len(full_third_urls_text))
#print(full_third_urls)
#print(len(full_third_urls))
#print(full_second_urls)
    #str(standard_list[0][3:19])

#将数据写入excel文件
workbook=xlwt.Workbook(encoding='utf-8')
booksheet=workbook.add_sheet('标准', cell_overwrite_ok=True)

DATAS = (('编号','标题','ICS分类','发布部门','发布日期','实施日期','状态'),
         ('DB21/T 2457-2015',standard_list[0][24:36],standard_list[0][54:63],standard_list[0][83:93],standard_list[0][99:110],standard_list[0][115:125],state_list[0]))
for i,row in enumerate(DATAS):
    for j,col in enumerate(row):
        booksheet.write(i,j,col)
workbook.save('grade.xls')


import requests
import os
from lxml import html
import pickle

balance_sheet_url = 'https://economictimes.indiatimes.com/hdfc-bank-ltd/balancesheet/companyid-{}.cms'
balance_sheet_output_file = os.path.join(os.path.dirname(__file__), 'balance_sheet_data.txt')

bank_nifty = {  #'yes_bank' :'16552',
                # 'icici_bank' :'9194',
                'federal_bank' :'9211',
                'pnb_bank' :'11585',
                'rbl_bank' :'7750',
                'idfc_bank' :'62245',
                'bob_bank' : '12040',
                'kotak_bank' : '12161',
                'hdfc_bank' : '9195',
                'axis_bank' : '9175',
                'sbin' : '11984',
                'indusind' : '9196'}
et_codes = open('et_codes', 'rb')
codes = pickle.load(et_codes)
# print(codes)
output_file = open(balance_sheet_output_file, 'a')

for bank, code in codes.items():
    url = balance_sheet_url.format(code)
    response = requests.get(url)
    tree = html.fromstring(response.text)
    try :
        Data = tree.xpath('//td/text()')
        NetWorth = Data.index('Net Worth');  # print (NetWorth)
        SecuredLoan = Data.index('Secured Loan');
        TotalYears = SecuredLoan - NetWorth
        TotalLiabilities = Data.index('TOTAL LIABILITIES');  # print (TotalLiabilities)
        TotalCurrentAssests = Data.index('Total Current Assets');  # print (TotalCurrentAssests)
        TotalCurrentLiabilities = Data.index('Total Current Liabilities');  # print (TotalCurrentLiabilities)
        NETCURRENTASSETS = Data.index('NET CURRENT ASSETS');  # print (NETCURRENTASSETS)
        TOTALASSETS = Data.index('TOTAL ASSETS(A+B+C+D+E)');  # print (TOTALASSETS)
    except Exception as err:
        print(err)
        continue
    years = ['2019-04-01', '2018-04-01', '2017-04-01', '2016-04-01', '2015-04-01', ]

    detail_list = []
    try :
        for j in range(1, len(years) + 1):
            detail = {
                'year': years[j - 1],
                'stock_name': bank,
                # 'code':self.company_ids[self.i],
                'net_worth': Data[NetWorth + j],
                'total_liabilities': Data[TotalLiabilities + j],
                'total_current_assests': Data[TotalCurrentAssests + j],
                'net_current_assets': Data[NETCURRENTASSETS + j],
                'total_assets': Data[TOTALASSETS + j]
            }
            detail_list.append(detail)
    except Exception as err:
        print("------------->",bank, err)
    output_file.write(str(detail_list))
    output_file.write('\n')

    print(detail_list)


import json
from json import JSONDecodeError

import requests

from regear_functions import Spreadsheet

def get_guildmember_list():
	respond = requests.get(
		'https://gameinfo.albiononline.com/api/gameinfo/guilds/6g1WhBpsQ_W2eldCMtuTrw/members')
	return respond

def get_last_death(playerId):
	respond = requests.get(
		f'https://gameinfo.albiononline.com/api/gameinfo/players/{playerId}/deaths')
	try:
		outp = respond.json()[0]['EventId']
	except IndexError:
		outp = None
	except JSONDecodeError:
		outp = None
	print(f'Last death : {outp}')
	return outp

def main_writer_json(outp_get_guildmember_list, SpreadsheetVariable):
	try:
		raw_json = outp_get_guildmember_list.json()
		hot_json = {k['Name']: k['Id'] for k in raw_json}
		events_json = {k: get_last_death(hot_json[k]) for k in hot_json.keys()}
	except JSONDecodeError:
		return
	print(f'events_json: {events_json}')

	with open('guild_members.json', 'w') as handler:
		json.dump(hot_json, handler, indent=4)

	with open('guild_events.json', 'r') as handler:
		old_events = json.load(handler)
	print(f'old_events: {old_events}')

	new_events = old_events

	for i in events_json:
		op = events_json[i]
		print(f'Processing {op}')
		try:
			if old_events[i] != events_json[i] and op is not None:
				print(f'{op} IS NOT IN OLD_EVENTS!!! APPENDING IT INTO SPREADSHEET!')
				try:
					SpreadsheetVariable.push(str(op))
				except TimeoutError:
					try:
						SpreadsheetVariable.push(str(op))
					except TimeoutError:
						SpreadsheetVariable.push(str(op))
				new_events[i] = op
				print(new_events)
		except KeyError:
			continue

	with open('guild_events.json', 'w') as handler:
		print('IN ENCLOSING GUILD_EVENTS_JSON')
		json.dump(new_events, handler, indent=4)

if __name__ == "__main__":

	outp_get_guildmember_list = get_guildmember_list()
	raw_json = outp_get_guildmember_list.json()
	hot_json = {k['Name']: k['Id'] for k in raw_json}
	events_json = {k: get_last_death(hot_json[k]) for k in hot_json.keys()}
	with open('guild_events.json', 'w') as handler:
		json.dump(events_json, handler, indent=4)

	spreadsheet = Spreadsheet()
	print(spreadsheet.url())
	while True:
		main_writer_json(get_guildmember_list(), spreadsheet)


from typing import Optional
import asyncio
import datetime as dt

import discord

from utils.lib import utc_now

class TextSession:
    """
    Represents an ongoing text session for a single member.

    Attributes
    ----------
    userid
    guildid
    start_time
    total_messages
    total_words
    total_periods
    this_period_start
    this_period_messages
    this_period_words
    timeout
    """
    __slots__ = (
        'userid', 'guildid',
        'start_time', 'end_time',
        'total_messages', 'total_words', 'total_periods',
        'this_period_start', 'this_period_messages', 'this_period_words',
        'last_message_at', 'timeout_task',
        'finish_callback', 'finish_task', 'finished', 'finished_at',
    )

    # Length of a single period
    # period_length = 5 * 60
    period_length = 10
    timeout_length = 2 * period_length

    # Maximum length of a session
    # session_length = 60 * 60
    session_length = 120

    def __init__(self, userid, guildid, start_time):
        self.userid = userid
        self.guildid = guildid

        self.start_time = start_time
        self.end_time = start_time + dt.timedelta(seconds=self.session_length)

        self.total_messages = 0
        self.total_words = 0
        self.total_periods = 0

        self.this_period_start = start_time
        self.this_period_messages = 0
        self.this_period_words = 0

        self.last_message_at = None
        self.timeout_task = None

        self.finish_callback = None
        self.finish_task = None
        self.finished = asyncio.Event()
        self.finished_at = None

    @property
    def duration(self) -> int:
        if self.start_time is None:
            raise ValueError("Cannot take duration of uninitialised session!")

        end = self.finished_at or utc_now()
        return int((end - self.start_time).total_seconds())

    def __repr__(self):
        return (
            "("
            "{self.__class__.__name__}: "
            "userid={self.userid}, "
            "guildid={self.guildid}, "
            "start_time={self.start_time}, "
            "end_time={self.end_time}, "
            "total_messages={self.total_messages}, "
            "total_words={self.total_words}, "
            "total_periods={self.total_periods}, "
            "last_message_at={self.last_message_at}, "
            "finished_at={self.finished_at}"
            ")"
        ).format(self=self)

    @classmethod
    def from_message(cls, message: discord.Message):
        """
        Instantiate a new TextSession from an initial discord message.

        Does not process the given message.
        """
        if not message.guild:
            raise ValueError("Cannot initialise from message outside of Guild context!")
        self = cls(message.author.id, message.guild.id,  message.created_at)
        return self

    def process(self, message: discord.Message):
        """
        Process a message into the session.
        """
        if not message.guild:
            return

        if (message.author.id != self.userid) or (message.guild.id != self.guildid):
            raise ValueError("Invalid attempt to process message from a different member!")

        # Identify if we need to start a new period
        start = self.this_period_start
        if start is not None and (message.created_at - start).total_seconds() < self.period_length:
            self.this_period_messages += 1
            self.this_period_words += len(message.content.split())
        else:
            self.roll_period()
            self.this_period_start = message.created_at
            self.this_period_messages = 1
            self.this_period_words = len(message.content.split())
        self.last_message_at = message.created_at

        # Update the session expiry
        self._reschedule_timeout(self.last_message_at + dt.timedelta(seconds=self.timeout_length))

    def roll_period(self):
        """
        Add pending stats from the current period, and start a new period.
        """
        if self.this_period_messages:
            self.total_messages += self.this_period_messages
            self.total_words += self.this_period_words
            self.total_periods += 1
        self.this_period_start = None

    async def finish(self):
        """
        Finalise the session and set the finished event. Idempotent.

        Also calls the registered finish callback, if set.
        """
        if self.finished.is_set():
            return

        self.roll_period()
        self.finished_at = self.last_message_at or utc_now()

        self.finished.set()
        if self.finish_callback:
            await self.finish_callback(self)

    async def cancel(self):
        """
        Cancel this session.

        Does not execute the finish_callback.
        """
        ...

    def on_finish(self, callback):
        """
        Register a callback coroutine to be executed when the session finishes.
        """
        self.finish_callback = callback

    async def _timeout(self, diff):
        if diff > 0:
            await asyncio.sleep(diff)
        await asyncio.shield(self.finish())

    def _reschedule_timeout(self, target_time):
        """
        Schedule the finish timeout for the given target time.
        """
        if self.finished.is_set():
            return
        if self.finish_task and not self.finish_task.cancelled():
            self.finish_task.cancel()

        target_time = min(self.end_time, target_time)
        dist = (target_time - utc_now()).total_seconds()
        self.finish_task = asyncio.create_task(self._timeout(dist))


import torch

def evaluate_gan(generator, test_loader, device):
    generator.eval()
    total_loss = 0
    criterion = torch.nn.MSELoss()

    with torch.no_grad():
        for images, voxels in test_loader:
            images = images.to(device)
            voxels = voxels.to(device)
            generated_voxels = generator(images)
            loss = criterion(generated_voxels, voxels)
            total_loss += loss.item()

    generator.train()
    return total_loss / len(test_loader)


#!/usr/bin/env python

# spinal_cord_classification.py performs several types of machine learning techniques to explore the relationship between spinal cord shape and clinical outcomes in MS

import nibabel as nib
import numpy as np
import glob
import os
import sys
import csv
import scipy.io
import glob
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestCentroid
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.cross_decomposition import PLSRegression
from sklearn.metrics import confusion_matrix

# Visualize data with PCA
X_deformation = np.genfromtxt('deformation_data.csv',dtype='str', delimiter=',')
X_deformation = X_deformation[:,1:]
X_deformation = X_deformation.astype(float)
X_jacobian = np.genfromtxt('jacobian_data.csv',dtype='str', delimiter=',')
X_jacobian = X_jacobian[:,1:]
X_jacobian = X_jacobian.astype(float)
X_all = np.hstack((X_deformation, X_jacobian))
X_patients = X_all[32:,:]

Y = np.loadtxt('EDSS.csv',dtype='str',delimiter=',')
Y = Y[:,1:]
Y = Y.astype(float)
Y_disease_course = Y[:,1]
Y_disease_course_patients = (Y_disease_course>1.5)[32:]
Y_edss = Y[:,0]
Y_edss_severity = (Y_edss >= 3)
Y_patient = (Y_disease_course >0)

model = PCA(n_components=5)
X_new = model.fit_transform(X_all)
plt.scatter(X_new[:,0], X_new[:,1], c=Y_edss, cmap='hot')
plt.colorbar()
plt.show()
print(model.explained_variance_ratio_)

# Try centroid method
Y = Y_disease_course_patients[:,None]
X = X_new[32:,:]
YX = np.hstack((Y,X))
np.random.shuffle(YX)
X_shuffle = YX[:,1:]
Y_shuffle = YX[:,0]
Y_shuffle = np.squeeze(Y_shuffle)

halfway_pt = 65
Xtrain = X_shuffle[0:halfway_pt,:]
Ytrain = Y_shuffle[0:halfway_pt]
Xvalidate = X_shuffle[halfway_pt:,:]
Yvalidate = Y_shuffle[halfway_pt:]

model = NearestCentroid()
model.fit(Xtrain, Ytrain)
Ypredict = model.predict(Xvalidate)

confusion_matrix(Yvalidate, Ypredict)

accuracy = sum(np.equal(Ypredict,Yvalidate))/float(len(Yvalidate))
print(accuracy)

# Try SVM
iterations = 1000
confusion_percentages = np.zeros((2,2,iterations))
for i in range(0,iterations):
    Y = Y_edss_severity[:,None]
    X = X_all
    YX = np.hstack((Y,X))
    np.random.shuffle(YX)
    X_shuffle = YX[:,1:]
    Y_shuffle = YX[:,0]
    Y_shuffle = np.squeeze(Y_shuffle)

    halfway_pt = 80
    Xtrain = X_shuffle[0:halfway_pt,:]
    Ytrain = Y_shuffle[0:halfway_pt]
    Xvalidate = X_shuffle[halfway_pt:,:]
    Yvalidate = Y_shuffle[halfway_pt:]

    model = SVC()
    model.fit(Xtrain, Ytrain)
    Ypredict = model.predict(Xvalidate)

    confusion = confusion_matrix(Yvalidate, Ypredict)
    confusion_percentage = np.divide(confusion, np.sum(confusion,0).astype(float))
    confusion_percentages[:,:,i] = confusion_percentage

avg_confusion = np.mean(confusion_percentages, axis=2)
print(avg_confusion)

# Try PLS

iterations = 1000
confusion_percentages = np.zeros((2,2,iterations))
for i in range(0,iterations):
    Y = Y_edss_severity[:,None]
    X = X_all
    YX = np.hstack((Y,X))
    np.random.shuffle(YX)
    X_shuffle = YX[:,1:]
    Y_shuffle = YX[:,0]
    Y_shuffle = np.squeeze(Y_shuffle)

    halfway_pt = 80
    Xtrain = X_shuffle[0:halfway_pt,:]
    Ytrain = Y_shuffle[0:halfway_pt]
    Xvalidate = X_shuffle[halfway_pt:,:]
    Yvalidate = Y_shuffle[halfway_pt:]

    model = PLSRegression(n_components = 5)
    model.fit(Xtrain, Ytrain)
    Ypredict = model.predict(Xvalidate)
    Ypredict = np.squeeze(Ypredict)
    Ypredict = (Ypredict >= 0.5)
    Yvalidate = Yvalidate.astype(int)
    confusion = confusion_matrix(Yvalidate, Ypredict)
    confusion_percentage = np.divide(confusion, np.sum(confusion,0).astype(float))
    confusion_percentages[:,:,i] = confusion_percentage

    accuracy = sum(Ypredict == Yvalidate)/float(len(Yvalidate))

avg_confusion = np.mean(confusion_percentages, axis=2)
print(avg_confusion)


from __future__ import division
import os
import cv2
import dlib
from part_of_face import Part

class FaceTracking(object):
    """
    얼굴 좌표 인식 후 반환
    """

    def __init__(self):
        self.frame = None

        self.face_left = None
        self.face_right = None
        self.face_top = None
        self.face_bottom = None

        self._face_detector = dlib.get_frontal_face_detector()

        cwd = os.path.abspath(os.path.dirname(__file__))
        model_path = os.path.abspath(os.path.join(cwd, "trained_models/shape_predictor_68_face_landmarks.dat"))
        self._predictor = dlib.shape_predictor(model_path)

    @property
    def face_located(self):
        """Check that the face have been located"""
        try:
            int(self.face_left.x)
            int(self.face_left.y)
            int(self.face_right.x)
            int(self.face_right.y)

            int(self.face_top.x)
            int(self.face_top.y)
            int(self.face_bottom.x)
            int(self.face_bottom.y)
            return True
        except Exception:
            return False

    def refresh(self, frame):
        """Refreshes the frame and analyzes it.

        Arguments:
            frame (numpy.ndarray): 원본 웹캠 영상
        """
        self.frame = frame
        self._analyze()

    # 좌표
    def face_left_coords(self):
        """Returns the coordinates of the left pupil"""
        if self.face_located:
            x = self.face_left.x
            y = self.face_left.y
            return (x, y)

    # 좌표
    def face_right_coords(self):
        """Returns the coordinates of the left pupil"""
        if self.face_located:
            x = self.face_right.x
            y = self.face_right.y
            return (x, y)

    def face_top_coords(self):
        """Returns the coordinates of the left pupil"""
        if self.face_located:
            x = self.face_top.x
            y = self.face_top.y
            return (x, y)

    def face_bottom_coords(self):
        """Returns the coordinates of the left pupil"""
        if self.face_located:
            x = self.face_bottom.x
            y = self.face_bottom.y
            return (x, y)

    def _analyze(self):
        """Detects the face and initialize Eye objects"""
        # 원본 웹캠 frame을 흑백으로 변환
        frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
        # dlib의 정면 얼굴 검출기
        faces = self._face_detector(frame)

        try:
            landmarks = self._predictor(frame, faces[0])

            self.face_left = Part(landmarks, 0)
            self.face_right = Part(landmarks, 1)
            self.face_top = Part(landmarks, 2)
            self.face_bottom = Part(landmarks, 3)

        except IndexError:
            self.face_left = None
            self.face_right = None
            self.face_top = None
            self.face_bottom = None

    def annotated_frame(self):
        """
        얼굴 point 4개 프레임 반환
        """
        frame = self.frame.copy()

        if self.face_located:
            color = (0, 255, 0)
            face_left_x, face_left_y = self.face_left.x , self.face_left.y
            face_right_x, face_right_y = self.face_right.x , self.face_right.y
            face_top_x, face_top_y = self.face_top.x, self.face_top.y
            face_bottom_x, face_bottom_y = self.face_bottom.x, self.face_bottom.y

            cv2.circle(frame, (face_left_x, face_left_y) , 2,  color)
            cv2.circle(frame, (face_right_x, face_right_y),2,  color)
            cv2.circle(frame, (face_top_x, face_top_y), 2, color)
            cv2.circle(frame, (face_bottom_x, face_bottom_y), 2,  color)

        return frame


from aiogram import types
from aiogram.utils.keyboard import ReplyKeyboardBuilder, InlineKeyboardBuilder

EMPTY = types.ReplyKeyboardRemove()

def get_reply_keyboard(lst_name_btn: list, kb_type):
    builder = ReplyKeyboardBuilder()

    if kb_type == "row3":
        for i in lst_name_btn:
            builder.add(types.KeyboardButton(text=str(i)))
        if len(lst_name_btn) > 3:
            builder.adjust(round(len(lst_name_btn) / 3))
        return builder.as_markup(resize_keyboard=True)

def get_inline_keyboard(lst_name_btn, kb_type):
    builder = InlineKeyboardBuilder()

    if kb_type == "url":
        for key in lst_name_btn:
            builder.add(types.InlineKeyboardButton(text=str(key),
                                                   url=str(lst_name_btn[key])))
        return builder.as_markup(resize_keyboard=True)
    elif kb_type == "callback_data":
        for key in lst_name_btn:
            builder.add(types.InlineKeyboardButton(text=str(key),
                                                   callback_data=str(lst_name_btn[key])))
        return builder.as_markup(resize_keyboard=True)


from dataclasses import dataclass
from typing import List

import astropy.units as u
import numpy as np
from astropy.coordinates import spherical_to_cartesian

__all__ = ["FieldLines", "FieldLine"]

@dataclass
class FieldLine:
    """
    A single field line.

    Parameters
    ----------
    r : numpy.ndarray
        Radial coordinates.
    lat : numpy.ndarray
        Latitude coordinates **in radians**.
    lon : numpy.ndarray
        Longitude coordinates **in radians**.
    runit : astropy.units.Unit
        Radial coordinate unit.
    """

    r: u.Quantity
    lat: u.Quantity
    lon: u.Quantity

    def __init__(
        self, *, r: np.ndarray, lat: np.ndarray, lon: np.ndarray, runit: u.Unit
    ):
        self.r = r * runit
        self.lat = lat * u.rad
        self.lon = lon * u.rad
        self.runit = runit

    @property
    def xyz(self) -> u.Quantity:
        """
        Cartesian coordinates as a (n, 3) shaped array.
        """
        x, y, z = spherical_to_cartesian(self.r, self.lat, self.lon)
        return np.array([x, y, z]).T * self.r.unit

    @property
    def _rlatlon(self):
        """
        Spherical coordinates as a (n, 3) shaped array.
        """
        return np.column_stack(
            [
                self.r.to_value(self.runit),
                self.lat.to_value(u.rad),
                self.lon.to_value(u.rad),
            ]
        )

@dataclass
class FieldLines:
    """
    A container for multiple field lines.
    """

    flines: List[FieldLine]

    def __init__(self, xs: np.ndarray, runit: u.Unit):
        """
        Parameters
        ----------
        xs : list[numpy.ndarray]
            Field lines. Each array must have lon, lat, r columns in that
            order.
        runit : astropy.units.Unit
            Unit for radial coordinate.
        """
        self.flines = [
            FieldLine(r=x[:, 2], lat=x[:, 1], lon=x[:, 0], runit=runit) for x in xs
        ]
        self.runit = runit

    def __getitem__(self, i):
        return self.flines[i]

    def __len__(self):
        return len(self.flines)

    def __iter__(self):
        for fline in self.flines:
            yield fline

    def save(self, filename):
        """
        Save field lines to file.

        Parameters
        ----------
        filename : pathlib.Path, str
            File to save field lines to.

        Notes
        -----
        Arrays are saved using `numpy.savez_compressed`.
        """
        flines = {f"fline_{i}": fline._rlatlon for i, fline in enumerate(self.flines)}
        flines["runit"] = np.array(self.runit.to_string())
        np.savez_compressed(filename, **flines)

    @classmethod
    def load(cls, filename):
        """
        Load field lines from a file.

        The field lines must have been saved using the ``.save()`` method.

        Parameters
        ----------
        filename : pathlib.Path, str
            File to load field lines from.

        Returns
        -------
        flines : FieldLines
        """
        arrs = np.load(str(filename))
        if "runit" in arrs:
            runit = u.Unit(str(arrs["runit"]))
        else:
            # For backwards compatibility with versions < 0.4, assume
            # solar radii
            runit = u.R_sun

        fline_data = np.array([arrs[k][:, ::-1] for k in arrs if k != "runit"])
        return cls(fline_data, runit=runit)


from trajalign.traj import Traj
from trajalign.average import load_directory
from trajalign.average import nanMAD as MAD
from matplotlib import pyplot as plt
from scipy import stats
import numpy as np

def measure_lifetimes( list_of_trajectories , min_cutoff = 3 , max_cutoff = 125 ):

	#measure_lifetimes of the trajectories in the list

	lifetimes = []

	for element in list_of_trajectories :

		if ( element.frames()[ 0 ] > min_cutoff ) & ( element.frames()[ len( element ) - 1 ] < max_cutoff ) :

			lifetimes.append( element.lifetime() )

	return lifetimes

def average_lifetime( l , how ) :

	#average the lifetime of the trajectories

	if how == 'mean' or how == 'Mean' :

		a = [ np.mean( l ) , np.std( l ) / np.sqrt( len( l ) ) ]

	elif how == 'median' or how == 'Median' :

		a = [ np.nanmedian( l ) , MAD( l ) / np.sqrt( len( l ) ) ]

	else :

		print( 'how must be either [mM]ean or [mM]edian' )

	return a

def ctrlifetimes( path , dt , how , protein_name , pattern , cutoff = ( 1 , np.inf ) , comment_char = '%' , t_unit = 's' , coord_unit = 'pxl' , frames = 0 , coord = ( 1 , 2 ) , f = 3 ) :

	#wrapper to load the trajectories and compute their average_lifetime

	l = load_directory(
			path = path ,
			pattern = pattern ,
			comment_char = comment_char ,
			dt = dt ,
			t_unit = t_unit ,
			frames = frames ,
			coord = coord ,
			coord_unit = coord_unit ,
			f = f ,
			protein = protein_name
			)

	ml = measure_lifetimes( l , min_cutoff = cutoff[ 0 ] , max_cutoff = cutoff[ 1 ] )
	al = average_lifetime( ml , how )

	return( { 'average' : al , 'lifetimes' : ml , 'name' : protein_name } )

def significance( c1 , c2 ) :

	p = stats.mannwhitneyu( c1[ 'lifetimes' ] , c2[ 'lifetimes' ] )

	print( 'H0: the distribution of ' + c1[ 'name' ] + ' is equal to the distribution of ' + c2[ 'name' ] + '. P value = ' + str( p[ 1 ] ) )

	return p

def barplot( l , filename , figsize , ylabel , reference = [ ] , x_rotation = 0 ) :

	# l is a list of ctralifetimes outputs
	# it can be that a user inputs a tuple instead
	l = list( l )

	g , ( barplot ) = plt.subplots( 1 , 1 , gridspec_kw = { 'height_ratios' : [ 1 ] , 'width_ratios' : [ 1 ] } , figsize = figsize )

	#reference (f), if present,  is the first bar
	if len( reference ) :

		bplt_x = [ 0 ] + [ i + 1 for i in range( len( l ) ) ]
		bplt_y = [ reference[ 'average' ][ 0 ] ] + [ i[ 'average' ][ 0 ] for i in l ]
		bplt_yerr = [ reference[ 'average' ][ 1 ] ] + [ i[ 'average' ][ 1 ] for i in l ]
		bplt_names = [ reference[ 'name' ] ] + [ i[ 'name' ] for i in l ]

	else :

		bplt_x = [ i + 1 for i in range( len( l ) ) ]
		bplt_y = [ i[ 'average' ][ 0 ] for i in l ]
		bplt_yerr = [ i[ 'average' ][ 1 ] for i in l ]
		bplt_names = [ i[ 'name' ] for i in l ]

	barplot.bar( bplt_x , bplt_y , yerr = bplt_yerr , color = 'grey' , ecolor = 'black' )

	plt.subplot( barplot )

	xaxt = [ 0 ]

	plt.text( 0 , 1 , "n=" + str( len( reference[ 'lifetimes' ] ) ) , horizontalalignment = 'center' )

	for i in  range( len( l ) ):

		xaxt.append( i + 1 )

		plt.text( i + 1 , 1 , "n=" + str( len( l[ i ][ 'lifetimes' ] ) ) , horizontalalignment = 'center' )

	plt.ylabel( ylabel )
	barplot.set_xticks( xaxt )
	barplot.set_xticklabels( bplt_names , rotation = x_rotation )

	g.savefig( filename )

def write_csv( path , data ) :

	"""
	write_csv: write the lifetimes in the result dictionary outputs of ctrlifetimes, inputed as a tuple, into a csv file named path.
	The path includes the file name but not .csv, which is added automatically by the script
	"""

	f = open( path + '.csv' , "w" )

	header = ""
	data_len = []
	l = len( data )

	for i in range( l ) :

		header = header + data[ i ][ 'name' ] + ","
		data_len.append( len( data[ i ][ 'lifetimes' ] ) )

	f.write( header )
	f.write( "\n" )

	m = max( data_len )

	for i in range( m ) :

		line = ""

		for ii in range( l ) :

			try :

				line = line + str( data[ ii ][ 'lifetimes' ][ i ] ) + ","

			except :

				line = line + "" + ","

		f.write( line )
		f.write( "\n" )

	f.close()


from orml.dbbase import DBSession
from dbmodels.usermodel import User
from dbmodels.usertokenmodel import Usertoken
from dbmodels.secretmodel import Secret
from dbmodels.securitymodel import Security
from dbmodels.questionnairemodel import Questionnaire
from dbmodels.classifymodel import Classify
from dbmodels.interlocutionmodel import Interlocution
from dbmodels.optiontablemodel import Optiontable
from dbmodels.subjectmodel import Subject
from orml.userorml import Userorml
from orml.dbbase import engine
from sqlalchemy.sql import func
from sqlalchemy import desc
import time,datetime
import uuid
import demjson

class Questionnaireorml:

    def createquestionnaire(self,usertoken,title,classifyid,finishdate,intro):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            fdate = datetime.datetime.strptime(finishdate,'%Y-%m-%d')
            ndate = datetime.datetime.now()
            if session.query(User).filter_by(user_id=userid).count()>0 and fdate>ndate:
                session.add(Questionnaire(questionnaire_user_id=userid,questionnaire_title=title,questionnaire_intro
=intro,questionnaire_classify_id=classifyid,questionnaire_startdate=ndate,questionnaire_finishdate
=fdate,questionnaire_status=1,questionnaire_flag=0,questionnaire_complaint_count=0))
                session.commit()
                lastInsertID = session.query(func.max(Questionnaire.questionnaire_id)).one()[0]
                session.close()
                return lastInsertID
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def updatequestionnaire(self,token,questionid,title,finishdate,intro):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                fdate = datetime.datetime.strptime(finishdate,'%Y-%m-%d')
                session.query(Questionnaire).filter_by(questionnaire_id = questionid).update({"questionnaire_title":title,"questionnaire_finishdate":fdate,
                "questionnaire_intro":intro})
                session.commit()
                session.close()
                return True
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def myquestionnaires(self,usertoken,flag):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            if session.query(User).filter_by(user_id=userid).count()>0:
                questions = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_id).filter_by(questionnaire_user_id=userid,questionnaire_flag=flag).all()
                allValues = []
                for item in questions:
                    value = {}
                    value["subject_title"] = item[0]
                    value["subject_questionnaire_id"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return "false"
        except Exception as a:
            print(a)
            return False

    def classifycontents(self):
        session = DBSession()
        try:
            if session.query(Classify).count()>0:
                classifies = session.query(Classify.classify_id,Classify.classify_content).all()
                allValues = []
                for item in classifies:
                    value = {}
                    value["classify_id"] = item[0]
                    value["classify_content"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def deletequestionnaire(self,usertoken,questionnaireid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,usertoken)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Questionnaire).filter_by(questionnaire_id=questionnaireid).delete()
                session.commit()
                session.query(Subject).filter_by(subject_questionnaire_id
=questionnaireid).delete()
                session.commit()
                session.close()
                return True
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def searchquestionnaire(self,key,flag):
        session = DBSession()
        try:
            questions = []
            questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title,
            Questionnaire.questionnaire_startdate,Questionnaire.questionnaire_user_id,
            Questionnaire.questionnaire_finishdate).filter(Questionnaire.questionnaire_title.like('%'+key+'%')).filter_by(questionnaire_flag = flag).all()
            if len(questions)>0:
                allValues = []
                for item in questions:
                    value = {}
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    value["questionnaire_startdate"] = str(item[2])
                    username = session.query(User.user_name).filter_by(user_id = item[3]).one()[0]
                    value["questionnaire_username"] = username
                    value["questionnaire_finishdate"] = str(item[4])
                    allValues.append(value)
                return allValues
            else:
                return 'false'
        except Exception as a:
            print(a)
            return False

    def quicksearch(self,key,flag):
        session = DBSession()
        try:
            questions = []
            questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter(Questionnaire.questionnaire_title.like('%'+key+'%')).filter_by(questionnaire_flag = flag).all()
            if len(questions)>0:
                allValues = []
                for item in questions:
                    value = {}
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return 'false'
        except Exception as a:
            print(a)
            return False

    def allquestionnaires(self):
        session = DBSession()
        try:
            ids = session.query(Questionnaire.questionnaire_id).order_by(desc(Questionnaire.questionnaire_heat)).filter_by(questionnaire_flag=0).all()
            if len(ids)>0:
                allValues = []
                for i in ids:
                    questionnaireid = i[0]
                    value = {}
                    value["questionnaire_id"] = questionnaireid

                    title = session.query(Questionnaire.questionnaire_title).filter_by(questionnaire_id = questionnaireid).one()[0]
                    value["questionnaire_title"] = title

                    userid = session.query(Questionnaire.questionnaire_user_id).filter_by(questionnaire_id = questionnaireid).one()[0]
                    username = session.query(User.user_name).filter_by(user_id = userid).one()[0]
                    value["questionnaire_username"] = username

                    classifyid = session.query(Questionnaire.questionnaire_classify_id).filter_by(questionnaire_id = questionnaireid).one()[0]
                    classifycontent = session.query(Classify.classify_content).filter_by(classify_id = classifyid).one()[0]
                    value["classify_content"] = classifycontent

                    allValues.append(value)

                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def publishquestionnaire(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Questionnaire).filter_by(questionnaire_id=questionid).update({"questionnaire_flag":0,"questionnaire_heat":0,"questionnaire_status":1})
                session.commit()
                link = session.query(Questionnaire.questionnaire_link).filter_by(questionnaire_id=questionid).one()[0]
                result = {}
                result["questionnaire_link"] = link
                return result
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def getquestionnaireid(self,token):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:

                info = {}
                title = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_intro,
                Questionnaire.questionnaire_id).filter_by(questionnaire_user_id = userid).order_by(desc(Questionnaire.questionnaire_id)).first()
                info["questionnaire_title"] = title[0]
                info["questionnaire_intro"] = title[1]
                info["questionnaire_id"] = title[2]
                return info
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def savetemplate(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.add(Questionnaire(questionnaire_flag=1))
                session.commit()
                lastInsertQuestionID = session.query(func.max(Questionnaire.questionnaire_id)).one()[0]
                print(lastInsertQuestionID)
                result = session.query(Questionnaire.questionnaire_title,Questionnaire.questionnaire_intro,
                Questionnaire.questionnaire_classify_id).filter_by(questionnaire_id = questionid).one()
                ndate = datetime.datetime.now()
                session.query(Questionnaire).filter_by(questionnaire_id = lastInsertQuestionID).update({"questionnaire_user_id":userid,"questionnaire_title":result[0],"questionnaire_intro":result[1],"questionnaire_classify_id":result[2],"questionnaire_startdate":ndate,"questionnaire_flag":1})
                session.commit()
                subject = session.query(Subject.subject_title,Subject.subject_option_flag,Subject.subject_id).filter_by(subject_questionnaire_id = questionid).all()
                for item in subject:
                    session.add(Subject(subject_title = item[0],subject_option_flag = item[1],subject_questionnaire_id = lastInsertQuestionID))
                    session.commit()
                    lastInsertSubjectID = session.query(func.max(Subject.subject_id)).one()[0]
                    optlist = session.query(Optiontable.option_content).filter_by(option_subject_id = item[2]).all()
                    for i in optlist:
                        print(i)
                        session.add(Optiontable(option_subject_id = lastInsertSubjectID,option_content=i[0]))
                        session.commit()
                session.close()
        except Exception as a:
            print(a)
            return False

    def questionnairelink(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                link = session.query(Questionnaire.questionnaire_link).filter_by(questionnaire_id=questionid).one()[0]
                if link.strip()=='':
                    return False
                else:
                    return link
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def templateshow(self,token,classifyid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter_by(questionnaire_user_id = userid,questionnaire_flag=1).all()

                allValues = []
                for item in tmp:
                    value = {}
                    number = session.query(Subject.subject_id).filter_by(subject_questionnaire_id=item[0]).count()
                    print('id'+str(item[0]))
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    value["subject_number"]=number
                    allValues.append(value)
                return allValues
        except Exception as a:
            print(a)
            return False

    def questionnaireshow(self,token,flag):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title,Questionnaire.questionnaire_startdate).filter_by(questionnaire_user_id = userid,questionnaire_flag=flag).all()
                allValues = []
                for item in tmp:
                    value = {}
                    number = session.query(Subject).filter_by(subject_questionnaire_id=item[0]).count()
                    value["questionnaire_id"] = item[0]
                    value["questionnaire_title"] = item[1]
                    allValues.append(value)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def createsubjectitem(self,token,questionid,title,flag,options):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.add(Subject(subject_questionnaire_id = questionid,subject_title = title,subject_option_flag = flag))
                session.commit()
                lastInsertSubjectID = session.query(func.max(Subject.subject_id)).one()[0]
                print('*'*10+str(lastInsertSubjectID))
                if flag != 3 and options != '':
                    optlist=demjson.decode(options)
                    for item in optlist:
                        print("*"*10+str(item))
                        session.add(Optiontable(option_subject_id = lastInsertSubjectID,option_content = item["option_content"],option_total=0))
                        session.commit()
                return lastInsertSubjectID
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def getsubjectid(self,token,subjectid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                title = session.query(Subject.subject_title).filter_by(subject_id = subjectid).one()[0]
                flag = session.query(Subject.subject_option_flag).filter_by(subject_id = subjectid).one()[0]
                options = session.query(Optiontable.option_content).filter_by(option_subject_id = subjectid).all()
                content = []
                for item in options:
                    tmp = {}
                    tmp["option_content"] = item[0]
                    print(item[0])
                    content.append(tmp)
                result = {}
                result["subject_title"] = title
                result["subject_option_flag"] = flag
                result["options"] = content
                return result
        except Exception as a:
            print(a)
            return False

    def deletesubject(self,token,subjectid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Subject).filter_by(subject_id = subjectid).delete()
                session.query(Optiontable).filter_by(option_subject_id = subjectid).delete()
                session.commit()
                session.close()
                return True
        except Exception as a:
            print(a)
            return False

    def updatesubject(self,token,subjectid,title,options):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                session.query(Subject).filter_by(subject_id = subjectid).update({"subject_title":title})
                session.query(Optiontable).filter_by(option_subject_id = subjectid).delete()
                optlist=demjson.decode(options)
                for item in optlist:
                    session.add(Optiontable(option_content = item["option_content"],option_subject_id = subjectid))
                    session.commit()
                session.commit()
                session.close()
        except Exception as a:
            print(a)
            return False

    def questionnairedetail(self,token,questionid):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                tmp = session.query(Questionnaire.questionnaire_title,
                Questionnaire.questionnaire_intro).filter_by(questionnaire_id = questionid).one()
                questionnaire_detail = []
                subjects = session.query(Subject.subject_option_flag,Subject.subject_title,Subject.subject_id).filter_by(subject_questionnaire_id = questionid).all()
                for item in subjects:
                    subject = {}
                    subject["subject_option_flag"] = item[0]
                    subject["subject_title"] = item[1]
                    subject["subject_id"] = item[2]
                    options = []
                    option = session.query(Optiontable.option_content,Optiontable.option_total,Optiontable.option_percent).filter_by(option_subject_id = item[2]).all()
                    for i in option:
                        temp = {}
                        temp["option_content"] = i[0]
                        temp["option_total"] = i[1]
                        temp["option_percent"] = i[2]
                        options.append(temp)
                    subject["subject_options"] = options
                    questionnaire_detail.append(subject)
                allValues = {}
                allValues["questionnaire_title"] = tmp[0]
                allValues["questionnaire_intro"] = tmp[1]
                allValues["questionnaire_detail"] = questionnaire_detail
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False

    def questionnaireclassify(self,token,classify):
        session = DBSession()
        try:
            userid=Userorml.getidbytoken(self,token)
            if session.query(User).filter_by(user_id=userid).count()>0:
                questions = session.query(Questionnaire.questionnaire_id,Questionnaire.questionnaire_title).filter_by(questionnaire_classify_id = classify,questionnaire_flag = 1).all()
                allValues = []
                for item in questions:
                    question = {}
                    count = session.query(Subject).filter_by(subject_questionnaire_id = item[0]).count()
                    question["questionnaire_title"] = item[1]
                    question["questionnaire_id"] = item[0]
                    question["subject_number"] = count
                    allValues.append(question)
                return allValues
            else:
                return False
        except Exception as a:
            print(a)
            return False


import svgwrite

def inches(coords):
  a = "%1.4fin" % coords[0]
  b = "%1.4fin" % coords[1]
  return (a,b)

def draw_line(dwg, start, end, stroke=svgwrite.rgb(10,10,16,'%')):
    start = inches(start)
    end = inches(end)
    stroke = stroke
    print("Start at %s, End at %s" % (start, end))
    dwg.add(dwg.line(start, end, stroke=stroke))

# all widths are in inches
canvas_width = "20in"
canvas_height = "12in"

# Draftboard dividers
material = "draftboard"
slot_width = 0.124

# Acrylic dividers
#material = "acrylic"
#slot_width = 0.108

dd_length = 16.7 # same as width
dd_height = 5.38
slot_height = dd_height / 2
end_spacer = .5  # how wide is the last tab on each side?
num_of_slots = 7
slot_spacing = (-(slot_width * num_of_slots)-(2*end_spacer)+dd_length)/(num_of_slots-1)

filename = ("%s drawer_divider %sx%s - %d slots - %s slot width - %1.4f spacing - %s end tabs.svg"
            % (material, dd_length, dd_height, num_of_slots, slot_width, slot_spacing, end_spacer))
# offset to start drawing
x = 1
y = 1

dwg = svgwrite.Drawing(filename, profile='tiny', height=canvas_height, width=canvas_width)

lines = [
    ((x,y), (x,y+dd_height)), # Left side vertical line
    ((x+dd_length,y), (x+dd_length,y+dd_height)), # right side, vertical line
    ((x,y+dd_height), (x+dd_length, y+dd_height)), # bottom edge
    ((x,y), (x+end_spacer, y)), # left side, first tab
    ((x+dd_length, y), (x+dd_length-end_spacer, y)), # right side, last tab
]

for line in lines:
    draw_line(dwg, line[0], line[1])

# offset the width of the first tab
xx = x+end_spacer

print("draw first slot")
draw_line(dwg, (xx, y), (xx, y+slot_height))  # down
draw_line(dwg, (xx, y+slot_height), (xx+slot_width, y+slot_height))  # over
draw_line(dwg, (xx+slot_width, y+slot_height), (xx+slot_width, y))  # up
xx = xx + slot_width

for i in range(1,num_of_slots):
    print("draw %s slot" % (i+1))
    draw_line(dwg, (xx, y), (xx+slot_spacing, y)) # tab
    xx = xx+slot_spacing
    draw_line(dwg, (xx, y), (xx, y+slot_height)) # down
    draw_line(dwg, (xx,y+slot_height), (xx+slot_width, y+slot_height)) # over
    draw_line(dwg, (xx+slot_width, y+slot_height), (xx+slot_width, y)) # up
    xx = xx+slot_width

dwg.save()


# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description:
"""
import sys

sys.path.append('..')

from nerpy.ner_model import NERModel

def test_get_entity():
    from seqeval.metrics.sequence_labeling import get_entities
    seq = ['B-PER', 'I-PER', 'I-PER', 'O', 'B-LOC', 'O']
    r = get_entities(seq)
    print(r)
    # [('PER', 0, 2), ('LOC', 4, 4)]
    sent = ['温家宝在京的']
    line_entities = get_entities(seq)
    pairs = []
    for i in line_entities:
        word = sent[0][i[1]: i[2] + 1]
        entity_type = i[0]
        pairs.append((word, entity_type))
    print(pairs)


###################################
# tellurium setup script
#
# develop install via
# pip install -e .
###################################
from setuptools import setup  # allows 'python setup.py develop'
import os
try:
    with open(os.path.join(os.path.dirname(__file__), 'VERSION.txt'), 'r') as f:
        version = f.read().rstrip()
except IOError:
    # fallback location
    with open(os.path.join(os.path.dirname(__file__), 'tellurium/VERSION.txt'), 'r') as f:
        version = f.read().rstrip()

setup(name='tellurium',
      version=version,
      author='J. Kyle Medley, Kiri Choi, Matthias König, Lucian Smith, Herbert M. Sauro',
      description='Tellurium: An biological modeling environment for Python',
      url='http://tellurium.analogmachine.org/',
      packages=[
          'tellurium',
          'tellurium.analysis',
          'tellurium.teio',
          'tellurium.sedml',
          'tellurium.notebooks',
          'tellurium.optimization',
          'tellurium.visualization',
          'tellurium.sedml',
          'tellurium.tests',
      ],
      package_data={
          "tellurium": ["*.txt"],
          "tellurium.sedml": ["templates/*.template"],
      },
      install_requires=['pandas>=0.19.2', 'matplotlib>=2.0.0', 'appdirs>=1.4.3']
      )


from models.classifier import Classifier
import models.xor_resnet as XOR
import torch
import torch.nn as nn
import torch.nn.functional as F

import torchvision.models as models

XORS = {
    'xor_resnet18': XOR.resnet18,
    'xor_resnet34': XOR.resnet34,
    'xor_resnet50': XOR.resnet50,
    'xor_resnet101': XOR.resnet101,
    'xor_resnet152': XOR.resnet152
}

class Model(nn.Module):
	def __init__(self, args, nclass):
		super(Model, self).__init__()

		self.nclass = nclass
		if args.dataset == 'embedding':
			self.backbone = Classifier(self.nclass)
		elif 'xor_resnet' in args.arch:
			self.backbone = XORS[args.arch](pretrained=args.pretrained)
			final_feature = self.backbone.fc.in_features
			self.backbone.fc = nn.Linear(final_feature, self.nclass)
		else:
			self.backbone = models.__dict__[args.arch](pretrained=args.pretrained)
			final_feature = self.backbone.fc.in_features
			self.backbone.fc = nn.Linear(final_feature, self.nclass)

	def forward(self, input):
		x = self.backbone(input)
		return x


import numpy
import matplotlib.pyplot  as plt

x = range(0, 10)
ax = numpy.array(x)
ay1 = ax * ax * ax
ay2 = ax ** 2 + numpy.sin(ax)
plt.plot(ax, ay1, 'ro--',)

plt.show()


from crawler import extract_news
import os

from bson.son import SON
from bson.json_util import dumps, loads
from dotenv import load_dotenv, find_dotenv
from flask import jsonify
from pymongo import MongoClient

load_dotenv(find_dotenv())

connection_string = os.environ.get("MONGODB_CONNECTION_STRING")
client = MongoClient(connection_string)

db = client["dgPadProjectDB"]
collection = db["MayadeenNews"]

"""This function is the one in which we insert our pipeline to aggregate, it's created to avoid redundant repetitive code"""
def aggregationQuery(pipeline):
    cursor = collection.aggregate(
        pipeline,
        allowDiskUse=True
    )

    try:
        cursorList = list(cursor)
        jsonData = dumps(cursorList, indent=2)
        return jsonData
    finally:
        cursor.close()

def countArticlesByMonthPipeline():
    return [
        {
            '$project': {
                'year': {
                    '$toInt': {
                        '$substr': [
                            '$publication_date', 0, 4
                        ]
                    }
                },
                'month': {
                    '$toInt': {
                        '$substr': [
                            '$publication_date', 5, 2
                        ]
                    }
                }
            }
        }, {
            '$group': {
                '_id': {
                    'year': '$year',
                    'month': '$month'
                },
                'count': {
                    '$sum': 1
                }
            }
        }, {
            '$sort': {
                '_id.year': 1,
                '_id.month': 1
            }
        }
    ]

def topKeywordsPipeline(limit: int):
    return [
        {
            '$project': {
                'keyword': {
                    '$split': [
                        '$keywords', ','
                    ]
                }
            }
        }, {
            '$unwind': {
                'path': '$keyword'
            }
        }, {
            '$group': {
                '_id': '$keyword',
                'keywordCount': {
                    '$sum': 1
                }
            }
        }, {
            '$sort': {
                'keywordCount': -1
            }
        }, {
            '$limit': limit
        }
    ]

def articlesWithImagePipeline():
    return [
        {
            '$match': {
                'image': {
                    '$ne': None
                }
            }
        }, {
            '$count': 'count'
        }
    ]

def countArticlesByMonthQuery():
    return aggregationQuery(countArticlesByMonthPipeline())

def topKeywordsQuery():
    return aggregationQuery(topKeywordsPipeline(100))

def articlesWithImageQuery():
    return aggregationQuery(articlesWithImagePipeline())

def countAllDocuments():
    count = collection.count_documents({})
    return jsonify(count)

"""Calls to crawl the news off news website"""
# extract_news('https://www.aljazeera.com/sitemap.xml', 'aljazeera_sitemap.json', 'aljazeera_news.jsonl')
# extract_news('https://www.almayadeen.net/sitemaps/news.xml', 'almayadeen_sitemap.json', 'almayadeen_news.jsonl')