text:
import asyncio
import io
import re
import traceback
import logging
import sys
import inspect

# handy libs to have for eval()
import datetime
import time
import requests
import sympy
import os
import re
import random
import math
import json

from bot import alemiBot

from pyrogram import filters

from util.command import filterCommand
from util.parse import cleartermcolor
from util.message import tokenize_json, tokenize_lines, is_me, edit_or_reply
from util.serialization import convert_to_dict
from util.permission import is_superuser
from plugins.help import HelpCategory

logger = logging.getLogger(__name__)

class GlobalThings():
    def __str__(self):
        return str(convert_to_dict(self))

GLOBALS = GlobalThings()

class stdoutWrapper():
    def __init__(self):
        self.buffer = io.StringIO()
        self.old_stdout = sys.stdout
        self.old_stderr = sys.stderr

    def __enter__(self):
        sys.stdout = self.buffer
        sys.stderr = self.buffer
        return self.buffer

    def __exit__(self, exc_type, exc_value, exc_traceback):
        sys.stdout = self.old_stdout
        sys.stderr = self.old_stderr

HELP = HelpCategory("EXECUTION")

HELP.add_help(["run", "r"], "run command on server",
                "runs a command on server. Shell will be from user running bot. " +
                "Every command starts in bot root folder.", args="<cmd>")
@alemiBot.on_message(is_superuser & filterCommand(["run", "r"], list(alemiBot.prefixes)))
async def runit(client, message):
    args = re.sub(r"-delme(?: |)(?:[0-9]+|)", "", message.command["raw"])
    msg = await edit_or_reply(message, "` → ` Running")
    try:
        logger.info(f"Running command \"{args}\"")
        proc = await asyncio.create_subprocess_shell(
            args,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.STDOUT)

        stdout, stderr = await proc.communicate()
        output = cleartermcolor(stdout.decode())
        if len(args) + len(output) > 4080:
            await msg.edit(f"```$ {args}\n → Output too long, sending as file```")
            out = io.BytesIO((f"$ {args}\n" + output).encode('utf-8'))
            out.name = "output.txt"
            await client.send_document(message.chat.id, out)
        else:
            await msg.edit(tokenize_lines(f"$ {args}\n\n" + output, mode='html'), parse_mode='html')
    except Exception as e:
        traceback.print_exc()
        await msg.edit(f"`$ {args}`\n`[!] → ` " + str(e))

HELP.add_help(["eval", "e"], "eval a python expression",
                "eval a python expression. No imports can be made nor variables can be " +
                "assigned. Some common libs are already imported. `eval` cannot have side effects. " +
                "Returned value will be printed upon successful evaluation. `stdout` won't be captured (use `.ex`). If " +
                "a coroutine is returned, it will be awaited. Use the GLOBALS object for persistence: no assignation can " +
                "be done in `eval`, but getting fields is possible. This won't tokenize large outputs per-line, " +
                "use .ex if you need that.", args="<expr>")
@alemiBot.on_message(is_superuser & filterCommand(["eval", "e"], list(alemiBot.prefixes)))
async def evalit(client, message):
    global GLOBALS
    args = re.sub(r"-delme(?: |)(?:[0-9]+|)", "", message.command["raw"])
    msg = await edit_or_reply(message, "` → ` Evaluating")
    try:
        logger.info(f"Evaluating \"{args}\"")
        result = eval(args)
        if inspect.iscoroutine(result):
            result = await result
        if len(args) + len(str(result)) > 4080:
            await msg.edit(f"```>>> {args}\n → Output too long, sending as file```")
            out = io.BytesIO((f">>> {args}\n" + str(result)).encode('utf-8'))
            out.name = "output.txt"
            await client.send_document(message.chat.id, out, parse_mode="markdown")
        else:
            await msg.edit(f"`>>>` `{args}`\n` → ` `" + str(result) + "`", parse_mode="markdown")
    except Exception as e:
        traceback.print_exc()
        await msg.edit(f"`>>>` `{args}`\n`[!] → ` " + str(e), parse_mode='markdown')

async def aexec(code, client, message): # client and message are passed so they are in scope
    global GLOBALS
    exec(
        f'async def __aex(): ' +
        ''.join(f'\n {l}' for l in code.split('\n')),

        locals()
    )
    return await locals()['__aex']()

HELP.add_help(["exec", "ex"], "execute python code",
                "execute python code. This, unlike `eval`, has no bounds and " +
                "**can have side effects**. Use with more caution than `eval`. " +
                "`exec` always returns `None`, but anything printed to `stdout` " +
                "will be shown. You can set anything in the GLOBALS object for " +
                "persistence. The `exec` call is wrapped to make it work with async " +
                "code.", args="<code>")
@alemiBot.on_message(is_superuser & filterCommand(["exec", "ex"], list(alemiBot.prefixes)))
async def execit(client, message):
    args = re.sub(r"-delme(?: |)(?:[0-9]+|)", "", message.command["raw"])
    fancy_args = args.replace("\n", "\n... ")
    msg = message if is_me(message) else await message.reply("`[PLACEHOLDER]`")
    await msg.edit("```" + fancy_args + "```\n` → ` Executing")
    try:
        logger.info(f"Executing \"{args}\"")
        with stdoutWrapper() as fake_stdout:
            await aexec(args, client, message)
        result = fake_stdout.getvalue()
        if len(args) + len(result) > 4080:
            await msg.edit(f"```>>> {fancy_args}\n → Output too long, sending as file```")
            out = io.BytesIO((f">>> {fancy_args}\n" + result).encode('utf-8'))
            out.name = "output.txt"
            await client.send_document(message.chat.id, out, parse_mode='markdown')
        else:
            await msg.edit(tokenize_lines(f">>> {fancy_args}\n\n" + result), parse_mode='markdown')
    except Exception as e:
        traceback.print_exc()
        await msg.edit(f"`>>> {args}`\n`[!] → ` " + str(e), parse_mode='markdown')

max_stars_repo_name: reloonfire/alemibot

max_issues_repo_name: reloonfire/alemibot

max_forks_repo_name: reloonfire/alemibot

reason: import sympy


text:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

import requests
import numpy as np
import pandas as pd

# ------------------------------------------------------------------------------

df = pd.read_csv('https://api.covid19india.org/csv/latest/states.csv')
df['Active'] = df['Confirmed'] - df['Recovered'] - df['Deceased'] - df['Other']
df['Date'] = pd.to_datetime(df['Date'])

r = requests.get(url='https://raw.githubusercontent.com/geohacker/india/master/state/india_telengana.geojson')
geojson = r.json()
st = pd.read_csv('https://api.covid19india.org/csv/latest/state_wise.csv')

st_map = st.copy()

st_map['State'] = st_map['State'].apply(lambda x: x.replace('Odisha', 'Orissa') if 'Odisha' in str(x) else x)
st_map['State'] = st_map['State'].apply(
    lambda x: x.replace('Uttarakhand', 'Uttaranchal') if 'Uttarakhand' in str(x) else x)
st_map['State'] = st_map['State'].apply(
    lambda x: x.replace('Andaman and Nicobar Islands', 'Andaman and Nicobar') if 'Andaman and Nicobar Islands' in str(
        x) else x)

st_map = st_map.iloc[:, :5]

st_map.drop([0, 31], inplace=True)

st_map.reset_index(drop=True, inplace=True)

States = []
for i in np.sort(df['State'].unique()):
    States.append({'label': i, 'value': i})

color = {'Confirmed': px.colors.sequential.Reds,
         'Active': px.colors.sequential.Blues,
         'Recovered': px.colors.sequential.Greens,
         'Deaths': px.colors.sequential.Greys}

# ------------------------------------------------------------------------------

app = dash.Dash(__name__)
server = app.server

app.layout = html.Div([
    html.H1(children='COVID-19 India Dashboard',
            style={'textAlign': 'center', 'color': '#0062ff'}),

    html.Div([

        dcc.Graph(id='indicator'),

        html.Label(['States & Union Territories:'], style={ "text-align": "center"}),

        dcc.Dropdown(id='my_dropdown', options=States, optionHeight=35, value='India',
                     disabled=False, multi=False, searchable=True, search_value='India',
                     placeholder='Select a State or Union Territory', clearable=False, style={'width': "50%"}),

        dcc.Graph(id='donut'),

        dcc.Graph(id='our_graph'),

        dcc.Graph(id='our_graph2')
    ]),

    html.Div([
        html.Label(['Status:'], style={'font-weight': 'bold', "text-align": "center"}),

        dcc.RadioItems(
            id='candidate',
            options=[{'value': x, 'label': x} for x in st_map.columns[1:]],
            value='Confirmed',
            labelStyle={'display': 'inline-block'}),

        dcc.Graph(id='choro'),

    ]),

])

# ------------------------------------------------------------------------------

@app.callback(
    Output(component_id='indicator', component_property='figure'),
    Output(component_id='donut', component_property='figure'),
    Output(component_id='our_graph', component_property='figure'),
    Output(component_id='our_graph2', component_property='figure'),
    [Input(component_id='my_dropdown', component_property='value')]
)
def build_graph(column_chosen):
    df1 = df[df['State'] == column_chosen]
    df1['Daily Confirmed'] = df1['Confirmed'].diff()
    df1['Daily Active'] = df1['Active'].diff()
    df1['Daily Recovered'] = df1['Recovered'].diff()
    df1['Daily Deceased'] = df1['Deceased'].diff()
    df1['Daily Tested'] = df1['Tested'].diff()
    df1['Positivity'] = (df1['Daily Confirmed'] / df1['Daily Tested']) * 100

    dfm = df[df['State'] == column_chosen]

    data1 = [go.Indicator(mode="number+delta", value=dfm.iloc[-1, 2],
                          delta={'position': "bottom", 'reference': (dfm.iloc[-2, 2])},
                          title={"text": 'Confirmed'},
                          domain={'x': [0, 0.25], 'y': [0, 1]}),

             go.Indicator(mode="number+delta", value=dfm.iloc[-1, 3],
                          delta={'position': "bottom", 'reference': (dfm.iloc[-2, 3])},
                          title={"text": 'Recovered'},
                          domain={'x': [0.25, 0.5], 'y': [0, 1]}),

             go.Indicator(mode="number+delta", value=dfm.iloc[-1, 4],
                          delta={'position': "bottom", 'reference': (dfm.iloc[-2, 4])},
                          title={"text": 'Deceased'},
                          domain={'x': [0.5, 0.75], 'y': [0, 1]}),

             go.Indicator(mode="number+delta", value=dfm.iloc[-1, 7],
                          delta={'position': "bottom", 'reference': (dfm.iloc[-2, 7])},
                          title={"text": 'Active'},
                          domain={'x': [0.75, 1], 'y': [0, 1]})]

    fig1 = go.Figure(data=data1)
    fig1.update_layout(paper_bgcolor="lightgray", height=250, width=1320)

    labels = ['Active', 'Recovered', 'Deceased']

    recovered = dfm.iloc[-1, 3]
    deceased = dfm.iloc[-1, 4]
    active = dfm.iloc[-1, 7]

    values = [active, recovered, deceased]

    colors = ['#66b3ff', '#02bf47', '#db1200']

    fig_d = make_subplots(rows=1, cols=2, specs=[[{'type': 'domain'}, {"type": "xy"}]],
                          subplot_titles=("Current Status", "Test Positivity Ratio"))

    fig_d.add_trace(go.Pie(labels=labels, values=values, name="GHG Emissions", hole=.4, hoverinfo="label+percent",
                           marker=dict(colors=colors, line=dict(color='#ffffff', width=2))), row=1, col=1)

    fig_d.add_trace(
        go.Scatter(name="Postivity Ratio", x=df1['Date'], y=df1['Positivity'], mode='lines', marker_color='#ff4714', ),
        row=1, col=2)

    fig_d.update_layout(showlegend=False,
                        title="Current Status in {}".format(column_chosen),
                        title_x=0.2,
                        annotations=[dict(text=column_chosen, x=0.225, y=0.46, font_size=16, showarrow=False),
                                     dict(text='Test Positivity Ratio of {}'.format(column_chosen), x=0.75, y=1,
                                          font_size=17, showarrow=False)])

    data2 = [go.Scatter(name="Confirmed", x=df1['Date'], y=df1['Confirmed'], marker_color='Red'),
             go.Scatter(name="Active", x=df1['Date'], y=df1['Active'], marker_color='Blue'),
             go.Scatter(name='Recovered', x=df1['Date'], y=df1['Recovered'], marker_color='Green'),
             go.Scatter(name='Deceased', x=df1['Date'], y=df1['Deceased'], marker_color='Grey')]

    layout2 = go.Layout(
        title='Cumulative Graph of {}'.format(column_chosen),
        title_x=0.5,
        yaxis={'title': 'No of cases'},
        xaxis={'title': 'Date'},
        font=dict(size=14))
    fig2 = go.Figure(data=data2, layout=layout2)

    data3 = [go.Bar(name="Confirmed", x=df1['Date'], y=df1['Daily Confirmed'], marker_color='Red'),
             go.Bar(name="Active", x=df1['Date'], y=df1['Daily Active'], marker_color='Blue'),
             go.Bar(name="Recovered", x=df1['Date'], y=df1['Daily Recovered'], marker_color='Green'),
             go.Bar(name="Deceased", x=df1['Date'], y=df1['Daily Deceased'], marker_color='Grey')]

    layout3 = go.Layout(
        plot_bgcolor='#000000',
        template='plotly',
        title='Daily Graph of {}'.format(column_chosen),
        title_x=0.5,
        yaxis={'title': 'No of cases'},
        xaxis={'title': 'Date'},
        font=dict(size=14))
    fig3 = go.Figure(data=data3, layout=layout3)

    return fig1, fig_d, fig2, fig3

# ------------------------------------------------------------------------------

@app.callback(
    Output('choro', "figure"),
    [Input('candidate', "value")])
def display_choropleth(status):
    fig1 = px.choropleth(st_map, geojson=geojson, color=status,
                         locations="State", featureidkey="properties.NAME_1",
                         hover_name='State',
                         hover_data=['Confirmed', 'Recovered', 'Deaths', 'Active'],
                         color_continuous_scale=color[status],
                         title='India: Total {} cases per state'.format(status)
                         )
    fig1.update_geos(fitbounds="locations", visible=False)
    fig1.update_geos(projection_type="orthographic")
    fig1.update_layout(height=600, margin={"r": 0, "t": 30, "l": 0, "b": 30})

    return fig1

# ------------------------------------------------------------------------------

if __name__ == '__main__':
    app.run_server(debug=True)

ext: py

reason: import numpy


text:
import numpy as np
import random
from collections import deque

from models.direction import Directions
from models.tile import Tile

from core.logging import log

DISTANCE_PENALTY_FOR_PLAYER = 2

class Pathfinder:
    def __init__(self, state, origin, safety_advisor, blocking_tiles=None):
        if blocking_tiles is None:
            blocking_tiles = [Tile.WALL, Tile.BLOCK]

        self.state = state
        self.max_distance = state.bounds.shape[0] * state.bounds.shape[1]
        self.safety_advisor = safety_advisor
        self.distance_matrix = np.full(state.bounds.shape, self.max_distance)

        self.distance_matrix[origin] = 0

        positions_to_explore = deque([origin])
        while len(positions_to_explore) > 0:
            position_to_explore = positions_to_explore.pop()

            for direction in Directions.tolist():
                next_position = position_to_explore.apply(direction)
                next_distance = self.distance_matrix[position_to_explore] + 1

                if state.bounds.contains(next_position) and \
                        state.tiles[next_position] not in blocking_tiles:
                    bomb_at_next_position = next((bomb for bomb in state.bombs if bomb.position == next_position), None)
                    if bomb_at_next_position:
                        next_distance += bomb_at_next_position.countdown
                    else:
                        player_at_next_position = next((player for player in state.other_players if player.position == next_position), None)
                        if player_at_next_position:
                            next_distance += DISTANCE_PENALTY_FOR_PLAYER

                    if self.distance_matrix[next_position] > next_distance and self.safety_advisor.is_safe(next_position, next_distance):
                        self.distance_matrix[next_position] = next_distance
                        positions_to_explore.appendleft(next_position)

    def get_distance_to(self, destination):
        return self.distance_matrix[destination]

    def get_closest_position(self, positions):
        shuffled_positions = random.sample(positions, len(positions))
        distances = [self.get_distance_to(position) for position in shuffled_positions]

        return shuffled_positions[np.argmin(distances)]

    def get_path_to(self, destination):
        path = [destination]
        position_to_explore = destination
        distance_left = self.distance_matrix[destination]
        if distance_left == self.max_distance:
            return None

        while distance_left > 1:
            neighbours_positions = [position_to_explore.apply(direction) for direction in Directions.tolist()]
            valid_neighbours_positions = [neighbour_position for neighbour_position in neighbours_positions if self.state.bounds.contains(neighbour_position)]
            neighbours_distances = [self.distance_matrix[position] for position in valid_neighbours_positions]

            best_neighbour = valid_neighbours_positions[np.argmin(neighbours_distances)]
            path.append(best_neighbour)
            position_to_explore = best_neighbour
            distance_left = self.distance_matrix[best_neighbour]

        path.reverse()

        return path

max_stars_repo_name: Guillaume-Docquier/bomberjam-bot

max_issues_repo_name: Guillaume-Docquier/bomberjam-bot

max_forks_repo_name: Guillaume-Docquier/bomberjam-bot


text:
from netCDF4 import Dataset

import numpy as np

import pandas as pd

import pylab as pl

import scipy.io

import scipy as spy

import sys

import canyon_tools.readout_tools as rout

import canyon_tools.metrics_tools as mpt

### -----------------------------------------------------------------------------------------------------------------------------------

def main():

  expPath = sys.argv[1]
  run = sys.argv[2]
  trac = sys.argv[3]
  exp = sys.argv[4]

  Grid1, GridOut1, State1,StateOut1,Ptracers1, PtracersOut1 = mpt.getDatasets(expPath, run)

  nx = 360
  ny = 360
  nz = 90

  rc = GridOut1.variables['RC']
  xc = rout.getField(Grid1, 'XC') # x coords tracer cells
  yc = rout.getField(Grid1, 'YC') # y coords tracer cells

  drF = GridOut1.variables['drF'] # vertical distance between faces
  dxF = rout.getField(Grid1,'dxF')
  dyF = rout.getField(Grid1,'dyF')

  MaskCan = rout.getMask(Grid1,'HFacC')
  hFacCCan = rout.getField(Grid1,'HFacC')
  rACan = rout.getField(Grid1,'rA')
  drFCan=GridOut1.variables['drF']
  time = StateOut1.variables['T']
  nt = len(time)
  print('Finished reading grid variables')

  #Transect definitions (indices x,y,z,t)

  CS1 = [0,40,227,227,0,29,0,nt]
  CS2 = [40,120,227,227,0,29,0,nt]
  CS3 = [120,240,267,267,0,29,0,nt]
  CS3sb = [120,240,227,227,0,29,0,nt]
  CS4 = [240,320,227,227,0,29,0,nt]
  CS5 = [320,359,227,227,0,29,0,nt]
  AS1 = [120,120,227,267,0,29,0,nt]
  AS2 = [240,240,227,267,0,29,0,nt]
  LID1 = [120,180,227,267,29,29,0,nt]
  LID2 = [180,240,227,267,29,29,0,nt]

  #day = [0.0,0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5, 5., 5.5,  6., 6.5,  7., 7.5,  8., 8.5]
  deltat = 86400.0/(time[2]-time[1])
  print(deltat)
  day = np.linspace(0,nt/deltat,nt)
  print(day)
  velfile = State1

  keyw = 'W'
  keyv = 'V'
  keyu = 'U'

  W = rout.getField(velfile,keyw)
  UT = rout.getField(velfile,keyu)
  VT = rout.getField(velfile,keyv)
  U,V = rout.unstagger(UT,VT)

  Tr =  rout.getField(Ptracers1,trac)
  target = Tr[0,30,50,180] # threshold concentration

  #get masks
  MaskHC_CS1 = mpt.maskHC(target,Tr,CS1[0],CS1[1],CS1[2],CS1[3],CS1[4],CS1[5],CS1[6],CS1[7])# mask cells along slice depending on their tracer concentration
  MaskHC_CS2 = mpt.maskHC(target,Tr,CS2[0],CS2[1],CS2[2],CS2[3],CS2[4],CS2[5],CS2[6],CS2[7])# mask cells along slice depending on their tracer concentration
  MaskHC_CS3 = mpt.maskHC(target,Tr,CS3[0],CS3[1],CS3[2],CS3[3],CS3[4],CS3[5],CS3[6],CS3[7])# mask cells along slice depending on their tracer concentration
  MaskHC_CS4 = mpt.maskHC(target,Tr,CS4[0],CS4[1],CS4[2],CS4[3],CS4[4],CS4[5],CS4[6],CS4[7])# mask cells along slice depending on their tracer concentration
  MaskHC_CS5 = mpt.maskHC(target,Tr,CS5[0],CS1[1],CS1[2],CS1[3],CS1[4],CS1[5],CS1[6],CS1[7])# mask cells along slice depending on their tracer concentration
  MaskHC_CS3sb = mpt.maskHC(target,Tr,CS3sb[0],CS3sb[1],CS3sb[2],CS3sb[3],CS3sb[4],CS3sb[5],CS3sb[6],CS3sb[7])# mask cells along slice depending on their tracer concentration
  MaskHC_AS1 = mpt.maskHC(target,Tr,AS1[0],AS1[1],AS1[2],AS1[3],AS1[4],AS1[5],AS1[6],AS1[7])# mask cells along slice depending on their tracer concentration
  MaskHC_AS2 = mpt.maskHC(target,Tr,AS2[0],AS2[1],AS2[2],AS2[3],AS2[4],AS2[5],AS2[6],AS2[7])# mask cells along slice depending on their tracer concentration
  MaskHC_LID1 = mpt.maskHC(target,Tr,LID1[0],LID1[1],LID1[2],LID1[3],LID1[4],LID1[5],LID1[6],LID1[7])# mask cells along slice depending on their tracer concentration
  MaskHC_LID2 = mpt.maskHC(target,Tr,LID2[0],LID2[1],LID2[2],LID2[3],LID2[4],LID2[5],LID2[6],LID2[7])# mask cells along slice depending on their tracer concentration

  #Get slices
  V_CS1a = mpt.slice_TRAC(V,CS1[0],CS1[1],CS1[2],CS1[3],CS1[4],CS1[5],CS1[6],CS1[7])
  V_CS2a = mpt.slice_TRAC(V,CS2[0],CS2[1],CS2[2],CS2[3],CS2[4],CS2[5],CS2[6],CS2[7])
  V_CS3a = mpt.slice_TRAC(V,CS3[0],CS3[1],CS3[2],CS3[3],CS3[4],CS3[5],CS3[6],CS3[7])
  V_CS4a = mpt.slice_TRAC(V,CS4[0],CS4[1],CS4[2],CS4[3],CS4[4],CS4[5],CS4[6],CS4[7])
  V_CS5a = mpt.slice_TRAC(V,CS5[0],CS5[1],CS5[2],CS5[3],CS5[4],CS5[5],CS5[6],CS5[7])
  V_CS3sba = mpt.slice_TRAC(V,CS3sb[0],CS3sb[1],CS3sb[2],CS3sb[3],CS3sb[4],CS3sb[5],CS3sb[6],CS3sb[7])
  U_AS1a = mpt.slice_TRAC(U,AS1[0],AS1[1],AS1[2],AS1[3],AS1[4],AS1[5],AS1[6],AS1[7])
  U_AS2a = mpt.slice_TRAC(U,AS2[0],AS2[1],AS2[2],AS2[3],AS2[4],AS2[5],AS2[6],AS2[7])
  W_LID1a = mpt.slice_TRAC(W,LID1[0],LID1[1],LID1[2],LID1[3],LID1[4],LID1[5],LID1[6],LID1[7])
  W_LID2a = mpt.slice_TRAC(W,LID2[0],LID2[1],LID2[2],LID2[3],LID2[4],LID2[5],LID2[6],LID2[7])

  #Get areas
  V_CS1area = mpt.slice_area( dxF,drFCan,rACan,hFacCCan,CS1[0],CS1[1],CS1[2],CS1[3],CS1[4],CS1[5])
  V_CS2area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,CS2[0],CS2[1],CS2[2],CS2[3],CS2[4],CS2[5])
  V_CS3area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,CS3[0],CS3[1],CS3[2],CS3[3],CS3[4],CS3[5])
  V_CS4area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,CS4[0],CS4[1],CS4[2],CS4[3],CS4[4],CS4[5])
  V_CS5area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,CS5[0],CS5[1],CS5[2],CS5[3],CS5[4],CS5[5])
  V_CS3sbarea = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,CS3sb[0],CS3sb[1],CS3sb[2],CS3sb[3],CS3sb[4],CS3sb[5])
  U_AS1area = mpt.slice_area(  dyF,drFCan,rACan,hFacCCan,AS1[0],AS1[1],AS1[2],AS1[3],AS1[4],AS1[5])
  U_AS2area = mpt.slice_area(  dyF,drFCan,rACan,hFacCCan,AS2[0],AS2[1],AS2[2],AS2[3],AS2[4],AS2[5])
  W_LID1area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,LID1[0],LID1[1],LID1[2],LID1[3],LID1[4],LID1[5])
  W_LID2area = mpt.slice_area(  dxF,drFCan,rACan,hFacCCan,LID2[0],LID2[1],LID2[2],LID2[3],LID2[4],LID2[5])

  #add up
  V_CS1 = np.sum(np.sum(np.ma.masked_array(V_CS1a*V_CS1area,mask=MaskHC_CS1),axis=1),axis=1)
  V_CS2 = np.sum(np.sum(np.ma.masked_array(V_CS2a*V_CS2area,mask=MaskHC_CS2),axis=1),axis=1)
  V_CS3 = np.sum(np.sum(np.ma.masked_array(V_CS3a*V_CS3area,mask=MaskHC_CS3),axis=1),axis=1)
  V_CS4 = np.sum(np.sum(np.ma.masked_array(V_CS4a*V_CS4area,mask=MaskHC_CS4),axis=1),axis=1)
  V_CS5 = np.sum(np.sum(np.ma.masked_array(V_CS5a*V_CS5area,mask=MaskHC_CS5),axis=1),axis=1)
  V_CS3sb = np.sum(np.sum(np.ma.masked_array(V_CS3sba*V_CS3sbarea,mask=MaskHC_CS3sb),axis=1),axis=1)
  U_AS1 = np.sum(np.sum(np.ma.masked_array(U_AS1a*U_AS1area,mask=MaskHC_AS1),axis=1),axis=1)
  U_AS2 = np.sum(np.sum(np.ma.masked_array(U_AS2a*U_AS2area,mask=MaskHC_AS2),axis=1),axis=1)
  W_LID1 = np.sum(np.sum(np.ma.masked_array(W_LID1a*W_LID1area,mask=MaskHC_LID1),axis=2),axis=1)
  W_LID2 = np.sum(np.sum(np.ma.masked_array(W_LID2a*W_LID2area,mask=MaskHC_LID2),axis=2),axis=1)

  print(np.shape(day), np.shape(W_LID1))

  raw_data = {'day':day, 'CS1': V_CS1, 'CS2': V_CS2, 'CS3': V_CS3, 'CS3sb': V_CS3sb, 'CS4': V_CS4, 'CS5': V_CS5, 'AS1':U_AS1, 'AS2': U_AS2, 'LID1': W_LID1, 'LID2': W_LID2}
  df = pd.DataFrame(raw_data, columns = ['day', 'CS1', 'CS2', 'CS3', 'CS3sb', 'CS4', 'CS5', 'AS1', 'AS2', 'LID1', 'LID2'])

  filename1 = ('../results/metricsDataFrames/%s_%s_HCWCSTRANS.csv' % (exp,run))

  df.to_csv(filename1)

  print(filename1)

  print('Done water transport')

main()

max_stars_repo_name: UBC-MOAD/outputanalysisnotebooks

max_issues_repo_name: UBC-MOAD/outputanalysisnotebooks

max_forks_repo_name: UBC-MOAD/outputanalysisnotebooks

reason: import numpy,import scipy


text:
#!/usr/bin/env python3
import matplotlib
from utils import TANGO, filename
from matplotlib import pyplot
from numpy import linspace

matplotlib.rcParams['lines.linewidth'] = 2

def histogram_plt():
    y = [0.1, 1, 5, 6, 0.2, 1, 10, 8, 0]
    x = linspace(0, 8, num = 9)
    T = 0.5

    FILLS   = (TANGO["red"][0], TANGO["green"][1])
    STROKES = (TANGO["red"][1], TANGO["green"][2])
    colors = [FILLS[yy > T] for yy in y]
    edgecolors = [STROKES[yy > T] for yy in y]

    pyplot.clf()
    pyplot.bar(x, y, width=1)

    ax = pyplot.axes(frameon = False)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.set_xlim(-1, 10)

    pyplot.bar(x, y, width = 1, color = colors, edgecolor = edgecolors, linewidth = 2)
    pyplot.tight_layout()

batch_mode, fname = filename("models-plots.pdf")

if batch_mode:
    histogram_plt()
    pyplot.savefig(fname, transparent = True)

max_stars_repo_name: adrianlut/raha

max_issues_repo_name: adrianlut/raha

max_forks_repo_name: adrianlut/raha

reason: from numpy


text:
import sys
from PyQt5.QtWidgets import (QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QGraphicsView,
    QLabel, QApplication, QGraphicsScene, QGraphicsRectItem, QAction, qApp, QLineEdit, QSplitter, QFrame, QGraphicsWidget, QGraphicsItem, QComboBox, QPushButton, QSlider, QDesktopWidget)
from PyQt5.QtGui import QPixmap, QIcon, QDragEnterEvent, QDragLeaveEvent, QMouseEvent, QDrag, QDropEvent, QPaintEvent, QPainter, QColor, QFont, QPen
from PyQt5.QtCore import Qt, QMimeData, QObject, pyqtSignal, QBasicTimer, QTimerEvent, QRectF
import pyqtgraph as pg
import numpy as np

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        hbox = QHBoxLayout()

        lbl = QLabel(self)
        lbl.setText('My Text Label')
        hbox.addWidget(lbl)

        pw = (pg.PlotWidget())
        hbox.addWidget(pw)

        p1 = pw.plotItem
        p1.setTitle('This is Title')
        p1.setLabels(left='axis 1')

        p1.plot([1,2,3,4,8,16,32])
        p1.addItem(pg.PlotCurveItem([10,20,30], pen='b'))

        p2 = pg.ViewBox()
        p1.showAxis('right')
        p1_s = (QGraphicsScene)(p1.scene())
        p1_s.addItem(p2)
        p1.getAxis('right').linkToView(p2)
        p2.setXLink(p1)
        p1.getAxis('right').setLabel('axis 2', color='#0000ff')

        p2.addItem(pg.PlotCurveItem([3200, 1600], pen='b'))

        p1vb = (QGraphicsWidget)(p1.vb)
        p1vb.sceneBoundingRect()

        pg.GraphicsWidget

        self.setLayout(hbox)

        self.show()

def run():
    app = QApplication([])
    e = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':

    run()

max_stars_repo_name: xyise/xyise

max_issues_repo_name: xyise/xyise

max_forks_repo_name: xyise/xyise


text:
from .multi_agent_env import MultiAgentEnv
import numpy as np
from .agents import Agent
import six
from gym import spaces

class KickAndDefend(MultiAgentEnv):
    def __init__(self, max_episode_steps=500, randomize_ball=True, **kwargs):
        super(KickAndDefend, self).__init__(**kwargs)
        self._max_episode_steps = max_episode_steps
        self._elapsed_steps = 0
        self.GOAL_REWARD = 1000
        self.ball_jnt_id = self.env_scene.model.joint_names.index(six.b('ball'))
        self.jnt_nqpos = Agent.JNT_NPOS[int(self.env_scene.model.jnt_type[self.ball_jnt_id])]
        if self.agents[0].team == 'walker':
            self.walker_id = 0
            self.blocker_id = 1
        else:
            self.walker_id = 1
            self.blocker_id = 0
        self.GOAL_X = self.agents[self.walker_id].TARGET
        # print("GOAL_X:", self.GOAL_X)
        self.GOAL_Y = 3
        self.randomize_ball = randomize_ball
        self.LIM_X = [(-3.5, -0.5), (1.6, 3.5)]
        # self.RANGE_X = self.LIM_X = [(-2, -2), (1.6, 3.5)]
        self.LIM_Y = [(-2, 2), (-2, 2)]
        self.RANGE_X = self.LIM_X.copy()
        self.RANGE_Y = self.LIM_Y.copy()
        self.BALL_LIM_X = (-2, 1)
        self.BALL_LIM_Y = (-4, 4)
        self.BALL_RANGE_X = self.BALL_LIM_X
        self.BALL_RANGE_Y = self.BALL_LIM_Y
        self.keeper_touched_ball = False

    def _past_limit(self):
        if self._max_episode_steps <= self._elapsed_steps:
            return True
        return False

    def get_ball_qpos(self):
        start_idx = int(self.env_scene.model.jnt_qposadr[self.ball_jnt_id])
        return self.env_scene.model.data.qpos[start_idx:start_idx+self.jnt_nqpos]

    def get_ball_qvel(self):
        start_idx = int(self.env_scene.model.jnt_dofadr[self.ball_jnt_id])
        # ball has 6 components: 3d translation, 3d rotational
        return self.env_scene.model.data.qvel[start_idx:start_idx+6]

    def get_ball_contacts(self, agent_id):
        mjcontacts = self.env_scene.data._wrapped.contents.contact
        ncon = self.env_scene.model.data.ncon
        contacts = []
        for i in range(ncon):
            ct = mjcontacts[i]
            g1 , g2 = ct.geom1, ct.geom2
            g1 = self.env_scene.model.geom_names[g1]
            g2 = self.env_scene.model.geom_names[g2]
            if g1.find(six.b('ball')) >= 0:
                if g2.find(six.b('agent' + str(agent_id))) >= 0:
                    if ct.dist < 0:
                        contacts.append((g1, g2, ct.dist))
        return contacts

    def _set_ball_xyz(self, xyz):
        start = int(self.env_scene.model.jnt_qposadr[self.ball_jnt_id])
        qpos = self.env_scene.model.data.qpos.flatten().copy()
        qpos[start:start+3] = xyz
        qvel = self.env_scene.model.data.qvel.flatten()
        self.env_scene.set_state(qpos, qvel)

    def is_goal(self):
        ball_xyz = self.get_ball_qpos()[:3]
        if self.GOAL_X > 0 and ball_xyz[0] > self.GOAL_X and abs(ball_xyz[1]) <= self.GOAL_Y:
            return True
        elif self.GOAL_X < 0 and ball_xyz[0] < self.GOAL_X and abs(ball_xyz[1]) <= self.GOAL_Y:
            return True
        return False

    def goal_rewards(self, infos=None, agent_dones=None):
        self._elapsed_steps += 1
        # print(self._elapsed_steps, self.keeper_touched_ball)
        goal_rews = [0. for _ in range(self.n_agents)]
        ball_xyz = self.get_ball_qpos()[:3]
        done = self._past_limit() or (self.GOAL_X > 0 and ball_xyz[0] > self.GOAL_X) or (self.GOAL_X < 0 and ball_xyz[0] < self.GOAL_X)
        ball_vel = self.get_ball_qvel()[:3]
        if ball_vel[0] < 0 and np.linalg.norm(ball_vel) > 1:
            done = True
            # print("Keeper stopped ball, vel:", ball_vel)
        # agent_fallen = [self.agents[i].get_qpos()[2] < 0.5 for i in range(self.n_agents)]
        # import ipdb; ipdb.set_trace()
        ball_contacts = self.get_ball_contacts(self.blocker_id)
        if len(ball_contacts) > 0:
            # print("detected contacts for keeper:", ball_contacts)
            self.keeper_touched_ball = True
        if self.is_goal():
            for i in range(self.n_agents):
                if self.agents[i].team == 'walker':
                    goal_rews[i] += self.GOAL_REWARD
                    infos[i]['winner'] = True
                else:
                    goal_rews[i] -= self.GOAL_REWARD
            done = True
        elif done or all(agent_dones):
            for i in range(self.n_agents):
                if self.agents[i].team == 'walker':
                        goal_rews[i] -= self.GOAL_REWARD
                else:
                    goal_rews[i] += self.GOAL_REWARD
                    infos[i]['winner'] = True
                    if self.keeper_touched_ball:
                        # ball contact bonus
                        goal_rews[i] += 0.5 * self.GOAL_REWARD
                    if self.agents[i].get_qpos()[2] > 0.8:
                        # standing bonus
                        goal_rews[i] += 0.5 * self.GOAL_REWARD
        else:
            keeper_penalty = False
            for i in range(self.n_agents):
                if self.agents[i].team == 'blocker':
                    if np.abs(self.GOAL_X - self.agents[i].get_qpos()[0]) > 2.5:
                        keeper_penalty = True
                        # print("keeper x:", self.agents[i].get_qpos()[0], "goal_x:", self.GOAL_X)
                        print("Keeper foul!")
                        break
            if keeper_penalty:
                done = True
                for i in range(self.n_agents):
                    if self.agents[i].team == 'blocker':
                        goal_rews[i] -= self.GOAL_REWARD
            else:
                for i in range(self.n_agents):
                    if self.agents[i].team == 'walker':
                        # goal_rews[i] -= np.abs(ball_xyz[0] - self.GOAL_X)
                        infos[i]['reward_move'] -= np.asscalar(np.abs(ball_xyz[0] - self.GOAL_X))
                    else:
                        infos[i]['reward_move'] += np.asscalar(np.abs(ball_xyz[0] - self.GOAL_X))
                        # if len(ball_contacts) > 0:
                        #     # ball contact bonus
                        #     print("detected contacts for keeper:", ball_contacts)
                        #     goal_rews[i] += 0.5 * self.GOAL_REWARD
        return goal_rews, done

    def _set_ball_vel(self, vel_xyz):
        start = int(self.env_scene.model.jnt_dofadr[self.ball_jnt_id])
        qvel = self.env_scene.model.data.qvel.flatten().copy()
        qvel[start:start+len(vel_xyz)] = vel_xyz
        qpos = self.env_scene.model.data.qpos.flatten()
        self.env_scene.set_state(qpos, qvel)

    def _set_random_ball_pos(self):
        x = np.random.uniform(*self.BALL_RANGE_X)
        y = np.random.uniform(*self.BALL_RANGE_Y)
        z = 0.35
        # print("setting ball to {}".format((x, y, z)))
        self._set_ball_xyz((x,y,z))
        if self.get_ball_qvel()[0] < 0:
            self._set_ball_vel((0.1, 0.1, 0.1))

    def _reset_range(self, version):
        decay_func = lambda x: 0.05 * np.exp(0.001 * x)
        v = decay_func(version)
        self.BALL_RANGE_X = (max(self.BALL_LIM_X[0], -v), min(self.BALL_LIM_X[1], v))
        self.BALL_RANGE_Y = (max(self.BALL_LIM_Y[0], -v), min(self.BALL_LIM_Y[1], v))
        self.RANGE_X[0] = (max(self.LIM_X[0][0], -2-v),  min(self.LIM_X[0][1], -2+v))
        self.RANGE_Y[0] = (max(self.LIM_Y[0][0], -v),  min(self.LIM_Y[0][1], v))
        self.RANGE_X[1] = (max(self.LIM_X[1][0], 2-v),  min(self.LIM_X[1][1], 2+v))
        self.RANGE_Y[1] = (max(self.LIM_Y[1][0], -v),  min(self.LIM_Y[1][1], v))
        # print(self.RANGE_X)
        # print(self.RANGE_Y)
        # print(self.BALL_RANGE_X)
        # print(self.BALL_RANGE_Y)

    def _reset(self, version=None):
        self._elapsed_steps = 0
        self.keeper_touched_ball = False
        _ = self.env_scene.reset()
        if version is not None:
            self._reset_range(version)
        for i in range(self.n_agents):
            x = np.random.uniform(*self.RANGE_X[i])
            y = np.random.uniform(*self.RANGE_Y[i])
            # print("setting agent {} to pos {}".format(i, (x,y)))
            self.agents[i].set_xyz((x, y, None))
            self.agents[i].reset_agent()
        if self.randomize_ball:
            self._set_random_ball_pos()
        return self._get_obs()

    def reset(self, margins=None, version=None):
        _ = self._reset(version=version)
        if self.agents[0].team == 'walker':
            self.walker_id = 0
            self.blocker_id = 1
        else:
            self.walker_id = 1
            self.blocker_id = 0
        self.GOAL_X = self.agents[self.walker_id].TARGET
        if margins is not None:
            for i in range(self.n_agents):
                self.agents[i].set_margin(margins[i])
        # print("GOAL_X:", self.GOAL_X)
        ob = self._get_obs()
        return ob

max_stars_repo_name: MachengShen/torchbeast

max_issues_repo_name: MachengShen/torchbeast

max_forks_repo_name: MachengShen/torchbeast


text:
import logging
import math
import os
import unittest
from typing import Set

import luigi
import numpy as np
from netCDF4 import Dataset, Group, Variable

from iasi.compression import CompressDataset, DecompressDataset
from iasi.file import MoveVariables
from iasi.util import child_groups_of, child_variables_of

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

class TestCompareDecompressionResult(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        file = 'test/resources/MOTIV-slice-100.nc'
        # make sure there is a compressed file for testing purpose
        compression = DecompressDataset(
            file=file,
            dst='/tmp/iasi',
            force_upstream=True,
            log_file=False,
            compress_upstream=True
        )
        uncompressed = MoveVariables(
            file=file,
            dst='/tmp/iasi',
            force_upstream=True,
            log_file=False
        )
        assert luigi.build([compression, uncompressed], local_scheduler=True)
        cls.compressed = Dataset(compression.output().path)
        cls.uncompressed = Dataset(uncompressed.output().path)

    @classmethod
    def tearDownClass(cls):
        cls.compressed.close()
        cls.uncompressed.close()

    def variable_names(self, variables) -> Set:
        return set(map(lambda v: v.name, variables))

    def group_paths(self, groups) -> Set:
        return set(map(lambda g: g.path, groups))

    def test_all_variables_exist(self):
        for group in child_groups_of(self.uncompressed):
            if group.path == '/':
                other_vars = set(self.compressed.variables.keys())
            else:
                other_vars = set(self.compressed[group.path].variables.keys())
            self.assertSetEqual(set(group.variables.keys()), other_vars)

    def test_all_variable_values_are_close(self):
        for group, var in child_variables_of(self.uncompressed['state']):
            path = os.path.join(group.path, var.name)
            # test only reconstructed variables
            if var.name not in ['avk', 'Tatmxavk', 'n'] or group.name in ['Tskin']:
                continue
            original = var[...]
            reconstructed = self.compressed[path][...]
            # assert math.isnan(reconstructed.fill_value)
            for event in range(original.shape[0]):
                if reconstructed[event].mask.all():
                    continue
                same_mask = np.equal(
                    original[event].mask, reconstructed[event].mask).all()
                self.assertTrue(
                    same_mask, f'reconstruced mask is not equal for {path} at {event}')
                a = original[event].compressed()
                b = reconstructed[event].compressed()
                close = np.ma.allclose(a, b, atol=1e-2, rtol=5e-2)
                if not close:
                    logger.error('max difference is %f', np.abs(a - b).max())
                self.assertTrue(
                    close, f'reconstruction values are not close for {path} at {event}')
            logger.debug('All variables are close for %s', path)


text:
import numpy as np
from supervised.algorithms.knn import KNeighborsAlgorithm, KNeighborsRegressorAlgorithm
import optuna

from supervised.utils.metric import Metric
from supervised.algorithms.registry import BINARY_CLASSIFICATION
from supervised.algorithms.registry import MULTICLASS_CLASSIFICATION
from supervised.algorithms.registry import REGRESSION

class KNNObjective:
    def __init__(
        self,
        ml_task,
        X_train,
        y_train,
        sample_weight,
        X_validation,
        y_validation,
        sample_weight_validation,
        eval_metric,
        n_jobs,
        random_state,
    ):
        self.ml_task = ml_task
        self.X_train = X_train
        self.y_train = y_train
        self.sample_weight = sample_weight
        self.X_validation = X_validation
        self.y_validation = y_validation
        self.eval_metric = eval_metric
        self.n_jobs = n_jobs
        self.seed = random_state

    def __call__(self, trial):
        try:
            params = {
                "n_neighbors": trial.suggest_int("n_neighbors", 1, 128),
                "weights": trial.suggest_categorical(
                    "weights", ["uniform", "distance"]
                ),
                "n_jobs": self.n_jobs,
                "rows_limit": 100000,
                "ml_task": self.ml_task,
            }
            Algorithm = (
                KNeighborsRegressorAlgorithm
                if self.ml_task == REGRESSION
                else KNeighborsAlgorithm
            )
            model = Algorithm(params)
            model.fit(self.X_train, self.y_train, sample_weight=self.sample_weight)
            preds = model.predict(self.X_validation)

            score = self.eval_metric(self.y_validation, preds)
            if Metric.optimize_negative(self.eval_metric.name):
                score *= -1.0

        except optuna.exceptions.TrialPruned as e:
            raise e
        except Exception as e:
            print("Exception in KNNObjective", str(e))
            return None

        return score

max_stars_repo_name: stjordanis/mljar-supervised

max_issues_repo_name: stjordanis/mljar-supervised

max_forks_repo_name: stjordanis/mljar-supervised


text:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as st
import datetime
from statsmodels.graphics.gofplots import qqplot
import plotly.express as px

dataset='Online Retail .csv'
df=pd.read_csv(dataset,dtype={'CustomerID':'Int64'})
print(df.info())

#checking the total number of missing values
print(df.isnull().sum().sort_values(ascending=False))
#
# '''We see that there are some missing values for Customers ID and Description.
# The rows with any of these missing values will therefore be removed. '''
# #droping the rows having missing values
df=df.dropna(axis=0)
#
# #understanding the data in a more descriptive manner
print(df.describe().round(2))
# '''
# Quantity has negative values
# Unit Price has zero values,
# we have some odd and irregular values in the 'UnitPrice' and 'Quantity' columns
# that we will find and remove to prevent them from negatively affect our analysis.
# In the 'StockCode' variable we can see that some of the transaction are not
# actually products, but they are some costs or fees regarding to the post or
# bank or other transactions that we don't really need in our data'''
#
df.drop(df[df['Quantity'] < 0].index, inplace = True)
df.drop(df[df['UnitPrice'] == 0].index, inplace = True)
df['InvoiceDate']=pd.to_datetime(df['InvoiceDate'],format='%m/%d/%y %H:%M')

#Fixing Duplicate CustomerIDs
for i,v in df.groupby('CustomerID')['Country'].unique().items():
    if len(v)>1:
        df.Country[df['CustomerID'] == i] = df.Country[df['CustomerID'] == i].mode()[0]
print(df['Country'].unique())

df = df[(np.abs(st.zscore(df['UnitPrice']))<3) & (np.abs(st.zscore(df['Quantity']))<5)]

q1_quantity, q2_quantity, q3_quantity = df['Quantity'].quantile([0.25,0.5,0.75])
iqr_quantity = q3_quantity - q1_quantity
lower_limit_quantity = q1_quantity - 1.5*iqr_quantity
upper_limit_quantity = q3_quantity + 1.5*iqr_quantity
print(f'Q1 and Q3 of Quantity is {q1_quantity:.2f} & {q3_quantity:.2f} .')
print(f'IQR for the Quantity {iqr_quantity:.2f} .')
print(f'Any Quantity < {lower_limit_quantity:.2f} and Quantity > {upper_limit_quantity:.2f} is an outlier')

sns.set_style('darkgrid')
sns.boxplot(y=df['Quantity'])
plt.title('Boxplot of Quantity')
plt.show()
df= df[(df['Quantity'] >= lower_limit_quantity) & (df['Quantity'] <= upper_limit_quantity)]
sns.set_style('darkgrid')
sns.boxplot(y=df['Quantity'])
plt.title('Boxplot of quantity after removing outliers')
plt.show()

q1_UnitPrice, q2_UnitPrice, q3_UnitPrice = df['UnitPrice'].quantile([0.25,0.5,0.75])
iqr_UnitPrice = q3_UnitPrice - q1_UnitPrice
lower_limit_UnitPrice = q1_UnitPrice - 1.5*iqr_UnitPrice
upper_limit_UnitPrice = q3_UnitPrice + 1.5*iqr_UnitPrice
print(f'Q1 and Q3 of UnitPrice is {q1_UnitPrice:.2f} & {q3_UnitPrice:.2f} .')
print(f'IQR for the UnitPrice {iqr_UnitPrice:.2f} .')
print(f'Any UnitPrice < {lower_limit_UnitPrice:.2f} and UnitPrice > {upper_limit_UnitPrice:.2f} is an outlier')

sns.set_style('darkgrid')
sns.boxplot(y=df['UnitPrice'])
plt.title('Boxplot of UnitPrice')
plt.show()
df = df[(df['UnitPrice'] >= lower_limit_UnitPrice) & (df['UnitPrice'] <= upper_limit_UnitPrice)]
sns.set_style('darkgrid')
sns.boxplot(y=df['UnitPrice'])
plt.title('Boxplot of UnitPrice after removing outliers')
plt.show()

q1_UnitPrice2, q2_UnitPrice2, q3_UnitPrice2 = df['UnitPrice'].quantile([0.25,0.5,0.75])
iqr_UnitPrice2 = q3_UnitPrice2 - q1_UnitPrice2
lower_limit_UnitPrice2 = q1_UnitPrice2 - 1.5*iqr_UnitPrice2
upper_limit_UnitPrice2 = q3_UnitPrice2 + 1.5*iqr_UnitPrice2
print(f'Q1 and Q3 of UnitPrice is {q1_UnitPrice2:.2f} & {q3_UnitPrice2:.2f} .')
print(f'IQR for the UnitPrice {iqr_UnitPrice2:.2f} .')
print(f'Any UnitPrice < {lower_limit_UnitPrice2:.2f} and UnitPrice > {upper_limit_UnitPrice2:.2f} is an outlier')

df = df[(df['UnitPrice'] >= lower_limit_UnitPrice2) & (df['UnitPrice'] <= upper_limit_UnitPrice2)]
sns.set_style('darkgrid')
sns.boxplot(y=df['UnitPrice'])
plt.title('Boxplot of UnitPrice after removing outliers')
plt.show()

#PCA
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
features=['Quantity','UnitPrice']
x_f=df[features].values
X=StandardScaler().fit_transform(x_f)
H_pca = np.matmul(x_f.T,x_f)
_,d_PCA,_ = np.linalg.svd(H_pca)
print(f'Transformed X singular values: {d_PCA}')
print(f'Transformed X condition number: {np.linalg.cond(x_f)}')

sns.heatmap(df.corr(),annot=True)
plt.title("correlation coefficient between features - orginal feature space")
plt.show()
pca = PCA(n_components='mle', svd_solver='full')
pca.fit(X)
X_pca = pca.transform(X)
print("explained variance ratio:",pca.explained_variance_ratio_)
print("shape:",X_pca.shape)
plt.plot(np.arange(1, len(np.cumsum(pca.explained_variance_ratio_))+1,1), np.cumsum(pca.explained_variance_ratio_))
plt.xticks(np.arange(1, len(np.cumsum(pca.explained_variance_ratio_))+1,1))
plt.xlabel("number of components")
plt.ylabel("cumulative explained variance")
df1=pd.DataFrame(X_pca).corr()
a,b = X_pca.shape
col = []
for i in range(b):
    col.append(f'Principle column {i+1}')

# sns.heatmap(df1,annot=True,xticklabels=col,yticklabels=col)
# plt.title("correlation coefficient")
# plt.show()
df_PCA = pd.DataFrame(data=X_pca, columns=col)
print("old one:",df.head().to_string)
print("new one:",df_PCA.head().to_string)

kstest_Quantity = st.kstest(df['Quantity'],'norm')
kstest_UnitPrice = st.kstest(df['UnitPrice'],'norm')

print(f"K-S test: statistics={kstest_UnitPrice[0]}, p-value={kstest_UnitPrice[1]}")
print(f"K-S test:  dataset looks {'Normal' if kstest_UnitPrice[1] > 0.01 else 'Non-Normal'}")
print(f"K-S test: statistics={kstest_Quantity[0]}, p-value={kstest_Quantity[1]}")
print(f"K-S test: looks {'Normal' if kstest_Quantity[1] > 0.01 else 'Non-Normal'}")

shapiro_test_Quantity = st.shapiro(df['Quantity'])
shapiro_test_UnitPrice = st.shapiro(df['UnitPrice'])

print(f"Shapiro test: statistics={shapiro_test_Quantity[0]:.5f}, p-value={shapiro_test_Quantity[1]:.5f}")
print(f"Shapiro test: dataset looks {'Normal' if shapiro_test_Quantity[1] > 0.01 else 'Non-Normal'}")
print(f"Shapiro test: statistics={shapiro_test_UnitPrice[0]:.5f}, p-value={shapiro_test_UnitPrice[1]:.5f}")
print(f"Shapiro test: dataset looks {'Normal' if shapiro_test_UnitPrice[1] > 0.01 else 'Non-Normal'}")

da_test_Quantity = st.normaltest(df['Quantity'])
da_test_UnitPrice = st.normaltest(df['UnitPrice'])

print(f"da_k_squared test: statistics={da_test_UnitPrice[0]:.5f}, p-value={da_test_UnitPrice[1]:.5f}")
print(f"da_k_squared test: dataset looks {'Normal' if da_test_UnitPrice[1] > 0.01 else 'Non-Normal'}")
print(f"da_k_squared test: statistics={da_test_Quantity[0]:.5f}, p-value={da_test_Quantity[1]:.5f}")
print(f"da_k_squared test: dataset looks {'Normal' if da_test_Quantity[1] > 0.01 else 'Non-Normal'}")

#adding features
europe = ['Germany','Spain', 'France', 'Italy', 'Netherlands', 'Norway', 'Sweden','Czech Republic', 'Finland',
          'Denmark', 'Switzerland', 'United Kingdom', 'Poland', 'Greece','Austria', 'Belgium','Portugal', 'Lithuania', 'Iceland','EIRE',
          'Channel Islands','Cyprus','European Community','Malta']
asia = ['Bahrain', 'United Arab Emirates','Saudi Arabia','Lebanon','Japan','Singapore','Israel']
other=['Australia','Canada','Unspecified','Brazil','USA','RSA']

conti = []
for i in df['Country']:
    if i in europe:
        conti.append('Europe')
    elif i in asia:
        conti.append('Asia')

    elif i in other:
        conti.append('Other')

df['Continent']=conti
df['FinalPrice'] = df['Quantity']*df['UnitPrice']
df['InvoiceMonth'] = df['InvoiceDate'].apply(lambda x: x.strftime('%B'))
df['Day of week'] = df['InvoiceDate'].dt.day_name()
print(df.head())
print(np.max(df['Quantity']))
print(np.min(df['Quantity']))
print(df['Quantity'])

#================================================

# df.to_csv(r'/Users/sunny/Desktop/ecommerce project/Online-Retail-cleaned.csv', date_format = '%Y-%m-%d %H:%M', index = False)

max_stars_repo_path: code/dataPreprocessing copy 2.py

max_issues_repo_path: code/dataPreprocessing copy 2.py

max_forks_repo_path: code/dataPreprocessing copy 2.py

reason: import numpy,import scipy,from statsmodels


text:
import xgboost as xgb
import numpy as np
import pandas as pd
from configs import *
import argparse

def get_args():
    parser = argparse.ArgumentParser(description='Cross validation')
    parser.add_argument('--seq_pos', type=str, default='./data/pos_seq.npy',
                        help='positive data set path')
    parser.add_argument('--seq_neg', type=str, default='./data/neg_seq.npy',
                        help='negtive data set path')
    parser.add_argument('--TF_pos', type=str, default='./data/pos_tf.npy',
                        help='positive tf motif score set path')
    parser.add_argument('--TF_neg', type=str, default='./data/neg_tf.npy',
                        help='negtive tf motif score set path')
    parser.add_argument('--save', type=str, default='./data/result/',
                        help='path to save the final feature')
    parser.add_argument('--nfolds', type=int, default=10,
                        help='num of folds')
    parser.add_argument('--celltype', type=str, default='K562',
                        help='cell line')
    args = parser.parse_args()
    return args
args = get_args()

# load params
silencer = np.load(args.seq_pos)
silencer_TF = np.load(args.TF_pos)
enhancer = np.load(args.seq_neg)
enhancer_TF = np.load(args.TF_neg)
folds = args.nfold
cell = args.celltype
save_path = args.save

# split length
silencer_len = len(silencer)
split_len_si = int(silencer_len/folds)

enhancer_len = len(enhancer)
split_len_en = int(enhancer_len/folds)

# n fold cross validation
for cv in range(1,folds):
    train_s = silencer[np.array(list(range(0,(cv-1)*split_len_si)) + list(range(cv*split_len_si,silencer_len)))]
    train_e = enhancer[np.array(list(range(0,(cv-1)*split_len_en)) + list(range(cv*split_len_en,enhancer_len)))]

    train_TF_s = silencer_TF[np.array(list(range(0,(cv-1)*split_len_si)) + list(range(cv*split_len_si,silencer_len)))]
    train_TF_e = enhancer_TF[np.array(list(range(0,(cv-1)*split_len_en)) + list(range(cv*split_len_en,enhancer_len)))]

    test_data_s = silencer[np.array(list(range((cv-1)*split_len_si , cv * split_len_si)))]
    test_data_e = enhancer[np.array(list(range((cv-1)*split_len_en , cv * split_len_en)))]

    test_TF_s = silencer_TF[np.array(list(range((cv-1)*split_len_si , cv * split_len_si)))]
    test_TF_e = enhancer_TF[np.array(list(range((cv-1)*split_len_en , cv * split_len_en)))]

    # last fold
    if cv == folds:
        train_s = silencer[np.array(list(range(0,(cv-1)*split_len_si)) )]
        train_e = enhancer[np.array(list(range(0,(cv-1)*split_len_en)))]

        train_TF_s = silencer_TF[np.array(list(range(0,(cv-1)*split_len_si)) )]
        train_TF_e = enhancer_TF[np.array(list(range(0,(cv-1)*split_len_en)))]

        test_data_s = silencer[np.array(list(range((cv-1)*split_len_si , silencer_len)))]
        test_data_e = enhancer[np.array(list(range((cv-1)*split_len_en , enhancer_len)))]
        test_TF_s = silencer_TF[np.array(list(range((cv-1)*split_len_si , silencer_len)))]
        test_TF_e = enhancer_TF[np.array(list(range((cv-1)*split_len_en , enhancer_len)))]

    train_data = np.vstack((train_s,train_e))
    train_TF = np.vstack((train_TF_s,train_TF_e))
    test_data = np.vstack((test_data_s,test_data_e))
    test_TF = np.vstack((test_TF_s,test_TF_e))

    # tf exp data
    exp = np.array(pd.read_table("/data/temp/gaozijing/DeepSilencer/TF/rna/tf_%s.txt" % cell, header=None, sep='\t').values)
    exp = exp[:, 1].astype(float)
    exp[np.isnan(exp)] = 0.0

    train_TF = (-np.log10(train_TF)) * exp
    test_TF = (-np.log10(test_TF)) * exp

    train_data = np.hstack((train_data,train_TF))
    test_data = np.hstack((test_data,test_TF))

    train_data = np.squeeze(train_data)
    test_data = np.squeeze(test_data)

    # generate label
    train_label = [1]*len(train_s) + [0]*len(train_e)
    dtrain = xgb.DMatrix(train_data, label = train_label)
    dtest = xgb.DMatrix(test_data)

    # xgboost params
    params={'booster':booster,
        'objective': objective,
        'eval_metric': eval_metric,
        'max_depth':max_depth,
        'lambda':Lambda,
        'n_estimators':n_estimators,
        'gamma':gamma,
        'subsample':subsample,
        'colsample_bytree':colsample_bytree,
        'min_child_weight':min_child_weight,
        'eta': eta,
        'seed':seed,
        'nthread':8,
         'silent':0}

    watchlist = [(dtrain,'train')]
    bst=xgb.train(params,dtrain,num_boost_round=ROUNDS,evals=watchlist)
    yscore=bst.predict(dtest)
    yscore = np.array(yscore)
    # save result
    np.save(save_path + 'cross_validation_cv%s.npy'%cv,yscore)
    bst.save_model('./model/xgb_2_cv%s.bin'%cv)

max_stars_repo_name: ZjGaothu/KmerTR

max_issues_repo_name: ZjGaothu/KmerTR

max_forks_repo_name: ZjGaothu/KmerTR


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 18 19:58:19 2021

@author: kanghyun
"""

import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator

class DataGenerator(keras.utils.Sequence):
    def __init__(self,
                 num_images,
                 base_path='/home/kanghyun_gpu/Desktop/dual-channel-virtual-fluorescence/Data/', # /data2/amey/TB/Data/
                 is_train=True,
                 is_green=True,
                 is_semantic=False,
                 class1_weight=1.0,
                 class2_weight=1.0,
                 batch_size=16,
                 crop_size=256,
                 n_in_channels=21,
                 n_out_channels=1,
                 shuffle=True,
                 random_seed=0):
        self.crop_size = crop_size
        self.num_images = num_images
        self.base_path = base_path
        self.is_train = is_train
        self.augmentor = ImageDataGenerator(
            horizontal_flip=True, vertical_flip=True)\
            if is_train else ImageDataGenerator()

        self.is_green = is_green
        self.is_semantic = is_semantic
        self.class1_weight = class1_weight
        self.class2_weight = class2_weight
        self.batch_size = batch_size
        self.n_in_channels = n_in_channels
        self.n_out_channels = n_out_channels
        self.shuffle = shuffle
        self.random_seed = random_seed
        np.random.seed(self.random_seed)
        self.on_epoch_end()

    def __getitem__(self, index):
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
        if self.is_semantic:
            X, y, w = self.__data_generation(indexes)
            return X, y, w
        else:
            X, y = self.__data_generation(indexes)
            return X, y

    def __len__(self):
        return int(np.floor(self.num_images / self.batch_size))

    def on_epoch_end(self):
        self.indexes = np.arange(self.num_images)
        if self.shuffle is True and self.is_train is True:
            np.random.shuffle(self.indexes)

    def custom_augmentor_flow_reindex(self, Xy,
                                      rotation_state, horizontal_flip_state):
        rotation_reindex = np.array([[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21],
                            [0,4,1,2,3,11,12,5,6,7,8,9,10,19,20,13,14,15,16,17,18, 21],
                            [0,3,4,1,2,9,10,11,12,5,6,7,8,17,18,19,20,13,14,15,16, 21],
                            [0,2,3,4,1,7,8,9,10,11,12,5,6,15,16,17,18,19,20,13,14, 21]])
        horizontal_flip_reindex = np.array([[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21],
                                   [0,2,1,4,3,10,9,8,7,6,5,12,11,18,17,16,15,14,13,20,19, 21]])
        if self.is_semantic:
            rotation_reindex = np.insert(rotation_reindex,
                                         rotation_reindex.shape[1], 22, axis=1)
            horizontal_flip_reindex = np.insert(horizontal_flip_reindex,
                                         horizontal_flip_reindex.shape[1], 22, axis=1)

        Xy = np.array(list(map(lambda Xy, rotation_state:
                               Xy[...,rotation_reindex[rotation_state]],
                               Xy, rotation_state)))
        Xy = np.array(list(map(lambda Xy, horizontal_flip_state:
                               Xy[...,horizontal_flip_reindex[horizontal_flip_state]],
                               Xy, horizontal_flip_state)))

        return Xy

    def custom_augmentor_flow_augment(self, Xy,
                              rotation_state, horizontal_flip_state):
        Xy = np.array(list(map(lambda Xy, rotation_state:
                               np.rot90(Xy, rotation_state),
                               Xy, rotation_state)))
        Xy = np.array(list(map(lambda Xy, horizontal_flip_state:\
                               np.flip(Xy, 1) if horizontal_flip_state\
                                   else Xy,\
                                       Xy, horizontal_flip_state)))

        return self.custom_augmentor_flow_reindex(Xy,
                                    rotation_state=rotation_state,
                                    horizontal_flip_state=horizontal_flip_state)

    def class_weight_generation(self, y):
        class_weights = tf.constant([1.0, self.class1_weight, self.class2_weight])
        class_weights = class_weights/tf.reduce_sum(class_weights)

        sample_weights = tf.gather(class_weights, indices=tf.cast(y, tf.int32))
        return sample_weights

    def __data_generation(self, indexes):
        X = np.empty((self.batch_size, self.crop_size,
                      self.crop_size, self.n_in_channels))
        y = np.empty((self.batch_size, self.crop_size,
                      self.crop_size, self.n_out_channels))

        for i, ID in enumerate(indexes):
            train_test = 'train' if self.is_train else 'test'
            green_red = 'green' if self.is_green else 'red'
            X[i] = np.load(os.path.join(
                self.base_path, train_test,
                f'cropped_LED_array_{ID:04d}_{train_test}.npy'),
                mmap_mode='r')
            if self.is_semantic:
                y[i] = np.load(os.path.join(
                  self.base_path, train_test,
                  f'cropped_flu_mask_array_{ID:04d}_{train_test}.npy'),
                  mmap_mode='r')
            else:
                y[i] = np.load(os.path.join(
                  self.base_path, train_test,
                  f'cropped_{green_red}_flu_AIF_array_{ID:04d}_{train_test}.npy'),
                  mmap_mode='r')

        if self.is_semantic:
            sample_weights = self.class_weight_generation(y)
            Xy = np.concatenate((X, y, sample_weights), axis=3)
        else:
            Xy = np.concatenate((X, y), axis=3)
        rotation_state = np.random.choice(4, self.batch_size)
        horizontal_flip_state = np.random.choice(2, self.batch_size)
        if self.is_train:
            Xy = self.custom_augmentor_flow_augment(Xy,
                                    rotation_state=rotation_state,
                                    horizontal_flip_state=horizontal_flip_state)

        if self.is_semantic:
            return Xy[..., :self.n_in_channels],\
                Xy[..., self.n_in_channels:self.n_in_channels+self.n_out_channels],\
                Xy[..., self.n_in_channels+self.n_out_channels:]
        else:
            return Xy[..., :self.n_in_channels], Xy[..., self.n_in_channels:]


text:
import numpy as np
import random

from typing import Generator, List, Optional, Tuple, Union

def to_one_hot(vector: Union[List[int], Tuple[int, ...], np.ndarray],
               n_columns: Optional[int] = None) -> np.ndarray:
    """Convert array of values into one-hot vector."""
    vector = np.array(vector).squeeze()
    assert len(vector.shape) == 1, "`vector` passed is multi-dimensional"
    if not n_columns:
        n_columns = np.max(vector) + 1
    one_hot = np.zeros((vector.shape[0], n_columns))
    one_hot[np.arange(vector.shape[0]), vector] = 1
    return one_hot

def split_in_batches(features: np.ndarray,
                     targets: np.ndarray,
                     batch_size: int = 32,
                     shuffle: bool = True) -> Generator[Tuple[np.ndarray, np.ndarray], None, None]:
    """Ideally, you would create your own generator so as not to
    load the entire dataset in memory. Here, all the features
    and targets are passed. Wrap it using
    models.DataGeneratorWrapper.
    """
    assert features.shape[0] == targets.shape[0], "Incompatible feature and targets sizes"
    assert batch_size <= features.shape[0], "Not enough samples for batch size"
    indexes = list(range(features.shape[0]))
    if shuffle:
        random.shuffle(indexes)
    for batch in range(int(features.shape[0] / batch_size)):
        start = batch * batch_size
        yield features[indexes[start:start+batch_size], ...], targets[indexes[start:start+batch_size], ...]

max_stars_repo_name: gmontamat/poor-mans-transformers

max_issues_repo_name: gmontamat/poor-mans-transformers

max_forks_repo_name: gmontamat/poor-mans-transformers


text:
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator, LogLocator, NullFormatter
import matplotlib.transforms as mtransforms
import h5py
import mpi4py
import argparse
import sys
import numpy as np

def visualize_elasticity():
    f = h5py.File('bench_edge_output.hdf5', 'r',
                  driver='mpio', comm=mpi4py.MPI.COMM_WORLD)
    iterations = f.get("its")[:]
    dofs = f.get("num_dofs")[:]
    slaves = np.sum(f.get("num_slaves")[:], axis=1)
    solver = f.get("solve_time").attrs["solver"].decode("utf-8")
    ct = f.get("solve_time").attrs["ct"].decode("utf-8")
    degree = f.get("solve_time").attrs["degree"].decode("utf-8")
    f.close()
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    plt.plot(dofs, iterations, "-ro", label="MPC", markersize=12)

    f_ref = h5py.File('elasticity_ref.hdf5', 'r', driver='mpio',
                      comm=mpi4py.MPI.COMM_WORLD)
    iterations_ref = f_ref.get("its")[:]
    dofs_ref = f_ref.get("num_dofs")[:]
    f_ref.close()

    plt.plot(dofs_ref, iterations_ref, "-bs", label="Unconstrained")

    ax.tick_params(axis='both', which='major', labelsize=20)
    ax.set_xscale("log")
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.xlabel("# DOFS", fontsize=20)
    plt.ylabel("# Iterations", fontsize=20)
    trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
                                           x=0.025, y=-0.1, units='inches')
    for i in range(len(iterations)):
        plt.text(dofs[i], iterations[i], slaves[i],
                 transform=trans_offset, fontsize=20)
    plt.title("Linear elasticity (CG{0:s}, {1:s}) with {2:s}".format(
        degree, ct, solver), fontsize=25)
    plt.legend(fontsize=15)
    ax.minorticks_on()
    ax.set_ylim([0, max([max(iterations), max(iterations_ref)]) + 1])
    ax.set_xlim([1e2, 1e8])
    locmax = LogLocator(
        base=10.0,
        numticks=8)
    ax.xaxis.set_major_locator(locmax)
    locmin = LogLocator(
        base=10.0, subs=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9),
        numticks=9)
    ax.xaxis.set_minor_locator(locmin)
    ax.xaxis.set_minor_formatter(NullFormatter())
    plt.grid(True, which="both", axis="both")
    plt.savefig("elasticity_iterations_CG{0:s}_{1:s}.png".format(
        degree, ct), bbox_inches='tight')

def visualize_periodic():
    f = h5py.File('periodic_output.hdf5', 'r',
                  driver='mpio', comm=mpi4py.MPI.COMM_WORLD)
    iterations = f.get("its")[:]
    dofs = f.get("num_dofs")[:]
    slaves = np.sum(f.get("num_slaves")[:], axis=1)

    solver = f.get("solve_time").attrs["solver"].decode("utf-8")
    ct = f.get("solve_time").attrs["ct"].decode("utf-8")
    degree = f.get("solve_time").attrs["degree"].decode("utf-8")
    f.close()

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    plt.plot(dofs, iterations, "-ro", label="MPC", markersize=12)

    f_ref = h5py.File('periodic_ref_output.hdf5', 'r',
                      driver='mpio', comm=mpi4py.MPI.COMM_WORLD)
    iterations_ref = f_ref.get("its")[:]
    dofs_ref = f_ref.get("num_dofs")[:]
    f_ref.close()

    plt.plot(dofs_ref, iterations_ref, "-bs", label="Unconstrained")

    ax.tick_params(axis='both', which='major', labelsize=20)
    ax.set_xscale("log")
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    ax.set_ylim([0, max(iterations) + 1])
    ax.set_xlim([1e2, max(dofs) + 1])
    plt.xlabel("# DOFS", fontsize=20)
    plt.ylabel("# Iterations", fontsize=20)

    trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
                                           x=0.025, y=0.025, units='inches')
    for i in range(len(iterations)):
        plt.text(dofs[i], iterations[i], slaves[i],
                 transform=trans_offset, fontsize=20)
    plt.title("Periodic Poisson (CG {0:s}, {1:s}) with {2:s}".format(
        degree, ct, solver), fontsize=25)
    plt.legend(fontsize=15)
    ax.minorticks_on()
    ax.set_ylim([0, max(iterations) + 1])
    ax.set_xlim([1e2, 1e8])
    locmax = LogLocator(
        base=10.0,
        numticks=8)
    ax.xaxis.set_major_locator(locmax)
    locmin = LogLocator(
        base=10.0, subs=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9),
        numticks=9)
    ax.xaxis.set_minor_locator(locmin)
    ax.xaxis.set_minor_formatter(NullFormatter())
    plt.grid(True, which="both", axis="both")
    plt.savefig("periodic_iterations_CG{0:s}_{1:s}.png".format(
        degree, ct), bbox_inches='tight')

if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--elasticity", action="store_true",
                        dest="elasticity", default=False,
                        help="Visualize iterations for elasticity")
    parser.add_argument("--periodic", action="store_true",
                        dest="periodic", default=False,
                        help="Visualize iterations for periodic")
    args = parser.parse_args()
    thismodule = sys.modules[__name__]
    periodic = elasticity = None
    for key in vars(args):
        setattr(thismodule, key, getattr(args, key))
    if elasticity:
        visualize_elasticity()

    if periodic:
        visualize_periodic()