#
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: EPL-2.0
#

from __future__ import print_function
from conditional import conditional

import pytest
import uuid
from mlt.commands.events import EventsCommand
from test_utils.io import catch_stdout

@pytest.fixture
def open_mock(patch):
    return patch('open')

@pytest.fixture
def get_only_one_job(patch):
    def mocker(job_desired, error_msg):
        return job_desired
    return patch('files.get_only_one_job', mocker)

@pytest.fixture
def process_helpers(patch):
    return patch('process_helpers.run_popen')

@pytest.fixture
def verify_init(patch):
    return patch('config_helpers.load_config')

def get_events(catch_exception=None, job_name=None):
    events_command = EventsCommand({'events': True, '--job-name': job_name})
    events_command.config = {'name': 'app', 'namespace': 'namespace'}

    with catch_stdout() as caught_output:
        with conditional(catch_exception, pytest.raises(catch_exception)):
            events_command.action()
        output = caught_output.getvalue().strip()
    return output

# END SIMILAR STUFF TO TEST_LOGS

def test_events_get_events(open_mock, verify_init, process_helpers,
                           get_only_one_job):
    head_value = bytearray("LAST SEEN   FIRST SEEN   COUNT", 'utf-8')
    # technically event_value is different from app_run_id
    # but didn't want to set a global app_run_id
    # if others disagree, I'm not super opposed to it
    job = 'app-{}'.format(uuid.uuid4())
    event_value = bytearray(job, 'utf-8')
    process_helpers.return_value.stdout.readline.side_effect = \
        [head_value, event_value, bytearray('', 'utf-8')]
    process_helpers.return_value.poll.return_value = 1
    process_helpers.return_value.stderr.readline.return_value = ''

    events = get_events(job_name=job)
    assert head_value.decode('utf-8') in events
    assert event_value.decode('utf-8') in events

def test_events_no_push_json_file(open_mock, verify_init, process_helpers):
    error_msg = "Please use --job-name flag to query for job events."
    assert error_msg in get_events(catch_exception=SystemExit)

def test_events_no_resources_found(open_mock, verify_init,
                                   get_only_one_job, process_helpers):
    process_helpers.side_effect = Exception("No resources found")

    assert "No resources found" in get_events(catch_exception=SystemExit)

def test_events_no_events_to_display(open_mock, verify_init,
                                     get_only_one_job, process_helpers):
    head_value = "LAST SEEN   FIRST SEEN   COUNT"
    process_helpers.return_value.stdout.readline.side_effect = \
        [head_value, bytearray("current job events missing", 'utf-8'),
         bytearray('', 'utf-8')]
    process_helpers.return_value.poll.return_value = 1
    process_helpers.return_value.stderr.readline.return_value = ''

    assert "No events to display for this job" in get_events()


"""CPU functionality."""

import sys

OP1 = 0b10000010 # LDI
OP2 = 0b01000111 # PRN
OP3 = 0b10100010 # MUL
OP4 = 0b01000101 # PUSH
OP5 = 0b01000110 # POP
OP6 = 0b00000001 # HALT
OP7 = 0b01010000 # CALL
OP8 = 0b00010001 # RET
OP9 = 0b10100000 # ADD

class CPU:
    """Main CPU class."""

    def __init__(self, reg = [0] * 8, ram = [0] * 256, pc = 0):
        """Construct a new CPU."""
        self.reg = reg
        self.ram = ram
        self.pc = pc
        self.sp = 244
        self.running = True

    def load(self):
        """Load a program into memory."""

        program = []
        address = 0

        with open(sys.argv[1]) as f:
            for line in f:
                l = line.split('#')
                operation = l[0].strip()
                try:
                    program.append(int(operation, 2))
                except ValueError:
                    pass

        for operation in program:
            self.ram[address] = operation
            address += 1

    def alu(self, op, reg_a, reg_b):
        """ALU operations."""

        if op == "ADD":
            self.reg[reg_a] += self.reg[reg_b]
        #elif op == "SUB": etc
        else:
            raise Exception("Unsupported ALU operation")

    def trace(self):
        """
        Handy function to print out the CPU state. You might want to call this
        from run() if you need help debugging.
        """

        print(f"TRACE: %02X | %02X %02X %02X |" % (
            self.pc,
            #self.fl,
            #self.ie,
            self.ram_read(self.pc),
            self.ram_read(self.pc + 1),
            self.ram_read(self.pc + 2)
        ), end='')

        for i in range(8):
            print(" %02X" % self.reg[i], end='')

        print()

    def ram_read(self, register):
        return self.ram[register]

    def ram_write(self, register, address):
        self.ram[address] = register

    def hlt(self):
        self.running = False

    def ldi(self):
        address = self.ram[self.pc + 1]
        value = self.ram[self.pc + 2]
        self.reg[address] = value

    def prn(self):
        address = self.ram[self.pc + 1]
        print(self.reg[address])

    def mul(self):
        value_1 = self.ram[self.pc + 1]
        value_2 = self.ram[self.pc + 2]
        self.alu("MUL", value_1, value_2)

    def run(self):
        """Run the CPU."""
        while self.running:
            ir = self.pc
            operation = self.ram[ir]
            # Perform operation here


import tensorflow as tf
import numpy as np
import skimage.io as io
import os, sys
from matplotlib import pyplot as plt

# Use second GPU -- change if you want to use a first one
os.environ["CUDA_VISIBLE_DEVICES"] = '0'

# Add a path to a custom fork of TF-Slim
# Get it from here:
# https://github.com/warmspringwinds/models/tree/fully_conv_vgg
sys.path.append("models/slim/")

# Add path to the cloned library
sys.path.append("tf-image-segmentation/")

checkpoints_dir = 'checkpoints'
log_folder = 'log_folder'

slim = tf.contrib.slim

from tf_image_segmentation.utils.tf_records import read_tfrecord_and_decode_into_image_annotation_pair_tensors
from tf_image_segmentation.models.fcn_8s import FCN_8s

from tf_image_segmentation.utils.pascal_voc import pascal_segmentation_lut

from tf_image_segmentation.utils.training import get_valid_logits_and_labels

from tf_image_segmentation.utils.augmentation import (distort_randomly_image_color,
                                                      flip_randomly_left_right_image_with_annotation,
                                                      scale_randomly_image_with_annotation_with_fixed_size_output)
#epochs=3 #3 10 outofRange fail
'''
epochs=70
'''

'''
vesselBatch_size=1
'''

'''
numOfTrainingImage=1545
'''

'''
epochs=1
vesselBatch_size=5
numOfTrainingImage=int(1545/vesselBatch_size) # it works
gpu_memory_fraction=0.7 #0.7
capacity=2000# 3000 OutOFRangeError
image_train_size = [384, 384]
number_of_classes = 21
'''
#trying to train pascal dataset
'''
tfrecord_filename = 'pascal_augmented_train.tfrecords'
'''
epochs=300
vesselBatch_size=32 #32 #batch size has to be smaller when image size gets larger
                    #or GPU OOM will be raised.
'''
vesselBatch_size=1
'''
#numOfTrainingImage=1545
numOfTrainingImage=4105 #with Random Translation #4173 #4105
numOfTrainingIteration=int(numOfTrainingImage/vesselBatch_size)
gpu_memory_fraction=0.7 #restrict the program from using GPU memory up to 70%.
image_train_size = [224, 224 ] #[384, 384]
#image_train_size = [384, 384 ] #[384, 384]
number_of_classes = 2 #because Pascal dataset has 21 classes

base_lr=0.000001 #default lr

#trying to train 3DBuilderVesselSemanticSeg dataset
#tfrecord_filename = '3DBuilderVessel_augmented_train_withoutNoise.tfrecords'
tfrecord_filename ='3DBuilderVessel_augmented_train_with_Rand_Translation.tfrecords'

pascal_voc_lut = pascal_segmentation_lut()
class_labels = list(pascal_voc_lut.keys())

#based on pascal trained FCN16
#fcn_16s_checkpoint_path = './PascalModelForFcn/Pascal_fcn_16s_checkpoint/model_fcn16s_final.ckpt'

fcn_16s_checkpoint_path = './3DBuilderVesselModelForFCN/FCN16_Model/300Epochs/model_fcn16s_3DVessel_300Epochs_with_Rand_TranslationData.ckpt'

filename_queue = tf.train.string_input_producer(
    [tfrecord_filename], num_epochs=epochs)

image, annotation = read_tfrecord_and_decode_into_image_annotation_pair_tensors(filename_queue)

# Various data augmentation stages
image, annotation = flip_randomly_left_right_image_with_annotation(image, annotation)

# image = distort_randomly_image_color(image)

resized_image, resized_annotation = scale_randomly_image_with_annotation_with_fixed_size_output(image, annotation, image_train_size)

resized_annotation = tf.squeeze(resized_annotation)

image_batch, annotation_batch = tf.train.shuffle_batch( [resized_image, resized_annotation],
                                             batch_size=vesselBatch_size,
                                             capacity=3000,
                                             num_threads=2,
                                             min_after_dequeue=1000)

upsampled_logits_batch, fcn_16s_variables_mapping = FCN_8s(image_batch_tensor=image_batch,
                                                           number_of_classes=number_of_classes,
                                                           is_training=True)

valid_labels_batch_tensor, valid_logits_batch_tensor = get_valid_logits_and_labels(annotation_batch_tensor=annotation_batch,
                                                                                logits_batch_tensor=upsampled_logits_batch,
                                                                                class_labels=class_labels)

cross_entropies = tf.nn.softmax_cross_entropy_with_logits(logits=valid_logits_batch_tensor,
                                                          labels=valid_labels_batch_tensor)

#cross_entropy_sum = tf.reduce_sum(cross_entropies)

cross_entropy_sum = tf.reduce_mean(cross_entropies)

pred = tf.argmax(upsampled_logits_batch, dimension=3)

probabilities = tf.nn.softmax(upsampled_logits_batch)

with tf.variable_scope("adam_vars"):
    train_step = tf.train.AdamOptimizer(learning_rate=base_lr).minimize(cross_entropy_sum)

'''
with tf.variable_scope("RMSProp_vars"):
    train_step = tf.train.RMSPropOptimizer(learning_rate=0.001,momentum=0.9).minimize(cross_entropy_sum)
'''

#adam_optimizer_variables = slim.get_variables_to_restore(include=['adam_vars'])

# Variable's initialization functions
init_fn = slim.assign_from_checkpoint_fn(model_path=fcn_16s_checkpoint_path,
                                         var_list=fcn_16s_variables_mapping)

global_vars_init_op = tf.global_variables_initializer()

tf.summary.scalar('cross_entropy_loss', cross_entropy_sum)

merged_summary_op = tf.summary.merge_all()

summary_string_writer = tf.summary.FileWriter(log_folder)

# Create the log folder if doesn't exist yet
if not os.path.exists(log_folder):
     os.makedirs(log_folder)

#optimization_variables_initializer = tf.variables_initializer(adam_optimizer_variables)

#The op for initializing the variables.
local_vars_init_op = tf.local_variables_initializer()

combined_op = tf.group(local_vars_init_op, global_vars_init_op)

# We need this to save only model variables and omit
# optimization-related and other variables.
model_variables = slim.get_model_variables()
saver = tf.train.Saver(model_variables)

#to limit the usage of GPU memory to avoid error( InternalError: Dst tensor is not initialized.)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = gpu_memory_fraction

#create cross entropy accumulation list for plot after training
crossEntropyAccumList=[]

with tf.Session(config=config)  as sess:

    sess.run(combined_op)
    init_fn(sess)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    #save model for each n epochs
    saveModerForEachN=50

    # Let's read off 3 batches just for example
    for i in range(numOfTrainingIteration * epochs):

        cross_entropy, summary_string, _ = sess.run([ cross_entropy_sum,
                                                      merged_summary_op,
                                                      train_step ])

        summary_string_writer.add_summary(summary_string, numOfTrainingIteration * epochs * 2 + i)

        print("step :" + str(i) + " Loss: " + str(cross_entropy))

        #record cross entropy loss  when finishing each epoch
        if i % numOfTrainingIteration == 0:
            #append cross entropy of each epoch to the accumulation list for plot after training
            crossEntropyAccumList.append(cross_entropy)

        #save model when finishing each 10 epochs
        if i % (numOfTrainingIteration*saveModerForEachN) == 0:
            save_path = saver.save(sess, "./3DBuilderVesselModelForFCN/FCN8_Model/300Epochs/model_fcn8s_3DVessel_300Epochs_with_Rand_TranslationData_epochNo"+str(i/(numOfTrainingIteration*saveModerForEachN)+1)+".ckpt")
            print("Model saved in file: %s" % save_path)

    coord.request_stop()
    coord.join(threads)

    save_path = saver.save(sess,"./3DBuilderVesselModelForFCN/FCN8_Model/300Epochs/model_fcn8s_3DVessel_300Epochs_with_Rand_TranslationData.ckpt")
    print("Model saved in file: %s" % save_path)

summary_string_writer.close()


import argparse
import datetime
import dso
import os
import sys
import time
import torch
import torch.distributed as dist
from tqdm import tqdm

from dso.optimizers import NGDOptimizer

from torchinfo import summary
from torch.utils.tensorboard import SummaryWriter

from dso.utils.comm import get_comm_backend
from dso.utils.datasets import get_dataset
from dso.utils.metric import accuracy, Metric, LabelSmoothLoss
from dso.utils.models import get_model
from dso.utils.optimizers import get_optimizer
from dso.utils.scheduler import get_lr_schedule

# settings
parser = argparse.ArgumentParser(description='Distributed Second-order Optimizer')
parser.add_argument('--data-dir', type=str, default='./datasets')
parser.add_argument('--log-dir', type=str, default='./logs')
parser.add_argument('--model', type=str, default='resnet32')
parser.add_argument('--dataset', type=str, default='cifar10')
parser.add_argument('--batch-size', type=int, default=1024)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--lr', type=float, default=0.3)
parser.add_argument('--lr-decay', type=float, default=0.35)
parser.add_argument('--milestone', nargs='+', type=int, default=[30,50,70,90])
parser.add_argument('--warmup-epochs', type=int, default=5)
parser.add_argument('--warmup-init-lr', type=int, default=0.1)
parser.add_argument('--damping', type=float, default=1)
parser.add_argument('--target-damping', type=float, default=1)
parser.add_argument('--momentum', type=float, default=0.90)
parser.add_argument('--weight-decay', type=float, default=0.0004)
parser.add_argument('--freq', type=int, default=10)
parser.add_argument('--kl-clip', type=int, default=0.01)
parser.add_argument('--compression-ratio', type=float, default=0.1)
parser.add_argument('--label-smoothing', type=float, default=0.1)
parser.add_argument('--checkpoint-freq', type=int, default=5)
parser.add_argument('--backend', type=str, default='nccl')
parser.add_argument('--profiling', action='store_true')
parser.add_argument('--grad-norm', action='store_true')
parser.add_argument('--rank-analysis', action='store_true')
parser.add_argument('--url', type=str, default='env://')
parser.add_argument('--node-idx', type=int, default=0)
parser.add_argument('--nproc-per-node', type=int, default=4)
parser.add_argument('--world-size', type=int, default=64)
parser.add_argument('--local_rank', type=int, default=0)
parser.add_argument('--adaptive', nargs='+', type=int, default=None)

args = parser.parse_args()

def train(epoch, model, optimizer, loss_fn, batch_size, train_sampler, train_loader, backend, verbose):
    model.train()
    train_sampler.set_epoch(epoch)
    train_loss = Metric('train_loss')
    train_acc = Metric('train_acc')

    with tqdm(total=len(train_loader),
        bar_format='{l_bar}{bar:10}{r_bar}',
        desc='Epoch {:3d}/{:3d}'.format(epoch, args.epochs),
        disable=not verbose) as t:
        for batch_idx, (data, target) in enumerate(train_loader):
            data, target = data.cuda(), target.cuda()
            optimizer.zero_grad()

            outputs = model(data)
            loss = loss_fn(outputs, target)

            with torch.no_grad():
                train_loss.update(backend, loss)
                train_acc.update(backend, accuracy(outputs, target))

            if optimizer.steps % optimizer.freq == 0:
                optimizer.acc_stats = True
            loss.backward()
            optimizer.acc_stats = False

            optimizer.step()

            t.set_postfix_str("loss: {:.4f}, acc: {:.2f}%, lr: {:.4f} damp: {:.4f}".format(
                train_loss.avg, 100*train_acc.avg,
                optimizer.param_groups[0]['lr'],
                optimizer.damping))
            t.update(1)

    return train_loss, train_acc

def test(epoch, model, loss_fn, test_loader, backend, verbose):
    model.eval()
    test_loss = Metric('test_loss')
    test_acc = Metric('test_acc')

    with tqdm(total=len(test_loader),
        bar_format='{l_bar}{bar:10}|{postfix}',
        desc='             '.format(epoch, args.epochs),
        disable=not verbose) as t:
        with torch.no_grad():
            for idx, (data, target) in enumerate(test_loader):
                data, target = data.cuda(), target.cuda()
                output = model(data)
                test_loss.update(backend, loss_fn(output, target))
                test_acc.update(backend, accuracy(output, target))

                t.update(1)
                if idx + 1 == len(test_loader):
                    t.set_postfix_str("\b\b test loss: {:.4f}, test acc: {:.2f}%".format(
                        test_loss.avg, 100*test_acc.avg), refresh=False)

    return test_loss, test_acc

def log(epoch, loss, acc, log_writter, optimizer=None, train=False):
    if train:
        log_writter.add_scalar('train/loss', loss.avg, epoch)
        log_writter.add_scalar('train/accuracy', acc.avg, epoch)
        log_writter.add_scalar('train/lr', optimizer.param_groups[0]['lr'], epoch)
    else:
        log_writter.add_scalar('test/loss', loss.avg, epoch)
        log_writter.add_scalar('test/accuracy', acc.avg, epoch)

def save_checkpoint(model, optimizer, scheduler, filepath):
    state = {
        'model': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'scheduler': scheduler.state_dict()
    }

    torch.save(state, filepath)

def main():
    if not torch.cuda.is_available():
        print('Error: CUDA is not available.')
        raise RuntimeError

    device = 'cuda'
    args.local_rank = int(os.environ['LOCAL_RANK'])
    print('Start init process group...')
    torch.distributed.init_process_group(
        backend=args.backend,
        init_method=args.url,
        world_size=int(os.environ['WORLD_SIZE']),
        rank=int(os.environ['RANK']))
    print('done')
    backend = get_comm_backend()

    torch.cuda.set_device(args.local_rank)
    torch.cuda.manual_seed(0)

    print('rank = {}, world size = {}, device ids = {}'.format(
        backend.rank(), backend.size(), args.local_rank))

    train_sampler, train_loader, test_sampler, test_loader = get_dataset(
        dataset=args.dataset, data_dir=args.data_dir, batch_size=args.batch_size,
        world_size=backend.size(), rank=backend.rank(), local_rank=args.local_rank)

    model = get_model(name=args.model)
    model.to(device)

    verbose = True if dist.get_rank() == 0 else False
    if verbose:
        if args.dataset == 'cifar10':
            summary(model, (args.batch_size, 3, 32, 32), device=device)
        elif args.dataset == 'imagenet':
            summary(model, (args.batch_size, 3, 224, 224), device=device)
        else:
            raise NotImplementedError

    model = torch.nn.parallel.DistributedDataParallel(
        model, device_ids=[args.local_rank])

    os.makedirs(args.log_dir, exist_ok=True)
    log_writter = SummaryWriter(args.log_dir) if verbose else None

    args.checkpoint_format = 'checkpoint_{epoch}.pth.tar'
    args.checkpoint_format = os.path.join(args.log_dir, args.checkpoint_format)
    args.resume_from_epoch = 0
    for try_epoch in range(args.epochs, 0, -1):
        if os.path.exists(args.checkpoint_format.format(epoch=try_epoch)):
            args.resume_from_epoch = try_epoch
            break

    optimizer = get_optimizer(
        model, args.lr, args.damping, args.target_damping, args.weight_decay, args.freq, args.momentum,
        args.kl_clip, args.batch_size, len(train_loader), args.warmup_epochs, backend, args.compression_ratio,
        args.profiling, args.grad_norm, args.rank_analysis, args.adaptive)

    lr_schedule = get_lr_schedule(
        backend.size(), args.warmup_epochs, args.milestone, args.lr_decay)
    lr_scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_schedule)

    if args.dataset == 'cifar10':
        loss_fn = torch.nn.CrossEntropyLoss()
    elif args.dataset == 'imagenet':
        loss_fn = LabelSmoothLoss(args.label_smoothing)
    else:
        raise NotImplementedError

    if args.resume_from_epoch > 0:
        filepath = args.checkpoint_format.format(epoch=args.resume_from_epoch)
        map_location = {'cuda:0': 'cuda:{}'.format(args.local_rank)}
        checkpoint = torch.load(filepath, map_location=map_location)
        model.module.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        if isinstance(checkpoint['scheduler'], list):
            for sched, state in zip(lr_schedule, checkpoint['scheduler']):
                sched.load_state_dict(state)

    acc = []
    timing = []

    start_training = time.time()

    for epoch in range(args.resume_from_epoch + 1, args.epochs + 1):
        start = torch.cuda.Event(enable_timing=True)
        end = torch.cuda.Event(enable_timing=True)

        start.record()

        train_loss, train_acc = train(epoch, model, optimizer, loss_fn, args.batch_size,
            train_sampler, train_loader, backend, verbose)

        end.record()
        torch.cuda.synchronize()
        timing.append(start.elapsed_time(end))

        if verbose:
            log(epoch, train_loss, train_acc, log_writter, optimizer, train=True)

        test_loss, test_acc = test(epoch, model, loss_fn, test_loader, backend, verbose)

        acc.append(test_acc.avg)

        if verbose:
            log(epoch, test_loss, test_acc, log_writter)

        lr_scheduler.step()

        if epoch > 0 and epoch % args.checkpoint_freq == 0 and dist.get_rank() == 0:
            save_checkpoint(model.module, optimizer, lr_scheduler, args.checkpoint_format.format(epoch=epoch))

    if verbose:
        print('\nEnd-to-End time: {}'.format(datetime.timedelta(seconds=time.time() - start_training)))

    fname = args.model + '-' + args.dataset + '-lr' + str(args.lr)
    fname += '-d' + str(args.damping) + '-td' + str(args.target_damping)
    fname += '-wd' + str(args.weight_decay) + '-f' + str(args.freq)
    fname += '.csv'
    f = open(fname, 'w')
    f.write('acc, time(s), total_time(s)\n')
    total_time = 0
    for i in range(args.resume_from_epoch, args.epochs):
        total_time += timing[i]
        line = str(acc[i]) + ', ' + str(timing[i]) + ', ' + str(total_time) + '\n'
        f.write(line)
    f.close()

if __name__ == '__main__':
    main()


__all__ = ['show_record']

from ..utils import *
from .show_annotation import *

def show_record(r, label=True, bbox=True, mask=True, catmap=None, ax=None, show=True):
    im = open_img(r.info.filepath)
    h,w,_ = im.shape
    names = [catmap.i2o[i].name for i in r.annot.labels] if notnone(catmap) else r.annot.labels
    return show_annotation(im, ax=ax, show=show,
                           labels=names if label else None,
                           bboxes=r.annot.bboxes if bbox else None,
                           masks=r.annot.get_mask(h,w) if mask else None)


import numpy as np
from sklearn.datasets import fetch_openml
from nn_layers import FullyConnect, Activation, Softmax, BatchNormalization, Conv, TrasposedConv
import matplotlib.pyplot as plt

def noise(n_x, n_d):
    return np.random.randn(n_x, n_d)

def bce_loss(pred, y):
    eps = 1e-20
    return -((1 - y) * np.log(1 - pred + eps) + y * np.log(pred + eps)).mean()

def bce_grad(pred, y):
    eps = 1e-20
    return (- y / (pred + eps) + (1 - y) / (1 - pred + eps)) / pred.shape[0]

class NN(object):

    def __init__(self, layers):
        self.layers = layers

    def predict(self, x):
        out = x
        for layer in self.layers:
            out = layer.predict_forward(out) if isinstance(
                layer, BatchNormalization) else layer.forward(out)
        return out

    def forward(self, x):
        out = x
        for layer in self.layers:
            out = layer.forward(out)
        return out

    def gradient(self, grad_loss):
        grad = grad_loss
        for layer in self.layers[::-1]:
            grad = layer.gradient(grad)
        return grad

    def backward(self):
        for layer in self.layers:
            layer.backward()

class GAN(object):

    def __init__(self):
        self.n_epochs, self.batch_size = 5, 32
        self.gen_input = 100
        self.dc_gan()

    def vanilla_gan(self):
        gen_lr, dis_lr = 2e-3, 5e-4
        self.generator = NN([
            FullyConnect([self.gen_input], [256], lr=gen_lr),
            BatchNormalization([256], lr=gen_lr),
            Activation(act_type='ReLU'),
            FullyConnect([256], [512], lr=gen_lr),
            BatchNormalization([512], lr=gen_lr),
            Activation(act_type='ReLU'),
            FullyConnect([512], [1024], lr=gen_lr),
            BatchNormalization([1024], lr=gen_lr),
            Activation(act_type='ReLU'),
            FullyConnect([1024], [1, 28, 28], lr=gen_lr),
            Activation(act_type='Tanh')
        ])
        self.discriminator = NN([
            FullyConnect([1, 28, 28], [1024], lr=dis_lr),
            Activation(act_type='LeakyReLU'),
            FullyConnect([1024], [512], lr=dis_lr),
            Activation(act_type='LeakyReLU'),
            FullyConnect([512], [256], lr=dis_lr),
            Activation(act_type='LeakyReLU'),
            FullyConnect([256], [1], lr=dis_lr),
            Activation(act_type='Sigmoid')
        ])

    def dc_gan(self):
        gen_lr, dis_lr = 2e-3, 5e-4
        tconv1 = TrasposedConv((128, 7, 7), k_size=4,
                               k_num=128, stride=2, padding=1, lr=gen_lr)
        tconv2 = TrasposedConv(tconv1.out_shape, k_size=4,
                               k_num=128, stride=2, padding=1, lr=gen_lr)
        tconv3 = TrasposedConv(tconv2.out_shape, k_size=7,
                               k_num=1, stride=1, padding=3, lr=gen_lr)
        self.generator = NN([
            FullyConnect([self.gen_input], tconv1.in_shape, lr=gen_lr),
            BatchNormalization(tconv1.in_shape, lr=gen_lr),
            Activation(act_type='ReLU'),
            tconv1,
            BatchNormalization(tconv1.out_shape, lr=gen_lr),
            Activation(act_type='ReLU'),
            tconv2,
            BatchNormalization(tconv2.out_shape, lr=gen_lr),
            Activation(act_type='ReLU'),
            tconv3,
            BatchNormalization(tconv3.out_shape, lr=gen_lr),
            Activation(act_type='Tanh')
        ])
        conv1 = Conv((1, 28, 28), k_size=7, k_num=128,
                     stride=1, padding=3, lr=dis_lr)
        conv2 = Conv(conv1.out_shape, k_size=4, k_num=128,
                     stride=2, padding=1, lr=dis_lr)
        conv3 = Conv(conv2.out_shape, k_size=4, k_num=128,
                     stride=2, padding=1, lr=dis_lr)
        self.discriminator = NN([
            conv1,
            Activation(act_type='LeakyReLU'),
            conv2,
            BatchNormalization(conv2.out_shape, lr=dis_lr),
            Activation(act_type='LeakyReLU'),
            conv3,
            BatchNormalization(conv3.out_shape, lr=dis_lr),
            Activation(act_type='LeakyReLU'),
            FullyConnect(conv3.out_shape, [1], lr=dis_lr),
            Activation(act_type='Sigmoid')
        ])

    def fit(self, x):
        y_true = np.ones((self.batch_size, 1))
        y_false = np.zeros((self.batch_size, 1))
        y_dis = np.concatenate([y_true, y_false], axis=0)
        generated_img = []

        for epoch in range(self.n_epochs):
            permut = np.random.permutation(
                x.shape[0] // self.batch_size * self.batch_size).reshape([-1, self.batch_size])
            for b_idx in range(permut.shape[0]):
                x_true = x[permut[b_idx, :]]
                pred_dis_true = self.discriminator.forward(x_true)
                self.discriminator.gradient(bce_grad(pred_dis_true, y_true))
                self.discriminator.backward()

                x_gen = self.generator.forward(
                    noise(self.batch_size, self.gen_input))
                pred_dis_gen = self.discriminator.forward(x_gen)
                self.discriminator.gradient(bce_grad(pred_dis_gen, y_false))
                self.discriminator.backward()

                pred_gen = self.discriminator.forward(x_gen)
                grad = self.discriminator.gradient(bce_grad(pred_gen, y_true))
                self.generator.gradient(grad)
                self.generator.backward()
                print(
                    f'Epoch {epoch} batch {b_idx} discriminator:',
                    bce_loss(np.concatenate(
                        [pred_dis_true, pred_dis_gen], axis=0), y_dis),
                    'generator:', bce_loss(pred_gen, y_true)
                )
            generated_img.append(
                self.generator.predict(noise(10, self.gen_input)))
        return generated_img

def main():
    x, _ = fetch_openml('mnist_784', return_X_y=True, data_home='data')
    x = 2 * (x / x.max()) - 1
    gan = GAN()
    images = gan.fit(x.reshape((-1, 1, 28, 28)))
    for i, img in enumerate(np.array(images).reshape(-1, 784)):
        plt.subplot(len(images), 10, i + 1)
        plt.imshow(img.reshape(28, 28), cmap='gray', vmin=-1, vmax=1)
    plt.show()

if __name__ == "__main__":
    main()


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

try:
    import setuptools
except ImportError:
    import distutils.core as setuptools

__author__ = 'Peter Bryzgalov'
__copyright__ = 'Copyright 2015'
__credits__ = []

__license__ = 'Apache 2.0'
__version__ = '0.9.001'
__maintainer__ = __author__
__email__ = 'peterbryz@yahoo.com'
__status__ = 'Beta'

__title__ = 'docker-registry-driver-git'
__build__ = 0x000001

__url__ = 'https://github.com/pyotr777/docker-registry-driver-git'
__description__ = 'Docker registry backend git driver'
d = ''

setuptools.setup(
    name=__title__,
    version=__version__,
    author=__author__,
    author_email=__email__,
    maintainer=__maintainer__,
    maintainer_email=__email__,
    url=__url__,
    description=__description__,
    download_url=d,
    long_description=open('./README.md').read(),
    classifiers=['Development Status :: 4 - Beta',
                 'Intended Audience :: Developers',
                 'Programming Language :: Python',
                 'Programming Language :: Python :: 2.7',
                 'Operating System :: OS Independent',
                 'Topic :: Utilities',
                 'License :: OSI Approved :: Apache Software License'],
    platforms=['Independent'],
    license=open('./LICENSE').read(),
    namespace_packages=['docker_registry', 'docker_registry.drivers'],
    packages=['docker_registry', 'docker_registry.drivers'],
    install_requires=open('./requirements.txt').read(),
    zip_safe=True,
    test_suite='nose.collector'
)


import sys
import os
import re
import subprocess

try_encodings = 'utf-8','gb18030','shift_jis','euc_jp'

PY3 = True if sys.version_info > (3,) else False

def file2utf8(f):
    subprocess.call(['textutil','-convert','txt','-encoding','utf-8',f])
    os.rename(os.path.splitext(f)[0]+'.txt',f)

def detect_encoding_by_unicode(unicode_str):
    iso_str = unicode_str.encode('iso-8859-1')
    for enc in try_encodings:
        try:
            iso_str.decode(enc)
            break
        except UnicodeError:
            pass
    else:
        raise ValueError('None of the try_encoding')
    return enc

def detect_encoding_by_bytes(bytes_data):
    for enc in try_encodings:
        try:
            try_str = str(bytes_data,encoding=enc)
            break
        except UnicodeError:
            pass
    else:
        raise ValueError('None of the try_encoding')
    return enc

def add_music(f,music_list):
    if os.path.isdir(f):
        file_list = [os.path.join(f,x) for x in os.listdir(f)]
        dir_list = []
        cue_list = []
        tak_list = []
        for x in file_list:
                if os.path.isdir(x):
                    dir_list.append(x)
                elif os.path.splitext(x)[1] == '.cue':
                    cue_list.append(x)
                elif os.path.splitext(x)[1] == '.tak':
                    tak_list.append(x)

        for x in dir_list:
            add_music(x,music_list)

        # only add cue file when .cue exists
        if not cue_list:
            for x in file_list:
                if re.match(r'.((flac)|(wav)|(ape)|(mp3)|(m4a))',os.path.splitext(x)[1]):
                    add_music(x,music_list)
        else:
            # if exists tak file, convert it to flac and change the cue file
            for tak in tak_list:
                add_music(tak,music_list)
            for cue in cue_list:
                if tak_list:
                    cue_look_for_wav(cue)
                add_music(cue,music_list)

    # convert encode of .mp3 from gbk to utf-8(if needed)
    else:
        if os.path.splitext(f)[1] == '.mp3':
            # convert encoding of id3 to utf-8
            try:
                from mutagen.easyid3 import ID3
                id3 = ID3(f)
                for key,value in id3.items():
                    # str for python3 and unicode for python2
                    if hasattr(value,'text'):
                        if value.encoding == 0 and ((PY3 and isinstance(value.text[0],str)) or (not PY3 and isinstance(value.text[0],unicode))):
                            enc = detect_encoding_by_unicode('\n'.join(value.text))
                            for i in range(len(value.text)):
                                value.text[i] = value.text[i].encode('iso-8859-1').decode(enc)
                            value.encoding = 3
                id3.save()
            except ImportError:
                print('Mutagen package not found. Id3 encoding of mp3 will not be converted to utf-8')

        # convert encoding of .cue to utf-8
        elif os.path.splitext(f)[1] == '.cue':
            with open(f,'rb') as file:
                bytes_data = file.read()
            enc = detect_encoding_by_bytes(bytes_data)
            with open(f,'w') as file:
                file.write(str(bytes_data,encoding=enc))

        elif os.path.splitext(f)[1] == '.tak':
            subprocess.call(['/usr/local/bin/ffmpeg', '-i', f,  os.path.splitext(f)[0] + '.wav'])
            f = os.path.splitext(f)[0] + '.wav'

        music_list.append(os.path.abspath(f))

def cue_look_for_wav(cue_file):
    lines = []
    with open(cue_file,'r') as f:
        for line in f:
            pattern = r'FILE\s*\".*'
            if re.match(pattern,line):
                lines.append(line.replace('.tak','.wav',1))
            else:
                lines.append(line)
    with open(cue_file,'w') as f:
        f.write(''.join(lines))

if __name__ == '__main__':
    music_list = []

    for f in sys.argv[1:]:
        add_music(f,music_list)

    open_arg = ['open', '-a', 'XLD']
    open_arg.extend(music_list)
    subprocess.check_output(open_arg)


#pulling out data from the xml

import xml.etree.ElementTree as ET
data = '''<person>
    <name>Chuck</name>
    <phone type="intl">
        +1 734 303 4456
    </phone>
    <email hide="yes"/>
    </person>'''

tree = ET.fromstring(data)
print('Name:',tree.find('name').text)
print('Attr:',tree.find('email').get('hide'))

#Triple-strings means multi line strings

# fromstring() takes the data string and give us back the tree

Course 3 - Using Python to Access Web Data/Week 5/parsing_XML.py


"""
Uses dadi to infer the demographic history using syn-SFS and fitdadi to infer the DFE of a nonsynonymous sfs.
For now trying to use the example data to compute the SFS. Later we will test different h coefficients

JCM 201907011

usage: python DFE_inference_discrete_intervals.py 0.5

in which 0.5 is the dominance_coefficient
"""

import sys
import os
import logging
import time
import argparse
import warnings
import numpy
import dadi
import Selection
import scipy.stats.distributions
import scipy.integrate
import scipy.optimize
import pandas as pd

# dadi characterizes genotype fitnesses as:
# 1, 1 + 2sh, and 1+2s, where 1+2sh
h_coefficient = sys.argv[1]
times = 5 # iteractions across the different DFE models
print("Using the dominance coefficient equal to")
print(h_coefficient)

#####################################
# Defining the functions used
#####################################

def gamma_dist(mgamma, alpha, beta):
	"""Define a gamma distribution.

	mgamma: float which describes the mean value of this gamma
		 distribution.
	alpha: shape parameter of the gamma distribution.
	beta: scale parameter of the gamma distribution.
	"""
	return scipy.stats.distributions.gamma.pdf(-mgamma, alpha, scale=beta)

def two_epoch_sel(params, ns, pts, h=0.5):
	nu, T, gamma = params
	xx = dadi.Numerics.default_grid(pts)
	phi = dadi.PhiManip.phi_1D(xx, gamma=gamma, h=h)
	phi = dadi.Integration.one_pop(phi, xx, T, nu, gamma=gamma)
	fs = dadi.Spectrum.from_phi(phi, ns, (xx,))
	return fs

def discrete_not_complementary(mgamma, p1, p2, p3, p4): #, alpha, beta
	"""Define a discrete not complementary distribtuion.

	mgamma: float which describes the mean value of this gamma distribution.
	p1, p2, p3, p4: are the breakpoints of a discrete distribution

	five bins, (the discrete DFE) with breaks at s = [0, 10-5, 10-4, 10-3, 10-2, 1]
	# bins are discretized by scaling s coefficients to 2N_anc*s (ancestral 2N is 100000)
	"""
	two_Nanc=10000
	mgamma=-mgamma
	#assume anything with gamma<1e-4 is neutral
	# Assuming anything with s = 1e-5 is bin 1 2Ns = (10000*1e-5)
	if (two_Nanc*0 <= mgamma) and (mgamma < two_Nanc*1e-5):
		bin1 = p1/(0.1 - 0)
		return(bin1)

	# Assume anything with s => 1e-5 and s < 1e-4 is bin 2 (2Ns = 0.1 - 1)
	if (two_Nanc*1e-5 <= mgamma) and (mgamma < two_Nanc*1e-4):
		bin2 = p2/(1 - 0.1)
		return(bin2)

	# Assume anything with s [1e-4, < 1e-3) is bin 3 (2Ns = 1 - 10)
	if (two_Nanc*1e-4 <= mgamma) and (mgamma < two_Nanc*1e-3):
		bin3 = p3/(10 - 1)
		return(bin3)

	# Assume anything with s [1e-3, < 1e-2) is bin 4 (2Ns = 10 - 100)
	if (two_Nanc*1e-3 <= mgamma) and (mgamma < two_Nanc*1e-2):
		bin4 = p4/(100 - 10)
		return(bin4)
	else:
		return (1-(p1+p2+p3+p4))/(100-1000)

def discrete_not_complementary(mgamma, a1, a2, a3, a4):
    #made this range overlap with int_breaks for convenience
    Nanc=10000.
    x0 = 0. * 2 * Nanc
    x1 = 1e-5 * 2 * Nanc
    x2 = 1e-4 * 2 * Nanc
    x3 = 1e-3 * 2 * Nanc
    x4 = 1e-2 * 2 * Nanc
    x5 = 1 * 2 * Nanc
    a5 = 1. - a1 - a2 - a3 - a4
    mgamma = -mgamma
    if (mgamma >= x0) and (mgamma <= x1):
        return (a1/(x1-x0))
    elif (mgamma > x1) and (mgamma <= x2):
        return (a2/(x2-x1))
    elif (mgamma > x2) and (mgamma <= x3):
        return (a3/(x3-x2))
    elif (mgamma > x3) and (mgamma <= x4):
        return (a4/(x4-x3))
    elif (mgamma > x4) and (mgamma <= x5):
        return (a5/(x5-x4))
    else:
        return 0

def discretedfe(mgamma, p1, p2, p3, p4, p5): #, alpha, beta
	"""Define a discrete uniform distribution with 5 discrete bins, in which p_i (i:1-5) are the variables for each selection coefficient bin.

	five bins, (the discrete DFE) with breaks at s = [0, 10-5, 10-4, 10-3, 10-2, 1]
	# bins are discretized by scaling s coefficients to 2N_anc*s (ancestral 2N is 100000)
	"""
	mgamma = -mgamma
	two_Nanc=10000
	# Assuming anything with s = 1e-5 is bin 1 2Ns = (10000*1e-5)
	if (two_Nanc*0 <= mgamma) and (mgamma < two_Nanc*1e-5):
		bin1 = p1/(two_Nanc*1e-5 - 0)
		return(bin1)

	# Assume anything with s => 1e-5 and s < 1e-4 is bin 2 (2Ns = 0.1 - 1)
	if (two_Nanc*1e-5 <= mgamma) and (mgamma < two_Nanc*1e-4):
		bin2 = p2/(two_Nanc*1e-4 - two_Nanc*1e-5)
		return(bin2)

	# Assume anything with s [1e-4, < 1e-3) is bin 3 (2Ns = 1 - 10)
	if (two_Nanc*1e-4 <= mgamma) and (mgamma < two_Nanc*1e-3):
		bin3 = p3/(two_Nanc*1e-3 - two_Nanc*1e-4)
		return(bin3)

	# Assume anything with s [1e-3, < 1e-2) is bin 4 (2Ns = 10 - 100)
	if (two_Nanc*1e-3 <= mgamma) and (mgamma < two_Nanc*1e-2):
		bin4 = p4/(two_Nanc*1e-2 - two_Nanc*1e-3)
		return(bin4)

	if (two_Nanc*1e-2 <= mgamma) and (mgamma < two_Nanc*1e-1):
		bin5 = p5/(two_Nanc*1e-1 - two_Nanc*1e-2)
		return(bin5)

	if (mgamma > 100):
		return(0)

# define a constraint function. At MLE, consfunc = 0, for which all the 4 discrete parameters should sum up to 1
def consfunc(x, *args):
	return 1-sum(x) #[0:-2]

def integrating_over_multiple_gammas(demog_params = [2, 0.05],
									theta_ns = 4000.,
									ns = numpy.array([250]),
									pts_l = [600, 800, 1000],
									tiny_e = 0.000000000001,
									int_breaks = [1e-5, 0.1, 1, 10, 100, 500]):
	# Npts log-spaced points
	# -1e-9 is just adding tiny values so it is not exactly in the border of the break.
	int_breaks = [i - tiny_e for i in int_breaks]
	print(int_breaks)

	spectra = Selection.spectra(demog_params, ns, two_epoch_sel, pts_l=pts_l,
								int_breaks=int_breaks, Npts=200,
								echo=True, mp=True)
	return(spectra)

########################################################
# Integrating over a range of gammas and set breaks
########################################################

#set demographic parameters and theta. this is usually inferred from the synonymous sites, we will do it when not using simulated data
# demog_params = [2, 0.05]

theta_ns = 4000. # NS = 4000 = 4NALNS.
spectra = integrating_over_multiple_gammas()

########################################################
# Loading sample data
########################################################

data = dadi.Spectrum.from_file('example.sfs')
# This sample data has the 2anc size = 10000
# A two fold expansion was simulated (10000 -> 20000)
# demographic parameters are:
# Sample data was drawn from a gamma distribution with alpha = 0.18 and beta = 686.7

########################################################
# Fitting a gamma DFE to the data with a grid search not complementary function
########################################################
gamma_vec = numpy.frompyfunc(gamma_dist, 3, 1)
sel_params = [0.1, 1000] # alpha, beta
lower_bound = [1e-3, 1e-3] # lower bound of alpha, lower bound of beta
upper_bound = [1, 50000.] # upper bound of alpha, upper bound of beta

gamma_max_likelihoods = []
gamma_guesses = dict()
for i in range(times):
	p0 = dadi.Misc.perturb_params(sel_params, lower_bound=lower_bound, upper_bound=upper_bound)
	print("Beginning optimization with guess, {0}.".format(p0))

	popt = Selection.optimize_log(p0, data, spectra.integrate, gamma_vec,
								   theta_ns, lower_bound=lower_bound,
								   upper_bound=upper_bound, verbose=len(sel_params),
								   maxiter=50)

	gamma_max_likelihoods.append(popt[0])
	gamma_guesses[popt[0]] = popt

gamma_max_likelihoods.sort()

########################################################
# Outputting guesses
########################################################

results_discrete = []
print(gamma_guesses.keys())
for i in range(len(gamma_guesses.keys())):
	best_popt_discrete = gamma_guesses[gamma_max_likelihoods[i]]
	results_discrete.append([best_popt_discrete[0], best_popt_discrete[1][0], best_popt_discrete[1][1]]) # likelihood, alpha, beta

# Writing results as csvs:
df_discrete = pd.DataFrame(results_discrete, columns =['Likelihood', 'alpha', 'beta'], dtype = float) #, 'alpha', 'beta'
df_discrete.to_csv('DFE_inference_gamma_{h}_.csv'.format(h=h_coefficient), index=False)

########################################################
# Fitting a discrete DFE to the data with a grid search not complementary function
########################################################

# Vectorizing the DFE
discrete_vec = numpy.frompyfunc(discrete_not_complementary, 5, 1)
print(discrete_vec)

# Selection parameters in terms of 2Nanc*s
sel_params = [0.1, 1, 10, 100] # 0.2, 1000. #bin1, # bin2, #bin3, #bin4 #alpha, #beta
lower_bound = [1e-2, 1e-2, 1e-2, 1e-2] #, 1e-2, 1e-2 #bin1, # bin2, #bin3, #bin4, #bin5, #alpha, #beta
upper_bound = [1, 1, 1, 1] #1, 50000.

discrete_max_likelihoods = []
discrete_guesses = dict()
for i in range(times):
	p0 = dadi.Misc.perturb_params(sel_params, lower_bound=lower_bound, upper_bound=upper_bound)
	print("Beginning optimization with guess, {0}.".format(p0))
	popt = Selection.optimize_log(p0, data, spectra.integrate, discrete_vec,
								   theta_ns, lower_bound=lower_bound,
								   upper_bound=upper_bound, verbose=len(sel_params),
								   maxiter=50)

	discrete_max_likelihoods.append(popt[0])
	discrete_guesses[popt[0]] = popt

discrete_max_likelihoods.sort()

########################################################
# Outputting guesses
########################################################

results_discrete = []
print(discrete_guesses.keys())
for i in range(len(discrete_guesses.keys())):
	best_popt_discrete = discrete_guesses[discrete_max_likelihoods[i]]
	results_discrete.append([best_popt_discrete[0], best_popt_discrete[1][0], best_popt_discrete[1][1], best_popt_discrete[1][2], best_popt_discrete[1][3]]) #,  best_popt_discrete[1][4],  best_popt_discrete[1][5]]

# Writing results as csvs:
df_discrete = pd.DataFrame(results_discrete, columns =['Likelihood', 'bin1', 'bin2', 'bin3', 'bin4'], dtype = float) #, 'alpha', 'beta'
df_discrete.to_csv('DFE_inference_discrete_{h}_.csv'.format(h=h_coefficient), index=False)

########################################################
# Fitting a discrete DFE to the data with a grid search COMPLEMENTARY
########################################################

# Vectorizing the DFE
discrete_vec = numpy.frompyfunc(discretedfe, 6, 1)

# Selection parameters in terms of 2Nanc*s
sel_params = [0.1, 1, 10, 100, 1000] # 0.2, 1000. #bin1, # bin2, #bin3, #bin4 #bin5
lower_bound = [1e-2, 1e-2, 1e-2, 1e-2, 1e-2] #, 1e-2, 1e-2 #bin1, # bin2, #bin3, #bin4, #bin5
upper_bound = [1, 1, 1, 1, 1] #1, 50000.

discrete_max_likelihoods = []
discrete_guesses = dict()
for i in range(times):
	p0 = dadi.Misc.perturb_params(sel_params, lower_bound=lower_bound, upper_bound=upper_bound)
	print("Beginning optimization with guess, {0}.".format(p0))
	popt = Selection.optimize_cons(p0, data, spectra.integrate, discrete_vec,
								   theta_ns, lower_bound=lower_bound,
								   upper_bound=upper_bound, verbose=len(sel_params),
								   maxiter=100, constraint=consfunc)

	discrete_max_likelihoods.append(popt[0])
	discrete_guesses[popt[0]] = popt

discrete_max_likelihoods.sort()

########################################################
# Outputting guesses
########################################################

results_discrete = []
print(discrete_guesses.keys())
for i in range(len(discrete_guesses.keys())):
	best_popt_discrete = discrete_guesses[discrete_max_likelihoods[i]]
	print(best_popt_discrete)
	results_discrete.append([best_popt_discrete[0], best_popt_discrete[1][0], best_popt_discrete[1][1], best_popt_discrete[1][2], best_popt_discrete[1][3], best_popt_discrete[1][4]]) #,  best_popt_discrete[1][4],  best_popt_discrete[1][5]]

# Writing results as csvs:
df_discrete = pd.DataFrame(results_discrete, columns =['Likelihood', 'bin1', 'bin2', 'bin3', 'bin4', 'bin5'], dtype = float) #, 'alpha', 'beta'
df_discrete.to_csv('DFE_inference_discrete_constrained_{h}_.csv'.format(h=h_coefficient), index=False)


import click
from cooking import bake, deliver, pickup
from pizza import Pizza, Margherita, Pepperoni, Hawaiian

@click.group()
def cli():
    pass

@cli.command()
@click.option("--delivery", default=False, is_flag=True)
@click.option(
    "--size", default="L", type=click.Choice(["L", "XL"], case_sensitive=False)
)
@click.argument(
    "pizza",
    nargs=1,
    type=click.Choice(["Margherita", "Pepperoni", "Hawaiian"],
                      case_sensitive=False),
)
def order(pizza: str, delivery: bool, size: str) -> None:
    """Готовит и доставляет пиццу"""
    pizza = pizza.lower().title()
    cls_pizza = eval(pizza)
    obj_pizza = cls_pizza(size)
    if delivery:
        print(bake(obj_pizza), deliver(obj_pizza), sep="\n")
    else:
        print(bake(obj_pizza), pickup(obj_pizza), sep="\n")

@cli.command()
def menu() -> None:
    """Выводит меню"""
    for pizza in ["Margherita", "Pepperoni", "Hawaiian"]:
        cls_pizza = eval(pizza)
        obj_pizza = cls_pizza()
        print(f'- {obj_pizza.name}: {", ".join(obj_pizza.dict().keys())}')

if __name__ == "__main__":
    cli()


# find F at 'n' position
# import time
# start_time = time.time()

# def calc_lastfib(n):
#     alist = [None] * (n+1)
#     alist[0] = 0
#     alist[1] = 1

#     for i in range(2,n+1):
#         alist[i] = (alist[i-1] + alist[i-2])%10
#     return alist[n]
# # n = int(input())
# # print(calc_lastfib(n))

# print('answer is',calc_lastfib(3), 'took time ' ,"--- %s seconds ---" % (time.time() - start_time))
# print('answer is',calc_lastfib(331), 'took time ' ,"--- %s seconds ---" % (time.time() - start_time))
# print('answer is',calc_lastfib(327305), 'took time ' ,"--- %s seconds ---" % (time.time() - start_time))

# for submission

# Uses python3
import sys

def get_fib_last(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    F = [None] * (n+1)
    F[0] = 0
    F[1] = 1

    for i in range(2,n+1):
        F[i] = (F[i-1]+F[i-2]) % 10
    return F[n]

if __name__ == '__main__':
    input = sys.stdin.read()
    n = int(input)
    print(get_fib_last(n))


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('madplan', '0003_auto_20141228_0330'),
    ]

    operations = [
        migrations.AlterField(
            model_name='madhold',
            name='medlemmer',
            field=models.ManyToManyField(related_name=b'madhold', null=True, to=b'madplan.Person', db_table=b'madplan_person_hele_madhold', blank=True),
        ),
    ]


# -*- coding: utf-8 -*-
"""Episode: 1-2.教会なう
"""
## path
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append('storybuilder')
## local libs
from storybuilder.builder.world import World
from storybuilder.builder.writer import Writer

## define alias
W = Writer
_ = W.getWho()

## scenes
def sc_church(w: World):
    hero = W(w.hero)
    yula = W(w.yula)
    inside, outside = W(w.inside), W(w.outside)
    return w.scene("教会にて",
            hero.come("教会を訪れる"),
            inside.look("玄関の木戸を潜ると、そこには長椅子の並ぶホールが広がる"),
            inside.look("細長いステンドグラスには$w_godや、世界創生神話が描かれる"),
            w.eventPoint("人物紹介", "$yula初登場"),
            yula.be("神父の代わりに、知らない女性がいる"),
            yula.look("神父の衣装ではない",
                "長い金髪にすらりと背の高い女性",
                "まつげが長く、しゅっと切れ長の目",
                "涼し気な口元に濃い紅"),
            yula.look("肩からズタ袋を下げている"),
            yula.do("背を向けて立っている",
                "聖書を開いている"),
            hero.talk("あのー、$clerc神父は？"),
            yula.talk("あ、えっとね、神父なら今旅行中だから、$meが代行しているのよ"),
            hero.talk("そうなんですか"),
            yula.look("普通の服装", "長い金髪を後ろでくくっている", "美人でスリム",
                "胸は大きい"),
            hero.talk("あの、お祈りいいすか？"),
            yula.talk("ええ、どうぞ"),
            hero.do("祈りの言葉を唱えて、手を合わせる"),
            yula.talk("でも最近の若い子にしては珍しいわね",
                "何？　親が熱心なの？"),
            hero.talk("ああ、$me、父親が失踪してるんですよ"),
            hero.do("こう見えて$dadが失踪して以来、毎日帰ってくることを願って祈りを欠かさなかった"),
            yula.talk("そう", "それじゃあしっかり祈っときなさい"),
            yula.do("何故か$Sも一緒に女神像に祈る"),
            w.comment("$yulaと話しておく"),
            stage=w.on_church1,
            )

def sc_explainsmaph(w: World):
    hero = W(w.hero)
    yula = W(w.yula)
    inside, outside = W(w.inside), W(w.outside)
    return w.scene("$smaphの使い方",
            yula.be(),
            hero.be(),
            yula.do("$smaphを触って何か確認している$S"),
            yula.talk("へえ、何かやたらと賑わってるわね", "何でだろ"),
            hero.talk("あのー、代理さん"),
            yula.talk("代理ってねえ、$meは$yulaって名前があるのよ",
                "で、何さ？"),
            hero.talk("これ、使い方って分かります？"),
            yula.talk("あんた$smaphも知らないの？"),
            hero.talk("いや、今日初めてなもんで"),
            yula.talk("最新型じゃない", "あんたひょっとして金持ちのぼんぼん？"),
            hero.talk("全然", "抽選で当たって"),
            yula.talk("うらやましいわ"),
            hero.do("$yulaから$smaphの使い方について少し教わる"),
            yula.talk("基本的には画面にいろいろ表示されるし、",
                "わからなければヘルプを見ればいい",
                "説明が出てるでしょ？"),
            hero.talk("これはどうやって出すんですか？"),
            yula.do("頭を抱えるが、一応教えてやる"),
            hero.talk("あー！", "これで$w_tweetすればいいのか！"),
            yula.talk("だからね、ここをこうして"),
            hero.talk("おお、すげえ！"),
            hero.do("画面にはずらずらと世界中の人の$w_tweetが流れてくる"),
            hero.do("その中にやけに「$w_Y」という文字が踊っている"),
            yula.talk("あれ？　なんかバズってるわね"),
            hero.talk("バズ？"),
            yula.talk("人気だってこと",
                "で、もういい？", "$meもそろそろ行かないと"),
            hero.talk("あ、どうもありがとうございました"),
            yula.go("出て行ってしまう"),
            hero.talk("あれ？　代行はいいの？"),
            )

def sc_followers(w: World):
    hero = W(w.hero)
    yula = W(w.yula)
    inside, outside = W(w.inside), W(w.outside)
    return w.scene("勇者のフォロワー",
            hero.come("教会を出て、とりあえず中央広場に戻ってくる"),
            outside.look("閑散として露店も何もなくなっている"),
            hero.talk("あれ？　どうしたんだろう",
                "今日って何かあったかな？"),
            hero.think("生まれる前は魔物が街に何度も押し入ってきて、その度に全員避難とかしていたらしい"),
            hero.do("警鐘も聞いていないし、と"),
            hero.do("閑散とした空気を感じる"),
            hero.do("黒猫一匹だけいるのを見て"),
            hero.do("と、$smaphに通知が"),
            hero.talk("何だこれ？"),
            hero.do("画面にはフォローされましたと出る",
                "名前は全然知らない。$makoとかいう文字が出ている"),
            hero.talk("何だこれ"),
            hero.talk("ま、いっか"),
            hero.do("旅の手引きを確認する$S"),
            w.eventPoint("旅の手引き", "手引きを見て仲間と準備が必要と知る"),
            hero.do("旅の手引きには仲間を集めることと、装備や薬草など、旅に必要なものを揃えてから出発するように書かれている"),
            hero.talk("仲間ってどこで集めればいいんだ？"),
            hero.do("よく旅の商人が傭兵を雇っていることを思い出して"),
            hero.talk("傭兵ギルドか"),
            hero.go("ギルド本部に向かう"),
            stage=w.on_centralsquare,
            )

## episode
def ep_church(w: World):
    return w.episode("1-2.教会なう",
            sc_church(w),
            sc_explainsmaph(w),
            sc_followers(w),
            ## NOTE
            ##  - 教会にやってきて祈ろうとする
            ##  - 教会は神父代理という女性がいた
            ##  - 祈りを済ませて外に出る。勇者のフォローが増えていて喜ぶ
            )