from typing import List

class LinkedPair:
    '''
    Linked List hash table key/value pair
    '''

    def __init__(self, key, value):
        self.key = key
        self.value = value
        self.next = None

class HashTable:
    '''
    A hash table that with `capacity` buckets
    that accepts string keys
    '''

    def __init__(self, capacity: int):
        self.length = 0
        self.capacity = capacity
        self.initial_capacity = capacity
        self.storage: List[LinkedPair] = [None] * capacity

    def _hash(self, key: str):
        '''
        Hash an arbitrary key and return an integer.

        You may replace the Python hash with DJB2 as a stretch goal.
        '''
        return hash(key)

    def _hash_djb2(self, key: str):
        '''
        Hash an arbitrary key using DJB2 hash

        OPTIONAL STRETCH: Research and implement DJB2
        '''
        pass

    def _hash_mod(self, key: str):
        '''
        Take an arbitrary key and return a valid integer index
        within the storage capacity of the hash table.
        '''
        return self._hash(key) % self.capacity

    def _change_length(self, size: int):
        self.length += size

        if self.length / self.capacity <= 0.2 and size < 0 and self.capacity > self.initial_capacity:
            self.resize(up=False)

        if self.length / self.capacity >= 0.7 and size > 0:
            self.resize(up=True)

    def insert(self, key: str, value: any):
        '''
        Store the value with the given key.

        Hash collisions should be handled with Linked List Chaining.
        '''
        hashed_key = self._hash_mod(key)
        node = self.storage[hashed_key]

        if node:
            while node.next and node.key != key:
                node = node.next

            if node.key == key:
                node.value = value
            else:
                node.next = LinkedPair(key, value)
                self._change_length(1)
        else:
            self.storage[hashed_key] = LinkedPair(key, value)
            self._change_length(1)

    def remove(self, key: str):
        '''
        Remove the value stored with the given key.

        Print a warning if the key is not found.
        '''
        hashed_key = self._hash_mod(key)
        trailing_node = self.storage[hashed_key]
        leading_node = trailing_node.next

        if trailing_node and trailing_node.key == key:
            self.storage[hashed_key] = trailing_node.next
            self._change_length(-1)
            return

        while leading_node and leading_node.next and leading_node.key != key:
            trailing_node = trailing_node.next
            leading_node = leading_node.next

        if leading_node and leading_node.key == key:
            trailing_node.next = leading_node.next if leading_node else None
            self._change_length(-1)
            return

        print("Value not found...")

    def retrieve(self, key: str):
        '''
        Retrieve the value stored with the given key.

        Returns None if the key is not found.
        '''
        hashed_key = self._hash_mod(key)
        node = self.storage[hashed_key]

        while node and node.next and node.key != key:
            node = node.next

        return node.value if node and node.key == key else None

    def resize(self, up=True):
        '''
        Doubles or Halves the capacity of the hash table and
        rehash all key/value pairs.
        '''
        items: List[LinkedPair] = [*self.storage]
        self.capacity = int(self.capacity * 2 if up else self.capacity // 2)
        self.storage = [None] * self.capacity
        self.length = 0

        for item in items:
            while item:
                self.insert(item.key, item.value)
                item = item.next

if __name__ == "__main__":
    ht = HashTable(2)

    old_capacity = len(ht.storage)
    ht.insert("line_1", "Tiny hash table")
    ht.insert("line_2", "Filled beyond capacity")
    ht.insert("line_3", "Linked list saves the day!")

    print("")

    # Test storing beyond capacity
    print(ht.retrieve("line_1"))
    print(ht.retrieve("line_2"))
    print(ht.retrieve("line_3"))

    # Test resizing

    new_capacity = len(ht.storage)
    print(f"\nResized from {old_capacity} to {new_capacity}.\n")

    # Test if data intact after resizing
    print(ht.retrieve("line_1"))
    print(ht.retrieve("line_2"))
    print(ht.retrieve("line_3"))

    print("")


# Imports

from __future__ import unicode_literals
from django.db import models
from django.utils.translation import ugettext_lazy as _

# Exports

__all__ = (
    "DefaultModel",
    "PrimaryModel",
)

# Constants

# Models

class DefaultModel(models.Model):
    """Allows a record to be marked as "default".

    This model supplies the following:

    - A ``NullBooleanField`` called ``is_default``.
    - Two static methods for use with the ``pre_save`` signal: ``allow_one_default()`` and ``require_one_default()``.

    In your model:

    .. code-block:: py

        from django.db import models
        from superdjango.constraints.managers import DefaultManager
        from superdjango.constraints.models import DefaultModel
        from django.db.models.signals import pre_save

        class AddressType(DefaultModel):
            objects = DefaultManager()

        pre_save.connect(
            DefaultModel.allow_one_default,
            AddressType,
            dispath_uid="addresstype_allow_one_default"
        )

        # Or to require a default record.
        pre_save.connect(
            DefaultModel.require_one_default,
            AddressType,
            dispath_uid="addresstype_require_one_default"
        )

    To implement the default in a form:

    .. code-block:: py

        from django import forms

        class EmployeeForm(forms.ModelForm):

            class Meta:
                model = Employee
                fields = [
                    "address_type",
                ]

            def __init__(*args, **kwargs):
                super(EmployeeForm, self).__init__(*args, **kwargs)

                self.fields['address_type'].initial = AddressType.objects.default()

    """

    is_default = models.NullBooleanField(
        _("default"),
        help_text=_("Indicates that this is the default record.")
    )

    class Meta:
        abstract = True

    @staticmethod
    def allow_one_default(sender, **kwargs):
        """Use this when there can be only one ``is_default`` that is ``True``.

        """
        # noinspection PyTypeChecker
        there_can_be_only_one(sender, kwargs['instance'], "is_default")

    @staticmethod
    def require_one_default(sender, **kwargs):
        """Works like ``allow_one_default()`` but also insures that the first record entered becomes the default.

        """
        # noinspection PyTypeChecker
        instance = kwargs['instance']
        if not instance.is_default:
            if sender.objects.filter(is_default=True).count() == 0:
                instance.is_default = True

        there_can_be_only_one(sender, instance, "is_default")

class PrimaryModel(models.Model):
    """Allows a record to be marked as "primary".

    This model supplies the following:

    - A ``NullBooleanField`` called ``is_primary``.
    - Two static methods for use with the ``pre_save`` signal: ``allow_one_primary()`` and ``require_one_primary()``.

    .. code-block:: py

        from django.db import models
        from superdjango.constraints.models import PrimaryModel
        from django.db.models.signals import pre_save

        class Address(models.Model):
            # A contact's address.

        class Contact(models.Model):
            # A contact in an address book.

        class LinkContactAddress(PrimaryModel):
            # Connect contacts with addresses.

            address = models.ForeignKey(Address)
            contact = models.ForeignKey(Contact)

        pre_save.connect(
            PrimaryModel.allow_one_primary,
            JoinContactAddress,
            dispath_uid="joincontactaddress_allow_one_primary"
        )

        # Or to require a primary record.
        pre_save.connect(
            PrimaryModel.require_one_primary,
            JoinContactAddress,
            dispath_uid="joincontactaddress_require_one_primary"
        )

    """

    is_primary = models.NullBooleanField(
        _("primary"),
        help_text=_("Indicates that this is the primary record.")
    )

    class Meta:
        abstract = True

    @staticmethod
    def allow_one_primary(sender, **kwargs):
        """Use this when there can be only one ``is_primary`` that is ``True``.  See ``allow_one_default`` for
        implementation example.

        """
        # noinspection PyTypeChecker
        there_can_be_only_one(sender, kwargs['instance'], "is_primary")

    @staticmethod
    def require_one_primary(sender, **kwargs):
        """Works like ``allow_one_primary()`` but also insures that the first record entered becomes the primary.

        """
        # noinspection PyTypeChecker
        instance = kwargs['instance']
        if not instance.is_primary:
            if sender.objects.filter(is_primary=True).count() == 0:
                instance.is_primary = True

        there_can_be_only_one(sender, instance, "is_primary")

# Utilities

def there_can_be_only_one(cls, instance, field_name):
    """Helper function for the ``allow_*`` methods.

    :param cls: The class (sender) that emitted the signal.
    :type cls: class

    :param instance: The instance to be checked.
    :type: instance: object

    :param field_name: The name of the field to be checked.

    """
    try:
        field_value = getattr(instance, field_name, False)
    except AttributeError:
        return

    if field_value:
        cls.objects.update(**{field_name: False})


from tensorflow.keras import backend as K
from tensorflow.keras.layers import Conv2D, Reshape, ZeroPadding2D, Concatenate, Input, Activation, Flatten, DepthwiseConv2D
from tensorflow.keras.layers import BatchNormalization, ReLU, add
from tensorflow.keras.models import Model
from ssd_keras_layers.anchorBoxes import AnchorBoxes
from ssd_keras_layers.normalize import Normalize

def _make_divisible(v, divisor, min_value=None):

    if min_value is None:
        min_value = divisor
    new_v = max(min_value, int(v + divisor/2) // divisor * divisor)
    if new_v < 0.9 * v:
        new_v += divisor
    return new_v

def correct_pad(inputs, kernel_size):

    img_dim = 2 if K.image_data_format() == 'channels_first' else 1
    input_size = K.int_shape(inputs)[img_dim:(img_dim + 2)]

    if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size)

    if input_size[0] is None:
        adjust = (1, 1)
    else:
        adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2)
    correct = (kernel_size[0]//2, kernel_size[1]//2)
    return ((correct[0] - adjust[0], correct[0]),
            (correct[1] - adjust[1], correct[1]))

def _inverted_res_block(inputs, expansion, strides, filters, alpha=1.0, block_id=None):

    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1

    in_channels = K.int_shape(inputs)[channel_axis]
    pointwise_conv_filters = int(filters * alpha)
    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
    x = inputs
    prefix = 'block_{}_'.format(block_id)

    if block_id:
        # Expand
        x = Conv2D(expansion * in_channels, (1, 1), padding='same', use_bias=False, name=prefix + "expand")(x)
        x = BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name=prefix + "expand_BN")(x)
        x = ReLU(6., name=prefix + 'expand_relu')(x)
    else:
        prefix = 'expanded_conv_'

    # Depthwise
    if strides == 2:
        x = ZeroPadding2D(padding=correct_pad(x, 3), name=prefix + 'pad')(x)

    x = DepthwiseConv2D((3, 3), strides=strides, use_bias=False,
                        padding='same' if strides == 1 else 'valid',
                        name=prefix + "depthwise")(x)
    x = BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name=prefix + "depthwise_BN")(x)
    x = ReLU(6., name=prefix + "depthwise_relu")(x)

    x = Conv2D(pointwise_filters, (1, 1), padding='same', use_bias=False, name=prefix + 'project')(x)
    x = BatchNormalization(axis=channel_axis, name=prefix + 'project_BN')(x)

    if in_channels == pointwise_conv_filters and strides == 1:
        return add([inputs, x])
    return x

def ssd_conv(inputs, filters, kerner_size, padding='same', strides=(1, 1), l2_reg=5e-4, name=None):

    x = Conv2D(filters, kerner_size, strides=strides, use_bias=False, padding=padding, name=name)(inputs)
    x = BatchNormalization(momentum=0.99, epsilon=0.00001)(x)
    x = ReLU(6.)(x)
    return x

def SSD300(img_size, n_classes,
            anchors=[30, 60, 111, 162, 213, 264, 315],
            variances=[0.1, 0.1, 0.2, 0.2]):

    classes = n_classes + 1# Account for the background class.
    n_boxes = [4, 6, 6, 6, 4, 4]

    # Build network

    if K.image_data_format() == "channels_last":
        bn_axis = 3
    else:
        bn_axis = 1

    x = Input(shape=(img_size))
    alpha = 1.0

    first_block_filters = _make_divisible(32 * alpha, 8)
    stage1 = ZeroPadding2D(padding=correct_pad(x, 3), name='conv1_pad')(x)
    stage1 = Conv2D(first_block_filters, 3, strides=(2, 2), padding='same', use_bias=False, name='conv1')(x)
    stage1 = BatchNormalization(axis=bn_axis, name="conv1_bn")(stage1)
    stage1 = ReLU(6., name="conv1_relu")(stage1)
    stage1 = _inverted_res_block(stage1, filters=16, alpha=alpha, strides=1, expansion=1, block_id=0)

    stage2 = _inverted_res_block(stage1, filters=24, alpha=alpha, strides=2, expansion=6, block_id=1)
    stage2 = _inverted_res_block(stage2, filters=24, alpha=alpha, strides=1, expansion=6, block_id=2)

    stage3 = _inverted_res_block(stage2, filters=32, alpha=alpha, strides=2, expansion=6, block_id=3)
    stage3 = _inverted_res_block(stage3, filters=32, alpha=alpha, strides=1, expansion=6, block_id=4)
    stage3 = _inverted_res_block(stage3, filters=32, alpha=alpha, strides=1, expansion=6, block_id=5)

    stage4 = _inverted_res_block(stage3, filters=64, alpha=alpha, strides=2, expansion=6, block_id=6)
    stage4 = _inverted_res_block(stage4, filters=64, alpha=alpha, strides=1, expansion=6, block_id=7)
    stage4 = _inverted_res_block(stage4, filters=64, alpha=alpha, strides=1, expansion=6, block_id=8)
    stage4 = _inverted_res_block(stage4, filters=64, alpha=alpha, strides=1, expansion=6, block_id=9)
    stage4 = _inverted_res_block(stage4, filters=96, alpha=alpha, strides=1, expansion=6, block_id=10)
    stage4 = _inverted_res_block(stage4, filters=96, alpha=alpha, strides=1, expansion=6, block_id=11)
    stage4 = _inverted_res_block(stage4, filters=96, alpha=alpha, strides=1, expansion=6, block_id=12)

    stage5 = _inverted_res_block(stage4, filters=160, alpha=alpha, strides=2, expansion=6, block_id=13)
    stage5 = _inverted_res_block(stage5, filters=160, alpha=alpha, strides=1, expansion=6, block_id=14)
    stage5 = _inverted_res_block(stage5, filters=160, alpha=alpha, strides=1, expansion=6, block_id=15)
    stage5 = _inverted_res_block(stage5, filters=320, alpha=alpha, strides=1, expansion=6, block_id=16)

    stage5 = Conv2D(1280, (1, 1), use_bias=False, name='conv_last')(stage5)
    stage5 = BatchNormalization(axis=bn_axis, epsilon=1e-3, momentum=0.999, name='conv_last_bn')(stage5)
    stage5 = ReLU(6., name='out_relu')(stage5)

    print("Stage5 shape:{}".format(stage5.shape))
    '''
    conv6_1 = Conv2D(256, (1, 1), activation='relu', padding='same', name='conv6_1')(stage5)
    conv6_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv6_padding')(conv6_1)
    conv6_2 = Conv2D(512, (3, 3), strides=(2, 2), activation='relu', padding='valid', name='conv6_2')(conv6_1)

    conv7_1 = Conv2D(128, (1, 1), activation='relu', padding='same', name='conv7_1')(conv6_2)
    conv7_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv7_padding')(conv7_1)
    conv7_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', name='conv7_2')(conv7_1)

    conv8_1 = Conv2D(128, (1, 1), activation='relu', padding='same', name='conv8_1')(conv7_2)
    conv8_2 = Conv2D(256, (3, 3), activation='relu', padding='valid', name='conv8_2')(conv8_1)

    conv9_1 = Conv2D(128, (1, 1), activation='relu', padding='same',  name='conv9_1')(conv8_2)
    conv9_2 = Conv2D(256, (3, 3), activation='relu', padding='valid', name='conv9_2')(conv9_1)
    '''
    conv6_1 = ssd_conv(stage5, 256, (1, 1), padding='same', name='conv6_1')
    conv6_2 = ssd_conv(conv6_1, 512, (3, 3), strides=(2, 2), padding='same', name='conv6_2')

    conv7_1 = ssd_conv(conv6_2, 128, (1, 1), padding='same', name='conv7_1')
    conv7_2 = ssd_conv(conv7_1, 256, (3, 3), strides=(2, 2), padding='same', name='conv7_2')

    conv8_1 = ssd_conv(conv7_2, 128, (1, 1), padding='same', name='conv8_1')
    conv8_2 = ssd_conv(conv8_1, 256, (3, 3), strides=(2, 2), padding='same', name='conv8_2')

    conv9_1 = ssd_conv(conv8_2, 64, (1, 1), padding='same', name='conv9_1')
    conv9_2 = ssd_conv(conv9_1, 128, (3, 3), strides=(2, 2), padding='same', name='conv9_2')

    # Build SSD network

    conv4_3_norm = Normalize(20, name='conv4_3_norm')(stage4)

    # Build "n_classes" confidence values for each box. Ouput shape: (b, h, w, n_boxes*n_classes)
    conv4_3_norm_mbox_conf = Conv2D(n_boxes[0] * classes, (3, 3), padding='same',  name='conv4_3_norm_mbox_conf')(conv4_3_norm)
    fc7_mbox_conf = Conv2D(n_boxes[1] * classes, (3, 3), padding='same', name='fc7_mbox_conf')(stage5)
    conv6_2_mbox_conf = Conv2D(n_boxes[2] * classes, (3, 3), padding='same', name='conv6_2_mbox_conf')(conv6_2)
    conv7_2_mbox_conf = Conv2D(n_boxes[3] * classes, (3, 3), padding='same',  name='conv7_2_mbox_conf')(conv7_2)
    conv8_2_mbox_conf = Conv2D(n_boxes[4] * classes, (3, 3), padding='same', name='conv8_2_mbox_conf')(conv8_2)
    conv9_2_mbox_conf = Conv2D(n_boxes[5] * classes, (3, 3), padding='same', name='conv9_2_mbox_conf')(conv9_2)

    # Build 4 box coordinates for each box. Output shape: (b, h, w, n_boxes * 4)
    conv4_3_norm_mbox_loc = Conv2D(n_boxes[0] * 4, (3, 3), padding='same', name='conv4_3_norm_mbox_loc')(conv4_3_norm)
    fc7_mbox_loc = Conv2D(n_boxes[1] * 4, (3, 3), padding='same', name='fc7_mbox_loc')(stage5)
    conv6_2_mbox_loc = Conv2D(n_boxes[2] * 4, (3, 3), padding='same', name='conv6_2_mbox_loc')(conv6_2)
    conv7_2_mbox_loc = Conv2D(n_boxes[3] * 4, (3, 3), padding='same',  name='conv7_2_mbox_loc')(conv7_2)
    conv8_2_mbox_loc = Conv2D(n_boxes[4] * 4, (3, 3), padding='same', name='conv8_2_mbox_loc')(conv8_2)
    conv9_2_mbox_loc = Conv2D(n_boxes[5] * 4, (3, 3), padding='same', name='conv9_2_mbox_loc')(conv9_2)

    # Generate the anchor boxes. Output shape: (b, h, w, n_boxes, 8)
    conv4_3_norm_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[0], max_size=anchors[1],aspect_ratios=[2],
                                             variances=variances,name='conv4_3_norm_mbox_priorbox')(conv4_3_norm)
    fc7_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[1], max_size=anchors[2],aspect_ratios=[2,3],
                                             variances=variances,name='fc7_mbox_priorbox')(stage5)
    conv6_2_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[2], max_size=anchors[3],aspect_ratios=[2, 3],
                                             variances=variances, name='conv6_2_mbox_priorbox')(conv6_2)
    conv7_2_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[3], max_size=anchors[4],aspect_ratios=[2, 3],
                                             variances=variances, name='conv7_2_mbox_priorbox')(conv7_2)
    conv8_2_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[4], max_size=anchors[5],aspect_ratios=[2],
                                             variances=variances, name='conv8_2_mbox_priorbox')(conv8_2)
    conv9_2_mbox_priorbox = AnchorBoxes(img_size=img_size, min_size=anchors[5], max_size=anchors[6],aspect_ratios=[2],
                                        variances=variances, name='conv9_2_mbox_priorbox')(conv9_2)

    ### Reshape

    # Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
    # We want the classes isolated in the last axis to perform softmax on them
    conv4_3_norm_mbox_conf_reshape = Flatten(name='conv4_3_norm_mbox_conf_reshape')(conv4_3_norm_mbox_conf)
    fc7_mbox_conf_reshape = Flatten(name='fc7_mbox_conf_reshape')(fc7_mbox_conf)
    conv6_2_mbox_conf_reshape = Flatten(name='conv6_2_mbox_conf_reshape')(conv6_2_mbox_conf)
    conv7_2_mbox_conf_reshape = Flatten(name='conv7_2_mbox_conf_reshape')(conv7_2_mbox_conf)
    conv8_2_mbox_conf_reshape = Flatten(name='conv8_2_mbox_conf_reshape')(conv8_2_mbox_conf)
    conv9_2_mbox_conf_reshape = Flatten(name='conv9_2_mbox_conf_reshape')(conv9_2_mbox_conf)
    # Reshape the box predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
    # We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
    conv4_3_norm_mbox_loc_reshape = Flatten(name='conv4_3_norm_mbox_loc_reshape')(conv4_3_norm_mbox_loc)
    fc7_mbox_loc_reshape = Flatten(name='fc7_mbox_loc_reshape')(fc7_mbox_loc)
    conv6_2_mbox_loc_reshape = Flatten(name='conv6_2_mbox_loc_reshape')(conv6_2_mbox_loc)
    conv7_2_mbox_loc_reshape = Flatten(name='conv7_2_mbox_loc_reshape')(conv7_2_mbox_loc)
    conv8_2_mbox_loc_reshape = Flatten(name='conv8_2_mbox_loc_reshape')(conv8_2_mbox_loc)
    conv9_2_mbox_loc_reshape = Flatten(name='conv9_2_mbox_loc_reshape')(conv9_2_mbox_loc)

    ### Concatenate the predictions from the different layers

    mbox_conf = Concatenate(axis=1, name='mbox_conf')([conv4_3_norm_mbox_conf_reshape,
                                                       fc7_mbox_conf_reshape,
                                                       conv6_2_mbox_conf_reshape,
                                                       conv7_2_mbox_conf_reshape,
                                                       conv8_2_mbox_conf_reshape,
                                                       conv9_2_mbox_conf_reshape])
    mbox_loc = Concatenate(axis=1, name='mbox_loc')([conv4_3_norm_mbox_loc_reshape,
                                                     fc7_mbox_loc_reshape,
                                                     conv6_2_mbox_loc_reshape,
                                                     conv7_2_mbox_loc_reshape,
                                                     conv8_2_mbox_loc_reshape,
                                                     conv9_2_mbox_loc_reshape])
    mbox_priorbox = Concatenate(axis=1, name='mbox_priorbox')([conv4_3_norm_mbox_priorbox,
                                                               fc7_mbox_priorbox,
                                                               conv6_2_mbox_priorbox,
                                                               conv7_2_mbox_priorbox,
                                                               conv8_2_mbox_priorbox,
                                                               conv9_2_mbox_priorbox])

    mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)
    mbox_conf = Reshape((-1, classes), name='mbox_conf_logits')(mbox_conf)
    mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf)

    predictions = Concatenate(axis=2, name='predictions')([mbox_loc,
                            mbox_conf,
                            mbox_priorbox])

    model = Model(inputs=x, outputs=predictions)
    return model


#! /usr/bin/env python2

# This script adds sequences h4238 to 5' end of a read sequence in order to fit Nextera experimental pipeline.
# SYNTAX: python h4328_add.py read_2_file.fastq > read_2_with_h4328_file.fastq

import sys, os.path
import re

#System for opening arguments and automatically adding the -h option. Possible to add more options.
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("read2_file", help="Read 2 file in fastq format")
args = parser.parse_args()

add_seq = 'TAGGACCAGCGTCTCAGTAT'

file_list = []
# Opens fastq file
with open(args.read2_file) as fastqopen:
    for stuff in fastqopen:
        file_list.append(stuff)

counter = 0
for line in file_list:

    counter = counter +1
    if counter == 1:
        sys.stdout.write(line)
    elif counter == 2:
        sys.stdout.write(''.join([add_seq,line.rstrip()]))
        sys.stdout.write('\n')
    elif counter == 3:
        sys.stdout.write(line)
    elif counter == 4:
        sys.stdout.write(''.join(['~'*len(add_seq),line.rstrip()]))
        sys.stdout.write('\n')
        counter = 0

#counter = 0
#for line in file_list:
#
#    counter = counter +1
#    if counter == 1:
#        sys.stdout.write(line)
#    elif counter == 2:
#        sys.stdout.write(line[:-20])
#        sys.stdout.write('\n')
#    elif counter == 3:
#        sys.stdout.write(line)
#    elif counter == 4:
#        sys.stdout.write(line[:-20])
#        sys.stdout.write('\n')
#        counter = 0


#!/usr/bin/env python
# (C) Copyright 2011 Dapper Vision, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__author__ = 'Vlad I. Morariu <morariu@umd.edu>'
__license__ = 'GPL V3'

import cv
import numpy as np
import imfeat

class Eigenfaces(object):

    def __init__(self, images=None, vectors=None, verbose=False):
        self.MODES = [('opencv', 'gray', 32)]
        self.verbose = verbose
        if not images == None:
            images = [imfeat.convert_image(i, self.MODES) for i in images]
            self.train(images, vectors)

    def train(self, images, vectors):
        if images == None or len(images) == 0:
            raise ValueError('\'images\' must contain at least one image')
        if vectors == None or len(vectors) == 0:
            # discard the first 3 eigenvectors
            vectors = range(3, min(len(images), 65))

        # assemble image matrix, subtract mean
        X = np.array([np.asarray(cv.GetMat(i)).ravel() for i in images])
        self.mean = np.mean(X, 0)
        X = X - self.mean

        # assume that the number of images is smaller than the # of pixels
        M = np.dot(X, X.T)
        es, vs = np.linalg.eigh(M)
        vs = np.dot(X.T, vs)        # get e.v. of X^T*X from that of X*X^T
        for i in range(vs.shape[1]):
            vs[:, i] /= np.linalg.norm(vs[:, i])  # normalize each eigenvector

        # order eigenvalues by magnitude and keep only desired vectors
        ordering = sorted([(e, i) for (i, e) in enumerate(es)], reverse=True)
        indexes = np.array(ordering).take(vectors, 0)[:, 1]
        self.vectors = vs.take(indexes.tolist(), 1)

    def make_features(self, image_cv):
        image = np.asarray(cv.GetMat(image_cv), dtype=np.float32).ravel()
        if image.size != self.mean.size:
            raise ValueError('Face Feature input size[%d] != mean size[%d]' % (image.size, self.mean.size))
        out = np.asfarray(np.dot(image - self.mean, self.vectors))
        return [out]

    def __call__(self, image):
        image_cv = imfeat.convert_image(image, self.MODES)
        return self.make_features(image_cv)[0]

def main():
    import glob
    images = [cv.LoadImage(x)
              for x in glob.glob('/home/morariu/downloads/lfwcrop_color/faces/*')[:100]]
    vectors = range(4, 10)
    feat = Eigenfaces(images, vectors)
    out = imfeat.compute(feat, images[0])
    print(out)

if __name__ == "__main__":
    main()


import io
import sys
import types
from thrift.protocol.TCompactProtocol import TCompactProtocolAccelerated as TCompactProtocol
from thrift.protocol.TProtocol import TProtocolException

from .parquet_thrift.parquet import ttypes as parquet_thrift
from .util import ParquetException

def read_thrift(file_obj, ttype):
    """Read a thrift structure from the given fo."""
    from thrift.transport.TTransport import TFileObjectTransport, TBufferedTransport
    starting_pos = file_obj.tell()

    # set up the protocol chain
    ft = TFileObjectTransport(file_obj)
    bufsize = 2 ** 16
    # for accelerated reading ensure that we wrap this so that the CReadable transport can be used.
    bt = TBufferedTransport(ft, bufsize)
    pin = TCompactProtocol(bt)

    # read out type
    obj = ttype()
    obj.read(pin)

    # The read will actually overshoot due to the buffering that thrift does. Seek backwards to the correct spot,.
    buffer_pos = bt.cstringio_buf.tell()
    ending_pos = file_obj.tell()
    blocks = ((ending_pos - starting_pos) // bufsize) - 1
    if blocks < 0:
        blocks = 0
    file_obj.seek(starting_pos + blocks * bufsize + buffer_pos)
    return obj

def write_thrift(fobj, thrift):
    """Write binary compact representation of thiftpy structured object

    Parameters
    ----------
    fobj: open file-like object (binary mode)
    thrift: thriftpy object to write

    Returns
    -------
    Number of bytes written
    """
    t0 = fobj.tell()
    pout = TCompactProtocol(fobj)
    try:
        thrift.write(pout)
        fail = False
    except TProtocolException as e:
        typ, val, tb = sys.exc_info()
        frames = []
        while tb is not None:
            frames.append(tb)
            tb = tb.tb_next
        frame = [tb for tb in frames if 'write_struct' in str(tb.tb_frame.f_code)]
        variables = frame[0].tb_frame.f_locals
        obj = variables['obj']
        name = variables['fname']
        fail = True
    if fail:
        raise ParquetException('Thrift parameter validation failure %s'
                               ' when writing: %s-> Field: %s' % (
            val.args[0], obj, name
        ))
    return fobj.tell() - t0

def is_thrift_item(item):
    return hasattr(item, 'thrift_spec') and hasattr(item, 'read')

def thrift_print(structure, offset=0):
    """
    Handy recursive text ouput for thrift structures
    """
    if not is_thrift_item(structure):
        return str(structure)
    s = str(structure.__class__) + '\n'
    for key in dir(structure):
        if key.startswith('_') or key in ['thrift_spec', 'read', 'write',
                                          'default_spec', 'validate']:
            continue
        s = s + ' ' * offset + key + ': ' + thrift_print(getattr(structure, key)
                                                         , offset+2) + '\n'
    return s

def bind_method(cls, name, func):
    if sys.version_info.major == 2:
        setattr(cls, name, types.MethodType(func, None, cls))
    else:
        setattr(cls, name, func)

for clsname in dir(parquet_thrift):
    if clsname[0].isupper():
        cls = getattr(parquet_thrift, clsname)
        bind_method(cls, '__repr__', thrift_print)

def getstate_method(ob):
    b = io.BytesIO()
    write_thrift(b, ob)
    b.seek(0)
    return b.read()

def copy_method(self):
    cls = type(self)
    out = cls.__new__(cls)
    out.__dict__ = self.__dict__.copy()
    return out

def setstate_method(self, state):
    b = io.BytesIO(state)
    out = read_thrift(b, type(self))
    self.__dict__ = out.__dict__

for cls in [parquet_thrift.FileMetaData,
            parquet_thrift.RowGroup,
            parquet_thrift.ColumnChunk]:
    bind_method(cls, '__getstate__', getstate_method)
    bind_method(cls, '__setstate__', setstate_method)
    bind_method(cls, '__copy__', copy_method)


#!usr/bin/python3

import matplotlib

matplotlib.use("agg")

import numpy as np

import matplotlib.pyplot as plt

points=100000

file=open('/home/richard/NetBeansProjects/inapik/inapi18d3.txt',"r")
x,y,z,a=[],[],[],[]
for k in file:
	row=k.split()
	x.append(float(row[0]))
	y.append(float(row[1]))
ax=np.array(x)
ay=np.array(y)

v2=np.zeros(points)
vav=np.mean(ay)
for i in range(0,points):
	v2[i]=(ay[i]-vav)**2
v2av=np.mean(v2)

plt.figure()
plt.xlabel('time')
plt.ylabel('membrane voltage')
plt.xlim(50,70)
plt.plot(ax,ay,label='%.6f' %v2av)
#plt.plot(ax,az)
#plt.plot(ax,aa)
plt.legend()
plt.savefig('inapivar3.pdf')


import sys

from multiprocessing import Process
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QInputDialog
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt
from Image_Manipulation.artScreen import artScreen
from Image_Manipulation.randomArt import randomArt
import stroopy_words
import record

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        appName = 'RemBRAINdt'

        self.setWindowTitle(appName)
        self.resize(800, 400)
        self.setStyleSheet("QMainWindow { background-color: black; color: white }")

        label = QLabel(appName)
        label.setFont(QFont('Arial', 20))
        label.setStyleSheet("QLabel { color: white}")
        label.setAlignment(Qt.AlignCenter)

        btn1 = QPushButton('Set Baseline')
        btn2 = QPushButton('Start')
        btn1.setStyleSheet("QPushButton { max-width: 15em}")
        btn2.setStyleSheet("QPushButton { max-width: 15em}")

        beta = QLabel('β')
        beta.setStyleSheet("QLabel { color: white}")
        beta.setFont(QFont('Arial', 22))
        alpha = QLabel('α')
        alpha.setStyleSheet("QLabel { color: white}")
        alpha.setFont(QFont('Arial', 22))
        theta = QLabel('θ')
        theta.setStyleSheet("QLabel { color: white}")
        theta.setFont(QFont('Arial', 22))
        delta = QLabel('δ')
        delta.setStyleSheet("QLabel { color: white}")
        delta.setFont(QFont('Arial', 22))

        def addFeatures(self):
            for f in features:
                self.addItem(f)

        artFeatures1 = QComboBox()
        addFeatures(artFeatures1)

        artFeatures2 = QComboBox()
        addFeatures(artFeatures2)

        artFeatures3 = QComboBox()
        addFeatures(artFeatures3)

        artFeatures4 = QComboBox()
        addFeatures(artFeatures4)

        currentStates = [0, 1, 2, 3]
        print(currentStates)

        def setStates():
            artFeatures1.setCurrentIndex(currentStates[0])
            artFeatures2.setCurrentIndex(currentStates[1])
            artFeatures3.setCurrentIndex(currentStates[2])
            artFeatures4.setCurrentIndex(currentStates[3])

        setStates()

        def changeFeatures(index):
            if index == 0:
                wave = artFeatures1
            if index == 1:
                wave = artFeatures2
            if index == 2:
                wave = artFeatures3
            if index == 3:
                wave = artFeatures4
            new = wave.currentIndex() # set new = 1
            old = currentStates[index] #get old value
            change = currentStates.index(new) #find last position of 1
            currentStates[change] = old # change found position to old value in map
            currentStates[index] = new # change old value to new value in map
            print(currentStates)
            setStates()

        artFeatures1.currentIndexChanged.connect(lambda: changeFeatures(0)) #if 0 changed to 1
        artFeatures2.currentIndexChanged.connect(lambda: changeFeatures(1)) #if 0 changed to 1
        artFeatures3.currentIndexChanged.connect(lambda: changeFeatures(2)) #if 0 changed to 1
        artFeatures4.currentIndexChanged.connect(lambda: changeFeatures(3)) #if 0 changed to 1

        layout1 = QVBoxLayout()

        layout2 = QHBoxLayout()

        layout2b = QGridLayout()
        layout2b.addWidget(beta, 0, 0)
        layout2b.addWidget(alpha, 1, 0)
        layout2b.addWidget(theta, 2, 0)
        layout2b.addWidget(delta, 3, 0)
        layout2b.addWidget(artFeatures1, 0, 1)
        layout2b.addWidget(artFeatures2, 1, 1)
        layout2b.addWidget(artFeatures3, 2, 1)
        layout2b.addWidget(artFeatures4, 3, 1)

        layout2.addLayout(layout2b)

        layout3 = QHBoxLayout()
        layout3.addWidget(btn1)
        layout3.addWidget(btn2)

        layout1.addWidget(label)
        layout1.addLayout(layout2)
        layout1.addLayout(layout3)

        self.widget = QWidget()
        self.widget.setLayout(layout1)
        self.setCentralWidget(self.widget)

        btn1.clicked.connect(self.RecordBaseline)
        btn2.clicked.connect(self.open_artScreen)

    def open_artScreen(self):
        art_screen_size = [526,526]
        screen = artScreen(art_screen_size)
        screen.updateScreen(randomArt(art_screen_size))

    def RecordBaseline(self):
        default = 120
        minTime = 5
        maxTime = 300
        increment = 5
        time,ok = QInputDialog.getInt(self, "Enter recording duration", "Time (s):", default, minTime, maxTime, increment)
        if ok:
            stimulus = Process(target=stroopy_words.present, args=(time,))
            recording = Process(target=record.record, args=(time, 'data\csvtest.txt'))
            stimulus.start()
            recording.start()

features = ["Feature 1",
            "Feature 2",
            "Feature 3",
            "Feature 4",
            ]

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec())


#!/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.
#
#from codecs import encode
#import re
#import cgi

#def escape_html(s):
#	return cgi.escape(s, quote=True)

############################################################
#
#Main page listing all path's within this project
#
############################################################
def write_form(host="http://localhost:9080/"):
	#host = self.request.url
	print(host)
	pages = makeList(str(host), ["login", "rot13"])
	print(pages)
	#self.response.out.write(mainForm % {"first": escape_html(host+"rot13"),
	#									"second": escape_html(host+"login")
	#									})

def makeList(host, args):
	#args = args or [""]
	pageList = ""
	for page in args:
		pageList += '
' + page + '
'

	return pageList

write_form()


from inspect import signature
from pyot.core.objects import PyotCoreObject

class SyotBaseObject:
    _bridges = {} # riot.py will modify this

    def __getattribute__(self, name):
        obj = super().__getattribute__(name)
        if isinstance(obj, list):
            return [self.syot_bridge_proxy(item) for item in obj]
        return self.syot_bridge_proxy(obj)

    def syot_bridge_proxy(self, obj):
        if isinstance(obj, PyotCoreObject):
            cls_name = obj.__class__.__name__
            load = {attr: getattr(obj, attr, None) for attr in signature(obj.__init__).parameters}
            return self._bridges[cls_name](**load)
        return obj


from screens import *

import logging
import Printer
import Hardware
import thread
import Lcd

screens = {
    "mainScreen": PrintScreen("mainScreen", "mainScreen", "Tricolour Mendel"),
    "mainMenu": ListScreen("mainScreen", "mainMenu", "Tricolour Mendel"),
    "print": FuncScreen("mainMenu", "print", "Print"),
    "power": ListScreen("mainMenu", "power", "Power"),
    "files": FileScreen("mainMenu", "files", "Files"),
    "temperature": TempScreen("mainMenu", "temperature", "Temperature"),
    "move": ListScreen("mainMenu", "move", "Move"),
    "light": ListScreen("mainMenu", "light", "Light"),
    "camera": ListScreen("mainMenu", "camera", "Camera"),
    "file": FileInfoScreen("files", "file", "File"),
    "temp": TempChangeScreen("temperature", "temp", "Set Temp"),
    "printer": GpioScreen("power", "printer", "Printer Power"),
    "outlet": GpioScreen("power", "outlet", "Outlet Power")
}

curScreen = "mainScreen"
prevScreen = "mainScreen"

mainMenuOptions = [
    ["print", "Print"],
    ["power", "Power"],
    ["files", "Files"],
    ["temperature", "Temerature"],
    ["move", "Move"],
    ["filament", "Filament"],
    ["light", "Light"],
    ["camera", "Camera"]
]

powerOptions = [
    ["printer", "Printer"],
    ["outlet", "Outlet"]
]

printerOptions = [
    [["printer", 0], "On"],
    [["printer", 1], "Off"]
]

outletOptions = [
    [["outlet", 0], "On"],
    [["outlet", 1], "Off"]
]

printerFuntions = [
    [Printer.pprint, "Print"],
    [Printer.cancel, "Cancel"],
    [Printer.pause, "Pause / Resume"],
    [Printer.restart, "Restart"],
]

def start():

    logging.info("Starting...")

    global screens
    global curScreen
    global prevScreen

    screens["mainMenu"].setOptions(mainMenuOptions)
    screens["power"].setOptions(powerOptions)
    screens["print"].setOptions(printerFuntions)
    screens["printer"].setOptions(printerOptions)
    screens["outlet"].setOptions(outletOptions)

    logging.debug("Clearing LCD")
    Lcd.clear()

    logging.debug("Initing Hardware")
    Hardware.init()

    logging.debug("Starting checkPrinter thread")
    thread.start_new_thread(checkPrinter, ())

    logging.info("Started")

    while(True):
        Hardware.update()
        if not curScreen == prevScreen:
            screens[curScreen].init()
            logging.info("Changing Screen to {}".format(curScreen))
            prevScreen = curScreen

        screens[curScreen].update()
        screens[curScreen].draw()
        curScreen = screens[curScreen].nextScreen()

def checkPrinter():

    while(True):
        try:
            Printer.update()
        except:
            logging.exception('Exception in checkPrinter thread')
            raise


"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""

from django.conf.urls import *
from django.contrib import admin
from mysite.views import hello, current_datetime, current_datetime_short, hours_ahead, use_include
from mysite.views import inheritance_version_1, inheritance_version_2, display_meta, contact, simple_form, show_publishers, download
from books import views as books_view
# from contact import views as contact_view

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', hello, name='home'),
    url(r'^time/$', current_datetime),
    url(r'^time_short/$', current_datetime_short),
    url(r'^time/plus/(\d{1,2})/$', hours_ahead),
    url(r'^mypage/$', use_include),
    url(r'^page1/$', inheritance_version_1),
    url(r'^page2/$', inheritance_version_2),
    url(r'^meta/$', display_meta),
    # url(r'^search-form/$', books_view.search_form),
    url(r'^search/$', books_view.search),
    url(r'^contact/$', contact),
    url(r'^publishers/$', show_publishers),
    url(r'^simple_form/$', simple_form, name='simple_form'),
    url(r'^download/$', download, name='download'),
]
# urlpatterns = patterns('',
# 	('^hello/$', hello),
# )


import asyncio
import logging
import pickle

logging.basicConfig(level=logging.DEBUG)

class Client:
    def __init__(self, pid, host='127.0.0.1', port=8888):
        self.pid = pid
        self.host = host
        self.port = port
        self.reader = None
        self.writer = None

    async def __aenter__(self):
        self.reader, self.writer = await asyncio.open_connection(
            self.host, self.port)
        logging.info(f'Connected to {(self.host, self.port)}')
        return self

    async def __aexit__(self, *args):
        logging.info(f'Disconnected from {(self.host, self.port)}')
        self.writer.close()

    async def send(self, obj):
        obj = pickle.dumps(obj)
        self.writer.write(len(obj).to_bytes(4, byteorder='big'))
        self.writer.write(obj)
        await self.writer.drain()

    async def recv(self):
        size = int.from_bytes(await self.reader.readexactly(4),
                              byteorder='big')
        obj = pickle.loads(await self.reader.read(size))
        return obj

async def coro(id_):
    async with Client(pid=id_) as client:
        for i in range(3):
            req = ('get', str(i), i)
            logging.info(f'{[client.pid]} -> {req}')
            await client.send(req)
            resp = await client.recv()
            logging.info(f'{[client.pid]} <- {resp}')

async def main():
    tasks = [asyncio.ensure_future(coro(id_)) for id_ in range(3)]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    asyncio.run(main())


# Py code to input 2 lists and append one at the end of other.

# start with empty lists
list1 = []
list2 = []

num1 = int(input("\nEnter num of elements for 1st list: "))

for item in range(num1):
	list1.append(input(f"\nEnter val {item} : "))

print(f"\nList 1 : {list1}")

num2 = int(input("\nEnter num of elements for 2nd list: "))

for item in range(1, (num2 + 1)):
	list2.append(input(f"\nEnter val {item} : "))

# Appending list 2 at the end of list 1
for var in list2:
	list1.append(var)

print("\nNew List 1 : {}".format(list1))


from django.urls import path
from . import views
urlpatterns = [
    path("", views.index,name ="ShopName"),
    path("about/", views.about,name ="AboutUs"),
    path("contect/", views.contect,name ="contect"),
    path("tracker/", views.tracker,name ="TrackingStatus"),
    path("search/", views.search,name ="Search"),
    path("products/<int:myid>", views.productview,name ="productview"),
    path("checkout/", views.checkout,name ="Checkout"),
    path("handlerequest/", views.handlerequest,name ="HandleRequest"),

]


import openpyxl

wb = openpyxl.load_workbook("censuspopdata.xlsx") #this is my workbook object

sheets = wb.get_sheet_names()
print(sheets) #['Population by Census Tract']

sheet = wb.get_sheet_by_name("Population by Census Tract")

# now that we have the sheet, let us make a list of the columns:
# before that we need to get the total number of rows and total number of columns...

rows = sheet.max_row
columns = sheet.max_column

colist = []
for i in range(1,columns+1):
    colist.append(openpyxl.utils.get_column_letter(i))

# now let us get into the main loop that will get all the details from the sheet:

for i in range(1,rows+1):
    for j in colist:
        print(sheet["{}{}".format(j,i)].value, end=" ")
    print()

# this code is working very well....


from __future__ import print_function

import os.path
import matplotlib.pyplot as plt

# get file with data on it
directory = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(directory, 'county_data.csv')
datafile = open(filename, 'r')
data = datafile.readlines()
datafile.close()

def toInt(x):
    return int("".join([i for i in x if i.isdigit()]))

# lists that hold data for plotting
counties = [i[1:] for i in data[0].split(',')[2:] if ' New Jersey' not in i]
med_incomes = [i.strip() for i in data[48].split('","$')[1:]]
med_incomes[len(med_incomes)-1] = med_incomes[len(med_incomes)-1][:-2]
med_incomes = map(toInt, med_incomes)

fig, ax  = plt.subplots(1, 1)
ax.barh([i for i in range(len(counties))], med_incomes, align='center', alpha=0.5, tick_label = counties)
ax.set_title('Household Income by County\n(2017 U.S. Sample)')
ax.set_xlabel('Household income ($/yr)')
ax.set_ylabel('County')

plt.show()


# Kata URL: https://www.codewars.com/kata/51646de80fd67f442c000013

def strip_url_params(url, params_to_strip = []):
    params  = []
    seen = []
    if "?" in url:
        queries = url[url.index("?")+1:].split("&")
        for i in range(0,len(queries)):
            if queries[i][0] not in seen and queries[i][0] not in params_to_strip:
                seen.append(queries[i][0])
                params.append(queries[i])
        return(url[:url.index("?")+1]+"&".join(params))
    else:
        return url


"""
@author: Harisankar
"""

from fenics import *
from multiphenics import *
import numpy as np
import scipy.optimize as sp
#from scipy.optimize import minimize
from mshr import *

#Iterative solve#############
# Set PETSc solve type (conjugate gradient) and preconditioner
PETScOptions.set("ksp_type", "gmres") # cg, gmres
PETScOptions.set("pc_type", "ilu") # ilu, gamg
PETScOptions.set("pc_factor_levels", 2) # ILU fill_level

# Set the solver tolerance
PETScOptions.set("ksp_rtol", 1.0e-7)
PETScOptions.set("ksp_atol", 1.0e-8)

# Set the maximum solver iterations
PETScOptions.set("ksp_max_it", 1200)

# Allowing non zero initial guess
PETScOptions.set("ksp_initial_guess_nonzero", 1)

# Print PETSc solver configuration
#PETScOptions.set("ksp_view")
#PETScOptions.set("ksp_monitor")

# Create Krylov solver and set operator
solver = PETScKrylovSolver()
solver.set_from_options() # Set PETSc options on the solver

# Helper function to generate subdomain restriction based on a gmsh subdomain id
def generate_subdomain_restriction(mesh, subdomains, subdomain_id):
    D = mesh.topology().dim()
    # Initialize empty restriction
    restriction = MeshRestriction(mesh, None)
    for d in range(D + 1):
        mesh_function_d = MeshFunction("bool", mesh, d)
        mesh_function_d.set_all(False)
        restriction.append(mesh_function_d)
    # Mark restriction mesh functions based on subdomain id
    for c in cells(mesh):
        if subdomains[c] == subdomain_id:
            restriction[D][c] = True
            for d in range(D):
                for e in entities(c, d):
                    restriction[d][e] = True
    # Return
    return restriction

# Helper function to generate interface restriction based on a pair of gmsh subdomain ids
def generate_interface_restriction(mesh, subdomains, subdomain_ids):
    assert isinstance(subdomain_ids, set)
    assert len(subdomain_ids) is 2
    D = mesh.topology().dim()
    # Initialize empty restriction
    restriction = MeshRestriction(mesh, None)
    for d in range(D + 1):
        mesh_function_d = MeshFunction("bool", mesh, d)
        mesh_function_d.set_all(False)
        restriction.append(mesh_function_d)
    # Mark restriction mesh functions based on subdomain ids (except the mesh function corresponding to dimension D, as it is trivially false)
    for f in facets(mesh):
        subdomains_ids_f = set(subdomains[c] for c in cells(f))
        assert len(subdomains_ids_f) in (1, 2)
        if subdomains_ids_f == subdomain_ids:
            restriction[D - 1][f] = True
            for d in range(D - 1):
                for e in entities(f, d):
                    restriction[d][e] = True
    # Return
    return restriction

#### Mesh generation #################

domain = Ellipse(Point(0, 0), 1.25 , 1)
domain.set_subdomain(1, Ellipse(Point(0, 0), 1.25 , 1))
domain.set_subdomain(2, Ellipse(Point(0, 0), 1.15 , 0.9))
domain.set_subdomain(3, Ellipse(Point(0, 0), 1.00 , 0.75))
domain.set_subdomain(4, Circle(Point(0.4, 0), 0.2))

N = 60
mesh = generate_mesh(domain, N)

############### Mesh refinement ########################
refine_domain = CompiledSubDomain("(x[0]-0)*(x[0]-0)/(a*a)+(x[1]-0)*(x[1]-0)/(b*b) >= (1 - 5e-3)", a = 1, b = 0.75)
r_markers = MeshFunction("bool", mesh, mesh.topology().dim(), False)
refine_domain.mark(r_markers, True)
refinedMesh = refine(mesh,r_markers)
mesh = refinedMesh
#refinedMesh = refine(mesh,r_markers)
#mesh = refinedMesh
########################################################

File("output/mesh.pvd") << mesh
N_nodes = mesh.num_vertices()
N_cells = mesh.num_cells()
print("Number of nodes = ",N_nodes)
print("Number of cells = ",N_cells)

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

# Domain definition ##################

celltag = MeshFunction("size_t", mesh, mesh.topology().dim())
celltag.set_all(2)
class shell(SubDomain):
    def inside(self, x, on_boundary):
        a = 1; b=0.75; tol = 1e-3
        return pow(x[0] - 0, 2)/(a*a) + pow(x[1] - 0, 2)/(b*b) >= 1-tol
shell = shell()
for c in cells(mesh):
    if shell.inside(c.midpoint(), True):
        celltag[c.index()] = 1

# Restrictions #
# Generate restriction corresponding to interior subdomain (celltag = 1,2)
solid = generate_subdomain_restriction(mesh, celltag, 1)
fluid = generate_subdomain_restriction(mesh, celltag, 2)

#
class ctrl(SubDomain):
    def inside(self, x, on_boundary):
        a = 1; b=0.75; tol = 1e-3
        return (pow(x[0] - 0, 2)/(a*a) + pow(x[1] - 0, 2)/(b*b) >= 1-tol)
ctrl= ctrl()
for c in cells(mesh):
    if ctrl.inside(c.midpoint(), True):
        celltag[c.index()] = 3
#
class shell(SubDomain):
    def inside(self, x, on_boundary):
        a = 1.15; b=0.9; tol = 1e-3
        return pow(x[0] - 0, 2)/(a*a) + pow(x[1] - 0, 2)/(b*b) >= 1-tol
shell = shell()
for c in cells(mesh):
    if shell.inside(c.midpoint(), True):
        celltag[c.index()] = 1
#
class roi(SubDomain):
    def inside(self, x, on_boundary):
        r = 0.2;tol = 1e-3
        return pow(x[0] - 0.4, 2) + pow(x[1] - 0, 2) <= (r-tol)*(r-tol)
roi = roi()
for c in cells(mesh):
    if roi.inside(c.midpoint(), True):
        celltag[c.index()] = 4
#

#Defining boundaries
facettag = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)

class Interface(SubDomain):
    def inside(self, x, on_boundary):
        a = 1; b=0.75;tol=6e-3
        return (pow(x[0] - 0, 2)/(a*a) + pow(x[1] - 0, 2)/(b*b) >= 1-tol) and (pow(x[0] - 0, 2)/(a*a) + pow(x[1] - 0, 2)/(b*b) <= 1+tol)

class Traction(SubDomain):
    def inside(self, x, on_boundary):
        return ((x[1]<=0.2) and (x[1]>=-0.2) and (x[0]<=0) and on_boundary)

facettag.set_all(0)
traction = Traction()
traction.mark(facettag, 1)
interface = Interface()
interface.mark(facettag, 2)

# FUNCTION SPACES ####################################
p_order = 1
# Function space
P1 = VectorElement("Lagrange", mesh.ufl_cell(), p_order)
EL = P1 * P1
V = FunctionSpace(mesh, EL)
M1 = FunctionSpace(mesh, P1)
P2 = FiniteElement("Lagrange", mesh.ufl_cell(), p_order)
EL = P2 * P2
Q = FunctionSpace(mesh, EL)
M2 = FunctionSpace(mesh, P2)

# Block function space
W = BlockFunctionSpace([V, Q], restrict=[solid,fluid])

# TRIAL/TEST FUNCTIONS #
UP = BlockTrialFunction(W)
(u, p) = block_split(UP)
(ur, ui) = (as_vector((u[0], u[1])), as_vector((u[2], u[3])))
(pr, pi) = as_vector((p[0], p[1]))
VQ = BlockTestFunction(W)
(v, q) = block_split(VQ)
(vr, vi) = (as_vector((v[0], v[1])), as_vector((v[2], v[3])))
(qr, qi) = as_vector((q[0], q[1]))

####### Constants  #########################################
#Material properties: Steel for solid and water for fluid
omega = 8000 #forcing frequency
kappa = 2.15e9 #bulk modulus of fluid
kappar = Constant(kappa)#1.28E5
kappai = Constant(1e-8*omega)#8.9e-4*omega  bulk viscocity *omega = 1.81e-5*3000=5.43E-2
rhof = Constant(997)#1.25 #density of fluid
omega = Constant(omega)#forcing frequency

##Weights for objective function##
beta1 = Constant(1e-3)# weight for the regularization term
beta2 = Constant(1e-5)
alpha = Constant(1) #weight of the minimization functional term
c = Constant(1e-3)
n = 6
eps = (2**n +1)/(2**n)
eps = Constant(eps)

'''# Air as fluid
omega = 7500 #forcing frequency
kappa = 1.28e5 #bulk modulus of fluid
kappar = Constant(kappa)#1.28E5
kappai = Constant(1e-8*omega)#8.9e-4*omega  bulk viscocity *omega = 1.81e-5*3000=5.43E-2
rhof = Constant(1.25)#1.25 #density of fluid
#'''

rhos = Constant(7800)#density of solid
E = 2E11 #Young's modulus steel
nu = 0.3 # poisson's ratio
lamda = E/(2*(1-nu)) - E/(2*(1+nu))#65.9e9
lamdar = Constant(lamda)# Lame's 1st coefficient
lamdai = Constant(1e-8*omega)

mu0 = E/(2*(1+nu))#76.9e9
mu0 = Constant(mu0)
mur = Constant(1)# Lame's 2nd coefficient normalized
mui = Constant(1e-8*omega/mu0)

mua = 0.1# material A
mub = 1# materail B
mua = Constant(mua)# material A
mub = Constant(mub)# materail B

psi = Constant(1)
psi = Expression(("(1+(x[0]+c1)/c2)"),c1 = 1.25,c2 = 2.5,
                  element = M2.ufl_element())
psi = interpolate(psi,M2)

Pext = Constant(100)# External time harmonic pressure load
L = Constant(1.0)# Scale factor for the length of the domain
Uo = L*Pext/mu0 # Nominal deformation

wt = Expression(("((x[0]-0)*(x[0]-0)/(a1*a1)+(x[1]-0)*(x[1]-0)/(b1*b1) >= (1 - tol)) && \
                  ((x[0]-0)*(x[0]-0)/(a2*a2)+(x[1]-0)*(x[1]-0)/(b2*b2) <= (1 + tol))"),
                  a1 = 1,b1 = 0.75, a2 = 1.15,b2 = 0.9,tol = 1e-3,
                  element = M2.ufl_element())
wt = interpolate(wt,M2)
mur = interpolate(mur,M2)
#mur.vector()[:] = mur.vector()[:]*wt.vector()[:]
File("output/wt.pvd") << wt

# DEFINE Forcing function #
fur = Expression(("0.0","0.0"), element = M1.ufl_element())
fui = Expression(("0.0","0.0"), element = M1.ufl_element())
o = Expression(("0.0"), element = M2.ufl_element())

##### Non dimensional constants ###################
a1 = mu0/(rhos*(omega**2)*(L**2))
#a2 = lamda/mu0
a2r = lamdar/mu0
a2i = lamdai/mu0
#a3 = fu/(rhos*(omega**2)*Uo)
a3r = fur/(rhos*(omega**2)*Uo)
a3i = fui/(rhos*(omega**2)*Uo)
#a4 = (rhof*(omega**2)*(L**2))/kappa
a4r = (rhof*(omega**2)*(L**2))*kappar/(kappar**2+kappai**2)
a4i = -(rhof*(omega**2)*(L**2))*kappai/(kappar**2+kappai**2)
a5 = Pext/(rhof*(omega**2)*L*Uo)
a6 = Pext*L/(mu0*Uo)
### u, p and mu are non dimensionalised

## Defining integral elements and normal ##################################
dx = Measure("dx", domain = mesh, subdomain_data = celltag)
ds = Measure("ds", domain = mesh, subdomain_data = facettag)
dS = Measure("dS", domain = mesh, subdomain_data = facettag)

#    n = FacetNormal(mesh) # for obtaining interface normal
normal = Expression(("-(b*b)*x[0]/sqrt((b*b*b*b)*x[0]*x[0] + (a*a*a*a)*x[1]*x[1])",
                     "-(a*a)*x[1]/sqrt((b*b*b*b)*x[0]*x[0] + (a*a*a*a)*x[1]*x[1])"),
                    a = 1,b = 0.75, element = M1.ufl_element())

n_outer = Expression(("(b*b)*x[0]/sqrt((b*b*b*b)*x[0]*x[0] + (a*a*a*a)*x[1]*x[1])",
                      "(a*a)*x[1]/sqrt((b*b*b*b)*x[0]*x[0] + (a*a*a*a)*x[1]*x[1])"),
                     a = 1.25,b = 1, element = M1.ufl_element())

# ASSEMBLING GALERKIN EQUATION ####################################
I = Identity(2)
mur = (mua+psi*(mub-mua))
#two = Constant(2)
#one = Constant(1)
#mur = (mua+ eps*((two*psi)**n)*((one+(two*psi)**n)**(-1))*(mub-mua))
sigmarr = 2*mur*0.5*(grad(ur)+grad(ur).T) + mur*a2r*div(ur)*I
sigmari = 2*mur*0.5*(grad(ui)+grad(ui).T) + mur*a2r*div(ui)*I
sigmair = 2*mui*0.5*(grad(ur)+grad(ur).T) + a2i*div(ur)*I
sigmaii = 2*mui*0.5*(grad(ui)+grad(ui).T) + a2i*div(ui)*I

#Bilinear and linear terms
Bs = a1*(inner(grad(vr), sigmarr-sigmaii) - inner(grad(vi), sigmair+sigmari)) - (dot(vr,ur) - dot(vi,ui))

Csf1 = avg(pr)*dot(avg(vr),-1*normal) - avg(pi)*dot(avg(vi),-1*normal)
Csf2 = avg(pr)*dot(avg(vr),normal) - avg(pi)*dot(avg(vi),normal)

Cfs1 = avg(qr)*dot(avg(ur),-1*normal) - avg(qi)*dot(avg(ui),-1*normal)
Cfs2 = avg(qr)*dot(avg(ur),normal) - avg(qi)*dot(avg(ui),normal)

Bf = (dot(grad(qr),grad(pr)) - dot(grad(qi),grad(pi))) - (a4r*(qr*pr - qi*pi) - a4i*(qi*pr + qr*pi))

#Galerkin equation
a = [[Bs*(dx(1)+dx(3))                                ,                     a1*a6*Csf2*dS(2) ],
     [                    (1/a5)*Cfs2*dS(2)   ,  Bf*(dx(2)+dx(4))                            ]]
f = [-( 1*inner(n_outer,vr) - o*inner(n_outer,vi) )*ds(1) + (inner(a3r,vr) - inner(a3i,vi))*dx(1), o*(qr-qi)*dx(2)] # Pextr = Pext, Pexti = 0

##### Dirichlet BCs #######
pf = Expression((("1","0.0")),element = M1.ufl_element())
bc1 = DirichletBC(W.sub(1), pf, facettag, 3)#magnitude of the external load
solid_fixed = Expression(("0","0"), element = M1.ufl_element())
bc1 = DirichletBC(W.sub(0).sub(1), solid_fixed, facettag, 1)
bcs = BlockDirichletBC([bc1])

## DIRECT SOLVER ######################
A = block_assemble(a)
F = block_assemble(f)
#bcs.apply(A)
#bcs.apply(F)

U = BlockFunction(W)
U.block_vector()[:] = 0.0 #initial guess
print("Number of degrees of freedom = ",len(U.block_vector()))
block_solve(A, U.block_vector(), F)
u = U.sub(0)
p = U.sub(1)
#

#### ITERATIVE SOLVER #
## SOLVE #
#A = block_assemble(a)
#F = block_assemble(f)
##bcs.apply(A)
##bcs.apply(F)
#
#U = BlockFunction(W)
#U.block_vector()[:] = 0.0 # initial guess
#print("Number of degrees of freedom = ",len(U.block_vector()))
#
## Solving the linear system
#solver.solve(A, U.block_vector(), F)
#U.block_vector().block_function().apply("to subfunctions")
#u = U.sub(0)
#p = U.sub(1)
##
#######################################

#### Storing the primal solution #########
ur0 = interpolate(u.sub(0),M1)
pr0 = interpolate(p.sub(0),M2)
ui0 = interpolate(u.sub(1),M1)
pi0 = interpolate(p.sub(1),M2)

#rmesh = refine(mesh)
rmesh = mesh
Qh = FunctionSpace(rmesh, "Lagrange", 1)
P1 = VectorElement("Lagrange", rmesh.ufl_cell(), 1)
Vh = FunctionSpace(rmesh, P1)
Pr = interpolate(pr0,Qh)
Ur = interpolate(ur0,Vh)
Pi = interpolate(pi0,Qh)
Ui = interpolate(ui0,Vh)

mur_init = project(mur,Qh)

# OUTPUT #
file1 = XDMFFile("output/ur_init.xdmf")
file1.write(Ur,1)
file2 = XDMFFile("output/pr_init.xdmf")
file2.write(Pr,1)
file1 = XDMFFile("output/ui_init.xdmf")
file1.write(Ui,1)
file2 = XDMFFile("output/pi_init.xdmf")
file2.write(Pi,1)

file3 = XDMFFile("output/mu_init.xdmf")
file3.write(mur_init,1)

wt_interface = Expression(("(x[0]-0)*(x[0]-0)/(a*a)+(x[1]-0)*(x[1]-0)/(b*b) >= (1 - tol) && \
                            (x[0]-0)*(x[0]-0)/(a*a)+(x[1]-0)*(x[1]-0)/(b*b) <= (1 + tol)"),
                          a = 1, b = 0.75, tol = 5E-3, element = M2.ufl_element())
Wt_interface = project(wt_interface,Qh)
file3 = XDMFFile("output/wt_interface.xdmf")
file3.write(Wt_interface,1)

File("output/celltag.pvd") << celltag

l = Constant(1)
print("Length of actuator region = ", assemble(l*ds(1)))

one = Constant(1)

Rpsi_1 = assemble(beta1*psi*(one-psi)*dx(3))
Rpsi_2 = assemble(beta2*(dot(grad(psi),grad(psi))+c)**(1/2)*dx(3))
Jpsi = Rpsi_1 + Rpsi_2 + assemble(alpha*(pr0*pr0 + pi0*pi0)*dx(4))

print("Jpsi =", Jpsi)

#### Solving adjoint equation#############
# TRIAL/TEST FUNCTIONS #
WQ = BlockTrialFunction(W)
(w, q) = block_split(WQ)
(wr, wi) = (as_vector((w[0], w[1])), as_vector((w[2], w[3])))
(qr, qi) = as_vector((q[0], q[1]))
dUdP = BlockTestFunction(W)
(du, dp) = block_split(dUdP)
(dur, dui) = (as_vector((du[0], du[1])), as_vector((du[2], du[3])))
(dpr, dpi) = as_vector((dp[0], dp[1]))

# ASSEMBLE #
I = Identity(2)
sigma_wrr = 2*mur*0.5*(grad(wr)+grad(wr).T) + mur*a2r*div(wr)*I
sigma_wri = 2*mur*0.5*(grad(wi)+grad(wi).T) + mur*a2r*div(wi)*I
sigma_wir = 2*mui*0.5*(grad(wr)+grad(wr).T) + a2i*div(wr)*I
sigma_wii = 2*mui*0.5*(grad(wi)+grad(wi).T) + a2i*div(wi)*I

#Bilinear and linear terms
Bs = a1*(inner(grad(dur), sigma_wrr-sigma_wii) - inner(grad(dui), sigma_wir+sigma_wri)) - (dot(dur,wr) - dot(dui,wi))

Csf1 = avg(qr)*dot(avg(dur),-1*normal) - avg(qi)*dot(avg(dui),-1*normal)
Csf2 = avg(qr)*dot(avg(dur),normal) - avg(qi)*dot(avg(dui),normal)

Cfs1 = avg(dpr)*dot(avg(wr),-1*normal) - avg(dpi)*dot(avg(wi),-1*normal)
Cfs2 = avg(dpr)*dot(avg(wr),normal) - avg(dpi)*dot(avg(wi),normal)

Bf = (dot(grad(dpr),grad(qr)) - dot(grad(dpi),grad(qi))) - (a4r*(dpr*qr - dpi*qi) - a4i*(dpi*qr + dpr*qi))

two = Constant(2)
#Galerkin equation
a_adj = [[Bs*(dx(1)+dx(3))                                ,                     (1/a5)*Csf2*dS(2) ],
         [                    a1*a6*Cfs2*dS(2)    ,  Bf*(dx(2)+dx(4))                             ]]
f_adj = [o*(inner(dur,a3r) + inner(dui,a3i))*(dx(1)+dx(3)), -two*alpha*(pr0*dpr + pi0*dpi)*dx(4)]

##### Dirichlet BCs #######
pf = Expression((("0","0.0")),element = M1.ufl_element())
bc1 = DirichletBC(W.sub(1), pf, facettag, 3)#magnitude of the external load
solid_fixed = Expression(("0","0"), element = M1.ufl_element())
bc1 = DirichletBC(W.sub(0).sub(1), solid_fixed, facettag, 1)
bcs_adj = BlockDirichletBC([bc1])

## Direct SOLVER ############################
A_adj = block_assemble(a_adj)
F_adj = block_assemble(f_adj)
#bcs_adj.apply(A_adj)
#bcs_adj.apply(F_adj)

U_adj = BlockFunction(W)
block_solve(A_adj, U_adj.block_vector(), F_adj)
w = U_adj.sub(0)
q = U_adj.sub(1)
#

#### ITERATIVE SOLVER #
## SOLVE #
#A_adj = block_assemble(a_adj)
#F_adj = block_assemble(f_adj)
##bcs_adj.apply(A_adj)
##bcs_adj.apply(F_adj)
#
#U_adj = BlockFunction(W)
#U_adj.block_vector()[:] = 0.0 # initial guess
#
## Solving the linear system
#solver.solve(A_adj, U_adj.block_vector(), F_adj)
#U_adj.block_vector().block_function().apply("to subfunctions")
#w = U_adj.sub(0)
#q = U_adj.sub(1)
###########################################

#### Storing the dual solution #########
wr = project(w.sub(0),M1)
qr = project(q.sub(0),M2)
wi = project(w.sub(1),M1)
qi = project(q.sub(1),M2)

#### Computing gradient #######

DJ = Function(M2)
one = Constant(1)
J = a1*(mua+ psi*(mub-mua))*(inner(grad(ur0),(grad(wr) + grad(wr).T) + a2r*div(wr)*I) - \
                             inner(grad(ui0),(grad(wi) + grad(wi).T) + a2r*div(wi)*I))*dx(3) \
    + beta1*psi*(one-psi)*dx(3)
#       + beta*((mua+(psi**6)*(mub-mua))-mua)*((mua+(psi**6)*(mub-mua))-muA)*(mub-(mua+(psi**6)*(mub-mua)))*(mub-(mua+(psi**6)*(mub-mua)))*dx(3) \

dpsi = Constant(1)
dpsi = project(dpsi,M2)
J2 = beta2*dot(grad(psi),grad(dpsi))*(dot(grad(psi),grad(psi))+c)**(-1/2)*dx(3)
DJ2 = derivative(J2,dpsi)
DJ2 = assemble(DJ2)

DJdpsi = derivative(J,psi)
DJdpsi = assemble(DJdpsi)
DJ.vector()[:] = DJdpsi[:]

Sensitivity_vector = np.add(DJdpsi,DJ2)
DJ.vector()[:] = Sensitivity_vector[:]

DJplot = project(DJ,Qh)
file4 = XDMFFile("output/DJdpsi.xdmf")
file4.write(DJplot,1)

dpsi = Function(M2)
dpsi.vector()[:] = DJ.vector()[:]
dpsi_mag = sqrt(dpsi.vector().inner(dpsi.vector()))
dpsi.vector()[:] = (1/dpsi_mag)*dpsi.vector()[:] # normalizing dmu
#

ps = Function(M2)
ps.vector()[:] = psi.vector()[:]*wt.vector()[:]
h = -(0.00125/8)*sqrt(ps.vector().inner(ps.vector()))
dpsi.vector()[:] = h*dpsi.vector()[:]
psi.vector()[:] = psi.vector()[:] + dpsi.vector()[:]

######## Finding optimized solution ###################

# TRIAL/TEST FUNCTIONS #
UP = BlockTrialFunction(W)
(u, p) = block_split(UP)
(ur, ui) = (as_vector((u[0], u[1])), as_vector((u[2], u[3])))
(pr, pi) = as_vector((p[0], p[1]))
VQ = BlockTestFunction(W)
(v, q) = block_split(VQ)
(vr, vi) = (as_vector((v[0], v[1])), as_vector((v[2], v[3])))
(qr, qi) = as_vector((q[0], q[1]))

# ASSEMBLING GALERKIN EQUATION ####################################
I = Identity(2)
mur = (mua+psi*(mub-mua))
#two = Constant(2)
#one = Constant(1)
#mur = (mua+ eps*((two*psi)**n)*((one+(two*psi)**n)**(-1))*(mub-mua))

sigmarr = 2*mur*0.5*(grad(ur)+grad(ur).T) + mur*a2r*div(ur)*I
sigmari = 2*mur*0.5*(grad(ui)+grad(ui).T) + mur*a2r*div(ui)*I
sigmair = 2*mui*0.5*(grad(ur)+grad(ur).T) + a2i*div(ur)*I
sigmaii = 2*mui*0.5*(grad(ui)+grad(ui).T) + a2i*div(ui)*I

#Bilinear and linear terms
Bs = a1*(inner(grad(vr), sigmarr-sigmaii) - inner(grad(vi), sigmair+sigmari)) - (dot(vr,ur) - dot(vi,ui))

Csf1 = avg(pr)*dot(avg(vr),-1*normal) - avg(pi)*dot(avg(vi),-1*normal)
Csf2 = avg(pr)*dot(avg(vr),normal) - avg(pi)*dot(avg(vi),normal)

Cfs1 = avg(qr)*dot(avg(ur),-1*normal) - avg(qi)*dot(avg(ui),-1*normal)
Cfs2 = avg(qr)*dot(avg(ur),normal) - avg(qi)*dot(avg(ui),normal)

Bf = (dot(grad(qr),grad(pr)) - dot(grad(qi),grad(pi))) - (a4r*(qr*pr - qi*pi) - a4i*(qi*pr + qr*pi))

#Galerkin equation
a = [[Bs*(dx(1)+dx(3))                                ,                     a1*a6*Csf2*dS(2) ],
     [                    (1/a5)*Cfs2*dS(2)   ,  Bf*(dx(2)+dx(4))                            ]]
f = [-( 1*inner(n_outer,vr) - o*inner(n_outer,vi) )*ds(1) + (inner(a3r,vr) - inner(a3i,vi))*dx(1), o*(qr-qi)*dx(2)] # Pextr = Pext, Pexti = 0

## DIRECT SOLVER ######################
A = block_assemble(a)
F = block_assemble(f)
#bcs.apply(A)
#bcs.apply(F)

U = BlockFunction(W)
block_solve(A, U.block_vector(), F)
u = U.sub(0)
p = U.sub(1)
#

#### ITERATIVE SOLVER #
## SOLVE #
#A = block_assemble(a)
#F = block_assemble(f)
##bcs.apply(A)
##bcs.apply(F)
#
#U = BlockFunction(W)
#U.block_vector()[:] = 0.0 # initial guess
#
## Solving the linear system
#solver.solve(A, U.block_vector(), F)
#U.block_vector().block_function().apply("to subfunctions")
#u = U.sub(0)
#p = U.sub(1)
##
#######################################

#### Storing the optimized solution #########
ur0 = interpolate(u.sub(0),M1)
pr0 = interpolate(p.sub(0),M2)
ui0 = interpolate(u.sub(1),M1)
pi0 = interpolate(p.sub(1),M2)

Rpsi_1h = assemble(beta1*psi*(one-psi)*dx(3))
Rpsi_2h = assemble(beta2*(dot(grad(psi),grad(psi))+c)**(1/2)*dx(3))
Jpsi_h = Rpsi_1h + Rpsi_2h + assemble(alpha*(pr0*pr0 + pi0*pi0)*dx(4))

print("Jpsi_h =", Jpsi_h)

#### Finding residue ##########
Residue = abs(Jpsi_h - Jpsi - DJ.vector().inner(dpsi.vector()))
print("Residue =",Residue)
print("del_J =",DJ.vector().inner(dpsi.vector()))


# Chapter-1: Fundamentals
# Print-One-Return-Another
# Build a function that takes array of numbers. The function should print second to last value in the array and return first odd value in array

# Assume the argument passed is an array and it consists of numbers
# Assume the array has 2 or more elements
def pora(arr):
    print("The second to last element is '{}'").format(arr[len(arr) - 2])

    for i in range(0, len(arr)):
        if arr[i] % 2:
            return arr[i]

    return "No odd numbers in the array"

myArr = [2,4,6,8,0]
print("The first odd number is '{}'").format(pora(myArr))


def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3, 'd':3,
     'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8, 'i':8,
      'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N':13,
       'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16, 'R':17, 'r':17,
       'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
        'w':22, 'X':23, 'x':23, 'Y': 24, 'y':24, 'Z':25, 'z':25}
    pos = alphabet_pos[letter]
    return pos

def rotate_character(char, amount):
    rotated = ""
    if not char.isalpha():
        return char # append the letter to the string without rotating
    else:
        rotated = chr(((ord(char.lower()) + amount - 97) % 26) + 97)
    if char.isupper():
        return rotated.upper()
    else:
        return rotated

def rotate_string(text, rot):
    encrypted = ""
    for char in text:
        encrypted += rotate_character(char, rot)
    return encrypted

def main():
    letter = input("Type a message: ")
    rot = int(input("Rotate by: "))
    print(encrypt(letter, rot))
    q = input()

if __name__ == "__main__":
    main()


from netCDF4 import Dataset
import sys

from . import utilities as util

def main(cfg):
    """
    This script takes two processed inventories
    and copies all of their variables into a new one
    """

    with Dataset(cfg.inv_out,'w') as outf:
        util.prepare_output_file(cfg.cosmo_grid, cfg.nc_metadata, outf)

        for inv,name in zip([cfg.inv_1,cfg.inv_2],
                            [cfg.inv_name_1,cfg.inv_name_2]):
            with Dataset(inv) as inf:
                if inv == cfg.inv_1:
                    util.add_country_mask(inf['country_ids'][:].T, outf)

                for v in inf.variables:
                    if v in ['rotated_pole','rlon','rlat','country_ids','lon','lat']:
                        continue

                    var = inf[v]
                    nc_var = outf.createVariable(v+name, var.dtype, var.dimensions)
                    nc_var.units = var.units
                    nc_var.grid_mapping = var.grid_mapping
                    nc_var[:] = var[:]


import redis
import json
from message_queue.utils import redis_config, LogWrapper, _logger
import requests

logger = LogWrapper(_logger)

r = redis.Redis(**redis_config)

def run():
    while True:
        json_data = r.brpop('notification')
        ok_data = json_data[1].decode('utf-8')
        data = json.loads(ok_data)

        logger.info("data: {}".format(ok_data))

        req = requests.post('http://localhost:8000/social/save/notif/', json=data)

        logger.info("data: {}, status: {}".format(ok_data, req.status_code))

if __name__ == '__main__':
    run()


import datetime
import logging
import urllib

import socorro.lib.util as util
import socorro.webapi.webapiService as webapi
import socorro.database.database as db
from socorro.lib.datetimeutil import UTC, string_to_datetime

logger = logging.getLogger("webapi")

#=================================================================================================================
class EmailCampaignVolume(webapi.JsonServiceBase):
  """ Hoopsnake API which estimates the total volume of emails
      a given campaign will generate. A campaign is based on:
      * Product
      * Crash Signature
      * Date Range

      The API returns a raw number
  """
  #-----------------------------------------------------------------------------------------------------------------
  def __init__(self, configContext):
    super(EmailCampaignVolume, self).__init__(configContext)
    self.database = db.Database(configContext)

  #-----------------------------------------------------------------------------------------------------------------
  # curl http://localhost:8085/emailcampaigns/volume/p/Firefox/v/4.0b6/sig/js_FinishSharingTitle/start/2010-06-05/end/2010-06-13
  "/emailcampaigns/volume/p/{product}/v/{versions}/sig/{signature}/start/{start_date}/end/{end_date}"
  uri = '/emailcampaigns/volume/p/(.*)/v/(.*)/sig/(.*)/start/(.*)/end/(.*)'

  #-----------------------------------------------------------------------------------------------------------------
  def get(self, *args):
    " Webpy method receives inputs from uri "
    stringListFromCSV = lambda s: tuple([x.strip() for x in s.split(',')])
    convertedArgs = webapi.typeConversion([str, stringListFromCSV, str, string_to_datetime, string_to_datetime], args)
    parameters = util.DotDict(zip(['product', 'versions', 'signature', 'start_date', 'end_date'], convertedArgs))

    connection = self.database.connection()
    try:
      return {'emails': self.estimate_campaign_volume(connection, parameters)}
    finally:
      connection.close()

  #-----------------------------------------------------------------------------------------------------------------
  def estimate_campaign_volume(self, connection, parameters):
    " Queries reports tables for a count of unique email addresses "

    # Yeah, it isn't a half day or anything like that -- lumbergh
    # start date at 00:00:00 and end date at 23:59:59
    end_date = parameters['end_date']
    parameters['end_date'] = datetime.datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59, tzinfo=UTC)

    encoded_signature = parameters['signature']
    parameters['signature'] = urllib.unquote_plus(encoded_signature)

    parameters['version_clause'] = ''
    if len(parameters['versions']) > 0:
      parameters['version_clause'] = " version IN %(versions)s AND "

    sql = """
        SELECT count(distinct email) as total FROM reports
        WHERE TIMESTAMP WITH TIME ZONE '%(start_date)s' <= reports.date_processed AND
              TIMESTAMP WITH TIME ZONE '%(end_date)s' > reports.date_processed AND
              product = %%(product)s AND
              %(version_clause)s
              signature = %%(signature)s AND
              email IS NOT NULL AND
              email ~ '.*@.*\.[a-zA-Z]{2,4}';
    """ % parameters
    cursor = connection.cursor()
    logger.info(cursor.mogrify(sql, parameters))
    cursor.execute(sql, parameters)
    return cursor.fetchone()[0]


#!/usr/bin/env python

import argparse
import re

AMPLICON_NAME_RE = r'.*_(?P<num>\d+)_[^0-9]*(?P<name>L(?:EFT)?|R(?:IGHT)?)'

def primer_info_to_position(name):
    position = 0
    re_match = re.match(AMPLICON_NAME_RE, name)
    if re_match is None:
        raise ValueError("{} does not match expected amplicon name format".format(name))
    side = re_match.group('name')
    num = re_match.group('num')
    if side == 'RIGHT' or side == 'R':
        position += 1000
    if num is not None:
        position += int(num)
    return position

def write_amplicon_info_file(bed_file, amplicon_info_file):
    amplicon_sets = {}
    amplicon_ids = set()
    for line in bed_file:
        fields = line.strip().split('\t')
        name = fields[3]
        re_match = re.match(AMPLICON_NAME_RE, name)
        if re_match is None:
            raise ValueError("{} does not match expected amplicon name format".format(name))
        amplicon_id = int(re_match.group('num'))
        amplicon_set = amplicon_sets.get(amplicon_id, [])
        amplicon_set.append(name)
        amplicon_sets[amplicon_id] = amplicon_set
        amplicon_ids.add(amplicon_id)

    for id in sorted(list(amplicon_ids)):
        amplicon_info = '\t'.join([name for name in sorted(amplicon_sets[id], key=primer_info_to_position)]) + '\n'
        amplicon_info_file.write(amplicon_info)
    amplicon_info_file.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Write an amplicon info file for iVar from a BED file describing primer positions')
    parser.add_argument('bed_file', type=argparse.FileType(), help='Primer BED file')
    parser.add_argument('amplicon_info_file', type=argparse.FileType('w'), help='Output file: amplicon info file in TSV format')
    args = parser.parse_args()

    write_amplicon_info_file(args.bed_file, args.amplicon_info_file)


# Error Chaching

'''
Name of Function: error1
Parameters:
Return Value:
What it does: Error Chatching #1, if the user is below 18.
'''
def error1():
    age = int(input())
    if age<18:
        print(child)

'''
Name of Function: error2
Parameters:
Return Value:
What it does: Error Chatching #2, post letters
'''
def error2():
    for x in range(10):
        print("abcdefjklm"[x])

'''
Name of Function: error3
Parameters:
Return Value:
What it does: Error Chatching #3, work otu a length/width
'''
def error3():
    length = int(input("Input the length "))
    width = int(input("Input the width "))
    area = length * width
    print(area)

'''
Name of Function: numbercheck
Parameters: value
Return Value: value
What it does: Gets a number with a error check in it.
'''
def numbercheck(value):
    number = -1
    while number < 0:
        try:
            number = int(input(value))
        except ValueError:
            print("This is not a number, please enter again")
    return number

'''
Name of Function: divisoncheck()
Parameters:
Return Value:
What it does: It works out x and y by dividing it.
'''
def divisoncheck():
    try:
        x = numbercheck("Enter a number to divide ")
        y = numbercheck("ENter a number to divide by: ")
        print(x/y)

    except ZeroDivisionError:
        print("Cannot divide by zero ")
        y = 1

'''
Name of Function: areaRectangle
Parameters:
Return Value: Area
What it does: Works out the area of the Rectangle with error chatching
'''

def areaRectangle():
    length = -1
    width = -1
    while length < 0:
        try:
            length = int(input("Please enter the length of the rectangle. "))
            while length < 0:
                print("We do not accept negitive numbers! Sorry.")
                length = int(input("Please enter the length of the rectangle. "))
        except:
            print("There was a error somewhere! Please try again.")
    while width < 0:
        try:
            width = int(input("Please enter the width of the rectangle. "))
            while length < 0:
                print("We do not accept negitive numbers! Sorry.")
                length = int(input("Please enter the length of the rectangle. "))
        except:
            print("There was a error somewhere! Please try again.")
    area = length*width
 #   print("Your area is ",  area)
    return area

'''
Name of Function: error4
Parameters:
Return Value: Number
What it does: Prints square numbers from 1 to 12
'''
def error4():
    number = 1
    while number < 13:
        print(number, "Squared is ", number*number)
        number = number + 1


import os
from collections import *

from utils import get_actual
from utils import *

class Halt(Exception):
    pass

def run_input(pos, l, input_=0):
    opcode = l[pos]

    if opcode == 99:
        raise Halt("found 99, exiting")

    if opcode >= 100:
        inp = str(opcode)
        while len(inp) < 5:
            inp = f"0{inp}"
        opcode = int(inp[-2:])
        pa, pb, pc = inp[:-2]
    else:
        pa, pb, pc = "", "", ""
    del pa

    if opcode in (1, 2):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        pos3 = l[pos + 3]
        c = l[pos1] if pc != "1" else pos1
        b = l[pos2] if pb != "1" else pos2
        l[pos3] = c + b if opcode == 1 else c * b

        if pos3 != pos:
            return pos + 4

        return l[pos3]

    if opcode == 3:
        pos1 = l[pos + 1]
        l[pos1] = input_

        if pos1 != pos:
            return pos + 2

        return l[pos1]

    if opcode == 4:
        pos1 = l[pos + 1]
        output = l[pos1] if pc != "1" else pos1
        print(f"value is {output}")

        return pos + 2

    if opcode in (5, 6):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        if opcode == 5:
            check = l[pos1] if pc != "1" else pos1
            if check != 0:
                l[pos] = l[pos2] if pb != "1" else pos2
                return l[pos]

        if opcode == 6:
            check = l[pos1] if pc != "1" else pos1
            if check == 0:
                l[pos] = l[pos2] if pb != "1" else pos2
                return l[pos]

        return pos + 3

    if opcode in (7, 8):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        pos3 = l[pos + 3]

        if opcode == 7:
            c = l[pos1] if pc != "1" else pos1
            b = l[pos2] if pb != "1" else pos2
            l[pos3] = 1 if c < b else 0

        if opcode == 8:
            c = l[pos1] if pc != "1" else pos1
            b = l[pos2] if pb != "1" else pos2
            l[pos3] = 1 if c == b else 0

        if pos3 != pos:
            return pos + 4

        return l[pos3]

if __name__ == "__main__":
    day = os.path.dirname(os.path.abspath(__file__)).rsplit("/", 1)[-1]
    _input = get_actual(day=int(day), year=2019)

    _input = ints(_input)

    try:
        pos = 0
        while True:
            pos = run_input(pos, _input, 5)
    except Halt as e:
        print(e)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import folium

# import data
train_data = pd.read_csv('train_users_2.csv')
test_data = pd.read_csv('test_users.csv')
countries = pd.read_csv('countries.csv')

sns.set_style("white", {'ytick.major.size': 10.0})
sns.set_style("whitegrid", {'axes.edgecolor': '0'})
sns.set_context("poster", font_scale=1.1)

print("We have", train_data.shape[0], "users in the training set and",
      test_data.shape[0], "in the test set.")
print("In total we have", train_data.shape[0] + test_data.shape[0], "users.")

# Remove 'id' feature
train_data.drop('id', axis=1, inplace=True)
train_data.head()
print(train_data.isnull().sum())

#------------------------ Missing value --------------------------
train_data.replace('-unknown-', np.nan, inplace=True)
print(train_data.isnull().sum())

train_nan = (train_data.isnull().sum() / train_data.shape[0]) * 100
train_nan1 = train_nan[train_nan > 0]

print("After check for the missing value, we find that date_first_booking have",
      int((train_data.date_first_booking.isnull().sum() / train_data.shape[0]) * 100),
      "% of missing values in the training data")

width=0.4
train_nan1.plot(kind='bar',width=width, color='c',position=0, label= 'features', rot=0)
plt.xlabel('features')
plt.ylabel('percentage of missing')
plt.show()
plt.clf()

#------------------------ Gender ---------------------------
train_data.gender.value_counts(dropna=False).plot(kind='bar', color='#7D9EC0', rot=0)
plt.xlabel('Gender')
plt.ylabel('Count')
plt.show()
plt.clf()

women = sum(train_data['gender'] == 'FEMALE')
men = sum(train_data['gender'] == 'MALE')
female_destinations =train_data.loc[train_data['gender'] == 'FEMALE', 'country_destination'].value_counts() / women * 100
male_destinations = train_data.loc[train_data['gender'] == 'MALE', 'country_destination'].value_counts() / men * 100
# Bar width
width = 0.4
male_destinations.plot(kind='bar', width=width, color='#96CDCD', position=0, label='Male', rot=0)
female_destinations.plot(kind='bar', width=width, color='#FFA38D', position=1, label='Female', rot=0)
plt.legend()
plt.xlabel('Destination Country')
plt.ylabel('Percentage')
sns.despine()
plt.show()
plt.clf()

#------------------------- Age --------------------------
train_data.age.describe()
print('After checking for age, we find that max of age feature ', train_data.age.max(), 'is year number instead of age number')

# Airbnb only allow 18 or older to create an account.
print(sum(train_data.age > 100))
print(sum(train_data.age < 18))

train_data[train_data.age > 100]['age'].describe()
train_data[train_data.age < 18]['age'].describe()

user_with_year_age_mask = train_data['age'] > 1000
train_data.loc[user_with_year_age_mask, 'age'] = 2015 - train_data.loc[user_with_year_age_mask, 'age']
train_data.loc[(train_data['age'] > 100) | (train_data['age'] < 18), 'age'] = np.nan

age_dist= sns.distplot(train_data.age.dropna(), color='#458B74')
plt.xlabel('Age')
sns.despine()
plt.clf()

data_nN= train_data[train_data['country_destination']!='NDF']
data_nNU = data_nN[data_nN['country_destination']!='US']
sns.boxplot(x='country_destination',y='age' , data=train_data)
plt.ylim(10,70)
plt.xlabel('Destination Country')
plt.ylabel('Age of Users')
sns.despine()
plt.clf()

#-------------------------------language--------------------------------
language_pre = train_data.language.value_counts() / train_data.shape[0] * 100
print('Percentage of users using English is ', language_pre[0].round(2),'%.')

#-----------------------------destination-------------------------------
destinations = train_data.country_destination.value_counts() / train_data.shape[0] * 100
print('Percentage of users have not book yet is ', destinations[0].round(2),
      '%. Percentage of users chose the US as their first destination is ', destinations[1].round(2), '%.')

sns.countplot(x="country_destination", data=train_data, order=list(train_data.country_destination.value_counts().keys()))
plt.xlabel('Destination Country')
plt.ylabel('Count')
sns.despine()
plt.clf()

sns.countplot(x="country_destination", data=data_nNU, order=list(data_nNU.country_destination.value_counts().keys()))
plt.xlabel('Destination Country')
plt.ylabel('Count')
sns.despine()
plt.clf()

destinations1 = train_data.country_destination.value_counts()
dest_f = pd.DataFrame({'country_destination':destinations1.index,'count':destinations1.values})
dest = pd.merge(dest_f, countries, on=('country_destination'))

m = folium.Map(location=[20, 0], tiles="Mapbox Bright", zoom_start=2)
for i in range(0, len(dest)):
    folium.Marker([dest.iloc[i]['lat_destination'], dest.iloc[i]['lng_destination']], popup=str(dest.iloc[i]['count'])).add_to(m)
m.save('lx_markers_on_folium_map1.html')

# ------------------------- Date features ---------------------------------
train_data['date_account_created'] = pd.to_datetime(train_data['date_account_created'])
train_data.date_account_created.value_counts().plot(kind='line', linewidth=1, color='#F4D03F')
plt.xlabel('account created date')
plt.show()
plt.clf()

train_data['date_first_active'] = pd.to_datetime(train_data['timestamp_first_active'], format='%Y%m%d%H%M%S')
date_first_active = train_data.date_first_active.apply(lambda x: datetime.datetime(x.year, x.month, x.day))
date_first_active.value_counts().plot(kind='line', linewidth=1, color='#BCEE68')
plt.xlabel('first active date')
plt.show()
plt.clf()

# -------------------------- categorical features ------------------------------
print((train_data.signup_method.value_counts() / train_data.shape[0]) * 100)
train_data.signup_method.value_counts().plot(kind='bar', rot=0, color='#9AC0CD')
plt.xlabel('sign up methods')
plt.ylabel('count')
plt.show()
plt.clf()

print((train_data.signup_flow.value_counts() / train_data.shape[0]) * 100)
train_data.signup_flow.value_counts().plot(kind='bar', rot=0, color='#9AC0CD')
plt.xlabel('sign up flow')
plt.ylabel('count')
plt.show()
plt.clf()

print((train_data.signup_app.value_counts() / train_data.shape[0]) * 100)
train_data.signup_app.value_counts().plot(kind='bar', rot=0, color='#9AC0CD')
plt.xlabel('sign up app')
plt.ylabel('count')
plt.show()
plt.clf()

print((train_data.affiliate_channel.value_counts() / train_data.shape[0]) * 100)
train_data.affiliate_channel.value_counts().plot(kind='bar', rot=0, color='#9AC0CD')
plt.xlabel('affiliate channel')
plt.ylabel('count')
plt.show()
plt.clf()

print((train_data.affiliate_provider.value_counts() / train_data.shape[0]) * 100)
train_data.affiliate_provider.value_counts().plot(kind='bar', rot=25, fontsize=10, color='#9AC0CD')
plt.xlabel('affiliate provider')
plt.ylabel('count')
plt.show()
plt.clf()

print((train_data.first_affiliate_tracked.value_counts() / train_data.shape[0]) * 100)
train_data.first_affiliate_tracked.value_counts().plot(kind='bar', rot=0, color='#9AC0CD')
plt.xlabel('first affiliate tracked')
plt.ylabel('count')
sns.despine()
plt.show()
plt.clf()

print((train_data.first_device_type.value_counts() / train_data.shape[0]) * 100)
train_data.first_device_type.value_counts().plot(kind='bar', rot=25, color='#9AC0CD')
plt.xlabel('first device type')
plt.ylabel('count')
sns.despine()
plt.show()
plt.clf()

print((train_data.first_browser.value_counts() / train_data.shape[0]) * 100)
train_data.first_browser.value_counts().plot(kind='bar', fontsize=10, color='#9AC0CD')
plt.xlabel('first browser')
plt.ylabel('count')
plt.show()
plt.clf()


def find_pairs(nums, Sum):
    if len(nums) <= 1 and nums[0] != Sum:
        return None

    table = dict()
    for num in nums:
        table[hash(num)] = num
    for num in nums:
        target = Sum - num
        if target in table:
            return (num, target)
    return None

if __name__ == "__main__":
    tuple = find_pairs(range(1, 20), 10)
    print(tuple)

    tuple2 = find_pairs([2,2], 4)
    print(tuple2)

    tuple3 = find_pairs([2], 4)
    print(tuple3)

    tuple4 = find_pairs([], 3)
    print(tuple4)


from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

session = tf.InteractiveSession()

tf.global_variables_initializer().run()

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    session.run(train_step, feed_dict={ x: batch_xs, y_: batch_ys })

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# print(session.run(accuracy, feed_dict={ x: mnist.test.images, y_: mnist.test.labels}))
# print(session.run(y, feed_dict={ x: mnist.test.images }))

class NeuralNetwork1(object):

    @classmethod
    def compute(cls, image):
        possibility = session.run(y, feed_dict={x: image })
        return possibility


# Задание-1:
# Написать программу, выполняющую операции (сложение и вычитание) с простыми дробями.
# Дроби вводятся и выводятся в формате:
# n x/y ,где n - целая часть, x - числитель, у - знаменатель.
# Дроби могут быть отрицательные и не иметь целой части, или иметь только целую часть.
# Примеры:
# Ввод: 5/6 + 4/7 (всё выражение вводится целиком в виде строки)
# Вывод: 1 17/42  (результат обязательно упростить и выделить целую часть)
# Ввод: -2/3 - -2
# Вывод: 1 1/3

fract_str = input('enter fraction operation "n x/y +- k m/s": ')
fr_lst = fract_str.split(' ')

if fr_lst.count('+') == 1:
    ops_indx = fr_lst.index('+')
    ops = 'add'

elif fr_lst.count('-') == 1:
    ops_indx = fr_lst.index('-')
    ops = 'subtract'

leftfr_int = '0'
leftfr_up = '0'
leftfr_lo = '1'
rightfr_int = '0'
rightfr_up = '0'
rightfr_lo = '1'

i = 0

while i < ops_indx:
    if fr_lst[i].count('/') == 1:
        leftfr_up = (fr_lst[i].split('/'))[0]
        leftfr_lo = (fr_lst[i].split('/'))[1]
    elif fr_lst[i].count('/') == 0:
        leftfr_int = fr_lst[i]
    i += 1

i = ops_indx + 1

while i < len(fr_lst):
    if fr_lst[i].count('/') == 1:
        rightfr_up = (fr_lst[i].split('/'))[0]
        rightfr_lo = (fr_lst[i].split('/'))[1]
    elif fr_lst[i].count('/') == 0:
        rightfr_int = fr_lst[i]
    i += 1

intp = 0
frup = 0
frlo = 1

dleftfr_int = int(leftfr_int)
dleftfr_up = int(leftfr_up)
dleftfr_lo = int(leftfr_lo)
drightfr_int = int(rightfr_int)
drightfr_up = int(rightfr_up)
drightfr_lo = int(rightfr_lo)
if dleftfr_int <0:
    dleftfr_up = -dleftfr_up
if drightfr_int <0:
    drightfr_up = -drightfr_up

if ops == 'add':
    intp = dleftfr_int + drightfr_int
    frlo = dleftfr_lo * drightfr_lo
    frup = dleftfr_up * drightfr_lo + drightfr_up * dleftfr_lo
    intp2 = frup // frlo
    frup2 = frup % frlo
    intp = intp + intp2
    print(intp, frup2, frlo)
    if frup2 != 0 or frlo != 1:
        outfr = '{} {}/{}'.format(intp, frup2, frlo)
    elif frup2 != 0 and frlo == 1:
        intp = intp + frup
        outfr = '{}'.format(intp)
    elif frup2 == 0:
        outfr = '{}'.format(intp)

elif ops == 'subtract':
    intp = dleftfr_int - drightfr_int
    frlo = dleftfr_lo * drightfr_lo
    frup = dleftfr_up * drightfr_lo - drightfr_up * dleftfr_lo
    intp2 = frup // frlo
    frup2 = frup % frlo
    intp = intp + intp2
    if frup2 != 0 or frlo != 1:
        outfr = '{} {}/{}'.format(intp, frup2, frlo)
    elif frup2 != 0 and frlo == 1:
        intp = intp + frup
        outfr = '{}'.format(intp)
    elif frup2 == 0:
        outfr = '{}'.format(intp)

print('Output: ',outfr)

# Задание-2:
# Дана ведомость расчета заработной платы (файл "data/workers").
# Рассчитайте зарплату всех работников, зная что они получат полный оклад,
# если отработают норму часов. Если же они отработали меньше нормы,
# то их ЗП уменьшается пропорционально, а за заждый час переработки
# они получают удвоенную ЗП, пропорциональную норме.
# Кол-во часов, которые были отработаны, указаны в файле "data/hours_of"

# Задание-3:
# Дан файл ("data/fruits") со списком фруктов.
# Записать в новые файлы все фрукты, начинающиеся с определенной буквы.
# Т.е. в одном файле будут все фрукты на букву “А”, во втором на “Б” и т.д.
# Файлы назвать соответственно.
# Пример имен файлов: fruits_А, fruits_Б, fruits_В ….
# Важно! Обратите внимание, что нет фруктов, начинающихся с некоторых букв.
# Напишите универсальный код, который будет работать с любым списком фруктов
# и распределять по файлам в зависимости от первых букв, имеющихся в списке фруктов.
# Подсказка:
# Чтобы получить список больших букв русского алфавита:
# print(list(map(chr, range(ord('А'), ord('Я')+1))))


# coding=utf-8
from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.utils import translation
from feincms.content.richtext.models import RichTextContent
from feincms.module.page.extensions.navigation import NavigationExtension
from feincms.module.page.models import Page
from django.utils.translation import ugettext_lazy as _
from gallery.models import CatalogContent

Page._meta.verbose_name = u'CMS'
Page._meta.verbose_name_plural = u'CMS'

Page.register_templates(
    {
        'title': u'Szablon strony głównej',
        'path': 'cms/home.html',
        'regions': (
            ('content_about', u'content_about'),
            ('content_contact', u'content_contact'),
            ('content_gallery', u'content_gallery'),
            ('content_demo', u'content_demo'),
            ('character', u'postać'),
        ),
    }, {
        'title': u'Szablon prosty',
        'path': 'cms/simple.html',
        'regions': (
            ('main', u'main'),
        ),
    }, {
        'title': u'Szablon prosty z ustami w tle',
        'path': 'cms/simple_with_mouth_bg.html',
        'regions': (
            ('main', u'main'),
        ),
    }, {
        'title': u'Rączki',
        'path': 'cms/hands.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    }, {
        'title': u'Usta',
        'path': 'cms/lips.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    }, {
        'title': u'Stopy',
        'path': 'cms/foots.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    },
)

Page.create_content_type(RichTextContent)
Page.create_content_type(CatalogContent)

class CurrentLanguageOnly(NavigationExtension):
    name = 'show entries in current language only'

    def children(self, page, **kwargs):
        return page.children.in_navigation().filter(language=translation.get_language())

Page.register_extensions('feincms.module.extensions.translations', 'feincms.module.page.extensions.navigation')

class CharacterContent(models.Model):
    name = models.CharField(_('name'), max_length=80, blank=True)
    mouth = models.ForeignKey(Page, related_name=_('mouth'), verbose_name=_('mouth'))
    body = models.ForeignKey(Page, related_name=_('body'), verbose_name=_('body'))
    hands = models.ForeignKey(Page, related_name=_('hands'), verbose_name=_('hands'))
    foots = models.ForeignKey(Page, related_name=_('foots'), verbose_name=_('foots'))

    class Meta:
        abstract = True
        verbose_name = _('Character content')
        verbose_name_plural = _('Characters contents')

    def render(self, **kwargs):
        return render_to_string('cms/_character.html', {'character': self, 'STATIC_URL': settings.STATIC_URL})

Page.create_content_type(CharacterContent)