text:
import numpy as np
import random
import time
import matplotlib.pyplot as plt
import sys
import warnings

if not sys.warnoptions:
     warnings.simplefilter("ignore")

class Blackjack:
    def __init__(self):
        self.states = None
        self.actions = ['hit', 'stand']
        self.cards = {'A': 11,
                      '2': 2,
                      '3': 3,
                      '4': 4,
                      '5': 5,
                      '6': 6,
                      '7': 7,
                      '8': 8,
                      '9': 9,
                      '10': 10,
                      'J': 10,
                      'Q': 10,
                      'K': 10}
        self.rewards = None
        self.q_rewards = None
        self.q_values = None
        self.policy = None

    def get_action(self):
        return self.actions

    def get_states(self):
        return self.states

    def get_cards(self):
        return self.cards

    def get_rewards(self):
        return self.rewards

    def get_q_rewards(self):
        return self.q_rewards

    def get_q_values(self):
        return self.q_values

    def get_policy(self):
        return self.policy

    def init_states(self):
        states = [{'cards':cards, 'usable_ace':usable_ace, 'dealer_card':dealer_card}
        for cards in range(12,22)
        for usable_ace in [True,False]
        for dealer_card in range(2,12)]
        self.states = states
        return None

    def init_rewards(self):
        rewards = {}
        for s in self.get_states():
            tmp = [v for k,v in s.items()]
            rewards[tuple(tmp)] = {'count':0, 'reward':0}

        self.rewards = rewards
        return None

    def init_q_rewards(self):
        rewards = {}
        Q = {}
        for s in self.get_states():
            tmp = [v for k,v in s.items()]
            for a in self.get_action():
                rewards[tuple(tmp), a] = {'count':0, 'reward':0}
                Q[tuple(tmp), a] = 0

        self.q_values = Q
        self.q_rewards = rewards
        return None

    def random_card(self):
        card = random.choice(list(self.get_cards().keys()))
        return self.get_cards()[card]

    def hit(self, msg, hand):
        curr_card = hand['cards']
        is_usable_ace = hand['usable_ace']
        new_card = self.random_card()
        new_hand = {'cards': curr_card + new_card, 'usable_ace': is_usable_ace}

        if is_usable_ace and new_hand['cards'] > 21:
            new_hand['cards'] -= 10
            new_hand['usable_ace'] = False

        else:
            if new_card == 11 and new_hand['cards'] <= 21:
                new_hand['usable_ace'] = True
            elif new_card == 11 and new_hand['cards'] > 21:
                new_hand['cards'] -= 10
                new_hand['usable_ace'] = False
        return new_hand

    def deal(self):
        natural = False
        first_card = self.random_card()
        second_card = self.random_card()
        sum_cards = first_card + second_card
        is_Ace = first_card == 11 or second_card == 11

        if sum_cards > 21:
            sum_cards -= 10
        player_hand = {'cards':sum_cards, 'usable_ace':is_Ace}

        if player_hand['cards'] == 21 and player_hand['usable_ace'] == True:
            natural = True

        while player_hand['cards'] < 12:
            new_hand = self.hit('deal',player_hand)
            player_hand = new_hand

        dealer_hidden_card = self.random_card()
        dealer_show_card = self.random_card()

        env = {'player hand': player_hand,
               'natural': natural,
               'dealer_hidden': dealer_hidden_card,
               'dealer_show': dealer_show_card}

        return env

    def play_hand(self, hand, action, dealer_hand):
        player = hand
        dealer = dealer_hand
        if action == 'stand':
            while dealer['cards'] < 17:
                new_hand = self.hit('dealer hit',dealer)
                dealer = new_hand
            if dealer['cards'] > 21 or dealer['cards'] < hand['cards']:
                return (1, 'Game Over')

            elif dealer['cards'] == player['cards']:
                return (0, 'Game Over')

            else:
                return (-1, 'Game Over')

        else:
            new_hand = self.hit('player hit',player)
            player = new_hand
            if player['cards'] > 21:
                return (-1, 'Game Over')

            else:
                return (player, 'Continue')

    def policy_valuation(self, state, rewards, implied_prob = False):
        (count,total_reward) = rewards[state]['count'], rewards[state]['reward']
        if count == 0:
            return 0
        else:
            if not implied_prob:
                return total_reward/count
            else:
                return(count+total_reward)/(2*count)

    def print_q_policy_val(self):
        for k,v in self.get_q_rewards().items():
            print(k, self.policy_valuation(k, self.get_q_rewards()))

    def play_new_game(self):
        curr_env = self.deal()
        player_hand = curr_env['player hand']
        natural = curr_env['natural']
        dealer_hidden_card = curr_env['dealer_hidden']
        dealer_show_card = curr_env['dealer_show']
        dealer_sum_card = dealer_hidden_card + dealer_show_card

        game_state = {'Player Hand': player_hand,
                      'Init Action': None,
                      'Dealer Hand': dealer_show_card,
                      'Outcome': None}

        if natural:
            game_state['Init Action'] = 'stand'
            if dealer_sum_card < 21:
                game_state['Outcome'] = 1.5
            else:
                game_state['Outcome'] = 0
            return game_state

        else:
            if dealer_sum_card == 21:
                game_state['Outcome'] = -1
                game_state['Init Action'] = 'stand'
                return game_state
            else:
                dealer_state = {'cards': dealer_sum_card,
                                'usable_ace': False}
            action = np.random.choice(self.get_action())
            game_state['Init Action'] = action
            game_status = 'Continue'

            outcome = self.play_hand(player_hand, action, dealer_state)

            if outcome[1] == 'Game Over':
                game_state['Outcome'] = outcome[0]
                return game_state
            else:
                player_hand = outcome[0]
                game_status = 'Continue'

                while game_status == 'Continue':
                    action = self.get_policy()[(player_hand['cards'], player_hand['usable_ace'],dealer_show_card)]
                    outcome = self.play_hand(player_hand, action, dealer_state)
                    if type(outcome[0]) == int:
                        game_status = outcome[1]
                        break
                    player_cards, usable_ace_state = outcome[0]['cards'], outcome[0]['usable_ace']
                    player_hand['cards'] = player_cards
                    player_hand['usable_ace'] = usable_ace_state

                    game_status = outcome[1]

            game_outcome = outcome[0]
            game_state['Outcome'] = game_outcome

        return game_state

    def init_policy(self):
        pol = {}
        for s in self.get_states():
            tmp = [v for k,v in s.items()]
            pol[tuple(tmp)] = np.random.choice(self.get_action())
        self.policy = pol

    def get_q_action(self, state):
        Q = self.get_q_values()
        return self.get_action()[np.argmax([Q[state, 'hit'], Q[state,'stand']])]

    def monte_carlo_q_sim(self, iter):
        t1 = time.time()
        for epoch in range(iter):
            if epoch % (iter//10) == 0 and epoch >0:
                t2 = time.time()
                print('{}% Complete in {:.3f}s'.format(int(100*epoch/iter),t2-t1))
            game_outcome = self.play_new_game()
            player = game_outcome['Player Hand']
            dealer = game_outcome['Dealer Hand']
            init_action = game_outcome['Init Action']
            outcome = game_outcome['Outcome']

            key, action = (player['cards'], player['usable_ace'], dealer) , init_action

            self.get_q_values()[key, action] = (self.get_q_rewards()[key, action]['reward'] +
            outcome)/(self.get_q_rewards()[key, action]['count'] + 1)

            self.get_q_rewards()[key, action]['reward'] += outcome
            self.get_q_rewards()[key, action]['count'] += 1

            self.get_policy()[key] = self.get_q_action(key)

        return None

    def optimal_policy(self, implied_prob = False):
        opt = {}
        for s in self.get_states():
            player = s['cards']
            ace = s['usable_ace']
            dealer = s['dealer_card']
            key = (player, ace, dealer)

            action_val = {}
            for a in self.get_action():
                top_action = None
                val = self.policy_valuation((key,a),self.get_q_rewards(), implied_prob)
                action_val[a] = val

            curr_max = -np.inf
            for k, v in action_val.items():
                if v > curr_max:
                    curr_max = v
                    top_action = k
                opt[key] = (top_action, curr_max)

        return opt

    def plot_optimal_policy(self, is_ace = False):

        opt = self.optimal_policy(implied_prob = True)

        fig = plt.figure(figsize=(12,12))
        ax = fig.add_subplot(111)
        data = np.empty((10,10)) * np.nan
        cax = ax.matshow(data, cmap = 'binary')
        is_ace = False
        for j in range(10):
            for i in range(10):
                state_val = opt[(i+12,is_ace,j+2)]
                action, prob = state_val[0], state_val[1]
                prob = '{:.2f}%'.format(100*prob)
                if action == 'hit':
                    c = 'firebrick'
                else:
                    c = 'forestgreen'
                ax.fill_between([i,i+1], j, j+1, facecolor=c)
                ax.text(i+0.5, j+0.5, prob, color='black', ha='center', va='center', fontsize = 12,
                    bbox=dict(boxstyle='round', facecolor='white', edgecolor='black'))

        extent = (0,10,10,0)
        ax.set_xticks(np.arange(0,10,1))
        ax.set_yticks(np.arange(0,10,1))
        ax.set_xticklabels(np.arange(12,22,1), fontsize = 12)
        ax.set_yticklabels(np.arange(2,12,1), fontsize = 12)
        ax.set_xlabel("Players Card", fontsize = 16)
        ax.xaxis.set_label_position('top')
        ax.set_ylabel("Dealer's Cards", fontsize = 16)
        ax.grid(which = 'major', color='k',lw=2)
        ax.imshow(data, extent=extent)
        plt.show()

    def init_game(self):
        self.init_states()
        self.init_rewards()
        self.init_policy()
        self.init_q_rewards()


text:
from collections import defaultdict
import warnings

import numpy as np
from sklearn.exceptions import FitFailedWarning

def complexity(estimator):
    return np.count_nonzero(estimator.coef_)

def net(estimator, x, y, attr="alpha", max_coarsity=2, filter=True, r_max=1e3, **kw):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", UserWarning)
        return _net(estimator, x, y, attr=attr, max_coarsity=max_coarsity, filter=filter, r_max=r_max, **kw)

def _net(estimator, x, y, attr="alpha", max_coarsity=2, filter=True, r_max=1e3, return_full=False, **kw):
    n_features = x.shape[1]

    memory = defaultdict(list)   # just a convenience list; this information is redundant
    models = defaultdict(list)

    def fit_in_memory(r):
        if not any(r in memory[k] for k in memory):
            est = estimator(**{**kw, **{attr: r}}).fit(x, y)
            c = complexity(est)
            memory[c].append(r)
            models[c].append(est)
            return c

    fit_in_memory(0)
    while True:
        try:
            c = fit_in_memory(r_max)
            if c == 0:
                r_max *= 0.8
            else:
                break
        except FitFailedWarning:
            r_max *= 0.8

    # greedy forward
    def greed_forward(c_lower, c_upper, coarsity):
        upper = min(memory[c_lower])
        lower = max(memory[c_upper])
        for r in np.linspace(lower, upper, 2**coarsity)[::-1]:
            fit_in_memory(r)

    # greedy search for transitions

    coarsity = 1
    all_expected = list(range(min(memory), max(memory) + 1))

    while True:
        it = ((d, e) for d, e in zip(sorted(memory.keys()), all_expected) if d != e)
        try:
            discovered, expected = next(it)
        except StopIteration:
            break
        n_keys = len(memory)
        greed_forward(max([k for k in memory if k < expected]), discovered, coarsity)
        if n_keys < len(memory):
            coarsity = 0
        else:
            coarsity += 1
            if coarsity >= max_coarsity:
                if discovered == max(all_expected):
                    break
                else:
                    all_expected.pop(all_expected.index(expected))
                    coarsity = 0

    if filter:
        models = {k: min(v, key=lambda x: getattr(x, attr)) for k, v in models.items()}

    if not return_full:
        return models
    else:
        return models, sum(len(v) for v in memory.values())

ext: py

max_stars_repo_name: Thomasillo/sparsereg

max_issues_repo_name: Thomasillo/sparsereg

max_forks_repo_name: Thomasillo/sparsereg

reason: import numpy


text:
import math
import numpy as np

def Adadelta(data):
    pass

def Adagrad(data):
    gradient_sums = np.zeros(theta.shape[0])
    for t in range(num_iterations):
        gradients = compute_gradients(data, weights)
        gradient_sums += gradients ** 2
        gradient_update = gradients / (np.sqrt(gradient_sums + epsilon))
        weights = weights - lr * gradient_update
    return weights

def Adam(data):
    pass

def LBFGS(data):
    pass

def RMSProp(data):
    pass

def SGD(data, batch_size, lr):
    N = len(data)
    np.random.shuffle(data)
    mini_batches = np.array([data[i:i+batch_size]
     for i in range(0, N, batch_size)])
    for X,y in mini_batches:
        backprop(X, y, lr)

def SGD_Momentum():
    pass


text:
import glob
import logging
import multiprocessing
import os
import shutil
import time

from matplotlib.pyplot import imshow
import bilby
import corner
import lalsimulation
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from PIL import Image
from bilby.core.prior import Cosine, Interped, Uniform

from gwpopulation.conversions import convert_to_beta_parameters
from gwpopulation.models.mass import SinglePeakSmoothedMassDistribution
from gwpopulation.models.redshift import PowerLawRedshift
from gwpopulation.models.spin import iid_spin_magnitude_beta, truncnorm
from matplotlib import rcParams
from numpy import cos, sin
from tqdm import tqdm

logging.getLogger("bilby").setLevel(logging.ERROR)
logging.getLogger().setLevel(logging.INFO)

REFERENCE_FREQ = 10 ** -3
NUM = 10000

def sample_from_agn_population(sigma_1, sigma_12, n=NUM):
    samples = get_samples(
        num_samples=n,
        population_params=dict(sigma_1=sigma_1, sigma_12=sigma_12),
    )
    return samples[[k for k in PARAMS.keys()]]

def overlaid_corner(
    samples_list,
    sample_labels,
    samples_colors,
    params,
    fname,
    title,
    show_legend=True,
):
    """Plots multiple corners on top of each other"""
    # print(f"plotting {fname}")
    # sort the sample columns
    samples_list = [s[params] for s in samples_list]

    # get some constants
    n = len(samples_list)
    _, ndim = samples_list[0].shape
    min_len = min([len(s) for s in samples_list])

    CORNER_KWARGS.update(
        range=[PARAMS[p]["r"] for p in params],
        labels=[PARAMS[p]["l"] for p in params],
    )

    fig = corner.corner(
        samples_list[0],
        color=samples_colors[0],
        **CORNER_KWARGS,
    )

    for idx in range(1, n):
        fig = corner.corner(
            samples_list[idx],
            fig=fig,
            weights=get_normalisation_weight(len(samples_list[idx]), min_len),
            color=samples_colors[idx],
            **CORNER_KWARGS,
        )
    if show_legend:
        plt.legend(
            handles=[
                mlines.Line2D(
                    [], [], color=samples_colors[i], label=sample_labels[i]
                )
                for i in range(n)
            ],
            fontsize=20,
            frameon=False,
            bbox_to_anchor=(1, ndim),
            loc="upper right",
        )
    plt.suptitle(title)
    fig.savefig(fname)
    plt.close(fig)

def get_normalisation_weight(len_current_samples, len_of_longest_samples):
    return np.ones(len_current_samples) * (
        len_of_longest_samples / len_current_samples
    )

def plot_overlaid_corners(
    sigma_1_list,
    sigma_12_list,
    pltdir,
    n=NUM,
    show_aligned=False,
    show_isotropic=False,
    show_agn=True,
):
    isotropic_samples = get_samples_from_prior(
        num_samples=n, prior_fname="isotropic.prior"
    )
    aligned_samples = get_samples_from_prior(
        num_samples=n, prior_fname="aligned.prior"
    )

    if os.path.isdir(pltdir):
        assert pltdir != "."
        shutil.rmtree(pltdir)
    os.makedirs(pltdir, exist_ok=False)

    i = 0
    for i_sig1, i_sig12 in tqdm(
        zip(sigma_1_list, sigma_12_list),
        total=len(sigma_1_list),
        desc="Hyper-Param settings",
    ):
        f_theta = (
            f"{pltdir}/{i:02}_sig12-{i_sig12:.1f}_sig1-{i_sig1:.1f}_theta.png"
        )
        f_chi = (
            f"{pltdir}/{i:02}_sig12-{i_sig12:.1f}_sig1-{i_sig1:.1f}_chi.png"
        )
        f_combo = (
            f"{pltdir}/{i:02}_sig12-{i_sig12:.1f}_sig1-{i_sig1:.1f}_combo.png"
        )
        agn_label = (
            r"$\sigma_{1}="
            + f"{i_sig1:.1f}"
            + r", \sigma_{12}="
            + f"{i_sig12:.1f}$"
        )

        samples_list, samples_colors, samples_labels = [], [], []
        if show_aligned:
            samples_list.append(aligned_samples)
            samples_colors.append("tab:green")
            samples_labels.append("Field")
        if show_isotropic:
            samples_list.append(isotropic_samples)
            samples_colors.append("tab:orange")
            samples_labels.append("Dynamic")
        if show_agn:
            agn_samples = sample_from_agn_population(
                sigma_1=i_sig1, sigma_12=i_sig12, n=n
            )
            samples_list.append(agn_samples)
            samples_colors.append("tab:blue")
            samples_labels.append("AGN " + agn_label)

        overlaid_corner(
            samples_list,
            samples_labels,
            samples_colors,
            params=["chi_eff", "chi_p"],
            fname=f_chi,
            title="",
            show_legend=False,
        )
        overlaid_corner(
            samples_list,
            samples_labels,
            samples_colors,
            params=["cos_tilt_1", "cos_tilt_2", "cos_theta_12"],
            fname=f_theta,
            title="",
        )

        images = [[Image.open(f_chi), Image.open(f_theta)]]

        combined_image = join_images(
            *images, bg_color="white", alignment=(0.5, 0.5)
        )
        combined_image.save(f_combo)
        i += 1

ISO_SAMPLES = get_samples_from_prior(
    num_samples=1000, prior_fname="isotropic.prior"
)
ALIGNED_SAMPLES = get_samples_from_prior(
    num_samples=1000, prior_fname="aligned.prior"
)

def plot_one_corner(
    show_aligned, show_isotropic, show_agn, n=NUM, sig1=0, sig12=0
):
    plt.close("all")
    f_theta = f"tmp_theta.png"
    f_chi = f"tmp_chi.png"
    agn_label = (
        r"$\sigma_{1}=" + f"{sig1:.1f}" + r", \sigma_{12}=" + f"{sig12:.1f}$"
    )
    samples_list, samples_colors, samples_labels = [], [], []
    if show_aligned:
        samples_list.append(ALIGNED_SAMPLES)
        samples_colors.append("tab:green")
        samples_labels.append("Field")
    if show_isotropic:
        samples_list.append(ISO_SAMPLES)
        samples_colors.append("tab:orange")
        samples_labels.append("Dynamic")
    if show_agn:
        agn_samples = sample_from_agn_population(
            sigma_1=sig1, sigma_12=sig12, n=n
        )
        samples_list.append(agn_samples)
        samples_colors.append("tab:blue")
        samples_labels.append("AGN " + agn_label)

    overlaid_corner(
        samples_list,
        samples_labels,
        samples_colors,
        params=["chi_eff", "chi_p"],
        fname=f_chi,
        title="",
        show_legend=False,
    )
    overlaid_corner(
        samples_list,
        samples_labels,
        samples_colors,
        params=["cos_tilt_1", "cos_tilt_2", "cos_theta_12"],
        fname=f_theta,
        title="",
    )

    images = [[Image.open(f_chi), Image.open(f_theta)]]

    combined_image = join_images(
        *images, bg_color="white", alignment=(0.5, 0.5)
    )
    plt.axis("off")
    fig, ax = plt.subplots(figsize=(18, 12))
    ax.grid(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.imshow(np.asarray(combined_image), aspect="equal")

def gif_main():
    sigma12_vals = list(np.arange(0.3, 3, 0.3))
    sigma1_vals = [1 for _ in range(len(sigma12_vals))]

    outdir = "vary_sigma12"
    plot_overlaid_corners(
        sigma_1_list=sigma1_vals, sigma_12_list=sigma12_vals, pltdir=outdir
    )
    # save_gif("vary_12.gif", outdir=outdir, loop=True, regex="*_combo.png")

    sigma1_vals = list(np.arange(0.3, 3, 0.3))
    sigma12_vals = [0.1 for _ in range(len(sigma1_vals))]

    outdir = "vary_sigma1"
    plot_overlaid_corners(
        sigma_1_list=sigma1_vals, sigma_12_list=sigma12_vals, pltdir=outdir
    )
    # save_gif("vary_1.gif", outdir=outdir, loop=True, regex="*_combo.png")

def test_main():
    # plot_one_corner(sigma_1=0.01, sigma_12=0.01)
    gif_main()

if __name__ == "__main__":
    test_main()

reason: import numpy,from numpy


text:
import numpy as np
import os
import random
from sys import platform as sys_pf
import matplotlib
if sys_pf == 'darwin':
	matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
# ---
# Demo for how to load image and stroke data for a character
# ---

# Plot the motor trajectory over an image
#
# Input
#  I [105 x 105 nump] grayscale image
#  drawings: [ns list] of strokes (numpy arrays) in motor space
#  lw : line width
def plot_motor_to_image(I,drawing,lw=2):
	drawing = [d[:,0:2] for d in drawing] # strip off the timing data (third column)
	drawing = [space_motor_to_img(d) for d in drawing] # convert to image space
	plt.imshow(I,cmap='gray')
	ns = len(drawing)
	for sid in range(ns): # for each stroke
		plot_traj(drawing[sid],get_color(sid),lw)
	plt.xticks([])
	plt.yticks([])

# Plot individual stroke
#
# Input
#  stk: [n x 2] individual stroke
#  color: stroke color
#  lw: line width
def plot_traj(stk,color,lw):
	n = stk.shape[0]
	if n > 1:
		plt.plot(stk[:,0],stk[:,1],color=color,linewidth=lw)
	else:
		plt.plot(stk[0,0],stk[0,1],color=color,linewidth=lw,marker='.')

# Color map for the stroke of index k
def get_color(k):
    scol = ['r','g','b','m','c']
    ncol = len(scol)
    if k < ncol:
       out = scol[k]
    else:
       out = scol[-1]
    return out

# convert to str and add leading zero to single digit numbers
def num2str(idx):
	if idx < 10:
		return '0'+str(idx)
	return str(idx)

# Load binary image for a character
#
# fn : filename
def load_img(fn):
	I = plt.imread(fn)
	I = np.array(I,dtype=bool)
	return I

# Load stroke data for a character from text file
#
# Input
#   fn : filename
#
# Output
#   motor : list of strokes (each is a [n x 3] numpy array)
#      first two columns are coordinates
#	   the last column is the timing data (in milliseconds)
def load_motor(fn):
	motor = []
	with open(fn,'r') as fid:
		lines = fid.readlines()
	lines = [l.strip() for l in lines]
	for myline in lines:
		if myline =='START': # beginning of character
			stk = []
		elif myline =='BREAK': # break between strokes
			stk = np.array(stk)
			motor.append(stk) # add to list of strokes
			stk = []
		else:
			arr = np.fromstring(myline,dtype=float,sep=',')
			stk.append(arr)
	return motor

#
# Map from motor space to image space (or vice versa)
#
# Input
#   pt: [n x 2] points (rows) in motor coordinates
#
# Output
#  new_pt: [n x 2] points (rows) in image coordinates
def space_motor_to_img(pt):
	pt[:,1] = -pt[:,1]
	return pt
def space_img_to_motor(pt):
	pt[:,1] = -pt[:,1]
	return

if __name__ == "__main__":
	img_dir = 'images_background'
	stroke_dir = 'strokes_background'
	nreps = 20 # number of renditions for each character
	nalpha = 5 # number of alphabets to show

	alphabet_names = [a for a in os.listdir(img_dir) if a[0] != '.'] # get folder names
	alphabet_names = random.sample(alphabet_names,nalpha) # choose random alphabets

	for a in range(nalpha): # for each alphabet
		print('generating figure ' + str(a+1) + ' of ' + str(nalpha))
		alpha_name = alphabet_names[a]

		# choose a random character from the alphabet
		character_id = random.randint(1,len(os.listdir(os.path.join(img_dir,alpha_name))))

		# get image and stroke directories for this character
		img_char_dir = os.path.join(img_dir,alpha_name,'character'+num2str(character_id))
		stroke_char_dir = os.path.join(stroke_dir,alpha_name,'character'+num2str(character_id))

		# get base file name for this character
		fn_example = os.listdir(img_char_dir)[0]
		fn_base = fn_example[:fn_example.find('_')]

		plt.figure(a,figsize=(10,8))
		plt.clf()
		for r in range(1,nreps+1): # for each rendition
			plt.subplot(4,5,r)
			fn_stk = stroke_char_dir + '/' + fn_base + '_' + num2str(r) + '.txt'
			fn_img = img_char_dir + '/' + fn_base + '_' + num2str(r) + '.png'
			motor = load_motor(fn_stk)
			I = load_img(fn_img)
			plot_motor_to_image(I,motor)
			if r==1:
				plt.title(alpha_name[:15] + '\n character ' + str(character_id))
		plt.tight_layout()
	plt.show()

max_stars_repo_name: NeonYouth/omniglot

max_issues_repo_name: NeonYouth/omniglot

max_forks_repo_name: NeonYouth/omniglot


text:
import numpy as np

def create_index_dict(board):
    """
    Creates a dictionary from values to indices
    """
    index_dict = {}
    for i, row in enumerate(board):
        for j, elem in enumerate(row):
            if elem not in index_dict:
                index_dict[elem] = (i, j)
    return index_dict

def check_win(row_dict, col_dict):
    """
    Check if a board has won
    """
    row_win = any(x == 5 for x in row_dict.values())
    col_win = any(v == 5 for v in col_dict.values())
    return row_win or col_win

def play_board(board, moves):
    """
    Play a bingo board
    """
    marks = []
    row_dict = {}
    col_dict = {}

    index_dict = create_index_dict(board)
    for move in moves:
        if move in index_dict:
            row, col = index_dict[move]
            marks.append((row, col))
            if row not in row_dict:
                row_dict[row] = 1
            else:
                row_dict[row] += 1
            if col not in col_dict:
                col_dict[col] = 1
            else:
                col_dict[col] += 1
            if check_win(row_dict, col_dict):
                return True, marks, move
    return False, marks, None

def parse_board(board):
    """
    Parse a bingo board into a list of lists
    """
    return np.array([[int(x) for x in line.split()] for line in board.split("\n")])

def play_boards(boards, moves):
    """
    Play all boards in a list
    """
    winners = []
    for board in boards:
        won, marks, move = play_board(board, moves)
        if won:
            winners.append((marks, move))
    return winners

def find_first_winner(boards, moves):
    """
    Find the first winner in a list of boards
    """
    winners = play_boards(boards, moves)
    minarg = np.argmin([len(m) for m, _ in winners])
    return minarg, winners[minarg]

def find_last_winner(boards, moves):
    """
    Find the first winner in a list of boards
    """
    winners = play_boards(boards, moves)
    winners = winners[::-1]
    lengths = [len(m) for m, _ in winners]
    maxarg = np.argmax(lengths)
    return maxarg, winners[maxarg]

def calc_score(board, marks, last_move):
    """
    Calculate the score of a list of boards
    """
    score = 0
    for row in range(board.shape[0]):
        for col in range(board.shape[1]):
            if (row, col) not in marks:
                score += board[row, col]
    score *= last_move
    return score

if __name__ == "__main__":
    with open("input.txt", "r") as f:
        contents = f.read().split("\n\n")
        moves = [int(x) for x in contents[0].split(",")]
        boards = [parse_board(x) for x in contents[1:]]
        index, (marks, last_move) = find_first_winner(boards, moves)
        score = calc_score(boards[index], marks, last_move)
        print(f"Score of first winning board is {score}")

        index, (marks, last_move) = find_last_winner(boards, moves)
        score = calc_score(boards[index], marks, last_move)
        print(f"Score of last winning board is {score}")

max_stars_repo_name: nhrade/advent-of-code

max_issues_repo_name: nhrade/advent-of-code

max_forks_repo_name: nhrade/advent-of-code


text:
import unittest
from pyalink.alink import *
import numpy as np
import pandas as pd
class TestHashCrossFeatureBatchOp(unittest.TestCase):
    def test_hashcrossfeaturebatchop(self):

        df = pd.DataFrame([
            ["1.0", "1.0", 1.0, 1],
            ["1.0", "1.0", 0.0, 1],
            ["1.0", "0.0", 1.0, 1],
            ["1.0", "0.0", 1.0, 1],
            ["2.0", "3.0", None, 0],
            ["2.0", "3.0", 1.0, 0],
            ["0.0", "1.0", 2.0, 0],
            ["0.0", "1.0", 1.0, 0]])
        data = BatchOperator.fromDataframe(df, schemaStr="f0 string, f1 string, f2 double, label bigint")
        cross = HashCrossFeatureBatchOp().setSelectedCols(['f0', 'f1', 'f2']).setOutputCol('cross').setNumFeatures(4)
        print(cross.linkFrom(data))
        pass


text:
import unittest

import numpy as np
from numpy.linalg import qr as qr_numpy
from parameterized import parameterized
from pycompss.api.api import compss_wait_on

from dislib.data.array import random_array
from dislib.decomposition import qr
from dislib.decomposition.qr.base import (
    _dot_task,
    _qr_task,
    _transpose_block,
    _validate_ds_array,
    ZEROS,
    IDENTITY
)

class QRTest(unittest.TestCase):

    @parameterized.expand([
        (1, 1, 2), (1, 1, 4), (2, 2, 2), (2, 2, 4),
        (3, 3, 3), (3, 3, 4), (4, 4, 2), (4, 4, 3),
        (4, 4, 4), (6, 6, 6), (8, 8, 8), (2, 1, 2),
        (2, 1, 4), (3, 2, 2), (3, 2, 4), (4, 3, 3),
        (4, 3, 4), (5, 4, 2), (10, 6, 6), (10, 6, 6),
    ])
    def test_qr(self, m_size, n_size, b_size):
        """Tests qr_blocked full mode"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        shape = (m_size * b_size, n_size * b_size)
        m2b_ds = random_array(shape, (b_size, b_size))

        (q, r) = qr(m2b_ds)

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.dot(q.T), np.identity(m_size * b_size)))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))

    @parameterized.expand([
        ((7, 6), (3, 3)), ((7, 5), (2, 2)), ((10, 4), (3, 3)),
        ((4, 4), (3, 3)), ((6, 4), (3, 3)), ((6, 5), (2, 2)),
    ])
    def test_qr_with_padding(self, m_shape, b_shape):
        """Tests qr_blocked with padding"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        m2b_ds = random_array(m_shape, b_shape)

        (q, r) = qr(m2b_ds, mode='full')

        q = q.collect()
        r = r.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.dot(q.T), np.identity(m_shape[0])))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b_ds.collect()))

        (q, r) = qr(m2b_ds, mode="economic")

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.T.dot(q), np.identity(m_shape[1])))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
        # check the dimensions of Q
        self.assertTrue(q.shape == (m_shape[0], m_shape[1]))
        # check the dimensions of R
        self.assertTrue(r.shape == (m_shape[1], m_shape[1]))

    @parameterized.expand([
        (1, 1, 2), (1, 1, 4), (2, 2, 2), (2, 2, 4),
        (3, 3, 3), (3, 3, 4), (4, 4, 2), (4, 4, 3),
        (4, 4, 4), (6, 6, 6), (8, 8, 8), (2, 1, 2),
        (2, 1, 4), (3, 2, 2), (3, 2, 4), (4, 3, 3),
        (4, 3, 4), (5, 4, 2), (10, 6, 6), (10, 6, 6),
    ])
    def test_qr_economic(self, m_size, n_size, b_size):
        """Tests qr_blocked economic mode"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        shape = (m_size * b_size, n_size * b_size)
        m2b_ds = random_array(shape, (b_size, b_size))

        (q, r) = qr(m2b_ds, mode="economic")

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.T.dot(q), np.identity(n_size * b_size)))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
        # check the dimensions of Q
        self.assertTrue(q.shape == (m_size * b_size, n_size * b_size))
        # check the dimensions of R
        self.assertTrue(r.shape == (n_size * b_size, n_size * b_size))

    @parameterized.expand([
        (1, 1, 2), (1, 1, 4), (2, 2, 2), (2, 2, 4),
        (3, 3, 3), (3, 3, 4), (4, 4, 2), (4, 4, 3),
        (4, 4, 4), (6, 6, 6), (8, 8, 8), (2, 1, 2),
        (2, 1, 4), (3, 2, 2), (3, 2, 4), (4, 3, 3),
        (4, 3, 4), (5, 4, 2), (10, 6, 6), (10, 6, 6),
    ])
    def test_qr_r(self, m_size, n_size, b_size):
        """Tests qr_blocked r mode"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        shape = (m_size * b_size, n_size * b_size)
        m2b_ds = random_array(shape, (b_size, b_size))

        r = qr(m2b_ds, mode="r")
        _, r_full = qr(m2b_ds, mode="full")

        r = r.collect()
        r_full = r_full.collect()

        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if R matrix is the same as when the full mode is applied
        self.assertTrue(np.allclose(r, r_full))

    def test_special_cases(self):
        """Tests special cases not covered in other tests"""
        np.set_printoptions(precision=2)
        np.random.seed(8)

        # testing a block transpose
        a = None
        a_type = [[ZEROS]]
        a_transposed_type, a = _transpose_block(a, a_type)
        self.assertEqual(a_type, a_transposed_type)
        self.assertIsNone(a)

        a = None
        a_type = [[IDENTITY]]
        a_transposed_type, a = _transpose_block(a, a_type)
        self.assertEqual(a_type, a_transposed_type)
        self.assertIsNone(a)

        # testing array validation
        a = random_array((10, 10), (2, 5))
        with self.assertRaises(ValueError):
            _validate_ds_array(a)

        # testing transpose dot
        a = np.random.rand(3, 2)
        b = np.random.rand(2, 5)
        a_dot_b_transposed = compss_wait_on(_dot_task(a, b,
                                                      transpose_result=True))
        self.assertTrue(np.allclose(np.transpose(np.dot(a, b)),
                                    a_dot_b_transposed))

        # testing qr of zeros or identity
        b_size = (5, 5)
        a = None
        q, r = _qr_task(a, np.full((1, 1), ZEROS), b_size)
        q = compss_wait_on(q)
        r = compss_wait_on(r)
        q_numpy, r_numpy = qr_numpy(np.zeros(b_size), mode='reduced')
        self.assertTrue(np.allclose(q_numpy, q))
        self.assertTrue(np.allclose(r_numpy, r))
        q, r = _qr_task(a, np.full((1, 1), IDENTITY), b_size)
        q = compss_wait_on(q)
        r = compss_wait_on(r)
        q_numpy, r_numpy = qr_numpy(np.identity(b_size[0]), mode='reduced')
        self.assertTrue(np.allclose(q_numpy, q))
        self.assertTrue(np.allclose(r_numpy, r))

    def test_economic_overwrite(self):
        """Tests economic qr with overwrite = True"""
        np.set_printoptions(precision=2)
        np.random.seed(8)
        m_size = 25
        n_size = 10
        b_size = 5
        m2b_ds = random_array((m_size, n_size), (b_size, b_size))
        (q, r) = qr(m2b_ds, mode="economic", overwrite_a=True)

        q = q.collect()
        r = r.collect()
        m2b = m2b_ds.collect()

        # check if arrays original and r arrays are different
        # as it is not possible to overwrite the original matrix in the
        # economic mode
        self.assertFalse(np.array_equal(m2b, r))

        # check if Q matrix is orthogonal
        self.assertTrue(np.allclose(q.T.dot(q), np.identity(n_size)))
        # check if R matrix is upper triangular
        self.assertTrue(np.allclose(np.triu(r), r))
        # check if the product Q * R is the original matrix
        self.assertTrue(np.allclose(q.dot(r), m2b))
        # check the dimensions of Q
        self.assertTrue(q.shape == (m_size, n_size))
        # check the dimensions of R
        self.assertTrue(r.shape == (n_size, n_size))

    def test_exceptions(self):
        m2b_ds = random_array((100, 100), (10, 10))
        with self.assertRaises(ValueError):
            # unsupported mode
            qr(m2b_ds, mode="x")

        m2b_ds = random_array((50, 100), (10, 10))
        with self.assertRaises(ValueError):
            # m > n is required for matrices m x n
            qr(m2b_ds)

        m2b_ds = random_array((100, 100), (10, 5))
        with self.assertRaises(ValueError):
            # Square blocks are required
            qr(m2b_ds)

        m2b_ds = random_array((100, 100), (10, 10))
        m2b_ds = m2b_ds[:, 0:5]
        with self.assertRaises(ValueError):
            qr(m2b_ds)

def main():
    unittest.main()

if __name__ == '__main__':
    main()

max_stars_repo_name: alexbarcelo/dislib

max_issues_repo_name: alexbarcelo/dislib

max_forks_repo_name: alexbarcelo/dislib


text:
# -*- coding: utf-8 -*-
"""
Defines unit tests for :mod:`colour_datasets.loaders.asano2015` module.
"""

import numpy as np
import unittest

from colour import SpectralShape

from colour_datasets.loaders import DatasetLoader_Asano2015, build_Asano2015

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2019-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-developers@colour-science.org'
__status__ = 'Production'

__all__ = ['TestDatasetLoader_Asano2015', 'TestBuildAsano2015']

class TestDatasetLoader_Asano2015(unittest.TestCase):
    """
    Defines :class:`colour_datasets.loaders.asano2015.DatasetLoader_Asano2015`
    class unit tests methods.
    """

    def test_required_attributes(self):
        """
        Tests presence of required attributes.
        """

        required_attributes = ('ID', )

        for attribute in required_attributes:
            self.assertIn(attribute, dir(DatasetLoader_Asano2015))

    def test_required_methods(self):
        """
        Tests presence of required methods.
        """

        required_methods = ('__init__', 'load', 'parse_workbook_Asano2015')

        for method in required_methods:
            self.assertIn(method, dir(DatasetLoader_Asano2015))

    def test_load(self):
        """
        Tests :func:`colour_datasets.loaders.asano2015.\
DatasetLoader_Asano2015.load` method.
        """

        dataset = DatasetLoader_Asano2015()
        self.assertEqual(
            sorted(dataset.load().keys()),
            ['Categorical Observers', 'Colour Normal Observers'])

        self.assertEqual(
            dataset.content['Categorical Observers'][1].XYZ_2.shape,
            SpectralShape(390, 780, 5))

        np.testing.assert_almost_equal(
            dataset.content['Categorical Observers'][1].XYZ_2[390],
            np.array([
                0.003774670254076,
                0.000033807427536,
                0.017705556255144,
            ]),
            decimal=7)

        np.testing.assert_almost_equal(
            dataset.content['Categorical Observers'][10].LMS_10[780],
            np.array([
                0.000101460310461,
                9.67131698024335e-06,
                0.000000000000000,
            ]),
            decimal=7)

        self.assertAlmostEqual(
            dataset.content['Categorical Observers'][5].parameters[
                'Shift in S [nm]'],
            0.233255808,
            places=7)

        self.assertEqual(
            dataset.content['Colour Normal Observers'][1].XYZ_2.shape,
            SpectralShape(390, 780, 5))

        np.testing.assert_almost_equal(
            dataset.content['Colour Normal Observers'][1].XYZ_2[390],
            np.array([
                0.001627436785620,
                0.000021871064674,
                0.007492391403616,
            ]),
            decimal=7)

        np.testing.assert_almost_equal(
            dataset.content['Colour Normal Observers'][10].LMS_10[780],
            np.array([
                0.000092440377130,
                6.93870146211108e-06,
                0.000000000000000,
            ]),
            decimal=7)

        self.assertAlmostEqual(
            dataset.content['Colour Normal Observers'][5].parameters[
                'Shift in S [nm]'],
            0.000649602695013,
            places=7)

        self.assertEqual(
            dataset.content['Colour Normal Observers'][151].others['Location'],
            'Darmstadt')

class TestBuildAsano2015(unittest.TestCase):
    """
    Defines :func:`colour_datasets.loaders.asano2015.build_Asano2015`
    definition unit tests methods.
    """

    def test_build_Asano2015(self):
        """
        Tests :func:`colour_datasets.loaders.asano2015.build_Asano2015`
        definition.
        """

        self.assertIs(build_Asano2015(), build_Asano2015())

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

max_stars_repo_name: JGoldstone/colour-datasets

max_issues_repo_name: JGoldstone/colour-datasets

max_forks_repo_name: JGoldstone/colour-datasets


text:
from __future__ import division, print_function, absolute_import
import sys
#sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import os
import datetime
from timeit import time
import warnings
import cv2
import numpy as np
import argparse
from PIL import Image
from yolo import YOLO

from deep_sort import preprocessing
from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
from deep_sort.detection import Detection as ddet
from collections import deque
from keras import backend
import tensorflow as tf
from tensorflow import InteractiveSession
from keras.models import model_from_json
from PyQt5 import QtGui

from utils.distance import moving_distance
from utils.curvature import path_curvature
from utils.fuzzy import fuzzy_system, motility_system

from keras.applications.mobilenet import preprocess_input
from utils.images_resize import resize_keep_aspectratio

# visem_01_14
# visem_02_14
# visem_03_23
# visem_04_38
# visem_05_47
# visem_06_33
# visem_07_39

video_name = r'visem_05_47.mp4'
output_name = video_name
video_dir = r'./video/sperm'
output_dir = r'./video/results'

video_path = os.path.join(video_dir, video_name)
output_path = os.path.join(output_dir, output_name)

video_capture = cv2.VideoCapture(video_path)

def main(yolo,
         video_capture=video_capture,
         output_path=output_path,
         yolo_box=False,
         label=False,
         deepsort_box=True,
         center_point=True,
         item_number_old=False,
         item_number_new=False,
         trajectory=True,
         show_moving_dist=False,
         show_path_curv=False,
         point_visualization=False,
         fuzzy=True,
         mobilenetv2=True,
         label_show_image=None,
         lcdNumber_1=None,
         lcdNumber_2=None,
         lcdNumber_3=None,
         information=False):
    '''
    yolo_box: 劃出yolo辨識框
    label: 劃出物件名稱
    deepsort_box: 劃出deepsort預測框
    item_number_old: 劃出舊的ID
    item_number_new: 劃出新的ID
    trajectory: 劃出軌跡
    show_moving_dist: 在每隻精子旁顯示移動距離
    show_path_curv: 在每支精子旁顯示路徑軌跡
    '''

    g1 = tf.Graph()  # 加载到Session 1的graph
    g2 = tf.Graph()  # 加载到Session 2的graph

    sess1 = tf.Session(graph=g1)  # Session1
    sess2 = tf.Session(graph=g2)  # Session2

    with sess1.as_default():
        with g1.as_default():
            # load the model
            # load json and create model
            json_file = open(
                r'./model_weights/model-mobilenetv2-cbam-hushem-gray-ratio4-alpha1e-4-dropout0.5-ep100-lr1e-3.json',
                'r')
            loaded_model_json = json_file.read()
            json_file.close()
            model_mobilenet = model_from_json(loaded_model_json)
            # load weights into new model
            model_mobilenet.load_weights(
                r'./model_weights/mobilenetv2-ep0100-acc1.0000-loss0.0017-val_acc0.8931-val_loss0.4444.h5'
            )

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)

    # 設定軌跡長度
    maxlen = 61
    pts = [deque(maxlen=maxlen) for _ in range(9999)]
    warnings.filterwarnings('ignore')

    # initialize a list of colors to represent each possible class label
    np.random.seed(2021)
    COLORS = np.random.randint(0, 255, size=(200, 3), dtype='uint8')
    #list = [[] for _ in range(100)]

    start = time.time()
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0

    counter = []
    #deep_sort
    model_filename = 'model_data/market1501.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)

    find_objects = ['person']
    metric = nn_matching.NearestNeighborDistanceMetric('cosine',
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)

    writeVideo_flag = True

    if writeVideo_flag:
        # Define the codec and create VideoWriter object
        w = int(video_capture.get(3))
        h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        out = cv2.VideoWriter(output_path, fourcc, 30, (w, h))
        list_file = open(r'./detection_information/detection_rslt.txt', 'w')
        frame_index = -1

    fps = 0.0
    real_id = 0
    data = {}
    data_id = {}

    data_dist = []
    data_curv = []
    fps_list = []

    # 定義歸屬函數與模糊規則庫
    motility_base, motility_ = fuzzy_system()

    while True:

        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break
        try:
            org_img = frame.copy()
        except AttributeError:
            break
        t1 = time.time()

        #image = Image.fromarray(frame)
        image = Image.fromarray(frame[..., ::-1])  #bgr to rgb
        boxs, confidence, class_names = yolo.detect_image(image)
        features = encoder(frame, boxs)
        # score to 1.0 here).
        detections = [
            Detection(bbox, 1.0, feature)
            for bbox, feature in zip(boxs, features)
        ]
        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)

        i = int(0)
        indexIDs = []
        c = []
        boxes = []

        for det in detections:
            bbox = det.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            if yolo_box:
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (0, 0, 0), 2)
            # print(class_names)

        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            #boxes.append([track[0], track[1], track[2], track[3]])
            indexIDs.append(int(track.track_id))
            counter.append(int(track.track_id))
            bbox = track.to_tlbr()
            color = [int(c) for c in COLORS[indexIDs[i] % len(COLORS)]]
            # 第幾幀
            # print(frame_index)

            # 將辨識資訊寫入記事本裡
            list_file.write(str(frame_index) + ',')
            list_file.write(str(track.track_id) + ',')
            b0 = str(bbox[0]
                     )  #.split('.')[0] + '.' + str(bbox[0]).split('.')[0][:1]
            b1 = str(bbox[1]
                     )  #.split('.')[0] + '.' + str(bbox[1]).split('.')[0][:1]
            b2 = str(bbox[2] - bbox[0]
                     )  #.split('.')[0] + '.' + str(bbox[3]).split('.')[0][:1]
            b3 = str(bbox[3] - bbox[1])
            list_file.write(
                str(b0) + ',' + str(b1) + ',' + str(b2) + ',' + str(b3))
            # 物件編號
            # print(str(track.track_id))
            # 換行
            list_file.write('\n')
            #list_file.write(str(track.track_id)+',')

            # 劃出deepsort預測框
            if deepsort_box:
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (color), 2)

            # 舊的物件編號
            if item_number_old:
                cv2.putText(frame, str(track.track_id),
                            (int(bbox[0]), int(bbox[1] - 50)), 0, 5e-3 * 150,
                            (color), 2)
            # 新的物件編號（不會跳號）
            elif item_number_new:
                try:
                    cv2.putText(frame, str(data_id[track.track_id]),
                                (int(bbox[0]), int(bbox[1] - 8)), 0,
                                5e-3 * 125, (color), 2)
                except KeyError:
                    real_id += 1
                    data_id.setdefault(track.track_id, real_id)
                    cv2.putText(frame, str(data_id[track.track_id]),
                                (int(bbox[0]), int(bbox[1] - 8)), 0,
                                5e-3 * 125, (color), 2)

            if len(class_names) > 0:
                class_name = class_names[0]
                # 畫出物件名稱
                if label:
                    cv2.putText(frame, str(class_names[0]),
                                (int(bbox[0]), int(bbox[1] - 8)), 0,
                                5e-3 * 125, (0, 0, 0), 2)

            i += 1
            #bbox_center_point(x,y)
            center = (int(
                ((bbox[0]) + (bbox[2])) / 2), int(((bbox[1]) + (bbox[3])) / 2))
            #track_id[center]

            # 將每一幀之中心位置儲存起來
            pts[track.track_id].append(center)

            # 框中心點之大小
            thickness = 5
            # 劃出框中心點
            if center_point:
                cv2.circle(frame, (center), 1, color, thickness)

            # 劃出移動路徑
            if trajectory:
                for j in range(1, len(pts[track.track_id])):
                    if pts[track.track_id][j - 1] is None or pts[
                            track.track_id][j] is None:
                        continue
                    # 移動路徑由細至粗
                    # thickness = int(np.sqrt(64 / float(j + 1)) * 2)
                    # 固定路徑粗細
                    thickness = 2
                    cv2.line(frame, (pts[track.track_id][j - 1]),
                             (pts[track.track_id][j]), (color), thickness)

            # 計算移動距離與轉向角
            # pts[track.track_id][(len(pts[track.track_id])-1)] 為當前偵之座標
            if len(pts[track.track_id]) == maxlen:
                # 計算移動距離
                frame, moving_dist = moving_distance(frame, pts, track)
                # 計算路徑曲率
                frame, path_curv = path_curvature(
                    frame,
                    pts,
                    track,
                    maxlen,
                    point_visualization=point_visualization)
                # 將距離四捨五入至小數點第二位
                moving_dist = round(moving_dist, 2)
                data_dist.append(moving_dist)
                # 將曲率四捨五入至小數點第二位
                path_curv = round(path_curv, 2)
                data_curv.append(path_curv)

                # 將計算資訊顯示至精子旁
                if show_moving_dist:
                    cv2.putText(frame, '{:.2f}'.format(moving_dist),
                                (int(center[0] + 20), int(center[1])), 0,
                                5e-3 * 100, (0, 0, 139), 2)  # Red4
                if show_path_curv:
                    cv2.putText(frame, '{:.2f}'.format(path_curv),
                                (int(center[0] + 20), int(center[1] + 20)), 0,
                                5e-3 * 100, (139, 0, 0), 2)  # Blue4

                # 模糊推論系統
                if fuzzy:
                    # fuzzy_system
                    score = motility_system(
                        motility_base=motility_base,
                        motility=motility_,
                        moving_distance=moving_dist,
                        path_curvature=path_curv,
                    )
                    score = round(score, 2)

                    try:
                        # 將分數顯示至精子旁
                        cv2.putText(frame, '{:.2f}'.format(score),
                                    (int(center[0] + 20), int(center[1] + 5)),
                                    0, 5e-3 * 100, (0, 0, 0),
                                    2)  # purple4 (139, 26, 85)
                        # 劃出框
                        # if data[track.track_id][3][0] >= 0 and data[
                        #         track.track_id][3][0] <= 20:
                        #     cv2.rectangle(frame,
                        #                   (int(bbox[0] - 5), int(bbox[1]) - 5),
                        #                   (int(bbox[2] + 5), int(bbox[3] + 5)),
                        #                   [255, 0, 0], 2)
                        # elif data[track.track_id][3][0] <= 40:
                        #     cv2.rectangle(frame,
                        #                   (int(bbox[0] - 5), int(bbox[1]) - 5),
                        #                   (int(bbox[2] + 5), int(bbox[3] + 5)),
                        #                   [0, 255, 242], 2)
                        # elif data[track.track_id][3][0] <= 60:
                        #     cv2.rectangle(frame,
                        #                   (int(bbox[0] - 5), int(bbox[1]) - 5),
                        #                   (int(bbox[2] + 5), int(bbox[3] + 5)),
                        #                   [0, 255, 0], 2)
                        # elif data[track.track_id][3][0] <= 80:
                        #     cv2.rectangle(frame,
                        #                   (int(bbox[0] - 5), int(bbox[1] - 5)),
                        #                   (int(bbox[2] + 5), int(bbox[3] + 5)),
                        #                   [0, 0, 255], 2)
                    except:
                        continue
                if mobilenetv2:
                    try:
                        # 裁切圖片
                        x1 = int(bbox[0])
                        x2 = int(bbox[2])
                        y1 = int(bbox[1])
                        y2 = int(bbox[3])
                        # if x1 < 0:
                        #     x1 = 0
                        crop_img = org_img[y1:y2, x1:x2]
                        # mobilenet 分類
                        with sess1.as_default():
                            with sess1.graph.as_default():
                                # cv2.imwrite('classImg.jpg',crop_img)
                                # img_input = cv2.imread('classImg.jpg')
                                head_img = resize_keep_aspectratio(crop_img)
                                # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                                head_img = np.expand_dims(head_img, axis=0)
                                head_img = preprocess_input(head_img)
                                preds = model_mobilenet.predict(head_img)
                                predictions = np.argmax(preds, axis=1)[0]
                                classes = [
                                    'normal', 'tapered', 'pyriform',
                                    'amorphous'
                                ]
                        cv2.putText(frame, str(classes[predictions]),
                                    (x1, y1 - 8), 0, 5e-3 * 120, (0, 0, 0), 2)
                    except:
                        pass
        # 顯示偵測相關資訊
        count = len(set(counter))
        try:
            new_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            new_frame = cv2.resize(new_frame, (512, 384))
            showImage = QtGui.QImage(new_frame.data, new_frame.shape[1],
                                     new_frame.shape[0],
                                     QtGui.QImage.Format_RGB888)
            label_show_image.setPixmap(QtGui.QPixmap.fromImage(showImage))
            lcdNumber_1.display(round(fps))
            lcdNumber_2.display(i)
            lcdNumber_3.display(count)
        except:
            if information:
                cv2.putText(frame, 'FPS: {:.2f}'.format(fps), (10, 25), 0,
                            5e-3 * 125, (0, 255, 0), 2)
                cv2.putText(
                    frame,
                    'Current ' + 'sperm'.capitalize() + ' Counter: ' + str(i),
                    (10, 50), 0, 5e-3 * 125, (0, 255, 0), 2)
                cv2.putText(
                    frame, 'Total ' + 'sperm'.capitalize() + ' Counter: ' +
                    str(count), (10, 75), 0, 5e-3 * 125, (0, 255, 0), 2)
            cv2.namedWindow('YOLOv4_DeepSORT', 0)
            cv2.resizeWindow('YOLOv4_DeepSORT', 1024, 768)
            cv2.imshow('YOLOv4_DeepSORT', frame)

        if writeVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps = (fps + (1. / (time.time() - t1))) / 2
        fps_list.append(fps)
        out.write(frame)
        frame_index = frame_index + 1

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    print(' ')
    print('[Finish]')
    end = time.time()

    print('distance max:', max(data_dist))
    print('curvature max:', max(data_curv))
    print('FPS:', round(np.mean(fps_list), 2))

    if len(pts[track.track_id]) != None:
        print(str(count) + ' ' + str(class_name) + ' Found')

    else:
        print('[No Found]')

    #print('[INFO]: model_image_size = (960, 960)')
    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main(YOLO())

max_stars_repo_name: BlackyYen/AISS

max_issues_repo_name: BlackyYen/AISS

max_forks_repo_name: BlackyYen/AISS


text:
import numpy as np
from scipy import ndimage as nd
import tensorflow as tf

from prdepth import sampler
import prdepth.utils as ut

import cv2

H, W = sampler.H, sampler.W
IH, IW = sampler.IH, sampler.IW
PSZ = sampler.PSZ
STRIDE = sampler.STRIDE
HNPS, WNPS = sampler.HNPS, sampler.WNPS

class S2DOptimizer:
    ''' Optimizer class for sparse-to-dense with random sampling.

    Optimizations are done in the DORN output resolution, which is lower than
    the original image. The optimized global prediction is upsampled to the
    original resolution.
    '''

    def __init__(self, depth_sampler):
        self.patched_samples = depth_sampler.patched_samples
        self.nsamples = depth_sampler.nsamples

        self.PO = ut.PatchOp(1, IH, IW, PSZ, STRIDE)

        # Variables for optimization
        # Global estimation (DORN resolution).
        self.image_current = tf.Variable(
            tf.zeros([1, IH, IW, 1], dtype=tf.float32))
        # Patches used to get image_current, i.e. if group average these
        # patches, you would get image_current.
        self.patched_before = tf.Variable(
            tf.zeros([1, HNPS, WNPS, PSZ**2], dtype=tf.float32))
        # Global estimation (original resolution).
        self.resized_current = tf.image.resize_images(
            self.image_current, [H, W], align_corners=True)

        # Graph for initialization
        patched_init = tf.reduce_mean(self.patched_samples, axis=0)
        image_init = self.PO.group_patches(patched_init)
        self._init_op = tf.group([
            tf.assign(self.patched_before, patched_init).op,
            tf.assign(self.image_current, image_init).op])

        # Graph for updating sample selection (i.e., patched_before) based on
        # the updated global estimation, which is got by carrying out a few
        # number of gradient steps using the addtional global cost function.
        # In this sparse-to-dense application, the global estimation is updated
        # using sparse depth and Eq 9. & 10. in the paper.
        self._resized_updated_ph = tf.placeholder(
            shape=[H, W], dtype=tf.float32)
        image_current = tf.image.resize_images(
            self._resized_updated_ph[None, :, :, None],
            [IH, IW], align_corners=True)
        patched_current = self.PO.extract_patches(image_current)

        # Select the sample with the min distance to the (patch of) updated
        # global prediction.
        distance = ut.mean_diff(
            patched_current[None], self.patched_samples, axis=-1)
        min_index = tf.argmin(distance, axis=0)
        indices = tf.meshgrid(
            *[np.arange(i) for i in min_index.get_shape().as_list()], indexing='ij')
        min_indices = tf.stack([min_index] + indices, axis=-1)
        patched_best = tf.gather_nd(self.patched_samples, min_indices)
        image_best = self.PO.group_patches(patched_best)
        # Difference b/w the current prediction and the previous, used for
        # stopping the optimization.
        self._diff = ut.mean_diff(image_best, self.image_current)
        with tf.control_dependencies([self._diff]):
            self._sample_selection_op = tf.group([
                tf.assign(self.patched_before, patched_best).op,
                tf.assign(self.image_current, image_best).op])

    def initialize(self, sess):
        ''' Initialize the prediction. '''
        sess.run(self._init_op)

    def update_global_estimation(self, sparse_depth, gamma, num_gd_steps, sess):
        ''' Update the global depth estimation using sparse depth.

        By carrying out a few number of gradient steps using the addtional
        global cost function (Eq 9. & 10. in the paper). The sampling operataion
        in this case is just sampling at the measured locations in the sparse
        depth map. The tranpose of the sampling operataion is nearest neighbor
        interpolation of the valid pixels in the sparse depth.

        Args:
            sparse_depth: a sparse depth map (numpy array).
            gamma: step size for gradient descent.
            num_gd_steps: number of gradient descent steps.
            sess: TF session.
        Returns:
            Updated global estimation of the original resolution.
        '''

        # A map of indices, of which each pixel is the indices of the closest
        # valid measurement on the sparse depth map to this pixel.
        # This is used for filling values for all pixels of the sparse depth map
        # using nearest neighbor.
        if not hasattr(self, '_edt_indices'):
            invalid = (sparse_depth == 0)
            self._edt_indices = tuple(nd.distance_transform_edt(
                invalid, return_distances=False, return_indices=True))

        global_current = sess.run(self.resized_current).squeeze()
        for i in range(num_gd_steps):
            diff = global_current - sparse_depth
            gradient = diff[self._edt_indices]
            global_current = global_current - gamma * gradient

        return global_current

    def update_sample_selection(self, global_current, sess):
        ''' Update sample selection using the current global estimation.

        Args:
            global_current: the current global depth estimation of the original
                resolution.
            sess: TF session.
        Returns:
            Averaged squared difference of the current estimation and the
            previous estimation.
        '''
        diff, _ = sess.run(
            [self._diff, self._sample_selection_op],
            feed_dict={self._resized_updated_ph: global_current})
        return diff

class UpsamplingOptimizer(S2DOptimizer):
    def update_global_estimation(self, lowres_depth, gamma, num_gd_steps, sess):
        ''' Update the global depth estimation using low-resolution depth map.

        By carrying out a few number of gradient steps using the addtional
        global cost function (Eq 9. & 10. in the paper). The sampling operataion
        in this case is bicubic downsampling. The tranpose of the sampling
        operataion is bi-linear interpolation of the low-resolution depth.

        Args:
            lowres_depth: a low-resolution depth map (numpy array).
            gamma: step size for gradient descent.
            num_gd_steps: number of gradient descent steps.
            sess: TF session.
        Returns:
            Updated global estimation of the original resolution.
        '''

        global_current = sess.run(self.resized_current).squeeze()
        lh, lw = lowres_depth.shape
        for i in range(num_gd_steps):
            down_current = cv2.resize(
                global_current, (lw, lh), interpolation=cv2.INTER_CUBIC)
            diff = down_current - lowres_depth
            gradient = cv2.resize(diff, (W, H), interpolation=cv2.INTER_LINEAR)
            global_current = global_current - gamma * gradient

        return global_current

max_stars_repo_name: aneeldamaraju/prdepth

max_issues_repo_name: aneeldamaraju/prdepth

max_forks_repo_name: aneeldamaraju/prdepth

reason: import numpy,from scipy


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

import numpy as np

class SourceFunctionGenerator():

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

    def Ricker(self, ampl, peak_freq):
        """ A Ricker wavelet in the frequency domain. """

        source = ampl * 2 / np.sqrt(np.pi) * self.freq**2 / peak_freq**3 \
            * np.exp(-self.freq**2 / peak_freq**2).astype(complex)

        # Correct the amplitude
        source *= 2 * self.freq[-1]

        # Apply time shift
        tshift = 1.2 / peak_freq
        source *= np.exp(-1j * 2 * np.pi * self.freq * tshift)

        return source

max_stars_repo_name: mniskanen/composti

max_issues_repo_name: mniskanen/composti

max_forks_repo_name: mniskanen/composti


text:
# --------------
import pandas as pd
from sklearn.model_selection import train_test_split
#path - Path of file

# Code starts here
df = pd.read_csv(path)

X = df.drop(['customerID','Churn'],1)
y = df['Churn'].copy()

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, random_state = 0)

# --------------
import numpy as np
from sklearn.preprocessing import LabelEncoder

# Code starts here
X_train['TotalCharges'] = X_train['TotalCharges'].replace({' ':np.NaN})
X_test['TotalCharges'] = X_test['TotalCharges'].replace({' ':np.NaN})

X_train['TotalCharges'] = X_train['TotalCharges'].astype(float)
X_test['TotalCharges'] = X_test['TotalCharges'].astype(float)

X_train['TotalCharges'].fillna(value=np.mean(X_train['TotalCharges']), inplace=True)
X_test['TotalCharges'].fillna(value=np.mean(X_test['TotalCharges']), inplace=True)

print(X_train['TotalCharges'].isnull().sum())
print(X_test['TotalCharges'].isnull().sum())

le = LabelEncoder()
cat_col = list(X_train.select_dtypes(include = 'object').columns)
for i in cat_col:
    X_train[i] = le.fit_transform(X_train[i])
    X_test[i] = le.fit_transform(X_test[i])

y_train = y_train.replace({'No':0, 'Yes':1})
y_test = y_test.replace({'No':0, 'Yes':1})

# --------------
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix

# Code starts here
print(X_train.head())
print(y_train.head())
print(X_test.head())
print(y_test.head())

ada_model = AdaBoostClassifier(random_state=0)
ada_model.fit(X_train, y_train)

y_pred = ada_model.predict(X_test)
ada_score = accuracy_score(y_test, y_pred)
print(ada_score)

ada_cm = confusion_matrix(y_test, y_pred)
print(ada_cm)

ada_cr = classification_report(y_test, y_pred)
print(ada_cr)

# --------------
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV

#Parameter list
parameters={'learning_rate':[0.1,0.15,0.2,0.25,0.3],
            'max_depth':range(1,3)}

# Code starts here
#XGBoost
xgb_model = XGBClassifier(random_state=0)
xgb_model.fit(X_train, y_train)

y_pred = xgb_model.predict(X_test)
xgb_score = accuracy_score(y_test, y_pred)
print("XGBoost:-")
print("Accuracy: ",xgb_score)

xgb_cm = confusion_matrix(y_test, y_pred)
print('Confusion matrix: \n',xgb_cm)

xgb_cr = classification_report(y_test, y_pred)
print('Classification report: \n',xgb_cr)

#GridSearchCV
clf_model = GridSearchCV(estimator=xgb_model, param_grid=parameters)
clf_model.fit(X_train, y_train)

y_pred = clf_model.predict(X_test)
clf_score = accuracy_score(y_test, y_pred)
print("GridSearchCV:-")
print("Accuracy: ",clf_score)

clf_cm = confusion_matrix(y_test, y_pred)
print('Confusion matrix: \n',clf_cm)

clf_cr = classification_report(y_test, y_pred)
print('Classification report: \n',clf_cr)

max_stars_repo_name: PrasadKolase/ga-learner-dsb-repo

max_issues_repo_name: PrasadKolase/ga-learner-dsb-repo

max_forks_repo_name: PrasadKolase/ga-learner-dsb-repo


text:
import os
import io
import time
import traceback
import logging
import ctc_utils
import numpy as np
import simpleaudio as sa
from pathlib import Path
from midi.player import *
from scipy.io.wavfile import write as WAV
import tensorflow.python.util.deprecation as deprecation
def generateWAV(all_predictions, merged):
    logging.basicConfig(filename='logs/melody.log', level=logging.DEBUG)
    SEMANTIC = ''
    playlist = []
    track = 0
    export = 0
    single = merged
    directory = 'data\\melody\\'
    del_directory = '\\data\\melody'
    mypath = Path().absolute()
    delete_str = str(mypath) + del_directory
    remove_dir = os.listdir(delete_str)
    for item in remove_dir:
        if (item.endswith(".wav")) or (item.endswith(".txt")):
            os.remove(os.path.join(delete_str, item))
    all_predictions = [x for x in all_predictions if x.strip()]
    logging.info("SYMBOL: printing all predictions")
    logging.info(all_predictions)
#     all_txt = ''.join(map(str, all_predictions))
#     with open(directory + 'all_predictions'+'.txt', 'w') as file:
#         file.write(all_txt)
    try:
        for SEMANTIC in all_predictions:
            if SEMANTIC:
                # gets the audio file
                audio = get_sinewave_audio(SEMANTIC)
                # horizontally stacks the freqs
                audio =  np.hstack(audio)
                # normalizes the freqs
                audio *= 32767 / np.max(np.abs(audio))
                #converts it to 16 bits
                audio = audio.astype(np.int16)
                playlist.append(audio)
                with open(directory + 'predictions'+ str(export) +'.txt', 'w') as file:
                    file.write(SEMANTIC)
                export+=1
    except Exception as e:
        logging.error(traceback.format_exc())
        logging.error("AUDIO: could not generate sinewave audio")
    try:
        if(playlist):
            if(single == "true"):
                for song in playlist:
                    output_file = directory + 'staff' + str(track) + '.wav'
                    WAV(output_file, 44100, song)
                    track+=1
            else:
                output_file = directory + "full_song" + '.wav'
                full_song = None
                for song in playlist:
                    small_file = directory + 'staff' + str(track) + '.wav'
                    WAV(small_file, 44100, song)
                    track+=1
                    if (full_song) is None:
                        full_song = song
                    else:
                        full_song = np.concatenate((full_song, song))

                WAV(output_file, 44100, full_song)
    except Exception as e:
        logging.error(traceback.format_exc())
        logging.error("AUDIO: could not generate sinewave audio")

max_stars_repo_name: Aroueterra/notable-flask

max_issues_repo_name: Aroueterra/notable-flask

max_forks_repo_name: Aroueterra/notable-flask


text:
from   __future__ import print_function
import numpy as np
from   PIL import Image
import pickle as pkl
import os
import glob
import csv
from   tqdm import tqdm
import cv2

class dataset_tiered(object):
    def __init__(self, n_examples, n_episodes, split, args):
        self.im_width, self.im_height, self.channels = list(map(int, args['x_dim'].split(',')))
        self.n_examples = n_examples
        self.n_episodes = n_episodes
        self.split      = split
        self.ratio      = args['ratio']
        self.seed       = args['seed']
        self.root_dir   = './data/tieredImagenet'

        self.iamge_data = []
        self.dict_index_label = []
        self.dict_index_unlabel = []

    def load_data_pkl(self):
        """
            load the pkl processed tieredImagenet into label,unlabel
            maintain label,unlabel data dictionary for indexes
        """
        labels_name = '{}/data/{}_labels.pkl'.format(self.root_dir, self.split)
        images_name = '{}/data/{}_images.npz'.format(self.root_dir, self.split)
        print('labels:',labels_name)
        print('images:',images_name)

        # decompress images if npz not exits
        if not os.path.exists(images_name):
            png_pkl = images_name[:-4] + '_png.pkl'
            if os.path.exists(png_pkl):
                decompress(images_name, png_pkl)
            else:
                raise ValueError('path png_pkl not exits')

        if os.path.exists(images_name) and os.path.exists(labels_name):
            try:
                with open(labels_name) as f:
                    data            = pkl.load(f)
                    label_specific  = data["label_specific"]
                    #label_general  = data["label_general"]
                    #label_specific_str = data["label_specific_str"]
                    #label_general_str  = data["label_general_str"]
            except:
                with open(labels_name, 'rb') as f:
                    data            = pkl.load(f, encoding='bytes')
                    #label_specific  = data[b'label_specific']
                    label_specific = data['label_specific']
                    #label_general  = data[b"label_general"]
                    #label_specific_str = data[b"label_specific_str"]
                    #label_general_str  = data[b"label_general_str"]
            print('read label data:{}'.format(len(label_specific)))
        labels      = label_specific

        with np.load(images_name, mmap_mode="r", encoding='latin1') as data:
            image_data = data["images"]
            print('read image data:{}'.format(image_data.shape))

        n_classes   = np.max(labels)+1

        print('n_classes:{}, n_label:{}%, n_unlabel:{}%'.format(n_classes,self.ratio*100,(1-self.ratio)*100))
        dict_index_label    = {} # key:label, value:idxs
        dict_index_unlabel  = {}

        for cls in range(n_classes):
            idxs = np.where(labels==cls)[0]
            nums = idxs.shape[0]
            np.random.RandomState(self.seed).shuffle(idxs) # fix the seed to keep label,unlabel fixed

            n_label     = int(self.ratio*nums)
            n_unlabel   = nums-n_label

            dict_index_label[cls]   = idxs[0:n_label]
            dict_index_unlabel[cls] = idxs[n_label:]

        self.image_data         = image_data
        self.dict_index_label   = dict_index_label
        self.dict_index_unlabel = dict_index_unlabel
        self.n_classes          = n_classes
        print(dict_index_label[0])
        print(dict_index_unlabel[0])

    def next_data(self, n_way, n_shot, n_query, num_unlabel=0, n_distractor=0, train=True):
        """
            get support,query,unlabel data from n_way
            get unlabel data from n_distractor
        """
        support     = np.zeros([n_way, n_shot, self.im_height, self.im_width, self.channels], dtype=np.float32)
        query       = np.zeros([n_way, n_query, self.im_height, self.im_width, self.channels], dtype=np.float32)
        if num_unlabel>0:
            unlabel = np.zeros([n_way+n_distractor, num_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
        else:
            unlabel = []
            n_distractor = 0

        selected_classes = np.random.permutation(self.n_classes)[:n_way+n_distractor]
        for i, cls in enumerate(selected_classes[0:n_way]): # train way
            # labled data
            idx             = self.dict_index_label[cls]
            np.random.RandomState().shuffle(idx)
            idx1            = idx[0:n_shot + n_query]
            support[i]      = self.image_data[idx1[:n_shot]]
            query[i]        = self.image_data[idx1[n_shot:]]

            # unlabel
            if num_unlabel>0:
                idx         = self.dict_index_unlabel[cls]
                np.random.RandomState().shuffle(idx)
                idx2        = idx[0:num_unlabel]
                unlabel[i]  = self.image_data[idx2]

        for j,cls in enumerate(selected_classes[self.n_classes:]): # distractor way
            idx             = self.dict_index_unlabel[cls]
            np.random.RandomState().shuffle(idx)
            idx3            = idx[0:num_unlabel]
            unlabel[i+j]    = self.image_data[idx3]

        support_labels      = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_shot)).astype(np.uint8)
        query_labels        = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_query)).astype(np.uint8)
        # unlabel_labels = np.tile(np.arange(n_way+n_distractor)[:, np.newaxis], (1, num_unlabel)).astype(np.uint8)
        return support, support_labels, query, query_labels, unlabel

def compress(path, output):
  with np.load(path, mmap_mode="r") as data:
    images = data["images"]
    array = []
    for ii in tqdm(six.moves.xrange(images.shape[0]), desc='compress'):
      im = images[ii]
      im_str = cv2.imencode('.png', im)[1]
      array.append(im_str)
  with open(output, 'wb') as f:
    pkl.dump(array, f, protocol=pkl.HIGHEST_PROTOCOL)

def decompress(path, output):
  with open(output, 'rb') as f:
    array = pkl.load(f, encoding='bytes')
  images = np.zeros([len(array), 84, 84, 3], dtype=np.uint8)
  for ii, item in tqdm(enumerate(array), desc='decompress'):
    im = cv2.imdecode(item, 1)
    images[ii] = im
  np.savez(path, images=images)


text:
"""
code from here: https://github.com/junhyukoh/value-prediction-network/blob/master/maze.py
"""

import copy
import pandas as pd

import seaborn as sns

import gym.spaces
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.pyplot as plt
import numpy as np
from exploration_models import *
from graphics import *
from gym import spaces

class GridWorld(gym.Env):
    """
    empty grid world
    """

    def __init__(self,
                 num_rooms=0,
                 start_position=(25.0, 25.0),
                 goal_position=(75.0, 75.0),
                 goal_reward=+100.0,
                 dense_goals=None,
                 dense_reward=+5,
                 goal_radius=1.0,
                 per_step_penalty=-0.01,
                 max_episode_len=1000,
                 grid_len=100,
                 wall_breadth=1,
                 door_breadth=5,
                 action_limit_max=1.0,
                 silent_mode=False):
        """
        params:
        """

        # num of rooms
        self.num_rooms = num_rooms
        self.silent_mode = silent_mode

        # grid size
        self.grid_len = float(grid_len)
        self.wall_breadth = float(wall_breadth)
        self.door_breadth = float(door_breadth)
        self.min_position = 0.0
        self.max_position = float(grid_len)

        # goal stats
        self.goal_position = np.array(goal_position)
        self.goal_radius = goal_radius
        self.start_position = np.array(start_position)

        # Dense reward stuff:
        self.dense_reward = dense_reward
        # List of dense goal coordinates
        self.dense_goals = dense_goals

        # rewards
        self.goal_reward = goal_reward
        self.per_step_penalty = per_step_penalty

        self.max_episode_len = max_episode_len

        # observation space
        self.low_state = np.array([self.min_position, self.min_position])
        self.high_state = np.array([self.max_position, self.max_position])

        # how much the agent can move in a step (dx,dy)
        self.min_action = np.array([-action_limit_max, -action_limit_max])
        self.max_action = np.array([+action_limit_max, +action_limit_max])

        self.observation_space = spaces.Box(low=self.low_state, high=self.high_state)
        self.action_space = spaces.Box(low=self.min_action, high=self.max_action)
        self.nb_actions = self.action_space.shape[-1]

        # add the walls here
        self.create_walls()
        self.scale = 5

        # This code enables live visualization of trajectories
        # Susan added these lines for visual purposes
        if not self.silent_mode:
            self.win1 = GraphWin("2DGrid", self.max_position * self.scale + 40, self.max_position * self.scale + 40)
            rectangle1 = Rectangle(Point(self.min_position * self.scale + 20, self.min_position * self.scale + 20),
                                   Point(self.max_position * self.scale + 20, self.max_position * self.scale + 20))
            rectangle1.setOutline('red')
            rectangle1.draw(self.win1)

            if self.num_rooms > 0:
                wall1 = Rectangle(Point(self.min_position * self.scale + 20,
                                        self.max_position * self.scale / 2 + 20 - self.wall_breadth * self.scale),
                                  Point(self.max_position * self.scale / 2 + 20,
                                        self.max_position * self.scale / 2 + 20 + self.wall_breadth * self.scale))
                wall1.draw(self.win1)
                wall1.setFill('aquamarine')

                wall2 = Rectangle(Point(self.max_position * self.scale / 2 + 20 - self.wall_breadth * self.scale,
                                        self.min_position * self.scale + 20),
                                  Point(self.max_position * self.scale / 2 + 20 + self.wall_breadth * self.scale,
                                        self.max_position * self.scale / 4 + 20 - self.door_breadth * self.scale))
                wall2.draw(self.win1)
                wall2.setFill('aquamarine')

                wall3 = Rectangle(Point(self.max_position * self.scale / 2 + 20 - self.wall_breadth * self.scale,
                                        self.max_position * self.scale / 4 + 20 + self.door_breadth * self.scale),
                                  Point(self.max_position * self.scale / 2 + 20 + self.wall_breadth * self.scale,
                                        self.max_position * self.scale / 2 + 20 + self.wall_breadth * self.scale))
                wall3.draw(self.win1)
                wall3.setFill('aquamarine')
            start_point = Circle(Point(start_position[0] * self.scale + 20, start_position[1] * self.scale + 20),
                                 goal_radius * self.scale)
            start_point.draw(self.win1)
            start_point.setFill('red')
            goal_point = Circle(Point(goal_position[0] * self.scale + 20, goal_position[1] * self.scale + 20),
                                goal_radius * self.scale)
            goal_point.draw(self.win1)
            goal_point.setFill('green')

            # Drawing the dense goals:
            for idx, mini_goal in enumerate(self.dense_goals):
                mini_goal_point = Circle(Point(mini_goal[0] * self.scale + 20, mini_goal[1] * self.scale + 20),
                                         goal_radius * self.scale)
                mini_goal_point.draw(self.win1)
                mini_goal_point.setFill('blue')

            # self.win1.getMouse()

        self.seed()
        self.reset()

    def reset(self):
        self.state = copy.deepcopy(self.start_position)
        self.t = 0
        self.done = False

        return self._get_obs()

    def _get_obs(self):
        return copy.deepcopy(self.state)

    def step(self, a):
        """
        take the action here
        """

        # check if the action is valid
        assert self.action_space.contains(a)
        assert self.done is False

        # Susan added this line
        self.state_temp = copy.deepcopy(self.state)

        self.t += 1

        # check if collides, if it doesn't then update the state
        if self.num_rooms == 0 or not self.collides((self.state[0] + a[0], self.state[1] + a[1])):
            # move the agent and update the state
            self.state[0] += a[0]
            self.state[1] += a[1]

        # clip the state if out of bounds
        self.state[0] = np.clip(self.state[0], self.min_position, self.max_position)
        self.state[1] = np.clip(self.state[1], self.min_position, self.max_position)

        # the reward logic
        reward = self.per_step_penalty

        # Adding dense Rewards:
        for idx, mini_goal in enumerate(self.dense_goals):
            if np.linalg.norm(np.array(self.state) - np.array(mini_goal), 2) <= self.goal_radius:
                reward = self.dense_reward

        # if reached goal (within a radius of 1 unit)
        if np.linalg.norm(np.array(self.state) - np.array(self.goal_position), 2) <= self.goal_radius:
            # episode done
            self.done = True
            reward = self.goal_reward

        if self.t >= self.max_episode_len:
            self.done = True

        line = Line(Point(self.state_temp[0] * self.scale + 20, self.state_temp[1] * self.scale + 20),
                    Point(self.state[0] * self.scale + 20, self.state[1] * self.scale + 20))

        if not self.silent_mode:
            line.draw(self.win1)
            line.setOutline('black')
            # self.win1.getMouse()
        self.state_temp = self.state

        if self.silent_mode:
            return self._get_obs(), reward, self.done, None

        # return self.win1,self._get_obs(), reward, self.done, None
        return self._get_obs(), reward, self.done, None

    def sample(self, exploration, b_0, l_p, ou_noise, stddev):
        """ take a random sample """
        if exploration == 'RandomWalk':
            return np.random.uniform(low=self.min_action[0], high=self.max_action[0], size=(2,))
        elif exploration == 'PolyRL':
            return PolyNoise(L_p=float(l_p), b_0=float(b_0), action_dim=self.nb_actions, ou_noise=ou_noise,
                             sigma=float(stddev))
        else:
            raise Exception("The exploration method " + self.exploration + " is not defined!")

    def create_walls(self):
        """
        create the walls here, the polygons
        """
        self.walls = []

        # codes for drawing the polygons in matplotlib
        codes = [path.Path.MOVETO,
                 path.Path.LINETO,
                 path.Path.LINETO,
                 path.Path.LINETO,
                 path.Path.CLOSEPOLY,
                 ]

        if self.num_rooms == 0:
            # no walls required
            return
        elif self.num_rooms == 1:
            # create one room with one opening

            # a wall parallel to x-axis, at (0,grid_len/2), (grid_len/2,grid_len/2)
            self.walls.append(path.Path([(0, self.grid_len / 2.0 + self.wall_breadth),
                                         (0, self.grid_len / 2.0 - self.wall_breadth),
                                         (self.grid_len / 2.0, self.grid_len / 2.0 - self.wall_breadth),
                                         (self.grid_len / 2.0, self.grid_len / 2.0 + self.wall_breadth),
                                         (0, self.grid_len / 2.0 + self.wall_breadth)
                                         ], codes=codes))

            # the top part  of wall on (0,grid_len/2), parallel to y -axis containg
            self.walls.append(path.Path([(self.grid_len / 2.0 - self.wall_breadth, self.grid_len / 2.0),
                                         (self.grid_len / 2.0 - self.wall_breadth,
                                          self.grid_len / 4.0 + self.door_breadth),
                                         (self.grid_len / 2.0 + self.wall_breadth,
                                          self.grid_len / 4.0 + self.door_breadth),
                                         (self.grid_len / 2.0 + self.wall_breadth, self.grid_len / 2.0),
                                         (self.grid_len / 2.0 - self.wall_breadth, self.grid_len / 2.0),
                                         ], codes=codes))

            # the bottom part  of wall on (0,grid_len/2), parallel to y -axis containg
            self.walls.append(
                path.Path([(self.grid_len / 2.0 - self.wall_breadth, self.grid_len / 4.0 - self.door_breadth),
                           (self.grid_len / 2.0 - self.wall_breadth, 0.),
                           (self.grid_len / 2.0 + self.wall_breadth, 0.),
                           (self.grid_len / 2.0 + self.wall_breadth, self.grid_len / 4.0 - self.door_breadth),
                           (self.grid_len / 2.0 - self.wall_breadth, self.grid_len / 4.0 - self.door_breadth),
                           ], codes=codes))

        elif self.num_rooms == 4:
            # create 4 rooms
            raise Exception("Not implemented yet :(")
        else:
            raise Exception("Logic for current number of rooms " +
                            str(self.num_rooms) + " is not implemented yet :(")

    def collides(self, pt):
        """
        to check if the point (x,y) is in the area defined by the walls polygon (i.e. collides)
        """
        wall_edge_low = self.grid_len / 2 - self.wall_breadth
        wall_edge_high = self.grid_len / 2 + self.wall_breadth
        for w in self.walls:
            if w.contains_point(pt):
                return True
            elif pt[0] <= self.min_position and pt[1] > wall_edge_low and pt[1] < wall_edge_high:
                return True
            elif pt[1] <= self.min_position and pt[0] > wall_edge_low and pt[0] < wall_edge_high:
                return True
        return False

    def vis_trajectory(self, traj, name_plot, experiment_id=None, imp_states=None):
        """
        creates the trajectory and return the plot

        trj: numpy_array

        Code taken from: https://discuss.pytorch.org/t/example-code-to-put-matplotlib-graph-to-tensorboard-x/15806
        """
        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_subplot(111)

        # convert the environment to the image
        ax.set_xlim(0.0, self.max_position)
        ax.set_ylim(0.0, self.max_position)

        # add the border here
        # for i in ax.spines.itervalues():
        #     i.set_linewidth(0.1)

        # plot any walls if any
        for w in self.walls:
            patch = patches.PathPatch(w, facecolor='gray', lw=2)
            ax.add_patch(patch)

        # plot the start and goal points
        ax.scatter([self.start_position[0]], [self.start_position[1]], c='g')
        ax.scatter([self.goal_position[0]], [self.goal_position[1]], c='y')

        # Plot the dense rewards:
        for idx, mini_goal in enumerate(self.dense_goals):
            ax.scatter([mini_goal[0]], [mini_goal[1]], c='b')

        # add the trajectory here
        # https://stackoverflow.com/questions/36607742/drawing-phase-space-trajectories-with-arrows-in-matplotlib

        ax.quiver(traj[:-1, 0], traj[:-1, 1],
                  traj[1:, 0] - traj[:-1, 0], traj[1:, 1] - traj[:-1, 1],
                  scale_units='xy', angles='xy', scale=1, color='black')

        # plot the decision points/states
        if imp_states is not None:
            ax.scatter(imp_states[:, 0], imp_states[:, 1], c='r')

        # return the image buff

        ax.set_title("grid")
        # fig.savefig(buf, format='jpeg') # maybe png
        fig.savefig('install/{}_{}'.format(name_plot, experiment_id), dpi=300)  # maybe png

    def test_vis_trajectory(self, traj, name_plot, heatmap_title, experiment_id=None, heatmap_normalize=False,
                            heatmap_vertical_clip_value=2500):

        # Trajectory heatmap
        x = np.array([point[0] * self.scale for point in traj])
        y = np.array([point[1] * self.scale for point in traj])

        # Save heatmap for different bin scales
        for num in range(2, 6):
            fig, ax = plt.subplots()

            bin_scale = num * 0.1

            h = ax.hist2d(x, y, bins=[np.arange(self.min_position * self.scale,
                                                self.max_position * self.scale, num),
                                      np.arange(self.min_position * self.scale,
                                                self.max_position * self.scale, num)],
                          cmap='Blues', normed=heatmap_normalize, vmax=heatmap_vertical_clip_value)
            image = h[3]
            plt.colorbar(image, ax=ax)

            # Build graph barriers and start and goal positions
            start_point = (self.start_position[0] * self.scale, self.start_position[1] * self.scale)
            radius = self.goal_radius * self.scale / 2
            start_circle = patches.Circle(start_point, radius,
                                          facecolor='gold', edgecolor='black', lw=0.5, zorder=10)

            goal_point = (self.goal_position[0] * self.scale, self.goal_position[1] * self.scale)
            goal_circle = patches.Circle(goal_point, radius,
                                         facecolor='brown', edgecolor='black', lw=0.5, zorder=10)

            for idx, dense_goal in enumerate(self.dense_goals):
                dense_goal_point = (dense_goal[0] * self.scale, dense_goal[1] * self.scale)
                dense_goal_circle = patches.Circle(dense_goal_point, radius,
                                                   facecolor='crimson', edgecolor='black', lw=0.5, zorder=10)
                ax.add_patch(dense_goal_circle)

            ax.add_patch(start_circle)
            ax.add_patch(goal_circle)

            if self.num_rooms == 1:
                wall1_xy = (self.min_position * self.scale,
                            self.max_position/2 * self.scale - self.wall_breadth * self.scale)
                wall1_width = (self.max_position/2 - self.min_position) * self.scale
                wall1_height = 2 * self.wall_breadth * self.scale
                wall1_rect = patches.Rectangle(xy=wall1_xy, width=wall1_width, height=wall1_height,
                                               facecolor='grey', zorder=10)

                wall2_xy = (self.max_position/2 * self.scale - self.wall_breadth * self.scale,
                            self.min_position * self.scale)
                wall2_width = 2 * self.wall_breadth * self.scale
                wall2_height = (self.max_position/4 - self.door_breadth - self.min_position) * self.scale
                wall2_rect = patches.Rectangle(xy=wall2_xy, width=wall2_width, height=wall2_height,
                                               facecolor='grey', zorder=10)

                wall3_xy = ((self.max_position/2 - self.wall_breadth) * self.scale,
                            (self.max_position/4 + self.door_breadth) * self.scale)
                wall3_width = 2 * self.wall_breadth * self.scale
                wall3_height = (self.max_position/2 + self.wall_breadth - self.max_position/4 - self.door_breadth) * self.scale
                wall3_rect = patches.Rectangle(xy=wall3_xy, width=wall3_width, height=wall3_height,
                                               facecolor='grey', zorder=10)

                ax.add_patch(wall1_rect)
                ax.add_patch(wall2_rect)
                ax.add_patch(wall3_rect)

            ax.set_title(heatmap_title)
            plt.savefig('install/{}_{}_{}.pdf'.format(name_plot, experiment_id, num))

max_stars_repo_name: joeybose/FloRL

max_issues_repo_name: joeybose/FloRL

max_forks_repo_name: joeybose/FloRL