text:
# General stuff:
import sys
import os
import numpy as np
import math
import pandas as pd
from datetime import date
from tqdm import tqdm_notebook
from scipy.stats import pearsonr

# Plotting:
from matplotlib import pyplot as plt
import seaborn as sns
import matplotlib.cm as cm
import matplotlib as mpl

def convert_size(size_bytes):
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    p = math.pow(1024, i)
    s = round(size_bytes / p, 2)
    return "%s %s" % (s, size_name[i])

### PLOTTING FUNCTIONS #################################################################################

def fake_ratemap(nbins=40):
    fake_ratemap = np.ones((nbins,nbins))
    masked_ratemap = np.ma.masked_where(fake_ratemap == 1, fake_ratemap)
    return masked_ratemap

def plot_comparisons(scores_df,score,ylabel,separator,figure_size):
    figure = plt.figure(figsize=figure_size,facecolor='w')
    sns.set(font_scale=1.)
    sns.set_style('white')

    ax1 = figure.add_subplot(2,1,1)
    # Draw a nested boxplot to show bills by day and sex
    ax1.axhline(y=0,zorder=0,color='k')
    sns.violinplot(x=separator, y=score, hue='excited',data=scores_df, palette="Greys",split=True,
                bw=.3,axes=ax1)
    sns.stripplot(x=separator, y=score, hue='excited',data=scores_df, palette="Reds",split=True,
                axes=ax1,jitter=.1,alpha=.5)
    ax1.set_title('{}'.format(ylabel),y=1.05)
    ax1.set_ylabel(ylabel)
    ax1.set_xlabel('')

    handles, labels = ax1.get_legend_handles_labels()
    l = plt.legend(handles[2:4], labels[2:4],loc=1, borderaxespad=0.)

    # do the same for inhibited cells
    ax2 = figure.add_subplot(2,1,2)
    # Draw a nested boxplot to show bills by day and sex
    ax2.axhline(y=0,zorder=0,color='k')
    sns.violinplot(x=separator, y=score, hue='inhibited',data=scores_df, palette="Greys",split=True,
                bw=.3,axes=ax2)
    sns.stripplot(x=separator, y=score, hue='inhibited',data=scores_df, palette="Blues",split=True,
                axes=ax2,jitter=.1,alpha=.5)
    ax2.set_ylabel(ylabel)
    ax2.set_xlabel('')

    handles, labels = ax2.get_legend_handles_labels()
    l = plt.legend(handles[2:4], labels[2:4], loc=1, borderaxespad=0.)
    sns.despine(offset=0, trim=True)
    plt.show()

# draw spikeplots
def draw_path_spike_plots(df,column_names,number_plots,speed_cutoff=5.,offset=0,plot_hd=False, clr='r'):

    '''
    Input:
    - dataframe (df)
    - column names: [tracking_column_name,spiketimes_tracking_column_name]
    - number_plots
    - speed_cutoff (if np.nan: skipped!)
    - offset
    - plot_hd (boolean)
    '''
    figure = plt.figure(figsize=(25,25),facecolor='w')
    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset

        current_tracking = df.iloc[row][column_names[0]]
        current_spiket_tracking = df.iloc[row][column_names[1]]

        if np.isnan(speed_cutoff):
            tracking_x = current_tracking['correct_x_inter']
            tracking_y = current_tracking['correct_y_inter']
            spiket_tracking_x = current_spiket_tracking['correct_x_inter']
            spiket_tracking_y = current_spiket_tracking['correct_y_inter']
            if plot_hd: head_angle_ = (current_spiket_tracking.head_angle.values/(2*np.pi))*255
            if row == 0 + offset: print('No speed filter active!')

        else:
            tracking_x = current_tracking['correct_x_inter'][current_tracking['speed_filtered']>speed_cutoff]
            tracking_y = current_tracking['correct_y_inter'][current_tracking['speed_filtered']>speed_cutoff]
            spiket_tracking_x = current_spiket_tracking['correct_x_inter'][(current_spiket_tracking['speed_filtered']>speed_cutoff)]
            spiket_tracking_y = current_spiket_tracking['correct_y_inter'][(current_spiket_tracking['speed_filtered']>speed_cutoff)]
            if plot_hd: head_angle_ = (current_spiket_tracking.head_angle[(current_spiket_tracking['speed_filtered']>speed_cutoff)].values/(2*np.pi))*255

        try:
            ax = figure.add_subplot(5,5,row-offset+1)

            if not plot_hd:
                ax.plot(tracking_x,tracking_y,color=[.6, .6, .6],linewidth=.7,zorder=1)
                ax.scatter(spiket_tracking_x,spiket_tracking_y, color=clr,s=10,alpha=1, zorder=2)
            else:
                ax.plot(tracking_x,tracking_y,color=[.8, .8, .8],linewidth=.7,zorder=1)
                ax.scatter(spiket_tracking_x,spiket_tracking_y,c=head_angle_,s=15,alpha=.8, zorder=4,cmap='jet',lw=0)

        except TypeError as err:
            ax.axis('off');ax.axis('equal')

            continue

        #ax.set_title('{} spikes > {} cm/s'.format(len(current_spiket_tracking.index[current_spiket_tracking['speed_filtered']>speed_cutoff]),
        #                                           speed_cutoff),y=-0.1, fontsize=10)
        ax.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no,
        df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=10)

        ax.axis('off');ax.axis('equal')
        ax.set_xlim(np.min(current_tracking['correct_x_inter']),np.max(current_tracking['correct_x_inter']))
        ax.set_ylim(np.min(current_tracking['correct_y_inter']),np.max(current_tracking['correct_y_inter']))
        ax.invert_yaxis()

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.05, hspace=0.12)
    plt.rcParams['svg.fonttype'] = 'none'
    plt.savefig(r'N:\davidcr\stellate paper images\python output\putstellate_pathplot.svg', format='svg', dpi=1000)
    print('Generating plot ...')
    plt.show()

def draw_ratemaps(ratemaps_cells,column_name,number_plots,offset, cmp = 'inferno'):
    figure = plt.figure(figsize=(17,17),facecolor='w')
    df = ratemaps_cells.reset_index(drop = True)
    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset
        ax = figure.add_subplot(5,5,row-offset+1)

        current_ratemap = df[column_name].iloc[row]
        peakRate = np.around(df['peak_rate_bnt'].iloc[row],decimals = 1)
        try:
            ax.imshow(current_ratemap.T, cmap=cmp, interpolation='nearest')
        except AttributeError as err:
            current_ratemap = fake_ratemap()
            ax.imshow(current_ratemap, cmap=cmr, interpolation='nearest')

        ax.axis('off');ax.axis('equal')
        ax.set_xlim(0,current_ratemap.shape[0])
        ax.set_ylim(0,current_ratemap.shape[1])
        ax.invert_yaxis()
        ax.set_title('{} C{} T{} | {} {}'.format(df.iloc[row].animal_id,df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].session_ts.strftime("%b %d, %Y"),peakRate),fontsize=10)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.05, hspace=0.12)
    print('Generating plot ...')
    plt.savefig(r'N:\davidcr\stellate paper images\python output\putstellate_grids.svg', format='svg', dpi=1000, bbox_inches='tight')
    plt.show()

def draw_auto_corrs(df,number_plots,offset):
    figure = plt.figure(figsize=(25,15),facecolor='w')

    for row in tqdm_notebook(range(number_plots)):
        row = row + offset
        sns.set_style('whitegrid')

        ax = figure.add_subplot(5,5,row-offset+1)
        sns.set(font_scale=1.4)

        #a4.yaxis.set_visible(False)
        try:
            ax.bar(np.arange(501) - 250, df.iloc[row].st_autocorr, width=1.001, ec='none',color='black',alpha=.85);
        except ValueError as err:
            continue

        ax.bar(np.arange(501) - 250, df.iloc[row].st_autocorr, width=1.001, ec='none',color='black',alpha=.85);
        ax.set_xlim(-250,250)
        [y1,y2] = ax.get_ylim()
        ax.set_ylim(0,1.1*y2)
        ax.axes.get_yaxis().set_visible(False)
        ax.grid(color='k', linestyle=':', linewidth=1,alpha=.6)
        sns.despine(left=True, bottom=True, right=True,top=True)
        ax.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=14,y=1.1)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.1, hspace=0.8)

    print('Generating plot ...')
    plt.show()

def draw_tuning_curves(df,column_names,number_plots,offset,zero_location,clr='k'):
    figure = plt.figure(figsize=(17,17),facecolor='w')
    df = df.reset_index()
    sns.set_style('darkgrid')

    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset
        ax = figure.add_subplot(5,5,row-offset+1,projection='polar')

        bins_angle_center = df.iloc[row][column_names[0]]
        hist_angle_smooth = df.iloc[row][column_names[1]]

        try:
            ax.set_theta_zero_location(zero_location)
            ax.plot(bins_angle_center, hist_angle_smooth,lw=2,color=clr,alpha=.85)
            #ax.set_yticklabels([])
            ax.spines['polar'].set_visible(False)

        except AttributeError as err:
            continue
        except ValueError as err:
            print('Not found: C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")))

        ax.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=10,y=1.1)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
    print('Generating plot ...')
    plt.rcParams['svg.fonttype'] = 'none'
    plt.savefig(r'N:\davidcr\stellate paper images\python output\stellate_grids_theta_tuning.svg', format='svg', dpi=1000, bbox_inches='tight')

    plt.show()

def draw_spiket_LFP(df,number_plots,offset,sample_rate=250.):
    figure = plt.figure(figsize=(17,17),facecolor='w')
    df = df.reset_index()
    sns.set_style('whitegrid')

    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset
        ax = figure.add_subplot(5,5,row-offset+1)
        try:
            time_st_lfp = np.arange(df.spike_trig_lfp[row].shape[0])/float(sample_rate)
            time_spike_trig_lfp = time_st_lfp - time_st_lfp[-1]/2

            ax.plot(time_spike_trig_lfp,df.spike_trig_lfp[row].spike_trig_LFP_avg,color='k',alpha=.85, lw=3)
            ax.plot(time_spike_trig_lfp,df.spike_trig_lfp[row].spike_trig_LFP_strong_avg,color='k',alpha=.4, lw=2.5)
            #ax4.plot(time_spike_trig_lfp,base_dataframe.spike_trig_lfp[0].hilbert_angle_avg*20,color='r',alpha=.3, lw=2)
            ylim = ax.get_ylim()
            ax.set_title('-0.2     0.2'.format(),y=-0.17,fontsize=18)
            ax.axvline(x=0,color='k',linestyle=':',lw=1.5)
            ax.set_xlim(-0.2,0.2)
            ax.set_ylim(-50,50)
            ax.axes.get_xaxis().set_visible(False)
            ax.axes.get_yaxis().set_visible(False)
            sns.despine(left=True)

        except AttributeError as err:
            continue
        except ValueError as err:
            print('Not found: C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")))

        ax.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=10,y=1.1)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
    print('Generating plot ...')
    plt.show()

def draw_waveforms(df, column_name, number_plots, offset):
    figure = plt.figure(figsize=(17,17),facecolor='w')
    df = df.reset_index()
    sns.set_style('white')
    sns.set_palette("PuBuGn_d")
    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset
        ax = figure.add_subplot(5,5,row-offset+1)
        try:
            ax.plot(df[column_name].iloc[row][0],lw=2.5)
            ax.plot(df[column_name].iloc[row][1],lw=2.5)
            ax.plot(df[column_name].iloc[row][2],lw=2.5)
            ax.plot(df[column_name].iloc[row][3],lw=2.5)

            ax.axes.get_xaxis().set_visible(False)
            ax.axes.get_yaxis().set_visible(False)
            sns.despine(left=True,bottom=True)

        except AttributeError as err:
            continue
        except ValueError as err:
            print('Not found: C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")))

        ax.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no, df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=10,y=1.1)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
    print('Generating plot ...')
    plt.show()

def create_spike_plots_stimulus(df,number_plots,offset,draw_spikes):
    '''
    Needed in input dataframe:
        - analysis_window
        - sample_rate
        - counter_stimuli
        - stimulus_timepoints
        - spiketimes_cluster
        - sum_1ms
        - stim_freq
        - stim_length
        - "excited"
        - salt_p
        - salt_i
        - ex_latency_median, ex_latency_var, ex_latency_reliabil
        - "inhibited"
        - change_point_ms
    '''

    figure = plt.figure(figsize=(35,35),facecolor='w')
    sns.set(font_scale=2.5)
    sns.set_style('white')
    counter_fig = 1

    df = df.reset_index(drop=True)

    for row in tqdm_notebook(np.arange(number_plots)):
        row = row + offset

        # continue with rest ...
        window = df.iloc[row].analysis_window
        num_bins = int(df.iloc[row].analysis_window*2*df.iloc[row].sample_rate/1000.)

        # initialize axes:
        ax1 = figure.add_subplot(5,5,row-offset+1)
        counter_fig +=1

        if draw_spikes:
            for no,ts in enumerate(df.iloc[row].stimulus_timepoints):
                spikes = df.iloc[row].spiketimes_cluster[(df.iloc[row].spiketimes_cluster>(ts-df.iloc[row].analysis_window)) &
                                            (df.iloc[row].spiketimes_cluster<(ts+df.iloc[row].analysis_window))]
                ax1.scatter(spikes-ts,np.zeros(len(spikes))+no,s=35,color='k',marker=',',alpha=.7)

        ax1.axvline(x=0,lw=2,color='k',linestyle="-")
        window_f = float(window)
        lower_x = -20.; lower_x_plus = (window_f+lower_x)/(2*window_f)
        upper_x = 40.; upper_x_plus = (window_f+upper_x)/(2*window_f)
        ax1.set_xlim(int(lower_x),int(upper_x))
        ax2 = ax1.twiny()
        if draw_spikes:
            bar = ax2.bar((np.linspace(-int(window),int(window),2*int(window))),
                          df.iloc[row].sum_1ms*(df.iloc[row].counter_stimuli/df.iloc[row].sum_1ms.max()),
                          1,alpha=.15,color='k',align='edge')
        else:
            # make bars a bit darker
            bar = ax2.bar((np.linspace(-int(window),int(window),2*int(window))),
                          df.iloc[row].sum_1ms*(df.iloc[row].counter_stimuli/df.iloc[row].sum_1ms.max()),
                          1,alpha=.75,color='k',align='edge')

        bar[1].set_linewidth(0)
        ax2.axis('off')
        ax2.set_xlim(int(lower_x+lower_x_plus),int(upper_x+upper_x_plus)) # +1 for the whole window !!! if you want to plot them into the same coordinates
        ax1.set_ylim(-5,df.iloc[row].counter_stimuli)
        sns.despine(top=True,bottom=False,left=True)

        if df.iloc[row].excited:
            ax1.axvline(x=df.ex_latency_median.iloc[row],lw=2,color='r',linestyle=":")
            info_bx_laser = ax1.text(0.5, 0.96, 'p: {:.3f} \nI: {:.3f} \nL: {:.2f} \nV: {:.2f}\nR: {:.2f}'.format(df.salt_p.iloc[row],
                                                                                df.salt_i.iloc[row],
                                                                                df.ex_latency_median.iloc[row],
                                                                                df.ex_latency_var.iloc[row],
                                                                                df.ex_latency_reliabil.iloc[row]),
                                        transform=ax2.transAxes, fontsize=25,verticalalignment='top')
            info_bx_laser.set_bbox(dict(color='white', alpha=0.75))
        if df.iloc[row].inhibited:
            ax1.axvline(x=df.change_point_ms.iloc[row],lw=2,color='b',linestyle=":")
            info_bx_laser = ax1.text(0.5, 0.96, 'p: {:.3f} ({})\nL: {:.2f}'.format(df.inhib_lowest_p.iloc[row],
                                                                                df.inhib_lowest_p_interval.iloc[row],
                                                                                df.change_point_ms.iloc[row]),
                                        transform=ax2.transAxes, fontsize=25,verticalalignment='top')
            info_bx_laser.set_bbox(dict(color='white', alpha=0.75))

        # display general stimulus information:
        info_bx_laser = ax1.text(0.01, 0.96, 'f: {:.1f} Hz\nl: {:.1f} ms'.format(df.stim_freq.iloc[row],
                                                                                df.stim_length.iloc[row]),
                                        transform=ax2.transAxes, fontsize=25,verticalalignment='top')
        info_bx_laser.set_bbox(dict(color='white', alpha=0.75))

        ax1.set_title('C{} T{} | {} {}'.format(df.iloc[row].cluster_no, df.iloc[row].tetrode_no,
                                               df.iloc[row].animal_id, df.iloc[row].session_ts.strftime("%b %d, %Y")),fontsize=25)

    figure.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.3, hspace=0.35)
    print('Generating plot...')
    plt.show()

def get_session_indices(basenames,spiketimes_cluster,time_stamps,sample_rate):
    '''
    Copied from analysis_helpers!
    Exchanged the spiketime position dataframe with the direct time index of spikes
    for that cluster (spiketimes_cluster)
    '''
    indices_session = [0] # start with zero!
    for no_session, session in enumerate(basenames):
        no_session+=1
        idx = np.argmax(spiketimes_cluster > (time_stamps[no_session]/sample_rate)-1)
        indices_session.append(idx)
    indices_session[-1] = len(spiketimes_cluster)-1 # exchange last value with end index

    return indices_session

def get_waveforms_stats(spike_chs_wfs,indices_session,no_session):
    '''
    Exact copy from analysis_helpers!
    '''
    # get basic waveform statistics
    # feed in waveforms for this cluster and session indices
    maxima_wf = {}
    mean_wf = {}
    std_wf = {}
    for ch in range(4):
        mean_wf[ch] = np.mean(spike_chs_wfs[ch][indices_session[no_session]:indices_session[no_session+1]],axis=0)
        std_wf[ch] = np.std(spike_chs_wfs[ch][indices_session[no_session]:indices_session[no_session+1]],axis=0)
        maxima_wf[ch] = np.max(mean_wf[ch])
    return mean_wf, std_wf, maxima_wf

def corr_wf_base_laser(df, column_name_base, column_name_laser, plotting=False):
    '''
    Extract waveforms and plot basesession waveform vs. laser waveform;
    Calculate pearson'r between waveforms and return
    '''
    sns.set_style('white')
    all_pears_r = []
    all_pears_p = []

    for no in (range(len(df))):
        if plotting: figure = plt.figure(figsize=(10,10))

        tmp_pears_r = []
        tmp_pears_p = []

        for counter_ch in range(4):
            r,p = pearsonr(df[column_name_base].iloc[no][counter_ch], df[column_name_laser].iloc[no][counter_ch])
            tmp_pears_r.append(r);tmp_pears_p.append(p)

            if plotting:
                ax = figure.add_subplot(2,2,counter_ch+1)
                ax.plot(df[column_name_base].iloc[no][counter_ch],color='k',label='base session',lw=5)
                ax.plot(df[column_name_laser].iloc[no][counter_ch],color=[1,.2,.4],label='laser session',lw=4)
                #if counter_ch == 2: plt.legend()

                ylim = ax.get_ylim()
                ax.text(1,.7*ylim[0],'r: {:.3f}\np: {:.3f}'.format(r,p))
                #ax.text(1,.7*ylim[1],'Ch {}'.format(counter_ch+1),fontweight='bold')

                sns.despine(left=True,bottom=True)
                ax.get_yaxis().set_visible(False)
                ax.get_xaxis().set_visible(False)

        if plotting: plt.show()
        # get median correlation over all channels:
        all_pears_r.append(np.median(tmp_pears_r))
        all_pears_p.append(np.median(tmp_pears_p))
    return all_pears_r,all_pears_p

print('Loaded postgres_analysis_helpers -> general')

reason: import numpy,from scipy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 22 22:15:25 2018

@author: luogan
"""
'''
from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets('MNIST_data/',one_hot=True)
'''
#################################
import tensorflow as tf

import numpy as np
from pymongo import MongoClient
import tensorflow as tf
import pandas as pd
client=MongoClient('localhost',27017)
db=client.mnist.data
from sklearn.preprocessing import OneHotEncoder, StandardScaler

import mnist_data
sess=tf.InteractiveSession()

in_units=784
h1_units=500

h2_units=100

w1=tf.Variable(  tf.truncated_normal([in_units,h1_units],stddev=0.1 ) )
b1=tf.Variable(tf.zeros([h1_units]))

w2=tf.Variable(tf.truncated_normal([h1_units,h2_units],stddev=0.1 ))
b2=tf.Variable(tf.zeros([h2_units]))

print('*'*20)

w3=tf.Variable(tf.zeros([h2_units,10]))
b3=tf.Variable(tf.zeros([10]))

x=tf.placeholder(tf.float32,[None,784])

#x=tf.placeholder(tf.float32,shape=[None,28,28])
#
#x2=tf.reshape(x,[None,784])

keep_prob=tf.placeholder(tf.float32)

hidden1=tf.nn.relu(tf.matmul(x,w1)+b1)
hidden1_drop=tf.nn.dropout(hidden1,keep_prob)

hidden2=tf.nn.relu(tf.matmul(hidden1_drop,w2)+b2)

hidden2_drop=tf.nn.dropout(hidden2,keep_prob)

y2=tf.nn.softmax(tf.matmul(hidden2_drop,w3)+b3)

y_=tf.placeholder(tf.float32,[None,10])

cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y2),
                                            reduction_indices=[1]))

train_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

pre_num=tf.argmax(y2,1,output_type='int64',name="output")#输出节点名：output
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(pre_num, tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

tf.global_variables_initializer().run()

X,Y=mnist_data.mnist_soft()

import time
import random
t1=time.time()
#for i in range(1000):
#    cut=100
#    rand=random.randint(cut, 60000)
##    rand=10000
#    down=rand-cut
##    batch_xs,batch_ys=mnist.train.next_batch(1000)
#    batch_xs,batch_ys=X[down:rand],Y[down:rand]
#
#    batch_xs1=batch_xs.reshape([100,784])
#    train_step.run({x:batch_xs1,y_:batch_ys,keep_prob:1.0})

for i in range(2000):
  cut=100
  rand=random.randint(cut, 60000)
#    rand=10000
  down=rand-cut
#    batch_xs,batch_ys=mnist.train.next_batch(1000)
  batch_xs,batch_ys=X[down:rand],Y[down:rand]
  batch_xs1=batch_xs.reshape([100,784])
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
        x:batch_xs1, y_: batch_ys, keep_prob: 1.0})
    print ("step %d, training accuracy %.3f"%(i, train_accuracy))
  train_step.run(feed_dict={x: batch_xs1, y_: batch_ys, keep_prob: 0.5})

print(time.time()-t1)

print ("test accuracy %.3f" % accuracy.eval(feed_dict={
    x: X[50000:].reshape(10000,784), y_: Y[50000:], keep_prob: 1.0}))

#correct_prediction=tf.equal(tf.argmax(y2,1),tf.argmax(y_,1))
#
#accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#
#print(time.time()-t1)
#print(accuracy.eval({x:X[50000:].reshape([10000,784]),y_:Y[50000:],keep_prob:1}))

ext: py

reason: import numpy


text:
# check for duplicate detections
import numpy as np

def iou(box1, box2):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(box1[0], box2[0])
    yA = max(box1[1], box2[1])
    xB = min(box1[2], box2[2])
    yB = min(box1[3], box2[3])

    # compute the area of intersection rectangle
    intersction = abs(max((xB - xA, 0)) * max((yB - yA), 0))
    if intersction == 0:
        return 0
    # compute the area of both the prediction and ground-truth
    # rectangles
    box1_Area = abs((box1[2] - box1[0]) * (box1[3] - box1[1]))
    box2_Area = abs((box2[2] - box2[0]) * (box2[3] - box2[1]))

    # compute the intersection over union
    # union is sum of areas of two boxes - intersection
    iou = intersction / float(box1_Area + box2_Area - intersction)

    # return the intersection over union value
    return iou

def iou_check(cutter_list):
    # record indices of duplicates
    dupl_temp = []
    for i in range(len(cutter_list)):
        for j in range(len(cutter_list)):
            if i == j:
                continue
            elif iou(cutter_list[i], cutter_list[j]) > 0.5:
                dupl_temp.append(i)

    # get only first half of indices
    if len(dupl_temp) != 0:
        # remove the indices from original list
        to_remove = []
        for i in dupl_temp[len(dupl_temp)//2:]:
            to_remove.append(cutter_list[i])

        for j in to_remove:
            try:
                cutter_list.remove(j)
            except ValueError:
                continue
        return cutter_list
    else:
        return cutter_list


text:
from __future__ import division

import numpy as np
import scipy.stats as stats
import copy

from numpy import linalg as la
from warnings import warn as Warn
from .sample import logp_rho_cov, logp_lambda_cov
from ...abstracts import Sampler_Mixin, Hashmap, Trace
from ... import verify
from ... steps import Metropolis, Slice
from ... import priors
from ...utils import speigen_range, ind_covariance, chol_mvn
from pysal.spreg.utils import spdot

SAMPLERS = ['Alphas', 'Betas', 'Sigma2', 'Tau2', 'Lambda', 'Rho']

class Base_Generic(Sampler_Mixin):
    """
    This is a class that provides the generic structures required for the two-level variance components models with spatially-correlated error. All of the arguments are documented in the Generic class.
    """
    def __init__(self, Y, X, W, M, Delta,
                 n_samples=1000, n_jobs=1,
                 extra_traced_params = None,
                 priors=None,
                 starting_values=None,
                 configs=None,
                 truncation=None):
        super(Base_Generic, self).__init__()

        N, p = X.shape
        _N, J = Delta.shape
        self.state = Hashmap(**{'X':X, 'Y':Y, 'M':M, 'W':W, 'Delta':Delta,
                           'N':N, 'J':J, 'p':p })
        self.traced_params = copy.deepcopy(SAMPLERS)
        if extra_traced_params is not None:
            self.traced_params.extend(extra_traced_params)
        hashmaps = [{k:[] for k in self.traced_params}]*n_jobs
        self.trace = Trace(*hashmaps)

        if priors is None:
            priors = dict()
        if starting_values is None:
            starting_values = dict()
        if configs is None:
            configs = dict()
        if truncation is None:
            truncation = dict()

        self._setup_priors(**priors)
        self._setup_configs(**configs)
        self._setup_truncation(**truncation)
        self._setup_starting_values(**starting_values)

        ## Covariance, computing the starting values
        self.state.Psi_1 = ind_covariance
        self.state.Psi_1i = ind_covariance
        self.state.Psi_2 = ind_covariance
        self.state.Psi_2i = ind_covariance

        self.cycles = 0

        if n_samples > 0:
            try:
                self.sample(n_samples, n_jobs=n_jobs)
            except (np.linalg.LinAlgError, ValueError) as e:
                Warn('Encountered the following LinAlgError. '
                     'Model will return for debugging. \n {}'.format(e))

    def _setup_priors(self, Betas_cov0 = None, Betas_mean0=None,
                      Sigma2_a0 = .001, Sigma2_b0 = .001,
                      Tau2_a0 = .001, Tau2_b0 = .001,
                      Log_Lambda0 = priors.constant,
                      Log_Rho0 = priors.constant):
        """
        This sets up the data and hyperparameters of the problem.
        If the hyperparameters are to be adjusted, pass them as keyword arguments.
        """
        st = self.state
        st.Sigma2_a0 = Sigma2_a0
        st.Sigma2_b0 = Sigma2_b0
        if Betas_cov0 is None:
            Betas_cov0 = np.eye(self.state.p) * 100
        if Betas_mean0 is None:
            Betas_mean0 = np.zeros((self.state.p, 1))
        st.Betas_cov0 = Betas_cov0
        st.Betas_mean0 = Betas_mean0
        st.Tau2_a0 = .001
        st.Tau2_b0 = .001
        st.Log_Lambda0 = Log_Lambda0
        st.Log_Rho0 = Log_Rho0

    def _finalize(self):
        """
        This computes derived properties of hyperparameters that do not change
        over iterations. This is called one time before sampling.
        """
        st = self.state

        st.In = np.eye(st.N)
        st.Ij = np.eye(st.J)

        ## Derived factors from the prior
        st.Betas_cov0i = np.linalg.inv(st.Betas_cov0)
        st.Betas_covm = np.dot(st.Betas_cov0, st.Betas_mean0)
        st.Sigma2_an = self.state.N / 2 + st.Sigma2_a0
        st.Tau2_an = self.state.J / 2 + st.Tau2_a0

        st.PsiRhoi = st.Psi_1i(st.Rho, st.W)
        st.PsiLambdai = st.Psi_2i(st.Lambda, st.M)

        ## Data invariants
        st.XtX = np.dot(self.state.X.T, self.state.X)
        st.DeltatDelta = np.dot(self.state.Delta.T, self.state.Delta)

        st.DeltaAlphas = np.dot(st.Delta, st.Alphas)
        st.XBetas = np.dot(st.X, st.Betas)

        st.initial_values = Hashmap(Betas = st.Betas, Alphas=st.Alphas, Rho=st.Rho, Lambda=st.Lambda,
                                    Sigma2=st.Sigma2, Tau2=st.Tau2)

    def _setup_configs(self, Lambda_method = 'met', Lambda_configs = None,
                             Rho_method = 'met', Rho_configs = None,
                             **uncaught):
        """
        Omnibus function to assign configuration parameters to the correct
        configuration namespace. Any options in uncaught get passed to the step methods during initialization.
        """
        if Lambda_configs is None:
            Lambda_configs = dict()
        if Rho_configs is None:
            Rho_configs = dict()

        if uncaught != dict():
            if 'method' in uncaught:
                Lambda_method = Rho_method = method
            Lambda_configs = copy.deepcopy(uncaught)
            Rho_configs = copy.deepcopy(uncaught)

        if Lambda_method.lower().startswith('met'):
            Lambda_method = Metropolis
            Lambda_configs['jump'] = Lambda_configs.pop('jump', .5)
            Lambda_configs['max_tuning'] = Lambda_configs.pop('tuning',0)
        elif Lambda_method.lower().startswith('slice'):
            Lambda_method = Slice
            Lambda_configs['width'] = Lambda_configs.pop('width', .5)
        else:
            raise Exception('Sample method for Lambda not understood:\n{}'
                            .format(Lambda_method))
        if Rho_method.lower().startswith('met'):
            Rho_method = Metropolis
            Rho_configs['jump'] = Rho_configs.pop('jump', .5)
            Rho_configs['max_tuning'] = Rho_configs.pop('tuning',0)

        elif Rho_method.lower().startswith('slice'):
            Rho_method = Slice
            Rho_configs['width'] = Rho_configs.pop('width', .5)
        else:
            raise Exception('Sample method for Rho not understood:\n{}'
                            .format(Rho_method))

        self.configs = Hashmap()
        self.configs.Rho = Rho_method('Rho', logp_rho_cov, **Rho_configs)
        self.configs.Lambda = Lambda_method('Lambda', logp_lambda_cov, **Lambda_configs)

    def _setup_truncation(self, Rho_min=None, Rho_max = None,
                          Lambda_min = None, Lambda_max = None):
        """
        This computes truncations for the spatial parameters.

        If configs.truncate is set to 'eigs', computes the eigenrange of the two
        spatial weights matrices using speigen_range

        If configs.truncate is set to 'stable', sets the truncation to -1,1

        If a tuple is passed to truncate, then this will truncate the
        distribution according to this tuple
        """
        st = self.state
        if hasattr(st, 'W'):
            if (Rho_min is None) or (Rho_max is None):
                W_emin, W_emax = speigen_range(st.W)
            if (Rho_min is None):
                Rho_min = 1./W_emin
            if (Rho_max is None):
                Rho_max = 1./W_emax
            st.Rho_min = Rho_min
            st.Rho_max = Rho_max
        if hasattr(st, 'M'):
            if (Lambda_min is None) or (Lambda_max is None):
                M_emin, M_emax = speigen_range(st.M)
            if (Lambda_min is None):
                Lambda_min = 1./M_emin
            if (Lambda_max is None):
                Lambda_max = 1./M_emax
            st.Lambda_min = Lambda_min
            st.Lambda_max = Lambda_max

    def _setup_starting_values(self, Betas = None, Alphas = None,
                               Sigma2 = 4, Tau2 = 4,
                               Rho = None, Lambda = None):
        """
        Set arbitrary starting values for the Metropolis sampler
        """
        st = self.state
        if Betas is None:
            Betas = np.zeros((self.state.p, 1))
        if Alphas is None:
            Alphas = np.zeros((self.state.J, 1))
        st.Betas = Betas
        st.Alphas = Alphas
        st.Sigma2 = Tau2
        st.Tau2 = Sigma2
        if Rho is None:
            Rho = -1.0 / (self.state.N - 1)
        if Lambda is None:
            Lambda = -1.0 / (self.state.J - 1)
        st.Rho = Rho
        st.Lambda = Lambda

    def _iteration(self):
        """
        Compute a single iteration of the sampler.
        This steps through all parameter updates exactly once.
        """
        st = self.state

        ### Sample the Beta conditional posterior
        ### P(beta | . ) \propto L(Y|.) \dot P(\beta)
        ### is
        ### N(Sb, S) where
        ### S = (X' Sigma^{-1}_Y X + S_0^{-1})^{-1}
        ### b = X' Sigma^{-1}_Y (Y - Delta Alphas) + S^{-1}\mu_0
        covm_update = spdot(st.X.T, spdot(st.PsiRhoi, st.X)) / st.Sigma2
        covm_update += st.Betas_cov0i
        covm_update = np.asarray(covm_update)
        covm_update = la.inv(covm_update)

        resids = st.Y - st.Delta.dot(st.Alphas)
        XtSresids = spdot(st.X.T, spdot(st.PsiRhoi, resids)) / st.Sigma2
        mean_update = XtSresids + st.Betas_cov0i.dot(st.Betas_mean0)
        mean_update = np.asarray(mean_update)
        mean_update = np.dot(covm_update, mean_update)
        st.Betas = chol_mvn(mean_update, covm_update)
        st.XBetas = np.dot(st.X, st.Betas)

        ### Sample the Random Effect conditional posterior
        ### P( Alpha | . ) \propto L(Y|.) \dot P(Alpha | \lambda, Tau2)
        ###                               \dot P(Tau2) \dot P(\lambda)
        ### is
        ### N(Sb, S)
        ### Where
        ### S = (Delta'Sigma_Y^{-1}Delta + Sigma_Alpha^{-1})^{-1}
        ### b = (Delta'Sigma_Y^{-1}(Y - X\beta) + 0)
        covm_update = spdot(st.Delta.T, spdot(st.PsiRhoi, st.Delta)) / st.Sigma2
        covm_update += st.PsiLambdai / st.Tau2
        covm_update = np.asarray(covm_update)
        covm_update = la.inv(covm_update)

        resids = st.Y - st.XBetas
        mean_update = spdot(st.Delta.T, spdot(st.PsiRhoi, resids)) / st.Sigma2
        mean_update = np.asarray(mean_update)
        mean_update = np.dot(covm_update, mean_update)
        st.Alphas = chol_mvn(mean_update, covm_update)
        st.DeltaAlphas = np.dot(st.Delta, st.Alphas)

        ### Sample the Random Effect aspatial variance parameter
        ### P(Tau2 | .) \propto L(Y|.) \dot P(\Alpha | \lambda, Tau2)
        ###                            \dot P(Tau2) \dot P(\lambda)
        ### is
        ### IG(J/2 + a0, u'(\Psi(\lambda))^{-1}u * .5 + b0)
        bn = spdot(st.Alphas.T, spdot(st.PsiLambdai, st.Alphas)) * .5 + st.Tau2_b0
        st.Tau2 = stats.invgamma.rvs(st.Tau2_an, scale=bn)

        ### Sample the response aspatial variance parameter
        ### P(Sigma2 | . ) \propto L(Y | .) \dot P(Sigma2)
        ### is
        ### IG(N/2 + a0, eta'Psi(\rho)^{-1}eta * .5 + b0)
        ### Where eta is the linear predictor, Y - X\beta + \DeltaAlphas
        eta = st.Y - st.XBetas - st.DeltaAlphas
        bn = spdot(eta.T, spdot(st.PsiRhoi, eta)) * .5 + st.Sigma2_b0
        st.Sigma2 = stats.invgamma.rvs(st.Sigma2_an, scale=bn)

        ### Sample the spatial components using metropolis-hastings
        ### P(Psi(\lambda) | .) \propto L(Y | .) \dot P(\lambda)
        ### is
        ### |Psi(lambda)|^{-1/2} exp(1/2(Alphas'Psi(lambda)^{-1}Alphas * Tau2^{-1}))
        ###  * 1/(emax-emin)
        st.Rho = self.configs.Rho(st)

        st.PsiRhoi = st.Psi_1i(st.Rho, st.W) #BtB or CiCti

        ### P(Psi(\rho) | . ) \propto L(Y | .) \dot P(\rho)
        ### is
        ### |Psi(rho)|^{-1/2} exp(1/2(eta'Psi(rho)^{-1}eta * Sigma2^{-1})) * 1/(emax-emin)
        st.Lambda = self.configs.Lambda(st)

        st.PsiLambdai = st.Psi_2i(st.Lambda, st.M)

class Generic(Base_Generic):
    """
    This is a class that provides the generic structures required for the two-level variance components models with spatially-correlated error.

    Arguments
    ----------
    Y       :   numpy.ndarray
                The (n,1) array containing the response to be modeled
    X       :   numpy.ndarray
                The (n,p) array containing covariates used to predict the  response, Y.
    W       :   pysal.weights.W
                a spatial weights object for the n observations
    M       :   pysal.weights.W
                a spatial weights object for the J regions.
    Z       :   numpy.ndarray
                The (J,p') array of region-level covariates used to predict the response, Y.
    Delta   :   numpy.ndarray
                The (n,J) dummy variable matrix, relating observation i to region j. Required if membership is not passed.
    membership: numpy.ndarray
                The (n,) vector of labels assigning each observation to a region. Required if Delta is not passed.
    transform  : string
                 weights transform to use in modeling.
    verbose    : bool
                 whether to provide verbose output about sampling progress
    n_samples : int
                the number of samples to draw. If zero, the model will only be initialized, and later sampling can be done using model.sample
    n_jobs  :   int
                the number of parallel chains to run. Defaults to 1.
    extra_traced_param  :   list of strings
                            extra parameters in the trace to record.
    center  :   bool
                Whether to center the input data so that its mean is zero. Occasionally improves the performance of the sampler.
    scale   :   bool
                Whether to rescale the input data so that its variance is one. May dramatically improve the performance of the sampler, at the cost of interpretability of the coefficients.
    priors  :   dictionary
                A dictionary used to configure the priors for the model. This may include the following keys:
                    Betas_cov0  : prior covariance for Beta parameter vector
                                (default: I*100)
                    Betas_mean0 : prior mean for Beta parameters
                                (default: 0)
                    Sigma2_a0   : prior shape parameter for inverse gamma prior on response-level variance
                                (default: .001)
                    Sigma2_b0   : prior scale parameter for inverse gamma prior on response-level variance
                                (default: .001)
                    Tau2_a0     : prior shape parameter for inverse gamma prior on regional-level variance
                                (default: .001)
                    Tau2_b0     : prior scale parameter for inverse gamma prior on regional-level variance
                                (default: .001)
                    Log_Lambda0 : prior on Lambda, the region-level autoregressive parameter. Must be a callable function that takes a single argument and returns a single value providing the log prior likelihood.
                                (default: priors.constant)
                    Log_Rho0    : prior on Rho, the response-level autoregressive paraameter. Must be a callable function that takes a single argument and returns a single value providing the log prior likelihood.
                                (default: priors.constant)
    starting_value :    dictionary
                        A dictionary containing the starting values of the sampler. If n_jobs > 1, these starting values are perturbed randomly to induce overdispersion.

                        This dicutionary may include the following keys:
                        Betas   : starting vector of Beta parameters.
                                (default: np.zeros(p,1))
                        Alphas  : starting vector of Alpha variance components.
                                (default: np.zeros(J,1))
                        Sigma2  : starting value of response-level variance
                                (default: 4)
                        Tau2    : starting value of region-level varaince
                                (default: 4)
                        Rho     : starting value of response-level spatial autoregressive parameter
                                (default: -1/n)
                        Lambda  : starting value of region-level spatial autoregressive parameter
                                (default -1/J)
    config  :   dictionary
                A dictionary containing the configuration values for the non-Gibbs samplers for the spatial parameters. May be specified for each parameter individually (if both are in the model), or may be specified implicitly for the relevant parameter to the model. Keys may include:
                Rho_method      : string specifying whether "met" or "slice" sampling should be used for the response-level effects
                Rho_configs     : configuration options for the sampler for the response-level effects that will be used in the step method.
                Lambda_method   : string specifying whether 'met' or 'slice' sampling should be used for the region-level effects
                Lambda_configs  : configuration options for the sampler for the region-level effects that will be used in the step method.

                For options that can be in Lambda/Rho_configs, see:
                spvcm.steps.Slice, spvcm.steps.Metropolis
    truncation  :   dictionary
                    A dictionary containing the configuration values for the maximum and minimum allowable Lambda and Rho parameters. If these are not provided, the support is row-standardized by default, and the minimal eigenvalue computed for the lower bound on the parameters. *only* the single minimum eigenvalue is computed, so this is still rather efficient for large matrices. Keys may include:
                    Rho_min     : minimum value allowed for response-level autoregressive coefficient
                    Rho_max     : maximum value allowed for response-level autoregressive coefficient
                    Lambda_min  : minimum value allowed for region-level autoregressive coefficient
                    Lambda_max  : maximum value allowed for region-level autoregressive coefficient.
    """
    def __init__(self, Y, X, W, M, Z=None, Delta=None, membership=None,
                 #data options
                 transform ='r', verbose=False,
                 n_samples=1000, n_jobs=1,
                 extra_traced_params = None,
                 priors=None,
                 starting_values=None,
                 configs=None,
                 truncation=None,
                 center=False,
                 scale=False):
        if X is None:
            X = np.ones_like(Y)
            center=False
            scale=False
        W,M = verify.weights(W,M, transform=transform)
        self.M = M

        N,_ = X.shape
        J = M.n
        Mmat = M.sparse
        Wmat = W.sparse
        Delta, membership = verify.Delta_members(Delta, membership, N, J)

        if Z is not None:
            Z = Delta.dot(Z)
            X = np.hstack((X,Z))
        if center:
            X = verify.center(X)
        if scale:
            X = verify.scale(X)

        X = verify.covariates(X)

        self._verbose = verbose

        super(Generic, self).__init__(Y, X, Wmat, Mmat, Delta,
                                      n_samples=n_samples, n_jobs=n_jobs,
                                      extra_traced_params = extra_traced_params,
                                      priors=priors,
                                      starting_values=starting_values,
                                      configs=configs,
                                      truncation=truncation)

max_stars_repo_name: ljwolf/spvcm

max_issues_repo_name: gisdevelope/spvcm

max_forks_repo_name: gisdevelope/spvcm

reason: import numpy,from numpy,import scipy


text:
#!/usr/bin/env python

import cv2
import numpy as np
import sys, getopt

def main(argv):
    if (len(argv) < 3):
        print 'usage: ' + sys.argv[0] + ' imagefile angle outfile'
        sys.exit(1)

    imagefile = argv[0]
    angle = int(argv[1])
    outfile = argv[2]
    img = cv2.imread(imagefile)
    rows, cols = img.shape[:2]
    print rows, cols
    center = ((cols-1.0)/2, (rows-1.0)/2)
    matrix = cv2.getRotationMatrix2D(center,angle,1)
    dst = cv2.warpAffine(img,matrix,(cols,rows))

    cv2.imwrite(outfile, dst)

if __name__ == "__main__":
    main(sys.argv[1:])

max_stars_repo_name: jeoygin/gadget

max_issues_repo_name: jeoygin/gadget

max_forks_repo_name: jeoygin/gadget


text:
"""There are some discrepancies between the fna and faa files. What are they?"""

import numpy
from fasta import FASTA

names = ['2236446587', '2236446227', '2236446226', '2236446115', '2236446114', '2236445762', '2236446248', '2236446050', '2236446154', '2236446074', '2236445702', '2236446303', '2236446345', '2236446625', '2236446051', '2236446049', '2236446581', '2236445824', '2236446355', '2236446351', '2236446370', '2236446339', '2236446062', '2236445915', '2236445916', '2236445669', '2236446147', '2236446048', '2236446182']
path = "/proj/b2013274/mcl/2236347014.genes.fna"
fasta = FASTA(path)

print "all:", numpy.mean(fasta.lengths)
print "missing:", numpy.mean([len(fasta[name]) for name in names])


text:
import datetime
import jwt
from flask import current_app
from memos import db, login_manager
from flask_login import UserMixin
from memos import bcrypt
import re
import numpy
from sqlalchemy.orm import relationship

from memos.models.MemoSubscription import MemoSubscription

# This is used by the flask UserMixin
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

class Delegate(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    owner_id = db.Column(db.String(120), db.ForeignKey('user.username'),nullable=False)   # who is suppsoed to sign
    delegate_id = db.Column(db.String(120), db.ForeignKey('user.username'),nullable=False)  # who is alloed to perform the signature

    @staticmethod
    def is_delegate(owner,delegate):
    #current_app.logger.info(f"Owner={owner} delegate={delegate}")
        if owner is None or delegate is None:
            return False
        if owner.username == delegate.username:
            return True

        if delegate.admin == True:
            return True

        d = Delegate.query.filter_by(owner_id=owner.username,delegate_id=delegate.username).first()
        if d:
            return True
        else:
            return False

    @staticmethod
    def get_delegates(owner):
        """Lookup delegates owner has defined."""
        delegate_list = Delegate.query.filter_by(owner_id=owner.username).all()
        rval = []
        for delegate_entry in delegate_list:
            user = User.find(username=delegate_entry.delegate_id)
            rval.append(user)
        return rval

    @staticmethod
    def get_delegated_users(delegate):
        """Lookup owners that have added this delegate."""
        delegate_list = Delegate.query.filter_by(delegate_id=delegate.username).all()
        rval = []
        for delegate_entry in delegate_list:
            user = User.find(username=delegate_entry.owner_id)
            rval.append(user)
        return rval

    @staticmethod
    def add(owner, delegate):
        new_delegate = Delegate(owner_id=owner.username,delegate_id=delegate.username)

        db.session.add(new_delegate)
        db.session.commit()
        pass

    @staticmethod
    def delete(owner,delegate=None):
        if delegate == None:
            Delegate.query.filter_by(owner_id=owner.username).delete()
        else:
            Delegate.query.filter_by(owner_id=owner.username,delegate_id=delegate.username).delete()

class User(db.Model, UserMixin):
    username = db.Column(db.String(120), primary_key=True)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    admin = db.Column(db.Boolean,default=False)
    readAll = db.Column(db.Boolean,default=False)
    pagesize = db.Column(db.Integer, nullable=False, default = 10)

    memos = db.relationship('Memo',backref=db.backref('user', lazy=True))
    history = db.relationship('MemoHistory',backref=db.backref('user', lazy=True))

    def get_id(self):
        return self.username

    def get_reset_token(self, expires_sec=1800):
        reset_token = jwt.encode(
            {
                "confirm": self.username,
                "exp": datetime.datetime.now(tz=datetime.timezone.utc)
                       + datetime.timedelta(seconds=expires_sec)
            },
            current_app.config['SECRET_KEY'],
            algorithm="HS256"
        )
        return reset_token

    def check_password(self,check_pw):
        return bcrypt.check_password_hash(self.password, check_pw)

    @property
    def delegate_for(self):
        """owners user is a delegate for"""
        delegate_list = Delegate.get_delegated_users(delegate=self)
        current_app.logger.info(f"Delegate List = {delegate_list}")
        rval = ''
        for delegate in delegate_list:
            rval = rval + delegate.username + ' '
        return {"usernames":rval,"users":delegate_list}

    @property
    def delegates(self):
        """delegates defined for this user"""
        delegate_list = Delegate.get_delegates(owner=self)
        current_app.logger.info(f"Delegate List = {delegate_list}")
        rval = ''
        for delegate in delegate_list:
            rval = rval + delegate.username + ' '
        return {"usernames":rval,"users":delegate_list}

    @delegates.setter
    def delegates(self,delegates):

        Delegate.delete(owner=self)
        for delegate_name in re.split(r'\s|\,|\t|\;|\:',delegates):
            delegate = User.find(username=delegate_name)
            if delegate != None:
                Delegate.add(self,delegate)

    # is the userid a valid delgate for "self"
    def is_delegate(self,delegate=None):
        return Delegate.is_delegate(self,delegate)

    @property
    def subscriptions(self):
        sublist = MemoSubscription.get(self)
        l = []
        for sub in sublist:
            l.append( sub.subscription_id)
        return ' '.join(l)

    @subscriptions.setter
    def subscriptions(self,sub_names):
        self._subscriptions = sub_names
        users = User.valid_usernames(sub_names)
        MemoSubscription.delete(self)
        for user in users['valid_users']:
            #current_app.logger.info(f"adding subscription {self} to {user}")
            ms = MemoSubscription(subscriber_id=self.username,subscription_id=user.username)
            db.session.add(ms)
            db.session.commit()

    @staticmethod
    def create_hash_pw(plaintext):
        return bcrypt.generate_password_hash(plaintext).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        try:
            data = jwt.decode(
                token,
                current_app.config['SECRET_KEY'],
                leeway=datetime.timedelta(seconds=10),
                algorithms=["HS256"]
            )
        except:
            return None

        return User.query.get(data.get('confirm'))

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"

    @staticmethod
    def find(username):
        """Lookup user by username

        Returns:
            [User]: The User you are looking for... or None
        """
        return User.query.filter_by(username=username).first()

    # this function takes a string of "users" where they are seperated by , or space and checks if they are valid
    @staticmethod
    def valid_usernames(userlist):
        invalid_usernames = []
        valid_usernames = []
        users = re.split(r'\s|\,',userlist)
        if '' in users: users.remove('')

        for username in users:
            user = User.find(username=username)
            if username != "" and  user == None:
                invalid_usernames.append(username)
            else:
                valid_usernames.append(username)

        valid_usernames = numpy.unique(valid_usernames)
        valid_users = []
        for username in valid_usernames:
            valid_users.append(User.find(username=username))

        return {'valid_usernames':valid_usernames,'invalid_usernames':invalid_usernames,'valid_users':valid_users}

    @staticmethod
    def is_admin(username):
        user = User.find(username=username)
        if user and user.admin:
            return True
        else:
            return False

    @staticmethod
    def is_readAll(username):
        user = User.find(username=username)
        if user and user.readAll:
            return True
        else:
            return False

    @staticmethod
    def get_pagesize(user):
        return user.pagesize

max_stars_repo_name: iotexpert/docmgr

max_issues_repo_name: iotexpert/docmgr

max_forks_repo_name: iotexpert/docmgr


text:
import numpy as np

from skhyper.process import Process
from skhyper.cluster import KMeans

class TestCluster:
    def test_kmeans(self):
        data_3d = np.random.rand(6, 6, 25)
        data_4d = np.random.rand(5, 5, 2, 25)

        X_3d = Process(data_3d)
        X_4d = Process(data_4d)

        # ensure that 3- and 4-dimensional data produce non-zero labels and data clusters
        mdl_3d = KMeans(n_clusters=2)
        mdl_3d.fit(X_3d)
        assert mdl_3d.labels_ is not None

        mdl_4d = KMeans(n_clusters=2)
        mdl_4d.fit(X_4d)
        assert mdl_4d.labels_ is not None

max_stars_repo_name: tensor-strings/scikit-hyper

max_issues_repo_name: tensor-strings/scikit-hyper

max_forks_repo_name: tensor-strings/scikit-hyper