text:
import torch
import torch.nn as nn
import torchvision
import numpy as np
from torch.autograd import Variable
from torch.utils import model_zoo
from utils.config import opt
from data.amapdata import Dataset, TestDataset, inverse_normalize
from torch.utils import data as data_

print("PyTorch Version: ",torch.__version__)
print("Torchvision Version: ",torchvision.__version__)

__all__ = ['ResNet50', 'ResNet101','ResNet152']

def Conv1(in_planes, places, stride=2):
  return nn.Sequential(
    nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False),
    nn.BatchNorm2d(places),
    nn.ReLU(inplace=True),
    nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  )

class Bottleneck(nn.Module):
    def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):
      super(Bottleneck,self).__init__()
      self.expansion = expansion
      self.downsampling = downsampling

      self.bottleneck = nn.Sequential(
        nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
        nn.BatchNorm2d(places),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
        nn.BatchNorm2d(places),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=places, out_channels=places*self.expansion, kernel_size=1, stride=1, bias=False),
        nn.BatchNorm2d(places*self.expansion),
      )

      if self.downsampling:
        self.downsample = nn.Sequential(
          nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
          nn.BatchNorm2d(places*self.expansion)
        )
      self.relu = nn.ReLU(inplace=True)
    def forward(self, x):
      residual = x
      out = self.bottleneck(x)

      if self.downsampling:
        residual = self.downsample(x)

      out += residual
      out = self.relu(out)
      return out

class ResNet(nn.Module):
    def __init__(self,blocks, num_classes=1000, expansion = 4):
      super(ResNet,self).__init__()
      self.expansion = expansion

      self.conv1 = Conv1(in_planes = 3, places= 64)

      self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
      self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
      self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
      self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)

      self.avgpool = nn.AvgPool2d(7, stride=1)
      self.fc = nn.Linear(2048,num_classes)

      for m in self.modules():
        if isinstance(m, nn.Conv2d):
          nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
        elif isinstance(m, nn.BatchNorm2d):
          nn.init.constant_(m.weight, 1)
          nn.init.constant_(m.bias, 0)

    def make_layer(self, in_places, places, block, stride):
      layers = []
      layers.append(Bottleneck(in_places, places,stride, downsampling =True))
      for i in range(1, block):
        layers.append(Bottleneck(places*self.expansion, places))

      return nn.Sequential(*layers)

    def forward(self, x):
      x = self.conv1(x)

      x = self.layer1(x)
      x = self.layer2(x)
      x = self.layer3(x)
      x = self.layer4(x)

      x = self.avgpool(x)
      x = x.view(x.size(0), -1)
      x = self.fc(x)
      # add the softmax classification
      return x

    # def get_optimizer(self):
    #     """
    #     return optimizer, It could be overwriten if you want to specify
    #     special optimizer
    #     """
    #     lr = opt.lr
    #     params = []
    #     for key, value in dict(self.named_parameters()).items():
    #         if value.requires_grad:
    #             if 'bias' in key:
    #                 params += [{'params': [value], 'lr': lr * 2, 'weight_decay': 0}]
    #             else:
    #                 params += [{'params': [value], 'lr': lr, 'weight_decay': opt.weight_decay}]
    #     if opt.use_adam:
    #         self.optimizer = t.optim.Adam(params)
    #     else:
    #         self.optimizer = t.optim.SGD(params, momentum=0.9)
    #     return self.optimizer

    # def scale_lr(self, decay=0.1):
    #     for param_group in self.optimizer.param_groups:
    #         param_group['lr'] *= decay
    #     return self.optimizer

def ResNet50(pretrained = False):
    model = ResNet([3, 4, 6, 3])
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model

def ResNet101(pretrained = False):
    model = ResNet([3, 4, 23, 3])
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model

def ResNet152(pretrained = False):
    model = ResNet([3, 8, 36, 3])
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model

def train_model(model, criterion, optimizer, scheduler, num_epochs=50):
    opt._parse(kwargs)

    dataset = Dataset(opt)
    print(dataset)
    print('load data')
    dataloader = data_.DataLoader(dataset, \
                                  batch_size=1, \
                                  shuffle=True, \
                                  # pin_memory=True,
                                  num_workers=opt.num_workers)
    testset = TestDataset(opt)
    test_dataloader = data_.DataLoader(testset,
                                       batch_size=1,
                                       num_workers=opt.test_num_workers,
                                       shuffle=False, \
                                       pin_memory=True
                                       )
    data_loaders = {'train': dataloader, 'val': test_dataloader}
    Loss_list = {'train': [], 'val': []}
    Accuracy_list_species = {'train': [], 'val': []}
    Accuracy_list_classes = {'train': [], 'val': []}

    best_model_wts = copy.deepcopy(model.state_dict())
    best_class_acc = 0.0
    best_specy_acc = 0.0
    best_acc = 0.0
    best_loss = 10000

    #epoch循环训练
    for epoch in range(num_epochs):
        print('epoch {}/{}'.format(epoch,num_epochs - 1))
        print('-*' * 10)

        # 每个epoch都有train(训练)和val(测试)两个阶段
        for phase in ['train', 'val']:
            if phase == 'train':
                model.train()
            else:
                model.eval()

            running_loss = 0.0
            corrects_classes = 0
            corrects_species = 0

            for idx, (img, label, scale) in enumerate(data_loaders[phase]):

                #将数据存在gpu上
                inputs = Variable(img.cuda())
                labels_classes = Variable(label.cuda())
                # labels_species = Variable(data['species'].cuda())
                optimizer.zero_grad()

                #训练阶段
                with torch.set_grad_enabled(phase == 'train'):
                    x_classes = model(inputs)
                    _, preds_classes = torch.max(x_classes, 1)
                    # _, preds_species = torch.max(x_species, 1)
                    #计算训练误差
                    loss = criterion(x_classes, labels_classes)

                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                running_loss += loss.item() * inputs.size(0)
                corrects_classes += torch.sum(preds_classes == labels_classes)
                # corrects_species += torch.sum(preds_species == labels_species)

            epoch_loss = running_loss / len(data_loaders[phase].dataset)
            Loss_list[phase].append(epoch_loss)
            epoch_acc_classes = corrects_classes.double() / len(data_loaders[phase].dataset)
            # epoch_acc_species = corrects_species.double() / len(data_loaders[phase].dataset)

            Accuracy_list_classes[phase].append(100 * epoch_acc_classes)
            # Accuracy_list_species[phase].append(100 * epoch_acc_species)
            print('{} Loss: {:.4f}  Acc_classes: {:.2%}'
                  .format(phase, epoch_loss,epoch_acc_classes))

            #测试阶段
            if phase == 'val':
                #如果当前epoch下的准确率总体提高或者误差下降，则认为当下的模型最优
                if epoch_acc_classes > best_acc or epoch_loss < best_loss:
                    best_acc_classes = epoch_acc_classes
                    # best_acc_species = epoch_acc_species
                    best_acc = best_acc_classes
                    best_loss = epoch_loss
                    best_model_wts = copy.deepcopy(model.state_dict())
                    print('Best_model:  classes Acc: {:.2%}'
                          .format(best_acc_classes))

    model.load_state_dict(best_model_wts)
    torch.save(model.state_dict(), 'best_model.pt')
    print('Best_model:  classes Acc: {:.2%}'
          .format(best_acc_classes))
    return model, Loss_list,Accuracy_list_classes

net = ResNet50()
network = net.cuda()
optimizer = torch.optim.SGD(network.parameters(), lr=0.01, momentum=0.9)
criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.1) # Decay LR by a factor of 0.1 every 1 epochs
model, Loss_list, Accuracy_list_classes = train_model(network, criterion, optimizer, exp_lr_scheduler, num_epochs=100)
# if __name__=='__main__':
  #model = torchvision.models.resnet50()
  # model = ResNet50()
#   print(model)

# change the input to the amap images
  # input = torch.randn(1, 3, 224, 224)
  # out = model(input)
  # print(out)

# where is the optimizer? feedback? backpropogation?

max_stars_repo_name: qiutianzzz/amap

max_issues_repo_name: qiutianzzz/amap

max_forks_repo_name: qiutianzzz/amap


text:
# utility.py

version = '29th April 2021'

# simple utility function definitions:
#    def kji0_from_ijk1(index_ijk1):   # reverse order of indices and subtract 1
#    def ijk1_from_kji0(index_kji0):   # reverse order of indices and add 1
#    def extent_switch_ijk_kji(extent_in):   # reverse order of elements in extent (either ijk to kji or vice versa)
#    def string_ijk1_for_cell_kji0(cell_kji0):
#    def string_ijk1_for_cell_ijk1(cell_ijk1):
#    def string_ijk_for_extent_kji(extent_kji):
#    def string_xyz(xyz):
#    def cell_count_from_extent(extent):
#    def horizon_float(k0, plus_or_minus):

import numpy as np

def kji0_from_ijk1(index_ijk1):   # reverse order of indices and subtract 1
   """Returns index converted from simulator protocol to python protocol."""
   dims = index_ijk1.size
   result = np.zeros(dims, dtype = 'int')
   for d in range(dims):
      result[d] = index_ijk1[dims-d-1] - 1
   return result

def ijk1_from_kji0(index_kji0):   # reverse order of indices and add 1
   """Returns index converted from python protocol to simulator protocol."""
   dims = index_kji0.size
   result = np.zeros(dims, dtype = 'int')
   for d in range(dims):
      result[d] = index_kji0[dims-d-1] + 1
   return result

def extent_switch_ijk_kji(extent_in):   # reverse order of elements in extent
   """Returns equivalent grid extent switched either way between simulator and python protocols."""
   dims = extent_in.size
   result = np.zeros(dims, dtype = 'int')
   for d in range(dims):
      result[d] = extent_in[dims-d-1]
   return result

def cell_count_from_extent(extent):
   """Returns the number of cells in a grid with the given extent"""
   result = 1
   for d in range(len(extent)):   # list, tuple or 1D numpy array
      result *= extent[d]
   return result

def string_ijk1_for_cell_kji0(cell_kji0):
   """Returns a string showing indices for a cell in simulator protocol, from data in python protocol."""
   return '[{:}, {:}, {:}]'.format(cell_kji0[2]+1, cell_kji0[1]+1, cell_kji0[0]+1)

def string_ijk1_for_cell_ijk1(cell_ijk1):
   """Returns a string showing indices for a cell in simulator protocol, from data in simulator protocol."""
   return '[{:}, {:}, {:}]'.format(cell_ijk1[0], cell_ijk1[1], cell_ijk1[2])

def string_ijk_for_extent_kji(extent_kji):
   """Returns a string showing grid extent in simulator protocol, from data in python protocol."""
   return '[{:}, {:}, {:}]'.format(extent_kji[2], extent_kji[1], extent_kji[0])

def string_xyz(xyz):
   """Returns an xyz point as a string like (121234.56, 567890.12, 3456.789)"""

   return '({0:4.2f}, {1:4.2f}, {2:5.3f})'.format(xyz[0], xyz[1], xyz[2])

def horizon_float(k0, plus_or_minus):
   """Returns a floating point representation of k direction face index; eg. 3.5 for face between layers 3 and 4."""
   result = float(k0)
   if plus_or_minus == '+': result += 0.5
   elif plus_or_minus == '-': result -= 0.5
   else: assert False
   return result

ext: py

max_stars_repo_name: seryozni/resqpy

max_issues_repo_name: seryozni/resqpy

max_forks_repo_name: seryozni/resqpy

reason: import numpy


text:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import seaborn as sns
import numpy as np

# valid_mem_access_dict: key is block_id, value is a tuple of
# (block type, memory addr, block_size, access time interval)
def print_valid_mem_access_info(valid_mem_access_dict):
    for k in valid_mem_access_dict:
        v = valid_mem_access_dict[k]
        print('Block: {} access {} times:'.format(k, len(v)))
        for i in v:
            print(i)

def dump2file_valid_mem_access_info(valid_mem_access_dict):
    with open('valid_mem_access.txt', 'w') as mem_access_writer:
        for k in valid_mem_access_dict:
            v = valid_mem_access_dict[k]
            for i in v:
                # dump access time interval and size(in KB) into file
                time_interval, size = i[-1], i[-2]
                mem_access_writer.write(str(i[-1])+','+str(size)+'\n')

def extract_block_type(type_info_log):
    # block_info_map key is the memory address, value is a set of block type,
    # since the high-level framework usually performs some memory optimizations.
    block_info_map = {}
    with open(type_info_log, 'r') as log_reader:
        info_list = log_reader.readlines()
        for info in info_list:
            info = info.rstrip('\n')
            if info[0] == '%':
                continue
            it_list = info.split(' ')
            mem_addr, blk_type = it_list[0], it_list[1]
            if mem_addr in block_info_map:
                # FIXME:check blk_type
                block_info_map[mem_addr].add(blk_type)
            else:
                block_info_map[mem_addr] = set({blk_type})
    print(block_info_map)
    print(len(block_info_map))
    return block_info_map

def extract_and_plot(block_type_map,
                     log_file_name,
                     net_name,
                     time_scale=1000.0,
                     mem_scale=1024.0):
    # 1. extract the malloc & free memory behaviours.
    with open(log_file_name, 'r') as mem_info_reader:
        info_list = mem_info_reader.readlines()
        # malloc_free_dict, key is the mem ptr, value is a tupple of
        # ([mem_start_addr], [malloc_size], [malloc_time_stamp], [free_time_stamp])
        malloc_free_dict = {}
        # read_write_dict, key is the mem ptr, value is a tupple of
        # ([read or write], [mem_start_addr], [block_size], [time_stamp])
        read_write_dict = {}
        start_mem_addr = 0
        # maintain the lex order of the original logs.
        for info in info_list:
            info = info.rstrip('\n')
            #print(info)
            it_list = info.split(' ')
            # 'MALLOC: 0x7f7db6008200 4 1602580526755720327'
            # 'MALLOC: Type: Inter 0x7f8c4e005c00 3200 1602941501356865290'
            if it_list[0] == 'MALLOC:':
                if it_list[1] == 'Type:':
                    addr_key = it_list[3]
                    mem_addr = int(it_list[3], 16) / mem_scale
                    malloc_size = float(it_list[4]) / mem_scale
                    malloc_time_stamp = int(it_list[5]) / time_scale
                else:
                    addr_key = it_list[1]
                    mem_addr = int(it_list[1], 16) / mem_scale
                    malloc_size = float(it_list[2]) / mem_scale
                    malloc_time_stamp = int(it_list[3]) / time_scale
                if addr_key in malloc_free_dict:
                    print('Malloc multiple times! ==> {}: {}, {}, {}'.format(
                            addr_key, mem_addr, malloc_size, malloc_time_stamp))
                    malloc_free_dict[addr_key][0].append(mem_addr)
                    malloc_free_dict[addr_key][1].append(malloc_size)
                    malloc_free_dict[addr_key][2].append(malloc_time_stamp)
                else:
                    malloc_free_dict[addr_key] = ([mem_addr], [malloc_size],
                                               [malloc_time_stamp], [])
            if it_list[0] == 'WRITE:':
                # 'WRITE: 0x7f76ee001e00 4800 1602759703270048403'
                mem_addr = int(it_list[1], 16) / mem_scale
                block_size = float(it_list[2]) / mem_scale
                time_stamp = int(it_list[3]) / time_scale
                if it_list[1] in read_write_dict:
                    read_write_dict[it_list[1]][0].append('WRITE')
                    read_write_dict[it_list[1]][1].append(mem_addr)
                    read_write_dict[it_list[1]][2].append(block_size)
                    read_write_dict[it_list[1]][3].append(time_stamp)
                else:
                    read_write_dict[it_list[1]] = (['WRITE'], [mem_addr], [block_size], [time_stamp])
            if it_list[0] == 'READ:':
                # 'READ: 0x7f76ee003200 1600 1602759703270058576'
                mem_addr = int(it_list[1], 16) / mem_scale
                block_size = float(it_list[2]) / mem_scale
                time_stamp = int(it_list[3]) / time_scale
                if it_list[1] in read_write_dict:
                    read_write_dict[it_list[1]][0].append('READ')
                    read_write_dict[it_list[1]][1].append(mem_addr)
                    read_write_dict[it_list[1]][2].append(block_size)
                    read_write_dict[it_list[1]][3].append(time_stamp)
                else:
                    read_write_dict[it_list[1]] = (['READ'], [mem_addr], [block_size], [time_stamp])

            if it_list[0] == 'FREE:':
                # 'FREE: 0x7f7db6003200 1602580526794718008'
                free_time_stamp = int(it_list[2]) / time_scale
                if it_list[1] in malloc_free_dict:
                    malloc_free_dict[it_list[1]][3].append(free_time_stamp)
                else:
                    print('===> !!!FREE before!!!{}:{}'.format(
                        (int(it_list[1], 16) - start_mem_addr) / mem_scale,
                        free_time_stamp))
        print('malloc_free_dict len: {}'.format(len(malloc_free_dict)))
        print('read_write_dict len: {}'.format(len(read_write_dict)))

    # 2. fill out the invalid info
    # malloc_free_dict, key is the mem ptr, value is a tupple of
    # ([mem_start_addr], [malloc_size], [malloc_time_stamp], [free_time_stamp])
    # valid_mem_list: a list of tuple of (x, y, width, height)
    # (start malloc time, start mem addr, lifetime, malloc size)
    valid_mem_list = []
    block_id = 0
    # valid_block_dict, key is block id, value is a tuple of
    # (start_mem_addr, malloc size, malloc time stamp, free time stamp)
    valid_block_dict = {}
    block_id_map = {}
    for k in malloc_free_dict:
        v = malloc_free_dict[k]
        #print('len v[0]: {}, v[1]: {}, v[2]: {}, v[3]: {}'.format(
        #    len(v[0]), len(v[1]), len(v[2]), len(v[3])))
        min_len = min(len(v[0]), len(v[1]), len(v[2]), len(v[3]))
        if min_len > 0:
            for i in range(min_len):
                start_mem_addr = v[0][i]
                malloc_size = v[1][i]
                malloc_time_stamp = v[2][i]
                free_time_stamp = v[3][i]
                valid_block_dict[block_id] = (start_mem_addr, malloc_size, malloc_time_stamp, free_time_stamp)
                if k in block_id_map:
                    block_id_map[k].append(block_id)
                else:
                    block_id_map[k] = [block_id]
                block_id += 1
    print('valid_block_dict meta block info len: {}'.format(len(valid_block_dict)))
    # valid_mem_access_dict: key is block_id, value is a tuple of
    # (block type, memory addr, block_size, access time interval)
    valid_mem_access_dict = {}
    # for read_write_dict, key is the mem ptr, value is a tuple of
    # ([read or write], [mem_start_addr], [block_size], [time_stamp])
    for k in read_write_dict:
        # skip current block
        if k not in block_id_map or k not in block_type_map:
            continue
        block_type = list(block_type_map[k])[0]
        v = read_write_dict[k]
        prev_access_time_stamp = v[3][0]
        # start from the second line
        for i, rw in enumerate(v[0][1:]):
            current_mem_start_addr = v[1][i+1]
            current_block_size = v[2][i+1]
            current_time_stamp = v[3][i+1]
            time_interval = current_time_stamp - prev_access_time_stamp
            # find out which block id current block belongs to
            for blk_id in block_id_map[k]:
                start_mem_addr = valid_block_dict[blk_id][0]
                malloc_size = valid_block_dict[blk_id][1]
                malloc_time_stamp = valid_block_dict[blk_id][2]
                free_time_stamp = valid_block_dict[blk_id][3]
                if start_mem_addr == current_mem_start_addr and \
                        malloc_size == current_block_size and \
                        current_time_stamp >= malloc_time_stamp and \
                        current_time_stamp <= free_time_stamp:
                    #if time_interval >= 200.0:
                    #    print('---> {}, {}'.format(k, read_write_dict[k]))
                    # get it, we need to record the block info.
                    if blk_id in valid_mem_access_dict:
                        valid_mem_access_dict[blk_id].append((block_type, current_mem_start_addr, current_block_size, time_interval))
                    else:
                        valid_mem_access_dict[blk_id] = [(block_type, current_mem_start_addr, current_block_size, time_interval)]
                    break
            prev_access_time_stamp = current_time_stamp
    print('malloc_free_dict len: {}'.format(len(malloc_free_dict)))
    print('read_write_dict len: {}'.format(len(read_write_dict)))
    print('valid_block_dict meta block info len: {}'.format(len(valid_block_dict)))
    print('valid_mem_access_dict len: {}'.format(len(valid_mem_access_dict)))
    print_valid_mem_access_info(valid_mem_access_dict)
    dump2file_valid_mem_access_info(valid_mem_access_dict)

    # 3. plot cdf for access time_interval of each memory block.
    #time_interval_list = [it[-1] for k, v in valid_mem_access_dict.items() for it in v]
    time_interval_list = []
    for k, v in valid_mem_access_dict.items():
        for it in v:
            # access time interval less than 500.0 us.
            if it[-1] <= 500.0:
                time_interval_list.append(it[-1])

    print('time_interval_list len: {}'.format(len(time_interval_list)))
    plt.subplot(121)
    x, y = sorted(time_interval_list), np.arange(len(time_interval_list)) / len(time_interval_list)
    plt.plot(x, y, color = 'blue', linewidth = 1.0, linestyle = '--')
    plt.xlabel('Access time interval(/us)')
    plt.ylabel('CDF')
    plt.show()

    # 4. plot cdf for memory block size of each memory block.
    block_size_list = [it[-2] for k, v in valid_mem_access_dict.items() for it in v]
    x, y = sorted(block_size_list), np.arange(len(block_size_list)) / len(block_size_list)
    plt.plot(x, y, color = 'blue', linewidth = 1.0, linestyle = '--')
    plt.xlabel('Memory block size(/KB)')
    plt.show()

    block_size_counter = {}
    for size in block_size_list:
        if size in block_size_counter:
            block_size_counter[size] += 1
        else:
            block_size_counter[size] = 1
    print(block_size_counter)
    print(len(block_size_counter))

if __name__ == '__main__':
    blk_info_map = extract_block_type('large-logs/block-type-info.log')
    extract_and_plot(blk_info_map, 'large-logs/mem-info.log', 'MLP')
    #blk_info_map = extract_block_type('block-type-info.log')
    #extract_and_plot(blk_info_map, 'mem-info.log', 'MLP')
    #blk_info_map = extract_block_type('mlp-small-logs/block-type-info.log')
    #extract_and_plot(blk_info_map, 'mlp-small-logs/mem-info.log', 'MLP')

max_stars_repo_name: lijiansong/lang

max_issues_repo_name: lijiansong/lang

max_forks_repo_name: lijiansong/lang


text:
#!/usr/bin/python
#
# Handles the plotting window
#
# Hazen 02/13
#

from PyQt5 import QtCore, QtGui
import pyqtgraph

import numpy

pyqtgraph.setConfigOption('background', 'w')
pyqtgraph.setConfigOption('foreground', 'k')

def saveData(filename, x, y, z):
    numpy.savetxt(filename, numpy.concatenate((x[:,None], y[:,None], z[:,None]), axis = 1))

class PlotWindow(pyqtgraph.PlotWidget):

    def __init__(self, x_label, x_range, y_label, y_range, qt_parent):
        pyqtgraph.PlotWidget.__init__(self)

        self.viewbox = self.getPlotItem().getViewBox()
        self.viewbox.disableAutoRange()
        self.viewbox.setMouseEnabled(x = False, y = False)
        self.viewbox.setRange(xRange = (x_range[0], x_range[1]),
                              yRange = (y_range[0], y_range[1]))

        self.setLabel('left', y_label)
        self.setLabel('bottom', x_label)

        self.static_text = False

    def paintEvent(self, event):
        pyqtgraph.PlotWidget.paintEvent(self, event)

        if self.static_text:
            painter = QtGui.QPainter(self.viewport())
            painter.drawStaticText(70, 10, self.static_text)

    def plotBinnedData(self, b_sz, b_wx, b_wy):
        a_pen = pyqtgraph.mkPen(0,0,0)
        a_pen.setWidth(1)
        a_brush = pyqtgraph.mkBrush(255,255,255)
        self.plot(b_sz, b_wx,
                  pen = None,
                  symbol = 'o',
                  symbolPen = a_pen,
                  symbolBrush = a_brush,
                  symbolSize = 12)

        self.plot(b_sz, b_wy,
                  pen = None,
                  symbol = 'o',
                  symbolPen = a_pen,
                  symbolBrush = a_brush,
                  symbolSize = 12)
        saveData("binned.txt", b_sz, b_wx, b_wy)

    def plotData(self, sz, wx, wy):
        self.plot(sz, wx,
                  pen = None,
                  symbol = 'o',
                  symbolPen = pyqtgraph.mkPen(255,0,0),
                  symbolBrush = pyqtgraph.mkBrush(0,0,255),
                  symbolSize = 6)

        self.plot(sz, wy,
                  pen = None,
                  symbol = 'o',
                  symbolPen = pyqtgraph.mkPen(0,255,0),
                  symbolBrush = pyqtgraph.mkBrush(0,0,255),
                  symbolSize = 6)

    def plotFit(self, sz, xfit, yfit):
        a_pen = pyqtgraph.mkPen(0,0,0)
        self.plot(sz, xfit, pen = a_pen)
        self.plot(sz, yfit, pen = a_pen)
        saveData("fit.txt", sz, xfit, yfit)

    def plotStageQPD(self, stage, qpd, slope, offset):
        qpd_min = numpy.min(qpd)
        qpd_max = numpy.max(qpd)
        self.viewbox.setRange(yRange = (1.1*qpd_min, 1.1*qpd_max))
        self.plot(stage, qpd,
                  pen = None,
                  symbol = 'o',
                  symbolPen = pyqtgraph.mkPen(0,0,0),
                  symbolBrush = pyqtgraph.mkBrush(255,255,255),
                  symbolSize = 6)

        x_vals = numpy.array([-1.0, 1.0])
        y_vals = (x_vals - offset)/slope
        self.plot(x_vals, y_vals, pen = pyqtgraph.mkPen(0,0,0))

        self.static_text = QtGui.QStaticText("slope = {0:.3f} au/um".format(-1.0/slope))

    def plotWxWyData(self, wx, wy, cat):
        self.plot(wx, wy,
                  pen = None,
                  symbol = 'o',
                  symbolPen = pyqtgraph.mkPen(255,0,0),
                  symbolBrush = pyqtgraph.mkBrush(0,0,255),
                  symbolSize = 6)

    def plotWxWyFit(self, wxfit, wyfit):
        a_pen = pyqtgraph.mkPen(0,0,0)
        self.plot(wxfit, wyfit, pen = a_pen)

class SquarePlotWindow(PlotWindow):

    def __init__(self, x_label, x_range, y_label, y_range, parent):
        PlotWindow.__init__(self, x_label, x_range, y_label, y_range, parent)

        policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        #policy = QtGui.QSizePolicy()
        policy.setHeightForWidth(True)
        self.setSizePolicy(policy)

    def heightForWidth(self, w):
        #print "hFW:", w
        return w

#
# The MIT License
#
# Copyright (c) 2013 Zhuang Lab, Harvard University
#
# 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.
#


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# g1d.py

"""
matplotlib lineplot
"""
import numpy as np
import matplotlib.pyplot as plt

# -2 <= x <= 2 の範囲での y=x^2 のグラフ
x = np.linspace(-2, 2, 100)
y = x ** 2

# plt.plot(x, y)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, y)

plt.show()

max_stars_repo_name: happy-beans/pylearning

max_issues_repo_name: happy-beans/pylearning

max_forks_repo_name: happy-beans/pylearning


text:
from __future__ import division, absolute_import, print_function

import sys
from nose import SkipTest

def test_import():
    if sys.version_info[0] >= 3:
        raise SkipTest("traitsdoc not ported to Python3")
    import numpydoc.traitsdoc

# No tests at the moment...

max_stars_repo_name: Kleptobismol/scikit-bio


text:
import pandas as pd
import numpy as np
import random
import hashlib

def all_table_attrs(table_dict):
    return np.array(sorted(list(table_dict.keys()))),np.zeros(len(table_dict))

def all_join_attrs(table_dict):
    join_li = []
    len_join_enc = 0
    t_li = sorted(table_dict.keys())
    for i in range(len(table_dict)):
        for j in range(len(table_dict)):
            if i<j:
                intersect = sorted([k for k in table_dict[t_li[i]].columns.tolist() \
                             if k in table_dict[t_li[j]].columns.tolist()])
                for s in intersect:
                    join_li.append(f'join-attr-{t_li[i]}.{s}-to-{t_li[j]}.{s}')
                len_join_enc += len(intersect)
    join_enc = np.zeros(len(join_li))
    return np.array(join_li), join_enc

def all_num_predicate_attrs(table_dict):
    num_attrs_li = []
    num_attrs_types = []
    for i in table_dict.keys():
        sub_attrs_li=table_dict[i].columns.tolist()
        sub_attrs_types=table_dict[i].dtypes.tolist()
        for j in range(len(sub_attrs_li)):
            poss_el = f'{i}.{sub_attrs_li[j]}'
            if poss_el not in num_attrs_li: #Check if already present
                #print(sub_attrs_types[j].char,sub_attrs_li[j])
                if sub_attrs_types[j].char=='?': #BOOL
                    num_attrs_li.append(poss_el)
                    num_attrs_types.append(sub_attrs_types[j])
                elif sub_attrs_types[j].char=='l': #INT
                    num_attrs_li.append(poss_el)
                    num_attrs_types.append(sub_attrs_types[j])
                elif sub_attrs_types[j].char=='d': #FLOAT
                    num_attrs_li.append(poss_el)
                    num_attrs_types.append(sub_attrs_types[j])
    num_attrs_li+=['<','>','=','NORMALIZED_NUM']
    num_attrs_types+=[0,0,0,0]
    num_attrs_enc = np.zeros(len(num_attrs_li))
    return np.array(num_attrs_li), np.array(num_attrs_types), num_attrs_enc

def all_non_num_predicate_attrs(table_dict):
    num_attrs_li = []
    num_attrs_types = []
    for i in table_dict.keys():
        sub_attrs_li=table_dict[i].columns.tolist()
        sub_attrs_types=table_dict[i].dtypes.tolist()
        for j in range(len(sub_attrs_li)):
            poss_el = f'{i}.{sub_attrs_li[j]}'
            if poss_el not in num_attrs_li: #Check if already present
                if sub_attrs_types[j].char=='O': #STRING OBJECT
                    num_attrs_li.append(poss_el)
                    num_attrs_types.append(sub_attrs_types[j])
    num_attrs_li+=['HASHED_INT','E_BALL']
    num_attrs_types+=[0,0]
    num_attrs_enc = np.zeros(len(num_attrs_li))
    return np.array(num_attrs_li), np.array(num_attrs_types), num_attrs_enc

def rand_select_joins(join_attrs_li,num_joins):
    joins_select = np.random.choice(range(len(join_attrs_li)),1).tolist()
    valid_tables = []
    while len(joins_select)!=num_joins:
        t = join_attrs_li[joins_select[-1]].split('.')
        table_1 = t[0].replace('join-attr-','')
        table_2 = t[1].split('-')[-1]
        valid_tables+=[table_1,table_2]

        curr_len = len(joins_select)
        inds = list(range(len(join_attrs_li)))
        random.shuffle(inds)
        for i in inds:
            if i not in joins_select:
                t2 = join_attrs_li[i].split('.')
                table_A = t2[0].replace('join-attr-','')
                table_B = t2[1].split('-')[-1]
                tA=(table_A in valid_tables)
                tB=(table_B in valid_tables)
                if tA or tB:
                    joins_select.append(i)
                    break
        if len(joins_select)==curr_len:
            return []
    return joins_select

def str_hash_procedure(string):
    #print(string)
    return float(int(hashlib.sha1(string.encode("utf-8"\
                    )).hexdigest(), 16) % (10 ** 8))/(10**8)

def str_L1_distance(str_embedding_1, str_embedding_2):
    return abs(str_embedding_1 - str_embedding_2)

class Card_Dataset:
    def __init__(self, join_lim=4, num_predicate_lim=4, non_num_predicate_lim=2,\
                 string_embedding=str_hash_procedure,string_dist_func=str_L1_distance):
        self.join_lim = join_lim
        self.num_predicate_lim = num_predicate_lim
        self.non_num_predicate_lim = non_num_predicate_lim
        self.string_embedding = string_embedding
        self.string_dist_func = string_dist_func
        self.table_dict = {}

    def add_table(self,table,name=None):
        if name is None:
            name = str(int(np.random.choice(range(10**6),1)[0]))
        self.table_dict[name]=table

    def featurize_space(self,verbose=False,join_override=[]):
        #TABLE ATTRS
        try:
            self.table_attrs_li, self.table_attrs_enc = \
                        all_table_attrs(self.table_dict)
        except Exception as e:
            raise Exception(f'Failed During Table Parsing:\n{e}')
        if verbose:
            print('All Table Names:')
            print(self.table_attrs_li)
            print('')

        #JOIN ATTRS
        try:
            if len(join_override)==0:
                self.join_attrs_li, self.join_attrs_enc = \
                                all_join_attrs(self.table_dict)
            else:
                self.join_attrs_li = [f'join-attr-{i[0]}-to-{i[1]}' \
                                for i in join_override]
                self.join_attrs_enc = np.zeros(len(self.join_attrs_li))
        except Exception as e:
            raise Exception(f'Failed During Join Parsing:\n{e}')
        if verbose:
            print('All Enumerated Joins:')
            print(self.join_attrs_li)
            print('')

        #NUMERICAL PREDICATE ATTRS
        try:
            self.num_preds_li, self.num_preds_type, self.num_preds_enc = \
                            all_num_predicate_attrs(self.table_dict)
        except Exception as e:
            raise Exception(f'Failed During Numerical Predicate Parsing:\n{e}')
        if verbose:
            print('Numerical Predicate Space:')
            print(self.num_preds_li)
            print('')

        #NON-NUMERICAL PREDICATE ATTRS
        try:
            self.non_num_preds_li, self.non_num_preds_type, self.non_num_preds_enc = \
                            all_non_num_predicate_attrs(self.table_dict)
        except Exception as e:
            raise Exception(f'Failed During Non-Numerical Predicate Parsing:\n{e}')
        if verbose:
            print('Non-Numerical Predicate Space:')
            print(self.non_num_preds_li)
            print('')
        return

    def run_query(self,full_flat_query):
        full_flat_query = np.array(full_flat_query).flatten() #Just in case
        table_enc = full_flat_query[:len(self.table_attrs_enc)]
        join_enc = full_flat_query[len(self.table_attrs_enc):\
                                len(self.table_attrs_enc)+\
                                len(self.join_attrs_enc)]
        num_preds_enc = full_flat_query[len(self.table_attrs_enc)+\
                                len(self.join_attrs_enc):\
                                len(full_flat_query)-\
                                self.non_num_predicate_lim*len(self.non_num_preds_enc)]
        non_num_preds_enc = full_flat_query[len(full_flat_query)-\
                                self.non_num_predicate_lim*len(self.non_num_preds_enc):]

        joins_select = np.nonzero(join_enc)[0].tolist()
        #Merging procedure based on joining routine
        res_df = pd.DataFrame()
        for i in joins_select:
            t = self.join_attrs_li[i].split('.')
            table_1 = t[0].replace('join-attr-','')
            table_2 = t[1].split('-')[-1]
            sel_attr = t[-1]
            if len(res_df)==0:
                res_df = pd.merge(self.table_dict[table_1],\
                                  self.table_dict[table_2],\
                                  how='outer',on=[sel_attr])
            else:
                res_df = res_df.merge(pd.merge(self.table_dict[table_1],\
                                  self.table_dict[table_2],\
                                  how='outer',on=[sel_attr]))
        full_len = len(res_df)
        if len(res_df)==0:
            return 0,full_len
        #Drop any redundant columns that failed on matching
        off_cols = [i for i in res_df.columns.tolist() \
                        if '_x'==i[-2:] or '_y'==i[-2:]]
        for i in off_cols:
            res_df[i.split('_')[0]]=res_df[i]
            res_df = res_df.drop(columns=[i])

        #Done with joins, moving to numerical predicates
        for i in np.array_split(num_preds_enc,self.num_predicate_lim):
            if np.sum(i)!=0: #Check to make sure we have a valid predicate
                if len(res_df)==0:
                    return 0,full_len
                pred_col = np.nonzero(i[:-1])[0][0]
                pred_op = np.nonzero(i[:-1])[0][1]
                pred_val = i[-1]
                t_col = self.num_preds_li[pred_col].split('.')[1]
                if res_df[t_col].dtype.char == '?':
                    v_min=0
                    v_max=1
                else:
                    v_min = np.min(res_df[t_col].tolist())
                    v_max = np.max(res_df[t_col].tolist())
                pred_val_scaled = (pred_val*(v_max-v_min))+v_min
                if self.num_preds_li[pred_op]=='<':
                    res_df = res_df.loc[res_df[t_col]<pred_val_scaled]
                elif self.num_preds_li[pred_op]=='>':
                    res_df = res_df.loc[res_df[t_col]>pred_val_scaled]
                else:
                    res_df = res_df.loc[res_df[t_col]==pred_val_scaled]

        #Done with numerical predicates, now on to non-numerical predicates
        if self.non_num_predicate_lim!=0:
            for i in np.array_split(non_num_preds_enc,self.non_num_predicate_lim):
                if np.sum(i)!=0: #Check to make sure we have a valid predicate
                    pred_col = np.nonzero(i[:-2])[0][0]
                    pred_val = int(i[-2]*(10**8))
                    e_ball = i[-1]
                    t_col = self.non_num_preds_li[pred_col].split('.')[1]
                    if t_col not in res_df.columns:
                        return 0,0
                    hash_it = [int(self.string_embedding(j)*(10**8)) for \
                               j in res_df[t_col].tolist()]
                    res_df = res_df.loc[self.string_dist_func(np.array(hash_it),\
                                                        pred_val)<=e_ball] #ADD E_BALL?

        return len(res_df),full_len

    def generate_random_query(self,flatten=True):
        num_joins = np.random.choice(range(1,self.join_lim+1))
        joins_select = []
        while len(joins_select)!=num_joins:
            #Note that there's some cleverness about chosing join
            #statements that do not automatically conflict with one another.
            joins_select = rand_select_joins(self.join_attrs_li,num_joins)

        #Embedding one-hot enc for join operations
        new_join_enc = self.join_attrs_enc.copy()
        for i in joins_select:
            new_join_enc[i]=1

        #Embedding one-hot enc for table operators, based on join commands
        new_table_enc = self.table_attrs_enc.copy()
        all_tables_li = []
        for i in joins_select:
            t = self.join_attrs_li[i].split('.')
            table_1 = t[0].replace('join-attr-','')
            table_2 = t[1].split('-')[-1]
            all_tables_li+=[table_1,table_2]
        all_tables_li = sorted(list(set(all_tables_li)))
        for i in all_tables_li:
            new_table_enc[self.table_attrs_li.tolist().index(i)]=1

        #Embedding for numerical predicates (int, float, bool)
        all_poss_num_attrs = []
        for i in all_tables_li:
            c = self.table_dict[i].columns
            for j in c:
                if f'{i}.{j}' in self.num_preds_li:
                    all_poss_num_attrs.append(self.num_preds_li.tolist(
                                                    ).index(f'{i}.{j}'))
        num_predicates = np.random.choice(range(1,self.num_predicate_lim+1))
        new_num_predicates_enc = []
        for i in range(num_predicates):
            new_num_pred_enc = self.num_preds_enc.copy()
            attr_choice = np.random.choice(all_poss_num_attrs,1)[0]
            new_num_pred_enc[attr_choice]=1
            if self.num_preds_type[attr_choice].char == '?': #? --> BOOL, use =
                eq_choice = '='
                val_choice = np.random.choice([0,1],1)[0]
            elif self.num_preds_type[attr_choice].char == 'l': #l --> INT, USE ANY
                eq_choice = np.random.choice(['<','>','='],1)[0]
                val_choice = np.random.uniform(0,1,1)[0]
            else: #d --> FLOAT, USE < or >
                eq_choice = np.random.choice(['<','>'],1)[0]
                val_choice = np.random.uniform(0,1,1)[0]
            new_num_pred_enc[self.num_preds_li.tolist().index(eq_choice)]=1
            new_num_pred_enc[-1]=val_choice
            new_num_predicates_enc+=new_num_pred_enc.tolist() #FILL WITH BLANKS!
        new_num_predicates_enc+=np.zeros((self.num_predicate_lim-\
                                num_predicates)*len(self.num_preds_enc)).tolist()

        #Embedding for non-numerical predicates (strings)
        all_poss_str_attrs = []
        for i in all_tables_li:
            c = self.table_dict[i].columns
            for j in c:
                if f'{i}.{j}' in self.non_num_preds_li:
                    all_poss_str_attrs.append(self.non_num_preds_li.tolist(
                                                    ).index(f'{i}.{j}'))
        if len(all_poss_str_attrs)!=0 and self.non_num_predicate_lim!=0:
            num_str_predicates = np.random.choice(range(1,self.non_num_predicate_lim+1))
            new_str_predicates_enc = []
            for i in range(num_str_predicates):
                new_str_pred_enc = self.non_num_preds_enc.copy()
                attr_choice = np.random.choice(all_poss_str_attrs,1)[0]
                attr_table = self.non_num_preds_li[attr_choice].split('.')[0]
                attr_col = self.non_num_preds_li[attr_choice].split('.')[1]
                str_c = np.random.choice(self.table_dict[attr_table]\
                                              [attr_col].tolist(),1)[0]
                new_str_pred_enc[attr_choice]=1
                #TRY OTHER METHODS OF HASHING AT SOME OTHER POINT, MORE SENSIBLE!
                float_c = str_hash_procedure(str_c)
                new_str_pred_enc[-2] = float_c
                #DON'T MESS WITH THE E-BALL FOR NOW EITHER! KEEP IT AT 0 (perfect match)
                new_str_predicates_enc+=new_str_pred_enc.tolist()

            new_str_predicates_enc+=np.zeros((self.non_num_predicate_lim-\
                                    num_str_predicates)*\
                                    len(self.non_num_preds_enc)).tolist()
        else:
            new_str_predicates_enc=np.zeros(len(self.non_num_preds_enc)*\
                                            self.non_num_predicate_lim)

        final_query = [np.array(new_table_enc),np.array(new_join_enc),\
            np.array(new_num_predicates_enc),np.array(new_str_predicates_enc)]

        final_query_flat = np.concatenate((new_table_enc,new_join_enc,\
              new_num_predicates_enc,new_str_predicates_enc))
        final_card,full_len = self.run_query(final_query_flat)
        if full_len==0:
            norm_card=0
        else:
            norm_card=final_card/float(full_len)

        if flatten:
            return final_query_flat, norm_card, full_len
        else:
            return final_query, norm_card, full_len

    def generate_N_queries(self,n,flatten=True):
        all_queries = []
        all_card_fracs = []
        while len(all_queries)!=n:
            query, frac, full_len = self.generate_random_query(flatten)
            if full_len!=0:
                all_queries.append(query)
                all_card_fracs.append(frac)
        return np.array(all_queries), np.array(all_card_fracs)

    def verbose_from_vector(self,vector):
        vector = np.array(vector).flatten()
        table_enc = vector[:len(self.table_attrs_enc)]
        join_enc = vector[len(self.table_attrs_enc):\
                                len(self.table_attrs_enc)+\
                                len(self.join_attrs_enc)]
        num_preds_enc = vector[len(self.table_attrs_enc)+\
                                len(self.join_attrs_enc):\
                                len(full_flat_query)-\
                                self.non_num_predicate_lim*len(self.non_num_preds_enc)]
        non_num_preds_enc = vector[len(full_flat_query)-\
                                self.non_num_predicate_lim*len(self.non_num_preds_enc):]

        tables_select = np.nonzero(join_enc)[0].tolist()
        #NOT FULLY IMPLEMENTED YET, NOT STRICTLY NECESSARY


text:
from functools import partial
from typing import List, Tuple

import haiku as hk
import jax
import jax.numpy as jnp
import numpy as np

from rljax.algorithm.qrdqn import QRDQN
from rljax.network import DiscreteImplicitQuantileFunction
from rljax.util import fake_state

class IQN(QRDQN):
    name = "IQN"

    def __init__(
        self,
        num_agent_steps,
        state_space,
        action_space,
        seed,
        max_grad_norm=None,
        gamma=0.99,
        nstep=1,
        buffer_size=10 ** 6,
        use_per=False,
        batch_size=32,
        start_steps=50000,
        update_interval=4,
        update_interval_target=8000,
        eps=0.01,
        eps_eval=0.001,
        eps_decay_steps=250000,
        loss_type="huber",
        dueling_net=False,
        double_q=False,
        setup_net=True,
        fn=None,
        lr=5e-5,
        units=(512,),
        num_quantiles=64,
        num_quantiles_eval=32,
        num_cosines=64,
    ):
        if fn is None:

            def fn(s, cum_p):
                return DiscreteImplicitQuantileFunction(
                    num_cosines=num_cosines,
                    action_space=action_space,
                    hidden_units=units,
                    dueling_net=dueling_net,
                )(s, cum_p)

        if not hasattr(self, "fake_args"):
            self.fake_args = (fake_state(state_space), np.empty((1, num_quantiles), dtype=np.float32))
        if not hasattr(self, "use_key_forward"):
            self.use_key_forward = True
        if not hasattr(self, "num_keys_loss"):
            self.num_keys_loss = 3

        super(IQN, self).__init__(
            num_agent_steps=num_agent_steps,
            state_space=state_space,
            action_space=action_space,
            seed=seed,
            max_grad_norm=max_grad_norm,
            gamma=gamma,
            nstep=nstep,
            buffer_size=buffer_size,
            batch_size=batch_size,
            use_per=use_per,
            start_steps=start_steps,
            update_interval=update_interval,
            update_interval_target=update_interval_target,
            eps=eps,
            eps_eval=eps_eval,
            eps_decay_steps=eps_decay_steps,
            loss_type=loss_type,
            dueling_net=dueling_net,
            double_q=double_q,
            setup_net=setup_net,
            fn=fn,
            lr=lr,
            num_quantiles=num_quantiles,
        )
        self.num_quantiles_eval = num_quantiles_eval

    @partial(jax.jit, static_argnums=0)
    def _forward(
        self,
        params: hk.Params,
        state: np.ndarray,
        key: jnp.ndarray,
    ) -> jnp.ndarray:
        cum_p = jax.random.uniform(key, (state.shape[0], self.num_quantiles_eval))
        return jnp.argmax(self.net.apply(params, state, cum_p).mean(axis=1), axis=1)

    @partial(jax.jit, static_argnums=0)
    def _calculate_target(
        self,
        params: hk.Params,
        params_target: hk.Params,
        reward: np.ndarray,
        done: np.ndarray,
        next_state: np.ndarray,
        cum_p_prime: jnp.ndarray,
        key: jnp.ndarray,
    ) -> jnp.ndarray:
        if self.double_q:
            next_action = self._forward(params, next_state, key=key)[..., None]
        else:
            next_action = self._forward(params_target, next_state, key=key)[..., None]
        next_quantile = self._calculate_value(params_target, next_state, next_action, cum_p=cum_p_prime)
        target = reward[:, None] + (1.0 - done[:, None]) * self.discount * next_quantile
        return jax.lax.stop_gradient(target).reshape(-1, 1, self.num_quantiles)

    @partial(jax.jit, static_argnums=0)
    def _loss(
        self,
        params: hk.Params,
        params_target: hk.Params,
        state: np.ndarray,
        action: np.ndarray,
        reward: np.ndarray,
        done: np.ndarray,
        next_state: np.ndarray,
        weight: np.ndarray,
        key_list: List[np.ndarray],
    ) -> Tuple[jnp.ndarray, jnp.ndarray]:
        cum_p = jax.random.uniform(key_list[0], (state.shape[0], self.num_quantiles))
        cum_p_prime = jax.random.uniform(key_list[1], (state.shape[0], self.num_quantiles))
        quantile = self._calculate_value(params, state, action, cum_p=cum_p)
        target = self._calculate_target(params, params_target, reward, done, next_state, cum_p_prime, key_list[2])
        return self._calculate_loss_and_abs_td(quantile, target, cum_p, weight)

reason: import numpy,import jax


text:
import numpy as np
import json
import pickle
import sqlite3

from konlpy.tag import Okt
from scipy.sparse import lil_matrix
from sklearn.naive_bayes import MultinomialNB
from sklearn import linear_model

from retrain import read_data, tokenize

# # Req 2-2-4. app.db 를 연동하여 웹에서 주고받는 데이터를 DB로 저장
def save_text_to_db(sentence):
    # db에 저장
    global output

    con = sqlite3.connect('./app.db')
    cur = con.cursor()

    cur.execute(
        'INSERT INTO search_history(question, answer) VALUES(?,?)', (sentence, output,))
    con.commit()

    output = -1
    cur.close()

# 결과값이 틀린 경우 데이터를 DB에 저장
def edit_data():
    chk = False
    con = sqlite3.connect('./app.db')
    cur = con.cursor()

    recent_record = cur.execute(
        'SELECT max(id), answer FROM search_history')
    idx = 0
    answer = -1
    for value in recent_record:
        idx = value[0]
        answer = value[1]

    if(answer == 0):
        cur.execute('UPDATE search_history SET answer = 1 WHERE id = %s' % idx)
        print("|\t긍정(1)으로 수정완료")
        chk = True
    elif(answer == 1):
        cur.execute('UPDATE search_history SET answer = 0 WHERE id = %s ' % idx)
        print("|\t부정(0)으로 수정완료")
        chk = True
    else:
        print("error")

    con.commit()
    cur.close()

    # db 업데이트 성공유무를 리턴
    return chk

# 추가 데이터 트레이닝
def data_training():
    global beforeTrainDataIdx

    chk = True
    con = sqlite3.connect('./app.db')
    cur = con.cursor()
    # DB에 저장된 데이터 개수 확인
    recent_record = cur.execute(
        'SELECT max(id), answer FROM search_history')

    recent_idx = 0
    for value in recent_record:
        recent_idx = value[0]

    # DB에 데이터가 10개 미만일 경우 chk -> false
    if((recent_idx - beforeTrainDataIdx) < 10):
        print("|\t추가로 저장된 데이터가 10개 미만입니다")
        print("|\t이전 문장으로 10번 학습시키겠습니까?(y/n)",end=" ")
        ans = input()
        if ans == "y":
            recent_record = cur.execute(
            'SELECT max(id), question, answer FROM search_history')

            for value in recent_record:
                temp_sentence = value[1]
                temp_ans = value[2]
            for _ in range(10):
                cur.execute(
                'INSERT INTO search_history(question, answer) VALUES(?,?)', (temp_sentence, temp_ans,))
                con.commit()

        chk = False

    # DB에 데이터가 10개 이상일 경우 chk -> true
    else:
        print("|\t학습하겠습니다")
        print("|\t{}부터 학습합니다".format(beforeTrainDataIdx+1))

        f = open('retrain.txt', mode='wt', encoding='utf-8')
        readData = cur.execute(
            'SELECT * FROM search_history WHERE id > %s ' % beforeTrainDataIdx
        )
        temp = ""
        for data in readData:
            row = str(data[1]) + "\t" + data[2] + "\t" + str(data[3]) + "\n"
            temp += row

        f.write(temp)
        print("|\t텍스트 파일 쓰기완료")
        f.close()

        beforeTrainDataIdx = recent_idx  # 현재 데이터까지 학습했음을 저장
        print("|\t{} idx 까지 학습완료".format(beforeTrainDataIdx))
        chk = True

        # 추가 데이터 트레이닝
        train_data = read_data('retrain.txt')
        train_docs = tokenize(train_data)
        print('|\tread_data, tokenize')

        X = lil_matrix((len(train_docs), len(word_indices)))
        Y = np.zeros(len(train_docs))

        print('|\tX, Y init')
        for idx in range(len(train_docs)):
            temp = [0]*len(word_indices)
            for morph in train_docs[idx]:
                word = morph.split('/')[0]
                if word_indices.get(word)!=None:
                    temp[word_indices[word]]=1
            X[idx]=temp
        print('|\tX one hot embedding ')
        for idx in range(len(train_data)):
            word = train_data[idx][2].split('\n')[0]
            Y[idx]=word
        print('|\tY label')
        NB.partial_fit(X, Y) # naive Bayes
        print('|\tnaive')
        LR.partial_fit(X, Y) # Logistic
        print('|\tlogistic')
        SVM.partial_fit(X, Y) # SVM
        print('|\tSVM')

        fl = open('origin_model.clf', 'wb')
        pickle.dump(NB, fl)
        pickle.dump(LR, fl)
        pickle.dump(SVM, fl)
        pickle.dump(DT, fl)
        pickle.dump(word_indices, fl)
        fl.close()
    cur.close()

    return chk

# 전처리:토큰화
def preprocess(sentence):
    pos_tagger = Okt()
    Word_vector = []
    result = ['/'.join(t) for t in pos_tagger.pos(sentence, norm=True, stem=True)]
    Word_vector += [result]

    # one-hot 임베딩
    for idx in range(len(Word_vector)):
        temp = [0]*len(word_indices)
        for morph in Word_vector[idx]:
            word = morph.split('/')[0]
            if word_indices.get(word) != None:
                temp[word_indices[word]] = 1
        Word_vector[idx] = temp

    return Word_vector

def predict(Word_vector, test_clf):
    predict_result = test_clf.predict(Word_vector)[0]
    return predict_result

# # Req 2-2-3. 긍정 혹은 부정으로 분류

def classify(sentence, test_clf, model_name):
    global neg
    global pos

    weight = [0.3, 0.7]

    predict_result = int(predict(sentence, test_clf))

    if model_name=="NB" or model_name=="DTC":
        neg += test_clf.predict_proba(sentence)[0][0]
        pos += test_clf.predict_proba(sentence)[0][1]

    else:
        neg += weight[1-predict_result]
        pos += weight[predict_result]

    return predict_result

def send_message(sentence):
    global neg, pos
    global output

    Word_vector = preprocess(sentence)
    predict_NB = classify(Word_vector, NB, "NB")
    predict_LR = classify(Word_vector, LR, "LR")
    predict_SVM = classify(Word_vector, SVM, "SVM")
    predict_DTC = classify(Word_vector, DT, "DT")

    if neg > pos:
        output = 0
        print("|\t결과: < 부정 >")
    else:
        output = 1
        print("|\t결과: < 긍정 >")

    neg = 0
    pos = 0

    return output

if __name__ == '__main__':

    # Req 2-2-1. pickle로 저장된 model.clf 파일 불러오기
    pickle_obj = open('origin_model.clf', 'rb')
    NB = pickle.load(pickle_obj) # naive bayes
    LR = pickle.load(pickle_obj) # Logistic Regression
    SVM = pickle.load(pickle_obj) # SVM
    DT = pickle.load(pickle_obj) # 의사결정트리
    word_indices = pickle.load(pickle_obj)

    # Req 2-2-2. 토큰화 및 one-hot 임베딩하는 전 처리
    pos_tagger = Okt()

    # 기능 수행 위한 초기 변수들
    neg = 0
    pos = 0
    output = -1  # 트레이닝결과값 0/1을 db에 쓰기위한 초기화 변수
    beforeTrainDataIdx = 0  # 까지 학습했다.를 저장하는 전역변수로 사용

    ''' 데모테스트 '''

    while True:
        print()
        print("============================================================")
        print("|\t메뉴를 선택해주세요")
        print("|\t1.문장입력")
        print("|\t2.Retraining")
        print("|\t3.종료")
        print("============================================================")
        select = int(input("|\t메뉴선택: "))
        if select == 3:
            break
        elif select == 1:
            sentence = input("|\t입력: ")
            send_message(sentence)
            save_text_to_db(sentence)
            print("|\t수정하시겠습니까?(y/n)", end=" ")
            ans = input()
            if ans == 'y':
                edit_data()
        elif select == 2:
            data_training()
        else:
            print("|\t잘못입력했습니다.")
        print("============================================================")

reason: import numpy,from scipy


text:
"""

Carlyn C. Dougherty
ccd2134@columbia.edu
Robotics Research, Fall 2018

"""

import os
import sys
import pybullet as p
from math import pi, sqrt
import pybullet_data
from time import sleep, time
import astropy.coordinates
import random
from transforms3d import euler
from configparser import ConfigParser
from pyquaternion import Quaternion
import numpy as np
from scipy.spatial import ConvexHull, distance

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))

#PYBULLET HOUSEKEEPING + GUI MAINTENANCE
physicsClient = p.connect(p.GUI)  # or p.DIRECT for non-graphical version
p.setAdditionalSearchPath(pybullet_data.getDataPath())  # optionally

# This is to change the visualizer window settings
p.configureDebugVisualizer(p.COV_ENABLE_RGB_BUFFER_PREVIEW, enable=0)
p.configureDebugVisualizer(p.COV_ENABLE_DEPTH_BUFFER_PREVIEW, enable=0)
p.configureDebugVisualizer(p.COV_ENABLE_SEGMENTATION_MARK_PREVIEW, enable=0)
# change init camera distance/location to view scene
p.resetDebugVisualizerCamera(cameraDistance=.5, cameraYaw=135, cameraPitch=-20, cameraTargetPosition=[0.0, 0.0, 0.0])

#GLOBAL VARIABLES  - from config file
config = ConfigParser()
print(os.path.join(os.path.dirname(__file__), 'bh_config.ini'))
config.read(os.path.join(os.path.dirname(__file__), 'bh_config.ini'))

robot_path = config.get('file_paths', 'robot_path')
robot_path = os.path.join(PROJECT_ROOT, robot_path)
object_path = config.get('file_paths', 'object_path')
object_path = os.path.join(PROJECT_ROOT, object_path)
object_scale = config.getfloat('file_paths', 'object_scale')

init_grasp_distance = config.getfloat('grasp_settings', 'init_grasp_distance')
speed_find_distance = config.getfloat('grasp_settings', 'speed_find_distance')
grasp_distance_margin = config.getfloat('grasp_settings', 'grasp_distance_margin')

max_grasp_force = config.getfloat('grasp_settings', 'max_grasp_force')
target_grasp_velocity = config.getfloat('grasp_settings', 'target_grasp_velocity')
grasp_time_limit = config.getfloat('grasp_settings', 'grasp_time_limit')
active_grasp_joints = [int(j.strip()) for j in config.get('grasp_settings', 'active_grasp_joints').split(',')]
num_grasps_per_cycle = config.getint('grasp_settings', 'num_grasps_per_cycle')
num_cycles_to_grasp = config.getint('grasp_settings', 'num_cycles_to_grasp')
num_wrist_rotations = config.getint('grasp_settings', 'num_wrist_rotations')
use_wrist_rotations = config.getboolean('grasp_settings', 'use_wrist_rotations')

force_pyramid_sides = config.getint('eval_settings', 'force_pyramid_sides')
force_pyramid_radius = config.getfloat('eval_settings', 'force_pyramid_radius')

use_gui = config.getboolean('gui_settings', 'use_gui')
debug_lines = config.getboolean('gui_settings', 'debug_lines')
debug_text = config.getboolean('gui_settings', 'debug_text')

# UTILIES
def rand_coord():
    rand_theta = random.uniform(-pi / 2, pi / 2)
    rand_phi = random.uniform(-pi / 2, pi / 2)
    return rand_theta, rand_phi

def add_debug_lines(rID, line_dist=.3, line_width = 500):
    """
    Use pybullet's built-in line functionality to see the z/y/z coords of the hand
    """
    p.addUserDebugLine([0, 0, 0], [line_dist, 0, 0], [1, 0, 0], parentObjectUniqueId=rID, parentLinkIndex=-1,
                       lineWidth=line_width)
    p.addUserDebugLine([0, 0, 0], [0, line_dist, 0], [0, 1, 0], parentObjectUniqueId=rID, parentLinkIndex=-1,
                       lineWidth=line_width)
    p.addUserDebugLine([0, 0, 0], [0, 0, line_dist], [0, 0, 1], parentObjectUniqueId=rID, parentLinkIndex=-1,
                       lineWidth=line_width)

def reset_hand(rID=None, rPos=(0, 0, -init_grasp_distance), rOr=(0, 0, 0, 1), fixed=True):
    """
    move the hand back to the starting pos
    """

    if rID is not None:
        p.removeBody(rID)

    rID = p.loadURDF(robot_path, basePosition=rPos, baseOrientation=rOr, useFixedBase=fixed, globalScaling=1)
    if debug_lines:
        add_debug_lines(rID)

    return rID

def reset_ob(oID=None, oPos=(0, 0, 0), fixed=True):
    """
    reset by deleting
    """
    if oID is not None:
        p.removeBody(oID)

    oID = p.loadURDF(object_path, oPos, globalScaling=object_scale, useFixedBase=fixed)

    return oID

def clean_up(rID):
    """
    remove robot + all associated debug feedback
    """
    p.removeBody(rID)
    p.removeAllUserDebugItems()

"""#####################################################################################################################
                                            HAND ORIENTATION + LOCATION
#####################################################################################################################"""

#TODO: make these more user specifiable (range of angles w reasonable defaults)

def hand_dist(oID, rID, pos, oren):
    """
    actually does tthe movement to have hand touch object to judge distance

    returns position of the hand when it touches the object
    """
    # print("reset hand for non-fixed base")
    reset_hand(rID, rPos=pos, rOr=oren, fixed=False)
    relax(rID)  # want fingers splayed to get distance
    force_vector = np.array(pos)*-speed_find_distance

    has_contact = 0
    while not has_contact:  # while still distance between hand/object
        p.applyExternalForce(rID, -1, force_vector, pos, p.WORLD_FRAME)
        p.stepSimulation()
        has_contact = len(p.getContactPoints(rID, oID))
    t_pos, t_oren = p.getBasePositionAndOrientation(rID)
    clean_up(rID)
    return t_pos  # only need the position of the object

def adjust_point_dist(theta_rad, phi_rad, rID, oID, carts, quat):
    """
    move the hand w/fingers splayed until it touches the object
    should touch in center/palm - this should be the best for an initial grasp

    returns set of position coordinates representing how far from the object the hand should be (touching + a margin)
    """

    t_pos = hand_dist(oID, rID, carts, quat)
    t_dist = distance.euclidean(t_pos, [0,0,0])
    m_dist = t_dist + grasp_distance_margin
    carts = astropy.coordinates.spherical_to_cartesian(m_dist, theta_rad, phi_rad)
    flip_carts = np.array(carts)*-1 #adjust to face obj

    return flip_carts

def get_given_point(dist, theta_rad, phi_rad, rID, oID):
    """
    For the transform3d euler to quat: (seems like their z is our x, their y is our y, their x is our z)
    Rotate about the current z-axis by ϕ. Then, rotate about the new y-axis by θ
    """
    carts = astropy.coordinates.spherical_to_cartesian(dist, theta_rad, phi_rad)
    flip_carts = np.array(carts)*-1 #adjust to face obj
    quat = euler.euler2quat(phi_rad + pi, pi / 2 - theta_rad, pi, axes='sxyz') #pi in the z to face "up"
    close_carts = adjust_point_dist(theta_rad, phi_rad, rID, oID, flip_carts, quat) #find dist to grasp

    return (close_carts, quat)

def sphere_set(rID, oID, phi_init = pi, phi_span = 2*pi, theta_init = pi/2, theta_span = pi/2):
    """
    move the hand around the object in a reasonable way
    returns an array of (position, orientation) pairs
    """
    set = []

    phi = phi_init
    increment_phi = phi_span/num_grasps_per_cycle

    theta = theta_init
    increment_theta = theta_span/num_cycles_to_grasp

    for theta_i in range(0, (num_cycles_to_grasp+1)):
        for phi_i in range(0, (num_grasps_per_cycle)):
            point = get_given_point(dist=init_grasp_distance,
                                    theta_rad=(-theta) + increment_theta * theta_i,
                                    phi_rad=(-phi) + increment_phi * phi_i,
                                    rID=rID, oID=oID)
            set.append(point)

    return set

def rand_set(rID, oID, dist=init_grasp_distance, n=10):
    """
    get n pairs for the hand randomly distributed dist away from the origin
    returns an array of (position, orientation) pairs
    TODO: make this more uniformly distributed https://www.cmu.edu/biolphys/deserno/pdf/sphere_equi.pdf
    """
    set = []

    for i in range(n):
        theta_rad, phi_rad = rand_coord()
        set.append(get_given_point(dist, theta_rad, phi_rad, rID, oID))

    return set

def wrist_rotations(pose):
    """
    rotates the wrist of hand in place, increasing number of grasp possibilities for one position
    """

    rotated_poses = []
    point = pose[0]
    quat = pose[1]
    # change to from xyzw to wxyz
    quat_w = quat[3]
    quat_x = quat[0]
    quat_y = quat[1]
    quat_z = quat[2]
    current_quat = Quaternion(quat_w, quat_x, quat_y, quat_z)
    rot_iter = (2*pi)/num_wrist_rotations
    for i in range(0, num_wrist_rotations):
        rot_quat = Quaternion(axis=np.array(point)*-1, radians=(pi/2)+(rot_iter * i))
        delta_quat = rot_quat * current_quat
        pyb_quat = (delta_quat[1], delta_quat[2], delta_quat[3], delta_quat[0])
        rotated_poses.append((point, pyb_quat))
    return rotated_poses

"""#####################################################################################################################
                                            GRIPPER FUNCTIONS/MOVEMENT
#####################################################################################################################"""

def grasp(handId):
    """
    closes the gripper uniformly + attempts to find a grasp
    this is based on time + not contact points because contact points could just be a finger poking the object
    relies on grip_joints - specified by user/config file which joints should close
    """
    finish_time = time() + grasp_time_limit
    while time() < finish_time:
        p.stepSimulation()
        for joint in active_grasp_joints:
            p.setJointMotorControl2(bodyUniqueId=handId, jointIndex=joint, controlMode=p.VELOCITY_CONTROL,
                                    targetVelocity=target_grasp_velocity, force=max_grasp_force)

def relax(rID):
    """
    return all joints to neutral/furthest extended, based on urdf specification
    """
    joint = 0
    num = p.getNumJoints(rID)
    while joint < num:
        p.resetJointState(rID, jointIndex=joint, targetValue=0.0)
        joint = joint + 1

"""#####################################################################################################################
                                        POSITION/ORIENTATION DATA - GRAP MEMORY
#####################################################################################################################"""

class Grasp:

    def __init__(self, robot_position, robot_orientation, robot_joints,
                 final_object_position, final_object_orientation, vol, ep):
        self.robot_pose = (robot_position, robot_orientation)
        self.robot_joints = robot_joints
        self.final_object_pose = (final_object_position, final_object_orientation)
        self.vol = vol
        self.ep = ep

    def __repr__(self):
        return "ROBOT: Pose: " + str(self.robot_pose) + " , Joints: " + str(self.robot_joints) + \
               " OBJECT: Pose: " + str(self.final_object_pose) + \
               " QUALITY: Volume" + str(self.vol)+ " , Epsilon: " + str(self.vol) + " "

    def __str__(self):
        return "ROBOT: Pose: " + str(self.robot_pose) + " , Joints: " + str(self.robot_joints) + \
               " OBJECT: Pose: " + str(self.final_object_pose) + \
               " QUALITY: Volume" + str(self.vol)+ " , Epsilon: " + str(self.vol) + " "

def get_robot_config(rID, oID):
    r_pos, r_oren = p.getBasePositionAndOrientation(rID)
    joints = {}
    num = p.getNumJoints(rID)
    for joint in range(0, num):
        joints[joint] = p.getJointState(rID, joint)
    o_pos, o_oren = p.getBasePositionAndOrientation(oID)
    vol, ep = grip_qual(rID,oID)
    return Grasp(r_pos, r_oren, joints, o_pos, o_oren, vol, ep)

"""#####################################################################################################################
                                            GRASP EVALUATION FUNCTIONS
#####################################################################################################################"""

def check_grip(oID, rID):
    """
    check grip by adding in gravity
    """
    #print("checking strength of current grip")
    mag = 1
    pos, oren = p.getBasePositionAndOrientation(rID)
    time_limit = .5
    finish_time = time() + time_limit
    p.addUserDebugText("Grav Check!", [-.07, .07, .07], textColorRGB=[0, 0, 1], textSize=1)
    while time() < finish_time:
        p.stepSimulation()
        p.applyExternalForce(oID, linkIndex=-1, forceObj=[0, 0, -mag], posObj=pos, flags=p.WORLD_FRAME)
    contact = p.getContactPoints(oID, rID)  # see if hand is still holding obj after gravity is applied
    if len(contact) > 0:
        p.removeAllUserDebugItems()
        p.addUserDebugText("Grav Check Passed!", [-.07, .07, .07], textColorRGB=[0, 1, 0], textSize=1)
        print("Grav Check Passed")
        sleep(.2)
        return get_robot_config(rID, oID)
    else:
        p.removeAllUserDebugItems()
        p.addUserDebugText("Grav Check Failed!", [-.07, .07, .07], textColorRGB=[1, 0, 0], textSize=1)
        print("Grav Check Failed")
        sleep(.2)
        return None

def grip_qual(oID, rID):
    """
    evaluate the grasp quality
    """
    contact = p.getContactPoints(oID, rID)  # see if hand is still holding obj after gravity is applied
    if len(contact) > 0:
        force_torque = gws_pyramid_extension(rID, oID)
        #print("force_torque: ", force_torque)
        #print("force_torque shape: ", np.array(force_torque).shape)
        vol = volume(force_torque)
        #print("volume: ", vol)
        ep = eplison(force_torque)
        #print("eplison: ", ep)
    else:
        vol = None
        ep = None
    return vol, ep

def get_obj_info(oID): #TODO: what about not mesh objects?
    """
    get object data to figure out how far away the hand needs to be to make its approach
    """
    obj_data = p.getCollisionShapeData(oID, -1)[0]
    geometry_type = obj_data[2]
    #print("geometry type: " + str(geometry_type))
    dimensions = obj_data[3]
    #print("dimensions: "+ str(dimensions))
    local_frame_pos = obj_data[5]
    #print("local frome position: " + str(local_frame_pos))
    local_frame_orn = obj_data[6]
    #print("local frame oren: " + str(local_frame_orn))
    diagonal = sqrt(dimensions[0]**2+dimensions[1]**2+dimensions[2]**2)
    #print("diagonal: ", diagonal)
    max_radius = diagonal/2
    return local_frame_pos, max_radius

def gws(rID, oID):
    """
    calculate force/torque vectors for use in evaluation

    """
    print("eval gws")
    local_frame_pos, max_radius = get_obj_info(oID)
    #sim uses center of mass as a reference for the Cartesian world transforms in getBasePositionAndOrientation
    obj_pos, obj_orn = p.getBasePositionAndOrientation(oID)
    force_torque = []
    contact_points = p.getContactPoints(rID, oID)
    for point in contact_points:
        contact_pos = point[6]
        normal_vector_on_obj = point[7]
        normal_force_on_obj = point[9]
        force_vector = np.array(normal_vector_on_obj)*normal_force_on_obj

        radius_to_contact = np.array(contact_pos) - np.array(obj_pos)
        torque_numerator = np.cross(radius_to_contact, force_vector)
        torque_vector = torque_numerator/max_radius

        force_torque.append(np.concatenate([force_vector, torque_vector]))

    return force_torque

def get_new_normals(force_vector, normal_force, sides, radius):
    """
    utility function to help with GWS/pyramid extension for contact points
    """
    return_vectors = []
    #get arbitrary vector to get cross product which should be orthogonal to both
    vector_to_cross = np.array((force_vector[0]+1,force_vector[1]+2,force_vector[2]+3))
    orthg = np.cross(force_vector, vector_to_cross)
    orthg_vector = (orthg/np.linalg.norm(orthg))*radius
    rot_angle = (2*pi)/sides
    split_force = normal_force/sides

    for side_num in range(0,sides):
        rotated_orthg = Quaternion(axis=force_vector, angle=(rot_angle*side_num)).rotate(orthg_vector)
        new_vect = force_vector+np.array(rotated_orthg)
        norm_vect = (new_vect/np.linalg.norm(new_vect))*split_force
        return_vectors.append(norm_vect)

    return return_vectors

def gws_pyramid_extension(rID, oID, pyramid_sides=force_pyramid_sides, pyramid_radius=force_pyramid_radius):
    #often dont have enough contact points to create a qhull of the right dimensions, so create more that are very close to the existing ones
    local_frame_pos, max_radius = get_obj_info(oID)
    #sim uses center of mass as a reference for the Cartesian world transforms in getBasePositionAndOrientation
    obj_pos, obj_orn = p.getBasePositionAndOrientation(oID)
    force_torque = []
    contact_points = p.getContactPoints(rID, oID)
    for point in contact_points:
        contact_pos = point[6]
        normal_vector_on_obj = point[7]
        normal_force_on_obj = point[9]
        force_vector = np.array(normal_vector_on_obj)*normal_force_on_obj
        if np.linalg.norm(force_vector) > 0:
            new_vectors = get_new_normals(force_vector, normal_force_on_obj, pyramid_sides, pyramid_radius)

            radius_to_contact = np.array(contact_pos) - np.array(obj_pos)

            for pyramid_vector in new_vectors:
                torque_numerator = np.cross(radius_to_contact, pyramid_vector)
                torque_vector = torque_numerator/max_radius
                force_torque.append(np.concatenate([pyramid_vector, torque_vector]))

    return force_torque

def volume(force_torque):
    """
    get qhull of the 6 dim vectors [fx, fy, fz, tx, ty, tz] created by gws (from contact points)
    get the volume
    """
    vol = ConvexHull(points = force_torque)
    return vol.volume

def eplison(force_torque):
    """
    get qhull of the 6 dim vectors [fx, fy, fz, tx, ty, tz] created by gws (from contact points)
    get the distance from centroid of the hull to the closest vertex
    """
    hull = ConvexHull(points=force_torque)
    centroid = []
    for dim in range(0,6):
        centroid.append(np.mean(hull.points[hull.vertices, dim]))
    shortest_distance = 500000000
    closest_point = None
    for point in force_torque:
        point_dist = distance.euclidean(centroid, point)
        if point_dist < shortest_distance:
            shortest_distance = point_dist
            closest_point = point

    return shortest_distance

"""#####################################################################################################################
                                        MAIN MAIN MAIN MAIN MAIN MAIN
#####################################################################################################################"""

print("grasp!")
rID = reset_hand()
oID = reset_ob()

print("Sphere Set")
hand_set = sphere_set(rID=rID, oID=oID)
#hand_set = rand_set(rID=handID, oID=cubeID, n=45)
print(hand_set)

rID = reset_hand()
oID = reset_ob(oID, [0, 0, 0])

good_grips = []

pos = 0
for pose in hand_set:
    poses = []
    poses.append(pose)
    if use_wrist_rotations:
        rotated_poses = wrist_rotations(pose)
        poses = poses + rotated_poses

    for pose in poses:
        print(" ")
        print("Pose #: ", pos)
        relax(rID)
        p.removeAllUserDebugItems()
        p.resetBasePositionAndOrientation(rID, pose[0], pose[1])
        if debug_lines:
            add_debug_lines(rID)
        oID = reset_ob(oID, [0, 0, 0], fixed=False)
        grasp(rID)
        vol, ep = grip_qual(oID, rID)
        print("Volume: ", vol)
        print("Epslion: ", ep)
        good_grips.append(check_grip(oID, rID))
        pos += 1

print("Num Good Grips: ", len(good_grips))
print("Grips:")
for grip in good_grips:
    print(grip)

max_stars_repo_name: planetceres/pybullet-grasp

max_issues_repo_name: planetceres/pybullet-grasp

max_forks_repo_name: planetceres/pybullet-grasp

reason: import numpy,from scipy,import astropy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 19 22:55:35 2020

@author: jingci
"""

import numpy as np
import pandas as pd
import pickle
from RLBrain import RLBrain

'''
Q-learning models for learning the position of target
'''
class QLearningTable1(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list

        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'q_table1.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            #The q_table after path training
            f = open(RLBrain.FILEPATH + 'path_qtable1.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()

class QLearningTable2(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list

        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'q_table2.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            #The q_table after path training
            f = open(RLBrain.FILEPATH + 'path_qtable2.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()

class QLearningTable3(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list

        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'q_table3.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            #The q_table after path training
            f = open(RLBrain.FILEPATH + 'path_qtable3.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()

'''
Q-learning models for learning the position of starting point
'''
class ReturnQLearningTable1(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list
        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'Return_q_table1.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            #The q_table after path training
            f = open(RLBrain.FILEPATH + 'Return_q_table1.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()

class ReturnQLearningTable2(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list
        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'Return_table2.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            #The q_table after path training
            f = open(RLBrain.FILEPATH + 'Return_qtable2.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()

class ReturnQLearningTable3(RLBrain):
    def __init__(self, actions, state):
        self.actions = actions  # a list
        if state == "RAW":
            #The q_table without previous knowledge
            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float64)
        elif state == "MAP":
            #The q_table after map training
            f = open(RLBrain.FILEPATH + 'Return_table3.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()
        elif state == "PATH":
            f = open(RLBrain.FILEPATH + 'Return_qtable3.txt', 'rb')
            self.q_table = pickle.load(f)
            f.close()


text:
"""
This module contains functions that are strictly the backbone of many
of the modeling and fitting functionality.
"""

import astropy as ap
import astropy.modeling as ap_mod
import numpy as np
import numpy.ma as np_ma

import ifa_smeargle.core as core

def fit_gaussian_function(x_data, y_data, inital_guesses):
    """ This function fits a Gaussian function to a specific set of
    data.

    Gaussian fitting is hard, this function exists as a port so that
    all fitting functions use the same algorithm and said algorithm
    is easy to change.

    Parameters
    ----------
    x_data : ndarray
        The X data that the Gaussian will be fit over.
    y_data : ndarray
        The Y data that the Gaussian will be fit over.
    inital_guesses : dictionary
        Initial guesses for the Gaussian fitting function. The
        allowed inputs and their keys are:

            * 'mean' : The initial guess of the Gaussian function's
                       mean. Defaults to 0.
            * 'studded' : The initial guess of the Gaussian
                          function's standard deviation.
                          Defaults to 1.
            * 'amplitude' : The initial guess of the Gaussian
                            function's  amplitude. Defaults to 1.
    Returns
    -------
    gaussian_function : function
        A callable function that when provided an X value, it
        will return the value of the function.
    gaussian_parameters : dictionary
        A compiled dictionary of all of the parameters of the
        Gaussian fit.
    """

    # Ensure that the x_data and y_data are the same length.
    if (len(x_data) != len(y_data)):
        raise core.error.InputError("The x-data and y-data arrays must be "
                                    "parallel arrays; thus, their sizes "
                                    "must be the same.")

    # Extract the initial guesses.
    inital_guesses = dict(inital_guesses)
    guess_mean = inital_guesses.get('mean', 0)
    guess_stddev = inital_guesses.get('stddev', 1)
    guess_amplitude = inital_guesses.get('amplitude', 1)

    # Plotting/fitting the Gaussian function. For some reasons beyond
    # what Sparrow can explain, Astropy seems to have better fitting
    # capabilities, in this specific application, than Scipy.
    gaussian_init = ap_mod.models.Gaussian1D(
        amplitude=guess_amplitude, mean=guess_mean, stddev=guess_stddev)
    gaussian_fit_model = ap_mod.fitting.LevMarLSQFitter()
    # If the number of data points is less than three, a model cannot
    # be reasonably fit as it is over-fit.
    try:
        gaussian_fit = gaussian_fit_model(gaussian_init, x_data, y_data)
    except:
        raise core.error.DataError("The number of data points is {num}. "
                                   "This is likely less than the number of "
                                   "fitting parameters (3). The data cannot "
                                   "be fit."
                                   .format(num=len(x_data)))

    # Deriving basic information form Gaussian model to return back
    # to the user.
    gaussian_mean = gaussian_fit.mean.value
    gaussian_stddev = gaussian_fit.stddev.value
    gaussian_amplitude = gaussian_fit.amplitude.value
    temp_gauss_x_axis = np.linspace(np.nanmin(x_data) - 1,
                                    np.nanmax(x_data) + 1,
                                    1000 + x_data.size**2)
    gaussian_max = np.max(gaussian_fit(temp_gauss_x_axis))
    gaussian_parameters = {'mean':gaussian_mean, 'stddev':gaussian_stddev,
                           'amplitude':gaussian_amplitude, 'max':gaussian_max}

    # The Gaussian function
    gaussian_function = lambda input: gaussian_fit(np.array(input))

    return gaussian_function, gaussian_parameters

def fit_histogram_gaussian_function(data_array, bin_width):
    """ This function fits a Gaussian function to a specific set
    of data.

    Gaussian fitting is hard, this function exists as a port so that
    all fitting functions use the same algorithm and said algorithm
    is easy to change. This applies it to the histogram of the data.

    Parameters
    ----------
    data_array : ndarray
        The data that the histogram Gaussian function is fitting.
    bin_width : float
        The width of the bins to use for the histogram fitting
        function.

    Returns
    -------
    gaussian_function : function
        A callable function that when provided an X value, it will
        return the value of the function.
    gaussian_parameters : dictionary
        A compiled dictionary of all of the parameters of the
        Gaussian fit.
    """

    # Be able to accept both masked arrays and standard arrays and
    # be able to tell.
    if (np_ma.is_masked(data_array)):
        flat_data = data_array.compressed()
    else:
        flat_data = data_array.flatten()

    # Numpy does not support histogram bin widths, instead using
    # bins defined by values in an array. Converting equal bin
    # widths to this array.
    hist_bins = core.math.generate_numpy_bin_width_array(data_array=flat_data,
                                                         bin_width=bin_width)

    # Extract histogram data from the data. The x locations are in
    # the middle of the bin.
    hist_data = np.histogram(flat_data, bins=hist_bins)
    hist_x = (hist_data[1][0:-1] + hist_data[1][1:]) / 2
    hist_y = hist_data[0]

    # Determine the initial guesses of the Gaussian histogram
    # fit. So far magic is the best way.
    guess_mean, guess_stddev, guess_amplitude = \
        core.magic.magic_inital_gaussian_parameters(x_data=hist_x,
                                                    y_data=hist_y)

    # Do the Gaussian fit.
    inital_guesses = {'mean': guess_mean,
                      'stddev': guess_stddev,
                      'amplitude': guess_amplitude}
    gauss_funct, gauss_param = fit_gaussian_function(hist_x, hist_y,
                                                     inital_guesses)
    # For naming convention.
    gaussian_function = gauss_funct
    gaussian_parameters = gauss_param
    return gaussian_function, gaussian_parameters

reason: import numpy,import astropy


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import _init_paths

import os
import sys
import cv2
import json
import copy
import numpy as np
from src.lib.opts_mayank_mutlitask import opts
from src.lib.detector_multitask import Detector
score_thres=0.1
saving_path='/home/mayank_s/Desktop/centertrack/demo'
image_ext = ['jpg', 'jpeg', 'png', 'webp']
video_ext = ['mp4', 'mov', 'avi', 'mkv']
time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge', 'display']

import pandas as pd
# class_name = ['car', 'truck', 'bus', 'trailer', 'construction_vehicle', 'pedestrian', 'motorcycle', 'bicycle', 'traffic_cone', 'barrier']

class_name = ["person", "rider", "car", "bus", "truck", "bike", "motor", "traffic light", "traffic sign", "train"]

show = True

bblabel = []

def demo(opt):
  os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str
  opt.debug = max(opt.debug, 1)
  detector = Detector(opt)
  bblabel = []
  if opt.demo == 'webcam' or \
    opt.demo[opt.demo.rfind('.') + 1:].lower() in video_ext:
    is_video = True
    # demo on video stream
    cam = cv2.VideoCapture(0 if opt.demo == 'webcam' else opt.demo)
  else:
    is_video = False
    # Demo on images sequences
    if os.path.isdir(opt.demo):
      image_names = []
      ls = os.listdir(opt.demo)
      for file_name in sorted(ls):
          ext = file_name[file_name.rfind('.') + 1:].lower()
          if ext in image_ext:
              image_names.append(os.path.join(opt.demo, file_name))
    else:
      image_names = [opt.demo]

  # Initialize output video
  out = None
  out_name = opt.demo[opt.demo.rfind('/') + 1:]
  print('out_name', out_name)
  # if opt.save_video:
  #   # fourcc = cv2.VideoWriter_fourcc(*'XVID')
  #   fourcc = cv2.VideoWriter_fourcc(*'H264')
  #   out = cv2.VideoWriter('../results/{}.mp4'.format(
  #     opt.exp_id + '_' + out_name),fourcc, opt.save_framerate, (
  #       opt.video_w, opt.video_h))

  if opt.debug < 5:
    detector.pause = False
  cnt = -1
  results = {}

  for img_path in image_names:
      img = cv2.imread(img_path)
      # cv2.imshow('input', img)

      # track or detect the image.
      ret = detector.run(img)
      ############################33333
      ############################33333

      time_str = 'frame {} |'.format(cnt)
      for stat in time_stats:
          time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat])
      print(time_str)
      ######################################
      resu = ret['results']
      for data in resu:
          # print(resu[data])
          arr=data['bbox']
          # $$$$$$$$$$$$$$$$$$$$$$
          xmin = int(arr[0])
          ymin = int(arr[1])
          xmax = int(arr[2])
          ymax = int(arr[3])
          score = float(data["score"])
          cls = int(data["class"])
          width = img.shape[1]
          height = img.shape[0]
          if score<score_thres:
              continue

          object_name=class_name[cls-1]
          # image_name="cool"
          # print(image_names)
          # image_name = (image_names[cnt-1].split("/")[-1])
          image_name = (img_path.split("/")[-1])

          data_label = [image_name, width, height, object_name, xmin, ymin, xmax, ymax, score]
          # data_label = [file_name, width, height, object_name, xmin, ymin, xmax, ymax]
          if not ((xmin == xmax) and (ymin == ymax)):
              if not xmin<0 or ymin<0 or xmax >width or ymax >height:
                 bblabel.append(data_label)
                  # print(file_name)
                  # print()

          top = (xmin,ymax)
          bottom = (xmax, ymin)
          cv2.rectangle(img, pt1=top, pt2=bottom, color=(0, 255, 0), thickness=2)
          result_info=str(object_name)+str(round(score,2))
          # cv2.putText(image_scale, y[read_index][4], ((y[read_index][0]+y[read_index][2])/2, y[read_index][1]), cv2.CV_FONT_HERSHEY_SIMPLEX, 2, 255)
          cv2.putText(img, str(result_info), (int((xmin +xmax) / 2), ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 0), lineType=cv2.LINE_AA)
          # cv2.putText(img, str(object_name), (int((xmin +xmax) / 2), ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 0), lineType=cv2.LINE_AA)
          # cv2.putText(img, (str(round(score,2))), (int((xmin +xmax) / 3), ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 0), lineType=cv2.LINE_AA)
      # else:
      #     1
      # print('cool2')
      if show:
          cv2.imshow('streched_image', img)
          ch = cv2.waitKey(10)
          if ch & 0XFF == ord('q'):
              cv2.destroyAllWindows()
          # cv2.waitKey(1)
          # cv2.destroyAllWindows()
          # output_path = saving_path + ((image_names[cnt].split("/")[-1])
          # image_name= (image_names[cnt].split("/")[-1])
          output_path = os.path.join(saving_path, image_name)
          cv2.imwrite(output_path, img)

  print("cool")
  columns = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax', 'score']
  df = pd.DataFrame(bblabel, columns=columns)
  # df.to_csv('centertrack_bdd_prediction_val.csv', index=False)

if __name__ == '__main__':
  opt = opts().init()
  # opt = opts().parse()
  demo(opt)


text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
       _
      /  |     | __  _ __  _
     /   |    /  |_||_|| ||
    /    |   /   |  |\ | ||_
   /____ |__/\ . |  | \|_|\_|
   __________________________ .

Created on Mon Nov 12 14:52:58 2018

@author: chrisunderwood
    Produce the spectrum of electrons from the final sdf dump
"""

import argparse
import numpy as np
import os
import matplotlib.pyplot as plt
import sdf_helper as sh
import matplotlib as mpl
#from matplotlib.colors import colorConverter
import matplotlib.colors as colors
import matplotlib.gridspec as gridspec
from matplotlib import ticker

#==============================================================================
# Define global constants
#==============================================================================
q_e = 1.6e-19
m_e = 9.11e-31

def FilesInFolder(DirectoryPath, splice):
    files = os.listdir(DirectoryPath)
    shots = []
    inputDeck = []
    for i in files:
        if not i.startswith('.') and i.endswith('.sdf'):
            shots.append(i)
        if not i.startswith('.') and i.endswith('.deck'):
            inputDeck.append(i)

    # Sort
    timeStep = []
    for i in range(len(shots)):
#        print shots[i], '\t',shots[i][splice[0]:splice[1]]
        timeStep.append(int(shots[i][splice[0]:splice[-1]]))

    timeStep = np.asarray(timeStep)
    sorted_points = sorted(zip(timeStep, shots))
    timeStep = [point[0] for point in sorted_points]
    sdf_list = [point[1] for point in sorted_points]

    return sdf_list, timeStep, inputDeck

def stepFolders(folderPath):
    scratchPath = '/work/scratch/scarf585/' + folderPath
    savePath = '/home/clfg/scarf585/' + folderPath
    #Create folder to save into
    if not os.path.exists(savePath):
        os.makedirs(savePath)

    sdf_list, simTimeSteps, inputDeck = FilesInFolder(scratchPath, [0, -4])
    print '    Files in folder'
    print scratchPath
    print 'File Names list:', sdf_list
    print
    print 'Time Step ints: ', simTimeSteps
    if len(inputDeck) == 1:
        import shutil
        shutil.copy2(scratchPath+inputDeck[0], savePath)

    return scratchPath, savePath, sdf_list, simTimeSteps

def spectrumPlot(grid, spec, time,  savePath, number):
    plt.plot(grid, spec)
    plt.xlabel('Momentum')
    plt.ylabel('Number of Electrons')
    plt.title('Simulation time {:.2f}ps'.format(time*1e12))
    plt.savefig(savePath +  number + 'FinalMomentumSpec.png')
    plt.clf()

    plt.plot(((grid**2 / (2 * m_e)) / q_e) * 1e-6, spec)
    plt.title('Simulation time {:.2f}ps'.format(time*1e12))
    plt.xlabel('Energy (MeV)')
    plt.ylabel('Number of Electrons')
    plt.savefig(savePath + number + 'FinalEnergySpec.png')
    plt.clf()

#==============================================================================
# Get the file input from the cmd line
#==============================================================================
parser = argparse.ArgumentParser()
parser.add_argument("--folder", "-f", type=str, required=True)
parser.add_argument("--doSDF_N", "-n", type=str, required=False)

folderPath = str(parser.parse_args().folder)
scratchPath, savePath, sdf_list, simTimeSteps = stepFolders(folderPath)

def produceSpectrum(sdf):
    # Load the last sdf file
    print scratchPath + sdf
    d = sh.getdata(scratchPath + sdf)
    px = d.dist_fn_x_px_electron.data
    grid = d.dist_fn_x_px_electron.grid.data
    t = d.Header['time']

    if len(np.shape(px)) == 3:
        print 'ERROR IN PX FILE ---- Too many dimensions'

    spectrum = np.sum(px, axis = 0)
    spectrumPlot(grid[1] , spectrum, t, savePath, sdf[:-4])
    np.savetxt(savePath + sdf[:-4] + 'spectrum.txt',np.c_[grid[1], spectrum])

# If option -n <Number> is used
if parser.parse_args().doSDF_N is not None:
    produceSpectrum(sdf_list[int(parser.parse_args().doSDF_N)])
else:
    produceSpectrum(sdf_list[-1])

max_stars_repo_name: chunders/EpochAnalysis

max_issues_repo_name: chunders/EpochAnalysis

max_forks_repo_name: chunders/EpochAnalysis