from __future__ import absolute_import

from .dataset_iter import default_collate, DatasetIter
from .samplers import RandomSampler, SequentialSampler

import torch

import os
import os.path
import warnings
import fnmatch
import math

import numpy as np
try:
    import nibabel
except:
    warnings.warn('Cant import nibabel.. Cant load brain images')

try:
    from PIL import Image
except:
    warnings.warn('Cant import PIL.. Cant load PIL images')

IMG_EXTENSIONS = [
    '.jpg', '.JPG', '.jpeg', '.JPEG',
    '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
    '.nii.gz', '.npy'
]

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

def find_classes(dir):
    classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
    classes.sort()
    class_to_idx = {classes[i]: i for i in range(len(classes))}
    return classes, class_to_idx

def pil_loader(path):
    return Image.open(path).convert('RGB')

def npy_loader(path):
    return torch.from_numpy(np.load(path).astype('float32'))

def nifti_loader(path):
    return nibabel.load(path)

def make_dataset(directory, class_mode, class_to_idx=None,
            input_regex=None, target_regex=None, ):
    """Map a dataset from a root folder"""
    if class_mode == 'image':
        if not input_regex and not target_regex:
            raise ValueError('must give input_regex and target_regex if'+
                ' class_mode==image')
    inputs = []
    targets = []
    for subdir in sorted(os.listdir(directory)):
        d = os.path.join(directory, subdir)
        if not os.path.isdir(d):
            continue

        for root, _, fnames in sorted(os.walk(d)):
            for fname in fnames:
                if fnmatch.fnmatch(fname, input_regex):
                    path = os.path.join(root, fname)
                    inputs.append(path)
                    if class_mode == 'label':
                        targets.append(class_to_idx[subdir])
                if class_mode == 'image' and fnmatch.fnmatch(fname, target_regex):
                    path = os.path.join(root, fname)
                    targets.append(path)
    if class_mode is None:
        return inputs
    else:
        return inputs, targets

class Dataset(object):
    """An abstract class representing a Dataset.

    All other datasets should subclass it. All subclasses should override
    ``__len__``, that provides the size of the dataset, and ``__getitem__``,
    supporting integer indexing in range from 0 to len(self) exclusive.
    """

    def __getitem__(self, index):
        raise NotImplementedError

    def __len__(self):
        raise NotImplementedError

    def one_epoch(self):
        """Return an iterator that will loop through all the samples one time"""
        return DatasetIter(self)

    def __iter__(self):
        """Return an iterator that will loop through all the samples one time"""
        return DatasetIter(self)

    def __next__(self):
        """Return the next batch in the data. If this batch is the last
        batch in the data, the iterator will be reset -- allowing you
        to loop through the data ad infinitum
        """
        new_batch = next(self._iter)
        self.batches_seen += 1
        if self.batches_seen % self.nb_batches == 0:
            #print('Last Batch of Current Epoch')
            self._iter = DatasetIter(self)
        return new_batch

    next = __next__

class FolderDataset(Dataset):

    def __init__(self,
                 root,
                 class_mode='label',
                 input_regex='*',
                 target_regex=None,
                 transform=None,
                 target_transform=None,
                 co_transform=None,
                 loader='npy',
                 batch_size=1,
                 shuffle=False,
                 sampler=None,
                 num_workers=0,
                 collate_fn=default_collate,
                 pin_memory=False):
        """Dataset class for loading out-of-memory data.

        Arguments
        ---------
        root : string
            path to main directory

        class_mode : string in `{'label', 'image'}`
            type of target sample to look for and return
            `label` = return class folder as target
            `image` = return another image as target as found by 'target_regex'
                NOTE: if class_mode == 'image', you must give an
                input and target regex and the input/target images should
                be in a folder together with no other images in that folder

        input_regex : string (default is any valid image file)
            regular expression to find input images
            e.g. if all your inputs have the word 'input',
            you'd enter something like input_regex='*input*'

        target_regex : string (default is Nothing)
            regular expression to find target images if class_mode == 'image'
            e.g. if all your targets have the word 'segment',
            you'd enter somthing like target_regex='*segment*'

        transform : torch transform
            transform to apply to input sample individually

        target_transform : torch transform
            transform to apply to target sample individually

        loader : string in `{'npy', 'pil', 'nifti'} or function
            defines how to load samples from file
            if a function is provided, it should take in a file path
            as input and return the loaded sample.

        Examples
        --------
        For loading input images and target images (e.g. image and its segmentation):
            >>> data = FolderDataset(root=/path/to/main/dir,
                    class_mode='image', input_regex='*input*',
                    target_regex='*segment*', loader='pil')

        For loading input images with sub-directory as class label:
            >>> data = FolderDataset(root=/path/to/main/dir,
                    class_mode='label', loader='pil')
        """
        if loader == 'npy':
            loader = npy_loader
        elif loader == 'pil':
            loader = pil_loader
        elif loader == 'nifti':
            loader = nifti_loader

        root = os.path.expanduser(root)

        classes, class_to_idx = find_classes(root)
        inputs, targets = make_dataset(root, class_mode,
            class_to_idx, input_regex, target_regex)

        if len(inputs) == 0:
            raise(RuntimeError("Found 0 images in subfolders of: " + root + "\n"
                    "Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))

        self.root = os.path.expanduser(root)
        self.inputs = inputs
        self.targets = targets
        self.classes = classes
        self.class_to_idx = class_to_idx
        self.transform = transform
        self.target_transform = target_transform
        self.co_transform = co_transform
        self.loader = loader
        self.class_mode = class_mode

        self.batch_size = batch_size
        self.num_workers = num_workers
        self.collate_fn = collate_fn
        self.pin_memory = pin_memory

        if sampler is not None:
            self.sampler = sampler
        elif shuffle:
            self.sampler = RandomSampler(nb_samples=len(self.inputs))
        elif not shuffle:
            self.sampler = SequentialSampler(nb_samples=len(self.inputs))

        if class_mode == 'image':
            print('Found %i input images and %i target images' %
                (len(self.inputs), len(self.targets)))
        elif class_mode == 'label':
            print('Found %i input images across %i classes' %
                (len(self.inputs), len(self.classes)))

        self.batches_seen = 0
        self.nb_batches = int(math.ceil(len(self.sampler) / float(self.batch_size)))
        self._iter = DatasetIter(self)

    def __getitem__(self, index):
        # get paths
        input_sample = self.inputs[index]
        target_sample = self.targets[index]

        # load samples into memory
        input_sample = self.loader(os.path.join(self.root, input_sample))
        if self.class_mode == 'image':
            target_sample = self.loader(os.path.join(self.root, target_sample))

        # apply transforms
        if self.transform is not None:
            input_sample = self.transform(input_sample)
        if self.target_transform is not None:
            target_sample = self.target_transform(target_sample)
        if self.co_transform is not None:
            input_sample, target_sample = self.co_transform(input_sample, target_sample)

        return input_sample, target_sample

    def __len__(self):
        return len(self.inputs)

class TensorDataset(Dataset):

    def __init__(self,
                 input_tensor,
                 target_tensor=None,
                 transform=None,
                 target_transform=None,
                 co_transform=None,
                 batch_size=1,
                 shuffle=False,
                 sampler=None,
                 num_workers=0,
                 collate_fn=default_collate,
                 pin_memory=False):
        """Dataset class for loading in-memory data.

        Arguments
        ---------
        input_tensor : torch tensor

        target_tensor : torch tensor

        transform : torch transform
            transform to apply to input sample individually

        target_transform : torch transform
            transform to apply to target sample individually

        loader : string in `{'npy', 'pil', 'nifti'} or function
            defines how to load samples from file
            if a function is provided, it should take in a file path
            as input and return the loaded sample.

        Examples
        --------
        For loading input images and target images (e.g. image and its segmentation):
            >>> data = FolderDataset(root=/path/to/main/dir,
                    class_mode='image', input_regex='*input*',
                    target_regex='*segment*', loader='pil')

        For loading input images with sub-directory as class label:
            >>> data = FolderDataset(root=/path/to/main/dir,
                    class_mode='label', loader='pil')
        """
        self.inputs = input_tensor
        self.targets = target_tensor
        if target_tensor is None:
            self.has_target = False
        else:
            self.has_target = True
        self.transform = transform
        self.target_transform = target_transform
        self.co_transform = co_transform

        self.batch_size = batch_size
        self.num_workers = num_workers
        self.collate_fn = collate_fn
        self.pin_memory = pin_memory

        if sampler is not None:
            self.sampler = sampler
        else:
            if shuffle:
                self.sampler = RandomSampler(nb_samples=len(self.inputs))
            elif not shuffle:
                self.sampler = SequentialSampler(nb_samples=len(self.inputs))

        self.batches_seen = 0
        self.nb_batches = int(math.ceil(len(self.sampler) / float(self.batch_size)))
        self._iter = DatasetIter(self)

    def __getitem__(self, index):
        """Return a (transformed) input and target sample from an integer index"""
        # get paths
        input_sample = self.inputs[index]
        if self.has_target:
            target_sample = self.targets[index]

        # apply transforms
        if self.transform is not None:
            input_sample = self.transform(input_sample)
        if self.has_target and self.target_transform is not None:
            target_sample = self.target_transform(target_sample)
        if self.has_target and self.co_transform is not None:
            input_sample, target_sample = self.co_transform(input_sample, target_sample)

        if self.has_target:
            return input_sample, target_sample
        else:
            return input_sample

    def __len__(self):
        """Number of samples"""
        return self.inputs.size(0)


#!/usr/bin/env python3

import fnmatch
import os
import re
import ntpath
import sys
import argparse

# handle x64 python clipboard, ref https://forums.autodesk.com/t5/maya-programming/ctypes-bug-cannot-copy-data-to-clipboard-via-python/m-p/9197068/highlight/true#M10992
import ctypes
from ctypes import wintypes
CF_UNICODETEXT = 13

user32 = ctypes.WinDLL('user32')
kernel32 = ctypes.WinDLL('kernel32')

OpenClipboard = user32.OpenClipboard
OpenClipboard.argtypes = wintypes.HWND,
OpenClipboard.restype = wintypes.BOOL
CloseClipboard = user32.CloseClipboard
CloseClipboard.restype = wintypes.BOOL
EmptyClipboard = user32.EmptyClipboard
EmptyClipboard.restype = wintypes.BOOL
GetClipboardData = user32.GetClipboardData
GetClipboardData.argtypes = wintypes.UINT,
GetClipboardData.restype = wintypes.HANDLE
SetClipboardData = user32.SetClipboardData
SetClipboardData.argtypes = (wintypes.UINT, wintypes.HANDLE)
SetClipboardData.restype = wintypes.HANDLE
GlobalLock = kernel32.GlobalLock
GlobalLock.argtypes = wintypes.HGLOBAL,
GlobalLock.restype = wintypes.LPVOID
GlobalUnlock = kernel32.GlobalUnlock
GlobalUnlock.argtypes = wintypes.HGLOBAL,
GlobalUnlock.restype = wintypes.BOOL
GlobalAlloc = kernel32.GlobalAlloc
GlobalAlloc.argtypes = (wintypes.UINT, ctypes.c_size_t)
GlobalAlloc.restype = wintypes.HGLOBAL
GlobalSize = kernel32.GlobalSize
GlobalSize.argtypes = wintypes.HGLOBAL,
GlobalSize.restype = ctypes.c_size_t

GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040

def Paste( data ):
    data = data.encode('utf-16le')
    OpenClipboard(None)
    EmptyClipboard()
    handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len(data) + 2)
    pcontents = GlobalLock(handle)
    ctypes.memmove(pcontents, data, len(data))
    GlobalUnlock(handle)
    SetClipboardData(CF_UNICODETEXT, handle)
    CloseClipboard()

def getFunctions(filepath):
    selfmodule = (re.search(r'addons[\W]*([_a-zA-Z0-9]*)', filepath)).group(1)
    # print("Checking {0} from {1}".format(filepath,selfmodule))
    if (selfmodule.startswith("compat")): return []

    with open(filepath, 'r') as file:
        content = file.read()

        srch = re.compile(r'[^E]FUNC\(([_a-zA-Z0-9]*)\)')
        modfuncs = srch.findall(content)
        modfuncs = sorted(set(modfuncs))

        srch = re.compile(r'EFUNC\(([_a-zA-Z0-9]*),([_a-zA-Z0-9]*)\)')
        exfuncs = srch.findall(content)
        exfuncs = sorted(set(exfuncs))

    fileFuncs = []
    for func in modfuncs:
        fileFuncs.append("ace_{0}_fnc_{1}".format(selfmodule,func))

    for exModule,func in exfuncs:
        fileFuncs.append("ace_{0}_fnc_{1}".format(exModule, func))

    return fileFuncs

def getStrings(filepath):
    selfmodule = (re.search(r'addons[\W]*([_a-zA-Z0-9]*)', filepath)).group(1)
    # print("Checking {0} from {1}".format(filepath,selfmodule))
    if (selfmodule.startswith("compat")): return []

    with open(filepath, 'r') as file:
        content = file.read()

        srch = re.compile(r'[^E][CL]STRING\(([_a-zA-Z0-9]*)\)')
        modStrings = srch.findall(content)
        modStrings = sorted(set(modStrings))

        srch = re.compile(r'E[CL]STRING\(([_a-zA-Z0-9]*),([_a-zA-Z0-9]*)\)')
        exStrings = srch.findall(content)
        exStrings = sorted(set(exStrings))

    fileStrings = []
    for localString in modStrings:
        fileStrings.append("STR_ACE_{0}_{1}".format(selfmodule, localString))

    for (exModule, exString) in exStrings:
        fileStrings.append("STR_ACE_{0}_{1}".format(exModule, exString))

    return fileStrings

def main():

    print("#########################")
    print("# All Functions  #")
    print("#########################")

    sqf_list = []

    allFunctions = []
    allStrings = []

    parser = argparse.ArgumentParser()
    parser.add_argument('-m','--module', help='only search specified module addon folder', required=False, default=".")
    args = parser.parse_args()

    addon_base_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    for root, dirnames, filenames in os.walk(addon_base_path +"/" + 'addons' + '/' + args.module):
      for filename in fnmatch.filter(filenames, '*.sqf'):
        sqf_list.append(os.path.join(root, filename))
      for filename in fnmatch.filter(filenames, '*.cpp'):
        sqf_list.append(os.path.join(root, filename))
      for filename in fnmatch.filter(filenames, '*.hpp'):
        sqf_list.append(os.path.join(root, filename))

    for filename in sqf_list:
        allFunctions = allFunctions + getFunctions(filename)
    for filename in sqf_list:
        allStrings = allStrings + getStrings(filename)

    codeHeader = "diag_log text '*********** Scaning for nil functions [funcs {0} / strings {1}]';".format(len(set(allFunctions)), len(set(allStrings)))
    codeFuncCheck = "{ if (isNil _x) then {systemChat format ['%1 is nil', _x]; diag_log text format ['%1 is nil', _x];}} forEach allFunctions;"
    codeStringCheck = "{ if (!isLocalized _x) then {systemChat format ['%1 is not in stringtable', _x]; diag_log text format ['%1 is not in stringtable', _x];}} forEach allStrings;"

    outputCode = "{0} allFunctions = {1}; allStrings = {2}; {3} {4}".format(codeHeader, list(set(allFunctions)), list(set(allStrings)), codeFuncCheck, codeStringCheck)

    print(outputCode)
    Paste(outputCode)

    print ("")
    print ("Copied to clipboard, [funcs {0} / strings {1}]'".format(len(set(allFunctions)), len(set(allStrings))))

if __name__ == "__main__":
    main()


import os
from abc import ABC

from keras import Model, layers
from keras.layers import Conv2D, BatchNormalization, Add, MaxPool2D, GlobalAveragePooling2D, Flatten, Dense, Rescaling
import tensorflow as tf

class ResnetBlock(Model, ABC):
    """
    A standard resnet block.
    """

    def __init__(self, channels: int, down_sample=False):
        """
        channels: same as number of convolution kernels
        """
        super().__init__()

        self.__channels = channels
        self.__down_sample = down_sample
        self.__strides = [2, 1] if down_sample else [1, 1]

        KERNEL_SIZE = (3, 3)
        # use He initialization, instead of Xavier (a.k.a 'glorot_uniform' in Keras), as suggested in [2]
        INIT_SCHEME = "he_normal"

        self.conv_1 = Conv2D(self.__channels, strides=self.__strides[0],
                             kernel_size=KERNEL_SIZE, padding="same", kernel_initializer=INIT_SCHEME)
        self.bn_1 = BatchNormalization()
        self.conv_2 = Conv2D(self.__channels, strides=self.__strides[1],
                             kernel_size=KERNEL_SIZE, padding="same", kernel_initializer=INIT_SCHEME)
        self.bn_2 = BatchNormalization()
        self.merge = Add()

        if self.__down_sample:
            # perform down sampling using stride of 2, according to [1].
            self.res_conv = Conv2D(
                self.__channels, strides=2, kernel_size=(1, 1), kernel_initializer=INIT_SCHEME, padding="same")
            self.res_bn = BatchNormalization()

    def call(self, inputs, training=None, mask=None):
        res = inputs

        x = self.conv_1(inputs)
        x = self.bn_1(x)
        x = tf.nn.relu(x)
        x = self.conv_2(x)
        x = self.bn_2(x)

        if self.__down_sample:
            res = self.res_conv(res)
            res = self.res_bn(res)

        # if not perform down sample, then add a shortcut directly
        x = self.merge([x, res])
        out = tf.nn.relu(x)
        return out

class ResNet18(Model):

    def __init__(self, num_classes, **kwargs):
        """
            num_classes: number of classes in specific classification task.
        """
        super().__init__(**kwargs)
        self.conv_1 = Conv2D(64, (7, 7), strides=2,
                             padding="same", kernel_initializer="he_normal")
        self.init_bn = BatchNormalization()
        self.pool_2 = MaxPool2D(pool_size=(2, 2), strides=2, padding="same")
        self.res_1_1 = ResnetBlock(64)
        self.res_1_2 = ResnetBlock(64)
        self.res_2_1 = ResnetBlock(128, down_sample=True)
        self.res_2_2 = ResnetBlock(128)
        self.res_3_1 = ResnetBlock(256, down_sample=True)
        self.res_3_2 = ResnetBlock(256)
        self.res_4_1 = ResnetBlock(512, down_sample=True)
        self.res_4_2 = ResnetBlock(512)
        self.avg_pool = GlobalAveragePooling2D()
        self.flat = Flatten()
        self.fc = Dense(num_classes, activation="softmax")
        self.data_augmentation = tf.keras.Sequential(
            [
                layers.RandomFlip("horizontal",
                                  input_shape=(int(os.getenv("img_height")),
                                               int(os.getenv("img_width")),
                                               3)),
                layers.RandomRotation(0.1),
                layers.RandomZoom(0.1),
            ]
        )

    def call(self, inputs, training=None, mask=None):
        out = self.data_augmentation(inputs)
        out = Rescaling(scale=1.0/255)(out)
        out = self.conv_1(out)
        out = self.init_bn(out)
        out = tf.nn.relu(out)
        out = self.pool_2(out)
        for res_block in [self.res_1_1, self.res_1_2, self.res_2_1, self.res_2_2, self.res_3_1, self.res_3_2,
                          self.res_4_1, self.res_4_2]:
            out = res_block(out)
        out = self.avg_pool(out)
        out = self.flat(out)
        out = self.fc(out)
        return out


from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from app.main import ShowcaseScreen
from app.widgets.heartfelt_hellos_button import HeartfeltHellosButton
from app.widgets.heartfelt_hellos_step_progression_button import HeartfeltHellosStepProgressionButton
from app.data.data_types.idea import Idea

class IdeaCreationScreen(ShowcaseScreen):

    scroll_view = None
    grid_layout = None

    def __init__(self, **kwargs):
        super(IdeaCreationScreen, self).__init__(**kwargs)
        # NOTE: replace tags and ideas with global list so that it can be editted in this class
        self.tags=["books", "movies", "sports"]
        self.ideas=[]
        #self.progress_grid = GridLayout(spacing='10dp', padding='10dp', cols=5, size_hint_y=None)
        self.grid_layout = GridLayout(spacing='10dp', padding='10dp', cols=1, size_hint_y=None)
        self.grid_layout.bind(minimum_height=self.grid_layout.setter("height"))
        self.scroll_view = ScrollView(do_scroll_y=True)
        self.add_widget(self.scroll_view)
        self.scroll_view.add_widget(self.grid_layout)

    def on_pre_enter(self, *args):
        self.stepOne()

    def stepOne(self):
        self.grid_layout.clear_widgets()
        self.grid_layout.add_widget(Label(text="What is your conversation idea?", font_size=24, color=(255,255,255)))
        self.grid_layout.add_widget(Label())

        # text box
        textinput = TextInput(hint_text="Are you still into sports?", font_size=24, size_hint_y=None, multiline=False)
        textinput.bind(text=lambda x, y: print("Hi"))
        self.grid_layout.add_widget(textinput)
        #self.name = textinput.text

        # next and back button rendering
        next_button = HeartfeltHellosStepProgressionButton(text="next",on_press=lambda x: self.stepTwo(textinput.text))
        progress_grid=GridLayout(spacing='10dp', padding='10dp', cols=3, size_hint_y=None)
        progress_grid.add_widget(Label())
        progress_grid.add_widget(Label())
        progress_grid.add_widget(next_button)
        self.grid_layout.add_widget(progress_grid)

    def stepTwo(self, prompt):
        self.grid_layout.clear_widgets()
        self.prompt=prompt
        self.grid_layout.add_widget(Label(text="Search and select the tag(s)\nthat matches with your idea!", height=50, color=(255,255,255), size_hint_y=None))

        # text box
        #NOTE TO SELF: add filtering for search bar
        textinput = TextInput(hint_text="Search Tag here", height=50, font_size=24, size_hint_y=None)
        #textinput.bind(on_text_validate=on_enter(textinput.text))
        self.grid_layout.add_widget(textinput)

        # NOTE TO SELF: add scroll bar to tags section
        #tag_grid=GridLayout(spacing='10dp', padding='10dp', cols=1, size_hint_y=None)
        for tag in self.getTags():
            tag_button = HeartfeltHellosButton(text=tag, height=50, on_press=lambda x: self.pressTag(x.text), size_hint_y=None)
            self.grid_layout.add_widget(tag_button)
        #self.grid_layout.add_widget(tag_grid)

        # next and back button rendering
        create_person_button = HeartfeltHellosStepProgressionButton(text="Create\nIdea", on_press=lambda x: self.createIdea())
        back_button = HeartfeltHellosStepProgressionButton(text="back", on_press=lambda x: self.stepOne())
        progress_grid=GridLayout(spacing='10dp', padding='10dp', cols=3, size_hint_y=None)
        progress_grid.add_widget(back_button)
        progress_grid.add_widget(Label())
        progress_grid.add_widget(create_person_button)
        self.grid_layout.add_widget(progress_grid)

    def pressTag(self, name: str):
        print("pressed " + str)
        if name not in self.tags:
            self.tags.append(name)
        else:
            self.tags.remove(name)

    def createIdea(self):
        print("pressed create idea")
        # NOTE: return to idea screen + add self.idea to global list of ideas as input somehow
        self.ideas.append(Idea(self.prompt, self.tags)) #place holder (something like that maybe?)

    def on_leave(self, *args):
        self.grid_layout.clear_widgets()

    def getTags(self) -> list:
        return ["sports", "books", "movies"]


import numpy as np
import time
from scipy import ndimage
from .toolkit import vectools
from .toolkit.colors import Colors as _C
import matplotlib.pyplot as plt
import matplotlib
import math
import cv2

import sys
import os

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))

class Image:

    def __init__(self, fn, calibration, lock=None):
        """
        Image processing class

        Parameters
        ----------
        fn: string
            filename of the image to be processed
        """
        # t0 = time.time()
        self.fn = fn
        self.fn_npy = self.fn.split('.')[0] + '.npy'
        self.id = int(self.fn.split('cpt')[-1].split('.')[0])
        self.calibration = calibration
        # calibration_path = __location__ + '/../data/calibration.npy'
        # calibration = np.load(calibration_path)
        self.midpoint = calibration[self.id - 1][:-1]
        # self.midpoint = calibration[0][:-1]
        print(_C.YEL + 'Processing image ' + _C.BOLD + fn + _C.ENDC)
        if lock is not None:
            with lock:
                self.image = np.load(self.fn_npy)
                # self.image = cv2.imread(self.fn, cv2.IMREAD_GRAYSCALE)
        else:
            # self.image = cv2.imread(self.fn, cv2.IMREAD_GRAYSCALE)
            self.image = np.load(self.fn_npy)
        # self.image = np.rot90(self.image)
        # print('Image loaded in', str(round(time.time() - t0, 2)), 's')
        self.dimensions = np.shape(self.image)
        self.dimy, self.dimx = self.dimensions

    def transformRadial(self, env=None, midpoint=None, plot=False):
        """
        Creates a transformed image where a sector is mapped to r/phi coordinates

        Parameters
        ----------
        midpoint: 2-tuple of floats, optional
            Origin of the polar coordinate system. If None is given, the calibration data from the current class instantation is taken
        plot: bool, optional
            Plot the transformed image. Default is False
            Cannot be used if multiprocessing is active

        Returns
        -------
        transformed: 2D array
            Coordinate transformed image
        angles: 1D array of floats
            angles between which the image is fully covered
        radii: 1D array of float
            distance scaling of rmax in the transformed image
        """
        r = self.dimx
        if midpoint is None:
            midpoint = self.midpoint
        # t0 = time.time()
        dr = midpoint[0] - self.dimx
        rmax = r + dr

        hplus = midpoint[1]
        hminus = self.dimy - midpoint[1]

        thetaPlus = -math.asin(hplus / rmax)
        thetaMinus = math.asin(hminus / rmax)

        # thetaPlus, thetaMinus = -thetaMinus, -thetaPlus

        thetaPlus_idx = int((thetaPlus + np.pi) / (2 * np.pi) * self.dimy)
        thetaMinus_idx = int((thetaMinus + np.pi) / (2 * np.pi) * self.dimy)

        # c = tuple(midpoint)
        cx, cy = midpoint
        c = (cx, cy)

        transformed = cv2.linearPolar(self.image, c, rmax, cv2.WARP_FILL_OUTLIERS)

        # Destroy the image object to free memory
        del self.image

        angles = np.linspace(thetaPlus, thetaMinus, thetaMinus_idx - thetaPlus_idx, endpoint=True)
        radii = np.linspace(0, rmax, self.dimx)

        self.dimensions = np.shape(transformed)
        self.dimy, self.dimx = self.dimensions
        absoluteZero = (self.dimy / 2 - thetaPlus_idx) - 1
        transformed = transformed[thetaPlus_idx:thetaMinus_idx]

        # Pad the transformed image with the boundary value
        """
        start_idx = np.argmax(transformed > 0, axis=1)
        start_idx = np.ones((len(transformed)), np.uint8) * 1000
        transformed[:, :999] = 0
        for i in range(len(transformed)):
            transformed[i][transformed[i] == 0] = transformed[i, start_idx[i]]"""

        # Remove Calibration features
        calib_size_px = np.mean(np.array([x[2] for x in self.calibration]))
        calib_size_mm = env.calib_size_mm  # Outer radius of calibration piece
        tolerance = 1.1
        calib_width_mm = env.calib_width_mm * tolerance  # Width of the calibration piece
        # pitch_mm = self.env.pitch_mm  # Nominal electrode pitch
        scale = calib_size_mm / calib_size_px
        self.calibrationCutoff = (calib_size_mm - calib_width_mm) / scale * r / rmax
        # pitch = pitch_mm / scale
        transformed[:, int(self.calibrationCutoff):] = 0
        for i in range(len(transformed)):
            transformed[i][transformed[i] == 0] = transformed[i, int(self.calibrationCutoff) - 1]

        # plot = True

        if plot:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.imshow(transformed)
            ax.axhline(y=absoluteZero)
            ax.set_aspect('auto')
            fig.savefig(__location__ + '/../img/out/cv2transform.png', dpi=300)
        # print('Coordinate transformation completed in ', str(round(time.time() - t0, 2)), 's')
        return transformed, angles, radii

    def detectFeatures(self, matrix, thresh_std=.5, plot=False):
        """
        Distinguish band from background in binary matrix

        Parameters
        ----------
        matrix: 2D array
            8Bit single channel source matrix to be processed
        plot: bool, optional
            Plot the transformed image. Default is False
            Cannot be used if multiprocessing is active

        Returns
        -------
        proc: 2D array
            Processed binary image

        """
        # t0 = time.time()
        start_range = 2000
        # end_range = np.shape(matrix)[1] - start_range
        # Initializing Empty array in Memory
        proc = np.empty(np.shape(matrix))
        start_search = np.empty(np.shape(matrix))[:, :start_range]
        end_search = np.empty(np.shape(matrix))[:, start_range:]
        matrix = matrix.astype(np.float64, copy=False)
        # print('Blurring')
        # Gaussian Blur to remove fast features

        cv2.GaussianBlur(src=matrix, ksize=(15, 3), dst=proc, sigmaX=1.5, sigmaY=5)
        # ndimage.maximum_filter(proc, size=(5, 5), output=proc)
        # cv2.GaussianBlur(src=matrix[:, :start_range], ksize=(3, 0), dst=start_search, sigmaX=0, sigmaY=3)

        # cv2.GaussianBlur(src=matrix[:, start_range:], ksize=(31, 11), dst=end_search, sigmaX=0, sigmaY=0.1)
        start_search = matrix[:, :start_range]
        end_search = matrix[:, start_range:int(self.calibrationCutoff)]
        # print('Convolving')
        # Convolving with Prewitt kernel in x-direction
        prewitt_kernel_x = np.tile([-1, 0, 1], (15, 1))
        # prewitt_kernel_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
        # print(prewitt_kernel_x)
        # prewitt_kernel_x = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]])
        kernel_y_width = 15
        prewitt_kernel_y = np.array([[1] * kernel_y_width, [0] *
                                     kernel_y_width, [-1] * kernel_y_width])

        # prewitt_kernel_y_element = np.tile(np.ones(kernel_y_width), (15, 1))
        # prewitt_kernel_y_end = np.concatenate((prewitt_kernel_y_element, np.zeros(15), -1 * prewitt_kernel_y_element))
        # print(prewitt_kernel_y_end)

        # print(prewitt_kernel_y_end)
        cv2.threshold(src=start_search, dst=start_search, thresh=20, maxval=255, type=cv2.THRESH_TOZERO)
        cv2.GaussianBlur(src=start_search, ksize=(21, 21), dst=start_search, sigmaX=50, sigmaY=0)
        cv2.filter2D(src=start_search, kernel=prewitt_kernel_y, dst=start_search, ddepth=-1)
        np.abs(start_search, out=start_search)
        cv2.threshold(src=start_search, dst=start_search, thresh=80, maxval=1, type=cv2.THRESH_BINARY)
        start_search = start_search.astype(np.uint8, copy=False)
        n_labels, labels, l_stats, l_centroids = cv2.connectedComponentsWithStats(image=start_search, connectivity=4)
        sizes = [s[-1] for s in l_stats]
        sizes_original = sizes[:]
        sizes.remove(max(sizes))
        if len(sizes) == 0:
            start_amp = 0
            start_centroid = (0, 0)
        else:
            start_amp = max(sizes)
            start_centroid_idx = sizes_original.index(start_amp)
            start_centroid = (int(l_centroids[start_centroid_idx][1]), int(l_centroids[start_centroid_idx][0]))

        cv2.threshold(src=end_search, dst=end_search, thresh=20, maxval=255, type=cv2.THRESH_TOZERO)
        cv2.GaussianBlur(src=end_search, ksize=(21, 21), dst=end_search, sigmaX=50, sigmaY=0)
        cv2.filter2D(src=end_search, kernel=prewitt_kernel_y, dst=end_search, ddepth=-1)
        np.abs(end_search, out=end_search)
        cv2.threshold(src=end_search, dst=end_search, thresh=80, maxval=1, type=cv2.THRESH_BINARY)
        end_search = end_search.astype(np.uint8, copy=False)
        n_labels, labels, l_stats, l_centroids = cv2.connectedComponentsWithStats(image=end_search, connectivity=4)
        sizes = [s[-1] for s in l_stats]
        sizes_original = sizes[:]
        sizes.remove(max(sizes))
        if len(sizes) == 0:
            end_amp = 0
            end_centroid = (0, 0)
        else:
            end_amp = max(sizes)
            end_centroid_idx = sizes_original.index(end_amp)
            end_centroid = (int(l_centroids[end_centroid_idx][1]), int(l_centroids[end_centroid_idx][0]) + start_range)

        cv2.filter2D(src=proc, kernel=prewitt_kernel_x, dst=proc, ddepth=-1)
        # ndimage.maximum_filter(proc, size=(5, 5), output=proc)
        # cv2.GaussianBlur(src=proc, ksize=(11, 3), dst=proc, sigmaX=0, sigmaY=5)

        np.abs(proc, out=proc)

        #start_amp = start_search.max()
        #start_idx = np.unravel_index(start_search.argmax(), start_search.shape)
        start = (start_centroid, start_amp)

        # end_amp = end_search.max()
        # end_idx = np.unravel_index(end_search.argmax(), end_search.shape)
        #ex, ey = end_idx
        # if ey != 0:
        #    ey += start_range
        #end_idx = (ex, ey)
        end = (end_centroid, end_amp)

        print(self.id)
        print(start)
        print(end)
        print()

        # del start_search
        del end_search
        # print('Thresholding')
        # proc_mean = np.mean(proc)
        # proc_std = np.std(proc)

        # thresh = proc_mean + thresh_std * proc_std
        thresh = 50.0
        # thresh = proc_mean
        # thresh = 0.1
        #proc = proc * 255 / np.max(proc)

        cv2.threshold(src=proc, dst=proc, thresh=thresh, maxval=1, type=cv2.THRESH_BINARY)

        # cv2.adaptiveThreshold(src=proc,
        #                      dst=proc,
        #                      maxValue=1,
        #                      thresholdType=cv2.THRESH_BINARY,
        #                      adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C,
        #                      blockSize=1001,
        #                      C=0)

        proc = proc.astype(np.uint8, copy=False)
        Morphkernel = np.ones((11, 11), np.uint8)
        cv2.dilate(proc, Morphkernel, proc)
        cv2.erode(proc, Morphkernel, proc)

        # print('Connecting')
        # Label the complement regions of the binary image
        proc_inv = 1 - proc
        n_labels, labels, l_stats, l_centroids = cv2.connectedComponentsWithStats(image=proc_inv, connectivity=4)
        # The maximum number of pixels in a noise field
        # Everything larger is considered to be background
        fieldsize = 2e4
        # Label background fields
        gaps = []
        for i, stat in enumerate(l_stats):
            if stat[-1] > fieldsize:
                gaps.append(i)

        # Set background fields to zero
        for gap in gaps:
            labels[labels == gap] = 0
        # Set all forground fields to one
        labels[labels != 0] = 1
        labels = labels.astype(np.uint8, copy=False)

        # Combine foreground noise with with thresholded image
        cv2.bitwise_or(src1=proc, src2=labels, dst=proc)
        filtered = np.copy(proc)
        #Morphkernel = np.ones((11, 11), np.uint8)
        #cv2.dilate(proc, Morphkernel, proc)
        #cv2.erode(proc, Morphkernel, proc)

        # plot = True
        if plot:
            print('Plotting')
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.imshow(filtered)
            # ax.plot(filtered[0], lw=0.2)
            # ax.imshow(end_search)

            # ax.plot(filtered[0])
            ax.set_aspect('auto')
            ax.set_xlabel('Radius [px]')
            ax.set_ylabel('Angle [idx]')
            fig.savefig(__location__ + '/../img/out/filter' + str(self.id) + '.png', dpi=300, interpolation='none')

        # print('Features detected in', str(round(time.time() - t0, 2)), 's')
        return proc, (start, end)


# 문자열 내 마음대로 정렬하기
# 12915

def solution(strings, n):
    words = []
    answer = []
    strings.sort()
    for i in range(len(strings)):
        words.append((strings[i][n], i))

    words.sort()
    for w in words:
        answer.append(strings[w[1]])

    return answer

# strings	n	return
# ["sun", "bed", "car"]	1	["car", "bed", "sun"]
# ["abce", "abcd", "cdx"]	2	["abcd", "abce", "cdx"]

문자열_내_마음대로_정렬하기.py


from datetime import datetime
def unix_to_dt(time):
    return datetime.utcfromtimestamp(time).strftime('%Y-%m-%d %H:%M:%S')

class event():
    def __init__(self, events):
        self.type = events['type']
        self.empty = False

        if self.type == 'None':
            self.empty = True
        elif self.type == 'membership.nickname_changed':
            self.nickname_changed(events['data'])
        elif self.type == 'membership.announce.joined':
            self.user_joined(events['data'])
        elif self.type == 'membership.notifications.exited':
            self.user_exited(events['data'])
        elif self.type == 'membership.notifications.removed':
            self.user_removed(events['data'])
        elif self.type == 'membership.notifications.autokicked':
            self.user_autokicked(events['data'])
        elif self.type == 'group.avatar_change':
            self.group_avatar_change(events['data'])
        elif self.type == 'group.like_icon_set':
            self.like_icon_change(events['data'])
        elif self.type == 'group.name_change':
            self.group_name_change(events['data'])
        elif self.type == 'poll.created':
            self.poll_created(events['data'])
        elif self.type == 'poll.reminder':
            self.poll_reminder(events['data'])
        elif self.type == 'poll.finished':
            self.poll_finished(events['data'])
        elif self.type == 'message.deleted':
            self.message_deleted(events['data'])
        else:
            print('Unknown event type: ' + self.type)

    def nickname_changed(self, event_data):
        self.user = event_data['user']
        self.new_name = event_data['name']

    def user_joined(self, event_data):
        self.user = event_data['user']

    def user_added(self, event_data):
        self.user = event_data['adder_user']
        self.added_users = event_data['added_users']

    def user_exited(self, event_data):
        self.placeholder = 'X'
        self.charmap = event_data['charmap']

    def user_removed(self, event_data):
        self.user = event_data['remover_user']
        self.removed_user = event_data['removed_user']

    def user_autokicked(self, event_data):
        self.user = event_data['user']

    def group_avatar_change(self, event_data):
        self.user = event_data['user']
        self.url = event_data['avatar_url']

    def like_icon_change(self, event_data):
        self.user = event_data['user']
        self.like_icon = [event_data['pack_id'], event_data['icon_index']]
        # like_icon->type is ignored

    def group_name_change(self, event_data):
        self.user = event_data['user']
        self.new_name = event_data['name']

    '''POLL EVENTS'''

    def poll_created(self, event_data):
        self.user = event_data['user']
        self.conversation = event_data['conversation']
        self.poll = event_data['poll']

    def poll_reminder(self, event_data):
        self.conversation = event_data['conversation']
        self.poll = event_data['poll']
        self.expiration = unix_to_dt(event_data['expiration'])

    def poll_finished(self, event_data):
        self.conversation = event_data['conversation']
        self.raw_options = event_data['options']
        self.options = []
        for opt in self.raw_options:
            temp_option = {
                "id": None,
                "title": None,
                "votes": None,
                "voter_ids": None
            }
            temp_option["id"] = opt["id"]
            temp_option["title"] = opt["title"]
            try:
                temp_option["votes"] = opt["votes"]
                temp_option["voter_ids"] = opt["voter_ids"]
            except:
                temp_option["votes"] = 0
                temp_option["voter_ids"] = []
            self.options.append(temp_option)

    '''MISC EVENTS'''

    def message_deleted(self, event_data):
        self.message_id = event_data['message_id']
        self.deleted_at = unix_to_dt(event_data['deleted_at'])
        self.deleted_at_ts = event_data['deleted_at']
        self.deletion_actor = event_data['deletion_actor']
        self.deleter_id = event_data['deleter_id']


import jsonlines
import os

from pathlib import Path
from xml_handler import XmlHandler
from google_cloud_storage_client import GoogleCloudStorageClient

def main(event, context):
    # Retrieve file from GCS
    input_filename = event.get("name")
    input_bucket_name = event.get("bucket")
    output_bucket_name = os.environ.get("OUTPUT_BUCKET_NAME")

    gcs_client = GoogleCloudStorageClient()

    local_filename = gcs_client.download_file_from_gcs(bucket_name=input_bucket_name, source_blob=input_filename)

    # Read file and parse to List[dict]
    xml_content_dict = XmlHandler.read_xml_file(file_path=local_filename)
    parsed_row_list = XmlHandler.parse_harvest_xml_to_json(content_dict=xml_content_dict)

    # Write file to jsonlines
    output_filename = Path(input_filename).stem + '.jsonl'

    local_output_file = Path("/tmp") / output_filename
    with jsonlines.open(local_output_file, mode="w") as writer:
        writer.write_all(parsed_row_list)

    # Upload file to GCS
    gcs_client.upload_file_to_gcs(bucket_name=output_bucket_name, destination_blob=Path(output_filename).name,
                                  source_filename=local_output_file, remove_local_file=True)


""" Text formatting script.
Alice in Wonderland, obtained from:
https://www.gutenberg.org/ebooks/28885
"""

import string

def format_book(file_in, file_out):
    with open(file_in) as f:
        text = f.read()
    text = text.replace('\r', ' ').replace(
        '\n', ' ').replace('\t', ' ').replace('-', ' ')
    text = string.join(text.split())
    ok_chars = [' ', '.', ',']
    text = string.join(
        [char for char in text if char.isalpha() or char in ok_chars], '')
    text = text.lower()
    text = text.replace(',', '').replace('.', '')
    with open(file_out, 'w') as f:
        f.write(text)

if __name__ == 'main':
    format_book('alice_in_wonderland_RAW.txt',
                'alice_in_wonderland_FORMATTED.txt')


from bisect import bisect
from math import ceil, floor, log10

from numpy import abs, argmin, array, isnan, linspace

# Local imports
from .formatters import BasicFormatter

__all__ = [
    "AbstractScale",
    "DefaultScale",
    "FixedScale",
    "Pow10Scale",
    "LogScale",
    "ScaleSystem",
    "heckbert_interval",
    "frange",
]

def frange(min, max, delta):
    """ Floating point range. """
    count = int(round((max - min) / delta)) + 1
    return [min + i * delta for i in range(count)]

class AbstractScale(object):
    """ Defines the general interface for scales. """

    DEFAULT_NUM_TICKS = 8

    def ticks(self, start, end, desired_ticks=None):
        """Returns the set of "nice" positions on this scale that enclose and
        fall inside the interval (*start*,*end*).

        Parameters
        ----------
        start : number
            The beginning of the scale interval.
        end : number
            The end of the scale interval.
        desired_ticks : integer
            Number of ticks that the caller would like to get

        """
        raise NotImplementedError

    def num_ticks(self, start, end, desired_ticks=None):
        """Returns an approximate number of ticks that this scale
        produces for the given interval.

        This method is used by the scale system to determine whether this is
        the appropriate scale to use for an interval; the returned number of
        ticks does not have to be exactly the same as what ticks() returns.

        Parameters
        ----------
        start : number
            The beginning of the scale interval.
        end : number
            The end of the scale interval.
        desired_ticks : integer
            Number of ticks that the caller would like to get

        Returns
        -------
        A float or an integer.
        """
        raise NotImplementedError

    def labels(self, start, end, numlabels=None, char_width=None):
        """Returns a series of ticks and corresponding strings for labels
        that fall inside the interval (*start*,*end*).

        Parameters
        ----------
        start : number
            The beginning of the scale interval.
        end : number
            The end of the scale interval.
        numlabels : number
            The ideal number of labels to generate on the interval.
        char_width : number
            The total character width available for labelling the interval.

        One of *numlabels* or *char_width* must be provided. If both are
        provided, then both are considered when picking label density and format.
        """
        ticks = self.ticks(start, end, numlabels)
        labels = self.formatter.format(ticks, numlabels, char_width)
        return list(zip(ticks, labels))

    def label_width(self, start, end, numlabels=None, char_width=None):
        """Returns an estimate of the total number of characters used by the
        the labels that this scale produces for the given set of
        inputs, as well as the number of labels.

        Parameters
        ----------
        start : number
            The beginning of the scale interval.
        end : number
            The end of the scale interval.
        numlabels : number
            The ideal number of labels to generate on the interval.
        char_width : number
            The total character width available for labelling the interval.

        Returns
        -------
        (numlabels, total label width)
        """
        return self.formatter.estimate_width(
            start, end, numlabels, char_width, ticker=self
        )

class FixedScale(AbstractScale):
    """A scale with fixed resolution, and "nice" points that line up at
    multiples of the resolution.  An optional zero value can be defined
    that offsets the "nice" points to (N*resolution+zero).
    """

    def __init__(self, resolution, zero=0.0, formatter=None):
        self.resolution = resolution
        self.zero = zero
        if formatter is None:
            formatter = BasicFormatter()
        self.formatter = formatter

    def ticks(self, start, end, desired_ticks=None):
        """For FixedScale, *desired_ticks* is ignored.

        Overrides AbstractScale.
        """
        if start == end or isnan(start) or isnan(end):
            return []
        res = self.resolution
        start -= self.zero
        end -= self.zero
        start_tick = int(ceil(start / res))
        end_tick = int(floor(end / res))
        ticks = [i * res for i in range(start_tick, end_tick + 1)]
        return ticks

    def num_ticks(self, start, end, desired_ticks=None):
        """For FixedScale, *desired_ticks* is ignored.

        Overrides AbstractScale.
        """
        if self.resolution is None or self.resolution == 0.0:
            return 0
        else:
            return (end - start) / self.resolution

def _nice(x, round=False):
    """Returns a bracketing interval around interval *x*, whose endpoints fall
    on "nice" values.  If *round* is False, then it uses ceil(range)

    This function is adapted from the original in Graphics Gems; the boundaries
    have been changed to use (1, 2.5, 5, 10) as the nice values instead of
    (1, 2, 5, 10).
    """
    if x <= 0:
        import warnings

        warnings.warn(
            "Invalid (negative) range passed to tick interval calculation"
        )
        x = abs(x)
    expv = floor(log10(x))
    f = x / pow(10, expv)
    if round:
        if f < 1.75:
            nf = 1.0
        elif f < 3.75:
            nf = 2.5
        elif f < 7.0:
            nf = 5.0
        else:
            nf = 10.0
    else:
        if f <= 1.0:
            nf = 1.0
        elif f <= 2.5:
            nf = 2.5
        elif f <= 5.0:
            nf = 5.0
        else:
            nf = 10.0
    return nf * pow(10, expv)

def heckbert_interval(
    data_low, data_high, numticks=8, nicefunc=_nice, enclose=False
):
    """Returns a "nice" range and resolution for an interval and a preferred
    number of ticks, using Paul Heckbert's algorithm in Graphics Gems.

    If *enclose* is True, then the function returns a min and a max that fall
    inside *data_low* and *data_high*; if *enclose* is False, the nice interval
    can be larger than the input interval.
    """
    if data_high == data_low:
        return data_high, data_low, 0
    if numticks == 0:
        numticks = 1

    range = nicefunc(data_high - data_low)
    if numticks > 1:
        numticks -= 1
    d = nicefunc(range / numticks, round=True)
    if enclose:
        graphmin = ceil(data_low / d) * d
        graphmax = floor(data_high / d) * d
    else:
        graphmin = floor(data_low / d) * d
        graphmax = ceil(data_high / d) * d
    return graphmin, graphmax, d

class DefaultScale(AbstractScale):
    """A dynamic scale that tries to place ticks at nice numbers (1, 2, 5, 10)
    so that ticks don't "pop" as the resolution changes.
    """

    def __init__(self, formatter=None):
        if formatter is None:
            formatter = BasicFormatter()
        self.formatter = formatter

    def ticks(self, start, end, desired_ticks=8):
        """Returns the set of "nice" positions on this scale that enclose and
        fall inside the interval (*start*,*end*).

        Implements AbstractScale.
        """
        if start == end or isnan(start) or isnan(end):
            return [start]
        min, max, delta = heckbert_interval(
            start, end, desired_ticks, enclose=True
        )
        return frange(min, max, delta)

    def num_ticks(self, start, end, desired_ticks=8):
        """Returns an approximate number of ticks that this scale
        produces for the given interval.

        Implements AbstractScale.
        """
        return len(self.ticks(start, end, desired_ticks))

class Pow10Scale(AbstractScale):
    """A dynamic scale that shows only whole multiples of powers of 10
    (including powers < 1).
    """

    def __init__(self, formatter=None):
        if formatter is None:
            formatter = BasicFormatter()
        self.formatter = formatter

    def ticks(self, start, end, desired_ticks=8):
        """Returns the set of "nice" positions on this scale that enclose and
        fall inside the interval (*start*,*end*).

        Implements AbstractScale.
        """
        if start == end or isnan(start) or isnan(end):
            return [start]
        min, max, delta = heckbert_interval(
            start, end, desired_ticks, nicefunc=self._nice_pow10, enclose=True
        )
        return frange(min, max, delta)

    def num_ticks(self, start, end, desired_ticks=8):
        """Returns an approximate number of ticks that this scale
        produces for the given interval.

        Implements AbstractScale.
        """
        return len(self.ticks(start, end, desired_ticks))

    def _nice_pow10(self, x, round=False):
        return pow(10, floor(log10(x)))

class LogScale(AbstractScale):
    """A dynamic scale that only produces ticks and labels that work well when
    plotting data on a logarithmic scale.
    """

    def __init__(self, formatter=None):
        if formatter is None:
            formatter = BasicFormatter()
        self.formatter = formatter

    # In the following utility functions, "irep" stands for "integer representation".
    # For a given base interval size i (i.e. "magic number"), there is a one-to-one
    # mapping between the nice tick values and the integers.

    def _irep_to_value(self, n, i):
        """For a given "magic number" i (i.e. spacing of the evenly spaced ticks
        in the decade [1,10]), compute the tick value of the given integer
        representation."""
        if i == 1:
            j, k = divmod(n, 9)
            v = (k + 1) * 10 ** j
            return v
        else:
            j, k = divmod(n, int(10.0 / i))
            if k == 0:
                v = 10 ** j
            else:
                v = i * k * 10 ** j
            return v

    def _power_and_interval(self, x, i):
        # j is the power of 10 of the decade in which x lies
        j = int(ceil(log10(x))) - 1
        # b is the interval size of the evenly spaced ticks in the decade
        b = i * 10 ** j
        return (j, b)

    def _power_and_index_to_irep(self, j, k, i):
        if i == 1:
            n = j * 9 + (k - 1)
        else:
            n = j * int(10.0 / i) + k
        return n

    def _logtickceil_as_irep(self, x, i):
        """For a given "magic number" i (i.e. spacing of the evenly spaced ticks
        in the decade [1,10]), compute the integer representation of the smallest
        tick not less than x."""
        j, b = self._power_and_interval(x, i)
        k = int(ceil(float(x) / b))
        n = self._power_and_index_to_irep(j, k, i)
        return n

    def _logtickfloor_as_irep(self, x, i):
        """For a given "magic number" i (i.e. spacing of the evenly spaced ticks
        in the decade [1,10]), compute the integer representation of the largest
        tick not greater than x."""
        j, b = self._power_and_interval(x, i)
        k = int(floor(float(x) / b))
        n = self._power_and_index_to_irep(j, k, i)
        return n

    def ticks(self, start, end, desired_ticks=8):
        """ Compute a "nice" set of ticks for a log scale."""
        if start > end:
            start, end = end, start

        if start == 0.0:
            # Whoever calls us with a value of 0.0 puts themselves at our mercy
            log_start = 1e-9
        else:
            log_start = log10(start)

        if end == 0.0:
            log_end = 1e-9
        else:
            log_end = log10(end)
        log_interval = log_end - log_start

        if log_interval < 1.0:
            # If the data is spaced by less than a factor of 10, then use
            # regular/linear ticking
            min, max, delta = heckbert_interval(
                start, end, desired_ticks, enclose=True
            )
            return frange(min, max, delta)

        elif log_interval < desired_ticks:
            magic_numbers = [1, 2, 5]
            for interval in magic_numbers:
                n1 = self._logtickceil_as_irep(start, interval)
                n2 = self._logtickfloor_as_irep(end, interval)
                ticks = [
                    self._irep_to_value(n, interval) for n in range(n1, n2 + 1)
                ]
                if len(ticks) < desired_ticks * 1.5:
                    return ticks
            return ticks

        else:
            # Put lines at every power of ten
            startlog = ceil(log_start)
            endlog = floor(log_end)
            expticks = linspace(startlog, endlog, endlog - startlog + 1)
            return 10 ** expticks

    def num_ticks(self, start, end, desired_ticks=8):
        """Returns an approximate number of ticks that this scale
        produces for the given interval.

        Implements AbstractScale.
        """
        return len(self.ticks(start, end, desired_ticks))

##############################################################################
#
# ScaleSystem
#
##############################################################################

class ScaleSystem(object):
    """Represents a collection of scales over some range of resolutions.

    This class has settings for a default scale that is used when ticking an
    interval that is smaller than the finest resolution scale or larger than
    the coarsest resolution scale.
    """

    def __init__(self, *scales, **kw):
        """Creates a ScaleSystem

        Usage::

            ScaleSystem(scale1, .., scaleN, default_scale = DefaultScale())

        If *default_scale* is not specified, then an instance of DefaultScale()
        is created.  If no *default_scale* is needed, then set it to None.
        """
        self.scales = scales
        self.default_scale = kw.get("default_scale", DefaultScale())

        # Heuristics for picking labels
        # The ratio of total label character count to the available character width
        self.fill_ratio = 0.3
        self.default_numticks = 8

    def ticks(self, start, end, numticks=None):
        """Computes nice locations for tick marks.

        Parameters
        ==========
        start, end : number
            The start and end values of the data.
        numticks : number
            The desired number of ticks to produce.
        scales : a list of tuples of (min_interval, Scale)
            Scales to use, in order from fine resolution to coarse.
            If the end-start interval is less than a particular scale's
            *min_interval*, then the previous scale is used.

        Returns
        =======
        A list of positions where the ticks are to be placed.
        """

        if numticks == 0:
            return []
        elif start == end or isnan(start) or isnan(end):
            return []
        elif numticks is None:
            numticks = self.default_numticks

        scale = self._get_scale(start, end, numticks)
        ticks = scale.ticks(start, end, numticks)
        return ticks

    def labels(self, start, end, numlabels=None, char_width=None):
        """Computes position and labels for an interval

        Parameters
        ----------
        start : number
            The beginning of the scale interval.
        end : number
            The end of the scale interval.
        numlabels : number
            The ideal number of labels to generate on the interval.
        char_width : number
            The total character width available for labelling the interval.

        One of *numlabels* or *char_width* must be provided.  If both are
        provided, then both are considered when picking label density and format.

        Returns
        -------
        A list of (tick position, string) tuples.
        """

        # Check for insufficient arguments.
        if numlabels is None and char_width is None:
            raise ValueError(
                "Either numlabels or char_width (or both) must be given."
            )

        if numlabels == 0 or char_width == 0 or isnan(start) or isnan(end):
            return []

        # There are three cases:
        #   1. we are given numlabels but not char_width
        #   2. we are given char_width and not numlabels
        #   3. we are given both
        #
        # Case 1: Use numlabels to find the closest scale purely on tick count.
        # Case 2: Query all scales for their approximate label_width, pick the
        #         closest one to char_width * self.fill_ratio
        # Case 3: Use numlabels to find the closest scale based on tick count.

        if numlabels and not char_width:
            # numlabels was given, but not char_width.
            scale = self._get_scale(start, end, numlabels)
            labels = scale.labels(start, end, numlabels)

        else:
            # char_width was given.
            if numlabels:
                # Both numlabels and char_width were given.
                scale = self._get_scale(start, end, numlabels)
                try:
                    ndx = list(self.scales).index(scale)
                    low = max(0, ndx - 1)
                    high = min(len(self.scales), ndx + 1)
                    scales = self.scales[low:high]
                except ValueError:
                    scales = [scale]
            else:
                # Only char_width was given.
                if len(self.scales) == 0:
                    scales = [self.default_scale]
                else:
                    scales = self.scales

            counts, widths = zip(
                *[
                    s.label_width(start, end, char_width=char_width)
                    for s in scales
                ]
            )
            widths = array(widths)
            closest = argmin(abs(widths - char_width * self.fill_ratio))
            if numlabels is None:
                numlabels = scales[closest].num_ticks(
                    start, end, counts[closest]
                )
            labels = scales[closest].labels(
                start, end, numlabels, char_width=char_width
            )

        return labels

    def _get_scale(self, start, end, numticks):
        if len(self.scales) == 0:
            closest_scale = self.default_scale
        else:
            closest_scale = self._get_scale_np(start, end, numticks)

            if self.default_scale is not None:
                # Handle the edge cases and see if there is a major discrepancy between
                # what the scales offer and the desired number of ticks; if so, revert
                # to using the default scale
                approx_ticks = closest_scale.num_ticks(start, end, numticks)
                if (
                    (approx_ticks == 0)
                    or (numticks == 0)
                    or (abs(approx_ticks - numticks) / numticks > 1.2)
                    or (abs(numticks - approx_ticks) / approx_ticks > 1.2)
                ):
                    closest_scale = self.default_scale
        return closest_scale

    def _get_scale_bisect(self, start, end, numticks):
        scale_intervals = [
            s.num_ticks(start, end, numticks) for s in self.scales
        ]
        sorted_scales = sorted(zip(scale_intervals, self.scales))
        ndx = bisect(sorted_scales, numticks, lo=0, hi=len(self.scales))
        if ndx == len(self.scales):
            ndx -= 1
        return sorted_scales[ndx][1]

    def _get_scale_np(self, start, end, numticks):
        # Extract the intervals from the scales we were given
        scale_intervals = array(
            [s.num_ticks(start, end, numticks) for s in self.scales]
        )
        closest = argmin(abs(scale_intervals - numticks))
        return self.scales[closest]


### This file is meant to run from pc, *not* from the server. It extracts the
# data from the datafile, posts it to the database and finally runs the day
# command to add the day to the data.
import math

import pandas as pd
import requests

day_of_month = 6

def upload_data(fname):
    data = extract_data(fname)
    data_dict_list = []
    url = create_url()
    for _, row in data.iterrows():
        aaa = row[5]
        if math.isnan(aaa):
            aaa = 'NULL'
        dict = {"SubmitDateTime": str(row[0]),
                "UserId": row[1]+1000,
                "ExerciseId": row[2],
                "LearningObjectiveId": 8025,
                "Correct": min(row[4], 1),
                "AbilityAfterAnswer": aaa}
        data_dict_list.append(dict)
        print(dict, ",")
    # r = requests.post(url=url + "insert/", json=data_dict_list[0],
    #                   auth=("Group2", "Group2-1234"))
    # print(r.status_code, r.reason, url + "insert/")
    r = requests.get(url + "add_days/start=2018-06-04&end=2018-06-0{}".format( str(day_of_month)), auth=("Group2", "Group2-1234"))
    print(r.status_code, r.reason,
          url + "add_days/start=2018-06-04&end=2018-06-0{}".format(
              day_of_month))

def extract_data(fname):
    data = pd.read_excel(fname)
    return data

def create_url():
    url = "http://applab.ai.ru.nl:5000/"
    return url

if __name__ == "__main__":
    day_of_month = 6
    upload_data("C:/Users/Rick "
                "Dijkstra/Documents/Study/Applab/SnappetDataAnoniem/"
                "resultaten-radboud_anoniem 4-6-18.xlsx")


#!/usr/bin/env python3

from bcc import BPF
from http.server import HTTPServer, BaseHTTPRequestHandler
import sys
import threading

clone_ebpf = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
#include <linux/fs.h>

#define ARGSIZE  128

BPF_PERF_OUTPUT(events);

struct data_t {
    u32 pid;  // PID as in the userspace term (i.e. task->tgid in kernel)
    u32 ppid; // Parent PID as in the userspace term (i.e task->real_parent->tgid in kernel)
    u32 uid;
    char comm[TASK_COMM_LEN];
};

int clone_ebpf(struct pt_regs *ctx) {
    struct data_t data = {};
    struct task_struct *task;

    data.uid = bpf_get_current_uid_gid() & 0xffffffff;
    data.pid = bpf_get_current_pid_tgid() >> 32;

    task = (struct task_struct *)bpf_get_current_task();
    data.ppid = task->real_parent->tgid;

    bpf_get_current_comm(&data.comm, sizeof(data.comm));

    events.perf_submit(ctx, data, sizeof(struct data_t));

    return 0;
}
"""

pid = ""
ppid = ""
uid = ""
command = ""

def clone_ebpf_thread():
    b = BPF(text=clone_ebpf)
    clone_fn_name = b.get_syscall_fnname("clone")
    b.attach_kprobe(event=clone_fn_name, fn_name="clone_ebpf")
    b["events"].open_perf_buffer(collect_events)
    while 1:
      try:
          b.perf_buffer_poll()
      except KeyboardInterrupt:
          exit()

def collect_events():
    event = b["events"].event(data)
    pid = event.pid
    ppid = event.ppid
    uid = event.uid
    command = event.comm

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        print("received a get message", sys.stdout)
        self.send_response(200)
        self.end_headers()
        response = "pid: " + pid + " ppid: " + ppid + " uid: " + uid + " command: " + command
        self.wfile.write(response)

x = threading.Thread(target=clone_ebpf_thread)
x.start()
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()


def busca_profundidade(grafo, inicio, destino, visitados=None):

    if visitados is None:
        visitados = [inicio]

    if inicio == destino:
        return visitados

    for proximo in grafo[inicio]:

        if proximo not in visitados and destino not in visitados:
            visitados = busca_profundidade(grafo, proximo, destino, visitados + [proximo])

    return visitados


from PIL import Image

def brighten_Image(pixelList):

	pix_len = len(pixelList)
	for i in range(pix_len): #assigns each part of tuple to a variable
		current_pixel = pixelList[i] #then will brigthen each by 50
		red = current_pixel[0]
		green = current_pixel[1]
		blue = current_pixel[2]
		#this is not the best way, ex : replace  current_pixel
		#with pixList[i][0] ect
		newPixel = (red+50, green+50,  blue+50)
		pixelList[i] = newPixel #replaces current pixel with one
								#whos R,G,B values are +50

	return pixelList

def main():

	myFile = "/home/h702546919/Desktop/jimmy.jpg"
	my_img_obj = Image.open(myFile)
	#my_img_obj.show()

	#grabs each pixel's tuple
	pixelList = list(my_img_obj.getdata())

	#sends list of tuples to be brightend, gives back newPic
	newPic = brighten_Image(pixelList)
	#pushes new brighter pixels
	my_img_obj.putdata(newPic)

	#displays new pic and old one
	#(learn how to make a canvas and print side by side ? )
	my_img_obj.show()

	return

#---#
main()