text:
#!/usr/bin/env python
#-*- coding: utf8 -*-
from __future__ import division
import numpy as np
import os
import datetime
import support
from svm.lib_svm.svmutil import *

class Indicator:
	def __init__(self, values):
		[self.stock_index, self.amount, self.tendency, self.MACD, self.KDJK, self.KDJD, self.KDJJ] = values
		self.normalize(10)

	def normalize(self, coefficient):
		self.stock_index = str(int(float(self.stock_index) / coefficient))
		self.amount = str(int(float(self.amount) / coefficient))
		self.tendency = str(int(float(self.tendency) * coefficient))
		self.MACD = str(int(float(self.MACD) / coefficient))
		self.KDJK = str(int(float(self.KDJK) / coefficient))
		self.KDJD = str(int(float(self.KDJD) / coefficient))
		self.KDJJ = str(int(float(self.KDJJ) / coefficient))

	def to_list(self):
		return ["stockindex:" + self.stock_index, "amount" + self.amount, "tendency" + self.tendency, "MACD" + self.MACD, "KDJK" + self.KDJK, "KDJD" + self.KDJD, "KDJJ" + self.KDJJ]

class Sentiment:
	def __init__(self, values):
		[self.lex_positive, self.lex_negative, self.lex_calm, self.SVM_positve, self.SVM_negative] = values

	def to_list(self):
		return ["lexpos" + str(self.lex_positive), "lexneg" + str(self.lex_negative), "lexcalm" + str(self.lex_calm), "svmpos" + str(self.SVM_positve), "svmneg" + str(self.SVM_negative)]

	def display(self):
		print self.lex_positive, self.lex_negative, self.lex_calm, self.SVM_positve, self.SVM_negative

class DataPair:
	def __init__(self, features, stock_index):
		self.stock_index = stock_index
		self.features = features

def create_matrix(root_dir, input_file_name, real_file_name):
	# root_dir:	root_dir
	# file_name: source file
	# rtype: matrix of input data

	matrix = {}
	with open(root_dir + '/' + input_file_name, 'r') as input_file, \
		open(root_dir + '/' + real_file_name, 'r') as real_file:

		for line in real_file:
			key, value = line.rstrip().split(':')
			values = map(float, value.split())
			matrix[key] = [[values[0]]]

		for line in input_file:
			key, value = line.rstrip().split(':')
			date = datetime.datetime.strptime(key, "%Y-%m-%d")
			date += datetime.timedelta(days=1)
			date_str = date.strftime("%Y-%m-%d")
			if date_str in matrix:
				matrix[date_str] += [map(float, value.split())]
	input_matrix = [matrix[key][1] for key in sorted(matrix)]
	output_matrix = [matrix[key][0] for key in sorted(matrix)]

	return input_matrix, output_matrix

def get_coeffient(input_matrix, output_matrix):
	# input_matrix: X
	# output_matrix: Y
	# y = f(x)
	# rtype: coeffient

	x = np.matrix(input_matrix, dtype=np.float64)
	y = np.matrix(output_matrix, dtype=np.float64)
	coeffient = (x.T * x).I * x.T * y
	e = y - x * coeffient
	mean_e = e.sum() / e.size
	coeffient = np.vstack((coeffient, [mean_e]))
	return coeffient

def predicate(coeffient, input_matrix):
	# coeffient: a
	# input_matrix: x
	# y = ax
	# rtype: predicate value

	x = np.matrix(input_matrix, dtype=np.float64)
	y = x * coeffient[:-1] + coeffient[-1]
	return y

def linear_regression(root_dir, input_file, output_file, train_sample_length):
	# root_dir: root_dir
	# input_file: file contains x
	# output_file: file contains y
	# train_sample_length: how many sample to train
	# rtype: return predicate value

	input_matrix, output_matrix = create_matrix(root_dir, input_file, output_file)
	coeffient = get_coeffient(input_matrix[:train_sample_length], output_matrix[:train_sample_length])
	return predicate(coeffient, input_matrix[train_sample_length:])

def generate_top_k_words(root_dir, target_file_name, num_words):
	# root_dir: root_dir
	# target_file_name: source file
	# num_words: top k

	directories = support.get_subdirs(root_dir)
	word_count = {}
	for directory in directories:
		with open(directory + '/' + target_file_name, 'r') as file:
			for line in file:
				words = line.strip().split()
				for word in words:
					word_count[word] = word_count.get(word, 0) + 1

	return set(sorted(word_count, key=word_count.get)[:num_words])

def generate_sentiment_words(sentiment_lexicon_file):
	# sentiment_lexicon_file: setiment_file_path

	with open(sentiment_lexicon_file, 'r') as file:
		return [line.strip().split()[0] for line in file]

def generate_tech_feature(root_dir, tech_file_name, group_window_length):
	# root_dir: root
	# tech_file_name: tech_file_path
	# group_window_length: group the tech figures

	original_tech_feature = {}
	with open(root_dir + '/' + tech_file_name, 'r') as real_file:
		for line in real_file:
			key, value = line.rstrip().split(':')
			values = map(float, value.split())
			tech = Indicator(values)
			original_tech_feature[key] = tech.to_list()
	tech_feature = {}
	orginal_keys = sorted(original_tech_feature.keys())
	for index, date in enumerate(orginal_keys[group_window_length-1:]):
		tech_feature[date] = [str(index+1) + value for index, key in enumerate(orginal_keys[index:index+group_window_length]) for value in original_tech_feature[key] ]
	return tech_feature

def generate_sentiment_feature(root_dir, sentiment_result_file):
	# root_dir: root_dir
	# sentiment_result_file: sentiment results

	sentiment_feature = {}
	with open(root_dir + '/' + sentiment_result_file, 'r') as sentiment_file:
		for line in sentiment_file:
			date, values = line.rstrip().split(':')
			sentiment_feature[date] = Sentiment(map(float, values.split())).to_list()
	return sentiment_feature

def generate_sentiment_word_feature(root_dir, target_file_name):
	# root_dir: root_dir
	# target_file_name: text content file

	sentiment_words = generate_sentiment_words("./lexicon/dictionary.txt")
	directories = support.get_subdirs(root_dir)

	sentiment_word_feature = {}
	for directory in directories:
		key = directory.split('/')[-1]
		date = key[:4] + '-' + key[4:6] + '-' + key[6:8]
		with open(directory + '/' + target_file_name, 'r') as file:
			raw_words = set(map(str.rstrip, file.read().split()))
			words = [word for word in raw_words if word in sentiment_words]
		sentiment_word_feature[date] = words

	return sentiment_word_feature

def generate_word_feature(root_dir, target_file_name, num_words):
	# root_dir: root
	# target_file_name: content txt file
	# num_words: top K words

	frequent_words = generate_top_k_words(root_dir, target_file_name, num_words)
	directories = support.get_subdirs(root_dir)

	word_feature = {}
	for directory in directories:
		key = directory.split('/')[-1]
		date = key[:4] + '-' + key[4:6] + '-' + key[6:8]
		with open(directory + '/' + target_file_name, 'r') as file:
			raw_words = set(map(str.rstrip, file.read().split()))
			words = [word for word in raw_words if word in frequent_words]
		word_feature[date] = words

	return word_feature

def assign_weight(feature, weight):
	# feature: feature
	# weight: weight of feature

	weighted_form = {}
	for key in feature:
		weighted_form[key] = {value:weight for value in feature[key]}
	return weighted_form

def merge_feature(tech_feature, features):
	# tech_feature: base feature to create dict
	# feature: other features map to tech_feature

	new_feature = dict(tech_feature.items())
	for feature in features:
		for key in feature:
			if key in new_feature:
				new_feature[key] = dict(new_feature[key].items() + feature[key].items())
	return new_feature

def generate_features(root_dir, sentiment_result_file, tech_file_name, target_file_name, num_words):
	# root_dir: root_dir
	# sentiment_result_file: sentiment analysis result
	# tech_file_name: tech indicator file
	# target_file_name: content text file
	# num_words: top k words

	tech_feature = generate_tech_feature(root_dir, tech_file_name, 3)
	tech_feature_weighted = assign_weight(tech_feature, 9)

	sentiment_feature = generate_sentiment_feature(root_dir, sentiment_result_file)
	sentiment_feature_weighted = assign_weight(sentiment_feature, 9)

	word_feature = generate_word_feature(root_dir, target_file_name, num_words)
	word_feature_weighted = assign_weight(word_feature, 1)

	sentiment_word_feature = generate_sentiment_word_feature(root_dir, target_file_name)
	sentiment_word_feature_weighted = assign_weight(sentiment_word_feature, 1)

	return merge_feature(tech_feature_weighted, [sentiment_feature_weighted, word_feature_weighted, sentiment_word_feature_weighted])

def generate_stock_index(root_dir, tech_file_name):
	# root_dir: root
	# tech_file_name: tech indicator

	stock_index = {}
	with open(root_dir + '/' + tech_file_name, 'r') as real_file:
		for line in real_file:
			key, value = line.rstrip().split(':')
			values = map(float, value.split())
			stock_index[key] = values[0]
	return stock_index

def generate_data_pairs(features, root_dir, tech_file_name, train_sample_length):
	# features: features
	# root_dir: root
	# tech_file_name: tech files
	# train_sample_length: top k

	stock_index = generate_stock_index(root_dir, tech_file_name)
	y_values = [stock_index[key] for key in sorted(stock_index.keys())]
	x_values = [features[key] for key in sorted(features.keys())]
	data_pairs = [DataPair(x, y) for x,y in zip(x_values, y_values)]
	return data_pairs

def convert_to_svm_input(data_pairs):
	# data_pairs: Data pair

	features = []
	for data_pair in data_pairs:
		features += data_pair.features.keys()
	features=set(features)

	lexicon={}
	for index, feature in enumerate(features):
		lexicon[feature] = index + 1

	x, y = [], []
	for data_pair in data_pairs:
		pairs = dict([(lexicon[feature], data_pair.features[feature]) for feature in data_pair.features.keys() if feature in lexicon])
		x.append(pairs)
		y.append(data_pair.stock_index)
	return [x, y]

def svm_regression(root_dir, sentiment_result_file, tech_file_name, stock_index_file_name, target_file_name, num_words, train_sample_length):
	# root_dir: root
	# sentiment_result_file: sentimen analysis file
	# tech_file_name: tech indicator file
	# stock_index_file_name: stock index
	# target_file_name: content text file
	# num_words: top k
	# train_sample_length: num of train samples

	features = generate_features(root_dir, sentiment_result_file, tech_file_name, target_file_name, num_words)
	data_pairs = generate_data_pairs(features, root_dir, stock_index_file_name, train_sample_length)

	x, y = convert_to_svm_input(data_pairs)
	train_samples = [x[:train_sample_length], y[:train_sample_length]]
	test_samples = [x[train_sample_length:], y[train_sample_length:]]

	prob = svm_problem(train_samples[1], train_samples[0])
	param = svm_parameter('-s 3 -t 0')
	model = svm_train(prob, param)
	svm_save_model('./models/svr.model',model)
	p_labels, p_acc, p_vals = svm_predict(test_samples[1], test_samples[0], model)
	values =  [round(value, 2) for value in p_labels]
	return values


text:
from features import lrf_estimator as lrfe

from dataloader import dataset_localpcd_benchmark as ds
from dataloader import spherical_voxel as sv

from models import network_s2_layer as nsl
from models import network_lrf_layer as nll

from utils import torch as utor
from utils import progress_bar

import numpy as np

from s2cnn import so3_rotation
import torch

import pdb

class CompassEstimator(lrfe.LRFEstimator):
    """
    Self orienting canonical LRF estimator
    """

    def __init__(self, path_s2, path_lrf, device, num_points, radius_support, lrf_bandwidths, lrf_features, use_equatorial_grid, softmax_temp, num_workers, size_batch, *args, **kwargs):
        super(CompassEstimator, self).__init__(*args, **kwargs)

        self.path_s2 = path_s2
        self.path_lrf = path_lrf
        self.device = device
        self.num_points = num_points
        self.radius_support = radius_support
        self.lrf_features = lrf_features
        self.lrf_bandwidths = lrf_bandwidths
        self.use_equatorial_grid = use_equatorial_grid
        self.softmax_temp = softmax_temp
        self.num_workers = num_workers
        self.size_batch = size_batch

        self.input_transformation = sv.ConvertToSphericalVoxel(bandwidth=self.lrf_bandwidths[0],
                                                        radius_support=self.radius_support,
                                                        num_radial_division=self.lrf_features[0],
                                                        num_points=self.num_points,
                                                        random_sampling=True)

        # Load S2 layer
        self.layer_s2 = nsl.S2Layer(bandwidths=self.lrf_bandwidths[0:2],
                                features=self.lrf_features[0:2],
                                use_equatorial_grid = self.use_equatorial_grid)

        utor.load_models_from_ckp(self.path_s2, self.layer_s2)

        self.layer_s2 = self.layer_s2.eval()
        self.layer_s2.to(device)

        # Load local reference frame layer
        self.layer_lrf = nll.LrfLayer(bandwidths=self.lrf_bandwidths[1:],
                                 features=self.lrf_features[1:],
                                 use_equatorial_grid=self.use_equatorial_grid,
                                 softmax_temp=self.softmax_temp)

        utor.load_models_from_ckp(self.path_lrf, self.layer_lrf)

        self.layer_lrf = self.layer_lrf.eval()
        self.layer_lrf.to(device)

    def __call__(self, cloud, indices, return_features_map=False):
        #pdb.set_trace()
        self.dataset = ds.LRFBenchmarkLPCDDataset(cloud, indices, self.radius_support, self.input_transformation)
        self.dataloader = torch.utils.data.DataLoader(self.dataset,
                                                   batch_size=self.size_batch,
                                                   shuffle=False,
                                                   num_workers=self.num_workers,
                                                   pin_memory=True,
                                                   drop_last=False)

        #pdb.set_trace()
        lrfs = np.empty((len(indices), 3, 3), dtype='float64')
        clouds = np.empty((len(indices), self.num_points, 3), dtype='float64')
        if (return_features_map):
            features_map = np.empty((len(indices), self.lrf_features[-1], self.lrf_bandwidths[-1]*2, self.lrf_bandwidths[-1]*2, self.lrf_bandwidths[-1]*2))
        else:
            features_map = None

        for i_c, data in enumerate(self.dataloader):
            #pdb.set_trace()
            points, signals, indices_kp = data #torch.Size([1, 1220, 3]), torch.Size([1, 4, 48, 48]), tensor([1,1], dtype=torch.int32)
            signals = signals.to(self.device)

            with torch.no_grad():

                batch_so3_signals = self.layer_s2(signals) #torch.Size([1, 40, 48, 48, 48])

                lrf_features_map, batch_lrfs = self.layer_lrf(batch_so3_signals)
                batch_lrfs = batch_lrfs.data.cpu().numpy()
                lrf_features_map = lrf_features_map.cpu().numpy()

            lrfs[i_c*self.size_batch : min((i_c+1)*self.size_batch, len(indices))] = batch_lrfs
            clouds[i_c*self.size_batch : min((i_c+1)*self.size_batch, len(indices))] = points
            if (return_features_map):
                features_map[i_c*self.size_batch : min((i_c+1)*self.size_batch, len(indices))] = lrf_features_map

            #torch.cuda.empty_cache() # When commented it increases performances

            # Avoid useless progress bars
            if (len(indices) > 1):
                progress_bar.print_progress(i_c+1, len(self.dataloader))

        return lrfs, clouds, features_map

    def __repr__(self):
        return self.__class__.__name__ + ' rad={0}'.format(self.radius_support)

ext: py

max_stars_repo_name: aycatakmaz/compass

max_issues_repo_name: aycatakmaz/compass

max_forks_repo_name: aycatakmaz/compass

reason: import numpy


text:
# $Id$
#
# Copyright (C) 2004-2008 Greg Landrum and Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
from rdkit import Geometry
from rdkit import Chem
import numpy
import math

# BIG NOTE: we are going assume atom IDs starting from 0 instead of 1
# for all the functions in this file. This is so that they
# are reasonably indepedent of the combicode. However when using
# with combicode the caller needs to make sure the atom IDs from combicode
# are corrected before feeding them in here.

def cross(v1, v2):
  res = numpy.array([v1[1] * v2[2] - v1[2] * v2[1], -v1[0] * v2[2] + v1[2] * v2[0],
                     v1[0] * v2[1] - v1[1] * v2[0]], numpy.double)
  return res

def findNeighbors(atomId, adjMat):
  """
  Find the IDs of the neighboring atom IDs

  ARGUMENTS:
  atomId - atom of interest
  adjMat - adjacency matrix for the compound
  """
  nbrs = []
  for i, nid in enumerate(adjMat[atomId]):
    if nid >= 1:
      nbrs.append(i)
  return nbrs

def _findAvgVec(conf, center, nbrs):
  # find the average of the normalized vectors going from the center atoms to the
  # neighbors
  # the average vector is also normalized
  avgVec = 0
  for nbr in nbrs:
    nid = nbr.GetIdx()
    pt = conf.GetAtomPosition(nid)
    pt -= center
    pt.Normalize()
    if (avgVec == 0):
      avgVec = pt
    else:
      avgVec += pt

  avgVec.Normalize()
  return avgVec

def GetAromaticFeatVects(conf, featAtoms, featLoc, scale=1.5):
  """
  Compute the direction vector for an aromatic feature

  ARGUMENTS:
     conf - a conformer
     featAtoms - list of atom IDs that make up the feature
     featLoc - location of the aromatic feature specified as point3d
     scale - the size of the direction vector
  """
  dirType = 'linear'
  head = featLoc
  ats = [conf.GetAtomPosition(x) for x in featAtoms]

  p0 = ats[0]
  p1 = ats[1]
  v1 = p0 - head
  v2 = p1 - head
  norm1 = v1.CrossProduct(v2)
  norm1.Normalize()
  norm1 *= scale
  #norm2 = norm1
  norm2 = head - norm1
  norm1 += head
  return ((head, norm1), (head, norm2)), dirType

def ArbAxisRotation(theta, ax, pt):
  theta = math.pi * theta / 180
  c = math.cos(theta)
  s = math.sin(theta)
  t = 1 - c
  X = ax.x
  Y = ax.y
  Z = ax.z
  mat = [[t * X * X + c, t * X * Y + s * Z, t * X * Z - s * Y],
         [t * X * Y - s * Z, t * Y * Y + c, t * Y * Z + s * X],
         [t * X * Z + s * Y, t * Y * Z - s * X, t * Z * Z + c]]
  mat = numpy.array(mat)
  if isinstance(pt, Geometry.Point3D):
    pt = numpy.array((pt.x, pt.y, pt.z))
    tmp = numpy.dot(mat, pt)
    res = Geometry.Point3D(tmp[0], tmp[1], tmp[2])
  elif isinstance(pt, list) or isinstance(pt, tuple):
    pts = pt
    res = []
    for pt in pts:
      pt = numpy.array((pt.x, pt.y, pt.z))
      tmp = numpy.dot(mat, pt)
      res.append(Geometry.Point3D(tmp[0], tmp[1], tmp[2]))
  else:
    res = None
  return res

def GetAcceptor2FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Acceptor of type 2

  This is the acceptor with two adjacent heavy atoms. We will special case a few things here.
  If the acceptor atom is an oxygen we will assume a sp3 hybridization
  the acceptor directions (two of them)
  reflect that configurations. Otherwise the direction vector in plane with the neighboring
  heavy atoms

  ARGUMENTS:
      featAtoms - list of atoms that are part of the feature
      scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]
  cpt = conf.GetAtomPosition(aid)

  mol = conf.GetOwningMol()
  nbrs = list(mol.GetAtomWithIdx(aid).GetNeighbors())
  hydrogens = []
  tmp = []
  while len(nbrs):
    nbr = nbrs.pop()
    if nbr.GetAtomicNum() == 1:
      hydrogens.append(nbr)
    else:
      tmp.append(nbr)
  nbrs = tmp
  assert len(nbrs) == 2

  bvec = _findAvgVec(conf, cpt, nbrs)
  bvec *= (-1.0 * scale)

  if (mol.GetAtomWithIdx(aid).GetAtomicNum() == 8):
    # assume sp3
    # we will create two vectors by rotating bvec by half the tetrahedral angle in either directions
    v1 = conf.GetAtomPosition(nbrs[0].GetIdx())
    v1 -= cpt
    v2 = conf.GetAtomPosition(nbrs[1].GetIdx())
    v2 -= cpt
    rotAxis = v1 - v2
    rotAxis.Normalize()
    bv1 = ArbAxisRotation(54.5, rotAxis, bvec)
    bv1 += cpt
    bv2 = ArbAxisRotation(-54.5, rotAxis, bvec)
    bv2 += cpt
    return ((cpt, bv1),
            (cpt, bv2), ), 'linear'
  else:
    bvec += cpt
    return ((cpt, bvec), ), 'linear'

def _GetTetrahedralFeatVect(conf, aid, scale):
  mol = conf.GetOwningMol()

  cpt = conf.GetAtomPosition(aid)
  nbrs = mol.GetAtomWithIdx(aid).GetNeighbors()
  if not _checkPlanarity(conf, cpt, nbrs, tol=0.1):
    bvec = _findAvgVec(conf, cpt, nbrs)
    bvec *= (-1.0 * scale)
    bvec += cpt
    res = ((cpt, bvec), )
  else:
    res = ()
  return res

def GetDonor3FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Donor of type 3

  This is a donor with three heavy atoms as neighbors. We will assume
  a tetrahedral arrangement of these neighbors. So the direction we are seeking
  is the last fourth arm of the sp3 arrangment

  ARGUMENTS:
    featAtoms - list of atoms that are part of the feature
    scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]

  tfv = _GetTetrahedralFeatVect(conf, aid, scale)
  return tfv, 'linear'

def GetAcceptor3FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Donor of type 3

  This is a donor with three heavy atoms as neighbors. We will assume
  a tetrahedral arrangement of these neighbors. So the direction we are seeking
  is the last fourth arm of the sp3 arrangment

  ARGUMENTS:
    featAtoms - list of atoms that are part of the feature
    scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]
  tfv = _GetTetrahedralFeatVect(conf, aid, scale)
  return tfv, 'linear'

def _findHydAtoms(nbrs, atomNames):
  hAtoms = []
  for nid in nbrs:
    if atomNames[nid] == 'H':
      hAtoms.append(nid)
  return hAtoms

def _checkPlanarity(conf, cpt, nbrs, tol=1.0e-3):
  assert len(nbrs) == 3
  v1 = conf.GetAtomPosition(nbrs[0].GetIdx())
  v1 -= cpt
  v2 = conf.GetAtomPosition(nbrs[1].GetIdx())
  v2 -= cpt
  v3 = conf.GetAtomPosition(nbrs[2].GetIdx())
  v3 -= cpt
  normal = v1.CrossProduct(v2)
  dotP = abs(v3.DotProduct(normal))
  if (dotP <= tol):
    return 1
  else:
    return 0

def GetDonor2FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Donor of type 2

  This is a donor with two heavy atoms as neighbors. The atom may are may not have
  hydrogen on it. Here are the situations with the neighbors that will be considered here
    1. two heavy atoms and two hydrogens: we will assume a sp3 arrangement here
    2. two heavy atoms and one hydrogen: this can either be sp2 or sp3
    3. two heavy atoms and no hydrogens

  ARGUMENTS:
    featAtoms - list of atoms that are part of the feature
    scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]
  mol = conf.GetOwningMol()

  cpt = conf.GetAtomPosition(aid)

  # find the two atoms that are neighbors of this atoms
  nbrs = list(mol.GetAtomWithIdx(aid).GetNeighbors())
  assert len(nbrs) >= 2

  hydrogens = []
  tmp = []
  while len(nbrs):
    nbr = nbrs.pop()
    if nbr.GetAtomicNum() == 1:
      hydrogens.append(nbr)
    else:
      tmp.append(nbr)
  nbrs = tmp

  if len(nbrs) == 2:
    # there should be no hydrogens in this case
    assert len(hydrogens) == 0
    # in this case the direction is the opposite of the average vector of the two neighbors
    bvec = _findAvgVec(conf, cpt, nbrs)
    bvec *= (-1.0 * scale)
    bvec += cpt
    return ((cpt, bvec), ), 'linear'
  elif len(nbrs) == 3:
    assert len(hydrogens) == 1
    # this is a little more tricky we have to check if the hydrogen is in the plane of the
    # two heavy atoms (i.e. sp2 arrangement) or out of plane (sp3 arrangement)

    # one of the directions will be from hydrogen atom to the heavy atom
    hid = hydrogens[0].GetIdx()
    bvec = conf.GetAtomPosition(hid)
    bvec -= cpt
    bvec.Normalize()
    bvec *= scale
    bvec += cpt
    if _checkPlanarity(conf, cpt, nbrs):
      # only the hydrogen atom direction needs to be used
      return ((cpt, bvec), ), 'linear'
    else:
      # we have a non-planar configuration - we will assume sp3 and compute a second direction vector
      ovec = _findAvgVec(conf, cpt, nbrs)
      ovec *= (-1.0 * scale)
      ovec += cpt
      return ((cpt, bvec),
              (cpt, ovec), ), 'linear'

  elif len(nbrs) >= 4:
    # in this case we should have two or more hydrogens we will simple use there directions
    res = []
    for hid in hydrogens:
      bvec = conf.GetAtomPosition(hid)
      bvec -= cpt
      bvec.Normalize()
      bvec *= scale
      bvec += cpt
      res.append((cpt, bvec))
    return tuple(res), 'linear'

def GetDonor1FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Donor of type 1

  This is a donor with one heavy atom. It is not clear where we should we should be putting the
  direction vector for this. It should probably be a cone. In this case we will just use the
  direction vector from the donor atom to the heavy atom

  ARGUMENTS:

    featAtoms - list of atoms that are part of the feature
    scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]
  mol = conf.GetOwningMol()
  nbrs = mol.GetAtomWithIdx(aid).GetNeighbors()

  # find the neighboring heavy atom
  hnbr = -1
  for nbr in nbrs:
    if nbr.GetAtomicNum() != 1:
      hnbr = nbr.GetIdx()
      break

  cpt = conf.GetAtomPosition(aid)
  v1 = conf.GetAtomPosition(hnbr)
  v1 -= cpt
  v1.Normalize()
  v1 *= (-1.0 * scale)
  v1 += cpt
  return ((cpt, v1), ), 'cone'

def GetAcceptor1FeatVects(conf, featAtoms, scale=1.5):
  """
  Get the direction vectors for Acceptor of type 1

  This is a acceptor with one heavy atom neighbor. There are two possibilities we will
  consider here
  1. The bond to the heavy atom is a single bond e.g. CO
     In this case we don't know the exact direction and we just use the inversion of this bond direction
     and mark this direction as a 'cone'
  2. The bond to the heavy atom is a double bond e.g. C=O
     In this case the we have two possible direction except in some special cases e.g. SO2
     where again we will use bond direction

  ARGUMENTS:
    featAtoms - list of atoms that are part of the feature
    scale - length of the direction vector
  """
  assert len(featAtoms) == 1
  aid = featAtoms[0]
  mol = conf.GetOwningMol()
  nbrs = mol.GetAtomWithIdx(aid).GetNeighbors()

  cpt = conf.GetAtomPosition(aid)

  # find the adjacent heavy atom
  heavyAt = -1
  for nbr in nbrs:
    if nbr.GetAtomicNum() != 1:
      heavyAt = nbr
      break

  singleBnd = mol.GetBondBetweenAtoms(aid, heavyAt.GetIdx()).GetBondType() > Chem.BondType.SINGLE

  # special scale - if the heavy atom is a sulfur (we should proabably check phosphorous as well)
  sulfur = heavyAt.GetAtomicNum() == 16

  if singleBnd or sulfur:
    v1 = conf.GetAtomPosition(heavyAt.GetIdx())
    v1 -= cpt
    v1.Normalize()
    v1 *= (-1.0 * scale)
    v1 += cpt
    return ((cpt, v1), ), 'cone'
  else:
    # ok in this case we will assume that
    # heavy atom is sp2 hybridized and the direction vectors (two of them)
    # are in the same plane, we will find this plane by looking for one
    # of the neighbors of the heavy atom
    hvNbrs = heavyAt.GetNeighbors()
    hvNbr = -1
    for nbr in hvNbrs:
      if nbr.GetIdx() != aid:
        hvNbr = nbr
        break

    pt1 = conf.GetAtomPosition(hvNbr.GetIdx())
    v1 = conf.GetAtomPosition(heavyAt.GetIdx())
    pt1 -= v1
    v1 -= cpt
    rotAxis = v1.CrossProduct(pt1)
    rotAxis.Normalize()
    bv1 = ArbAxisRotation(120, rotAxis, v1)
    bv1.Normalize()
    bv1 *= scale
    bv1 += cpt
    bv2 = ArbAxisRotation(-120, rotAxis, v1)
    bv2.Normalize()
    bv2 *= scale
    bv2 += cpt
    return ((cpt, bv1),
            (cpt, bv2), ), 'linear'

max_stars_repo_name: docking-org/rdk

max_issues_repo_name: docking-org/rdk

max_forks_repo_name: docking-org/rdk


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Feb  9 13:55:16 2015

@author: adelpret
"""
import numpy as np

NJ = 32;

COULOMB_FRICTION_COMPENSATION_PERCENTAGE = 0.0;
k_p_torque                              = np.array(NJ*[0.5,]);   # torque control proportional gains
k_d_torque                              = np.array(NJ*[0.0,]);   # torque control derivative gains
k_i_torque                              = np.array(NJ*[3.0,]);   # torque control integral gains
torque_integral_saturation              = np.array(NJ*[0.0,]);
poly_sign_dq                            = 3;                     # order of polynomial to approximate Coulomb friction around zero velocity
k_d_velocity                            = np.array(NJ*[0.0,]);   # torque control derivative gains
alpha_leaking                           = np.array([75.,75.,75.,75.,75.,75.,
                                                    75.,75.,75.,75.,75.,75.,
                                                    0.0,0.0,
                                                    0.0,0.0,0.0,0.0,0.0,0.0,0.0,
                                                    0.0,
                                                    0.0,0.0,0.0,0.0,0.0,0.0,0.0,
                                                    0.0,
                                                    0.0,0.0]);

## PARAMETERS OF R_hip_y JOINT 0
#k_p_torque[0] = 12.0;
## PARAMETERS OF R_hip_r JOINT 1
#k_p_torque[1] = 5; #15.0 # could easily go up to 20, but it's a bit less stable
## PARAMETERS OF R_hip_p JOINT 2
#k_p_torque[2] = 6; #with delay 30 ms
## PARAMETERS OF R_knee JOINT 3
#k_p_torque[3] = 10.0;  # with 12 it starts vibrating at low velocity
## PARAMETERS OF R_ankle pitch JOINT 4
#k_p_torque[4] = 10.0;  # 10 feels good, but maybe i could go higher
## PARAMETERS OF R_ankle roll JOINT 5
#k_p_torque[5] = 15.0; # could go higher, but it feels already good
## PARAMETERS OF Left hip pitch JOINT 8
#k_p_torque[8] = 6.0;   # with delay 20 ms


text:
# -*- coding: utf-8 -*-
"""
##summarize_dataset, summarize_by_class, calculate_class_probabilities NAKIJKEN
LET OP ALS class_value, dit is nu 'target'

Verdeling training en test maken.
"""
import numpy as np
from sklearn.datasets import load_diabetes, load_breast_cancer
import copy
import matplotlib.pyplot as plt

# Split the dataset by classifier target, returns a dictionary
def separate_by_class(dataset):
	separated = dict()
	for i in range(len(dataset)):
		valuetarget = dataset[i]
		target = dataset[i,1]
		if (target not in separated):
			separated[target] = list()
		separated[target].append(valuetarget)
	return separated

# Calculate the mean, std and count for each column in the dataset
def summarize_dataset(dataset):
	summaries = [(np.mean(column), np.std(column), len(column)) for column in zip(*dataset)]
	del(summaries[-1])
	return summaries

# Split dataset by class then calculate statistics for each row
def summarize_by_class(dataset):
	separated = separate_by_class(dataset)
	summaries = dict()
	for class_value, rows in separated.items():
		summaries[class_value] = summarize_dataset(rows)
	return summaries

# Gaussian probability distribution function for x
def gaussian_probability(x, mean, std):
    gaussian=(1 / (np.sqrt(2 * np.pi) * std)) * np.exp(-((x-mean)**2 / (2 * std**2 )))
    return gaussian

# Calculate the probabilities of predicting each class for a given row
def calculate_class_probabilities(summaries, row):
	total_rows = sum([summaries[label][0][2] for label in summaries])
	probabilities = dict()
	for class_value, class_summaries in summaries.items():
		probabilities[class_value] = summaries[class_value][0][2]/float(total_rows)
		for i in range(len(class_summaries)):
			mean, std, _ = class_summaries[i]
			probabilities[class_value] *= gaussian_probability(row[i], mean, std)
	return probabilities

def normalize(probs):
    prob_factor = 1 / sum(probs)
    return [prob_factor * p for p in probs]

breast_cancer = load_breast_cancer()
feature_names = breast_cancer.feature_names
#verdeling nog aanpassen met training en test set, nu resp. 300 : 269
probability_0 = np.ones((269,30))
probability_1 = np.ones((269,30))
for k in range(0,len(feature_names)):
    X_train = breast_cancer.data[:300, k]
    y_train = breast_cancer.target[:300]
    X_test = breast_cancer.data[300:, k]
    y_test = breast_cancer.target[300:]

#onderstaande regel wordt volgens mij verder nergens gebruikt in het script
    dataset_0 = np.append(np.transpose(X_train), np.transpose(y_train))

#Create datasets which contains the values for the k-feature and the corresponding target
    dataset = np.ones((len(X_train),2))
    dataset_test = np.ones((len(X_test),2))
    for i in range(0,len(X_train)):
        dataset[i] = [X_train[i],y_train[i]]
        for i in range(0,len(X_test)):
            dataset_test[i] = [X_test[i],y_test[i]]

    summaries = summarize_by_class(dataset)

    for j in range(0,len(y_test)):
        probability = calculate_class_probabilities(summaries, dataset_test[j])
        prob_0 = probability[0]
        prob_1 = probability[1]
        probs = [prob_0,prob_1]
        prob_0,prob_1 = normalize(probs)

        probability_0[j,k] = prob_0
        probability_1[j,k] = prob_1

fig, axs = plt.subplots(6,5, figsize=(45, 45), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace = .5, wspace=.001)

axs = axs.ravel()

for i in range(30):

    axs[i].plot(probability_0[:,i], 'bx', label='malignant')
    axs[i].plot(probability_1[:,i], 'rx', label='benign')

    axs[i].set_title(feature_names[i])
    axs[i].set_ylabel('Probability')
   # axs[i].legend()


text:
import os
import random
import torch
from os.path import join as pjoin
from tqdm import tqdm
import utils.arg_parser
import argparse
import numpy as np
import json
import matplotlib.pyplot as plt
from PIL import Image

from rsa_eval import load_inc_rsa_model
from rsa import BirdDistractorDataset

def get_args(argstring=None, verbose=False):
    if argstring is None:
        argstring = "--model gve --dataset cub --eval ./checkpoints/gve-cub-D2020-03-14-T18-04-04-G0-best-ckpt.pth"
    args = utils.arg_parser.get_args(argstring)
    if verbose:
        # Print arguments
        utils.arg_parser.print_args(args)

    return args

def plot_images(target_img, dis_cell, sim_cell, issue_name, max_display=5):
    # 3 rows: target image (first row)
    # then 5 distractors, 5 similars

    # Create a figure with sub-plots
    fig, axes = plt.subplots(3, max_display, figsize=(20, 10))

    # Adjust the vertical spacing
    hspace = 0.2
    fig.subplots_adjust(hspace=hspace, wspace=0.3)

    # plot the target image
    axes.flat[0].imshow(Image.open("./data/cub/images/" + target_img))
    axes.flat[0].set_xlabel("Target Image \n" + target_img.split("/")[0])
    axes.flat[0].set_xticks([])
    axes.flat[0].set_yticks([])
    for i in range(1, max_display):
        fig.delaxes(axes.flat[i])

    for i in range(min(len(sim_cell), max_display)):
        img_id = sim_cell[i]
        axes.flat[max_display + i].imshow(Image.open("./data/cub/images/" + img_id))
        axes.flat[max_display + i].set_xlabel("Similar Image \n" + img_id.split("/")[0])
        axes.flat[max_display + i].set_xticks([])
        axes.flat[max_display + i].set_yticks([])

    for i in range(min(len(dis_cell), max_display)):
        img_id = dis_cell[i]
        axes.flat[max_display * 2 + i].imshow(Image.open("./data/cub/images/" + img_id))
        axes.flat[max_display * 2 + i].set_xlabel("Dissimilar Image \n" + img_id.split("/")[0])
        axes.flat[max_display * 2 + i].set_xticks([])
        axes.flat[max_display * 2 + i].set_yticks([])

    fig.suptitle("Issue: {}".format(issue_name))
    # Show the plot
    plt.show()

class CUBPartitionDataset(object):
    def __init__(self, cell_select_strategy=None, argstring=None, return_labels=True):
        self.cell_select_strategy = cell_select_strategy

        self.args = args = get_args(argstring)
        self.device = torch.device('cuda:{}'.format(args.cuda_device) if
                                            torch.cuda.is_available() and not args.disable_cuda else 'cpu')

        self.image_folder = './data/cub/'

        # load issues here
        self.attr_issue_matrix = np.load(pjoin(self.image_folder, "strict_cap_issue_matrix.npz"))['data']
        self.mat_idx_to_imgid = json.load(open(pjoin(self.image_folder, "mat_idx_to_imgid.json")))
        self.imgid_to_mat_idx = json.load(open(pjoin(self.image_folder, "imgid_to_mat_idx.json")))
        self.issue_vocab = json.load(open(pjoin(self.image_folder, "issue_vocab.json")))

    def get_cells_by_partition(self, img_id, issue_id, max_cap_per_cell=60, cell_select_strategy=None,
                               no_similar=False):

        assert type(issue_id) == int and 0 <= issue_id <= len(self.issue_vocab)
        assert issue_id in self.get_valid_issues(img_id)[0], "question_id not valid!"

        sim_img_indices = self.attr_issue_matrix[:, issue_id].nonzero()[0].tolist()
        dis_img_indices = (self.attr_issue_matrix[:, issue_id] == 0).nonzero()[0].tolist()

        sampled_sim_img_indices = random.sample(sim_img_indices, max_cap_per_cell)
        sampled_dis_img_indices = random.sample(dis_img_indices, max_cap_per_cell)

        sim_img_ids = [self.mat_idx_to_imgid[i] for i in sampled_sim_img_indices]
        dis_img_ids = [self.mat_idx_to_imgid[i] for i in sampled_dis_img_indices]

        if no_similar:
            sim_img_ids = []

        return dis_img_ids, sim_img_ids, 0, sampled_dis_img_indices, sampled_dis_img_indices

    def get_valid_issues(self, img_id):
        issue_row = self.attr_issue_matrix[self.imgid_to_mat_idx[img_id]]
        valid_issue_ids = issue_row.nonzero()[0].tolist()

        labels = []
        for issue_id in valid_issue_ids:
            labels.append(self.issue_vocab[issue_id])

        return valid_issue_ids, labels

    def clone(self, b):
        a = np.empty_like(b)
        a[:] = b
        return a

def generate_caption_for_test(save_file_prefix, max_cap_per_cell=40, rationality=20, entropy_penalty_alpha=0.4,
                              no_retry=False, no_similar=False):

    open(save_file_prefix + "_gen_captions.json", 'w').close()
    open(save_file_prefix + "_sampled_partitions.json", 'w').close()

    cub_partition = CUBPartitionDataset()
    rsa_dataset = BirdDistractorDataset(randomized=True)
    rsa_model = load_inc_rsa_model(rsa_dataset)

    test_ids = []
    with open(pjoin(cub_partition.image_folder, 'test.txt')) as f:
        for line in f:
            test_ids.append(line.strip())

    img_id_to_caption = {}
    img_id_to_partition_idx = {}

    for imgid in tqdm(test_ids):

        img_issues, issue_names = cub_partition.get_valid_issues(imgid)
        img_id_to_caption[imgid] = {}
        img_id_to_partition_idx[imgid] = {}

        for issue_id, issue_name in zip(img_issues, issue_names):
            dis_cell2, sim_cell2, _, dis_indices, sim_indices = cub_partition.get_cells_by_partition(
                imgid, issue_id, max_cap_per_cell=max_cap_per_cell)

            if no_similar:
                sim_cell2 = []

            cap = rsa_model.greedy_pragmatic_speaker_free(
                    [imgid] + sim_cell2 + dis_cell2,
                    num_sim=len(sim_cell2), rationality=rationality,
                    speaker_prior=True, entropy_penalty_alpha=entropy_penalty_alpha)[0]

            img_id_to_caption[imgid][issue_id] = cap
            img_id_to_partition_idx[imgid][issue_id] = [dis_indices, sim_indices]

    json.dump(img_id_to_caption, open(save_file_prefix+"_gen_captions.json", 'w'))
    json.dump(img_id_to_partition_idx, open(save_file_prefix+"_sampled_partitions.json", 'w'))

def generate_literal_caption_for_test(save_file_prefix):
    cub_partition = CUBPartitionDataset()
    rsa_dataset = BirdDistractorDataset(randomized=True)
    rsa_model = load_inc_rsa_model(rsa_dataset)

    open(save_file_prefix + "_gen_captions.json", 'w').close()

    test_ids = []
    with open(pjoin(cub_partition.image_folder, 'test.txt')) as f:
        for line in f:
            test_ids.append(line.strip())

    img_id_to_caption = {}
    img_id_to_partition_idx = {}

    for imgid in tqdm(test_ids):
        img_id_to_caption[imgid] = {}
        img_id_to_partition_idx[imgid] = {}
        img_issues, issue_names = cub_partition.get_valid_issues(imgid)

        cap = rsa_model.semantic_speaker([imgid])[0]
        for issue_id, issue_name in zip(img_issues, issue_names):
            img_id_to_caption[imgid][issue_id] = cap

    json.dump(img_id_to_caption, open(save_file_prefix + "_gen_captions.json", 'w'))

if __name__ == '__main__':
    # both S1-Q, and S1-QH get "retry"
    parser = argparse.ArgumentParser()
    parser.add_argument('--rationality', type=float, default=10, help="raitionality")
    parser.add_argument('--entropy', type=float, default=0.4, help="raitionality")
    parser.add_argument('--max_cell_size', type=int, default=40, help="cell size")

    parser.add_argument('--exp_num', type=int, help="which evaluation experiment to run; this helps parallelization")
    parser.add_argument('--root_dir', type=str, default="./results/", help="format is ./results/{}, no slash aft+er")
    parser.add_argument('--file_prefix', type=str, default="{}", help="prefix hyperparameter for the run")
    parser.add_argument('--run_time', type=int, default=1, help="format is ./results/{}, no slash after")
    args = parser.parse_args()

    for time in range(args.run_time):

        os.makedirs(pjoin(args.root_dir, "random_run_{}".format(time), "test.txt"), exist_ok=True)

        save_dir = pjoin(args.root_dir, "random_run_{}".format(time))

        if args.exp_num == 0:
            generate_literal_caption_for_test(args.save_dir.format("S0"))

        if args.exp_num == 1:
            generate_caption_for_test(save_dir + "/S1", max_cap_per_cell=args.max_cell_size,
                                              rationality=args.rationality,
                                              entropy_penalty_alpha=0, no_similar=True)
        if args.exp_num == 2:
            generate_caption_for_test(save_dir + "/S1_Q", max_cap_per_cell=args.max_cell_size,
                                      rationality=args.rationality,
                                      entropy_penalty_alpha=0)

        if args.exp_num == 3:
            generate_caption_for_test(save_dir + "/S1_QH", max_cap_per_cell=args.max_cell_size,
                                      rationality=args.rationality,
                                      entropy_penalty_alpha=args.entropy)


text:
from typing import Union

import numpy as np

from prts.base.time_series_metrics import BaseTimeSeriesMetrics

class TimeSeriesRecall(BaseTimeSeriesMetrics):
    """ This class calculates recall for time series """

    def __init__(self, alpha=0.0, cardinality="one", bias="flat"):
        """Constructor

        Args:
            alpha (float, optional): 0 <= alpha <= 1. Defaults to 0.0.
            cardinality (str, optional): ["one", "reciprocal", "udf_gamma"]. Defaults to "one".
            bias (str, optional): ["flat", "front", "middle", "back"]. Defaults to "flat".
        """

        assert (alpha >= 0) & (alpha <= 1)
        assert cardinality in ["one", "reciprocal", "udf_gamma"]
        assert bias in ["flat", "front", "middle", "back"]

        self.alpha = alpha
        self.cardinality = cardinality
        self.bias = bias

    def score(self, real: Union[np.ndarray, list], pred: Union[np.ndarray, list]) -> float:
        """Computing recall score

        Args:
            real (np.ndarray or list):
                One-dimensional array of correct answers with values of 1 or 0.
            pred (np.ndarray or list):
                One-dimensional array of predicted answers with values of 1 or 0.
        Returns:
            float: recall
        """

        assert isinstance(real, np.ndarray) or isinstance(real, list)
        assert isinstance(pred, np.ndarray) or isinstance(pred, list)

        if not isinstance(real, np.ndarray):
            real = np.array(real)
        if not isinstance(pred, np.ndarray):
            pred = np.array(pred)

        real_anomalies, predicted_anomalies = self._prepare_data(real, pred)
        recall = self._update_recall(real_anomalies, predicted_anomalies)

        return recall

    def _update_recall(self, real_anomalies: np.ndarray, predicted_anomalies: np.ndarray) -> float:
        """Update recall
        Args:
            real_anomalies (np.ndarray):
                2-dimensional array of the first and last indexes of each real anomaly range.
                e.g. np.array([[1933,  1953],[1958,  2000], ...])
            predicted_anomalies (np.ndarray):
                2-dimensional array of the first and last indexes of each predicted anomaly range.
                e.g. np.array([[1933,  1953],[1958,  2000], ...])
        Returns:
            float: recall
        """

        recall = 0
        if len(real_anomalies) == 0:
            return 0
        for i in range(len(real_anomalies)):
            omega_reward = 0
            overlap_count = [0]
            range_r = real_anomalies[i, :]
            for j in range(len(predicted_anomalies)):
                range_p = predicted_anomalies[j, :]
                omega_reward += self._compute_omega_reward(range_r, range_p, overlap_count)
            overlap_reward = self._gamma_function(overlap_count) * omega_reward

            if overlap_count[0] > 0:
                existence_reward = 1
            else:
                existence_reward = 0

            recall += self.alpha * existence_reward + (1 - self.alpha) * overlap_reward
        recall /= len(real_anomalies)
        return recall

max_stars_repo_name: CompML/PRTS

max_issues_repo_name: CompML/PRTS

max_forks_repo_name: CompML/PRTS


text:
#!/usr/bin/env python

from bme280 import BME280
from ccs811 import CCS811

import tkinter
import numpy as np

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.animation as animation

X_LIMIT = 100
RESOLUSION = 0.2

class GUI():
    def __init__(self):
        self.ccs811 = CCS811()
        self.bme280 = BME280()
        p, t, h = self.bme280.get()
        self.ccs811.compensate(h, t)

        self.data_co2 = np.zeros(int(X_LIMIT/RESOLUSION), dtype=int)
        self.data_t = np.zeros(int(X_LIMIT/RESOLUSION), dtype=int)
        self.data_h = np.zeros(int(X_LIMIT/RESOLUSION), dtype=int)
        self.data_p = np.zeros(int(X_LIMIT/RESOLUSION), dtype=int)

        self.root = tkinter.Tk()
        self.root.wm_title("Embedding in Tk anim")

        self.fig = Figure()
        # FuncAnimationより前に呼ぶ必要がある
        canvas = FigureCanvasTkAgg(self.fig, master=self.root)  # A tk.DrawingArea.

        x = np.arange(0, X_LIMIT, RESOLUSION)  # x軸(固定の値)
        self.l = np.arange(0, X_LIMIT, RESOLUSION)  # 表示期間(FuncAnimationで指定する関数の引数になる)
        plt_co2 = self.fig.add_subplot(211)
        plt_co2.set_ylim([0, 4000])
        self.line_co2, = plt_co2.plot(x, self.data_co2, 'C3', label="CO2 ppm")

        h0, l0 = plt_co2.get_legend_handles_labels()
        plt_co2.legend(h0, l0, loc='upper left')

        plt_t = self.fig.add_subplot(212)
        plt_t.set_ylim([-10, 50])
        self.line_t, = plt_t.plot(x, self.data_t, 'C1', label="Celsius")

        plt_h = plt_t.twinx()
        plt_h.set_ylim([0, 100])
        self.line_h, = plt_h.plot(x, self.data_h, 'C0', label="humidity %")

        plt_p = plt_t.twinx()
        plt_p.set_ylim([900, 1200])
        self.line_p, = plt_p.plot(x, self.data_p, 'C2', label="pressure hPa")

        h1, l1 = plt_t.get_legend_handles_labels()
        h2, l2 = plt_h.get_legend_handles_labels()
        h3, l3 = plt_p.get_legend_handles_labels()
        plt_t.legend(h1+h2+h3, l1+l2+l3, loc='upper left')

        self.ani = animation.FuncAnimation(self.fig, self.animate, self.l,
            init_func=self.init, interval=int(1000*RESOLUSION), blit=False,
            )

        toolbar = NavigationToolbar2Tk(canvas, self.root)
        canvas.get_tk_widget().pack(fill='both')

        button = tkinter.Button(master=self.root, text="Quit", command=self.quit)
        button.pack()

    def quit(self):
        self.root.quit()     # stops mainloop
        self.root.destroy()  # this is necessary on Windows to prevent
                        # Fatal Python Error: PyEval_RestoreThread: NULL tstate

    def init(self):  # only required for blitting to give a clean slate.
        self.line_co2.set_ydata(self.data_co2)
        return self.line_co2,

    def animate(self, i):
        try:
            p, t, h = self.bme280.get()
            voc, co2 = self.ccs811.get()
        except OSError:
            return

        print(f"{p:7.2f} hPa, {t:6.2f} C, {h:5.2f} %, TVOC:{voc:4d} ppb, eCO2:{co2:4d} ppm")
        self.data_co2 = np.append(self.data_co2[1:], co2)
        self.line_co2.set_ydata(self.data_co2)  # update the data_co2.

        self.data_t = np.append(self.data_t[1:], t)
        self.line_t.set_ydata(self.data_t)  # update the data_t.
        self.data_h = np.append(self.data_h[1:], h)
        self.line_h.set_ydata(self.data_h)  # update the data_h.
        self.data_p = np.append(self.data_p[1:], p)
        self.line_p.set_ydata(self.data_p)  # update the data_p.

    def run(self):
        tkinter.mainloop()

if __name__ == '__main__':
    gui = GUI()
    gui.run()


text:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

expnum = 3

# sizes = [i for i in range(100000, 2000001, 100000)]
# ylabels = ["Query", "Rules Generated", "Facts Generated", "Parsing Time", "Translating Time", "Reasoning Time", "Num. of Answers"]
# filenames = ["query", "rules", "facts", "parset", "transt", "reasont", "answers"]

# a1 = {k: [0 for i in range(50)] for k in sizes}
# a2 = {k: [0 for i in range(50)] for k in sizes}
# a3 = {k: [0 for i in range(50)] for k in sizes}

# size = 1500000
# graphOf = 5
# ylabel = ylabels[graphOf]

# for num in range(expnum):
# 	for i in range(len(sizes)):
# 		with open(f'1-{sizes[i]}-{num+1}.csv', 'r') as reader:
# 			next(reader)
# 			j = 0
# 			for line in reader.readlines():
# 				a1[sizes[i]][j] += int(line.split(',')[graphOf].strip())
# 				j += 1
# 		with open(f'2-{sizes[i]}-{num+1}.csv', 'r') as reader:
# 			next(reader)
# 			j = 0
# 			for line in reader.readlines():
# 				a2[sizes[i]][j] += int(line.split(',')[graphOf].strip())
# 				j += 1
# 		# with open(f'3-{size[i]}-{num+1}.csv', 'r') as reader:
# 		# 	next(reader)
# 		# 	j = 0
# 		# 	for line in reader.readlines():
# 		# 		a3[sizes[i]][j] += (int(line.split(',')[graphOf].strip())
# 		# 		j += 1

# print(a1)

# ########################### BAR CHART #####################################

# labels = [i for i in range(50)]
# d1 = [i / expnum for i in a1[size]]
# d2 = [i / expnum for i in a2[size]]
# # d3 = a3[size] / expnum

# x = np.arange(len(labels))  # the label locations
# width = 0.35  # the width of the bars

# fig, ax = plt.subplots()
# rects1 = ax.bar(x - width/3, d1, width, label='Translation 1')
# rects2 = ax.bar(x + width/3, d2, width, label='Translation 2')
# # rects3 = ax.bar(x + width/3, d3, width, label='Translation 3')

# # Add some text for labels, title and custom x-axis tick labels, etc.
# ax.set_ylabel(ylabel)
# ax.set_title("Comparison of "+ylabel+" by Translation")
# ax.set_xticks(x)
# ax.set_xticklabels(labels)
# ax.legend()

# def autolabel(rects):
# 	"""Attach a text label above each bar in *rects*, displaying its height."""
# 	for rect in rects:
# 		height = rect.get_height()
# 		ax.annotate('{}'.format(height),
# 					xy=(rect.get_x() + rect.get_width() / 2, height),
# 					xytext=(0, 3),  # 3 points vertical offset
# 					textcoords="offset points",
# 					ha='center', va='bottom')

# # autolabel(rects1)
# # autolabel(rects2)
# # autolabel(rects3)

# fig.tight_layout()

# # plt.savefig(f'query-{filenames[graphOf]}-{size}',bbox_inches='tight')
# plt.ylim(0,55000000000)
# plt.show()

##################################### BY SIZE #########################################

sizes = [i for i in range(100000, 2000001, 100000)]
ylabels = ["Query", "Rules Generated", "Facts Generated", "Parsing Time", "Translating Time", "Reasoning Time", "Num. of Answers"]

a1 = {k: [0 for i in range(len(sizes))] for k in range(50)}
a2 = {k: [0 for i in range(len(sizes))] for k in range(50)}
a3 = {k: [0 for i in range(len(sizes))] for k in range(50)}

query = 10
graphOf = 6
ylabel = ylabels[graphOf]

for num in range(expnum):
	for i in range(len(sizes)):
		j = 0
		with open(f'1-{sizes[i]}-{num+1}.csv', 'r') as reader:
			next(reader)
			for line in reader.readlines():
				a1[j][i] += int(line.split(',')[graphOf].strip())
				j += 1
		j = 0
		with open(f'2-{sizes[i]}-{num+1}.csv', 'r') as reader:
			next(reader)
			for line in reader.readlines():
				a2[j][i] += int(line.split(',')[graphOf].strip())
				j += 1
		# with open(f'3-{size[i]}.csv', 'r') as reader:
		# 	next(reader)
		# 	for line in reader.readlines():
		# 		a3[sizes[i]].append(int(line.split(',')[graphOf].strip()))

print(a1)

########################### BAR CHART #####################################

labels = [str(int(i/1000))+"K" for i in range(100000, 2000001, 100000)]
d1 = [i / expnum for i in a1[query]]
d2 = [i / expnum for i in a2[query]]
# d3 = a3[query] / expnum

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/3, d1, width, label='Translation 1')
rects2 = ax.bar(x + width/3, d2, width, label='Translation 2')
# rects3 = ax.bar(x + width/3, d3, width, label='Translation 3')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel(ylabel)
ax.set_title("Comparison of "+ylabel+" by Translation")
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
	"""Attach a text label above each bar in *rects*, displaying its height."""
	for rect in rects:
		height = rect.get_height()
		ax.annotate('{}'.format(height),
					xy=(rect.get_x() + rect.get_width() / 2, height),
					xytext=(0, 3),  # 3 points vertical offset
					textcoords="offset points",
					ha='center', va='bottom')

# autolabel(rects1)
# autolabel(rects2)
# autolabel(rects3)

fig.tight_layout()
plt.ylim(0,50000000000)
plt.show()

max_stars_repo_name: RidhwanD/rulewerk

max_issues_repo_name: RidhwanD/rulewerk

max_forks_repo_name: RidhwanD/rulewerk


text:
import logging
import os
import sys
import importlib
import argparse
import numpy as np
import h5py
import subprocess

from numpy.lib.index_tricks import AxisConcatenator
import munch
import yaml
# from utils.vis_utils import plot_single_pcd
# from utils.train_utils import *
from vis_utils import plot_single_pcd
from train_utils import *
from dataset import MVP_CP, MVP_CP_EX

import warnings
warnings.filterwarnings("ignore")

def test():
    dataset_test = MVP_CP_EX(prefix="test")
    dataloader_test = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size,
                                                  shuffle=False, num_workers=int(args.workers))
    dataset_length = len(dataset_test)
    logging.info('Length of test dataset:%d', len(dataset_test))

    # load model
    model_module = importlib.import_module('.%s' % args.model_name, 'models')
    net = torch.nn.DataParallel(model_module.Model(args))
    net.cuda()
    net.module.load_state_dict(torch.load(args.load_model)['net_state_dict'])
    logging.info("%s's previous weights loaded." % args.model_name)
    net.eval()

    logging.info('Testing...')
    with torch.no_grad():
        results_list = []
        for i, data in enumerate(dataloader_test):

            label, inputs_cpu, _, voxels, coords = data

            inputs = inputs_cpu.float().cuda()
            label = label.long().cuda()
            inputs = inputs.transpose(2, 1).contiguous()

            result_dict = net(inputs, gt = inputs, prefix="test", label = label, voxels = voxels, coords = coords)
            results_list.append(result_dict['result'].cpu().numpy())

            if i % args.step_interval_to_print == 0:
                logging.info('test [%d/%d]' % (i, dataset_length / args.batch_size))

        all_results = np.concatenate(results_list, axis=0)
        print(all_results.shape)

        with h5py.File(log_dir + '/results.h5', 'w') as f:
            f.create_dataset('results', data=all_results)

        # cur_dir = os.getcwd()
        # cmd = "cd %s; zip -r submission.zip results.h5 ; cd %s" % (log_dir, cur_dir)
        # process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        # _, _ = process.communicate()
        # print("Submission file has been saved to %s/submission.zip" % (log_dir))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Test config file')
    parser.add_argument('-c', '--config', help='path to config file', required=True)
    arg = parser.parse_args()
    config_path = arg.config
    args = munch.munchify(yaml.safe_load(open(config_path)))

    if not args.load_model:
        raise ValueError('Model path must be provided to load model!')

    exp_name = os.path.basename(args.load_model)
    log_dir = os.path.dirname(args.load_model)
    logging.basicConfig(level=logging.INFO, handlers=[logging.FileHandler(os.path.join(log_dir, 'test.log')), logging.StreamHandler(sys.stdout)])

    test()

max_stars_repo_name: Dizzy-cell/HOUV

max_issues_repo_name: Dizzy-cell/HOUV

max_forks_repo_name: Dizzy-cell/HOUV

reason: import numpy,from numpy


text:
import dotenv
import os

class Config():

    @property
    def seed(self):
        return int(os.getenv('SEED', 42))

    @property
    def k_folds(self):
        return int(os.getenv('K_FOLDS', 10))

    @property
    def pipline_fnt(self):
        return os.getenv('PIPELINE_FNT', 'build_sklearn_pipline')

    @property
    def data_url(self):
        return os.getenv(
            'URL',
            'https://archive.ics.uci.edu/ml/machine-learning-databases/'
            '00275/Bike-Sharing-Dataset.zip'
        )

    def __str__(self):
        return 'seed: %s, k_folds: %s, pipline_fnt: %s' % \
            (self.seed, self.k_folds, self.pipline_fnt)

DEFAULT = Config()

def set_global_seed(seed=None):

    import numpy as np
    import random as rd

    if not seed:
        seed = DEFAULT.seed

    np.random.seed(seed)
    rd.seed(seed)

def load_file(path):

    if not os.path.isfile(path):
        print('%s not found.' % path)
        print('Default config loaded.')
        print(DEFAULT)

    return dotenv.load_dotenv(path)

max_stars_repo_name: GeorgesAlkhouri/bikeshare

max_issues_repo_name: GeorgesAlkhouri/bikeshare

max_forks_repo_name: GeorgesAlkhouri/bikeshare


text:
from . import config
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import os
import random
import torch
from torchvision.ops import nms
from collections import Counter
from torch.utils.data import DataLoader
from tqdm import tqdm

def warning_on_one_line(message, category, filename, lineno, line=None):
    return '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)

def iou_width_height(boxes1, boxes2):
    """
    Parameters:
        boxes1 : tensor
                width and height of the first bounding boxes
        boxes2 : tensor
                width and height of the second bounding boxes
    Returns:
        tensor
        Intersection over union of the corresponding boxes
    """
    intersection = torch.min(boxes1[..., 0], boxes2[..., 0]) * torch.min(
        boxes1[..., 1], boxes2[..., 1]
    )
    union = (
            boxes1[..., 0] * boxes1[..., 1] + boxes2[..., 0] * boxes2[..., 1] - intersection
    )
    return intersection / union

def intersection_over_union(box1, box2, box_format="midpoint", eps=1e-7):
    box2 = box2.T
    box1 = box1.T

    if box_format == "corner":  # x1, y1, x2, y2 = box1
        b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
        b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
    elif box_format == "midpoint":  # transform from xywh to xyxy
        b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
        b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
        b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
        b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2

    # Intersection area
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

    # Union Area
    w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
    w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
    union = w1 * h1 + w2 * h2 - inter + eps

    iou = inter / union

    return iou  # IoU

def old_intersection_over_union(boxes_preds, boxes_labels, box_format="midpoint"):
    """

    This function calculates intersection over union (iou) given pred boxes
    and target boxes.

    Parameters:
        boxes_preds : tensor
                    Predictions of Bounding Boxes (BATCH_SIZE, 4)
        boxes_labels : tensor
                    Correct labels of Bounding Boxes (BATCH_SIZE, 4)
        box_format : str
                    midpoint/corners, if boxes (x,y,w,h) or (x1,y1,x2,y2)

    Returns:
        tensor
        Intersection over union for all examples
    """

    if box_format == "midpoint":
        box1_x1 = boxes_preds[..., 0:1] - boxes_preds[..., 2:3] / 2
        box1_y1 = boxes_preds[..., 1:2] - boxes_preds[..., 3:4] / 2
        box1_x2 = boxes_preds[..., 0:1] + boxes_preds[..., 2:3] / 2
        box1_y2 = boxes_preds[..., 1:2] + boxes_preds[..., 3:4] / 2
        box2_x1 = boxes_labels[..., 0:1] - boxes_labels[..., 2:3] / 2
        box2_y1 = boxes_labels[..., 1:2] - boxes_labels[..., 3:4] / 2
        box2_x2 = boxes_labels[..., 0:1] + boxes_labels[..., 2:3] / 2
        box2_y2 = boxes_labels[..., 1:2] + boxes_labels[..., 3:4] / 2

    if box_format == "corners":
        box1_x1 = boxes_preds[..., 0:1]
        box1_y1 = boxes_preds[..., 1:2]
        box1_x2 = boxes_preds[..., 2:3]
        box1_y2 = boxes_preds[..., 3:4]
        box2_x1 = boxes_labels[..., 0:1]
        box2_y1 = boxes_labels[..., 1:2]
        box2_x2 = boxes_labels[..., 2:3]
        box2_y2 = boxes_labels[..., 3:4]

    x1 = torch.max(box1_x1, box2_x1)
    y1 = torch.max(box1_y1, box2_y1)
    x2 = torch.min(box1_x2, box2_x2)
    y2 = torch.min(box1_y2, box2_y2)

    intersection = (x2 - x1).clamp(0) * (y2 - y1).clamp(0)
    box1_area = abs((box1_x2 - box1_x1) * (box1_y2 - box1_y1))
    box2_area = abs((box2_x2 - box2_x1) * (box2_y2 - box2_y1))

    return intersection / (box1_area + box2_area - intersection + 1e-6)

def non_max_suppression(bboxes, iou_threshold, threshold, box_format="midpoint"):
    """
    Using the NMS implemented by torchvision
    Parameters
    ----------
    bboxes : list
            list of lists containing all bboxes with each bboxes
            specified as [prob_score, x1, y1, x2, y2]
    iou_threshold : float
                    IoU threshold where predicted bboxes is correct
    threshold : float
                Threshold to remove predicted bboxes before NMS
    box_format : float
                "midpoint" or "corners" used to specify bboxes

    Returns
    -------
    list
    bboxes after performing NMS given a specific IoU threshold
    """
    assert type(bboxes) == list

    bboxes = torch.tensor([box for box in bboxes if box[0] > threshold])
    if len(bboxes) == 0:
        return []

    if box_format == "midpoint":
        box1_x1 = bboxes[..., 1:2] - bboxes[..., 3:4] / 2
        box1_y1 = bboxes[..., 2:3] - bboxes[..., 4:5] / 2
        box1_x2 = bboxes[..., 1:2] + bboxes[..., 3:4] / 2
        box1_y2 = bboxes[..., 2:3] + bboxes[..., 4:5] / 2

        mid_bboxes = torch.cat((bboxes[..., 0:1], box1_x1, box1_y1, box1_x2, box1_y2), dim=1)
    else:
        mid_bboxes = bboxes
    index_tensor = nms(mid_bboxes[..., 1:], mid_bboxes[..., 0], iou_threshold)
    return bboxes[index_tensor].tolist()

def average_precision(
        pred_boxes, true_boxes, iou_threshold=0.5, box_format="midpoint"
):
    """
    This function calculates average precision (AP)
    Parameters
    ----------
    pred_boxes : list
                list of lists containing all bboxes with each bboxes
                specified as [train_idx, confidence, x, y, w, h]
    true_boxes : list
                Similar as pred_boxes except all the correct ones
    iou_threshold : float
                    Threshold where predicted bboxes is correct
    box_format : str
                "midpoint" or "corners" used to specify bboxes

    Returns
    -------
    float
    AP value given a specific IoU threshold
    """
    # used for numerical stability later on
    epsilon = 1e-6

    # find the amount of bboxes for each training example
    # Counter here finds how many ground truth bboxes we get
    # for each training example, so let's say img 0 has 3,
    # img 1 has 5 then we will obtain a dictionary with:
    # amount_bboxes = {0:3, 1:5}
    amount_bboxes = Counter([gt[0] for gt in true_boxes])

    # We then go through each key, val in this dictionary
    # and convert to the following (w.r.t same example):
    # ammount_bboxes = {0:torch.tensor[0,0,0], 1:torch.tensor[0,0,0,0,0]}
    for key, val in amount_bboxes.items():
        amount_bboxes[key] = torch.zeros(val)

    # sort by box probabilities which is index 2
    pred_boxes.sort(key=lambda x: x[1], reverse=True)
    TP = torch.zeros((len(pred_boxes)))
    FP = torch.zeros((len(pred_boxes)))
    total_true_bboxes = len(true_boxes)

    # If none exists then we can safely skip
    if total_true_bboxes == 0:
        return 0

    for detection_idx, detection in enumerate(pred_boxes):
        # Only take out the ground_truths that have the same
        # training idx as detection
        ground_truth_img = [
            bbox for bbox in true_boxes if bbox[0] == detection[0]
        ]

        # num_gts = len(ground_truth_img)
        best_iou = 0

        for idx, gt in enumerate(ground_truth_img):
            iou = intersection_over_union(
                torch.tensor(detection[2:]),
                torch.tensor(gt[2:]),
                box_format=box_format,
            )

            if iou > best_iou:
                best_iou = iou
                best_gt_idx = idx

        if best_iou > iou_threshold:
            # only detect ground truth detection once
            if amount_bboxes[detection[0]][best_gt_idx] == 0:
                # true positive and add this bounding box to seen
                TP[detection_idx] = 1
                amount_bboxes[detection[0]][best_gt_idx] = 1
            else:
                FP[detection_idx] = 1

        # if IOU is lower then the detection is a false positive
        else:
            FP[detection_idx] = 1

    TP_cumsum = torch.cumsum(TP, dim=0)
    FP_cumsum = torch.cumsum(FP, dim=0)
    recalls = TP_cumsum / (total_true_bboxes + epsilon)
    precisions = TP_cumsum / (TP_cumsum + FP_cumsum + epsilon)
    precisions = torch.cat((torch.tensor([1]), precisions))
    recalls = torch.cat((torch.tensor([0]), recalls))

    # torch.trapz for numerical integration
    # ap.append(torch.trapz(precisions, recalls))

    return torch.trapz(precisions, recalls).float()

def save_PR_curve(pred_boxes, true_boxes, iou_threshold=0.5, box_format="midpoint", figure_path=None):
    """
    Save PR curve to a figure for checking performance conveniently.
    Parameters
    ----------
    pred_boxes : list
                list of lists containing all bboxes with each bboxes
                specified as [train_idx, confidence, x, y, w, h]
    true_boxes : list
                Similar as pred_boxes except all the correct ones
    iou_threshold : float
                    Threshold where predicted bboxes is correct
    box_format : str
                "midpoint" or "corners" used to specify bboxes
    """
    # used for numerical stability later on
    epsilon = 1e-6

    # find the amount of bboxes for each training example
    # Counter here finds how many ground truth bboxes we get
    # for each training example, so let's say img 0 has 3,
    # img 1 has 5 then we will obtain a dictionary with:
    # amount_bboxes = {0:3, 1:5}
    amount_bboxes = Counter([gt[0] for gt in true_boxes])

    # We then go through each key, val in this dictionary
    # and convert to the following (w.r.t same example):
    # ammount_bboxes = {0:torch.tensor[0,0,0], 1:torch.tensor[0,0,0,0,0]}
    for key, val in amount_bboxes.items():
        amount_bboxes[key] = torch.zeros(val)

    # sort by box probabilities which is index 2
    pred_boxes.sort(key=lambda x: x[1], reverse=True)
    TP = torch.zeros((len(pred_boxes)))
    FP = torch.zeros((len(pred_boxes)))
    total_true_bboxes = len(true_boxes)

    # If none exists then we can safely skip
    if total_true_bboxes == 0:
        return 0

    for detection_idx, detection in enumerate(pred_boxes):
        # Only take out the ground_truths that have the same
        # training idx as detection
        ground_truth_img = [
            bbox for bbox in true_boxes if bbox[0] == detection[0]
        ]

        best_iou = 0

        for idx, gt in enumerate(ground_truth_img):
            iou = intersection_over_union(
                torch.tensor(detection[2:]),
                torch.tensor(gt[2:]),
                box_format=box_format,
            )

            if iou > best_iou:
                best_iou = iou
                best_gt_idx = idx

        if best_iou > iou_threshold:
            # only detect ground truth detection once
            if amount_bboxes[detection[0]][best_gt_idx] == 0:
                # true positive and add this bounding box to seen
                TP[detection_idx] = 1
                amount_bboxes[detection[0]][best_gt_idx] = 1
            else:
                FP[detection_idx] = 1

        # if IOU is lower then the detection is a false positive
        else:
            FP[detection_idx] = 1

    TP_cumsum = torch.cumsum(TP, dim=0)
    FP_cumsum = torch.cumsum(FP, dim=0)
    recalls = TP_cumsum / (total_true_bboxes + epsilon)
    precisions = TP_cumsum / (TP_cumsum + FP_cumsum + epsilon)
    precisions = torch.cat((torch.tensor([1]), precisions))
    recalls = torch.cat((torch.tensor([0]), recalls))

    with plt.rc_context({'backend': 'agg'}):
        plt.figure()
        plt.plot(recalls, precisions, 'k')
        plt.savefig(figure_path)
        plt.close()

def plot_image(image, boxes):
    """Plots predicted bounding boxes on the image"""
    # cmap = plt.get_cmap("tab20b")
    # colors = [cmap(i) for i in np.linspace(0, 1, len(boxes))]
    im = np.array(image)

    height, width, _ = im.shape

    # Create figure and axes
    fig, ax = plt.subplots(1)
    # Display the image
    ax.imshow(im.reshape(image.shape[:2]), cmap='binary_r', origin='upper')

    # box[0] is x midpoint, box[2] is width
    # box[1] is y midpoint, box[3] is height

    # Create a Rectangle patch
    for i, box in enumerate(boxes):
        assert len(box) == 5, "box should contain confidence, x, y, width, height"
        confidence = box[0]
        box = box[1:]
        upper_left_x = box[0] - box[2] / 2
        upper_left_y = box[1] - box[3] / 2
        rect = patches.Rectangle(
            (upper_left_x * width, upper_left_y * height),
            box[2] * width,
            box[3] * height,
            linewidth=1,
            edgecolor='lime',
            # edgecolor=colors[i],
            facecolor="none",
        )

        # Add the patch to the Axes
        ax.add_patch(rect)
        plt.text(
            upper_left_x * width,
            upper_left_y * height,
            s=f"{confidence:.2f}",
            color="white",
            verticalalignment="top",
            bbox={"pad": 0},
        )

    plt.show()

def predict_bboxes(image, model, iou_threshold, threshold, anchors,
                   device_str):
    image = torch.tensor(np.stack(image), device=device_str)

    with torch.no_grad():
        predictions = model(image)

    batch_size = image.shape[0]
    bboxes = [[] for _ in range(batch_size)]
    for i in range(3):
        S = predictions[i].shape[2]
        anchor = torch.tensor([*anchors[i]], device=device_str) * S
        boxes_scale_i = cells_to_bboxes(
            predictions[i], anchor, S=S, is_preds=True
        )
        for idx, (box) in enumerate(boxes_scale_i):
            bboxes[idx] += box

    nms_boxes = []
    for lc_index in range(batch_size):
        nms_boxes.append(non_max_suppression(
            bboxes[lc_index],
            iou_threshold=iou_threshold,
            threshold=threshold,
            box_format="midpoint",
        ))
    return nms_boxes

def get_evaluation_bboxes(
        loader,
        model,
        iou_threshold,
        anchors,
        threshold,
        box_format="midpoint",
        device=config.DEVICE,
):
    # make sure models is in eval before get bboxes
    model.eval()
    train_idx = 0
    all_pred_boxes = []
    all_true_boxes = []
    for batch_idx, (x, labels) in enumerate(tqdm(loader)):
        # x = x.float().to(device)
        x = x.to(device)

        with torch.no_grad():
            predictions = model(x)

        batch_size = x.shape[0]
        bboxes = [[] for _ in range(batch_size)]
        for i in range(3):
            S = predictions[i].shape[2]
            anchor = torch.tensor([*anchors[i]], device=device) * S
            boxes_scale_i = cells_to_bboxes(
                predictions[i], anchor, S=S, is_preds=True
            )
            for idx, (box) in enumerate(boxes_scale_i):
                bboxes[idx] += box

        # we just want one bbox for each label, not one for each scale
        true_bboxes = cells_to_bboxes(
            labels[2], anchor, S=S, is_preds=False
        )

        for idx in range(batch_size):
            nms_boxes = non_max_suppression(
                bboxes[idx],
                iou_threshold=iou_threshold,
                threshold=threshold,
                box_format=box_format,
            )

            for nms_box in nms_boxes:
                all_pred_boxes.append([train_idx] + nms_box)

            for box in true_bboxes[idx]:
                if box[0] > threshold:
                    all_true_boxes.append([train_idx] + box)

            train_idx += 1

    model.train()
    return all_pred_boxes, all_true_boxes

def cells_to_bboxes(predictions, anchors, S, is_preds=True):
    """
    Scales the predictions coming from the models to
    be relative to the entire image such that they for example later
    can be plotted or evaluated.
    Parameters
    ----------
    predictions : tensor
                The size is (N, 3, S, S, 5)
    anchors : np.ndarray
            The anchors used for the predictions
    S : int
        The number of cells the image is divided in on the width (and height)
    is_preds : bool
            Whether the input is predictions or the true bounding boxes
    Returns
    -------
    converted_bboxes : list
                    the converted boxes of sizes (N, num_anchors, S, S, 1+5) with
                    object confidence, bounding box coordinates
    """
    batch_size = predictions.shape[0]
    num_anchors = len(anchors)
    box_predictions = predictions[..., 1:5]
    if is_preds:
        anchors = anchors.reshape(1, num_anchors, 1, 1, 2)
        box_predictions[..., 0:2] = torch.sigmoid(box_predictions[..., 0:2])
        box_predictions[..., 2:] = torch.exp(box_predictions[..., 2:]) * anchors
        scores = torch.sigmoid(predictions[..., 0:1])
    else:
        scores = predictions[..., 0:1]

    cell_indices = (
        torch.arange(S)
            .repeat(predictions.shape[0], num_anchors, S, 1)
            .unsqueeze(-1)
            .to(predictions.device)
    )
    x = 1 / S * (box_predictions[..., 0:1] + cell_indices)
    y = 1 / S * (box_predictions[..., 1:2] + cell_indices.permute(0, 1, 3, 2, 4))
    w_h = 1 / S * box_predictions[..., 2:4]
    converted_bboxes = torch.cat((scores, x, y, w_h), dim=-1).reshape(batch_size, num_anchors * S * S, 5)
    return converted_bboxes.tolist()

def get_mean_std(loader):
    # var[X] = E[X**2] - E[X]**2
    channels_sum, channels_sqrd_sum, num_batches = 0, 0, 0

    for data, _ in tqdm(loader):
        channels_sum += torch.mean(data, dim=[0, 2, 3])
        channels_sqrd_sum += torch.mean(data ** 2, dim=[0, 2, 3])
        num_batches += 1

    mean = channels_sum / num_batches
    std = (channels_sqrd_sum / num_batches - mean ** 2) ** 0.5

    return mean, std

def save_checkpoint(model, optimizer, epoch, lr_scheduler, file_path="checkpoint.pth.tar"):
    tqdm.write("=> Saving checkpoint")
    checkpoint = {
        "epoch": epoch,
        "state_dict": model.state_dict(),
        "optimizer": optimizer.state_dict(),
        "scheduler_state": lr_scheduler.state_dict(),
        "anchors" : config.ANCHORS,
        "nms_iou_threshold" : config.NMS_IOU_THRESH,
        "confidence_threshold" : config.CONF_THRESHOLD,
    }
    torch.save(checkpoint, file_path)

def load_checkpoint(checkpoint_file, model, optimizer, lr, lr_scheduler):
    tqdm.write("=> Loading checkpoint")
    checkpoint = torch.load(checkpoint_file, map_location=config.DEVICE)
    model.load_state_dict(checkpoint["state_dict"])
    optimizer.load_state_dict(checkpoint["optimizer"])
    lr_scheduler.load_state_dict(checkpoint["scheduler_state"])
    epoch = checkpoint["epoch"]

    # If we don't do this then it will just have learning rate of old checkpoint
    # and it will lead to many hours of debugging \:
    # for param_group in optimizer.param_groups:
    #     param_group["lr"] = lr
    return epoch

def load_model(model_file, model, device_str=None):
    tqdm.write(f"Loading Model: {model_file}")
    if device_str is not None:
        ckpt = torch.load(model_file, map_location=device_str)
    else:
        ckpt = torch.load(model_file)
    if 'state_dict' in ckpt.keys():
        model.load_state_dict(ckpt['state_dict'])
        return model, {key:ckpt[key] for key in ckpt.keys() if key != 'state_dict'}
    else:
        # compatiable with bare dump
        model.load_state_dict(ckpt)
        return  model, {"anchors" : config.ANCHORS,
                        "nms_iou_threshold" : config.NMS_IOU_THRESH,
                        "confidence_threshold" : config.CONF_THRESHOLD}

def save_checkpoint_to_model(checkpoint_path, model_path):
    """
    Save trained a checkpoint to a model

    Parameters
    ----------
    checkpoint_path : str
    model_path : str
    """
    from . import config
    from ._utils import load_checkpoint
    from .model import YOLOv3
    import torch.optim as optim

    model = YOLOv3().to(config.DEVICE)
    optimizer = optim.Adam(model.parameters(), lr=config.LEARNING_RATE, weight_decay=config.WEIGHT_DECAY)
    lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=2, factor=0.5, verbose=True,
                                                        cooldown=3)
    _ = load_checkpoint(checkpoint_path, model, optimizer, config.LEARNING_RATE, lr_scheduler)
    print("=> Saving model")
    torch.save({
        'state_dict': model.state_dict(),
        "anchors": config.ANCHORS,
        "nms_iou_threshold": config.NMS_IOU_THRESH,
        "confidence_threshold": config.CONF_THRESHOLD}, model_path)

def export_model_to_onnx(model_file, onnx_file):
    import torch.onnx
    from . import config
    from ._utils import load_checkpoint
    from .model import YOLOv3
    import torch.optim as optim

    # A model class instance (class not shown)
    model = YOLOv3()

    # Load the weights from a file (.pth usually)
    state_dict = torch.load(model_file)

    # Load the weights now into a model net architecture defined by our class
    model.load_state_dict(state_dict)

    # Create the right input shape (e.g. for an image)
    dummy_input = torch.randn(2, 1, 416, 416)

    torch.onnx.export(model, dummy_input, onnx_file)

def get_loaders(train_csv_path, validation_csv_path):
    from .dataset import YOLODataset

    image_size = config.IMAGE_SIZE
    train_dataset = YOLODataset(
        train_csv_path,
        transform=config.data_transforms(),
        S=[image_size // 32, image_size // 16, image_size // 8],
        img_dir=config.IMG_DIR,
        label_dir=config.LABEL_DIR,
        anchors=config.ANCHORS,
    )
    validation_dataset = YOLODataset(
        validation_csv_path,
        transform=config.data_transforms(),
        S=[image_size // 32, image_size // 16, image_size // 8],
        img_dir=config.IMG_DIR,
        label_dir=config.LABEL_DIR,
        anchors=config.ANCHORS,
    )

    train_loader = DataLoader(
        dataset=train_dataset,
        batch_size=config.BATCH_SIZE,
        num_workers=config.NUM_WORKERS,
        pin_memory=config.PIN_MEMORY,
        shuffle=False,
        drop_last=False,
    )
    validation_loader = DataLoader(
        dataset=validation_dataset,
        batch_size=config.BATCH_SIZE,
        num_workers=config.NUM_WORKERS,
        pin_memory=config.PIN_MEMORY,
        shuffle=False,
        drop_last=False,
    )

    return train_loader, validation_loader

def seed_everything(seed=42):
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False


text:
# Copyright 2020 The Netket Authors. - All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from netket import legacy as nk
import numpy as np

# 1D Lattice
l = 20
g = nk.graph.Hypercube(length=l, n_dim=1, pbc=True)

# Hilbert space of spins on the graph
# with total Sz equal to 0
hi = nk.hilbert.Spin(s=1 / 2, N=g.n_nodes, total_sz=0)

# Heisenberg hamiltonian
ha = nk.operator.Heisenberg(hilbert=hi)

# Symmetric RBM Spin Machine
ma = nk.machine.RbmSpinSymm(alpha=1, hilbert=hi, automorphisms=g)
ma.init_random_parameters(seed=1234, sigma=0.01)

# defining the custom sampler
# here we use two types of moves : 2-spin exchange between two random neighboring sites,
# and 4-spin exchanges between 4 random neighboring sites
# note that each line and column have to add up to 1.0 (stochastic matrices)

one_exchange_operator = [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
two_exchange_operator = np.kron(one_exchange_operator, one_exchange_operator)

ops = [one_exchange_operator] * l + [two_exchange_operator] * l

# Single exchange operator acts on pairs of neighboring sites
acting_on = [[i, (i + 1) % l] for i in range(l)]

# Double exchange operator acts on cluster of 4 neighboring sites
acting_on += [[i, (i + 1) % l, (i + 2) % l, (i + 3) % l] for i in range(l)]

move_op = nk.operator.LocalOperator(hilbert=hi, operators=ops, acting_on=acting_on)

sa = nk.sampler.CustomSampler(machine=ma, move_operators=move_op)

# Optimizer
op = nk.optimizer.Sgd(ma, learning_rate=0.05)

# Stochastic reconfiguration
gs = nk.variational.Vmc(
    hamiltonian=ha,
    sampler=sa,
    optimizer=op,
    n_samples=1000,
    diag_shift=0.1,
    method="Sr",
)

gs.run(n_iter=300, out="test")

max_stars_repo_name: gpescia/MyNetKet

max_issues_repo_name: gpescia/MyNetKet

max_forks_repo_name: gpescia/MyNetKet


text:
import torch.nn as nn
import torch.nn.functional as F
import torch
import numpy as np

class Discriminator(nn.Module):
    def __init__(self,x):
        super(Discriminator, self).__init__()

        self.model = nn.Sequential(
            nn.Linear(x, 512),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(512, 256),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(256, 3),
            nn.Sigmoid(),
        )

    def forward(self, img):
        img_flat = img.view(img.size(0), -1)
        validity = self.model(img_flat)

        return validity

max_stars_repo_name: OZOOOOOH/colon

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_name: OZOOOOOH/colon

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_name: OZOOOOOH/colon

max_forks_repo_licenses: MIT Unlicense


text:
# Copyright 2019 The ROBEL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Avoid task.

The robot must navigate to a target point while avoiding an obstacle.
By default, this environment does not employ any randomizations, and so will
exhibit poor sim2real transfer. For better transfer, one may want to borrow
some of the randomizations used in DKittyWalkRandomDynamics.
"""

import collections
from typing import Dict, Optional, Sequence, Union

import numpy as np

from robel.components.tracking import TrackerComponentBuilder, TrackerState
from robel.dkitty.walk import BaseDKittyWalk
from robel.utils.math_utils import calculate_cosine

DKITTY_ASSET_PATH = 'robel/dkitty/assets/dkitty_avoid-v0.xml'

DEFAULT_OBSERVATION_KEYS = (
    'root_pos',
    'root_euler',
    'kitty_qpos',
    'root_vel',
    'root_angular_vel',
    'kitty_qvel',
    'last_action',
    'upright',
    'heading',
    'block_error',
    'target_error',
)

class DKittyAvoid(BaseDKittyWalk):
    """Avoid task."""

    def __init__(
            self,
            asset_path: str = DKITTY_ASSET_PATH,
            observation_keys: Sequence[str] = DEFAULT_OBSERVATION_KEYS,
            block_tracker_id: Optional[Union[str, int]] = None,
            frame_skip: int = 40,
            upright_threshold: float = 0.9,  # cos(~25deg)
            upright_reward: float = 1,
            falling_reward: float = 0,  # Don't penalize falling.
            **kwargs):
        """Initializes the environment.

        Args:
            See BaseDKittyWalk.
        """
        self._block_tracker_id = block_tracker_id

        super().__init__(
            asset_path=asset_path,
            observation_keys=observation_keys,
            frame_skip=frame_skip,
            upright_threshold=upright_threshold,
            upright_reward=upright_reward,
            falling_reward=falling_reward,
            **kwargs)
        self._initial_block_pos = np.array([1., 0., 0.2])

    def _configure_tracker(self, builder: TrackerComponentBuilder):
        """Configures the tracker component."""
        super()._configure_tracker(builder)
        builder.add_tracker_group(
            'block',
            hardware_tracker_id=self._block_tracker_id,
            sim_params=dict(
                element_name='block',
                element_type='body',
            ))

    def _reset(self):
        """Resets the environment."""
        target_dist = self.np_random.uniform(low=1.5, high=2.5)
        target_theta = np.pi / 2 + self.np_random.uniform(low=-1, high=1)
        self._initial_target_pos = target_dist * np.array([
            np.cos(target_theta), np.sin(target_theta), 0
        ])

        block_dist = max(
            0.6, target_dist * self.np_random.uniform(low=0.3, high=0.8))
        block_theta = target_theta + self.np_random.uniform(low=-0.5, high=0.5)
        self._initial_block_pos = np.array([
            block_dist * np.cos(block_theta),
            block_dist * np.sin(block_theta),
            0.2,
        ])

        self._reset_dkitty_standing()

        target_pos = self._initial_target_pos
        heading_pos = self._initial_heading_pos
        if heading_pos is None:
            heading_pos = target_pos
        block_pos = self._initial_block_pos

        # Set the tracker locations.
        self.tracker.set_state({
            'torso': TrackerState(pos=np.zeros(3), rot=np.identity(3)),
            'target': TrackerState(pos=target_pos),
            'heading': TrackerState(pos=heading_pos),
            'block': TrackerState(pos=block_pos),
        })

    def get_obs_dict(self) -> Dict[str, np.ndarray]:
        """Returns the current observation of the environment.

        Returns:
            A dictionary of observation values. This should be an ordered
            dictionary if `observation_keys` isn't set.
        """
        robot_state = self.robot.get_state('dkitty')
        target_state, heading_state, block_state, torso_track_state = (
            self.tracker.get_state(['target', 'heading', 'block', 'torso']))

        target_xy = target_state.pos[:2]
        block_xy = block_state.pos[:2]
        kitty_xy = torso_track_state.pos[:2]

        # Get the heading of the torso (the x-axis).
        current_heading = torso_track_state.rot[:2, 0]

        # Get the direction towards the heading location.
        desired_heading = heading_state.pos[:2] - kitty_xy

        # Calculate the alignment of the heading with the desired direction.
        heading = calculate_cosine(current_heading, desired_heading)

        return collections.OrderedDict((
            # Add observation terms relating to being upright.
            *self._get_upright_obs(torso_track_state).items(),
            ('root_pos', torso_track_state.pos),
            ('root_euler', torso_track_state.rot_euler),
            ('root_vel', torso_track_state.vel),
            ('root_angular_vel', torso_track_state.angular_vel),
            ('kitty_qpos', robot_state.qpos),
            ('kitty_qvel', robot_state.qvel),
            ('last_action', self._get_last_action()),
            ('heading', heading),
            ('target_pos', target_xy),
            ('block_error', block_xy - kitty_xy),
            ('target_error', target_xy - kitty_xy),
        ))

    def get_reward_dict(
            self,
            action: np.ndarray,
            obs_dict: Dict[str, np.ndarray],
    ) -> Dict[str, np.ndarray]:
        """Returns the reward for the given action and observation."""
        target_xy_dist = np.linalg.norm(obs_dict['target_error'])

        block_xy_dist = np.abs(obs_dict['block_error'])
        block_cost = -40 * np.all(block_xy_dist <= 0.5)
        bonus = 20 * (target_xy_dist < 0.5) * np.any(block_xy_dist > 0.5)

        reward_dict = collections.OrderedDict((
            # Add reward terms for being upright.
            *self._get_upright_rewards(obs_dict).items(),
            # Reward for proximity to the target.
            ('target_dist_cost', -4 * target_xy_dist),
            ('block_cost', block_cost),
            ('bonus', bonus),
        ))
        return reward_dict

    def get_score_dict(
            self,
            obs_dict: Dict[str, np.ndarray],
            reward_dict: Dict[str, np.ndarray],
    ) -> Dict[str, np.ndarray]:
        """Returns a standardized measure of success for the environment."""
        return collections.OrderedDict((
            ('points', -np.linalg.norm(obs_dict['target_error'])),
            ('success', reward_dict['bonus'] > 0.0),
        ))

    def get_done(
            self,
            obs_dict: Dict[str, np.ndarray],
            reward_dict: Dict[str, np.ndarray],
    ) -> np.ndarray:
        """Returns whether the episode should terminate."""
        return np.array(False)  # Never terminate.


text:
#importing required library
import cv2
import numpy as np
from PIL import Image

#create a cascade classifier to detect face
faceDet = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#create a recognizer
rec = cv2.face.LBPHFaceRecognizer_create()
#load recognizer with trained data
rec.read('recognizer/training.yml')

#open camera
vid_cap = cv2.VideoCapture(0)
while True:
    _ , face = vid_cap.read()
    gray = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
    points = faceDet.detectMultiScale(gray,1.3,5)
    for x,y,w,h in points:
        #draw a rectangle on face in image
        cv2.rectangle(face,(x,y),(x+w,y+h),(255,0,0),2)
        ## predicting the id of image
        id ,config = rec.predict(gray[y:y+h , x:x+w])
        if id == 1:
            id="neeraj"
        if id == 2:
            id = "uday"
        # put label on photo
        cv2.putText(face,id,(x,y),cv2.FONT_HERSHEY_SIMPLEX,2,(255,255,0),2,cv2.LINE_AA)
        cv2.imshow('complete',face)

    if cv2.waitKey(1) & 0xFF == ord('q'): # If we type 'q' on the keyboard:
        break

# close all stuffs
vid_cap.release()
cv2.destroyAllWindows()