text:
import torch.nn as nn
import torch
from torch.utils import data
import os
from scipy.io import loadmat
import PIL
from torch.utils.data.sampler import Sampler
from random import shuffle
import numpy as np
from PIL import Image
import resnet

def imresize(x,h,w):
    return np.array(Image.fromarray(x).resize(size=(h, w)))

factor = 113*113

def rel_snr(output,tar_img):
    eps = 1e-9
    tar_img = tar_img + 1e-12
    loss = torch.sum(torch.sum((output - tar_img).pow(2)))
    norm_factor = torch.sum(torch.sum(tar_img.pow(2)))
    loss = (loss+eps)/(norm_factor+eps)
    return 10*torch.log10(loss/weight),loss,weight

def divide_to_minibatch(lst,idx,minibatch):
    tmp_lst = [lst[idxs] for idxs in idx]
    new_lst = []
    while len(tmp_lst) > minibatch:
        new_lst.append(tmp_lst[:minibatch])
        try:
            for i in range(minibatch):
                tmp_lst.pop(i)
        except Exception:
            continue
    new_lst.append(tmp_lst)
    return new_lst

class dataset(data.Dataset):
    def __init__(self,geometry_main_folder,data_main_folder,max_folder = 10000,mini_batch_size = 40):
        super(dataset,self).__init__()
        self.data_folder = data_main_folder
        self.max_folder = max_folder
        self.folder_list = [os.path.join(geometry_main_folder, o) for o in os.listdir(geometry_main_folder)
                    if (os.path.isdir(os.path.join(geometry_main_folder,o)) and (int(o.split('_')[-1])<self.max_folder) and (os.path.isfile(os.path.join(geometry_main_folder, o) + '//geometry.pt')))]
        data_geom_tupple = [torch.load(folder + '//' + 'geometry.pt') for folder in self.folder_list]
        self.data_vec = []
        self.data_geom = []
        self.minibatch_size = mini_batch_size
        for t in data_geom_tupple:
            g,v = t
            self.data_geom.append(g)
            self.data_vec.append(v)
        self.data_res = [loadmat(self.data_folder + '//' + 'sim_no_{}.mat'.format(folder.split('_')[-1]))['rad'] for folder in self.folder_list]
        print(len(self.data_res))
        self.minibatch_orig = [(g,v,d) for g,v,d in zip(self.data_geom,self.data_vec,self.data_res)]
        self.train_indecis = [i for i in range(len(self.minibatch_orig))]
        self.cnt = np.ceil(len(self.minibatch_orig)/mini_batch_size)
        self.minibatch = divide_to_minibatch(self.minibatch_orig,self.train_indecis,mini_batch_size)
        print(len(self.minibatch))
        print('done loading data to cpu RAM')
    def __len__(self):
        return len(self.minibatch)
    def shuffle(self):
        shuffle(self.train_indecis)
        self.minibatch = divide_to_minibatch(self.minibatch_orig,self.train_indecis,self.minibatch_size)
    def __getitem__(self,index):
        if index == (len(self.minibatch) - 1):
            print("Warning this batch is for evaluation ! (not complete batch)")
        data_list_of_tupple = self.minibatch[index]
        for i,(g,v,d) in enumerate(data_list_of_tupple):
            if i == 0:
                geometry = (100*g.type(torch.float)).unsqueeze(0)
                result = 180 + 10*torch.log10(torch.tensor(imresize(d,64,64))).unsqueeze(0)
                coordinate = v.type(torch.float).unsqueeze(0)
            else:
                g_tmp = (100*g.type(torch.float)).unsqueeze(0)
                res_tmp = (180 + 10*torch.log10(torch.tensor(imresize(d,64,64)))).unsqueeze(0)
                cord_tmp = (v.type(torch.float)).unsqueeze(0)
                geometry = torch.cat([geometry,g_tmp],dim=0)
                result =  torch.cat((result,res_tmp),0)
                coordinate = torch.cat((coordinate,cord_tmp),0)
        return geometry,coordinate,result

    def fix_meas(self,input):
        shape = input.shape
        if shape[2] == 16:
            input = torch.cat([input,(255.0*torch.ones(512,512)).unsqueeze(2)],dim = 2)
        return 360*(1-(input/255.0))

class DataLoader(object):
    def __init__(self, dataset = dataset, batch_size = 1, drop_last=True):
        self.ds = dataset
        self.batch_size = batch_size
        self.drop_last = drop_last
        self.sampler = [x for x in range(self.ds.__len__())]
        shuffle(self.sampler)
        self.current = 0

    def __iter__(self):
        for index in self.sampler:
            index = self.sampler[self.current]
            geom,res = self.ds[index]
            yield geom,res

class simulator_cnn(nn.Module):
    def __init__(self):
        super(simulator_cnn,self).__init__()
        kernel_size_first_conv = 3
        self.relu = nn.ReLU()
        self.hypernet = hypernet(kernel_size_first_conv,16,1)
        self.elu = nn.ELU()
        self.downscale = nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,bias=True)
        self.net = nn.Sequential(nn.Dropout2d(0.3),resnet.ResNetBasicBlock(1,32),nn.Dropout2d(0.3),resnet.ResNetBasicBlock(32,1),nn.Dropout2d(0.3),resnet.ResNetBasicBlock(32,1))
        self.linear = nn.Linear(in_features= 60*60,out_features= 64*64,bias=True)
        self.deep_cnn_2 = nn.Conv2d(in_channels=1,out_channels=1,kernel_size= 1,bias=True)
        torch.nn.init.kaiming_uniform_(self.downscale.weight)
        torch.nn.init.kaiming_uniform_(self.linear.weight)
    def forward(self,inp):
        input_s,vector = inp
        batch_size = vector.shape[0]
        input_s = input_s.permute(0,3,1,2).contiguous()
        for i in range(batch_size):
            tmp_vector = vector[i,:,:]
            weight = self.hypernet(tmp_vector.view(1,-1))
            tmp_input = (input_s[i,:,:,:]).unsqueeze(0)
            output = self.elu(torch.nn.functional.conv2d(tmp_input,weight))
            if i == 0:
                output_batchified = output
            else:
                output_batchified = torch.cat((output_batchified,output),dim=0)
        output = self.elu(self.downscale(output_batchified))
        output = (self.elu(self.net(output)))
        output = self.elu(self.linear(output.view(batch_size,-1)).view(batch_size,1,64,64))
        output = self.elu(self.deep_cnn_2(output))
        return output

class hypernet(nn.Module):
    def __init__(self,kernel_size,input_channels,output_channels):
        super(hypernet,self).__init__()
        self.elu = nn.ELU()
        self.input_channels = input_channels
        self.output_channels = output_channels
        self.kernel_size = kernel_size
        total_size = kernel_size*kernel_size*input_channels*output_channels
        self.linear_1 = nn.Linear(in_features=6,out_features= total_size,bias=True)
        self.linear_3 = nn.Linear(in_features= total_size,out_features= total_size,bias=True)
        torch.nn.init.kaiming_uniform_(self.linear_1.weight)
        self.init_weight_network(self.linear_3.weight,3)
    def forward(self,input):
        output = self.elu(self.linear_1(input))
        output = self.elu(self.linear_3(output)).view(self.output_channels,self.input_channels,self.kernel_size,self.kernel_size)
        return output
    def init_weight_network(self,w,fan_out_dj):
        with torch.no_grad():
            fan_in = w.shape[1]
            a = torch.sqrt(torch.tensor(3*2/(fan_in*fan_out_dj)))
            return w.uniform_(-a,a)

max_stars_repo_name: ShaharLutatiPersonal/hyperhypernetworks

max_issues_repo_name: ShaharLutatiPersonal/hyperhypernetworks

max_forks_repo_name: ShaharLutatiPersonal/hyperhypernetworks


text:
import numpy as np
from flask import render_template, request
import bs4 as bs
import urllib.request
import joblib
import ssl

from application import app
from application.main import rcmd, get_suggestions, convert_to_list

# load the nlp model and tfidf vectorizer from disk
clf = joblib.load('predictor1.joblib')
vectorizer = joblib.load('transformer1.joblib')

# to fix url open Error
ssl._create_default_https_context = ssl._create_unverified_context

@app.route("/")
@app.route("/home")
def home():
    suggestions = get_suggestions()
    return render_template('home.html', suggestions=suggestions)

@app.route("/similarity", methods=["POST"])
def similarity():
    movie = request.form['name']
    rc = rcmd(movie)
    if type(rc) == type('string'):
        return rc
    else:
        m_str = "---".join(rc)
        return m_str

@app.route("/recommend", methods=["POST", "GET"])
def recommend():
    # getting data from AJAX request
    title = request.form['title']
    cast_ids = request.form['cast_ids']
    cast_names = request.form['cast_names']
    cast_chars = request.form['cast_chars']
    cast_bdays = request.form['cast_bdays']
    cast_bios = request.form['cast_bios']
    cast_places = request.form['cast_places']
    cast_profiles = request.form['cast_profiles']
    imdb_id = request.form['imdb_id']
    poster = request.form['poster']
    genres = request.form['genres']
    overview = request.form['overview']
    vote_average = request.form['rating']
    vote_count = request.form['vote_count']
    release_date = request.form['release_date']
    runtime = request.form['runtime']
    status = request.form['status']
    rec_movies = request.form['rec_movies']
    rec_posters = request.form['rec_posters']

    # call the convert_to_list function for every string
    # that needs to be converted to list
    # convert string to list (eg. "[1,2,3]" to [1,2,3])
    rec_movies = convert_to_list(rec_movies)
    rec_posters = convert_to_list(rec_posters)
    cast_names = convert_to_list(cast_names)
    cast_chars = convert_to_list(cast_chars)
    cast_profiles = convert_to_list(cast_profiles)
    cast_bdays = convert_to_list(cast_bdays)
    cast_bios = convert_to_list(cast_bios)
    cast_places = convert_to_list(cast_places)

    cast_ids = cast_ids.split(',')
    cast_ids[0] = cast_ids[0].replace("[", "")
    cast_ids[-1] = cast_ids[-1].replace("]", "")

    # rendering the string to python string
    for i in range(len(cast_bios)):
        cast_bios[i] = cast_bios[i].replace(r'\n', '\n').replace(r'\"', '\"')

    # combining multiple lists as a dictionary which can
    # be passed to the html file so that it can be processed
    # easily and the order of information will be preserved
    movie_cards = {rec_posters[i]: rec_movies[i]
                for i in range(len(rec_posters))}

    casts = {cast_names[i]: [cast_ids[i], cast_chars[i], cast_profiles[i]]
            for i in range(len(cast_profiles))}

    cast_details = {cast_names[i]: [cast_ids[i], cast_profiles[i],
                cast_bdays[i], cast_places[i], cast_bios[i]]
                for i in range(len(cast_places))}

    # web scraping to get user reviews from IMDB site
    sauce = urllib.request.urlopen(
        'https://www.imdb.com/title/{}/reviews?ref_=tt_ov_rt'.format(imdb_id)).read()
    soup = bs.BeautifulSoup(sauce, 'lxml')
    soup_result = soup.find_all("div", {"class": "text show-more__control"})

    reviews_list = []  # list of reviews
    reviews_status = []  # list of comments (good or bad)
    for reviews in soup_result:
        if reviews.string:
            reviews_list.append(reviews.string)
            # passing the review to our model
            movie_review_list = np.array([reviews.string])
            movie_vector = vectorizer.transform(movie_review_list)
            pred = clf.predict(movie_vector)
            reviews_status.append('Good' if pred else 'Bad')

    # combining reviews and comments into a dictionary
    movie_reviews = {reviews_list[i]: reviews_status[i]
                    for i in range(len(reviews_list))}

    # passing all the data to the html file
    return render_template('recommend.html', title=title, poster=poster,
                        overview=overview, vote_average=vote_average,
                        vote_count=vote_count, release_date=release_date,
                        runtime=runtime, status=status, genres=genres,
                        movie_cards=movie_cards, reviews=movie_reviews,
                        casts=casts, cast_details=cast_details)

ext: py

reason: import numpy


text:
## Portions of Code from, copyright 2018 Jochen Gast

from __future__ import absolute_import, division, print_function

import torch
import numpy as np
from scipy import ndimage

def numpy2torch(array):
    assert(isinstance(array, np.ndarray))
    if array.ndim == 3:
        array = np.transpose(array, (2, 0, 1))
    else:
        array = np.expand_dims(array, axis=0)
    return torch.from_numpy(array.copy()).float()

def read_flo_as_float32(filename):
    with open(filename, 'rb') as file:
        magic = np.fromfile(file, np.float32, count=1)
        assert(202021.25 == magic), "Magic number incorrect. Invalid .flo file"
        w = np.fromfile(file, np.int32, count=1)[0]
        h = np.fromfile(file, np.int32, count=1)[0]
        data = np.fromfile(file, np.float32, count=2*h*w)
    data2D = np.resize(data, (h, w, 2))
    return data2D

def read_occ_image_as_float32(filename):
    occ = ndimage.imread(filename).astype(np.float32) / np.float32(255.0)
    if occ.ndim == 3:
        occ = occ[:, :, 0]
    return occ

def read_image_as_float32(filename):
    return ndimage.imread(filename).astype(np.float32) / np.float32(255.0)

def read_image_as_byte(filename):
    return ndimage.imread(filename)

max_stars_repo_name: gml-sync/irr-train

max_issues_repo_name: gml-sync/irr-train

max_forks_repo_name: gml-sync/irr-train

reason: import numpy,from scipy


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import pytest
import astropy.units as u
from ....utils.testing import assert_quantity_allclose, requires_data
from ....maps import WcsGeom
from .. import JFactory, profiles, PrimaryFlux, compute_dm_flux

@pytest.fixture(scope="session")
def geom():
    return WcsGeom.create(binsz=0.5, npix=10)

@pytest.fixture(scope="session")
def jfact(geom):
    jfactory = JFactory(geom=geom, profile=profiles.NFWProfile(), distance=8 * u.kpc)
    return jfactory.compute_jfactor()

@pytest.fixture(scope="session")
def prim_flux():
    return PrimaryFlux(mDM=1 * u.TeV, channel="W")

@requires_data("gammapy-data")
def test_dmfluxmapmaker(jfact, prim_flux):
    x_section = 1e-26 * u.Unit("cm3 s-1")
    energy_range = [0.1, 1] * u.TeV
    flux = compute_dm_flux(
        jfact=jfact, prim_flux=prim_flux, x_section=x_section, energy_range=energy_range
    )

    actual = flux[5, 5]
    desired = 6.49463e-13 / u.cm ** 2 / u.s
    assert_quantity_allclose(actual, desired, rtol=1e-5)

max_stars_repo_name: watsonjj/gammapy

max_issues_repo_name: watsonjj/gammapy

max_forks_repo_name: watsonjj/gammapy

reason: import astropy


text:
import h5py
import itertools
import numpy as np
import os
import pescador
import random

# This function implements the Bresenham's line algorithm in dimension one,
# in order to produce a list of pooling lengths of length n_layers such that
# np.prod(pool_list) * input_length = output_length
# These pooling lengths are chosen as powers of two, in such a way that
# the decrease in lengths between input_length and output_length is as
# progressive as possible.
# inspired by https://stackoverflow.com/a/19293966
def bresenham(input_length, output_length, n_layers):
    log2_total_pooling = np.floor(np.log2(input_length / output_length))
    floor_log2_pooling = int(np.floor(log2_total_pooling / n_layers))
    floor_pooling = 2**floor_log2_pooling
    ceil_log2_pooling = floor_log2_pooling + 1
    ceil_pooling = 2**ceil_log2_pooling

    n_floors = int(n_layers * ceil_log2_pooling - log2_total_pooling)
    n_ceils = n_layers - n_floors
    output_length * floor_pooling**n_floors * ceil_pooling**n_ceils

    floor_list = range(1, n_floors+1)
    ceil_list = range(-1, -n_ceils-1, -1)

    short_list, long_list = sorted((floor_list, ceil_list), key=len)
    n_shorts = len(short_list)
    n_longs = len(long_list)

    if long_list[0] > 0:
        short_pooling = ceil_pooling
        long_pooling = floor_pooling
    else:
        short_pooling = floor_pooling
        long_pooling = ceil_pooling

    if n_shorts == 0:
        pool_list = [long_pooling] * n_longs
    elif n_shorts == 1:
        prefix = [long_pooling] * int(np.floor(n_longs / 2))
        suffix = [long_pooling] * int(np.ceil(n_longs / 2))
        pool_list = prefix + [short_pooling] + suffix
    else:
        groups = itertools.groupby(
            ((long_list[n_longs*i//n_layers], short_list[n_shorts*i//n_layers])
              for i in range(n_layers)),
              key=lambda x:x[0])
        raw_list = [j[i] for k,g in groups for i,j in enumerate(g)]

        pool_list = []
        for n in raw_list:
            if n < 0:
                pool_list.append(ceil_pooling)
            else:
                pool_list.append(floor_pooling)

# We perform leave-one-unit-out cross-validation with 2 units for validation.
def fold_units():
    units = get_units()
    n_units = len(units)
    folds = []
    for fold_id in range(n_units):
        test_units = [units[fold_id]]
        train_units = []
        for unit_id in range(fold_id+1, fold_id+n_units-2):
            train_units.append(units[np.mod(unit_id, n_units)])
        val_units = [
            units[np.mod(fold_id-2, n_units)],
            units[np.mod(fold_id-1, n_units)]]
        folds.append([test_units, train_units, val_units])
    # TODO: assert that len(test_units)==1
    return folds

def get_augmentations():
    units = get_units()
    augmentations = {("noise-" + unit_str): 4 for unit_str in units}
    augmentations["original"] = 1
    augmentations["pitch"] = 4
    augmentations["stretch"] = 4
    return augmentations

def get_data_dir():
	return "/beegfs/vl1019/BirdVox-70k_data"

def get_dataset_name():
    return "BirdVox-70k"

def get_logmelspec_settings():
    logmelspec_settings = {
        "fmin": 2000,
        "fmax": 11025,
        "hop_length": 32,
        "n_fft": 1024,
        "n_mels": 128,
        "sr": 22050,
        "win_length": 256,
        "window": "hann"}
    return logmelspec_settings

def get_models_dir():
    return "/scratch/vl1019/BirdVox-70k_models"

def get_negative_labels():
    return ["alarm"]

def get_pcen_settings():
    pcen_settings = {
        "fmin": 2000,
        "fmax": 11025,
        "hop_length": 32,
        "n_fft": 1024,
        "n_mels": 128,
        "pcen_delta_denominator": 10,
        "pcen_time_constant_frames": 40,
        "pcen_norm_exponent": -0.8,
        "pcen_power": 0.25,
        "sr": 22050,
        "win_length": 256,
        "window": "hann"}
    return pcen_settings

def get_sample_rate():
    return 24000 # in Hertz

def get_tolerances():
    return np.arange(0.100, 0.550, 0.05) # in seconds

def get_units():
    return ["unit" + str(unit).zfill(2) for unit in [1, 2, 3, 5, 7, 10]]
    # TODO: assert that unit names do not contain underscores.

def multiplex_tfr(aug_kind_str, fold_units, n_hops, batch_size,
        tfr_str="logmelspec"):
    # Parse augmentation kind string (aug_kind_str).
    if aug_kind_str == "none":
        augs = ["original"]
    elif aug_kind_str == "pitch":
        augs = ["original", "pitch"]
    elif aug_kind_str == "stretch":
        augs = ["original", "stretch"]
    elif aug_kind_str == "all-but-noise":
        augs = ["original", "pitch", "stretch"]
    else:
        noise_augs = ["noise-" + unit_str for unit_str in fold_units]
        if aug_kind_str == "all":
            augs = noise_augs + ["original", "pitch", "stretch"]
        elif aug_kind_str == "noise":
            augs = noise_augs + ["original"]

    # Generate a Pescador streamer for every HDF5 container, that is,
    # every unit-augmentation-instance triplet.
    aug_dict = get_augmentations()
    data_dir = get_data_dir()
    dataset_name = get_dataset_name()
    tfr_name = "_".join([dataset_name, "clip-" + tfr_str])
    tfr_dir = os.path.join(data_dir, tfr_name)
    streams = []
    for aug_str in augs:
        aug_dir = os.path.join(tfr_dir, aug_str)
        if aug_str == "original":
            instances = [aug_str]
        else:
            n_instances = aug_dict[aug_str]
            instances = ["-".join([aug_str, str(instance_id)])
                for instance_id in range(n_instances)]
        if aug_str[:5] == "noise" and tfr_str == "logmelspec":
            bias = np.float32(-17.0)
        else:
            bias = np.float32(0.0)
        for instanced_aug_str in instances:
            for unit_str in fold_units:
                lms_name = "_".join([dataset_name, instanced_aug_str, unit_str])
                lms_path = os.path.join(aug_dir, lms_name + ".hdf5")
                stream = pescador.Streamer(yield_tfr,
                    lms_path, n_hops, bias, tfr_str)
                streams.append(stream)

    # Multiplex streamers together.
    mux = pescador.Mux(streams,
        k=len(streams), lam=None, with_replacement=True, revive=True)

    # Create buffered streamer with specified batch size.
    buffered_streamer = pescador.BufferedStreamer(mux, batch_size)

    return buffered_streamer.tuples("X", "y", cycle=True)

def pick_peaks(odf):
    derivative = np.diff(odf)
    pre_slope = np.insert(derivative, 0, -np.inf)
    post_slope = np.append(derivative, np.inf)
    peak_bools = np.logical_and((pre_slope > 0), (post_slope <= 0))
    locations = np.where(peak_bools)[0]
    return locations

def rsync():
    data_dir = get_data_dir()
    archive_dir = get_archive_dir()
    flags = ["-r"]
    command_words = ["rsync"] + flags + [data_dir, archive_dir]
    command_str = " ".join(command_words)
    os.command(command_str)

def yield_tfr(lms_path, n_hops, bias, tfr_str):
    # Open HDF5 container.
    with h5py.File(lms_path, "r") as lms_container:
        # Open HDF5 group corresponding to time-freq representation (TFR).
        lms_group = lms_container[tfr_str]

        # The naming convention of a key is
        # [unit]_[time]_[freq]_[y]_[aug]_[instance]
        # where y=1 if the key corresponds to a positive clip and 0 otherwise.
        keys = list(lms_group.keys())
        while True:
            # Pick a key uniformly as random.
            key = random.choice(keys)

            # Load TFR.
            X = lms_group[key]

            # Trim TFR in time to required number of hops.
            X_width = X.shape[1]
            first_col = int((X_width-n_hops) / 2)
            last_col = int((X_width+n_hops) / 2)
            X = X[:, first_col:last_col]

            # Add trailing singleton dimension for Keras interoperability.
            X = X[:, :, np.newaxis]

            # Apply bias
            X = X + bias

            # Retrieve label y from key name.
            y = np.array([np.float32(key.split("_")[3])])

            # Yield data and label as dictionary.
            yield dict(X=X, y=y)


text:
import sys
import numpy as np
from scipy import linalg
import copy

eps = 10 ** -9

kmax = 10000

class MatrixFromFile:
    dimension = 0
    data = []
    b = []
    v = []
    omega = dict()

    def __init__(self, path):
        with open(path) as fp:
            line = fp.readline()
            self.dimension = int(line)

            self.data = [[] for i in range(self.dimension)]
            self.b = []

            while line:
                line = fp.readline()
                if len(line) == 0:
                    continue

                values = line.strip('\n').split(', ')
                if len(values) == 3:
                    value, lin, col = float(values[0]), int(values[1]), int(values[2])
                    flag = False

                    pairNo = -1
                    for pair in self.data[lin]:
                        pairNo += 1
                        if pair[1] == col:
                            self.data[lin][pairNo][0] += value
                            flag = True

                    if not flag:
                        self.data[lin].append([value, col])

                elif len(values) == 1:
                    if values[0] != '':
                        self.b.append(float(values[0]))

                else:
                    print("Caz netratat: *" + line + '*')

        testV = np.random.uniform(1.0,5.0,self.dimension)
        vNorm = np.linalg.norm(testV,2)
        self.v = ([element/vNorm for element in testV])

        for line in self.data:
            line.sort(key=lambda elem: elem[1])

    def compareMatrix(self, mat, matDimension):
        if self.dimension != matDimension:
            return False

        for lineNo in range(self.dimension):
            myLineElemNo = len(self.data[lineNo])
            otherLineElemNo = len(mat[lineNo])

            if (myLineElemNo != otherLineElemNo):
                return False

            for pairSelf in self.data[lineNo]:
                exists = False

                for pairOther in mat[lineNo]:
                    if abs(pairSelf[0] - pairOther[0]) < eps and pairSelf[1] == pairOther[1]:
                        exists = True
                        break

                if not exists:
                    return False

        return True

    def compareVector(self, vector):
        if self.dimension != len(vector):
            return False

        for index in range(self.dimension):
            if abs(self.b[index] - vector[index]) >= eps:
                return False

        return True

    def printMatrix(self):
        for line in self.data:
            print(line)

    def matrixVectorMultiply(self, x):
        result = []
        # x = [self.dimension - i for i in range(0, self.dimension)]
        for line in self.data:
            sum = 0
            for pair in line:
                sum += pair[0] * x[pair[1]]

            result.append(sum)

        return result

    def getTranspose(self):
        result = [[] for i in range(self.dimension)]

        for lineIndex in range(self.dimension):
            for pair in self.data[lineIndex]:
                val, colIndex = pair[0], pair[1]
                result[colIndex].append([val, lineIndex])

        for line in result:
            line.sort(key=lambda elem: elem[1])
        return result

    def powerMethod(self):
        lambdaVector = []
        w = self.matrixVectorMultiply(self.v)
        lambdaVector = np.dot(w,self.v)

        k = 0

        while True:
            wNorm = np.linalg.norm(w)
            self.v = [element/wNorm for element in w]

            w = self.matrixVectorMultiply(self.v)
            lambdaVector = np.dot(w,self.v)

            k += 1
            testValueVector = []
            for index in range(0,len(self.v)):
                testValueVector.append(w[index] - lambdaVector * self.v[index])
            if np.linalg.norm(testValueVector) <= self.dimension * eps  or k > 10000:
                break

        if k > 10000:
            print("Algoritmul nu a reusit sa calculeze valoarea proprie")
        else:
            print("O aproximare a unei valori proprii de modul maxim a matricei din fisier este " + str(lambdaVector))

class MatrixRandom:
    dimension = 0
    data = []
    v = []

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

        self.data = [[] for i in range(self.dimension)]
        randnums= np.random.randint(1, 100, self.dimension)

        for randint in randnums:
            line = np.random.randint(0 , self.dimension)
            column = np.random.randint(0, self.dimension)
            index = self.getElementPosition(line,column)

            if  index == -1:
                self.data[line].append([randint,column])
                if line!=column:
                    self.data[column].append([randint,line])
            else:
                self.data[line][index][0] += randint
                if line!=column:
                    index = self.getElementPosition(column,line)
                    self.data[column][index][0] += randint

        testV = np.random.uniform(1.0,5.0,self.dimension)
        vNorm = np.linalg.norm(testV,2)
        self.v = ([element/vNorm for element in testV])

        for line in self.data:
            line.sort(key=lambda elem: elem[1])

    def matrixVectorMultiply(self, x):
        result = []
        # x = [self.dimension - i for i in range(0, self.dimension)]
        for line in self.data:
            sum = 0
            for pair in line:
                sum += pair[0] * x[pair[1]]

            result.append(sum)

        return result

    def compareMatrix(self, mat, matDimension):
        if self.dimension != matDimension:
            return False

        for lineNo in range(self.dimension):
            myLineElemNo = len(self.data[lineNo])
            otherLineElemNo = len(mat[lineNo])

            if (myLineElemNo != otherLineElemNo):
                return False

            for pairSelf in self.data[lineNo]:
                exists = False

                for pairOther in mat[lineNo]:
                    if abs(pairSelf[0] - pairOther[0]) < eps and pairSelf[1] == pairOther[1]:
                        exists = True
                        break

                if not exists:
                    return False

        return True

    def getElementPosition(self, line, column):
        if len(self.data[line]) == 0:
            return -1
        for index in range(len(self.data[line])):
            if  self.data[line][index][1] == column:
                return index
        return -1

    def printMatrix(self):
        for line in self.data:
            print(line)

    def getTranspose(self):
        result = [[] for i in range(self.dimension)]

        for lineIndex in range(self.dimension):
            for pair in self.data[lineIndex]:
                val, colIndex = pair[0], pair[1]
                result[colIndex].append([val, lineIndex])

        for line in result:
            line.sort(key=lambda elem: elem[1])
        return result

    def powerMethod(self):
        lambdaVector = []
        w = self.matrixVectorMultiply(self.v)
        lambdaVector = np.dot(w,self.v)

        k = 0

        while True:
            wNorm = np.linalg.norm(w)
            self.v = [element/wNorm for element in w]

            w = self.matrixVectorMultiply(self.v)
            lambdaVector = np.dot(w,self.v)

            k += 1
            testValueVector = []
            for index in range(0,len(self.v)):
                testValueVector.append(w[index] - lambdaVector * self.v[index])
            if np.linalg.norm(testValueVector) <= self.dimension * eps  or k > 10000:
                break

        if k > 10000:
            print("Algoritmul nu a reusit sa calculeze valoarea proprie")
        else:
            print("O aproximare a unei valori proprii de modul maxim a matricei generata aleator este " + str(lambdaVector))

""" fileMat = MatrixFromFile("input\m_rar_sim_2019_500.txt")
randMat = MatrixRandom(500)
if  randMat.compareMatrix(randMat.getTranspose(),randMat.dimension) == True:
    randMat.powerMethod()
else:
    print("Matricea generata aleator nu este simetrica")
if fileMat.compareMatrix(fileMat.getTranspose(),fileMat.dimension) == True:
    fileMat.powerMethod()
else:
    print("Matricea din fisier nu este simetrica")  """
A = np.array([[1,2,3],[2,3,3],[4,2,1],[2,1,3]])
b = np.array([1,2,3,4])
U,S,V = np.linalg.svd(A)
mooreMatrix = np.linalg.pinv(A)

rangMatrix = 0
singularValues = []
minSingularValue = -1
maxSingularValue = -1

for element in S:
    if element >= 0:
        singularValues.append(element)
        rangMatrix += 1
        if element < minSingularValue or minSingularValue == -1:
            minSingularValue = element
        if element > maxSingularValue:
            maxSingularValue = element

print ("Valorile singulare ale matricii A pentru p > n sunt: " +  str(singularValues))
print ("Rangul matricii A comform calculelor noastre este: " + str(rangMatrix))
print ("Rangul matricii A comform numpy este: " + str(np.linalg.matrix_rank(A)))
print ("Numarul de conditionare al matricii A comform calculelor noastre este: " + str(maxSingularValue/ minSingularValue))
print ("Numarul de conditionare al matricii A comform numpy este: " + str(np.linalg.cond(A)))

print ("Pseudoinversa Moore-Penrose a matricei A este: ")

x = mooreMatrix.dot(b)
print("Solutia sistemului Ax = b: " + str(x ))

Ax = A.dot(x)
equationResult = []

for index in range(len(Ax)):
    equationResult.append(b[index] - Ax[index])

print("Norma ecuatiei b − Ax este: " + str(np.linalg.norm(equationResult)))

max_stars_repo_name: anggabard/CN

max_issues_repo_name: anggabard/CN

max_forks_repo_name: anggabard/CN


text:
import sys
import time
from abc import abstractmethod, ABCMeta

import cv2
import numpy as np
import pygame

from apps.facepong2.pongFaceDetector import PongFaceDetector
from apps.facepong2.pongPhysics import PongPhysics
from apps.facepong2.pongConfig import CONFIG
from apps.facepong2.pongRenderer import PongRenderer, TextAlign

class PongGame:
    def __init__(self):
        self.cap = cv2.VideoCapture(CONFIG.cam)
        #self.cap.set(3, 1920)
        #self.cap.set(4, 1080)
        _, img = self.cap.read()
        self.gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        self.faceDetector = PongFaceDetector()
        self.width = img.shape[1]
        self.height = img.shape[0]
        self.third_size = self.width / 3
        self.renderer = PongRenderer((self.width, self.height), CONFIG.graphics.monitor_size)
        self.physics = PongPhysics(self.width, self.height)
        self.state = ReadyState()
        self.fps = 0.0
        self.last_img_time = 0
        self.last_img = None
        self.last_fps_print = 0
        self.last_tick = 0

        # Restart
        self.paused = False
        self.winPaused = False
        self.wins = [0, 0]
        self.restart()

    def restart(self):
        self.paused = False
        self.winPaused = False
        self.wins = [0, 0]

    def run(self):
        try:
            self.last_tick = time.time()
            while True:
                self.fps = 1 / max(time.time() - self.last_tick, 0.000001)
                if time.time() - self.last_fps_print > CONFIG.fps_interval:
                    self.last_fps_print = time.time()
                    print("{0:.2f} Fps".format(self.fps))
                img = self.get_image()
                delta = self.update_time()

                self.state.loop(self, img, delta)

                self.renderer.render(img, self, self.state)

                self.handle_events()

        except KeyboardInterrupt:
            pygame.quit()
        except SystemExit:
            pygame.quit()

    def reset(self):
        self.physics.throw_in()

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == 27 or event.key == 113:
                    sys.exit(0)
            elif event.type == pygame.VIDEORESIZE:
                # TODO What did I want do do here?
                # screen = pygame.display.set_mode(event.dict['size'],
                #                                 pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.FULLSCREEN)
                pygame.display.flip()

    def get_image(self):
        if self.last_img is None or time.time() - self.last_img_time > (1 / CONFIG.graphics.target_cam_fps):
            _, self.last_img = self.cap.read()
            self.last_img = np.flip(self.last_img, 1)
            self.last_img_time = time.time()
        return self.last_img

    def get_faces(self, image):
        return self.faceDetector.get_face_positions(image, (30, 30),
                                                    (300, 300), self.third_size, self.third_size * 2)

    def update_faces(self, delta, img):
        right_face, left_face = self.get_faces(img)
        self.physics.faceOne.push_pos(right_face, delta)
        self.physics.faceTwo.push_pos(left_face, delta)
        cnt = 0 if right_face is None else 1
        if left_face is not None:
            cnt += 1
        return cnt

    def update_time(self):
        curr_time = time.time()
        dt = curr_time - self.last_tick
        self.last_tick = curr_time
        return dt

    def win(self, plr):
        self.wins[int((plr + 1) / 2)] += 1

    def wins_of(self, plr):
        return self.wins[int((plr + 1) / 2)]

    def change_state_to(self, state):
        #print("Changing state...")
        #print(state)
        self.state = state

class GameState(metaclass=ABCMeta):
    @abstractmethod
    def loop(self, game: PongGame, img, delta):
        pass

    @abstractmethod
    def render(self, renderer: PongRenderer, video, game: PongGame):
        pass

class ReadyState(GameState):
    def render(self, renderer, video, game: PongGame):
        mat = np.zeros(video.shape, dtype=np.float32)
        mat.fill(0.0)

        if self.face_one is not None:
            cv2.circle(mat, (int(self.face_one[1]), int(self.face_one[0])), 100, (1, 1, 1), -1)

        if self.face_two is not None:
            cv2.circle(mat, (int(self.face_two[1]), int(self.face_two[0])), 100, (1, 1, 1), -1)

        mat = cv2.blur(mat, (20, 20))
        img = cv2.multiply(cv2.cvtColor(video, cv2.COLOR_RGB2GRAY), 0.5)
        img = cv2.blur(img, (50, 50))
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
        img = cv2.multiply(img, 1 - mat, dtype=3)
        video = cv2.multiply(video, mat, dtype=3)
        img = cv2.add(img, video)
        renderer.draw_background(img)
        renderer.text(None, (255, 255, 255), "{:.1f}s".format(self.duration - self.time), size=120, out=True)

    def __init__(self, duration=None, reset=True):
        if duration is None:
            duration = CONFIG.ready_state_duration
        self.duration = duration
        self.time = 0
        self.face_one = None
        self.face_two = None
        self.reset = reset

    def loop(self, game, img, delta):
        self.face_one, self.face_two = game.get_faces(img)
        if self.face_one is None or self.face_two is None:
            delta *= -1
        self.time = self.time + delta
        if self.time < 0:
            self.time = 0
        if self.time > self.duration:
            if self.reset:
                game.reset()
            game.change_state_to(PlayingState())

class PlayingState(GameState):
    def __init__(self):
        self.timeout = 0

    @staticmethod
    def get_blurred_field(video, upper_left, lower_right, blur, brightness):
        middle_mat = np.zeros(video.shape, dtype=np.float32)
        cv2.rectangle(middle_mat, upper_left, lower_right, (1.0, 1, 1), thickness=-1)
        middle_mat = cv2.blur(middle_mat, (20, 20))

        middle_field_vid = cv2.blur(video, (int(blur), int(blur)))
        middle_field_vid = cv2.multiply(middle_field_vid, brightness * middle_mat, dtype=3)
        rest_vid = cv2.multiply(video, 1 - middle_mat, dtype=3)

        return cv2.add(middle_field_vid, rest_vid)

    def draw_points(self, renderer: PongRenderer, y, points_a, points_b, alpha=255):
        # Goals
        renderer.text((None, y), (255, 255, 255, alpha), "{0} - {1}".format(points_a, points_b), out=True,
                      align=(TextAlign.CENTER, TextAlign.LEFT), size=50, draw_in_front=False)

    def render(self, renderer: PongRenderer, video, game: PongGame):
        # Middlefield
        background = self.get_blurred_field(video,
                                            (0, int(video.shape[0] / 3)), (video.shape[1], int(video.shape[0] / 3 * 2)),
                                            CONFIG.graphics.middle_field_blur, CONFIG.graphics.middle_field_brightness)

        # Faces
        face_mat = np.zeros(video.shape, dtype=np.float32)
        face_mat.fill(1.0)
        face_a_pos = game.physics.faceOne.get_pos()
        face_b_pos = game.physics.faceTwo.get_pos()
        thickness = CONFIG.graphics.face_border_thickness

        if face_a_pos is not None:
            cv2.circle(face_mat, (int(face_a_pos[1]), int(face_a_pos[0])),
                       game.physics.faceOne.radius + thickness, CONFIG.graphics.color_face_left_border, -1)
            cv2.circle(face_mat, (int(face_a_pos[1]), int(face_a_pos[0])),
                       game.physics.faceOne.radius, CONFIG.graphics.color_face_left, -1)
        if face_b_pos is not None:
            cv2.circle(face_mat, (int(face_b_pos[1]), int(face_b_pos[0])),
                       game.physics.faceOne.radius + thickness, CONFIG.graphics.color_face_right_border, -1)
            cv2.circle(face_mat, (int(face_b_pos[1]), int(face_b_pos[0])),
                       game.physics.faceOne.radius, CONFIG.graphics.color_face_right, -1)
        face_mat = cv2.blur(face_mat, CONFIG.graphics.face_blur)
        ball_pos = game.physics.ball.get_pos()
        cv2.circle(face_mat, (int(ball_pos[1]), int(ball_pos[0])),
                   game.physics.ball.radius+CONFIG.graphics.ball_border_thickness,
                   CONFIG.graphics.color_ball_border, -1)
        cv2.circle(face_mat, (int(ball_pos[1]), int(ball_pos[0])), game.physics.ball.radius,
                   CONFIG.graphics.color_ball, -1)
        face_mat = cv2.blur(face_mat, CONFIG.graphics.ball_blur)
        background = cv2.multiply(background, face_mat, dtype=3)

        renderer.draw_background(background)

        # Ball
        #ball_pos = game.physics.ball.get_pos()
        #renderer.circle((0, 0, 30),
        #                (int(ball_pos[0]), int(ball_pos[1])), game.physics.ball.radius)

        self.draw_points(renderer, -CONFIG.graphics.goal_font_size, game.wins[0], game.wins[1])

    def loop(self, game, img, delta):
        if game.update_faces(delta, img) < 2:
            self.timeout += delta
        if self.timeout > CONFIG.face_missing_timeout:
            game.change_state_to(ReadyState(CONFIG.timeout_ready_state_duration, False))
        game.physics.tick(delta)
        self.check_state(game)

    def check_state(self, game):
        if game.physics.is_invalid_state():
            game.reset()  # TODO Animation here
        self.check_win(game.physics.get_win(), game)

    def check_win(self, won_player, game):
        if won_player != 0:
            game.win(won_player)
            if won_player < 0:
                game.change_state_to(WinState(won_player))
            else:
                game.change_state_to(WinState(won_player))

class WinState(PlayingState):
    def render(self, renderer: PongRenderer, video, game: PongGame):
        #print(game.wins_of(self.player))
        if game.wins_of(self.player) >= CONFIG.goals_to_win:
            game.change_state_to(FinalState(self.player))
            return

        # Timing
        time_progress = min(1.0, (self.duration - self.time) / self.duration)

        fade_in_end = CONFIG.win_screen_times[0] / CONFIG.win_screen_duration
        fade_out_start = CONFIG.win_screen_times[1] / CONFIG.win_screen_duration
        time_progress = time_progress / fade_in_end if time_progress < fade_in_end else (
            1 if time_progress < fade_out_start else
            (1 - time_progress) / (1 - fade_out_start)
        )

        # Background-Animation
        player_id = (self.player + 1) / 2
        a_third = video.shape[0] / 3
        x_start = (1 - time_progress * player_id) * a_third
        x_end = video.shape[0] - ((1 - time_progress * (1 - player_id)) * a_third)
        blur = time_progress * CONFIG.graphics.middle_field_blur * 4 + CONFIG.graphics.middle_field_blur
        brightness = CONFIG.graphics.middle_field_brightness - (time_progress * CONFIG.graphics.middle_field_brightness
                                                                * (1 - CONFIG.graphics.win_screen_brightness))
        background = self.get_blurred_field(video, (0, int(x_start)), (video.shape[1], int(x_end)), blur, brightness)
        renderer.draw_background(background)

        # Texts
        text_x = a_third + player_id * a_third - self.player * 30
        text_align = TextAlign.RIGHT if self.player > 0 else TextAlign.LEFT
        lr_text = "Left" if self.player < 0 else "Right"
        text_alpha = max(0.0, time_progress * 355 - 100)
        renderer.text((text_x, None), (255, 255, 255, text_alpha), "{0} Goal".format(lr_text), size=60,
                      align=(text_align, TextAlign.CENTER), out=True)

        # Goals Text
        win_a_pre = int(game.wins[0] - (1 - player_id))
        win_b_pre = int(game.wins[1] - player_id)
        if self.time > self.duration * fade_in_end:
            self.draw_points(renderer, -CONFIG.graphics.goal_font_size * (1 - time_progress), win_a_pre, win_b_pre,
                             alpha=255 * (1 - time_progress))
        else:
            self.draw_points(renderer, -CONFIG.graphics.goal_font_size * (1 - time_progress), game.wins[0],
                             game.wins[1],
                             alpha=255 * (1 - time_progress))

    def __init__(self, player):
        super().__init__()
        self.duration = CONFIG.win_screen_duration
        self.time = self.duration
        self.player = player

    def loop(self, game, img, delta):
        self.time -= delta
        if self.time < 0:
            game.reset()
            game.change_state_to(PlayingState())

class FinalState(PlayingState):
    def __init__(self, won_player):
        super().__init__()
        self.player = won_player
        self.time = 0
        self.duration = CONFIG.final_screen_duration

    def render(self, renderer: PongRenderer, video, game: PongGame):
        # Timing
        time_progress = min(1.0, self.time / CONFIG.final_screen_times[0])

        # Background-Animation
        player_id = (self.player + 1) / 2
        a_third = video.shape[0] / 3
        x_start = (1 - time_progress) * a_third
        x_end = video.shape[0] - ((1 - time_progress) * a_third)
        blur = time_progress * CONFIG.graphics.middle_field_blur * 4 + CONFIG.graphics.middle_field_blur
        brightness = CONFIG.graphics.middle_field_brightness - (time_progress * CONFIG.graphics.middle_field_brightness
                                                                * (1 - CONFIG.graphics.win_screen_brightness))
        background = self.get_blurred_field(video, (0, int(x_start)), (video.shape[1], int(x_end)), blur, brightness)
        renderer.draw_background(background)

        # Points
        pointsY = -CONFIG.graphics.goal_font_size \
                  + (CONFIG.graphics.goal_font_size + renderer.screen.get_height() / 2
                  - CONFIG.graphics.final_screen_font_size / 2) * time_progress
        renderer.text((None, pointsY), (255, 255, 255), "{0} - {1}".format(game.wins[0], game.wins[1]), out=True,
                      align=(TextAlign.CENTER, TextAlign.LEFT),
                      size=int(CONFIG.graphics.goal_font_size +
                               (
                                           CONFIG.graphics.final_screen_font_size - CONFIG.graphics.goal_font_size) * time_progress))

    def loop(self, game, img, delta):
        self.time += delta
        if self.time >= self.duration:
            game.restart()
            game.change_state_to(ReadyState())

max_stars_repo_name: Obyoxar/RobolabStatistics

max_issues_repo_name: Obyoxar/RobolabStatistics

max_forks_repo_name: Obyoxar/RobolabStatistics


text:
import unittest2 as unittest
import numpy as np

from vsm.viewer.tfidfviewer import *
from vsm.viewer.labeleddata import *

class TestTfIdfViewer(unittest.TestCase):

    def setUp(self):

        from vsm.corpus.util.corpusbuilders import random_corpus
        from vsm.model.tf import TfSeq
        from vsm.model.tfidf import TfIdf

        c = random_corpus(1000, 50, 0, 10, context_type='document', metadata=True)

        tf = TfSeq(c, 'document')
        tf.train()

        m = TfIdf.from_tf(tf)
        m.train()

        self.v = TfIdfViewer(c, m)

    def test_TfIdfViewer(self):

        li = [0,1]

        sww = self.v.dist_word_word('0')
        swwl = self.v.dist_word_word(['0','1'])
        sdd = self.v.dist_doc_doc(0)
        sddl = self.v.dist_doc_doc(li)

        dismatw = self.v.dismat_word(['0','2','5'])
        dismatd = self.v.dismat_doc(li)

        self.assertEqual(type(sww), LabeledColumn)
        self.assertEqual(type(swwl), LabeledColumn)
        self.assertEqual(type(sdd), LabeledColumn)
        self.assertEqual(type(sddl), LabeledColumn)

        self.assertEqual(type(dismatw), IndexedSymmArray)
        self.assertEqual(type(dismatd), IndexedSymmArray)

#Define and run test suite
suite = unittest.TestLoader().loadTestsFromTestCase(TestTfIdfViewer)
unittest.TextTestRunner(verbosity=2).run(suite)

max_stars_repo_name: inpho/vsm

max_issues_repo_name: inpho/vsm

max_forks_repo_name: inpho/vsm


text:
import os
import numpy as np
from scipy.io import loadmat
from scipy.signal import welch
from mindaffectBCI.decoder.utils import block_randomize, butter_sosfilt, upsample_codebook, lab2ind
from mindaffectBCI.decoder.multipleCCA import robust_whitener
from mindaffectBCI.decoder.updateSummaryStatistics import updateCxx

def load_mark_EMG(datadir, sessdir=None, sessfn=None, fs_out=60, stopband=((45,65),(0,10),(45,55),(95,105),(145,-1)), filterbank=None, verb=0, log=True, whiten=True, plot=False):

    fs=1000
    ch_names=None

    # load the data file
    Xfn = os.path.expanduser(datadir)
    if sessdir:
        Xfn = os.path.join(Xfn, sessdir)
    if sessfn:
        Xfn = os.path.join(Xfn, sessfn)
    sessdir = os.path.dirname(Xfn)

    print("Loading {}".format(Xfn))
    data = loadmat(Xfn)

    def squeeze(v):
        while v.size == 1 and v.ndim > 0:
            v = v[0]
        return v

    X = np.array([squeeze(d['buf']) for d in squeeze(data['data'])]) # ( nTrl, nch, nSamp)
    X = np.moveaxis(X,(0,1,2),(0,2,1)) # ( nTr, nSamp, nCh)
    X = np.ascontiguousarray(X) # ensure memory efficient
    lab = np.array([squeeze(e['value']) for e in data['devents']],dtype=int) # (nTrl,)

    import matplotlib.pyplot as plt
    if plot: plt.figure(100);plt.plot(X[0,:,:]);plt.title("raw")

    # preprocess -> spectral filter, in continuous time!
    if stopband is not None:
        if verb > 0:
            print("preFilter: {}Hz".format(stopband))
        X, _, _ = butter_sosfilt(X,stopband,fs)
        if plot:plt.figure(101);plt.plot(X[0,:,:]);plt.title("hp+notch+lp")
        # preprocess -> spatial whiten
        # TODO[] : make this  fit->transform method
        if whiten:
            print("spatial whitener")
            Cxx = updateCxx(None,X,None)
            W,_ = robust_whitener(Cxx)
            X = np.einsum("tsd,dw->tsw",X,W)
            if plot:plt.figure(102);plt.plot(X[0,:,:]);plt.title("+whiten")

    if not filterbank is None:
        if verb > 0:  print("Filterbank: {}".format(filterbank))
        # apply filter bank to frequency ranges into virtual channels
        Xs=[]
        # TODO: make a nicer shape, e.g. (tr,samp,band,ch)
        for bi,band in enumerate(filterbank):
            Xf, _, _ = butter_sosfilt(X,band,fs)
            Xs.append(Xf)
        # stack the bands as virtual channels
        X = np.concatenate(Xs,-1)

    X = np.abs(X) # rectify
    if log:
        print("log amplitude")
        X = np.log(np.maximum(X,1e-6))
    if plot:plt.figure(103);plt.plot(X[0,:,:]);plt.title("+abs")
    X, _, _ = butter_sosfilt(X,(40,-1),fs) # low-pass = envelope extraction
    if plot:plt.figure(104);plt.plot(X[0,:,:]);plt.title("env")

    # preprocess -> downsample @60hz
    resamprate=int(fs/fs_out)
    if resamprate > 1:
        if verb > 0:
            print("resample: {}->{}hz rsrate={}".format(fs,fs_out,resamprate))
        X = X[:, ::resamprate, :] # decimate X (trl, samp, d)
        fs = fs/resamprate

    # get Y
    Y_true, lab2class = lab2ind(lab) # (nTrl, e)
    Y_true = Y_true[:, np.newaxis, :] # ( nTrl,1,e)
    # TODO[] : exhaustive list of other targets...
    Yall = np.eye(Y_true.shape[-1],dtype=bool) # (nvirt,e)
    Yall = np.tile(Yall,(Y_true.shape[0],1,1)) # (nTrl,nvirt,e)
    Y = np.append(Y_true,Yall,axis=-2) # (nTrl,nvirt+1,e)
    # upsample to fs_out
    Y = np.tile(Y[:,np.newaxis,:,:],(1,X.shape[1],1,1)) #  (nTrl, nSamp, nY, e)
    Y = Y.astype(np.float32)

    # make coords array for the meta-info about the dimensions of X
    coords = [None]*X.ndim
    coords[0] = {'name':'trial'}
    coords[1] = {'name':'time','unit':'ms', \
                 'coords':np.arange(X.shape[1])*1000/fs, \
                 'fs':fs}
    coords[2] = {'name':'channel','coords':ch_names}
    # return data + metadata
    return (X, Y, coords)

def testcase():
    import sys

    sessfn = '~/data/bci/own_experiments/emg/facial/training_data_SV_6.mat'
    # command-line, for testing
    if len(sys.argv) > 1:
        sessfn = sys.argv[1]

    #from offline.load_mark_EMG import load_mark_EMG
    oX, oY, coords = load_mark_EMG(sessfn, fs_out=125, stopband=((0,10),(45,55),(95,105),(145,-1)), plot=False)
    fs = coords[1]['fs']
    ch_names = coords[2]['coords']
    X=oX.copy()
    Y=oY.copy()

    print("X({}){}".format([c['name'] for c in coords],X.shape))
    print("Y={}".format(Y.shape))
    print("fs={}".format(fs))

    # try as simple linear classification problem
    from sklearn.linear_model import Ridge, LogisticRegression, LogisticRegressionCV
    from sklearn.svm import LinearSVR, LinearSVC
    # trial average
    Xc = np.sum(X,axis=-2)
    Yc = np.argmax(Y[:,0,0,:],axis=-1)
    clsfr = LogisticRegression(C=1e8) # turn off regularization
    clsfr.fit(Xc,Yc)
    Yest = clsfr.predict(Xc)
    acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print("Acc:{}".format(acc))
    W = clsfr.coef_
    b = clsfr.intercept_
    Fe = np.einsum("Td,ed->Te",Xc,W) + b
    Fy = Fe
    Yest = np.argmax(Fy,-1)
    acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print("Acc:{}".format(acc))

    # per-sample
    clsfr = LogisticRegression(C=1e8)
    Xc = X.reshape((-1,X.shape[-1]))
    Yc = np.argmax(Y[...,0,:],axis=-1).reshape((-1,))
    clsfr.fit(Xc,Yc)
    W = clsfr.coef_
    b = clsfr.intercept_
    Fe = np.einsum("Td,ed->Te",Xc,W) + b
    Fy = Fe
    Yest = np.argmax(Fy,-1)
    acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print('Acc: {}'.format(acc))

    # visualize the dataset
    from analyse_datasets import debug_test_dataset
    debug_test_dataset(X, Y, coords, tau_ms=20, evtlabs=None, rank=15, model='cca')

if __name__=="__main__":
    testcase()

max_issues_repo_name: CkiChen/pymindaffectBCI

max_forks_repo_name: CkiChen/pymindaffectBCI


text:
import numpy as np

# EXTRACTING SOME COLUMNS OR ROWS OF THE 2D ARRAY

arr1 = np.arange(36).reshape(6,6)
print("THE 2D ARRAY : ",arr1)
ex = np.array(arr1)
print("EXTRACTION OF THE 4 ROW AND 5 ROW : ", ex[3:5])

# FINDING THE SUM OF THE COLUMNS

arr2 = np.array([[1,3,5,6],[3,6,7,3],[2,3,6,6],[4,5,8,5]])
print("THE 2D ARRAY : ", arr2)
print("THE SUM OF THE COLUMNS OF THE ARRAY : ", arr2.sum(axis =0))

max_stars_repo_path: TWO D ARRAY MANIPULATION.py

max_stars_repo_name: Vedakeerthi/Python-programs

max_issues_repo_path: TWO D ARRAY MANIPULATION.py

max_issues_repo_name: Vedakeerthi/Python-programs

max_forks_repo_path: TWO D ARRAY MANIPULATION.py

max_forks_repo_name: Vedakeerthi/Python-programs


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""GB2 density.

"""
from __future__ import print_function, division

import numpy as np
import scipy.special as ss
import scipy.integrate as si
import matplotlib.pylab as plt
import seaborn as sns

class GB2(object):

    def __init__(self, a=1.5, b=1.03, p=.2, q=.2, T=1):

        self.p, self.q = p, q
        # Scaling
        self.a = 100 * a / T**.5
        # Martingale restriction
        self.b = np.ones_like(b) * ss.beta(p, q) \
            / ss.beta(p+1/self.a, q-1/self.a)

    def compute_mom(self):
        self.mean = self.mom(1)
        self.var = self.mom(2) - self.mean**2
        self.std = self.var**.5
        m1 = self.mom(1)
        m2 = self.mom(2)
        m3 = self.mom(3)
        m4 = self.mom(4)
        self.skew = (m3 - 3*m1*m2 + 2*m1**3) / self.var**(3/2)
        self.kurt = (m4 - 4*m1*m3 + 6*m1**2*m2 - 3*m1**4) / self.var**2 - 3

    def density(self, x):
        a, b, p, q = self.a, self.b, self.p, self.q
        return a * x**(a*p-1) / (b**(a*p) * ss.beta(p, q) \
            * (1 + (x/b)**a)**(p+q))

    def mom(self, n):
        a, b, p, q = self.a, self.b, self.p, self.q
        return b**n * ss.beta(p+n/a, q-n/a) / ss.beta(p, q)

class GB2log(GB2):

    def __init__(self, a=1.5, b=1.03, p=.2, q=.2, c=.0, T=1):
        #super(ClassName, self).__init__()
        self.N = np.max([np.array(a).size, np.array(b).size,
                         np.array(p).size, np.array(q).size,
                         np.array(T).size, np.array(c).size])
        self.p = p / 100
        self.q = q / 100
        # Scaling
        self.a = 100 * a / T**(.5 - c)
        # Martingale restriction
        self.b = np.ones_like(b) * ss.beta(p, q) \
            / ss.beta(p+1/self.a, q-1/self.a)

    def density(self, x):
        return super(GB2log, self).density(np.exp(x)) * np.exp(x)

    def mom(self, n):
        out = []
        for i in range(self.N):
            f = lambda x: x**n * self.density(x)[i]
            #print(f(np.linspace(1,2,10)))
            out.append(si.quad(f, -3, 3)[0])
            #out.append(si.quadrature(f, -3, 3)[0])
        return np.array(out)

def test_params():
    #x = np.linspace(.8, 1.2, 1e2)
    x = np.linspace(-.2, .2, 1e2)

    num = 5
    range_a = np.linspace(1, 2, num)
    range_b = np.linspace(1., 1.1, num)
    range_p = np.linspace(.1, .4, num)
    range_q = np.linspace(.1, .4, num)
    range_T = np.linspace(30, 365, num) / 365

    args_def = {'a' : range_a.mean(), 'b' : range_b.mean(),
                'p' : range_p.mean(), 'q' : range_q.mean(),
                'T' : range_T.mean()}

    ranges = {'a' : range_a, 'b' : range_b,
              'p' : range_p, 'q' : range_q, 'T' : range_T}

    fig, axes = plt.subplots(nrows = len(ranges), figsize = (6,12))
    for name, a in zip(sorted(ranges.keys()), axes):
        args = args_def.copy()
        for pi in ranges[name]:
            args[name] = pi
            f = GB2(**args).density(x)
            a.plot(x, f, label = pi)
        a.legend(title = name)
    plt.show()

def draw_moments():

    num = 10
    range_a = np.linspace(1, 3, num)
    range_c = np.linspace(-.1, .5, num)
    range_p = np.linspace(.1, .3, num) * 100
    range_q = np.linspace(.1, .3, num) * 100
    range_T = np.linspace(30, 365, num) / 365

    args_def = {'T' : range_T.mean(),
                'a' : range_a.mean(),
                'c' : range_c.mean(),
                'p' : range_p.mean(),
                'q' : range_q.mean()}

    moms = ['Std', 'Skew', 'Kurt']
    ranges = {'T' : range_T, 'a' : range_a, 'c': range_c,
              'p' : range_p, 'q' : range_q}
    params = ['T', 'a', 'c', 'p', 'q']

    fig, axes = plt.subplots(nrows=len(moms), ncols=len(ranges), sharey='row',
                             sharex='col', figsize=(7, 6))
    for col, param in enumerate(params):
        args = args_def.copy()
        args[param] = ranges[param]
        #gb2 = GB2(**args)
        gb2 = GB2log(**args)
        gb2.compute_mom()
        data = {'Std': 100 * gb2.std * (365 / args['T'])**.5,
                'Skew': gb2.skew, 'Kurt': gb2.kurt}

        for row, mom in enumerate(moms):

            axes[row, col].plot(ranges[param], data[mom], lw=4)
            axes[row, col].axvline(args_def[param], c='red', linestyle='--')
            axes[row, col].set_xlim([ranges[param].min(), ranges[param].max()])
            axes[row, 0].set_ylabel(mom)
            if mom == 'Kurt':
                axes[row, 0].set_ylim([2.5, 4.5])

        axes[-1, col].set_xlabel(param)
        axes[-1, col].locator_params(axis='x', nbins=4)

    plt.tight_layout()
    plt.savefig('../plots/paper/gb2_moments.eps',
                bbox_inches='tight', pad_inches=0)
    plt.show()

def draw_term_structure():

    num = 3
    range_a = np.linspace(4, 5, num)
    range_c = np.linspace(-.2, .2, num)
    range_T = np.linspace(.2, 1, 20)

    args_def = {'a': 3.5, 'c': 0, 'p' : 20, 'q' : 20, 'T': range_T}

    ranges = {'a' : range_a, 'c': range_c}
    params = ['a', 'c']

    fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True,
                             sharex='col', figsize=(7, 3))

    for col, param in enumerate(params):
        for pvalue in ranges[param]:

            args = args_def.copy()
            args[param] = pvalue
            #gb2 = GB2(**args)
            gb2 = GB2log(**args)
            gb2.compute_mom()
            data = 100 * gb2.std * (365 / args['T'])**.5

            axes[col].plot(range_T, data, lw=4)

        axes[col].legend(ranges[param], title=param)

        axes[col].set_xlabel('T')

    axes[0].set_ylabel('Std')
    plt.tight_layout()
    plt.savefig('../plots/paper/gb2_term_structure.eps',
                bbox_inches='tight', pad_inches=.01)
    plt.show()

if __name__ == '__main__':

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

    #test_params()
#    test_moments()

#    draw_moments()

    draw_term_structure()

max_stars_repo_name: khrapovs/rndensity

max_issues_repo_name: akoryagin/rndensity

max_forks_repo_name: akoryagin/rndensity

reason: import numpy,import scipy


text:
import os

import click
import numpy as np
from joblib import Parallel, delayed

import trisicell as tsc

@click.command(short_help="Run SCITE.")
@click.argument(
    "genotype_file",
    required=True,
    type=click.Path(
        exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
    ),
)
@click.argument(
    "alpha",
    required=True,
    type=float,
)
@click.argument(
    "beta",
    required=True,
    type=float,
)
@click.option(
    "--n_iters",
    "-l",
    default=1000000,
    type=int,
    show_default=True,
    help="Number of iterations.",
)
@click.option(
    "--n_restarts",
    "-r",
    default=3,
    type=int,
    show_default=True,
    help="Number of restarts.",
)
@click.option(
    "--experiment",
    "-e",
    is_flag=True,
    default=False,
    type=bool,
    show_default=True,
    help="Is in experiment mode.",
)
@click.option(
    "--n_hours",
    "-h",
    default=24,
    type=float,
    show_default=True,
    help="Number of hours for the experiment part.",
)
def scite(genotype_file, alpha, beta, n_iters, n_restarts, experiment, n_hours):
    """Tree inference for single-cell data :cite:`SCITE`.

    trisicell scite input.SC 0.0001 0.1 -l 1000000 -r 3 -e -h 24
    """

    outfile = os.path.splitext(genotype_file)[0]

    tsc.settings.verbosity = "info"

    df_in = tsc.io.read(genotype_file)
    if not experiment:
        tsc.settings.logfile = f"{outfile}.scite.log"
        df_out = tsc.tl.scite(
            df_in,
            alpha=alpha,
            beta=beta,
            n_iters=n_iters,
            n_restarts=n_restarts,
        )
        tsc.io.write(df_out, f"{outfile}.scite.CFMatrix")
    else:
        tsc.settings.logfile = f"{outfile}.scite.log"
        df_out, running_time, _, _ = tsc.tl.scite(
            df_in,
            alpha=alpha,
            beta=beta,
            n_iters=30000,
            n_restarts=1,
            experiment=True,
        )
        n_iters = int(2 * 30000 * n_hours * 60 * 60 / running_time)

        def run(i):
            do, r, s, b = tsc.tl.scite(
                df_in,
                alpha=alpha,
                beta=beta,
                n_iters=n_iters,
                n_restarts=1,
                experiment=True,
            )
            return do, r, s, b

        output = Parallel(n_jobs=3)(delayed(run)(i) for i in range(3))

        scores = [x[2] for x in output]
        betas = [x[3] for x in output]
        best_i = np.argmax(scores)
        df_out = output[best_i][0]

        tsc.ul.stat(df_in, df_out, alpha, beta, output[best_i][1])
        tsc.logg.info(f"score: {output[best_i][2]}")
        tsc.logg.info(f"beta: {output[best_i][3]}")
        tsc.logg.info(f"n_iters: {n_iters}")
        tsc.logg.info(f"scores: {','.join(list(map(str, scores)))}")
        tsc.logg.info(f"betas: {','.join(list(map(str, betas)))}")
        tsc.logg.info(f"picked: {best_i}")

        tsc.io.write(df_out, f"{outfile}.scite.CFMatrix")

    return None

max_stars_repo_name: faridrashidi/trisicell

max_issues_repo_name: faridrashidi/trisicell

max_forks_repo_name: faridrashidi/trisicell


text:
from ruamel.yaml import YAML, dump, RoundTripDumper
from raisimGymTorch.env.bin.rsg_ouzel import RaisimGymEnv
from raisimGymTorch.env.bin.rsg_ouzel import NormalSampler
from raisimGymTorch.env.RaisimGymVecEnv import RaisimGymVecEnv as VecEnv
from raisimGymTorch.helper.raisim_gym_helper import ConfigurationSaver, load_param, tensorboard_launcher
import os
import math
import time
import raisimGymTorch.algo.ppo.module as ppo_module
import raisimGymTorch.algo.ppo.ppo as PPO
import torch.nn as nn
import numpy as np
import torch
import datetime
import argparse

print("started runner.py script")

# task specification
task_name = "ouzel_only_planning"

# configuration
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--mode", help="set mode either train or test", type=str, default="train")
parser.add_argument("-w", "--weight", help="pre-trained weight path", type=str, default="")
parser.add_argument("-c", "--config", help="config file name", type=str, default="cfg.yaml")
args = parser.parse_args()
mode = args.mode
weight_path = args.weight

# check if gpu is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"using device {device}!")

# directories
task_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
home_path = task_path + "/../../../../.."

# config
config_fpath = os.path.join(task_path, "config", args.config)
print(f"loading config file {config_fpath}")
cfg = YAML().load(open(config_fpath, "r"))

# create environment from the configuration file
env = VecEnv(RaisimGymEnv(home_path + "/rsc", dump(cfg["environment"], Dumper=RoundTripDumper)), normalize_ob=True)

# shortcuts
ob_dim = env.num_obs
act_dim = env.num_acts
num_threads = cfg["environment"]["num_threads"]

# Training
n_steps = math.floor(cfg["environment"]["max_time"] / cfg["environment"]["control_dt"])
total_steps = n_steps * env.num_envs

avg_rewards = []

activation = None
if cfg["activation"] == "tanh":
    activation = nn.Tanh
else:
    print("Error ! No valid activation given in cfg file")

actor = ppo_module.Actor(
    ppo_module.MLP(cfg["architecture"]["policy_net"], activation, ob_dim, act_dim),
    ppo_module.MultivariateGaussianDiagonalCovariance(act_dim, env.num_envs, 1.0, NormalSampler(act_dim), cfg["seed"]),
    device,
)
critic = ppo_module.Critic(ppo_module.MLP(cfg["architecture"]["value_net"], activation, ob_dim, 1), device)

saver = ConfigurationSaver(
    log_dir=home_path + "/raisimGymTorch/data/" + task_name, save_items=[config_fpath, task_path + "/Environment.hpp"]
)
# tensorboard_launcher(saver.data_dir+"/..")  # press refresh (F5) after the first ppo update

learning_rate_schedule = "adaptive" if cfg["use_adaptive_lr"] else "constant"
ppo = PPO.PPO(
    actor=actor,
    critic=critic,
    num_envs=cfg["environment"]["num_envs"],
    num_transitions_per_env=n_steps,
    num_learning_epochs=4,
    gamma=0.996,
    lam=0.95,
    num_mini_batches=4,
    device=device,
    log_dir=saver.data_dir,
    shuffle_batch=False,
    learning_rate_schedule=learning_rate_schedule,
    learning_rate=cfg["learning_rate"],
)

on_cluster = cfg["on_cluster"]

if mode == "retrain":
    load_param(weight_path, env, actor, critic, ppo.optimizer, saver.data_dir)

for update in range(1000000):
    start = time.time()
    env.reset()
    reward_ll_sum = 0
    done_sum = 0
    average_dones = 0.0

    if update % cfg["environment"]["eval_every_n"] == 0:
        print("Visualizing and evaluating the current policy")
        torch.save(
            {
                "actor_architecture_state_dict": actor.architecture.state_dict(),
                "actor_distribution_state_dict": actor.distribution.state_dict(),
                "critic_architecture_state_dict": critic.architecture.state_dict(),
                "optimizer_state_dict": ppo.optimizer.state_dict(),
            },
            saver.data_dir + "/full_" + str(update) + ".pt",
        )
        # we create another graph just to demonstrate the save/load method
        loaded_graph = ppo_module.MLP(cfg["architecture"]["policy_net"], activation, ob_dim, act_dim)
        loaded_graph.load_state_dict(
            torch.load(saver.data_dir + "/full_" + str(update) + ".pt")["actor_architecture_state_dict"]
        )

        if not on_cluster:
            env.turn_on_visualization()
            env.start_video_recording(
                datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "policy_" + str(update) + ".mp4"
            )

        for step in range(n_steps * 2):
            with torch.no_grad():
                frame_start = time.time()
                obs = env.observe(False)
                action_ll = loaded_graph.architecture(torch.from_numpy(obs).cpu())
                reward_ll, dones = env.step(action_ll.cpu().detach().numpy())
                frame_end = time.time()
                wait_time = cfg["environment"]["control_dt"] - (frame_end - frame_start)
                if wait_time > 0.0 and not on_cluster:
                    time.sleep(wait_time)

        if not on_cluster:
            env.stop_video_recording()
            env.turn_off_visualization()

        env.reset()
        env.save_scaling(saver.data_dir, str(update))

    # actual training
    for step in range(n_steps):
        obs = env.observe()
        action = ppo.act(obs)
        reward, dones = env.step(action)
        ppo.step(value_obs=obs, rews=reward, dones=dones)
        done_sum = done_sum + np.sum(dones)
        reward_ll_sum = reward_ll_sum + np.sum(reward)

    # take st step to get value obs
    obs = env.observe()
    ppo.update(actor_obs=obs, value_obs=obs, log_this_iteration=update % 10 == 0, update=update)
    average_ll_performance = reward_ll_sum / total_steps
    average_dones = done_sum / total_steps
    avg_rewards.append(average_ll_performance)

    actor.update()
    actor.distribution.enforce_minimum_std((torch.ones(act_dim) * 0.2).to(device))

    # curriculum update. Implement it in Environment.hpp
    env.curriculum_callback()

    end = time.time()

    print("----------------------------------------------------")
    print("{:>6}th iteration".format(update))
    print("{:<40} {:>6}".format("average ll reward: ", "{:0.10f}".format(average_ll_performance)))
    print("{:<40} {:>6}".format("dones: ", "{:0.6f}".format(average_dones)))
    print("{:<40} {:>6}".format("time elapsed in this iteration: ", "{:6.4f}".format(end - start)))
    print("{:<40} {:>6}".format("fps: ", "{:6.0f}".format(total_steps / (end - start))))
    print(
        "{:<40} {:>6}".format(
            "real time factor: ", "{:6.0f}".format(total_steps / (end - start) * cfg["environment"]["control_dt"])
        )
    )
    print("----------------------------------------------------\n")

max_stars_repo_name: vschorp/raisimLib

max_issues_repo_name: vschorp/raisimLib

max_forks_repo_name: vschorp/raisimLib


text:
from flask import Flask, request, render_template, Response
import pandas as pd
import sys
import numpy as np
import jinja2
import json
import os
import ast
from PIL import Image
import pickle
import PIL
import matplotlib.pyplot as plt

app = Flask(__name__)

class Model():
    pass

@app.route("/")
def main():

    info_list = list()
    with open('models/index.txt', 'r') as f:
        index_list = f.readlines()
    for line in index_list:
        info_list.append(ast.literal_eval(line))
    return render_template('home.html', info_list=info_list)

@app.route('/report', methods=['GET','POST'])
def report_page():
    summary = None
    model = None
    with open('app/static/meta_images/meta_map.txt', 'r') as f:
        meta_map = ast.literal_eval(f.readline())

    with open('models/index.txt', 'r') as f:
        info_list = f.readlines()
    for line in info_list:
        summary = ast.literal_eval(line)
        # breakpoint()
        try:
            if 'Generate Report' == request.form[str(summary['id'])]:
                break
        except:
            pass

    with open(summary['pickle path'], 'rb') as fb:
        model = pickle.load(fb)

    wrong = model.preds[model.preds.actual_class != model.preds.predicted_class]
    w_samples = wrong.image_location.sample(min(len(wrong),75))

    right = model.preds[model.preds.actual_class == model.preds.predicted_class]
    r_samples = right.image_location.sample(min(len(right),75))

    return render_template(
        'report.html',
        summary=summary,
        fit_time = model.fit_time,
        description = model.description,
        filters = model.filters,
        architecture = model.architecture,
        model_summary = model.summary,
        wrong_list=w_samples,
        right_list=r_samples,
        meta_map=meta_map,
        most_tp = list(model.class_data.most_tp),
        least_tp = list(model.class_data.least_tp),
        acc = list(model.class_data.acc),
        class_preds = list(model.class_data.class_pred)
        )

@app.route('/final', methods=['GET','POST'])
def final():
    final_eval = list()
    with open('app/static/final_report/final_eval.txt', 'r') as f:
        for line in f.readlines():
            final_eval.append(line.split(','))
    return render_template('final.html', final_eval=final_eval)

@app.route('/about', methods=['GET','POST'])
def about():
    return render_template('about.html')

app.run(host='0.0.0.0', port=8080, debug=True)

max_stars_repo_name: gavinraym/traffic

max_issues_repo_name: gavinraym/traffic

max_forks_repo_name: gavinraym/traffic


text:
import argparse
import glob
import os
import warnings

import numpy as np

def read_joint_names(filename):
    """Read motion capture's body joint names from file

      Args:
          filename:     file name to read

      Returns:
          joint_names:  list of joint names
    """

    with open(filename, 'r') as f:
        org = f.read()
        joint_names = org.split(',')

    return joint_names

def compute_jerks(data, dim=3):
    """Compute jerk between adjacent frames

      Args:
          data:         array containing joint positions of gesture
          dim:          gesture dimensionality

      Returns:
          np.ndarray:   jerks of each joint averaged over all frames
    """

    # Third derivative of position is jerk
    jerks = np.diff(data, n=3, axis=0)

    num_jerks = jerks.shape[0]
    num_joints = jerks.shape[1] // dim

    jerk_norms = np.zeros((num_jerks, num_joints))

    for i in range(num_jerks):
        for j in range(num_joints):
            x1 = j * dim + 0
            x2 = j * dim + dim
            jerk_norms[i, j] = np.linalg.norm(jerks[i, x1:x2])

    return np.mean(jerk_norms, axis=0)

def compute_acceleration(data, dim=3):
    """Compute acceleration between adjacent frames

      Args:
          data:         array containing joint positions of gesture
          dim:          gesture dimensionality

      Returns:
          np.ndarray:   accelerations of each joint averaged over all frames
    """

    # Second derivative of position is acceleration
    accs = np.diff(data, n=2, axis=0)

    num_accs = accs.shape[0]
    num_joints = accs.shape[1] // dim

    acc_norms = np.zeros((num_accs, num_joints))

    for i in range(num_accs):
        for j in range(num_joints):
            x1 = j * dim + 0
            x2 = j * dim + dim
            acc_norms[i, j] = np.linalg.norm(accs[i, x1:x2])

    return np.mean(acc_norms, axis=0)

def save_result(lines, out_dir, measure):
    """Write computed measure to CSV

      Args:
          lines:        list of strings to be written
          out_dir:      output directory
          measure:      used measure
    """

    # Make output directory
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    if measure == "jerk":
        outname = os.path.join(out_dir, 'aj.csv')
    elif measure == "acceleration":
        outname = os.path.join(out_dir, 'aa.csv')

    with open(outname, 'w') as out_file:
        out_file.writelines(lines)

    print('More detailed result was writen to the file: ' + outname)
    print('')

def main():
    measures = {
        'jerk': compute_jerks,
        'acceleration': compute_acceleration,
    }

    parser = argparse.ArgumentParser(
        description='Calculate prediction errors')
    parser.add_argument('--original', '-o', default='data/original',
                        help='Original gesture directory')
    parser.add_argument('--predicted', '-p', default='data/predicted',
                        help='Predicted gesture directory')
    parser.add_argument('--joints', '-j', default='joints.txt',
                        help='Joint name file')
    parser.add_argument('--gesture', '-g', required=True,
                        help='Directory storing predicted txt files')
    parser.add_argument('--measure', '-m', default='jerk',
                        help='Measure to calculate (jerk or acceleration)')
    parser.add_argument('--select', '-s', nargs='+',
                        help='Joint subset to compute (if omitted, use all)')
    parser.add_argument('--out', default='result',
                        help='Directory to output the result')
    args = parser.parse_args()

    predicted_dir = os.path.join(args.predicted, args.gesture)

    original_files = sorted(glob.glob(os.path.join(args.original, '*.txt')))
    predicted_files = sorted(glob.glob(os.path.join(predicted_dir, '*.txt')))

    # Check number of files
    if len(original_files) != len(predicted_files):
        warnings.warn('Inconsistent number of files : {} vs {}'
                      ''.format(len(original_files), len(predicted_files)),
                      RuntimeWarning)

    # Check if error measure was correct
    if args.measure not in measures:
        raise ValueError('Unknown measure: \'{}\'. Choose from {}'
                         ''.format(args.measure, list(measures.keys())))

    """joint_names = read_joint_names(args.joints)

    if args.select is not None:
        selected_joints = []
        for s in args.select:
            try:
                index = joint_names.index(s)
            except ValueError:
                print('Ignore invalid joint: {}'.format(s))
            else:
                selected_joints.append(index)
        selected_joints.sort()

        if len(selected_joints) == 0:
            selected_joints = range(len(joint_names))
            print('No valid joints are selected. Use all joints')
    else:
        # Use all joints
        selected_joints = range(len(joint_names))

    joint_names = [joint_names[s] for s in selected_joints]"""
    original_out_lines = ['Fake first line \n'] #[','.join(['file'] + joint_names) + '\n']
    predicted_out_lines = ['Fake first line \n'] # [','.join(['file'] + joint_names) + '\n']

    original_values = []
    predicted_values = []
    for original_file, predicted_file in zip(original_files, predicted_files):
        original = np.loadtxt(original_file)
        predicted = np.loadtxt(predicted_file)

        if original.shape[0] != predicted.shape[0]:
            # Cut them to the same length
            length = min(original.shape[0], predicted.shape[0])
            original = original[:length]
            predicted = predicted[:length]

        original_value = measures[args.measure](original)#[selected_joints]
        predicted_value = measures[args.measure](predicted)#[selected_joints]

        original_values.append(original_value)
        predicted_values.append(predicted_value)

        basename = os.path.basename(original_file)
        original_line = basename
        predicted_line = basename
        for ov, pv in zip(original_value, predicted_value):
            original_line += ',' + str(ov)
            predicted_line += ',' + str(pv)
        original_line += '\n'
        predicted_line += '\n'

        original_out_lines.append(original_line)
        predicted_out_lines.append(predicted_line)

    original_average_line = 'Average'
    predicted_average_line = 'Average'
    original_avgs = np.mean(original_values, axis=0)
    predicted_avgs = np.mean(predicted_values, axis=0)
    for oa, pa in zip(original_avgs, predicted_avgs):
        original_average_line += ',' + str(oa)
        predicted_average_line += ',' + str(pa)

    original_out_lines.append(original_average_line)
    predicted_out_lines.append(predicted_average_line)

    original_out_dir = os.path.join(args.out, 'original')
    predicted_out_dir = os.path.join(args.out, args.gesture)

    save_result(original_out_lines, original_out_dir, args.measure)
    save_result(predicted_out_lines, predicted_out_dir, args.measure)

    if args.measure == 'jerk':
        print('AJ:')
    elif args.measure == 'acceleration':
        print('AA:')
    print('original: {:.2f}'.format(np.mean(original_values)))
    print('predicted: {:.2f}'.format(np.mean(predicted_values)))

if __name__ == '__main__':
    main()


text:
# https://deeplearningcourses.com/c/deep-learning-advanced-nlp
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Model
from keras.layers import Input, LSTM, GRU, Bidirectional
import numpy as np
import matplotlib.pyplot as plt

import keras.backend as K
if len(K.tensorflow_backend._get_available_gpus()) > 0:
  from keras.layers import CuDNNLSTM as LSTM
  from keras.layers import CuDNNGRU as GRU

T = 8
D = 2
M = 3

X = np.random.randn(1, T, D)

input_ = Input(shape=(T, D))
# rnn = Bidirectional(LSTM(M, return_state=True, return_sequences=True))
rnn = Bidirectional(LSTM(M, return_state=True, return_sequences=False))
x = rnn(input_)

model = Model(inputs=input_, outputs=x)
o, h1, c1, h2, c2 = model.predict(X)
print("o:", o)
print("o.shape:", o.shape)
print("h1:", h1)
print("c1:", c1)
print("h2:", h2)
print("c2:", c2)

max_stars_repo_name: JouniVatanen/NLP-and-Deep-Learning


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: 03_mulearn.ipynb (unless otherwise specified).

__all__ = ['logger', 'FuzzyInductor']

# Cell

import numpy as np
from warnings import warn
import copy

from sklearn.base import BaseEstimator, RegressorMixin
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.utils import check_random_state

import mulearn.kernel as kernel
import mulearn.fuzzifier as fuzz
from .optimization import solve_optimization
from .optimization import solve_optimization_tensorflow

import logging

logger = logging.getLogger(__name__)

# Cell

class FuzzyInductor(BaseEstimator, RegressorMixin):

    def __init__(self,
                 c=1,
                 k=kernel.GaussianKernel(),
                 #sample_generator=None,
                 fuzzifier=(fuzz.ExponentialFuzzifier, {}),
                 #fuzzifier=fuzz.ExponentialFuzzifier,
                 #fuzzifier_opts = {},
                 solve_strategy=(solve_optimization_tensorflow, {}),
                 random_state=None,
                 #return_vars=False,
                 return_profile=False):
        r'''Creates an instance of `FuzzyInductor`

        - `c`: trade-off constant (`float`).

        - `k`: kernel function (`mulearn.kernel.Kernel`).

        - `fuzzifier`: fuzzifier mapping distance values to membership
           degrees (tuple containing a `mulearn.fuzzifiers.Fuzzifier`
           subclass F and a dictionary holding fixed parameters to be
           passed to the constructor of F).

        - `solve_strategy`: strategy to be used to solve optimization (tuple
           containing the reference to an optimization function and a
           dictionary holding optimization parameters).

        - `random_state`: seed of pseudorandom generator (`int`).

        - `return_profile`: flag triggering the generation of the profile of
          the learnt fuzzy set (`bool`).

        '''

        self.c = c
        self.k = k
        #self.sample_generator = sample_generator

        #self.fuzzifier = fuzzifier_descr[0]
        #self.fuzzifier_opts = fuzzifier_descr[1]
        self.solve_strategy = solve_strategy
        self.fuzzifier = fuzzifier
        self.random_state = random_state
        #self.return_vars = return_vars
        self.return_profile = return_profile

    def fix_object_state(self, X, y):
        self.X = X
        self.y = y

        def x_to_sq_dist(x_new):
            ret = self.k.compute(x_new, x_new) \
                  - 2 * np.array([self.k.compute(x_i, x_new)
                                  for x_i in X]).dot(self.chis_) \
                  + self.fixed_term_
            return ret
        self.x_to_sq_dist_ = x_to_sq_dist

        self.chi_SV_index_ = [i for i, (chi, mu) in enumerate(zip(self.chis_,
                                                                  y))
                              if -self.c * (1-mu) < chi < self.c * mu]

        #self.chi_SV_index_ = [i for i in range(len(self.chis)_) \
        #        if -self.c*(1-self.mu[i]) < self.chis_[i] < self.c*self.mu[i]]

        chi_sq_radius = map(x_to_sq_dist, X[self.chi_SV_index_])
        chi_sq_radius = list(chi_sq_radius)
        #chi_sq_radius = [x_to_sq_dist(x[i]) for i in chi_SV_index]

        if len(chi_sq_radius) == 0:
            self.estimated_membership_ = None
            self.train_error_ = np.inf
            self.chis_ = None
            self.profile = None
            logger.warn('No support vectors found')
            return self
            #raise ValueError('No support vectors found')

        self.sq_radius_ = np.mean(chi_sq_radius)
        #num_samples = 500

        #if self.sample_generator is None:
        #    self.sample_generator = lambda x: x

        #sample = map(self.sample_generator,
        #             self.random_state_.random_sample(num_samples))
        #sample = self.sample_generator(num_samples)

        fuzzifier = self.fuzzifier[0](X, y, **self.fuzzifier[1])
        result = fuzzifier.get_membership(
                self.sq_radius_, # sq_radius, was SV_square_distance_
                self.x_to_sq_dist_,
                return_profile=self.return_profile)

        if self.return_profile:
            self.estimated_membership_, self.profile_ = result
        else:
            self.estimated_membership_ = result[0]

    def fit(self, X, y, **kwargs):
        r'''Induces the membership function starting from a labeled sample

        - `X`: vectors in data space (iterable of `float` vectors having the
          same length).

        - `y`: membership for the vectors in `X` (iterable of `float` having
          the same length of `X`).

        Returns: self

        Throws:

        - ValueError if the values in `y` are not between 0 and 1, if `X` and
          have different lengths, or if `X` contains elements of different
          lengths.

        '''

        if type(X) is not np.array:
            X = np.array(X)

        for e in y:
            if e < 0 or e > 1:
                raise ValueError('`y` values should belong to [0, 1]')

        check_X_y(X, y)
        self.random_state_ = check_random_state(self.random_state)

        if 'warm_start' in kwargs and kwargs['warm_start']:
            check_is_fitted(self, ['chis_', 'estimated_membership_'])
            self.solve_strategy[1]['init'] = self.chis_

        self.chis_ = solve_optimization(X, y,
                                        self.c, self.k,
                                        self.solve_strategy[0],
                                        **self.solve_strategy[1])

        if type(self.k) is kernel.PrecomputedKernel:
            self.gram_ = self.k.kernel_computations
        else:
            self.gram_ = np.array([[self.k.compute(x1, x2) for x1 in X]
                                    for x2 in X])
        self.fixed_term_ = np.array(self.chis_).dot(self.gram_.dot(self.chis_))

        self.fix_object_state(X, y)

        self.train_error_ = np.mean([(self.estimated_membership_(x) - mu)**2
                                    for x, mu in zip(X, y)])

        return self

    def predict(self, X):
        r'''Computes predictions for the membership grades of one or more
        vectors.

        - `X`: vectors in data space (iterable of vectors having the same
          length).

        Returns: array of the predictions for each value in `X`.

        '''

        check_is_fitted(self, ['chis_', 'estimated_membership_'])
        X = check_array(X)
        return np.array([self.estimated_membership_(x) for x in X])

    def score(self, X, y):
        r'''Computes the negated accuracy of the learnt fuzzy set against
        a set of labeled data.

        - `X`: vectors in data space (iterable of `float` vectors having the
          same length).

        - `y`: membership for the vectors in `X` (iterable of `float` having
          the same length of `X`).

        Returns: negated accuracy of the predictions done on the elements in
        `X` w.r.t. the labels in `y`.

        '''

        check_X_y(X, y)

        if self.estimated_membership_ is None:
            return -np.inf
        else:
            return -np.mean([(self.estimated_membership_(x) - mu)**2
                             for x, mu in zip(X, y)])

    def __getstate__(self):
        d = copy.deepcopy(self.__dict__)
        del d['estimated_membership_']
        del d['x_to_sq_dist_']
        return d

    def __setstate__(self, d):
        self.__dict__ = d
        self.fix_object_state(self.X, self.y)
        self.__dict__['estimated_membership_'] = self.estimated_membership_
        self.__dict__['x_to_sq_dist_'] = self.x_to_sq_dist_

max_stars_repo_name: vishalbelsare/mulearn

max_issues_repo_name: vishalbelsare/mulearn

max_forks_repo_name: vishalbelsare/mulearn


text:
import argparse, os
import os.path as osp
import numpy as np

from env_frontend import MantsinenBasic
from common.server_utils import is_server_running
from time import sleep
from stable_baselines.ppo.ppod import PPOD as ppo
from stable_baselines.ppo.policies import PPOPolicy as policy
from stable_baselines.common.vec_env.mevea_vec_env import MeveaVecEnv
from common.data_utils import get_test_waypoints
from config import *

def make_env(env_class, *args):
    fn = lambda: env_class(*args)
    return fn

if __name__ == '__main__':

    #os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

    # process arguments

    parser = argparse.ArgumentParser()
    parser.add_argument('-w', '--waypoints', help='Text file with waypoints.', nargs='+', type=int, default=[32, 33, 34, 35])
    parser.add_argument('-m', '--model', help='Model directory.', default='models/mevea/mantsinen/ppo')
    parser.add_argument('-c', '--checkpoint', help='Checkpoint', default='first', choices=['first', 'last', 'best'])
    parser.add_argument('-v', '--video', help='Record video?', type=bool, default=True)
    args = parser.parse_args()

    chkpt_dir = args.model

    # check that server is running

    while not is_server_running(server):
        print('Start the server: python3 env_server.py')
        sleep(sleep_interval)

    # extract waypoints

    fpaths = []
    waypoints = []
    for i in args.waypoints:
        fpath = osp.join(waypoints_dir, f'wps{i}.txt')
        fpaths.append(fpath)
        waypoints.append(get_test_waypoints(fpath))

    # create environment

    env_fns = [make_env(
        MantsinenBasic,
        0,
        model_path,
        model_dir,
        signal_dir,
        server,
        waypoints,
        nsteps,
        lookback,
        use_inputs,
        use_outputs,
        action_scale,
        wp_size,
        tstep,
        bonus
    )]
    env = MeveaVecEnv(env_fns)

    # load model and run it in demo mode

    try:
        model = ppo(policy, env, 1, policy_kwargs=dict(net_arch=ppo_net_arch), batch_size=batch_size, n_steps=nsteps, action_scale=action_scale,
                    model_path=chkpt_dir, chkpt_name=args.checkpoint, tensorboard_log='tensorboard_log', verbose=1)
        cp_name = osp.basename(args.checkpoint)
        for fpath in fpaths:
            wp_name = osp.basename(fpath)
            img_fname = f"{wp_name.split('.txt')[0]}_{cp_name}.png"
            img_fpath = osp.join(img_output, 'ppo', img_fname)
            if args.video:
                video_fname = f"{wp_name.split('.txt')[0]}_{cp_name}.mp4"
                video_fpath = osp.join(video_output, 'ppo', video_fname)
                print(f'Recording to {video_fpath}')
            else:
                video_fpath = None
            model.demo(img_file=img_fpath, video_file=video_fpath)
    except Exception as e:
        print(e)


text:
import pickle
import rdflib
import numpy as np
from typing import List
from scipy.sparse import csr_matrix, vstack

food_ns = rdflib.Namespace('http://idea.rpi.edu/heals/kb/')

class RecToVec:

    def __init__(self, graph,
                 food_path_file:str='../data/out/foodon_to_root_path.pkl',
                 food_index_file:str='../data/out/food_index_dict.pkl'):
        with open(food_path_file, 'rb') as f:
            self.food_path = pickle.load(f)
        with open(food_index_file, 'rb') as f:
            self.food_index = pickle.load(f)
        self.vector_shape = (1, len(self.food_index.keys()))
        self.graph = graph
        self.ing_to_foodon_dict = dict()
        self.ing_order = []
        self.foodon_ing_order = []
        for foodkg_food in graph.subjects(predicate=rdflib.namespace.RDF['type'], object=food_ns['ingredientname']):
            if foodkg_food is not None:
                equiv_food = graph.value(subject=foodkg_food, predicate=food_ns['equivalentFoodOnClass'])
                if equiv_food:
                    self.ing_to_foodon_dict[foodkg_food] = equiv_food
                    if equiv_food not in self.foodon_ing_order:
                        self.foodon_ing_order.append(equiv_food)
            self.ing_order.append(foodkg_food)

    def get_recipe_ingredients_and_weights(self, recipe_uri, unweighted=False, only_foodon_classes:bool=False):
        ing_uses = [ing_use for ing_use in self.graph.objects(recipe_uri, food_ns['uses'])]
        ing_names = []
        total_grams = 0
        use_grams = []
        if len(ing_uses) == 0:
            return None, None
        for ing_use in ing_uses:
            ing_name = self.graph.value(subject=ing_use, predicate=food_ns['ing_name'])
            if only_foodon_classes:
                foodon_eq = self.ing_to_foodon_dict.get(ing_name, 0)
                if foodon_eq:
                    ing_names.append(foodon_eq)
                else:
                    return None, None
            else:
                ing_names.append(ing_name)

            gram_quant = self.graph.value(subject=ing_use, predicate=food_ns['ing_computed_gram_quantity'])
            if gram_quant:
                use_grams.append(gram_quant.value)
                total_grams += gram_quant.value
            else:
                use_grams.append(1.0)
                total_grams += 1.0

        weights = []
        for ing_index, _ in enumerate(ing_uses):
            if unweighted:
                prop = 1 / len(ing_uses)
            else:
                prop = use_grams[ing_index] / total_grams
            weights.append(prop)
        return ing_names, weights

    def convert_recipe_to_vector(self, recipe_uri, unweighted=False, only_foodon_classes:bool = False):
        ing_names, weights = self.get_recipe_ingredients_and_weights(recipe_uri=recipe_uri, unweighted=unweighted, only_foodon_classes=only_foodon_classes)
        if not ing_names:
            return None

        return self.convert_ing_uses_to_vector(ings=ing_names, weights=weights)

    def convert_ing_uses_to_vector(self, ings: List[rdflib.URIRef], weights: List[float], using_path=True):
        recipe_vec = np.zeros(shape=self.vector_shape)
        for ing_index, ing in enumerate(ings):
            ing_weight = weights[ing_index]

            food_index = self.food_index[ing]
            if self.using_ic:
                recipe_vec[0, food_index] += ing_weight * self.food_to_ic.get(ing, 1)
            else:
                recipe_vec[0, food_index] += ing_weight

            if using_path:
                use_path = self.food_path.get(self.ing_to_foodon_dict.get(ing, ing), [])
                for path_food in use_path:
                    food_index = self.food_index[path_food]

                    recipe_vec[0, food_index] += ing_weight

        return recipe_vec

    def ing_at_index(self, index):
        return self.ing_order[index]

    def index_for_ing(self, ing):
        return self.food_index[ing]

    def create_substitution_matrix(self, ings: List[rdflib.URIRef], weights: List[float],
                                   to_remove_ing: rdflib.URIRef, to_remove_wt: float, only_foodon_classes=False):
        base_vec = self.convert_ing_uses_to_vector(ings=ings, weights=weights)
        remove_vec = self.convert_ing_uses_to_vector(ings=[to_remove_ing], weights=[to_remove_wt])
        context_vec = base_vec - remove_vec

        substitution_recipe_matrix = []
        if only_foodon_classes:
            for ing in self.foodon_ing_order:
                sub_vec = context_vec + self.convert_ing_uses_to_vector(ings=[ing], weights=[to_remove_wt])
                substitution_recipe_matrix.append(csr_matrix(sub_vec))
        else:
            for ing in self.ing_order:
                sub_vec = context_vec + self.convert_ing_uses_to_vector(ings=[ing], weights=[to_remove_wt])
                substitution_recipe_matrix.append(csr_matrix(sub_vec))

        stacked_matrix = vstack(substitution_recipe_matrix)

        return stacked_matrix

max_stars_repo_name: solashirai/FoodSubstitutionDataScripts

max_issues_repo_name: solashirai/FoodSubstitutionDataScripts

max_forks_repo_name: solashirai/FoodSubstitutionDataScripts


text:
import cv2 as cv
import numpy as np

img_rgb = cv.imread('mario.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('mario_template.png', cv.IMREAD_GRAYSCALE)

res = cv.matchTemplate(img_gray, template, cv.TM_CCORR_NORMED)

threshold = 0.9

loc = np.where(res >= threshold)

print(res)              # optional
print("\n\n",loc)           # optional
minVal, maxVal, minLoc, maxLoc = cv.minMaxLoc(res)        # optional
print('\nmax value :\t', maxVal,"\nmin value :\t", minVal)       # optional

width, height = template.shape[::-1]

for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + width, pt[1] + height), (0,0,255), 2)

cv.imshow('matcehd', img_rgb)
cv.waitKey(0)
cv.destroyAllWindows()

max_stars_repo_name: EnesMercan/Computer-Vision-Python

max_issues_repo_name: EnesMercan/Computer-Vision-Python

max_forks_repo_name: EnesMercan/Computer-Vision-Python