text:
"""Plotting for graph functions."""
from __future__ import annotations

from types import MappingProxyType
from typing import Any, Union, Mapping, Optional, Sequence, TYPE_CHECKING
from pathlib import Path
from typing_extensions import Literal

from anndata import AnnData
from scanpy.plotting._utils import add_colors_for_categorical_sample_annotation

import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt

from squidpy._docs import d
from squidpy.gr._utils import (
    _get_valid_values,
    _assert_categorical_obs,
    _assert_non_empty_sequence,
)
from squidpy.pl._utils import _heatmap, save_fig
from squidpy._constants._constants import RipleyStat
from squidpy._constants._pkg_constants import Key

__all__ = ["centrality_scores", "interaction_matrix", "nhood_enrichment", "ripley", "co_occurrence"]

Palette_t = Optional[Union[str, mcolors.ListedColormap]]

def _maybe_set_colors(source: AnnData, target: AnnData, key: str, palette: str | None = None) -> None:
    color_key = Key.uns.colors(key)
    try:
        if palette is not None:
            raise KeyError("Unable to copy the palette when there was other explicitly specified.")
        target.uns[color_key] = source.uns[color_key]
    except KeyError:
        add_colors_for_categorical_sample_annotation(target, key=key, force_update_colors=True, palette=palette)

def _get_data(adata: AnnData, cluster_key: str, func_name: str, **kwargs: Any) -> Any:
    key = getattr(Key.uns, func_name)(cluster_key, **kwargs)
    try:
        return adata.uns[key]
    except KeyError:
        raise KeyError(
            f"Unable to get the data from `adata.uns[{key!r}]`. "
            f"Please run `squidpy.gr.{func_name}(..., cluster_key={cluster_key!r})` first."
        ) from None

def _get_palette(adata: AnnData, cluster_key: str, categories: Sequence[Any]) -> Mapping[str, Any] | None:
    try:
        palette = adata.uns[Key.uns.colors(cluster_key)]
        if len(palette) < len(categories):
            raise ValueError(
                f"Expected to find at least `{len(categories)}` colors, "
                f"found `{len(palette)}` for key `{cluster_key}`."
            )
        return dict(zip(categories, palette))
    except KeyError:
        return None

@d.dedent
def centrality_scores(
    adata: AnnData,
    cluster_key: str,
    score: str | Sequence[str] | None = None,
    legend_kwargs: Mapping[str, Any] = MappingProxyType({}),
    palette: Palette_t = None,
    figsize: tuple[float, float] | None = None,
    dpi: int | None = None,
    save: str | Path | None = None,
    **kwargs: Any,
) -> None:
    """
    Plot centrality scores.

    The centrality scores are computed by :func:`squidpy.gr.centrality_scores`.

    Parameters
    ----------
    %(adata)s
    %(cluster_key)s
    score
        Whether to plot all scores or only selected ones.
    legend_kwargs
        Keyword arguments for :func:`matplotlib.pyplot.legend`.
    %(cat_plotting)s

    Returns
    -------
    %(plotting_returns)s
    """
    _assert_categorical_obs(adata, key=cluster_key)
    df = _get_data(adata, cluster_key=cluster_key, func_name="centrality_scores").copy()

    legend_kwargs = dict(legend_kwargs)
    if "loc" not in legend_kwargs:
        legend_kwargs["loc"] = "center left"
        legend_kwargs.setdefault("bbox_to_anchor", (1, 0.5))

    scores = df.columns.values
    df[cluster_key] = df.index.values

    clusters = adata.obs[cluster_key].cat.categories
    palette = _get_palette(adata, cluster_key=cluster_key, categories=clusters) if palette is None else palette

    score = scores if score is None else score
    score = _assert_non_empty_sequence(score, name="centrality scores")
    score = sorted(_get_valid_values(score, scores))

    fig, axs = plt.subplots(1, len(score), figsize=figsize, dpi=dpi, constrained_layout=True)
    axs = np.ravel(axs)  # make into iterable
    for g, ax in zip(score, axs):
        sns.scatterplot(
            x=g,
            y=cluster_key,
            data=df,
            hue=cluster_key,
            hue_order=clusters,
            palette=palette,
            ax=ax,
            **kwargs,
        )
        ax.set_title(str(g).replace("_", " ").capitalize())
        ax.set_xlabel("value")

        ax.set_yticks([])
        ax.legend(**legend_kwargs)

    if save is not None:
        save_fig(fig, path=save)

@d.dedent
def interaction_matrix(
    adata: AnnData,
    cluster_key: str,
    annotate: bool = False,
    method: str | None = None,
    title: str | None = None,
    cmap: str = "viridis",
    palette: Palette_t = None,
    cbar_kwargs: Mapping[str, Any] = MappingProxyType({}),
    figsize: tuple[float, float] | None = None,
    dpi: int | None = None,
    save: str | Path | None = None,
    **kwargs: Any,
) -> None:
    """
    Plot cluster interaction matrix.

    The interaction matrix is computed by :func:`squidpy.gr.interaction_matrix`.

    Parameters
    ----------
    %(adata)s
    %(cluster_key)s
    %(heatmap_plotting)s
    kwargs
        Keyword arguments for :func:`matplotlib.pyplot.text`.

    Returns
    -------
    %(plotting_returns)s
    """
    _assert_categorical_obs(adata, key=cluster_key)
    array = _get_data(adata, cluster_key=cluster_key, func_name="interaction_matrix")

    ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)})
    _maybe_set_colors(source=adata, target=ad, key=cluster_key, palette=palette)
    if title is None:
        title = "Interaction matrix"
    fig = _heatmap(
        ad,
        key=cluster_key,
        title=title,
        method=method,
        cont_cmap=cmap,
        annotate=annotate,
        figsize=(2 * ad.n_obs // 3, 2 * ad.n_obs // 3) if figsize is None else figsize,
        dpi=dpi,
        cbar_kwargs=cbar_kwargs,
        **kwargs,
    )

    if save is not None:
        save_fig(fig, path=save)

@d.dedent
def nhood_enrichment(
    adata: AnnData,
    cluster_key: str,
    mode: Literal["zscore", "count"] = "zscore",
    annotate: bool = False,
    method: str | None = None,
    title: str | None = None,
    cmap: str = "viridis",
    palette: Palette_t = None,
    cbar_kwargs: Mapping[str, Any] = MappingProxyType({}),
    figsize: tuple[float, float] | None = None,
    dpi: int | None = None,
    save: str | Path | None = None,
    **kwargs: Any,
) -> None:
    """
    Plot neighborhood enrichment.

    The enrichment is computed by :func:`squidpy.gr.nhood_enrichment`.

    Parameters
    ----------
    %(adata)s
    %(cluster_key)s
    mode
        Which :func:`squidpy.gr.nhood_enrichment` result to plot. Valid options are:

            - `'zscore'` - z-score values of enrichment statistic.
            - `'count'` - enrichment count.

    %(heatmap_plotting)s
    kwargs
        Keyword arguments for :func:`matplotlib.pyplot.text`.

    Returns
    -------
    %(plotting_returns)s
    """
    _assert_categorical_obs(adata, key=cluster_key)
    array = _get_data(adata, cluster_key=cluster_key, func_name="nhood_enrichment")[mode]

    ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)})
    _maybe_set_colors(source=adata, target=ad, key=cluster_key, palette=palette)
    if title is None:
        title = "Neighborhood enrichment"
    fig = _heatmap(
        ad,
        key=cluster_key,
        title=title,
        method=method,
        cont_cmap=cmap,
        annotate=annotate,
        figsize=(2 * ad.n_obs // 3, 2 * ad.n_obs // 3) if figsize is None else figsize,
        dpi=dpi,
        cbar_kwargs=cbar_kwargs,
        **kwargs,
    )

    if save is not None:
        save_fig(fig, path=save)

@d.dedent
def ripley(
    adata: AnnData,
    cluster_key: str,
    mode: Literal["F", "G", "L"] = "F",
    plot_sims: bool = True,
    palette: Palette_t = None,
    figsize: tuple[float, float] | None = None,
    dpi: int | None = None,
    save: str | Path | None = None,
    legend_kwargs: Mapping[str, Any] = MappingProxyType({}),
    **kwargs: Any,
) -> None:
    """
    Plot Ripley's statistics for each cluster.

    The estimate is computed by :func:`squidpy.gr.ripley`.

    Parameters
    ----------
    %(adata)s
    %(cluster_key)s
    mode
        Ripley's statistics to be plotted.
    plot_sims
        Whether to overlay simulations in the plot.
    %(cat_plotting)s
    legend_kwargs
        Keyword arguments for :func:`matplotlib.pyplot.legend`.
    kwargs
        Keyword arguments for :func:`seaborn.lineplot`.

    Returns
    -------
    %(plotting_returns)s
    """
    _assert_categorical_obs(adata, key=cluster_key)

    res = _get_data(adata, cluster_key=cluster_key, func_name="ripley", mode=mode)

    mode = RipleyStat(mode)  # type: ignore[assignment]
    if TYPE_CHECKING:
        assert isinstance(mode, RipleyStat)

    legend_kwargs = dict(legend_kwargs)
    if "loc" not in legend_kwargs:
        legend_kwargs["loc"] = "center left"
        legend_kwargs.setdefault("bbox_to_anchor", (1, 0.5))

    categories = adata.obs[cluster_key].cat.categories
    palette = _get_palette(adata, cluster_key=cluster_key, categories=categories) if palette is None else palette

    fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
    sns.lineplot(
        y="stats", x="bins", hue=cluster_key, data=res[f"{mode.s}_stat"], hue_order=categories, palette=palette, ax=ax
    )
    if plot_sims:
        sns.lineplot(y="stats", x="bins", ci="sd", alpha=0.01, color="gray", data=res["sims_stat"], ax=ax)
    ax.legend(**legend_kwargs)
    ax.set_ylabel("value")
    ax.set_title(f"Ripley's {mode.s}")

    if save is not None:
        save_fig(fig, path=save)

@d.dedent
def co_occurrence(
    adata: AnnData,
    cluster_key: str,
    palette: Palette_t = None,
    clusters: str | Sequence[str] | None = None,
    figsize: tuple[float, float] | None = None,
    dpi: int | None = None,
    save: str | Path | None = None,
    legend_kwargs: Mapping[str, Any] = MappingProxyType({}),
    **kwargs: Any,
) -> None:
    """
    Plot co-occurrence probability ratio for each cluster.

    The co-occurrence is computed by :func:`squidpy.gr.co_occurrence`.

    Parameters
    ----------
    %(adata)s
    %(cluster_key)s
    clusters
        Cluster instances for which to plot conditional probability.
    %(cat_plotting)s
    legend_kwargs
        Keyword arguments for :func:`matplotlib.pyplot.legend`.
    kwargs
        Keyword arguments for :func:`seaborn.lineplot`.

    Returns
    -------
    %(plotting_returns)s
    """
    _assert_categorical_obs(adata, key=cluster_key)
    occurrence_data = _get_data(adata, cluster_key=cluster_key, func_name="co_occurrence")

    legend_kwargs = dict(legend_kwargs)
    if "loc" not in legend_kwargs:
        legend_kwargs["loc"] = "center left"
        legend_kwargs.setdefault("bbox_to_anchor", (1, 0.5))

    out = occurrence_data["occ"]
    interval = occurrence_data["interval"][1:]
    categories = adata.obs[cluster_key].cat.categories

    clusters = categories if clusters is None else clusters
    clusters = _assert_non_empty_sequence(clusters, name="clusters")
    clusters = sorted(_get_valid_values(clusters, categories))

    palette = _get_palette(adata, cluster_key=cluster_key, categories=categories) if palette is None else palette

    fig, axs = plt.subplots(
        1,
        len(clusters),
        figsize=(5 * len(clusters), 5) if figsize is None else figsize,
        dpi=dpi,
        constrained_layout=True,
    )
    axs = np.ravel(axs)  # make into iterable

    for g, ax in zip(clusters, axs):
        idx = np.where(categories == g)[0][0]
        df = pd.DataFrame(out[idx, :, :].T, columns=categories).melt(var_name=cluster_key, value_name="probability")
        df["distance"] = np.tile(interval, len(categories))

        sns.lineplot(
            x="distance",
            y="probability",
            data=df,
            dashes=False,
            hue=cluster_key,
            hue_order=categories,
            palette=palette,
            ax=ax,
            **kwargs,
        )
        ax.legend(**legend_kwargs)
        ax.set_title(rf"$\frac{{p(exp|{g})}}{{p(exp)}}$")
        ax.set_ylabel("value")

    if save is not None:
        save_fig(fig, path=save)

max_stars_repo_name: dnjst/squidpy

max_issues_repo_name: dnjst/squidpy

max_forks_repo_name: dnjst/squidpy


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

import os
import sys

import numpy as np
#import scipy as sp
from scipy.optimize import curve_fit
from scipy.optimize import OptimizeWarning

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

"""
Author: jyxu
Description:
    Brich-Murnaghan Equation (BM Equation)
    E0 - Equilibrium Energy V0 - Equilibrium Volume
    B0 - Bulk Modulus B0' - Derivate of Bulk Modulus
    E(V) = E0 + (9V0B0)/16 * {\
            [(V0/V)^(2/3)-1]^3*B0' + \
            [(V0/V)^(2/3)-1]^2*[6-4(V0/V)^(2/3)]
            }
Notes:
    Currently, only for bulk optimization.
"""

def BM_FIT(data='fitting_data', picname='curve.png'):
    # read system name
    try:
        with open('INCAR', 'r') as reader:
            lines = reader.readlines()
        lines = [line.strip('\n').split() for line in lines]
        sysname = [line[2] for line in lines if len(line) > 0 and line[0] == 'SYSTEM'][0]
        print(sysname)
    except IOError:
        sysname = ''

    # read data
    with open(data, 'r') as reader:
        lines = reader.readlines()
    lines = [line.strip('\n').split() for line in lines]

    dimension = int(lines[0][0][0]) # 2D / 3D
    print('BM Fit for %dD Lattice Optimization.' %dimension)

    x, y = [], []
    for xy in lines[1:]:
        if xy != []:
            x.append(float(xy[0]))
            y.append(float(xy[1]))

    assert len(x) == len(y), 'Inconsitent number of X and Y.'

    # read poscar and calculate volume
    with open('POSCAR', 'r') as reader:
        lines = reader.readlines()
    lines = [line.strip('\n').split() for line in lines]

    a, b, c = [np.array(line, 'float') for line in lines[2:5]]
    volume = np.dot(a, np.cross(b,c)) # experimental volume

    for i, s in enumerate(x):
        x[i] = s**dimension*volume

    # curve fitting
    def BM_FUNCTION(x, E0, V0, B0, B1):
        return E0 + (9.0*V0*B0)/16.0 * (\
                ((V0/x)**(2/3.0)-1)**3.0*B1 + \
                ((V0/x)**(2/3.0)-1)**2.0*(6-4*(V0/x)**(2/3.0))\
                )

    # initial guess is critical for curve fitting
    try:
        #coefs, cov = curve_fit(BM_FUNCTION, x, y)
        initial_guess = [y[int(len(y)/2)], volume, 1., 1.] # E0, V0, B0, B1
        coefs, cov = curve_fit(BM_FUNCTION, x, y, initial_guess)
    except RuntimeWarning or OptimizeWarning:
        print('Initial guess is critical for curve fitting.')
    else:
        print('Initial guess is critical for curve fitting.')

    E0, V0, B0, B1 = coefs
    best_scaling = (V0 / volume)**(1/dimension)

    # save curve-fitting figure
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8))
    ax.set_title('%s Lattice Optimization Using BM-Equation' %sysname, \
            fontsize=16, fontweight='bold')

    ax.set_xlabel(r'$\bf{Volume\ /\ Å^3}$', \
            fontsize=16, fontweight='bold')
    ax.set_ylabel('Energy / eV', \
            fontsize=16, fontweight='bold')

    # plot scatter points
    points = ax.scatter(x, y, marker='x', color='r', zorder=20, \
    label='Best Scaling %.2f' %best_scaling)

    # plot fitting curve
    x = np.arange(np.min(x), np.max(x), 0.1)
    y = [BM_FUNCTION(i, E0, V0, B0, B1) for i in x]
    curve = ax.plot(x, y, zorder=10, \
            label='E0 = %.2f V0 = %.2f B0 = %.2f B1 = %.2f ' \
            %(coefs[0], coefs[1], coefs[2], coefs[3]))

    # plot (V0, E0)
    point = ax.scatter(V0, E0, marker='*', color='y', s=50, zorder=30, \
            label='Optimized Lattice')

    ax.legend()

    plt.savefig(picname)

if __name__ == '__main__':
    argvs = sys.argv
    nargvs = len(argvs)
    if nargvs == 1:
        BM_FIT()
    elif nargvs == 2:
        data = argvs[1]
        BM_FIT(data)
    elif nargvs == 3:
        data = argvs[1]
        picname = argvs[2]
        BM_FIT(data, picname)

ext: py

max_stars_repo_name: hsulab/DailyScripts

max_issues_repo_name: hsulab/DailyScripts

max_forks_repo_name: hsulab/DailyScripts

reason: import numpy,import scipy,from scipy


text:
from collections import deque
import random

import numpy as np

from gym_super_mario_bros import make
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT

import pylab

from keras.layers import Dense
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.layers.convolutional import Conv2D

from skimage.color import rgb2gray
from skimage.transform import resize

from nes_py.wrappers import BinarySpaceToDiscreteSpaceEnv

EPISODES = 4000
MODELFILENAME = './model/supermariobros_dqn.h5'
GRAPHFILENAME = './graph/supermariobros_dqn.png'
ENVNAME = 'SuperMarioBros-v0'
DOWNSAMPLEH = 84

class DQNAgent:
    def __init__(self, state_size, action_size):
        # Cartpole이 학습하는 것을 보려면 "True"로 바꿀 것
        self.render = True

        # state와 action의 크기를 가져와서 모델을 생성하는데 사용함
        self.state_size = state_size
        self.action_size = action_size

        # Cartpole DQN 학습의 Hyper parameter 들
        # deque를 통해서 replay memory 생성
        self.discount_factor = 0.99
        self.learning_rate = 0.001
        self.epsilon = 1.0
        self.epsilon_min = 0.005
        self.epsilon_decay = (self.epsilon - self.epsilon_min) / 50000
        self.batch_size = 64
        self.train_start = 1000
        self.memory = deque(maxlen=10000)

        # 학습할 모델과 타겟 모델을 생성
        self.model = self.build_model()
        self.target_model = self.build_model()
        # 학습할 모델을 타겟 모델로 복사 --> 타겟 모델의 초기화(weight를 같게 해주고 시작해야 함)
        self.update_target_model()

    # Deep Neural Network를 통해서 Q Function을 근사
    # state가 입력, 각 행동에 대한 Q Value가 출력인 모델을 생성
    def build_model(self):
        model = Sequential()
        model.add(Conv2D(32, (8, 8), strides=(4, 4), activation='relu',
                         input_shape=self.state_size))
        model.add(Conv2D(64, (4, 4), strides=(2, 2), activation='relu'))
        model.add(Conv2D(64, (3, 3), strides=(1, 1), activation='relu'))
        model.add(Flatten())
        model.add(Dense(512, activation='relu'))
        model.add(Dense(self.action_size))
        model.summary()
        model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate))
        return model

    # 일정한 시간 간격마다 타겟 모델을 현재 학습하고 있는 모델로 업데이트
    def update_target_model(self):
        self.target_model.set_weights(self.model.get_weights())

    # 행동의 선택은 현재 네트워크에 대해서 epsilon-greedy 정책을 사용
    def get_action(self, state):
        if np.random.rand() <= self.epsilon:
            return random.randrange(self.action_size)
        else:
            q_value = self.model.predict(state)
            return np.argmax(q_value[0])

    # <s,a,r,s'>을 replay_memory에 저장함
    def replay_memory(self, state, action, reward, next_state, done):
        self.memory.append((state, action, reward, next_state, done))
        if self.epsilon > self.epsilon_min:
            self.epsilon -= self.epsilon_decay
        # print(len(self.memory))

    # replay memory에서 batch_size 만큼의 샘플들을 무작위로 뽑아서 학습
    def train_replay(self):
        if len(self.memory) < self.train_start:
            return

        print("train_replay : -----")

        batch_size = min(self.batch_size, len(self.memory))
        mini_batch = random.sample(self.memory, batch_size)

        update_input = np.zeros((batch_size, self.state_size[0], self.state_size[1], self.state_size[2]))
        update_target = np.zeros((batch_size, self.action_size))

        for i in range(batch_size):
            state, action, reward, next_state, done = mini_batch[i]
            target = self.model.predict(state)[0]

            # 큐러닝에서와 같이 s'에서의 최대 Q Value를 가져옴. 단, 타겟 모델에서 가져옴
            if done:
                target[action] = reward
            else:
                target[action] = reward + self.discount_factor * \
                                          np.amax(self.target_model.predict(next_state)[0])
            update_input[i] = state
            update_target[i] = target

        # 학습할 정답인 타겟과 현재 자신의 값의 minibatch를 만들고 그것으로 한 번에 모델 업데이트
        self.model.fit(update_input, update_target, batch_size=batch_size, epochs=1, verbose=0)

        print("train_replay : =====")

    # 저장한 모델을 불러옴
    def load_model(self, name):
        from pathlib import Path
        my_file = Path(name)
        if my_file.is_file():
            self.model.load_weights(name)

    # 학습된 모델을 저장함
    def save_model(self, name):
        self.model.save_weights(name)

# 210*160*3(color) --> 84*84(mono)
# float --> integer (to reduce the size of replay memory)
def pre_processing(observe):
    processed_observe = np.uint8(
        resize(rgb2gray(observe), (DOWNSAMPLEH, DOWNSAMPLEH), mode='constant') * 255)
    return processed_observe

if __name__ == "__main__":
    # CartPole-v1의 경우 500 타임스텝까지 플레이가능
    env = make(ENVNAME)
    env = BinarySpaceToDiscreteSpaceEnv(env, SIMPLE_MOVEMENT)

    # 환경으로부터 상태와 행동의 크기를 가져옴
    state_size  = env.observation_space.shape
    action_size = env.action_space.n

    # print(state_size)
    # print(action_size)
    # import sys
    # sys.exit(0)

    # DQN 에이전트의 생성
    agent = DQNAgent(state_size, action_size)
    agent.load_model(MODELFILENAME)
    scores, episodes = [], []

    for e in range(EPISODES):
        done = False
        score = 0
        state = env.reset()
        #state = pre_processing(state)
        state = np.reshape(state, (1, state_size[0], state_size[1], state_size[2]))
        #print(state)

        # 같은 액션을 4번하기 위한 카운터
        action_count = 0

        while not done:
            if agent.render:
                env.render()

            # 현재 상태에서 행동을 선택하고 한 스텝을 진행
            action_count = action_count + 1
            action = 0
            if action_count == 4:
                action = agent.get_action(state)
                action_count = 0

            # 선택한 액션으로 1 step을 시행한다
            next_state, reward, done, info = env.step(action)
            #next_state = pre_processing(next_state)
            next_state = np.reshape(next_state, (1, state_size[0], state_size[1], state_size[2]))
            # 에피소드를 끝나게 한 행동에 대해서 -100의 패널티를 줌
            #reward = reward if not done else -100

            # <s, a, r, s'>을 replay memory에 저장
            agent.replay_memory(state, action, reward, next_state, done)
            # 매 타임스텝마다 학습을 진행
            agent.train_replay()
            score += reward
            state = next_state

            if done:
                env.reset()
                # 매 에피소드마다 학습하는 모델을 타겟 모델로 복사
                agent.update_target_model()

                # 각 에피소드마다 cartpole이 서있었던 타임스텝을 plot
                scores.append(score)
                episodes.append(e)
                #pylab.plot(episodes, scores, 'b')
                #pylab.savefig(GRAPHFILENAME)
                print("episode:", e, "  score:", score, "  memory length:",
                    len(agent.memory), "  epsilon:", agent.epsilon)

        # 50 에피소드마다 학습 모델을 저장
        if e % 50 == 0:
             agent.save_model(MODELFILENAME)

max_stars_repo_name: iamslash/examplesofml

max_issues_repo_name: iamslash/examplesofml

max_forks_repo_name: iamslash/examplesofml

reason: import numpy


text:
from profittm.ProfitTM import ProfitTM
import numpy as np
import pandas as pd
from profittm.save_load import save, load
import networkx as nx
import uuid
from profittm.graph_draw import plotGraph
from sklearn.preprocessing import OneHotEncoder

class TreeProfitTM():

    def __init__(self, maxDepth=None, curLevel=0, parentsName=None):
        self.node = None
        self.childs = {}
        self.topicNames = None
        self.topicCount = None
        self.maxDepth = maxDepth
        self.curLevel = curLevel
        self.treeName = str(uuid.uuid4())

        if curLevel == 0 and parentsName is None:
            self.isRoot = True
        else:
            self.isRoot = False

        pass

    def fit(self, x):

        if self.curLevel == 0:
            #x = np.array(x)
            self.node = ProfitTM()
            self.node.fitTextVectorizer(x)
            self.node.cacheTextVectors(x)
            self.node.fit(x)
            self.topicNames = self.node.getTopicNames(x)
            self.topicCount = self.node.topicCount
        else:
            self.node.cacheTextVectors(x)
            self.node.fit(x)
            self.topicNames = self.node.getTopicNames(x)
            self.topicCount = self.node.topicCount

        if self.curLevel + 1 < self.maxDepth:
            y = self.node.predict(x)
            #self.node.cleanCache()
            uniqY = np.unique(y)
            topicDocs = {}
            for topic in uniqY:
                #topicDocs[topic] = x[y == topic]
                ########
                topicDocs[topic] = []
                for i in range(len(y)):
                    if y[i] == topic:
                        topicDocs[topic].append(x[i])
                ########
                self.childs[topic] = TreeProfitTM(self.maxDepth, self.curLevel + 1)
                self.childs[topic].node = ProfitTM()
                self.childs[topic].node.setVectorizer(self.node.vectorizer)
                #if self.isRoot is False:
                #    self.node.vectorizer = None

        self.node.cleanCache()

        if self.curLevel + 1 < self.maxDepth:
            for topic in topicDocs.keys():
                self.childs[topic].fit(topicDocs[topic])
        pass

    def predict(self, x, returnVectors=False):

        sharedPredicts = self.prepareToPredict(x)
        self.hPredict(sharedPredicts, predInds=None)

        if returnVectors:
            sharedPredicts = self.convertPredictsToVectors(sharedPredicts)

        return sharedPredicts

    def prepareToPredict(self, x):
        sharedPredicts = pd.DataFrame({"docs": x})
        for i in range(self.maxDepth):
            sharedPredicts[i] = None
        return sharedPredicts

    def hPredict(self, sharedPredicts, predInds=None):

        if self.curLevel < self.maxDepth:

            if predInds is None:
                predInds = sharedPredicts.index

            docs = sharedPredicts["docs"]
            y = self.node.predict(docs[predInds].values)
            sharedPredicts.iloc[predInds, self.curLevel + 1] = y
            uniqY = np.unique(y)
            for topic in uniqY:
                nextInds = docs[predInds][y == topic].index
                if len(self.childs.keys()) == 0:
                    self.leafPredict(sharedPredicts, nextInds)
                else:
                    self.childs[topic].hPredict(sharedPredicts, nextInds)
        pass

    def leafPredict(self, sharedPredicts, predInds):
        docs = sharedPredicts["docs"]
        y = self.node.predict(docs[predInds].values)
        sharedPredicts.iloc[predInds, self.curLevel + 1] = y
        pass

    def plotTopicTree(self):
        topicGraph = nx.OrderedGraph()
        self.buildTopicGraph(topicGraph)
        plotGraph(topicGraph)
        pass

    def buildTopicGraph(self, graph):

        if self.isRoot:
            for parentTopic in self.topicNames.keys():
                parentTopicName = self.extractTopicName(self.topicNames[parentTopic])
                graph.add_edge("docs", parentTopicName)

        if len(self.childs.keys()) != 0:
            for parentTopic in self.topicNames.keys():
                parentTopicName = self.extractTopicName(self.topicNames[parentTopic])
                for childTopic in self.childs[parentTopic].topicNames.keys():
                    childTopicName = self.extractTopicName(self.childs[parentTopic].topicNames[childTopic])
                    graph.add_edge(parentTopicName, childTopicName)
            for key in self.childs.keys():
                self.childs[key].buildTopicGraph(graph)
        pass

    def extractTopicName(self, topicTuple):
        topic = []
        for i in range(len(topicTuple)):
            topic.append(topicTuple[i][0])
        topic = "\n".join(topic)
        return topic

    def buildTreeGraph(self, graph):
        if len(self.childs.keys()) != 0:
            for key in self.childs.keys():
                graph.add_edge(self.treeName, self.childs[key].treeName, weight=key)
            for key in self.childs.keys():
                self.childs[key].buildTreeGraph(graph)
        pass

    def getTopicDict(self):

        sharedTopicDict = {}
        self.enrichTopicDict(sharedTopicDict)
        return sharedTopicDict

    def enrichTopicDict(self, sharedTopicDict, parentTopicID=None):
        if self.isRoot:
            for parentTopic in self.topicNames.keys():
                sharedTopicDict[parentTopic] = self.extractTopicName(self.topicNames[parentTopic])

        if len(self.childs.keys()) != 0:
            for parentTopic in self.topicNames.keys():
                for childTopic in self.childs[parentTopic].topicNames.keys():
                    if self.isRoot:
                        parentTopicID = str(parentTopic)
                    childID = str(parentTopicID) + "." + str(childTopic)
                    sharedTopicDict[childID] = self.extractTopicName(self.childs[parentTopic].topicNames[childTopic])
                    self.childs[parentTopic].enrichTopicDict(sharedTopicDict, childID)
        pass

    def getTopicVectorsDict(self):
        topicDict = self.getTopicDict()
        topicIds = np.array(list(topicDict.keys())).reshape((-1, 1))
        topicVecs = OneHotEncoder(dtype=int, sparse=False).fit_transform(topicIds)
        topicIds = topicIds.reshape((-1, ))

        topicVecsDict = {}
        for i in range(len(topicVecs)):
            topicVecsDict[topicIds[i]] = topicVecs[i]

        return topicVecsDict

    def convertPredictsToVectors(self, sharedPredicts):
        labels = np.zeros((len(sharedPredicts), self.maxDepth), dtype=int)

        labels = labels.T
        for i in range(self.maxDepth):
            labels[i] = sharedPredicts[i].values
        labels = labels.T
        print(labels[:10])

        labels = list(labels)
        for i in range(len(labels)):
            labels[i] = list(labels[i])
            for j in range(len(labels[i])):
                labels[i][j] = str(labels[i][j])
        print(labels[:10])

        ids = []
        for i in range(len(labels)):
            ids.append( ".".join(labels[i]) )
        print(ids[:10])

        topicVectorsDict = self.getTopicVectorsDict()
        vectors = []
        for i in range(len(ids)):
            vectors.append( topicVectorsDict[ids[i]] )
        vectors = np.array(vectors)
        print(vectors[:10])

        return vectors

    def save(self, name, dir):
        print("Saving whole hierarchy topic model to {}...".format(dir + name))
        treeGraph = nx.OrderedGraph()
        self.buildTreeGraph(treeGraph)
        save(dir + name + "_treegraph.pkl", treeGraph)
        vectorizer = self.node.vectorizer
        save(dir + name + "_vectorizer.pkl", vectorizer)
        self.removeVectorizers()
        self.saveTrees(name, dir)
        self.placeBackVectorizers(vectorizer)
        print("The whole hierarchy topic model saved.")
        pass

    def removeVectorizers(self):
        self.node.vectorizer = None
        if len(self.childs.keys()) != 0:
            for key in self.childs.keys():
                self.childs[key].removeVectorizers()

    def placeBackVectorizers(self, vectorizer):
        self.node.vectorizer = vectorizer
        if len(self.childs.keys()) != 0:
            for key in self.childs.keys():
                self.childs[key].node.vectorizer = vectorizer

    def saveTrees(self, name, dir):

        print("Saving tree {}".format(dir + name + "_{}".format(self.treeName)))

        self.node.save(name + "_{}".format(self.treeName), dir)
        node = self.node
        self.node = None

        childs = self.childs
        self.childs = None
        save(dir + name + "_{}_metadata.pkl".format(self.treeName), self)
        self.node = node
        self.childs = childs

        for key in self.childs.keys():
            self.childs[key].saveTrees(name, dir)

    def load(self, name, dir):

        print("Loading whole hierarchy topic model from {}...".format(dir + name))
        treeGraph = load(dir + name + "_treegraph.pkl")
        vectorizer = load(dir + name + "_vectorizer.pkl")

        trees = {}
        for node in treeGraph.nodes:
            treeName = node
            trees[node] = self.loadTree(name, dir, treeName)

        edges = list(treeGraph.edges.data("weight"))
        for edge in edges:
            trees[edge[0]].childs[edge[2]] = trees[edge[1]]

        loadedTree = None
        for key in trees.keys():
            if trees[key].isRoot:
                loadedTree = trees[key]
                break

        loadedTree.placeBackVectorizers(vectorizer)
        print("The whole hierarchy topic model loaded.")
        return loadedTree

    def loadTree(self, name, dir, treeName):

        tree = load(dir + name + "_{}_metadata.pkl".format(treeName))
        tree.node = ProfitTM()
        tree.node.load(name + "_{}".format(treeName), dir)
        tree.childs = {}
        return tree

max_stars_repo_name: CameleoGrey/ProfitTM

max_issues_repo_name: CameleoGrey/ProfitTM

max_forks_repo_name: CameleoGrey/ProfitTM

reason: import numpy,import networkx


text:
import pytest

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

from fracdiff import Fracdiff

class TestScikitLearn:
    @pytest.mark.parametrize("seed", [42])
    @pytest.mark.parametrize("n_samples", [20, 100])
    @pytest.mark.parametrize("n_features", [1, 10])
    @pytest.mark.parametrize("d", [0.5])
    def test_sample(self, seed, n_samples, n_features, d):
        np.random.seed(seed)

        X = np.random.randn(n_samples, n_features)
        fracdiff = Fracdiff(d).transform(X)

    @pytest.mark.parametrize("seed", [42])
    @pytest.mark.parametrize("n_samples", [20, 100])
    @pytest.mark.parametrize("n_features", [1, 10])
    @pytest.mark.parametrize("d", [0.5])
    def test_sample(self, seed, n_samples, n_features, d):
        np.random.seed(seed)

        X = np.random.randn(n_samples, n_features)
        y = np.random.randn(n_samples)

        pipeline = Pipeline(
            [
                ("scaler", StandardScaler()),
                ("fracdiff", Fracdiff(d)),
                ("regressor", LinearRegression()),
            ]
        )

        pipeline.fit(X, y)

max_stars_repo_name: salihkurucan/fracdiff

max_issues_repo_name: salihkurucan/fracdiff

max_forks_repo_name: salihkurucan/fracdiff


text:
import threading
import numpy as np
import tensorflow as tf
import pylab
import time
import gym
import os
from keras.layers import Dense, Input, Lambda
from keras.models import Model
from keras.optimizers import Adam
from keras import backend as K

# global variables for threading
episode = 0
scores = []

EPISODES = 5000

model_path = os.path.join(os.getcwd(), 'save_model')
graph_path = os.path.join(os.getcwd(), 'save_graph')

if not os.path.isdir(model_path):
    os.mkdir(model_path)

if not os.path.isdir(graph_path):
    os.mkdir(graph_path)

# This is A3C(Asynchronous Advantage Actor Critic) agent(global) for the Cartpole
# In this example, we use A3C algorithm
class A3CAgent:
    def __init__(self, state_size, action_size, env_name):
        # get size of state and action
        self.state_size = state_size
        self.action_size = action_size

        # get gym environment name
        self.env_name = env_name

        # these are hyper parameters for the A3C
        self.actor_lr = 0.0001
        self.critic_lr = 0.001
        self.discount_factor = .9
        self.hidden1, self.hidden2 = 24, 24
        self.threads = 8

        # create model for actor and critic network
        self.actor, self.critic = self.build_model()

        # method for training actor and critic network
        self.optimizer = [self.actor_optimizer(), self.critic_optimizer()]

        self.sess = tf.InteractiveSession()
        K.set_session(self.sess)
        self.sess.run(tf.global_variables_initializer())

    # approximate policy and value using Neural Network
    # actor -> state is input and probability of each action is output of network
    # critic -> state is input and value of state is output of network
    # actor and critic network share first hidden layer
    def build_model(self):
        state = Input(batch_shape=(None,  self.state_size))
        actor_input = Dense(self.hidden1, input_dim=self.state_size, activation='relu')(state)
        actor_hidden = Dense(self.hidden2, activation='relu')(actor_input)
        mu_0 = Dense(self.action_size, activation='tanh')(actor_hidden)
        sigma_0 = Dense(self.action_size, activation='softplus')(actor_hidden)

        mu = Lambda(lambda x: x * 2)(mu_0)
        sigma = Lambda(lambda x: x + 0.0001)(sigma_0)

        critic_input = Dense(self.hidden1, input_dim=self.state_size, activation='relu')(state)
        value_hidden = Dense(self.hidden2, activation='relu', kernel_initializer='he_uniform')(critic_input)
        state_value = Dense(1, activation='linear', kernel_initializer='he_uniform')(value_hidden)

        actor = Model(inputs=state, outputs=(mu, sigma))
        critic = Model(inputs=state, outputs=state_value)

        actor._make_predict_function()
        critic._make_predict_function()

        actor.summary()
        critic.summary()

        return actor, critic

    # make loss function for Policy Gradient
    # [log(action probability) * advantages] will be input for the back prop
    # we add entropy of action probability to loss
    def actor_optimizer(self):
        action = K.placeholder(shape=(None,1))
        advantages = K.placeholder(shape=(None,1))

        # mu = K.placeholder(shape=(None, self.action_size))
        # sigma_sq = K.placeholder(shape=(None, self.action_size))

        mu, sigma_sq = self.actor.output

        pdf = 1. / K.sqrt(2. * np.pi * sigma_sq) * K.exp(-K.square(action - mu) / (2. * sigma_sq))
        log_pdf = K.log(pdf + K.epsilon())
        entropy = K.sum(0.5 * (K.log(2. * np.pi * sigma_sq) + 1.))

        exp_v = log_pdf * advantages

        exp_v = K.sum(exp_v + 0.01 * entropy)
        actor_loss = -exp_v

        optimizer = Adam(lr=self.actor_lr)
        updates = optimizer.get_updates(self.actor.trainable_weights, [], actor_loss)

        train = K.function([self.actor.input, action, advantages], [], updates=updates)
        return train

    # make loss function for Value approximation
    def critic_optimizer(self):
        discounted_reward = K.placeholder(shape=(None, 1))

        value = self.critic.output

        loss = K.mean(K.square(discounted_reward - value))

        optimizer = Adam(lr=self.critic_lr)
        updates = optimizer.get_updates(self.critic.trainable_weights, [], loss)
        train = K.function([self.critic.input, discounted_reward], [], updates=updates)
        return train

    # make agents(local) and start training
    def train(self):
        # self.load_model('./save_model/cartpole_a3c.h5')
        agents = [Agent(i, self.actor, self.critic, self.optimizer, self.env_name, self.discount_factor,
                        self.action_size, self.state_size) for i in range(self.threads)]

        for agent in agents:
            agent.start()

        while True:
            time.sleep(20)

            plot = scores[:]
            pylab.plot(range(len(plot)), plot, 'b')
            pylab.savefig("./save_graph/cartpole_a3c.png")

            self.save_model('./save_model/cartpole_a3c.h5')

    def save_model(self, name):
        self.actor.save_weights(name + "_actor.h5")
        self.critic.save_weights(name + "_critic.h5")

    def load_model(self, name):
        self.actor.load_weights(name + "_actor.h5")
        self.critic.load_weights(name + "_critic.h5")

# This is Agent(local) class for threading
class Agent(threading.Thread):
    def __init__(self, index, actor, critic, optimizer, env_name, discount_factor, action_size, state_size):
        threading.Thread.__init__(self)

        self.states = []
        self.rewards = []
        self.actions = []

        self.index = index
        self.actor = actor
        self.critic = critic
        self.optimizer = optimizer
        self.env_name = env_name
        self.discount_factor = discount_factor
        self.action_size = action_size
        self.state_size = state_size

    # Thread interactive with environment
    def run(self):
        global episode
        env = gym.make(self.env_name)
        while episode < EPISODES:
            state = env.reset()
            score = 0
            step = 0
            while True:
                action = self.get_action(state)

                next_state, reward, done, _ = env.step(action)
                reward /= 10
                score += reward
                step += 1

                state = list(state)
                action = action[0]
                self.memory(state, action, reward)

                state = next_state

                if done:
                    episode += 1
                    print("episode: ", episode, "/ score : ", score, "/ step : ", step)
                    scores.append(score)
                    self.train_episode(score != 500)
                    break

    # In Policy Gradient, Q function is not available.
    # Instead agent uses sample returns for evaluating policy
    def discount_rewards(self, rewards, done=True):
        discounted_rewards = np.zeros_like(rewards)
        running_add = 0
        if not done:
            running_add = self.critic.predict(np.reshape(self.states[-1], (1, self.state_size)))[0]
        for t in reversed(range(0, len(rewards))):
            running_add = running_add * self.discount_factor + rewards[t]
            discounted_rewards[t] = running_add
        return discounted_rewards

    # save <s, a ,r> of each step
    # this is used for calculating discounted rewards
    def memory(self, state, action, reward):
        self.states.append(state)
        self.actions.append(action)
        self.rewards.append(reward)

    # update policy network and value network every episode
    def train_episode(self, done):
        discounted_rewards = self.discount_rewards(self.rewards, done)

        # print("states size : ",len(self.states)," ", len(self.states[0]))
        # print("actions_size : ",len(self.actions))

        states = np.asarray(self.states, dtype='float32')

        values = self.critic.predict(states)
        # print("value : ", values.shape)
        # values = np.reshape(values, (len(values), 1))
        # print("value2 : ", values.shape)

        advantages = discounted_rewards - values

        # action = np.array(self.actions)
        # print(action.shape)
        # print(advantages.shape)

        self.optimizer[0]([self.states, self.actions, advantages])
        self.optimizer[1]([self.states, discounted_rewards])
        self.states, self.actions, self.rewards = [], [], []

    def get_action(self, state):
        mu, sigma_sq = self.actor.predict(np.reshape(state, [1, self.state_size]))
        # sigma_sq = np.log(np.exp(sigma_sq + 1))
        epsilon = np.random.randn(self.action_size)
        action = mu + np.sqrt(sigma_sq) * epsilon
        action = np.clip(action, -2, 2)
        return action

if __name__ == "__main__":
    # env_name = 'CartPole-v1'
    env_name = 'Pendulum-v0'
    env = gym.make(env_name)

    state_size = env.observation_space.shape[0]
    action_size = env.action_space.shape[0]
    print("action size : ", action_size)
    print("state size : ", state_size)
    action_bound = [env.action_space.low, env.action_space.high]
    action_gap = env.action_space.high - env.action_space.low

    env.close()

    global_agent = A3CAgent(state_size, action_size, env_name)
    global_agent.train()

max_stars_repo_name: basab-dattaray/ActorCritic

max_issues_repo_name: basab-dattaray/ActorCritic

max_forks_repo_name: basab-dattaray/ActorCritic


text:
from sympy import Abs, exp, Expr, I, pi, Q, Rational, refine, S, sqrt
from sympy.abc import x, y, z
from sympy.core.relational import Eq, Ne
from sympy.functions.elementary.piecewise import Piecewise

def test_Abs():
    assert refine(Abs(x), Q.positive(x)) == x
    assert refine(1 + Abs(x), Q.positive(x)) == 1 + x
    assert refine(Abs(x), Q.negative(x)) == -x
    assert refine(1 + Abs(x), Q.negative(x)) == 1 - x

    assert refine(Abs(x**2)) != x**2
    assert refine(Abs(x**2), Q.real(x)) == x**2

def test_pow():
    assert refine((-1)**x, Q.even(x)) == 1
    assert refine((-1)**x, Q.odd(x)) == -1
    assert refine((-2)**x, Q.even(x)) == 2**x

    # nested powers
    assert refine(sqrt(x**2)) != Abs(x)
    assert refine(sqrt(x**2), Q.complex(x)) != Abs(x)
    assert refine(sqrt(x**2), Q.real(x)) == Abs(x)
    assert refine(sqrt(x**2), Q.positive(x)) == x
    assert refine((x**3)**(S(1)/3)) != x

    assert refine((x**3)**(S(1)/3), Q.real(x)) != x
    assert refine((x**3)**(S(1)/3), Q.positive(x)) == x

    assert refine(sqrt(1/x), Q.real(x)) != 1/sqrt(x)
    assert refine(sqrt(1/x), Q.positive(x)) == 1/sqrt(x)

    # powers of (-1)
    assert refine((-1)**(x + y), Q.even(x)) == (-1)**y
    assert refine((-1)**(x + y + z), Q.odd(x) & Q.odd(z)) == (-1)**y
    assert refine((-1)**(x + y + 1), Q.odd(x)) == (-1)**y
    assert refine((-1)**(x + y + 2), Q.odd(x)) == (-1)**(y + 1)
    assert refine((-1)**(x + 3)) == (-1)**(x + 1)

    # powers of Abs
    assert refine(Abs(x)**2, Q.real(x)) == x**2
    assert refine(Abs(x)**3, Q.real(x)) == Abs(x)**3
    assert refine(Abs(x)**2) == Abs(x)**2

def test_exp():
    assert refine(exp(pi*I*2*x), Q.integer(x)) == 1
    assert refine(exp(pi*I*2*(x + Rational(1, 2))), Q.integer(x)) == -1
    assert refine(exp(pi*I*2*(x + Rational(1, 4))), Q.integer(x)) == I
    assert refine(exp(pi*I*2*(x + Rational(3, 4))), Q.integer(x)) == -I

def test_Relational():
    assert refine(x < 0, ~Q.is_true(x < 0)) == False
    assert refine(x < 0, Q.is_true(x < 0)) == True
    assert refine(x < 0, Q.is_true(y < 0)) == (x < 0)
    assert refine(x <= 0, ~Q.is_true(x <= 0)) == False
    assert refine(x <= 0,  Q.is_true(x <= 0)) == True
    assert refine(x <= 0,  Q.is_true(y <= 0)) == (x <= 0)
    assert refine(x > 0, ~Q.is_true(x > 0)) == False
    assert refine(x > 0,  Q.is_true(x > 0)) == True
    assert refine(x > 0,  Q.is_true(y > 0)) == (x > 0)
    assert refine(x >= 0, ~Q.is_true(x >= 0)) == False
    assert refine(x >= 0,  Q.is_true(x >= 0)) == True
    assert refine(x >= 0,  Q.is_true(y >= 0)) == (x >= 0)
    assert refine(Eq(x, 0), ~Q.is_true(Eq(x, 0))) == False
    assert refine(Eq(x, 0),  Q.is_true(Eq(x, 0))) == True
    assert refine(Eq(x, 0),  Q.is_true(Eq(y, 0))) == Eq(x, 0)
    assert refine(Ne(x, 0), ~Q.is_true(Ne(x, 0))) == False
    assert refine(Ne(x, 0),  Q.is_true(Ne(x, 0))) == True
    assert refine(Ne(x, 0),  Q.is_true(Ne(y, 0))) == (Ne(x, 0))

def test_Piecewise():
   assert refine(Piecewise((1, x < 0), (3, True)),  Q.is_true(x < 0)) == 1
   assert refine(Piecewise((1, x < 0), (3, True)), ~Q.is_true(x < 0)) == 3
   assert refine(Piecewise((1, x < 0), (3, True)),  Q.is_true(y < 0)) == Piecewise((1, x < 0), (3, True))
   assert refine(Piecewise((1, x > 0), (3, True)),  Q.is_true(x > 0)) == 1
   assert refine(Piecewise((1, x > 0), (3, True)), ~Q.is_true(x > 0)) == 3
   assert refine(Piecewise((1, x > 0), (3, True)),  Q.is_true(y > 0)) == Piecewise((1, x > 0), (3, True))
   assert refine(Piecewise((1, x <= 0), (3, True)),  Q.is_true(x <= 0)) == 1
   assert refine(Piecewise((1, x <= 0), (3, True)), ~Q.is_true(x <= 0)) == 3
   assert refine(Piecewise((1, x <= 0), (3, True)),  Q.is_true(y <= 0)) == Piecewise((1, x <= 0), (3, True))
   assert refine(Piecewise((1, x >= 0), (3, True)),  Q.is_true(x >= 0)) == 1
   assert refine(Piecewise((1, x >= 0), (3, True)), ~Q.is_true(x >= 0)) == 3
   assert refine(Piecewise((1, x >= 0), (3, True)),  Q.is_true(y >= 0)) == Piecewise((1, x >= 0), (3, True))
   assert refine(Piecewise((1, Eq(x, 0)), (3, True)),  Q.is_true(Eq(x, 0))) == 1
   assert refine(Piecewise((1, Eq(x, 0)), (3, True)), ~Q.is_true(Eq(x, 0))) == 3
   assert refine(Piecewise((1, Eq(x, 0)), (3, True)),  Q.is_true(Eq(y, 0))) == Piecewise((1, Eq(x, 0)), (3, True))
   assert refine(Piecewise((1, Ne(x, 0)), (3, True)),  Q.is_true(Ne(x, 0))) == 1
   assert refine(Piecewise((1, Ne(x, 0)), (3, True)), ~Q.is_true(Ne(x, 0))) == 3
   assert refine(Piecewise((1, Ne(x, 0)), (3, True)),  Q.is_true(Ne(y, 0))) == Piecewise((1, Ne(x, 0)), (3, True))

def test_func_args():
    class MyClass(Expr):
        # A class with nontrivial .func

        def __init__(self, *args):
            self.my_member = ""

        @property
        def func(self):
            def my_func(*args):
                obj = MyClass(*args)
                obj.my_member = self.my_member
                return obj
            return my_func

    x = MyClass()
    x.my_member = "A very important value"
    assert x.my_member == refine(x).my_member

max_stars_repo_name: eriknw/sympy

reason: from sympy


text:
import os
import random
import time
from abc import abstractmethod, ABCMeta

import numpy as np

from .utils import show_img, get_pallete, get_index_list, save_img

class BaseVis(metaclass=ABCMeta):
    """The base class of visualization.

    All subclasses should implement the following APIs:

    - ``img_process()``

    Args:
        cfg (list or tuple): The data may used in this task.
        gt (dict): Gt data for visualization.
        pred (dict): Pred data for visualization.
        colors (dict): Colors for visualization.
        extension (str): Image extension. Default: 'jpg'.
    """

    def __init__(self,
                 cfg,
                 gt=None,
                 pred=None,
                 colors=None,
                 extension='jpg',
                 ):
        super(BaseVis, self).__init__()
        self.cfg = cfg
        self.data = dict(gt=gt, pred=pred)
        self.extension = extension
        self.default_title = dict(ori='ori_img',
                                  gt='img_with_gt',
                                  pred='img_with_pred')

        for key in list(self.data.keys()):
            if self.data[key] is not None:
                attr = self.data[key]
                self.set('img_names', attr.get('img_names'))
                self.set('categories', attr.get('categories'))
                for k in list(attr.keys()):
                    if k not in self.cfg:
                        attr.pop(k)
            else:
                self.data.pop(key)

        self.check()

        self._colors = get_pallete(
            self.categories) if colors is None else colors
        self.fnames = list(map(self._abs_path, self.img_names.tolist()))

    @staticmethod
    def _abs_path(path):
        return os.path.abspath(path)

    @property
    def colors(self):
        return self._colors

    @colors.setter
    def colors(self, v):
        self._colors.update(v)

    def check(self):
        """check attributes."""

        assert hasattr(self, 'img_names'), \
            "Keys of input data should contain 'img_names'."
        assert hasattr(self, 'categories'), \
            "Keys of input data should contain 'categories.'"
        assert self.data, \
            "Keys of input data should contain at least one item in 'gt' or 'pred'."

    def set(self, name, value):
        """Set the named attribute on the given object to the specified value."""

        if not hasattr(self, name):
            return setattr(self, name, value)

    def show(self,
             save_folder=None,
             specified_imgs=None,
             shuffle=False,
             **kwargs):
        """Display data dynamically.

        The function supports interaction,
        press key 's' to save the current picture,
        press key 'b' to go back to the previous picture, cache latest 32 images,
        press key 'q' to quit browse mode, press rest keys to go into next image.

        Args:
            save_folder (str, optional): Folder path where the images may store.
            specified_imgs: (str, optional): Specified visualization to show, folder or txt file.
            shuffle: (bool, optional): Shuffle the dataset. default: False.
        """

        if save_folder is None:
            save_folder = f"./{time.strftime('%Y-%m-%d-%H-%M-%S')}"
        os.makedirs(save_folder, exist_ok=True)

        index_list = get_index_list(specified_imgs, self.fnames, self.extension)

        if shuffle:
            random.seed(1)
            random.shuffle(index_list)

        current_show = -1
        key = -1
        while len(index_list):
            fname = index_list[0]
            img, flag = self.img_process(fname, **kwargs)
            if flag:
                key = show_img(img)
                if key == ord('q'):
                    break
                elif key == ord('s'):
                    if save_folder:
                        os.makedirs(save_folder, exist_ok=True)
                        save_path = os.path.join(save_folder,
                                                 os.path.split(fname)[-1])
                        save_img(save_path, img)
                    index_list.append(index_list.popleft())
                elif key == ord('b'):
                    if current_show >= 0:
                        index_list.appendleft(index_list.pop())
                        current_show -= 2
                    else:
                        print("We now at the start point and can't show previous image.")
                        continue
                else:
                    index_list.append(index_list.popleft())
                current_show += 1
                if current_show >= len(index_list) - 1:
                    print('Exit! All images have been shown.')
                    break
            else:
                print('ignore image, skip!')
                if key == ord('b'):
                    index_list.appendleft(index_list.pop())
                    current_show -= 1
                else:
                    index_list.append(index_list.popleft())
                    current_show += 1
                if current_show >= len(index_list) - 1:
                    break
                continue

    def save(self, save_folder=None, specified_imgs=None, **kwargs):
        """Save all data in a specified folder.

        Args:
            save_folder (str): Folder path where the images store.
            specified_imgs: (str, optional): Specified visualization to show, folder or txt file.
        """

        assert save_folder is not None, "save_folder shouldn't be None"
        os.makedirs(save_folder, exist_ok=True)

        index_list = get_index_list(specified_imgs, self.fnames, self.extension)

        for index, fname in enumerate(index_list):
            print(f'idx:{index + 1}, fname:{fname.split("/")[-1]}')
            img, flag = self.img_process(fname, **kwargs)
            if flag:
                save_path = os.path.join(save_folder, os.path.split(fname)[-1])
                save_img(save_path, img)
        print('All images are save!')

    def show_single_image(self, fname, save_folder=None, **kwargs):
        """Show a single image and save it in a specified folder.

        Args:
            fname (str): Path of image.
            save_folder: Folder path where the image may store.
        """
        fname = self._abs_path(fname)
        assert fname in self.fnames, f'{fname} should be in dataset.'

        img, _ = self.img_process(fname, **kwargs)
        show_img(img)

        if save_folder is not None:
            os.makedirs(save_folder, exist_ok=True)
            save_path = os.path.join(save_folder, os.path.split(fname)[-1])
            save_img(save_path, img)

    @abstractmethod
    def img_process(self, fname, show_ori=False, **kwargs):
        """Image process.

        Args:
            fname (str): Path of image.
            show_ori (bool): Whether show original image. Default: False.
        """

        pass

    def get_single_data(self, fname, category_to_show):
        """Get single image's annotation.

        Args:
            fname (str): Path of image.
            category_to_show (tuple or None): Categories need to be displayed.

        Returns:
            dict: Image's annotation.
        """

        data = {}
        labels = []
        for key, value in self.data.items():
            if fname in self.fnames:
                index = self.fnames.index(fname)
                anno = {k: v[index] for k, v in value.items() if v is not None}
                if isinstance(anno['labels'], np.ndarray):
                    labels.extend(anno['labels'].tolist())
                else:
                    labels.append(anno['labels'])
            else:
                anno = {}
            data.update({key: anno})
        labels = [self.categories[l] for l in labels]

        if category_to_show is not None and not (set(labels) & set(category_to_show)):
            flag = False
        else:
            flag = True

        return data, labels, flag

max_stars_repo_name: YuxinZou/volkscv

max_issues_repo_name: ChaseMonsterAway/volkscv

max_forks_repo_name: ChaseMonsterAway/volkscv


text:
import numpy as np
from arqtic.program import Program, Gate

def heisenberg_evolution_program(sim_obj, evol_time): #creates evolution program
    N = sim_obj.num_spins
    dt = sim_obj.delta_t
    H_BAR = sim_obj.H_BAR
    prop_steps = int(evol_time/dt)
    P = Program(N)
    if (len(sim_obj.Jx) > 0):
        theta_Jx = 2.0*sim_obj.Jx*dt/H_BAR
    if (len(sim_obj.Jy) > 0):
        theta_Jy = 2.0*sim_obj.Jy*dt/H_BAR
    if (len(sim_obj.Jz) > 0):
        theta_Jz = 2.0*sim_obj.Jz*dt/H_BAR
    if (len(sim_obj.hx) > 0):
        theta_hx = 2.0*sim_obj.hx*dt/H_BAR
    if (len(sim_obj.hy) > 0):
        theta_hy = 2.0*sim_obj.hy*dt/H_BAR
    if (len(sim_obj.hz) > 0):
        theta_hz = 2.0*sim_obj.hz*dt/H_BAR

    for step in range(prop_steps):
        #for time-dependent Hamiltonians
        if (sim_obj.time_dep_flag == "True"):
            t = (step + 0.5) * dt
            if (sim_obj.custom_time_dep == "True"):
                ###needs to be implemented###
                raise Error(f'Custom time-dependence not yet implemented. Use sin or cos.')
            else:
                if (len(sim_obj.td_Jx_func) > 0):
                    func_name = sim_obj.td_Jx_func[0]
                    freq = sim_obj.td_Jx_func[1]
                    if (func_name == "sin"):
                        theta_Jx = 2.0*sim_obj.Jx*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_Jx = 2.0*sim_obj.Jx*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')
                if (len(sim_obj.td_Jy_func) > 0):
                    func_name = sim_obj.td_Jy_func[0]
                    freq = sim_obj.td_Jy_func[1]
                    if (func_name == "sin"):
                        theta_Jy = 2.0*sim_obj.Jy*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_Jy = 2.0*sim_obj.Jy*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')
                if (len(sim_obj.td_Jz_func) > 0):
                    func_name = sim_obj.td_Jz_func[0]
                    freq = sim_obj.td_Jz_func[1]
                    if (func_name == "sin"):
                        theta_Jz = 2.0*sim_obj.Jz*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_Jz = 2.0*sim_obj.Jz*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')
                if (len(sim_obj.td_hx_func) > 0):
                    func_name = sim_obj.td_hx_func[0]
                    freq = sim_obj.td_hx_func[1]
                    if (func_name == "sin"):
                        theta_hx = 2.0*sim_obj.hx*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_hx = 2.0*sim_obj.hx*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')
                if (len(sim_obj.td_hy_func) > 0):
                    func_name = sim_obj.td_hy_func[0]
                    freq = sim_obj.td_hy_func[1]
                    if (func_name == "sin"):
                        theta_hy = 2.0*sim_obj.hy*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_hy = 2.0*sim_obj.hy*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')
                if (len(sim_obj.td_hz_func) > 0):
                    func_name = sim_obj.td_hz_func[0]
                    freq = sim_obj.td_hz_func[1]
                    if (func_name == "sin"):
                        theta_hz = 2.0*sim_obj.hz*np.sin(2*np.pi*freq*t)*dt/H_BAR
                    elif (func_name == "cos"):
                        theta_hz = 2.0*sim_obj.hz*np.cos(2*np.pi*freq*t)*dt/H_BAR
                    else:
                        raise Error(f'Unknown time-dependent function for Jx: {func_name}')

        #add coupling term instruction sets
        if (len(sim_obj.Jx) >0):
            for q in range(N-1):
                Jx_instr_set=[]
                Jx_instr_set.append(Gate([q], 'H'))
                Jx_instr_set.append(Gate([q+1], 'H'))
                Jx_instr_set.append(Gate([q, q+1], 'CNOT'))
                Jx_instr_set.append(Gate([q+1], 'RZ', angles=[theta_Jx[q]]))
                Jx_instr_set.append(Gate([q, q+1], 'CNOT'))
                Jx_instr_set.append(Gate([q], 'H',))
                Jx_instr_set.append(Gate([q+1], 'H',))
                P.add_instr(Jx_instr_set)
        if (len(sim_obj.Jy) >0):
            for q in range(N-1):
                Jy_instr_set=[]
                Jy_instr_set.append(Gate([q],'RX',angles=[-np.pi/2]))
                Jy_instr_set.append(Gate([q+1],'RX',angles=[-np.pi/2]))
                Jy_instr_set.append(Gate([q, q+1],'CNOT'))
                Jy_instr_set.append(Gate([q+1], 'RZ', angles=[theta_Jy[q]]))
                Jy_instr_set.append(Gate([q, q+1], 'CNOT'))
                Jy_instr_set.append(Gate([q],'RX',angles=[np.pi/2]))
                Jy_instr_set.append(Gate([q+1],'RX',angles=[np.pi/2]))
                P.add_instr(Jy_instr_set)
        if (len(sim_obj.Jz) >0):
            for q in range(N-1):
                Jz_instr_set=[]
                Jz_instr_set.append(Gate([q, q+1], 'CNOT'))
                Jz_instr_set.append(Gate([q+1], 'RZ', angles=[theta_Jz[q]]))
                Jz_instr_set.append(Gate([q, q+1], 'CNOT'))
                P.add_instr(Jz_instr_set)

        #add external magnetic field instruction sets
        if (len(sim_obj.hx) > 0):
            for q in range(N):
                hx_instr_set = [Gate([q], 'RX', angles=[theta_hx[q]])]
                P.add_instr(hx_instr_set)
        if (len(sim_obj.hy) > 0):
            for q in range(N):
                hy_instr_set = [Gate([q], 'RY', angles=[theta_hy[q]])]
                P.add_instr(hy_instr_set)
        if (len(sim_obj.hz) > 0):
            for q in range(N):
                hz_instr_set = [Gate([q], 'RZ', angles=[theta_hz[q]])]
                P.add_instr(hz_instr_set)

    return P

max_stars_repo_name: lebassman/ArQTiC

max_issues_repo_name: lebassman/ArQTiC

max_forks_repo_name: lebassman/ArQTiC


text:
"""Metrics to assess performance on ite prediction task."""
from typing import Optional

import numpy as np
import pandas as pd

def expected_response(y: np.ndarray, w: np.ndarray, policy: np.ndarray,
                      mu: Optional[np.ndarray]=None, ps: Optional[np.ndarray]=None) -> float:
    """Estimate expected response.

    Parameters
    ----------
    y: array-like of shape = (n_samples)
        Observed target values.

    w: array-like of shape = shape = (n_samples)
        Treatment assignment variables.

    policy: array-like of shape = (n_samples)
        Estimated treatment policy.

    mu: array-like of shape = (n_samples, n_trts), optional
        Estimated potential outcomes.

    ps: array-like of shape = (n_samples, n_trts), optional
        Estimated propensity scores.

    Returns
    -------
    expected_response: float
        Estimated expected_response.

    """
    mu = np.zeros((w.shape[0], np.unique(w).shape[0])) if mu is None else mu
    ps = pd.get_dummies(w).mean(axis=0).values if ps is None else ps
    indicator = np.array(w == policy, dtype=int)
    expected_response = np.mean(mu[np.arange(w.shape[0]), policy]
                                + (y - mu[np.arange(w.shape[0]), policy]) * indicator / ps[w])

    return expected_response

def ips_value(y: np.ndarray, w: np.ndarray, policy: np.ndarray, ps: Optional[np.ndarray]=None) -> float:
    """Decision Value Estimator based on Inverse Propensity Score Weighting method.

    Parameters
    ----------
    y: array-like of shape = (n_samples)
        Observed target values.

    w: array-like of shape = shape = (n_samples)
        Treatment assignment indicators.

    policy: array-like of shape = (n_samples)
        Estimated decision model.

    ps: array-like of shape = (n_samples), optional
        Estimated propensity scores.

    Returns
    -------
    decision_value: float
        Estimated decision value using Inverse Propensity Score Weighting method.

    References
    ----------
    [1] Y. Zhao, X. Fang, D. S. Levi: Uplift modeling with multiple treatments and general response types, 2017.
    [2] A. Schuler, M. Baiocchi, R. Tibshirani, N. Shah: A comparison of methods for model selection when estimating individual treatment effects, 2018.

    """
    if not isinstance(y, np.ndarray):
        raise TypeError("y must be a numpy.ndarray.")
    if not isinstance(w, np.ndarray):
        raise TypeError("w must be a numpy.ndarray.")
    if not isinstance(policy, np.ndarray):
        raise TypeError("policy must be a numpy.ndarray.")
    if ps is None:
        trts_probs = pd.get_dummies(w).mean(axis=0).values
        ps = np.ones((w.shape[0], np.unique(w).shape[0])) * np.expand_dims(trts_probs, axis=0)
    else:
        assert (np.max(ps) < 1) and (np.min(ps) > 0), "ps must be strictly between 0 and 1."

    treatment_matrix = pd.get_dummies(w).values
    if np.unique(policy).shape[0] == np.unique(w).shape[0]:
        policy = pd.get_dummies(policy).values
    else:
        diff = np.setdiff1d(np.unique(w), np.unique(policy))
        policy = pd.get_dummies(policy).values
        for _diff in diff:
            policy = np.insert(policy, _diff, 0, axis=1)
    indicator_matrix = policy * treatment_matrix
    outcome_matrix = np.expand_dims(y, axis=1) * treatment_matrix
    decision_value = np.mean(np.sum(indicator_matrix * (outcome_matrix / ps), axis=1))
    return decision_value

def dr_value(y: np.ndarray, w: np.ndarray, policy: np.ndarray,
             mu: np.ndarray, ps: Optional[np.ndarray]=None) -> float:
    """Decision Value Estimator based on Doubly Robust method.

    Parameters
    ----------
    y: array-like of shape = (n_samples)
        Observed target values.

    w: array-like of shape = (n_samples)
        Treatment assignment indicators.

    policy: array-like of shape = (n_samples)
        Estimated decision model.

    ps: array-like of shape = (n_samples)
        Estimated propensity scores.

    mu: array-like of shape = (n_samples, n_treatments), optional
        Estimated potential outcome for each treatment.

    Returns
    -------
    decision_value: float
        Estimated decision value using Doubly Robust method.

    References
    ----------
    [1] A. Schuler, M. Baiocchi, R. Tibshirani, N. Shah: A comparison of methods for model selection when estimating individual treatment effects, 2018.

    """
    if not isinstance(y, np.ndarray):
        raise TypeError("y must be a numpy.ndarray.")
    if not isinstance(w, np.ndarray):
        raise TypeError("w must be a numpy.ndarray.")
    if not isinstance(policy, np.ndarray):
        raise TypeError("policy must be a numpy.ndarray.")
    if not isinstance(mu, np.ndarray):
        raise TypeError("mu must be a numpy.ndarray.")
    if ps is None:
        trts_probs = pd.get_dummies(w).mean(axis=0).values
        ps = np.ones((w.shape[0], np.unique(w).shape[0])) * np.expand_dims(trts_probs, axis=0)
    else:
        assert (np.max(ps) < 1) and (np.min(ps) > 0), "ps must be strictly between 0 and 1."

    treatment_matrix = pd.get_dummies(w).values
    policy = pd.get_dummies(policy).values
    diff = np.setdiff1d(np.unique(w), np.unique(policy))
    for _diff in diff:
        policy = np.insert(policy, _diff, 0, axis=1)
    indicator_matrix = policy * treatment_matrix
    outcome_matrix = np.expand_dims(y, axis=1) * treatment_matrix
    decision_value = np.mean(np.sum(treatment_matrix * mu + indicator_matrix * (outcome_matrix - mu) / ps, axis=1))
    return decision_value

max_stars_repo_name: smn-ailab/lift-learn

max_issues_repo_name: smn-ailab/lift-learn

max_forks_repo_name: smn-ailab/lift-learn


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

import pandas as pd
pd.set_option("display.max_rows", 5)
pd.set_option("display.max_columns", 6)
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('barchart.pdf')
print("Setup Complete")

sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
print(tips.head())
plot = sns.barplot(x="day", y="total_bill", data=tips)
plot.set_title("Fig 1")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
plot.set_title("Fig 2")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="tip", y="day", data=tips)
plot.set_title("Fig 3")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="time", y="tip", data=tips,
                 order=["Dinner", "Lunch"])
plot.set_title("Fig 4")
pp.savefig()
plot.get_figure().clf()

from numpy import median
plot = sns.barplot(x="day", y="tip", data=tips, estimator=median)
plot.set_title("Fig 5")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="day", y="tip", data=tips, ci=68)
plot.set_title("Fig 6")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="day", y="tip", data=tips, ci="sd")
plot.set_title("Fig 6")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="day", y="tip", data=tips, capsize=.2)
plot.set_title("Fig 7")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="size", y="total_bill", data=tips,
                 palette="Blues_d")
plot.set_title("Fig 8")
pp.savefig()
plot.get_figure().clf()

tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
plot = sns.barplot(x="day", y="total_bill", hue="weekend",
                 data=tips, dodge=False)
plot.set_title("Fig 9")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="size", y="total_bill", data=tips,
                 color="salmon", saturation=.5)
plot.set_title("Fig 10")
pp.savefig()
plot.get_figure().clf()

plot = sns.barplot(x="day", y="total_bill", data=tips,
                 linewidth=2.5, facecolor=(1, 1, 1, 0),
                 errcolor=".2", edgecolor=".2")
plot.set_title("Fig 11")
pp.savefig()
plot.get_figure().clf()

g = sns.catplot(x="sex", y="total_bill",
                hue="smoker", col="time",
                data=tips, kind="bar",
                height=4, aspect=.7);
g.fig.suptitle("Fig 12")
pp.savefig()

pp.close()

reason: from numpy


text:
'''
 TODO
 0. Central agent baseline
 1. Implement shared relational encoder function (basically graph net / self-attention)
 1.a. Baseline: shared MLP action decoder function
 1.b. Skill Pool: action decoders indexed by key (basically a non-parametric mixture of experts)
        Try different gating strategies

'''

import numpy as np
import torch

from rlpyt.utils.tensor import infer_leading_dims, restore_leading_dims
from rlpyt.models.mlp import MlpModel

class MultiFfModel(torch.nn.Module):
    def __init__(
            self,
            observation_shape,
            action_size,
            hidden_sizes=None,  # None for default (see below).
            hidden_nonlinearity=torch.nn.Tanh,  # Module form.
            mu_nonlinearity=torch.nn.Tanh,  # Module form.
            init_log_std=0.,
            pooling="average",
            ):
        super().__init__()
        self._obs_ndim = len(observation_shape)
        self._n_pop = observation_shape[0]

        input_size = int(observation_shape[-1])
        output_size = int(action_size[-1])
        hidden_sizes = hidden_sizes or [64]

        # import pdb; pdb.set_trace()
        self.encoder = MlpModel(
                input_size=input_size,
                hidden_sizes=hidden_sizes*2,
                output_size=None,
                nonlinearity=hidden_nonlinearity
        )

        self.pooling = pooling

        input_size = 64
        if self.pooling is not None:
            input_size *=2

        mu_mlp = MlpModel(
            input_size=input_size,
            hidden_sizes=hidden_sizes,
            output_size=output_size,
            nonlinearity=hidden_nonlinearity,
        )

        if mu_nonlinearity is not None:
            self.mu = torch.nn.Sequential(mu_mlp, mu_nonlinearity())
        else:
            self.mu = mu_mlp

        self.v = MlpModel(
            input_size=input_size,
            hidden_sizes=hidden_sizes,
            output_size=1,
            nonlinearity=hidden_nonlinearity,
        )
        self.log_std = torch.nn.Parameter(init_log_std * torch.ones(action_size))

    def forward(self, observation, prev_action, prev_reward):
        """Feedforward layers process as [T*B,H]. Return same leading dims as
        input, can be [T,B], [B], or []."""

        observation = self.encoder(observation)

        if self.pooling is not None:
            if self.pooling == 'average':
                pooled = observation.mean(-2)

            elif self.pooling == 'max':
                pooled = observation.max(-2)

            pooled = pooled.unsqueeze(-2).expand_as(observation)
            observation = torch.cat([observation, pooled], dim=-1)

        # Infer (presence of) leading dimensions: [T,B], [B], or [].
        lead_dim, T, B, _ = infer_leading_dims(observation, self._obs_ndim)
        obs_flat = observation.view(T * B * self._n_pop, -1)

        mu = self.mu(obs_flat)
        v = self.v(obs_flat).squeeze(-1)
        log_std = self.log_std.repeat(T * B, 1)

        mu = mu.view(T * B, self._n_pop, -1)
        v = v.view(T * B, self._n_pop)
        log_std = log_std.view(T * B, self._n_pop, -1)

        # Restore leading dimensions: [T,B], [B], or [], as input.
        mu, log_std, v = restore_leading_dims((mu, log_std, v), lead_dim, T, B)

        return mu, log_std, v

max_stars_repo_name: ajabri/rlpyt

max_issues_repo_name: ajabri/rlpyt

max_forks_repo_name: ajabri/rlpyt


text:
import csv
import os
import networkx as nx

def _positive(value):
    return abs(int(value))

NODE_ID = 'ID'
X_AXIS = 'x_axis'
Y_AXIS = 'y_axis'
QUANTITY = 'quantity'
PAIR = 'pair'
NODE_TYPE = 'type'
CONSOLIDATION = 'consolidation'
VEHICLE = 'vehicle'

COLUMN_NAMES = (NODE_ID, X_AXIS, Y_AXIS, QUANTITY, PAIR)
COLUMN_TYPES = {
    NODE_ID: str,
    X_AXIS: int,
    Y_AXIS: int,
    QUANTITY: _positive,
    PAIR: str,
}
CROSS_DOCK = 0
SUPPLIER = 1
CUSTOMER = 2

def get_consolidation(nu_pairs):
    return {i: [0, 0] for i in range(nu_pairs)}

def get_node_type(quantity):
    quantity = int(quantity)
    if quantity == 0:
        return CROSS_DOCK
    elif quantity > 0:
        return SUPPLIER
    else:
        return CUSTOMER

def validate_data(data):
    parsed_data = []
    for i, column_name in enumerate(COLUMN_NAMES):
        column_value = data[i]
        column_type = COLUMN_TYPES.get(column_name)
        try:
            parsed_data.append(column_type(column_value))
        except TypeError:
            msg = 'Value of {!r} is not {!r} ({!r}).'
            raise TypeError(msg.format(column_value, column_type,
                                       column_value))
    return parsed_data

def _get_node_kwargs(node_type, node_data, nu_pairs):
    kwargs = {COLUMN_NAMES[i + 1]: node_attr
              for i, node_attr in enumerate(node_data)}
    kwargs.update({NODE_TYPE: node_type})
    if node_type == CROSS_DOCK:
        kwargs.update(
            {CONSOLIDATION: get_consolidation(nu_pairs)})
    return kwargs

def load_data(path, delimiter=' '):
    if not os.path.isfile(path):
        msg = 'Given path {!r} is not a file'
        raise IOError(msg.format(path))
    G = nx.DiGraph()
    with open(path, 'r') as fil:
        nodes = list(csv.reader(fil, delimiter=delimiter))
        assert len(nodes) % 2, (
            'The number of nodes must be an odd number. There must be a pair'
            ' of suppliers and customers and a cross dock.'
            ' Found: {!s}'.format(len(nodes)))
        for i, row in enumerate(nodes):
            assert len(row) == len(COLUMN_NAMES), (
                'Row {!r} is incomplete. Missing node data.'.format(i + 1))
            data = validate_data(row)
            node, node_data = data[0], data[1:]
            node_type = get_node_type(row[-2])
            nu_pairs = (len(nodes) - 1) / 2
            node_kwargs = _get_node_kwargs(node_type, node_data, nu_pairs)
            G.add_node(node, **node_kwargs)
    return G

reason: import networkx


text:
from comet_ml import Experiment
import sys
import os
import pickle
from collections import Counter
from tqdm import tqdm
import argparse

import numpy as np
import torch
from torch import nn
from torch import optim

from dataset import get_dataloader
from utils import load_pretrained_embedings
from model import MACNetwork
from config import get_default_cfg, config_to_comet

ON_SLURM = "SLURM_JOBID" in os.environ

device = None

def accumulate(model1, model2, decay=0.999):
    par1 = dict(model1.named_parameters())
    par2 = dict(model2.named_parameters())

    for k in par1.keys():
        par1[k].data.mul_(decay).add_(1 - decay, par2[k].data)

def train(epoch, train_dataloader):
    dataloader = iter(train_dataloader)
    if not ON_SLURM:
        pbar = tqdm(dataloader)
    else:
        pbar = dataloader
    moving_accuracy = None
    moving_steps = None

    net.train(True)
    for image, question, q_len, answer, _, _ in pbar:
        image, question, answer = (
            image.to(device),
            question.to(device),
            answer.to(device),
        )

        net.zero_grad()
        output, ponder_cost, steps = net(image, question, q_len)
        if cfg.MAC.USE_ACT:
            loss = criterion(output, answer) + \
                (cfg.ACT.PENALTY_COEF * torch.mean(ponder_cost))
        else:
            loss = criterion(output, answer)

        loss.backward()

        if cfg.SOLVER.GRAD_CLIP:
            nn.utils.clip_grad_norm_(net.parameters(), cfg.SOLVER.GRAD_CLIP)
        optimizer.step()

        # train metrics
        correct = output.detach().argmax(1) == answer
        accuracy = correct.float().mean().item()
        steps = torch.mean(steps).item()
        if moving_accuracy is None:
            moving_accuracy = accuracy
            moving_steps = steps
        else:
            moving_accuracy = moving_accuracy * 0.99 + accuracy * 0.01
            moving_steps = moving_steps * 0.99 + steps * 0.01

        if not ON_SLURM:
            pbar.set_description(
                'Epoch: {}; Loss: {:.5f}; Acc: {:.5f}'.format(
                    epoch, loss.item(), moving_accuracy
                )
            )

        accumulate(net_running, net)

    experiment.log_metrics({
        "train_precision": moving_accuracy,
        "train_steps": moving_steps,
        })

def valid(epoch, val_dataloader):
    dataloader = iter(val_dataloader)
    if not ON_SLURM:
        pbar = tqdm(dataloader)
    else:
        pbar = dataloader

    net_running.train(False)
    with torch.no_grad():
        all_corrects = 0
        acc_steps = 0

        for i, (image, question, q_len, answer, _, _) in enumerate(pbar):
            image, question = image.to(device), question.to(device)

            output, ponder_cost, steps  = net_running(image, question, q_len)
            correct = output.detach().argmax(1) == answer.to(device)

            all_corrects += correct.float().mean().item()
            acc_steps += torch.mean(steps).item()

            if not ON_SLURM:
                pbar.set_description(
                    'Val Epoch: {}; Acc: {:.5f}; Steps: {:.2f}'.format(
                        epoch, all_corrects / (i + 1), acc_steps / (i + 1)
                    )
                )

        experiment.log_metrics({
                "valid_precision": all_corrects / len(dataloader),
                "valid_steps": acc_steps / len(dataloader),
                })

        experiment.log_epoch_end(epoch)

        if scheduler:
            scheduler.step(all_corrects / len(dataloader))

def generate_cfg():
    parser = argparse.ArgumentParser(description="Train for DACT-Mac")
    parser.add_argument(
        "--mode",
        default="clevr",
        choices=["clevr", "gqa"],
        help="whether to use clevr or gqa default parameters",
    )
    parser.add_argument(
        "--config-file",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )
    args = parser.parse_args()

    # get default configuration params
    cfg = get_default_cfg(mode=args.mode)

    # update config with configuration file
    if args.config_file:
        cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    return cfg, args.mode

if __name__ == '__main__':
    cfg, mode = generate_cfg()
    device = torch.device(cfg.DEVICE if torch.cuda.is_available() else "cpu")
    print("using {}".format(device))

    experiment = Experiment(
        api_key = os.environ["COMET_KEY"],
        project_name=cfg.COMET.PROJECT_NAME,
        workspace = os.environ["COMET_USER"],
    )

    experiment.log_parameters(config_to_comet(cfg))
    experiment.add_tag("NEW_MAC")
    if cfg.COMET.EXPERIMENT_NAME:
        experiment.set_name(cfg.COMET.EXPERIMENT_NAME)

    net = MACNetwork(cfg).to(device)
    if cfg.MAC.TRAINED_EMBD_PATH:
        load_pretrained_embedings(cfg, net.embed, device)
    net_running = MACNetwork(cfg).to(device)

    if cfg.LOAD:
        with open(cfg.LOAD_PATH, 'rb') as f:
            state = torch.load(f, map_location=device)
        net.load_state_dict(state, strict=False)
    accumulate(net_running, net, 0)

    if cfg.MAC.USE_ACT and cfg.ACT.SMOOTH:
        _criterion = nn.NLLLoss()
        criterion = lambda x, y: _criterion(torch.log(x.clamp(min=1e-8)), y)
    else:
        criterion = nn.CrossEntropyLoss()

    if cfg.SOLVER.GATE_LR:
        filtered = ['actmac.gate.weight', 'actmac.gate.bias']
        gate_params = map(lambda kv: kv[1], (filter(lambda kv: kv[0] in filtered,
                    net.named_parameters())))
        base_params = map(lambda kv: kv[1], (filter(lambda kv: kv[0] not in filtered,
                    net.named_parameters())))
        optimizer = optim.Adam([{
            'params': base_params
        }, {
            'params': gate_params,
            'lr': cfg.SOLVER.GATE_LR
        }],
        lr=cfg.SOLVER.LR)
    else:
        optimizer = optim.Adam(net.parameters(), lr=cfg.SOLVER.LR)

    # LR scheduler
    scheduler = None
    if cfg.SOLVER.USE_SCHEDULER:
        scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', factor=0.5, patience=0, threshold=0.001, threshold_mode='rel', verbose=True)

    train_dataloader, val_dataloader = get_dataloader(cfg, mode=mode)

    for epoch in range(1, cfg.SOLVER.EPOCHS + 1):
        train(epoch, train_dataloader)
        valid(epoch, val_dataloader)

        with open(
            '{}_{}.model'.format(
                cfg.SAVE_PATH,
                str(epoch).zfill(2)),
            'wb') as f:
            torch.save(net_running.state_dict(), f)


text:
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from random import sample
from sklearn.linear_model import LogisticRegression
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.cross_validation import cross_val_score
from sklearn import metrics
from IPython.display import Image
from pydotplus import graph_from_dot_data

# Set working directory and read data
path = os.path.expanduser('~/Projects/kaggle_HR/')
os.chdir(path)
# Read in the data
df = pd.read_csv('HR_comma_sep.csv')

np.random.seed(42)

# Explore the data set
col_names = df.columns.tolist()
df.shape
df.describe

# Prepend string prior to encoding
df.dtypes
df['salary'] = '$_' + df['salary'].astype(str)
df['sales'] = 'sales_' + df['sales'].astype(str)

# Create salary dummies and join
one_hot_salary = pd.get_dummies(df['salary'])
df = df.join(one_hot_salary)

# Create sales dummies and join
one_hot_sales = pd.get_dummies(df['sales'])
df = df.join(one_hot_sales)

# Drop unnecessay columns
df = df.drop(['salary', 'sales', '$_low', 'sales_IT'], axis=1)

# TODO interaction terms

# Some basic qtys
print '# people left = {}'.format(len(df[df['left'] == 1]))
print '# people stayed = {}'.format(len(df[df['left'] == 0]))
print '% people left = {}%'.format(round(float(len(df[df['left'] == 1])) / len(df) * 100), 3)

# Check missing for values (none)
df.apply(lambda x: sum(x.isnull()), axis=0)

# Correlation heatmap
correlation = df.corr()
plt.figure(figsize=(10, 10))
sns.heatmap(correlation, vmax=1, square=True, annot=True, cmap='cubehelix')

# Pairwise plots (take a 5% sample as CPU intensive)
df_sample = df.sample(frac=0.05)
pplot = sns.pairplot(df_sample, hue="left")
pplot_v2 = sns.pairplot(df_sample, diag_kind="kde")

# TODO check for outliers

# Split data into train, test and validate
# 60% - train set, 20% - validation set, 20% - test set
train, test, validate = np.split(df.sample(frac=1), [int(.6*len(df)), int(.8*len(df))])
print train.shape, test.shape, validate.shape

# Separate target and predictors
y_train = train['left']
x_train = train.drop(['left'], axis=1)
y_test = test['left']
x_test = test.drop(['left'], axis=1)
y_validate = validate['left']
x_validate = validate.drop(['left'], axis=1)

# Variable importance / selection
rf = RandomForestClassifier()
rf.fit(x_train, y_train)
print "Features sorted by their score:"
print sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), x_train), reverse=True)

# Assign feature importance and sort
importances = rf.feature_importances_
std = np.std([rf.feature_importances_ for tree in rf.estimators_], axis=0)
indices = np.argsort(importances)[::-1]

# Plot variable importance
plt.figure()
plt.title("Feature importance")
plt.bar(range(x_train.shape[1]), importances[indices], color="r", yerr=std[indices], align="center")

# Create variable lists and drop
all_vars = x_train.columns.tolist()
top_5_vars = ['satisfaction_level', 'number_project', 'time_spend_company',
              'average_montly_hours', 'last_evaluation']
bottom_vars = [cols for cols in all_vars if cols not in top_5_vars]

# Drop less important variables leaving the top_5
x_train    = x_train.drop(bottom_vars, axis=1)
x_test     = x_test.drop(bottom_vars, axis=1)
x_validate = x_validate.drop(bottom_vars, axis=1)

# LOGISTIC REGRESSION
# Instantiate, fit and obtain accuracy score
logit_model = LogisticRegression()
logit_model = logit_model.fit(x_train, y_train)
logit_model.score(x_train, y_train)

# Examine the coefficients
pd.DataFrame(zip(x_train.columns, np.transpose(logit_model.coef_)))

# Predictions on the test dataset
predicted = pd.DataFrame(logit_model.predict(x_test))
print predicted.head(n=15)

# Probabilities on the test dataset
probs = pd.DataFrame(logit_model.predict_proba(x_test))
print probs.head(n=15)

# Store metrics
logit_accuracy = metrics.accuracy_score(y_test, predicted)
logit_roc_auc = metrics.roc_auc_score(y_test, probs[1])
logit_confus_matrix = metrics.confusion_matrix(y_test, predicted)
logit_classification_report = metrics.classification_report(y_test, predicted)
logit_precision = metrics.precision_score(y_test, predicted, pos_label=1)
logit_recall = metrics.recall_score(y_test, predicted, pos_label=1)
logit_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
logit_cv_scores = cross_val_score(LogisticRegression(), x_test, y_test, scoring='precision', cv=10)
logit_cv_mean = np.mean(logit_cv_scores)

# DECISION TREE (pruned to depth of 3)
# TODO optimise depth

# Instantiate with a max depth of 3
tree_model = tree.DecisionTreeClassifier(max_depth=3)
# Fit a decision tree
tree_model = tree_model.fit(x_train, y_train)
# Training accuracy
tree_model.score(x_train, y_train)

# Predictions/probs on the test dataset
predicted = pd.DataFrame(tree_model.predict(x_test))
probs = pd.DataFrame(tree_model.predict_proba(x_test))

# Store metrics
tree_accuracy = metrics.accuracy_score(y_test, predicted)
tree_roc_auc = metrics.roc_auc_score(y_test, probs[1])
tree_confus_matrix = metrics.confusion_matrix(y_test, predicted)
tree_classification_report = metrics.classification_report(y_test, predicted)
tree_precision = metrics.precision_score(y_test, predicted, pos_label=1)
tree_recall = metrics.recall_score(y_test, predicted, pos_label=1)
tree_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
tree_cv_scores = cross_val_score(tree.DecisionTreeClassifier(max_depth=3),
                                x_test, y_test, scoring='precision', cv=10)
tree_cv_mean = np.mean(tree_cv_scores)

# Output decision plot
dot_data = tree.export_graphviz(tree_model, out_file=None,
                     feature_names=x_test.columns.tolist(),
                     class_names=['remain', 'left'],
                     filled=True, rounded=True,
                     special_characters=True)
graph = graph_from_dot_data(dot_data)
graph.write_png("images/decision_tree.png")

# RANDOM FOREST
# Instantiate
rf = RandomForestClassifier()
# Fit
rf_model = rf.fit(x_train, y_train)
# Training accuracy
rf_model.score(x_train, y_train)

# Predictions/probs on the test dataset
predicted = pd.DataFrame(rf_model.predict(x_test))
probs = pd.DataFrame(rf_model.predict_proba(x_test))

# Store metrics
rf_accuracy = metrics.accuracy_score(y_test, predicted)
rf_roc_auc = metrics.roc_auc_score(y_test, probs[1])
rf_confus_matrix = metrics.confusion_matrix(y_test, predicted)
rf_classification_report = metrics.classification_report(y_test, predicted)
rf_precision = metrics.precision_score(y_test, predicted, pos_label=1)
rf_recall = metrics.recall_score(y_test, predicted, pos_label=1)
rf_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
rf_cv_scores = cross_val_score(RandomForestClassifier(), x_test, y_test, scoring='precision', cv=10)
rf_cv_mean = np.mean(rf_cv_scores)

# SUPPORT VECTOR MACHINE
# Instantiate
svm_model = SVC(probability=True)
# Fit
svm_model = svm_model.fit(x_train, y_train)
# Accuracy
svm_model.score(x_train, y_train)

# Predictions/probs on the test dataset
predicted = pd.DataFrame(svm_model.predict(x_test))
probs = pd.DataFrame(svm_model.predict_proba(x_test))

# Store metrics
svm_accuracy = metrics.accuracy_score(y_test, predicted)
svm_roc_auc = metrics.roc_auc_score(y_test, probs[1])
svm_confus_matrix = metrics.confusion_matrix(y_test, predicted)
svm_classification_report = metrics.classification_report(y_test, predicted)
svm_precision = metrics.precision_score(y_test, predicted, pos_label=1)
svm_recall = metrics.recall_score(y_test, predicted, pos_label=1)
svm_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
svm_cv_scores = cross_val_score(SVC(probability=True), x_test, y_test, scoring='precision', cv=10)
svm_cv_mean = np.mean(svm_cv_scores)

# KNN
# Instantiate learning model (k = 3)
knn_model = KNeighborsClassifier(n_neighbors=3)
# Fit the model
knn_model.fit(x_train, y_train)
# Accuracy
knn_model.score(x_train, y_train)

# Predictions/probs on the test dataset
predicted = pd.DataFrame(knn_model.predict(x_test))
probs = pd.DataFrame(knn_model.predict_proba(x_test))

# Store metrics
knn_accuracy = metrics.accuracy_score(y_test, predicted)
knn_roc_auc = metrics.roc_auc_score(y_test, probs[1])
knn_confus_matrix = metrics.confusion_matrix(y_test, predicted)
knn_classification_report = metrics.classification_report(y_test, predicted)
knn_precision = metrics.precision_score(y_test, predicted, pos_label=1)
knn_recall = metrics.recall_score(y_test, predicted, pos_label=1)
knn_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
knn_cv_scores = cross_val_score(KNeighborsClassifier(n_neighbors=3), x_test, y_test, scoring='precision', cv=10)
knn_cv_mean = np.mean(knn_cv_scores)

# TWO CLASS BAYES
# Instantiate
bayes_model = GaussianNB()
# Fit the model
bayes_model.fit(x_train, y_train)
# Accuracy
bayes_model.score(x_train, y_train)

# Predictions/probs on the test dataset
predicted = pd.DataFrame(bayes_model.predict(x_test))
probs = pd.DataFrame(bayes_model.predict_proba(x_test))

# Store metrics
bayes_accuracy = metrics.accuracy_score(y_test, predicted)
bayes_roc_auc = metrics.roc_auc_score(y_test, probs[1])
bayes_confus_matrix = metrics.confusion_matrix(y_test, predicted)
bayes_classification_report = metrics.classification_report(y_test, predicted)
bayes_precision = metrics.precision_score(y_test, predicted, pos_label=1)
bayes_recall = metrics.recall_score(y_test, predicted, pos_label=1)
bayes_f1 = metrics.f1_score(y_test, predicted, pos_label=1)

# Evaluate the model using 10-fold cross-validation
bayes_cv_scores = cross_val_score(KNeighborsClassifier(n_neighbors=3), x_test, y_test, scoring='precision', cv=10)
bayes_cv_mean = np.mean(bayes_cv_scores)

# EVALUATE
# Model comparison
models = pd.DataFrame({
    'Model'    : ['Logistic Regression', 'Decision Tree', 'Random Forest', 'SVM', 'kNN', 'Bayes'],
    'Accuracy' : [logit_accuracy, tree_accuracy, rf_accuracy, svm_accuracy, knn_accuracy, bayes_accuracy],
    'Precision': [logit_precision, tree_precision, rf_precision, svm_precision, knn_precision, bayes_precision],
    'recall'   : [logit_recall, tree_recall, rf_recall, svm_recall, knn_recall, bayes_recall],
    'F1'       : [logit_f1, tree_f1, rf_f1, svm_f1, knn_f1, bayes_f1],
    'cv_precision' : [logit_cv_mean, tree_cv_mean, rf_cv_mean, svm_cv_mean, knn_cv_mean, bayes_cv_mean]
    })
models.sort_values(by='Precision', ascending=False)

# Create x and y from all data
y = df['left']
x = df.drop(['left'], axis=1)

# Re-train model on all data
rf_model = rf.fit(x, y)

# Save model
import cPickle
with open('churn_classifier.pkl', 'wb') as fid:
    cPickle.dump(rf_model, fid)


text:
import pyCompute

import numpy

#custom operation

def custom_op(a, kwargs) :
    #simply increments each operant by value 10
    return a + 10

#custom op can use passed kwargs
def custom_op2(a, kwargs) :

    if kwargs["increment"] :

        return a + 20
    else :

        return a

a = pyCompute.constant([1,2,3,4,5,6,7,8], name = "const_1")

#call the custom op, pass the function, name and operand
op = pyCompute.custom_op(custom_op, "op1", a)

#call 2nd op with kwargs :

op2 = pyCompute.custom_op(custom_op, "op2", op, increment = True)

session = pyCompute.SequentialSession()

op = session.run(pyCompute.default_graph, op2)

print(op)