text:
"""
This module provides an api that can be passed instead for the
:class:`qcodes.instrument_drivers.AlazarTech.ats_api.AlazarATSAPI`
, which does not require a real alazar card present in the system.
This mock does currently represent a ATS9360 and does not provide
any functionality whatsoever.
"""

from typing import Tuple, Callable, Optional, Dict
import numpy as np
import ctypes

from qcodes.instrument_drivers.AlazarTech.dll_wrapper import (
    _mark_params_as_updated, ReturnCode)
from qcodes.instrument_drivers.AlazarTech.constants import (
    Capability)
from qcodes.instrument_drivers.AlazarTech.ats_api import AlazarATSAPI

class SimulatedATS9360API(AlazarATSAPI):

    registers = {
        8: 70254688,
        58: np.uint32(1 << 26)  # Trigger hold off
    }

    def __init__(
            self,
            dll_path: str = None,  # Need this for meta super class
            buffer_generator: Optional[Callable[[np.ndarray], None]] = None,
            dtype: type = np.uint16
    ):
        def _default_buffer_generator(buffer: np.ndarray) -> None:
            upper = buffer.size // 2
            buffer[0:upper] = 30000 * np.ones(upper)

        # don't call `super().__init__` here to avoid dependence on the
        # alazar driver, when loading the dll
        self._buffer_generator = (
            buffer_generator or _default_buffer_generator)
        self.dtype = dtype
        self.buffers: Dict[int, np.array] = {}

    def _sync_dll_call(self, c_name: str, *args):
        _mark_params_as_updated(*args)

    def get_board_by_system_id(self, system_id: int, board_id: int) -> int:
        # This is the handle....
        return 12335

    def busy(self, handle: int) -> int:
        return 0

    # OTHER API-RELATED METHODS

    def get_board_model(self, handle: int) -> str:
        return 'ATS9360'

    def get_channel_info_(self, handle: int) -> Tuple[int, int]:
        return 4294967294, 12

    def get_cpld_version_(self, handle: int) -> str:
        return "25.16"

    def get_driver_version_(self) -> str:
        return '6.5.1'

    def get_sdk_version_(self) -> str:
        return '6.5.1'

    def query_capability_(self, handle: int, capability: int) -> int:
        capabilities = {
            Capability.ASOPC_TYPE: 1763017568,
            Capability.GET_SERIAL_NUMBER: 970396,
            Capability.MEMORY_SIZE: 4294967294,
            Capability.GET_PCIE_LINK_WIDTH: 8,
            Capability.GET_PCIE_LINK_SPEED: 2,
            Capability.GET_LATEST_CAL_DATE: 250117}
        return capabilities[Capability(capability)]

    def read_register_(self, handle: int, offset: int) -> int:
        return self.registers[offset]

    def write_register_(self, handle: int, offset: int, value: int) -> None:
        self.registers[offset] = value

    def post_async_buffer(
            self,
            handle: int,
            buffer: ctypes.c_void_p,
            buffer_length: int
    ) -> ReturnCode:
        if buffer.value is None:
            raise RuntimeError(
                '`post_async_buffer` received buffer with invalid address.')
        ctypes_array = (ctypes.c_uint16 *
                        (buffer_length // 2)).from_address(buffer.value)
        self.buffers[buffer.value] = np.frombuffer(
            ctypes_array, dtype=self.dtype)
        return self._sync_dll_call(
            'AlazarPostAsyncBuffer', handle, buffer, buffer_length)

    def wait_async_buffer_complete(
            self,
            handle: int,
            buffer: ctypes.c_void_p,
            timeout_in_ms: int
    ) -> ReturnCode:
        if buffer.value is None:
            raise RuntimeError(
                '`wait_async_buffer` received buffer with invalid address.')
        b = self.buffers.get(buffer.value)
        self._buffer_generator(b)
        return self._sync_dll_call(
            'AlazarWaitAsyncBufferComplete', handle, buffer, timeout_in_ms)

max_stars_repo_name: QTransportLabUAM/Qcodes

max_issues_repo_name: Dominik-Vogel/Qcodes

max_forks_repo_name: Dominik-Vogel/Qcodes


text:
import numpy as np
import pandas as pd

from graphviz import Digraph

def read_TimeSeries(inputfile, format='csv'):
    if format=='csv':
        return pd.read_csv(inputfile,sep='\t')
    elif format=='sequential':
        a = open(inputfile, 'r').read()
        if (a[0:2] == '((' and a[-1] == ')'):
            a = a[1:-1]
        h1 = a[a.find('(') + 1: a.find(')')].split(',')
        headers = [h.strip('"') for h in h1]
        a = a[ a.find('(', a.find(')')) :]
        a = a.strip('()')
        dat = a.split('),(')
        dat = [ s.split(',') for s in dat]
        m = np.array(dat, dtype=float)
        # tab = np.fromregex(inputfile, ("\(" + "(\d+),\W+"*(len(headers)) + "\)") , dtype= ([float]*len(headers)) )
        return (headers, m)

def draw_graph(causal_network, outfile='network.gv', view=False):
    g = Digraph('G', filename=outfile)
    for c in causal_network.columns:
        for i in causal_network.index:
            if causal_network.at[i,c] == 1: g.edge(c,i, arrowhead="vee")
            elif causal_network.at[i,c] == -1: g.edge(c,i, arrowhead="tee")
    g.render(outfile, view=view)

if __name__ == '__main__':
    h, a = read_TimeSeries('../test_data/prob1/run-1.tsd', format='sequential')

ext: py

max_stars_repo_name: ttdtrang/pygenenet

max_issues_repo_name: ttdtrang/pygenenet

max_forks_repo_name: ttdtrang/pygenenet

reason: import numpy


text:
import warnings
from typing import Optional

from numpy import random
from sklearn.decomposition import TruncatedSVD

from ..definitions import (
    DenseMatrix,
    DenseScoreArray,
    InteractionMatrix,
    UserIndexArray,
)
from .base import (
    BaseRecommenderWithItemEmbedding,
    BaseRecommenderWithUserEmbedding,
    RecommenderConfig,
)

class TruncatedSVDConfig(RecommenderConfig):
    n_components: int = 4
    random_seed: int = 0

class TruncatedSVDRecommender(
    BaseRecommenderWithUserEmbedding,
    BaseRecommenderWithItemEmbedding,
):
    """Use (randomized) SVD to factorize the input matrix into low-rank matrices.

    Args:
        X_train_all:
            Input interaction matrix.

        n_components:
            The rank of approximation. Defaults to 4.
            If this is larger than X_train_all, the value will be truncated into ``X_train_all.shape[1]``

        random_seed:
            The random seed to be passed on core TruncSVD.
    """

    config_class = TruncatedSVDConfig

    def __init__(
        self,
        X_train_all: InteractionMatrix,
        n_components: int = 4,
        random_seed: int = 0,
    ) -> None:

        assert X_train_all.shape[1] > 1
        super().__init__(X_train_all)
        if n_components >= self.X_train_all.shape[1]:
            warnings.warn(
                "n_components >= than X_train_all.shape[1]. Set it to X_train_all.shape[1] - 1."
            )
            n_components = self.X_train_all.shape[1] - 1
        self.n_components = n_components
        self.decomposer_: Optional[TruncatedSVD] = None
        self.z_: Optional[DenseMatrix] = None
        self.random_seed = random_seed

    @property
    def z(self) -> DenseMatrix:
        if self.z_ is None:
            raise RuntimeError("z fetched before fit")
        return self.z_

    @property
    def decomposer(self) -> TruncatedSVD:
        if self.decomposer_ is None:
            raise RuntimeError("decomposer fetched before fit.")
        return self.decomposer_

    def _learn(self) -> None:
        self.decomposer_ = TruncatedSVD(
            n_components=self.n_components, random_state=self.random_seed
        )
        self.z_ = self.decomposer_.fit_transform(self.X_train_all)

    def get_score(self, user_indices: UserIndexArray) -> DenseScoreArray:
        return self.z[user_indices].dot(self.decomposer.components_)

    def get_score_block(self, begin: int, end: int) -> DenseScoreArray:
        return self.z[begin:end].dot(self.decomposer.components_)

    def get_score_cold_user(self, X: InteractionMatrix) -> DenseScoreArray:
        return self.decomposer.transform(X).dot(self.decomposer.components_)

    def get_user_embedding(self) -> DenseMatrix:
        return self.z

    def get_score_from_user_embedding(
        self, user_embedding: DenseMatrix
    ) -> DenseScoreArray:
        return user_embedding.dot(self.decomposer.components_)

    def get_item_embedding(self) -> DenseMatrix:
        return self.decomposer.components_.T

    def get_score_from_item_embedding(
        self, user_indices: UserIndexArray, item_embedding: DenseMatrix
    ) -> DenseScoreArray:
        return self.z[user_indices].dot(item_embedding.T)

max_stars_repo_name: tnakae/irspack

max_issues_repo_name: tnakae/irspack

max_forks_repo_name: tnakae/irspack

reason: from numpy


text:
import math
import sys
import numpy as np

from bin.classes.class_system import LinearProblem
from bin.input import input_vars

def main():
    n, m, type_, c, A, sign, b = input_vars()

    b = np.array(b).reshape((n, 1))
    sign = np.array(sign).reshape((n, 1))
    b = np.hstack((sign, b)).tolist()
    for i in range(len(b)):
        b[i][1] = float(b[i][1])
    A = A.tolist()
    c = c.tolist()

    system = LinearProblem(type_, A, b, c)
    system.print_problem()
    print()
    system.gomory_cut()

if __name__ == "__main__":
    main()

max_stars_repo_path: 08. Gomory algorithm - Branch and Cut/main.py


text:
from time import sleep
import os.path

import cv2 as cv
import numpy as np
import win32ui
import win32gui
import win32con

import flexframe
import autoconfig

HEIGHT = 0
WIDTH = 1
CHANNELS = 2
##    -pcnt_regions are defined in % from top left origin
##     and must be provided as a flexframe with the following item format:
##        {'top':%, 'left':%,'bottom':%, 'right':%}
##    -internal storage for templates and data is:
##        {index:{'source':source,
##                'output_data':data,
##                'template_*method name1*':image,
##                ...
##                'template_*method nameN*':image}}

class ImageToData:
    """Utility for converting regions of images to data through several methods

    Public Interface:
    get_data()
    method -- method of matching image regions with templates
              'rgb correlation', 'grayscale correlation', 'average color'
    crop_to_resolution -- True/False -- crop window frame from images
    crop_to_4to3_aspect -- True/False -- crop widescreen portion from images
    shrink_to_height -- height in pixels for same-aspect ratio shrinking of image

    """
    def __init__(self, templates_and_data, method = 'rgb correlation',
                 crop_to_resolution = False, crop_to_4to3_aspect = False,
                 shrink_to_height = None, resolutions = None):
        """Create the ImageToData object

        Arguments:
        templates_and_data -- {index: {'source': source,
                                       'output_data': data}}
            where source can be a path to an image, an opencv image,
            or window title which will be grabbed as a screenshot

        Optional Keyword Arguments:
        method -- as in class docstring
        crop_to_resolution -- as in class docstring
        crop_to_4to3_aspect -- as in class docstring
        resolutions -- optional. must be provided as [(width,height),...]

        """
        self._templates_and_data = templates_and_data
        self.method = method
        self.crop_to_resolution = crop_to_resolution
        self.crop_to_4to3_aspect = crop_to_4to3_aspect
        self.shrink_to_height = shrink_to_height

        #load config from module, not from calling application
        if '__file__' in globals(): #path to this source file
            this_dir = os.path.abspath(os.path.dirname(__file__))
        else: #relative path when running IDLE, perhaps other situations
            this_dir = ''
        config_path = os.path.join(this_dir, 'image_to_data.ini')
        self.ac = autoconfig.AutoConfig.from_file(config_path,
                                                  interpret_data = True)
        if resolutions: self.ac.override('crop','resolutions', resolutions)

    def get_data(self, image_source, pcnt_regions = None):
        """ full image region if none given """
        if pcnt_regions == None:
            pcnt_regions = [{'top':0, 'left':0,'bottom':100, 'right':100}]
        # prepare image and templates for testing
        out_frame = flexframe.FlexFrame(*pcnt_regions._dimensions)
        try:
            image = self._prepare_image(image_source)
            self._prepare_templates() # ugly but function only prepares once
        except RuntimeError as e:
            print(e)
            return out_frame #if a problem getting data, return empty frame
        for pcnt_region, frame_position in pcnt_regions.nodes():
            data_index = self._identify(image, pcnt_region)
            data = self._templates_and_data[data_index]['output_data']
            out_frame.place(data, *frame_position)
        return out_frame

    def _prepare_image(self, source):
        image = self._source_to_image(source)
        image = self._adjust_for_rules(image)
        image = self._adjust_for_method(image)
        return image

    def _prepare_template(self, source):
        image = self._source_to_image(source)
        image = self._adjust_for_method(image)
        return image

    def _prepare_templates(self):
        # only creates templates one time and stores afterward
        name = 'template_' + self.method
        for index, td in self._templates_and_data.items():
            if name not in td:
                td[name] = self._prepare_template(td['source'])

    def _identify(self, image, pcnt_region):
        left, top, width, height = self._pcnt_region_to_ROI(image, pcnt_region)
        image = image[top:top+height, left:left+width]
        t_name = 'template_' + self.method
        # compare ROI to all templates
        all_comparisons = {index: 0 for index in self._templates_and_data.keys()}
        for index, td in self._templates_and_data.items():
            template = td[t_name] #get appropriate template image for method
            w_result = image.shape[WIDTH] - template.shape[WIDTH] + 1
            h_result = image.shape[HEIGHT] - template.shape[HEIGHT] + 1
            # minimum_loc = cvPoint(0,0)
            # maximum_loc = cvPoint(0,0)
            result = cv.matchTemplate(image, template, cv.TM_SQDIFF)
            min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
            all_comparisons[index] = min_val
        index = min(all_comparisons, key=all_comparisons.get)
        return index

    def _pcnt_region_to_ROI(self, image, region):
        """
        region is in top,left,bottom,right
        ROI must be a cvRect(left, top, width, height)
        """
        left = int(round(image.shape[WIDTH] * region['left'] / 100))
        width = int(round(image.shape[WIDTH] * (region['right'] - region['left']) / 100))
        top = int(round(image.shape[HEIGHT] * region['top'] / 100))
        height = int(round(image.shape[HEIGHT] * (region['bottom'] - region['top']) / 100))
        return left, top, width, height

    def _source_to_image(self, source):
        try:
            if os.path.isfile(source): #always start with 3 channel rgb
                return cv.imread(source, flags=cv.IMREAD_COLOR)
        except: pass # just safely testing for a path
        if hasattr(source, 'height'): #lame duck typing for iplimage
            return source
        if hasattr(source, 'shape'):  # test for numpy image
            return source
        return self._get_screenshot(source) #screenshot if nothing else works

    def _adjust_for_rules(self, image):
        if self.crop_to_resolution:
            image = self._crop_to_resolution(image)
        if self.crop_to_4to3_aspect:
            image = self._crop_to_4to3_aspect(image)
        if self.shrink_to_height:
            image = self._shrink_to_height(image)
        return image

    def _adjust_for_method(self, image):
        ### these conversion methods could be isolated into another class
        if self.method == 'rgb correlation':
            result = self._convert_to_rgb(image)
        elif self.method == 'grayscale correlation':
            result = self._convert_to_grayscale(image)
        elif self.method == 'average color':
            result = self._convert_to_average_color(image)
        else:
            raise RuntimeError('no method')
        return result

    def _convert_to_rgb(self, image):
        try:
            return cv.cvtColor(image, cv.COLOR_BGRA2BGR)
        except:
            pass
        try:
            return cv.cvtColor(image, cv.COLOR_GRAY2BGR)
        except:
            pass
        raise RuntimeError('Unable to convert to rgb')

    def _convert_to_grayscale(self, image):
        try:
            return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        except:
            pass
        try:
            return cv.cvtColor(image, cv.COLOR_BGRA2GRAY)
        except:
            pass
        raise RuntimeError('Unable to convert to grayscale')

    def _convert_to_average_color(self, image):
        image = self._convert_to_rgb(image)
        avg = np.average(image)
        avg_image = np.zeros((1, 1, image.shape[CHANNELS]), image.dtype)
        avg_image.fill(avg)
        return avg_image #return an image of the average color for consistency

    def _get_screenshot(self, window_title):
        """ Get the pq hwnd """
        hwnd = self._get_hwnd(window_title)

        # Get a DC for the client area of the window
        clientHandle = win32gui.GetDC(hwnd)
        clientDC = win32ui.CreateDCFromHandle(clientHandle)

        # Make and connect DC and bitmap for the screenshot image
        shotDC = clientDC.CreateCompatibleDC()
        l,t,r,b = win32gui.GetClientRect(hwnd)
        h = b-t
        w = r-l
        bmp = win32ui.CreateBitmap()
        bmp.CreateCompatibleBitmap(clientDC, w, h)
        shotDC.SelectObject(bmp)

        # Copy the client area into the screenshot DC
        dest_offset = (0,0)
        size = (w, h)
        src_offset = (0,0)
        raster = win32con.SRCCOPY

        # Make window visible and copy it. Certainly kludgy
        try: #allow it to whack the window without dying
            win32gui.SetForegroundWindow(hwnd) #can trigger permission error
##            win32gui.BringWindowToTop(hwnd) #not sure if this helps any
##            win32gui.SetFocus(hwnd) #doesn't trigger error without permission like setforegroundwindow
##            win32gui.SetActiveWindow(hwnd) #not sure if this helps any
        except:
            pass ### someday...
        sleep(.2) #lame way to allow screen to draw before taking shot
        shotDC.BitBlt(dest_offset, size, clientDC, src_offset, raster)

        ###### THIS SHOULD BE REMOVED BUT... LEAVE UNTIL HAVE  METHOD ###
        ###### TO PASS IMAGE WITHOUT A FILE. e.g. PIL for 3.x         ###
        # Scale bitmap if not already a Wx480 resolution
        if h != 600:
            h_scaled = 600
##            if w == 1280:
##                """ handle non- 4:3 resolutions """
##                w_scaled = 640
##            else:
##                """ handle 4:3 and widescreen resolutions (maybe need to split) """
            w_scaled = 800 #int(round(w * h_scaled / h)) #kludge until scale invariant working
            scaledDC = shotDC.CreateCompatibleDC()
            bmp_scaled = win32ui.CreateBitmap()
            bmp_scaled.CreateCompatibleBitmap(shotDC, w_scaled, h_scaled)
            scaledDC.SelectObject(bmp_scaled)
            scaledDC.StretchBlt((0,0), (w_scaled, h_scaled),
                                shotDC, (0,0), (w, h),
                                raster)
        else:
            bmp_scaled = bmp
            scaledDC = shotDC
        # Get screenshot bitmap into opencv image
        file_path = 'tmp.bmp'
        bmp_scaled.SaveBitmapFile(scaledDC, file_path) ##why is a DC needed?
        ###################################################################
        image_temp = cv.imread(file_path)
        os.remove(file_path)
        return cv.cvtColor(image_temp, cv.COLOR_BGRA2BGR)

    def _get_hwnd(self, window_title):
        """ get a handle to the window """
        def _window_callback(hwnd, all_windows):
            all_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
        all_windows = []
        win32gui.EnumWindows(_window_callback, all_windows)
        if all_windows:
            hwnd = [hwnd for hwnd, title in all_windows
                    if window_title in title]
            if hwnd:
                return hwnd[0]
        raise RuntimeError('window not found: ', window_title)

    def _crop_to_resolution(self, image):
        if (image.shape[WIDTH], image.shape[HEIGHT]) in self.ac.get('crop','resolutions'):
            return image

#        bad(?) trick: find the closest resolution with both
#        smaller width and height and crop to that resolution
#        or if no acceptable resolution, do nothing
#        border = half difference between image and resolution width
#        top = subtract border from height of corresponding resolution
#              (i.e. assume border on bottom is the same as sides)
        large_number = 99999
        closest_width = large_number
        closest_height = large_number
        closest_difference = large_number
        # find closest smaller width
        for res_width, res_height in self.ac.get('crop','resolutions'):
            difference = image.shape[WIDTH] - res_width
            if 0 <= difference < closest_difference:
                closest_width = res_width
                closest_difference = difference
        # find closest smaller height
        closest_difference = large_number
        filtered_resolutions = [(width, height) for width, height in
                                self.ac.get('crop','resolutions') if
                                width == closest_width]
        for res_width, res_height in filtered_resolutions:
            difference = image.shape[HEIGHT] - res_height
            if 0 <= difference < closest_difference:
                closest_height = res_height
                closest_difference = difference
        # do nothing if no appropriate resolution found
        if (closest_width==large_number) or (closest_height==large_number):
            return image #if no resolution smaller/equal in both height/width
        border = int(round((image.shape[WIDTH] - closest_width)/2))
        top = image.shape[HEIGHT] - closest_height - border
        image_cropped = image[top:top+closest_height, border:border+closest_width]
        return image_cropped

    def _crop_to_4to3_aspect(self, image):
        extra_width = image.shape[WIDTH] - image.shape[HEIGHT]*4/3
        if extra_width <= 0:
            return image # stop if not widescreen
        cropped_width = int(round(image.shape[WIDTH] - extra_width))
        left_side = int(round((image.shape[WIDTH] - cropped_width)/2))
        image_cropped = image[0:image.shape[HEIGHT], left_side:left_side+cropped_width]
        return image_cropped

    def _shrink_to_height(self, image):
        if image.shape[HEIGHT] <= self.shrink_to_height: return image
        new_height = self.shrink_to_height
        new_width = int(round(new_height * image.shape[WIDTH] / image.shape[HEIGHT]))
        result = cv.resize(image, (new_height, new_width), interpolation=cv.INTER_AREA)

        cv.namedWindow("asdf")
        cv.imshow("asdf", result)
        cv.waitKey(0)
        cv.destroyAllWindows()

        return result

def _debug_display(image, title = 'debug display'):
    if (image.shape[WIDTH] == 1) and (image.shape[HEIGHT] == 1):
        pixel = image[0, 0]
        image = np.zeros((100, 100, 3), np.uint8)
        image.fill(pixel)

    cv.namedWindow(title)
    cv.moveWindow(title, 0, 0)
    cv.imshow(title, image)
    cv.waitKey()
    cv.destroyWindow(title)

def main():
    pass

if __name__ == '__main__':
    main()

max_stars_repo_name: kobejohn/BookwormUtility

max_issues_repo_name: kobejohn/BookwormUtility

max_forks_repo_name: kobejohn/BookwormUtility


text:
#================================================
# Authors:
#		Rodrigo Souza
#		<rsouza01@gmail.com>
#       2015-06-07
#================================================

import numpy as np
import scipy.special as scps

import Atomic_Constants as ac
import scipy
import math
from enum import Enum

from scipy.optimize import fsolve

fm = 5.07e-3

#================================================
# QCD
#================================================
'''
Here come the nucleons.
'''
class Nucleons(Enum):
    proton = 0
    neutron = 1

nucleons_masses = np.array([938, 939])

'''
Quarks are ordered here from lower to higher masses.
'''
class Quarks(Enum):
    up = 0
    down = 1
    strange = 2
    charm = 3
    bottom = 4
    top = 5

"""
Quarks masses
Source: https://en.wikipedia.org/wiki/Quark :-(
"""
quark_masses = np.array([5, 7, 150, 1290, 4200, 173000]) * fm

quark_fermi_momenta = np.array([1, 1, 1, 1, 1, 1])

class Leptons(Enum):
    electron = 0
    muon = 1
    tau = 2
    electron_neutrino = 3
    muon_neutrino = 4
    tau_neutrino = 5

"""
Lepton masses.
Source: https://en.wikipedia.org/wiki/Lepton :-(
"""
lepton_masses = np.array([0.5, 106, 1777, 0, 0.17, 15.5]) * fm

#================================================
#Constants
#================================================

qcd_coupling_g = 1

B_QCD = 1

dynamic_gluon_mass = 1

eos_quark_masses = quark_masses[3:]

"""
Quark degeneracy factor
"""
gamma_Q = 6.
gamma_E = 2.

m_parameter = 1

electron_mass = lepton_masses[Leptons.electron.value]

#================================================
#FUNCTIONS
#================================================

def mu(K_F_i, m_i):
	return np.sqrt(K_F_i**2. + m_i ** 2.)

def println(message):
	print message

"""
Energy
"""

def eos_energy(rho, dynamic_gluon_mass, particles_momenta):

    # particles_momenta = [k_u, k_d, k_s, k_e]
    quarks_momenta = np.asarray(particles_momenta[:3])      # [k_u, k_d, k_s]
    electron_momentum = np.asarray(particles_momenta[-1])   # k_e

    varepsilon = (27. * qcd_coupling_g ** 2. / (16 * dynamic_gluon_mass ** 2.)) * rho ** 2. + \
                 B_QCD + \
                 energy_quarks(rho, quarks_momenta) + \
                 energy_electrons(rho, electron_momentum)  # last item

    return varepsilon

def energy_quarks(rho, quark_fermi_momenta):

    mu_quarks = mu(quark_fermi_momenta, eos_quark_masses)

    varepsilon = 3 * gamma_Q / (2. * math.pi ** 2.) * \
                 np.sum(
                     quark_fermi_momenta ** 3. * mu_quarks / 4. +
                     eos_quark_masses ** 2. * quark_fermi_momenta * mu_quarks / 8. -
                     eos_quark_masses ** 4. / 8. * np.log(quark_fermi_momenta + mu_quarks) +
                     (eos_quark_masses ** 4. / 16.) * np.log(eos_quark_masses ** 2.))

    return varepsilon

def energy_electrons(rho, electron_momentum):
    mu_electron = mu(electron_momentum, electron_mass)

    varepsilon = gamma_Q / (2. * math.pi ** 2.) * \
                 (
                     electron_momentum ** 3. * mu_electron / 4. +
                     electron_mass ** 2. * electron_momentum * mu_electron / 8. -
                     electron_mass ** 4. / 8. * np.log(electron_momentum + mu_electron) +
                     (electron_mass ** 4. / 16.) * np.log(electron_mass ** 2.))

    return varepsilon

"""
Pressure, parameterized by the fermi momenta.
"""
def pressure_q(rho, dynamic_gluon_mass, particles_momenta):

    # particles_momenta = [k_u, k_d, k_s, k_e]
    quarks_momenta = np.asarray(particles_momenta[:3])      # [k_u, k_d, k_s]
    electron_momentum = np.asarray(particles_momenta[-1])   # k_e

    mu_quarks = mu(quark_fermi_momenta, eos_quark_masses)
    mu_electron = mu(electron_momentum, electron_mass)

    pressure = (27. * qcd_coupling_g ** 2. / (16 * dynamic_gluon_mass ** 2.)) * rho ** 2. - \
                 B_QCD + \
                 gamma_Q / (2. * math.pi ** 2.) * \
                 np.sum(
                     quark_fermi_momenta ** 3. * mu_quarks / 4. -
                     3.*eos_quark_masses ** 2. * quark_fermi_momenta * mu_quarks / 8. + \
                     3.*eos_quark_masses ** 4. / 8. * np.log(quark_fermi_momenta + mu_quarks) - \
                     (3.*eos_quark_masses ** 4. / 16.) * np.log(eos_quark_masses ** 2.)) + \
                 gamma_Q / (6. * math.pi ** 2.) * \
                 ( \
                     electron_momentum ** 3. * mu_electron / 4. - \
                     3.*electron_mass ** 2. * electron_momentum * mu_electron / 8. + \
                     3.*electron_mass ** 4. / 8. * np.log(electron_momentum + mu_electron) - \
                     (3.*electron_mass ** 4. / 16.) * np.log(electron_mass ** 2.))

    return pressure

def quarks_momenta(p, parameters):

    # Variaveis
    ku, kd, ks, ke = p

    # Parametros
    rho, mu, md, ms, me = parameters

    # Lista de
    return (
        ku**3 + kd**3 + ks**3 - 3*math.pi**2 * rho,
        2*ku**3 - kd**3 - ks**3 - ke**3,
        kd**2 + md**2 -  ks**2 - ms**2,
        (ku**2 + mu**2)**(.5)  + (ke**2 + me**2)**(.5) - (ks**2 + ms**2)**(.5)
    )

number_output = 6
output_formaters = "{: >20} "*number_output

def print_program_header():
    println ("="*125)
    println (" "* 45 + " MFTQCD EOS file generator")
    println ("="*125)

def print_header():

    header = ["rho", "ku", "kd", "ks", "ke", "bag"]

    separator = "="*20
    separators = [separator] * len(header)

    print(output_formaters.format(*separators))
    print(output_formaters.format(*header))
    print(output_formaters.format(*separators))

def print_footer():
    print_header()

#================================================
#FUNCTION MAIN
#================================================

def fFuncMain():
    print_program_header()
    print_header()

    for rho in np.linspace(0.15, 0.6, 10):
        fsolve_parameters = [rho,
                             eos_quark_masses[Quarks.up.value],
                             eos_quark_masses[Quarks.down.value],
                             eos_quark_masses[Quarks.strange.value],
                             lepton_masses[Leptons.electron.value]]

        # ku, kd, ks, ke = fsolve(quarks_momenta, (1, 1, 1, 1), fsolve_parameters).tolist()
        # print rho, ku, kd, ks, ke

        k_solution = fsolve(quarks_momenta, (1, 1, 1, .1), fsolve_parameters).tolist()

        energy = eos_energy(rho, 1, k_solution)
        print energy

        # bag_constant = (((nucleons_masses[Nucleons.neutron.value]*rho)-(eq + ee - pq - pe)) /2);
        bag_constant = 1

        i_line = ([rho] + k_solution + [bag_constant])

        number_output = 6
        output_formaters = "{: >20} " * number_output
        print(output_formaters.format(*i_line))

    print_footer()

reason: import numpy,import scipy,from scipy


text:
import os
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, GridSearchCV, cross_val_score
from datasets import Datasets
from skopt.learning import GaussianProcessRegressor
from skopt import gp_minimize
from skopt.space import Real, Integer, Categorical
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
import joblib
from sklearn import metrics
from sklearn import svm
from skopt import BayesSearchCV
from sklearn.svm import LinearSVC, SVC
from sklearn.pipeline import Pipeline
from hyperopt import Trials, STATUS_OK, tpe, hp, fmin, space_eval, tpe
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
import autosklearn.classification
from sklearn.neural_network import MLPClassifier
from functools import partial

# squared exponential kernel
from sklearn.gaussian_process.kernels import RBF
from skopt.utils import use_named_args

# https://github.com/scikit-optimize/scikit-optimize/issues/978
def bayes_search_CV_init(
    self,
    estimator,
    search_spaces,
    optimizer_kwargs=None,
    n_iter=50,
    scoring=None,
    fit_params=None,
    n_jobs=1,
    n_points=1,
    iid=True,
    refit=True,
    cv=None,
    verbose=0,
    pre_dispatch="2*n_jobs",
    random_state=None,
    error_score="raise",
    return_train_score=False,
):

    self.search_spaces = search_spaces
    self.n_iter = n_iter
    self.n_points = n_points
    self.random_state = random_state
    self.optimizer_kwargs = optimizer_kwargs
    self._check_search_space(self.search_spaces)
    self.fit_params = fit_params

    super(BayesSearchCV, self).__init__(
        estimator=estimator,
        scoring=scoring,
        n_jobs=n_jobs,
        refit=refit,
        cv=cv,
        verbose=verbose,
        pre_dispatch=pre_dispatch,
        error_score=error_score,
        return_train_score=return_train_score,
    )

BayesSearchCV.__init__ = bayes_search_CV_init

class Model:

    """initializes models, dicts"""

    def __init__(self, df):
        self.df = df
        self.best_score = 0
        self.run = self.train(df)
        self.best_accuracy
        self.best_model
        self.best_model_name
        self.best_param

    def add_model(self, model):
        """Adds models to the list"""
        self.models.append(model)

    def _GradientBoosting(self, df):
        print("Start GradientBoosting...")
        param_grid = [
            Integer(10, 120, name="n_estimators"),
            Real(0, 0.999, name="min_samples_split"),
            Integer(1, 5, name="max_depth"),
            Categorical(["deviance", "exponential"], name="loss"),
        ]

        # set up the gradient boosting classifier

        gbm = GradientBoostingClassifier(random_state=0)
        kernel = 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))

        gpr = GaussianProcessRegressor(
            kernel=kernel, normalize_y=True, noise="gaussian", n_restarts_optimizer=2
        )
        # the decorator allows our objective function to receive the parameters as

        @use_named_args(param_grid)
        def objective(**params):

            # model with new parameters
            gbm.set_params(**params)

            # optimization function (hyperparam response function)
            value = np.mean(
                cross_val_score(
                    gbm,
                    df.feature_train,
                    df.target_train,
                    cv=5,
                    n_jobs=-4,
                    scoring="roc_auc",
                )  # "accuracy"
            )

            # negate because we need to minimize
            return -value

        gp_ = gp_minimize(
            objective,
            dimensions=param_grid,
            base_estimator=gpr,
            n_initial_points=5,
            acq_optimizer="sampling",
            random_state=42,
        )
        params = {
            "n_estimators": gp_.x[0],
            "min_samples_split": gp_.x[1],
            "max_depth": gp_.x[2],
            "loss": gp_.x[3],
        }
        final_model = GradientBoostingClassifier(
            n_estimators=gp_.x[0],
            min_samples_split=gp_.x[1],
            max_depth=gp_.x[2],
            loss=gp_.x[3],
        )
        final_model.fit(df.feature_train, df.target_train)
        y_preds = final_model.predict(df.feature_test)
        joblib.dump(final_model, "./log/GradientBoosting.pkl")
        return {
            "GradientBoosting": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": params,
                "model": final_model,
            }
        }

    def _Losgistic(self, df):
        print("Start Logistic...")
        param_grid = [
            Categorical(["newton-cg", "lbfgs", "liblinear"], name="solver"),
            Categorical(["l2"], name="penalty"),
            Real(1e-5, 100, name="C"),
        ]

        # set up the logistic regressoion classifier

        lgt = LogisticRegression(random_state=0)
        kernel = 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))

        gpr = GaussianProcessRegressor(
            kernel=kernel, normalize_y=True, noise="gaussian", n_restarts_optimizer=2
        )
        # the decorator allows our objective function to receive the parameters as

        @use_named_args(param_grid)
        def objective(**params):

            # model with new parameters
            lgt.set_params(**params)

            # optimization function (hyperparam response function)
            value = np.mean(
                cross_val_score(
                    lgt,
                    df.feature_train,
                    df.target_train,
                    cv=5,
                    n_jobs=-4,
                    scoring="roc_auc",
                )  # "accuracy"
            )

            # negate because we need to minimize
            return -value

        gp_ = gp_minimize(
            objective,
            dimensions=param_grid,
            base_estimator=gpr,
            n_initial_points=5,
            acq_optimizer="sampling",
            random_state=42,
        )
        params = {
            "solver": gp_.x[0],
            "penalty": gp_.x[1],
            "C": gp_.x[2],
        }
        final_model = LogisticRegression(
            solver=gp_.x[0],
            penalty=gp_.x[1],
            C=gp_.x[2],
        )
        final_model.fit(df.feature_train, df.target_train)
        y_preds = final_model.predict(df.feature_test)
        joblib.dump(final_model, "./log/Logistic.pkl")
        return {
            "Logistic": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": params,
                "model": final_model,
            }
        }

    def _RandomForest(self, df):
        print("Start RandomForest...")
        param_grid = [
            Categorical(["sqrt", "log2", None], name="max_features"),
            Integer(120, 1200, name="n_estimators"),
            Integer(5, 30, name="max_depth"),
            Integer(2, 15, name="min_samples_split"),
            Integer(1, 10, name="min_samples_leaf"),
        ]

        # set up the logistic regressoion classifier

        rf = RandomForestClassifier(random_state=0)
        kernel = 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))

        gpr = GaussianProcessRegressor(
            kernel=kernel, normalize_y=True, noise="gaussian", n_restarts_optimizer=2
        )
        # the decorator allows our objective function to receive the parameters as

        @use_named_args(param_grid)
        def objective(**params):

            # model with new parameters
            rf.set_params(**params)

            # optimization function (hyperparam response function)
            value = np.mean(
                cross_val_score(
                    rf,
                    df.feature_train,
                    df.target_train,
                    cv=5,
                    n_jobs=-4,
                    scoring="roc_auc",
                )  # "accuracy"
            )

            # negate because we need to minimize
            return -value

        gp_ = gp_minimize(
            objective,
            dimensions=param_grid,
            base_estimator=gpr,
            n_initial_points=5,
            acq_optimizer="sampling",
            random_state=42,
        )
        params = {
            "max_features": gp_.x[0],
            "n_estimators": gp_.x[1],
            "max_depth": gp_.x[2],
            "min_samples_split": gp_.x[3],
            "min_samples_leaf": gp_.x[4],
        }
        final_model = RandomForestClassifier(
            max_features=gp_.x[0],
            n_estimators=gp_.x[1],
            max_depth=gp_.x[2],
            min_samples_split=gp_.x[3],
            min_samples_leaf=gp_.x[4],
        )
        final_model.fit(df.feature_train, df.target_train)
        y_preds = final_model.predict(df.feature_test)
        joblib.dump(final_model, "./log/RandomForest.pkl")
        return {
            "RandomForest": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": params,
                "model": final_model,
            }
        }

    def _SuportVector(self, df):
        """Suport vector machine using grid search"""
        print("Start SuportVector...")
        # defining parameter range
        param_grid = {
            "C": [0.1, 1, 10, 100, 1000],
            "gamma": ["auto"],
            "kernel": ["rbf", "linear", "poly"],
            "class_weight": ["balanced", None],
        }
        cross_validation = StratifiedKFold(n_splits=5, shuffle=True, random_state=10)
        sv = GridSearchCV(
            SVC(),
            param_grid,
            scoring="roc_auc",
            cv=cross_validation,
        )
        sv.fit(df.feature_train, df.target_train)
        y_preds = sv.predict(df.feature_test)
        joblib.dump(sv, "./log/SuportVector.pkl")
        return {
            "SuportVector": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": sv.get_params(),
                "model": sv,
            }
        }

    def _XGBoost(self, df):
        """Tuning hyper paramater of XGBoost using Hyperot"""
        print("Start XGBoost...")

        def objective(space):
            classifier = XGBClassifier(
                n_estimators=space["n_estimators"],
                max_depth=int(space["max_depth"]),
                learning_rate=space["learning_rate"],
                gamma=space["gamma"],
                min_child_weight=space["min_child_weight"],
                subsample=space["subsample"],
                colsample_bytree=space["colsample_bytree"],
            )

            classifier.fit(df.feature_train, df.target_train)
            # Applying k-Fold Cross Validation

            accuracies = cross_val_score(
                estimator=classifier, X=df.feature_train, y=df.target_train, cv=5
            )
            CrossValMean = accuracies.mean()

            return {"loss": 1 - CrossValMean, "status": STATUS_OK}

        # space of parameters
        space = {
            "max_depth": hp.choice("max_depth", range(5, 30, 1)),
            "learning_rate": hp.quniform("learning_rate", 0.01, 0.5, 0.01),
            "n_estimators": hp.choice("n_estimators", range(20, 205, 5)),
            "gamma": hp.quniform("gamma", 0, 0.50, 0.01),
            "min_child_weight": hp.quniform("min_child_weight", 1, 10, 1),
            "subsample": hp.quniform("subsample", 0.1, 1, 0.01),
            "colsample_bytree": hp.quniform("colsample_bytree", 0.1, 1.0, 0.01),
        }
        # find the best
        trials = Trials()
        best = fmin(
            fn=objective, space=space, algo=tpe.suggest, max_evals=50, trials=trials
        )
        xbg = XGBClassifier(
            n_estimators=best["n_estimators"],
            max_depth=best["max_depth"],
            learning_rate=best["learning_rate"],
            gamma=best["gamma"],
            min_child_weight=best["min_child_weight"],
            subsample=best["subsample"],
            colsample_bytree=best["colsample_bytree"],
        )
        xbg.fit(df.feature_train, df.target_train)
        y_preds = xbg.predict(df.feature_test)
        joblib.dump(xbg, "./log/XGBoost.pkl")
        return {
            "XGBoost": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": best,
                "model": xbg,
            }
        }

    def _NeuralNetwork(self, df):
        print("Start NeuralNetwork...")

        def objective(params, model, x, y, k=5):
            model.set_params(**params)
            kfold = StratifiedKFold(n_splits=k)
            score = cross_val_score(
                model, df.feature_train, df.target_train, cv=kfold, scoring="accuracy"
            )
            return 1 - score.mean()

        def optimize_params(model, x, y, space, k=5, max_evals=100, eval_space=False):
            trials = Trials()
            best = fmin(
                partial(objective, model=model, x=x, y=y, k=k),
                space,
                algo=tpe.suggest,
                max_evals=max_evals,
                trials=trials,
            )

            param_values = [t["misc"]["vals"] for t in trials.trials]
            param_values = [
                {key: value for key in params for value in params[key]}
                for params in param_values
            ]

            if eval_space:
                param_values = [space_eval(space, params) for params in param_values]

            param_df = pd.DataFrame(param_values)
            param_df["accuracy"] = [1 - loss for loss in trials.losses()]
            return space_eval(space, best), param_df

        space_mlp = {}
        space_mlp["hidden_layer_sizes"] = 10 + hp.randint("hidden_layer_sizes", 40)
        space_mlp["alpha"] = hp.loguniform("alpha", -8 * np.log(10), 3 * np.log(10))
        space_mlp["activation"] = hp.choice("activation", ["relu", "logistic", "tanh"])
        space_mlp["solver"] = hp.choice("solver", ["lbfgs", "sgd", "adam"])
        model = MLPClassifier()
        best_mlp, param_mlp = optimize_params(
            model, df.feature_train, df.target_train, space_mlp, max_evals=100
        )
        mlp = MLPClassifier(
            hidden_layer_sizes=best_mlp["hidden_layer_sizes"],
            alpha=best_mlp["alpha"],
            activation=best_mlp["activation"],
            solver=best_mlp["solver"],
        )
        mlp.fit(df.feature_train, df.target_train)
        y_preds = mlp.predict(df.feature_test)
        joblib.dump(mlp, "./log/NeuralNetwork.pkl")
        return {
            "NeuralNetwork": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": best_mlp,
                "model": mlp,
            }
        }

    def _autoSK(self, df):
        print("Start AutoSK...")
        automl = autosklearn.classification.AutoSklearnClassifier(
            time_left_for_this_task=120,
            per_run_time_limit=30,
            disable_evaluator_output=False,
            resampling_strategy="cv",
            resampling_strategy_arguments={"folds": 5},
        )
        automl.fit(df.feature_train, df.target_train)
        y_preds = automl.predict(df.feature_test)
        joblib.dump(automl, "./log/AutoSK.pkl")
        return {
            "AutoSK": {
                "Best_score": metrics.roc_auc_score(y_preds, df.target_test),
                "Best_accuracy": metrics.accuracy_score(y_preds, df.target_test),
                "params": None,
                "model": automl,
            }
        }

    def _select_model(self, name, result):
        sample_auc_score = result[name]["Best_score"]
        sample_acc_score = result[name]["Best_accuracy"]
        sample_parms = result[name]["params"]
        print("Start Logging...")
        with open(os.path.join("log", f"log_{name}.txt"), "w") as f:
            f.writelines(f"AUC score : {str(sample_auc_score)}")
            f.writelines("\n")
            f.writelines(f"Accuracy score : {str(sample_acc_score)}")
            f.writelines("\n")
            f.writelines(f"params : {str(sample_parms)}")
        f.close()
        if result[name]["Best_score"] > self.best_score:
            self.best_score = result[name]["Best_score"]
            self.best_accuracy = result[name]["Best_accuracy"]
            self.best_model = result[name]["model"]
            self.best_model_name = name
            self.best_param = result[name]["params"]

    def train(self, df):
        """prints summary of models, best model"""

        model_list = [
            "AutoSK",
            "NeuralNetwork",
            "XGBoost",
            "SuportVector",
            "RandomForest",
            "Logistic",
            "GradientBoosting",
        ]
        for name in model_list:
            file = name + ".pkl"
            if not os.path.isfile(os.path.join("./log", file)):
                if name == "AutoSK":
                    self._select_model(name, self._autoSK(df))
                elif name == "NeuralNetwork":
                    self._select_model(name, self._NeuralNetwork(df))
                elif name == "XGBoost":
                    self._select_model(name, self._XGBoost(df))
                elif name == "SuportVector":
                    self._select_model(name, self._SuportVector(df))
                elif name == "RandomForest":
                    self._select_model(name, self._RandomForest(df))
                elif name == "Logistic":
                    self._select_model(name, self._Losgistic(df))
                elif name == "GradientBoosting":
                    self._select_model(name, self._GradientBoosting(df))
                else:
                    print("Model is not support yet")
            else:
                model_ = joblib.load(os.path.join("./log", file))
                y_preds = model_.predict(df.feature_test)
                model_score = (metrics.roc_auc_score(y_preds, df.target_test),)
                model_accuracy = metrics.accuracy_score(y_preds, df.target_test)
                if model_score > self.best_score:
                    self.best_score = model_score
                    self.best_accuracy = model_accuracy
                    self.best_model = model_
                    self.best_model_name = name
                    self.best_param = None
        print(f"Best model is : {self.best_model_name}")
        print(f"Best AUC score is : {self.best_score}")
        print(f"Best accuracy score is : {self.best_accuracy}")
        print(f"Best params are : {self.best_param}")
        print("Saving...best model")
        joblib.dump(self.best_model, f"./log/best_model/{self.best_model_name}.pkl")

if __name__ == "__main__":
    # define input files
    data_file = "/home/giangdip/Giangdip/HUS/Python/Project_Final/framingham.csv"
    numeric_var = [
        "age",
        "cigsPerDay",
        "totChol",
        "sysBP",
        "diaBP",
        "BMI",
        "heartRate",
        "glucose",
    ]
    level_var = ["education"]
    category_var = [
        "male",
        "currentSmoker",
        "BPMeds",
        "prevalentStroke",
        "prevalentHyp",
        "diabetes",
    ]
    target = ["TenYearCHD"]

    # Create Data object
    data = Datasets(
        data_file=data_file,
        cat_cols=category_var,
        num_cols=numeric_var,
        level_cols=level_var,
        label_col=target,
        train=True,
    )

    X_train = data.feature_train
    y_train = data.target_train

    X_test = data.feature_test
    y_test = data.feature_test
    # training data
    models = Model(data)


text:
from collections import Counter

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px

class Unsupervised(object):
    def __init__(self, count_matrix, metadata, outdir):
        self.count_matrix = count_matrix
        self.metadata = metadata
        self.outdir = outdir

    def pca(self):
        from sklearn.decomposition import PCA
        from sklearn.preprocessing import StandardScaler
        from mpl_toolkits.mplot3d import Axes3D

        df = self.count_matrix
        x = StandardScaler().fit_transform(df)
        clf = PCA(n_components=3)
        principal_components = clf.fit_transform(x)
        pc1_value, pc2_value, pc3_value = clf.explained_variance_ratio_
        pc1_label = '{:4} : {:5.2f}%'.format('PC1', pc1_value * 100)
        pc2_label = '{:4} : {:5.2f}%'.format('PC2', pc2_value * 100)
        pc3_label = '{:4} : {:5.2f}%'.format('PC3', pc3_value * 100)
        principal_df = pd.DataFrame(data=principal_components,
                                    columns=['pc1', 'pc2', 'pc3'])

        fig = plt.figure(figsize=(8, 8))
        ax = Axes3D(fig)
        ax.set_xlabel(pc1_label)
        ax.set_ylabel(pc2_label)
        ax.set_zlabel(pc3_label)
        ax.scatter3D(principal_df.loc[:, 'pc1'], principal_df.loc[:, 'pc2'], principal_df.loc[:, 'pc3'])
        plt.show()

    def kmeans(self, filename, n_clusters=2):
        from sklearn.cluster import KMeans
        from sklearn import preprocessing

        print('\nRunning K-Means analysis on {} samples and {} variables, setting {} clusters'
              .format(self.count_matrix.shape[0], self.count_matrix.shape[1], n_clusters))
        km = KMeans(init='k-means++', n_clusters=n_clusters, max_iter=1500)
        x = self.count_matrix
        x_normalized = preprocessing.maxabs_scale(x)
        x_standardized = preprocessing.StandardScaler().fit_transform(x)
        x_robust = preprocessing.RobustScaler().fit_transform(x)

        # Generate the data frame with the sample numbers and their cluster
        # In order of cluster members
        data_sets = (x, x_normalized, x_standardized, x_robust)
        set_names = ('Raw', 'Norm', 'Stand', 'Robust_S')
        # Setup the clinical data to which to add the cluster data.
        d_final = self.metadata
        for normalization, name in zip(data_sets, set_names):
            # Train and then fit the data
            y_clusters = km.fit_predict(normalization)

            # Do a count of the clusters and report for each type of normalization
            c = dict(Counter(y_clusters))
            print("Sample distribution over the %s clusters for %s data pretreatment: %s" % (n_clusters, name, c))

            # This section will add cluster labels for each sample in the metadata file
            # The first step is to import the metadata file. The Index is the sample name.
            samples = self.count_matrix.index
            dc = pd.DataFrame(zip(samples, y_clusters), columns=('Sample', name))

            # Count the number of samples in each cluster, and sort from largest to smallest cluster.
            # The grouping value becomes the index.
            cluster_summary = dc.groupby(name).count().sort_values(by="Sample", ascending=False)

            # Add a column from the index, which is now in order of cluster size.
            # This is useful as being independent of the number of clusters set
            # The original clusters are still the index
            cluster_summary['rename'] = np.sort(np.array(cluster_summary.index))

            # Build a dictionary of replacements to make, original clusters for new clusters
            # Add 1 to start counting clusters at 1 instead of 0
            rename_dictionary = dict(zip(cluster_summary.index, cluster_summary['rename']+1))
            dc[name].replace(to_replace=rename_dictionary, inplace=True)
            # Append the cluster information for each sample to the clinical parameters
            d_final = pd.merge(d_final, dc[name], left_index=True, right_index=True)
            d_final.index.name = 'sample'
        output_file = self.outdir+'/'+filename
        print("\n\tAppending KMeans clustering information to original Metadata file. Saving as %s" % output_file)
        d_final.to_csv(path_or_buf=output_file)
        print(d_final.head())

        data_clusters = px.data.gapminder()  # noqa
        fig = px  # noqa

    def gmm(self):
        from sklearn.mixture import GaussianMixture
        from sklearn.decomposition import PCA
        # x_train = self.count_matrix
        # clf = GaussianMixture(n_components=2, covariance_type='full')
        # clf.fit(x_train)
        pca = PCA(0.99)
        data = pca.fit_transform(self.count_matrix)
        n_components = np.arange(10, 500, 20)
        models = [GaussianMixture(n, covariance_type='full', random_state=0)
                  for n in n_components]
        aics = [model.fit(data).aic(data) for model in models]
        plt.plot(n_components, aics)
        plt.show()

        # print(gmm.means_)
        # print('\n')
        # print(gmm.covariances_)

max_stars_repo_name: motleystate/moonstone

max_issues_repo_name: motleystate/moonstone

max_forks_repo_name: motleystate/moonstone


text:
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import seaborn as sns

def main():
    sns.set_style("whitegrid")
    # plt.style.use('seaborn-whitegrid')
    pd.set_option("display.max_rows", None)

    df = pd.read_csv(sys.argv[1], names=["Method", "N", "Time"])
    des = df.groupby(["Method", "N"]).describe()
    print(des)

    fig = plt.figure(figsize=(8, 6), dpi=100)
    ax = plt.axes()
    ax.set_title("Finishing Time of Dining Philosophers")
    ax.margins(x=0)
    ax.xaxis.set_major_locator(plticker.MultipleLocator(base=1))
    ax.yaxis.set_major_locator(plticker.MultipleLocator(base=10))
    colors = {"Ordered": "#71c040", "SmartAndPolite":"#fd4d1b"}
    for method in des.index.unique(level="Method"):
        d = des.loc[method]
        x = d.index
        color = colors[method]
        ax.plot(x, d[("Time", "mean")], label=f"{method}", color=color)
        ax.fill_between(x, d[("Time", "25%")], d[("Time", "75%")], label=f"{method} Mid 50%-ile", alpha=0.2, color=color)
        ax.fill_between(x, d[("Time", "min")], d[("Time", "max")], label=f"{method} Min to Max", alpha=0.07, color=color)
        ax.set_ylabel("Seconds")
        ax.set_xlabel("Number of Philosophers")
    idealX = range(2, 33)
    idealY = [10 * x / ((x - x % 2) / 2) for x in idealX]
    ax.plot(idealX, idealY, label="Theoretical Minimum", color="#53acfc")
    ax.set_ylim(ymin=0)
    fig.tight_layout()
    plt.legend(loc="upper left")
    plt.show()

if __name__ == "__main__":
    main()

max_stars_repo_name: keithyipkw/InSync

max_issues_repo_name: keithyipkw/InSync

max_forks_repo_name: keithyipkw/InSync


text:
import numpy as np
from scipy import stats
from pm4py.algo.filtering.log.end_activities import end_activities_filter

def end_activities(log):
    log_end = end_activities_filter.get_end_activities(log)

    n_unique_end_activities = len(log_end)

    end_activities_occurrences = list(log_end.values())
    end_activities_min = np.min(end_activities_occurrences)
    end_activities_max = np.max(end_activities_occurrences)
    end_activities_mean = np.mean(end_activities_occurrences)
    end_activities_median = np.median(end_activities_occurrences)
    end_activities_std = np.std(end_activities_occurrences)
    end_activities_variance = np.var(end_activities_occurrences)
    end_activities_q1 = np.percentile(end_activities_occurrences, 25)
    end_activities_q3 = np.percentile(end_activities_occurrences, 75)
    end_activities_iqr = stats.iqr(end_activities_occurrences)
    end_activities_skewness = stats.skew(end_activities_occurrences)
    end_activities_kurtosis = stats.kurtosis(end_activities_occurrences)

    return [
        n_unique_end_activities,
        end_activities_min,
        end_activities_max,
        end_activities_mean,
        end_activities_median,
        end_activities_std,
        end_activities_variance,
        end_activities_q1,
        end_activities_q3,
        end_activities_iqr,
        end_activities_skewness,
        end_activities_kurtosis,
    ]

reason: import numpy,from scipy


text:
import numpy.core.multiarray
from fmod import fmod

print (fmod.fib (8))


text:
"""
==========
SPHERE CVT
==========

Iteratively approximated centroidal Voronoi tessellation (CVT) on the unit
sphere. It samples N points uniformly on the unit sphere.
This is an implmentation of the algorithm described in
https://people.sc.fsu.edu/~jburkardt/m_src/sphere_cvt/sphere_cvt.html

by Zishun Liu <liuzishun@gmail.com>
Feb 13, 2021
"""

import numpy as np
from scipy.spatial import SphericalVoronoi

__all__ = ['sphere_cvt']

def sphere_cvt(n, iters=100):
    """
    Iteratively approximated centroidal Voronoi tessellation (CVT) on the unit
    sphere.

    Parameters
    ----------
    n : int
        # points
    iters : int
        # iterations
    Returns
    -------
    verts : m x 3 array (float)
        Voronoi region centers
    faces : n x 3 array (int)
        Delaunay triangulation of verts.
    """
    scvt = SphereCVT(n, iters)
    verts = scvt.get_points()
    faces = scvt.get_faces()
    return verts, faces

class SphereCVT:
    def __init__(self, n, iters=100):
        self.num = n
        self.points = self.uniform(n)
        for i in range(iters):
            sv = SphericalVoronoi(self.points)
            for j in range(len(sv.regions)):
                region = sv.regions[j]
                p = np.mean(sv.vertices[region, :], axis=0)
                p /= np.linalg.norm(p)
                self.points[j, :] = p[:]

            print('%d/%d\r' % (i+1, iters), end='')
        print()  # close \r
        self.sv = sv

    def uniform(self, num):
        vec = np.random.randn(3, num)
        vec /= np.linalg.norm(vec, axis=0)
        return vec.T

    def get_points(self):
        return self.points

    def get_faces(self):
        num_v = np.max([np.max(x) for x in self.sv.regions])+1
        faces = np.zeros((num_v, 3), dtype=int)
        idx = np.zeros((num_v,), dtype=int)
        for i in range(len(self.sv.regions)):
            for v in self.sv.regions[i]:
                faces[v, idx[v]] = i
                idx[v] += 1
        # re-orient
        for i in range(faces.shape[0]):
            v0 = self.points[faces[i, 0], :]
            e1 = self.points[faces[i, 1], :] - v0
            e2 = self.points[faces[i, 2], :] - v0
            n = np.cross(e1, e2)
            if np.dot(v0, n) < 0:
                faces[i, 1], faces[i, 2] = faces[i, 2], faces[i, 1]
        return faces

if __name__ == '__main__':
    pass

max_stars_repo_name: zishun/MeshUtility

max_issues_repo_name: zishun/MeshUtility

max_forks_repo_name: zishun/MeshUtility


text:
#!/usr/bin/env python
from nose.tools import *
import networkx as nx
from networkx.algorithms import isomorphism as iso

class TestIsomorph:

    def setUp(self):
        self.G1=nx.Graph()
        self.G2=nx.Graph()
        self.G3=nx.Graph()
        self.G4=nx.Graph()
        self.G1.add_edges_from([ [1,2],[1,3],[1,5],[2,3] ])
        self.G2.add_edges_from([ [10,20],[20,30],[10,30],[10,50] ])
        self.G3.add_edges_from([ [1,2],[1,3],[1,5],[2,5] ])
        self.G4.add_edges_from([ [1,2],[1,3],[1,5],[2,4] ])

    def test_could_be_isomorphic(self):
       assert_true(iso.could_be_isomorphic(self.G1,self.G2))
       assert_true(iso.could_be_isomorphic(self.G1,self.G3))
       assert_false(iso.could_be_isomorphic(self.G1,self.G4))
       assert_true(iso.could_be_isomorphic(self.G3,self.G2))

    def test_fast_could_be_isomorphic(self):
        assert_true(iso.fast_could_be_isomorphic(self.G3,self.G2))

    def test_faster_could_be_isomorphic(self):
        assert_true(iso.faster_could_be_isomorphic(self.G3,self.G2))

    def test_is_isomorphic(self):
        assert_true(iso.is_isomorphic(self.G1,self.G2))
        assert_false(iso.is_isomorphic(self.G1,self.G4))

max_stars_repo_name: nishaero/wifi-userseg-ryu

max_issues_repo_name: nishaero/wifi-userseg-ryu

max_forks_repo_name: nishaero/wifi-userseg-ryu

reason: import networkx,from networkx


text:
__author__ = "David Camhy, Markus Pichler"
__copyright__ = "Copyright 2017, University of Technology Graz"
__credits__ = ["David Camhy", "Markus Pichler"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "David Camhy, Markus Pichler"

import codecs
import json

import requests
from io import BytesIO, TextIOWrapper, IOBase, StringIO
from zipfile import ZipFile
from os import path
import pandas as pd
from pandas.errors import ParserError

from .sww_utils import guess_freq

import warnings
import numba

warnings.filterwarnings("ignore", category=numba.NumbaDeprecationWarning)

def csv_args(unix=False):
    """
    how to read and write csv file
    differs between windows and unix (mac+linux)

    Args:
        unix (bool): if it is a unix computer

    Returns:
        dict: arguments to read and write csv files
    """
    if unix:
        return dict(sep=',', decimal='.')
    else:
        return dict(sep=';', decimal=',')

def check_path(pth=None):
    """
    use the local directory if not path is give

    Args:
        pth (str): path to directory

    Returns:
        str: path to directory
    """
    if pth == '':
        return pth
    elif pth is None:
        return ''
    elif path.isdir(pth):
        return pth
    else:
        raise UserWarning('Path is not available')

def export_series(series, filename, export_path=None, save_as='csv', unix=False):
    """
    export the series to a given format
    may be extended

    Args:
        series (pandas.Series):
        filename (str): name of the file
        export_path (str): path where the file will be stored.
        save_as (str): export format
        unix (bool): whether to use "," or ";" for the csv

    Returns:
        str: path to created file
    """
    fn = path.join(check_path(export_path), '{}.{}'.format(filename, save_as))

    if save_as == 'csv':
        series.to_csv(fn, **csv_args(unix))

    elif save_as == 'parquet':
        series.to_frame().to_parquet(fn, compression=None)

    else:
        raise NotImplementedError('Sorry, but only csv files are implemented. Maybe there will be more options soon.')

    return fn

def import_series(filename, series_label='precipitation', index_label='datetime', unix=False):
    """

    Args:
        filename (str):
        series_label (str):
        index_label (str):
        unix (bool): whether to use "," or ";" for the csv

    Returns:
        pandas.Series: rain series
    """
    if filename.endswith('csv'):
        try:
            ts = pd.read_csv(filename, index_col=0, header=0, squeeze=True, **csv_args(unix))
            ts.index = pd.to_datetime(ts.index)
            ts.name = series_label
            ts.index.name = index_label
            return ts
        except (ParserError, UnicodeDecodeError):
            return _parse(filename)
    elif filename.endswith('parquet'):
        return pd.read_parquet(filename).iloc[:, 0].asfreq('T').copy()
    else:
        raise NotImplementedError('Sorry, but only csv files are implemented. Maybe there will be more options soon.')

ehyd_stations = json.load(open(path.join(path.dirname(__file__), 'ehyd_stations.json'), 'r', encoding='utf-8'))

def _get_file(id_):
    """
    get the file of the series of the station

    Args:
        id_ (int): Gitterpunktnummer (id number) of the station

    Returns:
        TextIOWrapper | IOBase:
    """
    url = 'https://ehyd.gv.at/eHYD/MessstellenExtraData/nlv?id={id}&file=2'.format(id=id_)
    r = requests.get(url, allow_redirects=True)
    c = r.content
    if c != b'':
        z = ZipFile(BytesIO(c))
        filename = z.namelist()[0]
        csv_file = TextIOWrapper(z.open(filename), encoding='iso8859')
        return csv_file

def _read_file(filepath_or_buffer):
    """
    read the file

    Args:
        filepath_or_buffer (IOBase | str): file of the series or path to the local file

    Returns:
        IOBase: file of the series
    """
    if isinstance(filepath_or_buffer, str):
        return codecs.open(filepath_or_buffer, 'r', encoding='iso8859')
    elif isinstance(filepath_or_buffer, IOBase):
        return filepath_or_buffer
    else:
        raise NotImplementedError()

def _split_file(file):
    """
    split the file in meta-data and series-data

    Args:
        file (IOBase): file of the series

    Returns:
        (list, list): meta-data and time-series-data
    """
    lines = file.readlines()
    file.close()

    items = {':': 2,
             '.': 2,
             ';': 1,
             ',': 1}
    i = 0
    for i, line in enumerate(lines):
        if all([line.count(s) == n for s, n in items.items()]):
            break

    meta = lines[:i]
    data = lines[i:]
    return meta, data

def parse_ehyd_file(filepath_or_buffer, series_label='precipitation', index_label='datetime'):
    """

    Args:
        filepath_or_buffer (IOBase | str):
        series_label (str):
        index_label (str):

    Returns:
        (list, pandas.Series): meta-data and time-series
    """
    csv_file = _read_file(filepath_or_buffer)

    # ___________________________
    meta, data = _split_file(csv_file)

    # ___________________________
    ts = pd.read_csv(StringIO('\n'.join(data).replace(' ', '')), sep=';', decimal=',', index_col=0, na_values='Lücke',
                     header=None, squeeze=True, names=[series_label],
                     date_parser=lambda s: pd.to_datetime(s, format='%d.%m.%Y%H:%M:%S')
                     )

    # ___________________________
    ts.index.name = index_label
    # ts = ts.rename_axis(index_label, axis='index')
    return meta, ts

def _parse(filepath_or_buffer, series_label='precipitation', index_label='datetime', with_meta=False):
    """

    Args:
        filepath_or_buffer (IOBase | str):
        series_label (str):
        index_label (str):
        with_meta (bool): whether to return meta data or not

    Returns:
        pandas.Series | (pandas.Series, pandas.Series):
    """
    meta, ts = parse_ehyd_file(filepath_or_buffer, series_label=series_label, index_label=index_label)

    freq = guess_freq(ts.index)
    ts = ts.resample(freq).ffill()

    # ___________________________
    if with_meta:
        # meta = '\n'.join(meta)
        meta = pd.Series(meta).str.replace('\n', '').str.split(';', expand=True).fillna('').apply(
            lambda x: x.str.strip())
        return ts, meta

    else:
        return ts

def get_station(id_):
    """
    get the name of the station based on the id number

    Args:
        id_ (int): number

    Returns:
        str: name of the station
    """
    return ehyd_stations[id_]

def get_all_stations():
    """
    print all station names and ids
    """
    for id, location in ehyd_stations:
        print(id, ':', location)

def get_series(id_, with_meta=False):
    """
    get the series from the ehyd platform

    Args:
        id_ (int): number
        with_meta (bool): whether to return meta data or not

    Returns:
        pandas.Series | list[pandas.Series, str]: mata or data with the meta-data
    """
    if id_ in ehyd_stations:
        print('You choose the station: "{}" with the id: "{}".'.format(get_station(id_), id_))
    return _parse(_get_file(id_), with_meta=with_meta)

def get_station_meta(id_):
    """
    get the station meta data

    Args:
        id_ (int):

    Returns:
        str:
    """
    url = 'https://ehyd.gv.at/eHYD/MessstellenExtraData/nlv?id={id}&file=1'.format(id=id_)
    r = requests.get(url, allow_redirects=True)
    c = r.content
    if c != b'':
        file = TextIOWrapper(BytesIO(c), encoding='iso8859')
        return file.read()

# if __name__ == '__main__':
# print(pd.Series(ehyd_stations).to_string())
# import time
# NOW = time.time()
# print('{:0.0f}'.format(time.time() - NOW))
# print(get_series(105445))
# print('{:0.0f}'.format(time.time() - NOW))

reason: import numba


text:
import torch
import numpy as np
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.optim import SGD
from torch.optim.optimizer import required
from gradient_color import linear_gradient
import argparse

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser()
parser.add_argument('--learning_rate_idx', type=int, default=0)
parser.add_argument('--epochs', type=int, default=5000000)
args = parser.parse_args()
update_rule = 'sgd'
sgd_sim = False
epochs = args.epochs
starting_phi = 0.5
starting_theta = 0.5
start_to_nash_distance = starting_phi ** 2 + starting_theta ** 2
grid_length = 2
momentum = -0.5
grad_scale = 15
learning_rate_idx = args.learning_rate_idx

epsilon = 0.001
lr_N = 4
mom_N = 4
max_distance = 10

# lr_list = ([1 * 10**(lr) for lr in np.linspace(-4, 0.1, lr_N)])
lr_list = [0.1, 0.001, 0.0001]
lr_list.sort()
learning_rate = lr_list[learning_rate_idx]
# mom_list = np.arange(-1.2, 0.1, 1.3 / mom_N)
mom_list = [-0.9, -0.5, -0.1, 0, 0.1, 0.5, 0.9]

class SGDNM(SGD):
    def __init__(self, params, lr=required, momentum=0, dampening=0,
                 weight_decay=0, nesterov=False):
        if not 0.0 <= lr:
            raise ValueError("Invalid learning rate: {}".format(lr))
        if not 0.0 <= weight_decay:
            raise ValueError(
                "Invalid weight_decay value: {}".format(weight_decay))

        defaults = dict(lr=lr, momentum=momentum, dampening=dampening,
                        weight_decay=weight_decay, nesterov=nesterov)
        if nesterov and (momentum <= 0 or dampening != 0):
            raise ValueError(
                "Nesterov momentum requires a momentum and zero dampening")
        super(SGD, self).__init__(params, defaults)

def plot_fig(dis_grad_list, gen_grad_list):
    plt.figure()
    plt.scatter(
        dis_grad_list, gen_grad_list,
        color=linear_gradient('#445ACC', '#b30000', epochs + 1), s=1)
    my_mesh = np.array(
        np.meshgrid(
            np.linspace(-grid_length, grid_length, 20),
            np.linspace(-grid_length, grid_length, 20))).reshape((2, -1)).T
    for i in range(len(my_mesh)):
        grad_x = my_mesh[i][1]
        grad_y = my_mesh[i][0]
        plt.arrow(my_mesh[i][0], my_mesh[i][1],
                  -grad_x / grad_scale, grad_y / grad_scale,
                  head_width=0.02, head_length=0.02,
                  color='gray')

    plt.xlim([-grid_length, grid_length])
    plt.ylim([-grid_length, grid_length])

    plt.savefig('figs_2/' + update_rule +
                '_' + str(sgd_sim) + '_' +
                str(learning_rate) + '_' + str(momentum) + '.png', dpi=300)

class Dis(nn.Module):
    def __init__(self):
        super(Dis, self).__init__()
        self.theta = Parameter(torch.Tensor(1))
        self.theta.data = torch.FloatTensor([starting_theta])

    def forward(self):
        return self.theta

class Gen(nn.Module):
    def __init__(self):
        super(Gen, self).__init__()
        self.phi = Parameter(torch.Tensor(1))
        self.phi.data = torch.FloatTensor([starting_phi])

    def forward(self):
        return self.phi

def train(update_rule, learning_rate, momentum, sgd_sim):
    conv_time = -2
    dis = Dis()
    gen = Gen()
    optimizer_d = SGDNM(
        dis.parameters(), lr=learning_rate, momentum=momentum)
    optimizer_g = SGDNM(
        gen.parameters(), lr=learning_rate, momentum=momentum)
    dis_grad_list = []
    gen_grad_list = []
    for i in range(epochs):
        gen_out = gen()
        dis_out = dis()
        loss_d = gen_out.detach() * dis_out
        loss_g = -gen_out * dis_out.detach()
        optimizer_d.zero_grad()
        optimizer_g.zero_grad()
        if sgd_sim:
            loss_d.backward()
            loss_g.backward()
            optimizer_d.step()
            optimizer_g.step()
        else:
            loss_d.backward()
            optimizer_d.step()
            loss_g.backward()
            optimizer_g.step()
        dis_grad_list.append(dis.theta.grad.cpu().data.numpy()[0])
        gen_grad_list.append(gen.phi.grad.cpu().data.numpy()[0])
        distance = gen.phi.data ** 2 + dis.theta.data**2
        distance = distance.cpu().numpy()[0]
        if distance < epsilon:
            conv_time = i
            break
        if distance > max_distance:
            break
    if distance > start_to_nash_distance:
        conv_time = -1
    elif distance == start_to_nash_distance:
        conv_time = - 0.5
    elif distance > epsilon:
        conv_time = 0
    plot_fig(dis_grad_list, gen_grad_list)
    return conv_time

conv_times = np.ones((mom_N)) * -1
for idx_m, momentum in enumerate(mom_list):
    print momentum
    conv_times[idx_m] = train(update_rule, learning_rate, momentum, sgd_sim)
    print conv_times[idx_m]

# np.save(("new_np_out/" +
#          update_rule + '_' + str(sgd_sim) + '_' +
#          str(learning_rate_idx)), conv_times)
# np.load("out_np/"+ update_rule + '_' + str(sgd_sim) + '_' + str(lr) + '_' + str(momentum)))

# np.save("conv_matrix_learning_rate", conv_times)
# plt.figure()
# plt.imshow(conv_times, interpolation='nearest')
# plt.savefig("out_learning_rate.png")


text:
import pytest
import xarray
import dask.array as da
from itertools import product
from collections import namedtuple
from quartical.gains.gain import gain_spec_tup
from quartical.interpolation.interpolate import (load_and_interpolate_gains,
                                                 convert_and_drop,
                                                 sort_datasets,
                                                 domain_slice,
                                                 make_concat_xds_list,
                                                 make_interp_xds_list)
import numpy as np
from copy import deepcopy

# TODO: These deliberately have enough points to work with all interpolation
# methods. Add tests for the case when we don't.

GAIN_PROPERTIES = {
    "between": ((0, 2, 2, 2, 0, 2, 2, 2), (2, 2, 2, 1, 2, 2, 2, 1)),
    "aligned": ((0, 4, 4, 1, 0, 4, 4, 1), (0, 4, 4, 1, 0, 4, 4, 1)),
    "overlap": ((0, 2, 2, 2, 0, 2, 2, 2), (1, 4, 2, 1, 1, 4, 2, 1)),
    "contain": ((0, 4, 4, 1, 0, 4, 4, 1), (1, 2, 6, 1, 1, 2, 6, 1)),
    "outside": ((2, 4, 4, 1, 2, 4, 4, 1), (0, 2, 4, 2, 0, 2, 4, 2)),
}

BOUNDS = namedtuple("BOUNDS", "min_t max_t min_f max_f")

def mock_gain_xds_list(start_time,
                       n_time,
                       gap_time,
                       n_xds_time,
                       start_freq,
                       n_freq,
                       gap_freq,
                       n_xds_freq):

    n_ant = 3
    n_dir = 1
    n_corr = 4

    _gain_xds_list = []

    for t_ind, f_ind in product(range(n_xds_time), range(n_xds_freq)):

        time_lb = start_time + t_ind*(n_time + gap_time)
        time_range = np.arange(time_lb, time_lb + n_time)

        freq_lb = start_freq + f_ind*(n_freq + gap_freq)
        freq_range = np.arange(freq_lb, freq_lb + n_freq)

        coords = {
            "gain_t": time_range,
            "gain_f": freq_range,
            "ant": np.arange(n_ant),
            "dir": np.arange(n_dir),
            "corr": np.arange(n_corr)
        }

        gains = da.zeros((n_time, n_freq, n_ant, n_dir, n_corr),
                         dtype=np.complex128)
        gains += da.array([1, 0, 0, 1])

        flags = da.zeros((n_time, n_freq, n_ant, n_dir),
                         dtype=np.int8)

        # Include a dummy data_var to check that it doesn't break anything.
        data_vars = {
            "gains": (("gain_t", "gain_f", "ant", "dir", "corr"), gains),
            "gain_flags": (("gain_t", "gain_f", "ant", "dir"), flags),
            "dummy": (("ant",), np.arange(n_ant))
        }

        attrs = {
            "GAIN_AXES": ("gain_t", "gain_f", "ant", "dir", "corr"),
            "GAIN_SPEC": gain_spec_tup((n_time,), (n_freq,), (n_ant,),
                                       (n_dir,), (n_corr,))
        }

        xds = xarray.Dataset(
            data_vars=data_vars,
            coords=coords,
            attrs=attrs
        )

        _gain_xds_list.append(xds)

    return _gain_xds_list

@pytest.fixture(scope="module")
def opts(base_opts, interp_mode, interp_method):

    # Don't overwrite base config - instead duplicate and update.

    _opts = deepcopy(base_opts)

    _opts.solver.terms = ["G", "B"]
    _opts.G.load_from = "ignored"  # Must not be None.
    _opts.G.interp_method = interp_method
    _opts.G.interp_mode = interp_mode

    return _opts

@pytest.fixture(scope="module", params=GAIN_PROPERTIES.values())
def _params(request):
    return request.param[0], request.param[1]

@pytest.fixture(scope="module")
def load_params(_params):
    return _params[0]

@pytest.fixture(scope="module")
def gain_params(_params):
    return _params[1]

@pytest.fixture(scope="module")
def gain_xds_lod(gain_params):
    return [{"G": xds, "B": xds} for xds in mock_gain_xds_list(*gain_params)]

@pytest.fixture(scope="module")
def term_xds_list(gain_xds_lod):
    return [xds_list["G"] for xds_list in gain_xds_lod]

@pytest.fixture(scope="module")
def load_xds_list(load_params):
    return mock_gain_xds_list(*load_params)

# ------------------------------convert_and_drop-------------------------------

@pytest.fixture(scope="module", params=["reim", "ampphase"])
def interp_mode(request):
    return request.param

@pytest.fixture(scope="module", params=["2dlinear",
                                        "2dspline"])
def interp_method(request):
    return request.param

@pytest.fixture(scope="module")
def converted_xds_list(load_xds_list, interp_mode):
    return convert_and_drop(load_xds_list, interp_mode)

def test_data_vars(converted_xds_list, interp_mode):
    reim_keys = {"gain_flags", "re", "im"}
    ampphase_keys = {"gain_flags", "amp", "phase"}

    expected_keys = reim_keys if interp_mode == "reim" else ampphase_keys

    assert all([set(xds.keys()) ^ expected_keys == set()
                for xds in converted_xds_list])

# --------------------------------sort_datasets--------------------------------

@pytest.fixture(scope="module")
def sorted_xds_lol(converted_xds_list):
    return sort_datasets(converted_xds_list)

def test_time_grouping(sorted_xds_lol):
    assert all([len(set(xds.gain_t.values[0] for xds in xds_list)) == 1
                for xds_list in sorted_xds_lol])

def test_time_ordering(sorted_xds_lol):
    times = [xds_list[0].gain_t.values[0] for xds_list in sorted_xds_lol]
    assert sorted(times) == times

def test_freq_ordering(sorted_xds_lol):
    for xds_list in sorted_xds_lol:
        freqs = [xds.gain_f.values[0] for xds in xds_list]
        assert sorted(freqs) == freqs

def test_nogrid(converted_xds_list):
    if len(converted_xds_list) > 1:  # A single dataset is always on a grid.
        with pytest.raises(ValueError):
            # Check that we fail when gains don't fall on a grid.
            sort_datasets(converted_xds_list[:-1])
    else:
        assert True

# ---------------------------------domain_slice--------------------------------

expected_slicing = {
    (10, 19, (0, 20, 40), (9, 29, 49)): slice(0, 2),  # Between
    (10, 19, (0, 10, 20), (9, 19, 29)): slice(1, 2),  # Aligned
    ( 8, 22, (0, 10, 20), (9, 19, 29)): slice(0, 3),  # Overlap  # noqa
    (12, 18, (0, 10, 20), (9, 19, 29)): slice(1, 2),  # Contain
    (30, 39, (0, 10, 20), (9, 19, 29)): slice(2, 3),  # Outside (upper)
    (0, 9, (10, 20, 30), (19, 29, 39)): slice(0, 1),  # Outside (lower)
    (18, 32, (0, 10, 20), (9, 19, 29)): slice(1, 3),  # Overlap + upper edge
    (8, 22, (10, 20, 30), (19, 29, 39)): slice(0, 2),  # Overlap + lower edge
    (50, 59, (0, 20, 40, 60, 80), (9, 29, 49, 69, 89)): slice(2, 4),
    (30, 39, (0, 10, 20, 30, 40), (9, 19, 29, 39, 49)): slice(3, 4),
    (18, 42, (0, 10, 20, 30, 40), (9, 19, 29, 39, 49)): slice(1, 5),
    (22, 28, (0, 10, 20, 30, 40), (9, 19, 29, 39, 49)): slice(2, 3)
}

@pytest.mark.parametrize("input,expected", expected_slicing.items())
def test_slices(input, expected):
    lb, ub, lbounds, ubounds = input
    result = domain_slice(lb, ub, np.array(lbounds), np.array(ubounds))
    assert result == expected

# -----------------------------make_concat_xds_list----------------------------

@pytest.fixture(scope="module")
def concat_xds_list(term_xds_list, sorted_xds_lol):
    return make_concat_xds_list(term_xds_list, sorted_xds_lol)

@pytest.fixture(scope="module")
def bounds(load_params):
    start, step, skip, reps = load_params[:4]  # Time
    min_t = start
    max_t = start + (step + skip)*reps - skip - 1
    start, step, skip, reps = load_params[4:]  # Freq
    min_f = start
    max_f = start + (step + skip)*reps - skip - 1

    return BOUNDS(min_t, max_t, min_f, max_f)

def test_nxds(concat_xds_list, term_xds_list):
    assert len(concat_xds_list) == len(term_xds_list)

def test_time_lower_bounds(concat_xds_list, term_xds_list, bounds):

    concat_lb = [xds.gain_t.values[0] for xds in concat_xds_list]
    term_lb = [xds.gain_t.values[0] for xds in term_xds_list]
    req_extrapolation = [tlb < bounds.min_t for tlb in term_lb]

    zipper = zip(concat_lb, term_lb, req_extrapolation)

    assert all([(clb <= tlb) or re for clb, tlb, re in zipper])

def test_time_upper_bounds(concat_xds_list, term_xds_list, bounds):

    concat_ub = [xds.gain_t.values[-1] for xds in concat_xds_list]
    term_ub = [xds.gain_t.values[-1] for xds in term_xds_list]
    req_extrapolation = [tub > bounds.max_f for tub in term_ub]

    zipper = zip(concat_ub, term_ub, req_extrapolation)

    assert all([(cub >= tub) or re for cub, tub, re in zipper])

def test_freq_lower_bounds(concat_xds_list, term_xds_list, bounds):

    concat_lb = [xds.gain_f.values[0] for xds in concat_xds_list]
    term_lb = [xds.gain_f.values[0] for xds in term_xds_list]
    req_extrapolation = [tlb < bounds.min_f for tlb in term_lb]

    zipper = zip(concat_lb, term_lb, req_extrapolation)

    assert all([(clb <= tlb) or re for clb, tlb, re in zipper])

def test_freq_upper_bounds(concat_xds_list, term_xds_list, bounds):

    concat_ub = [xds.gain_f.values[-1] for xds in concat_xds_list]
    term_ub = [xds.gain_f.values[-1] for xds in term_xds_list]
    req_extrapolation = [tub > bounds.max_f for tub in term_ub]

    zipper = zip(concat_ub, term_ub, req_extrapolation)

    assert all([(cub >= tub) or re for cub, tub, re in zipper])

# -----------------------------make_interp_xds_list----------------------------

@pytest.fixture(scope="module")
def interp_xds_list(term_xds_list,
                    concat_xds_list,
                    interp_mode,
                    interp_method):
    return make_interp_xds_list(term_xds_list,
                                concat_xds_list,
                                interp_mode,
                                interp_method)

def test_has_gains(interp_xds_list):
    assert all(hasattr(xds, "gains") for xds in interp_xds_list)

def test_chunking(interp_xds_list, term_xds_list):
    # TODO: Chunking behaviour is tested but not adequately probed yet.
    assert all(ixds.chunks == txds.chunks
               for ixds, txds in zip(interp_xds_list, term_xds_list))

# -----------------------------load_and_interpolate----------------------------

@pytest.fixture(scope="function")
def interp_xds_lol(gain_xds_lod, chain_opts, load_xds_list, monkeypatch):

    monkeypatch.setattr(
        "quartical.interpolation.interpolate.xds_from_zarr",
        lambda store: load_xds_list
    )

    return load_and_interpolate_gains(gain_xds_lod, chain_opts)

@pytest.fixture(scope="function")
def compute_interp_xds_lol(interp_xds_lol):
    return da.compute(interp_xds_lol)[0]

def test_cixl_has_gains(compute_interp_xds_lol):
    assert all([hasattr(xds, "gains")
               for xds_dict in compute_interp_xds_lol
               for xds in xds_dict.values()])

def test_cixl_gains_ident(compute_interp_xds_lol):
    # NOTE: Splines will not be exactly identity due to numerical precision.
    assert all(np.allclose(xds.gains.values, np.array([1, 0, 0, 1]))
               for xds_dict in compute_interp_xds_lol
               for xds in xds_dict.values())

# -----------------------------------------------------------------------------

max_stars_repo_name: JSKenyon/QuartiCal

max_issues_repo_name: JSKenyon/QuartiCal

max_forks_repo_name: JSKenyon/QuartiCal


text:
import numpy as np
#import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

FILE_NAME = "CRM.csv"

def run(debug = False):
    training_set = load(FILE_NAME, debug)

    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_set = scaler.fit_transform(training_set)

    if debug:
        print(scaled_set)

def load(file, debug = False):
    if file is None:
        return None

    train = pd.read_csv(file)
    training_set = train.iloc[:, 1:2].values

    if debug:
        print(train.head())
        print()

    return training_set

if __name__ == '__main__':
    run(True)
    print("Done!")

max_stars_repo_name: Chopinsky/quantf

max_issues_repo_name: Chopinsky/quantf

max_forks_repo_name: Chopinsky/quantf


text:
import numpy as np
import sympy as sp

def get_mandel_base_sym():
    factor = sp.sqrt(2) / sp.S(2)

    DIM = 3
    DIM_MANDEL6 = 6
    B = np.zeros((DIM_MANDEL6, DIM, DIM), dtype=sp.Symbol)

    B[0, 0, 0] = sp.S(1)
    B[1, 1, 1] = sp.S(1)
    B[2, 2, 2] = sp.S(1)
    B[3, 1, 2] = B[3, 2, 1] = factor
    B[4, 0, 2] = B[4, 2, 0] = factor
    B[5, 0, 1] = B[5, 1, 0] = factor
    return B

mandel_base = get_mandel_base_sym()

def to_mandel(tensor4):
    tmp = np.tensordot(mandel_base, tensor4, axes=2)
    out = np.tensordot(tmp, np.einsum("bkl->klb", mandel_base), axes=2)
    return out

reason: import numpy,import sympy


text:
#!/usr/bin/env python
# encoding: utf-8
# Andre Anjos <andre.anjos@idiap.ch>
# Thu 18 Jun 14:24:51 CEST 2015

'''A simple pre-processing that applies Z-normalization to the input
features'''

import numpy

def estimate_norm(X):
  '''Estimates the mean and standard deviation from a data set

  Parameters:

    X (numpy.ndarray): A 2D numpy ndarray in which the rows represent examples
      while the columns, features of the data you want to estimate
      normalization parameters on

  Returns:

    numpy.ndarray: A 1D numpy ndarray containing the estimated mean over
      dimension 1 (columns) of the input data X

    numpy.ndarray: A 1D numpy ndarray containing the estimated unbiased
      standard deviation over dimension 1 (columns) of the input data X

  '''

  return X.mean(axis=0), X.std(axis=0, ddof=1)

def normalize(X, norm):
  '''Applies the given norm to the input data set

  Parameters:

    X (numpy.ndarray): A 3D numpy ndarray in which the rows represent examples
      while the columns, features of the data set you want to normalize. Every
      depth corresponds to data for a particular class

    norm (tuple): A tuple containing two 1D numpy ndarrays corresponding to the
      normalization parameters extracted with :py:func:`estimated_norm` above.

   Returns:

     numpy.ndarray: A 3D numpy ndarray with the same dimensions as the input
       array ``X``, but with its values normalized according to the norm input.

  '''

  return numpy.array([(k - norm[0]) / norm[1] for k in X])

max_stars_repo_name: anjos/rrpack

max_issues_repo_name: anjos/rrpack

max_forks_repo_name: anjos/rrpack


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

import numpy as np

import mars.tensor as mt
from mars.core import tile

def test_params():
    raw = np.random.rand(10, 10)
    a = mt.tensor(raw)
    a = a[a[0] < 0.5]
    a = tile(a)
    c = a.chunks[0]

    assert any(np.isnan(s) for s in c.params['shape'])
    c.params = c.get_params_from_data(raw[raw[0] < 0.5])
    assert not any(np.isnan(s) for s in c.params['shape'])

    params = c.params.copy()
    params.pop('index', None)
    a.params = params
    assert np.prod(a.shape) > 0
    a.refresh_params()

max_stars_repo_name: hxri/mars

max_issues_repo_name: hxri/mars

max_forks_repo_name: hxri/mars


text:
import numpy as np
import pandas as pd

from database.writer import Writer

class Entity:
	def __init__(self, tablename: str, file_name: str, attrs: dict = None, primary_keys: list = None,
				 replace_vals: list = None, fill_val: object = None):
		self.file_name = file_name
		self.tablename = tablename
		self.replace_vals = replace_vals
		self.fill_val = fill_val
		self.primary_keys = primary_keys
		self.attrs = attrs

	def set_writer(self, writer: Writer):
		self.writer = writer

	def load(self):
		df = pd.read_csv(self.file_name)
		if self.replace_vals is not None:
			if self.fill_val is None:
				raise Exception(f'Specify a fill value for replacing: {self.replace_vals}')
			else:
				df = df.replace(self.replace_vals, np.NaN).fillna(self.fill_val)

		self.writer.write(self.tablename, df, primary_keys=self.primary_keys, attrs=self.attrs)

max_stars_repo_name: woojiahao/pds-analysis

max_issues_repo_name: woojiahao/pds-analysis

max_forks_repo_name: woojiahao/pds-analysis