text:
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2017-2019 Met Office.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
""" Provides support utilities."""

import copy

import cartopy.crs as ccrs
import iris
import numpy as np
import scipy.ndimage
from iris.coords import CellMethod
from iris.cube import Cube, CubeList
from iris.exceptions import CoordinateNotFoundError
from scipy.interpolate import griddata

from improver.threshold import BasicThreshold
from improver.utilities.cube_checker import (
    check_cube_coordinates, spatial_coords_match)

# Maximum radius of the neighbourhood width in grid cells.
MAX_DISTANCE_IN_GRID_CELLS = 500

def check_if_grid_is_equal_area(cube):
    """Identify whether the grid is an equal area grid.
    If not, raise an error.
    Args:
        cube (iris.cube.Cube):
            Cube with coordinates that will be cMAhecked.
    Raises:
        ValueError : Invalid grid: projection_x/y coords required
        ValueError : Intervals between points along the x and y axis vary.
                     Therefore the grid is not an equal area grid.
        ValueError : The size of the intervals along the x and y axis
                     should be equal.
    """
    try:
        for coord_name in ['projection_x_coordinate',
                           'projection_y_coordinate']:
            cube.coord(coord_name)
    except CoordinateNotFoundError:
        raise ValueError("Invalid grid: projection_x/y coords required")
    for coord_name in ['projection_x_coordinate',
                       'projection_y_coordinate']:
        if np.sum(np.diff(np.diff(cube.coord(coord_name).points))) > 0:
            msg = ("Intervals between points along the {} axis vary."
                   "Therefore the grid is not an equal area grid.")
            msg = msg.format(coord_name)
            raise ValueError(msg)
    x_diff = np.diff(cube.coord("projection_x_coordinate").points)[0]
    y_diff = np.diff(cube.coord("projection_y_coordinate").points)[0]
    if abs(x_diff) != abs(y_diff):
        msg = ("The size of the intervals along the x and y axis "
               "should be equal. x axis interval: {}, y axis interval: {}")
        msg = msg.format(x_diff, y_diff)
        raise ValueError(msg)

def convert_distance_into_number_of_grid_cells(
        cube, distance, max_distance_in_grid_cells=None, int_grid_cells=True):
    """
    Return the number of grid cells in the x and y direction based on the
    input distance in metres.

    Args:
        cube (iris.cube.Cube):
            Cube containing the x and y coordinates, which will be used for
            calculating the number of grid cells in the x and y direction,
            which equates to the requested distance in the x and y direction.
        distance (float):
            Distance in metres.
        max_distance_in_grid_cells (int or None):
            Maximum distance in grid cells.  Defaults to None, which bypasses
            the check.
        int_grid_cells (bool):
            If true only integer number of grid_cells are returned, rounded
            down. If false the number of grid_cells returned will be a float.

    Returns:
        (tuple) : tuple containing:
            **grid_cells_x** (int):
                Number of grid cells in the x direction based on the requested
                distance in metres.
            **grid_cells_y** (int):
                Number of grid cells in the y direction based on the requested
                distance in metres.

    Raises:
        ValueError:
            If the projection is not "equal area" (proxied by projection_x/y
            spatial coordinate names).
        ValueError:
            If the distance in grid cells is larger than the maximum dimension
            of the rectangular domain (measured across the diagonal).  Needed
            for neighbourhood processing.
        ValueError:
            If the distance in grid cells is zero.
        ValueError:
            If the distance in grid cells is negative.  (Assuming the distance
            argument is positive, this indicates one or more spatial axes are
            not correctly ordered.)
        Value Error:
            If max_distance_in_grid_cells is set and the distance in grid cells
            exceeds this value.  Needed for neighbourhood processing.
    """
    try:
        x_coord = cube.coord("projection_x_coordinate").copy()
        y_coord = cube.coord("projection_y_coordinate").copy()
    except CoordinateNotFoundError:
        raise ValueError("Invalid grid: projection_x/y coords required")
    x_coord.convert_units("metres")
    y_coord.convert_units("metres")
    max_distance_of_domain = np.sqrt(
        (x_coord.points.max() - x_coord.points.min())**2 +
        (y_coord.points.max() - y_coord.points.min())**2)
    if distance > max_distance_of_domain:
        raise ValueError(
            ("Distance of {0}m exceeds max domain"
             " distance of {1}m".format(distance, max_distance_of_domain)))
    d_north_metres = y_coord.points[1] - y_coord.points[0]
    d_east_metres = x_coord.points[1] - x_coord.points[0]
    grid_cells_y = distance / abs(d_north_metres)
    grid_cells_x = distance / abs(d_east_metres)
    if int_grid_cells:
        grid_cells_y = int(grid_cells_y)
        grid_cells_x = int(grid_cells_x)
    if grid_cells_x == 0 or grid_cells_y == 0:
        raise ValueError(
            "Distance of {0}m gives zero cell extent".format(distance))
    elif grid_cells_x < 0 or grid_cells_y < 0:
        raise ValueError(
            "Distance of {0}m gives a negative cell extent - "
            "check coordinate ordering".format(distance))
    if max_distance_in_grid_cells is not None:
        if (grid_cells_x > max_distance_in_grid_cells or
                grid_cells_y > max_distance_in_grid_cells):
            raise ValueError(
                "Distance of {0}m exceeds maximum permitted "
                "grid cell extent".format(distance))
    return grid_cells_x, grid_cells_y

def convert_number_of_grid_cells_into_distance(cube, grid_points):
    """
    Calculate radius size in metres from the given number of gridpoints
    based on the coordinates on an input cube.

    Args:
        cube (iris.cube.Cube):
            The iris cube that the number of grid points for the radius
            refers to.
        grid_points (int):
            The number of grid points you want to convert.
    Returns:
        radius_in_metres (float):
            The radius in metres.
    """
    check_if_grid_is_equal_area(cube)
    cube.coord("projection_x_coordinate").convert_units("m")
    x_diff = np.diff(cube.coord("projection_x_coordinate").points)[0]
    # Make sure the radius isn't exactly on a grid box boundary.
    radius_in_metres = x_diff*grid_points
    return radius_in_metres

class DifferenceBetweenAdjacentGridSquares(object):

    """
    Calculate the difference between adjacent grid squares within
    a cube. The difference is calculated along the x and y axis
    individually.
    """

    def __init__(self, gradient=False):
        """
        Initialise class.
        """
        self.is_gradient = gradient

    def create_difference_cube(self, cube, coord_name, diff_along_axis):
        """
        Put the difference array into a cube with the appropriate
        metadata.

        Args:
            cube (iris.cube.Cube):
                Cube from which the differences have been calculated.
            coord_name (str):
                The name of the coordinate over which the difference
                have been calculated.
            diff_along_axis (numpy.ndarray):
                Array containing the differences.

        Returns:
            diff_cube (iris.cube.Cube):
                Cube containing the differences calculated along the
                specified axis.
        """
        points = cube.coord(coord_name).points
        mean_points = (points[1:] + points[:-1]) / 2

        # Copy cube metadata and coordinates into a new cube.
        # Create a new coordinate for the coordinate along which the
        # difference has been calculated.
        metadata_dict = copy.deepcopy(cube.metadata._asdict())
        diff_cube = Cube(diff_along_axis, **metadata_dict)

        for coord in cube.dim_coords:
            dims = cube.coord_dims(coord)
            if coord.name() in [coord_name]:
                coord = coord.copy(points=mean_points)
            diff_cube.add_dim_coord(coord.copy(), dims)
        for coord in cube.aux_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)
        for coord in cube.derived_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)

        # Add metadata to indicate that a difference has been calculated.
        # TODO: update metadata for difference and add metadata for gradient
        #       when proper conventions have been agreed upon.
        if not self.is_gradient:
            cell_method = CellMethod("difference", coords=[coord_name],
                                     intervals='1 grid length')
            diff_cube.add_cell_method(cell_method)
            diff_cube.attributes["form_of_difference"] = (
                "forward_difference")
        diff_cube.rename('difference_of_' + cube.name())
        return diff_cube

    def calculate_difference(self, cube, coord_axis):
        """
        Calculate the difference along the axis specified by the
        coordinate.

        Args:
            cube (iris.cube.Cube):
                Cube from which the differences will be calculated.
            coord_axis (str):
                Short-hand reference for the x or y coordinate, as allowed by
                iris.util.guess_coord_axis.

        Returns:
            diff_cube (iris.cube.Cube):
                Cube after the differences have been calculated along the
                specified axis.
        """
        coord_name = cube.coord(axis=coord_axis).name()
        diff_axis = cube.coord_dims(coord_name)[0]
        diff_along_axis = np.diff(cube.data, axis=diff_axis)
        diff_cube = self.create_difference_cube(
            cube, coord_name, diff_along_axis)
        return diff_cube

    @staticmethod
    def gradient_from_diff(diff_cube, ref_cube, coord_axis):
        """
        Calculate the gradient along the x or y axis from differences between
        adjacent grid squares.

        Args:
            diff_cube (iris.cube.Cube):
                Cube containing differences along the x or y axis
            ref_cube (iris.cube.Cube):
                Cube with correct output dimensions
            coord_axis (str):
                Short-hand reference for the x or y coordinate, as allowed by
                iris.util.guess_coord_axis.

        Returns:
            gradient (iris.cube.Cube):
                A cube of the gradients in the coordinate direction specified.
        """
        grid_spacing = np.diff(diff_cube.coord(axis=coord_axis).points)[0]
        gradient = diff_cube.copy(data=(diff_cube.data) / grid_spacing)
        gradient = gradient.regrid(ref_cube, iris.analysis.Linear())
        gradient.rename(diff_cube.name().replace('difference_', 'gradient_'))
        return gradient

    def process(self, cube):
        """
        Calculate the difference along the x and y axes and return
        the result in separate cubes. The difference along each axis is
        calculated using numpy.diff.

        Args:
            cube (iris.cube.Cube):
                Cube from which the differences will be calculated.

        Returns:
            (tuple) : tuple containing:
                **diff_along_y_cube** (iris.cube.Cube):
                    Cube after the differences have been calculated along the
                    y axis.
                **diff_along_x_cube** (iris.cube.Cube):
                    Cube after the differences have been calculated along the
                    x axis.

        """
        diff_along_y_cube = self.calculate_difference(cube, "y")
        diff_along_x_cube = self.calculate_difference(cube, "x")

        if self.is_gradient:
            diff_along_y_cube = self.gradient_from_diff(diff_along_y_cube,
                                                        cube, "y")
            diff_along_x_cube = self.gradient_from_diff(diff_along_x_cube,
                                                        cube, "x")
        return diff_along_x_cube, diff_along_y_cube

class OccurrenceWithinVicinity(object):

    """Calculate whether a phenomenon occurs within the specified distance."""

    def __init__(self, distance):
        """
        Initialise the class.

        Args:
            distance (float):
                Distance in metres used to define the vicinity within which to
                search for an occurrence.

        """
        self.distance = distance

    def __repr__(self):
        """Represent the configured plugin instance as a string."""
        result = ('<OccurrenceWithinVicinity: distance: {}>')
        return result.format(self.distance)

    def maximum_within_vicinity(self, cube):
        """
        Find grid points where a phenomenon occurs within a defined distance.
        The occurrences within this vicinity are maximised, such that all
        grid points within the vicinity are recorded as having an occurrence.
        For non-binary fields, if the vicinity of two occurrences overlap,
        the maximum value within the vicinity is chosen.

        Args:
            cube (iris.cube.Cube):
                Thresholded cube.

        Returns:
            cube (iris.cube.Cube):
                Cube where the occurrences have been spatially spread, so that
                they're equally likely to have occurred anywhere within the
                vicinity defined using the specified distance.

        """
        # The number of grid cells returned along the x and y axis will be
        # the same.
        _, grid_cell_y = (
            convert_distance_into_number_of_grid_cells(
                cube, self.distance, MAX_DISTANCE_IN_GRID_CELLS))

        # Convert the number of grid points (e.g. grid_cell_y) represented
        # by self.distance, e.g. where grid_cell_y=1 is an increment to
        # a central point, into grid_cells which is the total number of points
        # within the defined vicinity along the y axis e.g grid_cells=3.
        grid_cells = (2 * grid_cell_y) + 1

        max_cube = cube.copy()
        unmasked_cube_data = cube.data.copy()
        if np.ma.is_masked(cube.data):
            unmasked_cube_data = cube.data.data.copy()
            unmasked_cube_data[cube.data.mask] = np.nan
        # The following command finds the maximum value for each grid point
        # from within a square of length "size"
        max_data = (
            scipy.ndimage.filters.maximum_filter(unmasked_cube_data,
                                                 size=grid_cells))
        if np.ma.is_masked(cube.data):
            # Update only the unmasked values
            max_cube.data.data[~cube.data.mask] = max_data[~cube.data.mask]
        else:
            max_cube.data = max_data
        return max_cube

    def process(self, cube):
        """
        Ensure that the cube passed to the maximum_within_vicinity method is
        2d and subsequently merged back together.

        Args:
            cube (iris.cube.Cube):
                Thresholded cube.

        Returns:
            Iris.cube.Cube
                Cube containing the occurrences within a vicinity for each
                xy 2d slice, which have been merged back together.

        """

        max_cubes = CubeList([])
        for cube_slice in cube.slices([cube.coord(axis='y'),
                                       cube.coord(axis='x')]):
            max_cubes.append(self.maximum_within_vicinity(cube_slice))
        result_cube = max_cubes.merge_cube()

        # Put dimensions back if they were there before.
        result_cube = check_cube_coordinates(cube, result_cube)

        return result_cube

def lat_lon_determine(cube):
    """
    Test whether a diagnostic cube is on a latitude/longitude grid or uses an
    alternative projection.

    Args:
        cube (iris.cube.Cube):
            A diagnostic cube to examine for coordinate system.

    Returns:
        trg_crs (cartopy.crs.CRS or None):
            Coordinate system of the diagnostic cube in a cartopy format unless
            it is already a latitude/longitude grid, in which case None is
            returned.

    """
    trg_crs = None
    if (not cube.coord(axis='x').name() == 'longitude' or
            not cube.coord(axis='y').name() == 'latitude'):
        trg_crs = cube.coord_system().as_cartopy_crs()
    return trg_crs

def lat_lon_transform(trg_crs, latitude, longitude):
    """
    Transforms latitude/longitude coordinate pairs from a latitude/longitude
    grid into an alternative projection defined by trg_crs.

    Args:
        trg_crs (cartopy.crs.CRS or None):
            Target coordinate system in cartopy format or None.

        latitude (float):
            Latitude coordinate.

        longitude (float):
            Longitude coordinate.

    Returns:
        x, y (float):
            Longitude and latitude transformed into the target coordinate
            system.

    """
    if trg_crs is None:
        return longitude, latitude
    else:
        return trg_crs.transform_point(longitude, latitude,
                                       ccrs.PlateCarree())

def transform_grid_to_lat_lon(cube):
    """
    Calculate the latitudes and longitudes of each points in the cube.

    Args:
        cube (iris.cube.Cube):
            Cube with points to transform

    Returns
        (tuple): tuple containing
            **lats** (numpy.ndarray):
                Array of cube.data.shape of Latitude values
            **lons** (numpy.ndarray):
                Array of cube.data.shape of Longitude values

    """
    trg_latlon = ccrs.PlateCarree()
    trg_crs = cube.coord_system().as_cartopy_crs()
    x_points = cube.coord(axis='x').points
    y_points = cube.coord(axis='y').points
    x_zeros = np.zeros_like(x_points)
    y_zeros = np.zeros_like(y_points)

    # Broadcast x points and y points onto grid
    all_x_points = y_zeros.reshape(len(y_zeros), 1) + x_points
    all_y_points = y_points.reshape(len(y_points), 1) + x_zeros

    # Transform points
    points = trg_latlon.transform_points(trg_crs,
                                         all_x_points,
                                         all_y_points)
    lons = points[..., 0]
    lats = points[..., 1]

    return lats, lons

def get_nearest_coords(cube, latitude, longitude, iname, jname):
    """
    Uses the iris cube method nearest_neighbour_index to find the nearest grid
    points to a given latitude-longitude position.

    Args:
        cube (iris.cube.Cube):
            Cube containing a representative grid.
        latitude (float):
            Latitude coordinates of spot data site of interest.
        longitude (float):
            Longitude coordinates of spot data site of interest.
        iname (str):
            String giving the name of the y coordinates to be searched.
        jname (str):
            String giving the names of the x coordinates to be searched.

    Returns:
        Tuple[int, int]: Grid coordinates of the nearest grid point to the
        spot data site.

    """
    i_latitude = cube.coord(iname).nearest_neighbour_index(latitude)
    j_longitude = cube.coord(jname).nearest_neighbour_index(longitude)
    return i_latitude, j_longitude

class RegridLandSea():
    """
    Replace data values at points where the nearest-regridding technique
    selects a source grid-point with an opposite land-sea-mask value to the
    target grid-point.
    The replacement data values are selected from a vicinity of points on the
    source-grid and the closest point of the correct mask is used.
    Where no match is found within the vicinity, the data value is not changed.
    """

    def __init__(self, extrapolation_mode="nanmask", vicinity_radius=25000.):
        """
        Initialise class

        Args:
            extrapolation_mode (str):
                Mode to use for extrapolating data into regions
                beyond the limits of the source_data domain.
                Available modes are documented in
                `iris.analysis <https://scitools.org.uk/iris/docs/latest/iris/
                iris/analysis.html#iris.analysis.Nearest>`_

                Defaults to "nanmask".
            vicinity_radius (float):
                Distance in metres to search for a sea or land point.
        """
        self.input_land = None
        self.nearest_cube = None
        self.output_land = None
        self.output_cube = None
        self.regridder = iris.analysis.Nearest(
            extrapolation_mode=extrapolation_mode)
        self.vicinity = OccurrenceWithinVicinity(vicinity_radius)

    def __repr__(self):
        """
        Print a human-readable representation of the instantiated object.
        """
        return "<RegridLandSea: regridder: {}; vicinity: {}>".format(
            self.regridder, self.vicinity)

    def correct_where_input_true(self, selector_val):
        """
        Replace points in the output_cube where output_land matches the
        selector_val and the input_land does not match, but has matching
        points in the vicinity, with the nearest matching point in the
        vicinity in the original nearest_cube.

        Updates self.output_cube.data

        Args:
            selector_val (int):
                Value of mask to replace if needed.
                Intended to be 1 for filling land points near the coast
                and 0 for filling sea points near the coast.
        """
        # Find all points on output grid matching selector_val
        use_points = np.where(self.input_land.data == selector_val)

        # If there are no matching points on the input grid, no alteration can
        # be made. This tests the size of the y-coordinate of use_points.
        if use_points[0].size is 0:
            return

        # Get shape of output grid
        ynum, xnum = self.output_land.shape

        # Using only these points, extrapolate to fill domain using nearest
        # neighbour. This will generate a grid where the non-selector_val
        # points are filled with the nearest value in the same mask
        # classification.
        (y_points, x_points) = np.mgrid[0:ynum, 0:xnum]
        selector_data = griddata(use_points,
                                 self.nearest_cube.data[use_points],
                                 (y_points, x_points), method="nearest")

        # Identify nearby points on regridded input_land that match the
        # selector_value
        if selector_val > 0.5:
            thresholder = BasicThreshold(0.5)
        else:
            thresholder = BasicThreshold(0.5, below_thresh_ok=True)
        in_vicinity = self.vicinity.process(
            thresholder.process(self.input_land))

        # Identify those points sourced from the opposite mask that are
        # close to a source point of the correct mask
        mismatch_points, = np.logical_and(
            np.logical_and(self.output_land.data == selector_val,
                           self.input_land.data != selector_val),
            in_vicinity.data > 0.5)

        # Replace these points with the filled-domain data
        self.output_cube.data[mismatch_points] = (
            selector_data[mismatch_points])

    def process(self, cube, input_land, output_land):
        """
        Update cube.data so that output_land and sea points match an input_land
        or sea point respectively so long as one is present within the
        specified vicinity radius.

        Args:
            cube (iris.cube.Cube):
                Cube of data to be updated (on same grid as output_land).
            input_land (iris.cube.Cube):
                Cube of land_binary_mask data on the grid from which "cube" has
                been reprojected (it is expected that the iris.analysis.Nearest
                method would have been used).
                This is used to determine where the input model data is
                representing land and sea points.
            output_land (iris.cube.Cube):
                Cube of land_binary_mask data on target grid.
        """
        # Check cube and output_land are on the same grid:
        if not spatial_coords_match(cube, output_land):
            raise ValueError('X and Y coordinates do not match for cubes {}'
                             'and {}'.format(repr(cube), repr(output_land)))
        self.output_land = output_land

        # Regrid input_land to output_land grid.
        self.input_land = input_land.regrid(self.output_land, self.regridder)

        # Slice over x-y grids for multi-realization data.
        result = iris.cube.CubeList()
        for xyslice in cube.slices(
                [cube.coord(axis='y'), cube.coord(axis='x')]):

            # Store and copy cube ready for the output data
            self.nearest_cube = xyslice
            self.output_cube = self.nearest_cube.copy()

            # Update sea points that were incorrectly sourced from land points
            self.correct_where_input_true(0)

            # Update land points that were incorrectly sourced from sea points
            self.correct_where_input_true(1)

            result.append(self.output_cube)

        result = result.merge_cube()
        return result

max_stars_repo_name: TomekTrzeciak/improver

max_issues_repo_name: TomekTrzeciak/improver

max_forks_repo_name: TomekTrzeciak/improver

reason: import numpy,import scipy,from scipy


text:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from KNN.utils import get_donut
from KNN.knn import KNN

if __name__ == '__main__':
    X, y = get_donut()
    plt.figure()
    plt.scatter(X[:,0], X[:, 1], s=100, c=y)
    plt.show()

    model = KNN(K=3)
    model.fit(X, y)
    print("Model accuracy: {}".format(model.score(X,y)))

ext: py

max_stars_repo_name: AndreiRoibu/SupervisedMachineLearning

max_issues_repo_name: AndreiRoibu/SupervisedMachineLearning

max_forks_repo_name: AndreiRoibu/SupervisedMachineLearning

reason: import numpy


text:
import cv2
import numpy as np
import os
import glob
import params

'''
    This code receives an image path and four coordinates in a cartesian plane
    and color the area not delimited by these four points with black.
'''

def paint(img_path,x1,y1,x2,y2,x3,y3,x4,y4):
    img = cv2.imread(img_path)
    linhas,colunas,layer = img.shape

    xres = params.original_xres
    yres = params.original_yres

    dx = colunas/xres
    dy = linhas/yres

    x1*=dx
    x2*=dx
    x3*=dx
    x4*=dx
    y1*=dy
    y2*=dy
    y3*=dy
    y4*=dy

    a1 = (y1-y2)/(x1-x2)
    b1 = y1 - a1*x1
    a2 = (y3-y4)/(x3-x4)
    b2 = y3 - a2*x3
    img = np.array(img)

    #print(a1, b1)
    #print(a2, b2)

    #x2 = (0 - b1) / a1
    #x3 = (0 - b2)/a2
    #x1 = (linhas-1 - b1) / a1
    #x4 = (linhas-1 - b2) / a2

    #print(x1, linhas-1)
    #print(x2, 0)
    #print(x3, 0)
    #print(x4, linhas-1)

    for y in range (linhas):

        posX1 = int((y-b1) / a1)
        posX2 = int((y - b2)/a2)

        img[y, :posX1] = [0, 0, 0]
        img[y, posX2:] = [0, 0, 0]

    cv2.imwrite(img_path[:-4] + "_z.png",img)

max_stars_repo_name: gpspelle/Crowd-Counting

max_issues_repo_name: gpspelle/Crowd-Counting

max_forks_repo_name: gpspelle/Crowd-Counting


text:
"""Grunfeld (1950) Investment Data"""
import pandas as pd

from statsmodels.datasets import utils as du

__docformat__ = 'restructuredtext'

COPYRIGHT   = """This is public domain."""
TITLE       = __doc__
SOURCE      = """This is the Grunfeld (1950) Investment Data.

The source for the data was the original 11-firm data set from Grunfeld's Ph.D.
thesis recreated by Kleiber and Zeileis (2008) "The Grunfeld Data at 50".
The data can be found here.
http://statmath.wu-wien.ac.at/~zeileis/grunfeld/

For a note on the many versions of the Grunfeld data circulating see:
http://www.stanford.edu/~clint/bench/grunfeld.htm
"""

DESCRSHORT  = """Grunfeld (1950) Investment Data for 11 U.S. Firms."""

DESCRLONG   = DESCRSHORT

NOTE        = """::

    Number of observations - 220 (20 years for 11 firms)

    Number of variables - 5

    Variables name definitions::

        invest  - Gross investment in 1947 dollars
        value   - Market value as of Dec. 31 in 1947 dollars
        capital - Stock of plant and equipment in 1947 dollars
        firm    - General Motors, US Steel, General Electric, Chrysler,
                Atlantic Refining, IBM, Union Oil, Westinghouse, Goodyear,
                Diamond Match, American Steel
        year    - 1935 - 1954

    Note that raw_data has firm expanded to dummy variables, since it is a
    string categorical variable.
"""

def load(as_pandas=None):
    """
    Loads the Grunfeld data and returns a Dataset class.

    Parameters
    ----------
    as_pandas : bool
        Flag indicating whether to return pandas DataFrames and Series
        or numpy recarrays and arrays.  If True, returns pandas.

    Returns
    -------
    Dataset
        See DATASET_PROPOSAL.txt for more information.

    Notes
    -----
    raw_data has the firm variable expanded to dummy variables for each
    firm (ie., there is no reference dummy)
    """
    return du.as_numpy_dataset(load_pandas(), as_pandas=as_pandas)

def load_pandas():
    """
    Loads the Grunfeld data and returns a Dataset class.

    Returns
    -------
    Dataset
        See DATASET_PROPOSAL.txt for more information.

    Notes
    -----
    raw_data has the firm variable expanded to dummy variables for each
    firm (ie., there is no reference dummy)
    """
    data = _get_data()
    data.year = data.year.astype(float)
    raw_data = pd.get_dummies(data)
    ds = du.process_pandas(data, endog_idx=0)
    ds.raw_data = raw_data
    return ds

def _get_data():
    data = du.load_csv(__file__, 'grunfeld.csv')
    return data

max_stars_repo_name: EkremBayar/bayar

max_issues_repo_name: EkremBayar/bayar

max_forks_repo_name: EkremBayar/bayar

reason: from statsmodels


text:
# Copyright 2019-2022 ETH Zurich and the DaCe authors. All rights reserved.
#
# This sample shows the AXPY BLAS routine. It is implemented through Xilinx IPs in order to utilize floating point
# operations. It is intended for running hardware_emulation or hardware xilinx targets.

import dace
import numpy as np

# add symbol
N = dace.symbol('N')

def make_sdfg(veclen=2):
    # add sdfg
    sdfg = dace.SDFG('axpy')

    # add state
    state = sdfg.add_state('device_state')

    # add parameter
    sdfg.add_constant('VECLEN', veclen)

    # add arrays
    sdfg.add_scalar('a', dtype=dace.float32, storage=dace.StorageType.FPGA_Global)
    sdfg.add_array('x', [N // veclen], dtype=dace.vector(dace.float32, veclen), storage=dace.StorageType.CPU_Heap)
    sdfg.add_array('y', [N // veclen], dtype=dace.vector(dace.float32, veclen), storage=dace.StorageType.CPU_Heap)
    sdfg.add_array('result', [N // veclen], dtype=dace.vector(dace.float32, veclen), storage=dace.StorageType.CPU_Heap)
    sdfg.add_array('fpga_x', [N // veclen],
                   dtype=dace.vector(dace.float32, veclen),
                   transient=True,
                   storage=dace.StorageType.FPGA_Global)
    sdfg.add_array('fpga_y', [N // veclen],
                   dtype=dace.vector(dace.float32, veclen),
                   transient=True,
                   storage=dace.StorageType.FPGA_Global)
    sdfg.add_array('fpga_result', [N // veclen],
                   dtype=dace.vector(dace.float32, veclen),
                   transient=True,
                   storage=dace.StorageType.FPGA_Global)

    # add streams
    sdfg.add_stream('x_stream',
                    buffer_size=32,
                    dtype=dace.vector(dace.float32, veclen),
                    transient=True,
                    storage=dace.StorageType.FPGA_Local)
    sdfg.add_stream('y_stream',
                    buffer_size=32,
                    dtype=dace.vector(dace.float32, veclen),
                    transient=True,
                    storage=dace.StorageType.FPGA_Local)
    sdfg.add_stream('result_stream',
                    buffer_size=32,
                    dtype=dace.vector(dace.float32, veclen),
                    transient=True,
                    storage=dace.StorageType.FPGA_Local)

    # add custom rtl tasklet
    rtl_tasklet = state.add_tasklet(name='rtl_tasklet',
                                    inputs={'a_in', 'x_in', 'y_in'},
                                    outputs={'result_out'},
                                    code='''
        /*
            Convention:
            |--------------------------------------------------------|
            |                                                        |
         -->| ap_aclk (slow clock input)                             |
         -->| ap_areset (slow reset input, rst on high)              |
         -->| ap_aclk (fast clock input)                             |
         -->| ap_areset_2 (fast reset input, rst on high)            |
         -->| ap_start (start pulse from host)                       |
         <--| ap_done (tells the host that the kernel is done)       |
            |                                                        |
            | For each input:             For each output:           |
            |                                                        |
         -->|     s_axis_{input}_tvalid   reg m_axis_{output}_tvalid |-->
         -->|     s_axis_{input}_tdata    reg m_axis_{output}_tdata  |-->
         <--| reg s_axis_{input}_tready       m_axis_{output}_tready |<--
         -->|     s_axis_{input}_tkeep    reg m_axis_{output}_tkeep  |-->
         -->|     s_axis_{input}_tlast    reg m_axis_{output}_tlast  |-->
            |                                                        |
            |--------------------------------------------------------|
        */
        assign ap_done = 1; // free-running kernel

        wire        axis_ax_tvalid;
        wire [31:0] axis_ax_tdata;
        wire        axis_ax_tready;

        reg [VECLEN-1:0] s_axis_x_in_tready_tmp;
        reg [VECLEN-1:0] s_axis_y_in_tready_tmp;
        reg [VECLEN-1:0] m_axis_result_out_tvalid_tmp;

        generate for (genvar i = 0; i < VECLEN; i++) begin

            wire        axis_ax_tvalid;
            wire [31:0] axis_ax_tdata;
            wire        axis_ax_tready;

            floating_point_mult multiplier (
                .aclk(ap_aclk),

                .s_axis_a_tvalid(scalars_valid),
                .s_axis_a_tdata(a_in),
                //.s_axis_a_tready(),

                .s_axis_b_tvalid(s_axis_x_in_tvalid),
                .s_axis_b_tdata( s_axis_x_in_tdata[i]),
                .s_axis_b_tready(s_axis_x_in_tready_tmp[i]),

                .m_axis_result_tvalid(axis_ax_tvalid),
                .m_axis_result_tdata( axis_ax_tdata),
                .m_axis_result_tready(axis_ax_tready)
            );

            floating_point_add adder (
                .aclk(ap_aclk),

                .s_axis_a_tvalid(axis_ax_tvalid),
                .s_axis_a_tdata( axis_ax_tdata),
                .s_axis_a_tready(axis_ax_tready),

                .s_axis_b_tvalid(s_axis_y_in_tvalid),
                .s_axis_b_tdata( s_axis_y_in_tdata[i]),
                .s_axis_b_tready(s_axis_y_in_tready_tmp[i]),

                .m_axis_result_tvalid(m_axis_result_out_tvalid_tmp[i]),
                .m_axis_result_tdata( m_axis_result_out_tdata[i]),
                .m_axis_result_tready(m_axis_result_out_tready)
            );

        end endgenerate

        assign s_axis_x_in_tready = &s_axis_x_in_tready_tmp;
        assign s_axis_y_in_tready = &s_axis_y_in_tready_tmp;
        assign m_axis_result_out_tvalid = &m_axis_result_out_tvalid_tmp;
        ''',
                                    language=dace.Language.SystemVerilog)

    rtl_tasklet.add_ip_core(
        'floating_point_mult', 'floating_point', 'xilinx.com', '7.1', {
            "CONFIG.Operation_Type": "Multiply",
            "CONFIG.C_Mult_Usage": "Max_Usage",
            "CONFIG.Axi_Optimize_Goal": "Performance",
            "CONFIG.A_Precision_Type": "Single",
            "CONFIG.C_A_Exponent_Width": "8",
            "CONFIG.C_A_Fraction_Width": "24",
            "CONFIG.Result_Precision_Type": "Single",
            "CONFIG.C_Result_Exponent_Width": "8",
            "CONFIG.C_Result_Fraction_Width": "24",
            "CONFIG.C_Latency": "9",
            "CONFIG.C_Rate": "1"
        })

    rtl_tasklet.add_ip_core('floating_point_add', 'floating_point', 'xilinx.com', '7.1', {
        "CONFIG.Add_Sub_Value": "Add",
        "CONFIG.Axi_Optimize_Goal": "Performance",
        "CONFIG.C_Latency": "14"
    })

    # add read and write tasklets
    read_x = state.add_tasklet('read_x', {'inp'}, {'out'}, 'out = inp')
    read_y = state.add_tasklet('read_y', {'inp'}, {'out'}, 'out = inp')
    write_result = state.add_tasklet('write_result', {'inp'}, {'out'}, 'out = inp')

    # add read and write maps
    read_x_entry, read_x_exit = state.add_map('read_x_map',
                                              dict(i='0:N//VECLEN'),
                                              schedule=dace.ScheduleType.FPGA_Device)
    read_y_entry, read_y_exit = state.add_map('read_y_map',
                                              dict(i='0:N//VECLEN'),
                                              schedule=dace.ScheduleType.FPGA_Device)
    write_result_entry, write_result_exit = state.add_map('write_result_map',
                                                          dict(i='0:N//VECLEN'),
                                                          schedule=dace.ScheduleType.FPGA_Device)

    # add read_a memlets and access nodes
    read_x_inp = state.add_read('fpga_x')
    read_x_out = state.add_write('x_stream')
    state.add_memlet_path(read_x_inp, read_x_entry, read_x, dst_conn='inp', memlet=dace.Memlet('fpga_x[i]'))
    state.add_memlet_path(read_x, read_x_exit, read_x_out, src_conn='out', memlet=dace.Memlet('x_stream[0]'))

    read_y_inp = state.add_read('fpga_y')
    read_y_out = state.add_write('y_stream')
    state.add_memlet_path(read_y_inp, read_y_entry, read_y, dst_conn='inp', memlet=dace.Memlet('fpga_y[i]'))
    state.add_memlet_path(read_y, read_y_exit, read_y_out, src_conn='out', memlet=dace.Memlet('y_stream[0]'))

    # add tasklet memlets
    a = state.add_read('a')
    x = state.add_read('x_stream')
    y = state.add_read('y_stream')
    result = state.add_write('result_stream')
    state.add_memlet_path(a, rtl_tasklet, dst_conn='a_in', memlet=dace.Memlet('a[0]'))
    state.add_memlet_path(x, rtl_tasklet, dst_conn='x_in', memlet=dace.Memlet('x_stream[0]'))
    state.add_memlet_path(y, rtl_tasklet, dst_conn='y_in', memlet=dace.Memlet('y_stream[0]'))
    state.add_memlet_path(rtl_tasklet, result, src_conn='result_out', memlet=dace.Memlet('result_stream[0]'))

    # add write_c memlets and access nodes
    write_result_inp = state.add_read('result_stream')
    write_result_out = state.add_write('fpga_result')
    state.add_memlet_path(write_result_inp,
                          write_result_entry,
                          write_result,
                          dst_conn='inp',
                          memlet=dace.Memlet('result_stream[0]'))
    state.add_memlet_path(write_result,
                          write_result_exit,
                          write_result_out,
                          src_conn='out',
                          memlet=dace.Memlet('fpga_result[i]'))

    # add copy to device state
    copy_to_device = sdfg.add_state('copy_to_device')
    cpu_x = copy_to_device.add_read('x')
    cpu_y = copy_to_device.add_read('y')
    dev_x = copy_to_device.add_write('fpga_x')
    dev_y = copy_to_device.add_write('fpga_y')
    copy_to_device.add_memlet_path(cpu_x, dev_x, memlet=dace.Memlet('x[0:N//VECLEN]'))
    copy_to_device.add_memlet_path(cpu_y, dev_y, memlet=dace.Memlet('y[0:N//VECLEN]'))
    sdfg.add_edge(copy_to_device, state, dace.InterstateEdge())

    # add copy to host state
    copy_to_host = sdfg.add_state('copy_to_host')
    dev_result = copy_to_host.add_read('fpga_result')
    cpu_result = copy_to_host.add_write('result')
    copy_to_host.add_memlet_path(dev_result, cpu_result, memlet=dace.Memlet('result[0:N//VECLEN]'))
    sdfg.add_edge(state, copy_to_host, dace.InterstateEdge())

    # validate sdfg
    sdfg.validate()

    return sdfg

######################################################################

if __name__ == '__main__':
    with dace.config.set_temporary('compiler', 'xilinx', 'mode', value='hardware_emulation'):
        # init data structures
        N.set(4096)
        a = np.random.rand(1)[0].astype(np.float32)
        x = np.random.rand(N.get()).astype(np.float32)
        y = np.random.rand(N.get()).astype(np.float32)
        result = np.zeros((N.get(), )).astype(np.float32)

        # show initial values
        print("a={}, x={}, y={}".format(a, x, y))

        # Build the SDFG
        sdfg = make_sdfg()

        # call program
        sdfg(a=a, x=x, y=y, result=result, N=N)

        # show result
        print("result={}".format(result))

        # check result
        expected = a * x + y
        diff = np.linalg.norm(expected - result) / N.get()
        print("Difference:", diff)
    exit(0 if diff <= 1e-5 else 1)

max_stars_repo_name: meshtag/dace

max_issues_repo_name: meshtag/dace

max_forks_repo_name: meshtag/dace


text:
from numpy import *

def unroll_params(Theta):

    nn_params = reshape(Theta[0],(1,-1)).transpose()
    for i in range(1,len(Theta)):
        nn_params = concatenate((nn_params,reshape(Theta[i], (1, -1)).transpose()))

    nn_params = ndarray.flatten(nn_params)

    return nn_params

max_stars_repo_path: Assignment 1 - Neural networks/unroll_params.py

max_stars_repo_name: marioyc/Hand-written-Digit-Recognition

max_issues_repo_name: marioyc/Hand-written-Digit-Recognition

max_forks_repo_name: marioyc/Hand-written-Digit-Recognition

reason: from numpy


text:
import json
from os.path import dirname
import re

import numpy as np

PAD_token = 0
SOS_token = 1
EOS_token = 2

class Voc:
    def __init__(self):
        pass

    def load_data(self, path):
        with open(path) as f:
            self.__dict__ = json.load(f)

voc = Voc()
voc_file = f'{dirname(dirname(dirname(__file__)))}/models/chatbot/voc.json'
voc.load_data(voc_file)

def normalize_string(s):
    s = s.lower()
    s = re.sub(r"([.!?])", r" \1", s)
    s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
    return s

def get_batched_indices(sentence):
    return [[el for el in [voc.word2index.get(word) for word in sentence.split(' ')] if el is not None] + [EOS_token]]
    # return [[voc.word2index[word] for word in sentence.split(' ')] + [EOS_token]]

def list2numpy(indices):
    return np.array(indices, dtype=np.long).transpose()

def get_length(array):
    return np.array([len(array)], dtype=np.long)

def indices2str(indices):
    return ' '.join([voc.index2word[str(ind)] for ind in indices])

max_stars_repo_name: hhsecond/redisai-examples

max_issues_repo_name: hhsecond/redisai-examples

max_forks_repo_name: hhsecond/redisai-examples


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar  1 18:44:04 2018

@author: JavaWizards
"""

import numpy as np

file = "/Users/nuno_chicoria/Downloads/b_should_be_easy.in"

handle = open(file)

R, C, F, N, B, T = handle.readline().split()

rides = []

index = []
for i in range(int(N)):
    index.append(i)

for line in handle:
    rides.append(line.split())

rides_np = np.asarray(rides)
rides_np = np.column_stack([rides_np, index])
rides_np = rides_np.astype(np.int)
rides_np = rides_np[rides_np[:,5].argsort()]

vehicles = {}
for i in range(int(F)):
    vehicles [i] = ["A", [0, 0], [0, 0], [0, 0], []]

for i in range(int(T)):
    rides_np = rides_np[rides_np[:,5] > i]
    for item in range(len(vehicles)):
        if vehicles[item][0] == "A":
            if rides_np.size != 0:
                if abs(vehicles[item][1][0] - rides_np[0, 0]) + abs(vehicles[item][1][1] - rides_np[0, 1]) + i >= rides_np[0, 4]:
                    if  abs(vehicles[item][1][0] - rides_np[0, 0]) + abs(vehicles[item][1][1] - rides_np[0, 1]) + i + abs(rides_np[0,0] - rides_np[0,2]) + abs(rides_np[0,1] - rides_np[0,3]) <= rides_np[0, 5]:
                        vehicles[item][0] = "C"
                        vehicles[item][2] = [rides_np[0, 0], rides_np[0, 1]]
                        vehicles[item][3] = [rides_np[0, 2], rides_np[0, 3]]
                        vehicles[item][4].append(rides_np[0, 6])
                        rides_np = np.delete(rides_np, (0), axis=0)
                    else:
                        rides_np = np.delete(rides_np, (0), axis=0)
    for item in range(len(vehicles)):
        if vehicles[item][0] == "C":
            if vehicles[item][1][0] < vehicles[item][2][0]:
                vehicles[item][1][0] = vehicles[item][1][0] + 1
            elif vehicles[item][1][0] > vehicles[item][2][0]:
                vehicles[item][1][0] = vehicles[item][1][0] - 1
            elif vehicles[item][1][0] == vehicles[item][2][0]:
                if vehicles[item][1][1] < vehicles[item][2][1]:
                    vehicles[item][1][1] =  vehicles[item][1][1] + 1
                elif vehicles[item][1][1] > vehicles[item][2][1]:
                    vehicles[item][1][1] = vehicles[item][1][1] - 1
                else:
                   vehicles[item][0] = "D"
    for item in range(len(vehicles)):
        if vehicles[item][0] == "D":
            if vehicles[item][1][0] < vehicles[item][3][0]:
                vehicles[item][1][0] += 1
            elif vehicles[item][1][0] > vehicles[item][3][0]:
                vehicles[item][1][0] -= 1
            elif vehicles[item][1][0] == vehicles[item][3][0]:
                if vehicles[item][1][1] < vehicles[item][3][1]:
                    vehicles[item][1][1] += 1
                elif vehicles[item][1][1] > vehicles[item][3][1]:
                    vehicles[item][1][1] -= 1
                else:
                    vehicles[item][0] = "A"
                    vehicles[item][2] = None
                    vehicles[item][3] = None

results = open("ghc2018.txt", "w+")
for item in range(len(vehicles)):
    if len(vehicles[item][4]) !=0:
        results.write(str(len(vehicles[item][4])))
        for ride in vehicles[item][4]:
            results.write(" ")
            results.write(str(ride))
        results.write("\n")
results.close()


text:
import numpy as np
from typing import Any, Dict, List, Tuple, NoReturn

import argparse
import os

import pandas as pd
import json

import matplotlib.pyplot as plt

def parse_arguments() -> Any:
	"""Parse command line arguments."""

	parser = argparse.ArgumentParser()

	parser.add_argument(
		"--data_file",
		default="",
		type=str,
		help="Path where the results of the algorithm (csv files) is saved",
	)

	parser.add_argument(
		"--result_dir",
		default="",
		type=str,
		help="Directory where the result of the analysis (csv files) should be saved",
	)
	parser.add_argument(
		"--algorithm",
		default="",
		type=str,
		help="name of the algorithm",
		choices=['kmeans', 'gmc', 'fuzzy_cm', 'fls_t1', 'fls_t2']
	)

	return parser.parse_args()

def plot_clusters(root_dir:str, X:np.ndarray, Y:np.ndarray) -> NoReturn:

	'''
		plot the clusters and classification
	'''

	colors = {0: 'tab:blue', 1:'tab:orange', 2:'tab:green'}
	titles = ['velocity', 'acceleration', 'deceleration', 'lateral jerk']

	fig, axs = plt.subplots(4,4)

	for i in range(0, 4):
		for j in range(0, 4):
			for c in [2,1,0]:
				i_c = np.where(Y==c)[0]

				if len(i_c) == 0:
					continue

				axs[i, j].scatter(X[i_c, i], X[i_c, j], s=1., color=colors[c])

			axs[i, j].set(xlabel=titles[j], ylabel=titles[i])

	for ax in axs.flat:
		ax.label_outer()

	fig.legend(['c_0', 'c_1', 'c_2'])
	plt.savefig(root_dir)

if __name__== '__main__':

	'''
		- compute evaluation metrics for each algorithm
		- based on clustering analysis and descriptive statistics

		- metrics:

			- Silhouette Coefficient
				+ https://en.wikipedia.org/wiki/Silhouette_(clustering)
				+ https://www.sciencedirect.com/science/article/pii/0377042787901257
				+ https://scikit-learn.org/stable/modules/generated/sklearn.metrics
					.silhouette_score.html#sklearn-metrics-silhouette-score
			- Calinski Harabasz Score
				+ https://medium.com/@haataa/how-to-measure-clustering-performances
					-when-there-are-no-ground-truth-db027e9a871c
				+ https://www.tandfonline.com/doi/abs/10.1080/03610927408827101
				+ https://scikit-learn.org/stable/modules/generated/sklearn.metrics
					.calinski_harabasz_score.html#sklearn.metrics.calinski_harabasz_score
			- Davis-Bouldin Index
				+ https://en.wikipedia.org/wiki/Davies%E2%80%93Bouldin_index
				+ https://ieeexplore.ieee.org/document/4766909
				+ https://scikit-learn.org/stable/modules/generated/sklearn.metrics
					.davies_bouldin_score.html#sklearn.metrics.davies_bouldin_score
			- Mean, STD, min, max, Q-25, Q-50, Q-75

	'''

	args = parse_arguments()

	assert os.path.exists(args.data_file),\
		f'[Clustering Analysis][main][ERROR] data_file not found!({args.data_file})'

	print (f'[Clustering Analysis][main] \033[92mprocessing file\033[0m: {args.data_file}')

	_split = args.data_file.split('_')
	mode = _split[-3]
	obs_len = int(_split[-2][0])
	filter_name = _split[-1].split('.')[0]

	print (f'[Clustering Analysis][main] loading data...')

	# [mean_v, mean_acc, mean_deac, std_jy, y]
	data = pd.read_csv(args.data_file)

	X = data[['mean_velocity', 'mean_acceleration', 'mean_deceleration', 'std_lateral_jerk']].values
	Y = np.asarray(data[['driving_style']].values, dtype=int)
	Y = np.squeeze(Y)

	result_file = f'{args.algorithm}_{mode}_{obs_len}s_{filter_name}.png'
	result_file = os.path.join(args.result_dir, result_file)
	plot_clusters(root_dir=result_file, X=X, Y=Y)


text:
'''Implements Field II using the MATLAB Engine API for Python.'''
import numpy as np
import time
import io
import os
import warnings

try:
    import matlab
    import matlab.engine
except ImportError:
    warnings.warn('Failed to import MATLAB engine module')

class PyField(object):
    '''
    A Field II instance. Class methods implement an identical interface to
    the MATLAB version.
    '''
    def __init__(self, path=None, quiet=False):
        '''
        Starts a new MATLAB session.

        Parameters
        ----------
        path : str, optional
            [description]. Default is None.
        quiet : bool, optional
            If True, redirects print output of Field II to a string buffer.
            Default is False.

        Raises
        ------
        matlab.engine.EngineError
            If MATLAB Engine fails to start.
        '''
        # set default path to location of m-files (where this module is)
        if path is None:
            path = os.path.dirname(os.path.abspath(__file__))

        if quiet:
            self.stdout = io.StringIO()
        else:
            self.stdout = None

        # try, for at most 30s, to start MATLAB engine
        success = False
        for i in range(6):
            try:
                self._mateng = matlab.engine.start_matlab()
                success = True
                break
            except (matlab.engine.EngineError, TypeError):
                time.sleep(5)

        if not success:
            raise matlab.engine.EngineError

        # set MATLAB engine path to location of m-files
        self._mateng.cd(str(os.path.normpath(path)), nargout=0)

    def __del__(self):
        '''Shutdown MATLAB Engine'''
        # self.field_end() # end FIELD II
        self._mateng.quit()  # shutdown MATLAB engine

    def _numpy_to_mat(self, array, orient='row'):
        '''Convert ndarray to MATLAB array'''
        array = np.atleast_2d(array)
        assert array.ndim == 2

        if orient.lower() == 'row':
            if array.shape[1] == 1:
                array = array.T
        elif orient.lower() in ('col', 'column'):
            if array.shape[0] == 1:
                array = array.T

        return matlab.double(initializer=array.tolist(), size=array.shape)

    def _mat_to_numpy(self, array):
        '''Convert MATLAB array to ndarray'''
        return np.array(array).squeeze()

    '''Field functions '''

    def field_debug(self, state):
        self._mateng.field_debug(state, nargout=0)

    def field_end(self):
        self._mateng.field_end(nargout=0)

    def field_guide(self):
        self._mateng.field_guide(nargout=0)

    def field_info(self):
        self._mateng.field_info(nargout=0)

    def field_init(self, suppress=-1):
        self._mateng.field_init(suppress, nargout=0)

    def field_logo(self):
        self._mateng.field_logo(nargout=0)

    def set_field(self, option_name, value):
        self._mateng.set_field(option_name, value, nargout=0)

    def set_sampling(self, fs):
        self._mateng.set_sampling(fs, nargout=0)

    '''Calc functions'''

    def calc_scat(self, Th1, Th2, points, amplitudes):
        points_mat = self._numpy_to_mat(points, orient='row')
        amplitudes_mat = self._numpy_to_mat(amplitudes, orient='col')
        ret = self._mateng.calc_scat(Th1,
                                     Th2,
                                     points_mat,
                                     amplitudes_mat,
                                     nargout=2)

        scat = self._mat_to_numpy(ret[0])
        t0 = ret[1]

        return scat, t0

    def calc_scat_all(self, Th1, Th2, points, amplitudes, dec_factor):
        points_mat = self._numpy_to_mat(points, orient='row')
        amplitudes_mat = self._numpy_to_mat(amplitudes, orient='col')
        ret = self._mateng.calc_scat_all(Th1,
                                         Th2,
                                         points_mat,
                                         amplitudes_mat,
                                         dec_factor,
                                         nargout=2)

        scat = self._mat_to_numpy(ret[0])
        t0 = ret[1]
        return scat, t0

    def calc_scat_multi(self, Th1, Th2, points, amplitudes):
        points_mat = self._numpy_to_mat(points, orient='row')
        amplitudes_mat = self._numpy_to_mat(amplitudes, orient='col')
        ret = self._mateng.calc_scat_multi(Th1,
                                           Th2,
                                           points_mat,
                                           amplitudes_mat,
                                           nargout=2)

        scat = self._mat_to_numpy(ret[0])
        t0 = ret[1]
        return scat, t0

    def calc_h(self, Th, points):
        points_mat = self._numpy_to_mat(points, orient='row')
        ret = self._mateng.calc_h(Th, points_mat, nargout=2)
        h = self._mat_to_numpy(ret[0])
        t0 = ret[1]
        return h, t0

    def calc_hp(self, Th, points):
        points_mat = self._numpy_to_mat(points, orient='row')
        ret = self._mateng.calc_hp(Th, points_mat, nargout=2)
        hp = self._mat_to_numpy(ret[0])
        t0 = ret[1]
        return hp, t0

    def calc_hhp(self, Th1, Th2, points):
        points_mat = self._numpy_to_mat(points, orient='row')
        ret = self._mateng.calc_hhp(Th1, Th2, points_mat, nargout=2)
        hhp = self._mat_to_numpy(ret[0])
        t0 = ret[1]
        return hhp, t0

    '''Xdc functions'''

    def xdc_2d_array(self, no_elem_x, no_elem_y, width, heights, kerf_x,
                     kerf_y, enabled, no_sub_x, no_sub_y, focus):
        enabled_mat = self._numpy_to_mat(enabled, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_2d_array(no_elem_x,
                                         no_elem_y,
                                         width,
                                         heights,
                                         kerf_x,
                                         kerf_y,
                                         enabled_mat,
                                         no_sub_x,
                                         no_sub_y,
                                         focus_mat,
                                         nargout=1)

    def xdc_apodization(self, Th, times, values):
        times_mat = self._numpy_to_mat(times, orient='col')
        values_mat = self._numpy_to_mat(values, orient='row')
        self._mateng.xdc_apodization(Th, times_mat, values_mat, nargout=0)

    def xdc_baffle(self, Th, soft_baffle):
        self._mateng.xdc_baffle(Th, soft_baffle, nargout=0)

    def xdc_center_focus(self, Th, point):
        point_mat = self._numpy_to_mat(point, orient='row')
        self._mateng.xdc_center_focus(Th, point_mat, nargout=0)

    def xdc_concave(self, radius, focal_radius, ele_size):
        return self._mateng.xdc_concave(radius,
                                        focal_radius,
                                        ele_size,
                                        nargout=1)

    def xdc_convert(self, Th):
        self._mateng.xdc_convert(Th, nargout=0)

    def xdc_convex_array(self, no_elements, width, height, kerf, Rconvex,
                         no_sub_x, no_sub_y, focus):
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_convex_array(no_elements,
                                             width,
                                             height,
                                             kerf,
                                             Rconvex,
                                             no_sub_x,
                                             no_sub_y,
                                             focus_mat,
                                             nargout=1)

    def xdc_convex_focused_array(self, no_elements, width, height, kerf,
                                 Rconvex, Rfocus, no_sub_x, no_sub_y, focus):
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_convex_focused_array(no_elements,
                                                     width,
                                                     height,
                                                     kerf,
                                                     Rconvex,
                                                     Rfocus,
                                                     no_sub_x,
                                                     no_sub_y,
                                                     focus_mat,
                                                     nargout=1)

    def xdc_convex_focused_multirow(self, no_elem_x, width, no_elem_y, heights,
                                    kerf_x, kerf_y, Rconvex, Rfocus, no_sub_x,
                                    no_sub_y, focus):
        heights_mat = self._numpy_to_mat(heights, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_convex_focused_multirow(no_elem_x,
                                                        width,
                                                        no_elem_y,
                                                        heights_mat,
                                                        kerf_x,
                                                        kerf_y,
                                                        Rconvex,
                                                        Rfocus,
                                                        no_sub_x,
                                                        no_sub_y,
                                                        focus_mat,
                                                        nargout=1)

    def xdc_dynamic_focus(self, Th, time, dir_zx, dir_zy):
        self._mateng.xdc_dynamic_focus(Th, time, dir_zx, dir_zy, nargout=0)

    def xdc_excitation(self, Th, pulse):
        pulse_mat = self._numpy_to_mat(pulse, orient='row')
        self._mateng.xdc_excitation(Th, pulse_mat, nargout=0)

    def xdc_focus(self, Th, times, points):
        times_mat = self._numpy_to_mat(times, orient='col')
        points_mat = self._numpy_to_mat(points, orient='row')
        self._mateng.xdc_focus(Th, times_mat, points_mat, nargout=0)

    def xdc_focus_times(self, Th, times, delays):
        times_mat = self._numpy_to_mat(times, orient='col')
        delays_mat = self._numpy_to_mat(delays, orient='row')
        self._mateng.xdc_focus_times(Th, times_mat, delays_mat, nargout=0)

    def xdc_focused_array(self, no_elements, width, height, kerf, rfocus,
                          no_sub_x, no_sub_y, focus):
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_focused_array(no_elements,
                                              width,
                                              height,
                                              kerf,
                                              rfocus,
                                              no_sub_x,
                                              no_sub_y,
                                              focus_mat,
                                              nargout=1)

    def xdc_focused_multirow(self, no_elem_x, width, no_elem_y, heights,
                             kerf_x, kerf_y, Rfocus, no_sub_x, no_sub_y,
                             focus):
        heights_mat = self._numpy_to_mat(heights, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_focused_multirow(no_elem_x,
                                                 width,
                                                 no_elem_y,
                                                 heights_mat,
                                                 kerf_x,
                                                 kerf_y,
                                                 Rfocus,
                                                 no_sub_x,
                                                 no_sub_y,
                                                 focus_mat,
                                                 nargout=1)

    def xdc_free(self, Th):
        self._mateng.xdc_free(Th, nargout=0)

    def xdc_get(self, Th, info_type='rect'):
        return self._mat_to_numpy(
            self._mateng.xdc_get(Th, info_type, nargout=1))

    def xdc_impulse(self, Th, pulse):
        pulse_mat = self._numpy_to_mat(pulse, orient='row')
        self._mateng.xdc_impulse(Th, pulse_mat, nargout=0)

    def xdc_line_convert(self, Th):
        self._mateng.xdc_line_convert(Th, nargout=0)

    def xdc_linear_array(self, no_elements, width, height, kerf, no_sub_x,
                         no_sub_y, focus):

        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_linear_array(no_elements,
                                             width,
                                             height,
                                             kerf,
                                             no_sub_x,
                                             no_sub_y,
                                             focus_mat,
                                             nargout=1)

    def xdc_linear_multirow(self, no_elem_x, width, no_elem_y, heights, kerf_x,
                            kerf_y, no_sub_x, no_sub_y, focus):
        heights_mat = self._numpy_to_mat(heights, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_linear_multirow(no_elem_x,
                                                width,
                                                no_elem_y,
                                                heights_mat,
                                                kerf_x,
                                                kerf_y,
                                                no_sub_x,
                                                no_sub_y,
                                                focus_mat,
                                                nargout=1)

    def xdc_lines(self, lines, center, focus):
        lines_mat = self._numpy_to_mat(lines, orient='row')
        center_mat = self._numpy_to_mat(center, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_lines(lines_mat,
                                      center_mat,
                                      focus_mat,
                                      nargout=1)

    def xdc_piston(self, radius, ele_size):
        return self._mateng.xdc_piston(radius, ele_size, nargout=1)

    def xdc_quantization(self, Th, value):
        self._mateng.xdc_quantization(Th, value, nargout=0)

    def xdc_rectangles(self, rect, center, focus):
        rect_mat = self._numpy_to_mat(rect, orient='row')
        center_mat = self._numpy_to_mat(center, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_rectangles(rect_mat,
                                           center_mat,
                                           focus_mat,
                                           nargout=1)

    def xdc_show(self, Th, info_type='all'):
        self._mateng.xdc_show(Th, info_type, nargout=0)

    def xdc_times_focus(self, Th, times, delays):
        times_mat = self._numpy_to_mat(times, orient='col')
        delays_mat = self._numpy_to_mat(delays, orient='row')
        self._mateng.xdc_times_focus(Th, times_mat, delays_mat, nargout=0)

    def xdc_triangles(self, data, center, focus):
        data_mat = self._numpy_to_mat(data, orient='row')
        center_mat = self._numpy_to_mat(center, orient='row')
        focus_mat = self._numpy_to_mat(focus, orient='row')
        return self._mateng.xdc_triangles(data_mat,
                                          center_mat,
                                          focus_mat,
                                          nargout=1)

    '''Ele functions'''

    def ele_apodization(self, Th, element_no, apo):
        element_no_mat = self._numpy_to_mat(element_no, orient='col')
        apo_mat = self._numpy_to_mat(apo, orient='row')
        self._mateng.ele_apodization(Th, element_no_mat, apo_mat, nargout=0)

    def ele_delay(self, Th, element_no, delays):
        element_no_mat = self._numpy_to_mat(element_no, orient='col')
        delays_mat = self._numpy_to_mat(delays, orient='row')
        self._mateng.ele_delay(Th, element_no_mat, delays_mat, nargout=0)

    def ele_waveform(self, Th, element_no, samples):
        element_no_mat = self._numpy_to_mat(element_no, orient='col')
        samples_mat = self._numpy_to_mat(samples, orient='row')
        self._mateng.ele_waveform(Th, element_no_mat, samples_mat, nargout=0)

if __name__ == '__main__':
    pass

max_stars_repo_name: bdshieh/pyfield

max_issues_repo_name: bdshieh/pyfield

max_forks_repo_name: bdshieh/pyfield


text:
import csv
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np

dir_prefix = 'test_diffparams_quant_2021_04_16_09_15_46_00'
quantization_tunings = [1, 10, 15]
participation_rates = [0.5, 1]
agents_numbers = [1, 5, 10]
average_period = 1

files_id = ['0'+str(i) for i in range(1,10)] + [str(i) for i in range(10,19)]
file = '/progress.csv'

fig = plt.figure(figsize=(20,10), dpi=70)
axs = fig.subplots(len(participation_rates), len(agents_numbers))
for k_quant, quantization_tuning in enumerate(quantization_tunings):
	for k_part, participation_rate in enumerate(participation_rates):
		for k_agent, agents_number in enumerate(agents_numbers):
			id_file = files_id[len(agents_numbers)*len(participation_rates)*k_quant + len(agents_numbers)*k_part + k_agent]
			transf_bits = [0]
			average_returns = [0]
			with open(dir_prefix + id_file + file) as csvfile:
				reader = csv.DictReader(csvfile)
				for row in reader:
					transf_bits.append(int(row['TransfBits']))
					average_returns.append(float(row['TotalAverageReturn']))
			axs[k_part][k_agent].plot(transf_bits, average_returns, label='Quantization tuning: ' + str(quantization_tuning))
			axs[k_part][k_agent].set_title('Average period: ' + str(average_period) + ' ; Agents number: ' + str(agents_number) + ' ; Participation rate: ' + str(participation_rate))
			axs[k_part][k_agent].set(xlabel='Transferred Bits', ylabel='Average returns')

handles, labels = axs[0,0].get_legend_handles_labels()
fig.legend(handles, labels, shadow=True, fancybox=True, bbox_to_anchor=[0.9, 0.3])
fig.suptitle('Difference parameters, every agent update their mean parameters reference, quantized, average period: ' + str(average_period))
fig.savefig(dir_prefix)

max_stars_repo_name: Emilie-Thome/Multi-Agent-RL

max_issues_repo_name: Emilie-Thome/Multi-Agent-RL

max_forks_repo_name: Emilie-Thome/Multi-Agent-RL

reason: import numpy,from scipy


text:
# Copyright (C) 2017 Christian T. Jacobs

# 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 numpy
import h5py
from matplotlib.pylab import *
import matplotlib.animation as animation
import matplotlib
plt.style.use('dark_background')
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
Writer = animation.writers['avconv']
writer = Writer(fps=8, metadata=dict(title="Compressible Rayleigh-Taylor instability", artist="Christian T. Jacobs"), bitrate=1800)

Nh = 1
Nx = 1022
Ny = 1022
Nxh = Nx+2*Nh
Nyh = Ny+2*Nh

Lx = 1.0
Ly = 3.0

fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111,aspect='equal')

cbar = None
def update(i):
    global cbar
    j = int(i)*250
    f = h5py.File("fields_%d.h5" % j, 'r')
    group = f['fields']

    x = linspace(0, Lx, Nxh)

    u = group["r"].value
    u = u.reshape((Nyh, Nxh))
    print i, j, u.min(), u.max()

    ax.cla()
    cmap = plt.cm.get_cmap("jet")
    c = ax.imshow(u[0:Nyh, 0:Nxh], extent = [0, Lx, 0, Ly], origin="lower", interpolation="none", cmap=cmap)
    if not cbar:
        cbar = plt.colorbar(c, aspect=50, pad=0.12, orientation='vertical')
        cbar.set_label(r'$\rho$', fontsize=14)
        cbar.set_ticks([0, 0.5, 1.0, 1.5, 2.0, 2.5])

    ax.set_xlabel(r"$x$", fontsize=14, labelpad=-0.5)
    ax.set_ylabel(r"$y$", fontsize=14, labelpad=-1.0)
    ax.set_title("Compressible Rayleigh-Taylor instability\nChristian T. Jacobs, 2017\n", fontsize=9)

    return ax

ani = animation.FuncAnimation(fig, update, frames=range(125,650), interval=100, repeat=False)
ani.save('compressible-rayleigh-taylor-instability.mp4', writer=writer)


text:
import os
import random
from collections import defaultdict
from cyvcf2 import VCF, Writer
import numpy as np
from eskedit.constants import get_autosome_names_grch38, get_autosome_lengths_grch38
from eskedit.kclass import GRegion

def get_split_chrom_vcf(vcf_path, chrom, nbins):
    vcf = VCF(vcf_path)
    nbins = int(nbins)
    names = get_autosome_names_grch38()
    if chrom not in names:
        print('Chromosome key, %s, not found in VCF, please check for typos.' % str(chrom))
    chrom_len = 0
    for kv in zip(vcf.seqnames, vcf.seqlens):
        if kv[0] == chrom:
            chrom_len = kv[1]
    chunk_size = chrom_len // nbins
    regions = []
    start = 1
    stop = chunk_size
    for i in range(nbins):
        if i < nbins - 1:
            regions.append(GRegion(chrom, start, stop))
            start = stop
            stop += chunk_size
        else:
            regions.append(GRegion(chrom, start, chrom_len))
    return regions

def get_split_vcf_regions(vcf_path, nprocs):
    vcf = VCF(vcf_path)
    nprocs = int(nprocs)
    num_entries = np.sum(vcf.seqlens)
    chunk_size = int(num_entries / nprocs) + 1
    names = get_autosome_names_grch38()
    num_chunk = 0
    regions = []
    gen_pos = 0
    current_chromosome = 0
    chrom_pos = 0
    while num_chunk < nprocs and current_chromosome < len(names):
        current_chunk = 0
        region = []
        while current_chunk < chunk_size:
            if current_chromosome >= len(names):
                current_chunk = chunk_size
                continue
            remaining_chunk = chunk_size - current_chunk
            if remaining_chunk <= (vcf.seqlens[current_chromosome] - chrom_pos):
                new_region = GRegion(vcf.seqnames[current_chromosome], chrom_pos, chrom_pos + remaining_chunk)
                region.append(new_region)
                chrom_pos += remaining_chunk
                current_chunk += new_region.size()
                gen_pos += new_region.size()
                continue
            else:  # remaining chunk can fit remainder of chromosome and then some
                new_region = GRegion(vcf.seqnames[current_chromosome], chrom_pos, vcf.seqlens[current_chromosome])
                region.append(new_region)
                current_chunk += new_region.size()
                gen_pos += new_region.size()
                chrom_pos = 0
                current_chromosome += 1
        regions.append(region)
    # print(regions)
    return regions

def split_seq(sequence, nprocs, overlap=None):
    """
    :param sequence: string sequence to split
    :param nprocs: number of CPUs to use
    :param overlap: for use when counting k-mers. If considering 3-mers, overlap=3
    :return: a list of 'nprocs' roughly equal chunks to pass as an argument to python multiprocessing
    """
    chunk_size = int(len(sequence) / nprocs) + 1
    args = []
    start = 0
    end = chunk_size
    for proc in range(nprocs):
        if overlap is not None:
            args.append(sequence[start:(end + overlap - 1)])
        else:
            args.append(sequence[start:end])
        start = end
        end += chunk_size
        if end > len(sequence):
            end = len(sequence)
    return args

def get_bed_windows(window, overlap=0, outfile=None, bins=False):
    chrom_info = get_autosome_lengths_grch38()
    regions = []
    for chrom, length in chrom_info.items():
        if bins:
            window_size = length // window + 1
        else:
            window_size = window
        if window_size > length:
            print("Window size is larger than chromosome. Please reduce window size")
            exit(1)
        if overlap >= window_size:
            print("Overlap value (%d) is too large for window size (%d). Using overlap=0" % (overlap, window_size))
            overlap = 0
        current_position = 1
        while current_position < length:
            regions.append((chrom, max(1, current_position - overlap), min(length, current_position + window_size - 1)))
            current_position += window_size
    if outfile is not None:
        with open(outfile, 'w') as bedfile:
            for region in regions:
                bedfile.write('%s\t%s\t%s\n' % (str(region[0]), str(region[1]), str(region[2])))
    return regions

def append_variants_to_vcf(filename, chrom, start, stop, outfile='samp_build38.vcf'):
    return "tabix " + filename + " " + str(chrom) + ":" + str(
        start) + "-" + str(
        stop) + " >> " + outfile

def generate_sample_vcf(vcf_path, outfile='samp_build38.vcf'):
    """Takes a large VCF file and takes random samples from each chromosome to make a smaller VCF for testing"""
    vcf = VCF(vcf_path)
    write = Writer(outfile, vcf)
    write.write_header()
    key_values = zip(vcf.seqnames, vcf.seqlens)
    chrom_keys = defaultdict(int)
    chroms = get_autosome_names_grch38()
    for kv in list(key_values):
        if kv[0] in chroms:
            chrom_keys[kv[0]] = kv[1]
    for chrom_num, chrom_len in chrom_keys.items():
        begin = random.randint(100000, chrom_len - 100000)
        os.system(append_variants_to_vcf(vcf_path, chrom_num, begin, begin + 10_000, outfile=outfile))
    write.close()

max_stars_repo_name: quinlan-lab/kmertools

max_issues_repo_name: quinlan-lab/kmertools

max_forks_repo_name: quinlan-lab/kmertools


text:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 22 17:51:08 2019

@author: Oscar Flores
"""
#%% Importar Librerías.
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from Simulacion import Optimizacion
from Simulacion import Graficos
from Simulacion import Genetico
from Simulacion import Kclusters

simulacion = Optimizacion.simulacion
grafico = Graficos.simulacion
genetico = Genetico.genetico
k_clusters = Kclusters.k_clusters
#%%############################################################################
############################  Crear model_close ###############################'ALFAA.MX',
###############################################################################

#%% Datos en csv
csv = ['AMXL.MX','WALMEX.MX','TLEVISACPO.MX','GMEXICOB.MX','GFNORTEO.MX','CEMEXCPO.MX','PENOLES.MX','GFINBURO.MX','ELEKTRA.MX','BIMBOA.MX','AC.MX','KIMBERA.MX','LABB.MX','LIVEPOL1.MX','ASURB.MX','GAPB.MX','ALPEKA.MX','GRUMAB.MX','ALSEA.MX','GCARSOA1.MX','PINFRA.MX']
for i in np.arange(len(csv)):
    csv[i] = '../Data/%s.csv'%csv[i]

#ndias = [3,5,8,13,21,34,55,89,144]
ndias = [5,20,40,125]
n_clusters = 4
#%% Guardamos model_close en model_close3.sav
nombre = 'model_close1'
#k_clusters(csv, ndias, n_clusters, nombre)

#%% Importamos model_close para futuro uso.
model_close = pickle.load(open('model_close1.sav','rb'))

#%% Se crea vector de toma de decisiones para futuro uso
Ud = np.random.randint(-1,2,len(model_close)**len(ndias))

#%%############################################################################
######################  Simulación para optimización ##########################
###############################################################################

#%% Simulamos
#Vp = simulacion(csv,ndias,model_close,Ud)
Rend =  np.diff(Vp) / Vp[:,:-1] #Rendimientos diarios.
Port = Rend.mean(axis=0) #Creamos un portafolio con la misma cantidad de dinero en cada activo.
Mean = Port.mean()
Std = Port.std()
plt.scatter((Std*252)**0.5,Mean*252)

#%%############################################################################
#########################  Simulación para Graficar ###########################
###############################################################################

#%% graficamos todas las simulaciones
#Sim = grafico(csv,ndias,model_close,Ud)

#%%############################################################################
###################  Optimización por algorigmo genético ######################
###############################################################################

#%% Simulamos
l_vec = 512 # longitud de cada vector de toma de decisiones
n_vec = 16 # cantidad de vectores de toma de decisiones por generacion
iteraciones = 5
C = 0
nombre = 'prueba'
#####genetico(func,csv,ndias,model_close,l_vec,l_dec,iteraciones,C)
genetico(simulacion,csv,ndias,model_close,l_vec,n_vec,iteraciones,C,nombre)

#%%
[p,a,m,hist_m,hist_s,hist_a,m_hist] = pickle.load(open(nombre + '.sav','rb'))


text:
import math, copy, sys
import numpy as np

class Map(object):
  def __init__(self, size_x, size_y, offset_x, offset_y, resolution, data):
    self.size_x_ = size_x
    self.size_y_ = size_y
    self.offset_x_ = offset_x
    self.offset_y_ = offset_y
    self.resolution_ = resolution
    self.data_ = data

  def world2map(self, x, y):
    map_x = np.round( (x-self.offset_x_)/self.resolution_ )
    map_y = np.round( (y-self.offset_y_)/self.resolution_ )
    if map_x<0 or map_y<0 or map_x>=self.size_x_ or map_y>=self.size_y_: return 0, 0, False
    return map_x, map_y, True

  def map2world(self, map_x, map_y):
    x = (map_x + 0.5)*self.resolution_ + self.offset_x_
    y = (map_y + 0.5)*self.resolution_ + self.offset_y_
    return x, y

class Laser(object):
  def __init__(self, max_range, min_angle, resolution_angle, no_of_beams, noise_variance, map_ptr):
    self.max_range_ = max_range
    self.min_angle_ = min_angle
    self.resolution_angle_ = resolution_angle
    self.no_of_beams_ = no_of_beams
    self.noise_variance_ = noise_variance
    self.map_ptr_ = map_ptr
    self.ranges_ = np.zeros(no_of_beams)

  def scan(self, x, y, theta):
    current_angle = self.updateAngle(theta, self.min_angle_)
    for r in range(self.ranges_.shape[0]):
      self.ranges_[r] = self.rayCast(x, y, current_angle)
      current_angle = self.updateAngle(current_angle, self.resolution_angle_)
    return self.ranges_

  def rayCast(self, x, y, theta):
    start_x, start_y, is_ok = self.map_ptr_.world2map(x,y)
    if not is_ok: return 0

    # Initialization
    direction = np.zeros(2, dtype="float64")
    direction[0] = np.cos(theta)
    direction[1] = np.sin(theta)

    origin = np.zeros(2, dtype="float64")
    origin[0] = x
    origin[1] = y

    current = np.zeros(2, dtype="float64")
    current[0] = start_x
    current[1] = start_y

    voxelBorder = np.zeros(2, dtype="float64")
    voxelBorder[0], voxelBorder[1] = self.map_ptr_.map2world(current[0], current[1])
    voxelBorder[0] -= 0.5 * self.map_ptr_.resolution_
    voxelBorder[1] -= 0.5 * self.map_ptr_.resolution_

    step = np.zeros(2, dtype="float64")
    tMax = np.zeros(2, dtype="float64")
    tDelta = np.zeros(2, dtype="float64")

    # Compute step direction
    for i in range(2):
      if direction[i]>0.0: step[i] = 1
      elif direction[i]<0.0: step[i] = -1
      else: step[i] = 0

      if step[i]!=0:
        if step[i]==1:
          voxelBorder[i] += float( step[i] * self.map_ptr_.resolution_ * 1.0 )
        tMax[i] = (voxelBorder[i] - origin[i]) / direction[i]
        tDelta[i] = self.map_ptr_.resolution_ / abs(direction[i])
      else:
        tMax[i] = sys.float_info.max
        tDelta[i] = sys.float_info.max

    # Incremental phase
    while True:

      # Find minimum tMax
      if tMax[0] < tMax[1]: dim = 0
      else: dim = 1

      # Advance in direction of dim
      current[dim] += step[dim]
      tMax[dim] += tDelta[dim]

      if np.sqrt((current[0] - start_x)*(current[0] - start_x) + (current[1] - start_y)*(current[1] - start_y)) * self.map_ptr_.resolution_ > self.max_range_:
        return self.max_range_
      else:
        if current[0] < 0 or current[1] < 0 or current[0]>=self.map_ptr_.size_x_ or current[1]>=self.map_ptr_.size_y_:
          flag = False
        else:
          value = self.map_ptr_.data_[current[0], current[1]]
          flag = True
        if flag:
          if value < 0.55:
            mu = 0
            sigma = np.sqrt(self.noise_variance_)
            noise = sigma * np.random.randn() + mu
            return np.sqrt((current[0] - start_x)*(current[0] - start_x) + (current[1] - start_y)*(current[1] - start_y)) * self.map_ptr_.resolution_ + noise
        else:
          return self.max_range_

  def updateAngle(self, angle, increment):
    angle += increment
    if angle > math.pi*2:
      angle -= math.pi*2
    return angle

max_stars_repo_name: shamengjun/localization

max_issues_repo_name: shamengjun/localization

max_forks_repo_name: Dtananaev/localization


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

from super_klust.super_klust import SuperKlust
from super_k.super_k import SuperK

from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier

from tabulate import tabulate
import timeit

N_REPEAT = 10
N_NUMBER = 1

TIME_SCALE = 1e-3 # milliseconds

datasets = [
    "optdigits",
    ("USPS", 2),
    "satimage",
    "letter",
    "isolet",
    ]

test_sizes = {
    "optdigits": 1797,
    "USPS":      2007,
    "letter":    4000,
    "isolet":    1557,
    "default":   0.2
}

k_search_ranges = {
    "optdigits": range(5, 75, 5),
    "USPS": range(5, 75, 5),
    "satimage": range(5, 75, 5),
    "letter": range(5, 100, 5),
    "isolet": range(5, 75, 5),
    "default": range(5, 1000, 10)
}

superklust_params = {
    "optdigits": {"k": 10},
    "USPS":      {"k": 40},
    "satimage":  {"k": 35},
    "letter":    {"k": 65},
    "isolet":    {"k": 5},
}

superk_params = {
    "optdigits": {"k": 10},
    "USPS":      {"k": 40},
    "satimage":  {"k": 35},
    "letter":    {"k": 65},
    "isolet":    {"k": 5},
}

def grid_search_k(X, y, k_range):

    params = {'k': k_range}
    spk = SuperKlust()
    clf = GridSearchCV(spk, params)
    clf.fit(X, y)
    best_k_index = clf.cv_results_["mean_test_score"].argmax()
    k = clf.cv_results_["param_k"][best_k_index]

    return k

def tvt_main():

    classifiers = ["superklust", "superk", "svmlinear", "svmrbf", "svmpoly", "knn"]

    accuracies = {clfier : {} for clfier in classifiers}
    train_times = {clfier : {} for clfier in classifiers}
    test_times = {clfier : {} for clfier in classifiers}

    for item in datasets:

        try:
            ds_name, ver = item
        except:
            ds_name = item
            ver = 1

        print("{} {}".format(ds_name, ver))
        dataset = fetch_openml(ds_name, version=ver)
        try:
            test_size = test_sizes[ds_name]
        except:
            test_size = test_sizes["default"]
        X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=test_size, shuffle=False)

        #### Super-klust ####
        try:
            k = superklust_params[ds_name]["k"]
        except:
            try:
                param_range = k_search_ranges[ds_name]
            except:
                param_range = k_search_ranges["default"]
            k = grid_search_k(X_train, y_train, param_range)

        clf = SuperKlust(k, verbose=True)

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["superklust"][ds_name] = accuracy
        train_times["superklust"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["superklust"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nSuper-klust\n{}\nk: {}, n_genpts: {}\nTest score: {}\n{}".format("-" * 40, ds_name, k, clf.n_genpts, accuracy, "-" * 40))

        #### Super-k ####
        k = superk_params[ds_name]["k"]
        clf = SuperK(k)

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["superk"][ds_name] = accuracy
        train_times["superk"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["superk"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nSuper-k\n{}\nk: {}, n_genpts: {}\nTest score: {}\n{}".format("-" * 40, ds_name, k, clf.n_genpts, accuracy, "-" * 40))

        #### SVM Linear ####
        clf = SVC(kernel="linear")

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["svmlinear"][ds_name] = accuracy
        train_times["svmlinear"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["svmlinear"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nSVM Linear\n{}\nTest score: {}\n{}".format("-" * 40, ds_name, accuracy, "-" * 40))

        #### SVM RBF ####
        clf = SVC(kernel="rbf")

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["svmrbf"][ds_name] = accuracy
        train_times["svmrbf"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["svmrbf"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nSVM RBF\n{}\nTest score: {}\n{}".format("-" * 40, ds_name, accuracy, "-" * 40))

        #### SVM Poly ####
        clf = SVC(kernel="poly")

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["svmpoly"][ds_name] = accuracy
        train_times["svmpoly"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["svmpoly"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nSVM Poly\n{}\nTest score: {}\n{}".format("-" * 40, ds_name, accuracy, "-" * 40))

        #### KNN ####
        clf = KNeighborsClassifier()

        train_timer = timeit.Timer(lambda : clf.fit(X_train, y_train))
        train_results = train_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracy = None
        def f():
            nonlocal accuracy
            accuracy = clf.score(X_test, y_test)
        test_timer = timeit.Timer(f)
        test_results = test_timer.repeat(repeat=N_REPEAT, number=N_NUMBER)

        accuracies["knn"][ds_name] = accuracy
        train_times["knn"][ds_name] = (np.mean(train_results) / TIME_SCALE, np.std(train_results) / TIME_SCALE)
        test_times["knn"][ds_name] = (np.mean(test_results) / TIME_SCALE, np.std(test_results) / TIME_SCALE)

        print("{}\nKNN\n{}\nTest score: {}\n{}".format("-" * 40, ds_name, accuracy, "-" * 40))

    print("Super-klust Results:\n{}\n{}\n{}\n".format(accuracies["superklust"], train_times["superklust"], test_times["superklust"]))
    print("Linear SVM Results:\n{}\n{}\n{}\n".format(accuracies["svmlinear"], train_times["svmlinear"], test_times["svmlinear"]))
    print("SVM with RBF kernel results:\n{}\n{}\n{}\n".format(accuracies["svmrbf"], train_times["svmrbf"], test_times["svmrbf"]))
    print("SVM with Poly kernel results:\n{}\n{}\n{}\n".format(accuracies["svmpoly"], train_times["svmpoly"], test_times["svmpoly"]))
    print("KNN results:\n{}\n{}\n{}\n".format(accuracies["knn"], train_times["knn"], test_times["knn"]))

    accuracy_table = []

    accuracy_table.append([""] + ["{}".format(key) for key, _ in accuracies["superklust"].items()])
    accuracy_table.append(["Super-klust"] + ["{:.3f}".format(value) for _, value in accuracies["superklust"].items()])
    accuracy_table.append(["Super-k"] + ["{:.3f}".format(value) for _, value in accuracies["superk"].items()])
    accuracy_table.append(["SVM Linear"] + ["{:.3f}".format(value) for _, value in accuracies["svmlinear"].items()])
    accuracy_table.append(["SVM RBF"] + ["{:.3f}".format(value) for _, value in accuracies["svmrbf"].items()])
    accuracy_table.append(["SVM Poly"] + ["{:.3f}".format(value) for _, value in accuracies["svmpoly"].items()])
    accuracy_table.append(["KNN"] + ["{:.3f}".format(value) for _, value in accuracies["knn"].items()])

    accuracy_latex = tabulate(accuracy_table, tablefmt="latex_raw")
    print(accuracy_latex)

    with open('accuracy_table.tex', "w") as f:
        f.write(accuracy_latex)

    time_str = "{:.1f}{{\small ({:.1f})}}"

    train_times_table = []

    train_times_table.append([""] + ["{}".format(key) for key, _ in train_times["superklust"].items()])
    train_times_table.append(["Super-klust"] + [time_str.format(*value) for _, value in train_times["superklust"].items()])
    train_times_table.append(["Super-k"] + [time_str.format(*value) for _, value in train_times["superk"].items()])
    train_times_table.append(["SVM Linear"] + [time_str.format(*value) for _, value in train_times["svmlinear"].items()])
    train_times_table.append(["SVM RBF"] + [time_str.format(*value) for _, value in train_times["svmrbf"].items()])
    train_times_table.append(["SVM Poly"] + [time_str.format(*value) for _, value in train_times["svmpoly"].items()])
    train_times_table.append(["KNN"] + [time_str.format(*value) for _, value in train_times["knn"].items()])

    train_times_latex = tabulate(train_times_table, tablefmt="latex_raw")
    print(train_times_latex)

    with open('train_times_table.tex', "w") as f:
        f.write(train_times_latex)

    test_times_table = []

    test_times_table.append([""] + ["{}".format(key) for key, _ in test_times["superklust"].items()])
    test_times_table.append(["Super-klust"] + [time_str.format(*value) for _, value in test_times["superklust"].items()])
    test_times_table.append(["Super-k"] + [time_str.format(*value) for _, value in test_times["superk"].items()])
    test_times_table.append(["SVM Linear"] + [time_str.format(*value) for _, value in test_times["svmlinear"].items()])
    test_times_table.append(["SVM RBF"] + [time_str.format(*value) for _, value in test_times["svmrbf"].items()])
    test_times_table.append(["SVM Poly"] + [time_str.format(*value) for _, value in test_times["svmpoly"].items()])
    test_times_table.append(["KNN"] + [time_str.format(*value) for _, value in test_times["knn"].items()])

    test_times_latex = tabulate(test_times_table, tablefmt="latex_raw")
    print(test_times_latex)

    with open('test_times_table.tex', "w") as f:
        f.write(test_times_latex)

if __name__ == "__main__":
    tvt_main()

max_stars_repo_name: ituamg/super-klust

max_issues_repo_name: ituamg/super-klust

max_forks_repo_name: ituamg/super-klust