text:
import tensorflow as tf

import lasagne
import theano
import time

import numpy as np
import cPickle as cp #serializing and de-serializing a Python object structure
import theano.tensor as T
from sliding_window import sliding_window

# Hardcoded number of sensor channels employed in the OPPORTUNITY challenge
NB_SENSOR_CHANNELS = 113

# Hardcoded number of classes in the gesture recognition problem
NUM_CLASSES = 18

# Hardcoded length of the sliding window mechanism employed to segment the data
SLIDING_WINDOW_LENGTH = 24

# Length of the input sequence after convolutional operations
FINAL_SEQUENCE_LENGTH = 8

# Hardcoded step of the sliding window mechanism employed to segment the data
SLIDING_WINDOW_STEP = 12

# Batch Size
BATCH_SIZE = 100

# Number filters convolutional layers
NUM_FILTERS = 64

# Size filters convolutional layers
FILTER_SIZE = 5

# Number of unit in the long short-term recurrent layers
NUM_UNITS_LSTM = 128

#Load the OPPORTUNITY processed dataset. Sensor data is segmented using a sliding window of fixed length. The class associated with each segment corresponds to the gesture which has been observed during that interval. Given a sliding window of length T, we choose the class of the sequence as the label at t=T, or in other words, the label of last sample in the window.
# In[14]:

def load_dataset(filename):

    f = file(filename, 'rb')
    data = cp.load(f)
    f.close()

    X_train, y_train = data[0]
    X_test, y_test = data[1]

    print(" ..from file {}".format(filename))
    print(" ..reading instances: train {0}, test {1}".format(X_train.shape, X_test.shape))

    X_train = X_train.astype(np.float32)
    X_test = X_test.astype(np.float32)

    # The targets are casted to int8 for GPU compatibility.
    y_train = y_train.astype(np.uint8)
    y_test = y_test.astype(np.uint8)

    return X_train, y_train, X_test, y_test

print("Loading data...")
X_train, y_train, X_test, y_test = load_dataset('gestures.data')

assert NB_SENSOR_CHANNELS == X_train.shape[1]
def opp_sliding_window(data_x, data_y, ws, ss):
    data_x = sliding_window(data_x,(ws,data_x.shape[1]),(ss,1))
    data_y = np.asarray([[i[-1]] for i in sliding_window(data_y,ws,ss)])
    return data_x.astype(np.float32), data_y.reshape(len(data_y)).astype(np.uint8)

# Sensor data is segmented using a sliding window mechanism
X_test, y_test = opp_sliding_window(X_test, y_test, SLIDING_WINDOW_LENGTH, SLIDING_WINDOW_STEP)
print(" ..after sliding window (testing): inputs {0}, targets {1}".format(X_test.shape, y_test.shape))
# Data is reshaped since the input of the network is a 4 dimension tensor
X_test = X_test.reshape((-1, SLIDING_WINDOW_LENGTH, NB_SENSOR_CHANNELS, 1))

# Sensor data is segmented using a sliding window mechanism
X_train, y_train = opp_sliding_window(X_train, y_train, SLIDING_WINDOW_LENGTH, SLIDING_WINDOW_STEP)
print(" ..after sliding window (testing): inputs {0}, targets {1}".format(X_test.shape, y_test.shape))
# Data is reshaped since the input of the network is a 4 dimension tensor
X_train = X_train.reshape((-1, SLIDING_WINDOW_LENGTH, NB_SENSOR_CHANNELS, 1))

def one_hot(label):
    """convert label from dense to one hot
      argument:
        label: ndarray dense label ,shape: [sample_num,1]
      return:
        one_hot_label: ndarray  one hot, shape: [sample_num,n_class]
    """
    label_num = len(label)
    new_label = label.reshape(label_num)  # shape : [sample_num]
    # because max is 5, and we will create 6 columns
    n_values = np.max(new_label) + 1
    return np.eye(n_values)[np.array(new_label, dtype=np.int32)]
y_test=one_hot(y_test)
y_train=one_hot(y_train)

print("data is ready")

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, w, w2, w3, w4, rnnW, rnnB, lstm_size):
    l1a = tf.nn.relu(tf.nn.conv2d(X, w,                       # l1a shape=(?, 28, 28, 32)
                        strides=[1, 1, 1, 1], padding='VALID'))
    # l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],              # l1 shape=(?, 14, 14, 32)
    #                     strides=[1, 2, 2, 1], padding='SAME')
    # l1 = tf.nn.dropout(l1, p_keep_conv)

    l2a = tf.nn.relu(tf.nn.conv2d(l1a, w2,                     # l2a shape=(?, 14, 14, 64)
                        strides=[1, 1, 1, 1], padding='VALID'))
    # l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],              # l2 shape=(?, 7, 7, 64)
    #                     strides=[1, 2, 2, 1], padding='SAME')
    # l2 = tf.nn.dropout(l2, p_keep_conv)

    l3a = tf.nn.relu(tf.nn.conv2d(l2a, w3,                     # l3a shape=(?, 7, 7, 128)
                        strides=[1, 1, 1, 1], padding='VALID'))
    # l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],              # l3 shape=(?, 4, 4, 128)
    #                     strides=[1, 2, 2, 1], padding='SAME')
    # l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])    # reshape to (?, 2048)
    # l3 = tf.nn.dropout(l3, p_keep_conv)
    l4a = tf.nn.relu(tf.nn.conv2d(l3a, w4,                     # l3a shape=(?, 7, 7, 128)
                        strides=[1, 1, 1, 1], padding='VALID'))
    shuff = tf.transpose(l4a, [1, 0, 2, 3])
    shp1 = tf.reshape(shuff, [-1, lstm_size])
    X_split = tf.split(shp1, 8, 0) # split them to time_step_size (28 arrays)
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size, forget_bias=1.0, state_is_tuple=True)
    # Stack two LSTM layers, both layers has the same shape
    lstm_layers = tf.contrib.rnn.MultiRNNCell([lstm_cell] * 2)

    outputs, _states = tf.contrib.rnn.static_rnn(lstm_layers, X_split, dtype=tf.float32)

    print("tf net end")

    # Linear activation
    # Get the last output
    return tf.matmul(outputs[-1], rnnW) + rnnB

X = tf.placeholder("float", [None, 24, 113, 1])
Y = tf.placeholder("float", [None, 18])

lstm_size = 128
w = init_weights([5, 1, 1, 64])       # 3x3x1 conv, 32 outputs
w2 = init_weights([5, 1, 64, 64])     # 3x3x32 conv, 64 outputs
w3 = init_weights([5, 1, 64, 64])    # 3x3x32 conv, 128 outputs
w4 = init_weights([5, 1, 64, 64]) # FC 128 * 4 * 4 inputs, 625 outputs
rnnW = init_weights([lstm_size, 128])
rnnB = init_weights([128])
pre_Y = model(X, w, w2, w3, w4, rnnW, rnnB,lstm_size)
print ("get cnn output");

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pre_Y, labels=Y))
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
predict_op = tf.argmax(pre_Y, 1)

session_conf = tf.ConfigProto()
session_conf.gpu_options.allow_growth = True
print("net work done")

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]

# Launch the graph in a session
with tf.Session(config=session_conf) as sess:
    # you need to initialize all variables
    tf.global_variables_initializer().run()

    for i in range(100):
        for start, end in zip(range(0, len(X_train), BATCH_SIZE), range(BATCH_SIZE, len(X_train)+1, BATCH_SIZE)):
            sess.run(train_op, feed_dict={X: X_train[start:end], Y: y_train[start:end]})

        test_indices = np.arange(len(X_test))  # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0: 100]

        print(i, np.mean(np.argmax(y_test[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict={X: X_test[test_indices]})))

reason: import numpy,import theano


text:
from collections import deque

import cv2
import gym
import numpy as np
import random

class DQNEnvironment(gym.Wrapper):

    def __init__(self, environment, config):
        super().__init__(environment)
        self.config = config

        self.action_meanings = self.env.unwrapped.get_action_meanings()
        self.observation_buffer = deque(maxlen=2)

        # max. number of 'do nothing' actions at the beginning of a new game
        self.max_n_wait_actions = config.max_n_wait_actions

        # number of times an action should be repeated in each step (frame-skipping)
        self.action_repeat = config.action_repeat

        # consider end-of-life as end-of-episode, but only call Gym environment reset function if all lives exhausted
        self.lives = 0
        self.game_over = True

    def get_lives(self):
        return self.env.unwrapped.ale.lives()

    def is_game_over(self):
        return self.game_over

    def start_episode(self):

        if self.game_over:
            observation = self.env.reset(seed=random.randint(1, 69420))
        else:
            # advance using a 'do nothing' action
            observation, _, _, _ = self.env.step(0)
        self.lives = self.env.unwrapped.ale.lives()

        # some environments remain unchanged until impulse action is performed
        if 'FIRE' in self.action_meanings:
            observation, _, _, _ = self.env.step(self.action_meanings.index('FIRE'))

        return observation

    def start_random_episode(self):

        observation = self.start_episode()

        # execute a random number of 'do nothing' actions to randomize the initial game state
        n_wait_actions = random.randint(1, self.max_n_wait_actions + 1)
        for _ in range(n_wait_actions):
            observation, _, episode_done, _ = self.env.step(0)

            # in case the episode ends during random actions, call Gym environment reset function
            if episode_done:
                self.env.reset(seed=random.randint(1, 69420))

        return observation

    def step(self, action):

        assert self.action_space.contains(action)

        cumulative_reward = 0

        for _ in range(self.action_repeat):

            observation, reward, done, info = self.env.step(action)

            self.observation_buffer.append(observation)
            cumulative_reward = cumulative_reward + reward
            self.game_over = done

            # consider end-of-life as end-of-episode to improve value estimation
            if self.lives > self.env.unwrapped.ale.lives():
                done = True
            self.lives = self.env.unwrapped.ale.lives()

            if done:
                break

        # select maximum value for each pixel colour over the last two frame to remove flickering
        observation = np.maximum(self.observation_buffer[0], self.observation_buffer[-1], dtype=np.float32)

        # reduce observation dimensionality through gray-scaling and down-sampling to 84 x 84
        observation = cv2.cvtColor(observation, cv2.COLOR_BGR2GRAY)
        observation = cv2.resize(observation, (84, 84), interpolation=cv2.INTER_LINEAR)
        return observation, cumulative_reward, done, info

ext: py

max_stars_repo_name: meetjannik/deep-q-network

max_issues_repo_name: meetjannik/dqn

max_forks_repo_name: meetjannik/dqn

reason: import numpy


text:
import os
import torch

import cytofpy

import math
import matplotlib.pyplot as plt
import copy
import numpy as np
import pickle

def add_gridlines_Z(Z):
    J, K = Z.shape
    for j in range(J):
        plt.axhline(y=j+.5, color='grey', linewidth=.5)

    for k in range(K):
        plt.axvline(x=k+.5, color='grey', linewidth=.5)

if __name__ == '__main__':
    path_to_exp_results = 'results/test/'
    os.makedirs(path_to_exp_results, exist_ok=True)

    show_plots = True

    # torch.manual_seed(2) # Data with seed(2) is good
    torch.manual_seed(0) # This data is good
    np.random.seed(0)

    # Success
    # data = cytofpy.util.simdata(N=[30000, 10000, 20000], L0=1, L1=1, J=4, a_W=[300, 700])
    # data = cytofpy.util.simdata(N=[30000, 10000, 20000], L0=3, L1=3, J=4, a_W=[300, 700])

    # TODO: Make this work
    data = cytofpy.util.simdata(N=[30000, 10000, 20000], L0=3, L1=3, J=8)

    cb = data['data']
    y = copy.deepcopy(cb['y'])
    I = len(y)

    # Color map
    cm_greys = plt.cm.get_cmap('Greys')
    cm = plt.cm.get_cmap('bwr')
    cm.set_under(color='blue')
    cm.set_over(color='red')
    cm.set_bad(color='black')

    if show_plots:
        plt.imshow(data['params']['Z'], aspect='auto', vmin=0, vmax=1, cmap=cm_greys)
        J, K = data['params']['Z'].shape
        add_gridlines_Z(data['params']['Z'])
        plt.savefig('{}/Z_true.pdf'.format(path_to_exp_results))
        plt.show()

        # Plot yi histograms
        plt.hist(y[0][:, 1], bins=100, density=True); plt.xlim(-15, 15); plt.show()
        plt.hist(y[1][:, 3], bins=100, density=True); plt.xlim(-15, 15); plt.show()
        plt.hist(y[2][:, -1], bins=100, density=True); plt.xlim(-15, 15); plt.show()

        # Heatmaps
        for i in range(I):
            plt.imshow(y[i], aspect='auto', vmin=-2, vmax=2, cmap=cm)
            plt.colorbar()
            plt.show()

    K = 10
    L = [2, 2]

    # model.debug=True
    priors = cytofpy.model.default_priors(y, K=K, L=L)
    out = cytofpy.model.fit(y, max_iter=5000, lr=1e-1, print_freq=1, eps_conv=1e-6,
                            priors=priors, minibatch_size=100000, tau=0.1,
                            verbose=0, seed=1)

    # Save output
    pickle.dump(out, open('{}/out.p'.format(path_to_exp_results), 'wb'))

    elbo = out['elbo']

    out = pickle.load(open('{}/out.p'.format(path_to_exp_results), 'rb'))
    mod = cytofpy.model.Model(y=out['y'], priors=priors,
                              tau=out['tau'],
                              use_stick_break=out['use_stick_break'],
                              model_noisy=out['model_noisy'])

    plt.plot(elbo)
    plt.ylabel('ELBO / NSUM')
    plt.show()

    # Posterior Inference
    B = 100
    idx = [np.random.choice(mod.N[i], 1) for i in range(mod.I)]
    post = [mod.sample_params(idx) for b in range(B)]

    # Plot Z
    H = torch.stack([p['H'] for p in post]).detach().reshape((B, mod.J, mod.K))
    v = torch.stack([p['v'] for p in post]).detach().reshape((B, 1, mod.K))
    Z = (v.cumprod(2) > H).numpy()
    plt.imshow(Z.mean(0) > .5, aspect='auto', vmin=0, vmax=1, cmap=cm_greys)
    add_gridlines_Z(Z[0])
    plt.savefig('{}/Z.pdf'.format(path_to_exp_results))
    plt.show()

    # Plot sig
    # sig0 = torch.stack([p['sig0'] for p in post]).detach().numpy()
    # plt.boxplot(sig0, showmeans=True, whis=[2.5, 97.5], showfliers=False)
    # plt.xlabel('$\sigma$_0', fontsize=15)
    # for yint in data['params']['sig'].tolist():
    #     plt.axhline(yint)

    # plt.show()

    W = torch.stack([p['W'] for p in post]).detach().numpy()
    v = torch.stack([p['v'] for p in post]).detach().numpy()
    alpha = torch.stack([p['alpha'] for p in post]).detach().numpy()

    # Plot W, v
    plt.figure()
    for i in range(mod.I):
        plt.subplot(mod.I + 1, 1, i + 1)
        plt.boxplot(W[:, i, :], showmeans=True, whis=[2.5, 97.5], showfliers=False)
        plt.ylabel('$W_{}$'.format(i+1), rotation=0, labelpad=15)
        for yint in data['params']['W'][i, :].tolist():
            plt.axhline(yint)

    plt.subplot(mod.I + 1, 1, mod.I + 1)
    plt.boxplot(v.cumprod(1), showmeans=True, whis=[2.5, 97.5], showfliers=False)
    plt.ylabel('$v$', rotation=0, labelpad=15)
    plt.tight_layout()
    plt.show()

    # Trace plots of variational parameters
    W_trace = torch.stack([t['W'].dist().mean for t in out['trace_g']]).detach().numpy()
    v_trace = torch.stack([t['v'].dist().mean for t in out['trace_g']]).detach().numpy()

    # Trace for v
    plt.plot(v_trace)
    plt.title('v trace')
    plt.show()

    # Plot sig mean trace
    # sig0_m_trace = torch.stack([t['sig0'].dist().mean for t in out['trace_g']])
    # plt.plot(sig0_m_trace.detach().numpy())

    # for i in range(mod.I):
    #     plt.axhline(data['params']['sig'][i])

    # plt.title('trace plot for $\sigma$_0 vp mean')
    # plt.show()

max_stars_repo_name: luiarthur/CytoPy

max_issues_repo_name: luiarthur/CytofPy

max_forks_repo_name: luiarthur/CytoPy


text:
# import pandas as pd
# import numpy as np
# import time

# from sklearn.metrics import (
#     f1_score,
#     roc_auc_score,
#     recall_score,
#     precision_score,
#     mean_absolute_percentage_error,
#     mean_squared_error,
#     make_scorer
# )

# def cross_val_score_classfication(model, X_train, y_train, X_val, y_val, return_train_score=True):
#     """
#     Returns scores of cross validation

#     Parameters
#     ----------
#     model :
#         scikit-learn model
#     X_train : numpy array or pandas DataFrame
#         X in the training data
#     y_train :
#         y in the training data
#     X_val:
#         X in the validation data
#     y_val:
#         y in the training data
#     return_train_score:
#         True/False

#     Returns
#     ----------
#         pandas Series with training and validation scores from cross_validation
#     """
#     start_time_fit = time.time()
#     model.fit(X_train, y_train)
#     end_time_fit = time.time()
#     start_time_score = time.time()
#     y_val_pred = model.predict(X_val)

#     scores_dict = {
#         "roc_auc_test":roc_auc_score(y_val, y_val_pred, average="weighted"),
#         "f1_test":f1_score(y_val, y_val_pred, average="weighted"),
#         "recall_test":recall_score(y_val, y_val_pred, average="weighted"),
#         "precision_test": precision_score(y_val, y_val_pred, average="weighted")
#     }
#     end_time_score = time.time()
#     scores_dict["score_time"] = end_time_score - start_time_score

#     if return_train_score:
#         y_train_pred = model.predict(X_train)
#         scores_dict["roc_auc_train"] = roc_auc_score(y_train, y_train_pred, average="weighted"),
#         scores_dict["f1_train"] = f1_score(y_train, y_train_pred, average="weighted"),
#         scores_dict["recall_train"] = recall_score(y_train, y_train_pred, average="weighted"),
#         scores_dict["precision_test"] = precision_score(y_train, y_train_pred, average="weighted")
#         scores_dict["fit_time"] = end_time_fit - start_time_fit

#     scores_results = pd.Series(scores_dict)

#     return model, scores_results

# def cross_val_score_regression(model, X_train, y_train, X_val, y_val, return_train_score=True):
#     """
#     Returns scores of cross validation

#     Parameters
#     ----------
#     model :
#         scikit-learn model
#     X_train : numpy array or pandas DataFrame
#         X in the training data
#     y_train :
#         y in the training data
#     X_val:
#         X in the validation data
#     y_val:
#         y in the training data
#     return_train_score:
#         True/False

#     Returns
#     ----------
#         pandas Series with training and validation scores from cross_validation
#     """

#     model.fit(X_train, y_train)
#     y_val_pred = model.predict(X_val)

#     score_dict = {
#         "r2_test": model.score(X_val, y_val),
#         "mse_test": mean_squared_error(y_val, y_val_pred),
#         "mape_test": mean_absolute_percentage_error(y_val, y_val_pred)
#     }

#     if return_train_score:
#         y_train_pred = model.predict(X_train)

#         score_dict["r2_train"] = model.score(X_train, y_train)
#         score_dict["mse_train"] = mean_squared_error(y_train, y_train_pred)
#         score_dict["mape_train"] = mean_absolute_percentage_error(y_train, y_train_pred)

#     scores_result = pd.Series(score_dict)

#     return model, scores_result


text:
from __future__ import division
from __future__ import print_function

import cv2
import numpy as np
import copy

from core.detection_input import DetectionAugmentation, AnchorTarget2D

class Resize2DImageBboxMask(DetectionAugmentation):
    """
    input: image, ndarray(h, w, rgb)
           gt_bbox, ndarry(n, 5)
           gt_poly, [[(p, 2)]]
    output: image, ndarray(h', w', rgb)
            im_info, tuple(h', w', scale)
            gt_bbox, ndarray(n, 5)
            gt_poly, [[ndarray, ndarray, ...]]
    """

    def __init__(self, pResize):
        super().__init__()
        self.p = pResize  # type: ResizeParam

    def apply(self, input_record):
        p = self.p

        image = input_record["image"]
        gt_bbox = input_record["gt_bbox"]
        gt_poly = input_record["gt_poly"]

        short = min(image.shape[:2])
        long = max(image.shape[:2])
        scale = min(p.short / short, p.long / long)

        input_record["image"] = cv2.resize(image, None, None, scale, scale,
                                           interpolation=cv2.INTER_LINEAR)
        # make sure gt boxes do not overflow
        gt_bbox[:, :4] = gt_bbox[:, :4] * scale
        gt_bbox[:, [0, 2]] = np.clip(gt_bbox[:, [0, 2]], 0, input_record["image"].shape[1] - 1)
        gt_bbox[:, [1, 3]] = np.clip(gt_bbox[:, [1, 3]], 0, input_record["image"].shape[0] - 1)
        input_record["gt_bbox"] = gt_bbox

        # exactly as opencv
        h, w = image.shape[:2]
        input_record["im_info"] = np.array([round(h * scale), round(w * scale), scale], dtype=np.float32)

        # resize poly
        for i, segms in enumerate(gt_poly):
            input_record["gt_poly"][i] = [segm_j * scale for segm_j in segms]

class Flip2DImageBboxMask(DetectionAugmentation):
    """
    input: image, ndarray(h, w, rgb)
           gt_bbox, ndarry(n, 5)
           gt_poly, [[ndarray, ndarray, ...]]
    output: image, ndarray(h, w, rgb)
            gt_bbox, ndarray(n, 5)
            gt_poly, [[ndarray, ndarray, ...]]
    """

    def __init__(self):
        super().__init__()

    def apply(self, input_record):
        def _flip_poly(poly, width):
            flipped_poly = poly.copy()
            flipped_poly[0::2] = width - poly[0::2] - 1
            return flipped_poly

        if input_record["flipped"]:
            image = input_record["image"]
            gt_bbox = input_record["gt_bbox"]
            gt_poly = input_record["gt_poly"]

            input_record["image"] = image[:, ::-1]
            flipped_bbox = gt_bbox.copy()
            h, w = image.shape[:2]
            flipped_bbox[:, 0] = (w - 1) - gt_bbox[:, 2]
            flipped_bbox[:, 2] = (w - 1) - gt_bbox[:, 0]
            input_record["gt_bbox"] = flipped_bbox

            # flip poly
            for i, segms in enumerate(gt_poly):
                input_record["gt_poly"][i] = [_flip_poly(segm_j, w) for segm_j in segms]

class Pad2DImageBboxMask(DetectionAugmentation):
    """
    input: image, ndarray(h, w, rgb)
           gt_bbox, ndarry(n, 5)
           gt_poly, [[ndarray, ndarray, ...]]
    output: image, ndarray(h, w, rgb)
            gt_bbox, ndarray(max_num_gt, 5)
            gt_poly, [[ndarray, ndarray, ...]]
    """

    def __init__(self, pPad):
        super().__init__()
        self.p = pPad  # type: PadParam

    def apply(self, input_record):
        p = self.p

        image = input_record["image"]
        gt_bbox = input_record["gt_bbox"]
        gt_poly = input_record["gt_poly"]

        h, w = image.shape[:2]
        shape = (p.long, p.short, 3) if h >= w \
            else (p.short, p.long, 3)

        padded_image = np.zeros(shape, dtype=np.float32)
        padded_image[:h, :w] = image
        padded_gt_bbox = np.full(shape=(p.max_num_gt, 5), fill_value=-1, dtype=np.float32)
        padded_gt_bbox[:len(gt_bbox)] = gt_bbox

        padded_gt_poly = np.full(shape=(p.max_num_gt, p.max_len_gt_poly), fill_value=-1, dtype=np.float32)
        padded_gt_poly[:len(gt_bbox)] = gt_poly

        input_record["image"] = padded_image
        input_record["gt_bbox"] = padded_gt_bbox
        input_record["gt_poly"] = padded_gt_poly

class PreprocessGtPoly(DetectionAugmentation):
    # TODO: remove this function and set gt_poly in cache to ndarray
    """
    input: gt_poly
    output: gt_poly
    """

    def __init__(self):
        super().__init__()

    def apply(self, input_record):
        ins_poly = input_record["gt_poly"]
        gt_poly = [None] * len(ins_poly)
        for i, ins_poly_i in enumerate(ins_poly):
            segms = [None] * len(ins_poly_i)
            for j, segm_j in enumerate(ins_poly_i):
                segms[j] = np.array(segm_j, dtype=np.float32)
            gt_poly[i] = segms
        input_record["gt_poly"] = gt_poly

class EncodeGtPoly(DetectionAugmentation):
    """
    input: gt_class, gt_poly
    output: gt_poly
    """

    def __init__(self, pPad):
        super().__init__()
        self.p = pPad

    def apply(self, input_record):
        gt_class = input_record["gt_class"]
        gt_poly = input_record["gt_poly"] # [[ndarray, ndarray, ...]]

        num_instance = len(gt_class)
        encoded_gt_poly = np.full((num_instance, self.p.max_len_gt_poly), -1, dtype=np.float32)

        for i, (class_id, segms) in enumerate(zip(gt_class, gt_poly)):
            # encoded_gt_poly_i: [class_id, num_segms, len_segm1, len_segm2, segm1, segm2]
            encoded_gt_poly[i][0] = class_id
            num_segms = len(segms)
            encoded_gt_poly[i][1] = num_segms
            segms_len = [len(segm_j) for segm_j in segms]
            encoded = np.hstack([np.array(segms_len), np.hstack(segms)])
            encoded_gt_poly[i][2:2+len(encoded)] = encoded

        input_record["gt_poly"] = encoded_gt_poly

if __name__ == "__main__":
    import pickle as pkl
    import time

    import pycocotools.mask as mask_util

    from core.detection_input import ReadRoiRecord, \
        ConvertImageFromHwcToChw, Flip2DImageBbox, Pad2DImageBbox, \
        RenameRecord, AnchorTarget2D, AnchorLoader

    from models.maskrcnn.input import PreprocessGtPoly, EncodeGtPoly, \
        Resize2DImageBboxMask, Flip2DImageBboxMask, Pad2DImageBboxMask

    def vis_mask(img, mask, col, alpha=0.4):
        """Visualizes a single binary mask."""

        img = img.astype(np.float32)
        idx = np.nonzero(mask)

        img[idx[0], idx[1], :] *= 1.0 - alpha
        img[idx[0], idx[1], :] += alpha * col

        return img.astype(np.uint8)

    class ResizeParam:
        short = 800
        long = 1200

    class PadParam:
        short = 800
        long = 1200
        max_num_gt = 100
        max_len_gt_poly = 2500

    class AnchorTarget2DParam:
        class generate:
            short = 800 // 16
            long = 1200 // 16
            stride = 16
            scales = (2, 4, 8, 16, 32)
            aspects = (0.5, 1.0, 2.0)

        class assign:
            allowed_border = 0
            pos_thr = 0.7
            neg_thr = 0.3
            min_pos_thr = 0.0

        class sample:
            image_anchor = 256
            pos_fraction = 0.5

    class RenameParam:
        mapping = dict(image="data")

    transform = [
        ReadRoiRecord(None),
        PreprocessGtPoly(),
        Resize2DImageBboxMask(ResizeParam),
        Flip2DImageBboxMask(),
        EncodeGtPoly(PadParam),
        Pad2DImageBboxMask(PadParam),
        ConvertImageFromHwcToChw(),
        AnchorTarget2D(AnchorTarget2DParam),
        RenameRecord(RenameParam.mapping)
    ]

    DEBUG = True

    with open("data/cache/coco_valminusminival2014.roidb", "rb") as fin:
        roidb = pkl.load(fin)
        roidb = [rec for rec in roidb if rec["gt_bbox"].shape[0] > 0]
        roidb = [roidb[i] for i in np.random.choice(len(roidb), 20, replace=False)]

        print(roidb[0])
        flipped_roidb = []
        for rec in roidb:
            new_rec = rec.copy()
            new_rec["flipped"] = True
            flipped_roidb.append(new_rec)
        roidb = roidb + flipped_roidb

        loader = AnchorLoader(roidb=roidb,
                              transform=transform,
                              data_name=["data", "im_info", "gt_bbox", "gt_poly"],
                              label_name=["rpn_cls_label", "rpn_reg_target", "rpn_reg_weight"],
                              batch_size=2,
                              shuffle=False,
                              kv=None)

        tic = time.time()
        while True:
            try:
                data_batch = loader.next()
                if DEBUG:
                    print(data_batch.provide_data)
                    print(data_batch.provide_label)
                    print(data_batch.data[0].shape)
                    print(data_batch.label[1].shape)
                    print(data_batch.label[2].shape)
                    data = data_batch.data[0]
                    gt_bbox = data_batch.data[2]
                    gt_poly = data_batch.data[3]
                    for i, (im, bbox, poly) in enumerate(zip(data, gt_bbox, gt_poly)):
                        im = im.transpose((1, 2, 0))[:, :, ::-1].asnumpy()
                        im = np.uint8(im)
                        valid_instance = np.where(bbox[:, -1] != -1)[0]
                        bbox = bbox[valid_instance].asnumpy()
                        poly = poly[valid_instance].asnumpy()
                        for j, (bbox_j, poly_j) in enumerate(zip(bbox, poly)):
                            x1, y1, x2, y2 = bbox_j[:4].astype(int)
                            cv2.rectangle(im, (x1, y1), (x2, y2), (255, 0, 0), 2)
                            class_index = poly_j[0]
                            assert int(class_index) == int(bbox_j[-1])
                            num_segms = poly_j[1]
                            len_segms = poly_j[2:2+int(num_segms)]
                            cur_start = 2 + int(num_segms)
                            segms = []
                            for len_segm in len_segms:
                                segm = poly_j[cur_start:cur_start+int(len_segm)]
                                segm = segm.tolist()
                                segms.append(segm)
                                cur_start = cur_start + int(len_segm)
                            rle = mask_util.frPyObjects(segms, im.shape[0], im.shape[1])
                            mask = mask_util.decode(rle)
                            mask = np.sum(mask, axis=2)
                            mask = np.array(mask > 0, dtype=np.float32)
                            im = vis_mask(im, mask, np.array([18, 127, 15]), alpha=0.4)
                        cv2.imshow("im", im)
                        cv2.waitKey(0)
            except StopIteration:
                toc = time.time()
                print("{} samples/s".format(len(roidb) / (toc - tic)))
                break

max_stars_repo_name: xchani/simpledet

max_issues_repo_name: xchani/simpledet

max_forks_repo_name: xchani/simpledet


text:
import os
import pandas as pd
import numpy as np
import pandas as pd
import pymongo
import json
import pickle
# from flask_pymongo import PyMongo
from flask import Flask, jsonify, render_template, request, redirect, url_for
from sklearn.svm import SVC
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)

DATABASE_URL = os.environ.get('DATABASE_URL')
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
db = SQLAlchemy(app)

class Contact(db.Model):
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    pwd = db.Column(db.String(30), nullable=False)
    date = db.Column(db.String(12), nullable=True)
    email = db.Column(db.String(20), nullable=False)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/form")
def form():
    return render_template("form.html")

@app.route("/thanks")
def thanks():
    return render_template("thanks.html")

@app.route('/charge', methods=['POST', 'GET'])
def pay():
	return redirect(url_for('form'))

#prediction function
def ValuePredictor(to_predict_list):
    to_predict = np.array(to_predict_list).reshape(1,34)
    loaded_model = pickle.load(open('./model/model.pkl',"rb"))
    result = loaded_model.predict(to_predict)
    return result[0]

@app.route('/result',methods = ['POST'])
def result():
    if request.method == 'POST':
        to_predict_list = request.form.to_dict()
        to_predict_list=list(to_predict_list.values())
        to_predict_list = list(map(int, to_predict_list))
        result = ValuePredictor(to_predict_list)

        if int(result)==1:
            prediction='Class 1: Psoriasis'
        elif int(result)==2:
            prediction='Class 2: Seboreic Dermatitis'
        elif int(result)==3:
            prediction = 'Class 3: Lichen Planus'
        elif int(result)==4:
            prediction ="Class 4: pityriasis rosea"
        elif int(result)==5:
            prediction ="Class 5: Chronic Dermatitis"
        elif int(result)==6:
            prediction = "Class 6: Pityriasis Rubra Pilaris"

        return render_template("form.html",result=prediction)

# @app.route("/contact", methods = ['GET', 'POST'])
# def contact():
#     if(request.method == 'POST'):
#         name = request.form.get('name')
#         email = request.form.get('email')
#         pwd = request.form.get('pwd')
#         entry = Contact(name=name, phone_num = phone, msg = message, date= datetime.now(),email = email )
#         db.session.add(entry)
#         db.session.commit()

#     return render_template("contact.html")

if __name__ == "__main__":
    app.run(debug=True)


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 30 12:09:34 2020

@author: KX764QE
"""
import numpy as np

# data
RAW_DATA_FILE = "app_churn_data.csv"
PIPELINE_NAME = 'churn_pipeline'

TARGET = ['churn']

# input variables
FEATURES = ['age', 'housing', 'credit_score', 'deposits', 'withdrawal',
       'purchases_partners', 'purchases', 'cc_taken', 'cc_recommended',
       'cc_disliked', 'cc_liked', 'cc_application_begin', 'app_downloaded',
       'web_user', 'app_web_user', 'ios_user',
       'registered_phones', 'payment_type', 'waiting_4_loan', 'cancelled_loan',
       'received_loan', 'rejected_loan', 'zodiac_sign',
       'left_for_two_month_plus', 'left_for_one_month', 'rewards_earned',
       'reward_rate', 'is_referred']

# numerical variables with na
NUMERICAL_VARS_WITH_NA = ['age', 'credit_score', 'rewards_earned']

# categorical variables with na
CATEGORICAL_VARS_WITH_NA = ['housing', 'payment_type', 'zodiac_sign']

# categorical variables to encode
CATEGORICAL_VARS_ENCODE = ['housing', 'payment_type', 'zodiac_sign']

# numerical variables to scale
NUMERICAL_VARS_SCALE = ['age', 'credit_score', 'deposits', 'withdrawal', 'purchases_partners',
                        'purchases',  'cc_taken',  'cc_recommended', 'cc_disliked',  'cc_liked',
                        'cc_application_begin',  'app_downloaded', 'web_user',  'app_web_user',
                        'ios_user',  'registered_phones', 'waiting_4_loan',  'cancelled_loan',
                        'received_loan', 'rejected_loan', 'left_for_two_month_plus', 'left_for_one_month',
                        'rewards_earned',  'reward_rate',  'is_referred']

# missing values
MISSING_VALUES = ['NA', 'Na', 'na', np.nan ]

# random seed
RANDOM_SEED= 101


text:
#!/usr/bin/env python
import aslam_backend as ab
import numpy as np

import unittest

class TestJacobianContainerSparse(unittest.TestCase):
  def test_add(self):
    point = ab.Point2d(np.array([1,1]))
    point.setBlockIndex(0)
    point.setActive(True)
    jc = ab.JacobianContainerSparse(2)
    self.assertEqual(jc.rows(), 2)
    J = np.array([[1,1], [1,1]])
    jc.add(point, J) # With design variable
    self.assertTrue(jc.isFinite(point))
    self.assertTrue( (jc.asDenseMatrix() == J).all() )
    jc2 = ab.JacobianContainerSparse(2)
    jc2.add(point, J) # With design variable
    jc.add(jc2) # Without chain rule
    self.assertTrue( (jc.asDenseMatrix() == 2.*J).all() )
    jc2 = ab.JacobianContainerSparse(2)
    jc2.add(point, J) # With design variable
    jc.add(jc2, np.eye(2,2)) # With chain rule
    self.assertTrue( (jc.asDenseMatrix() == 3.*J).all() )

class TestJacobianContainerDense(unittest.TestCase):
  def test_add(self):
    point = ab.Point2d(np.array([1,1]))
    point.setBlockIndex(0)
    point.setColumnBase(0)
    point.setActive(True)
    jc = ab.JacobianContainerDense(2, point.minimalDimensions())
    self.assertEqual(jc.rows(), 2)
    J = np.array([[1,1], [1,1]])
    jc.add(point, J) # With design variable
    self.assertTrue(jc.isFinite(point))
    self.assertTrue( (jc.asDenseMatrix() == J).all() )

class TestRprop(unittest.TestCase):
    def test_simple_optimization(self):
        options = ab.OptimizerOptionsRprop()
        options.maxIterations = 500;
        options.numThreads = 1;
        options.convergenceGradientNorm = 1e-6
        options.method = ab.RpropMethod.RPROP_PLUS
        optimizer = ab.OptimizerRprop(options)
        problem = ab.OptimizationProblem()

        D = 2
        E = 3

        # Add some design variables.
        p2ds = [];
        for d in range(0, D):
          point = ab.Point2d(np.array([d, d]))
          p2ds.append(point);
          problem.addDesignVariable(point);
          point.setBlockIndex(d);
          point.setActive(True);

        # Add some error terms.
        errors = [];
        for e in range(0, E):
          for d in range(0, D):
            grad = np.array( [d+1, e+1] );
            err = ab.TestNonSquaredError(p2ds[d], grad);
            err._p = 1.0;
            errors.append(err);
            problem.addScalarNonSquaredErrorTerm(err);

        # Now let's optimize.
        optimizer.setProblem(problem);
        optimizer.checkProblemSetup();
        optimizer.optimize();

        self.assertLessEqual(optimizer.status.gradientNorm, 1e-3);

class TestMetropolisHastings(unittest.TestCase):
    def test_simple_sampling_problem(self):
        '''Sample from a twodimensional Gaussian distribution N(0, I)
        '''

        options = ab.SamplerMetropolisHastingsOptions()
        options.transitionKernelSigma = 0.1;
        sampler = ab.SamplerMetropolisHastings(options)
        negLogDensity = ab.OptimizationProblem()

        # Add a scalar design variables.
        point = ab.Point2d(np.array([0, 0]))
        point.setBlockIndex(0);
        point.setActive(True);
        negLogDensity.addDesignVariable(point);

        # Add the error term.
        grad = np.array( [-1., -1.] );
        err = ab.TestNonSquaredError(point, grad);
        err._p = 0.0; # set mean
        negLogDensity.addScalarNonSquaredErrorTerm(err);

        # Now let's sample.
        sampler.setNegativeLogDensity(negLogDensity);
        sampler.checkNegativeLogDensitySetup();
        sampler.run(10000);

class TestOptimizerCallback(unittest.TestCase):
  def test_registry(self):
    registry = ab.CallbackRegistry()
    registry.add(ab.EVENT_COST_UPDATED, lambda: None)
    self.assertEqual(registry.numCallbacks(ab.EVENT_COST_UPDATED), 1)

    registry.add(ab.EVENT_COST_UPDATED, lambda: None)
    self.assertEqual(registry.numCallbacks(ab.EVENT_COST_UPDATED), 2)

    registry.add([ab.EVENT_ITERATION_START, ab.EVENT_ITERATION_END] , lambda: None)
    self.assertEqual(registry.numCallbacks(ab.EVENT_ITERATION_START), 1)
    self.assertEqual(registry.numCallbacks(ab.EVENT_ITERATION_END), 1)

    registry.clear(ab.EVENT_ITERATION_START)
    self.assertEqual(registry.numCallbacks(ab.EVENT_ITERATION_START), 0)

    registry.clear()
    self.assertEqual(registry.numCallbacks(ab.EVENT_COST_UPDATED), 0)

if __name__ == '__main__':
    import rostest
    rostest.rosrun('aslam_backend_python', 'Rprop', TestRprop)
    rostest.rosrun('aslam_backend_python', 'MetropolisHastings', TestMetropolisHastings)
    rostest.rosrun('aslam_backend_python', 'OptimizerCallback', TestOptimizerCallback)


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from rlkit.torch.networks import Mlp

class CombineNet(torch.nn.Module):
    def __init__(self,
                encoders,
                decoder,
                no_gradient=False,
                ):
        super(CombineNet, self).__init__()

        self.encoders = nn.ModuleList(encoders)
        self.decoder = decoder
        self.no_gradient = no_gradient

    def forward(self, obs, **kwargs):
        if hasattr(self,'no_gradient') and self.no_gradient:
            latent = []
            latent.append(self.encoders[0](obs))
            with torch.no_grad():
                for encoder in self.encoders[1:]:
                    latent.append(encoder(obs))
            latent = torch.cat(latent, dim=-1)
        else:
            latent = torch.cat([
                        encoder(obs) for encoder in self.encoders
                        ], dim=-1)
        output = self.decoder(latent, **kwargs)
        return output

max_stars_repo_name: maxiaoba/rlk

max_issues_repo_name: maxiaoba/rlkit

max_forks_repo_name: maxiaoba/rlkit


text:
import os
import time
import numpy as np
import re
import awkward
import h5py
from .utils import *
from .dependencies import *
from .ProducerWrapper import ProducerWrapper, expand_wildcard
from .cache import FrameworkCache

def load_producers(producers_path):
    producers = []

    for file_name in os.listdir(producers_path):
        if file_name == "__init__.py" or file_name[-3:] != ".py":
            continue
        name = file_name[:-3]
        module = load_module(name, os.path.join(producers_path, file_name))
        for item in dir(module):
            func = getattr(module, item)
            if not hasattr(func, "product") or not hasattr(func, "requirements"):
                continue
            file_path = os.path.join(producers_path, file_name)
            producers.append(func)
    del file_name

    return producers

def match_product(product, func, verbose=False):

    if verbose:
        print("Matching product " + product + "...")

    regex = re.sub("<[^<>]*>", "[^/]*", func.product)
    match = re.match(".*" + regex + "$", product)

    if match is None:
        return None, 0

    depth = func.product.count("/")

    # The matching pattern
    group = "/".join(match.group().split("/")[-depth - 1 :])
    # The "matching depth". Products which match deeper are resolving ambiguities.
    score = group.count("/") + 1

    # hotfix for problem in pattern matching:
    # producer functions where the last identifier in the path is matched are usually to be favoured
    if func.product.split("/")[-1] == product.split("/")[-1]:
        score = score + 100

    # Penalize matching depth score with number of template specializations
    # to give priority to full specializations.
    score = score - len(group.split("/")) / 100.0

    # if verbose:
    # print(func.product, score)

    return group, score

def get_required_producers(product, producer_funcs, datasets, record, cache):

    if product in cache and not product in record:
        print("Loading  " + product + "from cache...")
        record[product] = cache[product]
        return []

    groups, scores = zip(*[match_product(product, f) for f in producer_funcs])

    if max(scores) == 0:
        return []
    i = np.argmax(scores)

    func, group = producer_funcs[i], groups[i]

    # The substitutions for the template specialization
    subs = {t: s for t, s in zip(func.product.split("/"), group.split("/")) if t != s}

    working_dir = product[: -len(group)]
    producers = [ProducerWrapper(func, subs, working_dir, datasets)]

    # print("Requirements for " + product + ":")

    for req in producers[0].flattened_requirements:
        # print("    " + req)
        producers += get_required_producers(req, producer_funcs, datasets, record, cache)

    return producers

def produce(
    products=None, producers=[], datasets=None, n_stream_workers=1, cache_time=2, verbosity=1, cache_dir=".geeksw_cache"
):

    target_products = products

    cache = FrameworkCache(cache_dir=cache_dir)

    if isinstance(producers, str):
        producers = load_producers(producers)

    producer_instances = []

    target_products = [expand_wildcard(t[1:], datasets) for t in target_products]
    target_products = [y for x in target_products for y in x]

    record = {}

    for t in target_products:
        producer_instances += get_required_producers(t, producers, datasets, record, cache)

    producers = list(set(producer_instances))

    exec_order = toposort(make_dependency_graph(producers))

    if verbosity > 0:
        print("Producers:")
        for i, ip in enumerate(exec_order):
            print("{0}. ".format(i) + producers[ip].description)

    # Loop over producers needed to get to the desired output products
    for i, ip in enumerate(exec_order):

        start_time = time.time()

        pname = producers[ip].product
        if verbosity > 0:
            print("Producing " + pname + "...")

        record[producers[ip].product] = producers[ip].run(record, n_stream_workers=n_stream_workers)

        requirements_all = []
        for ipremain in exec_order[i + 1 :]:
            requirements_all += producers[ipremain].flattened_requirements

        if producers[ip].cache and time.time() - start_time > cache_time:
            print("Pruducer time longer than {0:.2f} seconds, caching product...".format(cache_time))
            pname = producers[ip].product
            cache[pname] = record[pname]

        for key in list(record.keys()):
            if key not in requirements_all and key not in target_products:
                del record[key]

    return record

max_stars_repo_name: guitargeek/geeksw

max_issues_repo_name: guitargeek/geeksw

max_forks_repo_name: guitargeek/geeksw


text:
import websocket
try:
	import thread
except ImportError:
	import _thread as thread
import time
import yeelight
import json
import config
import numpy as np

lights_available = dict()
lights_enabled = set()
lastRGB = (0,0,0)

#Global variables for note flow combonation effect
noteHitRgbToSpike = np.array([0,0,0])
noteHitTimeSinceLastSpike = time.time()

# disable allEnabled if you wish for lights to be whitelisted. Set enabled property on bulb to enable
allEnabled = True

def changeLightColour(colour,):
	for bulb in get_selected():
		print("Updating brightness: ",bulb)
		bulb.set_rgb(colour[0],colour[1],colour[2])

def setUpLights(mode=config.lightingMode):
    config.lightingMode = mode
    for bulb in get_selected():
        print("Initiating light: ",bulb)
        bulb.set_rgb(config.colours.neutral[0],config.colours.neutral[1],config.colours.neutral[2])
        bulb.set_brightness(config.colours.neutralBrightness)
        bulb.start_music()
        bulb.turn_on()

def disconnectLights():
	for bulb in get_selected():
		print("Disconnecting light: ",bulb)
		bulb.set_rgb(config.colours.neutral[0],config.colours.neutral[1],config.colours.neutral[2])
		bulb.set_brightness(config.colours.neutralBrightness)
		try: bulb.stop_music()
		except: print("Failed to turn off music mode...")

def changeLightBrightness(brightness):
	for bulb in get_selected():
		print("Updating brightness: ",bulb)
		bulb.set_brightness(brightness)

def startLightFlow(Flow):
	for bulb in get_selected():
		print("Starting flow: ",bulb)
		bulb.start_flow(Flow)

def getScoreRGB(event):
	global lastRGB
	print("Received event:",event["event"])
	try:
		if event["status"]["performance"]["currentMaxScore"] != 0:
			score = round(event["status"]["performance"]["score"]/event["status"]["performance"]["currentMaxScore"],2)
		else:
			score = 1
		print("Score:",score)
		colourScale = int((score * 255))
		print("New percentage: ",colourScale)
		rgb = (abs(colourScale-255),colourScale,0)
		return rgb
	except Exception as e:
		print("Error:",e)
		return lastRGB

def getSongLights(event):
	global lastRGB
	print("Received event:",event["event"])
	print("lightChange", event["beatmapEvent"]["value"], event["beatmapEvent"]["type"])
	try:
		if event["beatmapEvent"]["type"] == 1:
			lightChange = event["beatmapEvent"]["value"]
			print("lightChange", lightChange)
			if lightChange in [1,2,3]:
				rgb = config.colours.rightColour
			elif lightChange in [5,6,7]:
				rgb = config.colours.leftColour
			else:
				rgb = lastRGB
			return rgb
		else: return lastRGB
	except Exception as e:
		print("Exception:",e)
		return lastRGB

def on_message(ws, message):
	global lastRGB
	global noteHitRgbToSpike
	global noteHitTimeSinceLastSpike

	event = json.loads(message)
	if event["event"] == "noteCut":
		if config.lightingMode.mode == "Score":
			print("Received event:",event["event"])
			rgb = getScoreRGB(event)
			print("RGB:",rgb)
			if rgb != lastRGB:
				lastRGB = rgb
				changeLightColour(rgb)
		elif config.lightingMode.mode == "Notes":
			print("Note cut, updating lights to:",event["noteCut"]["noteType"][-1:])
			try:
				if (time.time() - noteHitTimeSinceLastSpike) > 0.1:
					noteHitRgbToSpike = np.array([0,0,0])
					noteHitTimeSinceLastSpike = time.time()
				if event["noteCut"]["noteType"][-1:] == "A":
					noteHitRgbToSpike = np.add(noteHitRgbToSpike, np.array(config.colours.leftColour))
				elif event["noteCut"]["noteType"][-1:] == "B":
					noteHitRgbToSpike = np.add(noteHitRgbToSpike, np.array(config.colours.rightColour))
				else:
					return
				np.clip(noteHitRgbToSpike,0,255)
			except Exception as e:
				print(e)
			print("Combined RGB to:",noteHitRgbToSpike)
			#Only flow if 50 mills has passed since last flow
			noteHitFlow = yeelight.Flow(
				count = 1,
				action = yeelight.Flow.actions.recover,
				transitions = [
					yeelight.RGBTransition(noteHitRgbToSpike[0],noteHitRgbToSpike[1],noteHitRgbToSpike[2],brightness=config.colours.spikeBrightness),
					]
			)
			startLightFlow(noteHitFlow)

		if config.lightingMode.spikeBrightnessOnNoteCut and config.lightingMode.mode != "Notes":
			print("Note cut received, spiking brightness")
			brightnessSpikeFlow = yeelight.Flow(
				count = 1,
				action = yeelight.Flow.actions.recover,
				transitions = [
					yeelight.RGBTransition(lastRGB[0],lastRGB[1],lastRGB[2],brightness=config.colours.spikeBrightness),
					yeelight.SleepTransition(duration=50),
					yeelight.RGBTransition(lastRGB[0],lastRGB[1],lastRGB[2],brightness=config.colours.neutralBrightness),
					]
			)
			startLightFlow(brightnessSpikeFlow)

	elif event["event"] == "beatmapEvent":
		if config.lightingMode.mode == "SongLights":
			rgb = getSongLights(event)
			print("RGB:",rgb)
			if rgb != lastRGB:
				changeLightColour(rgb)
				lastRGB = rgb
	elif event["event"] == "finished":
		changeLightBrightness(config.colours.neutralBrightness)
		changeLightColour(config.colours.neutral)
	elif event["event"] == "failed":
		changeLightBrightness(config.colours.neutralBrightness)
		changeLightColour(config.colours.failedLevel)
		time.sleep(3)
		changeLightColour(config.colours.neutral)

def on_error(ws, error):
	print("### error ###")
	print(error)
	print("Trying to reconnect in 3s")
	time.sleep(3)
	print("Retrying...")
	start_socket()

def on_close(ws):
	print("### closed ###")

def on_open(ws):
	print("Connected to beatsaber")

wsApp = False
def start_socket():
	global wsApp
	wsApp = websocket.WebSocketApp("ws://"+config.beatsaberServer.host+":"+str(config.beatsaberServer.port)+"/socket",
							  on_message = on_message,
							  on_error = on_error,
							  on_close = on_close)
	wsApp.on_open = on_open
	wsApp.run_forever()

def discover_lights():
	global lights_available
	while len(lights_available) == 0:
		lights_available = dict()
		for light in yeelight.discover_bulbs():
			prefix = light["capabilities"]["name"] + '-'
			name = (prefix if prefix != '-' else '') + light["ip"]
			lights_available[name] = yeelight.Bulb(light["ip"],effect=config.lightingMode.lightTransitions)
		return lights_available

def get_selected():
	if(allEnabled): return lights_available.values()
	return [l for (n,l) in lights_available.items() if n in lights_enabled]

if __name__ == "__main__":
	print("Finding lights available")
	discover_lights()
	print("Found",len(lights_available),"light(s)")
	setUpLights()

	print("Done, opening web socket")
	start_socket()

max_stars_repo_name: Fallstop/YeeSaber

max_issues_repo_name: Fallstop/YeeSaber

max_forks_repo_name: Fallstop/YeeSaber


text:
import unittest
import sympy, numpy, random
import kgprim.values as values

class ValuesTests(unittest.TestCase):
    def test_variable_symbol_name(self):
        name = 'whatever'
        v = values.Variable(name)
        self.assertTrue( v.symbol.name == name )

    def test_constant_value(self):
        value = random.random()
        c = values.Constant(name="c", value=value)
        self.assertTrue( c.value == value )

        e = values.Expression(c)
        self.assertTrue( e.evalf() == value )

    def test_pi_expr_value(self):
        '''Test numerical evaluation of a PI expression'''
        coeff = random.random()
        pi = values.MyPI.instance()
        e = values.Expression(pi, pi.symbol * coeff)
        err = round( e.evalf() - numpy.pi*coeff, 6 )
        self.assertTrue( err == 0.0 )

    def test_no_evalf(self):
        '''Variables and Parameters do not evaluate to floats'''
        v = values.Variable('v')
        p = values.Parameter(name='p', defValue=0.0) # even if it has a default value

        e = values.Expression(v)
        self.assertRaises(RuntimeError, e.evalf)

        e = values.Expression(p)
        self.assertRaises(RuntimeError, e.evalf)

    def test_expression_attributes(self):
        '''Test consistency of the attributes of a `kgprim.values.Expression`'''

        name = "v1"
        v1 = values.Variable(name)
        e1 = values.Expression(v1)

        # construct a non-trivial expression of v1
        e2 = 3 * e1;

        s1 = sympy.Symbol(name)

        # Check that the argument of the new expression is still the same
        # Variable, and then check the Sympy attributes
        self.assertTrue( e2.argument == v1 )
        self.assertTrue( e2.argument.symbol == s1 )
        self.assertTrue( e2.expr == 3*s1 )

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

reason: import sympy


text:
###################################################
#
# Functions for downloading the CIFAR-10 dataset
#
###################################################

from clint.textui import progress

import numpy as np
import download
import requests
import tarfile
import hashlib
import shutil
import pickle
import glob
import os

# Configuration

_CIFAR_10_URL_ = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
_CIFAR_10_MD5_ = "c58f30108f718f92721af3b95e74349a"
_PATH_CPICKLE_ = "DATA/cifar-10-batches-py/"
_PATH_ARCHIVE_ = "DATA/archive.tar.gz"
_PATH_EXTRACT_ = "DATA/"
_FILES_TRAIN_ = "data_batch*"
_FILES_TEST_ = "test_batch*"
_FILE_META_ = "batches.meta"
_KEY_X_ = b'data'
_KEY_Y_ = b'labels'
_KEY_L_ = b'label_names'

# Logic

def __cifar_prompt(question):
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    prompt = " [Y/n] "

    while True:
        raw_inp = input(question + prompt)
        choice = raw_inp.lower()
        if choice == '':
            return True
        elif choice in valid:
            return valid[choice]
        else:
            print("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")

def __cifar_exists():
    if not os.path.isfile(_PATH_ARCHIVE_):
        return False
    file = open(_PATH_ARCHIVE_, 'rb')
    hash = hashlib.md5(file.read()).hexdigest()
    if hash == _CIFAR_10_MD5_:
        return True
    return hash

def __cifar_download():
    print("--> Downloading data...")
    request = requests.get(_CIFAR_10_URL_, stream=True)
    with open(_PATH_ARCHIVE_, 'wb') as f:
        total_length = int(request.headers.get('content-length'))
        for chunk in progress.bar(request.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
            if chunk:
                f.write(chunk)
                f.flush()

def __cifar_extract():
    print("--> Extracting data...")
    if os.path.exists(_PATH_CPICKLE_):
        if not __cifar_prompt("Target folder exists already. Replace?"):
            return
        shutil.rmtree(_PATH_CPICKLE_)

    tar = tarfile.open(_PATH_ARCHIVE_, "r:gz")
    tar.extractall(path = _PATH_EXTRACT_)
    tar.close()

def __cifar_unpickle(file):
    #print("Loading %s..." % file, end="")
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict
    #print(" ok")

def cifar_download():
    print("--> Loading CIFAR-10...")
    state = __cifar_exists()
    if state == True:
        print("File already present!")
        print("MD5-Hash matches: %s" % _CIFAR_10_MD5_)
    elif state == False:
        print("File not present!")
        __cifar_download()
    else:
        print("File already present!")
        print("MD5-Hash mismatch:\n\texpected:\t%s\n\tcalculated:\t%s" % (_CIFAR_10_MD5_, state))
        if __cifar_prompt("Replace file?"):
            __cifar_download()

    __cifar_extract()
    print("--> CIFAR-10 loaded sucessfully")

def __cifar_load_batch(pattern):
    if not os.path.exists(_PATH_CPICKLE_):
        raise Exception("Data not found. Run cifar.py as a script to download the data!")
    files = glob.glob(_PATH_CPICKLE_ + pattern)
    batch = __cifar_unpickle(files[0])
    X = batch[_KEY_X_]
    y = batch[_KEY_Y_]
    for file in range(1, len(files)):
        batch = __cifar_unpickle(files[file])
        X = np.append(X, batch[_KEY_X_], axis=0)
        y = np.append(y, batch[_KEY_Y_])
    assert(X.shape[0] == len(y))
    return X, y

def cifar_ready():
    return os.path.exists(_PATH_CPICKLE_)

def cifar_load_train():
    return __cifar_load_batch(_FILES_TRAIN_)

def cifar_load_test():
    return __cifar_load_batch(_FILES_TEST_)

def cifar_load_labels():
    return __cifar_unpickle(_PATH_CPICKLE_ + _FILE_META_)[_KEY_L_]

if __name__ == "__main__":
    cifar_download()

max_stars_repo_name: StoneLabs/cifarpy

max_issues_repo_name: StoneLabs/cifarpy

max_forks_repo_name: StoneLabs/cifarpy


text:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import datetime

import cv2
import base64
import numpy as np
from PIL import Image

from aniso.tensor import DiffusionTensor2D
from aniso.derivatives import apply_isotropic_smoothing
from aniso.dash_reusable_components import b64_to_numpy, b64_to_pil

logo = 'test_images/aniso.png'
encoded_logo = base64.b64encode(open(logo, 'rb').read())

image = np.asarray(Image.open('test_images/crabnebula.jpg'))
dtensor = DiffusionTensor2D.fromimage(image)

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

markdown_intro = '''
### An anisotropic image smoother

Adjust the smoothing parameter to increase (or decrease) smoothing.
The leftmost image is the original image, the middle is the image after
isotropic smoothing and the rightmost image is the anisotropically smoothed image.
'''

markdown_instr = '''
Go head, try and upload your own image!
'''

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Img(src='data:image/png;base64,{}'.format(encoded_logo.decode()), style={'textAlign': 'center'}),
    dcc.Markdown(children=markdown_intro),
    dcc.Slider(
        id='alpha-slider',
        min=1,
        max=100,
        value=10,
        marks={str(alpha): str(alpha) for alpha in np.arange(0, 110, 10)},
        step=5
    ),
    dcc.Graph(id='graph'),
    dcc.Markdown(children=markdown_instr),
    dcc.Upload(
        id='upload-image',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        }
    ),
    dcc.Graph(id='user-graph'),
    dcc.Slider(
        id='user-alpha-slider',
        min=1,
        max=100,
        value=50,
        marks={str(alpha): str(alpha) for alpha in np.arange(0, 110, 10)},
        step=5
    )
])

@app.callback(
    Output('graph', 'figure'),
    Input('alpha-slider', 'value'))
def update_image(alpha):
    fig = make_subplots(1,3)
    fig.add_trace(go.Image(z=image), 1, 1)

    iso_smooth = np.zeros_like(image)
    for i in range(3):
        iso_smooth[:, :, i] = apply_isotropic_smoothing(image[:, :, i], alpha=alpha)

    fig.add_trace(go.Image(z=iso_smooth), 1, 2)

    aniso_smooth = dtensor.smooth(image, alpha=alpha)
    fig.add_trace(go.Image(z=aniso_smooth), 1, 3)

    fig.update_layout(transition_duration=500)

    return fig

@app.callback(
    Output('user-graph', 'figure'),
    Input('upload-image', 'contents'),
    Input('user-alpha-slider', 'value'))
def smooth_user_image(contents, alpha):

    image = load_image(contents)
    user_dtensor = DiffusionTensor2D.fromimage(image)

    fig = make_subplots(1,3)
    fig.add_trace(go.Image(z=image), 1, 1)

    iso_smooth = np.zeros_like(image)
    for i in range(3):
        iso_smooth[:, :, i] = apply_isotropic_smoothing(image[:, :, i], alpha=alpha)

    fig.add_trace(go.Image(z=iso_smooth), 1, 2)

    aniso_smooth = user_dtensor.smooth(image, alpha=alpha)
    fig.add_trace(go.Image(z=aniso_smooth), 1, 3)

    fig.update_layout(transition_duration=500)

    return fig

def load_image(contents):
    # Convert to numpy array
    content_string = contents.split(';base64,')[-1]
    image = np.asarray(b64_to_pil(content_string))

    height, width = image.shape[:2]
    max_height, max_width = (1024, 1024)

    height, width = image.shape[:2]

    if height > max_height or width > max_width:
        image = downsize_image(image, max_height, max_width)

    return image

def downsize_image(image, max_height, max_width):

    height, width = image.shape[:2]

    if height > width:
        scaling_factor = max_height / float(height)
    else:
        scaling_factor = max_width / float(width)

    image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)

    return image

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', port=8050, debug=True)

max_stars_repo_name: gianmatharu/Aniso

max_issues_repo_name: gianmatharu/Aniso

max_forks_repo_name: gianmatharu/Aniso


text:
from PauliInteraction import PauliInteraction
from Ising import Ising
from CoefficientGenerator import CoefficientGenerator
from Evaluator import Evaluator

#from DataVisualizer import DataVisualizer
#from DataLogger import DataLogger

import Driver as Driver_H
import HardInstances as HI

from MasterEquation import MasterEquation
from QuantumAnnealer import QuantumAnnealer
from Test import test

import qutip as qp
import numpy as np
import time as time

X_HAT = "X"
Y_HAT = "Y"
Z_HAT = "Z"

N = 7
T = 100
NUM_INTERACTIONS = int((N * (N - 1)) / 2)
RAND_COEF_FLAG = False
DRIVER_FLAG = False

RAND_INSTANCES = 100
# MAIN TEST CASES FROM SAMPLE CODE

def main():

    for i in range(0, RAND_INSTANCES):
        test(RAND_COEF_FLAG, i, DRIVER_FLAG)

    #test(RAND_COEF_FLAG, 0, DRIVER_FLAG)

if __name__ == "__main__":
    main()


text:
from pyscenario import *
from util.arg import *
import util.scenario
import numpy as np

class Scenario(PyScenario):
    def get_description(self):
        return 'Test spatial taycast functionality'

    def get_map(self):
        return 'four-square'

    def get_map_parameters(self):
        return {'l':arg_get('-map_l', 50.0)}

    def init(self):
        util.scenario.add_random_cars(self, arg_get('-car_count', 5), component_names=['wa'])

    def step(self, frame, t, dt):
        origin = np.multiply(self.coord.height_3d(), 1.7)
        azimuth_center = (t * 20.0) - int((t*2.0)/360)*360
        hits = self.spatial_eng.cast_rays_spherical(origin, np.deg2rad(azimuth_center-90), np.deg2rad(azimuth_center+90), 60,
                                np.deg2rad(-20), np.deg2rad(-3), 40, 300.0)
        for hit in hits:
            self.dbg_draw.add_point(hit[1])

max_stars_repo_name: jadnohra/daisy

max_issues_repo_name: jadnohra/daisy

max_forks_repo_name: jadnohra/daisy


text:
#!/usr/bin/python3

import gym
from gym import wrappers
import sys
import argparse
import numpy as np
import sklearn.pipeline
import sklearn.preprocessing
from sklearn.linear_model import SGDRegressor
from sklearn.kernel_approximation import RBFSampler

from matplotlib import pyplot as plt

plt.style.use("ggplot")

def plot_unimetric(history, metric, save_dir):
    plt.figure()
    plt.plot(history[metric])
    plt.title('model {}'.format(metric))
    plt.ylabel(metric)
    plt.xlabel('epoch')
    plt.savefig("{}/{}.png".format(save_dir, metric),
                format='png', dpi=300)

def make_epsilon_greedy_policy(estimator, epsilon, nA):
    """
    Creates an epsilon-greedy policy based on a given Q-function approximator and epsilon.

    Args:
        estimator: An estimator that returns q values for a given state
        epsilon: The probability to select a random action . float between 0 and 1.
        nA: Number of actions in the environment.

    Returns:
        A function that takes the observation as an argument and returns
        the probabilities for each action in the form of a numpy array of length nA.

    """

    def policy_fn(observation):
        A = np.ones(nA, dtype=float) * epsilon / nA
        q_values = estimator.predict(observation)
        best_action = np.argmax(q_values)
        A[best_action] += (1.0 - epsilon)
        return A

    return policy_fn

class Estimator(object):
    """
    Value Function approximator.
    """

    def __init__(self, env):
        self._prepare_estimator_for_env(env)
        # We create a separate model for each action in the environment's
        # action space. Alternatively we could somehow encode the action
        # into the features, but this way it's easier to code up.
        self.models = []
        for _ in range(env.action_space.n):
            model = SGDRegressor(learning_rate="constant")
            # We need to call partial_fit once to initialize the model
            # or we get a NotFittedError when trying to make a prediction
            # This is quite hacky.
            model.partial_fit([self.featurize_state(env.reset())], [0])
            self.models.append(model)

    def _prepare_estimator_for_env(self, env):
        observation_examples = np.array(
            [env.observation_space.sample() for _ in range(1000)])
        observation_examples = self._vectorise_state(observation_examples)

        scaler = sklearn.preprocessing.StandardScaler()
        scaler.fit(observation_examples)
        self.scaler = scaler

        featurizer = sklearn.pipeline.FeatureUnion([
            ("rbf1", RBFSampler(gamma=5.0, n_components=100)),
            ("rbf2", RBFSampler(gamma=2.0, n_components=100)),
            ("rbf3", RBFSampler(gamma=1.0, n_components=100)),
            ("rbf4", RBFSampler(gamma=0.5, n_components=100))
        ])
        featurizer.fit(scaler.transform(observation_examples))
        self.featurizer = featurizer

    def _vectorise_state(self, states):
        obs_shape = states.shape
        if len(obs_shape) > 2:
            states = states.reshape((obs_shape[0], -1))
        return states

    def featurize_state(self, state):
        """
        Returns the featurized representation for a state.
        """
        state = self._vectorise_state(np.array([state]))
        scaled = self.scaler.transform(state)
        featurized = self.featurizer.transform(scaled)
        return featurized[0]

    def predict(self, s, a=None):
        """
        Makes value function predictions.

        Args:
            s: state to make a prediction for
            a: (Optional) action to make a prediction for

        Returns
            If an action a is given this returns a single number as the prediction.
            If no action is given this returns a vector or predictions for all actions
            in the environment where pred[i] is the prediction for action i.

        """
        features = self.featurize_state(s)
        return self.models[a].predict([features])[0] if a \
            else np.array([model.predict([features])[0] for model in self.models])

    def update(self, s, a, y):
        """
        Updates the estimator parameters for a given state and action towards
        the target y.
        """
        features = self.featurize_state(s)
        self.models[a].partial_fit([features], [y])

def q_learning(env, estimator, num_episodes, discount_factor=1.0, epsilon=0.1, epsilon_decay=1.0,
               verbose=False):
    """
    Q-Learning algorithm for fff-policy TD control using Function Approximation.
    Finds the optimal greedy policy while following an epsilon-greedy policy.

    Args:
        env: OpenAI environment.
        estimator: Action-Value function estimator
        num_episodes: Number of episodes to run for.
        discount_factor: Lambda time discount factor.
        epsilon: Chance the sample a random action. Float betwen 0 and 1.
        epsilon_decay: Each episode, epsilon is decayed by this factor

    Returns:
        An EpisodeStats object with two numpy arrays for episode_lengths and episode_rewards.
    """

    # Keeps track of useful statistics
    episode_lengths = np.zeros(num_episodes)
    episode_rewards = np.zeros(num_episodes)

    for i_episode in range(num_episodes):

        # The policy we're following
        policy = make_epsilon_greedy_policy(
            estimator, epsilon * epsilon_decay ** i_episode, env.action_space.n)

        # Print out which episode we're on, useful for debugging.
        # Also print reward for last episode
        if verbose:
            last_reward = episode_rewards[i_episode - 1]
            print("\rEpisode {}/{} ({})".format(i_episode + 1, num_episodes, last_reward), end="")
            sys.stdout.flush()

        state = env.reset()
        n_action = None

        len_counter = 0
        reward_counter = 0
        done = False
        while not done:
            if verbose:
                pass
                # env.render()
            if n_action is None:
                probs = policy(state)
                action = np.random.choice(np.arange(len(probs)), p=probs)
            else:
                action = n_action

            n_state, reward, done, info = env.step(action)
            reward_counter += reward
            len_counter += 1

            q_val_next = estimator.predict(n_state)
            td_target = reward + discount_factor * np.max(q_val_next)

            estimator.update(state, action, td_target)

            state = n_state

        episode_rewards[i_episode] = reward_counter
        episode_lengths[i_episode] = len_counter

    return {"episode_rewards": episode_rewards, "episode_lengths": episode_lengths}

def _parse_args():
    parser = argparse.ArgumentParser(description='Policy iteration example')
    parser.add_argument(
        '--env',
        type=str,
        default='MountainCar-v0',  # CartPole-v0, MountainCar-v0
        help='The environment to use')
    parser.add_argument(
        '--num_episodes',
        type=int,
        default=1000,
        help='Number of episodes')
    parser.add_argument(
        '--gamma',
        type=float,
        default=0.99,
        help='Gamma discount factor')
    parser.add_argument(
        '--verbose',
        action='store_true',
        default=False)
    parser.add_argument(
        '--plot_stats',
        action='store_true',
        default=False)
    parser.add_argument(
        '--api_key',
        type=str,
        default=None)

    args, _ = parser.parse_known_args()
    return args

def save_stats(stats, save_dir="./"):
    for key in stats:
        plot_unimetric(stats, key, save_dir)

def run(env, n_episodes, discount_factor, verbose=False, plot_stats=False, api_key=None):
    env_name = env
    env = gym.make(env)

    estimator = Estimator(env)

    if api_key is not None:
        env = gym.wrappers.Monitor(env, "/tmp/" + env_name, force=True)

    stats = q_learning(env, estimator, n_episodes,
                       discount_factor=discount_factor, epsilon=0.0,
                       verbose=verbose)
    if plot_stats:
        save_stats(stats)

    if api_key is not None:
        env.close()
        gym.upload("/tmp/" + env_name, api_key=api_key)

def main():
    args = _parse_args()
    run(args.env, args.num_episodes, args.gamma,
        args.verbose, args.plot_stats, args.api_key)

if __name__ == '__main__':
    main()

max_stars_repo_name: Scitator/rl-course-experiments

max_issues_repo_name: Scitator/rl-course-experiments

max_forks_repo_name: Scitator/rl-course-experiments


text:
from __future__ import print_function
import sys
import copy
import random
import numpy as np
from collections import defaultdict
import os

def data_partition(fname, FLAGS):
    print("inside utility function")
    usernum = 1
    itemnum = 1
    User = defaultdict(list)
    user_train = {}
    user_valid = {}
    user_test = {}
    # assume user/item index starting from 1
    f = open(fname, 'r')
    for line in f:
        u, i = line.rstrip().split(' ')
        u = int(u)
        i = int(i)
        usernum = max(u, usernum)
        itemnum = max(i, itemnum)
        User[u].append(i)
    path = FLAGS.data_path_kgat + FLAGS.dataset_name
    for user in User:
        nfeedback = len(User[user])
        if nfeedback < 3:
            user_train[user] = User[user]
            user_valid[user] = []
            user_test[user] = []
        else:
            user_train[user] = User[user][:-2]
            user_valid[user] = []
            user_valid[user].append(User[user][-2])
            user_test[user] = []
            user_test[user].append(User[user][-1])
                #print " ".join(map(str, user_train[user]))

        with open(path + "/"+ "train.txt", "a") as train_file:
            train_file.write(str(user) + " " +" ".join([str(x) for x in user_train[user]]) + "\n")

        with open(path + "/"+ "test.txt", "a") as test_file:
            test_file.write(str(user) + " " +" ".join([str(x) for x in user_test[user]]) + "\n")

    return [user_train, user_valid, user_test, usernum, itemnum]

max_stars_repo_name: DanialTaheri/KATRec

max_issues_repo_name: js-ts/KATRec

max_forks_repo_name: js-ts/KATRec


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>

# <markdowncell>

# First, we're going to import some modules, add a few utility functions, and specify some symbols and variables we need.

# <codecell>

# Import all sorts of things
from sympy.interactive.printing import init_printing
init_printing(use_unicode=False, wrap_line=False, no_global=True)
from sympy.matrices import *
from sympy import var, sin, cos, symbol, MatrixSymbol, Matrix, eye, Symbol, simplify, cse, solve, jscode, Function, acos, asin, sqrt, Wild
from functools import reduce
from jinja2 import Template
import re
import sys

# <codecell>

# Replace references to matrix elements with JS references
def subs_matrix_elements(expr_str, matrix_name):
    for row in range(4):
        for col in range(4):
            expr_str = expr_str.replace("%s_%d,%d" % (matrix_name, row, col),
                                        "%s.elements[%d]" % (matrix_name,
                                                             4*col + row,))
    return expr_str

# Given an expression, find the parameters that will be needed
def extract_parameters(expr):
    atoms = expr.atoms(Symbol)
    matrices = set([])
    scalars = set([])
    for atom in atoms:
        matrix_name = re.findall("([a-zA-Z]+)_\d,\d", atom.name)
        if len(matrix_name) > 0:
            matrices.add(matrix_name[0])
        else:
            scalars.add(atom.name)

    # Put in a nice order
    matrices = list(matrices)
    matrices.sort()
    scalars = list(scalars)
    scalars.sort()
    return matrices+scalars

# Dummy matrix for solving
def make_dummy_matrix(name):
    elements = []
    for i in range(4):
        for j in range(4):
            elements.append(Symbol("%s_%d,%d" % (name, i, j)))

    return Matrix(4,4, elements)

# Formats a 4x4 Sympy matrix as a Javascript matrix
def format_js_mat(mat):
    js_exprs = [jsify_expr(x) for x in mat]
    return "new THREE.Matrix4().set(%s)" % (", ".join(js_exprs),)

# Makes massive redundant expression easier to read for debugging
def format_big_expr(expr):
    terms, vec = cse(expr)
    return (Matrix(terms), vec)

def jsify_expr(expr):
    clamp = Function("clamp")
    bottom_clamp = Function("bottom")
    x = Wild("x")
    # Prevent NaNs on inverse trig functions
    expr = expr.replace(asin, lambda x: asin(clamp(x, -1, 1)))
    expr = expr.replace(acos, lambda x: acos(clamp(x, -1, 1)))
    # Prevent NaNs on sqrts
    expr = expr.replace(sqrt(x + 1),
                        sqrt(bottom_clamp(x + 1, 0)))
    js_expr = jscode(expr, user_functions = {
            "clamp": "THREE.Math.clamp",
            "bottom": "THREE.Math.clampBottom"
        })
    # Convert all matrix references for compatibility with
    # three.js
    atoms = expr.atoms(Symbol)
    matrices = set([])
    for atom in atoms:
        matrix_name = re.findall("([a-zA-Z]+)_\d,\d", atom.name)
        if len(matrix_name) > 0:
            matrices.add(matrix_name[0])
    for matrix in matrices:
        js_expr = subs_matrix_elements(js_expr, matrix)
    return js_expr

# Utilities to format JS
def jsify_terms(terms):
    lines = []
    for var_name, term in terms:
        term_js = jsify_expr(term)
        lines.append("var %s = %s;" % (jscode(var_name), term_js))
    return "\n".join(lines)

def jsify_list(list_expr):
    return "[%s]" % ", ".join([jsify_expr(x) for x in list_expr])

# <codecell>

# Variable setup
for i in range(1,8):
    var("theta_" + str(i))

for i in range(1,8):
    var("s_%d" % i)
    var("c_%d" % i)

# Shoulder location
var("shoulder_x")
var("shoulder_y")
var("shoulder_z")
# Arm bone length
var("l_4")
var("l_5")

# <markdowncell>

# We're making assumptions about the model geometry here. We assume that the x-axis is used to
# displace the elbow from the shoulder and the wrist from the elbow. We also assume that, in
# the elbow's coordinate system the arm is bending in the XZ-plane and the wrist twists in
# the YZ-plane.

# <codecell>

R = [Matrix([[1, 0, 0, shoulder_x],
             [0, 1, 0, shoulder_y],
             [0, 0, 1, shoulder_z],
             [0, 0, 0, 1]])]
R.append(Matrix([[   1,    0,    0, 0],
                 [   0,  c_1, -s_1, 0],
                 [   0,  s_1,  c_1, 0],
                 [   0,    0,    0, 1]]))
R.append(Matrix([[ c_2, -s_2,    0, 0],
                 [ s_2,  c_2,    0, 0],
                 [   0,    0,    1, 0],
                 [   0,    0,    0, 1]]))
R.append(Matrix([[ c_3,    0, -s_3, 0],
                 [   0,    1,    0, 0],
                 [ s_3,    0,  c_3, 0],
                 [  0,     0,    0, 1]]))
R.append(Matrix([[ c_4,    0, -s_4, l_4],
                 [   0,    1,    0, 0],
                 [ s_4,    0,  c_4, 0],
                 [   0,    0,    0, 1]]))
R.append(Matrix([[   1,    0,    0, 0],
                 [   0,  c_5, -s_5, 0],
                 [   0,  s_5,  c_5, 0],
                 [   0,    0,    0, 1]]))
R.append(Matrix([[ c_6, -s_6,    0, l_5],
                 [ s_6,  c_6,    0, 0],
                 [   0,    0,    1, 0],
                 [   0,    0,    0, 1]]))
R.append(Matrix([[ c_7,    0, -s_7, 0],
                 [   0,    1,    0, 0],
                 [ s_7,    0,  c_7, 0],
                 [   0,    0,    0, 1]]))

z_0 = Matrix([0,0,1,0])
o_0 = Matrix([0,0,0,1])

# These are to represent a black box matrix for the joints
# to use in a solver
dummy_shoulder_mat = make_dummy_matrix("shoulder")
dummy_elbow_mat = make_dummy_matrix("elbow")
dummy_wrist_mat = make_dummy_matrix("wrist")
generic_mat4 = make_dummy_matrix("M")

# These are to specify a new matrix for the joints
shoulder_mat = R[0] * R[1] * R[2] * R[3]
elbow_mat = R[4] * R[5]
wrist_mat = R[6] * R[7]

def rotation_matrix(i):
    R_0_i = eye(4)
    for i in range(0, i+1):
        R_0_i = R_0_i * R[i]
    return R_0_i

R_0 = [rotation_matrix(i) for i in range(8)]
o_7 = R_0[7] * o_0

# External variables
# The current hand world matrix
hand = make_dummy_matrix("hand")
# The current torso world matrix
torso = make_dummy_matrix("torso")
# The hand displacement for this step
delta_s = Symbol("delta_s")
# rotational displacement
delta_r = Symbol("delta_r")

# <markdowncell>

# We're going to work on the configuration, but, before we can work with the configuration, we need to calculate a sane initial configuration from the model matrices.

# <codecell>

from sympy.solvers import solve
from sympy import Symbol

# Replace the placeholder trig function variables with actual trig functions
def subs_trig_exprs(expr):
    result = expr
    for i in range(1,8):
        result = result.subs("s_%d" % (i,), "sin(theta_%d)" % (i,))
        result = result.subs("c_%d" % (i,), "cos(theta_%d)" % (i,))
    return result

trig_shoulder = subs_trig_exprs(shoulder_mat)
trig_elbow = subs_trig_exprs(elbow_mat)
trig_wrist = subs_trig_exprs(wrist_mat)

shoulder_solns = solve(dummy_shoulder_mat - trig_shoulder, theta_1, theta_2, theta_3)
elbow_solns = solve(dummy_elbow_mat - trig_elbow, theta_4, theta_5)
wrist_solns = solve(dummy_wrist_mat - trig_wrist, theta_6, theta_7)

def find_shortest_soln(solns):
    shortest_sol_len = float("inf")
    shortest_sol = None
    for sol in solns:
        if len(str(sol)) < shortest_sol_len:
            shortest_sol_len = len(str(sol))
            shortest_sol = sol
    return shortest_sol

inverse_config = reduce(lambda x, y: x+y, [list(find_shortest_soln(solns)) for solns in [shoulder_solns, elbow_solns, wrist_solns]])

config_terms, config_list = cse(inverse_config)
config_terms_js = jsify_terms(config_terms)
config_list_js = ", ".join([jsify_expr(x) for x in config_list])
config_list_js = "[%s]" %config_list_js

# <markdowncell>

# Maybe I'm calculating the Jacobian wrong? Sympy has a Jacobian function. I also just realized that those angles have nothing to do with the actual Euler angle of the hand. Since the most important component of the hand orientation is the direction the palm of the hand faces, we will take a local palm vector, which corresponds to a point on the palm, and rotate it into world space. This is nice because all the units will match, allowing us to use Euclidean distance.

# <codecell>

local_palm_vector = Matrix([Symbol("palm_x"), Symbol("palm_y"), Symbol("palm_z"), 1])
global_palm_vector = R_0[7] * local_palm_vector

target2 = Matrix([Symbol("target_%s" % s) for s in ["x", "y", "z", "palm_x", "palm_y", "palm_z"]])

hand_pose2 = Matrix([R_0[7][0,3],
                     R_0[7][1,3],
                     R_0[7][2,3]]).col_join(global_palm_vector[:3,0])

hand_pose2 = hand_pose2.subs([("shoulder_%s" % axis, dummy_shoulder_mat[row, 3])
                                  for row, axis in enumerate(["x","y","z"])])
hand_pose2 = subs_trig_exprs(hand_pose2)
J2 = hand_pose2.jacobian(
        [Symbol("theta_%d" % i) for i in range(1,8)])
hand_dist = (Matrix(hand_pose2)-target2).norm()
delta_pose2 = delta_s * (Matrix(hand_pose2)-target2)
delta_config2 = J2.T * delta_pose2
new_config = Matrix([Symbol("theta_%d" % i) for i in range(1,8)]) + delta_config2

# <codecell>

step_config_terms, step_config_list = cse(new_config, optimizations='basic')
step_config_terms_js = jsify_terms(step_config_terms)
step_config_list_js = jsify_list(step_config_list[0])
extract_parameters(new_config)

# <codecell>

# Joining all the matrices is a hack to get around a problem in cse
config_mats = trig_shoulder.col_join(trig_elbow).col_join(trig_wrist)
config_mats = config_mats.subs([("shoulder_%s" % axis, dummy_shoulder_mat[row, 3])                                      for row, axis in enumerate(["x","y","z"])])
config_mat_terms, config_mat_clean = cse(config_mats, optimizations='basic')
config_mat_terms_js = jsify_terms(config_mat_terms)
config_shoulder_mat_js = format_js_mat(config_mat_clean[0][0:4,:])
config_elbow_mat_js = format_js_mat(config_mat_clean[0][4:8,:])
config_wrist_mat_js = format_js_mat(config_mat_clean[0][8:12,:])

# <codecell>

hand_dist_terms, hand_dist_clean = cse(hand_dist, optimizations='basic')
hand_dist_terms_js = jsify_terms(hand_dist_terms)
hand_dist_js = jsify_expr(hand_dist_clean[0])

# <codecell>

func_template2 = Template("""

// This file is automatically generated. Do not commit or edit. Instead, edit
// build_motion.py

'use strict';

var THREE = require('./three');

/** @exports vbot/motion */

/**
 * Calculates the distance of the hand from it's target in the workspace.
 *
 * @memberof module:vbot/motion
 * @param {THREE.Matrix4} shoulder the local shoulder matrix
 * @param {Number} upperArmLength
 * @param {Number} lowerArmLength
 * @param {THREE.Vector3} localPalmPos the location of the center of
 *                                     the palm in wrist space
 * @param {THREE.Vector3} targetWristPos the target position of the
 *                                       wrist in torso space
 * @param {THREE.Vector3} targetPalmPos the target position of the
 *                                      center of the palm in torso
 *                                      space
 * @param {Number[]} config the configuration of the joints
 * @returns {Number} the distance of config from the target
 */
function handDist(shoulder,
                  upperArmLength,
                  lowerArmLength,
                  localPalmPos,
                  targetWristPos,
                  targetPalmPos,
                  config) {
    var theta_1 = config[0];
    var theta_2 = config[1];
    var theta_3 = config[2];
    var theta_4 = config[3];
    var theta_5 = config[4];
    var theta_6 = config[5];
    var theta_7 = config[6];
    var target_x = targetWristPos.x;
    var target_y = targetWristPos.y;
    var target_z = targetWristPos.z;
    var target_palm_x = targetPalmPos.x;
    var target_palm_y = targetPalmPos.y;
    var target_palm_z = targetPalmPos.z;
    var palm_x = localPalmPos.x;
    var palm_y = localPalmPos.y;
    var palm_z = localPalmPos.z;
    var l_4 = upperArmLength;
    var l_5 = lowerArmLength;
    {{hand_dist_terms|indent|indent}}
    return {{hand_dist}};
}

/**
 * Calculates the configuration parameters from matrices.
 *
 * @param {THREE.Matrix4} shoulder the local shoulder matrix
 * @param {THREE.Matrix4} elbow the local elbow matrix
 * @param {THREE.Matrix4} wrist the local wrist matrix
 * @returns {Number[]} the configuration those matrices indicate
 */
function calcConfig(shoulder, elbow, wrist) {
    {{config_terms|indent|indent}}
    return {{config_list}};
}

/**
 * Calculates the next step of motion in configuration space.
 *
 * @memberof module:vbot/motion
 * @param {THREE.Matrix4} shoulder the local shoulder matrix
 * @param {Number[]} config the configuration of the joints
 * @param {Number} delta_s the distance to step the hand
 * @param {Number} upperArmLength
 * @param {Number} lowerArmLength
 * @param {THREE.Vector3} localPalmPos the location of the center of
 *                                     the palm in wrist space
 * @param {THREE.Vector3} targetWristPos the target position of the
 *                                       wrist in torso space
 * @param {THREE.Vector3} targetPalmPos the target position of the
 *                                      center of the palm in torso
 *                                      space
 * @returns {Number[]} the new configuration
 */
function stepConfig(shoulder,
                    config,
                    delta_s,
                    upperArmLength,
                    lowerArmLength,
                    localPalmPos,
                    targetWristPos,
                    targetPalmPos) {
    var l_4 = upperArmLength;
    var l_5 = lowerArmLength;
    var theta_1 = config[0];
    var theta_2 = config[1];
    var theta_3 = config[2];
    var theta_4 = config[3];
    var theta_5 = config[4];
    var theta_6 = config[5];
    var theta_7 = config[6];
    var target_x = targetWristPos.x;
    var target_y = targetWristPos.y;
    var target_z = targetWristPos.z;
    var target_palm_x = targetPalmPos.x;
    var target_palm_y = targetPalmPos.y;
    var target_palm_z = targetPalmPos.z;
    var palm_x = localPalmPos.x;
    var palm_y = localPalmPos.y;
    var palm_z = localPalmPos.z;
    {{step_config_terms|indent|indent}}
    return {{step_config_list}};
}

/**
 * Calculates the matrices of each joint
 *
 * @memberof module:vbot/motion
 * @param {THREE.Matrix4} shoulder the local shoulder matrix
 * @param {Number} upperArmLength
 * @param {Number} lowerArmLength
 * @param {Number[]} config the configuration of the joints
 * @returns {Object} the matrices
 */
function configToMatrices(shoulder,
                          upperArmLength,
                          lowerArmLength,
                          config) {
    var theta_1 = config[0];
    var theta_2 = config[1];
    var theta_3 = config[2];
    var theta_4 = config[3];
    var theta_5 = config[4];
    var theta_6 = config[5];
    var theta_7 = config[6];
    var l_4 = upperArmLength;
    var l_5 = lowerArmLength;
    {{config_mat_terms|indent|indent}}
    return {
        shoulder: {{config_shoulder_mat}},
        elbow: {{config_elbow_mat}},
        wrist: {{config_wrist_mat}}
    };
}

module.exports = {
    configToMatrices: configToMatrices,
    stepConfig: stepConfig,
    calcConfig: calcConfig,
    handDist: handDist
};
""")

code = func_template2.render(config_terms=config_terms_js,
                            config_list=config_list_js,
                            step_config_terms=step_config_terms_js,
                            step_config_list=step_config_list_js,
                            config_mat_terms=config_mat_terms_js,
                            config_shoulder_mat=config_shoulder_mat_js,
                            config_elbow_mat=config_elbow_mat_js,
                            config_wrist_mat=config_wrist_mat_js,
                            hand_dist_terms=hand_dist_terms_js,
                            hand_dist=hand_dist_js)

# If the code is running as a script, check for an argument for
# the target location.
def is_interactive():
    import __main__ as main
    return not hasattr(main, '__file__')

if not is_interactive():
    # If there is no argument, print to stdout
    if len(sys.argv) == 1:
        print(code)
    elif len(sys.argv) == 2:
        open(sys.argv[1], "w").write(code)
    else:
        print("Syntax: %s [<output file>]" % sys.argv[0])
        sys.exit(1)
else:
    print(code)

# <codecell>

max_stars_repo_name: TJSomething/ncb

max_issues_repo_name: TJSomething/ncb

max_forks_repo_name: TJSomething/ncb

reason: from sympy


text:
from core.things import Thing
import networkx as nx

class Environment:
    def __init__(self):
        self.things = []
        self.env = []

    def display(self):
        raise NotImplementedError

    def _perform_action(self, agent, action):
        raise NotImplementedError

    def _get_things_at(self, location: tuple, kind=Thing):
        return [thing for thing in self.things if thing.location == location and isinstance(thing, kind)]

    def _get_all_things(self, kind=Thing):
        return [thing for thing in self.things if isinstance(thing, kind)]

    def run(self, max_steps=20):
        raise NotImplementedError

    def add_thing(self, thing, location=None):
        if thing in self.things:
            print("Can't add the same thing twice")
        else:
            thing.location = location or thing.location or (0, 0)
            self.things.append(thing)

    def delete_thing(self, thing):
        try:
            self.things.remove(thing)
        except ValueError as e:
            print(e)

class Environment2D(Environment):
    def __init__(self, height=3, width=3):
        super().__init__()
        self.height = height
        self.width = width
        self.env = nx.grid_2d_graph(height, width)

    def display(self):
        i = 0
        for node in self.env.nodes():
            if i % self.width == 0:
                print('\n')
            print(str(node) + str(self._get_things_at(node)), end='\t')
            i += 1

    def _perform_action(self, agent, action):
        raise NotImplementedError

    def run(self, max_steps=20):
        raise NotImplementedError

max_stars_repo_name: pranav-ap/intelligent-agents

max_issues_repo_name: pranav-ap/intelligent-agents

max_forks_repo_name: pranav-ap/intelligent-agents

reason: import networkx


text:
#!/usr/bin/env python
#
## @file
#
# Generate a pattern for the SLM.
#
# Hazen 01/15
#

import numpy
import scipy
import scipy.misc
import sys

size_x = 1080
size_y = 1920
image = numpy.zeros((size_x, size_y)) + 127.0

# vertical grating.
if 0:
    for i in range(size_y):
        image[:,i] += -51.2 * i

# horizontal grating.
if 1:
    for i in range(size_x):
        image[i,:] += -51.2 * i

# Additional pattern.

midx = size_x/2
midy = size_y/2

# focus.
if 0:
    for i in range(size_x):
        for j in range(size_y):
            dx = (i - midx)
            dy = (j - midy)
            mag = -3.0e-2 * (dx * dx + dy * dy)
            image[i,j] += mag

# horizontal astigmatism
if 1:
    for i in range(size_y):
        dy = (i - midy) + 50
        mag = 0.07 * dy * dy
        image[:,i] += mag
    for i in range(size_x):
        dx = (i - midx) - 50
        if (abs(dx) > 200):
            image[i,:] = 127.0

# vertical astigmatism
if 0:
    for i in range(size_x):
        dx = (i - midx) + xoffset
        mag = 0.2 * dx * dx
        image[i,:] += mag
        #if (abs(dx) > 25):
        #    image[i,:] = 127
    for i in range(size_y):
        dx = (i - midy) + yoffset
        if (abs(dx) > 100):
            image[:,i] = 127.0

# light grid
if 0:
    cx = midx + 150
    cy = midy
    temp = numpy.copy(image)
    image = numpy.zeros((size_x, size_y)) + 127.0
    image[(cx-150):(cx+150),(cy+150):(cy+200)] = temp[(cx-150):(cx+150),(cy+150):(cy+200)]
    image[(cx-150):(cx+150),(cy-200):(cy-150)] = temp[(cx-150):(cx+150),(cy-200):(cy-150)]
    image[(cx+150):(cx+200),(cy-100):(cy+100)] = temp[(cx+150):(cx+200),(cy-100):(cy+100)]
    image[(cx-200):(cx-150),(cy-100):(cy+100)] = temp[(cx-200):(cx-150),(cy-100):(cy+100)]

# hole
if 0:
    cx = midx + xoffset
    cy = midy + yoffset
    for i in range(size_x):
        for j in range(size_y):
            dx = (i - cx)
            dy = (j - cy)
            if ((dx*dx + dy*dy) < (150*150)):
                image[i,j] = 127.0

# reduce na
if 0:
    cx = midx + xoffset
    cy = midy + yoffset
    for i in range(size_x):
        for j in range(size_y):
            dx = (i - cx)
            dy = (j - cy)
            if ((dx*dx + dy*dy) > (100*100)):
                image[i,j] = 127.0

# ring
if 0:
    cx = midx + 150
    cy = midy
    for i in range(size_x):
        for j in range(size_y):
            dx = (i - cx)
            dy = (j - cy)
            if ((dx*dx + dy*dy) > (300*300)):
                image[i,j] = 127.0
            elif ((dx*dx + dy*dy) < (250*250)):
                image[i,j] = 127.0

# airy beam
if 0:
    cx = midx + 50
    cy = midy - 50
    for i in range(size_x):
        for j in range(size_y):
            dx = (i - cx)
            dy = (j - cy)
            mag = 1.0e-4 * (dx * dx * dx + dy * dy * dy)
            image[i,j] += mag

image = numpy.round(image).astype(numpy.uint8)

scipy.misc.imsave(sys.argv[1], image)

reason: import numpy,import scipy


text:
import numpy as np
import torch

from torch_connectomics.data.dataset.misc import rebalance_binary_class

def test_rebalance():
    data = np.zeros([50,50],np.uint8)
    data[:30]=1
    data[:,30:]=1
    data_c = data.copy()
    data = torch.from_numpy(data)
    # check: from_numpy doesn't change value
    print((data.data.numpy()!=data_c).sum())

    data_t = data.clone()
    weight_factor, weight = rebalance_binary_class(data)
    print((data.data.numpy()!=data_t.data.numpy()).sum())

if __name__ == '__main__':
    test_rebalance()