text:
import numpy as np
from utils import fgsm

def linear_solve(A, b):
    AAT = A@(A.T)
    # w = np.linalg.solve(AAT, b)
    w = np.linalg.lstsq(AAT, b)[0]
    x = (A.T)@w
    return x

class IterativeSinModel:
    '''
    H(n) :  h(x) = \sum_j [b_j*sin(a_0+a_1*x+a_2*x^2+...)]
    '''

    def __init__(self, n, MAX_DEGREE=3):
        self.D = MAX_DEGREE+1
        self.n = n # No. of Features
        self.F = np.random.rand(self.D, self.n) * 10 # Feature Tranformation matrix D X n
        self.a = None

    def generate_features(self, X):
        X = X.flatten()
        arrays = [X**i for i in range(self.D)] # includes constant
        A = np.stack(arrays, axis = 1)        # N_data X D
        A = A @ self.F                        # N_data X n
        A = np.sin(A)
        return A

    def feed(self, X):
        '''
        Feed Data to Store its Full Representation A
        '''
        self.A = self.generate_features(X)

    def feed_test(self, X):
        self.A_test = self.generate_features(X)

    def fit(self, X, Y, n_fit):
        '''
        X: (d,1)
        Y: (d,1)
        n_fit: No. of features to fit
        '''
        # self.a = linear_solve(A, Y)
        self.a = np.linalg.lstsq(self.A[:,:n_fit], Y)[0]       # N_data x n TODO: Faster
        return self

    def predict(self, n_fit, A):
        return (A[:,:n_fit])@self.a

    def score_test(self, y, n_fit):
        '''
            RMSE
        '''
        y_pred = self.predict(n_fit, self.A_test)
        return np.linalg.norm(y-y_pred, ord=2)/np.sqrt(y.shape[0])

    def score_train(self, y, n_fit):
        '''
            RMSE
        '''
        y_pred = self.predict(n_fit, self.A)
        return np.linalg.norm(y-y_pred, ord=2)/np.sqrt(y.shape[0])

    def score_adversarial(self, y, n_fit, epsilon):
        '''
            RMSE
        '''
        dt = lambda X : fgsm.loss_derivative_mse(X,y,self.a.reshape(-1,1))
        adversarial_data = fgsm.fgsm_adversarial_data(self.A_test[:,:n_fit], dt, epsilon)
        y_pred = self.predict(n_fit, adversarial_data)
        return np.linalg.norm(y-y_pred, ord=2)/np.sqrt(y.shape[0])

max_stars_repo_name: layjain/Deep-Multiple-Descent

max_issues_repo_name: layjain/Deep-Multiple-Descent

max_forks_repo_name: layjain/Deep-Multiple-Descent


text:
import cv2
import json
import os
import random
from avi_r import AVIReader
import multiprocessing
from multiprocessing import Pool
import numpy as np
import cv2

def draw_box(im, acts,color_dict,show_gt,check_gt=False):
    rm_list = []
    if check_gt:
        gt_act_list = []
        for act in acts:
            cat = act["act"]
            typ = act["type"]
            if typ=="gt" or typ=="spl":
                if cat not in gt_act_list:
                    gt_act_list.append(cat)

    for i in range(len(acts)):
        act = acts[i]
        bbox = act["bbox"]
        cat = act["act"]
        if "type" in act:
            typ = act["type"]
            if typ=="pred" or typ=="lijun":
                if check_gt:
                    if cat not in gt_act_list:
                        continue
                cv2.rectangle(im, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color=color_dict[str(cat)], thickness=3)
                im[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2])] = im[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2])]*1.2
                for i in range(3):
                    im[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2]),i] = im[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2]),i] + color_dict[str(cat)][i]*0.1
                if str(cat) not in rm_list:
                    cv2.putText(im,str(cat), (20,(len(rm_list)+1)*20),color=color_dict[str(cat)], fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=1, thickness=2)
                    rm_list.append(str(cat))
            elif typ == "gt" and show_gt:
                cv2.rectangle(im, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color=(255,255,255), thickness=3)
            elif typ == "spl" and show_gt:
                cv2.rectangle(im, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color=(0,0,0), thickness=3)
        else:
            cv2.rectangle(im, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color=(255,255,255), thickness=3)
            cv2.putText(im,str(cat), (bbox[0],max(0,bbox[1]-5)),color=(255,255,255), fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=1, thickness=2)
    return im

def split_box(objects,vid_name,timeline,act_type):
    for obj in objects:
        boxes = obj["localization"][vid_name]
        for k,v in boxes.items():
            if "boundingBox" in v:
                x0 = v["boundingBox"]["x"]
                y0 = v["boundingBox"]["y"]
                x1 = x0 + v["boundingBox"]["w"]
                y1 = y0 + v["boundingBox"]["h"]
                frame = int(k)
                if frame not in timeline:
                    timeline[frame] = []
                timeline[frame].append({"act":act_type,"bbox":[x0,y0,x1,y1],"type":"spl"})
    return timeline

def merge_box(objects,vid_name,typ):
    x0 = 10000
    y0 = 10000
    x1 = 0
    y1 = 0
    if typ == "lijun":
        obj = objects[0]
        boxes = obj["localization"][vid_name]
        for k,v in boxes.items():
            if "boundingBox" in v:
                x0 = v["boundingBox"]["x"]
                y0 = v["boundingBox"]["y"]
                x1 = x0 + v["boundingBox"]["w"]
                y1 = y0 + v["boundingBox"]["h"]
        return [x0,y0,x1,y1]

    for obj in objects:
        boxes = obj["localization"][vid_name]
        for k,v in boxes.items():
            if "boundingBox" not in v:
                continue
            x0 = min(v["boundingBox"]["x"],x0)
            y0 = min(v["boundingBox"]["y"],y0)
            x1 = max(x1,v["boundingBox"]["x"]+v["boundingBox"]["w"])
            y1 = max(y1,v["boundingBox"]["y"]+v["boundingBox"]["h"])
    return [x0,y0,x1,y1]

def gen_timeline(acts,targets,vid_name,typ="pred",timeline=None):
    if timeline == None:
        timeline = {}
    for act in acts:
        act_type = act["activity"]
        if act["activity"] not in targets:
            continue
        if vid_name not in act["localization"]:
            continue
        tmps = list(act["localization"][vid_name].keys())
        for tmp in tmps:
            if tmp == "bbox":
                continue
            if act["localization"][vid_name][tmp]==1:
                start=int(tmp)
            else:
                end = int(tmp)
        if "objects" in act:
            bbox = merge_box(act["objects"],vid_name,typ)
            if typ == "gt":
                timeline = split_box(act["objects"],vid_name,timeline,act_type)
        else:
            bbox = act["localization"][vid_name]["bbox"]
        for frame in range(start,end+1):
            if frame not in timeline:
                timeline[frame] = []
            timeline[frame].append({"act":act_type,"bbox":bbox,"type":typ})
    print(len(list(timeline.keys())))
    return timeline

def visualize(output_dir,video_path,video_name,acts,targets):
    video_file = os.path.join(video_path,video_name)
    timeline = gen_timeline(acts,targets,video_name)
    cap = AVIReader(video_file)
    sizex = cap.width
    sizey = cap.height
    writer = cv2.VideoWriter(output_dir+'/' + video_name.strip(".avi")+'_viz.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 30, (sizex,sizey))
    success, im = cap.read()
    cur_frame = 1
    while(success):
        if cur_frame in timeline:
            im = draw_box(im,timeline[cur_frame])
        writer.write(im)
        cur_frame+=1
        success,im = cap.read()
    # print(count)
    cap.release()
    writer.release()
    return

def visualize_single(output_dir,video_path,video_name,target,color_dict,set_x=852,set_y=480,conc_neg=False):
    video_file = os.path.join(video_path,video_name)
    timeline = gen_timeline(acts_gt,[target],video_name,"gt")
    if not len(list(timeline.keys())):
        return
    cap = AVIReader(video_file)
    sizex = cap.width
    sizey = cap.height
    if not os.path.exists(os.path.join(output_dir,video_name.strip(".avi"))):
        os.makedirs(os.path.join(output_dir,video_name.strip(".avi")))
    if os.path.exists(os.path.join(output_dir,video_name.strip(".avi"),'{}.mp4'.format(target))):
        return
    writer = cv2.VideoWriter(os.path.join(output_dir,video_name.strip(".avi"),'{}.mp4'.format(target)), cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 30, (set_x,set_y))
    success, im = cap.read()
    cur_frame = 1
    while success:
        if cur_frame in timeline:
            im = draw_box(im,timeline[cur_frame],color_dict)
            im = cv2.resize(im,(set_x,set_y))
        if conc_neg:
            im = cv2.resize(im,(set_x,set_y))
        writer.write(im)
        cur_frame+=1
        success,im = cap.read()
    cap.release()
    writer.release()
    return

def visualize_duo(output_dir,video_path,video_name,target,color_dict,set_x=1920,set_y=1080,conc_neg=False,vid_version="r13",record_full=True,show_gt = False):
    video_file = os.path.join(video_path,video_name)
    timeline = gen_timeline(acts_pred,[target],video_name,"lijun")
    timeline = gen_timeline(acts_gt,[target],video_name,"gt",timeline)
    if not len(list(timeline.keys())):
        return
    if vid_version == "r0":
        cap = AVIReader(video_file)
        sizex = cap.width
        sizey = cap.height
    elif vid_version == "r13":
        cap = cv2.VideoCapture(video_file)
        sizex = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        sizey = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    else:
        raise RuntimeError("video format {} is not supported".format(vid_version))

    if not os.path.exists(os.path.join(output_dir,video_name.strip(".avi"))):
        os.makedirs(os.path.join(output_dir,video_name.strip(".avi")))
    if os.path.exists(os.path.join(output_dir,video_name.strip(".avi"),'{}.mp4'.format(target))):
        return
    writer = cv2.VideoWriter(os.path.join(output_dir,video_name.strip(".avi"),'{}.mp4'.format(target)), cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 30, (set_x,set_y))
    success, im = cap.read()
    im = im*0.9
    cur_frame = 1
    while success:
        if cur_frame in timeline:
            im = draw_box(im,timeline[cur_frame],color_dict,show_gt)
            im = cv2.resize(im,(set_x,set_y))
        if conc_neg:
            im = cv2.resize(im,(set_x,set_y))
        if record_full:
            im = cv2.resize(im,(set_x,set_y))
        im = np.clip(im,0,255)
        writer.write(np.uint8(im))
        cur_frame+=1
        success,im = cap.read()
        if success:
            im = im*0.9
    # print(count)
    cap.release()
    writer.release()
    return

def visualize_multiduo(output_dir,video_path,video_name,targets,color_dict,set_x=1920,set_y=1080,conc_neg=False,vid_version="r13",record_full=True,show_gt=False,check_gt=False):
    video_file = os.path.join(video_path,video_name)
    timeline = gen_timeline(acts_pred,targets,video_name,"lijun")
    timeline = gen_timeline(acts_gt,targets,video_name,"gt",timeline)
    if not len(list(timeline.keys())):
        return
    if vid_version == "r0":
        cap = AVIReader(video_file)
        sizex = cap.width
        sizey = cap.height
    elif vid_version == "r13":
        cap = cv2.VideoCapture(video_file)
        sizex = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        sizey = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    else:
        raise RuntimeError("video format {} is not supported".format(vid_version))

    if not os.path.exists(os.path.join(output_dir,video_name.strip(".avi"))):
        os.makedirs(os.path.join(output_dir,video_name.strip(".avi")))
    if os.path.exists(os.path.join(output_dir,video_name.strip(".avi"),'all.mp4')):
        return
    writer = cv2.VideoWriter(os.path.join(output_dir,video_name.strip(".avi"),'all.mp4'), cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 30, (set_x,set_y))
    success, im = cap.read()
    im = im*0.5
    cur_frame = 1
    while success:
        if cur_frame in timeline:
            im = draw_box(im,timeline[cur_frame],color_dict,show_gt,check_gt)
            im = cv2.resize(im,(set_x,set_y))
        if conc_neg:
            im = cv2.resize(im,(set_x,set_y))
        if record_full:
            im = cv2.resize(im,(set_x,set_y))
        im = np.clip(im,0,255)
        writer.write(np.uint8(im))
        cur_frame+=1
        success,im = cap.read()
        if success:
            im = im*0.5
    # print(count)
    cap.release()
    writer.release()
    return

if __name__ == "__main__":
    gt_path = "/home/kevinq/datasets/KF1_DET/reference/kitware_eo-all_257.json"
    js = json.load(open(gt_path,"r"))

    pred_path = "/home/kevinq/exps/round3-filt/output-filt-vis.json"
    js_p = json.load(open(pred_path,"r"))

    act_types = []
    f = open("./labels_det.txt","r")
    lines = f.readlines()
    for line in lines:
        act_types.append(line.strip())
    color_list = [(255,0,0),(0,255,0),(0,0,255),(255,255,0),(0,255,255),(255,0,255),
    (127,255,0),(255,127,0),(127,0,255),(255,0,127),(0,255,127),(0,127,255),
    (50,150,250),(50,250,150),(150,250,50),(150,50,250),(250,150,50),(250,50,150),
    (100,100,200),(200,100,100),(100,200,100),(100,200,200),(200,100,200),(200,200,100),
    (50,150,0),(150,50,0),(50,0,150),(150,0,50),(0,150,50),(0,50,150),
    (100,0,0),(0,100,0),(100,100,0),(100,100,0),(100,0,100),(0,100,100),
    (255,255,255)]
    color_dict = {}
    for i in range(len(act_types)):
        act_type = act_types[i]
        color_dict[act_type] = color_list[i]

    global acts_gt
    acts_gt = js["activities"]
    global acts_pred
    acts_pred = js_p["activities"]

    files_gt = js["filesProcessed"]

    output_dir = "/home/kevinq/exps/round3-filt"
    video_path = "/mnt/data/MEVA/videos_r13"

    # visualize_duo(output_dir,video_path,"2018-03-11.16-20-00.16-25-00.bus.G331.avi","person_texts_on_phone",color_dict)
    files = ["2018-03-11.11-45-00.11-50-00.school.G299.avi",
        "2018-03-11.11-55-00.12-00-00.school.G423.avi",
        "2018-03-11.11-25-00.11-30-00.school.G420.avi"
    ]
    for f in files:
        visualize_multiduo(output_dir,video_path,f,act_types,color_dict)

    # args = []
    # for fname in files_gt:
    #     for act in act_types:
    #         args.append((output_dir,video_path,fname,act))

    # pool = Pool(20)

    # pool.starmap(visualize_single,args)

ext: py

reason: import numpy


text:
""" Feature Selection
Author : QiaoTian
Date   : 6th Oct 2016
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.metrics import mutual_info_score

def main():
    xpath = '/Users/qiaotian/Downloads/dataset/sample1/feature.txt'
    ypath = '/Users/qiaotian/Downloads/dataset/sample1/label.txt'
    y = pd.read_csv(ypath, sep=',', header=None).iloc[:,1].to_frame() # to_frame is used to convert series to dataframe
    X = pd.read_csv(xpath, sep=',', header=None).iloc[0:len(y),:]

    MI = np.zeros(len(X.iloc[0,:])) # mutual information
    for i in range(len(MI)):
        #print(X.iloc[:,i].as_matrix())
        #print(y.as_matrix().ravel())
        MI[i] = mutual_info_score(X.iloc[:, i].as_matrix(), y.as_matrix().ravel())

    plt.figure(1)
    plt.plot(MI)
    plt.xlabel("feature_index")
    plt.ylabel("MI")

    mi_rank = np.argsort(np.abs(MI))[-1::-1]
    plt.figure(2)
    plt.plot(MI[mi_rank])
    plt.ylabel("MI")
    plt.show()

if __name__ == '__main__':
    main()

max_stars_repo_name: qiaotian/UltrasoundTracking

max_issues_repo_name: qiaotian/UltrasoundTracking

max_forks_repo_name: qiaotian/UltrasoundTracking


text:
'''
Evaluate Direction of Arrival Algorithms
========================================

This example evaluate the performance of three direction of arrival (DOA)
algorithms on the recorded samples. It compares the discrepancy between the
output of the DOA algorithm and the calibrated locations (manual and
optimized).

The script requires `numpy`, `scipy`, `pyroomacoustics`, and `joblib` to run.

The three DOA algorithms are `MUSIC` [1], `SRP-PHAT` [2], and `WAVES` [3].

References
----------

.. [1] R. Schmidt, *Multiple emitter location and signal parameter estimation*,
    IEEE Trans. Antennas Propag., Vol. 34, Num. 3, pp 276--280, 1986

.. [2] J. H. DiBiase, *A high-accuracy, low-latency technique for talker localization
    in reverberant environments using microphone arrays*, PHD Thesis, Brown University, 2000

.. [3] E. D. di Claudio, R. Parisi, *WAVES: Weighted average of signal subspaces for
    robust wideband direction finding*, IEEE Trans. Signal Process., Vol. 49, Num. 10,
    2179--2191, 2001
'''
import json
import numpy as np
from scipy.io import wavfile
import pyroomacoustics as pra
from joblib import Parallel, delayed

# We choose the sample to use
sample_name = 'fq_sample0'
fn = 'segmented/{name}/{name}_spkr{spkr}_angle{angle}.wav'

fs = 16000  # This needs to be changed to 48000 for 'noise', 'sweep_lin', 'sweep_exp'
nfft = 256
stft_hop = 256

# We use a spherical grid with 30000 points
algorithms = {
        "SRP-PHAT" : { "algo_obj" : "SRP",   "n_grid" : 30000 },
        "MUSIC" :    { "algo_obj" : "MUSIC", "n_grid" : 30000 },
        "WAVES" :    { "algo_obj" : "WAVES", "n_grid" : 30000, "num_iter" : 10 },
        }

locate_kwargs = {
        "MUSIC" : {     "freq_range" : [1500.0, 3500.0], "n_bands" : 20 },
        "SRP-PHAT" : {  "freq_range" : [1500.0, 3500.0], "n_bands" : 20 },
        "WAVES" : {     "freq_range" : [1500.0, 3500.0], "n_bands" : 20 },
        }

def run_doa(angle, h, algo, doa_kwargs, freq_bins, speakers_numbering):
    ''' Run the doa localization for one source location and one algorithm '''

    # Prepare the DOA localizer object
    algo_key = doa_kwargs['algo_obj']
    doa = pra.doa.algorithms[algo_key](mic_array, fs, nfft, c=c, num_src=1, dim=3, **doa_kwargs)

    # get the loudspeaker index from its name
    spkr = speakers_numbering[h]

    # open the recording file
    filename = fn.format(name=sample_name, spkr=spkr, angle=angle)
    fs_data, data = wavfile.read(filename)

    if fs_data != fs:
        raise ValueError('Sampling frequency mismatch')

    # do time-freq decomposition
    X = np.array([
        pra.stft(signal, nfft, stft_hop, transform=np.fft.rfft).T
        for signal in data.T ])

    # run doa
    doa.locate_sources(X, freq_bins=freq_bins)
    col = float(doa.colatitude_recon[0])
    az = float(doa.azimuth_recon[0])

    # manual calibration groundtruth
    col_gt_man = locations['speakers_manual_colatitude'][h]
    az_gt_man = np.radians(int(angle))
    error_man = pra.doa.great_circ_dist(1., col, az, col_gt_man, az_gt_man)

    # optimized calibration groundtruth
    col_gt_opt = locations['sources'][h]['colatitude'][angle]
    az_gt_opt = locations['sources'][h]['azimuth'][angle]
    error_opt = pra.doa.great_circ_dist(1., col, az, col_gt_opt, az_gt_opt)

    print(algo, h, angle, ': Err Man=', error_man, 'Opt=', error_opt)

    return {
            'algo' : algo,
            'angle' : angle,
            'spkr_height' : h,
            'loc_man' : (col_gt_man, az_gt_man),
            'loc_opt' : (col_gt_opt, az_gt_opt),
            'loc_doa' : (col, az),
            'error_man' : float(error_man),
            'error_opt' : float(error_opt),
            }

def main_run(args):

    with open(args.calibration_file, 'r') as f:
        locations = json.load(f)

    c = locations['sound_speed_mps']

    # microphone locations
    mic_array = np.array(locations['microphones']).T

    # Recover the list of all sources locations
    spkr_azimuths = list(locations['sources']['low']['azimuth'].keys())
    spkr_height = list(locations['sources'].keys())

    errors = {
            'manual' : dict(zip(spkr_height, [[],[],[]])),
            'opt' : dict(zip(spkr_height, [[],[],[]]))
            }

    all_args = []
    for algo, doa_kwargs in algorithms.items():
        # select frequency bins uniformly in the range
        freq_hz = np.linspace(
            locate_kwargs[algo]['freq_range'][0],
            locate_kwargs[algo]['freq_range'][1],
            locate_kwargs[algo]['n_bands']
        )

        freq_bins = np.unique(
            np.array([int(np.round(f / fs * nfft))
                      for f in freq_hz])
        )

        # This will loop over all sources locations
        for h in spkr_height:
            for angle in spkr_azimuths:

                all_args.append(
                    (angle, h, algo, doa_kwargs, freq_bins, locations['speakers_numbering'])
                    )

    # Now run this in parallel with joblib
    results = Parallel(n_jobs=18)(delayed(run_doa)(*args) for args in all_args)

    with open(args.output, 'w') as f:
        json.dump(results, f)

def main_plot(args):
    ''' Plot the result of the Evaluation '''

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns

    df = pd.read_json(args.result)

    # Compute the average error with the grid used
    grid = pra.doa.GridSphere(n_points=30000)
    avg_error = np.degrees(grid.min_max_distance()[2])

    # remove the location columns, only keep error
    df = df[['algo', 'angle', 'spkr_height', 'error_man', 'error_opt']]
    df2 = pd.melt(df, value_vars=['error_man', 'error_opt'],
            value_name='Error', var_name='Calibration',
            id_vars=['algo','angle','spkr_height'])

    df2['Error'] = df2['Error'].apply(np.degrees)
    df2['Calibration'] = df2['Calibration'].replace({'error_man' : 'Manual', 'error_opt' : 'Optimized'})

    # Ignore WAVES as the algorithm does not work so well
    df2 = df2[df2.algo != 'WAVES']
    df2 = df2.rename(index=str, columns={'algo' : 'Algorithms'})

    sns.set_style('white')
    sns.set_context('paper')

    fig, ax = plt.subplots(figsize=(3.38846 / 2, 1.))

    pal = sns.cubehelix_palette(2, start=-0.5, rot=0.1, dark=0.4, light=.7, reverse=True)

    sns.boxplot(ax=ax, x="Algorithms", y="Error", hue="Calibration", data=df2, palette=pal, whis=[5, 95], linewidth=0.7, fliersize=1.)
    plt.legend(framealpha=0.8, frameon=True, loc='upper right', fontsize='xx-small')
    plt.xlabel('')
    plt.xticks(fontsize='x-small')
    plt.ylabel('Error [$^\circ$]', fontsize='x-small')
    plt.axhline(y=avg_error)
    sns.despine(ax=ax, offset=5)
    plt.tight_layout(pad=0.1)

    if args.save is not None:
        plt.savefig(args.save, dpi=300)

    plt.show()

if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser(description="Evaluate a few DOA algorithms")
    subparsers = parser.add_subparsers()

    parser_run = subparsers.add_parser('run', description='Run the DOA algorithms on the measurements')
    parser_run.set_defaults(func=main_run)
    parser_run.add_argument('calibration_file', type=str, help='The JSON file containing the calibrated locations')
    parser_run.add_argument('--output', '-o', type=str, default='doa_results.json', help='The JSON file where to save the results')

    parser_plot = subparsers.add_parser('plot', description='Plot the results of the evaluation')
    parser_plot.set_defaults(func=main_plot)
    parser_plot.add_argument('result', type=str, help='The JSON file containing the results')
    parser_plot.add_argument('-s', '--save', metavar='FILE', type=str, help='Save plot')

    args = parser.parse_args()
    args.func(args)

max_stars_repo_name: fakufaku/pyramic-dataset

max_issues_repo_name: fakufaku/pyramic-dataset

max_forks_repo_name: fakufaku/pyramic-dataset

reason: import numpy,from scipy


text:
import numpy as np
import unittest as ut

from scipy.stats import unitary_group

from qfactor.gates import Gate
from qfactor.tensors import CircuitTensor

class TestCalcEnvMatrix ( ut.TestCase ):

    def test_calc_env_matrix ( self ):
        u1 = unitary_group.rvs( 8 )
        u2 = u1.conj().T

        ct = CircuitTensor( u1, [] )

        env = ct.calc_env_matrix( [0, 1, 2] )
        self.assertTrue( np.allclose( env, u2  ) )

    def test_calc_env_matrix_invalid ( self ):
        u1 = unitary_group.rvs( 8 )
        u2 = u1.conj().T

        ct = CircuitTensor( u1, [] )

        self.assertRaises( ValueError, ct.calc_env_matrix, [0, 1, 2, 3] )
        self.assertRaises( TypeError, ct.calc_env_matrix, "a" )

if __name__ == "__main__":
    ut.main()

max_stars_repo_name: edyounis/QFactor

max_issues_repo_name: edyounis/qFactor

max_forks_repo_name: edyounis/qFactor


text:
from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct, ArrayContraction, ArrayAdd, \
    ArrayDiagonal
from sympy.tensor.array.expressions.conv_array_to_matrix import convert_array_to_matrix
from sympy.tensor.array.expressions.conv_indexed_to_array import convert_indexed_to_array
from sympy.tensor.array.expressions.conv_matrix_to_array import convert_matrix_to_array
from sympy.utilities.exceptions import sympy_deprecation_warning

sympy_deprecation_warning(
    """
    Array expressions inside the codegen module are deprecated. Use the
    experimental module in sympy.tensor.array.expressions instead
    """,
    deprecated_since_version="1.8",
    active_deprecations_target='deprecated-arrayexpressions',
)

CodegenArrayTensorProduct = ArrayTensorProduct
CodegenArrayContraction = ArrayContraction
CodegenArrayElementwiseAdd = ArrayAdd
CodegenArrayDiagonal = ArrayDiagonal

recognize_matrix_expression = convert_array_to_matrix
parse_matrix_expression = convert_matrix_to_array
parse_indexed_expression = convert_indexed_to_array

max_stars_repo_name: val-verde/python-sympy

max_issues_repo_name: val-verde/python-sympy

max_forks_repo_name: val-verde/python-sympy

reason: import sympy,from sympy


text:
import pickle
import random
import argparse

import numpy as np
import pandas as pd

from scipy import stats
from pprint import pprint
from functools import partial
from collections import defaultdict

parser = argparse.ArgumentParser(description='Forecast fundraising')
parser.add_argument('--scenarios', type=int, help='How many Monte Carlo simulations to run?', default=10000)
parser.add_argument('--interval', type=float, help='Width of the credible interval for the gift, conditional on giving', default=0.9)
# TODO: Should we jitter the chance of giving as well, to account for correlated errors / make the interval wider?

error_mean_doc = ('Specify an additional constant error term that is normally distributed with an interval specified by `CREDIBLE_INTERVAL`,'
                  'a mean specified `CONSTANT_ERROR_MEAN`, and a width of the interval specified by `CONSTANT_ERROR_WIDTH` (width is on both sides of the mean).'
                  'Set `CONSTANT_ERROR_WIDTH` to 0 to ignore.')
parser.add_argument('--constant_error_mean',type=float, help=error_mean_doc, default=0)
parser.add_argument('--constant_error_width', type=float, help='Width of the interval for the constant error term', default=0)
parser.add_argument('--csv', type=str, help='Define the relative path to the CSV with the donor information', default='forecast-example.csv')
parser.add_argument('--path', type=str, help='Define a custom path for the saved model outputs', default='')
parser.add_argument('--save', type=bool, help='Set to False to not save (overwrite) model outputs', default=True)
parser.add_argument('--update_every', type=int, help='How many steps to run before updating?', default=1000)
parser.add_argument('--verbose', type=bool, help='Set to True to get scenario-specific output', default=False)
args = parser.parse_args()

N_SCENARIOS = args.scenarios
CREDIBLE_INTERVAL = args.interval
CONSTANT_ERROR_MEAN = args.constant_error_mean
CONSTANT_ERROR_WIDTH = args.constant_error_width
VERBOSE = args.verbose
CSV = args.csv
SAVE = args.save
PATH = args.path
update_every = args.update_every

SCENARIO_RANGES = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99]  # Generate printed output for these percentiles

def parse_currency(currency):
    if isinstance(currency, int) or isinstance(currency, float):
        return currency
    if currency == ' $ -   ':
        return 0
    if isinstance(currency, str):
        currency = currency.replace('$', '').replace(' ', '').replace(',', '')
        return float(currency)
    else:
        return 0

def print_money(money):
    if money >= 0:
        return '${:,.2f}'.format(money)
    else:
        return '-${:,.2f}'.format(-money)

# Either round to nearest hundred if <1000 or nearest thousand if >= 1000
def round_to_nearest(num):
    n_digits = len(str(num))
    n_digits = n_digits - 1
    if n_digits > 3:
        n_digits = 3
    return np.round(num, -n_digits)

def parse_percent(percent):
    if isinstance(percent, int) or isinstance(percent, float):
        return percent
    if isinstance(percent, str):
        return float(percent.replace('%', '')) / 100
    else:
        return 0

def normal_sample(low, high, interval):
    if (low > high) or (high < low):
        raise ValueError
    if low == high:
        return low
    else:
        mu = (high + low) / 2
        cdf_value = 0.5 + 0.5 * interval
        normed_sigma = stats.norm.ppf(cdf_value)
        sigma = (high - mu) / normed_sigma
        return np.random.normal(mu, sigma)

def lognormal_sample(low, high, interval):
    if (low > high) or (high < low):
        raise ValueError
    if low == high:
        return low
    else:
        log_low = np.log(low)
        log_high = np.log(high)
        mu = (log_high + log_low) / 2
        cdf_value = 0.5 + 0.5 * interval
        normed_sigma = stats.norm.ppf(cdf_value)
        sigma = (log_high - mu) / normed_sigma
        return np.random.lognormal(mu, sigma)

raw_data = pd.read_csv(CSV)
raw_data = raw_data[['Donor', '2021 Gift Potential - Low', '2021 Gift Potential - High',
                     '2021 Likelihood of Gift', '2022 Gift Potential - Low',
                     '2022 Gift Potential - High', '2022 Likelihood of Gift']]
raw_data = raw_data[raw_data['Donor'] != 'Current Donors'][raw_data['Donor'] != 'Possible Donors']
fundraising_data = {}
for index, row in raw_data.iterrows():
    donor = row['Donor']
    if donor and isinstance(donor, str):
        y2021_low = parse_currency(row['2021 Gift Potential - Low'])
        y2021_high = parse_currency(row['2021 Gift Potential - High'])
        y2021_prob = parse_percent(row['2021 Likelihood of Gift'])
        y2022_low = parse_currency(row['2022 Gift Potential - Low'])
        y2022_high = parse_currency(row['2022 Gift Potential - High'])
        y2022_prob = parse_percent(row['2022 Likelihood of Gift'])

        fundraising_data[donor] = {'2021': {'low': y2021_low,
                                            'high': y2021_high,
                                            'prob': y2021_prob},
                                   '2022': {'low': y2022_low,
                                            'high': y2022_high,
                                            'prob': y2022_prob}}

y2021_all_scenario_totals = []
y2022_all_scenario_totals = []
y2021_fundraising_totals = defaultdict(partial(np.array, 0))
y2022_fundraising_totals = defaultdict(partial(np.array, 0))
joint_fundraising_totals = defaultdict(partial(np.array, 0))
constant_errors = []

for s in range(N_SCENARIOS):
    if s % update_every == 0:
        if VERBOSE:
            print('-')
            print('### SCENARIO {} ###'.format(s + 1))
        else:
            print('... Completed {}/{}'.format(s + 1, N_SCENARIOS))

    y2021_donations = []
    y2022_donations = []

    for donor, donation in fundraising_data.items():
        if random.random() <= donation['2021']['prob']:
            y2021_donation = lognormal_sample(low=donation['2021']['low'],
                                              high=donation['2021']['high'],
                                              interval=CREDIBLE_INTERVAL)
        else:
            y2021_donation = 0

        if random.random() <= donation['2022']['prob']:
            y2022_donation = lognormal_sample(low=donation['2022']['low'],
                                              high=donation['2022']['high'],
                                              interval=CREDIBLE_INTERVAL)
        else:
            y2022_donation = 0

        y2021_donation = round_to_nearest(y2021_donation)
        y2022_donation = round_to_nearest(y2022_donation)

        if s % update_every == 0 and VERBOSE:
            print('{} gives {} in 2021 and {} in 2022'.format(donor,
                                                              print_money(y2021_donation),
                                                              print_money(y2022_donation)))
        y2021_donations.append(y2021_donation)
        y2022_donations.append(y2022_donation)
        y2021_fundraising_totals[donor] = np.append(y2021_fundraising_totals[donor], y2021_donation)
        y2022_fundraising_totals[donor] = np.append(y2022_fundraising_totals[donor], y2022_donation)
        joint_fundraising_totals[donor] = np.append(joint_fundraising_totals[donor], y2021_donation + y2022_donation)

    y2021_total_raised = sum(y2021_donations)
    if CONSTANT_ERROR_WIDTH > 0:
        scenario_constant_error = normal_sample(low=-CONSTANT_ERROR_WIDTH/2.0 - CONSTANT_ERROR_MEAN,
                                                high=CONSTANT_ERROR_WIDTH/2.0 - CONSTANT_ERROR_MEAN,
                                                interval=CREDIBLE_INTERVAL)
    else:
        scenario_constant_error = 0

    y2022_total_raised = sum(y2022_donations) + scenario_constant_error

    if s % update_every == 0 and VERBOSE:
        print('CONSTANT ERROR TERM: {}'.format(print_money(scenario_constant_error)))
        print('TOTAL RAISED IN 2021: {}'.format(print_money(y2021_total_raised)))
        print('TOTAL RAISED IN 2022: {}'.format(print_money(y2022_total_raised)))

    y2021_all_scenario_totals.append(y2021_total_raised)
    y2022_all_scenario_totals.append(y2022_total_raised)
    constant_errors.append(scenario_constant_error)

if VERBOSE:
    print('-')
    print('-')
    print('-')

joint_scenarios = np.array(y2021_all_scenario_totals) + np.array(y2022_all_scenario_totals)
y2021_percentiles = np.percentile(y2021_all_scenario_totals, SCENARIO_RANGES)
y2022_percentiles = np.percentile(y2022_all_scenario_totals, SCENARIO_RANGES)
joint_percentiles = np.percentile(joint_scenarios, SCENARIO_RANGES)

print('SCENARIO 2021 -- {}'.format(' -- '.join(['{}%: {}'.format(s, print_money(y2021_percentiles[i])) for i, s in enumerate(SCENARIO_RANGES)])))
print('SCENARIO 2022 -- {}'.format(' -- '.join(['{}%: {}'.format(s, print_money(y2022_percentiles[i])) for i, s in enumerate(SCENARIO_RANGES)])))
print('SCENARIO 2021+2022 -- {}'.format(' -- '.join(['{}%: {}'.format(s, print_money(joint_percentiles[i])) for i, s in enumerate(SCENARIO_RANGES)])))

if SAVE:
    print('... Saving 1/8')
    pickle.dump(fundraising_data, open('{}fundraising_data.p'.format(PATH), 'wb'))
    print('... Saving 2/8')
    pickle.dump(y2021_fundraising_totals, open('{}y2021_fundraising_totals.p'.format(PATH), 'wb'))
    print('... Saving 3/8')
    pickle.dump(y2022_fundraising_totals, open('{}y2022_fundraising_totals.p'.format(PATH), 'wb'))
    print('... Saving 4/8')
    pickle.dump(joint_fundraising_totals, open('{}joint_fundraising_totals.p'.format(PATH), 'wb'))
    print('... Saving 5/8')
    pickle.dump(y2021_all_scenario_totals, open('{}y2021_all_scenario_totals.p'.format(PATH), 'wb'))
    print('... Saving 6/8')
    pickle.dump(y2022_all_scenario_totals, open('{}y2022_all_scenario_totals.p'.format(PATH), 'wb'))
    print('... Saving 7/8')
    pickle.dump(joint_scenarios, open('{}joint_scenarios.p'.format(PATH), 'wb'))
    print('... Saving 8/8')
    pickle.dump(constant_errors, open('{}constant_errors.p'.format(PATH), 'wb'))

max_issues_repo_name: rethinkpriorities/fundraising-forecast

max_forks_repo_name: rethinkpriorities/fundraising-forecast


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 30 11:03:00 2020

@author: seungmin
"""
import numpy as np
import gym
import matplotlib.pyplot as plt

from matplotlib import animation

def display_frames_as_gif(frames):

    plt.figure(figsize=(frames[0].shape[1]/72.0, frames[0].shape[0]/72.0),
               dpi=72)
    patch = plt.imshow(frames[0])
    plt.axis('off')

    def animate(i):
        patch.set_data(frames[i])

    anim = animation.FuncAnimation(plt.gcf(), animate, frames=len(frames),
                                   interval=50)

    anim.save('movie_cartpole.mp4')

frames=[]
env = gym.make('CartPole-v0')
observation = env.reset()

for step in range(200):
    frames.append(env.render(mode='rgb_array'))
    action = np.random.choice(2)
    observation, reward, done, info = env.step(action)
    print(observation, reward, done, info)

print("Saving all frames to .mp4 format...")
display_frames_as_gif(frames)

env.close()

max_stars_repo_name: seungmin-h/deep-reinforcement-learning

max_issues_repo_name: seungmin-h/deep-reinforcement-learning

max_forks_repo_name: seungmin-h/deep-reinforcement-learning


text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import os
import numpy as np
import json
import sklearn.base
import bhtsne
from sklearn import manifold

class MDS(sklearn.base.BaseEstimator):

    def __init__(self, n_componets=2, dissimilarity='euclidean', random_state=6, n_jobs=4):
        self.n_components = n_componets
        self.dissimilarity = dissimilarity
        self.random_state = random_state
        self.n_jobs = n_jobs

    def compute(self, x):
        mds = manifold.MDS(n_components=self.n_components, dissimilarity=self.dissimilarity,
                           random_state=self.random_state, n_jobs=self.n_jobs)
        return mds.fit_transform(x)

class BHTSNE(sklearn.base.BaseEstimator, sklearn.base.TransformerMixin):

    def __init__(self, dimensions=2, perplexity=30.0, theta=0.5, rand_seed=-1):
        self.dimensions = dimensions
        self.perplexity = perplexity
        self.theta = theta
        self.rand_seed = rand_seed

    def fit_transform(self, x):
        return bhtsne.tsne(
            x.astype(np.float64), dimensions=self.dimensions, perplexity=self.perplexity,
            theta=self.theta, rand_seed=self.rand_seed)

# tensorflow projections -> npy file"
def tf2npy(state_path):
    f = open(state_path, 'r')
    json_dict = json.load(f)[0]
    projections = json_dict['projections']

    results = []
    for projection in projections:
        x = projection['tsne-0']
        y = projection['tsne-1']
        z = projection['tsne-2']
        temp = [x, y, z]
        results.append(temp)

    return results

def main():
    inputdir = '../bin/data/cfd'

    # t-SNE
    # inputfile = os.path.join(inputdir, 'lfw-vgg_center.npy')
    # X = np.load(inputfile)
    # tsne = BHTSNE(dimensions=3, perplexity=30.0, theta=0.5, rand_seed=6)
    # Y = tsne.fit_transform(X)

    # # t-SNE (tensorflow)
    inputfile = os.path.join(inputdir, 'embedding/state.txt')
    Y = np.asarray(tf2npy(inputfile))

    np.save(os.path.join(inputdir, 'cfd-vgg-tsne_tf.npy'), Y)
    print 'data shape: {}'.format(Y.shape)
    print 'complete.'

if __name__ == '__main__':
    main()

max_stars_repo_name: yugosato/Interface

max_issues_repo_name: yugosato/FaceRetrieval

max_forks_repo_name: yugosato/FaceRetrieval


text:
#!/usr/bin/python3
"""Changes the frame id of the GNSS message and changes the covariance for
metric coordinates.
"""
import numpy as np
import rospy
from sensor_msgs.msg import NavSatFix

class Node:

    def __init__(self):
        rospy.init_node("carina_demo_gps_fixer")

        self.navsat_fix_subscriber = rospy.Subscriber("/carina/driver/septentrio/fix", NavSatFix, self.subscriber_navsat_fix, queue_size=10)
        self.navsat_fix_publisher = rospy.Publisher("/carina/driver/septentrio/fix_corrected", NavSatFix, queue_size=10)

        self.default_confidence_xy = 0.1
        self.default_confidence_altitude = 10.0 # meters

    def subscriber_navsat_fix(self, msg : NavSatFix):

        # Prepare message
        out_msg = NavSatFix()
        out_msg.header = msg.header
        out_msg.header.frame_id = "gps"
        out_msg.status = msg.status
        out_msg.latitude = msg.latitude
        out_msg.longitude = msg.longitude
        out_msg.altitude = msg.altitude
        xy_variance = ( self.default_confidence_xy / 3.0 ) ** 2.0
        altitude_variance = ( self.default_confidence_altitude / 3.0 ) ** 2.0
        out_msg.position_covariance = np.diag([xy_variance, xy_variance, altitude_variance]).flatten()
        out_msg.position_covariance_type = msg.position_covariance_type

        # Publish
        self.navsat_fix_publisher.publish(out_msg)

    def spin(self):
        rospy.spin()

if __name__ == "__main__":
    node = Node()
    node.spin()

max_stars_repo_name: cabraile/Mapless-MCL-ROS

max_issues_repo_name: cabraile/Mapless-MCL-ROS

max_forks_repo_name: cabraile/Mapless-MCL-ROS


text:
import sys
import time
import pickle
import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.layers import Dense, Activation, Flatten
from tensorflow.keras.callbacks import TensorBoard

def build_model():
    mdl = keras.models.Sequential()
    mdl.add(Dense(64, input_dim=12, activation=tf.nn.softplus))
    mdl.add(Dense(64, activation=tf.nn.softplus))
    mdl.add(Dense(32, activation=tf.nn.softplus))
    mdl.add(Dense(32, activation=tf.nn.softplus))
    mdl.add(Dense(1, activation=tf.nn.relu))
    mdl.add(Dense(1, activation=tf.nn.softplus))

    mdl.compile(
        optimizer='adam',
        loss='binary_crossentropy',
        metrics=['accuracy']
    )

    return mdl

def train_model(mdl):
    xtrain = pickle.load(open('saved/normal/xtrain.pickle', 'rb'))
    ytrain = pickle.load(open('saved/normal/ytrain.pickle', 'rb'))
    xtest = pickle.load(open('saved/normal/xtest.pickle', 'rb'))
    ytest = pickle.load(open('saved/normal/ytest.pickle', 'rb'))

    name = 'cartpole-{}'.format(int(time.time()))
    tensorboard = TensorBoard(log_dir='saved/logs/{}'.format(name))

    mdl.fit(xtrain, ytrain, batch_size=32, epochs=3, callbacks=[tensorboard])
    print(mdl.evaluate(xtest, ytest, verbose=1))
    count = 0
    for i in range(len(xtest)):
        if int(mdl.predict(np.array([xtest[i]]))) == ytest[i]:
            print(xtest[i])
            count += 1
            if count > 10:
                quit()

    return mdl

if __name__ == '__main__':
    mdl = build_model()
    mdl = train_model(mdl)
    mdl.save('saved/latest_model.h5')

max_stars_repo_name: third-meow/openai-gym-exs

max_issues_repo_name: third-meow/openai-gym-exs

max_forks_repo_name: third-meow/openai-gym-exs


text:
#!/usr/bin/env python3
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def heart_3d(x,y,z):
   return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3
def plot_implicit(fn, bbox=(-1.5, 1.5)):
    xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    A = np.linspace(xmin, xmax, 100)
    B = np.linspace(xmin, xmax, 40)
    A1, A2 = np.meshgrid(A, A)
    for z in B:
        X, Y = A1, A2
        Z = fn(X, Y, z)
        cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',))
    for y in B:
        X, Z = A1, A2
        Y = fn(X, y, Z)
        cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red',))
    for x in B:
        Y, Z = A1, A2
        X = fn(x, Y, Z)
        cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red',))
    ax.set_zlim3d(zmin, zmax)
    ax.set_xlim3d(xmin, xmax)
    ax.set_ylim3d(ymin, ymax)
    plt.show()
if __name__ == '__main__':
    plot_implicit(heart_3d)


text:
from haxballgym.game.common_values import COLLISION_FLAG_WALL, COLLISION_FLAG_ALL
from haxballgym.game.objects.base import PhysicsObject
import numpy as np

class Plane(PhysicsObject):
    """
    A class to represent the state of a plane from the game.
    """

    def __init__(self, data_object: dict, data_stadium: dict):

        if data_object is None:
            data_object = {}

        self.collision_group: int = self.transform_collision_dict(
            data_object.get("cGroup")
        )
        self.collision_mask: int = self.transform_collision_dict(
            data_object.get("cMask")
        )
        self.normal: np.ndarray = np.array(data_object.get("normal"), dtype=np.float)
        self.distance_origin: float = data_object.get("dist")
        self.bouncing_coefficient: float = data_object.get("bCoef")
        self.trait = data_object.get("trait")

        self.apply_trait(self, data_stadium)
        self.apply_default_values()

    def apply_default_values(self):
        """
        Applies the default values to the plane if they are none
        """
        if self.bouncing_coefficient is None:
            self.bouncing_coefficient = 1
        if self.collision_group is None:
            self.collision_group = COLLISION_FLAG_WALL
        if self.collision_mask is None:
            self.collision_mask = COLLISION_FLAG_ALL

max_stars_repo_name: HaxballGym/HaxballGym

max_issues_repo_name: HaxballGym/HaxballGym

max_forks_repo_name: HaxballGym/HaxballGym


text:
# --------------
# Importing header files
import numpy as np
import warnings

warnings.filterwarnings('ignore')

#New record
new_record=[[50,  9,  4,  1,  0,  0, 40,  0]]

#Reading file
data = np.genfromtxt(path, delimiter=",", skip_header=1)

#Code starts here
# Importing header files
import numpy as np

#New record
new_record=[[50,  9,  4,  1,  0,  0, 40,  0]]

#Code starts here

#Loading data file and saving it into a new numpy array
data = np.genfromtxt(path, delimiter=",", skip_header=1)

print(data.shape)

#Concatenating the new record to the existing numpy array
census=np.concatenate((data, new_record),axis = 0)

print(census.shape)

#Code starts here

#Subsetting the array to include only 'Age' column
age=census[:,0]
#Finding the max value of age
max_age=age.max()
print("Max Age= ",max_age)
print(age)
#Find the min value of age
min_age=age.min()
print("Min Age= ",min_age)

#Find the mean of age
age_mean=age.mean()
print("Age Average= ", age_mean)

#Find the standard deviation of age
age_std=age.std()
print("Age Standard Deviation= ",age_std)

#Code starts here

#Creating new subsets based on 'Age'
race_0=census[census[:,2]==0]
race_1=census[census[:,2]==1]
race_2=census[census[:,2]==2]
race_3=census[census[:,2]==3]
race_4=census[census[:,2]==4]

#Finding the length of the above created subsets
len_0=len(race_0)
len_1=len(race_1)
len_2=len(race_2)
len_3=len(race_3)
len_4=len(race_4)

#Printing the length of the above created subsets
print('Race_0: ', len_0)
print('Race_1: ', len_1)
print('Race_2: ', len_2)
print('Race_3: ', len_3)
print('Race_4: ', len_4)

#Storing the different race lengths with appropriate indexes
race_list=[len_0, len_1,len_2, len_3, len_4]

#Storing the race with minimum length into a variable
minority_race=race_list.index(min(race_list))

#Subsetting the array based on the age
senior_citizens=census[census[:,0]>60]

#Calculating the sum of all the values of array
working_hours_sum=senior_citizens.sum(axis=0)[6]

#Finding the length of the array
senior_citizens_len=len(senior_citizens)

#Finding the average working hours
avg_working_hours=working_hours_sum/senior_citizens_len

#Printing the average working hours
print((avg_working_hours))

#Creating an array based on 'education' column
high=census[census[:,1]>10]

#Finding the average pay
avg_pay_high=high[:,7].mean()

#Printing the average pay
print(avg_pay_high)

#Creating an array based on 'education' column
low=census[census[:,1]<=10]

#Finding the average pay
avg_pay_low=low[:,7].mean()

#Printing the average pay
print(avg_pay_low)


text:
#!/usr/bin/env python

# set up Python environment: numpy for numerical routines, and matplotlib for plotting
import numpy as np
import matplotlib.pyplot as plt

# The caffe module needs to be on the Python path;
#  we'll add it here explicitly.
import sys
caffe_root = '../../'  # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')

import caffe
# If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.

import os
model_weights = './c3d_ucf101_iter_5000.caffemodel'
if not os.path.isfile(model_weights):
    print "[Error] model weights can't be found."
    sys.exit(-1)

print 'model found.'
caffe.set_mode_gpu()
caffe.set_device(1)
model_def = './c3d_ucf101_deploy.prototxt'

net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)

# load the mean ImageNet image (as distributed with Caffe) for subtraction
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1)  # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)

# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

# convert from HxWxCxL to CxLxHxW (L=temporal length)
length = 16
transformer.set_transpose('data', (2,3,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

# set the size of the input (we can skip this if we're happy
#  with the default; we can also change it later, e.g., for different batch sizes)
net.blobs['data'].reshape(1,        # batch size
                          3,         # 3-channel (BGR) images
                          length,        # length of a clip
                          112, 112)  # image size

clip = np.tile(
        caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'),
        (16,1,1,1)
        )
clip = np.transpose(clip, (1,2,3,0))
print "clip.shape={}".format(clip.shape)

transformed_image = transformer.preprocess('data', clip)

#plt.imshow(image)

# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image
for l in range(0, length):
    print "net.blobs['data'].data[0,:,{},:,:]={}".format(
            l,
            net.blobs['data'].data[0,:,l,:,:]
            )

### perform classification
output = net.forward()

output_prob = output['prob'][0]  # the output probability vector for the first image in the batch

print 'predicted class is:', output_prob.argmax()

max_stars_repo_name: CXXT-Projects/video-caffe

max_issues_repo_name: CXXT-Projects/video-caffe

max_forks_repo_name: CXXT-Projects/video-caffe


text:
import sys
sys.path.append('..')
import numpy as np
import parameters.aerosonde_parameters as MAV
import tools.signals as sigs
from tools.rotations import Quaternion2Rotation, Quaternion2Euler, Euler2Quaternion
import mav_dynamics
import mavsim_python_chap5_model_coef as chap5
import matplotlib.pyplot as plt

# Define sim parameters
dt = 0.01
uav = mav_dynamics.mavDynamics(dt)

T0 = 0
Tf = 5

n = int(np.floor((Tf-T0)/dt))

alpha = [uav._alpha*180/np.pi]
beta = [uav._beta*180/np.pi]
phi = [MAV.phi0*180/np.pi]
theta = [MAV.theta0*180/np.pi]
psi = [MAV.psi0*180/np.pi]
gamma = [uav.true_state.gamma*180/np.pi]
t_history = [T0]

ail_doublet = sigs.signals(amplitude=0.2,frequency=1.0,start_time=1,duration=1,dc_offset = chap5.u_trim[1])
for ii in range(n):
    delta_a = ail_doublet.doublet(dt*ii)
    delta = np.array([chap5.u_trim[0],delta_a,chap5.u_trim[2],chap5.u_trim[3]])
    wind = np.array([[0.], [0.], [0.], [0.], [0.], [0.]])
    uav.update(delta,wind)
    alpha.append(uav._alpha*180/np.pi)
    beta.append(uav._beta*180/np.pi)
    t_history.append(ii*dt)
    phi_t,theta_t,psi_t = Quaternion2Euler(uav._state[6:10])
    phi.append(phi_t*180/np.pi)
    theta.append(theta_t*180/np.pi)
    psi.append(psi_t*180/np.pi)
    gamma.append(uav.true_state.gamma*180/np.pi)

# close all figures

print(uav._state)
plt.close('all')

plt.figure()
plt.plot(t_history,alpha,'r',label = 'aoa')
plt.xlabel('t')
plt.ylabel('angle [deg]')
plt.legend()
plt.show()

plt.figure()
plt.plot(t_history,beta,'r',label = 'aos')
plt.xlabel('t')
plt.ylabel('angle [deg]')
plt.legend()
plt.show()

plt.figure()
plt.plot(t_history,phi,'r',label = 'phi')
plt.plot(t_history,theta,'b',label = 'theta')
plt.plot(t_history,psi,'g',label = 'psi')
plt.xlabel('t')
plt.ylabel('angle [deg]')
plt.legend()
plt.show()


text:
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from keras.models import Sequential
from keras.optimizers import sgd_experimental
from keras.layers import Dense
from keras.datasets import cifar10
import matplotlib.pyplot as plt
import argparse
import numpy as np

ap=argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
                help="sciezka do wystawienia wykresu")

args=vars(ap.parse_args())

print("[INFO] ładowanie biblioteki Cifar-10...")

((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0
trainX = trainX.reshape((trainX.shape[0], 3072))
testX = testX.reshape((testX.shape[0], 3072))

lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

trainY=lb.fit_transform(trainY)
testY=lb.transform(testY)

etykiety =['samolot','samochod','ptak','kot','jelen','pies',
           'zaba','kon','owca','ciezarowka']

model=Sequential()
model.add(Dense(1024,input_shape=(3072,),activation="relu"))
model.add(Dense(512,activation="relu"))
model.add(Dense(10,activation="softmax"))

print("[INFO] trenowanie sieci...")
sgd=sgd_experimental.SGD(0.01)
model.compile(loss="categorical_crossentropy",optimizer=sgd,
              metrics=['accuracy'])
H = model.fit(trainX, trainY,validation_data=(testX,testY),
              epochs=100, batch_size=32)

print("[INFO] ewaluacja...")
predictions =model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1),
                            predictions.argmax(axis=1),
                            target_names=etykiety))

plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0,100),H.history["loss"], label="strata przy trenowaniu")
plt.plot(np.arange(0,100),H.history["val_loss"], label="wartość straty")
plt.plot(np.arange(0,100),H.history["accuracy"], label="dopasowanie")
plt.plot(np.arange(0,100),H.history["val_accuracy"], label="wartość dopasowania")
plt.title("Trenowanie modelu i straty")
plt.xlabel("Epoka #")
plt.ylabel("strata/dokładność")
plt.legend()
plt.savefig(args["output"])


text:
import torch
import torch.nn as nn
from copy import deepcopy
from search_cells import NASNetSearchCell as SearchCell
import numpy as np
import pdb

# The macro structure is based on NASNet
class NASNetworkPRDARTS(nn.Module):

    def __init__(self, C, N, steps, multiplier, stem_multiplier, num_classes, search_space, affine,
                 track_running_stats):
        super(NASNetworkPRDARTS, self).__init__()
        self._C = C
        self._layerN = N
        self._steps = steps
        self._multiplier = multiplier
        self.stem = nn.Sequential(
            nn.Conv2d(3, C * stem_multiplier, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(C * stem_multiplier))

        # config for each layer
        layer_channels = [C] * N + [C * 2] + [C * 2] * (N - 1) + [C * 4] + [C * 4] * (N - 1)
        layer_reductions = [False] * N + [True] + [False] * (N - 1) + [True] + [False] * (N - 1)

        num_edge, edge2index = None, None
        C_prev_prev, C_prev, C_curr, reduction_prev = C * stem_multiplier, C * stem_multiplier, C, False

        self.cells = nn.ModuleList()
        for index, (C_curr, reduction) in enumerate(zip(layer_channels, layer_reductions)):
            cell = SearchCell(search_space, steps, multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev,
                              affine, track_running_stats)
            print(index)
            print(search_space, steps, multiplier, C_prev_prev, C_prev, C_curr, reduction, reduction_prev, affine,
                  track_running_stats)
            # pdb.set_trace()
            if num_edge is None:
                num_edge, edge2index = cell.num_edges, cell.edge2index
            else:
                assert num_edge == cell.num_edges and edge2index == cell.edge2index, 'invalid {:} vs. {:}.'.format(
                    num_edge, cell.num_edges)
            self.cells.append(cell)
            C_prev_prev, C_prev, reduction_prev = C_prev, multiplier * C_curr, reduction

        self.op_names = deepcopy(search_space)
        self._Layer = len(self.cells)
        self.edge2index = edge2index
        self.lastact = nn.Sequential(nn.BatchNorm2d(C_prev), nn.ReLU(inplace=True))
        self.global_pooling = nn.AdaptiveAvgPool2d(1)
        self.classifier = nn.Linear(C_prev, num_classes)

        self.arch_normal_parameters = nn.Parameter(1e-3 * torch.randn(num_edge, len(search_space)))
        self.arch_reduce_parameters = nn.Parameter(1e-3 * torch.randn(num_edge, len(search_space)))

        self.num_edge = num_edge
        self.tau = 10
        self.limit_a = -0.1
        self.limit_b = 1.1
        self.k = 2
        self.dropout_pro = 0.2
        self.regular_lambda1=1.0
        self.regular_lambda2=0.5
        self.regular_lambda3=0.5
        self.threshold = 0.2

    def set_threshold(self, threshold):
        self.threshold = threshold

    def set_dropout(self, dropout_pro):
        self.dropout_pro = dropout_pro

    def get_weights(self):
        xlist = list(self.stem.parameters()) + list(self.cells.parameters())
        xlist += list(self.lastact.parameters()) + list(self.global_pooling.parameters())
        xlist += list(self.classifier.parameters())
        return xlist

    def set_tau(self, tau):
        self.tau = tau

    def set_k(self, k):
        self.k = k

    def get_tau(self):
        return self.tau

    def set_sparsity(self, sparsity):
        self.sparsity = sparsity

    def set_ab(self, a, b):
        self.limit_a = a
        self.limit_b = b

    def set_weights(self, a, b):
        self.visit_weight = a
        self.loss_weight = b

    def get_alphas(self):
        return [self.arch_normal_parameters, self.arch_reduce_parameters]

    def get_a(self):
        return self.limit_a

    def get_b(self):
        return self.limit_b

    def get_sparsity(self):
        return self.sparsity

    def sampling_hard_gate(self, xins):
        # probs = torch.sigmoid(xins - self.tau * np.log(-self.limit_a/self.limit_b))

        logits = (xins + 0.5) / self.tau
        probs = torch.sigmoid(logits) * (self.limit_b - self.limit_a) + self.limit_a
        probs = probs.clamp_(min=0.0, max=1.0)
        return probs

    def show_alphas(self):
        with torch.no_grad():
            probs_A = self.sampling_hard_gate(self.arch_normal_parameters)
            probs_B = self.sampling_hard_gate(self.arch_reduce_parameters)

            A = 'arch-normal-parameters :\n{:}'.format(probs_A.cpu())
            B = 'arch-reduce-parameters :\n{:}'.format(probs_B.cpu())
        return '{:}\n{:}'.format( A, B)

    def get_message(self):
        string = self.extra_repr()
        for i, cell in enumerate(self.cells):
            string += '\n {:02d}/{:02d} :: {:}'.format(i, len(self.cells), cell.extra_repr())
        return string

    def extra_repr(self):
        return ('{name}(C={_C}, N={_layerN}, steps={_steps}, multiplier={_multiplier}, L={_Layer})'.format(
            name=self.__class__.__name__, **self.__dict__))

    def genotype(self):
        def _parse(weights):
            gene = []
            for i in range(self._steps):
                edges = []
                for j in range(2 + i):
                    node_str = '{:}<-{:}'.format(i, j)
                    ws = weights[self.edge2index[node_str]]
                    for k, op_name in enumerate(self.op_names):
                        if op_name == 'none': continue
                        edges.append((op_name, j, ws[k]))
                edges = sorted(edges, key=lambda x: -x[-1])
                selected_edges = edges[:2]
                gene.append(tuple(selected_edges))
            return gene

        with torch.no_grad():
            gene_normal = _parse(torch.softmax(self.arch_normal_parameters, dim=-1).cpu().numpy())
            gene_reduce = _parse(torch.softmax(self.arch_reduce_parameters, dim=-1).cpu().numpy())
        return {'normal': gene_normal,
                'normal_concat': list(range(2 + self._steps - self._multiplier, self._steps + 2)),
                'reduce': gene_reduce,
                'reduce_concat': list(range(2 + self._steps - self._multiplier, self._steps + 2))}

    def set_regular_lambda(self, regular_lambda1,  regular_lambda2,  regular_lambda3):
        self.regular_lambda1, self.regular_lambda2, self.regular_lambda3=regular_lambda1,  regular_lambda2,  regular_lambda3

    def get_probability(self):
        para = self.tau * np.log(-self.limit_a / self.limit_b)
        normal_probability = (self.arch_normal_parameters - para).sigmoid().mean().cpu().item()
        # pdb.set_trace()
        # normal_probability =normal_probability.cpu().item()
        reduction_probability = (self.arch_reduce_parameters - para).sigmoid().mean().cpu().item()
        logits = (self.arch_normal_parameters + 0.5) / self.tau
        probs = torch.sigmoid(logits) * (self.limit_b - self.limit_a) + self.limit_a
        normal_probability2 = probs.clamp_(min=0.0, max=1.0).mean().cpu().item()
        logits = (self.arch_reduce_parameters + 0.5) / self.tau
        probs = torch.sigmoid(logits) * (self.limit_b - self.limit_a) + self.limit_a
        reduction_probability2 = probs.clamp_(min=0.0, max=1.0).mean().cpu().item()
        probabilitys = [normal_probability, reduction_probability, \
                        normal_probability2, reduction_probability2]

        return probabilitys

    def get_regularization(self,normal_hardwts_idx,reduce_hardwts_idx):
        para = self.tau * np.log(-self.limit_a / self.limit_b)

        ## expectation activation probability
        normal_cell_activation = (self.arch_normal_parameters - para).sigmoid().mul(normal_hardwts_idx)
        reduction_cell_activation = (self.arch_reduce_parameters - para).sigmoid().mul(reduce_hardwts_idx)

        ##  expectation activation probability of skip group, idx 1 refers to skip connection
        skip_group_activation = 0.5 * normal_cell_activation[:,1].sum()/normal_hardwts_idx[:,1].sum() if normal_hardwts_idx[:,1].sum()>0 else 0.0
        skip_group_activation += (0.5 * reduction_cell_activation[:,1].sum()/reduce_hardwts_idx[:,1].sum() if reduce_hardwts_idx[:,1].sum()>0 else 0.0)

        # ##  expectation activation probability of skip group, idx 1 refers to skip connection
        # edge_number_normal = normal_hardwts_idx.sum() - normal_hardwts_idx[:,1].sum()
        # edge_number_reduction = reduce_hardwts_idx.sum() - reduce_hardwts_idx[:,1].sum()
        # nonskip_group_activation = (0.5/edge_number_normal) * (normal_cell_activation[:,0].sum() + normal_cell_activation[:,2:].sum()) if edge_number_normal else 0.0
        # nonskip_group_activation += ((0.5/edge_number_reduction) *(reduction_cell_activation[:,0].sum() + reduction_cell_activation[:,2:].sum()) if edge_number_reduction else 0.0)

        ## pooling
        edge_number_normal_pooling = normal_hardwts_idx[:, 6:8].sum()
        edge_number_reduction_pooling = reduce_hardwts_idx[:, 6:8].sum()
        pooling_group_activation = (0.5 / edge_number_normal_pooling) * normal_cell_activation[:, 6:8].sum() \
                                         if edge_number_normal_pooling else 0.0
        pooling_group_activation += ((0.5 / edge_number_reduction_pooling) * reduction_cell_activation[:, 6:8].sum() \
                                         if edge_number_reduction_pooling else 0.0)

        ##  expectation activation probability of skip group, idx 1 refers to skip connection
        edge_number_normal = normal_hardwts_idx.sum() - normal_hardwts_idx[:,1].sum() - edge_number_normal_pooling
        edge_number_reduction = reduce_hardwts_idx.sum() - reduce_hardwts_idx[:,1].sum() - edge_number_reduction_pooling
        nonskip_group_activation = (0.5/edge_number_normal) * (normal_cell_activation[:,0].sum() + normal_cell_activation[:,2:6].sum()) if edge_number_normal else 0.0
        nonskip_group_activation += ((0.5/edge_number_reduction) *(reduction_cell_activation[:,0].sum() + reduction_cell_activation[:,2:6].sum()) if edge_number_reduction else 0.0)

        ## expectation of path regularization
        # if idx.is_cuda:
        #     rand_idx = rand_idx.cuda()
        # pdb.set_trace()
        path_idx = torch.tensor([self.edge2index['0<-1'],self.edge2index['1<-2'],self.edge2index['2<-3'],self.edge2index['3<-4']])
        # pdb.set_trace()
        select_idx = path_idx[normal_hardwts_idx[path_idx,2:6].sum(dim=1).nonzero().squeeze()]
        # select_idx = path_idx[AA[path_idx, 2:6].sum(dim=1).nonzero().squeeze()]
        path_normal_activation = 0.0
        if select_idx.numel()==1:
            # pdb.set_trace()
            path_normal_activation = normal_cell_activation[select_idx,2:6].sum().mul(1.0/normal_hardwts_idx[select_idx,2:6].sum())
        if select_idx.numel() > 1:
            path_normal_activation = normal_cell_activation[select_idx, 2:6].sum(dim=1).mul(
                1.0 / normal_hardwts_idx[select_idx, 2:6].sum(dim=1)).prod()

        path_reducation_activation = 0.0
        select_idx = path_idx[reduce_hardwts_idx[path_idx,2:6].sum(dim=1).nonzero().squeeze()]
        if select_idx.numel()==1:
            path_reducation_activation = reduction_cell_activation[select_idx,2:6].sum().mul(1.0/reduce_hardwts_idx[select_idx,2:6].sum())
        if select_idx.numel() > 1:
            path_reducation_activation = reduction_cell_activation[select_idx, 2:6].sum(dim=1).mul(
                1.0 / reduce_hardwts_idx[select_idx, 2:6].sum(dim=1)).prod()

        path_activation = 0.5*(path_normal_activation + path_reducation_activation)

        ## combine together
        skip_group_activation = skip_group_activation - self.threshold if skip_group_activation > self.threshold else 0.0
        nonskip_group_activation = nonskip_group_activation - self.threshold if nonskip_group_activation > self.threshold else 0.0
        pooling_group_activation = pooling_group_activation - self.threshold if pooling_group_activation > self.threshold else 0.0
        regularizer = self.regular_lambda1 * skip_group_activation \
                      + self.regular_lambda2 * (nonskip_group_activation + pooling_group_activation) \
                      - self.regular_lambda3 * path_activation

        # regularizer = self.regular_lambda1 * skip_group_activation \
        #               + self.regular_lambda2 * (nonskip_group_activation + pooling_group_activation) \
        #               - self.regular_lambda3 * path_activation
        return regularizer
        #skip_group_activation, nonskip_group_activation, path_activation

    def forward(self, inputs):
        def get_gumbel_prob(xins):
            while True:
                # pdb.set_trace()
                gumbels = torch.rand_like(xins)  # .to('cuda')
                gumbels = torch.log(gumbels) - torch.log(1 - gumbels)
                # pdb.set_trace()
                # logits = (xins.sigmoid().log() + gumbels) / self.tau
                logits = (xins + gumbels) / self.tau

                probs = torch.sigmoid(logits) * (self.limit_b - self.limit_a) + self.limit_a
                idx = probs.clamp_(min=0.0, max=1.0)
                # pdb.set_trace()
                if self.dropout_pro <= 0.0:
                    if idx.sum():
                        one_num = idx.ge(1.0).int().sum(dim=1)## number of operation > 1.0, we need to randomly select
                        if one_num.ge(self.k).sum():  ## number of operation > 1.0 is larger than k, we need to randomly selectk ops
                            rand_idx = torch.randperm(idx.shape[1])
                            if idx.is_cuda:
                                rand_idx = rand_idx.cuda()
                            idx = idx[:, rand_idx]
                            _, top_idx = torch.topk(idx, dim=1, k=self.k)
                            rand_idx = rand_idx.repeat(idx.shape[0], 1)
                            top_idx = rand_idx.gather(dim=1, index=top_idx)
                        else:
                            _, top_idx = torch.topk(idx, dim=1, k=self.k)
                        hardwts_idx = torch.zeros_like(idx).scatter_(-1, top_idx, 1.0)
                        # without gradient:
                        #     hardwts_idx = torch.zeros_like(idx).scatter_(-1, top_idx, 1.0)
                        hardwts = hardwts_idx - probs.detach() + probs
                        break
                else:  # dropout
                    ber_drop = - torch.bernoulli(torch.zeros_like(idx), self.dropout_pro)  ## add -1 to the death nodes to avoid selected
                    idx = idx + ber_drop
                    rand_idx = torch.randperm(idx.shape[1])
                    if idx.is_cuda:
                        rand_idx = rand_idx.cuda()
                    idx = idx[:, rand_idx]
                    _, top_idx = torch.topk(idx, dim=1, k=self.k)
                    rand_idx = rand_idx.repeat(idx.shape[0], 1)
                    top_idx = rand_idx.gather(dim=1, index=top_idx)
                    hardwts_idx = torch.zeros_like(idx).scatter_(-1, top_idx, 1.0)
                    hardwts = hardwts_idx - probs.detach() + probs
                    break
            return hardwts_idx, hardwts

        normal_hardwts_idx, normal_hardwts = get_gumbel_prob(self.arch_normal_parameters)
        reduce_hardwts_idx, reduce_hardwts = get_gumbel_prob(self.arch_reduce_parameters)

        s0 = s1 = self.stem(inputs)
        for i, cell in enumerate(self.cells):
            if cell.reduction:
                hardwts = reduce_hardwts
            else:
                hardwts = normal_hardwts
            s0, s1 = s1, cell.forward_prdarts(s0, s1, hardwts)

        out = self.lastact(s1)
        out = self.global_pooling(out)
        out = out.view(out.size(0), -1)
        logits = self.classifier(out)

        return out, logits, self.get_regularization(normal_hardwts_idx, reduce_hardwts_idx)

max_stars_repo_name: salesforce/PR-DARTS

max_issues_repo_name: salesforce/PR-DARTS

max_forks_repo_name: salesforce/PR-DARTS


text:
#! /usr/bin/env python

# Copyright 2019 Luca Fedeli, Maxence Thevenet
#
# This file is part of WarpX.
#
# License: BSD-3-Clause-LBNL

import yt
import numpy as np
import scipy.stats as st
import sys

# This script checks if photons initialized with Breit Wheeler process enabled
# do actually have an exponentially distributed optical depth

# Tolerance
tol = 1e-2

def check():
    filename = sys.argv[1]
    data_set = yt.load(filename)

    all_data = data_set.all_data()
    res_tau = all_data["photons", 'particle_tau']

    loc, scale = st.expon.fit(res_tau)

    # loc should be very close to 0, scale should be very close to 1
    assert(np.abs(loc - 0) < tol)
    assert(np.abs(scale - 1) < tol)

def main():
    check()

if __name__ == "__main__":
    main()

max_stars_repo_name: guj/WarpX

max_issues_repo_name: guj/WarpX

max_forks_repo_name: guj/WarpX

reason: import numpy,import scipy


text:
import numpy as np
from setting import tag2id, id2tag, unigram_template, bigram_template, mydictPath, target_epochs
from tools import gendata_fromfile, create_mydictKey, gen_ans_fromMaxScore
import pickle

class CRF:
    def __init__(self, dictPath=mydictPath):
        # scoreMap存在就先读取
        try:
            print("正在载入模型，请稍候：")
            fp = open(dictPath, "rb")
            self.crf_dict = pickle.load(fp)
            fp.close()
            print("加载模型成功")
        except IOError:
            self.crf_dict = {'Init': 1}
            print("模型不存在，初始化成功")
        self.unigram = unigram_template
        self.bigram = bigram_template

    def calUnigram(self, sentence, pos, cur_tag):
        score = 0
        for i in range(len(self.unigram)):
            key = create_mydictKey(self.unigram[i], str(i), sentence, pos, cur_tag)
            if key in self.crf_dict.keys():
                score += self.crf_dict[key]
        return score

    def calBigram(self, sentence, pos, last_tag, cur_tag):
        score = 0
        for i in range(len(self.bigram)):
            key = create_mydictKey(self.bigram[i], str(i), sentence, pos, last_tag + cur_tag)
            if key in self.crf_dict.keys():
                score += self.crf_dict[key]
        return score

    def start_train(self, epochs=target_epochs):
        sentences, tags = gendata_fromfile(FILEPATH=f"./2014_corpus.txt")
        for i in range(epochs):
            wrong_num = 0
            test = 0
            length = len(sentences)
            for j in range(len(sentences)):
                if j % 1000 == 0:
                    print(f"epoch:{i+1}/{epochs}" + f" process:{j}/{length}" + (" rate:%.2f%%" % (100 * j/length)))
                sentence = sentences[j]
                test += len(sentence)
                result = tags[j]
                wrong_num += self.train(sentence, result)
            corr_num = test - wrong_num
            self.savemydict()
            print(f'EPOCH: {i + 1} END, Dict Saved')
            print(f"epoch {i+1}" + f" accuracy: %.2f%%" % (100 * corr_num / test))

    def train(self, sentence, tags):
        predict_tags = self.getansTag(sentence)
        length = len(sentence)
        wrong_count = 0
        for i in range(length):
            if predict_tags[i] != tags[i]:
                tag_predict = predict_tags[i]
                tag_real = tags[i]
                wrong_count += 1
                for j in range(len(self.unigram)):
                    key_err = create_mydictKey(self.unigram[j], j, sentence, i, tag_predict)
                    if key_err in self.crf_dict.keys():
                        self.crf_dict[key_err] -= 1
                    else:
                        self.crf_dict[key_err] = -1
                    key_corr = create_mydictKey(self.unigram[j], j, sentence, i, tag_real)
                    if key_corr in self.crf_dict.keys():
                        self.crf_dict[key_corr] += 1
                    else:
                        self.crf_dict[key_corr] = 1
                for j in range(len(self.bigram)):
                    if 1 < i:
                        key_err = create_mydictKey(self.bigram[j], str(j), sentence, i, predict_tags[i - 1:i + 1])
                        key_corr = create_mydictKey(self.bigram[j], str(j), sentence, i, tags[i - 1:i + 1])
                    else:
                        key_err = create_mydictKey(self.bigram[j], str(j), sentence, i, tag_predict)
                        key_corr = create_mydictKey(self.bigram[j], str(j), sentence, i, tag_real)

                    if key_err in self.crf_dict.keys():
                        self.crf_dict[key_err] -= 1
                    else:
                        self.crf_dict[key_err] = -1
                    if key_corr in self.crf_dict.keys():
                        self.crf_dict[key_corr] += 1
                    else:
                        self.crf_dict[key_corr] = 1

        return wrong_count

    def getansTag(self, sentence):
        length = len(sentence)
        pre_tags = np.zeros((len(tag2id), length), dtype=str)
        max_values = np.zeros((len(tag2id), length))
        for i in range(length):
            for j in range(len(tag2id)):
                cur_tag = id2tag[j]
                if i == 0:
                    unigram_v = self.calUnigram(sentence, 0, cur_tag)
                    bigram_v = self.calBigram(sentence, 0, ' ', cur_tag)
                    max_values[j][i] = unigram_v + bigram_v
                    pre_tags[j][i] = ''
                else:
                    scores = np.zeros(len(tag2id))
                    for k in range(len(tag2id)):
                        pre_tag = id2tag[k]
                        last_word = max_values[k][i - 1]
                        unigram_v = self.calUnigram(sentence, i, cur_tag)
                        bigram_v = self.calBigram(sentence, i, pre_tag, cur_tag)
                        scores[k] = last_word + unigram_v + bigram_v
                    max_values[j][i] = np.max(scores)
                    pre_tags[j][i] = id2tag[np.argmax(scores)]
        ans = gen_ans_fromMaxScore(length, max_values, pre_tags)
        return ans

    def savemydict(self):
        with open(mydictPath, "wb") as fp:
            pickle.dump(self.crf_dict, fp)

max_stars_repo_name: Lionel-Peiran/CRF

max_issues_repo_name: Lionel-Peiran/CRF

max_forks_repo_name: Lionel-Peiran/CRF


text:
import linescan
from PIL import Image
import numpy as np
import pylab
import os
import glob

#14.01.09-18.09: created report.py to assemble individual folder report functions
#14.01.13-15.39: added raw (where every sweep is shown) average plots for each structure.
#14.01.13-15.39: added colormap to color-code sweep time

def reportCompareLS(scans,CH="dGR",show=True):
  if len(scans)==0: return
  pylab.figure(figsize=(10,8))
  for i in range(len(scans)):
    scan=scans[i]
    path,stName,label,color=scan
    if type(color)==str:
      print("setting color to",color)
    else:
      c=float(i)/(len(scans)+1)
      color=pylab.cm.jet(c)
    LS=linescan.LineScan(path)
    LS.project()
    if not label:
      label=path.split("-")[-1]
      label+=" ("+LS.config["timeFirst"].split(" ",1)[1]+")"
    for structure in LS.structures:
      if not stName==structure:
        continue
      for y in range(len(LS.structures[structure][CH])):
        pylab.plot(LS.Xs,LS.structures[structure][CH][y],color=color,alpha=.2)
      pylab.plot(LS.Xs,np.average(LS.structures[structure][CH],axis=0),label=label,color=color,alpha=1,lw=2)
  if CH=='dGR':
    LS.prettyFormat(r"Average ($\Delta$G)/R",legendSize=12,maxmin=False)
  else:
    LS.prettyFormat(r"Raw PMT Intensity [CH=%s]"%CH,legendSize=12,rawPMT=True,maxmin=False)
  if show:
    pylab.show()

def reportSummaryLS(path):

  out="""

%s

  """%(os.path.basename(path))

  outpath=path+"/SAG/html/"
  if not os.path.exists(outpath):
    os.makedirs(outpath)

  LS=linescan.LineScan(path)
  LS.project() #force all projections

  # reference image
  im=Image.open(path+"/References/"+LS.config["ref"])
  im.save(outpath+"ref.jpg")

  # merged image
  data=np.zeros((LS.imProj.shape[1],LS.imProj.shape[2],3))
  data[:,:,0]=linescan.npContrast(LS.imProj[0,:,:]) #RED
  data[:,:,1]=linescan.npContrast(LS.imProj[1,:,:]) #GREEN
  data[:,:,2]=linescan.npContrast(LS.imProj[0,:,:]) #BLUE
  imRG=Image.fromarray(data.astype(np.uint8),mode="RGB")
  linescan.imSave(imRG,outpath+'/all_RG.png',size=(None,200))
  linescan.imSave(linescan.imFromNumpyArray(data[:,:,0]),outpath+'all_R.png',size=(None,200))
  linescan.imSave(linescan.imFromNumpyArray(data[:,:,1]),outpath+'all_G.png',size=(None,200))
  out+='
'
  out+=''
  out+=' '
  out+=''
  out+='
'

  # indidual channel gray projection
  LS.peaksGraph(channel=0).save(outpath+'all_Rp.png')
  LS.peaksGraph(channel=1).save(outpath+'all_Gp.png')

  #dict_keys(['Gb', 'G', 'dGRavS', 'R', 'dGRpkS', 'dGRavA', 'dR',
              #'dGR', 'dG', 'dGRpkA', 'Rb'])

  # python graph of average line for each structure
  pylab.figure(figsize=(8,6))
  for structure in LS.structures:
    pylab.plot(LS.Xs,np.average(LS.structures[structure]["dGR"],axis=0),label=structure)
  LS.prettyFormat("All Structures",legendSize=10)
  pylab.savefig(outpath+'plot_all.png',dpi=80)
  pylab.close()
  out+='
'

  # image for each structure
  for structure in LS.structures:

    out+="""

Structure: %STR
SWEEPS

    """.replace("%STR",structure)

    #DGR AV
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["dGR"].shape[0]):
      pylab.plot(LS.Xs,LS.structures[structure]["dGR"][i],'b-',alpha=.2)
    pylab.plot(LS.Xs,np.average(LS.structures[structure]["dGR"],axis=0),'b-',lw=2)
    pylab.title(r"Average ($\Delta$G)/R")
    pylab.xlabel("Experiment time (ms)")
    pylab.ylabel(r"($\Delta$G)/R")
    pylab.savefig(outpath+'st_dGRav_'+structure,dpi=60)
    pylab.close()

    #DGR RAW
    #14.01.24-14.51: fixed issue with colorbar having n-1 colors
    #14.01.24-14.51: rather than show time points as seconds, show it sa sweep number
    #timePoints=np.arange(LS.structures[structure]["dGR"].shape[0]+1)/LS.structures[structure]["R"].shape[0]
    timePoints=np.arange(LS.structures[structure]["dGR"].shape[0]+1)
    if "absoluteTime" in LS.config.keys():
      timePoints=timePoints*LS.config["absoluteTime"]
    CLR=pylab.contourf([timePoints],timePoints,cmap=pylab.cm.rainbow)
    pylab.clf() #clear the colorbar plot
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["dGR"].shape[0]):
      c=float(i)/LS.structures[structure]["dGR"].shape[0]
      pylab.plot(LS.Xs,LS.structures[structure]["dGR"][i],alpha=.8,color=pylab.cm.rainbow(c))
    pylab.title(r"($\Delta$G)/R by Sweep")
    pylab.xlabel("Experiment time (ms)")
    pylab.ylabel(r"($\Delta$G)/R")
    pylab.colorbar(CLR)
    pylab.savefig(outpath+'st_dGRraw_'+structure,dpi=60)
    pylab.close()

    #RED RAW
    CLR=pylab.contourf([timePoints],timePoints,cmap=pylab.cm.rainbow)
    pylab.clf() #clear the colorbar plot
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["R"].shape[0]):
      c=float(i)/LS.structures[structure]["R"].shape[0]
      pylab.plot(LS.Xs,LS.structures[structure]["R"][i],color=pylab.cm.rainbow(c))
    LS.prettyFormat("CH1 (red) PMT Value",rawPMT=True,baseline=False)
    pylab.colorbar(CLR)
    pylab.savefig(outpath+'st_Rraw_'+structure,dpi=60)
    pylab.close()

    #RED AV
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["R"].shape[0]):
      pylab.plot(LS.Xs,LS.structures[structure]["R"][i],'r:',alpha=.8)
    pylab.plot(LS.Xs,np.average(LS.structures[structure]["R"],axis=0),'r-')
    LS.prettyFormat("CH1 (red) PMT Value",rawPMT=True,baseline=False)
    pylab.savefig(outpath+'st_Rav_'+structure,dpi=60)
    pylab.close()

    #GREEN RAW
    CLR=pylab.contourf([timePoints],timePoints,cmap=pylab.cm.rainbow)
    pylab.clf() #clear the colorbar plot
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["G"].shape[0]):
      c=float(i)/LS.structures[structure]["G"].shape[0]
      pylab.plot(LS.Xs,LS.structures[structure]["G"][i],color=pylab.cm.rainbow(c))
    pylab.plot(LS.Xs,np.average(LS.structures[structure]["G"],axis=0),'g-')
    LS.prettyFormat("CH2 (green) PMT Value",rawPMT=True,baseline=False)
    pylab.colorbar(CLR)
    pylab.savefig(outpath+'st_Graw_'+structure,dpi=60)
    pylab.close()

    #GREEN AV
    pylab.figure(figsize=(8,6))
    for i in range(LS.structures[structure]["G"].shape[0]):
      pylab.plot(LS.Xs,LS.structures[structure]["G"][i],'g:',alpha=.8)
    pylab.plot(LS.Xs,np.average(LS.structures[structure]["G"],axis=0),'g-')
    LS.prettyFormat("CH2 (green) PMT Value",rawPMT=True,baseline=False)
    pylab.savefig(outpath+'st_Gav_'+structure,dpi=60)
    pylab.close()

    #stackable dGR
    #for i in range(LS.structures[structure]["dGR"].shape[0]):
      #data=LS.structures[structure]["dGR"][i],

    for s in LS.config["structures"]:
      if s[2]==structure:
        x1,x2,sName=s
        h=int(x2-x1)

    out2="
"
    chunk=LS.imGauss[:,LS.config["guiSelectedSweeps"],x1:x2,:]
    chunk=chunk[1] #GREEN ONLY
    chunk=linescan.npContrast(chunk) #do this only once, before splitting things
    for y in range(chunk.shape[0]):
      fname="st_G_swp_%03d_%s.png"%(y,structure)
      linescan.imFromNumpyArray(chunk[y,:,:],outpath+fname)
      out2+='
%d
'%(y,fname)
    out2+="
"

    out=out.replace("SWEEPS",out2)

  out+="

"
  out+="
"*5
  out+=""+linescan.dictSave(LS.config).replace("\n",'
')+""

  out+=""

  f=open(outpath+"index.html",'w')
  f.write(out)
  f.close()

  print("DONE")

if __name__=="__main__":
  #testPath=r'X:\Data\2P01\2014\2014-01\2014-01-11 SH rat HPC stability\LineScan-01112014-1240-728'
  #reportSummaryLS(testPath)
  scans="""X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1223
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1224
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1225
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1226
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1227
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1228
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1229
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1231
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1233
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1230
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1232
X:/Data/2P01/2014/2014-01/2014-01-29 rat pup/LineScan-01292014-1431-1234""".split("\n")
  print(scans)
  reportCompareLS(scans)

max_stars_repo_name: toddsahagian/Microscopy

max_issues_repo_name: toddsahagian/Microscopy

max_forks_repo_name: toddsahagian/Microscopy


text:
from scipy.io import loadmat
import gsw
import numpy as np
import matplotlib.pyplot as plt

from src import Config, Section
from src import grid_field, SA_CT_from_sigma0_spiciness0

plt.ion()
cf = Config()

sec4 = Section()
z = sec4.lvls.copy()[0, :, :]
spice = sec4.lvls.copy()[1, :, :]

plot_i = [37, 40, 48]

#fig, ax = plt.subplots()
#ax.plot(sec4.x_a / 1e3, spice[plot_i].T)
#ax.plot(sec4.x_a / 1e3, np.sort(spice[plot_i])[::-1])

fig, ax = plt.subplots()
ax.plot(sec4.x_a / 1e3, z[plot_i].T)
ax.set_ylim(150, 0)


text:
# https://stackoverflow.com/questions/20133479/how-to-draw-directed-graphs-using-networkx-in-python
import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [
        ("A", "B"),
        ("A", "C"),
        ("D", "B"),
        ("E", "C"),
        ("E", "F"),
        ("B", "H"),
        ("B", "G"),
        ("B", "F"),
        ("C", "G"),
    ]
)

val_map = {"A": 1.0, "D": 0.5714285714285714, "H": 0.0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

# Specify the edges you want here
red_edges = [("A", "C"), ("E", "C")]
edge_colours = ["black" if not edge in red_edges else "red" for edge in G.edges()]
black_edges = [edge for edge in G.edges() if edge not in red_edges]

# Need to create a layout when doing
# separate calls to draw nodes and edges
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(
    G, pos, cmap=plt.get_cmap("jet"), node_color=values, node_size=500
)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edgelist=red_edges, edge_color="r", arrows=True)
nx.draw_networkx_edges(G, pos, edgelist=black_edges, arrows=False)
plt.show(block=False)
plt.pause(10)

max_stars_repo_name: russellw/ml

max_issues_repo_name: russellw/ml

max_forks_repo_name: russellw/ml

reason: import networkx


text:
import gym
import gym_panda #need to be imported !!
import random
import numpy as np
import matplotlib.pyplot as plt

import execnet

from pilco.models import PILCO
from pilco.controllers import RbfController, LinearController
from pilco.rewards import ExponentialReward
import PILCO_AC_utils as utils
import tensorflow as tf
from gpflow import set_trainable
np.random.seed(0)
#from examples.utils import policy, rollout#, Normalised_Env

from save_load_utils import load_pilco_model
from save_load_utils import save_pilco_model
from save_load_utils import save_minimal_pilco_model

np.set_printoptions(precision=2)

"""
This script is running the admittance controller in the PILCO/Gym-interface

1) An agent is first performing random actions (no training)
	- the possible actions are increasing/deacreasing of damping and stiffness

2) The data is then used to make a model of the mapping between actions and states

3) The resulting model is used to find a policy for how to adjust damping and stiffness
"""

#rewards
Force_precision = 5
noise_reduction = 1

Fd = 3

if __name__ == "__main__":
	print('started PILCO_admittance')

	SUBS = "5"
	frac_T = 1/15
	num_random_rollouts = 2
	num_rollouts = 16
	save_path = '/home/martin/PILCO/Compliant_panda/trained models/100Hz_Admittance_SUBS-5_rbfPolicy'

	print('starting first rollout')

	X1,Y1, _, _,T,data_for_plotting = utils.rollout_panda(utils.gw, pilco=None, random=True, SUBS=SUBS, render=False) # function imported from PILCO (EXAMPLES/UTILS)

	np.save('/home/martin/Figures master/data from runs/random' + '/admittance_100Hz_.npy',data_for_plotting)
	#utils.plot_run(data_for_plotting)

	"""
	These initial rollouts with "random=True" is just gathering data so that we can make a model of the systems dynamics (performing random actions)
		X1: states and actions recorded at each iteration
		Y1: change in states between each iteration
	"""

	print('gathering more data...')

	for i in range(1,num_random_rollouts):
		print('	- At rollout ',i+1, ' out of ',num_random_rollouts)
		X1_, Y1_,_,_,_, data_for_plotting = utils.rollout_panda(utils.gw, pilco=None, random=True, SUBS=SUBS, render=False)
		X1 = np.vstack((X1, X1_))
		Y1 = np.vstack((Y1, Y1_))
		#utils.plot_run(data_for_plotting)

	state_dim = Y1.shape[1]
	control_dim = X1.shape[1] - state_dim

	norm_env_m = np.mean(X1[:,:state_dim],0) #input for normalised_env
	norm_env_std = np.std(X1[:,:state_dim], 0)  #input for normalised_env

	X = np.zeros(X1.shape)
	X[:, :state_dim] = np.divide(X1[:, :state_dim] - np.mean(X1[:,:state_dim],0), np.std(X1[:,:state_dim], 0))  # states are normalised
	X[:, state_dim],X[:, state_dim+1] = X1[:,-2],X1[:,-1] # control inputs are not normalised
	Y = np.divide(Y1 , np.std(X1[:,:state_dim], 0)) # state-changes are normalised

	"""
	X consists of normalised states along with the control inputs (u)
	Y consists of the normalised state-transitions
	"""

	# THE BLOCK BELOW IS USED WHEN YOU WANT TO USE PREVIOUSLY RECORDED DATA

	m_init =  np.transpose(X[0,:-control_dim,None])
	S_init =  0.001 * np.eye(state_dim)
	controller = RbfController(state_dim=state_dim, control_dim=control_dim, num_basis_functions=15)
	rbf=True #controller = LinearController(state_dim=state_dim, control_dim=control_dim)
	target = np.zeros(state_dim)
	target[0] = Fd #desired force (must also be specified in the controller as this one is just related to rewards)
	W_diag = np.zeros(state_dim)
	W_diag[0] = Force_precision
	#W_diag[3] = noise_reduction

	R = ExponentialReward(state_dim=state_dim, t=np.divide(target - norm_env_m, norm_env_std),W=np.diag(W_diag))

	pilco = PILCO((X, Y), controller=controller, horizon=int(T*frac_T), reward=R, m_init=m_init, S_init=S_init)

	best_r = 0
	all_Rs = np.zeros((X.shape[0], 1))
	for i in range(len(all_Rs)):
		all_Rs[i,0] = R.compute_reward(X[i,None,:-control_dim], 0.001 * np.eye(state_dim))[0]  #

	ep_rewards = np.zeros((len(X)//T,1))

	for i in range(len(ep_rewards)):
		ep_rewards[i] = sum(all_Rs[i * T: i*T + T])
	"""
	for model in pilco.mgpr.models:
		model.likelihood.variance.assign(0.05)
		set_trainable(model.likelihood.variance, False)
	"""
	r_new = np.zeros((T, 1))
	print('doing more rollouts, optimizing the model between each run')
	for rollouts in range(num_rollouts):
		print('	- optimizing models...')
		pilco.optimize_models()
		print('	- optimizing policy...')
		pilco.optimize_policy(maxiter=300, restarts=0) #(maxiter=100, restarts=3) # 4 minutes when (1,0) #RESTART PROBLEMATIC? (25)
		#import pdb; pdb.set_trace()
		X_new, Y_new, _, _, plot_data = utils.rollout_panda_norm(utils.gw, state_dim, X1, pilco=pilco, SUBS=SUBS, render=False, RBF_status =rbf)

		X = np.vstack((X, X_new)); Y = np.vstack((Y, Y_new))
		pilco.mgpr.set_data((X, Y))
		save_pilco_model(pilco,X1,X,Y,target, W_diag,save_path,rbf=True)
		#save_minimal_pilco_model(pilco,X1,X_new,Y_new,target, W_diag,save_path,rbf=False)

		for i in range(len(X_new)):
			r_new[:, 0] = R.compute_reward(X_new[i,None,:-control_dim], 0.001 * np.eye(state_dim))[0] #-control_dim

		total_r = sum(r_new)
		#_, _, r = pilco.predict(m_init, S_init, T)

		#print("Total ", total_r, " Predicted: ", r)

		all_Rs = np.vstack((all_Rs, r_new)); ep_rewards = np.vstack((ep_rewards, np.reshape(total_r,(1,1))))
		np.save(save_path + '/admittance_data_' + str(rollouts) + '.npy',plot_data)
		#utils.plot_run(plot_data)

	for i in range(10):
		X_new, Y_new, _, _, data_for_plotting = utils.rollout_panda_norm(utils.gw, state_dim, X1, pilco=pilco, SUBS=SUBS, render=False,RBF_status =rbf)
		np.save(save_path + '/admittance_data_final_' + str(i) + '.npy',data_for_plotting)

	# Plot multi-step predictions manually
	m_p = np.zeros((T, state_dim))
	S_p = np.zeros((T, state_dim, state_dim))

	m_p[0,:] = m_init
	S_p[0, :, :] = S_init

	for h in range(1, T):
		m_p[h,:], S_p[h,:,:] = pilco.propagate(m_p[h-1, None, :], S_p[h-1,:,:])

	np.save(save_path + '/GP__m_p.npy',m_p)
	np.save(save_path + '/GP__S_p.npy',S_p)
	for i in range(state_dim):
		plt.plot(range(T-1), m_p[0:T-1, i], X_new[1:T, i]) # can't use Y_new because it stores differences (Dx)
		plt.fill_between(range(T-1),
					m_p[0:T-1, i] - 2*np.sqrt(S_p[0:T-1, i, i]),
					m_p[0:T-1, i] + 2*np.sqrt(S_p[0:T-1, i, i]), alpha=0.2)
		plt.show()


text:
import numpy as np
import pandas as pd
import csv
import seaborn as sns
import sys
import matplotlib.pyplot as plt
from collections import OrderedDict
from argparse import ArgumentParser
import pathlib
import math

def main():
    args = get_args()
    sns.set(style='darkgrid',)
    palette=sns.color_palette('pastel')

    for f in args.input:
        sample = f.replace(".csv", "")
        enums = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','w','x','y','z']
        with open(f, 'r') as csvfile:
            data = pd.read_csv(csvfile)
            plt.rcParams['figure.figsize']=(6,3)
            g = []
            dname = data.Data[0]
            g.append(sns.kdeplot(x = "Matches", hue = "Tool", data = data, palette='pastel', fill=True))
            g[0].set_xlabel(xlabel="Length of Matching Bases in Overlaps", fontweight='bold')
            g[0].set_ylabel(ylabel='Proportion', fontweight='bold')
            g[0].set_title(dname, weight='bold')
            g[0].tick_params(labelsize='small')
            g[0].get_legend().set_title(None)

            plt.tight_layout()
            plt.subplots_adjust(top=0.9, bottom=0.1, left=0.13, right=0.95, hspace=0.1, wspace=0.42)
            plt.savefig(f'overlap_finding-matching_blocks_{sample}.pdf')
            #plt.show()

def get_args():
    parser = ArgumentParser(description="Creates PDF plots")
    parser.add_argument("input",
                        nargs='+',
                        help="the dist file(s) to use for plotting")
    return parser.parse_args()

if __name__ == '__main__':
    main()

max_stars_repo_name: CMU-SAFARI/BLEND

max_issues_repo_name: CMU-SAFARI/BLEND

max_forks_repo_name: CMU-SAFARI/BLEND


text:
from striped.job import SinglePointStripedSession as Session
import numpy as np
from numpy.lib.recfunctions import append_fields
import fitsio, healpy as hp
import sys, time

from striped.common import Tracer
T = Tracer()

def match_file(session, path):

    with T["fits/read"]:
            input_data = fitsio.read(path, ext=2, columns=["ALPHAWIN_J2000","DELTAWIN_J2000"])
    with T["hpix"]:
            hpix = hp.ang2pix(nside=16384,theta=input_data['ALPHAWIN_J2000'],phi=input_data['DELTAWIN_J2000'],
                    lonlat=True, nest=True)

    hpix = np.asarray(hpix, np.float64)
    input_data = append_fields(input_data, "HPIX", hpix)
    np.sort(input_data, order="HPIX")

    input_data = np.array(zip(input_data['ALPHAWIN_J2000'], input_data['DELTAWIN_J2000'], input_data['HPIX']))
    matches = []

    class Callback:

        def on_streams_update(self, nevents, data):
            if "matches" in data:
                for m in data["matches"]:
                    matches.append(m)
                    for obs_i, cat_id, rgid, obs_ra, obs_dec, cat_ra, cat_dec in m:
                        print "Match:      index: %10d    RA: %9.4f    Dec: %9.4f   RG: %d" % (int(obs_i), obs_ra, obs_dec, rgid)
                        print "   COADD oject id: %10d        %9.4f         %9.4f" % (int(cat_id), cat_ra, cat_dec)
            if "message" in data:
                for msg in data["message"]:
                    print msg

        def on_exception(self, wid, info):
            print "Worker exception:\n--------------------"
            print info
            print "--------------------"

    job = session.createJob("Bliss",     #"Bliss",
                                user_callback = Callback(),
                                worker_class_file="match_worker.py",
                                user_params = {"observations":input_data})
    with T["job"]:
            job.run()
    runtime = job.TFinish - job.TStart
    catalog_objects = job.EventsProcessed
    print "Compared %d observations against %d catalog objects, elapsed time=%f" % (len(input_data), catalog_objects, runtime)

    if matches:
        matches = np.concatenate(matches, axis=0)
    return matches

job_server_address = ("ifdb01.fnal.gov", 8765) #production
session = Session(job_server_address)

for path in sys.argv[1:]:
    matches = match_file(session, path)
    print path, matches

max_stars_repo_name: ivmfnal/striped

max_issues_repo_name: ivmfnal/striped

max_forks_repo_name: ivmfnal/striped

reason: import numpy,from numpy