import numpy as np
import tecplot as tp
from tecplot.exception import *
from tecplot.constant import *

#TODO: Make this work for 2D Cartesian. No Z.

def find_maximum(dataset, plot, zone, variable):
    #pull Tecplot 360 data set into Python Numpy
    arr = zone.values(variable)[:]

    #find node with variable max
    #use numpy.argmax() on the variable array to find the index of the maximum value
    node = np.argmax(arr)
    print('Node/Cell # of max value: '+ str(node))

    #Find XYZ position of node with max variable value
    x = zone.values(plot.axes.x_axis.variable)
    y = zone.values(plot.axes.y_axis.variable)
    try: z = zone.values(plot.axes.z_axis.variable)
    except: z = 0
    if zone.values(variable).location == ValueLocation.Nodal:
        print('Getting position of the node')
        x = x[node]
        y = y[node]
        try: z = z[node]
        except: z = 0
    else:
        print('Getting position of the cell center')
        nmap = zone.nodemap
        pts = nmap[node]
        x = np.average(x[:][pts])
        y = np.average(y[:][pts])
        try: z = np.average(z[:][pts])
        except: z = 0

    #max value in specified zone
    maxvar = zone.values(variable)[node]

    #max value for all zones
    #othermax = dataset.variable(varnum).max()
    print('Location: ({:.3g}, {:.3g}, {:.3g})'.format(x, y, z))
    print('Max value: {:.5g}'.format(maxvar))
    #print('Max value for all Zones: '+str(othermax))

    znname = 'MAXVARCALC 1D Zone'
    pointzone = dataset.zone(znname)
    if not pointzone:
        pointzone = dataset.add_ordered_zone(znname, 1)
        print("New Point Zone created.")

    pointzone.values(plot.axes.x_axis.variable)[0] = x
    pointzone.values(plot.axes.y_axis.variable)[0] = y
    try: pointzone.values(plot.axes.z_axis.variable)[0] = z
    except: None

    return pointzone, maxvar

def highlight_maximum(frame, plot, pointzone, variable, maxvar):

    #plot scatter for new point zone
    print("Turning off Scatter for all Zones.")
    # clever hack to use internal/undocumented pytecplot methods
    from tecplot.tecutil import sv
    oset = set(list(range(plot.num_fieldmaps)))
    tp.session.set_style(False, sv.FIELDMAP, sv.SCATTER, sv.SHOW,
                         uniqueid=frame.uid, objectset=oset)

    # this should be available in pytecplot 1.1
    #plot.fieldmaps().scatter.show = False

    # pure pytecplot API version (slow)
    #with tp.session.suspend():
    #    for item_index in range(0, dataset.num_zones):
    #        plot.fieldmap(item_index).scatter.show=False

    plot.show_scatter = True

    print("Applying Scatter for Point Zone.")
    fmap = plot.fieldmap(pointzone)
    fmap.show = True

    scatter = fmap.scatter
    scatter.show=True
    scatter.size=2
    scatter.color=Color.RedOrange
    scatter.symbol().shape=GeomShape.Diamond
    scatter.line_thickness=0.3

    #draw text box displaying max variable
    #TODO: Use python commands for text box, and overwrite any previous text boxes
    print("Adding text on plot for maximum value.")
    text = list(filter(lambda t: t.text_string.startswith('[MAXVARCALC] Maximum '), frame.texts()))
    msg = '[MAXVARCALC] Maximum {}: {:.3f}'.format(variable.name, maxvar)
    if text:
        text[-1].text_string = msg
    else:
        frame.add_text(msg, (3, 97), bold=False)

    #View Fit Surfaces
    try: plot.view.fit_surfaces(consider_blanking=True)
    except: None

if __name__ == '__main__':
    # Uncomment the following line to connect to a running instance of Tecplot 360:
    tp.session.connect()

    #NOTE: Run this script on a data set already loaded into Tecplot 360
    #plot
    frame = tp.active_frame()
    dataset = frame.dataset
    plot = frame.plot()

    #select zone and variable
    while True:
        zone_id = input('Zone (name or index): ')
        try:
            zone_id = int(zone_id) - 1
            if zone_id < 0:
                continue
        except: pass
        zone = dataset.zone(zone_id)
        if zone:
            break
        else:
            print('No zone found for', zone_id)

    while True:
        var_id = input('Variable (name or index): ')
        try:
            var_id = int(var_id) - 1
            if var_id < 0:
                continue
        except: pass
        variable = dataset.variable(var_id)
        if variable:
            break
        else:
            print('No variable found for', var_id)

    pointzone, maxvar = find_maximum(dataset, plot, zone, variable)
    highlight_maximum(frame, plot, pointzone, variable, maxvar)


import numpy as np
import matplotlib.pyplot as plt

DataTab = np.loadtxt("SFD_residual.dat", skiprows=1)
DataTab = DataTab[DataTab[:, 0].argsort()]
time = DataTab[:,0]
L2 = DataTab[:,2]

delta = input ("input filter width: delta =")
chi = input ("input dissipation control coefficient: chi =")

plt.semilogy(time, L2)
# plt.xlim ([0, 3000])
plt.ylim ([1e-10, 1e-1])
plt.xlabel ("time")
plt.ylabel ("residual")
plt.grid(visible=True, which='major', color='grey', linestyle='-')
plt.grid(visible=True, which='minor', color='grey', linestyle=':', alpha=0.2)
plt.savefig("d"+str(delta)+"x"+str(chi)+".jpg", dpi=600)
plt.show()


from rptest.clients.rpk import RpkTool
from rptest.clients.types import TopicSpec
from rptest.services.admin import Admin
from rptest.services.cluster import cluster
from rptest.tests.redpanda_test import RedpandaTest
from rptest.services.redpanda import SISettings, get_cloud_storage_type
from rptest.services.kgo_verifier_services import KgoVerifierProducer
from rptest.utils.si_utils import parse_s3_segment_path, quiesce_uploads, BucketView, NTP, NTPR

from ducktape.mark import matrix
from ducktape.utils.util import wait_until

import json
import random
import time

SCRUBBER_LOG_ALLOW_LIST = [
    # Attempts to compute the retention point for the cloud log will fail
    # after a spillover manifest is manually removed.
    r"cloud_storage - .* failed to download manifest {key_not_found}",
    r"cloud_storage - .* failed to download manifest.*cloud_storage::error_outcome:2",
    r"cloud_storage - .* Failed to materialize.*cloud_storage::error_outcome:2",
    r"cloud_storage - .* failed to seek.*cloud_storage::error_outcome:2",
    r"cloud_storage - .* Failed to compute time-based retention",
    r"cloud_storage - .* Failed to compute size-based retention",
    r"cloud_storage - .* Failed to compute retention",
    r"archival - .* Failed to compute archive retention",

    # The test removes a segment from the manifest for the scrubber to detect. A reupload
    # may be attempted somewhere after the base offset of the removed segment. The STM
    # manifest will refuse to apply the command and log the error below.
    r"cloud_storage - .* New replacement segment does not line up with previous segment",
    r"cluster - .* Can't add segment:"
]

class CloudStorageScrubberTest(RedpandaTest):
    scrub_timeout = 90
    partition_count = 3
    message_size = 16 * 1024  # 16KiB
    segment_size = 1024 * 1024  # 1MiB
    to_produce = 100 * 1024 * 1024  # 100MiB per partition
    topics = [TopicSpec(partition_count=partition_count)]

    def __init__(self, test_context):
        self.si_settings = SISettings(
            test_context,
            log_segment_size=self.segment_size,
            cloud_storage_spillover_manifest_max_segments=10,
            cloud_storage_housekeeping_interval_ms=1000 * 10,
            fast_uploads=True)

        super().__init__(
            test_context=test_context,
            extra_rp_conf={
                "cloud_storage_enable_scrubbing": True,
                "cloud_storage_scrubbing_interval_ms": 1000,
                "cloud_storage_scrubbing_interval_jitter_ms": 100,
                # Small quota forces partial scrubs
                "cloud_storage_background_jobs_quota": 30,
                # Disable segment merging since it can reupload
                # the deleted segment and remove the gap
                "cloud_storage_enable_segment_merging": False
            },
            si_settings=self.si_settings)

        self.bucket_name = self.si_settings.cloud_storage_bucket
        self.rpk = RpkTool(self.redpanda)

    def _produce(self):
        # Use a smaller working set for debug builds to keep the test timely
        if self.debug_mode:
            self.to_produce //= 2

        msg_count = self.to_produce * self.partition_count // self.message_size
        KgoVerifierProducer.oneshot(self.test_context,
                                    self.redpanda,
                                    self.topic,
                                    self.message_size,
                                    msg_count,
                                    batch_max_bytes=self.message_size * 8,
                                    timeout_sec=60)

        quiesce_uploads(self.redpanda, [self.topic], timeout_sec=60)

        def all_partitions_spilled():
            bucket = BucketView(self.redpanda)
            for pid in range(self.partition_count):
                spillover_metas = bucket.get_spillover_metadata(
                    NTP(ns="kafka", topic=self.topic, partition=pid))

                if len(spillover_metas) == 0:
                    self.logger.debug(f"{self.topic}/{pid} did not spill yet")
                    return False

            return True

        wait_until(all_partitions_spilled,
                   timeout_sec=60,
                   backoff_sec=10,
                   err_msg="Some or all partitions did not spill")

    def _collect_anomalies(self):
        anomalies_per_ntpr = {}

        admin = Admin(self.redpanda)
        for pid in range(self.partition_count):
            anomalies = admin.get_cloud_storage_anomalies(namespace="kafka",
                                                          topic=self.topic,
                                                          partition=pid)

            ntpr = NTPR(ns=anomalies["ns"],
                        topic=anomalies["topic"],
                        partition=anomalies["partition"],
                        revision=anomalies["revision_id"])

            if ("missing_partition_manifest" not in anomalies
                    and "missing_spillover_manifests" not in anomalies
                    and "missing_segments" not in anomalies
                    and "segment_metadata_anomalies" not in anomalies):
                anomalies = None

            anomalies_per_ntpr[ntpr] = anomalies

        return anomalies_per_ntpr

    def _assert_no_anomalies(self):
        anomalies_per_ntpr = self._collect_anomalies()
        for ntpr, anomalies in anomalies_per_ntpr.items():
            assert anomalies is None, f"{ntpr} reported unexpected anomalies: {anomalies}"

    def _delete_segment_and_await_anomaly(self):
        segment_metas = [
            meta for meta in self.cloud_storage_client.list_objects(
                self.bucket_name) if "log" in meta.key
        ]

        view = BucketView(self.redpanda)
        to_delete = random.choice(segment_metas)
        attempts = 1
        while view.is_segment_part_of_a_manifest(to_delete) == False:
            to_delete = random.choice(segment_metas)
            attempts += 1

            assert attempts < 100, "Too many attempts to find a segment to delete"

        self.logger.info(f"Deleting segment at {to_delete.key}")
        self.cloud_storage_client.delete_object(self.bucket_name,
                                                to_delete.key,
                                                verify=True)

        ntpr = parse_s3_segment_path(to_delete.key).ntpr

        self.logger.info(
            f"Waiting for missing segment anomaly {to_delete.key} to be reported"
        )

        def ntpr_missing_segment():
            anomalies_per_ntpr = self._collect_anomalies()
            self.logger.debug(f"Reported anomalies {anomalies_per_ntpr}")

            if ntpr not in anomalies_per_ntpr:
                return False

            if anomalies_per_ntpr[ntpr] is None:
                return False

            if "missing_segments" not in anomalies_per_ntpr[ntpr]:
                return False

            return to_delete.key in anomalies_per_ntpr[ntpr][
                "missing_segments"]

        wait_until(ntpr_missing_segment,
                   timeout_sec=self.scrub_timeout,
                   backoff_sec=2,
                   err_msg="Missing segment anomaly not reported")

    def _delete_spillover_manifest_and_await_anomaly(self):
        pid = random.randint(0, 2)
        view = BucketView(self.redpanda)
        spillover_metas = view.get_spillover_metadata(
            NTP(ns="kafka", topic=self.topic, partition=pid))
        to_delete = random.choice(spillover_metas)
        ntpr = to_delete.ntpr

        self.logger.info(f"Deleting spillover manifest at {to_delete.path}")
        self.cloud_storage_client.delete_object(self.bucket_name,
                                                to_delete.path,
                                                verify=True)

        self.logger.info(
            f"Waiting for missing spillover anomaly {to_delete.path} to be reported"
        )

        def ntpr_missing_spill_manifest():
            anomalies_per_ntpr = self._collect_anomalies()
            self.logger.debug(f"Reported anomalies {anomalies_per_ntpr}")

            if ntpr not in anomalies_per_ntpr:
                return False

            if anomalies_per_ntpr[ntpr] is None:
                return False

            if "missing_spillover_manifests" not in anomalies_per_ntpr[ntpr]:
                return False

            return to_delete.path in anomalies_per_ntpr[ntpr][
                "missing_spillover_manifests"]

        wait_until(ntpr_missing_spill_manifest,
                   timeout_sec=self.scrub_timeout,
                   backoff_sec=2,
                   err_msg="Missing spillover manifest anomaly not reported")

    def _assert_anomalies_stable_after_leader_shuffle(self):
        self.logger.info("Checking anomalies stay stable after leader shuffle")

        old_anomalies = self._collect_anomalies()
        assert any([
            a is not None for a in old_anomalies.values()
        ]), f"Expected anomalies on at least one ntpr, but got {old_anomalies}"

        admin = Admin(self.redpanda)
        for pid in range(self.partition_count):
            admin.transfer_leadership_to(namespace="kafka",
                                         topic=self.topic,
                                         partition=pid)
            admin.await_stable_leader(namespace='kafka',
                                      topic=self.topic,
                                      partition=pid)

        def anomalies_stable():
            anomalies = self._collect_anomalies()
            self.logger.debug(f"Reported anomalies {anomalies}")

            return anomalies == old_anomalies

        wait_until(
            anomalies_stable,
            timeout_sec=self.scrub_timeout,
            backoff_sec=2,
            err_msg="Reported anomalies changed after leadership shuffle")

    def _assert_anomalies_stable_after_restart(self):
        self.logger.info(
            "Checking anomalies stay stable after full cluster restart")

        old_anomalies = self._collect_anomalies()
        assert any([
            a is not None for a in old_anomalies.values()
        ]), f"Expected anomalies on at least one ntpr, but got {old_anomalies}"

        for node in self.redpanda.nodes:
            self.redpanda.stop_node(node)

        for node in self.redpanda.nodes:
            self.redpanda.start_node(node)

        def anomalies_stable():
            anomalies = self._collect_anomalies()
            self.logger.debug(f"Reported anomalies {anomalies}")

            return anomalies == old_anomalies

        wait_until(anomalies_stable,
                   timeout_sec=self.scrub_timeout,
                   backoff_sec=2,
                   err_msg="Reported anomalies changed after full restart")

    def _assert_segment_metadata_anomalies(self):
        self.logger.info(
            "Fudging manifest and waiting on segment metadata anomalies")

        view = BucketView(self.redpanda)
        manifest = view.manifest_for_ntp(topic=self.topic, partition=0)

        sorted_segments = sorted(manifest['segments'].items(),
                                 key=lambda entry: entry[1]['base_offset'])
        assert len(
            sorted_segments
        ) > 2, f"Not enough segments in manifest: {json.dumps(manifest)}"

        # Remove the metadata for the penultimate segment, thus creating an offset gap
        # for the scrubber to detect
        seg_to_remove_name, seg_to_remove_meta = sorted_segments[-2]
        manifest['segments'].pop(seg_to_remove_name)
        self.logger.info(f"Removing segment with meta {seg_to_remove_meta}")

        detect_at_seg_meta = sorted_segments[-1][1]
        self.logger.info(f"Anomaly should be detected at {detect_at_seg_meta}")

        # Forcefully reset the manifest. Remote writes are disabled first
        # to make this operation safe.
        json_man = json.dumps(manifest)
        self.logger.info(f"Re-setting manifest to:{json_man}")

        self.rpk.alter_topic_config(self.topic, 'redpanda.remote.write',
                                    'false')
        time.sleep(1)

        admin = Admin(self.redpanda)
        admin.unsafe_reset_cloud_metadata(self.topic, 0, manifest)

        self.rpk.alter_topic_config(self.topic, 'redpanda.remote.write',
                                    'true')

        ntpr = NTPR(ns="kafka",
                    topic=self.topic,
                    partition=0,
                    revision=seg_to_remove_meta["ntp_revision"])

        def gap_reported():
            anomalies_per_ntpr = self._collect_anomalies()
            self.logger.debug(f"Reported anomalies {anomalies_per_ntpr}")

            if ntpr not in anomalies_per_ntpr:
                return False

            if anomalies_per_ntpr[ntpr] is None:
                return False

            if "segment_metadata_anomalies" not in anomalies_per_ntpr[ntpr]:
                return False

            seg_meta_anomalies = anomalies_per_ntpr[ntpr][
                "segment_metadata_anomalies"]

            for meta in seg_meta_anomalies:
                if (meta["type"] == "offset_gap"
                        and meta["at_segment"]["base_offset"]
                        == detect_at_seg_meta["base_offset"]):
                    return True

        wait_until(
            gap_reported,
            # A new manifest needs to be uploaded, so the timeout is more generous
            timeout_sec=self.scrub_timeout + 10,
            backoff_sec=2,
            err_msg="Gap not reported")

    @cluster(num_nodes=4, log_allow_list=SCRUBBER_LOG_ALLOW_LIST)
    @matrix(cloud_storage_type=get_cloud_storage_type())
    def test_scrubber(self, cloud_storage_type):
        self._produce()
        self._assert_no_anomalies()
        self._delete_spillover_manifest_and_await_anomaly()
        self._delete_segment_and_await_anomaly()

        self._assert_segment_metadata_anomalies()

        self._assert_anomalies_stable_after_leader_shuffle()
        self._assert_anomalies_stable_after_restart()

        # This test deletes segments, spillover manifests
        # and fudges the manifest. rp-storage-tool also picks
        # up on some of these things.
        self.redpanda.si_settings.set_expected_damage(
            {"missing_segments", "metadata_offset_gaps"})


import torch
import numpy as np
from torch.autograd import Variable
from math import ceil
import random

def prepare_generator_batch(samples, start_letter=0, gpu=False):
    """
    Takes samples (a batch) and returns

    Inputs: samples, start_letter, cuda
        - samples: batch_size x seq_len (Tensor with a sample in each row)

    Returns: inp, target
        - inp: batch_size x seq_len (same as target, but with start_letter prepended)
        - target: batch_size x seq_len (Variable same as samples)
    """

    batch_size, seq_len = samples.size()

    inp = torch.zeros(batch_size, seq_len)
    target = samples
    inp[:, 0] = start_letter
    inp[:, 1:] = target[:, :seq_len-1]

    inp = Variable(inp).type(torch.LongTensor)
    target = Variable(target).type(torch.LongTensor)

    if gpu:
        inp = inp.cuda()
        target = target.cuda()

    return inp, target

def prepare_discriminator_data(pos_samples, neg_samples, gpu=False):
    """
    Takes positive (target) samples, negative (generator) samples and prepares inp and target data for discriminator.

    Inputs: pos_samples, neg_samples
        - pos_samples: pos_size x seq_len
        - neg_samples: neg_size x seq_len

    Returns: inp, target
        - inp: (pos_size + neg_size) x seq_len
        - target: pos_size + neg_size (boolean 1/0)
    """

    inp = torch.cat((pos_samples, neg_samples), 0).type(torch.LongTensor)
    target = torch.ones(pos_samples.size()[0] + neg_samples.size()[0])
    target[pos_samples.size()[0]:] = 0

    # shuffle
    perm = torch.randperm(target.size()[0])
    target = target[perm]
    inp = inp[perm]

    inp = Variable(inp)
    target = Variable(target)

    if gpu:
        inp = inp.cuda()
        target = target.cuda()

    return inp, target

def batchwise_sample(gen, num_samples, batch_size, seq_len):
    """
    Sample num_samples samples batch_size samples at a time from gen.
    Does not require gpu since gen.sample() takes care of that.
    """

    samples = []
    for i in range(int(ceil(num_samples/float(batch_size)))):
        samples.append(gen.sample(batch_size, seq_len))

    return torch.cat(samples, 0)[:num_samples]

def batchwise_oracle_nll(gen, oracle, num_samples, batch_size, max_seq_len, start_letter=0, gpu=False):
    s = batchwise_sample(gen, num_samples, batch_size)
    oracle_nll = 0
    for i in range(0, num_samples, batch_size):
        inp, target = prepare_generator_batch(s[i:i+batch_size], start_letter, gpu)
        oracle_loss = oracle.batchNLLLoss(inp, target) / max_seq_len
        oracle_nll += oracle_loss.data.item()

    return oracle_nll/(num_samples/batch_size)

def load_music_file(file):
    with open(file, 'r') as file:
        text = file.read()

    # get vocabulary set
    words = sorted(tuple(set(text.split())))
    n = len(words)

    # create word-integer encoder/decoder
    word2int = dict(zip(words, list(range(n))))
    int2word = dict(zip(list(range(n)), words))

    # encode all words in dataset into integers
    encoded = np.array([word2int[word] for word in text.split()])

    return n, word2int, int2word, encoded

def batch_music_samples(data, seq_len):
    batches = []
    i=0
    while i+seq_len <= len(data):
        batch = list(data[i:i+seq_len])
        batches.append(batch)
        i += seq_len
    return batches

def train_val_split(data, train_size):
    random.seed(1)
    random.shuffle(data)
    train = torch.LongTensor(data[:train_size])
    val = torch.LongTensor(data[train_size:])
    return train, val

def positive_sample(data, n):
    torch.tensor(data)
    sample_idx = random.sample(range(1, len(data)), n)
    return data[sample_idx]


def sendToArduino(sendStr, ser):
    ser.write(sendStr.encode())

# ======================================

def recvFromArduino(ser):
    startMarker = 60
    endMarker = 62

    ck = ""
    x = "z"  # any value that is not an end- or startMarker
    byteCount = -1  # to allow for the fact that the last increment will be one too many

    # wait for the start character
    while ord(x) != startMarker:
        x = ser.read()

    # save data until the end marker is found
    while ord(x) != endMarker:
        if ord(x) != startMarker:
            ck = ck + str(x)
            byteCount += 1
        x = str(ser.read(), 'utf-8')

    return(ck)

# ============================

def waitForArduino(ser):

   # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
   # it also ensures that any bytes left over from a previous message are discarded

    startMarker = 60
    endMarker = 62

    msg = ""
    while msg.find("Arduino is ready") == -1:

        while ser.inWaiting() == 0:
            pass

        msg = recvFromArduino(ser)

        print(msg)

# ======================================

def sendAlarmSignal(sig, ser):
    waitingForReply = False
    n = 0
    sendstr = buildSigMsg(sig)

    if waitingForReply == False:
        sendToArduino(sendstr, ser)
        waitingForReply = True

    if waitingForReply == True:

        while ser.inWaiting() == 0:
            pass

        dataRecvd = recvFromArduino(ser)
        # print("REC:  " + dataRecvd)
        waitingForReply = False

        # print("===========")

# ======================================

def buildSigMsg(sig):
    startMarker = "<"
    endMarker = ">"
    return startMarker + str(sig) + endMarker


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

# In[233]:

get_ipython().run_line_magic('matplotlib', 'inline')
# Necessary Packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Model and data processing packages
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import GridSearchCV

# In[242]:

np.random.seed(234)

# In[243]:

df = pd.read_csv('diabetes.csv')
df.head(5)

# In[244]:

df.shape

# In[245]:

X = df.drop('Outcome', axis=1).values
y = df['Outcome'].values

# In[246]:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42, stratify=y)

# In[247]:

neighbors = np.arange(1,20)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))

# In[248]:

for i,k in enumerate(neighbors):

    knn = KNeighborsClassifier(n_neighbors=k)

    knn.fit(X_train, y_train)

    train_accuracy[i] = knn.score(X_train, y_train)

    test_accuracy[i] = knn.score(X_test, y_test)

# In[249]:

plt.plot(neighbors, test_accuracy, label='Testing Accuracy')
plt.plot(neighbors, train_accuracy, label='Training Accuracy')
plt.legend()
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.title('KNN Varying Number of Neighbors')
plt.show()

# In[250]:

n_neighbors=18
knn = KNeighborsClassifier(n_neighbors)

# In[251]:

knn.fit(X_train, y_train)

# In[252]:

knn.score(X_test, y_test)

# In[253]:

y_pred = knn.predict(X_test)

# In[254]:

confusion_matrix(y_test, y_pred)

# In[255]:

pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)

# In[256]:

print(classification_report(y_test, y_pred))

# In[257]:

y_pred_proba = knn.predict_proba(X_test)[:,1]

# In[258]:

# FPR = False Positive Rate, TPR = True Positive Rate
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)

# In[259]:

plt.plot([0,1],[0,1], 'k--')
plt.plot(fpr, tpr, label='KNN')
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.title(f'KNN (n_neighbors={n_neighbors}) ROC Curve')
plt.show()

# In[260]:

roc_auc_score(y_test, y_pred_proba)

# In[261]:

param_grid = {'n_neighbors':np.arange(1,100)}

# In[262]:

knn = KNeighborsClassifier()
knn_cv = GridSearchCV(knn, param_grid, cv=5)
knn_cv.fit(X, y)

# In[263]:

knn_cv.best_score_

# In[264]:

knn_cv.best_params_

# In[265]:

n_neighbors=14
knn = KNeighborsClassifier(n_neighbors)

# In[266]:

knn.fit(X_train, y_train)

# In[267]:

knn.score(X_test, y_test)

# In[268]:

y_pred = knn.predict(X_test)

# In[269]:

confusion_matrix(y_test, y_pred)

# In[270]:

pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)

# In[272]:

print(classification_report(y_test, y_pred))

# In[273]:

y_pred_proba = knn.predict_proba(X_test)[:,1]

# In[274]:

fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)

# In[275]:

plt.plot([0,1],[0,1], 'k--')
plt.plot(fpr, tpr, label='KNN')
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.title(f'KNN (n_neighbors={n_neighbors}) ROC Curve')
plt.show()

# In[276]:

roc_auc_score(y_test, y_pred_proba)

# In[ ]:


#Define a dictionary that holds our pet's attributes
from cuddlypet import CuddlyPet
from pet import Pet
from toy import Toy

# cujo = Pet('Cujo the Dog')
# benji = Pet('Benji')
# print(cujo.name)
# print(benji.name)

# print(cujo.fullness)
# cujo.eat_food()
# print(cujo.fullness)

pet_name = input("What is your pet's name?: ")
pet_type = int(input('''
1. Pet
2. Cuddly Pet
Which type of pet would you like?
'''))

if pet_type == 1:
  pet = Pet(pet_name)
elif pet_type == 2:
  pet = CuddlyPet(pet_name)

# This will print out something obb b/c it's an object
print(pet)

while True:
  print(pet)
  print("""
  1. Feed Pet
  2. Play with Pet
  3. Do Nothing
  4. Give toy
  """)
  choice = int(input(": "))
  if choice == 1:
    #feed pet
    pet.eat_food()
  elif choice == 2:
    #play with the pet
    pet.get_love()
  elif choice == 4:
    #play with the pet
    pet.give_toy(Toy())
  # each time the loop runs the pet will need some attention
  pet.be_alive()
  if not pet.is_alive():
    print(pet)
    print(f"Rest in peace {pet.name}")
    break


import unittest
from utils import build_full_name_variations

class TestBuildFullNameVariations(unittest.TestCase):

    def test_build_full_name_variations(self):
        full_names_and_expectations = [
            ('Stohner', []),
            ('Stohner Jürgen', []),
            ('Stohner, Jürgen', [('Stohner', 'Jürgen')]),
            ('Pothier, Joël F.', [('Pothier', 'Joël F.'), ('Pothier', 'Joël')]),
            ('Pothier, Joël (F.)', [('Pothier', 'Joël (F.)'), ('Pothier', 'Joël')]),
            ('Pehlke-Milde, Jessica', [('Pehlke-Milde', 'Jessica'), ('Pehlke', 'Jessica'), ('Milde', 'Jessica')]),
            ('von Wyl, Agnes', [('von Wyl', 'Agnes'), ('von-Wyl', 'Agnes'), ('von', 'Agnes'), ('Wyl', 'Agnes')]),
            ('Föhn, Martina', [('Föhn', 'Martina')]),
            ('Foo, Bazhong (Frank)', [('Foo', 'Bazhong (Frank)'), ('Foo', 'Bazhong-Frank'), ('Foo', 'Bazhong'), ('Foo', 'Frank')])
        ]

        for fullname_and_expectation in full_names_and_expectations:
            full_name, expectation = fullname_and_expectation
            with self.subTest(full_name=full_name, expectation=expectation):
                self.assertEqual(build_full_name_variations(full_name), expectation, f"Expectation for {full_name} not matched.")

if __name__ == '__main__':
    unittest.main()


from datetime import date
import ast
from time import sleep

a = open("dadoscadastro.txt", "a+")
a.close()

def linha(tam=42):
    return '-' * tam

def cabecalho(txt):
    print(linha())
    print(txt.center(42))
    print(linha())

def leiaInt(msg):
    while True:
        try:
            n = int(input(msg))
        except (ValueError, TypeError):
            print('\033[31mERRO: por favor, digite um número inteiro valido.\033[m')
        except KeyboardInterrupt:
            print('\033[31mO usuario preferiu não informar o número.\033[m')
        else:
            return n

def leiastr(msg):
    while True:
        try:
            n = str(input(msg))
        except (ValueError, TypeError):
            print('\033[31mERRO! Digite um Sexo valido, Apenas M ou F\033[m')
        except KeyboardInterrupt:
            print('\033[31mERRO! Digite um Sexo valido, Apenas M ou F\033[m')
        else:
            return n

arquivo = open("dadoscadastro.txt", "r")
linhas = arquivo.readlines()
arquivo.close()
codigo = 0
for linhass in linhas:
    codigo += 1

def Cadastro(nome='<desconhecido>', idade=0, sexo='não informado'):

    arquivo = open("dadoscadastro.txt", "a")
    nome = input('Nome: ').upper()
    nascimento = leiaInt('Ano de nascimento: ')
    atual = date.today().year
    idade = atual - nascimento
    sexo = str(input('Sexo: [M/F] ')).upper()[0]
    while sexo not in 'MF':
        print('\033[31mERRO! Digite um Sexo valido, Apenas M ou F\033[m')
        sexo = str(input('Sexo: [M/F] ')).upper()[0]

    email = str(input('Digite o email: '))
    salario = float(input('Salario do funcionario: R$').replace(',', '').replace('.', ''))

    usuario = {"nome": nome,"data de nascimento": nascimento, "idade": idade, "sexo": sexo, "email": email, "salario": f'{salario:.2f}, "codigo": {codigo}'}
    arquivo.write(str(usuario) + "\n")
    arquivo.close()

    print(linha())
    print('\033[32mCadastrado com sucesso.\033[m')
    print(linha())

def ler_arquivo():
    print(linha())
    print('<FUNCIONARIOS CADASTRADOS>'.center(42))
    print(linha())
    arquivo = open("dadoscadastro.txt", "r")
    linhas = arquivo.readlines()
    arquivo.close()
    indice = 0
    for linhass in linhas:
        print(f'{linhass}')
        indice += 1

    if len(linhas) == 0:
        print('Não Existe Registros aqui.')

def remover_cadastro():
    codigo = leiaInt('Digite o código do funcionário a ser removido: ')
    arquivo = open("dadoscadastro.txt", "r")
    linhas = arquivo.readlines()
    arquivo.close()
    indice = 0
    encontrado = False
    for linha in linhas:
        if f'"codigo": {codigo}' in linha:
            encontrado = True
            break
        indice += 1
    if encontrado:
        del linhas[indice]
        arquivo = open("dadoscadastro.txt", "w")
        arquivo.writelines(linhas)
        arquivo.close()
        print('-'*42)
        print(f'\033[32mFuncionário com código {codigo} removido com sucesso.\033[m')
        print('-'*42)
    else:
        print('-'*42)
        print(f'\033[31mNão foi encontrado nenhum funcionário com código {codigo}.\033[m')
        print('-'*42)

while True:
    print(linha())
    print('CADASTRO DE FUNCIONARIOS DA EMPRESA'.center(42))
    print(linha())

    print('[1] - CADASTRAR NOVA PESSOA\n'
            '[2] - VER LISTA DE FUNCIONARIOS CADASTRADOS\n'
            '[3] - REMOVER CADASTRO\n'
            '[4] - SAIR')

    print(linha())
    opc = leiaInt('Digite sua opção: ')
    print(linha())

    while opc != 1 and opc != 2 and opc != 3 and opc != 4:
        print('\033[31mERRO!Digite uma opção valida!\033[m')
        opc = leiaInt('Digite sua opção: ')
    if opc == 1:
        Cadastro()
    if opc == 2:
        ler_arquivo()
    if opc == 3:
        remover_cadastro()
    if opc == 4:
        print('\033[97mUM MOMENTO...\033[m')
        sleep(3)
        print('\033[33mATÉ LOGO, VOLTE SEMPRE!\033[m')
        break


# -*- coding: utf-8 -*-
"""
Created on  2018/5/18 13:30
@author: lhua
"""
import imp
import sys

imp.reload(sys)
import numpy as np
import pandas as pd
import jieba
import re
import random
from gensim.models import Word2Vec
from keras.preprocessing import sequence
from gensim.corpora.dictionary import Dictionary
import multiprocessing

from keras.layers import BatchNormalization
from keras.models import Model
from keras.utils import to_categorical
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import yaml
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
from keras.layers import Convolution1D, GlobalMaxPooling1D, MaxPooling1D, LSTM, GRU,Input, merge, Merge, Flatten
from keras.layers.core import Dense, Dropout, Activation
from keras.models import model_from_yaml
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping, ModelCheckpoint

import tensorflow as tf

import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'

np.random.seed(1337)  # For Reproducibility
# the dimension of word vector
vocab_dim = 64
# sentence length
maxlen = 100
# iter num
n_iterations = 1
# the number of words appearing
n_exposures = 10
# what is the maximum distance between the current word and the prediction word in a sentence, what is the maximum distance between the current and the prediction word in a sentence
window_size = 7
# batch size
batch_size = 32
# epoch num
n_epoch = 20
# input length
input_length = 100
# multi processing cpu number
cpu_count = multiprocessing.cpu_count()

#source
sourecefileneg = '../data/computerneg.xls'
sourecefilepos = '../data/computerpos.xls'

#target
targetfileneg = '../data/hotelneg.xls'
targetfilepos = '../data/hotelpos.xls'

# loading training file
def loadfile():
    sourceneg = pd.read_excel(sourecefileneg, header=None, index=None)
    sourcepos = pd.read_excel(sourecefilepos, header=None, index=None)
    targetneg = pd.read_excel(targetfileneg, header=None, index=None)
    targetpos = pd.read_excel(targetfilepos, header=None, index=None)
    #merge all data
    sourceneg = np.array(sourceneg[0])
    sourcepos = np.array(sourcepos[0])
    targetneg = np.array(targetneg[0])
    targetpos = np.array(targetpos[0])
    return sourceneg,sourcepos,targetneg,targetpos

#generating set of disused words
def getstopword(stopwordPath):
    stoplist = set()
    for line in stopwordPath:
        stoplist.add(line.strip())
        # print line.strip()
    return stoplist

#divide the sentence and remove the disused words
def wordsege(text):
    # get disused words set
    stopwordPath = open('../data/stopword.txt', 'r',encoding='utf-8')
    stoplist = getstopword(stopwordPath)
    stopwordPath.close()

    # divide the sentence and remove the disused words with jieba,return list
    text_list = []
    for document in text:

        seg_list = jieba.cut(document.strip())
        # seg_list = list(document.strip())
        fenci = []

        for item in seg_list:
            if item not in stoplist and re.match(r'-?\d+\.?\d*', item) == None and len(item.strip()) > 0:
                fenci.append(item)
        # if the word segmentation of the sentence is null,the label of the sentence should be deleted accordingly
        if len(fenci) > 0:
            text_list.append(fenci)
    return text_list

def tokenizer(neg, post,tarneg,tarpost):
    neg_sege = wordsege(neg)
    post_sege = wordsege(post)
    tarneg_sege = wordsege(tarneg)
    tarpost_sege = wordsege(tarpost)
    traincombined = np.concatenate((post_sege,neg_sege))
    textcombined = np.concatenate((tarpost_sege,tarneg_sege))
    # generating label and meging label data
    train_y = np.concatenate((np.ones(len(post_sege), dtype=int), np.zeros(len(neg_sege), dtype=int)))
    text_y = np.concatenate((np.ones(len(tarpost_sege), dtype=int), np.zeros(len(tarneg_sege), dtype=int)))
    return traincombined,train_y,textcombined,text_y

# create a dictionary of words and phrases,return the index of each word,vector of words,and index of words corresponding to each sentence
def create_dictionaries(model=None,
                        combined=None):
    ''' Function does are number of Jobs:
        1- Creates a word to index mapping
        2- Creates a word to vector mapping
        3- Transforms the Training and Testing Dictionaries
    '''
    if (combined is not None) and (model is not None):
        gensim_dict = Dictionary()
        gensim_dict.doc2bow(model.wv.vocab.keys(),
                            allow_update=True)
        # the index of a word which have word vector is not 0
        w2indx = {v: k + 1 for k, v in gensim_dict.items()}
        # integrate all the corresponding word vectors into the word vector matrix
        w2vec = {word: model[word] for word in w2indx.keys()}

        # a word without a word vector is indexed 0,return the index of word
        def parse_dataset(combined):
            ''' Words become integers
            '''
            data = []
            for sentence in combined:
                new_txt = []
                for word in list(sentence):
                    try:
                        new_txt.append(w2indx[word])
                    except:
                        new_txt.append(0)
                data.append(new_txt)
            return data

        combined = parse_dataset(combined)
        # unify the length of the sentence with the pad_sequences function of keras
        combined = sequence.pad_sequences(combined, maxlen=maxlen)
        # return index, word vector matrix and the sentence with an unifying length and indexed
        return w2indx, w2vec, combined
    else:
        print('No data provided...')

# the training of the word vector
def word2vec_train(combined):
    # model = Word2Vec(size=vocab_dim,
    #                  min_count=n_exposures,
    #                  window=window_size,
    #                  workers=cpu_count,
    #                  iter=n_iterations)
    # # build the vocabulary dictionary
    # model.build_vocab(combined)
    # # train the word vector model
    # model.train(combined, total_examples=model.corpus_count, epochs=50)
    # # save the trained model
    # model.save('data/Word2vec_model.pkl')
    # index, word vector matrix and the sentence with an unifying length and indexed based on the trained model
    word2model = Word2Vec.load('../model/Word2vec_new_model.pkl')
    index_dict, word_vectors, combined = create_dictionaries(model=word2model, combined=combined)

    return index_dict, word_vectors, combined

def get_data(index_dict, word_vectors, combined, y):
    # total number of word including the word without word vector
    n_symbols = len(index_dict) + 1
    # build word vector matrix which corresponding to the word index one by one
    embedding_weights = np.zeros((n_symbols, vocab_dim))
    for word, index in index_dict.items():  # 从索引为1的词语开始，对每个词语对应其词向量
        embedding_weights[index, :] = word_vectors[word]
    # partition test set and training set
    # x_train, x_test, y_train, y_test = train_test_split(combined, y, test_size=0.2)
    x_train = combined[:4000]
    x_test = combined[4000:]
    y_train = y[:4000]
    y_test = y[4000:]
    print(x_train.shape, y_train.shape)
    print(x_test.shape, y_test.shape)
    # return the input parameters needed of the lstm model
    return n_symbols, embedding_weights, x_train, y_train, x_test, y_test

def training_vis(hist):
    loss = hist.history['loss']
    val_loss = hist.history['val_loss']
    acc = hist.history['acc']
    val_acc = hist.history['val_acc']

    # make a figure
    fig = plt.figure(figsize=(8,4))
    # subplot loss
    ax1 = fig.add_subplot(121)
    ax1.plot(loss,label='train_loss')
    ax1.plot(val_loss,label='val_loss')
    ax1.set_xlabel('Epochs')
    ax1.set_ylabel('Loss')
    ax1.set_title('Loss on Training and Validation Data')
    ax1.legend()
    # subplot acc
    ax2 = fig.add_subplot(122)
    ax2.plot(acc,label='train_acc')
    ax2.plot(val_acc,label='val_acc')
    ax2.set_xlabel('Epochs')
    ax2.set_ylabel('Accuracy')
    ax2.set_title('Accuracy  on Training and Validation Data')
    ax2.legend()
    plt.tight_layout()
    plt.show()

##定义网络结构
def train_lstm(n_symbols, embedding_weights, x_train, y_train, x_test, y_test):
    print('Defining a Simple Keras Model...')
    model = Sequential()  # or Graph or whatever
    # model.add(Input(shape=(input_length,)))
    model.add(Embedding(output_dim=vocab_dim,
                        input_dim=n_symbols,
                        # mask_zero=True,
                        weights=[embedding_weights],
                        input_length=input_length,trainable=False))  # Adding Input Length
    model.add(Convolution1D(256, vocab_dim, border_mode='same', subsample_length=1,trainable=True))
    model.add(Activation('relu'))
    model.add(GlobalMaxPooling1D())
    model.add(Dropout(0.5))
    model.add(Dense(1, activation='sigmoid'))
    print ('Compiling the Model...')
    model.compile(loss='binary_crossentropy',
                  optimizer='adam', metrics=['accuracy'])
    best_weights_filepath = '../model/best_lstm.h5'
    saveBestModel = ModelCheckpoint(best_weights_filepath, monitor='val_acc', verbose=1, save_best_only=True,
                                    mode='auto')

    model.summary()
    print ("Train...")
    history = model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=n_epoch, verbose=1,validation_data=(x_test, y_test),callbacks=[saveBestModel])

    print ("Evaluate...")
    score = model.evaluate(x_test, y_test,
                           batch_size=batch_size)
    # save the trained lstm model
    yaml_string = model.to_yaml()
    with open('../model/lstm.yml', 'w') as outfile:
        outfile.write(yaml.dump(yaml_string, default_flow_style=True))
    model.save_weights('../model/lstm.h5')
    print ('Test score:', score)
    print (training_vis(history))

# 训练模型，并保存
def train():
    print ('Loading Data...')
    sourceneg, sourcepos, targetneg, targetpos= loadfile()
    print('Tokenising...')
    traincombined, train_y, testcombined, text_y = tokenizer(sourceneg, sourcepos,targetneg, targetpos)
    print(len(traincombined), len(train_y),len(testcombined),len(text_y))
    sumcombined = np.concatenate((traincombined,testcombined))
    sumlabel = np.concatenate((train_y,text_y))
    print('Training a Word2vec model...')
    index_dict, word_vectors, combined = word2vec_train(sumcombined)
    model = Word2Vec.load('../model/Word2vec_new_model.pkl')
    _, _, traincombined = create_dictionaries(model,traincombined)
    _, _, sumcombined = create_dictionaries(model,sumcombined)
    print(combined)
    print('Setting up Arrays for Keras Embedding Layer...')
    # n_symbols, embedding_weights, x_train, y_train, x_test, y_test = get_data(index_dict, word_vectors, sumcombined, sumlabel)
    n_symbols, embedding_weights, x_train, y_train, x_test, y_test = get_data(index_dict, word_vectors, sumcombined, sumlabel)
    print(y_test)
    print(x_train.shape, y_train.shape)
    # train_lstm(n_symbols, embedding_weights, x_train, y_train, x_test, y_test)
    train_DANN( x_train, y_train, x_test, y_test,enable_dann = True)

# building the input format data
def input_transform(string):
    words = list(jieba.cut(string))
    # reshape the list to bilayer list
    words = np.array(words).reshape(1, -1)
    model = Word2Vec.load('../model/Word2vec_model.pkl')
    # create a dictionary of words and phrases,return the index of each word,vector of words,and index of words corresponding to each senten
    _, _, combined = create_dictionaries(model, words)
    return combined

def lstm_predict(string):
    print('loading model......')
    with open('../model/lstm.yml', 'r') as f:
        yaml_string = yaml.load(f)
    model = model_from_yaml(yaml_string)

    print('loading weights......')
    model.load_weights('../model/lstm.h5')
    model.compile(loss='binary_crossentropy',
                  optimizer='adam', metrics=['accuracy'])
    data = input_transform(string)
    data.reshape(1, -1)
    print(data)
    # predict the new data
    result = model.predict_classes(data)
    if result[0][0] == 1:
        print(string, ' positive')
    else:
        print(string, ' negative')

def build_models(n_neurons):
    """Creates three different models, one used for source only training, two used for domain adaptation"""
    inputs = Input(shape=(100,))
    x3 = Convolution1D(128, vocab_dim, border_mode='same', subsample_length=1)(inputs)
    x3 = Activation("relu")(x3)
    x4 = Dense(n_neurons, activation='linear')(x3)
    x4 = BatchNormalization()(x4)
    x4 = Activation("relu")(x4)

    source_classifier = Dense(2, activation='softmax', name="mo")(x4)
    domain_classifier = Dense(16, activation='linear', name="do4")(x4)
    domain_classifier = BatchNormalization(name="do5")(domain_classifier)
    domain_classifier = Activation("relu", name="do6")(domain_classifier)
    domain_classifier = Dropout(0.5)(domain_classifier)

    domain_classifier = Dense(2, activation='softmax', name="do")(domain_classifier)

    comb_model = Model(inputs=inputs, outputs=[source_classifier, domain_classifier])
    comb_model.compile(optimizer="Adam",
                       loss={'mo': 'categorical_crossentropy', 'do': 'categorical_crossentropy'},
                       loss_weights={'mo': 1, 'do': 2}, metrics=['accuracy'], )

    source_classification_model = Model(inputs=inputs, outputs=[source_classifier])
    source_classification_model.compile(optimizer="Adam",
                                        loss={'mo': 'categorical_crossentropy'}, metrics=['accuracy'], )

    domain_classification_model = Model(inputs=inputs, outputs=[domain_classifier])
    domain_classification_model.compile(optimizer="Adam",
                                        loss={'do': 'categorical_crossentropy'}, metrics=['accuracy'])

    embeddings_model = Model(inputs=inputs, outputs=[x4])
    embeddings_model.compile(optimizer="Adam", loss='categorical_crossentropy', metrics=['accuracy'])

    return comb_model, source_classification_model, domain_classification_model, embeddings_model

def batch_generator(data, batch_size):
    """Generate batches of data.

    Given a list of numpy data, it iterates over the list and returns batches of the same size
    This
    """
    all_examples_indices = len(data[0])
    while True:
        mini_batch_indices = np.random.choice(all_examples_indices, size=batch_size, replace=False)
        tbr = [k[mini_batch_indices] for k in data]
        yield tbr

def train_DANN(Xs, ys, Xt, yt, enable_dann=True, n_iterations=15000):
    # batch_size = 64

    model, source_classification_model, domain_classification_model, embeddings_model = build_models(64)

    y_class_dummy = np.ones((len(Xt), 2))
    y_adversarial_1 = to_categorical(np.array(([1] * batch_size + [0] * batch_size)))

    sample_weights_class = np.array(([1] * batch_size + [0] * batch_size))
    sample_weights_adversarial = np.ones((batch_size * 2,))

    S_batches = batch_generator([Xs, to_categorical(ys)], batch_size)
    T_batches = batch_generator([Xt, np.zeros(shape=(len(Xt), 2))], batch_size)

    for i in range(n_iterations):
        # print(y_class_dummy.shape, ys.shape)
        y_adversarial_2 = to_categorical(np.array(([0] * batch_size + [1] * batch_size)))

        X0, y0 = next(S_batches)
        X1, y1 = next(T_batches)

        X_adv = np.concatenate([X0, X1])
        y_class = np.concatenate([y0, np.zeros_like(y0)])

        adv_weights = []
        for layer in model.layers:
            if (layer.name.startswith("do")):
                adv_weights.append(layer.get_weights())

        if (enable_dann):
            # note - even though we save and append weights, the batchnorms moving means and variances
            # are not saved throught this mechanism
            stats = model.train_on_batch(X_adv, [y_class, y_adversarial_1],
                                         sample_weight=[sample_weights_class, sample_weights_adversarial])

            k = 0
            for layer in model.layers:
                if (layer.name.startswith("do")):
                    layer.set_weights(adv_weights[k])
                    k += 1

            class_weights = []

            for layer in model.layers:
                if (not layer.name.startswith("do")):
                    class_weights.append(layer.get_weights())

            stats2 = domain_classification_model.train_on_batch(X_adv, [y_adversarial_2])

            k = 0
            for layer in model.layers:
                if (not layer.name.startswith("do")):
                    layer.set_weights(class_weights[k])
                    k += 1

        else:
            source_classification_model.train_on_batch(X0, y0)

        if ((i + 1) % 1000 == 0):
            # print(i, stats)
            y_test_hat_t = source_classification_model.predict(Xt).argmax(1)
            y_test_hat_s = source_classification_model.predict(Xs).argmax(1)
            print("Iteration %d, source accuracy =  %.3f, target accuracy = %.3f" % (
            i, accuracy_score(ys, y_test_hat_s), accuracy_score(yt, y_test_hat_t)))
    return embeddings_model

def predict():
    sum = np.zeros(2000).reshape(2000,1)
    targetneg = pd.read_excel(targetfileneg, header=None, index=None)

    targetpos = pd.read_excel(targetfilepos, header=None, index=None)
    targetneg = np.random.permutation(np.array(targetneg[0]))
    targetpos = np.random.permutation(np.array(targetpos[0]))

    for  i in range(5):
        print('loading model......')
        with open('../model/lstm.yml', 'r') as f:
            yaml_string = yaml.load(f)
        model = model_from_yaml(yaml_string)
        print('loading weights......')
        model.load_weights('../model/best_lstm.h5')
        model.compile(loss='binary_crossentropy',
                      optimizer='adam', metrics=['accuracy'])
        word2model = Word2Vec.load('../model/Word2vec_new_model.pkl')

        for i in range(len(model.layers)):
            print(model.layers[i].name, i, model.layers[i].trainable)
        for i in range(4):
            model.layers[i].trainable = False
        sgd = SGD(lr=1e-4, decay=1e-4, momentum=0.9, nesterov=True)
        model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])

        targetneg_sege = wordsege(targetneg)
        targetpos_sege = wordsege(targetpos)
        texttarget_x = np.concatenate((targetneg_sege, targetpos_sege))
        texttarget_y = np.concatenate((np.zeros(len(targetneg_sege), dtype=int), np.ones(len(targetpos_sege), dtype=int)))
        # print(texttarget.reshape(-1,1).shape)
        # model.predict_classes(texttarget)
        texttarget_x.reshape(-1, 1)
        print(texttarget_x)
        _, _, combined = create_dictionaries(word2model, texttarget_x)
        print(combined)
        print('输出最好的模型结果：')
        print(model.evaluate(combined, texttarget_y))
        print('加入text进行微调：')
        # 训练抽取一半数据进行训练
        train_target_x = np.concatenate((combined[0:1000],combined[2000:3000]))
        train_target_y = np.concatenate((texttarget_y[0:1000],texttarget_y[2000:3000]))
        text_target_x =  np.concatenate((combined[1000:2000],combined[3000:]))
        text_target_y = np.concatenate((texttarget_y[1000:2000], texttarget_y[3000:]))
        print(text_target_y.shape)

        history = model.fit(train_target_x, train_target_y, batch_size=batch_size, nb_epoch=n_epoch, shuffle=True,
                  validation_data=(text_target_x, text_target_y), verbose=1)
        print('生成随机数')
        s = np.array(random.sample(list(combined),100))
        print (s)
        model.save_weights('../model/finll_lstm.h5')
        print(model.evaluate(text_target_x, text_target_y))
        print(model.predict(text_target_x))
        sum += model.predict(text_target_x)
        print(training_vis(history))
    result = ((1*((sum/5)>0.5)).T^text_target_y).sum()
    print(1-result/2000)

if __name__ == '__main__':
    train()
    # predict()

    # lstm_predict(string)


# -*- coding: utf-8 -*-
"""Installer for the collective.contact.widget package."""

from setuptools import find_packages
from setuptools import setup

long_description = (
    open('README.rst').read()
    + '\n' +
    'Contributors\n'
    '============\n'
    + '\n' +
    open('CONTRIBUTORS.rst').read()
    + '\n' +
    open('CHANGES.rst').read()
    + '\n')

setup(
    name='collective.contact.widget',
    version='1.14.dev0',
    description="Contact widget",
    long_description=long_description,
    # Get more from http://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Web Environment",
        "Framework :: Plone",
        "Framework :: Plone :: 4.2",
        "Framework :: Plone :: 4.3",
        "License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2.7",
    ],
    keywords='plone contact widget field',
    author='Vincent Fretin',
    author_email='vincentfretin@ecreall.com',
    url='https://github.com/collective/collective.contact.widget',
    download_url='https://pypi.org/project/collective.contact.widget',
    license='GPL',
    packages=find_packages('src', exclude=['ez_setup']),
    namespace_packages=['collective', 'collective.contact'],
    package_dir={'': 'src'},
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'five.grok',
        'setuptools',
        'collective.contact.core >= 1.27',
        'future',
        'plone.api',
        'plone.formwidget.contenttree >= 1.0.11',
        'plone.formwidget.autocomplete',
        'z3c.relationfield',
    ],
    extras_require={
        'test': [
            'ecreall.helpers.testing',
            'plone.app.testing',
        ],
    },
    entry_points="""
    """,
)


import requests
import pygame

BASE_URL = "https://www.random.org/"

def get_nums_array(size, minimum, maximum, col=1, base=10):
    if size > 10000:
        size = 10000
    full_url = BASE_URL + 'integers/?num=' + str(size) + '&min=' + \
        str(minimum) + '&max=' + str(maximum) + '&col=' + str(col) + \
        '&base=' + str(base) + '&format=plain&rnd=new'
    print(full_url)
    request = requests.get(full_url)
    numbers_list = []
    for num in request.content:
        numbers_list.append(num)
    print(numbers_list)
    return numbers_list

def generate_random_image(x_size, y_size, canvas):
    total_pixels = x_size * y_size
    pixArr = pygame.PixelArray(canvas)
    color_nums = get_nums_array(total_pixels, 0, 255)
    color_iter = 0
    x_pos = 0
    y_pos = 0
    for i in range(0, total_pixels):
        # color = pygame.Color.hsla((code, 100, 100, 100))
        # Draw on each pixel
        color = ''
        color_range_check = (i - (len(color_nums) * color_iter)) * 3
        print(color_range_check)
        if color_range_check + 2 >= len(color_nums):
            get_nums_array(total_pixels, 0, 255)
            color_iter += 1
        color = [color_nums[(i - (len(color_nums) * color_iter)) * 3 + j] for j in range(0, 3)]
        if x_pos >= x_size:
            y_pos += 1
            x_pos = 0
        pixArr[x_pos][y_pos] = (color[0], color[1], color[2])

if __name__ == '__main__':
    pygame.init()
    size = width, height = 128, 128
    white = 255, 255, 255
    screen = pygame.display.set_mode(size)
    generate_random_image(width, height, screen)


# librerias basicas
import torch, torchvision
import torch.backends.cudnn as cudnn
from torchvision import transforms, datasets

import mmdet
from mmcv.ops import get_compiling_cuda_version, get_compiler_version

# librerias para el detector de mascaras
import mmcv
from mmcv.runner import load_checkpoint

from mmdet.apis import inference_detector, show_result_pyplot
from mmdet.models import build_detector
from mmdet.datasets.pipelines import Compose

# profundidad
from models.model import GLPDepth
#tracker
from tracker.byte_tracker import BYTETracker

#librerias de fichero
import os, sys, errno

import glob
import argparse

# librerias para trar con la imagen
import numpy as np
import PIL.Image as pil
import matplotlib as mpl
import matplotlib.cm as cm

import cv2
from collections import OrderedDict

# tiempo
from timer import Timer

class Me_model():

    def __init__ (self,detector,depth,tracker,camera=[0,0,1],device=None,half_img_mm=True):
        """
        detector : argumentos detector
        depth : argumentos depth
        tracker :argumentos tracker
        camera : punto de convergencia de lines de perpectiva
        """
        torch.cuda.empty_cache()
        # Cargamos los modelos necesarios

        # Set the device to be used for evaluation

        if device:
            self.device=device
        else:
            self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        # Load the config
        config = mmcv.Config.fromfile(detector.config)
        # Set pretrained to be None since we do not need pretrained model here
        config.model.pretrained = None

        # Initialize the detector
        self.model = build_detector(config.model)

        # Load checkpoint
        checkpoint = load_checkpoint(self.model,detector.checkpoint, map_location=self.device)

        # Set the classes of models for inference
        self.model.CLASSES = checkpoint['meta']['CLASSES']

        # We need to set the model's cfg for inference
        self.model.cfg = config

        # Convert the model to GPU
        self.model.to(self.device)

        # Convert the model into evaluation mode
        self.model.eval()

        self.depth = GLPDepth(max_depth=10, is_train=False).to(self.device)
        depth_weight = torch.load(depth.checkpoint)
        if 'module' in next(iter(depth_weight.items()))[0]:
                depth_weight = OrderedDict((k[7:], v) for k, v in depth_weight.items())
        self.depth.load_state_dict(depth_weight)
        self.depth.eval()

        self.tracker = BYTETracker(tracker, frame_rate=tracker.frame_rate)
        self.ags_tracker=tracker
        self.shape=(480,640)

        self.pp=[camera[0],camera[1]]
        self.valpix=camera[2]
        self.half_img_mm=half_img_mm # lo hacemos si tomamos como referencia la mitad de la imagen para saber los metros en pixel

    def pixel2realx(self,punto):

        punto[1]=self.pp[1]*2-punto[1]
        pp=np.array(self.pp)
        m=(punto[0]-pp[1])/(punto[0]-pp[0])
        x=((-pp[1])/m)+pp[0]
        #x=self.valpix*x
        return x*(0.4*8/self.valpix)

    def pixel2realhx(self,punto):
        p=[punto[0],punto[1],1]
        rows = self.shape[0]; cols = self.shape[1]
        pts1 = np.float32([[0,250],[0,rows],[cols,250],[cols,rows]])
        x = 195
        pts2 = np.float32([[0,0],[x,rows],[cols,0],[cols-x,rows]])
        #Se calcula la matriz para la corrección de perspectiva
        M = cv2.getPerspectiveTransform(pts1,pts2)
        #print(M)
        pn=np.dot(M,p.T)
        pn=[pn[0]/pn[2],pn[1]/pn[2]]

        return (pn[0]-x)*(0.4*16/(cols-x-x))

    def pixel2reald(self,punto,b = 57.66066148,w = -0.22307059):
        #w = [[-0.22307059]], b = [57.66066148]
        #b = 57.66066148
        if punto>200:
            return -w*punto+b
        else:
            w=-0.00185376
            b=11.51382925
            return -w*punto+b

    def plot_tracking(self,image, tlwhs, obj_ids, scores=None, frame_id=0, fps=0., ids2=None):
        im = np.ascontiguousarray(np.copy(image))
        im_h, im_w = im.shape[:2]

        top_view = np.zeros([im_w, im_w, 3], dtype=np.uint8) + 255

        #text_scale = max(1, image.shape[1] / 1600.)
        #text_thickness = 2
        #line_thickness = max(1, int(image.shape[1] / 500.))
        text_scale = 2
        text_thickness = 2
        line_thickness = 3

        radius = max(5, int(im_w/140.))
        cv2.putText(im, 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
                    (0, int(15 * text_scale)), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), thickness=2)

        for i, tlwh in enumerate(tlwhs):
            x1, y1, w, h = tlwh
            intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
            obj_id = int(obj_ids[i])
            id_text = '{}'.format(int(obj_id))
            if ids2 is not None:
                id_text = id_text + ', {}'.format(int(ids2[i]))
            color = self.get_color(abs(obj_id))
            cv2.rectangle(im, intbox[0:2], intbox[2:4], color=color, thickness=line_thickness)
            cv2.putText(im, id_text, (intbox[0], intbox[1]), cv2.FONT_HERSHEY_PLAIN, text_scale, (0, 0, 255),
                        thickness=text_thickness)
        return im

    def pos_x_y_new(self,boxs,ids,masks,depth,depths,frame,view=True):
        if view:

            depth1= cv2.cvtColor(depth, cv2.COLOR_RGB2GRAY )
            imagen_depth=np.zeros((depth1.shape))
            image_depth_human=np.zeros((depth.shape))
            imagen_view_new=np.zeros((depth.shape))
            self.shape=depth1.shape

        puntos=[]
        puntos_real=[]
        cont_nop=0

        for idx_box in range(len(boxs)):
            if view:
                mascara=masks[idx_box]*255
                mascara=np.uint8(mascara)

                imagen_unida=  cv2.bitwise_and(depth1, depth1, mask=mascara)
                imagen_depth+=imagen_unida

            depth= depths[idx_box]

            puntos.append((int( boxs[idx_box][0]+boxs[idx_box][2]//2) , int(depth*self.shape[0]/255) ) )

            x_real=self.pixel2realx([ boxs[idx_box][0]+boxs[idx_box][2]//2,boxs[idx_box][3]] )
            x_real_3=self.pixel2realhx([ boxs[idx_box][0]+boxs[idx_box][2]//2,boxs[idx_box][3]] )
            puntos_real.append((int( x_real ) , int(depth),int(x_real_3) ) )
            if view:
                str_save="\t".join([str(frame),str(ids[idx_box]),str(puntos[idx_box][0]),str(puntos[idx_box][1])])
                dep_r=self.pixel2reald(puntos_real[idx_box][1])
                str_save2="\t".join([str(frame),str(ids[idx_box]),str(puntos_real[idx_box][0]),str(puntos_real[idx_box][1]),str(punto),str(dep_r)])
                str_save2="\t".join([str(frame),str(ids[idx_box]),str(puntos_real[idx_box][0]),str(puntos_real[idx_box][1]),str(punto),str(dep_r),str(puntos_real[idx_box][3])])
                self.fichero.write(str_save + os.linesep)
                self.fichero2.write(str_save2 + os.linesep)
                self.fichero3.write(str_save3 + os.linesep)
            if view:
                cv2.circle(imagen_view_new,(puntos[idx_box][0],puntos[idx_box][1]),10,(self.get_color(ids[idx_box])),-1)

        if view:
            image_depth_human[:,:,0]=imagen_depth
            image_depth_human[:,:,1]=imagen_depth
            image_depth_human[:,:,2]=imagen_depth

            return np.concatenate([image_depth_human,imagen_view_new], 1),puntos

        else:
            return puntos

    def change_view(self,path,paths_out="out",ext="png",save=True):

        paths= sorted(glob.glob(os.path.join(path, '*.{}'.format(ext))))
        os.makedirs(paths_out, exist_ok=True)
        if save:
            self.fichero = open(paths_out+'/mi_fichero', 'w')
            self.fichero2 = open(paths_out+'/mi_ficheror', 'w')
            self.fichero3 = open(paths_out+'/mi_ficheror2', 'w')
        timer = Timer()
        for idx, image_path in enumerate(paths):# Use the detector to do inference

                timer.tic()

                img= cv2.imread(image_path)

                self.shape=img.shape
                if idx==0:
                    self.pp=[self.shape[1]//2,self.shape[0]//2]
                    if self.half_img_mm:
                        self.valpix=self.valpix/self.pp[0]

                if idx%1==0:
                    result=inference_detector(self.model, img)

                    bboxes,masks=self.gef_get_box([result[0][0],[result[1][0]]])

                    #profundidad en los pixeles pares

                    im = pil.fromarray(img)

                    original_width, original_height= im.size

                    input_image = transforms.ToTensor()(im).unsqueeze(0)
                    input_image = input_image.to(self.device)
                    outputs = self.depth(input_image)

                    disp = outputs["pred_d"]
                    disp_resized = torch.nn.functional.interpolate(
                    disp, (original_height, original_width), mode="bilinear", align_corners=False)

                    output_name = os.path.splitext(os.path.basename(image_path))[0]

                    name_dest_npy = os.path.join(paths_out, "{}.png".format(output_name))

                    disp_resized_np = disp_resized.squeeze().cpu().detach().numpy()

                    imga=np.ones((disp_resized_np.shape))*disp_resized_np.max()
                    disp_resized_np=imga-disp_resized_np
                    vmax = np.percentile(disp_resized_np, 95)
                    normalizer = mpl.colors.Normalize(vmin=disp_resized_np.min(), vmax=vmax)
                    mapper = cm.ScalarMappable(norm=normalizer, cmap='magma')
                    colormapped_im = (mapper.to_rgba(disp_resized_np)[:, :, :3] * 255).astype(np.uint8)

                if bboxes is not None:
                        if idx%1==0:
                            online_targets = self.tracker.update(bboxes,masks ,[img.shape[0],img.shape[1]], [img.shape[0],img.shape[1]],
                                                            colormapped_im)
                        else:
                            #actualizamos la profundida con el mismo valor
                            online_targets = self.tracker.update(bboxes,masks ,[img.shape[0],img.shape[1]], [img.shape[0],img.shape[1]],
                            update_depth=True)

                        online_tlwhs = []
                        online_mask = []
                        online_ids = []
                        online_scores = []
                        online_depth=[]
                        for t in online_targets:
                                tlwh = t.tlwh

                                tid = t.track_id
                                vertical = tlwh[2] / tlwh[3] > self.ags_tracker.aspect_ratio_thresh
                                if tlwh[2] * tlwh[3] > self.ags_tracker.min_box_area and not vertical:
                                    online_tlwhs.append(tlwh)
                                    online_ids.append(tid)
                                    online_scores.append(t.score)
                                    online_mask.append(t.mask)
                                    online_depth.append(t.depth)
                                    #print(np.max(np.array(online_depth)))

                        timer.toc()

                        image_depth_human=self.pos_x_y_new(online_tlwhs,online_ids,online_mask,colormapped_im,online_depth,idx,save)

                        if len(image_depth_human)>1:
                            image_depth_human,puntos=image_depth_human
                            image_depth_human=self.plot_tracking(
                                image_depth_human, online_tlwhs, online_ids, frame_id= idx + 1, fps=1. / timer.average_time)

                        else:
                            puntos=image_depth_human
                            image_depth_human=np.zeros((img.shape[0],img.shape[1]*2,img.shape[2]))
                        if save:
                            online_im = self.plot_tracking(
                                    img, online_tlwhs, online_ids, frame_id= idx + 1, fps=1. / timer.average_time)

                else:
                    online_im = img
                    image_depth_human=np.zeros((img.shape[0],img.shape[1]*2,img.shape[2]))
                    timer.toc()

                if save:
                    colormapped_im2=self.plot_tracking(
                                    colormapped_im.copy(), online_tlwhs, online_ids, frame_id= idx + 1, fps=1. / timer.average_time)

                    online_im=np.concatenate([online_im,colormapped_im2], 1)

                    online_im=np.concatenate([online_im,image_depth_human], 0)

                    cv2.imwrite(name_dest_npy, online_im)

                print(" foto ",idx,"fps", 1. / max(1e-5, timer.average_time))

        self.fichero3.close()
        self.fichero.close()
        self.fichero2.close()
    @staticmethod
    def gef_get_box(result):
        bbox_result,mask_result = result
        bboxes = np.vstack(bbox_result)
        scores = bboxes[:,4]

        if len(result) == 0:
            bboxes = np.zeros([0, 5])
            masks = np.zeros([0, 0, 0])

        else:
            masks = mmcv.concat_list(mask_result)

            if isinstance(masks[0], torch.Tensor):
                masks = torch.stack(masks, dim=0).detach().cpu().numpy()
            else:
                masks = np.stack(masks, axis=0)

            if bboxes[:, :4].sum() == 0:
                num_masks = len(bboxes)
                x_any = masks.any(axis=1)
                y_any = masks.any(axis=2)
                for idx in range(num_masks):
                    x = np.where(x_any[idx, :])[0]
                    y = np.where(y_any[idx, :])[0]
                    if len(x) > 0 and len(y) > 0:
                        bboxes[idx, :4] = np.around( np.array(
                            [x[0], y[0], x[-1] + 1, y[-1] + 1],
                            dtype=np.float32) )

        return  bboxes,masks

    @staticmethod
    def get_color(idx):
        idx = idx * 3
        color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)

        return color


from collections import deque

n, k = map(int, input().split())

graph = []
virus_data = []

for i in range(n):
	graph.append(list(map(int, input.split())))
	for j in range(n):
		if graph[i][j] != 0:
			# 바이러스 종류, 초 (해당 초의 상태), X, Y
			virus_data.append((graph[i][j], 0, i, j))

virus_data.sort()
q = deque(virus_data)

target_s, target_x, target_y = map(int, input().split())

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

while q:
	virus, s, x, y = q.popleft()
	# s가 target_s 일 때는 이미 s초의 상태가 저장되어있는 상태
	# 여기서 break하지 않으면 s초를 넘어간 이후의 상태까지 구하는 것
	if s == target_s:
		break

	for i in range(4):
		nx = x + dx[i]
		ny = y + dy[i]

		if 0 <= nx and nx < n and 0 <= ny and ny < n:
			if graph[nx][ny] == 0:
				graph[nx][ny] = virus
				q.append((virus, s + 1, nx, ny))

print(graph[target_x - 1][target_y - 1])


def log(message, values):
    if not values:
        print(message)
    else:
        values_str = ', '.join(str(x) for x in values)
        print(f'{message}: {values_str}')
log('My number are ', [1, 2])
log('Hi there', [])  #空配列渡さないといけない、いまいち

def log(message, *values):
    if not values:
        print(message)
    else:
        values_str = ', '.join(str(x) for x in values)
        print(f'{message}: {values_str}')
log('My number are ', 1, 2)
log('Hi there')  #余分な値渡さなくて良い

#問題1:メモリ大量消費
def my_generator():
    for i in range(10):
        yield i
def my_func(*args):
    print(args)
it = my_generator()
my_func(it)  #ここでジェネレータをタプルに展開するので、メモリ食いつぶす可能性あり

#問題2:引数追加する場合、呼び出し元も修正する必要あり
def log(sequence, message, *values):
    if not values:
        print(f'{sequence} - {message}')
    else:
        values_str = ', '.join(str(x) for x in values)
        print(f'{sequence} - {message}: {values_str}')
log(1, 'Favorites', 7, 33)
log(1, 'Hi there')
log('Favorite numbers', 7, 33) #->古い呼び出し方は意図しない動きをする（エラー発生しないので気づきづらい）


def rot(s, d = 1):
	return (s + d + 6) % 6

def neighbor(c, s, l, w, f, r):
	if (s < 2 and l[c] == 0) or ((s == 3 or s == 4) and l[c] == 2*r-2):
		return None
	if s == 0: # northwest
		t = c - w[l[c]] if l[c] < r else c - w[l[c]-1]
	elif s == 1: # northeast
		t = (c - w[l[c]] if l[c] < r else c - w[l[c]-1]) + 1
	elif s == 2: # east
		return c + 1 if c + 1 < f[l[c]] + w[l[c]] else None
	elif s == 3: # southeast
		t = c + w[l[c]+1] if l[c] < r-1 else c + w[l[c]]
	elif s == 4: # southwest
		t = (c + w[l[c]+1] if l[c] < r-1 else c + w[l[c]]) - 1
	else: # west
		return c - 1 if c - 1 >= f[l[c]] else None
	if s < 2:
		return t if f[l[c]-1] <= t < f[l[c]-1] + w[l[c]-1] else None
	else:
		return t if f[l[c]+1] <= t < f[l[c]+1] + w[l[c]+1] else None

r, k = [int(_) for _ in input().split()]
house = [int(_) for _ in input().split()]
n = r**3 - (r-1)**3

level = []
width = list(range(r, r+r-1)) + list(range(r+r-1, r-1, -1))
for l in range(len(width)):
	level.extend([l] * width[l])
first = [0]
for l in range(len(width)-1):
	first.append(first[-1] + width[l])

walls = [[0]*6 for _ in range(n)]
for c in house:
	for i in range(6):
		nbr = neighbor(c-1, i, level, width, first, r)
		if nbr != None and walls[nbr][rot(i, 3)] > 0:
			walls[nbr][rot(i, 3)] += 1
		else:
			walls[c-1][i] += 1

cell = orig = min(house) - 1
side = 0
perim = 0
while True:
	perim += 1
	lnbr = neighbor(cell, side, level, width, first, r)
	rnbr = neighbor(cell, rot(side), level, width, first, r)
	if walls[cell][rot(side)] == 1 or (rnbr != None and walls[rnbr][rot(side, 4)] == 1):
		side = rot(side)
	elif lnbr != None and walls[lnbr][rot(side, 2)] == 1 or rnbr != None and walls[rnbr][rot(side, -1)] == 1:
		cell = rnbr
		side = rot(side, -1)
	else:
		print('no no no')
	if cell == orig and side == 0:
		break
print(perim)


import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Activation

from abbert2.vendored.cerebras_modelzoo.common.layers.tf.BaseLayer import BaseLayer
from abbert2.vendored.cerebras_modelzoo.common.layers.tf.utils import boundary_cast, summary_layer

class ActivationLayer(BaseLayer):
    """Wrapper around the Keras activation layer.

    Also supports ``activation="GeLU"``, which is currently missing in
    ``keras.layers.ActivationLayer``.

    Args:
        activation (Union[str, Callable]): The function to be applied. This can
            either be callable string name of a Tensorflow built-in activation,
            or one of ``"gelu"`` or ``"lrelu"`` (``lrelu`` denotes LeakyReLU).
        boundary_casting (bool): If ``True``, outputs the values in half
            precision and casts the input values up to full precision.
        tf_summary (bool): If ``True``, saves the activations with the
            ``summary_layer``.

    """

    def __init__(
        self, activation, boundary_casting=False, tf_summary=False, **kwargs,
    ):
        super(ActivationLayer, self).__init__(
            boundary_casting, tf_summary, **kwargs
        )

        self.layer = None
        if isinstance(activation, str):
            if activation.lower() == "gelu":
                self.layer = self.gelu
            elif activation.lower() == "lrelu":
                raise ValueError("We only support LeakyReLU as a callable.")

        if self.layer is None:
            self.layer = Activation(
                activation, name=self.name, dtype=self.dtype_policy
            )

    def call(self, inputs, **kwargs):
        """
        Apply the activation layer.

        Args:
            inputs: Arbitrary tensor.
        Returns:
            Tensor: A tensor of the same shape as the input.
        """

        if self.boundary_casting:
            inputs = boundary_cast(inputs)

        if self.tf_summary:
            inputs = summary_layer(inputs)

        output = self.layer(inputs)
        if self.tf_summary:
            output = summary_layer(output)
        return output

    @staticmethod
    def gelu(x):
        cdf = 0.5 * (
            1.0 + tf.tanh((np.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))
        )
        return x * cdf


#!/usr/bin/python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from keras import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import InputLayer, Dense, Flatten, Conv2D, \
    Activation, MaxPooling2D, Dropout
from keras import backend as K
from keras.models import load_model
from keras.preprocessing.image import load_img
from flask import Flask, render_template, url_for, request, app, jsonify

app = Flask(__name__)

import tensorflow as tf
new_model = tf.keras.models.load_model('saved_model/my_model')

# Check its architecture
# new_model.summary()

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/predict_image', methods=['POST'])
def predict_image():
    file = request.files['file']
    filename = file.filename
    if str(filename).strip():
        image = load_img('dog.jpg', target_size=(256, 256))
        img = np.array(image)
        img = img / 255.0
        img = img.reshape(1, 256, 256, 3)
        label = model.predict(img)
        print ('Predicted Class (0 - Cat , 1- Dog): ',
               round(label[0][0]))
        val = round(label[0][0])
        pred = ('Cat' if val else 'Dog')
        return render_template('home.html', prediction=val,
                               img_name=filename)
    else:
        return render_template('home.html')

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


from __future__ import annotations

import functools
from operator import add
from operator import mul

ADD = 1
HALT = 99

@functools.cache
def parse(filename: str) -> tuple[int, ...]:
    with open(filename) as f:
        return tuple(int(n) for n in f.read().split(","))

def run(filename: str, noun: int, verb: int) -> int:
    ops = list(parse(filename))
    ops[1], ops[2] = noun, verb

    for i in range(0, len(ops), 4):
        op, n1, n2, n3 = ops[i : i + 4]
        if op == HALT:
            break

        ops[n3] = (add if op == ADD else mul)(ops[n1], ops[n2])

    return ops[0]

def part1(filename: str) -> int:
    return run(filename, 12, 2)

def part2(filename: str) -> int:
    for noun in range(100):
        for verb in range(100):
            if run(filename, noun, verb) == 19690720:
                return 100 * noun + verb
    return 0

if __name__ == "__main__":
    from advent_of_code.common.main import main

    raise SystemExit(main(part1, part2))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 15 18:29:12 2019

@author: jannik
"""

import wx
import os

class FileMenu(wx.Menu):
    def __init__(self, parentFrame):
        super().__init__()
        self.OnInit()
        self.parentFrame = parentFrame

    def OnInit(self):
        newItem = wx.MenuItem(parentMenu=self, id=wx.ID_NEW, text="&New\tCtrl+N", kind=wx.ITEM_NORMAL)
        self.Append(newItem)

        openItem = wx.MenuItem(parentMenu=self, id=wx.ID_OPEN, text='&Open\tCtrl+O', kind=wx.ITEM_NORMAL)
        self.Append(openItem)
        self.Bind(wx.EVT_MENU, handler=self.onOpen, source=openItem)

        saveItem = wx.MenuItem(parentMenu=self, id=wx.ID_SAVE, text="&Save\tCtrl+S", helpString="Save your file", kind=wx.ITEM_NORMAL)
        self.Append(saveItem)
        self.Bind(wx.EVT_MENU, handler=self.onSave, source=saveItem)

        self.AppendSeparator()

        radioItem1 = wx.MenuItem(self, id=200, text = "Option 1", kind = wx.ITEM_RADIO)
        self.Append(radioItem1)
        radioItem2 = wx.MenuItem(self, id=300, text = "Option 2", kind = wx.ITEM_RADIO)
        self.Append(radioItem2)

        self.AppendSeparator()

        quitItem = wx.MenuItem(parentMenu=self, id=wx.ID_EXIT, text='&Quit\tCtrl+Q')
        self.Append(quitItem)
        self.Bind(wx.EVT_MENU, handler=self.onQuit, source=quitItem)

    def onOpen(self, event):
        wildcard = "TXT files (*.txt)|*.txt"
        dialog = wx.FileDialog(self.parentFrame, "Open Text Files", wildcard=wildcard,
                               style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        if dialog.ShowModal() == wx.ID_CANCEL:
            return None

        path = dialog.GetPath()
        if os.path.exists(path):
            with open(path) as myfile:
                for line in myfile:
                    self.parentFrame.text.WriteText(line)

    def onSave(self, event):
        dialog = wx.FileDialog(self.parentFrame, message="Save your data",
                            defaultFile="Untitled.txt", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)

        if dialog.ShowModal() == wx.ID_CANCEL:
            return None

        path = dialog.GetPath()
        data = self.parentFrame.text.GetValue()
        print(data)
        data = data.split('\n')
        print(data)
        with open(path, "w+") as myfile:
            for line in data:
                myfile.write(line+"\n")

    def onQuit(self, event):
        self.parentFrame.Close()


import boto3
from botocore.exceptions import ClientError
from collections import defaultdict, OrderedDict
from time import sleep

from benchmark.utils import Print, BenchError, progress_bar
from benchmark.settings import Settings, SettingsError

class AWSError(Exception):
    def __init__(self, error):
        assert isinstance(error, ClientError)
        self.message = error.response['Error']['Message']
        self.code = error.response['Error']['Code']
        super().__init__(self.message)

class InstanceManager:
    INSTANCE_NAME = 'dag-node'
    SECURITY_GROUP_NAME = 'dag'

    def __init__(self, settings):
        assert isinstance(settings, Settings)
        self.settings = settings
        self.clients = OrderedDict()
        for region in settings.aws_regions:
            self.clients[region] = boto3.client('ec2', region_name=region)

    @classmethod
    def make(cls, settings_file='settings.json'):
        try:
            return cls(Settings.load(settings_file))
        except SettingsError as e:
            raise BenchError('Failed to load settings', e)

    def _get(self, state):
        # Possible states are: 'pending', 'running', 'shutting-down',
        # 'terminated', 'stopping', and 'stopped'.
        ids, ips = defaultdict(list), defaultdict(list)
        for region, client in self.clients.items():
            r = client.describe_instances(
                Filters=[
                    {
                        'Name': 'tag:Name',
                        'Values': [self.INSTANCE_NAME]
                    },
                    {
                        'Name': 'instance-state-name',
                        'Values': state
                    }
                ]
            )
            instances = [y for x in r['Reservations'] for y in x['Instances']]
            for x in instances:
                ids[region] += [x['InstanceId']]
                if 'PublicIpAddress' in x:
                    ips[region] += [x['PublicIpAddress']]
        return ids, ips

    def _wait(self, state):
        # Possible states are: 'pending', 'running', 'shutting-down',
        # 'terminated', 'stopping', and 'stopped'.
        while True:
            sleep(1)
            ids, _ = self._get(state)
            if sum(len(x) for x in ids.values()) == 0:
                break

    def _create_security_group(self, client):
        client.create_security_group(
            Description='HotStuff node',
            GroupName=self.SECURITY_GROUP_NAME,
        )

        client.authorize_security_group_ingress(
            GroupName=self.SECURITY_GROUP_NAME,
            IpPermissions=[
                {
                    'IpProtocol': 'tcp',
                    'FromPort': 22,
                    'ToPort': 22,
                    'IpRanges': [{
                        'CidrIp': '0.0.0.0/0',
                        'Description': 'Debug SSH access',
                    }],
                    'Ipv6Ranges': [{
                        'CidrIpv6': '::/0',
                        'Description': 'Debug SSH access',
                    }],
                },
                {
                    'IpProtocol': 'tcp',
                    'FromPort': self.settings.base_port,
                    'ToPort': self.settings.base_port + 2_000,
                    'IpRanges': [{
                        'CidrIp': '0.0.0.0/0',
                        'Description': 'Dag port',
                    }],
                    'Ipv6Ranges': [{
                        'CidrIpv6': '::/0',
                        'Description': 'Dag port',
                    }],
                }
            ]
        )

    def _get_ami(self, client):
        # The AMI changes with regions.
        response = client.describe_images(
            Filters=[{
                'Name': 'description',
                'Values': ['Canonical, Ubuntu, 20.04 LTS, amd64 focal image build on 2020-10-26']
            }]
        )
        return response['Images'][0]['ImageId']

    def create_instances(self, instances):
        assert isinstance(instances, int) and instances > 0

        # Create the security group in every region.
        for client in self.clients.values():
            try:
                self._create_security_group(client)
            except ClientError as e:
                error = AWSError(e)
                if error.code != 'InvalidGroup.Duplicate':
                    raise BenchError('Failed to create security group', error)

        try:
            # Create all instances.
            size = instances * len(self.clients)
            progress = progress_bar(
                self.clients.values(), prefix=f'Creating {size} instances'
            )
            for client in progress:
                client.run_instances(
                    ImageId=self._get_ami(client),
                    InstanceType=self.settings.instance_type,
                    KeyName=self.settings.key_name,
                    MaxCount=instances,
                    MinCount=instances,
                    SecurityGroups=[self.SECURITY_GROUP_NAME],
                    TagSpecifications=[{
                        'ResourceType': 'instance',
                        'Tags': [{
                            'Key': 'Name',
                            'Value': self.INSTANCE_NAME
                        }]
                    }],
                    EbsOptimized=True,
                    BlockDeviceMappings=[{
                        'DeviceName': '/dev/sda1',
                        'Ebs': {
                            'VolumeType': 'gp2',
                            'VolumeSize': 200,
                            'DeleteOnTermination': True
                        }
                    }],
                )

            # Wait for the instances to boot.
            Print.info('Waiting for all instances to boot...')
            self._wait(['pending'])
            Print.heading(f'Successfully created {size} new instances')
        except ClientError as e:
            raise BenchError('Failed to create AWS instances', AWSError(e))

    def terminate_instances(self):
        try:
            ids, _ = self._get(['pending', 'running', 'stopping', 'stopped'])
            size = sum(len(x) for x in ids.values())
            if size == 0:
                Print.heading(f'All instances are shut down')
                return

            # Terminate instances.
            for region, client in self.clients.items():
                if ids[region]:
                    client.terminate_instances(InstanceIds=ids[region])

            # Wait for all instances to properly shut down.
            Print.info('Waiting for all instances to shut down...')
            self._wait(['shutting-down'])
            for client in self.clients.values():
                client.delete_security_group(
                    GroupName=self.SECURITY_GROUP_NAME
                )

            Print.heading(f'Testbed of {size} instances destroyed')
        except ClientError as e:
            raise BenchError('Failed to terminate instances', AWSError(e))

    def start_instances(self, max):
        size = 0
        try:
            ids, _ = self._get(['stopping', 'stopped'])
            for region, client in self.clients.items():
                if ids[region]:
                    target = ids[region]
                    target = target if len(target) < max else target[:max]
                    size += len(target)
                    client.start_instances(InstanceIds=target)
            Print.heading(f'Starting {size} instances')
        except ClientError as e:
            raise BenchError('Failed to start instances', AWSError(e))

    def stop_instances(self):
        try:
            ids, _ = self._get(['pending', 'running'])
            for region, client in self.clients.items():
                if ids[region]:
                    client.stop_instances(InstanceIds=ids[region])
            size = sum(len(x) for x in ids.values())
            Print.heading(f'Stopping {size} instances')
        except ClientError as e:
            raise BenchError(AWSError(e))

    def hosts(self, flat=False):
        try:
            _, ips = self._get(['pending', 'running'])
            return [x for y in ips.values() for x in y] if flat else ips
        except ClientError as e:
            raise BenchError('Failed to gather instances IPs', AWSError(e))

    def print_info(self):
        hosts = self.hosts()
        key = self.settings.key_path
        text = ''
        for region, ips in hosts.items():
            text += f'\n Region: {region.upper()}\n'
            for i, ip in enumerate(ips):
                new_line = '\n' if (i+1) % 6 == 0 else ''
                text += f'{new_line} {i}\tssh -i {key} ubuntu@{ip}\n'
        print(
            '\n'
            '----------------------------------------------------------------\n'
            ' INFO:\n'
            '----------------------------------------------------------------\n'
            f' Available machines: {sum(len(x) for x in hosts.values())}\n'
            f'{text}'
            '----------------------------------------------------------------\n'
        )


from django.test import TestCase
import json
# Create your tests here.

class TestAPI(TestCase):

	# def test_elastic(self):
	# 	response = self.client.get('/elastic/api/elastic/?pathInput=%s&indexInput=%s' %('/home/jk/Jk/python/DjangoProject/New/djangoprojectfinal/MutualFunds.csv','jk'))
	# 	res = (response.content).decode('utf-8')
	# 	res_dict = json.loads(res)
	# 	status = res_dict.get('status')
	# 	self.assertEquals('success',status)

	# def test_deleteInput(self):
	# 	response = self.client.get('/elastic/api/deleteInput/?deleteInput=%s' %('abcd6'))
	# 	res = (response.content).decode('utf-8')
	# 	res_dict = json.loads(res)
	# 	status = res_dict.get('status')
	# 	self.assertEquals('success',status)

	# def test_oneByOneInput(self):
	# 	response = self.client.get('/elastic/api/oneByOneInput/?indexNamePath=%s&jsonObj=%s' %('abcd10','{"mfname":"testMfname","aum":2000,"ret_3yrs":3000,"ret_2yrs":8000,"ret_1yr":2000,"ret_5yrs":2500}'))
	# 	res = (response.content).decode('utf-8')
	# 	res_dict = json.loads(res)
	# 	status = res_dict.get('status')
	# 	self.assertEquals('success',status)

	def test_retrieve(self):
		response = self.client.get('/elastic/api/retrieve/?tablename=%s&indexname=%s' %('MobileDB','jkmob3'))
		res = (response.content).decode('utf-8')
		res_dict = json.loads(res)
		status = res_dict.get('status')
		self.assertEquals('success',status)

Analytics And Visualization Tool/elastic/tests.py


"""Definition of the Galleryfolder content type
"""

from zope.interface import implements

from Products.Archetypes import atapi
from Products.ATContentTypes.content import folder
from Products.ATContentTypes.content import schemata

# -*- Message Factory Imported Here -*-

from collective.ptg.galleryfolder.interfaces import IGalleryfolder
from collective.ptg.galleryfolder.config import PROJECTNAME

GalleryfolderSchema = folder.ATFolderSchema.copy() + atapi.Schema((

    # -*- Your Archetypes field definitions here ... -*-

))

# Set storage on fields copied from ATFolderSchema, making sure
# they work well with the python bridge properties.

GalleryfolderSchema['title'].storage = atapi.AnnotationStorage()
GalleryfolderSchema['description'].storage = atapi.AnnotationStorage()

schemata.finalizeATCTSchema(
    GalleryfolderSchema,
    folderish=True,
    moveDiscussion=False
)

class Galleryfolder(folder.ATFolder):
    """Gallery Content Type for Plone Truegallery"""
    implements(IGalleryfolder)

    meta_type = "Galleryfolder"
    schema = GalleryfolderSchema

    title = atapi.ATFieldProperty('title')
    description = atapi.ATFieldProperty('description')

    # -*- Your ATSchema to Python Property Bridges Here ... -*-

atapi.registerType(Galleryfolder, PROJECTNAME)


#   Wap for overflow error
def overFlowError():
    try:
        i = 1
        f = 3.0 ** i
        for i in range(50):
            print(i)
            f = f ** 2
    except Exception as e:
        print(type(e))
overFlowError()