text:
# TODO: Implement session state to control test and next button
# TODO: Show results in the plot
# TODO: Conditional coloring in the plot

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

from . import equity

rcParams["font.family"] = "monospace"

SUITS_COLORS = {"s": "k", "h": "r", "c": "g", "d": "b"}

def ellipse(theta):
    """ Ellipse shape."""
    a = 2
    b = 1
    return a * b / np.sqrt((a * np.sin(theta) ** 2) + (b * np.cos(theta) ** 2))

def hand(
    n_seats,
    pot,
    hero_name,
    hero_hand,
    hero_chips,
    villains_names,
    villains_ranges,
    villains_chips,
):
    """ Hand viewer. """

    # Table line
    theta_arr = np.linspace(start=0, stop=2 * np.pi, num=100, endpoint=True)
    radius_arr = [ellipse(t) for t in theta_arr]

    # Seats coordinates.
    seats_theta = -1 * np.linspace(start=0, stop=2 * np.pi, num=n_seats, endpoint=False)
    seats_radius = [ellipse(t) for t in seats_theta]

    # Place players into the seats. (+ 1 because of the hero).
    players_theta = [seats_theta[i] for i in range(len(villains_names) + 1)]
    players_radius = [seats_radius[i] for i in range(len(villains_names) + 1)]

    # Create figure and polar axes.
    fig = plt.figure(figsize=(8, 4))
    ax = fig.add_axes([0.1, 0, 0.8, 1], polar=True)

    # Create table.
    ax.plot(theta_arr, radius_arr, color="gainsboro", linewidth=5)
    ax.fill_between(theta_arr, radius_arr, color="whitesmoke")
    ax.axis("off")

    # Get hero hand percentage.
    hero_descr = equity.hand_to_descr(hero_hand)
    hero_percent = equity.descr_to_percentage(hero_descr)

    # Concatenate hero and villains.
    players_name = np.concatenate([[hero_name], villains_names])
    players_range = np.concatenate([[hero_percent], villains_ranges])
    players_chips = np.concatenate([[hero_chips], villains_chips])

    iterator = zip(
        players_name, players_range, players_chips, players_theta, players_radius,
    )

    # Players
    for name, rng, chips, theta, radius in iterator:

        bbox_props = dict(boxstyle="Round", fc="whitesmoke", ec="gray")
        ax.annotate(
            f"{name} {rng:.0f}%\n{chips} BB",
            xy=(theta, radius),
            ha="center",
            va="center",
            bbox=bbox_props,
        )

    # How much the small blind will be moved to avoid overlapping.
    move_small_blind = 0

    # Dealer index.
    if n_seats == 2:
        dealer_idx = -2
        move_small_blind = -0.2  # Displace small blind to not overlap the button.
    elif len(players_name) == 2:
        dealer_idx = None
    else:
        dealer_idx = -3

    if dealer_idx is not None:
        bbox_props = dict(boxstyle="Circle", fc="pink", ec="k")
        ax.annotate(
            "D",
            xy=(players_theta[dealer_idx], 0.7 * players_radius[dealer_idx]),
            ha="center",
            va="center",
            bbox=bbox_props,
        )

    # Blinds and antes
    ax.annotate(f"Antes\n{pot - 1.5} BB", xy=(0, 0), ha="center", va="center")
    ax.annotate(
        "1 BB",
        xy=(players_theta[-1], 0.7 * players_radius[-1]),
        ha="center",
        va="center",
    )
    ax.annotate(
        ".5 BB",
        xy=(players_theta[-2], (0.7 + move_small_blind) * players_radius[-2]),
        ha="center",
        va="center",
    )

    # Hero cards.
    card0 = hero_hand[:2]
    card1 = hero_hand[2:]

    rank0 = card0[0]
    rank1 = card1[0]

    suit0 = card0[1]
    suit1 = card1[1]

    bbox_props = dict(boxstyle="Round", fc="w", ec="gray")
    hero_cards_kwargs = dict(
        va="center",
        bbox=bbox_props,
        fontsize="x-large",
        fontweight="bold",
        annotation_clip=False,
    )
    ax.annotate(
        rank0,
        xy=(players_theta[0], 1.35 * players_radius[0]),
        ha="right",
        color=SUITS_COLORS[suit0],
        **hero_cards_kwargs,
    )
    ax.annotate(
        rank1,
        xy=(players_theta[0], 1.41 * players_radius[0]),
        ha="left",
        color=SUITS_COLORS[suit1],
        **hero_cards_kwargs,
    )

    return fig

ext: py

max_stars_repo_name: streamlit-badge-bot/poker-coach

max_issues_repo_name: streamlit-badge-bot/poker-coach

max_forks_repo_name: streamlit-badge-bot/poker-coach

reason: import numpy


text:
import numpy as np
import argparse
import json

parser = argparse.ArgumentParser(description='Parse a spectra')
parser.add_argument('file', type=str, help="A temp file containing a JSON-encoded spectral string")
parser.add_argument('--detector_min', type=float, default=650)
parser.add_argument('--detector_max', type=float, default=3600)
parser.add_argument('--compression', type=int, default=10)
args = parser.parse_args()

file = open(args.file, 'r')
spectra = json.loads(file.read())
spectra = [float(numeric_string) for numeric_string in spectra]

x_series = spectra[0::2] #take evens starting from 0
y_series = spectra[1::2] #take odds starting from 0

x_series = x_series[::args.compression] #compress by taking steps of (10)
y_series = y_series[::args.compression] #compress by taking steps of (10)

paired_data = [];

for x in range(len(x_series)):
    if(y_series[x] > 0): #skip negative numbers
        paired_data.append([round(x_series[x],2), round(y_series[x])])

print(json.dumps(paired_data))

max_stars_repo_name: ostannick/maldb

max_issues_repo_name: ostannick/maldb

max_forks_repo_name: ostannick/maldb


text:
from logging import error
import streamlit as st
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import altair as alt

data_type = ['Test Data', 'Train Data']

xgboost_list = [0.7867491776691122, 0.804894044676069]
randomforest_list = [0.6329507787480243, 0.6406985506761919]
Linear_regression_list = [0.2929796878382063, 0.2948116798857159]
Lasso_list = [-8.30655766355311e-06, 3.407978172024928e-12]
knn_list = [0.7463683061096252, 0.8164847956978979]

def r2_scorepj():
    st.sidebar.subheader("Choose Model ?")
    select_model1 = st.sidebar.checkbox('Xgboost')
    select_model2 = st.sidebar.checkbox('Random Forest')
    select_model3 = st.sidebar.checkbox('Linear Regression')
    select_model4 = st.sidebar.checkbox('Lasso')
    # select_model5 = st.sidebar.checkbox(
    #     'Vector Auto Regression Moving Average')
    select_model6 = st.sidebar.checkbox('K Nearest Neighbour')
    # select_model7 = st.sidebar.checkbox('Support Vector Machine')
    # select_model8 = st.sidebar.checkbox('LSTM')

    models = list()
    arr = list()

    if select_model1:
        models.append(["Xgboost", xgboost_list[0], "Test Data"])
        models.append(["Xgboost", xgboost_list[1], "Train Data"])

    if select_model2:
        models.append(["Random Forest", randomforest_list[0], "Test Data"])
        models.append(["Random Forest", randomforest_list[1], "Train Data"])

    if select_model3:
        models.append(["Linear",
                      Linear_regression_list[0], "Test Data"])
        models.append(["Linear",
                      Linear_regression_list[1], "Train Data"])

    if select_model4:
        models.append(["Lasso", Lasso_list[0], "Test Data"])
        models.append(["Lasso", Lasso_list[1], "Train Data"])

    if select_model6:
        models.append(["KNN", knn_list[0], "Test Data"])
        models.append(["KNN", knn_list[1], "Train Data"])

    errors = ['R2 Score on Test', 'R2 Score on Train']
    if len(models) > 0:
        st.header("Plot for R2 Score different Models")
        x = list(range(0, len(models)))
        chart_data = pd.DataFrame(
            models, x, columns=['Models', 'R2 Score', 'Data Type'])
        chart_data
        st.table(chart_data)
        chart = alt.Chart(chart_data).mark_bar().encode(
            x='Data Type:O',
            y='R2 Score:Q',
            color='Data Type:N',
            column='Models:N'
        )
        st.altair_chart(chart)
    else:
        title = st.title("Welcome to Air Quality Result Analysis Web App")
        st.markdown("Comparative analysis on dataset of different machine learning models  and find out the best model having more accurate predictions of air quality .")
        st.markdown("The dataset is a collection of sensor data for 7 different locations of Malaysia which is collected through OPC sensors. These data are for 15 months that are from March 2019 to May 2020. These data are in CSV or Excel files. These data are saved in month wise format. These data have 57 columns and 35847 rows. These data are recorded for every hour.")
        st.markdown("For the sensor data, out of these 57 features(i.e. number of columns) we are taking 5 features as our input target and 7 as our output target. The input targets are Node value, date, time, external temperature and external RH. The output targets are NO2 ppb, O3 ppb, NO ppb, CO ppb, PM1, PM2.5 and PM10.We are processing the data before sending it to the network. ")


text:
#General Imports
import sys
import numpy as np
import pandas as pd
import argparse
import copy
import random
import json
import pickle

#PyTorch
import torch
from torch.autograd import grad
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
import torch.utils.data as data_utils

#Tensorflow
from absl import flags
import tensorflow as tf
from tensorflow.keras import layers

#Avertorch
from advertorch.utils import predict_from_logits
from advertorch_examples.utils import _imshow
from advertorch.attacks import LinfPGDAttack

from .base_eval import BaseEval

class AdvAttack(BaseEval):

    def __init__(self, args, train_dataset, test_dataset, train_domains, total_domains, domain_size, training_list_size, base_res_dir, run, cuda):

        super().__init__(args, train_dataset, test_dataset, train_domains, total_domains, domain_size, training_list_size, base_res_dir, run, cuda)

    def get_metric_eval(self):

        utr_score=[]
        tr_score=[]
        for i in range(1):

            ##TODO: Customise input parameters to methods like LinfPGDAttack
            adversary = LinfPGDAttack(
                self.phi, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=self.args.adv_eps, nb_iter=70, eps_iter=0.01, rand_init=True, clip_min=(0.0-0.1307)/0.3081, clip_max=(1.0-0.1307)/0.3081,
                targeted=False)

            pred_cln=[]
            pred_untargeted_adv=[]
            pred_targeted_adv=[]
            temp_counter=0
            for batch_idx, (x_e, y_e ,d_e, idx_e) in enumerate(self.test_dataset):
                x_e= x_e.to(self.cuda)
                print(torch.min(x_e), torch.max(x_e))
                y_e= torch.argmax(y_e, dim=1).to(self.cuda)

                adversary.targeted = False
                adv_untargeted = adversary.perturb(x_e, y_e)

                target = torch.ones_like(y_e)*3
                adversary.targeted = True
                adv_targeted = adversary.perturb(x_e, target)
                print(torch.min(adv_untargeted), torch.max(adv_untargeted))
                pred_cln.append( predict_from_logits(self.phi(x_e)) )
                pred_untargeted_adv.append( predict_from_logits(self.phi(adv_untargeted)) )
                pred_targeted_adv.append( predict_from_logits(self.phi(adv_targeted)) )

#                 temp_counter+=1
#                 if temp_counter ==3:
#                     break

            pred_cln= torch.cat(pred_cln)
            pred_untargeted_adv= torch.cat(pred_untargeted_adv)
            pred_targeted_adv= torch.cat(pred_targeted_adv)
            utr_score.append( torch.sum( pred_cln != pred_untargeted_adv).detach().cpu().numpy() / pred_cln.shape[0] )
            tr_score.append( torch.sum(pred_cln!= pred_targeted_adv).detach().cpu().numpy() / pred_cln.shape[0] )

#             batch_size=5
#             plt.figure(figsize=(10, 8))
#             for ii in range(batch_size):
#                 plt.subplot(3, batch_size, ii + 1)
#                 _imshow(x_e[ii])
#                 plt.title("clean \n pred: {}".format(pred_cln[ii]))
#                 plt.subplot(3, batch_size, ii + 1 + batch_size)
#                 _imshow(adv_untargeted[ii])
#                 plt.title("untargeted \n adv \n pred: {}".format(
#                     pred_untargeted_adv[ii]))
#                 plt.subplot(3, batch_size, ii + 1 + batch_size * 2)
#                 _imshow(adv_targeted[ii])
#                 plt.title("targeted to 3 \n adv \n pred: {}".format(
#                     pred_targeted_adv[ii]))

#             plt.tight_layout()
#             plt.savefig( self.save_path + '.png' )

        utr_score= np.array(utr_score)
        tr_score= np.array(tr_score)
        print('MisClassifcation on Untargetted Attack ', np.mean(utr_score), np.std(utr_score), self.args.adv_eps  )
        print('MisClassifcation on Targeted Atttack', np.mean(tr_score), np.std(tr_score), self.args.adv_eps )

        self.metric_score['Untargetted Method']= np.mean( utr_score )
        self.metric_score['Targetted Method']= np.mean( tr_score )

        return


text:
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
import numpy as np
import itertools

from adapt.refine import refine_scalar_field, smallest_length, average_length, refine_1D

NUM_X 	= 250
NUM_Y 	= 250
NUM_COARSE_X = 5
NUM_COARSE_Y = 5

NOISE_LEVEL = 0.1

ITERATION  = 100
MAX_POINTS = 50	# Maximum number of points to take

def ideal_tc(x, xc=9.0, k=20.0):
	return x*1.0/(1.0 + np.exp(-k*(x-xc)))

xs = np.linspace(0, 20, NUM_X)

coarse_xs = np.linspace(xs[0], xs[-1], NUM_COARSE_X)
values_orig = ideal_tc(coarse_xs) + NOISE_LEVEL*np.random.random(NUM_COARSE_X)

# Evaluate values at original mesh points
points = coarse_xs
values = values_orig

# Find new points and update values
for i in range(ITERATION):
	new_points = refine_1D(points, values, all_points=False,
								criterion="difference", threshold = "one_sigma")
	if new_points is None:
		print("No more points can be added.")
		break

	# Update points and values
	points     = np.append(points, new_points, axis=0)
	new_values = ideal_tc(new_points)
	new_values += NOISE_LEVEL*np.random.random(new_values.shape)
	values     = np.append(values, new_values, axis=0)

	if len(points) > MAX_POINTS:
		print("Reach maximum number of points! Stop.")
		break

plt.title("Ideal vs. Refined")
plt.plot(xs, ideal_tc(xs), 'k-', lw=1.0, label='Ideal Function')
plt.plot(points, values, 'bo', label='Final points')
plt.plot(coarse_xs, values_orig, 'ro', label="Initial Coarse Points")
plt.legend()
plt.show()

max_stars_repo_name: BBN-Q/Adapt

max_issues_repo_name: BBN-Q/Adapt

max_forks_repo_name: BBN-Q/Adapt

reason: import numpy,from scipy


text:
#!/usr/bin/env python
# coding: utf-8

# # The flu model
#
# <left><img src="util/flu_image.png" width="400px">
#
# The flu model is a simple mathematical description of the spread of a flu in a small town. The model divides the population of N individuals into four "compartments" which may vary as a function of time, t:
#
# - **Vulnerable**: people vulnerable but not yet infected with the flu;
#
# - **Sick**: individuals who are infected with the flu;
#
# - **Immune**: individuals have immunity to the flu. This includes individuals who either have recovered from the flu or have been vaccinated.
#
# ### Model parameters
#
# The model describes the change in the population of each of these compartments in terms of five parameters:
#
# - *Initial number of immune individuals*: people who are immune at the start of the infection because they have been previously vaccinated
# - *Contact rate per day*: number of times that an infected individual comes into contact with vulnerable individuals in a day
# - *Contagion ratio*: proportion of contacts that get infected
# - *Recovery time*: the number of days during which a sick individual can pass it on
# - *Vaccination rate*: number of inviduals who are vaccinated per day# Workflow to perform Global Sensitivity Analysis with the PAWN method
#
# #### Import libraries

# In[1]:

import numpy as np
import plotly.graph_objs as go
from ipywidgets import widgets

import util.flu.flu_model as flu_model

def flu_interactive():
    # #### Simulation time range

    # In[2]:

    T = 100 # days
    time_range = np.arange(1,T+1)
    pop = 100

    # ## Interactive figure
    # ### Function to update the simulation when changing the parameters with the sliders

    # In[3]:

    def update_sim(RI_0, contact, contagion, recovery, vaccination):
        param = np.array([RI_0.value/1000, contact.value, contagion.value, recovery.value, vaccination.value/1000])
        S, RI, V,S_max,cost = flu_model.model(param,time_range, pop)
        return S, RI, V, S_max, cost

    # ### Function to update the figure when changing the parameters with the sliders

    # In[4]:

    def update_figure(change):
        with fig_flu.batch_animate(duration=0):
            fig_flu.data[0].y = update_sim(RI_0, contact, contagion, recovery, vaccination)[0]*1000
            fig_flu.data[1].y = update_sim(RI_0, contact, contagion, recovery, vaccination)[1]*1000
            fig_flu.data[2].y = update_sim(RI_0, contact, contagion, recovery, vaccination)[2]*1000
            fig_flu.layout.title = 'Peak = '+f"{update_sim(RI_0, contact, contagion, recovery, vaccination)[3]*1000:,.0f}"+        ' sick individuals - Cost = '+f"{update_sim(RI_0, contact, contagion, recovery, vaccination)[4]*1000:,.0f}"+' £'

    # ### Definition of the sliders
    # #### Ratio of population who is initially immune

    # In[5]:

    # Initial number of immune individuals
    RI_0 = widgets.IntSlider(min = 0,
                            max = 50000,
                            value = 0, step = 1,
                            description = 'Initial number of vaccinated individuals: ',
                            continuous_update=True,
                            style = {'description_width': '300px'} ,
                            layout={'width': '700px'})
    RI_0.observe(update_figure,names = 'value')

    # #### Contact rate per day

    # In[6]:

    contact = widgets.FloatSlider(min = 0.3,
                               max = 2,
                               value = 2, step = 0.01,
                               description = 'Contact rate per day: ',
                               continuous_update=True,
                               style = {'description_width': '300px'} ,
                               layout={'width': '700px'})
    contact.observe(update_figure,names = 'value')

    # #### Contagion ratio

    # In[7]:

    contagion = widgets.FloatSlider(min = 0.3,
                               max = 1,
                               value = 1, step = 0.01,
                               description = 'Contagion ratio: ',
                               continuous_update=True,
                               style = {'description_width': '300px'} ,
                               layout={'width': '700px'})
    contagion.observe(update_figure,names = 'value')

    # #### Recovery time in days

    # In[8]:

    recovery = widgets.IntSlider(min = 7,
                               max = 21,
                               value = 21, step = 1,
                               description = 'Recovery time (days): ',
                               continuous_update=True,
                               style = {'description_width': '300px'} ,
                               layout={'width': '700px'})
    recovery.observe(update_figure,names = 'value')

    # #### Vaccination rate per day

    # In[9]:

    vaccination = widgets.IntSlider(min = 0,
                               max = 10000,
                               value = 0, step = 1,
                               description = 'Vaccination rate per day: ',
                               continuous_update=True,
                               style = {'description_width': '300px'} ,
                               layout={'width': '700px'})
    vaccination.observe(update_figure,names = 'value')

    # ### Plot the interactive figure
    # #### Initial simulation

    # In[10]:

    param = np.array([RI_0.value, contact.value, contagion.value, recovery.value, vaccination.value])
    S, RI, V, S_max, cost = flu_model.model(param,time_range, pop)

    # #### Plot the data on four separate curves for V(t), S(t), RI(t) and D(t)

    # In[11]:

    NHS_capacity = 40*1000
    S_trace  = go.Scatter(x=time_range, y=S*1000,  name='Sick',  mode='lines',line=dict(width=0, color = 'red'),stackgroup='one')
    RI_trace = go.Scatter(x=time_range, y=RI*1000, name='Immune', mode='lines',line=dict(width=0, color = 'green'),stackgroup='one')
    V_trace  = go.Scatter(x=time_range, y=V*1000,  name='Vulnerable', mode='lines',line=dict(width=0, color = 'blue'),stackgroup='one')
    NHS_trace = go.Scatter(x=[0,T+1],y=[NHS_capacity,NHS_capacity], mode = 'lines',name='40% of population',line=dict(color="darkblue",width=1, dash='dash'))
    fig_flu  = go.FigureWidget(data   = [S_trace,RI_trace,V_trace,NHS_trace],
                              layout = go.Layout(xaxis = dict(range = [1,100],title = 'days',rangeslider=dict(visible=False,range=[1,T])),
                                                 yaxis = dict(range = [0,pop*1000],title = 'population'),
                                                 height = 400, title = 'Peak = '+f"{S_max*1000:,.0f}"+' sick individuals - Cost = '+f"{cost*1000:,.0f}"+' £'))

    # #### Plot

    # In[12]:

#    widgets.VBox([widgets.VBox([RI_0, contact, contagion, recovery, vaccination]),
#                  fig_flu])

    return RI_0,contact, contagion,recovery,vaccination,fig_flu

max_stars_repo_name: AndresPenuela/SAFE-Notebooks

max_issues_repo_name: AndresPenuela/SAFE-Notebooks

max_forks_repo_name: AndresPenuela/SAFE-Notebooks


text:
# Data : https://archive.ics.uci.edu/ml/datasets/Online+News+Popularity
import json

import lightgbm as lgb
import numpy as np
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split

if __name__ == "__main__":
    n = 10
    cols = [
        "url",
        "timedelta",
        "n_tokens_title",
        "n_tokens_content",
        "n_unique_tokens",
        "n_non_stop_words",
        "n_non_stop_unique_tokens",
        "num_hrefs",
        "num_self_hrefs",
        "num_imgs",
        "num_videos",
        "average_token_length",
        "num_keywords",
        "data_channel_is_lifestyle",
        "data_channel_is_entertainment",
        "data_channel_is_bus",
        "data_channel_is_socmed",
        "data_channel_is_tech",
        "data_channel_is_world",
        "kw_min_min",
        "kw_max_min",
        "kw_avg_min",
        "kw_min_max",
        "kw_max_max",
        "kw_avg_max",
        "kw_min_avg",
        "kw_max_avg",
        "kw_avg_avg",
        "self_reference_min_shares",
        "self_reference_max_shares",
        "self_reference_avg_sharess",
        "weekday_is_monday",
        "weekday_is_tuesday",
        "weekday_is_wednesday",
        "weekday_is_thursday",
        "weekday_is_friday",
        "weekday_is_saturday",
        "weekday_is_sunday",
        "is_weekend",
        "LDA_00",
        "LDA_01",
        "LDA_02",
        "LDA_03",
        "LDA_04",
        "global_subjectivity",
        "global_sentiment_polarity",
        "global_rate_positive_words",
        "global_rate_negative_words",
        "rate_positive_words",
        "rate_negative_words",
        "avg_positive_polarity",
        "min_positive_polarity",
        "max_positive_polarity",
        "avg_negative_polarity",
        "min_negative_polarity",
        "max_negative_polarity",
        "title_subjectivity",
        "title_sentiment_polarity",
        "abs_title_subjectivity",
        "abs_title_sentiment_polarity",
        "shares",
    ]

    data = pd.read_csv("../data/news/OnlineNewsPopularity.csv", names=cols, skiprows=1)
    data["shares"] = np.log(data["shares"] + 1)

    train, test = train_test_split(data, test_size=0.2, random_state=1337)

    targets = ["shares"]
    num_cols = [
        "n_tokens_title",
        "n_tokens_content",
        "n_unique_tokens",
        "n_non_stop_words",
        "n_non_stop_unique_tokens",
        "num_hrefs",
        "num_self_hrefs",
        "num_imgs",
        "num_videos",
        "average_token_length",
        "num_keywords",
        "data_channel_is_lifestyle",
        "data_channel_is_entertainment",
        "data_channel_is_bus",
        "data_channel_is_socmed",
        "data_channel_is_tech",
        "data_channel_is_world",
        "kw_min_min",
        "kw_max_min",
        "kw_avg_min",
        "kw_min_max",
        "kw_max_max",
        "kw_avg_max",
        "kw_min_avg",
        "kw_max_avg",
        "kw_avg_avg",
        "self_reference_min_shares",
        "self_reference_max_shares",
        "self_reference_avg_sharess",
        "weekday_is_monday",
        "weekday_is_tuesday",
        "weekday_is_wednesday",
        "weekday_is_thursday",
        "weekday_is_friday",
        "weekday_is_saturday",
        "weekday_is_sunday",
        "is_weekend",
        "LDA_00",
        "LDA_01",
        "LDA_02",
        "LDA_03",
        "LDA_04",
        "global_subjectivity",
        "global_sentiment_polarity",
        "global_rate_positive_words",
        "global_rate_negative_words",
        "rate_positive_words",
        "rate_negative_words",
        "avg_positive_polarity",
        "min_positive_polarity",
        "max_positive_polarity",
        "avg_negative_polarity",
        "min_negative_polarity",
        "max_negative_polarity",
        "title_subjectivity",
        "title_sentiment_polarity",
        "abs_title_subjectivity",
        "abs_title_sentiment_polarity",
    ]
    cat_cols = []

    for k in num_cols:
        mean = train[k].mean()
        std = train[k].std()
        train[k] = (train[k] - mean) / std
        test[k] = (test[k] - mean) / std

    train = train.sample(frac=1)

    for feature in cat_cols:
        train[feature] = pd.Series(train[feature], dtype="category")
        test[feature] = pd.Series(test[feature], dtype="category")

    sizes = [200, 500, 1000, 2000, 5000]

    scratch_mae = []

    lgbm_params = {
        "objective": "regression_l1",
        "metric": "l1",
        # 'max_depth': 5,
        "num_leaves": 300,
        "feature_fraction": 0.5,
        "bagging_fraction": 0.75,
        "bagging_freq": 2,
        "learning_rate": 0.0001,
        "verbose": 0,
    }

    for size in sizes:

        mae = 0

        for _ in range(n):

            tr, val = train_test_split(train[:size], test_size=0.1)

            pred = np.zeros((test.shape[0], len(targets)))

            for i, target in enumerate(targets):
                train_data = lgb.Dataset(
                    tr[num_cols + cat_cols],
                    label=tr[target],
                    feature_name=num_cols + cat_cols,
                    categorical_feature=cat_cols,
                )
                validation_data = lgb.Dataset(
                    val[num_cols + cat_cols],
                    label=val[target],
                    feature_name=num_cols + cat_cols,
                    categorical_feature=cat_cols,
                )

                num_round = 1000

                bst = lgb.train(
                    lgbm_params,
                    train_data,
                    num_round,
                    valid_sets=[validation_data],
                    early_stopping_rounds=20,
                    verbose_eval=10,
                )

                pred[:, i] = bst.predict(test[num_cols + cat_cols])

            pred = pred.ravel()
            y_true = np.array(test[targets].values).ravel()

            mae += mean_absolute_error(y_true, pred) / n

        scratch_mae.append(mae)

    print("sizes", sizes)
    print("scratch_mae", scratch_mae)

    with open("news_lgbm_performance.json", "w") as f:
        json.dump(
            {"sizes": sizes, "scratch_mae": scratch_mae}, f, indent=4,
        )

max_stars_repo_name: cloudarchgopal/pythonproject

max_issues_repo_name: cloudarchgopal/pythonproject

max_forks_repo_name: cloudarchgopal/pythonproject


text:
from cluster.neuralnet.neuralnet_node import NeuralNetNode
from master.workflow.netconf.workflow_netconf_seq2seq import WorkFlowNetConfSeq2Seq as WfNetconfSeq2Seq
from cluster.service.service_predict_w2v import PredictNetW2V
import numpy as np
import tensorflow as tf
from common.utils import *
from konlpy.tag import Mecab
from common.graph.nn_graph_manager import NeuralNetModel
import tensorflow as tf
import tensorflow.contrib.seq2seq as seq2seq
from tensorflow.contrib.rnn import LSTMCell, LSTMStateTuple, GRUCell, MultiRNNCell
from tensorflow.contrib.rnn.python.ops.rnn_cell import _linear
from tensorflow.python.ops.rnn_cell_impl import _zero_state_tensors
from tensorflow.python.layers.core import Dense

class NeuralNetNodeAttnSeq2Seq(NeuralNetNode):
    """

    """

    def run(self, conf_data):
        try :
            # init parms for word2vec node
            node_id = conf_data['node_id']
            self._init_node_parm(node_id)

            # get prev node for load data
            train_data_set = self.get_linked_prev_node_with_grp('preprocess')[0]

            # prepare net conf
            tf.reset_default_graph()
            self._set_train_model()

            # create session
            with tf.Session() as sess:
                sess.run(tf.initialize_all_variables())
                saver = tf.train.Saver(tf.all_variables())
                if (self.check_batch_exist(conf_data['node_id'])):
                    path = ''.join([self.md_store_path, '/', self.get_eval_batch(node_id), '/'])
                    set_filepaths(path)
                    saver.restore(sess, path)

                for self.epoch in range(self.num_epochs):
                    # run train
                    while(train_data_set.has_next()) :
                        for i in range(0, train_data_set.data_size(), self.batch_size):
                            data_set = train_data_set[i:i + self.batch_size]
                            if(len(data_set[0]) != self.batch_size) : break
                            targets = self._get_dict_id(data_set[1])
                            decode_batch = self._word_embed_data(data_set[1])
                            encode_batch = self._word_embed_data(data_set[0])
                            self._run_train(sess, encode_batch, decode_batch, targets)
                        train_data_set.next()
                    train_data_set.reset_pointer()

                # save model and close session
                path = ''.join([self.md_store_path, '/', self.make_batch(node_id)[1], '/'])
                set_filepaths(path)
                saver.save(sess, path)
        except Exception as e :
            raise Exception (e)
        finally :
            if (self.word_embed_type == 'onehot'):
                self.wf_conf.set_vocab_list(self.onehot_encoder.dics())

    def _init_node_parm(self, node_id):
        """
        init necessary parameters
        :param node_id:
        :return:
        """
        try :
            try :
                self.wf_conf = WfNetconfSeq2Seq(node_id)
                wf_conf = self.wf_conf
                self.bidirectional = wf_conf.bidirectional
                self.attention = wf_conf.attention
                self.input_vocab_size = wf_conf.input_vocab_size
                self.target_vocab_size = wf_conf.target_vocab_size
                self.enc_hidden_size = wf_conf.enc_hidden_size
                self.enc_num_layers = wf_conf.enc_num_layers
                self.dec_hidden_size = wf_conf.dec_hidden_size
                self.dec_num_layers = wf_conf.dec_num_layers
                self.batch_size = wf_conf.batch_size
                self.learning_rate = tf.Variable(float(wf_conf.learning_rate), trainable=False)
                self.learning_rate_decay_op = self.learning_rate.assign(
                    self.learning_rate * wf_conf.learning_rate_decay_factor)
                self.global_step = tf.Variable(0, trainable=False)
                self.max_gradient_norm = wf_conf.max_gradient_norm
                self.buckets = wf_conf.buckets
            except Exception as e :
                raise Exception ("seq2seq netconf parms not set")

        except Exception as e :
            raise Exception (e)

    def _set_progress_state(self):
        return None

    def predict(self, node_id, parm = {"input_data" : {}, "num" : 0, "clean_ans": True}):
        """

        :param node_id:
        :param parm:
        :return:
        """
        try :
            # get unique key
            unique_key = '_'.join([node_id, self.get_eval_batch(node_id)])

            # set init params
            self.node_id = node_id
            self._init_node_parm(self.node_id)

            # prepare net conf
            tf.reset_default_graph()

            ## create tensorflow graph
            if (NeuralNetModel.dict.get(unique_key)):
                self = NeuralNetModel.dict.get(unique_key)
                graph = NeuralNetModel.graph.get(unique_key)
            else:
                self._set_predict_model()
                NeuralNetModel.dict[unique_key] = self
                NeuralNetModel.graph[unique_key] = tf.get_default_graph()
                graph = tf.get_default_graph()

            with tf.Session(graph=graph) as sess :
                sess.run(self.init_val)
                result = self._run_predict(sess, parm['input_data'],
                                           predict_num=parm.get("num") if parm.get("num") != None else 0,
                                           clean_ans = parm.get("clean_ans") if parm.get("clean_ans") != None else True,
                                           batch_ver='eval', # TODO : need to be predict version
                                           saver=self.saver)
            return result
        except Exception as e :
            raise Exception ("seq2seq predict error : {0}".format(e))
        finally:
            sess.close()

    def eval(self, node_id, conf, data=None, result=None):
        """
        eval result wit test data
        :param node_id:
        :param parm:
        :return:
        """
        try :
            result.set_result_data_format({})
            tf.reset_default_graph()
            sess = tf.Session()
            sess.run(tf.initialize_all_variables())
            # prepare net conf
            self._set_predict_model()
            self.node_id = node_id

            while (data.has_next()):
                for i in range(0, data.data_size(), self.predict_batch):
                    data_set = data[i:i + self.predict_batch]
                    if (len(data_set[0]) != self.predict_batch): break
                    predict = self._run_predict(sess, data_set[0][0], type='pre', clean_ans=False)
                    result.set_result_info(' '.join(data_set[1][0]), ' '.join(predict[0]), input=' '.join(data_set[0][0]), acc=None)
                data.next()
            return result
        except Exception as e :
            raise Exception("seq2seq eval error : {0}".format(e))
        finally:
            sess.close()

    def _set_train_model(self):
        """
        define train graph
        :return:
        """
        # Create the internal multi-layer cell for our RNN.
        if use_lstm:
            single_cell1 = LSTMCell(self.enc_hidden_size)
            single_cell2 = LSTMCell(self.dec_hidden_size)
        else:
            single_cell1 = GRUCell(self.enc_hidden_size)
            single_cell2 = GRUCell(self.dec_hidden_size)
        enc_cell = MultiRNNCell([single_cell1 for _ in range(self.enc_num_layers)])
        dec_cell = MultiRNNCell([single_cell2 for _ in range(self.dec_num_layers)])

        self.encoder_cell = enc_cell
        self.decoder_cell = dec_cell

        self._make_graph(forward_only)
        self.saver = tf.train.Saver(tf.global_variables())


text:
import sys, time

import dill as pickle

import numpy as np
import matplotlib.pyplot as plt

import scipy.interpolate as interp
import scipy.stats as stats
import scipy.optimize as opti

import bead_util as bu
import calib_util as cal
import transfer_func_util as tf
import configuration as config

theory_data_dir = '/data/grav_sim_data/2um_spacing_data/'

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

def build_mod_grav_funcs(theory_data_dir):
    '''Loads data from the output of /data/grav_sim_data/process_data.py
       which processes the raw simulation output from the farmshare code

       INPUTS: data_dir, path to the directory containing the data

       OUTPUTS: gfuncs, 3 element list with 3D interpolating functions
                        for regular gravity [fx, fy, fz]
                yukfuncs, 3 x Nlambda array with 3D interpolating function
                          for modified gravity with indexing:
                          [[y0_fx, y1_fx, ...], [y0_fy, ...], [y0_fz, ...]]
                lambdas, np.array with all lambdas from the simulation
                lims, 3 element with tuples for (min, max) of coordinate
                      limits in interpolation
    '''

    # Load modified gravity curves from simulation output
    Gdata = np.load(theory_data_dir + 'Gravdata.npy')
    yukdata = np.load(theory_data_dir + 'yukdata.npy')
    lambdas = np.load(theory_data_dir + 'lambdas.npy')
    xpos = np.load(theory_data_dir + 'xpos.npy')
    ypos = np.load(theory_data_dir + 'ypos.npy')
    zpos = np.load(theory_data_dir + 'zpos.npy')

    lambdas = lambdas[::-1]
    yukdata = np.flip(yukdata, 0)

    # Find limits to avoid out of range erros in interpolation
    xlim = (np.min(xpos), np.max(xpos))
    ylim = (np.min(ypos), np.max(ypos))
    zlim = (np.min(zpos), np.max(zpos))

    # Build interpolating functions for regular gravity
    g_fx_func = interp.RegularGridInterpolator((xpos, ypos, zpos), Gdata[:,:,:,0])
    g_fy_func = interp.RegularGridInterpolator((xpos, ypos, zpos), Gdata[:,:,:,1])
    g_fz_func = interp.RegularGridInterpolator((xpos, ypos, zpos), Gdata[:,:,:,2])

    # Build interpolating functions for yukawa-modified gravity
    yuk_fx_funcs = []
    yuk_fy_funcs = []
    yuk_fz_funcs = []
    for lambind, yuklambda in enumerate(lambdas):
        fx_func = interp.RegularGridInterpolator((xpos, ypos, zpos), yukdata[lambind,:,:,:,0])
        fy_func = interp.RegularGridInterpolator((xpos, ypos, zpos), yukdata[lambind,:,:,:,1])
        fz_func = interp.RegularGridInterpolator((xpos, ypos, zpos), yukdata[lambind,:,:,:,2])
        yuk_fx_funcs.append(fx_func)
        yuk_fy_funcs.append(fy_func)
        yuk_fz_funcs.append(fz_func)

    gfuncs = [g_fx_func, g_fy_func, g_fz_func]
    yukfuncs = [yuk_fx_funcs, yuk_fy_funcs, yuk_fz_funcs]
    lims = [xlim, ylim, zlim]

    return np.array(gfuncs), np.array(yukfuncs), \
            np.array(lambdas), np.array(lims)

def ptarr(xarr, yarr, zarr):
    '''Generates the correctly shaped array to be taken by the interpolating
       functions from the (n, 1) dimensional position arrays'''
    return np.stack((xarr, yarr, zarr), axis = -1)

gfuncs, yukfuncs, lambdas, lims = build_mod_grav_funcs(theory_data_dir)

#lam25umind = np.argmin((lambdas-2.5e-5)**2)

#xpltarr = np.arange(lims[0][0], lims[0][1], 1e-7)
#ypltarr = np.arange(-4E-5, 4E-5, 1e-7)
#zpltarr = np.arange(lims[2][0], lims[2][1], 1e-7)

#ones = np.ones_like(ypltarr)
#pts = ptarr(2.5e-5*ones, ypltarr, 0.*ones)

#plt.plot(ypltarr, yukfuncs[0][lam25umind](pts))
#plt.xlabel("displacement [m]")
#plt.ylabel("Fx[N]")
#plt.show()

reason: import numpy,import scipy


text:
"""A simple DQN agent trained to play cartpole."""
from absl import app
from absl import flags
import jax
import jax.numpy as jnp
from jax.experimental import optix
import haiku as hk
import rlax

import gym
import numpy as np

import random
from collections import deque
from typing import Callable, Mapping, NamedTuple, Tuple, Sequence

import matplotlib.pyplot as plt

#Hyperparameters
FLAGS = flags.FLAGS
flags.DEFINE_integer("num_episodes", 5000, "Number of train episodes.")
flags.DEFINE_integer("batch_size", 32, "Size of the training batch")
flags.DEFINE_float("discount_factor", 0.99, "Q-learning discount factor.")
flags.DEFINE_float("learning_rate", 1e-3, "Optimizer learning rate.")
flags.DEFINE_integer("seed", 1729, "Random seed.")

def plot(frame_idx, rewards, losses):
    plt.figure(figsize=(20,5))
    plt.subplot(131)
    plt.title('frame %s. reward: %s' % (frame_idx, np.mean(rewards[-10:])))
    plt.plot(rewards)
    plt.subplot(132)
    plt.title('loss')
    plt.plot(losses)
    plt.show()

class ReplayBuffer(object):
    def __init__(self, capacity):
        self.buffer = deque(maxlen=capacity)

    def push(self, state, action, reward, next_state, done):
        state      = jnp.expand_dims(state, 0)
        next_state = jnp.expand_dims(next_state, 0)

        self.buffer.append((state, action, reward, next_state, done))

    def sample(self, batch_size):
        state, action, reward, next_state, done = zip(*random.sample(self.buffer, batch_size))
        return (jnp.concatenate(state), jnp.concatenate(next_state),
                jnp.asarray(action), jnp.asarray(reward),
                (1.-jnp.asarray(done, dtype=jnp.float32))*FLAGS.discount_factor)

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

def build_network(num_actions: int) -> hk.Transformed:

  def q(obs):
    network = hk.Sequential(
        [hk.Linear(128), jax.nn.relu, hk.Linear(128), jax.nn.relu,
         hk.Linear(num_actions)])
    return network(obs)

  return hk.transform(q)

def main_loop(unused_args):
  # Build env
  env_id = "CartPole-v0"
  env = gym.make(env_id)
  replay_buffer = ReplayBuffer(2000)

  #Epsilon-greedy
  epsilon_start = 1.0
  epsilon_final = 0.01
  epsilon_decay = 500
  def epsilon_by_frame(frame_idx):
    return epsilon_final + (epsilon_start - \
      epsilon_final) * jnp.exp(-1. * frame_idx / epsilon_decay)

  #logging
  losses = []
  all_rewards = []
  episode_reward = 0

  # Build and initialize DQN
  num_actions = env.action_space.n
  network = build_network(num_actions)
  sample_input = env.reset()
  rng = hk.PRNGSequence(jax.random.PRNGKey(FLAGS.seed))
  net_params = network.init(next(rng), sample_input)
  target_params = net_params

   # Build and initialize optimizer.
  optimizer = optix.adam(FLAGS.learning_rate)
  opt_state = optimizer.init(net_params)

  @jax.jit
  def policy(net_params, key, obs, epsilon):
    """Sample action from epsilon-greedy policy."""
    q = network.apply(net_params, obs)
    a = rlax.epsilon_greedy(epsilon).sample(key, q)
    return a

  @jax.jit
  def eval_policy(net_params, key, obs):
    """Sample action from greedy policy."""
    q = network.apply(net_params, obs)
    return rlax.greedy().sample(key, q)

  batched_loss = jax.vmap(rlax.double_q_learning)
  @jax.jit
  def update(net_params, target_params, opt_state, batch):
    """Update network weights wrt Q-learning loss."""

    def dqn_learning_loss(net_params, target_params, batch):
      obs_tm1, obs_t, a_tm1, r_t, discount_t  = batch
      q_tm1 = network.apply(net_params, obs_tm1)
      q_t_value = network.apply(target_params, obs_t)
      q_t_selector = network.apply(net_params, obs_t)

      td_error = batched_loss(q_tm1, a_tm1, r_t, discount_t, q_t_value,
                              q_t_selector)
      return jnp.mean(rlax.l2_loss(td_error))

    loss, dloss_dtheta = jax.value_and_grad(dqn_learning_loss)(net_params,
                                                         target_params, batch)
    updates, opt_state = optimizer.update(dloss_dtheta, opt_state)
    net_params = optix.apply_updates(net_params, updates)
    return net_params, opt_state, loss

  state = env.reset()
  print(f"Training agent for {FLAGS.num_episodes} episodes...")
  for idx in range(1, FLAGS.num_episodes+1):
    epsilon = epsilon_by_frame(idx)

    #Act in the environment and update replay_buffer
    action = policy(net_params, next(rng), state, epsilon)
    next_state, reward, done, _ = env.step(int(action))
    replay_buffer.push(state, action, reward, next_state, done)

    state = next_state
    episode_reward += reward

    if done:
        state = env.reset()
        all_rewards.append(episode_reward)
        episode_reward = 0

    if len(replay_buffer) > FLAGS.batch_size:
      batch = replay_buffer.sample(FLAGS.batch_size)
      net_params, opt_state, loss = update(net_params, target_params,
                                           opt_state, batch)
      losses.append(float(loss))

    #Update Target model parameters
    if idx % 100 == 0:
      target_params = net_params

    if idx % 200 == 0:
      print("Average rewards:", np.mean(all_rewards[-10:]))

  plot(idx, all_rewards, losses)
if __name__ == "__main__":
  app.run(main_loop)

reason: import numpy,import jax,from jax


text:
import numpy as np
from pydrake.all import (RotationMatrix)

def get_rotation_matrix_from_normal(normal):
    n = normal / np.linalg.norm(normal)
    R = np.eye(3)
    R[:, 2] = n
    if np.linalg.norm(n[:2]) < 1e-6:
        R[:, 0] = [0, n[2], -n[1]]
    else:
        R[:, 0] = [n[1], -n[0], 0]
    R[:, 0] /= np.linalg.norm(R[:, 0])
    R[:, 1] = np.cross(n, R[:, 0])

    return R

def calc_tangent_vectors(normal, nd):
    normal = normal.copy()
    normal /= np.linalg.norm(normal)
    if nd == 2:
        # Makes sure that dC is in the yz plane.
        dC = np.zeros((2, 3))
        dC[0] = np.cross(np.array([1, 0, 0]), normal)
        dC[1] = -dC[0]
    else:
        # R = get_rotation_matrix_from_normal(normal)
        R = RotationMatrix.MakeFromOneVector(normal, 2).matrix()
        dC = np.zeros((nd, 3))

        for i in range(nd):
            theta = 2 * np.pi / nd * i
            dC[i] = [np.cos(theta), np.sin(theta), 0]

        dC = (R.dot(dC.T)).T
    return dC


text:
from __future__ import print_function

from skimage.transform import rotate
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage import transform
import numpy as np
import random
import os

import arg_config

# for finding all the different types of shearing files
shear_tag_list = ['sh','shu','shd','shl','shr']
# val:test split is 1:5
val_split = 6

# valid and train dataset lists
idstr_train = []
idstr_valid = []
n_idstr_train = 0
n_idstr_valid = 0

# sets up idstr_train, idstr_valid, n_idstr_train and n_idstr_valid
# idstr_valid is a list of id strings
# idstr_train is a list of different combinations of aumentation
#   which are used to construct the corresponding file name
#   and other proprocessing ops like flipping
def load_data():

  global n_idstr_train
  global n_idstr_valid
  global val_split
  global idstr_train
  global idstr_valid

  # only load once
  if n_idstr_train > 0: return

  # Get train ID Strings from directory names
  train_path = arg_config.cfg['train_path']
  idstrs = next(os.walk(train_path))[1]

  # remove duff training examples
  if '7b38c9173ebe69b4c6ba7e703c0c27f39305d9b2910f46405993d2ea7a963b80' in idstrs:
    print('ignoring 7b38c9173ebe69b4c6ba7e703c0c27f39305d9b2910f46405993d2ea7a963b80')
    idstrs.remove('7b38c9173ebe69b4c6ba7e703c0c27f39305d9b2910f46405993d2ea7a963b80')

  do_short = False
  # short version on mac (always)
  if arg_config.args.machine == 'james_mac':
    do_short = True
  if do_short:
    idstrs = idstrs[:10]

  # stats
  n_idstr = len(idstrs)

  # build the valid and train dataset lists
  for idx in range(n_idstr):
    if idx % val_split == 0:
      # for the valid set we don't want augmented data
      idstr_valid.append(idstrs[idx])
    else:
      # we want all the angles x24
      for angle in range(0,360,15):
        # all the shears x5
        for shear in shear_tag_list:
          # and all filps x2
          for flip in range(2):
            idstr_train.append((idstrs[idx],angle,shear,flip))

  n_idstr_train = len(idstr_train)
  n_idstr_valid = len(idstr_valid)

  # shuffle the valid and train dataset lists
  random.Random(2018).shuffle(idstr_train)
  random.Random(2018).shuffle(idstr_valid)
  # print stats
  print('load_data()...')
  print("n_train_ids / train / val",n_idstr,n_idstr_train,n_idstr_valid)
  print()

# returns X (3 channel input image), y (truth mask) and whether the
# image should be flipped
def gen_XY(is_train=True):
  global idstr_train
  global idstr_valid

  load_data()

  train_path = arg_config.cfg['train_path']

  if not is_train:
    # VALIDATIONS SET
    for idstr in idstr_valid:

      path_X = os.path.join(train_path,idstr,'images','aug_0_sh_' + idstr + '.png')
      path_Y = os.path.join(train_path,idstr,'masks','aug_0_sh_' + idstr + '.png')
      img_X = imread(path_X)[:,:,:3]
      img_Y = imread(path_Y)

      # convert uint8 to float64
      img_X = np.array(img_X) / 255.0
      img_Y = np.array(img_Y) / 255.0

      (xh,xw,_) = img_X.shape
      (yh,yw) = img_Y.shape

      assert yh == xh, "mask is not same shape as image"
      assert yw == xw, "mask is not same shape as image"

      # last field is about flipping - ignore
      yield img_X,img_Y,-1
    return

  while(True):
    # TRAINING SET
    for idstr,angle,shear,do_flip, in idstr_train:
      path_X = os.path.join(train_path,idstr,
                            'images',
                            'aug_' + str(angle) + '_' + shear + '_' + idstr + '.png')
      path_Y = os.path.join(train_path,idstr,
                            'masks',
                            'aug_' + str(angle) + '_' + shear + '_' + idstr + '.png')
      img_X = imread(path_X)[:,:,:3]
      img_Y = imread(path_Y)

      img_X = np.array(img_X) / 255.0
      img_Y = np.array(img_Y) / 255.0

      (xh,xw,_) = img_X.shape
      (yh,yw) = img_Y.shape

      # convert uint8 to float64
      assert yh == xh, "mask is not same shape as image"
      assert yw == xw, "mask is not same shape as image"

      yield img_X,img_Y,do_flip

# takes two images (X,Y) and crops them in the middle
# the middle is shifted randomly according to 'shift'
# if one of the images is empty then it is ignored
def do_random_crop(img_X,img_Y,crop_w,crop_h,shift):

  if len(img_X) > 0:
    h,w = img_X.shape[:2]
  else:
    h,w = img_Y.shape[:2]

  crop_x = (w - crop_w) // 2
  crop_y = (h - crop_h) // 2

  crop_x += random.randint(-shift,shift)
  crop_y += random.randint(-shift,shift)

  if len(img_X) > 0:
    img_X = img_X[crop_y:crop_y+crop_h,
                crop_x:crop_x+crop_w,
                :]
  if len(img_Y) > 0:
    img_Y = img_Y[crop_y:crop_y+crop_h,
                crop_x:crop_x+crop_w]

  return img_X, img_Y

# rotates X & Y by the same angle
def do_rotate(img_X,img_Y,rotation_angle):

    img_X = rotate(img_X, rotation_angle, order=3)
    img_Y = rotate(img_Y, rotation_angle, order=0)

    return img_X,img_Y

# this shears the X & Y around the central point
# if one of the images is empty then it is ignored
def do_shear(img_X,img_Y,sh,horz):

  if len(img_X) > 0:
    h,w = img_X.shape[:2]
  else:
    h,w = img_Y.shape[:2]

  matrix = np.zeros((3,3))
  matrix[0,0] = 1.0
  matrix[1,1] = 1.0
  matrix[2,2] = 1.0

  if horz:
    matrix[0,1] = sh
    matrix[0,2] = -np.sin(sh)* w / 2
  else:
    matrix[1,0] = sh
    matrix[1,2] = -np.sin(sh)* h / 2

  # Create Afine transform
  afine_tf = transform.AffineTransform(matrix)
  #print(afine_tf.params)
  # Apply transform to image data
  if len(img_X) > 0:
    img_X = transform.warp(img_X, inverse_map=afine_tf,mode='edge', order=3)
  if len(img_Y) > 0:
    img_Y = transform.warp(img_Y, inverse_map=afine_tf,mode='edge', order=0)

  return img_X, img_Y

# this returns X&Y after doing some any necesarry preprocessing
def gen_XY_aug(is_train=True):

  XY_gen = gen_XY(is_train=is_train)

  for img_X,img_Y,do_flip in XY_gen:

    (h,w,_) = img_X.shape

    # width of the original center rounded to the nearest 16
    # then add a border of 16 (32)
    # we like a bit of random jitter of the traing data
    # consequently we can't use the whole 32 pixel border
    # contained in the augmented images
    # we can only have a SAFE border of 16 pixels
    w_crop = int(round((w-64)/16))*16 + 32
    h_crop = int(round((h-64)/16))*16 + 32

    if is_train:
      # we like a bit of random jitter of the traing data
      aug_X, aug_Y = do_random_crop(img_X,img_Y,w_crop,h_crop,16)

      # flipping of requested
      if do_flip:
          img_X = np.fliplr(img_X)
          img_Y = np.fliplr(img_Y)
    else:
      # return cropped images with no jitter or augmentation
      aug_X,aug_Y = do_random_crop(img_X,img_Y,w_crop,h_crop,0)

    # get the dimentions ready for the NN
    aug_X = np.expand_dims(aug_X, axis=0)
    aug_Y = np.expand_dims(aug_Y, axis=0)
    aug_Y = np.expand_dims(aug_Y, axis=-1)

    aug_X = np.array(aug_X,dtype=np.float32)
    aug_Y = np.array(aug_Y,dtype=np.float32)

    yield aug_X,aug_Y

if __name__ == "__main__":

  arg_config.arg_config(do_print=True)

  XY_gen = gen_XY(is_train=True)

  for img_X,img_Y,do_flip in XY_gen:

    (h,w,_) = img_X.shape

    # width of the original center rounded to the nearest 16
    # then add a border of 16 (32)
    w_crop = int(round((w-64)/16))*16 + 32
    h_crop = int(round((h-64)/16))*16 + 32

    aug_X, aug_Y = do_random_crop(img_X,img_Y,w_crop,h_crop,16)

    if do_flip:
        img_X = np.fliplr(img_X)
        img_Y = np.fliplr(img_Y)

    img_X,img_Y = do_random_crop(img_X,img_Y,w_crop,h_crop,0)

    print('img_X',np.min(img_X),np.max(img_X))
    print('img_Y',np.min(img_Y),np.max(img_Y))
    print('aug_X',np.min(aug_X),np.max(aug_X))
    print('aug_Y',np.min(aug_Y),np.max(aug_Y))

    plt.subplot(221)
    plt.imshow(img_X)
    plt.subplot(222)
    plt.imshow(img_Y)

    plt.subplot(223)
    plt.imshow(aug_X)
    plt.subplot(224)
    plt.imshow(aug_Y)
    plt.show()


text:
import numpy as np
from Utils.Data.DatasetUtils import is_test_or_val_set, get_train_set_id_from_test_or_val_set, \
    get_test_or_val_set_id_from_train
from Utils.Data.Features.Generated.TweetFeature.IsEngagementType import *
from Utils.Data.Features.MappedFeatures import MappedFeatureEngagerId, MappedFeatureTweetHashtags, MappedFeatureCreatorId, MappedFeatureTweetId
import time
from abc import ABC, abstractmethod

def find_and_increase(creator_id, engager_id, counter_array):
    current_count = counter_array[creator_id]
    counter_array[engager_id] += 1
    return current_count

class CreatorNumberOfPreviousEngagementGivenAbstract(GeneratedFeaturePickle, ABC):

    def __init__(self, dataset_id: str):

        super().__init__("creator_feature_number_of_previous_" + self._get_suffix() + "_engagements_given", dataset_id)
        self.pck_path = pl.Path(
            f"{Feature.ROOT_PATH}/{self.dataset_id}/generated/creator_features/{self.feature_name}.pck.gz")
        self.csv_path = pl.Path(
            f"{Feature.ROOT_PATH}/{self.dataset_id}/generated/creator_features/{self.feature_name}.csv.gz")

    @abstractmethod
    def _get_suffix(self) -> str:
        pass

    @abstractmethod
    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        pass

    @classmethod
    def _save_train_result_if_not_present(cls, result, train_dataset_id):
        if not cls(train_dataset_id).has_feature():
            result.sort_index(inplace=True)
            cls(train_dataset_id).save_feature(result)

    @classmethod
    def _exists_test_feature(cls, test_dataset_id):
        return cls(test_dataset_id).has_feature()

    @classmethod
    def _save_test_result(cls, result, test_dataset_id):
        cls(test_dataset_id).save_feature(result)

    def create_feature(self):
        # Check if the dataset id is train or test
        if is_test_or_val_set(self.dataset_id):
            train_dataset_id = get_train_set_id_from_test_or_val_set(self.dataset_id)
            test_dataset_id = self.dataset_id
        else:
            train_dataset_id = self.dataset_id
            test_dataset_id = get_test_or_val_set_id_from_train(train_dataset_id)

        start_time = time.time()

        # Load features
        creation_timestamps_feature = RawFeatureTweetTimestamp(train_dataset_id)
        creator_id_feature = MappedFeatureCreatorId(train_dataset_id)
        engager_id_feature = MappedFeatureEngagerId(train_dataset_id)
        engagement_feature = self._get_engagement_feature(train_dataset_id)

        # save column names
        creator_id_col = creator_id_feature.feature_name
        engager_id_col = engager_id_feature.feature_name
        engagement_col = engagement_feature.feature_name

        dataframe = pd.concat([
            creation_timestamps_feature.load_or_create(),
            creator_id_feature.load_or_create(),
            engager_id_feature.load_or_create(),
            engagement_feature.load_or_create()
        ], axis=1)

        dataframe.sort_values(creation_timestamps_feature.feature_name, inplace=True)

        max_id = max(dataframe[creator_id_col].max(), dataframe[engager_id_col].max())
        counter_array = np.zeros(max_id + 1, dtype=int)

        result = pd.DataFrame(
            [find_and_increase(creator_id=creator_id, engager_id=engager_id, counter_array=counter_array)
             if engagement else counter_array[creator_id]
             for creator_id, engager_id, engagement in
             zip(dataframe[creator_id_col], dataframe[engager_id_col], dataframe[engagement_col])],
            index=dataframe.index
        )
        self._save_train_result_if_not_present(result, train_dataset_id)

        if not self._exists_test_feature(test_dataset_id):
            # Load features
            # Load features
            creation_timestamps_feature = RawFeatureTweetTimestamp(test_dataset_id)
            creator_id_feature = MappedFeatureCreatorId(test_dataset_id)
            engager_id_feature = MappedFeatureEngagerId(test_dataset_id)

            # save column names
            creator_id_col = creator_id_feature.feature_name
            engager_id_col = engager_id_feature.feature_name

            dataframe = pd.concat([
                creator_id_feature.load_or_create(),
                engager_id_feature.load_or_create(),
                creation_timestamps_feature.load_or_create(),
            ], axis=1)

            dataframe.sort_values(creation_timestamps_feature.feature_name, inplace=True)

            max_id = max(dataframe[creator_id_col].max(), dataframe[engager_id_col].max())

            if max_id + 1 > counter_array.size:
                counter_array = np.pad(
                    counter_array,
                    pad_width=(0, max_id + 1 - counter_array.size),
                    mode='constant',
                    constant_values=0
                )

            result = pd.DataFrame(dataframe[creator_id_col].map(lambda x: counter_array[x]),
                                  index=dataframe.index)

            result.sort_index(inplace=True)

            print("time:")
            print(time.time() - start_time)

            self._save_test_result(result, test_dataset_id)

class CreatorNumberOfPreviousEngagementGivenPositive(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "positive"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsPositive(dataset_id=dataset_id)

class CreatorNumberOfPreviousEngagementGivenNegative(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "negative"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsNegative(dataset_id=dataset_id)

class CreatorNumberOfPreviousEngagementGivenLike(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "like"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsLike(dataset_id=dataset_id)

class CreatorNumberOfPreviousEngagementGivenRetweet(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "retweet"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsRetweet(dataset_id=dataset_id)

class CreatorNumberOfPreviousEngagementGivenReply(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "reply"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsReply(dataset_id=dataset_id)

class CreatorNumberOfPreviousEngagementGivenComment(CreatorNumberOfPreviousEngagementGivenAbstract):

    def _get_suffix(self) -> str:
        return "comment"

    def _get_engagement_feature(self, dataset_id) -> GeneratedFeaturePickle:
        return TweetFeatureEngagementIsComment(dataset_id=dataset_id)


text:
# -*- coding: utf-8 -*-
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from lifelines.utils import CensoringType
from lifelines.fitters import RegressionFitter
from lifelines import CRCSplineFitter

def survival_probability_calibration(model: RegressionFitter, df: pd.DataFrame, t0: float, ax=None):
    r"""
    Smoothed calibration curves for time-to-event models. This is analogous to
    calibration curves for classification models, extended to handle survival probabilities
    and censoring. Produces a matplotlib figure and some metrics.

    We want to calibrate our model's prediction of :math:`P(T < \text{t0})` against the observed frequencies.

    Parameters
    -------------

    model:
        a fitted lifelines regression model to be evaluated
    df: DataFrame
        a DataFrame - if equal to the training data, then this is an in-sample calibration. Could also be an out-of-sample
        dataset.
    t0: float
        the time to evaluate the probability of event occurring prior at.

    Returns
    ----------
    ax:
        mpl axes
    ICI:
        mean absolute difference between predicted and observed
    E50:
        median absolute difference between predicted and observed

    https://onlinelibrary.wiley.com/doi/full/10.1002/sim.8570

    """

    def ccl(p):
        return np.log(-np.log(1 - p))

    if ax is None:
        ax = plt.gca()

    T = model.duration_col
    E = model.event_col

    predictions_at_t0 = np.clip(1 - model.predict_survival_function(df, times=[t0]).T.squeeze(), 1e-10, 1 - 1e-10)

    # create new dataset with the predictions
    prediction_df = pd.DataFrame({"ccl_at_%d" % t0: ccl(predictions_at_t0), T: df[T], E: df[E]})

    # fit new dataset to flexible spline model
    # this new model connects prediction probabilities and actual survival. It should be very flexible, almost to the point of overfitting. It's goal is just to smooth out the data!
    knots = 3
    regressors = {"beta_": ["ccl_at_%d" % t0], "gamma0_": "1", "gamma1_": "1", "gamma2_": "1"}

    # this model is from examples/royson_crowther_clements_splines.py
    crc = CRCSplineFitter(knots, penalizer=0.000001)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore")
        if CensoringType.is_right_censoring(model):
            crc.fit_right_censoring(prediction_df, T, E, regressors=regressors)
        elif CensoringType.is_left_censoring(model):
            crc.fit_left_censoring(prediction_df, T, E, regressors=regressors)
        elif CensoringType.is_interval_censoring(model):
            crc.fit_interval_censoring(prediction_df, T, E, regressors=regressors)

    # predict new model at values 0 to 1, but remember to ccl it!
    x = np.linspace(np.clip(predictions_at_t0.min() - 0.01, 0, 1), np.clip(predictions_at_t0.max() + 0.01, 0, 1), 100)
    y = 1 - crc.predict_survival_function(pd.DataFrame({"ccl_at_%d" % t0: ccl(x)}), times=[t0]).T.squeeze()

    # plot our results
    ax.set_title("Smoothed calibration curve of \npredicted vs observed probabilities of t ≤ %d mortality" % t0)

    color = "tab:red"
    ax.plot(x, y, label="smoothed calibration curve", color=color)
    ax.set_xlabel("Predicted probability of \nt ≤ %d mortality" % t0)
    ax.set_ylabel("Observed probability of \nt ≤ %d mortality" % t0, color=color)
    ax.tick_params(axis="y", labelcolor=color)

    # plot x=y line
    ax.plot(x, x, c="k", ls="--")
    ax.legend()

    # plot histogram of our original predictions
    color = "tab:blue"
    twin_ax = ax.twinx()
    twin_ax.set_ylabel("Count of \npredicted probabilities", color=color)  # we already handled the x-label with ax1
    twin_ax.tick_params(axis="y", labelcolor=color)
    twin_ax.hist(predictions_at_t0, alpha=0.3, bins="sqrt", color=color)

    plt.tight_layout()

    deltas = ((1 - crc.predict_survival_function(prediction_df, times=[t0])).T.squeeze() - predictions_at_t0).abs()
    ICI = deltas.mean()
    E50 = np.percentile(deltas, 50)
    print("ICI = ", ICI)
    print("E50 = ", E50)

    return ax, ICI, E50


text:
import os

import random
from copy import deepcopy

from agents.greedy_agent_boost import GreedyAgentBoost
from agents.greedysearch_agent import GreedySearchAgent
from agents.minmax_agent import MinMaxAgent
from agents.random_agent import RandomAgent
import numpy as np

from agents.state_evaluator_heuristic import StateEvaluatorHeuristic
from arena.arena_multi_thread import ArenaMultiThread
from gym_splendor_code.envs.mechanics.game_settings import USE_TQDM
from gym_splendor_code.envs.mechanics.state_as_dict import StateAsDict
from nn_models.utils.vectorizer import Vectorizer
import pickle

from mpi4py import MPI

from nn_models.value_function_heura.value_function import ValueFunction

comm = MPI.COMM_WORLD
my_rank = MPI.COMM_WORLD.Get_rank()
main_thread = my_rank == 0

def produce_data(when_to_start, dump_p, n_games, filename, folder):
    list_of_agents = [RandomAgent(), GreedyAgentBoost(), MinMaxAgent()]

    arek = ArenaMultiThread()
    arek.start_collecting_states()
    arek.collect_only_from_middle_game(when_to_start, dump_p)
    arek.all_vs_all('deterministic', list_of_agents, n_games)
    arek.dump_collected_states(filename, folder)

def flip_states(list_of_states, list_of_values):
    rev_states = []
    rev_values = []
    for i in range(len(list_of_states)):
        rev_state = deepcopy(list_of_states[i])
        rev_state.change_active_player()
        rev_states.append(rev_state)
        rev_values.append(-list_of_values[i])
    return rev_states, rev_values

def evaluate_states(files_dir, dump_dir):
    evaluator = ValueFunction()

    list_of_files = os.listdir(files_dir)
    for file_name in list_of_files:
        with open(os.path.join(files_dir, file_name), 'rb') as f:
            X, _ = pickle.load(f)
            Y = []
        for x in X:
            state_to_eval = StateAsDict(x).to_state()
            Y.append(evaluator.evaluate(state_to_eval))
            del state_to_eval

        with open(os.path.join(dump_dir, file_name), 'wb') as f:
            pickle.dump((X, Y), f)
            print(len(X))
        del X
        del Y

def pick_data_for_training(epochs_range, files_dir, dump_dir, bufor_size=10):
    states = []
    values = []

    files_list = os.listdir(files_dir)
    for epoch in epochs_range:
        print(f'Epoch = {epoch}')
        bufor = 0
        part = 0
        for file_name in files_list:
            bufor += 1
            print(f'Current file = {file_name}')
            with open(os.path.join(files_dir, file_name), 'rb') as f:
                one_file_data = pickle.load(f)
                for key in one_file_data:
                    if one_file_data[key]['states']:
                        random_idx = random.randint(0, len(one_file_data[key]['states']) - 1)
                        states.append(one_file_data[key]['states'][random_idx])
                        values.append(one_file_data[key]['values'][random_idx])
                del one_file_data
        if bufor > bufor_size:
            bufor = 0
            part += 1
            print('\n Flipping \n')
            # states_rev, values_rev = flip_states(states, values)
            # print('States flipped')
            # states = states + states_rev
            # values = values + values_rev
            # del states_rev
            # del values_rev
            print('Ready to save')
            with open(os.path.join(dump_dir, f'epoch_{epoch}_part_{part}.pickle'), 'wb') as f:
                pickle.dump((states, values), f)
                del states
                del values
                states = []
                values = []

def flatten_data_from_games(source_file, target_file):
    with open(source_file, 'rb') as f:
        one_file_data = pickle.load(f)
    states = []
    values = []
    for key in one_file_data:
        states += one_file_data[key]['states']
        values += one_file_data[key]['values']
    with open(target_file, 'wb') as f:
        pickle.dump((states, values), f)

def load_data_for_model(file):
    with open(file, 'rb') as f:
        data_to_return = pickle.load(f)
    return data_to_return

max_stars_repo_name: TomaszOdrzygozdz/gym-splendor

max_issues_repo_name: TomaszOdrzygozdz/gym-splendor

max_forks_repo_name: TomaszOdrzygozdz/gym-splendor


text:
"""
This is a package for a python interface to ipopt.

The underlying C interface is in pyipoptcore.
"""
import os
import sys

sys.path.append(os.path.dirname(__file__))

import functools

import numpy

from ipoptconst import *
from pyipoptcore import *
from ipoptunconstrained import fmin_unconstrained

# verbose messages from the C interface
set_loglevel(2)

max_stars_repo_name: esmjah/PlantwideControl

max_issues_repo_name: esmjah/PlantwideControl

max_forks_repo_name: esmjah/PlantwideControl


text:
import napari
import numpy as np
from PyQt5.QtCore import Qt
from superqt import QDoubleRangeSlider

from .DataToLayerInterface import DataToLayerInterface

class RangeSlider2(QDoubleRangeSlider):
    def __init__(self, parent=None, type="x"):
        self._parent = parent
        super().__init__(parent)
        self.setOrientation(Qt.Horizontal)
        self.setRange(0, 100)
        self.Range = 100
        self.range = None
        self.setSingleStep(1)
        self.setValue((0, 100))
        self.type = type
        self.backup = self.value()
        self.created_feedback_layer = False
        self.valueChanged.connect(self.virtual_feedback)
        self.reset_in_progress = False

    def reset(self):
        self.reset_in_progress = True
        self.setValue((0, 100))
        self.reset_in_progress = False
        self.parent().update_render_range(self.type, self.getRange())

    def getRange(self):
        return self.value()

    def parent(self):
        return self._parent

    def mouseReleaseEvent(self, event):
        self.backup = self.value()
        self.parent().update_render_range(self.type, self.getRange())
        self.remove_visual_feedback()

    def virtual_feedback(self):
        if not self.reset_in_progress:
            if self.created_feedback_layer:
                if self.value()[0] != self.backup[0]:
                    self.update_visual_feedback()
                elif self.value()[1] != self.backup[1]:
                    self.update_visual_feedback()
                else:
                    pass
            else:
                self.created_feedback_layer = True
                self.create_visual_feedback(0)

    def create_visual_feedback(self, slider):
        v = napari.current_viewer()
        coords, faces = self.get_coords_faces()
        self.range = v.add_surface(
            (coords, faces), opacity=0, shading="smooth", name="render-range",
            colormap=self.parent().active_render_range_box_color
        )

    def update_visual_feedback(self):
        v = napari.current_viewer()
        coords, faces = self.get_coords_faces()
        # print(coords)
        v.layers["render-range"].data = (coords, faces)
        v.layers["render-range"].opacity = self.parent().render_range_box_opacity
        self.range.coords = coords

    def remove_visual_feedback(self):
        v = napari.current_viewer()
        try:
            v.layers.remove(self.range)
        except:
            print("something went wrong while removing feedback layer")
        self.created_feedback_layer = False
        DataToLayerInterface.update_layers(self.parent().data_to_layer_itf)

    def get_coords_faces(self):
        if self.parent().zdim:
            rrx_full = self.parent().data_to_layer_itf.render_range_y
            rrx = rrx_full.copy()
            rrx[0] += (rrx_full[1] - rrx_full[0]) * self.parent().render_range_slider_x_percent[0] / 100
            rrx[1] -= (rrx_full[1] - rrx_full[0]) * (1 - self.parent().render_range_slider_x_percent[1] / 100)
            rry_full = self.parent().data_to_layer_itf.render_range_x
            rry = rry_full.copy()
            rry[0] += (rry_full[1] - rry_full[0]) * self.parent().render_range_slider_y_percent[0] / 100
            rry[1] -= (rry_full[1] - rry_full[0]) * (1 - self.parent().render_range_slider_y_percent[1] / 100)
            rrz_full = self.parent().data_to_layer_itf.render_range_z
            rrz = rrz_full.copy()
            rrz[0] += (rrz_full[1] - rrz_full[0]) * self.parent().render_range_slider_z_percent[0] / 100
            rrz[1] -= (rrz_full[1] - rrz_full[0]) * (1 - self.parent().render_range_slider_z_percent[1] / 100)
            coords = []
            slider_1 = self.value()[0] / self.Range
            slider_2 = self.value()[1] / self.Range
            if self.type == "x":
                for i in [rrz[1], rrz[0]]:
                    for j in [rry[1], rry[0]]:
                        for k in [slider_1 * rrx_full[1], slider_2 * rrx_full[1]]:
                            coords.append([i, j, k])
            elif self.type == "y":
                for i in [rrz[1], rrz[0]]:
                    for j in [slider_1 * rry_full[1], slider_2 * rry_full[1]]:
                        for k in [rrx[1], rrx[0]]:
                            coords.append([i, j, k])
            else:
                for i in [slider_1 * rrz_full[1], slider_2 * rrz_full[1]]:
                    for j in [rry[1], rry[0]]:
                        for k in [rrx[1], rrx[0]]:
                            coords.append([i, j, k])
            faces = [
                [1, 2, 5],
                [2, 5, 6],
                [3, 4, 7],
                [4, 7, 8],
                [1, 3, 7],
                [1, 7, 5],
                [5, 6, 8],
                [5, 7, 8],
                [2, 6, 8],
                [2, 4, 8],
                [1, 2, 4],
                [1, 3, 4],
            ]
            return (
                np.reshape(np.asarray(coords), (8, 3)),
                np.asarray(faces) - 1,
            )
        else:
            rrx_full = self.parent().data_to_layer_itf.render_range_y
            rrx = rrx_full.copy()
            rrx[0] += (rrx_full[1] - rrx_full[0]) * self.parent().render_range_slider_x_percent[0] / 100
            rrx[1] -= (rrx_full[1] - rrx_full[0]) * (1 - self.parent().render_range_slider_x_percent[1] / 100)
            rry_full = self.parent().data_to_layer_itf.render_range_x
            rry = rry_full.copy()
            rry[0] += (rry_full[1] - rry_full[0]) * self.parent().render_range_slider_y_percent[0] / 100
            rry[1] -= (rry_full[1] - rry_full[0]) * (1 - self.parent().render_range_slider_y_percent[1] / 100)
            coords = []
            slider_1 = self.value()[0] / self.Range
            slider_2 = self.value()[1] / self.Range
            if self.type == "x":
                for j in [rry[1], rry[0]]:
                    for k in [slider_1 * rrx_full[1], slider_2 * rrx_full[1]]:
                        coords.append([j, k])
            elif self.type == "y":
                for j in [slider_1 * rry_full[1], slider_2 * rry_full[1]]:
                    for k in [rrx[1], rrx[0]]:
                        coords.append([j, k])
            faces = [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
            return (
                np.reshape(np.asarray(coords), (4, 2)),
                np.asarray(faces) - 1,
            )

max_stars_repo_name: napari-storm/napari-storm

max_issues_repo_name: napari-storm/napari-storm

max_forks_repo_name: napari-storm/napari-storm


text:
# -*- coding: utf-8 -*-
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
#
# Contributors: Michael Laier <m_laie01@uni-muenster.de>

from __future__ import absolute_import, division, print_function

import numpy as np

from pymor.core import ImmutableInterface
from pymor.domaindescriptions import RectDomain
from pymor.functions import ConstantFunction

class InstationaryAdvectionProblem(ImmutableInterface):
    """Instationary advection problem.

    The problem is to solve ::

        ∂_t u(x, t, μ)  +  ∇ ⋅ f(u(x, t, μ), t, μ) = s(x, t, μ)
                                        u(x, 0, μ) = u_0(x, μ)

    for u with t in [0, T], x in Ω.

    Parameters
    ----------
    domain
        A |DomainDescription| of the domain Ω the problem is posed on.
    flux_function
        The |Function| f. The current time is provided by adding the key `'_t'`
        to the |Parameter| `mu`.
    flux_function_derivative
        The derivative of f with respect to u.
    rhs
        The |Function| s. The current time is provided by adding the key `'_t'`
        to the |Parameter| `mu`.
    dirichlet_data
        |Function| providing the Dirichlet boundary values in global coordinates.
    initial_data
        |Function| providing the initial values in global coordinates.
    T
        The end time T.
    name
        Name of the problem.

    Attributes
    ----------
    domain
    rhs
    flux_function
    flux_function_derivative
    initial_data
    dirichlet_data
    T
    """

    def __init__(self, domain=RectDomain(), rhs=ConstantFunction(dim_domain=2),
                 flux_function=ConstantFunction(value=np.array([0, 0]), dim_domain=1),
                 flux_function_derivative=ConstantFunction(value=np.array([0, 0]), dim_domain=1),
                 dirichlet_data=ConstantFunction(value=0, dim_domain=2),
                 initial_data=ConstantFunction(value=1, dim_domain=2), T=1, name=None):
        self.domain = domain
        self.rhs = rhs
        self.flux_function = flux_function
        self.flux_function_derivative = flux_function_derivative
        self.dirichlet_data = dirichlet_data
        self.initial_data = initial_data
        self.T = T
        self.name = name

max_stars_repo_name: ftalbrecht/pymor

max_issues_repo_name: ftalbrecht/pymor

max_forks_repo_name: ftalbrecht/pymor