text:
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 14:47:11 2019

@author: Daiwei Lin
"""
import time
import numpy as np
import math

class Node:
    def __init__(self, sma_num, led_num, moth_num, para=None):
        self.sma_num = sma_num
        self.led_num = led_num
        self.moth_num = moth_num

        self.sma_act = False
        self.led_act = False
        self.moth_act = False

        self.sma_start_t = 0.0
        self.led_start_t = 0.0
        self.moth_start_t = 0.0

        self.sma_val = [0.0] * sma_num
        self.led_val = [0.0] * led_num
        self.moth_val = [0.0] * moth_num

        # ====================#
        # Tunable parameters  #
        # ====================#
        self.para_length = 10
        self.para_buffer = [None] * 10
        if para is not None:
            self.led_ru = para['led_ru']
            self.led_ho = para['led_ho']
            self.led_rd = para['led_rd']
            self.moth_ru = para['moth_ru']
            self.moth_ho = para['moth_ho']
            self.moth_rd = para['moth_rd']
            self.I_max = para['I_max']

            self.ml_gap = para['ml_gap']
            self.sma_gap = para['sma_gap']

        else:
            self.led_ru = 1
            self.led_ho = 2
            self.led_rd = 1
            self.moth_ru = 1
            self.moth_ho = 2
            self.moth_rd = 1
            self.I_max = 1

            self.ml_gap = 1
            self.sma_gap = 0.2

    def set_parameters(self, para):
        """
        Set tunable parameters. If the actuator is being used, the parameters will be saved in the buffer, which will be
        used later after actions are complete.
        :param para: an array [led_ru, led_ho, led_rd, moth_ru, moth_ho, moth_rd, I_max, ml_gap, sma_gap]
        """
        # LED
        if not self.led_act:
            # update parameters immediately
            self.led_ru = para[0]
            self.led_ho = para[1]
            self.led_rd = para[2]
        else:
            self.para_buffer[0:3] = para[0:3]

        # moth
        if not self.moth_act:
            self.moth_ru = para[3]
            self.moth_ho = para[4]
            self.moth_rd = para[5]
        else:
            self.para_buffer[3:6] = para[3:6]

        # SMA
        if not self.sma_act:
            self.I_max = para[6]
            self.sma_gap = para[8]
        else:
            self.para_buffer[6] = para[6]
            self.para_buffer[8] = para[8]

    def update_para_from_buffer(self, actuator):
        '''
        Update parameters and clear buffer
        '''

        if actuator == 'led' and self.para_buffer[0] is not None:
            self.led_ru = self.para_buffer[0]
            self.led_ho = self.para_buffer[1]
            self.led_rd = self.para_buffer[2]

            self.para_buffer[0] = None
            self.para_buffer[1] = None
            self.para_buffer[2] = None

        elif actuator == 'sma' and self.para_buffer[6] is not None:
            self.I_max = self.para_buffer[6]
            self.sma_gap = self.para_buffer[8]

            self.para_buffer[6] = None
            self.para_buffer[8] = None

    def node_step(self):
        """
        Perform one step of one node and returns actuators actions of the node
        :return: led*num_led, sma*num_sma
        """

        curr_t = time.time()

        # SMA
        sma_duration = curr_t - self.sma_start_t
        if sma_duration <= 5.0:
            for i in range(self.sma_num):
                self.sma_val[i] = self.I_max * sma_duration / 5.0
        else:
            for i in range(self.sma_num):
                self.sma_val[i] = 0
            self.sma_act = False
            self.update_para_from_buffer('sma')
        # print("SMA : {}".format(self.sma_val))

        # LED
        led_duration = curr_t - self.led_start_t
        if led_duration <= self.led_ru:
            for i in range(self.led_num):
                self.led_val[i] = led_duration / self.led_ru
        elif led_duration <= self.led_ho:
            for i in range(self.led_num):
                self.led_val[i] = 1
        elif led_duration <= self.led_rd:
            t = led_duration - self.led_ho - self.led_ru
            value = 1 - (t / self.led_rd)
            for i in range(self.led_num):
                self.led_val[i] = value
        else:
            for i in range(self.led_num):
                self.led_val[i] = 0
            self.led_act = False
            self.update_para_from_buffer('led')

        # print("LED : {}".format(self.led_val))

        # Moth here
        # ---------
        return self.led_val, self.sma_val  # self.moth_val

    def activate(self, act_type):
        # start timer
        if act_type == 'sma':
            if not self.sma_act:
                self.sma_act = True
                self.sma_start_t = time.time()
        elif act_type == 'led':
            if not self.led_act:
                self.led_act = True
                self.led_start_t = time.time()
        elif act_type == 'moth':
            if not self.moth_act:
                self.moth_act = True
                self.moth_start_t = time.time()

        else:
            raise ValueError("Invalid actuator type: {}".format(act_type))

class Sculpture:
    def __init__(self, node_num, sma_num, led_num, moth_num):

        self.node_list = list()
        self.num_nodes = node_num

        self.sma_num = sma_num
        self.led_num = led_num
        self.moth_num = moth_num

        for _ in range(node_num):
            self.node_list.append(Node(sma_num, led_num, moth_num))

        print("Sculpture initialized.")
        self._sculpture_info()

        # create a chain that defines the structure of sculpture
        # Used in propagation
        self.chain = list()
        for i in range(int(node_num / 2)):
            self.chain.append([2 * i, 2 * i + 1])

    def sculpture_step(self):
        '''
        Perform one step of the sculpture and returns actions of all actuators
        :return: [led*num_led*num_node, sma*num_sma*num_node]
        '''

        # loop through all nodes
        sma_action = list()
        led_action = list()
        for node in self.node_list:
            node_sma_action, node_led_action = node.node_step()
            sma_action = sma_action + node_sma_action
            led_action = led_action + node_led_action

        return sma_action + led_action

    def activate_local_reflex(self, index, act_list):
        '''

        :param index: Node index
        :param act_list: list of actuators to activate in the node
        '''
        for act in act_list:
            self.node_list[index].activate(act)

    def _sculpture_info(self):
        print("{} nodes".format(self.num_nodes))
        print("Each node has {} sma, {} led, {} moth".format(self.sma_num, self.led_num, self.moth_num))

class Behaviour:
    def __init__(self, sculpture, system_freq=10):
        self.state = 'idle'  # either idle or active
        self.sculpture = sculpture
        self.state_timer = time.time()
        self.idle_timer = time.time()
        self.propagate_timer = time.time()

        self.propagation_list = list()

        self.n_gap = 1
        self.t_sma = 5

        self.time_scale = 10 / system_freq
        self.para_mapping_scale = np.array([5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 1.0, 5.0, 5.0, 5.0, 4.0]) * np.array(
                                            [1, 1, 1, 1, 1, 1, 1, self.time_scale, self.time_scale, self.time_scale, self.time_scale])
        self.para_mapping_offset = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
        print("Behaviour initialized.")

    def set_parameter(self, para):
        '''

        :param para:[led_ru, led_ho, led_rd, moth_ru, moth_ho, moth_rd, I_max, ml_gap, sma_gap, n_gap, t_sma]
        '''

        para = para * self.para_mapping_scale + self.para_mapping_offset
        self.n_gap = para[9]
        self.t_sma = para[10]
        for node in self.sculpture.node_list:
            node.set_parameters(para[0:9])

    def step(self, observation):
        # Produce actions of all actuators for one step

        # <state transition>
        for obs in observation:
            if obs > 0:
                self.state = 'active'
                self.state_timer = time.time()
                break

        if self.state == 'active' and time.time() - self.state_timer >= 30 and len(self.propagation_list) == 0:
            print("Enter Idle state.")
            self.state = 'idle'
        # <end of state transition>

        # print("\n{} Step ----------".format(self.state))
        if self.state == 'idle':
            # randomly select one node
            if time.time() - self.idle_timer >= self.t_sma:
                self.idle_timer = time.time()

                rand_idx = np.random.randint(0, self.sculpture.num_nodes)
                rand_actuator = np.random.choice(['led', 'sma'], 1)[0]
                # print("(IDLE)Activate {} on node{}".format(rand_actuator, rand_idx))
                self.sculpture.activate_local_reflex(rand_idx, [rand_actuator])
        else:
            # actuate at the triggered node and propagate
            trigger_nodes = observation > 0
            for idx in range(len(observation)):
                if trigger_nodes[idx]:
                    # print("(ACTIVE)Activate {} on node{}".format(['led', 'sma', 'moth'], idx))
                    self.sculpture.activate_local_reflex(idx, ['led', 'sma', 'moth'])
                    self.create_propagation_list(idx)

            # activate the nodes for propagation
            if len(self.propagation_list) > 0:
                if time.time() - self.propagate_timer >= self.n_gap:
                    self.propagate_timer = time.time()
                    # print("Propagation list {}".format(self.propagation_list))
                    for node in self.propagation_list[0]:
                        # print("(ACTIVE)(P)Activate {} on node{}".format(['led', 'sma', 'moth'], node))
                        self.sculpture.activate_local_reflex(node, ['led', 'sma', 'moth'])
                    self.propagation_list.pop(0)

        action = self.sculpture.sculpture_step()
        # print("action = {}".format(action))
        return action

    def create_propagation_list(self, node_index):
        # Find starting location
        start_point = -1
        for i in range(len(self.sculpture.chain)):
            if node_index in self.sculpture.chain[i]:
                start_point = i
                break
        assert start_point >= 0, "No starting point found for propagation."

        # Create list
        p_list = list()
        i = 1
        while True:
            if node_index - i >= 0 and node_index + i <= self.sculpture.num_nodes - 1:
                p_list.append([node_index - i, node_index + i])
            elif node_index - i >= 0:
                p_list.append([node_index - i])
            elif node_index + i <= self.sculpture.num_nodes - 1:
                p_list.append([node_index + i])
            else:
                break
            i += 1
        self.propagation_list = p_list
        # print("Propagation list {}".format(self.propagation_list))

if __name__ == '__main__':
    ROM_sculpture = Sculpture(node_num=10, sma_num=2, led_num=1, moth_num=1)
    behaviour = Behaviour(ROM_sculpture)

    for i in range(1000):
        observation = np.array([0, 0, 0, 0])
        if i % 30 == 29:
            observation = np.array([0, 0, 1, 0])

        action = behaviour.step(observation)
        print(action)
        time.sleep(0.2)


text:
# ============= WHAT IS NUMPY ? =============

# NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides
# a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment
# of routines for fast operations on arrays,
# including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms,
# basic linear algebra, basic statistical operations,
# random simulation and much more.

#  ============= REQUIREMENT =============
# pip install numpy

# ============= IMPORT MODULE/PACKAGE =============
import numpy as np

# ============= MAKE ARRAY USING NUMPY =============
array1 = np.array([1, 2, 3 ,4, 5])
print("Result is :" , array1)

# RESULT :
    #  Result is : [1 2 3 4 5]

# ============= MAKE ARRAY WITH 0 VALUE =============
array2 = np.zeros(5) # MAKE ARRAY 0 VALUE AS MUCH 5
print("Result is :", array2)

# RESULT :
    #  Result is : [0. 0. 0. 0. 0.]

# ============= MAKE ARRAY WITH 1 VALUE =============
array3 = np.ones(5) # MAKE ARRAY 1 VALUE AS MUCH 5
print("Result is :", array3)

# RESULT :
    #  Result is : [1. 1. 1. 1. 1.]

# ==================== THANKS FOR YOUR ATTENTION, I HOPE YOU LIKE THIS ====================

ext: py

reason: import numpy


text:
#!/usr/bin/env python

import argparse
from functools import partial
from pathlib import Path

import h5py
import numpy as np
import pandas as pd
from numba import jit

from dautil.date_time import strptime_np
from tail.window import zero_pad_ratio
from tail.util import effective_sky

__version__ = '0.3'

def get_greg(path):
    return strptime_np(path.parent.parent.name)

def get_idx(path):
    greg = get_greg(path)

    temp = path.stem.split('_')
    null_split = '_'.join(temp[1:-1])
    sub_split = temp[-1]
    return null_split, sub_split, greg

def get_weight_fsky(tmask_apo, pmask_apo, pixel_size, path):
    with h5py.File(path, 'r') as f:
        tweights = f['w0'][:]
        pweights = f['w4'][:]
    tweight = tweights.sum()
    pweight = pweights.sum()
    tmask = tmask_apo * zero_pad_ratio(tweights > 0.)
    pmask = pmask_apo * zero_pad_ratio(pweights > 0.)
    t_fsky = effective_sky(tmask, pixel_size)
    p_fsky = effective_sky(pmask, pixel_size)
    return tweight, pweight, t_fsky, p_fsky

def main(
    basedir,
    glob,
    mask_path,
    pixel_size,
    no_mpi,
    processes,
    full
):
    all_paths = list(basedir.glob(glob))
    pixel_size *= np.pi / 10800.
    with h5py.File(mask_path, 'r') as f2:
        tmask = f2['t'][:]
        pmask = f2['p'][:]

    _get_weight_fsky = partial(get_weight_fsky, tmask, pmask, pixel_size)
    if not no_mpi:
        from mpi4py.futures import MPIPoolExecutor
        with MPIPoolExecutor() as executor:
            data = executor.map(
                _get_weight_fsky,
                all_paths
            )
    else:
        from dautil.util import map_parallel
        data = map_parallel(
            _get_weight_fsky,
            all_paths,
            processes=processes
        )

    index = pd.Index(
        list(map(get_greg, all_paths)),
        name='date'
    ) if full else pd.MultiIndex.from_tuples(
        list(map(get_idx, all_paths)),
        names=('nullsplit', 'subsplit', 'date')
    )

    df = pd.DataFrame(
        data=data,
        index=index,
        columns=('tweight', 'pweight', 't_fsky', 'p_fsky')
    )

    df.sort_index(inplace=True)
    return df

def cli():
    parser = argparse.ArgumentParser(description="Collect temperature and polarization stat. such as total weight and f-sky.")

    parser.add_argument('basedir',
                        help="Input base directory of weights.")
    parser.add_argument('-m', '--mask', required=True,
                        help='HDF5 file holding the apodization masks.')
    parser.add_argument('-o', '--output', required=True,
                        help='Output filename for DataFrame in HDF5.')
    parser.add_argument('--glob', default='????????_??????/coadd/realmap_*_?.hdf5',
                        help='glob pattern. Default: ????????_??????/coadd/realmap_*_?.hdf5')
    parser.add_argument('--full', action='store_true',
                        help='If specified, assume full map intead.')

    parser.add_argument('--no-mpi', action='store_true',
                        help='If specified, do not use MPI.')
    parser.add_argument('-p', '--processes', default=1, type=int,
                        help='No. of multiprocessing processes when --no-mpi is used.')
    parser.add_argument('--pixel-size', default=2., type=float)

    parser.add_argument('-c', '--compress-level', default=9, type=int,
                        help='compress level of gzip algorithm. Default: 9. Note: 9 comparing to 4 adds about 29 percent of compute time.')
    parser.add_argument('-v', '--version', action='version',
                        version='%(prog)s {}'.format(__version__))

    args = parser.parse_args()

    df = main(
        Path(args.basedir),
        args.glob,
        args.mask,
        args.pixel_size,
        args.no_mpi,
        args.processes,
        args.full
    )
    df.to_hdf(
        args.output,
        'df',
        format='table',
        complevel=args.compress_level,
        fletcher32=True
    )

if __name__ == "__main__":
    cli()

max_stars_repo_name: ickc/TAIL

max_issues_repo_name: ickc/TAIL

max_forks_repo_name: ickc/TAIL

reason: import numpy,from numba


text:
# Copyright 2020 Google LLC, University of Victoria, Czech Technical University
#
# 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 cv2
import numpy as np
from copy import deepcopy
from time import time
from utils.match_helper import get_matching_dist_type, remove_duplicate_matches

def match(desc1, desc2, cfg, kps1=None, kps2=None):
    ''' Computes and returns matches with the ratio test

    param desc1: descriptors of the first image
    param desc2: descriptors of the second image
    param cfg: Configuration

    return matches: np.ndarray with match indices
    '''

    # Parse options
    cur_key = 'config_{}_{}'.format(cfg.dataset, cfg.task)
    method_match = cfg.method_dict[cur_key]['matcher']
    filter_type = method_match['filtering']['type'].lower()

    # Ratio test threshold
    if filter_type.lower() in [
            'snn_ratio_pairwise', 'snn_ratio_vs_last', 'fginn_ratio_pairwise'
    ]:
        ratio_th = method_match['filtering']['threshold']
        if ratio_th < 0.1 or ratio_th > 1.01:
            raise ValueError('Ratio test threshold outside expected range')

    # FGINN spatial threshold
    if filter_type in ['fginn_ratio_pairwise']:
        fginn_spatial_th = method_match['filtering']['fginn_radius']
        if fginn_spatial_th < 0 or fginn_spatial_th > 500:
            raise ValueError('FGINN radius outside expected range')

    # Distance threshold
    max_dist = None
    if 'descriptor_distance_filter' in method_match:
        max_dist = method_match['descriptor_distance_filter']['threshold']

    # Skip this if there are no features
    if desc1.shape[0] == 0 or desc2.shape[0] == 0:
        return np.empty((2, 0)), 0

    # Reformat descriptors according to the distance type
    dist = get_matching_dist_type(cfg)
    if method_match['distance'].lower() == 'hamming':
        desc1 = desc1.astype(np.uint8)
        desc2 = desc2.astype(np.uint8)
    elif method_match['distance'].lower() in ['l1', 'l2']:
        desc1 = desc1.astype(np.float32)
        desc2 = desc2.astype(np.float32)
    else:
        raise ValueError('Unknown distance type')

    t_start = time()

    # Use opencv BF/FLANN matcher
    do_flann = method_match['flann']

    if do_flann:
        if dist == cv2.NORM_L2:
            FLANN_INDEX_KDTREE = 1  # FLANN_INDEX_KDTREE
        elif dist == cv2.NORM_HAMMING:
            FLANN_INDEX_KDTREE = 5  # FLANN_INDEX_HIERARCHICAL
        else:
            FLANN_INDEX_KDTREE = 0  # FLANN_INDEX_LINEAR
        index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
        search_params = dict(checks=128)  # or pass empty dictionary
        bf = cv2.FlannBasedMatcher(index_params, search_params)
    else:
        bf = cv2.BFMatcher(dist, crossCheck=False)

    # Matching
    num_nn = method_match['num_nn']

    if 'fginn' in filter_type:
        matches = bf.knnMatch(desc1, desc2, k=10 + num_nn)
    else:
        matches = bf.knnMatch(desc1, desc2, k=max(2, num_nn + 1))

    # Apply filtering (ratio test or something else)
    valid_matches = []

    if filter_type == 'none':
        for cur_match in matches:
            tmp_valid_matches = [nn_1 for nn_1 in cur_match[:-1]]
            valid_matches.extend(tmp_valid_matches)
    elif filter_type == 'snn_ratio_pairwise':
        for cur_match in matches:
            tmp_valid_matches = [
                nn_1 for nn_1, nn_2 in zip(cur_match[:-1], cur_match[1:])
                if nn_1.distance <= ratio_th * nn_2.distance
            ]
            valid_matches.extend(tmp_valid_matches)
    elif filter_type == 'snn_ratio_vs_last':
        for cur_match in matches:
            nn_n = cur_match[-1]
            tmp_valid_matches = [
                nn_i for nn_i in cur_match[:-1]
                if nn_i.distance <= ratio_th * nn_n.distance
            ]
            valid_matches.extend(tmp_valid_matches)
    elif filter_type == 'fginn_ratio_pairwise':
        flann = cv2.BFMatcher(cv2.NORM_L2, crossCheck=False)
        not_fginns = flann.radiusMatch(kps2.astype(np.float32),
                                       kps2.astype(np.float32),
                                       fginn_spatial_th)
        for m_idx, cur_match in enumerate(matches):
            for mii in range(num_nn):
                cur_non_fginns = [
                    x.trainIdx for x in not_fginns[cur_match[mii].trainIdx]
                ]
                for nn_2 in cur_match[mii + 1:]:
                    if cur_match[mii].distance <= ratio_th * nn_2.distance:
                        valid_matches.append(cur_match[mii])
                        break
                    if nn_2.trainIdx not in cur_non_fginns:
                        break
    else:
        raise ValueError('Unknown filter type')

    # Filter matches by descriptor distance
    if max_dist:
        valid_matches_with_dist = []
        for valid_match in valid_matches:
            if valid_match.distance <= max_dist:
                valid_matches_with_dist.append(valid_match)
        valid_matches = valid_matches_with_dist

    # Turn opencv return format into numpy
    matches_list = []
    for m in valid_matches:
        matches_list.append([m.queryIdx, m.trainIdx])
    matches = np.asarray(matches_list).T

    # If two-way matching is enabled
    if method_match['symmetric']['enabled']:
        reduce_method = method_match['symmetric']['reduce'].lower()

        # Hacky but works: disable symmetric matching and call self
        cfg_temp = deepcopy(cfg)
        cfg_temp.method_dict['config_{}_{}'.format(
            cfg.dataset, cfg.task)]['matcher']['symmetric']['enabled'] = False
        matches_other_direction, ellapsed_other_direction = match(
            desc2, desc1, cfg_temp, kps2, kps1)

        if reduce_method == 'any':
            if len(matches.shape) == 2 and \
               len(matches_other_direction.shape) == 2:
                matches = np.concatenate(
                    [matches, matches_other_direction[::-1]], axis=1)
        elif reduce_method == 'both':
            m1 = matches.T
            m2 = matches_other_direction.T
            out = []
            if len(m2) < 2:
                matches = np.zeros((2, 0)).astype(np.int32)
            else:
                for i in range(len(m1)):
                    i1, i2 = m1[i]
                    mask = m2[:, 0] == i2
                    row = m2[mask]
                    if len(row) > 0:
                        i22, i11 = row[0]
                        if (i1 == i11) and (i2 == i22):
                            out.append(m1[i].reshape(1, -1))
                if len(out) > 0:
                    matches = np.concatenate(out, axis=0).T.astype(np.int32)
                else:
                    matches = np.zeros((2, 0)).astype(np.int32)
        else:
            raise ValueError('Unknown symmetrical match reduce ',
                             reduce_method)
    else:
        ellapsed_other_direction = 0

    # Remove duplicate matches
    matches = remove_duplicate_matches(matches, kps1, kps2)

    return matches, time() - t_start + ellapsed_other_direction

max_issues_repo_name: QinZiwen/image-matching-benchmark

max_forks_repo_name: QinZiwen/image-matching-benchmark


text:
import os
import sys
import glob
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow import keras
import processing as prc
import numpy as np

# load the labels. these are just the names of the folders in
# the extracted_images folder

labels = sorted([os.path.basename(i) for i in glob.glob("../data/extracted_images/*")])

# load our saved model that we trained

model = keras.models.load_model('./testmodel.data')

# for each image file given on the commandline:
#   1. load the image
#   2. preprocess is to make it white-on-black
#   3. ask the classifier to predict a label
# see: https://keras.io/api/preprocessing/image/

for fn in sys.argv[1:]:
    image = keras.preprocessing.image.load_img(fn, color_mode="grayscale")
    input_arr = keras.preprocessing.image.img_to_array(image)
    input_arr = np.array([input_arr])
    input_arr = np.abs(input_arr-255.0)
    p1 = model.predict(input_arr)

    maxpred = p1.max()
    predindex = int(p1.argmax(axis=-1))
    predlabel = labels[predindex]
    print(f"{os.path.basename(fn)} is predicted to be '{predlabel}' with probabilty: {maxpred:.3f}")


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Provide a wrapper around the Non-Linear optimizers (NLopt).

References:
    [1] https://nlopt.readthedocs.io/en/latest/
"""

from autograd import numpy as np
from autograd import grad
import torch

# NLopt optimizers
try:
    import nlopt
except ImportError as e:
    raise ImportError(e.__str__() + "\n HINT: you can install nlopt via `pip install nlopt`.")

from pyrobolearn.optimizers import Optimizer

__author__ = "Brian Delhaisse"
__copyright__ = "Copyright 2018, PyRoboLearn"
__credits__ = ["Brian Delhaisse"]
__license__ = "GNU GPLv3"
__version__ = "1.0.0"
__maintainer__ = "Brian Delhaisse"
__email__ = "briandelhaisse@gmail.com"
__status__ = "Development"

class NLopt(Optimizer):
    r"""Non-Linear Optimizer

    Non-linear optimizers based on the `nlopt` libraries.

    Here is a brief of lists of the current algorithms implemented:
    *

    Nonlinear optimization algos that can handle nonlinear inequality and EQUALITY constraints are:
    - ISRES (Improved Stochastic Ranking Evolution Strategy) --> global derivative-free
    - COBYLA (Constrained Optimization BY Linear Approximations) --> local derivative-free
    - SLSQP (Sequential Least-SQuares Programming) --> local gradient-based
    - AUGLAG (AUGmented LAGrangian) --> global/local derivative-free/gradient based (determined based on the
        subsidiary algo)

    More information about:
    - algorithms: https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/

    References:
        - [1] NLopt: https://nlopt.readthedocs.io/en/latest/
        - [2] NLopt with Python: with Python: https://nlopt.readthedocs.io/en/latest/NLopt_Python_Reference/
        - [3] Github repo: https://github.com/stevengj/nlopt
    """

    def __init__(self, method, submethod=None, seed=None, *args, **kwargs):
        """
        Initialize the non-linear optimizer.

        Args:
            method (str): primary optimization method to be used.
            submethod (str): sub-optimization method to be used in the primary optimization method.
            seed (None, int): random seed
            *args:
            **kwargs:
        """
        super(NLopt, self).__init__(*args, **kwargs)

        # define useful variables
        self.results = {1: 'success', 2: 'stop_val reached', 3: 'ftol reached', 4: 'xtol reached',
                        5: 'maxeval reached', 6: 'maxtime reached', -1: 'failure', -2: 'invalid args',
                        -3: 'out of memory', -4: 'roundoff limited', -5: 'forced stop'}

        # define random seed
        nlopt.srand(seed)

        # define which solver to use
        def get_optimizer(method):
            """
            Get the optimizer associated with the given method.

            Args:
                method (str): optimizer string

            Returns:

            """
            if method == 'ISRES':
                return nlopt.opt(nlopt.GN_ISRES, M)
            elif method == 'COBYLA':
                return nlopt.opt(nlopt.LN_COBYLA, M)
            elif method == 'SLSQP':
                return nlopt.opt(nlopt.LD_SLSQP, M)
            elif method == 'AUGLAG':
                return nlopt.opt(nlopt.AUGLAG, M)
            else:
                raise NotImplementedError("The given method has not been implemented")

        if method is None:
            method = 'SLSQP'
        self.optimizer = get_optimizer(method)

        # define subsolver to use (if we use the AUGLAG method)
        if method == 'AUGLAG':
            if submethod is None:
                submethod = 'SLSQP'
            elif submethod == 'AUGLAG':
                raise ValueError("Submethod should be different from AUGLAG")
            subopt = get_optimizer(submethod)
            subopt.set_lower_bounds(-1)
            subopt.set_upper_bounds(1)
            # subopt.set_ftol_rel(1e-2)
            # subopt.set_maxeval(100)
            self.optimizer.set_local_optimizer(subopt)

    def optimize(self, parameters, loss, max_iters=1, verbose=False, *args, **kwargs):
        """
        Optimize the given objective function using the optimizer.

        Args:
            parameters (np.array): parameters to optimize.
            loss (callable): callable objective / loss function to minimize.
            bounds (tuple, list, np.array): parameter bounds. E.g. bounds=[0, np.inf]
            max_iters (int): number of maximum iterations.
            verbose (bool): if True, it will display information during the optimization process.
            *args: list of arguments to give to the loss function if callable.
            **kwargs: dictionary of arguments to give to the loss function if callable.

        Returns:
            float, torch.Tensor, np.array: loss scalar value.
            object: best parameters
        """
        # define objective function and its gradient
        def f(x, grad):
            loss_value = loss(x)
            if grad.size > 0:
                grad[:] = grad(loss, x)
            return loss_value

        # define objective function to maximize
        self.optimizer.set_min_objective(f)

        # if nlopt.GN_ISRES, we can define the population size
        self.optimizer.set_population(0)  # by default for ISRES: pop=20*(M+1)

        # define bound constraints (should be between -1 and 1 because the norm should be 1)
        self.optimizer.set_lower_bounds(-1.)
        self.optimizer.set_upper_bounds(1.)

        # define norm constraint and its gradient
        def c1(x, grad):
            if grad.size > 0:
                grad[:] = 2 * x
            return x.T.dot(x) - 1

        # define orthogonal constraint
        class OrthogonalConstraint(object):

            def __init__(self, v):
                self.v = np.copy(v)

            def constraint(self, x, grad):
                if grad.size > 0:
                    grad[:] = self.v
                return x.T.dot(self.v)

        # define equality constraints
        self.optimizer.add_equality_constraint(c1, 0)
        # opt.add_equality_mconstraint(constraints, tol)

        # define stopping criteria
        # self.optimizer.set_stopval(stopval)
        self.optimizer.set_ftol_rel(1e-8)
        # opt.set_xtol_rel(1e-4)
        self.optimizer.set_maxeval(100000)  # nb of iteration
        self.optimizer.set_maxtime(2)  # time in secs

        # define initial value
        x0 = np.array([0.1] * M)  # important that the initial value != 0 for the computation of the grad!

        evals, evecs, msgs = [], [], {}
        for i in range(M):
            # add constraint
            if i > 0:
                c = OrthogonalConstraint(x)
                self.optimizer.add_equality_constraint(c.constraint, 0)

            # optimize
            try:
                x = self.optimizer.optimize(x0)
            except nlopt.RoundoffLimited as e:
                pass

            # save values
            evecs.append(x)  # param vector
            evals.append(self.optimizer.last_optimum_value())  # max value
            msgs[i] = nlopt_results[self.optimizer.last_optimize_result()]

max_stars_repo_name: Pandinosaurus/pyrobolearn

max_issues_repo_name: Pandinosaurus/pyrobolearn

max_forks_repo_name: Pandinosaurus/pyrobolearn


text:
from pymote.logger import logger
from numpy import sqrt, pi, sin, cos
from numpy.random import rand

Mobility_Type = {0: "Fixed", 1: "Mobile-Uniform Velocity", 2: "Mobile-Uniform Velocity-Random Heading",
                 3: "Mobile-Random"}
MAX_VELOCITY = 50  # m/s

class MobilityModel(object):

    # Velocity
    VELOCITY = 20.0   # m/s
    HEADING = pi/4  # in rad (pi = 180 deg)

    MAX_RANDOM_MOVEMENT = 30  # m

    def __init__(self, mobile_type=0, node_type=None, **kwargs):
        """
        Initialize the node object.

        node_type: 'N' regular, 'B' base station/Sink, 'C' coordinator/cluster head/relay

        """
        self.type = node_type or 'N'
        self.mobile_type = mobile_type
        self.moved_x = 0
        self.moved_y = 0
        self.moved_z = 0

    def __repr__(self):
        return "<Mobility Type=%s>" % (Mobility_Type[self.mobile_type])

    def drift(self, time=1.0):
        x_drift = 0
        y_drift = 0
        z_drift = 0
        velocity = min(self.VELOCITY, MAX_VELOCITY)

        if self.mobile_type == 1:  # S = v * t
            x_drift = velocity * cos(self.HEADING) * time
            y_drift = velocity * sin(self.HEADING) * time
        elif self.mobile_type == 2:  # uniform velocity but random direction
            rnd = rand(2)
            x_drift = velocity * cos(rnd[0]*2*pi) * time * time
            y_drift = velocity * sin(rnd[0]*2*pi) * time * time
        elif self.mobile_type == 3:  # random
            rnd = rand(2)
            x_drift = (rnd[0] - 0.5) * self.MAX_RANDOM_MOVEMENT
            y_drift = (rnd[1] - 0.5) * self.MAX_RANDOM_MOVEMENT

        self.moved_x += x_drift
        self.moved_y += y_drift
        self.moved_z += z_drift

        return x_drift, y_drift, z_drift

    def have_moved(self):
        if abs(self.moved_x) + abs(self.moved_y) + abs(self.moved_z) > 1e-5:
            return True
        return False

    @property
    def mobilityType(self):
        return self.mobile_type

    @mobilityType.setter
    def mobilityType(self, mobile_type):
        self.mobile_type = mobile_type

max_stars_repo_name: chinmaydd/RouteSense

max_issues_repo_name: chinmaydd/RouteSense

max_forks_repo_name: chinmaydd/RouteSense

reason: from numpy


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

from IPython.display import display_html, HTML

def data_set():

    pd.set_option('display.max_columns', None)
    df = pd.read_stata("data/oreopoulos_resume_study_replication_data_file.dta")
    #fill_values = {'chinese': 0, 'indian': 0, "british":0, 'pakistani': 0, 'pakistani':0, "Chn_Cdn":0, "same_exp":0}
    #df.fillna(value=fill_values, inplace=True)
    return df

def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str+=df.to_html()
    display_html(html_str.replace('table','table style="display:inline"'),raw=True)

##############################################   Section 3  ##############################################################

def second_tableA():

    df = data_set()

    x1 = df[(df["type"] == 0) & (df["female"] == 0)].groupby(["name_ethnicity", "name"]).apply(len)
    x2 = df[(df["type"] == 1) & (df["female"] == 0)].groupby(["name_ethnicity", "name"]).apply(len)
    x3 = df[(df["type"] == 2) & (df["female"] == 0)].groupby(["name_ethnicity", "name"]).apply(len)
    x4 = df[(df["type"] == 3) & (df["female"] == 0)].groupby(["name_ethnicity", "name"]).apply(len)
    x5 = df[(df["type"] == 4) & (df["female"] == 0)].groupby(["name_ethnicity", "name"]).apply(len)

    y1 = df[(df["type"] == 0) & (df["female"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y2 = df[(df["type"] == 1) & (df["female"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y3 = df[(df["type"] == 2) & (df["female"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y4 = df[(df["type"] == 3) & (df["female"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y5 = df[(df["type"] == 4) & (df["female"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)

    pd.options.display.float_format = '{:,.0f}'.format

    ma_names = np.transpose(pd.DataFrame([x1,x2,x3,x4,x5],
                                    index=["Type 0","Type 1", "Type 2", "Type 3", "Type 4"])).fillna(value=" ")
    ma_names = ma_names.rename_axis(["Name ethnicity", "Names"])

    fe_names = np.transpose(pd.DataFrame([y1,y2,y3,y4,y5],
                                    index=["Type 0","Type 1", "Type 2", "Type 3", "Type 4"])).fillna(value=" ")
    fe_names = fe_names.rename_axis(["Name ethnicity", "Names"])

    display_side_by_side(ma_names, fe_names)

def second_tableB():

    df = data_set()

    x1 = df[(df["type"] == 0) & (df["female"] == 0) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    x2 = df[(df["type"] == 1) & (df["female"] == 0) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    x3 = df[(df["type"] == 2) & (df["female"] == 0) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    x4 = df[(df["type"] == 3) & (df["female"] == 0) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    x5 = df[(df["type"] == 4) & (df["female"] == 0) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)

    y1 = df[(df["type"] == 0) & (df["female"] == 1) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y2 = df[(df["type"] == 1) & (df["female"] == 1) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y3 = df[(df["type"] == 2) & (df["female"] == 1) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y4 = df[(df["type"] == 3) & (df["female"] == 1) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)
    y5 = df[(df["type"] == 4) & (df["female"] == 1) & (df["callback"] == 1)].groupby(["name_ethnicity", "name"]).apply(len)

    pd.options.display.float_format = '{:,.0f}'.format

    ma_names = np.transpose(pd.DataFrame([x1,x2,x3,x4,x5],
                                    index=["Type 0","Type 1", "Type 2", "Type 3", "Type 4"])).fillna(value=" ")
    ma_names = ma_names.rename_axis(["Name ethnicity", "Names"])

    fe_names = np.transpose(pd.DataFrame([y1,y2,y3,y4,y5],
                                    index=["Type 0","Type 1", "Type 2", "Type 3", "Type 4"])).fillna(value=" ")
    fe_names = fe_names.rename_axis(["Name ethnicity", "Names"])

    display_side_by_side(ma_names, fe_names)

def third_table(df = data_set()):

    frame = [[], [], [], [], [], [], [], [], []]
    X = []

    index=["Female", "Top 200 world ranking university", "Extra curricular activities listed",
           "Fluent in French and other languages", "Canadian master’s degree", "High quality work experience",
           "List Canadian references", "Accreditation of foreign education", "Permanent resident indicated"]

    columns = ["Type 0", "Type 1", "Type 2", "Type 3", "Type 4"]

    types = [0,1,2,3,4,5]

    for i in types:

        if i in [0,1,2,3,4]:

            frame[0].append(len(df[(df["type"] == i) & (df["female"] == 1)])/len(df[df["type"] == i]))
            frame[1].append(len(df[(df["type"] == i) & (df["ba_quality"] == 1)])/len(df[df["type"] == i]))
            frame[2].append(len(df[(df["type"] == i) & (df["extracurricular_skills"] == 1)])/len(df[df["type"] == i]))
            frame[3].append(len(df[(df["type"] == i) & (df["language_skills"] == 1)])/len(df[df["type"] == i]))
            frame[4].append(len(df[(df["type"] == i) & (df["ma"] == 1)])/len(df[df["type"] == i]))
            frame[5].append(len(df[(df["type"] == i) & (df["exp_highquality"] == 1)])/len(df[df["type"] == i]))
            frame[6].append(len(df[(df["type"] == i) & (df["reference"] == 1)])/len(df[df["type"] == i]))
            frame[7].append(len(df[(df["type"] == i) & (df["accreditation"] == 1)])/len(df[df["type"] == i]))
            frame[8].append(len(df[(df["type"] == i) & (df["legal"] == 1)])/len(df[df["type"] == i]))

        else:

            X.append(len(df[df["female"] == 1])/len(df))
            X.append(len(df[df["ba_quality"] == 1])/len(df))
            X.append(len(df[df["extracurricular_skills"] == 1])/len(df))
            X.append(len(df[df["language_skills"] == 1])/len(df))
            X.append(len(df[df["ma"] == 1])/len(df))
            X.append(len(df[df["exp_highquality"] == 1])/len(df))
            X.append(len(df[df["reference"] == 1])/len(df))
            X.append(len(df[df["accreditation"] == 1])/len(df))
            X.append(len(df[df["legal"] == 1])/len(df))

    pd.options.display.float_format = '{:,.3f}'.format

    fr1 = pd.DataFrame(frame, columns=columns, index=index)
    fr2 = pd.DataFrame(X, columns=["Full sample"], index=index)

    third_table = pd.concat([fr2,fr1], axis=1)
    third_table = third_table.rename_axis("Charactersistics of resume")

    dfp = pd.DataFrame(df["name_ethnicity"].value_counts()/len(df))
    dfp.set_axis(["Full sample"], axis="columns",inplace=True)

    X = []
    for t in [0,1,2,3,4]:

        X.append(df[df["type"] == t].groupby("name_ethnicity").count()["firmid"]/len(df[df["type"] == t]))

    dfd = np.transpose(pd.DataFrame(X,index=("Type 0","Type 1","Type 2","Type 3","Type 4")))
    share_name = pd.concat([dfp,dfd], axis=1)
    share_name.style.set_caption("Test")

    share_name = share_name.rename_axis("Name ethnicity")

    display_side_by_side(third_table,share_name.fillna(0))

def count_name_frequency():

    df = data_set()
    sns.set_palette("cubehelix",1)
    sns.set_style("whitegrid")

    plt.figure(num=None, figsize=(9,6))
    fig = sns.countplot(data=df,x="name_ethnicity", hue="female")
    plt.xlabel("Name ethnicity")
    plt.ylabel("Frequency")
    plt.title("Frequency of Names")
    plt.show(fig)


text:
import pandas as pd
import sys
sys.path.append("/shares/perception-working/minh/CMU-MultimodalSDK")
import mmsdk
from mmsdk import mmdatasdk
import numpy as np
import os

mosi_path = "/shares/perception-working/minh/openface_mosi/"
output_path = "/shares/perception-working/minh/openface_mosi_segmented/"
dataset=mmdatasdk.mmdataset(mosi_path)
num_features = 713
pseudo_header = []
for i in range(num_features):
    pseudo_header.append("ft_" + str(i))

all_output = []
for user_id in dataset['Opinion Segment Labels'].keys():
    #query data
    try:
        current_intervals = np.array(dataset['Opinion Segment Labels'][user_id]['intervals'])
        current_scores = np.array(dataset['Opinion Segment Labels'][user_id]['features'])
        current_openface = np.array(dataset['OpenFace_2'][user_id]['features'])
    except:
        continue
    index = 0
    time_stamp_col = current_openface[:,1]
    for i in range(current_intervals.shape[0]):
        interval = current_intervals[i]
        current_score = current_scores[i][0]
        bool_arr = np.logical_and(time_stamp_col >= interval[0], time_stamp_col <= interval[1])
        current_segment = current_openface[bool_arr]
        output_file_name = user_id +  str(index) + ".csv"
        current_output_path = os.path.join(output_path,output_file_name)
        pd.DataFrame(current_segment).to_csv(current_output_path, header=pseudo_header, index=False)
        all_output.append([output_file_name, current_score])
        index += 1
    print("Done ", user_id)
pd.DataFrame(all_output).to_csv("../data/mosi_segment_scores.csv", header=None, index=False)
print("here")


text:
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as io
from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.ticker as ticker
plt.switch_backend('agg')

def errorfill(x, y, yerr, color=None, alpha_fill=0.3, ax=None):
    ax = ax if ax is not None else plt.gca()
    if color is None:
        color = ax._get_lines.color_cycle.next()
    if np.isscalar(yerr) or len(yerr) == len(y):
        ymin = y - yerr
        ymax = y + yerr
    elif len(yerr) == 2:
        ymin, ymax = yerr
    ax.plot(x, y, color=color)
    ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill)

def error_bar(actual,pred,epoch,layer):
    actual = actual
    actual = actual.reshape(64,64).transpose()
    pred_mean = np.mean(pred,axis=0).transpose()
    pred_mean = pred_mean.reshape(64,64).transpose()
    pred_diag = np.diag(pred_mean)
    actdiag = np.diag(actual)
    pred_std = np.std(pred,axis=0).transpose()
    pred_std = pred_std.reshape(64,64)
    std_diag = np.diag(pred_std)
    std_val = np.std(actdiag)
    x = np.linspace(0, 64,64)
    errorfill(x, pred_diag, 2*std_diag, 'b')
    plt.plot(x,actdiag,'g')
    plt.savefig('./results/diag_error_{}_layer{}.pdf'.format(epoch,layer),bbox_inches='tight')
    plt.close()

def train_test_error(nll_train,nll_test,epoch):
    plt.figure()
    plt.plot(nll_test, label="Test: {:.3f}".format(np.mean(nll_test[-5:])))
    plt.xlabel('Epoch')
    plt.ylabel(r'NLL')
    plt.legend(loc='lower right')
    plt.savefig("Test_nll.pdf", dpi=600)
    plt.close()
    np.savetxt("./results/Test_nll.txt", nll_test)

def plot_std(samples,epoch):
    plt.imshow(samples, cmap='jet', origin='lower',interpolation='bilinear')
    plt.colorbar()
    plt.tight_layout(pad=0.05, w_pad=0.05, h_pad=0.05)
    plt.savefig('std_%d.pdf'%epoch,
                dpi=300, bbox_inches='tight')
    plt.close()

max_stars_repo_name: zabaras/inn-surrogate

max_issues_repo_name: zabaras/inn-surrogate

max_forks_repo_name: zabaras/inn-surrogate

reason: import numpy,import scipy


text:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

#Condiciones Dircihlet
c0 = 40
c1 = 0
c2 = 25

#puntos
h = 0.1
puntos = int(5/h)

#Matriz indices
indices = np.zeros((puntos, puntos), dtype = int)
for i in range(puntos):
    for j in range(puntos):
        indices[i][j] = i + j * puntos

#Matriz ecuaciones
ecuaciones = np.zeros((puntos ** 2, puntos ** 2))

# Matriz resultados
resultados = np.zeros(puntos ** 2)

#Condiciones Dirichlet
for i in range(puntos):
    #eje i
    resultados[indices[i][0]] = c0
    ecuaciones[indices[i][0]][indices[i][0]] = 1

    resultados[indices[i][puntos - 1]] = c1
    ecuaciones[indices[i][puntos - 1]][indices[i][puntos - 1]] = 1

    #eje j
    resultados[indices[0][i]] = c2
    ecuaciones[indices[0][i]][indices[0][i]] = 1

##Asignamos el resto de las ecuaciones
# -4 * u_ij + u_i+1j + u_ij-1 + u_ij+1 + u_ij-1 = 0
#  -4 * u_ij + 2 * u_i-1j + u_ij+1 + u_ij-1 = 0 Para el borde derecho con u' = 0
#cuidado con los puntos vecinos a los bordes de cond Dirichlet

for i in range(1, puntos):
    for j in range(1, puntos -1):
        indice = indices[i][j]
        ecuaciones[indice][indice] = -4

        #eje j
        if( j -1 == 0):
            resultados[indice] -= c0
            ecuaciones[indice][indices[i][j + 1]] = 1
        elif( j + 1 == puntos - 1):
            resultados[indice] -= c1
            ecuaciones[indice][indices[i][j - 1]] = 1
        else:
            ecuaciones[indice][indices[i][j + 1]] = 1
            ecuaciones[indice][indices[i][j - 1]] = 1

        #eje i

        if( i - 1 == 0):
            resultados[indice] -= c2
            ecuaciones[indice][indices[i + 1][j]] = 1
        elif( i == puntos - 1):
            ecuaciones[indice][indices[i - 1][j]] = 2
        else:
            ecuaciones[indice][indices[i + 1][j]] = 1
            ecuaciones[indice][indices[i - 1][j]] = 1

# ecuaciones * soluciones = resultados
# soluiones = resultados * ecuaciones ^ -1
ecuacionesInv = np.linalg.inv(ecuaciones)
soluciones = np.dot(resultados, ecuacionesInv)

#Graficamos

#Matriz solucion A
A = np.zeros((puntos, puntos))
for i in range(puntos):
    for j in range(puntos):
        A[i][j] = soluciones[indices[i][j]]

plt.imshow(A)
plt.colorbar()

#Ploteo 3d

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = []
Y = []
for i in range(puntos):
    X.append(i*h + h)
    Y.append(i*h + h)
X, Y = np.meshgrid(X, Y)
Z = A

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm ,linewidth=0 , antialiased=False)
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
print(X)
plt.show()

max_stars_repo_path: aux 3/aux3.py

max_issues_repo_path: aux 3/aux3.py

max_forks_repo_path: aux 3/aux3.py


text:
# Copyright (c) Yuta Saito, Yusuke Narita, and ZOZO Technologies, Inc. All rights reserved.
# Licensed under the Apache 2.0 License.

"""Class for Generating Synthetic Logged Bandit Feedback."""
from dataclasses import dataclass
from typing import Optional, Callable

import numpy as np
from scipy.stats import truncnorm
from sklearn.utils import check_random_state

from .base import BaseSyntheticBanditDataset
from ..types import BanditFeedback
from ..utils import sigmoid, softmax

@dataclass
class SyntheticBanditDataset(BaseSyntheticBanditDataset):
    """Class for generating synthetic bandit dataset.

    Note
    -----
    By calling the `obtain_batch_bandit_feedback` method several times,
    we have different bandit samples with the same setting.
    This can be used to estimate confidence intervals of the performances of OPE estimators.

    If None is set as `behavior_policy_function`, the synthetic data will be context-free bandit feedback.

    Parameters
    -----------
    n_actions: int
        Number of actions.

    dim_context: int, default=1
        Number of dimensions of context vectors.

    reward_type: str, default='binary'
        Type of reward variable, must be either 'binary' or 'continuous'.
        When 'binary' is given, rewards are sampled from the Bernoulli distribution.
        When 'continuous' is given, rewards are sampled from the truncated Normal distribution with `scale=1`.

    reward_function: Callable[[np.ndarray, np.ndarray], np.ndarray]], default=None
        Function generating expected reward with context and action context vectors,
        i.e., :math:`\\mu: \\mathcal{X} \\times \\mathcal{A} \\rightarrow \\mathbb{R}`.
        If None is set, context **independent** expected reward for each action will be
        sampled from the uniform distribution automatically.

    behavior_policy_function: Callable[[np.ndarray, np.ndarray], np.ndarray], default=None
        Function generating probability distribution over action space,
        i.e., :math:`\\pi: \\mathcal{X} \\rightarrow \\Delta(\\mathcal{A})`.
        If None is set, context **independent** uniform distribution will be used (uniform random behavior policy).

    random_state: int, default=None
        Controls the random seed in sampling synthetic bandit dataset.

    dataset_name: str, default='synthetic_bandit_dataset'
        Name of the dataset.

    Examples
    ----------

    .. code-block:: python

        >>> import numpy as np
        >>> from obp.dataset import (
            SyntheticBanditDataset,
            linear_reward_function,
            linear_behavior_policy
        )

        # generate synthetic contextual bandit feedback with 10 actions.
        >>> dataset = SyntheticBanditDataset(
                n_actions=10,
                dim_context=5,
                reward_function=logistic_reward_function,
                behavior_policy=linear_behavior_policy,
                random_state=12345
            )
        >>> bandit_feedback = dataset.obtain_batch_bandit_feedback(n_rounds=100000)
        >>> bandit_feedback
        {
            'n_rounds': 100000,
            'n_actions': 10,
            'context': array([[-0.20470766,  0.47894334, -0.51943872, -0.5557303 ,  1.96578057],
                    [ 1.39340583,  0.09290788,  0.28174615,  0.76902257,  1.24643474],
                    [ 1.00718936, -1.29622111,  0.27499163,  0.22891288,  1.35291684],
                    ...,
                    [ 1.36946256,  0.58727761, -0.69296769, -0.27519988, -2.10289159],
                    [-0.27428715,  0.52635353,  1.02572168, -0.18486381,  0.72464834],
                    [-1.25579833, -1.42455203, -0.26361242,  0.27928604,  1.21015571]]),
            'action_context': array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]),
            'action': array([7, 4, 0, ..., 7, 9, 6]),
            'position': array([0, 0, 0, ..., 0, 0, 0]),
            'reward': array([0, 1, 1, ..., 0, 1, 0]),
            'expected_reward': array([[0.80210203, 0.73828559, 0.83199558, ..., 0.81190503, 0.70617705,
                    0.68985306],
                    [0.94119582, 0.93473317, 0.91345213, ..., 0.94140688, 0.93152449,
                    0.90132868],
                    [0.87248862, 0.67974991, 0.66965669, ..., 0.79229752, 0.82712978,
                    0.74923536],
                    ...,
                    [0.64856003, 0.38145901, 0.84476094, ..., 0.40962057, 0.77114661,
                    0.65752798],
                    [0.73208527, 0.82012699, 0.78161352, ..., 0.72361416, 0.8652249 ,
                    0.82571751],
                    [0.40348366, 0.24485417, 0.24037926, ..., 0.49613133, 0.30714854,
                    0.5527749 ]]),
            'pscore': array([0.05423855, 0.10339675, 0.09756788, ..., 0.05423855, 0.07250876,
                    0.14065505])
        }

    """

    n_actions: int
    dim_context: int = 1
    reward_type: str = "binary"
    reward_function: Optional[Callable[[np.ndarray, np.ndarray], np.ndarray]] = None
    behavior_policy_function: Optional[
        Callable[[np.ndarray, np.ndarray], np.ndarray]
    ] = None
    random_state: Optional[int] = None
    dataset_name: str = "synthetic_bandit_dataset"

    def __post_init__(self) -> None:
        """Initialize Class."""
        assert self.n_actions > 1 and isinstance(
            self.n_actions, int
        ), f"n_actions must be an integer larger than 1, but {self.n_actions} is given"
        assert self.dim_context > 0 and isinstance(
            self.dim_context, int
        ), f"dim_context must be a positive integer, but {self.dim_context} is given"
        assert self.reward_type in [
            "binary",
            "continuous",
        ], f"reward_type must be either 'binary' or 'continuous, but {self.reward_type} is given.'"

        self.random_ = check_random_state(self.random_state)
        if self.reward_function is None:
            self.expected_reward = self.sample_contextfree_expected_reward()
        if self.behavior_policy_function is None:
            self.behavior_policy = np.ones(self.n_actions) / self.n_actions
        # one-hot encoding representations characterizing each action
        self.action_context = np.eye(self.n_actions, dtype=int)

    @property
    def len_list(self) -> int:
        """Length of recommendation lists."""
        return 1

    def sample_contextfree_expected_reward(self) -> np.ndarray:
        """Sample expected reward for each action from the uniform distribution."""
        return self.random_.uniform(size=self.n_actions)

    def obtain_batch_bandit_feedback(self, n_rounds: int) -> BanditFeedback:
        """Obtain batch logged bandit feedback.

        Parameters
        ----------
        n_rounds: int
            Number of rounds for synthetic bandit feedback data.

        Returns
        ---------
        bandit_feedback: BanditFeedback
            Generated synthetic bandit feedback dataset.

        """
        assert n_rounds > 0 and isinstance(
            n_rounds, int
        ), f"n_rounds must be a positive integer, but {n_rounds} is given"

        context = self.random_.normal(size=(n_rounds, self.dim_context))
        # sample actions for each round based on the behavior policy
        if self.behavior_policy_function is None:
            behavior_policy_ = np.tile(self.behavior_policy, (n_rounds, 1))
            action = self.random_.choice(
                np.arange(self.n_actions), p=self.behavior_policy, size=n_rounds
            )
        else:
            behavior_policy_ = self.behavior_policy_function(
                context=context,
                action_context=self.action_context,
                random_state=self.random_state,
            )
            action = np.array(
                [
                    self.random_.choice(
                        np.arange(self.n_actions), p=behavior_policy_[i],
                    )
                    for i in np.arange(n_rounds)
                ]
            )
        pscore = behavior_policy_[np.arange(n_rounds), action]

        # sample reward for each round based on the reward function
        if self.reward_function is None:
            expected_reward_ = np.tile(self.expected_reward, (n_rounds, 1))
        else:
            expected_reward_ = self.reward_function(
                context=context,
                action_context=self.action_context,
                random_state=self.random_state,
            )
        expected_reward_factual = expected_reward_[np.arange(n_rounds), action]
        if self.reward_type == "binary":
            reward = self.random_.binomial(n=1, p=expected_reward_factual)
        elif self.reward_type == "continuous":
            min_, max_ = 0, 1e10
            mean, std = expected_reward_factual, 1.0
            a, b = (min_ - mean) / std, (max_ - mean) / std
            reward = truncnorm.rvs(
                a=a, b=b, loc=mean, scale=std, random_state=self.random_state
            )
            # correct expected_reward_, as we use truncated normal distribution here
            mean = expected_reward_
            a, b = (min_ - mean) / std, (max_ - mean) / std
            expected_reward_ = truncnorm.stats(
                a=a, b=b, loc=mean, scale=std, moments="m"
            )
        return dict(
            n_rounds=n_rounds,
            n_actions=self.n_actions,
            context=context,
            action_context=self.action_context,
            action=action,
            position=np.zeros(n_rounds, dtype=int),
            reward=reward,
            expected_reward=expected_reward_,
            pscore=pscore,
        )

def logistic_reward_function(
    context: np.ndarray, action_context: np.ndarray, random_state: Optional[int] = None,
) -> np.ndarray:
    """Logistic mean reward function for synthetic bandit datasets.

    Parameters
    -----------
    context: array-like, shape (n_rounds, dim_context)
        Context vectors characterizing each round (such as user information).

    action_context: array-like, shape (n_actions, dim_action_context)
        Vector representation for each action.

    random_state: int, default=None
        Controls the random seed in sampling dataset.

    Returns
    ---------
    expected_reward: array-like, shape (n_rounds, n_actions)
        Expected reward given context (:math:`x`) and action (:math:`a`), i.e., :math:`q(x,a):=\\mathbb{E}[r|x,a]`.

    """
    assert (
        isinstance(context, np.ndarray) and context.ndim == 2
    ), "context must be 2-dimensional ndarray"
    assert (
        isinstance(action_context, np.ndarray) and action_context.ndim == 2
    ), "action_context must be 2-dimensional ndarray"

    random_ = check_random_state(random_state)
    logits = np.zeros((context.shape[0], action_context.shape[0]))
    # each arm has different coefficient vectors
    coef_ = random_.uniform(size=(action_context.shape[0], context.shape[1]))
    action_coef_ = random_.uniform(size=action_context.shape[1])
    for d in np.arange(action_context.shape[0]):
        logits[:, d] = context @ coef_[d] + action_context[d] @ action_coef_

    return sigmoid(logits)

def linear_reward_function(
    context: np.ndarray, action_context: np.ndarray, random_state: Optional[int] = None,
) -> np.ndarray:
    """Linear mean reward function for synthetic bandit datasets.

    Parameters
    -----------
    context: array-like, shape (n_rounds, dim_context)
        Context vectors characterizing each round (such as user information).

    action_context: array-like, shape (n_actions, dim_action_context)
        Vector representation for each action.

    random_state: int, default=None
        Controls the random seed in sampling dataset.

    Returns
    ---------
    expected_reward: array-like, shape (n_rounds, n_actions)
        Expected reward given context (:math:`x`) and action (:math:`a`), i.e., :math:`q(x,a):=\\mathbb{E}[r|x,a]`.

    """
    assert (
        isinstance(context, np.ndarray) and context.ndim == 2
    ), "context must be 2-dimensional ndarray"
    assert (
        isinstance(action_context, np.ndarray) and action_context.ndim == 2
    ), "action_context must be 2-dimensional ndarray"

    random_ = check_random_state(random_state)
    expected_reward = np.zeros((context.shape[0], action_context.shape[0]))
    # each arm has different coefficient vectors
    coef_ = random_.uniform(size=(action_context.shape[0], context.shape[1]))
    action_coef_ = random_.uniform(size=action_context.shape[1])
    for d in np.arange(action_context.shape[0]):
        expected_reward[:, d] = context @ coef_[d] + action_context[d] @ action_coef_

    return expected_reward

def linear_behavior_policy(
    context: np.ndarray, action_context: np.ndarray, random_state: Optional[int] = None,
) -> np.ndarray:
    """Linear contextual behavior policy for synthetic bandit datasets.

    Parameters
    -----------
    context: array-like, shape (n_rounds, dim_context)
        Context vectors characterizing each round (such as user information).

    action_context: array-like, shape (n_actions, dim_action_context)
        Vector representation for each action.

    random_state: int, default=None
        Controls the random seed in sampling dataset.

    Returns
    ---------
    behavior_policy: array-like, shape (n_rounds, n_actions)
        Action choice probabilities given context (:math:`x`), i.e., :math:`\\pi: \\mathcal{X} \\rightarrow \\Delta(\\mathcal{A})`.

    """
    assert (
        isinstance(context, np.ndarray) and context.ndim == 2
    ), "context must be 2-dimensional ndarray"
    assert (
        isinstance(action_context, np.ndarray) and action_context.ndim == 2
    ), "action_context must be 2-dimensional ndarray"

    random_ = check_random_state(random_state)
    logits = np.zeros((context.shape[0], action_context.shape[0]))
    coef_ = random_.uniform(size=context.shape[1])
    action_coef_ = random_.uniform(size=action_context.shape[1])
    for d in np.arange(action_context.shape[0]):
        logits[:, d] = context @ coef_ + action_context[d] @ action_coef_

    return softmax(logits)

max_stars_repo_name: fullflu/zr-obp

max_issues_repo_name: fullflu/zr-obp

max_forks_repo_name: fullflu/zr-obp

reason: import numpy,from scipy


text:
from sympy.abc import a, b
from sympy import pprint

expr = b * a + -4 * a + b + a * b + 4 * a + (a + b) * 3

print(expr.subs([(a, 3), (b, 2)]))

reason: from sympy


text:
import numpy as np

def histogram_equalize(data, max_val=None, endpoint=False):
    input_shape = np.shape(data)
    data_flat = np.asarray(data).flatten()
    if max_val is None:
        max_val = data_flat.max()
    indices = np.argsort(data_flat)
    replacements = np.linspace(0, max_val, len(indices), endpoint=endpoint)
    data_flat[indices] = replacements
    return data_flat.reshape(*input_shape)

max_stars_repo_name: philshem/python-utils

max_issues_repo_name: philshem/python-utils

max_forks_repo_name: philshem/python-utils