text:
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.modules.normalization as mm
import numpy
import torch

class SmallerNetwork2(nn.Module):
    def __init__(self, n_past_lidars, n_past_steps, n_future_steps):
        super(SmallerNetwork2, self).__init__()

        # Change these values if you want to change the number of steps in the
        # input or output
        self.values_width = 11 # the number of values on each row in values
        self.vs = (n_past_steps + 1) * self.values_width # values size
        self.os = n_future_steps * 2 # output size

        #encoder
        self.conv0 = nn.Conv2d(n_past_lidars+1,8,3, padding=1) # with elu #twice!
        self.conv1 = nn.Conv2d(8,8,3, padding=1)
        self.maxpool1 = nn.MaxPool2d(2, stride=2)
        self.conv2 = nn.Conv2d(8,16,3, padding=1) # with elu
        self.conv3 = nn.Conv2d(16,16,3, padding=1) # with elu #twice!
        self.maxpool2 = nn.MaxPool2d(2, stride=2)

        # context modules
        # all followed by elu
        # layer 1-12 is also followed by spatial dropout layer with pd=0.20
        self.spatial_dropout = nn.Dropout2d(p=0.2)
        self.Layer1 = nn.Conv2d(16,96,3, padding=1, dilation=1)
        self.Layer2 = nn.Conv2d(96,96,3, padding=1, dilation=1)
        self.Layer3 = nn.Conv2d(96,96,3, padding=(2,1), dilation=(2,1))
        self.Layer4 = nn.Conv2d(96,96,3, padding=(4,2), dilation=(4,2))
        self.Layer5 = nn.Conv2d(96,96,3, padding=(8,4), dilation=(8,4))
        self.Layer6 = nn.Conv2d(96,96,3, padding=(12,8), dilation=(12,8))
        self.Layer7 = nn.Conv2d(96,96,3, padding=(16,12), dilation=(16,12))
        self.Layer8 = nn.Conv2d(96,96,3, padding=(20,16), dilation=(20,16))
        self.Layer9 = nn.Conv2d(96,96,3, padding=(24,20), dilation=(24,20))
        self.Layer10 = nn.Conv2d(96,96,3, padding=(28,24), dilation=(28,24))
        self.Layer11 = nn.Conv2d(96,96,3, padding=(32,28), dilation=(32,28))
        self.Layer12 = nn.Conv2d(96,96,3, padding=(1,32), dilation=(1,32))
        self.Layer13 = nn.Conv2d(96,16,3, padding=1, dilation=1)

        #extra convolutions # 150 x 150 x 16
        self.exconv1 = nn.Conv2d(16,16,3, padding=1)
        self.exmaxpool1 = nn.MaxPool2d(3, stride=3) # 50 x 50
        self.exmaxpool2 = nn.MaxPool2d(2, stride=2) # 25 x 25 x 16

        #decoder #all followed by elu
        #concatenate new values here
        self.Linear1 = nn.Linear(25*25*16 + self.vs, 800)
        self.Linear2 = nn.Linear(800, 300)
        self.Linear3 = nn.Linear(300, self.os)

    def forward(self, l, v): # 600 x 600 x 1
        # encoder
        l = F.elu(self.conv0(l)) # 600 x 600 x 8
        l = F.elu(self.conv1(l)) # 600 x 600 x 8
        l = self.maxpool1(l) # 300 x 300 x 8
        l = F.elu(self.conv2(l)) # 300 x 300 x 16
        l = F.elu(self.conv3(l)) # 300 x 300 x 16
        #l = F.elu(self.conv3(l)) # 300 x 300 x 16
        l = self.maxpool2(l) # 150 x 150 x 16
        #context module
        l = self.spatial_dropout(F.elu(self.Layer1(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer2(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer3(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer4(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer5(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer6(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer7(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer8(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer9(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer10(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer11(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer12(l))) # 150 x 150 x 96
        l = F.elu(self.Layer13(l)) # 150 x 150 x 16

        # extra convolutions
        l = F.elu(self.exconv1(l)) # 150 x 150 x 16
        l = self.exmaxpool1(l) # 50 x 50 x 16
        l = F.elu(self.exconv1(l)) # 50 x 50 x 16
        l = self.exmaxpool2(l) # 25 x 25 x 16

        # decoder
        l = l.view(-1, 25*25*16) # 10000
        v = v.view(-1, (self.n_past_steps+1) *self.values_width)
        x = torch.cat((l,v),1) # 10330
        x = F.elu(self.Linear1(x)) # 800
        x = F.elu(self.Linear2(x)) # 300
        x = self.Linear3(x) #60

        return x

class SmallerNetwork1(nn.Module):
    def __init__(self, n_past_lidars, n_past_steps, n_future_steps):
        super(SmallerNetwork1, self).__init__()

        # Change these values if you want to change the number of steps in the
        # input or output
        self.values_width = 11 # the number of values on each row in values
        self.vs = (n_past_steps + 1) * self.values_width # values size
        self.os = n_future_steps * 2 # output size

        #encoder
        self.conv0 = nn.Conv2d(n_past_lidars+1,4,3, padding=1)
        self.conv1 = nn.Conv2d(4,4,3, padding=1)
        self.conv2 = nn.Conv2d(4,8,3, padding=1)
        self.conv3 = nn.Conv2d(8,8,3, padding=1)
        self.conv4 = nn.Conv2d(8,16,3, padding=1)
        self.conv5 = nn.Conv2d(16,16,3, padding=1)
        self.maxpool2 = nn.MaxPool2d(2, stride=2)
        self.maxpool3_2 = nn.MaxPool2d(3, stride=3, padding=1)
        self.maxpool3 = nn.MaxPool2d(3, stride=3)

        # context modules
        # all followed by elu
        # layer 1-12 is also followed by spatial dropout layer with pd=0.20
        self.spatial_dropout = nn.Dropout2d(p=0.2)
        self.Layer1 = nn.Conv2d(16,96,3, padding=1, dilation=1)
        self.Layer2 = nn.Conv2d(96,96,3, padding=1, dilation=1)
        self.Layer3 = nn.Conv2d(96,96,3, padding=(2,1), dilation=(2,1))
        self.Layer4 = nn.Conv2d(96,96,3, padding=(4,2), dilation=(4,2))
        self.Layer5 = nn.Conv2d(96,96,3, padding=(8,4), dilation=(8,4))
        self.Layer12 = nn.Conv2d(96,96,3, padding=(1,8), dilation=(1,8))
        self.Layer13 = nn.Conv2d(96,16,3, padding=1, dilation=1)

        #decoder #all followed by elu
        #concatenate new values here
        self.Linear1 = nn.Linear(18826,540)
        self.Linear2 = nn.Linear(540,256)
        self.Linear3 = nn.Linear(256, self.os)

    def forward(self, l, v): # 600 x 600 x 1
        # encoder
        l = F.elu(self.conv0(l)) # 600 x 600 x 4
        l = F.elu(self.conv1(l)) # 600 x 600 x 4
        l = self.maxpool2(l) # 300 x 300 x 4
        l = F.elu(self.conv2(l)) # 300 x 300 x 8
        l = F.elu(self.conv3(l)) # 300 x 300 x 8
        l = self.maxpool3(l) # 100 x 100 x 8
        l = F.elu(self.conv4(l)) # 100 x 100 x 16
        l = F.elu(self.conv5(l)) # 100 x 100 x 16
        l = self.maxpool3_2(l) # 34 x 34 x 16

        #context module
        l = self.spatial_dropout(F.elu(self.Layer1(l))) # 34 x 34 x 96
        l = self.spatial_dropout(F.elu(self.Layer2(l))) # 34 x 34 x 96
        l = self.spatial_dropout(F.elu(self.Layer3(l))) # 34 x 34 x 96
        l = self.spatial_dropout(F.elu(self.Layer4(l))) # 34 x 34 x 96
        l = self.spatial_dropout(F.elu(self.Layer5(l))) # 34 x 34 x 96
        l = self.spatial_dropout(F.elu(self.Layer12(l))) # 34 x 34 x 96
        l = F.elu(self.Layer13(l)) # 34 x 34 x 16

        #decoder
        l = l.view(-1, 34*34*16) # 18496
        v = v.view(-1, (self.n_past_steps+1) * self.values_width)
        x = torch.cat((l,v),1) # 18826
        x = F.elu(self.Linear1(x)) # 540
        x = F.elu(self.Linear2(x)) # 256
        x = self.Linear3(x) # 60

        return x

class LucaNetwork(nn.Module):
    def __init__(self, n_past_lidars, n_past_steps, n_future_steps):
        super(LucaNetwork, self).__init__()

        # Change these values if you want to change the number of steps in the
        # input or output
        self.values_width = 11 # the number of values on each row in values
        self.vs = (n_past_steps + 1) * self.values_width # values size
        self.os = n_future_steps * 2 # output size

        #encoder
        self.conv0 = nn.Conv2d(n_past_lidars+1,8,3, padding=1) # with elu #twice!
        self.conv1 = nn.Conv2d(8,8,3, padding=1)
        self.maxpool1 = nn.MaxPool2d(2, stride=2)
        self.conv2 = nn.Conv2d(8,16,3, padding=1) # with elu
        self.conv3 = nn.Conv2d(16,16,3, padding=1) # with elu #twice!
        self.maxpool2 = nn.MaxPool2d(2, stride=2)

        # context modules
        # all followed by elu
        # layer 1-12 is also followed by spatial dropout layer with pd=0.20
        self.spatial_dropout = nn.Dropout2d(p=0.2)
        self.Layer1 = nn.Conv2d(16,96,3, padding=1, dilation=1)
        self.Layer2 = nn.Conv2d(96,96,3, padding=1, dilation=1)
        self.Layer3 = nn.Conv2d(96,96,3, padding=(2,1), dilation=(2,1))
        self.Layer4 = nn.Conv2d(96,96,3, padding=(4,2), dilation=(4,2))
        self.Layer5 = nn.Conv2d(96,96,3, padding=(8,4), dilation=(8,4))
        self.Layer6 = nn.Conv2d(96,96,3, padding=(12,8), dilation=(12,8))
        self.Layer7 = nn.Conv2d(96,96,3, padding=(16,12), dilation=(16,12))
        self.Layer8 = nn.Conv2d(96,96,3, padding=(20,16), dilation=(20,16))
        self.Layer9 = nn.Conv2d(96,96,3, padding=(24,20), dilation=(24,20))
        self.Layer10 = nn.Conv2d(96,96,3, padding=(28,24), dilation=(28,24))
        self.Layer11 = nn.Conv2d(96,96,3, padding=(32,28), dilation=(32,28))
        self.Layer12 = nn.Conv2d(96,96,3, padding=(1,32), dilation=(1,32))
        self.Layer13 = nn.Conv2d(96,16,3, padding=1, dilation=1)

        #decoder #all followed by elu
        #concatenate new values here
        self.Linear1 = nn.Linear(360330,1000) # add more data here
        self.Linear2 = nn.Linear(1000,500)
        self.Linear3 = nn.Linear(500, 500)
        self.Linear4 = nn.Linear(500, self.os)

    def forward(self, l, v): # 600 x 600 x 1
        # encoder
        l = F.elu(self.conv0(l)) # 600 x 600 x 8
        l = F.elu(self.conv1(l)) # 600 x 600 x 8
        l = self.maxpool1(l) # 300 x 300 x 8
        l = F.elu(self.conv2(l)) # 300 x 300 x 16
        l = F.elu(self.conv3(l)) # 300 x 300 x 16
        #l = F.elu(self.conv3(l)) # 300 x 300 x 16
        l = self.maxpool2(l) # 150 x 150 x 16
        #context module
        l = self.spatial_dropout(F.elu(self.Layer1(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer2(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer3(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer4(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer5(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer6(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer7(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer8(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer9(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer10(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer11(l))) # 150 x 150 x 96
        l = self.spatial_dropout(F.elu(self.Layer12(l))) # 150 x 150 x 96
        l = F.elu(self.Layer13(l)) # 150 x 150 x 16

        # decoder
        l = l.view(-1, 150*150*16) # 360000
        v = v.view(-1, (self.n_past_steps+1) * self.values_width)
        x = torch.cat((l,v),1) # 360330
        x = F.elu(self.Linear1(x)) # 5000
        x = F.elu(self.Linear2(x)) # 1000
        x = F.elu(self.Linear3(x)) # 500
        x = self.Linear4(x)

        return x

max_stars_repo_name: AnnaNylander/exjobb

max_issues_repo_name: AnnaNylander/exjobb

max_forks_repo_name: AnnaNylander/exjobb


text:
"""
There are many mouse events available in cv2 package.\
In this code we wanna handle this mouse events.

(Acccessing Coordinate and BGR of a point)
"""
import cv2
# import numpy as np

events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)
"""These lines of code list out all of the events in cv2 package."""

def click_events(event, x, y, flags, param):
    """
    Show the coordinate of the position where we clicked left button down\
    and show the BGR of the position where we clicked right button down.

    Args:
        event ([int]): What kind of click is it?
        x ([int]): The x coordinate of the postion where event occurred.
        y ([int]): The y coordinate of the postion where event occurred.
        flags ([type]): [description] (IDK)
        param ([type]): [description] (IDK)
    """
    if event is cv2.EVENT_LBUTTONDOWN:
        """
        If you click left button down the coordinate of the postion
        where we clicked left button down, will be shown.
        """
        print(x, ', ', y)
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = f'{x}, {y}'
        cv2.putText(img, strXY, (x, y), font, .3, (255, 0, 0), 1)
        cv2.imshow('image', img)

    if event is cv2.EVENT_RBUTTONDOWN:
        """
        If you click right button down the BGR of the the postion
        where we clicked right button down, will be shown.
        """
        blue = img[y, x, 0]
        green = img[y, x, 1]
        red = img[y, x, 2]
        """
         img Args:
            1. y coordinate
            2. x coordinate
            3. BGR (0 for B 1 for G and 2 for R)
        """
        print(f'{blue}, {green}, {red}')
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = f'{blue}, {red}, {green}'
        cv2.putText(img, strBGR, (x, y), font, .3,
                    (int(255-blue), int(255-green), int(255-red)), 1)
        """These lines of code print out the BGR
        of the of the position where we clicked down
        on the image.
        The color of this text is opposite of the coordinate color.
        """
        cv2.imshow('image', img)

img = cv2.imread('Medias/lena.jpg')
cv2.imshow('image', img)
cv2.setMouseCallback('image', click_events)
"""
setMouseCallback(windowName, onMouse [, param]) -> None
"""
k = cv2.waitKey(0) & 0xFF
if k == 27:
    cv2.destroyAllWindows()
elif k == ord('s'):
    cv2.imwrite('img_copy.jpg', img)
    cv2.destroyAllWindows()

ext: py

max_stars_repo_name: ErfanRasti/PythonCodes

max_issues_repo_name: ErfanRasti/PythonCodes

max_forks_repo_name: ErfanRasti/PythonCodes

reason: import numpy


text:
# Copyright (c) Dietmar Wolz.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory.

# See https://github.com/dietmarwo/fast-cma-es/blob/master/tutorials/Scheduling.adoc for a detailed description.

import math
import pandas as pd
import numpy as np
import sys, math, time
from fcmaes import retry, advretry, mode, modecpp
from fcmaes.optimizer import logger, Bite_cpp, Cma_cpp, De_cpp, de_cma, dtime, Dual_annealing, Differential_evolution, Minimize
from scipy.optimize import Bounds
import ctypes as ct
import multiprocessing as mp
from numba import njit, numba

STATION_NUM = 12 # number of dyson ring stations
TRAJECTORY_NUM = 50 # we select 10 mothership trajectories from these trajectories
ASTEROID_NUM = 83454 # number of asteroids

MAX_TIME = 20.0 # mission time in years
WAIT_TIME = 90/365.25 # years, after arrival wait until construction may start
ALPHA = 6.0e-9 # conversion factor time of flight -> arrival mass
#A_DYSON = 1.3197946923098154 # ESAs dyson sphere
A_DYSON = 1.1 # Tsinhuas dyson sphere

DAY = 24 * 3600
YEAR = DAY*365.25

@njit(fastmath=True)
def select(asteroid, station, trajectory, mass, transfer_start, transfer_time, x):
    trajectories = trajectory_selection(x, TRAJECTORY_NUM)[1] # select 10 trajectories representing the 10 mothership trajectories
    stations = dyson_stations(x, STATION_NUM) # derive dyson_stations targeted at time slots from argument vector
    times = timings(x, STATION_NUM) # derive station build time slot boundaries from argument vector (in years)
    slot_mass = np.zeros(STATION_NUM) # mass sum per time slot
    ast_val = np.zeros(ASTEROID_NUM) # deployed mass for asteroid
    ast_slot = np.zeros(ASTEROID_NUM, dtype=numba.int32) # build time slot used for asteroid
    for i in range(asteroid.size):
        tr = int(trajectory[i]) # current trajectory
        if trajectories[tr] == 0: # trajectory not selected
            continue
        ast_id = int(asteroid[i]) # asteroid transferred
        stat = int(station[i]) # dyson sphere station targeted
        m = mass[i] # estimated asteroid mass at arrival time
        time_of_flight = transfer_time[i] # TOF of asteroid transfer
        arrival_time = transfer_start[i] + transfer_time[i] # arrival time of asteroid transfer
        # which station time slot ?
        for slot in range(STATION_NUM):
            max_time = times[slot+1] # time interval of time slot
            slot_time = times[slot]
            min_time = slot_time + WAIT_TIME # we have to wait 90 days
            if min_time >= MAX_TIME:
                continue
            if arrival_time >= slot_time and arrival_time <= max_time: # inside time slot
                if stat == stations[slot]: # does the station fit?
                    tof = time_of_flight
                    #if we have to fly a non optimal transfer, arrival mass is reduced
                    if arrival_time < min_time: # 90 DAYS are not yet over
                        to_add = min_time - arrival_time # add time difference
                        to_add *= math.sqrt(1 + to_add/WAIT_TIME) # add some more time to enable transfer
                        tof += to_add
                    mval = (1.0 - YEAR*tof*ALPHA) * m # estimated asteroid mass at arrival time
                    if ast_val[ast_id] > 0: # asteroid already transferred
                        old_slot = ast_slot[ast_id]
                        min_mass = np.amin(slot_mass) # greedily replace if current mass is higher
                        old_mass = slot_mass[old_slot] # but never replace at a nearly minimal slot
                        if (old_slot == slot or min_mass < 0.99*old_mass) and ast_val[ast_id] < mval:
                            # replace with actual transfer, remove old asteroid mass
                            slot_mass[old_slot] -= ast_val[ast_id]
                        else: # keep old transfer, don't use the new one
                            mval = 0
                    if mval > 0:  # register actual transfer
                        slot_mass[slot] += mval
                        ast_val[ast_id] = mval
                        ast_slot[ast_id] = slot
    slot_mass.sort()
    min_mass = slot_mass[0]
    f = 1.0
    for m in slot_mass:
        # help the optimizer in case the minimum is 0
        min_mass += f*m
        f *= 0.5
    return min_mass, slot_mass

class fitness(object): # the objective function

    def __init__(self, transfers):
        self.evals = mp.RawValue(ct.c_long, 0)  # writable across python processes
        self.best_y = mp.RawValue(ct.c_double, math.inf) # writable across python processes
        self.t0 = time.perf_counter()
        self.transfers = transfers
        self.asteroid = transfers["asteroid"].to_numpy()
        self.station = transfers["station"].to_numpy()
        self.trajectory = transfers["trajectory"].to_numpy()
        self.transfer_start = transfers["transfer_start"].to_numpy()
        self.transfer_time = transfers["transfer_time"].to_numpy()
        self.mass = transfers["mass"].to_numpy()
        self.dv = transfers["dv"].to_numpy()
        self.trajectory_dv = trajectory_dv(self.asteroid, self.trajectory, self.dv)
        self.nobj = 2
        self.ncon = 0

    def __call__(self, x):
        # determine the minimal station mass
        min_mass, slot_mass = select(self.asteroid, self.station, self.trajectory, self.mass,
                        self.transfer_start, self.transfer_time, x)
        sdv = select_dvs(self.trajectory_dv, x)
        y = -score(min_mass, sdv)
        self.evals.value += 1
        if y < self.best_y.value:
            self.best_y.value = y
            trajectories = trajectory_selection(x, TRAJECTORY_NUM)[0]
            stations = dyson_stations(x, STATION_NUM)
            times = timings(x, STATION_NUM)

            sc = score(np.amin(slot_mass), sdv)

            logger().info("evals = {0}: time = {1:.1f} s = {2:.0f} a = {3:.0f} t = {4:s} s = {5:s} b = {6:s} m = {7:s} dv = {8:s}"
                .format(self.evals.value, dtime(self.t0), sc, ast_num(x, self.asteroid, self.trajectory),
                        str([round(ti,2) for ti in times[1:-1]]),
                        str([int(si) for si in stations]),
                        str([int(ti) for ti in trajectories]),
                        str([round(mi,2) for mi in slot_mass*1E-15]),
                        str([round(di,2) for di in sdv])
                        ))
        return y

    def fun(self, x):
        min_mass, slot_mass = select(self.asteroid, self.station, self.trajectory, self.mass,
                        self.transfer_start, self.transfer_time, x)
        sdv = select_dvs(self.trajectory_dv, x)
        scr, dv_val = score_vals(np.amin(slot_mass), sdv)
        y = -scr
        ys = [-min_mass*1E-10, dv_val]
        self.evals.value += 1

        if y < self.best_y.value:
            self.best_y.value = y
            trajectories = trajectory_selection(x, TRAJECTORY_NUM)[0]
            stations = dyson_stations(x, STATION_NUM)
            times = timings(x, STATION_NUM)
            sc = score(np.amin(slot_mass), sdv)
            logger().info("evals = {0}: time = {1:.1f} s = {2:.0f} a = {3:.0f} t = {4:s} s = {5:s} b = {6:s} m = {7:s} dv = {8:s}"
                .format(self.evals.value, dtime(self.t0), -self.best_y.value, ast_num(x, self.asteroid, self.trajectory),
                        str([round(ti,2) for ti in times[1:-1]]),
                        str([int(si) for si in stations]),
                        str([int(ti) for ti in trajectories]),
                        str([round(mi,2) for mi in slot_mass*1E-15]),
                        str([round(di,2) for di in sdv])
                        ))
        return ys

def optimize():
    name = 'tsin3000.60' # 60 trajectories to choose from
    # name = 'tsin3000.10' # 10 fixed trajectories
    transfers = pd.read_csv('data/' + name + '.xz', sep=' ', usecols=[1,2,3,4,5,6,7], compression='xz',
                    names=['asteroid', 'station', 'trajectory', 'mass', 'dv', 'transfer_start', 'transfer_time'])
    # uncomment to write a clear text csv
    # transfers.to_csv('data/' + name + '.txt', sep=' ', header=False)

    global TRAJECTORY_NUM, ASTEROID_NUM # adjust number of asteroids / trajectories
    TRAJECTORY_NUM = int(np.amax(transfers["trajectory"]) + 1)
    ASTEROID_NUM = int(np.amax(transfers["asteroid"]) + 1)

    # bounds for the objective function
    dim = 10+2*STATION_NUM-1
    lower_bound = np.zeros(dim)
    # lower_bound[10+STATION_NUM:dim] = 0.00001
    upper_bound = np.zeros(dim)
    lower_bound[:] = 0.0000001
    upper_bound[10:] = 0.9999999
    upper_bound[:10] = TRAJECTORY_NUM-0.00001 # trajectory indices
    bounds = Bounds(lower_bound, upper_bound)

    fit = fitness(transfers)
    fit.bounds = bounds

    # multi objective optimization 'modecpp' multi threaded, NSGA-II population update
    xs, front = modecpp.retry(fit.fun, fit.nobj, fit.ncon, fit.bounds, num_retries=640, popsize = 96,
                  max_evaluations = 3000000, nsga_update = True, logger = logger(), workers=16)

    # multi objective optimization 'modecpp' multi threaded, DE population update
    # xs, front = modecpp.retry(fit.fun, fit.nobj, fit.ncon, fit.bounds, num_retries=640, popsize = 67,
    #               max_evaluations = 3000000, nsga_update = False, logger = logger(), workers=16)

    # smart boundary management (SMB) with DE->CMA
    # store = advretry.Store(fitness(transfers), bounds, num_retries=10000, max_eval_fac=5.0, logger=logger())
    # advretry.retry(store, de_cma(10000).minimize)

    # smart boundary management (SMB) with CMA-ES
    # store = advretry.Store(fitness(transfers), bounds, num_retries=10000, max_eval_fac=5.0, logger=logger())
    # advretry.retry(store, Cma_cpp(10000).minimize)

    # BiteOpt algorithm multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Bite_cpp(1000000, M=1).minimize, num_retries=3200)

    # CMA-ES multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Cma_cpp(1000000).minimize, num_retries=3200)

    # scipy minimize algorithm multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Minimize(1000000).minimize, num_retries=3200)

    # fcmaes differential evolution multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, De_cpp(1000000).minimize, num_retries=3200)

    # scipy differential evolution multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Differential_evolution(1000000).minimize, num_retries=3200)

    # scipy dual annealing multi threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Dual_annealing(1000000).minimize, num_retries=3200)

    # scipy differential evolution single threaded
    # store = retry.Store(fitness(transfers), bounds, logger=logger())
    # retry.retry(store, Differential_evolution(1000000).minimize, num_retries=320, workers=1)

    return store.get_xs(), store.get_ys()

# utility functions

@njit(fastmath=True)
def next_free(used, p):
    while used[p]:
        p = (p + 1) % used.size
    used[p] = True
    return p

@njit(fastmath=True)
def disjoined(s, n):
    disjoined_s = np.zeros(s.size, dtype=numba.int32)
    used = np.zeros(n, dtype=numba.boolean)
    for i in range(s.size):
        disjoined_s[i] = next_free(used, s[i])
    return disjoined_s, used

@njit(fastmath=True)
def timings(x, n):
    times = np.zeros(n+1)
    for i in range(n-1):
        times[i] = MAX_TIME * x[10+STATION_NUM+i]
    times[n-1] = 0
    times[n] = MAX_TIME
    times.sort()
    return times

@njit(fastmath=True)
def dyson_stations(x, n):
    stations = np.argsort(x[10:10+n])
    # station numbers start with 1
    return np.array([s+1 for s in stations])

@njit(fastmath=True)
def trajectory_selection(x, n):
    trajectories = np.zeros(10, dtype=numba.int32)
    for i in range(10):
        trajectories[i] = int(x[i])
    return disjoined(trajectories, n)

@njit(fastmath=True)
def score(min_mass, trajectory_dv):
    mass_val = min_mass * 1E-10
    dv_val = 0
    for dv in trajectory_dv:
        dv_val += (1.0 + dv/50.0)**2
    return mass_val / (A_DYSON * A_DYSON * dv_val)

@njit(fastmath=True)
def score_vals(min_mass, trajectory_dv):
    mass_val = min_mass * 1E-10
    dv_val = 0
    for dv in trajectory_dv:
        dv_val += (1.0 + dv/50.0)**2
    return mass_val / (A_DYSON * A_DYSON * dv_val), dv_val

@njit(fastmath=True)
def trajectory_dv(asteroid, trajectory, delta_v):
    ast_dv = np.zeros((TRAJECTORY_NUM,ASTEROID_NUM))
    for i in range(asteroid.size):
        ast_id = int(asteroid[i]) # asteroid transferred
        tr = trajectory[i] # current trajectory
        ast_dv[tr, ast_id] = delta_v[i] # mothership delta velocity to reach the asteroid
    trajectory_dv = np.sum(ast_dv, axis=1)
    return trajectory_dv

@njit(fastmath=True)
def select_dvs(bdv, x):
    trajectories = trajectory_selection(x, TRAJECTORY_NUM)[0]
    sdv = np.zeros(10)
    for i in range(10):
        sdv[i] = bdv[int(trajectories[i])]
    return sdv

@njit(fastmath=True)
def ast_num(x, asteroid, trajectory):
    asts = np.zeros((ASTEROID_NUM))
    trajectories = trajectory_selection(x, TRAJECTORY_NUM)[1]
    for i in range(asteroid.size):
        if not trajectories[trajectory[i]]: # trajectory not selected
            continue
        asts[int(asteroid[i])] = 1 # asteroid transferred
    return np.sum(asts)

def main():
   optimize()

if __name__ == '__main__':
    main()

reason: import numpy,from scipy,from numba


text:
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy as np

kernel_str = """
    __global__ void vector_add(const float *a, const float *b, float *c, int N)
    {
       int idx = blockDim.x * blockIdx.x + threadIdx.x;
       if (idx < N) {
         c[idx] = a[idx] + b[idx];
       }
    }
"""

def test_vector_add():
    print("Max block dim x = ",cuda.Device(0).get_attribute(cuda.device_attribute.MAX_BLOCK_DIM_X))
    n = 1024*1024
    a = np.array([i for i in range(n)], dtype=np.float32)
    b = np.ones(n, dtype=np.float32)
    c = np.zeros(n, dtype=np.float32)
    print("Vector size (MB) = ",a.nbytes/1024.0/1024.0)

    mod = SourceModule(kernel_str)

    a_gpu = cuda.mem_alloc(a.nbytes)
    b_gpu = cuda.mem_alloc(b.nbytes)
    c_gpu = cuda.mem_alloc(c.nbytes)

    cuda.memcpy_htod(a_gpu, a)
    cuda.memcpy_htod(b_gpu, b)

    vector_add_func = mod.get_function("vector_add")

    start = cuda.Event()
    end = cuda.Event()

    start.record()
    vector_add_func(a_gpu, b_gpu, c_gpu, np.int32(n), block=(1024,1,1),grid=(int(n/1024.0)+1,1))
    end.record()
    end.synchronize()
    kernel_ms = end.time_since(start)
    print("kernel ms = ",kernel_ms)

    bw_bytes = a.nbytes + b.nbytes + c.nbytes
    print("BW (GB/s) = ",bw_bytes*1e-6/kernel_ms)

    cuda.memcpy_dtoh(c, c_gpu)
    print("c[0] = ",c[0])
    print("c[-1] = ",c[-1])

if __name__ == '__main__':
    test_vector_add()

reason: import numpy,import pycuda,from pycuda


text:
from __future__ import print_function, absolute_import, division  # makes these scripts backward compatible with python 2.6 and 2.7

# Importing the base class
from time_integration_base_scheme import TimeIntegrationBaseScheme

# Importing tools
from co_simulation_tools import ValidateAndAssignDefaults

# Other imports
import numpy as np
import json
import os

def CreateScheme(scheme_settings):
    return TimeIntegrationBackwardEuler1Scheme(scheme_settings)

# PMT to be checked, seems to be written acceleration based, might need correction

class TimeIntegrationBackwardEuler1Scheme(TimeIntegrationBaseScheme):
    """
    A single-degree-of-freedom SDoF model

    Using for testing of the MDoF solver
    """
    def __init__(self, scheme_settings):

        default_settings = {
                "type"      : "backward_euler1",
                "settings"  : {}
            }

        ValidateAndAssignDefaults(default_settings, scheme_settings)

        # add buffer size - this is not user-specified
        # each derived scheme specififes it
        scheme_settings["settings"].update({"buffer_size":3})

        # base scheme settings
        super(TimeIntegrationBackwardEuler1Scheme, self).__init__(scheme_settings["settings"])

    def _AssembleLHS(self, model):
        """
        """
        return model.m

    def _AssembleRHS(self, model):
        """
        """
        self.buffer[3,0,:] = self.force

        RHS = self.buffer[3,1,:] * self.dt**2
        RHS -= np.dot(- 2 * model.m + model.b * self.dt, self.buffer[0,1,:])
        RHS -= np.dot(model.m - model.b * self.dt + model.k * self.dt**2, self.buffer[0,2,:])

        return RHS

    def UpdateDerivedValues(self):
        """
        """
        self.buffer[1,0,:] = (self.buffer[0,0,:] - self.buffer[0,1,:]) / self.dt
        self.buffer[2,0,:] = (self.buffer[0,0,:] - 2 * self.buffer[0,1,:] + self.buffer[0,2,:]) / self.dt**2

max_stars_repo_name: lcirrott/Kratos

max_issues_repo_name: lcirrott/Kratos

max_forks_repo_name: lcirrott/Kratos


text:
# from tuneta.optimize import _weighted_spearman
import pandas as pd
from tabulate import tabulate
import numpy as np
import re
from scipy.spatial.distance import squareform, pdist
import dcor

def col_name(function, study_best_params):
    """
    Create consistent column names given string function and params
    :param function:  Function represented as string
    :param study_best_params:  Params for function
    :return:
    """

    # Optuna string of indicator
    function_name = function.split("(")[0].replace(".", "_")

    # Optuna string of parameters
    params = re.sub('[^0-9a-zA-Z_:,]', '', str(study_best_params)).replace(",", "_").replace(":", "_")

    # Concatenate name and params to define
    col = f"{function_name}_{params}"

    # Remove any trailing underscores
    col = re.sub(r'_$', '', col)
    return col

def distance_correlation(a, b):
    return dcor.distance_correlation(a, b)

def remove_consecutive_duplicates_and_nans(s):
    shifted = s.astype(object).shift(-1, fill_value=object())
    return s.loc[
        (shifted != s)
        & ~(shifted.isna() & s.isna())
    ]

# import seaborn as sns
# import matplotlib.pyplot as plt

def gen_plot(indicators, title):
    data = pd.DataFrame()
    for fitted in indicators.fitted:
        fitted.fitness = []
        fitted.length = []
        for trial in fitted.study.trials:
            print(trial)
            fitted.fitness.append(trial.value)
            fitted.length.append(trial.params['length'])
        fitted.fitness = pd.Series(fitted.fitness, name="Correlation")
        fitted.length = pd.Series(fitted.length, name="Length")
        fitted.data = pd.DataFrame([fitted.fitness, fitted.length]).T
        fitted.fn = fitted.function.split('(')[0]
        fitted.data['Indicator'] = fitted.fn
        data = pd.concat([data, fitted.data])
        fitted.x = fitted.study.best_params['length']
        fitted.y = fitted.study.best_value

    plt.figure(figsize=(10, 6))
    sns.scatterplot(x="Length", y="Correlation", data=data, hue="Indicator")
    plt.title(title)
    for fit in indicators.fitted:
        plt.vlines(x=fit.x, ymin=0, ymax=fit.y, linestyles='dotted')

reason: import numpy,from scipy


text:
import cPickle as pickle
import numpy as np
import tensorflow as tf
from util import accuracy
from ch02.util.matplot_util import draw_plot

pickle_file = 'notMNIST.pickle'

image_size = 28
num_of_labels = 10

RELU = 'RELU'
RELU6 = 'RELU6'
CRELU = 'CRELU'
SIGMOID = 'SIGMOID'
ELU = 'ELU'
SOFTPLUS = 'SOFTPLUS'

def reformat(dataset, labels):
    dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
    # Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]
    labels = (np.arange(num_of_labels) == labels[:, None]).astype(np.float32)
    return dataset, labels

    def activation(name, features):
        if name == RELU:
            return tf.nn.relu(features)
        if name == RELU6:
            return tf.nn.relu6(features)
        if name == SIGMOID:
            return tf.nn.sigmoid(features)
        if name == CRELU:
            return tf.nn.crelu(features)
        if name == ELU:
            return tf.nn.elu(features)
        if name == SOFTPLUS:
            return tf.nn.softplus(features)

batch_size = 128
#activations = [RELU, RELU6, SIGMOID, CRELU, ELU, SOFTPLUS]
activations = [RELU, RELU6, SIGMOID, ELU, SOFTPLUS]
plot_loss = False

def run(name):
    print(name)
    print('---------')
    with open(pickle_file, 'rb') as f:
        save = pickle.load(f)
        training_dataset = save['train_dataset']
        training_labels = save['train_labels']
        validation_dataset = save['valid_dataset']
        validation_labels = save['valid_labels']
        test_dataset = save['test_dataset']
        test_labels = save['test_labels']
        del save  # hint to help gc free up memory
        print 'Training set', training_dataset.shape, training_labels.shape
        print 'Validation set', validation_dataset.shape, validation_labels.shape
        print 'Test set', test_dataset.shape, test_labels.shape

    train_dataset, train_labels = reformat(training_dataset, training_labels)
    valid_dataset, valid_labels = reformat(validation_dataset, validation_labels)
    test_dataset, test_labels = reformat(test_dataset, test_labels)
    print 'Training dataset shape', train_dataset.shape, train_labels.shape
    print 'Validation dataset shape', valid_dataset.shape, valid_labels.shape
    print 'Test dataset shape', test_dataset.shape, test_labels.shape

    graph = tf.Graph()
    no_of_neurons = 1024
    with graph.as_default():
        # Placeholder that will be fed
        # at run time with a training minibatch in the session
        tf_train_dataset = tf.placeholder(tf.float32,
                                          shape=(batch_size, image_size * image_size))
        tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_of_labels))
        tf_valid_dataset = tf.constant(valid_dataset)
        tf_test_dataset = tf.constant(test_dataset)

        # Variables.
        w1 = tf.Variable(
            tf.truncated_normal([image_size * image_size, no_of_neurons]))
        b1 = tf.Variable(tf.zeros([no_of_neurons]))
        if name == CRELU:
            w2 = tf.Variable(
                tf.truncated_normal([no_of_neurons*2, num_of_labels]))
        else:
            w2 = tf.Variable(
                tf.truncated_normal([no_of_neurons, num_of_labels]))
        b2 = tf.Variable(tf.zeros([num_of_labels]))

        hidden1 = activation(name,tf.matmul(tf_train_dataset, w1) + b1)

        # Training computation.
        logits = tf.matmul(hidden1, w2) + b2
        loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels))

        # Optimizer.
        optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

        # Predictions for the training, validation, and test data.
        train_prediction = tf.nn.softmax(logits)
        valid_prediction = tf.nn.softmax(
            tf.matmul(activation(name,tf.matmul(tf_valid_dataset, w1) + b1), w2) + b2)
        test_prediction = tf.nn.softmax(
            tf.matmul(activation(name,tf.matmul(tf_test_dataset, w1) + b1), w2) + b2)

    num_steps = 101
    minibatch_acc = []
    validation_acc = []
    loss_array = []

    with tf.Session(graph=graph) as session:
        tf.initialize_all_variables().run()
        print("Initialized")
        for step in xrange(num_steps):

            offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
            # Generate a minibatch.
            batch_data = train_dataset[offset:(offset + batch_size), :]
            batch_labels = train_labels[offset:(offset + batch_size), :]

            feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels}
            _, l, predictions = session.run(
                [optimizer, loss, train_prediction], feed_dict=feed_dict)
            minibatch_accuracy = accuracy(predictions, batch_labels)
            validation_accuracy = accuracy(
                valid_prediction.eval(), valid_labels)

            if (step % 10 == 0):
                print("Minibatch loss at step", step, ":", l)
                print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels))
                print("Validation accuracy: %.1f%%" % accuracy(
                    valid_prediction.eval(), valid_labels))
            minibatch_acc.append(minibatch_accuracy)
            validation_acc.append(validation_accuracy)
            loss_array.append(l)

        print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
    return validation_acc, loss_array

def main():
    t = []
    loss_ = []
    num_steps = 101
    x = np.arange(num_steps)
    for a in activations:
      val_acc, loss = run(a)
      t.append(np.array(val_acc))
      loss_.append(np.array(loss))
    if plot_loss ==False:
      title = "NotMNIST DataSet - Activation functions Validation accuracy"
      label = activations
      draw_plot(x, t, title, label)
    else:
      title = "NotMNIST DataSet - Activation functions Minibatch loss"
      label = activations
      draw_plot(x, loss_, title, label)

if __name__ == '__main__':
  main()


text:
'''Train CK+ with PyTorch.'''
# 10 crop for data enhancement
from __future__ import print_function

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import transforms as transforms
import numpy as np
import os
import argparse
import utils
from CK import CK
from torch.autograd import Variable
from models import *
import matplotlib.pyplot as plt
import visdom
import time

parser = argparse.ArgumentParser(description='PyTorch CK+ CNN Training')
parser.add_argument('--model', type=str, default='Resnet18', help='CNN architecture')
parser.add_argument('--dataset', type=str, default='CK+', help='dataset')
parser.add_argument('--fold', default=1, type=int, help='k fold number')
parser.add_argument('--bs', default=128, type=int, help='batch_size')
parser.add_argument('--lr', default=0.01, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
opt = parser.parse_args()

# use_cuda = torch.cuda.is_available()
use_cuda = True

best_Test_acc = 0  # best Test accuracy
best_Test_acc_epoch = 0
start_epoch = 0  # start from epoch 0 or last checkpoint epoch

learning_rate_decay_start = 20  # 50
learning_rate_decay_every = 1 # 5
learning_rate_decay_rate = 0.9 # 0.8

cut_size = 44
total_epoch = 60 # 60

path = os.path.join(opt.dataset + '_' + opt.model, str(opt.fold))

# Data
print('==> Preparing data..')
transform_train = transforms.Compose([
    transforms.RandomCrop(cut_size),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
])

# 数据扩增
# 随机切割，扩充数据集，减缓了过拟合的作用
transform_test = transforms.Compose([
    transforms.TenCrop(cut_size),
    transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
])

trainset = CK(split = 'Training', fold = opt.fold, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=opt.bs, shuffle=True, num_workers=0)
testset = CK(split = 'Testing', fold = opt.fold, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=5, shuffle=False, num_workers=0)

# Model
if opt.model == 'VGG19':
    net = VGG('VGG19')
elif opt.model == 'Resnet18':
    net = ResNet18()

if opt.resume:
    # Load checkpoint.
    print('==> Resuming from checkpoint..')
    assert os.path.isdir(path), 'Error: no checkpoint directory found!'
    checkpoint = torch.load(os.path.join(path,'Test_model.t7'))

    net.load_state_dict(checkpoint['net'])
    best_Test_acc = checkpoint['best_Test_acc']
    best_Test_acc_epoch = checkpoint['best_Test_acc_epoch']
    start_epoch = best_Test_acc_epoch + 1
else:
    print('==> Building model..')

if use_cuda:
    net.cuda()

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=opt.lr, momentum=0.9, weight_decay=5e-4)
# optimizer = optim.Adam(net.parameters(), lr=opt.lr, weight_decay=0)

batch_size = trainloader.batch_size
print("batch size =",batch_size)
print("Net =",opt.model)

# interval: 隔几个epoch取一个点
def drawCurve(in_list,interval,title):
    plt.figure()
    t = np.arange(len(in_list))
    plt.style.use("ggplot")
    plt.title(title)
    plt.xlabel("Epoch")
    plt.ylabel("Loss")
    plt.plot(interval*t, in_list) # 确保epoch的显示不受interval影响

train_loss_list = [0]
train_acc_list = [0]
test_loss_list = [0]
test_acc_list = [0]
# Training
def train(epoch):
    print('\nEpoch: %d' % epoch)
    print("Start Training!")
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate ** frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    # 迭代，迭代的次数 = 981(训练集大小)/batch_size = batch数，计算loss和accuracy
    # batch_idx计迭代数
    iter_num = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()

        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()

        # train_loss += loss.data[0]
        train_loss += loss.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        Train_acc = int(correct) / int(total)  # 也就是最后一次迭代的准确率，代表一个epoch的准确率（完整的数据集通过了神经网络）

        # utils.progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
        #     % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))

        iter_num += 1

    train_loss = train_loss / iter_num
    train_loss_list.append(train_loss)
    train_acc_list.append(Train_acc)
    print("Train Accuracy:",Train_acc*100,"%")
    print("Train Loss:",train_loss)

def test(epoch):
    print("Start Testing!")
    global Test_acc
    global best_Test_acc
    global best_Test_acc_epoch
    net.eval()
    test_loss = 0
    correct = 0
    total = 0
    iter_num = 0
    for batch_idx, (inputs, targets) in enumerate(testloader):
        bs, ncrops, c, h, w = np.shape(inputs)
        inputs = inputs.view(-1, c, h, w)

        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        with torch.no_grad():  # add
            inputs, targets = Variable(inputs), Variable(targets)
            outputs = net(inputs)
        outputs_avg = outputs.view(bs, ncrops, -1).mean(1)  # avg over crops

        loss = criterion(outputs_avg, targets)
        # test_loss += loss.data[0]
        test_loss += loss.item()
        _, predicted = torch.max(outputs_avg.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        Test_acc = int(correct) / int(total)

        # utils.progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
        #     % (test_loss / (batch_idx + 1), 100. * correct / total, correct, total))
        iter_num += 1

    test_loss = test_loss/iter_num
    test_loss_list.append(test_loss)
    test_acc_list.append(Test_acc)
    print("Test Accuracy:",Test_acc*100,"%")
    print("Test Loss:",test_loss)

    # Save checkpoint.
    if Test_acc > best_Test_acc:
        print('Saving..')
        print("best_Test_acc: %0.3f" % Test_acc)
        state = {'net': net.state_dict() if use_cuda else net,
            'best_Test_acc': Test_acc,
            'best_Test_acc_epoch': epoch,
        }
        if not os.path.isdir(opt.dataset + '_' + opt.model):
            os.mkdir(opt.dataset + '_' + opt.model)
        if not os.path.isdir(path):
            os.mkdir(path)
        torch.save(state, os.path.join(path, 'Test_model.t7'))
        best_Test_acc = Test_acc
        best_Test_acc_epoch = epoch

#开启visdom
vis1 = visdom.Visdom()
vis2 = visdom.Visdom()

# loss
win1 = vis1.line(
    X=np.array([0, 0]),
    Y=np.array([0, 0]),
    opts=dict(
        title='Loss on CK+',
        showlegend=False,
        xtickmin=0,
        xtickmax=60,
        xtickstep=5,
        ytickmin=0,
        ytickmax=5,
        ytickstep=1,
        markersymbol='dot',
        markersize=5
    ),
    name='Loss'
)
# accuracy
win2 = vis2.line(
    X=np.array([0, 0]),
    Y=np.array([0, 0]),
    opts=dict(
        title='Accuracy on CK+',
        showlegend=False,
        xtickmin=0,
        xtickmax=60,
        xtickstep=5,
        ytickmin=0,
        ytickmax=1,
        ytickstep=0.1,
        markersymbol='dot',
        markersize=5
    ),
    name='Accuracy'
)

#visdom可视化函数
def visualize(epoch):
    vis1.line(
        X=np.array([epoch, epoch + 1]),
        Y=np.array([train_loss_list[epoch], train_loss_list[epoch + 1]]),
        opts=dict(markercolor=np.array([1]),
                  markersymbol='dot'),
        win=win1,
        update='new',
        name='Train Loss '+str(epoch)
    )
    vis1.line(
        X=np.array([epoch, epoch + 1]),
        Y=np.array([test_loss_list[epoch], test_loss_list[epoch + 1]]),
        opts=dict(markercolor=np.array([1]),
                  markersymbol='dot'),
        win=win1,
        update='new',
        name='Test Loss '+str(epoch)
    )

    vis2.line(
        X=np.array([epoch, epoch + 1]),
        Y=np.array([train_acc_list[epoch], train_acc_list[epoch + 1]]),
        opts=dict(markercolor=np.array([1]),
                  markersymbol='dot'),
        win=win2,
        update='new',
        name='Train Accuracy '+str(epoch)
    )
    vis2.line(
        X=np.array([epoch, epoch + 1]),
        Y=np.array([test_acc_list[epoch], test_acc_list[epoch + 1]]),
        opts=dict(markercolor=np.array([1]),
                  markersymbol='dot'),
        win=win2,
        update='new',
        name='Test Accuracy ' + str(epoch)
    )

start_time = time.time()
for epoch in range(start_epoch, total_epoch):
    train(epoch)
    test(epoch)
    visualize(epoch)

end_time = time.time()
total_time= end_time- start_time
print("best_Test_acc: %0.3f" % best_Test_acc)
print("best_Test_acc_epoch: %d" % best_Test_acc_epoch)
print("time =",total_time,'s')

# drawCurve(train_loss_list,1,'Train Loss')
# drawCurve(train_acc_list,1,'Train Accuracy')
# drawCurve(test_loss_list,1,'Test Loss')
# drawCurve(test_acc_list,1,'Test Accuracy')
# plt.show()


text:
import cv2
import numpy as np
import face_recognition
import time
import pickle
def face_encode(path):
    all_face_encodings = {}
    # Load the cascade
    TIMER = int(5)
    # To capture video from webcam.
    #cap = cv2.VideoCapture(0)
    # To use a video file as input
    cap = cv2.VideoCapture('media/{}.mp4'.format(path))
    framerate = int(cap.get(cv2.CAP_PROP_FPS))
    if framerate % 2 != 0:
        framerate = framerate-1
    framecount = (framerate/2)-1
    count = 0
    while True:

        # Read and display each frame
        success, img = cap.read()
        framecount += 1
        try:
            imgS = cv2.resize(img,(0,0),None,0.25,0.25)
            imgS = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        except:
            break
        # Convert to grayscale

        if framecount == (framerate/2):
            framecount = 0
            cv2.imshow('image',img)
            try:
                facesCurFrame = face_recognition.face_locations(imgS)
                all_face_encodings["{0} {1}".format(path,count)] = face_recognition.face_encodings(imgS,facesCurFrame)[0]
                count = count + 1
            except:
                pass
        k = cv2.waitKey(30) & 0xff
        if k==27:
            break
        if count > 10   :
            break
    # Release the VideoCapture object
    cap.release()
    cv2.destroyAllWindows()
    # ... etc ...

    with open('media/{}.xml'.format(path), 'wb') as f:
        pickle.dump(all_face_encodings, f)

    return "{}.xml".format(path)

def check_face_encode(path):
    all_face_encodings = {}
    # Load the cascade
    TIMER = int(5)
    # To capture video from webcam.
    #cap = cv2.VideoCapture(0)
    # To use a video file as input
    cap = cv2.VideoCapture('media/{}.mp4'.format(path))
    framerate = int(cap.get(cv2.CAP_PROP_FPS))
    if framerate % 2 != 0:
        framerate = framerate-1
    framecount = (framerate/2)-1
    count = 0
    while True:

        # Read and display each frame
        success, img = cap.read()
        framecount += 1
        try:
            imgS = cv2.resize(img,(0,0),None,0.25,0.25)
            imgS = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        except:
            break
        # Convert to grayscale

        if framecount == (framerate/2):
            framecount = 0
            cv2.imshow('image',img)
            try:
                facesCurFrame = face_recognition.face_locations(imgS)
                all_face_encodings["{0} {1}".format(path,count)] = face_recognition.face_encodings(imgS,facesCurFrame)[0]
                count = count + 1
            except:
                pass
        k = cv2.waitKey(30) & 0xff
        if k==27:
            break
        if count > 10   :
            break
    # Release the VideoCapture object
    cap.release()
    cv2.destroyAllWindows()
    # ... etc ...

    with open('media/{}_check.xml'.format(path), 'wb') as f:
        pickle.dump(all_face_encodings, f)

    return "{}_check.xml".format(path)

def compair(path1,path2):

    with open('media/{}'.format(path1), 'rb') as f:
	    all_face_encodings = pickle.load(f)

    with open('media/{}'.format(path2), 'rb') as g:
	    all_check_face_encodings = pickle.load(g)

    face_encodings = np.array(list(all_face_encodings.values()))
    check_face_encodings = np.array(list(all_check_face_encodings.values()))
    result = face_recognition.compare_faces(face_encodings, check_face_encodings)
    res = 0
    for val in result:
        if val == True:
            res = res + 1
    print(res)
    if res >= (len(result)/2):
        print()
        return True
    else:
        return False


text:
import os
import math
from mp.paths import JIP_dir
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.linear_model import Ridge
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, plot_confusion_matrix

#set environmental variables
#for data_dirs folder, nothing changed compared to Simons version
os.environ["WORKFLOW_DIR"] = os.path.join(JIP_dir, 'data_dirs')
os.environ["OPERATOR_IN_DIR"] = "input"
os.environ["OPERATOR_OUT_DIR"] = "output"
os.environ["OPERATOR_TEMP_DIR"] = "temp"
os.environ["OPERATOR_PERSISTENT_DIR"] = os.path.join(JIP_dir, 'data_dirs', 'persistent')

# preprocessing dir and subfolders
os.environ["PREPROCESSED_WORKFLOW_DIR"] = os.path.join(JIP_dir, 'preprocessed_dirs')
os.environ["PREPROCESSED_OPERATOR_OUT_SCALED_DIR"] = "output_scaled"
os.environ["PREPROCESSED_OPERATOR_OUT_SCALED_DIR_TRAIN"] = "output_scaled_train"

#dir where train data for intensites is stored (this only needs to be trains_dirs, but since i have more
# datasets, another subfolder is here)
os.environ["TRAIN_WORKFLOW_DIR"] = os.path.join(JIP_dir, 'train_dirs')

#ignore
##below is for christian only, used for older data structures where models are trained on
os.environ["TRAIN_WORKFLOW_DIR_GT"] = os.path.join('Covid-RACOON','All images and labels')
os.environ["TRAIN_WORKFLOW_DIR_PRED"] = os.path.join('Covid-RACOON','All predictions')
#os.environ["TRAIN_WORKFLOW_DIR_GT"] = os.path.join('gt_small')
#os.environ["TRAIN_WORKFLOW_DIR_PRED"] = os.path.join('pred_small')

#which mode is active either 'train' or 'inference'
os.environ["INFERENCE_OR_TRAIN"] = 'train'

#ignore
# the ending of the image files in train_dir is only for older datasets
os.environ["INPUT_FILE_ENDING"] = 'nii.gz'

from mp.utils.preprocess_utility_functions import basic_preprocessing
from mp.utils.preprocess_utility_functions import extract_features_all_data,compute_all_prediction_dice_scores
from mp.utils.feature_extractor import Feature_extractor

# all steps in 1. are not used anymore
## 2. function to split data into splits depending on task
# 2.1 (SET splits for training and eval) set the filter function, such that the splits are as wanted
def filter_feature_extr(id,model):
    '''function to decide which split a prediction should be put in.
    Is infered from the id(giving the dataset of the image) and model
    (the dataset the predicting model was trained on)
    Args:
        id(str):giving the dataset of the image
        model(str):the dataset the predicting model was trained on

    Returns(str):the split for the pair of id and model, can be other, which can be
        category of unused data'''
    # # train data
    # if model[:7] in ['Task740'] and id[:7] in ['Task740','Task541']:
    #     return 'train'
    # # id data
    # if model[:7] in ['Task740'] and id[:7] in ['Task741']:
    #     return 'id_challenge'
    # if model[:7] in ['Task740'] and id[:7] in ['Task542']:
    #     return 'id_inhouse'
    # # od data
    # if model[:7] in ['Task740'] and id[:7] in ['Task200','Task201']:
    #     return 'ood_mosmed'

    # # train data
    # if model[:7] in ['Task740'] and id[:7] in ['Task740','Task541']:
    #     return 'id_train'
    # # id data
    # if model[:7] in ['Task740'] and id[:7] in ['Task741','Task542']:
    #     return 'id_test'
    # # od data
    # if model[:7] in ['Task740'] and id[:7] in ['Task200','Task201','Task100','Task101']:
    #     return 'ood_test'

    # train data
    if model[:7] in ['Task740'] and id[:7] in ['Task740']:
        return 'train'
    if model[:7] in ['Task740'] and id[:7] in ['Task541']:
        return 'train'
    # id data
    if model[:7] in ['Task740'] and id[:7] in ['Task741']:
        return 'id_challenge'
    if model[:7] in ['Task740'] and id[:7] in ['Task542']:
        return 'id_inhouse'
    # od data
    if model[:7] in ['Task740'] and id[:7] in ['Task200','Task201']:
        return 'ood_mosmed'
    if model[:7] in ['Task740'] and id[:7] in ['Task100','Task101']:
        return 'other'

    return 'other'

# 2.2 extract the feature
CLASS = False
def extract_features_train_id_od(filter,splits,used_feat=[0,1,2,3,4,5]):
    '''extracts the features from the preprocess dictionary, and returns a  list,
    in which each entry corresponds to the features/labels of one split
    Args:
        filter(function: (str,str)->str): should be modified filter_feature_extr
        splits(list(str)):the name of the splits
        used_feat(list(int)):the used features, see in main() for more depth

    Returns(list): list, in which each entry corresponds to the features/labels of one split'''
    X = []
    y = []
    paths_to_pred = []
    for i in range(len(splits)):
        X.append([])
        y.append([])
        paths_to_pred.append([])

    feat_extr = Feature_extractor()
    work_path = os.path.join(os.environ["PREPROCESSED_WORKFLOW_DIR"],os.environ["PREPROCESSED_OPERATOR_OUT_SCALED_DIR_TRAIN"])
    for id in os.listdir(work_path):
        all_pred_path = os.path.join(work_path,id,'pred')
        if os.path.exists(all_pred_path):
            for model in os.listdir(all_pred_path):
                split = filter(id,model)
                for i,s in enumerate(splits):
                    if split == s:
                        feature_path = os.path.join(all_pred_path,model,'features.json')
                        label_path = os.path.join(all_pred_path,model,'dice_score.json')
                        feat_vec = feat_extr.read_feature_vector(feature_path)
                        feat_vec = [feat_vec[index] for index in used_feat]
                        label = feat_extr.read_prediction_label(label_path)
                        if CLASS:
                            label = get_class(label)
                        if np.isnan(np.sum(np.array(feat_vec))) or feat_vec[0]>100000:
                            pass
                        else:
                            X[i].append(feat_vec)
                            y[i].append(label)
                            paths_to_pred[i].append(os.path.join(all_pred_path,model))

    return X,y,paths_to_pred

def get_class(label):
    if label < 0.2:
        return 1
    elif label < 0.4:
        return 2
    elif label < 0.6:
        return 3
    elif label < 0.8:
        return 4
    else:
        return 5

def get_float(classes):
    return np.array([(clas-1)/4 for clas in classes])

# 3. loss functions
def l2_loss(pred,truth):
    n = len(pred)
    return (1/n)*(np.sum((pred - truth)**2))

def l1_loss(pred,truth, std = False):
    losses = np.absolute(pred-truth)
    mean = np.mean(losses)
    std = np.std(losses)
    if std:
        return mean,std
    return mean

def l1_loss_overestimation(pred,truth,std=False):
    '''computes the error, but underpredictions
    are counted as correct predictions '''
    losses = pred-truth
    losses_pos = np.array([ max(loss,0) for loss in losses])
    mean = np.mean(losses_pos)
    std = np.std(losses_pos)
    if std:
        return mean,std
    return mean

def get_l1_losses(truth,pred):
    return np.absolute(truth-pred)

def confidence_bootstrap(X_train,y_train,X,y,svm,lr,scaler,splits):
    '''This is based on
    https://machinelearningmastery.com/confidence-intervals-for-machine-learning/
    I simply resample from the training data and then evaluate the performance of the model
    on each boostrap'''
    from numpy.random import randint
    from numpy import percentile
    scores = [[] for _ in range(len(splits))] #scores for smv per run
    scores_lr = [[] for _ in range(len(splits))]# scores for lr per run
    for _ in range(10000): # i conudct 10000 runs
        indices = randint(0,len(X_train),len(X_train)) #choose the indices in data to use

        X_train_b = X_train[indices]
        y_train_b = y_train[indices]
        svm = SVC(class_weight='balanced')
        lr = LogisticRegression(class_weight='balanced')
        svm.fit(X_train_b,y_train_b)
        lr.fit(X_train_b,y_train_b)

        for i,_ in enumerate(splits):

            X_eval = scaler.transform(X[i])
            y_eval = y[i]

            y_svm = svm.predict(X_eval)
            y_lr = lr.predict(X_eval)

            conf_ma = conf_matrix(y_eval,y_svm)
            conf_ma_lr = conf_matrix(y_eval,y_lr)

            scores[i].append(specificity(conf_ma))
            scores_lr[i].append(specificity(conf_ma_lr))
    lower_p = 2.5
    upper_p = 97.5
    # Now searach for the 95 percent intervalls done like in the link in
    # the description
    for i in range(len(splits)):
        print(splits[i])
        lower = max(0.0, percentile(scores[i], lower_p))
        print('%.1fth percentile = %.3f' % (lower_p, lower))
        upper = min(1.0, percentile(scores[i], upper_p))
        print('%.1fth percentile = %.3f' % (upper_p, upper))
    print()
    print('LR')
    for i in range(len(splits)):
        print(splits[i])
        lower = max(0.0, percentile(scores_lr[i], lower_p))
        print('%.1fth percentile = %.3f' % (lower_p, lower))
        upper = min(1.0, percentile(scores_lr[i], upper_p))
        print('%.1fth percentile = %.3f' % (upper_p, upper))

def conf_matrix(truth,pred):

    conf_ma =  confusion_matrix(np.array(truth),np.array(pred),labels=[1,2,3,4,5])
    #print(conf_ma)
    ma = np.zeros((2,2))
    ma [0,0] = np.sum(conf_ma[:3,:3])
    ma [0,1] = np.sum(conf_ma[:3,3:])
    ma [1,0] = np.sum(conf_ma[3:,:3])
    ma [1,1] = np.sum(conf_ma[3:,3:])
    return ma

def sensitivity(ma):
    found = int(ma[0,0])
    not_f = int(ma[0,1])
    if found == 0:
        return 0
    else:
        return found/(found+not_f)

def p_value_sensitivity(ma):
    found = int(ma[0,0])
    not_f = int(ma[0,1])
    return find_p_p_test(found,not_f)

def specificity(ma):
    tn = int(ma[1,1])
    fp = int(ma[1,0])
    return tn/(tn+fp)

def p_value_specificity(ma):
    tn = int(ma[1,1])
    fp = int(ma[1,0])
    return find_p_p_test(tn,fp)

def p_test(p,y,n):
    '''takes a p value for the null hypothesis, the number of
    successfull results and the number of unseccessfull results'''
    prob = 0
    combined = y + n
    for times in np.arange(y,combined+1,1):
        term = p**times * (1-p)**(combined-times)
        bin = math.comb(combined,times)
        prob += term * bin
    return prob

def find_p_p_test(y,n):
    for p in np.arange(1,0,-0.01):
        # print(p,p_test(p,y,n))
        if p_test(p,y,n) <= 0.05:
            return p, p_test(p,y,n)

def micro_sens(conf_ma):
    conf_ma = np.array(conf_ma)
    tp = 0
    p = np.sum(conf_ma)
    for i in range(0,5):
        tp += conf_ma[i,i]
    return tp/p

def micro_spec(conf_ma):
    conf_ma = np.array(conf_ma)
    tn_cum = 0
    fp_cum = 0
    all = np.sum(conf_ma)
    for i in range(5):
        fp = np.sum(conf_ma[:,i])-conf_ma[i,i]
        tp = conf_ma[i,i]
        fn = np.sum(conf_ma[i,:])-conf_ma[i,i]
        tn = all - (fp+tp+fn)
        tn_cum += tn
        fp_cum += fp
    return tn_cum/(tn_cum+fp_cum)

# 4. plots for further analysis.
# (OPTIONAL SET the saving path for the bar plots)
def l1_loss_bins(pred,truth,split,by_sign=True):
    '''computes and plots a bar plot for the errors of a model, sorted by bins depending on the truth,
    e.g. the error is taken for all predictions, where the ground trouth is in [0.5,0.6]. The errors
    are divided into over and under estimation. Additionally the number of data points and the std
    per bin is displayed
    Args:
        pred: the vector of predictions
        truth: the vector of ground truths
        split(str): the name of the split
        by_sign(bool): whether the errors should be divided into over and underestimation, setting to
            false is missing some normally displayed features'''
    save_path = os.path.join('storage','Results','Setup 5','all','Bins_{}'.format(split))
    if not by_sign:
        loss_by_bin = [[] for _ in range(10)]
        bins = np.array([])
        bins_std = np.array([])
        for i in range(10):
            for pre,tru in zip(pred,truth):
                if i*0.1 <= tru and (i+1)*0.1 > tru:
                    loss_by_bin[i].append(abs(pre-tru))
            if loss_by_bin[i]:
                bins = np.append(bins,np.mean(loss_by_bin[i]))
                bins_std = np.append(bins_std,np.std(loss_by_bin[i]))
            else:
                bins = np.append(bins,0)
                bins_std = np.append(bins_std,0)
        plt.bar(0.05+np.arange(10)*0.1,height = bins, width = 0.05,yerr = bins_std)
        plt.plot(0.05+np.arange(10)*0.1,[0.1 for i in range(10)],'-r')
        plt.title(split)
        plt.xlabel('bin (e.g. 0.1 to 0.2)')
        plt.ylabel('l1_error')
    else :
        widths = [[] for _ in range(10)]
        loss_by_bin = [[[],[]] for _ in range(10)]
        bins = [[] for _ in range(10)]
        bins_std = [[] for _ in range(10)]
        for i in range(10):
            for pre,tru in zip(pred,truth):
                if i*0.1 <= tru and (i+1)*0.1 > tru:
                    err = pre-tru
                    if err >= 0:
                        loss_by_bin[i][1].append(err)
                    else:
                        loss_by_bin[i][0].append(err)
            for j in range(2):
                if loss_by_bin[i][j]:
                    bins[i].append(np.mean(loss_by_bin[i][j]))
                    bins_std[i].append(np.std(loss_by_bin[i][j]))
                else:
                    bins[i].append(0)
                    bins_std[i].append(0)
            unders = len(loss_by_bin[i][0])
            overs = len(loss_by_bin[i][1])
            total = unders+overs
            if total > 0:
                if unders/total > 0.66:
                    widths[i]=[0.05,0.03]
                elif overs/total >0.66:
                    widths[i]=[0.03,0.05]
                else:
                    widths[i]=[0.05,0.05]
            else:
                widths[i]=[0.05,0.05]
        bins = np.array(bins)
        bins_std = np.array(bins_std)
        widths = np.array(widths)
        plt.bar(0.05+np.arange(10)*0.1,height = bins[:,0], width = widths[:,0],yerr = bins_std[:,0])
        plt.bar(0.05+np.arange(10)*0.1,height = bins[:,1], width = widths[:,1],yerr = bins_std[:,1])
        for i,x in enumerate(0.05+np.arange(10)*0.1):
            len_bin = len(loss_by_bin[i][0])
            if len_bin > 0:
                plt.text(x,bins[i,0],str(len_bin))
        for i,x in enumerate(0.05+np.arange(10)*0.1):
            len_bin = len(loss_by_bin[i][1])
            if len_bin > 0:
                plt.text(x,bins[i,1],str(len(loss_by_bin[i][1])))
        plt.plot(0.05+np.arange(10)*0.1,[0.1 for i in range(10)],'-r')
        plt.plot(0.05+np.arange(10)*0.1,[-0.1 for i in range(10)],'-r')
        plt.xlabel('y_true bin ')
        plt.ylabel('approximation error')
    plt.title(split)
    plt.savefig(save_path)
    plt.show()

# (OPTIONAL SET the saving path for the plots)
def plot_variable_influence(X_train,X,y,splits):
    '''for every feature in the data gives a scatter plot of all predicted segmentations,
    where the y axis is the dice score of the prediction and the x axis is the feature. The
    different splits are also visualised.
    Additionally trains a ridge regression, support vector regression and an MLP predictor
    on the data, who are also displayed
    Args:
        X_train: the data to train the models on
        X,y: The output of extract_features_train_id_od (so all features/labels sorted into
            groups by splits)
        splits(list): the name of the splits in a list
    '''
    #per variable
    for i in range(len(X[0][0])):

        save_path = os.path.join('storage','Results','Setup 5','Variable {}'.format(i))
        plt.title('Feature {} vs dice'.format(i))

        # Plot the models predictions based on single features
        X_train = [[X[0][k][i]] for k in range(len(X_train))]
        y_train = y[0]

        ridge = Ridge(normalize=False)
        svr = SVR()
        mlp = MLPRegressor((50,100,100,50))

        scaler = StandardScaler()
        X_train_s = scaler.fit_transform(X_train)

        ridge.fit(X_train_s,y_train)
        svr.fit(X_train_s,y_train)
        mlp.fit(X_train_s,y_train)

        x_min,x_max = np.min(X_train), np.max(X_train)
        x_span = x_max - x_min
        # take input range to plot model and scale it
        points = np.reshape(np.arange(x_min,x_max,step=x_span/100),(-1,1))
        points_s = scaler.transform(points)

        y_ridge = ridge.predict(points_s)
        y_svr = svr.predict(points_s)
        y_mlp = mlp.predict(points_s)

        plt.plot(points,y_ridge, label='ridge')
        plt.plot(points,y_svr, label='svr')
        plt.plot(points,y_mlp, label='mlp')

        #plot the data points
        for j,split in enumerate(splits):
            filt_X = [[X[j][k][i]] for k in range(len(X[j]))]
            plt.scatter(filt_X,y[j],label=split)

        plt.legend(loc='upper left')
        plt.xlabel('feature')
        plt.ylabel('dice score of prediction')
        #plt.savefig(save_path)
        plt.show()

def paper_figures_by_split(X_train,X,y,splits):
    #per variable
    #plt.rcParams.update({'font.size': 20})

    for i in range(len(X[0][0])):

        title,leg_loc,xlabel,legend_prop = figure_namesand_settings(i)
        save_path = os.path.join('storage','Results','for_paper','Variable {} _lesslabels'.format(i))
        # plt.title(title)

        for j,split in enumerate(splits):
            filt_X = [[X[j][k][i]] for k in range(len(X[j]))]
            plt.scatter(filt_X,y[j],label=split)
        if i == 0:
            pass #plt.legend(loc=leg_loc,prop = legend_prop)
        #plt.xlabel(xlabel)
        #plt.ylabel('Dice Score of segmentation')
        plt.xticks(fontsize=14)
        plt.yticks(fontsize=14)
        #plt.savefig(save_path)
        plt.show()

def figure_namesand_settings(feat_nr):
    if feat_nr == 0:
        title = 'Segmentation smoothness vs Dice Score prediction'
        leg_loc = 'upper left'
        xlabel = 'Segmentation smoothness'
        legend_prop = {'size':15}
    if feat_nr == 1:
        title = 'Number Connected Components vs Dice Score prediction'
        leg_loc = 'upper right'
        xlabel = 'Number Connected Components'
        legend_prop = {'size':8}
    if feat_nr == 2:
        title = 'Intensity Mode vs Dice Score prediction'
        leg_loc = 'upper right'
        xlabel = 'Intensity Mode'
        legend_prop = {'size':8}
    return title,leg_loc,xlabel,legend_prop

def find_ex_pred(X,y,paths):
    paths_ex = []
    seen_paths = ['storage\\JIP\\preprocessed_dirs\\output_scaled_train\\Task100_RadiopediaTrain_0007\\pred\\Task740_ChallengeTrainF4',
                    'storage\\JIP\\preprocessed_dirs\\output_scaled_train\\Task200_MosmedTrain_0027\\pred\\Task740_ChallengeTrainF4',
                    'storage\\JIP\\preprocessed_dirs\\output_scaled_train\\Task200_MosmedTrain_0013\\pred\\Task740_ChallengeTrainF4',
                    'storage\\JIP\\preprocessed_dirs\\output_scaled_train\\Task100_RadiopediaTrain_0008\\pred\\Task541_FrankfurtTrainF4']
    for i in range(len(X[0][0])):
        thresh = 0.4
        if i == 0:
            smallest_val = 1
            smallest_ind = [0,0]
            for j in range(len(X)):
                filt_X = np.array([[X[j][k][i]] for k in range(len(X[j]))])
                for k in range(len(filt_X)):
                    if filt_X[k] < smallest_val and y[j][k] <= thresh and paths[j][k] not in seen_paths:
                        smallest_val = filt_X[k]
                        smallest_ind = [j,k]
            paths_ex.append(paths[smallest_ind[0]][smallest_ind[1]])
            print(smallest_val)
            print(y[smallest_ind[0]][smallest_ind[1]])
        if i == 1:
            smallest_val = 1
            smallest_ind = [0,0]
            for j in range(len(X)):
                filt_X = np.array([[X[j][k][i]] for k in range(len(X[j]))])
                for k in range(len(filt_X)):
                    if filt_X[k] > smallest_val and y[j][k] <= thresh and paths[j][k] not in paths_ex and paths[j][k] not in seen_paths:
                        smallest_val = filt_X[k]
                        smallest_ind = [j,k]
            paths_ex.append(paths[smallest_ind[0]][smallest_ind[1]])
            print(smallest_val)
            print(y[smallest_ind[0]][smallest_ind[1]])
        if i == 2:
            smallest_val = 0
            smallest_ind = [0,0]
            for j in range(len(X)):
                filt_X = np.array([[X[j][k][i]] for k in range(len(X[j]))])
                for k in range(len(filt_X)):
                    if filt_X[k] > smallest_val and y[j][k] <= thresh and paths[j][k] not in paths_ex and paths[j][k] not in seen_paths:
                        smallest_val = filt_X[k]
                        smallest_ind = [j,k]
            paths_ex.append(paths[smallest_ind[0]][smallest_ind[1]])
            print(smallest_val)
            print(y[smallest_ind[0]][smallest_ind[1]])
        if i == 3:
            smallest_val = 1
            smallest_ind = [0,0]
            for j in range(len(X)):
                filt_X = np.array([[X[j][k][i]] for k in range(len(X[j]))])
                for k in range(len(filt_X)):
                    if filt_X[k] < smallest_val and y[j][k] <= thresh and paths[j][k] not in seen_paths:
                        smallest_val = filt_X[k]
                        smallest_ind = [j,k]
            paths_ex.append(paths[smallest_ind[0]][smallest_ind[1]])
            print(smallest_val)
            print(y[smallest_ind[0]][smallest_ind[1]])
    print(paths_ex)
    return paths_ex

def main(used_feat=[0,1,2,3,4,5],preprocessing=True,feature_extraction=True,
            extract_dice_scores=True,model_train=True,label=1):
    '''
    Args:
        used_feat(list(ints)): a list of ints to tell which features to use for model training:
                                1: density_distance
                                2: slice dice (smoothnes of segmentation)
                                3: slice dice diff (basically the first derivation of 2 (not really used)
                                4: connected components
                                5: mean of a gaussian fitted to intensity values
                                6: variance of a gaussain fitted to intenstiy values
        preprocessing(bool): whether to copy data from a directory into the working directoy
            includes things such as resizing and scaling of images
        train_density(bool): whether to retrain the density for intensity values (time consuming)
        feature_extraction(bool): weather to extract features from the data, not needed, when features.json
            are already in the working directory(in e.g. subfolder seg for an id)
        extract_dice_scores(bool): whether to extract the dice scores of the predictions, not needed
            when there are already dice_score.json in the working directory(in e.g. subfolder seg for an id)
        model_train(bool): whether to train and evaluate the models '''
    if preprocessing:
        basic_preprocessing(label)
    if feature_extraction:
        extract_features_all_data()
    if extract_dice_scores:
        compute_all_prediction_dice_scores()
    if model_train:

        scaler = StandardScaler()
        splits = ['train','id_challenge','id_inhouse','ood_mosmed'] #['train_gc','train_fra','gc_gc','gc_frank','gc_mosmed','gc_radio']['id_train','id_test','ood_test']['train','gc_gc','gc_frank','gc_mosmed','gc_radio']['id_train','id_test','ood_test']['other']
        X,y,paths_pred = extract_features_train_id_od(filter_feature_extr,splits,used_feat)

        X_train = scaler.fit_transform(X[0])
        y_train = np.array(y[0])
        # #START of experiments with classification task

# #################conf_ma class#################################################################################################################################
#         svm = SVC(class_weight='balanced')
#         lr = LogisticRegression(class_weight='balanced')
#         models = [svm,lr]
#         model_names = ['SVM','Logistic_Regression']

#         for model, model_name in zip(models,model_names):
#             model.fit(X_train,y_train)
#             for i,split in enumerate(splits):

#                 X_eval = scaler.transform(X[i])
#                 y_eval = y[i]

#                 plot_confusion_matrix(model,X_eval,y_eval,labels=[1,2,3,4,5])
#                 plt.title('Model {} on split {}'.format(model_name,split))
#                 plt.savefig(os.path.join('storage','Results','confusion_matrixes',model_name+' '+split))
#                 plt.show()
#                 # y_pred = model.predict(X_eval)

#                 # conf_matrix = confusion_matrix(y_eval,y_pred,labels=[1,2,3,4,5])
#                 # if i != 0:
#                 #     print(model_name,split)
#                 #     print(conf_matrix)
# #################conf_ma class#################################################################################################################################

# ##################3 feat models lr################################################################################################################################
#         lr = LogisticRegression(class_weight='balanced')
#         lr.fit(X_train,y_train)
#         for i,split in enumerate(splits):
#             X_eval = scaler.transform(X[i])
#             y_eval = y[i]
#             y_pred = lr.predict(X_eval)
#             ma = conf_matrix(y_eval,y_pred)
#             sens = sensitivity(ma)
#             spec = specificity(ma)
#             print(split)
#             print(sens, spec)
# ##################3 feat models lr################################################################################################################################
        # # confidence_bootstrap(X_train,y_train,X,y,svm,lr,scaler,splits)
        # model = svm
        # model.fit(X_train,[get_class(tru) for tru in y_train])
        # print(model)
        # for i,split in enumerate(splits):

        #     X_eval = scaler.transform(X[i])
        #     y_eval = y[i]
        #     y_gt = y_eval
        #     y_eval = np.array([get_class(tru) for tru in y_eval])

        #     y_pred =model.predict(X_eval)
        #     y_pred = get_float(y_pred)

        #     print(split)

        #     print('err :{}'.format(l1_loss(y_gt,y_pred,std=False)))
        #     print('overest errr :{}'.format(l1_loss_overestimation(y_gt,y_pred)))
        # model = lr
        # model.fit(X_train,y_train)
        # print(model)
        # for i,split in enumerate(splits):

        #     X_eval = scaler.transform(X[i])
        #     y_eval = y[i]

        #     y_pred =model.predict(X_eval)
        #     print(split)
        #     conf_ma = confusion_matrix(y_eval,y_pred,labels=[1,2,3,4,5])
        #     # print('sensitivity :{}'.format(micro_sens(conf_ma)))
        #     print('specificity :{}'.format(micro_spec(conf_ma)))
        # print('oberved, p-values')
        # print('LR')
        # lr.fit(X_train,y_train)
        # for i,split in enumerate(splits):

        #     X_eval = scaler.transform(X[i])
        #     y_eval = y[i]

        #     y_lr =lr.predict(X_eval)
        #     print(split)
        #     ma = conf_matrix(y_eval,y_lr)
        #     # print(ma)
        #     print('sensitivity :{},  {}'.format(sensitivity(ma), p_value_sensitivity(ma)))
        #     print('specificity :{},  {}'.format(specificity(ma), p_value_specificity(ma)))
        # print()
        # print('SVM')
        # svm.fit(X_train,y_train)
        # for i,split in enumerate(splits):

        #     X_eval = scaler.transform(X[i])
        #     y_eval = y[i]

        #     y_svm =svm.predict(X_eval)
        #     print(split)
        #     ma = conf_matrix(y_eval,y_svm)
        #     #print(ma)
        #     print('sensitivity :{},  {}'.format(sensitivity(ma), p_value_sensitivity(ma)))
        #     print('specificity :{},  {}'.format(specificity(ma), p_value_specificity(ma)))
        ##  END of classification tests

        # #START of part for regression task
        # plot_variable_influence(X_train,X,y,splits)
        # paper_figures_by_split(X_train,X,y,splits)
        # find_ex_pred(X,y,paths_pred)

############conf_ma regression####################################################################################################
        from sklearn.metrics._plot.confusion_matrix import ConfusionMatrixDisplay

        ridge = Ridge(normalize=False)
        svr = SVR()
        mlp = MLPRegressor((50,100,100,50))
        models = [ridge,svr,mlp]
        model_names = ['RidgeRegression','SVR','MLP']

        for model, model_name in zip(models,model_names):
            model.fit(X_train,y_train)
            for i,split in enumerate(splits):

                X_eval = scaler.transform(X[i])
                y_eval = y[i]

                y_pred_cont = model.predict(X_eval)

                #In order to get a confusion matrix, we target and prediction values need to be binned:
                y_eval = [get_class(truth) for truth in y_eval]
                y_pred = [get_class(pred) for pred in y_pred_cont]
                conf_matrix = confusion_matrix(y_eval,y_pred,labels=[1,2,3,4,5])
                print(conf_matrix)
                disp = ConfusionMatrixDisplay(conf_matrix,display_labels=[1,2,3,4,5])
                disp.plot()
                plt.title('Model {} on split {}'.format(model_name,split))
                plt.savefig(os.path.join('storage','Results','confusion_matrixes',model_name+' '+split))
                plt.show()
                # print(model_name,split)
                # print(conf_matrix)
# ###################################################################################################################################

        # ridge.fit(X_train,y_train)
        # svr.fit(X_train,y_train)
        # mlp.fit(X_train,y_train)
        # for i,split in enumerate(splits):
        #     print()
        #     print(split)
        #     X_eval = scaler.transform(X[i])
        #     y_eval = y[i]
        #     print(split,'{:.3f} {:.3f} '.format(np.mean(y_eval),np.std(y_eval)))

            # y_ridge = ridge.predict(X_eval)
            # y_svr = svr.predict(X_eval)
            # y_mlp = mlp.predict(X_eval)

            # # # sens_r = sensitivity(conf_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_ridge]))
            # # # sens_s = sensitivity(conf_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_svr]))
            # # # sens_m = sensitivity(conf_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_mlp]))

            # print('minimal pred values {},{},{}'.format(np.min(y_ridge),np.min(y_svr),np.min(y_mlp)))
            # # ma_r = confusion_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_ridge],labels=[1,2,3,4,5])
            # # ma_s = confusion_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_svr],labels=[1,2,3,4,5])
            # # ma_m = confusion_matrix([get_class(truth) for truth in y_eval],[get_class(pred) for pred in y_mlp],labels=[1,2,3,4,5])

            # # # for matrix, model in zip([sens_r,sens_s,sens_m],['ridge','svr','mlp']):
            # # #     print(model)
            # # #     print('sensitivity :{}'.format(matrix))

        #     ridge_err,ridge_std = l1_loss(y_ridge,y_eval, std = True)
        #     svr_err, svr_std  = l1_loss(y_svr,y_eval,std = True)
        #     mlp_err, mlp_std = l1_loss(y_mlp,y_eval,std = True)

        #     ridge_err_over,ridge_std_over = l1_loss_overestimation(y_ridge,y_eval, std = True)
        #     svr_err_over, svr_std_over  = l1_loss_overestimation(y_svr,y_eval,std = True)
        #     mlp_err_over, mlp_std_over = l1_loss_overestimation(y_mlp,y_eval,std = True)

        #     # l1_loss_bins(y_svr,y_eval,split)

        #     for i,std in enumerate([ridge_std,svr_std,mlp_std]):
        #         stds_of_splits[i].append(std)
        #     for i,std in enumerate([ridge_std_over,svr_std_over,mlp_std_over]):
        #         stds_of_splits_over[i].append(std)
        #     for i,errors in enumerate([get_l1_losses(y_ridge,y_eval),get_l1_losses(y_svr,y_eval),get_l1_losses(y_mlp,y_eval)]):
        #         for err in errors:
        #             all_errors[i].append(err)
        #     for i in range(3):
        #         all_errors_over[i] = [max(err,0) for err in all_errors[i]]

        #     #a vector that predicts the mean value of y_train, is a baseline
        #     # y_mean = np.mean(y_train)*np.ones(np.shape(y_eval))
        #     print('{}    :        ridge               svr             mlp  '.format(split))
        #     print(u"error          {:.3f} \u00B1 {:.3f},   {:.3f} \u00B1 {:.3f},  {:.3f} \u00B1 {:.3f}".format(ridge_err,ridge_std,svr_err,svr_std,mlp_err,mlp_std))
        #     print(u"error overest. {:.3f} \u00B1 {:.3f},   {:.3f} \u00B1 {:.3f},  {:.3f} \u00B1 {:.3f}".format(ridge_err_over,ridge_std_over,svr_err_over,svr_std_over,mlp_err_over,mlp_std_over))

        #     # print('Using mean of train values, has error of {} and std of {}'.format(l1_loss(y_mean,y_train,std=True)[0],l1_loss(y_mean,y_train,std=True)[1]))
        #     print()

        # print('TOTAL:                       ridge     svr      mlp  ')
        # print('mean of split stds          {:.3f},   {:.3f},  {:.3f}'.format(np.mean(stds_of_splits[0]),np.mean(stds_of_splits[1]),np.mean(stds_of_splits[2])))
        # print('std of all errors           {:.3f},   {:.3f},  {:.3f}'.format(np.std(all_errors[0]),np.std(all_errors[1]),np.std(all_errors[2])))
        # print('mean of split stds overest. {:.3f},   {:.3f},  {:.3f}'.format(np.mean(stds_of_splits_over[0]),np.mean(stds_of_splits_over[1]),np.mean(stds_of_splits_over[2])))
        # print('std of all errors overest.  {:.3f},   {:.3f},  {:.3f}'.format(np.std(all_errors_over[0]),np.std(all_errors_over[1]),np.std(all_errors_over[2])))
        # # END regression task

if __name__ == "__main__":
    #(SET all params, depending on desired train procedurey)
    main([0,1,2,3],preprocessing=False,feature_extraction=False, extract_dice_scores=False,model_train=True)

## Examples for images to show
# for dices : copies: img:  Task100_RadiopediaTrain_0009 pred:  Task740_ChallengeTrainF4'
# for int mode : not lung tissue: img : Task200_MosmedTrain_0033 pred : Task740_ChallengeTrainF4'
# for conn comp : too muh and many: img : Task100_RadiopediaTrain_0008 pred : Task541_FrankfurtTrainF4'
#                                or img : Task100_RadiopediaTrain_0007 pred : Task541_FrankfurtTrainF4'
# seg in lung

reason: import numpy,from numpy


text:
import os, os.path, re

from ..config import on_rtd

if not on_rtd:
    DATADIR = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','data'))

    # Wavelength dependence of extinction from Schlafly+ (2016)
    #  http://e.schlaf.ly/apored/extcurve.html
    from .schlafly.extcurve_s16 import extcurve

    extcurve_0 = extcurve(0.)

    #Read data defining effective central wavelengths of filters
    FILTERFILE = os.path.join(DATADIR,'filters.txt')
    LAMBDA_EFF = {}
    for line in open(FILTERFILE,'r'):
        if re.match('#', line):
            continue
        line = line.split()
        LAMBDA_EFF[line[0]] = float(line[1])

    #Read data defining extinction in different bands (relative to A_V)
    EXTINCTIONFILE = '{}/extinction.txt'.format(DATADIR)
    EXTINCTION = dict()
    EXTINCTION5 = dict()
    for line in open(EXTINCTIONFILE,'r'):
        line = line.split()
        EXTINCTION[line[0]] = float(line[1])
        EXTINCTION5[line[0]] = float(line[2])

    EXTINCTION['kep'] = 0.85946
    EXTINCTION['V'] = 1.0
    EXTINCTION['Ks'] = EXTINCTION['K']
    EXTINCTION['Kepler'] = EXTINCTION['kep']

    from astropy.coordinates import SkyCoord
    from six.moves import urllib
    import re

def get_AV_infinity(ra,dec,frame='icrs'):
    """
    Gets the A_V exctinction at infinity for a given line of sight.

    Queries the NED database.

    :param ra,dec:
        Desired coordinates, in degrees.
    :param frame: (optional)
        Frame of input coordinates (e.g., ``'icrs', 'galactic'``)
    """
    coords = SkyCoord(ra,dec,unit='deg',frame=frame).transform_to('icrs')

    rah,ram,ras = coords.ra.hms
    decd,decm,decs = coords.dec.dms
    if decd > 0:
        decsign = '%2B'
    else:
        decsign = '%2D'
    url = 'http://ned.ipac.caltech.edu/cgi-bin/nph-calc?in_csys=Equatorial&in_equinox=J2000.0&obs_epoch=2010&lon='+'%i' % rah + \
        '%3A'+'%i' % ram + '%3A' + '%05.2f' % ras + '&lat=%s' % decsign + '%i' % abs(decd) + '%3A' + '%i' % abs(decm) + '%3A' + '%05.2f' % abs(decs) + \
        '&pa=0.0&out_csys=Equatorial&out_equinox=J2000.0'

    AV = None
    for line in urllib.request.urlopen(url).readlines():
        m = re.search(b'^Landolt V \(0.54\)\s+(\d+\.\d+)', line)
        if m:
            AV = (float(m.group(1)))
            break

    if AV is None:
        raise RuntimeError('AV query fails!  URL is {}'.format(url))

    return AV

max_stars_repo_name: joelczinn/isochrones

max_issues_repo_name: joelczinn/isochrones

max_forks_repo_name: joelczinn/isochrones

reason: from astropy


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

# bootstrap
import gym_selfx.selfx

import gym
import argparse
import os
import numpy as np
import torch

from pathlib import Path
from PIL import Image
from gym import wrappers, logger

import tianshou as ts
import torchvision.transforms as T

from gym_selfx.nn.modelt import Net

logger.set_level(logger.INFO)

parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, default=10, help="number of demo examples")
parser.add_argument("-g", type=str, default='0', help="index of gpu")
parser.add_argument("-m", type=str, default='', help="model path")
opt = parser.parse_args()

cuda = True if torch.cuda.is_available() else False
if cuda:
    os.environ['CUDA_VISIBLE_DEVICES'] = opt.g

device = torch.device(int(opt.g) if torch.cuda.is_available() else "cpu")

env = gym.make('selfx-billard-v0')
outdir = 'demo/selfx-billard'
env = wrappers.Monitor(env, directory=outdir, force=True, mode='evaluation', video_callable=lambda episode_id: True)
env.seed(0)
env.reset()

resize = T.Compose([
    T.ToPILImage(),
    T.Resize((256 * 3, 512), interpolation=Image.CUBIC),
    T.ToTensor(),
])

def get_screen(env):
    screen = env.render(mode='rgb_array').transpose((2, 0, 1))
    _, screen_height, screen_width = screen.shape
    screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
    screen = torch.from_numpy(screen)
    screen = resize(screen).unsqueeze(0)
    return screen.cuda() if cuda else screen

init_screen = get_screen(env)
_, _, screen_height, screen_width = init_screen.shape
n_actions = len(env.action_space)

if opt.m == '':
    pattern = '*.chk'
else:
    pattern = opt.m

model_path = Path('results/selfx-billard')
net = Net((screen_height, screen_width), 2 * n_actions, cuda)
policy_net = ts.policy.DQNPolicy(net, None, discount_factor=0.9, estimation_step=3, target_update_freq=320)
policy_net.load_state_dict(torch.load(sorted(list(model_path.glob(pattern)))[-1], map_location=device))

def select_action(observation, reward, done):
    with torch.no_grad():
        if torch.is_tensor(observation):
            b, _, h, w = observation.size()
            r, g, b = observation[:, 0:1], observation[:, 1:2], observation[:, 2:3]
            r1, r2, r3 = r[:, :, :h // 3], r[:, :, h // 3:2 * h // 3], r[:, :, 2 * h // 3:]
            g1, g2, g3 = g[:, :, :h // 3], g[:, :, h // 3:2 * h // 3], g[:, :, 2 * h // 3:]
            b1, b2, b3 = b[:, :, :h // 3], b[:, :, h // 3:2 * h // 3], b[:, :, 2 * h // 3:]
            observation = torch.cat((r1, g1, b1, r2, g2, b2, r3, g3, b3), dim=1)
        else:
            c, h, w = observation.shape
            observation = np.reshape(observation, (1, c, h, w))

        batch = ts.data.Batch({'obs': observation, 'info': {}})
        expected_reward = policy_net(batch).logits[0].view(2, 400)[0]
        index = torch.argmax(expected_reward, dim=0).item()
        action = env.action_space[index]
        return [action]

if __name__ == '__main__':
    episode_count = opt.n

    for i in range(episode_count):
        env.reset()
        env.game.policy = select_action
        reward = 0
        done = False

        i = 0
        state = env.state()
        while True:
            i += 1

            action = env.game.act(state, reward, done)
            _, reward, done, info = env.step(action)

            state = env.state()

            if done or i > 54000:
                break
            env.render(mode='rgb_array')

    # Close the env and write monitor result info to disk
    env.close()

max_stars_repo_name: mountain/self

max_issues_repo_name: mountain/self

max_forks_repo_name: mountain/self