import unittest
from unittest.mock import patch

import os
import json
import tkinter
import _tkinter

from simplabel import ImageClassifier

class TestLabelingTool(unittest.TestCase):

    def setUp(self):

        self.test_folder = 'tests/test_images'
        self.label_file = os.path.join(self.test_folder, '.labels.json')

        self.cleanup_files()

        # Create label file
        labels = ["Label1", "Label2"]
        with open(self.label_file, 'w') as f:
            json.dump(labels, f)

        self.root=tkinter.Tk()
        self.pump_events()
        self.classifier = ImageClassifier(self.root, directory=self.test_folder, username="testuser")

    def tearDown(self):
        if self.classifier.gotLock:
            self.classifier.lock.release()
        if self.root:
            self.root.destroy()
            self.pump_events()

        self.cleanup_files()

    def pump_events(self):
        while self.root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT):
            pass

    def cleanup_files(self):
        # Delete any saved files
        savefiles = [file for file in os.listdir(self.test_folder) if file.startswith("labeled_") and file.endswith(".json")]
        if savefiles:
            for file in savefiles:
                os.remove(os.path.join(self.test_folder, file))

        # Delete all lock files
        lockfiles = [file for file in os.listdir(self.test_folder) if file.endswith("_lock.txt")]
        if lockfiles:
            for file in lockfiles:
                os.remove(os.path.join(self.test_folder, file))

        # Delete label file
        if os.path.exists(self.label_file):
            os.remove(self.label_file)

    def test_next_image_button(self):
        prevValue = self.classifier.counter
        self.classifier.nextButton.invoke()
        self.assertEqual(self.classifier.counter, prevValue + 1)

    def test_previous_image_button(self):
        while self.classifier.counter == 0:
            self.classifier.next_image()
        prevValue = self.classifier.counter
        self.classifier.prevButton.invoke()
        self.assertEqual(self.classifier.counter, prevValue - 1)

    def test_goto_last_image_button(self):
        self.classifier.lastButton.invoke()
        self.assertEqual(self.classifier.counter, len(self.classifier.image_list) - 1)

    def test_label_one_image(self):
        '''Checks that clicking on a label button advances to the next image and has labeled the previous image'''
        prevValue = self.classifier.counter
        imgName = self.classifier.image_list[prevValue]
        self.classifier.catButton[0].invoke()
        if prevValue < len(self.classifier.image_list) - 1:
            self.assertEqual(self.classifier.counter, prevValue + 1)
        else:
            self.assertEqual(self.classifier.counter, prevValue)
        self.assertEqual(self.classifier.labeled[imgName], "Label1")

    def test_labeled_image_has_colored_button(self):
        '''Checks that a labeled image has a colored label button with the user's color'''
        self.classifier.catButton[1].invoke()
        self.classifier.firstButton.invoke()

        # Check that the Label2 button has the color of the user
        self.assertEqual(self.classifier.catButton[1].config()['highlightbackground'][-1], self.classifier.userColor)
        self.assertEqual(self.classifier.catButton[1].config()['background'][-1], self.classifier.userColor)

        # Check that the Label1 button has the original color
        self.assertEqual(self.classifier.catButton[0].config()['highlightbackground'][-1], self.classifier.buttonOrigColor)
        self.assertEqual(self.classifier.catButton[0].config()['background'][-1], self.classifier.buttonBgOrigColor)

    def test_save_labels(self):
        '''Checks that the labels are correctly saved to file'''
        imgName = self.classifier.image_list[self.classifier.counter]
        self.classifier.catButton[1].invoke()
        self.classifier.saveButton.invoke()

        with open(os.path.join(self.test_folder, "labeled_testuser.json"), 'r') as savefile:
            savedict = json.load(savefile)

        self.assertEqual(savedict[imgName], self.classifier.labeled[imgName])

    def test_goto_last_unlabeled_image(self):
        self.classifier.catButton[0].invoke()
        self.classifier.firstButton.invoke()
        self.assertEqual(self.classifier.counter, 0)
        self.classifier.nextUnlabeledButton.invoke()
        self.assertEqual(self.classifier.counter, 1)

    @patch('simplabel.simpledialog.askstring')
    def test_add_label(self, mock_dialog):
        '''Check that adding a label through the GUI adds it to the list, creates a new button and saves it to file'''

        # Mock the dialog that requests the name of the label
        mock_dialog.configure_mock(return_value="Label3")

        # Add a label
        self.classifier.addCatButton.invoke()

        # Check that the label was added to the list
        self.assertEqual(self.classifier.categories[2], "Label3")

        # Check that a button was created for it
        self.assertEqual(len(self.classifier.catButton), 3)

        # Check that the label was saved to file
        with open(self.label_file, 'r') as f:
            savedlabels = json.load(f)

        self.assertIn("Label3", savedlabels)

    @patch('simplabel.simpledialog.askstring')
    def test_add_already_existing_label(self, mock_dialog):
        '''Check that adding an already existing label through the GUI does not add it to the list'''

        # Mock the dialog that requests the name of the label
        mock_dialog.configure_mock(return_value="Label1")

        # Add a label
        self.classifier.addCatButton.invoke()

        # Check that the label was not added to the list
        self.assertEqual(len(self.classifier.categories), 2)

        # Check that a button was not created for it
        self.assertEqual(len(self.classifier.catButton), 2)

        # Check that the label was saved to file
        with open(self.label_file, 'r') as f:
            savedlabels = json.load(f)

        self.assertEqual(len(savedlabels),2)

    @patch('simplabel.simpledialog.askstring')
    def test_add_10_labels(self, mock_dialog):
        '''Check that adding 10 labels works as intended (3 rows of labels)'''

        # Add labels
        mock_dialog.configure_mock(return_value="Label3")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label4")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label5")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label6")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label7")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label8")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label9")
        self.classifier.addCatButton.invoke()
        mock_dialog.configure_mock(return_value="Label10")
        self.classifier.addCatButton.invoke()

        # Check that the labels were added to the list
        self.assertEqual(len(self.classifier.categories), 10)

        # Check that a button werecreated
        self.assertEqual(len(self.classifier.catButton), 10)

        # Check that there are 3 rows
        self.assertEqual(len(self.classifier.labelFrameList), 3)

        # Check that the labels were saved to file
        with open(self.label_file, 'r') as f:
            savedlabels = json.load(f)

        self.assertEqual(len(savedlabels),10)

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


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov  4 19:09:06 2018

@author: oscar seurat
"""
import numpy as np;
import sys;
from numpy import random;
import math;
import Loader as ld;
import matplotlib.pyplot as plt;
import Transformer;
import Convolver;
import scipy.signal;

class Combiner:
    def __init__(self, img1, img2):
        # Images are assumed to be numpy arrays
        m1, n1 = img1.shape;
        m2, n2 = img2.shape;

        if (m1 != m2 or n1 != n2):
            print('Combiner.combiner: the images to combine should have the ' +
                  'same sizes.', sys.stderr);
            sys.exit(-1);

        self.img1 = img1;
        self.img2 = img2;
        self.inner_rect = 0.80; """Currently (11/10/2018), the combiner picks
         a similar rectangle (remember similar triangles?) centrally (or
         symmertically) located in the image and select from it randomly
         centers of circular masks """

    """
    Apply mask: 1 chooses pixels from self.img2, 0 from self.img1
    """
    def _simple_merge(self, mask):

        m, n = mask.shape;
        p, q = self.img1.shape;

        if (m != p or n != q):
            print('Combiner._simple_merge: the combining mask is not of the ' +
                  'same size as the images to be combined.', sys.stderr);

        c_img = self.img1*(1-mask) + mask * self.img2;
        return c_img;

    """
    This simply returns mask whose all non-zero values are located
    in the circle (center, radius)
    """
    def _circular_mask(self, center, radius):
        m, n = self.img1.shape;
        mask = np.zeros([m, n]);

        idx = [(x, y) for x in range(m) for y in range(n)
                if (math.sqrt((x-center[0])**2+(y-center[1])**2) <= radius)]
        # Note: slow
        for pp in range(len(idx)):
            mask[idx[pp]] = 1;

        return mask;

    """
    Given an array of masks find their union
    The array of mask is intended to be short
    (<= 10 elems or so). In
    Masks are assumed to be stacked along dimension 0.
    The mask_array is a cube (not necessarily even sided)
    """
    @staticmethod
    def _mask_union(mask_array):
        shape = mask_array.shape;
        if (len(shape)<2 or len(shape) > 3):
            print('Combiner._mask_union: the mask array must contain at least ' +
                   'one mask(layer) and be at most 3 dimensional.', sys.stderr);
            sys.exit(-1);

        if (len(shape)==2):
            return mask_array;

        ret_mask = np.amax(mask_array, axis=2);
        return ret_mask;

    def random_circle_mask_array(self, k, radius_min, radius_max):
        m, n = self.img1.shape;
        if (radius_min <= 0 or radius_max <= 0 or
            radius_min > radius_max or
            radius_max > np.amin([m,n])/2):
            print('Combiner.random_circle_mask: rradii of circular masks are ' +
                  'incorrect.', sys.stderr);
            sys.exit(-1);

        mask_array = np.zeros([m, n, k]);

        L = int(self.inner_rect*m);
        D = (m-L)/2;

        LL = int(self.inner_rect*m);
        DD = (n-LL)/2;
        for s in range(k):
            center_x = random.randint(D, m-D);
            center_y = random.randint(DD, n-DD);
            radius   = random.randint(radius_min, radius_max);
            mask = self._circular_mask([center_x, center_y], radius);
            mask_array[:,:, s] = mask;

        return mask_array;

    """
    This function is a bespoke one ("one off" to a degree). It takes the Transformer's method
    example_transf1 and applies it with a range of parameters producing
    an np.array of images. The arguments are based on those of Transormer.example_transf1,
    except that those with _t extension are pairs/triple in which the second element/third
    is a delta by which to increment/decrement the first element. The augmentation
    is repeated n times.
    **** DISTORTION PARAMETERS ****
    df_range is the distortion frequency range (an int) -- how much in maximum will the original
             circle be shifted randomly to generate varying frequencies of interference
             (note: the delta of the df_range shifts is given in the second element of the
             radii-t argument; 1 (pixel0 is a good value of delta)

    ph_range is the phase distortion range - to generate different values of phase shifts
             in order to vary that parameter value over the many generated distortions;
             again the delta is given in the second element of the pahse_p parameter

    amp_range is the amplitude of the distortion; we want to vary that parameter
             across all the distortion we make (again, the delta of the distorion is given
             as the second element of the argument)

    sec_range is the shift in the angles of the sector of directions for distortions
             that will be generated; the extent of the sector and its delta is
             given in the sector_t argument; then that sector is shifted (randomly)
             by the sec_range parameter.

    ******* MASK PARAMETERS *******
    f_min is the fraction of min(img.shape) that the smaller radii of (important!)
          distortion circles will have (there will be anumber of such circles)

    f_max is the fraction of max(img.shape) that the larger of the radii of
          distorion circles will have

    These lines of code explain the action of f_min, f_max (R1, R2 are the radii)
    R1 = int(f_min*np.amin([r,c]));
    R2 = int(f_max*np.amin([r,c]));

    num_circ_min is the minimal number of distortion circles that will be generated

    num_circ_max is the maximal number of circles that will be generated
    *******************************

    aux_img -- an image I2 which meant to 1) be distorted by transf specified by the
             previous parameters, and 2) overlayed over the original image in the
             spots specified by the previous params. Normally I2 will be the original
             image itself (in other words we're simply distorting the original image).
             The need for this parameter arose out of the need to combine different types
             of distortions and I2 is really meant to be the original image distorted by
             a different independent transformation.
    """
    @staticmethod
    def loop_example_transf1(img, radii_t, phase_p, center, ampl_p, sector_t, num_img=4,
                             df_range = 10, ph_range = 3, amp_range = 3, sec_range = 10,
                             f_min=0.1, f_max=0.3, num_circ_min=1, num_circ_max=4,
                             aux_img = []):
        if (len(radii_t) != 3):
            print('Combiner.loop_exampe_transf1: the radii argument must be a tripple.',
                  sys.stderr);
            # sys.exit(-1);
            raise ValueError;

        if (len(phase_p) != 2):
            print('Combiner.loop_exampe_transf1: the phase_p argument must be a pair.',
                  sys.stderr);
            # sys.exit(-1);
            raise ValueError;

        if (len(ampl_p) != 2):
            print('Combiner.loop_exampe_transf1: the ampl_p argument must be a pair.',
                  sys.stderr);
            # sys.exit(-1);
            raise ValueError;

        if (len(sector_t) != 3):
            print('Combiner.loop_exampe_transf1: the sector_t argument must be a triple.',
                  sys.stderr);
            # sys.exit(-1);
            raise ValueError;

        rad1  = radii_t[0];  rad2 = radii_t[1];   dlt_r  = radii_t[2];
        phase = phase_p[0];                       dlt_ph = phase_p[1];
        ampl  = ampl_p[0];                        dlt_a  = ampl_p[1];
        alph1 = sector_t[0]; alph2 = sector_t[1]; dlt_s  = sector_t[2];

        r, c = img.shape;
        aout = np.zeros([r,c,num_img]);

        T = Transformer.Transformer(img);
        if (len(aux_img) != 0):
            T = Transformer.Transformer(aux_img);

        img_cntr = 0;
        for s in range(num_img):
            rr = random.randint(-df_range, df_range);
            pp = random.randint(0,ph_range);
            aa = random.randint(0,amp_range);
            AA = random.randint(0,sec_range);

            try:
                # This call generates distortion across the whole image
                img2, ignore = T.example_transf1(rad1 + rr*dlt_r, rad2 + rr*dlt_r,
                                                 phase + pp*dlt_ph, [0,0],
                                                 ampl  + aa*dlt_a,
                                                 [alph1 + AA*dlt_s, alph2 + AA*dlt_s],
                                                 fix_hist = True);
            except:
                continue;

            # The below  few calls select a part of the image (a mask) to apply the
            # distortion to
            cB   = Combiner(img, img2);
            nn   = random.randint(num_circ_min, num_circ_max);
            # Image size is (r, c). What should the range of mask circle radii
            # be? Guess: between f_min=105, f_max=30%. Note that there will be many
            # such circles.
            R1 = int(f_min*np.amin([r,c]));
            R2 = int(f_max*np.amin([r,c]));
            mA   = cB.random_circle_mask_array(nn, R1, R2);
            mU   = Combiner._mask_union(mA);
            img3 = cB._simple_merge(mU);
            img4 = Transformer.Transformer.hist_match(img3, img);
            if (np.std(img4)!= 0):
                img_cntr += 1;
                aout[:,:, s] = img4;

        aout = np.delete(aout, range(img_cntr, num_img), 2);
        return aout;

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

if __name__ == "__main__":
    MRI_PATH='/Users/yzhao11/Documents/Research/MachineLearning/MRI/zhao_dataset_20181011/sub-NC188/ses-20180825/anat/';
    SCAN = 'sub-NC188_ses-20180825_acq-nomotion_run-01_T1w.nii';
    # MRI_PATH = '/Users/yzhao11/Documents/Research/MachineLearning/MRI/zhao_dataset_20181011/sub-NC189/ses-20180825/anat/';
    # SCAN = 'sub-NC189_ses-20180825_acq-motion_run-02_T1w.nii'

    mri = ld.mri_scan(MRI_PATH + SCAN);
   # img1 = mri.get_image(88, axis='x', crop_center=[10,10], crop_wh=[100,100]);
    img1 = mri.get_image(100, axis='x');

   # fig = plt.figure(figsize= (7,7));
   # plt.imshow(img1);
   # plt.show();

    """
    T = Transformer.Transformer(img1);
    # img2, ignore = T.example_transf1(60,65, np.pi, [0,0], 16, [1.5*np.pi, 1.82*np.pi],
    #                                 fix_hist = True);

    img2, ignore = T.example_transf1(55,60, 2.51, [0,0], 8.89, [5.18, 5.67],
                                     fix_hist = True);

    cB   = Combiner(img1, img2);
    mA   = cB.random_circle_mask_array(2, 10, 40);
    mU   = Combiner._mask_union(mA);
    img3 = cB._simple_merge(mU);
    img4 = Transformer.Transformer.hist_match(img3, img1);

    fig, ax = plt.subplots(1, 3, figsize=(15, 10));
    ax[0].imshow(img1);
    ax[1].imshow(img2)
    ax[2].imshow(img4);
    plt.show();
    """

    # imarr = Combiner.loop_example_transf1(img1, [60, 65, 1], [2.51, 0], [0,0], [8.89, 0], [0.18, 0.67, np.pi/7], 9);

    # U should 2, 3, or 4
    U =2;
    # imarr = Combiner.loop_example_transf1(img1, radii_t = [52.25, 55.25, 0.1], df_range = 5, phase_p=[2.4, 0], center=[0,0],
    #                                      ampl_p = [0.1, 0.00], amp_range = 4, sector_t = [0, 3, np.pi/18], sec_range=16,
    #                                      num_circ_min = 12, num_circ_max = 15, n = U**2);

    # These are good settings for 100 x 100 images
    # imarr = Combiner.loop_example_transf1(img1, [31, 36, 1], [2.51, 0], [0,0], [8.89, 0], [0.18, 0.67, np.pi/7], 4);

    kernel = np.zeros((16,16))
    kernel[10,3] = 0.2
    kernel[12,12] = 0.2
    kernel[5,5] = 0.2
    kernel[2,2] = 0.2
    kernel[3,10] = 0.2

    # The below code uses Timek's masking mechannism; I actually do not need it
    """
    aux_img= Convolver.generate_blended_images_constant_kernel(img1, 1, kernel);
    aux_img = aux_img[:,:, 0];
    """
    aux_img = scipy.signal.convolve2d(img1, kernel, boundary = 'symm', mode='same');
    imarr = Combiner.loop_example_transf1(img1, [61, 66, 1.5], [2.51, 0], [0,0], [8.89, 0], [0.18, 0.67, np.pi/7],
                                          f_min=0.06, f_max=0.18, num_img = U**2, aux_img = aux_img);

    # save_path = "/Users/yzhao11/Documents/Research/MachineLearning/MRI/zhao_dataset_20181011/sub-NC188/ses-20180825/anat/";
    # save_file = "1000imagesZ";
    # np.savez_compressed(save_path + save_file, imarr=imarr);

    fig, ax = plt.subplots(U,U, figsize=(14,16));
    for q in range(U):
        for r in range(U):
            ax[q,r].imshow(imarr[:,:, U*q + r], interpolation='nearest', aspect='equal');
    plt.show();

    """
    Configuration 1:

    Radii [r1, r2] = [35.40 , 38.40] (spacing 3) vary from r1 = 25 --> r2 = 45.50
    Phase = 2.19 <-- do not vary
    Ampl (!) = 0.27 --> vary from 0.1 --> 0.4
    center = [0,0]
    A1, A2 = [1.59, 4.59] (spacing 3) vary from 0 --> .27

    """


#!/usr/bin python3
# -*- coding: utf-8 -*-

import re
from os import path
from codecs import open
from setuptools import setup
from pkg_resources import parse_version

VERSIONFILE = "vafnet/__version__.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
    __version__ = mo.group(1)
else:
    raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name="vafnet",
    version=__version__,
    license="Apache License 2.0",
    author="Max W. Y. Lam",
    author_email="maxingaussian@gmail.com",
    description="Variational Activation Functions for Deep Learning",
    long_description=long_description,
    keywords="machine-learning deep-learning neural-networks gaussian-process",
    url="https://github.com/MaxInGaussian/VAFnet",
    download_url='https://github.com/MaxInGaussian/VAFnet/tarball/'+__version__,
    classifiers=[
        'License :: OSI Approved :: Apache Software License'
        'Natural Language :: English',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX :: Linux',
        'Intended Audience :: Developers',
        'Programming Language :: Python :: 3.5',
        'Topic :: Scientific/Engineering :: Artificial Intelligence'
    ],
    packages=["vafnet"],
    package_dir={'vafnet': 'vafnet'},
    py_modules=['vafnet.__init__'],
    install_requires=['numpy>=1.7',
                      'six>=1.10.0'],
    extras_require={
        'edward': ['edward>=1.3.4'],
        'tensorflow': ['tensorflow>=1.2.0rc0'],
        'tensorflow with gpu': ['tensorflow-gpu>=1.2.0rc0'],
        'notebooks': ['jupyter>=1.0.0'],
        'visualization': ['matplotlib>=1.3']},
    tests_require=['pytest', 'pytest-pep8'],
)


from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import generics
from .serializer import *
from .email import *
from rest_framework import views

class Send_otp(APIView):
    def post(self, request):
        try:
            data = request.data
            serializer = UserSerializer(data = data)
            usr = client.objects.filter(email = data['email'])
            if usr:
                send_otp(request.data['email'])
                return Response({'status':200,'message':'OTP Send Successfull','data':request.data['email']})
            if serializer.is_valid():
                serializer.save()
                send_otp(serializer.data['email'])
                return Response({'status':200,'message':'OTP created succesfull check email','data':serializer.data })
            return Response({'status':400,'message':'Something went Wrong','data':serializer.errors})
        except Exception as e:
            print(e)

class verify_otp(APIView):
    def post(self, request):
        try:
            data = request.data
            serializer = verifyaccount(data = data)
            if serializer.is_valid():
                email = serializer.data['email']
                otp = serializer.data['otp']
                user = client.objects.filter(email = email)
                if not user.exists():
                    return Response({
                    'status':400,
                    'message':'Something went Wrong',
                    'data':'invalid email'
                    })
                if user[0].otp != otp:
                    return Response({
                    'status':400,
                    'message':'Something went Wrong',
                    'data':'wrong otp'
                    })
                user.update(is_verified = True)
                return Response({
                    'status':200,
                    'message':'account verified',
                    'data':{}
                })
            return Response({
                'status':400,
                'message':'Something went Wrong',
                'data':serializer.errors
            })
        except Exception as e:
            print(e)


import torch
import numpy as np
from torch.autograd import Variable
import torch.nn.functional as F
import datetime
from torch.optim.rmsprop import RMSprop
from sklearn.metrics import explained_variance_score  # 可释方差也叫解释方差（explained_variance_score）
from sklearn.metrics import mean_absolute_error  # 平均绝对误差（mean_absolute_error）
from sklearn.metrics import mean_squared_error  # 均方误差（mean_squared_error）
from sklearn.metrics import median_absolute_error  # 中值绝对误差（median_absolute_error）
from sklearn.metrics import r2_score  # R方值，确定系数（r2_score）

# 定义mlp
class Mlp(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Mlp, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)
        self.predict = torch.nn.Linear(n_hidden, n_output)

    def forward(self, x):
        x = F.relu(self.hidden(x))
        x = self.predict(x)
        return x

class my_mlp():

    _converge_train = []
    _converge_test = []

    def __init__(self, train_set, test_set, epoch, input_size, hidden_size, output_size, optimizer, loss_func, lr):
        self.train_set = train_set  # 训练集
        self.test_set = test_set  # 测试集
        self.epoch = epoch  #
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size
        self.optimizer = optimizer
        self.loss_func = loss_func
        self.lr = lr

        self.net = self.set_mlp_index()  # 初始化网络
        self.opt = self.set_mlp_optim()  # 初始化优化函数
        self.loss_function = self.set_mlp_lossfunc()  # 初始化loss函数

    # 查看是否使用GPU
    def set_device(self):
        ngpu = 1
        # Decide which device we want to run on
        device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
        return device

    # 设置mlp参数
    def set_mlp_index(self):
        return Mlp(self.input_size, self.hidden_size, self.output_size).to(self.set_device())

    # 设置优化器
    def set_mlp_optim(self):
        if self.optimizer == "Adam":
            return torch.optim.Adam(self.net.parameters(), lr=self.lr)
        elif self.optimizer == "SGD":
            return torch.optim.SGD(self.net.parameters(), lr=self.lr)
        elif self.optimizer == "rmsprop":
            return RMSprop(self.net.parameters(), lr=self.lr)

    # 设置loss函数
    def set_mlp_lossfunc(self):
        if self.loss_func == "MSE":
            return torch.nn.MSELoss()
        elif self.loss_func == "SmoothL1Loss":
            return torch.nn.SmoothL1Loss()
        elif self.loss_func == "RMSE":
            return mean_squared_error

    # 训练训练集
    def train_train_set(self):
        for step_train, (x_train, y_train) in enumerate(self.train_set):
            # 将数据放入cuda
            x = Variable(x_train).to(self.set_device())
            y = Variable(y_train).to(self.set_device())

            # net = self.set_mlp_index()
            prediction_train = self.net(x)

            # lossfunc = self.set_mlp_lossfunc()
            loss_train = self.loss_function(prediction_train, y)

            self.opt.zero_grad()
            loss_train.backward()
            self.opt.step()
        print("    训练集loss:{}".format(loss_train))
        self._converge_train.append(loss_train)

    # 测试测试集
    def test_test_set(self):
        for step_test, (x_test, y_test) in enumerate(self.test_set):
            # 将数据放入cuda
            x = Variable(x_test).to(self.set_device())
            y = Variable(y_test).to(self.set_device())
            # net = self.set_mlp_index()
            prediction_test = self.net(x)
            # lossfunc = self.set_mlp_lossfunc()
            loss_test = self.loss_function(prediction_test, y)
        print("    测试集loss:{}".format(loss_test))
        self._converge_test.append(loss_test)

    # 导出数据
    def export_data(self):
        np.set_printoptions(suppress=True)
        np.set_printoptions(precision=3)
        path_train = "./dataset/converge_train"
        np.savetxt(path_train, self._converge_train, fmt='%.06f')
        path_test = "./dataset/converge_test"
        np.savetxt(path_test, self._converge_test, fmt='%.06f')

    # 将训练好的网络保存
    def export_nn_params(self):
        state = {'model': self.net.state_dict(),
                 'optimizer_adam': self.opt.state_dict(),
                 'epoch': self.epoch}
        torch.save(state, "./dataset/sd")
        print("网络保存完成")

    # 训练mlp
    def train_my_mlp(self):

        for t in range(self.epoch):
            print("****************************************************")
            print("        第{}个epoch       ".format(t))
            start_time = datetime.datetime.now()
            self.train_train_set()

            self.test_test_set()
            end_time = datetime.datetime.now()
            print("        用时{}            ".format(end_time - start_time))
            self.export_data()
        self.export_nn_params()

# 勘察误差
def check_error(label, pre_y):
    label = label.cpu().detach().numpy()
    pre_y = pre_y.cpu().detach().numpy()
    print("可释方差(explained_variance_score)为: {}"
          .format(mean_absolute_error(label, pre_y)))
    print("平均绝对误差(mean_absolute_error)为: {}".format(mean_absolute_error(label, pre_y)))
    print("均方误差（mean_squared_error）为: {}".format(mean_squared_error(label, pre_y)))
    print("中值绝对误差（median_absolute_error）为: {}".format(median_absolute_error(label, pre_y)))
    print("R方值，确定系数（r2_score）为: {}".format(r2_score(label, pre_y)))


import requests
from bs4 import BeautifulSoup

url ="https://www.mygov.in/corona-data/covid19-statewise-status"
req = requests.get(url)

bs = BeautifulSoup(req.text,"html.parser")

data= bs.find_all("div",class_="field-item odd")

maharashtra = data[7].text
Confirmed  =42 # index = 42
Discharged = 73 # index = 73
Deaths = 83 # index = 83

confirmed = int(maharashtra[int(Confirmed):46])
discharged = int(maharashtra[int(Discharged):76])
deaths = int(maharashtra[int(Deaths):])

print("===MAHARASHTRA REPORT COVID-19===")
print("Total number of confermed cases:",confirmed)
print("Total dischargged:",discharged)
print("Total deaths:",deaths)


import torch.nn as nn
import torch.nn.functional as F
import torch

class UNetDown(nn.Module):
    def __init__(self, in_size, out_size, normalize=True, dropout=0.0):
        super(UNetDown, self).__init__()
        layers = [nn.Conv2d(in_size, out_size, kernel_size=4, stride=2, padding=1)]
        if normalize:
            layers.append(nn.InstanceNorm2d(out_size))
        layers.append(nn.LeakyReLU(0.2))
        if dropout:
            layers.append(nn.Dropout(dropout))
        self.model = nn.Sequential(*layers)

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

class UNetUp(nn.Module):
    def __init__(self, in_size, out_size, dropout=0.0):
        super(UNetUp, self).__init__()
        layers = [nn.ConvTranspose2d(in_size, out_size, kernel_size=4, stride=2, padding=1),
                nn.InstanceNorm2d(out_size),
                nn.ReLU()]
        if dropout:
            layers.append(nn.Dropout(dropout))
        self.model = nn.Sequential(*layers)

    def forward(self, x, skip_input):
        x = self.model(x)
        x = torch.cat((x, skip_input), 1)

        return x

class GeneratorUNet(nn.Module):
    def __init__(self, in_channels=3, out_channels=3):
        super(GeneratorUNet, self).__init__()

        self.down1 = UNetDown(in_channels, 64, normalize=False)
        self.down2 = UNetDown(64, 128)
        self.down3 = UNetDown(128, 256, dropout=0.5)
        self.down4 = UNetDown(256, 512, dropout=0.5)
        self.down5 = UNetDown(512, 512, dropout=0.5)
        self.down6 = UNetDown(512, 512, dropout=0.5, normalize=False)

        self.up1 = UNetUp(512, 512, dropout=0.5)
        self.up2 = UNetUp(1024, 512, dropout=0.5)
        self.up3 = UNetUp(1024, 256, dropout=0.5)
        self.up4 = UNetUp(512, 128, dropout=0.5)
        self.up5 = UNetUp(256, 64)

        self.final = nn.Sequential(
            nn.Upsample(scale_factor=2),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.Conv2d(128, out_channels=out_channels, kernel_size=4, stride=1),
            nn.Tanh()
        )

    def forward(self, x):
        d1 = self.down1(x)
        d2 = self.down2(d1)
        d3 = self.down3(d2)
        d4 = self.down4(d3)
        d5 = self.down5(d4)
        d6 = self.down6(d5)
        u1 = self.up1(d6, d5)
        u2 = self.up2(u1, d4)
        u3 = self.up3(u2, d3)
        u4 = self.up4(u3, d2)
        u5 = self.up5(u4, d1)

        return self.final(u5)

class Discriminator(nn.Module):
    def __init__(self, in_channels=3):
        super(Discriminator, self).__init__()

        def discriminator_block(in_channels, out_channels, normalization=True):
            layers = [nn.Conv2d(in_channels, out_channels, 4, stride=2, padding=1)]
            if(normalization):
                layers.append(nn.InstanceNorm2d(out_channels))
            layers.append(nn.LeakyReLU(0.2, inplace=True))

            return layers

        self.model = nn.Sequential(
            *discriminator_block(in_channels, 64, normalization=False),
            *discriminator_block(64, 128),
            *discriminator_block(128, 256),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.Conv2d(256, 1, 4, padding=1),
        )

        self.lin = nn.Linear(64, 1)

    def forward(self, x):
        return (self.lin(self.model(x).view(-1, 64)))


import os
import random
import shutil
import argparse

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

def count_images(directory, image_extensions):
    image_count = 0
    for root, _, files in os.walk(directory):
        for file in files:
            if any(file.lower().endswith(ext) for ext in image_extensions):
                image_count += 1
    return image_count

def split_dataset(source_dir, train_dir, test_dir, split_ratio=0.8):
    image_extensions = ['.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG']

    for root, _, files in os.walk(source_dir):
        for file in files:
            if any(file.lower().endswith(ext) for ext in image_extensions):
                base_filename, file_extension = os.path.splitext(file)
                source_image_path = os.path.join(root, file)
                source_xml_path = os.path.join(root, base_filename + ".xml")

                # Determine whether to put the file in the train or test set
                if random.uniform(0, 1) < split_ratio:
                    destination_dir = train_dir
                else:
                    destination_dir = test_dir

                # Recreate the subfolder structure in the destination directory
                relative_path = os.path.relpath(root, source_dir)
                destination_subdir = os.path.join(destination_dir, relative_path)
                os.makedirs(destination_subdir, exist_ok=True)

                # Copy the image and its corresponding XML file
                destination_image_path = os.path.join(destination_subdir, file)
                destination_xml_path = os.path.join(destination_subdir, base_filename + ".xml")
                shutil.copy(source_image_path, destination_image_path)
                shutil.copy(source_xml_path, destination_xml_path)

    train_image_count = count_images(train_dir, image_extensions)
    test_image_count = count_images(test_dir, image_extensions)

    print("--------------------------DATASET SPLIT COMPLETED------------------------------")
    print(f'Train Dataset Directory: {train_dir}')
    print(f'Train Dataset Count: {train_image_count} Images')
    print(f'Test Dataset Directory: {test_dir}')
    print(f'Test Dataset Count: {test_image_count} Images')
    print("-------------------------------------------------------------------------------")

def main():

    # source_directory = "/Users/ofotech_fitri/Documents/tf_object_detection/script/script_temp/dataset/temp"
    # train_directory = "/Users/ofotech_fitri/Documents/tf_object_detection/script/script_temp/dataset/train"
    # test_directory = "/Users/ofotech_fitri/Documents/tf_object_detection/script/script_temp/dataset/test"
    # split_ratio = 0.8  # 80% for training, 20% for testing

        # Initiate argument parser
    parser = argparse.ArgumentParser(description="Partition dataset of images into training and testing sets",
                                     formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument(
        '-i', '--imageDir',
        dest = "imageDir",
        help='Path to the folder where the image dataset is stored. If not specified, the CWD will be used.',
        type=str,
        default=os.getcwd()
    )
    parser.add_argument(
        '-o', '--trainDir',
        dest = "trainDir",
        help='Path to the output folder where the train and test dirs should be created. '
             'Defaults to the same directory as IMAGEDIR.',
        type=str,
        default=None
    )
    parser.add_argument(
        '-t', '--testDir',
        dest = "testDir",
        help='Path to the output folder where the train and test dirs should be created. '
             'Defaults to the same directory as IMAGEDIR.',
        type=str,
        default=None
    )
    parser.add_argument(
        '-r', '--ratio',
        dest = "ratio",
        help='The ratio of the number of test images over the total number of images. The default is 0.1.',
        default=0.1,
        type=float)

    args = parser.parse_args()

    os.makedirs(args.trainDir, exist_ok=True)
    os.makedirs(args.testDir, exist_ok=True)

    # split_dataset(source_directory, train_directory, test_directory, split_ratio)
    split_dataset(args.imageDir, args.trainDir, args.testDir, args.ratio)

if __name__ == '__main__':
    main()


from aiogram import types
import re

from aiogram.types import InlineKeyboardMarkup

from data.config import group_likes
from data.text import text
from filters import IsGroup
from loader import dp
from aiogram.utils.deep_linking import get_start_link
from utils.check_like import get_owner, check_like
from utils.db_api.mongo import post_db

instagram_regex = r'Dx([1-9]|[1-4]\d|50) (?:(?:https?|ftp):\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am)\/([A-Za-z0-9-_]+)\/'

@dp.message_handler(IsGroup())
async def get_link(message: types.Message):
    if re.search(instagram_regex, message.text):
        ### Wait text
        await message.reply(text['checking_list'].format(message.from_user.get_mention(message.from_user.full_name)))

        ### Get instagram link, shortcode and owner
        instagram_link = message.text.split(' ')[-1]
        shortcode = instagram_link.split('/')[4]
        owner_id = "@{}".format(await get_owner(shortcode))

        if not await check_like(shortcode):
            await message.delete()
            await message.answer(
                text['warning_text'].format(message.from_user.get_mention(message.from_user.full_name)))
            return

        temp = post_db.insert_one({'link': instagram_link})
        print(temp.inserted_id)
        ### Create Button with link
        deeplink = await get_start_link(group_likes[message.chat.id])
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [(types.InlineKeyboardButton(text['instagram_button'], url=instagram_link)),
             (types.InlineKeyboardButton(text['instagram_link_list'], url=deeplink))],
        ], row_width=2)

        ### Message delete
        await message.delete()

        ### Send message
        await message.answer(text['instagram_text'].format(message.from_user.get_mention(message.from_user.full_name),
                                                           message.text.split(" ")[0],
                                                           owner_id,
                                                           instagram_link), reply_markup=keyboard)
        return

    await message.delete()
    await message.answer(text['warning_text'].format(message.from_user.get_mention(message.from_user.full_name)))


import os
import getpass
import hashlib
import re
import time
import datetime
import logging
import six

import boto
from boto.emr.step import HiveStep
from boto.emr.step import InstallHiveStep
from boto.emr.connection import EmrConnection
from apiarist.s3 import copy_s3_file, is_dir, upload_file_to_s3
from apiarist.script import generate_hive_script_file, get_script_file_location

logger = logging.getLogger(__name__)

class EMRRunner():

    def __init__(self, job_name=None, input_path=None, hive_query=None,
                 output_dir=None, scratch_uri=None, log_path=None,
                 ami_version=None, hive_version=None, num_instances=None,
                 master_instance_type=None, slave_instance_type=None,
                 iam_instance_profile=None, iam_service_role=None,
                 aws_access_key_id=None, aws_secret_access_key=None,
                 visible_to_all_users=None,
                 s3_sync_wait_time=5, check_emr_status_every=30,
                 label=None, owner=None, temp_dir=None):

        self.job_name = job_name
        self.job_id = self._generate_job_id()
        self.start_time = time.time()

        # AWS credentials can come from arguments or environment
        # set AWS credentials if supplied (override ENV)
        if aws_access_key_id:
            os.environ['AWS_ACCESS_KEY_ID'] = aws_access_key_id
            self.aws_access_key_id = aws_access_key_id
        else:
            logger.debug("Getting AWS access key from ENV")
            self.aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']

        if aws_secret_access_key:
            os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_access_key
            self.aws_secret_access_key = aws_secret_access_key
        else:
            logger.debug("Getting AWS secret key from ENV")
            self.aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']

        # Set visibility of job flow to all users if visible_to_all_users is None
        if visible_to_all_users is None:
            self.visible_to_all_users = True
        else:
            self.visible_to_all_users = visible_to_all_users

        self.s3_sync_wait_time = s3_sync_wait_time
        self.check_emr_status_every = check_emr_status_every

        # I/O for job data
        self.input_path = input_path
        self.output_dir = output_dir

        # is the input multiple files in a 'directory'?
        try:
            self.input_is_dir = is_dir(input_path)
        except TypeError:
            self.input_is_dir = False

        # the Hive script object
        self.hive_query = hive_query

        #  EMR options
        self.master_instance_type = master_instance_type
        self.slave_instance_type = slave_instance_type
        self.ami_version = ami_version
        self.hive_version = hive_version
        self.num_instances = num_instances
        self.iam_instance_profile = iam_instance_profile
        self.iam_service_role = iam_service_role
        self.label = label
        self.owner = owner

        # give this job a unique name
        self.job_key = self._make_unique_job_key(
            label=self.label, owner=self.owner)

        # S3 'scratch' directory
        if scratch_uri:
            self.base_path = scratch_uri
            os.environ['S3_SCRATCH_URI'] = scratch_uri
        else:
            self.base_path = os.environ['S3_SCRATCH_URI']

        # allow alternate logging path
        self.log_path = log_path or self.base_path + 'logs/'
        # other temp files live in a jobID bucket
        self.job_files = self.base_path + self.job_id + '/'
        self.data_path = self.job_files + 'data'
        if self.input_is_dir:
            self.data_path += '/'
        self.table_path = self.job_files + 'tables/'
        self.script_path = self.job_files + 'script.hql'
        self.output_path = self.output_dir or self.job_files + 'output/'

        # a local temp dir is used to write the script
        self.local_script_file = get_script_file_location(self.job_id,
                                                          temp_dir)

        logger.info("JobID {0}, started at {1}".format(self.job_id,
                                                       self.start_time))

    def _generate_hive_script(self, data_source):
        """Write the HQL to a local (temp) file
        """
        hq = self.hive_query.emr_hive_script(data_source, self.output_path,
                                             self.table_path)
        generate_hive_script_file(hq, self.local_script_file)

    def _generate_job_id(self):
        """Create a unique job run identifier
        """
        run_id = self.job_name + str(time.time())
        digest = hashlib.md5(six.b(run_id)).hexdigest()
        return 'hj-' + digest

    def _make_unique_job_key(self, label=None, owner=None):
        """Come up with a useful unique ID for this job
        """
        # use the name of the job as the label if one wasn't explicitly
        # specified
        if not label:
            label = self.job_name

        if not owner:
            try:
                owner = getpass.getuser()
            except:
                owner = 'no_user'

        now = datetime.datetime.utcnow()
        return '%s.%s.%s.%06d' % (
            label, owner,
            now.strftime('%Y%m%d.%H%M%S'), now.microsecond)

    def _generate_and_upload_hive_script(self):
        self._generate_hive_script(self.data_path)
        upload_file_to_s3(self.local_script_file, self.script_path)

    #  hooks for the with statement ###

    def __enter__(self):
        """Don't do anything special at start of with block"""
        return self

    def __exit__(self, type, value, traceback):
        """Call self.cleanup() at end of with block."""
        self.cleanup()

    def run(self):
        """Run the Hive job on EMR cluster
        """
        #  copy the data source to a new object
        #  (Hive deletes/moves the original)
        copy_s3_file(self.input_path, self.data_path)

        # and create the hive script
        self._generate_and_upload_hive_script()

        logger.info("Waiting {} seconds for S3 eventual consistency".format(
                    self.s3_sync_wait_time))
        time.sleep(self.s3_sync_wait_time)

        # TODO more options like setting aws region
        conn = EmrConnection(self.aws_access_key_id,
                             self.aws_secret_access_key)

        setup_step = InstallHiveStep(self.hive_version)
        run_step = HiveStep(self.job_name, self.script_path)

        cluster_id = conn.run_jobflow(
            self.job_key,
            self.log_path,
            action_on_failure='CANCEL_AND_WAIT',
            master_instance_type=self.master_instance_type,
            slave_instance_type=self.slave_instance_type,
            ami_version=self.ami_version,
            num_instances=self.num_instances,
            job_flow_role=self.iam_instance_profile,
            service_role=self.iam_service_role,
            visible_to_all_users=self.visible_to_all_users)

        conn.add_jobflow_steps(cluster_id, [setup_step, run_step])

        logger.info("Job started on cluster {0}".format(cluster_id))

        self._wait_for_job_to_complete(conn, cluster_id)

        logger.info("Output file is in: {0}".format(self.output_path))

    def cleanup(self):
        # TODO _ remove scratch dirs?
        logger.info("cleaning up ... ")

    # wait for job and log status
    # this method extracted from mrjob.job
    def _wait_for_job_to_complete(self, conn, cluster_id):
        """
        Wait for the job to complete, and raise an exception if
        the job failed.

        Also grab log URI from the job status (since we may not know it)
        """
        success = False
        chk_status_freq = self.check_emr_status_every
        # opts = {'check_emr_status_every': 30}
        # s3_logs = self.log_path
        emr_job_start = self.start_time

        while True:
            # don't antagonize EMR's throttling
            logger.debug('Waiting {0} seconds'.format(chk_status_freq))
            time.sleep(chk_status_freq)

            cluster = conn.describe_cluster(cluster_id)

            job_state = cluster.status.state
            reason = getattr(
                getattr(cluster.status, 'statechangereason', None),
                'message', '')

            # find all steps belonging to us, and get their state
            step_states = []
            running_step_name = ''
            total_step_time = 0.0
            step_nums = []  # step numbers belonging to us. 1-indexed

            steps = conn.list_steps(cluster.id).steps or []
            for i, step in enumerate(steps):

                # ignore steps belonging to other jobs
                if not step.name.startswith(self.job_name):
                    continue

                step_nums.append(i + 1)

                step_states.append(step.status.state)
                if step.status.state == 'RUNNING':
                    running_step_name = step.name

                if hasattr(step, 'startdatetime') and \
                   hasattr(step, 'enddatetime'):

                    start_time = iso8601_to_timestamp(step.startdatetime)
                    end_time = iso8601_to_timestamp(step.enddatetime)
                    total_step_time += end_time - start_time

            if not step_states:
                raise AssertionError("Can't find our steps in the job flow!")

            # if all our steps have completed, we're done!
            if all(state == 'COMPLETED' for state in step_states):
                success = True
                break

            # if any step fails, give up
            if any(state in ('FAILED', 'CANCELLED') for state in step_states):
                break

            # (the other step states are PENDING and RUNNING)

            # keep track of how long we've been waiting
            running_time = time.time() - emr_job_start

            # logging message values
            loginfo = (int(running_time), job_state, reason, running_step_name)

            # otherwise, we can print a status message
            if running_step_name:
                logger.info("Job launched {0} ago. "
                            " Status {1}: {2} ({3})".
                            format(*loginfo))

            # other states include STARTING and SHUTTING_DOWN
            elif reason:
                logger.info("Job launched {0} ago. "
                            "Status {1}: {2}".format(*loginfo))
            else:
                logger.info("Job launched {0} ago. "
                            "Status {1}".format(*loginfo))

        if success:
            logger.info("Job completed on cluster {}.".format(cluster.id))
            logger.info("Running time was {0} "
                        "(excludes time spent waiting for the EC2 instances)".
                        format(total_step_time))
        else:
            msg = "Job on cluster {0} failed with status {1}: {2}".format(
                  cluster.id, job_state, reason)
            logger.info(msg)

            raise Exception(msg)

#  AWS Date-time parsing

# sometimes AWS gives us seconds as a decimal, which we can't parse
# with boto.utils.ISO8601
SUBSECOND_RE = re.compile('\.[0-9]+')

# Thu, 29 Mar 2012 04:55:44 GMT
RFC1123 = '%a, %d %b %Y %H:%M:%S %Z'

def iso8601_to_timestamp(iso8601_time):
    iso8601_time = SUBSECOND_RE.sub('', iso8601_time)
    try:
        return time.mktime(time.strptime(iso8601_time, boto.utils.ISO8601))
    except ValueError:
        return time.mktime(time.strptime(iso8601_time, RFC1123))

def iso8601_to_datetime(iso8601_time):
    iso8601_time = SUBSECOND_RE.sub('', iso8601_time)
    try:
        return datetime.strptime(iso8601_time, boto.utils.ISO8601)
    except ValueError:
        return datetime.strptime(iso8601_time, RFC1123)


import scipy.io as spio
import numpy as np
import time
import os.path
from SBM_DC_nested import compute_likelihood

# settings
networks = [line.rstrip('\n') for line in open('networks.txt')]
nested = [False]
DC = [True]
iters = 10

# paths
inputpath = '../../data/sparsified_matrices/'
suffix_list = ['_linkrem10', '_linkrem10_linkrem20noconn']
inputvar = 'matrices'
outputpath = './output/'
temppath = './TEMP/'
timepath = './time/'

for suffix in suffix_list:
    for nes in nested:
        for deg in DC:

            if deg==False:
                DC_str = ''
            else:
                DC_str = '_DC'

            if nes==False:
                nested_str = ''
            else:
                nested_str = '_N'

            for net in networks:
                x = (spio.loadmat(inputpath + net + suffix + '.mat'))[inputvar]
                N = x[1][0].shape[0]
                if N <= 100:
                    sweeps = 100
                elif N <= 1000:
                    sweeps = 50
                else:
                    sweeps = 10

                for i in range(iters):
                    outfile = outputpath + net + '_' + str(i+1) + DC_str + nested_str + suffix + '_scores.txt'
                    tempfile = temppath + net + '_' + str(i+1) + DC_str + nested_str + suffix + '.TEMP'
                    timefile = timepath + net + '_' + str(i+1) + DC_str + nested_str + suffix + '_time.txt'
                    if (not (os.path.isfile(outfile))) and (not (os.path.isfile(tempfile))):
                        os.mknod(tempfile)

                        print(net + ' ' + str(i+1) + ' (DC = ' + str(deg) + ', Nested = ' + str(nes) + ') sweeps=' + str(sweeps) + ' ' + suffix, flush=True)
                        start = time.time()
                        probs = compute_likelihood(x[i][0], deg, nes, sweeps)
                        np.savetxt(outfile, probs, fmt=['%d','%d','%.18e'])
                        end = time.time()
                        print(end-start, flush=True)

                        file = open(timefile, 'w')
                        file.write(str(end-start))
                        file.close()

                        os.remove(tempfile)


"""Perform inference on an example and visualize the attention maps."""
import numpy as np
import torch
from nets import AttentionUNet
from utils.interpretation import BlockActivations
from utils import load_preprocess_image

import cv2
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

import config
from config import PATCH_SIZE

from viz_common import parser, _kwargs

from typing import List
import json
import argparse

with open("dataset_statistics.json") as f:
    dataset_stats_ = json.load(f)

if torch.cuda.is_available():
    from torch.backends import cudnn
    cudnn.benchmark = True
    DEVICE = "cuda"
else:
    DEVICE = "cpu"

parser.prog = "visualize-attention"
parser.description = "Visualize attention maps."

args = parser.parse_args()

model = AttentionUNet(**_kwargs)
if args.weights is not None:
    state_dict = torch.load(args.weights)
    model.load_state_dict(state_dict['model_state_dict'])
else:
    import warnings
    warnings.warn("Model weights not loaded.")
model.to(DEVICE)

img, img_t = load_preprocess_image(args.img, gray=args.gray)
img_t = img_t.to(DEVICE)
input_size = img.shape[:2]
att_viz = BlockActivations(model, "att")

# Perform prediction

with torch.no_grad():
    prediction_ = model(img_t)
    probas_ = torch.softmax(prediction_, dim=1)
    prediction_ = prediction_.data.cpu().numpy()[0, 1]
    probas_ = probas_.data.cpu().numpy()[0, 1]

# Plotting logic

fig: plt.Figure = plt.figure(figsize=(7, 8))
gs = fig.add_gridspec(3, 4)

ax = fig.add_subplot(gs[0, :2])
ax.imshow(img)
ax.set_title("Initial image")
ax.axis('off')

ax = fig.add_subplot(gs[0, 2:])
# ax.imshow(prediction_)
# ax.set_title("Network output")
# ax.axis('off')

# ax = fig.add_subplot(gs[0, 3])
ax.imshow(probas_)
ax.set_title("Proba map")
ax.axis('off')

for idx, (name, arr) in enumerate(att_viz.get_maps(img_t)):
    i_loc = 2 * idx + 4
    arr = arr[0, 0]
    ax = fig.add_subplot(gs[i_loc:i_loc+2])
    ims_ = ax.imshow(arr, cmap='viridis')
    ax.set_title("Attention Map: {:s}".format(name))
    ax.axis('off')
    # Plot colorbar on 1st map
    if idx==0:
        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='5%', pad=0.05)
        plt.colorbar(ims_, cax=cax)

fig.tight_layout()
plt.show()

if args.save_path is not None:
    fname = "{:s}.png".format(args.save_path)
    fig.savefig(fname, bbox_inches=None)


import config
import datetime
import threading
import time
from whiteboard import whiteboard
import config_data_collection
from ca_logging import log
import helper_functions as helper
import pyrebase_multiple_refs
import math
from termcolor import colored
import config_modules

####################################################################################################
##                                          Stream handlers                                       ##
####################################################################################################

def stream_handler_users_ref(message):
    if server.server_started == True:
        client_id = message["path"][1:]
        server.new_client(client_id)

def filter_client_id_data(message):
    data = message["data"]
    path = message["path"]
    # print(data, path)
    if data:
        try:
            if config.FIREBASE_KEY_SOURCE in data.keys() and data[config.FIREBASE_KEY_SOURCE] == config.FIREBASE_VALUE_SOURCE_AGENT:
                return False, False
            if config.FIREBASE_KEY_CLIENTID in data.keys():
                client_id = data[config.FIREBASE_KEY_CLIENTID]
            else:
                first_key = next(iter(data))
                client_id = data[first_key][config.FIREBASE_KEY_CLIENTID]
            if client_id and client_id in server.clients_services:
                server.reset_timer(client_id)
                return client_id, data
            else:
                return False, False
        except KeyError as e:
            log.warn("No client_id key")
    return False, False

def stream_handler_datacollection_ref(message):
    client_id, data = filter_client_id_data(message)
    if client_id:
        topic = config.MSG_DATACOL_IN + client_id
        whiteboard.publish(data, topic)
        if config.FIREBASE_KEY_FOOD_DIAGNOSIS_ANSWERS in data.keys() and data[config.FIREBASE_KEY_FOOD_DIAGNOSIS_ANSWERS]:
            topic = config.MSG_HEALTH_DIAGNOSTIC_IN + client_id
            whiteboard.publish(data[config.FIREBASE_KEY_FOOD_DIAGNOSIS_ANSWERS], topic)

def stream_handler_dialog_ref(message):
    client_id, data = filter_client_id_data(message)
    if client_id:
        topic = config.MSG_DATACOL_IN + client_id
        if config.FIREBASE_KEY_TEXT in data.keys():
            utterance = data[config.FIREBASE_KEY_TEXT]
        elif config.FIREBASE_LIKED_RECIPES in data.keys():
            utterance = data[config.FIREBASE_LIKED_RECIPES]
        # publish for data collection
        whiteboard.publish({config_data_collection.DIALOG: data}, topic)
        # distribute to NLU
        topic = config.MSG_SERVER_IN + client_id
        whiteboard.publish(utterance, topic)

def stream_handler_rsdata_ref(message):
    client_id, data = filter_client_id_data(message)
    if client_id:
        topic = config.MSG_RS_IN + client_id
        log.debug(message)
        whiteboard.publish(data, topic)

def get_path_in_sessions(client_id, key=None):
    if key:
        return config.FIREBASE_KEY_SESSIONS + '/' + client_id + '/' + key
    else:
        return config.FIREBASE_KEY_SESSIONS + '/' + client_id

class ServerUsingFirebase:
    """Singleton class"""
    __instance = None

    @staticmethod
    def getInstance():
        """
        :return: the unique ServerUsingFirebase object
        """
        if ServerUsingFirebase.__instance == None:
            ServerUsingFirebase()
        return ServerUsingFirebase.__instance

    def __init__(self):
        if ServerUsingFirebase.__instance != None:
            log.debug("Calling constructor of ServerUsingFirebase")
        else:
            ServerUsingFirebase.__instance = self
            log.warn("THIS MESSAGE SHOULD APPEAR ONLY ONCE...")

            self.name = "Server"
            self.clients_services = dict()
            self.n_clients = 0
            self.clients_threads = dict()
            self.timer_threads = dict()
            self.server_started = False

            self.firebase_root_ref = pyrebase_multiple_refs.PyrebaseMultipleRefs()
            self.firebase_users_ref = self.firebase_root_ref.new_ref(config.FIREBASE_KEY_USERS)
            self.firebase_streams_users = None
            self.firebase_streams_dialog = dict()
            self.firebase_streams_datacol = dict()

    ####################################################################################################
    ##                                          General Methods                                       ##
    ####################################################################################################

    # @overrides(ds_manager.DSManager)
    def start_service(self):
        try:
            self.firebase_streams_users = self.firebase_users_ref.stream(stream_handler_users_ref, stream_id="stream_users")
            time.sleep(2)
            self.server_started = True
        except KeyboardInterrupt:
            self.quit()

    # @overrides(ds_manager.DSManager)
    def quit(self, gui_quit=False):
        self.stop_all_services()
        log.info("------------ QUIT ------------")
        if not gui_quit:
            exit(0)

    ####################################################################################################
    ##                                Methods related to firebase                                     ##
    ####################################################################################################

     # @overrides(ds_manager.DSManager)
    def publish_for_client(self, message, client_id, firebase_key):
        # log.debug("In publish_for_client")
        # log.debug("firebase key")
        # log.debug(firebase_key)
        if client_id in self.clients_services:
            # ref = self.firebase_db.child(config.FIREBASE_KEY_SESSIONS).child(client_id)
            if firebase_key == config.FIREBASE_KEY_DIALOG or firebase_key == config.FIREBASE_KEY_ACK or firebase_key == config.FIREBASE_KEY_XP_COND or firebase_key == config.FIREBASE_KEY_RECIPE:

                if firebase_key == config.FIREBASE_KEY_DIALOG:
                    message_to_send, sentences_and_delays = dict(), None
                    for key, value in message.items():
                        if key != 'sentences_and_delays':
                            message_to_send[key] = value
                        else:
                            sentences_and_delays = value
                    n_messages_to_send = len(sentences_and_delays)
                    for i, elt in enumerate(sentences_and_delays):
                        sentence, delay = elt['sentence'], elt['delay']
                        if delay:
                            time.sleep(delay)
                        message_to_send['sentence'] = sentence
                        message_to_send['delay'] = delay
                        message_to_send['wait_for_more'] = True if i < (n_messages_to_send - 1) else False
                        timestamp = datetime.datetime.now().__str__()
                        message_to_send[config.FIREBASE_KEY_DATETIME] = timestamp
                        message_to_send[config.FIREBASE_KEY_SOURCE] = config.FIREBASE_VALUE_SOURCE_AGENT
                        # if message_to_send['food_recipe']:
                        #     message_to_send['rid'] = message_to_send['food_recipe']['id']
                        # message_to_send['food_recipe'] = None

                        # if 'utilities' in message_to_send.keys():
                        #     message_to_send['utilities'] = None
                        # if 'cf_scores' in message_to_send.keys():
                        #     message_to_send['cf_scores'] = None
                        # if 'relaxed_constraints' in message_to_send.keys():
                        #     message_to_send['relaxed_constraints'] = None

                        print(colored(message_to_send, 'blue'))
                        print(type(message_to_send))
                        self.firebase_root_ref.push_at(message_to_send, path=get_path_in_sessions(client_id=client_id, key=firebase_key))
                        # if 'recipe_card' in message_to_send and message_to_send['recipe_card']:
                        #     self.firebase_root_ref.put_here(message_to_send['recipe_card'])
                        #     print(colored("trying to push card", 'red'))
                        # publish for data collection
                        topic = config.MSG_DATACOL_IN + client_id
                        for_data_col = dict()
                        for_data_col[config_data_collection.DIALOG] = message_to_send
                        whiteboard.publish(message=for_data_col, topic=topic)

                elif firebase_key == config.FIREBASE_KEY_RECIPE:
                    timestamp = datetime.datetime.now().__str__()
                    message_to_send = message
                    message_to_send[config.FIREBASE_KEY_DATETIME] = timestamp
                    message_to_send[config.FIREBASE_KEY_SOURCE] = config.FIREBASE_VALUE_SOURCE_AGENT
                    self.firebase_root_ref.push_at(message_to_send, path=get_path_in_sessions(client_id=client_id, key=config.FIREBASE_KEY_DIALOG))
                    # publish for data collection
                    topic = config.MSG_DATACOL_IN + client_id
                    for_data_col = dict()
                    for_data_col[config_data_collection.DIALOG] = message_to_send
                    whiteboard.publish(message=for_data_col, topic=topic)

                elif firebase_key == config.FIREBASE_KEY_XP_COND or (isinstance(message, dict) and (config.FIREBASE_KEY_DATA_RECO in message.keys() or config.FIREBASE_KEY_RS_EVAL_DATA in message.keys())):
                    self.firebase_root_ref.update_at(message, path=get_path_in_sessions(client_id))

                else:
                    data = dict()
                    data[config.FIREBASE_KEY_ACK] = True
                    self.firebase_root_ref.update_at(data, path=get_path_in_sessions(client_id))
                helper.print_message(self.name, "published", message, topic=firebase_key)
            else:
                log.error("Calling publish_for_client with firebase key %s. Was expecting topic %s, %s or %s" % (firebase_key, config.FIREBASE_KEY_ACK, config.FIREBASE_KEY_DIALOG, config.FIREBASE_KEY_RECIPE))
                self.quit()
        else:
            log.error("%s, publish_for_client: unknown client %s" % (self.name, client_id))

    ####################################################################################################
    ##                                          Timer methods                                         ##
    ####################################################################################################

    def start_timer(self, client_id):
        timer = threading.Timer(config.CONNECTION_TIMEOUT, function=self.stop_services, args=(client_id,))
        timer.name = client_id
        timer.start()
        self.timer_threads[client_id] = timer

    def reset_timer(self, client_id):
        if client_id not in self.timer_threads.keys():
            log.warn("%s: client %s  is already stopped!" % (self.name, client_id))
        else:
            self.timer_threads[client_id].cancel()
            self.start_timer(client_id)

    ####################################################################################################
    ##                                             New client                                         ##
    ####################################################################################################

    def new_client(self, client_id):
        """
        Creates dedicated services for client on first connection
        :param msg: client's message
        """

        log.debug("%s: new client, id = %s" % (self.name, client_id))
        # Listeners: for modules
        if config.DOMAIN == config.DOMAIN_RS_EVAL:
            self.subscribe_whiteboard(config.MSG_RS_OUT + client_id)
        else:
            self.subscribe_whiteboard(config.MSG_NLG + client_id)
        self.subscribe_whiteboard(config.MSG_DATACOL_OUT + client_id)
        # Listener: for client
        dialog_ref = self.firebase_root_ref.new_ref(get_path_in_sessions(client_id, config.FIREBASE_KEY_DIALOG))
        datacol_ref = self.firebase_root_ref.new_ref(get_path_in_sessions(client_id, config.FIREBASE_KEY_DATACOLLECTION))
        rs_data_ref = self.firebase_root_ref.new_ref(get_path_in_sessions(client_id, config.FIREBASE_KEY_RS_DATA))
        self.firebase_streams_dialog[client_id] = dialog_ref.stream(stream_handler_dialog_ref, stream_id=client_id+"dialog")
        self.firebase_streams_datacol[client_id] = datacol_ref.stream(stream_handler_datacollection_ref, stream_id=client_id+"dialog")
        self.firebase_streams_dialog[client_id] = rs_data_ref.stream(stream_handler_rsdata_ref, stream_id=client_id+"RSdata")
        # Create services
        self.create_services(client_id)
        self.start_timer(client_id)
        self.publish_for_client(True, client_id, firebase_key=config.FIREBASE_KEY_ACK)

    ####################################################################################################
    ##                                Methods related to local whiteboard                             ##
    ####################################################################################################
    def publish_whiteboard(self, message, topic):
        whiteboard.publish(message, topic)

    def on_whiteboard_message(self, message, topic):
        client_id = topic.split("/")[-1]
        if config.MSG_DATACOL_OUT in topic:
            self.publish_for_client(message, client_id, firebase_key=config.FIREBASE_KEY_ACK)
        elif config.MSG_NLG in topic:
            # print(colored(message, 'blue'))
            self.publish_for_client(message, client_id, firebase_key=config.FIREBASE_KEY_DIALOG)
            if isinstance(message, dict) and "intent" in message.keys() and message["intent"] == "bye":
                self.publish_for_client(message, client_id, firebase_key=config.FIREBASE_KEY_ACK)
        elif config.MSG_RS_OUT in topic:
            self.publish_for_client(message, client_id, firebase_key=config.FIREBASE_KEY_RECIPE)
        else:
            log.critical("Not implemented yet")

    def subscribe_whiteboard(self, topic):
        log.debug("%s subsdribing to %s" %(self.name, topic))
        whiteboard.subscribe(subscriber=self, topic=topic)

    ####################################################################################################
    ##                                Methods related to local modules                                ##
    ####################################################################################################

    def create_services(self, client_id):
        self.clients_services[client_id] = list()
        for module_config in config_modules.modules.modules:
            args = list(module_config.values())[2:]
            # print(module_config)
            new_module_name = module_config["name"]
            if new_module_name == "NLG":
                if config.aamas_study_CS:
                    cs = config.possible_CS[self.n_clients % len(config.possible_CS)]
                    self.publish_for_client({config.FIREBASE_KEY_XP_COND: cs}, client_id, config.FIREBASE_KEY_XP_COND)
                    self.publish_whiteboard({config.FIREBASE_KEY_XP_COND: cs}, config.MSG_DATACOL_IN+client_id)
                    msg = "XP condition, cs = " + cs
                    print(colored(msg, "cyan"))
                    log.debug(msg)
                else:
                    cs = config.exp_cs_control
                args.append(cs)
            # print(module_config["module"])
            new_module = module_config["module"](client_id, *args)

            self.clients_services[client_id].append(new_module)

        self.n_clients += 1
        for s in self.clients_services[client_id]:
            s.start_service()

        self.publish_whiteboard({config_data_collection.CLIENT_ID: client_id}, config.MSG_DATACOL_IN+client_id)

    def close_stream_for_client(self, client_id, streams_dict):
        if client_id in streams_dict.keys():
            try:
                streams_dict[client_id].close()
            except AttributeError as e:
                log.warn("AttributeError: %s. Not solved." % e)
            del streams_dict[client_id]

    def stop_services(self, client_id):
        log.info("Shutting down services for client %s" % client_id)
        if client_id in self.clients_services.keys():
            for c in self.clients_services[client_id]:
                c.stop_service()
        if client_id in self.timer_threads.keys():
            self.timer_threads[client_id].cancel()
        self.close_stream_for_client(client_id, self.firebase_streams_dialog)
        self.close_stream_for_client(client_id, self.firebase_streams_datacol)

    def stop_all_services(self):
        log.debug("in stop_all_services, thread(s) left:")
        log.debug(threading.enumerate())
        for client_id, services_list in self.clients_services.items():
            # for service in service_dict.values():
            #     service.stop_service()
            self.stop_services(client_id)
            if client_id in self.timer_threads.keys():
                self.timer_threads[client_id].cancel()
            time.sleep(0.1)
        self.firebase_streams_users.close()
        log.debug("in stop_all_services, thread(s) left:")
        log.debug(threading.enumerate())

server = ServerUsingFirebase.getInstance()


# REFERENCE https://www.geeksforgeeks.org/search-and-insertion-in-k-dimensional-tree/
import random
import matplotlib.pyplot as plt

class Node:
    def __init__(self, point, left=None, right=None):
        self.point = point
        self.left = left
        self.right = right

def build_kd_tree(points, depth=0):
    if len(points) == 0:
        return None

    k = len(points[0])
    axis = depth % k
    points.sort(key=lambda x: x[axis])

    mid = len(points) // 2
    node = Node(points[mid], axis)

    node.left = build_kd_tree(points[:mid], depth + 1)
    node.right = build_kd_tree(points[mid + 1:], depth + 1)

    return node

points = [(random.uniform(-10, 10), random.uniform(-10, 10)) for _ in range(200)]
kd_tree = build_kd_tree(points)

ranges = [
    ([-1, 1], [-2, 2]),
    ([-2, 1], [3, 5]),
    ([-7, 0], [-6, 4]),
    ([-2, 2], [-3, 3]),
    ([-7, 5], [-3, 1])
]

colors = ['red', 'blue', 'green', 'orange', 'purple']

for i, point in enumerate(points):
    x, y = point

    if -1 < x < 1 and -2 < y < 2:
        plt.scatter(x, y, c=colors[0], label=f'Range 1' if i == 0 else '')
    elif -2 < x < 1 and 3 < y < 5:
        plt.scatter(x, y, c=colors[1], label=f'Range 2' if i == 0 else '')
    elif -7 < x < 0 and -6 < y < 4:
        plt.scatter(x, y, c=colors[2], label=f'Range 3' if i == 0 else '')
    elif -2 < x < 2 and -3 < y < 3:
        plt.scatter(x, y, c=colors[3], label=f'Range 4' if i == 0 else '')
    elif -7 < x < 5 and -3 < y < 1:
        plt.scatter(x, y, c=colors[4], label=f'Range 5' if i == 0 else '')
    else:
        plt.scatter(x, y, c='black', label='Outside Ranges' if i == 0 else '')

plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()


import torch.nn as nn
import copy
import torch
from utils import *

def build_model(configfile):
    pass

class BaseModel(nn.Module):

    def __init__(self, device=None):
        if device is None:
            self.device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
        else:
            self.device = device
        torch.set_default_dtype(torch.double)
        super(BaseModel, self).__init__()

    def load_recent(self, path):
        model = mostrecent(path)
        self.load_state_dict(torch.load(model))

    def copy(self):
        new_model = copy.deepcopy(self)
        return new_model

    # cast to a proper tensor
    def preprocess_state(self, state):
        if not isinstance(state, torch.Tensor):
            try:
                out = torch.tensor(state).to(self.device)
            except:
                raise TypeError('could not cast %s to tensor' % type(state))
        else:
            try:
                out = state.to(self.device)
            except:
                raise TypeError('coud not cast to %s' % self.device)
        return out

    # use an optional backwards hook to clamp gradients
    # from https://stackoverflow.com/questions/54716377/how-to-do-gradient-clipping-in-pytorch
    # call once and good to go
    def clampgrad(self, clip=1):
        for p in self.parameters():
            p.register_hook(lambda grad: torch.clamp(grad, -clip, clip))

    def __call__(self, state):
        state = self.preprocess_state(state)
        output = super(BaseModel, self).__call__(state)
        return output

class WrappedModel(BaseModel):

    def __init__(self, model):
        super(WrappedModel, self).__init__()
        self.model = model

    def __call__(self, state):
        state = self.preprocess_state(state)
        output = self.model(state)
        return output


import pyrealsense2 as rs
import cv2

import numpy as np
import pandas as pd
import os
import time

import dlib
import face_recognition as facerec
import tensorflow as tf

#=============== Reference Link ===============
#
# Face Recognition
#http://www.daydev.com/developer/s6-programming-language/python/face-recognition-real-time-python-opencv.html
#
# Intel Realsense SDK
#https://github.com/IntelRealSense/librealsense
#
# Tensorflow: Binary classifier
#https://towardsdatascience.com/performing-classification-in-tensorflow-95368fde289c
#https://medium.com/analytics-vidhya/save-and-load-a-tensorflow-estimator-model-for-predictions-233b798620a9
#
#=============================================

# Detect data during VIDEO
data_locations = []
data_encodings = []
data_names = []
frameProcess = True
# Database for recognition
face_names = []
face_encodings = []

# Attribute for frame
colorF = (255, 0, 0)
colorL = (255, 255, 255)
colorT = (0, 0, 255)
font = cv2.FONT_HERSHEY_SIMPLEX

name = 'UNKNOWN'

# Import all face from ./Profile
# Name your picture as your name
# Such as "PRUETIKORN.jpg"
entries = os.listdir('Profile/')
for pic in entries:
    if '.jpg' in pic or '.png' in pic:
        face_names.append(pic.split('_')[0])
        database_image = facerec.load_image_file("Profile/"+pic)
        database_encoding = facerec.face_encodings(database_image)[0]
        face_encodings.append(database_encoding)

# dataFrame from depth image
depth_points = {
    "Chin": [],
    "Nose": [],
    "RightEye1": [],
    "RightEye2": [],
    "LeftEye1": [],
    "LeftEye2": [],
    "MouthRight": [],
    "MouthTop": [],
    "MouthLeft": [],
    "MouthBottom": [],
}
input_fn = pd.DataFrame(data = depth_points, dtype = np.float64)

# Collecting data from depth image
depth_ps = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
data_ps = [9, 34, 37, 40, 43, 46, 49, 52, 55, 58]

# Load the model
path_model = './Model_dir/model'
new_model = tf.saved_model.load(path_model)

# Load the detector
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

#predict the output from the trained model
def predict(dfeval, importedModel):
    colNames = dfeval.columns
    dtypes = dfeval.dtypes
    predictions = []
    for row in dfeval.iterrows():
        example = tf.train.Example()
        for i in range(len(colNames)):
            dtype = dtypes[i]
            colName = colNames[i]
            value = row[1][colName]
            if dtype == "object":
                value = bytes(value, "utf-8")
                example.features.feature[colName].bytes_list.value.extend(
                    [value])
            elif dtype == "float":
                example.features.feature[colName].float_list.value.extend(
                    [value])
            elif dtype == "int":
                example.features.feature[colName].int64_list.value.extend(
                    [value])

        predictions.append(
           importedModel.signatures["predict"](
           examples=tf.constant([example.SerializeToString()])))
    return predictions

#optional features
max_time = 0
record = cv2.VideoWriter('presenter.avi', cv2.VideoWriter_fourcc(*'MJPG'), 10, (640, 480))

# Start streaming
pipeline.start(config)

try:
    while True:
        time_s = time.time()

        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        if not depth_frame or not color_frame:
            continue

        # Convert images to numpy arrays
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        resizing = cv2.resize(color_image, (0, 0), fx=0.25, fy=0.25)
        result = resizing[:, :, ::-1]
        gray = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)

        #Check that there is a face or not
        if frameProcess:
            data_l = detector(gray)
            data_n = []
            newPred = []
            for f in data_l:
                landmarks = predictor(gray, f)
                for n in data_ps:
                     x = landmarks.part(n).x
                     y = landmarks.part(n).y
                     try:
                         depth_ps[data_ps.index(n)] = depth_image[y, x]
                     except IndexError:
                         pass
            input_fn.loc[0] = depth_ps
            face_pred = predict(input_fn, new_model)
            for pred in face_pred:
                newPred.append(np.argmax(pred["probabilities"]))

            data_l = facerec.face_locations(result)
            data_e = facerec.face_encodings(result, data_l)
            for enc in data_e:
                recog = facerec.compare_faces(face_encodings, enc)
                name = 'UNKNOWN'
                if True in recog:
                    match_index = recog.index(True)
                    name = face_names[match_index]
                    data_n.append(name)

        frameProcess = not frameProcess

        for (t, r, b, l), n, p in zip(data_l, data_n, newPred):
            if p == 1:
                t *= 4
                r *= 4
                b *= 4
                l *= 4
                cv2.rectangle(color_image, (l, t), (r, b), colorF, 2)
                cv2.rectangle(color_image, (l, b-35), (r, b), colorF, cv2.FILLED)
                cv2.putText(color_image, n, (l+6, b-6), font, 1.0, colorL, 1)

        # Show images
        record.write(color_image)
        cv2.namedWindow('RealSense Face Recognition', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense Face Recognition', color_image)
        key = cv2.waitKey(1)

        process_time = time.time() - time_s
        print('\033[1;35;40m Time process: ' + str(process_time) + ' ms')
        if max_time < process_time:
            max_time = process_time

        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break

finally:
    print("\033[1;36;40m-----------------------------------------------------------------")
    print('Maximum time: ' + str(max_time) + ' ms')

    # Stop streaming
    pipeline.stop()


#coding=utf-8
from django.conf.urls import url

from . import views

urlpatterns = [

     url(r'^$',views.getindex),  #主页面
     url(r'^developer/$', views.getdeveloper),  # 主页面
     url(r'^table/$',views.gettable),  #设备管理页面
     url(r'^register/$',views.register),#注册账号
     url(r'^add/$',views.addlist),#添加设备
     url(r'^update/$',views.updatelist),#更新设备信息
     url(r'^del/$',views.dellist),#删除设备
     url(r'^detail/$',views.detail),#设备详细
     url(r'^open/$',views.open),#开设备
     url(r'^close/$',views.close),#关设备
     url(r'^logtable/$',views.get_log_table),#ajax设备log_table
     url(r'^AjaxTable/$',views.AjaxTable),#ajax所有设备表
     url(r'^user_set/$',views.user_set),#ajax用户更改密码
     url(r'^search_machine/$',views.detail),  #打开查找页面
     url(r'^warning_history/$',views.warning_histort),  #报警历史
     url(r'^ajax_warning/$',views.warning_history_ajax),  #报警历史
     url(r'client_model/$',views.client_model),

     url(r'^login/$',views.my_login), #登陆
     url(r'^logout/$',views.my_logout),#注销

]


import os
import sqlite3
class GfwList(object):
    def __init__(self,domain):
        self.domain= domain
        self.conn = sqlite3.connect('gfwlist.db')
    def _add_():
        c = self.conn.cursor()
        c.execute("insert in to gfw values ('%s')"%self.domain)
        self.conn.commit()
    def _check_from_db():
        c = self.conn.cursor()
        res = c.execute("select * from gfw")
        if self.domain in res:
            return True
        else:
            return False
    def _route_select():
        if _check_from_db():
            return True
        else:
            response = os.system("ping -n 1 " + self.domain)
            if response == 0:
                return False
            else:
                _add_()
                return False

route_select('www.google.com')


from flask import Flask, render_template, make_response
from router.customerRoute import customer
from router.consumerRoute import consumer

app = Flask(__name__)

app.register_blueprint(customer)
app.register_blueprint(consumer)

@app.route('/')
def homePage():
    return render_template('home.html')

if __name__ == '__main__':
    app.run(debug = True)


import numpy
import numpy as np
import PIL
from PIL import Image
import cv2
import random
from operator import xor

img = PIL.Image.open("samolot.jpg").convert('1')    #ladowanie obrazu
imgarr = numpy.array(img)
im_g=cv2.imread("samolot.jpg",0)

f = open("testb.txt", "wb")
f.writelines(imgarr)
pilimg = Image.fromarray(imgarr)

licznik_calosci=0
licznik_0=0
licznik_1=0
for pixel in iter(img.getdata()):                   #zamiana 255 na 1 oraz wypisanie DATA
    if pixel == 255:
        pixel=1
    print(pixel)
    if pixel ==1:
        licznik_1+=1
    else:
        licznik_0+=1
    licznik_calosci+=1
print("liczba elementow: ",licznik_calosci, '\n', "liczba 0: ",licznik_0,'\n', "liczba 1:" , licznik_1)

f.close()
#-----------------------------------------ZLICZANIE-POWTARZAJACYCH-SIE-SEKWENCJI---------------------------------------

f = open("testb.txt", "rb")
wystepowanie_11=0
wystepowanie_111=0
wystepowanie_1111=0
wystepowanie_11111=0
wystepowanie_00=0
wystepowanie_000=0
wystepowanie_0000=0
wystepowanie_00000=0

for line in f:
    if 255 in line:
        print("znalezionio cokolwiek")
        wystepowanie_11 = wystepowanie_11 +1
print(wystepowanie_11)
len(imgarr)

print(im_g)
f.close()


import cv2
import numpy as np

img = cv2.imread('base.png', 1)
rate = float(img.shape[0])/float(img.shape[1])
if img.shape[1] != 100 :
	height = int(100*rate)
	img = cv2.resize(img, (100, height), interpolation=cv2.INTER_CUBIC)

def custom_blur_demo(image):
    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
    dst = cv2.filter2D(image, -1, kernel=kernel)
    return dst

#img = custom_blur_demo(img)
cv2.imwrite('1.png', img)


import configparser
import psycopg2
from sql_queries import copy_table_queries, insert_table_queries

def load_staging_tables(cur, conn):

    """Function that copies the tables from the S3 bucket into our project
    Queries defined in sql_queries.py file"""

    for query in copy_table_queries:
        cur.execute(query)
        conn.commit()

def insert_tables(cur, conn):

    """Function that inserts the desired information from the copies made from the S3 bucket into the new tables with our sketched     structure
    Queries defined in sql_queries.py file"""

    for query in insert_table_queries:
        cur.execute(query)
        conn.commit()

def main():

    config = configparser.ConfigParser()
    config.read('dwh.cfg')

    conn = psycopg2.connect("host={} dbname={} user={} password={} port={}".format(*config['CLUSTER'].values()))
    cur = conn.cursor()

    load_staging_tables(cur, conn)
    insert_tables(cur, conn)

    conn.close()

if __name__ == "__main__":
    main()


from flask import Flask, redirect, render_template, request, session
from flask_socketio import SocketIO

app = Flask(__name__)
app.config["SECRET_KEY"] = "ABC"
socketio = SocketIO(app)
global answer
global current_user

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/redirect_login")
def redirect_login():
    username = request.values["username"]
    session["username"] = username
    return redirect("/login")

@app.route("/login")
def login():
    username = session.get("username")
    return render_template("login.html", username=username)

@app.route("/question")
def question():
    return render_template("question.html")

@socketio.on("set")
def set(data):
    global answer
    if data == "game_over":
        answer = None
        socketio.emit("set_q", "
Game Over!
")
        socketio.emit("set_qa", "
Game Over!
")
        socketio.emit("close_answer", "No one")
        return
    answer = data[1]
    socketio.emit(
        "set_q",
        f"
Question:{data[0]}
",
    )
    socketio.emit(
        "set_qa",
        f"
Question:{data[0]}

Answer:{data[1]}
",
    )
    socketio.emit("open_answer", "")

@socketio.on("press_answer")
def press_answer(data):
    global current_user
    username = session.get("username")
    current_user = username
    session_id = request.sid
    socketio.emit("close_answer", username)
    socketio.emit("open_message", "", room=session_id)

@socketio.on("send_message")
def send_message(data):
    global answer
    username = session.get("username")
    socketio.emit("set_box", f"{username}: {data}
")
    session_id = request.sid

    if data == answer:
        socketio.emit("set_box", f"{username} get the answer!
")
        socketio.emit("set_q", "
No question!
")
        socketio.emit("close_answer", "No one")
    else:
        socketio.emit("open_answer", "")

    socketio.emit("close_message", "", room=session_id)

if __name__ == "__main__":
    app.debug = True
    socketio.run(app, host="0.0.0.0")


from __future__ import annotations

from unittest import mock

import pytest

from airflow.exceptions import AirflowException
from airflow.providers.segment.hooks.segment import SegmentHook
from airflow.providers.segment.operators.segment_track_event import SegmentTrackEventOperator

TEST_CONN_ID = "test_segment"
WRITE_KEY = "foo"

class TestSegmentHook:
    def setup_method(self):

        self.conn = conn = mock.MagicMock()
        conn.write_key = WRITE_KEY
        self.expected_write_key = WRITE_KEY
        self.conn.extra_dejson = {"write_key": self.expected_write_key}

        class UnitTestSegmentHook(SegmentHook):
            def get_conn(self):
                return conn

            def get_connection(self, unused_connection_id):
                return conn

        self.test_hook = UnitTestSegmentHook(segment_conn_id=TEST_CONN_ID)

    def test_get_conn(self):
        expected_connection = self.test_hook.get_conn()
        assert expected_connection == self.conn
        assert expected_connection.write_key is not None
        assert expected_connection.write_key == self.expected_write_key

    def test_on_error(self):
        with pytest.raises(AirflowException):
            self.test_hook.on_error("error", ["items"])

class TestSegmentTrackEventOperator:
    @mock.patch("airflow.providers.segment.operators.segment_track_event.SegmentHook")
    def test_execute(self, mock_hook):
        # Given
        user_id = "user_id"
        event = "event"
        properties = {}

        operator = SegmentTrackEventOperator(
            task_id="segment-track",
            user_id=user_id,
            event=event,
            properties=properties,
        )

        # When
        operator.execute(None)

        # Then
        mock_hook.return_value.track.assert_called_once_with(
            user_id=user_id,
            event=event,
            properties=properties,
        )