text:
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import argparse
import os
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
from torch.autograd import Variable
import torchvision.utils as vutils
import torch.nn.functional as F
import numpy as np
import time
from tensorboardX import SummaryWriter
from datasets import __datasets__
from models import __models__
from utils import *
from utils.color_map import *
from torch.utils.data import DataLoader
from torchvision.utils import save_image
import gc
from PIL import Image
import random
import torchvision.transforms as transforms

cudnn.benchmark = True

parser = argparse.ArgumentParser(description='Net')
parser.add_argument('--mode', type=str, default='test', help='train or test')
parser.add_argument('--model', default='nn', help='select a model structure', choices=__models__.keys())
parser.add_argument('--dataset', required=True, help='dataset name', choices=__datasets__.keys())
parser.add_argument('--datapath', default='', help='data path')
parser.add_argument('--channels', type=int, default=3, help='net input channels')
parser.add_argument('--out_channels', type=int, default=1, help='net output channels')
parser.add_argument('--testlist', required=True, help='testing list')
parser.add_argument('--test_batch_size', type=int, default=8, help='testing batch size')
parser.add_argument('--test_crop_height', type=int, default=128, help='test crop height')
parser.add_argument('--test_crop_width', type=int, default=256, help='test crop width')
parser.add_argument('--logdir', required=True, help='the directory to save logs and checkpoints')
parser.add_argument('--loadckpt', help='load the weights from a specific checkpoint')
parser.add_argument('--saveresult', default='', help='save result images')

# parse arguments, set seeds
args = parser.parse_args()
os.makedirs(args.saveresult, exist_ok=True)

# dataset, dataloader
StereoDataset = __datasets__[args.dataset]
test_dataset = StereoDataset(args.datapath, args.testlist, False, args.test_crop_height, args.test_crop_width, args.channels)
TestImgLoader = DataLoader(test_dataset, args.test_batch_size, shuffle=False, num_workers=1, drop_last=False)

# model, optimizer
model = __models__[args.model](args.out_channels)
model = nn.DataParallel(model)
model.cuda()

# test one sample
@make_nograd_func
def test_sample(sample, compute_metrics=True):
    model.eval()
    ori, gt = sample['ori'], sample['gt']
    ori = ori.cuda()
    ori_name = sample['img_name']

    torch.cuda.synchronize()
    start_time = time.time()
    pre = model(ori)

    pred = nn.functional.softmax(pre, dim=1)  # 测试--取最大值下标
    pred_label = torch.max(pred, dim=1)

    torch.cuda.synchronize()
    end_time = time.time()
    time_consume = end_time - start_time
    print('Time_Consume: ', time_consume * 1000)
    image_outputs = {"ori": ori, "gt": gt, "ori_name": ori_name, "pre": pred_label}  # tuple
    return image_outputs

def test_batch():
    # find all checkpoints file and sort according to epoch id
    #all_saved_ckpts = [fn for fn in os.listdir(args.logdir) if fn.endswith(".ckpt")]
    all_saved_ckpts = [fn for fn in os.listdir(args.logdir) if fn.endswith(".tar")]
    all_saved_ckpts = sorted(all_saved_ckpts, key=lambda x: int(x.split('_')[-1].split('.')[0]))
    for ckpt_idx, ckpt_path in enumerate(all_saved_ckpts):
        loadckpt = os.path.join(args.logdir, ckpt_path)
        print("loading the model in logdir: {}".format(loadckpt))
        state_dict = torch.load(loadckpt)
        # model.load_state_dict(state_dict['model'])
        model.load_state_dict(state_dict['state_dict'])

        for batch_idx, sample in enumerate(TestImgLoader):
            image_outputs = test_sample(sample, compute_metrics=False)

            # add left_png.name 切分
            left_name = image_outputs["ori_name"][0]
            save_name = left_name.split("/")[-1]
            save_name = save_name[:-4]

            #  ---save---begin---success
            # save test results
            # output3 = image_outputs["pre"][1]  #
            # output3 = torch.squeeze(output3, 1)
            # np_output3 = output3.detach().cpu().numpy()  #
            # np_output3_4c = np.expand_dims(np_output3, 3)
            # np_output3_3c = np.squeeze(np_output3_4c[0])
            # im_output3 = Image.fromarray(np_output3_3c.astype('uint8'))  #
            # im_output3.save(args.saveresult + '{}_pre_gray.png'.format(save_name))
            # ---save---end---

            # save color pred --success
            output3 = image_outputs["pre"][1]  #
			#np_out3 = lapa_cm[output3[0].cpu().detach().numpy()]  # if lapa
            np_out3 = voc_cm[output3[0].cpu().detach().numpy()]  # if camvid
            im_output3 = Image.fromarray(np_out3, mode='RGB')  # to
            im_output3.save(args.saveresult + '{}_pre_color.png'.format(save_name))
            # ---save---end---

            # save  GT color
            gt_out = image_outputs["gt"]  # tensor
			#gt_out_color = lapa_cm[gt_out[0].cpu().detach().numpy()]  # if lapa
            gt_out_color = voc_cm[gt_out[0].cpu().detach().numpy()]  # if camvid
            im_gt_out_color = Image.fromarray(gt_out_color, mode='RGB')  #
            im_gt_out_color.save(args.saveresult + '{}_gt_color.png'.format(save_name))

            del image_outputs
            # del output3

        gc.collect()

if __name__ == '__main__':
    if args.mode == 'test':
        test_batch()

max_stars_repo_name: TheDetial/Image-Segmentation

max_issues_repo_name: TheDetial/Image-Segmentation

max_forks_repo_name: TheDetial/Image-Segmentation


text:
#coding:utf-8
'''
计算文章的diversity

diversity = balance * variaty * diaparsity

'''
from basic_config import *
import math
from scipy import stats

def cal_paper_div():

    subj_refnum = json.loads(open('data/fos1_fos2_refnum.json').read())

    citnum_total = defaultdict(int)
    for subj in subj_refnum.keys():
        for subj2 in subj_refnum[subj].keys():
            citnum_total[subj2] += subj_refnum[subj][subj2]

    paper_subj_citnum = defaultdict(lambda:defaultdict(int))
    total_subsjs = set([])

    for line in open("data/paper_ITR.csv"):
        line = line.strip()

        if line.startswith('pid'):
            continue

        pid, subj, osubj, func, I0, It, ITR = line.split(',')

        paper_subj_citnum[pid][osubj] = It

        total_subsjs.add(subj)

    total_num = len(total_subsjs)
    paper_DIV = {}
    # 计算每一篇论文的diversity
    for pid in paper_subj_citnum.keys():

        subjs = list(paper_subj_citnum[pid].keys())
        subj_num = list(paper_subj_citnum[pid].values())

        variety = len(subjs)/float(total_num)

        balance = gini(subj_num)

        disparsity = cal_disparsity(subjs, subj_refnum, citnum_total)

        diversity = balance * variety * disparsity

        paper_DIV[pid] = diversity

    open('data/paper_DIV.json','w').write(json.dumps(paper_DIV))
    logging.info('DONE, data saved to data/paper_DIV.json')

def cal_relations():

    paper_DIV = json.loads(open('data/paper_DIV.json').read())

    pid_ITRs = defaultdict(list)

    for line in open("data/paper_ITR.csv"):
        line = line.strip()

        if line.startswith('pid'):
            continue

        pid, subj, osubj, func, I0, It, ITR = line.split(',')

        if subj!=osubj:
            pid_ITRs[pid].append(float(ITR))

    DIVs = []
    maxITRs = []

    for pid in pid_ITRs.keys():

        DIV = paper_DIV.get(pid,None)

        if DIV is None or math.isnan(DIV):
            continue

        ITRs = pid_ITRs[pid]
        max_ITR = np.max(ITRs)

        DIVs.append(DIV)
        maxITRs.append(max_ITR)

    # 计算相似度
    rho, pval = stats.spearmanr(DIVs, maxITRs)

    plt.figure(figsize=(5,4))
    data = {'TDI': maxITRs, 'DIV': DIVs}
    # sns.lineplot(data={'TDI':maxITRs,'DIV':DIVs},x='TDI',y='DIV',label='spearman coef:{:.2f},p-Value:{:.2f}'.format(rho,pval))

    # sns.histplot(
    #     data = data , x="TDI", y="DIV",
    #     bins=50, discrete=(False, False), log_scale=(True, False), label='spearman coef:{:.2f},p-Value:{:.2f}'.format(rho, pval)
    # )

    sns.scatterplot(data=data, x="TDI", y="DIV")

    plt.xscale('log')

    plt.title('spearman coef:{:.2f},p-Value:{:.2f}'.format(rho, pval))

    plt.legend()

    plt.tight_layout()

    plt.savefig('fig/relations2.png',dpi=400)
    logging.info('DONE')

def cal_disparsity(subj_set, subj_refnum, citnum_total):

    all_dij = []
    for i in range(len(subj_set)):

        for j in range(i+1, len(subj_set)):

            if i == j:
                continue

            subj1 = subj_set[i]
            subj2 = subj_set[j]

            dij = 1-Sij(subj1, subj2, subj_refnum, citnum_total)

            all_dij.append(dij)

    return np.mean(all_dij)

def Rij(subj1, subj2, subj_refnum):

    return int(subj_refnum[subj1].get(subj2, 0))

def Sij(subj1, subj2, subj_refnum, citnum_total):

    return (Rij(subj1, subj2, subj_refnum)+Rij(subj2, subj1, subj_refnum))/np.sqrt((total_refnum_of_subj(subj1, subj_refnum)+total_citnum_of_subj(subj1, citnum_total))*(total_refnum_of_subj(subj2, subj_refnum)+total_citnum_of_subj(subj2, citnum_total)))

def total_refnum_of_subj(subj, subj_refnum):

    return np.sum([int(i) for i in subj_refnum[subj].values()])

def total_citnum_of_subj(subj, citnum_total):
    return citnum_total[subj]

def gini(array):
    """Calculate the Gini coefficient of a numpy array."""
    # based on bottom eq:
    # http://www.statsdirect.com/help/generatedimages/equations/equation154.svg
    # from:
    # http://www.statsdirect.com/help/default.htm#nonparametric_methods/gini.htm
    # All values are treated equally, arrays must be 1d:
    array = np.array([float(i) for i in array])
    # array = array.flatten()
    if np.amin(array) < 0:
        # Values cannot be negative:
        array -= np.amin(array)
    # Values cannot be 0:
    array += 0.0000001
    # Values must be sorted:
    array = np.sort(array)
    # Index per array element:
    index = np.arange(1, array.shape[0] + 1)
    # Number of array elements:
    n = array.shape[0]
    # Gini coefficient:
    return ((np.sum((2 * index - n - 1) * array)) / (n * np.sum(array)))

def top_paper_info():

    paper_DIV = json.loads(open('data/paper_DIV.json').read())

    top_20_DIV = set(sorted(paper_DIV.keys(),key= lambda x:float(paper_DIV[x]),reverse=True)[:100])

    paper_ITR = {}

    for line in open("data/paper_ITR.csv"):
        line = line.strip()

        if line.startswith('pid'):
            continue

        pid, subj, osubj, func, I0, It, ITR = line.split(',')

        if subj != osubj:
            paper_ITR[pid] = max([float(paper_ITR.get(pid, 0)), float(ITR)])

    TOP_20_ITR = set(sorted(paper_ITR.keys(), key=lambda x: float(
        paper_ITR[x]), reverse=True)[:100])

    logging.info(f'length of DIV: {len(top_20_DIV)},length of ITR:{len(TOP_20_ITR)}')

    query_op = dbop()

    sql = "select A.paper_id,A.year,A.paper_title,A.original_venue,C.display_name from mag_core.papers as A, mag_core.paper_author_affiliations as B, mag_core.authors as C where A.paper_id = B.paper_id and B.author_id = C.author_id"
    lines = ["paper_id, year, paper_title, original_venue"]
    ITR_lines = ["paper_id, year, paper_title, original_venue"]
    for paper_id, year, paper_title, original_venue,display_name in query_op.query_database(sql):
        if paper_id in top_20_DIV:
            lines.append(f"{paper_id},{year},{paper_title},{original_venue},{display_name}")

        if paper_id in TOP_20_ITR:
            ITR_lines.append(
                f"{paper_id},{year},{paper_title},{original_venue},{display_name}")

    open('data/top_20_DIV.csv','w').write('\n'.join(lines))
    logging.info('data saved to data/top_20_DIV.csv.')

    open('data/top_20_ITR.csv', 'w').write('\n'.join(ITR_lines))
    logging.info('data saved to data/top_20_ITR.csv.')

if __name__=='__main__':
    # cal_paper_div()

    # cal_relations()
    top_paper_info()

ext: py

reason: from scipy


text:
import torch
import numpy as np
from skimage.measure import block_reduce
###################################
## Speech handling fns
## Elizabeth Nielsen
## enielsen@ed.ac.uk
###################################

def pad_tok_frames(tok_frames,padded_len = 100):
  if padded_len >= tok_frames.shape[-1]:
    padded = np.pad(tok_frames,((0,0),(0,padded_len-tok_frames.shape[-1])))
  else:
    subsampled = tok_frames
    while subsampled.shape[-1] > padded_len:
      subsampled = subsampled[:,::2]
    padded = np.pad(subsampled,((0,0),(0,padded_len-subsampled.shape[-1])))
  return padded

def pad_batch_frames(feats,back_context,for_context,device):
  batched = []
  num_channel_in = 1
  max_toks = max([len(sent_feats) for sent_feats in feats])
  zero_tok = np.zeros(feats[0][0].shape)
  for i,sent in enumerate(feats):
    while len(feats[i]) < max_toks:
      feats[i].append(zero_tok)
  batched = []
  for i in range(max_toks):
    all_tok = np.stack([sent[i] for sent in feats],axis=0)
    bat_shape = all_tok.shape
    all_tok = torch.tensor(all_tok,requires_grad=True).type(torch.float).view(bat_shape[0],num_channel_in,bat_shape[1],bat_shape[2])
    if device:
      all_tok = all_tok.to(device)
    batched.append(all_tok)
  return batched

def trim_repeated_frame(token,trim_front=True):
  if trim_front:
    return token[:,1:]
  else:
    return token[:,:-1]

def frame_batch_w_context(batch_feats,back_context,for_context,context_strat,tok_frame_len):
  feats_w_context = []
  for sent in batch_feats:
    sent_w_context = []
    for curr_idx,tok in enumerate(sent):
      if context_strat == 'all' or context_strat == 'all_boundary':
        start_idx = max(0,curr_idx-back_context)
        end_idx = min(len(sent)-1,curr_idx+for_context)
        w_context = sent[start_idx:end_idx+1]

        if len(w_context) > 1:
          new_w_context = [w_context[0]]
          for tok in w_context[1:]:
            new_w_context.append(trim_repeated_frame(tok))
          w_context = new_w_context
        if context_strat == 'all_boundary':
          boundary = np.zeros((w_context[0].shape[0],2))
          new_w_context = []
          for tok in w_context:
            new_w_context.append(tok)
            new_w_context.append(boundary)
          w_context = new_w_context
        w_context = np.concatenate(w_context,axis=1)
        w_context = pad_tok_frames(w_context,tok_frame_len)
        sent_w_context.append(w_context)

      elif context_strat == 'pool':
        start_idx = max(0,curr_idx-back_context)
        end_idx = min(len(sent)-1,curr_idx+for_context)
        if back_context > 0:
          prev_toks = sent[start_idx:curr_idx]
          block_size = (1,max(tok.shape[-1],1))
          prev_toks = [block_reduce(tok,block_size=(1,tok.shape[-1]),func=np.mean) for tok in prev_toks]
        else:
          prev_toks = []
        if for_context > 0:
          next_toks = sent[curr_idx+1:end_idx+1]
          block_size = (1,max(tok.shape[-1],1))
          next_toks = [block_reduce(tok,block_size=block_size,func=np.mean) for tok in next_toks]
        else:
          next_toks = []
        w_context = np.concatenate(prev_toks+[tok]+next_toks,axis=1)
        w_context = pad_tok_frames(w_context,tok_frame_len)
        sent_w_context.append(w_context)
      elif context_strat == 'leading':
        N = 5 # NUMBER OF LEADING FRAMES
        assert for_context == 1
        assert back_context == 0
        if curr_idx < len(sent)-1:
          leading_frames = sent[curr_idx+1][:,:N]
          w_context = np.concatenate((tok,leading_frames),axis=1)
        else:
          w_context = tok
        w_context = pad_tok_frames(w_context,tok_frame_len)
        sent_w_context.append(w_context)

    feats_w_context.append(sent_w_context)

  return feats_w_context

def bat_frame_feats(speech_feats,pitch=True,fbank=True,tok_frame_len=100,device=None,back_context=0,for_context=0,context_strat='all'):
  if pitch:
    pitch_feats = [feats['pitch'] for feats in speech_feats]
    feats_w_context = []
    if back_context == 0 and for_context == 0:
      pitch_batched = [[pad_tok_frames(tok,tok_frame_len) for tok in feats] for feats in pitch_feats]
    else:
      pitch_batched = frame_batch_w_context(pitch_feats,back_context,for_context,context_strat,tok_frame_len)
    pitch_batched = pad_batch_frames(pitch_batched,back_context,for_context,device)
  if fbank:
    fbank_feats = [feats['fbank'] for feats in speech_feats]
    feats_w_context = []
    if back_context == 0 and for_context == 0:
      fbank_batched  = [[pad_tok_frames(tok,tok_frame_len) for tok in feats] for feats in fbank_feats]
    else:
      fbank_batched = frame_batch_w_context(fbank_feats,back_context,for_context,context_strat,tok_frame_len)
    fbank_batched = pad_batch_frames(fbank_batched,back_context,for_context,device)
  if fbank and pitch:
    frame_batched = [torch.cat([pit,fb],dim=2) for pit,fb in zip(pitch_batched,fbank_batched)]
  elif fbank:
    frame_batched = fbank_batched
  elif pitch:
    frame_batched = pitch_batched

  return frame_batched

def bat_pause_feats(speech_feats,back_context,for_context,device):
  max_num_toks = 0
  pause_batched = []
  back = [[] for _ in range(back_context)]
  forward = [[] for _ in range(for_context)]
  for feats in speech_feats:
    max_num_toks = max(max_num_toks,len(feats['pause']))
    pause_batched.append(np.array(feats['pause']))
    #"""#EKN
    if back_context > 0:
      for i in range(1,back_context+1):
        padding = i if i <= max_num_toks else max_num_toks
        back_shifted = [0]*padding + feats['pause'][:-i]
        back[-i].append(np.array(back_shifted))
    if for_context > 0:
      for i in range(1,for_context+1):
        padding = i if i <= max_num_toks else max_num_toks
        for_shifted = feats['pause'][i:] + [0]*padding
        forward[i-1].append(np.array(for_shifted))
    #""" #EKN
  pause_batched = torch.tensor(np.stack([np.pad(sent,((0,max_num_toks-sent.shape[0])),constant_values=0) for sent in pause_batched])).type(torch.int)
  #""" #EKN
  if back_context > 0:
    back = [torch.tensor(np.stack([np.pad(sent,((0,max_num_toks-sent.shape[0])),constant_values=0) for sent in shift])).type(torch.int).to(device) for shift in back]
  if for_context > 0:
    forward = [torch.tensor(np.stack([np.pad(sent,((0,max_num_toks-sent.shape[0])),constant_values=0) for sent in shift])).type(torch.int).to(device) for shift in forward]

  if back_context == 0 and for_context == 0:
    return pause_batched.to(device)
  else:
    return pause_batched.to(device),back,forward
  #""" #EKN
  return pause_batched.to(device)

def bat_dur_feats(speech_feats,back_context,for_context,device=None):
  max_num_toks = 0
  dur_batched = []
  for feats in speech_feats:
    max_num_toks = max(max_num_toks,len(feats['dur']))
    dur_batched.append(np.stack(feats['dur']))

  dur_batched = [np.pad(sent,((0,max_num_toks-sent.shape[0]),(0,0)),constant_values=0) for sent in dur_batched]
  dur_batched = torch.tensor(np.stack(dur_batched)).type(torch.float)
  #""" #EKN
  back = []
  forward = []
  if back_context > 0 or for_context > 0:
    if back_context > 0:
      back = []
      for i in range(1,back_context+1):
        padding = i if i <= max_num_toks else max_num_toks
        shifted = dur_batched[:,:-i,:]
        shifted = np.pad(shifted,((0,0),(padding,0),(0,0)),constant_values=0)
        back = [torch.tensor(shifted).type(torch.float).to(device)] + back
    if for_context > 0:
      forward = []
      for i in range(1,for_context+1):
        padding = i if i <= max_num_toks else max_num_toks
        shifted = dur_batched[:,i:,:]
        shifted = np.pad(shifted,((0,0),(0,padding),(0,0)),constant_values=0)
        forward.append(torch.tensor(shifted).type(torch.float).to(device))
    #dur_batched = np.concatenate(back+[dur_batched]+forward,axis=-1)
    return dur_batched.to(device),back,forward
  #""" #EKN
  #dur_batched = torch.tensor(dur_batched)
  return dur_batched.to(device)

def get_sp_feats(args,speech_feats,device,speech_feat_types=[],tok_frame_len=None,include_id=False):

  if not speech_feat_types:
    speech_feat_types = args.speech_feat_types
  if not tok_frame_len:
    tok_frame_len = args.tok_frame_len

  try:
    back_context = args.back_context
    for_context = args.for_context
    context_strat = args.context_strat
  except:
    back_context = 0
    for_context = 0
    context_strat = 0

  if speech_feat_types:
    if 'pause' in speech_feat_types:
      pause = bat_pause_feats(speech_feats,back_context,for_context,device)
    else:
      pause = None
    if 'dur' in speech_feat_types:
      dur = bat_dur_feats(speech_feats,back_context,for_context,device)
    else:
      dur = None
    if 'pitch' and 'fbank' in speech_feat_types:
      frames = bat_frame_feats(speech_feats,pitch=True,fbank=True,tok_frame_len=tok_frame_len,device=device,back_context=back_context,for_context=for_context,context_strat=context_strat)
    elif 'pitch' in speech_feat_types:
      frames = bat_frame_feats(speech_feats,pitch=True,fbank=False,tok_frame_len=tok_frame_len,device=device,back_context=back_context,for_context=for_context,context_strat=context_strat)
    elif 'fbank' in speech_feat_types:
      frames = bat_frame_feats(speech_feats,pitch=False,fbank=True,tok_frame_len=tok_frame_len,device=device,back_context=back_context,for_context=for_context,context_strat=context_strat)
    else:
      frames = None
  else:
      pause = dur = frames = None
  if include_id:
    ids = []
    for feat in speech_feats:
      ids.append(feat['idnum'])
    return pause,dur,frames,ids
  else:
    return pause,dur,frames

max_stars_repo_name: ekayen/rnng-pytorch

max_issues_repo_name: ekayen/rnng-pytorch

max_forks_repo_name: ekayen/rnng-pytorch

reason: import numpy


text:
# MIT License
#
# Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import math

import numpy as np

class Box(object):
    def __init__(
        self,
        width=1,
        height=1,
        length=1,
        centerX=0,
        centerY=0,
        centerZ=0,
        yaw=0.0,
        pitch=0.0,
        roll=0.0,
        translationX=0,
        translationY=0,
        translationZ=0,
    ):
        # In webots length is in z-axis, width is in x-axis and height is in y-axis
        # Center is the rotation center for the box
        #  -> in webots, this should be the rear axle location relative to the center of the box
        #  -> center is the vector from the true center of the box to the rotation center of the box
        # In webots yaw is CC around the y-axis!
        # In webots pitch is CC around the z-axis!
        # In webots roll is CC around the x-axis!
        # NOTE: this geometry class applies a translation to get the center of rotation,
        #  rotates the box and then applies a global translation to move the rectangle in a global coordinate system
        self.dimensions = np.array([width, height, length])
        self.center = np.array([centerX, centerY, centerZ])
        self.translation = np.array([translationX, translationY, translationZ])
        self.yaw = yaw
        self.pitch = pitch
        self.roll = roll
        self.unrotatedegocorners = self._getunrotatedegocorners()
        self.rotation = self.getyawrollpitchrotation(self.yaw, self.pitch, self.roll)
        # The transpose is the inverse rotation matrix
        self.reverserotation = np.transpose(self.rotation)
        self.corners = self.getcorners()

    def __str__(self):
        return "[({},{},{}), center=({},{},{}), rotation=({},{},{}), translation=({},{},{})]".format(
            self.dimensions[0],
            self.dimensions[1],
            self.dimensions[2],
            self.center[0],
            self.center[1],
            self.center[2],
            self.yaw,
            self.pitch,
            self.roll,
            self.translation[0],
            self.translation[1],
            self.translation[2],
        )

    def getyawrollpitchrotation(self, yaw, pitch, roll):
        sin_p = math.sin(pitch)
        cos_p = math.cos(pitch)
        sin_y = math.sin(yaw)
        cos_y = math.cos(yaw)
        sin_r = math.sin(roll)
        cos_r = math.cos(roll)
        return np.array(
            [
                [
                    cos_p * cos_y,
                    cos_p * sin_y * sin_r - sin_p * cos_r,
                    cos_p * sin_y * cos_r + sin_p * sin_r,
                ],
                [
                    sin_p * cos_y,
                    sin_p * sin_y * sin_r + cos_p * cos_r,
                    sin_p * sin_y * cos_r - cos_p * sin_r,
                ],
                [-sin_y, cos_y * sin_r, cos_y * cos_r],
            ]
        )

    def _getunrotatedegocorners(self):
        x_diff1, y_diff1, z_diff1 = -self.dimensions / 2.0 - self.center
        x_diff2, y_diff2, z_diff2 = self.dimensions / 2.0 - self.center
        x1, y1, z1 = [
            min(x_diff1, x_diff2),
            min(y_diff1, y_diff2),
            min(z_diff1, z_diff2),
        ]
        x2, y2, z2 = [
            max(x_diff1, x_diff2),
            max(y_diff1, y_diff2),
            max(z_diff1, z_diff2),
        ]
        corners = np.array(
            [
                [x1, y1, z1],
                [x1, y1, z2],
                [x1, y2, z1],
                [x1, y2, z2],
                [x2, y1, z1],
                [x2, y1, z2],
                [x2, y2, z1],
                [x2, y2, z2],
            ]
        )
        return corners

    def getcorners(self):
        corners = self._getunrotatedegocorners()

        if abs(self.yaw) > 1e-30 or abs(self.pitch) > 1e-30 or abs(self.roll) > 1e-30:
            corners = np.inner(corners, self.rotation)

        corners += self.translation

        return corners

    def getvolume(self):
        return np.prod(self.dimensions)

    def containspoint(self, point):
        return self.containspoints(np.array([point]))

    def containspoints(self, points):
        # 1.) Rotate the point around the center
        # 2.) Check to see if the points lie inside the co-linear rectangle
        N, d = points.shape
        ego_points = points - self.translation
        if abs(self.yaw) > 1e-30 or abs(self.pitch) > 1e-30 or abs(self.roll) > 1e-30:
            rotated_points = np.inner(ego_points, self.reverserotation)
        else:
            rotated_points = ego_points
        low_corner = self.unrotatedegocorners[0]
        high_corner = self.unrotatedegocorners[7]

        # This is why we rotate the points rather than the box -> simpler to check if the box is
        #  co-linear with the axis of the local coordinate system
        return np.all(
            np.logical_and(
                (high_corner >= rotated_points), (rotated_points >= low_corner)
            ),
            axis=1,
        )

    # Note to be used externly
    def _unrotated_containspoints(self, unrotated_points):
        low_corner = self.unrotatedegocorners[0]
        high_corner = self.unrotatedegocorners[7]

        # This is why we rotate the points rather than the box -> simpler to check if the box is
        #  co-linear with the axis of the local coordinate system
        return np.all(
            np.logical_and(
                (high_corner >= unrotated_points), (unrotated_points >= low_corner)
            ),
            axis=1,
        )

    def _getnormals(self):
        # Just need three normals of the unrotated box
        p1, p2, p3, p4, p5, p6, p7, p8 = self.unrotatedegocorners
        xn = np.cross(p3 - p1, p2 - p1)
        yn = np.cross(p2 - p1, p5 - p1)
        zn = np.cross(p5 - p1, p3 - p1)

        return xn, yn, zn

    def getlines(self):
        p1, p2, p3, p4, p5, p6, p7, p8 = self.corners
        start_points = np.array([p1, p1, p1, p2, p2, p3, p3, p4, p5, p5, p6, p7])
        end_points = np.array([p2, p3, p5, p4, p6, p7, p4, p8, p6, p7, p8, p8])
        return start_points, end_points

    def intersects(self, box):
        # NOTE: the order of the points in self.corners and self.unrotatedegocorners must not change!

        # Calculates whether any corners of rect fall within self
        start1, end1 = box.getlines()
        intersect1 = self.intersectswithlines(points=start1, end_points=end1)

        # Also need to see if any of the corners of self fall in rect
        start2, end2 = self.getlines()
        intersect2 = box.intersectswithlines(points=start2, end_points=end2)

        return np.any(np.concatenate((intersect1, intersect2)))

    # Calculates intersection point between two parallel planes with norm and defined by points 1 and 2 respectively
    # norm must be the outer norm for plane1 defined by point pts_on_plane1
    def _get_line_intersect_with_planes_3d(
        self, points, directions, norm, pts_on_plane1, pts_on_plane2
    ):
        r = directions
        n1 = norm
        n2 = -norm
        d1 = -np.inner(n1, pts_on_plane1[0])
        d2 = -np.inner(n2, pts_on_plane2[0])

        r_n1 = np.inner(r, n1)
        r_n2 = np.inner(r, n2)
        n1_px = np.inner(n1, points) + d1
        n2_px = np.inner(n2, points) + d2
        n1_p = np.inner(n1, points)
        n2_p = np.inner(n2, points)

        t1 = np.zeros(len(points))
        t2 = np.zeros(len(points))

        # Check for parallel
        z1 = np.abs(r_n1) < 1e-20
        z2 = np.abs(r_n2) < 1e-20
        nz1 = np.logical_not(z1)
        nz2 = np.logical_not(z2)
        # Check for points on plane
        on1 = np.abs(n1_px) < 1e-20
        on2 = np.abs(n2_px) < 1e-20
        non1 = np.logical_not(on1)
        non2 = np.logical_not(on2)

        # All points that are not on the plane but are perpendicular -> inf
        t1[np.logical_and(z1, non1)] = -np.inf
        t2[np.logical_and(z2, non2)] = np.inf

        # All points not perpendicular and not on the plane
        nz_non1 = np.logical_and(nz1, non1)
        nz_non2 = np.logical_and(nz2, non2)
        t1[nz_non1] = -(d1 + n1_p[nz_non1]) / r_n1[nz_non1]
        t2[nz_non2] = -(d2 + n2_p[nz_non2]) / r_n2[nz_non2]

        # Re-order points if necessary
        t = np.stack((t1, t2), axis=1)
        tpos = np.min(t, axis=1)
        tneg = np.max(t, axis=1)

        # print("POS {}\nNEG {}".format(tpos, tneg))

        return tpos, tneg

    # NOTE: directions must be vectors from points (start) to end_points
    def intersectswithlines(self, points, end_points):
        # Method is described here: https://math.stackexchange.com/questions/1477930/does-a-line-intersect-a-box-in-a-3d-space
        rot_points = points - self.translation
        rot_end_points = end_points - self.translation
        if abs(self.yaw) > 1e-30 or abs(self.pitch) > 1e-30 or abs(self.roll) > 1e-30:
            rot_points = np.inner(rot_points, self.reverserotation)
            rot_end_points = np.inner(rot_end_points, self.reverserotation)

        rot_directions = rot_end_points - rot_points

        xn, yn, zn = self._getnormals()

        with np.errstate(divide="ignore"):
            low_xpoints = [self.unrotatedegocorners[0]]
            low_ypoints = [self.unrotatedegocorners[0]]
            low_zpoints = [self.unrotatedegocorners[0]]
            high_xpoints = [self.unrotatedegocorners[7]]
            high_ypoints = [self.unrotatedegocorners[7]]
            high_zpoints = [self.unrotatedegocorners[7]]

            t_xpos, t_xneg = self._get_line_intersect_with_planes_3d(
                rot_points, rot_directions, xn, high_xpoints, low_xpoints
            )
            t_ypos, t_yneg = self._get_line_intersect_with_planes_3d(
                rot_points, rot_directions, yn, high_ypoints, low_ypoints
            )
            t_zpos, t_zneg = self._get_line_intersect_with_planes_3d(
                rot_points, rot_directions, zn, high_zpoints, low_zpoints
            )

        pos_ts = np.stack((t_xpos, t_ypos, t_zpos), axis=1)
        neg_ts = np.stack((t_xneg, t_yneg, t_zneg), axis=1)

        # print("{} {}".format(pos_ts, neg_ts))

        maxpos = np.max(pos_ts, axis=1)
        minneg = np.min(neg_ts, axis=1)

        condition = np.array([False] * len(points))

        start_contains = self._unrotated_containspoints(rot_points)
        end_contains = self._unrotated_containspoints(rot_end_points)
        both = np.logical_and(start_contains, end_contains)
        one = np.logical_xor(start_contains, end_contains)
        none = np.logical_not(np.logical_or(both, one))

        # print("MAX {}; MIN {}".format(maxpos[none], minneg[none]))
        # print("POS {}; NEG {}".format(pos_ts[none], neg_ts[none]))

        # Handle the case where both points are in the box
        condition[both] = True
        # Handle the case where one point is in the box
        condition[one] = np.logical_and(
            maxpos[one] <= minneg[one],
            np.logical_and(maxpos[one] <= 1, minneg[one] >= 0),
        )
        # Handle the case where both points are outside the box
        if np.any(none):
            possibles = np.array([False] * len(points))
            possibles[none] = np.logical_and(
                maxpos[none] <= minneg[none],
                np.logical_and(
                    maxpos[none] >= 0,
                    np.logical_and(
                        minneg[none] <= 1,
                        np.logical_and(maxpos[none] <= 1, minneg[none] >= 0),
                    ),
                ),
            )
            if np.any(possibles):
                none_start_points = rot_points[possibles]
                none_directions = rot_directions[possibles]
                none_surface1 = none_start_points + np.transpose(
                    np.transpose(none_directions) * maxpos[possibles]
                )
                none_surface2 = none_start_points + np.transpose(
                    np.transpose(none_directions) * minneg[possibles]
                )
                # Update any possibles that were potentially true
                possibles[possibles] = np.logical_and(
                    self._unrotated_containspoints(none_surface1),
                    self._unrotated_containspoints(none_surface2),
                )
            condition[none] = possibles[none]

        return condition

# Support library for converting a LIDAR point (where the points are in scan order) to an image
class SphericalCartesianConverter(object):
    def __init__(self, hfov, vfov, width, height):
        # Assume tilt angle is 0!
        # Scan from left to right
        az = np.linspace(
            math.pi + hfov / 2.0, math.pi - hfov / 2.0, width, dtype=np.float32
        )
        # Scan from top layer to bottom layer
        el = np.linspace(vfov / 2.0, -vfov / 2.0, height, dtype=np.float32)
        az_grid, el_grid = np.meshgrid(az, el, sparse=True)
        self.z_factor = np.cos(az_grid) * np.cos(el_grid)
        self.x_factor = np.sin(az_grid) * np.cos(el_grid)
        self.y_factor = np.sin(el_grid)

    def calculate_point_cloud(self, depth):
        # These x, y, z are webots axes!
        z = depth * self.z_factor  # Points behind vehicle
        x = depth * self.x_factor  # Points to right of vehicle
        y = depth * self.y_factor  # Points up

        points = np.stack((x.flatten(), y.flatten(), z.flatten()), axis=-1)
        return points

def filter_points(points, ground_height=0.0, max_distance=99.9):
    distances = np.linalg.norm(points, axis=1)
    filtered_points = points[distances < max_distance]
    return filtered_points[filtered_points[:, 1] > ground_height]

def calculate_point_cloud(depth, hfov, vfov, width, height):
    # Assume tilt angle is 0!
    # Scan from left to right
    az = np.linspace(
        math.pi + hfov / 2.0, math.pi - hfov / 2.0, width, dtype=np.float32
    )
    # Scan from top layer to bottom layer
    el = np.linspace(vfov / 2.0, -vfov / 2.0, height, dtype=np.float32)
    az_grid, el_grid = np.meshgrid(az, el, sparse=True)
    z_factor = np.cos(az_grid) * np.cos(el_grid)
    x_factor = np.sin(az_grid) * np.cos(el_grid)
    y_factor = np.sin(el_grid)

    # These x, y, z are webots axes!
    z = depth * z_factor  # Points behind vehicle
    x = depth * x_factor  # Points to right of vehicle
    y = depth * y_factor  # Points up

    points = np.stack((x.flatten(), y.flatten(), z.flatten()), axis=-1)
    return points

def yaw_rotation(yaw):
    cos_y = math.cos(yaw)
    sin_y = math.sin(yaw)
    return np.array([[cos_y, 0, sin_y], [0, 1, 0], [-sin_y, 0, cos_y]])

def yaw_rotate(yaw, vectors):
    rotation = yaw_rotation(yaw)
    return np.inner(vectors, rotation)

# In webots yaw is CC around the y-axis!
# In webots pitch is CC around the z-axis!
# In webots roll is CC around the x-axis!
def yawrollpitch_rotation(yaw, pitch, roll):
    sin_p = math.sin(pitch)
    cos_p = math.cos(pitch)
    sin_y = math.sin(yaw)
    cos_y = math.cos(yaw)
    sin_r = math.sin(roll)
    cos_r = math.cos(roll)
    return np.array(
        [
            [
                cos_p * cos_y,
                cos_p * sin_y * sin_r - sin_p * cos_r,
                cos_p * sin_y * cos_r + sin_p * sin_r,
            ],
            [
                sin_p * cos_y,
                sin_p * sin_y * sin_r + cos_p * cos_r,
                sin_p * sin_y * cos_r - cos_p * sin_r,
            ],
            [-sin_y, cos_y * sin_r, cos_y * cos_r],
        ]
    )

def rotate(yaw, pitch, roll, vectors):
    rotation = yawrollpitch_rotation(yaw, pitch, roll)
    return np.inner(vectors, rotation)

def visualize_boxes(ax, boxes, markers=None, colors=None, faces_color=None):
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection

    if markers is None:
        markers = ["o"] * len(boxes)
    if colors is None:
        colors = ["red"]
    if faces_color is None:
        faces_color = ["cyan"]

    for box, marker, color, face_color in zip(boxes, markers, colors, faces_color):
        Z = box.corners
        ax.scatter(
            box.corners[:, 0],
            box.corners[:, 1],
            box.corners[:, 2],
            marker=marker,
            color=color,
        )

        # generate list of sides' polygons of our pyramid
        verts = [
            [Z[0], Z[1], Z[3], Z[2]],
            [Z[4], Z[5], Z[7], Z[6]],
            [Z[0], Z[1], Z[5], Z[4]],
            [Z[2], Z[3], Z[7], Z[6]],
            [Z[1], Z[5], Z[7], Z[3]],
            [Z[4], Z[0], Z[2], Z[6]],
        ]

        # plot sides
        ax.add_collection3d(
            Poly3DCollection(
                verts, facecolors=face_color, linewidths=0.5, edgecolors="k", alpha=0.5
            )
        )
    ax.set_zlim(0, 50)
    ax.set_xlim(50, 150)
    ax.set_ylim(50, 150)

    return ax

max_stars_repo_name: isgeles/SMARTS

max_issues_repo_name: isgeles/SMARTS

max_forks_repo_name: isgeles/SMARTS


text:
#!/usr/bin/env python

# Simulation + plotting requires a robot, visualizer and world
from simulator import Robot, Visualizer, World

# Supported resampling methods (resampling algorithm enum for SIR)
from core.resampling import ResamplingAlgorithms

# Load variables
from shared_simulation_settings import *

# Particle filters that will be compared
from core.particle_filters import AdaptiveParticleFilterKld, ParticleFilterSIR

# For showing plots (plt.show())
import matplotlib.pyplot as plt

import numpy as np
import copy

if __name__ == '__main__':
    """
    This file demonstrates the difference between the SIR particle filter that has a constant number of particles and
    the adaptive particle filter that varies the number of particles on the fly. Afterwards, the number of particles
    for both particle filters are plotted over time together with the estimation error of the robot's x-position. The
    results show that the adaptive particle filter achieves a similar estimation accuracy with less particles once the
    particles have converged.
    """

    print("Starting adaptive particle filter demo.")

    ##
    # Set simulated world and visualization properties
    ##
    world = World(world_size_x, world_size_y, landmark_positions)
    visualizer = Visualizer()
    num_time_steps = 50

    # Initialize simulated robot
    robot = Robot(x=world.x_max * 0.75,
                  y=world.y_max / 5.0,
                  theta=3.14 / 2.0,
                  std_forward=true_robot_motion_forward_std,
                  std_turn=true_robot_motion_turn_std,
                  std_meas_distance=true_robot_meas_noise_distance_std,
                  std_meas_angle=true_robot_meas_noise_angle_std)

    ##
    # Particle filter settings
    # The process and measurement model noise is not equal to true noise.
    ##

    # Number of particles
    number_of_particles = 750

    # Initialize particle filter

    # Set resampling algorithm used (where applicable)
    resampling_algorithm = ResamplingAlgorithms.MULTINOMIAL

    # Process model noise (values used in the paper)
    motion_model_forward_std = 0.20
    motion_model_turn_std = 0.05
    process_noise = [motion_model_forward_std, motion_model_turn_std]

    # Initialize SIR particle filter
    particle_filter_sir = ParticleFilterSIR(
        number_of_particles,
        pf_state_limits,
        process_noise,
        measurement_noise,
        resampling_algorithm)
    particle_filter_sir.initialize_particles_uniform()

    # Adaptive particle filter specific settings (see AdaptiveParticleFilterKld constructor for more extensive
    # documentation
    resolutions_grid = [0.2, 0.2, 0.3]
    epsilon = 0.15
    upper_quantile = 3
    min_number_of_particles = 50
    max_number_of_particles = 2e4

    # Initialize adaptive particle filter with the same set of particles
    adaptive_particle_filter_kld = AdaptiveParticleFilterKld(
        number_of_particles,
        pf_state_limits,
        process_noise,
        measurement_noise,
        resolutions_grid,
        epsilon,
        upper_quantile,
        min_number_of_particles,
        max_number_of_particles)
    adaptive_particle_filter_kld.set_particles(copy.deepcopy(particle_filter_sir.particles))

    ##
    # Start simulation
    ##
    errors_kld = []
    npar_kld = []
    errors_sir = []
    npar_sir = []
    for i in range(num_time_steps):

        # Simulate robot motion (required motion will not exactly be achieved)
        robot.move(desired_distance=robot_setpoint_motion_forward,
                   desired_rotation=robot_setpoint_motion_turn,
                   world=world)

        # Simulate measurement
        measurements = robot.measure(world)

        # Real robot pose
        robot_pose = np.array([robot.x, robot.y, robot.theta])

        # Update adaptive particle filter KLD
        adaptive_particle_filter_kld.update(robot_forward_motion=robot_setpoint_motion_forward,
                                            robot_angular_motion=robot_setpoint_motion_turn,
                                            measurements=measurements,
                                            landmarks=world.landmarks)
        errors_kld.append(robot_pose - np.asarray(adaptive_particle_filter_kld.get_average_state()))
        npar_kld.append(len(adaptive_particle_filter_kld.particles))

        # Update SIR particle filter
        particle_filter_sir.update(robot_forward_motion=robot_setpoint_motion_forward,
                                   robot_angular_motion=robot_setpoint_motion_turn,
                                   measurements=measurements,
                                   landmarks=world.landmarks)
        errors_sir.append(robot_pose - np.asarray(particle_filter_sir.get_average_state()))
        npar_sir.append(len(particle_filter_sir.particles))

    print(npar_kld)

    # Show results
    fontSize = 18
    plt.rcParams.update({'font.size': fontSize})
    plt.subplot(211)
    l1, = plt.plot([error[0] for error in errors_sir], 'k-')
    l2, = plt.plot([error[0] for error in errors_kld], 'r--')
    l1.set_label('Standard')
    l2.set_label('Adaptive')
    plt.legend()
    plt.ylabel('Error x-position [m]')
    plt.subplot(212)
    plt.plot([npar for npar in npar_sir], 'k-')
    plt.plot([npar for npar in npar_kld], 'r--')
    plt.yscale('symlog')
    plt.legend()
    plt.xlabel('Time index')
    plt.ylabel('Number of particles')
    plt.show()

max_stars_repo_name: bsugerman/particle-filter-tutorial

max_issues_repo_name: bsugerman/particle-filter-tutorial

max_forks_repo_name: bsugerman/particle-filter-tutorial


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

# In[1]:

import pandas
import numpy as np

# In[2]:

dataset = pandas.read_csv("iris.csv")
dataset = dataset.sample(frac=1)

# In[3]:

#Getting the input Data.
X = dataset[["sepal_length","sepal_width","petal_length","petal_width"]]
print(X.head())
X = np.array(X)
print(f"\nshape of X is {X.shape}")

# In[4]:

#Getting the Classes.
Y = dataset["species"]
print(Y.head())
Y = np.array(Y).reshape((150,1))
print(f"\nShape of Y is {Y.shape}")

# In[5]:

#Pre-Processing the output Classes.
Y[Y=="Iris-setosa"] = 0
Y[Y=="Iris-versicolor"] = 1
Y[Y=="Iris-virginica"] = 2

# In[6]:

print(f"Shape of Y after Pre-Processing is {Y.shape}\n")
print(np.squeeze(Y))

# In[7]:

from sklearn import preprocessing

# In[8]:

#One Hot Encoding Y.
one_hot = preprocessing.OneHotEncoder()
O_e =  one_hot.fit(Y)
Y_e = O_e.transform(Y)
Y = Y_e.toarray()
print(f"Shape of Y is {Y.shape}")

# In[9]:

import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout

# In[10]:

model = Sequential()
model.add(Dense(10,input_shape=(4,),activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3))

# In[11]:

predictions = model(X).numpy()

# In[12]:

#Initial Predictions
Y_beta = tf.nn.softmax(predictions).numpy()
print(Y_beta[:5])
print(np.sum(Y_beta[:5]))
Y_beta = np.argmax(Y_beta,axis = 1)
print(Y_beta[:5])

# In[13]:

lossSoft = tf.keras.losses.CategoricalCrossentropy(from_logits=True)

# In[14]:

print(lossSoft(Y[:5],predictions[:5]).numpy())

# In[15]:

X_train =  X[:-50]
Y_train = Y[:-50]
X_test = X[100:-1]
Y_test = Y[100:-1]
model.compile(optimizer = "adam",loss=lossSoft,metrics=["accuracy"])

# In[21]:

model.fit(X,Y,epochs=10)

# In[22]:

probability_model = tf.keras.Sequential([
  model,
  tf.keras.layers.Softmax()
])

# In[23]:

print(np.argmax(probability_model(X[:100]).numpy(),axis=1))
print(Y[:100])

# In[ ]:


text:
"""
Test for file IO
"""
import numpy as np
from bioptim import Solver

from .utils import TestUtils

def test_cyclic_nmpc():
    def update_functions(_nmpc, cycle_idx, _sol):
        return cycle_idx < n_cycles  # True if there are still some cycle to perform

    bioptim_folder = TestUtils.bioptim_folder()
    nmpc_module = TestUtils.load_module(bioptim_folder + "/examples/moving_horizon_estimation/cyclic_nmpc.py")

    n_cycles = 3
    cycle_len = 20
    nmpc = nmpc_module.prepare_nmpc(
        model_path=bioptim_folder + "/examples/moving_horizon_estimation/models/arm2.bioMod",
        cycle_len=cycle_len,
        cycle_duration=1,
        max_torque=50,
    )
    sol = nmpc.solve(update_functions, solver=Solver.IPOPT())

    # Check some of the results
    states, controls = sol.states, sol.controls
    q, qdot, tau = states["q"], states["qdot"], controls["tau"]

    # initial and final position
    np.testing.assert_equal(q.shape, (3, n_cycles * cycle_len))
    np.testing.assert_almost_equal(q[:, 0], np.array((-3.14159265, 0.12979378, 2.77623291)))
    np.testing.assert_almost_equal(q[:, -1], np.array((2.82743339, 0.63193395, 2.68235056)))

    # initial and final velocities
    np.testing.assert_almost_equal(qdot[:, 0], np.array((6.28268908, -11.63289399, 0.37215021)))
    np.testing.assert_almost_equal(qdot[:, -1], np.array((6.28368154, -7.73180135, 3.56900657)))

    # initial and final controls
    np.testing.assert_almost_equal(tau[:, 0], np.array((0.01984925, 17.53758229, -1.92204945)))
    np.testing.assert_almost_equal(tau[:, -2], np.array((0.01984925, -3.09892348, 0.23160067)), decimal=6)

def test_multi_cyclic_nmpc():
    def update_functions(_nmpc, cycle_idx, _sol):
        return cycle_idx < n_cycles_total  # True if there are still some cycle to perform

    bioptim_folder = TestUtils.bioptim_folder()
    nmpc_module = TestUtils.load_module(bioptim_folder + "/examples/moving_horizon_estimation/multi_cyclic_nmpc.py")

    n_cycles_simultaneous = 2
    n_cycles_to_advance = 1
    n_cycles_total = 3
    cycle_len = 20
    nmpc = nmpc_module.prepare_nmpc(
        model_path=bioptim_folder + "/examples/moving_horizon_estimation/models/arm2.bioMod",
        cycle_len=cycle_len,
        cycle_duration=1,
        n_cycles_simultaneous=n_cycles_simultaneous,
        n_cycles_to_advance=n_cycles_to_advance,
        max_torque=50,
    )
    sol = nmpc.solve(update_functions, solver=Solver.IPOPT(), n_cycles_simultaneous=n_cycles_simultaneous)

    # Check some of the results
    states, controls = sol.states, sol.controls
    q, qdot, tau = states["q"], states["qdot"], controls["tau"]

    # initial and final position
    np.testing.assert_equal(q.shape, (3, n_cycles_total * cycle_len))
    np.testing.assert_almost_equal(q[:, 0], np.array((-12.56637061, 1.04359174, 1.03625065)))
    np.testing.assert_almost_equal(q[:, -1], np.array((-6.59734457, 0.89827771, 1.0842402)))

    # initial and final velocities
    np.testing.assert_almost_equal(qdot[:, 0], np.array((6.28293718, 2.5617072, -0.00942694)))
    np.testing.assert_almost_equal(qdot[:, -1], np.array((6.28343343, 3.28099958, -1.27304428)))

    # initial and final controls
    np.testing.assert_almost_equal(tau[:, 0], np.array((0.00992505, 4.88488618, 2.4400698)))
    np.testing.assert_almost_equal(tau[:, -2], np.array((0.00992505, 9.18387711, 5.22418771)), decimal=6)

max_stars_repo_name: leasanchez/BiorbdOptim

max_issues_repo_name: leasanchez/BiorbdOptim

max_forks_repo_name: leasanchez/BiorbdOptim


text:
import numpy as np
import tensorflow as tf
import time

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="output/model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model FPS on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
start = time.time()
for idx in range(10):
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()
    # The function `get_tensor()` returns a copy of the tensor data.
    # Use `tensor()` in order to get a pointer to the tensor.
    output_data = interpreter.get_tensor(output_details[0]['index'])
    print(output_data.shape)
end = time.time()
print(end-start)

# cap = cv2.VideoCapture('/home/anurag/lightspeed/data/pushup-random.mp4')

# while(cap.isOpened()):
#     ret, frame = cap.read()

#     resized = cv2.resize(frame, (192, 192) , interpolation = cv2.INTER_LINEAR)

#     cv2.imshow('frame', resized)

#     interpreter.set_tensor(input_details[0]['index'], resized)

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

# cap.release()
# cv2.destroyAllWindows()

max_stars_repo_name: anuragmundhada/nn-conversion

max_issues_repo_name: anuragmundhada/nn-conversion

max_forks_repo_name: anuragmundhada/nn-conversion


text:
from __future__ import print_function
import math
import time
import numpy as np
import tensorflow as tf

def create_padding_dimension(n):
    # Halve the padded dimension and round up the first result, and round down the second to create a correct
    # padding dimension.
    return [int(math.ceil(n / 2.0)), int(math.floor(n / 2.0))]

def zero_pad(input, shape):
    #print "input shape %s" % np.array(input.get_shape().as_list())
    #print "target shape %s" % np.array(shape)
    input_shape = input.get_shape().as_list()
    shape_disparity = np.array(shape) - np.array(input_shape)
    for i in range(len(shape)):
        if shape[i] == -1:
            shape_disparity[i] = 0
    padding = list(map(create_padding_dimension, shape_disparity))
    #print "Zero pad: padding %s" % padding
    return tf.pad(input, padding)

def depth_concat(values):
    # This method assumes that all values will be in the shape of (batch, x, y, filters),
    # where batch is equal for all tensors and the rest of the dimensions may vary.

    # The output of this method will have a shape (batch, output_x, output_y, total_feature_maps),
    # where output_x and output_y is the largest dimensions out of all of the input layers,
    # total_feature_maps is the number of feature maps in all concatenated layers.

    # In neural networks, depth can refer to the number of layers in a model, but it can also refer to the number of channels in an 'activation volume'.
    # http://cs231n.github.io/convolutional-networks/#conv states:
    # 'In particular, unlike a regular Neural Network, the layers of a ConvNet have neurons arranged in 3 dimensions: width, height, depth.
    # (Note that the word depth here refers to the third dimension of an activation volume, not to the depth of a full Neural Network, which
    # can refer to the total number of layers in a network.)'.
    #
    # so in this case the depth is the channels index, the fourth dimension.
    batch_index = 0
    x_index = 1
    y_index = 2
    depth_index = 3
    max_x = max(map(lambda a: a.get_shape().as_list()[x_index], values))
    max_y = max(map(lambda a: a.get_shape().as_list()[y_index], values))
    #print max_x, max_y
    batch_size = values[0].get_shape().as_list()[batch_index] # Assume all values have this dimension value
    depth_size = sum(map(lambda a: a.get_shape().as_list()[depth_index], values))
    shape = [batch_size, max_x, max_y, -1]
    padded_values = list(map(lambda a: zero_pad(a, shape), values))
    #print(padded_values)
    return tf.concat(depth_index, padded_values)

def get_filter_output_size(input_size, filter_size, filter_stride):
        return ((input_size - filter_size) // filter_stride) + 1

def get_l2_loss(l2_lambda, layer_weights):
    return l2_lambda * sum(map(tf.nn.l2_loss, layer_weights))


text:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import csv
import numpy as np
import os
import sys

from observations.util import maybe_download_and_extract

def cricketer(path):
  """Lifespans of UK 1st class cricketers born 1840-1960

  Year and birth, lifespan, etc, of British first class cricketers, born
  1840-1960, whose handedness could be determined from information in the
  Who's who of cricketers. The status (alive=0, dead =1), and lifetime or
  lifespan, is for 1992.

  A data frame with 5960 observations on the following 8 variables.

  `left`
      a factor with levels `right` `left`

  `year`
      numeric, year of birth

  `life`
      numeric, lifetime or lifespan to 1992

  `dead`
      numeric (0 = alive (censored), 1 = dead, in 1992)

  `acd`
      numeric (0 = not accidental or not dead, 1 = accidental death)

  `kia`
      numeric (0 = not killed in action, 1 = killed in action)

  `inbed`
      numeric (0 = did not die in bed, 1 = died in bed)

  `cause`
      a factor with levels `alive` `acd` (accidental death) `inbed`
      (died in bed)

  John Aggleton, Martin Bland. Data were collated as described in Aggleton
  et al.

  Args:

    path: str.
      Path to directory which either stores file or otherwise file will
      be downloaded and extracted there.
      Filename is `cricketer.csv`.

  Returns:

    Tuple of np.ndarray `x_train` with 5960 rows and 8 columns and
    dictionary `metadata` of column headers (feature names).
  """
  import pandas as pd
  path = os.path.expanduser(path)
  filename = 'cricketer.csv'
  if not os.path.exists(os.path.join(path, filename)):
    url = 'http://dustintran.com/data/r/DAAG/cricketer.csv'
    maybe_download_and_extract(path, url,
                               save_file_name='cricketer.csv',
                               resume=False)

  data = pd.read_csv(os.path.join(path, filename), index_col=0,
                     parse_dates=True)
  x_train = data.values
  metadata = {'columns': data.columns}
  return x_train, metadata


text:
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 The Pybricks Authors

from ..drv.button import VirtualButtons
from ..drv.battery import VirtualBattery
from ..drv.led import VirtualLed
from ..drv.clock import CountingClock
from ..drv.ioport import VirtualIOPort, PortId, IODeviceTypeId

import numpy
import random
import math

from ..physics.motors import SimpleMotor as SimMotor

class VirtualMotorDriver:
    """
    Virtual motor driver chip implementation, with optionally a (simulated)
    dc motor attached to its output wires.
    """

    def __init__(self, sim_motor):
        self.sim_motor = sim_motor
        self.coasting = True

    def on_coast(self, timestamp: int):
        """
        Called when ``pbdrv_motor_driver_coast()`` is called.
        """
        # Don't simulate repeated coasts. This also ensures we skip
        # simulating motors that are attached but otherwise not used.
        if self.coasting:
            return

        # Simulate the motor up to the current time, with previous actuation.
        time = timestamp / 1000000
        self.sim_motor.simulate(time)

        # Set new actuation signal from now on.
        self.sim_motor.actuate(time, numpy.array([self.sim_motor.COAST_DUTY]))
        self.coasting = True

    def on_set_duty_cycle(self, timestamp: int, duty_cycle: float):
        """
        Called when ``pbdrv_motor_driver_set_duty_cycle()`` is called.
        """

        # Simulate the motor up to the current time, with previous actuation.
        time = timestamp / 1000000
        self.sim_motor.simulate(time)

        # Set new actuation signal from now on.
        self.sim_motor.actuate(time, numpy.array([duty_cycle]))
        self.coasting = False

class VirtualCounter:
    """
    Virtual counter driver implementation, with optionally a (simulated)
    dc motor with rotation sensors attached to it.
    """

    def get_counter_data(self):
        """Get all encoder data"""

        # Return zeros if there is no motor.
        if self.sim_motor is None:
            return 0, 0, 0

        # Get data from model.
        time = self.clock.microseconds / 1e6
        angle, speed = self.sim_motor.get_output_at_time(time, tolerance=2e-3)

        # Get "absolute" angle.
        mod_angle = angle % 360
        abs_angle = mod_angle if mod_angle < 180 else mod_angle - 360

        # Return all values.
        return int(angle - self.initial_angle), int(abs_angle), int(speed)

    def __init__(self, sim_motor, clock):
        # Store references to motor and clock
        self.sim_motor = sim_motor
        self.clock = clock

        # Initialize starting position
        self.initial_angle = 0
        self.initial_angle, _, _ = self.get_counter_data()

    @property
    def count(self):
        """
        Provides the value for ``pbdrv_counter_virtual_get_count()``.
        """
        return self.get_counter_data()[0]

    @property
    def abs_count(self):
        """
        Provides the value for ``pbdrv_counter_virtual_get_abs_count()``.
        """
        return self.get_counter_data()[1]

    @property
    def rate(self):
        """
        Provides the value for ``pbdrv_counter_virtual_get_rate()``.
        """
        return self.get_counter_data()[2]

class Platform:

    # Ports and attached devices.
    PORTS = {
        PortId.A: IODeviceTypeId.TECHNIC_L_ANGULAR_MOTOR,
        PortId.B: IODeviceTypeId.TECHNIC_M_ANGULAR_MOTOR,
        PortId.C: IODeviceTypeId.TECHNIC_M_ANGULAR_MOTOR,
        PortId.D: IODeviceTypeId.TECHNIC_M_ANGULAR_MOTOR,
        PortId.E: IODeviceTypeId.NONE,
        PortId.F: IODeviceTypeId.NONE,
    }

    def on_poll(self, *args):
        # Push clock forward by one tick on each poll.
        self.clock[-1].tick()

    def __init__(self):

        # Initialize devices internal to the hub.
        self.battery = {-1: VirtualBattery()}
        self.button = {-1: VirtualButtons()}
        self.clock = {-1: CountingClock(start=0, fuzz=0)}
        self.led = {0: VirtualLed()}

        # Initialize all ports
        self.ioport = {}
        self.counter = {}
        self.motor_driver = {}
        self.sim_motor = {}

        for i, (port_id, type_id) in enumerate(self.PORTS.items()):
            # Initialize IO Port.
            self.ioport[port_id] = VirtualIOPort(port_id)
            self.ioport[port_id].motor_type_id = type_id

            # Initialize motor simulation model.
            self.sim_motor[i] = None
            if type_id != IODeviceTypeId.NONE:

                # Get current time.
                initial_time = self.clock[-1].microseconds / 1000000

                # Random initial motor angle with zero speed.
                initial_angle = random.randint(-180, 179)
                initial_speed = 0
                initial_state = numpy.array(
                    [
                        math.radians(initial_angle),
                        math.radians(initial_speed),
                    ]
                )

                # Initialize simulated motor.
                self.sim_motor[i] = SimMotor(t0=initial_time, x0=initial_state)

            # Initialize counter and motor drivers with the given motor.
            self.counter[i] = VirtualCounter(self.sim_motor[i], self.clock[-1])
            self.motor_driver[i] = VirtualMotorDriver(self.sim_motor[i])

max_stars_repo_name: Novakasa/pybricks-micropython

max_issues_repo_name: Novakasa/pybricks-micropython

max_forks_repo_name: Novakasa/pybricks-micropython


text:
import numpy as np
import matplotlib.pyplot as plt
import spectrum_utils.plot as sup
import spectrum_utils.spectrum as sus
from pyteomics import mgf
import sys
import argparse

def main(cluster_file,consensus_file):
    with mgf.read(consensus_file) as reader:
        for spectrum_dict in reader:
            peptide_seq = spectrum_dict['params']['title']
            precursor_mz = spectrum_dict['params']['pepmass'][0]
            precursor_charge = spectrum_dict['params']['charge'][0]
            cons_mz = spectrum_dict['m/z array']
            cons_intensity = spectrum_dict['intensity array']
            retention_time = float(spectrum_dict['params']['rtinseconds'])
            break
    cons_spec = sus.MsmsSpectrum(
        peptide_seq, precursor_mz=precursor_mz, precursor_charge=precursor_charge,mz=cons_mz, intensity=cons_intensity,
        retention_time=retention_time, peptide=peptide_seq)
    with mgf.read(cluster_file) as reader:
        for spectrum_dict in reader:
            precursor_mz = spectrum_dict['params']['pepmass'][0]
            precursor_charge = spectrum_dict['params']['charge'][0]
            mz = spectrum_dict['m/z array']
            intensity = spectrum_dict['intensity array']
            retention_time = float(spectrum_dict['params']['rtinseconds'])

        spectrum = sus.MsmsSpectrum(peptide_seq, precursor_mz=precursor_mz, precursor_charge=precursor_charge, mz=mz, intensity=intensity,
                retention_time=retention_time, peptide=peptide_seq)
        # Process the MS/MS spectrum.
        fragment_tol_mass = 10
        fragment_tol_mode = 'ppm'
#    fragment_tol_mass = .5
#    fragment_tol_mode = 'Da'
        spectrum = (spectrum.set_mz_range(min_mz=100, max_mz=1400)
            .remove_precursor_peak(fragment_tol_mass, fragment_tol_mode)
            .filter_intensity(min_intensity=0.05, max_num_peaks=50)
            .scale_intensity('root')
            .annotate_peptide_fragments(fragment_tol_mass, fragment_tol_mode,
                                        ion_types='aby'))
    # Generate theoretical spec
    # Plot the MS/MS spectrum.
    fig, ax = plt.subplots(figsize=(12, 6))
#    sup.spectrum(spectrum, ax=ax)
    sup.mirror(spectrum, tspec, ax=ax)
    plt.show()
    plt.close()

if __name__ == "__main__":
    ap = argparse.ArgumentParser(prog='plot_cluster_vs_consensus', usage='%(prog)s [options]',
        description= "Program plotting mirror plots of cluster members an their representative spectrum")

    # Add the arguments to the parser
    ap.add_argument("cluster_file", nargs=1, help='The mgf file defining the cluster members')
    ap.add_argument("consensus_file", nargs=1, help='The mgf file defining the representative spectrum')
    args = vars(ap.parse_args())

    main(args["cluster_file"][0],args["consensus_file"][0])

max_stars_repo_name: levitsky/specpride

max_issues_repo_name: levitsky/specpride

max_forks_repo_name: levitsky/specpride


text:
#!/home/wli/env python3
# -*- coding: utf-8 -*-
"""
Title: patches near tumor
=========================

Created: 10-31-2019
Python-Version: 3.5, 3.6

Description:
------------

This module is used to extract image patches near the border of tumor
region.

Inputs:
*******

    slide_path = '/raida/wjc/CAMELYON16/training/tumor'
    anno_path = '/raida/wjc/CAMELYON16/training/lesion_annotations'
    mask_dir = '/raida/wjc/CAMELYON16/training/masking'

Output:
*******

    patch_near_tumor_dir = '/raidb/wli/testing_1219/patch_near_tumor'

"""

import numpy as np
import pandas as pd
import os.path as osp
import openslide
# from scipy.misc import imsave as saveim
from imageio import imwrite as saveim
import glob
import cv2 as cv2
import xml.etree.ElementTree as et
from tqdm import tqdm
#########################################
import dldp.utils.fileman as fm

def convert_xml_df(file):
    """
    To convert xml file to a dataframe with all the coordinates

    :param file: xml file with the annotations from pathologists
    :type file: string
    :returns: all coordinates
    :rtype: dataframe

    """
    parseXML = et.parse(file)
    root = parseXML.getroot()
    dfcols = ['Name', 'Order', 'X', 'Y']
    df_xml = pd.DataFrame(columns=dfcols)
    for child in root.iter('Annotation'):
        for coordinate in child.iter('Coordinate'):
            Name = child.attrib.get('Name')
            Order = coordinate.attrib.get('Order')
            X_coord = float(coordinate.attrib.get('X'))
            Y_coord = float(coordinate.attrib.get('Y'))
            df_xml = df_xml.append(
                pd.Series([Name, Order, X_coord, Y_coord], index=dfcols), ignore_index=True)
            df_xml = pd.DataFrame(df_xml)
    return (df_xml)

def region_crop(slide, truth, crop_size, coord):
    """
    To extract all the four regions around a (x, y) coordinate together
    with the corresponding mask files.

    :param slide: the object created by openslide
    :type slide: object
    :param truth: the object created by openslide
    :type truth: object
    :param crop_size: the size of image patch to be extracted
    :type crop_size: list
    :param coord: a (x, y) coordinate
    :type coord: list
    :returns: four images patches and four mask images with the index
    :rtype: tuple

    """

    # width, height = slide.level_dimensions[0]
    dy, dx = crop_size
    x, y = coord
    x = int(x)
    y = int(y)
    # print(x, y)
    index = [[x, y], [x, y+dy-1], [x-dx+1, y], [x-dx+1, y+dy-1]]
    # print(index)
    #cropped_img = (image[x:(x+dx), y:(y+dy),:], rgb_binary[x:(x+dx), y:(y+dy)], mask[x:(x+dx), y:(y+dy)])
    rgb_image1 = slide.read_region((x, y), 0, crop_size)
    rgb_mask1 = truth.read_region((x, y), 0, crop_size)
    rgb_mask1 = (cv2.cvtColor(np.array(rgb_mask1),
                              cv2.COLOR_RGB2GRAY) > 0).astype(int)

    rgb_image2 = slide.read_region((x, y+dy-1), 0, crop_size)
    rgb_mask2 = truth.read_region((x, y+dy-1), 0, crop_size)
    rgb_mask2 = (cv2.cvtColor(np.array(rgb_mask2),
                              cv2.COLOR_RGB2GRAY) > 0).astype(int)

    rgb_image3 = slide.read_region((x-dx+1, y), 0, crop_size)
    rgb_mask3 = truth.read_region((x-dx+1, y), 0, crop_size)
    rgb_mask3 = (cv2.cvtColor(np.array(rgb_mask3),
                              cv2.COLOR_RGB2GRAY) > 0).astype(int)

    rgb_image4 = slide.read_region((x-dx+1, y+dy-1), 0, crop_size)
    rgb_mask4 = truth.read_region((x-dx+1, y+dy-1), 0, crop_size)
    rgb_mask4 = (cv2.cvtColor(np.array(rgb_mask4),
                              cv2.COLOR_RGB2GRAY) > 0).astype(int)

    # rgb_mask = (cv2.cvtColor(np.array(rgb_mask),
    #                         cv2.COLOR_RGB2GRAY) > 0).astype(int)
    # cropped_img = image[x:(x+dx), y:(y+dy),:]
    # cropped_binary = rgb_binary[x:(x+dx), y:(y+dy)]
    # cropped_mask = mask[x:(x+dx), y:(y+dy)]
    # print(index)
    return (rgb_image1, rgb_image2, rgb_image3, rgb_image4, rgb_mask1, rgb_mask2, rgb_mask3, rgb_mask4, index)

#sampletotal = pd.DataFrame([])

if __name__ == "__main__":

    slide_path = '/raida/wjc/CAMELYON16/training/tumor'
    # anno_path = '/Users/liw17/Documents/camelyon16/train/'
    anno_path = '/raida/wjc/CAMELYON16/training/lesion_annotations'
    mask_dir = '/raida/wjc/CAMELYON16/training/masking'
    slide_paths_total = glob.glob(osp.join(slide_path, '*.tif'))
    slide_paths_total.sort()

    crop_size = [224, 224]

    patch_near_tumor_dir = '/raidb/wli/testing_1219/patch_near_tumor'

    for i in tqdm(range(0, len(slide_paths_total))):
        # sampletotal = pd.DataFrame([])

        with openslide.open_slide(slide_paths_total[i]) as slide:

            new_folder = fm.creat_folder(osp.splitext(
                osp.basename(slide_paths_total[i]))[0], patch_near_tumor_dir)
            truth_slide_path = osp.join(mask_dir, osp.basename(
                slide_paths_total[i]).replace('.tif', '_mask.tif'))
            Anno_path_xml = osp.join(anno_path,
                                     osp.basename(slide_paths_total[i]).replace('.tif', '.xml'))

            with openslide.open_slide(str(truth_slide_path)) as truth:

                #slide = openslide.open_slide(slide_paths_total[i])
                annotations = convert_xml_df(str(Anno_path_xml))
                x_values = list(annotations['X'].get_values())
                y_values = list(annotations['Y'].get_values())
                coord = zip(x_values, y_values)
                coord = list(coord)

                m = 0
                while m in range(0, len(coord)):
                    r = region_crop(slide, truth, crop_size, coord[m])
                    for n in range(0, 4):
                        # selectlly save the image patches only outside of tumor regions
                        if (cv2.countNonZero(r[n+4]) == 0):

                            saveim('%s/%s_%d_%d.png' %
                                   (new_folder, osp.splitext(osp.basename(slide_paths_total[i]))[0], r[8][n][0], r[8][n][1]), r[n])

                            # io.imsave('/Users/liw17/Documents/new pred/%s_%d_%d_mask.png' % (osp.splitext(
                            #    osp.basename(slide_paths_total[i]))[0], r[8][n][0], r[8][n][1]), r[n+4])

                            # print(r[n])

                        m = m+5

                    # print(m)

max_issues_repo_name: DIDSR/dldp

max_forks_repo_name: DIDSR/dldp

reason: import numpy,from scipy