text:
"""

``nn()`` is used to train an instance of ``globalemu`` on the preprocessed
data in ``base_dir``. All of the parameters for ``nn()`` are kwargs and
a number of them can be left at their default values however you will
need to set the ``base_dir`` and possibly ``epochs`` and ``xHI`` (see below and
the tutorial for details).

"""

import tensorflow as tf
from tensorflow import keras
import numpy as np
import time
import os
from globalemu.models import network_models
from globalemu.losses import loss_functions

class nn():

    r"""

    **kwargs:**

        batch_size: **int / default: 100**
            | The batch size used by ``tensorflow`` when performing training.
                Corresponds to the number of samples propagated before the
                networks hyperparameters are updated. Keep the value ~100 as
                this will help with memory management and training speed.

        epochs: **int / default: 10**
            | The number of epochs to train the network on. An epoch
                corresponds to training on x batches where x is sufficiently
                large for every sample to have influenced an update of the
                network hyperparameters.

        activation: **string / default: 'tanh'**
            | The type of activation function used in the neural networks
                hidden layers. The activation function effects the way that the
                network learns and updates its hyperparameters. The defualt
                is a commonly used activation for regression neural networks.

        lr: **float / default: 0.001**
            | The learning rate acts as a "step size" in the optimization and
                its value can effect the quality of the emulation. Typical
                values fall in the range 0.001-0.1.

        dropout: **float / default: 0**
            | The dropout for the neural network training. ``globalemu`` is
                designed so that you shouldn't need dropout to prevent
                overfitting but we leave it as an option.

        input_shape: **int / default: 8**
            | The number of input parameters (astrophysical parameters
                plus redshift) for the neural network. The default accounts
                for 7 astrophysical
                parameters and a single redshift input.

        output_shape: **int / default: 1**
            | The number of ouputs (temperature) from the neural network.
                This shouldn't need changing.

        layer_sizes: **list / default: [input_shape, input_shape]**
            | The number of hidden layers and the number of nodes in each
                layer. For example ``layer_sizes=[8, 8]`` will create
                two hidden layers both with 8 nodes (this is the default).

        base_dir: **string / default: 'model_dir/'**
            | This should be the same as the ``base_dir`` used when
                preprocessing. It contains the data that the network will
                work with and is the directory in which the trained model will
                be saved in.

        early_stop: **Bool / default: False**
            | If ``early_stop`` is set too ``True`` then the network will stop
                learning if the loss has not changed up to an accuracy given
                by ``early_stop_lim`` within the last ten epochs.

        early_stop_lim: **float / default: 1e-4**
            | The precision with which to assess the change in loss over the
                last ten epochs when ``early_stop=True``. The value of this
                parameter is strongly dependent on the magnitude of the
                evaluated loss at each epoch and the default may be to high or
                too low for the desired outcome. For example if our loss value
                is initially 0.01 and decreases with each epoch then a
                ``epoch_stop_lim`` of 0.1 will cause training to stop after
                10 epochs and give poor results.

        xHI: **Bool / default: False**
            | If True then ``globalemu`` will act as if it is training a
                neutral fraction history emulator.

        output_activation: **string / default: 'linear'**
            | Determines the output activation function for the network.
                Modifying this
                is useful if the emulator output is required to be positive or
                negative etc. If xHI is True then the output activation is
                set to 'relu' else the function is 'linear'. See the tensorflow
                documentation for more details on the types of activation
                functions available.

        loss_function: **Callable/ default: None**
            | By default the code uses an MSE loss however users are able to
                pass their own loss functions when training the neural
                network. These should be functions that take in the true labels
                (temperatures) and the predicted labels and return some measure
                of loss. Care needs to be taken to ensure that the correct loss
                function is supplied when resuming the training of
                a previous run as ``globalemu`` will not check this. In order
                for the loss function to work it must be built
                using the tensorflow.keras backend. An example would be

                .. code:: python

                    from tensorflow.keras import backend as K

                    def custom_loss(true_labels, predicted_labels,
                            netowrk_inputs):
                        return K.mean(K.abs(true_labels - predicted_labels))

                The function must take in as arguments the `true_labels`,
                the `predicted_labels` and the `network_inputs`.

        resume: **Bool / default: False**
            | If set to ``True`` then ``globalemu`` will look in the
                ``base_dir`` for a trained model and ``loss_history.txt``
                file (which contains the loss recorded at each epoch) and
                load these in to continue training. If ``resume`` is ``True``
                then you need to make sure all of the kwargs are set the
                with the same values that they had in the initial training
                for a consistent run.
                There will be a human readable file in ``base_dir`` called
                "kwargs.txt" detailing
                the values of the kwargs that were provided for the
                initial training run. Anything missing from this file will
                of had its default value. This file will not be overwritten
                if ``resume=True``.

        random_seed: **int or float / default: None**
            | This kwarg sets the random seed used by tensorflow with the
                function ``tf.random.set_seed(random_seed)``. It should
                be used if you want to have reproducible results but note
                that it may cause an 'out of memory' error if training on
                large amounts of data
                (see https://github.com/tensorflow/tensorflow/issues/37252).

    """
    def __init__(self, **kwargs):

        for key, values in kwargs.items():
            if key not in set(
                    ['batch_size', 'activation', 'epochs',
                        'lr', 'dropout', 'input_shape',
                        'output_shape', 'layer_sizes', 'base_dir',
                        'early_stop', 'early_stop_lim', 'xHI', 'resume',
                        'random_seed', 'output_activation',
                        'loss_function']):
                raise KeyError("Unexpected keyword argument in nn()")

        self.resume = kwargs.pop('resume', False)
        self.base_dir = kwargs.pop('base_dir', 'model_dir/')
        if type(self.base_dir) is not str:
            raise TypeError("'base_dir' must be a sting.")
        elif self.base_dir.endswith('/') is False:
            raise KeyError("'base_dir' must end with '/'.")

        if self.resume is not True:
            with open(self.base_dir + 'kwargs.txt', 'w') as f:
                for key, values in kwargs.items():
                    f.write(str(key) + ': ' + str(values) + '\n')
                f.close()

        self.batch_size = kwargs.pop('batch_size', 100)
        self.activation = kwargs.pop('activation', 'tanh')
        if type(self.activation) is not str:
            raise TypeError("'activation' must be a string.")
        self.epochs = kwargs.pop('epochs', 10)
        self.lr = kwargs.pop('lr', 1e-3)
        self.drop_val = kwargs.pop('dropout', 0)
        self.input_shape = kwargs.pop('input_shape', 8)
        self.output_shape = kwargs.pop('output_shape', 1)
        self.layer_sizes = kwargs.pop(
            'layer_sizes', [self.input_shape, self.input_shape])
        if type(self.layer_sizes) is not list:
            raise TypeError("'layer_sizes' must be a list.")
        self.early_stop_lim = kwargs.pop('early_stop_lim', 1e-4)
        self.early_stop = kwargs.pop('early_stop', False)
        self.xHI = kwargs.pop('xHI', False)
        self.random_seed = kwargs.pop('random_seed', None)

        boolean_kwargs = [self.resume, self.early_stop, self.xHI]
        boolean_strings = ['resume', 'early_stop', 'xHI']
        for i in range(len(boolean_kwargs)):
            if type(boolean_kwargs[i]) is not bool:
                raise TypeError("'" + boolean_strings[i] + "' must be a bool.")

        int_kwargs = [self.batch_size, self.epochs, self.input_shape,
                      self.output_shape]
        int_strings = ['batch_size', 'epochs', 'input_shape',
                       'output_shape']
        for i in range(len(int_kwargs)):
            if type(int_kwargs[i]) is not int:
                raise TypeError("'" + int_strings[i] + "' must be a int.")

        float_kwargs = [self.lr, self.early_stop_lim, self.drop_val,
                        self.random_seed]
        float_strings = ['lr', 'early_stop_lim', 'dropout', 'random_seed']
        for i in range(len(float_kwargs)):
            if float_kwargs[i] is not None:
                if type(float_kwargs[i]) not in set([float, int]):
                    raise TypeError("'" + float_strings[i] +
                                    "' must be a float.")

        loss_function = kwargs.pop('loss_function', None)
        if loss_function is not None:
            if not callable(loss_function):
                raise TypeError('loss_function should be a callable.')

        if self.random_seed is not None:
            tf.random.set_seed(self.random_seed)

        if not os.path.exists(self.base_dir):
            os.mkdir(self.base_dir)

        pwd = os.getcwd()
        train_dataset_fp = pwd + '/' + self.base_dir + 'train_dataset.csv'

        column_names = [
            'p' + str(i)
            for i in range(self.input_shape + self.output_shape)]
        label_names = column_names[-1]

        train_dataset = tf.data.experimental.make_csv_dataset(
            train_dataset_fp,
            self.batch_size,
            column_names=column_names,
            label_name=label_names,
            num_epochs=1)

        def pack_features_vector(features, labels):
            return tf.stack(list(features.values()), axis=1), labels

        train_dataset = train_dataset.map(pack_features_vector)

        self.output_activation = kwargs.pop('output_activation', 'linear')
        if self.xHI is True:
            self.output_activation = 'relu'

        if self.resume is True:
            model = keras.models.load_model(
                self.base_dir + 'model.h5',
                compile=False)
        else:
            model = network_models().basic_model(
                self.input_shape, self.output_shape,
                self.layer_sizes, self.activation, self.drop_val,
                self.output_activation)

        def loss(model, x, y, training):
            y_ = tf.transpose(model(x, training=training))[0]
            lf = loss_functions(y, y_)
            if loss_function is None:
                return lf.mse(), lf.rmse()
            else:
                return loss_function(y, y_, x), lf.rmse()

        def grad(model, inputs, targets):
            with tf.GradientTape() as tape:
                loss_value, rmse = loss(model, inputs, targets, training=True)
            return loss_value, rmse, tape.gradient(
                loss_value, model.trainable_variables)

        optimizer = keras.optimizers.Adam(learning_rate=self.lr)

        if self.resume is True:
            train_loss_results = list(
                np.loadtxt(self.base_dir + 'loss_history.txt'))
        else:
            train_loss_results = []
        train_rmse_results = []
        num_epochs = self.epochs
        for epoch in range(num_epochs):
            s = time.time()
            epoch_loss_avg = tf.keras.metrics.Mean()
            epoch_rmse_avg = tf.keras.metrics.Mean()

            for x, y in train_dataset:
                loss_values, rmse, grads = grad(model, x, y)
                optimizer.apply_gradients(
                    zip(grads, model.trainable_variables))
                epoch_loss_avg.update_state(loss_values)
                epoch_rmse_avg.update_state(rmse)

            train_loss_results.append(epoch_loss_avg.result())
            train_rmse_results.append(epoch_rmse_avg.result())
            e = time.time()

            print(
                'Epoch: {:03d}, Loss: {:.5f}, RMSE: {:.5f}, Time: {:.3f}'
                .format(
                    epoch, epoch_loss_avg.result(),
                    epoch_rmse_avg.result(), e-s))

            if self.early_stop is True:
                if len(train_loss_results) > 10:
                    if np.isclose(
                            train_loss_results[-10], train_loss_results[-1],
                            self.early_stop_lim, self.early_stop_lim):
                        print('Early Stop')
                        model.save(self.base_dir + 'model.h5')
                        break
            if (epoch + 1) % 10 == 0:
                model.save(self.base_dir + 'model.h5')
                np.savetxt(
                    self.base_dir + 'loss_history.txt', train_loss_results)

        model.save(self.base_dir + 'model.h5')
        np.savetxt(self.base_dir + 'loss_history.txt', train_loss_results)

max_stars_repo_name: htjb/globalemu

max_issues_repo_name: htjb/globalemu

max_forks_repo_name: htjb/globalemu


text:
from __future__ import print_function
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
# This line only needed if building with NumPy in Cython file.
from numpy import get_include
from os import system

# compile the fortran modules without linking
fortran_mod_comp = 'make'
system(fortran_mod_comp)

ext_modules = [Extension(# module name:
                         "pyhazel",
                         # source file:
                         ['pyhazel.pyx'],
                         # other compile args for gcc
                         extra_compile_args=['-fPIC', '-O3'],
                         # other files to link to
                         extra_link_args=['vars.o', 'maths.o', 'allen.o', 'svd.o', 'io.o', 'SEE.o', 'rt_coef.o', 'synth.o',
														'hazel.o','singleton.o', '-lgfortran'])]

setup(name = 'pyhazel',
      cmdclass = {'build_ext': build_ext},
      # Needed if building with NumPy.
      # This includes the NumPy headers when compiling.
      include_dirs = [get_include()],
      ext_modules = ext_modules)

system('cp *.so ../RunPy')

ext: py

max_stars_repo_name: aasensio/pyiacsun

max_issues_repo_name: aasensio/pyiacsun

max_forks_repo_name: aasensio/pyiacsun

reason: from numpy


text:
import argparse
import os
import shutil
import scipy.interpolate

os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'

from upsampling.utils import Upsampler
import numpy as np
import torch
from face_detection import detect_face, convert_frames_to_video
import cv2
import ntpath
import os
import tqdm
import moviepy.editor as mp
import skvideo
skvideo.setFFmpegPath('yor_ffmpeg_path')

try:
    import esim_py
except ImportError:
    print("esim_py not found, importing binaries. These do not correspond to source files in this repo")
    import sys

    binaries_folder = os.path.join(os.path.dirname(__file__), "..", "bin")
    sys.path.append(binaries_folder)
    print(sys.path)
    import esim_py

timestamps_file = "Data/Upsampled/seq0/timestamps.txt"
input_path = "Data/RGB_video/"
upsample_dir = "Data/Upsampled"

def upsample(fps, video_name, device):
    input_dir = input_path + video_name
    output_dir = upsample_dir

    # create fps file
    file = open(input_dir + "/seq0/fps.txt", "w")
    file.write(str(fps))
    file.close()

    upsampler = Upsampler(
        input_dir=input_dir,
        output_dir=output_dir,
        device=device
    )
    upsampler.upsample()

    sample = cv2.imread(upsample_dir + "/seq0/imgs/00000000.png")
    return sample.shape[0], sample.shape[1]

def generate_events(cp=0.1, cn=0.1, rp=1e-4, log_eps=1e-3, use_log=True):
    esim = esim_py.EventSimulator(cp, cn, rp, log_eps, use_log)
    events = esim.generateFromFolder(upsample_dir + "/seq0/imgs", timestamps_file)
    try:
        shutil.rmtree(upsample_dir)
    except OSError as e:
        print("Error: %s - %s." % (e.filename, e.strerror))
    return events

def viz_events(events, resolution):
    pos_events = events[events[:, -1] == 1]
    neg_events = events[events[:, -1] == -1]

    image_pos = np.zeros(resolution[0] * resolution[1], dtype="uint8")
    image_neg = np.zeros(resolution[0] * resolution[1], dtype="uint8")

    np.add.at(image_pos, (pos_events[:, 0] + pos_events[:, 1] * resolution[1]).astype("int32"), pos_events[:, -1] ** 2)
    np.add.at(image_neg, (neg_events[:, 0] + neg_events[:, 1] * resolution[1]).astype("int32"), neg_events[:, -1] ** 2)

    image_rgb = np.stack(
        [
            image_pos.reshape(resolution),
            image_neg.reshape(resolution),
            np.zeros(resolution, dtype="uint8")
        ], -1
    ) * 50

    return image_rgb

def plot_events(events, H, W, i, xs_intrp, ys_intrp, video_name, is_not_none):
    image_rgb = viz_events(events, [H, W])
    gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)

    cv2.imwrite("Data/Result/" + video_name + "/frame" + str(i) + ".png", gray_image)

    if is_not_none:
        xs = xs_intrp(i)
        ys = ys_intrp(i)

        xmax, ymax = xs.max(), ys.max()
        xmin, ymin = xs.min(), ys.min()

        return [xmin, ymin, xmax, ymax]
    else:
        print("No detections on frame " + str(i))
        return [None, None, None, None]

def adapt_events_array(events):
    tmp = []
    h, w = 0, 0  # maxh and maxw
    for evs in events:
        evs[0] = int(evs[0])
        if evs[0] > w:
            w = evs[0]
        evs[1] = int(evs[1])
        if evs[1] > h:
            h = evs[1]
        evs[3] = int(evs[3])
        evs[2] = int(evs[2] * 1000000)  # us
        tmp.append((evs[0], evs[1], evs[3], evs[2]))
    return tmp, int(h) + 1, int(w) + 1

def generate_frames(ac_time, events, H, W, xs_intrp, ys_intrp, video_name, nones):
    i = 0
    step = 0
    j = 0
    bboxs = []
    total = int(events[-1][2] / ac_time) + 1  # this number is not relevant
    indxs = [True for _ in range(total)]
    for x in nones:
        indxs[x] = False
    pbar = tqdm.tqdm(total=total)
    pbar.set_description("Frames generated")
    while j < len(events) - 1:
        while events[j][2] - events[step][2] < ac_time and j < len(events) - 1:
            j += 1
        next_step = j
        bbox = plot_events(events[step:next_step], H, W, i, xs_intrp, ys_intrp, video_name, indxs[i])
        bboxs.append(bbox)
        step = next_step
        i += 1
        pbar.update()
    return np.array(bboxs)

def manage_input_path(video_path, res):
    video_name = ntpath.basename(video_path).split(".")[0]
    video_format = ntpath.basename(video_path).split(".")[1]
    if not os.path.exists(input_path + video_name):
        os.mkdir(input_path + video_name)
    if not os.path.exists(input_path + video_name + "/seq0"):
        os.mkdir(input_path + video_name + "/seq0")
    if not os.path.exists("Data/Result/" + video_name):
        os.mkdir("Data/Result/" + video_name)
    clip = mp.VideoFileClip(video_path)
    clip_resized = clip.resize(height=res)
    clip_resized.write_videofile(input_path + video_name + "/seq0/" + video_name + "." + video_format)
    # shutil.copy(video_path, input_path + video_name + "/seq0")
    return video_name, video_format

def interpolate_detections(detections, k, res_frame_num):
    xs = []
    ys = []
    nones = []
    count = 0
    for i in range(len(detections)):
        if detections[i] is None:
            nones.append(i)
        else:
            xs.append(detections[i][0][:, 0])
            ys.append(detections[i][0][:, 1])
    p = [0]
    for i in range(1, res_frame_num):
        count += k
        if i not in nones:
            p.append(count)
    xs_intrp = scipy.interpolate.interp1d(p, np.vstack(xs), axis=0, fill_value='extrapolate')
    ys_intrp = scipy.interpolate.interp1d(p, np.vstack(ys), axis=0, fill_value='extrapolate')
    _nones = []
    for i in nones:
        for j in range(round(i * k), round(i * k + k)):
            _nones.append(j)
    return xs_intrp, ys_intrp, _nones

def get_flags():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_file", required=True,
                        help='Path to input file. See README.md for output directory.')
    parser.add_argument("--device", type=str, default="cpu", help='Device to be used (cpu, cuda:X)')
    parser.add_argument("--ups_fps", type=int, default=25, help='Upsampling fps')
    parser.add_argument("--resize_h", type=int, default=240, help='Height of resized frames')
    parser.add_argument("--acc_time", type=float, required=True, help='Accumulation time for frame creation')
    parser.add_argument("--video_res", type=bool, default=False,
                        help='Set this True if you want to generate a video with the result frames')

    args = parser.parse_args()
    return args

def main():
    # parameters TODO parser

    flags = get_flags()
    video_path = flags.input_file
    output_path = "Data/Result/"
    resolution = flags.resize_h
    up_fps = flags.ups_fps
    acc_time = flags.acc_time
    # res_fps = 60

    video_name, video_format = manage_input_path(video_path, resolution)
    H, W = upsample(up_fps, video_name, flags.device)
    tmp_path = input_path + video_name + "/seq0/" + video_name + "." + video_format
    detections, video_frame_num = detect_face(H, W, input_path + video_name, tmp_path, flags.device)
    events = generate_events()
    video_len = events[-1][2]
    res_frame_num = video_len / acc_time
    k = res_frame_num / video_frame_num
    xs_intrp, ys_intrp, nones = interpolate_detections(detections, k, video_frame_num)
    del detections
    bboxs = generate_frames(acc_time, events, H, W, xs_intrp, ys_intrp, video_name, nones)
    np.save("Data/Detections/bboxs/" + video_name, bboxs)
    del events
    if flags.video_res:
        convert_frames_to_video(output_path + video_name + "/", output_path + video_name + "." + video_format,
                                int(1 / acc_time), video_name)

if __name__ == '__main__':
    main()

reason: import numpy,import scipy


text:
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from runx.logx import logx
import torch
from torch2trt import TRTModule
import time
import importlib
import torchvision.transforms as standard_transforms
import transforms.joint_transforms as joint_transforms
from config import cfg
import argparse
import numpy as np
import cv2
import tensorrt as trt

def get_trt_model(name):
    model_trt = TRTModule()
    model_trt.load_state_dict(torch.load(name))
    return model_trt

def get_color_mask(y, colorize_mask_fn):
    # prob_mask, predictions = y.data.max(1)
    prob_mask, predictions = y
    predictions = predictions.cpu().squeeze(0).squeeze(0).numpy()
    color_mask = colorize_mask_fn(predictions)
    return color_mask

def arg_parse():
    # Argument Parser
    parser = argparse.ArgumentParser(
        description='Semantic Segmentation TensorRT Inference')
    parser.add_argument('--save', action='store_true', default=False)
    parser.add_argument('--model_name',
                        type=str,
                        default='ocrnet_trt.DDRNet23_Slim_trt.pth')
    parser.add_argument('--img_folder', type=str, default='imgs/test_imgs')
    parser.add_argument('--output_path', type=str, default="output")
    args = parser.parse_args()
    return args

def main():
    args = arg_parse()

    # eval_size = (384, 768)
    eval_size = (720, 1280)

    # Update dataset version.
    cfg.immutable(False)
    cfg.DATASET.MAPILLARY_VERSION = "v2.0_dm"
    cfg.immutable(True)

    logx.initialize(logdir="/tmp", tensorboard=False, global_rank=0)
    mod = importlib.import_module('datasets.{}'.format("mapillary"))
    dataset_cls = getattr(mod, 'Loader')
    val_joint_transform_list = [
        joint_transforms.Scale(eval_size[1]),
        joint_transforms.CenterCrop(eval_size)
    ]

    val_input_transform = standard_transforms.Compose([
        standard_transforms.ToTensor(),
        standard_transforms.Normalize([0.485, 0.456, 0.406],
                                      [0.229, 0.224, 0.225])
    ])
    val_set = dataset_cls(mode='folder',
                          joint_transform_list=val_joint_transform_list,
                          img_transform=val_input_transform,
                          eval_folder=args.img_folder)

    model_trt = get_trt_model(args.model_name)

    if args.save:
        os.makedirs(args.output_path, exist_ok=True)

    for input_images, _, img_names, _ in val_set:
        x = input_images.cuda().unsqueeze(0)
        start_time = time.time()
        y_trt = model_trt(x)
        color_mask = get_color_mask(y_trt, val_set.colorize_mask)
        used_time = time.time() - start_time
        print("Inference time: ", used_time)
        if args.save:
            color_mask.save("{}/{}_pred_trt.png".format(
                args.output_path, img_names))
        else:
            cv_img = np.asarray(color_mask.convert('RGB'))
            cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
            cv2.imshow("img", cv_img)
            cv2.waitKey(1)

if __name__ == "__main__":
    main()

max_stars_repo_name: sijin-dm/semantic-segmentation

max_issues_repo_name: sijin-dm/semantic-segmentation

max_forks_repo_name: sijin-dm/semantic-segmentation

reason: import numpy


text:
import json
import os
import tempfile
import unittest
import shutil
import time

import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F

from trixi.experiment import PytorchExperiment
from trixi.util import ResultLogDict

class TestPytorchExperiment(unittest.TestCase):

    def setUp(self):
        self.test_dir = tempfile.gettempdir()
        self.experiment = PytorchExperiment(name="test_experiment", base_dir=self.test_dir, n_epochs=10)

    def tearDown(self):
        self.experiment._exp_state = "Ended"
        if os.path.exists(self.test_dir):
            shutil.rmtree(self.test_dir, ignore_errors=True)

    def test_train(self):
        self.cntr = []
        self.experiment.train = lambda epoch: self.cntr.append(0)
        self.experiment.run()
        self.assertTrue(len(self.cntr) == 10, "Did not call train for the right number of epochs")

    def test_validate(self):
        self.cntr = []
        self.experiment.validate = lambda epoch: self.cntr.append(0)
        self.experiment.run()
        self.assertTrue(len(self.cntr) == 10, "Did not call train for the right number of epochs")

    def test_update_attributes(self):
        results2 = ResultLogDict("results-log.json", base_dir=self.test_dir)
        results2["test"] = 0
        self.experiment.test_var = "test"

        self.experiment.update_attributes({"results": results2, "test_var": "test2"})

        self.assertTrue(self.experiment.test_var == "test2", "Could not update simple var in update_attributes")
        self.assertTrue(self.experiment.results['test'] == 0, "Could not result dict var in update_attributes")

    def test_get_pytorch_modules(self):
        module = Net()
        self.experiment.net = module

        pyt_module = self.experiment.get_pytorch_modules()

        self.assertTrue("net" in pyt_module, "Name for module could not be correctly returned in get_pytorch_modules")
        self.assertTrue(pyt_module['net'] == module, "Module could not be correctly returned in  get_pytorch_modules")

    def test_get_pytorch_optimizers(self):
        optimizer = torch.optim.Adam(params=[torch.tensor(1)])
        self.experiment.optim = optimizer

        pyt_optim = self.experiment.get_pytorch_optimizers()

        self.assertTrue("optim" in pyt_optim, "Name for optimizer could not be correctly returned in "
                                              "get_pytorch_optimizers")
        self.assertTrue(pyt_optim['optim'] == optimizer, "Optimizer could not be correctly returned in  "
                                                         "get_pytorch_optimizers")

    def test_get_simple_vars(self):
        self.experiment.a = "test"
        self.experiment.b = 1
        self.experiment.c = True
        self.experiment.d = (1, 2, 3, 4)

        var = self.experiment.get_simple_variables()

        self.assertTrue("a" in var and "b" in var and "c" in var and "d" in var,
                        "Names for attributes could not be correctly returned in get_simple_variables")
        self.assertTrue(var['a'] == "test" and var['b'] == 1 and var['c'] == True and var['d'] == (1, 2, 3, 4),
                        "variables could not be correctly returned in get_simple_variables")

    def test_get_pytorch_vars(self):
        var = torch.tensor(1)
        self.experiment.pyt_var = var

        pyt_vars = self.experiment.get_pytorch_variables()

        self.assertTrue("pyt_var" in pyt_vars, "Name for variable could not be correctly returned in "
                                               "get_pytorch_variables")
        self.assertTrue(pyt_vars['pyt_var'] == var, "Variable could not be correctly returned in  "
                                                    "get_pytorch_variables")

    def test_save_results(self):
        self.experiment.add_result(0, "test")
        self.experiment.save_results("results-test.json")

        self.assertTrue(os.path.exists(os.path.join(self.experiment.elog.result_dir, "results-test.json")),
                        "result file could not be stored")

        with open(os.path.join(self.experiment.elog.result_dir, "results-test.json"), "r") as f:
            content = f.read()
            self.assertTrue("test" in content and "0" in content, "results content not sucessfully saved")

    def test_save_and_load_checkpoints(self):
        net = Net()
        optim = torch.optim.Adam(params=net.parameters())
        ptvar = torch.tensor(1)
        svar = "test"
        lvar = (1, 2, 3, 4)

        self.experiment.net = net
        self.experiment.optim = optim
        self.experiment.ptvar = ptvar
        self.experiment.svar = svar
        self.experiment.lvar = lvar
        self.experiment.results["test"] = 1

        self.experiment.save_checkpoint(name="test_checkpoint")

        self.experiment.net = Net()
        self.experiment.optim = torch.optim.Adam(params=Net().parameters(), lr=1)
        self.experiment.ptvar = torch.tensor(0)
        self.experiment.svar = "test2"
        self.experiment.lvar = (-1, -2)
        self.experiment.results["test"] = 0

        self.experiment.load_checkpoint(name="test_checkpoint")

        self.assertTrue((list(self.experiment.net.parameters())[0] - list(net.parameters())[0]).sum().item() < 0.00001,
                        "Net could not be restored from checkpoint")
        self.assertTrue(
            self.experiment.optim.state_dict()['param_groups'][0]['lr'] == optim.state_dict()['param_groups'][0]['lr'],
            "optim could not be restored from checkpoint")
        self.assertTrue(self.experiment.ptvar == ptvar.item(), "ptvar could not be restored from checkpoint")
        self.assertTrue(self.experiment.svar == svar, "svar could not be restored from checkpoint")
        self.assertTrue(self.experiment.lvar == lvar, "lvar could not be restored from checkpoint")
        self.assertTrue(self.experiment.results["test"] == 1, "Results could not be restored from checkpoint")

    def test_add_results(self):
        self.experiment.add_result(name="test", value=1)

        self.assertTrue(self.experiment.results["test"] == 1,
                        "Result was not added")
        self.assertTrue(self.experiment.get_result("test") == 1,
                        "Result was not added")

        self.assertTrue(os.path.exists(os.path.join(self.experiment.elog.result_dir, "results-log.json")),
                        "result file could not be stored")

        with open(os.path.join(self.experiment.elog.result_dir, "results-log.json"), "r") as f:
            content = f.read()
        self.assertTrue("test" in content and "1" in content, "results content not sucessfully saved")

    def test_save_tmp_results(self):
        self.experiment.add_result(name="test", value=1)
        self.experiment.run()

        self.assertTrue(os.path.exists(os.path.join(self.experiment.elog.result_dir, "results.json")),
                        "result file could not be stored")

        with open(os.path.join(self.experiment.elog.result_dir, "results.json"), "r") as f:
            content = f.read()
        self.assertTrue("test" in content and "1" in content, "results content not sucessfully temporarily saved")

    def test_save_tmp_checkpoint(self):
        self.experiment.test_var = "test"
        self.experiment.run()

        time.sleep(5)

        self.assertTrue(os.path.exists(os.path.join(self.experiment.elog.checkpoint_dir, "checkpoint_current.pth.tar")),
                        "Temp Checkpoint file could not be stored")
        self.assertTrue(os.path.exists(os.path.join(self.experiment.elog.checkpoint_dir, "checkpoint_last.pth.tar")),
                        "Last Checkpoint file could not be stored")

        self.experiment.test_var = "test2"
        self.experiment.load_checkpoint("checkpoint_current")
        self.assertTrue(self.experiment.test_var == "test",
                        "Temp Checkpoint file loading could not sucessful")
        self.experiment.test_var = "test2"
        self.experiment.load_checkpoint("checkpoint_last")
        self.assertTrue(self.experiment.test_var == "test",
                        "Temp Checkpoint file loading could not sucessful")

    def test_save_exp_info(self):
        self.experiment.run()

        with open(os.path.join(self.experiment.elog.config_dir, "exp.json"), "r") as f:
            exp_info = json.load(f)

        self.assertTrue(exp_info['epoch'] == 10, "Epoch not sucessfully stored in exp info")
        self.assertTrue(exp_info['name'] == 'test_experiment', "Name not sucessfully stored in exp info")
        self.assertTrue(exp_info['state'] == 'Trained', "State not sucessfully stored as 'Trained' in exp info")

    def test_print(self):
        self.experiment.print("0o zD 0o")
        with open(os.path.join(self.experiment.elog.log_dir, "default.log"), "r") as f:
            content = f.read()
        self.assertTrue("0o zD 0o" in content, "Print not sucessfully saved")

    def test_resume(self):
        # TODO

        self.cntr = []
        self.experiment.train = lambda epoch: self.cntr.append(0)
        self.experiment.end = lambda: time.sleep(2)
        self.experiment.run()
        self.assertTrue(len(self.cntr) == 10, "Did not call train for the right number of epochs")

        exp2 = PytorchExperiment(name="test-exp2", base_dir=self.test_dir, resume=self.experiment.elog.work_dir)
        exp3 = PytorchExperiment(name="test-exp2", base_dir=self.test_dir, resume=self.experiment.elog.work_dir,
                                 resume_reset_epochs=False)

        exp2.prepare_resume()
        exp3.prepare_resume()

        self.assertTrue(exp2.exp_name == 'test_experiment', "Did not restore exp_name")
        self.assertTrue(exp2._epoch_idx == 0, "Did not reset epochs")
        self.assertTrue(exp3._epoch_idx == 10, "Did reset epochs")

        exp2.train = lambda epoch: self.cntr.append(0)
        exp3.train = lambda epoch: self.cntr.append(0)
        exp2.run()
        self.assertTrue(len(self.cntr) == 20, "Did call not train for exp2")
        exp3.run()
        self.assertTrue(len(self.cntr) == 20, "Did call train for exp3")

        exp2._exp_state = "Ended"
        exp3._exp_state = "Ended"

class Net(nn.Module):
    """
    Small network to test save/load functionality
    """

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

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

max_stars_repo_name: georgepar/trixi

max_issues_repo_name: georgepar/trixi

max_forks_repo_name: georgepar/trixi


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Jan  2 13:04:57 2018

@author: pscog
"""

import pandas as pd
import numpy as np
import patsy
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pickle

import pystan
import ranking as rk

import seaborn as sns

import pdb

def get_cond_idx(conds):
    return dict(zip(conds, range(len(conds))))

def plot_raw_data_complex(conds, cond_plot, stan_data, ax = None, color = '#E4F1FE'):
    """Plot the propotion of subjects who chose each possible ranking"""

    if ax == None:

        ax = plt.gca()

    cond_idx = get_cond_idx(conds)
    N_conds = stan_data['C']

    X_rep = np.diag(np.ones(N_conds))

    cond_data = y[np.all(X == X_rep[cond_idx[cond_plot]], axis = -1)]

    bord_count = rk.borda_count(cond_data)

#    bord_count_idx = bord_count - 1

    tdist = rk.kendall_tau_dist_vec(np.tile(bord_count, (cond_data.shape[0],1)), cond_data)

    ax.hist(tdist, normed = True, rwidth = 0.8, color = '#FFC0CB')

    return

def plot_bayesian_aggregate_complex(mean_rank, all_ranks, conds, cond_plot, ax = None):

    if ax == None:

        ax = plt.gca()

    cond_idx = get_cond_idx(conds)

    est_True_mean = mean_rank[cond_idx[cond_plot]]

    tau_dist = rk.kendall_tau_dist_vec(np.tile(est_True_mean, (all_ranks.shape[0],1)), all_ranks[:, cond_idx[cond_plot]])

    tau_bins = rk.bin_taus(tau_dist, all_ranks.shape[-1])
    tau_bins_0 = [float(i) for i in tau_bins[0]]
    width = np.diff(tau_bins_0).min()

    colors = np.repeat('#E4F1FE', len(tau_bins[0]))

    ax.bar(tau_bins_0, tau_bins[1] / tau_bins[1].sum(), width, color = colors, edgecolor = ['k' for i in range(len(tau_bins[0]))])

#    plt.xticks(np.arange(0, 1+0.25, 0.25))
#    ax.hist(tau_dist, normed = True, rwidth = 1, color = '#FFC0CB', alpha = 0.8, cumulative = False)
#    ax.hist(tau_dist, normed = True, rwidth = 1, color = '#22313F', alpha = 0.8, cumulative = True)

    return est_True_mean, tau_dist

def plot_bayesian_contrast(all_ranks, conds, cond1, cond2, ax = None):

    if ax == None:

        ax = plt.gca()

    cond_idx = get_cond_idx(conds)

    comp_taus = rk.kendall_tau_dist_vec(all_ranks[:,cond_idx[cond1]], all_ranks[:,cond_idx[cond2]])

    comp_bins = rk.bin_taus(comp_taus, K)

    colors = np.repeat('#E4F1FE', len(comp_bins[0]))

    ax.bar(comp_bins[0], comp_bins[1] / comp_bins[1].sum(),
                  color = colors, edgecolor = ['k' for i in range(len(comp_bins[0]))])

def plot_bayesian_agreement(all_ranks, conds, cond, ax = None):

    if ax == None:

        ax = plt.gca()
    cond_idx = get_cond_idx(conds)
    X_rep = np.diag(np.ones(N_conds))

    X_rep == X_rep[cond_idx[cond]]

    cond_mask = np.all(X == X_rep[cond_idx[cond]], axis = -1)

    z_rep_cond = z_rank[:, cond_mask]

    all_z_tau = np.empty(z_rep_cond.shape[:2])

    for part in range(z_rep_cond.shape[1]):
        all_z_tau[:, part] = rk.kendall_tau_dist_vec(z_rep_cond[:,part], all_ranks[:, cond_idx[cond]])

    ax.hist(all_z_tau.mean(1), color = '#E4F1FE', rwidth = 0.85, normed = True)

if __name__ == '__main__':

    #Set plotting preferences
    sns.set_context("paper")
    sns.set_style("whitegrid")

    #Load and preprocess data
    data = pd.read_csv("sim_data_complex.csv")

    conds = data['Condition'].unique()
    N_conds = len(conds)
    X = patsy.dmatrix("~0+Condition", data = data)
    y = data[[i for i in data.columns if 'Y' in i]].values

    part = pd.factorize(data['Subject'])

    N = y.shape[0] #Number of data points
    K = y.shape[1] #Number of items to rank
    C = X.shape[1] #Number of predictors (or number of conditions)
    J = np.unique(part[0]).shape[0] #Number of participants

    stan_data = {'N': N, 'K': K, 'C': C, 'J': J, 'X': X, 'y': y, 'rater': part[0] + 1, 'beta_sd_prior': C*3,
                 'scale_sd_prior': 0.5, 'rep_data': 1} #Stan data dictionary

    resample = False
    ##Fit the stan model (This could be done in RStan or any other stan interface is required)
    try:
        with open("thurstonian.pkl", 'rb') as f:
            sm = pickle.load(f)
    except:
        sm = pystan.StanModel(file="thurstonian_cov.stan")
        with open("thurstonian.pkl", 'wb') as f:
            pickle.dump(sm, f)

    if resample:
        fit = sm.sampling(data=stan_data, iter=4000, chains=4, n_jobs = 1)
        la = fit.extract(permuted = True)
        with open("la_complex.pkl", 'wb') as f:
            pickle.dump(la, f)

    else:
        with open("la_complex.pkl", 'rb') as f:
            la = pickle.load(f)

    #Extract mcmc samples for the parameters of interest
    beta = la['beta']
    z_rank = la['z_rank']

    ##Post process and visualization
    mean_rank = rk.rank(beta.mean(0), axis = -1) #Get the posterior mean rank
    all_ranks = rk.rank(beta, axis = -1) #Rank all the beta samples

#
#
#    ###Main plots
#
#    ##Plot the raw data
    cond_order = ['C1', 'C2', 'C3']
#
#    fig, ax = plt.subplots(1, len(conds), sharex = True, sharey = True, figsize = (6, 2.5))
#
#    for i, c in enumerate(cond_order):
#        print(i)
#        plot_raw_data_complex(conds, c, stan_data, ax = ax[i])
#        ax[i].set_xlim([0,1])

#        borda_text = "-".join(map(str, borda_count))
#        ax[i].text(0.02, 0.88, "Borda: {}".format("-".join(map(str, borda_count))), transform=ax[i].transAxes)

#        ax[i].set_title("Condition = {}".format(c))
#        plt.sca(fig.axes[i])
#        plt.ylim([0,1])
#        plt.xticks(rotation=60)
#
#        ax[i].set_xlabel("Ranking")
#        ax[i].set_ylabel("Proportion Ranking")
#
#    plt.tight_layout()

#
#    #Plot the mcmc samples
    sns.set_style("white")
    fig, ax = plt.subplots(1, len(conds), sharex = True, sharey = True, figsize = (6, 2))

    for i, c in enumerate(cond_order):

        T_mean, tdist = plot_bayesian_aggregate_complex(mean_rank, all_ranks, conds, c, ax = ax[i])

        ax[i].xaxis.set_major_locator(ticker.MultipleLocator(0.25))
        ax[i].set_xlim([0, 1])
        sns.despine()

        confidence_interval = np.sort(tdist)[int(tdist.shape[0] * 0.95)]
        ax[i].fill_between([0, confidence_interval], 0, 0.4, color = 'r', alpha = 0.25)
#        ax[i].errorbar(confidence_interval/2, 0.005, xerr = confidence_interval/2, fmt='none', color = 'k', elinewidth = 2)

#        text = "-".join(map(str, T_mean))
##        ax[i].text(0.02, 0.88, "Mean est: {}".format(text), transform=ax[i].transAxes)
##        plt.sca(fig.axes[i])
##        plt.xticks(rotation=60)
##        plt.ylim([0,1])
        ax[i].set_title("Condition = {}".format(c))
        ax[i].set_xlabel("Kendall Tau")
        ax[i].set_ylabel("P(Kendall Tau)")
##
    plt.tight_layout()

    sns.set_style("whitegrid")
    #Plot agreement with the aggregate rank
    fig, ax = plt.subplots(1, len(conds), sharex = True, sharey = True, figsize = (6, 2.5))

    for i, c in enumerate(cond_order):
        plot_bayesian_agreement(all_ranks, conds, c, ax = ax[i])
        ax[i].set_title("Condition = {}".format(c))
        ax[i].axvline(0.5, linestyle = '--', color = 'k')

    ax[0].set_xlim([0, 1])

    plt.tight_layout()
#    #Comparision betweeen conditions
#    fig, ax = plt.subplots(1, 1, sharex= True, sharey = True, figsize = (2.5, 2))

#    plot_bayesian_contrast(all_ranks, conds, "C1", "C2", ax = ax)

#    plt.ylim([0,1])
#    ax.set_title("Contrast: {} vs {}".format("Real", "Sim1"))
#    #ax[1].set_title("{} vs {}".format("Real", "Sim2"))
#    #ax[2].set_title("{} vs {}".format("Sim1", "Sim2"))
#
#    ax.set_xlabel("Kendall Tau Distance")
#    ax.set_ylabel("P(Kendal Tau Distance)")
#    plt.tight_layout()
#     #Comparison plots
#

max_issues_repo_name: OscartGiles/PyThurstonian

max_forks_repo_name: OscartGiles/PyThurstonian


text:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('../../images/owl.jpg')

# Computes the median of all the pixeds under that kernel window and the central
# pixel is replaced with this median value.
# Effective in removing salt-and-pepper noise
# Note: for Gaussian and box filters, the filtered value may not exist in the Original
# image but for the median filtering, the central element will always be replaced
# with some pixel value in the image
# Param 1: Kernel size (positive and odd)
dst = cv2.medianBlur(img, 5)

plt.subplot(121), plt.imshow(img), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img), plt.title('Median Blur')
plt.xticks([]), plt.yticks([])
plt.show()

max_stars_repo_name: vibinash/vision

max_issues_repo_name: vibinash/vision

max_forks_repo_name: vibinash/vision


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 12 00:10:53 2020

@author: Axel Blaze
"""
import tkinter
from tkinter import *
import numpy as np
import pandas as pd
import os
from sklearn import model_selection
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
df=pd.read_excel(r'source.xlsx')
X_train=df.iloc[:,0:19]
y_train=df.iloc[:,19]
model = DecisionTreeClassifier()
num_trees = 100
model = BaggingClassifier(base_estimator=model, n_estimators=num_trees, random_state=0)
model.fit(X_train,y_train)
def test():
    ch=[]
    ar=[]
    i1=v1.get()
    ch.append(i1)
    i2=v2.get()
    ch.append(i2)
    i3=v3.get()
    ch.append(i3)
    i4=v4.get()
    ch.append(i4)
    i5=v5.get()
    ch.append(i5)
    i6=v6.get()
    ch.append(i6)
    i7=v7.get()
    ch.append(i7)
    i8=v8.get()
    ch.append(i8)
    i9=v9.get()
    ch.append(i9)
    i10=v10.get()
    ch.append(i10)
    i11=v11.get()
    ch.append(i11)
    i12=v12.get()
    ch.append(i12)
    i13=v13.get()
    ch.append(i13)
    i14=v14.get()
    ch.append(i14)
    i15=v15.get()
    ch.append(i15)
    i16=v16.get()
    ch.append(i16)
    i17=v17.get()
    ch.append(i17)
    i18=v18.get()
    ch.append(i18)
    i19=v19.get()
    ch.append(i19)
    ar=pd.DataFrame(ch)
    ar.to_csv(r'test.csv',index=False)
    t=pd.read_csv(r'test.csv')
    x=t.iloc[0:19,0]
    X_test=np.vstack((x,x))
    y_pred = model.predict(X_test)
    os.remove(r'test.csv')
    if y_pred[0]==0:
        l20=Label(Login,text="Result:",font=("times new roman",12,"bold"),bg="cyan").grid(row=20,column=0,padx=20)
        r=Entry(Login,bd=5,textvariable=v20,relief=GROOVE,font=("times new roman",12),fg="green").grid(row=20,column=1,padx=20) #19
        v20.set("It's Normal")
        l20.destroy()
        r.destroy()

    else:
        l20=Label(Login,text="Result:",font=("times new roman",12,"bold"),bg="cyan").grid(row=20,column=0,padx=20)
        r=Entry(Login,bd=5,textvariable=v20,relief=GROOVE,font=("times new roman",12),fg="red").grid(row=20,column=1,padx=20) #19
        v20.set("Depression Detected")
        messagebox.showinfo(message="Depression Detected",title="Result")

def demo():
    v1.set("1")
    v2.set("0.000161")
    v3.set("0.00000154")
    v4.set("0.00359")
    v5.set("0.00276")
    v6.set("0.9963")
    v7.set("0.00347")
    v8.set("0.9963")
    v9.set("0.9291")
    v10.set("0.9918")
    v11.set("0.9990")
    v12.set("0.0038")
    v13.set("0.0002")
    v14.set("0.0060")
    v15.set("0.0185")
    v16.set("0.9808")
    v17.set("0.0190")
    v18.set("0.980852")
    v19.set("0.5298")

def reset():
    v1.set("")
    v2.set("")
    v3.set("")
    v4.set("")
    v5.set("")
    v6.set("")
    v7.set("")
    v8.set("")
    v9.set("")
    v10.set("")
    v11.set("")
    v12.set("")
    v13.set("")
    v14.set("")
    v15.set("")
    v16.set("")
    v17.set("")
    v18.set("")
    v19.set("")
    v20.set("")

def demo2():
    v1.set("0.821576")
    v2.set("0.249583")
    v3.set("0.00000289")
    v4.set("0.023302")
    v5.set("0.226832")
    v6.set("0.784104")
    v7.set("0.215857")
    v8.set("0.78406")
    v9.set("0.929139")
    v10.set("0.998472")
    v11.set("0.840509")
    v12.set("0.22268")
    v13.set("0.00000293")
    v14.set("0.055637")
    v15.set("0.297828")
    v16.set("0.704763")
    v17.set("0.295206")
    v18.set("0.704763")
    v19.set("0.526313")
    v20.set("")
root=Tk()
root.geometry("1160x774")
root.title("Detect of MDD using Signal Data")
root.resizable(0,0)
back=PhotoImage(file="signal.png")
v1=StringVar()
v2=StringVar()
v3=StringVar()
v4=StringVar()
v5=StringVar()
v6=StringVar()
v7=StringVar()
v8=StringVar()
v9=StringVar()
v10=StringVar()
v11=StringVar()
v12=StringVar()
v13=StringVar()
v14=StringVar()
v15=StringVar()
v16=StringVar()
v17=StringVar()
v18=StringVar()
v19=StringVar()
v20=StringVar()
v21=StringVar()
title=Label(root,text="                                  Detection Of MDD Using Signals                                ",font=("lucida calligraphy",20,"bold"),bg="#213970",fg="#F49F1C",bd=10,relief=GROOVE)
title.place(x=0,y=0,relwidth=2)
L=Label(root,image=back,font=("arial",15))
Login=Frame(root,bg='cyan')
Login.place(x=400, y=70)
l1=Label(Login,text="Channel 1:",font=("times new roman",12,"bold"),bg="cyan").grid(row=1,column=0,padx=20)
l2=Label(Login,text="Channel 2:",font=("times new roman",12,"bold"),bg="cyan").grid(row=2,column=0,padx=20)
l3=Label(Login,text="Channel 3:",font=("times new roman",12,"bold"),bg="cyan").grid(row=3,column=0,padx=20)
l4=Label(Login,text="Channel 4:",font=("times new roman",12,"bold"),bg="cyan").grid(row=4,column=0,padx=20)
l5=Label(Login,text="Channel 5:",font=("times new roman",12,"bold"),bg="cyan").grid(row=5,column=0,padx=20)
l6=Label(Login,text="Channel 6:",font=("times new roman",12,"bold"),bg="cyan").grid(row=6,column=0,padx=20)
l7=Label(Login,text="Channel 7:",font=("times new roman",12,"bold"),bg="cyan").grid(row=7,column=0,padx=20)
l8=Label(Login,text="Channel 8:",font=("times new roman",12,"bold"),bg="cyan").grid(row=8,column=0,padx=20)
l9=Label(Login,text="Channel 9:",font=("times new roman",12,"bold"),bg="cyan").grid(row=9,column=0,padx=20)
l10=Label(Login,text="Channel 10:",font=("times new roman",12,"bold"),bg="cyan").grid(row=10,column=0,padx=20)
l11=Label(Login,text="Channel 11:",font=("times new roman",12,"bold"),bg="cyan").grid(row=11,column=0,padx=20)
l12=Label(Login,text="Channel 12:",font=("times new roman",12,"bold"),bg="cyan").grid(row=12,column=0,padx=20)
l13=Label(Login,text="Channel 13:",font=("times new roman",12,"bold"),bg="cyan").grid(row=13,column=0,padx=20)
l14=Label(Login,text="Channel 14:",font=("times new roman",12,"bold"),bg="cyan").grid(row=14,column=0,padx=20)
l15=Label(Login,text="Channel 15:",font=("times new roman",12,"bold"),bg="cyan").grid(row=15,column=0,padx=20)
l16=Label(Login,text="Channel 16:",font=("times new roman",12,"bold"),bg="cyan").grid(row=16,column=0,padx=20)
l17=Label(Login,text="Channel 17:",font=("times new roman",12,"bold"),bg="cyan").grid(row=17,column=0,padx=20)
l18=Label(Login,text="Channel 18:",font=("times new roman",12,"bold"),bg="cyan").grid(row=18,column=0,padx=20)
l19=Label(Login,text="Channel 19:",font=("times new roman",12,"bold"),bg="cyan").grid(row=19,column=0,padx=20)

c1=Entry(Login,bd=5,textvariable=v1,relief=GROOVE,font=("times new roman",12)).grid(row=1,column=1,padx=20) #1
c2=Entry(Login,bd=5,textvariable=v2,relief=GROOVE,font=("times new roman",12)).grid(row=2,column=1,padx=20) #2
c3=Entry(Login,bd=5,textvariable=v3,relief=GROOVE,font=("times new roman",12)).grid(row=3,column=1,padx=20) #3
c4=Entry(Login,bd=5,textvariable=v4,relief=GROOVE,font=("times new roman",12)).grid(row=4,column=1,padx=20) #4
c5=Entry(Login,bd=5,textvariable=v5,relief=GROOVE,font=("times new roman",12)).grid(row=5,column=1,padx=20) #5
c6=Entry(Login,bd=5,textvariable=v6,relief=GROOVE,font=("times new roman",12)).grid(row=6,column=1,padx=20) #6
c7=Entry(Login,bd=5,textvariable=v7,relief=GROOVE,font=("times new roman",12)).grid(row=7,column=1,padx=20) #7
c8=Entry(Login,bd=5,textvariable=v8,relief=GROOVE,font=("times new roman",12)).grid(row=8,column=1,padx=20) #8
c9=Entry(Login,bd=5,textvariable=v9,relief=GROOVE,font=("times new roman",12)).grid(row=9,column=1,padx=20) #9
c10=Entry(Login,bd=5,textvariable=v10,relief=GROOVE,font=("times new roman",12)).grid(row=10,column=1,padx=20) #10
c11=Entry(Login,bd=5,textvariable=v11,relief=GROOVE,font=("times new roman",12)).grid(row=11,column=1,padx=20) #11
c12=Entry(Login,bd=5,textvariable=v12,relief=GROOVE,font=("times new roman",12)).grid(row=12,column=1,padx=20) #12
c13=Entry(Login,bd=5,textvariable=v13,relief=GROOVE,font=("times new roman",12)).grid(row=13,column=1,padx=20) #13
c14=Entry(Login,bd=5,textvariable=v14,relief=GROOVE,font=("times new roman",12)).grid(row=14,column=1,padx=20) #14
c15=Entry(Login,bd=5,textvariable=v15,relief=GROOVE,font=("times new roman",12)).grid(row=15,column=1,padx=20) #15
c16=Entry(Login,bd=5,textvariable=v16,relief=GROOVE,font=("times new roman",12)).grid(row=16,column=1,padx=20) #16
c17=Entry(Login,bd=5,textvariable=v17,relief=GROOVE,font=("times new roman",12)).grid(row=17,column=1,padx=20) #17
c18=Entry(Login,bd=5,textvariable=v18,relief=GROOVE,font=("times new roman",12)).grid(row=18,column=1,padx=20) #18
c19=Entry(Login,bd=5,textvariable=v19,relief=GROOVE,font=("times new roman",12)).grid(row=19,column=1,padx=20) #19

#r=Entry(Login,bd=5,textvariable=v21,relief=GROOVE,font=("times new roman",12),fg="red",state=DISABLED).grid(row=20,column=1,padx=20) #19
B2=Button(root,text="Click for results",font=("Lucida Calligraphy",14),fg="white",bg="purple",relief=GROOVE,command=test)
B2.pack()
B2.place(x=877,y=183)
B3=Button(root,text="Reset",font=("Bodoni MT",14),fg="white",bg="purple",relief=GROOVE,command=reset)
B3.pack()
B3.place(x=935,y=600)
B4=Button(root,text="Click Here For\n Demo Values",font=("Bodoni MT",14),fg="white",bg="purple",relief=GROOVE,command=demo)
B4.pack()
B4.place(x=900,y=525)
#B5=Button(root,text="Click Here For\n Demo Values",font=("Bodoni MT",14),fg="white",bg="purple",relief=GROOVE,command=demo2)
#B5.pack()
lblRules = Label(
    root,
    text = "The System predicts whether a person\n is suffering from Depression or not\n by using the signal data of 19 channels.\n This system is trained under theta band \npower which has the frequency of 4-8Hz.\n You just have to enter the signal data of\n all 19 channels in theta band power and\n click on the results  ",
    width = 35,
    font = ("Times",14),
    background = "#000000",
    foreground = "#FACA2F",
)
lblRules.pack()
lblRules.place(x=775,y=300)
title.pack()
L.pack()

root.mainloop()


text:
#!/usr/bin/env python

##
#  Retrieve the latlons and retrieve the material properties
#

import getopt
import sys
import subprocess
import struct
import numpy as np
import pdb

dimension_x = 27
dimension_y = 8
dimension_z = 101

lon_origin = -124.6472
lat_origin = 32.7

lon_upper = -116.84720
lat_upper = 34.8

delta_lon = (lon_upper - lon_origin )/(dimension_x-1)
delta_lat = (lat_upper - lat_origin)/(dimension_y-1)

def usage():
    print("\n./query_data_files.py\n\n")
    sys.exit(0)

def myrange(start, end, step):
    while start <= end:
        yield start
        start += step

def main():

    count =0

    f_lonlat = open("./test_lonlat.txt")

    f_vp = open("./alba/vp.dat")
    f_vs = open("./alba/vs.dat")
    f_rho = open("./alba/rho.dat")

    vp_arr = np.fromfile(f_vp, dtype=np.float32)
    vs_arr = np.fromfile(f_vs, dtype=np.float32)
    rho_arr = np.fromfile(f_rho, dtype=np.float32)

    f_vp.close()
    f_vs.close()
    f_rho.close()

    lon_start = lon_origin
    lat_start = lat_origin
    depth_start = 0.0;

    for line in f_lonlat:
        arr = line.split()
        lon_v = float(arr[0])
        lat_v = float(arr[1])
        depth_v = float(arr[2])

        y_pos = int(round((lat_v - lat_origin) / delta_lat))
        x_pos = int(round((lon_v - lon_origin) / delta_lon))
        z_pos = int(depth_v/1000)

        offset=z_pos * (dimension_y * dimension_x) + (y_pos * dimension_x) + x_pos
        vp=vp_arr[offset];
        vs=vs_arr[offset];
        rho=rho_arr[offset];

        print (">>", x_pos," ",y_pos," ",z_pos," >> ", lon_v, " ",lat_v, " ", float(depth_v) , "-->vp", vp," vs", vs," rho", rho)
        print ("offset ",offset)

    f_lonlat.close()
    print("\nDone!")

if __name__ == "__main__":
    main()

max_stars_repo_name: SCECcode/albacore

max_issues_repo_name: SCECcode/albacore

max_forks_repo_name: SCECcode/albacore


text:
#!/usr/bin/env python

#===============================================================================
#    Copyright 2017 Geoscience Australia
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#===============================================================================
'''
Functions to obtain edge points and convex hull vertices from a gridded NetCDF dataset

Created on 12Sep.,2016

@author: Alex Ip
'''
import numpy as np
import math
from scipy import ndimage
import shapely.geometry as geometry
from shapely.ops import cascaded_union, polygonize
from scipy.spatial import Delaunay
from ._array_pieces import array_pieces
from skimage import filters

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # Initial logging level for this module

def get_grid_edge_points(grid_array, dimension_ordinates, nodata_value, max_bytes=None):
    '''
    Function to return a list of coordinates corresponding to pixels on the edge of data-containing areas of the NetCDF dataset
    Data is retrieved in pieces, and pieces around external edge of the full array are padded outwards with no-data to ensure
    that edges are detected when data runs all the way to the array edge.
    @param grid_array: 2D netCDF4 dataset variable
    @param dimension_ordinates: tuple of two arrays containing ordinates for grid_array (e.g. lat/lons)
    @param nodata_value: Value in grid array representing null value
    @param max_bytes: Maximum number of bytes to retrieve in each array piece
    @return edge_points: n x 2 array of edge point coordinates
    '''
    assert len(grid_array.shape) == 2, 'grid_array is not 2D'

    edge_point_list = []  # Complete list of edge points (unknown length)
    for piece_array, array_offset in array_pieces(grid_array, max_bytes=max_bytes):
        dimension_subset = [dimension_ordinates[dim_index][array_offset[dim_index]:array_offset[
            dim_index] + piece_array.shape[dim_index]] for dim_index in range(2)]

        # Convert masked array to plain array
        if isinstance(piece_array, np.ma.core.MaskedArray):
            piece_array = piece_array.data

        unpadded_piece_shape = piece_array.shape
        #logger.debug('array_offset = {}, unpadded_piece_shape = {}, piece_array.size = {}'.format(
        #    array_offset, unpadded_piece_shape, piece_array.size))

        # Convert to padded boolean True=data/False=no-data array
        piece_array = np.pad((piece_array != nodata_value), pad_width=1, mode='constant', constant_values=False)

        piece_slices = tuple([slice(*[
            0 if array_offset[dim_index] == 0 else 1, # Trim padding away if not on lower limit
            piece_array.shape[dim_index]-1 # Trim off higher end padding
            ])
            for dim_index in range(2)
            ])

        #logger.debug(piece_slices)
        #logger.debug('piece_array = {}'.format(piece_array))
        piece_array = piece_array[piece_slices] # Trim away padding on internal edges if required
        #logger.debug('piece_array = {}'.format(piece_array))

        # Set upper edges to false to get complete edge detection
        piece_array[-1,:] = False
        piece_array[:,-1] = False
        #logger.debug('piece_array = {}'.format(piece_array))

        edge_mask = (ndimage.filters.maximum_filter(piece_array, size=2, mode='constant', cval=False) !=
                     ndimage.filters.minimum_filter(piece_array, size=2, mode='constant', cval=False))

        #edge_mask = filters.sobel(grid_array).astype(np.bool)

        #logger.debug('edge_mask = {}'.format(edge_mask))

        # Detect indices of data/no-data edge points in an n x 2 array
        edge_point_indices = np.transpose(np.array(np.where(edge_mask)))

        #logger.debug('{}\n{}'.format(edge_point_indices.shape, edge_point_indices))

        # Remove index offset introduced by padding lower edges
        for dim_index in range(2):
            if array_offset[dim_index] == 0:
                #logger.debug('removing offset for dimension {}'.format(dim_index))
                edge_point_indices[:,dim_index] = edge_point_indices[:,dim_index] - 1

        #logger.debug('edge_point_indices.shape = {}\nedge_point_indices = {}'.format(edge_point_indices.shape, edge_point_indices))

        # Discard any false edge points detected on inner piece edges
        edge_point_indices = edge_point_indices[np.where(np.all((edge_point_indices < unpadded_piece_shape), axis = 1))]

        #logger.debug('edge_point_indices.shape = {}\nedge_point_indices = {}'.format(edge_point_indices.shape, edge_point_indices))

        if edge_point_indices.shape[0]: # If any edge points found
            piece_edge_points = np.zeros(edge_point_indices.shape, dimension_ordinates[0].dtype)
            #logger.debug('piece_edge_points = {}'.format(piece_edge_points))
            # TODO: Do something more general here to account for YX or XY dimension order - this is for YX only
            pixel_offset = [0.0, 0.0]
            for dim_index in range(2):
                piece_edge_points[:,1-dim_index] = dimension_subset[dim_index][edge_point_indices[:,dim_index]]
                pixel_offset[1-dim_index] = (dimension_subset[dim_index][1] - dimension_subset[dim_index][0]) / 2.0

            #logger.debug('pixel_offset = {}'.format(pixel_offset))
            #logger.debug('piece_edge_points = {}'.format(piece_edge_points))
            piece_edge_points = piece_edge_points - np.array(pixel_offset) # Apply half pixel offset
            #logger.debug('offset piece_edge_points = {}'.format(piece_edge_points))
            edge_point_list.append(piece_edge_points)
            #logger.debug('{} edge points found for piece'.format(piece_edge_points.shape[0]))
        #else:
        #    logger.debug('No edge points found for piece')

    return np.concatenate(edge_point_list, axis=0)

def get_netcdf_edge_points(netcdf_dataset, max_bytes=None):
    '''
    Function to return a list of coordinates corresponding to pixels on the edge of data-containing areas of the NetCDF dataset
    @param netcdf_dataset: netCDF4.Dataset object
    @param max_bytes: Maximum number of bytes to retrieve in each array piece
    '''
    # Find variable with "grid_mapping" attribute - assumed to be 2D data
    # variable
    try:
        data_variable = [variable for variable in netcdf_dataset.variables.values(
        ) if hasattr(variable, 'grid_mapping')][0]
    except:
        raise Exception(
            'Unable to determine data variable (must have "grid_mapping" attribute')
# print 'Variable %s has shape %s' % (data_variable.name,
# data_variable.shape)

    assert len(data_variable.dimensions) == 2, '%s is not 2D' % data_variable.name
    dimension_ordinates = [netcdf_dataset.variables[
        data_variable.dimensions[dim_index]] for dim_index in range(2)]
    nodata_value = data_variable._FillValue

    return get_grid_edge_points(data_variable, dimension_ordinates, nodata_value, max_bytes)

def points2convex_hull(point_list, dilation=0, tolerance=0):
    '''
    Function to return a list of vertex coordinates in the convex hull around data-containing areas of a point list
    @param point_list: Iterable containing coordinates from which to compute convex hull
    @param dilation: distance to dilate convex hull
    @param tolerance: distance tolerance for the simplification of the convex hull
    '''
    convex_hull = geometry.MultiPoint(point_list).convex_hull

    # Offset outward by specified dilation and simplify with specified
    # tolerance
    if dilation != 0:
        convex_hull = convex_hull.buffer(
            dilation, cap_style=2, join_style=2, mitre_limit=tolerance)

    if tolerance != 0:
        convex_hull = convex_hull.simplify(tolerance)

    # Convert polygon to list
    try:
        return [coordinates for coordinates in convex_hull.exterior.coords]
    except AttributeError:
        return [coordinates for coordinates in convex_hull.coords]

def netcdf2convex_hull(netcdf_dataset, max_bytes=None):
    '''
    Function to return a list of vertex coordinates in the convex hull around data-containing areas of the NetCDF dataset
    @param netcdf_dataset: netCDF4.Dataset object
    @param max_bytes: Maximum number of bytes to retrieve in each array piece
    '''
    # Find variable with "GeoTransform" attribute - assumed to be grid mapping
    # variable
    try:
        grid_mapping_variable = [variable for variable in netcdf_dataset.variables.values(
        ) if hasattr(variable, 'GeoTransform')][0]
    except:
        raise Exception(
            'Unable to determine grid mapping variable (must have "GeoTransform" attribute)')
    GeoTransform = [float(
        number) for number in grid_mapping_variable.GeoTransform.strip().split(' ')]
    avg_pixel_size = (abs(GeoTransform[1]) + abs(GeoTransform[5])) / 2.0

    return points2convex_hull(get_netcdf_edge_points(
        netcdf_dataset, max_bytes), avg_pixel_size, avg_pixel_size)

#=========================================================================
# def netcdf2concave_hull(netcdf_dataset, max_bytes=None):
#     '''
#     Function to return a list of vertex coordinates in the convex hull around data-containing areas of the NetCDF dataset
#     @param netcdf_dataset: netCDF4.Dataset object
#     @param max_bytes: Maximum number of bytes to retrieve in each array piece
#     '''
#     # Find variable with "GeoTransform" attribute - assumed to be grid mapping variable
#     try:
#         grid_mapping_variable = [variable for variable in netcdf_dataset.variables.values() if hasattr(variable, 'GeoTransform')][0]
#     except:
#         raise Exception('Unable to determine grid mapping variable (must have "GeoTransform" attribute')
#     GeoTransform = [float(number) for number in grid_mapping_variable.GeoTransform.strip().split(' ')]
#     avg_pixel_size = (abs(GeoTransform[1]) + abs(GeoTransform[5])) / 2.0
#
#     edge_points = get_edge_points(netcdf_dataset, max_bytes)
#
#     #TODO: Compute alpha value dynamically from point density
#     alpha = 1
#
#     return points2alpha_shape(edge_points, alpha, avg_pixel_size, avg_pixel_size)
#=========================================================================

def points2alpha_shape(points, alpha, dilation=0, tolerance=0):
    """
    Compute the alpha shape (concave hull) of a set of points.
    source: http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/
    @param points: Iterable container of points.
    @param alpha: alpha value to influence the gooeyness of the border. Smaller numbers don't fall inward as much as larger numbers. Too large, and you lose everything!
    @param dilation: distance to dilate convex hull
    @param tolerance: distance tolerance for the simplification of the convex hull
    """
    if len(points) < 4:
        # When you have a triangle, there is no sense
        # in computing an alpha shape.
        return geometry.MultiPoint(list(points)).convex_hull

    def add_edge(edges, edge_points, coords, i, j):
        """
        Add a line between the i-th and j-th points,
        if not in the list already
        """
        if (i, j) in edges or (j, i) in edges:
            # already added
            return
        edges.add((i, j))
        edge_points.append(coords[[i, j]])

    coords = np.array([point.coords[0]
                       for point in points])
    tri = Delaunay(coords)
    edges = set()
    edge_points = []
    # loop over triangles:
    # ia, ib, ic = indices of corner points of the
    # triangle
    for ia, ib, ic in tri.vertices:
        pa = coords[ia]
        pb = coords[ib]
        pc = coords[ic]
        # Lengths of sides of triangle
        a = math.sqrt((pa[0] - pb[0])**2 + (pa[1] - pb[1])**2)
        b = math.sqrt((pb[0] - pc[0])**2 + (pb[1] - pc[1])**2)
        c = math.sqrt((pc[0] - pa[0])**2 + (pc[1] - pa[1])**2)
        # Semiperimeter of triangle
        s = (a + b + c) / 2.0
        try:
            # Area of triangle by Heron's formula
            area = math.sqrt(s * (s - a) * (s - b) * (s - c))
            try:
                circum_r = a * b * c / (4.0 * area)
                # Here's the radius filter.
                # print circum_r
                if circum_r < 1.0 / alpha:
                    add_edge(edges, edge_points, coords, ia, ib)
                    add_edge(edges, edge_points, coords, ib, ic)
                    add_edge(edges, edge_points, coords, ic, ia)
            except ZeroDivisionError:
                pass
        except ValueError:
            pass

    m = geometry.MultiLineString(edge_points)
    triangles = list(polygonize(m))
    concave_hull = cascaded_union(triangles)

    # Offset outward by specified dilation and simplify with specified
    # tolerance
    concave_hull = concave_hull.buffer(
        dilation, cap_style=2, join_style=2, mitre_limit=tolerance).simplify(tolerance)

    # Convert polygon to list
    return [coordinates for coordinates in concave_hull.exterior.coords]

reason: import numpy,from scipy


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

"""Unittests for functional upsample operations."""

import unittest
import jax
import jax.numpy as jn
import numpy as np

import objax

def shaparange(s):
    return jn.arange(np.prod(s), dtype=np.float).reshape(s)

class TestUpsample(unittest.TestCase):
    methods = ['nearest', 'linear', 'bilinear', 'trilinear', 'triangle', 'cubic',
               'bicubic', 'tricubic', 'lanczos3', 'lanczos5']

    def test_upsample2d(self):
        x = shaparange((2, 3, 10, 30))
        shape = x.shape
        for method in self.methods:
            y = objax.functional.core.ops.upsample_2d(x, (2, 3), method)
            output = jax.image.resize(x.transpose([0, 2, 3, 1]),
                                      shape=(shape[0], shape[2] * 2, shape[3] * 3, shape[1]),
                                      method=method).transpose([0, 3, 1, 2])
            self.assertEqual(y.tolist(), output.tolist())

    def test_interpolate(self):
        x = shaparange((1, 3, 2, 3, 10, 30))
        shape = x.shape
        for method in self.methods:
            output = 2
            y = objax.functional.core.ops.interpolate(x, scale_factor=output, mode=method)
            self.assertEqual(y.shape, (shape[0], *(jn.array(shape[1:])) * output))
            output = (2, 2, 2)
            y = objax.functional.core.ops.interpolate(x, scale_factor=output, mode=method)
            self.assertEqual(y.shape, (*shape[:len(shape) - len(output)],
                                       *(jn.array(shape[len(shape) - len(output):]) * jn.array(output))))
            output = (2, 2, 2, 2)
            y = objax.functional.core.ops.interpolate(x, scale_factor=output, mode=method)
            self.assertEqual(y.shape, (*shape[:len(shape) - len(output)],
                                       *(jn.array(shape[len(shape) - len(output):]) * jn.array(output))))
            output = 2
            y = objax.functional.core.ops.interpolate(x, size=output, mode=method)
            self.assertEqual(y.shape, (*shape[:-1], output))
            output = (2, 2, 2)
            y = objax.functional.core.ops.interpolate(x, size=output, mode=method)
            self.assertEqual(y.shape, (*shape[:len(shape) - len(output)], *output))
            output = (2, 2, 2, 2)
            y = objax.functional.core.ops.interpolate(x, size=output, mode=method)
            self.assertEqual(y.shape, (*shape[:len(shape) - len(output)], *output))

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

max_stars_repo_name: kihyuks/objax

max_issues_repo_name: kihyuks/objax

max_forks_repo_name: kihyuks/objax

reason: import numpy,import jax


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 17:37:24 2021

@author: claude_kloeckl
"""

import numpy as np
import random

class UniformNoise(object):
    def __init__(self, action_space, initial_exploration = 0.99, final_exploration = 0.05, decay_rate = 0.999):

        self.action_dim      = action_space.shape[0] # Requires Space with (10,) shape!
        self.low             = action_space.low
        self.high            = action_space.high
        self.distance        = abs(self.low - self.high)

        self.initial_exploration = initial_exploration
        self.final_exploration   = final_exploration
        self.decay_rate = decay_rate

    def reset(self):
        self.state = np.ones(self.action_dim)

    def get_action(self, action, step = 0):

        decay = self.decay_rate ** step
        exploration_probabilty = decay*self.initial_exploration + (1-decay)*self.final_exploration

        # Exploration Probability
        explore_yes = np.random.binomial(1,exploration_probabilty)

        # Unnormalized Uniform Numbers
        noise_list = np.random.uniform(self.low, self.high ,size=self.action_dim) #used self.low/10 before

        #Renormalize
        #sum_noise = noise_list.sum()
        noisy_action = explore_yes * noise_list + (1 - explore_yes) * action

        return noisy_action

# Ornstein-Ulhenbeck Process, Taken from #https://github.com/vitchyr/rlkit/blob/master/rlkit/exploration_strategies/ou_strategy.py
class OUNoise(object):
    def __init__(self, action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=100000):
        self.mu           = mu
        self.theta        = theta
        self.sigma        = max_sigma
        self.max_sigma    = max_sigma
        self.min_sigma    = min_sigma
        self.decay_period = decay_period

        #BiddingMarket_energy_Environment Params
        self.action_dim   = action_space.shape[0]
        self.low          = action_space.low
        self.high         = action_space.high
        # only relevant for Discrete action_space
        if len(self.low) > 3:
            self.low = 0
            self.high = 1

        self.reset()

    def reset(self):
        self.state = np.ones(self.action_dim) * self.mu

    def evolve_state(self):
        x  = self.state
        dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.action_dim)
        self.state = x + dx
        return self.state

    def get_action(self, action, t=0):
        ou_state = self.evolve_state()
        self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t / self.decay_period)
        return np.clip(action + ou_state, self.low, self.high)

class GaussianNoise(object):
    def __init__(self, action_space, mu = 0.0, sigma = 0.1, regulation_coef = 1, decay_rate = 0):

        self.action_dim      = action_space.shape[0]
        self.low             = action_space.low
        self.high            = action_space.high

        self.distance        = abs(self.low - self.high)

        self.decay_rate = decay_rate
        self.regulation_coef = regulation_coef
        self.mu              = mu
        self.sigma           = sigma

        self.reset()

    def reset(self):
        self.state = np.ones(self.action_dim) * self.mu

    def get_action(self, action, step = 0):

        noise_list = np.random.normal(self.mu, self.sigma, self.action_dim)* ((1 - self.decay_rate)**step) * self.regulation_coef

        # What is this!?
        #if ((noise_list)**2)**0.5 < 0.01:
        #    noise_list = np.random.normal(0,0.01,self.action_dim)

        noisy_action = np.clip(action + noise_list, self.low, self.high)

        return noisy_action


text:
# Internal modules
from processing.data_management import load_excel, load_excel, load_document
import processing.preprocessors as pp
from config import config
from graphs import graphs

# External libraries
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
#import geopandas as gpd

from utils import Header, make_dash_table
import pandas as pd
import numpy as np
import pathlib

from dash.dependencies import Input,Output
import json
import docx

#data = load_excel(file_name=config.DATA_FILE)
#gdf = load_geometry(file_name=config.GEO_DATA_FILE)
#geojsdata = pp.shapefile_to_geojson(gdf, list(gdf.index),tolerance=0)
#afghanistan_geometry = load_geometry(file_name=config.GEO_DATA_FILE)

########## TEMPORARY MAP DETAILS ######

mapbox_token = 'pk.eyJ1IjoiY2F3aWUiLCJhIjoiY2s1cGVsN3U3MHVrYTNsbnNpd3pubGR3ZSJ9.5sgCAI1IM9pOmmk4dZeD4Q'

def create_layout(app):
    # Page layouts
    return html.Div(
        [
            html.Div([Header(app)]),
            # page 1
            html.Div(
                [
                    # Row 3
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.Div(html.H5("Appendix", style={"padding-left": "10px"}), className="inner-product2"),
                                    html.Br([]),
                                    html.P(load_document(config.TEXT_FILE),
                                        style={"color": "#000000"},
                                        className="row",
                                    ),
                                ],
                                className="product2",
                            )
                        ],
                        className="row",
                    ),
                    # Row 4
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                     # Row 5
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                ],
                className="sub_page",
            ),
            html.Div(html.P('We could put some information here',style={"color": "#ffffff"}),style= {"background-color": "#565656","border-bottom": "5px solid #76323F", "padding": "20px","margin-bottom": "30px"}),
            html.Br([]),
        ],
        className="page",
    ), html.Div(
        [
            #html.Div([Header(app)]),
            # page 1
            html.Div(
                [
                    # Row 3
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.Div(html.H5("Appendix cont.", style={"padding-left": "10px"}), className="inner-product2"),
                                    html.Br([]),
                                    html.P(load_document(config.TEXT_FILE),
                                        style={"color": "#000000"},
                                        className="row",
                                    ),
                                ],
                                className="product2",
                            )
                        ],
                        className="row",
                    ),
                    # Row 4
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                     # Row 5
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                ],
                className="sub_page",
            ),
            html.Div(html.P('We could put some information here',style={"color": "#ffffff"}),style= {"background-color": "#565656","border-bottom": "5px solid #76323F", "padding": "20px","margin-bottom": "30px"}),
            html.Br([]),
        ],
        className="page",
    ),
    html.Div(
        [
            html.Div([Header(app)]),
            # page 1
            html.Div(
                [
                    # Row 3
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.Div(html.H5("Appendix cont.", style={"padding-left": "10px"}), className="inner-product2"),
                                    html.Br([]),
                                    html.P(load_document(config.TEXT_FILE),
                                        style={"color": "#000000"},
                                        className="row",
                                    ),
                                ],
                                className="product2",
                            )
                        ],
                        className="row",
                    ),
                    # Row 4
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                     # Row 5
                    html.Div(
                        [
                            html.Div(
                                [
                                    html.H6(
                                        ["Header"], className="subtitle padded"
                                    )
                                ],
                                className="six columns",
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Header",
                                        className="subtitle padded",
                                    )
                                ],
                                className="six columns",
                            ),
                        ],
                        className="row",
                        style={"margin-bottom": "35px"},
                    ),
                ],
                className="sub_page",
            ),
            html.Div(html.P('We could put some information here',style={"color": "#ffffff"}),style= {"background-color": "#565656","border-bottom": "5px solid #76323F", "padding": "20px","margin-bottom": "30px"}),
            html.Br([]),
        ],
        className="pagetwo",
    )

max_stars_repo_name: Cawies/poverty-dashboard

max_issues_repo_name: Cawies/poverty-dashboard

max_forks_repo_name: Cawies/poverty-dashboard