import depth_tools
import cv2
import numpy as np
import pandas as pd
from itertools import product
import os
from tqdm import tqdm
from glob import glob
import re
import sys
import os
import random

from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras.callbacks import CSVLogger, ModelCheckpoint, ReduceLROnPlateau

import network

'''
ARGV
1: local output dir
2: is model exists (is not training start)
3: epoch num
4-6: parameter
'''
argv = sys.argv
# _, out_local, is_model_exist, epoch_num, augment_type, dropout, net_type, transfer_learn = argv
# _, out_local, learn_type, start, epoch_num, augment_type, dropout, learning_rate = argv
_, out_local, learn_type, start, epoch_num, augment_type, dropout, scale = argv
# _, out_local, learn_type, start, epoch_num, augment_type, dropout = argv

learning_rate = '0'
if learning_rate is '0':
    learning_rate = 0.001
elif learning_rate is '1':
    learning_rate = 0.0001
elif learning_rate is '2':
    learning_rate = 0.00001

# Transfer Learning
is_transfer_learning = False
is_finetune = False
if learn_type is '1':
    is_transfer_learning = True
elif learn_type is '2':
    is_finetune = True

is_start = True
if start is '1':
    is_start = False

# if is_transfer_learning or is_finetune:
if is_transfer_learning:
    is_model_exist = '1'
else:
    if is_start:
        is_model_exist = '0'
    else:
        is_model_exist = '1'

net_type = '0'

os.chdir(os.path.dirname(os.path.abspath(__file__))) #set currenct dir

#InputData
if is_transfer_learning or is_finetune:
    src_dir = '../data/input_200318'
else:
    src_dir = '../data/render'

# src_dir = '../data/render_no-tilt'

#RemoteOutput
out_dir = 'output'
#LocalOutput
# Train on local
is_local_train = True
# is_local_train = False
out_local = 'output_' + out_local
if is_local_train:
    out_dir = '../output/' + out_local

# resume_from = None  # start new without resume
# resume_from = 'auto'  # resume from latest model file
# resume_from = ['output /model-final.hdf5', 5]   # [file, epoch_num]

'''
Test Data
ori 110cm : 16 - 23
small 100cm : 44 - 47
mid 110cm : 56 - 59
'''
'''data index'''
# data_idx_range = range(80)
# data_idx_range = range(160)

if is_transfer_learning or is_finetune:
    # data_idx_range = list(range(16)) # 0 - 15
    # data_idx_range.extend(list(range(24, 40))) # 24 - 43
    # data_idx_range.extend(list(range(40, 44)))
    # data_idx_range.extend(list(range(48, 56))) # 48 - 55
    # data_idx_range.extend(list(range(60, 68))) # 60 -67

    data_idx_range = [0, 1, 3, 6, 40, 41, 42, 43, 48, 49, 50, 51]
else:
    data_idx_range = range(160)

# parameters
depth_threshold = 0.2
difference_threshold = 0.1
# difference_threshold = 0.005
# difference_threshold = 0.003
patch_remove = 0.5
# dropout_rate = 0.12
dropout_rate = int(dropout) / 100

# model
save_period = 1

# monitor train loss or val loss
monitor_loss = 'val_loss'
# monitor_loss = 'loss'

# input
is_input_depth = True
is_input_frame = True

# normalization
is_shading_norm = True
# is_shading_norm = False

batch_shape = (120, 120)
batch_tl = (0, 0)  # top, left

train_batch_size = 64
# train_batch_size = 100
# train_batch_size = 128

# val_rate = 0.1
val_rate = 0.3

# progress bar
verbose = 1
# verbose = 0

# difference_scaling = 100
# difference_scaling = 10
difference_scaling = int(scale)

# augmentation
is_augment = True
if augment_type == '0':
    is_augment = False
# augment_rate = 1
augment_rate = 4
# augment_val_rate = 1
augment_val_rate = 4

shift_max = 0.1
# shift_max = 0.2
# shift_max = 0.5
rotate_max = 45
# rotate_max = 90
# zoom_range=[0.5, 1.5]
zoom_range=[0.9, 1.1]
# zoom_range=[0.8, 1.2]

def augment_zoom(img):
    h, w, s = img.shape
    random.seed(int(np.sum(img[:, :, 1])))
    scale = random.uniform(zoom_range[0], zoom_range[1])
    resize_w, resize_h = int(w*scale), int(h*scale)

    x = cv2.resize(img, (resize_w, resize_h))
    x[:, :, 1] = x[:, :, 1] / scale
    if s is 2:
        x[:, :, 0] = x[:, :, 0] / scale

    if scale > 1:
        new_img = x[int((resize_h - h)//2): int((resize_h + h)//2),
                   int((resize_w - w)//2): int((resize_w + w)//2), :]
    else:
        new_img = np.zeros_like(img)
        new_img[int((h - resize_h)//2): int((h + resize_h)//2),
                int((w - resize_w)//2): int((w + resize_w)//2), :] = x
    return new_img

if is_augment:
    if augment_type is '1':
        datagen_args = dict(
            rotation_range=rotate_max,
            width_shift_range=shift_max,
            height_shift_range=shift_max,
            shear_range=0,
            # zoom_range=[0.9, 1.1],
            fill_mode='constant',
            cval=0,
            # horizontal_flip=True,
            # vertical_flip=True
            preprocessing_function=augment_zoom
        )
    elif augment_type is '2': # shift
        datagen_args = dict(
            width_shift_range=shift_max,
            height_shift_range=shift_max,
            shear_range=0,
            fill_mode='constant',
            cval=0,
        )
    elif augment_type is '3': # rotate
        datagen_args = dict(
            rotation_range=rotate_max,
            shear_range=0,
            fill_mode='constant',
            cval=0,
        )
    elif augment_type is '4': # zoom
        datagen_args = dict(
            shear_range=0,
            fill_mode='constant',
            cval=0,
            preprocessing_function=augment_zoom
        )
    elif augment_type is '5': # no-zoom
        datagen_args = dict(
            rotation_range=rotate_max,
            width_shift_range=shift_max,
            height_shift_range=shift_max,
            shear_range=0,
            fill_mode='constant',
            cval=0,
        )
    elif augment_type is '6': # no-rotate
        datagen_args = dict(
            width_shift_range=shift_max,
            height_shift_range=shift_max,
            shear_range=0,
            fill_mode='constant',
            cval=0,
            preprocessing_function=augment_zoom
        )
    elif augment_type is '7': # no-scale
        datagen_args = dict(
            rotation_range=rotate_max,
            width_shift_range=shift_max,
            height_shift_range=shift_max,
            shear_range=0,
            zoom_range=zoom_range,
            fill_mode='constant',
            cval=0
        )

    x_datagen = ImageDataGenerator(**datagen_args)
    y_datagen = ImageDataGenerator(**datagen_args)
    # x_datagen = ImageDataGenerator() # train loss no-aug
    # y_datagen = ImageDataGenerator() # train loss no-aug
    x_val_datagen = ImageDataGenerator(**datagen_args) # val loss aug
    y_val_datagen = ImageDataGenerator(**datagen_args) # val loss aug
    # x_val_datagen = ImageDataGenerator()
    # y_val_datagen = ImageDataGenerator()
    seed_train = 1
    seed_val = 2

epoch_num = int(epoch_num)

if is_model_exist is '0':
    resume_from = None  # start new without resume
else:
    # resume_from = 'auto'  # resume from latest model file

    if is_start:
        resume_from = 0
    else:
        df_log = pd.read_csv(out_dir + '/training.log')
        pre_epoch = int(df_log.tail(1).index.values) + 1
        # resume_from = pre_epoch
        resume_from = 'auto'

# def zip(*iterables):
#     sentinel = object()
#     iterators = [iter(it) for it in iterables]
#     while iterators:
#         result = []
#         for it in iterators:
#             elem = next(it, sentinel)
#             if elem is sentinel:
#                 return
#             result.append(elem)
#         yield tuple(result)

def prepare_data(data_idx_range):
    def clip_batch(img, top_left, size):
        # t, l, h, w = *top_left, *size
        t = top_left[0]
        l = top_left[1]
        h = size[0]
        w = size[1]
        return img[t:t + h, l:l + w]

    if is_transfer_learning or is_finetune:
        src_rec_dir = src_dir + '/rec'
        src_rec_dir = src_dir + '/rec_ajusted'
        src_frame_dir = src_dir + '/frame'
        src_gt_dir = src_dir + '/gt'
        src_shading_dir = src_dir + '/shading'
    else:
        src_frame_dir = src_dir + '/proj'
        src_gt_dir = src_dir + '/gt'
        src_shading_dir = src_dir + '/shade'
        src_rec_dir = src_dir + '/rec'

    # read data
    print('loading data...')
    x_train = []
    y_train = []
    for data_idx in tqdm(data_idx_range):
        if is_transfer_learning or is_finetune:
            src_bgra = src_frame_dir + '/frame{:03d}.png'.format(data_idx)
            # src_depth_gap = src_rec_dir + '/depth{:03d}.png'.format(data_idx)
            src_depth_gap = src_rec_dir + '/depth{:03d}.bmp'.format(data_idx)
            src_depth_gt = src_gt_dir + '/gt{:03d}.bmp'.format(data_idx)
            # src_shading = src_shading_dir + '/shading{:03d}.png'.format(data_idx)
            src_shading = src_shading_dir + '/shading{:03d}.bmp'.format(data_idx)
        else:
            src_bgra = src_frame_dir + '/{:05d}.png'.format(data_idx)
            src_depth_gt = src_gt_dir + '/{:05d}.bmp'.format(data_idx)
            src_shading = src_shading_dir + '/{:05d}.png'.format(data_idx)
            src_depth_gap = src_rec_dir + '/{:05d}.bmp'.format(data_idx)

        # read images
        bgr = cv2.imread(src_bgra, -1) / 255.
        depth_img_gap = cv2.imread(src_depth_gap, -1)
        # depth_gap = depth_tools.unpack_png_to_float(depth_img_gap)
        depth_gap = depth_tools.unpack_bmp_bgra_to_float(depth_img_gap)

        depth_img_gt = cv2.imread(src_depth_gt, -1)
        depth_gt = depth_tools.unpack_bmp_bgra_to_float(depth_img_gt)
        img_shape = bgr.shape[:2]

        # shading_bgr = cv2.imread(src_shading, -1)
        # shading[:, :, 0] = 0.299 * shading_bgr[:, :, 2] + 0.587 * shading_bgr[:, :, 1] + 0.114 * shading_bgr[:, :, 0]
        shading_gray = cv2.imread(src_shading, 0) # GrayScale
        shading = shading_gray

        is_shading_available = shading > 0
        mask_shading = is_shading_available * 1.0
        # depth_gap = depth_gt[:, :] * mask_shading
        # mean_depth = np.sum(depth_gap) / np.sum(mask_shading)
        # depth_gap = mean_depth * mask_shading
        depth_gap *= mask_shading

        if is_shading_norm: # shading norm : mean 0, var 1
            is_shading_available = shading > 16.0
            mask_shading = is_shading_available * 1.0
            mean_shading = np.sum(shading*mask_shading) / np.sum(mask_shading)
            var_shading = np.sum(np.square((shading - mean_shading)*mask_shading)) / np.sum(mask_shading)
            std_shading = np.sqrt(var_shading)
            shading = (shading - mean_shading) / std_shading
        else:
            shading = shading / 255.

        # is_depth_available = depth_gt > depth_threshold
        # mask_depth = is_depth_available * 1.0
        # depth_gap = np.zeros_like(depth_gt)
        # mean_depth = np.sum(depth_gt) / np.sum(mask_depth)
        # depth_gap = mean_depth * mask_depth

        # normalization (may not be needed)
        # depth_gap /= depth_gap.max()
        # depth_gt /= depth_gt.max()

        depth_thre = depth_threshold

        # merge bgr + depth_gap
        if is_input_frame:
            if is_input_depth:
                bgrd = np.dstack([shading[:, :], depth_gap, bgr[:, :, 0]])
            else:
                bgrd = np.dstack([shading[:, :], bgr[:, :, 0]])
        else:
            bgrd = np.dstack([shading[:, :], depth_gap])

        # clip batches
        b_top, b_left = batch_tl
        b_h, b_w = batch_shape
        top_coords = range(b_top, img_shape[0], b_h)
        left_coords = range(b_left, img_shape[1], b_w)

        # add training data
        for top, left in product(top_coords, left_coords):
            batch_train = clip_batch(bgrd, (top, left), batch_shape)

            # do not add batch if not valid ################
            # valid_pixels = np.logical_and(
            #     batch_train[:, :, 0].mean() > 0,
            #     batch_train[:, :, 1] > depth_threshold)
            # if np.count_nonzero(valid_pixels) < (b_h * b_w * 0.5):
            #     continue

            batch_gt_depth = clip_batch(depth_gt, (top, left), batch_shape)
            batch_gt_mask = clip_batch(depth_gap, (top, left), batch_shape)
            batch_gt = np.dstack([batch_gt_depth, batch_gt_mask])

            # do not add batch if not close ################
            is_gt_available = batch_gt_depth > depth_thre
            is_depth_close = np.logical_and(
                np.abs(batch_gt_mask - batch_gt_depth) < difference_threshold,
                is_gt_available)
            if np.count_nonzero(is_depth_close) < (b_h * b_w * patch_remove):
                continue

            if is_input_depth or is_input_frame:
                x_train.append(batch_train)
            else:
                # x_train.append(batch_train[:, :, 0].reshape((*batch_shape, 1)))
                x_train.append(batch_train[:, :, 0].reshape((batch_shape[0], batch_shape[1], 1)))
            # y_train.append(batch_gt.reshape((*batch_shape, 2)))
            y_train.append(batch_gt.reshape((batch_shape[0], batch_shape[1], 2)))
    return np.array(x_train), np.array(y_train), depth_thre

def main():
    x_data, y_data, depth_thre = prepare_data(data_idx_range)
    print('x train data:', x_data.shape)
    print('y train data:', y_data.shape)

    if is_augment:
        x_train, x_val, y_train, y_val = train_test_split(x_data, y_data,
                                                        test_size=val_rate, shuffle=False)
    else:
        x_train, y_train = x_data, y_data

    # check training data
    print('x_train length:', len(x_train))

    if is_augment:
        print('x_val length  :', len(x_val))
        print('data augmentation')
        x_datagen.fit(x_train, augment=True, seed=seed_train)
        y_datagen.fit(y_train, augment=True, seed=seed_train)
        x_val_datagen.fit(x_val, augment=True, seed=seed_val)
        y_val_datagen.fit(y_val, augment=True, seed=seed_val)

        x_generator = x_datagen.flow(x_train, batch_size=train_batch_size, seed=seed_train)
        y_generator = y_datagen.flow(y_train, batch_size=train_batch_size, seed=seed_train)
        x_val_generator = x_val_datagen.flow(x_val, batch_size=train_batch_size, seed=seed_val)
        y_val_generator = y_val_datagen.flow(y_val, batch_size=train_batch_size, seed=seed_val)

        train_generator = zip(x_generator, y_generator)
        val_generator = zip(x_val_generator, y_val_generator)
        print('generator created')

    if is_input_depth:
        if is_input_frame:
            ch_num = 3
        else:
            ch_num = 2
    else:
        if is_input_frame:
            ch_num = 2
        else:
            ch_num = 1

    # model configuration
    if net_type is '0':
        model = network.build_unet_model(
            batch_shape,
            ch_num,
            depth_threshold=depth_thre,
            difference_threshold=difference_threshold,
            # decay=decay,
            drop_rate=dropout_rate,
            scaling=difference_scaling,
            transfer_learn=is_transfer_learning,
            lr=learning_rate
            )
    elif net_type is '1':
        model = network.build_resnet_model(
            batch_shape,
            ch_num,
            depth_threshold=depth_thre,
            difference_threshold=difference_threshold,
            drop_rate=dropout_rate,
            scaling=difference_scaling
            )
    elif net_type is '2':
        model = network.build_dense_resnet_model(
            batch_shape,
            ch_num,
            depth_threshold=depth_thre,
            difference_threshold=difference_threshold,
            drop_rate=dropout_rate
            )

    # resume
    model_dir = out_dir + '/model'
    initial_epoch = 0
    if resume_from is None:
        initial_epoch = 0

    elif resume_from == 'auto':

        def extract_epoch_num(filename):
            return int(re.search(r'model-([0-9]+)\.hdf5', filename).group(1))

        model_files = glob(model_dir + '/model-*.hdf5')
        model_file = sorted(model_files, key=extract_epoch_num)[-1]
        initial_epoch = extract_epoch_num(model_file)
        print('resume from ', model_file, ', epoch number', initial_epoch)

    else:
        # model_file, initial_epoch = resume_from
        initial_epoch = resume_from
        # model_file = model_dir + '/model-best.hdf5'
        model_file = model_dir + '/model-final.hdf5'
        # print('resume from ', model_file, ', epoch number', initial_epoch)

    if resume_from is not None:
        model.load_weights(model_file)

    # make output dirs
    if is_model_exist is '0':
        # os.makedirs(out_dir, exist_ok=True)
        # os.makedirs(model_dir, exist_ok=True)
        os.makedirs(out_dir)
        os.makedirs(model_dir)

    # train
    # model_save_cb = ModelCheckpoint(model_dir + '/model-best.hdf5',
    #                                 # model_dir + '/model-{epoch:03d}.hdf5',
    #                                 monitor=monitor_loss,
    #                                 verbose=verbose,
    #                                 save_best_only=True,
    #                                 save_weights_only=True,
    #                                 mode='min',
    #                                 period=1)
    model_save_cb = ModelCheckpoint(model_dir + '/model-{epoch:03d}.hdf5',
                                    period=save_period,
                                    save_weights_only=True)
    csv_logger_cb = CSVLogger(out_dir + '/training.log',
                              append=(resume_from is not None))
    reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                    factor=0.5,
                                    patience=10,
                                    verbose=1)

    print('training')
    if is_augment:
        model.fit_generator(
            train_generator,
            steps_per_epoch=len(x_train)*augment_rate / train_batch_size + 1,
            epochs=epoch_num,
            initial_epoch=initial_epoch,
            shuffle=True,
            callbacks=[model_save_cb, csv_logger_cb],
            # callbacks=[model_save_cb, csv_logger_cb, reduce_lr],
            validation_data=val_generator,
            validation_steps=len(x_val)*augment_val_rate / train_batch_size + 1,
            verbose=verbose)
    else:
        model.fit(
            x_train,
            y_train,
            epochs=epoch_num,
            batch_size=train_batch_size,
            initial_epoch=initial_epoch,
            shuffle=True,
            validation_split=val_rate,
            callbacks=[model_save_cb, csv_logger_cb],
            verbose=verbose)

    model.save_weights(out_dir + '/model-final.hdf5')
    # model.save_weights(model_dir + '/model-final.hdf5')

if __name__ == "__main__":
    main()


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

'''
Author - Mohd Aquib
'''
import torch
import torch.nn as nn
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
import torch.optim as optim
from torchvision.utils import save_image
from PIL import Image
import matplotlib.pyplot as plt

model = models.vgg19(pretrained=True).features

device = 'cuda' if torch.cuda.is_available() else 'cpu'

class VGG(nn.Module):
    def __init__(self):
        super(VGG,self).__init__()
        self.req_features = ['0','5','10','19','28']
        self.model = models.vgg19(pretrained=True).features[:29]
    def forward(self,x):
        features = []
        for layer_num,layer in enumerate(self.model):
            x = layer(x)
            if(str(layer_num) in self.req_features):
                features.append(x)
        return features

def image_loader(path):
    image = Image.open(path)
    loader = transforms.Compose([transforms.Resize((512,512)), transforms.ToTensor()])
    image = loader(image).unsqueeze(0)
    return image.to(device,torch.float)

original_image = image_loader('/content/Nikhil.JPG')
style_image = image_loader('/content/style.jpg')

generated_image = original_image.clone().requires_grad_(True)

def calc_content_loss(gen_feat,orig_feat):
    content_l = torch.mean((gen_feat - orig_feat)**2)
    return content_l

def calc_style_loss(gen,style):
    batch_size,channel,height,width = gen.shape
    G = torch.mm(gen.view(channel,height*width),gen.view(channel,height*width).t())
    A = torch.mm(style.view(channel,height*width),style.view(channel,height*width).t())
    style_l = torch.mean((G-A)**2)
    return style_l

def calculate_loss(gen_features,orig_features,style_features):
    style_loss=content_loss=0
    for gen,con,style in zip(gen_features,orig_features,style_features):
        content_loss += calc_content_loss(gen,con)
        style_loss += calc_style_loss(gen,style)
    total_loss = alpha*content_loss + beta*style_loss
    return total_loss

model = VGG().to(device).eval()

epoch = 7000
lr = 0.004
alpha = 8
beta = 70
optimizer = optim.Adam([generated_image],lr=lr)

for i in range(epoch):
    gen_features = model(generated_image)
    orig_features = model(original_image)
    style_features = model(style_image)

    total_loss = calculate_loss(gen_features,orig_features,style_features)
    optimizer.zero_grad()
    total_loss.backward()
    optimizer.step()

    if(not(i%100)):
        print(total_loss)
        save_image(generated_image,'gen.png')


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

"""The setup script."""
import warnings
from setuptools import setup, find_packages, Command
from setuptools.command.install import install as _install

with open('README.md') as readme_file:
    readme = readme_file.read()

requirements = [
    'requests',
    'Click'
]

setup_requirements = [
    'pytest-runner'
]

test_requirements = [
    'pytest'
]

class install(_install):
    def run(self):
        # _install.run(self)
        _install.do_egg_install(self)

class FakeBdist(Command):
    """Fake bdist wheel class for ignoring bdist_wheel build
    """

    user_options = [(
        "dist-dir=",
        "d",
        "directory to put final built distributions in [default: dist]"
    ), (
        "python-tag=",
        None,
        "Python tag (cp32|cp33|cpNN) for abi3 wheel tag (default:false)"
    )]

    def initialize_options(self):
        self.dist_dir = None
        self.python_tag = None

    def finalize_options(self):
        pass

    def run(self):
        warnings.warn(
            "{name}{version} does not support building wheels".format(
                name=about["__title__"],
                version=about["__version__"]
            )
        )

setup(
    name='split-downloader',
    version='0.2.9',
    description="Parallel downloader",
    long_description=readme,
    author="Joe Paul",
    author_email='joeirimpan@gmail.com',
    url='https://github.com/joeirimpan/pyparallel',
    packages=find_packages(include=['pyparallel']),
    include_package_data=True,
    install_requires=requirements,
    entry_points='''
        [console_scripts]
        download=pyparallel:cli
    ''',
    license="MIT license",
    zip_safe=False,
    keywords='pyparallel',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        "Programming Language :: Python :: 2",
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],
    test_suite='tests',
    tests_require=test_requirements,
    long_description_content_type='text/markdown',
    setup_requires=setup_requirements,
    cmdclass={"install": install, "bdist_wheel": FakeBdist}
)


import sys
from types import FunctionType
from core import Disco, Params, result_iterator
import func

class DeprecatedFunc:
        def __init__(self, fun, name):
                self.fun = fun
                self.name = name

        def __getattr__(self, name):
                if name == "func_code":
                        print >> sys.stderr, "WARNING! disco.%s is "\
                                "deprecated. Use disco.func.%s instead."\
                                        % (self.name, self.name)
                        return self.fun.func_code
                raise AttributeException("%s not found" % name)

chain_reader = DeprecatedFunc(func.chain_reader, "chain_reader")

def job(master, name, input, fun_map = None, **kw):
        kw.update({"name": name, "input": input})
        kw["map"] = kw.get("map", fun_map)
        return Disco(master).new_job(**kw).wait(clean = kw.get("clean", True))


'''
Linked List Data Structure
'''

class Node:
    """
    This is Node class, which is automatically created using class Linked List.
    It contains Data and a "link" to next Node in order
    When initaized, it sets variables data and next, which acts like a pointer
    The Aviable Commands:
        None
    """

    def __init__(self, data):
        self.data = data
        self.next = None

    def __str__(self):
        return "The data: " + str(self.data)

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

    def __gt__(self, other):
        return self.data > other.data

class LinkedList:
    """
    This is the Single Linked List Data Structure.
    It works like this: A -> B -> C -> Null, where A is the Head, Null is the End and each capital letter is representing each Node (type Node().__doc__() to learn more), which points to another Node
    You can initialize Linked List like this: llist = LinkedList()")
    The Aviable Commands:
        print_llist() - Prints every element in Linked List
        append(data) - Puts new Node to the end of Linked List
        prepend(data) - Puts new Node to the beginning of Linked List
        insert(data, index) - Inserts Node on specified index
        delete(index, data) - Deletes Node on specified index or containing specified data
        swap(index1, index2) - Swaps two nodes
        reverse_list() - 1 -> 2 -> 3 - > None  == 3 -> 2 -> 1 -> None
        merge(self, other) - makes one Linked List out of two - 1 -> 3 -> 5 + 2 -> 4 -> 6 == 1 -> 2 -> 3 -> 4 -> 5 -> 6
        sort() - sorts Linked List elements from lowest to highest
        remove_duplicates() - removes duplicate data
        get_node(index) - gets node on specified index
    """

    def __init__(self):
        self.head = None

    def print_llist(self):
        curr_node = self.head

        while curr_node:
            print(curr_node.data, "-> ", end="")
            curr_node = curr_node.next
        print("None")

    def get_llist(self):
        llist_array = []
        curr_node = self.head

        while curr_node:
            llist_array.append(curr_node.data)
            curr_node = curr_node.next
        return llist_array

    def append(self, data):
        new_node = Node(data)

        if self.head == None:
            self.head = new_node
            return

        last_node = self.head

        while last_node.next:
            last_node = last_node.next

        last_node.next = new_node

    def prepend(self, data):
        new_node = Node(data)

        new_node.next = self.head
        self.head = new_node

    def insert(self, data, index):
        if index == 0:
            self.prepend(data)
            return

        new_node = Node(data)

        prev_node = self.head

        while prev_node.next and index != 1:
            prev_node = prev_node.next
            index -= 1
            if prev_node.next is None:
                print("llist.insert('", data ,"', ", index, ")")
                print("Error: The previous Node does not exist yet.")
                return
        next_node = prev_node.next

        new_node.next = next_node
        prev_node.next = new_node

    def delete(self, index=None, data=None):
        if index:
            if index == 0:
                self.head = self.head.next
                curr_node = self.head
                curr_node = None
                return

            prev_node = self.head
            while prev_node.next and index != 1:
                prev_node = prev_node.next
                index -= 1
                if prev_node.next is None:
                    print("Error: The Node does not exist yet, thus cannot be deleted.")
                    return

            curr_node = prev_node.next
            prev_node.next = prev_node.next.next

            curr_node = None
        else:
            curr_node = self.head
            prev_node = None

            while curr_node and curr_node.data != data:
                prev_node = curr_node
                curr_node = curr_node.next

            if curr_node == None:
                return

            if prev_node:
                prev_node.next = curr_node.next
            else:
                self.head = curr_node.next
            curr_node = None

    def swap(self, index_1, index_2):
        if index_1 == index_2:
            return

        if index_2 == 0:
            index_1, index_2 = index_2, index_1

        curr_node = self.head

        if index_1 == 0:
            base_index_2 = index_2
            while curr_node and index_2 >= 1:
                index_2 -= 1
                prev_node = curr_node
                curr_node = curr_node.next

            if curr_node == None:
                print("You cannot swap index 0 with", base_index_2,", because there is not index", base_index_2, "in this Stack.")
                return

            head_next = self.head.next

            prev_node.next = self.head
            self.head.next = curr_node.next
            if base_index_2 != 1:
                curr_node.next = head_next
            else:
                curr_node.next = self.head

            self.head = curr_node
        else:
            base_index_1 = index_1
            base_index_2 = index_2
            curr_node_1 = self.head
            curr_node_2 = self.head
            while curr_node_1 and index_1 >= 1:
                index_1 -= 1
                prev_node_1 = curr_node_1
                curr_node_1 = curr_node_1.next
            while curr_node_2 and index_2 >= 1:
                index_2 -= 1
                prev_node_2 = curr_node_2
                curr_node_2 = curr_node_2.next
            if curr_node_1 == None or curr_node_2 == None:
                print("You cannot swap indexes", base_index_1, "with", base_index_2, ", because one of them does not exist.")
                return

            prev_node_1.next = curr_node_2
            prev_node_2.next = curr_node_1
            curr_node_1.next, curr_node_2.next = curr_node_2.next, curr_node_1.next

    def reverse_list(self):
        curr_node = self.head
        prev_node = None

        while curr_node:
            next_node = curr_node.next
            curr_node.next = prev_node
            prev_node = curr_node
            curr_node = next_node

        self.head = prev_node

    def __reversed__(self):
        curr_node = self.head
        prev_node = None

        while curr_node:
            next_node = curr_node.next
            curr_node.next = prev_node
            prev_node = curr_node
            curr_node = next_node

        self.head = prev_node

    def merge(self, other):
        if not isinstance(self, LinkedList) or not isinstance(other, LinkedList):
            print("One of your inserted variables is not instance of Linked List")
            return

        new_self = self.sort()
        new_other = other.sort()
        new_llist = LinkedList()

        curr_self = new_self.head
        curr_other = new_other.head
        prev = None

        while curr_self or curr_other:
            prev_next = prev

            if curr_other == None or curr_self.data < curr_other.data:
                new_node = Node(curr_self.data)
                new_llist.append(new_node.data)

                prev = curr_self
                curr_self = curr_self.next
            elif curr_self.data > curr_other.data:
                new_node = Node(curr_other.data)
                new_llist.append(curr_other.data)

                prev = curr_other
                curr_other = curr_other.next
            else:
                return new_llist

        return new_llist

    def sort(self):
        while True:
            l_sorted = True
            counter = 0
            curr = self.head
            prev = None

            while curr:
                if prev and curr:
                    if curr < prev:
                        self.swap(counter-1, counter)
                        l_sorted = False
                        counter -= 1
                prev = curr
                curr = curr.next
                counter += 1

            if l_sorted:
                break

        return self

    def remove_duplicates(self):
        duplicates = []

        curr = self.head

        while curr:
            if curr.data in duplicates:
                self.delete(data=curr.data)
            else:
                duplicates.append(curr.data)
            curr = curr.next

    def get_node(self, index):
        curr = self.head

        while index >= 0:
            try:
                data = curr.data
                curr = curr.next
                index -= 1
            except:
                return None
        return data

    def __getitem__(self, index):
        curr = self.head

        while index >= 0:
            try:
                data = curr.data
                curr = curr.next
                index -= 1
            except:
                return None
        return data

    def count_occurencies(self, search, curr="a"):
        if curr == "a":
            curr = self.head
        if not curr:
            return 0
        elif curr.data == search:
            return 1 + self.count_occurencies(search, curr.next)
        else:
            return self.count_occurencies(search, curr.next)

    def rotate(self, index):
        curr = self.head
        nxt = curr.next

        while nxt:
            curr = nxt
            nxt = curr.next
            if index == 1:
                new_last = curr
            index -= 1

        if index > 0:
            return print("There is not that much Nodes in this Linked List. \nRotate() did not make any changes")

        curr.next = self.head
        self.head = new_last.next
        new_last.next = None

    def __len__(self):
        curr_node = self.head
        counter = 0

        while curr_node:
            counter += 1
            curr_node = curr_node.next
        return counter

    def __eq__(self, other):
        curr_self = self.head
        curr_other = other.head
        try:
            while curr_self or curr_other:
                if not curr_self.data == curr_other.data:
                    print(curr_self.data.data == curr_other.data.data)
                    return False
                curr_self = curr_self.next
                curr_other = curr_other.next
        except:
            return False

        return True

    def __add__(self, other):
        self_new = LinkedList()
        self.copy_list(self_new)

        other_new = LinkedList()
        other.copy_list(other_new)

        while len(self_new) < len(other_new):
            self_new.append(0)
        while len(self_new) > len(other_new):
            other_new.append(0)

        self_new.print_llist()
        other_new.print_llist()

        curr_other = other_new.head
        curr_self = self_new.head
        new_list = LinkedList()

        leftover = 0
        while curr_self and curr_other:
            result = curr_self.data + curr_other.data
            if new_list.head == None:
                if result >= 10:
                    result -= 10
                    new_node = Node(result + leftover)
                    leftover = 1
                else:
                    new_node = Node(result + leftover)
                    leftover = 0

                new_list.head = new_node
                prev = new_node
                curr_self = curr_self.next
                curr_other = curr_other.next
            else:
                if result >= 10:
                    result -= 10
                    new_node = Node(result + leftover)
                    leftover = 1
                else:
                    new_node = Node(result + leftover)
                    leftover = 0
                prev.next = new_node
                prev = new_node
                curr_self = curr_self.next
                curr_other = curr_other.next
        if leftover == 1:
            new_list.append(1)
        new_list.reverse_list()
        return new_list

    def copy_list(self, other):
        curr = self.head
        other_prev = None

        while curr:
            new_node = Node(curr.data)
            if curr == self.head:
                other.head = new_node
            else:
                other_prev.next = new_node
            other_prev = new_node
            curr = curr.next

    def is_palindrome(self):
        length = len(self)

        if length % 2 == 0:
            return

        curr = self.head
        length = int(length) / 2
        first_half = LinkedList()
        second_half = LinkedList()

        while length >= 1:
            first_half.append(curr.data)
            curr = curr.next
            length -= 1

        curr = curr.next
        while curr:
            second_half.append(curr.data)
            curr = curr.next

        second_half.reverse_list()

        if first_half == second_half:
            return True
        else:
            return False

    def __setitem__(self, index, data):
        curr = self.head

        while index >= 0:
            try:
                prev = curr
                curr = curr.next
                index -= 1
            except:
                return None
        prev.data = data

if __name__ == "__main__":
    '''llist = LinkedList()
    llist.append(5)
    llist.append(10)
    llist.append(12)
    llist.append(13)
    llist.print_llist()

    print("Lenght is: ", len(llist))
    print()

    llist.prepend("D")
    llist.insert("E", 2)
    llist.print_llist()
    print("Lenght is: ", len(llist))
    print()
    llist.delete(data="A")
    llist.print_llist()
    print("Lenght is: ", len(llist))
    print(LinkedList.__doc__)
    llist.swap(1, 1)
    llist.reverse_list()
    llist2 = LinkedList()
    llist2.append(11)
    llist2.append(7)
    llist2.append(8)
    llist2.append(6)
    llist2.print_llist()
    new_llist = LinkedList.merge(llist, llist2)
    print("Merged:", new_llist.get_llist())

    llist3 = LinkedList()
    llist3.append(1)
    llist3.append(4)
    llist3.append(25)
    llist3.append(0)
    llist3.sort()
    llist3.print_llist()

    llist4 = LinkedList()
    llist4.append("r")
    llist4.append("a")
    llist4.append("c")
    llist4.append("e")
    llist4.append("c")
    llist4.append("a")
    llist4.append("s")
    print(llist4.is_palindrome())
    llist4.rotate(5)
    llist4.print_llist()

    llist_add = LinkedList()

    llist_add.append(0)
    llist_add.append(5)

    llist_add_2 = LinkedList()

    llist_add_2.append(5)
    llist_add_2.append(7)

    added = llist_add + llist_add_2
    added.print_llist()'''

    llist = LinkedList()
    llist.append(5)
    llist.append(10)
    llist.append(12)
    llist.append(13)
    llist.print_llist()
    llist[0] = 1
    llist.print_llist()
    reversed(llist)
    llist.print_llist()
    print(llist[0])


# flake8: noqa

# Copyright (c) 2016 Tesora, Inc.
#
# This file is part of the Tesora DBaas Platform Enterprise Edition.
#
# Tesora DBaaS Platform is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# According to sec. 7 of the GNU Affero General Public License, version 3, the
# terms of the AGPL are supplemented with the following terms:
#
# "Tesora", "Tesora DBaaS Platform", and the Tesora logo are trademarks
#  of Tesora, Inc.,
#
# The licensing of the Program under the AGPL does not imply a trademark
# license. Therefore any rights, title and interest in our trademarks remain
# entirely with us.
#
# However, if you propagate an unmodified version of the Program you are
# allowed to use the term "Tesora" solely to indicate that you distribute the
# Program. Furthermore you may use our trademarks where it is necessary to
# indicate the intended purpose of a product or service provided you use it in
# accordance with honest practices in industrial or commercial matters.
#
# If you want to propagate modified versions of the Program under the name
# "Tesora" or "Tesora DBaaS Platform", you may only do so if you have a written
# permission by Tesora, Inc. (to acquire a permission please contact
# Tesora, Inc at trademark@tesora.com).
#
# The interactive user interface of the software displays an attribution notice
# containing the term "Tesora" and/or the logo of Tesora.  Interactive user
# interfaces of unmodified and modified versions must display Appropriate Legal
# Notices according to sec. 5 of the GNU Affero General Public License,
# version 3, when you propagate unmodified or modified versions of  the
# Program. In accordance with sec. 7 b) of the GNU Affero General Public
# License, version 3, these Appropriate Legal Notices must retain the logo of
# Tesora or display the words "Initial Development by Tesora" if the display of
# the logo is not reasonably feasible for technical reasons.

from eventlet.timeout import Timeout
from oslo_log import log as logging
from oslo_utils import importutils

from trove.common import cfg
from trove.common import exception
from trove.common.i18n import _
from trove.common.strategies.cluster import base
from trove.common.strategies.cluster.oracle_rac import utils as rac_utils
from trove.common import utils
from trove.extensions.security_group import models as secgroup_models
from trove.instance import models as instance_models
from trove.taskmanager import api as task_api
from trove.taskmanager import models as task_models

LOG = logging.getLogger(__name__)
CONF = cfg.CONF
USAGE_SLEEP_TIME = CONF.usage_sleep_time  # seconds.

class OracleRACTaskManagerStrategy(base.BaseTaskManagerStrategy):

    @property
    def task_manager_api_class(self):
        return OracleRACTaskManagerAPI

    @property
    def task_manager_cluster_tasks_class(self):
        return OracleRACClusterTasks

class OracleRACClusterTasks(task_models.ClusterTasks):

    def create_cluster(self, context, cluster_id):
        LOG.debug("Begin create_cluster for id: %s." % cluster_id)
        self.ds_conf = CONF.get(self.datastore_version.manager)
        self.network_driver = (importutils.import_class(
            CONF.network_driver))(context, None)

        def _create_cluster():
            db_instances = instance_models.DBInstance.find_all(
                cluster_id=cluster_id).all()
            instance_ids = [db_instance.id for db_instance in db_instances]
            LOG.debug("instances in cluster %s: %s" % (cluster_id,
                                                       instance_ids))
            if not self._all_instances_ready(instance_ids, cluster_id):
                return
            LOG.debug("all instances in cluster %s ready." % cluster_id)
            instances = [instance_models.Instance.load(
                context, instance_id) for instance_id in instance_ids]

            self._open_firewall_between_nodes(instance_ids)
            self._get_subnets()

            instance_guests = [self.get_guest(instance)
                               for instance in instances]

            for instance_guest in instance_guests:
                instance_guest.configure_hosts(
                    self.name,
                    self.pub_subnet['cidr'],
                    self.int_subnet['cidr'])
            self._get_hostnames(instances)
            for instance_guest in instance_guests:
                instance_guest.establish_ssh_user_equivalency(
                    self.host_ip_pairs)

            instance_guests[0].configure_grid(
                self.id, self.name, self._make_nodes_string(include_vips=True),
                self.pub_subnet['cidr'], self.int_subnet['cidr'])
            for instance_guest in instance_guests:
                instance_guest.run_grid_root()

            instance_guests[0].install_oracle_database(
                self._make_nodes_string())
            for instance_guest in instance_guests:
                instance_guest.run_oracle_root()

            self._allow_interconnect_vips(instances, instance_guests)

            instance_guests[0].create_rac_database(self._make_nodes_string())

            # for instance_guest in instance_guests[1:]:
            #     instance_guest.determine_sid()

            for instance in instances:
                self.get_guest(instance).cluster_complete()

        timeout = Timeout(CONF.cluster_usage_timeout)
        try:
            _create_cluster()
            self.reset_task()
        except Timeout as t:
            if t is not timeout:
                raise  # not my timeout
            LOG.exception(_("Timeout for building cluster."))
            self.update_statuses_on_failure(cluster_id)
        finally:
            timeout.cancel()

        LOG.debug("End create_cluster for id: %s." % cluster_id)

    def _open_firewall_between_nodes(self, instance_ids):
        LOG.debug("Opening firewall between cluster instances.")
        security_groups = [
            secgroup_models.SecurityGroup.
                get_security_group_by_id_or_instance_id(instance_id,
                                                        self.tenant_id)
            for instance_id in instance_ids]
        for security_group in security_groups:
            for friendly_group in security_groups:
                if security_group == friendly_group.id:
                    # same group
                    continue
                self.network_driver.create_security_group_rule(
                    security_group.id, ethertype='IPv4',
                    direction='ingress', remote_group_id=friendly_group.id)

    def _get_subnets(self):
        public_subnet_name = rac_utils.make_object_name(
            self.ds_conf, ['public', 'subnet'], self.id)
        interconnect_subnet_name = rac_utils.make_object_name(
            self.ds_conf, ['interconnect', 'subnet'], self.id)
        subnets = self.network_driver.list_subnets()
        self.pub_subnet = None
        self.int_subnet = None
        for subnet in subnets:
            if subnet['name'] == public_subnet_name:
                self.pub_subnet = subnet
            elif subnet['name'] == interconnect_subnet_name:
                self.int_subnet = subnet
        if not self.pub_subnet and self.int_subnet:
            raise exception.TroveError(_(
                "Could not find cluster's public and interconnect subnets."))
        LOG.debug("Cluster {clu} is using public subnet {pubsub} and "
                  "interconnect subnet {intsub}".format(
                      clu=self.id, pubsub=self.pub_subnet['cidr'],
                      intsub=self.int_subnet['cidr']))

    def _get_hostnames(self, instances):
        self.host_ip_pairs = []
        pub_subnet_manager = rac_utils.RACPublicSubnetManager(
            self.pub_subnet['cidr'])
        network_name = self.network_driver.get_network_by_id(
            self.pub_subnet['network_id'])['name']
        for instance in instances:
            public_ip = instance.addresses[network_name][0]['addr']
            i = pub_subnet_manager.instance_index_from_ip(public_ip)
            hostname = rac_utils.make_instance_hostname(self.name, i)
            # append a tuple
            self.host_ip_pairs.append((hostname, public_ip))

    def _make_nodes_string(self, include_vips=False):
        nodes = []
        for pair in self.host_ip_pairs:
            hostname = pair[0]
            if include_vips:
                nodes.append(':'.join([hostname, hostname + '-vip']))
            else:
                nodes.append(hostname)
        return ','.join(nodes)

    def _allow_interconnect_vips(self, instances, instance_guests):
        network_name = rac_utils.make_interconnect_network_name(self.ds_conf,
                                                                self.id)
        # get the interconnect subnet's ports
        ports = [port for port in self.network_driver.list_ports()
                 if port['fixed_ips'][0]['subnet_id'] == self.int_subnet['id']]
        for i in range(len(instances)):
            ip = instances[i].addresses[network_name][0]['addr']
            vip = instance_guests[i].get_private_vip(ip)
            port_id = [port for port in ports
                       if port['fixed_ips'][0]['ip_address'] == ip][0]['id']
            self.network_driver.update_port(
                port_id, allowed_address_pairs=[{'ip_address': vip}])

class OracleRACTaskManagerAPI(task_api.API):
    pass


import pandas as pd
import os
from datetime import datetime

def howmany_user():
    path1 = os.path.join(root_dir, 'tags.csv')
    path2 = os.path.join(root_dir, 'ratings.csv')
    df1 = pd.read_csv(path1)
    df2 = pd.read_csv(path2)
    user1 = list(df1['userId'].unique())
    user2 = list(df2['userId'].unique())
    user = user1+user2
    user_set = set(user)
    nums_user = len(user_set)
    return nums_user

def howmany_movie():
    path = os.path.join(root_dir, 'movies.csv')
    df = pd.read_csv(path)
    nums_movie = df['movieId'].count()
    return nums_movie

def howmany_type_movie():
    path = os.path.join(root_dir, 'movies.csv')
    df = pd.read_csv(path)
    genres_movie = df['genres'].str.split('|')
    nums_type = []
    for v in genres_movie:
        if '(' in v[0]:
            continue
        nums_type += v
    type_set = set(nums_type)
    return len(type_set)

def howmany_nolink():
    path = os.path.join(root_dir, 'links.csv')
    df = pd.read_csv(path)
    t0 = df['imdbId'].count()
    t1 = df['tmdbId'].count()
    return t0-t1

def howmany_rate_user():
    path = os.path.join(root_dir, 'ratings.csv')
    df = pd.read_csv(path)
    df['year'] = pd.to_datetime(df['timestamp'], unit='s').dt.year
    df_2018 = df[df['year']==2018]
    # print(df_2018)
    num = df_2018['year'].count()
    return num

if __name__ == '__main__':
    root_dir = r'E:\work\ZYBank\Study\prestudy\data\ml-25m'
    files = os.listdir(root_dir)
    # print(files)
    nums_user = howmany_user()
    nums_movie = howmany_movie()
    nums_movie_type = howmany_type_movie()
    nums_nolink = howmany_nolink()
    nums_2018 = howmany_rate_user()
    print("一共有{}不同的用户".format(nums_user))
    print("一共有{}不同的电影".format(nums_movie))
    print("一共有{}不同的电影种类".format(nums_movie_type))
    print("一共有{}电影没有外部链接".format(nums_nolink))
    print("2018年一共有{}人进行过电影评分".format(nums_2018))


import get_tip as gt
#Note: you could not import get_tip if it was not saved in the current working directory

def main():
    #Enter Bill Amount
    billAmount = input("Enter Total Bill Amount: ")
    # Enter Tip Amount
    tipPercentage = input("Enter the Tip Percentage in decimal form (20% = 0.2): ")

    #Grab the calculated Tip amount with our two parameters
    tip = gt.get_tip(billAmount, tipPercentage)

    #return print(get_tip.get_tip(billAmount, tipPercentage))
    return print ("A "+ str(float(tipPercentage)*100) + " Percent Tip is: " + str(tip) + " USD")

main()


import os
import random
from sh import git, ghp_import

import pyladies_cz

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
OUTPUT_DIR = os.path.join(ROOT_DIR, '_build')

COMMIT_EMOJIS = [
    ':sunglasses:', ':two_hearts:', ':sparkles:', ':star2:', ':star:',
    ':blue_heart: :yellow_heart:', ':raised_hands:', ':ok_hand:', ':ok_woman:',
    ':dancer:', ':raising_hand:', ':woman:', ':girl:', ':princess:', ':sunny:',
    ':cat:', ':koala:', ':snake:', ':paw_prints:', ':four_leaf_clover:',
    ':octocat:', ':gift_heart:', ':tada:', ':balloon:', ':cake:', ':rocket:',
]

def deploy():
    print('Generating HTML...')
    pyladies_cz.freeze_app(path=OUTPUT_DIR, base_url='http://pyladies.cz')

    if os.environ.get('TRAVIS'):  # Travis CI
        print('Setting up Git...')
        git.config(
            'user.name',
            git('show', format='%cN', s=True, _tty_out=False).strip()
        )
        git.config(
            'user.email',
            git('show', format='%cE', s=True, _tty_out=False).strip()
        )

        github_token = os.environ.get('GITHUB_TOKEN')  # encrypted in .travis.yml
        repo_slug = os.environ.get('TRAVIS_REPO_SLUG')
        origin = 'https://{}@github.com/{}.git'.format(github_token, repo_slug)
        git.remote('set-url', 'origin', origin)

    print('Rewriting gh-pages branch...')
    commit_message = 'Deploying {}'.format(random.choice(COMMIT_EMOJIS))
    ghp_import('-n', '-m', commit_message, OUTPUT_DIR)  # -n includes .nojekyll file

    print('Pushing to GitHub...')
    git.push('origin', 'gh-pages:gh-pages', force=True)

if __name__ == '__main__':
    deploy()


# coding=utf-8
# Das Modul mdservos beinhaltet die Funktionen, um die Servos (Ruder, Segel) anzusteuern.

import RPi.GPIO as GPIO
import math

GPIO.setmode(GPIO.BCM) # Belegung GPIO-Ports definieren
GPIO.setwarnings(False) # Sonst kommt Warnung "Channel already in use"

# Initialisierung Ruder-Servo
GPIO.setup(17, GPIO.OUT)
RuderServo = GPIO.PWM(17, 50) # Pin 17 mit 50 Hz
RuderServo.start(7.5) # gerades Ruder

# Initialisierung Segel-Servo
GPIO.setup(18, GPIO.OUT)
SegelServo = GPIO.PWM(18, 50) # Pin 18 mit 50 Hz
SegelServo.start(5) # Segel angelegt

# Funktion RuderWinkel() nimmt Winkelwert zw. -45 und 45 entgegen und
# stellt das Ruder entsprechend ein. Die Minuswerte sind links.
def RuderWinkel(Winkel):

    if not (Winkel <= 45 and Winkel >= -45):
        print("Funktion RuderWinkel hat Wert ausserhalb des gueltigen Bereichs erhalten.")
        return False

    Pulsweite = 5 + ((5 / 90) * (Winkel + 45))

    RuderServo.ChangeDutyCycle(Pulsweite)

    return True

# Funktion SegelWinkel() nimmt Winkelwert zw. 0 und 90 Grad entgegen und
# stellt das Segel entsprechend ein. Das Segel wird im Umfang des
# eingestellten Winkels von der Mitte abweichen; in welche Richtung
# entscheidet der Wind.
def SegelWinkel(Winkel):

    if not (Winkel <= 90 and Winkel >= 0):
        print("Funktion SegelWinkel hat Wert ausserhalb des gueltigen Bereichs erhalten.")
        return False

    if Winkel == 0:
        SegelServo.ChangeDutyCycle(5)
        return True

    Winkel_rad = math.radians(Winkel)

    Seillaenge = math.sqrt(281**2 + 310**2 + 110**2 - 2 * 281 * 310 * math.cos(Winkel_rad)) # in mm

    # umrechnen in Pulsweite (5% = 100mm / 8.4% = 436mm) ! 436mm waeren knapp ueber 90 Grad
    Pulsweite = 5 + (3.4 / (436 - 100)) * (Seillaenge - 100)

    SegelServo.ChangeDutyCycle(Pulsweite)

    return True


# coding: utf-8
import RPi.GPIO as GPIO
import time
from datetime import datetime
import argparse
import csv

def readadc(adcnum, clockpin, mosipin, misopin, cspin):
    if adcnum > 7 or adcnum < 0:
        return -1

    GPIO.output(cspin, GPIO.HIGH)
    GPIO.output(clockpin, GPIO.LOW)
    GPIO.output(cspin, GPIO.LOW)

    commandout = adcnum
    commandout |= 0x18
    commandout <<= 3
    for i in range(5):
        if commandout & 0x80:
                GPIO.output(mosipin, GPIO.HIGH)
        else:
                 GPIO.output(mosipin, GPIO.LOW)
        commandout <<= 1
        GPIO.output(clockpin, GPIO.HIGH)
        GPIO.output(clockpin, GPIO.LOW)
    adcout = 0
    for i in range(11):
        GPIO.output(clockpin, GPIO.HIGH)
        GPIO.output(clockpin, GPIO.LOW)
        adcout <<= 1
        if i>0 and GPIO.input(misopin)==GPIO.HIGH:
            adcout |= 0x1
    GPIO.output(cspin, GPIO.HIGH)
    return adcout

def init_adc(SPICLK, SPIMOSI, SPIMISO, SPICS, READPIN):
    GPIO.setup(SPICLK, GPIO.OUT)
    GPIO.setup(SPIMOSI, GPIO.OUT)
    GPIO.setup(SPIMISO, GPIO.IN)
    GPIO.setup(SPICS, GPIO.OUT)
    GPIO.setup(READPIN, GPIO.OUT)
    GPIO.output(READPIN, GPIO.HIGH)

def main(args):
    GPIO.setmode(GPIO.BCM)
    SPICS = 8
    SPIMISO = 9
    SPIMOSI = 10
    SPICLK = 11
    READPIN = 25
    channel = 0
    init_adc(SPICLK, SPIMOSI, SPIMISO, SPICS, READPIN)

    fname = args.name
    run_time = args.time
    sleep_time = 1 / args.hz

    dir_ = r'./data/raw/'
    f = open(dir_+args.name+'.csv', 'a', newline='')
    writer = csv.writer(f)
    try:
        t_s = time.time()
        t = 0
        while t < run_time:
            t = time.time() - t_s
            value = readadc(channel, SPICLK, SPIMOSI, SPIMISO, SPICS)
            writer.writerow([t, value])
            print('time: {:.2f}/{}\t\tvalue: {}'.format(t, run_time, value))
            time.sleep(sleep_time)

    except KeyboardInterrupt as e:
        print(e)

    finally:
        print('finish.')
        f.close()
        GPIO.cleanup()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--name',type=str, default=datetime.now().isoformat())
    parser.add_argument('-t', '--time', type=int, default=60)
    parser.add_argument('--hz', type=int, default=2)
    args = parser.parse_args()
    main(args)


import os, time

class TailFile(object):
  def __init__(self, filename):
    self.filename = filename

  def __call__(self, interval=1.0):
    with open(self.filename, 'r') as file:

      # Seek to end of file
      file.seek(os.stat(self.filename)[6])

      while True:
        where = file.tell()
        line = file.readline()
        if line:
          yield line
        else:
          time.sleep(interval)
          file.seek(where)


from keras.callbacks import Callback
from keras import backend as K

import random
import glob
import wandb
from wandb.keras import WandbCallback
from PIL import Image
import numpy as np

#initialize wandb

hyperparams = {"num_epochs": 10,
          "batch_size": 32,
          "height": 96,
          "width": 96}

wandb.init(config=hyperparams)
config = wandb.config

val_dir = '/home/thinhluong/Desktop/VideoPredict/catz/test'
train_dir = '/home/thinhluong/Desktop/VideoPredict/catz/train'

# generator to loop over train and test images

def my_generator(batch_size, img_dir):
    """A generator that returns 5 images plus a result image"""
    cat_dirs = glob.glob(img_dir + "/*")
    counter = 0
    while True:
        input_images = np.zeros(
            (batch_size, config.height, config.width, 3 * 5))
        output_images = np.zeros((batch_size, config.height, config.width, 3))
        random.shuffle(cat_dirs)
        if (counter+batch_size >= len(cat_dirs)):
            counter = 0
        for i in range(batch_size):
            input_imgs = glob.glob(cat_dirs[counter + i] + "/cat_[0-4]*")
            imgs = [Image.open(img) for img in sorted(input_imgs)]
            input_images[i] = np.concatenate(imgs, axis=2)
            output_img = glob.glob(cat_dirs[counter +i] + "/cat_result.jpg")
            output_images[i] = np.array(Image.open(output_img[0]))
            input_images[i] /= 255.
            output_images[i] /= 255.
        yield (input_images, output_images)
        counter += batch_size

#callback to log the images

class ImageCallback(Callback):
    def on_epoch_end(self, epoch, logs):
        validation_X, validation_y = next(
            my_generator(15, val_dir))
        output = self.model.predict(validation_X)
        wandb.log({
            "input": [wandb.Image(np.concatenate(np.split(c, 5, axis=2), axis=1)) for c in validation_X],
            "output": [wandb.Image(np.concatenate([validation_y[i], o], axis=1)) for i, o in enumerate(output)]
        }, commit=False)

# Function for measuring how similar two images are

def perceptual_distance(y_true, y_pred):
    y_true *= 255.
    y_pred *= 255.
    rmean = (y_true[:, :, :, 0] + y_pred[:, :, :, 0]) / 2
    r = y_true[:, :, :, 0] - y_pred[:, :, :, 0]
    g = y_true[:, :, :, 1] - y_pred[:, :, :, 1]
    b = y_true[:, :, :, 2] - y_pred[:, :, :, 2]
    return K.mean(K.sqrt((((512+rmean)*r*r)/256) + 4*g*g + (((767-rmean)*b*b)/256)))

# GRU

from keras.layers import Lambda, Reshape, Permute, Input, add, Conv2D, TimeDistributed, Conv2DTranspose, MaxPooling2D, ConvLSTM2D
from keras.models import Model

inp          = Input((config.height, config.width, 5 * 3))
reshaped     = Reshape((config.height,config.width, 5, 3))(inp)
permuted     = Permute((1,2,4,3))(reshaped)
permuted_2   = Permute((4,1,2,3))(permuted)

encode_conv1 = TimeDistributed(Conv2D(4, (3,3), padding="same", activation="relu"))(permuted_2)
encode_conv2 = TimeDistributed(Conv2D(4, (3,3), padding="same", activation="relu"))(encode_conv1)
max1         = TimeDistributed(MaxPooling2D((2,2), strides= (2,2)))(encode_conv2)

encode_conv3 = TimeDistributed(Conv2D(8, (3,3), padding="same", activation="relu"))(max1)
encode_conv4 = TimeDistributed(Conv2D(8, (3,3), padding="same", activation="relu"))(encode_conv3)
max2         = TimeDistributed(MaxPooling2D((2,2), strides= (2,2)))(encode_conv4)

convLSTM2D   = ConvLSTM2D(8, (3,3), padding="same")(max2)

decode_conv2 = Conv2DTranspose(8, (3,3), strides= (2,2), padding="same", activation="relu")(convLSTM2D)
decode_conv3 = Conv2DTranspose(4, (3,3), strides= (2,2), padding="same", activation="relu")(decode_conv2)

otp= Conv2D(3, (3,3), padding="same", activation="relu")(decode_conv3)

model=Model(inputs=[inp], outputs=[otp])

model.compile(optimizer='adam', loss='mse', metrics=[perceptual_distance])

model.fit_generator(my_generator(config.batch_size, train_dir),
                    steps_per_epoch=len(glob.glob(train_dir + "/*")) // config.batch_size,
                    epochs=config.num_epochs, callbacks=[
    ImageCallback(), WandbCallback()],
    validation_steps=len(glob.glob(val_dir + "/*")) // config.batch_size,
    validation_data=my_generator(config.batch_size, val_dir))


#!/usr/bin/env python3
# HW05_ex00_logics.py

##############################################################################
def even_odd():
    """ Print even or odd:
        Takes one integer from user
            accepts only non-word numerals
            must validate
        Determines if even or odd
        Prints determination
        returns None
    """
    user_number = ''
    user_number = input('What is your number?')
    while True:
        try:
            while user_number != '':
                user_number = int(user_number)
                if user_number%2 == 0:
                    print("Your number is even")
                    return
                else:
                    print("Your number is odd")
                    return
            break
        except:
            print("Only use integers!")
            even_odd()
            return

def ten_by_ten():
    """ Prints integers 1 through 100 sequentially in a ten by ten grid."""

    line_start = int(1)
    line_stop = int(11)
    while line_stop <= 101:
        for numbers in range (line_start, line_stop):
            print ('{:3}'.format(numbers), end=' ') #found this {:3}.format on SO, and don't completely understand it. I assume it tells the number to print on the 3rd position on the line.
        print("\n")
        line_start += 10
        line_stop += 10
    return

    # I just want to point out all the crazy stuff
    # I tried before I figured this one out. At least
    #  I think I figured it out.

    # count = ''
    # number = 1
    # while number <= 10:
    #    print(number, " ")
    #    number += 1
    # # for x in range (1, 101):
    # #     print (x+0, end=' ')
    # # print()

    # for numbers in range (11, 21):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (21, 31):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (31, 41):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (41, 51):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (51, 61):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (61, 71):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (71, 81):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (91, 101):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")

    # for i in     for numbers in range (1, 11):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")
    # for numbers in range (11, 21):
    #     print ('{:3}'.format(numbers), end=' ')
    # print("\n")range(11, 101):
    # #     print(i, end = ' ')
    #     # if i == len(range(1,11)):print('{:3}'.format(i), end=' ')

    #     if i == len(range(1,21)):print()
    #     if i == len(range(1,31)):print()
    #     if i == len(range(1,41)):print()
    #     if i == len(range(1,51)):print()
    #     if i == len(range(1,61)):print()
    #     if i == len(range(1,71)):print()
    #     if i == len(range(1,81)):print()
    #     if i == len(range(1,91)):print()

from statistics import mean

def find_average():
    """ Takes numeric input (non-word numerals) from the user, one number
    at a time. Once the user types 'done', returns the average.
    """
    numeric_input = ''
    counter = 1
    found_average = ''
    input_sum = ''
    inputted_numbers = []

    while True:
        try:
            while numeric_input != 'done':
                numeric_input = input("What is your input?")
                inputted_numbers.append(int(numeric_input))
                if numeric_input == 'done':
                    print("You're done!")
        except:
             print('Only use numeric values!')

        print("We found the average:", mean(inputted_numbers))
        break

    return
##############################################################################
def main():
    even_odd()
    ten_by_ten()
    find_average()

if __name__ == '__main__':
    main()


import turtle
import math

wn = turtle.Screen()
wn.bgcolor("black")

sun =turtle.Turtle()
sun.color("yellow")
sun.shape("blank")
sun.speed(0)
sun.screen.delay(0)
sun.up()
sun.backward(10)
sun.left(90)
sun.backward(5)
sun.left(-90)
sun.down()
sun.begin_fill()

for i in range(20):
    sun.forward(20)
    sun.left(126)

sun.end_fill()

def away(t,r):
    t.up()
    t.left(90)
    t.forward(r)
    t.left(-90)
    t.down()

mer = turtle.Turtle()
mer.color("cyan")
mer.shape("circle")
mer.shapesize(0.122)
away(mer,13)

ven = turtle.Turtle()
ven.color("gold")
ven.shape("circle")
ven.shapesize(0.6052)
away(ven,24)

ear = turtle.Turtle()
ear.color("turquoise")
ear.shape("circle")
ear.shapesize(0.6378)
away(ear,33)

mar = turtle.Turtle()
mar.color("salmon")
mar.shape("circle")
mar.shapesize(0.1699)
away(mar,50)

jup = turtle.Turtle()
jup.color("lightgreen")
jup.shape("circle")
jup.shapesize(0.71492)
away(jup,170)

sat = turtle.Turtle()
sat.color("tan")
sat.shape("circle")
sat.shapesize(0.60268)
away(sat,318)

def dc(t,r,n):
    l=n*r*math.pi/1800
    t.forward(l)
    t.left(-0.1*n)

for t in [mer,ven,ear,mar,jup,sat]:
    t.screen.delay(0)
    t.speed(10)

for i in range(10000000000000000000000000):
    dc(mer,13,10)
    dc(ven,24,5)
    dc(ear,33,4)
    dc(mar,50,2)
    dc(jup,170,1)
    dc(sat,318,0.4)

wn.exitonclick()


from operator import itemgetter
import sys
from collections import OrderedDict

current_word = None
current_count = 0
word = None
dic = {}
for line in sys.stdin:
    line = line.strip()
    word, count = line.split('\t', 1)
    try:
        count = int(count)
    except ValueError:
        continue

    if current_word == word:
        current_count += count
    else:
        if current_word:
            dic[current_word] = current_count
        current_count = count
        current_word = word

if current_word == word:
    dic[current_word] = current_count

sorted_dic = OrderedDict(sorted(dic.items(), key=itemgetter(1), reverse=True))

count = 0
for key in sorted_dic.keys():
    if count < 10:
        print ('%s\t%s' % (key, sorted_dic[key]))
        count+=1
    else:
        break


#! python3
# A program to store information like in a database

n1 = ['Andile Mbele', 20, 'Systems Developer', 30000]
n2 = ['Jaden Scott', 21, 'Network Administrator', 28000]

people = [n1, n2]

for data in people:
    print(data)

for payIncr in people:
    print(payIncr[0].split()[-1])
    payIncr[3] *= 1.20

    for payIncr in people:
        print(payIncr[3])


import random
import math
import os

def get_mask_word(mot_complet, lettres_trouvees):
    mot_masque = ""
    for lettre in mot_complet:
        if lettre in lettres_trouvees:
            mot_masque += lettre
        else:
            mot_masque += "*"
        mot_masque += " "

    return mot_masque

replay = 'y'
score = 0;

print("\n------------ Bienvenue dans le jeu du Pendu !!! ------------\n")

while replay == 'y':

    trouve = []
    coup = 10

    mode = input("------------ Choissisez votre mode de jeu : (1: un joueur * 2: deux joueurs) : ")

    # Verify if mode is numeric and equal to 1 or 2
    while ( not mode.isnumeric() or (int (mode) != 1 and int (mode) != 2) ):
        print("Choix incorrect")
        mode = input(" ------------ Choissisez votre mode de jeu : (1: un joueur * 2: deux joueurs) : ")

    mode = int(mode)

    if mode == 1:
        # Choice the difficulty Choix de la difficulté
        level = input("\n------------ Choissisez votre difficulté (1:facile * 2:moyen * 3:difficile) : ")

        # Verify if level is numeric and equal to 1 or 2 or 3
        while (not level.isnumeric() or (int(level) != 1 and int(level) != 2 and int(level) != 3)):
            print("Choix incorrect")
            level = input("------------ Choissisez votre difficulté (1:facile * 2:moyen * 3:difficile) : ")

        level = int(level)

        if level == 1:
            file = "easy.txt"
        elif level == 2:
            file = "medium.txt"
        else:
            file = "hard.txt"

        # Choice a random word in the file compared the difficulty
        words = open(file).read().splitlines()
        secret_word = random.choice(words)
    else :
        secret_word = input("------------ Taper le mot secret à deviner : ")

        # Verify if secret_word is alpanumerique and betwenn 3 and 15 characters
        while (not secret_word.isalpha() or len(secret_word) < 3 or len(secret_word) > 15):
            print("Mot incorrect (le mot doit comporté des lettres et être compris entre 3 et 15 caractéres")
            secret_word = input("------------ Taper le mot secret à deviner : ")

        # If mode is two player the level is calculate with lenght of secret xord
        level = math.floor(len(secret_word) / 3)

        if (level > 3):
            level = 3

        # level 1 => 3 , 4 , 5
        # level 2 => 6 , 7 , 8
        # level 3 => 9 , 10 , 11 , 12 , 13 , 14 , 15

        secret_word = secret_word.upper()

    # Show the secret word
    #print(secret_word)

    # Begin the game
    print("\n\n------------ Aller vous trouver le mot secret ... ??? ------------\n")
    print(get_mask_word(secret_word,trouve))

    # While the word is not found and the number of try is upper 0
    while get_mask_word(secret_word,trouve).replace(' ' , '') != secret_word and coup > 0:

        print("Il vous reste" , coup , "coups")
        print("Score :", score)

        user_letter = input("\n------------ TAPER UNE LETTRE : ")

        # Verify if seizure is a letter
        while (len(user_letter) > 1 or not user_letter.isalpha()):
            print("Vous n'avez pas saisie une lettre valide.")
            user_letter = input("------------ TAPER UNE LETTRE : ")

        while (user_letter.upper() in trouve):
            print("Vous avez déjà saisie cette lettre.")
            user_letter = input("------------ TAPER UNE LETTRE : ")

        if user_letter.upper() in secret_word:
            print("\n### Trouve ( +" , level , "points) !! ###\n")
            trouve.append(user_letter.upper())
            score = score + level
        else:
            print("\n### Pas trouvé ( -" , 4-level , "points) !! ###\n")
            coup -= 1
            score = score - (4 - level)

        print(get_mask_word(secret_word,trouve))

        #print("user :" , get_mask_word(secret_word,trouve).replace(' ' , '') , "secret:" , secret_word)

    # End of the party and verify if user is find the world or no

    print("\n------------------------------------------------------------------------\n\n")

    if get_mask_word(secret_word,trouve).replace(' ' , '') == secret_word:
        print("GAGNE LE MOT ETAIT :" ,secret_word , " ( + " , (2 * level) , "points )")
        score = score + (2 * level)
        print("\nScore :", score)
    else:
        print("PERDU LE MOT ETAIT :", secret_word, "( - ", (2 * (4-level)), "points )")
        score = score - (2 * (4-level))
        print("\nScore :", score)

    # Ask player if he want replay

    replay = input("\n------------ VOULEZ VOUS REJOUER ? (y/n) : ")

    while (not replay.isalpha() or (replay != 'y' and replay != 'n')):
        print("Choix incorrect")
        replay = input("------------ VOULEZ VOUS REJOUER ? (y/n) : ")

# End of the game

print("\n------------------------------------------------------------------------\n\n")

print("------------ Votre score final est :" , score , "------------")

print("\n------------ Merci d'avoir jouer a bientot !!! ------------")


## Date: 04.09.2016
## Author: Anna Majewski
## Description: Ratespiel so umschreiben, damit es eine Funktion ist.

# Zuerst Ratespiel hinüberkopieren.
# Ich könnte import nutzen, ABER ich will aus der anderen Datei
# nicht alle prints entfernen, damit sie ausführbar bleibt.

from random import randint
# Importieren von randint aus random

def guess_game(min, max, guesses):
    """number guess game in range from 'min' to 'max', with 'guesses' guesses"""
# min und max bestimmen die Range der zufälligen Zahl
# guesses bestimmt die Anzahl der Versuche.
    zahl = randint(min,max)
    # Zufallszahl wird generiert
    anzahl = guesses
    # Anzahl der Würfe

    print("Willkommen beim Ratespiel. Erraten Sie eine Zahl zwischen", min, "und", max, "!")

    # TIPP: Hier können Sie den 'else'-Zweig der for Schleife verwenden, um noch eine game_over
    # Nachricht zu schicken
    # else:
    #   print("Du hast leider verloren.")

    # TIPP: enumerate können Sie sich hier sparen, da index hier
    # gleich x ist
    # for index range(anzahl):
    for index, x in enumerate(range(anzahl)):
        eingabe = input("Geben Sie Ihre Zahl ein:")
    # Eingabe wird direkt in einer Variable gespeichert.
        eingabe = int(eingabe)
    # Die Zahl wird von einem String zu einem Integer umgewandelt.
        if eingabe == zahl:
            print("SUPER! Sie haben die Zahl erraten!")
            # TIPP: Anstatt nur die Schleife zu verlassen, können Sie mit return
            # auch gleich die Funktion verlassen und auch gleich einen Rückgabewert
            # mitgeben. Z.B. bei Erfolg die erratete Zahl und bei nicht Erfolg None
            # zurückgeben. Somit können Sie den Rückgabewert der Funktion in andern
            # Funktionen mitverabrbeiten, wenn gewünscht.
            #    return zahl
            break
        elif eingabe < zahl:
            print("Zu niedrig. Die Zahl ist größer!", "Sie haben noch", anzahl-1-index, "Versuche!")
        elif eingabe > zahl:
            print("Zu hoch. Die Zahl ist kleiner!", "Sie haben noch", anzahl-1-index, "Versuche!")

guess_game(1, 8, 2)


"""Main application"""
from .model_trainers import ModelTrainer
from .predictors import Predictor
from .evaluation import ModelEvaluator
from .individual_importance import IndividualImportanceCalculator
from .model_grouping import ModelGrouper
from .subsetters import Subsetter
from .utils import filename_friendly_hash

import logging

import numpy

class ModelTrainTester(object):
    def __init__(
        self,
        matrix_storage_engine,
        model_trainer,
        model_evaluator,
        individual_importance_calculator,
        predictor,
        subsets,
    ):
        self.matrix_storage_engine = matrix_storage_engine
        self.model_trainer = model_trainer
        self.model_evaluator = model_evaluator
        self.individual_importance_calculator = individual_importance_calculator
        self.predictor = predictor
        self.subsets = subsets

    def generate_tasks(self, split, grid_config, model_comment=None):
        logging.info("Generating train/test tasks for split %s", split["train_uuid"])
        train_store = self.matrix_storage_engine.get_store(split["train_uuid"])
        if train_store.empty:
            logging.warning(
                """Train matrix for split %s was empty,
            no point in training this model. Skipping
            """,
                split["train_uuid"],
            )
            return []
        if len(train_store.labels.unique()) == 1:
            logging.warning(
                """Train Matrix for split %s had only one
            unique value, no point in training this model. Skipping
            """,
                split["train_uuid"],
            )
            return []
        train_tasks = self.model_trainer.generate_train_tasks(
            grid_config=grid_config,
            misc_db_parameters=dict(test=False, model_comment=model_comment),
            matrix_store=train_store
        )

        train_test_tasks = []
        for test_matrix_def, test_uuid in zip(
            split["test_matrices"], split["test_uuids"]
        ):
            test_store = self.matrix_storage_engine.get_store(test_uuid)

            if test_store.empty:
                logging.warning(
                    """Test matrix for uuid %s
                was empty, no point in generating predictions. Not creating train/test task.
                """,
                    test_uuid,
                )
                continue
            for train_task in train_tasks:
                train_test_tasks.append(
                    {
                        "test_store": test_store,
                        "train_store": train_store,
                        "train_kwargs": train_task,
                    }
                )
        return train_test_tasks

    def process_all_tasks(self, tasks):
        for task in tasks:
            self.process_task(**task)

    def process_task(self, test_store, train_store, train_kwargs):
        logging.info("Beginning train task %s", train_kwargs)
        with self.model_trainer.cache_models(), test_store.cache(), train_store.cache():
            # will cache any trained models until it goes out of scope (at the end of the task)
            # this way we avoid loading the model pickle again for predictions
            model_id = self.model_trainer.process_train_task(**train_kwargs)
            if not model_id:
                logging.warning("No model id returned from ModelTrainer.process_train_task, "
                                "training unsuccessful. Not attempting to test")
                return
            logging.info("Trained task %s and got model id %s", train_kwargs, model_id)
            as_of_times = test_store.metadata["as_of_times"]
            logging.info(
                "Testing and scoring model id %s with test matrix %s. "
                "as_of_times min: %s max: %s num: %s",
                model_id,
                test_store.uuid,
                min(as_of_times),
                max(as_of_times),
                len(as_of_times),
            )

            self.individual_importance_calculator.calculate_and_save_all_methods_and_dates(
                model_id, test_store
            )

            # Generate predictions for the testing data then training data
            for store in (test_store, train_store):
                predictions_proba = numpy.array(None)
                if self.predictor.replace:
                    logging.info(
                        "Replace flag set; generating new predictions and evaluations for"
                        "matrix %s-%s, and model %s",
                        store.uuid,
                        store.matrix_type,
                        model_id
                    )

                    predictions_proba = self.predictor.predict(
                        model_id,
                        store,
                        misc_db_parameters=dict(),
                        train_matrix_columns=train_store.columns(),
                    )

                for subset in self.subsets:
                    if self.predictor.replace or self.model_evaluator.needs_evaluations(
                        store, model_id, filename_friendly_hash(subset)
                    ):
                        logging.info(
                            "Evaluating matrix %s-%s, subset %s, and model %s",
                            store.uuid,
                            store.matrix_type,
                            filename_friendly_hash(subset),
                            model_id,
                        )

                        if not predictions_proba.any():
                            logging.info(
                                "Generating new predictions for"
                                "matrix %s-%s, and model %s to make evaluation",
                                store.uuid,
                                store.matrix_type,
                                model_id
                            )

                            predictions_proba = self.predictor.predict(
                                model_id,
                                store,
                                misc_db_parameters=dict(),
                                train_matrix_columns=train_store.columns(),
                            )

                        self.model_evaluator.evaluate(
                            predictions_proba=predictions_proba,
                            matrix_store=store,
                            model_id=model_id,
                            subset=subset,
                        )

                    else:
                        logging.info(
                            "The evaluations needed for matrix %s-%s, subset %s, and "
                            "model %s are all present"
                            "in db from a previous run (or none needed at all), so skipping!",
                            store.uuid,
                            store.matrix_type,
                            filename_friendly_hash(subset),
                            model_id
                        )

__all__ = (
    "IndividualImportanceCalculator",
    "ModelEvaluator",
    "ModelGrouper"
    "ModelTrainer",
    "Predictor",
    "ModelTrainTester",
    "Subsetter",
)


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

from matplotlib import pyplot as plt
import numpy as np

def cate_num_distribution(target):
    """
    展示各标签样本数量
    :param target: 标签列表，每个元素是一个字符串形式，需要转成数字
    :return:
    """

    # 字符串要转成数字
    target = [int(cate) for cate in target]
    cates = set(target)
    # 生成类别字典，值为文本数量
    cate_num_dict = {}
    for i in cates:
        cate_num_dict[i] = 0

    length = len(target)

    for i in range(0, length):
        cur_cate = target[i]
        cate_num_dict[cur_cate] += 1

    # 统计每个类别的漏洞数量，并用柱状图表示

    keys = cate_num_dict.keys()
    values = cate_num_dict.values()

    for key in keys:
        print('cate = {}, num = {}'.format(key, cate_num_dict[key]))

    print("number of cates is {}".format(keys.__len__()))

    X = np.arange(keys.__len__())
    Y = np.array(values)

    plt.bar(X, Y, width=0.35, facecolor='lightskyblue', edgecolor='white')

    for x, y in zip(X, Y):
        plt.text(x, y, '(%d, %d)' % (keys[x], y), ha='center', va='bottom')

    plt.show()


# Массив размером 2m + 1, где m — натуральное число, заполнен случайным образом. Найдите в массиве медиану.
# Медианой называется элемент ряда,
# делящий его на две равные части: в одной находятся элементы,
# которые не меньше медианы, в другой — не больше медианы.

import random

m = int(input('Введите натуральное число: '))

MINVAL = 0
MAXVAL = 100
LONG = 2*m + 1

rand_arr = [random.randint(MINVAL, MAXVAL) for _ in range(LONG)]
print(f' Массив случайных чисел от {MINVAL} до {MAXVAL} '.center(len(rand_arr)*4, "*"))
print(rand_arr)

new_arr = []
while len(new_arr) < LONG/2:
    val = min(rand_arr)
    rand_arr.remove(val)
    new_arr.append(val)

print(f'Медиана: {new_arr.pop()}')


import File_Reader
import Notifaction
import webbrowser
import time
import os
import time
import string
from types import *
import Command_Line_Functions
import Loading_Bar
import sys

import Prompts
get_prompt = Prompts.Prompting()
move_speed = get_prompt.get_speed()

Command_Line_Functions.confirmMirror()
Command_Line_Functions.confirmXcode()

import Map_Plotter

displaycoords = Map_Plotter.Display()
bot = File_Reader.Teleport()
notify = Notifaction.DoNotifaction()
displaycoords.openChooseLocation()
Command_Line_Functions.chromeFront()

ChoosenLocation = False
while not ChoosenLocation:
    try:
        displaycoords.getLat_Test()
        break
    except:
        ChoosenLocation = False
    time.sleep(.01)

ChoosenLocation = True
time.sleep(1)

SpawnX = displaycoords.getLat(); SpawnY = displaycoords.getLong()

Lati = SpawnX; Longi = SpawnY

bot.TeleportTo(Lati, Longi)

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (1100,200)
import pygame
pygame.init()
pygame.font.init()

SCREEN_SIZE = (200, 450)
display = pygame.display.set_mode(SCREEN_SIZE)
myfont = pygame.font.SysFont('Comic Sans MS', 20); infofont = pygame.font.SysFont('Comic Sans MS', 15)
pygame.display.set_caption("Character Controller", "Character Controller")
clock = pygame.time.Clock()
pokeball = pygame.image.load('Pokeball_200.png')

Command_Line_Functions.show_loading_info()

from selenium import webdriver

#browser = webdriver.Chrome()
#browser.set_window_position(700, 0)
#browser.set_window_size(600, 1000)
#browser.get('https://thesilphroad.com/atlas#4.55/{}/{}"'.format(str(Lati), str(Longi)))

displaycoords.DisplayCoords(str(Lati),str(Longi))

while True:

    display.fill((255, 255, 255)) #white

    Controls = infofont.render('Arrow Keys (Move) - W&S (Speed) - K(Kill)', False, (0, 0, 0))
    Latitude_Text = myfont.render('Latitude: ' + str(Lati), False, (0,0,0))
    Longitude_Text = myfont.render('Longitude: ' + str(Longi), False, (0,0,0))
    Python_Version = myfont.render('Running Python: ' + str(sys.version.split(' ')[0]), False, (0,0,0))

    display.blit(Controls, (0, 1))
    display.blit(Latitude_Text, (20, 50))
    display.blit(Longitude_Text, (20, 100))
    display.blit(Python_Version, (20, 150))
    display.blit(pokeball, (0, 200))

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            notify.notify("Quitting App", "")
            pygame.quit();

        if event.type == pygame.KEYDOWN:
            Command_Line_Functions.update()

            if event.key == pygame.K_LEFT:
                print("Moving Left")
                Longi -= move_speed
                bot.TeleportTo(format(Lati, '.6f'), format(Longi, '.6f'))

            if event.key == pygame.K_RIGHT:
                print("Moving Right")
                Longi += move_speed
                bot.TeleportTo(format(Lati, '.6f'), format(Longi, '.6f'))

            if event.key == pygame.K_UP:
                print("Moving Forward")
                Lati += move_speed
                bot.TeleportTo(format(Lati, '.6f'), format(Longi, '.6f'))

            if event.key == pygame.K_DOWN:
                print("Moving Down")
                Lati -= move_speed
                bot.TeleportTo(format(Lati, '.6f'), format(Longi, '.6f'))

            if event.key == pygame.K_w:
                Command_Line_Functions.say("Speed Increase")
                move_speed += 0.0001
                notify.notify("Speed Increased by: " + "+0.0001 |" + " Speed: " + str(move_speed), "Easy Walk")

            if event.key == pygame.K_s:
                Command_Line_Functions.say("Speed Decrease")
                move_speed -= 0.0001
                notify.notify("Speed Decreased by: " + "-0.0001 |" + " Speed: " + str(move_speed), "Easy Walk")

            if event.key == pygame.K_k:
                notify.notify("Quitting Processes", "Easy Walk")
                os.system("pkill -f 'osascript'")

            if event.key == pygame.K_t:

                from pymsgbox import *
                from pygame.locals import *
                lat_extract = prompt(text='Enter Latitude to Teleport to', title='Info')
                long_extract = prompt(text='Enter Longitude to Teleport to', title='Info')
                Longi = format(float(long_extract), '.6f')
                Lati = format(float(lat_extract), '.6f')
                bot.TeleportTo(format(Lati, '.6f'), format(Longi, '.6f'))

    clock.tick(3)


import os
import shutil
import time

try:
	i = 0
	wd = 'C:\\Users\\YOURUSERNAME\\Documents\\My Games\\Fallout4\\Saves'
	dst_dir = 'C:\\Users\\YOURUSERNAME\\Desktop\\survial_saves'
	while True:
		for files in os.listdir(wd):
			src = os.path.join(wd, files)
			dst = os.path.join(dst_dir, files)
			if os.path.exists(dst):
				time.sleep(10)
			# 其实这个时间可以随意一些，例如“1”，例如“60”，例如“300”，因为脚本的目的是备份，而根据操作逻辑：在游戏中新建存档后，并不会立即删除它，删除它的时候，应该是下一次存档之前，期间的时间差，即为此SLEEP值
			# 好吧，其实我担心的是频繁的读目录结构会对磁盘造成影响（maybe?），但是我不明白windows对此是否有优化，或者python的os模块对此有无优化，但是为免此纠结，可是使用win32files等API进行文件夹监控。
			else:
				if os.path.splitext(files)[1] == '.fos':
					shutil.copy(src, dst)
					i += 1
					print("%d files backup done @ " % i)
except FileNotFoundError as e:
	print(e)

# README
# 生存模式下的FALLOUT4，不能快速存档，不能手动存档，必须找到一张用于睡眠的床才可以激活自动存档功能，但是在快节奏的战斗期间非常影响游戏体验，这种操作个人感觉纯属无哩头，
	# 但是好在可以通过一种折衷的方式解决，即在“载入存档”菜单中删除该角色名下所有其它存档后即时退入上一层菜单存档功能即可激活，但是注意此时不能再往上退了，否则会导致彻底无法存档且没有了原始存档，
	# 如果这时候挂掉那就真的彻底没有存档了，所以萌生了用脚本自动处理的想法
	# 此外，可以考虑用win32api来处理，应该更加科学，回头试试。


#!/usr/bin/python
# coding:utf8

import os.path

from wsgiauth.basic import BasicAuth

from vilya.config import DEVELOP_MODE
from vilya.libs.git_http_backend import assemble_WSGI_git_app
from vilya.libs.permdir import get_repo_root
from vilya.models.user import User
from vilya.models.project import Project
from vilya.models.organization import Organization

DOUBAN_REALM = "douban wsgi basic auth"

def get_proj(path):
    '''
    >>> get_proj('/a.git/info/refs')
    'a'
    >>> get_proj('/testuser/a.git/info/refs')
    'testuser/a'
    '''
    path_split = path.split("/")
    git_name = path_split[1]
    if not git_name.endswith('.git'):
        git_name = "/".join(path_split[1:3])
    assert "." in git_name, "Need a string looking like project.git, got '%s'" % git_name
    proj_name, _ = os.path.splitext(git_name)
    return proj_name

def get_git_path_info(path):
    path_split = path.split("/")
    git_name = path_split[1]
    # raw path: project_id.git
    if git_name.endswith('.git'):
        project = Project.get_by_name(git_name[:-4])
        if project:
            path_split[1] = "%s.git" % project.id
            return '/'.join(path_split)
    else:
        owner_name, git_name = path_split[1:3]
        # user project: user/project.git
        user = User.get_by_name(owner_name)
        if user:
            project = Project.get_by_name_and_owner(git_name[:-4], user.id)
            if project:
                path_split[1] = ""
                path_split[2] = "%s.git" % project.id
                return '/'.join(path_split[1:])
            return
        # org project: org/project.git
        org = Organization.get_by_name(owner_name)
        if org:
            project = Project.get_by_name_and_owner(git_name[:-4], user.id)
            if project:
                path_split[1] = ""
                path_split[2] = "%s.git" % project.id
                return '/'.join(path_split[1:])

def authfunc(environ, username, passwd):
    # no auth
    return True

    if DEVELOP_MODE or (environ['REMOTE_ADDR'] == '127.0.0.1'
                        and environ['HTTP_HOST'] == 'localhost:8080'):
        return True
    if not passwd:
        return
    if username == 'code' and passwd == 'code':
        return True
    # FIXME: login
    is_good = True
    if not is_good:
        return
    is_push = 'service=git-receive-pack' in environ['QUERY_STRING'] or '/git-receive-pack' in environ['PATH_INFO']
    if is_push:
        pass
        # FIXME: push permission
    return True

class HTTPAuth(object):
    def __init__(self, application, realm, authfunc, scheme, **kw):
        self.application = application
        self.authenticate = scheme(realm, authfunc, **kw)
        self.scheme = scheme.authtype

    def __call__(self, environ, start_response):
        path_info = environ['PATH_INFO']
        environ['GIT_PATH_INFO'] = get_git_path_info(path_info)
        if environ.get('REMOTE_USER') is None:
            result = self.authenticate(environ)
            if not isinstance(result, str):
                # Request credentials if authentication fails
                return result(environ, start_response)
            environ['REMOTE_USER'] = result
            environ['AUTH_TYPE'] = self.scheme
        return self.application(environ, start_response)

class WebRedirect(object):
    def __init__(self, application, **kw):
        self.application = application

    def __call__(self, environ, start_response):
        http_accept = environ.get('HTTP_ACCEPT')
        path_info = environ.get('PATH_INFO')
        proj_name = get_proj(path_info)
        if http_accept and 'text/html' in http_accept:
            start_response('301 Redirect',
                           [('Location', '/%s/' % proj_name), ])
            return []
        return self.application(environ, start_response)

app = assemble_WSGI_git_app(content_path=get_repo_root())
app = WebRedirect(app)
app = HTTPAuth(app, DOUBAN_REALM, authfunc, BasicAuth)