import sys
sys.path.append('/UNITER')
import math
import statistics
from functools import partial

import numpy as np
import torch
import torch.nn.functional as F
from tqdm import tqdm

from utils_cma import batch, convert_results_to_pd

from model.nlvr2 import UniterForNlvr2Triplet

from utils.const import IMG_DIM

from apex import amp

np.random.seed(1)
torch.manual_seed(1)

class Model():
    '''
    Wrapper for all model logic
    '''
    def __init__(self,
                 ckpt_file,
                 model_config,
                 opts,
                 device='cuda',
                 output_attentions=False,
                 random_weights=False):
        super()
        self.device = device

        # Load UNITER finetuned for NLVR2 (triplet version)
        checkpoint = torch.load(ckpt_file)
        self.model = UniterForNlvr2Triplet(model_config, img_dim=IMG_DIM)
        self.model.init_type_embedding()
        self.model.load_state_dict(checkpoint, strict=False)
        self.model.to(self.device)
        self.model, _ = amp.initialize(self.model, enabled=opts.fp16, opt_level='O2')
        self.model.eval()

        # TODO: this does not work currently
        # if random_weights:
        #     print('Randomizing weights')
        #     self.model.init_weights()

        # Grab model configuration parameters
        self.num_layers = model_config.num_hidden_layers
        self.num_neurons = model_config.hidden_size

    def get_representations(self, context, position=0):
        # Hook for saving the representation
        def extract_representation_hook(module,
                                        input,
                                        output,
                                        position,
                                        representations,
                                        layer):
            representations[layer] = output[0][position]
        handles = []
        representation = {}
        with torch.no_grad():
            # construct all the hooks
            # word embeddings will be layer -1
            handles.append(self.model.uniter.embeddings.word_embeddings.register_forward_hook(
                    partial(extract_representation_hook,
                            position=position,
                            representations=representation,
                            layer=-1)))
            # hidden layers; intervening on the output of the feedforward network
            for layer in range(self.num_layers):
                handles.append(self.model.uniter.encoder.layer[layer]\
                                   .output.dropout.register_forward_hook(
                    partial(extract_representation_hook,
                            position=position,
                            representations=representation,
                            layer=layer)))

            logits = self.model(context, compute_loss=False)

            for h in handles:
                h.remove()

        return representation

    def get_probabilities_for_examples(self, context):
        """Return probabilities of single-token candidates given context"""

        logits = self.model(context, compute_loss = False)
        probs = F.softmax(logits, dim=-1)
        return probs.tolist()

    def neuron_intervention(self,
                            context,
                            outputs,
                            rep,
                            layers,
                            neurons,
                            position,
                            intervention_type='replace',
                            alpha=1.):
        # Hook for changing representation during forward pass
        def intervention_hook(module,
                              input,
                              output,
                              position,
                              neurons,
                              intervention,
                              intervention_type):
            # Get the neurons to intervene on
            neurons = torch.LongTensor(neurons).to(self.device)
            # First grab the position across batch
            # Then, for each element, get correct index w/ gather
            base = output[:, position, :].gather(
                1, neurons)
            intervention_view = intervention.view_as(base)

            if intervention_type == 'replace':
                base = intervention_view
            elif intervention_type == 'diff':
                base += intervention_view
            else:
                raise ValueError(f"Invalid intervention_type: {intervention_type}")
            # Overwrite values in the output
            # First define mask where to overwrite
            scatter_mask = torch.zeros_like(output).byte()
            for i, v in enumerate(neurons):
                scatter_mask[i, position, v] = 1
            # Then take values from base and scatter
            output.masked_scatter_(scatter_mask, base.flatten())

        # Set up the context as batch
        batch_size = len(neurons)
        batch_context = {}
        for key, value in context.items():
            try:
                if len(value.shape) == 2:
                    batch_context[key] = value.repeat(batch_size,1)
                elif len(value.shape) == 3:
                    batch_context[key] = value.repeat(batch_size, 1, 1)
            except AttributeError:
                continue

        handle_list = []
        for layer in set(layers):
          neuron_loc = np.where(np.array(layers) == layer)[0]
          n_list = []
          for n in neurons:
            unsorted_n_list = [n[i] for i in neuron_loc]
            n_list.append(list(np.sort(unsorted_n_list)))
          intervention_rep = alpha * rep[layer][n_list]
          if layer == -1:
              wte_intervention_handle = self.model.uniter.embeddings.word_embeddings.register_forward_hook(
                  partial(intervention_hook,
                          position=position,
                          neurons=n_list,
                          intervention=intervention_rep,
                          intervention_type=intervention_type))
              handle_list.append(wte_intervention_handle)
          else:
              mlp_intervention_handle = self.model.uniter.encoder.layer[layer]\
                                            .output.dropout.register_forward_hook(
                  partial(intervention_hook,
                          position=position,
                          neurons=n_list,
                          intervention=intervention_rep,
                          intervention_type=intervention_type))
              handle_list.append(mlp_intervention_handle)
        new_probabilities = self.get_probabilities_for_examples(
            batch_context)
        for hndle in handle_list:
          hndle.remove()
        return new_probabilities

    def neuron_intervention_single_experiment(self,
                                              intervention,
                                              intervention_type, layers_to_adj=[],
                                              neurons_to_adj=[],
                                              alpha=100,
                                              bsize=500, intervention_loc='all'):
        """
        run one full neuron intervention experiment
        """

        with torch.no_grad():

            # Compute representations of the non-negated and negated versions; position 0 corresponds to the [CLS] token
            orig_representations = self.get_representations(
                intervention[0],
                position=0)
            negated_representations = self.get_representations(
                intervention[1],
                position=0)

            # e.g. There aren't two dogs.
            if intervention_type == 'negate_direct':
                context = intervention[1]
                rep = orig_representations
                replace_or_diff = 'replace'
            # e.g. There are two dogs.
            elif intervention_type == 'negate_indirect':
                context = intervention[0]
                rep = negated_representations
                replace_or_diff = 'replace'
            else:
                raise ValueError(f"Invalid intervention_type: {intervention_type}")

            # Probabilities without intervention (Base case)
            # Candidate 1 is False; Candidate 2 is True
            # TODO: this can be simplified since there are only two candidates
            candidate1_orig_prob, candidate2_orig_prob = self.get_probabilities_for_examples(
                intervention[0])[0]
            candidate1_neg_prob, candidate2_neg_prob = self.get_probabilities_for_examples(
                intervention[1])[0]

            # Running interventions
            if intervention_loc == 'all':
              candidate1_probs = torch.zeros((self.num_layers + 1, self.num_neurons))
              candidate2_probs = torch.zeros((self.num_layers + 1, self.num_neurons))

              for layer in range(-1, self.num_layers):
                for neurons in batch(range(self.num_neurons), bsize):
                    neurons_to_search = [[i] + neurons_to_adj for i in neurons]
                    layers_to_search = [layer] + layers_to_adj

                    probs = self.neuron_intervention(
                        context=context,
                        outputs=[0,1],
                        rep=rep,
                        layers=layers_to_search,
                        neurons=neurons_to_search,
                        position=0,
                        intervention_type=replace_or_diff,
                        alpha=alpha)

                    for neuron, (p1, p2) in zip(neurons, probs):
                        candidate1_probs[layer + 1][neuron] = p1
                        candidate2_probs[layer + 1][neuron] = p2

            elif intervention_loc == 'layer':
              layers_to_search = (len(neurons_to_adj) + 1)*[layers_to_adj]
              candidate1_probs = torch.zeros((1, self.num_neurons))
              candidate2_probs = torch.zeros((1, self.num_neurons))

              for neurons in batch(range(self.num_neurons), bsize):
                neurons_to_search = [[i] + neurons_to_adj for i in neurons]

                probs = self.neuron_intervention(
                    context=context,
                    outputs=[0,1],
                    rep=rep,
                    layers=layers_to_search,
                    neurons=neurons_to_search,
                    position=0,
                    intervention_type=replace_or_diff,
                    alpha=alpha)
                for neuron, (p1, p2) in zip(neurons, probs):
                    candidate1_probs[0][neuron] = p1
                    candidate2_probs[0][neuron] = p2
            else:
              probs = self.neuron_intervention(
                        context=context,
                        outputs=[0,1],
                        rep=rep,
                        layers=layers_to_adj,
                        neurons=neurons_to_adj,
                        position=0,
                        intervention_type=replace_or_diff,
                        alpha=alpha)
              for neuron, (p1, p2) in zip(neurons_to_adj, probs):
                  candidate1_probs = p1
                  candidate2_probs = p2

        return (candidate1_orig_prob, candidate2_orig_prob,
                candidate1_neg_prob, candidate2_neg_prob,
                candidate1_probs, candidate2_probs)


class Train:
    def __init__(self,name,fare,seats):
        self.name=name
        self.fare=fare
        self.seats=seats

    def status(self):
        print(f"The Name of the Train is {self.name}")
        print(f"The Availabe seats of the Train is {self.seats}")

    def fareinfo(self):
        print(f"The Amount of the Ticket is Rs {self.fare}")

    def bookticket(self):
        if self.seats > 0:
            print(f"Your ticket has been Booked. Your ticket Number is {self.seats}\n")
            self.seats=self.seats - 1
        else:
            print("Sorry!,This Train is fulled!\n ")

    def cancelticket(self):
        pass

a=Train("indian Railway",80,10)
a.status()
a.fareinfo()
a.bookticket()
a.bookticket()
a.bookticket()
a.status()


from multics.globals import *

@system_privileged
def print_motd():
    homedir = parm()
    person  = parm()
    project = parm()
    acct    = parm()
    code    = parm()

    #== Open user's Person_id.motd file if it exists
    call.user_info_.homedir(homedir)
    call.user_info_.whoami(person, project, acct)
    user_motd_path = vfile_(homedir.val + ">" + person.id + ".motd")
    try:
        uf = open(user_motd_path)
        user_file_text = uf.read()
        uf.close()
    except:
        user_file_text = ""
    # end try

    #== Open system motd file if it exists
    try:
        f = open(vfile_(supervisor.fs.system_control_dir + ">motd"))
        file_text = f.read()
        f.close()
    except:
        file_text = ""
    # end try

    diff = _find_differences(file_text, user_file_text)
    call.ioa_(diff)

    #== Save the current system motd in the user's .motd file if it
    #== is different from the user's version (create it if necessary)
    try:
        if diff:
            uf = open(user_motd_path, "w")
            uf.write(file_text)
            uf.close()
        # end if
    except:
        pass

#-- end def print_motd

def _find_differences(file_text, user_file_text):
    sys_motd_lines = file_text.split("\n")
    user_motd_lines = user_file_text.split("\n")
    diff_lines = []
    while sys_motd_lines and user_motd_lines:
        sys_line = sys_motd_lines.pop(0)
        user_line = user_motd_lines.pop(0)
        if user_line != sys_line:
            diff_lines.append(sys_line)
        # end if
    # end while
    if sys_motd_lines:
        diff_lines.extend(sys_motd_lines)
    # end if
    return "\n".join(diff_lines)

#-- end def _find_differences

pmotd = print_motd


import sys
import heapq
input = sys.stdin.readline

MOD = 10 ** 9 + 7

def main():
    N = int(input())
    A = [0] * N
    B = [0] * N
    h = []

    for i in range(N):
        A, B = map(int, input().split())
        # deadline, effort
        heapq.heappush(h, (B, A))

    days = 0
    for i in range(N):
        work = heapq.heappop(h)
        days += work[1]
        # print(days, work)
        if days > work[0]:
            print('No')
            return

    print('Yes')

if __name__ == '__main__':
    main()


#!/usr/bin/python2.5
"""Create processed Pulse log files with story contents when run as a program.

The only public function is fetch_story_contents, which behaves like the
program.
"""

import sys, os, time, socket, html2text
from os import path
from utilities import DELIMITER, check_num_arguments, write_iterable, \
    write_2d_iterable
from process_data import TIMEOUT_LENGTH, MIN_STORY_LENGTH, STORIES_FILENAME, \
    READS_FILENAME, CLICKTHROUGHS_FILENAME, USER_IDS_FILENAME, \
    STORIES_DESCRIPTOR, READS_DESCRIPTOR, CLICKTHROUGHS_DESCRIPTOR, \
    NEW_STORIES_URL_INDEX, NEW_STORIES_TITLE_INDEX, EVENTS_STORY_ID_INDEX, \
    open_safely, report_time_elapsed, get_user_ids

NUM_ARGUMENTS = 2
"""The expected number of arguments to this module when executed as a script.
The path to this file is included in this count.
"""

PROGRAM_USAGE = "Usage: %s <input_directory>" % __file__
# A description of how to run execute this program from the command-line.

SUB_DIRECTORY_NAME = "Processed Data with Story Contents/"
# The name of the directory in which to place the output log files.

def _read_stories(input_file_path):
    """Return a list of stories with full contents and a dict with new IDs.

    Generate list elements in tuple form, where each element corresponds to a
    single line of the given processed stories log file.  Do not trim the
    newline off the end of the last element of the tuple.  Append a space
    followed by the full story contents to the title of each story.  Omit
    stories for which the full story contents could not be fetched.  Generate
    dict entries mapping from story IDs in the input file to story IDs in the
    output file.  Maintain the ordering of the input file in the list.  This
    ordering is equivalent to ascending order of both old story IDs and new
    story IDs.  Be warned that fetching full story contents is quite slow and
    consumes a great deal of bandwidth, so you or other users on your network
    may experience connectivity problems while executing this function.

    input_file_path, a str, is the file path to the processed Pulse stories log
    file that contains story URLs and titles but not the full contents of the
    stories themselves.
    """
    start_time = time.time()
    old_story_id = 0
    new_story_id = 0
    stories_list = []
    story_id_dict = {}
    story_contents_dict = {}
    socket.setdefaulttimeout(TIMEOUT_LENGTH)
    input_stream = open_safely(input_file_path)

    for story_as_str in input_stream:
        story_as_list = story_as_str.split(DELIMITER)
        story_url = story_as_list[NEW_STORIES_URL_INDEX]
        if story_url in story_contents_dict:
            story_contents = story_contents_dict[story_url]
        else:
            story_contents = html2text.extractFromURL(story_url)
            if (story_contents is not None) and \
                    (len(story_contents) <= MIN_STORY_LENGTH):
                story_contents = None
            story_contents_dict[story_url] = story_contents
        if story_contents is not None:
            story_as_list[NEW_STORIES_TITLE_INDEX] += " " + story_contents
            stories_list.append(tuple(story_as_list))
            story_id_dict[old_story_id] = new_story_id
            new_story_id += 1
        old_story_id += 1

    input_stream.close()
    num_stories_discarded = old_story_id - new_story_id
    discard_rate = float(100 * num_stories_discarded) / float(old_story_id)
    print(("Read a total of %d %s, %d (%.2f%%) of which were discarded " + \
           "because their full contents could not be fetched.") % \
           (old_story_id, STORIES_DESCRIPTOR, num_stories_discarded,
            discard_rate))
    report_time_elapsed(start_time)
    return (stories_list, story_id_dict)

def _read_events(story_id_dict, input_file_path, event_descriptor):
    """"Return a list of the events in the given file with the given story IDs.

    Generate list elements in [user_id, new_story_id, time_occurred] form, and
    maintain the ordering of the input file.

    inpurt_file_path, a str, is the file path to the Pulse event log file to
    read in.
    story_id_dict, a dict, maps from old story IDs to new story IDs.  Only
    events with story IDs that are keys in story_id_dict are retained in the
    output, but these old story IDs are replaced with the corresponding new
    story IDs.
    event_descriptor, a str, briefly describes the events in the plural form and
    is used to notify the user upon completion.
    """
    events_list = []
    num_events = 0
    num_events_kept = 0
    input_stream = open_safely(input_file_path)
    for event_as_str in input_stream:
        event_as_list = map(int, event_as_str[:-1].split(DELIMITER))
        old_user_id = event_as_list[EVENTS_STORY_ID_INDEX]
        if old_user_id in story_id_dict:
            event_as_list[EVENTS_STORY_ID_INDEX] = story_id_dict[old_user_id]
            events_list.append(event_as_list)
            num_events_kept += 1
        num_events += 1
    input_stream.close()
    num_events_discarded = num_events - num_events_kept
    discard_rate = float(100 * num_events_discarded) / float(num_events)
    print(("Read a total of %d %s, %d (%.2f%%) of which were discarded " + \
           "because the full contents of the associated story could not be " + \
           "fetched.") % (num_events, event_descriptor, num_events_discarded,
                          discard_rate))
    return events_list

def fetch_story_contents(input_directory):
    """Write processed Pulse log files with full story contents.

    Output files by the same name in a sub-directory of the given directory
    named SUB_DIRECTORY_NAME.  Append a space followed by the full story
    contents to the story titles.  Remove stories for which no content could not
    be fetched and events involving these stories.  Reassign story and user IDs
    to 0, 1, 2, etc. to fill the resulting gaps in the ID sequences.  Output a
    user IDs log file in which row numbers correspond to the new user IDs, and
    row values correspond to the old user IDs.

    input_directory, a str, is the file path to a directory containing processed
    Pulse log files lacking full story contents.
    """
    if not isinstance(input_directory, str):
        raise TypeError("Expected input_directory to be of type str.")

    if not path.isdir(input_directory):
        raise ValueError("Could not find given directory: %s" % input_directory)

    input_stories_path = path.join(input_directory, STORIES_FILENAME)
    stories_list, story_id_dict = _read_stories(input_stories_path)
    input_reads_path = path.join(input_directory, READS_FILENAME)
    reads_list = _read_events(story_id_dict, input_reads_path, READS_DESCRIPTOR)
    input_clickthroughs_path = path.join(input_directory,
                                         CLICKTHROUGHS_FILENAME)
    clickthroughs_list = _read_events(story_id_dict, input_clickthroughs_path,
                                      CLICKTHROUGHS_DESCRIPTOR)
    user_ids_list = get_user_ids(reads_list, clickthroughs_list)
    reads_list = [map(str, read) for read in reads_list]
    clickthroughs_list = [map(str, clickthrough) for clickthrough in \
                          clickthroughs_list]
    user_ids_list = map(str, user_ids_list)

    output_directory = path.join(input_directory, SUB_DIRECTORY_NAME)
    if not path.exists(output_directory):
        os.mkdir(output_directory)

    output_stories_path = path.join(output_directory, STORIES_FILENAME)
    write_2d_iterable(stories_list, output_stories_path, "")
    output_reads_path = path.join(output_directory, READS_FILENAME)
    write_2d_iterable(reads_list, output_reads_path)
    output_clickthroughs_path = path.join(output_directory,
                                          CLICKTHROUGHS_FILENAME)
    write_2d_iterable(clickthroughs_list, output_clickthroughs_path)
    output_users_path = path.join(output_directory, USER_IDS_FILENAME)
    write_iterable(user_ids_list, output_users_path)

if __name__ == "__main__":
    check_num_arguments(NUM_ARGUMENTS, PROGRAM_USAGE)
    fetch_story_contents(sys.argv[1])


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

import os
import pathlib
import sys

import csv
import glob
import openpyxl
import xlrd

""" import local liblarys """
from mylibs import filelist
from mylibs import excellibs

def main():
    argv = sys.argv
    argc = len(argv)

    if argc < 1:
        exit_msg(argv[0])

    if argc > 2:
        Path = argv[1]
    else:
        Path = "../"

    if argc > 3:
        Pattern = argv[2]
    else:
        Pattern = "*実装表*.xls*"

    if argc > 4:
        Exclude = argv[3]
    else:
        Exclude = ""

    if argc > 5:
        Depth = argv[4]
    else:
        Depth = "20"    # not use

    if Depth.isnumeric() == False:
        print("{0} is not numeric.".format(Depth))
        exit(1)

    """
    print("Path = {0}".format(Path))
    print("Pattern = {0}".format(Pattern))
    print("Exclude = {0}".format(Exclude))
    print("Depth = {0}".format(Depth))
    print("Hit Enter key...")
    input()
    """

    loopmain(Path, Pattern, Exclude, Depth)

def loopmain(Path, Pattern, Exclude, Depth):
    files = []
    for fileName in filelist.find_all_files(Path, Pattern, Exclude):
        files.append(fileName)

    # print(files)
    for file in files:
        print(file)

    for file in files:
        print(file)
        if file[-5:] == ".xlsx":
            T_WB = openpyxl.load_workbook(file)
            excellibs.readExcel_xlsx(T_WB)

        elif file[-4:] == ".xls":
            T_WB = xlrd.open_workbook(file)
            excellibs.readExcel_xls(T_WB)
        else:
            print("Error...")
            exit(-1)

def exit_msg(argv0):
    print("Usage: python {0} <Path> <Pattern> <Exclude> <Depth>".format(argv0))
    exit(0)

if __name__ == "__main__":
    main()


import torch
from torch.utils.data import TensorDataset
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np

def Create_Datasets(dataPath,vars,tstPer):
	data = pd.read_pickle(dataPath) #Loads pandas data stored in pickle object
	y = np.array(data.loc[:,'Category']).astype(int)

	# Creates train test split from the data
	xTrn,xTst,yTrn,yTst = train_test_split(
		data.iloc[:,data.columns != "Category"],
		y,
		test_size=tstPer,
		shuffle=True
		)

	#Convert to torch tensors
	xTrn_Torch = torch.Tensor(xTrn.loc[:,vars].to_numpy())
	xTst_Torch = torch.Tensor(xTst.loc[:,vars].to_numpy())
	yTrn_Torch = torch.Tensor(yTrn).type(torch.LongTensor)
	yTst_Torch = torch.Tensor(yTst).type(torch.LongTensor)

	# Creates a training dataset
	train_ds = torch.utils.data.TensorDataset(xTrn_Torch,yTrn_Torch)

	# Creates a testing dataset
	test_ds = torch.utils.data.TensorDataset(xTrn_Torch,yTrn_Torch)

	return train_ds,test_ds


import time

import numpy as np
from z3 import And, Or, sat, Sum, IntVector, Tactic, Implies, If

from utils import write_solution

class SMTsolver:

    def __init__(self, data, output_dir, timeout):
        self.data = data
        if output_dir == "":
            output_dir = "./smt/out/no_rot"
        self.output_dir = output_dir
        self.timeout = timeout

        self.circuits_num = None
        self.circuits = None
        self.max_width = None
        self.y_positions = None
        self.x_positions = None
        self.sol = None
        self.h = None
        self.w = None

    def solve(self):
        solutions = []
        for d in self.data:
            ins_num = d[0]
            solutions.append(self.solve_instance(d, ins_num))
        return solutions

    def solve_instance(self, instance, ins_num):
        _, self.max_width, self.circuits = instance
        self.circuits_num = len(self.circuits)
        self.w = IntVector('widths', self.circuits_num)
        self.h = IntVector('heights', self.circuits_num)

        for i in range(self.circuits_num):
            self.w[i], _ = self.circuits[i]
            _, self.h[i] = self.circuits[i]

        lower_bound = sum([self.h[i] * self.w[i] for i in range(self.circuits_num)]) // self.max_width
        upper_bound = sum(self.h) - min(self.h)

        for plate_height in range(lower_bound, upper_bound + 1):
            self.sol = Tactic('auflia').solver()
            self.sol.set(timeout=self.timeout * 1000)
            self.sol.set(threads=4)

            self.set_constraints(plate_height, self.w, self.h)

            solve_time = time.time()
            if self.sol.check() == sat:
                spent_time = time.time() - solve_time
                circuits_pos = self.evaluate()
                write_solution(self.output_dir, ins_num, ((self.max_width, plate_height), circuits_pos),
                               spent_time)
                return ins_num, ((self.max_width, plate_height), circuits_pos), spent_time
            else:
                try_timeout = round((self.timeout - (time.time() - solve_time)))
                if try_timeout <= 0:
                    write_solution(self.output_dir, ins_num, None, 0)
                    return ins_num, None, 0
        return ins_num, None, 0

    def set_constraints(self, plate_height, widths, heights):

        self.x_positions = IntVector('x_pos', self.circuits_num)
        self.y_positions = IntVector('y_pos', self.circuits_num)

        areas_index = np.argsort([self.h[i] * self.w[i] for i in range(self.circuits_num)])
        areas_index = areas_index[::-1]
        biggests = areas_index[0], areas_index[1]

        # Constraints

        # Domains
        self.sol.add([And(0 <= self.x_positions[i], self.x_positions[i] <= self.max_width - self.w[i])
                      for i in range(self.circuits_num)])

        self.sol.add([And(0 <= self.y_positions[i], self.y_positions[i] <= plate_height - self.h[i])
                      for i in range(self.circuits_num)])

        for i in range(1, self.circuits_num):
            for j in range(0, i):
                # No Overlapping
                self.sol.add(Or(Sum(self.y_positions[i], self.h[i]) <= self.y_positions[j],
                                Sum(self.y_positions[j], self.h[j]) <= self.y_positions[i],
                                Sum(self.x_positions[i], self.w[i]) <= self.x_positions[j],
                                Sum(self.x_positions[j], self.w[j]) <= self.x_positions[i]))

                # Symmetry breaking: two rectangles with same dimensions
                self.sol.add(Implies(And(self.w[i] == self.w[j], self.h[i] == self.h[j]),
                                     Or(self.x_positions[j] > self.x_positions[i],
                                        And(self.x_positions[j] == self.x_positions[i],
                                            self.y_positions[j] >= self.y_positions[i]))))

        # Symmetry breaking : fix relative position of the two biggest rectangles
        self.sol.add(Or(self.x_positions[biggests[1]] > self.x_positions[biggests[0]],
                        And(self.x_positions[biggests[1]] == self.x_positions[biggests[0]],
                            self.y_positions[biggests[1]] >= self.y_positions[biggests[0]])))

        # Cumulative over columns
        for u in range(self.max_width):
            self.sol.add(plate_height >= Sum([If(And(self.x_positions[i] <= u, u < Sum(self.x_positions[i], self.w[i])),
                                                 self.h[i], 0) for i in range(self.circuits_num)]))

    def evaluate(self):
        x = [int(self.sol.model().evaluate(self.x_positions[i]).as_string()) for i in range(self.circuits_num)]
        y = [int(self.sol.model().evaluate(self.y_positions[i]).as_string()) for i in range(self.circuits_num)]

        return [(self.w[i], self.h[i], x[i], y[i]) for i in range(self.circuits_num)]


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
xmind_name="jvm"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("动态分派")
r2=s2.getRootTopic()
r2.setTitle("动态分派")

content={
'动态分派':[
    {'定义':[
        '运行期根据【实际类型】确定【方法执行版】本的分派动作',
        {'典型表现':[
            '方法重写（Override）'
        ]}
    ]},
    {'invokevirtual指令运行时解析过程':[
        '1.找到操作数栈顶的第一个元素所指向的对象实际类型，记作C',
        {'2.在类型C中找到与常量中的描述符和简单名称都相符的方法':[
            {'2.1找得到，进行访问权限校验':[
                '通过:返回方法的直接引用，查找过程结束',
                '不通过:返回java.lang.IllegalAccessError异常'
            ]},
            {'2.2找不到，按照继承关系从下往上对C的各个父类进行搜索和验证':[

            ]}
        ]},
        '3.如始终没有找到合适的方法，抛出java.lang.AbstractMethodError'
    ]},
    {'方法重写本质':[
        'invokevirtual指令执行的第一步：在运行期确定接收者的实际类型',
        '根据【方法接收者】的【实际类型】来选择【方法版本】',
        '把常量池中方法的符号引用解析到直接引用上'
    ]},
    {'动态分派实现':[
        '方法版本选择过程需在【接收者类型的方法元数据】中搜索目标方法',
        {'优化手段':[
            '为类型在方法区中建【虚方法表】,使用虚方法表索引来代替元数据查找'
        ]},
        '虚方法表在【类加载】【连接阶段】进行初始化，类的变量初始值后，把类的虚方法表一同初始化'
    ]}
],
'单分派与多分派':[
    {'方法的宗量':[
        '方法的接收者与参数'
    ]},
    {'单分派':[
        '根据一个宗量对目标方法进行选择'
    ]},
    {'多分派':[
        '根据多个宗量对目标方法进行选择'
    ]},
    {'静态分派,选择目标方法的依据':[
        '静态类型',
        '方法参数',
        '属于多分派类型,根据两个宗量进行选择'
    ]},
    {'动态分派,选择目标方法的依据':[
        '方法接受者的实际类型'
    ]}
],
'总结':[
    {'静态分派':[
        '方法（括号内）参数的类型有关（只关注有几个参数类型）'
    ]},
    {'动态分派':[
        '和方法的调用者有关'
    ]}
]
}

#构建xmind
xmind.build(content,r2)

#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")


#Permite extraer porciones de texto añadiéndolas a una nueva variable

texto = "ABCDEFGHIJKLMÑOPQURST"

fragmento = texto[2:6] #<-----------------------------------------
#                 |                                              |
#                 |                          Número en que termina
#      Número en que comienza

fragmento1 = texto[2:] #Si se deja el último valor vacío irá hasta el final de la cadena
fragmento2 = texto[:7] #Si se deja el primer valor vacío comienza en la posición 0 hasta la especificada
fragmento3 = texto[::2] #Si se añade un tercer valor saltará las posiciones indicadas
fragmento4 = texto[::-1] #Si se añade un elemento negativo en el tercer valor se escribirá la cadena al revés
print(fragmento4)


import colorGenerator as cG

red = cG.Color(rgb=(255, 0, 0))
blue = cG.Color(rgb=(0, 255, 0))

# Save red into test3-red.json
red.save("test3-red.json")
# Loading saved color
load_red = cG.Color.load("test3-red.json")
print("The saved color is : ", end="")
load_red.show()

# We create a palette
palette = cG.Palette(colors=[red, blue])
# Save the palette test3-palette.json
palette.save("test3-palette.json")
# Loading saved palette
load_palette = cG.Palette.load("test3-palette.json")
print("The saved palette of red and blue is : ", end="")
load_palette.show()


import Queue
import os
os.chdir("c:/users/matthew/watchdog")
from watchdog import WatchDog
import threading

dirs = ["c:/users/matthew/watchdog", "c:/users/matthew/pyimgur"]

class WD(threading.Thread):
    def run(self):
        while True:
            path = pathPool.get()
            if path:
                w = WatchDog(path, 2)
                w.monitor()

pathPool = Queue.Queue(0)
for x in xrange(len(dirs)):
    WD().start()

for dir in dirs:
    pathPool.put(dir)


import os
import time

import boto3
import pytest

from main import LOCALSTACK_URL, release_files

PUBLISH_BUCKET = "test-publish-bucket"
EMBARGO_BUCKET = "test-embargo-bucket"

# This key corresponds to assets belonging to a dataset in the embargo bucket
# that needs to be released to the public bucket.
S3_PREFIX_TO_MOVE = "1/10/"

# This key corresponds to assets belonging to a dataset version
# that should remain untouched by this lambda function
S3_PREFIX_TO_LEAVE = "1/100/"

# This is a dummy file
FILENAME = "test.txt"

s3_resource = boto3.resource("s3", endpoint_url=LOCALSTACK_URL)

@pytest.fixture(scope="module")
def setup():
    os.environ.update(
        {
            "PUBLISH_BUCKET": PUBLISH_BUCKET,
            "EMBARGO_BUCKET": EMBARGO_BUCKET,
        }
    )

    for _ in range(10):
        try:
            list(s3_resource.buckets.all())
            print("Localstack running")
            break
        except:
            print("Waiting for Localstack to start...")
            time.sleep(1)
    else:
        raise Exception("Localstack did not start.")

@pytest.fixture(scope="function")
def publish_bucket(setup):
    return setup_bucket(PUBLISH_BUCKET)

@pytest.fixture(scope="function")
def embargo_bucket(setup):
    return setup_bucket(EMBARGO_BUCKET)

def test_copy_files_to_publish_bucket(publish_bucket, embargo_bucket):
    s3_key_to_move = os.path.join(S3_PREFIX_TO_MOVE, FILENAME)
    s3_key_to_leave = os.path.join(S3_PREFIX_TO_LEAVE, FILENAME)

    embargo_bucket.upload_file(Filename=FILENAME, Key=s3_key_to_move)
    embargo_bucket.upload_file(Filename=FILENAME, Key=s3_key_to_leave)

    assert sorted(s3_keys(publish_bucket)) == []
    assert sorted(s3_keys(embargo_bucket)) == sorted([s3_key_to_move, s3_key_to_leave])

    release_files(S3_PREFIX_TO_MOVE, EMBARGO_BUCKET, PUBLISH_BUCKET)

    # VERIFY RESULTS
    release_results_key = os.path.join(
        S3_PREFIX_TO_MOVE, "discover-release-results.json"
    )
    assert sorted(s3_keys(publish_bucket)) == sorted(
        [s3_key_to_move, release_results_key]
    )
    assert sorted(s3_keys(embargo_bucket)) == sorted([s3_key_to_leave])

def test_handle_key_without_trailing_slash(publish_bucket, embargo_bucket):
    s3_key_to_move = os.path.join(S3_PREFIX_TO_MOVE, FILENAME)
    s3_key_to_leave = os.path.join(S3_PREFIX_TO_LEAVE, FILENAME)

    embargo_bucket.upload_file(Filename=FILENAME, Key=s3_key_to_move)
    embargo_bucket.upload_file(Filename=FILENAME, Key=s3_key_to_leave)

    assert sorted(s3_keys(publish_bucket)) == []
    assert sorted(s3_keys(embargo_bucket)) == sorted([s3_key_to_move, s3_key_to_leave])

    release_files(S3_PREFIX_TO_MOVE, EMBARGO_BUCKET, PUBLISH_BUCKET)

    # VERIFY RESULTS
    release_results_key = os.path.join(
        S3_PREFIX_TO_MOVE, "discover-release-results.json"
    )
    assert sorted(s3_keys(publish_bucket)) == sorted(
        [s3_key_to_move, release_results_key]
    )
    assert sorted(s3_keys(embargo_bucket)) == [s3_key_to_leave]

def test_copy_files_pagination(publish_bucket, embargo_bucket):
    # More keys than the S3 page size
    s3_keys_to_move = create_keys(S3_PREFIX_TO_MOVE, FILENAME, 1200)
    s3_keys_to_leave = [os.path.join(S3_PREFIX_TO_LEAVE, FILENAME)]

    for key in s3_keys_to_move:
        embargo_bucket.upload_file(Filename=FILENAME, Key=key)

    for key in s3_keys_to_leave:
        embargo_bucket.upload_file(Filename=FILENAME, Key=key)

    assert sorted(s3_keys(publish_bucket)) == []
    assert sorted(s3_keys(embargo_bucket)) == sorted(s3_keys_to_move + s3_keys_to_leave)

    release_files(S3_PREFIX_TO_MOVE, EMBARGO_BUCKET, PUBLISH_BUCKET)

    # VERIFY RESULTS
    release_results_key = os.path.join(
        S3_PREFIX_TO_MOVE, "discover-release-results.json"
    )
    s3_keys_to_move.append(release_results_key)
    assert sorted(s3_keys(publish_bucket)) == sorted(s3_keys_to_move)
    assert sorted(s3_keys(embargo_bucket)) == sorted(s3_keys_to_leave)

def setup_bucket(bucket_name):
    s3_resource.create_bucket(Bucket=bucket_name)
    bucket = s3_resource.Bucket(bucket_name)
    bucket.objects.all().delete()
    return bucket

def s3_keys(bucket):
    return [obj.key for obj in bucket.objects.all()]

def create_keys(prefix, filename, n):
    return ["{}/{}{}".format(prefix, i, filename) for i in range(n)]


import tensorflow as tf
from block import dense_layer, sample_normal

def inference_block(inputs, d_theta, output_units, name):
    """
    Three dense layers in sequence.
    :param inputs: batch of inputs.
    :param d_theta: dimensionality of the intermediate hidden layers.
    :param output_units: dimensionality of the output.
    :param name: name used to scope this operation.
    :return: batch of outputs.
     """
    h = dense_layer(inputs, d_theta, tf.nn.elu, True, name + '1')
    h = dense_layer(h, d_theta, tf.nn.elu, True, name + '2')
    h = dense_layer(h, output_units, None, True, name + '3')
    # print('inference: 17',h.shape)
    return h

def infer_classifier(features, labels, d_theta, num_classes):
    """
    Infer a linear classifier by concatenating vectors for each class.
    :param features: tensor (tasks_per_batch x num_features) feature matrix
    :param labels:  tensor (tasks_per_batch x num_classes) one-hot label matrix
    :param d_theta: Integer number of features on final layer before classifier.
    :param num_classes: Integer number of classes per task.
    :return: Dictionary containing output classifier layer (including means and
    :        log variances for weights and biases).
    """

    classifier = {}
    class_weight_means = []
    class_weight_logvars = []
    class_bias_means = []
    class_bias_logvars = []
    # print('inference 37:  labels', labels.shape)
    for c in range(num_classes):
        class_mask = tf.equal(tf.argmax(labels, 1), c)  #tf.argmax在axis=1的时候，将每一行最大元素所在的索引记录下来
        # class_mask = tf.equal(labels, c)
        class_features = tf.boolean_mask(features, class_mask)
        # print('inference: 45:  class_features', class_features.shape)  # (?,3200)

        # Pool across dimensions
        nu = tf.expand_dims(tf.reduce_mean(class_features, axis=0), axis=0)  #均值
        # print('inference: 43', nu.shape)

        class_weight_means.append(inference_block(nu, d_theta, d_theta, 'weight_mean'))
        class_weight_logvars.append(inference_block(nu, d_theta, d_theta, 'weight_log_variance'))
        class_bias_means.append(inference_block(nu, d_theta, 1, 'bias_mean'))
        class_bias_logvars.append(inference_block(nu, d_theta, 1, 'bias_log_variance'))

    classifier['weight_mean'] = tf.transpose(tf.concat(class_weight_means, axis=0))
    classifier['bias_mean'] = tf.reshape(tf.concat(class_bias_means, axis=1), [num_classes, ])
    classifier['weight_log_variance'] = tf.transpose(tf.concat(class_weight_logvars, axis=0))
    classifier['bias_log_variance'] = tf.reshape(tf.concat(class_bias_logvars, axis=1), [num_classes, ])
    return classifier

def _post_process(pooled, units):
    """
    Process a pooled variable through 2 dense layers
    :param pooled: tensor of rank (1 x num_features).
    :param units: integer number of output features.
    :return: tensor of rank (1 x units)
    """
    h = dense_layer(pooled, units, tf.nn.elu, True, 'post_process_dense_1')
    h = dense_layer(h, units, tf.nn.elu, True, 'post_process_dense_2')

    return h


import numpy as np
from scipy.stats import dirichlet, mode
from itertools import product
from copy import deepcopy
from operator import attrgetter
from tqdm import trange

import argparse
import h5py
from preprocess import read_file, subst_file_suffix
import cProfile

class Structure:
    '''
    Implements the STRUCTURE 1.0 population structure model with admixture.

    Inputs:
    - X[l, i, a] (int): the genotype of allele copy `a` at locus `l` for individual `i`
    - J[l] (int): the number of possible alleles at locus `l`
    - POS[l] (int): unused, but required for ROLLOFF
    - K (int): the number of populations

    Want to estimate:
    - Z[l, i, a] (int): the origin of the `a`th allele copy at locus `l` in individual `i`
    - P[k, l, j] (float): the frequency of allele `j` at locus `l` in population `k`
    - Q[i, k] (float): the fraction of individual `i`'s ancestry from population `k`
    - alpha (float): a parameter to the underlying Dirichlet distribution. Indicates the
      degree of admixture in the populations.
    '''
    def __init__(self, X: np.ndarray, J: np.ndarray, POS: np.ndarray, K: int) -> None:
        '''
        Initializes a STRUCTURE data object.
        '''
        # Copy inputs
        self.X = X
        self.J = J
        self.POS = POS
        self.K = K
        # Convenience parameters: number of loci, number of individuals, and number of copies
        # per allele (ploidy)
        self.num_loci, self.sample_size, self.ploidy = self.X.shape
        # Initialize generator
        self.rng = np.random.default_rng()
        # Initialize Z: draw each Z[l, i, a] uniformly from k in {0, ..., K-1}
        self.Z = self.rng.integers(self.K, size=self.X.shape)
        # Initialize P: empty K * num_loci * max(J) array
        self.P = np.zeros((self.K, self.num_loci, np.max(self.J)))
        # Initialize Q: empty K * sample_size array
        self.Q = np.zeros((self.sample_size, self.K))
        # Initialize alpha: draw uniformly from [0, 10)
        self.alpha = self.rng.uniform(10)

    def step_pq(self):
        '''
        Performs the first step in the Gibbs training loop: sampling P, Q from Pr(P, Q | X, Z)
        '''
        # Sample P
        for l in range(self.num_loci):
            # N[k, j] = "the number of copies of allele j at locus l ... assigned (by Z)
            # to population k" (Algorithm A2, Pritchard et al. 2000)
            N = np.zeros((self.K, self.J[l]), dtype=np.uint64)
            for k, j in product(range(self.K), range(self.J[l])):
                N[k, j] = np.count_nonzero((self.X[l] == j) & (self.Z[l] == k))
            # Sample each P[k, l, :] from Dirichlet (N[k] + lambda)
            # Using different values of lambda[j] for each allele allows us to model correlations
            # between allele frequencies. This assumption is better for closely related populations.
            # We use lambda = 1.0, implying that allele frequencies are independent.
            lmbda = 1.0
            for k in range(self.K):
                # Since the sampled vector is variable-length, we assign it only to the first
                # J[l] elements of P[k, l]
                self.P[k, l, 0:self.J[l]] = self.rng.dirichlet(N[k] + lmbda)
        # Sample Q
        M = np.zeros_like(self.Q)
        for i in range(self.sample_size):
            # M[i, k] = "the number of allele copies in individual i that originated (according to Z)
            # in population k" (Algorithm A3, Pritchard et al. 2000)
            for k in range(self.K):
                M[i, k] = np.count_nonzero(self.Z[:, i, :] == k)
            # Sample each Q[i] from Dirichlet (M[i] + alpha)
            self.Q[i] = self.rng.dirichlet(M[i] + self.alpha)

    def step_z(self):
        '''
        Performs the second step in the Gibbs training loop: sampling Z from Pr(Z | X, P, Q)
        '''
        ## This vectorized sampling algorithm converts continuous uniform samples to k-values
        ## by using the cdf of Pr(Z[l, i, a] | X, P, Q). Previously, we used a naive
        ## sampling algorithm based on calling rng.choice() once for each [l, i, a]. This took
        ## a very long time to run, and rng.choice() cannot be vectorized.

        # Compute the pmf of Pr(Z[l, i, a] = k | X, P, Q) using Equation A12
        pmf_k = np.zeros((self.num_loci, self.sample_size, self.ploidy, self.K))
        # Use np.einsum to calculate the pmf separately for each l; can't vectorize past that
        for l in range(self.num_loci):
            pmf_k[l] = np.einsum('ik,kia->iak', self.Q, self.P[:, l, self.X[l]])
        # Divide through by the sum over k
        pmf_k /= np.sum(pmf_k, axis=3, keepdims=True)
        # Convert to cdf; remove the last "hyper-row" (always 1.0)
        cdf = np.cumsum(pmf_k[:, :, :, :-1], axis=3)
        # Draw random samples from continuous uniform U(0, 1) for each [l, i, a]
        uniform_samples = self.rng.random(self.Z.shape + (1,))
        # Convert the uniform samples to k-values by counting the number of k-indices for which
        # the uniform sample is greater than the cdf at that index
        self.Z = np.count_nonzero(uniform_samples > cdf, axis=3)

    def log_likelihood_alpha(self, alpha):
        '''
        Returns the log likelihood of alpha given the current values of Q.
        The likelihood function of alpha is equal to the product of the probabilities of
        each Q[i] as drawn from a Dirichlet distribution where all concentration parameters
        are set to alpha.
        '''
        log_likelihoods = np.zeros(self.sample_size)
        alpha_vec = np.full(self.K, alpha)
        for i in range(self.sample_size):
            log_likelihoods[i] = dirichlet.logpdf(self.Q[i], alpha_vec)
        return np.sum(log_likelihoods)

    def step_alpha(self):
        '''
        Performs a Metropolis-Hastings step for alpha.
        '''
        # Sample a new alpha from N(alpha, 0.25)
        new_alpha = self.rng.normal(self.alpha, 0.25)
        # alpha must be in (0, 10)
        if 0 < new_alpha and new_alpha < 10:
            # Compute the likelihood ratio of the old and new alphas
            likelihood_ratio = np.exp(self.log_likelihood_alpha(new_alpha) - self.log_likelihood_alpha(self.alpha))
            # Draw a uniform random variable between 0 and 1
            uniform = self.rng.random()
            # With probability `likelihood_ratio`, accept the new alpha
            if uniform <= likelihood_ratio:
                self.alpha = new_alpha

    def gibbs_round(self):
        '''
        Performs one round of Gibbs sampling. See Algorithm 2 in Pritchard et al. (2000).
        '''
        self.step_pq()
        self.step_z()
        self.step_alpha()

    def gibbs_sampling(self, m: int = 1000, c: int = 50, num_samples: int = 30):
        '''
        Performs a Gibbs sampling loop.

        Parameters:
        - m (int): the burn-in period
        - c (int): the number of rounds between samples
        - num_samples (int): the number of samples to take
        '''
        # Burn-in: discard the first several rounds
        for _ in trange(m, desc='Burn-in rounds'):
            self.gibbs_round()
        # Take a sample every t rounds
        samples = []
        for sample_num in trange(num_samples, desc='Take samples'):
            for _ in trange(c, desc=f'Rounds for sample {sample_num}'):
                self.gibbs_round()
            # Use deepcopy to take a snapshot of the object state
            samples.append(deepcopy(self))
        # Aggregate all samples
        Z_all = np.stack(list(map(attrgetter('Z'), samples)), axis=0)
        P_all = np.stack(list(map(attrgetter('P'), samples)), axis=0)
        Q_all = np.stack(list(map(attrgetter('Q'), samples)), axis=0)
        alpha_all = np.array(list(map(attrgetter('alpha'), samples)))
        # Take the mean of P, Q, alpha, and the mode of Z; and assign them to this object
        self.Z = mode(Z_all, axis=0)[0].squeeze(axis=0)
        self.P = np.mean(P_all, axis=0)
        self.Q = np.mean(Q_all, axis=0)
        self.alpha = np.mean(alpha_all)

    def save(self, hdf5_file):
        '''
        Saves the state of this data object to an HDF5 file.
        '''
        with h5py.File(hdf5_file, 'w') as f:
            attrs_to_store = ['X', 'J', 'POS', 'K', 'Z', 'P', 'Q', 'alpha']
            for attr in attrs_to_store:
                f[attr] = getattr(self, attr)

def parse_args():
    parser = argparse.ArgumentParser(description='Infer the population structure of a genetic sample using the STRUCTURE algorithm')
    parser.add_argument('file', metavar='data_file')
    parser.add_argument('-k', type=int, default=2, metavar='num_populations')
    parser.add_argument('-o', '--out', metavar='output.hdf5')
    parser.add_argument('--profile', action='store_true')
    parser.add_argument('-d', '--drop-frac', type=float, default=0.6, metavar='drop_frac')
    parser.add_argument('-m', '--burn-in', type=int, default=400, metavar='num_burn_in_rounds')
    parser.add_argument('-s', '--num-samples', type=int, default=20, metavar='num_samples')
    parser.add_argument('-c', '--sample-interval', type=int, default=20, metavar='num_rounds_btwn_samples')
    return parser.parse_args()

def main():
    # Parse arguments
    args = parse_args()
    # Read from file and randomly drop loci
    X, J, POS = read_file(args.file, drop_frac=args.drop_frac)
    # Run the STRUCTURE algorithm
    structure = Structure(X, J, POS, args.k)
    if args.profile:
        cProfile.runctx('structure.gibbs_round()', {}, {'structure': structure}, sort='cumtime')
    else:
        structure.gibbs_sampling(args.burn_in, args.sample_interval, args.num_samples)
        # Create output file using command-line argument if provided. If not, construct
        # the filename by replacing the file extension with .hdf5
        if args.out:
            out = args.out
        else:
            out = subst_file_suffix(args.file, '.hdf5')
        # Write to the output file
        structure.save(out)

if __name__ == '__main__':
    main()


import numpy as np
from PIL import Image
from DeepFeatures import DeepFeatures
from datetime import datetime
from flask import Flask, request, render_template
from pathlib import Path

app = Flask(__name__)

df = DeepFeatures()

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        features = []
        img_paths = []
        for feature_path in Path("./static/feature").glob("*.npy"):
            features.append(np.load(feature_path))
            img_paths.append(Path("./static/image") / (feature_path.stem + ".jpg"))
        features = np.array(features)
        file = request.files['query_img']
        img = Image.open(file.stream)
        uploaded_img_path = "static/uploaded/" + datetime.now().isoformat().replace(":", ".") + "_" + file.filename
        img.save(uploaded_img_path)
        query = df.extract(img)
        dists = np.linalg.norm(features-query, axis=1)
        ids = np.argsort(dists)[:5]
        scores = [(img_paths[id].stem, img_paths[id]) for id in ids]

        return render_template('index.html', query_path=uploaded_img_path, scores=scores)
    else:
        return render_template('index.html')

@app.route('/upload', methods=['GET','POST'])
def upload():
    if request.method == 'POST':
        file = request.files['query_img']
        img = Image.open(file.stream)
        room = request.form['room']
        store = request.form['store']
        rack = request.form['rack']
        name = room + '-' + store + '-' + rack
        uploaded_img_path = "static/image/" + name + ".jpg"
        img.save(uploaded_img_path)

        feature = df.extract(img)
        fpath = Path("./static/feature")/(name + ".npy")
        np.save(fpath, feature)

        return render_template('upload.html')
    else:
        return render_template('upload.html')

if __name__=="__main__":
    app.run(host = "127.0.0.1", port = 8000)


"""
Plot things vs. Qprism time series as a little scribble for each section.

"""
from pathlib import Path
import sys
import matplotlib.pyplot as plt
import numpy as np
import pickle
import pandas as pd
from time import time
from datetime import datetime, timedelta
import xarray as xr

from lo_tools import Lfun, zfun
from lo_tools import plotting_functions as pfun

year = 2018
g = 9.8
beta = 7.7e-4
year_str = str(year)

# limit the time range
if True:
    dt0 = datetime(year, 7, 1, 12)
    dt1 = datetime(year, 10, 31, 12)
else:
    dt0 = datetime(year, 1, 1, 12)
    dt1 = datetime(year, 12, 31, 12)

plt.close('all')
for gtagex in ['cas6_v3_lo8b', 'cas6_v3t075_lo8', 'cas6_v3t110_lo8']:

    gridname, tag, ex_name = gtagex.split('_')
    Ldir = Lfun.Lstart(gridname=gridname, tag=tag, ex_name=ex_name)

    pth = Ldir['LO'] / 'extract' / 'tef'
    if str(pth) not in sys.path:
        sys.path.append(str(pth))
    import tef_fun
    import flux_fun

    if False:
        sect_df = tef_fun.get_sect_df(gridname)
        sect_list = list(sect_df.index)
    else:
        sect_list = ['ai1','ai4', 'tn2', 'sji1']

    # specify bulk folder
    dates_string = str(year) + '.01.01_' + str(year) + '.12.31'
    # ext_in_dir = Ldir['LOo'] / 'extract' / Ldir['gtagex'] / 'tef' / ('extractions_' + dates_string)
    bulk_in_dir = Ldir['LOo'] / 'extract' / Ldir['gtagex'] / 'tef' / ('bulk_' + dates_string)

    # prep output location for plots
    out_dir = Ldir['parent'] / 'LPM_output' / 'extract' / 'tef'
    Lfun.make_dir(out_dir)

    # PLOTTING
    pfun.start_plot(fs=16, figsize=(18,6))

    fig = plt.figure()

    ax1 = fig.add_subplot(131)
    ax1.grid(True)
    ax1.set_xlabel(r'$Q_{prism}\ [10^{3}\ m^{3}s^{-1}]$')
    ax1.set_ylabel(r'$Q_{in}\ [10^{3}\ m^{3}s^{-1}]$')

    ax2 = fig.add_subplot(132)
    ax2.grid(True)
    ax2.set_xlabel(r'$Q_{prism}\ [10^{3}\ m^{3}s^{-1}]$')
    ax2.set_ylabel(r'$\Delta S\ [g\ kg^{-1}]$')

    ax3 = fig.add_subplot(133)
    ax3.grid(True)
    ax3.set_xlabel(r'$Q_{prism}\ [10^{3}\ m^{3}s^{-1}]$')
    ax3.set_ylabel(r'$Q_{in} \Delta S\ [g\ kg^{-1}\ 10^{3}\ m^{3}s^{-1}]$')

    # ax4 = fig.add_subplot(224)
    # ax4.grid(True)
    # ax4.set_xlabel(r'$Q_{prism}\ [10^{3}\ m^{3}s^{-1}]$')
    # ax4.set_ylabel(r'$Ri$')

    for sect_name in sect_list:
        # get two-layer time series
        tef_df, in_sign, dir_str, sdir = flux_fun.get_two_layer(bulk_in_dir, sect_name, 'cas6')

        # limit the time range
        tef_df = tef_df[dt0:dt1]

        # make derived variables
        tef_df['DS'] = tef_df['salt_in'] - tef_df['salt_out']
        qp = tef_df['qabs'].to_numpy()/2
        tef_df['Qprism'] = qp

        # drop times with negative DS
        tef_df[tef_df['DS']<=0] = np.nan

        # # get section info
        # ds = xr.open_dataset(ext_in_dir / (sect_name + '.nc'))
        # A = ds.DA0.sum().values
        # A2 = A*A
        # H = ds.h.max().values
        # ds.close()
        # # and calculate Ri
        # tef_df['Ri'] = g*beta*tef_df['DS']*A2*H/(32*tef_df['Qin']*tef_df['Qin'])
        # tef_df['Ri'][tef_df['Ri'] <= 0] = np.nan

        # and plot this section
        ax1.loglog(tef_df['Qprism'].to_numpy()/1000,tef_df['Qin'].to_numpy()/1000,'-', label=sect_name, alpha=.8)
        ax2.loglog(tef_df['Qprism'].to_numpy()/1000,tef_df['DS'].to_numpy(),'-', label=sect_name, alpha=.8)
        ax3.loglog(tef_df['Qprism'].to_numpy()/1000,(tef_df['Qin']*tef_df['DS']).to_numpy()/1000,'-', label=sect_name, alpha=.8)
        # ax4.loglog(tef_df['Qprism'].to_numpy()/1000,tef_df['Ri'],'-', label=sect_name, alpha=.8)

    ax1.legend()
    #ax1.set_title('lag = %d days' % (lag))
    fig.tight_layout()

    ax1.set_xlim(10,1000)
    ax2.set_xlim(10,1000)
    ax3.set_xlim(10,1000)
    ax1.set_ylim(3,300)
    ax2.set_ylim(.1,30)
    ax3.set_ylim(.5,500)

    # add some reference slopes
    xx = np.logspace(np.log10(10),np.log10(1000),100)
    ax1.plot(xx,2000/xx,'-k', label=r'$Q_{prism}^{-1}$',alpha=.5,linewidth=2)
    ax1.plot(xx,xx/3,'--k', label=r'$Q_{prism}^{-1}$',alpha=.5,linewidth=2)
    ax2.plot(xx,5000/xx**2,'-k', label=r'$Q_{prism}^{-2}$',alpha=.5,linewidth=2)
    ax3.plot(xx,1e7/xx**3,'-k', label=r'$Q_{prism}^{-2}$',alpha=.5,linewidth=2)

    ax1.text(.1,.9,gtagex,transform=ax1.transAxes,fontweight='bold')

    bb = {'facecolor': 'w', 'edgecolor': 'None', 'alpha': 0.8}
    ax1.text(.83,.9,r'~$Q_{prism}^{+1}$',transform=ax1.transAxes,
        fontweight='bold',alpha=.5,ha='center',bbox=bb)
    ax1.text(.8,.05,r'~$Q_{prism}^{-1}$',transform=ax1.transAxes,
        fontweight='bold',alpha=.5,ha='center',bbox=bb)
    ax2.text(.67,.05,r'~$Q_{prism}^{-2}$',transform=ax2.transAxes,
        fontweight='bold',alpha=.5,ha='center',bbox=bb)
    ax3.text(.63,.05,r'~$Q_{prism}^{-3}$',transform=ax3.transAxes,
        fontweight='bold',alpha=.5,ha='center',bbox=bb)

    fig.savefig(out_dir / ('scribble_vs_Qprism_' + gtagex + '.png'))

    plt.show()
    pfun.end_plot()


from collections import namedtuple
import logging
import multiprocessing
import os
import sys

from .utils import Env

##
## Logging
##

# Options are: file, stdout
LOG_METHOD = Env.string('DDD_LOG_METHOD', 'stdout')
LOG_LEVEL = Env.string('DDD_LOGGING_LEVEL', 'INFO')
LOG_PATH = Env.string('DDD_LOG_PATH', os.path.dirname(__file__))
LOG_FILE = Env.string('DDD_LOG_FILE', 'ding-dong-ditch.log')

basicConfig_args = dict(
    format='[%(asctime)s] %(levelname)s %(name)s (%(thread)d): %(message)s',
    level=LOG_LEVEL
)

if LOG_METHOD == 'file':
    basicConfig_args['filename'] = os.path.join(LOG_PATH, LOG_FILE)
elif LOG_METHOD == 'stdout':
    basicConfig_args['stream'] = sys.stdout
else:
    raise ValueError('Unknown value for LOG_METHOD: {}'.format(LOG_METHOD))

logging.basicConfig(**basicConfig_args)

if LOG_LEVEL is not 'DEBUG':
    # Configure some sub-loggers to be quieter, unless the global level is DEBUG
    connectionpool_logger = logging.getLogger('requests.packages.urllib3.connectionpool')
    connectionpool_logger.setLevel(logging.WARNING)

    oauth_logger = logging.getLogger('oauth2client.client')
    oauth_logger.setLevel(logging.WARNING)

##
## Twilio
##

# Twilio REST API credentials
TWILIO_SID = Env.string('DDD_TWILIO_SID')
TWILIO_TOKEN = Env.string('DDD_TWILIO_SECRET_TOKEN')

# "From" phone number associated with the Twilio account
FROM_NUMBER = Env.string('DDD_FROM_NUMBER')

##
## Hardware Interface
##

# The throttle rate of the doorbell trigger, in seconds.
BUZZER_INTERVAL = Env.number('DDD_BUZZER_INTERVAL', 30)

# The time (in seconds) to wait after the buzzer is pushed to notify recipients.
BUZZER_HOLD = Env.number('DDD_BUZZER_HOLD', 0.3)

# Whether to always ring the bell, regardless of user setting, when the network is down
RING_FALLBACK = Env.boolean('DDD_RING_FALLBACK', True)

# The duration the strike should stay open when released, in seconds.
STRIKE_RELEASE_DURATION = Env.number('DDD_STRIKE_RELEASE_DURATION', 3)

##
## Unit 1
##

# The ID of Unit 1. This will also be the dial-in PIN.
UNIT_1_ID = Env.string('DDD_UNIT_1_ID')

# The GPIO input pin number that will pull up when the doorbell is rung
GPIO_INPUT_PIN_BELL_1 = Env.number('DDD_GPIO_INPUT_PIN_BELL_1')

# The GPIO output pin that will trigger the traditional doorbell
GPIO_OUTPUT_PIN_BELL_1 = Env.number('DDD_GPIO_OUTPUT_PIN_BELL_1')

# The GPIO output pin that will trigger the gate opener/strike
GPIO_OUTPUT_PIN_STIKE_1 = Env.number('DDD_GPIO_OUTPUT_PIN_STRIKE_1')

##
## Unit 2
##

# The ID of Unit 2. This will also be the dial-in PIN.
UNIT_2_ID = Env.string('DDD_UNIT_2_ID')

# The GPIO input pin number that will pull up when the doorbell is rung
GPIO_INPUT_PIN_BELL_2 = Env.number('DDD_GPIO_INPUT_PIN_BELL_2')

# The GPIO output pin that will trigger the traditional doorbell
GPIO_OUTPUT_PIN_BELL_2 = Env.number('DDD_GPIO_OUTPUT_PIN_BELL_2')

# The GPIO output pin that will trigger the gate opener/strike
GPIO_OUTPUT_PIN_STIKE_2 = Env.number('DDD_GPIO_OUTPUT_PIN_STRIKE_2')

##
## Firebase
##

# The data source for user settings. Currently, only Firebase is supported.
USER_SETTINGS_ADAPTER = 'firebase'

# The name of the Firebase app, used to construct the REST URL.
FIREBASE_APP_NAME = Env.string('DDD_FIREBASE_APP_NAME', 'ding-dong-ditch')

# The URL to the Firebase Cloud Function that initiates the voice doorbell.
FIREBASE_CLOUD_FUNCTION_NOTIFY_URL = Env.string('DDD_FIREBASE_CLOUD_FUNCTION_NOTIFY_URL')

# Path to service account credentials file
FIREBASE_KEY_PATH = Env.string('DDD_FIREBASE_KEY_PATH', '/home/pi/.firebasekey')

# Firebase web API key
FIREBASE_API_KEY = Env.string('DDD_FIREBASE_API_KEY')

# Firebase Cloud Messaging Server Key
FIREBASE_FCM_KEY = Env.string('DDD_FIREBASE_FCM_KEY')

# Root path for user settings in adapter
USER_SETTINGS_PATH = '/settings'

# Path to system-level settings in adapter
SYSTEM_SETTINGS_PATH = '/systemSettings'

##
## Unit configuration
##

# Representation of a unit in the building
Unit = namedtuple('Unit', 'id buzzer bell strike')

# The first unit
UNIT_1 = Unit(
    id=UNIT_1_ID,
    buzzer=GPIO_INPUT_PIN_BELL_1,
    bell=GPIO_OUTPUT_PIN_BELL_1,
    strike=GPIO_OUTPUT_PIN_STIKE_1
)

# The second unit
UNIT_2 = Unit(
    id=UNIT_2_ID,
    buzzer=GPIO_INPUT_PIN_BELL_2,
    bell=GPIO_OUTPUT_PIN_BELL_2,
    strike=GPIO_OUTPUT_PIN_STIKE_2
)

def get_unit_by_id(unit_id):
    """
    Return a Unit by its ID.
    """
    # unit ID should always be a string
    unit_id = str(unit_id)
    if unit_id == UNIT_1.id:
        return UNIT_1
    elif unit_id == UNIT_2.id:
        return UNIT_2
    return None

##
## Other configuration
##

# Number of threads to maintain in the notifier threadpool
NOTIFIER_THREADPOOL_SIZE = Env.number(
    'DDD_NOTIFIER_THREADPOOL_SIZE',
    multiprocessing.cpu_count() * 2
)


#!/usr/bin/env python
import csv
import requests

pat_ids = []
with open('KIRC.focal_score_by_genes.tsv') as f:
    pat_ids = f.readline().strip().split('\t')[3:]

num_pats = len(pat_ids)
print('Found patient ids of size:', num_pats)
with open('tcga-mapping.csv-2', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['ensembl_gene_id', 'entrez_gene_id'])
    i = 1
    for pat_id in pat_ids:
        res = requests.get(f"https://api.gdc.cancer.gov/v0/all?query={pat_id}&size=5").json()
        hits = res['data']['query']['hits']
        ent_id = hits[0]['submitter_id'] if len(hits) > 0 else None
        writer.writerow([pat_id, ent_id])
        print(f'\tFound match {i:3}/{num_pats} {pat_id} --> {ent_id}\r', end='')
        i += 1
    print('\n')


import re
import copy
from enum import Enum

# Setup classes
class Army(Enum):
    Immune = 0
    Infection = 1

class Damage(Enum):
    Bludgeoning = 'Bludgeoning'
    Cold = 'Cold'
    Fire = 'Fire'
    Radiation = 'Radiation'
    Slashing = 'Slashing'

class Group:
    def __init__(self, army, id_number, units, hp, damage, damage_type, weaknesses, immunities,
                 initiative):
        self.army = army
        self.id_number = id_number
        self.units = units
        self.hp = hp
        self.damage = damage
        self.damage_type = damage_type
        self.weaknesses = weaknesses
        self.immunities = immunities
        self.initiative = initiative

    @property
    def name(self):
        return '{} {}'.format(self.army.name, self.id_number)

    @property
    def effective_power(self):
        return self.units * self.damage

    def __repr__(self):
        return '{} - {} units ({} HP, {} {} DMG, {} INT) - {} EP'.format(
            self.name, self.units, self.hp, self.damage, self.damage_type.name,
            self.initiative, self.effective_power
        )

def read_input(filename):
    groups = []
    current_army = None
    current_id = None

    for line in open(filename, 'r').readlines():
        if line.isspace():
            continue

        if 'Immune System' in line:
            current_army = Army.Immune
            current_id = 1
        elif 'Infection' in line:
            current_army = Army.Infection
            current_id = 1

        # Process unit information from line
        else:
            # Extract numerical values
            numbers = [int(d) for d in re.findall(r'\d+', line)]
            units, hp, damage, initiative = numbers

            # Extract damage type
            damage_type = Damage(line.split()[-5].title())

            # Extract initiative and weaknesses
            weaknesses = []
            immunities = []
            section = line[line.find('(')+1:line.find(')')]
            for data in section.split(';'):
                data = data.replace(',', '').split()  # Remove commas
                if data[0] == 'weak':
                    for weakness_type in data[2:]:
                        weaknesses.append(Damage(weakness_type.title()))
                elif data[0] == 'immune':
                    for immunity_type in data[2:]:
                        immunities.append(Damage(immunity_type.title()))

            g = Group(current_army, current_id, units, hp, damage, damage_type, weaknesses,
                      immunities, initiative)
            groups.append(g)
            current_id += 1

    return groups

def calculate_damage(attacker, defender):
    # No damage done if the defender is immune
    if attacker.damage_type in defender.immunities:
        return 0

    damage = attacker.effective_power

    # Apply weaknesses
    if attacker.damage_type in defender.weaknesses:
        damage *= 2

    return damage

def select_target(group, groups, unavailable):
    max_damage = 0
    target = None

    for other in groups:
        #Don't attack yourself or your own team
        if group.army == other.army:
            continue

        #Don't attack a group already being attacked or one that is dead
        if other in unavailable or other.units == 0:
            continue

        damage = calculate_damage(group, other)

        #Don't attack an immune group
        if damage == 0:
            continue

        if damage > max_damage:
            max_damage = damage
            target = other

        # Resolve ties
        elif damage == max_damage:
            if ((other.effective_power, other.initiative) >
                    (target.effective_power, target.initiative)):
                target = other

    return target

def apply_damage(group, damage):
    units_killed = damage//group.hp
    group.units = max(0, group.units - units_killed)

def both_alive(groups):
    alive = set()
    for g in groups:
        if g.units > 0:
            alive.add(g.army)

    return len(alive) == 2

def fight(groups):
    # Target selection
    targets = {}  # Map a group to the group they are attacking
    for group in sorted(groups, key=lambda g: (-g.effective_power, -g.initiative)):
        target = select_target(group, groups, targets.values())
        if target is not None:
            targets[group] = target

    # Attack
    for group in sorted(groups, key=lambda g: (-g.initiative)):
        # Groups with no units or no target cannot attack
        if group.units == 0 or group not in targets:
            continue

        target = targets[group]
        damage = calculate_damage(group, target)
        #print('{} deals {} damage to {}'.format(group.name, damage, target.name))
        apply_damage(target, damage)

def simulate_battle():
    initial_groups = read_input('input.txt')
    winner = None
    boost = 0

    while winner != Army.Immune:
        groups = copy.deepcopy(initial_groups)

        # Apply the boost
        for g in groups:
            if g.army == Army.Immune:
                g.damage += boost

        stalemate = False
        prev_units = sum([g.units for g in groups])
        fight_count = 1
        while both_alive(groups):
            fight(groups)
            fight_count += 1

            # Stalemate check
            current_units = sum([g.units for g in groups])
            if current_units == prev_units:
                stalemate = True
                break

            prev_units = current_units

        # Determine winner
        if stalemate:
            print('There was a stalemate (boost was {})'.format(boost))
        else:
            surviving_groups = [g for g in groups if g.units > 0]
            winner = surviving_groups[0].army

            print('{} won after {} fights with {} surviving units (boost was {}).'.format(
                winner.name, fight_count, sum([g.units for g in groups]), boost)
            )

        boost += 1

simulate_battle()


import csv
import glob
import os
import numpy
import sys

def median(list_temp):
    return numpy.median(numpy.array(list_temp))

def main(args):

    path = args[0]
    data = []
    data.append(['csv', 'Red Clear Median', 'Blue Clear Median','Temperature', 'Battery'])

    for i,filename in enumerate(glob.glob(os.path.join(path, '*.csv'))):
        print (filename, i)
        csv_name = filename.split('/')[-1]
        if 'output' in filename:
            continue

        with open(filename,'rU') as f:
            reader = csv.reader(f)

            blue_clear = [] #Blue LED, Clear Channel
            blue_seq = [] #Blue LED, Sequence number
            red_clear = [] #Red LED, Clear Channel
            red_seq = [] #Red LED, Sequence number
            temperature = [] #Temperature recorded
            battery = [] #battery voltage
            sensor_data_start = False

            for i, row in enumerate(reader):
                if "Timestamp" in row[0]:
                    sensor_data_start = True

                elif sensor_data_start and "BLUE" in row[6]:
                    blue_clear.append(int(row[4]))
                    blue_seq.append(int(row[5]))
                    temperature.append(float(row[7]))
                    battery.append(float(row[8]))

                elif sensor_data_start and "RED" in row[6]:
                    red_clear.append(int(row[4]))
                    red_seq.append(int(row[5]))
                    temperature.append(float(row[7]))
                    battery.append(float(row[8]))

                #print row

        data.append([csv_name, median(red_clear[0:]), median(blue_clear[0:]), median(temperature[0:]), median(battery[0:])])

        output_file = path +  '/output.csv'

    with open(output_file,'w+') as f:
        writer = csv.writer(f)
        writer.writerows(data)

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


from token_constants import *
from parser_classes import *
from diff_lex import lex

def parse_float(tokens):
    assert type(tokens) == list
    assert len(tokens) == 1
    assert type(tokens[0]) == float

    return Float(tokens[0])

def parse_var(tokens):
    assert type(tokens) == list
    assert len(tokens) == 1
    assert type(tokens[0]) == str
    assert tokens[0] not in CONSTANTS

    return Var(tokens[0])

def parse_binop(op, tokens):
    assert type(tokens) == list
    assert len(tokens) >= 3
    assert op in BINOPS
    assert op in tokens

    op_positions = []
    for i in range(len(tokens)):
        sym = tokens[i]
        if sym == op:
            op_positions.append(i)

    if op_positions == []:
        raise Exception(f"No Op {op} found in {tokens}.")

    for pos in op_positions:
        try:
            left_expr = parse(tokens[:pos])
            try:
                right_expr = parse(tokens[pos + 1:])
                return Binop(op, left_expr, right_expr)
            except Exception:
                pass
        except Exception:
            pass
    else:
        raise Exception(f"Failed to parse binop {tokens}.")

def parse_parens(tokens):
    assert type(tokens) == list
    assert len(tokens) >= 3
    assert tokens[0] == LEFT_PAREN
    assert tokens[-1] == RIGHT_PAREN

    return parse(tokens[1:-1])

def parse_unop(op, tokens):
    assert type(tokens) == list
    assert len(tokens) >= 2
    assert op in UNOPS
    assert tokens[0] == op

    result = parse(tokens[1:])
    return Unop(op, result)

def parse_const(tokens):
    assert type(tokens) == list
    assert len(tokens) == 1
    assert type(tokens[0]) == str
    assert tokens[0] in CONSTANTS

    return Const(tokens[0])

def parse_function(tokens):
    assert type(tokens) == list
    assert len(tokens) >= 2
    assert tokens[0] in FUNCTIONS

    result = parse(tokens[1:])
    return Function(tokens[0], result)

def parse(tokens):
    assert type(tokens) == list

    try:
        return parse_float(tokens)
    except Exception:
        try:
            return parse_const(tokens)
        except Exception:
            try:
                return parse_var(tokens)
            except Exception:
                try:
                    return parse_parens(tokens)
                except Exception:
                    try:
                        return parse_function(tokens)
                    except Exception:
                        try:
                            for op in UNOPS:
                                try:
                                    parse_unop(op, tokens)
                                except Exception:
                                    pass
                            raise Exception()
                        except Exception:
                            try:
                                for op in BINOPS:
                                    try:
                                        return parse_binop(op, tokens)
                                    except Exception:
                                        pass
                                raise Exception()
                            except Exception:
                                raise Exception(
                                    f"Failed to match in Parse: {tokens}")

def get_vars(expr):
    assert isinstance(expr, Expr)

    if type(expr) == Float:
        return []
    elif type(expr) == Const:
        return []
    elif type(expr) == Var:
        return [expr.get_var()]
    elif type(expr) == Function:
        return get_vars(expr.get_e())
    elif type(expr) == Binop:
        return get_vars(expr.get_l()) + get_vars(expr.get_r())
    elif type(expr) == Unop:
        return get_vars(expr.get_e())

def check_single_var(expr):
    assert isinstance(expr, Expr)

    return len(set(get_vars(expr))) <= 1

def parse_main(tokens):
    assert type(tokens) == list

    expr = parse(tokens)
    if check_single_var(expr):
        return expr
    raise RuntimeError(f"More than 1 variable: {expr}.")

def test():
    tokens = [LN, LEFT_PAREN, 4., ADD, 3., RIGHT_PAREN, SUB, LEFT_PAREN,
              2., ADD, 5., ADD, 3., ADD, 3., MUL, 2., RIGHT_PAREN]
    s = "x + (x * x) * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x"
    tokens = lex(s)
    print(parse(tokens))

if __name__ == "__main__":
    test()


import os
from collections import OrderedDict
import pandas as pd

from os2d.utils.logger import extract_map_value_from_os2d_log

if __name__ == "__main__":
    config_path = os.path.dirname(os.path.abspath(__file__))
    config_job_name = "eval_instre"
    log_path = os.path.abspath(os.path.join(config_path, "..", "output/eval_instre"))

    def get_result(eval_dataset,
                   folder_suffix="",
                   result_suffix="out.txt"):
        d = OrderedDict()
        if "instre-s1" in eval_dataset:
            d["eval.dataset_scales"] = "[700.0]"
            folder_dataset_pattern = "instre-s1"
        elif "instre-s2" in eval_dataset:
            d["eval.dataset_scales"] = "[600.0]"
            folder_dataset_pattern = "instre-s2"
        else:
            raise RuntimeError(f"Unknown dataset {eval_dataset}")

        log_folder = f"{config_job_name}"
        if folder_suffix:
            log_folder += "." + folder_suffix
        log_folder = os.path.join(log_path, log_folder)

        result_file = f"eval_{folder_dataset_pattern}_scale{d['eval.dataset_scales'][1:-1]}_{result_suffix}"
        result_file = os.path.join(log_folder, result_file)

        return extract_map_value_from_os2d_log(result_file, eval_dataset)

    datasets = ["instre-s1", "instre-s2"]
    init = "imageNetCaffe2"
    table = pd.DataFrame(columns=["arch", "init", "type", "trained"] + datasets)

    for arch in ["ResNet50", "ResNet101"]:
        for job_type in ["v1", "v2"]:
            for train_type in ["train", "init"]:
                d = {}
                d["arch"] = arch
                d["init"] = init
                d["type"] = job_type
                d["trained"] = train_type

                for dataset in datasets:
                    folder_suffix = f"{dataset}_{job_type}-{train_type}_{arch}_{init}"

                    eval_dataset = dataset + "-test"
                    val = get_result(eval_dataset, folder_suffix=folder_suffix)
                    d[dataset] = val

                table = table.append(d, ignore_index=True)

    print(table, sep='\n')


def evaluate_model(self,
                   model_name,
                   model,
                   df,
                   df_features,
                   output_folder=None,
                   le_map=None,
                   show_extra=True,
                   find_nearest_on_cols=False,
                   zscore_low=-2,
                   zscore_high=2):
    """
    model_name:
        The string key to give the dict

    model:
        Cluster model type; it must have '.labels_' as an attribute

    df:
        Dataframe object

    df_features:
        DataFrameTypes object; organizes feature types into groups.

    output_folder:
        Sub directory to put the pngs

    le_map:
        Dict of dataframe cols to LabelEncoders

    show_extra:
        Show extra information from all functions

    find_nearest_on_cols:
            Allows columns to be converted to actual values found within
            the dataset.
            Ex: Can't have .7 in a bool column convert's it to 1.

            False: Just apply to obj columns and bool columns

            True: Apply to all columns

    zscore_low/zscore_high:
        Defines how the threshold to remove data points when profiling the
        cluster.

    The main purpose of 'evaluate_model' is to display/save tables/plots
    accoiated with describing the model's 'findings'.
    """

    df = copy.deepcopy(df)

    # Create folder structure for png outputs
    if not output_folder:
        output_path = str(model).split("(", 1)[0] + "/" + model_name
    else:
        output_path = output_folder + "/" + model_name

    # ---
    # self.__visualize_clusters(model=model,
    #                           output_path=output_path,
    #                           model_name=model_name)

    # ---
    df["Cluster_Name"] = model.labels_
    numerical_features = df_features.numerical_features()
    clustered_dataframes, shrunken_labeled_df = \
        self.__create_cluster_sub_dfs(
            df=df, model=model, numerical_features=numerical_features,
            zscore_low=zscore_low, zscore_high=zscore_high)

    rows_count, cluster_profiles_df = self.__create_cluster_profiles(
        clustered_dataframes=clustered_dataframes,
        shrunken_df=shrunken_labeled_df,
        numerical_features=df_features.numerical_features(),
        le_map=le_map,
        output_path=output_path,
        show=show_extra,
        find_nearest_on_cols=find_nearest_on_cols)

    # Check to see how many data points were removed during the profiling
    # stage
    print("Orginal points in dataframe: ", df.shape[0])
    print("Total points in all modified clusters: ", rows_count)
    print("Shrank by: ", df.shape[0] - rows_count)

    # In case to many data points were removed
    if cluster_profiles_df.shape[0] == 0:
        print(
            "The applied Z-scores caused the cluster profiles "
            "to shrink to far for the model {0}!".format(
                model_name))

    # Display and save dataframe table
    else:
        display(cluster_profiles_df)
        self.__render_mpl_table(cluster_profiles_df, sub_dir=output_path,
                                filename="All_Clusters",
                                header_columns=0, col_width=2.0)

def __create_cluster_profiles(self,
                              clustered_dataframes,
                              shrunken_df,
                              numerical_features,
                              le_map,
                              output_path,
                              find_nearest_on_cols=False,
                              show=True):
    """
        Profile each clustered dataframe based off the given mean.
        Displays extra information in dataframe tables to be understand
        each cluster.

        find_nearest_on_cols:
            Allows columns to be converted to actual values found within
            the dataset.
            Ex: Can't have .7 in a bool column convert's it to 1.

            False: Just apply to obj columns and bool columns

            True: Apply to all columns
    """

    def find_nearest(numbers, target):
        """
            Find the closest fitting number to the target number
        """
        numbers = np.asarray(numbers)
        idx = (np.abs(numbers - target)).argmin()
        return numbers[idx]

    cluster_profiles_df = pd.DataFrame(columns=shrunken_df.columns).drop(
        'Cluster_Name', axis=1)
    rows_count = 0
    for cluster_identfier, cluster_dataframe in \
            clustered_dataframes.items():
        df = pd.DataFrame(columns=cluster_dataframe.columns)
        df = df.append(cluster_dataframe.mean(), ignore_index=True)
        df.index = [cluster_identfier]

        if cluster_dataframe.shape[0] <= 1:
            continue

        # Attempt to convert numbers found within the full set of data
        for col in cluster_dataframe.columns:
            if col not in numerical_features or find_nearest_on_cols:
                df[col] = find_nearest(numbers=shrunken_df[
                    col].value_counts().index.tolist(),
                    target=df[col].values[0])

        # Evaluate cluster dataframe by dataframe
        eval_df = pd.DataFrame(columns=cluster_dataframe.columns)
        eval_df = eval_df.append(
            cluster_dataframe.mean(), ignore_index=True)
        eval_df = eval_df.append(
            cluster_dataframe.min(), ignore_index=True)
        eval_df = eval_df.append(
            cluster_dataframe.median(),
            ignore_index=True)
        eval_df = eval_df.append(
            cluster_dataframe.max(), ignore_index=True)
        eval_df = eval_df.append(
            cluster_dataframe.std(), ignore_index=True)
        eval_df = eval_df.append(
            cluster_dataframe.var(), ignore_index=True)
        eval_df.index = ["Mean", "Min", "Median",
                         "Max", "Standard Deviation", "Variance"]

        if show:
            print("Total found in {0} is {1}".format(
                cluster_identfier, cluster_dataframe.shape[0]))
            self.__render_mpl_table(
                df,
                sub_dir=output_path,
                filename=cluster_identfier +
                "_Means_Rounded_To_Nearest_Real_Numbers",
                header_columns=0,
                col_width=4.0)

            self.__render_mpl_table(
                eval_df,
                sub_dir=output_path,
                filename=cluster_identfier +
                "_Eval_Df",
                header_columns=0,
                col_width=4.0)
            display(df)
            display(eval_df)
            self.__vertical_spacing(7)

        cluster_profiles_df = cluster_profiles_df.append(
            self.__decode_df(df, le_map))

        rows_count += cluster_dataframe.shape[0]

    return rows_count, cluster_profiles_df


'''
In this program, we calculate the Gamow peak for
all of the reactions in the PP branches and the
CNO cycle, as functions of energy.
'''
import numpy as np
import matplotlib.pyplot as plt
import scipy.constants as const

plt.rcParams['lines.linewidth'] = 1 	# Setting linewidth for lines in plots
plt.rcParams['font.size'] = 18

'''
Defining constants
'''
e = const.e 					# Elementary charge [C]
h = const.h 					# Planck constant [J Hz^-1]
pi = const.pi 					# Pi
eV = const.eV					# Electron volt [J]
m_u = const.m_u					# Atomic mass unit [kg]
k_B = const.k 					# Boltzmann constant [JK^-1]
epsilon_0 = const.epsilon_0		# Vacuum permittivity

def lambda_sigma(m_i, m_k, Z_i, Z_k, energy):
	'''
	Function to compute the Gamow peak for the reaction
	between	element_i and element_k, at a given energy.
	Masses are m_i and m_k, atomic number is Z_i and Z_k.
	'''
	T = 1.57e7 		# Sun temperature [K]

	m = m_i * m_k / (m_i + m_k)		# Reduced mass

	'''
	The two exponentials from the proportionality function (lambda) and
	the cross section of the tunneling effect (sigma).
	'''
	lmbda = np.exp(-energy / (k_B * T))
	sigma = np.exp(-np.sqrt(m / (2 * energy)) * Z_i * Z_k * e**2 * pi / (epsilon_0 * h))

	'''
	The Gamow peak is found by the product of the proportionality
	function and the the tunneling effect cross section.
	'''
	gamow = lmbda * sigma
	idx = np.where(gamow == np.max(gamow))[0][0]	# Extracting the (first) index where the gamow variable has its peak
	peak = gamow[idx]

	return gamow / peak								# Returning the normalized gamow curve

'''
Defining element masses in atomic mass units
'''
H1 = 1.007825 * m_u			# Hydrogen mass in kg
D2 = 2.014 * m_u			# Deuterium mass in kg
He3 = 3.016 * m_u 			# Helium-3 mass in kg
He4 = 4.0026 * m_u			# Helium-4 mass in kg
Be7 = 7.01693 * m_u			# Beryllium-7 mass in kg
Li7 = 7.016004 * m_u		# Lithium-7 mass in kg

C12 = 12. * m_u				# Carbon-12 mass in kg
C13 = 13.00336 * m_u		# Carbon-13 mass in kg
N13 = 13.00574 * m_u		# Nitrogen-13 mass in kg
N14 = 14.00307 * m_u		# Nitrogen-14 mass in kg
N15 = 15.00109 * m_u		# Nitrogen-15 mass in kg

'''
Defining atomic numbers
'''
Z_H = 1 		# Hydrogen
Z_He = 2 		# Helium
Z_Be = 4 		# Beryllium
Z_Li = 3 		# Lithium

Z_C = 6 		# Carbon
Z_N = 7 		# Nitrogen
Z_O = 8 		# Oxygen

'''
Defining energy interval [10^-17, 10^-13] J
'''
N = 1001
E = np.logspace(-17, -13, N)

rel_prob = np.zeros((10, N))	# Array to hold all of the probabilities

PP0_1 = lambda_sigma(H1, H1, Z_H, Z_H, E)
PP0_2 = lambda_sigma(D2, H1, Z_H, Z_H, E)

PP1 = lambda_sigma(He3, He3, Z_He, Z_He, E)

PP2_1 = lambda_sigma(He3, He4, Z_He, Z_He, E)
PP2_2 = lambda_sigma(Li7, H1, Z_Li, Z_H, E)

PP3 = lambda_sigma(Be7, H1, Z_Be, Z_H, E)

CNO_1 = lambda_sigma(C12, H1, Z_C, Z_H, E)
CNO_2 = lambda_sigma(C13, H1, Z_C, Z_H, E)
CNO_3 = lambda_sigma(N14, H1, Z_N, Z_H, E)
CNO_4 = lambda_sigma(N15, H1, Z_N, Z_H, E)

rel_prob[0, :] = PP0_1
rel_prob[1, :] = PP0_2
rel_prob[2, :] = PP1
rel_prob[3, :] = PP2_1
rel_prob[4, :] = PP2_2
rel_prob[5, :] = PP3
rel_prob[6, :] = CNO_1
rel_prob[7, :] = CNO_2
rel_prob[8, :] = CNO_3
rel_prob[9, :] = CNO_4

labels = ['pp', 'pd', '33', '34', "17'", '17', 'p12', 'p13', 'p14', 'p15']

plt.figure(figsize=(10.6, 6))

for i in range(10):

	plt.plot(E / (eV * 1e3), rel_prob[i, :], label=labels[i])	# Converting from Joules to keV

	idx = np.where(rel_prob[i, :] == np.max(rel_prob[i, :]))[0][0]
	peak = E[idx] / (eV * 1e3)

	print('Gamow peak for ' + labels[i] + f': {peak:.3f} keV')

plt.xlabel('Energy [keV]')
plt.ylabel('Normalized probability')
plt.xscale('log')
plt.xlim([1, 100])

plt.legend()
plt.savefig('figures/gamow.pdf')
plt.savefig('figures/gamow.png')
plt.show()


#!/usr/bin/env python3
from argparse import ArgumentParser
import sys
import subprocess
import json
import requests
import os
from libcxx_utils.github import api as api

class GithubActionsUtil(object):
  def __init__(self):
    self.parser = ArgumentParser(
        description='Utilities for using the github API')
    self.parser.add_argument('--token', required=('GITHUB_TOKEN' not in os.environ), default=os.environ.get('GITHUB_TOKEN'))
    self.subparsers = self.parser.add_subparsers(help='subcommand help')
    self._create_parser()
    self.args = self.parser.parse_args()
    self.api = api.GithubActionsAPI(self.args.token)

  def run_command(self):
    try:
      self.args.func()
    except requests.exceptions.HTTPError as err:
      print(err)
      sys.exit(1)

  def _api_call(self, func):
    result = func(**vars(self.args))
    result.raise_for_status()
    return result

  def _print_result(self, result):
    print(json.dumps(result, indent=2))

  def _add_default_arguments(self, parser):
    parser.add_argument('--owner', dest='owner', default=os.environ.get('GITHUB_OWNER'))
    parser.add_argument('--repo', dest='repo', default=os.environ.get('GITHUB_REPO'))

    return parser

  def _cmd_parser(self, name, func):
    p = self.subparsers.add_parser(name)
    p = self._add_default_arguments(p)
    p.set_defaults(func=func)
    return p

  def _create_parser(self):
    def add_download_args(parser_downloads):
      parser_downloads.add_argument('-o', '--os', dest='os',
                                    help='the OS to download',
                                    default='linux')
      parser_downloads.add_argument('-a', '--arch',
                                    help='the architecture to download',
                                    dest='arch', default='x64')
      return parser_downloads

    add_download_args(self._cmd_parser('downloads', self._downloads_cmd))
    add_download_args(self._cmd_parser('org-downloads', self._org_downloads_cmd))

    self._cmd_parser('registration-token', self._registration_token_cmd)
    self._cmd_parser('org-registration-token', self._org_registration_token_cmd)
    self._cmd_parser('workflows', self._workflows_cmd)
    self._cmd_parser('workflow', self._workflow_cmd).add_argument('workflow_id')
    self._cmd_parser('workflow-runs', self._workflow_runs_cmd)
    self._cmd_parser('workflow-run', self._workflow_run_cmd).add_argument('run_id')
    self._cmd_parser('runners', self._runners_cmd)
    self._cmd_parser('get-org', self._get_org)
    self._cmd_parser('list-creds', self._list_creds)
    self._cmd_parser('list-branches', self._list_branches_cmd)
    self._cmd_parser('runners', self._runners_cmd)

    event_parser = self._cmd_parser('push-event', self._push_event_cmd)
    event_parser.add_argument('payload')
    event_parser.add_argument('-f', '--file', action='store_true',
                                    help="Read the payload from a file")

  def _get_org(self):
    res = self._api_call(self.api.getOrg)
    self._print_result(res.json())

  def _list_branches_cmd(self):
    res = self._api_call(self.api.listBranches)
    self._print_result(res.json())

  def _list_creds(self):
    res = self._api_call(self.api.listCredentials)
    self._print_result(res.json())

  def _push_event_cmd(self):
    payload = args.payload
    if args.file:
      with open(payload, 'r') as f:
        payload = f.read()
    res = self._api_call(self.api.repoDispatchEvent,
                         payload=payload)
    if res.status_code == 204:
      print("Success!")
    else:
      print('%r' % res)
      sys.exit(1)

  def _download_commands_base(self, endpoint):
    args = self.args
    res = self._api_call(endpoint)
    jres = res.json()
    for entry in jres:
      if entry['os'] == args.os and entry['architecture'] == args.arch:
        self._print_result(entry)
        return
    print('failed to find download for os=%s and arch=%s' % (
    args.os, args.arch))
    sys.exit(1)

  def _downloads_cmd(self):
    return self._download_commands_base(self.api.getActionsDownloads)

  def _org_downloads_cmd(self):
    return self._download_commands_base(self.api.getOrgActionsDownloads)

  def _registration_token_cmd(self):
    res = self._api_call(self.api.getCreationToken)
    self._print_result(res.json())

  def _org_registration_token_cmd(self):
    res = self._api_call(self.api.getCreationTokenForOrg)
    self._print_result(res.json())

  def _workflows_cmd(self):
    res = self._api_call(self.api.getWorkflows)
    self._print_result(res.json())

  def _workflow_cmd(self):
    res = self._api_call(self.api.getWorkflow)
    self._print_result(res.json())

  def _workflow_runs_cmd(self):
    res = self._api_call(self.api.getWorkflowRuns)
    self._print_result(res.json())

  def _workflow_run_cmd(self):
    res = self._api_call(self.api.getWorkflowRun)
    self._print_result(res.json())

  def _runners_cmd(self):
    res = self._api_call(self.api.getRunners)
    self._print_result(res.json())

def main():
  actions_cmd = GithubActionsUtil()
  actions_cmd.run_command()

if __name__ == '__main__':
  main()


"""
tests frua.base.code.git.py
"""
__author__ = 'David HEURTEVENT'
__copyright__ = 'David HEURTEVENT'
__license__ = 'MIT'

import pytest
import os

from frua.base.code.git import *
from frua.base.fs.dir import Dir

@pytest.fixture
def gitobj():
    gitobj = Git()
    return gitobj

@pytest.fixture
def ghobj():
    ghobj = GitHub()
    return ghobj

# test Git

def test_init(gitobj):
    assert isinstance(gitobj, Git)

def test_git_clone_https(gitobj):
    url = 'https://github.com/dheurtev/testrepo.git'
    outputdir = '/tmp/testrepo'
    #setup
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)
    #create
    branch = 'test'
    gitobj.clone_https(url, outputdir, branch, overwrite=True)
    #test
    assert os.path.exists(outputdir)
    #teadown
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)

# Test Github

def test_init_gh(ghobj):
    assert isinstance(ghobj, GitHub)

def test_github_repo_name(ghobj):
    name = 'dheurtev'
    ghobj.user = name
    assert ghobj._user == name

def test_github_repo(ghobj):
    repo = 'testrepo'
    ghobj.repo = repo
    assert ghobj._repo == repo

def test_github_branch(ghobj):
    branch = 'test'
    ghobj.branch = branch
    assert ghobj._branch == branch

def test_github_release(ghobj):
    version = '1.0.0'
    ghobj.release = version
    assert ghobj._release == version

def test_github_repo_url(ghobj):
    name = 'dheurtev'
    ghobj.user = name
    repo = 'testrepo'
    ghobj.repo = repo
    url = ghobj.repo_url
    assert url == 'https://github.com/dheurtev/testrepo.git'

def test_github_repo_branch_zip_url(ghobj):
    name = 'dheurtev'
    ghobj.user = name
    repo = 'testrepo'
    ghobj.repo = repo
    branch = 'test'
    ghobj.branch = branch
    url = ghobj.repo_branch_zip_url
    assert url == 'https://github.com/dheurtev/testrepo/archive/test.zip'

def test_github_clone_https(ghobj):
    outputdir = '/tmp/testrepo'
    #setup
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)
    #create
    name = 'dheurtev'
    ghobj.user = name
    repo = 'testrepo'
    ghobj.repo = repo
    branch = 'test'
    ghobj.branch = branch
    ghobj.clone_https_gh(outputdir=outputdir, overwrite=True)
    #test
    assert os.path.exists(outputdir)
    #teadown
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)

def test_github_clone_from_zip(ghobj):
    outputdir = '/tmp/testrepo'
    #setup
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)
    #create
    name = 'dheurtev'
    ghobj.user = name
    repo = 'testrepo'
    ghobj.repo = repo
    branch = 'test'
    ghobj.branch = branch
    ghobj.clone_from_zip(outputdir=outputdir, overwrite=True)
    #test
    assert os.path.exists(outputdir)
    #teadown
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)

def test_github_clone_release_from_zip(ghobj):
    outputdir = '/tmp/testrepo'
    #setup
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)
    #create
    name = 'dheurtev'
    ghobj.user = name
    repo = 'cryptopyutils'
    ghobj.repo = repo
    release = '0.1.0'
    ghobj.release = release
    ghobj.clone_release_from_zip(outputdir=outputdir, overwrite=True)
    #test
    assert os.path.exists(outputdir)
    #teadown
    if os.path.exists(outputdir):
        Dir().wipe(outputdir)