#-*- coding:utf-8-*-
'''
本代码是基于霍夫变换来做车道线检测的，个人感觉只针对直行道路有效。对于弯道，效果不好，其原因在于没有考虑到坐标转换下的效果。但canny概念倒是值得参考，具体可参见canny.py。
在笛卡尔坐标系中，我们可以通过绘制y对x来表示y = mx + b的直线。但是，我们也可以通过绘制b对m来将此线表示为霍夫空间中的单个点。
通常，在霍夫空间中相交的曲线越多意味着由该交点表示的线对应于更多的点。对于我们的实现，我们将在霍夫空间中定义最小阈值交叉点数以检测线。
因此，霍夫变换基本上跟踪帧中每个点的霍夫空间交叉点。如果交叉点的数量超过定义的阈值，我们将识别具有相应θ和r参数的线。
'''
import cv2
import os
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip

def do_canny(frame, kernel_size=5, low_threshold=50, high_threshold=150):
    # Converts frame to grayscale because we only need the luminance channel for detecting edges - less computationally expensive
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    # Applies a 5x5 gaussian blur with deviation of 0 to frame - not mandatory since Canny will do this for us
    blur = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
    # Applies Canny edge detector with minVal of 50 and maxVal of 150
    canny = cv2.Canny(blur, low_threshold, high_threshold)
    return canny

def do_segment(frame, trap_bottom_width, trap_top_width, trap_height):
    # Since an image is a multi-directional array containing the relative intensities of each pixel in the image, we can use frame.shape to return a tuple: [number of rows, number of columns, number of channels] of the dimensions of the frame
    # frame.shape[0] give us the number of rows of pixels the frame has. Since height begins from 0 at the top, the y-coordinate of the bottom of the frame is its height
#    height = frame.shape[0]
    # Creates a triangular polygon for the mask defined by three (x, y) coordinates
#    polygons = np.array([[(0, height), (800, height), (380, 290)]])
    polygons = np.array([[\
        ((frame.shape[1] * (1 - trap_bottom_width)) // 2, frame.shape[0]), \
        ((frame.shape[1] * (1 - trap_top_width)) // 2, frame.shape[0] - frame.shape[0] * trap_height), \
        (frame.shape[1] - (frame.shape[1] * (1 - trap_top_width)) // 2, frame.shape[0] - frame.shape[0] * trap_height), \
        (frame.shape[1] - (frame.shape[1] * (1 - trap_bottom_width)) // 2, frame.shape[0])]], dtype=np.int32)
    # Creates an image filled with zero intensities with the same dimensions as the frame
    mask = np.zeros_like(frame)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(frame.shape) > 2:
        channel_count = frame.shape[2]
        ignore_mask_color = (255, ) * channel_count
    else:
        ignore_mask_color = 255

    # Allows the mask to be filled with values of 1 and the other areas to be filled with values of 0
    cv2.fillPoly(mask, polygons, ignore_mask_color)
    # A bitwise and operation between the mask and frame keeps only the triangular area of the frame
    segment = cv2.bitwise_and(frame, mask)
    return segment

def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
	"""
	`img` is the output of the hough_lines(), An image with lines drawn on it.
	Should be a blank image (all black) with lines drawn on it.

	`initial_img` should be the image before any processing.

	The result image is computed as follows:

	initial_img * α + img * β + λ
	NOTE: initial_img and img must be the same shape!
	"""
	return cv2.addWeighted(initial_img, α, img, β, λ)

def filter_colors(image):
	"""
	Filter the image to include only yellow and white pixels
	"""
	# Filter white pixels
	white_threshold = 200 #130
	lower_white = np.array([white_threshold, white_threshold, white_threshold])
	upper_white = np.array([255, 255, 255])
	white_mask = cv2.inRange(image, lower_white, upper_white)
	white_image = cv2.bitwise_and(image, image, mask=white_mask)

	# Filter yellow pixels
	hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
	lower_yellow = np.array([90,100,100])
	upper_yellow = np.array([110,255,255])
	yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
	yellow_image = cv2.bitwise_and(image, image, mask=yellow_mask)

	# Combine the two above images
	image2 = cv2.addWeighted(white_image, 1., yellow_image, 1., 0.)

	return image2

def calculate_lines(frame, lines):
    # Empty arrays to store the coordinates of the left and right lines
    left = []
    right = []
    # Loops through every detected line
    for line in lines:
        # Reshapes line from 2D array to 1D array
        x1, y1, x2, y2 = line.reshape(4)
        # Fits a linear polynomial to the x and y coordinates and returns a vector of coefficients which describe the slope and y-intercept
        parameters = np.polyfit((x1, x2), (y1, y2), 1)
        slope = parameters[0]
        y_intercept = parameters[1]
        # If slope is negative, the line is to the left of the lane, and otherwise, the line is to the right of the lane
        if slope < 0:
            left.append((slope, y_intercept))
        else:
            right.append((slope, y_intercept))
    # Averages out all the values for left and right into a single slope and y-intercept value for each line
    left_avg = np.average(left, axis = 0)
    right_avg = np.average(right, axis = 0)
    # Calculates the x1, y1, x2, y2 coordinates for the left and right lines
    left_line = calculate_coordinates(frame, left_avg)
    right_line = calculate_coordinates(frame, right_avg)
    return np.array([left_line, right_line])

def calculate_coordinates(frame, parameters):
    slope, intercept = parameters
    # Sets initial y-coordinate as height from top down (bottom of the frame)
    y1 = frame.shape[0]
    # Sets final y-coordinate as 150 above the bottom of the frame
    y2 = int(y1 - 150)
    # Sets initial x-coordinate as (y1 - b) / m since y1 = mx1 + b
    x1 = int((y1 - intercept) / slope)
    # Sets final x-coordinate as (y2 - b) / m since y2 = mx2 + b
    x2 = int((y2 - intercept) / slope)
    return np.array([x1, y1, x2, y2])

def visualize_lines(frame, lines):
    # Creates an image filled with zero intensities with the same dimensions as the frame
    lines_visualize = np.zeros_like(frame)
    # Checks if any lines are detected
    if lines is not None:
        for x1, y1, x2, y2 in lines:
            # Draws lines between two coordinates with green color and 5 thickness
            cv2.line(lines_visualize, (x1, y1), (x2, y2), (0, 255, 0), 5)
    return lines_visualize

#trap_height height of the trapezoid expressed as percentage of image height
def draw_lines(img, lines, color=[255, 0, 0], thickness=10, trap_height=0.4):
	"""
	NOTE: this is the function you might want to use as a starting point once you want to
	average/extrapolate the line segments you detect to map out the full
	extent of the lane (going from the result shown in raw-lines-example.mp4
	to that shown in P1_example.mp4).

	Think about things like separating line segments by their
	slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left
	line vs. the right line.  Then, you can average the position of each of
	the lines and extrapolate to the top and bottom of the lane.

	This function draws `lines` with `color` and `thickness`.
	Lines are drawn on the image inplace (mutates the image).
	If you want to make the lines semi-transparent, think about combining
	this function with the weighted_img() function below
	"""
	# In case of error, don't draw the line(s)
	if lines is None:
		return
	if len(lines) == 0:
		return
	draw_right = True
	draw_left = True

	# Find slopes of all lines
	# But only care about lines where abs(slope) > slope_threshold
	slope_threshold = 0.5
	slopes = []
	new_lines = []
	for line in lines:
		x1, y1, x2, y2 = line[0]  # line = [[x1, y1, x2, y2]]

		# Calculate slope
		if x2 - x1 == 0.:  # corner case, avoiding division by 0
			slope = 999.  # practically infinite slope
		else:
			slope = (y2 - y1) / (x2 - x1)

		# Filter lines based on slope
		if abs(slope) > slope_threshold:
			slopes.append(slope)
			new_lines.append(line)

	lines = new_lines

	# Split lines into right_lines and left_lines, representing the right and left lane lines
	# Right/left lane lines must have positive/negative slope, and be on the right/left half of the image
	right_lines = []
	left_lines = []
	for i, line in enumerate(lines):
		x1, y1, x2, y2 = line[0]
		img_x_center = img.shape[1] / 2  # x coordinate of center of image
		if slopes[i] > 0 and x1 > img_x_center and x2 > img_x_center:
			right_lines.append(line)
		elif slopes[i] < 0 and x1 < img_x_center and x2 < img_x_center:
			left_lines.append(line)

	# Run linear regression to find best fit line for right and left lane lines
	# Right lane lines
	right_lines_x = []
	right_lines_y = []

	for line in right_lines:
		x1, y1, x2, y2 = line[0]

		right_lines_x.append(x1)
		right_lines_x.append(x2)

		right_lines_y.append(y1)
		right_lines_y.append(y2)

	if len(right_lines_x) > 0:
		right_m, right_b = np.polyfit(right_lines_x, right_lines_y, 1)  # y = m*x + b
	else:
		right_m, right_b = 1, 1
		draw_right = False

	# Left lane lines
	left_lines_x = []
	left_lines_y = []

	for line in left_lines:
		x1, y1, x2, y2 = line[0]

		left_lines_x.append(x1)
		left_lines_x.append(x2)

		left_lines_y.append(y1)
		left_lines_y.append(y2)

	if len(left_lines_x) > 0:
		left_m, left_b = np.polyfit(left_lines_x, left_lines_y, 1)  # y = m*x + b
	else:
		left_m, left_b = 1, 1
		draw_left = False

	# Find 2 end points for right and left lines, used for drawing the line
	# y = m*x + b --> x = (y - b)/m
	y1 = img.shape[0]
	y2 = img.shape[0] * (1 - trap_height)

	right_x1 = (y1 - right_b) / right_m
	right_x2 = (y2 - right_b) / right_m

	left_x1 = (y1 - left_b) / left_m
	left_x2 = (y2 - left_b) / left_m

	# Convert calculated end points from float to int
	y1 = int(y1)
	y2 = int(y2)
	right_x1 = int(right_x1)
	right_x2 = int(right_x2)
	left_x1 = int(left_x1)
	left_x2 = int(left_x2)

	# Draw the right and left lines on image
	if draw_right:
		cv2.line(img, (right_x1, y1), (right_x2, y2), color, thickness)
	if draw_left:
		cv2.line(img, (left_x1, y1), (left_x2, y2), color, thickness)

def annotate_image_array(image_in):
    frame = filter_colors(image_in)

    canny = do_canny(frame, kernel_size=5, low_threshold=50, high_threshold=150)
    cv2.imshow("canny", canny)

    # Region-of-interest vertices
    # We want a trapezoid shape, with bottom edge at the bottom of the image
    # trap_bottom_width = 0.85  # width of bottom edge of trapezoid, expressed as percentage of image width
    # trap_top_width = 0.07  # ditto for top edge of trapezoid
    # trap_height = 0.4  # height of the trapezoid expressed as percentage of image height
    segment = do_segment(canny, 0.85, 0.07, 0.4)

    # Hough Transform
    #rho = 2 # distance resolution in pixels of the Hough grid
    #theta = 1 * np.pi/180 # angular resolution in radians of the Hough grid
    #threshold = 15	 # minimum number of votes (intersections in Hough grid cell)
    #min_line_length = 10 #minimum number of pixels making up a line
    #max_line_gap = 20	# maximum gap in pixels between connectable line segments
    #lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=10, maxLineGap=20)
    #lines = cv2.HoughLinesP(segment, 2, np.pi / 180, 100, np.array([]), minLineLength = 100, maxLineGap = 50)
    lines = cv2.HoughLinesP(segment, 2, 1 * np.pi/180, 15, np.array([]), minLineLength=10, maxLineGap=20)
    if 1:
        line_img = np.zeros((*segment.shape, 3), dtype=np.uint8)  # 3-channel RGB image
        draw_lines(line_img, lines)

        # Draw lane lines on the original image
        initial_image = image_in.astype('uint8')
        annotated_image = weighted_img(line_img, initial_image)
    else:

        # Averages multiple detected lines from hough into one line for left border of lane and one line for right border of lane
        lines_img = calculate_lines(image_in, lines)
        # Visualizes the lines
        lines_visualize = visualize_lines(image_in, lines_img)
        cv2.imshow("hough", lines_visualize)

        # Overlays lines on frame by taking their weighted sums and adding an arbitrary scalar value of 1 as the gamma argument
        output = cv2.addWeighted(image_in, 0.9, lines_visualize, 1, 1)

    return annotated_image

if __name__ == "__main__":
    cap = cv2.VideoCapture("F:\\developing\\this_week\\lane-detection\\advanced_lane_detection-master\\challenge_video.mp4")
    while (cap.isOpened()):
        ret, frame = cap.read()
        output = annotate_image_array(frame)
        # Opens a new window and displays the output frame
        cv2.imshow("output", output)

        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


import logging, os

logger = logging.getLogger('main')
level = logging.getLevelName(os.environ.get('LOG_LEVEL', 'INFO'))
logger.setLevel(level)

# create console handler
ch = logging.StreamHandler()

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

logger.addHandler(ch)


import sqlite3, config

connection = sqlite3.connect(config.DB_FILE)

cursor = connection.cursor()

cursor.execute(
    """
    CREATE TABLE IF NOT EXISTS school (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    url TEXT NOT NULL
    )
    """
)

cursor.execute(
    """
    CREATE TABLE IF NOT EXISTS location (
    id INTEGER PRIMARY KEY,
    school_id INTEGER,
    city TEXT NOT NULL,
    zipcode TEXT NOT NULL,
    state TEXT NOT NULL,
    region TEXT NOT NULL,
    locale TEXT,
    FOREIGN KEY (school_id) REFERENCES school (id)
    )
    """
)

cursor.execute(
    """
    CREATE TABLE IF NOT EXISTS finance (
    id INTEGER PRIMARY KEY,
    school_id INTEGER,
    year DATE,
    cost4a REAL,
    in_state_tuition REAL,
    out_state_tuition REAL,
    FOREIGN KEY (school_id) REFERENCES school (id)
    )
    """
)

cursor.execute(
    """
    CREATE TABLE IF NOT EXISTS control (
    id INTEGER PRIMARY KEY,
    school_id INTEGER,
    under_investigation BOOLEAN,
    predominant_deg TEXT,
    highest_deg TEXT,
    control TEXT,
    FOREIGN KEY (school_id) REFERENCES school (id)
    )
    """
)

connection.commit()


import os.path as osp

from mmocr.datasets.builder import LOADERS, build_parser
from mmocr.utils import list_from_file, list_from_folder_table

import pickle

@LOADERS.register_module()
class Loader:
    """Load annotation from annotation file, and parse instance information to
    dict format with parser.

    Args:
        ann_file (str): Annotation file path.
        parser (dict): Dictionary to construct parser
            to parse original annotation infos.
        repeat (int): Repeated times of annotations.
    """

    def __init__(self, ann_file, parser, repeat=1, max_seq_len=40):
        assert isinstance(ann_file, str)
        assert isinstance(repeat, int)
        assert isinstance(parser, dict)
        assert repeat > 0
        assert osp.exists(ann_file), f'{ann_file} is not exist'

        self.max_seq_len = max_seq_len
        self.ori_data_infos = self._load(ann_file)
        self.parser = build_parser(parser)
        self.repeat = repeat

    def __len__(self):
        return len(self.ori_data_infos) * self.repeat

    def _load(self, ann_file):
        """Load annotation file."""
        raise NotImplementedError

    def __getitem__(self, index):
        """Retrieve anno info of one instance with dict format."""
        return self.parser.get_item(self.ori_data_infos, index)

    def __iter__(self):
        self._n = 0
        return self

    def __next__(self):
        if self._n < len(self):
            data = self[self._n]
            self._n += 1
            return data
        raise StopIteration

@LOADERS.register_module()
class HardDiskLoader(Loader):
    """Load annotation file from hard disk to RAM.

    Args:
        ann_file (str): Annotation file path.
    """

    def _load(self, ann_file):
        return list_from_file(ann_file)

@LOADERS.register_module()
class TableHardDiskLoader(Loader):
    """Load table structure recognition annotation file from hard disk to RAM.

    Args:
        ann_files_folder (str): Annotation file folder.
    """

    def _load(self, ann_files_folder):
        return list_from_folder_table(ann_files_folder, self.max_seq_len)

@LOADERS.register_module()
class LmdbLoader(Loader):
    """Load annotation file with lmdb storage backend."""

    def _load(self, ann_file):
        lmdb_anno_obj = LmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class LmdbAnnFileBackend:
    """Lmdb storage backend for annotation file.

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(
                txn.get('total_number'.encode(self.coding)).decode(
                    self.coding))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()
        with self.env.begin(write=False) as txn:
            line = txn.get(str(index).encode(self.coding)).decode(self.coding)
        return line

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class MJSTLmdbLoader(Loader):
    """Load annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = MJSTLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class MJSTLmdbAnnFileBackend:
    """Lmdb storage backend for annotation file FastOCR lmdb style.

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(txn.get(b"num-samples"))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        # MJST lmdb file is start as index 1, and offset 1 ...
        index = index + 1
        # return index as filename for lmdb image reading.
        label_key, filename = b'label-%09d' % index, r'%s' % index
        separator = ' '
        with self.env.begin(write=False) as txn:
            line = filename + separator + txn.get(label_key).decode()
        return line

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class TableMASTERLmdbLoader(Loader):
    """Load TableMASTER annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = TableMASTERLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class TableMASTERLmdbAnnFileBackend:
    """TableMASTER lmdb storage backend for annotation file FastOCR lmdb style.
        Create lmdb file by 'lmdb_maker.py'

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(pickle.loads(txn.get(b"__len__")))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        with self.env.begin(write=False) as txn:
            # data will be further parsed in parser.
            data = pickle.loads(txn.get(str.encode('{}'.format(index))))
        return data

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class MASTERLmdbLoader(Loader):
    """Load text-line MASTER annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = MASTERLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class MASTERLmdbAnnFileBackend:
    """Text-line MASTER lmdb storage backend for annotation file FastOCR lmdb style.
        Create lmdb file by 'lmdb_maker.py'

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(pickle.loads(txn.get(b"__len__")))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        with self.env.begin(write=False) as txn:
            # data will be further parsed in parser.
            data = pickle.loads(txn.get(str.encode('{}'.format(index))))
        return data

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )


import argparse
import os
import json
from src.plot_utils import plotLightCurve
from src.CNN import classification as CNN
import src.normalizing_flows as NF
import src.nf_metrics as nf_met
import src.cnn_metrics as cnn_met
import sys
import shutil
import numpy as np

def plots(nf):
    passband2name = {0: 'g', 1: 'r'}
    df = nf.df # Accessing the dataframe from NF object
    objects = nf.objects # Accessing unique objects from data
    path = "Nf_run_plots/"
    #creates Nf_run_plots and saves plots at each run
    isExist = os.path.exists(path)
    if isExist:
        shutil.rmtree(path)
    os.makedirs(path)

    for i in range(len(objects)):
        obj_name = objects[i]
        df_obj = df.loc[df['object_id'] == obj_name] # select data for object=object_name
        plotLightCurve(obj_name, df_obj, nf.flux_pred[i], nf.aug_timestamps[i], passband2name)

# Run binary classification
def run_CNN(cnn_params, report_file, nf = 0, og = 0, run_nf = 0):

    X_matrix = []
    y_vector = []
    X_test = []
    y_test = []

    if (not run_nf):

        X_file = os.path.join("data", "images.json")
        y_file = os.path.join("data", "labels.json")
        with open(X_file, 'r') as f:
            X_matrix = json.load(f)
        with open(y_file, 'r') as f:
            y_vector = json.load(f)

    else:
        if og:
            X_file = os.path.join("data", "images.json")
            y_file = os.path.join("data", "labels.json")
            with open(X_file, 'r') as f:
                X_matrix = json.load(f)
            with open(y_file, 'r') as f:
                y_vector = json.load(f)
            X_test = nf.X_matrix
            y_test = nf.y_vector
            X_test = np.array(X_test)
            y_test = np.array(y_test)
            X_test = np.array((X_test - X_test.mean()) / X_test.std(), dtype = np.float32)
        else:
            X_matrix = nf.X_matrix
            y_vector = nf.y_vector

    X_matrix = np.array(X_matrix)
    y_vector = np.array(y_vector)
    # normalize input data
    X_matrix = np.array((X_matrix - X_matrix.mean()) / X_matrix.std(), dtype = np.float32)

    print('Training CNN')
    y_test, y_test_pred = CNN(cnn_params, X_matrix, y_vector, report_file, og, X_test, y_test)

    # Report
    report = cnn_met.gen_report(y_test, y_test_pred)
    print(report)

    original_stdout = sys.stdout
    with open(report_file, 'a') as f:
        sys.stdout = f
        print(report)
        sys.stdout = original_stdout

if __name__ == '__main__':

    # Command line arguments
    parser = argparse.ArgumentParser(description='Classification of Supernovae Light Curves by NF approximation')

    # Default values of parameters are defined
    parser.add_argument('--param', default = 'param/param.json', help='file containing hyperparameters')
    parser.add_argument('-nf', '--nf', help="input number of objects generated by normalizing flows", type = int, default = 0)
    parser.add_argument('-v', '--verbose', help="increase output verbosity", action="store_true")
    parser.add_argument('-p', '--plot', help="generate plots", action="store_true" )
    parser.add_argument('-s', '--shuffle', help="Shuffles the objects generated by Normalizing Flows", action="store_true")
    parser.add_argument('-o', '--og', help="Runs CNN to classify the objects generated by Normalizing Flows", action="store_true")

    args = parser.parse_args()
    num_objects = args.nf

    verbose = args.verbose
    plot = args.plot
    shuffle = args.shuffle
    og = args.og

    # Hyperparameters from json file
    with open(args.param) as paramfile:
        param = json.load(paramfile)

    # Creates a report file
    report_file = 'report.txt'

    if os.path.exists(report_file):
        os.remove(report_file)
    f = open(report_file, 'w')
    f.close()

    cnn_params = param["CNN"]

    # Runs CNN as default
    if (num_objects == 0):
        run_CNN(cnn_params, report_file)
    else:
        # Load data
        nf_params = param["NF"]
        data_dir = 'data/ANTARES_NEW.csv'

        print('Training NF')
        # Run Normalizing Flows to obtain the approximate light curve
        nf = NF.FitNF(data_dir, shuffle, num_objects, nf_params, report_file, verbose)

        metrics = nf_met.generate_NF_report(nf.flux, nf.flux_pred_metrics, nf.flux_err, nf.flux_err_pred_metrics)
        metrics.to_csv('nfmetrics.csv')

        original_stdout = sys.stdout
        with open(report_file, 'a') as f:
            sys.stdout = f
            print(metrics)
            sys.stdout = original_stdout

        if plot:
            plots(nf)

        run_CNN(cnn_params, report_file, nf, og, run_nf = 1)


import json
from collections import OrderedDict

def buildCommand(jsonFile):
    return json.dumps(clean(json.loads(jsonFile, object_pairs_hook=OrderedDict)))

def clean(x):
    print('...',x)
    print()
    if type(x) is int or type(x) is float:
        return 0
    elif type(x) is str :
        return ""
    elif type(x) is list:
        return []
    elif type(x) is OrderedDict:
        print('this order dict')
        res = OrderedDict()
        for i, j in x.items():
            #처음 x가 들어오면, {}기준으로 짤라서 res가되어 clean함수로 재귀된다.
            res[i] = clean(j)
        return res

jsonFile= "{\"version\": \"0.1.0\",\"command\": \"c:python\",\"args\": [\"app.py\"],\"problemMatcher\": {\"fileLocation\": [\"relative\", \"${workspaceRoot}\"],\"pattern\": {\"regexp\": \"^(.*)+s$\", \"message\": 1}}}"

print(buildCommand(jsonFile))
#여기서는 dump의 개념과, loads()의 개념을 잡을 수 있다.

코드시그널/5. Python/Picturing the Parsibilities/BuildCommand.py


"""study_user

Revision ID: bb971b5db4ce
Revises: b8d7e4535317
Create Date: 2021-10-06 14:39:26.681488

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'bb971b5db4ce'
down_revision = 'b8d7e4535317'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('study_user',
    sa.Column('study_id', sa.Integer(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['study_id'], ['study.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('study_user')
    # ### end Alembic commands ###


import datetime
from app import db
from models.logActivity import LogActivity
from sqlalchemy.sql import text

ts = datetime.datetime.utcnow()

class LogActivityService(db.Model):
    __tablename__ = "log_activity"

    def add_log(user_id, activity):
        log = LogActivity(
            user_id = user_id,
            activity = activity,
            created_at = ts,
            deleted_at= None
        )
        db.session.add(log)
        db.session.commit()
        return log


# encoding=GBK
"""
    - 封装dlib的封装步骤

    - 关于xml文件
        1. imglab.exe -c xxx.xml img_path 生成xml文件
        2. imglab.exe xxx.xml 进行标注
            - 按住shift选定区域
            - 注意在上方的格子里添加label
        3. 完成后file-save
    - 关于svm文件
        1. 通过dlib的训练得到，相当于模型，用于预测
"""

import dlib

class LocateTrain:
    def __init__(self,
                 detect_xml_path: str,
                 output_svm_name: str,
                 open_flips: bool = True,
                 c_count: int = 5,
                 thread_number: int = 8,
                 verbose: bool = True):
        # 设置训练参数和模式
        self.options = dlib.simple_object_detector_training_options()
        # 启用左右翻转
        self.options.add_left_right_image_flips = open_flips
        # 向量机数量
        self.options.C = c_count
        # 线程数量
        self.options.num_threads = thread_number
        # 启动随机森林
        self.options.be_verbose = verbose
        # 启动训练
        output_svm_name = "../../../Resource/svm/trained/" + output_svm_name + ".svm"
        dlib.train_simple_object_detector(detect_xml_path, output_svm_name, self.options)

if __name__ == '__main__':
    # l = LocateTrain("../../../Resource/svm/label/bottle.xml", "bottle_svm", c_count=100)
    from utils.LocateObject.dlibLocate import LocateRoI
    import cv2 as cv
    svm_path = "../../../Resource/svm/trained/bottle_svm.svm"
    # img_path = "H:/pic/liquid/11.png"
    img_path = "F:/DataSet/bottle/Locate/pic/11.png"
    img = cv.imread(img_path)
    L = LocateRoI(svm_path)
    predict_list = L.predict(img)
    for start, end in predict_list:
        cv.rectangle(img, start, end, (0, 255, 255), 2)
    cv.imshow("img", img)
    cv.waitKey(0)


import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
	"""a clss to manage the bullets fired from the shjip"""
	def __init__(self, ai_settings, screen, ship):
		"""make a bullet obj at the ships current position"""
		super().__init__()
		self.screen = screen

		#init bullet rect at (0, 0) and set the correct position.
		#we're drawing it from scratch. we init the rects top left corner to 0,0
		#after init, we update the bullets centerx to be the same as the ships centerx,
		#and the bullets top to the sjhips top to make it look like the bullet
		#emerges from the ship
		self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
		self.rect.centerx = ship.rect.centerx
		self.rect.top = ship.rect.top

		#store the bullets position as a decimal value
		#this is the y value as it moves up the screen
		self.y = float(self.rect.y)

		self.color = ai_settings.bullet_color
		self.speed_factor = ai_settings.bullet_speed_factor

	def update(self):
		"""move the bullet up the screen"""
		#this function is called automatically by the pygame.sprite group object

		#update the y value of the bullet
		#the bullet travels up so we subtract y (pygame inits the upper left corner
		#of the window as 0,0 and the bottom right as positive integers for maxX, maxY.
		# maxX and maxY are whtever size of the screen you config'd) so upward
		#movement means we subtract. this will be in the main loop so it will
		#run a shitload of tiems and move the bullet up. no x coord is needed
		#cuz the bullet travels in a straight line
		self.y -= self.speed_factor

		#update the rect position
		self.rect.y = self.y

	def draw_bullet(self):
		"""draw bullet on the screen"""
		pygame.draw.rect(self.screen, self.color, self.rect)


from imageai.Detection import VideoObjectDetection
import os
import cv2
import ledControl
import time

execution_path = os.getcwd()
detector = VideoObjectDetection()
ledController = ledControl.LedController()
#Time since the last time an boject was recognised
lastReco=0
print('sudo python3 '+os.getcwd()+'/motorManager.py --right')

detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo-tiny.h5"))
detector.loadModel(detection_speed="fast")

camera = cv2.VideoCapture(0)

def forFrame(frame_number, output_array, output_count):
    global lastReco
    #Wait 5sec between recognition, to avoid recognizing many time the same object
    if(time.time() - lastReco > 5):
        print("FOR FRAME " , frame_number)
        print("Output for each object : ", output_array)
        for el in output_array:
            if(el['name'] == 'orange'  or  el['name'] == 'apple'):
                print('fruit  detected')
                time.sleep(1)
                ledController.endLed()
                #To control the motor we use the PWM mode of the pin, to do that we use the wiringpi library, to work it have to ba call using sudo, or due to some issue with numpy
                #the ImagaAI library can't be call using sudo, so we call the ImageAI library as an user and when we need the motor we use the os.system method to call the
                #motorManager script with sudo via the shell
                os.system('sudo python3 '+os.getcwd()+'/motorManager.py --left')
                lastReco = time.time()
            elif(el['name']== 'cup'):
                print('cup detected')
                time.sleep(1)
                ledController.endLed()
                os.system('sudo python3 '+os.getcwd()+'/motorManager.py --right')
                lastReco = time.time()
            elif(el['name'] == 'spoon'):
                print('spoon detected')
                time.sleep(1)
                ledController.endLed()
                os.system('sudo python3 '+os.getcwd()+'/motorManager.py --left')
                lastReco = time.time()
            elif(el['name'] == 'bottle'):
                print('bottle detected')
                ledController.endLed()
                os.system('sudo python3 '+os.getcwd()+'/motorManager.py --left')
                lastReco = time.time()
            else:
                print('Other object')
                time.sleep(1)
                ledController.endLed()
                os.system('sudo python3 '+os.getcwd()+'/motorManager.py --left')
                lastReco = time.time()
                print(el)

detector.detectObjectsFromVideo(save_detected_video=False, camera_input=camera, frames_per_second=1,frame_detection_interval=5, log_progress=True, minimum_percentage_probability=30, per_frame_function= forFrame)


#!/usr/bin/env python
import logging
import sys

import ldclient
import pandas as pd
from collections import OrderedDict
import uuid

#root = logging.getLogger()
#root.setLevel(logging.INFO)
#ch = logging.StreamHandler(sys.stdout)
#ch.setLevel(logging.INFO)
#formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#ch.setFormatter(formatter)
#root.addHandler(ch)

if __name__ == "__main__":
  ldclient.set_sdk_key("SDK KEY_Emily's Test Project - Test")

  # load dictionaries with name/sign data
  firstNameMap = { 'a': 'Amethyst', 'b': 'Bold', 'c': 'Curvy', 'd': 'Daring', 'e': 'Electric', 'f': 'Fatal', 'g': 'Grace', 'h': 'Horrendous', 'i': 'Ign', 'j': 'Jolly', 'k': 'Killer', 'l': 'Loud', 'm': 'Magnificent', 'n': 'Nasty', 'o': 'Orange', 'p': 'Plain', 'q': 'Queen', 'r': 'Righteous', 's': 'Saucy', 't': 'Terrifying', 'u': 'Unicorn', 'v': 'Vivacious', 'w': 'Wild', 'x': 'Xanthic', 'y': 'Yummy', 'z': 'Zesty'}
  lastNameMap = {'aries': 'Impulse', 'taurus': 'Bull', 'gemini': 'Twoface', 'cancer': 'Moody','leo': 'Star', 'virgo': 'Flawless', 'libra': 'Flake', 'scorpio': 'Intense','sagittarius': 'Airborne', 'capricorn': 'ambition', 'aquarius': 'Ice','pisces': 'Spacey'}

  # start generator
  playagain = "yes"
  while playagain.lower() == "yes":
    print ("Welcome to the Drag Name Generator!")
    # get user info
    firstname = input("Please enter your first name: ")
    while not firstname.isalpha():
        print ("You typed something wrong!")
        firstname = input("Please enter your first name: ")
    firstnameletter = firstname[:1]
    dragFirstName = firstNameMap[firstnameletter.lower()]

    sign = input("Please enter your astrological sign: ")
    while sign.lower() not in lastNameMap:
        print ("You typed something wrong!")
        sign = input("Please enter your astrological sign: ")
    dragLastName = lastNameMap[sign.lower()]

    # set user info
    user = {
      "key": uuid.uuid4(),
      "firstName": firstname,
      "custom": {
        "astroSign": sign
      }
    }

    # evaluate feature flag
    disable_lastname = ldclient.get().variation("dg-first-name-only", user, False)

    if disable_lastname:
      print("Your Drag First Name is: ", dragFirstName)
    else:
      print("Your Drag Name is: ", dragFirstName," ", dragLastName)

    # check if they want to play again
    playagain = input("Would you like to generate another drag name? Enter ""Yes"" or ""No"": ")
    while playagain.lower() != "yes" and playagain.lower() != "no":
        print ("You typed something wrong!")
        playagain = input("Would you like to generate another drag name? Enter ""Yes"" or ""No"": ")

  ldclient.get().close() # close the client before exiting the program - ensures that all events are delivered


import time

import pdfkit
import requests
from PyPDF2 import PdfFileMerger
from bs4 import BeautifulSoup

def get_chapter(bookname,url):
    i = 0
    pdfs = []
    html_template = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
        {content}
        </body>
        </html>
        """
    soup = BeautifulSoup(requests.get(url).text, 'html.parser')

    for cursor in soup.find_all( class_='chapter' ):
        i = i+1
        print(cursor.text)
        pdf_name = 'docker'+ str(i) + '.pdf'
        html_name = 'docker'+ str(i) + '.html'
        link = url+cursor.a.get('href')
        time.sleep(2);
        html = get_chapter_text(link)
        html = html_template.format(content = html)
        html = html.encode("utf-8")
        with open( html_name, 'wb') as f:
            f.write(html)
        save_pdf(html_name,pdf_name )
        pdfs.append(pdf_name)

    merge_pdf(bookname, pdfs)

def merge_pdf(bookname,pdfs):
    merger = PdfFileMerger()
    i = 0
    for pdf in pdfs:
        merger.append(open(pdf, 'rb'), import_bookmarks=False)
        print( "Merging " + str(i) + ' files:' + pdf)

        i = i + 1
    output = open(bookname+".pdf", "wb")
    merger.write(output)
    print("Finished!")

def get_chapter_text(url):
    current_soup = BeautifulSoup(requests.get(url).text, 'html.parser')
    content = current_soup.find(class_='search-noresults')
    return(content)

def save_pdf(htmls, file_name):
  """
  save all .html to .pdf
  :param htmls: name of .html files
  :param file_name: name of .pdf files
  """
  options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header': [
      ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
      ('cookie-name1', 'cookie-value1'),
      ('cookie-name2', 'cookie-value2'),
    ],
    'outline-depth': 10,
  }
  pdfkit.from_file(htmls, file_name, options=options)

if __name__ == '__main__':
    url = 'https://yeasy.gitbooks.io/docker_practice/content/'
    get_chapter("Docker_all",url)


import torch
from torch import nn
from torch import optim
from torch.nn import functional as F

from .deeplab import DeepLab

import lightning.pytorch as pl
from torchmetrics import JaccardIndex

class FocalLoss(nn.Module):
    def __init__(self, alpha=1, gamma=0, ignore_index=255):
        super(FocalLoss, self).__init__()
        self.alpha = alpha
        self.gamma = gamma
        self.ignore_index = ignore_index

    def forward(self, inputs, targets):
        CE_loss = F.cross_entropy(
            inputs, targets, reduction='none', ignore_index=self.ignore_index)
        pt = torch.exp(-CE_loss)
        focal_loss = self.alpha * (1-pt) ** self.gamma * CE_loss
        return focal_loss.mean()

class ModelWrapper(pl.LightningModule):
    def __init__(
        self,
        num_classes,
        epochs: int=1000,
        lr_start=1e-3,
        lr_final=1e-5,
        wd=1e-4,
        alpha=1,
        gamma=0,
        backbone='resnet101',
        output_stride=16):

        super(ModelWrapper, self).__init__()
        self.model = DeepLab(num_classes, backbone=backbone, output_stride=output_stride)
        self.num_classes = num_classes
        self.criterion = FocalLoss(alpha, gamma, ignore_index=255)
        self.train_metric = JaccardIndex(task = 'multiclass', num_classes=num_classes, ignore_index=255, average='macro')
        self.val_metric = JaccardIndex(task = 'multiclass', num_classes=num_classes, ignore_index=255, average='macro')

        self.best_train_pixel_acc = 0
        self.best_val_pixel_acc = 0
        self.best_train_miou = 0
        self.best_val_miou = 0

        self.save_hyperparameters()

    def training_step(self, batch, batch_idx):
        image, label = batch
        out_img = self.model(image)
        loss = self.criterion(out_img, label)
        pred = out_img.max(1)[1]
        pixel_acc = torch.sum(pred == label) / label.numel()
        self.best_train_pixel_acc = max(pixel_acc, self.best_train_pixel_acc)
        self.log("train/loss", loss, sync_dist=True)
        self.log("train/pixel_acc", pixel_acc, sync_dist=True)
        self.log("train/best_pixel_acc", self.best_train_pixel_acc, sync_dist=True)
        self.train_metric.update(pred, label)
        return loss

    def on_train_epoch_end(self):
        # log epoch metric
        miou = self.train_metric.compute()
        self.best_train_miou = max(miou, self.best_train_miou)
        self.log('train/mIoU', miou, sync_dist=True)
        self.log('train/best_mIoU', self.best_train_miou, sync_dist=True)
        self.train_metric.reset()

    def validation_step(self, batch, batch_idx):
        image, label = batch
        out_img = self.model(image)
        loss = self.criterion(out_img, label)
        pred = out_img.max(1)[1]
        pixel_acc = torch.sum(pred == label) / label.numel()
        self.best_val_pixel_acc = max(pixel_acc, self.best_val_pixel_acc)
        self.log("val/loss", loss, sync_dist=True)
        self.log("val/pixel_acc", pixel_acc, sync_dist=True)
        self.log("val/best_pixel_acc", self.best_val_pixel_acc, sync_dist=True)
        self.val_metric.update(pred, label)

    def on_validation_epoch_end(self):
        # log epoch metric
        miou = self.val_metric.compute()
        self.best_val_miou = max(miou, self.best_val_miou)
        self.log('val/mIoU', miou, sync_dist=True)
        self.log('val/best_mIoU', self.best_val_miou, sync_dist=True)
        self.val_metric.reset()

    def test_step(self, batch, batch_idx):
        image, label = batch
        out_img = self.model(image)
        loss = self.criterion(out_img, label)
        pred = out_img.max(1)[1]
        pixel_acc = torch.sum(pred == label) / label.numel()
        self.log("pixel_acc", pixel_acc, sync_dist=True)
        self.val_metric.update(pred, label)

    def on_test_epoch_end(self):
        miou = self.val_metric.compute()
        self.log('mIoU', miou, sync_dist=True)
        self.val_metric.reset()

    def forward(self, batch):
        image, _ = batch
        out_img = self.model(image)
        pred = out_img.max(1)[1]
        return pred

    def configure_optimizers(self):
        optimizer = optim.AdamW(
            params=[
                {'params': self.model.backbone.parameters(), 'lr': 0.1 * self.hparams.lr_start},
                {'params': self.model.ASPP.parameters(), 'lr': self.hparams.lr_start},
                {'params': self.model.decoder.parameters(), 'lr': self.hparams.lr_start},
            ], lr=self.hparams.lr_start, weight_decay=self.hparams.wd)
        scheduler = optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=(self.hparams.lr_final / self.hparams.lr_start) ** (1 / self.hparams.epochs))
        return [optimizer], [scheduler]


import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets

def detect_green_triangle(image, threshold_low, threshold_high):
    # Convert image to HSV color space
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # Define lower and upper green color thresholds
    lower_green = np.array([threshold_low, 50, 50])
    upper_green = np.array([threshold_high, 255, 255])

    # Create a binary mask of green pixels
    mask = cv2.inRange(hsv, lower_green, upper_green)

    # Apply morphological operations to enhance the mask
    kernel = np.ones((5, 5), np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

    # Find contours in the mask
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Iterate over contours to find the green triangle
    triangle_contour = None
    for cont in contours:
        perimeter = cv2.arcLength(cont, True)
        approx = cv2.approxPolyDP(cont, 0.03 * perimeter, True)
        area = cv2.contourArea(cont)
        if len(approx) == 3 and 300 < area < 2000:
            triangle_contour = approx
            break

    return triangle_contour

def update_threshold_low(val):
    global threshold_low
    threshold_low = val

def update_threshold_high(val):
    global threshold_high
    threshold_high = val

def on_button_press(event):
    # Check if the left mouse button was clicked
    if event.button == 1:
        # Get the current image coordinates
        x, y = int(event.xdata), int(event.ydata)

        # Check if the click is within the image boundaries
        if 0 <= x < image.shape[1] and 0 <= y < image.shape[0]:
            # Display the coordinates
            print("Clicked image coordinates: ({}, {})".format(x, y))

# Load the image
image = cv2.imread(r'C:\Users\rasmu\OneDrive\Skrivebord\CDIO\RobotProject\CDIO-RobtoScripts\ArucoTet\Chessimages\4.jpg')

# Create a figure and axes for plotting
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)

# Create sliders for threshold values
ax_threshold_low = plt.axes([0.25, 0.1, 0.5, 0.03])
slider_threshold_low = widgets.Slider(ax_threshold_low, 'Threshold Low', 0, 255, valinit=100)
slider_threshold_low.on_changed(update_threshold_low)

ax_threshold_high = plt.axes([0.25, 0.05, 0.5, 0.03])
slider_threshold_high = widgets.Slider(ax_threshold_high, 'Threshold High', 0, 255, valinit=200)
slider_threshold_high.on_changed(update_threshold_high)

# Detect the green triangle with initial threshold values
threshold_low = slider_threshold_low.val
threshold_high = slider_threshold_high.val
triangle_contour = detect_green_triangle(image, threshold_low, threshold_high)

# Plot the image with detected triangle
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Green Triangle Detection')
plt.axis('off')

# Connect mouse click event handler
cid = fig.canvas.mpl_connect('button_press_event', on_button_press)

# Display the plot
plt.show()

# Disconnect mouse click event handler
fig.canvas.mpl_disconnect(cid)


#-*-coding:utf-8-*-
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
from sklearn.model_selection import train_test_split,KFold
import datetime

tt = pd.read_csv('final_train.csv')
area = tt['area_id']
#tt['answer'] = tt['answer'] -1
answer=tt[['answer']]

del tt['answer']
del tt['area_id']
#tt.replace(np.inf,np.nan,inplace=True)
#tt.replace(-np.inf,np.nan,inplace=True)
tt.replace(np.inf,100000,inplace=True)
tt.replace(-np.inf,-100000,inplace=True)
tt.fillna(tt.mean(),inplace=True)

x_train, x_test, y_train, y_test=train_test_split(tt, answer, test_size=0.1, random_state=8)
features1 = x_train.values
features2 = x_test.values

answer1 = y_train.answer.values
answer2 = y_test.answer.values

rf = RandomForestClassifier(n_estimators=100,bootstrap = True, oob_score = True, criterion = 'gini',
                            n_jobs=-1,random_state=8)

start = datetime.datetime.now()
rf.fit(features1,answer1)
end = datetime.datetime.now()
print('user %d seconds'%(end - start).seconds)
print('oob_score:\t',rf.oob_score_)
print ("Accuracy:\t", (answer2 == rf.predict(features2)).mean())
print ("Total Correct:\t", (answer2 == rf.predict(features2)).sum())


import numpy as np
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import transforms, datasets,models
from collections import OrderedDict
from PIL import Image
def pretrained_model_info(pretrained_name):

    #upload the pretrained model
    x="models."+pretrained_name+"(pretrained=True)"
    #exec("model="+x)
    model = eval(x)
    #print(model)
    #Get keys and values of the model
    list_keys=[]
    list_value=[]
    for key in model.state_dict():
        value = model.state_dict().get(key)
        list_keys.append(key)
        list_value.append(value)

    #Get the last key of the model by eliminating chars after "."
    last_layer_key  = list_keys[-1].split('.')[0]

    #Get size of the input to the classification layer
    for key in list_keys:
        if(key.split('.')[0]==last_layer_key):
            break

    x= model.state_dict().get(key).size()

    #return name of the classifier layer and the size of its input
    return model,last_layer_key,int(x[1])

def building_model(hiden_units,pretrained_name,learning_rate):

    model,key,size = pretrained_model_info(pretrained_name)

    #Defining the classifier
    classifier =  nn.Sequential(OrderedDict([
                          ('fc1', nn.Linear(size, hiden_units)),
                          ('c_relu1', nn.ReLU()),
                          ('c_drop1', nn.Dropout()),
                          ('fc3', nn.Linear(hiden_units, 102)),
                          ('output', nn.LogSoftmax(dim=1))
                          ]))

    #fixing the gradient
    for param in model.parameters():
        param.requires_grad = False

    #replacing the classifier
    x="model."+key+"=classifier"
    exec(x)
    x= "optim.Adam(model."+key+".parameters(), learning_rate)"
    optimizer = eval(x)
    return model,optimizer

def data_preparation(data_dir,train_batch=32,valid_batch=16,test_batch=16):

    train_dir = data_dir + '/train'
    valid_dir = data_dir + '/valid'
    test_dir = data_dir + '/test'

    # Define your transforms for the training, validation, and testing sets
    train_transforms = transforms.Compose([transforms.RandomRotation(30),
                                       transforms.RandomResizedCrop(224),
                                       transforms.RandomHorizontalFlip(),
                                       transforms.ToTensor(),
                                       transforms.Normalize([0.485, 0.456, 0.406],
                                                            [0.229, 0.224, 0.225])])

    test_valid_transforms = transforms.Compose([transforms.Resize(256),
                                      transforms.CenterCrop(224),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406],
                                                           [0.229, 0.224, 0.225])])
    # Load the datasets with ImageFolder
    train_datasets = datasets.ImageFolder(root=train_dir,transform=train_transforms)
    valid_datasets = datasets.ImageFolder(root=valid_dir,transform=test_valid_transforms)
    test_datasets = datasets.ImageFolder(root=test_dir,transform=test_valid_transforms)

    # Using the image datasets and the trainforms, define the dataloaders
    trainloader = DataLoader(train_datasets, batch_size=train_batch, shuffle=True)
    validloader = DataLoader(valid_datasets, batch_size=valid_batch)
    testloader = DataLoader(test_datasets, batch_size=test_batch)

    return train_datasets,trainloader,validloader,testloader

criterion = nn.NLLLoss()

def train(model,train_datasets,trainloader,validloader,testloader,optimizer,device,epochs,hiden_units,pretrained_name,\
          learning_rate,file_path):

    #optimizer = optim.Adam(model.fc.parameters(), learning_rate)
    epochs = epochs
    model.train(True)
    # change to device
    model.to(device)
    print_every = 1
    for e in range(epochs):
        steps = 0
        running_loss = 0
        for ii, (inputs, labels) in enumerate(trainloader):
            steps += 1

            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            # Forward and backward passes
            outputs = model.forward(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if steps % print_every == 0:
                valid_loss,valid_accuracy = valid(model,validloader,device)
                print("Epoch: {}/{}... ".format(e+1, epochs),
                     "Loss: {:.4f}".format(running_loss/print_every))
                running_loss = 0
                print("validation_loss: {:.4f}".format(valid_loss),'validation_accuracy: %d %%' % valid_accuracy)

    save_checkpoint(model,epochs,train_datasets,hiden_units,pretrained_name,learning_rate,file_path)

def valid(model,validloader,device):
    correct = 0
    total = 0
    with torch.no_grad():
        model.train(False)
        model.to(device)
        running_loss = 0
        step=0
        for ii,(inputs,labels) in enumerate(validloader):
            step+=1

            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model.forward(inputs)
            loss = criterion(outputs, labels)
            running_loss += loss.item()

            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    #print("Validation_Loss: {:.4f}".format(running_loss/step))
    return running_loss/step,(100 * (correct / total))

def test(model,testloader,device):
    correct = 0
    total = 0
    with torch.no_grad():
        model.eval()
        model.to(device)
        for ii,(inputs,labels) in enumerate(testloader):
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model(inputs)

            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    return   (100 * correct / total)

def save_checkpoint(model,epoch,train_datasets,hiden_units,pretrained_name,learning_rate,filename='new.pkl'):
    state = {
    'hiden_units': hiden_units,
    'pretrained_name': pretrained_name,
    'learning_rate': learning_rate,
    'epoch': epoch,
    'state_dict': model.state_dict(),
    'class_to_idx': train_datasets.class_to_idx
            }
    torch.save(state, filename)

def load_checkpoint(filename='new.pkl'):

    saved = torch.load(filename,lambda storage, loc: storage)

    model,optimizer = building_model(saved['hiden_units'],saved['pretrained_name'],saved['learning_rate'])

    model.load_state_dict(saved['state_dict'])

    return model,saved['class_to_idx']

def process_image(image):
    ''' Scales, crops, and normalizes a PIL image for a PyTorch model,
        returns an Numpy array
    '''
    side = 256
    w,h = image.size
    max_side = max(w,h)
    #plt.imshow(image)

    if(max_side==w):
        ratio = w/h
        x=[int(256*ratio),256]
    else:
        ratio=h/w
        x=[256,int(256*ratio)]

    image = image.resize(x)
    width,height = image.size

    left = (width - 224)/2
    top = (height - 224)/2
    right = (width + 224)/2
    bottom = (height + 224)/2

    image = image.crop((left, top, right, bottom))

    np_image = np.array(image)

    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    np_image = np_image/255
    np_image = (np_image-mean)/std
    np_image = np_image.transpose((2,0,1))

    return np_image

def predict(image_path, model, device,class_to_idx,topk):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    with torch.no_grad():
        im = Image.open(image_path)
        input_im = process_image(im)
        model.to(device)
        model.eval()
        input=(torch.from_numpy(input_im)).unsqueeze_(0).float()
        input = input.to(device)

        output=model(input)
        topk, indices = output.topk(topk)

        topk = np.exp(topk.data.cpu().numpy()[0])
        indices = indices.data.cpu().numpy()[0]

        inv_map = {v: k for k, v in class_to_idx.items()}
        classes = [inv_map[x] for x in indices]

        return topk,classes


import os
import shutil
from datetime import datetime
import numpy as np

class MiniRNN:
    def __init__(
        self,
        n_hidden_neurons: int,
        train_data: str,
        seq_length=25,
        model_name="miniRNN",
    ) -> None:
        """Initialize Mini RNN for character level generation

        Args:
            n_hidden_neurons (int): Number of Neurons in the hidden layer
            vocab_size (int): Number of unique characters for the RNN to predict
            seq_length (int): number of steps to unroll the RNN for.
                Defaults to 25.
            model_name (str): Model Name. Defaults to miniRNN
        """
        self.data = train_data
        chars = list(set(train_data))
        self.data_size, self.vocab_size = len(train_data), len(chars)

        self.char_to_ix = {ch: i for i, ch in enumerate(chars)}
        self.ix_to_char = {i: ch for i, ch in enumerate(chars)}

        self.seq_length = seq_length
        self.n_hidden_neurons = n_hidden_neurons
        self.model_name = model_name

        # Initialize weights
        self.model_weights = {
            # input -> hidden_state
            "xh": np.random.randn(n_hidden_neurons, self.vocab_size) * 0.01,
            # prev_hidden_state -> next_hidden_state
            "hh": np.random.randn(n_hidden_neurons, n_hidden_neurons) * 0.01,
            # hidden_state -> output
            "hy": np.random.randn(self.vocab_size, n_hidden_neurons) * 0.01,
        }

        # Initialize Biases
        self.model_biases = {
            # input to hidden
            "xh": np.zeros((n_hidden_neurons, 1)),
            # hidden to output
            "hy": np.zeros((self.vocab_size, 1)),
        }

    def calc_gradients(self, inputs: list, targets: list, h_prev):
        """_summary_

        Args:
            inputs (list): _description_
            targets (list): _description_
            h_prev (_type_): _description_
        """
        inputs_x, hidden_states_h, preds_y, probabs_p = {}, {}, {}, {}

        # Last hidden state vars is hprev
        hidden_states_h[-1] = np.copy(h_prev)
        loss = 0

        # forward pass
        for t in range(len(inputs)):
            # one-hot encoding of input
            # create a list of zeros of size: vocab_size
            inputs_x[t] = np.zeros((self.vocab_size, 1))
            # set the index of the char to 1
            inputs_x[t][inputs[t]] = 1

            # calculate the hidden state
            hidden_states_h[t] = np.tanh(
                np.dot(self.model_weights["xh"], inputs_x[t])
                + np.dot(self.model_weights["hh"], hidden_states_h[t - 1])
                + self.model_biases["xh"]
            )

            # calculate the next output
            # unnormalized log probabilities for next chars
            preds_y[t] = (
                np.dot(self.model_weights["hy"], hidden_states_h[t])
                + self.model_biases["hy"]
            )

            # normalized probabilities for next chars
            probabs_p[t] = np.exp(preds_y[t]) / np.sum(np.exp(preds_y[t]))

            # Calculate loss: softmax (cross-entropy loss)
            loss += -np.log(probabs_p[t][targets[t], 0])

        # backward pass: compute gradients going backwards
        # set gradient matrices to be the same size as weights
        dWxh, dWhh, dWhy = (
            np.zeros_like(self.model_weights["xh"]),
            np.zeros_like(self.model_weights["hh"]),
            np.zeros_like(self.model_weights["hy"]),
        )
        # set bias matrices
        dbh, dby = (
            np.zeros_like(self.model_biases["xh"]),
            np.zeros_like(self.model_biases["hy"]),
        )
        # set a dummy dh_next var
        dhnext = np.zeros_like(hidden_states_h[0])

        # Go back every step and calculate grads
        for t in reversed(range(len(inputs))):
            # grad of y
            dy = np.copy(probabs_p[t])
            # backprop into y. see http://cs231n.github.io/neural-networks-case-study/#grad if confused here
            dy[targets[t]] -= 1
            dWhy = np.dot(dy, hidden_states_h[t].T)
            dby += dy
            dh = np.dot(self.model_weights["hy"].T, dy) + dhnext
            dhraw = (
                1 - hidden_states_h[t] * hidden_states_h[t]
            ) * dh  # grad of tanh
            dbh += dhraw
            dWxh += np.dot(dhraw, inputs_x[t].T)
            dWhh += np.dot(dhraw, hidden_states_h[t - 1].T)
            dhnext = np.dot(self.model_weights["hh"].T, dhraw)

        # Clip gradients to mitigate exploding grad problem
        for dparam in [dWxh, dWhh, dWhy, dbh, dby]:
            np.clip(dparam, -5, 5, out=dparam)

        return (
            loss,
            dWxh,
            dWhh,
            dWhy,
            dbh,
            dby,
            hidden_states_h[len(inputs) - 1],
        )

    def sample(self, h: np.array, seed_ix: int, n: int) -> list:
        """_summary_

        Args:
            h (np.array): _description_
            seed_ix (int): _description_
            n (int): _description_

        Returns:
            list: _description_
        """
        x = np.zeros((self.vocab_size, 1))
        x[seed_ix] = 1

        ixes = []
        for _ in range(n):
            # calc hidden state based on x, ws & bh, and prev state 'h'
            h = np.tanh(
                np.dot(self.model_weights["xh"], x)
                + np.dot(self.model_weights["hh"], h)
                + self.model_biases["xh"]
            )

            # calc predictions through the updated hidden state
            y = np.dot(self.model_weights["hy"], h) + self.model_biases["hy"]

            # normalize probabilites
            p = np.exp(y) / np.sum(np.exp(y))

            # TODO: Understand this piece of code.
            ix = np.random.choice(range(self.vocab_size), p=p.ravel())
            x = np.zeros((self.vocab_size, 1))
            x[ix] = 1
            ixes.append(ix)
        return ixes

    def save_model_params(self, model_dir: str, model_iter: int) -> None:
        """Save Model Weights"""

        # Make Dir for the model iteration
        os.makedirs(f"{model_dir}/{model_iter}")

        # Save Model Weights
        for param_name, param in self.model_weights.items():
            with open(
                f"{model_dir}/{model_iter}/weights_{param_name}.npy", "wb"
            ) as f:
                np.save(f, param)

        # Save Model Biases
        for param_name, param in self.model_biases.items():
            with open(
                f"{model_dir}/{model_iter}/biases_{param_name}.npy", "wb"
            ) as f:
                np.save(f, param)

    def load_model_params(self, model_dir=str, model_ckpt="latest") -> None:
        if not os.path.isdir(model_dir):
            print("Model Path doesn't exist")

        model_ckpts = next(os.walk(model_dir))[1]
        if model_ckpt == "latest":
            if "final" in model_ckpts:
                model_ckpt = "final"
            else:
                model_ckpt = max([int(x) for x in model_ckpts])
        else:
            if str(model_ckpt) not in model_ckpts:
                print("Model checkpoint not found!")
                return None

        # Load Weights
        for w_ in list(self.model_weights.keys()):
            with open(f"{model_dir}/{model_ckpt}/weights_{w_}.npy", "rb") as f:
                self.model_weights[w_] = np.load(f)

        # Load Biases
        for b_ in list(self.model_biases.keys()):
            with open(f"{model_dir}/{model_ckpt}/biases_{b_}.npy", "rb") as f:
                self.model_biases[b_] = np.load(f)

    def train(
        self,
        num_epochs: int,
        learning_rate=1e-1,
        checkpoint_range=1000,
        verbose=False,
        save_model=True,
    ) -> None:
        """Train Model

        Args:
            data (str): Data to train upon
            num_epochs (int): Number of epochs to train on
            learning_rate (float): Learning Rate of the model. Defaults to 1e-1.
            checkpoint_range (int): Checkpoint range. Defaults to 100.
            verbose (bool): Whether to print predictions at every checkpoint. Defaults to False.
            save_model_weights (bool): Whether to save model weights. Defaults to True.
        """

        print(f"Data size: {self.data_size}; Vocab Size: {self.vocab_size}")

        # Ensure checkpoints directory exists
        curr_time = datetime.now().strftime("%m_%d_%Y_T_%H_%M_%S")
        if self.model_name == "miniRNN":
            self.model_name += f"_{curr_time}"
        model_dir = f"saved_models/{self.model_name}"

        if os.path.isdir(model_dir):
            # to clear the existing
            shutil.rmtree(model_dir)
        os.makedirs(model_dir)

        p = 0
        # declare memory variables for Adagrad
        mWxh, mWhh, mWhy = (
            np.zeros_like(self.model_weights["xh"]),
            np.zeros_like(self.model_weights["hh"]),
            np.zeros_like(self.model_weights["hy"]),
        )
        mbh, mby = (
            np.zeros_like(self.model_biases["xh"]),
            np.zeros_like(self.model_biases["hy"]),
        )

        # loss at iteration 0
        smooth_loss = -np.log(1.0 / self.vocab_size) * self.seq_length

        print(f"Starting Training. Save Model: {save_model}, ")
        try:
            for epoch in range(num_epochs):
                # prepare inputs (we're sweeping from left to right in steps seq_length long)
                if p + self.seq_length + 1 >= len(self.data) or epoch == 0:
                    hprev = np.zeros(
                        (self.n_hidden_neurons, 1)
                    )  # reset RNN memory
                    p = 0  # go from start of self.data
                inputs = [
                    self.char_to_ix[ch]
                    for ch in self.data[p : p + self.seq_length]
                ]
                targets = [
                    self.char_to_ix[ch]
                    for ch in self.data[p + 1 : p + self.seq_length + 1]
                ]

                # sample from the model now and then
                if epoch % checkpoint_range == 0 and verbose:
                    sample_ix = self.sample(hprev, inputs[0], 200)
                    txt = "".join(self.ix_to_char[ix] for ix in sample_ix)
                    print("-" * 30, f"\n {txt} \n", "-" * 30)

                # forward seq_length characters through the net and fetch gradient
                loss, dWxh, dWhh, dWhy, dbh, dby, hprev = self.calc_gradients(
                    inputs, targets, hprev
                )
                smooth_loss = smooth_loss * 0.999 + loss * 0.001
                if epoch % checkpoint_range == 0:
                    print(f"Iteration: {epoch}; Loss: {smooth_loss}")

                    # Save Model
                    if save_model:
                        self.save_model_params(model_dir, epoch)

                # perform parameter update with Adagrad
                for param, dparam, mem in zip(
                    [
                        self.model_weights["xh"],
                        self.model_weights["hh"],
                        self.model_weights["hy"],
                        self.model_biases["xh"],
                        self.model_biases["hy"],
                    ],
                    [dWxh, dWhh, dWhy, dbh, dby],
                    [mWxh, mWhh, mWhy, mbh, mby],
                ):
                    mem += dparam * dparam
                    param += (
                        -learning_rate * dparam / np.sqrt(mem + 1e-8)
                    )  # adagrad update

                p += self.seq_length  # move self.data pointer
            print("Training Complete.")

        except KeyboardInterrupt:
            print("Halting Training.")

        # Save final weights & biases
        if save_model:
            print("Saving last checkpoint...")
            self.save_model_params(model_dir, "final")

    def predict_sequence(self, input_char: str, generated_str_len=200):
        hprev = np.zeros((self.n_hidden_neurons, 1))  # reset RNN memory

        sample_ix = self.sample(
            hprev, self.char_to_ix[input_char], generated_str_len
        )
        txt = "".join(self.ix_to_char[ix] for ix in sample_ix)
        print("-" * 30, f"\n {txt} \n", "-" * 30)


from cryptography.fernet import Fernet, base64, InvalidSignature, InvalidToken
import hashlib
from django.contrib.auth.hashers import make_password, PBKDF2PasswordHasher, BasePasswordHasher, get_random_string
import logging

log = logging.getLogger(__name__)
import requests

class JsonApi(object):

    @classmethod
    def get(cls, base_url, api_url):
        url = '{}{}'.format(base_url, api_url)
        data = {}
        response = None
        try:
            response = requests.get(url)
            response.raise_for_status()
            data = response.json()
        except Exception as exc:
            log.warning('GET failed: {} - {}'.format(url, exc))
            if response is not None and hasattr(response, 'content'):
                log.warning('RESPONSE {}'.format(response.content))
        finally:
            return data

    @classmethod
    def post(cls, base_url, api_url, data):
        url = '{}{}'.format(base_url, api_url)
        response_data = {}
        response = None
        try:
            response = requests.post(url, json=data)
            response.raise_for_status()
            if response.status_code == 201:
                log.info('Peer {} accepted block.'.format(base_url))
            if not len(response.content):
                if response.status_code == 304:
                    log.warning('Peer {}: unable to accept block.'.format(base_url))
            else:
                response_data = response.json()
        except Exception as exc:
            log.warning('POST failed: {} - {}'.format(url, exc))
            if response is not None and hasattr(response, 'content'):
                log.warning('RESPONSE {}'.format(response.content))
        finally:
            return response_data


import os
import argparse
import numpy as np
import torch
import pandas as pd
import data
import setGPU

from robustbench.data import load_cifar10, load_cifar10c
from robustbench.utils import clean_accuracy

from resnet import ResNet18
from models.c_models import PreActResNet18

def corr_eval(x_corrs, y_corrs, model):
    model.eval()
    res = np.zeros((5, 15))
    for i in range(1, 6):
        for j, c in enumerate(data.corruptions):
            res[i-1, j] = clean_accuracy(model, x_corrs[i][j].cuda(), y_corrs[i][j].cuda())
            print(c, i, res[i-1, j])

    return res

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', default=128, type=int)
    parser.add_argument('--data_dir', default='./data', type=str)
    parser.add_argument('--checkpoint', default='', type=str)
    parser.add_argument('--us_gpu', action='store_true')
    parser.add_argument('--output', default='output.csv', type=str)
    parser.add_argument('--only_clean', action='store_true')
    parser.add_argument('--n_samples', default=1000, type=int)
    return parser.parse_args()

corruptions = ["shot_noise", "motion_blur", "snow", "pixelate",
               "gaussian_noise", "defocus_blur", "brightness", "fog",
               "zoom_blur", "frost", "glass_blur", "impulse_noise", "contrast",
               "jpeg_compression", "elastic_transform"]

def main():
    args = get_args()
    device = "cuda"
    #os.environ["CUDA_VISIBLE_DEVICES"] = str("6")
    x_clean, y_clean = load_cifar10(n_examples=args.n_samples, data_dir=args.data_dir)

    mu = torch.tensor([0.485, 0.456, 0.406], dtype=torch.float, device=device).unsqueeze(-1).unsqueeze(-1)
    std = torch.tensor([0.229, 0.224, 0.225], dtype=torch.float, device=device).unsqueeze(-1).unsqueeze(-1)

    models_info = [["FrankWolfe", "nets/cifar_adv_training_attack-frank_eps-0.005.pth", "dict", None],
#                ["Wass 0.01", "models/cifar_adv_training_attack-l1wass_eps-0.01_epoch-30.pth", "dict", None],
#                ["Wass 0.005", "models/cifar_adv_training_attack-l1wass_eps-0.005_epoch-30.pth", "dict", None],
#                ["Wass 0.002", "models/cifar_adv_training_attack-l1wass_eps-0.002_epoch-30.pth", "dict", None],
                #  ["Wass 0.005", "models/cifar_frank_eps-0.005_epoch-30.pth", "dict", None],
                 ["Wass 0.001", "nets/cifar_frank_eps-0.001_epoch-30.pth", "dict", None],
                #  ["Wass 0.001 (60ep)", "models/cifar_frank_eps-0.001_epoch-60-c.pth", "dict", None],
                 ["Wass 0.001 (150ep)", "nets/cifar_frank_eps-0.001_epoch-150.pth", "dict", None],
                 ["Revnet 0.00025", "nets/cifar_revnet_eps-0.00025_alpha-0.01_epoch-30.pth", "dict", None],
                 ["Revnet 0.004", "nets/cifar_revnet_eps-0.004_alpha-0.01_epoch-30.pth", "dict", None],
                 ["Revnet 0.016", "nets/cifar_revnet_eps-0.016_alpha-0.01_epoch-30.pth", "dict", None],
                 #  ["Wass 0.00075", "models/cifar_frank_eps-0.00075_epoch-30.pth", "dict", None],
                #  ["Wass 0.0005", "models/cifar_frank_eps-0.0005_epoch-30.pth", "dict", None],
                #  ["Wass 0.0001", "models/cifar_frank_eps-0.0001_epoch-30.pth", "dict", None],
#                ["Wass 0.0008 (a,51ep)", "models/cifar_l1wass_eps-0.0008_alpha-0.005_epoch-51.pth", "dict", None],
#                ["Wass 0.01 (2)", "models/cifar_adv_training_attack-l1wass_eps-0.01_epoch-20.pth", "dict", None],
#                ["Wass 0.001 (2)", "models/cifar_adv_training_attack-l1wass_eps-0.001_epoch-20.pth", "dict", None],
#                ["Wass 0.0005 (2)", "models/cifar_adv_training_attack-l1wass_eps-0.0005_epoch-20.pth", "dict", None],
                ["L2", "nets/l2-at-eps=0.1-cifar10.pt", "pre", None],
                ["Standard", "nets/cifar_vanilla.pth", "net", None]]

    def print_info(name, elems):
        print("{} \t".format(name.ljust(20)), end="")
        for i in elems:
            print("{} \t".format(i.ljust(15)), end="")
        print()

    prnt = []
    for model in models_info:
        if model[2] == "dict":
            model[-1] = ResNet18().cuda()
            ckpt = torch.load(model[1])
            model[-1].load_state_dict(ckpt["model_state_dict"])
            model[-1].eval()
        elif model[2] == "net":
            model[-1] = ResNet18().cuda()
            ckpt = torch.load(model[1])
            if "net" in ckpt.keys():
                for key in ckpt["net"].keys():
                    assert "module" in key
                ckpt["net"] = dict((key[7:], value) for key, value in ckpt["net"].items())
                model[-1].load_state_dict(ckpt["net"])
            model[-1].eval()
        elif model[2] == "pre":
            model[-1] = PreActResNet18(n_cls=10, model_width=64, cifar_norm=True).cuda()
            model[-1].load_state_dict(torch.load(model[1])['last'])
            model[-1].eval()
        prnt += [model[0]]
    print_info("Corruption", prnt)

    prnt = []
    acc_cln = [0 for i in models_info]
    for i, model in enumerate(models_info):
        net = model[-1]
        if model[2] == "pre":
            acc_cln[i] = clean_accuracy(net, x_clean.cuda(), y_clean.cuda())
        elif model[2] == "dict" or model[2] == "net":
            acc_cln[i] = clean_accuracy(net, ((x_clean.cuda() - mu) / std), y_clean.cuda())
        prnt += ["{:.4f}".format(acc_cln[i])]
    print()
    print_info("Clean", prnt)

    mean_acc = [[0 for i in models_info] for j in range(6)]
    tot_mean = [0 for i in models_info]
    cnt = 0
    for corr in corruptions:
        print()
        for i in range(1,6):
            x_corr, y_corr = load_cifar10c(n_examples=args.n_samples, severity=i, corruptions=(corr,), data_dir=args.data_dir)
            cnt += 1

            prnt = []
            acc_corr = [0 for i in models_info]
            for j, model in enumerate(models_info):
                net = model[-1]
                if model[2] == "pre":
                    acc_corr[j] = clean_accuracy(net, x_corr.cuda(), y_corr.cuda())
                elif model[2] == "dict" or model[2] == "net":
                    acc_corr[j] = clean_accuracy(net, ((x_corr.cuda() - mu) / std), y_corr.cuda())
                mean_acc[i][j] += acc_corr[j]
                tot_mean[j] += acc_corr[j]
                prnt += ["{:.4f}".format(acc_corr[j])]
            print_info("{} {}".format(corr, i), prnt)

    for i in range(1,6):
        prnt = []
        for acc in mean_acc[i]:
            prnt += ["{:.4f}".format(acc / cnt * 5)]
        print()
        print_info("Average {}".format(i), prnt)

    prnt = []
    for acc in tot_mean:
        prnt += ["{:.4f}".format(acc / cnt)]
    print()
    print_info("Total Average", prnt)

    # if args.only_clean:
    #     return

    # x_corrs, y_corrs, _, _ = data.get_cifar10_numpy()

    # corr_res_last = corr_eval(x_corrs, y_corrs, model)
    # corr_data_last = pd.DataFrame({i+1: corr_res_last[i, :] for i in range(0, 5)}, index=data.corruptions)
    # corr_data_last.loc['average'] = {i+1: np.mean(corr_res_last, axis=1)[i] for i in range(0, 5)}
    # corr_data_last['avg'] = corr_data_last[list(range(1,6))].mean(axis=1)
    # corr_data_last.to_csv(args.output)
    # print(corr_data_last)

if __name__ == "__main__":
    main()


from django.http import HttpResponse
from datetime import date, time, datetime
from django.template import Template, Context
from App1.models import *
def saludo(request):
	return HttpResponse("Hola Django - Coder")

def quien_soy(request):
    return HttpResponse("Mi nombre es Mariana, tengo 34 años, tengo dos hijos y un marido hermoso")
def diadehoy(request):
    dia= date.today()
    documentoDeTexto= f" Hoy es día:
{dia}"
    return HttpResponse(documentoDeTexto)
def nombredeltipo(self, nombre):
    docdetexto= f"Mi nombre es

 {nombre}"
    return HttpResponse(docdetexto)
def pruebaTemplate(request):
    nom= "Pedrito"
    ap= "Urzua"
    diaDeFabr= datetime.today
    diccionario= {"nombre":nom, "apellido": ap, "dia":diaDeFabr}

    miHtml= open("H:/Mi unidad/data/python/dj_proy_1/prueba1/plantillas/template1.html")
    plantilla= Template(miHtml.read())
    miHtml.close()
    miContexto= Context(diccionario)
    documento=plantilla.render(miContexto)
    return HttpResponse(documento)

def curso1(request):
    curso1= curso(nombre="SQL",comision=1)
    curso1.save()
    documentoDeTexto= f"----> Curso: {curso1.nombre} Comision: {curso1.comision}"
    return HttpResponse(documentoDeTexto)
def alumnos1(request):
    alumnos1= estudiantes(nombre="Pedro", apellido= "urzua", email= 'pedro@gmail.com')
    alumnos1.save()
    diccionario= {"nombre":alumnos1.nombre, "apellido":alumnos1.apellido, "Correo":alumnos1.email}
    miHtml= open("H:/Mi unidad/data/python/dj_proy_1/prueba1/plantillas/template2.html")
    plantilla= Template(miHtml.read())
    miHtml.close()
    miContexto= Context(diccionario)
    documento=plantilla.render(miContexto)
    return HttpResponse(documento)
def entregable1(request):
    entregable1= entregable(nombre= "pedrourzua", fechaDeEntrega="2023-01-23", entregado= False)
    entregable1.save()
    docdetext= f"El alumno {entregable1.nombre} entrego la prueba: {entregable1.entregado} el dia:{entregable1.fechaDeEntrega}"
    return HttpResponse(docdetext)


import bpy
import random
import mathutils
import sys

import os

sys.path.append(os.getcwd())

from math_utilities import Math_Utilities
from tscn import TSCN
from materials import Material

class Convert_Particles():

    def __init__(self):
        self.math = Math_Utilities()
        self.depg = bpy.context.evaluated_depsgraph_get()
        self.blend_filename = os.path.basename(bpy.data.filepath)

        self.tscn = TSCN()

        self.material = Material()
        self.material_indices = {}

        self.blend_particle_dict = {"Emitter Objects" : {}}

    def deselect_all(self):
        for obj in bpy.context.scene.objects:
            obj.select_set(False)

    def export_object(self, obj, project_dir):

        bpy.context.scene.collection.objects.link(obj)

        obj.select_set(True)

        mat = mathutils.Matrix( [ [1, 0, 0, 0],
                                [0, 1, 0, 0],
                                [0, 0, 1, 0],
                                [0, 0, 0, 1] ])

        obj.matrix_world = mat
        bpy.ops.export_scene.obj(filepath=f"{project_dir}/{obj.name}.obj", use_selection=True, use_triangles=True, use_normals=True)
        obj.select_set(False)

        bpy.context.scene.collection.objects.unlink(obj)

    def convert(self, project_dir):

        self.material.get_all_objects_materials(project_dir)

        self.deselect_all()

        temp_objects = []

        for obj in bpy.context.scene.objects:

            for modifier in obj.modifiers:
                if modifier.type == "PARTICLE_SYSTEM":

                    # Object Has Particle System
                    # Get Object properties

                    # Get Object 3D Transform
                    obj_scale = obj.scale
                    obj_location = obj.location
                    obj_rotation = obj.rotation_euler

                    obj_s = self.math.S3D(obj_scale[0], obj_scale[2], obj_scale[1])
                    obj_r = self.math.R(obj_rotation[0], obj_rotation[2], obj_rotation[1])
                    obj_t = self.math.T(obj_location[0], obj_location[2], obj_location[1])

                    obj_transform = self.math.to_str(self.math.transform(obj_r, obj_s, obj_t))

                    # Other
                    obj_name = obj.name

                    self.blend_particle_dict["Emitter Objects"][obj_name] = {"Transform" : obj_transform, "Particle Objects" : {}}

                    # Get Object Particle Systems
                    obj_particle_systems = obj.evaluated_get(self.depg).particle_systems

                    for particle_system_idx, particle_system in enumerate(obj_particle_systems):

                        # Get particle system properties
                        num_of_particles = len(particle_system.particles)
                        particle_system_name = particle_system.name
                        particle_system_settings = particle_system.settings
                        particle_system_objects_dict = {}

                        # Get Particle Objects
                        if particle_system_settings.render_type == "COLLECTION":

                            for particle_obj_idx, particle_obj in enumerate(particle_system_settings.instance_collection.objects):

                                self.export_object(particle_obj, project_dir)

                                particle_system_objects_dict[particle_system_idx + particle_obj_idx] = {"Object" : particle_obj, "Random Weight" : 1, "Scale" : particle_obj.dimensions}

                                self.blend_particle_dict["Emitter Objects"][obj_name]["Particle Objects"][particle_obj.name] = particle_system_objects_dict

                        elif particle_system_settings.render_type == "OBJECT":

                            particle_obj_idx = 0
                            particle_obj = particle_system_settings.instance_object

                            self.export_object(particle_obj, project_dir)

                            particle_system_objects_dict[particle_system_idx + particle_obj_idx] = {"Object" : particle_obj, "Random Weight" : 1, "Scale" : particle_obj.dimensions}

                            self.blend_particle_dict["Emitter Objects"][obj_name]["Particle Objects"][particle_obj.name] = particle_system_objects_dict

                        particle_indices = []
                        particle_index_weights = []

                        for key, val in particle_system_objects_dict.items():

                            if not val['Object'].name in temp_objects:

                                self.tscn.add_ext_resource(f"{val['Object'].name}.obj", "ArrayMesh")

                                # ob_material = [mk for mk, mv in self.material.materials_dict.items() if mv["Object"] == val['Object'].name]
                                # if len(ob_material) > 0:
                                #     self.tscn.add_ext_resource(f"{ob_material[0]}.tres", "Material")

                                temp_objects += [val['Object'].name]

                            particle_indices += [key]
                            particle_index_weights += [val['Random Weight']]

                        particle_transforms_dict = {}
                        # Loop Through all Particles
                        for particle in particle_system.particles:

                            particle_obj_id = random.choices(particle_indices, weights=particle_index_weights, k=1)[0]

                            particle_obj = particle_system_objects_dict[particle_obj_id]
                            particle_obj_scale = particle_obj['Scale']

                            particle_loc = particle.location
                            particle_rot = particle.rotation.to_euler()

                            particle_r = self.math.Ry(particle_rot[2])
                            particle_s = self.math.S3D(particle.size * particle_obj_scale[0],
                                                       particle.size * particle_obj_scale[2],
                                                       particle.size * particle_obj_scale[1])
                            particle_t = self.math.T(particle_loc[0], 0.0, particle_loc[1])

                            particle_transform = self.math.to_str(self.math.transform(particle_r,
                                                                                particle_s,
                                                                                particle_t))

                            if not particle_obj_id in particle_transforms_dict:
                                #particle_transforms_dict[particle_obj_id] = []
                                particle_transforms_dict[particle_obj_id] = particle_transform
                            else:
                                particle_transforms_dict[particle_obj_id] += particle_transform

                        k = 0
                        for key, val in particle_transforms_dict.items():

                            sub_res_properties = {}
                            sub_res_properties["transform_format"] = 1
                            sub_res_properties["instance_count"] = int(len(val) / 12)
                            sub_res_properties["mesh"] = f"ExtResource( {particle_system_idx + k + 1} )"
                            sub_res_properties["transform_array"] = f"PoolVector3Array( {', '.join(val)} )"

                            self.tscn.add_sub_resource(res_type="MultiMesh", res_properties=sub_res_properties)

                            k += 1

                    break

            ob_material = [mk for mk, mv in self.material.materials_dict.items() if mv["Object"] == obj.name]
            if len(ob_material) > 0:
                self.tscn.add_ext_resource(f"{ob_material[0]}.tres", "Material")

        self.tscn.add_node(node_name=f"{os.path.splitext(self.blend_filename)[0]}", node_type="Spatial", node_parent=None, node_properties=None)

        print(self.blend_particle_dict["Emitter Objects"])

        current_obj = 1
        for key, val in self.blend_particle_dict["Emitter Objects"].items():
            ob_name = key
            ob_transform = val['Transform']
            ob_particle_objects = val["Particle Objects"]

            ob_materials = [mk for mk, mv in self.material.materials_dict.items() if mv["Object"] == ob_name]

            node_properties = {}
            node_properties["transform"] = f"Transform( {', '.join(ob_transform)} )"

            ob_material_name = bpy.context.scene.objects[key].active_material.name
            node_properties['material_override'] = f"ExtResource( {self.tscn.get_ext_resource_by_name(ob_material_name) + 1} )"

            self.tscn.add_node(node_name=ob_name, node_type="Spatial", node_parent=".", node_properties=node_properties)

            #print(ob_particle_objects)

            i = 0
            for k, v in ob_particle_objects.items():

                part_node_properties = {}
                part_node_properties['multimesh'] = f"SubResource( {current_obj + i} )"

                print("k", k)

                print(len(ob_materials))

                #if len(ob_materials) > 0:
                part_ob_material_name = bpy.context.scene.objects[k].active_material.name
                part_node_properties['material_override'] = f"ExtResource( {self.tscn.get_ext_resource_by_name(part_ob_material_name) + 1} )"

                part_ob_name = k

                self.tscn.add_node(node_name=part_ob_name, node_type="MultiMeshInstance", node_parent=ob_name, node_properties=part_node_properties)

                i += 1

            current_obj += 1

        self.tscn.write(f"{project_dir}/test.tscn")


import argparse
import librosa
import os
import soundfile as sf
ROOTPATH = 'input/tmp/'

def convert(audir):
    absdir = os.path.join(ROOTPATH, audir)
    name = audir.split('.')[-2]
    audio, sr = librosa.load(absdir, sr=16000)
    sf.write(ROOTPATH + name + '.flac', audio, sr, 'PCM_16')
    os.remove(absdir)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--audir', action='store', type=str, default='')
    args = parser.parse_args()
    audir = args.audir
    convert(audir)


from sklearn.ensemble import RandomForestClassifier
import pandas as pd

configdf = pd.read_excel('config.xlsx', sheet_name='CONFIG')
def random_forest(x_train, y_train, x_test):
    iter = configdf['var'][7].astype(int)
    Method = configdf['Method'][7]
    clasi = RandomForestClassifier(n_estimators=iter)
    clasi.fit(x_train, y_train)
    out = clasi.predict(x_test)
    return out


from cgi import parse_qs
from wsgiref.simple_server import make_server
import json

def wsgi_appication(environ,start_response):
    print("wsgi handler")
    response = ""

    #for key, val in sorted(environ.items()):
    #    response += "%s: %s\n" % (key, val)
    #print(response)

    try:
        body_size = int(environ.get('CONTENT_LENGTH', 0))
    except ValueError:
        body_size = 0

    body = environ['wsgi.input'].read(body_size)

    d = parse_qs(body)
    x_list = d.get('x'.encode(), [0])
    y_list = d.get('y'.encode(), [0])

    rx = int(x_list[0])
    ry = int(y_list[0])

    sum = rx + ry
    mul = rx * ry
    print("sum : %d, mul : %d" % (sum, mul))

    response_json = json.dumps({'sum':sum, 'mul':mul})

    start_response("200 OK", [('Content-Type','application/json; charset=utf-8')])
    return [response_json.encode()]

if __name__ == "__main__":
    print("Start Program")
    httpd = make_server("", 8001, wsgi_appication)
    httpd.serve_forever()


from flask import Flask

from extensions import bcrypt, db, giphyApi, jwt, cors
from gif import gifs
from user import users
from error_handling import register_errors

def create_app():
    app = Flask(__name__)
    app.config.from_envvar("ENV_FILE")

    register_blueprints(app)

    register_extensions(app)

    register_errors(app)

    return app

def register_blueprints(app):
    origins = app.config.get("CORS_ORIGIN_WHITELIST")
    cors.init_app(gifs.blueprint, origins=origins, supports_credentials=True)
    cors.init_app(users.blueprint, origins=origins, supports_credentials=True)

    app.register_blueprint(gifs.blueprint)
    app.register_blueprint(users.blueprint)

def register_extensions(app):
    bcrypt.init_app(app)
    db.init_app(app)
    giphyApi.init_app(app)
    jwt.init_app(app)