# -*- coding:utf-8 -*-
# PROJECT_NAME : django-ueditor-plugin
# FILE_NAME    :
# AUTHOR       : younger shen
import json
from django.views.generic import View
from django.http import HttpResponse, Http404
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.csrf import csrf_exempt
from .utils import get_config
from .utils import upload_file
from .models import UeditorFile
# Create your views here.

class JsonResponse(HttpResponse):
    def __init__(self, context=None, encoder=DjangoJSONEncoder, safe=True, **kwargs):
        if safe and not isinstance(context, dict):
            raise TypeError('In order to allow non-dict objects to be '
                            'serialized set the safe parameter to False')

        data = json.dumps(context, cls=encoder)
        kwargs.setdefault('content_type', 'application/json')
        super(JsonResponse, self).__init__(content=data, **kwargs)

class JsonResponseMixin(object):
    content_type = 'application/json'
    response_class = JsonResponse

    def render_to_response(self, context, **response_kwargs):
        response_kwargs.setdefault('content_type', self.content_type)
        return self.response_class(context=context, **response_kwargs)

class JsonView(View, JsonResponseMixin):
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(*args, **kwargs)
        return self.render_to_response(context)

class ControllerView(JsonView):

    def __init__(self, *args, **kwargs):
        self.config = get_config()
        super(ControllerView, self).__init__(*args, **kwargs)

    def get(self, request, *args, **kwarg):
        action_data = self.get_action(request)
        if action_data:
            return self.render_to_response(action_data)
        else:
            raise Http404

    def get_context_data(self):
        return self.config

    def post(self, request, *args, **kwargs):
        action_data = self.get_action(request, *args, **kwargs)
        if action_data:
            return self.render_to_response(action_data)
        else:
            raise Http404

    def config_action(self, request):
        return self.config

    def get_action(self, request):
        action = request.GET.get('action', '')
        action_method = getattr(self, action + '_action')
        if action_method:
            return action_method(request)
        return None

    def listfile_action(self, request):
        allow_file_types = self.config.get('fileManagerAllowFiles')
        default_size = self.config.get('fileManagerListSize')
        size = request.GET.get('size', '')
        start = request.GET.get('start', 0)
        try:
            size = int(size)
        except (ValueError, TypeError):
            size = default_size

        end = int(start) + size
        files = UeditorFile.objects.filter(type__in=allow_file_types, status=UeditorFile.UPLOAD_SUCCEED).values_list('url', flat=True)
        import random
        files_list = map(lambda x: {'url': x, 'mtime': random.random()}, files[start: end])

        if files.exists():
            return {'state': 'SUCCESS',
                    'list': files_list,
                    'start': start,
                    'total': files.count()
                    }
        else:
            return {'state': 'no match files',
                    'list': [],
                    'start': start,
                    'total': 0
                    }

    def listimage_action(self, request):
        allow_file_types = self.config.get('imageManagerAllowFiles')
        default_size = self.config.get('imageManagerListSize')
        size = request.GET.get('size', '')
        start = request.GET.get('start', 0)
        try:
            size = int(size)
        except (ValueError, TypeError):
            size = default_size

        end = int(start) + size
        files = UeditorFile.objects.filter(type__in=allow_file_types, status=UeditorFile.UPLOAD_SUCCEED).values_list('url', flat=True)
        import random
        files_list = map(lambda x: {'url': x, 'mtime': random.random()}, files[start: end])

        if files.exists():
            return {'state': 'SUCCESS',
                    'list': files_list,
                    'start': start,
                    'total': files.count()
                    }
        else:
            return {'state': 'no match files',
                    'list': [],
                    'start': start,
                    'total': 0
                    }

    def uploadimage_action(self, request):
        config = self.config

        uploadimage_config = {'pathFormat': config.get('imagePathFormat', ''),
                              'maxSize': config.get('imageMaxSize', ''),
                              'allowFiles': config.get('imageAllowFiles', ''),
                              'fieldName': config.get('imageFieldName', '')
                              }

        return upload_file(request, uploadimage_config)

    def uploadscrawl_action(self, request):
        config = self.config

        uploadscrawl_config = {'pathFormat': config.get('scrawlPathFormat', ''),
                               'maxSize': config.get('scrawlMaxSize', ''),
                               'allowFiles': config.get('scrawlAllowFiles', ''),
                               'oriName': config.get('scrawOriName', 'scrawl.png'),
                               'fieldName': config.get('scrawlFieldName', ''),
                               'base64': 'base64'
                               }

        return upload_file(request, uploadscrawl_config, base64='base64')

    def uploadvideo_action(self, request):
        config = self.config

        uploadvideo_config = {'pathFormat': config.get('videoPathFormat', ''),
                              'maxSize': config.get('videoMaxSize', ''),
                              'allowFiles': config.get('videoAllowFiles', ''),
                              'fieldName': config.get('videoFieldName', ''),
                              }

        return upload_file(request, uploadvideo_config)

    def uploadfile_action(self, request):
        config = self.config

        uploadfile_config = {'pathFormat': config.get('filePathFormat', ''),
                             'maxSize': config.get('fileMaxSize', ''),
                             'allowFiles': config.get('fileAllowFiles', ''),
                             'fieldName': config.get('fileFieldName', '')
                             }

        return upload_file(request, uploadfile_config)

controller_view = csrf_exempt(ControllerView.as_view())


# Copyright (c) 2014 NetApp Inc.
# All Rights Reserved.
# Copyright (c) 2016 NEC Corporation.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""NAS learning manager managers creating learnings and access rights.

**Related Flags**

:learning_driver: Used by :class:`LearningManager`.
"""

from oslo_config import cfg
from oslo_log import log
from oslo_utils import excutils
from oslo_utils import importutils
from oslo_utils import timeutils

from meteos.common import constants
import meteos.engine.configuration
from meteos import manager

LOG = log.getLogger(__name__)

engine_manager_opts = [
    cfg.StrOpt('learning_driver',
               default='meteos.engine.drivers.generic.GenericLearningDriver',
               help='Driver to use for learning creation.'),
    cfg.StrOpt('port_for_online_prediction',
               default='55000',
               help='Port for online prediction'),
]

CONF = cfg.CONF
CONF.register_opts(engine_manager_opts)
CONF.import_opt('periodic_interval', 'meteos.service')

class LearningManager(manager.Manager):

    """Manages Learning resources."""

    RPC_API_VERSION = '1.0'

    def __init__(self, learning_driver=None, service_name=None,
                 *args, **kwargs):
        """Load the driver from args, or from flags."""
        self.configuration = meteos.engine.configuration.Configuration(
            engine_manager_opts,
            config_group=service_name)
        super(LearningManager, self).__init__(*args, **kwargs)

        if not learning_driver:
            learning_driver = self.configuration.learning_driver

        self.driver = importutils.import_object(
            learning_driver, configuration=self.configuration,
        )

    def _update_status(self, context, resource_name,
                       id, job_id, stdout, stderr):

        if stderr:
            status = constants.STATUS_ERROR
            LOG.error("Fail to create %s.", id)
        else:
            status = constants.STATUS_AVAILABLE
            LOG.info("%s created successfully.", id)

        updates = {
            'status': status,
            'job_id': job_id,
            'launched_at': timeutils.utcnow(),
            'stderr': stderr,
        }

        if resource_name == 'DataSet':
            updates['head'] = stdout
            self.db.dataset_update(context, id, updates)

        elif resource_name == 'Model':
            updates['stdout'] = stdout
            self.db.model_update(context, id, updates)

        elif resource_name == 'Model Evaluation':
            updates['stdout'] = stdout.rstrip('\n')
            self.db.model_evaluation_update(context, id, updates)

        elif resource_name == 'Learning':
            updates['stdout'] = stdout.rstrip('\n')
            self.db.learning_update(context, id, updates)

    def create_template(self, context, request_spec=None):
        """Creates a template."""
        context = context.elevated()

        LOG.debug("Create template with request: %s", request_spec)

        try:
            response = self.driver.create_template(
                context, request_spec)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("template %s failed on creation.",
                          request_spec['id'])
                self.db.template_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        LOG.info("template %s created successfully.",
                 request_spec['id'])

        updates = response
        updates['status'] = constants.STATUS_AVAILABLE
        updates['launched_at'] = timeutils.utcnow()

        self.db.template_update(context, request_spec['id'], updates)

    def delete_template(self, context, id=None):
        """Deletes a template."""
        context = context.elevated()

        try:
            template = self.db.template_get(context, id)
            self.driver.delete_template(context, template)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Template %s failed on deletion.", id)
                self.db.template_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        LOG.info("Template %s deleted successfully.", id)
        self.db.template_delete(context, id)

    def create_experiment(self, context, request_spec=None):
        """Creates a Experiment."""
        context = context.elevated()

        LOG.debug("Create experiment with request: %s", request_spec)

        try:
            template = self.db.template_get(
                context, request_spec['template_id'])
            cluster_id = self.driver.create_experiment(
                context, request_spec, template.sahara_image_id,
                template.cluster_template_id, template.spark_version)
            self.driver.wait_for_cluster_create(context, cluster_id)

            experiment = request_spec

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Experiment %s failed on creation.",
                          request_spec['id'])
                self.db.experiment_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        LOG.info("Experiment %s created successfully.",
                 experiment['id'])
        updates = {
            'status': constants.STATUS_AVAILABLE,
            'launched_at': timeutils.utcnow(),
            'cluster_id': cluster_id,
        }
        self.db.experiment_update(context, experiment['id'], updates)

    def delete_experiment(self, context, id=None):
        """Deletes a experiment."""
        context = context.elevated()

        try:
            experiment = self.db.experiment_get(context, id)
            self.driver.delete_experiment(context, experiment['cluster_id'])

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Experiment %s failed on deletion.", id)
                self.db.experiment_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        LOG.info("Experiment %s deleted successfully.", id)
        self.db.experiment_delete(context, id)

    def create_dataset(self, context, request_spec=None):
        """Create a Dataset."""
        context = context.elevated()

        LOG.debug("Create dataset with request: %s", request_spec)

        try:
            job_id = self.driver.create_dataset(context, request_spec)
            stdout, stderr = self.driver.get_job_result(
                context,
                job_id,
                request_spec['template_id'],
                request_spec['cluster_id'])

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Dataset %s failed on creation.",
                          request_spec['id'])
                self.db.dataset_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        self._update_status(context, 'DataSet', request_spec['id'],
                            job_id, stdout, stderr)

        if request_spec['test_dataset']:
            self._update_status(context,
                                'DataSet',
                                request_spec['test_dataset']['id'],
                                job_id,
                                None,
                                stderr)

    def delete_dataset(self, context, cluster_id=None, job_id=None, id=None):
        """Deletes a Dataset."""
        context = context.elevated()

        try:
            self.driver.delete_dataset(context, cluster_id, job_id, id)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Dataset %s failed on deletion.", id)
                self.db.dataset_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        LOG.info("Dataset %s deleted successfully.", id)
        self.db.dataset_delete(context, id)

    def create_model(self, context, request_spec=None):
        """Create a Model."""
        context = context.elevated()

        LOG.debug("Create model with request: %s", request_spec)

        try:
            job_id = self.driver.create_model(context, request_spec)
            stdout, stderr = self.driver.get_job_result(
                context,
                job_id,
                request_spec['template_id'],
                request_spec['cluster_id'])

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model %s failed on creation.",
                          request_spec['id'])
                self.db.model_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        self._update_status(context, 'Model', request_spec['id'],
                            job_id, stdout, stderr)

    def delete_model(self, context, cluster_id=None, job_id=None, id=None,
                     recreate=False):
        """Deletes a Model."""
        context = context.elevated()

        try:
            self.driver.delete_model(context, cluster_id, job_id, id)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model %s failed on deletion.", id)
                self.db.model_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        LOG.info("Model %s deleted successfully.", id)

        if not recreate:
            self.db.model_delete(context, id)

    def load_model(self, context, request_spec=None):
        """Load a Model."""
        context = context.elevated()

        LOG.debug("Load model with request: %s", request_spec)

        port = self.configuration.port_for_online_prediction
        request_spec['port'] = port

        try:
            self.driver.load_model(context, request_spec)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model %s failed on loading.",
                          request_spec['id'])
                self.db.model_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        self.db.model_update(context,
                             request_spec['id'],
                             {'status': constants.STATUS_ACTIVE})

    def unload_model(self, context, request_spec=None):
        """Unload a Model."""
        context = context.elevated()
        port = self.configuration.port_for_online_prediction
        request_spec['port'] = port

        LOG.debug("Unload model with request: %s", request_spec)

        try:
            self.driver.unload_model(context, request_spec)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model %s failed on unloading.",
                          request_spec['id'])
                self.db.model_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        self.db.model_update(context,
                             request_spec['id'],
                             {'status': constants.STATUS_AVAILABLE})

    def create_model_evaluation(self, context, request_spec=None):
        """Create a Model Evaluation."""
        context = context.elevated()

        model_evaluation_id = request_spec['id']
        LOG.debug("Create model evaluation with request: %s", request_spec)

        try:
            job_id = self.driver.create_model_evaluation(context, request_spec)
            stdout, stderr = self.driver.get_job_result(
                context,
                job_id,
                request_spec['template_id'],
                request_spec['cluster_id'])

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model Evaluation %s failed on creation.",
                          model_evaluation_id)
                self.db.model_evaluation_update(
                    context, model_evaluation_id,
                    {'status': constants.STATUS_ERROR}
                )

        self._update_status(context, 'Model Evaluation', model_evaluation_id,
                            job_id, stdout, stderr)

    def delete_model_evaluation(self, context, cluster_id=None,
                                job_id=None, id=None):
        """Deletes a Model Evaluation."""
        context = context.elevated()

        try:
            self.driver.delete_model_evaluation(context,
                                                cluster_id,
                                                job_id,
                                                id)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Model Evaluation %s failed on deletion.", id)
                self.db.model_evaluation_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        self.db.model_evaluation_delete(context, id)
        LOG.info("Model Evaluation %s deleted successfully.", id)

    def create_learning(self, context, request_spec=None):
        """Create a Learning."""
        context = context.elevated()

        learning_id = request_spec['id']
        LOG.debug("Create learning with request: %s", request_spec)

        try:
            job_id = self.driver.create_learning(context, request_spec)
            stdout, stderr = self.driver.get_job_result(
                context,
                job_id,
                request_spec['template_id'],
                request_spec['cluster_id'])

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Learning %s failed on creation.",
                          learning_id)
                self.db.learning_update(
                    context, learning_id,
                    {'status': constants.STATUS_ERROR}
                )

        self._update_status(context, 'Learning', learning_id,
                            job_id, stdout, stderr)

    def create_online_learning(self, context, request_spec=None):
        """Create a Online Learning."""
        context = context.elevated()
        port = self.configuration.port_for_online_prediction
        request_spec['port'] = port

        LOG.debug("Create learning with request: %s", request_spec)

        try:
            stdout, stderr = self.driver.create_online_learning(context,
                                                                request_spec)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Learning %s failed on creation.",
                          request_spec['id'])
                self.db.learning_update(
                    context, request_spec['id'],
                    {'status': constants.STATUS_ERROR}
                )

        self._update_status(context, 'Learning', request_spec['id'],
                            None, stdout, stderr)

    def delete_learning(self, context, cluster_id=None, job_id=None, id=None):
        """Deletes a Learning."""
        context = context.elevated()

        try:
            self.driver.delete_learning(context, cluster_id, job_id, id)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Learning %s failed on deletion.", id)
                self.db.learning_update(
                    context, id,
                    {'status': constants.STATUS_ERROR_DELETING}
                )

        self.db.learning_delete(context, id)
        LOG.info("Learning %s deleted successfully.", id)


def main():
    texto = open("texto.txt").read()
    alfabeto = {"A": 0, "B": 0, "C": 0,
                "D": 0, "E": 0, "F": 0,
                "G": 0, "H": 0, "I": 0,
                "J": 0, "K": 0, "L": 0,
                "M": 0, "N": 0, "O": 0,
                "P": 0, "P": 0, "R": 0,
                "S": 0, "T": 0, "U": 0,
                "V": 0, "W": 0, "X": 0,
                "Y": 0, "Z": 0 }

    for alfa in alfabeto:
        count = 0
        for beta in texto:
            if(alfa == beta):
                count += 1
        alfabeto[alfa] = count
    decripta(texto, alfabeto)

def busca(alfabeto):
    moda = "A"
    for l in alfabeto:
        if(alfabeto[l] > alfabeto[moda]):
            moda = l
    return moda

def decripta(texto, alfabeto):
    n = 0
    string = ""

    frequencia = "AEOSRINDMUTCLPVGHQBFZJXKWY"
    m = busca(alfabeto)

    for i in frequencia:
        f = open("out/out"+str(n)+".txt", "w")
        k = ord(m)-ord(i)
        for j in texto:
            L = ord(j)-k
            if(L < 65):
                L+=26
            string+=(chr(L))
        print("\n"+string)
        f.write(string)

        cmd = input("\nDeseja continuar?(S/n)\n>>> ")

        if(cmd == "n" or cmd == "N"):
            return 0
        n+=1
        string = ""
        f.close()
main()


from django.shortcuts import get_object_or_404

from rest_framework import generics
from rest_framework.response import Response

from strings.models import String
from strings.serializers import StringSerializer, StringWithOptionsSerializer

class Strings(generics.ListAPIView):
	serializer_class = StringSerializer

	def get_queryset(self):
		return String.objects.filter(owner=self.request.user)

class NewStrings(generics.CreateAPIView):
    serializer_class = StringSerializer
    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

class StringDetails(generics.RetrieveUpdateDestroyAPIView):
	serializer_class = StringWithOptionsSerializer

	def get_object(self):
		return get_object_or_404(String, owner=self.request.user, id=self.kwargs['key'])

class GaugeChoices(generics.GenericAPIView):
    def get(self, request, *args, **kwargs):
        gauge_choices = String.GAUGE_CHOICES
        return Response(gauge_choices)


#BA model
import networkx as nx
import random
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import os
import logbin as lb
###############################################################################
# Will only print out information if N is below this number, use for testing
Nprint=20
# set seed to a number if you want to fix the random number seed for testing
# Otherwise set seed = None if you want true random numbers
#seed=0
seed = None
# Random numbers, not used here but in general of use
# see http://docs.python.org/2/library/random.html
# I often use random.randrange, random.sample, and random.random
if seed is None:
    random.seed()
    print(' Random number seed not fixed for proper run')
else:
    random.seed(seed)
    print (' Random number seed fixed for testing')
###############################################################################
#functions
# algin the eqautions in latex
def initG(N,edges,m_o,k,nodes):
    # initialise graph
    a=nx.Graph()
    # add nodes
    for i in range(N):
        a.add_node(i)
        nodes.append(i)
    # Now add edges
    # avoid self-edges
    for s in range(m_o+1):
        #print(s)
        for t in range(s,N):
                a.add_edge(s,t)
                #shw(a)
                edges.append([s,t])
                k[s] += 1
                edges.append([t,s])
                k[t] += 1
    return a
#plot fn
def shw(G):
    plt.figure()
    nx.draw_circular(G)
    plt.show()
#adding edge with preferential attatchment
def addedgerand(Graph, edges,nod,k,nodes):
    # want to add one end of edge to new vertex
    # then add other edge to an existing vertex preferentially
    #now insert edges
    ch = random.choice(nodes) # need to make this a list of current nodes at this time, not all total nodes
    k[ch] += 2
    edges.append([ch,ch])
    edges.append([ch,ch])
def addedgepref(Graph, edges,nod,k,nodes):
    # want to add one end of edge to new vertex
    # then add other edge to an existing vertex preferentially
    #now insert edges
    ch = random.choice(edges) # need to make this a list of current nodes at this time, not all total nodes
    v = random.choice(ch)
    #filter out self-loops
    #print(ch)
    # aviod self edges
    if v != nod:

        edges.append([nod,v])
        edges.append([v,nod])
        k[nod] += 1
        k[v] += 1
    elif v == nod:
        while v == nod:
            ch = random.choice(edges)
            v = random.choice(ch)
        k[nod] += 1
        k[v] += 1
        edges.append([nod,v])
        edges.append([v,nod])
# need parameters
#N total number of inital vertices
N = 3
#initialise graph with two connected nodes
#plt.figure()
#nx.draw_circular(G)
#plt.show()
#edge that has been added shouldnt be added again
n_add = 100000 #add this many nodes
#add nodes to initial graph
#degree of each nodes
nodestotal = [x for x in range(N+n_add+1)]
def addnewnodes(N,n_add,G,m,edges,k,nodes):
    for i in range(N,n_add+N+1):
        G.add_node(i)
        nodes.append(i)
        # Now add edges
        # avoid self-edges
        for h in range(int(m/2)):
            addedgepref(G,edges,i,k,nodes)
            #shw(G)
        for g in range(int(m/2)):
            addedgerand(G,edges,i,k,nodes)
def logbinn(k,scale,N,n_add,m):
    #need to do checks for this
    #need to log bin degrees
    #theory
    #plt.figure()
    #plt.rcParams.update({'font.size': 32})
    #plt.scatter(np.log(kt),np.log(pt), color = 'r', label = 'Theory, ' + str(m))
    #numerical
    #form a list of nodes - this is the number of nodes plus the number you add
    nodes = [x for x in range(N+n_add+1)]
    #subtract mink
    knew = np.array(k) - np.array(len(k)*[min(k)])
    #print(min(k),m)
    #print(edges)
    p = lb.logbin(knew,scale=scale)
    #p2 = lb.logbin(knew,scale=scale)
    #add back kmin to k values
    kf1 = np.array(p[0]) + np.array(len(p[0])*[min(k)])
    #kf2 = np.array(p2[0]) + np.array(len(p2[0])*[min(k)])
    return p,kf1 #return the k distribution
def theoryval(m,kk):
    ptt = []
    for i in kk:
        a = ((9*m) + 6)*((9*m) + 4)*((9*m) + 2)*(9*m)
        b = (i+(4*m)+4)*(i+(4*m)+3)*(i+(4*m)+2)*(i+(4*m)+1)*(i+(4*m))
        ptt.append(a/(4*b))
    pt = ptt
    return pt # returns theory prob for each k
def chisqttest(m,p2,kf): # mention why you did or didnt use the tests
    #for the statistical comparison
    # compare numerical data to theory
    # numerical only gives a fixed number of data points for certian k
    # plug this value of k into the theory and then use the kai squared test to compare answers.
    t = theoryval(m,kf)[:10]
    n = p2[1][:10]
    statistic, pval = stats.chisquare( n, t)
    print('stat = ',statistic,", for m = ",m)
    print('p-value = ',pval)
    critval = stats.chi2.ppf(q=0.95,df=len(kf)-1) # list of data set is df, check rwsidue qq plot- tell you if it as gaussian or not buuilt in func.
    print("crit val = ",critval)
    #plt.figure()
    #shift back logbin data
    #plt.scatter(np.log(kf),np.log(p2[1]), label = 'Numerical ' + str(m))
    #plt.scatter(np.log(kf[:15]),np.log(t), label = 'Theory ' + str(m))
    #plt.legend()
#list of m for same N
# need to do repeats and append before log binning!
repeats = 5
for x in [4,32]:#,8,16,32,64]:
    kreap = []
    for i in range(repeats):
        edges = []
        nodes = []
        k = len(nodestotal)*[0] # initial list of nodes with zero degree
        G = initG(N,edges,2,k,nodes) # create initial graph
        addnewnodes(N,n_add,G,x,edges,k,nodes) # add all nodes
        kreap.extend(k)
    pt = []
    plt.rcParams.update({'font.size': 32})
    #plt.scatter(np.log(kreap),np.log(pt),marker = '.', color = 'black')
    #p,p2,kf1,kf2 = logbinn(kreap,1.35,N,n_add,x) # outputs log binned data with scaling.
    #plt.scatter(np.log(kf1),np.log(p[1]),s = 10, label = 'No scaling logbin, m ='+str(x))
    #plt.scatter(np.log(kf2),np.log(p2[1]),s=150, label = 'Log binned, m = '+str(x) )
    p,kf = logbinn(kreap,1.35,N,n_add,x) # outputs log binned data with scaling.
    pt = theoryval(x,kf)
    #p = lb.logbin(k,scale=1.35)
    print(len(p[0]),len(p[1]))
    plt.scatter(np.log(kf),np.log(pt), color = 'r', label = 'Theory, ' + str(x))
    plt.scatter(np.log(kf),np.log(p[1]),s = 350,marker = '.', label = 'm ='+str(x))
    plt.legend()
    print('done ' + str(x))
    #plt.show()
    chisqttest(x,p,kf)
plt.rcParams.update({'font.size': 32})
plt.xlabel('ln($k$)')
plt.ylabel('ln($p_{\infty}(k)$)')
plt.show()
# do repeats for each m value and append each repeat then log bin.
# should help to get finite sized stuff


#!/usr/bin/python3
from xml.dom import minidom

htmldoc = minidom.parse('cut_riddles_file6.html')
body = htmldoc.getElementsByTagName('body')[0]
spans_all = body.getElementsByTagName('span')
print (spans_all)

# Open a file
test = open("test.txt", "w",)
print ("Name of the file: ", test.name)

to_the_file = []
for lop in spans_all:
    try:
        spans = lop.firstChild.data
    except AttributeError:
        continue
    to_the_file.append(spans)

print (to_the_file)
test.write(to_the_file)
# Close opend file
test.close()
print (to_the_file)


import turtle

frank = turtle.Turtle()

def square(t):
	for i in range(0,4):
		t.fd(100)
		t.lt(90)

square(frank)
turtle.mainloop()

alice = turtle.Turtle()
square(alice)
turtle.mainloop()


#!/usr/bin/env python
# -*- coding:utf-8 -*-

from rest_framework import status
from rest_framework.parsers import MultiPartParser
from rest_framework.views import APIView
from rest_framework.response import Response
import backend.common.video_handler as handler
import datetime
import logging
import uuid
import os
from valley.settings import UPLOAD_ROOT

logger = logging.getLogger("valley")

class UploadFileView(APIView):
    parser_classes = (MultiPartParser,)

    def post(self, request, format=None):
        files = request.FILES.getlist("attachment", None)
        result = {}
        files_address = []
        try:
            fileDir = os.path.join(UPLOAD_ROOT, 'attachments', str(uuid.uuid4()))
            if (not os.path.exists(fileDir)) and len(files) != 0:  # 如果路径不存在
                os.makedirs(fileDir)
            try:
                for file in files:
                    file_absolute_path = os.path.join(fileDir, file.name).replace("\"", "")
                    file_handler = open(file_absolute_path, 'wb')  # 打开特定的文件进行二进制的写操作
                    try:
                        for chunk in file.chunks():  # 分块写入文件
                            file_handler.write(chunk)
                    finally:
                        file_handler.close()
                    files_address.append(file_absolute_path)
            except Exception as e:
                logger.error(e)
            result['msg'] = '查询成功'
            result['success'] = True
            result['data'] = files_address
            return Response(result, status.HTTP_200_OK)
        except Exception as e:
            logger.error(e)
            result['msg'] = '查询失败 %s' % e
            result['success'] = False
            return Response(result, status.HTTP_500_INTERNAL_SERVER_ERROR)

class UploadVideoView(APIView):
    parser_classes = (MultiPartParser,)

    def post(self, request, format=None):
        files = request.FILES.getlist("video", None)
        result = {}
        files_address = []
        try:
            subdir = os.path.join('videos', str(uuid.uuid4()))
            fileDir = os.path.join(UPLOAD_ROOT, subdir)
            if (not os.path.exists(fileDir)) and len(files) != 0:  # 如果路径不存在
                os.makedirs(fileDir)
            try:
                for file in files:
                    ext = os.path.splitext(file.name)[1]
                    name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
                    file_relative_path = os.path.join(subdir, name + ext)
                    file_absolute_path = os.path.join(fileDir, name + ext).replace("\"", "")
                    file_handler = open(file_absolute_path, 'wb')  # 打开特定的文件进行二进制的写操作
                    try:
                        for chunk in file.chunks():  # 分块写入文件
                            file_handler.write(chunk)
                    finally:
                        file_handler.close()
                    try:
                        out_path = os.path.join(fileDir, 'out.m3u8').replace("\"", "")
                        cover_path = os.path.join(fileDir, 'cover.jpeg').replace("\"", "")
                        thumb_path = os.path.join(fileDir, 'thumb.jpeg').replace("\"", "")
                        handler.create_m3u8(file_absolute_path, out_path, cover_path, thumb_path)
                        file_relative_path = os.path.join(subdir, 'out.m3u8')
                    except Exception as ex:
                        logger.error(ex)
                    files_address.append(file_relative_path)
            except Exception as e:
                logger.error(e)
            result['msg'] = '上传成功'
            result['success'] = True
            result['data'] = files_address
            return Response(result, status.HTTP_200_OK)
        except Exception as e:
            logger.error(e)
            result['msg'] = '上传失败 %s' % e
            result['success'] = False
            return Response(result, status.HTTP_500_INTERNAL_SERVER_ERROR)


from django.conf.urls import include, url

from django.contrib import admin
admin.autodiscover()

import root.views

# Examples:
# url(r'^$', 'export_metrics_jira.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

urlpatterns = [
    url(r'^$', root.views.index, name='index'),
    url(r'^cost_of_sprint$', root.views.cost_detail, name='cost_detail'),
    url(r'^api/', include('root.urls')),
    url(r'^admin/', include(admin.site.urls)),
]


from __future__ import print_function
import random
# We'll start with a couple of globals...
CardTitlePrefix = "Life Quotes"
# --------------- Helpers that build all of the responses ----------------------
def build_speechlet_response(title, output, reprompt_text, should_end_session):
    """
    Build a speechlet JSON representation of the title, output text,
    reprompt text & end of session
    """
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Simple',
            'title': CardTitlePrefix + " - " + title,
            'content': output
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
    }
def build_response(session_attributes, speechlet_response):
    """
    Build the full response JSON from the speechlet response
    """
    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }
# --------------- Functions that control the skill's behavior ------------------
def get_welcome_response():
    session_attributes = {}
    card_title = "hello world"
    speech_output = "Hello! You can ask me to start quotes"
    # If the user either does not reply to the welcome message or says something
    # that is not understood, they will be prompted again with this text.
    reprompt_text = "I'm sorry - I didn't understand. You should ask me to start quotes..."
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(card_title, speech_output, reprompt_text, should_end_session))

def handle_session_end_request():
    card_title = "Session Ended"
    speech_output = "Have a nice day! "
    # Setting this to true ends the session and exits the skill.
    should_end_session = True
    return build_response({}, build_speechlet_response(
        card_title, speech_output, None, should_end_session))

def quotes():
	"""
	Return a suitable greeting...
	"""
	card_title = "Quotes"
	quotes=["Success is not final, failure is not fatal, It is the courage to continue that counts.","It is better to fail in originality than to succeed in imitation", "The road to success and the road to failure are almost exactly the same.", "Opportunities don't happen, You create them.", "Do one thing every day that scares you"]

	speech_output=random.choice(quotes)
	speech_output+=". Do you want me to read another quote? If yes, ask me for ""Next quote"""
	should_end_session=False
	return build_response({}, build_speechlet_response(card_title, speech_output, "Ask me to start quotes...", should_end_session))

# --------------- Events ------------------
def on_session_started(session_started_request, session):
    """ Called when the session starts """
    print("on_session_started requestId=" + session_started_request['requestId']
          + ", sessionId=" + session['sessionId'])
def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they want """
    print("on_launch requestId=" + launch_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # Dispatch to your skill's launch
    return get_welcome_response()
def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """
    print("on_intent requestId=" + intent_request['requestId'] +
			", sessionId=" + session['sessionId'])
    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']
    # Dispatch to your skill's intent handlers
    if intent_name == "WelcomeIntent":
        return get_welcome_response()
    elif intent_name == "QuotesIntent":
        return quotes()
    elif intent_name == "AMAZON.NextIntent" or intent_name == "AMAZON.MoreIntent":
        return quotes()
    elif intent_name == "AMAZON.HelpIntent":
        return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
        return handle_session_end_request()
    else:
        raise ValueError("Invalid intent")
def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session. Is not called when the skill returns should_end_session=true """
    print("on_session_ended requestId=" + session_ended_request['requestId'] +
          ", sessionId=" + session['sessionId'])
# --------------- Main handler ------------------
def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    # print("event.session.application.applicationId=" +
          # event['session']['application']['applicationId'])
    #return json.dumps(event)
    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId']},
                           event['session'])
    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])


import argparse
import os
import sys

from piprot.piprot import Piprot

def entrypoint():
    cli_parser = argparse.ArgumentParser(
        epilog="Here's hoping your requirements are nice and fresh!"
    )

    cli_parser.add_argument(
        "-d",
        "--delay",
        type=int,
        default=5,
        help="Delay before an outdated package triggers an error. (in days, defaults to 5).",
    )

    if os.path.isfile("requirements.txt"):
        nargs = "*"
        default = ["requirements.txt"]
    else:
        nargs = "+"
        default = None

    cli_parser.add_argument(
        "files", nargs=nargs, type=str, default=default, help="requirements file(s)"
    )

    cli_args = cli_parser.parse_args()
    piprot = Piprot(req_files=cli_args.files, delay_in_days=cli_args.delay)
    sys.exit(piprot.main())

if __name__ == "__main__":
    entrypoint()


import pickle

data = {}

while True:
    input1 = input("Key name? ")
    input2 = input("Key data? ")
    if input1 == "exit" or input2 == "exit":
        break
    data[input1] = input2

print(data)

with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)


import cube
from astropy.utils import data
from spectral_cube import SpectralCube, Projection
from radio_beam import Beam
from astropy import units as u
import time
import numpy as np
import matplotlib.pyplot as plt
import math
from astropy.io import fits
from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
from astropy.nddata import Cutout2D
from regions import read_ds9

from matplotlib import rcParams
rcParams['mathtext.default']='regular'

############################################################
# basic information

position=SkyCoord(dec=50.4167*u.arcmin,ra=204.9706*u.degree,frame='icrs')

############################################################
# function

def fits_import(fitsimage, item=0):
    hdr = fits.open(fitsimage)[item].header
    wcs = WCS(hdr).celestial
    data=fits.open(fitsimage)[item].data
    data=np.squeeze(data)
    data_masked=np.ma.masked_invalid(data)

    return wcs, data_masked

def cut_2d(data,position,size,wcs):
    cut=Cutout2D(data=data,position=position,size=size,wcs=wcs)
    data_cut=cut.data
    wcs_cut=cut.wcs

    return wcs_cut, data_cut

############################################################
# main program

Dir='/1/home/heh15/workingspace/Arp240/NGC5257/12CO10/'
imageDir=Dir+'casa5.4/'
picDir=Dir+'picture/'
regionDir=Dir+'region/'

name=imageDir+'NGC5257_12CO10_combine_contsub_pbcor.fits'
imagecube=SpectralCube.read(name)
# imcube=imagecube.with_spectral_unit(u.km/u.s,velocity_convention='radio',rest_value=112.73*10**9*u.Hz)
common_beam = imagecube.beams.common_beam(tolerance=1e-5)
Imcube = imagecube.convolve_to(common_beam)

## create rms cube
rmscube=cube.calc_noise_in_cube(Imcube, spatial_average_nbeam=1.0, spectral_average_nchan=2)

# # mask the the low value of rmscube.
# mask=rmscube<3.0e-3*u.Jy/u.beam
# lowrms=rmscube.with_mask(~mask)
# newrms=lowrms.with_fill_value(3.0e-3)

## find the signal of the cube.
outcube=cube.find_signal_in_cube(Imcube,rmscube,snr_hi=4,nchan_hi=1,nchan_lo=2)
outcube.write(imageDir+'NGC5257_12CO10_pbcor_cube.fits',overwrite=True)
# kcube=outcube.to(u.K)
# kcube.write(imageDir+'NGC5257_12CO10_kcube.fits')

fitsfile=imageDir+'NGC5257_12CO10_pbcor_cube.fits'
outcube=SpectralCube.read(fitsfile)

### moment 0 map ###
imcube=outcube.with_spectral_unit(u.km/u.s,velocity_convention='radio',rest_value=112.73*10**9*u.Hz)
moment0=imcube.moment(order=0)
moment0.write(imageDir+'NGC5257_12CO10_pbcor_cube_mom0.fits',overwrite=True)
wcs=WCS(moment0.hdu.header)
mom0_data=moment0.hdu.data

size=u.Quantity((54,42),u.arcsec)
wcs_cut=cut_2d(mom0_data,position,size,wcs)[0]
mom0_cut=cut_2d(mom0_data,position,size,wcs)[1]

### moment 1 map ###
imcube=outcube.with_spectral_unit(u.km/u.s,velocity_convention='radio',rest_value=112.73*10**9*u.Hz)
moment1=imcube.moment(order=1)
moment1.write(imageDir+'NGC5257_12CO10_pbcor_cube_mom1.fits',overwrite=True)

wcs=WCS(moment1.hdu.header)
mom1_data=moment1.hdu.data

size=u.Quantity((54,42),u.arcsec)
wcs_cut=cut_2d(mom1_data,position,size,wcs)[0]
mom1_cut=cut_2d(mom1_data,position,size,wcs)[1]

fig=plt.figure()
ax=plt.subplot('111',projection=wcs_cut)
im=plt.imshow(mom0_cut,origin='lower',cmap='rainbow')
cbar=plt.colorbar(im)
cbar.set_label('km/s')

# region=read_ds9(regionDir+'anomaly.reg')[0]
# region_pix=region.to_pixel(wcs_cut)
# region_pix.plot(color='black')
# plt.savefig(picDir+'NGC5257_12CO10_mom1.png')

# name=imageDir+'NGC5257_12CO10_combine_contsub_pbcor.fits'
# imagecube=SpectralCube.read(name)
# test=np.array(outcube)
# np.nansum(test[:,160,160])*10

# imagecube[:,160,160].quicklook()
# outcube[:,160,160].quicklook()

# print(moment0[160,160])

# name=imageDir+'NGC5257_12CO10_combine_contsub_pbcor_mom0.fits'
# mom0=fits_import(name)[1]
# print(mom0[160,160])


import numpy as np
from scipy.sparse import lil_matrix

def get_bigramms(word):
    """
    Функция, составляющая по слову множество биграмм его букв.
    """
    n_gramms = set()
    n_gramms.add(" " + word[0])
    n_gramms.add(word[-1] + " ")
    for i in range(0, len(word) - 2):
        n_gramms.add(word[i:i + 2])
    return n_gramms

class CountVectorizer:
    """
    Класс, реализующий преобразование строк в векторы с помощью подхода Bag of Words.
    :param analyzer: 'word' - мешок слов, 'char_bigrams' - мешок буквенных биграмм
    """
    def __init__(self, analyzer='word'):
        """
        """
        self.columns = []
        self.n_dimensions = 0
        self.analyzer = analyzer

    def fit(self, texts):
        """
        Функция, находящая все уникальные слова или буквенные n-граммы в данных текстах.
        """
        col = set()
        for text in texts:
            for word in text.split():
                if self.analyzer == 'word':
                    if word not in col:
                        col.add(word)
                elif self.analyzer == 'char_bigrams':
                    bigramms = get_bigramms(word)
                    col = col | bigramms
        self.n_dimensions = len(col)
        self.columns = list(col)

    def fit_transform(self, texts):
        self.fit(texts)
        return self.transform(texts)

    def transform(self, texts):
        """
        Функция, преобразующая список строк в массив numpy.array
        """
        shape = (len(texts), self.n_dimensions)
        matrix = lil_matrix(shape)
        for row_index, text in enumerate(texts):
            enterings = {k: 0 for k in self.columns}

            for key in enterings:
                if self.analyzer == 'word':
                    enterings[key] = text.count(key)
                elif self.analyzer == 'char_bigrams':
                    bigramms = []
                    for word in text.split():
                        bigramms.extend(get_bigramms(word))
                    enterings[key] = bigramms.count(key)

            for column_index, col in enumerate(self.columns):
                matrix[row_index, column_index] = enterings[col]
        return matrix

    def __setstate__(self, d):
        self.columns = d['columns']
        self.n_dimensions = d['n_dim']
        self.analyzer = d['analyzer']

    def __getstate__(self):
        return {'columns': self.columns, 'n_dim': self.n_dimensions, 'analyzer': self.analyzer}


import tkinter as tk
from tkinter import filedialog
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
import pandas as pd
import seaborn as sns
import sys

Nrange = 128
rIdx = 1024
Ntime = 300

debug = False
debug2 = False

class Window:
    def __init__(self, ts, f, bw, ws, fn, val):
        self.ts = ts
        self.f = f
        self.bw = bw
        self.ws = ws
        self.fn = fn
        self.val = val

class BlockIndex:
    def __init__(self, tID, rID):

        self.timeID = tID
        self.rangeID = rID
        self.idxArry  = []

    def add_win(self, win, idx):
        if ((win.f >= self.rangeID*Nrange & win.f < (self.rangeID+1)*Nrange) | (win.f <= self.rangeID*Nrange & win.f + win.bw >= self.rangeID*Nrange)) & \
            ((win.fn >= self.timeID * Ntime & win.fn < (self.timeID+1)*Ntime) | (win.fn > (self.timeID+1)*Ntime & win.fn - win.ts <= (self.timeID+1)*Ntime)):

            self.idxArry.append(idx)
        else:
            print('This window should not be here. Block ID: {},{} ({},{}). Window start: {} end: {} at frame: {} ts: {}'.format(self.timeID, self.rangeID, self.timeID*Ntime, self.rangeID*Nrange, win.f, win.f + win.bw, win.fn, win.ts))

    def size(self):
        return len(self.idxArry)

    def entries(self):
        return self.idxArry

class Edge:
    def __init__(self, u, v):
        self.u = u
        self.v = v

    def connect(self):
        temp = [u,v]
        return temp

    def print(self):
        print('{} - {}'.format(u,v))

def main():
    #ws='whitespace'
    #ts='timescale'
    #f='frequency'
    #bw='bandwidth'
    #fn='frame_no'

    matplotlib.style.use('ggplot')

    root = tk.Tk()
    root.withdraw()
    filename = filedialog.askopenfilename() #('Window_dump.csv')

    #graph(filename)
    legacy(filename)

def graph ( file ):

    VertexList = [] #V - list of vertices with the index of the vertex (window) list equal to the index of the window
    EdgeList = [] #E -

    BlockList = [] #B - psuedo 2d array of blocks. ... this actually could work well with a GPU architecture ................

    wins = pd.read_csv(file, header=0) #, chunksize = chnk)
    print(wins.columns)

    ######row[1]     row[2]    row[3]     row[4]     row[5]
    #cols:timescale,frequency,bandwidth,whitespace,frame_no
    cols = wins.columns

    t_thresh = 10
    b_thresh = 66

    candidate = 0
    total = 0
    counter = 0

    blockid = 0

    Nrows = 0
    Ncols = 0

    for row in wins.itertuples():
        total+=1
        winval = (row[1]- t_thresh)/t_thresh * (row[3] / b_thresh)
        if winval > 1 :

            temp = Window(int(row[1]),int(row[2]),int(row[3]),int(row[4]),int(row[5]),winval);

            VertexList.append(temp)

            #generate blocks (if necessary)
            if int(row[5]/Ntime) + 1 > int(blockid/rIdx):
                for x in range(0,rIdx):
                    BlockList.append(BlockIndex(int(blockid/rIdx),blockid%rIdx))
                    blockid += 1
                print('Blockid = {}'.format(blockid))

            #need to generate this list for ALL blocks covered, not just the first fit.
            #BlockList[int(row[5]/Ntime)*rIdx + int(row[2]/Nrange)].add_win(temp,candidate)

            for t in range(int((row[5] - row[1])/Ntime),int(row[5]/Ntime)+1):
                for b in range(int(row[2]/Nrange),int((row[2] + row[3])/Nrange)+1):

                    BlockList[t*rIdx + b].add_win(temp,candidate)

                    if debug: print('added candidate: {},{} to: {},{}'.format(row[5],row[2],t*rIdx,b))
            #generate edges

            edgeGen(VertexList,EdgeList,BlockList,temp,candidate)

            candidate += 1
        if total%1000 == 0:
            print('Row: {}, with {} windows'.format(total,candidate))
            print('Size of V: {}. Size of E: {}'.format(len(VertexList),len(EdgeList)))

    print('Total windows: {} Candiate windows: {}'.format(total,candidate))
    print('Final Size of V: {}. Size of E: {}'.format(len(VertexList),len(EdgeList)))

def edgeGen(V,E,B,win,id):
        #Nrows = row[3]/Nrange
        #Ncols = row[1]/Ntime
        #for t in range(row[5]/Ntime - Ncols,row[5]/Ntime):
        #    for b in range(row[2]/Nrange - Nrows,row[2]/Ntime):
        edgeCheck = []

        Nrows = int(win.ts/Ntime)
        Ncols = int(win.bw/Nrange)
        if debug2: print('Active block: {},{} with time {} and range {} - window details: f{}, bw{}, fn{}, ts{}'.format(int(win.fn/Ntime - Nrows),int(win.f/Nrange),Nrows,Ncols,win.f, win.bw, win.fn, win.ts))
        for t in range(int((win.fn - win.ts)/Ntime),int(win.fn/Ntime)+1):
            for b in range(int(win.f/Nrange),int((win.f + win.bw)/Nrange)+1):

                if debug2: print('Checking: {},{}'.format(t,b))
                edgeCheck += B[t*rIdx+b].entries()

        #print(edgeCheck)
        reducedEdge = np.unique(edgeCheck)
        if debug: print('id {}, possible conflict set: {}'.format(id, reducedEdge))

        for e in reducedEdge:
            unique = True
            test = V[e]
            if debug2: print('Current edge check id: {}'.format(e))
            if ((e != id) and ((test.f >= win.f and test.f < win.f+win.bw) or (test.f <= win.f and test.f + test.bw - 1 >= win.f)) and ((
                test.fn >= win.fn - win.ts + 1 and test.fn < win.fn) or (test.fn > win.fn and test.fn - test.ts < win.fn))):

                edge = Edge(id,e)
                E.append(edge)
                edge.print

                if debug: print('window id {}, ({},{} bw {} ts {}) overlapped by: id {}, ({},{} bw {} ts {})'.format(id, V[id].f, V[id].fn, V[id].bw, V[id].ts, e, V[e].f, V[e].fn, V[e].bw, V[e].ts,))

def ts_vs_ws ( file ):

    wins = pd.read_csv(file, header=0) #, chunksize = chnk)
    print(wins.columns)

    #cols:timescale,frequency,bandwidth,whitespace,frame_no
    cols = wins.columns

    ts = max(wins[cols[0]])
    fn = max(wins[cols[4]])

    ts_arr = np.arange(1,ts+1)
    ws_arr = np.zeros_like(ts_arr)

    totalws = 0

    #row:index,timescale,frequency,bandwidth,whitespace,frame_no
    for row in wins.itertuples():
        ws_arr[row[1]-1] += row[4]
        totalws += row[4]

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)

    ax.semilogy(ts_arr,ws_arr, label='Whitespace vs TS')

    ax.set_yscale('symlog') #super important to plot '0' values
    plt.text(0.2,0.8,"Total ws: " + "{:,}".format(totalws), horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
    ax.legend()
    plt.show()

def legacy (filename):

    wins = pd.read_csv(filename, header=0) #, chunksize = chnk)
    print(wins.columns)

    #cols:timescale,frequency,bandwidth,whitespace,frame_no
    cols = wins.columns

    #print(wins.head(10))
    #ts = wins.sort_values([cols[0],cols[2]])

    #uniq = pd.unique(wins[cols[0]].values.ravel())#, columns = [cols[0],cols[3]])
    #ts_sort = np.c_[uniq,np.zeros(uniq.size)]
    #ts_sum = np.empty_like(ts_sort)

    ##Row[0] is the index given by the dataframe
    ##Here the unique TS is used as the index, where the unique whitespace value is summed for each TS
    ##TS is already sorted ascending, by nature of the detection
    ## THIS WILL ONLY WORK IF EVERY TS IS OBSERVED !!!BAD!!!
    #for row in wins.itertuples():
    #    ts_sort[row[1]-1,1] += row[4]

    sns.jointplot(wins[cols[2]],wins[cols[0]],kind="hex",color="#4CB391", stat_func=None)
    sns.jointplot(x='bandwidth',y='timescale',data = wins,kind="kde",color="#4CB391", stat_func=None)
    plt.show()

    x = max(wins[cols[2]])
    y = max(wins[cols[0]])
    fn = max(wins[cols[4]])

    nbins = 131072

    print('Max Bandwidth: {}  Max Timescale: {}'.format(x,y))

    mesh = np.zeros((x,y))
    num_frames = np.zeros(fn)

    freqdensity = np.zeros(nbins)

    #print(mesh)

    uniq = pd.unique(wins[cols[2]].values.ravel())
    uniq.sort()

    ws_sort = np.c_[uniq,np.zeros(uniq.size)]
    ws_sum = np.empty_like(ws_sort)

    count = 0
    totalws = 0
    #Row[0] is the index given by the dataframe
    for row in wins.itertuples():
        mesh[row[3]-1,row[1]-1] += row[4]
        #temp = np.where(ws_sort[:,0]==row[3])
        #ws_sort[temp,1] += row[4]
        totalws += row[4]
    ##     mesh[row[1]-1,row[3]-1] += row[4]

        #populate frequency density array
        #for i in range(row[2],row[2]+row[3]-1):
        #    freqdensity[i]+=row[1]

        count += 1
        if (count%100000 == 0): print('Up to: {}'.format(count))

    print('Total Whitespace: ')
    print(totalws)

    fig = plt.figure()
    ##ax1 = fig.add_subplot(1,2,1) #convention (row,col,idx)
    ##ax2 = fig.add_subplot(1,2,2)

    ##pd.DataFrame.hist(wins,column=cols[0],bins=(x+1),log=True,ax=ax1)
    ##ax1.set_xlim([0,50])
    ##ax1.set_title('Timescale Density')
    ##ax1.set_xlabel('Timescale')
    ##ax1.set_ylabel('# Observations')

    ##ax2.plot(num_frames)
    ##ax2.set_ylim([0,100])
    ##ax2.set_title('Whitespace Channels')
    ##ax2.set_xlabel('Frame Number')
    ##ax2.set_ylabel('# Whitespace Channels')
    ##plt.show()

    #print('Mesh populated')
    #print(mesh)

    ax = fig.add_subplot(1,1,1)
    plt.pcolormesh(mesh, norm=LogNorm(vmin=1, vmax=np.amax(mesh)))
    plt.colorbar()
    #ax.set_yscale('log')
    #ax.set_xscale('log')
    ax.set_xlim(1,x)
    ax.set_ylim(1,y)
    ax.set_title('Whitespace Density')
    ax.set_xlabel('Bandwidth in Bins (190Hz/bin)')
    ax.set_ylabel('Timescale (5.3ms/unit)')

    #ax1 = fig.add_subplot(2,1,1)
    #ax1.plot(freqdensity)
    ##ax1.set_xlim([0,50])
    #ax1.set_title('Whitespace Density per Bin')
    #ax1.set_xlabel('Frequency in Bins (190Hz/bin)')
    #ax1.set_ylabel('Whitespace')

    plt.show()

    #ts_sum = ts_sort
    # np.copyto(ts_sum,ts_sort)
    np.copyto(ws_sum,ws_sort)

    #df = pd.DataFrame(data=ts_sort,columns=[cols[0],cols[3]])
    #print(df)

    #for i in range(uniq.size-1, 0, -1):
    #    ts_sum[i-1,1] += ts_sum[i,1]
        #print(ts_sum[i,1])
    for i in range(uniq.size-1, 0, -1):
        ws_sum[i-1,1] += ws_sum[i,1]

    print('\nws_sort:')
    print(ws_sort)
    print('\nws_sum:')
    print(ws_sum)

    sns.jointplot(ws_sort[:,0],ws_sort[:,1],kind="hex",color="#4CB391")
    plt.show()

    #for i in range(len(wins)):
     #       set1 = np.array(wins[wins.columns[0::2]])[i]

    #wins.plot(x=cols[0],y=cols[3],kind='scatter')
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    #ax.plot(ts_sort)
    #ax.set_yscale('log')
    #plt.semilogy(ts_sort[0], ts_sort[1], ts_sum[0], ts_sum[1])
    ax.semilogy(ws_sort[:,0], ws_sort[:,1], label='Unique Whitespace')
    ax.semilogy(ws_sum[:,0], ws_sum[:,1], label='Cumulative Whitespace')

    ax.set_yscale('symlog') #super important to plot '0' values
    ax.legend()
    plt.show()

if __name__ == "__main__":
    sys.exit(int(main() or 0))


from collections import Counter

def choose(n, k):
  k = min(k, n-k)
  result = 1
  for i in xrange(k):
    result *= n - i
    result /= i + 1
  return result

def factorial(n):
  result = 1
  for i in xrange(2, n+1):
    result *= i
  return result

def isFactorial(n):
  runningFactorial = 1
  next             = 2
  while runningFactorial < n:
    runningFactorial *= next
    next             += 1
  return runningFactorial == n

def inverseFactorial(n):
  runningFactorial = 1
  next             = 1
  while runningFactorial < n:
    next             += 1
    runningFactorial *= next
  if runningFactorial != n:
    raise ValueError('The argument is not a valid factorial.')
  return next

def numPermutations(iterable):
  length      = len(iterable)
  frequencies = Counter(iterable)
  result      = factorial(length)
  for element in frequencies:
    result /= factorial(frequencies[element])
  return result

def numIntegerPermutations(iterable):
  # Catch empty iterable
  if len(iterable) == 0:
    return 0
  # In case a string is passed in, and not an int list
  intIterable = map(int, iterable)
  # If all permutations are integers, return that count
  if 0 not in intIterable:
    return numPermutations(intIterable)
  # Check for the singleton digit 0
  if len(intIterable) == 1:
    return 1
  # General case
  length          = len(intIterable)
  frequencies     = Counter(intIterable)
  frequencies[0] -= 1
  length         -= 1
  permutationsWithZeroFirst = factorial(length)
  for element in frequencies:
    permutationsWithZeroFirst /= factorial(frequencies[element])
  return numPermutations(iterable) - permutationsWithZeroFirst


from itertools import product

def prime_sieve(N):
  """
  Compute all prime to N
  initial condition: N > 1
  O(n log log n)
  """
  primes = [True]*(N+1)
  primes[0] = primes[1] = False

  for i in range(2, int(N**0.5)+1):
    if primes[i]:
      for j in range(i*i, N+1, i):
        primes[j] = False

  return primes

def divisor(num):
  """
  Return lowest divisor of number
  """
  for x in range(2,int(num**0.5)+1):
    if num%x == 0:
      return x
  return num

def jamcoin(digits):
  for binary in product(('0','1'),repeat=digits-2):
    coin = ''.join(('1',) + binary + ('1',))

    if not any(divisor(int(coin,base))==int(coin,base) for base in range(2,11)):
      print('Coin found: {}'.format(coin))
      yield coin

fh = open('output_large.txt','w')

T = int(input().strip())
for i in range(T):
  N, J = [int(x) for x in input().strip().split()]

  print('Case #{}:'.format(i+1),file=fh)

  coins = 0

  # Find Jam Coin
  for i in jamcoin(N):
    coins += 1

    # Find divisors
    div = []
    for base in range(2,11):
      div.append(divisor(int(i,base)))

    print('{} {}'.format(i,' '.join(map(str,div))),file=fh)
    if coins >= J: break


import doit
from doit import create_after
import glob
from numpy import int32
import os
import pandas as pd
import config
import subprocess
import shlex
from io import StringIO
import time
import json

# def task_scan_serialnumbers():
#     def get_serialnumbers(dependencies, targets):
#         mountpoints = pd.DataFrame(json.loads(subprocess.getoutput('lsblk -J -o  NAME,SIZE,FSTYPE,TYPE,MOUNTPOINT'))['blockdevices'])
#         mountpoints = mountpoints[~mountpoints.children.isna()]
#         mountpoints =pd.DataFrame(mountpoints.children.apply(lambda x: x[0]).to_list())[['name','mountpoint']]
#         paths = pd.DataFrame(subprocess.getoutput('udevadm info -q path -n $(ls /dev/s*1)').splitlines(),columns=['Path'])
#         paths[['host','dev']]=paths.Path.str.extract(r'(?P<host>host\d+).*block\/(?P<dev>([^\\]+$))')[['host','dev']]
#         paths['name'] =paths.dev.str.split('/',expand=True)[1]
#         mountpoints =mountpoints.merge(paths, on='name', how='inner')
#         mountpoints['CardUUID']=mountpoints.mountpoint.str.extract(r'(?P<CardUUID>[0-9A-F]{4}-[0-9A-F]{4})')
#         mountpoints = mountpoints[~mountpoints.CardUUID.isna()]
#         os.makedirs(os.path.dirname(targets[0]),exist_ok=True)
#         path = f'{os.path.dirname(targets[0])}/{os.path.splitext(os.path.basename(targets[0]))[0].split("_")[1]}'
#         mountpoints['Destination'] = path
#         mountpoints.to_csv(targets[0],index=False)

#     t = time.localtime()
#     target = f"{config.geturl('imagehead')}/backup_{time.strftime('%Y%m%dT%H%M%S', t)}_source.csv"
#     return {
#         'actions':[get_serialnumbers],
#         'targets':[target],
#         'uptodate':[False],
#         'clean':True,
#     }

# @create_after(executed='scan_serialnumbers', target_regex='*.csv')
def task_rsync_cards():
    def rsync_cards(dependencies, targets):
        cards = pd.read_csv(dependencies[0])
        cards = cards.groupby('mountpoint').first()
        hosts = cards.host.unique()
        cards['RsyncDone']= False
        processes = set()
        max_processes = 4
        workon = True
        hosts = cards.host.unique()
        for host in hosts:
            item =cards[cards['host']==host].iloc[0]
            if os.path.exists(item.name):
                os.makedirs(item.Destination,  exist_ok=True)
                command = shlex.split(f"rsync -a -W  --exclude '*.LRV' --exclude '*.THM' --progress  {item.name} {item.Destination}")
                processes.add(subprocess.Popen(command))#,startupinfo=
                if len(processes) >= max_processes:
                    os.wait()
                processes.difference_update([p for p in processes if p.poll() is not None])
        for host in hosts:
            if len(cards[cards['host']==host])==2: #check to see if there were two cards on the host
                item =cards[cards['host']==host].iloc[-1]
                if os.path.exists(item.name):
                    os.makedirs(item.Destination, exist_ok=True)
                    command = shlex.split(f"rsync -a -W  --exclude '*.LRV' --exclude '*.THM' --progress  {item.name} {item.Destination}")
                    processes.add(subprocess.Popen(command))
                    if len(processes) >= max_processes:
                        os.wait()
                    processes.difference_update([p for p in processes if p.poll() is not None])
        for p in processes:
            if p.poll() is None:
                p.wait()
        cards['RsyncDone']= cards.Destination.apply(os.path.exists)
        cards.to_csv(targets[0])
    file_dep = glob.glob(f"{config.geturl('imagehead')}/backup_*_source.csv")
    for file in file_dep:
        target = file.replace('.csv','_cardsread.csv')
        yield {
            'name':file,
            'file_dep':[file],
            'actions':[rsync_cards],
            'targets':[target],
            'uptodate':[False],
            'clean':True,
        }

@create_after(executed='rsync_cards', target_regex='*.csv')
def task_sync_backup():
    file_dep = glob.glob(f"{config.geturl('imagehead')}/*_cardsread.csv")
    t = time.localtime()
    target = f"{config.geturl('imagehead')}/log_{time.strftime('%Y%m%dT%H%M%S', t)}.log"
    return {
        'file_dep':file_dep,
        'actions':[f"rsync -av --progress {config.geturl('imagehead')}/  {config.geturl('backuphead')} > {target}"],
        'targets':[target],
        'uptodate':[True],
        'clean':True,
    }

@create_after(executed='sync_backup', target_regex='*.csv')
def task_clean_cards():
    def rsync_cards(dependencies, targets):
        cards = pd.read_csv(dependencies[0])
        cards['Backup'] =cards.Destination.str.replace(config.geturl('imagehead'),config.geturl('backuphead'))
        cards = cards.groupby('mountpoint').first()
        hosts = cards.host.unique()
        cards['CleanDone']= False
        processes = set()
        max_processes = 4
        workon = True
        hosts = cards.host.unique()
        for host in hosts:
            item =cards[cards['host']==host].iloc[0]
            if os.path.exists(item.name):
                os.makedirs(item.Destination,  exist_ok=True)
                command = shlex.split(f"rsync -a -W  --exclude '*.LRV' --exclude '*.THM' --progress --remove-source-files {item.name} {item.Backup}")
                processes.add(subprocess.Popen(command))#,startupinfo=
                if len(processes) >= max_processes:
                    os.wait()
                processes.difference_update([p for p in processes if p.poll() is not None])
        for host in hosts:
            if len(cards[cards['host']==host])==2: #check to see if there were two cards on the host
                item =cards[cards['host']==host].iloc[-1]
                if os.path.exists(item.name):
                    os.makedirs(item.Destination, exist_ok=True)
                    command = shlex.split(f"rsync -a -W  --exclude '*.LRV' --exclude '*.THM' --progress --remove-source-files {item.name} {item.Backup}")
                    processes.add(subprocess.Popen(command))
                    if len(processes) >= max_processes:
                        os.wait()
                    processes.difference_update([p for p in processes if p.poll() is not None])
        for p in processes:
            if p.poll() is None:
                p.wait()
        cards['CleanDone']= True
        cards.to_csv(targets[0])
    file_dep = glob.glob(f"{config.geturl('imagehead')}/*_cardsread.csv")
    for file in file_dep:
        target = file.replace('.csv','_z`cleaned.csv')
        yield {
            'name':file,
            'file_dep':[file],
            'actions':[rsync_cards],
            'targets':[target],
            'uptodate':[False],
            'clean':True,
        }

    # def sync_dbackup()
    #         os.makedirs(item.Destination,  exist_ok=True)
    #         command = shlex.split(f"rsync -a -W  --exclude '*.LRV' --exclude '*.THM' --progress  {item.name} {item.Destination}")
    #         processes.add(subprocess.Popen(command))#,startupinfo=
# def task_scan_devs():
#     result = subprocess.getoutput('lsblk -J -o  NAME,SIZE,FSTYPE,TYPE,MOUNTPOINT')

# def task_process_cameras():
#     def process_cameras(dependencies, targets):
#         backupsessions = pd.read_csv(config.geturl('backupsessions'),index_col='TimeStamp',parse_dates=['TimeStamp']).sort_values('TimeStamp')
#         barnumbers = pd.read_csv(config.geturl('barnumbers'))
#         cameras = []
#         for item in dependencies:
#             filter = os.path.join(item,config.cfg['paths']['videowild'])
#             files = glob.glob(filter)
#             if files:
#                 command = f"exiftool -api largefilesupport=1 -u  -json -ext MP4 -q -CameraSerialNumber -CreateDate -SourceFile -Duration -FileSize -FieldOfView {item}"
#                 result =subprocess.getoutput(command)
#                 try:
#                     df =pd.read_json(result)
#                     cameras.append(df)
#                 except:
#                     print('No json')
#         cameras = pd.concat(cameras)
#         cameras['CreateDate'] = pd.to_datetime(cameras['CreateDate'],format='%Y:%m:%d %H:%M:%S')
#         cameras =cameras.merge(barnumbers, on='CameraSerialNumber', how='inner')
#         path = f"{config.geturl('cardstore')}/{backupsessions.iloc[-1].name:%Y%m%dT%H%M%S}"
#         os.makedirs(path,exist_ok=True)
#         cameras['Destination']=cameras.apply(lambda x: f"{path}/{x.CameraNumber}_{x.CameraSerialNumber}_{x.CreateDate:%Y%m%dT%H%M}",axis=1)
#         cameras.to_csv(targets[0],index=False)

#     gopro = glob.glob('/media/*/*/DCIM/100GOPRO')
#     backupsessions = pd.read_csv(config.geturl('backupsessions'),index_col='TimeStamp',parse_dates=['TimeStamp']).sort_values('TimeStamp')
#     target = f"{config.geturl('cardstore')}/backup_{backupsessions.iloc[-1].name:%Y%m%dT%H%M%S}.csv"
#     return {
#         'file_dep':gopro,
#         'actions':[get_serialnumbers],
#         'targets':[target],
#         'uptodate':[True],
#         'clean':True,
#     }

if __name__ == '__main__':
    import doit
    DOIT_CONFIG = {'check_file_uptodate': 'timestamp'}
    #print(globals())
    doit.run(globals())


"""
PONG! Demo - bounce sound using Esplora buzzer
"""
add_library('serial')
from Esplora_Processing import Esplora

xBall = 50
yBall = 10
sizeBall = 6
xMove = 1
yMove = 1
paddle = 0
widthPaddle = 30
heightPaddle = 6

def setup():
    size(300,200) # set size of window
    Esplora.open(Serial)

def updatePaddle(colorPaddle):
    global paddle
    # Update paddle position
    paddle = map(Esplora.readSlider(), 1023, 0, 0, width - widthPaddle)
    fill(colorPaddle)
    rect(paddle, height - heightPaddle, widthPaddle, heightPaddle)

def updateBall(colorBall):
    global xBall, yBall, xMove, yMove
    # Update ball position
    xBall = xBall + xMove
    yBall = yBall + yMove
    fill(colorBall)
    rect(xBall, yBall, sizeBall, sizeBall)
    # Bounce ball
    if xBall > width - sizeBall or xBall < 0:
        xMove = -xMove # Bounce off walls
        Esplora.writeBuzzer(1000, 10)
    if yBall < 0:
        yMove = -yMove # Bounce off top
        Esplora.writeBuzzer(1000, 10)
    if yBall > height - heightPaddle - sizeBall:
        # Check if ball hit paddle or game over
        if xBall + sizeBall >= paddle and xBall <= paddle + widthPaddle:
            yMove = -yMove # Bounce off paddle
            Esplora.writeBuzzer(2000, 10)
        else:
            Esplora.writeBuzzer(200, 500)
            exit() # Missed ball - game over

def draw():
    background(0)
    updatePaddle(128)
    updateBall(255)


"""For an input measure generator and measurement_function algorithm
this script will:
1) compare the output from measure to truth
2) return performance metrics

Performance metrics computed separately for detection, segmentation, flux,
redshift

"""
import numpy as np
import btk.config
import btk.get_input_catalog
import btk.create_blend_generator
import btk.create_observing_generator
import btk.measure
import btk.draw_blends
from scipy import spatial

class Metrics_params(object):
    def make_meas_generator(self, catalog_name, max_number=2,
                            batch_size=1, seed=999):
        """
        Creates the default btk.meas_generator for input catalog
        Overwrite this function for user defined measurement generator
        """
        # Load parameters
        param = btk.config.Simulation_params(
            catalog_name, max_number=max_number, batch_size=batch_size,
            seed=seed)
        np.random.seed(param.seed)
        # Load input catalog
        catalog = btk.get_input_catalog.load_catlog(param)
        # Generate catalogs of blended objects
        blend_generator = btk.create_blend_generator.generate(
            param, catalog)
        # Generates observing conditions
        observing_generator = btk.create_observing_generator.generate(
            param)
        # Generate images of blends in all the observing bands
        draw_blend_generator = btk.draw_blends.generate(
            param, blend_generator, observing_generator)
        meas_params = btk.measure.Measurement_params()
        meas_generator = btk.measure.generate(
            meas_params, draw_blend_generator, param)
        self.meas_generator = meas_generator

    def get_detections(self, index):

        return [], []

    def get_segmentation(self, data=None, index=None):
        return None

    def get_flux(self, data=None, index=None):
        return None

    def get_redshift(self, data=None, index=None):
        return None

def evaluate_detection(detected_centers, true_centers,
                       distance_upper_bound=4):
    """
    Compares the true centers and detected centers to identify the
    number of true detections, number of sources that were undetected
    and number of spurious detections.
    Args:
        true_catalog: Catalog with true parameters of galaxies in blend.
        detected_centers: Centroids from detection algorithm ([N, (x, y)])
        distance_upper_bound: Match objects within this distance (in pixels)
                              (Default:10)
    Returns:
    """
    if len(detected_centers) == 0:
        # no detection
        return 0, len(true_centers), 0
    z_tree = spatial.KDTree(true_centers)
    detected_centers = np.array(detected_centers).reshape(-1, 2)
    match = z_tree.query(detected_centers,
                         distance_upper_bound=distance_upper_bound)
    fin, = np.where(match[0] != np.inf)  # match exists
    inf, = np.where(match[0] == np.inf)  # no match within distance_upper_bound
    detected = len(np.unique(match[1][fin]))
    undetected = len(np.setdiff1d(range(len(true_centers)), match[1][fin]))
    spurious = len(match[1][inf])
    return detected, undetected, spurious

def evaluate_segmentation(segmentation, data=None, index=None):
    if segmentation is None:
        return None
    return None

def evaluate_flux(flux, data=None, index=None):
    if flux is None:
        return None
    return None

def evaluate_redshift(redshift, data=None, index=None):
    if redshift is None:
        return None
    return None

def run(Metrics_params, test_size=1000):
    results = {'detection': [], 'segmentation': [],
               'flux': [], 'redshift': []}
    for i in range(test_size):
        # Evaluate detection algorithm
        detected_centers, true_centers = Metrics_params.get_detections(
            index=i)
        results['detection'].append(evaluate_detection(
            detected_centers, true_centers))
        # Evaluate segmentation algorithm
        segmentation = Metrics_params.get_segmentation(index=i)
        results['segmentation'].append(evaluate_segmentation(
            segmentation))
        # Evaluate flux measurement algorithm
        flux = Metrics_params.get_flux(
            index=i)
        results['flux'].append(evaluate_flux(
            flux))
        # Evaluate redshift estimation algorithm
        redshift = Metrics_params.get_redshift(
            index=i)
        results['redshift'].append(evaluate_redshift(
            redshift))
    return results


# -*- coding: utf-8 -*-

from odoo import api, fields, models
from datetime import datetime

class AssetReportWizard(models.TransientModel):
    """Asset report wizard."""

    _name = 'asset.report.wizard'
    _description = 'Asset Report Wizard'
    # _inherit = "account.asset.asset"

    company_id = fields.Many2one(
        comodel_name='res.company',
        default=lambda self: self.env.user.company_id,
        required=False,
        string='Company'
    )
    date_at = fields.Date(string="Date", default=lambda self:datetime.today())
    asset_ids = fields.Many2many("account.asset.asset", "zeguierhuig", string="Asset")

    @api.multi
    def button_export_pdf(self):
        print("1")
        self.ensure_one()
        report_type = 'qweb-pdf'

        actif_ids = self.env['account.asset.asset'].search([('date','<=',self.date_at)])

        self.asset_ids = [(6, 0, [a.id for a in actif_ids])]

        return self._export(report_type)
    @api.multi
    def button_export_xlsx(self):
        self.ensure_one()
        report_type = 'xlsx'
        return self._export(report_type)

    def _export(self, report_type):
        print("2")
        print(self.asset_ids)
        """Default export is PDF."""
        # model = self.env['asset.report.wizard']
        # report = model.create(self._prepare_vat_report())
        return self.print_report(report_type)

    @api.multi
    def print_report(self, report_type='qweb'):
        print("4")
        print("DATE ===== ",self.date_at)
        self.ensure_one()
        report_name = 'print_actifs.project_journal_items_id'
        # report_name = 'print_actifs.project_journal_items_id'
        context = dict(self.env.context)
        action = self.env['ir.actions.report'].search(
            [('report_name', '=', report_name),
             ('report_type', '=', report_type)], limit=1)
        print(context)
        context.update({
            'test': "test2",
        })
        print(context)
        print("---------")
        return action.with_context(context).report_action(self)

    def _prepare_vat_report(self):
        print("3")
        self.ensure_one()
        return {
            'company_id': self.company_id.id,
            'date_at': self.date_at,
        }


import requests
from bs4 import BeautifulSoup
from RankingView import *
#定义函数获取并检测页面信息
def Text(url):
     # 爬虫请求头信息
    headers = {
         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
    }
    try:
        respone = requests.get(url,headers=headers,timeout=30)
        respone.raise_for_status()
        #解决中文字符编码问题
        respone.encoding = respone.apparent_encoding
        return respone.text
    except:
        return ""
def bang(url):
    html = Text(url)
    #使用BeautifulSoup解析页面信息
    soup = BeautifulSoup(html, "html.parser")
    name,zhishu,xingxi = [],[],[]
    #开始获取作品名称信息
    allname = soup.find_all('p',class_ = 'm-title')
    for x in allname:
        name.append(x.string) #使用列表方式 添加信息
    #获取作品昨日指数信息
    allzhishu = soup.find_all("li",class_='m-item-playcount')
    for y in allzhishu:
        zhishu.append(y.span.string)
    #使用中文字符的空格填充
#     print("{0}\t{1}\t{2:^40}".format("排名","作品名称","昨日指数",chr(12288)))
    #用range函数获取前50个数据
    for i in range(50):
#         print("{0}\t{1}\t{2:^40}".format(i+1, name[i], zhishu[i],chr(12288)))
#         xingxi.append([i+1, name[i], zhishu[i]])
        xingxi.append([name[i], zhishu[i]])
    return xingxi
def hot_search(mode='电视剧'):
    url=['https://www.360kan.com/rank/dianshi','https://www.360kan.com/rank/dianying']
    if mode=='电视剧':
        k=0
    elif mode=='电影':
        k=1
    else:
        return [['未找到!','','']]
    list = bang(url[k])
    if k==0:
        dainshi_bang(list)
        reli2(list)
    if k==1:
        dainying_bang(list)
        reli(list)
    return list
def cloudGen():
    lst=[]
    lst.append(hot_search('电视剧'))
    lst.append(hot_search('电影'))
    word_cloud(lst)
# cloudGen()
# t=hot_search('电影')
# print(t)


import Utils.globals, Utils.keyUtils.keys
from threading import Thread
from PyQt5 import QtCore, QtGui, QtWidgets
from UI.pyQt5_GUI.mainwindow import Ui_MainWindow
import sys
from Manager import core_manager

# To generate the gui python code form the .ui file, use this command:
# pyuic5 UI/pyQt5_GUI/mainwindow.ui  -o UI/pyQt5_GUI/mainwindow.py

# Used to update the UI when receving incoming message
class UI_updater(Thread):

    def __init__(self,ui):
        Thread.__init__(self)
        self.ui = ui
        self.receivingQueue = Utils.globals.receivingQueue

    def run(self):
        i = 0
        while True:
            message = self.receivingQueue.get()
            cmd = str(i) + "- Message: " + str(message["command"])
            self.ui.listWidget.addItem(cmd)
            i += 1

        print("Exit pyQt5_GUI Thread")

class Ui_manager:

    def __init__(self):
        self.receivingQueue = Utils.globals.receivingQueue
        self.sendingQueue = Utils.globals.sendingQueue

        self.app = QtWidgets.QApplication(sys.argv)
        self.MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()

        # Start UI
        self.ui.setupUi(self.MainWindow)

        # Init components
        self.init_components()

        UI_updater(self.ui).start()

        # CLose UI
        self.MainWindow.show()
        sys.exit(self.app.exec_())

    # define the actions when event triggered by the UI
    def init_components(self):

        # onClick on elt from listWidget, display content in the plainTextEdit
        self.ui.listWidget.itemClicked.connect(lambda: self.onClick_listWidget(self.ui.listWidget.currentItem()) )

        # onClick on Version button, send Version to node
        self.ui.pushButton.toggle()
        self.ui.pushButton.clicked.connect(lambda: self.onClick_version())

        # onClick on Version button, send Verack to node
        self.ui.pushButton_2.toggle()
        self.ui.pushButton_2.clicked.connect(lambda: self.onClick_verack())

        # onClick on Version button, send Ping to node
        self.ui.pushButton_3.toggle()
        self.ui.pushButton_3.clicked.connect(lambda: self.onClick_ping())

        # ...

        #... keyUtils tab

        self.ui.createAddressButton.toggle()
        self.ui.createAddressButton.clicked.connect(lambda: self.onClick_createAddress())

        # ...

    def onClick_listWidget(self, item):
        id = int(item.text().split('-')[0])
        self.ui.plainTextEdit.clear()
        self.ui.plainTextEdit.setPlainText(Utils.globals.node_messages[id]["header"] + "\n" + Utils.globals.node_messages[id]["payload"])

    def onClick_version(self):
        version = core_manager.get_version_pkt()
        self.sendingQueue.put(version)

    def onClick_verack(self):
        verack = core_manager.get_verack_pkt()
        self.sendingQueue.put(verack)

    def onClick_ping(self):
        ping = core_manager.get_ping_pkt()
        self.sendingQueue.put(ping)

    def onClick_createAddress(self):
        self.ui.KeysDisplay.clear()
        private_key = self.ui.privateKeyInsert.text()
        if (len(private_key)<1):
            print ("here")
            nk = Utils.keyUtils.keys.Key()
        else:
            print ("there")
            nk = Utils.keyUtils.keys.Key(private_key)
        self.ui.KeysDisplay.setPlainText(str(
              "Private key: " + nk.printable_pk) + "\n"
             + "Public key: " + str(nk.public_key, "ascii") + "\n"
             + "Hashes public key: " + str(nk.hashed_public_key, "ascii") + "\n"
             + "Address: " + str(nk.addr))


import os
import pandas as pd
from features.feature_extractor import Features
#%%
Fs = 300.0
waveform_path = 'E:\\学习资料\\大三上\\信号处理\\bsp_project\\ML\\data\\waveforms'
feature_path = 'E:\\学习资料\\大三上\\信号处理\\bsp_project\\ML\\data\\features'
#%%
filename_list = os.listdir(waveform_path)
dict_list = [{'filename': filename_list[i][2:8], 'label': filename_list[i][0]} for i in range(0, len(filename_list))]
labels = pd.DataFrame.from_dict(dict_list)
#%%
ecg_features = Features(file_path=waveform_path, fs=Fs, feature_groups=['full_waveform_features'], labels=labels)
ecg_features.extract_features(n_signals=None, show=True, normalize=True,
                              polarity_check=True, template_before=0.25, template_after=0.4)
#%%
features = pd.concat([labels, ecg_features.features], axis=1)
features.to_csv(os.path.join(feature_path, 'features.csv'), index=False)


import transaction
import zeit.cms.interfaces
import zeit.edit.interfaces
import zeit.newsletter.testing
import zope.component

class EditorTest(zeit.newsletter.testing.BrowserTestCase):

    def setUp(self):
        from zeit.newsletter.newsletter import Newsletter
        super(EditorTest, self).setUp()
        self.repository['newsletter'] = Newsletter()

    def test_newsletter_can_be_checked_out(self):
        self.browser.handleErrors = False
        self.browser.open(
            'http://localhost/++skin++vivi/repository/newsletter/@@checkout')

class BlockEditingTest(zeit.newsletter.testing.SeleniumTestCase):

    def setUp(self):
        super(BlockEditingTest, self).setUp()
        self.open('/repository/newsletter/@@checkout')
        # XXX without this, create_content_and_fill_clipboard raises a
        # ConflictError
        transaction.commit()

    def test_groups_should_be_addable(self):
        s = self.selenium
        s.assertElementNotPresent('css=.block.type-group')
        s.waitForElementPresent('link=*Add group')
        s.click('link=*Add group')
        s.waitForElementPresent('css=.block.type-group')

    def test_drag_content_to_group_zone_should_create_teaser_block(self):
        self.create_content_and_fill_clipboard()
        s = self.selenium

        s.waitForElementPresent('link=*Add group')
        s.click('link=*Add group')
        s.waitForElementPresent('css=.block.type-group')

        s.dragAndDropToObject(
            '//li[@uniqueid="Clip/c3"]', 'css=div.type-group .landing-zone',
            '10,10')
        s.waitForElementPresent('css=.block.type-teaser')
        s.assertText('css=.block.type-teaser', '*c3 teaser*')

    def test_drag_content_to_teaser_zone_should_append_teaser_block(self):
        self.create_content_and_fill_clipboard()
        self.test_groups_should_be_addable()
        s = self.selenium
        s.dragAndDropToObject(
            '//li[@uniqueid="Clip/c3"]', 'css=div.type-group .landing-zone',
            '10,10')
        s.waitForElementPresent('css=.block.type-teaser + .landing-zone')

        # before:
        # group
        #   Teaser (c3)
        s.dragAndDropToObject(
            '//li[@uniqueid="Clip/c1"]', 'css=div.type-teaser + .landing-zone',
            '10,10')
        # after:
        # group
        #   Teaser (c3)
        #   Teaser (c1)

        second_teaser = 'css=.type-teaser + * + .type-teaser'
        s.waitForElementPresent(second_teaser)
        s.assertText(second_teaser, '*c1 teaser*')

    def create_two_groups(self):
        s = self.selenium

        group1 = 'css=div.type-group'
        group2 = 'css=div.type-group + div.type-group'

        s.waitForElementPresent('link=*Add group')
        s.click('link=*Add group')
        s.waitForElementPresent(group1)
        s.click('link=*Add group')
        s.waitForElementPresent(group2)

        s.dragAndDropToObject(
            '//li[@uniqueid="Clip/c1"]', group1 + ' .landing-zone', '10,10')
        s.waitForElementPresent(group1 + ' .block.type-teaser')

        s.dragAndDropToObject(
            '//li[@uniqueid="Clip/c2"]', group2 + ' .landing-zone', '10,10')
        s.waitForElementPresent(group2 + ' .block.type-teaser')

    def test_groups_should_be_sortable_with_drag_and_drop(self):
        self.create_content_and_fill_clipboard()
        self.create_two_groups()
        s = self.selenium

        group1 = 'css=div.type-group'
        group2 = 'css=div.type-group + div.type-group'

        # before:
        # group
        #   Teaser (c1)
        # group
        #   Teaser (c2)
        s.dragAndDropToObject(
            group2 + ' .dragger', group1 + ' .dragger')
        # after:
        # group
        #   Teaser (c2)
        # group
        #   Teaser (c1)

        s.waitForText(group1 + ' .block.type-teaser', '*c2*')
        s.assertText(group2 + ' .block.type-teaser', '*c1*')

    def test_drag_teaser_between_groups_should_move_it(self):
        self.create_content_and_fill_clipboard()
        self.create_two_groups()
        s = self.selenium
        s.setWindowSize(self.window_width, 700)

        group1 = 'css=div.type-group'
        group2 = 'css=div.type-group + div.type-group'

        # before:
        # group
        #   Teaser (c1)
        # group
        #   Teaser (c2)
        s.dragAndDropToObject(
            group2 + ' .type-teaser .dragger', group1 + ' .dragger')
        # after:
        # group
        #   Teaser (c2)
        #   Teaser (c1)
        # group

        second_teaser = ' .type-teaser + .type-teaser'
        s.waitForElementPresent(group1 + second_teaser)
        s.assertText(group1 + ' .block.type-teaser', '*c2*')
        s.assertText(group1 + second_teaser, '*c1*')
        s.assertElementNotPresent(group2 + ' .block.type-teaser')

class AdvertisementTest(zeit.newsletter.testing.SeleniumTestCase):

    def setUp(self):
        from zeit.newsletter.newsletter import Newsletter
        from zeit.newsletter.category import NewsletterCategory
        super(AdvertisementTest, self).setUp()
        category = NewsletterCategory()
        category.ad_middle_title = u'Some ad'
        category.ad_middle_groups_above = 0
        category.ad_middle_image = zeit.cms.interfaces.ICMSContent(
            'http://xml.zeit.de/2006/DSC00109_2.JPG')
        category.ad_thisweeks_groups_above = 0
        self.repository['newsletter'] = category
        newsletter = Newsletter()
        ad_factory = zope.component.getAdapter(
            newsletter.body, zeit.edit.interfaces.IElementFactory,
            name='advertisement-middle')
        ad_factory()
        self.repository['newsletter']['one'] = newsletter
        transaction.commit()
        self.open('/repository/newsletter/one/@@checkout')

    def test_advertisement_is_wired_correctly(self):
        s = self.selenium
        s.waitForTextPresent('Some ad')
        s.waitForElementPresent('css=.newsletter-ad-image img')