from matplotlib.pyplot import imsave
from sklearn.cluster import KMeans
from matplotlib.image import imread
import matplotlib.pyplot as plt
import numpy as np
import sys
import os
import json
from skimage import io
from skimage.color import rgba2rgb

def kmeansCompressionOneway(inputImagePath, userDir, imageName):

    try:
        output_image_name = 'kmeans_compressed_'+imageName
        outputPath = os.path.join(userDir, output_image_name)
        matimg = io.imread(inputImagePath)
        if matimg.shape[2] == 4:
            matimg = rgba2rgb(matimg)
        io.imsave('contrast.png', matimg)
        print('saved')
        matimg = io.imread('contrast.png')

        # reshape into 2 dimensions
        height, width = matimg.shape[0], matimg.shape[1]
        img_in = matimg.reshape(height*width, 3)

        # K-Means model
        model = KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
                       n_clusters=16, n_init=10, n_jobs=None, precompute_distances='auto',
                       random_state=None, tol=0.0001, verbose=0)

        model.fit(img_in)

        # extracting all centroids
        centers = np.asarray(model.cluster_centers_, dtype=np.uint8)

        # extracting which cluster each pixel belongs to
        labels = np.asarray(model.labels_, dtype=np.uint8)
        labels = np.reshape(labels, (height, width))

        # reconstructing image
        comp_img = np.zeros((height, width, 3), dtype=np.uint8)
        for i in range(height):
            for j in range(width):
                # assinging every pixel the rgb color of their label's center
                comp_img[i, j, :] = centers[labels[i, j], :]

        io.imsave(outputPath, comp_img)
        return(json.dumps({'success': 'true', 'output_image': output_image_name}))
    except:
        return(json.dumps({'success': 'false'}))


import configparser
import csv
import tensorflow as tf
import glob
from tensorflow.io import decode_png
import tensorflow.keras.backend as K
import numpy as np

config = configparser.RawConfigParser()
config.read('configuration.txt')

PATCH_SIZE = (
    int(config.get('data attributes', 'patch_height')),
    int(config.get('data attributes', 'patch_width'))
)

def load_testset(filepath, batch_size):
    # This works with arrays as well
    dataset = tf.data.TFRecordDataset(glob.glob(filepath))

    # Maps the parser on every filepath in the array. You can set the number of parallel loaders here
    return dataset.map(_parse_function, num_parallel_calls=8) \
        .map(normalize, num_parallel_calls=8) \
        .batch(batch_size, drop_remainder=True) \
        .repeat()

def load_trainset(filepath, batch_size, N_imgs):
    dataset = tf.data.TFRecordDataset(glob.glob(filepath))

    # Set the number of datapoints you want to load and shuffle
    # do not reshuffle each iteration
    print('total samples:')
    print(N_imgs)
    dataset = dataset.shuffle(250000)

    # Maps the parser on every filepath in the array. You can set the number of parallel loaders here
    dataset = dataset.map(_parse_function, num_parallel_calls=8) \
        .map(normalize, num_parallel_calls=8)

    #split in testing and training
    test_data = dataset.take(int(N_imgs / 10)) \
        .batch(batch_size, drop_remainder=True) \
        .repeat()

    train_data = dataset.skip(int(N_imgs / 10)) \
        .batch(batch_size, drop_remainder=True) \
        .repeat()

    return test_data, train_data

def load_images_labels(filepath, batch_size, N_imgs):
    _, train_data = load_trainset(filepath, batch_size, N_imgs)

    # Create an iterator
    iterator = train_data.make_one_shot_iterator()

    # Create your tf representation of the iterator
    image, label = iterator.get_next()

    return image, label

# ================================ HELPER =======================================
def normalize(image, label):
    # image = (image - MEAN) / STD
    image = tf.cast(image, tf.float32) / 255. * 6. - 3.    # normalize to [-3, 3]
    label = tf.cast(label, tf.float32) / 255.         # label from 0 - 1

    foreground = tf.cast(label > 0.2, tf.float32)
    bkgrnd = 1 - foreground
    label = tf.concat([bkgrnd, foreground], 0)

    assert_max_label = tf.Assert(tf.less_equal(tf.reduce_max(label), 1.), [label])
    assert_max_image = tf.Assert(tf.less_equal(tf.reduce_max(image), 3.), [image])
    assert_min_label = tf.Assert(tf.greater_equal(tf.reduce_min(label), 0.), [label])
    assert_min_image = tf.Assert(tf.greater_equal(tf.reduce_min(image), -3.), [image])
    with tf.control_dependencies([
        assert_max_label,
        assert_max_image,
        assert_min_label,
        assert_min_image
    ]):
        return image, label

def _parse_function(proto):
    global PATCH_SIZE
    # define your tfrecord again. Remember that you saved your image as a string.
    keys_to_features = {'image': tf.FixedLenFeature([], tf.string),
                        'label': tf.FixedLenFeature([], tf.string)}

    # Load one example
    parsed_features = tf.parse_single_example(proto, keys_to_features)

    # Turn your saved image string into an array
    image = decode_png(parsed_features['image'])
    label = decode_png(parsed_features['label'])

    # Bring your picture back in shape
    image = tf.reshape(image, [1, PATCH_SIZE[0], PATCH_SIZE[1]])
    label = tf.reshape(label, [1, PATCH_SIZE[0], PATCH_SIZE[1]])

    return image, label


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Commands to carry out all the actions for uBitTool control.

This module internal functions can read any memory location from the micro:bit
via PyOCD, which uses the CMSIS interface provided by DAPLink.

The exposed function for the command line and GUI interfaces can safely read
areas of Flash (full flash, MicroPython, Python code) and UICR (to read the
customer data), and format the output into Intel Hex, a nicely decoded string
format, or human readable text (for the Python code).
"""
import os
import sys
import tempfile
import webbrowser
from io import StringIO
from threading import Timer
from difflib import HtmlDiff
from traceback import format_exc

from intelhex import IntelHex
from uflash import extract_script

from ubittool import programmer

#
# Data format conversions
#
def _bytes_to_intel_hex(data, offset=0x0000):
    """Take a list of bytes and return a string in the Intel Hex format.

    :param data: List of integers, each representing a single byte.
    :param offset: Start address offset.
    :return: A string with the Intel Hex encoded data.
    """
    i_hex = IntelHex()
    i_hex.frombytes(data, offset)

    fake_file = StringIO()
    try:
        i_hex.tofile(fake_file, format="hex")
    except IOError as e:
        sys.stderr.write("ERROR: File write: {}\n{}".format(fake_file, str(e)))
        return

    intel_hex_str = fake_file.getvalue()
    fake_file.close()
    return intel_hex_str

def _bytes_to_pretty_hex(data, offset=0x0000):
    """Convert a list of bytes to a nicely formatted ASCII decoded hex string.

    :param data: List of integers, each representing a single byte.
    :param offset: Start address offset.
    :return: A string with the formatted hex data.
    """
    i_hex = IntelHex()
    i_hex.frombytes(data, offset)

    fake_file = StringIO()
    try:
        i_hex.dump(tofile=fake_file, width=16, withpadding=False)
    except IOError as e:
        sys.stderr.write("ERROR: File write: {}\n{}".format(fake_file, str(e)))
        return

    pretty_hex_str = fake_file.getvalue()
    fake_file.close()
    return pretty_hex_str

#
# Reading data commands
#
def read_flash_hex(decode_hex=False, **kwargs):
    """Read data from the flash memory and return as a hex string.

    Read as a number of bytes of the micro:bit flash from the given address.
    Can return it in Intel Hex format or a pretty formatted and decoded hex
    string.

    :param address: Integer indicating the start address to read.
    :param count: Integer indicating hoy many bytes to read.
    :param decode_hex: True selects nice decoded format, False selects Intel
            Hex format.
    :return: String with the hex formatted as indicated.
    """
    flash_data = programmer.read_flash(**kwargs)
    if decode_hex:
        return _bytes_to_pretty_hex(
            flash_data, offset=programmer.MICROBIT_FLASH_START
        )
    else:
        return _bytes_to_intel_hex(
            flash_data, offset=programmer.MICROBIT_FLASH_START
        )

def read_uicr_customer_hex(decode_hex=False):
    """Read the UICR Customer data.

    :return: String with the nicely decoded UIR Customer area data.
    """
    uicr_data = programmer.read_uicr(
        address=programmer.UICR_CUSTOMER_START,
        count=programmer.UICR_CUSTOMER_SIZE_BYTES,
    )
    if decode_hex:
        return _bytes_to_pretty_hex(
            uicr_data, offset=programmer.UICR_CUSTOMER_START
        )
    else:
        return _bytes_to_intel_hex(
            uicr_data, offset=programmer.UICR_CUSTOMER_START
        )

def read_micropython():
    """Read the MicroPython runtime from the micro:bit flash.

    :return: String with Intel Hex format for the MicroPython runtime.
    """
    flash_data = programmer.read_flash(
        address=programmer.MICROPYTHON_START,
        count=programmer.MICROPYTHON_END - programmer.MICROPYTHON_START,
        decode_hex=False,
    )
    return _bytes_to_intel_hex(flash_data, offset=programmer.MICROPYTHON_START)

def read_python_code():
    """Read the MicroPython user code from the micro:bit flash.

    :return: String with the MicroPython code.
    """
    flash_data = programmer.read_flash(
        address=programmer.PYTHON_CODE_START,
        count=(programmer.PYTHON_CODE_END - programmer.PYTHON_CODE_START),
    )
    py_code_hex = _bytes_to_intel_hex(
        flash_data, offset=programmer.PYTHON_CODE_START
    )
    try:
        python_code = extract_script(py_code_hex)
    except Exception:
        sys.stderr.write(format_exc())
        raise Exception("Could not decode the MicroPython code from flash")
    return python_code

#
# Hex comparison commands
#
def _open_temp_html(html_str):
    """Create a temporary html file, open it in a browser and delete it.

    :param html_str: String to write to the temporary file.
    """
    fd, path = tempfile.mkstemp(suffix=".html")
    try:
        with os.fdopen(fd, "w") as tmp:
            # do stuff with temp file
            tmp.write(html_str)
        webbrowser.open("file://{}".format(os.path.realpath(path)))
    finally:
        # It can take a bit of time for the browser to open the file,
        # so wait some time before deleting it
        t = Timer(30.0, lambda del_f: os.remove(del_f), args=[path])
        t.start()

def _gen_diff_html(from_title, from_lines, to_title, to_lines):
    """Compare two strings and string of HTML code with the output.

    :param from_title: Title of the left content to compare.
    :param from_lines: List of lines to compare.
    :param to_title: Title of the right content to compare.
    :param to_lines: List of lines to compare.
    :return: String of HTML code with the comparison output.
    """
    html_template = """<!DOCTYPE html>

        <meta charset="UTF-8">
        <title>Diff {from_title} vs. {to_title}</title>

Colors

Added

Changed

Deleted

Links

(f)irst change

(n)ext change

(t)op

Files

Left: {from_title}

Right: {to_title}

        {diff_table}

    """
    differ = HtmlDiff()
    filled_template = html_template.format(
        from_title=from_title,
        to_title=to_title,
        diff_table=differ.make_table(from_lines, to_lines),
    )
    return filled_template

def compare_full_flash_hex(hex_file_path):
    """Compare the micro:bit flash contents with a hex file.

    Opens the default browser to display an HTML page with the comparison
    output.

    :param hex_file_path: File path to the hex file to compare against.
    """
    with open(hex_file_path, encoding="utf-8") as f:
        file_hex_str = f.readlines()
    flash_hex_str = read_flash_hex(decode_hex=False)

    html_code = _gen_diff_html(
        "micro:bit", flash_hex_str.splitlines(), "Hex file", file_hex_str
    )
    _open_temp_html(html_code)

def compare_uicr_customer(hex_file_path):
    """Compare the micro:bit User UICR contents with a hex file.

    Opens the default browser to display an HTML page with the comparison
    output.

    :param hex_file_path: File path to the hex file to compare against.
    """
    with open(hex_file_path, encoding="utf-8") as f:
        file_hex_str = f.readlines()
    flash_hex_str = read_uicr_customer_hex(decode_hex=False)

    html_code = _gen_diff_html(
        "micro:bit", flash_hex_str.splitlines(), "Hex file", file_hex_str
    )
    _open_temp_html(html_code)


import re
import time
import random
import cPickle as pickle

import pandas as pd

import flask_cors

import flask as f
from flask import Flask, request
app = Flask(__name__)

#### PRELOADED GLOBALS
DF = pickle.load(open('cooccurrence.pickle'))

#### HELPER/UTIL FUNCTIONS

def debug(msg):
    if app.debug:
        print("DEBUG: {}".format(msg))

#### ROUTING FUNCTIONS

@app.route('/')
def hello_world():
    return "
Don't Panic!

Cooccurrence backend is running
"

@app.route('/cooccurrence/', methods=['GET', 'OPTIONS'])
@flask_cors.crossdomain(origin='*', headers="Content-Type")
def cooccurrence():
    """
    Expects a comma-separated list of tags as a GET parameter
    example.com/cooccurrence/?tags=man,woman,fun
    """
    try:
        tag_string = request.args['tags']
        tags = re.split(',\s*', tag_string)
    except KeyError:
        return "No 'tags' found in GET parameters", 400

    debug("TAGS: %s" % tags)
    submatrix = DF.ix[tags][tags]

    ## Do this if we want to normalize. I can't remember.
    if False:
        for tag in tags:
            submatrix[tag] /= float(submatrix[tag][tag])

    debug('\n%s' % submatrix)
    response = {'tags': tags,
                'data': submatrix.as_matrix().tolist(),
                }

    return f.jsonify(response)

#### MAIN

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


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

import os
import wsgiref.handlers
from xml.dom import minidom
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch

BASE_URL = "http://twiliotourist.appspot.com"

class TriviaPage(webapp.RequestHandler):
	"""
	Accepts value from the caller, fetches the factoid.
	"""

	def get(self):
		self.post()

	def _error(self, msg, redirecturl=None):
		# NEED TO ADDRESS
		# ERROR HANDLING
		template_values = {
			'msg': msg,
			'redirecturl': redirecturl
		}
		xml_response(self, 'error.xml', templatevalues)

	def post(self):
		factoid_num = self.request.get('Digits')
		# this just handles errors for no factoid, what if there is a wrong num?
		if not factoid_num:
			self._error("Wrong number factoid.", BASE_URL)
			return

		factoid_num = factoid_num.replace('#', '').replace('*', '')[:1]

		if not int(factoid_num) == 1 and not int(factoid_num) == 2:
			if int(factoid_num) % 2 == 0:
				factoid_num = '2'
			else:
				factoid_num = '1'

		factoid = '/scenes/scene' + factoid_num + '.wav'
		template_values = {'factoid' : factoid}

		try:
			xml_response(self, 'factoid.xml', template_values)
		except:
			self._error('Error parsing digits. Goodbye.')
		#@end snippet

def xml_response(handler, page, templatevalues=None):
	"""
	Renders an XML response using a provided template page and values
	"""
	path = os.path.join(os.path.dirname(__file__), page)
	handler.response.headers["Content-Type"] = "text/xml"
	handler.response.out.write(template.render(path, templatevalues))

class GatherPage(webapp.RequestHandler):
	"""
	Initial user greeting.  Plays the welcome audio file then reads the
	"enter zip code" message.  The Play and Say are wrapped in a Gather
	verb to collect the 5 digit zip code from the caller.  The Gather
	will post the results to /weather
	"""
	def get(self):
		self.post()

	def post(self):
		templatevalues = {
			'postprefix': BASE_URL,
		}
		xml_response(self, 'gather.xml', templatevalues)
# @end snippet

def main():
    application = webapp.WSGIApplication([('/', GatherPage),
							('/fact', TriviaPage)],
                                         debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()


import json
import os
import time
import uuid
from copy import deepcopy

import boto3
import dash
import dash_core_components as dcc
import dash_html_components as html
import requests
from dash.dependencies import Input, Output, State
from dotenv import load_dotenv, find_dotenv
from flask_caching import Cache

import dash_reusable_components as drc
from utils import STORAGE_PLACEHOLDER, GRAPH_PLACEHOLDER, \
    IMAGE_STRING_PLACEHOLDER
from utils import apply_filters, show_histogram, generate_lasso_mask, \
    apply_enhancements

DEBUG = True

app = dash.Dash(__name__)
server = app.server

if 'REDIS_URL' in os.environ:
    # Change caching to redis if hosted on dds
    cache_config = {
        'CACHE_TYPE': 'redis',
        'CACHE_REDIS_URL': os.environ["REDIS_URL"],
        'CACHE_THRESHOLD': 400
    }
# Local Conditions
else:
    # Make sure that your credentials are saved inside your .env file, as
    # given here:
    # https://devcenter.heroku.com/articles/bucketeer#environment-setup
    load_dotenv(find_dotenv())

    # Caching with filesystem when served locally
    cache_config = {
        'CACHE_TYPE': 'filesystem',
        'CACHE_DIR': 'cache-directory',
    }

# S3 Client. It is used to store user images. The bucket name
# is stored inside the utils file, the key is
# the session id generated by uuid
access_key_id = os.environ.get('ACCESS_KEY_ID')
secret_access_key = os.environ.get('SECRET_ACCESS_KEY')
bucket_name = os.environ.get('BUCKET_NAME')

s3 = boto3.client('s3',
                  endpoint_url="https://storage.googleapis.com",
                  aws_access_key_id=access_key_id,
                  aws_secret_access_key=secret_access_key)

# Caching
cache = Cache()
cache.init_app(app.server, config=cache_config)

def store_image_string(string, key_name):
    # Generate the POST attributes
    post = s3.generate_presigned_post(
        Bucket=bucket_name,
        Key=key_name
    )

    files = {"file": string}
    # Post the string file using requests
    response = requests.post(post["url"], data=post["fields"], files=files)
    return response

def serve_layout():
    # Generates a session ID
    session_id = str(uuid.uuid4())

    # Post the image to the right key, inside the bucket named after the
    # session ID
    res = store_image_string(IMAGE_STRING_PLACEHOLDER, session_id)
    print(res)

    # App Layout
    return html.Div([
        # Session ID
        html.Div(session_id, id='session-id', style={'display': 'none'}),

        # Banner display
        html.Div([
            html.H2(
                'Image Processing App',
                id='title'
            ),
            html.Img(
                src="https://s3-us-west-1.amazonaws.com/plotly-tutorials/logo/new-branding/dash-logo-by-plotly-stripe-inverted.png"
            )
        ],
            className="banner"
        ),

        # Body
        html.Div(className="container", children=[
            html.Div(className='row', children=[
                html.Div(className='five columns', children=[
                    drc.Card([
                        dcc.Upload(
                            id='upload-image',
                            children=[
                                'Drag and Drop or ',
                                html.A('Select an Image')
                            ],
                            style={
                                'width': '100%',
                                'height': '50px',
                                'lineHeight': '50px',
                                'borderWidth': '1px',
                                'borderStyle': 'dashed',
                                'borderRadius': '5px',
                                'textAlign': 'center'
                            },
                            accept='image/*'
                        ),

                        drc.NamedInlineRadioItems(
                            name='Selection Mode',
                            short='selection-mode',
                            options=[
                                {'label': ' Rectangular', 'value': 'select'},
                                {'label': ' Lasso', 'value': 'lasso'}
                            ],
                            val='select'
                        ),

                        drc.NamedInlineRadioItems(
                            name='Image Display Format',
                            short='encoding-format',
                            options=[
                                {'label': ' JPEG', 'value': 'jpeg'},
                                {'label': ' PNG', 'value': 'png'}
                            ],
                            val='jpeg'
                        ),
                    ]),

                    drc.Card([
                        drc.CustomDropdown(
                            id='dropdown-filters',
                            options=[
                                {'label': 'Blur', 'value': 'blur'},
                                {'label': 'Contour', 'value': 'contour'},
                                {'label': 'Detail', 'value': 'detail'},
                                {'label': 'Enhance Edge', 'value': 'edge_enhance'},
                                {'label': 'Enhance Edge (More)', 'value': 'edge_enhance_more'},
                                {'label': 'Emboss', 'value': 'emboss'},
                                {'label': 'Find Edges', 'value': 'find_edges'},
                                {'label': 'Sharpen', 'value': 'sharpen'},
                                {'label': 'Smooth', 'value': 'smooth'},
                                {'label': 'Smooth (More)',
                                 'value': 'smooth_more'}
                            ],
                            searchable=False,
                            placeholder='Basic Filter...'
                        ),

                        drc.CustomDropdown(
                            id='dropdown-enhance',
                            options=[
                                {'label': 'Brightness', 'value': 'brightness'},
                                {'label': 'Color Balance', 'value': 'color'},
                                {'label': 'Contrast', 'value': 'contrast'},
                                {'label': 'Sharpness', 'value': 'sharpness'}
                            ],
                            searchable=False,
                            placeholder='Enhance...'
                        ),

                        html.Div(
                            id='div-enhancement-factor',
                            style={
                                'display': 'none',
                                'margin': '25px 5px 30px 0px'
                            },
                            children=[
                                f"Enhancement Factor:",
                                html.Div(
                                    style={'margin-left': '5px'},
                                    children=dcc.Slider(
                                        id='slider-enhancement-factor',
                                        min=0,
                                        max=2,
                                        step=0.1,
                                        value=1,
                                        updatemode='drag'
                                    )
                                )
                            ]
                        ),

                        html.Button(
                            'Run Operation',
                            id='button-run-operation',
                            style={'margin-right': '10px', 'margin-top': '5px'}
                        ),

                        html.Button(
                            'Undo',
                            id='button-undo',
                            style={'margin-top': '5px'}
                        )
                    ]),

                    dcc.Graph(id='graph-histogram-colors',
                              config={'displayModeBar': False})
                ]),

                html.Div(
                    className='seven columns',
                    style={'float': 'right'},
                    children=[
                        # The Interactive Image Div contains the dcc Graph
                        # showing the image, as well as the hidden div storing
                        # the true image
                        html.Div(id='div-interactive-image', children=[
                            GRAPH_PLACEHOLDER,
                            html.Div(
                                id='div-storage',
                                children=STORAGE_PLACEHOLDER,
                                style={'display': 'none'}
                            )
                        ])
                    ]
                )
            ])
        ])
    ])

app.layout = serve_layout

# Helper functions for callbacks
def add_action_to_stack(action_stack,
                        operation,
                        operation_type,
                        selectedData):
    """
    Add new action to the action stack, in-place.
    :param action_stack: The stack of action that are applied to an image
    :param operation: The operation that is applied to the image
    :param operation_type: The type of the operation, which could be a filter,
    an enhancement, etc.
    :param selectedData: The JSON object that contains the zone selected by
    the user in which the operation is applied
    :return: None, appending is done in place
    """

    new_action = {
        'operation': operation,
        'type': operation_type,
        'selectedData': selectedData
    }

    action_stack.append(new_action)

def undo_last_action(n_clicks, storage):
    action_stack = storage['action_stack']

    if n_clicks is None:
        storage['undo_click_count'] = 0

    # If the stack isn't empty and the undo click count has changed
    elif len(action_stack) > 0 and n_clicks > storage['undo_click_count']:
        # Remove the last action on the stack
        action_stack.pop()

        # Update the undo click count
        storage['undo_click_count'] = n_clicks

    return storage

# Recursively retrieve the previous versions of the image by popping the
# action stack
@cache.memoize()
def apply_actions_on_image(session_id,
                           action_stack,
                           filename,
                           image_signature):
    action_stack = deepcopy(action_stack)

    # If we have arrived to the original image
    if len(action_stack) == 0:
        # Retrieve the url in which the image string is stored inside s3,
        # using the session ID

        url = s3.generate_presigned_url(
            ClientMethod='get_object',
            Params={
                'Bucket': bucket_name,
                'Key': session_id
            }
        )

        # A key replacement is required for URL pre-sign in gcp

        url = url.replace('AWSAccessKeyId', 'GoogleAccessId')

        response = requests.get(url)
        print(len(response.text))
        im_pil = drc.b64_to_pil(response.text)
        return im_pil

    # Pop out the last action
    last_action = action_stack.pop()
    # Apply all the previous action_stack, and gets the image PIL
    im_pil = apply_actions_on_image(
        session_id,
        action_stack,
        filename,
        image_signature
    )
    im_size = im_pil.size

    # Apply the rest of the action_stack
    operation = last_action['operation']
    selectedData = last_action['selectedData']
    type = last_action['type']

    # Select using Lasso
    if selectedData and 'lassoPoints' in selectedData:
        selection_mode = 'lasso'
        selection_zone = generate_lasso_mask(im_pil, selectedData)
    # Select using rectangular box
    elif selectedData and 'range' in selectedData:
        selection_mode = 'select'
        lower, upper = map(int, selectedData['range']['y'])
        left, right = map(int, selectedData['range']['x'])
        # Adjust height difference
        height = im_size[1]
        upper = height - upper
        lower = height - lower
        selection_zone = (left, upper, right, lower)
    # Select the whole image
    else:
        selection_mode = 'select'
        selection_zone = (0, 0) + im_size

    # Apply the filters
    if type == 'filter':
        apply_filters(
            image=im_pil,
            zone=selection_zone,
            filter=operation,
            mode=selection_mode
        )
    elif type == 'enhance':
        enhancement = operation['enhancement']
        factor = operation['enhancement_factor']

        apply_enhancements(
            image=im_pil,
            zone=selection_zone,
            enhancement=enhancement,
            enhancement_factor=factor,
            mode=selection_mode
        )

    return im_pil

@app.callback(Output('interactive-image', 'figure'),
              [Input('radio-selection-mode', 'value')],
              [State('interactive-image', 'figure')])
def update_selection_mode(selection_mode, figure):
    if figure:
        figure['layout']['dragmode'] = selection_mode
    return figure

@app.callback(Output('graph-histogram-colors', 'figure'),
              [Input('interactive-image', 'figure')])
def update_histogram(figure):
    # Retrieve the image stored inside the figure
    enc_str = figure['layout']['images'][0]['source'].split(';base64,')[-1]
    # Creates the PIL Image object from the b64 png encoding
    im_pil = drc.b64_to_pil(string=enc_str)

    return show_histogram(im_pil)

@app.callback(Output('div-interactive-image', 'children'),
              [Input('upload-image', 'contents'),
               Input('button-undo', 'n_clicks'),
               Input('button-run-operation', 'n_clicks')],
              [State('interactive-image', 'selectedData'),
               State('dropdown-filters', 'value'),
               State('dropdown-enhance', 'value'),
               State('slider-enhancement-factor', 'value'),
               State('upload-image', 'filename'),
               State('radio-selection-mode', 'value'),
               State('radio-encoding-format', 'value'),
               State('div-storage', 'children'),
               State('session-id', 'children')])
def update_graph_interactive_image(content,
                                   undo_clicks,
                                   n_clicks,
                                   selectedData,
                                   filters,
                                   enhance,
                                   enhancement_factor,
                                   new_filename,
                                   dragmode,
                                   enc_format,
                                   storage,
                                   session_id):
    t_start = time.time()

    # Retrieve information saved in storage, which is a dict containing
    # information about the image and its action stack
    storage = json.loads(storage)
    filename = storage['filename']  # Filename is the name of the image file.
    image_signature = storage['image_signature']

    # Runs the undo function if the undo button was clicked. Storage stays
    # the same otherwise.
    storage = undo_last_action(undo_clicks, storage)

    # If a new file was uploaded (new file name changed)
    if new_filename and new_filename != filename:
        # Replace filename
        if DEBUG:
            print(filename, "replaced by", new_filename)

        # Update the storage dict
        storage['filename'] = new_filename

        # Parse the string and convert to pil
        string = content.split(';base64,')[-1]
        im_pil = drc.b64_to_pil(string)

        # Update the image signature, which is the first 200 b64 characters
        # of the string encoding
        storage['image_signature'] = string[:200]

        # Posts the image string into the Bucketeer Storage (which is hosted
        # on S3)
        store_image_string(string, session_id)
        if DEBUG:
            print(new_filename, "added to Bucketeer S3.")

        # Resets the action stack
        storage['action_stack'] = []

    # If an operation was applied (when the filename wasn't changed)
    else:
        # Add actions to the action stack (we have more than one if filters
        # and enhance are BOTH selected)
        if filters:
            type = 'filter'
            operation = filters
            add_action_to_stack(
                storage['action_stack'],
                operation,
                type,
                selectedData
            )

        if enhance:
            type = 'enhance'
            operation = {
                'enhancement': enhance,
                'enhancement_factor': enhancement_factor,
            }
            add_action_to_stack(
                storage['action_stack'],
                operation,
                type,
                selectedData
            )

        # Apply the required actions to the picture, using memoized function
        im_pil = apply_actions_on_image(
            session_id,
            storage['action_stack'],
            filename,
            image_signature
        )

    t_end = time.time()
    if DEBUG:
        print(f"Updated Image Storage in {t_end - t_start:.3f} sec")

    return [
        drc.InteractiveImagePIL(
            image_id='interactive-image',
            image=im_pil,
            enc_format=enc_format,
            display_mode='fixed',
            dragmode=dragmode,
            verbose=DEBUG
        ),

        html.Div(
            id='div-storage',
            children=json.dumps(storage),
            style={'display': 'none'}
        )
    ]

# Show/Hide Callbacks
@app.callback(Output('div-enhancement-factor', 'style'),
              [Input('dropdown-enhance', 'value')],
              [State('div-enhancement-factor', 'style')])
def show_slider_enhancement_factor(value, style):
    # If any enhancement is selected
    if value:
        style['display'] = 'block'
    else:
        style['display'] = 'none'

    return style

# Reset Callbacks
@app.callback(Output('dropdown-filters', 'value'),
              [Input('button-run-operation', 'n_clicks')])
def reset_dropdown_filters(_):
    return None

@app.callback(Output('dropdown-enhance', 'value'),
              [Input('button-run-operation', 'n_clicks')])
def reset_dropdown_enhance(_):
    return None

# Running the server
if __name__ == '__main__':
    app.run_server(debug=True)


from numpy.testing import assert_equal
from pandas.testing import assert_frame_equal
import pymc3 as pm

from ..utils import trace_to_dataframe, save_trace, load_trace

class TestUtils():

    @classmethod
    def setup_class(cls):
        with pm.Model():
            pm.Normal('a', 0, 1, shape=(2, 2))
            pm.Normal('b', 0, 1)
            cls.trace = pm.sample(1000, chains=2)

    def test_trace_to_dataframe(self):
        df_tc = trace_to_dataframe(self.trace, combined=True)
        df_fc = trace_to_dataframe(self.trace, combined=False)

        assert trace_to_dataframe(self.trace).shape == (2000, 5)
        assert trace_to_dataframe(self.trace, combined=False).shape == (1000, 10)

        for j, k in [(0, 0), (0, 1), (1, 0), (1, 1)]:
            assert_equal(self.trace['a'][:, j, k][:1000],
                         df_fc['a__{}_{}'.format(j, k)].iloc[:, 0].values)
            assert_equal(self.trace['a'][:, j, k][1000:],
                         df_fc['a__{}_{}'.format(j, k)].iloc[:, 1].values)

            assert_equal(self.trace['a'][:, j, k], df_tc['a__{}_{}'.format(j, k)].values)

        assert_equal(self.trace['b'], df_tc['b'])
        assert_equal(self.trace['b'], df_tc['b'])

        assert_equal(self.trace['b'][:1000], df_fc['b'].iloc[:, 0])
        assert_equal(self.trace['b'][1000:], df_fc['b'].iloc[:, 1])

    def test_save_and_load(self, tmpdir_factory):
        filename = str(tmpdir_factory.mktemp('traces').join('trace.gzip'))
        save_trace(self.trace, filename=filename)
        trl0 = load_trace(filename)
        tr = trace_to_dataframe(self.trace, combined=False)
        save_trace(tr, filename=filename)
        trl1 = load_trace(filename)

        assert_frame_equal(tr, trl0)
        assert_frame_equal(tr, trl1)
        assert_frame_equal(trl0, trl1)


import os

import pytest
from selenium import webdriver

from testcontainers.compose import DockerCompose
from testcontainers.core.exceptions import NoSuchPortExposed

def test_can_spawn_service_via_compose():
    compose = DockerCompose(os.path.dirname(__file__))

    try:
        compose.start()
        host = compose.get_service_host("hub", 4444)
        port = compose.get_service_port("hub", 4444)
        assert host == "0.0.0.0"
        assert port == "4444"
    finally:
        compose.stop()

def test_can_throw_exception_if_no_port_exposed():
    compose = DockerCompose(os.path.dirname(__file__))

    compose.start()
    with pytest.raises(NoSuchPortExposed):
        compose.get_service_host("hub", 5555)

    compose.stop()

def test_compose_wait_for_container_ready():
    compose = DockerCompose(os.path.dirname(__file__))
    with compose:
        compose.wait_for("http://localhost:4444/wd/hub")


"""add color to case statuses

Revision ID: a0a6f383a912
Revises: a44e24fa6b99
Create Date: 2020-11-24 19:17:50.275334

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'a0a6f383a912'
down_revision = 'a44e24fa6b99'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('case_statuses', sa.Column('color', sa.String(length=16), nullable=True))
    op.add_column('case_statuses', sa.Column('is_final', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('case_statuses', 'is_final')
    op.drop_column('case_statuses', 'color')
    # ### end Alembic commands ###


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

import sys
sys.path.append('../')

import os
import scipy.io as sio
import numpy as np
from HSMMIm3.HSMM import CHSMMModel

def getHSMM(path, num_th, flag):
    if flag == 3:
        HSMM = getHSMMIm3(path, num_th)

    return HSMM

def getHSMMIm3(path, num_th):

    filename = os.path.join(path, 'HSMM_a%d.mat' % num_th)
    a = sio.loadmat(filename)['a']

    filename = os.path.join(path, 'HSMM_b%d.mat' % num_th)
    b = sio.loadmat(filename)['b']

    filename = os.path.join(path, 'HSMM_BerlliB%d.mat' % num_th)
    B = sio.loadmat(filename)['B']

    filename = os.path.join(path, 'HSMM_BerlliC%d.mat' % num_th)
    C = sio.loadmat(filename)['C']

    filename = os.path.join(path, 'HSMM_Init%d.mat' % num_th)
    Init = sio.loadmat(filename)['Init']

    filename = os.path.join(path, 'HSMM_Trans%d.mat' % num_th)
    Trans = sio.loadmat(filename)['Trans']

    filename = os.path.join(path, 'HSMM_Theta%d.mat' % num_th)
    Theta = sio.loadmat(filename)['Theta']

    filename = os.path.join(path, 'HSMM_DurationProbs%d.mat' % num_th)
    DurationProbs = sio.loadmat(filename)['DurationProbs']

    hidden_num = Init.shape[0]
    item_num = Theta.shape[1]
    DurationMax = DurationProbs.shape[1]

    HSMM = CHSMMModel(hidden_num, item_num, DurationMax)
    HSMM.a = a
    HSMM.b = b
    HSMM.B = B
    HSMM.C = C
    HSMM.InitProbs = Init[:, 0]
    HSMM.TransProbs = Trans
    HSMM.Theta = Theta
    HSMM.DurationProbs = DurationProbs
    return HSMM


import json
from datetime import datetime
from typing import Tuple, List, Optional, Any
from ..entity import ticket_db
from ..entity.ticket import Ticket
from ..public import RECOMMEND_PRICE_COEF, RECOMMEND_REMAIN_COEF, RECOMMEND_TIME_COEF
from ..util import PriorityQueue

# 将一个航班插入数据库
def created_ticket(airline: str,
                   flight_number: str,
                   begin_place: str,
                   end_place: str,
                   begin_time: float,
                   end_time: float,
                   fly_by: str,
                   tickets: int,
                   price: float) -> Tuple[bool, str, Any]:

    # 构建航班节点
    ticket = Ticket(airline=airline, flight_number=flight_number,
                    begin_place=begin_place, end_place=end_place,
                    begin_time=begin_time, end_time=end_time,
                    fly_by=fly_by,
                    tickets=tickets, remain=tickets,
                    created_time=datetime.now().timestamp(), user_list='[]',
                    valid=1, appointment='[]', price=price)
    try:
        # 插入数据库
        res = ticket_db.insert(ticket)
    except ValueError as e:
        # 构建参数异常
        return False, "ValueError:" + str(e), None
    if res:
        return True, "航班添加成功", None
    else:
        return False, "航班添加失败", None

# 将一个航班从数据库中删除
def delete_ticket(ticket_id: int) -> Tuple[bool, str, Any]:

    # 从节点中删除航班
    res = ticket_db.delete('id', ticket_id)
    if res is False:
        return False, "航班删除失败", None
    else:
        return True, "航班删除成功", None

# 更新航班
def update(ticket: Ticket) -> Tuple[bool, str, Any]:
    if ticket.id is None:
        return False, "不知道要更新哪个节点", None
    else:
        if ticket_db.update_entirely('id', ticket.id, ticket):
            return True, "航班修改成功", None
        else:
            return False, "航班修改失败", None

# 增加改航班的预定用户
def append_appointment(ticket_id: int, username: str) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is not None:

        # 解析json
        appointment = json.loads(node.appointment)
        if isinstance(appointment, List):
            appointment.append(username)

            # 更新json
            node.appointment = json.dumps(appointment)
            return True, "添加预订成功", None
        else:
            return False, "预订字段格式异常", None
    else:
        return False, "ticket_id不存在", None

# 从航班弹出一个预定用户
def pop_appointment(ticket_id: int) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is not None:

        # 解析json
        appointment = json.loads(node.appointment)
        if isinstance(appointment, List):
            if len(appointment) == 0:
                return False, "the list is empty", None

            # 弹出节点
            username = appointment.pop(0)

            # 更新节点
            node.appointment = json.dumps(appointment)
            return True, '', username
        else:
            return False, "预订字段格式异常", None
    else:
        return False, "ticket_id不存在", None

# 将某用户取消该航班的预定
def cancel_appointment(ticket_id: int, username: str) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is not None:

        # 解析json
        appointment = json.loads(node.appointment)
        if isinstance(appointment, List):
            if len(appointment) == 0:
                return False, "该用户未预定此机票", None
            try:

                # 删除用户
                appointment.remove(username)

                # 更新json
                node.appointment = json.dumps(appointment)
            except ValueError as e:
                return False, "Value Error: " + str(e), None
            return True, "取消预定成功", None
        else:
            return False, "appointment字段格式异常", None
    else:
        return False, "ticket_id不存在", None

# 获取所有航班信息
def list_ticket() -> Tuple[bool, str, Any]:
    return True, '获取列表成功', ticket_db.list_all_json()

# 查询航班
def search_one(ticket_id: int) -> Tuple[bool, str, Optional[Ticket]]:
    return True, '', ticket_db.search_one('id', ticket_id)

# 推荐传入航班的相似航班
def recommend_ticket(ticket_id: int) -> Tuple[bool, str, List[Ticket]]:
    node = ticket_db.search_one('id', ticket_id)
    if node is None:
        return True, '', []
    begin_place_list = ticket_db.search('begin_place', node.begin_place)
    end_place_list = ticket_db.search('end_place', node.end_place)

    # 查询两个集合的交，并过滤余票和有效性
    return True, '', [val for val in begin_place_list if val in end_place_list and val.remain > 0 and val.valid == 1]

# 航班增加一个用户
def ticket_add_user(ticket_id: int, username: str) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is None:
        return False, "ticket_id不存在", None

    # 解析json
    user_list = json.loads(node.user_list)
    if isinstance(user_list, List):

        # 增加用户
        user_list.append(username)

        # 更新json
        node.user_list = json.dumps(user_list)
        return True, "机票增加客户成功", None
    else:
        return False, 'user_list字段格式异常', None

# 航班减少一张余票
def buy_one_ticket(ticket_id: int) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is None:
        return False, "the ticket id is not exist", None
    elif node.remain >= 1:
        node.remain = node.remain - 1
        return True, "购买机票成功", None
    else:
        return False, "机票为空，购买失败", None

# 航班删除一个用户，并增加余票
def ticket_remove_user(ticket_id: int, username: str) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)

    # 解析json
    user_list = json.loads(node.user_list)
    if node is None:
        return False, "ticket_id不存在", None
    elif node.remain >= node.tickets:
        return False, "余票不能超过总票数", None
    elif user_list is None \
            or (not isinstance(user_list, List)):
        return False, "user_list字段格式异常", None
    else:
        try:

            # 删除用户
            user_list.remove(username)

            # 更新json
            node.user_list = json.dumps(user_list)
        except ValueError:
            return False, "该用户尚未购买此机票", None
        node.remain = node.remain + 1
        return True, '机票删除用户成功', None

# 检查航班的预定情况，并弹出一个预定用户
def check_appointment(ticket_id: int) -> Tuple[bool, str, Any]:
    node = ticket_db.search_one('id', ticket_id)
    if node is None:
        return False, "ticket_id不存在", None
    elif node.appointment is None:
        return True, "无剩余预定", None

    # 解析json
    appointment_list = json.loads(node.appointment)
    if appointment_list is None or len(appointment_list) == 0:
        return True, "无剩余预定", None
    elif not isinstance(appointment_list, List):
        return False, "appointment字段格式异常", None
    elif len(appointment_list):
        return True, "无剩余预定", None
    else:
        # 返回预定用户
        return pop_appointment(ticket_id)

# 查询
def search_by_column(column_name: str, column_value: Any) -> Tuple[bool, str, List[Ticket]]:
    tickets = ticket_db.search(column_name, column_value)
    res = []
    for ticket in tickets:
        res.append(ticket)
    return True, '', res

# 查询低于某价格的航班
def search_under_price(price: float) -> Tuple[bool, str, List[Ticket]]:
    tickets = ticket_db.list_all()
    res = []
    for ticket in tickets:
        if ticket.price <= price:
            res.append(ticket)
    return True, '', res

# 查询有余票的航班
def search_has_remain(has_remain: bool) -> Tuple[bool, str, List[Ticket]]:
    tickets = ticket_db.list_all()
    res = []
    if has_remain:
        for ticket in tickets:
            if ticket.remain > 0:
                res.append(ticket)
        return True, '', res
    else:
        return True, '', tickets

# 给定出发地和目的地，推荐航班
def ticket_recommend_begin_end(begin_place: str, end_place: str) -> Tuple[bool, str, Any]:

    # 获取所有航班
    ticket_list = ticket_db.list_all()

    # 初始化变量
    index = 0
    place_dict = {}
    place_list = []

    # 将城市转为数字编码
    for ticket in ticket_list:
        if ticket.begin_place not in place_dict:
            place_dict[ticket.begin_place] = index
            place_list.append(ticket.begin_place)
            index = index + 1
        if ticket.end_place not in place_dict:
            place_dict[ticket.end_place] = index
            place_list.append(ticket.end_place)
            index = index + 1

    # 验证推荐路径的起点终点是否在图中
    if begin_place not in place_dict or end_place not in place_dict:
        return True, '', '[]'

    # 将起点终点转为数字编码
    begin_place = place_dict[begin_place]
    end_place = place_dict[end_place]

    # 初始化图
    graph: List[List[Edge]] = []
    dist = []
    vis = []
    path = []
    for i in range(index):
        dist.append(float('inf'))
        graph.append([])
        vis.append(False)
        path.append({'from': -1, 'ticket_id': None})

    # 计算边权并建图
    for ticket in ticket_list:
        cost = \
            RECOMMEND_PRICE_COEF * ticket.price + \
            RECOMMEND_TIME_COEF * (ticket.end_time - ticket.begin_time) + \
            RECOMMEND_REMAIN_COEF * ticket.remain
        edge = Edge(place_dict[ticket.end_place], cost, ticket.id, ticket.begin_time, ticket.end_time)
        graph[place_dict[ticket.begin_place]].append(edge)

    dist[begin_place] = 0
    pq = PriorityQueue()
    pq.push(Edge(begin_place, 0, None, 0, 0))
    while not pq.is_empty():

        # 获取边权最小的边
        u = pq.pop()
        v = u.to
        if dist[v] < u.cost or vis[v]:
            continue
        vis[v] = True
        for edge in graph[v]:
            if not vis[edge.to] and dist[edge.to] > dist[v] + edge.cost and edge.begin_time > u.end_time:
                # 如果松弛更新最短边
                dist[edge.to] = dist[v] + edge.cost
                pq.push(Edge(edge.to, dist[edge.to], edge.ticket_id, begin_time=edge.begin_time, end_time=edge.end_time))
                # 存储路径
                path[edge.to] = {'from': v, 'ticket_id': edge.ticket_id}

    # 获取路径
    res = []
    cur = path[end_place]
    while cur['from'] != -1:
        res.append(cur['ticket_id'])
        cur = path[cur['from']]
    res_ticket = []
    for ticket_id in res:
        _, _, ticket = search_one(ticket_id)
        res_ticket.append(ticket.to_dict)
    res_ticket.reverse()
    return True, '', json.dumps(res_ticket)

# 图的一条有向边
class Edge:
    def __init__(self, to, cost, ticket_id, begin_time, end_time):
        self.to = to
        self.cost = cost
        self.ticket_id = ticket_id
        self.begin_time = begin_time
        self.end_time = end_time

    def __lt__(self, other):
        return self.cost < other.cost


import pytest
from htt.htt import HTT

def test_normal_response_frame():
    response = HTT.parse(
        "HTTP/1.1 200 OK\r\n"
        "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n"
        "Server: Apache/2.2.4 (Unix) PHP/5.2.0\r\n"
        "Content-Type: text/html\r\n"
        "Content-Length: 37\r\n"
        "\r\n"
        ""
        ""
        "Hello World"
        ""
        ""
    )

def test_normal_request_frame():

    request = HTT.parse(
        "GET / HTTP/1.1\r\n"
        "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n"
        "Server: Apache/2.2.4 (Unix) PHP/5.2.0\r\n"
        "Content-Type: text/html\r\n"
        "\r\n"
    )


import com
import math
import settings
import pages
import matplotlib
import matplotlib.animation as animation
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
matplotlib.use("TkAgg")

comms = com.Com(settings.COMPORT)
time_interval = 0.0
NUM_POINTS = 40

class Data():

    def __init__(self, SIZE):
        self.data = [0.5]*SIZE

    def add(self, val):
        self.data.pop(0)
        self.data.append(val)

    def get_data(self):
        return self.data

atrial = Data(NUM_POINTS)
ventrical = Data(NUM_POINTS)
time = Data(NUM_POINTS)

global r_val

r_val = 0

def animate(i):
    if settings.PD_Flag == True:
        global time_interval
        count = 0
        data = comms.getEgramValues()
        while not len(data) and count < 5:
            data = comms.getEgramValues()
            count += 1
        time_interval += 0.04 + 0.02*count

        a_val = round(data[0], 3)
        v_val = round(data[1], 3)
        r_val = data[2:]  # for testing

        atrial.add(a_val)
        ventrical.add(v_val)
        time.add(time_interval)

        a.clear()

        if settings.PLOT_ATR == True:
            a.plot(time.get_data(), atrial.get_data(), label="atrial")
        if settings.PLOT_VTR == True:
            a.plot(time.get_data(), ventrical.get_data(), label="ventrical")
        a.legend(loc='upper right')

f = Figure(figsize=(5, 5), dpi=100)
a = f.add_subplot(111)
axes = plt.axes()
axes.set_ylim([0, 1])


import os
import re
import sys
from policy.models import Policy, Application, API
from utils import utils
from threading import Lock

class PolicyEngine:

  POLICY_STORE_DIR = 'policystore'

  def __init__(self):
    parent_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
    self.policy_store_dir = os.path.join(parent_dir, self.POLICY_STORE_DIR)
    self.active_policies = []
    self.inactive_policies = []
    self.lock = Lock()

    if os.path.exists(self.policy_store_dir):
      for policy_file in os.listdir(self.policy_store_dir):
        if policy_file.endswith('.a.py'):
          full_path = os.path.join(self.policy_store_dir, policy_file)
          try:
            policy = Policy(full_path)
            self.active_policies.append(policy)
          except Exception as ex:
            utils.log("Error while loading policy '{0}': {1}".format(policy_file, str(ex)))
        elif policy_file.endswith('.i.py'):
          full_path = os.path.join(self.policy_store_dir, policy_file)
          try:
            policy = Policy(full_path)
            self.inactive_policies.append(policy)
          except Exception as ex:
            utils.log("Error while loading policy '{0}': {1}".format(policy_file, str(ex)))

    if self.active_policies:
      msg_suffix = '1 policy'
      if len(self.active_policies) > 1:
        msg_suffix = '{0} policies'.format(len(self.active_policies))
      utils.log("Initialized policy engine with {0}.".format(msg_suffix))
    else:
      utils.log("No active policies found.")

  def __run_policy_enforcement(self, name, version, dependencies, api_list, owner):
    if self.active_policies:
      immutable_api_list = []
      for api in api_list:
        immutable_api_list.append(API(api['name'], api['version']))
      app = Application(name, version, dependencies, immutable_api_list, owner)
      errors = []
      for policy in self.active_policies:
        policy.evaluate(app, errors)
      if errors:
        return False, '|'.join(errors)
    return True, None

  def __add_policy(self, name, content, active):
    regex = re.compile("^[a-zA-Z0-9_]+$")
    if not regex.match(name):
      return False, 'Invalid policy name: Only letters, digits and underscores are allowed'

    reg_name = re.compile(name + '\\.([ai])\\.py')
    for item in os.listdir(self.policy_store_dir):
      if reg_name.match(item):
       return False, 'Policy {0} already exists'.format(name)

    if active:
      file_path = os.path.join(self.policy_store_dir, name + '.a.py')
    else:
      file_path = os.path.join(self.policy_store_dir, name + '.i.py')

    file_handle = open(file_path, 'w')
    file_handle.write(content)
    file_handle.flush()
    file_handle.close()
    try:
      new_policy = Policy(file_path)
      if active:
        self.active_policies.append(new_policy)
      else:
        self.inactive_policies.append(new_policy)
      return True, None
    except Exception as ex:
      os.remove(file_path)
      return False, 'Error while parsing policy: {0}'.format(ex.message)

  def __remove_policy(self, name):
    reg_name = re.compile(name + '\\.([ai])\\.py')
    for item in os.listdir(self.policy_store_dir):
      match = reg_name.match(item)
      if match:
        break;

    if not match:
      return False, "Policy {0} is not found!".format(name)

    path = os.path.join(self.policy_store_dir, name + '.' + match.groups()[0] + '.py')
    os.remove(path)
    if match.groups()[0] == 'a':
      for p in self.active_policies:
        if p.name == name:
          self.active_policies.remove(p)
    else:
      for p in self.inactive_policies:
        if p.name == name:
          self.inactive_policies.remove(p)
    return True, "Policy removed successfully"

  # Enable an inactive policy, this policy must exist in the policy store
  def __enable_policy(self, name):
    path = os.path.join(self.policy_store_dir, name + '.i.py')
    if not os.path.exists(path):
      return False, "Error while enabling policy: {0}, no such inactive policy!".format(name)

    for p in self.inactive_policies:
      if p.name == name:
        try:
          os.rename(path, os.path.join(self.policy_store_dir, name + '.a.py'))
        except Exception as ex:
          return False, "Erro while enabling policy: {0}, {1}".format(name, ex.message)

        self.active_policies.append(p)
        self.inactive_policies.remove(p)
        return True, None
    # This should never happen:
    return False, "Error while enabling policy: {0}, policy engine is not consistent!".format(name)

  # Disable an active policy, this policy must exist in the policy store
  def __disable_policy(self, name):
    path = os.path.join(self.policy_store_dir, name + '.a.py')
    if not os.path.exists(path):
      return False, "Error while disabling policy: {0}, no such active policy!".format(name)

    for p in self.active_policies:
      if p.name == name:
        try:
          os.rename(path, os.path.join(self.policy_store_dir, name + '.i.py'))
        except Exception as ex:
          return False, "Error while disabling policy: {0}, {1}".format(name, ex.message)
        self.inactive_policies.append(p)
        self.active_policies.remove(p)
        return True, None

    #This should never happen:
    return False, "Error while disabling policy: {0}, policy engine is not consistent!".format(name)

  def __list_policy(self, status):
    if status == 'active':
      return [policy.name for policy in self.active_policies]

    if status == "inactive":
      return [policy.name  for policy in self.inactive_policies]

    if status == "all":
      return [policy.name for policy in self.active_policies + self.inactive_policies]

  def __info_policy(self, name):
    for p in self.active_policies + self.inactive_policies:
      if p.name == name:
        return (p.name, p.source_code)
    return None

  # Wrapper methods, added mutex
  def run_policy_enforcement(self, name, version, dependencies, api_list, owner):
    self.lock.acquire()
    res = self.__run_policy_enforcement(name, version, dependencies, api_list, owner)
    self.lock.release()
    return res

  def add_policy(self, name, content, active):
    self.lock.acquire()
    res = self.__add_policy(name, content, active)
    self.lock.release()
    return res

  def remove_policy(self, name):
    self.lock.acquire()
    res = self.__remove_policy(name)
    self.lock.release()
    return res

  def enable_policy(self, name):
    self.lock.acquire()
    res = self.__enable_policy(name)
    self.lock.release()
    return res

  def disable_policy(self, name):
    self.lock.acquire()
    res = self.__disable_policy(name)
    self.lock.release()
    return res

  def list_policy(self, status):
    self.lock.acquire()
    res = self.__list_policy(status)
    self.lock.release()
    utils.log(res[0])
    return res

  def info_policy(self, name):
    self.lock.acquire()
    res = self.__info_policy(name)
    self.lock.release()
    return res


from collections import defaultdict
import random

mydeck = [r+s for r in '23456789TJQKA' for s in 'SHDC']

hand_names = {
    0 : "high card",
    1 : "2 of a kind",
    2 : "2-pair",
    3 : "3 of a kind",
    4 : "straight",
    5 : "flush",
    6 : "full house",
    7 : "four of a kind",
    8 : "straight flush",
}

def swap(deck,i,j):
    deck[i], deck[j] = deck[j], deck[i]

def shuffle(deck):
    n = len(deck)
    for i in range(n-1):
        swap(deck,i,random.randint(i,n-1))

def deal(numhands, n=5, deck=mydeck):
    "deal out 'numhands' hands of 'n' cards each after shuffling"
    #run python's shuffle algo
    #random.shuffle(mydeck)

    #run custom shuffling algo
    shuffle(deck)

    #prepare hands of 'n' cards each
    return [deck[n*i:n*(i+1)] for i in range(0,numhands)]

def factorial(n):
    if(n<=0):
        return 1
    else:
        return factorial(n-1) * n

def test_shuffler(shuffler, deck, num_iter):
    "Tests if the shuffler shuffles good"
    counts = defaultdict(int)
    for _ in range(num_iter):
        input = list(deck)
        shuffle(input)
        counts[''.join(input)] += 1

    expected_count = num_iter * (1/factorial(len(deck)))

    isShufflerGood = all( (0.9 <= counts[e]/expected_count <= 1.1) for e in counts )

    name = shuffler.__name__

    print("%s(%s) %s" % (name,deck,"good" if isShufflerGood else "bad"))

    for key,value in counts.items():
        print("%s: attained=>%7.4f%% expected=>%7.4f%%" % (key, (value/num_iter)*100, (expected_count/num_iter)*100))

    return isShufflerGood

def straight(ranks):
    "Return True if the ordered ranks form a 5-card straight."
    assert(ranks)

    return (max(ranks)-min(ranks)==4 and len(set(ranks))==5)

def flush(hand):
    "Return True if all the cards have the same suit."
    assert(hand)

    suits = [s for r,s in hand]
    return (len(set(suits))==1)

def kind(n, ranks):
    """Return the first rank that this hand has exactly n of.
    Return None if there is no n-of-a-kind in the hand."""
    assert(ranks)

    counts = defaultdict(int)
    for e in ranks:
        counts[e] += 1

    for k,v in counts.items():
        if v == n:
            return k

    return None

def two_pair(ranks):
    """If there are two pair, return the two ranks as a
    tuple: (highest, lowest); otherwise return None."""
    assert(ranks)

    temp = set(ranks)
    if len(temp) != 3:
        return None

    h = None
    l = None
    for e in temp:
        if ranks.count(e) == 2:
            if h==None:
                h = e
            else:
                l = e

    if h==None and l==None:
        return None
    else:
        return (h,l)

def card_ranks(cards):
    "Return a list of the ranks, sorted with higher first."

    ranks = ['--23456789TJQKA'.index(r[0]) for r in cards]
    ranks.sort(reverse=True)

    #to handle: ace can rank either above K or below 2
    if(ranks == [14,5,4,3,2]):
        ranks = [5,4,3,2,1]

    return ranks

def hand_rank(hand):
    "Returns the rank of the hand in the form of a tuple"

    ranks = card_ranks(hand)

    if straight(ranks) and flush(hand):            # straight flush
        return (8, max(ranks))
    elif kind(4, ranks):                           # 4 of a kind
        return (7, kind(4, ranks), kind(1, ranks))
    elif kind(3, ranks) and kind(2, ranks):        # full house
        return (6, kind(3,ranks), kind(2,ranks))
    elif flush(hand):                              # flush
        return (5, ranks)
    elif straight(ranks):                          # straight
        return (4, max(ranks))
    elif kind(3, ranks):                           # 3 of a kind
        return (3, kind(3,ranks), ranks)
    elif two_pair(ranks):                          # 2 pair
        return (2, two_pair(ranks), ranks)
    elif kind(2, ranks):                           # kind
        return (1, kind(2,ranks), ranks)
    else:                                          # high card
        return (0,ranks)

def hand_percentages(n=700*1000):
    "Prints a table indicating probablity of each type of hand"
    counts = [0]*9
    for i in range(int(n/10)):
        for hand in deal(10):
            rank = hand_rank(hand)[0]
            counts[rank] += 1

    for i in reversed(range(9)):
        print("%14s : %7.4f%%" % (hand_names[i],100*counts[i]/n))

def allmax(iterable, key=None):
    "Return a list of all items equal to the max of the iterable."

    result, maxval = [],None
    key = key or (lambda x : x)

    for x in iterable:
        xval = key(x)
        if not result or xval > maxval:
            result, maxval = [x], xval
        elif xval == maxval:
            result.append(x)

    return result

def poker(hands):
    "Return the best hand: poker([hand,...]) => hand"
    if not hands:
        return False

    return allmax(hands, key=hand_rank)

def test():
    "Test cases for the functions in poker program"
    sf = "6C 7C 8C 9C TC".split() # Straight Flush
    fk = "9D 9H 9S 9C 7D".split() # Four of a Kind
    fh = "TD TC TH 7C 7D".split() # Full House
    st1 = "AC 2S 3H 4D 5H".split() # Straight
    st2 = "AS 2C 3D 4D 5H".split() # Straight

    assert poker([sf, fk, fh]) == [sf]
    assert poker([fk, fh]) == [fk]
    assert poker([fh, fh]) == [fh,fh]
    assert poker([sf]) == [sf]
    assert poker([sf] + 99*[fh]) == [sf]
    assert poker([st1,st2]) == [st1,st2]

    assert hand_rank(sf) == (8, 10)
    assert hand_rank(fk) == (7, 9, 7)
    assert hand_rank(fh) == (6, 10, 7)

    assert straight([9, 8, 7, 6, 5]) == True
    assert straight([9, 8, 8, 6, 5]) == False

    assert flush(sf) == True
    assert flush(fk) == False

    assert card_ranks(st1) == [5,4,3,2,1]
    assert card_ranks(st2) == [5,4,3,2,1]

    print(deal(4))

    hand_percentages(4000)

    test_shuffler(shuffle,"abcd",10000)

    return 'tests pass'

test()


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

import os
import numpy as np

from single_shot.annotation import AnnotationLoader
from single_shot.dataset import DataWrapper, Dataset
from single_shot.label import SingleShotLabelWorker
from single_shot.label import SingleShotLabelBuilder, SingleShotLabelPreprocessor
from single_shot.images import ImageHandler

N_IMAGES = None

DATA_PATH = "/home/jjs/git/datasets/svhn/resize"
ANNOTATION_FILE = os.path.join(DATA_PATH, "resized.json")

def main():

    # 1. Load images
    Xs = ImageHandler.images_from_directory(DATA_PATH, "*.png")
    Xs = np.array(Xs)
    print (Xs.shape)
    # (33402, 224, 224, 3)

    n_train_files = 1000
    n_val_files = 16

    Xs_train = Xs[:n_train_files]
    Xs_val = Xs[n_train_files:n_train_files+n_val_files]

    # 3. list of boxes (N-Boxes instance)
    ann_loader = AnnotationLoader(ANNOTATION_FILE)
    Ys = ann_loader.get_list_of_boxes()
    Ys_train, Ys_val = Ys[:n_train_files], Ys[n_train_files:n_train_files+n_val_files]

    # 4. preprocess labels (N, 4, 4, 5)
    preprocessor = SingleShotLabelPreprocessor((224, 224), (7, 7))
    label_builder = SingleShotLabelBuilder((224, 224), (7, 7))
    worker = SingleShotLabelWorker(preprocessor, label_builder)

    Ys_train = worker.forward(Ys_train)
    Ys_val = worker.forward(Ys_val)

    print (Xs_train.shape, Xs_val.shape, Ys_train.shape, Ys_val.shape)

    # 5. Build Dataset
    dataset_tr = Dataset(Xs_train, Ys_train)
    dataset_val = Dataset(Xs_val, Ys_val)
    dataset = DataWrapper(dataset_tr, dataset_val)
    dataset.to_hdf()

    print (Xs_train.shape, Ys_train.shape)

if __name__ == "__main__":
    main()


# @Time    : 2019/6/29 10:06
# @Author  : Xu Huipeng
# @Blog    : https://brycexxx.github.io/

from typing import List
from collections import defaultdict

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        count = defaultdict(int)
        for i in range(len(s) - 9):
            count[s[i:i + 10]] += 1
        res = []
        for dna, cnt in count.items():
            if cnt > 1:
                res.append(dna)
        return res

    def findRepeatedDnaSequences1(self, s: str) -> List[str]:
        res, visited = set(), set()
        for i in range(len(s) - 9):
            tmp = s[i:i + 10]
            if tmp in visited:
                res.add(tmp)
            else:
                visited.add(tmp)
        return list(res)

    def findRepeatedDnaSequences2(self, s: str) -> List[str]:
        res = []
        count = defaultdict(int)
        for i in range(len(s) - 9):
            tmp = s[i:i + 10]
            count[tmp] += 1
            if count[tmp] == 2:
                res.append(tmp)
        return res


import abc
import re
from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, Optional, Tuple

import numpy as np

from automata.core.symbol.scip_pb2 import Descriptor as DescriptorProto  # type: ignore

class SymbolDescriptor:
    """
    Wraps the descriptor component of the URI into a python object
    """

    ScipSuffix = DescriptorProto

    class PyKind(Enum):
        Local = "local"
        Module = "module"
        Class = "class"
        Method = "method"
        Value = "value"
        Meta = "meta"
        Macro = "macro"
        Parameter = "parameter"
        TypeParameter = "type_parameter"

    def __init__(
        self, name: str, suffix: DescriptorProto, disambiguator: Optional[str] = None
    ) -> None:
        self.name = name
        self.suffix = suffix
        self.disambiguator = disambiguator

    def __repr__(self) -> str:
        return f"Descriptor({self.name}, {self.suffix}" + (
            f", {self.disambiguator})" if self.disambiguator else ")"
        )

    def unparse(self) -> str:
        """Converts back into URI string"""
        escaped_name = SymbolDescriptor.get_escaped_name(self.name)
        if self.suffix == SymbolDescriptor.ScipSuffix.Namespace:
            return f"{escaped_name}/"
        elif self.suffix == SymbolDescriptor.ScipSuffix.Type:
            return f"{escaped_name}#"
        elif self.suffix == SymbolDescriptor.ScipSuffix.Term:
            return f"{escaped_name}."
        elif self.suffix == SymbolDescriptor.ScipSuffix.Meta:
            return f"{escaped_name}:"
        elif self.suffix == SymbolDescriptor.ScipSuffix.Method:
            return f"{escaped_name}({self.disambiguator})."
        elif self.suffix == SymbolDescriptor.ScipSuffix.Parameter:
            return f"({escaped_name})"
        elif self.suffix == SymbolDescriptor.ScipSuffix.TypeParameter:
            return f"[{escaped_name}]"
        else:
            raise ValueError(f"Invalid descriptor suffix: {self.suffix}")

    @staticmethod
    def get_escaped_name(name) -> str:
        def is_simple_identifier(name):
            return re.match(r"^[\w$+-]+$", name) is not None

        if not name:
            return ""
        if is_simple_identifier(name):
            return name
        return "`" + re.sub("`", "``", name) + "`"

    @staticmethod
    def convert_scip_to_python_suffix(
        descriptor_suffix: DescriptorProto,
    ) -> PyKind:
        if descriptor_suffix == SymbolDescriptor.ScipSuffix.Local:
            return SymbolDescriptor.PyKind.Local

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Namespace:
            return SymbolDescriptor.PyKind.Module

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Type:
            return SymbolDescriptor.PyKind.Class

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Method:
            return SymbolDescriptor.PyKind.Method

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Term:
            return SymbolDescriptor.PyKind.Value

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Macro:
            return SymbolDescriptor.PyKind.Macro

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.Parameter:
            return SymbolDescriptor.PyKind.Parameter

        elif descriptor_suffix == SymbolDescriptor.ScipSuffix.TypeParameter:
            return SymbolDescriptor.PyKind.TypeParameter

        else:
            return SymbolDescriptor.PyKind.Meta

@dataclass
class SymbolPackage:
    """Wraps the package component of the URI"""

    manager: str
    name: str
    version: str

    def __repr__(self) -> str:
        return f"Package({self.unparse()})"

    def unparse(self) -> str:
        """Converts back into URI string"""
        return f"{self.manager} {self.name} {self.version}"

@dataclass
class Symbol:
    """
    Symbol is similar to a URI, it identifies a class, method, or a local variable. SymbolInformation contains rich metadata about symbols such as the docstring.

    Symbol has a standardized string representation, which can be used interchangeably with Symbol. The syntax for Symbol is the following:

    # (<x>)+ stands for one or more repetitions of <x>
    <symbol>               ::= <scheme> ' ' <package> ' ' (<descriptor>)+ | 'local ' <local-id>
    <package>              ::= <manager> ' ' <package-name> ' ' <version>
    <scheme>               ::= any UTF-8, escape spaces with double space.
    <manager>              ::= same as above, use the placeholder '.' to indicate an empty value
    <package-name>         ::= same as above
    <version>              ::= same as above
    <descriptor>           ::= <namespace> | <type> | <term> | <method> | <type-parameter> | <parameter> | <meta> | <macro>
    <namespace>            ::= <name> '/'
    <type>                 ::= <name> '#'
    <term>                 ::= <name> '.'
    <meta>                 ::= <name> ':'
    <macro>                ::= <name> '!'
    <method>               ::= <name> '(' <method-disambiguator> ').'
    <type-parameter>       ::= '[' <name> ']'
    <parameter>            ::= '(' <name> ')'
    <name>                 ::= <identifier>
    <method-disambiguator> ::= <simple-identifier>
    <identifier>           ::= <simple-identifier> | <escaped-identifier>
    <simple-identifier>    ::= (<identifier-character>)+
    <identifier-character> ::= '_' | '+' | '-' | '$' | ASCII letter or digit
    <escaped-identifier>   ::= '`' (<escaped-character>)+ '`'
    <escaped-characters>   ::= any UTF-8 character, escape backticks with double backtick.

    Examples -
    from automata.core.symbol.search.symbol_parser import parse_symbol

    symbol_class = parse_symbol(
        "scip-python python automata 75482692a6fe30c72db516201a6f47d9fb4af065 `automata.core.agent.agent_enums`/ActionIndicator#"
    )

    symbol_method = parse_symbol(
        "scip-python python automata 75482692a6fe30c72db516201a6f47d9fb4af065 `automata.core.base.tool`/ToolNotFoundError#__init__()."
    )
    """

    uri: str
    scheme: str
    package: SymbolPackage
    descriptors: Tuple[SymbolDescriptor, ...]

    def __repr__(self) -> str:
        """Converts back into URI string"""
        return f"Symbol({self.uri}, {self.scheme}, {self.package}, {self.descriptors})"

    def __hash__(self) -> int:
        """Hashes the URI string"""
        return hash(self.uri)

    def __eq__(self, other) -> bool:
        """Compares the URI string"""
        if isinstance(other, Symbol):
            return self.uri == other.uri
        elif isinstance(other, str):
            return self.uri == other
        return False

    def symbol_kind_by_suffix(self) -> SymbolDescriptor.PyKind:
        """Converts the suffix of the URI into a PyKind"""
        return SymbolDescriptor.convert_scip_to_python_suffix(self.symbol_raw_kind_by_suffix())

    def symbol_raw_kind_by_suffix(self) -> DescriptorProto:
        """Converts the suffix of the URI into a DescriptorProto"""
        if self.uri.startswith("local"):
            return SymbolDescriptor.ScipSuffix.Local
        if self.uri.endswith("/"):
            return SymbolDescriptor.ScipSuffix.Namespace
        elif self.uri.endswith("#"):
            return SymbolDescriptor.ScipSuffix.Type
        elif self.uri.endswith(")."):
            return SymbolDescriptor.ScipSuffix.Method
        elif self.uri.endswith("."):
            return SymbolDescriptor.ScipSuffix.Term
        elif self.uri.endswith(":"):
            return SymbolDescriptor.ScipSuffix.Meta
        elif self.uri.endswith(")"):
            return SymbolDescriptor.ScipSuffix.Parameter
        elif self.uri.endswith("]"):
            return SymbolDescriptor.ScipSuffix.TypeParameter
        else:
            raise ValueError(f"Invalid descriptor suffix: {self.uri}")

    def parent(self) -> "Symbol":
        """Returns the parent symbol of the current symbol"""
        parent_descriptors = list(self.descriptors)[:-1]
        return Symbol(self.uri, self.scheme, self.package, tuple(parent_descriptors))

    @property
    def dotpath(self) -> str:
        """Returns the dotpath of the symbol"""
        return ".".join([ele.name for ele in self.descriptors])

    @property
    def module_name(self) -> str:
        """Returns the module name of the symbol"""
        return self.descriptors[0].name

    @staticmethod
    def is_local(symbol: "Symbol") -> bool:
        """Returns True if the symbol is local"""
        return symbol.descriptors[0].suffix == SymbolDescriptor.ScipSuffix.Local

    @staticmethod
    def is_meta(symbol: "Symbol") -> bool:
        """Returns True if the symbol is meta"""
        return symbol.descriptors[0].suffix == SymbolDescriptor.ScipSuffix.Meta

    @staticmethod
    def is_parameter(symbol: "Symbol") -> bool:
        """Returns True if the symbol is parameter"""
        return symbol.descriptors[0].suffix == SymbolDescriptor.ScipSuffix.Parameter

    @staticmethod
    def is_protobuf(symbol: "Symbol") -> bool:
        """Returns True if the symbol is a protobuf symbol"""
        return symbol.module_name.endswith("pb2")

    @classmethod
    def from_string(cls, symbol_str: str) -> "Symbol":
        """
        Creates a Symbol instance from a string representation

        :param symbol_str: The string representation of the Symbol
        :return: A Symbol instance
        """
        # Assuming symbol_str is in the format: "Symbol({uri}, {scheme}, Package({manager} {name} {version}), [{Descriptor},...])"
        # Parse the symbol_str to extract the uri, scheme, package_str, and descriptors_str
        match = re.search(r"Symbol\((.*?), (.*?), Package\((.*?)\), \((.*?)\)\)", symbol_str)
        if not match:
            raise ValueError(f"Invalid symbol_str: {symbol_str}")
        uri, _, __, ___ = match.groups()
        # In current implementation, only the uri is used in re-construcing the symbol
        from automata.core.symbol.parser import parse_symbol

        return parse_symbol(uri)

@dataclass
class SymbolReference:
    """Represents a reference to a symbol in a file"""

    symbol: Symbol
    line_number: int
    column_number: int
    roles: Dict[str, Any]

    def __hash__(self) -> int:
        # This could cause collisions if the same symbol is referenced in different files at the same location
        return hash(f"{self.symbol.uri}-{self.line_number}-{self.column_number}")

    def __eq__(self, other) -> bool:
        if isinstance(other, SymbolReference):
            return (
                f"{self.symbol.uri}-{self.line_number}-{self.column_number}"
                == f"{other.symbol.uri}-{other.line_number}-{other.column_number}"
            )
        return False

@dataclass
class SymbolFile:
    """Represents a file that contains a symbol"""

    path: str
    occurrences: str

    def __hash__(self) -> int:
        return hash(self.path)

    def __eq__(self, other) -> bool:
        if isinstance(other, SymbolFile):
            return self.path == other.path
        elif isinstance(other, str):
            return self.path == other
        return False

class SymbolEmbedding(abc.ABC):
    """Abstract base class for different types of embeddings"""

    def __init__(self, symbol: Symbol, embedding_source: str, vector: np.ndarray):
        self.symbol = symbol
        self.embedding_source = embedding_source
        self.vector = vector

class SymbolCodeEmbedding(SymbolEmbedding):
    """Embedding for symbol code"""

    def __init__(self, symbol: Symbol, source_code: str, vector: np.ndarray):
        super().__init__(symbol, source_code, vector)

class SymbolDocEmbedding(SymbolEmbedding):
    """Embedding for symbol documents"""

    def __init__(
        self,
        symbol: Symbol,
        document: str,
        vector: np.ndarray,
        source_code: Optional[str] = None,
        summary: Optional[str] = None,
        context: Optional[str] = None,
    ) -> None:
        super().__init__(symbol, document, vector)
        # begin additional meta data
        self.source_code = source_code
        self.summary = summary
        self.context = context


import numpy as np
import pandas as pd

class PrepHelper:
    def __init__(self):
        pass

    @staticmethod
    def merge(data):
        xdata = data

        xdata["SLEEP"] = np.nan
        xdata["SLEEP"] = xdata["SLEEP"].fillna(xdata["SLEEP_LIGHT"])
        xdata["SLEEP"] = xdata["SLEEP"].fillna(xdata["SLEEP_DEEP"])

        xdata["EXERCISE"] = np.nan
        # Daten falsch ????
        # xdata["EXERCISE"] = xdata["EXERCISE"].fillna(xdata["EXERCISE_MANUAL"])
        xdata["EXERCISE"] = xdata["EXERCISE"].fillna(xdata["EXERCISE_WALK"])
        xdata["EXERCISE"] = xdata["EXERCISE"].fillna(xdata["EXERCISE_BICYCLE"])
        xdata["EXERCISE"] = xdata["EXERCISE"].fillna(xdata["EXERCISE_RUN"])
        # xdata["EXERCISE"] = xdata["EXERCISE"].fillna(xdata["EXERCISE_OTHER"])

        xdata["MEAL"] = np.nan
        xdata["MEAL"] = xdata["MEAL"].fillna(xdata["MEAL_BOLUS_CALCULATOR"])
        # xdata["MEAL"] = xdata["MEAL"].fillna(xdata["MEAL_MANUAL"])

        xdata["BASAL"] = np.nan
        xdata["BASAL"] = xdata["BASAL"].fillna(xdata["BASAL_MANUAL"].replace(0, np.nan))
        xdata["BASAL"] = xdata["BASAL"].fillna(xdata["BASAL_PROFILE"].replace(0, np.nan))
        xdata["BASAL"] = xdata["BASAL"].fillna(xdata["BASAL_INTERPRETER"].replace(0, np.nan))

        xdata["SUSPEND"] = np.nan
        xdata["SUSPEND"] = xdata["SUSPEND"].fillna(xdata["PUMP_SUSPEND"])
        xdata["SUSPEND"] = xdata["SUSPEND"].fillna(xdata["PUMP_AUTONOMOUS_SUSPEND"])

        return xdata

    @staticmethod
    def fill_logic(data):
        xdata = data

        xdata["GLUCOSE_CGM"] = xdata["GLUCOSE_CGM"].ffill()
        xdata["HEART_RATE"] = xdata["HEART_RATE"].ffill()
        xdata["HEART_RATE_VARIABILITY"] = xdata["HEART_RATE_VARIABILITY"].ffill()
        xdata["BASAL"] = xdata["BASAL"].ffill()

        return xdata

    @staticmethod
    def one_hot_logic_merge(data):
        """
        one hot encoding with slicing trick from:
        https://stackoverflow.com/questions/39393856/python-pandas-slice-dataframe-by-multiple-index-ranges
        https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html
        :param data:
        :type data: pandas dataframe
        :return:
        :rtype: pandas dataframe
        """
        xdata = data

        sleepwonan = xdata["SLEEP"].dropna()
        mask = ()
        for i, value in sleepwonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "SLEEP"] = 1

        exercisewonan = xdata["EXERCISE"].dropna()
        mask = ()
        for i, value in exercisewonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "EXERCISE"] = 1

        susvalues = xdata["SUSPEND"].dropna().index.tolist()
        unsusvalues = xdata["PUMP_UNSUSPEND"].dropna().index.tolist()
        endfound = False
        mask = ()
        i = 0
        j = 0
        while i < (len(susvalues)):
            if endfound:
                while susvalues[i] < unsusvalues[j]:
                    # skip other start values before end !!
                    # print(xdata["date"].loc[susvalues[i]])
                    i += 1
                    endfound = False
                j += 1

            if susvalues[i] <= unsusvalues[j]:
                mask += (slice(susvalues[i], unsusvalues[j]),)
                endfound = True
                i += 1
            else:
                # unsuspend ohne suspend !!!!
                # print("unsus")
                # print(xdata["date"].loc[unsusvalues[j]])
                j += 1
        xdata.loc[pd.np.r_[mask], "SUSPEND"] = 1

        xdata["PUMP_RESERVOIR_EMPTY"] = xdata["PUMP_RESERVOIR_EMPTY"].replace(-5.0, 1)

        xdata = pd.get_dummies(xdata, columns=["PUMP_NO_DELIVERY"])

        return xdata

    @staticmethod
    def remove_holes(data):
        cgmindizes = data["GLUCOSE_CGM"].dropna().index.tolist()
        mask = ()
        for i in range(len(cgmindizes) - 1):
            if np.asscalar(cgmindizes[i+1]) - np.asscalar(cgmindizes[i]) > 15:
                mask += (slice(cgmindizes[i] + 1, cgmindizes[i+1]),)
        idlist = data.loc[pd.np.r_[mask]].index.tolist()
        rdata = data.drop(idlist).reset_index(drop=True)
        return rdata

    @staticmethod
    def one_hot_logic_no_merge(data):
        xdata = data
        sleepwonan = xdata["SLEEP_DEEP"].dropna()
        mask = ()
        for i, value in sleepwonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "SLEEP_DEEP"] = 1

        sleepwonan = xdata["SLEEP_LIGHT"].dropna()
        mask = ()
        for i, value in sleepwonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "SLEEP_LIGHT"] = 1

        exercisewonan = xdata["EXERCISE_MANUAL"].dropna()
        mask = ()
        for i, value in exercisewonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "EXERCISE_MANUAL"] = 1

        exercisewonan = xdata["EXERCISE_WALK"].dropna()
        mask = ()
        for i, value in exercisewonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "EXERCISE_WALK"] = 1

        exercisewonan = xdata["EXERCISE_BICYCLE"].dropna()
        mask = ()
        for i, value in exercisewonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "EXERCISE_BICYCLE"] = 1

        exercisewonan = xdata["EXERCISE_RUN"].dropna()
        mask = ()
        for i, value in exercisewonan.items():
            mask += (slice(i, i + value),)
        xdata.loc[pd.np.r_[mask], "EXERCISE_RUN"] = 1

        return xdata


# -*- coding: utf-8 -*-
"""
Created on Sun Jun 25 17:34:06 2017

@author: Michael Zhang
"""

from labjack import ljm
import json
import datetime

#Smart Actuator Libraries
import TE_4030_RAW as acc_raw
import TE_HIGH_RAW as acc_high_raw
import Sensor_Profiles as SP

def get_data_and_log(handle):
    time_current = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    today_date = datetime.datetime.now().date()

    acc_low_x_value,acc_low_y_value,acc_low_z_value = acc_raw.TE_4030_LOW_ACC_RAW(handle,AIN=[0,2,4])
    acc_high_z_val = acc_high_raw.TE_HIGH_ACC_RAW(handle,AIN_names=["AIN6"])

    onboard_5V = ljm.eReadName(handle, "AIN8")
    motor_temp_1 = SP.AD22100K(ljm.eReadName(handle, "AIN1"),onboard_5V)
    motor_temp_2 = SP.AD22100K(ljm.eReadName(handle, "AIN3"),onboard_5V)
    system_hum = SP.HIH_4030(ljm.eReadName(handle, "AIN5"))
    #motor_current_1 = ljm.eReadName(handle, "AIN7")
    #motor_current_2 = ljm.eReadName(handle, "AIN9")
    DAQ_temp = SP.LJT7_onboard(ljm.eReadName(handle, "AIN14"))

    extra_temp = SP.AD22100K(ljm.eReadName(handle, "AIN10"),onboard_5V)
    sensor_data = {
            str(time_current):
                {
                    'Accelerometer X Raw Fs=400Hz': acc_low_x_value,
                    'Accelerometer Y Raw Fs=400Hz': acc_low_y_value,
                    'Accelerometer Z Raw Fs=400Hz': acc_low_z_value,
                    'High Frequency Accelerometer Z Raw Fs=10,000Hz': acc_high_z_val,
                    'Motor Temperature #1': motor_temp_1,
                    'Motor Temperature #2': motor_temp_2,
                    'System Humidity': system_hum,
                    'Motor Current #1': "NaN",
                    'Motor Current #2': "NaN",
                    'DAQ Temperature': DAQ_temp,
                    'Ambient Temperature' : extra_temp
                }
            }
    try:
        file_name='C:\Log Data\data_'+str(today_date)+'.json'
        with open(file_name, 'r+', encoding = 'utf-8') as outfile:
            outfile.seek(0,2)
            position = outfile.tell() -1
            outfile.seek(position)
            outfile.write(json.dumps(sensor_data).replace('{',',',1))
    except FileNotFoundError:
        file_name='C:\Log Data\data_'+str(today_date)+'.json'
        with open(file_name, 'w', encoding = 'utf-8') as outfile:
            json.dump(sensor_data, outfile)

    return motor_temp_1,motor_temp_2,DAQ_temp,system_hum,time_current,extra_temp


from PIL import Image
from selenium import webdriver

class ScreenShot:
    def __init__(self, file_name_: str = "screenshot.png"):
        """
        :type file_name_: str
        """
        self._filename = file_name_
        self._driver = webdriver.PhantomJS()
        self._driver.set_window_size(1024, 768)
        self._crop_margin = 0

    def screen_shot(self, url_: str) -> bool:
        """
        Take a screenshot of the specified url.
        :return: Success is True, Fail is False
        :param url_: the webpage to save screenshot
        """
        try:
            self._driver.get(url_)
            self._driver.save_screenshot(self._filename)
        except Exception as e:
            print(e)
            return False
        return True

    def screen_shot_crop(self, url_: str, search_element_name: str, search_element_type: str = "Id") -> bool:
        """
        Take a screenshot of the specified class of the specified url destination.
        :return: Success is True, Fail is False
        :param url_: the webpage to save screenshot
        :param search_element_name: search to element name
        :param search_element_type: search to element type
        """
        self.screen_shot(url_)
        before_script = """
                        var element = document.getElementBy""" + search_element_type + "('" + search_element_name + """');
                        var rect = element.getBoundingClientRect();
                        """
        try:
            left = self._driver.execute_script(before_script + "return rect.left;") - self._crop_margin
            top = self._driver.execute_script(before_script + "return rect.top;")
            right = self._driver.execute_script(before_script + "return rect.width;") + left + self._crop_margin
            bottom = self._driver.execute_script(before_script + "return rect.height;") + top + self._crop_margin
        except Exception as e:
            print(e)
            return False
        im = Image.open(self._filename)
        im = im.crop((left, top, right, bottom))
        im.save(self._filename)
        im.close()
        return True

    def set_file_name(self, filename_: str):
        self._filename = filename_

    def set_window_size(self, width_: int, height_: int):
        self._driver.set_window_size(width=width_, height=height_)

    def get_window_size(self) -> object:
        return self._driver.get_window_size()

    def set_crop_margin(self, crop_margin_: int):
        self._crop_margin = crop_margin_

    def ger_crop_margin(self) -> object:
        return self._crop_margin

    def __del__(self):
        self._driver.close()

if __name__ == "__main__":
    # スクリーンショットを撮るURLを指定
    screen_url = "http://gakumu.of.miyazaki-u.ac.jp/gakumu/campuslifeinfo/campuslifeinfo/3470-2017-07-06-07-36-07.html"
    # クロップする要素の属性を指定
    element_type = "Id"
    # クロップする要素名を指定
    element_name = "wrapper2"
    # インスタンスを生成するときに保存先ファイル名を指定
    ss = ScreenShot("screenshot.png")
    # screen_urlのスクリーンショットを保存
    ss.screen_shot(screen_url)
    # 保存先ファイル名を変更
    ss.set_file_name("screenshot_crop.png")
    # screen_urlのelement_type属性のelement_nameという要素のスクリーンショットを保存
    ss.screen_shot_crop(screen_url, element_name, element_type)
    # インスタンスの削除
    del ss


import tensorflow as tf

# 在系统默认的计算图上定义张量并计算
a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([2.0, 3.0], name="b")
result = a + b
sess = tf.Session()
print("第一个print:", sess.run(result))

# 利用tf.Graph函数新建计算图，下同
g1 = tf.Graph()
g2 = tf.Graph()

with g1.as_default():
    a = tf.constant([1.0, 1.0])
    b = tf.constant([1.0, 1.0])
    result = a + b

with tf.Session(graph=g1) as sess:
    tf.initialize_all_variables().run()
    with tf.variable_scope("", reuse=True):
        print("第二个print:", sess.run(result))

with g2.as_default():
    a = tf.constant([2.0, 2.0])
    b = tf.constant([2.0, 2.0])
    result = a + b

with tf.Session(graph=g2) as sess:
    tf.initialize_all_variables().run()
    with tf.variable_scope("", reuse=True):
        print("第三个print:", sess.run(result))