text:
"""Utilities to deal with the cameras of human3.6m"""

from __future__ import division

import h5py
import numpy as np
#import matplotlib.pyplot as plt
#import matplotlib.image as mpimg
#import data_utils
#import viz
import random

def project_point_radial( P, R, T, f, c, k, p ):
  """
  Project points from 3d to 2d using camera parameters
  including radial and tangential distortion

  Args
    P: Nx3 points in world coordinates
    R: 3x3 Camera rotation matrix
    T: 3x1 Camera translation parameters
    f: (scalar) Camera focal length
    c: 2x1 Camera center
    k: 3x1 Camera radial distortion coefficients
    p: 2x1 Camera tangential distortion coefficients
  Returns
    Proj: Nx2 points in pixel space
    D: 1xN depth of each point in camera space
    radial: 1xN radial distortion per point
    tan: 1xN tangential distortion per point
    r2: 1xN squared radius of the projected points before distortion
  """

  # P is a matrix of 3-dimensional points
  assert len(P.shape) == 2
  assert P.shape[1] == 3

  N = P.shape[0]
  X = R.dot( P.T - T ) # rotate and translate
  XX = X[:2,:] / X[2,:]
  r2 = XX[0,:]**2 + XX[1,:]**2

  radial = 1 + np.einsum( 'ij,ij->j', np.tile(k,(1, N)), np.array([r2, r2**2, r2**3]) );
  tan = p[0]*XX[1,:] + p[1]*XX[0,:]

  XXX = XX * np.tile(radial+tan,(2,1)) + np.outer(np.array([p[1], p[0]]).reshape(-1), r2 )

  Proj = (f * XXX) + c
  Proj = Proj.T

  D = X[2,]

  return Proj, D, radial, tan, r2

def world_to_camera_frame(P, R, T):
  """
  Convert points from world to camera coordinates

  Args
    P: Nx3 3d points in world coordinates
    R: 3x3 Camera rotation matrix
    T: 3x1 Camera translation parameters
  Returns
    X_cam: Nx3 3d points in camera coordinates
  """

  assert len(P.shape) == 2
  assert P.shape[1] == 3

  X_cam = R.dot( P.T - T ) # rotate and translate

  return X_cam.T

def camera_to_world_frame(P, R, T):
  """Inverse of world_to_camera_frame

  Args
    P: Nx3 points in camera coordinates
    R: 3x3 Camera rotation matrix
    T: 3x1 Camera translation parameters
  Returns
    X_cam: Nx3 points in world coordinates
  """

  assert len(P.shape) == 2
  assert P.shape[1] == 3

  X_cam = R.T.dot( P.T ) + T # rotate and translate

  return X_cam.T

def load_camera_params( hf, path ):
  """Load h36m camera parameters

  Args
    hf: hdf5 open file with h36m cameras data
    path: path or key inside hf to the camera we are interested in
  Returns
    R: 3x3 Camera rotation matrix
    T: 3x1 Camera translation parameters
    f: (scalar) Camera focal length
    c: 2x1 Camera center
    k: 3x1 Camera radial distortion coefficients
    p: 2x1 Camera tangential distortion coefficients
    name: String with camera id
  """

  R = hf[ path.format('R') ][:]
  R = R.T

  T = hf[ path.format('T') ][:]
  f = hf[ path.format('f') ][:]
  c = hf[ path.format('c') ][:]
  k = hf[ path.format('k') ][:]
  p = hf[ path.format('p') ][:]

  name = hf[ path.format('Name') ][:]
  name = "".join( [chr(item) for item in name] )

  return R, T, f, c, k, p, name

def synthetic_cam_parameters( hf, path ):
  rt = random.randint(1,9)
  R = hf[ path.format('R') ][:]
  R = R.T

  T = hf[ path.format('T') ][:] + rt
  f = hf[ path.format('f') ][:]
  c = hf[ path.format('c') ][:]
  k = hf[ path.format('k') ][:]
  p = hf[ path.format('p') ][:]

  name = hf[ path.format('Name') ][:]
  name = "".join( [chr(item) for item in name] )

  return R, T, f, c, k, p, name

def load_cameras( bpath='cameras.h5', subjects=[1,5,6,7,8,9,11] ):
  """Loads the cameras of h36m

  Args
    bpath: path to hdf5 file with h36m camera data
    subjects: List of ints representing the subject IDs for which cameras are requested
  Returns
    rcams: dictionary of 4 tuples per subject ID containing its camera parameters for the 4 h36m cams
  """
  rcams = {}

  with h5py.File(bpath,'r') as hf:
    for s in subjects:
      cams = []
      for c in range(4): # There are 4 cameras in human3.6m
        rcams[(s, c+1)] = load_camera_params(hf, 'subject%d/camera%d/{0}' % (s,c+1) )
        R, T, f, c, k, p, name = load_camera_params(hf, 'subject%d/camera%d/{0}' % (s,c+1) )
        cams.append(T)
      #changing the last canera value as synthetic value
      #rcams[(s, c + 2)] = synthetic_cam_parameters(hf, 'subject%d/camera%d/{0}' % (s, c + 1))
      """"
      import matplotlib.pyplot as plt
      fig = plt.figure()
      ax = fig.add_subplot(111, projection='3d')
      cam = np.array((cams))
      ax.scatter(cam[:,0],cam[:,1],cam[:,2], c='r')
      plt.show()
      """
  #find_avg_cam_pos(rcams)
  return rcams

def find_avg_cam_pos_per_subj(rcams, s=1):
  cams = {}

  #get all the cameras from the data
  #can have synthetic camera which are not saved in file
  #but present in data.
  for (sub,c),val in rcams.items():
    if sub == s :
      cams.update({c : val})
  x = np.array((
    ([0],[0],[0])
  ))
  #now average them
  for n,val in cams.items():
    R, T, f, c, k, p, name = val
    x = x+T

  #zeroing the 3rd value as we donot intend to average the z value as this will change the height of the camera
  x[2,:] = 0
  print("average of camera pos: \n", x/len(cams))
  return x/len(cams)


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

import pandas as pd
import numpy as np
import argparse
import os
import csv
import cv2
import time

sys.path.append('../')
from Analysis.utils import read_yaml

def gofAndSurfaceSweepFile(dataframe, settings_path, output_path, fileName, gof_thresh = 0.06, surface_thresh = 0.35):
    """
    Determine the Kalman predicted values for all the detections in the input file, using the provided threshold values for the Goodness-of-Fit and HOS Surface thresholds
    Saves a new csv file with the new Kalman predictions

    Input:
        dataframe: Pandas dataframe containing the csv data of the to be evaluated video
        settings_path: Path to the original settings file
        output_path: Where to save the output csv file with the updated Kalman predictions
        fileName: Filename of the investigated file
        gof_thresh: Threshold for the Goodness of Fit test
        surface_thresh: Threshold of the HOS Surface
    """

    old_settings = read_yaml(settings_path)
    c = old_settings["c"]
    maxBlobSize = old_settings["maximumBlobSize"]
    dm = old_settings["dm"]
    verbose = old_settings["verbose"]
    saveImage = old_settings["saveImg"]
    debugFlag = old_settings["debug"]

    settingsName = "Settings_{}_c_{}_dm_{}_mbs_{}_gof_{:.2f}_surf_{:.2f}.txt".format(fileName,c,dm,maxBlobSize, gof_thresh, surface_thresh)
    settingsFile = os.path.join(output_path, settingsName)

    ## Write settings file with parameters
    with open(settingsFile, "w") as file:
        file.write("%YAML:1.0\n")
        file.write("c: {}\n".format(c))
        file.write("minimumBlobSize: 4\n")
        file.write("maximumBlobSize: {}\n".format(maxBlobSize))
        file.write("dm: {}\n".format(dm))
        file.write("maxGoFDifference: {:.4f}\n".format(gof_thresh))
        file.write("minimumGaussianSurface: {:.4f}\n".format(surface_thresh))
        file.write("emMaxIterations: 100\n")
        file.write("saveImg: {}\n".format(saveImage))
        file.write("verbose: {}\n".format(verbose))
        file.write("debug: {}\n".format(debugFlag))

    # Initialize kalman filter for the HOS distribution parameters, using a constant velocity model
    kalman_mean = 0.0
    kalman_stddev = 0.0
    kalman_mixprop = 0.0

    KF = cv2.KalmanFilter(6,3,0, cv2.CV_64F)

    KF.transitionMatrix = np.array([[1., 0., 0., 1., 0., 0.],
                                    [0., 1., 0., 0., 1., 0.],
                                    [0., 0., 1., 0., 0., 1.],
                                    [0., 0., 0., 1., 0., 0.],
                                    [0., 0., 0., 0., 1., 0.],
                                    [0., 0., 0., 0., 0., 1.]])
    KF.measurementMatrix = np.array([[1., 0., 0., 0., 0., 0.],
                                    [0., 1., 0., 0., 0., 0.],
                                    [0., 0., 1., 0., 0., 0.],])
    KF.processNoiseCov = np.eye(6) * 0.01
    KF.measurementNoiseCov = np.eye(3) * 0.1
    KF.errorCovPost = np.eye(6)

    file_path = os.path.join(output_path, fileName)

    with open("{}_c_{}_dm_{}_mbs_{}_gof_{:.2f}_surf_{:.2f}.csv".format(file_path, c, dm, maxBlobSize, gof_thresh, surface_thresh), 'w', newline = "") as csvWriteFile:
        writer = csv.writer(csvWriteFile, delimiter=";")

        # Write the headers in the new csv file
        firstrow = []
        firstrow.append("settingsFile")
        firstrow.append("InputVideo")
        firstrow.append("Frame")
        firstrow.append("GaussMean")
        firstrow.append("GaussStdDev")
        firstrow.append("GaussMixProp")
        firstrow.append("Goodness-Of-Fit Value")
        firstrow.append("kalmanGaussMean")
        firstrow.append("kalmanGaussStdDev")
        firstrow.append("kalmanGaussMixProp")
        firstrow.append("Rain Intensity")
        firstrow.append("Kalman Rain Intensity")
        firstrow.append("EM Rain Detected")
        firstrow.append("Kalman Rain Detected")

        writer.writerow(firstrow)

        ## Go throuh each detection and evaluate according to the set threshold parameters
        for index, row in dataframe.iterrows():
            em_mean = row[" GaussMean"]
            em_stddev = row["GaussStdDev"]
            em_mixprop = row["GaussMixProp"]
            em_rainintensity_csv = row["Rain Intensity"]
            em_rainDetected = False
            kalman_rainDetected = False
            kalman_rainIntensity = 0.0

            ## Sanity check of whether the EM estimated mean is above 0
            if(em_mean >= 0):
                gof_val = row["Goodness-Of-Fit Value"]

                kalman_mixprop_csv = row["kalmanGaussMixProp"]
                kalman_rainintensity_csv = row["Kalman Rain Intensity"]

                # Estimate the surface value from the determined rain intensity, since the surface value is not directly saved.
                em_div = em_mixprop > 0.0
                kalman_div = kalman_mixprop_csv > 0.0
                surface = 0

                # If rain is detected for both EM and kalmna, take the average of the estimated surface values
                # Else take the value of the single estimate available
                if em_div and kalman_div:
                    em_surface = em_rainintensity_csv/em_mixprop
                    kalman_surface = kalman_rainintensity_csv/kalman_mixprop_csv

                    surface = (kalman_surface + em_surface)/2
                elif em_div:
                    surface = em_rainintensity_csv/em_mixprop
                elif kalman_div:
                    surface = kalman_rainintensity_csv/kalman_mixprop_csv
                else:
                    surface = 0.

                # Check whether the Goodness-Of-Fitness test is passed
                gof_passed = gof_val <= gof_thresh

                if gof_passed:
                    kalmanPredict = KF.predict()
                    measurement = np.array([em_mean, em_stddev, em_mixprop])
                    measurement = measurement.reshape((-1,1))
                    estimated = KF.correct(measurement)

                    kalman_mean = estimated[0]
                    kalman_stddev = estimated[1]
                    kalman_mixprop = estimated[2]

                # Check whether the Surface test is passed
                em_surface_passed = em_mixprop >= surface_thresh
                kalman_surface_passed = kalman_mixprop >= surface_thresh

                # Determine whether rain is detected or not
                em_rainDetected = gof_passed and em_surface_passed
                kalman_rainDetected = gof_passed and kalman_surface_passed
                kalman_rainIntensity = surface * kalman_mixprop

            csv_row = []
            csv_row.append(settingsFile)
            csv_row.append(row["InputVideo"])
            csv_row.append(row[" Frame#"])
            csv_row.append(em_mean)
            csv_row.append(em_stddev)
            csv_row.append(em_mixprop)
            csv_row.append(row["Goodness-Of-Fit Value"])
            csv_row.append(float(kalman_mean))
            csv_row.append(float(kalman_stddev))
            csv_row.append(float(kalman_mixprop))
            csv_row.append(em_rainintensity_csv)
            csv_row.append(float(kalman_rainIntensity))
            csv_row.append(int(em_rainDetected))
            csv_row.append(int(kalman_rainDetected))
            writer.writerow(csv_row)

def gofAndSurfaceSweep(args):
    """
    Main loop for analysing several files with new Goodness-Of-Fit and HOS Surface thresholds.
    Assumes the input is structured per date, and that per date there is a folder per instance (e.g. per hour), each containing the produced csv and settings files.

    Input:
        args:
            - inputFolder: Path to the input folder
            - outputFolder: Path to the output folder
            - minGOF: Minimum GOF threshold in the range [0; 100]
            - maxGOF: Maximum GOF threshold in the range [0; 100] (Not included)
            - GOFsteps: The step size used between minGOF and maxGOF. Should be integer
            - minSurface: Minimum surface threshold value in the range [0; 100]
            - maxSurface: Maximum surface threshold value in the range [0; 100] (Not included)
            - SurfaceSteps: The step size used between minSurface and maxSurface. Should be integer
    """

    main_path = args["inputFolder"]
    main_output_path = args["outputFolder"]

    if not os.path.exists(main_output_path):
        os.makedirs(main_output_path)

    for dirs in os.listdir(main_path):
        dir_path = os.path.join(main_path, dirs)
        print(dirs)

        output_path = os.path.join(main_output_path, dirs)

        for subdir in os.listdir(dir_path):
            subdir_path = os.path.join(dir_path, subdir)
            files =  os.listdir(subdir_path)

            csv_file = ""
            settings_file = ""

            # Get csv and settigns file paths
            for file in files:
                if os.path.splitext(file)[-1] == ".csv":
                    csv_file = os.path.join(subdir_path, file)
                if os.path.splitext(file)[-1] == ".txt":
                    settings_file = os.path.join(subdir_path, file)

            print(subdir)
            print(files)
            print(csv_file)
            print()

            # Create output folder
            subdir_output_path = os.path.join(output_path, subdir)
            if not os.path.exists(subdir_output_path):
                os.makedirs(subdir_output_path)

            ###### LOAD BOSSU OUTPUT ######
            #Load the supplied csv file
            rain_dataframe = pd.read_csv(csv_file, sep=";")

            for gof in range(args["minGOF"], args["maxGOF"], args["GOFsteps"]):
                gof /= 100
                for surface in range(args["minSurface"], args["maxSurface"], args["SurfaceSteps"]):
                    surface /= 100

                    _start = time.time()
                    gofAndSurfaceSweepFile(rain_dataframe, settings_file, subdir_output_path, dirs, gof, surface)
                    _end = time.time()

                    print("GoF: {:.2f}, Surface Thresh: {:.2f}, Time: {:.2f}".format(gof, surface, _end-_start))

if __name__ == "__main__":
    ap = argparse.ArgumentParser(
            description = "Takes a set of output files from the Bossu C++ code and sweeps over the parameter thresholds affecting the kalman filter")
    ap.add_argument("-inputFolder", "--inputFolder", type=str, required = True, default = ".\ParameterSweep",
                    help="Path to the main folder holding all the csv files to be evaluated")
    ap.add_argument("-outputFolder", "--outputFolder", type=str, required = True, default=".\ParameterSweepGoFSurface",
                    help="Path to the folder where the output plots and csv file should be saved")
    ap.add_argument("-minGOF", "--minGOF", type=int, default = 1, required = True,
                    help="Min surface threshold value. Should be integer and in the range [0; 100]")
    ap.add_argument("-maxGOF", "--maxGOF", type=int, default = 21, required = True,
                    help="Max GOF threshold value. Should be integer and in the range [0; 100]. The value is not included")
    ap.add_argument("-GOFsteps", "--GOFsteps", type=int, default = 1, required = True,
                    help="Step size for the GOF threshold parameters. Should be an integer value")
    ap.add_argument("-minSurface", "--minSurface", type=int, default = 20, required = True,
                    help="Min surface threshold value. Should be integer and in the range [0; 100]")
    ap.add_argument("-maxSurface", "--maxSurface", type=int, default = 51, required = True,
                    help="Max surface threshold value. Should be integer and in the range [0; 100]. The value is not included")
    ap.add_argument("-SurfaceSteps", "--SurfaceSteps", type=int, default = 2, required = True,
                    help="Step size for the surface threshold parameters. Should be an integer value")

    args = vars(ap.parse_args())

    gofAndSurfaceSweep(args)

ext: py

max_stars_repo_name: chrisbahnsen/aau-virada

max_issues_repo_name: chrisbahnsen/aau-virada

max_forks_repo_name: chrisbahnsen/aau-virada

reason: import numpy


text:
# encoding=utf8
import logging

import numpy as np

from niapy.algorithms.algorithm import Algorithm

logging.basicConfig()
logger = logging.getLogger('niapy.algorithms.modified')
logger.setLevel('INFO')

__all__ = ['ParameterFreeBatAlgorithm']

class ParameterFreeBatAlgorithm(Algorithm):
    r"""Implementation of Parameter-free Bat algorithm.

    Algorithm:
        Parameter-free Bat algorithm

    Date:
        2020

    Authors:
        Iztok Fister Jr.
        This implementation is based on the implementation of basic BA from niapy

    License:
        MIT

    Reference paper:
        Iztok Fister Jr., Iztok Fister, Xin-She Yang. Towards the development of a parameter-free bat algorithm . In: FISTER Jr., Iztok (Ed.), BRODNIK, Andrej (Ed.). StuCoSReC : proceedings of the 2015 2nd Student Computer Science Research Conference. Koper: University of Primorska, 2015, pp. 31-34.

    Attributes:
        Name (List[str]): List of strings representing algorithm name.

    See Also:
        * :class:`niapy.algorithms.Algorithm`

    """

    Name = ['ParameterFreeBatAlgorithm', 'PLBA']

    @staticmethod
    def info():
        r"""Get algorithms information.

        Returns:
            str: Algorithm information.

        See Also:
            * :func:`niapy.algorithms.Algorithm.info`

        """
        return r"""Iztok Fister Jr., Iztok Fister, Xin-She Yang. Towards the development of a parameter-free bat algorithm . In: FISTER, Iztok (Ed.), BRODNIK, Andrej (Ed.). StuCoSReC : proceedings of the 2015 2nd Student Computer Science Research Conference. Koper: University of Primorska, 2015, pp. 31-34."""

    def __init__(self, *args, **kwargs):
        """Initialize ParameterFreeBatAlgorithm."""
        super().__init__(80, *args, **kwargs)
        self.loudness = 0.9
        self.pulse_rate = 0.1

    def set_parameters(self, **kwargs):
        r"""Set the parameters of the algorithm.

        See Also:
            * :func:`niapy.algorithms.Algorithm.set_parameters`

        """
        super().set_parameters(population_size=80, **kwargs)
        self.loudness = 0.9
        self.pulse_rate = 0.1

    def init_population(self, task):
        r"""Initialize the initial population.

        Args:
            task (Task): Optimization task

        Returns:
            Tuple[numpy.ndarray, numpy.ndarray[float], Dict[str, Any]]:
                1. New population.
                2. New population fitness/function values.
                3. Additional arguments:
                    * velocities (numpy.ndarray[float]): Velocities

        See Also:
            * :func:`niapy.algorithms.Algorithm.init_population`

        """
        population, fitness, d = Algorithm.init_population(self, task)
        velocities = np.zeros((self.population_size, task.dimension))
        d.update({'velocities': velocities})
        return population, fitness, d

    def local_search(self, best, task, **_kwargs):
        r"""Improve the best solution according to the Yang (2010).

        Args:
            best (numpy.ndarray): Global best individual.
            task (Task): Optimization task.

        Returns:
            numpy.ndarray: New solution based on global best individual.

        """
        return task.repair(best + 0.001 * self.normal(0, 1, task.dimension))

    def run_iteration(self, task, population, population_fitness, best_x, best_fitness, **params):
        r"""Core function of Parameter-free Bat Algorithm.

        Args:
            task (Task): Optimization task.
            population (numpy.ndarray): Current population
            population_fitness (numpy.ndarray[float]): Current population fitness/function values
            best_x (numpy.ndarray): Current best individual
            best_fitness(float): Current best individual function/fitness value
            params (Dict[str, Any]): Additional algorithm arguments

        Returns:
            Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, Dict[str, Any]]:
                1. New population
                2. New population fitness/function values
                3. New global best solution
                4. New global best fitness/objective value
                5. Additional arguments:
                    * velocities (numpy.ndarray): Velocities

        """
        velocities = params.pop('velocities')
        upper, lower = task.upper, task.lower
        for i in range(self.population_size):
            frequency = ((upper[0] - lower[0]) / float(self.population_size)) * self.normal(0, 1)
            velocities[i] += (population[i] - best_x) * frequency
            if self.random() > self.pulse_rate:
                solution = self.local_search(best=best_x, task=task, i=i, Sol=population)
            else:
                solution = task.repair(population[i] + velocities[i], rng=self.rng)
            new_fitness = task.eval(solution)
            if (new_fitness <= population_fitness[i]) and (self.random() < self.loudness):
                population[i], population_fitness[i] = solution, new_fitness
            if new_fitness <= best_fitness:
                best_x, best_fitness = solution.copy(), new_fitness
        return population, population_fitness, best_x, best_fitness, {'velocities': velocities}


text:
# coding=utf8
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage import morphology,color,data
from skimage import filters
import numpy as np
import skimage
import os
from skimage import measure

def watershed(image, label=None):
    denoised = filters.rank.median(image, morphology.disk(2)) #过滤噪声
    #将梯度值低于10的作为开始标记点
    markers = filters.rank.gradient(denoised, morphology.disk(5)) < 10
    markers = ndi.label(markers)[0]

    gradient = filters.rank.gradient(denoised, morphology.disk(2)) #计算梯度
    labels =morphology.watershed(gradient, markers, mask=image) #基于梯度的分水岭算法

    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6))
    axes = axes.ravel()
    ax0, ax1, ax2, ax3 = axes

    ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
    ax0.set_title("Original")
    # ax1.imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest')
    ax1.imshow(gradient, cmap=plt.cm.gray, interpolation='nearest')
    ax1.set_title("Gradient")
    if label is not None:
        # ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')
        ax2.imshow(label, cmap=plt.cm.gray, interpolation='nearest')
    else:
        ax2.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')
    ax2.set_title("Markers")
    ax3.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')
    ax3.set_title("Segmented")

    for ax in axes:
        ax.axis('off')

    fig.tight_layout()
    plt.show()

def plot_4(image, gradient,label,segmentation, save_path=None):
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6, 6))
    axes = axes.ravel()
    ax0, ax1, ax2, ax3 = axes
    ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
    ax0.set_title("Original")
    ax1.imshow(gradient, cmap=plt.cm.gray, interpolation='nearest')
    ax1.set_title("Gradient")
    ax2.imshow(label, cmap=plt.cm.gray, interpolation='nearest')
    ax2.set_title("label")
    ax3.imshow(segmentation, cmap=plt.cm.spectral, interpolation='nearest')
    ax3.set_title("Segmented")

    for ax in axes:
        ax.axis('off')

    fig.tight_layout()
    if save_path:
        print save_path
        plt.savefig(save_path)
    else:
        plt.show()

def fill(image):
    '''
    填充图片内部空白
    临时写的函数
    建议后期替换
    '''
    label_img = measure.label(image, background=1)
    props = measure.regionprops(label_img)
    max_area = np.array([p.area for p in props]).max()
    for i,prop in enumerate(props):
        if prop.area < max_area:
            image[prop.coords[:,0],prop.coords[:,1]] = 1
    return image

def my_watershed(image, label=None, min_gray=480, max_gray=708, min_gradient=5, show=False, save_path='/tmp/x.jpg'):
    image = image - min_gray
    image[image>max_gray] = 0
    image[image< 10]  = 0
    image = image * 5

    denoised = filters.rank.median(image, morphology.disk(2)) #过滤噪声
    #将梯度值低于10的作为开始标记点
    markers = filters.rank.gradient(denoised, morphology.disk(5)) < 10
    markers = ndi.label(markers)[0]

    gradient = filters.rank.gradient(denoised, morphology.disk(2)) #计算梯度
    labels = gradient > min_gradient

    mask = gradient > min_gradient
    label_img = measure.label(mask, background=0)
    props = measure.regionprops(label_img)
    pred = np.zeros_like(gradient)
    for i,prop in enumerate(props):
        if prop.area > 50:
            region = np.array(prop.coords)
            vx,vy = region.var(0)
            v = vx + vy
            if v < 200:
                pred[prop.coords[:,0],prop.coords[:,1]] = 1

    # 填充边缘内部空白
    pred = fill(pred)

    if show:
        plot_4(image, gradient, label, pred)
    else:
        plot_4(image, gradient, label, pred, save_path)

    return pred

def segmentation(image_npy, label_npy,save_path):
    print image_npy
    image = np.load(image_npy)
    label = np.load(label_npy)
    if np.sum(label) == 0:
        return
    min_gray,max_gray = 480, 708
    my_watershed(image,label,min_gray, max_gray,show=False, save_path=save_path)

def main():
    data_dir = '/home/yin/all/PVL_DATA/preprocessed/2D/'
    save_dir = '/home/yin/all/PVL_DATA/tool_result/'
    os.system('rm -r ' + save_dir)
    os.system('mkdir ' + save_dir)
    for patient in os.listdir(data_dir):
        patient_dir = os.path.join(data_dir, patient)
        for f in os.listdir(patient_dir):
            if 'roi.npy' in f:
                label_npy = os.path.join(patient_dir,f)
                image_npy = label_npy.replace('.roi.npy','.npy')
                segmentation(image_npy,label_npy, os.path.join(save_dir,label_npy.strip('/').replace('/','.').replace('npy','jpg')))

if __name__ == '__main__':
    # image =color.rgb2gray(data.camera())
    # watershed(image)
    main()
    image_npy = '/home/yin/all/PVL_DATA/preprocessed/2D/JD_chen_xi/23.npy'
    image_npy = '/home/yin/all/PVL_DATA/preprocessed/2D/JD_chen_xi/14.npy'
    image_npy = '/home/yin/all/PVL_DATA/preprocessed/2D/JD_zhang_yu_chen/23.npy'
    label_npy = image_npy.replace('.npy','.roi.npy')
    segmentation(image_npy,label_npy)

reason: import numpy,from scipy


text:
"""Extracting Video Spatial Features using model-based transfer learning"""

from argparse import ArgumentParser
import torch
from torchvision import transforms, models
import torch.nn as nn
from torch.utils.data import Dataset
import skvideo.io
from PIL import Image
import h5py
import numpy as np
import random
import time
import os

class VideoDataset(Dataset):
    """Read data from the original dataset for feature extraction"""
    def __init__(self, videos_dir, video_names, score, video_format='RGB', width=None, height=None):

        super(VideoDataset, self).__init__()
        self.videos_dir = videos_dir
        self.video_names = video_names
        self.score = score
        self.format = video_format
        self.width = width
        self.height = height

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

    def __getitem__(self, idx):
        video_name = self.video_names[idx]
        assert self.format == 'YUV420' or self.format == 'RGB'
        if self.format == 'YUV420':
            video_data = skvideo.io.vread(os.path.join(self.videos_dir, video_name), self.height[idx], self.width[idx], inputdict={'-pix_fmt':'yuvj420p'})
        else:
            video_data = skvideo.io.vread(os.path.join(self.videos_dir, video_name))
        video_score = self.score[idx]

        transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])

        video_length = video_data.shape[0]
        video_channel = video_data.shape[3]
        video_height = video_data.shape[1]
        video_width = video_data.shape[2]
        print('video_width: {} video_height: {}'.format(video_width, video_height))
        transformed_video = torch.zeros([video_length, video_channel,  video_height, video_width])
        for frame_idx in range(video_length):
            frame = video_data[frame_idx]
            frame = Image.fromarray(frame)
            frame = transform(frame)
            transformed_video[frame_idx] = frame

        sample = {'video': transformed_video, 'score': video_score}

        return sample

class CNNModel(torch.nn.Module):
    """Modified CNN models for feature extraction"""
    def __init__(self, model='ResNet-50'):
        super(CNNModel, self).__init__()
        if model == 'SpatialExtractor':
            print("use SpatialExtractor")
            from SpatialExtractor.get_spatialextractor_model import make_spatial_model
            model = make_spatial_model()
            self.features = nn.Sequential(*list(model.module.backbone.children())[:-2])
        else:
            print("use default ResNet-50")
            self.features = nn.Sequential(*list(models.resnet50(pretrained=True).children())[:-2])

    def forward(self, x):
        x = self.features(x)
        features_mean = nn.functional.adaptive_avg_pool2d(x, 1)
        features_std = global_std_pool2d(x)
        return features_mean, features_std

def global_std_pool2d(x):
    """2D global standard variation pooling"""
    return torch.std(x.view(x.size()[0], x.size()[1], -1, 1), dim=2, keepdim=True)

def get_features(video_data, frame_batch_size=64, model='ResNet-50', device='cuda'):
    """feature extraction"""
    extractor = CNNModel(model=model).to(device)
    video_length = video_data.shape[0]
    frame_start = 0
    frame_end = frame_start + frame_batch_size
    output1 = torch.Tensor().to(device)
    output2 = torch.Tensor().to(device)
    extractor.eval()
    with torch.no_grad():
        while frame_end < video_length:
            batch = video_data[frame_start:frame_end].to(device)
            features_mean, features_std = extractor(batch)
            output1 = torch.cat((output1, features_mean), 0)
            output2 = torch.cat((output2, features_std), 0)
            frame_end += frame_batch_size
            frame_start += frame_batch_size

        last_batch = video_data[frame_start:video_length].to(device)
        features_mean, features_std = extractor(last_batch)
        output1 = torch.cat((output1, features_mean), 0)
        output2 = torch.cat((output2, features_std), 0)
        output = torch.cat((output1, output2), 1).squeeze()

    if output.ndim == 1:
        output = output.unsqueeze(0)

    return output

if __name__ == "__main__":
    parser = ArgumentParser(description='Extracting Video Spatial Features using model-based transfer learning')
    parser.add_argument("--seed", type=int, default=19901116)
    parser.add_argument('--database', default='CVD2014', type=str,
                        help='database name (default: CVD2014)')
    parser.add_argument('--model', default='SpatialExtractor', type=str,
                        help='which pre-trained model used (default: ResNet-50)')
    parser.add_argument('--frame_batch_size', type=int, default=64,
                        help='frame batch size for feature extraction (default: 64)')
    parser.add_argument('--disable_gpu', action='store_true', help='flag whether to disable GPU')
    parser.add_argument("--ith", type=int, default=0, help='start video id')
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(args.seed)
    random.seed(args.seed)

    torch.utils.backcompat.broadcast_warning.enabled = True

    if args.database == 'KoNViD-1k':
        videos_dir = 'KoNViD-1k/'  # videos dir, e.g., ln -s /xxx/KoNViD-1k/ KoNViD-1k
        features_dir = 'CNN_features_KoNViD-1k/SpatialFeature/'  # features dir
        datainfo = 'data/KoNViD-1kinfo.mat'  # database info: video_names, scores; video format, width, height, index, ref_ids, max_len, etc.
    if args.database == 'CVD2014':
        videos_dir = 'CVD2014/'
        features_dir = 'CNN_features_CVD2014/SpatialFeature/'
        datainfo = 'data/CVD2014info.mat'
    if args.database == 'LIVE-Qualcomm':
        videos_dir = 'LIVE-Qualcomm/'
        features_dir = 'CNN_features_LIVE-Qualcomm/SpatialFeature/'
        datainfo = 'data/LIVE-Qualcomminfo.mat'
    if args.database == 'LIVE-VQC':
        videos_dir = 'LIVE-VQC/'
        features_dir = 'CNN_features_LIVE-VQC/SpatialFeature/'
        datainfo = 'data/LIVE-VQCinfo.mat'
    if args.database == 'YouTube-UGC':
        videos_dir = 'YouTube_UGC/'
        features_dir = 'CNN_features_YouTube-UGC/SpatialFeature/'
        datainfo = 'data/YouTube-UGCinfo.mat'
    if args.database == 'LSVQ':
        videos_dir = 'LSVQ/'
        features_dir = 'CNN_features_LSVQ/SpatialFeature/'
        datainfo = 'data/LSVQinfo.mat'

    if not os.path.exists(features_dir):
        os.makedirs(features_dir)

    device = torch.device("cuda" if not args.disable_gpu and torch.cuda.is_available() else "cpu")

    Info = h5py.File(datainfo, 'r')
    video_names = [Info[Info['video_names'][0, :][i]][()].tobytes()[::2].decode() for i in range(len(Info['video_names'][0, :]))]
    scores = Info['scores'][0, :]
    video_format = Info['video_format'][()].tobytes()[::2].decode()
    width = Info['widths'][0, :]
    height = Info['heights'][0, :]
    dataset = VideoDataset(videos_dir, video_names, scores, video_format, width, height)

    max_len = 0
    min_len = 100000
    for i in range(args.ith, len(dataset)):
        start = time.time()
        current_data = dataset[i]
        print('Video {} : length {}'.format(i, current_data['video'].shape[0]))
        if max_len < current_data['video'].shape[0]:
            max_len = current_data['video'].shape[0]
        if min_len > current_data['video'].shape[0]:
            min_len = current_data['video'].shape[0]
        features = get_features(current_data['video'], args.frame_batch_size, args.model, device)
        np.save(features_dir + str(i) + '_' + args.model +'_last_conv', features.to('cpu').numpy())
        np.save(features_dir + str(i) + '_score', current_data['score'])
        end = time.time()
        print('{} seconds'.format(end-start))
    print('Max length: {} Min length: {}'.format(max_len,  min_len))


text:
import argparse
import json
import numpy as np
import pandas as pd

from sklearn.metrics import pairwise_distances

class MoviesRecommender:

    def __init__(self, data_path):
        self.data_file = self._get_data_file(data_path)

    def _get_data_file(self, data_path):
        """ Return a json object containing the file content """

        return json.load(open(data_path))

    def _get_movies(self):
        """ Return a dict of movies """

        return self.data_file['movies']

    def _normalize_dataset(self):
        """ Create DataFrame including rating 5 for all watched movies and
        split to row each movie_id by user """

        new_data = []
        columns = ['user_id', 'movie_id', 'rating']
        for line in self.data_file['users']:
            movies_by_user = [
                {'user_id': line['user_id'], 'movie_id': movie_id, 'rating': 5}
                for movie_id in line['movies']
            ]
            new_data.extend(movies_by_user)
        return pd.DataFrame(new_data, columns=columns)

    def _get_sparse_data(self):
        """ Read a normalized dataset and create a sparse matrix
        containing the ratings """
        data = self._normalize_dataset()

        n_users = data.user_id.unique().shape[0]
        n_movies = data.movie_id.unique().shape[0]

        ratings = np.zeros((n_users, n_movies))

        for row in data.itertuples():
            ratings[row[1]-1, row[2]-1] = row[3]
        return ratings

    def _create_train_test_data(self, ratings):
        test = np.zeros(ratings.shape)
        train = ratings.copy()
        for user in range(ratings.shape[0]):
            test_ratings = np.random.choice(
                ratings[user, :].nonzero()[0],
                size=5
            )
            train[user, test_ratings] = 0.
            test[user, test_ratings] = ratings[user, test_ratings]

        assert(np.all((train * test) == 0))
        return train, test

    def _get_item_correlation(self, train):
        item_correlation = 1 - pairwise_distances(
            train.T,
            metric='correlation'
        )
        item_correlation[np.isnan(item_correlation)] = 0.
        return item_correlation

    def _get_top_k_movies(self, similarity, movie_id, k):
        """ Return a list of k recommended movies """
        return [
            self._get_movies()[str(x+1)]
            for x in np.argsort(similarity[movie_id-1,:])[:-k-1:-1]
        ]

    def recommend(self, movie_id, k=5):
        ratings = self._get_sparse_data()
        train, _ = self._create_train_test_data(ratings)
        item_correlation = self._get_item_correlation(train)
        return self._get_top_k_movies(item_correlation, movie_id, k)

if __name__ == "__main__":
    k = 5
    parser = argparse.ArgumentParser(
        description="Movies Recommender Application",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument(
        '--input', type=str, required=True,
        dest='inputfile', help='data file path')

    parser.add_argument(
        '--movie_id', type=int, required=True,
        dest='movie_id', help='Movie ID')
    parser.add_argument(
        '--k', type=int, required=False,
        dest='k', help='Number of recommended movies'
    )

    args = parser.parse_args()
    if args.k:
        k = args.k
    movies_recommender = MoviesRecommender(args.inputfile)
    movie_ids = movies_recommender.recommend(args.movie_id, k)
    print(movie_ids)

max_stars_repo_name: nicolasmota/movies-recommender

max_issues_repo_name: nicolasmota/movies-recommender

max_forks_repo_name: nicolasmota/movies-recommender


text:
from datetime import datetime
import numpy as np
from scipy.ndimage import rotate
from tifffile import imread

from labeling import Labeling as lb
#from line_profiler import LineProfiler

result = np.zeros((512, 512))
resolution = (512, 512)

def read_img():
    files = []
    for i in range(1, 6):
        for j in range(1, 5):
            files.append("C:/mpicbg/metaseg_data/Euclidean_squared_distance/000/diverse" + str(i) + "/sweep_" + str(
                j) + "_mask.tif")
    images = imread(files)
    return images

def test1():
    # setting up the images for second example
    example2_images = [np.invert(imread("tutorial/up_big.tif"))]
    example2_images[0][example2_images[0] > 0] = 130
    example2_images.append(
        rotate(np.transpose(np.flip(example2_images[0]).copy()), angle=45, reshape=False, mode="constant", cval=0))
    example2_images[1][example2_images[1] > 0] = 131
    example2_images.append(np.transpose(np.flip(example2_images[0]).copy()))
    example2_images[2][example2_images[2] > 0] = 132
    example2_images.append(rotate(np.flip(example2_images[0]).copy(), angle=45, reshape=False, mode="constant", cval=0))
    example2_images[3][example2_images[3] > 0] = 133
    example2_images.append(np.flip(example2_images[0]).copy())
    example2_images[4][example2_images[4] > 0] = 134
    example2_images.append(
        rotate(np.transpose(example2_images[0]).copy(), angle=45, reshape=False, mode="constant", cval=0))
    example2_images[5][example2_images[5] > 0] = 135
    example2_images.append(np.transpose(example2_images[0]).copy())
    example2_images[6][example2_images[6] > 0] = 136
    example2_images.append(rotate(example2_images[0], angle=45, reshape=False, mode="constant", cval=0))
    example2_images[7][example2_images[7] > 0] = 137

    print(np.argwhere( example2_images[0]))

    merger = lb.Labeling.fromValues(np.zeros((512, 512), np.int32))
    merger.iterate_over_images(example2_images, [str(int) for int in list(range(1, len(example2_images) + 1))])
    meta = {
        "date": "2021-06-28",
        "revision": 1,
        "author": "Tom Burke"
    }
    #merger.add_metadata(meta)
    print(merger.metadata)
    img, labeling2 = merger.save_result("example2")
    print(merger.metadata)
    print(labeling2.metadata)

def test4():
    global result, img
    a = np.zeros((4, 4), np.int32)
    a[:2] = 1
    example1_images = []
    example1_images.append(a)
    #example1_images.append(a.copy())
    b = a.copy()
    example1_images.append(np.flip(b.transpose()))
    c = a.copy()
    example1_images.append(np.flip(c))
    d = a.copy()
    example1_images.append(d.transpose())
    e = np.zeros((4, 4), np.int32)
    e[1:3, 1:3] = 1
    example1_images.append(e)
    # Initialize the merger with the first image. This can also be an empty image of zeros in the correct shape
    merger = lb.Labeling.fromValues(first_image=np.zeros((4,4), dtype="int32"))
    merger2 = lb.Labeling.fromValues(first_image=np.zeros((4, 4), dtype="int32"))
    result = merger2.add_segments(example1_images[0], (0, 0), source_id=str(0))
    result = merger2.add_segments(example1_images[1], (0, 0), source_id=str(1))
    result = merger2.add_segments(example1_images[2], (0, 0), source_id=str(2))
    result = merger2.add_segments(example1_images[3], (0, 0), source_id=str(3))
    result = merger2.add_segments(example1_images[4], (0, 0), source_id=str(4))

    merger.iterate_over_images(example1_images,['a','b','c','d','e'])

    img, labeling = merger.save_result("example1_1", True)
    img, labeling = merger2.save_result("example1", True)
    print(img)
    print(vars(labeling))
    # add_anything(data, (x,y,z):Tuple, merge:dict=None)
    # returns dict of actual labels dif

def test3():
    # init
    images = read_img()
    patch_size = 64

    start = datetime.now()
    # initialize the merger
    merger = lb.Labeling(images[0].shape, np.int32)
    # iterate over all images
    i = 0
    for image in images:
        merger.add_segments(patch=image, position=(0, 0), source_id=str(i))
        i += 1
    img, labeling = merger.save_result("big_img", True)
    print(datetime.now() - start)
    #print(vars(labeling))

    # start = datetime.now()
    # # initialize the merger
    # merger = lb.Labeling(images[0].shape, np.int32)
    # # iterate over all images
    # i = 0
    # for image in images:
    #     merger.add_segments2(patch=image, position=(0, 0), source_id=str(i))
    #     i += 1
    # img, labeling = merger.save_result("big_img", True)
    # print(datetime.now() - start)
    #print(vars(labeling))

if __name__ == '__main__':
    #profiler = LineProfiler()
    #profiler.add_function(lb.Labeling.add_segments)
    #profiler.add_function(lb.Labeling.iterate_over_images)
    #profiler.add_function(lb.Labeling.save_result)
    #profiler.add_function(lb.Labeling.add_image)
    #test1()
    #profiler.runcall(test1)

    test1()
    #profiler.runcall(test3)
    #test4()
    #profiler.print_stats()
    #profiler.dump_stats("profiling.lprof")

max_stars_repo_name: Labelings/pyLabeling

max_issues_repo_name: Labelings/pyLabeling

max_forks_repo_name: Labelings/pyLabeling


text:
#!/usr/bin/python

# Copyright (C) 2012 The Android Open Source Project
#
# 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 consts import *
import numpy as np
import scipy as sp
import scipy.fftpack as fft
import matplotlib.pyplot as plt
import sys
sys.path.append(sys.path[0])
import calc_delay

# check if amplitude of DUT's playback
#  lies in the given error boundary
# input: host record
#        sampling rate
#        low frequency in Hz,
#        high frequency in Hz,
#        allowed error in negative side for pass in %,
#        allowed error ih positive side for pass
# output: min value in negative side, normalized to 1.0
#         max value in positive side
#         calculated freq spectrum in amplittude

def do_check_spectrum_playback(hostData, samplingRate, fLow, fHigh, margainLow, margainHigh):
    # reduce FFT resolution to have averaging effects
    N = 512 if (len(hostData) > 512) else len(hostData)
    iLow = N * fLow / samplingRate + 1 # 1 for DC
    if iLow > (N / 2 - 1):
        iLow = (N / 2 - 1)
    iHigh = N * fHigh / samplingRate + 1 # 1 for DC
    if iHigh > (N / 2 + 1):
        iHigh = N / 2 + 1
    print fLow, iLow, fHigh, iHigh, samplingRate

    Phh, freqs = plt.psd(hostData, NFFT=N, Fs=samplingRate, Fc=0, detrend=plt.mlab.detrend_none,\
        window=plt.mlab.window_hanning, noverlap=0, pad_to=None, sides='onesided',\
        scale_by_freq=False)
    print len(Phh)
    print "Phh", abs(Phh[iLow:iHigh])
    spectrum = np.sqrt(abs(Phh[iLow:iHigh]))
    spectrumMean = np.mean(spectrum)
    spectrum = spectrum / spectrumMean
    print "Mean ", spectrumMean
    print "Normalized spectrum", spectrum
    positiveMax = abs(max(spectrum))
    negativeMin = abs(min(spectrum))
    passFail = True if (positiveMax < (margainHigh / 100.0 + 1.0)) and\
        ((1.0 - negativeMin) < margainLow / 100.0) else False
    spectrumResult = np.zeros(len(spectrum), dtype=np.int16)
    for i in range(len(spectrum)):
        spectrumResult[i] = spectrum[i] * 1024 # make fixed point
    print "positiveMax", positiveMax, "negativeMin", negativeMin
    return (passFail, negativeMin, positiveMax, spectrumResult)

def check_spectrum_playback(inputData, inputTypes):
    output = []
    outputData = []
    outputTypes = []
    # basic sanity check
    inputError = False
    if (inputTypes[0] != TYPE_MONO):
        inputError = True
    if (inputTypes[1] != TYPE_I64):
        inputError = True
    if (inputTypes[2] != TYPE_I64):
        inputError = True
    if (inputTypes[3] != TYPE_I64):
        inputError = True
    if (inputTypes[4] != TYPE_DOUBLE):
        inputError = True
    if (inputTypes[5] != TYPE_DOUBLE):
        inputError = True
    if inputError:
        output.append(RESULT_ERROR)
        output.append(outputData)
        output.append(outputTypes)
        return output
    hostData = inputData[0]
    samplingRate = inputData[1]
    fLow = inputData[2]
    fHigh = inputData[3]
    margainLow = inputData[4]
    margainHigh = inputData[5]
    (passFail, minError, maxError, Spectrum) = do_check_spectrum_playback(hostData, \
        samplingRate, fLow, fHigh, margainLow, margainHigh)

    if passFail:
        output.append(RESULT_PASS)
    else:
        output.append(RESULT_OK)
    outputData.append(minError)
    outputTypes.append(TYPE_DOUBLE)
    outputData.append(maxError)
    outputTypes.append(TYPE_DOUBLE)
    outputData.append(Spectrum)
    outputTypes.append(TYPE_MONO)
    output.append(outputData)
    output.append(outputTypes)
    return output

# test code
if __name__=="__main__":
    sys.path.append(sys.path[0])
    mod = __import__("gen_random")
    peakAmpl = 10000
    durationInMSec = 1000
    samplingRate = 44100
    fLow = 500
    fHigh = 15000
    data = getattr(mod, "do_gen_random")(peakAmpl, durationInMSec, samplingRate, fHigh,\
        stereo=False)
    print len(data)
    (passFail, minVal, maxVal, amp) = do_check_spectrum_playback(data, samplingRate, fLow,\
        fHigh, 1.0, 1.0)
    plt.plot(amp)
    plt.show()

max_stars_repo_name: HelixOS/cts

max_issues_repo_name: HelixOS/cts

max_forks_repo_name: HelixOS/cts

reason: import numpy,import scipy


text:
import time
from absl import app, flags, logging
from absl.flags import FLAGS
import cv2
import numpy as np
import tensorflow as tf
from yolov3_tf2.models import YoloV3, YoloV3Tiny
from yolov3_tf2.dataset import transform_images, load_tfrecord_dataset, transform_targets
from yolov3_tf2.utils import draw_outputs
import config as cfg
import argparse
# from tensorflow_core.python.keras.models import load_model

def main(args):
    class_path = args.classes  # Path to classes file
    weights = args.weights  # Path to weight file
    image_size = cfg.IMAGE_SIZE  # Resize images to size - 416 04 608
    image = ''  # Path to input image
    tfrecord = args.dataset  # tfrecord instead of image or None
    output = args.output  # Path to output image
    num_classes = args.num_classes  # Number of classes in model

    anchors = cfg.YOLO_ANCHORS
    anchor_masks = cfg.YOLO_ANCHOR_MASKS

    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    for physical_device in physical_devices:
        tf.config.experimental.set_memory_growth(physical_device, True)

    yolo = YoloV3(image_size, training=False, classes=num_classes)
    yolo.load_weights(weights).expect_partial()
    print('weights loaded')

    class_names = [c.strip() for c in open(class_path).readlines()]
    print('classes loaded')

    if tfrecord:
        val_dataset = load_tfrecord_dataset(tfrecord, class_path, image_size)
        # val_dataset = val_dataset.shuffle(512)
        val_dataset = val_dataset.batch(1)
        val_dataset = val_dataset.map(lambda x, y: (
            transform_images(x, image_size),
            transform_targets(y, anchors, anchor_masks, image_size)))
        # img_raw, _label = next(iter(dataset.take(1)))
    else:
        img_raw = tf.image.decode_image(
            open(image, 'rb').read(), channels=3)

    index = 0
    for img_raw, _label in val_dataset.take(25):
        # img = tf.expand_dims(img_raw, 0)
        img = transform_images(img_raw, image_size)
        img = img * 255

        boxes, scores, classes, nums = yolo(img)

        output = '/Users/justinbutler/Desktop/test/test_images/test_{}.jpg'.format(index)
        output = '/home/justin/Models/yolov3-tf2/test_images/test_{}.jpg'.format(index)
        print('output saved to: {}'.format(output))

        img = cv2.cvtColor(img_raw[0].numpy(), cv2.COLOR_RGB2BGR)
        img = draw_outputs(img, (boxes, scores, classes, nums), class_names, thresh=0)
        img = img * 255
        cv2.imwrite(output, img)

        index = index + 1

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--num_classes', default=80, type=int)
    parser.add_argument('--classes', help='path to classes .names file')
    parser.add_argument('--weights', default='checkpoints/yolov3_608.tf')
    parser.add_argument('--dataset', help='path to tfrecord file')
    parser.add_argument('--output')

    arguments = parser.parse_args()
    main(arguments)


text:
'''

The code for the lookahead policy we use in our
Static Model

'''

import numpy as np

# the lookahead policy
class LookaheadPolicy():
	def __init__(self, model):
		self.model = model

	# function returning the decision x_t from the current state
	# and current time. The argument decisions is given to
	# use a local variable rather than for getting outside information
	def get_decision(self,  METRIC):

		# the matrix with decisions to be made for each node and each time
		decisions = [ ([0] * self.model.G.vertexCount) for row in range(self.model.G.Horizon + 1) ]

		# initialize the value costs at different nodes at different times to infinity
		V = np.ones((self.model.G.Horizon + 1, self.model.G.vertexCount)) * np.inf
		# make the costs at the destination 0
		for t_prime in range(self.model.G.Horizon + 1):
			V[t_prime][self.model.G.end_node] = 0

		# the algortihm that uses the "stepping backwards in time" method
		lookAheadTime = self.model.G.Horizon - 1
		while lookAheadTime >= 0:
			for k in range(self.model.G.vertexCount):
				# find the solutions to Bellman's eq. that are shown
				# in 5.22 and 5.23
				argMin = - 1
				minVal = np.inf
				for l in self.model.G.neighbors[k]:
					if (METRIC == "PERCENTILE"):
						spread = self.model.G.spreads[k][l]
						mean = self.model.estimated_costs[k][l]
						if minVal >= V[lookAheadTime + 1][l] + self.use_percentile_val(self.model.theta, spread, mean):
							argMin = l
							minVal = V[lookAheadTime + 1][l] + self.use_percentile_val(self.model.theta, spread, mean)
					else:
						if minVal >= V[lookAheadTime + 1][l] + dist[k][l]:
							argMin = l
							minVal = V[lookAheadTime + 1][l] + dist[k][l]

				# updating the solutions to the equations
				V[lookAheadTime][k] = minVal
				decisions[lookAheadTime][k] = argMin
			lookAheadTime -= 1

		return decisions[0][self.model.state.node]

	'''
	the function that takes as arguments the percentile we are going to
	use, theta (espressed as a value in [0,1]), the spread for a link and
	the mean cost of that link and returns the value corresponding to
	the theta precentile of the interval [(1 - spread) * mean, (1 + spread) * mean]
	'''
	def use_percentile_val(self,theta, spread, mean):
		point_val = 1 - spread + (2 * spread) * theta
		used_cost = mean * point_val
		return used_cost

max_stars_repo_name: nikunjpansari/stochastic-optimization

max_issues_repo_name: nikunjpansari/stochastic-optimization

max_forks_repo_name: nikunjpansari/stochastic-optimization


text:
import cv2
import numpy as np
import pickle
import sys
import os

face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

def face_detector(frame):

    # Convert image to grayscale
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray, 1.3, 5)
    if faces is ():
        return None,None

    roi = []
    cord = []

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
        cv2.rectangle(frame, (x,y-30), (x+w,y), (0,255,255), -1)
        cv2.putText(frame, 'None', (x+2,y-10), 4, 0.6, (255,255,255), 1)
        # cv2.circle(frame, (x+130,y), 1, (0,0,255), -1)
        croi = frame[y:y+h, x:x+w]
        l = []
        l.append((x,y))
        l.append((x+w,y+h))
        cord.append(l)
        roi.append(cv2.resize(croi, (200, 200)))
    return roi,cord

if __name__ == '__main__':

    totalModel = 0

    with open('train/FaceData/imagecount.txt','r') as f:
        totalModel = int(f.read())+1

    modelNameList = []

    for i in range(totalModel):
        modelNameList.append('train/FaceData/'+str(i)+'.cv2')

    nameList = []

    modelList = []

    pmodelList = []

    for i in range(totalModel):
        modelList.append(cv2.createLBPHFaceRecognizer())
        # print(modelList[i])

    for i in range(totalModel):
        modelList[i].load(modelNameList[i])

    with open('train/FaceData/nameList','r') as f:
                nameList = pickle.load(f)

    # print(nameList)

    try:

        if sys.argv[1]=='-dir':
            if os.path.exists(sys.argv[2]):
                frame = cv2.imread(sys.argv[2])

                frame = cv2.resize(frame,(500,400))

                faces,positions = face_detector(frame)

                if faces is None:
                    cv2.imshow('Face Recognition', frame )
                else:
                    try:
                        for i,face in enumerate(faces):
                            face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

                            for j,model in enumerate(modelList):
                                results = model.predict(face)

                                if results[1] < 500:
                                    confidence = int( 100 * (1 - (results[1])/400) )
                                    if confidence>=70:
                                        cv2.rectangle(frame, (positions[i][0][0],positions[i][0][1]-30), (positions[i][1][0],positions[i][0][1]), (0,0,255), -1)
                                        cv2.putText(frame, nameList[j], (positions[i][0][0]+2,positions[i][0][1]-10), 4, 0.6, (255,255,255), 1)
                                        cv2.rectangle(frame, positions[i][0], positions[i][1], (0,0,255), 2)
                                        break

                    except:
                        raise

                cv2.imshow('Face Recognition', frame )
                cv2.waitKey(0)
                cv2.destroyAllWindows()
            else:
                raise

        elif sys.argv[1]=='-cam':

            camNumber = 0

            try:
                camNumber =sys.argv[2]
            except:
                pass

            cap = cv2.VideoCapture(camNumber)

            while True:

                ret, frame = cap.read()

                frame = cv2.resize(frame, (500,400))

                faces,positions = face_detector(frame)

                if faces is None:
                    cv2.imshow('Face Recognition', frame )
                else:
                    try:
                        for i,face in enumerate(faces):
                            face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

                            for j,model in enumerate(modelList):
                                results = model.predict(face)

                                if results[1] < 500:
                                    confidence = int( 100 * (1 - (results[1])/400) )
                                    if confidence>=75:
                                        cv2.rectangle(frame, (positions[i][0][0],positions[i][0][1]-30), (positions[i][1][0],positions[i][0][1]), (0,0,255), -1)
                                        cv2.putText(frame, nameList[j], (positions[i][0][0]+2,positions[i][0][1]-10), 4, 0.6, (255,255,255), 1)
                                        cv2.rectangle(frame, positions[i][0], positions[i][1], (0,0,255), 2)
                                        break

                    except:
                        raise

                cv2.imshow('Face Recognition', frame )

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

            cap.release()
            cv2.destroyAllWindows()

        else:
            raise

    except:
        print('Please pass arguments , while running program.')
        print("-dir Absolute_Path_To_Testing_Image_File_Is")
        print("-cam Camera_Number_Default_Is_Set_To_0")
        print('----------')
        raise

max_stars_repo_name: prajwalsingh/Face-Recognition-With-Name

max_issues_repo_name: prajwalsingh/Face-Recognition-With-Name

max_forks_repo_name: prajwalsingh/Face-Recognition-With-Name


text:
# created by Tom Stesco tom.s@ecobee.com

from abc import ABC, abstractmethod
from enum import IntEnum
import logging

import attr
import pandas as pd
import numpy as np

@attr.s
class StateEstimatorModel(ABC):
    """ABC for state estimator models"""

    input_states = attr.ib()
    output_states = attr.ib()

    output = attr.ib(factory=dict)
    step_output = attr.ib(factory=dict)
    settings = attr.ib(factory=dict)

    @abstractmethod
    def initialize(
        self, start_utc, t_start, t_end, t_step, data_spec, categories_dict
    ):
        """Run on first setup and not again."""
        pass

    @abstractmethod
    def do_step(self):
        """Defines sequence of step internals."""
        pass

    @abstractmethod
    def change_settings(self, new_settings):
        """Change persistent internal settings to model."""
        pass

    @abstractmethod
    def get_model_name(self):
        """Defines human readable uniquely identifing name"""
        pass

max_stars_repo_name: Kunind/building-controls-simulator

max_issues_repo_name: Kunind/building-controls-simulator

max_forks_repo_name: Kunind/building-controls-simulator


text:
from copy import deepcopy

import numpy as np
import pandas as pd
from scipy.spatial import ConvexHull

# # Definitions and custom functions
# Color Cycle used throughout the plugin for collective events.
# Color values correspond to hex values of the matplotlib tab20
# colorscale
COLOR_CYCLE = [
    "#1f77b4",
    "#aec7e8",
    "#ff7f0e",
    "#ffbb78",
    "#2ca02c",
    "#98df8a",
    "#d62728",
    "#ff9896",
    "#9467bd",
    "#c5b0d5",
    "#8c564b",
    "#c49c94",
    "#e377c2",
    "#f7b6d2",
    "#7f7f7f",
    "#c7c7c7",
    "#bcbd22",
    "#dbdb8d",
    "#17becf",
    "#9edae5",
]
# text parameters for the timestamp
text_parameters = {
    "text": "{label}",
    "size": 12,
    "color": "white",
    "anchor": "center",
    "translation": [0, 0],
}

def make_timestamp(
    viewer,
    start_time=0,
    step_time=1,
    prefix="T =",
    suffix="frame",
    position="upper_left",
    size=12,
    x_shift=12,
    y_shift=0,
):
    """
    Create a timestamp displayed in the viewer.
    This is done by creating a dummy shape layer
    and annotating it with the current time.
    """
    anchors = ["upper_right", "upper_left", "lower_right", "lower_left", "center"]
    if position not in anchors:
        raise ValueError(f'"position" must be one of: {anchors}')

    text_parameters_tmstp = {
        "text": "{label}",
        "size": size,
        "color": "white",
        "anchor": position,
        "translation": [x_shift, y_shift],
    }
    out = {}
    rgt, rgy, rgx = deepcopy(viewer.dims.range)
    # Napari uses float64 for dims
    maxx, maxy, maxt = rgx[1], rgy[1], rgt[1] - 1
    # Points to the corners of the image at each frame
    corners = [
        np.array(
            [
                [t, np.float64(0), np.float64(0)],
                [t, np.float64(0), maxx],
                [t, maxy, maxx],
                [t, maxy, np.float64(0)],
            ]
        )
        for t in np.arange(maxt + 1).astype("float64")
    ]
    out["properties"] = {}
    timestamp = [start_time + step * step_time for step in range(int(maxt + 1))]
    out["properties"]["label"] = [f"{prefix} {str(i)} {suffix}" for i in timestamp]
    out["data"] = corners
    # Fully transparent white because just want the text, not the shape
    out["face_color"] = np.repeat("#ffffff00", len(corners))
    out["edge_color"] = np.repeat("#ffffff00", len(corners))
    out["shape_type"] = "rectangle"
    out["text"] = text_parameters_tmstp
    out["opacity"] = 1
    return out

def calculate_convex_hull(array):
    """Calculates the convex hull for a 2d array of points.

    Parameters:
        array (np.ndarray): A 2d array of points with x and y coordinates.

    Returns (np.ndarray): If shape of input array can be used
    to calculate convex hull the vertices of the convex hull are returned.
    If shape is less, the points themselfs are returned.
    """
    if array.shape[0] > 2:
        hull = ConvexHull(array[:, 2:])
        array_out = array[hull.vertices]
        return array_out
    if array.shape[0] == 2:
        return array

def calculate_convex_hull_3d(array):
    """Calculates the convex hull for a 3d array of points.

    Parameters:
        array (np.ndarray): A 2d array of points with x y and z coordinates.

    Returns (np.ndarray): If shape of input array can be used
    to calculate convex hull the vertices of the convex hull are returned.
    If shape is less, the points themselfs are returned.
    """
    if array.shape[0] > 3:
        hull = ConvexHull(array[:, 2:])
        array_faces = hull.simplices
        if array_faces.shape[0] != 0:
            return array_faces
    if array.shape[0] == 3:
        array_faces = np.array([[0, 1, 2]])
        return array_faces
    if array.shape[0] == 2:
        array_faces = np.array([[0, 1, 1]])
        return array_faces
    if array.shape[0] == 1:
        array_faces = np.array([[0, 0, 0]])
        return array_faces

def get_verticesHull(df, frame, colid, col_x, col_y):
    """Calculate convex hull for 2d collective events.

    Input dataframe is converted into a numpy array and split into groups
    according to unique collective ids.
    For each set array the convex hull is calculated.

    Parameters:
        df (pd.DataFrame): A dataframe with at least 4 columns
        containing the XY coordinates of a set of points aswell
        as frame and collective id columns.
        frame (str): Name of frame column in df.
        colid (str): Name of collective id column in df.
        col_x (str): Name of column x coordinate column in df.
        col_y (str): Name of column y coordinate column in df.

    Returns (np.ndarray,np.ndarray): Tuple containing arrays of vertices,
    one for each collective event. Array of colors, a unique
    one for each collective event.
    """
    df = df.sort_values([colid, frame])
    array_txy = df[[colid, frame, col_y, col_x]].to_numpy()
    array_txy = array_txy[~np.isnan(array_txy).any(axis=1)]
    grouped_array = np.split(
        array_txy, np.unique(array_txy[:, 0:2], axis=0, return_index=True)[1][1:]
    )
    # map to grouped_array
    convex_hulls = [calculate_convex_hull(i) for i in grouped_array if i.shape[0] > 1]
    color_ids = np.take(
        np.array(COLOR_CYCLE), [int(i[0, 0]) for i in convex_hulls], mode="wrap"
    )
    # color_ids = recycle_palette(COLOR_CYCLE, len(convex_hulls))
    out = [i[:, 1:] for i in convex_hulls]
    return out, color_ids

# @profile
def make_surface_3d(
    df: pd.DataFrame, frame: str, col_x: str, col_y: str, col_z: str, colid: str
):
    """Calculate convex hull for 3d collective events.

    Input dataframe is converted into a numpy array and split into groups
    according to unique collective ids.
    For each set array the convex hull is calculated.
    A tuple that can be used to add 3d convex hull with the napari
    add_surface function is generated.
    Output has to be appended with empy vertices and surfaces for the
    timepoints where no surface should be drawn. Otherwise will
    result in a nontype subscription error.

    Parameters:
        df (pd.DataFrame): A dataframe with at least 4 columns
        containing the XY coordinates of a set of points aswell
        as frame and collective id columns.
        frame (str): Name of frame column in df.
        colid (str): Name of collective id column in df.
        col_x (str): Name of x coordinate column in df.
        col_y (str): Name of y coordinate column in df.
        col_z (str): Name of z coordinate column in df.

    Returns (tuple(np.ndarray, np.ndarray, np.ndarray)): Tuple that contains
    vertex coordinates, face indices and color ids
    """
    dataFaces = []
    vertices_count = 0
    # sort needed for np.split
    df = df.sort_values([colid, frame])
    array_idtyxz = df[[colid, frame, col_y, col_x, col_z]].to_numpy()
    array_idtyxz = array_idtyxz[~np.isnan(array_idtyxz).any(axis=1)]
    # split array into list of arrays, one for each collid/timepoint combination
    grouped_array = np.split(
        array_idtyxz, np.unique(array_idtyxz[:, 0:2], axis=0, return_index=True)[1][1:]
    )
    # calc convex hull for every array in the list
    convex_hulls = [calculate_convex_hull_3d(i) for i in grouped_array]
    # generates color ids (integers for LUT in napari)
    color_ids = np.concatenate([i[:, 0].astype(np.int64) for i in grouped_array])
    out_vertices = np.concatenate(grouped_array)[:, 1:]
    # merge convex hull face list and shift indexes according to groups
    for i, val in enumerate(convex_hulls):
        dataFaces.append(np.add(val, vertices_count))
        vertices_count += len(grouped_array[i])
    out_faces = np.concatenate(dataFaces)
    return (out_vertices, out_faces, color_ids)

# @profile
def fix_3d_convex_hull(df, vertices, faces, colors, col_t):
    """Generate empty vertex and faces to fix napari subset error.

    Parameters:
        df (pd.DataFrame): A dataframe used to calculate convex hulls.
        vertices (np.ndarray): vertex coordinates.
        faces (np.ndarray): Array containing face indices.
        colors (np.ndarray): Array containing color ids.
        col_t (str): String name of frame column in df.
    """
    empty_vertex = []
    empty_faces = []
    empty_colors = []
    time_points = np.unique(vertices[:, 0])
    arr_size = vertices.shape[0]
    for i in df[col_t].unique():
        if i not in time_points:
            empty_vertex.append([i, 0, 0, 0])
            empty_faces.append([arr_size, arr_size, arr_size])
            arr_size = arr_size + 1
            empty_colors.append(0)

    surface_tuple_0 = np.concatenate((vertices, np.array(empty_vertex)), axis=0)
    surface_tuple_1 = np.concatenate((faces, np.array(empty_faces)), axis=0)
    surface_tuple_2 = np.concatenate((colors, np.array(empty_colors)), axis=0)

    return (surface_tuple_0, surface_tuple_1, surface_tuple_2)

def calc_bbox(array: np.ndarray):
    """Calcualte the bounding box of input array.

    Parameters:
        array (np.ndarray): 2d array of coordinates
        for which to compute the bounding box.

    Returns (np.ndarray): 2d array of coordinates for the bounding box.
    """
    t = array[0, 0]
    pos_array = array[:, 1:]
    # 3d case
    if pos_array.shape[1] == 3:

        miny, minx, minz = np.min(pos_array, axis=0)
        maxy, maxx, maxz = np.max(pos_array, axis=0)
        return np.array(
            [
                [t, miny, minx, minz],
                [t, miny, minx, maxz],
                [t, miny, maxx, maxz],
                [t, miny, maxx, minz],
                [t, maxy, maxx, minz],
                [t, maxy, minx, minz],
                [t, maxy, minx, maxz],
                [t, maxy, maxx, maxz],
            ]
        )
    # 2d case
    miny, minx = np.min(pos_array, axis=0)
    maxy, maxx = np.max(pos_array, axis=0)
    return np.array(
        [[t, miny, minx], [t, miny, maxx], [t, maxy, maxx], [t, maxy, minx]]
    )

def get_bbox(
    df: pd.DataFrame, clid: int, frame: str, xcol: str, ycol: str, edge_size: float = 10
):
    """Get bounding box of dataframe in long format with position columns, for 2d case.

    Parameters:
        df (pd.DataFrame): dataframe to get bounding box form.
        frame (str): Name of frame column.
        xcol (str): X coordinate column.
        ycol (str): Y coordinate column.
        edge_size (float): Bounding Box edge_size, also used to calculate text size.

    Returns (nd.array, dict): Array that can be added to
    napari with add_shapes function aswell
    as dictionary that can be unpacked
    containing kwargs for shapes layer.
    """
    df = df.sort_values([frame])
    array_tpos = df[[frame, ycol, xcol]].to_numpy()
    array_tpos = array_tpos[~np.isnan(array_tpos).any(axis=1)]
    # split array into list of arrays, one for each collid/timepoint combination
    grouped_array = np.split(
        array_tpos, np.unique(array_tpos[:, 0], axis=0, return_index=True)[1][1:]
    )
    # calc bbox for every array in the list
    bbox = [calc_bbox(i) for i in grouped_array]
    text_size = edge_size * 2.5
    if text_size < 1:
        text_size = 1
    text_parameters = {
        "text": "Event Nbr: {label}",
        "size": text_size,
        "color": "white",
        "anchor": "upper_left",
        "translation": [-3, 0],
    }
    bbox_layer: dict = {}
    bbox_layer["properties"] = {}
    bbox_layer["properties"]["label"] = clid
    bbox_layer["text"] = text_parameters
    bbox_layer["face_color"] = "transparent"
    bbox_layer["edge_color"] = "red"
    bbox_layer["edge_width"] = edge_size
    bbox_layer["name"] = "event_boundingbox"

    return bbox, bbox_layer

def get_bbox_3d(df: pd.DataFrame, frame: str, xcol: str, ycol: str, zcol: str):
    """Get bounding box of dataframe in long format with position columns, for 3d case.

    Can be added to napari with the add_surfaces function.

    Parameters:
        df (pd.DataFrame): dataframe to get bounding box form.
        frame (str): Name of frame column.
        xcol (str): X coordinate column.
        ycol (str): Y coordinate column.
        zcol (str): Z coordinate column.

    Returns (nd.array, np.ndarray, np.ndarray): Tuple that
    can be added to napari with add_shapes
    function. Need to be passed on to the fix_3d_convex_hull
    function to avoid indexing errors in napari.
    """
    df = df.sort_values([frame])
    array_tpos = df[[frame, ycol, xcol, zcol]].to_numpy()
    array_tpos = array_tpos[~np.isnan(array_tpos).any(axis=1)]
    # split array into list of arrays, one for each collid/timepoint combination
    grouped_array = np.split(
        array_tpos, np.unique(array_tpos[:, 0], axis=0, return_index=True)[1][1:]
    )
    # calc bbox for every array in the list
    bbox = [calc_bbox(i) for i in grouped_array]
    dataFaces = []
    vertices_count = 0
    data_colors = []
    # precalculated face indidec for a 3d bounding box
    face = np.array(
        [
            [3, 5, 4],
            [3, 5, 0],
            [3, 1, 2],
            [3, 1, 0],
            [7, 3, 2],
            [7, 3, 4],
            [6, 1, 0],
            [6, 5, 0],
            [6, 1, 2],
            [6, 7, 2],
            [6, 5, 4],
            [6, 7, 4],
        ]
    )
    for value in bbox:
        dataFaces.append(np.add(face, vertices_count))
        vertices_count += len(value)
    out_faces = np.concatenate(dataFaces)
    bbox_array = np.concatenate(bbox)
    data_colors = np.array([1 for i in range(bbox_array.shape[0])])
    return (bbox_array, out_faces, data_colors)

max_stars_repo_name: bgraedel/arcos-gui

max_issues_repo_name: bgraedel/arcos-gui

max_forks_repo_name: bgraedel/arcos-gui


text:
from sklearn import tree
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from operator import itemgetter
import pandas as pd
import numpy as np
import graphviz
import matplotlib.pyplot as plt

# get data for LOCF and mean preprocessed
df = pd.read_csv("data/shots_LOCF.csv", index_col="shot_num")
# turn categorical into numerical
df["play_pattern"] = pd.Categorical(df["play_pattern"])
df["play_pattern"] = df["play_pattern"].cat.codes
df["shot_taker_type"] = pd.Categorical(df["shot_taker_type"])
df["shot_taker_type"] = df["shot_taker_type"].cat.codes
df["shot_technique"] = pd.Categorical(df["shot_technique"])
df["shot_technique"] = df["shot_technique"].cat.codes
df["shot_body_part"] = pd.Categorical(df["shot_body_part"])
df["shot_body_part"] = df["shot_body_part"].cat.codes

df_mean = pd.read_csv("data/shots_mean.csv", index_col="shot_num")
df_mean["play_pattern"] = pd.Categorical(df_mean["play_pattern"])
df_mean["play_pattern"] = df_mean["play_pattern"].cat.codes
df_mean["shot_taker_type"] = pd.Categorical(df_mean["shot_taker_type"])
df_mean["shot_taker_type"] = df_mean["shot_taker_type"].cat.codes
df_mean["shot_technique"] = pd.Categorical(df_mean["shot_technique"])
df_mean["shot_technique"] = df_mean["shot_technique"].cat.codes
df_mean["shot_body_part"] = pd.Categorical(df_mean["shot_body_part"])
df_mean["shot_body_part"] = df_mean["shot_body_part"].cat.codes

df = df.sample(frac=1, random_state=42) # shuffle dataset
df_mean = df_mean.sample(frac=1, random_state=42) # shuffle dataset
# print(df.shape[0])
features = df.columns.tolist()
features.remove("goal")
# original X and Y
X_orig = df[features].to_numpy()
Y_orig = df["goal"].to_numpy()

# for mean
Xm_orig = df_mean[features].to_numpy()
Ym_orig = df_mean["goal"].to_numpy()

# oversampling for LOCF
max_size = df["goal"].value_counts().max()
sampled = [df]
for class_index, group in df.groupby("goal"):
    sampled.append(group.sample(max_size-len(group), replace=True))

# LOCF
df_new = pd.concat(sampled)

m_max_size = df_mean["goal"].value_counts().max()
sampled_m = [df_mean]
for class_index, group in df_mean.groupby("goal"):
    sampled_m.append(group.sample(max_size-len(group), replace=True))

df_mnew = pd.concat(sampled_m)
# print(df_new.shape[0])
# print(df_mnew.shape[0])

# split into X and Y
X_train = df_new[features]
Y_train = df_new["goal"]

Xm_train = df_mnew[features]
Ym_train = df_mnew["goal"]

"""
train = df.sample(frac=0.7, random_state=56)
test = df.drop(train.index)

X_train = train[features]
Y_train = train["goal"]

X_test = test[features]
Y_test = test["goal"]
"""

# do cross validation to find the best depth
depth = []
depth_m = []
for i in range(3, 6):
    dt = tree.DecisionTreeClassifier(max_depth=i)
    dt_m = tree.DecisionTreeClassifier(max_depth=i)
    # 5-fold cross validation
    scores = cross_val_score(estimator=dt, X=X_train, y=Y_train, cv=5, n_jobs=2)
    scores_m = cross_val_score(estimator=dt_m, X=Xm_train, y=Ym_train, cv=5, n_jobs=2)
    depth.append((i, scores.mean()))
    depth_m.append((i, scores_m.mean()))

# print(depth)
# print(depth_m)
m_depth = max(depth, key=itemgetter(1))[0] # get max depth from cross validation
mm_depth = max(depth_m, key=itemgetter(1))[0]
# print(m_depth)
# print(mm_depth)
dt_shots = tree.DecisionTreeClassifier(max_depth=m_depth)
dt_shots = dt_shots.fit(X_train, Y_train)

dtm_shots = tree.DecisionTreeClassifier(max_depth=mm_depth)
dtm_shots = dtm_shots.fit(Xm_train, Ym_train)

dot_data = tree.export_graphviz(dt_shots, out_file=None, class_names=["0", "1"],feature_names=features, filled=True, rounded=True, special_characters=True)
graph = graphviz.Source(dot_data)
graph.render("shots_dt")

dotm_data = tree.export_graphviz(dtm_shots, out_file=None, class_names=["0", "1"], feature_names=features, filled=True, rounded=True, special_characters=True)
graph_m = graphviz.Source(dotm_data)
graph_m.render("shots_dtm")

y_pred = dt_shots.predict(X_orig)
y_pred_m = dtm_shots.predict(Xm_orig)

print("LOCF Report")
print(classification_report(Y_orig, y_pred))

print("Mean Report")
print(classification_report(Ym_orig, y_pred_m))

print()
print("LOCF Confusion Matrix")
print(confusion_matrix(Y_orig, y_pred))

print("Mean Confusion Matrix")
print(confusion_matrix(Ym_orig, y_pred_m))


text:
#!/usr/bin/env python
#
# // SPDX-License-Identifier: BSD-3-CLAUSE
#
# (C) Copyright 2018, Xilinx, Inc.
#
#!/usr/bin/python

from __future__ import print_function

import os, sys, argparse
import subprocess
import tensorflow as tf
import numpy as np
import cv2

# Bring in VAI Quantizer, Compiler, and Partitioner
from vai.dpuv1.rt.xdnn_rt_tf import TFxdnnRT as xdnnRT
from vai.dpuv1.rt.xdnn_util import dict2attr, list_depth, make_list
from utils import get_input_fn, top5_accuracy, LABEL_OFFSET, BATCH_SIZE
from shutil import rmtree

# Environment Variables (obtained by running "source overlaybins/setup.sh")
if 'VAI_ALVEO_ROOT' in os.environ and os.path.isdir(os.path.join(os.environ['VAI_ALVEO_ROOT'], 'overlaybins/xdnnv3')):
    XCLBIN = os.path.join(os.environ['VAI_ALVEO_ROOT'], 'overlaybins/xdnnv3')
else:
    XCLBIN = '/opt/xilinx/overlaybins/xdnnv3/'

def get_default_compiler_args():
    return {
        'dsp':                  96,
        'memory':               9,
        'bytesperpixels':       1,
        'ddr':                  256,
        'data_format':          'NHWC',
        'mixmemorystrategy':    True,
        'pipelineconvmaxpool':  True,
        'xdnnv3':               True,
        'usedeephi':            True,
        'pingpongsplit':        True,
        'deconvinfpga':         True,
        'quantz':               '',
        'fcinfpga':             True,
        'bridges': ['bytype', 'Concat'],
    }

if __name__ == "__main__":
  parser = argparse.ArgumentParser(description='pyXFDNN')
  parser.add_argument('--model', default="", help='User must provide the network model file')
  parser.add_argument('--input_nodes', default="", help='User must provide the network input names [comma seperated with no spacing]')
  parser.add_argument('--output_nodes', default="", help='User must provide the network output names [comma seperated with no spacing]')
  parser.add_argument('--input_shapes', default="", help='User must provide the network input shapes [comma seperated with no spacing]')
  parser.add_argument('--output_dir', default="work", help='Optionally, save all generated outputs in specified folder')
  parser.add_argument('--pre_process', default="", help='pre-process function for quantization calibration')
  parser.add_argument('--label_offset', default=LABEL_OFFSET, help='Optionally, label offset of the dataset')
  parser.add_argument('--batch_size', default=BATCH_SIZE, help='batch sizes to run')
  parser.add_argument('--quantize', action="store_true", default=False, help='In quantize mode, model will be Quantize')
  parser.add_argument('--validate_cpu', action="store_true", help='If validation_cpu is enabled, the model will be validated on cpu')
  parser.add_argument('--validate', action="store_true", help='If validation is enabled, the model will be partitioned, compiled, and ran on the FPGA, and the validation set examined')
  parser.add_argument('--c_input_nodes', default=None, help='Compiler input node names')
  parser.add_argument('--c_output_nodes', default=None, help='Compiler output node names')
  args = dict2attr(parser.parse_args())

  if not args.pre_process:
    raise ValueError('please provide --pre_process input. Valid option: resnet50, inception_v1, inception_v3, inception_v4, squeezenet')

  if args.quantize:
    from tensorflow import GraphDef
    from tensorflow.contrib import decent_q

    input_graph_def = GraphDef()
    with open(args.model, "rb") as f:
      input_graph_def.ParseFromString(f.read())

    if os.path.isdir(args.output_dir):
      print('Cleaning model artifacts in {}'.format(os.path.abspath(args.output_dir)))
      filesToClean = [os.path.join(os.path.abspath(args.output_dir),f) for f in os.listdir(args.output_dir)]
      for f in filesToClean:
        if os.path.isfile(f):
          os.remove(f)
        elif os.path.isdir(f):
          rmtree(f)
    else:
      os.makedirs(args.output_dir)

    input_node_names = make_list(args.input_nodes)
    output_node_names = make_list(args.output_nodes)
    input_shapes = make_list(args.input_shapes)
    input_shapes = [input_shapes] if list_depth(input_shapes) == 1 else input_shapes

    if 'VAI_ALVEO_ROOT' in os.environ and os.path.isdir(os.path.join(os.environ['VAI_ALVEO_ROOT'], 'vai/dpuv1')):
      arch_json = os.path.join(os.environ['VAI_ALVEO_ROOT'], 'vai/dpuv1/tools/compile/bin/arch.json')
    elif 'VAI_ROOT' in os.environ:
      arch_json = os.path.join(os.environ['VAI_ROOT'], 'compiler/arch/dpuv1/ALVEO/ALVEO.json')
    else:
      arch_json = '/opt/vitis-ai/compiler/arch/dpuv1/ALVEO/ALVEO.json'
    input_fn = get_input_fn(args.pre_process, args.input_nodes)
    q_config = decent_q.QuantizeConfig(input_nodes = input_node_names,
        output_nodes = output_node_names,
        input_shapes = input_shapes,
        output_dir = args.output_dir,
        method= 1,
        calib_iter = 500 // args.batch_size)
    decent_q.quantize_frozen(input_graph_def, input_fn, q_config)

    #subprocess.call(['vai_q_tensorflow', 'inspect',
    #                 '--input_frozen_graph', args.model])
    #subprocess.call(['vai_q_tensorflow', 'quantize',
    #                 '--input_frozen_graph', args.model,
    #                 '--input_nodes', input_node_names,
    #                 '--output_nodes', output_node_names,
    #                 '--input_shapes', '{},{},{},{}'.format(*args.input_shapes),
    #                 '--output_dir', args.output_dir,
    #                 '--input_fn', 'utils.input_fn',
    #                 '--method', '1',
    #                 '--calib_iter', '{}'.format(500 // args.batch_size)])

    subprocess.call(['vai_c_tensorflow',
                     '--frozen_pb', args.output_dir+'/deploy_model.pb',
                     '--arch', arch_json,
                     '--output_dir', args.output_dir,
                     '--net_name', args.output_dir+'/fix_info.txt',
                     '--quant_info'])

    print("Generated model artifacts in %s"%os.path.abspath(args["output_dir"]))
    for f in os.listdir(args["output_dir"]):
      print("  "+f)

  if args.validate_cpu:
    iter_cnt = 500 // args.batch_size

    tf.reset_default_graph()
    with open(args.model, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString(f.read())
      tf.import_graph_def(graph_def, name='')

    graph = tf.get_default_graph()
    top5_accuracy(graph, args.input_nodes, args.output_nodes, iter_cnt, args.batch_size, args.pre_process, args.label_offset)

  if args.validate:
    iter_cnt = 500 // args.batch_size

    ### Partition and compile
    rt = xdnnRT(None,
                networkfile=args.model,
                quant_cfgfile=args.output_dir+'/fix_info.txt',
                batch_sz=args.batch_size,
                startnode=args.c_input_nodes,
                finalnode=args.c_output_nodes,
                xclbin=XCLBIN,
                device='FPGA',
                placeholdershape="{\'%s\': [%d,%d,%d,%d]}" % (args.input_nodes, args.batch_size, args.input_shapes[1], args.input_shapes[2], args.input_shapes[3]),
                savePyfunc=True,
                **get_default_compiler_args()
               )

    ### Accelerated execution
    ## load the accelerated graph
    graph = rt.load_partitioned_graph()

    top5_accuracy(graph, args.input_nodes, args.output_nodes, iter_cnt, args.batch_size, args.pre_process, args.label_offset)

max_stars_repo_name: asirasa-xilinx/Vitis-AI

max_issues_repo_name: asirasa-xilinx/Vitis-AI

max_forks_repo_name: asirasa-xilinx/Vitis-AI


text:
from abc import ABC, abstractmethod
from numpy import pi

class CoinInitialiser(ABC):

    def __init__(self) -> None:
        self._u1 = pi/2
        self._u2 = pi/2
        self._u3 = 3*pi/2

    def initialise(self,circuit,register):
        """Initialises the quantum coin"""
        circuit.u(self._u1,self._u2,self._u3,register[:])

class SymetricInitialiser(CoinInitialiser):

    def __init__(self) -> None:
        super().__init__()

    def initialise(self, circuit, register):
        """initialises the coin to traverse in symetric superposition"""
        if register.size == 2:
            circuit.u(pi/2,pi/2,3*pi/2,register[0])
            circuit.u(pi/2,3*pi/2,pi/2,register[1])

        else:
            return super().initialise(circuit, register)

class AsymetricInitialiser(CoinInitialiser):

    def __init__(self) -> None:
        self._u1 = 0
        self._u2 = 0
        self._u3 = 0

class CustomUnitaryInitialiser(CoinInitialiser):
    def __init__(self,U_array: list) -> None:
        for U in U_array:
            assert len(U) == 3
        self._U_array = U_array

    def initialise(self,circuit,register):
        """Initialises the quantum coin"""
        for idx in range(register.size):
            U = self._U_array[idx]
            circuit.u(*U,register[idx])
        return

reason: from numpy


text:
import math

import numpy as np
import torch
import torch.nn as nn
import itertools

class FuzzyLayer(nn.Module):
	def __init__(self, fuzzynum,channel):
		super(FuzzyLayer,self).__init__()
		self.n = fuzzynum
		self.channel = channel
		self.conv1 = nn.Conv2d(self.channel,1,3,padding=1)
		self.conv2 = nn.Conv2d(1,self.channel,3,padding=1)
		self.mu = nn.Parameter(torch.randn((self.channel,self.n)))
		self.sigma = nn.Parameter(torch.randn((self.channel,self.n)))
		self.bn1 = nn.BatchNorm2d(1, affine=True)
		self.bn2 = nn.BatchNorm2d(self.channel,affine=True)

	def forward(self, x):
		x = self.conv1(x)
		#tmp = torch.tensor(np.zeros((x.size()[0],x.size()[1],x.size()[2],x.size()[3])),dtype = torch.float).cuda()
		tmp = torch.tensor(np.zeros((x.size()[0], x.size()[1], x.size()[2], x.size()[3])), dtype=torch.float).to(device)
		for num,channel,w,h in itertools.product(range(x.size()[0]),range(x.size()[1]),range(x.size()[2]),range(x.size()[3])):
			for f in range(self.n):
				tmp[num][channel][w][h] -= ((x[num][channel][w][h]-self.mu[channel][f])/self.sigma[channel][f])**2
		fNeural = self.bn2(self.conv2(self.bn1(torch.exp(tmp))))
		return fNeural

class FuzzyNet(nn.Module):
	def __init__(self, n_class=2, testing=False):
		super(FuzzyNet, self).__init__()

		self.fuzzy_4 = FuzzyLayer(fuzzynum=1,channel=512)
		self.fuzzy_3 = FuzzyLayer(fuzzynum=1,channel=256)
		self.fuzzy_2 = FuzzyLayer(fuzzynum=1,channel=128)
		self.fuzzy_1 = FuzzyLayer(fuzzynum=1, channel=64)

		self.conv1_1 = nn.Conv2d(3, 64, 3, padding=1)
		self.relu1_1 = nn.ReLU(inplace=True)
		self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)
		self.relu1_2 = nn.ReLU(inplace=True)
		self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
		# 1/2

		self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
		self.relu2_1 = nn.ReLU(inplace=True)
		self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)
		self.relu2_2 = nn.ReLU(inplace=True)
		self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
		# 1/4

		self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
		self.relu3_1 = nn.ReLU(inplace=True)
		self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
		self.relu3_2 = nn.ReLU(inplace=True)
		self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)
		self.relu3_3 = nn.ReLU(inplace=True)
		self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
		# 1/8

		self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
		self.relu4_1 = nn.ReLU(inplace=True)
		self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
		self.relu4_2 = nn.ReLU(inplace=True)
		self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)
		self.relu4_3 = nn.ReLU(inplace=True)
		self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
		# 1/16

		self.deconv1 = nn.ConvTranspose2d(in_channels=512,out_channels=256,kernel_size=(2,2),stride=(2,2),bias=False)
		self.deconv2 = nn.ConvTranspose2d(in_channels=256,out_channels=128,kernel_size=(2,2),stride=(2,2),bias=False)
		self.deconv3 = nn.ConvTranspose2d(in_channels=128,out_channels=64,kernel_size=(2,2),stride=(2,2),bias=False)
		self.deconv4 = nn.ConvTranspose2d(in_channels=64,out_channels=6,kernel_size=(2,2),stride=(2,2),bias=False)  #

		self.fbn1 = nn.BatchNorm2d(64, affine=True)
		self.fbn2 = nn.BatchNorm2d(128, affine=True)
		self.fbn3 = nn.BatchNorm2d(256, affine=True)
		self.fbn4 = nn.BatchNorm2d(512, affine=True)

		self.bn1 = nn.BatchNorm2d(512, affine=True)
		self.bn2 = nn.BatchNorm2d(256, affine=True)
		self.bn3 = nn.BatchNorm2d(128, affine=True)
		self.bn4 = nn.BatchNorm2d(64, affine=True)

		self.testing = testing
		self._initialize_weights()

	def _initialize_weights(self):
		for m in self.modules():
			if isinstance(m, nn.Conv2d):
				n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
				m.weight.data.normal_(0, math.sqrt(2. / n))
				#m.weight.data.zero_()
				if m.bias is not None:
					m.bias.data.zero_()
			if isinstance(m, nn.ConvTranspose2d):
				n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels*m.in_channels
				m.weight.data.normal_(0,math.sqrt(2. / n))
				if m.bias is not None:
                                        m.bias.data.zero_()
			if isinstance(m,nn.Linear):
				m.weight.data.normal_(0, 1)

		for param in self.parameters():
			param.requires_grad = True

	def forward(self, x):
		h = x
		h = self.relu1_1(self.conv1_1(h))
		h = self.relu1_2(self.conv1_2(h))
		h = self.pool1(h)
		c1 = h
		f1 = self.fbn1(self.fuzzy_1(c1))+c1

		h = self.relu2_1(self.conv2_1(h))
		h = self.relu2_2(self.conv2_2(h))
		h = self.pool2(h+g)
		c2 = h
		f2 = self.fbn2(self.fuzzy_2(c2))+c2

		h = self.relu3_1(self.conv3_1(h))
		h = self.relu3_2(self.conv3_2(h))
		h = self.relu3_3(self.conv3_3(h))
		h = self.pool3(h+g)
		c3 = h
		f3 = self.fbn3(self.fuzzy_3(c3))+c3

		h = self.relu4_1(self.conv4_1(h))
		h = self.relu4_2(self.conv4_2(h))
		h = self.relu4_3(self.conv4_3(h))
		h = self.pool4(h+g)
		c4 = h
		f4 = self.fbn4(self.fuzzy_4(c4))

		h = self.bn1(h)
		h = self.bn2(self.deconv1(h))
		h = self.bn3(self.deconv2(h))
		h = self.bn4(self.deconv3(h))
		h = self.deconv4(h)
		return h

max_stars_repo_name: maxiangyueytu/FCUnet

max_issues_repo_name: maxiangyueytu/FCUnet

max_forks_repo_name: maxiangyueytu/FCUnet


text:
from __future__ import print_function, division
import os
import sys
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torchvision.utils import save_image
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)
    model = checkpoint['model']
    model.load_state_dict(checkpoint['state_dict'])
    for parameter in model.parameters():
        parameter.requires_grad = False

    model.eval()
    return model

dirName = 'results_super_resolution_zdim-6_beta-5'
parent_dir = os.path.join(os.path.dirname(__file__), '..')
model_name = 'super_resolution_model'
model = load_checkpoint(f'{dirName}/{model_name}.pth')

dims = 6
nums = 11
x_range = np.linspace(-3,3,nums)
z = np.zeros((dims, nums, dims), dtype=np.float32)

for dim in range(dims):
    z[dim, :, dim] = x_range

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
z = torch.tensor(z).view(dims*nums, z.shape[-1]).to(device)
sample = model.decode(z).cpu()
save_image(sample.view(dims*nums, 1, 64, 64),
           f'{dirName}/sample_latent_space.png',
           nrow=nums)

max_stars_repo_name: genyrosk/pytorch-VAE-models

max_issues_repo_name: genyrosk/pytorch-VAE-models

max_forks_repo_name: genyrosk/pytorch-VAE-models