text:
from __future__ import annotations

import inspect
import logging
import os
import re
from collections import abc
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, TypeVar

import numpy as np
import pytorch_lightning as pl
import torch
from more_itertools import always_iterable
from omegaconf import DictConfig, OmegaConf
from torch.optim import Optimizer  # type:ignore
from torch.optim.lr_scheduler import LambdaLR, _LRScheduler

logger = logging.getLogger(__name__)

def get_linear_schedule_with_warmup_and_minimum(
    optimizer: Optimizer,
    num_warmup_steps: int,
    num_training_steps: int,
    last_epoch: int = -1,
    min_ratio: float = 0.0,
    num_work_steps: int = 0,
) -> _LRScheduler:
    """Create a schedule with a learning rate that decreases linearly
    after linearly increasing during a warmup period and stabilizing
    for a working period.

    """

    def lr_lambda(current_step: int) -> float:
        if current_step < num_warmup_steps:
            return float(current_step) / float(max(1, num_warmup_steps))
        elif current_step < num_warmup_steps + num_work_steps:
            return 1.0
        cooldown = num_training_steps - num_warmup_steps - num_work_steps
        return max(
            min_ratio,
            float(num_training_steps - current_step) / float(max(1, cooldown)),
        )

    return LambdaLR(optimizer, lr_lambda, last_epoch)  # type: ignore

TPath = TypeVar("TPath", Sequence[str], str)

def get_path(data_dirs: Sequence[str], name: Optional[TPath]) -> Optional[TPath]:
    def resolve_path_for_one(one_name: str) -> str:
        for one in data_dirs:
            path = Path(one, one_name).expanduser()
            if path.is_file():
                return str(path)
        raise ValueError(f"Can't find {name} in {data_dirs}")

    if isinstance(name, str):
        return resolve_path_for_one(name)
    elif isinstance(name, abc.Sequence):
        return [resolve_path_for_one(x) for x in name]
    elif name is None:
        return None
    raise ValueError(f"Can't find {name} in {data_dirs}")

def fix_path(cfg: DictConfig) -> DictConfig:
    from hydra.utils import to_absolute_path as abs_path

    cfg.data_dirs = list(always_iterable(cfg.data_dirs))
    num_dirs = len(cfg.data_dirs)
    cfg.data_dirs = [
        abs_path(os.path.expanduser(cfg.data_dirs[i])) for i in range(num_dirs)
    ]
    path_patterns = [
        r"^(train|valid|test|cv)_(qrels|query|data)$",
        r"^collection$",
        r"qa_(train|valid|test)_path$",
        r"^(load_checkpoint|resume_from_checkpoint)$",
        r"^(train|valid|test)_(src|tgt)_path$",
    ]

    for key in cfg.keys():
        if any(re.search(pat, key) for pat in path_patterns):
            orig = cfg[key]
            cfg[key] = get_path(cfg.data_dirs, cfg[key])
            logger.debug(f"Resolve {key}: {orig} -> {cfg[key]}")

    return cfg

def get_adamw_params(model: torch.nn.Module[torch.Tensor]) -> List[Dict[str, Any]]:
    no_decay = ["bias", "LayerNorm.weight"]
    optimizer_grouped_parameters = [
        {
            "params": [
                p
                for n, p in model.named_parameters()
                if not any(nd in n for nd in no_decay)
            ],
            "weight_decay": 5e-5,
        },
        {
            "params": [
                p
                for n, p in model.named_parameters()
                if any(nd in n for nd in no_decay)
            ],
            "weight_decay": 0.0,
        },
    ]
    return optimizer_grouped_parameters

def seed_all(seed: int) -> None:
    import random
    import torch

    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)  # type: ignore
    torch.backends.cudnn.deterministic = True  # type: ignore
    torch.backends.cudnn.benchmark = False  # type: ignore

def suppress_trivial_warnings() -> None:
    import os
    import warnings

    os.environ["TOKENIZERS_PARALLELISM"] = "false"
    warnings.filterwarnings(action="ignore", message="Seems like")
    warnings.filterwarnings(action="ignore", message="Did not find hyperparameters")
    warnings.filterwarnings(action="ignore", message="Detected call of")

def log_versions() -> None:
    import transformers
    import pytorch_lightning as pl
    import subprocess
    from hydra.utils import get_original_cwd as cwd

    try:
        cmd = f"git -C {cwd()} describe --dirty --always --long"
        myver = subprocess.check_output(cmd, shell=True, text=True).strip()
    except subprocess.CalledProcessError:
        logger.error("Please run from the root of the code repo.")
        exit(1)

    logger.info(f"HEAD: {myver}")
    logger.info(f"PyTorch: {torch.__version__}")  # type: ignore
    logger.info(f"CUDA: {torch.version.cuda}")  # type: ignore
    logger.info(f"Transformers: {transformers.__version__}")
    logger.info(f"PyTorch-Lightning: {pl.__version__}")

def local_rank() -> int:
    if "LOCAL_RANK" in os.environ:
        return int(os.environ["LOCAL_RANK"])
    return 0

def to_trainer_args(cfg: DictConfig) -> Dict[str, Any]:
    dict_cfg = OmegaConf.to_container(cfg)
    assert isinstance(dict_cfg, abc.Mapping)

    if hasattr(pl.Trainer.__init__, "__wrapped__"):
        args = set(inspect.getfullargspec(pl.Trainer.__init__.__wrapped__).args)
    else:
        args = set(inspect.getfullargspec(pl.Trainer.__init__).args)
    dict_args = {k: v for k, v in dict_cfg.items() if k in args}
    return dict_args

max_stars_repo_name: binshengliu/gdmtl

max_issues_repo_name: binshengliu/gdmtl

max_forks_repo_name: binshengliu/gdmtl


text:
#!/usr/bin/env python

import numpy as np
import copy
import time

from collections import defaultdict

from debug_logger import setupLogging
import logging
from visuals import gridWorld
from common import state, action, mdp

setupLogging()
logger = logging.getLogger(__name__)

logger.info('Start main program')

# dimensions of the grid world
L = 6
W = 6
NUM_HEADINGS = 12

# define the goal position
GOAL_X = 3
GOAL_Y = 4

# define the start position
START_X = 1
START_Y = 2
START_HEAD = 8

# create constant rewards matrix based on problem
# matrix organized relative to coordinate system
# ie. to access reward given for x = 3, y = 2 => reward_matrix[3,2]
def createRewardMatrix(L, W, num_headings):
    '''Creates a rewards matrix based on the problem statement. This matrix is organized relative
    to the coordinate system. ie. to access reward given for x = 3, y = 2 => reward_matrix[3,2,heading]

    Arguments:
        L - int: length of grid
        W - int: width of grid
        num_headings - int: number of headings to consider
    '''

    reward_matrix = np.zeros((W,L,NUM_HEADINGS))
    reward_matrix[:,0,:] = -100
    reward_matrix[:,-1,:] = -100
    reward_matrix[0,:,:] = -100
    reward_matrix[-1,:,:] = -100
    reward_matrix[2,2:5,:] = -10
    reward_matrix[4,2:5,:] = -10
    reward_matrix[3,4,5:8] = 1

    # reward_matrix[3,4,0:2] = 1
    # reward_matrix[3,4,-1] = 1
    return reward_matrix

def createDisplayRewardMatrix(L, W, num_headings):
    '''Creates a rewards matrix based on the problem statement. This matrix is organized relative
    to the rows and columns. ie. to access reward given for r = 2, c = 3 => reward_matrix[2,3,heading]

    Arguments:
        L - int: length of grid
        W - int: width of grid
        num_headings - int: number of headings to consider
    '''
    display_reward_matrix = np.zeros((W,L))
    display_reward_matrix[:,0] = -100
    display_reward_matrix[:,-1] = -100
    display_reward_matrix[0,:] = -100
    display_reward_matrix[-1,:] = -100
    display_reward_matrix[2:5,2] = -10
    display_reward_matrix[2:5,4] = -10
    display_reward_matrix[4,3] = 10

    return display_reward_matrix

def runDynamics(s,a):
    '''Given a state and action, returns a new state based on the defined dynamics of the robot

    Arguments:
        s - state: state class defined in common.py. Current specified state
        a - action: action class defined in common.py. Current specified action
    '''

    # shallow copy to base calculations on without disturbing original state
    new_state = copy.copy(s)

    # change modifier based on whether the robot moves forwards or backwards
    if a.movement == 'forwards':
        move_modifier = 1
    elif a.movement == 'backwards':
        move_modifier = -1
    else:
        logger.error('Movement incorrectly specified in the action.')
        pass

    # based on the heading, compute which direction the robot moves in
    if s.heading >= 2 and s.heading  <= 4:
        new_state.x += move_modifier
    elif s.heading  >= 5 and s.heading  <= 7:
        new_state.y -= move_modifier
    elif s.heading  >= 8 and s.heading  <= 10:
        new_state.x -= move_modifier
    elif s.heading  == 11 or s.heading  == 0 or s.heading  == 1:
        new_state.y += move_modifier
    else:
        print s.heading
        logger.error('Heading incorrectly specified in the state.')

    # check the bounds such that the robot always stays in the state space
    if new_state.x > L-1:
        new_state.x = L-1
    if new_state.y > W-1:
        new_state.y = W-1
    if new_state.x < 0:
        new_state.x = 0
    if new_state.y < 0:
        new_state.y = 0

    # compute the new heading
    if a.rotation == 'right':
        new_state.heading = (new_state.heading + 1) % NUM_HEADINGS
    elif a.rotation == 'left':
        new_state.heading = (new_state.heading - 1) % NUM_HEADINGS
    elif a.rotation == None:
        pass
    else:
        logger.error('Rotation incorrectly specified in action.')

    return new_state

def createTransitionProbabilityTable(S,A,error_probability):
    '''Creates a transition probability table that contains all the system information on how the
    robot should behave given the dynamics. The table is hashed by a (state, action) pair and then
    by a new_state

    Arguments:
        S - set of states: state class defined in common.py. Represents all states
        A - set of actions: action class defined in common.py. Represents all actions
        error_probability - float: the probability that an a pre-rotation will occur
    '''

    transition_probability_dict = {}

    # loop over state space
    for s in S:
        # loop over action space
        for a in A:

            transition_probability_dict[(s,a)] = {}

            # if a robot stays, it will always end up in the same state
            if a.movement == 'stay':
                s_p = s
                transition_probability_dict[(s,a)][s_p] = 1

            # if a robot moves, it can pre-rotate before undertaking an action
            elif a.movement == 'forwards' or a.movement == 'backwards':

                # shallow copy the state to propogate pre-rotation errors
                left_rotated_s = copy.copy(s)
                right_rotated_s = copy.copy(s)

                # simulate pre-rotation errors
                left_rotated_s.heading = (left_rotated_s.heading - 1) % NUM_HEADINGS
                right_rotated_s.heading = (right_rotated_s.heading + 1) % NUM_HEADINGS

                # run the dynamics on the error and non-error states
                s_p = runDynamics(s,a)
                left_rotated_s_p = runDynamics(left_rotated_s,a)
                right_rotated_s_p = runDynamics(right_rotated_s,a)

                # assign probabilities corresponding to the new states
                transition_probability_dict[(s,a)][left_rotated_s_p] = error_probability
                transition_probability_dict[(s,a)][right_rotated_s_p] = error_probability
                transition_probability_dict[(s,a)][s_p] = 1 - 2*error_probability

            else:
                logger.error('The action is not supported.')
    return transition_probability_dict

def initializePolicy(S, A, goal_state):
    '''Initializes policy as the action that will point/move the robot closer to the goal x, y position.
    Computes the direction of the goal state relative to the current position and finds the action with
    a direction vector that best aligns with that vector.

    Arguments:
        S - set of states: state class defined in common.py. Represents all states
        A - set of actions: action class defined in common.py. Represents all actions
        goal_state: action clas
    '''

    pi = {}
    # loop through state space
    for s in S:

        # find rotation matrices based on heading to modify action direction vectors
        if s.heading >= 2 and s.heading  <= 4:
            Rt = np.array([[0, 1],[-1, 0]])
        elif s.heading  >= 5 and s.heading  <= 7:
            Rt = np.array([[-1, 0],[0, -1]])
        elif s.heading  >= 8 and s.heading  <= 10:
            Rt = np.array([[0, -1],[1, 0]])
        elif s.heading  == 11 or s.heading  == 0 or s.heading  == 1:
            Rt = np.array([[1, 0],[0, 1]])
        else:
            print s.heading
            logger.error('Heading incorrectly specified in the state.')

        # initiliaze max finding variables
        initial_action = None
        max_dot_product = -1e10

        # the starting state has the goal position and it does not need to move
        if (s.x == goal_state.x and s.y == goal_state.y):
            initial_action = action('stay',None,np.array([[0],[0]]))
        else:
            # loop over all actions
            for a in A:
                # determine the vector from the current state to the goal state
                state_to_goal_vector = np.array([[goal_state.x-s.x],[goal_state.y-s.y]])

                # rotate the normalized action direction vectors based on the current heading
                action_direction_vector = np.dot(Rt,a.vector)

                # compute the dot product and find the action that most aligns with the direction we want
                direction_dot_product = np.double(np.dot(state_to_goal_vector.T,action_direction_vector))
                if direction_dot_product > max_dot_product:
                    max_dot_product = direction_dot_product
                    initial_action = a

        # assign the best action to the policy
        pi[s] = initial_action

    return pi

def rewardFunctionNonHeadingDependent(reward_matrix, s):
    '''Computes the reward function that does not take into account the heading'''
    return np.max(reward_matrix[s.x,s.y,:])

def rewardFunctionHeadingDependent(reward_matrix, s):
    '''Computes the reward function that accounts for the heading'''
    return reward_matrix[s.x,s.y,s.heading]

def runProblem(error_probability, gamma, heading_dependent):
    '''Runs policy iteration and value iteration for each case'''

    # create lists of all possible position and heading values
    x_values = np.arange(0,L)
    y_values = np.arange(0,W)
    heading_values = np.arange(0,NUM_HEADINGS)
    down_heading_values = np.array([5,6,7])

    # size of the state space
    Ns = L*W*NUM_HEADINGS

    # create state space by pulling each combination of x, y, and heading values
    S = {state(x,y,heading) for x in x_values for y in y_values for heading in heading_values}

    # create the action space
    # action direction vectors are defined where a rotation is a 30 deg turn and forwards is in the positive
    # y direction
    # Note: since we must always go forward when the goal is directly to our right, we slightly adjust the
    # direction vector such that any 90 right or left turns will not align with a backwards movement
    VECTOR_TOLERANCE = 0.001

    A = set()
    A.add(action('forwards',None,np.array([[0], [1]])))
    A.add(action('forwards','right',np.array([[0.5], [np.sqrt(3)/2]])))
    A.add(action('forwards','left',np.array([[-0.5], [np.sqrt(3)/2]])))
    A.add(action('backwards',None,np.array([[0], [-1]])))
    A.add(action('backwards','right',np.array([[0.5-VECTOR_TOLERANCE], [-np.sqrt(3)/2]])))
    A.add(action('backwards','left',np.array([[-0.5+VECTOR_TOLERANCE], [-np.sqrt(3)/2]])))
    A.add(action('stay',None,np.array([[0], [0]])))

    # standard title
    title = '6 x 6 Grid World'

    if heading_dependent:
        # create new possible goal states
        possible_goal_states = {state(GOAL_X, GOAL_Y, heading) for heading in down_heading_values}
        # create a reward function to bind to the mdp
        reward_function = lambda s: rewardFunctionHeadingDependent(reward_matrix, s)

        head_string = '_down'
    else:
        # compute the possible goal states for all heading values
        possible_goal_states = {state(GOAL_X, GOAL_Y, heading) for heading in heading_values}
        # create a reward function to bind to the mdp
        reward_function = lambda s: rewardFunctionNonHeadingDependent(reward_matrix, s)

        head_string = ''

    # create a reward matrix for algorithm use and another to display
    reward_matrix = createRewardMatrix(L, W, NUM_HEADINGS)
    display_reward_matrix = createDisplayRewardMatrix(L, W, NUM_HEADINGS)

    # define goal states and start states
    goal_state = state(GOAL_X,GOAL_Y,0)
    start_state = state(START_X,START_Y,START_HEAD)

    # define the grid world
    grid_world = gridWorld(title, display_reward_matrix, possible_goal_states)

    # create the transition probability table
    transition_probability_dict = createTransitionProbabilityTable(S, A, error_probability)

    # initialize the policy
    initial_policy = initializePolicy(S, A, goal_state)

    # make a new mdp
    mdp_problem = mdp(S, A, transition_probability_dict, reward_function, gamma, possible_goal_states)

    # map the mdp new state function to the simulation
    getNewState = lambda s, a: mdp_problem.getNewState(s,a)

    # compute the initial value
    initial_value = mdp_problem.runPolicyEvaluation(initial_policy)
    logger.info('Value of initial state for initial policy: %f' % initial_value[start_state])

    if not heading_dependent:
        raw_input('Press Enter to show the initial policy')

        # simulate initial policy
        grid_world.runSimulation(getNewState, initial_policy, initial_value, start_state,
                'initial_policy' + str(head_string) + '_pe' + str(error_probability) + '_')

    raw_input('Press Enter to show the Policy Iteration')

    # simulate policy iteration
    policy_iteration_policy, policy_iteration_value = mdp_problem.runPolicyIteration(initial_policy)
    logger.info('Value of initial state for initial policy: %f' % policy_iteration_value[start_state])
    grid_world.runSimulation(getNewState, policy_iteration_policy, policy_iteration_value, start_state,
            'policy_iteration' + str(head_string) + '_pe' + str(error_probability) + '_')

    raw_input('Press Enter to show the Value Iteration')

    # simulate the value iteration
    value_iteration_policy, value_iteration_value = mdp_problem.runValueIteration(initial_policy)
    logger.info('Value of initial state for initial policy: %f' % value_iteration_value[start_state])
    grid_world.runSimulation(getNewState, value_iteration_policy, value_iteration_value, start_state,
            'value_iteration' + str(head_string) + '_pe' + str(error_probability) + '_')

    raw_input('Press Enter to continue')

# discount factor
gamma = 0.9
# default settings
runProblem(0,gamma,False)

# error probability = 0.25
runProblem(0.25,gamma,False)

# heading dependent
runProblem(0,gamma,True)

# heading dependent, error probability = 0.25
runProblem(0.25,gamma,True)

ext: py

reason: import numpy


text:
import sys
sys.path.insert(0, '..')

import agents
import time
import gym_2048
import gym
import numpy

import models.dqn_b.src.model            as Model
import models.dqn_b.src.config           as Config

from common.Training import *
from common.Game2048Wrapper import *

path = "models/dqn_b/"

env = gym.make("2048-v0")
env = Game2048Wrapper(env, 4)
env.reset()

agent = agents.AgentDQN(env, Model, Config)

max_iterations = 60*(10**6)
trainig = TrainingIterations(env, agent, max_iterations, path, 10000)
trainig.run()

'''
agent.load(path)

agent.disable_training()
agent.iterations = 0
while True:
    reward, done = agent.main()

    if done:
        print(env.stats)
        print(env.stats_norm)
        print("\n")
'''


text:
############################### Imports #######################################
import numpy as np
import scipy as sci
from matplotlib import pyplot as plt
from math import pi,degrees,sin,cos,sqrt,asin

############################ Kown Parameters ##################################

g = 9.81                        # Gravity
Vehicle_Weight = 380 + 80       # Weight + Driver
Tire_Radius = 0.318             # Rear Tire Radius
Cd = 0.825                      # Drag Coefficient
Frontal_Area_Cab = 1.84*1.14    # Frontal Area (Drag Calculations)
rho = 1.225                     # Desnsity
Vmax = 50                       # Max Speed of Cab
Vs = Vmax/3.6                   # Max Speed in m/s (Roaling Resistance Calcs)
effelectrical = 0.8             # Expected Electrical Eff
Battery_Cap = 7.2               # Battery Capacity kWh
Pressure_Tire = 2.2             # Tire Pressure (Bar)
Cab_Acceleration = 0.76         # Real World Tested Acceleration (m/s^2)

############################## Calculations ###################################

############################### Functions #####################################

def Acceleration_Distance(Top_End_Speed, Acceleration):
    # Calculate the distance required to get up to cruising speed
    t = Top_End_Speed / Acceleration # Time
    Acceleration_Distance = 0.5 * Acceleration * t**2 # Distance
    return(Acceleration_Distance)

def Congestion_Converter(Congestion_Factor):
    # Function to determine stop - go frequency (m)
    Congestion = 1000 / Congestion_Factor # Max min and medium
    return(Congestion)

def Constant_Speed_Power (Velocity, Weight, Altitude, Distance):
    # Function to determine load under constant speed
    # From test data create polynomial function to determine losses
    No_Load_Data = [0, 200, 400, 600]
    No_Load_Test_Speed = [0,  5.8, 10.3, 14.5]
    No_Load_Poly = np.polyfit(No_Load_Test_Speed, No_Load_Data, 2)
    No_Load_Speed = np.poly1d(No_Load_Poly)
    No_Load_Losses = No_Load_Speed(Velocity) # Gives the losses as a function of speed (Drivetrain)

    theta = asin(Altitude/(Distance*1000)) # Average incline over trip distance (rads)

    Crr = 0.005 + (1 / Pressure_Tire)*(0.01 + 0.0095*(Vs / 100)**2) # Roaling resistance Coefficient

    FORCE_Rolling_Resistance = Crr*Weight*g # Force contributed by Roaling Resistance
    FORCE_Drag = 0.5*Cd*Frontal_Area_Cab*(rho*(Velocity**2)) # Force contributed by Drag
    FORCE_Incline = Weight*g*sin(theta) # Force contributed by Incline
    # Resultant Force
    FORCE_Resultant = FORCE_Rolling_Resistance + FORCE_Drag + FORCE_Incline

    TORQUE_Wheel = FORCE_Resultant*Tire_Radius # Torque Required
    Radial_Wheel_Speed = Velocity/Tire_Radius # Radial wheel speed (Rad/s)

    eff = effelectrical # Electrical contribution to eff

    Pout = TORQUE_Wheel*Radial_Wheel_Speed # Power output
    Pin = (Pout/(eff)) + No_Load_Losses # Required Power Input

    return(Pin)

def Acceleration_Energy_Required (Velocity, Acceleration, WeightA, Altitude, Distance):

    # Function to calculate energy required for acceleration
    V_to_sec = Velocity/3.6 # km/h to m/s
    Acceleration_Time = int(np.round(V_to_sec/Acceleration)) # Time to Accelerate rounded to sec
    Top_V = Acceleration_Time*Acceleration # Top speed from rounded time
    Va = np.linspace(Acceleration, Top_V, Acceleration_Time) # Array with speed value at increments of seconds
    # From test data create polynomial function to determine eff at different speeds
    Motor_Eff = [10, 69.8, 75.1, 71.8, 74.9, 72, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 60]
    Speed = [0, 9.09, 9.23, 9.364, 9.5, 9.636, 9.7, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 15]
    Eff_Poly = np.polyfit(Speed, Motor_Eff,2)
    Eff_Eq = np.poly1d(Eff_Poly)

    theta = asin(Altitude/(Distance*1000)) # Average incline over trip distance (rads)

    Crr = 0.005 + (1 / Pressure_Tire)*(0.01 + 0.0095*(Va / 100)**2) # Roaling resistance Coefficient
    # Forces acting on cab at each increment of speed
    FORCE_Rolling_Resistance = Crr*WeightA*g
    FORCE_Drag = 0.5*Cd*Frontal_Area_Cab*(rho*(Va**2))
    FORCE_Incline = WeightA*g*sin(theta)
    FORCE_Acceleration = Cab_Acceleration*WeightA*np.ones(len(Va))

    FORCE_Resultant = FORCE_Rolling_Resistance + FORCE_Drag + FORCE_Incline + FORCE_Acceleration

    TORQUE_Wheel = FORCE_Resultant*Tire_Radius
    Radial_Wheel_Speed = Va/Tire_Radius

    Pout = TORQUE_Wheel*Radial_Wheel_Speed
    # Devide Pout array by calculated eff at each increment of speed
    Pin = np.divide( Pout, (Eff_Eq(Va)/100)*effelectrical)
    Total = np.sum(Pin) # Sum al the Power in array (W -> J/s and array is per sec so for each part of array the power is used over that second)
    Total_kWh = Total/3600000

    return(Total_kWh)

#################################### Main #####################################
# Required parameters from user
Speed_Input = float(input(f"Please provide average trip speed [km/h]. (This may not exceed vehicle maximum speed, thus between 0km/h and {Vmax}km/h): "))
Congestion_Input = float(input("Please provide opperating area relative congestion. (1 = Open Roads -> 10 = City Center): "))
Weight_Input = float(input(f"Please provide amount of passengers. (1 - 2 passengers): "))
Trip_Input = float(input(f"Please provide average trip distance [km]: "))
Altitude_Input = float(input(f"Please Provide Expected Altitude Gains [m]: "))

Total_Weight = Vehicle_Weight + Weight_Input*75 # Calculate Total weight

# Power Usage for up down and flat Constant Speed
PowerUp = Constant_Speed_Power((Speed_Input/3.6), Total_Weight, Altitude_Input,Trip_Input)
PowerFlat = Constant_Speed_Power((Speed_Input/3.6), Total_Weight, 0, Trip_Input)
PowerDown = Constant_Speed_Power((Speed_Input/3.6), Total_Weight, -Altitude_Input,Trip_Input)
# Power Usage for up down and flat Acceleration
AccelerationUp = Acceleration_Energy_Required(Speed_Input, Cab_Acceleration, Total_Weight, Altitude_Input,Trip_Input)
AccelerationFlat = Acceleration_Energy_Required(Speed_Input, Cab_Acceleration, Total_Weight, 0,Trip_Input)
AccelerationDown = Acceleration_Energy_Required(Speed_Input, Cab_Acceleration, Total_Weight, -Altitude_Input,Trip_Input)
# Intervals between stop go
Distance_Intervals = Congestion_Converter(Congestion_Input)
# Distance acceleration takes place
Acceleration_Interval = Acceleration_Distance(Speed_Input/3.6,Cab_Acceleration)
# Amount of times stop go
Stop_Go_Frequency = (Trip_Input*1000)/Distance_Intervals
# Distance acceleration takes place
Acceleration_Span = Stop_Go_Frequency * Acceleration_Interval
# Distance traveled at cruising speed
Distance_Delta = Trip_Input*1000 - Acceleration_Span
# Time of trip
Distance_Time = Distance_Delta/(Speed_Input/3.6)
# Cruising speed energy
Distance_Energy_Up = PowerUp*0.5*Distance_Time
Distance_Energy_Down = PowerDown*0.5*Distance_Time
Distance_Energy = Distance_Energy_Up + Distance_Energy_Down
# Acceleration energy
Acceleration_Energy = 0.5*Stop_Go_Frequency*AccelerationUp + 0.5*Stop_Go_Frequency*AccelerationDown
# Total energy for the trip
Trip_Energy_kWh = (Distance_Energy / 3600000) + Acceleration_Energy
# Range is how many times the battery can complete the simulated trip times the trip distance.
Range = (Battery_Cap/Trip_Energy_kWh)*Trip_Input
print(Range)

max_stars_repo_path: Vehicle Range Simulations.py

max_stars_repo_name: hornbartho/Vehicle-Simulation

max_issues_repo_path: Vehicle Range Simulations.py

max_issues_repo_name: hornbartho/Vehicle-Simulation

max_forks_repo_path: Vehicle Range Simulations.py

max_forks_repo_name: hornbartho/Vehicle-Simulation

reason: import numpy,import scipy


text:
# <<<<<<<<<<<<<<<<<<<<<<<<<< #
# Feature Engineering Module #
# >>>>>>>>>>>>>>>>>>>>>>>>>> #

import os
import numpy as np
import cv2

def compute_blur(img, norm=100):
    """
    """
    img_bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.Laplacian(img_bw, cv2.CV_64F).std()
    return blur / norm

def compute_img_stats(img_id, img_dir):
    """
    """

    # load image data
    img_path = os.path.join(img_dir, img_id + ".jpg")
    img0 = cv2.imread(img_path)

    # aspect ratio and orientation
    aspect_ratio = img0.shape[0]/img0.shape[1]
    portrait = int(aspect_ratio > 1)
    if portrait == 1:
        aspect_ratio = 1/aspect_ratio

    # RGB channel stats
    r_mu, g_mu, b_mu = img0.mean(axis=(0,1))/255
    r_sd, g_sd, b_sd = img0.std(axis=(0,1))/255

    # Return all stats
    return {
        "id": img_id,
        "portrait": portrait,
        "aspect_ratio": aspect_ratio,
        "resolution": np.sqrt(img0.shape[0]*img0.shape[1])/1280,
        "blur_level": compute_blur(img0),
        "r_mean": r_mu,
        "r_stdev": r_sd,
        "g_mean": g_mu,
        "g_stdev": g_sd,
        "b_mean": b_mu,
        "b_stdev": b_sd
    }

max_stars_repo_name: robbie-manolache/kaggle-pawpular

max_issues_repo_name: robbie-manolache/kaggle-pawpular

max_forks_repo_name: robbie-manolache/kaggle-pawpular


text:
import numpy

coords, folds = [], []

# why not just b = c = [] , you ask?
# in python, b would be a reference to c, updated simultaneously

# numpy 2d arrays are usually y,x instead of x,y

axes = {"x": 1, "y": 0}

with open("data.txt", "r") as fh:
    line = fh.readline()
    while "," in line:
        coords.append( [ int(i) for i in line.strip().split(",") ] )
        line = fh.readline()

    line = fh.readline() # throw away blank line in between

    while "fold" in line:
        fold = line.strip().split("fold along ")[1].split("=")
        fold[1] = int(fold[1])
        folds.append(fold)
        line = fh.readline()

size = numpy.max(numpy.array(coords), axis=0)
mapped = numpy.zeros((size[1]+1, size[0]+1),dtype=numpy.uint8)

for coord in coords:
    mapped[coord[1]][coord[0]] = 1

# print(mapped)
# print("Requested folds:", folds)

merged = mapped.copy()
step = 1

for (axis, pos) in folds:

    # Overall logic:
    # - Make a copy, flipped around the axis.
    # - Get [0:pos] from the unflipped and flipped matrices along the axis.
    # - Add 'em together.
    # - Replace original matrix with this.

    flipped = numpy.flip(merged, axis=axes[axis])

    unflippedrange = merged.take(range(0,pos), axis=axes[axis])
    flippedrange = flipped.take(range(0,pos), axis=axes[axis])

    size = (max(flippedrange.shape[0], unflippedrange.shape[0]), max(flippedrange.shape[1], unflippedrange.shape[1]))

    # For the flipped one, let's pad the END of each axis with 0s, so it pivots around the fold.

    flippedrange = numpy.pad(flippedrange, pad_width=[(0, size[0] - flippedrange.shape[0]), (0, size[1] - flippedrange.shape[1])])

    # Unflipped, let's pad the BEGINNING of each axis with 0s.

    unflippedrange = numpy.pad(unflippedrange, pad_width=[(size[0] - unflippedrange.shape[0], 0), (size[1] - unflippedrange.shape[1], 0)])

    # Just add together the ints in each position. Some will exceed 1, we'll clean up later.

    merged = flippedrange + unflippedrange

    flat = merged.copy().flatten()
    print("after fold",step,"the merged count is:",numpy.count_nonzero(flat))

    # print("MERGED")
    # print(merged)
    # print("UNFLIPPED")
    # print(unflippedrange)
    # print("FLIPPED")
    # print(flippedrange)

    step += 1

numpy.set_printoptions(linewidth=200)

# let's arbitrarily use "8" and "1" as our human-readable "on" and "off" for output bitmap

merged[numpy.where(merged > 1)] = 8
merged[numpy.where(merged == 0)] = 1
print(merged)

# squint!


text:
"""
Este script resuelve un problema simple de diffusion en 1D.
La ecuación a resover es:
    dT/dt = d2T/dx2;
    T(0,x) = sin(pi * x);
    T(t, 0) = T(t, 1) = 0
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import diags
from scipy.linalg import solve

# Setup
Nx = 35
h = 1 / (Nx - 1)

N_temporales = 400
# eps = h**2 / 2
eps = 0.001

s = eps / 2 / h**2

T = np.zeros((N_temporales, Nx))

# Condicion Inicial
T[0] = np.sin(np.pi * np.arange(Nx)*h)

# matrices tridiagonales
M_derecha = diags((s, (1-2*s), s), (-1, 0, 1), shape=(Nx-2, Nx-2))
M_izquierda = diags((-s, (1 + 2*s), -s), (-1, 0, 1), shape=(Nx-2, Nx-2))

# Avances en el tiempo
for i in range(1, N_temporales):
    b = np.matmul(M_derecha.toarray(), T[i-1, 1:-1])
    T[i, 1:-1] = solve(M_izquierda.toarray(), b)

# Graficos
x = np.arange(Nx) * h
time = np.arange(N_temporales) * eps

plt.figure(1)
plt.clf()

for i in range(0, N_temporales, 25):
    label = "T={:.3f}".format(eps * i)
    plt.plot(x, T[i], label=label)

plt.xlabel('x')
plt.ylabel('Temperatura')
plt.legend()
plt.show()

plt.figure(2)
plt.clf()

X, Time = np.meshgrid(x, time)
plt.pcolormesh(X, Time, T)

plt.xlabel("x")
plt.ylabel("tiempo [unidades]")

plt.show()

plt.figure(3)
plt.clf()

plt.plot(time, T[:, int(0.5*(Nx-1))])
plt.xlabel("tiempo")
plt.ylabel('Temperatura')

plt.show()

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-

"""Keras unit tests"""

import pytest

keras = pytest.importorskip('keras')

import keras.backend as K
from keras.models import Sequential, Model
from keras.layers import (
    Dense,
    Activation,
    Conv2D,
    GlobalAveragePooling2D,
    Input,
    Lambda,
)
from keras.backend import epsilon
import numpy as np

from eli5.keras.explain_prediction import (
    explain_prediction,
    _validate_doc,
    _get_activation_layer,
)
from eli5.keras.gradcam import (
    _get_target_prediction,
    _calc_gradient,
    gradcam,
)

# We need to put this layer in a fixture object AND access it in a parametrization
conv_layer = Conv2D(10, (3, 3))

@pytest.fixture(scope='module')
def simple_seq():
    """A simple sequential model for images."""
    model = Sequential([
        Activation('linear', input_shape=(32, 32, 1)), # index 0, input
        conv_layer,                                    # index 1, conv
        Conv2D(20, (3, 3)),                            # index 2, conv2
        GlobalAveragePooling2D(),                      # index 3, gap
        # output shape is (None, 20)
    ])
    print('Summary of model:')
    model.summary()
    # rename layers
    for i, layer in enumerate(model.layers):
        layer.name = 'layer%d' % i
    return model

# layer is the argument to _get_activation_layer
# expected_layer is a unique layer name
@pytest.mark.parametrize('layer, expected_layer', [
    (-3, 'layer1'), # index backwards
    ('layer0', 'layer0'), # name
    (conv_layer, 'layer1'), # instance
    (None, 'layer2'), # automatic, first matching layer going back from output layer
])
def test_get_activation_layer(simple_seq, layer, expected_layer):
    """Test different ways to specify activation layer, and automatic activation layer getter"""
    assert _get_activation_layer(simple_seq, layer) == simple_seq.get_layer(name=expected_layer)

def test_get_activation_layer_invalid(simple_seq):
    # invalid layer shape
    with pytest.raises(ValueError):
        # GAP has rank 2 shape, need rank 4
        _get_activation_layer(simple_seq, 'layer3')
    # invalid layer type
    with pytest.raises(TypeError):
        _get_activation_layer(simple_seq, 2.5)
    # can not find activation layer automatically
    # this is handled by _search_layer_backwards function
    with pytest.raises(ValueError):
        _get_activation_layer(
            Sequential(), # a model with no layers
            None,
        )

    # note that cases where an invalid layer index or name is passed are
    # handled by the underlying keras get_layer method()

def test_validate_doc(simple_seq):
    # should raise no errors
    _validate_doc(simple_seq, np.zeros((1, 32, 32, 1)))
    # batch has more than one sample
    with pytest.raises(ValueError):
        _validate_doc(simple_seq, np.zeros((3, 32, 32, 1)))
    # type is wrong
    with pytest.raises(TypeError):
        _validate_doc(simple_seq, 10)
    # incorrect dimensions
    with pytest.raises(ValueError):
        _validate_doc(simple_seq, np.zeros((5, 5)))

def test_validate_doc_custom():
    # model with custom (not rank 4) input shape
    model = Sequential([Dense(1, input_shape=(2, 3))])
    # not matching shape
    with pytest.raises(ValueError):
        _validate_doc(model, np.zeros((5, 3)))

def test_get_target_prediction_invalid(simple_seq):
    # only list of targets is currently supported
    with pytest.raises(TypeError):
        _get_target_prediction('somestring', simple_seq)
    # only one target prediction is currently supported
    with pytest.raises(ValueError):
        _get_target_prediction([1, 2], simple_seq)

    # these are dispatched to _validate_target
    # only an integer index target is currently supported
    with pytest.raises(TypeError):
        _get_target_prediction(['someotherstring'], simple_seq)
    # target index must correctly reference one of the nodes in the final layer
    with pytest.raises(ValueError):
        _get_target_prediction([20], simple_seq)

def test_explain_prediction_score(simple_seq):
    expl = explain_prediction(simple_seq, np.zeros((1, 32, 32, 1)))
    assert expl.targets[0].score is not None
    assert expl.targets[0].proba is None

@pytest.fixture(scope='module')
def differentiable_model():
    inpt = Input(shape=(1,))
    op = Lambda(lambda x: x)(inpt) # identity function
    model = Model(inpt, op)
    model.summary()
    return model

@pytest.fixture(scope='module')
def nondifferentiable_model():
    inpt = Input(shape=(1,))
    op = Lambda(lambda x: K.constant(0) if x == 0
        else K.constant(1))(inpt) # piecewise function
    model = Model(inpt, op)
    model.summary()
    return model

def test_calc_gradient(differentiable_model):
    _calc_gradient(differentiable_model.output,
        [differentiable_model.input])

def test_calc_gradient_nondifferentiable(nondifferentiable_model):
    with pytest.raises(ValueError):
        grads = _calc_gradient(nondifferentiable_model.output,
            [nondifferentiable_model.input])

def test_gradcam_zeros():
    activations = np.zeros((2, 2, 3)) # three 2x2 maps
    weights = np.zeros((3,)) # weight for each map
    lmap = gradcam(weights, activations)
    # all zeroes
    assert np.count_nonzero(lmap) == 0

def test_gradcam_ones():
    activations = np.ones((1, 1, 2))
    weights = np.ones((2,))
    lmap = gradcam(weights, activations)
    # all within eps distance to one
    assert np.isclose(lmap, np.ones((1, 1)), rtol=epsilon())


text:
#!/usr/bin/env python
#########################################################################################
#
# This script generates multiple images from a warping field.
#
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2015 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: Benjamin De Leener
# Modified: 2015-04-27
#
# About the license: see the file LICENSE.TXT
#########################################################################################

from __future__ import absolute_import, division

from spinalcordtoolbox.image import Image
from spinalcordtoolbox.utils import init_sct
from scipy.misc import toimage

import sct_utils as sct
from sct_utils import run, printv, extract_fname

class WarpingField(Image):
    def __init__(self, param=None, hdr=None, orientation=None, absolutepath="", verbose=1):
        super(WarpingField, self).__init__(param, hdr, orientation, absolutepath, verbose)
        self.num_of_frames = 5
        self.iteration = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.iteration <= self.num_of_frames:
            result = Image(self)
            sct.printv("Iteration #" + str(self.iteration))
            result.data *= float(self.iteration) / float(self.num_of_frames)
            result.file_name = "tmp." + result.file_name + "_" + str(self.iteration)
            self.iteration += 1
            return result, self.iteration
        else:
            raise StopIteration()

if __name__ == "__main__":
    init_sct()

    # TODO: Convert to argparse when fixing this script
    # from msct_parser import Parser
    import sys

    parser = Parser(__file__)
    parser.usage.set_description('This script generates multiple images from a warping field.')
    parser.add_option(name="-i",
                      type_value="file",
                      description="source image (moving). Can be 3D or 4D.",
                      mandatory=True,
                      example="t2.nii.gz")
    parser.add_option(name="-o",
                      type_value="file_output",
                      description="output file.",
                      mandatory=True,
                      example="movie.nii.gz")
    parser.add_option(name="-d",
                      type_value="file",
                      description="destination image (fixed). Must be 3D.",
                      mandatory=True,
                      example="template.nii.gz")
    parser.add_option(name="-w",
                      type_value=[[','], 'file'],
                      description="warping field. If more than one, separate with ','.\nThe movie generator currently supports only warping field and not affine transformation.",
                      mandatory=True,
                      example="warping_field.nii.gz")
    parser.add_option(name="-n",
                      type_value='int',
                      description="number of frames.",
                      mandatory=False,
                      example="20",
                      default_value=5)
    arguments = parser.parse(sys.argv[1:])

    input_file = arguments["-i"]
    output_file = arguments["-o"]
    reference_image = arguments["-d"]
    warping_fields_filename = arguments["-w"]
    number_of_frames = arguments["-n"]

    warping_fields = [WarpingField(filename) for filename in warping_fields_filename]

    filenames_output = []

    # path_tmp = sct.tmp_create()
    # tmp_copy_nifti(input_file, path_tmp, 'raw.nii')
    # sct.run('cp '+warping_fields_filename[0]+' '+path_tmp)
    # curdir = os.getcwd()
    # os.chdir(path_tmp)
    sct.mkdir("images")
    sct.mkdir("niftis")
    while True:
        try:
            warping_fields[0].num_of_frames = number_of_frames
            image_output_iter, iteration = next(warping_fields[0])
            image_output_iter.save()
            filename_warp = image_output_iter.path + image_output_iter.file_name + image_output_iter.ext
            filename_output = "niftis/tmp.warped_image_" + str(iteration - 1) + image_output_iter.ext
            sct.run(["sct_apply_transfo",
             "-i", input_file,
             "-d", reference_image,
             "-w", filename_warp,
             "-o", filename_output])
            result = Image(filename_output).change_orientation("RPI")

            toimage(result.data[int(result.data.shape[0] / 2)].squeeze(), cmin=0.0).save('images/' + extract_fname(filename_output)[1] + '.jpg')
            filenames_output.append(filename_output)
        except ValueError:
            printv('\nError during warping field generation...', 1, 'error')
        except StopIteration:
            printv('\nFinished iterations.')
            break

max_stars_repo_licenses: Unlicense MIT

max_issues_repo_licenses: Unlicense MIT

max_forks_repo_licenses: Unlicense MIT

reason: from scipy


text:
import requests
import numpy as np
import pickle
import csv
from gensim.parsing import preprocessing
import re
from tqdm import tqdm
import contractions
from copy import deepcopy
tqdm.pandas()
import os
import pandas as pd
import json
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("dbname=MAG19 user=mag password=1maG$ host=shetland.informatik.uni-freiburg.de")
cur = conn.cursor()
query="""SELECT originaltitle, publishedyear, citationcount FROM papers WHERE paperid in {}"""

query = """SELECT originaltitle, publishedyear, citationcount, abstract
           FROM papers LEFT JOIN paperabstracts on papers.paperid = paperabstracts.paperid
           WHERE papers.paperid=%(paperid)s
        """
from HyperDoc2Vec import *
hd2vmodel = HyperDoc2Vec.load('/home/ashwath/Programs/MAGCS/MAG-hyperdoc2vec/models/magcsenglish_window20.model')

from numpy.random import choice
from copy import deepcopy
from collections import Counter

# GLOBAL calculation for weights (same for all recommendations)
# Weights: reciproacal ranks of the 500 items in each list (hd2v and bm25)
hybrid_weights = [1/(i+1) for i in range(500)]
hybrid_weights.extend(hybrid_weights)
hybrid_weights = np.array(hybrid_weights)
# Convert to probabilities
hybrid_weights = hybrid_weights/hybrid_weights.sum()
# GLOBAL num_items_to_pick (with replacement) -- high number: half a million
num_picks = 1000000

topn = 500
def clean_text(text):
    """ Cleans the text in the only argument in various steps
    ARGUMENTS: text: content/title, string
    RETURNS: cleaned text, string"""
    # Replace newlines by space. We want only one doc vector.
    text = text.replace('\n', ' ').lower()
    # Remove URLs
    text = re.sub(r"http\S+", "", text)
    # Expand contractions: you're to you are and so on.
    text = contractions.fix(text)
    # Remove stop words
    text = preprocessing.remove_stopwords(text)
    # Remove punctuation -- all special characters
    text = preprocessing.strip_multiple_whitespaces(preprocessing.strip_non_alphanum(text))
    return text

def hd2v_wvindvout_recommend(context):
    """ Recommend based on the hyperdoc2vec model using IN and OUT vectors"""
    context_words_list = context.split()
    word_vocabs = [hd2vmodel.wv.vocab[w] for w in context_words_list if w in hd2vmodel.wv.vocab]
    word2_indices = [word.index for word in word_vocabs]
    # Get the sum of the IN word vectors
    l1 = np.sum(hd2vmodel.wv.syn0[word2_indices], axis=0)
    if word2_indices:
        l1 /= len(word2_indices)
    # Following hd2v code, e^(sumwvIN.docvecIN + sumwvOUT.docvecOUT)
    prob_values=exp(dot(l1, hd2vmodel.docvecs.doctag_syn1neg.T))
    prob_values = nan_to_num(prob_values)
    top_indices = matutils.argsort(prob_values, topn=topn, reverse=True)
    return [hd2vmodel.docvecs.offset2doctag[index1] for index1 in top_indices]

def solr_recommend(context):
    """ """
    num_rows = 500
    pred_docs_dict = search_solr_parse_json(context, 'mag_en_cs', 'content', num_rows)
    # Get only ids
    if pred_docs_dict is None:
        return None
    pred_docs_list = [doc['paperid'] for doc in pred_docs_dict]
    return pred_docs_list

def search_solr_parse_json(query, collection, search_field, num_rows):
    """ Searches the nounphrases collection on query,
    parses the json result and returns it as a list of dictionaries where
    each dictionary corresponds to a record.
    ARGUMENTS: query, string: the user's query entered in a search box
               (if it is comma-separated, only one part of the query is sent
               to this function).
               collection: the Solr collection name (=nounphrases)
               search_field: the Solr field which is queried (=phrase)
    RETURNS: docs, list of dicts: the documents (records) returned by Solr
             AFTER getting the JSON response and parsing it."""
    solr_url = 'http://localhost:8983/solr/' + collection + '/select'
    # Exact search only
    #query = '"' + query + '"'
    url_params = {'q': query, 'rows': num_rows, 'df': search_field}
    solr_response = requests.get(solr_url, params=url_params)
    #print(solr_response.url)
    if solr_response.ok:
        data = solr_response.json()
        docs = data['response']['docs']
        return docs
    else:
        print("Invalid response returned from Solr for", solr_response)
        return None
        #sys.exit(11)

def hybrid_recommend(hd2v_recommendations_wv0dv1, bm25_recommendations):
    """ Treat them as equal (50:50 importance)"""
    combined_recommendations = deepcopy(hd2v_recommendations_wv0dv1)
    weights = [1/(i+1) for i in range(500)]
    weights.extend(weights)
    weights = np.array(weights)
    combined_recommendations.extend(bm25_recommendations)
    combined_recommendations = np.array(combined_recommendations)
    draw = choice(combined_recommendations, num_picks, p=hybrid_weights)
    # Now, we have drawn 500000 times with replacement, we expect the recommended
    # ids with high probabilities to be drawn more. Recommendations in both the
    # recommendation lists (with 2 separate probabilities) will be drawn even more.
    # Create a Counter with number of times each recommended id is drawn.
    draw_frequencies = Counter(draw)
    rankedlist = [paperid for paperid, draw_count in draw_frequencies.most_common(500)]
    return rankedlist

def get_top10(recommendations_list):
    """ Returns the first 10 elements of a list"""
    return recommendations_list[:10]

def get_paper_details(recommendations_list):
    """ Gets details from the DB for the list of paper ids in the recommendations list"""
    cur.execute("""SELECT papers.paperid, originaltitle, publishedyear, citationcount, abstract
                    FROM papers
                    LEFT JOIN  paperabstracts on papers.paperid = paperabstracts.paperid
                    WHERE papers.paperid in {}""".format(tuple(recommendations_list)))
    return cur.fetchall()

df = pd.read_csv('NLPcontexts_grouped.tsv', sep='\t', encoding='utf-8') #15446 rows.
df['ground_truth'] = df['ground_truth'].astype('str').apply(lambda x: x.split(','))
df['cleanedcontext'] = df['context'].progress_apply(clean_text)
sample_df = df.sample(n=100, random_state=8)

sample_df['hd2v_recommendations_wv0dv1'] = sample_df['cleanedcontext'].progress_apply(hd2v_wvindvout_recommend)
sample_df['bm25_recommendations'] = sample_df['cleanedcontext'].progress_apply(solr_recommend)
hybrid_models_cols = ['hd2v_recommendations_wv0dv1', 'bm25_recommendations']
sample_df['hybrid_recommendations'] = sample_df[hybrid_models_cols].progress_apply(
          lambda x: hybrid_recommend(x.hd2v_recommendations_wv0dv1, x.bm25_recommendations), axis=1)
sample_df['hybrid_recommendations'] = sample_df['hybrid_recommendations'].apply(get_top10)
sample_df['bm25_recommendations'] = sample_df['bm25_recommendations'].apply(get_top10)
sample_df['hd2v_recommendations_wv0dv1'] = sample_df['hd2v_recommendations_wv0dv1'].apply(get_top10)

#sample_df[dbcols] = sample_df['hd2v_recommendations_wv0dv1'].apply(get_paper_details, axis=1, result_type='expand')
sample_df['hd2v_recommendations_metadata'] = sample_df['hd2v_recommendations_wv0dv1'].progress_apply(get_paper_details)
sample_df['bm25_recommendations_metadata'] = sample_df['bm25_recommendations'].progress_apply(get_paper_details)
sample_df['hybrid_recommendations_metadata'] = sample_df['hybrid_recommendations'].progress_apply(get_paper_details)
sample_df.to_pickle('sample_100_top10_3models.pickle')

cols_for_json = ['context', 'hd2v_recommendations_metadata', 'bm25_recommendations_metadata', 'hybrid_recommendations_metadata']
context_recommendations = sample_df[cols_for_json].values.tolist()

jsonarray = []
for context_recommendation in context_recommendations:
    # 0 is context, 1 is hd2v_recommendations_metadata, 2 is bm25_recommendations_metadata,
    # 3 is hybrid_recommendations_metadata
    jsonarray.append({
            'context': context_recommendation[0],
            'hd2v': context_recommendation[1],
            'bm25': context_recommendation[2],
            'hybrid': context_recommendation[3]
            })

with open('online_eval_recommendations_100.json', 'w') as jsonfile:
    json.dump(jsonarray, jsonfile, indent=4)

reason: import numpy,from numpy


text:
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
import pandas
import numpy as np
from helpers import load_obj

def plot():
	x = load_obj('x-50000-samples-14:00')[0:1000, 0:11]
	y = load_obj('y-50000-samples-14:00')[0:1000, None]

	# Plot correlation between
	names = ['queue', 'doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9', 'doc10', 'time']

	fm = np.concatenate((x, y), axis=1)

	data = pandas.DataFrame(fm, columns=names)
	scatter_matrix(data)
	plt.show()

max_stars_repo_name: axelniklasson/HelpER

max_issues_repo_name: axelniklasson/HelpER

max_forks_repo_name: axelniklasson/HelpER


text:
"""
Runge-Kutta method and Mackey-Glass series
"""

import numpy as np

# pylint: disable=R0913
def time_delay_runge_kutta_4(fun, t_0, y_0, tau, history=None, steps=1000,
                             width=1):
    """
    apply the classic Runge Kutta method to a time delay differential equation

    f: t, y(t), y(t-tau) -> y'(t)
    """
    width = float(width)
    if not isinstance(y_0, np.ndarray):
        y_0 = np.ones((1,), dtype=np.float)*y_0
    dim = len(y_0)

    hist_steps = np.floor(tau/width)
    assert tau/width == hist_steps, "tau must be a multiple of width"
    hist_steps = int(hist_steps)

    if history is None:
        history = np.zeros((hist_steps, dim), dtype=np.float)
    else:
        assert len(history) == hist_steps

    fun_eval = np.zeros((steps+1+hist_steps, dim), dtype=y_0.dtype)
    fun_eval[:hist_steps] = history
    fun_eval[hist_steps] = y_0

    for step in range(steps):
        k_1 = fun(t_0, y_0, fun_eval[step])
        k_2 = fun(t_0 + width/2, y_0 + width/2*k_1, fun_eval[step])
        k_3 = fun(t_0 + width/2, y_0 + width/2*k_2, fun_eval[step])
        k_4 = fun(t_0 + width, y_0 + width*k_3, fun_eval[step])
        t_0 += width
        y_0 += width*(k_1 + 2*k_2 + 2*k_3 + k_4)/6
        fun_eval[step+1+hist_steps] = y_0
    return fun_eval[hist_steps:]

# mackey glass does not depend on time explicitly, disable "unused arg"
# pylint: disable=W0613
# difference equations just need that many arguments, deal with it
# pylint: disable=R0913
def mackey_glass(beta, gamma, tau, exponent, steps=1000, width=.1):
    """
    approximation for Mackey-Glass-Function

    definition from http://www.hindawi.com/journals/ddns/2014/193758/
    """
    def mg_fun(time, fun, fun_delay):
        """
        Mackey-Glass time series depends only on current and delayed value
        """
        return beta*fun_delay/(1+fun_delay**exponent) + gamma*fun

    return time_delay_runge_kutta_4(mg_fun, 0, 1.2, tau, steps=steps,
                                    width=width)

def default_mackey_glass_series():
    """
    get an interesting MG-series for learning
    """
    return mackey_glass(.2, -.1, 17, 10, steps=12000, width=.1)

# uncomment to visualize Mackey-Glass series
# pylint: disable=W0105
'''
import vigra
from matplotlib import pyplot as plt

from tsdl.targets import OpExponentiallySegmentedPattern
from tsdl.tools import Graph

def main():
    plt.figure()
    mg_series = default_mackey_glass_series()
    timesteps = np.arange(len(mg_series))
    plt.plot(timesteps, mg_series)
    plt.hold(True)

    mg_series = vigra.taggedView(mg_series, axistags='tc').withAxes('t')

    mean = OpExponentiallySegmentedPattern(graph=Graph())
    mean.Input.setValue(mg_series)
    mean.NumSegments.setValue(1)

    legend = ["MackeyGlass"]
    for num in (8, 32, 128):
        mean.BaselineSize.setValue(num)
        smoothed = mean.Output[:].wait().squeeze()
        plt.plot(timesteps, smoothed)
        legend.append("mean over {}".format(num))

    plt.legend(legend)
    plt.show()

if __name__ == "__main__":
    main()
'''

max_stars_repo_name: burgerdev/hostload

max_issues_repo_name: burgerdev/hostload

max_forks_repo_name: burgerdev/hostload


text:
# coding: utf-8
# 2021/2/13 @ tongshiwei

import logging
import numpy as np

class ReplayBuffer(object):
    def __init__(self, logger=logging, seed=None):
        self._buffer = []
        self.logger = logger
        self._random_state = np.random.RandomState(seed)

    def add(self, *args, **kwargs):
        raise NotImplementedError

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

    def __getitem__(self, item):
        return self._buffer[item]

    def reset(self):
        self._buffer = []

    @property
    def size(self):
        return len(self._buffer)

    def sample(self, size):
        if size > len(self._buffer):
            self.logger.warning("requiring size bigger than the buffered, only return %s" % len(self._buffer))
            return self._buffer
        else:
            idx = self._random_state.randint(0, len(self) - 1, size)
            return [self._buffer[i] for i in idx]

class InfinityReplayBuffer(ReplayBuffer):
    def __init__(self, logger=logging, seed=None, *args, **kwargs):
        super(InfinityReplayBuffer, self).__init__(logger, seed)

    def add(self, item):
        self._buffer.append(item)

class FiniteReplayBuffer(ReplayBuffer):
    def __init__(self, max_size=1000000, logger=logging, seed=None, *args, **kwargs):
        super(FiniteReplayBuffer, self).__init__(logger, seed)
        self.max_size = max_size
        self.top = 0

    def add(self, *args, **kwargs):
        raise NotImplementedError

max_stars_repo_name: bigdata-ustc/EduRec

max_issues_repo_name: bigdata-ustc/EduRec

max_forks_repo_name: bigdata-ustc/EduRec


text:
#!/usr/bin/env python

# Display of NetworkX reference graphs using
# biofabric and GraphViz-provided layout

import biofabric
import networkx
import matplotlib.pyplot as plt

generators = (
	(networkx.generators.classic.complete_graph, (10,)),
	(networkx.generators.classic.grid_2d_graph, (5, 5)),
	(networkx.generators.classic.path_graph, (10,)),
	(networkx.generators.classic.ladder_graph, (5,)),
	(networkx.generators.classic.cycle_graph, (10,)),
	(networkx.generators.classic.star_graph, (10,)),
	(networkx.generators.classic.wheel_graph, (10,)),
	(networkx.generators.random_graphs.barabasi_albert_graph, (20, 5)),
	(networkx.generators.social.karate_club_graph, ()),
	(networkx.generators.social.davis_southern_women_graph, ()),
	(networkx.generators.social.florentine_families_graph, ()),
)

for (generator, args) in generators:
	g = generator(*args)

	print "processing '{}' ({:,} nodes, {:,} edges) ...".format(
		generator.__name__, g.number_of_nodes(), g.number_of_edges())

	# draw with biofabric
	biofabric.draw(g, "demo_networkx_graphs/%s-biofabric.png" % generator.__name__)

	# draw with networkx's Matplotlib backend
	plt.figure()
	networkx.draw(g, pos = networkx.graphviz_layout(g, prog = "neato"))
	plt.savefig("demo_networkx_graphs/%s-networkx.png" % generator.__name__)

max_stars_repo_name: brennengreen/biofabric

max_issues_repo_name: brennengreen/biofabric

max_forks_repo_name: brennengreen/biofabric

reason: import networkx


text:
import numpy as np
from numpy import pi, sin, cos
import matplotlib.pyplot as plt

T1 = 600
T2 = 400
Fo = 15
df = np.arange(0, 50, 0.1)

theta = pi
theta2 = pi/2.0
T = 1600
dT = 1

T2inom = 200
T22 = 1/(1/float(T2) + (1/float(T2inom)))

Rtheta = np.matrix([[1, 0, 0], [0, cos(theta), sin(theta)], [0, -sin(theta), cos(theta)]])
Rtheta2 = np.matrix([[1, 0, 0], [0, cos(theta2), sin(theta2)], [0, -sin(theta2), cos(theta2)]])
E1 = np.exp(-dT/float(T1))
E2 = np.exp(-dT/float(T2))
E = np.matrix([[E2, 0, 0], [0, E2, 0], [0, 0, E1]])
B = np.matrix([0, 0, 1 - E1])

Tn1 = 100

N0 = int(T/float(dT))
N1 = int(Tn1/float(dT))
N2 = N0 - N1

M = np.zeros((N0, 3))
Ms = np.zeros((N0, 3))

M[0] = [0, 0, 1]

for f in range(0, len(df)):
    phi = 2*pi*(df[f])*dT/1000.0
    Rphi = np.matrix([[cos(phi), -sin(phi), 0], [sin(phi), cos(phi), 0], [0, 0, 1]])
    M[1] = Rtheta @ M[0] + B

    for k in range(2, N1 + 1):
        M[k] = E @ Rphi @ M[k - 1] + B

    M[N1 + 1] = Rtheta2 @ M[N1] + B

    for k in range(1, N2 - 1):
        M[k + N1 + 1] = E @ Rphi @ M[k + N1] + B

    g = T22 / ((1 + (df[f] - Fo)**2) * (2*pi*T22/1000)**2)

    Ms[:, 0] = g*M[:, 0] + Ms[:, 0]
    Ms[:, 1] = g*M[:, 1] + Ms[:, 1]
    Ms[:, 2] = g*M[:, 2] + Ms[:, 2]

Ms = Ms/max(Ms[0])

tempo = np.arange(0, N0).T * dT

# ===== Graficando Resultados =====

# plt.plot(tempo, CurvaT22, 'y')
plt.plot(tempo, Ms[:, 0], 'b')
plt.plot(tempo, Ms[:, 1], 'k')
plt.plot(tempo, Ms[:, 2], 'r')
plt.grid()

plt.show()

max_stars_repo_name: camposouza/NMR

max_issues_repo_name: camposouza/NMR

max_forks_repo_name: camposouza/NMR


text:
import numpy as np

from nuspacesim import eas_optical

def test_known_values():
    altitude = np.arange(0, 21, dtype=np.float32)
    beta_tr = np.full_like(altitude, np.radians(42.0), dtype=np.float32)
    cpa = eas_optical.eas.CphotAng()

    dphots, cang = cpa(beta_tr, altitude)

    # print(dphots)
    # print(cang)

    old_dphots = np.array(
        [
            1.6210271e03,
            1.8776989e03,
            2.1580850e03,
            2.4297334e03,
            2.6037090e03,
            2.5893950e03,
            2.6665376e03,
            1.9930570e03,
            1.2905229e03,
            7.2669135e02,
            3.5919400e02,
            1.5781609e02,
            6.3526283e01,
            2.4607666e01,
            9.4170065e00,
            3.5231948e00,
            1.3178004e00,
            5.0189447e-01,
            1.9809218e-01,
            8.2580306e-02,
            3.6842916e-02,
        ]
    )

    old_cang = np.array(
        [
            1.1297,
            1.0476834,
            0.9589647,
            0.86524945,
            0.7722871,
            0.68627226,
            0.6104225,
            0.54487425,
            0.48861432,
            0.4402858,
            0.39882657,
            0.3633839,
            0.33331406,
            0.30719605,
            0.28355855,
            0.2634559,
            0.24582747,
            0.22999273,
            0.21532202,
            0.20063952,
            0.18477382,
        ]
    )

    assert np.allclose(dphots, old_dphots)
    assert np.allclose(cang, old_cang)

if __name__ == "__main__":
    # test_known_values()

    import matplotlib.pyplot as plt

    plt.style.use("seaborn-whitegrid")
    plt.rcParams["font.size"] = 16

    degs = list([1.0, 5.0, 10.0, 20.0, 30.0])
    ds = []
    cs = []

    jds = [
        np.array(
            [
                5.27674332e-04,
                0.409867704,
                3.94313812,
                10.1881313,
                18.0240078,
                28.4657440,
                39.6969986,
                54.5465965,
                73.7063370,
                97.1183090,
                127.589645,
                174.656738,
                238.554108,
                326.405029,
                452.014099,
                642.060242,
                904.732117,
                1232.60498,
                1535.39136,
                2066.62646,
                1633.27539,
            ]
        ),
        np.array(
            [
                9.30504322,
                25.3226070,
                42.3239822,
                58.3105316,
                74.3881149,
                92.6042557,
                113.324104,
                139.367432,
                156.780945,
                180.987335,
                228.783035,
                297.147156,
                389.135193,
                517.803101,
                684.509583,
                866.451965,
                993.215820,
                1206.91724,
                901.117920,
                556.321472,
                290.776581,
            ]
        ),
        np.array(
            [
                63.6738396,
                96.9967651,
                124.916992,
                151.680115,
                180.303146,
                212.641663,
                251.588882,
                298.172302,
                359.177368,
                447.978363,
                574.827698,
                744.875977,
                928.580750,
                1055.05176,
                1268.87500,
                986.335632,
                643.666016,
                358.620361,
                175.488449,
                78.0030975,
                32.5095978,
            ]
        ),
        np.array(
            [
                319.989349,
                392.296692,
                463.493835,
                540.937988,
                631.059265,
                744.649292,
                896.025574,
                1093.49854,
                1304.78015,
                1446.11072,
                1504.65527,
                1362.30811,
                905.672241,
                510.569275,
                250.467560,
                110.092865,
                44.7798576,
                17.4202747,
                6.75650692,
                2.54880404,
                0.951708376,
            ]
        ),
        np.array(
            [
                791.055542,
                929.935425,
                1084.46570,
                1267.08813,
                1485.99060,
                1725.16589,
                1917.01294,
                1957.82263,
                2058.22607,
                1530.02161,
                965.538391,
                520.899170,
                246.437302,
                105.856003,
                42.3585625,
                16.4105015,
                6.22363472,
                2.31331372,
                0.857409120,
                0.323087066,
                0.125590757,
            ]
        ),
    ]

    jcs = [
        np.array(
            [
                1.32911205,
                1.27261925,
                1.21342850,
                1.15533781,
                1.09440362,
                1.03490627,
                0.975558519,
                0.914052486,
                0.852542639,
                0.792654872,
                0.732216477,
                0.670476615,
                0.611326814,
                0.555313587,
                0.499191791,
                0.443307370,
                0.391291529,
                0.341089129,
                0.293872952,
                0.252725810,
                0.219138503,
            ]
        ),
        np.array(
            [
                1.31018233,
                1.25294042,
                1.19424653,
                1.13343668,
                1.07256615,
                1.00931919,
                0.947368860,
                0.884270132,
                0.821494460,
                0.757842660,
                0.694497168,
                0.629486024,
                0.566345632,
                0.504391909,
                0.444903165,
                0.386975050,
                0.333529383,
                0.287159741,
                0.249708071,
                0.220059916,
                0.196107492,
            ]
        ),
        np.array(
            [
                1.28595519,
                1.22553182,
                1.16376925,
                1.10031724,
                1.03530788,
                0.969625294,
                0.902465105,
                0.833714664,
                0.764868081,
                0.695196211,
                0.623690188,
                0.549934447,
                0.478442669,
                0.413230062,
                0.356946319,
                0.310444713,
                0.273142755,
                0.242978141,
                0.218469203,
                0.198584095,
                0.182161078,
            ]
        ),
        np.array(
            [
                1.23594880,
                1.16961718,
                1.10034442,
                1.02877235,
                0.955798030,
                0.880288124,
                0.800689518,
                0.717127085,
                0.633447826,
                0.554169774,
                0.483273327,
                0.422520190,
                0.371766686,
                0.330366224,
                0.296454549,
                0.268502116,
                0.245628387,
                0.226084396,
                0.208619133,
                0.193726748,
                0.180828407,
            ]
        ),
        np.array(
            [
                1.18540561,
                1.11206508,
                1.03592265,
                0.956088781,
                0.870580733,
                0.780635655,
                0.692408621,
                0.610369325,
                0.538554668,
                0.476793230,
                0.424100250,
                0.379572451,
                0.342216313,
                0.311301470,
                0.285714626,
                0.263185740,
                0.243161157,
                0.226112887,
                0.211174414,
                0.197692946,
                0.184871987,
            ]
        ),
    ]

    alt = np.arange(0, 21, dtype=np.float32)

    for v in degs:
        beta_tr = np.full_like(alt, np.radians(v), dtype=np.float32)
        cpa = eas_optical.eas.CphotAng()

        dphots, cang = cpa(beta_tr, alt)
        ds.append(dphots)
        cs.append(cang)

    fig, axs = plt.subplots(2, 2, sharex=True, constrained_layout=True)
    colors = ["red", "orange", "green", "blue", "purple"]

    for i in range(5):
        axs[0, 0].plot(
            alt,
            ds[i],
            "--",
            label=f"AWR: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[0, 0].plot(
            alt,
            jds[i],
            "-",
            label=f"JFK: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[0, 0].set_title("Photon Density")
        axs[0, 0].set_xlabel("altitude")
        axs[0, 0].set_ylabel("photon density")
        axs[0, 0].legend()

        axs[1, 0].plot(
            alt,
            ds[i],
            "--",
            label=f"AWR: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[1, 0].plot(
            alt,
            jds[i],
            "-",
            label=f"JFK: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[1, 0].set_title("Log Photon Density")
        axs[1, 0].set_xlabel("altitude")
        axs[1, 0].set_ylabel("log photon density")
        axs[1, 0].set_yscale("log")
        axs[1, 0].legend()

        axs[0, 1].plot(
            alt,
            cs[i],
            "--",
            label=f"AWR: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[0, 1].plot(
            alt,
            jcs[i],
            "-",
            label=f"JFK: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[0, 1].set_title("Cherenkov Angle")
        axs[0, 1].set_xlabel("altitude")
        axs[0, 1].set_ylabel("Cherenkov Angle")
        axs[0, 1].legend()

        axs[1, 1].plot(
            alt,
            cs[i],
            "--",
            label=f"AWR: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[1, 1].plot(
            alt,
            jcs[i],
            "-",
            label=f"JFK: {degs[i]}" + "\N{DEGREE SIGN}",
            color=colors[i],
        )
        axs[1, 1].set_title("Log Cherenkov Angle")
        axs[1, 1].set_xlabel("altitude")
        axs[1, 1].set_ylabel("Log Cherenkov Angle")
        axs[1, 1].set_yscale("log")
        axs[1, 1].legend()

    plt.show()

max_stars_repo_name: NuSpaceSim/nuSpaceSim

max_issues_repo_name: NuSpaceSim/nuSpaceSim

max_forks_repo_name: NuSpaceSim/nuSpaceSim


text:
"""
Measures of centrality (e.g. mean) used to combine predictions across an
ensemble. The input to these functions are log affinities, and they are expected
to return a centrality measure also in log-space.
"""

import numpy
from functools import partial

def robust_mean(log_values):
    """
    Mean of values falling within the 25-75 percentiles.

    Parameters
    ----------
    log_values : 2-d numpy.array
        Center is computed along the second axis (i.e. per row).

    Returns
    -------
    center : numpy.array of length log_values.shape[1]

    """
    if log_values.shape[1] <= 3:
        # Too few values to use robust mean.
        return numpy.nanmean(log_values, axis=1)
    without_nans = numpy.nan_to_num(log_values)  # replace nan with 0
    mask = (
        (~numpy.isnan(log_values)) &
        (without_nans <= numpy.nanpercentile(log_values, 75, axis=1).reshape((-1, 1))) &
        (without_nans >= numpy.nanpercentile(log_values, 25, axis=1).reshape((-1, 1))))
    return (without_nans * mask.astype(float)).sum(1) / mask.sum(1)

CENTRALITY_MEASURES = {
    "mean": partial(numpy.nanmean, axis=1),
    "median": partial(numpy.nanmedian, axis=1),
    "robust_mean": robust_mean,
}

max_stars_repo_name: ignatovmg/mhcflurry

max_issues_repo_name: ignatovmg/mhcflurry

max_forks_repo_name: ignatovmg/mhcflurry


text:
# -*- coding: utf-8 -*-

import tensorflow as tf
import tensorflow.keras as tk
import numpy as np
import time
from dataloader import Dataloader
import sys
sys.path.append('..')
from ops import *
from utils import *

class Model(object):
	def __init__(self, args):
		self.args = args

	def generator(self):
		x = Input((self.args.img_size, self.args.img_size, self.args.img_nc))
		c = Input((self.args.label_nc))

		c_tiled = tf.tile(tf.reshape(c, [-1, 1, 1, self.args.label_nc]), [1, self.args.img_size, self.args.img_size, 1])
		h = tf.concat([x, c_tiled], -1)

		h = tk.Sequential([
			Conv2d(64,  7, 1), IN(), Relu(),
			Conv2d(128, 4, 2), IN(), Relu(),
			Conv2d(256, 4, 2), IN(), Relu()])(h)

		for i in range(6):
			h = Resblock(256)(h)

		h = tk.Sequential([
			Deconv2d(128, 4), IN(), Relu(),
			Deconv2d(64,  4), IN(), Relu()])(h)

		t = Conv2d(self.args.img_nc, 7, 1, activation='tanh')(h)
		a = Conv2d(1, 7, 1, activation='sigmoid')(h)
		g = a * t + (1 - a) * x

		return tk.Model([x, c], [a, g])

	def discriminator(self):
		x = Input((self.args.img_size, self.args.img_size, self.args.img_nc))
		h = x

		dis_layer = 6
		filters = 64
		for i in range(dis_layer):
			h = lrelu(Conv2d(filters, 4, 2)(h))
			filters = min(filters * 2, 512)

		logit = Conv2d(1, 3, 1)(h)
		kernel_size = self.args.img_size // 2 ** dis_layer
		c = tf.reshape(Conv2d(self.args.label_nc, kernel_size, 1, padding='valid')(h), [-1, self.args.label_nc])

		return tk.Model(x, [logit, c])

	def build_model(self):
		dataloader = Dataloader(self.args)
		if self.args.phase == 'train':
			self.args.iteration = dataloader.dataset_size // self.args.batch_size
			self.iter = iter(dataloader.loader)

			self.G = self.generator()
			self.D = self.discriminator()

			boundaries = []
			values = [self.args.lr]
			for i in range(self.args.decay_epochs):
				boundaries.append(self.args.iteration * (self.args.epochs + i))
				values.append(self.args.lr - i * self.args.lr / self.args.decay_epochs)

			lr = tk.optimizers.schedules.PiecewiseConstantDecay(boundaries, values)
			self.optimizer_g = tk.optimizers.Adam(learning_rate=self.args.lr, beta_1=0.5, beta_2=0.999)
			self.optimizer_d = tk.optimizers.Adam(learning_rate=self.args.lr, beta_1=0.5, beta_2=0.999)

			self.summary_writer = tf.summary.create_file_writer(self.args.log_dir)

		elif self.args.phase == 'test':
			self.img = dataloader.img
			self.load()

	@tf.function
	def train_step(self, img, label, label_):
		with tf.GradientTape() as tape_g, tf.GradientTape() as tape_d:
			a_fake, fake = self.G([img, label_ - label])
			a_cyc, cyc = self.G([fake, label - label_])
			_, rec = self.G([img, tf.zeros_like(label)])

			d_real, c_real = self.D(img)
			d_fake, c_fake = self.D(fake)

			loss_d_adv = self.args.w_adv * discriminator_loss(d_real, d_fake, self.args.gan_type)
			loss_g_adv = self.args.w_adv * generator_loss(d_fake, self.args.gan_type)

			loss_d_cls = self.args.w_cls * c_loss(label, c_real)
			loss_g_cls = self.args.w_cls * c_loss(label_, c_fake)

			loss_g_cyc = self.args.w_cyc * l1_loss(img, cyc)
			loss_g_rec = self.args.w_rec * l1_loss(img, rec)

			loss_g_a = self.args.w_a * (l1_loss(a_fake, 0) + l1_loss(a_cyc, 0))
			loss_g_tv = self.args.w_tv * (tv_loss(a_fake) + tv_loss(a_cyc))

			loss_d = loss_d_adv + loss_d_cls
			loss_g = loss_g_adv + loss_g_cls + loss_g_cyc + loss_g_rec + loss_g_a + loss_g_tv

		vars_g = self.G.trainable_variables
		vars_d = self.D.trainable_variables
		self.optimizer_g.apply_gradients(zip(tape_g.gradient(loss_g, vars_g), vars_g))
		self.optimizer_d.apply_gradients(zip(tape_d.gradient(loss_d, vars_d), vars_d))

		return {'loss_g_adv': loss_g_adv, 'loss_g_cls': loss_g_cls, 'loss_g_cyc': loss_g_cyc, 'loss_g_rec': loss_g_rec,
				'loss_g_a': loss_g_a, 'loss_g_tv': loss_g_tv, 'loss_d_adv': loss_d_adv, 'loss_d_cls': loss_d_cls}

	def train(self):
		start_time = time.time()
		step = 0
		img_size = self.args.img_size
		for e in range(self.args.epochs + self.args.decay_epochs):
			for i in range(self.args.iteration):
				img, label = next(self.iter)
				label_ = tf.random.shuffle(label)

				item = self.train_step(img, label, label_)
				print('epoch: [%3d/%3d] iter: [%6d/%6d] time: %.2f' % (e, self.args.epochs + self.args.decay_epochs,
					i, self.args.iteration, time.time() - start_time))
				step += 1

				if step % self.args.log_freq == 0:
					with self.summary_writer.as_default():
						tf.summary.scalar('loss_g_adv', item['loss_g_adv'], step=step)
						tf.summary.scalar('loss_g_cls', item['loss_g_cls'], step=step)
						tf.summary.scalar('loss_g_cyc', item['loss_g_cyc'], step=step)
						tf.summary.scalar('loss_g_rec', item['loss_g_rec'], step=step)
						tf.summary.scalar('loss_g_a',   item['loss_g_a'],   step=step)
						tf.summary.scalar('loss_g_tv',  item['loss_g_tv'],  step=step)
						tf.summary.scalar('loss_d_adv', item['loss_d_adv'], step=step)
						tf.summary.scalar('loss_d_cls', item['loss_d_cls'], step=step)

			sample = np.ones((2 * self.args.batch_size * img_size, (self.args.label_nc + 1) * img_size, self.args.img_nc))
			for j in range(self.args.label_nc):
				label_ = label.numpy()
				label_[:, j] = 1. - label_[:, j]

				a_fake, fake = self.G([img, label_ - label])
				fake = imdenorm(fake.numpy())
				a_fake = a_fake.numpy()
				for i in range(self.args.batch_size):
					sample[2 * i * img_size: (2 * i + 1) * img_size, (j + 1) * img_size: (j + 2) * img_size] = fake[i]
					sample[(2 * i + 1) * img_size: (2 * i + 2) * img_size, (j + 1) * img_size: (j + 2) * img_size] = a_fake[i]

			img = imdenorm(img.numpy())
			for i in range(self.args.batch_size):
				sample[2 * i * img_size: (2 * i + 1) * img_size, :img_size] = img[i]

			imsave(os.path.join(self.args.sample_dir, f'{e}.jpg'), sample)
			self.save()

	def test(self):
		img_size = self.args.img_size
		result = np.ones((2 * img_size, (self.args.label_nc + 1) * img_size, self.args.img_nc))

		for i in range(self.args.label_nc):
			diff = np.zeros((1, self.args.label_nc), 'float32')
			diff[:, i] = 1.
			_, fake = self.G([self.img, diff])
			result[:img_size, (i + 1) * img_size: (i + 2) * img_size] = imdenorm(fake[0].numpy())
			_, fake = self.G([self.img, -diff])
			result[img_size:, (i + 1) * img_size: (i + 2) * img_size] = imdenorm(fake[0].numpy())

		result[:img_size, :img_size] = imdenorm(self.img[0].numpy())
		imsave(os.path.join(self.args.result_dir, 'result.jpg'), result)

	def load(self, all_module=False):
		self.G = self.generator()
		self.G.load_weights(os.path.join(self.args.save_dir, 'G.h5'))
		if all_module:
			self.D = tk.models.load_model(os.path.join(self.args.save_dir, 'G.h5'))

	def save(self):
		self.G.save_weights(os.path.join(self.args.save_dir, 'G.h5'))
		self.D.save(os.path.join(self.args.save_dir, 'D.h5'))


text:
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from stl import mesh

from .layout import FigAxes_create, set_ax_info

def stl2mpl_data(filename, ratio=1., seed=None):
    """Create A collection of 3D polygons from STL file.

    Note:
        When converting STL data to 3D Polygons, the number of data points becomes extremely large, so it is better to do sampling using the ``ratio`` parameter.

    Args:
        filename (str)    : The STL file to load
        ratio (float/int) : Sampling percentage or the exact number of sampling data points.
        seed (int)        : Value to ensure sampling reproducibility

    References:
        `numpy-stl - PyPI <https://pypi.org/project/numpy-stl/>`_
    """
    # Load the STL files and add the vectors to the plot
    stl_data = mesh.Mesh.from_file(filename)
    stl_vector = stl_data.vectors
    stl_points = stl_data.points
    num_stl_data_points = len(stl_vector)
    print(f"The number of STL data points: {num_stl_data_points:,}")

    # Extract only some of them.
    if isinstance(ratio, float):
        num_plot_data_points = int(num_stl_data_points*ratio)
    else:
        num_plot_data_points = int(ratio)
    num_plot_data_points = max(0, min(num_plot_data_points, num_stl_data_points))
    print(f"The number of data points to plot: {num_plot_data_points:,}")
    random_data_point = np.random.choice(a=np.arange(0, num_stl_data_points), size=num_plot_data_points, replace=False)
    stl_vector = stl_vector[random_data_point]
    stl_points = stl_points[random_data_point]

    poly_3D_collection = mplot3d.art3d.Poly3DCollection(stl_vector)
    scale = stl_points.flatten(order='C')
    return (poly_3D_collection, scale)

def plot_stl_file(filename, ax=None, ratio=1., seed=None, alpha=1, color=None):
    """Plot STL data.

    Note:
        When converting STL data to 3D Polygons, the number of data points becomes extremely large, so it is better to do sampling using the ``ratio`` parameter.

    Args:
        filename (str)    : The STL file to load
        ax (Axes)         : The 3D ``Axes`` instance.
        ratio (float/int) : Sampling percentage or the exact number of sampling data points.
        seed (int)        : Value to ensure sampling reproducibility
        alpha (float)     : The alpha blending value, between 0 (transparent) and 1 (opaque).
        color (str)       : The edge color of the marker.

    Examples:
        >>> from pycharmers.matplotlib import plot_stl_file, FigAxes_create, set_ax_info
        >>> fig, ax = FigAxes_create(figsize=(8,8), projection="3d", nplots=1)[0]
        >>> plot_stl_file("Scorpion.stl", ax=ax, ratio=.5, alpha=0.01, color="red")
        >>> set_ax_info(ax, title="Scorpion")
        >>> fig.savefig("Scorpion.png")

    References:
        If you want to see the result of the ``Examples``, see `my tweet <https://twitter.com/cabernet_rock/status/1304751796233986048>`_

    +-----------------------------------------------------------+
    |                         Results                           |
    +===========================================================+
    | .. image:: _images/matplotlib.plot3d.plot_stl_file.jpg    |
    +-----------------------------------------------------------+
    """
    poly_3D_collection, scale = stl2mpl_data(filename=filename, ratio=ratio, seed=seed)
    poly_3D_collection.set_alpha(alpha)
    poly_3D_collection.set_edgecolor(color)
    fig, ax = FigAxes_create(fig=None, ax=ax, projection="3d")
    ax.add_collection3d(poly_3D_collection)
    ax.auto_scale_xyz(scale, scale, scale)
    return ax

max_stars_repo_name: iwasakishuto/Py-utils

max_issues_repo_name: iwasakishuto/Py-utils

max_forks_repo_name: iwasakishuto/Py-utils


text:
# -*- coding: utf-8 -*-
'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling.
Copyright (C) 2020, Caleb Bell <Caleb.Andrew.Bell@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''

from numpy.testing import assert_allclose
import pytest
import thermo
from thermo import *
from thermo.coolprop import *
from thermo.phases import IAPWS95Gas, IAPWS95Liquid
from thermo.chemical_package import iapws_correlations
from fluids.numerics import *
from math import *
import json
import os
import numpy as np
try:
    import matplotlib.pyplot as plt
except:
    pass

fluid = 'water'
pure_surfaces_dir = os.path.join(thermo.thermo_dir, '..', 'surfaces', 'iapws95')

def test_iapws95_basic_flash():
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])
    PT = flasher.flash(T=300, P=1e6)

    assert_close(PT.rho_mass(), 996.9600226949985, rtol=1e-10)
    assert isinstance(PT.liquid0, IAPWS95Liquid)

    TV = flasher.flash(T=300, V=PT.V())
    assert_close(TV.P, PT.P, rtol=1e-10)
    assert isinstance(TV.liquid0, IAPWS95Liquid)

    PV = flasher.flash(P=PT.P, V=PT.V())
    assert_close(PV.T, PT.T, rtol=1e-13)
    assert isinstance(PV.liquid0, IAPWS95Liquid)

    TVF = flasher.flash(T=400.0, VF=1)
    assert_close(TVF.P, 245769.3455657166, rtol=1e-13)

    PVF = flasher.flash(P=TVF.P, VF=1)
    assert_close(PVF.T, 400.0, rtol=1e-13)

    assert_close(flasher.flash(P=1e5, H=flasher.flash(T=273.15, P=1e5).H()).T, 273.15, rtol=1e-10)

def test_iapws95_basic_flashes_no_hacks():
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])
    flasher.VL_only_IAPWS95 = False

    PT = flasher.flash(T=300, P=1e6)

    assert_close(PT.rho_mass(), 996.9600226949985, rtol=1e-10)

    TV = flasher.flash(T=300, V=PT.V())
    assert_close(TV.P, PT.P, rtol=1e-10)

    PV = flasher.flash(P=PT.P, V=PT.V())
    assert_close(PV.T, PT.T, rtol=1e-13)

    PH = flasher.flash(H=PT.H(), P=1e6)
    assert_close(PH.T, 300)

    PS = flasher.flash(S=PT.S(), P=1e6)
    assert_close(PS.T, 300)

    assert_close(flasher.flash(P=1e5, H=flasher.flash(T=273.15, P=1e5).H()).T, 273.15, rtol=1e-10)

@pytest.mark.plot
@pytest.mark.slow
@pytest.mark.parametric
def test_TV_plot_iapws95():
    eos = IAPWS95
    T, P = 298.15, 101325.0
    zs = [1.0]
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])

    res = flasher.TPV_inputs(zs=zs, pts=200, spec0='T', spec1='P', check0='T', check1='V', prop0='P',
                           trunc_err_low=1e-10,
                           trunc_err_high=1, color_map=cm_flash_tol(),
                           show=False)

    matrix_spec_flashes, matrix_flashes, errs, plot_fig = res

    path = os.path.join(pure_surfaces_dir, fluid, "TV")
    if not os.path.exists(path):
        os.makedirs(path)

    key = '%s - %s - %s' %('TV', eos.__name__, fluid)
    plot_fig.savefig(os.path.join(path, key + '.png'))
    plt.close()

    max_err = np.max(np.abs(errs))
    # CoolProp has same error characteritic
    assert max_err < 1e-5

@pytest.mark.plot
@pytest.mark.slow
@pytest.mark.parametric
def test_PV_plot_iapws95():
    eos = IAPWS95
    T, P = 298.15, 101325.0
    zs = [1.0]
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])

    res = flasher.TPV_inputs(zs=zs, pts=100, spec0='T', spec1='P', check0='P', check1='V', prop0='T',
                           trunc_err_low=1e-10,
                           trunc_err_high=1, color_map=cm_flash_tol(),
#                           auto_range=auto_range,
                           show=False)

    matrix_spec_flashes, matrix_flashes, errs, plot_fig = res

    path = os.path.join(pure_surfaces_dir, fluid, "PV")
    if not os.path.exists(path):
        os.makedirs(path)

    key = '%s - %s - %s' %('PV', eos.__name__, fluid)

    plot_fig.savefig(os.path.join(path, key + '.png'))
    plt.close()
    max_err = np.max(np.abs(errs))
    limit = 5e-11
#    assert max_err < limit
@pytest.mark.plot
@pytest.mark.slow
@pytest.mark.parametric
def test_PS_plot():
    '''
    '''
    eos = IAPWS95
    path = os.path.join(pure_surfaces_dir, fluid, "PS")
    if not os.path.exists(path):
        os.makedirs(path)
    key = '%s - %s - %s' %('PS', eos.__name__, fluid)

    T, P = 298.15, 101325.0
    zs = [1.0]
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])

    res = flasher.TPV_inputs(zs=zs, pts=200, spec0='T', spec1='P', check0='P', check1='S', prop0='T',
                           trunc_err_low=1e-15,
                           trunc_err_high=1, color_map=cm_flash_tol(),
                           show=False)

    matrix_spec_flashes, matrix_flashes, errs, plot_fig = res

    plot_fig.savefig(os.path.join(path, key + '.png'))
    plt.close()

    max_err = np.max(errs)
    assert max_err < 1e-8
#
#test_PS_plot()

@pytest.mark.plot
@pytest.mark.slow
@pytest.mark.parametric
def test_PH_plot():
    '''
    '''
    eos = IAPWS95
    path = os.path.join(pure_surfaces_dir, fluid, "PH")
    if not os.path.exists(path):
        os.makedirs(path)
    key = '%s - %s - %s' %('PH', eos.__name__, fluid)

    T, P = 298.15, 101325.0
    zs = [1.0]
    liquid = IAPWS95Liquid(T=300, P=1e5, zs=[1])
    gas = IAPWS95Gas(T=300, P=1e5, zs=[1])
    flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])

    res = flasher.TPV_inputs(zs=zs, pts=250, spec0='T', spec1='P', check0='P', check1='H', prop0='T',
                           trunc_err_low=1e-15,
                           trunc_err_high=1, color_map=cm_flash_tol(),
                           show=False)

    matrix_spec_flashes, matrix_flashes, errs, plot_fig = res

    plot_fig.savefig(os.path.join(path, key + '.png'))
    plt.close()

    max_err = np.max(errs)
    assert max_err < 1e-8

#test_PH_plot()

max_stars_repo_name: brunokiyoshi/thermo

max_issues_repo_name: brunokiyoshi/thermo

max_forks_repo_name: brunokiyoshi/thermo


text:
import json
import numpy as np
import scipy.stats as st
import pickle

import matplotlib
import matplotlib.pyplot as plt
from matplotlib import gridspec

matplotlib.use('TkAgg')

print(matplotlib.get_configdir())

if __name__ == '__main__':
    nodes = 6
    number_test = 0

    # fr = open('./emulator/MIMII/saxsNew.pkl', 'rb')
    # saxs = pickle.load(fr)
    # ss, aa, xx = saxs
    # s = ss[number_test]
    # x = xx[number_test]

    path_csv = 'measurement/pICA_'+str(nodes)+'details.csv'
    node_details = np.loadtxt(path_csv, delimiter=',', usecols=np.arange(
        1, nodes+2, 1))
    len_subset = node_details[0,:]
    process_latency = node_details[1,:]
    separation_accuracy = node_details[2, :]

    with plt.style.context(['science', 'ieee']):
        fig_width = 6.5
        barwidth = 0.15
        # bardistance = barwidth * 1.2
        colordict = {
            'compute_forward': '#0077BB',
            'store_forward': '#DDAA33',
            'darkblue': '#024B7A',
            'lightblue': '#3F9ABF',
            'midblue': '#7ACFE5'
        }
        markerdict = {
            'compute_forward': 'o',
            'store_forward': 'v',
            'store_forward_ia': 's'
        }

        plt.rcParams.update({'font.size': 11})

        fig = plt.figure(figsize=(fig_width, fig_width / 1.618))
        ax = fig.add_subplot(1, 1, 1)
        ax.yaxis.grid(True, linestyle='--', which='major',
                      color='lightgrey', alpha=0.5, linewidth=0.2)
        x_index = np.arange(nodes + 1)
        bar1 = ax.bar(x_index-barwidth, len_subset/np.max(len_subset)*100, barwidth, fill=True,
                      color=colordict['darkblue'], edgecolor='#FFFFFF', ecolor='#555555', hatch='\\')
        bar2 = ax.bar(x_index, process_latency/sum(process_latency)*100, barwidth, fill=True,
                      color=colordict['lightblue'], edgecolor='#FFFFFF', ecolor='#555555', hatch='/')
        bar3 = ax.bar(x_index+barwidth, separation_accuracy/separation_accuracy[-1]*100, barwidth, fill=True,
                      color=colordict['midblue'], edgecolor='#FFFFFF', ecolor='#555555', hatch='//')

        # ax.set_xlabel(r'Number of nodes $k$')
        ax.set_ylabel(r'Percent in total cost ($\%$)')
        ax.set_yticks(np.arange(0, 101, 20))
        # ax.set_xlim([2, 60000])
        # ax.set_ylim([0, 201])
        ax.legend([bar1, bar2, bar3], [
            'Subset data size', 'Processing time', 'Processing precision'], loc='upper left', ncol=1)
        plt.xticks(x_index, ['Node 1', 'Node 2', 'Node 3', 'Node 4', 'Node 5', 'Node 6', 'Remote \n Agent'], rotation=30)
        plt.savefig('./measurement/nodes_performance_simu.pdf',
                    dpi=600, bbox_inches='tight')

max_stars_repo_name: Huanzhuo/pICA

max_issues_repo_name: Huanzhuo/pICA

max_forks_repo_name: Huanzhuo/pICA