text:
# Copyright (c) OpenMMLab. All rights reserved.
# Modified from jbwang1997: https://github.com/jbwang1997/OBBDetection
import mmcv
import numpy as np
import torch
from mmdet.core.bbox.coder.base_bbox_coder import BaseBBoxCoder

from ..builder import ROTATED_BBOX_CODERS
from ..transforms import obb2poly, obb2xyxy, poly2obb

@ROTATED_BBOX_CODERS.register_module()
class MidpointOffsetCoder(BaseBBoxCoder):
    """Mid point offset coder.

    This coder encodes bbox (x1, y1, x2, y2) into delta
        (dx, dy, dw, dh, da, db) and decodes delta (dx, dy, dw, dh, da, db)
        back to original bbox (x1, y1, x2, y2).
    Args:
        target_means (Sequence[float]): Denormalizing means of target for
            delta coordinates
        target_stds (Sequence[float]): Denormalizing standard deviation of
            target for delta coordinates
        angle_range (str, optional): Angle representations. Defaults to 'oc'.
    """

    def __init__(self,
                 target_means=(0., 0., 0., 0., 0., 0.),
                 target_stds=(1., 1., 1., 1., 1., 1.),
                 angle_range='oc'):
        super(BaseBBoxCoder, self).__init__()
        self.means = target_means
        self.stds = target_stds
        self.version = angle_range

    def encode(self, bboxes, gt_bboxes):
        """Get box regression transformation deltas that can be used to
        transform the ``bboxes`` into the ``gt_bboxes``.

        Args:
            bboxes (torch.Tensor): Source boxes, e.g., object proposals.
            gt_bboxes (torch.Tensor): Target of the transformation, e.g.,
                ground-truth boxes.

        Returns:
            torch.Tensor: Box transformation deltas
        """
        assert bboxes.size(0) == gt_bboxes.size(0)
        assert bboxes.size(-1) == 4
        assert gt_bboxes.size(-1) == 5
        encoded_bboxes = bbox2delta(bboxes, gt_bboxes, self.means, self.stds,
                                    self.version)
        return encoded_bboxes

    def decode(self,
               bboxes,
               pred_bboxes,
               max_shape=None,
               wh_ratio_clip=16 / 1000):
        """Apply transformation `pred_bboxes` to `bboxes`.
        Args:
            bboxes (torch.Tensor): Basic boxes. Shape (B, N, 4) or (N, 4)
            pred_bboxes (torch.Tensor): Encoded offsets with respect to each
                roi. Has shape (B, N, 5) or (N, 5).
                Note N = num_anchors * W * H
               when rois is a grid of anchors.Offset encoding follows [1]_.
            max_shape (Sequence[int] or torch.Tensor or Sequence[
               Sequence[int]],optional): Maximum bounds for boxes, specifies
               (H, W, C) or (H, W). If bboxes shape is (B, N, 6), then
               the max_shape should be a Sequence[Sequence[int]]
               and the length of max_shape should also be B.
            wh_ratio_clip (float, optional): The allowed ratio between
                width and height.

        Returns:
            torch.Tensor: Decoded boxes.
        """
        assert pred_bboxes.size(0) == bboxes.size(0)
        assert bboxes.size(-1) == 4
        assert pred_bboxes.size(-1) == 6
        decoded_bboxes = delta2bbox(bboxes, pred_bboxes, self.means, self.stds,
                                    wh_ratio_clip, self.version)

        return decoded_bboxes

@mmcv.jit(coderize=True)
def bbox2delta(proposals,
               gt,
               means=(0., 0., 0., 0., 0., 0.),
               stds=(1., 1., 1., 1., 1., 1.),
               version='oc'):
    """Compute deltas of proposals w.r.t.

    gt.
    We usually compute the deltas of x, y, w, h, a, b of proposals w.r.t ground
    truth bboxes to get regression target.
    This is the inverse function of :func:`delta2bbox`.
    Args:
        proposals (torch.Tensor): Boxes to be transformed, shape (N, ..., 4)
        gt (torch.Tensor): Gt bboxes to be used as base, shape (N, ..., 5)
        means (Sequence[float]): Denormalizing means for delta coordinates
        stds (Sequence[float]): Denormalizing standard deviation for delta
            coordinates.
        version (str, optional): Angle representations. Defaults to 'oc'.

    Returns:
        Tensor: deltas with shape (N, 6), where columns represent dx, dy,
            dw, dh, da, db.
    """
    proposals = proposals.float()
    gt = gt.float()

    px = (proposals[..., 0] + proposals[..., 2]) * 0.5
    py = (proposals[..., 1] + proposals[..., 3]) * 0.5
    pw = proposals[..., 2] - proposals[..., 0]
    ph = proposals[..., 3] - proposals[..., 1]

    hbb, poly = obb2xyxy(gt, version), obb2poly(gt, version)
    gx = (hbb[..., 0] + hbb[..., 2]) * 0.5
    gy = (hbb[..., 1] + hbb[..., 3]) * 0.5
    gw = hbb[..., 2] - hbb[..., 0]
    gh = hbb[..., 3] - hbb[..., 1]

    x_coor, y_coor = poly[:, 0::2], poly[:, 1::2]
    y_min, _ = torch.min(y_coor, dim=1, keepdim=True)
    x_max, _ = torch.max(x_coor, dim=1, keepdim=True)

    _x_coor = x_coor.clone()
    _x_coor[torch.abs(y_coor - y_min) > 0.1] = -1000
    ga, _ = torch.max(_x_coor, dim=1)

    _y_coor = y_coor.clone()
    _y_coor[torch.abs(x_coor - x_max) > 0.1] = -1000
    gb, _ = torch.max(_y_coor, dim=1)

    dx = (gx - px) / pw
    dy = (gy - py) / ph
    dw = torch.log(gw / pw)
    dh = torch.log(gh / ph)
    da = (ga - gx) / gw
    db = (gb - gy) / gh
    deltas = torch.stack([dx, dy, dw, dh, da, db], dim=-1)

    means = deltas.new_tensor(means).unsqueeze(0)
    stds = deltas.new_tensor(stds).unsqueeze(0)
    deltas = deltas.sub_(means).div_(stds)

    return deltas

@mmcv.jit(coderize=True)
def delta2bbox(rois,
               deltas,
               means=(0., 0., 0., 0., 0., 0.),
               stds=(1., 1., 1., 1., 1., 1.),
               wh_ratio_clip=16 / 1000,
               version='oc'):
    """Apply deltas to shift/scale base boxes.
    Typically the rois are anchor or proposed bounding boxes and the deltas are
    network outputs used to shift/scale those boxes.
    This is the inverse function of :func:`bbox2delta`.
    Args:
        rois (torch.Tensor): Boxes to be transformed. Has shape (N, 4).
        deltas (torch.Tensor): Encoded offsets relative to each roi.
            Has shape (N, num_classes * 4) or (N, 4). Note
            N = num_base_anchors * W * H, when rois is a grid of
            anchors. Offset encoding follows [1]_.
        means (Sequence[float]): Denormalizing means for delta coordinates.
            Default (0., 0., 0., 0., 0., 0.).
        stds (Sequence[float]): Denormalizing standard deviation for delta
            coordinates. Default (1., 1., 1., 1., 1., 1.).
        wh_ratio_clip (float): Maximum aspect ratio for boxes. Default
            16 / 1000.
        version (str, optional): Angle representations. Defaults to 'oc'.

    Returns:
        Tensor: Boxes with shape (N, num_classes * 5) or (N, 5), where 5
           represent cx, cy, w, h, a.
    """
    means = deltas.new_tensor(means).repeat(1, deltas.size(1) // 6)
    stds = deltas.new_tensor(stds).repeat(1, deltas.size(1) // 6)
    denorm_deltas = deltas * stds + means
    dx = denorm_deltas[:, 0::6]
    dy = denorm_deltas[:, 1::6]
    dw = denorm_deltas[:, 2::6]
    dh = denorm_deltas[:, 3::6]
    da = denorm_deltas[:, 4::6]
    db = denorm_deltas[:, 5::6]
    max_ratio = np.abs(np.log(wh_ratio_clip))
    dw = dw.clamp(min=-max_ratio, max=max_ratio)
    dh = dh.clamp(min=-max_ratio, max=max_ratio)
    # Compute center of each roi
    px = ((rois[:, 0] + rois[:, 2]) * 0.5).unsqueeze(1).expand_as(dx)
    py = ((rois[:, 1] + rois[:, 3]) * 0.5).unsqueeze(1).expand_as(dy)
    # Compute width/height of each roi
    pw = (rois[:, 2] - rois[:, 0]).unsqueeze(1).expand_as(dw)
    ph = (rois[:, 3] - rois[:, 1]).unsqueeze(1).expand_as(dh)
    # Use exp(network energy) to enlarge/shrink each roi
    gw = pw * dw.exp()
    gh = ph * dh.exp()
    # Use network energy to shift the center of each roi
    gx = px + pw * dx
    gy = py + ph * dy

    x1 = gx - gw * 0.5
    y1 = gy - gh * 0.5
    x2 = gx + gw * 0.5
    y2 = gy + gh * 0.5

    da = da.clamp(min=-0.5, max=0.5)
    db = db.clamp(min=-0.5, max=0.5)
    ga = gx + da * gw
    _ga = gx - da * gw
    gb = gy + db * gh
    _gb = gy - db * gh
    polys = torch.stack([ga, y1, x2, gb, _ga, y2, x1, _gb], dim=-1)

    center = torch.stack([gx, gy, gx, gy, gx, gy, gx, gy], dim=-1)
    center_polys = polys - center
    diag_len = torch.sqrt(center_polys[..., 0::2] * center_polys[..., 0::2] +
                          center_polys[..., 1::2] * center_polys[..., 1::2])
    max_diag_len, _ = torch.max(diag_len, dim=-1, keepdim=True)
    diag_scale_factor = max_diag_len / diag_len
    center_polys = center_polys * diag_scale_factor.repeat_interleave(
        2, dim=-1)
    rectpolys = center_polys + center
    obboxes = poly2obb(rectpolys, version)
    return obboxes

max_stars_repo_name: williamcorsel/mmrotate

max_issues_repo_name: williamcorsel/mmrotate

max_forks_repo_name: williamcorsel/mmrotate


text:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
from tweepy import Cursor

import twitter_credentials
import numpy as np
import pandas as pd
# This application was coded by following  the "Tweet Visualization and Sentiment Analysis in Python Tutorial"
# from Vincent Russo of Lucid Programming

class TwitterClient:
    def __init__(self, twitter_user=None):
        self.auth = TwitterAuth().authenticate_twitter()
        self.twitter_client = API(self.auth)
        self.twitter_user = twitter_user

    def get_twitter_client_api(self):
        return self.twitter_client

    def get_user_timeline_tweets(self, num_tweets):
        tweets_list = []
        for tweet in Cursor(self.twitter_client.user_timeline, id=self.twitter_user).items(num_tweets):
            tweets_list.append(tweet)
        return tweets_list

class TwitterAuth:
    @staticmethod
    def authenticate_twitter():
        auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
        auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
        return auth

class TwitterStreamer:
    """
    A class to stream and process live tweets
    """
    def __init__(self):
        self.twitter_auth = TwitterAuth()

    def stream_tweets(self, fetched_tweets_file, hash_tags):
        # Handles connecting to and streaming tweets from the Twitter API
        listener = TwitterListener(fetched_tweets_file)
        auth = self.twitter_auth.authenticate_twitter()

        stream = Stream(auth, listener)
        stream.filter(track=hash_tags)

class TwitterListener(StreamListener):
    """
    Basic listener class that prints data from the Twitter stream to stdOut
    """
    def __init__(self, fetched_tweets_file):
        self.fetched_tweets_file = fetched_tweets_file

    def on_data(self, data):
        try:
            print(data)
            with open(self.fetched_tweets_file, 'a') as tf:
                tf.write(data)
            return True
        except BaseException as e:
            print("Error_data: %s" % str(e))
        return True

    def on_error(self, status):
        if status == 420:
            # Terminate connection if twitter rate limit is exceeded
            return False
        print(status)

class TweetAnalyzer:
    """
    Used to analyze tweet data and categorize it
    """
    def tweets_to_df(self, tweets):
        df = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['tweets'])
        df['date'] = np.array([tweet.created_at for tweet in tweets])
        df['favorites'] = np.array([tweet.favorite_count for tweet in tweets])
        df['retweets'] = np.array([tweet.retweet_count for tweet in tweets])

        return df

if __name__ == "__main__":

    twitter_client = TwitterClient()
    tweet_analyzer = TweetAnalyzer()

    api = twitter_client.get_twitter_client_api()
    tweets = api.user_timeline(screen_name="realDonaldTrump", count=200)

    df = tweet_analyzer.tweets_to_df(tweets)
    # print("Average likes - ", np.mean(df['favorites']))
    # print("Most likes - ", np.max(df['favorites']))
    # print(dir(tweets[0]))
    print(df.head(20))
    # print(tweets[0].in_reply_to_screen_name)

ext: py

max_stars_repo_name: brandon-fenty/twitter-analyzer

max_issues_repo_name: brandon-fenty/twitter-analyzer

max_forks_repo_name: brandon-fenty/twitter-analyzer

reason: import numpy


text:
from dash import dcc, html
import plotly.express as px
import plotly.graph_objects as go
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

class Scatterplot(html.Div):
    def __init__(self, name, feature_x, feature_y, input_df):
        self.html_id = name.lower().replace(" ", "-")
        self.df = input_df
        self.feature_x = feature_x
        self.feature_y = feature_y

        # Equivalent to `html.Div([...])`
        super().__init__(
            className="graph_card",
            children=[
                html.H6(name + " - 1 stands for Good RiskPerformance"),
                dcc.Graph(id=self.html_id)
            ],
        )

    def update(self, scplt_color, input_df):

        categorical = []
        if 'MaxDelqEver' in input_df.columns:
            categorical.append('MaxDelqEver')
        if 'MaxDelq/PublicRecLast12M' in input_df.columns:
            categorical.append('MaxDelq/PublicRecLast12M')

        scplot_cmap = \
            'RdYlGn' if scplt_color in ('y_pred_prob', 'y_pred', 'y_test') \
            else px.colors.qualitative.G10 if scplt_color in categorical \
            else 'Oranges'

        self.fig = px.scatter(
            input_df,
            x=self.feature_x,
            y=self.feature_y,
            hover_data=input_df.columns,
            custom_data=[input_df.index],
            color=scplt_color,
            color_continuous_scale=scplot_cmap,
            color_discrete_sequence=px.colors.qualitative.G10
        )

        # Estimate model's decision boundary using Voronoi tesselation
        # Source: https://stackoverflow.com/a/61225622/9994398

        # create meshgrid
        res = 80
        X2d_xmin = np.min(input_df[self.feature_x])
        X2d_xmax = np.max(input_df[self.feature_x])
        X2d_ymin = np.min(input_df[self.feature_y])
        X2d_ymax = np.max(input_df[self.feature_y])
        xx, yy = np.meshgrid(np.linspace(X2d_xmin, X2d_xmax, res),
                             np.linspace(X2d_ymin, X2d_ymax, res))

        X_embed = input_df[[self.feature_x, self.feature_y]]

        # approximate Voronoi tesselation using KNN
        background_model = KNeighborsClassifier(n_neighbors=10).fit(
            X_embed,
            input_df.y_pred
        )
        voronoiBackground = background_model.predict_proba(
            np.c_[xx.ravel(), yy.ravel()]
        )[:, 1]
        voronoiBackground = voronoiBackground.reshape((res, res))

        self.fig.add_trace(
            go.Contour(
                x=np.linspace(X2d_xmin, X2d_xmax, res),
                y=np.linspace(X2d_ymin, X2d_ymax, res),
                z=voronoiBackground,
                colorscale='YlGn',
                hoverinfo='skip',
                line_smoothing=0.75
            )
        )

        self.fig.update_layout(
            yaxis_zeroline=False,
            xaxis_zeroline=False,
            dragmode='select',
            height=650,
            coloraxis_colorbar=dict(yanchor="top", y=1, x=0, ticks="outside")
        )
        self.fig.update_xaxes(visible=False, showticklabels=False)
        self.fig.update_yaxes(visible=False, showticklabels=False)

        return self.fig

max_stars_repo_name: adubowski/va-heloc

max_issues_repo_name: adubowski/va-heloc

max_forks_repo_name: adubowski/va-heloc


text:
import numpy as np
def rgb2lab(rgb):
    return xyztolab(rgbtoxyz(rgb))

def rgbtoxyz(rgb):
    rgb /= 255.0
    processed_rgb = np.piecewise(rgb, [rgb > 0.4045, rgb <= 0.045], [lambda rgb: ((rgb + 0.055) / 1.055) ** 2.4, lambda rgb: rgb / 12.92]).T * 100.0
    xyz_weights = np.array([[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]])
    xyz = np.round(np.matmul(xyz_weights, processed_rgb), decimals=4)

    return xyz

def xyztolab(xyz):
    diffuser = np.array([95.047, 100.0, 108.883])
    xyz /= diffuser
    processed_xyz = np.piecewise(xyz, [xyz > 0.008856, xyz <= 0.00856], [lambda xyz: xyz ** (1.0 / 3.0), lambda xyz: (7.787 * xyz) + (16.0 / 116.0)])
    L = round(116 * processed_xyz[0] - 16, 4)
    a = round(500 * (processed_xyz[0] - processed_xyz[1]))
    b = round(200 * (processed_xyz[1] - processed_xyz[2]))

    Lab = np.array([L, a, b])
    return Lab


text:
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# author: jeremy.zhang(szujeremy@gmail.com, Shenzhen University, China)

import numpy as np
from sklearn.feature_selection import mutual_info_regression

from .utils import check_X_y
from .Information import mutual_info

def correlation(x1, x2):

    x1_2 = np.square(x1)
    x2_2 = np.square(x2)
    x1_x2 = x1 * x2

    u_x1 = np.mean(x1)
    u_x2 = np.mean(x2)

    s_x = np.sqrt(np.mean(x1_2) - np.square(u_x1))
    s_y = np.sqrt(np.mean(x2_2) - np.square(u_x2))
    s_xy = np.mean(x1_x2) - u_x1 * u_x2

    return s_xy / (s_x * s_y + 1e-8)

def mRMR(X, y):
    """
    Minimum Redundancy Maximum Relevance (mRMR) feature selection algorithm.
    paper: An Improved Minimum Redundancy Maximum Relevance Approach for Feature Selection in Gene Expression Data
    paper: Feature Selection Based on Mutual Information: Criteria of Max-Dependency, Max-Relevance, and Min-Redundancy
    """
    X, y = check_X_y(X, y)

    n_samples, n_features = X.shape

    all_index = np.arange(n_features)
    selected_index = []

    feature_relevance = mutual_info(X, y)

    index = np.nanargmax(feature_relevance)
    selected_index.append(index)

    scores = np.zeros((n_features,))
    scores[index] = feature_relevance[index]

    for i in range(1, n_features):
        left_index = np.setdiff1d(all_index, selected_index)

        relevance = feature_relevance[left_index]
        redundancy = [np.mean([correlation(X[:, i], X[:, s]) for s in selected_index]) for i in left_index]

        metric = relevance - np.abs(redundancy)
        index = np.nanargmax(metric)

        scores[left_index[index]] = metric[index]
        selected_index.append(left_index[index])

    return scores

max_issues_repo_name: MedcAILab/scikit-radiomics

max_forks_repo_name: MedcAILab/scikit-radiomics


text:
import netCDF4
from tvtk.api import tvtk
from mayavi import mlab
import numpy
from scipy.interpolate import griddata,RegularGridInterpolator
from numpy import mgrid, empty, sin, pi, array, meshgrid, arange, prod
import rasterio
import os
import matplotlib.pyplot as plt
from matplotlib import cm
from pyproj import Proj, transform
import fiona
from matplotlib.path import Path
from folium.plugins import TimestampedGeoJson
import mplleaflet
import folium
import branca
import dateutil
from datetime import timedelta
from matplotlib.dates import num2date,date2num
inProj = Proj(init='epsg:'+str(2193))
outProj = Proj(init='epsg:'+str(4326))

	# some path to your local netcdf file
#path = '/home/remy/Calypso/Projects/006_Southport/hydro/child/hg6_dt30_newD/outputs/schout_14.nc'
T0=dateutil.parser.parse('2019-09-02T12:00:00Z')
fileout='Fov.html'

path = '/home/remy/Calypso/Projects/006_Southport/hydro/mother/schism/run5/outputs/schout_14.nc'
#T0=dateutil.parser.parse('2020-01-03T09:00:00Z')#T0=datenum(2020,01,03,09,00,00)+0/24;ext=3;
#fileout='Neap_tide.html'

lim=[1241352-400,1245886+2500,4825135,4830428]
lim=[1110740,1304120,4704419,4874442]
#lim=[1236590,1254365,4821428,4836312]
quiver_res=300
quiver_res=50
quiver_scale=80
# try and import the dataset, prefer netcdf4, you might want to use pydap also here if you access a dap server.

ds = netCDF4.Dataset(path)
X=ds.variables['SCHISM_hgrid_node_x'][:]
Y=ds.variables['SCHISM_hgrid_node_y'][:]

X,Y=transform(inProj,outProj,X,Y)
lim[0],lim[2]=transform(inProj,outProj,lim[0],lim[2])
lim[1],lim[3]=transform(inProj,outProj,lim[1],lim[3])

off=0
gd = (X>=lim[0]-off) & (X<=lim[1]+off) & (Y>=lim[2]-off) & (Y<=lim[3]+off)
X=X[gd]
Y=Y[gd]
XY=numpy.array((X,Y)).T

d= ds.variables['depth'][:]
d=d[gd]
dtime = netCDF4.num2date(ds.variables['time'][:],ds.variables['time'].units)

dtime=[date2num(x._to_real_datetime()) for x in dtime]

idxTs=[]

for dt in numpy.arange(-6,6.5,1):
    to=date2num(T0+timedelta(hours=dt))
    idxTs.append(dtime.index(min(dtime, key=lambda x:abs(x-to))))

#D=D[gd]
Xreg, Yreg = numpy.meshgrid(numpy.linspace(lim[0],lim[1], quiver_res), numpy.linspace(lim[2], lim[3], quiver_res))

mapa = folium.Map(location=[Yreg.mean(), Xreg.mean()], tiles="Cartodb Positron",
                  zoom_start=10)

# shape = fiona.open('/home/remy/Calypso/Projects/006_Southport/hydro/child/animation/poly.shp')
verts=[]
# for poly in shape:
#     if poly['geometry']!=None :
#             nvert=len(poly['geometry']['coordinates'][0])
#             verts.append([(poly['geometry']['coordinates'][0][x][0],poly['geometry']['coordinates'][0][x][1]) for x in range(0,nvert)])

is_first=True

for i,idxT in enumerate(idxTs):
    print(i)
    if (i!=2) and (i!=9):
        continue

     # read the variables (these are just links to the objects for now)
    u = ds.variables['hvel'][idxT,:,-1,0] # velocity in u direction
    v = ds.variables['hvel'][idxT,:,-1,1] # v direction
    e = ds.variables['elev'][idxT,:] # v direction
    e=e[gd]
    u=u[gd]
    v=v[gd]
    bad=e+d<0.1

    U = griddata(XY[~bad,:], u[~bad],(Xreg,Yreg),method='linear')
    V = griddata(XY[~bad,:], v[~bad],(Xreg,Yreg),method='linear')
    E = griddata(XY[~bad,:], e[~bad],(Xreg,Yreg),method='linear')
    D = griddata(XY[~bad,:], d[~bad],(Xreg,Yreg),method='linear')

    fig = plt.figure(figsize=(30,18))
    ax = fig.add_subplot(111)
    ax.set_aspect('equal')

    mag=numpy.sqrt(U**2+V**2)*1.94

    U[numpy.isnan(U)]=0
    V[numpy.isnan(V)]=0
    mag[numpy.isnan(mag)]=0

    #Xreg[numpy.isnan(Xreg)]=0
    #Yreg[numpy.isnan(Yreg)]=0

    Q = ax.quiver(Xreg, Yreg, U*1.94, V*1.94,mag,scale=quiver_scale,color=cm.viridis(64),clim=[0,4])

    gj = mplleaflet.fig_to_geojson(fig=fig)

    if i==2:
        name='Ebb tide'
    elif i==9:
        name='Flood tide'
    feature_group0 = folium.FeatureGroup(name=name,show=is_first)
    #'%.1fH' % ((dtime[idxTs[i]]-date2num(T0))*24)
    is_first=False

    spd=RegularGridInterpolator([Yreg[:,0],Xreg[0,:]],mag)

    for feature in gj['features']:
        if feature['geometry']['type'] == 'Point':
            lon, lat = feature['geometry']['coordinates']

            div = feature['properties']['html']
            flag=False
            for vert in verts:
                p=Path(vert)
                flag = p.contains_points([[lon,lat]])
                if flag:
                   break

            Spd=spd([lat,lon])[0]
            if not flag and Spd>0.05:
                #print(Spd)
                icon_anchor = (feature['properties']['anchor_x'],
                               feature['properties']['anchor_y'])

                icon = folium.features.DivIcon(html=div,
                                               icon_anchor=icon_anchor)

                #spd=
                marker = folium.Marker([lat, lon], icon=icon)

                marker.add_child(folium.Popup('%.1f' % Spd))

                feature_group0.add_child(marker)
        else:
            msg = "Unexpected geometry {}".format
            raise ValueError(msg(feature['geometry']))

    mapa.add_child(feature_group0)

tile = folium.TileLayer(
        tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        attr = 'Esri',
        name = 'Esri Satellite',
        overlay = False,
        control = False
       ).add_to(mapa)

colormap = branca.colormap.linear.viridis.scale(0, 4)
colormap = colormap.to_step(index=numpy.arange(0,4.01,.01))
colormap.caption = 'Tidal speed [knt]'
colormap.add_to(mapa)

folium.LayerControl(collapsed=False).add_to(mapa)
mapa.save(fileout)

max_stars_repo_name: calypso-science/schism

max_issues_repo_name: calypso-science/schism

max_forks_repo_name: calypso-science/schism

reason: import numpy,from numpy,from scipy


text:
from typing import Dict, Iterable, Any

import numpy as np

from encoders.base_encoder import BaseEncoder

class EncoderCache(BaseEncoder):

    def __init__(self, encoder: BaseEncoder, cache: Dict[str, np.array] = None, verbose=False) -> None:
        super().__init__(verbose)
        self.encoder = encoder
        self.cache = cache or {}

    def encode(self, data: Iterable[Any]) -> Iterable[Any]:
        result = []
        for el in data:
            vector: np.array = self.cache.get(el)
            if vector is None:
                vector = self.encoder.encode([el])
                vector = vector[0]
                self.cache[el] = vector
            result.append(vector)
        return np.array(result)

    def input_dim(self) -> int:
        return self.encoder.input_dim()

    def output_dim(self) -> int:
        return self.encoder.output_dim()


text:
"""Tests of whether units behave."""

from assay import assert_raises
from numpy import array
from skyfield import units

try:
    from astropy import units as u
except ImportError:
    u = None

# needs_astropy = pytest.mark.skipif(u is None, reason='cannot import AstroPy')

def test_stringifying_vector_distance():
    a = array([1.23, 4.56])
    assert str(units.Distance(au=a)) == '[ 1.23  4.56] au'

def test_iterating_over_raw_measurement():
    distance = units.Distance(au=1.234)
    with assert_raises(units.UnpackingError):
        x, y, z = distance

def test_iterating_over_raw_velocity():
    velocity = units.Velocity(au_per_d=1.234)
    with assert_raises(units.UnpackingError):
        x, y, z = velocity

def test_converting_from_km_to_m():
    distance = units.Distance(km=1.234)
    assert abs(distance.m - 1234.0) < 1e-15

def test_converting_from_m_to_km():
    distance = units.Distance(m=1234.0)
    assert abs(distance.km - 1.234) < 1e-15

# @needs_astropy
def test_converting_distance_with_astropy():
    distance = units.Distance(au=1.234)
    value1 = distance.km
    value2 = distance.to(u.km)
    epsilon = 0.02         # definitions of AU seem to disagree slightly
    assert abs(value1 - value2.value) < epsilon

# @needs_astropy
def test_converting_velocity_with_astropy():
    velocity = units.Velocity(au_per_d=1.234)
    value1 = velocity.km_per_s
    value2 = velocity.to(u.km / u.s)
    epsilon = 1e-6
    assert abs(value1 - value2.value) < epsilon

max_stars_repo_name: aarose/python-skyfield

max_issues_repo_name: aarose/python-skyfield

max_forks_repo_name: aarose/python-skyfield

reason: from numpy,from astropy


text:
import tensorflow as tf
import numpy as np
import miditoolkit
import modules
import pickle
import utils
import time

class PopMusicTransformer(object):
    ########################################
    # initialize
    ########################################
    def __init__(self, checkpoint, is_training=False):
        # load dictionary
        self.dictionary_path = '{}/dictionary.pkl'.format(checkpoint)
        self.event2word, self.word2event = pickle.load(open(self.dictionary_path, 'rb'))
        # model settings
        self.x_len = 512
        self.mem_len = 512
        self.n_layer = 12
        self.d_embed = 512
        self.d_model = 512
        self.dropout = 0.1
        self.n_head = 8
        self.d_head = self.d_model // self.n_head
        self.d_ff = 2048
        self.n_token = len(self.event2word)
        self.learning_rate = 0.0002
        # load model
        self.is_training = is_training
        if self.is_training:
            self.batch_size = 4
        else:
            self.batch_size = 1
        self.checkpoint_path = '{}/model'.format(checkpoint)
        self.load_model()

    ########################################
    # load model
    ########################################
    def load_model(self):
        # placeholders
        self.x = tf.compat.v1.placeholder(tf.int32, shape=[self.batch_size, None])
        self.y = tf.compat.v1.placeholder(tf.int32, shape=[self.batch_size, None])
        self.mems_i = [tf.compat.v1.placeholder(tf.float32, [self.mem_len, self.batch_size, self.d_model]) for _ in range(self.n_layer)]
        # model
        self.global_step = tf.compat.v1.train.get_or_create_global_step()
        initializer = tf.compat.v1.initializers.random_normal(stddev=0.02, seed=None)
        proj_initializer = tf.compat.v1.initializers.random_normal(stddev=0.01, seed=None)
        with tf.compat.v1.variable_scope(tf.compat.v1.get_variable_scope()):
            xx = tf.transpose(self.x, [1, 0])
            yy = tf.transpose(self.y, [1, 0])
            loss, self.logits, self.new_mem = modules.transformer(
                dec_inp=xx,
                target=yy,
                mems=self.mems_i,
                n_token=self.n_token,
                n_layer=self.n_layer,
                d_model=self.d_model,
                d_embed=self.d_embed,
                n_head=self.n_head,
                d_head=self.d_head,
                d_inner=self.d_ff,
                dropout=self.dropout,
                dropatt=self.dropout,
                initializer=initializer,
                proj_initializer=proj_initializer,
                is_training=self.is_training,
                mem_len=self.mem_len,
                cutoffs=[],
                div_val=-1,
                tie_projs=[],
                same_length=False,
                clamp_len=-1,
                input_perms=None,
                target_perms=None,
                head_target=None,
                untie_r=False,
                proj_same_dim=True)
        self.avg_loss = tf.reduce_mean(loss)
        # vars
        all_vars = tf.compat.v1.trainable_variables()
        grads = tf.gradients(self.avg_loss, all_vars)
        grads_and_vars = list(zip(grads, all_vars))
        all_trainable_vars = tf.reduce_sum([tf.reduce_prod(v.shape) for v in tf.compat.v1.trainable_variables()])
        # optimizer
        decay_lr = tf.compat.v1.train.cosine_decay(
            self.learning_rate,
            global_step=self.global_step,
            decay_steps=400000,
            alpha=0.004)
        optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=decay_lr)
        self.train_op = optimizer.apply_gradients(grads_and_vars, self.global_step)
        # saver
        self.saver = tf.compat.v1.train.Saver()
        config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        self.sess = tf.compat.v1.Session(config=config)
        self.saver.restore(self.sess, self.checkpoint_path)

    ########################################
    # temperature sampling
    ########################################
    def temperature_sampling(self, logits, temperature, topk):
        probs = np.exp(logits / temperature) / np.sum(np.exp(logits / temperature))
        if topk == 1:
            prediction = np.argmax(probs)
        else:
            sorted_index = np.argsort(probs)[::-1]
            candi_index = sorted_index[:topk]
            candi_probs = [probs[i] for i in candi_index]
            # normalize probs
            candi_probs /= sum(candi_probs)
            # choose by predicted probs
            prediction = np.random.choice(candi_index, size=1, p=candi_probs)[0]
        return prediction

    ########################################
    # extract events for prompt continuation
    ########################################
    def extract_events(self, input_path):
        note_items, tempo_items = utils.read_items(input_path)
        note_items = utils.quantize_items(note_items)
        max_time = note_items[-1].end
        if 'chord' in self.checkpoint_path:
            chord_items = utils.extract_chords(note_items)
            items = chord_items + tempo_items + note_items
        else:
            items = tempo_items + note_items
        groups = utils.group_items(items, max_time)
        events = utils.item2event(groups)
        return events

    ########################################
    # generate
    ########################################
    def generate(self, n_target_bar, temperature, topk, output_path, prompt=None):
        # if prompt, load it. Or, random start
        if prompt:
            events = self.extract_events(prompt)
            words = [[self.event2word['{}_{}'.format(e.name, e.value)] for e in events]]
            words[0].append(self.event2word['Bar_None'])
        else:
            words = []
            for _ in range(self.batch_size):
                ws = [self.event2word['Bar_None']]
                if 'chord' in self.checkpoint_path:
                    tempo_classes = [v for k, v in self.event2word.items() if 'Tempo Class' in k]
                    tempo_values = [v for k, v in self.event2word.items() if 'Tempo Value' in k]
                    chords = [v for k, v in self.event2word.items() if 'Chord' in k]
                    ws.append(self.event2word['Position_1/16'])
                    ws.append(np.random.choice(chords))
                    ws.append(self.event2word['Position_1/16'])
                    ws.append(np.random.choice(tempo_classes))
                    ws.append(np.random.choice(tempo_values))
                else:
                    tempo_classes = [v for k, v in self.event2word.items() if 'Tempo Class' in k]
                    tempo_values = [v for k, v in self.event2word.items() if 'Tempo Value' in k]
                    ws.append(self.event2word['Position_1/16'])
                    ws.append(np.random.choice(tempo_classes))
                    ws.append(np.random.choice(tempo_values))
                words.append(ws)
        # initialize mem
        batch_m = [np.zeros((self.mem_len, self.batch_size, self.d_model), dtype=np.float32) for _ in range(self.n_layer)]
        # generate
        original_length = len(words[0])
        initial_flag = 1
        current_generated_bar = 0
        while current_generated_bar < n_target_bar:
            # input
            if initial_flag:
                temp_x = np.zeros((self.batch_size, original_length))
                for b in range(self.batch_size):
                    for z, t in enumerate(words[b]):
                        temp_x[b][z] = t
                initial_flag = 0
            else:
                temp_x = np.zeros((self.batch_size, 1))
                for b in range(self.batch_size):
                    temp_x[b][0] = words[b][-1]
            # prepare feed dict
            feed_dict = {self.x: temp_x}
            for m, m_np in zip(self.mems_i, batch_m):
                feed_dict[m] = m_np
            # model (prediction)
            _logits, _new_mem = self.sess.run([self.logits, self.new_mem], feed_dict=feed_dict)
            # sampling
            _logit = _logits[-1, 0]
            word = self.temperature_sampling(
                logits=_logit,
                temperature=temperature,
                topk=topk)
            words[0].append(word)
            # if bar event (only work for batch_size=1)
            if word == self.event2word['Bar_None']:
                current_generated_bar += 1
            # re-new mem
            batch_m = _new_mem
        # write
        if prompt:
            utils.write_midi(
                words=words[0][original_length:],
                word2event=self.word2event,
                output_path=output_path,
                prompt_path=prompt)
        else:
            utils.write_midi(
                words=words[0],
                word2event=self.word2event,
                output_path=output_path,
                prompt_path=None)

    ########################################
    # prepare training data
    ########################################
    def prepare_data(self, midi_paths):
        # extract events
        all_events = []
        for path in midi_paths:
            events = self.extract_events(path)
            all_events.append(events)
        # event to word
        all_words = []
        for events in all_events:
            words = []
            for event in events:
                e = '{}_{}'.format(event.name, event.value)
                if e in self.event2word:
                    words.append(self.event2word[e])
                else:
                    # OOV
                    if event.name == 'Note Velocity':
                        # replace with max velocity based on our training data
                        words.append(self.event2word['Note Velocity_21'])
                    else:
                        # something is wrong
                        # you should handle it for your own purpose
                        print('something is wrong! {}'.format(e))
            all_words.append(words)
        # to training data
        self.group_size = 5
        segments = []
        for words in all_words:
            pairs = []
            for i in range(0, len(words)-self.x_len-1, self.x_len):
                x = words[i:i+self.x_len]
                y = words[i+1:i+self.x_len+1]
                pairs.append([x, y])
            pairs = np.array(pairs)
            # abandon the last
            for i in np.arange(0, len(pairs)-self.group_size, self.group_size*2):
                data = pairs[i:i+self.group_size]
                if len(data) == self.group_size:
                    segments.append(data)
        segments = np.array(segments)
        return segments

    ########################################
    # finetune
    ########################################
    def finetune(self, training_data, output_checkpoint_folder):
        # shuffle
        index = np.arange(len(training_data))
        np.random.shuffle(index)
        training_data = training_data[index]
        num_batches = len(training_data) // self.batch_size
        st = time.time()
        for e in range(200):
            total_loss = []
            for i in range(num_batches):
                segments = training_data[self.batch_size*i:self.batch_size*(i+1)]
                batch_m = [np.zeros((self.mem_len, self.batch_size, self.d_model), dtype=np.float32) for _ in range(self.n_layer)]
                for j in range(self.group_size):
                    batch_x = segments[:, j, 0, :]
                    batch_y = segments[:, j, 1, :]
                    # prepare feed dict
                    feed_dict = {self.x: batch_x, self.y: batch_y}
                    for m, m_np in zip(self.mems_i, batch_m):
                        feed_dict[m] = m_np
                    # run
                    _, gs_, loss_, new_mem_ = self.sess.run([self.train_op, self.global_step, self.avg_loss, self.new_mem], feed_dict=feed_dict)
                    batch_m = new_mem_
                    total_loss.append(loss_)
                    print('>>> Epoch: {}, Step: {}, Loss: {:.5f}, Time: {:.2f}'.format(e, gs_, loss_, time.time()-st))
            self.saver.save(self.sess, '{}/model-{:03d}-{:.3f}'.format(output_checkpoint_folder, e, np.mean(total_loss)))
            # stop
            if np.mean(total_loss) <= 0.1:
                break

    ########################################
    # close
    ########################################
    def close(self):
        self.sess.close()


text:
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# arclytics_sim
# test_api_sim_configs.py
#
# Attributions:
# [1]
# -----------------------------------------------------------------------------

__author__ = ['Andrew Che <@codeninja55>']
__status__ = 'development'
__date__ = '2019.07.13'

import json
import unittest
from copy import deepcopy
from pathlib import Path

import numpy as np
from mongoengine import get_db

from arc_logging import AppLogger
from manage import BASE_DIR
from sim_api.models import User
from sim_api.schemas import AlloyStoreSchema, ConfigurationsSchema
from tests.test_api_base import BaseTestCase, app
from tests.test_utilities import test_login

logger = AppLogger(__name__)

_TEST_CONFIGS_PATH = Path(BASE_DIR) / 'tests' / 'sim_configs.json'
with open(_TEST_CONFIGS_PATH, 'r') as f:
    test_json = json.load(f)

ALLOY_STORE = {
    'alloy_option': 'single',
    'alloys': {
        'parent': {
            'name': 'Arc_Stark',
            'compositions': test_json['compositions']
        }
    }
}

CONFIGS = test_json['configurations']

# noinspection PyTypeChecker
class TestSimConfigurations(BaseTestCase):

    # Note:
    #  - We can treat the session stores of the compositions and configurations
    #    as always being the one that's displayed on the Client UI.

    _email = None
    _user_pw = 'IllShowYouFerocity!@!@'
    mongo = None

    @classmethod
    def setUpClass(cls) -> None:
        assert app.config['TESTING'] is True

        # Tony is an admin
        cls.user = User(
            **{
                'email': 'antman@avengers.io',
                'first_name': 'Hank',
                'last_name': 'Pym'
            }
        )
        cls.user.set_password(cls._user_pw)
        cls.user.verified = True

        configs = ConfigurationsSchema().load(test_json['configurations'])
        store_dict = {
            'alloy_option': 'single',
            'alloys': {
                'parent': {
                    'name': 'Arc_Stark',
                    'compositions': test_json['compositions']
                },
                'weld': None,
                'mix': None
            }
        }
        alloy_store = AlloyStoreSchema().load(store_dict)

        cls.user.last_alloy_store = alloy_store
        cls.user.last_configuration = configs

        cls.user.save()
        cls._email = cls.user.email

        mongo = get_db('default')
        user = mongo.users.find_one({'email': 'antman@avengers.io'})
        assert user is not None

    @classmethod
    def tearDownClass(cls) -> None:
        """On finishing, we should delete users collection so no conflict."""
        db = get_db('default')
        assert db.name == 'arc_test'
        db.users.drop()

    def login_client(self, client):
        """Set up a User for the simulation."""
        test_login(client, self._email, self._user_pw)

    def logout_client(self, client):
        resp_login = client.get(
            '/v1/sim/auth/logout',
            content_type='application/json',
            environ_base={'REMOTE_ADDR': '127.0.0.1'}
        )
        self.assertStatus(resp_login, 202)

    def test_auto_update_ms_invalid_payloads(self):
        """Ensure we get the correct error messages for invalid payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Empty payload
            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid payload.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No alloy store payload
            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps({'method': 'Li98'}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Alloy store required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No method payload
            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps({'alloy_store': ALLOY_STORE}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Wrong method payload
            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'LiAndKirkaldy',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            msg = 'Method must be one of ["Li98" | "Kirkaldy83"].'
            self.assertEqual(data['message'], msg)
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_ms_invalid_alloys(self):
        """Ensure we get the correct error messages for bad alloy payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Missing carbon elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'].pop(0)

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Missing element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['symbol'] = 'Cx'

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element symbol error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad Carbon weight elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['weight'] = 0.9

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element weight error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol key elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0].pop('symbol')

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_ms(self):
        """Ensure we get the correct calculations for MS given the alloy."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assert200(res)
            self.assertEqual(data['status'], 'success')
            ms_temp = np.float32(data['data']['ms_temp'])
            ms_rate_param = np.float32(data['data']['ms_rate_param'])
            self.assertAlmostEqual(ms_temp, 464.1792, 2)
            self.assertAlmostEqual(ms_rate_param, 0.020691, 2)

    def test_auto_update_ms_kirkaldy_method(self):
        """Ensure we get the correct calculations for MS with Kirkaldy83."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.post(
                '/v1/sim/configs/ms',
                data=json.dumps(
                    {
                        'method': 'Kirkaldy83',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assert200(res)
            self.assertEqual(data['status'], 'success')
            ms_temp = np.float32(data['data']['ms_temp'])
            ms_rate_param = np.float32(data['data']['ms_rate_param'])
            self.assertAlmostEqual(ms_temp, 477.5753, 2)
            self.assertAlmostEqual(ms_rate_param, 0.02069, 2)

    def test_deprecated_update_ms(self):
        """Check that updating Ms has been deprecated."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.put(
                '/v1/sim/configs/ms',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method Not Allowed.')
            self.assertEqual(data['status'], 'fail')
            self.assertEqual(res.status_code, 405)

    def test_auto_update_bs_invalid_payloads(self):
        """Ensure we get the correct error messages for invalid payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Empty payload
            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid payload.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No alloy store payload
            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps({'method': 'Li98'}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Alloy store required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No method payload
            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps({'alloy_store': ALLOY_STORE}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Wrong method payload
            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'LiAndKirkaldy',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            msg = 'Method must be one of ["Li98" | "Kirkaldy83"].'
            self.assertEqual(data['message'], msg)
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_bs_invalid_alloys(self):
        """Ensure we get the correct error messages for bad alloy payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Missing carbon elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'].pop(0)

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Missing element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['symbol'] = 'Cx'

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element symbol error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad Carbon weight elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['weight'] = 0.9

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element weight error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol key elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0].pop('symbol')

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_bs(self):
        """Ensure we get the correct calculations for BS given the alloy."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assert200(res)
            self.assertEqual(data['status'], 'success')
            ms_temp = np.float32(data['data']['bs_temp'])
            self.assertAlmostEqual(ms_temp, 563.2208, 2)

    def test_auto_update_bs_kirkaldy_method(self):
        """Ensure we get the correct calculations for BS given Kirkaldy83
        method testing configs and the testing compositions.
        """
        with app.test_client() as client:
            self.login_client(client)

            res = client.post(
                '/v1/sim/configs/bs',
                data=json.dumps(
                    {
                        'method': 'Kirkaldy83',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assert200(res)
            self.assertEqual(data['status'], 'success')
            bs_temp = np.float32(data['data']['bs_temp'])
            self.assertAlmostEqual(bs_temp, 565.723, 2)

    def test_deprecated_update_bs(self):
        """Check that updating Bs has been deprecated."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.put(
                '/v1/sim/configs/ae',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method Not Allowed.')
            self.assertEqual(data['status'], 'fail')
            self.assertEqual(res.status_code, 405)

    def test_auto_update_ae_invalid_payloads(self):
        """Ensure we get the correct error messages for invalid payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Empty payload
            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid payload.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No alloy store payload
            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps({'method': 'Li98'}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Alloy store required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # No method payload
            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps({'alloy_store': ALLOY_STORE}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method required.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Wrong method payload
            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'LiAndKirkaldy',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            msg = 'Method must be one of ["Li98" | "Kirkaldy83"].'
            self.assertEqual(data['message'], msg)
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_ae_invalid_alloys(self):
        """Ensure we get the correct error messages for bad alloy payloads."""
        with app.test_client() as client:
            self.login_client(client)

            # Missing carbon elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'].pop(0)

            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Missing element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['symbol'] = 'Cx'

            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element symbol error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad Carbon weight elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0]['weight'] = 0.9

            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element weight error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

            # Bad symbol key elements
            alloy_store = deepcopy(ALLOY_STORE)
            alloy_store['alloys']['parent']['compositions'][0].pop('symbol')

            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': alloy_store
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Invalid element error.')
            self.assertEqual(data['status'], 'fail')
            self.assert400(res)

    def test_auto_update_ae(self):
        """Ensure we get the correct calculations for Ae1 and Ae3."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.post(
                '/v1/sim/configs/ae',
                data=json.dumps(
                    {
                        'method': 'Li98',
                        'alloy_store': ALLOY_STORE
                    }
                ),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assert200(res)
            self.assertEqual(data['status'], 'success')
            ae1 = np.float32(data['data']['ae1_temp'])
            ae3 = np.float32(data['data']['ae3_temp'])
            self.assertAlmostEqual(ae1, 700.8947, 2)
            self.assertAlmostEqual(ae3, 845.8189, 2)

    def test_deprecated_update_ae(self):
        """Check that updating Ae has been deprecated."""
        with app.test_client() as client:
            self.login_client(client)

            res = client.put(
                '/v1/sim/configs/ae',
                data=json.dumps({}),
                content_type='application/json'
            )
            data = json.loads(res.data.decode())
            self.assertEqual(data['message'], 'Method Not Allowed.')
            self.assertEqual(data['status'], 'fail')
            self.assertEqual(res.status_code, 405)

if __name__ == '__main__':
    unittest.main()


text:
"""
Author : Koralp Catalsakal
Date : 20/03/2020
"""

import shap_bootstrap.utils as utils
import shap_bootstrap.datasets as datasets
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

def visualize_pairvs(benchmark_df, branch1, branch2, label_opt=None, epsilon=1e-03):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
        label_opt ([type], optional): [description]. Defaults to None.
        epsilon ([type], optional): [description]. Defaults to 1e-03.
    """

    # bench_b1_vs_b2 = benchmark_df.copy()
    # bsl1 = bench_b1_vs_b2.groupby('Dataset_name').agg({branch1 : min})
    # bsl2 = bench_b1_vs_b2.groupby('Dataset_name').agg({branch2 : min})
    # for j in range(len(bench_b1_vs_b2['Dataset_name'])):
    #    bench_b1_vs_b2[branch1].iloc[j] = bsl1.loc[bench_b1_vs_b2['Dataset_name'].iloc[j]][0]
    #    bench_b1_vs_b2[branch2].iloc[j] = bsl2.loc[bench_b1_vs_b2['Dataset_name'].iloc[j]][0]
    bench_b1_vs_b2 = utils.set_branch_best(benchmark_df, branch1, branch2)
    bench_b1_vs_b2["Vs_results"] = bench_b1_vs_b2.apply(
        lambda x: _label_vs(x, branch1, branch2, epsilon), axis=1
    )
    bench_df_grouped = bench_b1_vs_b2.groupby("Vs_results").agg(
        {"Dataset_name": "nunique"}
    )
    bench_df_grouped.reset_index(inplace=True)
    bench_df_grouped.columns = ["Comparison Winner", "Number of Wins"]
    bench_df_grouped_cat = bench_b1_vs_b2.groupby(["Dataset_name", "Vs_results"]).agg(
        {"Dataset_name": "nunique"}
    )
    bench_df_grouped_cat.columns = ["Dataset Index"]
    bench_df_grouped_cat["Dataset Index"] = np.arange(bench_df_grouped_cat.shape[0])
    bench_df_grouped_cat.reset_index(inplace=True)

    ax1 = sns.barplot(y="Number of Wins", x="Comparison Winner", data=bench_df_grouped)
    ax1.set_title("Comparison results with significance level σ: {}".format(epsilon))
    ax2 = sns.catplot(
        x="Vs_results", y="Dataset Index", hue="Dataset_name", data=bench_df_grouped_cat
    )

    if label_opt is None:
        ax2.set(
            xlabel="Better Performance",
            ylabel="Datasets",
            yticks=np.arange(bench_df_grouped_cat.shape[0]),
        )
        ax2.set_yticklabels(list(bench_df_grouped_cat["Dataset_name"]))
        plt.show()
    else:
        label_opt_list = [label_opt[i] for i in bench_df_grouped["Comparison Winner"]]
        print(label_opt_list)
        ax1.set_xticklabels(label_opt_list)
        ax2.set_xticklabels(label_opt_list)
        ax2.set(
            xlabel="Better Performance",
            ylabel="Datasets",
            yticks=np.arange(bench_df_grouped_cat.shape[0]),
        )
        ax2.set_yticklabels(list(bench_df_grouped_cat["Dataset_name"]))
        plt.show()

def visualize_pairvs_2(benchmark_df, branch1, branch2, label_opt=None, epsilon=1e-03):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
        label_opt ([type], optional): [description]. Defaults to None.
        epsilon ([type], optional): [description]. Defaults to 1e-03.
    """
    benchmark_mins, benchmark_min_idx = get_branch_mins(benchmark_df)
    benchmark_mins["Vs_results"] = benchmark_mins.apply(
        lambda x: _label_vs(x, branch1, branch2, epsilon), axis=1
    )
    bench_df_grouped = benchmark_mins.groupby("Vs_results").agg(
        {"Dataset_name": "nunique"}
    )
    bench_df_grouped.reset_index(inplace=True)
    bench_df_grouped.columns = ["Comparison Winner", "Number of Wins"]
    bench_df_grouped_cat = benchmark_mins.groupby(["Dataset_name", "Vs_results"]).agg(
        {"Dataset_name": "nunique"}
    )
    bench_df_grouped_cat.columns = ["Dataset Index"]
    bench_df_grouped_cat["Dataset Index"] = np.arange(bench_df_grouped_cat.shape[0])
    bench_df_grouped_cat.reset_index(inplace=True)
    ax1 = sns.barplot(y="Number of Wins", x="Comparison Winner", data=bench_df_grouped)
    ax1.set_title("Comparison results with significance level σ: {}".format(epsilon))
    ax2 = sns.catplot(
        x="Vs_results", y="Dataset Index", hue="Dataset_name", data=bench_df_grouped_cat
    )

    if label_opt is None:
        ax2.set(
            xlabel="Better Performance",
            ylabel="Datasets",
            yticks=np.arange(bench_df_grouped_cat.shape[0]),
        )
        ax2.set_yticklabels(list(bench_df_grouped_cat["Dataset_name"]))
        ax1.set(yticks=np.arange(0, bench_df_grouped_cat.shape[0], 2))
        plt.show()
    else:
        label_opt_list = [label_opt[i] for i in bench_df_grouped["Comparison Winner"]]

        ax1.set_xticklabels(label_opt_list)
        ax2.set_xticklabels(label_opt_list)
        ax1.set(yticks=np.arange(0, bench_df_grouped_cat.shape[0], 2))
        ax2.set(
            xlabel="Better Performance",
            ylabel="Datasets",
            yticks=np.arange(bench_df_grouped_cat.shape[0]),
        )
        ax2.set_yticklabels(list(bench_df_grouped_cat["Dataset_name"]))
        plt.show()

    return ax1

def visualize_multiple_vs(benchmark_df, branch_list, label_opt=None, epsilon=1e-03):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
        label_opt ([type], optional): [description]. Defaults to None.
        epsilon ([type], optional): [description]. Defaults to 1e-03.
    """
    i = 0
    plt.figure(figsize=(30, 10))
    for eps in [0.01, 0.001, 0.0001]:
        i += 1
        plt.subplot(1, 3, i)
        benchmark_mins, benchmark_min_idx = get_branch_mins(benchmark_df)
        benchmark_mins["Vs_results"] = benchmark_mins.apply(
            lambda x: label_vs_triple(x, branch_list, eps), axis=1
        )
        bench_df_grouped = benchmark_mins.groupby("Vs_results").agg(
            {"Dataset_name": "nunique"}
        )
        bench_df_grouped.reset_index(inplace=True)
        bench_df_grouped.sort_index(inplace=True)
        bench_df_grouped.columns = ["Comparison Winner", "Number of Wins"]
        bench_df_grouped_cat = benchmark_mins.groupby(
            ["Dataset_name", "Vs_results"]
        ).agg({"Dataset_name": "nunique"})
        bench_df_grouped_cat.columns = ["Dataset Index"]
        bench_df_grouped_cat["Dataset Index"] = np.arange(bench_df_grouped_cat.shape[0])
        bench_df_grouped_cat.reset_index(inplace=True)
        ax1 = sns.barplot(
            y="Number of Wins", x="Comparison Winner", data=bench_df_grouped
        )
        ax1.set_title(
            "Comparison results with significance level  \n σ: {}".format(eps),
            fontsize=30,
        )
        # ax2 = sns.catplot(x = 'Vs_results',y ='Dataset Index',hue = 'Dataset_name',data = bench_df_grouped_cat)

        if label_opt is None:
            # ax2.set(xlabel='Better Performance',ylabel = 'Datasets',yticks = np.arange(bench_df_grouped_cat.shape[0]))
            # ax2.set_yticklabels(list(bench_df_grouped_cat['Dataset_name']))
            ax1.set(
                yticks=np.arange(0, bench_df_grouped["Number of Wins"].max() + 2, 2)
            )
            # ax1.set(yticks = np.arange(0,bench_df_grouped_cat.shape[0],2))
            ax1.set_xlabel("Comparison Winner", fontsize=28)
            ax1.set_ylabel("Number of Wins", fontsize=28)
            ax1.tick_params(labelsize=28)
            # plt.show()
        else:
            label_opt_list = [
                label_opt[i] for i in bench_df_grouped["Comparison Winner"]
            ]

            ax1.set_xticklabels(label_opt_list)
            ax1.set_xlabel("Comparison Winner", fontsize=28)
            ax1.set_ylabel("Number of Wins", fontsize=28)
            ax1.tick_params(labelsize=28)
            # ax2.set_xticklabels(label_opt_list)
            ax1.set(
                yticks=np.arange(0, bench_df_grouped["Number of Wins"].max() + 2, 2)
            )
            # ax1.set(yticks = np.arange(0,bench_df_grouped_cat.shape[0],2))
            # ax2.set(xlabel='Better Performance',ylabel = 'Datasets',yticks = np.arange(bench_df_grouped_cat.shape[0]))
            # ax2.set_yticklabels(list(bench_df_grouped_cat['Dataset_name']))
            # plt.show()
    plt.tight_layout()

def _label_vs(row, branch1, branch2, epsilon=1e-03):
    if abs(row[branch1] - row[branch2]) <= epsilon:
        return "Draw"
    else:
        if row[branch1] < row[branch2]:
            return branch1
        elif row[branch1] > row[branch2]:
            return branch2

def label_vs_triple(row, branch_list, epsilon=1e-03):
    branch_array = np.asarray(branch_list)
    benchmark_min_idx = row[branch_list].astype(float).idxmin()
    for el in branch_array[branch_array != benchmark_min_idx]:
        if abs(row[el] - row[benchmark_min_idx]) <= epsilon:
            return "Draw"
    return benchmark_min_idx

def dataset_stats(index_range):
    """[summary]

    Args:
        index_range ([type]): [description]

    Returns:
        [type]: [description]
    """
    dataset_features = {}
    for i in range(index_range):
        X, _, name = datasets.returnDataset(i)
        dataset_features[name] = {"Instances": X.shape[0], "Features": X.shape[1]}

    return dataset_features

def scatter_datasets(benchmark_df, branch1, branch2, index_range=20):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
        index_range (int, optional): [description]. Defaults to 20.
    """
    dataset_dictionary = dataset_stats(index_range)
    bench_b1_vs_b2 = utils.set_branch_best(benchmark_df, branch1, branch2)
    bench_b1_vs_b2["Vs_results"] = bench_b1_vs_b2.apply(
        lambda x: _label_vs(x, branch1, branch2), axis=1
    )
    bench_b1_vs_b2["Instances"] = bench_b1_vs_b2.apply(
        lambda x: dataset_dictionary[x["Dataset_name"]]["Instances"], axis=1
    )
    bench_b1_vs_b2["Features"] = bench_b1_vs_b2.apply(
        lambda x: dataset_dictionary[x["Dataset_name"]]["Features"], axis=1
    )

    sns.scatterplot(x="Instances", y="Features", hue="Vs_results", data=bench_b1_vs_b2)
    return dataset_dictionary

def get_branch_mins(dataset):
    """[summary]

    Args:
        dataset ([type]): [description]
        sigma (float, optional): [description]. Defaults to 0.001.

    Returns:
        [type]: [description]
    """
    branch_mins_df = dataset.groupby("Dataset_name", as_index=False)[
        "B1", "B2", "B4", "B5", "B7", "B8"
    ].min()
    best_performance_idx = dataset.groupby("Dataset_name", as_index=False).agg(
        dict(zip(["B1", "B2", "B4", "B5", "B7", "B8"], ["idxmin"] * 6))
    )
    return branch_mins_df, best_performance_idx

def get_branch_minidx(grouped_df, best_performance_idx, branch1, branch2, sigma=0.001):
    """[summary]

    Args:
        grouped_df ([type]): [description]
        best_performance_idx ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]

    Returns:
        [type]: [description]
    """
    datasets_better = grouped_df[
        (grouped_df[branch1] > grouped_df[branch2])
        & (abs(grouped_df[branch1] - grouped_df[branch2]) > sigma)
    ]
    dataset_better_idx = best_performance_idx[
        best_performance_idx["Dataset_name"].isin(datasets_better["Dataset_name"])
    ]
    return dataset_better_idx.loc[:, ["Dataset_name", branch1, branch2]]

def compare_branch_best_runtime(
    benchmark_df, runtime_df, branch1, branch2, label_opt=None, sigma=0.001
):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        runtime_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
    """
    bcn_n0_c1_mins, bcn_n0_c1_minidx = get_branch_mins(benchmark_df)
    dataset_btt = get_branch_minidx(
        bcn_n0_c1_mins, bcn_n0_c1_minidx, branch1, branch2, sigma
    )
    b1_best_runtime = runtime_df[["Dataset_name", branch1]].loc[
        dataset_btt[branch1].tolist()
    ]
    b2_best_runtime = runtime_df[["Dataset_name", branch2]].loc[
        dataset_btt[branch2].tolist()
    ]
    b1_melt = pd.melt(b1_best_runtime, id_vars="Dataset_name")
    b2_melt = pd.melt(b2_best_runtime, id_vars="Dataset_name")
    branch_ct = pd.concat([b1_melt, b2_melt])
    if label_opt is None:
        ax = sns.catplot(
            y="Dataset_name", x="value", hue="variable", kind="bar", data=branch_ct
        )
        ax.set(
            xlabel="Training completion time(secs)",
            ylabel="Dataset name",
            title="Runtime comparison of {} and {}, for datasets where {} performed better with significance level σ: {}".format(
                branch1, branch2, branch2, sigma
            ),
        )
    else:
        branch_ct["variable"].loc[branch_ct["variable"] == branch1] = label_opt[branch1]
        branch_ct["variable"].loc[branch_ct["variable"] == branch2] = label_opt[branch2]
        ax = sns.catplot(
            y="Dataset_name", x="value", hue="variable", kind="bar", data=branch_ct
        )
        ax.set(
            xlabel="Training completion time(secs)",
            ylabel="Dataset name",
            title="Runtime comparison of {} and {}, for datasets where {} performed better with significance level σ: {}".format(
                label_opt[branch1], label_opt[branch2], label_opt[branch2], sigma
            ),
        )

def compare_branch_best_complexity(
    benchmark_df, complexity_df, branch1, branch2, label_opt=None, sigma=0.001
):
    """[summary]

    Args:
        benchmark_df ([type]): [description]
        runtime_df ([type]): [description]
        branch1 ([type]): [description]
        branch2 ([type]): [description]
    """
    bcn_n0_c1_mins, bcn_n0_c1_minidx = get_branch_mins(benchmark_df)
    dataset_btt = get_branch_minidx(
        bcn_n0_c1_mins, bcn_n0_c1_minidx, branch1, branch2, sigma
    )
    b1_best_complexity = complexity_df[["Dataset_name", (branch1 + "_avg_node")]].loc[
        dataset_btt[(branch1)].tolist()
    ]
    b2_best_complexity = complexity_df[["Dataset_name", (branch2 + "_avg_node")]].loc[
        dataset_btt[(branch2)].tolist()
    ]
    b1_melt = pd.melt(b1_best_complexity, id_vars="Dataset_name")
    b2_melt = pd.melt(b2_best_complexity, id_vars="Dataset_name")
    branch_ct = pd.concat([b1_melt, b2_melt])
    if label_opt is None:
        ax = sns.catplot(
            y="Dataset_name", x="value", hue="variable", kind="bar", data=branch_ct
        )
        ax.set(
            xlabel="Average number of nodes per tree",
            ylabel="Dataset name",
            title="Average node count per tree comparison of {} and {}, for datasets where {} performed better with significance level σ: {}".format(
                branch1, branch2, branch2, sigma
            ),
        )
    else:
        branch_ct["variable"].loc[branch_ct["variable"] == (branch1 + "_avg_node")] = (
            label_opt[branch1] + "_avg_node"
        )
        branch_ct["variable"].loc[branch_ct["variable"] == (branch2 + "_avg_node")] = (
            label_opt[branch2] + "_avg_node"
        )
        ax = sns.catplot(
            y="Dataset_name", x="value", hue="variable", kind="bar", data=branch_ct
        )
        ax.set(
            xlabel="Average number of nodes per tree",
            ylabel="Dataset name",
            title="Average node count per tree comparison of {} and {}, for datasets where {} performed better with significance level σ: {}".format(
                label_opt[branch1], label_opt[branch2], label_opt[branch2], sigma
            ),
        )

max_stars_repo_name: koralpc/shapley-bootstrapping

max_issues_repo_name: koralpc/shapley-bootstrapping

max_forks_repo_name: koralpc/shapley-bootstrapping


text:
# [Acknowledgement] The source codes are adapted from CMU 11-785 Deep Learning course (http://deeplearning.cs.cmu.edu/) with prior consent of Professor Bhiksha Raj.

# Do not import any additional 3rd party external libraries as they will not
# be available to AutoLab and are not needed (or allowed)

import numpy as np

class Conv1D():
    def __init__(self, in_channel, out_channel, kernel_size, stride,
                 weight_init_fn=None, bias_init_fn=None):
        # Do not modify this method
        self.in_channel = in_channel
        self.out_channel = out_channel
        self.kernel_size = kernel_size
        self.stride = stride

        if weight_init_fn is None:
            self.W = np.random.normal(0, 1.0, (out_channel, in_channel, kernel_size))
        else:
            self.W = weight_init_fn(out_channel, in_channel, kernel_size)

        if bias_init_fn is None:
            self.b = np.zeros(out_channel)
        else:
            self.b = bias_init_fn(out_channel)

        self.dW = np.zeros(self.W.shape)
        self.db = np.zeros(self.b.shape)

    def __call__(self, x):
        return self.forward(x)

    def forward(self, x):
        """
        Argument:
            x (np.array): (batch_size, in_channel, input_size)
        Return:
            out (np.array): (batch_size, out_channel, output_size)
        """
        self.batch, __, self.width = x.shape
        assert __ == self.in_channel, 'Expected the inputs to have {} channels'.format(self.in_channel)

        out_width = (self.width - self.kernel_size) // self.stride + 1
        self.out_width = out_width
        self.x = x
        result = np.zeros(shape=[self.batch, self.out_channel, out_width])      # NOTE: do NOT use np.float32

        # DONE:
        #----------------------->
        # Hint:
        # Calculate the output of convolutional layer by using self.W[out_c], self.b[out_c], and x with appropriate matrix slicing
        # Use the .sum() method of a array to get the sum of array elements
        # <---------------------

        for n in range(self.batch):
            for out_c in range(self.out_channel):
                for out_pos in range(out_width):
                    result[n, out_c, out_pos] = np.sum(self.W[out_c] * self.x[n,: , out_pos * self.stride:out_pos * self.stride + self.kernel_size])+ self.b[out_c]

        return result

    def backward(self, delta):
        """
        Argument:
            delta (np.array): (batch_size, out_channel, output_size)
        Return:
            dx (np.array): (batch_size, in_channel, input_size)
        """
        dx = np.zeros(shape=self.x.shape)   # NOTE: do NOT use np.float32, use np.float64 instead

        # DONE:
        #----------------------->
        # Hint:
        # Calculate the the gradients by filling self.kernel_size and self.stride in appropriate places (???)
        # <---------------------
        for n in range(self.batch):
            for out_c in range(self.out_channel):
                for out_pos in range(self.out_width):
                    self.db[out_c] += delta[n, out_c, out_pos] * 1.0
                    self.dW[out_c] += delta[n, out_c, out_pos] * self.x[n, :, out_pos*self.stride:out_pos*self.stride + self.kernel_size]
                    dx[n, :, out_pos*self.stride:out_pos*self.stride + self.kernel_size] += delta[n, out_c, out_pos] * self.W[out_c]

        return dx

class Flatten():
    def __call__(self, x):
        return self.forward(x)

    def forward(self, x):
        """
        Argument:
            x (np.array): (batch_size, in_channel, in_width)
        Return:
            out (np.array): (batch_size, in_channel * in width)
        """
        self.b, self.c, self.w = x.shape

        self.x = x
        return x.reshape(x.shape[0], -1)    # (N, C, W) -> (N, C*W)

    def backward(self, delta):
        """
        Argument:
            delta (np.array): (batch size, in channel * in width)
        Return:
            dx (np.array): (batch size, in channel, in width)
        """
        return np.reshape(delta, self.x.shape)

max_stars_repo_name: BobAnkh/MaC

max_issues_repo_name: BobAnkh/MaC

max_forks_repo_name: BobAnkh/MaC


text:
import numpy as np
from pyscf import cc
from pydmfet import tools, qcwrap
from functools import reduce

CHARGE_OF = 0

class LocInts:

    def __init__(self, mol, the_mf):

        self.mol = mol
        self.mf = the_mf
        self.Nelec = int(np.rint(np.sum(self.mf.mo_occ)))
        self.NOrb = self.mol.nao_nr()

        self.s1e_ao = self.mf.get_ovlp()
        self.hcore_ao = self.mf.get_hcore()
        self.fock_ao = self.mf.get_fock()

        self.vnuc_ao = self.hcore_ao - self.t_ao

    def energy_nuc(self):

        return self.mol.energy_nuc()

    def vnuc_loc(self):

        vnuc = reduce(np.dot, (self.ao2loc.T, self.vnuc_ao, self.ao2loc) )
        return vnuc

    def hcore_loc( self ):

        oei = reduce(np.dot, (self.ao2loc.T, self.hcore_ao, self.ao2loc) )
        return oei

    def fock_loc(self):

        fock = reduce(np.dot, (self.ao2loc.T, self.fock_ao, self.ao2loc) )
        return fock

    def frag_kin_loc(self):

        t_loc = reduce( np.dot, (self.ao2loc.T, self.t_ao, self.ao2loc) )
        return t_loc

    def build_1pdm_loc(self):

        NOcc = self.Nelec/2  #closed shell
        fock = self.fock_loc()
        onedm, mo_coeff = tools.fock2onedm(fock, NOcc)

        #fullDMao = self.mf.make_rdm1()
        #dm_loc = tools.dm_ao2loc(fullDMao, self.s1e_ao, self.ao2loc)
        #print 'dm_loc - dm_ao2loc(dm_ao) = ',np.linalg.norm(onedm-dm_loc)

        return (onedm, mo_coeff)

    def frag_mol_ao(self, impAtom):

        mol = self.mol
        NAtom = mol.natm

        mol1 = mol.copy()

        for i in range(NAtom):
            if(impAtom[i] == 0):
                mol1._atm[i,CHARGE_OF] = 0 #make environment atoms 0 charge

                symbol = mol1._atom[i][0]
                coord = mol1._atom[i][1]
                mol1._atom[i] = tuple(['ghost_'+symbol,coord]) #write symbol

        return mol1

    def bound_mol_ao(self, boundary_atoms):

        mol = self.mol
        NAtom = mol.natm

        mol1 = mol.copy()

        for i in range(NAtom):
            mol1._atm[i,CHARGE_OF] = boundary_atoms[i]
            if (boundary_atoms[i] == 0) :
                coord = mol1._atom[i][1]
                mol1._atom[i] = tuple(['ghost',coord])

        return mol1

    def debug_loc_orb(self):

        #fock = self.fock_loc()
        #eigvals, eigvecs = np.linalg.eigh(fock)
        ##eigvecs = eigvecs[ :, eigvals.argsort() ]
        #print eigvals

        oei = self.hcore_loc()
        tei = self.tei_loc()

        Ne = self.Nelec
        NOrb = self.NOrb
        mf = qcwrap.qc_scf(Ne, NOrb, 'hf', mol=self.mol, oei=oei, tei=tei)
        mf.runscf()

        #print mf.elec_energy
        mycc = cc.CCSD(mf).run()
        et = 0.0
        et = mycc.ccsd_t()
        e_hf = mf.elec_energy + self.energy_nuc()
        print (e_hf)
        print (mycc.e_corr + et)

        e_ccsd = e_hf + mycc.e_corr + et
        print ('total ccsd(t) energy = ',e_ccsd)
        exit()

    def check_loc_ortho( self ):

        ShouldBeI = reduce(np.dot, (self.ao2loc.T, self.s1e_ao, self.ao2loc) )
        return np.linalg.norm( ShouldBeI - np.eye( ShouldBeI.shape[0] ) )

max_stars_repo_name: fishjojo/pydmfe

max_issues_repo_name: fishjojo/pydmfet

max_forks_repo_name: fishjojo/pydmfet


text:
import concur as c
import numpy as np

@c.testing.benchmark_widget
def test_polylines_perf():
    side, length = int(np.sqrt(10000)), 4
    x, y, t = np.meshgrid(np.linspace(0, 1, side), np.linspace(0, 1, side), np.linspace(0, 2 * np.pi, length, endpoint=False))
    polylines = np.stack([np.sin(t) * 1/side/2 + x, np.cos(t) * 1/side/2 + y], axis=3).reshape(-1, length, 2)
    im = c.Image()

    def content(tf):
        return c.draw.polylines(polylines, 'white', tf=tf)
    while True:
        yield from c.image("Image", im, content_gen=content)
        yield

@c.testing.test_widget
def test_scatter(tester):
    def content(tf):
        np.random.seed(0)
        return c.orr([
            c.draw.scatter(np.random.rand(100, 2), 'white', '+', thickness=1, tf=tf),
            c.draw.scatter(np.random.rand(100, 2), 'yellow', 'x', thickness=1, tf=tf),
            c.draw.scatter(np.random.rand(100, 2), 'magenta', 'o', thickness=1, tf=tf),
            c.draw.scatter(np.random.rand(100, 2), 'green', '.', marker_size=3, tf=tf),
            ])
    yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])

@c.testing.test_widget
def test_shapes(tester):
    def content(tf):
        np.random.seed(0)
        return c.orr([
            # Empty polygonal shapes of all kinds
            c.draw.polygon([], 'white', tf=tf),
            c.draw.polygon(np.array([]), 'white', tf=tf),
            c.draw.polygon(np.zeros((0, 2)), 'white', tf=tf),
            c.draw.polyline([], 'white', tf=tf),
            c.draw.polyline(np.array([]), 'white', tf=tf),
            c.draw.polyline(np.zeros((0, 2)), 'white', tf=tf),
            c.draw.polygons(np.zeros((0, 123, 2)), 'white', tf=tf),
            c.draw.polygons(np.zeros((123, 0, 2)), 'white', tf=tf),
            c.draw.polylines([], 'white', tf=tf),
            c.draw.polylines(np.zeros((0, 123, 2)), 'white', tf=tf),
            c.draw.polylines(np.zeros((123, 0, 2)), 'white', tf=tf),
            c.draw.scatter(np.array([]), 'white', 'x', tf=tf),
            c.draw.scatter(np.zeros([0]), 'white', 'x', tf=tf),
            c.draw.scatter(np.zeros([0,2]), 'white', 'x', tf=tf),

            # Normal polygonal shapes
            c.draw.polygon(np.array([(0.5,0.5), (0.7,0.5), (0.7,0.7), (0.5,0.7)]), 'white', tf=tf),
            c.draw.polygon([(0.5,0.5), (0.6,0.5), (0.6,0.6), (0.5,0.6)], 'brown', tf=tf),
            ])
    yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])

max_stars_repo_name: ggrrin/python-concur

max_issues_repo_name: ggrrin/python-concur

max_forks_repo_name: ggrrin/python-concur


text:
#coding=utf-8
import numpy as np
import tensorflow as tf

import cv2

def getModel():
    input = tf.keras.layers.Input(shape=[16, 66, 3])  # change this shape to [None,None,3] to enable arbitraty shape input
    x = tf.keras.layers.Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input)
    x = tf.keras.layers.Activation("relu", name='relu1')(x)
    x = tf.keras.layers.MaxPool2D(pool_size=2)(x)
    x = tf.keras.layers.Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(x)
    x = tf.keras.layers.Activation("relu", name='relu2')(x)
    x = tf.keras.layers.Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x)
    x = tf.keras.layers.Activation("relu", name='relu3')(x)
    x = tf.keras.layers.Flatten()(x)
    output = tf.keras.layers.Dense(2,name = "dense")(x)
    output = tf.keras.layers.Activation("relu", name='relu4')(output)
    model = tf.keras.Model([input], [output])
    return model

model = getModel()
model.load_weights("./model/model12.h5")

def getmodel():
    return model

def gettest_model():
    input = tf.keras.layers.Input(shape=[16, 66, 3])  # change this shape to [None,None,3] to enable arbitraty shape input
    A = tf.keras.layers.Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input)
    B = tf.keras.layers.Activation("relu", name='relu1')(A)
    C = tf.keras.layers.MaxPool2D(pool_size=2)(B)
    x = tf.keras.layers.Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(C)
    x = tf.keras.layers.Activation("relu", name='relu2')(x)
    x = tf.keras.layers.Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x)
    K = tf.keras.layers.Activation("relu", name='relu3')(x)

    x = tf.keras.layers.Flatten()(K)
    dense = tf.keras.layers.Dense(2,name = "dense")(x)
    output = tf.keras.layers.Activation("relu", name='relu4')(dense)
    x = tf.keras.Model([input], [output])
    x.load_weights("./model/model12.h5")
    ok = tf.keras.Model([input], [dense])

    return ok

def finemappingVertical(image):
    resized = cv2.resize(image,(66,16))
    resized = resized.astype(np.float)/255
    res = model.predict(np.array([resized]))[0]
    res = res * image.shape[1]
    res = res.astype(np.int)
    H, T = res
    H -= 3

    if H < 0:
        H = 0
    T += 2;

    if T >= image.shape[1]-1:
        T = image.shape[1]-1

    image = image[0:35, H:T+2]

    image = cv2.resize(image, (int(136), int(36)))
    return image