text:
import numpy as np
from scipy.integrate import ode

from .name2idx import C, V
from .set_model import DifferentialEquation

observables = [
    'Per_mRNA',
    'Cry_mRNA',
    'Bmal1_mRNA',
]

class NumericalSimulation(DifferentialEquation):
    """ Simulate a model using scipy.integrate.ode

    Attributes
    ----------
    normalization : bool
        if True, simulation results in each observable are divided by their
        maximum values.

    """
    def __init__(self):
        super().__init__(perturbation={})
        self.normalization = False

    t = range(72+1)

    # Experimental conditions
    conditions = ['DD']

    simulations = np.empty((len(observables), len(t), len(conditions)))

    def simulate(self, x, y0, _perturbation={}):
        if _perturbation:
            self.perturbation = _perturbation
        for i, condition in enumerate(self.conditions):
            if condition == 'DD':
                pass

            (T, Y) = self._solveode(self.diffeq, y0, self.t, tuple(x))

            if T[-1] < self.t[-1]:
                return False
            else:
                self.simulations[observables.index('Per_mRNA'), :, i] = (
                    Y[:, V.MP]
                )
                self.simulations[observables.index('Cry_mRNA'), :, i] = (
                    Y[:, V.MC]
                )
                self.simulations[observables.index('Bmal1_mRNA'), :, i] = (
                    Y[:, V.MB]
                )

    def _solveode(self, diffeq, y0, tspan, args):
        """
        Solve a system of ordinary differential equations.

        Parameters
        ----------
        diffeq : callable f(y, t, f_args)
            Right-hand side of the differential equation.

        y0 : array
            Initial condition on y (can be a vector).

        tspan : array
            A sequence of time points for which to solve for y.

        args : tuple
            Model parameters.

        Returns
        -------
        T, Y : tuple
            T : array, shape (len(t))
                Evaluation points.

            Y : array, shape (len(t), len(y0))
                Array containing the value of y for each desired time in t,
                with the initial value y0 in the first row.

        """
        dt = (self.t[-1] - self.t[0]) / (len(self.t) - 1)
        sol = ode(diffeq)
        sol.set_integrator(
            'vode', method='bdf', with_jacobian=True,
            atol=1e-9, rtol=1e-9, min_step=1e-8
        )
        sol.set_initial_value(y0, tspan[0])
        sol.set_f_params(args)

        T = [tspan[0]]
        Y = [y0]

        while sol.successful() and sol.t < tspan[-1]:
            sol.integrate(sol.t+dt)
            T.append(sol.t)
            Y.append(sol.y)

        return np.array(T), np.array(Y)

    def _get_steady_state(self, diffeq, y0, args, eps=1e-6):
        """
        Find the steady state for the untreated condition.

        Parameters
        ----------
        diffeq : callable f(y, t, f_args)
            Right-hand side of the differential equation.

        y0 : array
            Initial condition on y (can be a vector).

        args : tuple
            Model parameters.

        eps : float (default: 1e-6)
            Run until a time t for which the maximal absolutevalue of the
            regularized relative derivative was smaller than eps.

        Returns
        -------
        y0 : array
            Steady state concentrations of all species.

        """
        while True:
            (T, Y) = self._solveode(diffeq, y0, range(2), args)
            if T[-1] < 1 or \
                    np.max(
                        np.abs((Y[-1, :] - y0) / (np.array(y0) + eps))
                    ) < eps:
                break
            else:
                y0 = Y[-1, :].tolist()

        return [] if T[-1] < 1 else Y[-1, :].tolist()

class ExperimentalData(object):
    """
    Set experimental data.

    Attributes
    ----------
    experiments : list of dict
        Time series data.

    error_bars : list of dict
        Error bars to show in figures.

    """
    def __init__(self):
        self.experiments = [None] * len(observables)
        self.error_bars = [None] * len(observables)

    def set_data(self):
        pass

    @staticmethod
    def get_timepoint(obs_name):
        pass

ext: py

reason: import numpy,from scipy


text:
import numpy as np
from skimage.transform import resize

def project_simplex(x):
    """Project Simplex

    Projects an arbitrary vector :math:`\mathbf{x}` into the probability simplex, such that,

    .. math:: \tilde{\mathbf{x}}_{i} = \dfrac{\mathbf{x}_{i}}{\sum_{j=1}^{n}\mathbf{x}_{j}}

    Parameters
    ----------
    x : :class:`numpy.ndarray`
        Numpy array of shape (n,)

    Returns
    -------
    y : :class:`numpy.ndarray`
        numpy array lying on the probability simplex of shape (n,)
    """
    x[x < 0] = 0
    if np.isclose(sum(x), 0):
        y = np.zeros_like(x)
    else:
        y = x.copy() / sum(x)
    return y

def create_digits_image(images, labels, digit=0, grid_size=64, n_digits=15, original_size=28, is_distribution=True):
    batch = images[np.where(labels==digit)[0]]
    images = []
    for i in range(n_digits):
        grid = np.zeros([grid_size, grid_size])
        final_size = np.random.randint(original_size // 2, original_size * 2)
        grid_cells = np.arange(0, grid_size - final_size)
        img = resize(batch[np.random.randint(0, len(batch))], (final_size, final_size))
        p = [10] + [1] * (len(grid_cells) - 2) + [10]
        center_x = np.random.choice(grid_cells, size=1, p=np.array(p) / sum(p))[0]
        center_y = np.random.choice(grid_cells, size=1, p=np.array(p) / sum(p))[0]
        grid[center_x:center_x+final_size, center_y:center_y+final_size] = img.copy()
        if is_distribution:
            grid = grid / np.sum(grid)
        images.append(grid.reshape(1, grid_size, grid_size))
    images = np.array(images).reshape(-1, grid_size ** 2)

    return images

max_stars_repo_name: eddardd/wasserstein-barycenters

max_issues_repo_name: eddardd/wasserstein-barycenters

max_forks_repo_name: eddardd/wasserstein-barycenters

reason: import numpy


text:
import sys
import os

sys.path.append(os.getcwd())

import math
import random
import numpy as np
from time import time
from typing import Dict, List, Tuple, Union, Set
from collections import defaultdict
import function
import argparse
import pandas as pd

from src.alns.solution import Solution, ProblemDataExtended
from src.models.production_model import ProductionModel
import src.alns.alns_parameters as alnsparam
from src.util import plot
from src.util import stats
from src.alns.production_problem_heuristic import ProductionProblemHeuristic, ProductionProblem
import locale

locale.setlocale(locale.LC_ALL, '')

int_inf = 999999

class Alns:
    it_seg_count: int
    best_sol: Solution
    best_sol_routing_cost: int
    best_sol_production_cost: int
    best_sol_total_cost: int
    current_sol: Solution
    current_sol_cost: int
    production_model: ProductionModel
    production_heuristic: ProductionProblemHeuristic
    inventory_reward: bool
    update_type: int
    destroy_op_score: Dict[str, float]
    repair_op_score: Dict[str, float]
    destroy_op_weight: Dict[str, float]
    repair_op_weight: Dict[str, float]
    destroy_op_segment_usage: Dict[str, int]
    repair_op_segment_usage: Dict[str, int]
    reaction_param: float
    score_params: Dict[int, int]
    weight_min_threshold: float
    temperature: float
    cooling_rate: float
    max_iter_same_solution: int
    iter_same_solution: int
    determinism_param: int
    relatedness_precedence: Dict[Tuple[str, str], int]
    related_removal_weight_param: Dict[str, List[float]]
    new_best_solution_feasible_production_count = 0
    new_best_solution_infeasible_production_count = 0
    ppfc_infeasible_count = 0
    production_infeasibility_strike = 0
    production_infeasibility_strike_max: int
    previous_solutions: Set[str] = set()

    def __init__(self, problem_data: ProblemDataExtended,
                 destroy_op: List[str],
                 repair_op: List[str],
                 weight_min_threshold: float,
                 reaction_param: float,
                 score_params: List[int],
                 start_temperature_controlparam: float,
                 cooling_rate: float,
                 max_iter_seg: int,
                 percentage_best_solutions_production_solved: float,
                 max_iter_same_solution: int,
                 remove_percentage_interval: Tuple[float, float],
                 remove_num_percentage_adjust: float,
                 determinism_param: int,
                 noise_param: float,
                 noise_destination_param: float,
                 relatedness_precedence: Dict[Tuple[str, str], int],
                 related_removal_weight_param: Dict[str, List[float]],
                 inventory_reward: bool,
                 production_infeasibility_strike_max: int,
                 ppfc_slack_increment: float,
                 reinsert_with_ppfc: bool,
                 verbose: bool = False) -> None:

        self.verbose = verbose

        # ALNS  parameters
        self.max_iter_seg = max_iter_seg
        self.remove_num_interval = [round(remove_percentage_interval[0] * len(problem_data.order_nodes)),
                                    round(remove_percentage_interval[1] * len(problem_data.order_nodes))]
        self.remove_num_adjust = math.ceil(remove_num_percentage_adjust * len(problem_data.order_nodes))
        self.determinism_param = determinism_param
        self.noise_param = noise_param
        self.noise_destination_param = noise_destination_param
        self.max_iter_same_solution = max_iter_same_solution
        self.iter_same_solution = 0
        self.reinsert_with_ppfc = reinsert_with_ppfc

        # Solutions
        self.current_sol = self.repair_kregret(2, Solution(problem_data))
        self.production_model = ProductionModel(prbl=problem_data,
                                                demands=self.current_sol.get_demand_dict(),
                                                inventory_reward_extension=inventory_reward)
        self.production_heuristic = ProductionProblemHeuristic(ProductionProblem(problem_data))

        # Ensure prod feasibility
        self.current_sol = self.adjust_sol_exact(self.current_sol, remove_num=self.remove_num_adjust)
        self.current_sol_cost = self.current_sol.get_solution_routing_cost()
        self.best_sol = self.current_sol
        self.best_sol_routing_cost = round(self.current_sol_cost)
        self.best_sol_total_cost = self.best_sol_routing_cost + self.production_heuristic.get_cost(routing_sol=self.best_sol)
        self.record_solution(self.current_sol)
        self.percentage_best_solutions_production_solved = percentage_best_solutions_production_solved

        # Operator weights, scores and usage
        noise_op = [True, False]
        self.weight_min_threshold = weight_min_threshold
        self.destroy_op_weight = {op: self.weight_min_threshold for op in destroy_op}
        self.repair_op_weight = {op: self.weight_min_threshold for op in repair_op}
        self.noise_op_weight = {op: self.weight_min_threshold for op in noise_op}
        self.destroy_op_score = {op: 0 for op in destroy_op}
        self.repair_op_score = {op: 0 for op in repair_op}
        self.noise_op_score = {op: 0 for op in noise_op}
        self.destroy_op_segment_usage = {op: 0 for op in destroy_op}
        self.repair_op_segment_usage = {op: 0 for op in repair_op}
        self.noise_op_segment_usage = {op: 0 for op in noise_op}
        self.reaction_param = reaction_param
        self.score_params = {i: score_params[i] for i in range(len(score_params))}

        self.cooling_rate = cooling_rate
        self.temperature = -(self.best_sol_routing_cost * start_temperature_controlparam) / math.log(0.5)

        self.it_seg_count = 0  # Iterations done in one segment - can maybe do this in run_alns_iteration?
        self.num_best_total_sol_updates = 0

        self.production_infeasibility_strike_max = production_infeasibility_strike_max
        self.ppfc_slack_increment = ppfc_slack_increment

        self.unique_initial_factory_visits: Set = set()
        self.performed_initial_factory_visits_permutations = 0

        # Relatedness operator parameters
        self.related_removal_weight_param = related_removal_weight_param
        self.relatedness_precedence = self.set_relatedness_precedence_dict(relatedness_precedence)

    def __repr__(self):
        destroy_op = [(k, round(v, 2)) for k, v in sorted(self.destroy_op_weight.items(), key=lambda item: item[0])]
        repair_op = [(k, round(v, 2)) for k, v in sorted(self.repair_op_weight.items(), key=lambda item: item[0])]
        return (
            f"Best solution with routing cost {self.best_sol_routing_cost}: \n"
            f"{self.best_sol} \n"
            # f"Current solution with routing cost {self.current_sol_cost}: \n"
            # f"{self.current_sol} \n"
            f"Insertion candidates (orders not served): {self.best_sol.get_orders_not_served()} \n"
            f"Destroy operators {destroy_op} \n"
            f"and repair operators {repair_op} \n")

    def set_relatedness_precedence_dict(self, d: Dict[Tuple[str, str], int]) -> Dict[Tuple[str, str], int]:
        # Returns a dict with key all combinations of zones (z1, z2) and value defined relatedness of two zones
        zones = set()
        original_keys: List[Tuple[str, str]] = list(d.keys())
        for (z1, z2) in original_keys:
            d[(z2, z1)] = d[(z1, z2)]  # symmetry
            zones.add(z1)
            zones.add(z2)
        for z in zones:
            d[(z, z)] = 0  # a zone is perfectly related to itself
        if len(zones) < len(self.current_sol.prbl.zones):
            print("Relatedness of zones is not properly defined.")
        return d

    def adjust_sol_exact(self, sol: Solution = None, remove_num: int = None) -> Solution:
        """
        :param sol: solution to be adjusted, default is self.current_sol
        :param remove_num: number of order nodes to be removed before checking for prod feasibility,
                default is self.remove_num
        :return: exact production feasible solution
        """
        sol = sol if sol else self.current_sol
        prod_feasible, infeasible_factory = self.production_heuristic.is_feasible(sol)
        while not prod_feasible:
            sol = self.remove_from_factory(sol, infeasible_factory, remove_num)
            prod_feasible, infeasible_factory = self.production_heuristic.is_feasible(sol)
        return sol

    def adjust_sol_ppfc(self, sol: Solution = None, remove_num: int = None, repair_op: str = None) -> Solution:
        """
        :param sol: solution to be adjusted, default is self.current_sol
        :param remove_num: number of order nodes to be removed before checking for prod feasibility,
                default is self.remove_num
        :param repair_op: repair operator
        :return: production feasible solution according to ppfc
        """
        sol = sol if sol else self.current_sol
        prod_feasible, infeasible_factory = sol.check_production_feasibility()
        self.ppfc_infeasible_count += int(not prod_feasible)
        infeasible = [infeasible_factory]
        while not prod_feasible:
            sol = self.remove_from_factory(sol, infeasible_factory, remove_num)
            prod_feasible, infeasible_factory = sol.check_production_feasibility()
            infeasible.append(infeasible_factory)

        # print("Infeasible:", infeasible)
        orders_not_served = len(sol.get_orders_not_served())
        if repair_op and self.reinsert_with_ppfc:  # Reinsert nodes using the ppfc check
            sol = self.repair(repair_op, sol, apply_noise=True, use_ppfc=True)
            # print(orders_not_served - len(sol.get_orders_not_served()), "orders reinserted")
        return sol

    def update_scores(self, destroy_op: str, repair_op: str, noise_op: bool, update_type: int) -> None:
        if update_type == -1:
            return
        elif update_type == -2:  # New global best routing, but not production feasible
            return
        elif update_type == -3:  # Solution was updated because max_iter_same_solution was reached
            return
        elif not 0 <= update_type < len(self.score_params):
            print("Update type does not exist. Scores not updated.")
            return
        self.destroy_op_score[destroy_op] += self.score_params[update_type]
        self.repair_op_score[repair_op] += self.score_params[update_type]
        self.noise_op_score[noise_op] += self.score_params[update_type]

    def update_weights(self) -> None:
        # Update weights based on scores, "blank" scores and operator segment usage
        # w = (1-r)w + r*(pi/theta)

        # Destroy
        for op in self.destroy_op_weight.keys():
            if self.destroy_op_segment_usage[op] > 0:
                self.destroy_op_weight[op] = max(((1 - self.reaction_param) * self.destroy_op_weight[op] +
                                                  self.reaction_param * self.destroy_op_score[op] /
                                                  self.destroy_op_segment_usage[op]),
                                                 self.weight_min_threshold)
            self.destroy_op_score[op] = 0
            self.destroy_op_segment_usage[op] = 0

        # Repair
        for op in self.repair_op_weight.keys():
            if self.repair_op_segment_usage[op] > 0:
                self.repair_op_weight[op] = max(((1 - self.reaction_param) * self.repair_op_weight[op] +
                                                 self.reaction_param * self.repair_op_score[op] /
                                                 self.repair_op_segment_usage[op]),
                                                self.weight_min_threshold)
            self.repair_op_score[op] = 0
            self.repair_op_segment_usage[op] = 0

        # Noise
        for op in self.noise_op_weight:
            if self.noise_op_segment_usage:
                self.noise_op_weight[op] = max(((1 - self.reaction_param) * self.noise_op_weight[op] +
                                                self.reaction_param * self.noise_op_score[op] /
                                                self.noise_op_segment_usage[op]),
                                               self.weight_min_threshold)

        self.it_seg_count = 0

    def choose_operators(self) -> Tuple[str, str, bool]:
        # Choose operators, probability of choosing an operator is based on current weights
        destroy_weights_normalized = (np.fromiter(self.destroy_op_weight.values(), float) /
                                      sum(self.destroy_op_weight.values()))
        destroy_op = np.random.choice(list(self.destroy_op_weight.keys()), p=destroy_weights_normalized)
        self.destroy_op_segment_usage[destroy_op] += 1

        noise_weights_normalized = (np.fromiter(self.noise_op_weight.values(), float) /
                                    sum(self.noise_op_weight.values()))
        noise_op = np.random.choice(list(self.noise_op_weight.keys()), p=noise_weights_normalized)
        self.noise_op_segment_usage[noise_op] += 1

        repair_weights_normalized = (np.fromiter(self.repair_op_weight.values(), float) /
                                     sum(self.repair_op_weight.values()))
        repair_op = np.random.choice(list(self.repair_op_weight.keys()), p=repair_weights_normalized)
        self.repair_op_segment_usage[repair_op] += 1

        # print(destroy_op, repair_op, 'noise' if noise_op else 'no noise')
        return destroy_op, repair_op, noise_op

    def generate_new_solution(self, destroy_op: str, repair_op: str, apply_noise: bool) -> Solution:
        # Generate new feasible solution x' based on given operators
        # Return x'
        candidate_sol = self.current_sol.copy()
        candidate_sol = self.destroy(destroy_op, candidate_sol)
        if random.random() < alnsparam.permute_chance:
            candidate_sol = self.permute_initial_factory_visits(candidate_sol)
        candidate_sol = self.repair(repair_op, candidate_sol, apply_noise)
        return candidate_sol

    def permute_initial_factory_visits(self, candidate_sol: Solution) -> Solution:
        permutation_found = candidate_sol.permute_factory_visits()
        self.performed_initial_factory_visits_permutations += int(permutation_found)
        for factory in candidate_sol.prbl.factory_nodes:
            self.unique_initial_factory_visits.add((factory, candidate_sol.get_initial_factory_visits(factory)))
        return candidate_sol

    def destroy(self, destroy_op: str, sol: Solution) -> Union[Solution, None]:
        remove_num = random.randint(*self.remove_num_interval)
        if destroy_op == "d_random":
            return self.destroy_random(sol, remove_num)
        elif destroy_op == "d_worst":
            return self.destroy_worst(sol, remove_num)
        elif destroy_op == "d_related_location_time":
            return self.destroy_related(sol, remove_num, rel_measure=self._relatedness_location_time)
        elif destroy_op == "d_related_location_precedence":
            return self.destroy_related(sol, remove_num, rel_measure=self._relatedness_location_precedence)
        elif destroy_op == "d_voyage_random":
            return self.destroy_voyage_random(sol, remove_num)
        elif destroy_op == "d_voyage_worst":
            return self.destroy_voyage_worst(sol, remove_num)
        elif destroy_op == "d_route_random":
            return self.destroy_route_random(sol, remove_num)
        elif destroy_op == "d_route_worst":
            return self.destroy_route_worst(sol, remove_num)
        else:
            print("Destroy operator does not exist")
            return None

    def remove_from_factory(self, sol: Solution, factory_node_id: str, remove_num: int = 1):
        # Compute removal candidates
        candidates = [(vessel, idx, sol.get_removal_utility(vessel, idx))
                      for vessel, idx in sol.get_order_vessel_idx_for_factory(factory_node_id)]
        candidates.sort(key=lambda tup: tup[2], reverse=True)

        removals = 0
        while removals < remove_num and candidates:
            chosen_idx = int(pow(random.random(), self.determinism_param) * len(candidates))
            vessel, idx, _ = candidates.pop(chosen_idx)
            sol.remove_node(vessel, idx)
            removals += 1

            # Recompute removal candidates
            candidates = [(vessel, idx, sol.get_removal_utility(vessel, idx))
                          for vessel, idx in sol.get_order_vessel_idx_for_factory(factory_node_id)]
            candidates.sort(key=lambda tup: tup[2], reverse=True)

        sol.recompute_solution_variables()
        return sol

    def destroy_random(self, sol: Solution, remove_num: int) -> Solution:
        served_orders = [(vessel, order)
                         for vessel in sol.prbl.vessels
                         for order in sol.routes[vessel]
                         if not sol.prbl.nodes[order].is_factory]
        random.shuffle(served_orders)
        removals = 0
        while removals < remove_num and served_orders:
            vessel, order = served_orders.pop()
            remove_index = sol.routes[vessel].index(order)
            sol.remove_node(vessel, remove_index)
            removals += 1
        sol.recompute_solution_variables()
        return sol

    def destroy_voyage_random(self, sol: Solution, remove_num: int) -> Solution:
        # voyage: factory visit + orders until next factory visit
        illegal_removals = sol.get_illegal_voyage_removals()
        voyage_start_indexes = [(vessel, idx)
                                for vessel in sol.prbl.vessels
                                for idx in range(len(sol.routes[vessel]) - 1)
                                if sol.prbl.nodes[sol.routes[vessel][idx]].is_factory
                                and len(sol.routes[vessel]) > 1
                                and (vessel, idx) not in illegal_removals]
        random.shuffle(voyage_start_indexes)
        destroy_voyage_vessels = []
        orders_removed = 0
        while orders_removed < remove_num and voyage_start_indexes:
            vessel, voyage_start_idx = voyage_start_indexes.pop()  # pick one voyage to destroy
            if vessel in destroy_voyage_vessels:
                # avoid choosing several voyages from the same vessel, as this is messy with the voyage_start_indexes
                continue
            remove_indexes = [i for i in range(voyage_start_idx, sol.get_temp_voyage_end_idx(vessel, voyage_start_idx))
                              if i > 0]  # don't remove initial factory
            for idx in reversed(remove_indexes):
                sol.remove_node(vessel, idx)
                orders_removed += 1
            orders_removed -= 1  # to adjust for one factory being removed
            destroy_voyage_vessels.append(vessel)

        sol.recompute_solution_variables()
        return sol

    def destroy_voyage_worst(self, sol: Solution, remove_num: int) -> Solution:
        # voyage: factory visit + orders until next factory visit
        illegal_removals = sol.get_illegal_voyage_removals()
        voyage_start_indexes = [(vessel, idx, sol.get_voyage_profit(vessel, idx))
                                for vessel in sol.prbl.vessels
                                for idx in range(len(sol.routes[vessel]) - 1)
                                if sol.prbl.nodes[sol.routes[vessel][idx]].is_factory
                                and len(sol.routes[vessel]) > 1
                                and (vessel, idx) not in illegal_removals]
        voyage_start_indexes.sort(key=lambda item: item[2], reverse=True)
        destroy_voyage_vessels = []
        orders_removed = 0
        while orders_removed < remove_num and voyage_start_indexes:
            chosen_idx = int(pow(random.random(), self.determinism_param) * len(voyage_start_indexes))
            vessel, voyage_start_idx, _ = voyage_start_indexes.pop(chosen_idx)  # pick one voyage to destroy
            if vessel in destroy_voyage_vessels:
                # avoid choosing several voyages from the same vessel, as this is messy with the voyage_start_indexes
                continue
            remove_indexes = [i for i in range(voyage_start_idx, sol.get_temp_voyage_end_idx(vessel, voyage_start_idx))
                              if i > 0]  # don't remove initial factory
            for idx in reversed(remove_indexes):
                sol.remove_node(vessel, idx)
                orders_removed += 1
            orders_removed -= 1  # to adjust for one factory being removed
            destroy_voyage_vessels.append(vessel)

        sol.recompute_solution_variables()
        return sol

    def destroy_route_random(self, sol: Solution, remove_num: int) -> Solution:
        # choose a vessel that has a route
        illegal_removals = sol.get_illegal_route_removals()
        vessel = random.choice([vessel for vessel in sol.prbl.vessels
                                if len(sol.routes[vessel]) > 1
                                and vessel not in illegal_removals])
        route = sol.routes[vessel]
        for idx in range(len(route) - 1, 0, -1):
            sol.remove_node(vessel, idx)
        sol.recompute_solution_variables()
        return sol

    def destroy_route_worst(self, sol: Solution, remove_num: int) -> Solution:
        illegal_removals = sol.get_illegal_route_removals()
        route_profits = [(vessel, sol.get_route_profit(vessel))
                         for vessel in sol.prbl.vessels
                         if len(sol.routes[vessel]) > 1
                         and vessel not in illegal_removals]
        route_profits.sort(key=lambda item: item[1], reverse=True)  # descending order

        chosen_idx = int(pow(random.random(), self.determinism_param) * len(route_profits))
        vessel_for_worst_route = route_profits.pop(chosen_idx)[0]
        route = sol.routes[vessel_for_worst_route]
        for idx in range(len(route) - 1, 0, -1):
            sol.remove_node(vessel_for_worst_route, idx)
        sol.recompute_solution_variables()
        return sol

    def destroy_worst(self, sol: Solution, remove_num: int) -> Solution:
        candidates = True  # dummy initialization
        removals = 0
        while removals < remove_num and candidates:
            candidates = [(vessel, idx, sol.get_removal_utility(vessel, idx))
                          for vessel in sol.prbl.vessels
                          for idx, order in enumerate(sol.routes[vessel])
                          if not sol.prbl.nodes[order].is_factory]
            candidates.sort(key=lambda tup: tup[2], reverse=True)
            chosen_idx = int(pow(random.random(), self.determinism_param) * len(candidates))
            vessel, idx, _ = candidates.pop(chosen_idx)
            sol.remove_node(vessel, idx)
            removals += 1
        sol.recompute_solution_variables()
        return sol

    def _relatedness_location_time(self, order1: str, order2: str, vessel: str) -> float:
        w_0 = self.related_removal_weight_param['relatedness_location_time'][0]
        w_1 = self.related_removal_weight_param['relatedness_location_time'][1]

        time_window_difference = (abs(self.current_sol.prbl.nodes[order1].tw_start
                                      - self.current_sol.prbl.nodes[order2].tw_start) +
                                  abs(self.current_sol.prbl.nodes[order1].tw_end
                                      - self.current_sol.prbl.nodes[order2].tw_end))

        # Normalize difference with respect to n.o. time periods in problem
        time_window_difference /= self.current_sol.prbl.no_time_periods

        return w_0 * self.current_sol.prbl.transport_times_exact[vessel, order1, order2] + w_1 * time_window_difference

    def _relatedness_location_precedence(self, order1: str, order2: str, vessel: str) -> float:
        w_0 = self.related_removal_weight_param['relatedness_location_precedence'][0]
        w_1 = self.related_removal_weight_param['relatedness_location_precedence'][1]

        return (w_0 * self.current_sol.prbl.transport_times_exact[vessel, order1, order2] +
                w_1 * self.relatedness_precedence[(self.current_sol.prbl.nodes[order1].zone,
                                                   self.current_sol.prbl.nodes[order2].zone)])

    def destroy_related(self, sol: Solution, remove_num: int, rel_measure: function) -> Solution:
        # Related removal, based on inputted relatedness measure
        similar_orders: List[Tuple[str, int, str]] = []  # (order, index, vessel)
        served_orders = [(sol.routes[v][idx], idx, v)  # (order, index, vessel)
                         for v in sol.prbl.vessels
                         for idx in range(len(sol.routes[v]))
                         if not sol.prbl.nodes[sol.routes[v][idx]].is_factory]

        # Select a random first order
        random_first_idx = random.randint(0, len(served_orders) - 1)
        similar_orders.append(served_orders.pop(random_first_idx))

        while len(similar_orders) < remove_num and served_orders:
            base_order = similar_orders[random.randint(0, len(similar_orders) - 1)][0]
            candidates = [(order, idx, vessel, rel_measure(base_order, order, vessel))
                          for vessel in sol.prbl.vessels
                          for idx, order in enumerate(sol.routes[vessel])
                          if (order, idx, vessel) in served_orders]
            candidates.sort(key=lambda tup: tup[3])  # sort according to relatedness asc (most related orders first)

            chosen_related = candidates[int(pow(random.random(), self.determinism_param) * len(candidates))][:3]
            similar_orders.append(chosen_related)
            served_orders.remove(chosen_related)

        similar_orders.sort(key=lambda tup: tup[1], reverse=True)  # sort according to idx desc

        for (order, idx, vessel) in similar_orders:
            sol.remove_node(vessel=vessel, idx=idx)

        sol.recompute_solution_variables()
        return sol

    def repair(self, repair_op: str, sol: Solution, apply_noise: bool, use_ppfc: bool = False) -> Union[None, Solution]:
        if repair_op == "r_greedy":
            repaired_sol = self.repair_greedy(sol, apply_noise, use_ppfc)
        elif repair_op == "r_2regret":
            repaired_sol = self.repair_kregret(2, sol, apply_noise, use_ppfc)
        elif repair_op == 'r_3regret':
            repaired_sol = self.repair_kregret(3, sol, apply_noise, use_ppfc)
        else:
            print("Repair operator does not exist")
            return None

        if not use_ppfc:  # If PPFC was not used during repair, we remove orders until PPFC is satisfied
            repaired_sol = self.adjust_sol_ppfc(repaired_sol, self.remove_num_adjust, repair_op)

        return repaired_sol

    def repair_greedy(self, sol: Solution, apply_noise: bool = False, ppfc: bool = False) -> Solution:
        insertion_cand = sol.get_orders_not_served()
        insertions = [(node_id, vessel, idx, sol.get_insertion_utility(sol.prbl.nodes[node_id], vessel, idx,
                                                                       apply_noise * self.noise_param))
                      for node_id in insertion_cand
                      for vessel in sol.prbl.vessels
                      for idx in range(1, len(sol.routes[vessel]) + 1)]
        insertions.sort(key=lambda tup: tup[3])  # sort by gain

        while len(insertion_cand) > 0:  # try all possible insertions
            insert_node_id, vessel, idx, _ = insertions[-1]
            if sol.check_insertion_feasibility(insert_node_id, vessel, idx,
                                               noise_factor=apply_noise * self.noise_destination_param, ppfc=ppfc):
                sol.insert_last_checked()
                insertion_cand.remove(insert_node_id)
                # recalculate profit gain and omit other insertions of node_id
                insertions = [(node_id, vessel, idx, sol.get_insertion_utility(sol.prbl.nodes[node_id], vessel, idx,
                                                                               apply_noise * self.noise_param))
                              for node_id, vessel, idx, utility in insertions if node_id != insert_node_id]
                insertions.sort(key=lambda item: item[3])  # sort by gain

                if self.verbose:
                    sol.print_routes(highlight=[(vessel, idx)])
                    print()
            else:
                sol.clear_last_checked()
                insertions.pop()

                # Remove node_id from insertion candidate list if no insertions left for this node_id
                if len([insert for insert in insertions if insert[0] == insert_node_id]) == 0:
                    insertion_cand.remove(insert_node_id)
        return sol

    def repair_kregret(self, k: int, sol: Solution, apply_noise: bool = False, ppfc: bool = False) -> Solution:
        unrouted_orders = sol.get_orders_not_served()
        while unrouted_orders:
            # find the largest regret for each order and insert the largest regret.
            # repeat until no unrouted orders, or no feasible insertion
            repair_candidates: List[Tuple[str, int, str, float]] = []  # (order, idx, vessel, regret)

            for order in unrouted_orders:
                # Get all insertion utilities
                insertions = [(idx, v, sol.get_insertion_utility(sol.prbl.nodes[order], v, idx,
                                                                 apply_noise * self.noise_param))
                              for v in sol.prbl.vessels for idx in range(1, len(sol.routes[v]) + 1)]
                insertions.sort(key=lambda item: item[2])  # sort by gain

                insertion_regret = 0
                num_feasible = 0
                best_insertion: Tuple[str, int, str] = tuple()

                while num_feasible < k and len(insertions) > 0:
                    idx, vessel, util = insertions.pop()
                    feasible = sol.check_insertion_feasibility(order, vessel, idx,
                                                               noise_factor=apply_noise * self.noise_destination_param,
                                                               ppfc=ppfc)
                    sol.clear_last_checked()

                    if feasible and num_feasible == 0:
                        insertion_regret += (k - 1) * util  # inserting for k-1 terms in sum
                        best_insertion = (order, idx, vessel)
                        num_feasible += 1
                    elif feasible and num_feasible > 0:
                        insertion_regret -= util
                        num_feasible += 1

                if num_feasible < k:  # less than k feasible solutions found -> num_feasible * infinite regret
                    insertion_regret -= - (k - num_feasible) * int_inf

                if num_feasible >= 1 and best_insertion:  # at least one feasible solution found
                    node_id, idx, vessel = best_insertion
                    repair_candidates.append((node_id, idx, vessel, insertion_regret))

            if not repair_candidates:  # no orders to insert feasibly
                return sol

            # insert the greatest regret from repair_candidates
            node_id, idx, vessel, _ = max(repair_candidates, key=lambda item: item[3])
            sol.check_insertion_feasibility(node_id, vessel, idx,
                                            noise_factor=apply_noise * self.noise_destination_param)
            sol.insert_last_checked()
            unrouted_orders.remove(node_id)

            if self.verbose:
                sol.print_routes(highlight=[(vessel, idx)])
                print()

        return sol

    def accept_solution(self, sol: Solution) -> Tuple[bool, int, int]:  # accept, update_type, cost
        """
        :param sol: solution to be compared with self.current_sol
        :return: True if sol should be accepted to self.current_sol, else False, update_type and cost of new solution
        """
        sol_cost = sol.get_solution_routing_cost()

        # If routing solution within 5% of best routing solution, check production cost/feasibility
        if sol_cost < self.best_sol_routing_cost * (1 + self.percentage_best_solutions_production_solved):
            prod_cost = self.production_heuristic.get_cost(routing_sol=sol)
            accept = prod_cost < math.inf
            if not accept:
                return False, -2, sol_cost
            elif (accept and prod_cost + sol_cost < self.best_sol_total_cost and
                sol.get_solution_hash() not in self.previous_solutions):
                self.best_sol_total_cost = prod_cost + sol_cost
                self.num_best_total_sol_updates += 1
                return True, 0, sol_cost

        # if sol_cost < self.best_sol_cost:
        #     accept, _ = self.production_heuristic.is_feasible(sol)
        #     # exact_accept = self.production_model.is_feasible(sol)
        #     # if accept != exact_accept:
        #     #     print(f"Heuristic result ({accept})  deviates from exact ({exact_accept}). Sol cost: {sol_cost}")
        #     update_type = 0 if accept else -2  # -2 means new global best was not production feasible
        #     return accept, update_type, sol_cost
        if sol_cost < self.current_sol_cost:
            update_type = -1 if sol.get_solution_hash() in self.previous_solutions else 1
            return True, update_type, sol_cost
        elif self.iter_same_solution < self.max_iter_same_solution:
            # Simulated annealing criterion
            prob = pow(math.e, -((sol_cost - self.current_sol_cost) / self.temperature))
            accept = np.random.choice(np.array([True, False]), p=(np.array([prob, (1 - prob)])))
            update_type = -1 if sol.get_solution_hash() in self.previous_solutions else -1 + 3 * accept
            return accept, update_type, sol_cost
        else:  # maximum number of iterations from same current solution is reached, solution is accepted
            print("Solution is accepted because maximum was reached,", self.iter_same_solution)
            if sol.get_solution_hash() not in self.previous_solutions:
                self.record_solution(sol)  # operators not rewarded (negative accept type), even if solution is new
            return True, -3, sol_cost

    def record_solution(self, sol: Solution) -> None:
        if self.verbose:
            print(f"> Recording solution hash...")
        self.previous_solutions.add(sol.get_solution_hash())

    def run_alns_iteration(self) -> None:
        # Choose a destroy heuristic and a repair heuristic based on adaptive weights wdm
        # for method d in the current segment of iterations m
        d_op, r_op, noise_op = self.choose_operators()

        # Generate a candidate solution x′ from the current solution x using the chosen destroy and repair heuristics
        candidate_sol: Solution
        candidate_sol = self.generate_new_solution(destroy_op=d_op, repair_op=r_op, apply_noise=noise_op)

        # if x′ is accepted by a simulated annealing–based acceptance criterion then set x = x′
        accept_solution, self.update_type, cost = self.accept_solution(candidate_sol)

        if accept_solution:
            self.iter_same_solution = 0
            self.current_sol = candidate_sol
            self.current_sol_cost = cost
            if self.update_type > -1:  # if we except a solution that has not been accepted before
                self.record_solution(candidate_sol)
            if self.verbose:
                print(f'> Solution is accepted as current solution')
        else:
            self.iter_same_solution += 1

        # Update scores πd of the destroy and repair heuristics - dependent on how good the solution is
        self.update_scores(destroy_op=d_op, repair_op=r_op, noise_op=noise_op, update_type=self.update_type)

        # if IS iterations has passed since last weight update then
        # Update the weight wdm+1 for method d for the next segment m + 1 based on
        # the scores πd obtained for each method in segment m
        if self.it_seg_count == self.max_iter_seg:
            self.update_weights()

        # if f(x) > f(x∗) then
        # set x∗ =x
        if self.update_type == 0:  # type 0 means global best solution is found
            self.best_sol = self.current_sol
            self.best_sol_routing_cost = round(self.current_sol_cost)
            self.new_best_solution_feasible_production_count += 1
            if self.verbose:
                print(f'> Solution is accepted as best solution')
            # print("New best solutions' routing obj:", self.best_sol_routing_cost)

        if self.update_type == -2:  # type -2 means solution gave global best routing, but was not production feasible
            self.new_best_solution_infeasible_production_count += 1
            self.production_infeasibility_strike += 1
            if self.production_infeasibility_strike > self.production_infeasibility_strike_max:
                self.current_sol.ppfc_slack_factor += self.ppfc_slack_increment
        else:
            self.production_infeasibility_strike = 0
            self.current_sol.ppfc_slack_factor = 1.0

        # update iteration parameters
        self.temperature = self.temperature * self.cooling_rate
        self.it_seg_count += 1

    def write_to_file(self, excel_writer: pd.ExcelWriter, id: int, stop_criterion: str, alns_iter: int,
                      alns_time: int, parameter_tune: str = None, parameter_tune_value=None) -> None:
        cooling_rate = str(0.999) if alns_iter == 0 else str(round(math.pow(0.002, (1 / alns_iter)), 4))
        solution_dict = {'obj_val': round(self.best_sol_total_cost, 2),
                         'production_cost': round(self.best_sol_production_cost, 2),
                         'routing_cost': round(self.best_sol_routing_cost, 2),
                         'transport_cost': self.best_sol.get_solution_transport_cost(),
                         'num_orders_not_served': len(self.best_sol.get_orders_not_served()),
                         'stop_crierion': stop_criterion,
                         'num_iterations': alns_iter,
                         'time [sec]': alns_time,
                         'score_params': str(alnsparam.score_params),
                         'reaction_param': str(alnsparam.reaction_param),
                         'start_temperature_controlparam': str(alnsparam.start_temperature_controlparam),
                         'cooling_rate': cooling_rate,
                         'remove_percentage_interval': str(alnsparam.remove_percentage_interval),
                         'noise_param': str(alnsparam.noise_param),
                         'determinism_param': str(alnsparam.determinism_param),
                         'related_removal_weight_param': str(alnsparam.related_removal_weight_param),
                         'noise_destination_param': str(alnsparam.noise_destination_param)}

        if parameter_tune is not None:
            if parameter_tune == 'relatedness_location_time':
                solution_dict['related_removal_weight_param'] = str(parameter_tune_value) + str(
                    alnsparam.related_removal_weight_param['relatedness_location_precedence'])
            elif parameter_tune == 'relatedness_location_precedence':
                solution_dict['related_removal_weight_param'] = str(
                    alnsparam.related_removal_weight_param['relatedness_location_time']) + str(parameter_tune_value)
            else:
                solution_dict[parameter_tune] = str(parameter_tune_value)

        # TODO: Add relevant data
        if id == -1:
            id = "initial"
        sheet_name = "run_" + str(id)
        df = pd.DataFrame(solution_dict, index=[0]).transpose()
        df.to_excel(excel_writer, sheet_name=sheet_name, startrow=1)


text:
import functools
import os
from pathlib import Path

import numpy as np
import onnxruntime as rt
import requests
from PIL import Image
from tqdm import tqdm

class Segmenter:
    """An object used for classifying images.
    Contains the ONNX refernce and has methods that
    take images as an input and output classified masks.
    """

    def __init__(
        self,
    ):

        self.checkpoint_path = Path(__file__).parent / "FPN-resnet50-imagenet.onnx"

        # If ONNX file is not available locally.
        if not self.checkpoint_path.exists():
            print("Downloading ONNX file...")
            self.download_checkpoint()
            print("Complete.\n")

        self.onnx_session = rt.InferenceSession(str(self.checkpoint_path))

    @staticmethod
    def read_image(image_path):
        return Image.open(image_path).convert("RGB")

    @staticmethod
    def to_tensor_image(x, **kwargs):
        return x.transpose(2, 0, 1).astype("float32")

    @staticmethod
    def transform_input(x, mean=None, std=None, input_space="RGB", input_range=None, **kwargs):
        """
        Reformat image input, as necessary to apply classifier.
        From segmentation-models-pytorch package.
        """

        if input_space == "BGR":
            x = x[..., ::-1].copy()

        if input_range is not None:
            if x.max() > 1 and input_range[1] == 1:
                x = x / 255.0

        if mean is not None:
            mean = np.array(mean)
            x = x - mean

        if std is not None:
            std = np.array(std)
            x = x / std

        return x

    def segment_image(self, image):
        """ Apply a classifier to a single image, given as PIL Image. """

        # Pre-process image.
        # Preprocess fun is based on segmentation-models-pytorch package.
        image = np.asarray(image)

        params = {
            "input_space": "RGB",
            "input_size": [3, 224, 224],
            "input_range": [0, 1],
            "mean": [0.485, 0.456, 0.406],
            "std": [0.229, 0.224, 0.225],
        }
        preprocess_fun = functools.partial(self.transform_input, **params)
        image = preprocess_fun(image)

        image = self.to_tensor_image(image)
        image = np.expand_dims(image, 0)

        # Produce raw prediction.
        input_name = self.onnx_session.get_inputs()[0].name
        output_name = self.onnx_session.get_outputs()[0].name
        prediction = self.onnx_session.run([output_name], {input_name: image})[0]

        # Format output and return.
        return prediction.squeeze().round().astype(bool)

    def run_images(self, image_paths):
        """
        Upper level wrapper to classify a series of images.
        Takes a list of file paths as input.
        """

        predictions = [None] * len(image_paths)  # initialize the predictions list

        # Loop through images and generate predictions.
        print("Classifying images...")
        for ii in range(len(image_paths)):
            print("Classifying image " + str(ii + 1) + " of " + str(len(image_paths)) + ".")
            image = Image.open(image_paths[ii]).convert("RGB")  # read in image
            predictions[ii] = self.segment_image(image)  # run classifier on image
        print("Finished classifying.\n")

        return predictions

    @staticmethod
    def save_predictions(predictions, image_paths, folder="output"):
        """ Utility to save predictions to the specified folder. """

        # Loop through image paths.
        for ii in range(len(predictions)):
            Image.fromarray(predictions[ii]).save(folder + os.path.sep + os.path.basename(image_paths[ii]))

        print("Images saved.")

    def download_checkpoint(self):
        """
        Get ONNX file is not currently available locally.
        Based on:
        https://stackoverflow.com/questions/37573483/progress-bar-while-download-file-over-http-with-requests
        """
        checkpoint_filename = self.checkpoint_path.name

        expected_checkpoint_filenames = ["FPN-resnet50-imagenet.onnx"]

        assert (
            checkpoint_filename in expected_checkpoint_filenames
        ), f"Expected checkpoint file name to be in {checkpoint_filename}."

        # url = os.path.join(CHECKPOINT_URL_BASE, checkpoint_filename)
        # TODO: Replace temporary checkpoint url.
        url = "https://uni-duisburg-essen.sciebo.de/s/J7bS47nZadg4bBH/download"
        request_stream = requests.get(url, stream=True)

        # Total size in bytes.
        total_size = int(request_stream.headers.get("content-length", 0))
        block_size = 1024  # 1 Kibibyte

        print(f"Downloading checkpoint file from {url}...")

        # print(f"Downloading checkpoint file from {url}...")
        progress_bar = tqdm(total=total_size, unit="iB", unit_scale=True)
        with open(self.checkpoint_path, "wb") as file:
            for data in request_stream.iter_content(block_size):
                progress_bar.update(len(data))
                file.write(data)
        progress_bar.close()
        if total_size != 0 and progress_bar.n != total_size:
            raise RuntimeError("Error while downloading checkpoint file.")

max_stars_repo_name: Hamed-NKR/atems

max_issues_repo_name: Hamed-NKR/atems

max_forks_repo_name: Hamed-NKR/atems


text:
from __future__ import annotations
from typing import TYPE_CHECKING
#import numpy as np
import h5py
from ..h5_utils import read_basic_element, h5py_to_dataframe
if TYPE_CHECKING:  # pragma: no cover
    from pyNastran.bdf.bdf import BDF

def read_cshear(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    #('EID', 'PID', 'G', 'DOMAIN_ID')
    read_basic_element(group, geom_model, geom_model.add_cshear)
    #EID = group['EID']
    #PID = group['PID']
    #NIDS = group['G']
    #DOMAIN_ID = group['DOMAIN_ID']
    #for eid, pid, nids in zip(EID, PID, NIDS):
        #obj = geom_model.add_cshear(eid, pid, nids, comment='')
        #obj.validate()

def read_ctria3(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    geom_model.card_count[name] = len(group['EID'])
    setattr(geom_model, name, group)
    return
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3 = [ti if ti != -1.0 else None
                      for ti in t]
        obj = geom_model.add_ctria3(
            eid, pid, nids,
            theta_mcid=theta_mcid, zoffset=zoffs, tflag=tflag,
            T1=t1, T2=t2, T3=t3, comment='')
        obj.validate()

def read_cquad4(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    """
    Dataset:
    attrs  : <Attributes of HDF5 object at 1739457403880>
    chunks : (270,)
    compression : 'gzip'
    compression_opts : 1
    dims   : <Dimensions of HDF5 object at 1739457403880>
    dtype  : dtype([('EID', '<i8'), ('PID', '<i8'), ('G', '<i8', (4,)), ('THETA', '<f8'), ('ZOFFS', '<f8'), ('TFLAG', '<i8'), ('T', '<f8', (4,)), ('MCID', '<i8'), ('DOMAIN_ID', '<i8')])
    external : None
    file   : <HDF5 file "6+element-nastran-sol103.h5" (mode r)>
    fillvalue : (0, 0, [0, 0, 0, 0], 0., 0., 0, [0., 0., 0., 0.], 0, 0)
    fletcher32 : False
    id     : <h5py.h5d.DatasetID object at 0x00000194FFBD9BE8>
    is_virtual : False
    maxshape : (None,)
    name   : '/NASTRAN/INPUT/ELEMENT/CQUAD4'
    nbytes : 4320
    ndim   : 1
    parent : <HDF5 group "/NASTRAN/INPUT/ELEMENT" (1 members)>
    ref    : <HDF5 object reference>
    regionref : <h5py._hl.base._RegionProxy object at 0x00000194FF9AA048>
    scaleoffset : None
    shape  : (36,)
    shuffle : True
    size   : 36
    """
    geom_model.card_count[name] = len(group['EID'])
    setattr(geom_model, name, group)
    return
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3, t4 = [ti if ti != -1.0 else None
                          for ti in t]
        obj = geom_model.add_cquad4(
            eid, pid, nids,
            theta_mcid=theta_mcid, zoffset=zoffs, tflag=tflag,
            T1=t1, T2=t2, T3=t3, T4=t4, comment='')
        obj.validate()

def read_ctria6(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3 = [ti if ti != -1.0 else None
                      for ti in t]
        obj = geom_model.add_ctria6(
            eid, pid, nids, theta_mcid=theta_mcid, zoffset=zoffs,
            tflag=tflag, T1=t1, T2=t2, T3=t3, comment='')
        obj.validate()

def read_cquad8(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3, t4 = [ti if ti != -1.0 else None
                          for ti in t]
        obj = geom_model.add_cquad8(
            eid, pid, nids, theta_mcid=theta_mcid, zoffset=zoffs,
            tflag=tflag, T1=t1, T2=t2, T3=t3, T4=t4, comment='')
        obj.validate()

def read_ctriar(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3= [ti if ti != -1.0 else None
                     for ti in t]
        obj = geom_model.add_ctriar(
            eid, pid, nids,
            theta_mcid=theta_mcid, zoffset=zoffs, tflag=tflag,
            T1=t1, T2=t2, T3=t3, comment='')
        obj.validate()

def read_cquadr(name: str, group: h5py._hl.dataset.Dataset, geom_model: BDF) -> None:
    #('EID', 'PID', 'G', 'THETA', 'ZOFFS', 'TFLAG', 'T', 'MCID', 'DOMAIN_ID')
    EID = group['EID']
    PID = group['PID']
    NIDS = group['G']
    THETA = group['THETA']
    ZOFFS = group['ZOFFS']
    TFLAG = group['TFLAG']
    T = group['T']
    MCID = group['MCID']
    DOMAIN_ID = group['DOMAIN_ID']
    for eid, pid, nids, theta, zoffs, tflag, t, mcid in zip(EID, PID, NIDS, THETA, ZOFFS, TFLAG, T, MCID):
        if mcid == -1:
            theta_mcid = theta
        else:
            asdf
        assert tflag == 0, tflag
        t1, t2, t3, t4 = [ti if ti != -1.0 else None
                          for ti in t]
        obj = geom_model.add_cquadr(
            eid, pid, nids,
            theta_mcid=theta_mcid, zoffset=zoffs, tflag=tflag,
            T1=t1, T2=t2, T3=t3, T4=t4, comment='')
        obj.validate()

max_stars_repo_name: luzpaz/pyNastran

max_issues_repo_name: luzpaz/pyNastran

max_forks_repo_name: luzpaz/pyNastran


text:
import pandas as pd
import ast
import numpy as np
from os import path
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import streamlit as st
# import plotly.express as px

def min_max_normalization(df_min_max):
    df_min_max = 1 + (df_min_max - df_min_max.min()) / (df_min_max.max() - df_min_max.min()) * 9
    return round(df_min_max, 2)

def mean_normalization(df_min_max):
    df_min_max = 1 + (df_min_max - df_min_max.mean()) / (df_min_max.max() - df_min_max.min()) * 9
    return round(df_min_max, 2)

def check_author(df, author, no_of_author):
    df = df.loc[df.author == author]
    df.sort_values(by=['minmax_norm_ratings'], ascending=[False])
    return df.head(no_of_author)

# Plot Authors and their total awards
def make_plot(df, x_label, y_label, title, st, kind):
    plt.style.use('ggplot')
    plt.rc('font', size=15)

    if kind != 'pie':
        fig, ax = plt.subplots(figsize=(12,10))
        ax = df.plot(kind=kind)
        ax.set_ylabel(y_label)
        ax.set_xlabel(x_label)
    else:
        pie_labels = df.index.values
        pie_data=df.values
        fig, ax = plt.subplots(figsize=(8,5))
        ax.pie(pie_data, labels=pie_labels, autopct='%1.1f%%', shadow=False)
        ax.axis('equal')

    ax.set_title(title+'\n')
    st.pyplot(fig)

def text_by_image(df,option,count=100,id=all):
    # if id is all:
    #     text = ' '.join([str(d) for d in df])
    # else:
    #     text = df[id]
    text = df['description'][df.title == option].values[0]

    # Create and generate a word cloud image:
    wordcloud = WordCloud(max_font_size=50, max_words=count, background_color="white").generate(text)
    fig, ax = plt.subplots()
    ax.imshow(wordcloud, interpolation='bilinear')
    ax.axis("off")
    st.write(fig)

### Get genre and places total counts and mean average ratings

def tranform_places_genres(df):

    genres_rating = {}
    genres_counts = {}

    places_rating = {}
    places_counts = {}

    genres,places,avg_rating = df.genres, df.places, df.avg_rating

    for id in range(len(avg_rating)):
        clean_genre = ast.literal_eval(genres[id])
        clean_places = ast.literal_eval(places[id])
        if len(clean_genre) != 0:
            for ng in clean_genre:
                for i in ng.split('>'):
                    genr = i.strip()
                    if genr in genres_rating:
                        genres_rating[genr].append(avg_rating[id])
                    else:
                        genres_rating[genr] = [avg_rating[id]]
                    if genr in genres_counts:
                        genres_counts[genr] += 1
                    else:
                        genres_counts[genr] = 1
        #for places
        if len(clean_places) != 0:
            for cp in clean_places:
                plac = cp.strip()
                if plac in places_rating:
                    places_rating[plac].append(avg_rating[id])
                else:
                    places_rating[plac] = [avg_rating[id]]
                if plac in places_counts:
                    places_counts[plac] += 1
                else:
                    places_counts[plac] = 1

    #genres
    for g in genres_rating.keys():
        genres_rating[g] = np.round(np.mean(genres_rating[g]),decimals=3)

    dic_genres_rating = dict(zip(genres_rating.keys(), genres_rating.values()))
    df_genres_rating = pd.DataFrame.from_dict(dic_genres_rating, orient='index',\
        columns=['mean_ratings']).sort_values(by='mean_ratings',ascending=False)

    dic_genres_counts = dict(zip(genres_counts.keys(), genres_counts.values()))
    df_genres_counts = pd.DataFrame.from_dict(dic_genres_counts, orient='index',\
        columns=['total_counts']).sort_values(by='total_counts',ascending=False)

    #places
    for g in places_rating.keys():
        places_rating[g] = np.round(np.mean(places_rating[g]),decimals=3)

    dic_places_rating = dict(zip(places_rating.keys(), places_rating.values()))
    df_places_rating = pd.DataFrame.from_dict(dic_places_rating, orient='index',\
        columns=['mean_ratings']).sort_values(by='mean_ratings',ascending=False)

    dic_places_counts = dict(zip(places_counts.keys(), places_counts.values()))
    df_places_counts = pd.DataFrame.from_dict(dic_places_counts, orient='index',\
        columns=['total_counts']).sort_values(by='total_counts',ascending=False)

    return df_genres_rating, df_genres_counts, df_places_rating, df_places_counts

max_stars_repo_name: TonyMTH/best-books-app

max_issues_repo_name: TonyMTH/best-books-app

max_forks_repo_name: TonyMTH/best-books-app


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

r"""Acme utils.
"""

import functools
from typing import Any, Dict, Optional

from acme import adders
from acme import core
from acme import wrappers
from acme.agents.jax import dqn
from acme.jax import networks as networks_lib
from acme.jax import utils
from balloon_learning_environment.agents import marco_polo_exploration
from balloon_learning_environment.agents import networks
from balloon_learning_environment.agents import random_walk_agent
from balloon_learning_environment.env import balloon_env
from balloon_learning_environment.env import simulator_data
from balloon_learning_environment.utils import units
import dm_env
from flax import linen as nn
import jax
import jax.numpy as jnp
import numpy as np
import optax
import rlax

def _balloon_is_within_radius(state: simulator_data.SimulatorState,
                              radius: units.Distance,
                              max_episode_length: int) -> float:
  balloon_state = state.balloon_state
  return (units.relative_distance(balloon_state.x, balloon_state.y) <=
          radius) / max_episode_length

def create_env(is_eval: bool, max_episode_length: int) -> dm_env.Environment:
  """Creates a BLE environment."""
  env = balloon_env.BalloonEnv()
  if is_eval:
    env = balloon_env.BalloonEnv(
        reward_function=functools.partial(
            _balloon_is_within_radius,
            radius=env.radius,
            max_episode_length=max_episode_length))
  env = wrappers.gym_wrapper.GymWrapper(env)
  env = wrappers.step_limit.StepLimitWrapper(
      env, step_limit=max_episode_length)
  env = wrappers.SinglePrecisionWrapper(env)
  return env

class QuantileNetwork(nn.Module):
  """Network used to compute the agent's return quantiles."""
  num_actions: int
  num_layers: int
  hidden_units: int
  num_atoms: int = 51
  inputs_preprocessed: bool = False

  @nn.compact
  def __call__(self, x: jnp.ndarray):
    ble_quantile_network = networks.QuantileNetwork(self.num_actions,
                                                    self.num_layers,
                                                    self.hidden_units,
                                                    self.num_atoms,
                                                    self.inputs_preprocessed)
    def batched_network(x):
      return ble_quantile_network(x)
    # Make network batched, since this is what Acme expects.
    output = jax.vmap(batched_network)(x)
    return {'q_dist': output.logits, 'q_values': output.q_values}

class CombinedActor(core.Actor):
  """Combines Acme's actor with MarcoPoloExploration exploration actor."""

  def __init__(
      self,
      actor: core.Actor,
      exploration_actor: marco_polo_exploration.MarcoPoloExploration,
  ):
    self._actor = actor
    self._exploration_actor = exploration_actor

  def select_action(self, observation: networks_lib.Observation):
    action = self._actor.select_action(observation)
    action = self._exploration_actor.step(0, observation, action)
    return np.array(action, dtype=np.int32)

  def observe_first(self, timestep: dm_env.TimeStep):
    self._actor.observe_first(timestep)
    self._exploration_actor.begin_episode(timestep.observation, 42)

  def observe(self, action: networks_lib.Action,
              next_timestep: dm_env.TimeStep):
    self._actor.observe(action, next_timestep)

  def update(self, wait: bool = False):
    self._actor.update(wait)

def marco_polo_actor(make_actor_fn):
  """Wraps make_actor_fn to include MarcoPoloExploration."""
  def make_actor(
      random_key: networks_lib.PRNGKey,
      policy_network,
      adder: Optional[adders.Adder] = None,
      variable_source: Optional[core.VariableSource] = None,
  ):
    original_actor = make_actor_fn(random_key, policy_network, adder,
                                   variable_source)
    if adder is None:  # eval actor
      return original_actor

    exploration = marco_polo_exploration.MarcoPoloExploration(
        num_actions=3,
        observation_shape=(1099,),
        exploratory_episode_probability=0.8,
        exploratory_agent_constructor=random_walk_agent.RandomWalkAgent)

    return CombinedActor(original_actor, exploration)

  return make_actor

def create_dqn(params: Dict[str, Any]):
  """Creates necessary components to run Acme's DQN."""
  use_marco_polo_exploration = params.pop('marco_polo_exploration', False)
  update_period = 4
  target_update_period = 100
  adaptive_learning_rate = params.pop('adaptive_learning_rate', False)

  config = dqn.DQNConfig(**params)
  config.discount = 0.993
  config.n_step = 5
  config.min_replay_size = 500
  config.target_update_period = target_update_period // update_period
  config.adam_eps = 0.00002
  config.max_replay_size = 2000000
  config.batch_size = 32
  config.samples_per_insert = config.batch_size / update_period
  config.prefetch_size = 0  # Somehow prefetching makes it much slower.
  if adaptive_learning_rate:
    config.learning_rate = optax.linear_schedule(
        init_value=2e-6, end_value=4e-7,
        transition_steps=5_000_000 // config.batch_size)
  else:
    config.learning_rate = 2e-6

  num_atoms = 51
  def make_networks(env_spec):
    q_network = QuantileNetwork(num_actions=3, num_layers=8,
                                hidden_units=600, num_atoms=num_atoms)

    dummy_obs = utils.tile_nested(utils.zeros_like(env_spec.observations), 1)
    dqn_network = networks_lib.FeedForwardNetwork(
        lambda key: q_network.init(key, dummy_obs), q_network.apply)
    return dqn_network

  def dqn_logger():
    return None

  loss_fn = dqn.QrDqn(num_atoms=num_atoms, huber_param=1)
  rl_agent = dqn.DQNBuilder(
      config=config, loss_fn=loss_fn, logger_fn=dqn_logger)

  def behavior_policy(dqn_network):
    def policy(params: networks_lib.Params, key: jnp.ndarray,
               observation: jnp.ndarray, epsilon: float) -> jnp.ndarray:
      observation = jnp.expand_dims(observation, axis=0)  # add batch dim
      action_values = dqn_network.apply(params, observation)['q_values']
      action_values = jnp.squeeze(action_values, axis=0)  # remove batch dim
      result = rlax.epsilon_greedy(epsilon).sample(key, action_values)
      return result
    return policy

  def eval_policy(dqn_network):
    def policy(params: networks_lib.Params, key: jnp.ndarray,
               observation: jnp.ndarray, _) -> jnp.ndarray:
      observation = jnp.expand_dims(observation, axis=0)  # add batch dim
      action_values = dqn_network.apply(params, observation)['q_values']
      action_values = jnp.squeeze(action_values, axis=0)  # remove batch dim
      result = rlax.epsilon_greedy(0).sample(key, action_values)
      return result
    return policy

  if use_marco_polo_exploration:
    rl_agent.make_actor = marco_polo_actor(rl_agent.make_actor)
  return rl_agent, config, make_networks, behavior_policy, eval_policy

max_stars_repo_name: johannah/balloon-learning-environment

max_issues_repo_name: johannah/balloon-learning-environment

max_forks_repo_name: johannah/balloon-learning-environment

reason: import numpy,import jax


text:
import cv2
import numpy as np
import torch
from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients

class BaseCAM:
    def __init__(self,
                 model,
                 target_layer,
                 use_cuda=False,
                 reshape_transform=None):
        self.model = model.eval()
        self.target_layer = target_layer
        self.cuda = use_cuda
        if self.cuda:
            self.model = model.cuda()
        self.reshape_transform = reshape_transform
        self.activations_and_grads = ActivationsAndGradients(self.model,
            target_layer, reshape_transform)

    def forward(self, input_img):
        return self.model(input_img)

    def get_cam_weights(self,
                        input_tensor,
                        target_category,
                        activations,
                        grads):
        raise Exception("Not Implemented")

    def get_loss(self, output, target_category):
        return output[:, target_category]

    def __call__(self, input_tensor, target_category=None):
        if self.cuda:
            input_tensor = input_tensor.cuda()

        output = self.activations_and_grads(input_tensor)

        if target_category is None:
            target_category = np.argmax(output.cpu().data.numpy())

        self.model.zero_grad()
        loss = self.get_loss(output, target_category)
        loss.backward(retain_graph=True)

        activations = self.activations_and_grads.activations[-1].cpu().data.numpy()[0, :]
        grads = self.activations_and_grads.gradients[-1].cpu().data.numpy()[0, :]

        weights = self.get_cam_weights(input_tensor, target_category, activations, grads)
        cam = np.zeros(activations.shape[1:], dtype=np.float32)

        for i, w in enumerate(weights):
            cam += w * activations[i, :, :]

        cam = np.maximum(cam, 0)
        cam = cv2.resize(cam, input_tensor.shape[2:][::-1])
        cam = cam - np.min(cam)
        cam = cam / np.max(cam)
        return cam

max_stars_repo_name: SWKoreaBME/pytorch-grad-cam

max_issues_repo_name: jueyunhiahia/pytorch-grad-cam

max_forks_repo_name: jueyunhiahia/pytorch-grad-cam


text:
import pickle
import os
import numpy as np
import requests
import json
from uuid import uuid4
import label_studio_tools
import http.client
import json

from label_studio_converter.utils import parse_config
from label_studio_tools.core.utils.io import get_local_path
from requests.auth import HTTPBasicAuth

import mimetypes
import time
import requests
from label_studio_ml.utils import DATA_UNDEFINED_NAME, get_single_tag_keys
from label_studio_ml.model import LabelStudioMLBase

HOSTNAME = os.getenv('LABEL_STUDIO_HOSTNAME')
API_KEY = os.getenv('LABEL_STUDIO_API_KEY')

class TranscriptionModel(LabelStudioMLBase):

    def __init__(self, **kwargs):
        # don't forget to initialize base class...
        super(TranscriptionModel, self).__init__(**kwargs)
        self.hostname = kwargs.get('hostname', '')
        self.access_token = kwargs.get('access_token', '')

    def predict(self, tasks, **kwargs):
        conn = http.client.HTTPSConnection("models.aixplain.com")

        for task in tasks:
            d = task['data'].get('audio') or task['data'].get(DATA_UNDEFINED_NAME)
            payload = json.dumps({
                "data": d
            })
            headers = {
                'x-api-key': '2+Qaq7Lg/v5YWfoNtsQHuni5q/3xuyLKPk8WMrn+QhBfdoxUmbb/c2JHel+vlFIhn465hC4vHm6flJFeDauFLQ==',
                'Content-Type': 'application/json'
            }
            conn.request("POST", "https://models.aixplain.com/api/v1/execute", payload, headers)
            res = conn.getresponse()
            resData = res.read()
            resData = json.loads(resData.decode("utf-8"))
            while True:
         #       print(resData)
                payload = json.dumps({})
                headers = {
                    'x-api-key': '2+Qaq7Lg/v5YWfoNtsQHuni5q/3xuyLKPk8WMrn+QhBfdoxUmbb/c2JHel+vlFIhn465hC4vHm6flJFeDauFLQ==',
                    'Content-Type': 'application/json'
                }
                conn.request("GET", resData['data'], payload, headers)
                pollRes = conn.getresponse()
                pollResData = pollRes.read()
                pollResData = json.loads(pollResData.decode("utf-8"))
                print(pollResData)
                if pollResData['completed'] == False:
                    time.sleep(5)
                else:
                    predictions = []
                    predictions.append({
                        'result': [{
                            'from_name': "transcription",
                            'to_name': "audio",
                            'type': 'textarea',
                            'value': {'text': pollResData['data']}
                        }],
                    })

                    return predictions

def fit(self, completions, workdir=None, **kwargs):
    pass


text:
"""Lookup Utilities and Layer"""

from typing import Dict, Callable
import logging

import numpy as np
import tensorflow as tf

from deepr.layers import base
from deepr.utils.field import TensorType

LOGGER = logging.getLogger(__name__)

class Lookup(base.Layer):
    """Lookup Layer

    Attributes
    ----------
    table_initializer_fn : Callable[[], tf.contrib.lookup.HashTable]
        Function that creates a table
    """

    def __init__(self, table_initializer_fn: Callable[[], tf.contrib.lookup.HashTable], **kwargs):
        super().__init__(n_in=1, n_out=1, **kwargs)
        self.table_initializer_fn = table_initializer_fn

    def forward(self, tensors, mode: str = None):
        """Forward method of the layer"""
        return self.table_initializer_fn().lookup(tensors)

class LookupFromFile(Lookup):
    """Lookup From File Layer

    Creates a table at runtime from a mapping file. The table will map
    each key to its corresponding line index as an tf.int64.

    Attributes
    ----------
    key_dtype : tf.DType
        Keys type
    path : str
        Path to mapping file
    table_name : str
        Name of the HashTable
    """

    def __init__(self, table_name: str, path: str, key_dtype=None, **kwargs):
        super().__init__(lambda: table_from_file(name=table_name, path=path, key_dtype=key_dtype), **kwargs)
        self.table_name = table_name
        self.path = path
        self.key_dtype = key_dtype

class LookupFromMapping(Lookup):
    """Lookup From Mapping Layer

    Attributes
    ----------
    default_value : Any
        Default value for missing keys
    key_dtype : tf.DType
        Keys type
    mapping : Dict[Any, Any]
        Mapping keys -> index
    table_name : str
        Name of the HashTable
    value_dtype : tf.DType
        Values type
    """

    def __init__(self, table_name: str, mapping: Dict, default_value=None, key_dtype=None, value_dtype=None, **kwargs):
        super().__init__(
            lambda: table_from_mapping(
                name=table_name,
                mapping=mapping,
                default_value=default_value,
                key_dtype=key_dtype,
                value_dtype=value_dtype,
            ),
            **kwargs,
        )
        self.table_name = table_name
        self.mapping = mapping
        self.default_value = default_value
        self.key_dtype = key_dtype
        self.value_dtype = value_dtype

def table_from_file(name: str, path: str, key_dtype=None):
    """Create table from file"""
    LOGGER.info(f"Creating table {name} from {path}")
    table = tf.contrib.lookup.index_table_from_file(vocabulary_file=path, name=name, key_dtype=key_dtype)
    return table

def table_from_mapping(name: str, mapping: Dict, default_value=None, key_dtype=None, value_dtype=None):
    """Create table from mapping"""
    LOGGER.info(f"Creating table {name} from mapping.")

    # Convert mapping to arrays of keys and values
    keys, values = zip(*mapping.items())  # type: ignore
    keys_np = np.array(keys)
    values_np = np.array(values)

    # Infer default value if not given
    if default_value is None:
        default_value = TensorType(type(values_np[0].item())).default

    # Infer types if not given
    if key_dtype is None:
        key_dtype = TensorType(type(keys_np[0].item())).tf
    if value_dtype is None:
        value_dtype = TensorType(type(values_np[0].item())).tf

    # Create table
    return tf.contrib.lookup.HashTable(
        tf.contrib.lookup.KeyValueTensorInitializer(
            keys=keys_np, values=values_np, key_dtype=key_dtype, value_dtype=value_dtype
        ),
        name=name,
        default_value=default_value,
    )

max_stars_repo_name: Mbompr/deepr

max_issues_repo_name: Mbompr/deepr

max_forks_repo_name: Mbompr/deepr


text:
import numpy as np
import quaternion

def generate_trajectory_6d_quat(init_p, init_q, y_delta_p, y_delta_q):
    cur_p = np.array(init_p)
    cur_q = quaternion.from_float_array(init_q)
    pred_p = []
    pred_p.append(np.array(cur_p))

    for [delta_p, delta_q] in zip(y_delta_p, y_delta_q):
        cur_p = cur_p + np.matmul(quaternion.as_rotation_matrix(cur_q), delta_p.T).T
        cur_q = cur_q * quaternion.from_float_array(delta_q).normalized()
        pred_p.append(np.array(cur_p))

    return np.reshape(pred_p, (len(pred_p), 3))

def generate_trajectory_3d(init_l, init_theta, init_psi, y_delta_l, y_delta_theta, y_delta_psi):
    cur_l = np.array(init_l)
    cur_theta = np.array(init_theta)
    cur_psi = np.array(init_psi)
    pred_l = []
    pred_l.append(np.array(cur_l))

    for [delta_l, delta_theta, delta_psi] in zip(y_delta_l, y_delta_theta, y_delta_psi):
        cur_theta = cur_theta + delta_theta
        cur_psi = cur_psi + delta_psi
        cur_l[0] = cur_l[0] + delta_l * np.sin(cur_theta) * np.cos(cur_psi)
        cur_l[1] = cur_l[1] + delta_l * np.sin(cur_theta) * np.sin(cur_psi)
        cur_l[2] = cur_l[2] + delta_l * np.cos(cur_theta)
        pred_l.append(np.array(cur_l))

    return np.reshape(pred_l, (len(pred_l), 3))


text:
from scipy import misc
import numpy as np
from PIL import Image
import cv2
import dlib
import glob
import os
import shutil
from io import BytesIO

detector = dlib.get_frontal_face_detector()
from ntpath import basename

def hangulFilePathImageRead(filePath):
    stream = open(filePath.encode("utf-8"), "rb")
    bytes = bytearray(stream.read())
    numpyArray = np.asarray(bytes, dtype=np.uint8)

    return cv2.imdecode(numpyArray, cv2.IMREAD_UNCHANGED)

def rect_to_bb(rect):
    # take a bounding predicted by dlib and convert it
    # to the format (x, y, w, h) as we would normally do
    # with OpenCV
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y

    # return a tuple of (x, y, w, h)
    return x, y, w, h

def get_iou(bb1, bb2):
    x_left = max(bb1[0], bb2[0])
    y_top = max(bb1[2], bb2[2])
    x_right = min(bb1[1], bb2[1])
    y_bottom = min(bb1[3], bb2[3])

    if x_right < x_left or y_bottom < y_top:
        return 0.0

    # The intersection of two axis-aligned bounding boxes is always an
    # axis-aligned bounding box
    intersection_area = (x_right - x_left) * (y_bottom - y_top)

    # compute the area of both AABBs
    bb1_area = (bb1[1] - bb1[0]) * (bb1[3] - bb1[2])
    bb2_area = (bb2[1] - bb2[0]) * (bb2[3] - bb2[2])

    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = intersection_area / float(bb1_area + bb2_area - intersection_area)
    assert iou >= 0.0
    assert iou <= 1.0
    return iou

def crop(filename):
    filename = filename.decode()
    img = hangulFilePathImageRead(filename)
    name = basename(filename)
    height, width, _ = img.shape
    dets, scores, idx = detector.run(img, 1, -1)
    if len(dets)==0:
        return
    faces = list(map(list, zip(dets, scores)))
    list.sort(faces, key=lambda x: x[1], reverse=True)

    det = faces[0][0]
    x, y, w, h = rect_to_bb(det)
    face = np.array([x, y, x + w, y + h])

    center = [(face[0] + face[2]) / 2, (face[1] + face[3]) / 2]
    size = (np.max([face[2] - face[0], face[3] - face[1]]) / 2)

    face[0] = int(center[0] - size * 1.2)
    face[1] = int(center[1] - size * 1.2)
    face[2] = int(center[0] + size * 1.2)
    face[3] = int(center[1] + size * 1.2)
    if face[0] < 0:
        face[0] = 0
    if face[1] < 0:
        face[1] = 0
    if face[2] > width - 1:
        face[2] = width - 1
    if face[3] > height - 1:
        face[3] = height - 1
    scaled = img[face[1]:face[3], face[0]:face[2], :]
    scaled = misc.imresize(scaled, (64, 64), interp='bilinear')
    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 100]
    result, encimg = cv2.imencode('.jpg', scaled, encode_param)
    res = BytesIO(encimg)
    val = res.getvalue()

    return val, name

if __name__ == '__main__':
    i = 93
    imgs = glob.glob('./../../data/sample/*/*.jpg')
    for img in imgs:
        crop(img)
        i += 1

max_stars_repo_name: fakeface-mmc/fakeface-mmc

max_issues_repo_name: fakeface-mmc/fakeface-mmc

max_forks_repo_name: fakeface-mmc/fakeface-mmc


text:
# Common imports
import os

# Where to save the figures and data files
DATA_ID = "Results/EnergyMin"

def data_path(dat_id):
    return os.path.join(DATA_ID, dat_id)

infile = open(data_path("Energies.dat"),'r')

from numpy import std, mean, concatenate, arange, loadtxt, zeros, ceil
from numpy.random import randint
from time import time

def tsboot(data,statistic,R,l):
    t = zeros(R); n = len(data); k = int(ceil(float(n)/l));
    inds = arange(n); t0 = time()

    # time series bootstrap
    for i in range(R):
        # construct bootstrap sample from
        # k chunks of data. The chunksize is l
        _data = concatenate([data[j:j+l] for j in randint(0,n-l,k)])[0:n];
        t[i] = statistic(_data)

    # analysis
    print ("Runtime: %g sec" % (time()-t0)); print ("Bootstrap Statistics :")
    print ("original           bias      std. error")
    print ("%8g %14g %15g" % (statistic(data), \
                             mean(t) - statistic(data), \
                             std(t) ))
    return t

# Demo

# data
T0 = time()
X = loadtxt(infile)

# statistic to be estimated. Takes two args.
# arg1: the data
def stat(data):
    return mean(data)

print ("Data loaded in %g sec" % (time() - T0))
t = tsboot(X, stat, 2**12, 2**10)

reason: from numpy


text:
import numpy as np
n = int(raw_input())
A = np.array([ map(int, raw_input().split()) for i in range(n) ])
B = np.array([ map(int, raw_input().split()) for i in range(n) ])
print np.dot(A, B)

max_stars_repo_path: Numpy/Dot and Cross.py

max_issues_repo_path: Numpy/Dot and Cross.py

max_forks_repo_path: Numpy/Dot and Cross.py


text:
# B36/S23 implementation

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation as anim
import numpy as np
import sys
import math
import random
import time

np.set_printoptions(threshold=np.nan)

length = int(sys.argv[1])
density = float(sys.argv[2])

fig = plt.figure()
ax = plt.axes(xlim=(0, length-1), ylim=(0, length-1))
arr = np.zeros((length, length), dtype = int)
for i in range(length):
	for j in range(length):
			if np.random.random_sample() <= density:
				arr[i][j] = 100

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                                    ['black','lightgreen'],
                                                    256)
bounds=[0,0,10,10]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

im = plt.imshow(arr, interpolation='nearest',
              cmap = cmap,
              origin = 'lower',
			  vmin = 0, vmax = 255,
			  animated= True)

# r pentanimo
"""arr[1][2] = 1
arr[1][3] = 1
arr[2][1] = 1
arr[2][2] = 1
arr[3][2] = 1"""

def sum_neighbors(arr, i, j):

	left = j - 1
	right = j + 1
	below = i + 1
	above = i - 1

	if right >= length:
		right = 0
	if below >= length:
		below = 0
	if left < 0:
		left = length - 1
	if above < 0:
		above = length - 1

	the_sum = arr[below][right] + arr[i][right] + arr[above][right] + arr[below][j] + arr[below][left] + arr[i][left] + arr[above][j] + arr[above][left]
	return the_sum

# Conway rules

def update(arr):
	status_arr = np.zeros((length, length), dtype = int)

	for i in range(length):
		for j in range(length):
			# underpopulation
			if sum_neighbors(arr,i,j) < 200:
				status_arr[i][j] = 0
			# overpopulation
			if sum_neighbors(arr,i,j) > 300:
				status_arr[i][j] = 0
			# cohabitation
			if sum_neighbors(arr,i,j) == 200 and arr[i][j] == 100:
				status_arr[i][j] = 100
			# reproduction
			if sum_neighbors(arr,i,j) == 300:
				status_arr[i][j] = 100
			# high life
			if sum_neighbors(arr,i,j) == 600 and arr[i][j] == 0:
				status_arr[i][j] = 100

	# update arr according to status
	for i in range(length):
		for j in range(length):
			arr[i][j] = status_arr[i][j]

	return arr

def animate(i):
    arr=im.get_array()
    arr = update(arr)
    im.set_array(arr)
    return [im]

anim = anim.FuncAnimation(fig, animate, frames=10, interval=1, blit=True)
plt.show()

max_stars_repo_name: johncmerfeld/GooseLife

max_issues_repo_name: johncmerfeld/GooseLife

max_forks_repo_name: johncmerfeld/GooseLife


text:
"""
regress out both RIN and top 3 PCs
"""

import numpy
import sklearn.decomposition
import os

basedir=os.environ['MYCONNECTOME_DIR']
rnaseqdir=os.path.join(basedir,'rna-seq')

def regress_rin_pca():
    rin=numpy.loadtxt(os.path.join(rnaseqdir,'rin.txt'))

    f=open(os.path.join(rnaseqdir,'varstab_data_prefiltered.txt'))
    subs=f.readline()
    gene_names=[]
    data=[]
    for l in f.readlines():
        l_s=l.strip().split()
        gene_names.append(l_s[0])
        data.append(l_s[1:])
    f.close()

    varstab=numpy.zeros((len(gene_names),48))

    varstab_rinregressed=numpy.zeros(varstab.shape)

    for i in range(len(gene_names)):
        varstab[i,:]=[float(x) for x in data[i]]
    pca=sklearn.decomposition.PCA(n_components=3)
    pca.fit(varstab.T)

    rin=numpy.array(rin,ndmin=2).T
    X=numpy.hstack((rin - numpy.mean(rin),pca.transform(varstab.T),numpy.ones((48,1))))

    for i in range(len(gene_names)):
        y=varstab[i,:].reshape((48,1))
        #print numpy.corrcoef(y.T,rin.T)[0,1]
        result=numpy.linalg.lstsq(X,y)
        resid=y - X.dot(result[0])
        varstab_rinregressed[i,:]=resid[:,0]

    f=open(os.path.join(rnaseqdir,'varstab_data_prefiltered_rin_3PC_regressed.txt'),'w')
    f.write(subs)
    for i in range(len(gene_names)):
        f.write(gene_names[i])
        for j in range(48):
            f.write(' %f'%varstab_rinregressed[i,j])
        f.write('\n')
    f.close()

if __name__ == "__main__":
    regress_rin_pca()

max_stars_repo_name: poldrack/myconnectome

max_issues_repo_name: poldrack/myconnectome

max_forks_repo_name: poldrack/myconnectome