from textwrap import dedent
import sublime

from unittesting import DeferrableTestCase
from SublimeLinter.tests.mockito import unstub
from SublimeLinter.tests.parameterized import parameterized as p

from SublimeLinter.lint.quick_fix import (
    apply_edits,
    fix_eslint_error,
    eslint_ignore_block,
    fix_codespell_error,
    fix_flake8_error,
    fix_mypy_error,
    fix_mypy_unused_ignore,
    fix_mypy_specific_unused_ignore,
    fix_stylelint_error,
    fix_shellcheck_error,
    ignore_rules_actions,

    DEFAULT_SUBJECT,
    DEFAULT_DETAIL
)

class TestActionReducer(DeferrableTestCase):
    @p.expand([
        (
            "filter error without code",
            [
                dict(linter="flake", code="", msg="foo unused", line=1),
            ],
            []
        ),
        (
            "single error",
            [
                dict(linter="flake", code="201", msg="foo unused", line=1),
            ],
            [
                'flake: Disable 201 — foo unused'
            ]
        ),
        (
            "two distinct errors",
            [
                dict(linter="flake", code="201", msg="foo unused", line=1),
                dict(linter="flake", code="202", msg="zoo unused", line=1),
            ],
            [
                'flake: Disable 201 — foo unused',
                'flake: Disable 202 — zoo unused'
            ]
        ),
        (
            "two similar errors, same line",
            [
                dict(linter="flake", code="201", msg="foo unused", line=1),
                dict(linter="flake", code="201", msg="zoo unused", line=1),
            ],
            [
                'flake: Disable 201 (2x) — e.g. foo unused',
            ]
        ),
        (
            "two similar errors, same line, same message",
            [
                dict(linter="flake", code="201", msg="too much", line=1),
                dict(linter="flake", code="201", msg="too much", line=1),
            ],
            [
                'flake: Disable 201 (2x) — too much',
            ]
        ),
        (
            "two similar errors, different line",
            [
                dict(linter="flake", code="201", msg="foo unused", line=1),
                dict(linter="flake", code="201", msg="zoo unused", line=2),
            ],
            [
                'flake: Disable 201 (2x) — e.g. foo unused',
            ]
        ),
    ])
    def test_action_descriptions(self, _, ERRORS, RESULT):
        fixer = lambda: None
        except_for = set()

        actions = ignore_rules_actions(
            DEFAULT_SUBJECT,
            DEFAULT_DETAIL,
            lambda e: not e["code"] or e["code"] in except_for,
            fixer,
            ERRORS,
            None
        )
        self.assertEquals(RESULT, [action.description for action in actions])

class TestIgnoreFixers(DeferrableTestCase):
    @classmethod
    def setUpClass(cls):
        # make sure we have a window to work with
        sublime.run_command("new_window")
        cls.window = sublime.active_window()
        s = sublime.load_settings("Preferences.sublime-settings")
        s.set("close_windows_when_empty", False)

    @classmethod
    def tearDownClass(cls):
        cls.window.run_command('close_window')

    def tearDown(self):
        unstub()

    def create_view(self, window):
        view = window.new_file()
        self.addCleanup(self.close_view, view)
        return view

    def close_view(self, view):
        view.set_scratch(True)
        view.close()

    @p.expand([
        (
            "clean line",
            "view = window.new_file()",
            "view = window.new_file()  # noqa: E203"
        ),
        (
            "extend one given",
            "view = window.new_file()  # noqa: F402",
            "view = window.new_file()  # noqa: E203, F402",
        ),
        (
            "extend two given",
            "view = window.new_file()  # noqa: F402, E111",
            "view = window.new_file()  # noqa: E111, E203, F402",
        ),
        (
            "normalize joiner",
            "view = window.new_file()  # noqa: F402,E111,E203",
            "view = window.new_file()  # noqa: E111, E203, F402",
        ),
        (
            "handle surrounding whitespace",
            "    view = window.new_file()  ",
            "    view = window.new_file()  # noqa: E203",
        ),
        (
            "keep existing comment",
            "view = window.new_file()  # comment ",
            "view = window.new_file()  # comment  # noqa: E203",
        ),
        (
            "keep existing comment with only one space preceding",
            "view = window.new_file() # comment",
            "view = window.new_file() # comment  # noqa: E203",
        ),
        (
            "keep existing comment while extending",
            "view = window.new_file()  # comment  # noqa: F403",
            "view = window.new_file()  # comment  # noqa: E203, F403",
        ),
        (
            "keep python comment position while extending",
            "view = window.new_file()  # noqa: F403  # comment",
            "view = window.new_file()  # noqa: E203, F403  # comment",
        ),
        (
            "keep informal comment position while extending",
            "view = window.new_file()  # noqa: F403, comment",
            "view = window.new_file()  # noqa: E203, F403, comment",
        ),
    ])
    def test_flake8(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        error = dict(code="E203", region=sublime.Region(4))
        edit = fix_flake8_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "clean line",
            "view = window.new_file()",
            "view = window.new_file()  # type: ignore[no-idea]"
        ),
        (
            "extend one given",
            "view = window.new_file()  # type: ignore[attr]",
            "view = window.new_file()  # type: ignore[attr, no-idea]"
        ),
        (
            "extend two given",
            "view = window.new_file()  # type: ignore[attr, import]",
            "view = window.new_file()  # type: ignore[attr, import, no-idea]",
        ),
        (
            "normalize joiner",
            "view = window.new_file()  # type: ignore[attr,import]",
            "view = window.new_file()  # type: ignore[attr, import, no-idea]",
        ),
        (
            "handle surrounding whitespace",
            "    view = window.new_file()  ",
            "    view = window.new_file()  # type: ignore[no-idea]"
        ),
        (
            "mypy comment must come before existing comment",
            "view = window.new_file()  # comment ",
            "view = window.new_file()  # type: ignore[no-idea]  # comment ",
        ),
        (
            "keep existing comment while extending",
            "view = window.new_file()  # type: ignore[attr]  # comment ",
            "view = window.new_file()  # type: ignore[attr, no-idea]  # comment ",
        ),
        (
            "keep existing type comment in-place",
            "view = window.new_file()  # type: sublime.View",
            "view = window.new_file()  # type: sublime.View  # type: ignore[no-idea]",
        ),
    ])
    def test_mypy(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        error = dict(code="no-idea", region=sublime.Region(4))
        edit = fix_mypy_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "remove ignore comment at EOL",
            "partial(fixer, error),  # type: ignore[arg-type]",
            "partial(fixer, error),",
        ),
        (
            "remove bare ignore comment at EOL",
            "partial(fixer, error),  # type: ignore",
            "partial(fixer, error),",
        ),
    ])
    def test_mypy_unused_ignore(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        error = dict(msg='Unused "type: ignore" comment', region=sublime.Region(4))
        edit = fix_mypy_unused_ignore(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "remove ignore comment at EOL 1",
            "partial(fixer, error),  # type: ignore[arg-type, import, other]",
            "partial(fixer, error),  # type: ignore[arg-type]",
        ),
        (
            "remove ignore comment at EOL 2",
            "partial(fixer, error),  # type: ignore[arg-type, other, import]",
            "partial(fixer, error),  # type: ignore[arg-type]",
        ),
        (
            "remove ignore comment at EOL 3",
            "partial(fixer, error),  # type: ignore[other, import, arg-type]",
            "partial(fixer, error),  # type: ignore[arg-type]",
        ),
    ])
    def test_mypy_specific_unused_ignore(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        error = dict(msg='Unused "type: ignore[import, other]" comment', region=sublime.Region(4))
        edit = fix_mypy_specific_unused_ignore(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "replace word",
            "# test crate",
            "# test create",
        ),
    ])
    def test_codespell(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        error = dict(msg='crate  ==> create', region=sublime.Region(7, 12))
        edit = fix_codespell_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "clean line",
            "let |document = node.ownerDocument",
            "// eslint-disable-next-line semi\nlet document = node.ownerDocument"
        ),
        (
            "handle surrounding whitespace",
            "  let |document = node.ownerDocument",
            "  // eslint-disable-next-line semi\n  let document = node.ownerDocument"
        ),
        (
            "extend one given",
            "// eslint-disable-next-line quote\nlet |document = node.ownerDocument",
            "// eslint-disable-next-line quote, semi\nlet document = node.ownerDocument"
        ),
        (
            "extend two given",
            "// eslint-disable-next-line no-alert, quote\nlet |document = node.ownerDocument",
            "// eslint-disable-next-line no-alert, quote, semi\nlet document = node.ownerDocument"
        ),
        (
            "normalize joiner",
            "// eslint-disable-next-line no-alert,quote,semi\nlet |document = node.ownerDocument",
            "// eslint-disable-next-line no-alert, quote, semi\nlet document = node.ownerDocument"
        ),
        (
            "keep existing comment while extending",
            "// eslint-disable-next-line quote -- some comment\nlet |document = node.ownerDocument",
            "// eslint-disable-next-line quote, semi -- some comment\nlet document = node.ownerDocument"
        ),
        (
            "recognize plugin rules",
            "// eslint-disable-next-line plugin/quote\nlet |document = node.ownerDocument",
            "// eslint-disable-next-line plugin/quote, semi\nlet document = node.ownerDocument"
        ),
    ])
    def test_eslint(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
        view.run_command("insert", {"characters": BEFORE})
        error = dict(code="semi", region=sublime.Region(POS))
        edit = fix_eslint_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "clean block",
            dedent("""\
            let document  = node.ownerDocument
            """.rstrip()),
            dedent("""\
            /* eslint-disable semi */
            let document  = node.ownerDocument
            /* eslint-enable semi */
            """.rstrip()),
            sublime.Region(4, 8)
        ),
        (
            "extend existing",
            dedent("""\
            /* eslint-disable emi */
            let document  = node.ownerDocument
            /* eslint-enable emi */
            """.rstrip()),
            dedent("""\
            /* eslint-disable emi, semi */
            let document  = node.ownerDocument
            /* eslint-enable emi, semi */
            """.rstrip()),
            sublime.Region(28, 32)
        )
    ])
    def test_eslint_block(self, _description, BEFORE, AFTER, REGION):
        view = self.create_view(self.window)
        view.run_command("insert", {"characters": BEFORE})
        errors = [
            dict(code="semi", region=sublime.Region(28)),
        ]
        edit = eslint_ignore_block(errors, REGION, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "clean line",
            "#id| {",
            "#id { /* stylelint-disable-line selector-no-id */"
        ),
        (
            "extend given comment",
            "#id| { /* stylelint-disable-line some-rule */",
            "#id { /* stylelint-disable-line selector-no-id, some-rule */",
        ),
    ])
    def test_stylelint(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
        view.run_command("insert", {"characters": BEFORE})
        error = dict(code="selector-no-id", region=sublime.Region(POS))
        edit = fix_stylelint_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)

    @p.expand([
        (
            "clean line",
            "r|esult=$variable",
            "# shellcheck disable=SC2154\nresult=$variable"
        ),
        (
            "add to existing rule",
            "# shellcheck disable=SC2034\nr|esult=$variable",
            "# shellcheck disable=SC2034,SC2154\nresult=$variable"
        ),
    ])
    def test_shellcheck(self, _description, BEFORE, AFTER):
        view = self.create_view(self.window)
        BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
        view.run_command("insert", {"characters": BEFORE})
        error = dict(msg="variable is referenced but not assigned. [SC2154]", region=sublime.Region(POS))
        edit = fix_shellcheck_error(error, view)
        apply_edits(view, edit)
        view_content = view.substr(sublime.Region(0, view.size()))
        self.assertEquals(AFTER, view_content)


# ### Motion Deblurring using CNN (tensorflow 1.x)
# Uniform motion deblurring을 Convolutional Neural Network(CNN)을 이용하여 구현해 본다.
# Idea 1) blurred - CNN - deblurred

# Jupyter notebook에서 matplotlib 사용할 때, 필요
# get_ipython().run_line_magic('matplotlib', 'inline')

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.contrib.data import Dataset, Iterator
from PIL import Image
print ("Package Loaded")

# Hyperparameter
batch_size = 64
training_rate = 0.000001
epochs = 30000

# input_parser 정의 (input & ground-truth)
def input_parser(img_path, label_path):
    reader = tf.WholeFileReader()
    img_file = tf.read_file(img_path)
    img_decoded = tf.cast(tf.image.decode_image(img_file,channels=1),tf.float32)
    label_file =tf.read_file(label_path)
    label_decoded = tf.cast(tf.image.decode_image(label_file,channels=1),tf.float32)
    return img_decoded, label_decoded

# training & validation 경로
train_imgs = tf.constant([("./dataset/train_in/%d_bi.png" % i) for i in range(1,40001)])
label_imgs = tf.constant([("./dataset/train_out/%d_gt.png" % i) for i in range(1,40001)])

dataset = tf.data.Dataset.from_tensor_slices((train_imgs,label_imgs))
dataset = dataset.map(input_parser)
dataset = dataset.shuffle(buffer_size=20000)
dataset = dataset.repeat()
dataset = dataset.batch(batch_size)

iterator = Iterator.from_structure(dataset.output_types,dataset.output_shapes)
training_init_op = iterator.make_initializer(dataset)
next_element = iterator.get_next()

#valid_imgs = tf.constant([("./dataset/validation_bp/%d_bi.png" % i) for i in range(1,501)])
#valid_label_imgs = tf.constant([("./dataset/validation_gp/%d_gt.png" % i) for i in range(1,501)])

#dataset2 = Dataset.from_tensor_slices((valid_imgs,valid_label_imgs))
#dataset2= dataset2.map(input_parser)
#dataset2 = dataset2.shuffle(buffer_size=20000)
#dataset2 = dataset2.repeat()
#dataset2 = dataset2.batch(batch_size)

#iterator2 = Iterator.from_structure(dataset2.output_types,dataset2.output_shapes)
#training_init_op2 = iterator2.make_initializer(dataset2)
#next_element2 = iterator2.get_next()

# CNN model
# 1. 입력층, 출력층 및 13개의 은닉층으로 구성
# 2. 각 층의 커널의 크기는 처음 두층은 128, 나머지 은닉층은 64, 출력층은 1을 사용
inputs = tf.placeholder(tf.float32, (None, 64, 64, 1), name='inputs')
labels = tf.placeholder(tf.float32, (None, 64, 64, 1), name='labels')

conv1 = tf.layers.conv2d(inputs, 128, (3,3), padding='same', activation=None)
conv1 = tf.nn.relu(conv1)

conv2 = tf.layers.conv2d(conv1, 128, (3,3), padding='same', activation=None)
conv2 = tf.nn.relu(conv2)

conv3 = tf.layers.conv2d(conv2, 64, (3,3), padding='same', activation=None)
conv3 = tf.nn.relu(conv3)

conv4 = tf.layers.conv2d(conv3, 64, (3,3), padding='same', activation=None)
conv4 = tf.nn.relu(conv4)

conv5 = tf.layers.conv2d(conv4, 64, (3,3), padding='same', activation=None)
conv5 = tf.nn.relu(conv5)

conv6 = tf.layers.conv2d(conv5, 64, (3,3), padding='same', activation=None)
conv6 = tf.nn.relu(conv6)

conv7 = tf.layers.conv2d(conv6, 64, (3,3), padding='same', activation=None)
conv7 = tf.nn.relu(conv7)

conv8 = tf.layers.conv2d(conv7, 64, (3,3), padding='same', activation=None)
conv8 = tf.nn.relu(conv8)

conv9 = tf.layers.conv2d(conv8, 64, (3,3), padding='same', activation=None)
conv9 = tf.nn.relu(conv9)

#conv10 = tf.layers.conv2d(conv9, 64, (3,3), padding='same', activation=None)
#conv10 = tf.nn.relu(conv10)

#conv11 = tf.layers.conv2d(conv10, 64, (3,3), padding='same', activation=None)
#conv11 = tf.nn.relu(conv11)

#conv12 = tf.layers.conv2d(conv11, 64, (3,3), padding='same', activation=None)
#conv12 = tf.nn.relu(conv12)

#conv13 = tf.layers.conv2d(conv12, 64, (3,3), padding='same', activation=None)
#conv13 = tf.nn.relu(conv13)

logits = tf.layers.conv2d(conv9, 1, (3,3), padding='same', activation=None)
logits = tf.identity(logits, name='logits')

# Loss function & Optimization
costc=tf.losses.mean_squared_error(labels,logits)
optc = tf.train.AdamOptimizer(training_rate).minimize(costc)

# Weight 저장 경로
save_file = './a/model_gs1.ckpt'
saver = tf.train.Saver()

# Learning
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    sess.run(training_init_op)
    #sess.run(training_init_op2)

    for e in range(epochs):
        input_tensor,label_tensor = sess.run(next_element)
        inpss = input_tensor.reshape((-1, 64, 64, 1))
        labss = label_tensor.reshape((-1, 64, 64, 1))
        batch_cost, _ = sess.run([costc, optc], feed_dict = {inputs: inpss, labels: labss})
        if (e%100 == 0):
            print("Epoch: {}/{}...".format(e+1, epochs), "loss: {:.6f} ".format(batch_cost))
    # 학습 종료 후 가중치 저장
    saver.save(sess, save_file)

# 1장 Test
# Blur된 영상에 Guided filtering or Shock filtering을 전처리한 영상을 입력으로 사용한다.

# file 경로
test_imgs = tf.constant("./dataset/test_bp/Bblur_1.png")
ground_imgs = tf.constant("./dataset/test_bp/gt_1.png")
base_t = tf.constant("./dataset/test_bp/Bblur_1.png")

img_filec = tf.read_file(test_imgs)
img_decodedc = tf.cast(tf.image.decode_image(img_filec, channels=1),tf.float32)

img_fileg = tf.read_file(ground_imgs)
img_decodedg = tf.cast(tf.image.decode_image(img_fileg, channels=1),tf.float32)

img_fileb = tf.read_file(base_t)
img_decodedb = tf.cast(tf.image.decode_image(img_fileb, channels=1),tf.float32)

# 테스트 전 영상 조정하기
with tf.Session() as sess:
    test_input = sess.run(img_decodedc)
    ground_input = sess.run(img_decodedg)
    base_input = sess.run(img_decodedb)
    sess.close()

img = Image.open("./dataset/test_bp/Bblur_1.png")
test_n, test_m = img.size
base_tt = Image.open("./dataset/test_bp/Bblur_1.png")
print ("height: {} width: {}".format(test_m, test_n))

test_input = test_input.reshape((test_m, test_n))
ground_input = ground_input.reshape((512,512))

# test 입력 영상 출력
plt.imshow(test_input, cmap='Greys_r')
plt.show()

# 출력 구하기
# 테스트 영상을 Neural Network에 입력하여 출력을 구한다.
save_model_path = './a/model_gs1.ckpt'
loaded_graph = tf.Graph()

with tf.Session(graph=loaded_graph) as sess:
    loader = tf.train.import_meta_graph(save_model_path + '.meta')
    loader.restore(sess, save_model_path)

    inputs = loaded_graph.get_tensor_by_name('inputs:0')
    labels = loaded_graph.get_tensor_by_name('labels:0')
    logits = loaded_graph.get_tensor_by_name('logits:0')

    # padding
    p_n = 64 - test_n%64
    p_m = 64 - test_n%64
    if (test_n%64 == 0):
        p_n = 0
    if (test_m%64 == 0):
        p_m = 0

    result_img = np.zeros([test_m+p_m, test_n+p_n])
    #(위,아래) (왼 ,오)
    test_inputp = np.pad(test_input,((16,16+p_m),(16,16+p_n)),'reflect')

    b_n = int((test_n+p_n)/32)
    b_m = int((test_m+p_m)/32)

    print ("height: {} width: {}".format(b_m, b_n))

    for i in range(b_n):
        for j in range(b_m):
            image_patch = np.zeros([64,64])
            for x in range(64):
                for y in range(64):
                    image_patch[y, x] = test_inputp[j*32+y,i*32+x]

            image_patch = image_patch.reshape((-1,64,64,1))
            resultc = sess.run(logits, feed_dict = {inputs: image_patch})
            resulta = resultc.reshape((64,64))

            for x in range(32):
                for y in range(32):
                    result_img[(j*32)+y,(i*32)+x] = resulta[y+16,x+16]

    result_imgs = np.zeros([test_m, test_n])
    for i in range(test_n):
        for j in range(test_m):
            result_imgs[j, i] = result_img[j,i]

    plt.imshow(result_imgs, cmap='Greys_r')

# 결과 영상 출력
# 순서대로 결과영상, 테스트 입력 영상(shock filtering 후 영상), 디블러된 영상
plt.imshow(ground_input, cmap='Greys_r')
plt.show()
plt.imshow(test_input, cmap='Greys_r')
plt.show()
plt.imshow(result_imgs.reshape((test_m, test_n)), cmap='Greys_r')
plt.show()

# 결과 영상 저장
im = Image.fromarray(result_imgs)
if im.mode != 'RGB':
    im = im.convert('RGB')
im.save("./dataset/test_bp/result3_1.png")

# 테스트 영상 전체 저장
for da in range(51,81):
    for db in range(1,9):

        test_imgs = tf.constant("./dataset/pre/Bblur_%d_%d.png" %(da, db))
        img_filec = tf.read_file(test_imgs)
        img_decodedc = tf.cast(tf.image.decode_image(img_filec,channels=1),tf.float32)

        with tf.Session() as sess:
            test_input  = sess.run(img_decodedc)
            sess.close()

        img = Image.open("./dataset/pre/Bblur_%d_%d.png" %(da, db))
        test_n, test_m = img.size
        base_tt = Image.open("./dataset/test_bp/Bblur_1.png")

        print ("height: {} width: {}".format(test_m, test_n))

        test_input = test_input.reshape((test_m, test_n))

        save_model_path = './a/model_gs1.ckpt'
        loaded_graph = tf.Graph()

        with tf.Session(graph=loaded_graph) as sess:
            loader = tf.train.import_meta_graph(save_model_path + '.meta')
            loader.restore(sess, save_model_path)

            inputs = loaded_graph.get_tensor_by_name('inputs:0')
            labels = loaded_graph.get_tensor_by_name('labels:0')
            logits = loaded_graph.get_tensor_by_name('logits:0')

            # padding
            p_n = 64 - test_n%64
            p_m = 64 - test_n%64
            if (test_n%64 == 0):
                p_n = 0
            if (test_m%64 == 0):
                p_m = 0

            result_img = np.zeros([test_m+p_m, test_n+p_n])
            #(위,아래) (왼 ,오)
            test_inputp = np.pad(test_input,((16,16+p_m),(16,16+p_n)),'reflect')

            b_n = int((test_n+p_n)/32)
            b_m = int((test_m+p_m)/32)

            print ("height: {} width: {}".format(b_m, b_n))

            for i in range(b_n):
                for j in range(b_m):
                    image_patch = np.zeros([64,64])
                    for x in range(64):
                        for y in range(64):
                            image_patch[y, x] = test_inputp[j * 32 + y, i * 32 + x]

                    image_patch = image_patch.reshape((-1, 64, 64, 1))
                    resultc = sess.run(logits, feed_dict = {inputs: image_patch})
                    resulta = resultc.reshape((64, 64))

                    for x in range(32):
                        for y in range(32):
                            result_img[(j * 32) + y, (i * 32) + x] = resulta[y + 16, x + 16]

            result_imgs = np.zeros([test_m, test_n])
            for i in range(test_n):
                for j in range(test_m):
                    result_imgs[j, i] = result_img[j, i]

            im = Image.fromarray(result_imgs)
            if im.mode != 'RGB':
                im = im.convert('RGB')

            im.save("./dataset/CNN/cnn_%d_%d.png" %(da, db))


# -*- coding: utf-8 -*-
"""
Created on Sat Mar 20 21:52:06 2021

@author: Karthik
"""

import numpy as np

Inp=[]
no_features=int(input('Enter the number of features: '))
no_instances=int(input('Enter the number of instances: '))
print('Enter the data with enter:')
for i in range(0, no_features*no_instances):
    ele = int(input())
    Inp.append(ele) # adding the element
print(Inp)

x=np.array(Inp)
c=np.array([1,1,-1,-1]).reshape(no_instances,1)
x=x.reshape(no_instances,no_features)
x=x.astype(float)

def class_error_calc(w):
#Part 1
    w=np.array(w)
    h=[]
    class_outputs=[]
    condlist1=[ (x[:,0]>-0.5),(x[:,0]<=-0.5)]
    choicelist1=[1,-1]
    h1=np.select(condlist1,choicelist1)
    print('Overall Error Rate= Total samples - succesrate')
    print((len(c)-np.sum(h1==c.T))/len(c))
    h.append(np.sum((h1!=c.T)*w))
    class_outputs.append(h1)

    condlist2=[ (x[:,0]>-0.5),(x[:,0]<=-0.5)]
    choicelist2=[-1,1]
    h2=np.select(condlist2,choicelist2)
    print((len(c)-np.sum(h2==c.T))/len(c))
    h.append(np.sum((h2!=c.T)*w))
    class_outputs.append(h2)

    condlist3=[ (x[:,0]>0.5),(x[:,0]<=0.5)]
    choicelist3=[1,-1]
    h3=np.select(condlist3,choicelist3)
    print((len(c)-np.sum(h3==c.T))/len(c))
    h.append(np.sum((h3!=c.T)*w))
    class_outputs.append(h3)

    return h,class_outputs

# condlist4=[ (x[:,0]>0.5),(x[:,0]<=0.5)]
# choicelist4=[-1,1]
# h4=np.select(condlist4,choicelist4)
# #print((4-np.sum(h4))/4*100)

# condlist5=[ (x[:,1]>-0.5),(x[:,1]<=-0.5)]
# choicelist5=[1,-1]
# h5=np.select(condlist5,choicelist5)
# #print((4-np.sum(h5))/4*100)

# condlist6=[ (x[:,1]>-0.5),(x[:,1]<=-0.5)]
# choicelist6=[-1,1]
# h6=np.select(condlist6,choicelist6)
# #print((4-np.sum(h6))/4*100)

# condlist7=[ (x[:,1]>0.5),(x[:,1]<=0.5)]
# choicelist7=[1,-1]
# h7=np.select(condlist7,choicelist7)
# #print((4-np.sum(h7))/4*100)

# condlist8=[ (x[:,1]>0.5),(x[:,1]<=0.5)]
# choicelist8=[-1,1]
# h8=np.select(condlist8,choicelist8)
# #print((4-np.sum(h8))/4*100)

# #Used for Q1.b Classification Error Q2.a Training Error as well
# #print('Overall Error Rate= Total samples - succesrate')
# Avg_m=(h1+h2+h3+h4+h5+h6+h7+h8)/8

# condlist_avg=[Avg_m<0,Avg_m==0,Avg_m>0]
# choicelist_avg=[-1,1,1]
# Avg_boost_result=np.select(condlist_avg,choicelist_avg)
# print('THe training error')
# print(np.sum(np.equal(Avg_boost_result,c.reshape(1,4)[0]))/len(c)*100)
# # THe output is

# #print(h1)

# #Used for Q2.b from above we find classifer 2,3,5 and 8gives
# #best classsfication so using that to classify the updated error rates for bagging algorightm
# Avg_m=(h2+h3+h5+h8)/4

# condlist_avg_1=[Avg_m<0,Avg_m==0,Avg_m>0]
# choicelist_avg_1=[-1,0,1]
# Avg_boost_result_1=np.select(condlist_avg_1,choicelist_avg_1)
# print(np.sum(np.equal(Avg_boost_result_1,c.reshape(1,4)[0]))/len(c)*100)

w=1/len(c)
h_hat_classifier=[]
alpha_list=[]
k_max=2

for i in range(k_max):
    h,class_outputs=class_error_calc(w)
    # Change this parameter if the professor says max value is needed
    h_hat_argmin_error=min(h)
    h_hat_classifier.append(h.index(h_hat_argmin_error))
    h_hat_index=h.index(h_hat_argmin_error)

    if h_hat_argmin_error>0.5:
        exit
    else:
        alpha=1/2*np.log((1-h_hat_argmin_error)/h_hat_argmin_error)
        w_temp=w*np.exp(-alpha*c.T*class_outputs[h_hat_index])
        w_temp1=w_temp/np.sum(w_temp)
        alpha_list.append(alpha)
        w=w_temp1

print(alpha_list)
print(h_hat_classifier)


from .. import backend as D

__all__ = [
    'IntegratorTemplate',
#     'named_integrator',
]

# def named_integrator(name, alt_names=tuple(), order=1.0):
#     def wrap(f):
#         f.__name__ = str(name)
#         f.__alt_names__ = alt_names
#         if hasattr(f, 'order'):
#             f.__order__ = f.order
#         else:
#             f.__order__ = order
#         if hasattr(f, "final_state"):
#             f.__adaptive__ = D.shape(f.final_state)[0] == 2
#         else:
#             f.__adaptive__ = False
#         return f
#     return wrap

class IntegratorTemplate(object):
    def __init__(self):
        raise NotImplementedError("Do not initialise this class directly!")

    def forward(self, rhs, initial_time, initial_state, constants, timestep):
        raise NotImplementedError("Do not use this class directly! How did you initialise it??")

    def dense_output(self):
        raise NotImplementedError("Do not use this class directly! How did you initialise it??")

    __call__ = forward

    def update_timestep(self, initial_state, dState, diff, initial_time, timestep, tol=0.8):
        err_estimate = D.max(D.abs(D.to_float(diff)))
        relerr = D.max(D.to_float(self.atol + self.rtol * D.abs(initial_state) + self.rtol * D.abs(dState / timestep)))
        if err_estimate != 0:
            corr = timestep * tol * (relerr / err_estimate) ** (1.0 / self.order)
            if corr != 0:
                timestep = corr
        if err_estimate > relerr:
            return timestep, True
        else:
            return timestep, False

    @classmethod
    def __str__(cls):
        return cls.__name__

    def __repr__(self):
        if D.backend() == 'torch':
            return "<{}({},{},{},{})>".format(self.__class__.__name__, self.dim, self.dtype, self.rtol, self.atol)
        else:
            return "<{}({},{},{},{},{})>".format(self.__class__.__name__, self.dim, self.dtype, self.rtol, self.atol, self.device)


#!/usr/bin/env python

import argparse
import random
import logging

import quantgov
import sklearn.preprocessing

from pathlib import Path

from sklearn.externals import joblib as jl

log = logging.getLogger(Path(__file__).stem)

def create_label(streamer):
    """
    Assign a label to a set of documents using a CountVectorizer

    Arguments:
    * streamer: a quantgov.corpora.CorpusStreamer object

    Returns:
    * index:    A tuple of index values for the documents labeled
    * classes:  A tuple of names of classes for which labels are generated
    * labels:   The label or labels for each document. If more than one class
                is being labeled, each element of this tuple will be a tuple
                corresponding to the returned classes object

    """
    label_names = ('randomly_true',)
    labels = tuple(random.choice([True, False]) for doc in streamer)
    return tuple(streamer.index), label_names, labels

def parse_args():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('corpus', type=Path)
    parser.add_argument('-o', '--outfile')
    verbosity = parser.add_mutually_exclusive_group()
    verbosity.add_argument('-v', '--verbose', action='store_const',
                           const=logging.DEBUG, default=logging.INFO)
    verbosity.add_argument('-q', '--quiet', dest='verbose',
                           action='store_const', const=logging.WARNING)
    return parser.parse_args()

def main():
    args = parse_args()
    logging.basicConfig(level=args.verbose)
    driver = quantgov.load_driver(args.corpus)
    jl.dump(create_label(driver.get_streamer()), args.outfile)

if __name__ == "__main__":
    main()


# %%
# import configparser
from os import path
import os
import sys

from torch.utils.data import DataLoader
import configparser
from tqdm import tqdm

PATH_ROOT = ""
PATH_DATA = ""

creds_path_ar = ["../../credentials.ini", "credentials.ini"]

for creds_path in creds_path_ar:
    if path.exists(creds_path):
        config_parser = configparser.ConfigParser()
        config_parser.read(creds_path)
        PATH_ROOT = config_parser['MAIN']["PATH_ROOT"]
        PATH_DATA = config_parser['MAIN']["PATH_DATA"]
        WANDB_enable = config_parser['MAIN']["WANDB_ENABLE"] == 'TRUE'
        ENV = config_parser['MAIN']["ENV"]

# %%
# adding cwd to path to avoid "No module named src.*" errors
sys.path.insert(0, os.path.join(PATH_ROOT))
from src.data.iterable_dataset import Config, DataDict, StreamingDataset, MultiStreamDataLoader

config = Config(file_path=PATH_DATA, num_tracks=3, valratio=6, get_shifts=False, output_data_type='spectrogram',
                get_horizontal_flip=False, get_vertical_flip=False, mother_wavelet='cgau1', wavelet_scale=3,
                batch_size=50, tracks_in_memory=500, include_doppler=True)

dataset = DataDict(config=config)
track_count = len(dataset.train_data) + len(dataset.val_data)
if config['get_shifts']: segment_count = sum([(v['doppler_burst'].shape[0] - 32) for v in dataset.train_data]) + len(dataset.val_data)
else: segment_count = dataset.data_df.shape[0]
if config['get_horizontal_flip']: segment_count *= 2
if config['get_vertical_flip']: segment_count *= 2
# train_datasets = StreamingDataset.split_track_dataset(dataset.train_data, config=config, max_workers=4)
# train_loader = MultiStreamDataLoader(train_datasets)
train_dataset = StreamingDataset(dataset.train_data, config, shuffle=True)
train_loader = DataLoader(train_dataset, batch_size=config['batch_size'])
val_data = StreamingDataset(dataset.val_data, config, is_val=True, shuffle=True)
val_loader = DataLoader(val_data, batch_size=config['batch_size'])
segment_ids = []
train_sample_counts = []
for sample in tqdm(train_loader):
    sample_len = len(sample['target_type'])
    train_sample_counts.append(sample_len)
    segment_ids.append(sample['segment_id'])
val_sample_counts = [len(sample['target_type']) for sample in val_loader]
sample_counts = val_sample_counts + train_sample_counts
count = sum(sample_counts)
print(f'Total segments generated: {count}')
print(f'Total segments expected: {segment_count}')
print(f'Segments missing: {segment_count - count}')
print(f'Tracks created: {track_count}')


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

import socket

serverMACAddress = 'DC:A6:32:BD:83:6E' #PoMini Bluetooth MAC Address

port = 5
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))

def server_program(data) :
    data = str(data)
    s.send(data.encode())

while True:
    data = str(input())

    if data == "0":
        break
   #s.send(bytes(text, 'UTF-8'))
    s.send(data.encode())
s.close()


#Time - O(M*N + S*T), M - len(workers), N - len(bikes), S - len(Hashmap), T - len(longest list)
#space - O(S*T)
#Not available on leetcode

class Solution:
    def assignBikes(self,workers, bikes):
        if workers == None or len(workers) == 0 or bikes == None or len(bikes) == 0: # edge cases
            return []

        hashmap = {}

        work = len(workers)
        bike = len(bikes)

        result = [-1 for i in range(work)]

        for i in range(len(workers)):
            for j in range(len(bikes)):
                #finding the distance between each worker and all bikes
                dist = abs((workers[i][0]-bikes[j][0]) + (workers[i][1] - bikes[j][1]))
                # creating a hash map of distance and corresponding worker and bike
                if dist not in hashmap.keys():
                    hashmap[dist] = [(i,j)]
                else:
                    val = hashmap.get(dist)
                    val.append((i,j))
        visworkers = [False for i in range(work)]
        visbikes = [False for i in range(bike)]

        for key, value in hashmap.items():
            if value == None:
                    continue
            for wb in range(len(value)):
                worker = value[wb][0]
                biker = value[wb][1]

                # assigning biker to a worker
                if visworkers[worker] == False and visbikes[biker] == False:
                    visworkers[worker] =True
                    visbikes[biker] =True
                    result[worker] = biker
                    # print(result)
        return result

sol = Solution()
w = [[0,0],[1,1],[2,0]]
b = [[1,0],[2,2],[2,1]]
print(sol.assignBikes(w,b))


#########################################
# Lucas Eduardo Silva Braga - 201570023 #
#########################################

import Complexo, Correcao, math, cmath

#Exercício 3

r = 0.107
L = 1.355 * 10**(-3)
C = 0.008 * 10**(-6)
f = 60
l = 123
w = 2*math.pi*f

# Circuito Pi Nominal
Z = complex(r,w*L)
Z = Complexo.Rect(Z.real, Z.imag)
Y = complex(0,w*C)
Y = Complexo.Rect(Y.real,Y.imag)
print(Y.polar)
Z = Correcao.Hiperbolica(Z,Y,l).Zlinha()
Y = Correcao.Hiperbolica(Z,Y,l).Ylinha()
print(Y.polar)

Yhalf = Y.rect*(1/2)
Yquart = Y.rect*(1/4)

Yhalf = Complexo.Rect(Yhalf.real,Yhalf.imag)
Yquart = Complexo.Rect(Yquart.real,Yquart.imag)

print(f'Para o circuito Pi Equivalente, temos que:\n'
      f'   - Z\' = {Z.polar}\n'
      f'   - Y\'/2 = {Yhalf.polar} ',end='\n \n')
###############################################################

# Constantes Generalizadas
A = Complexo.ProdutoComplexa(Z,Yhalf)
A = Complexo.SomaComplexa(A,Complexo.Rect(1,0))

B = Z

C = Complexo.ProdutoComplexa(Z,Yquart)

C = Complexo.SomaComplexa(C,Complexo.Rect(1,0))
C = Complexo.ProdutoComplexa(Y, C)

D = A

print('As Constantes Generalizadas foram calculadas:\n'
      f'    - A = {A.polar}\n'
      f'    - B = {B.polar}\n'
      f'    - C = {C.polar}\n'
      f'    - D = {D.polar}',end='\n \n')
##############################################################

# Tensão no Barramento dado uma Carga e a Tensão
Vr = (135*10 **3)/math.sqrt(3)
Vr = Complexo.Polar(Vr, 0)
print(f' Vr = {Vr.polar}')

S = (50*10**6)
S_mono = S/3
angS = math.degrees(math.acos(0.95))
S_mono = Complexo.Polar(S_mono, angS)

ir_conj = Complexo.DivisaoComplexa(S_mono, Vr)
ir = Complexo.ConjugadoComplexo(ir_conj)
print(f' Ir = {ir.polar}')

Vs = Complexo.SomaComplexa(Complexo.ProdutoComplexa(A, Vr), Complexo.ProdutoComplexa(B,ir))
p1 = Complexo.ProdutoComplexa(A, Vr)
p2 = Complexo.ProdutoComplexa(B,ir)
Vs = Complexo.SomaComplexa(p1,p2)

Vs_linha = Complexo.ProdutoComplexa(Complexo.Rect(math.sqrt(3),0),Vs)
Vr_linha = Complexo.ProdutoComplexa(Complexo.Rect(math.sqrt(3),0),Vr)

print('Foram calculados: \n'
      f'    - |Vs_linha| = {Vs_linha.mod/10**3} kV\n'
      f'    - Abertura Angular = {Vs_linha.ang} graus', end='\n \n')

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

# Fluxo de Potência e Rendimento
i_s = Complexo.SomaComplexa(Complexo.ProdutoComplexa(C, Vr), Complexo.ProdutoComplexa(D,ir))
i_s_conj = Complexo.ConjugadoComplexo(i_s)
Ss = Complexo.ProdutoComplexa(Vs, i_s_conj)
Pmono_emi = Ss.rect.real
Qmono_emi = Ss.rect.imag
Ptri_emi = 3*Pmono_emi
Qtri_emi = 3*Qmono_emi

Sr = Complexo.ProdutoComplexa(Vr, ir_conj)
Pmono_recept = Sr.rect.real
Qmono_recept = Sr.rect.imag
Ptri_recept = 3*Pmono_recept
Qtri_recept = 3*Qmono_recept

rend = ((Ptri_recept)/(Ptri_emi))*100

print('Calculando o Fluxo de Potência, temos: \n'
      f'    - P_trifasica_emissor = {Ptri_emi/10**6} MW\n'
      f'    - Q_trifasica_emissor = {Qtri_emi/10**6} Mvar\n'
      f'    - P_trifasica_receptor = {Ptri_recept/10**6} MW\n'
      f'    - Q_trifasica_receptor = {Qtri_recept/10**6} Mvar\n'
      f'    - Rendimento = {rend} %\n'
      , end='\n \n')

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

# Regulação e Efeito Ferranti

Reg = Complexo.DivisaoComplexa(Vs_linha, A)
Reg = Complexo.SubtracaoComplexa(Reg, Vr_linha)
Reg = Complexo.DivisaoComplexa(Reg,Vr_linha)
Reg = Reg.mod * 100

Ferranti = Vs_linha.mod/Vr_linha.mod

print('Condições de Operação: \n'
      f'    - Regulação de Tensão = {Reg} % \n'
      f'    - Efeito Ferranti = {Ferranti} ', end='\n \n')


import json
import time
import os
import sys

HOME = os.path.expanduser("~")
CONFIG_DIR = os.path.join(HOME + '/.config/centarra-cli/')
HISTORY_FILE = os.path.join(CONFIG_DIR, '.cli_history')
SUBSTITUTES_FILE = os.path.join(CONFIG_DIR, 'substitutes.json')
USER_SUBSTITUTES_FILE = os.path.join(CONFIG_DIR, 'user_substitutes.json')
CONFIGURATION_FILE = os.path.join(CONFIG_DIR, 'config.json')
LANGUAGE_FILE = 'languages/%s.json'

if not os.path.exists(CONFIG_DIR):
    try:
        os.makedirs(CONFIG_DIR)
    except os.error:
        print("Configuration directory {} could not be created or written to.".format(CONFIG_DIR))
        sys.exit(2)
if not os.path.exists(HISTORY_FILE):
    open(HISTORY_FILE, 'w+').close()
if not os.path.exists(SUBSTITUTES_FILE):
    with open(SUBSTITUTES_FILE, 'w+') as out:
        json.dump({}, out)
if not os.path.exists(USER_SUBSTITUTES_FILE):
    with open(USER_SUBSTITUTES_FILE, 'w+') as out:
        json.dump({}, out)
if not os.path.exists(CONFIGURATION_FILE):
    print("Welcome to Centarra-CLI, for accessing Centarra's comfortable management panel from the comfort of your shell.")
    print("First, we're going to need to get some configuration over with.")
    #if not 'y' == raw_input("Do you have a Centarra account? [y/N]"):
        # POST /create with arguments username, password, email. error argument is the error in the form.
    configuration = {"centarra_username": raw_input("Enter your Centarra username: "),
                     "centarra_api_key": raw_input("Enter your Centarra API key, from http://billing.centarra.com/profile/index : "),
                     "language": "en_US",
                     "debug_mode": False}
    with open(CONFIGURATION_FILE, 'w') as out:
        json.dump(configuration, out)

class Sub():

    def __init__(self, f):
        self.f = f
        js = open(f, "r+")
        self.data = json.load(js)

    def dump_subs(self):
        with open(self.f, 'w') as out:
            json.dump(self.data, out)

class UserSubstitutions(Sub):

    def fetch(self, key):
        return self.data.get(key, None)

    def sub(self, var, val, multiword=False):
        if not var in self.data:
            self.data[var] = {"value": val, "multiword": multiword}
            self.dump_subs()

class Substitutions(Sub):

    def swap(self, type_, key, intended=int):
        #if intended and type(key) is intended:
        #    return key
        key = str(key).lower()
        res = self.data.get(type_, {}).get(key, {}).get("value", False)
        if not res:
            res = self.loop(type_, key + " ")  # Prefer it with a space afterwards. this prevents against 512 => 5120 mb
        if not res:
            res = self.loop(type_, key)
        if not res:
            return key
        return res

    def loop(self, type_, key):
        for check in self.data.get(type_, {}):
            dc = self.data.get(type_, {})[check]
            if key in str(check):
                return dc['value']

    def sub(self, type_, var, val):
        if not type_ in self.data:
            self.data[type_] = {}
        self.data[type_][str(var).lower()] = {"value": val, "expiry": time.time() + (60 * 60 * 24)}
        self.dump_subs()

substitutes = Substitutions(SUBSTITUTES_FILE)
user_substitutes = UserSubstitutions(USER_SUBSTITUTES_FILE)

def conf_get(file):
    js = open(file, "r+")
    data = json.load(js)
    return data

config = conf_get(CONFIGURATION_FILE)
language = conf_get(LANGUAGE_FILE % config['language'])

from libs.api import centarra, flashed


#!/usr/bin/env python

import numpy as np
import math
from sklearn import metrics
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt

Y,X1,X2,X3,X4,X5,X6,X7=[],[],[],[],[],[],[],[]
for i in open("id_label_pred.txt","r"):
	ele=i.rstrip().split()
	Y.append(int(ele[1]))
	X1.append(float(ele[2]))
	if float(ele[3])==0:
		X2.append(1000)
	else:
		X2.append(-(math.log10(float(ele[3]))))
	X3.append(1-float(ele[4]))
	X4.append(float(ele[5]))
	if float(ele[6])==0:
		X5.append(1000)
	else:
		X5.append(-(math.log10(float(ele[6]))))
	if float(ele[7])==0:
		X6.append(1000)
	else:
		X6.append(-(math.log10(float(ele[7]))))
	X7.append(float(ele[8]))

y=np.array(Y)
x1=np.array(X1)
x2=np.array(X2)
x3=np.array(X3)
x4=np.array(X4)
x5=np.array(X5)
x6=np.array(X6)
x7=np.array(X7)

fpr1,tpr1,threshold1=metrics.roc_curve(y,x1)
fpr2,tpr2,threshold2=metrics.roc_curve(y,x2)
fpr3,tpr3,threshold3=metrics.roc_curve(y,x3)
fpr4,tpr4,threshold4=metrics.roc_curve(y,x4)
fpr5,tpr5,threshold5=metrics.roc_curve(y,x5)
fpr6,tpr6,threshold6=metrics.roc_curve(y,x6)
fpr7,tpr7,threshold7=metrics.roc_curve(y,x7)

auc1=metrics.auc(fpr1,tpr1)
auc2=metrics.auc(fpr2,tpr2)
auc3=metrics.auc(fpr3,tpr3)
auc4=metrics.auc(fpr4,tpr4)
auc5=metrics.auc(fpr5,tpr5)
auc6=metrics.auc(fpr6,tpr6)
auc7=metrics.auc(fpr7,tpr7)

fig = plt.figure(figsize=[6.4,6.4])
ax = fig.add_subplot()
ax.set_title('ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.plot(fpr1,tpr1,color="red",label='Tombo (AUC = %0.2f)' % auc1)
plt.plot(fpr2,tpr2,color="orange",label='Nanocompore (AUC = %0.2f)' % auc2)
plt.plot(fpr3,tpr3,color="yellow",label='Xpore (AUC = %0.2f)' % auc3)
plt.plot(fpr4,tpr4,color="green",label='DiffErr (AUC = %0.2f)' % auc4)
plt.plot(fpr5,tpr5,color="blue",label='DRUMMER (AUC = %0.2f)' % auc5)
plt.plot(fpr6,tpr6,color="purple",label='ELIGOS (AUC = %0.2f)' % auc6)
plt.plot(fpr7,tpr7,color="pink",label='Epinano (AUC = %0.2f)' % auc7)
plt.plot([0, 1], [0, 1], 'k--')
plt.legend(fontsize="small",frameon=False)
plt.savefig("ROC.pdf")

pr1,rc1,threshold1=metrics.precision_recall_curve(y,x1)
pr2,rc2,threshold2=metrics.precision_recall_curve(y,x2)
pr3,rc3,threshold3=metrics.precision_recall_curve(y,x3)
pr4,rc4,threshold4=metrics.precision_recall_curve(y,x4)
pr5,rc5,threshold5=metrics.precision_recall_curve(y,x5)
pr6,rc6,threshold6=metrics.precision_recall_curve(y,x6)
pr7,rc7,threshold7=metrics.precision_recall_curve(y,x7)

prauc1=metrics.auc(rc1,pr1)
prauc2=metrics.auc(rc2,pr2)
prauc3=metrics.auc(rc3,pr3)
prauc4=metrics.auc(rc4,pr4)
prauc5=metrics.auc(rc5,pr5)
prauc6=metrics.auc(rc6,pr6)
prauc7=metrics.auc(rc7,pr7)

fig = plt.figure(figsize=[6.4,6.4])
ax = fig.add_subplot()
ax.set_title('PR curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.plot(rc1,pr1,color="red",label='Tombo_com (AUC = %0.2f)' % prauc1)
plt.plot(rc2,pr2,color="orange",label='Nanocompore (AUC = %0.2f)' % prauc2)
plt.plot(rc3,pr3,color="yellow",label='Xpore (AUC = %0.2f)' % prauc3)
plt.plot(rc4,pr4,color="green",label='DiffErr (AUC = %0.2f)' % prauc4)
plt.plot(rc5,pr5,color="blue",label='DRUMMER (AUC = %0.2f)' % prauc5)
plt.plot(rc6,pr6,color="purple",label='ELIGOS_diff (AUC = %0.2f)' % prauc6)
plt.plot(rc7,pr7,color="pink",label='Epinano_delta (AUC = %0.2f)' % prauc7)
plt.plot([0, 1], [0, 1], 'k--')
plt.legend(fontsize="small",frameon=False)
plt.savefig("PR.pdf")


# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
from datadog_checks.istio.constants import BLACKLIST_LABELS

from .common import COUNT_METRICS

def _assert_tags_excluded(aggregator, addl_blacklist):
    """
    Test excluded labels
    """
    fail = 0
    for _, stubs in aggregator._metrics.items():
        for stub in stubs:
            for tag in stub.tags:
                for excluded_tag in BLACKLIST_LABELS + addl_blacklist:
                    if tag.startswith(excluded_tag + ':'):
                        fail += 1
    assert fail == 0
    aggregator.assert_all_metrics_covered()

def _assert_metric(aggregator, metric):
    if metric in COUNT_METRICS:
        aggregator.assert_metric(metric, metric_type=aggregator.MONOTONIC_COUNT)
    else:
        aggregator.assert_metric(metric, metric_type=aggregator.GAUGE)


#SDK 2.2.0 GA upgrade utilities

# Replace text in propertyId. Useful for changing define without checking
# object/module or type specifiers.
def renamePropertyText(xmlmode, oldPropertyText, newPropertyText, verbose=False):
  properties = xmlmode.getProperty()
  for p in properties:
    # prop[0] = module
    # prop[1] = property
    # prop[2] = type
    prop=p.getPropertyId().split(".")
    if oldPropertyText == prop[1]:
      prop[1] = newPropertyText
      newPropertyId = ".".join(prop)
      if verbose:
        print ("Renaming %s->%s" % (p.getPropertyId(), newPropertyId))
      p.setPropertyId(newPropertyId)


#! /bin/python3

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

print (s)

#s.connect(('www.purple.com', 80))
s.bind(('127.0.0.1', 3355))
s.listen(1)
conn, addr = s.accept()
print (conn)
print (addr)

while True :
    data = conn.recv(4)
    print (data.decode(), end='')
    if not data: break
    conn.send(data)

s.close()


carrinho = []
produto = ''

print('Adicione produtos ao carrinho ou digite sair: ')
while produto != 'sair':
    produto = input()
    if produto != 'sair':
        carrinho.append(produto)
carrinho.sort()
print('')
print('Produtos no carrinho: ')
for produto in carrinho:
    print(produto.title())


'''
Author: Leandro Kellermann de Oliveira <kellermann@alumni.usp.br>
Date:   2021-03-29 08:07:45
Last modified by:   Leandro Kellermann de Oliveira <kellermann@alumni.usp.br>
Last modified: 2021-03-29 08:07:45
myProjects <<license>>
'''
import yaml
import configparser
import os
from aws.redshift_cluster import Cluster

def main() -> None:
    """Method to create a RedShift cluster, open its VPC\
         and return the dwh.cfg file for later access.
    """
    # Reads YAML file which contains the RedShift
    # cluster creation parameters.
    print('Loading cluster setup from setup.yaml file.')
    with open("setup.yaml", 'r') as stream:
        setup = yaml.safe_load(stream)

    # Instantiates Cluster object:
    red = Cluster(setup['AWS']['KEY'],
                  setup['AWS']['SECRET'],
                  setup['AWS']['REGION'],
                  setup['CLUSTER']['CLUSTER_IDENTIFIER']
                  )

    # Creates RedShift cluster if does not exists:
    red.create_cluster(setup['CLUSTER']['CLUSTER_TYPE'],
                       setup['CLUSTER']['NUM_NODES'],
                       setup['CLUSTER']['NODE_TYPE'],
                       setup['CLUSTER']['CLUSTER_DB_NAME'],
                       setup['CLUSTER']['DB_USER'],
                       setup['CLUSTER']['DB_PASSWORD'],
                       setup['IAM_ROLE_NAME'],
                       )

    # Open Virtual Private Cloud if is not already open:
    red.ec2_redshift_connection(setup['CLUSTER']['CLUSTER_PORT'])

    # Get RedShift cluster properties:
    prop = red.cluster_properties
    print(prop)

    # Get RedShift Cluster endpoint address:
    endpoint = prop['Endpoint']['Address']

    # Get RedShift cluster associated IAM Role ARN:
    for x in prop['IamRoles']:
        if isinstance(x, dict):
            try:
                iam_arn = x['IamRoleArn']
            except Exception as e:
                print(f'Exception: {e} ')

    # Dump dwh.cfg file:
    config = configparser.ConfigParser()
    config['CLUSTER'] = {}
    config['CLUSTER']['HOST'] = endpoint
    config['CLUSTER']['DB_NAME'] = setup['CLUSTER']['CLUSTER_DB_NAME']
    config['CLUSTER']['DB_USER'] = setup['CLUSTER']['DB_USER']
    config['CLUSTER']['DB_PASSWORD'] = setup['CLUSTER']['DB_PASSWORD']
    config['CLUSTER']['DB_PORT'] = str(setup['CLUSTER']['CLUSTER_PORT'])
    config['IAM_ROLE'] = {}
    config['IAM_ROLE']['ARN'] = iam_arn

    if os.path.exists('dwh.cfg'):
        os.remove('dwh.cfg')

    # Saving parameters into a file:
    with open('dwh.cfg', 'w') as f:
        config.write(f)

    print('Cluster created!')

if __name__ == "__main__":
    main()


import arrow

from scrapy.commands import ScrapyCommand

from ..models import db, Lead
from ..helpers import send_template_email

class Command(ScrapyCommand):

    def syntax(self):
        return ''

    def short_desc(self):
        return 'Sends Tagless leads report'

    def add_options(self, parser):
        ScrapyCommand.add_options(self, parser)
        parser.add_option('-d', '--days', dest='days', help='Previous day count')
        parser.add_option('-r', '--recipient', dest='recipient', help='Email address to send repo')

    def run(self, args, opts):
        if opts.days:
            if not opts.recipient:
                raise ValueError('Recipient not provided')
            days = int(opts.days)
            leads = db.query(Lead).filter(
                Lead.source_zip.is_(None),
                Lead.created_date >= arrow.now().replace(days=-days).datetime,
                Lead.source == 'Zillow.com'
            ).all()

            if leads:
                send_template_email(
                    subject='REPORT - Zip Tagless Leads', recipients=[opts.recipient],
                    template_name='tagless-report.html', **{'leads': leads, 'days': days})
            else:
                print('No leads')
        else:
            print('Days not provided')


""" Microservice configurator file """
##
#
# This file configures the microservice.
#
# This microservice uses 'dotenv' (.env) files to get environmental variables.
# This is done to configure sensible parameters, like database connections,
# application secret keys and the like. In case the 'dotenv' file does not
# exists, a warning is generated at run-time.
#
# This application implements the '12 Factor App' principle:
# https://12factor.net and https://12factor.net/config
#
# Note about PyLint static code analyzer: items disable are false positives.
#
##

# pylint: disable=invalid-name;
# pylint: disable=too-few-public-methods;
# pylint: disable=C0301, R0912, R0914, R0915, R1702, W0703

import sys
from os import environ, path
from environs import Env

ENV_FILE = path.join(path.abspath(path.dirname(__file__)), '.env')

try:
    ENVIR = Env()
    ENVIR.read_env()
except Exception as e:
    print('Error: .env file not found: %s' % e)
    sys.exit(1)

class Config:
    """ This is the generic loader that sets common attributes """
    JSON_SORT_KEYS = False
    if environ.get('SERVERMQTT'):
        MQTT_HOST = ENVIR('SERVERMQTT')
    if environ.get('CALL'):
        CALL = ENVIR('CALL')


#%%
########################################################################################################
########################################################################################################
# I wrote these functions to fix the way dates came out of a databases that I was using for this project.
# The goal of these functions is to write the date in a format that excel will format as a date,
# so the end user can change date formats and filter by date.
#
# Date format from DB1 is:
#    -2020-01-01 01:01:01.111
#
# Date formats from DB2 are:
#    -Jan 15 2020 11:06AM
#    -2020-01-01 01:01:01.111
########################################################################################################
########################################################################################################

#this function is used when iterating through columns and date format is: 2020-01-01 01:01:01.111
def excel_date(x):
    return datetime.datetime.strptime(x, '%m/%d/%Y').date()

#this function treats an entire column that is in format: 2020-01-01 01:01:01.111
def fix_short_date(df, column):
    df[column] = pd.to_datetime(df[column])
    df[column] = df[column].dt.date
    df[column] = pd.to_datetime(df[column]).dt.strftime('%m-%d-%Y')
    df[column] = df[column].astype(str)
    new_list = []
    for i in df[column]:
        if str(i) == "":
            new_list.append("")
        elif str(i) == "nan":
            new_list.append("")
        elif str(i) == "NaT":
            new_list.append("")
        else: new_list.append(datetime.datetime.strptime(i, '%m-%d-%Y').date())
    df.drop(columns = column, inplace = True)
    df[column] = new_list

#this function treats an entire column that is in format: Jan 15 2020 11:06AM
def fix_long_date(df, column):
    df[column] = df[column].astype(str)
    df2 = df[column].str.split(n = 1, expand = True)
    df3 = df2[1].str.split(n = 1, expand = True)
    df4 = df3[1].str.split(n = 1, expand = True)
    d = {'Jan':'1', 'Feb':'2', 'Mar':'3', 'Apr':'4', 'May':'5', 'Jun':'6', 'Jul':'7', 'Aug':'8', 'Sep':'9', 'Oct':'10', 'Nov':'11', 'Dec':'12' }
    df2[0] = df2[0].map(d)
    df2[2] = df2[0] + "/" + df3[0] + "/" + df4[0]
    new_list = []
    for i in df2[2]:
        if str(i) == "":
            new_list.append("")
        elif str(i) == "nan":
            new_list.append("")
        elif str(i) == "NaT":
            new_list.append("")
        else: new_list.append(datetime.datetime.strptime(i, '%m/%d/%Y').date())
    df.drop(columns = column, inplace = True)
    df[column] = new_list


# Python Object Oriented Programming
# 'Class' (Employee variables the same across all instances
# 'Instance' (emp_X) variables can be different

# -------Create class --------
class Employee:

    raise_amount = 1.04
    num_of_employees = 0

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'

        Employee.num_of_employees += 1

    def fullname(self):
        return '{} {}'.format(self.first, self.last)

# call 'self.raise_amount' means that you can change amount for each instance
    def apply_raise(self):
        self.pay = int(self.pay * self.raise_amount)

    # Create a class method
    @classmethod
    def set_raise_amount(cls, amount):
        cls.raise_amount = amount

    @classmethod
    def from_string(cls, emp_str):
        first, last, pay = emp_str.split('-')
        return cls(first, last, pay)

    @staticmethod
    def is_workday(day):
        if day.weekday() == 5 or day.weekday() == 6:
            return False
        return True

# ------ Data input -----

emp_1 = Employee('Jacob', 'Marlow', 50000)
emp_2 = Employee('James', 'Barlow', 60000)

emp_str_1 = 'john-doe-70000'
emp_str_2 = 'steve-smith-50000'
emp_str_3 = 'jane-doe-60000'

new_emp_1 = Employee.from_string(emp_str_1)

print(new_emp_1.email)

# ----- Call methods ---------------

# call data
print(emp_1.email)

# call method
print(emp_2.fullname())

# apply method
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)

# call all instance info
print(emp_1.__dict__)

# call all class info
print(Employee.__dict__)

# print raise amount (change instance)
emp_1.raise_amount = 1.05

print(Employee.raise_amount)
print(emp_1.raise_amount)

# print raise amount (change in class method)
Employee.set_raise_amount(1.07)

print(Employee.raise_amount)
print(emp_1.raise_amount)

# Number of employees
print(Employee.num_of_employees)

# Static method
import datetime
my_date = datetime.date(2016, 7, 11)

print(Employee.is_workday(my_date))


import urllib
import requests
import exifread
import imageio
import uuid
import os
import sys
import logging
import ipdb
import pytz
from PIL import Image
from pytz import timezone
from datetime import datetime
from django_rq import job, get_queue
from django.conf import settings
from django.db import transaction
from apps.cascadia.models import Photo, Visit, Photoset
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile

logger = logging.getLogger(__name__)

def jobsInRedisQueue():
    high_queue = get_queue('high')
    default_queue = get_queue('default')
    low_queue = get_queue('low')

    jobs = {}

    jobs['count'] = high_queue.count + default_queue.count + low_queue.count

    if not jobs['count']:
        return False

    current_date_time = datetime.now(tz=pytz.timezone(settings.TIME_ZONE))

    if high_queue.count:
        high_queue_time = (current_date_time - high_queue.get_jobs()[0].enqueued_at.replace(tzinfo=pytz.UTC)).seconds
    else:
        high_queue_time = 0

    if low_queue.count:
        low_queue_time = (current_date_time - low_queue.get_jobs()[0].enqueued_at.replace(tzinfo=pytz.UTC)).seconds
    else:
        low_queue_time = 0

    if default_queue.count:
        default_queue_time = (current_date_time - default_queue.get_jobs()[0].enqueued_at.replace(tzinfo=pytz.UTC)).seconds
    else:
        default_queue_time = 0

    jobs['time_remaining'] = max([
        high_queue_time,
        low_queue_time,
        default_queue_time
    ])

    return jobs

def getEXIFData(f):
    # local_filename, headers = urllib.request.urlretrieve(photo.image.url)
    # f=open(local_filename, 'rb')
    tags = exifread.process_file(f)
    mydict = {}
    for tag in tags.keys():
        try:
            mydict[tag] = tags[tag].printable
        except Exception as e:
            logger.error(
                'Error parsing exif dict key: {}'.format(e)
            )
    # photo.exif_data = mydict
    # photo.save()
    # saveDateTime.delay(photo)
    return mydict

def getDateTime(photo):
    tz = timezone(settings.TIME_ZONE)
    mydate = datetime.strptime(
        photo.exif_data['Image DateTime'], "%Y:%m:%d %H:%M:%S")
    loc_dt = tz.localize(mydate)
    return loc_dt

# Group all images that have not yet been grouped into visits
def createVisits(photoset):

    # First find all photosets that don't have a set date
    d = Photoset.objects.get(
        id=photoset
    )

    # Handle missing exif photos
    # ---------------------------------------
    no_exif_photos = Photo.objects.filter(
        photoset=photoset,
        exif_data={}
    )

    if no_exif_photos:
        visit = Visit()
        visit.save()

        for p in no_exif_photos:
            p.visit = visit
            p.save()
        logger.info('{} photos with missing exif data'.format(photos.count()))
    # ----------------------------------------

    photos = Photo.objects.filter(
        photoset=photoset,
        visit__isnull=True
    ).order_by('image_date')

    # Create the first visit
    visit = Visit()
    visit.date = photos[0].image_date
    visit.save()
    logger.info('{}-{}'.format(photos[0].photoset.location, visit))

    for p in photos:
        # Delete any visit that was previously associated with this photo
        try:
            p.visit.delete()
        except Exception:
            pass

        # This will error on the first iteration, which is what we want
        # TODO: Check to see if this is where bad images are getting handled
        # I think images with bad or missing exif data will get handled here and
        # all get bumped into the same visit.
        # TODO: Make a special handler that groups all images with bad or missing exif data
        # into their own visit
        try:
            timegap = p.image_date - oldtime
        except Exception:
            p.visit = visit
            p.save()
            oldtime = p.image_date
            continue

        # Create a new visit if enough time has passed
        if timegap.total_seconds() > settings.CAMERA_VISIT_TIME_GAP:
            # make a gif for the previous visit
            makeGIF.delay(visit)
            # Create a new visit object
            visit = Visit()
            visit.date = p.image_date
            visit.save()
            location = p.photoset.location
            logger.info('{}-{}'.format(p.photoset.location, visit))

        p.visit = visit
        p.save()
        oldtime = p.image_date

    # make a gif for the last visit
    makeGIF.delay(visit)
    d.save()

def makeGifsForAllVisits():

    # Check the job queue.  We don't want to make gifs while images are
    # still being grouped into visits
    if jobsInRedisQueue():
        logger.info('Jobs are in the queue, try again later.')
        return 'Jobs are in the queue, try again later.'

    visits = Visit.objects.filter(
        gif=''
    ).order_by(
        'date'
    )
    for v in visits:
        makeGIF.delay(v)

@job('low')
def makeGIF(visit, previous_tries=False):
    gifpics = Photo.objects.filter(
        visit=visit,
        image_small__isnull=False
    ).order_by('image_date','image')

    images = []
    try:
        for g in gifpics:
            file_path = g.image_small.url
            images.append(imageio.imread(file_path))

        outputIoStream = BytesIO()

        imageio.mimsave(outputIoStream, images, format='GIF', fps=settings.GIF_FPS)
    except Exception as e:

        if not previous_tries:
            logger.error('First try failed making gif for {}: {}'.format(
                visit,
                e
            ))
            makeGIF.delay(visit, previous_tries=True)
        else:
            logger.error('Second try failed making gif for {}: {}'.format(
                visit,
                e
            ))

        raise

    outputIoStream.seek(0)

    image = InMemoryUploadedFile(
        outputIoStream,
        'ImageField',
        # TODO: maybe use visit.date for gif names so at least we have some reference
        "{}.gif".format(visit.date.strftime("%y%m%d-%H%M%S")),
        'image/gif',
        sys.getsizeof(outputIoStream),
        None
    )

    visit.gif = image
    visit.gif_size = sys.getsizeof(outputIoStream)
    visit.gif_fps = settings.GIF_FPS
    visit.save()

    logger.info(
        '{} pictures for {}'.format(
            gifpics.count(),
            visit
        )
    )
    logger.info(
        '{} MB file'.format(
            int(sys.getsizeof(outputIoStream) / 1000000)
        )
    )

    for g in gifpics:
        g.visit = visit
        g.save()

@job('high')
def compressImageFile(photo):
    try:
        photo.compressAndSave()
    except Exception as e:
        logger.error('Error compressing image file ({}): {}'.format(photo, e))

@job('high')
def testLogging():
    logger.info('I\'m an info log')
    logger.warn('I\'m a warning log')
    logger.debug('I\'m a debug log')
    logger.error('I\'m an error log')


#
# Stolen from Falcon (falconframework.org)
#
import imp
import io
from os import path
from setuptools import setup, find_packages

ROOT_DIR = path.abspath(path.dirname(__file__))

SRC_DIR = path.join(ROOT_DIR, 'aiochannel')
VERSION = imp.load_source('version', path.join(SRC_DIR, 'version.py'))
VERSION = VERSION.__version__
README = path.join(ROOT_DIR, 'README.md')

REQUIRES = []

try:
    from pypandoc import convert

    def read_md(f):
        return convert(f, 'rst')
except ImportError:
    print("warning: pypandoc module not found, could not convert Markdown to RST")

    def read_md(f):
        return io.open(README, 'r', encoding='utf-8').read()

LONG_DESCRIPTION = read_md(README)

packages = find_packages(exclude=['tests'])

setup(
    name='aiochannel',
    version=VERSION,
    description='asyncio Channels (closable queues) inspired by golang',
    long_description=LONG_DESCRIPTION,
    classifiers=[
        'Development Status :: 4 - Beta',
        'License :: OSI Approved :: Apache Software License',
        'Natural Language :: English',
        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],
    keywords='asyncio aio chan channel gochan',
    author='Henrik Tudborg',
    author_email='henrik@tud.org',
    url='github.com/tbug/aiochannel',
    packages=packages,
    include_package_data=True,
    zip_safe=False,
    install_requires=REQUIRES,
    setup_requires=[],
    license='Apache 2.0'
)


# !/usr/bin/env python

import RPi.GPIO as GPIO
import time
import signal
import atexit
import os
import cv2
import numpy as np
import AdbHelper as ad

scale = 0.5
alpha = 4.2
beta = 0

# 640,360

def getImg(path):
	img = cv2.imread("img\\" + path)
	img = cv2.resize(img, (int(img.shape[1] * 0.7), int(img.shape[0] * 0.7)), interpolation=cv2.INTER_CUBIC)

	return img

def getImgFromScreen(path):
	ad.screenShot("img", path)

	img = cv2.imread("img\\" + path)

	img = cv2.resize(img, (int(img.shape[1] * scale), int(img.shape[0] * scale)), interpolation=cv2.INTER_CUBIC)

	return img

def img2Canny(img):
	img = cv2.GaussianBlur(img, (3, 3), 0)
	canny = cv2.Canny(img, 50, 70)

	return canny

def showImg(img):
	cv2.imshow("img", img)

	cv2.waitKey(0)
	cv2.destroyAllWindows()

def cutImg(img, tuple0, tuple1):
	temp = img
	temp = temp[tuple0[0]:tuple1[0], tuple0[1]:tuple1[1]]
	return temp

def jumpMatchTemplate(img, template):
	width = height = 0
	img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

	res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)

	loc = np.where(res == np.max(res))

	# print(template.shape)
	# print(template.shape[0])
	for pt in zip(*loc[::-1]):
	    width = pt[0] + template.shape[1] // 2  # 114 + 21
	    height = pt[1] + template.shape[0] - 10  # 306 + 48
	    # cv2.rectangle(img, (pt[0], pt[1]), (pt[0] + template.shape[1], pt[1] + template.shape[0]), (0, 0, 255), 2)
	# 114 306
	# cv2.rectangle(img, (width,height), (width + 3, height + 3), (0, 255, 0), 2)
	return (height, width)

def matchScreen(img,template):
	width = height = 0
	img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

	res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)

	loc = np.where(res == np.max(res))

	# print(template.shape)
	# print(template.shape[0])
	for pt in zip(*loc[::-1]):
	    width = pt[0]# 114 + 21
	    height = pt[1]# 306 + 48
	    cv2.rectangle(img, (pt[0], pt[1]), (pt[0] + template.shape[1], pt[1] + template.shape[0]), (0, 0, 255), 2)
	return (height, width)

def deleteChessman(canny, loc, temshape):
	temp_height = loc[0] - temshape[0] + 10
	temp_width = loc[1] - temshape[1] // 2

	for height in range(0, temshape[0]):
	    for width in range(0, temshape[1]):
	        canny[temp_height + height][temp_width + width] = 0

	return canny

def getHighPoint(canny_cut):
	for h in range(0, canny_cut.shape[0]):
	    for w in range(0, canny_cut.shape[1]):
	        if canny_cut[h][w] == 255: return h + 128, w

	return -1, -1

def getRightPoint(canny_cut):
	for w in range(canny_cut.shape[1] - 1, 0, -1):
	    for h in range(0, canny_cut.shape[0]):
	        if canny_cut[h][w] == 255: return h + 128, w

	return -1, -1

def getCenterPoint(high_point, right_point):
	return right_point[0], high_point[1]

def calDistance(begin_point, center_point):
	dis2 = np.power(begin_point[0] - center_point[0], 2) + np.power(begin_point[1] - center_point[1], 2)
	return np.sqrt(dis2)

def outputDisTime(path, distance, press_time):
	f = open(path, "a")
	f.write(str(round(distance, 2)) + " " + str(round(press_time, 2)) + "\n")
	f.close()

def drawPoint(img, point, color):
	cv2.rectangle(img, (point[1], point[0]), (point[1] + 3, point[0] + 3), color, 3)

def SteeringEngineInitial():
	atexit.register(GPIO.cleanup)

	servopin = 21
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(servopin, GPIO.OUT, initial=False)
	p = GPIO.PWM(servopin, 50)  # 50HZ
	p.start(0)
	time.sleep(2)
	return p

# not include time delay, so u must add time.sleep after this function
def rotate2Degree(p, degree):
	p.ChangeDutyCycle(0.5 + 10 * degree / 180)
	time.sleep(0.02)
	# p.ChangeDutyCycle(0)
	# time.sleep(0.2)

def shoot():
	# take a picture after 1s
	os.system("raspistill -t 1000 -o image.jpg")

def test(p):
	while (True):
	    for i in range(0, 181, 10):
	        p.ChangeDutyCycle(2.5 + 10 * i / 180)
	        time.sleep(0.02)
	        p.ChangeDutyCycle(0)
	        time.sleep(0.2)

	    for i in range(181, 0, -10):
	        p.ChangeDutyCycle(2.5 + 10 * i / 180)
	        time.sleep(0.02)
	        p.ChangeDutyCycle(0)
	        time.sleep(0.2)

if __name__ == '__main__':
	p = SteeringEngineInitial()

	first_flag = 0
	screen_point = None
	screen_template = None

	while True:

	    shoot()

	    img = cv2.imread("image.jpg")
	    img = cv2.resize(img, (int(img.shape[1] * 0.42), int(img.shape[0] * 0.42)), interpolation=cv2.INTER_CUBIC)

	    if img is None:
	        print("the path of img is not correct, can't get img, please check the code or path!!")
	    else:
	        print("get the image!")

	    # only first time
	    if first_flag == 0:
	        screen_template = cv2.imread("begin.jpg")
	        screen_template = cv2.resize(screen_template,
	                                        (int(screen_template.shape[1] * 0.5), int(screen_template.shape[0] * 0.5)),
	                                        interpolation=cv2.INTER_CUBIC)

	        if screen_template is None:
	            print("the path of screen_template is not correct, can't get img, please check the code or path!!")
	        else:
	            print("get the screen_template!")

	        screen_point = matchScreen(img, screen_template)
	        first_flag = 1

	    # cv2.imshow("img",img)
	    # cv2.waitKey()

	    img = cutImg(img, screen_point,
	                    (screen_point[0] + screen_template.shape[0], screen_point[1] + screen_template.shape[1]))

	    print("cut the image! Job's finished")
	    # cv2.imshow("img", img)
	    # cv2.waitKey()

	    chessman_template = cv2.imread("template.png")
	    chessman_template = cv2.resize(chessman_template,
	                                    (int(chessman_template.shape[1] * 0.7), int(chessman_template.shape[0] * 0.7)),
                                    interpolation=cv2.INTER_CUBIC)
	    if chessman_template is None:
	        print("the path of chessman_template is not correct, can't get img, please check the code or path!!")
	    else:
	        print("get the chessman_template!")

	    begin_point = jumpMatchTemplate(img, chessman_template)
	    # cv2.imshow("img", img)
	    # cv2.waitKey()

	    canny = img2Canny(img)

	    canny = deleteChessman(canny, begin_point, chessman_template.shape)
	    # cv2.imshow("canny", canny)
	    # cv2.waitKey()

	    print("yo~~, canny's finished")

	    print("canny's shape:" + str(canny.shape))  # 640 * 360

	    print("calculate high_point, right_point and center_point...")

	    canny_cut_top = cutImg(canny, (128, 10), (305, 350))
	    # cv2.imshow("canny_cut_top", canny_cut_top)
	    # cv2.waitKey()

	    canny_cut_center = cutImg(canny, (340, 10), (400, 350))

	    high_point = getHighPoint(canny_cut_top)
	    print("high_point:" + str(high_point))
	    right_point = getRightPoint(canny_cut_top)
	    print("right_point:" + str(right_point))
	    center_point = getCenterPoint(high_point, right_point)
	    center_point = (center_point[0], center_point[1] + 10)
	    print("center_point:" + str(center_point))

	    # drawPoint(img, begin_point, (0, 255, 0))
	    # drawPoint(img, center_point, (255,0,0))
	    #
	    # cv2.imshow("img", img)
	    # cv2.waitKey()

	    print("calculation is finished!!")

	    dis_cal = calDistance(begin_point, center_point)
	    print("dis_cal:" + str(dis_cal))
	    press_time = dis_cal * alpha + beta

	    print("presstime:" + str(press_time / 1000.0))

	    rotate2Degree(p,0)
	    time.sleep(press_time/1000.0 + 0.1)
	    rotate2Degree(p,90)
	    time.sleep(1)


#!/usr/bin/env python3
"""
基于 Python 3.x 的命令行版 12306 火车票查询器

Usage:
    app.py (<from_city>) (<dest_city>) [<date>] [-g][-c][-d][-k][-t][-z][-l]
"""

import re
import sys
import time
from datetime import date
from json import JSONDecodeError

import requests
from docopt import docopt
from prettytable import PrettyTable

import colortext
from mysqlite import Sqlite

class TrainTicketsFinder:

    def __init__(self):
        # 解析命令行参数
        self.args = docopt(__doc__)
        # 指定 requests 响应编码
        self.response_encoding = 'utf-8'
        # 每次接口请求间隔时间限制，防止请求过快被返回异常
        self.request_interval_seconds = 5
        # 不支持的坐席类别用下面的符号表示
        self.unsupported_seat = colortext.light_yellow('×')
        # 初始化数据库并创建数据表
        self.db = Sqlite('data.sqlite3')
        # 获取并加载全国火车站站名信息
        self.fetch_all_station_names()
        # 尝试获取 12306 网站 cookie
        self.cookies = requests.get('https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc').cookies

    def fetch_all_station_names(self):
        """获取全国火车站站名信息，在 12306 网站上是以下面的 JavaScript 链接直接写死了返回来的"""
        if self.db.select_one_from(self.db.table_name_station) is None:
            response = requests.get('https://www.12306.cn/index/script/core/common/station_name_v10042.js')
            response.encoding = self.response_encoding
            if response.ok:
                stations = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)\|([a-z]+)\|([a-z]+)', response.text)
                self.db.batch_insert_stations_data(stations)

    def query_satisfied_trains_info(self):
        """
        查询满足条件的车票信息，接口需要传递四个参数
        leftTicketDTO.train_date - 乘车日期，例如：2019-10-24，我这里做了一点优化处理，如果命令行不输日期参数，就默认查询当天
        leftTicketDTO.from_station - 出发车站，需要传车站的站名英文简写，我这里做了一点简化，只传出发的城市名称，接口也能够正确查询出车票信息
        leftTicketDTO.to_station - 到达车站，同出发车站做了一样的处理
        purpose_codes - 普通票或学生票，普通票传值为 ADULT
        """
        from_city, dest_city, from_station_en, dest_station_en, train_date, need_filter = self._check_input_args()
        api = 'https://kyfw.12306.cn/otn/leftTicket/queryO'
        request_params = {
            'leftTicketDTO.train_date': train_date,
            'leftTicketDTO.from_station': from_station_en,
            'leftTicketDTO.to_station': dest_station_en,
            'purpose_codes': 'ADULT'
        }
        response = requests.get(api, params=request_params, cookies=self.cookies)

        if response.ok:
            try:
                response_json = response.json()
                train_list = response_json.get('data').get('result')
                print(colortext.light_green('\n车次及余票信息查询成功，正在查询票价数据...\n'))
            except JSONDecodeError:
                print(colortext.light_red('[ERROR] JSON解析异常，可能是旧的请求API发生变化\n%s' % api))
                sys.exit()

            result_table = PrettyTable()
            table_header = ['车次', '车站', '时间', '历时', '商务座/特等座', '一等座', '二等座', '软卧', '硬卧', '硬座', '站票']
            result_table.field_names = table_header

            # 遍历查询到的全部车次信息
            satisfied_train_count = 0
            for train in train_list:
                '''
                按 12306 现有的接口，返回的数据是一个列表，单条数据是以 | 分隔的字符串
                在分析这里的数据时，我是靠规律和基本猜测确定对应数据在哪个字段上的
                不知道官方接口为什么要这样返回数据，防止爬虫？感觉这样也防不住啊！
                '''
                train_info = train.split('|')
                train_number, station, train_time, duration = self._format_train_info_fields(train_info)

                # 根据输入参数过滤列车类型
                current_train_type = '-' + train_number[0].lower()
                if not need_filter or self.args[current_train_type] is True:
                    # 跳过【停运列车】的数据查询
                    if train_info[1] != '列车停运':
                        satisfied_train_count += 1
                        # 余票及对应票价
                        tickets_and_prices = self._query_train_tickets_and_prices(train_info, train_date)
                        result_table.add_row([
                            train_number, station, train_time, duration, tickets_and_prices['swz'],
                            tickets_and_prices['ydz'], tickets_and_prices['edz'], tickets_and_prices['rw'],
                            tickets_and_prices['yw'], tickets_and_prices['yz'], tickets_and_prices['wz']
                        ])

            # 打印数据结果
            train_date = colortext.light_yellow(train_date)
            from_city = colortext.light_green(self.db.select_station_name_cn(from_city))
            dest_city = colortext.light_red(self.db.select_station_name_cn(dest_city))
            train_count = colortext.light_blue(satisfied_train_count)
            print('\n查询到满足条件的 %s 从 %s 到 %s 的列车一共 %s 趟（已过滤掉停运列车数据）\n' % (
                train_date, from_city, dest_city, train_count
            ))
            print(result_table)

    def _format_train_info_fields(self, train_info):
        # 车次
        train_number = train_info[3]
        # 出发车站
        from_station_cn = colortext.light_green(self.db.select_station_name_cn(train_info[6]))
        # 到达车站
        dest_station_cn = colortext.light_red(self.db.select_station_name_cn(train_info[7]))
        station = from_station_cn + '\n' + dest_station_cn
        # 发车时间
        from_time = colortext.light_green(train_info[8])
        # 到达时间
        dest_time = colortext.light_red(train_info[9])
        train_time = from_time + '\n' + dest_time
        # 历时多久
        duration = train_info[10]

        return train_number, station, train_time, duration

    def _query_train_tickets_and_prices(self, train_info, train_date):
        """整理余票数据，查询各个坐席的票价"""

        # 查询票价需要用到的请求参数
        train_uuid = train_info[2]
        from_station_no = train_info[16]
        to_station_no = train_info[17]
        seat_types = train_info[35]

        api = 'https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice'
        request_params = {
            'train_no': train_uuid,
            'from_station_no': from_station_no,
            'to_station_no': to_station_no,
            'seat_types': seat_types,
            'train_date': train_date
        }
        response = requests.get(api, params=request_params, cookies=self.cookies)

        tickets_and_prices = {
            'swz': train_info[32] or self.unsupported_seat,  # 商务座/特等座余票
            'ydz': train_info[31] or self.unsupported_seat,  # 一等座余票
            'edz': train_info[30] or self.unsupported_seat,  # 二等座余票
            'rw': train_info[23] or self.unsupported_seat,  # 软卧余票
            'yw': train_info[28] or self.unsupported_seat,  # 硬卧余票
            'yz': train_info[29] or self.unsupported_seat,  # 硬座余票
            'wz': train_info[26] or self.unsupported_seat  # 站票余票
        }

        if response.ok:
            '''
            通过测试和观察，查询车票请求频率过快时会大概率导致请求失败，从而得到错误页面而不是正确的响应
            所以此处暂时以简单的方式做一个请求频率限制，每请求成功一次睡眠几秒钟，以此来规避请求异常问题
            '''
            print('编号为 %s 的列车票价请求成功，%d 秒后执行下一次车票查询请求' % (train_uuid, self.request_interval_seconds))
            time.sleep(self.request_interval_seconds)
            try:
                response_json = response.json()
                price_info = response_json.get('data')
            except JSONDecodeError:
                print(colortext.light_red('[ERROR] JSON解析异常，可能是请求参数异常\n%s' % request_params))
                sys.exit()
            tickets_and_prices['swz'] += '\n' + colortext.light_yellow(price_info.get('A9', ''))
            tickets_and_prices['ydz'] += '\n' + colortext.light_yellow(price_info.get('M', ''))
            tickets_and_prices['edz'] += '\n' + colortext.light_yellow(price_info.get('O', ''))
            tickets_and_prices['rw'] += '\n' + colortext.light_yellow(price_info.get('A4', ''))
            tickets_and_prices['yw'] += '\n' + colortext.light_yellow(price_info.get('A3', ''))
            tickets_and_prices['yz'] += '\n' + colortext.light_yellow(price_info.get('A1', ''))
            # 站票票价先匹配普通列车，等于硬座票价，如果匹配不到，那么就等于二等座的票价
            tickets_and_prices['wz'] += '\n' + colortext.light_yellow(price_info.get('A1', price_info.get('WZ', '')))

        return tickets_and_prices

    def _check_input_args(self):
        """检查输入参数"""
        from_city = self.args['<from_city>']
        dest_city = self.args['<dest_city>']

        # 检查输入的城市名是否正确
        from_station_en = self.db.select_station_name_en(from_city)
        if from_station_en is None:
            print(colortext.light_red('\n参数错误：出发城市 [%s] 不是一个正确的城市名' % from_city))
            sys.exit(1)

        dest_station_en = self.db.select_station_name_en(dest_city)
        if dest_station_en is None:
            print(colortext.light_red('\n参数错误：到达城市 [%s] 不是一个正确的城市名' % dest_city))
            sys.exit(1)

        # 检查输入的乘车日期是否正确
        train_date = self.args['<date>']
        today_date_str = str(date.today())
        train_date = train_date or today_date_str
        is_date = re.match(r'^(2\d{3}-\d{2}-\d{2})$', train_date)
        train_date_ymd = train_date.split('-')
        if not is_date or int(train_date_ymd[1]) > 12 or int(train_date_ymd[2]) > 31 or train_date < today_date_str:
            print(colortext.light_yellow('\n参数错误：乘车日期 [%s] 不正确，将自动查询今天的车次信息' % train_date))
            train_date = today_date_str

        # 判断是否需要执行列车类型过滤操作
        need_filter = 0
        for train_type in ['-g', '-c', '-d', '-k', '-t', '-z', '-l']:
            if self.args[train_type] is True:
                need_filter += 1

        return from_city, dest_city, from_station_en, dest_station_en, train_date, need_filter

if __name__ == '__main__':
    app = TrainTicketsFinder()
    app.query_satisfied_trains_info()


import cv2
import time
from pyzbar.pyzbar import decode
import json
import sqlite3
import queue
import threading

class Database(object):
    def __init__(self):
        self.queue = queue.Queue(maxsize=51200)

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

    def insert_queue(self, pc_time_ns, course_id="", sgin_id="", fashion="", pc_time_ms="", full_msg="",
                     time_ms_dev="", qr_local_time="", pc_local_time="", msg=""):
        self.queue.put((pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev,
                        qr_local_time, pc_local_time, msg), block=True, timeout=None)

    def insert_db(self):
        self.conn = sqlite3.connect("data.db")
        cursor = self.conn.cursor()
        while True:
            (pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev,
                    qr_local_time, pc_local_time, msg) = self.queue.get(block=True, timeout=None)
            sql = """INSERT INTO SyncData(pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev, qr_local_time, pc_local_time, msg) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"""\
                  % (pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev, qr_local_time, pc_local_time, msg)
            cursor.execute(sql)
            self.conn.commit()

class Camera(object):
    def __init__(self, camera_idx=0):
        self.cap = cv2.VideoCapture(camera_idx)

    def __del__(self):
        self.cap.release()

    def get_frame(self, camera_idx=0):
        ret, frame = self.cap.read()
        if ret is None:
            self.cap.release()
            timens = str(int(time.time_ns()))
            return -1, timens # 相机没有找到
        else:
            timens = str(int(time.time_ns()))
            cv2.imwrite("data/" + timens + ".png", frame)
            return frame, timens

def re_qrcode(frame, timens):
    dec = decode(frame)

    try:
        dec = dec[0]
    except IndexError:
        return -1, None, None, None, None, None, None, None, None

    try:
        data = json.loads(str(dec.data.decode('utf-8')))
    except json.JSONDecodeError:
        return -2, None, None, None, None, None, None, None, None

    try:
        course_id = str(data['course_id'])
        sgin_id = str(data['sgin_id'])
        fashion = str(data['fashion'])
    except Exception:
        return -3, None, None, None, None, None, None, None, None

    pc_time_ns = str(timens)
    pc_time_ms = str(int(int(timens)/1000000))
    full_msg = str(dec.data.decode('utf-8'))
    time_ms_dev = int(pc_time_ms) - int(sgin_id)
    qr_local_time = time.asctime(time.localtime(int(sgin_id)/1000.0))

    test = int(pc_time_ms)/1000.0
    pc_local_time = time.asctime(time.localtime(int(test)))

    return pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev, \
           qr_local_time, pc_local_time

def frame_process(frame, timens, db: Database, status: int):
    if status == -1:
        msg = "未找到相机"
        pc_time_ns = str(timens)
        db.insert_queue(pc_time_ns, msg=msg)
        print("微秒计：" + pc_time_ns + "-消息：" + msg)
        return None

    pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev, \
        qr_local_time, pc_local_time = re_qrcode(frame, timens)

    if pc_time_ns == -1:
        msg = "图像错误"
        pc_time_ns = str(timens)
        db.insert_queue(pc_time_ns, msg=msg)
        print("微秒计：" + pc_time_ns + "-消息：" + msg)
        return None
    elif pc_time_ns == -2:
        pc_time_ns = str(timens)
        msg = "二维码无法识别或者识别内容有误"
        db.insert_queue(pc_time_ns, msg=msg)
        print("微秒计：" + pc_time_ns + "-消息：" + msg)
        return None
    elif pc_time_ns == -3:
        msg = "数据错误"
        pc_time_ns = str(timens)
        db.insert_queue(pc_time_ns, msg=msg)
        print("微秒计：" + pc_time_ns + "-消息：" + msg)
        return None
    else:
        msg = ""
    db.insert_queue(pc_time_ns, course_id, sgin_id, fashion, pc_time_ms, full_msg, time_ms_dev,
                    qr_local_time, pc_local_time, msg)
    print("微秒计：" + str(pc_time_ns) + "-" + "二维码时间戳:" + str(sgin_id) + ";时间偏移量:" + str(time_ms_dev))

def io():
    cam = Camera()
    db = Database()

    q = threading.Thread(target=db.insert_db, args=())
    q.daemon = True
    q.start()

    while True:
        frame, timens = cam.get_frame()
        try:
            if frame == -1:
                frame = None
                status = -1
            else:
                status = 1
        except ValueError:
            status = 1

        t = threading.Thread(target=frame_process, args=(frame, timens, db, status))
        t.daemon = True
        t.start()

        time.sleep(0.3)

if __name__ == '__main__':
    io()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.core.files.storage

class Migration(migrations.Migration):

    dependencies = [
        ('news', '0009_auto_20151118_0748'),
    ]

    operations = [
        migrations.AlterField(
            model_name='news',
            name='img',
            field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(location=b'/media'), max_length=250, upload_to=b'', blank=True),
        ),
    ]


import torch
import random

from trainer import Trainer
from datasets import CtDataset
from trainer_utils import Result
from parser_options import parser
from unet_gen import UNetGenerator
from board_visualizer import Visualizer
from patch_disc import NLayerDiscriminator
from utils import get_group_dicts, ImagePool, init_weights

class SimpleGAN(Trainer):
    def __init__(self, parsed_args, parsed_groups):
        super().__init__(**parsed_groups['trainer arguments'])

        self.gen = UNetGenerator(**parsed_groups['generator arguments'])
        self.disc = NLayerDiscriminator(**parsed_groups['discriminator arguments'])

        init_weights(self.gen)
        init_weights(self.disc)

        self.real_label = torch.tensor(1.0)
        self.fake_label = torch.tensor(0.0)

        self.crit = torch.nn.MSELoss() # LSGAN
        self.image_pool = ImagePool(parsed_args.pool_size, parsed_args.replay_prob)

        self.sel_ind = 0
        self.un_normalize = lambda x: (1 + x.clamp(min=-1, max=1))/2.

        self.parsed_args = parsed_args
        self.n_vis = parsed_args.n_vis

        self.vis = Visualizer('board_exp/')
        self.writer = None

    def configure_optimizers(self):
        opt1 = torch.optim.Adam(self.disc.parameters(), lr=self.parsed_args.lr)
        opt2 = torch.optim.Adam(self.gen.parameters(), lr=self.parsed_args.lr)

        N = self.parsed_args.n_epochs
        N_start = int(N * self.parsed_args.frac_decay_start)
        sched_lamb = lambda x: 1.0 - max(0, x - N_start) / (N - N_start)

        sched1 = torch.optim.lr_scheduler.LambdaLR(opt1, lr_lambda=sched_lamb)
        sched2 = torch.optim.lr_scheduler.LambdaLR(opt2, lr_lambda=sched_lamb)

        return opt1, opt2, sched1, sched2

    def on_fit_start(self):
        self.vis.start()
        self.writer = self.vis.get_writer()

    def on_fit_end(self):
        self.vis.stop()

    def _shared_step(self, batch, save_img, is_train):
        res = Result()

        X, Y_real = batch
        Y_fake = self.gen(X)

        if is_train:
            Y_pool = self.image_pool.query(Y_fake.detach())
        else:
            Y_pool = Y_fake
            res.recon_error = self.crit(Y_real, Y_fake)

        real_predict = self.disc(Y_real)
        fake_predict = self.disc(Y_pool)

        real_label = self.real_label.expand_as(real_predict)
        fake_label = self.fake_label.expand_as(fake_predict)

        disc_loss = 0.5 * (self.crit(real_predict, real_label) + \
                           self.crit(fake_predict, fake_label))

        if is_train:
            res.step(disc_loss)
        res.disc_loss = disc_loss

        gen_predict = self.disc(Y_fake)
        gen_loss = self.crit(gen_predict, real_label)

        if is_train:
            res.step(gen_loss)
        res.gen_loss = gen_loss

        if save_img:
            res.img = [self.un_normalize(X[:self.n_vis]),
                       self.un_normalize(Y_fake[:self.n_vis]),
                       self.un_normalize(Y_real[:self.n_vis])]
        return res

    def training_step(self, batch, batch_idx):
        res = self._shared_step(batch,
                                save_img=(batch_idx == 0),
                                is_train=True)
        return res

    def validation_step(self, batch, batch_idx):
        res = self._shared_step(batch,
                                save_img=(batch_idx == self.sel_ind),
                                is_train=False)
        return res

    def _shared_end(self, result_outputs, is_train):
        phase = 'Train' if is_train else 'Valid'

        self.writer.add_scalars('Gen. Loss', {phase + ' Loss':
                                torch.mean(torch.stack(result_outputs.gen_loss))},
                                self.current_epoch)
        self.writer.add_scalars('Disc. Loss', {phase + ' Loss':
                                torch.mean(torch.stack(result_outputs.disc_loss))},
                                self.current_epoch)

        collated_imgs = torch.cat([*torch.cat(result_outputs.img[0], dim=3)], dim=1)
        self.writer.add_image(phase + ' Images', collated_imgs, self.current_epoch)

    def training_epoch_end(self, training_outputs):
        self._shared_end(training_outputs, is_train=True)

    def validation_epoch_end(self, validation_outputs):
        self._shared_end(validation_outputs, is_train=False)
        self.sel_ind = random.randint(0, len(self.validation_loader) - 1)

        return torch.mean(torch.stack(validation_outputs.recon_error))

parser = Trainer.add_trainer_args(parser)
args = parser.parse_args()
group_dicts = get_group_dicts(args, parser)

train_dataset = CtDataset('train', **group_dicts['data arguments'])
valid_dataset = CtDataset('val', **group_dicts['data arguments'])

GAN_experiment = SimpleGAN(args, group_dicts)
GAN_experiment.fit(train_dataset, valid_dataset)


import arcpy
import os

outdir = r'E:\Projects\OceanModels\BC_ROMS\Data\Bottom_Shapefiles'

# set workspace
arcpy.env.workspace = outdir

# list datasets
l = arcpy.ListFeatureClasses()

# empty list
fieldnames = []

# populate list with field names
for i in arcpy.ListFields(l[0]):
    fieldnames.append(i.name)

# drop extra fields
fieldnames = fieldnames[3:]

# make copy of feature classes with value name appended (i.e. circulation_min)
for i in l:
    for j in fieldnames:
        name = i[:-4] + '_' + j
        arcpy.FeatureClassToFeatureClass_conversion(in_features=i, out_path=outdir, out_name=name)

# now delete fields except for value field
arcpy.env.workspace = r'C:\TEMP'
os.chdir(r'C:\TEMP')
# list feature classes
fcs = arcpy.ListFeatureClasses(r'C:\Users\fieldsc\PycharmProjects\untitled')

fcs = arcpy.ListFeatureClasses()

# for each item, get the value name (min, max, etc)
for fc in fcs:
    # get name of value for layer (min, max, etc)
    value = str(fc).split('_')[1][:-4]
    # empty list for field names
    fields = []
    for field in arcpy.ListFields(fc):
        fields.append(field.name)
    print('There are {} fields in {}...\n'
          'Field names are: {}'.format(len(fields), fc, fields))
    keeps = ["FID", "Shape", value]
    print('Keeping the following fields: {}'.format(keeps))
    # keep not-deletable fields and the value fields in list
    drops = [x for x in fields if x not in keeps]
    print('Deleting the following fields: {}'.format(drops))
    if len(drops) > 0:
        # delete fields in drops list from fc
        arcpy.DeleteField_management(fc, drops)