text:
import random
import numpy as np

class ReplayBuffer:
    def __init__(self, capacity):
        self.capacity = capacity
        self.buffer = []
        self.position = 0

    def push(self, state, action, reward, next_state, done):
        if len(self.buffer) < self.capacity:
            self.buffer.append(None)
        self.buffer[self.position] = (state, action, reward, next_state, done)
        self.position = (self.position + 1) % self.capacity

    def sample(self, batch_size):
        batch = random.sample(self.buffer, batch_size)
        state, action, reward, next_state, done = map(np.stack, zip(*batch))
        return state, action, reward, next_state, done

    def __len__(self):
        return len(self.buffer)

class AverageBuffer:
    def __init__(self, capacity = 10):
        self.capacity = capacity
        self.buffer = []
        self.index = 0

    def push(self, goal):
        if len(self.buffer) < self.capacity:
            self.buffer.append(None)

        self.buffer[self.index] = goal
        self.index = (self.index + 1) % self.capacity

    def average(self):
        return np.mean(self.buffer if len(self.buffer) > 0 else [0])

    def state_dict(self):
        buffer_dict = {}
        buffer_dict['index'] = self.index
        buffer_dict['bufferSize'] = len(self.buffer)

        for i in range(len(self.buffer)):
            buffer_dict[i] = self.buffer[i]

        return buffer_dict

    def load_state_dict(self, buffer_dict):
        self.index = buffer_dict['index']
        size = buffer_dict['bufferSize']

        for i in range(size):
            self.buffer.append(buffer_dict[i])

    def __len__(self):
        return len(self.buffer)


text:
import sys
import os
sys.path = [os.path.join(os.path.dirname(__file__), "..")] + sys.path
import physt
import numpy as np
import pytest
from physt import special

class TestPolar(object):
    def test_simple_create(self):
        data = np.array([[0.01, 0.01], [0.01, 0.99], [-1, .01], [-1, -.01]])
        x = data[:,0]
        y = data[:,1]
        h = special.polar_histogram(x, y, radial_bins=2, phi_bins=4)
        assert h.bin_count == 8
        assert np.array_equal([[1, 0, 0, 0], [1, 1, 1, 0]], h.frequencies)

    def test_transform(self):
        t = special.PolarHistogram.transform([1, 0])
        assert np.array_equal(t, [1, 0])

        t = special.PolarHistogram.transform([0, 2])
        assert np.allclose(t, [2, np.pi / 2])

        data = np.asarray([[1, 0], [0, 2]])
        t = special.PolarHistogram.transform(data)
        assert np.allclose(t, [[1, 0], [2, np.pi / 2]])

    def test_projection_types(self):
        data = np.array([[0.01, 0.01], [0.01, 0.99], [-1, .01], [-1, -.01]])
        x = data[:, 0]
        y = data[:, 1]
        h = special.polar_histogram(x, y, radial_bins=2, phi_bins=4)
        assert special.RadialHistogram == type(h.projection("r"))
        assert special.AzimuthalHistogram == type(h.projection("phi"))

class TestSpherical(object):
    def test_transform(self):
        t = special.SphericalHistogram.transform([0, 0, 1])
        assert np.array_equal(t,  [1, 0, 0])

        t = special.SphericalHistogram.transform([2, 2, 0])
        assert np.array_equal(t,  [np.sqrt(8), np.pi / 2, np.pi / 4])

        data = np.asarray([[3, 0, 0], [0, 0, 0], [0, .5, -.5]])
        expected = np.asarray([[3, np.pi / 2, 0], [0, 0, 0], [np.sqrt(.5), .75 * np.pi, np.pi / 2]])
        assert np.allclose(expected, special.SphericalHistogram.transform(data))

    def test_projection_types(self):
        h = special.spherical_histogram([[1, 2, 3], [2, 3, 4]])
        assert special.DirectionalHistogram == type(h.projection("phi", "theta"))
        assert special.DirectionalHistogram == type(h.projection("theta", "phi"))

class TestCylindricalSurface(object):
    def test_radius(self):
        h = special.cylindrical_histogram([[1,2,3], [2, 3, 4]])
        proj = h.projection("phi", "z")
        assert proj.radius > 1

    def test_projection_types(self):
        h = special.cylindrical_histogram([[1, 2, 3], [2, 3, 4]])
        proj = h.projection("phi", "z")
        assert special.AzimuthalHistogram == type(proj.projection("phi"))

class TestCylindrical(object):
    def test_transform(self):
        t = special.CylindricalHistogram.transform([0, 0, 1])
        assert np.array_equal(t,  [0, 0, 1])

        t = special.CylindricalHistogram.transform([2, 2, 2])
        assert np.array_equal(t,  [np.sqrt(8), np.pi / 4, 2])

        data = np.asarray([[3, 0, 0], [0, 0, 0], [0, .5, -.5]])
        expected = np.asarray([[3, 0, 0], [0, 0, 0], [.5, np.pi / 2, -.5]])
        assert np.allclose(expected, special.CylindricalHistogram.transform(data))

    def test_projection_types(self):
        h = special.cylindrical_histogram([[1, 2, 3], [2, 3, 4]])
        assert special.CylinderSurfaceHistogram == type(h.projection("phi", "z"))
        assert special.CylinderSurfaceHistogram == type(h.projection("z", "phi"))
        assert special.PolarHistogram == type(h.projection("rho", "phi"))
        assert special.PolarHistogram == type(h.projection("phi", "rho"))

if __name__ == "__main__":
    pytest.main(__file__)

ext: py

max_stars_repo_name: ryanmackenziewhite/physt

max_issues_repo_name: ryanmackenziewhite/physt

max_forks_repo_name: ryanmackenziewhite/physt

reason: import numpy


text:
"""
Creates test fixtures to test neural network and utility functions
"""
import pytest
import numpy as np
import pandas as pd
from kerasbeats import NBeatsModel

# single univariate time series values
@pytest.fixture(scope='function')
def numeric_data():
    return np.arange(20)

# nested time series
@pytest.fixture(scope='function')
def nested_time_series():
    df = pd.DataFrame()
    df['label'] = ['a'] * 10 + ['b'] * 10
    df['value'] = [i for i in range(10)] * 2
    return df

# generic model
@pytest.fixture(scope='session')
def generic_model():
    model = NBeatsModel(model_type = 'generic',
                        batch_size = 5)
    return model

# interpretable model
@pytest.fixture(scope='session')
def interpretable_model():
    model = NBeatsModel(model_type = 'interpretable',
                        batch_size = 5,
                        horizon = 2)
    return model

max_stars_repo_name: JonathanBechtel/KerasBeats

max_issues_repo_name: Spurri/KerasBeats

max_forks_repo_name: Spurri/KerasBeats


text:
from torch.autograd import Variable
import warnings
warnings.filterwarnings("ignore")
import torch
import torch.optim as optim
from datetime import datetime, timedelta

from torch.utils.data import DataLoader
import torch.nn as nn
from data.dataset import NCPDataset,NCP2DDataset,NCPJPGDataset,NCPJPGDataset_new,NCPJPGtestDataset_new
import os,cv2
import numpy as np

def _validate(modelOutput, length, labels, total=None, wrong=None):

    averageEnergies = torch.mean(modelOutput.data, 1)
    for i in range(modelOutput.size(0)):
        #print(modelOutput[i,:length[i]].sum(0).shape)
        averageEnergies[i] = modelOutput[i,:length[i]].mean(0)

    maxvalues, maxindices = torch.max(averageEnergies, 1)
    #print(maxindices.cpu().numpy())
    #print(labels.cpu().numpy())
    count = 0

    for i in range(0, labels.squeeze(1).size(0)):
        l = int(labels.squeeze(1)[i].cpu())
        if total is not None:
            if l not in total:
                total[l] = 1
            else:
                total[l] += 1
        if maxindices[i] == labels.squeeze(1)[i]:
            count += 1
        else:
            if wrong is not None:
               if l not in wrong:
                   wrong[l] = 1
               else:
                   wrong[l] += 1

    return (averageEnergies, count)

class Validator():
    def __init__(self, options, mode,model,savenpy):
        self.R='R' in options['general'].keys()
        self.model=model
        self.cls_num=options['general']['class_num']
        self.use_plus = options['general']['use_plus']
        self.use_3d=options['general']['use_3d']
        self.usecudnn = options["general"]["usecudnn"]
        self.use_lstm = options["general"]["use_lstm"]
        self.batchsize = options["input"]["batchsize"]
        self.use_slice=options['general']['use_slice']
        self.asinput = options['general']['plus_as_input']
        self.USE_25D = options['general']['use25d']
        if options['general']['use_slice']:
            if self.USE_25D:
                f = 'data/3cls_test.list'
                self.validationdataset = NCPJPGtestDataset_new(options[mode]["data_root"],options["training"]["padding"],
                                                             f, cls_num=self.cls_num, mod=options['general']['mod'],
                                                               options=options)
            else:
                self.validationdataset = NCPJPGDataset_new(options[mode]["data_root"],
                                                        options[mode]["index_root"],
                                                        options[mode]["padding"],
                                                        False,cls_num=self.cls_num,
                                                        mod=options['general']['mod'],
                                                           options=options)
        else:
            if options['general']['use_3d']:
                self.validationdataset = NCPDataset(  options[mode]["index_root"],
                                                      options[mode]["padding"],
                                                      False,
                                                    z_length=options["model"]["z_length"])
            else:
                self.validationdataset = NCP2DDataset(options[mode]["data_root"],
                                                        options[mode]["index_root"],
                                                        options[mode]["padding"],
                                                        False)
        self.savingnpy=savenpy

        self.tot_data = len(self.validationdataset)
        self.validationdataloader = DataLoader(
                                    self.validationdataset,
                                    batch_size=options["input"]["batchsize"],
                                    shuffle=True,
                                    num_workers=options["input"]["numworkers"],
                                    drop_last=False
                                )
        self.mode = mode
        self.epoch=0

    def __call__(self):
        self.epoch+=1
        with torch.no_grad():
            print("Starting {}...".format(self.mode))
            count = np.zeros((self.cls_num+self.use_plus*2*(1-self.asinput)))
            Matrix=np.zeros((self.cls_num,self.cls_num))
            if self.use_3d:
                validator_function = self.model.validator_function()
            if self.use_lstm:
                validator_function = _validate
                self.model.eval()
            LL=[]
            GG=[]
            AA=[]
            if(self.usecudnn):
                net = nn.DataParallel(self.model).cuda()
            error_dir='error/'
            os.makedirs(error_dir,exist_ok=True)
            cnt=0
            num_samples = np.zeros((self.cls_num+self.use_plus*2*(1-self.asinput)))
            for i_batch, sample_batched in enumerate(self.validationdataloader):
                input = Variable(sample_batched['temporalvolume']).cuda()
                labels = Variable(sample_batched['label']).cuda()
                #length = len(sample_batched['length'][1])
                names=sample_batched['name']
                outputs,f = net(input)

                output_numpy = np.exp(outputs.cpu().numpy())
                output_numpy=output_numpy[:,[0,-1]]
                #output_numpy_ab=np.sum(output_numpy[:,1:],1)
                #output_numpy=np.stack([output_numpy[:,0],output_numpy_ab],-1)
                output_numpy=output_numpy/output_numpy.sum(1,keepdims=True)
                pre=np.array(output_numpy[:,-1]>0.5,np.int)
                isacc=labels.cpu().numpy().reshape(labels.size(0))==pre
                label_numpy=labels.cpu().numpy()[:,0]

               # argmax = (-vector.cpu().numpy()).argsort()
                for i in range(labels.size(0)):
                    LL.append([names[i], output_numpy[i,-1], label_numpy[i]])
                    Matrix[label_numpy[i],pre[i]]+=1
                    if isacc[i]==1 :
                        count[labels[i]] += 1
                    num_samples[labels[i]]+=1
                    if self.mode=='validation':
                        if labels[i] == 1 and output_numpy[i,-1]>0.99:
                            I = np.array(input[i, :, :, :].cpu().numpy() * 255, np.uint8).transpose(1, 2, 0) [:,:, [2, 1, 0]]
                            cv2.imwrite('/mnt/data9/covid_detector_jpgs/selected_train1/abnor/abnor_' +
                                        names[i].split('/')[-1].split('.')[0]+'.jpg',I)
                        if labels[i] == 0 and output_numpy[i,0]>0.99:
                            I = np.array(input[i, :, :, :].cpu().numpy() * 255, np.uint8).transpose(1, 2, 0) [:,:, [2, 1, 0]]
                            cv2.imwrite('/mnt/data9/covid_detector_jpgs/selected_train1/nor/nor_' +
                                        names[i].split('/')[-1].split('.')[0]+'.jpg',I)

        print(count[:self.cls_num].sum()/num_samples[:self.cls_num].sum(),np.mean(AA))
        LL=np.array(LL)
        np.save(self.savingnpy, LL)

        print(Matrix)
        return count/num_samples,count[:self.cls_num].sum()/num_samples[:self.cls_num].sum()


text:
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
'''
Created on Dec 18, 2018
Tensorflow Implementation of Knowledge Graph Attention Network (KGAT) model in:
Wang Xiang et al. KGAT: Knowledge Graph Attention Network for Recommendation. In KDD 2019.
@author: Xiang Wang (xiangwang@u.nus.edu)
'''
import collections
import numpy as np
import random as rd

class Data(object):
    def __init__(self, args, path):
        self.path = path
        self.args = args

        self.batch_size = args.batch_size

        train_file = path + '/train.txt'
        test_file = path + '/test.txt'

        kg_file = path + '/kg_final.txt'

        # ----------get number of users and items & then load rating data from train_file & test_file------------.
        self.n_train, self.n_test = 0, 0
        self.n_users, self.n_items = 0, 0

        self.train_data, self.train_user_dict = self._load_ratings(train_file)
        self.test_data, self.test_user_dict = self._load_ratings(test_file)
        self.exist_users = self.train_user_dict.keys()

        self._statistic_ratings()

        # ----------get number of entities and relations & then load kg data from kg_file ------------.
        self.n_relations, self.n_entities, self.n_triples = 0, 0, 0
        self.kg_data, self.kg_dict, self.relation_dict = self._load_kg(kg_file)

        # ----------print the basic info about the dataset-------------.
        self.batch_size_kg = self.n_triples // (self.n_train // self.batch_size)
        self._print_data_info()

    # reading train & test interaction data.
    def _load_ratings(self, file_name):
        user_dict = dict()
        inter_mat = list()

        lines = open(file_name, 'r').readlines()
        for l in lines:
            tmps = l.strip()
            inters = [int(i) for i in tmps.split(' ')]

            u_id, pos_ids = inters[0], inters[1:]
            pos_ids = list(set(pos_ids))

            for i_id in pos_ids:
                inter_mat.append([u_id, i_id])

            if len(pos_ids) > 0:
                user_dict[u_id] = pos_ids
        return np.array(inter_mat), user_dict

    def _statistic_ratings(self):
        self.n_users = max(max(self.train_data[:, 0]), max(self.test_data[:, 0])) + 1
        self.n_items = max(max(self.train_data[:, 1]), max(self.test_data[:, 1])) + 1
        self.n_train = len(self.train_data)
        self.n_test = len(self.test_data)

    # reading train & test interaction data.
    def _load_kg(self, file_name):
        def _construct_kg(kg_np):
            kg = collections.defaultdict(list)
            rd = collections.defaultdict(list)

            for head, relation, tail in kg_np:
                kg[head].append((tail, relation))
                rd[relation].append((head, tail))
            return kg, rd

        kg_np = np.loadtxt(file_name, dtype=np.int32)
        kg_np = np.unique(kg_np, axis=0)

        # self.n_relations = len(set(kg_np[:, 1]))
        # self.n_entities = len(set(kg_np[:, 0]) | set(kg_np[:, 2]))
        self.n_relations = max(kg_np[:, 1]) + 1
        self.n_entities = max(max(kg_np[:, 0]), max(kg_np[:, 2])) + 1
        self.n_triples = len(kg_np)

        kg_dict, relation_dict = _construct_kg(kg_np)

        return kg_np, kg_dict, relation_dict

    def _print_data_info(self):
        print('[n_users, n_items]=[%d, %d]' % (self.n_users, self.n_items))
        print('[n_train, n_test]=[%d, %d]' % (self.n_train, self.n_test))
        print('[n_entities, n_relations, n_triples]=[%d, %d, %d]' % (self.n_entities, self.n_relations, self.n_triples))
        print('[batch_size, batch_size_kg]=[%d, %d]' % (self.batch_size, self.batch_size_kg))

    def _generate_train_cf_batch(self):
        if self.batch_size <= self.n_users:
            users = rd.sample(self.exist_users, self.batch_size)
        else:
            users = [rd.choice(self.exist_users) for _ in range(self.batch_size)]

        def sample_pos_items_for_u(u, num):
            pos_items = self.train_user_dict[u]
            n_pos_items = len(pos_items)
            pos_batch = []
            while True:
                if len(pos_batch) == num: break
                pos_id = np.random.randint(low=0, high=n_pos_items, size=1)[0]
                pos_i_id = pos_items[pos_id]

                if pos_i_id not in pos_batch:
                    pos_batch.append(pos_i_id)
            return pos_batch

        def sample_neg_items_for_u(u, num):
            neg_items = []
            while True:
                if len(neg_items) == num: break
                neg_i_id = np.random.randint(low=0, high=self.n_items,size=1)[0]

                if neg_i_id not in self.train_user_dict[u] and neg_i_id not in neg_items:
                    neg_items.append(neg_i_id)
            return neg_items

        pos_items, neg_items = [], []
        for u in users:
            pos_items += sample_pos_items_for_u(u, 1)
            neg_items += sample_neg_items_for_u(u, 1)

        return users, pos_items, neg_items

    def get_sparsity_split(self):
        try:
            split_uids, split_state = [], []
            lines = open(self.path + '/sparsity.split', 'r').readlines()

            for idx, line in enumerate(lines):
                if idx % 2 == 0:
                    split_state.append(line.strip())
                    print(line.strip())
                else:
                    split_uids.append([int(uid) for uid in line.strip().split(' ')])
            print('get sparsity split.')

        except Exception:
            split_uids, split_state = self.create_sparsity_split()
            f = open(self.path + '/sparsity.split', 'w')
            for idx in range(len(split_state)):
                f.write(split_state[idx] + '\n')
                f.write(' '.join([str(uid) for uid in split_uids[idx]]) + '\n')
            print('create sparsity split.')

        return split_uids, split_state

    def create_sparsity_split(self):
        all_users_to_test = list(self.test_user_dict.keys())
        user_n_iid = dict()

        # generate a dictionary to store (key=n_iids, value=a list of uid).
        for uid in all_users_to_test:
            train_iids = self.train_user_dict[uid]
            test_iids = self.test_user_dict[uid]

            n_iids = len(train_iids) + len(test_iids)

            if n_iids not in user_n_iid.keys():
                user_n_iid[n_iids] = [uid]
            else:
                user_n_iid[n_iids].append(uid)
        split_uids = list()

        # split the whole user set into four subset.
        temp = []
        count = 1
        fold = 4
        n_count = (self.n_train + self.n_test)
        n_rates = 0

        split_state = []
        for idx, n_iids in enumerate(sorted(user_n_iid)):
            temp += user_n_iid[n_iids]
            n_rates += n_iids * len(user_n_iid[n_iids])
            n_count -= n_iids * len(user_n_iid[n_iids])

            if n_rates >= count * 0.25 * (self.n_train + self.n_test):
                split_uids.append(temp)

                state = '#inter per user<=[%d], #users=[%d], #all rates=[%d]' %(n_iids, len(temp), n_rates)
                split_state.append(state)
                print(state)

                temp = []
                n_rates = 0
                fold -= 1

            if idx == len(user_n_iid.keys()) - 1 or n_count == 0:
                split_uids.append(temp)

                state = '#inter per user<=[%d], #users=[%d], #all rates=[%d]' % (n_iids, len(temp), n_rates)
                split_state.append(state)
                print(state)

        return split_uids, split_state

max_stars_repo_name: Ascend/modelzoo

max_issues_repo_name: Ascend/modelzoo

max_forks_repo_name: Ascend/modelzoo


text:
# Implement the Call Center problem
import gurobipy as gp
import matplotlib.pyplot as plt
import numpy as np
from gurobipy import *

id = 903515184

def solve_lp(id):

    gm = gp.Model("Call_Center")

    c, D = get_data(id, 3, 7)

    #print(c,D)

    D_shifts = {
        'Mon': [1, 1, 1, 1, 1, 0, 0],
        'Tue': [0, 1, 1, 1, 1, 1, 0],
        'Wed': [0, 0, 1, 1, 1, 1, 1],
        'Thu': [1, 0, 0, 1, 1, 1, 1],
        'Fri': [1, 1, 0, 0, 1, 1, 1],
        'Sat': [1, 1, 1, 0, 0, 1, 1],
        'Sun': [1, 1, 1, 1, 0, 0, 1],
    }

    work_start, pay = gp.multidict({
        'Mon': c[0],
        'Tue': c[1],
        'Wed': c[2],
        'Thu': c[2],
        'Fri': c[2],
        'Sat': c[2],
        'Sun': c[1],
    })

    arrange = gm.addVars(work_start, vtype= GRB.INTEGER, name="arrangement")
    gm.setObjective(arrange.prod(pay), GRB.MINIMIZE)
    gm.addConstrs((gp.quicksum(D_shifts[work_start_day][day_num] * arrange[work_start_day] for work_start_day in work_start) >= day_need for day_num, day_need in enumerate(D)))

    #print([gp.quicksum(D_shifts[work_start_day][day_num] for work_start_day in work_start) >= day_need for day_num, day_need in enumerate(D)])
    #print("---")

    # Solve the model
    gm.update()
    gm.optimize()

    if gm.status == GRB.OPTIMAL:
        print(gm.objVal)
        arrangex = gm.getAttr('x', arrange)
        for f in work_start:
            if arrange[f].x > 0.0001:
                print('%s %g' % (f, arrangex[f]))

    gm.write('call_center.lp')
    return(gm)

# Generate the data (DO NOT MODIFY)
def get_data(id, i_max, t_max):
    np.random.seed(id)
    c = np.sort(10 * np.random.random(i_max) + 10)
    D = np.random.randint(10, 30, t_max)
    return(c, D)

solve_lp(id=id)


text:
""" Here we implement liftoff-status
"""

from argparse import Namespace
from collections import OrderedDict
import datetime
import os.path
import sys
import time
from typing import List
from tabulate import tabulate
from termcolor import colored as clr
import numpy as np
from .common.experiment_info import get_experiment_paths
from .common.options_parser import OptionParser

def parse_options() -> Namespace:
    """ Parse command line arguments and liftoff configuration.
    """

    opt_parser = OptionParser(
        "liftoff-status", ["experiment", "all", "timestamp_fmt", "results_path"]
    )
    return opt_parser.parse_args()

def experiment_status(experiment_path):
    """ Gets full info about about an experiment.
    """
    ntotal, nstarted, nended, ncrashed, nlocked, nlost = 0, 0, 0, 0, 0, 0
    durations = []
    live_durations = []

    time0 = time.time()
    time_now = time.time()  # Time now

    with os.scandir(experiment_path) as fit:
        for entry in fit:
            if entry.name.startswith(".") or not entry.is_dir():
                continue
            with os.scandir(entry.path) as fit2:
                for entry2 in fit2:
                    if entry2.name.startswith(".") or not entry2.is_dir():
                        continue
                    leaf_path = os.path.join(entry2.path, ".__leaf")
                    if not os.path.isfile(leaf_path):
                        continue

                    ntotal += 1

                    start_path = os.path.join(entry2.path, ".__start")
                    lock_path = os.path.join(entry2.path, ".__lock")
                    if os.path.isfile(start_path):
                        nstarted += 1
                        with open(start_path) as start_file:
                            try:
                                start_time = int(start_file.readline().strip())
                                time0 = min(start_time, time0)
                            except ValueError as _ex:
                                sys.stderr.write(
                                    f"Can't read timestamp in {start_path}.\n"
                                )

                            end_path = os.path.join(entry2.path, ".__end")
                            crash_path = os.path.join(entry2.path, ".__crash")

                            if os.path.isfile(end_path):
                                with open(end_path) as end_file:
                                    try:
                                        end_time = int(end_file.readline().strip())
                                        durations.append(end_time - start_time)
                                    except ValueError as _ex:
                                        sys.stderr.write(
                                            f"Can't read timestamp in {end_path}.\n"
                                        )
                                nended += 1
                            elif os.path.isfile(crash_path):
                                with open(crash_path) as end_file:
                                    try:
                                        end_time = int(end_file.readline().strip())
                                        durations.append(end_time - start_time)
                                    except ValueError as _ex:
                                        sys.stderr.write(
                                            f"Can't read timestamp in {crash_path}.\n"
                                        )
                                ncrashed += 1
                            elif os.path.isfile(lock_path):
                                nlocked += 1
                                live_durations.append(time_now - start_time)
                            else:
                                nlost += 1
                    elif os.path.isfile(lock_path):
                        nlocked += 1

    durations, live_durations = np.array(durations), np.array(live_durations)

    if durations.size > 0:
        avg_time = np.mean(durations)  # Average time based on finished runs
        # Here we limit the locked processes to maximum two standard deviations above
        # the mean known duration.
        # Without this a process locked and abandoned might add too much progress.
        # It's better to underestimate progress than to overestimate it. :)
        mean, std = np.mean(durations), np.std(durations)
        live_durations = live_durations.clip(0, mean + 2 * std)
    elif live_durations.size > 0:
        avg_time = np.mean(live_durations) * 2
    else:
        avg_time = 0

    if avg_time > 0:
        live_time_left = np.sum(avg_time - live_durations)
        nleft = ntotal - nended - ncrashed - len(live_durations)
        is_over = (nleft > 0) or (live_durations.size > 0)

        time_left = max(avg_time * nleft + live_time_left, 0 if is_over else 1)

        elapsed_time = np.sum(durations) + np.sum(live_durations)
        speedup = elapsed_time / (time_now - time0)

        left_wall_time = datetime.timedelta(seconds=int(time_left / speedup))

        progress = min(100.0 * elapsed_time / (elapsed_time + time_left), 100.0)

    else:
        progress = 0
        left_wall_time = datetime.timedelta(days=100)

    info = OrderedDict({})
    info["Experiment"] = os.path.basename(experiment_path)
    info["Total"] = f"{ntotal:d}"
    info["Locked"] = f"{nlocked:d}"
    info["Done"] = clr(f"{nended:d}", "green")
    info["Dead"] = clr(f"{ncrashed:d}", "red")
    if nlost > 0:
        info["Lost"] = clr(f"{nlost:d}", "white", "on_magenta", attrs=["bold"])
    info["Progress"] = clr(f"{progress:.3f}%", attrs=["bold"])
    info["ETL"] = str(left_wall_time)

    return info

def display_experiments(experiments_info: List[dict]):
    """ Here we nicely display the experiments.
    """
    print(tabulate(experiments_info, headers="keys"))

def status() -> None:
    """ Entry point for liftoff-status.
    """
    opts = parse_options()
    experiment_paths = get_experiment_paths(  # pylint: disable=bad-continuation
        opts.experiment, opts.results_path, opts.timestamp_fmt, latest=(not opts.all),
    )
    display_experiments(
        sorted(
            [experiment_status(p) for p in experiment_paths],
            key=lambda info: info["Experiment"],
        )
    )

max_stars_repo_name: tudor-berariu/liftoff

max_issues_repo_name: tudor-berariu/liftoff

max_forks_repo_name: tudor-berariu/liftoff


text:
from __future__ import print_function
from uritools import urisplit, uriunsplit, urijoin
from ndk.features import butter_low, butter_high, butter_bandpass
from scipy.signal import convolve, butter, lfilter, filtfilt
import numpy as np
import neo.io
import os
from .dsfilter import *

def spike2_to_floats(filename):
    """Convert the time series in a segment of a Spike2 file into an array of floats.  Also returns the rate, start time, and end time for the data."""
    print('Converting {}'.format(filename))
    r = neo.io.Spike2IO(filename=filename)
    # print('neo.io.Spike2IO returns {}'.format(r))
    hdr = r.header
    sig_channels = hdr['signal_channels']
    print(sig_channels)
    seg = r.read_segment()
    signals = seg.analogsignals
    print("signals[0] shape: {}".format(signals[0].shape))
    nchannels = len(signals)
    print('nchannels = {}'.format(nchannels))
    samprate = float(signals[0].sampling_rate)
    # len(signals) is nchannels.  len(signals[0]) is the number of
    # samples:
    print('spike2 data interval: [ {}, {} ]'.format(signals[0].t_start, signals[0].t_stop))
    t0 = int(signals[0].t_start * samprate)
    t1 = int(signals[0].t_stop * samprate)
    print('spike2 sample num interval: [ {}, {} ]'.format(t0, t1))
    print('spike2 signal array length: {}'.format(len(signals[0])))
    # Here, signals[i] represents an entire time series for channel i:
    return signals, samprate, t0, t1, seg

class spike2():
    def __init__(self, uri):
        if isinstance(uri, str):
            self.desc = urisplit(uri)
        else:
            self.desc = uri
            # self.filename = filename
        self.filename = self.desc.path
        print('# Filename: {}'.format(self.filename))
        sig, samprate, start, end, seg = spike2_to_floats(self.filename)
        self.seg = seg
        self.signal = sig
        self.samprate = samprate
        self.t0 = start
        self.t1 = end
        print('# Dataset interval: [{}, {}]'.format(self.t0, self.t1))
        self.dsf = dsfilter(samprate)

    def filter_data(self, low_cutoff, high_cutoff, order=5):
        self.dsf.filter_data(low_cutoff, high_cutoff, order)

    def get_signal(self, channel):
        return self.signal[channel]

    def get_chunk(self, channel, start, end):
        """Returns the portion of the time series for the specified channel that is between start and end inclusive (time)."""
        x = self.signal[channel]
        i0 = max(0, int(start - self.t0))
        #i1 = max(0, int(end+1 - self.t0))
        i1 = max(0, int(end - self.t0))
        # We should probably call numpy.asarray to coerce this to a pure array:
        r = np.asarray(x[i0:i1])
        result = self.dsf.maybe_filter(r)
        result = result.reshape(len(r))
        return result

    def fill_event_store(self, es, sigma=0.32, window=2.0, nfuncs=5):
        """Given an event store object 'es', populate it from the information
contained in this pre_ds data store object.  Only handles one tetrode
at a time."""
        samprate = self.samprate
        nchannels = len(self.signal)
        samp_sigma = sigma * 0.001 * samprate
        samp_window = window * 0.001 * samprate
        es.make_meta_table(samprate, nchannels, uriunsplit(self.desc))
        # See if the default sigma (0.32 ms) and window width (2ms)
        # will work here:

        es.make_basis_table()
        es.make_event_table()
        es.make_spiketimes_table()
        es.make_spikebasis_table()
        # es.make_channel_table()
        es.make_spikecoefs_table()
        es.make_spikelabels_table()
        es.make_default_labels()

        es.add_basis(0, samp_sigma, samp_window)

        seq = es.get_basis(0)

        zc = find_feature_peaks(self.signal[0], seq)
        last = len(zc)

        i = 0
        iprev = 0
        spike_id = -1
        prev_id = 0
        while i < last:
            spikep = (zc[i] != 0)
            if spikep:
                ts = t0 + i*dt
                if ( (i - iprev) > width ):
                    spike_id += 1
                    es.set_spike_time( spike_id, ts )
                    es.set_spike_basisid( spike_id, 0 )
                    es.set_spike_label(spike_id, 0)
                    for chan in range(nchannels):
                        x = ndk.features.get_window(signal[chan], ts, winsize)
                        coefs = ndk.features.waveform_coefs(w, seq)
                        es.set_spike_coefs(spike_id, chan, coefs)
                    iprev = i
            i = i + 1
            if spike_id - prev_id >= 100:
                if spikeid-previd >= 100:
                    print( '{} '.format(spikeid), end="")
                    prev_id = spike_id

                    for j in range(4):
                        chan = j
                        signal = w[j]
                        coefs = ndk.features.waveform_coefs(signal, seq)
                        es.set_spike_coefs(spike_id, chan, coefs)
                    spike_id += 1
        for ts, tag in self.events:
            es.add_event(tag, ts)

def neuralynx_to_floats(filename):
    """Convert the time series in a segment of a Neuralynx file into an array of floats.  Also returns the rate, start time, and end time for the data."""
    dir = os.path.dirname(filename)
    # r = neo.io.NeuralynxIO(dir, use_cache='never')
    r = neo.io.NeuralynxIO(dir, use_cache=True, cache_path='home')
    seg = r.read_segment()
    signals = seg.analogsignals  # nchan x nsamp - opposite of spike2.
    sigout = signals[0].transpose()
    nchannels = sigout.shape[0]
    print('Number of channels: {} ; Sample rate: {}'.format(nchannels, signals[0].sampling_rate))
    samprate = float(signals[0].sampling_rate.simplified)
    # len(signals) is nchannels.  len(signals[0]) is the number of
    # samples:
    print('neuralynx data interval: [ {}, {} ]'.format(signals[0].t_start, signals[0].t_stop))
    t0 = int(signals[0].t_start.simplified * samprate)
    t1 = int(signals[0].t_stop.simplified * samprate)
    print('neuralynx sample num interval: [ {}, {} ]'.format(t0, t1))
    print('neuralynx signal array length: {}'.format(len(sigout[0])))
    # Not good enough - misses start / end recording events:
    # events = r.get_event_timestamps()
    events = []
    for x in seg.events:
        times = x.times
        labels = x.labels
        for i in range(len(times)):
            ts = times[i]
            label = labels[i]
            events.append((int(float(ts)*samprate), label))
    print(events)
    return sigout, samprate, t0, t1, seg, events

def plexon_to_floats(filename):
    """Convert the time series in a segment of a Plexon file into an array of floats.  Also returns the rate, start time, and end time for the data."""
    dir = os.path.dirname(filename)
    print("Calling PlexonIO('{}')".format(filename))
    r = neo.io.PlexonIO(filename)
    print(r)
    seg = r.read_segment()
    signals = seg.analogsignals  # nchan x nsamp - opposite of spike2.

    # this is one difference between neo neuralynx and neo plexon:
    nchannels = len(signals)
    nsamples = signals[0].shape[0]
    samprate = float(signals[0].sampling_rate.simplified)
    print('plexon_to_floats - Number of channels: {} ; Sample rate: {}; nsamples: {}'.format(nchannels, samprate, nsamples))

    out = np.zeros((nchannels, nsamples))
    for k in range(nchannels):
        out[k,:] = signals[k].transpose()

    # Plexon uses sample numbers!!!!!
    print('plexon data interval: [ {}, {} ]'.format(signals[0].t_start, signals[0].t_stop))
    # t0 = int(signals[0].t_start.simplified * samprate)
    # t1 = int(signals[0].t_stop.simplified * samprate)
    t0 = int(signals[0].t_start.simplified)
    t1 = int(signals[0].t_stop.simplified)
    print('plexon sample num interval: [ {}, {} ]'.format(t0, t1))

    # Not good enough - misses start / end recording events:
    # events = r.get_event_timestamps()
    events = []
    for x in seg.events:
        times = x.times
        labels = x.labels
        for i in range(len(times)):
            ts = times[i]
            label = labels[i]
            events.append((int(float(ts)*samprate), label))
    print(events)
    return out, samprate, t0, t1, seg, events

max_stars_repo_name: SRI-CSL/ndk

max_issues_repo_name: Tubbz-alt/ndk

max_forks_repo_name: Tubbz-alt/ndk

reason: import numpy,from scipy


text:
"""Laplace distribution
"""
import numpy as np
from scipy.stats import cauchy, laplace

from xgboost_distribution.distributions.base import BaseDistribution

class Laplace(BaseDistribution):
    """Laplace distribution with log scoring

    Definition:

        f(x) = 1/2 * exp( -| (x - loc) / scale | ) / scale

    We reparameterize:
        loc   -> loc = a
        scale -> log(scale) = b to ensure scale >= 0.

    Thus:

        f(x) = 1/2 * exp( -| (x - a) / e^b | ) / e^b

    We compute the gradients:

        d/da -log[f(x)] = (a - x) / (scale * | a-x | )
        d/db -log[f(x)] = 1 - | a-x | / scale

    To second order:

        d2/da2 -log[f(x)] = 2 δ(x-a) / scale
        d2/db2 -log[f(x)] = | a-x | / scale

    The Fisher information is:

        I(loc) = 1 / scale^2
        I(scale) = 1 / scale^2

    which needs to be expressed in reparameterized form:

        1 / scale^2 = I_r(loc) (d/d(loc) loc) ^2
                    = I_r(loc)

        1 / scale^2 = I_r(scale) ( d/d(scale) log(scale) )^2
                    = I_r(scale) ( 1/ scale )^2

    Hence:

        I_r(loc) = 1 / scale^2
        I_r(scale) = 1

    """

    @property
    def params(self):
        return ("loc", "scale")

    def gradient_and_hessian(self, y, params, natural_gradient=True):
        """Gradient and diagonal hessian"""

        loc, log_scale = self._split_params(params)
        scale = np.exp(log_scale)

        grad = np.zeros(shape=(len(y), 2))
        grad[:, 0] = np.sign(loc - y) / scale
        grad[:, 1] = 1 - np.abs(loc - y) / scale

        if natural_gradient:
            fisher_matrix = np.zeros(shape=(len(y), 2, 2))
            fisher_matrix[:, 0, 0] = 1 / scale ** 2
            fisher_matrix[:, 1, 1] = 1

            grad = np.linalg.solve(fisher_matrix, grad)
            hess = np.ones(shape=(len(y), 2))  # we set the hessian constant
        else:
            hess = np.zeros(shape=(len(y), 2))  # diagonal elements only
            # Note: Delta functions won't work well, hence we approximate with cauchy
            hess[:, 0] = 2 * cauchy.pdf(y, loc, scale) / scale
            hess[:, 1] = 1 - grad[:, 1]

        return grad, hess

    def loss(self, y, params):
        loc, scale = self.predict(params)
        return "LaplaceError", -laplace.logpdf(y, loc=loc, scale=scale).mean()

    def predict(self, params):
        loc, log_scale = self._split_params(params)
        scale = np.exp(log_scale)
        return self.Predictions(loc=loc, scale=scale)

    def starting_params(self, y):
        return np.mean(y), np.log(np.std(y))

    def _split_params(self, params):
        """Return loc and log_scale from params"""
        return params[:, 0], params[:, 1]

max_stars_repo_name: thomasaarholt/xgboost-distribution

max_issues_repo_name: thomasaarholt/xgboost-distribution

max_forks_repo_name: thomasaarholt/xgboost-distribution


text:
"""
    Most of the logic/ heavy-lifting is based off of:
    github.com/facebookresearch/detectron2/blob/master/detectron2/evaluation/coco_evaluation.py#L225
"""

################################################################################
##  Import packages.                                                          ##
################################################################################
from lvis import LVIS, LVISResults, LVISEval
from operator import itemgetter
from tabulate import tabulate
import numpy as np
import itertools
import logging
import pickle
import json
import math
import os

from config import fetch_config, print_args_stdout
from utils import fetch_aps, evaluate_map, print_aps

import ipdb

################################################################################
##  Run stuff.                                                                ##
################################################################################
def main():

    config = fetch_config()
    print_args_stdout(config)
    ipdb.set_trace()

    print("Running eval.")
    lvis_eval = LVISEval(config.ann_path, config.results_path, config.ann_type)
    lvis_eval.run()
    lvis_eval.print_results()
    print("Finished eval.")

    ipdb.set_trace()
    # All precision values: 10 x 101 x 1230 x 4
    # precision has dims (iou, recall, cls, area range)
    precisions = lvis_eval.eval['precision']

    with open(config.ann_path, 'r') as outfile:
        gt = json.load(outfile)
    cat_metas = gt['categories']
    cats = []
    for cat_meta in cat_metas:
        cats.append((cat_meta['id'], cat_meta['name']))
    cats.sort(key=itemgetter(0))
    class_names = [cat[1] for cat in cats]

    area_type = 0
    results_per_category, per_cat_results = fetch_aps(precisions, class_names, area_type)
    print("mAP for area type {}: {}".format(area_type, evaluate_map(results_per_category)))

    # Print for eye-balling.
    # print_aps(results_per_category, class_names, n_cols=6)

    # Store results_per_category into a JSON.
    with open(config.aps_json_path, 'w') as json_file:
        json.dump(per_cat_results, json_file, indent=4)

    # Store the 4D precisions tensor as a PKL.
    with open(config.prec_pkl_path, 'wb') as pkl_file:
        pickle.dump(precisions, pkl_file)

################################################################################
##  Execute main.                                                             ##
################################################################################
if __name__ == '__main__':

    main()


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

__author__ = "Rafael Mosca"
__email__ = "rafael.mosca@mail.polimi.it"
__copyright__ = "Copyright 2020 - Rafael Mosca"
__license__ = "MIT"
__version__ = "1.0"

# ==============================================================================

import numpy as np

from aertb.core.loaders.interface import LoaderInterface
from aertb.core.types import event_dtype
from aertb.core.const import HEX

# ==============================================================================

class BinLoader(LoaderInterface):

    # ------------------------------------------------------------------------
    # Singleton
    # ------------------------------------------------------------------------
    instance = None
    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super().__new__(cls, *args, **kwargs)
        return cls.instance

    # override
    # ------------------------------------------------------------------------
    def load_events(self, filename, polarities, to_secs):
        """
            Reads a binary file containing events. To use with
            the N-MNIST or N-CALTECH101 dataset

            Each event occupies 40 bits as described below:
                bit 39 - 32: Xaddress (in pixels)
                bit 31 - 24: Yaddress (in pixels)
                bit 23: Polarity (0 for OFF, 1 for ON)
                bit 22 - 0: Timestamp (in microseconds)

        """

        # 5 Named bytes
        orchard_event_dtype = [('x', np.uint8), ('y', np.uint8), ('tp1', np.uint8),
                               ('tp2', np.uint8), ('tp3', np.uint8)]

        # Open file in binary mode
        fp = open(filename, "rb")

        orchard_events = np.fromfile(fp, dtype=orchard_event_dtype, count=-1)

        x = orchard_events['x']
        y = orchard_events['y']

        p = np.right_shift(np.bitwise_and(orchard_events['tp1'].astype(np.uint32),
            int('80', HEX)), 7)
        ts1 = np.left_shift(np.bitwise_and(orchard_events['tp1'].astype(np.uint32),
            int('7F', HEX)), 16)
        ts2 = np.left_shift(np.bitwise_and(orchard_events['tp2'].astype(np.uint32),
            int('FF', HEX)), 8)
        ts3 = np.left_shift(orchard_events['tp3'].astype(np.uint32), 0)

        ts = np.bitwise_or(np.bitwise_or(ts1, ts2), ts3)

        # Transform from millis to secs
        if to_secs:
            ts = ts / 1e6

        # transform to -1,1
        if polarities[0] == -1:
            p = -1 + 2 * p

        recarray = np.rec.fromarrays([x, y, ts, p], dtype=event_dtype)

        return recarray

    # override
    # ------------------------------------------------------------------------
    def get_header(self, filename):
        print('No header defined')


text:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import qpoint as qp
import healpy as hp
import quaternion as qt
import time

Q = qp.QMap(fast_pix=True)
nside = 64
q_off = Q.det_offset(0, 0, 0)
print q_off
ctime = time.time()
roll = 90

az_off = 40
el_off = 20
polang = 40

q_bore = Q.azel2bore(0, 0, None, None, 0, -89.9, ctime)
pix, sin2psi, cos2psi = Q.bore2pix(q_off, np.array([ctime]), q_bore, nside=nside, pol=True)

q_off = Q.det_offset(az_off, el_off, polang)
print 'det offset'
print q_off
print Q.quat2radecpa(q_off)
q = qt.as_quat_array(q_off)
q2 = qt.as_quat_array(Q.det_offset(0, 0, roll))

q3 = q2 * q# * np.conj(q2)
q_off = qt.as_float_array(q3)
q_off /= np.sqrt(np.sum(q_off**2))
print 'det offset rotated'
print q_off
print Q.quat2radecpa(q_off)
pix2, _, _ = Q.bore2pix(q_off, np.array([ctime]), q_bore, nside=nside, pol=True)

az_off2 = np.cos(np.radians(roll)) * az_off - np.sin(np.radians(roll)) * el_off
el_off2 = np.sin(np.radians(roll)) * az_off + np.cos(np.radians(roll)) * el_off
q_off2 = Q.det_offset(az_off2, el_off2, 0)
print 'det offset rotated flat sky'
print q_off2
print Q.quat2radecpa(q_off2)
pix3, _, _ = Q.bore2pix(q_off2, np.array([ctime]), q_bore, nside=nside, pol=True)

testmap = np.zeros(12 * nside**2)
testmap[pix] = 1.
testmap[pix2] = 2.
testmap[pix3] = -3.

plt.figure()
hp.mollview(testmap, coord='G')
plt.savefig('img/test_quat.png')
plt.close()

max_stars_repo_name: zonca/beamconv

max_issues_repo_name: zonca/beamconv

max_forks_repo_name: zonca/beamconv


text:
from torch.optim.optimizer import Optimizer
import numpy as np

class CyclicLR(object):
    '''
    Cyclical learning rates for training neural networks
    Example:
        >>> scheduler = CyclicLR(optimizer)
        >>> for epoch in range(100):
        >>>     scheduler.step()
        >>>     train(...)
        >>>         ...
        >>>         optimizer.zero_grad()
        >>>         loss.backward()
        >>>         optimizer.step()
        >>>         scheduler.batch_step()
        >>>     validate(...)
    '''
    def __init__(self, optimizer, base_lr=1e-3, max_lr=6e-3,
                 step_size=2000, mode='triangular', gamma=1.,
                 scale_fn=None, scale_mode='cycle', last_batch_iteration=-1):
        if not isinstance(optimizer, Optimizer):
            raise TypeError('{} is not an Optimizer'.format(
                type(optimizer).__name__))
        self.optimizer = optimizer

        if isinstance(base_lr, list) or isinstance(base_lr, tuple):
            if len(base_lr) != len(optimizer.param_groups):
                raise ValueError("expected {} base_lr, got {}".format(
                    len(optimizer.param_groups), len(base_lr)))
            self.base_lrs = list(base_lr)
        else:
            self.base_lrs = [base_lr] * len(optimizer.param_groups)

        if isinstance(max_lr, list) or isinstance(max_lr, tuple):
            if len(max_lr) != len(optimizer.param_groups):
                raise ValueError("expected {} max_lr, got {}".format(
                    len(optimizer.param_groups), len(max_lr)))
            self.max_lrs = list(max_lr)
        else:
            self.max_lrs = [max_lr] * len(optimizer.param_groups)

        self.step_size = step_size

        if mode not in ['triangular', 'triangular2', 'exp_range'] \
                and scale_fn is None:
            raise ValueError('mode is invalid and scale_fn is None')

        self.mode = mode
        self.gamma = gamma

        if scale_fn is None:
            if self.mode == 'triangular':
                self.scale_fn = self._triangular_scale_fn
                self.scale_mode = 'cycle'
            elif self.mode == 'triangular2':
                self.scale_fn = self._triangular2_scale_fn
                self.scale_mode = 'cycle'
            elif self.mode == 'exp_range':
                self.scale_fn = self._exp_range_scale_fn
                self.scale_mode = 'iterations'
        else:
            self.scale_fn = scale_fn
            self.scale_mode = scale_mode

        self.batch_step(last_batch_iteration + 1)
        self.last_batch_iteration = last_batch_iteration

    def _triangular_scale_fn(self, x):
        return 1.

    def _triangular2_scale_fn(self, x):
        return 1 / (2. ** (x - 1))

    def _exp_range_scale_fn(self, x):
        return self.gamma**(x)

    def get_lr(self):
        step_size = float(self.step_size)
        cycle = np.floor(1 + self.last_batch_iteration / (2 * step_size))
        x = np.abs(self.last_batch_iteration / step_size - 2 * cycle + 1)

        lrs = []
        param_lrs = zip(self.optimizer.param_groups, self.base_lrs, self.max_lrs)
        for param_group, base_lr, max_lr in param_lrs:
            base_height = (max_lr - base_lr) * np.maximum(0, (1 - x))
            if self.scale_mode == 'cycle':
                lr = base_lr + base_height * self.scale_fn(cycle)
            else:
                lr = base_lr + base_height * self.scale_fn(self.last_batch_iteration)
            lrs.append(lr)
        return lrs

    def batch_step(self, batch_iteration=None):
        if batch_iteration is None:
            batch_iteration = self.last_batch_iteration + 1
        self.last_batch_iteration = batch_iteration
        for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
            param_group['lr'] = lr

max_stars_repo_name: vandykai/ailib

max_issues_repo_name: vandykai/ailib

max_forks_repo_name: vandykai/ailib


text:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import scipy.spatial

from .complexity_embedding import complexity_embedding

def complexity_dimension(signal, delay=1, dimension_max=20, method="afnn", show=False, R=10.0, A=2.0, **kwargs):
    """Estimate optimal Dimension (m) for time-delay embedding.

    Parameters
    ----------
    signal : Union[list, np.array, pd.Series]
        The signal (i.e., a time series) in the form of a vector of values.
    delay : int
        Time delay (often denoted 'Tau', sometimes referred to as 'lag').
        In practice, it is common to have a fixed time lag (corresponding for instance to the
        sampling rate; Gautama, 2003), or to find a suitable value using some algorithmic heuristics
        (see ``complexity_delay()``).
    dimension_max : int
        The maximum embedding dimension (often denoted 'm' or 'd', sometimes referred to as 'order')
        to test.
    method : str
        Method can either be afnn (average false nearest neighbour) or fnn (false nearest neighbour).
    show : bool
        Visualize the result.
    R : float
        Relative tolerance (for fnn method).
    A : float
        Absolute tolerance (for fnn method)
    **kwargs
        Other arguments.

    Returns
    -------
    int
        Optimal dimension.

    See Also
    ------------
    complexity_delay, complexity_embedding

    Examples
    ---------
    >>> import neurokit2 as nk
    >>>
    >>> # Artifical example
    >>> signal = nk.signal_simulate(duration=10, frequency=1, noise=0.01)
    >>> delay = nk.complexity_delay(signal, delay_max=500)
    >>>
    >>> values = nk.complexity_dimension(signal, delay=delay, dimension_max=20, show=True)

    References
    -----------
    - Cao, L. (1997). Practical method for determining the minimum embedding dimension of a scalar
      time series. Physica D: Nonlinear Phenomena, 110(1-2), 43-50.

    """
    # Initalize vectors
    if isinstance(dimension_max, int):
        dimension_seq = np.arange(1, dimension_max + 1)
    else:
        dimension_seq = np.array(dimension_max)

    # Method
    method = method.lower()
    if method in ["afnn"]:
        E, Es = _embedding_dimension_afn(signal, dimension_seq=dimension_seq, delay=delay, **kwargs)
        E1 = E[1:] / E[:-1]
        E2 = Es[1:] / Es[:-1]

        # To find where E1 saturates, set a threshold of difference
        # threshold = 0.1 * (np.max(E1) - np.min(E1))
        min_dimension = [i for i, x in enumerate(E1 >= 0.85 * np.max(E1)) if x][0] + 1
        if show is True:
            _embedding_dimension_plot(
                method=method, dimension_seq=dimension_seq, min_dimension=min_dimension, E1=E1, E2=E2
            )

    elif method in ["fnn"]:
        f1, f2, f3 = _embedding_dimension_ffn(signal, dimension_seq=dimension_seq, delay=delay, R=R, A=A, **kwargs)

        min_dimension = [i for i, x in enumerate(f3 <= 1.85 * np.min(f3[np.nonzero(f3)])) if x][0]

        if show is True:
            _embedding_dimension_plot(
                method=method, dimension_seq=dimension_seq, min_dimension=min_dimension, f1=f1, f2=f2, f3=f3
            )

    else:
        raise ValueError("NeuroKit error: complexity_dimension(): 'method' " "not recognized.")

    return min_dimension

# =============================================================================
# Methods
# =============================================================================
def _embedding_dimension_afn(signal, dimension_seq, delay=1, **kwargs):
    """Return E(d) and E^*(d) for a all d in dimension_seq.

    E(d) and E^*(d) will be used to calculate E1(d) and E2(d)

    El(d) = E(d + 1)/E(d). E1(d) stops changing when d is greater than some value d0  if the time
    series comes from an attractor. Then d0 + 1 is the minimum embedding dimension we look for.

    E2(d) = E*(d + 1)/E*(d). E2(d) is a useful quantity to distinguish deterministic signals from
    stochastic signals. For random data, since the future values are independent of the past values,
    E2(d) will be equal to 1 for any d. For deterministic data, E2(d) is certainly related to d, it
    cannot be a constant for all d; there must exist somed's such that E2(d) is not 1.

    """
    values = np.asarray(
        [_embedding_dimension_afn_d(signal, dimension, delay, **kwargs) for dimension in dimension_seq]
    ).T
    E, Es = values[0, :], values[1, :]

    return E, Es

def _embedding_dimension_afn_d(signal, dimension, delay=1, metric="chebyshev", window=10, maxnum=None, **kwargs):
    """Return E(d) and E^*(d) for a single d.

    Returns E(d) and E^*(d) for the AFN method for a single d.

    """
    # We need to reduce the number of points in dimension d by tau
    # so that after reconstruction, there'll be equal number of points
    # at both dimension d as well as dimension d + 1.
    y1 = complexity_embedding(signal[:-delay], delay=delay, dimension=dimension)
    y2 = complexity_embedding(signal, delay=delay, dimension=dimension + 1)

    # Find near neighbors in dimension d.
    index, dist = _embedding_dimension_neighbors(y1, metric=metric, window=window, maxnum=maxnum)

    # Compute the near-neighbor distances in d + 1 dimension
    d = np.asarray([scipy.spatial.distance.chebyshev(i, j) for i, j in zip(y2, y2[index])])
    # Compute the ratio of near-neighbor distances in d + 1 over d dimension
    # Its average is E(d)
    E = np.mean(d / dist)

    # Calculate E^*(d)
    Es = np.mean(np.abs(y2[:, -1] - y2[index, -1]))
    return E, Es

def _embedding_dimension_ffn(signal, dimension_seq, delay=1, **kwargs):
    """Compute the fraction of false nearest neighbors.

    The false nearest neighbors (FNN) method described by Kennel et al.
    (1992) to calculate the minimum embedding dimension required to embed a scalar time series.

    Parameters
    ----------
    signal : Union[list, np.array, pd.Series]
        The signal (i.e., a time series) in the form of a vector of values.
    dimension_seq : int
        The embedding dimension.
    delay : int
        Time delay (often denoted 'Tau', sometimes referred to as 'lag').
    **kwargs
        Other arguments.

    Returns
    -------
    f1 : array
        Fraction of neighbors classified as false by Test I.
    f2 : array
        Fraction of neighbors classified as false by Test II.
    f3 : array
        Fraction of neighbors classified as false by either Test I
        or Test II.

    """
    values = np.asarray(
        [_embedding_dimension_ffn_d(signal, dimension, delay, **kwargs) for dimension in dimension_seq]
    ).T
    f1, f2, f3 = values[0, :], values[1, :], values[2, :]

    return f1, f2, f3

def _embedding_dimension_ffn_d(signal, dimension, delay=1, R=10.0, A=2.0, metric="euclidean", window=10, maxnum=None):
    """Return fraction of false nearest neighbors for a single d."""
    # We need to reduce the number of points in dimension d by tau
    # so that after reconstruction, there'll be equal number of points
    # at both dimension d as well as dimension d + 1.
    y1 = complexity_embedding(signal[:-delay], delay=delay, dimension=dimension)
    y2 = complexity_embedding(signal, delay=delay, dimension=dimension + 1)

    # Find near neighbors in dimension d.
    index, dist = _embedding_dimension_neighbors(y1, metric=metric, window=window, maxnum=maxnum)
    # Compute the near-neighbor distances in d + 1 dimension
    d = np.asarray([scipy.spatial.distance.chebyshev(i, j) for i, j in zip(y2, y2[index])])

    # Find all potential false neighbors using Kennel et al.'s tests.
    f1 = np.abs(y2[:, -1] - y2[index, -1]) / dist > R
    f2 = d / np.std(signal) > A
    f3 = f1 | f2

    return np.mean(f1), np.mean(f2), np.mean(f3)

# =============================================================================
# Internals
# =============================================================================
def _embedding_dimension_plot(
    method, dimension_seq, min_dimension, E1=None, E2=None, f1=None, f2=None, f3=None, ax=None
):

    if ax is None:
        fig, ax = plt.subplots()
    else:
        fig = None
    ax.set_title("Optimization of Dimension (d)")
    ax.set_xlabel("Embedding dimension $d$")
    ax.set_ylabel("$E_1(d)$ and $E_2(d)$")
    if method in ["afnn"]:
        ax.plot(dimension_seq[:-1], E1, "bo-", label="$E_1(d)$", color="#FF5722")
        ax.plot(dimension_seq[:-1], E2, "go-", label="$E_2(d)$", color="#f44336")

    if method in ["fnn"]:
        ax.plot(dimension_seq, 100 * f1, "bo--", label="Test I", color="#FF5722")
        ax.plot(dimension_seq, 100 * f2, "g^--", label="Test II", color="#f44336")
        ax.plot(dimension_seq, 100 * f3, "rs-", label="Test I + II", color="#852b01")

    ax.axvline(x=min_dimension, color="#E91E63", label="Optimal dimension: " + str(min_dimension))
    ax.legend(loc="upper right")

    return fig

def _embedding_dimension_neighbors(
    signal, dimension_max=20, delay=1, metric="chebyshev", window=0, maxnum=None, show=False
):
    """Find nearest neighbors of all points in the given array. Finds the nearest neighbors of all points in the given
    array using SciPy's KDTree search.

    Parameters
    ----------
    signal : ndarray or array or list or Series
        embedded signal: N-dimensional array containing time-delayed vectors, or
        signal: 1-D array (e.g.time series) of signal in the form of a vector of values.
        If signal is input, embedded signal will be created using the input dimension and delay.
    delay : int
        Time delay (often denoted 'Tau', sometimes referred to as 'lag'). In practice, it is common
        to have a fixed time lag (corresponding for instance to the sampling rate; Gautama, 2003),
        or to find a suitable value using some algorithmic heuristics (see ``delay_optimal()``).
    dimension_max : int
        The maximum embedding dimension (often denoted 'm' or 'd', sometimes referred to as 'order')
        to test.
    metric : str
        Metric to use for distance computation.  Must be one of "cityblock" (aka the Manhattan metric),
        "chebyshev" (aka the maximum norm metric), or "euclidean". Defaults to 'chebyshev'.
    window : int
        Minimum temporal separation (Theiler window) that should exist between near neighbors.
        This is crucial while computing Lyapunov exponents and the correlation dimension. Defaults to 0.
    maxnum : int
        Maximum number of near neighbors that should be found for each point.
        In rare cases, when there are no neighbors that are at a nonzero distance, this will have to
        be increased (i.e., beyond 2 * window + 3). Defaults to None (optimum).
    show : bool
        Defaults to False.

    Returns
    -------
    index : array
        Array containing indices of near neighbors.
    dist : array
        Array containing near neighbor distances.

    """

    # Sanity checks
    if len(signal.shape) == 1:
        y = complexity_embedding(signal, delay=delay, dimension=dimension_max)
    else:
        y = signal

    if metric == "chebyshev":
        p = np.inf
    elif metric == "cityblock":
        p = 1
    elif metric == "euclidean":
        p = 2
    else:
        raise ValueError('Unknown metric. Should be one of "cityblock", ' '"euclidean", or "chebyshev".')

    tree = scipy.spatial.cKDTree(y)  # pylint: disable=E1102
    n = len(y)

    if not maxnum:
        maxnum = (window + 1) + 1 + (window + 1)
    else:
        maxnum = max(1, maxnum)

    if maxnum >= n:
        raise ValueError("maxnum is bigger than array length.")

    dists = np.empty(n)
    indices = np.empty(n, dtype=int)

    for i, x in enumerate(y):
        for k in range(2, maxnum + 2):
            # querry for k number of nearest neighbours
            dist, index = tree.query(x, k=k, p=p)
            # remove points that are closer than min temporal separation
            # remove self reference (d > 0)
            valid = (np.abs(index - i) > window) & (dist > 0)

            if np.count_nonzero(valid):
                dists[i] = dist[valid][0]
                indices[i] = index[valid][0]
                break

            if k == (maxnum + 1):
                raise Exception(
                    "Could not find any near neighbor with a nonzero distance." "Try increasing the value of maxnum."
                )

    indices, values = np.squeeze(indices), np.squeeze(dists)

    if show is True:
        plt.plot(indices, values)

    return indices, values

max_stars_repo_name: aristotelisxs/NeuroKit

max_issues_repo_name: aristotelisxs/NeuroKit

max_forks_repo_name: aristotelisxs/NeuroKit

reason: import numpy,import scipy


text:
import math

import numpy as np
import pandas as pd

from houseregression_model.predict import make_predictions

def test_make_prediction(sample_data: pd.DataFrame) -> None:
    """Test for testing predictions and expected values"""

    # Given
    expected_first_prediction_value = 113422
    expected_no_predictions = sample_data.shape[0]

    # When
    result = make_predictions(input_data=sample_data)

    # Then
    predictions = result.get("predictions")

    assert isinstance(predictions, list)
    assert isinstance(predictions[0], np.float64)
    assert result.get("errors") is None
    assert math.isclose(len(predictions), expected_no_predictions, abs_tol=20)
    assert math.isclose(predictions[0], expected_first_prediction_value, abs_tol=100)


text:
import pytest
import numpy as np

from pyqumo.sim.helpers import FiniteFifoQueue, Queue, InfiniteFifoQueue, Server

# ###########################################################################
# TEST FiniteFifoQueue
# ###########################################################################
@pytest.mark.parametrize('capacity', [0, 5])
def test_finite_fifo_queue_props(capacity):
    """
    Validate capacity, empty and full properties of a queue with finite cap.
    """
    queue: Queue[int] = FiniteFifoQueue(capacity)
    assert queue.capacity == capacity
    assert queue.size == 0
    assert queue.empty

    if capacity == 0:
        assert queue.full

    # Fill half of the queue with values, check it is neither empty nor full:
    half_capacity = capacity // 2
    for i in range(half_capacity):
        queue.push(i)

    assert queue.size == half_capacity
    if half_capacity > 0:
        assert not queue.empty
        assert not queue.full

    # Fill the rest of the queue and make sure it becomes full then:
    for _ in range(half_capacity, capacity):
        queue.push(0)
    assert queue.size == capacity
    assert queue.full

def test_finite_fifo_queue_push_pop_order():
    """
    Validate push() and pop() works in FIFO order.
    """
    # Put 10 [OK] => 20 [OK] => 30 [LOST]
    queue: Queue[int] = FiniteFifoQueue(2)
    assert queue.push(10)
    assert queue.push(20)
    assert not queue.push(30)
    # queue = [10, 20]
    # Pop 10 => { queue = [20] } => push 40 => { queue = [20, 40] }
    assert queue.pop() == 10
    assert queue.push(40)
    # queue = [20, 40]
    # Pop 20 => Pop 40 => Pop [NONE]
    assert queue.pop() == 20
    assert queue.pop() == 40
    assert queue.pop() is None
    assert queue.empty

def test_finite_fifo_queue_str():
    """
    Validate __repr__() method of the FiniteFifoQueue.
    """
    queue: Queue[int] = FiniteFifoQueue(5)
    assert str(queue) == "(FiniteFifoQueue: q=[], capacity=5, size=0)"
    queue.push(34)
    queue.push(42)
    assert str(queue) == "(FiniteFifoQueue: q=[34, 42], capacity=5, size=2)"
    queue.push(1)
    queue.push(2)
    queue.push(3)
    assert str(queue) == "(FiniteFifoQueue: q=[34, 42, 1, 2, 3], " \
                         "capacity=5, size=5)"

# ###########################################################################
# TEST InfiniteFifoQueue
# ###########################################################################
def test_infinite_fifo_queue():
    """
    Validate basic properties and push/pop to the infinite FIFO queue.
    """
    queue: Queue[int] = InfiniteFifoQueue()
    assert queue.capacity == np.inf
    assert queue.size == 0
    assert queue.empty
    assert not queue.full

    # Add some elements:
    queue.push(1)
    queue.push(2)
    assert queue.size == 2
    assert not queue.full
    assert not queue.empty
    assert str(queue) == "(InfiniteFifoQueue: q=[1, 2], size=2)"

    # Pop:
    assert queue.pop() == 1
    assert queue.pop() == 2
    assert queue.pop() is None
    assert queue.empty
    assert str(queue) == "(InfiniteFifoQueue: q=[], size=0)"

    # Push many elements:
    num_elements = 1000
    for i in range(num_elements):
        item = (i + 42) * 10
        queue.push(item)
    assert queue.size == num_elements
    assert not queue.full
    assert queue.pop() == 420

# ############################################################################
# TEST Server
# ############################################################################
def test_server():
    """
    Validate Server operations.
    """
    server: Server[int] = Server()
    assert str(server) == "(Server: busy=False)"
    assert server.ready
    assert not server.busy
    assert server.size == 0

    # Push a packet
    server.serve(10)
    assert not server.ready
    assert server.busy
    assert server.size == 1
    assert str(server) == "(Server: busy=True, packet=10)"

    # Check that pushing another packet raises error:
    with pytest.raises(RuntimeError):
        server.serve(20)

    # Pop a packet:
    assert server.pop() == 10
    assert str(server) == "(Server: busy=False)"
    assert server.ready
    assert not server.busy
    assert server.size == 0

max_stars_repo_name: larioandr/thesis-queues

max_issues_repo_name: larioandr/thesis-queues

max_forks_repo_name: larioandr/thesis-queues


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

"""

#from waveform import AWG_instr, waveform_channel, waveform
#from waveform import major_channel, make_iterable, IEEE_block_format
from waveform_lazy import AWG_instr, waveform_channel, waveform
from waveform_lazy import major_channel, make_iterable
import numpy as np
from time import time, sleep
import sys
from instr import KeyboardInterruptProtection

from Queue import Queue
from threading import Thread

def print_status(msg, overwrite=True):
    # print a one-line, overwritten message
    sys.stdout.write("\r" + msg if overwrite else msg)
    sys.stdout.flush()

class AWG5000(AWG_instr):
    """
    addr: For TCPIP, specify host addr (e.g. '192.168.1.2') with interface = 'LAN' and
          start the VXI-11 server in the AWG 5000 instrument.
    """

    def _selfinit(self, **kwargs):
        #super(AWG5000, self)._selfinit(**kwargs)
        if hasattr(self, 'instr'):
            self.instr.read_termination = '\n'
            self.instr.timeout = 30000  #30 sec: some responses might be very slow
            self.clear_buffer()

            self.t_sample = kwargs.get('t_sample', 0.1)
            self.catalog_seq, self.catalog_wfm = {}, {}
            self.safe_write(':AWGC:RMOD SEQ')

            # Clear error queue
            self.check_errors(raise_exception=False)

        self.ch1 = waveform_channel(instr = self, ch_id = 'ch1')
        self.ch2 = waveform_channel(instr = self, ch_id = 'ch2')
        self.mk11 = waveform_channel(instr = self, ch_id = 'mk11')
        self.mk12 = waveform_channel(instr = self, ch_id = 'mk12')
        self.mk21 = waveform_channel(instr = self, ch_id = 'mk21')
        self.mk22 = waveform_channel(instr = self, ch_id = 'mk22')
        self.ch3 = waveform_channel(instr = self, ch_id = 'ch3')
        self.ch4 = waveform_channel(instr = self, ch_id = 'ch4')
        self.mk31 = waveform_channel(instr = self, ch_id = 'mk31')
        self.mk32 = waveform_channel(instr = self, ch_id = 'mk32')
        self.mk41 = waveform_channel(instr = self, ch_id = 'mk41')
        self.mk42 = waveform_channel(instr = self, ch_id = 'mk42')

        if hasattr(self, 'instr'):
            self.software_sequencer = kwargs.get('software_sequencer', False)
            self.safe_write(':AWGC:RMOD ENH')
            self.num_of_lines = 0
            self.scales = kwargs.get('scales', 1.)
            self.amplitudes = kwargs.get('amplitudes', 1.0)

    @property
    def t_sample(self):
        return  self._t_sample if hasattr(self,'instr') else 1.0

    @t_sample.setter
    def t_sample(self, newval):
        self._t_sample = np.clip(newval, 1./1.2, 100)
        self.safe_write(":FREQ %13.10f MHz" % (1000./self._t_sample))

    @property
    def scales(self):
        return [self.ch1.scale, self.ch2.scale,self.ch3.scale,self.ch4.scale]

    @scales.setter
    def scales(self, scales):
        scales = make_iterable(scales, repeat_len = 4)
        self.ch1.scale, self.ch2.scale,self.ch3.scale,self.ch4.scale = scales[0], scales[1],scales[2],scales[3]

    @property
    def amplitudes(self):
        return [float(self.safe_query('SOUR%d:VOLT?'%i)) for i in [1,2,3,4]]

    @amplitudes.setter
    def amplitudes(self, amps):
        amps = make_iterable(amps, repeat_len = 4)
        for i in [1,2,3,4]:
            if not 0.02 <= amps[i-1] <= 4.5:
                raise Exception('Amplitude out of range.')
            self.safe_write('SOUR%d:VOLT %f'%(i, amps[i-1]))

#"""
#    @property
#    def dac_resolutions(self):
#        return [float(self.instr.query('SOUR%d:DAC:RES?'%i)) for i in [1,2,3,4]]
#
#    @dac_resolutions.setter
#    def dac_resolutions(self, resolutions):
#        resol = make_iterable(resolutions, repeat_len = 4)
#        for i in [1,2,3,4]:
#            if not resol[i-1] in (8, 10):
#                raise Exception('Invalid number of bits.')
#            self.instr.write('SOUR%d:DAC:RES %f'%(i, resol[i-1]))
#"""

    def _check_wfm_length(self, wfm_length, index):
        if wfm_length < 256:
            raise Exception("The length of waveform for AWG5000 must be no less than 256 for correct sequencer operation.")

    def ch1_ids(self, id_list = ['ch1', 'mk11', 'mk12']):
        return [_id for _id in id_list if _id in ['ch1', 'mk11', 'mk12']]

    def ch2_ids(self, id_list = ['ch2', 'mk21', 'mk22']):
        return [_id for _id in id_list if _id in ['ch2', 'mk21', 'mk22']]

    def ch3_ids(self, id_list = ['ch3', 'mk31', 'mk32']):
        return [_id for _id in id_list if _id in ['ch3', 'mk31', 'mk32']]

    def ch4_ids(self, id_list = ['ch4', 'mk41', 'mk42']):
        return [_id for _id in id_list if _id in ['ch4', 'mk41', 'mk42']]

    def ch(self, ch_id):
        if ch_id in self.ch1_ids() or ch_id in self.ch2_ids() or ch_id in self.ch3_ids() or ch_id in self.ch4_ids():
            return getattr(self, ch_id)

    def send_wfms(self, ch_id, **kwargs):
        if ('id_list' in kwargs and ch_id == kwargs['id_list'][0]) or kwargs.get('clear_buffer', False):
            self.stop()
            print_status("Deleting waveforms...")
            self.safe_write('WLISt:WAVeform:DELete ALL')
        id_list = kwargs.get('id_list', [ch_id,])
        if ch_id in self.ch1_ids() and ch_id == self.ch1_ids(id_list)[0]:
            bundle = waveform([self.ch1, self.mk11, self.mk12])
            suffix = '_ch1'
        elif ch_id in self.ch2_ids() and ch_id == self.ch2_ids(id_list)[0]:
            bundle = waveform([self.ch2, self.mk21, self.mk22])
            suffix = '_ch2'
        elif ch_id in self.ch3_ids() and ch_id == self.ch3_ids(id_list)[0]:
            bundle = waveform([self.ch3, self.mk31, self.mk32])
            suffix = '_ch3'
        elif ch_id in self.ch4_ids() and ch_id == self.ch4_ids(id_list)[0]:
            bundle = waveform([self.ch4, self.mk41, self.mk42])
            suffix = '_ch4'
        else:
            return

        for index, wfm_length in enumerate(bundle.wfm_len_list()):
            self._check_wfm_length(wfm_length, index)

        self._OPC() #complete all pending operations
        self.check_errors()

        wfm_queue = Queue(maxsize=2)
        def _gen_wfms():  # Generate waveform text asynchronously
            for wfm_file, wfm_length, name in bundle.gen_format_TekWFM(format='integer'):
                info = ({'name': name + suffix, 'start_index':0, 'size': wfm_length, 'block_data': wfm_file})
                wfm_queue.put(info, block=True)
            wfm_queue.put(None, block=True)  # End flag
        t = Thread(target=_gen_wfms)
        t.daemon = True
        t.start()

        start = time()
        send_time = 0.
        sent_names = []
        while True:
            info = wfm_queue.get(block=True, timeout=30.0)
            if info is None:
                break
            if info['name'] not in sent_names:
                print_status("Sending {name}...".format(**info))
                from_time = time()
    #            self.safe_write('WLISt:WAVeform:NEW "{name}",{size},REAL'.format(**info))
                self.safe_write('WLISt:WAVeform:NEW "{name}",{size},INTeger'.format(**info))
                self._OPC()
                with KeyboardInterruptProtection():
                    self.instr.write_raw('WLISt:WAVeform:DATA "{name}",{start_index},{size},{block_data}'.format(**info))
                send_time += time() - from_time
                sent_names.append(info['name'])
        print_status("Waveforms generated in {gen_time:.2f} and sent in {time:.2f}".format(time=send_time, gen_time=time() - start - send_time))
        self.check_errors()

    def load_seq(self, ch_id='ch1', **kwargs):
        self.send_wfms(ch_id, **kwargs)
        id_list = kwargs.get('id_list', [ch_id,])
        id_list = [ch for ch in id_list if ch.startswith('ch')]

        if ch_id == id_list[-1]:
            self._OPC()  #complete all pending operations
            main_ch =  major_channel([self.ch(ch) for ch in (self.ch1_ids() + self.ch2_ids() + self.ch3_ids() + self.ch4_ids())])
            print_status('Loading the sequence into AWG ({addr})'.format(addr=self._addr))
            start = time()
            for cmd in main_ch.seq.format_TekSCPI(id_list=id_list):
                self.safe_write(cmd)
                self._OPC()  #complete all pending operations
            print_status(' in {time:.2f} seconds'.format(time = time() - start), overwrite=False)
            self.num_of_lines = len(main_ch.seq.data)

            self.check_errors()
            self.run(id_list=id_list, jump=kwargs.get('jump', None))

    def run(self, id_list=['ch1', 'ch2', 'ch3', 'ch4'], jump=None):
        # AWG must be run *before* channel outputs are turned on
        # to prevent unwanted waveform output
        self.safe_write(':AWGC:RUN:IMM')
        print_status("Starting AWG sequence. This may take a moment...")
        counter = 0
        while self.safe_query(':AWGC:RST?') not in (u'1', u'2'):
            counter += 1
            sleep(0.1)
            if counter % 10 == 0:
                self.check_errors()
        if jump is not None:
            self.jump(jump)
        print_status("Turning AWG output on. This may take a moment...")
        for ch in id_list:
            n = int(ch.lstrip('ch'))
            self.safe_write(':OUTP%d:STAT ON' % (n))
            counter = 0
            while self.safe_query(':OUTP%d:STAT?' % (n)) != u'1':
                counter += 1
                sleep(0.1)
                if counter % 10 == 0:
                    self.check_errors()
        print_status("Done.")
        self.check_errors()

    def stop(self):
        # All channel outputs must be turned off *before* AWG is stopped
        # to prevent unwanted waveform output
        print_status("Turning AWG output off. This may take a moment...")
        for n in [1, 2, 3, 4]:
            self.safe_write(':OUTP%d:STAT OFF' % (n))
            counter = 0
            while self.safe_query(':OUTP%d:STAT?' % (n)) != u'0':
                counter += 1
                sleep(0.1)
                if counter % 10 == 0:
                    self.check_errors()
        self.safe_write(':AWGC:STOP:IMM')
        print_status("Stopping AWG sequence. This may take a moment...")
        counter = 0
        while self.safe_query(':AWGC:RST?') in (u'1', u'2'):
            counter += 1
            sleep(0.1)
            if counter % 10 == 0:
                self.check_errors()
        print_status("Done.")
        self.check_errors()

    def jump(self, to, ch_id = 'ch1', **kwargs):
        id_list = kwargs.get('id_list', [ch_id,])
        if ch_id == id_list[-1]:
            t0 = time()
            while self.safe_query(':AWGC:RST?') != u'2':
                if time() > t0 + 10:#wait up to 10 seconds
                    raise Exception('Cannot jump while the AWG is waiting for trigger or stopped.')
            self.safe_write('SEQuence:JUMP:IMMediate %d'%(to%self.num_of_lines+1))

    def trigger(self):
        self.safe_write('*TRG')

    def _OPC(self):
        orig_timeout, self.instr.timeout = self.instr.timeout, float('+inf')
        try:
            code = self.instr.query('*OPC?')  # Wait forever, accepting keyboard interrupt
            if code == u'1':
                return
            else:
                print("*OPC? returned an unexpected code '{0}'".format(code))
                raise
        except:
            raise Exception("Failed to complete pending operations. You may have to call clear_buffer() to clear the message buffer.")
        finally:
            self.instr.timeout = orig_timeout

    def check_errors(self, raise_exception=True):
        self.errors = []
        while True:
            error = self.safe_query(':SYST:ERR?')
            if error.startswith('0'):
                break
            else:
                self.errors.append(error)
        if self.errors:
            if raise_exception:
                msg = 'Error occurred in AWG5000 (@ {addr}):\n'.format(addr=self._addr)
                for e in self.errors:
                    msg += e.replace('\r', '') + '\n'
                raise Exception(msg)
            else:
                print 'AWG ({addr}) has {num} error messages stored in self.errors.'.format(addr=self._addr,
                                                                                num=len(self.errors))


text:
"""Implements an operator that detects lanes."""
import math
from collections import namedtuple

import cv2

import erdos

import numpy as np

Line = namedtuple("Line", "x1, y1, x2, y2, slope")

class CannyEdgeLaneDetectionOperator(erdos.Operator):
    """Detects driving lanes using a camera.

    The operator uses standard vision techniques (Canny edge).

    Args:
        camera_stream (:py:class:`erdos.ReadStream`): The stream on which
            camera frames are received.
        detected_lanes_stream (:py:class:`erdos.WriteStream`): Stream on which
            the operator sends
            :py:class:`~pylot.perception.messages.LanesMessage` messages.
        flags (absl.flags): Object to be used to access absl flags.
    """
    def __init__(self, camera_stream, detected_lanes_stream, flags):
        camera_stream.add_callback(self.on_msg_camera_stream,
                                   [detected_lanes_stream])
        self._flags = flags
        self._logger = erdos.utils.setup_logging(self.config.name,
                                                 self.config.log_file_name)
        self._kernel_size = 7

    @staticmethod
    def connect(camera_stream):
        """Connects the operator to other streams.

        Args:
            camera_stream (:py:class:`erdos.ReadStream`): The stream on which
                camera frames are received.

        Returns:
            :py:class:`erdos.WriteStream`: Stream on which the operator sends
            :py:class:`~pylot.perception.messages.LanesMessage` messages.
        """
        detected_lanes_stream = erdos.WriteStream()
        return [detected_lanes_stream]

    @erdos.profile_method()
    def on_msg_camera_stream(self, msg, detected_lanes_stream):
        """Invoked whenever a frame message is received on the stream.

        Args:
            msg: A :py:class:`~pylot.perception.messages.FrameMessage`.
            detected_lanes_stream (:py:class:`erdos.WriteStream`): Stream on
                which the operator sends
                :py:class:`~pylot.perception.messages.LanesMessage` messages.
        """
        self._logger.debug('@{}: {} received message'.format(
            msg.timestamp, self.config.name))
        assert msg.frame.encoding == 'BGR', 'Expects BGR frames'
        # Make a copy of the image coming into the operator.
        image = np.copy(msg.frame.as_numpy_array())

        # Get the dimensions of the image.
        x_lim, y_lim = image.shape[1], image.shape[0]

        # Convert to grayscale.
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # Apply gaussian blur.
        image = cv2.GaussianBlur(image, (self._kernel_size, self._kernel_size),
                                 0)

        # Apply the Canny Edge Detector.
        image = cv2.Canny(image, 30, 60)

        # Define a region of interest.
        points = np.array(
            [[
                (0, y_lim),  # Bottom left corner.
                (0, y_lim - 60),
                (x_lim // 2 - 20, y_lim // 2),
                (x_lim // 2 + 20, y_lim // 2),
                (x_lim, y_lim - 60),
                (x_lim, y_lim),  # Bottom right corner.
            ]],
            dtype=np.int32)
        image = self._region_of_interest(image, points)

        # Hough lines.
        image = self._draw_lines(image)

        detected_lanes_stream.send(erdos.Message(msg.timestamp, image))

    def _region_of_interest(self, image, points):
        mask = np.zeros_like(image)
        cv2.fillPoly(mask, points, 255)
        return cv2.bitwise_and(image, mask)

    def _extrapolate_lines(self, image, left_line, right_line):
        top_y = None
        if left_line is not None and right_line is not None:
            top_y = min(
                [left_line.y1, left_line.y2, right_line.y1, right_line.y2])
        base_y = image.shape[0]

        final_lines = []
        if left_line is not None:
            actual_slope = float(left_line.y2 -
                                 left_line.y1) / float(left_line.x2 -
                                                       left_line.x1)
            base_x = int((base_y - left_line.y1) / actual_slope) + left_line.x1
            final_lines.append(
                Line(base_x, base_y, left_line.x1, left_line.y1, actual_slope))

            if top_y is None:
                top_y = min([left_line.y1, left_line.y2])

            top_x = int((top_y - left_line.y2) / actual_slope) + left_line.x2
            final_lines.append(
                Line(top_x, top_y, left_line.x2, left_line.y2, actual_slope))

        if right_line is not None:
            actual_slope = float(right_line.y2 -
                                 right_line.y1) / float(right_line.x2 -
                                                        right_line.x1)
            base_x = int(
                (base_y - right_line.y1) / actual_slope) + right_line.x1
            final_lines.append(
                Line(base_x, base_y, right_line.x1, right_line.y1,
                     actual_slope))

            if top_y is None:
                top_y = min([right_line.y1, right_line.y2])

            top_x = int((top_y - right_line.y2) / actual_slope) + right_line.x2
            final_lines.append(
                Line(top_x, top_y, right_line.x2, right_line.y2, actual_slope))
        return final_lines

    def _draw_lines(self, image):
        lines = cv2.HoughLinesP(image,
                                rho=1,
                                theta=np.pi / 180.0,
                                threshold=40,
                                minLineLength=10,
                                maxLineGap=30)
        line_img = np.zeros((image.shape[0], image.shape[1], 3),
                            dtype=np.uint8)

        if lines is None:
            return line_img

        # Construct the Line tuple collection.
        cmp_lines = []
        for line in lines:
            for x1, y1, x2, y2 in line:
                slope = math.degrees(math.atan2(y2 - y1, x2 - x1))
                cmp_lines.append(Line(x1, y1, x2, y2, slope))

        # Sort the lines by their slopes after filtering lines whose slopes
        # are > 20 or < -20.
        cmp_lines = sorted(filter(
            lambda line: line.slope > 20 or line.slope < -20, cmp_lines),
                           key=lambda line: line.slope)

        if len(cmp_lines) == 0:
            return line_img

        # Filter the lines with a positive and negative slope and choose
        # a single line out of those.
        left_lines = [
            line for line in cmp_lines if line.slope < 0 and line.x1 < 300
        ]
        right_lines = [
            line for line in cmp_lines
            if line.slope > 0 and line.x1 > image.shape[1] - 300
        ]

        final_lines = []
        # Find the longest line from the left and the right lines and
        # extrapolate to the middle of the image.
        left_line = None
        if len(left_lines) != 0:
            left_line = max(left_lines,
                            key=lambda line: abs(line.y2 - line.y1))
            final_lines.append(left_line)

        right_line = None
        if len(right_lines) != 0:
            right_line = max(right_lines,
                             key=lambda line: abs(line.y2 - line.y1))
            final_lines.append(right_line)

        final_lines.extend(
            self._extrapolate_lines(image, left_line, right_line))

        for x1, y1, x2, y2, slope in final_lines:
            cv2.line(line_img, (x1, y1), (x2, y2),
                     color=(255, 0, 0),
                     thickness=2)
            cv2.putText(line_img, "({}, {})".format(x1, y1), (x1, y1),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 2,
                        cv2.LINE_AA)
        return line_img

max_stars_repo_name: Shaviv-Hoffman-Lowitz/pylot


text:
import torch
import cv2
import numpy as np

cap = cv2.VideoCapture("ped.webm")
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

while 1:  # 727
    # Take each frame
    _, frame = cap.read()

    results = model(frame)
    results.save()
    #results.

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.destroyAllWindows()
cap.release()


text:
import cv2
import numpy as np

def SRS(r, i):
    mI = np.mean(i)
    r_eh = np.zeros_like(r)
    mask1 = r > mI
    mask2 = r <= mI

    r_changed = r * (i/mI)**0.5
    r_eh[mask1] = r_changed[mask1]
    r_eh[mask2] = r[mask2]

    return r_eh

def VIG(i, i_inv, v_s):
    i_inv /=np.max(i_inv)
    mI = np.mean(i)
    M = np.max(i)
    r = 1.0 - mI/M
    fv_s = [r*( 1/(1+np.exp(-1.0*(v - mI))) - 0.5 ) for v in v_s]

    I_k = [(1 + fv) * (i + fv * i_inv) for fv in fv_s]

    return I_k

def tone_production(R_eh, I_vts):
    L_s = [np.exp(R_eh) * I for I in I_vts]

    Ws = np.zeros_like(I_vts)
    for i, I in enumerate(I_vts):
        if i < 3:
            Ws[i,:,:] = I/np.max(I)
        else:
            Ws[i,:,:] = 1.0 - I/np.max(I)

    L_eh = np.zeros_like(R_eh)
    W_ = np.zeros_like(R_eh)
    for W, L in zip(Ws, L_s):
        L_eh += W * L
        W_ += W

    L_eh = L_eh/W_
    return L_eh

max_stars_repo_name: tuvovan/EnhanceHDR

max_issues_repo_name: tuvovan/EnhanceHDR

max_forks_repo_name: tuvovan/EnhanceHDR