text:
# coding: utf-8
from rnnmath import *
import numpy as np
from sys import stdout
import time
from tqdm import tqdm

class RNN(object):
	'''
	This class implements Recurrent Neural Networks.

	You should implement code in the following functions:
		predict				->	predict an output sequence for a given input sequence
		acc_deltas			->	accumulate update weights for the RNNs weight matrices, standard Back Propagation
		acc_deltas_bptt		->	accumulate update weights for the RNNs weight matrices, using Back Propagation Through Time
		compute_loss 		->	compute the (cross entropy) loss between the desired output and predicted output for a given input sequence
		compute_mean_loss	->	compute the average loss over all sequences in a corpus
		generate_sequence	->	use the RNN to generate a new (unseen) sequnce

	Do NOT modify any other methods!
	Do NOT change any method signatures!
	'''

	def __init__(self, vocab_size, hidden_dims):
		'''
		initialize the RNN with random weight matrices.

		DO NOT CHANGE THIS

		vocab_size		size of vocabulary that is being used
		hidden_dims		number of hidden units
		'''
		self.vocab_size = vocab_size
		self.hidden_dims = hidden_dims

		# matrices V (input -> hidden), W (hidden -> output), U (hidden -> hidden)
		self.U = np.random.randn(self.hidden_dims, self.hidden_dims)*np.sqrt(0.1)
		self.V = np.random.randn(self.hidden_dims, self.vocab_size)*np.sqrt(0.1)
		self.W = np.random.randn(self.vocab_size, self.hidden_dims)*np.sqrt(0.1)

		# matrices to accumulate weight updates
		self.deltaU = np.zeros((self.hidden_dims, self.hidden_dims))
		self.deltaV = np.zeros((self.hidden_dims, self.vocab_size))
		self.deltaW = np.zeros((self.vocab_size, self.hidden_dims))

	def apply_deltas(self, learning_rate):
		'''
		update the RNN's weight matrices with corrections accumulated over some training instances

		DO NOT CHANGE THIS

		learning_rate	scaling factor for update weights
		'''
		# apply updates to U, V, W
		self.U += learning_rate*self.deltaU
		self.W += learning_rate*self.deltaW
		self.V += learning_rate*self.deltaV

		# reset matrices
		self.deltaU.fill(0.0)
		self.deltaV.fill(0.0)
		self.deltaW.fill(0.0)

	def predict(self, x):
		'''
		predict an output sequence y for a given input sequence x

		x	list of words, as indices, e.g.: [0, 4, 2]

		returns	y,s
		y	matrix of probability vectors for each input word
		s	matrix of hidden layers for each input word

		'''

		# matrix s for hidden states, y for output states, given input x.
		#rows correspond to times t, i.e., input words
		# s has one more row, since we need to look back even at time 0 (s(t=0-1) will just be [0. 0. ....] )
		s = np.zeros((len(x)+1, self.hidden_dims))
		y = np.zeros((len(x), self.vocab_size))
		for t in range(len(x)):
			s[t] = sigmoid(self.V[:,x[t]] + np.dot(self.U,s[t-1,:].T))
			y[t] = softmax(np.dot(self.W,s[t,:].T))

		return y,s

	def acc_deltas(self, x, d, y, s):
		'''
		accumulate updates for V, W, U
		standard back propagation

		this should not update V, W, U directly. instead, use deltaV, deltaW, deltaU to accumulate updates over time

		x	list of words, as indices, e.g.: [0, 4, 2]
		d	list of words, as indices, e.g.: [4, 2, 3]
		y	predicted output layer for x; list of probability vectors, e.g., [[0.3, 0.1, 0.1, 0.5], [0.2, 0.7, 0.05, 0.05] [...]]
			should be part of the return value of predict(x)
		s	predicted hidden layer for x; list of vectors, e.g., [[1.2, -2.3, 5.3, 1.0], [-2.1, -1.1, 0.2, 4.2], [...]]
			should be part of the return value of predict(x)

		no return values
		'''
		for t in reversed(range(len(x))):
			delta = make_onehot(d[t],y.shape[1])
			delta_out = delta-y[t,:]
			delta_out = delta_out.reshape(delta_out.shape[0],1)
			s_temp = s[t,:].reshape(s[t,:].shape[0],1)
			self.deltaW += delta_out.dot(s_temp.T)
			delta_in = self.W.T.dot(delta_out)*(s_temp*(1-s_temp))
			x_temp = make_onehot(x[t],self.V.shape[1])
			x_temp = x_temp.reshape(x_temp.shape[0],1)
			self.deltaV += delta_in.dot(x_temp.T)
			s_temp_1 = s[t-1,:].reshape(s[t-1,:].shape[0],1)
			self.deltaU += delta_in.dot(s_temp_1.T)

	def acc_deltas_bptt(self, x, d, y, s, steps):
		'''
		accumulate updates for V, W, U
		back propagation through time (BPTT)

		this should not update V, W, U directly. instead, use deltaV, deltaW, deltaU to accumulate updates over time

		x		list of words, as indices, e.g.: [0, 4, 2]
		d		list of words, as indices, e.g.: [4, 2, 3]
		y		predicted output layer for x; list of probability vectors, e.g., [[0.3, 0.1, 0.1, 0.5], [0.2, 0.7, 0.05, 0.05] [...]]
				should be part of the return value of predict(x)
		s		predicted hidden layer for x; list of vectors, e.g., [[1.2, -2.3, 5.3, 1.0], [-2.1, -1.1, 0.2, 4.2], [...]]
				should be part of the return value of predict(x)
		steps	number of time steps to go back in BPTT

		no return values
		'''

		for t in reversed(range(len(x))):
			delta = make_onehot(d[t],y.shape[1])
			delta_out = delta-y[t,:]
			delta_out = delta_out.reshape(delta_out.shape[0],1)
			s_temp = s[t,:].reshape(s[t,:].shape[0],1)
			self.deltaW += delta_out.dot(s_temp.T)
			delta_in = self.W.T.dot(delta_out)*(s_temp*(1-s_temp))
			for i in range(t,max(-1,t-steps-1),-1):
				s_temp = s[i-1,:].reshape(s[i,:].shape[0],1)
				self.deltaU += delta_in.dot(s_temp.T)
				x_temp = make_onehot(x[i],self.V.shape[1])
				x_temp = x_temp.reshape(x_temp.shape[0],1)
				self.deltaV += delta_in.dot(x_temp.T)
				s_temp = s[i-1,:].reshape(s[i-1,:].shape[0],1)
				delta_in = self.U.T.dot(delta_in)*(s_temp*(1-s_temp))

	def compute_loss(self, x, d):
		'''
		compute the loss between predictions y for x, and desired output d.

		first predicts the output for x using the RNN, then computes the loss w.r.t. d

		x		list of words, as indices, e.g.: [0, 4, 2]
		d		list of words, as indices, e.g.: [4, 2, 3]

		return loss		the combined loss for all words
		'''
		loss = 0.
		y,_ = self.predict(x)
		log_y = -np.log(y)
		loss_temp = np.zeros(len(x))
		for i in range(0,len(x)):
			loss_temp[i] = log_y[i,d[i]]
		loss = np.sum(loss_temp)

		return loss

	def compute_mean_loss(self, X, D):
		'''
		compute the mean loss between predictions for corpus X and desired outputs in corpus D.

		X		corpus of sentences x1, x2, x3, [...], each a list of words as indices.
		D		corpus of desired outputs d1, d2, d3 [...], each a list of words as indices.

		return mean_loss		average loss over all words in D
		'''

		mean_loss = 0.
		mean_loss_temp=[]
		sum_w=0
		for x,d in zip(X,D):
			mean_loss_temp.append(self.compute_loss(x,d))
			sum_w += x.shape[0]
		mean_loss_a = np.array(mean_loss_temp)
		mean_loss = np.sum(mean_loss_a)/float(sum_w)

		return mean_loss

	def generate_sequence(self, start, end, maxLength):
		'''
		generate a new sequence, using the RNN

		starting from the word-index for a start symbol, generate some output until the word-index of an end symbol is generated, or the sequence
		exceed maxLength

		HINT: make use of the "multinomial_sample" method in rnnmath.py !!!

		start		word index of start symbol (the symbol <s> in a vocabulary)
		end			word index of end symbol (the symbol </s> in a vocabulary)
		maxLength	maximum length of the generated sequence

		return sequence, loss

		sequence	the generated sequence as a list of word indices, e.g., [4, 2, 3, 5]
		loss		the loss of the generated sequence
		'''
		sequence = [start]
		loss = 0.
		x = [start]
		flag = True
		i=0
		while i < maxLength and flag:
			y,s = self.predict(x)
			sample = multinomial_sample(y[i,:])
			while sample == 3 or sample == 14 or sample==37 or sample == 25 :
				sample = multinomial_sample(y[i,:])
			if sample != end:
				x.append(sample)
				loss += -np.log(y[i,sample])

			else:
				x.append(sample)
				loss += - np.log(y[i,sample])
				flag = False
			i += 1
		sequence = x
		return sequence, loss

	def train(self, X, D, X_dev, D_dev, epochs=10, learning_rate=0.5, anneal=5, back_steps=0, batch_size=100, min_change=0.0001, log=True):
		'''
		train the RNN on some training set X, D while optimizing the loss on a dev set X_dev, D_dev

		DO NOT CHANGE THIS

		training stops after the first of the following is true:
			* number of epochs reached
			* insignificant change observed for more than 2 consecutive epochs

		X				a list of input vectors, e.g., 		[[0, 4, 2], [1, 3, 0]]
		D				a list of desired outputs, e.g., 	[[4, 2, 3], [3, 0, 3]]
		X_dev			a list of input vectors, e.g., 		[[0, 4, 2], [1, 3, 0]]
		D_dev			a list of desired outputs, e.g., 	[[4, 2, 3], [3, 0, 3]]
		epochs			maximum number of epochs (iterations) over the training set. default 10
		learning_rate	initial learning rate for training. default 0.5
		anneal			positive integer. if > 0, lowers the learning rate in a harmonically after each epoch.
						higher annealing rate means less change per epoch.
						anneal=0 will not change the learning rate over time.
						default 5
		back_steps		positive integer. number of timesteps for BPTT. if back_steps < 2, standard BP will be used. default 0
		batch_size		number of training instances to use before updating the RNN's weight matrices.
						if set to 1, weights will be updated after each instance. if set to len(X), weights are only updated after each epoch.
						default 100
		min_change		minimum change in loss between 2 epochs. if the change in loss is smaller than min_change, training stops regardless of
						number of epochs left.
						default 0.0001
		log				whether or not to print out log messages. (default log=True)
		'''
		if log:
			stdout.write("\nTraining model for {0} epochs\ntraining set: {1} sentences (batch size {2})".format(epochs, len(X), batch_size))
			stdout.write("\nOptimizing loss on {0} sentences".format(len(X_dev)))
			stdout.write("\nVocab size: {0}\nHidden units: {1}".format(self.vocab_size, self.hidden_dims))
			stdout.write("\nSteps for back propagation: {0}".format(back_steps))
			stdout.write("\nInitial learning rate set to {0}, annealing set to {1}".format(learning_rate, anneal))
			stdout.write("\n\ncalculating initial mean loss on dev set")
			stdout.flush()

		t_start = time.time()

		loss_sum = sum([len(d) for d in D_dev])
		initial_loss = sum([self.compute_loss(X_dev[i], D_dev[i]) for i in range(len(X_dev))])/loss_sum

		if log or not log:
			stdout.write(": {0}\n".format(initial_loss))
			stdout.flush()

		prev_loss = initial_loss
		loss_watch_count = -1
		min_change_count = -1

		a0 = learning_rate

		best_loss = initial_loss
		bestU, bestV, bestW = self.U, self.V, self.W
		best_epoch = 0

		for epoch in range(epochs):
			if anneal > 0:
				learning_rate = a0/((epoch+0.0+anneal)/anneal)
			else:
				learning_rate = a0

			if log:
				stdout.write("\nepoch %d, learning rate %.04f" % (epoch+1, learning_rate))
				stdout.flush()

			t0 = time.time()
			count = 0

			# use random sequence of instances in the training set (tries to avoid local maxima when training on batches)
			permutation = np.random.permutation(range(len(X)))
			if log:
				stdout.write("\tinstance 1")
			for i in range(len(X)):
				c = i+1
				if log:
					stdout.write("\b"*len(str(i)))
					stdout.write("{0}".format(c))
					stdout.flush()
				p = permutation[i]
				x_p = X[p]
				d_p = D[p]

				y_p, s_p = self.predict(x_p)
				if back_steps == 0:
					self.acc_deltas(x_p, d_p, y_p, s_p)
				else:
					self.acc_deltas_bptt(x_p, d_p, y_p, s_p, back_steps)

				if i % batch_size == 0:
					self.deltaU /= batch_size
					self.deltaV /= batch_size
					self.deltaW /= batch_size
					self.apply_deltas(learning_rate)

			if len(X) % batch_size > 0:
				mod = len(X) % batch_size
				self.deltaU /= mod
				self.deltaV /= mod
				self.deltaW /= mod
				self.apply_deltas(learning_rate)

			loss = sum([self.compute_loss(X_dev[i], D_dev[i]) for i in range(len(X_dev))])/loss_sum

			if log:
				stdout.write("\tepoch done in %.02f seconds" % (time.time() - t0))
				stdout.write("\tnew loss: {0}".format(loss))
				stdout.flush()

			if loss < best_loss:
				best_loss = loss
				bestU, bestV, bestW = self.U.copy(), self.V.copy(), self.W.copy()
				best_epoch = epoch

			# make sure we change the RNN enough
			if abs(prev_loss - loss) < min_change:
				min_change_count += 1
			else:
				min_change_count = 0
			if min_change_count > 2:
				print("\n\ntraining finished after {0} epochs due to minimal change in loss".format(epoch+1))
				break

			prev_loss = loss

		t = time.time() - t_start

		if min_change_count <= 2:
			print("\n\ntraining finished after reaching maximum of {0} epochs".format(epochs))
		print("best observed loss was {0}, at epoch {1}".format(best_loss, (best_epoch+1)))

		print("setting U, V, W to matrices from best epoch")
		self.U, self.V, self.W = bestU, bestV, bestW

		return best_loss

if __name__ == "__main__":
	import sys
	from utils import *
	mode = sys.argv[1].lower()

	if mode == "estimate":
		'''
		starter code for parameter estimation.
		'''

		''' do NOT change until "your code here" '''
		data_folder = sys.argv[2]
		vocab_size = 2000
		train_size = 1000
		dev_size = 1000

		# get the data set vocabulary
		vocab = pd.read_table(data_folder + "/vocab.ptb.txt", header=None, sep="\s+", index_col=0, names=['count', 'freq'], )
		num_to_word = dict(enumerate(vocab.index[:vocab_size]))
		word_to_num = invert_dict(num_to_word)

		# calculate loss vocabulary words due to vocab_size
		fraction_lost = fraq_loss(vocab, word_to_num, vocab_size)
		print("Retained %d words from %d (%.02f%% of all tokens)\n" % (vocab_size, len(vocab), 100*(1-fraction_lost)))

		docs = load_dataset(data_folder + '/ptb-train.txt')
		S_train = docs_to_indices(docs, word_to_num)
		X_train, D_train = seqs_to_lmXY(S_train)

		# Load the dev set (for tuning hyperparameters)
		docs = load_dataset(data_folder + '/ptb-dev.txt')
		S_dev = docs_to_indices(docs, word_to_num)
		X_dev, D_dev = seqs_to_lmXY(S_dev)

		X = X_train[:train_size]
		D = D_train[:train_size]
		X_dev = X_dev[:dev_size]
		D_dev = D_dev[:dev_size]

		# q = best unigram frequency from omitted vocab
		# this is the best expected loss out of that set
		q = vocab.freq[vocab_size] / sum(vocab.freq[vocab_size:])

		best_loss = 100
		best_hdim = -1
		best_lookback = -1
		best_lr = -1
		h_dims=[25,50,75,100]
		lookbacks = [0,2,5,8,10]
		lrs=[0.5,0.1,0.05,0.01]
		for h_dim in h_dims:
			for lr in lrs:
				for lookback in lookbacks:
					a = RNN(vocab_size=2000, hidden_dims=h_dim)
					loss_found = a.train(X, D, X_dev, D_dev, epochs=10, learning_rate=lr, anneal=5, back_steps=lookback, batch_size=100, min_change=0.0001, log=True)
					if loss_found < best_loss:
						best_loss = loss_found
						best_hdim = h_dim
						best_lr = lr
						best_lookback = lookback

		best_params = [best_hdim,best_lookback,best_lr]
		print("\nestimation finished.\n\tbest loss {0}:\tbest hidden/lookback/learn rate: {1}/{2}/{3}".format(best_loss, best_params[0], best_params[1], best_params[2]))

	if mode == "train":
		'''
		starter code for parameter estimation.
		change this to different values, or use it to get you started with your own testing class
		'''

		data_folder = sys.argv[2]
		train_size = 25000
		dev_size = 1000
		vocab_size = 2000

		hdim = int(sys.argv[3])
		lookback = int(sys.argv[4])
		lr = float(sys.argv[5])

		# get the data set vocabulary
		vocab = pd.read_table(data_folder + "/vocab.ptb.txt", header=None, sep="\s+", index_col=0, names=['count', 'freq'], )
		num_to_word = dict(enumerate(vocab.index[:vocab_size]))
		word_to_num = invert_dict(num_to_word)

		# calculate loss vocabulary words due to vocab_size
		fraction_lost = fraq_loss(vocab, word_to_num, vocab_size)
		print("Retained %d words from %d (%.02f%% of all tokens)\n" % (vocab_size, len(vocab), 100*(1-fraction_lost)))

		docs = load_dataset(data_folder + '/ptb-train.txt')
		S_train = docs_to_indices(docs, word_to_num)
		X_train, D_train = seqs_to_lmXY(S_train)

		# Load the dev set (for tuning hyperparameters)
		docs = load_dataset(data_folder + '/ptb-dev.txt')
		S_dev = docs_to_indices(docs, word_to_num)
		X_dev, D_dev = seqs_to_lmXY(S_dev)

		X_train = X_train[:train_size]
		D_train = D_train[:train_size]
		X_dev = X_dev[:dev_size]
		D_dev = D_dev[:dev_size]

		# q = best unigram frequency from omitted vocab
		# this is the best expected loss out of that set
		q = vocab.freq[vocab_size] / sum(vocab.freq[vocab_size:])
		#train the model and save the parameters
		a = RNN(vocab_size, hidden_dims=hdim)
		loss_found = a.train(X_train, D_train, X_dev, D_dev, epochs=10, learning_rate=lr, anneal=5, back_steps=lookback, batch_size=100, min_change=0.0001, log=True)
		np.save('parameters/rnn.U.npy', a.U)
		np.save('parameters/rnn.V.npy', a.V)
		np.save('parameters/rnn.W.npy', a.W)
		#evaluate on the full dev set
		docs = load_dataset(data_folder + '/ptb-dev.txt')
		S_dev = docs_to_indices(docs, word_to_num)
		X_dev, D_dev = seqs_to_lmXY(S_dev)
		mean_loss = a.compute_mean_loss(X_dev, D_dev)
		run_loss = mean_loss
		adjusted_loss = adjust_loss(run_loss, fraction_lost, q)

		print "Unadjusted: %.03f" % np.exp(run_loss)
		print "Adjusted for missing vocab: %.03f" % np.exp(adjusted_loss)

	if mode == "generate":
		'''
		starter code for sequence generation
		'''
		data_folder = sys.argv[2]
		rnn_folder = sys.argv[3]
		maxLength = int(sys.argv[4])

		# get saved RNN matrices and setup RNN
		U,V,W = np.load(rnn_folder + "/rnn.U.npy"), np.load(rnn_folder + "/rnn.V.npy"), np.load(rnn_folder + "/rnn.W.npy")
		vocab_size = len(V[0])
		hdim = len(U[0])

		r = RNN(vocab_size, hdim)
		r.U = U
		r.V = V
		r.W = W

		# get vocabulary
		vocab = pd.read_table(data_folder + "/vocab.ptb.txt", header=None, sep="\s+", index_col=0, names=['count', 'freq'], )
		num_to_word = dict(enumerate(vocab.index[:vocab_size]))
		word_to_num = invert_dict(num_to_word)

		##########################
		# --- your code here --- #
		##########################

		# predict something
		s,l = r.generate_sequence(word_to_num['<s>'], word_to_num['</s>'], maxLength)
		sentence = ""
		for word in s[1:-1]:
			sentence += " " + num_to_word[word]
		mean_loss = l/float(len(s)-1)
		perplexity = np.exp(mean_loss)
		print('{0} : {1}'.format(sentence[1:], perplexity))


text:
from IMLearn.utils import split_train_test
from IMLearn.learners.regressors import LinearRegression
from IMLearn.metrics.loss_functions import mean_square_error

from typing import NoReturn
import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
pio.templates.default = "simple_white"

import matplotlib.pyplot as plt
from IMLearn.utils.utils import split_train_test

def load_data(filename: str):
    """
    Load house prices dataset and preprocess data.
    Parameters
    ----------
    filename: str
        Path to house prices dataset

    Returns
    -------
    Design matrix and response vector (prices) - either as a single
    DataFrame or a Tuple[DataFrame, Series]
    """
    df = pd.read_csv(filename)
    print(df.columns)

    wanted_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'view',
                       'condition', 'grade', 'sqft_above', 'sqft_living15', 'sqft_lot15']
    predicted_label = 'price'
    processed_df = df[wanted_features].fillna(0)
    return processed_df, pd.Series(df[predicted_label])

def feature_evaluation(X: pd.DataFrame, y: pd.Series, output_path: str = "C:/Users/t8851692/PycharmProjects/IML.HUJI/exercises/datafigures/") -> NoReturn:
    """
    Create scatter plot between each feature and the response.
        - Plot title specifies feature name
        - Plot title specifies Pearson Correlation between feature and response
        - Plot saved under given folder with file name including feature name
    Parameters
    ----------
    X : DataFrame of shape (n_samples, n_features)
        Design matrix of regression problem

    y : array-like of shape (n_samples, )
        Response vector to evaluate against

    output_path: str (default ".")
        Path to folder in which plots are saved
    """
    plot_features_to_price(X, output_path, y)
    calculate_pearson_correlation(X, y, output_path)

def calculate_pearson_correlation(X, y, output_path):
    def pearson_correlation(x, y):
        current_pearson_correlation = np.cov(x, y, rowvar=True) / (np.std(x) * np.std(y))
        # returns 2*2 matrix
        return current_pearson_correlation[0, 1]

    pearson_correlations_list = [pearson_correlation(X[X.columns[i]], y) for i in range(len(X.columns))]
    print(pearson_correlations_list)

    pearson_correlations_list = pearson_correlations_list
    # Creating histogram
    fig, ax = plt.subplots(1, 1)
    ax.hist(pearson_correlations_list, bins=len(X.columns))

    # Set title
    ax.set_title(f"pearson_correlations between features and price")

    # adding labels
    ax.set_xlabel('features')
    ax.set_ylabel('pearson correlations values')

    # Make some labels.
    rects = ax.patches
    labels = [X[X.columns[i]].name[:8] for i in range(len(rects))]
    print(labels)

    for rect, label in zip(rects, labels):
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width() / 2, height + 0.01, label,
                ha='center', va='bottom')

    plt.savefig(output_path + f'pearson_correlations.png')
    plt.show()

def plot_features_to_price(X, output_path, y):
    for index, column in enumerate(X.columns):
        if is_numeric_dtype(X[column]):
            x = X[column]
            plt.scatter(x, y)
            plt.savefig(output_path + f'fig_{X.columns[index]}.png')
            plt.title(f"{X.columns[index]}")
            plt.clf()

def activate_linear_regressor(X_train, Y_train, X_test, Y_test):
    iterations = 10
    mean_list, std_list = [], []
    model = LinearRegression()
    for percent in (range(10, 101)):
        data_length = int(len(X_train) * percent / 100)
        current_performance = []
        for _ in range(iterations):
            data = (pd.concat([X_train, Y_train], axis=1)).sample(data_length)
            train = data.drop(['price'], axis=1)
            test = data['price']
            model.fit(train, test)
            current_performance.append(model.loss(X_test, Y_test))
        mean_list.append(np.mean(current_performance))
        std_list.append(2 * np.std(current_performance))
    return list(range(10, 101)), mean_list, std_list

def plot_linear_regressor_performance(percentages_list, mean_list, std_list):
    plt.plot(percentages_list, mean_list, yerr=std_list)
    plt.show()

if __name__ == '__main__':
    np.random.seed(0)
    # Question 1 - Load and preprocessing of housing prices dataset
    X, y = load_data(r"C:\Users\t8851692\PycharmProjects\IML.HUJI\datasets\house_prices.csv")

    # Question 2 - Feature evaluation with respect to response
    feature_evaluation(X, y)
    exit()
    # Question 3 - Split samples into training- and testing sets.
    X_train, Y_train, X_test, Y_test = split_train_test(X, y)

    # Question 4 - Fit model over increasing percentages of the overall training data
    percentage, mean_list, std_list = activate_linear_regressor(X_train, Y_train, X_test, Y_test)
    plot_linear_regressor_performance(percentage, mean_list, std_list)

ext: py

reason: import numpy


text:
import cv2
import numpy as np
from keras.models import load_model
from keras.datasets import cifar10

# Image dimension
img_row, img_height, img_depth = 32,32,3

# Load trained Model
# 10 neurons
# 1000 epochs
classifier = load_model('./model/cifar_model.h5')

# Loads the CIFAR dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
color = True
scale = 8

# Images

def draw_test(name, res, input_im, scale, img_row, img_height):
    BLACK = [0,0,0]
    res = int(res)
    if res == 0:
        pred = "airplane"
    if res == 1:
        pred = "auto"
    if res == 2:
        pred = "bird"
    if res == 3:
        pred = "cat"
    if res == 4:
        pred = "deer"
    if res == 5:
        pred = "dog"
    if res == 6:
        pred = "frog"
    if res == 7:
        pred = "horse"
    if res == 8:
        pred = "ship"
    if res == 9:
        pred = "truck"

    expanded_image = cv2.copyMakeBorder(input_im, 0, 0, 0, imageL.shape[0]*2 ,cv2.BORDER_CONSTANT,value=BLACK)
    if color == False:
        expanded_image = cv2.cvtColor(expanded_image, cv2.COLOR_GRAY2BGR)
    cv2.putText(expanded_image, str(pred), (300, 80) , cv2.FONT_HERSHEY_COMPLEX_SMALL,4, (0,0,255), 2)
    cv2.imshow(name, expanded_image)

for i in range(0,10):
    rand = np.random.randint(0,len(x_test))
    input_im = x_test[rand]
    imageL = cv2.resize(input_im, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
    input_im = input_im.reshape(1,img_row, img_height, img_depth)

    # Get Prediction
    res = str(classifier.predict_classes(input_im, 1, verbose = 0)[0])

    draw_test("Prediction", res, imageL, scale, img_row, img_height)
    cv2.waitKey(0)

cv2.destroyAllWindows()


text:
from optparse import OptionParser
import os

import numpy as np
import pandas as pd

if __name__ == '__main__':

    parser = OptionParser()
    parser.add_option("--transcripts", dest="TRANSCRIPTS", help="file with transcript sequences")
    parser.add_option("--indir", dest="INDIR", help="folder with RNAplfold outputs")
    parser.add_option("--outdir", dest="OUTDIR", help="folder for writing outputs")

    (options, args) = parser.parse_args()

    transcripts = pd.read_csv(options.TRANSCRIPTS, sep='\t', index_col='transcript')

    for row in transcripts.iterrows():
        transcript_length = row[1]['orf_length'] + row[1]['utr3_length']

        # read in rnaplfold outputs
        lunp_file = os.path.join(options.INDIR, row[0]) + '_lunp'
        rnaplfold_data = pd.read_csv(lunp_file, sep='\t', header=1)
        rnaplfold_data = rnaplfold_data[rnaplfold_data.columns[:15]]

        # use parameters from Agarwal et al., 2015
        rnaplfold_data.columns = ['end'] + list(np.arange(14) + 1)
        rnaplfold_data = rnaplfold_data.set_index('end').astype(float)

        for ix in range(13):
            temp = rnaplfold_data.loc[ix+1]
            rnaplfold_data.loc[ix+1] = rnaplfold_data.loc[ix+1].fillna(temp[ix + 1])

        new_row = pd.Series({14: rnaplfold_data.iloc[-1][13]}, name=transcript_length+1)
        rnaplfold_data = rnaplfold_data[[14]].append(new_row)
        assert(len(rnaplfold_data) == len(rnaplfold_data.dropna()))

        # write to outfile
        outfile = os.path.join(options.OUTDIR, f'{row[0]}.txt')
        rnaplfold_data.to_csv(outfile, sep='\t')


text:
import numpy as np
import math
from scipy.optimize import curve_fit

def calc_lorentzian(CestCurveS, x_calcentires, mask, config):
    (rows, colums, z_slices, entires) = CestCurveS.shape

    lorenzian = {key: np.zeros((rows, colums, z_slices), dtype=float) for key in config.lorenzian_keys}
    for k in range(z_slices):
        for i in range(rows):
            for j in range(colums):
                if mask[i, j, k] != 0:
                    params = calc_lorenzian_pixel(CestCurveS[i, j, k, :], x_calcentires, config.Lorenzian['MT_f'],
                                                  config.Lorenzian['NOE1_f'], config.Lorenzian['NOE2_f'],
                                                  config.Lorenzian['OH_f'], config.Lorenzian['NH_f'])
                    if params is None:
                        continue
                    dic = {
                        'OH_a': params[3],
                        'OH_w': params[4],
                        'NH_a': params[5],
                        'NH_w': params[6],
                        'NOE1_a': params[7],
                        'NOE1_w': params[8],
                        'NOE2_a': params[9],
                        'NOE2_w': params[10],
                        'MT_a': params[11],
                        'MT_w': params[12],
                    }
                    for key in config.lorenzian_keys:
                        lorenzian[key][i, j, k] = dic[key]
    return lorenzian

def calc_lorenzian_pixel(values, x_calcentires, MT_f, NOE1_f, NOE2_f, OH_f, NH_f):
    # wassr_offset, da die Z-Spektren vorher korrigiert wurden
    fit = lorenz_like_matlab(wassr_offset=0, MT_f=MT_f, NOE1_f=NOE1_f, NOE2_f=NOE2_f, OH_f=OH_f, NH_f=NH_f)
    try:
        param, param_cov = curve_fit(fit, x_calcentires, values, bounds=([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                                                         [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                                                          10,
                                                                          10]))
    except RuntimeError:
        param = None

    return param

def lorenz_like_matlab(wassr_offset, MT_f: float = - 2.43, NOE1_f: float = - 1, NOE2_f: float = - 2.6,
                       OH_f: float = + 1.4, NH_f: float = + 3.2):
    # X_f = frequenz of X
    #ret = (a + ak) - (a * ((b ** 2) / 4) / (((b ** 2) / 4) + (x - wassr_offset) ** 2))
    pass

def one_lorenz(x, amplitude, width, wassr_offset, frequenz):
    return amplitude * ((width ** 2) / 4) / (((width ** 2) / 4) + (x - (wassr_offset + frequenz)) ** 2)

max_stars_repo_name: ludgerradke/bMRI

max_issues_repo_name: ludgerradke/bMRI

max_forks_repo_name: ludgerradke/bMRI

reason: import numpy,from scipy


text:
import nibabel as nib
import logging
import numpy as np
import math
from scipy.stats.mstats import zscore
from brainiak.fcma.classifier import Classifier
from sklearn.externals import joblib
import time

logger = logging.getLogger(__name__)

class SingleTRProcessor:
    def __init__(self, epoch_file, mask_file, model_file, total, window):
        self.epoch = np.load(epoch_file)
        self.mask = nib.load(mask_file).get_data()
        self.total = total
        self.window = window
        self.num_voxels = len(np.where(self.mask > 0)[0])
        self.raw_data = np.zeros((self.total, self.num_voxels), np.float32, order='C')
        self.current_epoch = -1
        time1 = time.time()
        self.clf = joblib.load(model_file)
        assert isinstance(self.clf, Classifier), \
                'the loaded classifier is incorrect'
        time2 = time.time()
        logger.info(
            'model loading done, takes: %.2f s' %
            (time2 - time1)
        )
        logger.debug(
                '%d voxels per brain in the classifier, ' \
                '%d training samples involved' %
                (self.clf.num_voxels_, self.clf.num_samples_)
                )
        logger.info(
                'Single TR processor initialized, '\
                'ready to perform the real-time classification'
                )

    def _prepare_data(self, start_tr, end_tr):
        # normalize the data
        data = np.copy(self.raw_data[start_tr: end_tr + 1])
        data = zscore(data, axis=0, ddof=0)
        # if zscore fails (standard deviation is zero),
        # set all values to be zero
        data = np.nan_to_num(data)
        data = data / math.sqrt(data.shape[0])
        return data

    def process_single_tr_nifti(self, nifti_file, tr_count):
        # read
        data = nib.load(nifti_file).get_data()
        # apply mask and accumulate data
        self.raw_data[tr_count, :] = np.copy(data[self.mask > 0])
        # compute correlation if needed
        if self.current_epoch == -1:
            if self.epoch[tr_count] == 1:
                self.current_epoch = tr_count
        elif self.epoch[tr_count] == 0:
            self.current_epoch = -1
        elif tr_count - self.current_epoch + 1 >= self.window:
            data = self._prepare_data(self.current_epoch,
                                     tr_count)
            # predict, calling BrainIAK
            y_pred = self.clf.predict([data])
            confidence = self.clf.decision_function([data])
            logger.info(
                        'predicted: %d with confidence %.2f, growing window, window size: %d' %
                        (y_pred[0], abs(confidence), tr_count + 1 - self.current_epoch)
                       )
            data = self._prepare_data(tr_count - self.window + 1,
                                      tr_count)
            # predict, calling BrainIAK
            y_pred = self.clf.predict([data])
            confidence = self.clf.decision_function([data])
            logger.info(
                        'predicted: %d with confidence %.2f, sliding window, window size: %d' %
                        (y_pred[0], abs(confidence), self.window)
                       )
        return

max_stars_repo_name: Brainiak/rtcloud

max_issues_repo_name: Brainiak/rtcloud

max_forks_repo_name: Brainiak/rtcloud


text:
"""
This is incomplete
"""

import numpy as np

from sklearn.ensemble.forest import BaseForest
from sklearn.tree.tree import BaseDecisionTree
from sklearn.ensemble.weight_boosting import AdaBoostClassifier
from sklearn.tree._tree import DTYPE
from sklearn.utils import check_X_y

class AdaBoostAbstainingClassifier(AdaBoostClassifier):
    def __init__(self,
                 base_estimator=None,
                 n_estimators=50,
                 learning_rate=1.,
                 algorithm='SAMME.R',
                 random_state=None):

        super(AdaBoostAbstainingClassifier, self).__init__(
            base_estimator=base_estimator,
            n_estimators=n_estimators,
            learning_rate=learning_rate,
            algorithm=algorithm,
            random_state=random_state)

    def fit(self, X, y, sample_weight=None):
        """Build a boosted classifier from the training set (X, y).
        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. DOK and LIL are converted to CSR.
        y : array-like of shape = [n_samples]
            The target values (class labels).
        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            ``1 / n_samples``.
        Returns
        -------
        self : object
            Returns self.
        """
        # Check that algorithm is supported
        if self.algorithm not in ('SAMME', 'SAMME.R'):
            raise ValueError("algorithm %s is not supported" % self.algorithm)

        # Fit
                # Check parameters
        if self.learning_rate <= 0:
            raise ValueError("learning_rate must be greater than zero")

        if (self.base_estimator is None or
                isinstance(self.base_estimator, (BaseDecisionTree,
                                                 BaseForest))):
            dtype = DTYPE
            accept_sparse = 'csc'
        else:
            dtype = None
            accept_sparse = ['csr', 'csc']

        X, y = check_X_y(X, y, accept_sparse=accept_sparse,
                         force_all_finite = False, dtype=dtype)

        if sample_weight is None:
            abstainrows = np.isnan(X).sum(axis=1) > 0
            # Initialize weights to 1 / n_samples
            sample_weight = np.empty(X.shape[0], dtype=np.float)
            sample_weight[:] = 1.
            sample_weight[abstainrows] = 0.

        # Normalize existing weights
        sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)

        # Check that the sample weights sum is positive
        if sample_weight.sum() <= 0:
            raise ValueError(
                "Attempting to fit with a non-positive "
                "weighted number of samples.")

        # Check parameters
        self._validate_estimator()

        # Clear any previous fit results
        self.estimators_ = []
        self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float)
        self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float)

        for iboost in range(self.n_estimators):
            # Boosting step
            sample_weight, estimator_weight, estimator_error = self._boost(
                iboost,
                X, y,
                sample_weight)

            # Early termination
            if sample_weight is None:
                break

            self.estimator_weights_[iboost] = estimator_weight
            self.estimator_errors_[iboost] = estimator_error

            # Stop if error is zero
            if estimator_error == 0:
                break

            sample_weight_sum = np.sum(sample_weight)

            # Stop if the sum of sample weights has become non-positive
            if sample_weight_sum <= 0:
                break

            if iboost < self.n_estimators - 1:
                # Normalize
                sample_weight /= sample_weight_sum

        return self

    def _boost_real(self, iboost, X, y, sample_weight):
        raise NotImplementedError

    def _boost_discrete(self, iboost, X, y, sample_weight):
        """Implement a single boost using the SAMME discrete algorithm."""
        estimator = self._make_estimator()

        try:
            estimator.set_params(random_state=self.random_state)
        except ValueError:
            pass

        abstaining = np.isnan(X).sum(axis=1) > 0

        X_trans = X[~abstaining]
        y_trans = y[~abstaining]
        sw_trans = sample_weight[~abstaining]
        estimator.fit(X_trans, y_trans, sample_weight=sw_trans)

        y_predict = estimator.predict(X_trans)

        if iboost == 0:
            self.classes_ = getattr(estimator, 'classes_', None)
            self.n_classes_ = len(self.classes_)

        # Instances classified
        incorrect = np.zeros(y.shape)
        incorrect[~abstaining] = y_predict != y_trans
        correct = np.zeros(y.shape)
        correct[~abstaining] = y_predict == y_trans

        n_classes = self.n_classes_

        # incorrect, correct, abstaining weights
        Wi = np.sum(incorrect) / X.shape[0]
        Wc = np.sum(correct) / X.shape[0]
        Wa = np.sum(abstaining) / X.shape[0]
        Z = Wa + 2. * np.sqrt(Wc/Wi)

        # Stop if classification is perfect
        if estimator_error <= 0 and abstaining.sum() == 0:
            return sample_weight, 1., 0.

        # Stop if the error is at least as bad as random guessing
        if estimator_error >= 1. - (1. / n_classes):
            self.estimators_.pop(-1)
            if len(self.estimators_) == 0:
                raise ValueError('BaseClassifier in AdaBoostClassifier '
                                 'ensemble is worse than random, ensemble '
                                 'can not be fit.')
            return None, None, None

        # Boost weight using multi-class AdaBoost SAMME alg
        estimator_weight = self.learning_rate * (
            np.log(Wc/Wi) +
            np.log(n_classes - 1.))

        # Only boost the weights if I will fit again
        if not iboost == self.n_estimators - 1:
            # Only boost positive weights
            sample_weight *= np.exp(estimator_weight * incorrect *
                                    ((sample_weight > 0) |
                                     (estimator_weight < 0)))

        return sample_weight, estimator_weight, estimator_error

max_stars_repo_name: ldamewood/renormalization

max_issues_repo_name: chrinide/renormalization

max_forks_repo_name: chrinide/renormalization


text:
"""Creates NFC time bound marker datasets from clip HDF5 files."""

from collections import defaultdict
from pathlib import Path
import time

import h5py
import numpy as np
import tensorflow as tf

import vesper.util.os_utils as os_utils
import vesper.signal.resampling_utils as resampling_utils

DATASET_SIZES = {
    'Training': None,
    'Validation': .1
}

CALL_TYPE = 'Tseep'

DATA_DIR_PATH = Path(
    '/Users/harold/Desktop/NFC/Data/Vesper ML/'
    'NFC Bounding Interval Annotator 1.0/')

INPUT_DIR_PATH = DATA_DIR_PATH / 'HDF5 Files' / CALL_TYPE

THRUSH_INPUT_FILE_NAMES = ''

TSEEP_INPUT_FILE_NAMES = '''
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Angela.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Bear.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Bell Crossing.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Darby.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Dashiell.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Davies.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Deer Mountain.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Floodplain.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Florence.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_KBK.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Lilo.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Nelson.h5
Tseep_2017 MPG Ranch 30k_Old Bird Redux 1.1_Powell.h5
'''.strip().split('\n')

INPUT_FILE_NAMES = {
    'Thrush': THRUSH_INPUT_FILE_NAMES,
    'Tseep': TSEEP_INPUT_FILE_NAMES,
}[CALL_TYPE]

OUTPUT_SAMPLE_RATE = 24000

OUTPUT_DIR_PATH = DATA_DIR_PATH / 'Datasets' / CALL_TYPE
OUTPUT_FILE_NAME_FORMAT = '{}_{}.tfrecords'

def main():

    dataset_sizes = defaultdict(int)

    for input_file_name in INPUT_FILE_NAMES:

        # Get clip IDs from input file.
        input_file_path = INPUT_DIR_PATH / input_file_name
        file_ = h5py.File(input_file_path, 'r')
        clip_group = file_['clips']
        clip_ids = list(clip_group.keys())

        dataset_clip_ids = get_dataset_clip_ids(DATASET_SIZES, clip_ids)

        # Create tfrecord files.
        file_name_prefix = input_file_path.stem
        for dataset_name, clip_ids in dataset_clip_ids.items():
            create_tfrecord_file(
                file_name_prefix, dataset_name, clip_group, clip_ids)
            dataset_sizes[dataset_name] += len(clip_ids)

    show_dataset_sizes(dataset_sizes)

def get_dataset_clip_ids(dataset_sizes, clip_ids):

    # Make sure all datasets have sizes.
    dataset_sizes = complete_dataset_sizes(dataset_sizes)

    # Shuffle clip IDs.
    clip_ids = np.random.permutation(clip_ids)

    dataset_clip_ids = {}
    clip_count = len(clip_ids)
    start_index = 0

    for name, fraction in dataset_sizes.items():

        end_index = start_index + int(round(fraction * clip_count))

        dataset_clip_ids[name] = clip_ids[start_index:end_index]

        start_index = end_index

    return dataset_clip_ids

def complete_dataset_sizes(sizes):
    fraction_sum = sum(s for s in sizes.values() if s is not None)
    return dict(
        (name, complete_dataset_size(fraction, fraction_sum))
        for name, fraction in sizes.items())

def complete_dataset_size(fraction, fraction_sum):
    if fraction is None:
        return 1 - fraction_sum
    else:
        return fraction

def create_tfrecord_file(file_name_prefix, dataset_name, clip_group, clip_ids):

    start_time = time.time()

    file_path = get_output_file_path(file_name_prefix, dataset_name)

    print(f'Writing {len(clip_ids)} clips to file "{file_path}"...')
    examples = create_tf_examples(clip_group, clip_ids)

    os_utils.create_parent_directory(file_path)
    with tf.io.TFRecordWriter(str(file_path)) as writer:
        for example in examples:
            writer.write(example.SerializeToString())

    end_time = time.time()
    delta = end_time - start_time
    rate = len(clip_ids) / delta
    print(
        f'    Wrote {len(clip_ids)} {dataset_name} clips in {delta:.1f} '
        f'seconds, a rate of {rate:.1f} clips per second.')

def create_tf_examples(clip_group, clip_ids):
    return [create_tf_example(clip_group, i) for i in clip_ids]

def create_tf_example(clip_group, clip_id):

    clip = clip_group[clip_id]

    (waveform, clip_start_index, clip_end_index, call_start_index,
        call_end_index) = get_clip_data(clip)

    clip_id_feature = create_int64_feature(int(clip_id))
    waveform_feature = create_bytes_feature(waveform.tobytes())
    clip_start_index_feature = create_int64_feature(clip_start_index)
    clip_end_index_feature = create_int64_feature(clip_end_index)
    call_start_index_feature = create_int64_feature(call_start_index)
    call_end_index_feature = create_int64_feature(call_end_index)

    features = tf.train.Features(
        feature={
            'clip_id': clip_id_feature,
            'waveform': waveform_feature,
            'clip_start_index': clip_start_index_feature,
            'clip_end_index': clip_end_index_feature,
            'call_start_index': call_start_index_feature,
            'call_end_index': call_end_index_feature,
        })

    return tf.train.Example(features=features)

def get_clip_data(clip):

    waveform = clip[:]

    attrs = clip.attrs

    extraction_start_index = attrs['extraction_start_index']
    clip_start_index = attrs['clip_start_index'] - extraction_start_index
    clip_end_index = clip_start_index + attrs['clip_length']
    call_start_index = attrs['call_start_index'] - extraction_start_index
    call_end_index = attrs['call_end_index'] - extraction_start_index

    # Resample if needed.
    sample_rate = attrs['sample_rate']
    if sample_rate != OUTPUT_SAMPLE_RATE:
        waveform = resampling_utils.resample_to_24000_hz(waveform, sample_rate)
        clip_start_index = adjust_index(
            clip_start_index, sample_rate, OUTPUT_SAMPLE_RATE)
        clip_end_index = adjust_index(
            clip_end_index, sample_rate, OUTPUT_SAMPLE_RATE)
        call_start_index = adjust_index(
            call_start_index, sample_rate, OUTPUT_SAMPLE_RATE)
        call_end_index = adjust_index(
            call_end_index, sample_rate, OUTPUT_SAMPLE_RATE)

    return (
        waveform, clip_start_index, clip_end_index, call_start_index,
        call_end_index)

def adjust_index(index, old_sample_rate, new_sample_rate):
    return int(round(index * (new_sample_rate / old_sample_rate)))

def create_bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def create_int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def get_output_file_path(file_name_prefix, dataset_name):

    file_name = OUTPUT_FILE_NAME_FORMAT.format(
        file_name_prefix, dataset_name)

    return OUTPUT_DIR_PATH / dataset_name / file_name

def show_dataset_sizes(dataset_sizes):
    print('Dataset sizes:')
    for name in sorted(dataset_sizes.keys()):
        size = dataset_sizes[name]
        print(f'    {name}: {size}')

if __name__ == '__main__':
    main()

max_stars_repo_name: RichardLitt/Vesper

max_issues_repo_name: Tubbz-alt/Vesper

max_forks_repo_name: Tubbz-alt/Vesper


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

import numpy as np
import pytest

import cunumeric as num

N = 8000

DTYPES = [np.int64, np.int32, np.int16]

@pytest.mark.parametrize("dtype", DTYPES)
def test_bincount_basic(dtype):
    v_num = num.random.randint(0, 9, size=N, dtype=dtype)

    v_np = v_num.__array__()

    out_np = np.bincount(v_np)
    out_num = num.bincount(v_num)
    assert num.array_equal(out_np, out_num)

@pytest.mark.parametrize("dtype", DTYPES)
def test_bincount_weights(dtype):
    v_num = num.random.randint(0, 9, size=N, dtype=dtype)
    w_num = num.random.randn(N)

    v_np = v_num.__array__()
    w_np = w_num.__array__()

    out_np = np.bincount(v_np, weights=w_np)
    out_num = num.bincount(v_num, weights=w_num)
    assert num.allclose(out_np, out_num)

if __name__ == "__main__":
    import sys

    pytest.main(sys.argv)

max_stars_repo_name: mferreravila/cunumeric

max_issues_repo_name: mferreravila/cunumeric

max_forks_repo_name: mferreravila/cunumeric